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

Method repl_fontnames

src/__init__.py:7609–7684  ·  view source on GitHub ↗

Populate 'font_buffers'. For each font candidate, store its xref and the list of names by which PDF text may refer to it (there may be multiple).

(doc)

Source from the content-addressed store, hash-verified

7607 return new_buffer
7608
7609 def repl_fontnames(doc):
7610 """Populate 'font_buffers'.
7611
7612 For each font candidate, store its xref and the list of names
7613 by which PDF text may refer to it (there may be multiple).
7614 """
7615
7616 def norm_name(name):
7617 """Recreate font name that contains PDF hex codes.
7618
7619 E.g. #20 -> space, chr(32)
7620 """
7621 while "#" in name:
7622 p = name.find("#")
7623 c = int(name[p + 1 : p + 3], 16)
7624 name = name.replace(name[p : p + 3], chr(c))
7625 return name
7626
7627 def get_fontnames(doc, item):
7628 """Return a list of fontnames for an item of page.get_fonts().
7629
7630 There may be multiple names e.g. for Type0 fonts.
7631 """
7632 fontname = item[3]
7633 names = [fontname]
7634 fontname = doc.xref_get_key(item[0], "BaseFont")[1][1:]
7635 fontname = norm_name(fontname)
7636 if fontname not in names:
7637 names.append(fontname)
7638 descendents = doc.xref_get_key(item[0], "DescendantFonts")
7639 if descendents[0] != "array":
7640 return names
7641 descendents = descendents[1][1:-1]
7642 if descendents.endswith(" 0 R"):
7643 xref = int(descendents[:-4])
7644 descendents = doc.xref_object(xref, compressed=True)
7645 p1 = descendents.find("/BaseFont")
7646 if p1 >= 0:
7647 p2 = descendents.find("/", p1 + 1)
7648 p1 = min(descendents.find("/", p2 + 1), descendents.find(">>", p2 + 1))
7649 fontname = descendents[p2 + 1 : p1]
7650 fontname = norm_name(fontname)
7651 if fontname not in names:
7652 names.append(fontname)
7653 return names
7654
7655 for i in range(doc.page_count):
7656 for f in doc.get_page_fonts(i, full=True):
7657 font_xref = f[0] # font xref
7658 font_ext = f[1] # font file extension
7659 basename = f[3] # font basename
7660
7661 if font_ext not in ( # skip if not supported by fontTools
7662 "otf",
7663 "ttf",
7664 "woff",
7665 "woff2",
7666 ):

Callers

nothing calls this directly

Calls 6

get_fontnamesFunction · 0.85
FontClass · 0.85
get_page_fontsMethod · 0.80
extract_fontMethod · 0.80
getMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected