Thanks for adding comments.Bigfoot71 wrote: ↑Mon Jan 09, 2023 11:19 pm Wow, super cool! I wanted to understand your approach to the problem, so I restructured it a bit by commenting on the passages, I also took the liberty of putting the declaration of `words` in the `while` loop because even if a redefinition in a loop is slower than predefining it upstream it seems to me that it is preferable to do so since another loop takes care of "emptying" it afterwards, or maybe I did not understand a subtlety.
The while loop is usually faster than allocating the table and clearing the garbage afterwards; also garbage collection is often responsible for CPU spikes that sometimes produce micro-stutters. There's a lot of garbage being generated in other sections, but that was one thing I could do something about. Ideally the table should be out of the function but that would have looked too extreme.
There's a minor issue in one of the comments; 'If the alignment is not "justify", use the default alignment' should not say default alignment, but specified alignment. Also, the math.min() line lacks a comment that says that it calculates the minimum between the width at the top and the width at the bottom of the line, which is the width to use. If the width of the circle at the bottom of the line is smaller, it means we're close to the bottom of the circle.
On a side note, after thinking about it, I could not have done much to use the built-in justify even if it worked well in this version. My initial plan was to include two lines and use scissor to cut out the second line, but that would have required a canvas rather than a text object. Probably slower, and almost surely more resource-intensive. I've requested a feature that would have helped.
And note that this version does not handle newline characters (\n) in the string, and doesn't let you choose a different line spacing, which would be cool.