MCPcopy Create free account
hub / github.com/pymupdf/PyMuPDF / set_subset_fontname

Function set_subset_fontname

src_classic/utils.py:5221–5242  ·  view source on GitHub ↗

Generate a name prefix to tag a font as subset. We use a random generator to select 6 upper case ASCII characters. The prefixed name must be put in the font xref as the "/BaseFont" value and in the FontDescriptor object as the '/FontName' value.

(new_xref)

Source from the content-addressed store, hash-verified

5219 return None
5220
5221 def set_subset_fontname(new_xref):
5222 """Generate a name prefix to tag a font as subset.
5223
5224 We use a random generator to select 6 upper case ASCII characters.
5225 The prefixed name must be put in the font xref as the "/BaseFont" value
5226 and in the FontDescriptor object as the '/FontName' value.
5227 """
5228 # The following generates a prefix like 'ABCDEF+'
5229 prefix = "".join(random.choices(tuple(string.ascii_uppercase), k=6)) + "+"
5230 font_str = doc.xref_object(new_xref, compressed=True)
5231 font_str = font_str.replace("/BaseFont/", "/BaseFont/" + prefix)
5232 df = doc.xref_get_key(new_xref, "DescendantFonts")
5233 if df[0] == "array":
5234 df_xref = int(df[1][1:-1].replace("0 R", ""))
5235 fd = doc.xref_get_key(df_xref, "FontDescriptor")
5236 if fd[0] == "xref":
5237 fd_xref = int(fd[1].replace("0 R", ""))
5238 fd_str = doc.xref_object(fd_xref, compressed=True)
5239 fd_str = fd_str.replace("/FontName/", "/FontName/" + prefix)
5240 doc.update_object(fd_xref, fd_str)
5241 doc.update_object(new_xref, font_str)
5242 return None
5243
5244 def build_subset(buffer, unc_set, gid_set):
5245 """Build font subset using fontTools.

Callers 2

subset_fontsMethod · 0.85
subset_fontsFunction · 0.85

Calls 3

xref_objectMethod · 0.80
xref_get_keyMethod · 0.80
update_objectMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…