1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:37:34 +00:00

LibGfx: Add Gfx::Painter::draw_ui_text()

This function draw text while interpreting "&" as "underline the next
character" and "&&" as "&".
This commit is contained in:
Andreas Kling 2021-04-05 22:40:07 +02:00
parent a1815b0f87
commit 89bc655765
2 changed files with 46 additions and 0 deletions

View file

@ -1806,4 +1806,49 @@ void Painter::blit_tiled(const IntRect& dst_rect, const Gfx::Bitmap& bitmap, con
}
}
void Gfx::Painter::draw_ui_text(const StringView& text, const Gfx::IntRect& rect, const Gfx::Font& font, Gfx::Color color)
{
auto parse_ampersand_string = [](const StringView& raw_text, Optional<size_t>& underline_offset) -> String {
if (raw_text.is_empty())
return String::empty();
StringBuilder builder;
for (size_t i = 0; i < raw_text.length(); ++i) {
if (raw_text[i] == '&') {
if (i != (raw_text.length() - 1) && raw_text[i + 1] == '&')
builder.append(raw_text[i]);
else if (!underline_offset.has_value())
underline_offset = i;
continue;
}
builder.append(raw_text[i]);
}
return builder.to_string();
};
Optional<size_t> underline_offset;
auto name_to_draw = parse_ampersand_string(text, underline_offset);
Gfx::IntRect text_rect { 0, 0, font.width(name_to_draw), font.glyph_height() };
text_rect.center_within(rect);
draw_text(text_rect, name_to_draw, font, Gfx::TextAlignment::CenterLeft, color);
if (underline_offset.has_value()) {
Utf8View utf8_view { name_to_draw };
int width = 0;
for (auto it = utf8_view.begin(); it != utf8_view.end(); ++it) {
if (utf8_view.byte_offset_of(it) >= underline_offset.value()) {
int y = text_rect.bottom() + 2;
int x1 = text_rect.left() + width;
int x2 = x1 + font.glyph_or_emoji_width(*it);
draw_line({ x1, y }, { x2, y }, Color::Black);
break;
}
width += font.glyph_or_emoji_width(*it);
}
}
}
}

View file

@ -85,6 +85,7 @@ public:
void draw_text(Function<void(const IntRect&, u32)>, const IntRect&, const StringView&, const Font&, TextAlignment = TextAlignment::TopLeft, TextElision = TextElision::None);
void draw_text(Function<void(const IntRect&, u32)>, const IntRect&, const Utf8View&, const Font&, TextAlignment = TextAlignment::TopLeft, TextElision = TextElision::None);
void draw_text(Function<void(const IntRect&, u32)>, const IntRect&, const Utf32View&, const Font&, TextAlignment = TextAlignment::TopLeft, TextElision = TextElision::None);
void draw_ui_text(const StringView&, const Gfx::IntRect&, const Gfx::Font&, Gfx::Color);
void draw_glyph(const IntPoint&, u32, Color);
void draw_glyph(const IntPoint&, u32, const Font&, Color);
void draw_emoji(const IntPoint&, const Gfx::Bitmap&, const Font&);