Correct decoder output table ordering
This commit is contained in:
+9
-15
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+2
-3
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "./woff2_dec.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <algorithm>
|
||||
#include <complex>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
@@ -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<uint32_t, uint16_t> 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;
|
||||
|
||||
Reference in New Issue
Block a user