GML Text String Justification - how can I disperse the spaces more evenly?

jerob1995

Member
I have been working on a function to draw a text string of any size and wrap it to a certain width and justify the text by adding spaces. I have it working however the spaces are added to each line starting from the left until the line is the correct width which makes it look kind of off-balance. Here is a screen-shot of the system and I have replaced the added spaces with "#" to make it more clear what I mean.

2021-02-01.png


I am just not sure of the best way to evenly disperse the spaces over the whole line without things getting pretty messy. My closest attempt so far was to divide the number of spaces on a line by the number of spaces needed to be added and then loop through the characters on the line and add a space to every nth space however sometimes too many or too few spaces were added which ruined the justification. Any ideas or points to resources would be much appreciated!
 

obscene

Member
I think you've done the best you could do unless you wanted to break it up line by line and use draw_text_ext(x, y, string, sep, w); for each unique line and then adjust the separation, which would probably be slower than your current method.
 
P

ParodyKnaveBob

Guest
My closest attempt so far was to divide the number of spaces on a line by the number of spaces needed to be added and then loop through the characters on the line and add a space to every nth space
This sounds like a good plan and that your math/logic might've had a small issue with it. What formula did you use? ("What's your code?")
Even YouTube has the famous 301 Viewers off-by-one error. $;^ J
 
Top