From e6551e685d7db43786398d24b715ce3cc3c573a8 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 19 Dec 2022 15:03:03 +0100 Subject: [PATCH] LibGfx/OpenType: Clean up "htmx" table reading Use a C++ struct to read out the LongHorMetrics, and make the rest of the code more idiomatic. --- .../Libraries/LibGfx/Font/OpenType/Font.cpp | 19 ++++++++----------- .../Libraries/LibGfx/Font/OpenType/Tables.h | 6 +++--- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp b/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp index 807aae833f..d2ab082778 100644 --- a/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp +++ b/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp @@ -153,7 +153,7 @@ u16 Maxp::num_glyphs() const Optional Hmtx::from_slice(ReadonlyBytes slice, u32 num_glyphs, u32 number_of_h_metrics) { - if (slice.size() < number_of_h_metrics * (u32)Sizes::LongHorMetric + (num_glyphs - number_of_h_metrics) * (u32)Sizes::LeftSideBearing) { + if (slice.size() < number_of_h_metrics * sizeof(LongHorMetric) + (num_glyphs - number_of_h_metrics) * sizeof(u16)) { return {}; } return Hmtx(slice, num_glyphs, number_of_h_metrics); @@ -335,21 +335,18 @@ DeprecatedString Name::string_for_id(NameId id) const GlyphHorizontalMetrics Hmtx::get_glyph_horizontal_metrics(u32 glyph_id) const { VERIFY(glyph_id < m_num_glyphs); + auto const* long_hor_metrics = bit_cast(m_slice.data()); if (glyph_id < m_number_of_h_metrics) { - auto offset = glyph_id * (u32)Sizes::LongHorMetric; - u16 advance_width = be_u16(m_slice.offset_pointer(offset)); - i16 left_side_bearing = be_i16(m_slice.offset_pointer(offset + 2)); return GlyphHorizontalMetrics { - .advance_width = advance_width, - .left_side_bearing = left_side_bearing, + .advance_width = static_cast(long_hor_metrics[glyph_id].advance_width), + .left_side_bearing = static_cast(long_hor_metrics[glyph_id].lsb), }; } - auto offset = m_number_of_h_metrics * (u32)Sizes::LongHorMetric + (glyph_id - m_number_of_h_metrics) * (u32)Sizes::LeftSideBearing; - u16 advance_width = be_u16(m_slice.offset_pointer((m_number_of_h_metrics - 1) * (u32)Sizes::LongHorMetric)); - i16 left_side_bearing = be_i16(m_slice.offset_pointer(offset)); + + auto const* left_side_bearings = bit_cast const*>(m_slice.offset_pointer(m_number_of_h_metrics * sizeof(LongHorMetric))); return GlyphHorizontalMetrics { - .advance_width = advance_width, - .left_side_bearing = left_side_bearing, + .advance_width = static_cast(long_hor_metrics[m_number_of_h_metrics - 1].advance_width), + .left_side_bearing = static_cast(left_side_bearings[glyph_id - m_number_of_h_metrics]), }; } diff --git a/Userland/Libraries/LibGfx/Font/OpenType/Tables.h b/Userland/Libraries/LibGfx/Font/OpenType/Tables.h index 728d1856f5..1668f0e191 100644 --- a/Userland/Libraries/LibGfx/Font/OpenType/Tables.h +++ b/Userland/Libraries/LibGfx/Font/OpenType/Tables.h @@ -177,9 +177,9 @@ public: GlyphHorizontalMetrics get_glyph_horizontal_metrics(u32 glyph_id) const; private: - enum class Sizes { - LongHorMetric = 4, - LeftSideBearing = 2 + struct LongHorMetric { + BigEndian advance_width; + BigEndian lsb; }; Hmtx(ReadonlyBytes slice, u32 num_glyphs, u32 number_of_h_metrics)