textwrap
textwrap copied to clipboard
Flow text into frames
- fantasizingly: this can be made non-constant to get some pretty dashing ASCII art flowing inside some particular shape like in inkscape. wow.
Textwrap takes a list of widths when wrapping text. This allows you to do things like cut out space for figures, but you also go further and wrap text inside circles, triangles and so on. I don't think it's very well known since i haven't created any demos with this yet :slightly_smiling_face:
Originally posted by @mgeisler in https://github.com/helix-editor/helix/issues/136#issuecomment-1399416641
wowww, that's super nice. Exactly what i was thinking.
- Do you have any idea how will you generate that list of widths?
- I can think that it will require some sort of stepped interpolation of the required curve at fixed intervals.
- Image editors likely do this inside.
- But i don't know how would i do this?
Title inspired by inkscape: ddg 🔍 : inkscape flow text
OT:
while searching for possible duplicates, i stumbled upon this: https://github.com/mgeisler/textwrap/issues/205#issuecomment-692941623 and wow, that can be used to implement typora like markup table source prettifier. nice.
Hi @goyalyashpal, thanks for writing! I'm glad you like this (mostly unknown) feature :smile:
- Do you have any idea how will you generate that list of widths?
I'm no expert on this, but I would imagine a desktop publishing (DTP) program would want to reserve a certain height and width for an embedded figure. So the program would do something like this
- The figure is 24 mm in height and 40 mm in width.
- The normal line width is 150 mm.
- The line height is 6 mm.
Thus, we need to shrink 10 lines by 40 mm. If the picture starts on line 3, the DTP program would have to use [150, 150, 110, 110, 110, 110, 150] as the line widths (the last line width is repeated as necessary). This should allow the DTP program to format a single paragraph with a cut-out for the figure.
Note that Textwrap fundamentally likes to format paragraphs. It also works with multiple paragraphs, but that's just because it splits on \n and formats each line by itself. A program that anchors a figure to paragraph would definitely want to format that paragraph by itself.
In the example above, one could imagine that the formatted paragraph is not height enough to contain the full figure:
Lorem ipsum dolor sit amet, consectetur
adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore +--------+
magnam aliquam quaerat . | |
| |
+--------+
The program can detect this because the wrapped paragraph has a height of 4 lines = 24 mm, but the picture started 2 lines down (12 mm) and it 24 mm tall. So the program knows that the picture sticks out by 12 mm or 2 lines. It can use this information to wrap the following paragraph appropriately (using [110, 150] as the line widths, assuming the paragraphs are spaced by 1 line of 6 mm).