From 67ab8a12fd6376fd7ee4c5be04a41fef0a7a25e3 Mon Sep 17 00:00:00 2001 From: "Jorj X. McKie" Date: Sat, 16 Dec 2023 04:03:40 -0400 Subject: [PATCH] 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. --- src/utils.py | 6 +++--- tests/test_general.py | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/utils.py b/src/utils.py index 725fe6752..39624d3df 100644 --- a/src/utils.py +++ b/src/utils.py @@ -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() @@ -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 diff --git a/tests/test_general.py b/tests/test_general.py index 3a91ff68e..818825be1 100644 --- a/tests/test_general.py +++ b/tests/test_general.py @@ -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