GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2015-08-26 05:47:28

gnysek
Member
Registered: 2011-12-29
Posts: 36

string_wordwrap_ext()

There is already script which break text after as many characters as you want. But, what if you want to have text with line breaks in same positions that in draw_text_ext (and will give same result when checked against string_width_ext/string_height_ext) ? For example, if you want to draw text character by character, and wan't to have non-proportional font, using draw_text_ext last word will always jump when becoming longer. With this script - you will not only get # insterted, you can also use normal draw_text from now!

hardbreak param is to hard break words - something that draw_text_ext doesn't do.

I've checked this script with normal text, words longer than width in pixels, and with text that already have newlines inserted. I'm not sure it's optimal yet, maybe it can work better :)

Also, you need to use draw_set_font(xxxx) before calling it, to have proper breaks.

Expand/// string_wordwrap_ext(string,length,hardbreak)
//
//  Returns a string which have newline # insterted
//  on same positions, that draw_text_ext would insert them
//  ! remember to set font style before using !
//
//      string      text to word wrap, string
//      length      maximum string length before a line break, real
//      hardbreak   to break if a word is longer than max length (word like this will always start from new line)
//
/// GMLscripts.com/license
{
    var str,len,brk,out,inbuff,wordbuff,currchar;
    str = argument0;
    len = argument1;
    brk = argument2;
    out = "";
    inbuff = "";
    wordbuff = "";
    currchar = "";
    
    while (string_length(str)) {
        currchar = string_copy(str,1,1);
        str = string_delete(str,1,1);
        
        if (currchar == "#") {
            out += inbuff + wordbuff + currchar;
            inbuff = "";
            wordbuff = "";
        } else if (currchar == " ") {
            inbuff += wordbuff;
            wordbuff = currchar;
        } else {
            wordbuff += currchar;
        }
        
        if (string_length(str) == 0) {
            out += inbuff + wordbuff;
        } else if ((brk == true) and (string_width(wordbuff) > len)) {
            out += string_copy(wordbuff, 1, string_length(wordbuff)-1) + '#';
            wordbuff = string_copy(wordbuff, string_length(wordbuff), 1);
        } else if (string_length(inbuff) > 0 and string_width(inbuff + wordbuff) > len) {
            out += inbuff + "#";
            inbuff = "";
            if (string_copy(wordbuff,1,1) = " ") {
                wordbuff = string_delete(wordbuff,1,1);
            }
        }
        
    }
    return out;
}

RSoUKKX.png

Last edited by gnysek (2015-08-26 07:51:46)

Offline

#2 2015-08-26 07:30:31

xot
Administrator
Registered: 2007-08-18
Posts: 1,240

Re: string_wordwrap_ext()

Hi Piotr, thanks for the submission. There actually is a script like this but it's not easily found because it's in the "Graphics > Text" category.

http://www.gmlscripts.com/script/string_wordwrap_width

I'll certainly check yours out, though, and see how it compares.


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB