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

Function repl_fontnames

src_classic/utils.py:5319–5395  ·  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

5317 return new_buffer
5318
5319 def repl_fontnames(doc):
5320 """Populate 'font_buffers'.
5321
5322 For each font candidate, store its xref and the list of names
5323 by which PDF text may refer to it (there may be multiple).
5324 """
5325
5326 def norm_name(name):
5327 """Recreate font name that contains PDF hex codes.
5328
5329 E.g. #20 -> space, chr(32)
5330 """
5331 while "#" in name:
5332 p = name.find("#")
5333 c = int(name[p + 1 : p + 3], 16)
5334 name = name.replace(name[p : p + 3], chr(c))
5335 return name
5336
5337 def get_fontnames(doc, item):
5338 """Return a list of fontnames for an item of page.get_fonts().
5339
5340 There may be multiple names e.g. for Type0 fonts.
5341 """
5342 fontname = item[3]
5343 names = [fontname]
5344 fontname = doc.xref_get_key(item[0], "BaseFont")[1][1:]
5345 fontname = norm_name(fontname)
5346 if fontname not in names:
5347 names.append(fontname)
5348 descendents = doc.xref_get_key(item[0], "DescendantFonts")
5349 if descendents[0] != "array":
5350 return names
5351 descendents = descendents[1][1:-1]
5352 if descendents.endswith(" 0 R"):
5353 xref = int(descendents[:-4])
5354 descendents = doc.xref_object(xref, compressed=True)
5355 p1 = descendents.find("/BaseFont")
5356 if p1 >= 0:
5357 p2 = descendents.find("/", p1 + 1)
5358 p1 = min(descendents.find("/", p2 + 1), descendents.find(">>", p2 + 1))
5359 fontname = descendents[p2 + 1 : p1]
5360 fontname = norm_name(fontname)
5361 if fontname not in names:
5362 names.append(fontname)
5363 return names
5364
5365 for i in range(doc.page_count):
5366 for f in doc.get_page_fonts(i, full=True):
5367 font_xref = f[0] # font xref
5368 font_ext = f[1] # font file extension
5369 basename = f[3] # font basename
5370
5371 if font_ext not in ( # skip if not supported by fontTools
5372 "otf",
5373 "ttf",
5374 "woff",
5375 "woff2",
5376 ):

Callers 2

subset_fontsMethod · 0.85
subset_fontsFunction · 0.85

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

Used in the wild real call sites across dependent graphs

searching dependent graphs…