LibGfx/OpenType: Read "hhea" table using a C++ struct

This commit is contained in:
Andreas Kling 2022-12-19 13:36:09 +01:00
parent 61be11960b
commit d201acf102
Notes: sideshowbarker 2024-07-17 09:48:50 +09:00
2 changed files with 27 additions and 15 deletions

View file

@ -107,7 +107,7 @@ IndexToLocFormat Head::index_to_loc_format() const
Optional<Hhea> Hhea::from_slice(ReadonlyBytes slice)
{
if (slice.size() < (size_t)Sizes::Table) {
if (slice.size() < sizeof(HorizontalHeaderTable)) {
return {};
}
return Hhea(slice);
@ -115,27 +115,27 @@ Optional<Hhea> Hhea::from_slice(ReadonlyBytes slice)
i16 Hhea::ascender() const
{
return be_i16(m_slice.offset_pointer((u32)Offsets::Ascender));
return header().ascender;
}
i16 Hhea::descender() const
{
return be_i16(m_slice.offset_pointer((u32)Offsets::Descender));
return header().descender;
}
i16 Hhea::line_gap() const
{
return be_i16(m_slice.offset_pointer((u32)Offsets::LineGap));
return header().line_gap;
}
u16 Hhea::advance_width_max() const
{
return be_u16(m_slice.offset_pointer((u32)Offsets::AdvanceWidthMax));
return header().advance_width_max;
}
u16 Hhea::number_of_h_metrics() const
{
return be_u16(m_slice.offset_pointer((u32)Offsets::NumberOfHMetrics));
return header().number_of_h_metrics;
}
Optional<Maxp> Maxp::from_slice(ReadonlyBytes slice)

View file

@ -28,6 +28,9 @@ struct LongDateTime {
BigEndian<u64> value;
};
using FWord = BigEndian<i16>;
using UFWord = BigEndian<u16>;
// https://learn.microsoft.com/en-us/typography/opentype/spec/head
// head: Font Header Table
class Head {
@ -86,17 +89,26 @@ public:
u16 number_of_h_metrics() const;
private:
enum class Offsets {
Ascender = 4,
Descender = 6,
LineGap = 8,
AdvanceWidthMax = 10,
NumberOfHMetrics = 34,
};
enum class Sizes {
Table = 36,
struct HorizontalHeaderTable {
BigEndian<u16> major_version;
BigEndian<u16> minor_version;
FWord ascender;
FWord descender;
FWord line_gap;
UFWord advance_width_max;
FWord min_left_side_bearing;
FWord min_right_side_bearing;
FWord x_max_extent;
BigEndian<i16> caret_slope_rise;
BigEndian<i16> caret_slope_run;
BigEndian<i16> caret_offset;
BigEndian<i16> reserved[4];
BigEndian<i16> metric_data_format;
BigEndian<u16> number_of_h_metrics;
};
HorizontalHeaderTable const& header() const { return *bit_cast<HorizontalHeaderTable const*>(m_slice.data()); }
Hhea(ReadonlyBytes slice)
: m_slice(slice)
{