Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revive Document.subset_fonts #2900

Merged
merged 1 commit into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading