Skip to content

Commit 872e199

Browse files
committed
Add sanitize method.
1 parent ec897ba commit 872e199

File tree

398 files changed

+145
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

398 files changed

+145
-1
lines changed

README.md

Lines changed: 22 additions & 0 deletions

fontbro/font.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pathlib import Path
99

1010
import fsutil
11+
import ots
1112
from fontTools import unicodedata
1213
from fontTools.subset import Options as SubsetterOptions
1314
from fontTools.subset import Subsetter
@@ -549,6 +550,8 @@ def get_format(self, *, ignore_flavor=False):
549550
format = self.FORMAT_WOFF
550551
elif version == "wOF2":
551552
format = self.FORMAT_WOFF2
553+
if format is None:
554+
raise TypeError("Unable to get the font format.")
552555
return format
553556

554557
def get_glyphs(self):
@@ -1133,6 +1136,47 @@ def rename(self, *, family_name="", style_name="", update_style_flags=True):
11331136
if update_style_flags:
11341137
self.set_style_flags_by_subfamily_name()
11351138

1139+
def sanitize(self, *, strict=True):
1140+
"""
1141+
Sanitize the font file using OpenType Sanitizer.
1142+
https://github.com/googlefonts/ots-python
1143+
1144+
:param strict: If True (default), raises an exception even on sanitizer warnings.
1145+
If False, only raises an exception on sanitizer failure (non-zero exit code).
1146+
:type strict: bool
1147+
1148+
:raises Exception: If the OpenType Sanitizer reports an error during the sanitization process.
1149+
:return: None
1150+
1151+
:note: Uses OpenType Sanitizer (ots) to sanitize the font file.
1152+
Saves the font to a temporary directory and invokes the sanitizer on the saved file.
1153+
If `strict` is True (default), treats sanitizer warnings as errors.
1154+
If `strict` is False, only checks for sanitizer errors.
1155+
"""
1156+
with tempfile.TemporaryDirectory() as dest:
1157+
filename = self.get_filename()
1158+
filepath = fsutil.join_path(dest, filename)
1159+
filepath = self.save(filepath)
1160+
result = ots.sanitize(
1161+
filepath,
1162+
capture_output=True,
1163+
encoding="utf-8",
1164+
)
1165+
error_code = result.returncode
1166+
errors = result.stderr
1167+
if error_code:
1168+
exc = Exception(
1169+
f"OpenType Sanitizer returned non-zero exit code ({error_code}): \n{errors}"
1170+
)
1171+
print(exc)
1172+
raise exc
1173+
elif strict:
1174+
warnings = result.stdout
1175+
success_message = "File sanitized successfully!\n"
1176+
if warnings != success_message:
1177+
warnings = warnings.rstrip(success_message)
1178+
raise Exception(f"OpenType Sanitizer warnings: \n{warnings}")
1179+
11361180
def save(self, filepath=None, *, overwrite=False):
11371181
"""
11381182
Saves the font at filepath.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ classifiers = [
5353
dependencies = [
5454
"fonttools[lxml,woff,unicode,pathops] >= 4.43.0, < 5.0",
5555
"imagehash >= 4.2.1, < 5.0.0",
56+
"opentype-sanitizer >= 9.1.0, < 10.0.0",
5657
"pillow >= 8.4.0, < 11.0.0",
5758
"python-fsutil >= 0.9.3, < 1.0.0",
5859
]

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
fonttools[lxml,woff,unicode,pathops] == 4.46.0
22
imagehash == 4.3.1
3+
opentype-sanitizer == 9.1.0
34
pillow == 10.1.0
4-
python-fsutil == 0.12.0
5+
python-fsutil == 0.13.0
Binary file not shown.
49.6 KB
Binary file not shown.
408 Bytes
Binary file not shown.
344 Bytes
Binary file not shown.
55.9 KB
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)