Truly independent web browser
LibGfx: Remove GlyphRun
Aliaksandr Kalenik31a91bc2f961
diff --git a/Libraries/LibGfx/Forward.h b/Libraries/LibGfx/Forward.hindex 03abfe9872b..73d9ac43cf7 100644--- a/Libraries/LibGfx/Forward.h+++ b/Libraries/LibGfx/Forward.h@@ -16,7 +16,6 @@ class Color;class Emoji;class Font;-class GlyphRun;class ImageDecoder;struct FontPixelMetrics;diff --git a/Libraries/LibGfx/Rust/src/text_layout.rs b/Libraries/LibGfx/Rust/src/text_layout.rsindex c1c08fead5e..cb38a849058 100644--- a/Libraries/LibGfx/Rust/src/text_layout.rs+++ b/Libraries/LibGfx/Rust/src/text_layout.rs@@ -590,43 +590,6 @@ pub fn shape_text(})}-/// # Safety-/// `font` must point to a live Gfx::Font, and `text` must contain `length` code-/// units (or be null when empty). The callback must consume the borrowed glyphs-/// synchronously and must not reenter the shaping cache.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn ladybird_gfx_shape_text_cached(- font: *const c_void,- text: *const u16,- length: usize,- text_type: TextType,- letter_spacing: f32,- word_spacing: f32,- sink: *mut c_void,- emit: unsafe extern "C" fn(*mut c_void, *const DrawGlyph, usize, f32, usize, f32),-) {- // SAFETY: The caller supplies a live font and a valid text slice.- let font = unsafe { FontHandle::intern(font) };- let text = if length == 0 {- &[]- } else {- unsafe { ak::slice_from_raw_parts(text, length) }- };- with_cached_shape(&font, text, text_type, letter_spacing, word_spacing, |shape| {- // SAFETY: The glyphs remain live for the synchronous callback.- unsafe {- emit(- sink,- shape.glyphs.as_ptr(),- shape.glyphs.len(),- shape.width,- shape.trailing_whitespace_length_in_code_units,- shape.trailing_whitespace_advance,- );- }- });-}-#[cfg(test)]mod shaping_cache_tests {use super::*;diff --git a/Libraries/LibGfx/TextLayout.cpp b/Libraries/LibGfx/TextLayout.cppindex 94bea57b418..2882c23dfd7 100644--- a/Libraries/LibGfx/TextLayout.cpp+++ b/Libraries/LibGfx/TextLayout.cpp@@ -24,16 +24,6 @@namespace Gfx {-GlyphRun::GlyphRun(Vector<DrawGlyph>&& glyphs, NonnullRefPtr<Font const> font, TextType text_type, float width)- : m_glyphs(move(glyphs))- , m_font(move(font))- , m_text_type(text_type)- , m_width(width)-{-}--GlyphRun::~GlyphRun() = default;-// Tight bounds of the ink of the painted glyphs, from their own extents, in the coordinates of glyph_run_bounding_box().// Empty if no painted glyph has ink.static FloatRect glyph_run_ink_bounding_box(Font const& font, ReadonlySpan<DrawGlyph> glyphs, float scale)@@ -391,7 +381,7 @@ Vector<float> glyph_run_glyph_intercepts(Font const& font, ReadonlySpan<DrawGlypreturn intervals;}-static hb_buffer_t* setup_text_shaping(Utf16View const& string, Font const& font, GlyphRun::TextType text_type)+static hb_buffer_t* setup_text_shaping(Utf16View const& string, Font const& font, FFI::TextType text_type){hb_buffer_t* buffer = hb_buffer_create();@@ -406,10 +396,10 @@ static hb_buffer_t* setup_text_shaping(Utf16View const& string, Font const& fontelsehb_buffer_add_utf16(buffer, reinterpret_cast<u16 const*>(string.utf16_span().data()), string.length_in_code_units(), 0, -1);// For non-ASCII, set direction from text_type if known, otherwise guess.- if (text_type == GlyphRun::TextType::Ltr) {+ if (text_type == FFI::TextType::Ltr) {hb_buffer_set_direction(buffer, HB_DIRECTION_LTR);hb_buffer_guess_segment_properties(buffer);- } else if (text_type == GlyphRun::TextType::Rtl) {+ } else if (text_type == FFI::TextType::Rtl) {hb_buffer_set_direction(buffer, HB_DIRECTION_RTL);hb_buffer_guess_segment_properties(buffer);} else {@@ -458,13 +448,18 @@ static size_t length_of_trailing_whitespace_run(Utf16View const& string)return length;}+struct TrailingWhitespace {+ size_t length_in_code_units { 0 };+ float advance { 0 };+};+struct ShapedGlyphs {Vector<DrawGlyph> glyphs;float width { 0 };TrailingWhitespace trailing_whitespace;};-static ShapedGlyphs build_origin_relative_shape(Utf16View const& string, Font const& font, GlyphRun::TextType text_type, float letter_spacing, float word_spacing)+static ShapedGlyphs build_origin_relative_shape(Utf16View const& string, Font const& font, FFI::TextType text_type, float letter_spacing, float word_spacing){auto const& metrics = font.pixel_metrics();auto* buffer = setup_text_shaping(string, font, text_type);@@ -548,33 +543,9 @@ static ShapedGlyphs build_origin_relative_shape(Utf16View const& string, Font coreturn ShapedGlyphs { move(glyphs), point.x(), trailing_whitespace };}-NonnullRefPtr<GlyphRun> shape_text(FloatPoint baseline_start, float letter_spacing, float word_spacing, Utf16View const& string, Font const& font, GlyphRun::TextType text_type, TrailingWhitespace* out_trailing_whitespace)-{- Vector<u16, 64> code_units;- code_units.ensure_capacity(string.length_in_code_units());- for (size_t i = 0; i < string.length_in_code_units(); ++i)- code_units.unchecked_append(string.code_unit_at(i));-- ShapedGlyphs shape;- FFI::ladybird_gfx_shape_text_cached(&font, code_units.data(), code_units.size(), static_cast<FFI::TextType>(text_type), letter_spacing, word_spacing, &shape,- [](void* sink, FFI::DrawGlyph const* glyphs, size_t count, float width, size_t trailing_length, float trailing_advance) {- auto& shape = *static_cast<ShapedGlyphs*>(sink);- shape.glyphs.append(reinterpret_cast<DrawGlyph const*>(glyphs), count);- shape.width = width;- shape.trailing_whitespace = { trailing_length, trailing_advance };- });- if (out_trailing_whitespace)- *out_trailing_whitespace = shape.trailing_whitespace;- if (!baseline_start.is_zero()) {- for (auto& glyph : shape.glyphs)- glyph.position.translate_by(baseline_start);- }- return adopt_ref(*new GlyphRun(move(shape.glyphs), font, text_type, shape.width));-}-float measure_text_width(Utf16View const& string, Font const& font, float letter_spacing){- auto* buffer = setup_text_shaping(string, font, GlyphRun::TextType::Common);+ auto* buffer = setup_text_shaping(string, font, FFI::TextType::Common);u32 glyph_count;auto const* positions = hb_buffer_get_glyph_positions(buffer, &glyph_count);@@ -589,11 +560,6 @@ float measure_text_width(Utf16View const& string, Font const& font, float letter}-static_assert(to_underlying(Gfx::FFI::TextType::Common) == to_underlying(Gfx::GlyphRun::TextType::Common));-static_assert(to_underlying(Gfx::FFI::TextType::ContextDependent) == to_underlying(Gfx::GlyphRun::TextType::ContextDependent));-static_assert(to_underlying(Gfx::FFI::TextType::EndPadding) == to_underlying(Gfx::GlyphRun::TextType::EndPadding));-static_assert(to_underlying(Gfx::FFI::TextType::Ltr) == to_underlying(Gfx::GlyphRun::TextType::Ltr));-static_assert(to_underlying(Gfx::FFI::TextType::Rtl) == to_underlying(Gfx::GlyphRun::TextType::Rtl));static_assert(sizeof(Gfx::FFI::DrawGlyph) == sizeof(Gfx::DrawGlyph));static_assert(alignof(Gfx::FFI::DrawGlyph) == alignof(Gfx::DrawGlyph));static_assert(IsTriviallyCopyable<Gfx::FFI::DrawGlyph>);@@ -633,7 +599,7 @@ extern "C" void ladybird_gfx_shape_text_uncached(auto shape = Gfx::build_origin_relative_shape(text,*static_cast<Gfx::Font const*>(font),- static_cast<Gfx::GlyphRun::TextType>(text_type),+ text_type,letter_spacing,word_spacing);emit(diff --git a/Libraries/LibGfx/TextLayout.h b/Libraries/LibGfx/TextLayout.hindex 8f1c9289ae0..db696f62395 100644--- a/Libraries/LibGfx/TextLayout.h+++ b/Libraries/LibGfx/TextLayout.h@@ -8,11 +8,9 @@#pragma once-#include <AK/AtomicRefCounted.h>#include <AK/Forward.h>#include <AK/Vector.h>#include <LibGfx/Font/Font.h>-#include <LibGfx/FontCascadeList.h>#include <LibGfx/Forward.h>#include <LibGfx/Point.h>#include <LibGfx/Rect.h>@@ -30,37 +28,6 @@ struct DrawGlyph {bool is_rtl { false };};-struct TrailingWhitespace {- size_t length_in_code_units { 0 };- float advance { 0 };-};--class GlyphRun : public AtomicRefCounted<GlyphRun> {-public:- enum class TextType {- Common,- ContextDependent,- EndPadding,- Ltr,- Rtl,- };-- GlyphRun(Vector<DrawGlyph>&& glyphs, NonnullRefPtr<Font const> font, TextType text_type, float width);- ~GlyphRun();-- [[nodiscard]] Font const& font() const { return m_font; }- [[nodiscard]] TextType text_type() const { return m_text_type; }- [[nodiscard]] Vector<DrawGlyph> const& glyphs() const { return m_glyphs; }- [[nodiscard]] Vector<DrawGlyph>& glyphs() { return m_glyphs; }- [[nodiscard]] float width() const { return m_width; }--private:- Vector<DrawGlyph> m_glyphs;- NonnullRefPtr<Font const> m_font;- TextType m_text_type;- float m_width { 0 };-};-// Conservative bounds of the painted glyphs in device pixels, relative to the run's baseline origin (glyph// positions scaled by `scale`, with the font ascent added to y). The bounding box of the glyph origins is// expanded by the font's overall glyph bounding box, falling back to tight per-glyph extents when the font@@ -71,7 +38,6 @@ private:// the horizontal band [y_top, y_bottom]. One pair per painted glyph that has ink inside the band, in run order.[[nodiscard]] Vector<float> glyph_run_glyph_intercepts(Font const&, ReadonlySpan<DrawGlyph>, float scale, float y_top, float y_bottom);-NonnullRefPtr<GlyphRun> shape_text(FloatPoint baseline_start, float letter_spacing, float word_spacing, Utf16View const&, Gfx::Font const& font, GlyphRun::TextType, TrailingWhitespace* = nullptr);float measure_text_width(Utf16View const&, Font const& font, float letter_spacing = 0.f);}diff --git a/Tests/LibGfx/TestFont.cpp b/Tests/LibGfx/TestFont.cppindex c15136b3509..fd759e5a4be 100644--- a/Tests/LibGfx/TestFont.cpp+++ b/Tests/LibGfx/TestFont.cpp@@ -17,12 +17,10 @@# include <LibGfx/Font/TypefaceCoreText.h>#endif#include <LibGfx/FontCascadeList.h>-#include <LibGfx/TextLayout.h>#include <LibIPC/Decoder.h>#include <LibIPC/Encoder.h>#include <LibTest/TestCase.h>#include <LibThreading/Thread.h>-#include <harfbuzz/hb.h>#define TEST_INPUT(x) ("test-inputs/" x)@@ -92,107 +90,6 @@ static NonnullRefPtr<Gfx::Font> load_text_font(float point_size)return adopt_ref(*new Gfx::Font(typeface, point_size, point_size, {}, {}));}-static NonnullRefPtr<Gfx::GlyphRun> shape(Gfx::Font const& font, StringView text)-{- auto utf16_text = Utf16String::from_utf8(text);- return Gfx::shape_text({}, 0, 0, utf16_text.utf16_view(), font, Gfx::GlyphRun::TextType::Common);-}--// The run's bounding box is conservative: it contains every glyph's own extents at the glyph's origin, and it-// scales with the device scale.-TEST_CASE(glyph_run_bounding_box_contains_glyph_extents)-{- auto font = load_text_font(16);- auto run = shape(font, "abc"sv);- EXPECT_EQ(run->glyphs().size(), 3u);-- auto bounds = Gfx::glyph_run_bounding_box(font, run->glyphs(), 1);- EXPECT(!bounds.is_empty());-- auto* hb_font = font->harfbuzz_font();- int x_scale = 0;- int y_scale = 0;- hb_font_get_scale(hb_font, &x_scale, &y_scale);- auto font_ascent = font->pixel_metrics().ascent;- for (auto const& glyph : run->glyphs()) {- hb_glyph_extents_t extents {};- EXPECT(hb_font_get_glyph_extents(hb_font, glyph.glyph_id, &extents));- Gfx::FloatRect glyph_bounds {- glyph.position.x() + extents.x_bearing * font->pixel_size() / x_scale,- glyph.position.y() + font_ascent - extents.y_bearing * font->pixel_size() / y_scale,- extents.width * font->pixel_size() / x_scale,- -extents.height * font->pixel_size() / y_scale,- };- EXPECT(!glyph_bounds.is_empty());- EXPECT(bounds.contains(glyph_bounds));- }-- auto scaled_bounds = Gfx::glyph_run_bounding_box(font, run->glyphs(), 2);- EXPECT_APPROXIMATE(scaled_bounds.x(), bounds.x() * 2);- EXPECT_APPROXIMATE(scaled_bounds.y(), bounds.y() * 2);- EXPECT_APPROXIMATE(scaled_bounds.width(), bounds.width() * 2);- EXPECT_APPROXIMATE(scaled_bounds.height(), bounds.height() * 2);-- auto empty_run = shape(font, ""sv);- EXPECT(Gfx::glyph_run_bounding_box(font, empty_run->glyphs(), 1).is_empty());-}--// Glyph intercepts report one [start, end] interval per glyph whose ink crosses the band, in run order and in-// run-local device pixels.-TEST_CASE(glyph_run_intercepts)-{- auto font = load_text_font(16);- auto run = shape(font, "abc"sv);-- // Bands are relative to the run's baseline origin; ink above the baseline has negative y.- auto mid_x_height = -font->pixel_metrics().x_height / 2;- auto intercepts = Gfx::glyph_run_glyph_intercepts(font, run->glyphs(), 1, mid_x_height - 1, mid_x_height + 1);- EXPECT_EQ(intercepts.size(), 6u);- float previous_end = 0;- for (size_t i = 0; i + 1 < intercepts.size(); i += 2) {- EXPECT(intercepts[i] < intercepts[i + 1]);- EXPECT(intercepts[i] >= previous_end);- EXPECT(intercepts[i + 1] <= run->width() + 1);- previous_end = intercepts[i + 1];- }-- // At twice the scale the intervals scale with it.- auto scaled_intercepts = Gfx::glyph_run_glyph_intercepts(font, run->glyphs(), 2, (mid_x_height - 1) * 2, (mid_x_height + 1) * 2);- EXPECT_EQ(scaled_intercepts.size(), 6u);- for (size_t i = 0; i < intercepts.size(); ++i)- EXPECT(AK::fabs(scaled_intercepts[i] - intercepts[i] * 2) < 0.01f);-- // A band far above the ascender touches no ink.- EXPECT(Gfx::glyph_run_glyph_intercepts(font, run->glyphs(), 1, -200, -190).is_empty());-- // A band just below the top of the 'b' ascender is only reached by that glyph.- auto* hb_font = font->harfbuzz_font();- int x_scale = 0;- int y_scale = 0;- hb_font_get_scale(hb_font, &x_scale, &y_scale);- hb_glyph_extents_t b_extents {};- EXPECT(hb_font_get_glyph_extents(hb_font, run->glyphs()[1].glyph_id, &b_extents));- auto b_top = -b_extents.y_bearing * font->pixel_size() / y_scale;- auto ascender_intercepts = Gfx::glyph_run_glyph_intercepts(font, run->glyphs(), 1, b_top + 0.5f, b_top + 1.5f);- EXPECT_EQ(ascender_intercepts.size(), 2u);- EXPECT(ascender_intercepts[0] >= run->glyphs()[1].position.x());- EXPECT(ascender_intercepts[1] <= run->glyphs()[2].position.x());-- // A degenerate scale produces nothing.- EXPECT(Gfx::glyph_run_glyph_intercepts(font, run->glyphs(), 0, mid_x_height - 1, mid_x_height + 1).is_empty());-}--TEST_CASE(invisible_fallback_preserves_metrics_and_shaping)-{- auto font = load_text_font(16);- auto invisible = font->invisible_variant();- EXPECT(invisible->is_invisible());- EXPECT(!font->is_invisible());- EXPECT_EQ(invisible->pixel_metrics().ascent, font->pixel_metrics().ascent);- EXPECT_EQ(invisible->pixel_metrics().descent, font->pixel_metrics().descent);- EXPECT_EQ(shape(*invisible, "abc"sv)->width(), shape(*font, "abc"sv)->width());-}-TEST_CASE(pending_font_only_hides_its_unicode_range){auto font = load_text_font(16);@@ -354,32 +251,6 @@ TEST_CASE(system_ui_italic_keeps_its_slope_with_variations)}#endif-TEST_CASE(shaping_cache_preserves_positions_spacing_and_trailing_whitespace)-{- auto font = load_text_font(16);- Gfx::TrailingWhitespace trailing_whitespace;- auto origin = Gfx::shape_text({}, 2, 3, u"abc "sv, font, Gfx::GlyphRun::TextType::Common, &trailing_whitespace);- auto translated = Gfx::shape_text({ 7, 11 }, 2, 3, u"abc "sv, font, Gfx::GlyphRun::TextType::Common);- Gfx::TrailingWhitespace cached_trailing_whitespace;- auto repeated = Gfx::shape_text({}, 2, 3, u"abc "sv, font, Gfx::GlyphRun::TextType::Common, &cached_trailing_whitespace);-- EXPECT_EQ(origin->width(), translated->width());- EXPECT_EQ(origin->width(), repeated->width());- EXPECT_EQ(origin->glyphs().size(), translated->glyphs().size());- EXPECT_EQ(origin->glyphs().size(), repeated->glyphs().size());- for (size_t i = 0; i < origin->glyphs().size(); ++i) {- EXPECT_EQ(translated->glyphs()[i].position, origin->glyphs()[i].position.translated(7, 11));- EXPECT_EQ(repeated->glyphs()[i].position, origin->glyphs()[i].position);- EXPECT_EQ(repeated->glyphs()[i].glyph_id, origin->glyphs()[i].glyph_id);- }- EXPECT_EQ(trailing_whitespace.length_in_code_units, 1u);- EXPECT_EQ(cached_trailing_whitespace.length_in_code_units, trailing_whitespace.length_in_code_units);- EXPECT_EQ(cached_trailing_whitespace.advance, trailing_whitespace.advance);-- auto without_spacing = Gfx::shape_text({}, 0, 0, u"abc "sv, font, Gfx::GlyphRun::TextType::Common);- EXPECT_APPROXIMATE(origin->width() - without_spacing->width(), 11.f);-}-// Only ThreadSanitizer can catch a memo race here, since every thread reaches the same verdict.TEST_CASE(emoji_classification_can_run_on_several_threads){