Skip to content

Commit e6420c9

Browse files
authored
Various small fixes (#53)
* Fix crash on Windows Without deleting the object the file stays locked preventing the removal of the temporary folder * Correctly center text in get_image() * Fix AttributeError in get_features() Closes #50 * Set unicode_name default to None Closes #51 * Check if chr() conversion is in acceptable range Closes #52 * Add unit tests for issue 50, 51, 52 * Expose all TTFont arguments * Fix typos in pull request template * Fix get_fingerprint()
1 parent 06880bd commit e6420c9

File tree

6 files changed

+37
-18
lines changed

6 files changed

+37
-18
lines changed

.github/pull_request_template.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ assignees: fabiocaccamo
1414

1515
**Checklist before requesting a review**
1616
- [ ] I have performed a self-review of my code.
17-
- [ ] I have added torough tests for the proposed changes.
18-
- [ ] I have run the tests and there are not errors.
17+
- [ ] I have added thorough tests for the proposed changes.
18+
- [ ] I have run the tests and there are no errors.

fontbro/font.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class Font:
201201
]
202202
_WIDTHS_BY_VALUE = {width["value"]: width for width in _WIDTHS}
203203

204-
def __init__(self, filepath, lazy=False):
204+
def __init__(self, filepath, **kwargs):
205205
"""
206206
Constructs a new Font instance loading a font file from the given filepath.
207207
@@ -213,22 +213,22 @@ def __init__(self, filepath, lazy=False):
213213
super().__init__()
214214

215215
self._filepath = None
216-
self._lazy = None
216+
self._kwargs = None
217217
self._ttfont = None
218218

219219
if isinstance(filepath, str):
220-
self._init_with_filepath(filepath, lazy=lazy)
220+
self._init_with_filepath(filepath, **kwargs)
221221
else:
222222
filepath_type = type(filepath).__name__
223223
raise ValueError(
224224
f"Invalid filepath type: expected str, found '{filepath_type}'."
225225
)
226226

227-
def _init_with_filepath(self, filepath, lazy=False):
227+
def _init_with_filepath(self, filepath, **kwargs):
228228
try:
229229
self._filepath = filepath
230-
self._lazy = lazy
231-
self._ttfont = TTFont(self._filepath, lazy=self._lazy)
230+
self._kwargs = kwargs
231+
self._ttfont = TTFont(self._filepath, **kwargs)
232232

233233
except TTLibError:
234234
raise ValueError(f"Invalid font at filepath: '{filepath}'.")
@@ -243,7 +243,7 @@ def clone(self):
243243
"""
244244
Creates a new Font instance reading the same binary file.
245245
"""
246-
return Font(self._filepath, lazy=self._lazy)
246+
return Font(self._filepath, **self._kwargs)
247247

248248
def close(self):
249249
"""
@@ -271,17 +271,17 @@ def get_characters(self, ignore_blank=False):
271271
glyfs = font.get("glyf")
272272
for code, char_name in cmap.items():
273273
code_hex = f"{code:04X}"
274-
char = chr(code)
274+
if 0 <= code < 0x110000:
275+
char = chr(code)
276+
else:
277+
continue
275278
if ascii.iscntrl(char):
276279
continue
277280
if glyfs and ignore_blank:
278281
glyf = glyfs.get(char_name)
279282
if glyf and glyf.numberOfContours == 0:
280283
continue
281-
try:
282-
unicode_name = unicodedata.name(char)
283-
except ValueError:
284-
pass
284+
unicode_name = unicodedata.name(char, None)
285285
unicode_block_name = unicodedata.block(code)
286286
unicode_script_tag = unicodedata.script(code)
287287
unicode_script_name = unicodedata.script_name(unicode_script_tag)
@@ -337,7 +337,10 @@ def get_features_tags(self):
337337
for table_tag in ["GPOS", "GSUB"]:
338338
if table_tag in font:
339339
table = font[table_tag].table
340-
feature_record = table.FeatureList.FeatureRecord or []
340+
try:
341+
feature_record = table.FeatureList.FeatureRecord or []
342+
except AttributeError:
343+
feature_record = []
341344
for feature in feature_record:
342345
features_tags.add(feature.FeatureTag)
343346
return sorted(features_tags)
@@ -357,11 +360,11 @@ def get_fingerprint(self, text=""):
357360

358361
text = text or "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
359362

360-
img = self.get_image(text=text[0], size=72)
363+
img = self.get_image(text=text, size=72)
361364
img_size = img.size
362365
img = img.resize((img_size[0] // 2, img_size[1] // 2))
363366
img = img.resize((img_size[0], img_size[1]), Image.Resampling.NEAREST)
364-
img = img.quantize(colors=2)
367+
img = img.quantize(colors=8)
365368
# img.show()
366369

367370
hash = imagehash.average_hash(img, hash_size=64)
@@ -484,7 +487,8 @@ def get_image(
484487
img_size = (img_width, img_height)
485488
img = img.resize(img_size)
486489
draw = ImageDraw.Draw(img)
487-
draw.text((0, 0), text, font=img_font, fill=color)
490+
draw.text((-img_bbox[0], -img_bbox[1]), text, font=img_font, fill=color)
491+
del img_font
488492
return img
489493

490494
def get_italic_angle(self):
23.9 KB
Binary file not shown.
69.8 KB
Binary file not shown.
260 KB
Binary file not shown.

tests/test_issues.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,18 @@ def test_issue_0048_get_characters_count(self):
1111
font = self._get_font("/issues/issue-0048/test.ttf")
1212
with self.assertRaises(TypeError):
1313
font.get_characters_count()
14+
15+
def test_issue_0050_get_features(self):
16+
font = self._get_font("/issues/issue-0050/LeagueGothic-Regular.otf")
17+
features = font.get_features_tags()
18+
self.assertEqual(features, ["kern"])
19+
20+
def test_issue_0051_get_characters_count(self):
21+
font = self._get_font("/issues/issue-0051/ANASTCN.ttf")
22+
chars_count = font.get_characters_count()
23+
self.assertEqual(chars_count, 212)
24+
25+
def test_issue_0052_get_characters_count(self):
26+
font = self._get_font("/issues/issue-0052/Bobbiefreebie.ttf")
27+
chars_count = font.get_characters_count()
28+
self.assertEqual(chars_count, 288)

0 commit comments

Comments
 (0)