mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:07:34 +00:00
LibGfx: Consider multi-code point glyphs when computing text width
Currently, we compute the width of text one code point at a time. This ignores grapheme clusters (emoji in particular). One effect of this is when highlighting a multi-code point emoji. We will errantly increase the highlight rect to the sum of all code point widths, rather than just the width of the resolved emoji bitmap.
This commit is contained in:
parent
a391ea3da3
commit
b823f3d29f
3 changed files with 13 additions and 8 deletions
|
@ -1518,7 +1518,9 @@ void draw_text_line(FloatRect const& a_rect, Utf8View const& text, Font const& f
|
|||
if (kerning != 0.0f)
|
||||
point.translate_by(direction == TextDirection::LTR ? kerning : -kerning, 0);
|
||||
|
||||
FloatSize glyph_size(font.glyph_or_emoji_width(code_point) + font.glyph_spacing(), font.pixel_size());
|
||||
auto it_copy = it; // The callback function will advance the iterator, so create a copy for this lookup.
|
||||
FloatSize glyph_size(font.glyph_or_emoji_width(it_copy) + font.glyph_spacing(), font.pixel_size());
|
||||
|
||||
if (direction == TextDirection::RTL)
|
||||
point.translate_by(-glyph_size.width(), 0); // If we are drawing right to left, we have to move backwards before drawing the glyph
|
||||
draw_glyph({ point, glyph_size }, it);
|
||||
|
@ -2480,11 +2482,11 @@ void Gfx::Painter::draw_ui_text(Gfx::IntRect const& rect, StringView text, Gfx::
|
|||
if (utf8_view.byte_offset_of(it) >= underline_offset.value()) {
|
||||
int y = text_rect.bottom() + 1;
|
||||
int x1 = text_rect.left() + width;
|
||||
int x2 = x1 + font.glyph_or_emoji_width(*it);
|
||||
int x2 = x1 + font.glyph_or_emoji_width(it);
|
||||
draw_line({ x1, y }, { x2, y }, color);
|
||||
break;
|
||||
}
|
||||
width += font.glyph_or_emoji_width(*it) + font.glyph_spacing();
|
||||
width += font.glyph_or_emoji_width(it) + font.glyph_spacing();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2514,7 +2516,7 @@ void Painter::draw_text_run(FloatPoint baseline_start, Utf8View const& string, F
|
|||
// FIXME: this is probably not the real space taken for complex emojis
|
||||
x += font.glyphs_horizontal_kerning(last_code_point, code_point);
|
||||
draw_glyph_or_emoji(FloatPoint { x, y }, code_point_iterator, font, color);
|
||||
x += font.glyph_or_emoji_width(code_point) + font.glyph_spacing();
|
||||
x += font.glyph_or_emoji_width(code_point_iterator) + font.glyph_spacing();
|
||||
last_code_point = code_point;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue