Skip to content

Commit

Permalink
Merge pull request #102132 from Ivorforce/optimize-font-internal-cow
Browse files Browse the repository at this point in the history
Optimize `Font` calculations by avoiding unnecessary copy-on-write.
  • Loading branch information
Repiteo committed Feb 7, 2025
2 parents 2d1d51a + f52c9e3 commit 568d628
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions scene/resources/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,10 @@ real_t Font::get_height(int p_font_size) const {
if (dirty_rids) {
_update_rids();
}

real_t ret = 0.f;
for (int i = 0; i < rids.size(); i++) {
ret = MAX(ret, TS->font_get_ascent(rids[i], p_font_size) + TS->font_get_descent(rids[i], p_font_size));
ret = MAX(ret, TS->font_get_ascent(rids.get(i), p_font_size) + TS->font_get_descent(rids.get(i), p_font_size));
}
return ret + get_spacing(TextServer::SPACING_BOTTOM) + get_spacing(TextServer::SPACING_TOP);
}
Expand All @@ -223,7 +224,7 @@ real_t Font::get_ascent(int p_font_size) const {
}
real_t ret = 0.f;
for (int i = 0; i < rids.size(); i++) {
ret = MAX(ret, TS->font_get_ascent(rids[i], p_font_size));
ret = MAX(ret, TS->font_get_ascent(rids.get(i), p_font_size));
}
return ret + get_spacing(TextServer::SPACING_TOP);
}
Expand All @@ -234,7 +235,7 @@ real_t Font::get_descent(int p_font_size) const {
}
real_t ret = 0.f;
for (int i = 0; i < rids.size(); i++) {
ret = MAX(ret, TS->font_get_descent(rids[i], p_font_size));
ret = MAX(ret, TS->font_get_descent(rids.get(i), p_font_size));
}
return ret + get_spacing(TextServer::SPACING_BOTTOM);
}
Expand All @@ -245,7 +246,7 @@ real_t Font::get_underline_position(int p_font_size) const {
}
real_t ret = 0.f;
for (int i = 0; i < rids.size(); i++) {
ret = MAX(ret, TS->font_get_underline_position(rids[i], p_font_size));
ret = MAX(ret, TS->font_get_underline_position(rids.get(i), p_font_size));
}
return ret + get_spacing(TextServer::SPACING_TOP);
}
Expand All @@ -256,7 +257,7 @@ real_t Font::get_underline_thickness(int p_font_size) const {
}
real_t ret = 0.f;
for (int i = 0; i < rids.size(); i++) {
ret = MAX(ret, TS->font_get_underline_thickness(rids[i], p_font_size));
ret = MAX(ret, TS->font_get_underline_thickness(rids.get(i), p_font_size));
}
return ret;
}
Expand Down Expand Up @@ -476,9 +477,9 @@ Size2 Font::get_char_size(char32_t p_char, int p_font_size) const {
_update_rids();
}
for (int i = 0; i < rids.size(); i++) {
if (TS->font_has_char(rids[i], p_char)) {
int32_t glyph = TS->font_get_glyph_index(rids[i], p_font_size, p_char, 0);
return Size2(TS->font_get_glyph_advance(rids[i], p_font_size, glyph).x, get_height(p_font_size));
if (TS->font_has_char(rids.get(i), p_char)) {
int32_t glyph = TS->font_get_glyph_index(rids.get(i), p_font_size, p_char, 0);
return Size2(TS->font_get_glyph_advance(rids.get(i), p_font_size, glyph).x, get_height(p_font_size));
}
}
return Size2();
Expand All @@ -489,10 +490,10 @@ real_t Font::draw_char(RID p_canvas_item, const Point2 &p_pos, char32_t p_char,
_update_rids();
}
for (int i = 0; i < rids.size(); i++) {
if (TS->font_has_char(rids[i], p_char)) {
int32_t glyph = TS->font_get_glyph_index(rids[i], p_font_size, p_char, 0);
TS->font_draw_glyph(rids[i], p_canvas_item, p_font_size, p_pos, glyph, p_modulate);
return TS->font_get_glyph_advance(rids[i], p_font_size, glyph).x;
if (TS->font_has_char(rids.get(i), p_char)) {
int32_t glyph = TS->font_get_glyph_index(rids.get(i), p_font_size, p_char, 0);
TS->font_draw_glyph(rids.get(i), p_canvas_item, p_font_size, p_pos, glyph, p_modulate);
return TS->font_get_glyph_advance(rids.get(i), p_font_size, glyph).x;
}
}
return 0.f;
Expand All @@ -503,10 +504,10 @@ real_t Font::draw_char_outline(RID p_canvas_item, const Point2 &p_pos, char32_t
_update_rids();
}
for (int i = 0; i < rids.size(); i++) {
if (TS->font_has_char(rids[i], p_char)) {
int32_t glyph = TS->font_get_glyph_index(rids[i], p_font_size, p_char, 0);
TS->font_draw_glyph_outline(rids[i], p_canvas_item, p_font_size, p_size, p_pos, glyph, p_modulate);
return TS->font_get_glyph_advance(rids[i], p_font_size, glyph).x;
if (TS->font_has_char(rids.get(i), p_char)) {
int32_t glyph = TS->font_get_glyph_index(rids.get(i), p_font_size, p_char, 0);
TS->font_draw_glyph_outline(rids.get(i), p_canvas_item, p_font_size, p_size, p_pos, glyph, p_modulate);
return TS->font_get_glyph_advance(rids.get(i), p_font_size, glyph).x;
}
}
return 0.f;
Expand All @@ -518,7 +519,7 @@ bool Font::has_char(char32_t p_char) const {
_update_rids();
}
for (int i = 0; i < rids.size(); i++) {
if (TS->font_has_char(rids[i], p_char)) {
if (TS->font_has_char(rids.get(i), p_char)) {
return true;
}
}
Expand All @@ -531,7 +532,7 @@ String Font::get_supported_chars() const {
}
String chars;
for (int i = 0; i < rids.size(); i++) {
String data_chars = TS->font_get_supported_chars(rids[i]);
String data_chars = TS->font_get_supported_chars(rids.get(i));
for (int j = 0; j < data_chars.length(); j++) {
if (chars.find_char(data_chars[j]) == -1) {
chars += data_chars[j];
Expand Down

0 comments on commit 568d628

Please sign in to comment.