From a28f035d76f0dbd4684b20b131ed700037b135c2 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 1 Nov 2023 15:23:29 +0000 Subject: [PATCH] LibGfx: Reduce bit casting in OpenType CBLC table after construction The subtables are still read at use-time for now. I'm sure we could build some kind of wrapper structures for them though. --- .../Libraries/LibGfx/Font/OpenType/Tables.cpp | 6 ++++-- Userland/Libraries/LibGfx/Font/OpenType/Tables.h | 16 ++++++++-------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Userland/Libraries/LibGfx/Font/OpenType/Tables.cpp b/Userland/Libraries/LibGfx/Font/OpenType/Tables.cpp index d96db972be..1d1a347e9d 100644 --- a/Userland/Libraries/LibGfx/Font/OpenType/Tables.cpp +++ b/Userland/Libraries/LibGfx/Font/OpenType/Tables.cpp @@ -451,12 +451,14 @@ ErrorOr CBLC::from_slice(ReadonlyBytes slice) if (slice.size() < total_size) return Error::from_string_literal("CBLC table too small"); - return CBLC { slice }; + ReadonlySpan bitmap_sizes { bit_cast(slice.data() + sizeof(CblcHeader)), num_sizes }; + + return CBLC { slice, header, bitmap_sizes }; } Optional CBLC::bitmap_size_for_glyph_id(u32 glyph_id) const { - for (auto const& bitmap_size : this->bitmap_sizes()) { + for (auto const& bitmap_size : m_bitmap_sizes) { if (glyph_id >= bitmap_size.start_glyph_index && glyph_id <= bitmap_size.end_glyph_index) { return bitmap_size; } diff --git a/Userland/Libraries/LibGfx/Font/OpenType/Tables.h b/Userland/Libraries/LibGfx/Font/OpenType/Tables.h index 97b97d0fa1..80520a67d0 100644 --- a/Userland/Libraries/LibGfx/Font/OpenType/Tables.h +++ b/Userland/Libraries/LibGfx/Font/OpenType/Tables.h @@ -549,26 +549,26 @@ public: BigEndian major_version; BigEndian minor_version; BigEndian num_sizes; - BitmapSize bitmap_sizes[]; + // Stored in a separate span: + // BitmapSize bitmap_sizes[]; }; static_assert(AssertSize()); - CblcHeader const& header() const { return *bit_cast(m_slice.data()); } - ReadonlySpan bitmap_sizes() const { return { header().bitmap_sizes, header().num_sizes }; } - Optional bitmap_size_for_glyph_id(u32 glyph_id) const; - static ErrorOr from_slice(ReadonlyBytes); - ReadonlyBytes bytes() const { return m_slice; } - + Optional bitmap_size_for_glyph_id(u32 glyph_id) const; Optional index_subtable_for_glyph_id(u32 glyph_id, u16& first_glyph_index, u16& last_glyph_index) const; private: - explicit CBLC(ReadonlyBytes slice) + explicit CBLC(ReadonlyBytes slice, CblcHeader const& header, ReadonlySpan bitmap_sizes) : m_slice(slice) + , m_header(header) + , m_bitmap_sizes(bitmap_sizes) { } ReadonlyBytes m_slice; + CblcHeader const& m_header; + ReadonlySpan m_bitmap_sizes; }; // https://learn.microsoft.com/en-us/typography/opentype/spec/ebdt