Fonts
A yuhwa SVG names its fonts. It does not carry the font files. Whoever shows the SVG supplies the fonts: the page, the browser, the operating system, or the PNG renderer. This page covers which fonts you can name and where each one works.
Picking a font
Set the font for one diagram with the font setting, or for every diagram that uses a theme with the theme's font key:
font: Georgia
font-size: 15
client "Browser" -> api "API" -> db "Orders" [shape: cylinder]theme {
base: paper
font: Inter
font-size: 14
}| value | font stack in the SVG |
|---|---|
| none | the theme's font. Every built-in theme uses sans |
sans | Inter, ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif |
mono | 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace |
serif | ui-serif, Georgia, Cambria, 'Times New Roman', Times, serif |
| any other name | that name, then ui-sans-serif, system-ui, sans-serif |
| a monospaced name | a name containing mono, code or courier is set as monospace, then ui-monospace, monospace |
Any font family name works: a system font such as Georgia or Menlo, or a web font the page loads, such as Inter or IBM Plex Sans. The diagram falls back to the rest of the stack when the font is missing, so it never shows without text.
Fallback fonts
Name more fonts, separated by commas, to pick what shows when the first one is missing. Each viewer sees the first font in the list that they have:
font: Lora, Georgia, serif
a "Lora, or Georgia" -> b "or any serif font"sans, serif and mono in a list stand for their whole stack, so font: Lora, serif falls back to Georgia, Cambria and the other serif fonts. A list that does not end in sans, serif, mono or a CSS generic family (sans-serif, serif, monospace, system-ui …) gets the sans or mono ending from the table. Whether the diagram counts as monospace depends on the first font only.
The visual editor suggests fonts for the font setting and says which font of the list it really shows. When the first font is missing on your computer, it names the fallback.
A node, edge, group or panel takes its own font the same way: a "Title" [font: serif], or [font: "Lora, serif"] for a list. A span sets the font of part of a text: "{font: mono | v2} release". See text.
Text weight comes from font-weight on a node, edge, group or panel, and from the theme (titles are 600). A weight only shows when the font has it. A font with only a regular face shows bold text as regular, or as a faked bold.
Card bodies
The body lines of a card are always monospace, so code and columns line up. They use the mono stack unless the diagram font is itself monospaced. mono: false on the node sets the body in the diagram font instead.
Where fonts come from
| how the SVG is shown | fonts it can use |
|---|---|
inline in a page: <yuhwa-diagram>, the script tag, docs output: 'svg', innerHTML | the page's web fonts and system fonts |
as an image: <img src="x.svg">, docs plugins (default), yuhwa md output, GitHub, image URLs | system fonts only |
a browser canvas or PNG: renderCanvas, svgToCanvas, the editor's PNG, render="canvas", docs output: 'canvas' | system fonts, plus the page's web fonts, which yuhwa embeds |
a CLI PNG: yuhwa render -o x.png, docs plugins with output: 'png' | system fonts, plus the files you pass with --font or png.fonts |
| an SVG file opened in an app (Figma, Inkscape, a browser tab) | fonts installed on the computer that opens it |
Inline SVG
An inline SVG is part of the page, so it uses the page's fonts like any other text. Load the font as you normally would, then name it:
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" />
<script src="https://cdn.jsdelivr.net/npm/yuhwa/dist/yuhwa.iife.js"></script>
<pre><code class="language-yuhwa">
font: Inter
a -> b -> c
</code></pre>In the browser, yuhwa measures text with a canvas in the diagram's font, so boxes fit the text exactly. A web font that has not loaded yet is measured with its fallback. The script tag, <yuhwa-diagram> and renderCanvas therefore load the diagram's web fonts, in the weights it uses, and draw the diagram again once they arrive.
When you call render yourself, loadFonts(svg) does the same step. It resolves true when a font arrived, and the diagram should be rendered again:
import { render, loadFonts } from 'yuhwa/browser';
let { svg } = render(source);
if (await loadFonts(svg)) ({ svg } = render(source));SVG as an image
A browser shows an SVG image in isolation. It cannot reach the page's fonts or load fonts from the network, so only fonts installed on the reader's computer apply. This covers <img> tags, CSS backgrounds, the image links that yuhwa md writes, SVGs on GitHub, and the yuhwa serve image URLs.
For an SVG that will be shown as an image, pick a font that readers have: the default sans, or a system font such as Georgia, Verdana or Courier New. If the diagram must use a web font, export a PNG instead.
Canvas and PNG in the browser
renderCanvas and svgToCanvas draw the SVG as an image, which normally means system fonts only. To keep web fonts, they first embed every @font-face font of the page that the diagram names, as data: URLs inside the SVG. A canvas drawn on a page that loads Inter therefore shows Inter.
import { renderCanvas } from 'yuhwa/browser';
const { canvas } = await renderCanvas(source, { scale: 2 });
canvas.toBlob((png) => save(png), 'image/png');The fonts option controls what is embedded:
| value | embeds |
|---|---|
'page' | the page's @font-face fonts the diagram names (default) |
| a list of fonts | those files, e.g. a font the page does not load |
false | nothing: system fonts only |
await svgToCanvas(svg, {
fonts: [
{ family: 'Inter', url: '/fonts/Inter.woff2', weight: '100 900' },
{ family: 'Inter', url: '/fonts/Inter-Italic.woff2', weight: '100 900', style: 'italic' },
],
});Embedding works with:
@font-facerules in the page's stylesheets, including<style>tags,@import, and Google Fonts or other font CDNs;woff2,woff,ttfandotffiles.
Only the faces the diagram needs are embedded:
- rules for a
unicode-rangethe diagram never uses are skipped, so a Google Fonts family pulls only the subsets it needs; - of a family offered in many weights, only the weights the diagram uses are embedded. A weight with no face of its own gets the face a browser would pick, e.g.
700for a650title. Italic faces come along only when the diagram has italic text.
It does not work with:
- fonts added from JavaScript with
new FontFace(...): they have no stylesheet rule to read. Pass them infontsinstead; - font files on another origin that send no CORS header. yuhwa must fetch the file to embed it. Google Fonts and most font CDNs allow this. A font that cannot be fetched is skipped, and the text falls back to system fonts;
- fonts that are only installed on the computer. They need no embedding: the canvas uses them directly.
PNG from the command line
yuhwa render -o x.png draws with resvg, through the optional @resvg/resvg-js package. Install it next to yuhwa first:
npm install -D @resvg/resvg-jspnpm add -D @resvg/resvg-jsrender shows how to run it once without installing anything.
resvg reads the fonts installed on the system. Pass font files, or folders of them, with --font. It can be repeated:
yuhwa render diagram.yu -o diagram.png --font fonts/Inter.ttf
yuhwa render diagram.yu -o diagram.png --font ./fonts --font ~/Library/FontsThe family name in the diagram must match the font's internal family name, as a font viewer shows it. resvg reads ttf, otf and ttc files. A woff or woff2 file is an error; in a folder, such files are ignored.
In CI and containers, few fonts are installed. Install the ones your diagrams name, or commit them and pass --font, so the PNG looks the same on every machine. --no-system-fonts ignores installed fonts entirely, so a machine with extra fonts cannot change the result. Text in a family that none of the --font files provide is then left out, so pass a font for every family the diagram uses, including the monospace one for card bodies:
yuhwa render diagram.yu -o diagram.png --font ./fonts --no-system-fontsMeasuring text
yuhwa sizes boxes to fit their labels, so it must know how wide text is.
| where | how text is measured |
|---|---|
| browser | a canvas in the diagram's font: exact for that font |
| Node, the CLI | a table of character widths tuned for Inter: close for most sans |
| your own function | measure or measureFor, see the API |
In Node, a much wider or narrower font than Inter can leave labels tight or loose in their boxes. Pass a measure that knows the font, for example one built on @napi-rs/canvas with the font registered.