Text Engine API¶
The text module handles font loading, text measurement, automatic line breaking, and vertical typesetting.
Module Overview¶
| Submodule | Responsibility |
|---|---|
text.font |
Font manager (FontManager), FreeType/Pillow backends |
text.shaper |
Text measurement, line breaking, alignment (measure_text, break_lines, align_lines) |
text.embed |
Font subsetting and WOFF2 embedding |
FontManager¶
Global singleton managing font discovery and loading.
from latticesvg.text.font import FontManager
fm = FontManager.instance()
path = fm.find_font("Noto Sans SC", weight="bold")
chain = fm.find_font_chain(["Times New Roman", "SimSun"], weight="normal")
Auto-generated API Docs¶
Font Management¶
font ¶
Font loading, glyph measurement, and caching via FreeType (with Pillow fallback).
FontInfo
dataclass
¶
Metadata for an indexed font.
GlyphMetrics
dataclass
¶
GlyphMetrics(advance_x: float, bearing_x: float, bearing_y: float, width: float, height: float, advance_y: float = 0.0, vert_origin_x: float = 0.0, vert_origin_y: float = 0.0)
Metrics for a single glyph at a given size.
FontManager ¶
Manages font discovery, loading, and glyph metric caching.
Usage::
fm = FontManager.instance()
path = fm.find_font(["Arial", "sans-serif"])
metrics = fm.glyph_metrics(path, 16, "A")
Source code in src/latticesvg/text/font.py
reset
classmethod
¶
add_font_directory ¶
find_font ¶
find_font(family_list: Optional[list] = None, weight: str = 'normal', style: str = 'normal') -> Optional[str]
Find a font file matching the requested family list.
Returns the first match found, or None.
Source code in src/latticesvg/text/font.py
find_font_chain ¶
find_font_chain(family_list: Optional[list] = None, weight: str = 'normal', style: str = 'normal') -> List[str]
Resolve each family to a font path, returning an ordered fallback chain.
Unlike :meth:find_font (which returns only the first match), this
method resolves every distinct family in family_list so that
characters missing in the primary font can be measured with a
fallback font.
When a generic family (e.g. "sans-serif") is given, all
concrete fonts reachable through the alias list are included in
the chain, giving a wide Unicode coverage.
Source code in src/latticesvg/text/font.py
glyph_metrics ¶
Return glyph metrics for char.
font_path may be a single path string or an ordered list of paths (a fallback chain). When a list is given the first font that contains a glyph for char is used.
Source code in src/latticesvg/text/font.py
font_family_name ¶
Get the CSS font-family name from a font file using FreeType.
Source code in src/latticesvg/text/font.py
get_font_path ¶
Return the filesystem path for family, or None if not found.
Unlike :meth:find_font, this method does not fall back to an
arbitrary default font when no match is found.
Source code in src/latticesvg/text/font.py
list_fonts ¶
Return a :class:FontInfo for every indexed font file.
Source code in src/latticesvg/text/font.py
parse_font_families ¶
Parse a CSS font-family value into a flat list of family names.
Accepts a comma-separated string, a list of strings, or None
(which falls back to ["sans-serif"]).
Source code in src/latticesvg/text/font.py
Text Shaping¶
shaper ¶
Text shaping — line breaking, alignment, and overflow handling.
Line
dataclass
¶
Line(text: str, width: float, x_offset: float = 0.0, char_count: int = 0, justified: bool = False, word_spacing_justify: float = 0.0, hyphenated: bool = False)
A single line of shaped text.
SpanFragment
dataclass
¶
SpanFragment(text: str, width: float, span_index: int, font_path: str = '', font_size: int = 16, svg_fragment: Optional[object] = None)
A fragment of a TextSpan that lives on one rendered line.
RichLine
dataclass
¶
RichLine(fragments: List[SpanFragment] = list(), width: float = 0.0, x_offset: float = 0.0, justified: bool = False, word_spacing_justify: float = 0.0, _cjk_justify: bool = False, hyphenated: bool = False)
A single line of rich (multi-span) text.
VerticalRun
dataclass
¶
A contiguous run of characters with the same orientation.
upright runs have each character standing upright (CJK style). sideways (rotated) runs are Latin/digit text rotated 90° CW as a group. combine runs are tate-chū-yoko (纵中横): multiple characters compressed horizontally into a single em-square and rendered upright.
Column
dataclass
¶
Column(text: str, height: float, y_offset: float = 0.0, char_count: int = 0, runs: List[VerticalRun] = list())
A single column of vertically-set text (analogous to Line).
measure_text ¶
measure_text(text: str, font_path: str, size: int, *, fm: Optional[FontManager] = None, letter_spacing: float = 0.0, word_spacing: float = 0.0) -> float
Return the total advance width of text in pixels.
Source code in src/latticesvg/text/shaper.py
measure_word ¶
measure_word(word: str, font_path: str, size: int, *, fm: Optional[FontManager] = None, letter_spacing: float = 0.0, word_spacing: float = 0.0) -> float
Measure a single word's advance width.
Source code in src/latticesvg/text/shaper.py
break_lines ¶
break_lines(text: str, available_width: float, font_path: str, size: int, white_space: str = 'normal', overflow_wrap: str = 'normal', *, fm: Optional[FontManager] = None, letter_spacing: float = 0.0, word_spacing: float = 0.0, hyphens: str = 'none', lang: str = 'en') -> List[Line]
Break text into lines that fit within available_width.
| PARAMETER | DESCRIPTION |
|---|---|
white_space
|
TYPE:
|
overflow_wrap
|
TYPE:
|
Source code in src/latticesvg/text/shaper.py
align_lines ¶
Set x_offset on each line based on text_align.
Does not mutate the input list; returns a new list.
For text-align: justify, computes per-word (or per-character for
CJK) extra spacing. The last line is always left-aligned (CSS
standard behaviour).
Source code in src/latticesvg/text/shaper.py
compute_text_block_size ¶
Return (width, height) of a text block.
line_height is the resolved line-height in absolute px. font_size is retained for API compatibility but unused when line_height is already absolute.
Source code in src/latticesvg/text/shaper.py
get_min_content_width ¶
get_min_content_width(text: str, font_path: str, size: int, white_space: str = 'normal', *, fm: Optional[FontManager] = None, letter_spacing: float = 0.0, word_spacing: float = 0.0, hyphens: str = 'none', lang: str = 'en') -> float
Return the minimum content width — the width of the longest word.
For CJK text each character is breakable so the min width is the widest single CJK character or the widest non-CJK word.
When hyphens is auto or manual, words can be split at
hyphenation points so the min width is the widest syllable fragment
(plus a trailing hyphen character).
Source code in src/latticesvg/text/shaper.py
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 | |
get_max_content_width ¶
get_max_content_width(text: str, font_path: str, size: int, white_space: str = 'normal', *, fm: Optional[FontManager] = None, letter_spacing: float = 0.0, word_spacing: float = 0.0) -> float
Return the max-content width — text on a single line.
Source code in src/latticesvg/text/shaper.py
break_lines_rich ¶
break_lines_rich(spans: 'List[TextSpan]', available_width: float, base_font_chain: list, base_size: int, white_space: str = 'normal', overflow_wrap: str = 'normal', *, fm: Optional[FontManager] = None, math_backend: Optional[object] = None, letter_spacing: float = 0.0, word_spacing: float = 0.0, hyphens: str = 'none', lang: str = 'en') -> List[RichLine]
Break a list of TextSpan into RichLine objects.
This is the rich-text counterpart of :func:break_lines. Each span
may use a different font/size; <br> spans force line breaks;
<math> spans are measured via math_backend if provided.
Source code in src/latticesvg/text/shaper.py
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 | |
align_lines_rich ¶
align_lines_rich(lines: List[RichLine], available_width: float, text_align: str = 'left') -> List[RichLine]
Set x_offset on each RichLine based on text_align.
For text-align: justify, counts space fragments as word gaps and
distributes extra space evenly. The last line stays left-aligned.
Source code in src/latticesvg/text/shaper.py
compute_rich_block_size ¶
Return (width, height) for a rich text block.
Source code in src/latticesvg/text/shaper.py
get_min_content_width_rich ¶
get_min_content_width_rich(spans: 'List[TextSpan]', base_font_chain: list, base_size: int, white_space: str = 'normal', *, fm: Optional[FontManager] = None, math_backend: Optional[object] = None, letter_spacing: float = 0.0, word_spacing: float = 0.0, hyphens: str = 'none', lang: str = 'en') -> float
Return the min-content width for a rich span list.
Source code in src/latticesvg/text/shaper.py
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 | |
get_max_content_width_rich ¶
get_max_content_width_rich(spans: 'List[TextSpan]', base_font_chain: list, base_size: int, white_space: str = 'normal', *, fm: Optional[FontManager] = None, math_backend: Optional[object] = None, letter_spacing: float = 0.0, word_spacing: float = 0.0) -> float
Return the max-content width for a rich span list (single line).
Source code in src/latticesvg/text/shaper.py
measure_text_vertical ¶
measure_text_vertical(text: str, font_path, size: int, *, fm: Optional[FontManager] = None, orientation: str = 'mixed', letter_spacing: float = 0.0, word_spacing: float = 0.0, text_combine_upright: str = 'none') -> float
Return the total advance height of text in vertical writing mode.
Upright characters contribute their vertical advance (advance_y,
falling back to advance_x for fonts lacking vertical metrics).
Sideways (rotated) runs contribute the horizontal width of the run
as vertical advance (since the run is rotated 90° CW).
Combine (纵中横) runs occupy one em-square of vertical advance.
Source code in src/latticesvg/text/shaper.py
break_columns ¶
break_columns(text: str, available_height: float, font_path, size: int, white_space: str = 'normal', overflow_wrap: str = 'normal', *, fm: Optional[FontManager] = None, orientation: str = 'mixed', letter_spacing: float = 0.0, word_spacing: float = 0.0, text_combine_upright: str = 'none') -> List[Column]
Break text into columns that fit within available_height.
This is the vertical counterpart of :func:break_lines. Each
Column represents one vertical column of text.
Characters are distributed top-to-bottom; when a column's height exceeds available_height, a new column starts.
Source code in src/latticesvg/text/shaper.py
1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 | |
align_columns ¶
align_columns(columns: List[Column], available_height: float, text_align: str = 'left') -> List[Column]
Set y_offset on each column based on text_align.
In vertical mode, text-align controls the block-direction
(vertical) alignment within each column:
- left / start → top-aligned (y_offset = 0)
- center → vertically centred
- right / end → bottom-aligned
Source code in src/latticesvg/text/shaper.py
compute_vertical_block_size ¶
compute_vertical_block_size(columns: List[Column], line_height: float, font_size: float) -> Tuple[float, float]
Return (width, height) for a vertical text block.
width = number of columns × line_height (inter-column spacing). height = tallest column.
Source code in src/latticesvg/text/shaper.py
get_min_content_height ¶
get_min_content_height(text: str, font_path, size: int, white_space: str = 'normal', *, fm: Optional[FontManager] = None, orientation: str = 'mixed', letter_spacing: float = 0.0, word_spacing: float = 0.0, text_combine_upright: str = 'none') -> float
Return the minimum content height for vertical text.
This is the height of the tallest single breakable unit — analogous
to get_min_content_width for horizontal text.
Source code in src/latticesvg/text/shaper.py
get_max_content_height ¶
get_max_content_height(text: str, font_path, size: int, white_space: str = 'normal', *, fm: Optional[FontManager] = None, orientation: str = 'mixed', letter_spacing: float = 0.0, word_spacing: float = 0.0, text_combine_upright: str = 'none') -> float
Return the max-content height for vertical text — all text in one column.
Source code in src/latticesvg/text/shaper.py
Font Embedding¶
embed ¶
Font subsetting and embedding for self-contained SVG output.
Collects all characters used per font path from the laid-out node tree,
creates WOFF2 subsets via fontTools, and generates @font-face CSS
rules that are injected into the drawsvg.Drawing.
Requires the optional fonttools package (with the brotli extension
for WOFF2 compression). When these are unavailable, the module raises
ImportError at call time — it is never imported unconditionally.
embed_fonts ¶
Collect used glyphs, subset fonts, and embed @font-face rules.
Modifies drawing in place by appending CSS via
drawing.append_css().
Raises ImportError if fonttools is not installed.