1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:27:45 +00:00

LibGfx: Cut down on magic constants in the TTF parser.

This commit is contained in:
Srimanta Barua 2020-06-06 15:37:01 +05:30 committed by Andreas Kling
parent 186499cc25
commit 1931535218
2 changed files with 206 additions and 90 deletions

View file

@ -59,37 +59,37 @@ static u32 tag_from_str(const char *str)
u16 Font::Head::units_per_em() const u16 Font::Head::units_per_em() const
{ {
return be_u16(m_slice.offset_pointer(18)); return be_u16(m_slice.offset_pointer((u32) Offsets::UnitsPerEM));
} }
i16 Font::Head::xmin() const i16 Font::Head::xmin() const
{ {
return be_i16(m_slice.offset_pointer(36)); return be_i16(m_slice.offset_pointer((u32) Offsets::XMin));
} }
i16 Font::Head::ymin() const i16 Font::Head::ymin() const
{ {
return be_i16(m_slice.offset_pointer(38)); return be_i16(m_slice.offset_pointer((u32) Offsets::YMin));
} }
i16 Font::Head::xmax() const i16 Font::Head::xmax() const
{ {
return be_i16(m_slice.offset_pointer(40)); return be_i16(m_slice.offset_pointer((u32) Offsets::XMax));
} }
i16 Font::Head::ymax() const i16 Font::Head::ymax() const
{ {
return be_i16(m_slice.offset_pointer(42)); return be_i16(m_slice.offset_pointer((u32) Offsets::YMax));
} }
u16 Font::Head::lowest_recommended_ppem() const u16 Font::Head::lowest_recommended_ppem() const
{ {
return be_u16(m_slice.offset_pointer(46)); return be_u16(m_slice.offset_pointer((u32) Offsets::LowestRecPPEM));
} }
Font::IndexToLocFormat Font::Head::index_to_loc_format() const Font::IndexToLocFormat Font::Head::index_to_loc_format() const
{ {
i16 raw = be_i16(m_slice.offset_pointer(50)); i16 raw = be_i16(m_slice.offset_pointer((u32) Offsets::IndexToLocFormat));
switch (raw) { switch (raw) {
case 0: case 0:
return IndexToLocFormat::Offset16; return IndexToLocFormat::Offset16;
@ -102,34 +102,53 @@ Font::IndexToLocFormat Font::Head::index_to_loc_format() const
i16 Font::Hhea::ascender() const i16 Font::Hhea::ascender() const
{ {
return be_i16(m_slice.offset_pointer(4)); return be_i16(m_slice.offset_pointer((u32) Offsets::Ascender));
} }
i16 Font::Hhea::descender() const i16 Font::Hhea::descender() const
{ {
return be_i16(m_slice.offset_pointer(6)); return be_i16(m_slice.offset_pointer((u32) Offsets::Descender));
} }
i16 Font::Hhea::line_gap() const i16 Font::Hhea::line_gap() const
{ {
return be_i16(m_slice.offset_pointer(8)); return be_i16(m_slice.offset_pointer((u32) Offsets::LineGap));
} }
u16 Font::Hhea::advance_width_max() const u16 Font::Hhea::advance_width_max() const
{ {
return be_u16(m_slice.offset_pointer(10)); return be_u16(m_slice.offset_pointer((u32) Offsets::AdvanceWidthMax));
} }
u16 Font::Hhea::number_of_h_metrics() const u16 Font::Hhea::number_of_h_metrics() const
{ {
return be_u16(m_slice.offset_pointer(34)); return be_u16(m_slice.offset_pointer((u32) Offsets::NumberOfHMetrics));
} }
u16 Font::Maxp::num_glyphs() const u16 Font::Maxp::num_glyphs() const
{ {
return be_u16(m_slice.offset_pointer(4)); return be_u16(m_slice.offset_pointer((u32) Offsets::NumGlyphs));
} }
enum class SimpleGlyfFlags {
// From spec.
OnCurve = 0x01,
XShortVector = 0x02,
YShortVector = 0x04,
RepeatFlag = 0x08,
XIsSameOrPositiveXShortVector = 0x10,
YIsSameOrPositiveYShortVector = 0x20,
// Combinations
XMask = 0x12,
YMask = 0x24,
XLongVector = 0x00,
YLongVector = 0x00,
XNegativeShortVector = 0x02,
YNegativeShortVector = 0x04,
XPositiveShortVector = 0x12,
YPositiveShortVector = 0x24,
};
class PointIterator { class PointIterator {
public: public:
struct Item { struct Item {
@ -159,33 +178,33 @@ public:
m_flags_remaining--; m_flags_remaining--;
} else { } else {
m_flag = m_slice[m_flags_offset++]; m_flag = m_slice[m_flags_offset++];
if (m_flag & 0x08) { if (m_flag & (u8) SimpleGlyfFlags::RepeatFlag) {
m_flags_remaining = m_slice[m_flags_offset++]; m_flags_remaining = m_slice[m_flags_offset++];
} }
} }
switch (m_flag & 0x12) { switch (m_flag & (u8) SimpleGlyfFlags::XMask) {
case 0x00: case (u8) SimpleGlyfFlags::XLongVector:
m_last_point.set_x(m_last_point.x() + be_i16(m_slice.offset_pointer(m_x_offset))); m_last_point.set_x(m_last_point.x() + be_i16(m_slice.offset_pointer(m_x_offset)));
m_x_offset += 2; m_x_offset += 2;
break; break;
case 0x02: case (u8) SimpleGlyfFlags::XNegativeShortVector:
m_last_point.set_x(m_last_point.x() - m_slice[m_x_offset++]); m_last_point.set_x(m_last_point.x() - m_slice[m_x_offset++]);
break; break;
case 0x12: case (u8) SimpleGlyfFlags::XPositiveShortVector:
m_last_point.set_x(m_last_point.x() + m_slice[m_x_offset++]); m_last_point.set_x(m_last_point.x() + m_slice[m_x_offset++]);
break; break;
default: default:
break; break;
} }
switch (m_flag & 0x24) { switch (m_flag & (u8) SimpleGlyfFlags::YMask) {
case 0x00: case (u8) SimpleGlyfFlags::YLongVector:
m_last_point.set_y(m_last_point.y() + be_i16(m_slice.offset_pointer(m_y_offset))); m_last_point.set_y(m_last_point.y() + be_i16(m_slice.offset_pointer(m_y_offset)));
m_y_offset += 2; m_y_offset += 2;
break; break;
case 0x04: case (u8) SimpleGlyfFlags::YNegativeShortVector:
m_last_point.set_y(m_last_point.y() - m_slice[m_y_offset++]); m_last_point.set_y(m_last_point.y() - m_slice[m_y_offset++]);
break; break;
case 0x24: case (u8) SimpleGlyfFlags::YPositiveShortVector:
m_last_point.set_y(m_last_point.y() + m_slice[m_y_offset++]); m_last_point.set_y(m_last_point.y() + m_slice[m_y_offset++]);
break; break;
default: default:
@ -193,7 +212,7 @@ public:
} }
m_points_remaining--; m_points_remaining--;
Item ret = { Item ret = {
.on_curve = (m_flag & 0x01) != 0, .on_curve = (m_flag & (u8) SimpleGlyfFlags::OnCurve) != 0,
.point = m_last_point, .point = m_last_point,
}; };
ret.point.move_by(m_x_translate, m_y_translate); ret.point.move_by(m_x_translate, m_y_translate);
@ -332,7 +351,7 @@ Font::GlyphHorizontalMetrics Font::Hmtx::get_glyph_horizontal_metrics(u32 glyph_
{ {
ASSERT(glyph_id < m_num_glyphs); ASSERT(glyph_id < m_num_glyphs);
if (glyph_id < m_number_of_h_metrics) { if (glyph_id < m_number_of_h_metrics) {
auto offset = glyph_id * 4; auto offset = glyph_id * (u32) Sizes::LongHorMetric;
u16 advance_width = be_u16(m_slice.offset_pointer(offset)); u16 advance_width = be_u16(m_slice.offset_pointer(offset));
i16 left_side_bearing = be_i16(m_slice.offset_pointer(offset + 2)); i16 left_side_bearing = be_i16(m_slice.offset_pointer(offset + 2));
return GlyphHorizontalMetrics { return GlyphHorizontalMetrics {
@ -340,9 +359,9 @@ Font::GlyphHorizontalMetrics Font::Hmtx::get_glyph_horizontal_metrics(u32 glyph_
.left_side_bearing = left_side_bearing, .left_side_bearing = left_side_bearing,
}; };
} else { } else {
auto offset = m_number_of_h_metrics * 4 + (glyph_id - m_number_of_h_metrics) * 2; 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) * 2)); 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 + 2)); i16 left_side_bearing = be_i16(m_slice.offset_pointer(offset));
return GlyphHorizontalMetrics { return GlyphHorizontalMetrics {
.advance_width = advance_width, .advance_width = advance_width,
.left_side_bearing = left_side_bearing, .left_side_bearing = left_side_bearing,
@ -379,7 +398,7 @@ Font::Cmap::Subtable::Format Font::Cmap::Subtable::format() const
u32 Font::Cmap::num_subtables() const u32 Font::Cmap::num_subtables() const
{ {
return be_u16(m_slice.offset_pointer(2)); return be_u16(m_slice.offset_pointer((u32) Offsets::NumTables));
} }
Optional<Font::Cmap::Subtable> Font::Cmap::subtable(u32 index) const Optional<Font::Cmap::Subtable> Font::Cmap::subtable(u32 index) const
@ -387,10 +406,10 @@ Optional<Font::Cmap::Subtable> Font::Cmap::subtable(u32 index) const
if (index >= num_subtables()) { if (index >= num_subtables()) {
return {}; return {};
} }
u32 record_offset = 4 + index * 8; u32 record_offset = (u32) Sizes::TableHeader + index * (u32) Sizes::EncodingRecord;
u16 platform_id = be_u16(m_slice.offset_pointer(record_offset)); u16 platform_id = be_u16(m_slice.offset_pointer(record_offset));
u16 encoding_id = be_u16(m_slice.offset_pointer(record_offset + 2)); u16 encoding_id = be_u16(m_slice.offset_pointer(record_offset + (u32) Offsets::EncodingRecord_EncodingID));
u32 subtable_offset = be_u32(m_slice.offset_pointer(record_offset + 4)); u32 subtable_offset = be_u32(m_slice.offset_pointer(record_offset + (u32) Offsets::EncodingRecord_Offset));
ASSERT(subtable_offset < m_slice.size()); ASSERT(subtable_offset < m_slice.size());
auto subtable_slice = ByteBuffer::wrap(m_slice.offset_pointer(subtable_offset), m_slice.size() - subtable_offset); auto subtable_slice = ByteBuffer::wrap(m_slice.offset_pointer(subtable_offset), m_slice.size() - subtable_offset);
return Subtable(subtable_slice, platform_id, encoding_id); return Subtable(subtable_slice, platform_id, encoding_id);
@ -411,25 +430,25 @@ u32 Font::Cmap::Subtable::glyph_id_for_codepoint(u32 codepoint) const
u32 Font::Cmap::Subtable::glyph_id_for_codepoint_table_4(u32 codepoint) const u32 Font::Cmap::Subtable::glyph_id_for_codepoint_table_4(u32 codepoint) const
{ {
u32 segcount_x2 = be_u16(m_slice.offset_pointer(6)); u32 segcount_x2 = be_u16(m_slice.offset_pointer((u32) Table4Offsets::SegCountX2));
if (m_slice.size() < segcount_x2 * 4 + 16) { if (m_slice.size() < segcount_x2 * (u32) Table4Sizes::NonConstMultiplier + (u32) Table4Sizes::Constant) {
return 0; return 0;
} }
for (u32 offset = 0; offset < segcount_x2; offset += 2) { for (u32 offset = 0; offset < segcount_x2; offset += 2) {
u32 end_codepoint = be_u16(m_slice.offset_pointer(14 + offset)); u32 end_codepoint = be_u16(m_slice.offset_pointer((u32) Table4Offsets::EndConstBase + offset));
if (codepoint > end_codepoint) { if (codepoint > end_codepoint) {
continue; continue;
} }
u32 start_codepoint = be_u16(m_slice.offset_pointer(16 + segcount_x2 + offset)); u32 start_codepoint = be_u16(m_slice.offset_pointer((u32) Table4Offsets::StartConstBase + segcount_x2 + offset));
if (codepoint < start_codepoint) { if (codepoint < start_codepoint) {
break; break;
} }
u32 delta = be_u16(m_slice.offset_pointer(16 + segcount_x2 * 2 + offset)); u32 delta = be_u16(m_slice.offset_pointer((u32) Table4Offsets::DeltaConstBase + segcount_x2 * 2 + offset));
u32 range = be_u16(m_slice.offset_pointer(16 + segcount_x2 * 3 + offset)); u32 range = be_u16(m_slice.offset_pointer((u32) Table4Offsets::RangeConstBase + segcount_x2 * 3 + offset));
if (range == 0) { if (range == 0) {
return (codepoint + delta) & 0xffff; return (codepoint + delta) & 0xffff;
} else { } else {
u32 glyph_offset = 16 + segcount_x2 * 3 + offset + range + (codepoint - start_codepoint) * 2; u32 glyph_offset = (u32) Table4Offsets::GlyphOffsetConstBase + segcount_x2 * 3 + offset + range + (codepoint - start_codepoint) * 2;
ASSERT(glyph_offset + 2 <= m_slice.size()); ASSERT(glyph_offset + 2 <= m_slice.size());
return (be_u16(m_slice.offset_pointer(glyph_offset)) + delta) & 0xffff; return (be_u16(m_slice.offset_pointer(glyph_offset)) + delta) & 0xffff;
} }
@ -439,18 +458,18 @@ u32 Font::Cmap::Subtable::glyph_id_for_codepoint_table_4(u32 codepoint) const
u32 Font::Cmap::Subtable::glyph_id_for_codepoint_table_12(u32 codepoint) const u32 Font::Cmap::Subtable::glyph_id_for_codepoint_table_12(u32 codepoint) const
{ {
u32 num_groups = be_u32(m_slice.offset_pointer(12)); u32 num_groups = be_u32(m_slice.offset_pointer((u32) Table12Offsets::NumGroups));
ASSERT(m_slice.size() >= 16 + 12 * num_groups); ASSERT(m_slice.size() >= (u32) Table12Sizes::Header + (u32) Table12Sizes::Record * num_groups);
for (u32 offset = 0; offset < num_groups * 12; offset += 12) { for (u32 offset = 0; offset < num_groups * (u32) Table12Sizes::Record; offset += (u32) Table12Sizes::Record) {
u32 start_codepoint = be_u32(m_slice.offset_pointer(16 + offset)); u32 start_codepoint = be_u32(m_slice.offset_pointer((u32) Table12Offsets::Record_StartCode + offset));
if (codepoint < start_codepoint) { if (codepoint < start_codepoint) {
break; break;
} }
u32 end_codepoint = be_u32(m_slice.offset_pointer(20 + offset)); u32 end_codepoint = be_u32(m_slice.offset_pointer((u32) Table12Offsets::Record_EndCode + offset));
if (codepoint > end_codepoint) { if (codepoint > end_codepoint) {
continue; continue;
} }
u32 glyph_offset = be_u32(m_slice.offset_pointer(24 + offset)); u32 glyph_offset = be_u32(m_slice.offset_pointer((u32) Table12Offsets::Record_StartGlyph + offset));
return codepoint - start_codepoint + glyph_offset; return codepoint - start_codepoint + glyph_offset;
} }
return 0; return 0;
@ -507,7 +526,7 @@ RefPtr<Bitmap> Font::Glyf::Glyph::raster(float x_scale, float y_scale) const
return raster_simple(x_scale, y_scale); return raster_simple(x_scale, y_scale);
case Type::Composite: case Type::Composite:
// FIXME: Add support for composite glyphs // FIXME: Add support for composite glyphs
ASSERT_NOT_REACHED(); TODO();
} }
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
@ -519,19 +538,19 @@ static void get_ttglyph_offsets(const ByteBuffer& slice, u32 num_points, u32 fla
u32 repeat_count; u32 repeat_count;
while (num_points > 0) { while (num_points > 0) {
u8 flag = slice[flags_offset + flags_size]; u8 flag = slice[flags_offset + flags_size];
if (flag & 0x08) { if (flag & (u8) SimpleGlyfFlags::RepeatFlag) {
flags_size++; flags_size++;
repeat_count = slice[flags_offset + flags_size] + 1; repeat_count = slice[flags_offset + flags_size] + 1;
} else { } else {
repeat_count = 1; repeat_count = 1;
} }
flags_size++; flags_size++;
switch (flag & 0x12) { switch (flag & (u8) SimpleGlyfFlags::XMask) {
case 0x00: case (u8) SimpleGlyfFlags::XLongVector:
x_size += repeat_count * 2; x_size += repeat_count * 2;
break; break;
case 0x02: case (u8) SimpleGlyfFlags::XNegativeShortVector:
case 0x12: case (u8) SimpleGlyfFlags::XPositiveShortVector:
x_size += repeat_count; x_size += repeat_count;
break; break;
default: default:
@ -646,13 +665,13 @@ RefPtr<Bitmap> Font::Glyf::Glyph::raster_simple(float x_scale, float y_scale) co
Font::Glyf::Glyph Font::Glyf::glyph(u32 offset) const Font::Glyf::Glyph Font::Glyf::glyph(u32 offset) const
{ {
ASSERT(m_slice.size() >= offset + 10); ASSERT(m_slice.size() >= offset + (u32) Sizes::GlyphHeader);
i16 num_contours = be_i16(m_slice.offset_pointer(offset)); i16 num_contours = be_i16(m_slice.offset_pointer(offset));
i16 xmin = be_i16(m_slice.offset_pointer(offset + 2)); i16 xmin = be_i16(m_slice.offset_pointer(offset + (u32) Offsets::XMin));
i16 ymin = be_i16(m_slice.offset_pointer(offset + 4)); i16 ymin = be_i16(m_slice.offset_pointer(offset + (u32) Offsets::YMin));
i16 xmax = be_i16(m_slice.offset_pointer(offset + 6)); i16 xmax = be_i16(m_slice.offset_pointer(offset + (u32) Offsets::XMax));
i16 ymax = be_i16(m_slice.offset_pointer(offset + 8)); i16 ymax = be_i16(m_slice.offset_pointer(offset + (u32) Offsets::YMax));
auto slice = ByteBuffer::wrap(m_slice.offset_pointer(offset + 10), m_slice.size() - offset - 10); auto slice = ByteBuffer::wrap(m_slice.offset_pointer(offset + (u32) Sizes::GlyphHeader), m_slice.size() - offset - (u32) Sizes::GlyphHeader);
if (num_contours < 0) { if (num_contours < 0) {
return Glyph::composite(slice); return Glyph::composite(slice);
} else { } else {
@ -680,11 +699,11 @@ RefPtr<Font> Font::load_from_file(const StringView& path, unsigned index)
u32 tag = be_u32(buffer.data()); u32 tag = be_u32(buffer.data());
if (tag == tag_from_str("ttcf")) { if (tag == tag_from_str("ttcf")) {
// It's a font collection // It's a font collection
if (buffer.size() < 12 + 4 * (index + 1)) { if (buffer.size() < (u32) Sizes::TTCHeaderV1 + sizeof(u32) * (index + 1)) {
dbg() << "Font file too small"; dbg() << "Font file too small";
return nullptr; return nullptr;
} }
u32 offset = be_u32(buffer.offset_pointer(12 + 4 * index)); u32 offset = be_u32(buffer.offset_pointer((u32) Sizes::TTCHeaderV1 + sizeof(u32) * index));
return adopt(*new Font(move(buffer), offset)); return adopt(*new Font(move(buffer), offset));
} else if (tag == tag_from_str("OTTO")) { } else if (tag == tag_from_str("OTTO")) {
dbg() << "CFF fonts not supported yet"; dbg() << "CFF fonts not supported yet";
@ -701,7 +720,7 @@ RefPtr<Font> Font::load_from_file(const StringView& path, unsigned index)
Font::Font(ByteBuffer&& buffer, u32 offset) Font::Font(ByteBuffer&& buffer, u32 offset)
: m_buffer(move(buffer)) : m_buffer(move(buffer))
{ {
ASSERT(m_buffer.size() >= offset + 12); ASSERT(m_buffer.size() >= offset + (u32) Sizes::OffsetTable);
Optional<ByteBuffer> head_slice = {}; Optional<ByteBuffer> head_slice = {};
Optional<ByteBuffer> hhea_slice = {}; Optional<ByteBuffer> hhea_slice = {};
Optional<ByteBuffer> maxp_slice = {}; Optional<ByteBuffer> maxp_slice = {};
@ -711,14 +730,14 @@ Font::Font(ByteBuffer&& buffer, u32 offset)
Optional<ByteBuffer> glyf_slice = {}; Optional<ByteBuffer> glyf_slice = {};
//auto sfnt_version = be_u32(data + offset); //auto sfnt_version = be_u32(data + offset);
auto num_tables = be_u16(m_buffer.offset_pointer(offset + 4)); auto num_tables = be_u16(m_buffer.offset_pointer(offset + (u32) Offsets::NumTables));
ASSERT(m_buffer.size() >= offset + 12 + num_tables * 16); ASSERT(m_buffer.size() >= offset + (u32) Sizes::OffsetTable + num_tables * (u32) Sizes::TableRecord);
for (auto i = 0; i < num_tables; i++) { for (auto i = 0; i < num_tables; i++) {
u32 record_offset = offset + 12 + i * 16; u32 record_offset = offset + (u32) Sizes::OffsetTable + i * (u32) Sizes::TableRecord;
u32 tag = be_u32(m_buffer.offset_pointer(record_offset)); u32 tag = be_u32(m_buffer.offset_pointer(record_offset));
u32 table_offset = be_u32(m_buffer.offset_pointer(record_offset + 8)); u32 table_offset = be_u32(m_buffer.offset_pointer(record_offset + (u32) Offsets::TableRecord_Offset));
u32 table_length = be_u32(m_buffer.offset_pointer(record_offset + 12)); u32 table_length = be_u32(m_buffer.offset_pointer(record_offset + (u32) Offsets::TableRecord_Length));
ASSERT(m_buffer.size() >= table_offset + table_length); ASSERT(m_buffer.size() >= table_offset + table_length);
auto buffer = ByteBuffer::wrap(m_buffer.offset_pointer(table_offset), table_length); auto buffer = ByteBuffer::wrap(m_buffer.offset_pointer(table_offset), table_length);
@ -767,11 +786,11 @@ Font::Font(ByteBuffer&& buffer, u32 offset)
} }
auto subtable = opt_subtable.value(); auto subtable = opt_subtable.value();
if (subtable.platform_id() == Cmap::Subtable::Platform::Windows) { if (subtable.platform_id() == Cmap::Subtable::Platform::Windows) {
if (subtable.encoding_id() == 10) { if (subtable.encoding_id() == (u16) Cmap::Subtable::WindowsEncoding::UnicodeFullRepertoire) {
m_cmap.set_active_index(i); m_cmap.set_active_index(i);
break; break;
} }
if (subtable.encoding_id() == 1) { if (subtable.encoding_id() == (u16) Cmap::Subtable::WindowsEncoding::UnicodeBMP) {
m_cmap.set_active_index(i); m_cmap.set_active_index(i);
break; break;
} }

View file

@ -35,6 +35,10 @@
#include <LibGfx/Size.h> #include <LibGfx/Size.h>
namespace Gfx { namespace Gfx {
#define POINTS_PER_INCH 72.0f
#define DEFAULT_DPI 96
namespace TTF { namespace TTF {
class ScaledFont; class ScaledFont;
@ -65,6 +69,17 @@ public:
static RefPtr<Font> load_from_file(const StringView& path, unsigned index); static RefPtr<Font> load_from_file(const StringView& path, unsigned index);
private: private:
enum class Offsets {
NumTables = 4,
TableRecord_Offset = 8,
TableRecord_Length = 12,
};
enum class Sizes {
TTCHeaderV1 = 12,
OffsetTable = 12,
TableRecord = 16,
};
Font(ByteBuffer&& buffer, u32 offset); Font(ByteBuffer&& buffer, u32 offset);
ScaledFontMetrics metrics(float x_scale, float y_scale) const; ScaledFontMetrics metrics(float x_scale, float y_scale) const;
ScaledGlyphMetrics glyph_metrics(u32 glyph_id, float x_scale, float y_scale) const; ScaledGlyphMetrics glyph_metrics(u32 glyph_id, float x_scale, float y_scale) const;
@ -82,7 +97,7 @@ private:
Head(const ByteBuffer& slice) Head(const ByteBuffer& slice)
: m_slice(slice) : m_slice(slice)
{ {
ASSERT(m_slice.size() >= 54); ASSERT(m_slice.size() >= (size_t) Sizes::Table);
} }
u16 units_per_em() const; u16 units_per_em() const;
i16 xmin() const; i16 xmin() const;
@ -93,6 +108,19 @@ private:
IndexToLocFormat index_to_loc_format() const; IndexToLocFormat index_to_loc_format() const;
private: private:
enum class Offsets {
UnitsPerEM = 18,
XMin = 36,
YMin = 38,
XMax = 40,
YMax = 42,
LowestRecPPEM = 46,
IndexToLocFormat = 50,
};
enum class Sizes {
Table = 54,
};
ByteBuffer m_slice; ByteBuffer m_slice;
}; };
@ -102,7 +130,7 @@ private:
Hhea(const ByteBuffer& slice) Hhea(const ByteBuffer& slice)
: m_slice(slice) : m_slice(slice)
{ {
ASSERT(m_slice.size() >= 36); ASSERT(m_slice.size() >= (size_t) Sizes::Table);
} }
i16 ascender() const; i16 ascender() const;
i16 descender() const; i16 descender() const;
@ -111,6 +139,17 @@ private:
u16 number_of_h_metrics() const; u16 number_of_h_metrics() const;
private: private:
enum class Offsets {
Ascender = 4,
Descender = 6,
LineGap = 8,
AdvanceWidthMax = 10,
NumberOfHMetrics = 34,
};
enum class Sizes {
Table = 36,
};
ByteBuffer m_slice; ByteBuffer m_slice;
}; };
@ -120,11 +159,18 @@ private:
Maxp(const ByteBuffer& slice) Maxp(const ByteBuffer& slice)
: m_slice(slice) : m_slice(slice)
{ {
ASSERT(m_slice.size() >= 6); ASSERT(m_slice.size() >= (size_t) Sizes::TableV0p5);
} }
u16 num_glyphs() const; u16 num_glyphs() const;
private: private:
enum class Offsets {
NumGlyphs = 4
};
enum class Sizes {
TableV0p5 = 6,
};
ByteBuffer m_slice; ByteBuffer m_slice;
}; };
@ -141,11 +187,16 @@ private:
, m_num_glyphs(num_glyphs) , m_num_glyphs(num_glyphs)
, m_number_of_h_metrics(number_of_h_metrics) , m_number_of_h_metrics(number_of_h_metrics)
{ {
ASSERT(m_slice.size() >= number_of_h_metrics * 2 + num_glyphs * 2); ASSERT(m_slice.size() >= number_of_h_metrics * (u32) Sizes::LongHorMetric + (num_glyphs - number_of_h_metrics) * (u32) Sizes::LeftSideBearing);
} }
GlyphHorizontalMetrics get_glyph_horizontal_metrics(u32 glyph_id) const; GlyphHorizontalMetrics get_glyph_horizontal_metrics(u32 glyph_id) const;
private: private:
enum class Sizes {
LongHorMetric = 4,
LeftSideBearing = 2
};
ByteBuffer m_slice; ByteBuffer m_slice;
u32 m_num_glyphs; u32 m_num_glyphs;
u32 m_number_of_h_metrics; u32 m_number_of_h_metrics;
@ -156,22 +207,25 @@ private:
class Subtable { class Subtable {
public: public:
enum class Platform { enum class Platform {
Unicode, Unicode = 0,
Macintosh, Macintosh = 1,
Windows, Windows = 3,
Custom, Custom = 4,
}; };
enum class Format { enum class Format {
ByteEncoding, ByteEncoding = 0,
HighByte, HighByte = 2,
SegmentToDelta, SegmentToDelta = 4,
TrimmedTable, TrimmedTable = 6,
Mixed16And32, Mixed16And32 = 8,
TrimmedArray, TrimmedArray = 10,
SegmentedCoverage, SegmentedCoverage = 12,
ManyToOneRange, ManyToOneRange = 13,
UnicodeVariationSequences, UnicodeVariationSequences = 14,
};
enum class WindowsEncoding {
UnicodeBMP = 1,
UnicodeFullRepertoire = 10,
}; };
Subtable(const ByteBuffer& slice, u16 platform_id, u16 encoding_id) Subtable(const ByteBuffer& slice, u16 platform_id, u16 encoding_id)
@ -187,6 +241,29 @@ private:
Format format() const; Format format() const;
private: private:
enum class Table4Offsets {
SegCountX2 = 6,
EndConstBase = 14,
StartConstBase = 16,
DeltaConstBase = 16,
RangeConstBase = 16,
GlyphOffsetConstBase = 16,
};
enum class Table4Sizes {
Constant = 16,
NonConstMultiplier = 4,
};
enum class Table12Offsets {
NumGroups = 12,
Record_StartCode = 16,
Record_EndCode = 20,
Record_StartGlyph = 24,
};
enum class Table12Sizes {
Header = 16,
Record = 12,
};
u32 glyph_id_for_codepoint_table_4(u32 codepoint) const; u32 glyph_id_for_codepoint_table_4(u32 codepoint) const;
u32 glyph_id_for_codepoint_table_12(u32 codepoint) const; u32 glyph_id_for_codepoint_table_12(u32 codepoint) const;
@ -199,7 +276,7 @@ private:
Cmap(const ByteBuffer& slice) Cmap(const ByteBuffer& slice)
: m_slice(slice) : m_slice(slice)
{ {
ASSERT(m_slice.size() > 4); ASSERT(m_slice.size() > (size_t) Sizes::TableHeader);
} }
u32 num_subtables() const; u32 num_subtables() const;
@ -209,6 +286,16 @@ private:
u32 glyph_id_for_codepoint(u32 codepoint) const; u32 glyph_id_for_codepoint(u32 codepoint) const;
private: private:
enum class Offsets {
NumTables = 2,
EncodingRecord_EncodingID = 2,
EncodingRecord_Offset = 4,
};
enum class Sizes {
TableHeader = 4,
EncodingRecord = 8,
};
ByteBuffer m_slice; ByteBuffer m_slice;
u32 m_active_index { UINT32_MAX }; u32 m_active_index { UINT32_MAX };
}; };
@ -251,7 +338,7 @@ private:
return m_meta.simple.ymax; return m_meta.simple.ymax;
} else { } else {
// FIXME: Support composite outlines. // FIXME: Support composite outlines.
ASSERT_NOT_REACHED(); TODO();
} }
} }
int descender() const int descender() const
@ -260,7 +347,7 @@ private:
return m_meta.simple.ymin; return m_meta.simple.ymin;
} else { } else {
// FIXME: Support composite outlines. // FIXME: Support composite outlines.
ASSERT_NOT_REACHED(); TODO();
} }
} }
@ -302,6 +389,16 @@ private:
Glyph glyph(u32 offset) const; Glyph glyph(u32 offset) const;
private: private:
enum class Offsets {
XMin = 2,
YMin = 4,
XMax = 6,
YMax = 8,
};
enum class Sizes {
GlyphHeader = 10,
};
ByteBuffer m_slice; ByteBuffer m_slice;
}; };
@ -319,12 +416,12 @@ private:
class ScaledFont { class ScaledFont {
public: public:
ScaledFont(RefPtr<Font> font, float point_width, float point_height, unsigned dpi_x = 96, unsigned dpi_y = 96) ScaledFont(RefPtr<Font> font, float point_width, float point_height, unsigned dpi_x = DEFAULT_DPI, unsigned dpi_y = DEFAULT_DPI)
: m_font(font) : m_font(font)
{ {
float units_per_em = m_font->m_head.units_per_em(); float units_per_em = m_font->m_head.units_per_em();
m_x_scale = (point_width * dpi_x) / (72.0 * units_per_em); m_x_scale = (point_width * dpi_x) / (POINTS_PER_INCH * units_per_em);
m_y_scale = (point_height * dpi_y) / (72.0 * units_per_em); m_y_scale = (point_height * dpi_y) / (POINTS_PER_INCH * units_per_em);
} }
u32 glyph_id_for_codepoint(u32 codepoint) const { return m_font->m_cmap.glyph_id_for_codepoint(codepoint); } u32 glyph_id_for_codepoint(u32 codepoint) const { return m_font->m_cmap.glyph_id_for_codepoint(codepoint); }
ScaledFontMetrics metrics() const { return m_font->metrics(m_x_scale, m_y_scale); } ScaledFontMetrics metrics() const { return m_font->metrics(m_x_scale, m_y_scale); }