Cropping the text at the bottom of a text field

dawidM

Member
Hello guys! One thing that is a bit strange in GMS is when you use draw_text_ext and you want to wrap a word it doesn’t work properly if one word is longer ( in pixels) than the width of the text field. Fortunately, I got some help from one user and it works almost perfectly now with one exception: I want to add one thing to the code I have now. I would like to ‘clip’ the text at the bottom of the text field if it is too long. So I would like to enter not only width but also height of the text field, and if the text is too long I wish it didn't show the remaining words/letters. So now I have the code below for wrapping :

Code:
/// scr_wrap_string(string,w)
var text = argument0, len = argument1;

var strlen = string_length(text);
var i = 1, c = '', w = '', word = '', line = '', result = '';

while (i <= strlen) {
    c = string_char_at(text, i++);
    w = string_char_at(word, 1);

    // text breaking
    if (c == '#') {
        result += line + word + '#';
        line = '';
        word = '';
        w = '';
        continue;
    }

    // line breaking
    if (line != '' && string_width(line + word) > len) {
        result += line + '#';
        line = '';
    }

    // word breaking
    if (string_width(word + c) > len) {
        line += word + '#';
        word = '';
        w = '';
    }

    // building a word
    // sequence of space buttons is treated as a word
    if ((c == ' ' && (w == '' || w == ' ')) || (c != ' ' && w != ' ')) {
        word += c;
    }
    else {
        line += word;
        word = c;
       
        // the line cannot be started with the space button
        if (string_char_at(line, 1) == ' ') {
            line = '';
        }
    }
}

result += line;
result += word;
return result;
And in the draw event I use this:

Code:
var text = scr_wrap_string("Lorem ipsum dolor sit amet,                     andverylongwordthatmustbesplitted", 100);
draw_set_color(c_black);

draw_rectangle(10, 10, 110, 210, false); // drawing text box

draw_set_color(c_white);
draw_text(10, 10, text);
How can I clip a text from the bottom of the text field? I would be grateful for help.

Kind regards,
Dawid
 

dawidM

Member
I am going to have 100+ text boxes like this in the single room. I've read that surfaces can take a lot of memory - I am not sure about that, I've never used surfaces before. Would you still advise to use surfaces in the case of one room 100+ text fields?

Regards,
dawidM
 
Does the text change at all? Or is it simply static text? Or does it change infrequently? Because it all depends. 100+ text draw calls is heavy as well. What exactly are the 100+ text boxes there for?
 

dawidM

Member
I create a program which is more or less an interactive mindmap, so you have many text boxes and you can resize and move them. You can also add and delete some text inside those boxes, delete them and so on. I have already programmed the engine and all this stuff like moving , resizing etc are implemented and work ok. The only problem is that when you squeeze the text box and it is very narrow, then the text is drawn below it (if the text is too long) which doesn't look very professional. I would like to simply crop the text which is below the text box. Maybe there is an option to avoid using surfaces in order to not complicate things. What do you think?

EDIT. I try to deactivate those boxes which are out of the view, so in this case 100+boxes is not that heavy
 
Last edited:
Is it on mobile? Surfaces are the most straightforward way of doing this. Another would be to implement a minimum box size. However, you could do essentially the same thing you are doing with the word wrap, but checking for text_height_ext() (in fact, you can combine it with your wordwrap script). Simply check the height of the overall text after each new line gets triggered and break out of the script when you've reached the maximum height you want, returning the result without the entirety of the text you inputted.
 
Top