Calculate length of a string for a built-in font. Args: fontname: name of the font. fontsize: font size points. encoding: encoding to use, 0=Latin (default), 1=Greek, 2=Cyrillic. Returns: (float) length of text.
(text: str, fontname: str ="helv", fontsize: float =11, encoding: int =0)
| 22489 | |
| 22490 | |
| 22491 | def get_text_length(text: str, fontname: str ="helv", fontsize: float =11, encoding: int =0) -> float: |
| 22492 | """Calculate length of a string for a built-in font. |
| 22493 | |
| 22494 | Args: |
| 22495 | fontname: name of the font. |
| 22496 | fontsize: font size points. |
| 22497 | encoding: encoding to use, 0=Latin (default), 1=Greek, 2=Cyrillic. |
| 22498 | Returns: |
| 22499 | (float) length of text. |
| 22500 | """ |
| 22501 | fontname = fontname.lower() |
| 22502 | basename = Base14_fontdict.get(fontname, None) |
| 22503 | |
| 22504 | glyphs = None |
| 22505 | if basename == "Symbol": |
| 22506 | glyphs = symbol_glyphs |
| 22507 | if basename == "ZapfDingbats": |
| 22508 | glyphs = zapf_glyphs |
| 22509 | if glyphs is not None: |
| 22510 | w = sum([glyphs[ord(c)][1] if ord(c) < 256 else glyphs[183][1] for c in text]) |
| 22511 | return w * fontsize |
| 22512 | |
| 22513 | if fontname in Base14_fontdict.keys(): |
| 22514 | return util_measure_string( |
| 22515 | text, Base14_fontdict[fontname], fontsize, encoding |
| 22516 | ) |
| 22517 | |
| 22518 | if fontname in ( |
| 22519 | "china-t", |
| 22520 | "china-s", |
| 22521 | "china-ts", |
| 22522 | "china-ss", |
| 22523 | "japan", |
| 22524 | "japan-s", |
| 22525 | "korea", |
| 22526 | "korea-s", |
| 22527 | ): |
| 22528 | return len(text) * fontsize |
| 22529 | |
| 22530 | raise ValueError(f"Font '{fontname}' is unsupported") |
| 22531 | |
| 22532 | |
| 22533 | def image_profile(img: ByteString) -> dict: |
no test coverage detected
searching dependent graphs…