Fill a rectangle with text. Args: writer: 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: Font object (default Font('helv')). fontsize: th
(
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,
)
| 4297 | |
| 4298 | |
| 4299 | def fill_textbox( |
| 4300 | writer: TextWriter, |
| 4301 | rect: rect_like, |
| 4302 | text: typing.Union[str, list], |
| 4303 | pos: point_like = None, |
| 4304 | font: typing.Optional[Font] = None, |
| 4305 | fontsize: float = 11, |
| 4306 | lineheight: OptFloat = None, |
| 4307 | align: int = 0, |
| 4308 | warn: bool = None, |
| 4309 | right_to_left: bool = False, |
| 4310 | small_caps: bool = False, |
| 4311 | ) -> tuple: |
| 4312 | """Fill a rectangle with text. |
| 4313 | |
| 4314 | Args: |
| 4315 | writer: TextWriter object (= "self") |
| 4316 | rect: rect-like to receive the text. |
| 4317 | text: string or list/tuple of strings. |
| 4318 | pos: point-like start position of first word. |
| 4319 | font: Font object (default Font('helv')). |
| 4320 | fontsize: the fontsize. |
| 4321 | lineheight: overwrite the font property |
| 4322 | align: (int) 0 = left, 1 = center, 2 = right, 3 = justify |
| 4323 | warn: (bool) text overflow action: none, warn, or exception |
| 4324 | right_to_left: (bool) indicate right-to-left language. |
| 4325 | """ |
| 4326 | rect = Rect(rect) |
| 4327 | if rect.is_empty: |
| 4328 | raise ValueError("fill rect must not empty.") |
| 4329 | if type(font) is not Font: |
| 4330 | font = Font("helv") |
| 4331 | |
| 4332 | def textlen(x): |
| 4333 | """Return length of a string.""" |
| 4334 | return font.text_length( |
| 4335 | x, fontsize=fontsize, small_caps=small_caps |
| 4336 | ) # abbreviation |
| 4337 | |
| 4338 | def char_lengths(x): |
| 4339 | """Return list of single character lengths for a string.""" |
| 4340 | return font.char_lengths(x, fontsize=fontsize, small_caps=small_caps) |
| 4341 | |
| 4342 | def append_this(pos, text): |
| 4343 | return writer.append( |
| 4344 | pos, text, font=font, fontsize=fontsize, small_caps=small_caps |
| 4345 | ) |
| 4346 | |
| 4347 | tolerance = fontsize * 0.2 # extra distance to left border |
| 4348 | space_len = textlen(" ") |
| 4349 | std_width = rect.width - tolerance |
| 4350 | std_start = rect.x0 + tolerance |
| 4351 | |
| 4352 | def norm_words(width, words): |
| 4353 | """Cut any word in pieces no longer than 'width'.""" |
| 4354 | nwords = [] |
| 4355 | word_lengths = [] |
| 4356 | for w in words: |
nothing calls this directly
no test coverage detected
searching dependent graphs…