MCPcopy Index your code
hub / github.com/pymupdf/PyMuPDF / set_subset_fontname

Method set_subset_fontname

src/__init__.py:7521–7543  ·  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

7519 return None
7520
7521 def set_subset_fontname(new_xref):
7522 """Generate a name prefix to tag a font as subset.
7523
7524 We use a random generator to select 6 upper case ASCII characters.
7525 The prefixed name must be put in the font xref as the "/BaseFont" value
7526 and in the FontDescriptor object as the '/FontName' value.
7527 """
7528 # The following generates a prefix like 'ABCDEF+'
7529 import random
7530 import string
7531 prefix = "".join(random.choices(tuple(string.ascii_uppercase), k=6)) + "+"
7532 font_str = doc.xref_object(new_xref, compressed=True)
7533 font_str = font_str.replace("/BaseFont/", "/BaseFont/" + prefix)
7534 df = doc.xref_get_key(new_xref, "DescendantFonts")
7535 if df[0] == "array":
7536 df_xref = int(df[1][1:-1].replace("0 R", ""))
7537 fd = doc.xref_get_key(df_xref, "FontDescriptor")
7538 if fd[0] == "xref":
7539 fd_xref = int(fd[1].replace("0 R", ""))
7540 fd_str = doc.xref_object(fd_xref, compressed=True)
7541 fd_str = fd_str.replace("/FontName/", "/FontName/" + prefix)
7542 doc.update_object(fd_xref, fd_str)
7543 doc.update_object(new_xref, font_str)
7544
7545 def build_subset(buffer, unc_set, gid_set):
7546 """Build font subset using fontTools.

Callers

nothing calls this directly

Calls 3

xref_objectMethod · 0.80
xref_get_keyMethod · 0.80
update_objectMethod · 0.80

Tested by

no test coverage detected