diff --git a/Userland/Libraries/LibGfx/Font/OpenType/Tables.cpp b/Userland/Libraries/LibGfx/Font/OpenType/Tables.cpp index cf97b154fa..4a74d2dd8d 100644 --- a/Userland/Libraries/LibGfx/Font/OpenType/Tables.cpp +++ b/Userland/Libraries/LibGfx/Font/OpenType/Tables.cpp @@ -118,15 +118,29 @@ u16 Hhea::number_of_h_metrics() const ErrorOr Maxp::from_slice(ReadonlyBytes slice) { - if (slice.size() < sizeof(MaximumProfileVersion0_5)) + // All Maximum Profile tables begin with a version. + if (slice.size() < sizeof(Version16Dot16)) return Error::from_string_literal("Could not load Maxp: Not enough data"); + Version16Dot16 const& version = *bit_cast(slice.data()); - return Maxp(slice); + if (version.major == 0 && version.minor == 5) { + if (slice.size() < sizeof(MaximumProfileVersion0_5)) + return Error::from_string_literal("Could not load Maxp: Not enough data"); + return Maxp(bit_cast(slice.data())); + } + + if (version.major == 1 && version.minor == 0) { + if (slice.size() < sizeof(MaximumProfileVersion1_0)) + return Error::from_string_literal("Could not load Maxp: Not enough data"); + return Maxp(bit_cast(slice.data())); + } + + return Error::from_string_literal("Could not load Maxp: Unrecognized version"); } u16 Maxp::num_glyphs() const { - return header().num_glyphs; + return m_data.visit([](auto const* any) { return any->num_glyphs; }); } ErrorOr Hmtx::from_slice(ReadonlyBytes slice, u32 num_glyphs, u32 number_of_h_metrics) diff --git a/Userland/Libraries/LibGfx/Font/OpenType/Tables.h b/Userland/Libraries/LibGfx/Font/OpenType/Tables.h index 69278bdd83..b03ce0d34f 100644 --- a/Userland/Libraries/LibGfx/Font/OpenType/Tables.h +++ b/Userland/Libraries/LibGfx/Font/OpenType/Tables.h @@ -201,14 +201,14 @@ private: }; static_assert(AssertSize()); - MaximumProfileVersion0_5 const& header() const { return *bit_cast(m_slice.data()); } - - Maxp(ReadonlyBytes slice) - : m_slice(slice) + Maxp(Variant data) + : m_data(move(data)) { + VERIFY(m_data.visit([](auto const* any) { return any != nullptr; })); } - ReadonlyBytes m_slice; + // NOTE: Whichever pointer is present is non-null, but Variant can't contain references. + Variant m_data; }; struct GlyphHorizontalMetrics {