Skip to content

Commit 67ab8a1

Browse files
committed
Update test_general.py
Revive Document.subset_fonts We are assuring that the subset font is not empty by counting its Unicodes - which is not yet implemented. This fix replaces this by checking the glyph count.
1 parent 50ce442 commit 67ab8a1

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5456,8 +5456,8 @@ def build_subset(buffer, unc_set, gid_set):
54565456
try: # invoke fontTools subsetter
54575457
fts.main(args)
54585458
font = fitz.Font(fontfile=newfont_path)
5459-
new_buffer = font.buffer
5460-
if len(font.valid_codepoints()) == 0:
5459+
new_buffer = font.buffer # subset font binary
5460+
if font.glyph_count == 0: # intercept empty font
54615461
new_buffer = None
54625462
except Exception:
54635463
fitz.exception_info()
@@ -5606,7 +5606,7 @@ def find_buffer_by_name(name):
56065606
print(f'Cannot subset {fontname!r}.')
56075607
continue
56085608
if verbose:
5609-
print('Built subset of font {fontname!r}.')
5609+
print(f"Built subset of font {fontname!r}.")
56105610
val = doc._insert_font(fontbuffer=new_buffer) # store subset font in PDF
56115611
new_xref = val[0] # get its xref
56125612
set_subset_fontname(new_xref) # tag fontname as subset font

tests/test_general.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,3 +723,23 @@ def test_2736():
723723
assert text == "CropBox not in MediaBox"
724724

725725

726+
def test_subset_fonts():
727+
"""Confirm subset_fonts is working."""
728+
if not hasattr(fitz, "mupdf"):
729+
print("Not testing 'test_subset_fonts' in classic.")
730+
return
731+
text = "Just some arbitrary text."
732+
arch = fitz.Archive()
733+
css = fitz.css_for_pymupdf_font("ubuntu", archive=arch)
734+
css += "* {font-family: ubuntu;}"
735+
doc = fitz.open()
736+
page = doc.new_page()
737+
page.insert_htmlbox(page.rect, text, css=css, archive=arch)
738+
doc.subset_fonts(verbose=True)
739+
found = False
740+
for xref in range(1, doc.xref_length()):
741+
if doc.xref_is_font(xref):
742+
if "+Ubuntu#20Regular" in doc.xref_object(xref):
743+
found = True
744+
break
745+
assert found is True

0 commit comments

Comments
 (0)