Split line of text
I was wondering if there is an algorithm for splitting a string into multiple lines so that the resulting set of multiple rows fits into a square shape rather than a wide rectangular shape. Let me give you some examples,
Input: Hello, this is a really long line.
Conclusion:
Hi, this is a really long line
Input: abcdef
Conclusion:
abc
def
Input: It's really such a looooooooooooooooooooong line. This is the end.
Conclusion:
It really is such a looooooooooooooooooooong
line This is the end.
If you see in the above examples, the input line fits into a wide rectangle. But the exit fits more or less into a square shape.
Essentially, here you just need to count the number of characters in the string, take the square root of this number. Then enter the square number of characters on each line. But in the above example, the splitting should be done with word wrappers instead of characters. Is there a standard algorithm for this? Any code examples / pointers would be appreciated!
a source to share
As you noticed, this basically means splitting lines with min and max widths, not just max widths, which is usually done. (And your example shows that this isn't always possible if you can't hyphenate words.) TeX can do this (look at the command \parshape
): its line break algorithm supports arbitrary forms (and hyphens) and is considered modern. Therefore, if you want to create squares in any serious way, you should definitely adapt the Knuth / Plass algorithm ( http://defoe.sourceforge.net/folio/knuth-plass.html ).
a source to share