Skip to content

Commit

Permalink
Update test_general.py
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JorjMcKie committed Dec 16, 2023
1 parent 50ce442 commit 67ab8a1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5456,8 +5456,8 @@ def build_subset(buffer, unc_set, gid_set):
try: # invoke fontTools subsetter
fts.main(args)
font = fitz.Font(fontfile=newfont_path)
new_buffer = font.buffer
if len(font.valid_codepoints()) == 0:
new_buffer = font.buffer # subset font binary
if font.glyph_count == 0: # intercept empty font
new_buffer = None
except Exception:
fitz.exception_info()
Expand Down Expand Up @@ -5606,7 +5606,7 @@ def find_buffer_by_name(name):
print(f'Cannot subset {fontname!r}.')
continue
if verbose:
print('Built subset of font {fontname!r}.')
print(f"Built subset of font {fontname!r}.")
val = doc._insert_font(fontbuffer=new_buffer) # store subset font in PDF
new_xref = val[0] # get its xref
set_subset_fontname(new_xref) # tag fontname as subset font
Expand Down
20 changes: 20 additions & 0 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,3 +723,23 @@ def test_2736():
assert text == "CropBox not in MediaBox"


def test_subset_fonts():
"""Confirm subset_fonts is working."""
if not hasattr(fitz, "mupdf"):
print("Not testing 'test_subset_fonts' in classic.")
return
text = "Just some arbitrary text."
arch = fitz.Archive()
css = fitz.css_for_pymupdf_font("ubuntu", archive=arch)
css += "* {font-family: ubuntu;}"
doc = fitz.open()
page = doc.new_page()
page.insert_htmlbox(page.rect, text, css=css, archive=arch)
doc.subset_fonts(verbose=True)
found = False
for xref in range(1, doc.xref_length()):
if doc.xref_is_font(xref):
if "+Ubuntu#20Regular" in doc.xref_object(xref):
found = True
break
assert found is True

0 comments on commit 67ab8a1

Please sign in to comment.