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)
| 22260 | |
| 22261 | |
| 22262 | def 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 | |
| 22299 | def get_pdf_str(s: str) -> str: |
no outgoing calls
no test coverage detected
searching dependent graphs…