GML How to Solve this String Wrap Problem?

Edwin

Member
Hello, everyone.

I'm having a quite silly problem; I'll start from the beginning.

I made a drawing text script that gets the tags from string and then converts these tags into new drawing functions. Having this I can't string warp the string regarding the width of it, because it will check not untagged string, so when I delete the tags, it will show me wrong linebreak positions.

Без имени-1.jpg

String looks like this after using draw_text_tag().

Now when I use string wordwrapping (which just places "#" string every X width in the string), it will look like this:

Без имени-2.jpg

This is immediately noticeable if the text is large enough.

Script which I'm using string_wordwrap_width() is just slightly modified script taken from this source.
Code:
/// string_wordwrap_width(string,width)

// Declare variables
var pos_space, pos_current, text_current, text_output;
pos_space = -1;
pos_current = 1;
text_current = argument[0];
text_output = "";

// Word wrap
while (string_length(text_current) >= pos_current) {
    if (string_width(string_copy(text_current, 1, pos_current)) > argument[1]) {
        if (pos_space != -1) {
            text_output += string_copy(text_current, 1, pos_space);
            text_output = string_insert_tag("#", text_output, string_length(text_output));
            text_current = string_copy(text_current, pos_space + 1, string_length(text_current) - (pos_space));
        } else {
            text_output += string_copy(text_current, 1, pos_current);
            text_output = string_insert("#", text_output, string_length(text_output) + 1);
            text_current = string_copy(text_current, pos_current + 1, string_length(text_current) - (pos_space));
        };
        pos_current = 1;
        pos_space = -1;
    };
    if (string_char_at(text_current,pos_current) == " ") {
        pos_space = pos_current;
    };
    pos_current ++;
};

// Finishing the text
if (string_length(text_current) > 0) {
    text_output += text_current;
};

// Replacing unnecessary spaces
text_output = string_replace_all(text_output, "# ", "#");

// Return ouput text
return text_output;
string_insert_tag() is just string_insert() function that ignores tags [colour=], [/colour], etc.

How should I get rid of this problem?
 

NightFrost

Member
You'll have to cut those tags out of the string in some manner to be able to measure the untagged length. The least amount of rewriting probably would be a temporary variable where you copy current letter of the string as you loop through it, and test that variable for length. You also check for tag opening "[" every step, and if one is found, instead of copying it to the string you enter a mode where you skip copying until a closing "]" is found. Use some variable to true/false flag this state and skip copy accordingly. Once the temporary variable holding the untagged text is long enough you do the linefeed stuff, then clear the variable in preparation for next row.
 

Edwin

Member
You'll have to cut those tags out of the string in some manner to be able to measure the untagged length. The least amount of rewriting probably would be a temporary variable where you copy current letter of the string as you loop through it, and test that variable for length. You also check for tag opening "[" every step, and if one is found, instead of copying it to the string you enter a mode where you skip copying until a closing "]" is found. Use some variable to true/false flag this state and skip copy accordingly. Once the temporary variable holding the untagged text is long enough you do the linefeed stuff, then clear the variable in preparation for next row.
Hey, thanks for feedback.

Yes, it's cool and interesting, but how to perform it?
 
Top