1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:47:36 +00:00

LibGfx: Decouple glyph positions calculation from draw_text_run()

This change separates a part of the `draw_text_run()` function, which
is responsible for calculating the positions for glyphs that need to be
painted, into a separate function called `get_glyph_run()`.

It is a part of the preparation for text run painting using OpenGL,
where we can't immediately blit glyph bitmaps but instead need to
prepare a sequence of quads for them in advance.
This commit is contained in:
Aliaksandr Kalenik 2023-11-05 00:17:04 +01:00 committed by Andreas Kling
parent 3ac77dac99
commit efdbd8238e
4 changed files with 121 additions and 61 deletions

View file

@ -6,6 +6,10 @@
*/
#include "TextLayout.h"
#include "Font/Emoji.h"
#include <AK/Debug.h>
#include <LibUnicode/CharacterTypes.h>
#include <LibUnicode/Emoji.h>
namespace Gfx {
@ -192,4 +196,74 @@ DeprecatedString TextLayout::elide_text_from_right(Utf8View text) const
return text.as_string();
}
DrawGlyphOrEmoji prepare_draw_glyph_or_emoji(FloatPoint point, Utf8CodePointIterator& it, Font const& font)
{
u32 code_point = *it;
auto next_code_point = it.peek(1);
ScopeGuard consume_variation_selector = [&, initial_it = it] {
static auto const variation_selector = Unicode::property_from_string("Variation_Selector"sv);
if (!variation_selector.has_value())
return;
// If we advanced the iterator to consume an emoji sequence, don't look for another variation selector.
if (initial_it != it)
return;
// Otherwise, discard one code point if it's a variation selector.
if (next_code_point.has_value() && Unicode::code_point_has_property(*next_code_point, *variation_selector))
++it;
};
// NOTE: We don't check for emoji
auto font_contains_glyph = font.contains_glyph(code_point);
auto check_for_emoji = !font.has_color_bitmaps() && Unicode::could_be_start_of_emoji_sequence(it, font_contains_glyph ? Unicode::SequenceType::EmojiPresentation : Unicode::SequenceType::Any);
// If the font contains the glyph, and we know it's not the start of an emoji, draw a text glyph.
if (font_contains_glyph && !check_for_emoji) {
return DrawGlyph {
.position = point,
.code_point = code_point,
.font = &font,
};
}
// If we didn't find a text glyph, or have an emoji variation selector or regional indicator, try to draw an emoji glyph.
if (auto const* emoji = Emoji::emoji_for_code_point_iterator(it)) {
return DrawEmoji {
.position = point.to_type<int>(),
.emoji = emoji,
.font = &font,
};
}
// If that failed, but we have a text glyph fallback, draw that.
if (font_contains_glyph) {
return DrawGlyph {
.position = point,
.code_point = code_point,
.font = &font,
};
}
// No suitable glyph found, draw a replacement character.
dbgln_if(EMOJI_DEBUG, "Failed to find a glyph or emoji for code_point {}", code_point);
return DrawGlyph {
.position = point,
.code_point = 0xFFFD,
.font = &font,
};
}
Vector<DrawGlyphOrEmoji> get_glyph_run(FloatPoint baseline_start, Utf8View const& string, Font const& font, IncludeLeftBearing include_left_bearing)
{
Vector<DrawGlyphOrEmoji> glyphs_or_emojis;
for_each_glyph_position(
baseline_start, string, font, [&](auto glyph_or_emoji) {
glyphs_or_emojis.append(glyph_or_emoji);
},
include_left_bearing);
return glyphs_or_emojis;
}
}