diff --git a/src/font.cc b/src/font.cc index babd9c0..3c2e652 100644 --- a/src/font.cc +++ b/src/font.cc @@ -217,7 +217,7 @@ size_t FontCollectionFileSize(const FontCollection& font_collection) { bool WriteFont(const Font& font, uint8_t* dst, size_t dst_size) { size_t offset = 0; - return WriteFont(font, &offset, dst, dst_size, true); + return WriteFont(font, &offset, dst, dst_size); } bool WriteTable(const Font::Table& table, size_t* offset, uint8_t* dst, @@ -246,7 +246,7 @@ bool WriteTable(const Font::Table& table, size_t* offset, uint8_t* dst, } bool WriteFont(const Font& font, size_t* offset, uint8_t* dst, - size_t dst_size, bool reorder_tables) { + size_t dst_size) { if (dst_size < 12ULL + 16ULL * font.num_tables) { return FONT_COMPRESSION_FAILURE(); } @@ -258,19 +258,13 @@ bool WriteFont(const Font& font, size_t* offset, uint8_t* dst, Store16(search_range, offset, dst); Store16(max_pow2, offset, dst); Store16(range_shift, offset, dst); - if (reorder_tables) { - for (const auto tag : font.OutputOrderedTags()) { - if (!WriteTable(font.tables.at(tag), offset, dst, dst_size)) { - return false; - } - } - } else { - for (const auto& i : font.tables) { - if (!WriteTable(i.second, offset, dst, dst_size)) { - return false; - } + + for (const auto& i : font.tables) { + if (!WriteTable(i.second, offset, dst, dst_size)) { + return false; } } + return true; } @@ -280,7 +274,7 @@ bool WriteFontCollection(const FontCollection& font_collection, uint8_t* dst, // It's simpler if this just a simple sfnt if (font_collection.fonts.size() == 1) { - return WriteFont(font_collection.fonts[0], &offset, dst, dst_size, true); + return WriteFont(font_collection.fonts[0], &offset, dst, dst_size); } // Write TTC header @@ -304,7 +298,7 @@ bool WriteFontCollection(const FontCollection& font_collection, uint8_t* dst, for (int i = 0; i < font_collection.fonts.size(); i++) { const auto& font = font_collection.fonts[i]; StoreU32(offset, &offset_table, dst); - if (!WriteFont(font, &offset, dst, dst_size, false)) { + if (!WriteFont(font, &offset, dst, dst_size)) { return false; } } diff --git a/src/font.h b/src/font.h index 95afe0c..ff7f85f 100644 --- a/src/font.h +++ b/src/font.h @@ -83,9 +83,8 @@ size_t FontCollectionFileSize(const FontCollection& font); // same as returned by FontFileSize(). Returns false upon buffer overflow (which // should not happen if dst_size was computed by FontFileSize()). bool WriteFont(const Font& font, uint8_t* dst, size_t dst_size); -// Write the font at a specific offset, optionally reordering tables -bool WriteFont(const Font& font, size_t* offset, uint8_t* dst, size_t dst_size, - bool reorder_tables); +// Write the font at a specific offset +bool WriteFont(const Font& font, size_t* offset, uint8_t* dst, size_t dst_size); bool WriteFontCollection(const FontCollection& font_collection, uint8_t* dst, size_t dst_size); diff --git a/src/woff2_common.h b/src/woff2_common.h index f1df21f..e6cfdf2 100644 --- a/src/woff2_common.h +++ b/src/woff2_common.h @@ -52,6 +52,10 @@ struct Table { uint32_t dst_offset; uint32_t dst_length; const uint8_t* dst_data; + + bool operator<(const Table& other) const { + return tag < other.tag; + } }; diff --git a/src/woff2_dec.cc b/src/woff2_dec.cc index b18339f..e202672 100644 --- a/src/woff2_dec.cc +++ b/src/woff2_dec.cc @@ -17,6 +17,7 @@ #include "./woff2_dec.h" #include +#include #include #include #include @@ -932,6 +933,24 @@ bool ConvertWOFF2ToTTF(uint8_t* result, size_t result_length, return FONT_COMPRESSION_FAILURE(); } + // Re-order tables in output (OTSpec) order + if (header_version) { + // collection; we have to sort the table offset vector in each font + for (auto& ttc_font : ttc_fonts) { + std::map sorted_index_by_tag; + for (auto table_index : ttc_font.table_indices) { + sorted_index_by_tag[tables[table_index].tag] = table_index; + } + uint16_t index = 0; + for (auto& i : sorted_index_by_tag) { + ttc_font.table_indices[index++] = i.second; + } + } + } else { + // non-collection; we can just sort the tables + std::sort(tables.begin(), tables.end()); + } + // Start building the font size_t offset = 0; size_t offset_table = 0;