Fill a rectangle with text. Args: writer: pymupdf.TextWriter object (= "self") rect: rect-like to receive the text. text: string or list/tuple of strings. pos: point-like start position of first word. font: pymupdf.Font object (def
(
writer: 'TextWriter',
rect: rect_like,
text: typing.Union[str, list],
pos: point_like = None,
font: typing.Optional[Font] = None,
fontsize: float = 11,
lineheight: OptFloat = None,
align: int = 0,
warn: bool = None,
right_to_left: bool = False,
small_caps: bool = False,
)
| 16844 | return text |
| 16845 | |
| 16846 | def fill_textbox( |
| 16847 | writer: 'TextWriter', |
| 16848 | rect: rect_like, |
| 16849 | text: typing.Union[str, list], |
| 16850 | pos: point_like = None, |
| 16851 | font: typing.Optional[Font] = None, |
| 16852 | fontsize: float = 11, |
| 16853 | lineheight: OptFloat = None, |
| 16854 | align: int = 0, |
| 16855 | warn: bool = None, |
| 16856 | right_to_left: bool = False, |
| 16857 | small_caps: bool = False, |
| 16858 | ) -> tuple: |
| 16859 | """Fill a rectangle with text. |
| 16860 | |
| 16861 | Args: |
| 16862 | writer: pymupdf.TextWriter object (= "self") |
| 16863 | rect: rect-like to receive the text. |
| 16864 | text: string or list/tuple of strings. |
| 16865 | pos: point-like start position of first word. |
| 16866 | font: pymupdf.Font object (default pymupdf.Font('helv')). |
| 16867 | fontsize: the fontsize. |
| 16868 | lineheight: overwrite the font property |
| 16869 | align: (int) 0 = left, 1 = center, 2 = right, 3 = justify |
| 16870 | warn: (bool) text overflow action: none, warn, or exception |
| 16871 | right_to_left: (bool) indicate right-to-left language. |
| 16872 | """ |
| 16873 | rect = Rect(rect) |
| 16874 | if rect.is_empty: |
| 16875 | raise ValueError("fill rect must not empty.") |
| 16876 | if type(font) is not Font: |
| 16877 | font = Font("helv") |
| 16878 | |
| 16879 | def textlen(x): |
| 16880 | """Return length of a string.""" |
| 16881 | return font.text_length( |
| 16882 | x, fontsize=fontsize, small_caps=small_caps |
| 16883 | ) # abbreviation |
| 16884 | |
| 16885 | def char_lengths(x): |
| 16886 | """Return list of single character lengths for a string.""" |
| 16887 | return font.char_lengths(x, fontsize=fontsize, small_caps=small_caps) |
| 16888 | |
| 16889 | def append_this(pos, text): |
| 16890 | ret = writer.append( |
| 16891 | pos, text, font=font, fontsize=fontsize, small_caps=small_caps |
| 16892 | ) |
| 16893 | return ret |
| 16894 | |
| 16895 | tolerance = fontsize * 0.2 # extra distance to left border |
| 16896 | space_len = textlen(" ") |
| 16897 | std_width = rect.width - tolerance |
| 16898 | std_start = rect.x0 + tolerance |
| 16899 | |
| 16900 | def norm_words(width, words): |
| 16901 | """Cut any word in pieces no longer than 'width'.""" |
| 16902 | nwords = [] |
| 16903 | word_lengths = [] |