Merge pull request #27021 from MarianoGnu/font
Add and expose to Font a function to get the word-wraped text size
This commit is contained in:
commit
0644040872
2 changed files with 34 additions and 0 deletions
|
@ -96,6 +96,7 @@ void Font::_bind_methods() {
|
||||||
ClassDB::bind_method(D_METHOD("get_height"), &Font::get_height);
|
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("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_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("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("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);
|
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());
|
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) {
|
void BitmapFont::set_fallback(const Ref<BitmapFont> &p_fallback) {
|
||||||
|
|
||||||
ERR_FAIL_COND(p_fallback == this);
|
ERR_FAIL_COND(p_fallback == this);
|
||||||
|
|
|
@ -53,6 +53,7 @@ public:
|
||||||
|
|
||||||
virtual Size2 get_char_size(CharType p_char, CharType p_next = 0) const = 0;
|
virtual Size2 get_char_size(CharType p_char, CharType p_next = 0) const = 0;
|
||||||
Size2 get_string_size(const String &p_string) const;
|
Size2 get_string_size(const String &p_string) const;
|
||||||
|
Size2 get_wordwrap_string_size(const String &p_string, float p_width) const;
|
||||||
|
|
||||||
virtual bool is_distance_field_hint() const = 0;
|
virtual bool is_distance_field_hint() const = 0;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue