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

Method insert_font

src/__init__.py:12201–12282  ·  view source on GitHub ↗
(self, fontname="helv", fontfile=None, fontbuffer=None,
                   set_simple=False, wmode=0, encoding=0)

Source from the content-addressed store, hash-verified

12199 return self.parent.get_page_xobjects(self.number)
12200
12201 def insert_font(self, fontname="helv", fontfile=None, fontbuffer=None,
12202 set_simple=False, wmode=0, encoding=0):
12203 doc = self.parent
12204 if doc is None:
12205 raise ValueError("orphaned object: parent is None")
12206 idx = 0
12207
12208 if fontname.startswith("/"):
12209 fontname = fontname[1:]
12210 inv_chars = INVALID_NAME_CHARS.intersection(fontname)
12211 if inv_chars != set():
12212 raise ValueError(f"bad fontname chars {inv_chars}")
12213
12214 font = CheckFont(self, fontname)
12215 if font is not None: # font already in font list of page
12216 xref = font[0] # this is the xref
12217 if CheckFontInfo(doc, xref): # also in our document font list?
12218 return xref # yes: we are done
12219 # need to build the doc FontInfo entry - done via get_char_widths
12220 doc.get_char_widths(xref)
12221 return xref
12222
12223 #--------------------------------------------------------------------------
12224 # the font is not present for this page
12225 #--------------------------------------------------------------------------
12226
12227 bfname = Base14_fontdict.get(fontname.lower(), None) # BaseFont if Base-14 font
12228
12229 serif = 0
12230 CJK_number = -1
12231 CJK_list_n = ["china-t", "china-s", "japan", "korea"]
12232 CJK_list_s = ["china-ts", "china-ss", "japan-s", "korea-s"]
12233
12234 try:
12235 CJK_number = CJK_list_n.index(fontname)
12236 serif = 0
12237 except Exception:
12238 # Verbose in PyMuPDF/tests.
12239 if g_exceptions_verbose > 1: exception_info()
12240 pass
12241
12242 if CJK_number < 0:
12243 try:
12244 CJK_number = CJK_list_s.index(fontname)
12245 serif = 1
12246 except Exception:
12247 # Verbose in PyMuPDF/tests.
12248 if g_exceptions_verbose > 1: exception_info()
12249 pass
12250
12251 if fontname.lower() in fitz_fontdescriptors.keys():
12252 import pymupdf_fonts
12253 fontbuffer = pymupdf_fonts.myfont(fontname) # make a copy
12254 del pymupdf_fonts
12255
12256 # install the font for the page
12257 if fontfile is not None:
12258 if type(fontfile) is str:

Callers 6

insert_textMethod · 0.80
insert_textboxMethod · 0.80
insert_textMethod · 0.80
insert_textboxMethod · 0.80
test_fontnameFunction · 0.80
test_insert_fontFunction · 0.80

Calls 6

_insertFontMethod · 0.95
CheckFontFunction · 0.85
CheckFontInfoFunction · 0.85
exception_infoFunction · 0.85
get_char_widthsMethod · 0.80
getMethod · 0.45

Tested by 2

test_fontnameFunction · 0.64
test_insert_fontFunction · 0.64