Skip to content

Commit

Permalink
LibGfx: Reduce bit casting in OpenType CBLC table after construction
Browse files Browse the repository at this point in the history
The subtables are still read at use-time for now. I'm sure we could
build some kind of wrapper structures for them though.
  • Loading branch information
AtkinsSJ authored and awesomekling committed Nov 8, 2023
1 parent 3c7d654 commit a28f035
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
6 changes: 4 additions & 2 deletions Userland/Libraries/LibGfx/Font/OpenType/Tables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,14 @@ ErrorOr<CBLC> CBLC::from_slice(ReadonlyBytes slice)
if (slice.size() < total_size)
return Error::from_string_literal("CBLC table too small");

return CBLC { slice };
ReadonlySpan<BitmapSize> bitmap_sizes { bit_cast<BitmapSize const*>(slice.data() + sizeof(CblcHeader)), num_sizes };

return CBLC { slice, header, bitmap_sizes };
}

Optional<CBLC::BitmapSize const&> 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;
}
Expand Down
16 changes: 8 additions & 8 deletions Userland/Libraries/LibGfx/Font/OpenType/Tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -549,26 +549,26 @@ class CBLC {
BigEndian<u16> major_version;
BigEndian<u16> minor_version;
BigEndian<u32> num_sizes;
BitmapSize bitmap_sizes[];
// Stored in a separate span:
// BitmapSize bitmap_sizes[];
};
static_assert(AssertSize<CblcHeader, 8>());

CblcHeader const& header() const { return *bit_cast<CblcHeader const*>(m_slice.data()); }
ReadonlySpan<BitmapSize> bitmap_sizes() const { return { header().bitmap_sizes, header().num_sizes }; }
Optional<BitmapSize const&> bitmap_size_for_glyph_id(u32 glyph_id) const;

static ErrorOr<CBLC> from_slice(ReadonlyBytes);
ReadonlyBytes bytes() const { return m_slice; }

Optional<BitmapSize const&> bitmap_size_for_glyph_id(u32 glyph_id) const;
Optional<EBLC::IndexSubHeader const&> 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<BitmapSize> bitmap_sizes)
: m_slice(slice)
, m_header(header)
, m_bitmap_sizes(bitmap_sizes)
{
}

ReadonlyBytes m_slice;
CblcHeader const& m_header;
ReadonlySpan<BitmapSize> m_bitmap_sizes;
};

// https://learn.microsoft.com/en-us/typography/opentype/spec/ebdt
Expand Down

0 comments on commit a28f035

Please sign in to comment.