mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 09:17:35 +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:
parent
3ac77dac99
commit
efdbd8238e
4 changed files with 121 additions and 61 deletions
|
@ -1416,48 +1416,14 @@ void Painter::draw_glyph_or_emoji(FloatPoint point, u32 code_point, Font const&
|
||||||
|
|
||||||
void Painter::draw_glyph_or_emoji(FloatPoint point, Utf8CodePointIterator& it, Font const& font, Color color)
|
void Painter::draw_glyph_or_emoji(FloatPoint point, Utf8CodePointIterator& it, Font const& font, Color color)
|
||||||
{
|
{
|
||||||
u32 code_point = *it;
|
auto draw_glyph_or_emoji = prepare_draw_glyph_or_emoji(point, it, font);
|
||||||
auto next_code_point = it.peek(1);
|
if (draw_glyph_or_emoji.has<DrawGlyph>()) {
|
||||||
|
auto& glyph = draw_glyph_or_emoji.get<DrawGlyph>();
|
||||||
ScopeGuard consume_variation_selector = [&, initial_it = it] {
|
draw_glyph(glyph.position, glyph.code_point, *glyph.font, color);
|
||||||
static auto const variation_selector = Unicode::property_from_string("Variation_Selector"sv);
|
} else {
|
||||||
if (!variation_selector.has_value())
|
auto& emoji = draw_glyph_or_emoji.get<DrawEmoji>();
|
||||||
return;
|
draw_emoji(emoji.position, *emoji.emoji, *emoji.font);
|
||||||
|
|
||||||
// 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) {
|
|
||||||
draw_glyph(point, code_point, font, color);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)) {
|
|
||||||
draw_emoji(point.to_type<int>(), *emoji, font);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If that failed, but we have a text glyph fallback, draw that.
|
|
||||||
if (font_contains_glyph) {
|
|
||||||
draw_glyph(point, code_point, font, color);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// No suitable glyph found, draw a replacement character.
|
|
||||||
dbgln_if(EMOJI_DEBUG, "Failed to find a glyph or emoji for code_point {}", code_point);
|
|
||||||
draw_glyph(point, 0xFFFD, font, color);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Painter::draw_glyph(IntPoint point, u32 code_point, Color color)
|
void Painter::draw_glyph(IntPoint point, u32 code_point, Color color)
|
||||||
|
@ -2473,8 +2439,14 @@ void Painter::draw_text_run(IntPoint baseline_start, Utf8View const& string, Fon
|
||||||
|
|
||||||
void Painter::draw_text_run(FloatPoint baseline_start, Utf8View const& string, Font const& font, Color color)
|
void Painter::draw_text_run(FloatPoint baseline_start, Utf8View const& string, Font const& font, Color color)
|
||||||
{
|
{
|
||||||
for_each_glyph_position(baseline_start, string, font, [&](GlyphPosition glyph_position) {
|
for_each_glyph_position(baseline_start, string, font, [&](DrawGlyphOrEmoji glyph_or_emoji) {
|
||||||
draw_glyph_or_emoji(glyph_position.position, glyph_position.it, font, color);
|
if (glyph_or_emoji.has<DrawGlyph>()) {
|
||||||
|
auto& glyph = glyph_or_emoji.get<DrawGlyph>();
|
||||||
|
draw_glyph(glyph.position, glyph.code_point, *glyph.font, color);
|
||||||
|
} else {
|
||||||
|
auto& emoji = glyph_or_emoji.get<DrawEmoji>();
|
||||||
|
draw_emoji(emoji.position, *emoji.emoji, *emoji.font);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -167,13 +167,16 @@ void Path::text(Utf8View text, Font const& font)
|
||||||
dbgln("Cannot path-ify bitmap fonts!");
|
dbgln("Cannot path-ify bitmap fonts!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto& scaled_font = static_cast<ScaledFont const&>(font);
|
auto& scaled_font = static_cast<ScaledFont const&>(font);
|
||||||
for_each_glyph_position(
|
for_each_glyph_position(
|
||||||
last_point(), text, font, [&](GlyphPosition glyph_position) {
|
last_point(), text, font, [&](DrawGlyphOrEmoji glyph_or_emoji) {
|
||||||
move_to(glyph_position.position);
|
if (glyph_or_emoji.has<DrawGlyph>()) {
|
||||||
// FIXME: This does not correctly handle multi-codepoint glyphs (i.e. emojis).
|
auto& glyph = glyph_or_emoji.get<DrawGlyph>();
|
||||||
auto glyph_id = scaled_font.glyph_id_for_code_point(*glyph_position.it);
|
move_to(glyph.position);
|
||||||
scaled_font.append_glyph_path_to(*this, glyph_id);
|
auto glyph_id = scaled_font.glyph_id_for_code_point(glyph.code_point);
|
||||||
|
scaled_font.append_glyph_path_to(*this, glyph_id);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
IncludeLeftBearing::Yes);
|
IncludeLeftBearing::Yes);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,10 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "TextLayout.h"
|
#include "TextLayout.h"
|
||||||
|
#include "Font/Emoji.h"
|
||||||
|
#include <AK/Debug.h>
|
||||||
|
#include <LibUnicode/CharacterTypes.h>
|
||||||
|
#include <LibUnicode/Emoji.h>
|
||||||
|
|
||||||
namespace Gfx {
|
namespace Gfx {
|
||||||
|
|
||||||
|
@ -192,4 +196,74 @@ DeprecatedString TextLayout::elide_text_from_right(Utf8View text) const
|
||||||
return text.as_string();
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,23 +75,33 @@ enum class IncludeLeftBearing {
|
||||||
No
|
No
|
||||||
};
|
};
|
||||||
|
|
||||||
struct GlyphPosition {
|
struct DrawGlyph {
|
||||||
FloatPoint position;
|
FloatPoint position;
|
||||||
float glyph_width;
|
u32 code_point;
|
||||||
AK::Utf8CodePointIterator& it;
|
Font const* font;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct DrawEmoji {
|
||||||
|
IntPoint position;
|
||||||
|
Gfx::Bitmap const* emoji;
|
||||||
|
Font const* font;
|
||||||
|
};
|
||||||
|
|
||||||
|
using DrawGlyphOrEmoji = Variant<DrawGlyph, DrawEmoji>;
|
||||||
|
|
||||||
|
Variant<DrawGlyph, DrawEmoji> prepare_draw_glyph_or_emoji(FloatPoint point, Utf8CodePointIterator& it, Font const& font);
|
||||||
|
|
||||||
template<typename Callback>
|
template<typename Callback>
|
||||||
void for_each_glyph_position(FloatPoint start, Utf8View text, Font const& font, Callback callback, IncludeLeftBearing include_left_bearing = IncludeLeftBearing::No)
|
void for_each_glyph_position(FloatPoint baseline_start, Utf8View string, Font const& font, Callback callback, IncludeLeftBearing include_left_bearing = IncludeLeftBearing::No)
|
||||||
{
|
{
|
||||||
float space_width = font.glyph_width(' ') + font.glyph_spacing();
|
float space_width = font.glyph_width(' ') + font.glyph_spacing();
|
||||||
|
|
||||||
u32 last_code_point = 0;
|
u32 last_code_point = 0;
|
||||||
|
|
||||||
auto point = start;
|
auto point = baseline_start;
|
||||||
point.translate_by(0, -font.pixel_metrics().ascent);
|
point.translate_by(0, -font.pixel_metrics().ascent);
|
||||||
|
|
||||||
for (auto code_point_iterator = text.begin(); code_point_iterator != text.end(); ++code_point_iterator) {
|
for (auto code_point_iterator = string.begin(); code_point_iterator != string.end(); ++code_point_iterator) {
|
||||||
auto code_point = *code_point_iterator;
|
auto code_point = *code_point_iterator;
|
||||||
if (should_paint_as_space(code_point)) {
|
if (should_paint_as_space(code_point)) {
|
||||||
point.translate_by(space_width, 0);
|
point.translate_by(space_width, 0);
|
||||||
|
@ -106,18 +116,19 @@ void for_each_glyph_position(FloatPoint start, Utf8View text, Font const& font,
|
||||||
auto it = code_point_iterator; // The callback function will advance the iterator, so create a copy for this lookup.
|
auto it = code_point_iterator; // The callback function will advance the iterator, so create a copy for this lookup.
|
||||||
auto glyph_width = font.glyph_or_emoji_width(it) + font.glyph_spacing();
|
auto glyph_width = font.glyph_or_emoji_width(it) + font.glyph_spacing();
|
||||||
|
|
||||||
auto glyph_point = point;
|
auto glyph_or_emoji = prepare_draw_glyph_or_emoji(point, code_point_iterator, font);
|
||||||
if (include_left_bearing == IncludeLeftBearing::Yes)
|
if (include_left_bearing == IncludeLeftBearing::Yes) {
|
||||||
glyph_point += FloatPoint(font.glyph_left_bearing(code_point), 0);
|
if (glyph_or_emoji.has<DrawGlyph>())
|
||||||
|
glyph_or_emoji.get<DrawGlyph>().position += FloatPoint(font.glyph_left_bearing(code_point), 0);
|
||||||
|
}
|
||||||
|
|
||||||
callback(GlyphPosition {
|
callback(glyph_or_emoji);
|
||||||
.position = glyph_point,
|
|
||||||
.glyph_width = glyph_width,
|
|
||||||
.it = code_point_iterator });
|
|
||||||
|
|
||||||
point.translate_by(glyph_width, 0);
|
point.translate_by(glyph_width, 0);
|
||||||
last_code_point = code_point;
|
last_code_point = code_point;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vector<DrawGlyphOrEmoji> get_glyph_run(FloatPoint baseline_start, Utf8View const& string, Font const& font, IncludeLeftBearing include_left_bearing = IncludeLeftBearing::No);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue