python-fontbro 0.30.0__py3-none-any.whl → 0.32.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fontbro/embedding_permissions.py +3 -4
- fontbro/exceptions.py +39 -0
- fontbro/family_classification.py +3 -4
- fontbro/font.py +70 -11
- fontbro/metadata.py +1 -1
- fontbro/monospace.py +3 -1
- fontbro/names.py +257 -18
- fontbro/pixel.py +2 -1
- fontbro/render.py +3 -2
- fontbro/tables.py +15 -0
- fontbro/variable.py +37 -4
- {python_fontbro-0.30.0.dist-info → python_fontbro-0.32.0.dist-info}/METADATA +57 -10
- {python_fontbro-0.30.0.dist-info → python_fontbro-0.32.0.dist-info}/RECORD +16 -16
- {python_fontbro-0.30.0.dist-info → python_fontbro-0.32.0.dist-info}/WHEEL +0 -0
- {python_fontbro-0.30.0.dist-info → python_fontbro-0.32.0.dist-info}/licenses/LICENSE.txt +0 -0
- {python_fontbro-0.30.0.dist-info → python_fontbro-0.32.0.dist-info}/top_level.txt +0 -0
fontbro/embedding_permissions.py
CHANGED
|
@@ -2,8 +2,9 @@ from __future__ import annotations
|
|
|
2
2
|
|
|
3
3
|
from fontTools.ttLib import TTFont
|
|
4
4
|
|
|
5
|
-
from fontbro.exceptions import ArgumentError
|
|
5
|
+
from fontbro.exceptions import ArgumentError
|
|
6
6
|
from fontbro.flags import get_flag, set_flag
|
|
7
|
+
from fontbro.tables import get_table_or_raise
|
|
7
8
|
|
|
8
9
|
# Embedding Permissions:
|
|
9
10
|
# https://learn.microsoft.com/en-us/typography/opentype/spec/os2#fstype
|
|
@@ -95,9 +96,7 @@ def set_embedding_permissions(
|
|
|
95
96
|
Sets the embedding permissions in the OS/2 fsType field of the given font,
|
|
96
97
|
the fsType field is left untouched if any argument is invalid.
|
|
97
98
|
"""
|
|
98
|
-
os2 = ttfont
|
|
99
|
-
if not os2:
|
|
100
|
-
raise OperationError("Invalid OS/2 table (doesn't exist).")
|
|
99
|
+
os2 = get_table_or_raise(ttfont, "OS/2")
|
|
101
100
|
|
|
102
101
|
usage_permissions = {
|
|
103
102
|
"installable": installable,
|
fontbro/exceptions.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
|
|
1
4
|
class ArgumentError(ValueError):
|
|
2
5
|
pass
|
|
3
6
|
|
|
@@ -10,5 +13,41 @@ class OperationError(TypeError):
|
|
|
10
13
|
pass
|
|
11
14
|
|
|
12
15
|
|
|
16
|
+
class ClosedFontError(OperationError):
|
|
17
|
+
"""
|
|
18
|
+
The operation was called on a closed font.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class MissingTableError(OperationError, KeyError):
|
|
23
|
+
"""
|
|
24
|
+
The operation requires a table the font doesn't have.
|
|
25
|
+
It's also a KeyError, raised before when accessing the missing table.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(
|
|
29
|
+
self,
|
|
30
|
+
table_tag: str,
|
|
31
|
+
message: str | None = None,
|
|
32
|
+
) -> None:
|
|
33
|
+
self.table_tag = table_tag
|
|
34
|
+
super().__init__(message or f"Invalid {table_tag} table (doesn't exist).")
|
|
35
|
+
|
|
36
|
+
def __reduce__(self) -> tuple[type[MissingTableError], tuple[str, str]]:
|
|
37
|
+
# pickle and copy recreate the exception passing its args (the message
|
|
38
|
+
# only) to __init__, the table tag must be passed too
|
|
39
|
+
return (self.__class__, (self.table_tag, str(self)))
|
|
40
|
+
|
|
41
|
+
def __str__(self) -> str:
|
|
42
|
+
# KeyError wraps the message in quotes
|
|
43
|
+
return str(self.args[0])
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class VariableFontRequiredError(OperationError):
|
|
47
|
+
"""
|
|
48
|
+
The operation requires a variable font.
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
|
|
13
52
|
class SanitizationError(Exception):
|
|
14
53
|
pass
|
fontbro/family_classification.py
CHANGED
|
@@ -4,7 +4,8 @@ from typing import Any
|
|
|
4
4
|
|
|
5
5
|
from fontTools.ttLib import TTFont
|
|
6
6
|
|
|
7
|
-
from fontbro.exceptions import ArgumentError
|
|
7
|
+
from fontbro.exceptions import ArgumentError
|
|
8
|
+
from fontbro.tables import get_table_or_raise
|
|
8
9
|
from fontbro.utils import concat_names, find_item, read_json
|
|
9
10
|
|
|
10
11
|
# Family Classification:
|
|
@@ -152,9 +153,7 @@ def set_family_classification(
|
|
|
152
153
|
"""
|
|
153
154
|
Sets the family classification of the given font in the OS/2.sFamilyClass field.
|
|
154
155
|
"""
|
|
155
|
-
os2 = ttfont
|
|
156
|
-
if not os2:
|
|
157
|
-
raise OperationError("Invalid OS/2 table (doesn't exist).")
|
|
156
|
+
os2 = get_table_or_raise(ttfont, "OS/2")
|
|
158
157
|
|
|
159
158
|
# validate class key and subclass key
|
|
160
159
|
class_item, subclass_item = _get_family_classification_items(
|
fontbro/font.py
CHANGED
|
@@ -32,7 +32,8 @@ from fontbro import variable as _variable
|
|
|
32
32
|
from fontbro import version as _version
|
|
33
33
|
from fontbro.exceptions import (
|
|
34
34
|
ArgumentError,
|
|
35
|
-
|
|
35
|
+
ClosedFontError,
|
|
36
|
+
VariableFontRequiredError,
|
|
36
37
|
)
|
|
37
38
|
|
|
38
39
|
|
|
@@ -325,7 +326,7 @@ class Font:
|
|
|
325
326
|
) -> None:
|
|
326
327
|
"""
|
|
327
328
|
Close the wrapped TTFont instance.
|
|
328
|
-
After closing, any operation on the font raises
|
|
329
|
+
After closing, any operation on the font raises ClosedFontError,
|
|
329
330
|
closing an already closed font does nothing.
|
|
330
331
|
"""
|
|
331
332
|
# TTFont.close() is idempotent (it closes the reader and sets it to None)
|
|
@@ -847,13 +848,13 @@ class Font:
|
|
|
847
848
|
:returns: The TTFont instance.
|
|
848
849
|
:rtype: TTFont
|
|
849
850
|
|
|
850
|
-
:raises
|
|
851
|
+
:raises ClosedFontError: If the font has been closed.
|
|
851
852
|
"""
|
|
852
853
|
# all the font operations get the TTFont instance through this method,
|
|
853
854
|
# the TTFont can also be closed directly (bypassing this instance):
|
|
854
855
|
# when closed, fontTools sets its reader to None
|
|
855
856
|
if self._ttfont is None or self._ttfont.reader is None:
|
|
856
|
-
raise
|
|
857
|
+
raise ClosedFontError("Invalid operation, the font is closed.")
|
|
857
858
|
return self._ttfont
|
|
858
859
|
|
|
859
860
|
def get_unicode_block_by_name(
|
|
@@ -1227,6 +1228,8 @@ class Font:
|
|
|
1227
1228
|
"""
|
|
1228
1229
|
Renames the font names records (1, 2, 4, 6, 16, 17) according to
|
|
1229
1230
|
the given family_name and style_name (subfamily_name).
|
|
1231
|
+
The names records (18, 25), the fvar named instances PostScript names
|
|
1232
|
+
and the CFF table names are updated too, only if already defined.
|
|
1230
1233
|
|
|
1231
1234
|
If family_name is not defined it will be auto-detected.
|
|
1232
1235
|
If style_name is not defined it will be auto-detected.
|
|
@@ -1247,6 +1250,48 @@ class Font:
|
|
|
1247
1250
|
if update_style_flags:
|
|
1248
1251
|
self.set_style_flags_by_subfamily_name()
|
|
1249
1252
|
|
|
1253
|
+
def rename_variable_instances(
|
|
1254
|
+
self,
|
|
1255
|
+
*,
|
|
1256
|
+
family_name: str = "",
|
|
1257
|
+
style_names: dict[str, str] | None = None,
|
|
1258
|
+
update_style_flags: bool = True,
|
|
1259
|
+
) -> None:
|
|
1260
|
+
"""
|
|
1261
|
+
Renames the named instances of the variable font.
|
|
1262
|
+
|
|
1263
|
+
The family_name replaces the family part of the instances PostScript names
|
|
1264
|
+
(and of the variations PostScript name prefix, if defined).
|
|
1265
|
+
The style_names replace the instances style names and the style part
|
|
1266
|
+
of their PostScript names. The axes names are unchanged.
|
|
1267
|
+
The default instance style name must match the font style name,
|
|
1268
|
+
so renaming it renames the font style too.
|
|
1269
|
+
|
|
1270
|
+
If family_name is not defined it will be auto-detected.
|
|
1271
|
+
|
|
1272
|
+
:param family_name: The family name
|
|
1273
|
+
:type family_name: str
|
|
1274
|
+
:param style_names: The new style names mapped by the old ones (case-insensitive),
|
|
1275
|
+
eg. {"Thin": "Hairline"}
|
|
1276
|
+
:type style_names: dict or None
|
|
1277
|
+
:param update_style_flags: if True the style flags will be updated by subfamily name
|
|
1278
|
+
(only if the default instance style name is renamed)
|
|
1279
|
+
:type update_style_flags: bool
|
|
1280
|
+
|
|
1281
|
+
:raises VariableFontRequiredError: if the font is not a variable font.
|
|
1282
|
+
:return: None
|
|
1283
|
+
"""
|
|
1284
|
+
ttfont = self.get_ttfont()
|
|
1285
|
+
style_name = self.get_style_name()
|
|
1286
|
+
_names.rename_variable_instances(
|
|
1287
|
+
ttfont,
|
|
1288
|
+
family_name=family_name,
|
|
1289
|
+
style_names=style_names,
|
|
1290
|
+
)
|
|
1291
|
+
|
|
1292
|
+
if update_style_flags and self.get_style_name() != style_name:
|
|
1293
|
+
self.set_style_flags_by_subfamily_name()
|
|
1294
|
+
|
|
1250
1295
|
def sanitize(
|
|
1251
1296
|
self,
|
|
1252
1297
|
*,
|
|
@@ -1468,10 +1513,10 @@ class Font:
|
|
|
1468
1513
|
includes 'instance' (containing instance metadata) and 'files' (a dictionary
|
|
1469
1514
|
with file formats as keys and file-paths as values).
|
|
1470
1515
|
|
|
1471
|
-
:raises
|
|
1516
|
+
:raises VariableFontRequiredError: If the font is not a variable font.
|
|
1472
1517
|
"""
|
|
1473
1518
|
if not self.is_variable():
|
|
1474
|
-
raise
|
|
1519
|
+
raise VariableFontRequiredError("Only a variable font can be instantiated.")
|
|
1475
1520
|
|
|
1476
1521
|
fsutil.assert_not_file(dirpath)
|
|
1477
1522
|
fsutil.make_dirs(dirpath)
|
|
@@ -1526,7 +1571,7 @@ class Font:
|
|
|
1526
1571
|
|
|
1527
1572
|
:param class_id: Numeric identifier of the font family class.
|
|
1528
1573
|
:param subclass_id: Optional numeric identifier of the font family subclass (default is 0).
|
|
1529
|
-
:raises
|
|
1574
|
+
:raises MissingTableError: If the OS/2 table is not available in the font.
|
|
1530
1575
|
:raises ArgumentError: If class_id is invalid or subclass_id is specified but invalid.
|
|
1531
1576
|
"""
|
|
1532
1577
|
ttfont = self.get_ttfont()
|
|
@@ -1679,7 +1724,7 @@ class Font:
|
|
|
1679
1724
|
:raises ArgumentError: If a value is not a bool or None, if more than one
|
|
1680
1725
|
of the mutually exclusive usage permissions is set to True, or if
|
|
1681
1726
|
installable is set to False while no other usage permission is in effect.
|
|
1682
|
-
:raises
|
|
1727
|
+
:raises MissingTableError: If the OS/2 table is not available in the font.
|
|
1683
1728
|
"""
|
|
1684
1729
|
ttfont = self.get_ttfont()
|
|
1685
1730
|
_embedding_permissions.set_embedding_permissions(
|
|
@@ -1791,6 +1836,8 @@ class Font:
|
|
|
1791
1836
|
self,
|
|
1792
1837
|
*,
|
|
1793
1838
|
coordinates: dict[str, Any],
|
|
1839
|
+
update_names: bool = True,
|
|
1840
|
+
update_style_flags: bool = True,
|
|
1794
1841
|
**options: Any,
|
|
1795
1842
|
) -> None:
|
|
1796
1843
|
"""
|
|
@@ -1805,15 +1852,27 @@ class Font:
|
|
|
1805
1852
|
{'wdth':100, 'wght':[100,600], 'ital':[30,65,70]} or
|
|
1806
1853
|
{'wdth':100, 'wght':{'min':100,'max':600}, 'ital':{'min':30,'default':65,'max':70}}
|
|
1807
1854
|
:type coordinates: dict
|
|
1855
|
+
:param update_names: if True the name records will be updated based on
|
|
1856
|
+
the closest instance to the new default coordinates (only if changed)
|
|
1857
|
+
:type update_names: bool
|
|
1858
|
+
:param update_style_flags: if True the style flags will be updated based on
|
|
1859
|
+
the closest instance to the new default coordinates (only if changed)
|
|
1860
|
+
:type update_style_flags: bool
|
|
1808
1861
|
:param options: The options for the fontTools.varLib.instancer
|
|
1809
1862
|
:type options: dictionary
|
|
1810
1863
|
|
|
1811
|
-
:raises
|
|
1864
|
+
:raises VariableFontRequiredError: If the font is not a variable font
|
|
1812
1865
|
:raises ValueError: If the coordinates are not defined (blank)
|
|
1813
1866
|
:raises ValueError: If the coordinates axes are all pinned
|
|
1814
1867
|
"""
|
|
1815
1868
|
ttfont = self.get_ttfont()
|
|
1816
|
-
_variable.to_sliced_variable(
|
|
1869
|
+
_variable.to_sliced_variable(
|
|
1870
|
+
ttfont,
|
|
1871
|
+
coordinates=coordinates,
|
|
1872
|
+
update_names=update_names,
|
|
1873
|
+
update_style_flags=update_style_flags,
|
|
1874
|
+
**options,
|
|
1875
|
+
)
|
|
1817
1876
|
|
|
1818
1877
|
def to_static(
|
|
1819
1878
|
self,
|
|
@@ -1842,7 +1901,7 @@ class Font:
|
|
|
1842
1901
|
:param options: The options for the fontTools.varLib.instancer
|
|
1843
1902
|
:type options: dictionary
|
|
1844
1903
|
|
|
1845
|
-
:raises
|
|
1904
|
+
:raises VariableFontRequiredError: If the font is not a variable font
|
|
1846
1905
|
:raises ValueError: If the coordinates axes are not all pinned
|
|
1847
1906
|
"""
|
|
1848
1907
|
ttfont = self.get_ttfont()
|
fontbro/metadata.py
CHANGED
fontbro/monospace.py
CHANGED
|
@@ -6,6 +6,7 @@ from collections import Counter
|
|
|
6
6
|
from fontTools import unicodedata
|
|
7
7
|
from fontTools.ttLib import TTFont
|
|
8
8
|
|
|
9
|
+
from fontbro.tables import get_table_or_raise
|
|
9
10
|
from fontbro.unicode import get_best_cmap
|
|
10
11
|
|
|
11
12
|
# same criteria used by Fontbakery (glyph_metrics_stats condition):
|
|
@@ -36,9 +37,10 @@ def _get_widths(
|
|
|
36
37
|
"""
|
|
37
38
|
Gets the advance widths of the given glyphs, zero widths are ignored.
|
|
38
39
|
"""
|
|
40
|
+
hmtx = get_table_or_raise(ttfont, "hmtx")
|
|
39
41
|
return [
|
|
40
42
|
advance
|
|
41
|
-
for glyph_name, (advance, _) in
|
|
43
|
+
for glyph_name, (advance, _) in hmtx.metrics.items()
|
|
42
44
|
if glyph_name in glyphs_names and advance != 0
|
|
43
45
|
]
|
|
44
46
|
|
fontbro/names.py
CHANGED
|
@@ -6,7 +6,8 @@ from typing import Any
|
|
|
6
6
|
from fontTools.misc.encodingTools import getEncoding
|
|
7
7
|
from fontTools.ttLib import TTFont
|
|
8
8
|
|
|
9
|
-
from fontbro.exceptions import ArgumentError
|
|
9
|
+
from fontbro.exceptions import ArgumentError, VariableFontRequiredError
|
|
10
|
+
from fontbro.tables import get_table_or_raise
|
|
10
11
|
from fontbro.utils import concat_names, remove_spaces
|
|
11
12
|
|
|
12
13
|
# Names:
|
|
@@ -182,26 +183,41 @@ def set_name(
|
|
|
182
183
|
ttfont: TTFont,
|
|
183
184
|
key: int | str,
|
|
184
185
|
value: str,
|
|
186
|
+
*,
|
|
187
|
+
windows: bool = True,
|
|
188
|
+
mac: bool = True,
|
|
189
|
+
unicode: bool = True,
|
|
185
190
|
) -> None:
|
|
186
191
|
"""
|
|
187
|
-
Sets the name by its identifier in the name table of the given font
|
|
192
|
+
Sets the name by its identifier in the name table of the given font,
|
|
193
|
+
only in the records of the platforms enabled by the windows, mac
|
|
194
|
+
and unicode flags.
|
|
188
195
|
"""
|
|
189
196
|
name_id = _get_name_id(key)
|
|
190
|
-
name_table = ttfont
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
197
|
+
name_table = get_table_or_raise(ttfont, "name")
|
|
198
|
+
if windows:
|
|
199
|
+
# https://github.com/fonttools/fonttools/blob/main/Lib/fontTools/ttLib/tables/_n_a_m_e.py#L568
|
|
200
|
+
# the windows record is always written, it's the one read first by get_name
|
|
201
|
+
name_table.setName(value, name_id, **_NAMES_WIN_IDS)
|
|
194
202
|
# the other records of the same name id are updated only if the font
|
|
195
203
|
# already has them (mac and unicode records are legacy, modern tools don't
|
|
196
204
|
# produce them anymore, so they are never added): a record left with its
|
|
197
205
|
# previous value would contradict the record just written
|
|
198
|
-
|
|
206
|
+
platforms = [
|
|
207
|
+
platform_ids
|
|
208
|
+
for platform_ids, enabled in (
|
|
209
|
+
(_NAMES_WIN_IDS, windows),
|
|
210
|
+
(_NAMES_UNI_IDS, unicode),
|
|
211
|
+
(_NAMES_MAC_IDS, mac),
|
|
212
|
+
)
|
|
213
|
+
if enabled
|
|
214
|
+
]
|
|
215
|
+
for platform_ids in platforms:
|
|
199
216
|
for record_ids in _get_name_records_ids(name_table, name_id, platform_ids):
|
|
200
217
|
# the value is written only if the record encoding can encode it
|
|
201
218
|
# (eg. mac roman can't encode greek, cyrillic, cjk), otherwise the
|
|
202
219
|
# record is removed (as fontTools addMultilingualName does),
|
|
203
|
-
# because the font would raise UnicodeEncodeError when saved
|
|
204
|
-
# removing it is safe because the windows record has just been written
|
|
220
|
+
# because the font would raise UnicodeEncodeError when saved
|
|
205
221
|
if _is_encodable(value, record_ids):
|
|
206
222
|
name_table.setName(value, name_id, **record_ids)
|
|
207
223
|
else:
|
|
@@ -249,6 +265,187 @@ def get_style_name(
|
|
|
249
265
|
)
|
|
250
266
|
|
|
251
267
|
|
|
268
|
+
def _get_postscript_name(
|
|
269
|
+
value: str,
|
|
270
|
+
) -> str:
|
|
271
|
+
"""
|
|
272
|
+
Gets a PostScript name from the given value,
|
|
273
|
+
replacing the characters not allowed with hyphens.
|
|
274
|
+
"""
|
|
275
|
+
# keep only printable ASCII subset:
|
|
276
|
+
# https://learn.microsoft.com/en-us/typography/opentype/spec/name#name-ids
|
|
277
|
+
# postscript_name_allowed_chars = {chr(code) for code in range(33, 127)}
|
|
278
|
+
# !"#$&'*+,-.0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\^_`abcdefghijklmnopqrstuvwxyz|~
|
|
279
|
+
postscript_name_pattern = (
|
|
280
|
+
r"[^0-9A-Za-z\!\"\#\$\&\'\*\+\,\-\.\:\;\=\?\@\\\^\_\`\|\~]"
|
|
281
|
+
)
|
|
282
|
+
postscript_name = re.sub(postscript_name_pattern, "-", value)
|
|
283
|
+
postscript_name = re.sub(r"[\-]+", "-", postscript_name).strip("-")
|
|
284
|
+
return postscript_name
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def _get_default_instance_style_name(
|
|
288
|
+
ttfont: TTFont,
|
|
289
|
+
) -> str:
|
|
290
|
+
"""
|
|
291
|
+
Gets the style name of the fvar named instance at the default coordinates.
|
|
292
|
+
"""
|
|
293
|
+
fvar = ttfont["fvar"]
|
|
294
|
+
coordinates = {axis.axisTag: axis.defaultValue for axis in fvar.axes}
|
|
295
|
+
for instance in fvar.instances:
|
|
296
|
+
if instance.coordinates == coordinates:
|
|
297
|
+
return get_name(ttfont, instance.subfamilyNameID) or ""
|
|
298
|
+
return ""
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
def rename_variable_instances(
|
|
302
|
+
ttfont: TTFont,
|
|
303
|
+
*,
|
|
304
|
+
family_name: str = "",
|
|
305
|
+
style_names: dict[str, str] | None = None,
|
|
306
|
+
) -> None:
|
|
307
|
+
"""
|
|
308
|
+
Renames the named instances of the given variable font: the family_name
|
|
309
|
+
replaces the family part of the instances PostScript names (and of the
|
|
310
|
+
variations PostScript name prefix, 25), the style_names (old style name
|
|
311
|
+
to new style name) replace the instances style names and the style part
|
|
312
|
+
of their PostScript names. The axes names are unchanged.
|
|
313
|
+
The default instance style name must match the font style name,
|
|
314
|
+
so renaming it renames the font style too.
|
|
315
|
+
"""
|
|
316
|
+
if "fvar" not in ttfont:
|
|
317
|
+
raise VariableFontRequiredError(
|
|
318
|
+
"Only the instances of a variable font can be renamed."
|
|
319
|
+
)
|
|
320
|
+
# style names are matched case-insensitively
|
|
321
|
+
style_names = {key.lower(): value for key, value in (style_names or {}).items()}
|
|
322
|
+
default_style_name = _get_default_instance_style_name(ttfont).lower()
|
|
323
|
+
default_style_name_new = style_names.get(default_style_name, "")
|
|
324
|
+
if default_style_name_new:
|
|
325
|
+
rename(ttfont, style_name=default_style_name_new)
|
|
326
|
+
family_name = (family_name or "").strip() or get_family_name(ttfont)
|
|
327
|
+
_rename_variable_instances(ttfont, family_name, style_names)
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
def _get_instance_postscript_name(
|
|
331
|
+
prefix: str,
|
|
332
|
+
style_name: str,
|
|
333
|
+
) -> str:
|
|
334
|
+
"""
|
|
335
|
+
Gets the PostScript name of a variable font named instance, the "Italic"
|
|
336
|
+
style suffix is omitted if the prefix already ends with it
|
|
337
|
+
(eg. "TourneyItalic-Thin" instead of "TourneyItalic-ThinItalic").
|
|
338
|
+
"""
|
|
339
|
+
style_name = remove_spaces(style_name)
|
|
340
|
+
if prefix.lower().endswith("italic"):
|
|
341
|
+
style_name = re.sub(r"italic$", "", style_name, flags=re.IGNORECASE)
|
|
342
|
+
style_name = style_name or "Regular"
|
|
343
|
+
return _get_postscript_name(f"{prefix}-{style_name}")
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
def _rename_variable_instances(
|
|
347
|
+
ttfont: TTFont,
|
|
348
|
+
family_name: str,
|
|
349
|
+
style_names: dict[str, str],
|
|
350
|
+
) -> None:
|
|
351
|
+
"""
|
|
352
|
+
Renames the named instances of the given variable font,
|
|
353
|
+
the style_names keys must be lowercase.
|
|
354
|
+
"""
|
|
355
|
+
name_table = ttfont["name"]
|
|
356
|
+
|
|
357
|
+
# the old family name is read before updating it (rename calls this function
|
|
358
|
+
# before updating the name records), the old prefixes are computed from it
|
|
359
|
+
family_name_old = get_family_name(ttfont)
|
|
360
|
+
prefix_old = get_name(ttfont, NAME_VARIATIONS_POSTSCRIPT_NAME_PREFIX) or ""
|
|
361
|
+
if prefix_old:
|
|
362
|
+
# variations postscript name prefix: only ASCII letters and digits allowed,
|
|
363
|
+
# its suffix is kept (eg. "Roman", "Italic") to avoid the same instances
|
|
364
|
+
# postscript names in the roman and italic fonts
|
|
365
|
+
suffix = ""
|
|
366
|
+
family_name_old_prefix = re.sub(r"[^0-9A-Za-z]", "", family_name_old)
|
|
367
|
+
if family_name_old_prefix and prefix_old.startswith(family_name_old_prefix):
|
|
368
|
+
suffix = prefix_old[len(family_name_old_prefix) :]
|
|
369
|
+
elif "italic" in get_style_name(ttfont).lower() and not (
|
|
370
|
+
family_name.lower().endswith("italic")
|
|
371
|
+
):
|
|
372
|
+
suffix = "Italic"
|
|
373
|
+
prefix = re.sub(r"[^0-9A-Za-z]", "", f"{family_name}{suffix}")
|
|
374
|
+
# the windows record is written only if the font already has it
|
|
375
|
+
name_id = _get_name_id(NAME_VARIATIONS_POSTSCRIPT_NAME_PREFIX)
|
|
376
|
+
windows = bool(name_table.getName(name_id, **_NAMES_WIN_IDS))
|
|
377
|
+
set_name(
|
|
378
|
+
ttfont, NAME_VARIATIONS_POSTSCRIPT_NAME_PREFIX, prefix, windows=windows
|
|
379
|
+
)
|
|
380
|
+
else:
|
|
381
|
+
prefix_old = _get_postscript_name(remove_spaces(family_name_old))
|
|
382
|
+
prefix = _get_postscript_name(remove_spaces(family_name))
|
|
383
|
+
|
|
384
|
+
# the style names are read before updating them,
|
|
385
|
+
# because the same name record can be shared by more instances
|
|
386
|
+
instances = ttfont["fvar"].instances
|
|
387
|
+
instances_style_names = [
|
|
388
|
+
get_name(ttfont, instance.subfamilyNameID) or "" for instance in instances
|
|
389
|
+
]
|
|
390
|
+
for instance, style_name in zip(instances, instances_style_names, strict=True):
|
|
391
|
+
style_name_new = style_names.get(style_name.lower(), "")
|
|
392
|
+
if style_name_new and instance.subfamilyNameID < 256:
|
|
393
|
+
# name ids 2 and 17 are the font style names (updated by rename),
|
|
394
|
+
# so a new name record is added for the instance
|
|
395
|
+
instance.subfamilyNameID = name_table.addName(
|
|
396
|
+
style_name_new, platforms=((3, 1, 0x409),)
|
|
397
|
+
)
|
|
398
|
+
elif style_name_new:
|
|
399
|
+
# the existing name record is updated, also where it is shared
|
|
400
|
+
# (eg. the STAT table axis values), the windows record is written
|
|
401
|
+
# only if the font already has it
|
|
402
|
+
name_id = instance.subfamilyNameID
|
|
403
|
+
windows = bool(name_table.getName(name_id, **_NAMES_WIN_IDS))
|
|
404
|
+
set_name(ttfont, name_id, style_name_new, windows=windows)
|
|
405
|
+
name_id = instance.postscriptNameID
|
|
406
|
+
name = get_name(ttfont, name_id) if 255 < name_id < 0xFFFF else None
|
|
407
|
+
if not name:
|
|
408
|
+
continue
|
|
409
|
+
if not style_name_new and prefix_old and name.startswith(prefix_old):
|
|
410
|
+
# only the prefix is replaced, to keep the font naming convention
|
|
411
|
+
name = prefix + name[len(prefix_old) :]
|
|
412
|
+
else:
|
|
413
|
+
# the name is generated if the style is renamed,
|
|
414
|
+
# or if it doesn't start with the old prefix (inconsistent font)
|
|
415
|
+
name = _get_instance_postscript_name(prefix, style_name_new or style_name)
|
|
416
|
+
set_name(ttfont, name_id, name)
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
def _rename_cff(
|
|
420
|
+
ttfont: TTFont,
|
|
421
|
+
postscript_name: str,
|
|
422
|
+
full_name: str,
|
|
423
|
+
family_name: str,
|
|
424
|
+
weight: str,
|
|
425
|
+
) -> None:
|
|
426
|
+
"""
|
|
427
|
+
Renames the CFF table names (otf fonts): the font name and the FDArray
|
|
428
|
+
font names (CID-keyed fonts) prefixed by it, FullName, FamilyName and
|
|
429
|
+
Weight (only if not empty) only if already defined.
|
|
430
|
+
"""
|
|
431
|
+
if "CFF " not in ttfont:
|
|
432
|
+
return
|
|
433
|
+
cff = ttfont["CFF "].cff
|
|
434
|
+
postscript_name_old = cff.fontNames[0]
|
|
435
|
+
cff.fontNames[0] = postscript_name
|
|
436
|
+
top_dict = cff.topDictIndex[0]
|
|
437
|
+
for font_dict in getattr(top_dict, "FDArray", []):
|
|
438
|
+
font_name = getattr(font_dict, "FontName", None)
|
|
439
|
+
if font_name and font_name.startswith(postscript_name_old):
|
|
440
|
+
font_dict.FontName = postscript_name + font_name[len(postscript_name_old) :]
|
|
441
|
+
if getattr(top_dict, "FullName", None):
|
|
442
|
+
top_dict.FullName = full_name
|
|
443
|
+
if getattr(top_dict, "FamilyName", None):
|
|
444
|
+
top_dict.FamilyName = family_name
|
|
445
|
+
if weight and getattr(top_dict, "Weight", None):
|
|
446
|
+
top_dict.Weight = weight
|
|
447
|
+
|
|
448
|
+
|
|
252
449
|
def rename(
|
|
253
450
|
ttfont: TTFont,
|
|
254
451
|
*,
|
|
@@ -259,6 +456,8 @@ def rename(
|
|
|
259
456
|
Renames the names records (1, 2, 3, 4, 6, 16, 17, 21, 22) of the given font
|
|
260
457
|
according to the given family_name and style_name (subfamily_name),
|
|
261
458
|
if not defined they will be auto-detected.
|
|
459
|
+
The records 18 and 25, the fvar instances PostScript names and the CFF
|
|
460
|
+
names are updated only if already defined.
|
|
262
461
|
"""
|
|
263
462
|
family_name = (family_name or "").strip() or get_family_name(ttfont)
|
|
264
463
|
style_name = (style_name or "").strip() or get_style_name(ttfont)
|
|
@@ -289,15 +488,7 @@ def rename(
|
|
|
289
488
|
remove_spaces(typographic_subfamily_name),
|
|
290
489
|
)
|
|
291
490
|
|
|
292
|
-
|
|
293
|
-
# https://learn.microsoft.com/en-us/typography/opentype/spec/name#name-ids
|
|
294
|
-
# postscript_name_allowed_chars = {chr(code) for code in range(33, 127)}
|
|
295
|
-
# !"#$&'*+,-.0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\^_`abcdefghijklmnopqrstuvwxyz|~
|
|
296
|
-
postscript_name_pattern = (
|
|
297
|
-
r"[^0-9A-Za-z\!\"\#\$\&\'\*\+\,\-\.\:\;\=\?\@\\\^\_\`\|\~]"
|
|
298
|
-
)
|
|
299
|
-
postscript_name = re.sub(postscript_name_pattern, "-", postscript_name)
|
|
300
|
-
postscript_name = re.sub(r"[\-]+", "-", postscript_name).strip("-")
|
|
491
|
+
postscript_name = _get_postscript_name(postscript_name)
|
|
301
492
|
postscript_name_length = len(postscript_name)
|
|
302
493
|
if postscript_name_length > 63:
|
|
303
494
|
raise ArgumentError(
|
|
@@ -325,4 +516,52 @@ def rename(
|
|
|
325
516
|
NAME_WWS_FAMILY_NAME: wws_family_name,
|
|
326
517
|
NAME_WWS_SUBFAMILY_NAME: wws_subfamily_name,
|
|
327
518
|
}
|
|
519
|
+
|
|
520
|
+
# compatible full name: if it's not the full name (eg. a designer-defined
|
|
521
|
+
# short menu name), only the family name in it is replaced
|
|
522
|
+
compatible_full_name = get_name(ttfont, NAME_COMPATIBLE_FULL) or ""
|
|
523
|
+
if compatible_full_name:
|
|
524
|
+
family_name_old = get_family_name(ttfont)
|
|
525
|
+
full_name_old = concat_names(family_name_old, get_style_name(ttfont))
|
|
526
|
+
if (
|
|
527
|
+
compatible_full_name == full_name_old
|
|
528
|
+
or not family_name_old
|
|
529
|
+
or family_name_old not in compatible_full_name
|
|
530
|
+
):
|
|
531
|
+
compatible_full_name = full_name
|
|
532
|
+
else:
|
|
533
|
+
compatible_full_name = compatible_full_name.replace(
|
|
534
|
+
family_name_old, typographic_family_name, 1
|
|
535
|
+
)
|
|
536
|
+
|
|
537
|
+
# cff weight (eg. "Bold" for "Bold Italic"), updated only if the style changes
|
|
538
|
+
style_name_old = get_style_name(ttfont)
|
|
539
|
+
weight = ""
|
|
540
|
+
if style_name_old != typographic_subfamily_name:
|
|
541
|
+
weight = re.sub(
|
|
542
|
+
r"\s*italic$", "", typographic_subfamily_name, flags=re.IGNORECASE
|
|
543
|
+
)
|
|
544
|
+
weight = weight or "Regular"
|
|
545
|
+
|
|
546
|
+
# fvar instances (and name id 25) need the old postscript name
|
|
547
|
+
if "fvar" in ttfont:
|
|
548
|
+
# the default instance style name must match the font style name, it's
|
|
549
|
+
# renamed only if it matches the old one (otherwise a family rename of
|
|
550
|
+
# an inconsistent font would rename it, and the STAT values sharing it)
|
|
551
|
+
style_names = {}
|
|
552
|
+
if (
|
|
553
|
+
style_name_old != typographic_subfamily_name
|
|
554
|
+
and _get_default_instance_style_name(ttfont) == style_name_old
|
|
555
|
+
):
|
|
556
|
+
style_names[style_name_old.lower()] = typographic_subfamily_name
|
|
557
|
+
_rename_variable_instances(ttfont, typographic_family_name, style_names)
|
|
558
|
+
|
|
328
559
|
set_names(ttfont, names)
|
|
560
|
+
# name id 18 (macintosh only) is updated only if already defined,
|
|
561
|
+
# the windows record is written only if the font already has it
|
|
562
|
+
if compatible_full_name:
|
|
563
|
+
name_id = _get_name_id(NAME_COMPATIBLE_FULL)
|
|
564
|
+
windows = bool(ttfont["name"].getName(name_id, **_NAMES_WIN_IDS))
|
|
565
|
+
set_name(ttfont, NAME_COMPATIBLE_FULL, compatible_full_name, windows=windows)
|
|
566
|
+
|
|
567
|
+
_rename_cff(ttfont, postscript_name, full_name, typographic_family_name, weight)
|
fontbro/pixel.py
CHANGED
|
@@ -9,6 +9,7 @@ from fontTools import unicodedata
|
|
|
9
9
|
from fontTools.pens.recordingPen import DecomposingRecordingPen
|
|
10
10
|
from fontTools.ttLib import TTFont
|
|
11
11
|
|
|
12
|
+
from fontbro.tables import get_table_or_raise
|
|
12
13
|
from fontbro.unicode import get_best_cmap
|
|
13
14
|
|
|
14
15
|
Segment = tuple[float, float]
|
|
@@ -219,7 +220,7 @@ def is_pixel(
|
|
|
219
220
|
|
|
220
221
|
# 2nd check: segments aligned to a plausible pixel grid,
|
|
221
222
|
# it excludes squared ("techno") fonts
|
|
222
|
-
units_per_em = ttfont
|
|
223
|
+
units_per_em = get_table_or_raise(ttfont, "head").unitsPerEm
|
|
223
224
|
pixel_width, pixel_height = (
|
|
224
225
|
_get_pixel_dimension(
|
|
225
226
|
orthogonal_glyphs_segments, axis, units_per_em, min_glyphs_count
|
fontbro/render.py
CHANGED
|
@@ -9,6 +9,7 @@ from fontTools.pens.svgPathPen import SVGPathPen
|
|
|
9
9
|
from fontTools.ttLib import TTFont
|
|
10
10
|
from PIL import Image, ImageDraw, ImageFont
|
|
11
11
|
|
|
12
|
+
from fontbro.tables import get_table_or_raise
|
|
12
13
|
from fontbro.unicode import get_best_cmap_or_raise
|
|
13
14
|
|
|
14
15
|
|
|
@@ -51,9 +52,9 @@ def get_svg(
|
|
|
51
52
|
Gets an SVG representation of the given font rendering some text.
|
|
52
53
|
"""
|
|
53
54
|
# get font metrics
|
|
54
|
-
units_per_em = ttfont
|
|
55
|
+
units_per_em = get_table_or_raise(ttfont, "head").unitsPerEm
|
|
55
56
|
scale = size / units_per_em
|
|
56
|
-
hhea = ttfont
|
|
57
|
+
hhea = get_table_or_raise(ttfont, "hhea")
|
|
57
58
|
ascent = hhea.ascent * scale
|
|
58
59
|
descent = hhea.descent * scale
|
|
59
60
|
width = 0
|
fontbro/tables.py
CHANGED
|
@@ -4,6 +4,8 @@ from typing import Any
|
|
|
4
4
|
|
|
5
5
|
from fontTools.ttLib import TTFont
|
|
6
6
|
|
|
7
|
+
from fontbro.exceptions import MissingTableError
|
|
8
|
+
|
|
7
9
|
# https://learn.microsoft.com/en-us/typography/opentype/spec/otff#font-tables
|
|
8
10
|
_TABLE_NAMES_BY_TAG: dict[str, str] = {
|
|
9
11
|
# required tables
|
|
@@ -95,6 +97,19 @@ _TABLE_NAMES_BY_TAG: dict[str, str] = {
|
|
|
95
97
|
}
|
|
96
98
|
|
|
97
99
|
|
|
100
|
+
def get_table_or_raise(
|
|
101
|
+
ttfont: TTFont,
|
|
102
|
+
tag: str,
|
|
103
|
+
) -> Any:
|
|
104
|
+
"""
|
|
105
|
+
Gets the table with the given tag of the given font,
|
|
106
|
+
MissingTableError is raised if the font has no table with the given tag.
|
|
107
|
+
"""
|
|
108
|
+
if tag not in ttfont:
|
|
109
|
+
raise MissingTableError(tag)
|
|
110
|
+
return ttfont[tag]
|
|
111
|
+
|
|
112
|
+
|
|
98
113
|
def get_tables_tags(
|
|
99
114
|
ttfont: TTFont,
|
|
100
115
|
*,
|
fontbro/variable.py
CHANGED
|
@@ -9,8 +9,9 @@ from fontTools.varLib.instancer import OverlapMode
|
|
|
9
9
|
|
|
10
10
|
from fontbro import names as _names
|
|
11
11
|
from fontbro import style_flags as _style_flags
|
|
12
|
-
from fontbro.exceptions import ArgumentError,
|
|
12
|
+
from fontbro.exceptions import ArgumentError, VariableFontRequiredError
|
|
13
13
|
from fontbro.math import get_euclidean_distance
|
|
14
|
+
from fontbro.tables import get_table_or_raise
|
|
14
15
|
from fontbro.utils import slugify
|
|
15
16
|
|
|
16
17
|
# Variable Axes:
|
|
@@ -159,7 +160,7 @@ def get_variable_instances(
|
|
|
159
160
|
"""
|
|
160
161
|
if not is_variable(ttfont):
|
|
161
162
|
return None
|
|
162
|
-
name_table = ttfont
|
|
163
|
+
name_table = get_table_or_raise(ttfont, "name")
|
|
163
164
|
return [
|
|
164
165
|
{
|
|
165
166
|
"coordinates": instance.coordinates,
|
|
@@ -301,10 +302,18 @@ def _remove_stat_table(
|
|
|
301
302
|
return False
|
|
302
303
|
|
|
303
304
|
|
|
305
|
+
def _get_default_coordinates(
|
|
306
|
+
ttfont: TTFont,
|
|
307
|
+
) -> dict[str, float]:
|
|
308
|
+
return {axis.axisTag: axis.defaultValue for axis in ttfont["fvar"].axes}
|
|
309
|
+
|
|
310
|
+
|
|
304
311
|
def to_sliced_variable(
|
|
305
312
|
ttfont: TTFont,
|
|
306
313
|
*,
|
|
307
314
|
coordinates: dict[str, Any],
|
|
315
|
+
update_names: bool = True,
|
|
316
|
+
update_style_flags: bool = True,
|
|
308
317
|
**options: Any,
|
|
309
318
|
) -> None:
|
|
310
319
|
"""
|
|
@@ -312,7 +321,7 @@ def to_sliced_variable(
|
|
|
312
321
|
the variable axes at the given coordinates.
|
|
313
322
|
"""
|
|
314
323
|
if not is_variable(ttfont):
|
|
315
|
-
raise
|
|
324
|
+
raise VariableFontRequiredError("Only a variable font can be sliced.")
|
|
316
325
|
|
|
317
326
|
coordinates = get_sliced_coordinates(ttfont, coordinates)
|
|
318
327
|
|
|
@@ -322,8 +331,25 @@ def to_sliced_variable(
|
|
|
322
331
|
options.setdefault("updateFontNames", False)
|
|
323
332
|
|
|
324
333
|
# instantiate the sliced variable font
|
|
334
|
+
default_coordinates = _get_default_coordinates(ttfont)
|
|
325
335
|
instancer.instantiateVariableFont(ttfont, coordinates, inplace=True, **options)
|
|
326
336
|
|
|
337
|
+
# update name records and style flags if the default instance changed
|
|
338
|
+
# (unless they have been updated by fontTools using the STAT table)
|
|
339
|
+
default_coordinates_new = _get_default_coordinates(ttfont)
|
|
340
|
+
if (
|
|
341
|
+
update_names
|
|
342
|
+
and default_coordinates_new != default_coordinates
|
|
343
|
+
and not options["updateFontNames"]
|
|
344
|
+
):
|
|
345
|
+
instance = get_variable_instance_closest_to_coordinates(
|
|
346
|
+
ttfont, default_coordinates_new
|
|
347
|
+
)
|
|
348
|
+
if instance:
|
|
349
|
+
_names.rename(ttfont, style_name=instance["style_name"])
|
|
350
|
+
if update_style_flags:
|
|
351
|
+
_style_flags.set_style_flags_by_subfamily_name(ttfont)
|
|
352
|
+
|
|
327
353
|
|
|
328
354
|
def to_static(
|
|
329
355
|
ttfont: TTFont,
|
|
@@ -339,7 +365,7 @@ def to_static(
|
|
|
339
365
|
the variable axes at the given coordinates.
|
|
340
366
|
"""
|
|
341
367
|
if not is_variable(ttfont):
|
|
342
|
-
raise
|
|
368
|
+
raise VariableFontRequiredError("Only a variable font can be made static.")
|
|
343
369
|
|
|
344
370
|
coordinates = get_static_coordinates(
|
|
345
371
|
ttfont,
|
|
@@ -363,6 +389,13 @@ def to_static(
|
|
|
363
389
|
# not useful in static fonts and after instancing it may contain incorrect values
|
|
364
390
|
_remove_stat_table(ttfont)
|
|
365
391
|
|
|
392
|
+
# remove variations postscript name prefix (25), used only by variable fonts
|
|
393
|
+
# (as fontTools does when instancing with updateFontNames=True)
|
|
394
|
+
# and the names used only by the removed fvar and STAT tables
|
|
395
|
+
if "name" in ttfont:
|
|
396
|
+
ttfont["name"].removeNames(nameID=25)
|
|
397
|
+
ttfont["name"].removeUnusedNames(ttfont)
|
|
398
|
+
|
|
366
399
|
# update name records and style flags based on instance style name
|
|
367
400
|
if instance and update_names:
|
|
368
401
|
_names.rename(ttfont, style_name=instance["style_name"])
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: python-fontbro
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.32.0
|
|
4
4
|
Summary: friendly font operations on top of fontTools.
|
|
5
5
|
Author-email: Fabio Caccamo <fabio.caccamo@gmail.com>
|
|
6
6
|
Maintainer-email: Fabio Caccamo <fabio.caccamo@gmail.com>
|
|
@@ -43,7 +43,6 @@ Classifier: License :: OSI Approved :: MIT License
|
|
|
43
43
|
Classifier: Natural Language :: English
|
|
44
44
|
Classifier: Operating System :: OS Independent
|
|
45
45
|
Classifier: Programming Language :: Python :: 3
|
|
46
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
47
46
|
Classifier: Programming Language :: Python :: 3.11
|
|
48
47
|
Classifier: Programming Language :: Python :: 3.12
|
|
49
48
|
Classifier: Programming Language :: Python :: 3.13
|
|
@@ -52,6 +51,7 @@ Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
|
52
51
|
Classifier: Topic :: Text Processing :: Fonts
|
|
53
52
|
Classifier: Topic :: Multimedia :: Graphics
|
|
54
53
|
Classifier: Topic :: Multimedia :: Graphics :: Graphics Conversion
|
|
54
|
+
Requires-Python: >=3.11
|
|
55
55
|
Description-Content-Type: text/markdown
|
|
56
56
|
License-File: LICENSE.txt
|
|
57
57
|
Requires-Dist: fonttools[lxml,pathops,unicode,woff]<5.0,>=4.43.0
|
|
@@ -149,6 +149,7 @@ with open("fonts/MyFont.ttf") as fh:
|
|
|
149
149
|
- [`is_static`](#is_static)
|
|
150
150
|
- [`is_variable`](#is_variable)
|
|
151
151
|
- [`rename`](#rename)
|
|
152
|
+
- [`rename_variable_instances`](#rename_variable_instances)
|
|
152
153
|
- [`sanitize`](#sanitize)
|
|
153
154
|
- [`save`](#save)
|
|
154
155
|
- [`save_as_woff`](#save_as_woff)
|
|
@@ -183,7 +184,7 @@ font_clone = font.clone()
|
|
|
183
184
|
```python
|
|
184
185
|
"""
|
|
185
186
|
Close the wrapped TTFont instance.
|
|
186
|
-
After closing, any operation on the font raises
|
|
187
|
+
After closing, any operation on the font raises ClosedFontError,
|
|
187
188
|
closing an already closed font does nothing.
|
|
188
189
|
"""
|
|
189
190
|
|
|
@@ -630,7 +631,7 @@ Gets the wrapped TTFont instance.
|
|
|
630
631
|
:returns: The TTFont instance.
|
|
631
632
|
:rtype: TTFont
|
|
632
633
|
|
|
633
|
-
:raises
|
|
634
|
+
:raises ClosedFontError: If the font has been closed.
|
|
634
635
|
"""
|
|
635
636
|
|
|
636
637
|
ttfont = font.get_ttfont()
|
|
@@ -966,6 +967,8 @@ variable = font.is_variable()
|
|
|
966
967
|
"""
|
|
967
968
|
Renames the font names records (1, 2, 4, 6, 16, 17) according to
|
|
968
969
|
the given family_name and style_name (subfamily_name).
|
|
970
|
+
The names records (18, 25), the fvar named instances PostScript names
|
|
971
|
+
and the CFF table names are updated too, only if already defined.
|
|
969
972
|
|
|
970
973
|
If family_name is not defined it will be auto-detected.
|
|
971
974
|
If style_name is not defined it will be auto-detected.
|
|
@@ -985,6 +988,39 @@ font.rename(
|
|
|
985
988
|
)
|
|
986
989
|
```
|
|
987
990
|
|
|
991
|
+
#### `rename_variable_instances`
|
|
992
|
+
```python
|
|
993
|
+
"""
|
|
994
|
+
Renames the named instances of the variable font.
|
|
995
|
+
|
|
996
|
+
The family_name replaces the family part of the instances PostScript names
|
|
997
|
+
(and of the variations PostScript name prefix, if defined).
|
|
998
|
+
The style_names replace the instances style names and the style part
|
|
999
|
+
of their PostScript names. The axes names are unchanged.
|
|
1000
|
+
The default instance style name must match the font style name,
|
|
1001
|
+
so renaming it renames the font style too.
|
|
1002
|
+
|
|
1003
|
+
If family_name is not defined it will be auto-detected.
|
|
1004
|
+
|
|
1005
|
+
:param family_name: The family name
|
|
1006
|
+
:type family_name: str
|
|
1007
|
+
:param style_names: The new style names mapped by the old ones (case-insensitive),
|
|
1008
|
+
eg. {"Thin": "Hairline"}
|
|
1009
|
+
:type style_names: dict or None
|
|
1010
|
+
:param update_style_flags: if True the style flags will be updated by subfamily name
|
|
1011
|
+
(only if the default instance style name is renamed)
|
|
1012
|
+
:type update_style_flags: bool
|
|
1013
|
+
|
|
1014
|
+
:raises VariableFontRequiredError: if the font is not a variable font.
|
|
1015
|
+
"""
|
|
1016
|
+
|
|
1017
|
+
font.rename_variable_instances(
|
|
1018
|
+
family_name="My Font New",
|
|
1019
|
+
style_names={"Thin": "Hairline"},
|
|
1020
|
+
update_style_flags=True,
|
|
1021
|
+
)
|
|
1022
|
+
```
|
|
1023
|
+
|
|
988
1024
|
#### `sanitize`
|
|
989
1025
|
```python
|
|
990
1026
|
"""
|
|
@@ -1095,7 +1131,7 @@ Save all instances of a variable font to specified directory in one or more form
|
|
|
1095
1131
|
includes 'instance' (containing instance metadata) and 'files' (a dictionary
|
|
1096
1132
|
with file formats as keys and file-paths as values).
|
|
1097
1133
|
|
|
1098
|
-
:raises
|
|
1134
|
+
:raises VariableFontRequiredError: If the font is not a variable font.
|
|
1099
1135
|
"""
|
|
1100
1136
|
|
|
1101
1137
|
saved_fonts = font.save_variable_instances(
|
|
@@ -1131,7 +1167,7 @@ The fsType field is left untouched if any argument is invalid.
|
|
|
1131
1167
|
:raises ArgumentError: If a value is not a bool or None, if more than one
|
|
1132
1168
|
of the mutually exclusive usage permissions is set to True, or if
|
|
1133
1169
|
installable is set to False while no other usage permission is in effect.
|
|
1134
|
-
:raises
|
|
1170
|
+
:raises MissingTableError: If the OS/2 table is not available in the font.
|
|
1135
1171
|
"""
|
|
1136
1172
|
|
|
1137
1173
|
font.set_embedding_permissions(
|
|
@@ -1151,7 +1187,7 @@ based on provided class_id and subclass_id.
|
|
|
1151
1187
|
|
|
1152
1188
|
:param class_id: Numeric identifier of the font family class.
|
|
1153
1189
|
:param subclass_id: Optional numeric identifier of the font family subclass (default is 0).
|
|
1154
|
-
:raises
|
|
1190
|
+
:raises MissingTableError: If the OS/2 table is not available in the font.
|
|
1155
1191
|
:raises ArgumentError: If class_id is invalid or subclass_id is specified but invalid.
|
|
1156
1192
|
"""
|
|
1157
1193
|
|
|
@@ -1360,15 +1396,26 @@ If an axis min and max values are equal, the axis will be pinned.
|
|
|
1360
1396
|
{'wdth':100, 'wght':[100,600], 'ital':[30,65,70]} or
|
|
1361
1397
|
{'wdth':100, 'wght':{'min':100,'max':600}, 'ital':{'min':30,'default':65,'max':70}}
|
|
1362
1398
|
:type coordinates: dict
|
|
1399
|
+
:param update_names: if True the name records will be updated based on
|
|
1400
|
+
the closest instance to the new default coordinates (only if changed)
|
|
1401
|
+
:type update_names: bool
|
|
1402
|
+
:param update_style_flags: if True the style flags will be updated based on
|
|
1403
|
+
the closest instance to the new default coordinates (only if changed)
|
|
1404
|
+
:type update_style_flags: bool
|
|
1363
1405
|
:param options: The options for the fontTools.varLib.instancer
|
|
1364
1406
|
:type options: dictionary
|
|
1365
1407
|
|
|
1366
|
-
:raises
|
|
1408
|
+
:raises VariableFontRequiredError: If the font is not a variable font
|
|
1367
1409
|
:raises ValueError: If the coordinates are not defined (empty)
|
|
1368
1410
|
:raises ValueError: If the coordinates axes are all pinned
|
|
1369
1411
|
"""
|
|
1370
1412
|
|
|
1371
|
-
font.to_sliced_variable(
|
|
1413
|
+
font.to_sliced_variable(
|
|
1414
|
+
coordinates,
|
|
1415
|
+
update_names=True,
|
|
1416
|
+
update_style_flags=True,
|
|
1417
|
+
**options,
|
|
1418
|
+
)
|
|
1372
1419
|
```
|
|
1373
1420
|
|
|
1374
1421
|
#### `to_static`
|
|
@@ -1391,7 +1438,7 @@ If coordinates are not specified each axis will be pinned at its default value.
|
|
|
1391
1438
|
:param options: The options for the fontTools.varLib.instancer
|
|
1392
1439
|
:type options: dictionary
|
|
1393
1440
|
|
|
1394
|
-
:raises
|
|
1441
|
+
:raises VariableFontRequiredError: If the font is not a variable font
|
|
1395
1442
|
:raises ValueError: If the coordinates axes are not all pinned
|
|
1396
1443
|
"""
|
|
1397
1444
|
|
|
@@ -1,38 +1,38 @@
|
|
|
1
1
|
fontbro/__init__.py,sha256=Jk5jOWcH6_nDZAz4VDK8flZTxI6dZVjkB7STfOtWwq4,344
|
|
2
2
|
fontbro/bitmap.py,sha256=VOzJ7_z3VDY967EtRS08EMoNfkLGPJ-gU-YOWATOYco,1480
|
|
3
3
|
fontbro/color.py,sha256=l8Cn7DckiSwJcDfQsHTvA9r9l1ze8Gi8Ps85CSbiMds,358
|
|
4
|
-
fontbro/embedding_permissions.py,sha256=
|
|
5
|
-
fontbro/exceptions.py,sha256=
|
|
6
|
-
fontbro/family_classification.py,sha256=
|
|
4
|
+
fontbro/embedding_permissions.py,sha256=onB4N_ug5yOKbkEIlk9Utl2ZnqP_kHVxZsjTWmpflQA,4282
|
|
5
|
+
fontbro/exceptions.py,sha256=Nw-VBSOMgZ_a9l9sxHF3q2xYUpXoUXeHj1CyQivjGJ0,1243
|
|
6
|
+
fontbro/family_classification.py,sha256=R9IoVEEUMOSpS80p_2fF6QC1E-PaIhI04P3AXsnJ9BY,10352
|
|
7
7
|
fontbro/features.py,sha256=KNcaP-bfQ1kn_2tGWOofooxwtmU_aM0jk6hmw8y8ebw,4076
|
|
8
8
|
fontbro/files.py,sha256=RS5f_XX3Fn8iXqbWMlFNtL8kAiXq-h7ZKWvM915qNjY,3355
|
|
9
9
|
fontbro/fingerprint.py,sha256=feDMpc0JAZqcOZ0ntH3lglbp-awg0nltpXOxcpbuZlg,1508
|
|
10
10
|
fontbro/flags.py,sha256=NIdKvelHytLpcbwzx-3s4aeNQKefU-7hlDV5XbBj3Oo,683
|
|
11
|
-
fontbro/font.py,sha256=
|
|
11
|
+
fontbro/font.py,sha256=iCQH-5xNqO4IWz2xUkx9jA_iKrWwrg0ERadbcMNXCLQ,73994
|
|
12
12
|
fontbro/glyphs.py,sha256=E4xyKLx8w1BeIPelMXOOqYt6-Z7ebu0foEIKo3FYEIg,1947
|
|
13
13
|
fontbro/math.py,sha256=DZOiXmGu2g_1lBiEaSifWCWf1VeSYv9ijuWDZYy6xUQ,349
|
|
14
|
-
fontbro/metadata.py,sha256=
|
|
14
|
+
fontbro/metadata.py,sha256=gBl5L9ynAMTLT-77xQ9MFJ41Rf1qh5IyC85_Y4-tXFA,264
|
|
15
15
|
fontbro/metrics.py,sha256=JUt-u65dDMXHekYKGxo_254Xk4W87UjEDLZFwuzkh8M,6401
|
|
16
|
-
fontbro/monospace.py,sha256=
|
|
17
|
-
fontbro/names.py,sha256=
|
|
18
|
-
fontbro/pixel.py,sha256=
|
|
16
|
+
fontbro/monospace.py,sha256=hjmjOGBwTt_qMDVNc0hlareMD2v6gi8oZ_aYhB_hcus,2988
|
|
17
|
+
fontbro/names.py,sha256=NPtN1Ycd9ZqhBBJUqhTsZr-laTx2PIrihwmcluQb4mc,21949
|
|
18
|
+
fontbro/pixel.py,sha256=8wa0vJ5CAhPHPSjIfBaaIIhJJClu25tbL71PZr5WcVE,7834
|
|
19
19
|
fontbro/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
20
|
-
fontbro/render.py,sha256=
|
|
20
|
+
fontbro/render.py,sha256=iVUzD0bFIVcKbYMX0BdcWrWv2BYTWZ16_8EC0YJH19Q,2762
|
|
21
21
|
fontbro/sanitize.py,sha256=nt1Ki1rWnnXEt5t4W4EbLRHl8K_HD57agYNtW_pEiVI,1265
|
|
22
22
|
fontbro/style_flags.py,sha256=2FD--5t6hkA-htP2Wuwi8fMe9NuQ23XWUKaCmOStFlY,4712
|
|
23
23
|
fontbro/subset.py,sha256=TJPaUri8Hax9orZXBv-zFjPQsKyLEUzGj0oDWUDcjv8,2476
|
|
24
24
|
fontbro/support.py,sha256=-hHc7C_AxRl7eK0sSwkkSv4eOqHJtFNNZRpVp8c1KPQ,9905
|
|
25
|
-
fontbro/tables.py,sha256
|
|
25
|
+
fontbro/tables.py,sha256=-SoAJl2yAabHj1ko8l26GuUYDgkKYuMz6L40Qsx6CwE,4619
|
|
26
26
|
fontbro/unicode.py,sha256=kmDUCfxeMD-B6TQZa6EnXkdFTLpD9qnZ9YzLldRM6jc,6432
|
|
27
27
|
fontbro/utils.py,sha256=iodvxg9rzSKGuLdOacPxH5G9LZw1qGMUaYH4eZbnkik,996
|
|
28
|
-
fontbro/variable.py,sha256=
|
|
28
|
+
fontbro/variable.py,sha256=llOw5T91FYiGCzSVvXag-d9R8T4v1bsfzAV0F1ks2ZM,14065
|
|
29
29
|
fontbro/version.py,sha256=3_m26Gpu6wfSE7WNnTEXENiP2hsYrBZjM_DS1awDqjU,9336
|
|
30
30
|
fontbro/data/family-classifications.json,sha256=2aj7AXVqwqQDSvGUOLIso3GrxH3motsa6a1SPxU4hZ4,24173
|
|
31
31
|
fontbro/data/features.json,sha256=QS539T_e0U8M-_m125ZvgNDpN_geKjAM1raDi90vqIM,30367
|
|
32
32
|
fontbro/data/unicode-blocks.json,sha256=F2qA46RN3X-__n9T8_VVdKr77Tj9iy2N4tUnnQOBmPI,28353
|
|
33
33
|
fontbro/data/unicode-scripts.json,sha256=XDiyXLN2TB6Oo8AjVODuV72Y1_kkCwL6-GrosdeprsU,17069
|
|
34
|
-
python_fontbro-0.
|
|
35
|
-
python_fontbro-0.
|
|
36
|
-
python_fontbro-0.
|
|
37
|
-
python_fontbro-0.
|
|
38
|
-
python_fontbro-0.
|
|
34
|
+
python_fontbro-0.32.0.dist-info/licenses/LICENSE.txt,sha256=NOGOw1tmp315ex224Or-4PWS9W5jbVWxcqbf2KW-EK8,1078
|
|
35
|
+
python_fontbro-0.32.0.dist-info/METADATA,sha256=zV6nZwGAbQD8Q6ZiLX9LRE-qs-vwbKfuPt18jbCXAss,43403
|
|
36
|
+
python_fontbro-0.32.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
37
|
+
python_fontbro-0.32.0.dist-info/top_level.txt,sha256=RPG3vBzvn6LKU2E_FNPBPuKlygx9aodErAYJNeyV3Jc,8
|
|
38
|
+
python_fontbro-0.32.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|