Skip to content

Commit

Permalink
[ttf] parse maxp table
Browse files Browse the repository at this point in the history
  • Loading branch information
harrand committed Sep 23, 2023
1 parent 771a1f7 commit 57cb42e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/tz/io/ttf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ namespace tz::io
tbl.length = ttf_read_value<std::uint32_t>(ptr);

tz::assert(std::string{tbl.tag} != "DEAD", "Detected no change to table tag - malformed TTF.");
if(std::string{tbl.tag} == "head")
std::string tagstr{tbl.tag};
if(tagstr == "head")
{
this->parse_head_table(full_data, tbl);
}
Expand All @@ -80,6 +81,10 @@ namespace tz::io
tz::assert(calc == tbl.checksum);

// parse other table types...
if(tagstr == "maxp")
{
this->parse_maxp_table(full_data, tbl);
}
}
}
}
Expand Down Expand Up @@ -131,4 +136,35 @@ namespace tz::io
// set canary to true, meaning we did indeed set the head table.
this->head.canary = true;
}

void ttf::parse_maxp_table(std::string_view data, ttf_table table_descriptor)
{
tz::assert(data.size() > table_descriptor.offset + table_descriptor.length);
data.remove_prefix(table_descriptor.offset);
data.remove_suffix(data.size() - table_descriptor.length);
tz::assert(data.size() == (table_descriptor.length));

tz::assert(!this->maxp.canary, "When parsing maxp table, noticed canary already switched to true. Double maxp table discovery? Most likely malformed TTF.");

const char* ptr = data.data();

//this->head.major_version = ttf_read_value<std::uint16_t>(ptr);
this->maxp.version_fixed_point = ttf_read_value<std::int32_t>(ptr);
this->maxp.version_fixed_point /= (1 << 16);
this->maxp.num_glyphs = ttf_read_value<std::uint16_t>(ptr);
this->maxp.max_points = ttf_read_value<std::uint16_t>(ptr);
this->maxp.max_contours = ttf_read_value<std::uint16_t>(ptr);
this->maxp.max_composite_points = ttf_read_value<std::uint16_t>(ptr);
this->maxp.max_composite_contours = ttf_read_value<std::uint16_t>(ptr);
this->maxp.max_zones = ttf_read_value<std::uint16_t>(ptr);
this->maxp.max_twilight_points = ttf_read_value<std::uint16_t>(ptr);
this->maxp.max_storage = ttf_read_value<std::uint16_t>(ptr);
this->maxp.max_function_defs = ttf_read_value<std::uint16_t>(ptr);
this->maxp.max_instruction_defs = ttf_read_value<std::uint16_t>(ptr);
this->maxp.max_stack_elements = ttf_read_value<std::uint16_t>(ptr);
this->maxp.max_size_of_instructions = ttf_read_value<std::uint16_t>(ptr);
this->maxp.max_component_elements = ttf_read_value<std::uint16_t>(ptr);
this->maxp.max_component_depth = ttf_read_value<std::uint16_t>(ptr);
this->maxp.canary = true;
}
}
22 changes: 22 additions & 0 deletions src/tz/io/ttf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ namespace tz::io
bool canary = false;
};

struct ttf_maxp_table
{
std::int32_t version_fixed_point = 0;
std::uint16_t num_glyphs = 0u;
std::uint16_t max_points = 0u;
std::uint16_t max_contours = 0u;
std::uint16_t max_composite_points = 0u;
std::uint16_t max_composite_contours = 0u;
std::uint16_t max_zones = 0u;
std::uint16_t max_twilight_points = 0u;
std::uint16_t max_storage = 0u;
std::uint16_t max_function_defs = 0u;
std::uint16_t max_instruction_defs = 0u;
std::uint16_t max_stack_elements = 0u;
std::uint16_t max_size_of_instructions = 0u;
std::uint16_t max_component_elements = 0u;
std::uint16_t max_component_depth = 0u;
bool canary = false;
};

class ttf
{
public:
Expand All @@ -58,10 +78,12 @@ namespace tz::io
void parse_table_info(std::string_view str, std::string_view full_data);
std::uint32_t calculate_table_checksum(std::string_view data, std::uint32_t offset, std::uint32_t length) const;
void parse_head_table(std::string_view data, ttf_table table_descriptor);
void parse_maxp_table(std::string_view data, ttf_table table_descriptor);
ttf_header header = {};

std::vector<ttf_table> tables = {};
ttf_head_table head = {};
ttf_maxp_table maxp = {};
};
}

Expand Down

0 comments on commit 57cb42e

Please sign in to comment.