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

Function get_char_widths

src_classic/utils.py:2975–3070  ·  view source on GitHub ↗

Get list of glyph information of a font. Notes: Must be provided by its XREF number. If we already dealt with the font, it will be recorded in doc.FontInfos. Otherwise we insert an entry there. Finally we return the glyphs for the font. This is a list of

(
    doc: Document, xref: int, limit: int = 256, idx: int = 0, fontdict: OptDict = None
)

Source from the content-addressed store, hash-verified

2973
2974
2975def get_char_widths(
2976 doc: Document, xref: int, limit: int = 256, idx: int = 0, fontdict: OptDict = None
2977) -> list:
2978 """Get list of glyph information of a font.
2979
2980 Notes:
2981 Must be provided by its XREF number. If we already dealt with the
2982 font, it will be recorded in doc.FontInfos. Otherwise we insert an
2983 entry there.
2984 Finally we return the glyphs for the font. This is a list of
2985 (glyph, width) where glyph is an integer controlling the char
2986 appearance, and width is a float controlling the char's spacing:
2987 width * fontsize is the actual space.
2988 For 'simple' fonts, glyph == ord(char) will usually be true.
2989 Exceptions are 'Symbol' and 'ZapfDingbats'. We are providing data for these directly here.
2990 """
2991 fontinfo = CheckFontInfo(doc, xref)
2992 if fontinfo is None: # not recorded yet: create it
2993 if fontdict is None:
2994 name, ext, stype, asc, dsc = _get_font_properties(doc, xref)
2995 fontdict = {
2996 "name": name,
2997 "type": stype,
2998 "ext": ext,
2999 "ascender": asc,
3000 "descender": dsc,
3001 }
3002 else:
3003 name = fontdict["name"]
3004 ext = fontdict["ext"]
3005 stype = fontdict["type"]
3006 ordering = fontdict["ordering"]
3007 simple = fontdict["simple"]
3008
3009 if ext == "":
3010 raise ValueError("xref is not a font")
3011
3012 # check for 'simple' fonts
3013 if stype in ("Type1", "MMType1", "TrueType"):
3014 simple = True
3015 else:
3016 simple = False
3017
3018 # check for CJK fonts
3019 if name in ("Fangti", "Ming"):
3020 ordering = 0
3021 elif name in ("Heiti", "Song"):
3022 ordering = 1
3023 elif name in ("Gothic", "Mincho"):
3024 ordering = 2
3025 elif name in ("Dotum", "Batang"):
3026 ordering = 3
3027 else:
3028 ordering = -1
3029
3030 fontdict["simple"] = simple
3031
3032 if name == "ZapfDingbats":

Callers

nothing calls this directly

Calls 5

CheckFontInfoFunction · 0.85
UpdateFontInfoFunction · 0.85
_get_char_widthsMethod · 0.80
_get_font_propertiesFunction · 0.70
appendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…