summaryrefslogtreecommitdiff
path: root/scene/resources/font.cpp
diff options
context:
space:
mode:
authorMariano Suligoy <marianognu.easyrpg@gmail.com>2019-03-13 15:14:50 -0300
committerMariano Suligoy <marianognu.easyrpg@gmail.com>2019-04-30 12:03:17 +0200
commit33c39e4c2595616c4f22fab1738c4e3a956e606b (patch)
tree77717e0580d822eb8e258949c1240cfa214b887a /scene/resources/font.cpp
parentceebd748b94a3ba5d294b24a863a9e9c70a1b721 (diff)
Add and expose to Font a function to get the rect size needed to draw a word-wraped text
Diffstat (limited to 'scene/resources/font.cpp')
-rw-r--r--scene/resources/font.cpp33
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);