Fonts¶
Building fonts from drawings; see Making fonts. svg.FontBuilder is also
available at the top level.
Fonts from drawings: letters, icons, kerning, OpenType and web-font export.
FontBuilder collects glyphs drawn with svgwriter shapes and writes
.otf, .ttf, .woff and .woff2 files, an icon stylesheet and a
cheatsheet. Compiling needs the fonts extra (pip install 'pysvgwriter[fonts]').
Examples
>>> import svgwriter as svg
>>> fb = svg.fonts.FontBuilder(family="Shapes")
>>> fb.icon("square", svg.rect(0, 0, 10, 10))
''
- class FontBuilder(family: str = 'Untitled', style: str = 'Regular', weight: int = 400, *, units_per_em: int = 1000, ascent: int = 800, descent: int = 200, cap_height: int = 700, x_height: int = 500, line_gap: int = 0, scale: float = 1.0, version: str = '1.000')[source]¶
Bases:
objectBuild a font from drawings: letters from shapes, icons on private-use code points.
Drawings keep svgwriter’s y-down coordinates. A glyph’s baseline is the drawing’s
y=0(move it withbaseline=), so letters are drawn above the baseline at negative y and descenders below it at positive y; one drawing unit isscalefont units. The outline is shifted so its left edge sitsleftunits after the glyph origin.- Parameters:
family (str, optional) – The family name. Defaults to “Untitled”.
style (str, optional) – The style name (“Regular”, “Bold”, “Italic”, …). Defaults to “Regular”.
weight (int, optional) – The CSS weight, 100 to 900. Defaults to 400.
units_per_em (int, optional) – Font units per em. Defaults to 1000.
ascent (int, optional) – Height above and depth below the baseline, in font units. Default to 800 and 200.
descent (int, optional) – Height above and depth below the baseline, in font units. Default to 800 and 200.
cap_height (int, optional) – Height of capitals and of lowercase “x”. Default to 700 and 500.
x_height (int, optional) – Height of capitals and of lowercase “x”. Default to 700 and 500.
line_gap (int, optional) – Extra space between lines. Defaults to 0.
scale (float, optional) – Font units per drawing unit for every glyph. Defaults to 1.0.
version (str, optional) – The font version. Defaults to “1.000”.
See also
svgwriter.TypefaceUse the built font for text.
Examples
>>> import svgwriter as svg >>> fb = svg.FontBuilder(family="Blocks") >>> fb.glyph("I", svg.rect(0, -700, 120, 700), left=60, right=60) >>> fb.advance("I") 240 >>> fb.icon("dot", svg.circle(0, 0, 10)) '\ue000' >>> blocks = fb.typeface() >>> round(svg.text("I", font_family=blocks, font_size=1000).to_path().bbox.height) 700
- glyph(char: str, shape: Element | None = None, *, advance: float | None = None, left: float = 0, right: float = 0, baseline: float = 0, scale: float | None = None) None[source]¶
Add (or replace) the glyph for one character.
- Parameters:
char (str) – The character the glyph draws.
shape (Element, optional) – Any shape, text or group; fills and strokes both become outline, overlaps merge. Defaults to None, an empty glyph such as a space.
advance (float, optional) – The advance width in font units. Defaults to None: the outline’s width plus left and right.
left (float, optional) – Side bearings in font units. Default to 0.
right (float, optional) – Side bearings in font units. Default to 0.
baseline (float, optional) – The drawing y that becomes the baseline. Defaults to 0.
scale (float, optional) – Font units per drawing unit. Defaults to None, the builder’s scale.
- Raises:
ValueError – If char is not one character, or an empty glyph has no advance.
Examples
>>> import svgwriter as svg >>> fb = svg.FontBuilder(scale=10) >>> fb.glyph("l", svg.path("M0 -70 V0", stroke="black", stroke_width=8, fill="none"), left=40, right=40) >>> fb.advance("l") 160
- icon(name: str, shape: Element, *, size: float | None = None) str[source]¶
Add an icon on the next free private-use code point (U+E000 upward, in call order).
The drawing is scaled to fit the em (ascent plus descent, or size) and centered in a square advance. Adding a name again replaces its drawing and keeps its code point.
- Parameters:
- Returns:
str – The character that shows the icon.
Examples
>>> import svgwriter as svg >>> fb = svg.FontBuilder() >>> hex(ord(fb.icon("home", svg.rect(0, 0, 24, 24)))) '0xe000'
- kern(left: str, right: str, value: float) None[source]¶
Adjust the space between two glyphs.
- Parameters:
- Raises:
KeyError – If either glyph is missing.
Examples
>>> import svgwriter as svg >>> fb = svg.FontBuilder() >>> fb.glyph("A", svg.path("M0 0 L300 -700 L600 0 Z")) >>> fb.glyph("V", svg.path("M0 -700 L300 0 L600 -700 Z")) >>> fb.kern("A", "V", -80)
- advance(glyph: str) int[source]¶
A glyph’s advance width in font units.
- Parameters:
glyph (str) – A character or icon name.
- Returns:
int – The advance.
Examples
>>> import svgwriter as svg >>> fb = svg.FontBuilder() >>> fb.glyph(" ", advance=250) >>> fb.advance(" ") 250
- property icons: dict[str, str]¶
Each icon name with the character that shows it, in code point order.
Examples
>>> import svgwriter as svg >>> fb = svg.FontBuilder() >>> _ = fb.icon("a", svg.rect(0, 0, 1, 1)), fb.icon("b", svg.rect(0, 0, 1, 1)) >>> list(fb.icons.values()) ['\ue000', '\ue001']
- to_bytes(file_format: str = 'otf') bytes[source]¶
The compiled font.
- Parameters:
file_format ({"otf", "ttf", "woff", "woff2"}, optional) – The container, with or without a leading dot.
otf,woffandwoff2keep the cubic outlines (CFF);ttfconverts them to quadratic TrueType curves. Defaults to “otf”.- Returns:
bytes – The font file’s content.
Examples
>>> import svgwriter as svg >>> fb = svg.FontBuilder() >>> fb.glyph("o", svg.circle(250, -250, 250)) >>> fb.to_bytes(".ttf")[:4] b'\x00\x01\x00\x00'
- save(path: str | PathLike[str]) Path[source]¶
Write the font; the suffix (
.otf,.ttf,.woff,.woff2) picks the format.- Parameters:
path (str or os.PathLike) – The file to write.
- Returns:
pathlib.Path – The written file.
Examples
>>> import svgwriter as svg >>> fb = svg.FontBuilder(family="Hand") >>> # fb.save("Hand.otf"); fb.save("Hand.woff2")
- typeface() Typeface[source]¶
The font as a
Typeface, registered so text can use it at once.- Returns:
Typeface – The family, named after family.
Examples
>>> import svgwriter as svg >>> fb = svg.FontBuilder(family="Squares") >>> fb.glyph("x", svg.rect(0, -500, 500, 500)) >>> fb.typeface().name 'Squares'
- css(font: str, *, prefix: str = 'icon') str[source]¶
An icon stylesheet:
@font-facefor font and a.prefix-name::beforeclass per icon.- Parameters:
- Returns:
str – The CSS.
Examples
>>> import svgwriter as svg >>> fb = svg.FontBuilder(family="Icons") >>> _ = fb.icon("rocket", svg.rect(0, 0, 24, 24)) >>> '.icon-rocket::before { content: "\\e000"; }' in fb.css("Icons.woff2") True
- save_css(path: str | PathLike[str], font: str, *, prefix: str = 'icon') Path[source]¶
Write
css()to path.- Parameters:
path (str or os.PathLike) – The stylesheet to write.
font (str) – The font file’s URL relative to the stylesheet.
prefix (str, optional) – The class prefix. Defaults to “icon”.
- Returns:
pathlib.Path – The written stylesheet.
Examples
>>> import svgwriter as svg >>> fb = svg.FontBuilder(family="Icons") >>> # fb.save("Icons.woff2"); fb.save_css("icons.css", font="Icons.woff2")
- cheatsheet(columns: int | None = None) Document[source]¶
A document showing every glyph with its name and code point.
- Parameters:
columns (int, optional) – Cells per row. Defaults to None, six.
- Returns:
Document – The sheet; glyphs are drawn as paths, so it needs no font.
Examples
>>> import svgwriter as svg >>> fb = svg.FontBuilder() >>> _ = fb.icon("star", svg.paths.star(0, 0, 10)) >>> "U+E000" in fb.cheatsheet().to_string() True