Preliminary support for CFF (just don't normalize). Support for adjusting loca entry size from 2 to 4 bytes during normalization if necessary. Support for extended metadata.
This commit is contained in:
+3
-2
@@ -37,6 +37,7 @@ typedef unsigned __int64 uint64_t;
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
@@ -49,8 +50,8 @@ namespace woff2 {
|
|||||||
#define FONT_COMPRESSION_FAILURE() \
|
#define FONT_COMPRESSION_FAILURE() \
|
||||||
util::compression::font::Failure(__FILE__, __LINE__, __PRETTY_FUNCTION__)
|
util::compression::font::Failure(__FILE__, __LINE__, __PRETTY_FUNCTION__)
|
||||||
inline bool Failure(const char *f, int l, const char *fn) {
|
inline bool Failure(const char *f, int l, const char *fn) {
|
||||||
std::fprintf(stderr, "ERROR at %s:%d (%s)\n", f, l, fn);
|
fprintf(stderr, "ERROR at %s:%d (%s)\n", f, l, fn);
|
||||||
std::fflush(stderr);
|
fflush(stderr);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+14
-3
@@ -130,8 +130,17 @@ int NumGlyphs(const Font& font) {
|
|||||||
if (head_table == NULL || loca_table == NULL || head_table->length < 52) {
|
if (head_table == NULL || loca_table == NULL || head_table->length < 52) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int index_fmt = head_table->data[51];
|
int index_fmt = IndexFormat(font);
|
||||||
return (loca_table->length / (index_fmt == 0 ? 2 : 4)) - 1;
|
int num_glyphs = (loca_table->length / (index_fmt == 0 ? 2 : 4)) - 1;
|
||||||
|
return num_glyphs;
|
||||||
|
}
|
||||||
|
|
||||||
|
int IndexFormat(const Font& font) {
|
||||||
|
const Font::Table* head_table = font.FindTable(kHeadTableTag);
|
||||||
|
if (head_table == NULL) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return head_table->data[51];
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GetGlyphData(const Font& font, int glyph_index,
|
bool GetGlyphData(const Font& font, int glyph_index,
|
||||||
@@ -146,7 +155,9 @@ bool GetGlyphData(const Font& font, int glyph_index,
|
|||||||
head_table->length < 52) {
|
head_table->length < 52) {
|
||||||
return FONT_COMPRESSION_FAILURE();
|
return FONT_COMPRESSION_FAILURE();
|
||||||
}
|
}
|
||||||
int index_fmt = head_table->data[51];
|
|
||||||
|
int index_fmt = IndexFormat(font);
|
||||||
|
|
||||||
Buffer loca_buf(loca_table->data, loca_table->length);
|
Buffer loca_buf(loca_table->data, loca_table->length);
|
||||||
if (index_fmt == 0) {
|
if (index_fmt == 0) {
|
||||||
uint16_t offset1, offset2;
|
uint16_t offset1, offset2;
|
||||||
|
|||||||
@@ -66,6 +66,9 @@ bool WriteFont(const Font& font, uint8_t* dst, size_t dst_size);
|
|||||||
// zero for CFF-flavored fonts.
|
// zero for CFF-flavored fonts.
|
||||||
int NumGlyphs(const Font& font);
|
int NumGlyphs(const Font& font);
|
||||||
|
|
||||||
|
// Returns the index format of the font
|
||||||
|
int IndexFormat(const Font& font);
|
||||||
|
|
||||||
// Sets *glyph_data and *glyph_size to point to the location of the glyph data
|
// Sets *glyph_data and *glyph_size to point to the location of the glyph data
|
||||||
// with the given index. Returns false if the glyph is not found.
|
// with the given index. Returns false if the glyph is not found.
|
||||||
bool GetGlyphData(const Font& font, int glyph_index,
|
bool GetGlyphData(const Font& font, int glyph_index,
|
||||||
|
|||||||
+80
-23
@@ -63,29 +63,16 @@ void NormalizeSimpleGlyphBoundingBox(Glyph* glyph) {
|
|||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
bool NormalizeGlyphs(Font* font) {
|
namespace {
|
||||||
Font::Table* head_table = font->FindTable(kHeadTableTag);
|
|
||||||
|
bool WriteNormalizedLoca(int index_fmt, int num_glyphs, Font* font) {
|
||||||
Font::Table* glyf_table = font->FindTable(kGlyfTableTag);
|
Font::Table* glyf_table = font->FindTable(kGlyfTableTag);
|
||||||
Font::Table* loca_table = font->FindTable(kLocaTableTag);
|
Font::Table* loca_table = font->FindTable(kLocaTableTag);
|
||||||
if (head_table == NULL || loca_table == NULL || glyf_table == NULL) {
|
|
||||||
return FONT_COMPRESSION_FAILURE();
|
|
||||||
}
|
|
||||||
int index_fmt = head_table->data[51];
|
|
||||||
int num_glyphs = NumGlyphs(*font);
|
|
||||||
|
|
||||||
// We need to allocate a bit more than its original length for the normalized
|
int glyph_sz = index_fmt == 0 ? 2 : 4;
|
||||||
// glyf table, since it can happen that the glyphs in the original table are
|
loca_table->buffer.resize(Round4(num_glyphs + 1) * glyph_sz);
|
||||||
// 2-byte aligned, while in the normalized table they are 4-byte aligned.
|
loca_table->length = (num_glyphs + 1) * glyph_sz;
|
||||||
// That gives a maximum of 2 bytes increase per glyph. However, there is no
|
|
||||||
// theoretical guarantee that the total size of the flags plus the coordinates
|
|
||||||
// is the smallest possible in the normalized version, so we have to allow
|
|
||||||
// some general overhead.
|
|
||||||
// TODO(user) Figure out some more precise upper bound on the size of
|
|
||||||
// the overhead.
|
|
||||||
size_t max_normalized_glyf_size = 1.1 * glyf_table->length + 2 * num_glyphs;
|
|
||||||
|
|
||||||
glyf_table->buffer.resize(max_normalized_glyf_size);
|
|
||||||
loca_table->buffer.resize(Round4(loca_table->length));
|
|
||||||
uint8_t* glyf_dst = &glyf_table->buffer[0];
|
uint8_t* glyf_dst = &glyf_table->buffer[0];
|
||||||
uint8_t* loca_dst = &loca_table->buffer[0];
|
uint8_t* loca_dst = &loca_table->buffer[0];
|
||||||
uint32_t glyf_offset = 0;
|
uint32_t glyf_offset = 0;
|
||||||
@@ -113,6 +100,10 @@ bool NormalizeGlyphs(Font* font) {
|
|||||||
}
|
}
|
||||||
glyf_offset += glyf_dst_size;
|
glyf_offset += glyf_dst_size;
|
||||||
}
|
}
|
||||||
|
if (glyf_offset == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
StoreLoca(index_fmt, glyf_offset, &loca_offset, loca_dst);
|
StoreLoca(index_fmt, glyf_offset, &loca_offset, loca_dst);
|
||||||
|
|
||||||
glyf_table->buffer.resize(glyf_offset);
|
glyf_table->buffer.resize(glyf_offset);
|
||||||
@@ -123,6 +114,74 @@ bool NormalizeGlyphs(Font* font) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
bool MakeEditableBuffer(Font* font, int tableTag) {
|
||||||
|
Font::Table* table = font->FindTable(tableTag);
|
||||||
|
if (table == NULL) {
|
||||||
|
return FONT_COMPRESSION_FAILURE();
|
||||||
|
}
|
||||||
|
int sz = Round4(table->length);
|
||||||
|
table->buffer.resize(sz);
|
||||||
|
uint8_t* buf = &table->buffer[0];
|
||||||
|
memcpy(buf, table->data, sz);
|
||||||
|
table->data = buf;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
bool NormalizeGlyphs(Font* font) {
|
||||||
|
Font::Table* cff_table = font->FindTable(kCffTableTag);
|
||||||
|
Font::Table* head_table = font->FindTable(kHeadTableTag);
|
||||||
|
Font::Table* glyf_table = font->FindTable(kGlyfTableTag);
|
||||||
|
Font::Table* loca_table = font->FindTable(kLocaTableTag);
|
||||||
|
if (head_table == NULL) {
|
||||||
|
return FONT_COMPRESSION_FAILURE();
|
||||||
|
}
|
||||||
|
// CFF, no loca, no glyf is OK for CFF. If so, don't normalize.
|
||||||
|
if (cff_table != NULL && loca_table == NULL && glyf_table == NULL) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (loca_table == NULL || glyf_table == NULL) {
|
||||||
|
return FONT_COMPRESSION_FAILURE();
|
||||||
|
}
|
||||||
|
int index_fmt = head_table->data[51];
|
||||||
|
int num_glyphs = NumGlyphs(*font);
|
||||||
|
|
||||||
|
// We need to allocate a bit more than its original length for the normalized
|
||||||
|
// glyf table, since it can happen that the glyphs in the original table are
|
||||||
|
// 2-byte aligned, while in the normalized table they are 4-byte aligned.
|
||||||
|
// That gives a maximum of 2 bytes increase per glyph. However, there is no
|
||||||
|
// theoretical guarantee that the total size of the flags plus the coordinates
|
||||||
|
// is the smallest possible in the normalized version, so we have to allow
|
||||||
|
// some general overhead.
|
||||||
|
// TODO(user) Figure out some more precise upper bound on the size of
|
||||||
|
// the overhead.
|
||||||
|
size_t max_normalized_glyf_size = 1.1 * glyf_table->length + 2 * num_glyphs;
|
||||||
|
|
||||||
|
glyf_table->buffer.resize(max_normalized_glyf_size);
|
||||||
|
|
||||||
|
// if we can't write a loca using short's (index_fmt 0)
|
||||||
|
// try again using longs (index_fmt 1)
|
||||||
|
if (!WriteNormalizedLoca(index_fmt, num_glyphs, font)) {
|
||||||
|
if (index_fmt != 0) {
|
||||||
|
return FONT_COMPRESSION_FAILURE();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rewrite loca with 4-byte entries & update head to match
|
||||||
|
index_fmt = 1;
|
||||||
|
if (!WriteNormalizedLoca(index_fmt, num_glyphs, font)) {
|
||||||
|
return FONT_COMPRESSION_FAILURE();
|
||||||
|
}
|
||||||
|
head_table->buffer[51] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool NormalizeOffsets(Font* font) {
|
bool NormalizeOffsets(Font* font) {
|
||||||
uint32_t offset = 12 + 16 * font->num_tables;
|
uint32_t offset = 12 + 16 * font->num_tables;
|
||||||
for (auto& i : font->tables) {
|
for (auto& i : font->tables) {
|
||||||
@@ -168,10 +227,7 @@ bool FixChecksums(Font* font) {
|
|||||||
if (head_table == NULL || head_table->length < 12) {
|
if (head_table == NULL || head_table->length < 12) {
|
||||||
return FONT_COMPRESSION_FAILURE();
|
return FONT_COMPRESSION_FAILURE();
|
||||||
}
|
}
|
||||||
head_table->buffer.resize(Round4(head_table->length));
|
|
||||||
uint8_t* head_buf = &head_table->buffer[0];
|
uint8_t* head_buf = &head_table->buffer[0];
|
||||||
memcpy(head_buf, head_table->data, Round4(head_table->length));
|
|
||||||
head_table->data = head_buf;
|
|
||||||
size_t offset = 8;
|
size_t offset = 8;
|
||||||
StoreU32(0, &offset, head_buf);
|
StoreU32(0, &offset, head_buf);
|
||||||
uint32_t file_checksum = 0;
|
uint32_t file_checksum = 0;
|
||||||
@@ -187,7 +243,8 @@ bool FixChecksums(Font* font) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool NormalizeFont(Font* font) {
|
bool NormalizeFont(Font* font) {
|
||||||
return (RemoveDigitalSignature(font) &&
|
return (MakeEditableBuffer(font, kHeadTableTag) &&
|
||||||
|
RemoveDigitalSignature(font) &&
|
||||||
NormalizeGlyphs(font) &&
|
NormalizeGlyphs(font) &&
|
||||||
NormalizeOffsets(font) &&
|
NormalizeOffsets(font) &&
|
||||||
FixChecksums(font));
|
FixChecksums(font));
|
||||||
|
|||||||
@@ -17,6 +17,8 @@
|
|||||||
#ifndef WOFF2_ROUND_H_
|
#ifndef WOFF2_ROUND_H_
|
||||||
#define WOFF2_ROUND_H_
|
#define WOFF2_ROUND_H_
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
namespace woff2 {
|
namespace woff2 {
|
||||||
|
|
||||||
// Round a value up to the nearest multiple of 4. Don't round the value in the
|
// Round a value up to the nearest multiple of 4. Don't round the value in the
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ static const uint32_t kGlyfTableTag = 0x676c7966;
|
|||||||
static const uint32_t kHeadTableTag = 0x68656164;
|
static const uint32_t kHeadTableTag = 0x68656164;
|
||||||
static const uint32_t kLocaTableTag = 0x6c6f6361;
|
static const uint32_t kLocaTableTag = 0x6c6f6361;
|
||||||
static const uint32_t kDsigTableTag = 0x44534947;
|
static const uint32_t kDsigTableTag = 0x44534947;
|
||||||
|
static const uint32_t kCffTableTag = 0x43464620;
|
||||||
|
|
||||||
extern const uint32_t kKnownTags[];
|
extern const uint32_t kKnownTags[];
|
||||||
|
|
||||||
|
|||||||
@@ -227,6 +227,12 @@ class GlyfEncoder {
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
bool TransformGlyfAndLocaTables(Font* font) {
|
bool TransformGlyfAndLocaTables(Font* font) {
|
||||||
|
// no transform for CFF
|
||||||
|
if (font->FindTable(kCffTableTag) != NULL
|
||||||
|
&& font->FindTable(kGlyfTableTag) == NULL
|
||||||
|
&& font->FindTable(kLocaTableTag) == NULL) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
Font::Table* transformed_glyf = &font->tables[kGlyfTableTag ^ 0x80808080];
|
Font::Table* transformed_glyf = &font->tables[kGlyfTableTag ^ 0x80808080];
|
||||||
Font::Table* transformed_loca = &font->tables[kLocaTableTag ^ 0x80808080];
|
Font::Table* transformed_loca = &font->tables[kLocaTableTag ^ 0x80808080];
|
||||||
|
|
||||||
|
|||||||
+59
-10
@@ -12,7 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
//
|
//
|
||||||
// Library for converting WOFF2 format font files to their TTF versions.
|
// Library for converting TTF format font files to their WOFF2 versions.
|
||||||
|
|
||||||
#include "./woff2_enc.h"
|
#include "./woff2_enc.h"
|
||||||
|
|
||||||
@@ -53,7 +53,7 @@ size_t Base128Size(size_t n) {
|
|||||||
void StoreBase128(size_t len, size_t* offset, uint8_t* dst) {
|
void StoreBase128(size_t len, size_t* offset, uint8_t* dst) {
|
||||||
size_t size = Base128Size(len);
|
size_t size = Base128Size(len);
|
||||||
for (int i = 0; i < size; ++i) {
|
for (int i = 0; i < size; ++i) {
|
||||||
int b = (int)(len >> (7 * (size - i - 1))) & 0x7f;
|
int b = static_cast<int>((len >> (7 * (size - i - 1))) & 0x7f);
|
||||||
if (i < size - 1) {
|
if (i < size - 1) {
|
||||||
b |= 0x80;
|
b |= 0x80;
|
||||||
}
|
}
|
||||||
@@ -96,6 +96,9 @@ int KnownTableIndex(uint32_t tag) {
|
|||||||
void StoreTableEntry(const Table& table, size_t* offset, uint8_t* dst) {
|
void StoreTableEntry(const Table& table, size_t* offset, uint8_t* dst) {
|
||||||
uint8_t flag_byte = KnownTableIndex(table.tag);
|
uint8_t flag_byte = KnownTableIndex(table.tag);
|
||||||
dst[(*offset)++] = flag_byte;
|
dst[(*offset)++] = flag_byte;
|
||||||
|
// The index here is treated as a set of flag bytes because
|
||||||
|
// bits 6 and 7 of the byte are reserved for future use as flags.
|
||||||
|
// 0x3f or 63 means an arbitrary table tag.
|
||||||
if ((flag_byte & 0x3f) == 0x3f) {
|
if ((flag_byte & 0x3f) == 0x3f) {
|
||||||
StoreU32(table.tag, offset, dst);
|
StoreU32(table.tag, offset, dst);
|
||||||
}
|
}
|
||||||
@@ -106,7 +109,8 @@ void StoreTableEntry(const Table& table, size_t* offset, uint8_t* dst) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t TableEntrySize(const Table& table) {
|
size_t TableEntrySize(const Table& table) {
|
||||||
size_t size = KnownTableIndex(table.tag) < 31 ? 1 : 5;
|
uint8_t flag_byte = KnownTableIndex(table.tag);
|
||||||
|
size_t size = ((flag_byte & 0x3f) != 0x3f) ? 1 : 5;
|
||||||
size += Base128Size(table.src_length);
|
size += Base128Size(table.src_length);
|
||||||
if ((table.flags & kWoff2FlagsTransform) != 0) {
|
if ((table.flags & kWoff2FlagsTransform) != 0) {
|
||||||
size += Base128Size(table.transform_length);
|
size += Base128Size(table.transform_length);
|
||||||
@@ -114,7 +118,8 @@ size_t TableEntrySize(const Table& table) {
|
|||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t ComputeWoff2Length(const std::vector<Table>& tables) {
|
size_t ComputeWoff2Length(const std::vector<Table>& tables,
|
||||||
|
size_t extended_metadata_length) {
|
||||||
size_t size = kWoff2HeaderSize;
|
size_t size = kWoff2HeaderSize;
|
||||||
for (const auto& table : tables) {
|
for (const auto& table : tables) {
|
||||||
size += TableEntrySize(table);
|
size += TableEntrySize(table);
|
||||||
@@ -123,6 +128,7 @@ size_t ComputeWoff2Length(const std::vector<Table>& tables) {
|
|||||||
size += table.dst_length;
|
size += table.dst_length;
|
||||||
size = Round4(size);
|
size = Round4(size);
|
||||||
}
|
}
|
||||||
|
size += extended_metadata_length;
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,15 +156,30 @@ size_t ComputeTotalTransformLength(const Font& font) {
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length) {
|
size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length) {
|
||||||
|
return MaxWOFF2CompressedSize(data, length, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length,
|
||||||
|
const string& extended_metadata) {
|
||||||
// Except for the header size, which is 32 bytes larger in woff2 format,
|
// Except for the header size, which is 32 bytes larger in woff2 format,
|
||||||
// all other parts should be smaller (table header in short format,
|
// all other parts should be smaller (table header in short format,
|
||||||
// transformations and compression). Just to be sure, we will give some
|
// transformations and compression). Just to be sure, we will give some
|
||||||
// headroom anyway.
|
// headroom anyway.
|
||||||
return length + 1024;
|
return length + 1024 + extended_metadata.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t CompressedBufferSize(uint32_t original_size) {
|
||||||
|
return 1.2 * original_size + 10240;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
|
bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
|
||||||
uint8_t *result, size_t *result_length) {
|
uint8_t *result, size_t *result_length) {
|
||||||
|
return ConvertTTFToWOFF2(data, length, result, result_length, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
|
||||||
|
uint8_t *result, size_t *result_length,
|
||||||
|
const string& extended_metadata) {
|
||||||
Font font;
|
Font font;
|
||||||
if (!ReadFont(data, length, &font)) {
|
if (!ReadFont(data, length, &font)) {
|
||||||
fprintf(stderr, "Parsing of the input font failed.\n");
|
fprintf(stderr, "Parsing of the input font failed.\n");
|
||||||
@@ -187,7 +208,7 @@ bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
|
|||||||
// the size. If the compressor overflows this, it should return false and
|
// the size. If the compressor overflows this, it should return false and
|
||||||
// then this function will also return false.
|
// then this function will also return false.
|
||||||
size_t total_transform_length = ComputeTotalTransformLength(font);
|
size_t total_transform_length = ComputeTotalTransformLength(font);
|
||||||
size_t compression_buffer_size = 1.2 * total_transform_length + 10240;
|
size_t compression_buffer_size = CompressedBufferSize(total_transform_length);
|
||||||
std::vector<uint8_t> compression_buf(compression_buffer_size);
|
std::vector<uint8_t> compression_buf(compression_buffer_size);
|
||||||
uint32_t total_compressed_length = compression_buffer_size;
|
uint32_t total_compressed_length = compression_buffer_size;
|
||||||
|
|
||||||
@@ -209,6 +230,23 @@ bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Compress the extended metadata
|
||||||
|
uint32_t compressed_metadata_buf_length =
|
||||||
|
CompressedBufferSize(extended_metadata.length());
|
||||||
|
std::vector<uint8_t> compressed_metadata_buf(compressed_metadata_buf_length);
|
||||||
|
|
||||||
|
if (extended_metadata.length() > 0) {
|
||||||
|
if (!Woff2Compress((const uint8_t*)extended_metadata.data(),
|
||||||
|
extended_metadata.length(),
|
||||||
|
compressed_metadata_buf.data(),
|
||||||
|
&compressed_metadata_buf_length)) {
|
||||||
|
fprintf(stderr, "Compression of extended metadata failed.\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
compressed_metadata_buf_length = 0;
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<Table> tables;
|
std::vector<Table> tables;
|
||||||
for (const auto& i : font.tables) {
|
for (const auto& i : font.tables) {
|
||||||
const Font::Table& src_table = i.second;
|
const Font::Table& src_table = i.second;
|
||||||
@@ -241,7 +279,8 @@ bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
|
|||||||
tables.push_back(table);
|
tables.push_back(table);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t woff2_length = ComputeWoff2Length(tables);
|
size_t woff2_length =
|
||||||
|
ComputeWoff2Length(tables, compressed_metadata_buf_length);
|
||||||
if (woff2_length > *result_length) {
|
if (woff2_length > *result_length) {
|
||||||
fprintf(stderr, "Result allocation was too small (%zd vs %zd bytes).\n",
|
fprintf(stderr, "Result allocation was too small (%zd vs %zd bytes).\n",
|
||||||
*result_length, woff2_length);
|
*result_length, woff2_length);
|
||||||
@@ -258,9 +297,16 @@ bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
|
|||||||
StoreU32(ComputeTTFLength(tables), &offset, result);
|
StoreU32(ComputeTTFLength(tables), &offset, result);
|
||||||
StoreU32(total_compressed_length, &offset, result);
|
StoreU32(total_compressed_length, &offset, result);
|
||||||
StoreBytes(head_table->data + 4, 4, &offset, result); // font revision
|
StoreBytes(head_table->data + 4, 4, &offset, result); // font revision
|
||||||
StoreU32(0, &offset, result); // metaOffset
|
if (compressed_metadata_buf_length > 0) {
|
||||||
StoreU32(0, &offset, result); // metaLength
|
StoreU32(woff2_length - compressed_metadata_buf_length,
|
||||||
StoreU32(0, &offset, result); // metaOrigLength
|
&offset, result); // metaOffset
|
||||||
|
StoreU32(compressed_metadata_buf_length, &offset, result); // metaLength
|
||||||
|
StoreU32(extended_metadata.length(), &offset, result); // metaOrigLength
|
||||||
|
} else {
|
||||||
|
StoreU32(0, &offset, result); // metaOffset
|
||||||
|
StoreU32(0, &offset, result); // metaLength
|
||||||
|
StoreU32(0, &offset, result); // metaOrigLength
|
||||||
|
}
|
||||||
StoreU32(0, &offset, result); // privOffset
|
StoreU32(0, &offset, result); // privOffset
|
||||||
StoreU32(0, &offset, result); // privLength
|
StoreU32(0, &offset, result); // privLength
|
||||||
for (const auto& table : tables) {
|
for (const auto& table : tables) {
|
||||||
@@ -270,6 +316,9 @@ bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
|
|||||||
StoreBytes(table.dst_data, table.dst_length, &offset, result);
|
StoreBytes(table.dst_data, table.dst_length, &offset, result);
|
||||||
offset = Round4(offset);
|
offset = Round4(offset);
|
||||||
}
|
}
|
||||||
|
StoreBytes(compressed_metadata_buf.data(), compressed_metadata_buf_length,
|
||||||
|
&offset, result);
|
||||||
|
|
||||||
if (*result_length != offset) {
|
if (*result_length != offset) {
|
||||||
fprintf(stderr, "Mismatch between computed and actual length "
|
fprintf(stderr, "Mismatch between computed and actual length "
|
||||||
"(%zd vs %zd)\n", *result_length, offset);
|
"(%zd vs %zd)\n", *result_length, offset);
|
||||||
|
|||||||
@@ -19,11 +19,17 @@
|
|||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using std::string;
|
||||||
|
|
||||||
|
|
||||||
namespace woff2 {
|
namespace woff2 {
|
||||||
|
|
||||||
// Returns an upper bound on the size of the compressed file.
|
// Returns an upper bound on the size of the compressed file.
|
||||||
size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length);
|
size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length);
|
||||||
|
size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length,
|
||||||
|
const string& extended_metadata);
|
||||||
|
|
||||||
// Compresses the font into the target buffer. *result_length should be at least
|
// Compresses the font into the target buffer. *result_length should be at least
|
||||||
// the value returned by MaxWOFF2CompressedSize(), upon return, it is set to the
|
// the value returned by MaxWOFF2CompressedSize(), upon return, it is set to the
|
||||||
@@ -31,6 +37,10 @@ size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length);
|
|||||||
bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
|
bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
|
||||||
uint8_t *result, size_t *result_length);
|
uint8_t *result, size_t *result_length);
|
||||||
|
|
||||||
|
bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
|
||||||
|
uint8_t *result, size_t *result_length,
|
||||||
|
const string& extended_metadata);
|
||||||
|
|
||||||
} // namespace woff2
|
} // namespace woff2
|
||||||
|
|
||||||
#endif // WOFF2_WOFF2_ENC_H_
|
#endif // WOFF2_WOFF2_ENC_H_
|
||||||
|
|||||||
Reference in New Issue
Block a user