API¶
- class ziafont.font.Font(name: str | Path | None = None, searchpaths: Sequence[str | Path] | None = None)¶
Class to read/parse a OpenType/TTF and write glyphs to SVG
- Parameters:
name – File name of the font. Defaults to DejaVuSans.
- advance(glyph: int, glyph2: int | None) int ¶
Get advance width in font units
- getsize(s) tuple[float, float] ¶
Calculate width and height (including ascent/descent) of string
- glyph(char: str) SimpleGlyph ¶
Get the Glyph for the character
- glyph_fromid(glyphid: int) SimpleGlyph | CompoundGlyph ¶
Read a glyph from the “glyf” table
- Parameters:
glyphid – Glyph index used to find glyph data
- glyphindex(char: str) int ¶
Get index of character glyph
- language(script, language)¶
Set script/language to use
- languages(script='DFLT')¶
Get list of languages in the script
- scripts()¶
Get list of scripts in the font
- text(s: str, size: float | None = None, linespacing: float = 1, halign: Literal['left', 'center', 'right'] = 'left', valign: Literal['base', 'center', 'top'] = 'base', color: str | None = None, rotation: float = 0, rotation_mode: str = 'anchor')¶
Create a Text object using this font
- Parameters:
s – String to convert.
size – Font size in points
linespacing – Space between lines
halign – Horizontal Alignment
valign – Vertical Alignment
color – Color for string
rotation – Rotation angle in degrees
rotation_mode – Either ‘default’ or ‘anchor’, to mimic Matplotlib behavoir. See: https://matplotlib.org/stable/gallery/text_labels_and_annotations/demo_text_rotation_mode.html
kern – Use font kerning adjustment
- usecmap(cmapidx: int) None ¶
Select cmap table by index. Only supported tables are included.
- verifychecksum() None ¶
Verify checksum of all font tables. Raises ValueError if invalid
- class ziafont.font.Text(s: str | Sequence[int], font: str | Font | None = None, size: float | None = None, linespacing: float = 1, halign: Literal['left', 'center', 'right'] = 'left', valign: Literal['base', 'center', 'top'] = 'base', color: str | None = None, rotation: float = 0, rotation_mode: str = 'anchor')¶
Draw text as SVG paths
- Parameters:
s – String to draw
font – Font name or ziafont.Font to use
size – Font size in points
linespacing – Spacing between lines
color – Color for string
halign – Horizontal Alignment
valign – Vertical Alignment
rotation – Rotation angle in degrees
rotation_mode – Either ‘default’ or ‘anchor’, to mimic Matplotlib behavoir. See: https://matplotlib.org/stable/gallery/text_labels_and_annotations/demo_text_rotation_mode.html
- drawon(svg: Element, x: float = 0, y: float = 0)¶
Draw text on the SVG
- getsize() tuple[float, float] ¶
Calculate width and height (including ascent/descent) of string
- getyofst() float ¶
Y-shift from bottom of bbox to 0
- svg() str ¶
Get SVG string
- svgxml() Element ¶
Get SVG XML element
- class ziafont.glyph.SimpleGlyph(index: int, operators: Sequence[SVGOpType], bbox: BBox, font: Font)¶
Simple Glyph
- advance(nextchr: 'SimpleGlyph' | None = None) int ¶
Get advance width in glyph units
- property char: set[str]¶
Get set of unicode character represented by this glyph
- describe() DescribeGlyph ¶
Get Glyph Test representation showing vertices and borders
- funits_to_points(value: float, scale_factor: float = 1) float ¶
Convert font units to points
- place(x: float, y: float, point_size: float) Element | None ¶
Get <use> svg tag translated/scaled to the right position
- svg(point_size: float | None = None) str ¶
Get SVG as string
- svgpath(x0: float = 0, y0: float = 0, scale_factor: float = 1) Element | None ¶
Get svg <path> element for glyph, normalized to 12-point font
- svgsymbol() Element ¶
Get svg <symbol> element for this glyph, scaled to 12-point font
- svgxml(point_size: float | None = None) Element ¶
Standalong SVG
- test(pxwidth: float = 400, pxheight: float = 400) InspectGlyph ¶
Get Glyph Test representation showing vertices and borders
- property viewbox: tuple[float, float, float, float]¶
Get viewbox of Glyph
- class ziafont.glyph.CompoundGlyph(index: int, glyphs: GlyphComp, font: Font)¶
Compound glyph, made of multiple other Glyphs
- class ziafont.config.Config(svg2: bool = True, debug: bool = False, fontsize: float = 48, precision: float = 3)¶
Global configuration options for Ziafont
- svg2¶
at the expense of SVG size
- Type:
Use SVG2.0. Disable for better browser compatibility
- debug¶
- Type:
Debug mode, draws bounding boxes around text
- fontsize¶
- Type:
Default font size in points
- precision¶
- Type:
Decimal precision for SVG coordinates
Font and Glyph Inspection¶
- class ziafont.inspect.DescribeFont(font: Font)¶
Display Font Metadata in HTML table
- format_languages()¶
Format the scripts/languages in the font
- table()¶
Generate the HTML table
- class ziafont.inspect.InspectGlyph(glyph: SimpleGlyph, pxwidth: float = 400, pxheight: float = 400)¶
Draw glyph svg with test/debug lines
- svg() str ¶
Glyph SVG string
- svgxml() Element ¶
Glyph svg as XML element tree
- class ziafont.inspect.DescribeGlyph(glyph: SimpleGlyph)¶
HTML Table of Glyph information
- describe()¶
HTML table with glyph parameters
- class ziafont.inspect.ShowGlyphs(font, size: float = 36, columns: int = 15, nmax: int | None = None)¶
Show all glyphs in the font in HTML table
- table() str ¶
Build HTML table
- class ziafont.inspect.ShowFeature(featname, font, size: float = 36)¶
Show all lookup tables in a feature