diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2019-04-30 18:33:00 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-30 18:33:00 +0200 |
commit | 06440408727cf30c2220b1a9225b14467302a8d0 (patch) | |
tree | e34264ba89a54b5f2fdb1b4c81fe17ad222c0783 /scene/resources/font.cpp | |
parent | 5902e15b9bd940c17e360cc4a95f2c175c8af0b6 (diff) | |
parent | 33c39e4c2595616c4f22fab1738c4e3a956e606b (diff) |
Merge pull request #27021 from MarianoGnu/font
Add and expose to Font a function to get the word-wraped text size
Diffstat (limited to 'scene/resources/font.cpp')
-rw-r--r-- | scene/resources/font.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/scene/resources/font.cpp b/scene/resources/font.cpp index c72ccc97db..128db3f109 100644 --- a/scene/resources/font.cpp +++ b/scene/resources/font.cpp @@ -96,6 +96,7 @@ void Font::_bind_methods() { ClassDB::bind_method(D_METHOD("get_height"), &Font::get_height); ClassDB::bind_method(D_METHOD("is_distance_field_hint"), &Font::is_distance_field_hint); ClassDB::bind_method(D_METHOD("get_string_size", "string"), &Font::get_string_size); + ClassDB::bind_method(D_METHOD("get_wordwrap_string_size", "string", "p_width"), &Font::get_wordwrap_string_size); ClassDB::bind_method(D_METHOD("has_outline"), &Font::has_outline); ClassDB::bind_method(D_METHOD("draw_char", "canvas_item", "position", "char", "next", "modulate", "outline"), &Font::draw_char, DEFVAL(-1), DEFVAL(Color(1, 1, 1)), DEFVAL(false)); ClassDB::bind_method(D_METHOD("update_changes"), &Font::update_changes); @@ -495,6 +496,38 @@ Size2 Font::get_string_size(const String &p_string) const { return Size2(w, get_height()); } + +Size2 Font::get_wordwrap_string_size(const String &p_string, float p_width) const { + + ERR_FAIL_COND_V(p_width <= 0, Vector2(0, get_height())); + + int l = p_string.length(); + if (l == 0) + return Size2(p_width, get_height()); + + float line_w = 0; + float h = 0; + float space_w = get_char_size(' ').width; + Vector<String> lines = p_string.split("\n"); + for (int i = 0; i < lines.size(); i++) { + h += get_height(); + String t = lines[i]; + line_w = 0; + Vector<String> words = t.split(" "); + for (int j = 0; j < words.size(); j++) { + line_w += get_string_size(words[j]).x; + if (line_w > p_width) { + h += get_height(); + line_w = get_string_size(words[j]).x; + } else { + line_w += space_w; + } + } + } + + return Size2(p_width, h); +} + void BitmapFont::set_fallback(const Ref<BitmapFont> &p_fallback) { ERR_FAIL_COND(p_fallback == this); |