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

Function getTJstr

src/__init__.py:22262–22296  ·  view source on GitHub ↗

Return a PDF string enclosed in [] brackets, suitable for the PDF TJ operator. Notes: The input string is converted to either 2 or 4 hex digits per character. Args: simple: no glyphs: 2-chars, use char codes as the glyph glyphs: 2-chars, use glyphs inste

(text: str, glyphs: typing.Union[list, tuple, None], simple: bool, ordering: int)

Source from the content-addressed store, hash-verified

22260
22261
22262def getTJstr(text: str, glyphs: typing.Union[list, tuple, None], simple: bool, ordering: int) -> str:
22263 """ Return a PDF string enclosed in [] brackets, suitable for the PDF TJ
22264 operator.
22265
22266 Notes:
22267 The input string is converted to either 2 or 4 hex digits per character.
22268 Args:
22269 simple: no glyphs: 2-chars, use char codes as the glyph
22270 glyphs: 2-chars, use glyphs instead of char codes (Symbol,
22271 ZapfDingbats)
22272 not simple: ordering < 0: 4-chars, use glyphs not char codes
22273 ordering >=0: a CJK font! 4 chars, use char codes as glyphs
22274 """
22275 if text.startswith("[<") and text.endswith(">]"): # already done
22276 return text
22277
22278 if not bool(text):
22279 return "[<>]"
22280
22281 if simple: # each char or its glyph is coded as a 2-byte hex
22282 if glyphs is None: # not Symbol, not ZapfDingbats: use char code
22283 otxt = "".join([f"{ord(c):02x}" if ord(c) < 256 else "b7" for c in text])
22284 else: # Symbol or ZapfDingbats: use glyphs
22285 otxt = "".join(
22286 [f"{glyphs[ord(c)][0]:02x}" if ord(c) < 256 else "b7" for c in text]
22287 )
22288 return "[<" + otxt + ">]"
22289
22290 # non-simple fonts: each char or its glyph is coded as 4-byte hex
22291 if ordering < 0: # not a CJK font: use the glyphs
22292 otxt = "".join([f"{glyphs[ord(c)][0]:04x}" for c in text])
22293 else: # CJK: use the char codes
22294 otxt = "".join([f"{ord(c):04x}" for c in text])
22295
22296 return "[<" + otxt + ">]"
22297
22298
22299def get_pdf_str(s: str) -> str:

Callers 4

insert_textMethod · 0.85
insert_textboxMethod · 0.85
insert_textMethod · 0.85
insert_textboxMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…