diff options
-rw-r--r-- | brotli/enc/encode.cc | 2 | ||||
-rw-r--r-- | woff2/Makefile | 3 | ||||
-rw-r--r-- | woff2/buffer.h (renamed from woff2/ots.h) | 54 | ||||
-rw-r--r-- | woff2/font.cc | 41 | ||||
-rw-r--r-- | woff2/font.h | 8 | ||||
-rw-r--r-- | woff2/glyph.cc | 62 | ||||
-rw-r--r-- | woff2/normalize.cc | 16 | ||||
-rw-r--r-- | woff2/table_tags.cc | 90 | ||||
-rw-r--r-- | woff2/table_tags.h | 34 | ||||
-rw-r--r-- | woff2/transform.cc | 7 | ||||
-rw-r--r-- | woff2/woff2_common.h | 50 | ||||
-rw-r--r-- | woff2/woff2_compress.cc | 2 | ||||
-rw-r--r-- | woff2/woff2_dec.cc (renamed from woff2/woff2.cc) | 674 | ||||
-rw-r--r-- | woff2/woff2_dec.h | 36 | ||||
-rw-r--r-- | woff2/woff2_decompress.cc | 2 | ||||
-rw-r--r-- | woff2/woff2_enc.cc | 281 | ||||
-rw-r--r-- | woff2/woff2_enc.h (renamed from woff2/woff2.h) | 22 |
17 files changed, 744 insertions, 640 deletions
diff --git a/brotli/enc/encode.cc b/brotli/enc/encode.cc index c54f01d..6e18845 100644 --- a/brotli/enc/encode.cc +++ b/brotli/enc/encode.cc @@ -553,7 +553,7 @@ int BestMaxZeroRunLengthPrefix(const std::vector<int>& v) { int max_run_length_prefix = max_prefix; RunLengthCodeZeros(v, &max_run_length_prefix, &rle_symbols, &extra_bits); if (max_run_length_prefix < max_prefix) break; - HistogramLiteral histogram; + HistogramContextMap histogram; for (int i = 0; i < rle_symbols.size(); ++i) { histogram.Add(rle_symbols[i]); } diff --git a/woff2/Makefile b/woff2/Makefile index 971feac..3782048 100644 --- a/woff2/Makefile +++ b/woff2/Makefile @@ -2,7 +2,8 @@ include ../shared.mk -OUROBJ = font.o glyph.o normalize.o transform.o woff2.o +OUROBJ = font.o glyph.o normalize.o table_tags.o transform.o \ + woff2_dec.o woff2_enc.o BROTLI = ../brotli ENCOBJ = $(BROTLI)/enc/*.o diff --git a/woff2/ots.h b/woff2/buffer.h index 9cf91bc..5111c65 100644 --- a/woff2/ots.h +++ b/woff2/buffer.h @@ -15,21 +15,39 @@ // The parts of ots.h & opentype-sanitiser.h that we need, taken from the // https://code.google.com/p/ots/ project. -#ifndef WOFF2_OTS_H_ -#define WOFF2_OTS_H_ - -#include <stdint.h> +#ifndef WOFF2_BUFFER_H_ +#define WOFF2_BUFFER_H_ + +#if defined(_WIN32) +#include <stdlib.h> +typedef signed char int8_t; +typedef unsigned char uint8_t; +typedef short int16_t; +typedef unsigned short uint16_t; +typedef int int32_t; +typedef unsigned int uint32_t; +typedef __int64 int64_t; +typedef unsigned __int64 uint64_t; +#define ntohl(x) _byteswap_ulong (x) +#define ntohs(x) _byteswap_ushort (x) +#define htonl(x) _byteswap_ulong (x) +#define htons(x) _byteswap_ushort (x) +#else #include <arpa/inet.h> +#include <stdint.h> +#endif + #include <cstdlib> #include <cstring> #include <limits> -namespace ots { +namespace woff2 { -#if defined(_MSC_VER) || !defined(OTS_DEBUG) -#define OTS_FAILURE() false +#if defined(_MSC_VER) || !defined(FONT_COMPRESSION_DEBUG) +#define FONT_COMPRESSION_FAILURE() false #else -#define OTS_FAILURE() ots::Failure(__FILE__, __LINE__, __PRETTY_FUNCTION__) +#define FONT_COMPRESSION_FAILURE() \ + util::compression::font::Failure(__FILE__, __LINE__, __PRETTY_FUNCTION__) inline bool Failure(const char *f, int l, const char *fn) { std::fprintf(stderr, "ERROR at %s:%d (%s)\n", f, l, fn); std::fflush(stderr); @@ -57,11 +75,11 @@ class Buffer { bool Read(uint8_t *buffer, size_t n_bytes) { if (n_bytes > 1024 * 1024 * 1024) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if ((offset_ + n_bytes > length_) || (offset_ > length_ - n_bytes)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if (buffer) { std::memcpy(buffer, buffer_ + offset_, n_bytes); @@ -72,7 +90,7 @@ class Buffer { inline bool ReadU8(uint8_t *value) { if (offset_ + 1 > length_) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } *value = buffer_[offset_]; ++offset_; @@ -81,7 +99,7 @@ class Buffer { bool ReadU16(uint16_t *value) { if (offset_ + 2 > length_) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } std::memcpy(value, buffer_ + offset_, sizeof(uint16_t)); *value = ntohs(*value); @@ -95,7 +113,7 @@ class Buffer { bool ReadU24(uint32_t *value) { if (offset_ + 3 > length_) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } *value = static_cast<uint32_t>(buffer_[offset_]) << 16 | static_cast<uint32_t>(buffer_[offset_ + 1]) << 8 | @@ -106,7 +124,7 @@ class Buffer { bool ReadU32(uint32_t *value) { if (offset_ + 4 > length_) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } std::memcpy(value, buffer_ + offset_, sizeof(uint32_t)); *value = ntohl(*value); @@ -120,7 +138,7 @@ class Buffer { bool ReadTag(uint32_t *value) { if (offset_ + 4 > length_) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } std::memcpy(value, buffer_ + offset_, sizeof(uint32_t)); offset_ += 4; @@ -129,7 +147,7 @@ class Buffer { bool ReadR64(uint64_t *value) { if (offset_ + 8 > length_) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } std::memcpy(value, buffer_ + offset_, sizeof(uint64_t)); offset_ += 8; @@ -148,6 +166,6 @@ class Buffer { size_t offset_; }; -} // namespace ots +} // namespace woff2 -#endif // WOFF2_OTS_H_ +#endif // WOFF2_BUFFER_H_ diff --git a/woff2/font.cc b/woff2/font.cc index 2733708..c0fb206 100644 --- a/woff2/font.cc +++ b/woff2/font.cc @@ -18,9 +18,10 @@ #include <algorithm> -#include "./ots.h" +#include "./buffer.h" #include "./port.h" #include "./store_bytes.h" +#include "./table_tags.h" namespace woff2 { @@ -35,14 +36,14 @@ const Font::Table* Font::FindTable(uint32_t tag) const { } bool ReadFont(const uint8_t* data, size_t len, Font* font) { - ots::Buffer file(data, len); + Buffer file(data, len); // We don't care about the search_range, entry_selector and range_shift // fields, they will always be computed upon writing the font. if (!file.ReadU32(&font->flavor) || !file.ReadU16(&font->num_tables) || !file.Skip(6)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } std::map<uint32_t, uint32_t> intervals; @@ -52,17 +53,17 @@ bool ReadFont(const uint8_t* data, size_t len, Font* font) { !file.ReadU32(&table.checksum) || !file.ReadU32(&table.offset) || !file.ReadU32(&table.length)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if ((table.offset & 3) != 0 || table.length > len || len - table.length < table.offset) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } intervals[table.offset] = table.length; table.data = data + table.offset; if (font->tables.find(table.tag) != font->tables.end()) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } font->tables[table.tag] = table; } @@ -71,7 +72,7 @@ bool ReadFont(const uint8_t* data, size_t len, Font* font) { uint32_t last_offset = 12UL + 16UL * font->num_tables; for (const auto& i : intervals) { if (i.first < last_offset || i.first + i.second < i.first) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } last_offset = i.first + i.second; } @@ -91,7 +92,7 @@ size_t FontFileSize(const Font& font) { bool WriteFont(const Font& font, uint8_t* dst, size_t dst_size) { if (dst_size < 12ULL + 16ULL * font.num_tables) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } size_t offset = 0; StoreU32(font.flavor, &offset, dst); @@ -110,13 +111,13 @@ bool WriteFont(const Font& font, uint8_t* dst, size_t dst_size) { StoreU32(table.length, &offset, dst); if (table.offset + table.length < table.offset || dst_size < table.offset + table.length) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } memcpy(dst + table.offset, table.data, table.length); size_t padding_size = (4 - (table.length & 3)) & 3; if (table.offset + table.length + padding_size < padding_size || dst_size < table.offset + table.length + padding_size) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } memset(dst + table.offset + table.length, 0, padding_size); } @@ -136,17 +137,17 @@ int NumGlyphs(const Font& font) { bool GetGlyphData(const Font& font, int glyph_index, const uint8_t** glyph_data, size_t* glyph_size) { if (glyph_index < 0) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } const Font::Table* head_table = font.FindTable(kHeadTableTag); const Font::Table* loca_table = font.FindTable(kLocaTableTag); const Font::Table* glyf_table = font.FindTable(kGlyfTableTag); if (head_table == NULL || loca_table == NULL || glyf_table == NULL || head_table->length < 52) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } int index_fmt = head_table->data[51]; - ots::Buffer loca_buf(loca_table->data, loca_table->length); + Buffer loca_buf(loca_table->data, loca_table->length); if (index_fmt == 0) { uint16_t offset1, offset2; if (!loca_buf.Skip(2 * glyph_index) || @@ -154,7 +155,7 @@ bool GetGlyphData(const Font& font, int glyph_index, !loca_buf.ReadU16(&offset2) || offset2 < offset1 || 2 * offset2 > glyf_table->length) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } *glyph_data = glyf_table->data + 2 * offset1; *glyph_size = 2 * (offset2 - offset1); @@ -165,7 +166,7 @@ bool GetGlyphData(const Font& font, int glyph_index, !loca_buf.ReadU32(&offset2) || offset2 < offset1 || offset2 > glyf_table->length) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } *glyph_data = glyf_table->data + offset1; *glyph_size = offset2 - offset1; @@ -173,4 +174,14 @@ bool GetGlyphData(const Font& font, int glyph_index, return true; } +bool RemoveDigitalSignature(Font* font) { + std::map<uint32_t, Font::Table>::iterator it = + font->tables.find(kDsigTableTag); + if (it != font->tables.end()) { + font->tables.erase(it); + font->num_tables = font->tables.size(); + } + return true; +} + } // namespace woff2 diff --git a/woff2/font.h b/woff2/font.h index dd003fb..01da720 100644 --- a/woff2/font.h +++ b/woff2/font.h @@ -25,11 +25,6 @@ namespace woff2 { -// Tags of popular tables. -static const uint32_t kGlyfTableTag = 0x676c7966; -static const uint32_t kHeadTableTag = 0x68656164; -static const uint32_t kLocaTableTag = 0x6c6f6361; - // Represents an sfnt font file. Only the table directory is parsed, for the // table data we only store a raw pointer, therefore a font object is valid only // as long the data from which it was parsed is around. @@ -76,6 +71,9 @@ int NumGlyphs(const Font& font); bool GetGlyphData(const Font& font, int glyph_index, const uint8_t** glyph_data, size_t* glyph_size); +// Removes the digital signature (DSIG) table +bool RemoveDigitalSignature(Font* font); + } // namespace woff2 #endif // WOFF2_FONT_H_ diff --git a/woff2/glyph.cc b/woff2/glyph.cc index 4752e09..4cef0d9 100644 --- a/woff2/glyph.cc +++ b/woff2/glyph.cc @@ -18,7 +18,7 @@ #include <stdlib.h> #include <limits> -#include "./ots.h" +#include "./buffer.h" #include "./store_bytes.h" namespace woff2 { @@ -36,14 +36,14 @@ static const int32_t kFLAG_WE_HAVE_AN_X_AND_Y_SCALE = 1 << 6; static const int32_t kFLAG_WE_HAVE_A_TWO_BY_TWO = 1 << 7; static const int32_t kFLAG_WE_HAVE_INSTRUCTIONS = 1 << 8; -bool ReadCompositeGlyphData(ots::Buffer* buffer, Glyph* glyph) { +bool ReadCompositeGlyphData(Buffer* buffer, Glyph* glyph) { glyph->have_instructions = false; glyph->composite_data = buffer->buffer() + buffer->offset(); size_t start_offset = buffer->offset(); uint16_t flags = kFLAG_MORE_COMPONENTS; while (flags & kFLAG_MORE_COMPONENTS) { if (!buffer->ReadU16(&flags)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } glyph->have_instructions |= (flags & kFLAG_WE_HAVE_INSTRUCTIONS) != 0; size_t arg_size = 2; // glyph index @@ -60,22 +60,22 @@ bool ReadCompositeGlyphData(ots::Buffer* buffer, Glyph* glyph) { arg_size += 8; } if (!buffer->Skip(arg_size)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } } if (buffer->offset() - start_offset > std::numeric_limits<uint32_t>::max()) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } glyph->composite_data_size = buffer->offset() - start_offset; return true; } bool ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) { - ots::Buffer buffer(data, len); + Buffer buffer(data, len); int16_t num_contours; if (!buffer.ReadS16(&num_contours)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if (num_contours == 0) { @@ -88,7 +88,7 @@ bool ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) { !buffer.ReadS16(&glyph->y_min) || !buffer.ReadS16(&glyph->x_max) || !buffer.ReadS16(&glyph->y_max)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if (num_contours > 0) { @@ -100,7 +100,7 @@ bool ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) { for (int i = 0; i < num_contours; ++i) { uint16_t point_index; if (!buffer.ReadU16(&point_index)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } uint16_t num_points = point_index - last_point_index + (i == 0 ? 1 : 0); glyph->contours[i].resize(num_points); @@ -109,11 +109,11 @@ bool ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) { // Read the instructions. if (!buffer.ReadU16(&glyph->instructions_size)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } glyph->instructions_data = data + buffer.offset(); if (!buffer.Skip(glyph->instructions_size)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } // Read the run-length coded flags. @@ -125,11 +125,11 @@ bool ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) { for (int j = 0; j < glyph->contours[i].size(); ++j) { if (flag_repeat == 0) { if (!buffer.ReadU8(&flag)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if (flag & kFLAG_REPEAT) { if (!buffer.ReadU8(&flag_repeat)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } } } else { @@ -149,7 +149,7 @@ bool ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) { // single byte x-delta coord value uint8_t x_delta; if (!buffer.ReadU8(&x_delta)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } int sign = (flag & kFLAG_XREPEATSIGN) ? 1 : -1; glyph->contours[i][j].x = prev_x + sign * x_delta; @@ -158,7 +158,7 @@ bool ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) { int16_t x_delta = 0; if (!(flag & kFLAG_XREPEATSIGN)) { if (!buffer.ReadS16(&x_delta)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } } glyph->contours[i][j].x = prev_x + x_delta; @@ -176,7 +176,7 @@ bool ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) { // single byte y-delta coord value uint8_t y_delta; if (!buffer.ReadU8(&y_delta)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } int sign = (flag & kFLAG_YREPEATSIGN) ? 1 : -1; glyph->contours[i][j].y = prev_y + sign * y_delta; @@ -185,7 +185,7 @@ bool ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) { int16_t y_delta = 0; if (!(flag & kFLAG_YREPEATSIGN)) { if (!buffer.ReadS16(&y_delta)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } } glyph->contours[i][j].y = prev_y + y_delta; @@ -196,22 +196,22 @@ bool ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) { } else if (num_contours == -1) { // Composite glyph. if (!ReadCompositeGlyphData(&buffer, glyph)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } // Read the instructions. if (glyph->have_instructions) { if (!buffer.ReadU16(&glyph->instructions_size)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } glyph->instructions_data = data + buffer.offset(); if (!buffer.Skip(glyph->instructions_size)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } } else { glyph->instructions_size = 0; } } else { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } return true; } @@ -236,7 +236,7 @@ bool StoreEndPtsOfContours(const Glyph& glyph, size_t* offset, uint8_t* dst) { end_point += contour.size(); if (contour.size() > std::numeric_limits<uint16_t>::max() || end_point > std::numeric_limits<uint16_t>::max()) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } Store16(end_point, offset, dst); } @@ -280,12 +280,12 @@ bool StorePoints(const Glyph& glyph, size_t* offset, } else { if (repeat_count != 0) { if (*offset >= dst_size) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } dst[(*offset)++] = repeat_count; } if (*offset >= dst_size) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } dst[(*offset)++] = flag; repeat_count = 0; @@ -297,13 +297,13 @@ bool StorePoints(const Glyph& glyph, size_t* offset, } if (repeat_count != 0) { if (*offset >= dst_size) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } dst[(*offset)++] = repeat_count; } if (*offset + x_bytes + y_bytes > dst_size) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } // Store the x and y coordinates. @@ -346,7 +346,7 @@ bool StoreGlyph(const Glyph& glyph, uint8_t* dst, size_t* dst_size) { if (*dst_size < ((10ULL + glyph.composite_data_size) + ((glyph.have_instructions ? 2ULL : 0) + glyph.instructions_size))) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } Store16(-1, &offset, dst); StoreBbox(glyph, &offset, dst); @@ -357,20 +357,20 @@ bool StoreGlyph(const Glyph& glyph, uint8_t* dst, size_t* dst_size) { } else if (glyph.contours.size() > 0) { // Simple glyph. if (glyph.contours.size() > std::numeric_limits<int16_t>::max()) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if (*dst_size < ((12ULL + 2 * glyph.contours.size()) + glyph.instructions_size)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } Store16(glyph.contours.size(), &offset, dst); StoreBbox(glyph, &offset, dst); if (!StoreEndPtsOfContours(glyph, &offset, dst)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } StoreInstructions(glyph, &offset, dst); if (!StorePoints(glyph, &offset, dst, *dst_size)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } } *dst_size = offset; diff --git a/woff2/normalize.cc b/woff2/normalize.cc index ef9f158..0812b00 100644 --- a/woff2/normalize.cc +++ b/woff2/normalize.cc @@ -19,12 +19,13 @@ #include <inttypes.h> #include <stddef.h> -#include "./ots.h" +#include "./buffer.h" #include "./port.h" #include "./font.h" #include "./glyph.h" #include "./round.h" #include "./store_bytes.h" +#include "./table_tags.h" namespace woff2 { @@ -67,7 +68,7 @@ bool NormalizeGlyphs(Font* font) { Font::Table* glyf_table = font->FindTable(kGlyfTableTag); Font::Table* loca_table = font->FindTable(kLocaTableTag); if (head_table == NULL || loca_table == NULL || glyf_table == NULL) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } int index_fmt = head_table->data[51]; int num_glyphs = NumGlyphs(*font); @@ -97,18 +98,18 @@ bool NormalizeGlyphs(Font* font) { size_t glyph_size; if (!GetGlyphData(*font, i, &glyph_data, &glyph_size) || (glyph_size > 0 && !ReadGlyph(glyph_data, glyph_size, &glyph))) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } NormalizeSimpleGlyphBoundingBox(&glyph); size_t glyf_dst_size = glyf_table->buffer.size() - glyf_offset; if (!StoreGlyph(glyph, glyf_dst + glyf_offset, &glyf_dst_size)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } glyf_dst_size = Round4(glyf_dst_size); if (glyf_dst_size > std::numeric_limits<uint32_t>::max() || glyf_offset + static_cast<uint32_t>(glyf_dst_size) < glyf_offset || (index_fmt == 0 && glyf_offset + glyf_dst_size >= (1UL << 17))) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } glyf_offset += glyf_dst_size; } @@ -165,7 +166,7 @@ uint32_t ComputeHeaderChecksum(const Font& font) { bool FixChecksums(Font* font) { Font::Table* head_table = font->FindTable(kHeadTableTag); if (head_table == NULL || head_table->length < 12) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } head_table->buffer.resize(Round4(head_table->length)); uint8_t* head_buf = &head_table->buffer[0]; @@ -186,7 +187,8 @@ bool FixChecksums(Font* font) { } bool NormalizeFont(Font* font) { - return (NormalizeGlyphs(font) && + return (RemoveDigitalSignature(font) && + NormalizeGlyphs(font) && NormalizeOffsets(font) && FixChecksums(font)); } diff --git a/woff2/table_tags.cc b/woff2/table_tags.cc new file mode 100644 index 0000000..0071e00 --- /dev/null +++ b/woff2/table_tags.cc @@ -0,0 +1,90 @@ +// Copyright 2014 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Font table tags + +#include "./table_tags.h" + +namespace woff2 { + +// Note that the byte order is big-endian, not the same as ots.cc +#define TAG(a, b, c, d) ((a << 24) | (b << 16) | (c << 8) | d) + +const uint32_t kKnownTags[63] = { + TAG('c', 'm', 'a', 'p'), // 0 + TAG('h', 'e', 'a', 'd'), // 1 + TAG('h', 'h', 'e', 'a'), // 2 + TAG('h', 'm', 't', 'x'), // 3 + TAG('m', 'a', 'x', 'p'), // 4 + TAG('n', 'a', 'm', 'e'), // 5 + TAG('O', 'S', '/', '2'), // 6 + TAG('p', 'o', 's', 't'), // 7 + TAG('c', 'v', 't', ' '), // 8 + TAG('f', 'p', 'g', 'm'), // 9 + TAG('g', 'l', 'y', 'f'), // 10 + TAG('l', 'o', 'c', 'a'), // 11 + TAG('p', 'r', 'e', 'p'), // 12 + TAG('C', 'F', 'F', ' '), // 13 + TAG('V', 'O', 'R', 'G'), // 14 + TAG('E', 'B', 'D', 'T'), // 15 + TAG('E', 'B', 'L', 'C'), // 16 + TAG('g', 'a', 's', 'p'), // 17 + TAG('h', 'd', 'm', 'x'), // 18 + TAG('k', 'e', 'r', 'n'), // 19 + TAG('L', 'T', 'S', 'H'), // 20 + TAG('P', 'C', 'L', 'T'), // 21 + TAG('V', 'D', 'M', 'X'), // 22 + TAG('v', 'h', 'e', 'a'), // 23 + TAG('v', 'm', 't', 'x'), // 24 + TAG('B', 'A', 'S', 'E'), // 25 + TAG('G', 'D', 'E', 'F'), // 26 + TAG('G', 'P', 'O', 'S'), // 27 + TAG('G', 'S', 'U', 'B'), // 28 + TAG('E', 'B', 'S', 'C'), // 29 + TAG('J', 'S', 'T', 'F'), // 30 + TAG('M', 'A', 'T', 'H'), // 31 + TAG('C', 'B', 'D', 'T'), // 32 + TAG('C', 'B', 'L', 'C'), // 33 + TAG('C', 'O', 'L', 'R'), // 34 + TAG('C', 'P', 'A', 'L'), // 35 + TAG('S', 'V', 'G', ' '), // 36 + TAG('s', 'b', 'i', 'x'), // 37 + TAG('a', 'c', 'n', 't'), // 38 + TAG('a', 'v', 'a', 'r'), // 39 + TAG('b', 'd', 'a', 't'), // 40 + TAG('b', 'l', 'o', 'c'), // 41 + TAG('b', 's', 'l', 'n'), // 42 + TAG('c', 'v', 'a', 'r'), // 43 + TAG('f', 'd', 's', 'c'), // 44 + TAG('f', 'e', 'a', 't'), // 45 + TAG('f', 'm', 't', 'x'), // 46 + TAG('f', 'v', 'a', 'r'), // 47 + TAG('g', 'v', 'a', 'r'), // 48 + TAG('h', 's', 't', 'y'), // 49 + TAG('j', 'u', 's', 't'), // 50 + TAG('l', 'c', 'a', 'r'), // 51 + TAG('m', 'o', 'r', 't'), // 52 + TAG('m', 'o', 'r', 'x'), // 53 + TAG('o', 'p', 'b', 'd'), // 54 + TAG('p', 'r', 'o', 'p'), // 55 + TAG('t', 'r', 'a', 'k'), // 56 + TAG('Z', 'a', 'p', 'f'), // 57 + TAG('S', 'i', 'l', 'f'), // 58 + TAG('G', 'l', 'a', 't'), // 59 + TAG('G', 'l', 'o', 'c'), // 60 + TAG('F', 'e', 'a', 't'), // 61 + TAG('S', 'i', 'l', 'l'), // 62 +}; + +} // namespace woff2 diff --git a/woff2/table_tags.h b/woff2/table_tags.h new file mode 100644 index 0000000..f1b219c --- /dev/null +++ b/woff2/table_tags.h @@ -0,0 +1,34 @@ +// Copyright 2014 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Font table tags + +#ifndef WOFF2_TABLE_TAGS_H_ +#define WOFF2_TABLE_TAGS_H_ + +#include <inttypes.h> + +namespace woff2 { + +// Tags of popular tables. +static const uint32_t kGlyfTableTag = 0x676c7966; +static const uint32_t kHeadTableTag = 0x68656164; +static const uint32_t kLocaTableTag = 0x6c6f6361; +static const uint32_t kDsigTableTag = 0x44534947; + +extern const uint32_t kKnownTags[]; + +} // namespace woff2 + +#endif // WOFF2_TABLE_TAGS_H_ diff --git a/woff2/transform.cc b/woff2/transform.cc index a218ed1..bb3a27a 100644 --- a/woff2/transform.cc +++ b/woff2/transform.cc @@ -18,9 +18,10 @@ #include <complex> // for std::abs -#include "./ots.h" +#include "./buffer.h" #include "./font.h" #include "./glyph.h" +#include "./table_tags.h" namespace woff2 { @@ -237,7 +238,7 @@ bool TransformGlyfAndLocaTables(Font* font) { size_t glyph_size; if (!GetGlyphData(*font, i, &glyph_data, &glyph_size) || (glyph_size > 0 && !ReadGlyph(glyph_data, glyph_size, &glyph))) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } encoder.Encode(i, glyph); } @@ -245,7 +246,7 @@ bool TransformGlyfAndLocaTables(Font* font) { const Font::Table* head_table = font->FindTable(kHeadTableTag); if (head_table == NULL || head_table->length < 52) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } transformed_glyf->buffer[7] = head_table->data[51]; // index_format diff --git a/woff2/woff2_common.h b/woff2/woff2_common.h new file mode 100644 index 0000000..c03d5d5 --- /dev/null +++ b/woff2/woff2_common.h @@ -0,0 +1,50 @@ +// Copyright 2014 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Common definition for WOFF2 encoding/decoding + +#ifndef WOFF2_WOFF2_COMMON_H_ +#define WOFF2_WOFF2_COMMON_H_ + +#include <inttypes.h> + +namespace woff2 { + +static const uint32_t kWoff2Signature = 0x774f4632; // "wOF2" + +const unsigned int kWoff2FlagsContinueStream = 1 << 4; +const unsigned int kWoff2FlagsTransform = 1 << 5; + +struct Point { + int x; + int y; + bool on_curve; +}; + +struct Table { + uint32_t tag; + uint32_t flags; + uint32_t src_offset; + uint32_t src_length; + + uint32_t transform_length; + + uint32_t dst_offset; + uint32_t dst_length; + const uint8_t* dst_data; +}; + +} // namespace woff2 + +#endif // WOFF2_WOFF2_COMMON_H_ diff --git a/woff2/woff2_compress.cc b/woff2/woff2_compress.cc index 778369b..057566f 100644 --- a/woff2/woff2_compress.cc +++ b/woff2/woff2_compress.cc @@ -17,7 +17,7 @@ #include <string> #include "file.h" -#include "./woff2.h" +#include "./woff2_enc.h" int main(int argc, char **argv) { diff --git a/woff2/woff2.cc b/woff2/woff2_dec.cc index 1625c00..fda5d85 100644 --- a/woff2/woff2.cc +++ b/woff2/woff2_dec.cc @@ -1,4 +1,4 @@ -// Copyright 2013 Google Inc. All Rights Reserved. +// Copyright 2014 Google Inc. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ // // Library for converting WOFF2 format font files to their TTF versions. -#include "./woff2.h" +#include "./woff2_dec.h" #include <stdlib.h> #include <complex> @@ -23,14 +23,12 @@ #include <string> #include <vector> -#include "./ots.h" +#include "./buffer.h" #include "./decode.h" -#include "./encode.h" -#include "./font.h" -#include "./normalize.h" #include "./round.h" #include "./store_bytes.h" -#include "./transform.h" +#include "./table_tags.h" +#include "./woff2_common.h" namespace woff2 { @@ -49,19 +47,13 @@ const int kGlyfThisXIsSame = 1 << 4; const int kGlyfThisYIsSame = 1 << 5; // composite glyph flags +// See CompositeGlyph.java in sfntly for full definitions const int FLAG_ARG_1_AND_2_ARE_WORDS = 1 << 0; -const int FLAG_ARGS_ARE_XY_VALUES = 1 << 1; -const int FLAG_ROUND_XY_TO_GRID = 1 << 2; const int FLAG_WE_HAVE_A_SCALE = 1 << 3; -const int FLAG_RESERVED = 1 << 4; const int FLAG_MORE_COMPONENTS = 1 << 5; const int FLAG_WE_HAVE_AN_X_AND_Y_SCALE = 1 << 6; const int FLAG_WE_HAVE_A_TWO_BY_TWO = 1 << 7; const int FLAG_WE_HAVE_INSTRUCTIONS = 1 << 8; -const int FLAG_USE_MY_METRICS = 1 << 9; -const int FLAG_OVERLAP_COMPOUND = 1 << 10; -const int FLAG_SCALED_COMPONENT_OFFSET = 1 << 11; -const int FLAG_UNSCALED_COMPONENT_OFFSET = 1 << 12; const size_t kSfntHeaderSize = 12; const size_t kSfntEntrySize = 16; @@ -70,76 +62,34 @@ const size_t kCheckSumAdjustmentOffset = 8; const size_t kEndPtsOfContoursOffset = 10; const size_t kCompositeGlyphBegin = 10; -// Note that the byte order is big-endian, not the same as ots.cc -#define TAG(a, b, c, d) ((a << 24) | (b << 16) | (c << 8) | d) - -const uint32_t kWoff2Signature = 0x774f4632; // "wOF2" - -const unsigned int kWoff2FlagsContinueStream = 1 << 4; -const unsigned int kWoff2FlagsTransform = 1 << 5; - -const size_t kWoff2HeaderSize = 44; -const size_t kWoff2EntrySize = 20; - -const size_t kLzmaHeaderSize = 13; - -// Compression type values common to both short and long formats -const uint32_t kCompressionTypeMask = 0xf; -const uint32_t kCompressionTypeNone = 0; -const uint32_t kCompressionTypeGzip = 1; -const uint32_t kCompressionTypeBrotli = 2; - -// This is a special value for the short format only, as described in -// "Design for compressed header format" in draft doc. -const uint32_t kShortFlagsContinue = 3; - -struct Point { - int x; - int y; - bool on_curve; -}; - -struct Table { - uint32_t tag; - uint32_t flags; - uint32_t src_offset; - uint32_t src_length; - - uint32_t transform_length; - - uint32_t dst_offset; - uint32_t dst_length; - const uint8_t* dst_data; -}; - // Based on section 6.1.1 of MicroType Express draft spec -bool Read255UShort(ots::Buffer* buf, unsigned int* value) { +bool Read255UShort(Buffer* buf, unsigned int* value) { static const int kWordCode = 253; static const int kOneMoreByteCode2 = 254; static const int kOneMoreByteCode1 = 255; static const int kLowestUCode = 253; uint8_t code = 0; if (!buf->ReadU8(&code)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if (code == kWordCode) { uint16_t result = 0; if (!buf->ReadU16(&result)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } *value = result; return true; } else if (code == kOneMoreByteCode1) { uint8_t result = 0; if (!buf->ReadU8(&result)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } *value = result + kLowestUCode; return true; } else if (code == kOneMoreByteCode2) { uint8_t result = 0; if (!buf->ReadU8(&result)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } *value = result + kLowestUCode * 2; return true; @@ -149,16 +99,16 @@ bool Read255UShort(ots::Buffer* buf, unsigned int* value) { } } -bool ReadBase128(ots::Buffer* buf, uint32_t* value) { +bool ReadBase128(Buffer* buf, uint32_t* value) { uint32_t result = 0; for (size_t i = 0; i < 5; ++i) { uint8_t code = 0; if (!buf->ReadU8(&code)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } // If any of the top seven bits are set then we're about to overflow. if (result & 0xe0000000) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } result = (result << 7) | (code & 0x7f); if ((code & 0x80) == 0) { @@ -167,24 +117,7 @@ bool ReadBase128(ots::Buffer* buf, uint32_t* value) { } } // Make sure not to exceed the size bound - return OTS_FAILURE(); -} - -size_t Base128Size(size_t n) { - size_t size = 1; - for (; n >= 128; n >>= 7) ++size; - return size; -} - -void StoreBase128(size_t len, size_t* offset, uint8_t* dst) { - size_t size = Base128Size(len); - for (int i = 0; i < size; ++i) { - int b = (int)(len >> (7 * (size - i - 1))) & 0x7f; - if (i < size - 1) { - b |= 0x80; - } - dst[(*offset)++] = b; - } + return FONT_COMPRESSION_FAILURE(); } int WithSign(int flag, int baseval) { @@ -199,7 +132,7 @@ bool TripletDecode(const uint8_t* flags_in, const uint8_t* in, size_t in_size, int y = 0; if (n_points > in_size) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } unsigned int triplet_index = 0; @@ -219,7 +152,7 @@ bool TripletDecode(const uint8_t* flags_in, const uint8_t* in, size_t in_size, } if (triplet_index + n_data_bytes > in_size || triplet_index + n_data_bytes < triplet_index) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } int dx, dy; if (flag < 10) { @@ -305,12 +238,12 @@ bool StorePoints(const std::vector<Point>& points, } else { if (repeat_count != 0) { if (flag_offset >= dst_size) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } dst[flag_offset++] = repeat_count; } if (flag_offset >= dst_size) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } dst[flag_offset++] = flag; repeat_count = 0; @@ -322,7 +255,7 @@ bool StorePoints(const std::vector<Point>& points, if (repeat_count != 0) { if (flag_offset >= dst_size) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } dst[flag_offset++] = repeat_count; } @@ -330,7 +263,7 @@ bool StorePoints(const std::vector<Point>& points, if (xy_bytes < x_bytes || flag_offset + xy_bytes < flag_offset || flag_offset + xy_bytes > dst_size) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } int x_offset = flag_offset; @@ -387,37 +320,37 @@ void ComputeBbox(const std::vector<Point>& points, uint8_t* dst) { // Process entire bbox stream. This is done as a separate pass to allow for // composite bbox computations (an optional more aggressive transform). -bool ProcessBboxStream(ots::Buffer* bbox_stream, unsigned int n_glyphs, +bool ProcessBboxStream(Buffer* bbox_stream, unsigned int n_glyphs, const std::vector<uint32_t>& loca_values, uint8_t* glyf_buf, size_t glyf_buf_length) { const uint8_t* buf = bbox_stream->buffer(); if (n_glyphs >= 65536 || loca_values.size() != n_glyphs + 1) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } // Safe because n_glyphs is bounded unsigned int bitmap_length = ((n_glyphs + 31) >> 5) << 2; if (!bbox_stream->Skip(bitmap_length)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } for (unsigned int i = 0; i < n_glyphs; ++i) { if (buf[i >> 3] & (0x80 >> (i & 7))) { uint32_t loca_offset = loca_values[i]; if (loca_values[i + 1] - loca_offset < kEndPtsOfContoursOffset) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if (glyf_buf_length < 2 + 10 || loca_offset > glyf_buf_length - 2 - 10) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if (!bbox_stream->Read(glyf_buf + loca_offset + 2, 8)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } } } return true; } -bool ProcessComposite(ots::Buffer* composite_stream, uint8_t* dst, +bool ProcessComposite(Buffer* composite_stream, uint8_t* dst, size_t dst_size, size_t* glyph_size, bool* have_instructions) { size_t start_offset = composite_stream->offset(); bool we_have_instructions = false; @@ -425,7 +358,7 @@ bool ProcessComposite(ots::Buffer* composite_stream, uint8_t* dst, uint16_t flags = FLAG_MORE_COMPONENTS; while (flags & FLAG_MORE_COMPONENTS) { if (!composite_stream->ReadU16(&flags)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } we_have_instructions |= (flags & FLAG_WE_HAVE_INSTRUCTIONS) != 0; size_t arg_size = 2; // glyph index @@ -442,12 +375,12 @@ bool ProcessComposite(ots::Buffer* composite_stream, uint8_t* dst, arg_size += 8; } if (!composite_stream->Skip(arg_size)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } } size_t composite_glyph_size = composite_stream->offset() - start_offset; if (composite_glyph_size + kCompositeGlyphBegin > dst_size) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } Store16(dst, 0, 0xffff); // nContours = -1 for composite glyph std::memcpy(dst + kCompositeGlyphBegin, @@ -464,10 +397,10 @@ bool StoreLoca(const std::vector<uint32_t>& loca_values, int index_format, const uint64_t loca_size = loca_values.size(); const uint64_t offset_size = index_format ? 4 : 2; if ((loca_size << 2) >> 2 != loca_size) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if (offset_size * loca_size > dst_size) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } size_t offset = 0; for (size_t i = 0; i < loca_values.size(); ++i) { @@ -486,42 +419,42 @@ bool ReconstructGlyf(const uint8_t* data, size_t data_size, uint8_t* dst, size_t dst_size, uint8_t* loca_buf, size_t loca_size) { static const int kNumSubStreams = 7; - ots::Buffer file(data, data_size); + Buffer file(data, data_size); uint32_t version; std::vector<std::pair<const uint8_t*, size_t> > substreams(kNumSubStreams); if (!file.ReadU32(&version)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } uint16_t num_glyphs; uint16_t index_format; if (!file.ReadU16(&num_glyphs) || !file.ReadU16(&index_format)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } unsigned int offset = (2 + kNumSubStreams) * 4; if (offset > data_size) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } // Invariant from here on: data_size >= offset for (int i = 0; i < kNumSubStreams; ++i) { uint32_t substream_size; if (!file.ReadU32(&substream_size)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if (substream_size > data_size - offset) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } substreams[i] = std::make_pair(data + offset, substream_size); offset += substream_size; } - ots::Buffer n_contour_stream(substreams[0].first, substreams[0].second); - ots::Buffer n_points_stream(substreams[1].first, substreams[1].second); - ots::Buffer flag_stream(substreams[2].first, substreams[2].second); - ots::Buffer glyph_stream(substreams[3].first, substreams[3].second); - ots::Buffer composite_stream(substreams[4].first, substreams[4].second); - ots::Buffer bbox_stream(substreams[5].first, substreams[5].second); - ots::Buffer instruction_stream(substreams[6].first, substreams[6].second); + Buffer n_contour_stream(substreams[0].first, substreams[0].second); + Buffer n_points_stream(substreams[1].first, substreams[1].second); + Buffer flag_stream(substreams[2].first, substreams[2].second); + Buffer glyph_stream(substreams[3].first, substreams[3].second); + Buffer composite_stream(substreams[4].first, substreams[4].second); + Buffer bbox_stream(substreams[5].first, substreams[5].second); + Buffer instruction_stream(substreams[6].first, substreams[6].second); std::vector<uint32_t> loca_values(num_glyphs + 1); std::vector<unsigned int> n_points_vec; @@ -531,7 +464,7 @@ bool ReconstructGlyf(const uint8_t* data, size_t data_size, size_t glyph_size = 0; uint16_t n_contours = 0; if (!n_contour_stream.ReadU16(&n_contours)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } uint8_t* glyf_dst = dst + loca_offset; size_t glyf_dst_size = dst_size - loca_offset; @@ -541,19 +474,19 @@ bool ReconstructGlyf(const uint8_t* data, size_t data_size, unsigned int instruction_size = 0; if (!ProcessComposite(&composite_stream, glyf_dst, glyf_dst_size, &glyph_size, &have_instructions)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if (have_instructions) { if (!Read255UShort(&glyph_stream, &instruction_size)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if (instruction_size + 2 > glyf_dst_size - glyph_size) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } Store16(glyf_dst, glyph_size, instruction_size); if (!instruction_stream.Read(glyf_dst + glyph_size + 2, instruction_size)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } glyph_size += instruction_size + 2; } @@ -565,17 +498,17 @@ bool ReconstructGlyf(const uint8_t* data, size_t data_size, unsigned int n_points_contour; for (unsigned int j = 0; j < n_contours; ++j) { if (!Read255UShort(&n_points_stream, &n_points_contour)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } n_points_vec.push_back(n_points_contour); if (total_n_points + n_points_contour < total_n_points) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } total_n_points += n_points_contour; } unsigned int flag_size = total_n_points; if (flag_size > flag_stream.length() - flag_stream.offset()) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } const uint8_t* flags_buf = flag_stream.buffer() + flag_stream.offset(); const uint8_t* triplet_buf = glyph_stream.buffer() + @@ -584,12 +517,12 @@ bool ReconstructGlyf(const uint8_t* data, size_t data_size, size_t triplet_bytes_consumed = 0; if (!TripletDecode(flags_buf, triplet_buf, triplet_size, total_n_points, &points, &triplet_bytes_consumed)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } const uint32_t header_and_endpts_contours_size = kEndPtsOfContoursOffset + 2 * n_contours; if (glyf_dst_size < header_and_endpts_contours_size) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } Store16(glyf_dst, 0, n_contours); ComputeBbox(points, glyf_dst); @@ -598,52 +531,52 @@ bool ReconstructGlyf(const uint8_t* data, size_t data_size, for (unsigned int contour_ix = 0; contour_ix < n_contours; ++contour_ix) { end_point += n_points_vec[contour_ix]; if (end_point >= 65536) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } offset = Store16(glyf_dst, offset, end_point); } if (!flag_stream.Skip(flag_size)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if (!glyph_stream.Skip(triplet_bytes_consumed)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } unsigned int instruction_size; if (!Read255UShort(&glyph_stream, &instruction_size)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if (glyf_dst_size - header_and_endpts_contours_size < instruction_size + 2) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } uint8_t* instruction_dst = glyf_dst + header_and_endpts_contours_size; Store16(instruction_dst, 0, instruction_size); if (!instruction_stream.Read(instruction_dst + 2, instruction_size)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if (!StorePoints(points, n_contours, instruction_size, glyf_dst, glyf_dst_size, &glyph_size)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } } else { glyph_size = 0; } loca_values[i] = loca_offset; if (glyph_size + 3 < glyph_size) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } glyph_size = Round4(glyph_size); if (glyph_size > dst_size - loca_offset) { // This shouldn't happen, but this test defensively maintains the // invariant that loca_offset <= dst_size. - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } loca_offset += glyph_size; } loca_values[num_glyphs] = loca_offset; if (!ProcessBboxStream(&bbox_stream, num_glyphs, loca_values, dst, dst_size)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } return StoreLoca(loca_values, index_format, loca_buf, loca_size); } @@ -664,31 +597,31 @@ const Table* FindTable(const std::vector<Table>& tables, uint32_t tag) { bool ReconstructTransformed(const std::vector<Table>& tables, uint32_t tag, const uint8_t* transformed_buf, size_t transformed_size, uint8_t* dst, size_t dst_length) { - if (tag == TAG('g', 'l', 'y', 'f')) { + if (tag == kGlyfTableTag) { const Table* glyf_table = FindTable(tables, tag); - const Table* loca_table = FindTable(tables, TAG('l', 'o', 'c', 'a')); + const Table* loca_table = FindTable(tables, kLocaTableTag); if (glyf_table == NULL || loca_table == NULL) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if (static_cast<uint64_t>(glyf_table->dst_offset + glyf_table->dst_length) > dst_length) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if (static_cast<uint64_t>(loca_table->dst_offset + loca_table->dst_length) > dst_length) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } return ReconstructGlyf(transformed_buf, transformed_size, dst + glyf_table->dst_offset, glyf_table->dst_length, dst + loca_table->dst_offset, loca_table->dst_length); - } else if (tag == TAG('l', 'o', 'c', 'a')) { + } else if (tag == kLocaTableTag) { // processing was already done by glyf table, but validate - if (!FindTable(tables, TAG('g', 'l', 'y', 'f'))) { - return OTS_FAILURE(); + if (!FindTable(tables, kGlyfTableTag)) { + return FONT_COMPRESSION_FAILURE(); } } else { // transform for the tag is not known - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } return true; } @@ -704,10 +637,10 @@ uint32_t ComputeChecksum(const uint8_t* buf, size_t size) { } bool FixChecksums(const std::vector<Table>& tables, uint8_t* dst) { - const Table* head_table = FindTable(tables, TAG('h', 'e', 'a', 'd')); + const Table* head_table = FindTable(tables, kHeadTableTag); if (head_table == NULL || head_table->dst_length < kCheckSumAdjustmentOffset + 4) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } size_t adjustment_offset = head_table->dst_offset + kCheckSumAdjustmentOffset; StoreU32(dst, adjustment_offset, 0); @@ -728,147 +661,57 @@ bool FixChecksums(const std::vector<Table>& tables, uint8_t* dst) { return true; } -bool Woff2Compress(const uint8_t* data, const size_t len, - uint32_t compression_type, - uint8_t* result, uint32_t* result_len) { - if (compression_type == kCompressionTypeBrotli) { - size_t compressed_len = *result_len; - brotli::BrotliParams params; - params.mode = brotli::BrotliParams::MODE_FONT; - brotli::BrotliCompressBuffer(params, len, data, &compressed_len, result); - *result_len = compressed_len; - return true; - } - return false; -} - bool Woff2Uncompress(uint8_t* dst_buf, size_t dst_size, - const uint8_t* src_buf, size_t src_size, uint32_t compression_type) { - if (compression_type == kCompressionTypeBrotli) { - size_t uncompressed_size = dst_size; - int ok = BrotliDecompressBuffer(src_size, src_buf, - &uncompressed_size, dst_buf); - if (!ok || uncompressed_size != dst_size) { - return OTS_FAILURE(); - } - return true; - } - // Unknown compression type - return OTS_FAILURE(); -} - -bool ReadLongDirectory(ots::Buffer* file, std::vector<Table>* tables, - size_t num_tables) { - for (size_t i = 0; i < num_tables; ++i) { - Table* table = &(*tables)[i]; - if (!file->ReadU32(&table->tag) || - !file->ReadU32(&table->flags) || - !file->ReadU32(&table->src_length) || - !file->ReadU32(&table->transform_length) || - !file->ReadU32(&table->dst_length)) { - return OTS_FAILURE(); - } + const uint8_t* src_buf, size_t src_size) { + size_t uncompressed_size = dst_size; + int ok = BrotliDecompressBuffer(src_size, src_buf, + &uncompressed_size, dst_buf); + if (!ok || uncompressed_size != dst_size) { + return FONT_COMPRESSION_FAILURE(); } return true; } -const uint32_t known_tags[29] = { - TAG('c', 'm', 'a', 'p'), // 0 - TAG('h', 'e', 'a', 'd'), // 1 - TAG('h', 'h', 'e', 'a'), // 2 - TAG('h', 'm', 't', 'x'), // 3 - TAG('m', 'a', 'x', 'p'), // 4 - TAG('n', 'a', 'm', 'e'), // 5 - TAG('O', 'S', '/', '2'), // 6 - TAG('p', 'o', 's', 't'), // 7 - TAG('c', 'v', 't', ' '), // 8 - TAG('f', 'p', 'g', 'm'), // 9 - TAG('g', 'l', 'y', 'f'), // 10 - TAG('l', 'o', 'c', 'a'), // 11 - TAG('p', 'r', 'e', 'p'), // 12 - TAG('C', 'F', 'F', ' '), // 13 - TAG('V', 'O', 'R', 'G'), // 14 - TAG('E', 'B', 'D', 'T'), // 15 - TAG('E', 'B', 'L', 'C'), // 16 - TAG('g', 'a', 's', 'p'), // 17 - TAG('h', 'd', 'm', 'x'), // 18 - TAG('k', 'e', 'r', 'n'), // 19 - TAG('L', 'T', 'S', 'H'), // 20 - TAG('P', 'C', 'L', 'T'), // 21 - TAG('V', 'D', 'M', 'X'), // 22 - TAG('v', 'h', 'e', 'a'), // 23 - TAG('v', 'm', 't', 'x'), // 24 - TAG('B', 'A', 'S', 'E'), // 25 - TAG('G', 'D', 'E', 'F'), // 26 - TAG('G', 'P', 'O', 'S'), // 27 - TAG('G', 'S', 'U', 'B'), // 28 -}; - -int KnownTableIndex(uint32_t tag) { - for (int i = 0; i < 29; ++i) { - if (tag == known_tags[i]) return i; - } - return 31; -} - -bool ReadShortDirectory(ots::Buffer* file, std::vector<Table>* tables, +bool ReadShortDirectory(Buffer* file, std::vector<Table>* tables, size_t num_tables) { - uint32_t last_compression_type = 0; for (size_t i = 0; i < num_tables; ++i) { Table* table = &(*tables)[i]; uint8_t flag_byte; if (!file->ReadU8(&flag_byte)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } uint32_t tag; - if ((flag_byte & 0x1f) == 0x1f) { + if ((flag_byte & 0x3f) == 0x3f) { if (!file->ReadU32(&tag)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } } else { - if ((flag_byte & 0x1f) >= (sizeof(known_tags) / sizeof(known_tags[0]))) { - return OTS_FAILURE(); - } - tag = known_tags[flag_byte & 0x1f]; + tag = kKnownTags[flag_byte & 0x3f]; } - uint32_t flags = flag_byte >> 6; - if (flags == kShortFlagsContinue) { - flags = last_compression_type | kWoff2FlagsContinueStream; - } else { - if (flags == kCompressionTypeNone || - flags == kCompressionTypeGzip || - flags == kCompressionTypeBrotli) { - last_compression_type = flags; - } else { - return OTS_FAILURE(); - } + // Bits 6 and 7 are reserved and must be 0. + if ((flag_byte & 0xC0) != 0) { + return FONT_COMPRESSION_FAILURE(); } - if ((flag_byte & 0x20) != 0) { + uint32_t flags = 0; + if (i > 0) { + flags |= kWoff2FlagsContinueStream; + } + // Always transform the glyf and loca tables + if (tag == kGlyfTableTag || tag == kLocaTableTag) { flags |= kWoff2FlagsTransform; } uint32_t dst_length; if (!ReadBase128(file, &dst_length)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } uint32_t transform_length = dst_length; if ((flags & kWoff2FlagsTransform) != 0) { if (!ReadBase128(file, &transform_length)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } } - uint32_t src_length = transform_length; - if ((flag_byte >> 6) == 1 || (flag_byte >> 6) == 2) { - if (!ReadBase128(file, &src_length)) { - return OTS_FAILURE(); - } - } else if ((flag_byte >> 6) == kShortFlagsContinue) { - // The compressed data for this table is in a previuos table, so we set - // the src_length to zero. - src_length = 0; - } table->tag = tag; table->flags = flags; - table->src_length = src_length; table->transform_length = transform_length; table->dst_length = dst_length; } @@ -878,7 +721,7 @@ bool ReadShortDirectory(ots::Buffer* file, std::vector<Table>* tables, } // namespace size_t ComputeWOFF2FinalSize(const uint8_t* data, size_t length) { - ots::Buffer file(data, length); + Buffer file(data, length); uint32_t total_length; if (!file.Skip(16) || @@ -890,38 +733,46 @@ size_t ComputeWOFF2FinalSize(const uint8_t* data, size_t length) { bool ConvertWOFF2ToTTF(uint8_t* result, size_t result_length, const uint8_t* data, size_t length) { - ots::Buffer file(data, length); + Buffer file(data, length); uint32_t signature; uint32_t flavor; if (!file.ReadU32(&signature) || signature != kWoff2Signature || !file.ReadU32(&flavor)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } // TODO(user): Should call IsValidVersionTag() here. uint32_t reported_length; if (!file.ReadU32(&reported_length) || length != reported_length) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } uint16_t num_tables; if (!file.ReadU16(&num_tables) || !num_tables) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } // We don't care about these fields of the header: // uint16_t reserved // uint32_t total_sfnt_size + if (!file.Skip(6)) { + return FONT_COMPRESSION_FAILURE(); + } + uint32_t compressed_length; + if (!file.ReadU32(&compressed_length)) { + return FONT_COMPRESSION_FAILURE(); + } + // We don't care about these fields of the header: // uint16_t major_version, minor_version // uint32_t meta_offset, meta_length, meta_orig_length // uint32_t priv_offset, priv_length - if (!file.Skip(30)) { - return OTS_FAILURE(); + if (!file.Skip(24)) { + return FONT_COMPRESSION_FAILURE(); } std::vector<Table> tables(num_tables); // Note: change below to ReadLongDirectory to enable long format. if (!ReadShortDirectory(&file, &tables, num_tables)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } uint64_t src_offset = file.offset(); uint64_t dst_offset = kSfntHeaderSize + @@ -930,35 +781,35 @@ bool ConvertWOFF2ToTTF(uint8_t* result, size_t result_length, for (uint16_t i = 0; i < num_tables; ++i) { Table* table = &tables[i]; table->src_offset = src_offset; + table->src_length = (i == 0 ? compressed_length : 0); src_offset += table->src_length; if (src_offset > std::numeric_limits<uint32_t>::max()) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } src_offset = Round4(src_offset); // TODO: reconsider table->dst_offset = dst_offset; dst_offset += table->dst_length; if (dst_offset > std::numeric_limits<uint32_t>::max()) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } dst_offset = Round4(dst_offset); - if ((table->flags & kCompressionTypeMask) != kCompressionTypeNone) { - uncompressed_sum += table->src_length; - if (uncompressed_sum > std::numeric_limits<uint32_t>::max()) { - return OTS_FAILURE(); - } + + uncompressed_sum += table->src_length; + if (uncompressed_sum > std::numeric_limits<uint32_t>::max()) { + return FONT_COMPRESSION_FAILURE(); } } // Enforce same 30M limit on uncompressed tables as OTS if (uncompressed_sum > 30 * 1024 * 1024) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if (src_offset > length || dst_offset > result_length) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } const uint32_t sfnt_header_and_table_directory_size = 12 + 16 * num_tables; if (sfnt_header_and_table_directory_size > result_length) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } // Start building the font @@ -987,18 +838,11 @@ bool ConvertWOFF2ToTTF(uint8_t* result, size_t result_length, const Table* table = &tables[i]; uint32_t flags = table->flags; const uint8_t* src_buf = data + table->src_offset; - uint32_t compression_type = flags & kCompressionTypeMask; size_t transform_length = table->transform_length; if ((flags & kWoff2FlagsContinueStream) != 0) { if (!continue_valid) { - return OTS_FAILURE(); - } - } else if (compression_type == kCompressionTypeNone) { - if (transform_length != table->src_length) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } - transform_buf = src_buf; - continue_valid = false; } else if ((flags & kWoff2FlagsContinueStream) == 0) { uint64_t total_size = transform_length; for (uint16_t j = i + 1; j < num_tables; ++j) { @@ -1007,40 +851,40 @@ bool ConvertWOFF2ToTTF(uint8_t* result, size_t result_length, } total_size += tables[j].transform_length; if (total_size > std::numeric_limits<uint32_t>::max()) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } } uncompressed_buf.resize(total_size); if (!Woff2Uncompress(&uncompressed_buf[0], total_size, - src_buf, table->src_length, compression_type)) { - return OTS_FAILURE(); + src_buf, compressed_length)) { + return FONT_COMPRESSION_FAILURE(); } transform_buf = &uncompressed_buf[0]; continue_valid = true; } else { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if ((flags & kWoff2FlagsTransform) == 0) { if (transform_length != table->dst_length) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } if (static_cast<uint64_t>(table->dst_offset + transform_length) > result_length) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } std::memcpy(result + table->dst_offset, transform_buf, transform_length); } else { if (!ReconstructTransformed(tables, table->tag, transform_buf, transform_length, result, result_length)) { - return OTS_FAILURE(); + return FONT_COMPRESSION_FAILURE(); } } if (continue_valid) { transform_buf += transform_length; - if (transform_buf > uncompressed_buf.data() + uncompressed_buf.size()) { - return OTS_FAILURE(); + if (transform_buf > &uncompressed_buf[0] + uncompressed_buf.size()) { + return FONT_COMPRESSION_FAILURE(); } } } @@ -1048,252 +892,4 @@ bool ConvertWOFF2ToTTF(uint8_t* result, size_t result_length, return FixChecksums(tables, result); } -void StoreTableEntry(const Table& table, size_t* offset, uint8_t* dst) { - uint8_t flag_byte = KnownTableIndex(table.tag); - if ((table.flags & kWoff2FlagsTransform) != 0) { - flag_byte |= 0x20; - } - if ((table.flags & kWoff2FlagsContinueStream) != 0) { - flag_byte |= 0xc0; - } else { - flag_byte |= ((table.flags & 3) << 6); - } - dst[(*offset)++] = flag_byte; - if ((flag_byte & 0x1f) == 0x1f) { - StoreU32(table.tag, offset, dst); - } - StoreBase128(table.src_length, offset, dst); - if ((flag_byte & 0x20) != 0) { - StoreBase128(table.transform_length, offset, dst); - } - if ((flag_byte & 0xc0) == 0x40 || (flag_byte & 0xc0) == 0x80) { - StoreBase128(table.dst_length, offset, dst); - } -} - -size_t TableEntrySize(const Table& table) { - size_t size = KnownTableIndex(table.tag) < 31 ? 1 : 5; - size += Base128Size(table.src_length); - if ((table.flags & kWoff2FlagsTransform) != 0) { - size += Base128Size(table.transform_length); - } - if ((table.flags & kWoff2FlagsContinueStream) == 0 && - ((table.flags & 3) == kCompressionTypeGzip || - (table.flags & 3) == kCompressionTypeBrotli)) { - size += Base128Size(table.dst_length); - } - return size; -} - -size_t ComputeWoff2Length(const std::vector<Table>& tables) { - size_t size = 44; // header size - for (const auto& table : tables) { - size += TableEntrySize(table); - } - for (const auto& table : tables) { - size += table.dst_length; - size = Round4(size); - } - return size; -} - -size_t ComputeTTFLength(const std::vector<Table>& tables) { - size_t size = 12 + 16 * tables.size(); // sfnt header - for (const auto& table : tables) { - size += Round4(table.src_length); - } - return size; -} - -size_t ComputeTotalTransformLength(const Font& font) { - size_t total = 0; - for (const auto& i : font.tables) { - const Font::Table& table = i.second; - if (table.tag & 0x80808080 || !font.FindTable(table.tag ^ 0x80808080)) { - // Count transformed tables and non-transformed tables that do not have - // transformed versions. - total += table.length; - } - } - return total; -} - -struct Woff2ConvertOptions { - uint32_t compression_type; - bool continue_streams; - bool keep_dsig; - bool transform_glyf; - - Woff2ConvertOptions() - : compression_type(kCompressionTypeBrotli), - continue_streams(true), - keep_dsig(true), - transform_glyf(true) {} - - -}; - -size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length) { - // Except for the header size, which is 32 bytes larger in woff2 format, - // all other parts should be smaller (table header in short format, - // transformations and compression). Just to be sure, we will give some - // headroom anyway. - return length + 1024; -} - -bool ConvertTTFToWOFF2(const uint8_t *data, size_t length, - uint8_t *result, size_t *result_length) { - - Woff2ConvertOptions options; - - Font font; - if (!ReadFont(data, length, &font)) { - fprintf(stderr, "Parsing of the input font failed.\n"); - return false; - } - - if (!NormalizeFont(&font)) { - fprintf(stderr, "Font normalization failed.\n"); - return false; - } - - if (!options.keep_dsig) { - font.tables.erase(TAG('D', 'S', 'I', 'G')); - } - - if (options.transform_glyf && - !TransformGlyfAndLocaTables(&font)) { - fprintf(stderr, "Font transformation failed.\n"); - return false; - } - - const Font::Table* head_table = font.FindTable(kHeadTableTag); - if (head_table == NULL) { - fprintf(stderr, "Missing head table.\n"); - return false; - } - - // Although the compressed size of each table in the final woff2 file won't - // be larger than its transform_length, we have to allocate a large enough - // buffer for the compressor, since the compressor can potentially increase - // the size. If the compressor overflows this, it should return false and - // then this function will also return false. - size_t total_transform_length = ComputeTotalTransformLength(font); - size_t compression_buffer_size = 1.2 * total_transform_length + 10240; - std::vector<uint8_t> compression_buf(compression_buffer_size); - size_t compression_buf_offset = 0; - uint32_t total_compressed_length = compression_buffer_size; - - if (options.continue_streams) { - // Collect all transformed data into one place. - std::vector<uint8_t> transform_buf(total_transform_length); - size_t transform_offset = 0; - for (const auto& i : font.tables) { - if (i.second.tag & 0x80808080) continue; - const Font::Table* table = font.FindTable(i.second.tag ^ 0x80808080); - if (table == NULL) table = &i.second; - StoreBytes(table->data, table->length, - &transform_offset, &transform_buf[0]); - } - // Compress all transformed data in one stream. - if (!Woff2Compress(transform_buf.data(), total_transform_length, - options.compression_type, - &compression_buf[0], - &total_compressed_length)) { - fprintf(stderr, "Compression of combined table failed.\n"); - return false; - } - } - - std::vector<Table> tables; - for (const auto& i : font.tables) { - const Font::Table& src_table = i.second; - if (src_table.tag & 0x80808080) { - // This is a transformed table, we will write it together with the - // original version. - continue; - } - Table table; - table.tag = src_table.tag; - table.flags = options.compression_type; - table.src_length = src_table.length; - table.transform_length = src_table.length; - const uint8_t* transformed_data = src_table.data; - const Font::Table* transformed_table = - font.FindTable(src_table.tag ^ 0x80808080); - if (transformed_table != NULL) { - table.flags |= kWoff2FlagsTransform; - table.transform_length = transformed_table->length; - transformed_data = transformed_table->data; - } - if (options.continue_streams) { - if (tables.empty()) { - table.dst_length = total_compressed_length; - table.dst_data = &compression_buf[0]; - } else { - table.dst_length = 0; - table.dst_data = NULL; - table.flags |= kWoff2FlagsContinueStream; - } - } else { - table.dst_length = table.transform_length; - table.dst_data = transformed_data; - if (options.compression_type != kCompressionTypeNone) { - uint32_t compressed_length = - compression_buf.size() - compression_buf_offset; - if (!Woff2Compress(transformed_data, table.transform_length, - options.compression_type, - &compression_buf[compression_buf_offset], - &compressed_length)) { - fprintf(stderr, "Compression of table %x failed.\n", src_table.tag); - return false; - } - if (compressed_length >= table.transform_length) { - table.flags &= (~3); // no compression - } else { - table.dst_length = compressed_length; - table.dst_data = &compression_buf[compression_buf_offset]; - compression_buf_offset += table.dst_length; - } - } - } - tables.push_back(table); - } - - size_t woff2_length = ComputeWoff2Length(tables); - if (woff2_length > *result_length) { - fprintf(stderr, "Result allocation was too small (%zd vs %zd bytes).\n", - *result_length, woff2_length); - return false; - } - *result_length = woff2_length; - - size_t offset = 0; - StoreU32(kWoff2Signature, &offset, result); - StoreU32(font.flavor, &offset, result); - StoreU32(woff2_length, &offset, result); - Store16(tables.size(), &offset, result); - Store16(0, &offset, result); // reserved - StoreU32(ComputeTTFLength(tables), &offset, result); - StoreBytes(head_table->data + 4, 4, &offset, result); // font revision - StoreU32(0, &offset, result); // metaOffset - StoreU32(0, &offset, result); // metaLength - StoreU32(0, &offset, result); // metaOrigLength - StoreU32(0, &offset, result); // privOffset - StoreU32(0, &offset, result); // privLength - for (const auto& table : tables) { - StoreTableEntry(table, &offset, result); - } - for (const auto& table : tables) { - StoreBytes(table.dst_data, table.dst_length, &offset, result); - offset = Round4(offset); - } - if (*result_length != offset) { - fprintf(stderr, "Mismatch between computed and actual length " - "(%zd vs %zd)\n", *result_length, offset); - return false; - } - return true; -} - } // namespace woff2 diff --git a/woff2/woff2_dec.h b/woff2/woff2_dec.h new file mode 100644 index 0000000..74ba7f5 --- /dev/null +++ b/woff2/woff2_dec.h @@ -0,0 +1,36 @@ +// Copyright 2014 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Library for converting WOFF2 format font files to their TTF versions. + +#ifndef WOFF2_WOFF2_DEC_H_ +#define WOFF2_WOFF2_DEC_H_ + +#include <stddef.h> +#include <inttypes.h> + +namespace woff2 { + +// Compute the size of the final uncompressed font, or 0 on error. +size_t ComputeWOFF2FinalSize(const uint8_t *data, size_t length); + +// Decompresses the font into the target buffer. The result_length should +// be the same as determined by ComputeFinalSize(). Returns true on successful +// decompression. +bool ConvertWOFF2ToTTF(uint8_t *result, size_t result_length, + const uint8_t *data, size_t length); + +} // namespace woff2 + +#endif // WOFF2_WOFF2_DEC_H_ diff --git a/woff2/woff2_decompress.cc b/woff2/woff2_decompress.cc index c083793..7668c4e 100644 --- a/woff2/woff2_decompress.cc +++ b/woff2/woff2_decompress.cc @@ -19,7 +19,7 @@ #include "file.h" -#include "./woff2.h" +#include "./woff2_dec.h" int main(int argc, char **argv) { using std::string; diff --git a/woff2/woff2_enc.cc b/woff2/woff2_enc.cc new file mode 100644 index 0000000..b8afd92 --- /dev/null +++ b/woff2/woff2_enc.cc @@ -0,0 +1,281 @@ +// Copyright 2014 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Library for converting WOFF2 format font files to their TTF versions. + +#include "./woff2_enc.h" + +#include <stdlib.h> +#include <complex> +#include <cstring> +#include <limits> +#include <string> +#include <vector> + +#include "./buffer.h" +#include "./encode.h" +#include "./font.h" +#include "./normalize.h" +#include "./round.h" +#include "./store_bytes.h" +#include "./table_tags.h" +#include "./transform.h" +#include "./woff2_common.h" + +namespace woff2 { + +namespace { + +using std::string; +using std::vector; + + +const size_t kWoff2HeaderSize = 48; +const size_t kWoff2EntrySize = 20; + +size_t Base128Size(size_t n) { + size_t size = 1; + for (; n >= 128; n >>= 7) ++size; + return size; +} + +void StoreBase128(size_t len, size_t* offset, uint8_t* dst) { + size_t size = Base128Size(len); + for (int i = 0; i < size; ++i) { + int b = (int)(len >> (7 * (size - i - 1))) & 0x7f; + if (i < size - 1) { + b |= 0x80; + } + dst[(*offset)++] = b; + } +} + +bool Woff2Compress(const uint8_t* data, const size_t len, + uint8_t* result, uint32_t* result_len) { + size_t compressed_len = *result_len; + brotli::BrotliParams params; + params.mode = brotli::BrotliParams::MODE_FONT; + brotli::BrotliCompressBuffer(params, len, data, &compressed_len, result); + *result_len = compressed_len; + return true; +} + +bool ReadLongDirectory(Buffer* file, std::vector<Table>* tables, + size_t num_tables) { + for (size_t i = 0; i < num_tables; ++i) { + Table* table = &(*tables)[i]; + if (!file->ReadU32(&table->tag) || + !file->ReadU32(&table->flags) || + !file->ReadU32(&table->src_length) || + !file->ReadU32(&table->transform_length) || + !file->ReadU32(&table->dst_length)) { + return FONT_COMPRESSION_FAILURE(); + } + } + return true; +} + +int KnownTableIndex(uint32_t tag) { + for (int i = 0; i < 63; ++i) { + if (tag == kKnownTags[i]) return i; + } + return 63; +} + +void StoreTableEntry(const Table& table, size_t* offset, uint8_t* dst) { + uint8_t flag_byte = KnownTableIndex(table.tag); + dst[(*offset)++] = flag_byte; + if ((flag_byte & 0x3f) == 0x3f) { + StoreU32(table.tag, offset, dst); + } + StoreBase128(table.src_length, offset, dst); + if ((table.flags & kWoff2FlagsTransform) != 0) { + StoreBase128(table.transform_length, offset, dst); + } +} + +size_t TableEntrySize(const Table& table) { + size_t size = KnownTableIndex(table.tag) < 31 ? 1 : 5; + size += Base128Size(table.src_length); + if ((table.flags & kWoff2FlagsTransform) != 0) { + size += Base128Size(table.transform_length); + } + return size; +} + +size_t ComputeWoff2Length(const std::vector<Table>& tables) { + size_t size = kWoff2HeaderSize; + for (const auto& table : tables) { + size += TableEntrySize(table); + } + for (const auto& table : tables) { + size += table.dst_length; + size = Round4(size); + } + return size; +} + +size_t ComputeTTFLength(const std::vector<Table>& tables) { + size_t size = 12 + 16 * tables.size(); // sfnt header + for (const auto& table : tables) { + size += Round4(table.src_length); + } + return size; +} + +size_t ComputeTotalTransformLength(const Font& font) { + size_t total = 0; + for (const auto& i : font.tables) { + const Font::Table& table = i.second; + if (table.tag & 0x80808080 || !font.FindTable(table.tag ^ 0x80808080)) { + // Count transformed tables and non-transformed tables that do not have + // transformed versions. + total += table.length; + } + } + return total; +} + +} // namespace + +size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length) { + // Except for the header size, which is 32 bytes larger in woff2 format, + // all other parts should be smaller (table header in short format, + // transformations and compression). Just to be sure, we will give some + // headroom anyway. + return length + 1024; +} + +bool ConvertTTFToWOFF2(const uint8_t *data, size_t length, + uint8_t *result, size_t *result_length) { + Font font; + if (!ReadFont(data, length, &font)) { + fprintf(stderr, "Parsing of the input font failed.\n"); + return false; + } + + if (!NormalizeFont(&font)) { + fprintf(stderr, "Font normalization failed.\n"); + return false; + } + + if (!TransformGlyfAndLocaTables(&font)) { + fprintf(stderr, "Font transformation failed.\n"); + return false; + } + + const Font::Table* head_table = font.FindTable(kHeadTableTag); + if (head_table == NULL) { + fprintf(stderr, "Missing head table.\n"); + return false; + } + + // Although the compressed size of each table in the final woff2 file won't + // be larger than its transform_length, we have to allocate a large enough + // buffer for the compressor, since the compressor can potentially increase + // the size. If the compressor overflows this, it should return false and + // then this function will also return false. + size_t total_transform_length = ComputeTotalTransformLength(font); + size_t compression_buffer_size = 1.2 * total_transform_length + 10240; + std::vector<uint8_t> compression_buf(compression_buffer_size); + uint32_t total_compressed_length = compression_buffer_size; + + // Collect all transformed data into one place. + std::vector<uint8_t> transform_buf(total_transform_length); + size_t transform_offset = 0; + for (const auto& i : font.tables) { + if (i.second.tag & 0x80808080) continue; + const Font::Table* table = font.FindTable(i.second.tag ^ 0x80808080); + if (table == NULL) table = &i.second; + StoreBytes(table->data, table->length, + &transform_offset, &transform_buf[0]); + } + // Compress all transformed data in one stream. + if (!Woff2Compress(transform_buf.data(), total_transform_length, + &compression_buf[0], + &total_compressed_length)) { + fprintf(stderr, "Compression of combined table failed.\n"); + return false; + } + + std::vector<Table> tables; + for (const auto& i : font.tables) { + const Font::Table& src_table = i.second; + if (src_table.tag & 0x80808080) { + // This is a transformed table, we will write it together with the + // original version. + continue; + } + Table table; + table.tag = src_table.tag; + table.flags = 0; + table.src_length = src_table.length; + table.transform_length = src_table.length; + const uint8_t* transformed_data = src_table.data; + const Font::Table* transformed_table = + font.FindTable(src_table.tag ^ 0x80808080); + if (transformed_table != NULL) { + table.flags |= kWoff2FlagsTransform; + table.transform_length = transformed_table->length; + transformed_data = transformed_table->data; + } + if (tables.empty()) { + table.dst_length = total_compressed_length; + table.dst_data = &compression_buf[0]; + } else { + table.dst_length = 0; + table.dst_data = NULL; + table.flags |= kWoff2FlagsContinueStream; + } + tables.push_back(table); + } + + size_t woff2_length = ComputeWoff2Length(tables); + if (woff2_length > *result_length) { + fprintf(stderr, "Result allocation was too small (%zd vs %zd bytes).\n", + *result_length, woff2_length); + return false; + } + *result_length = woff2_length; + + size_t offset = 0; + StoreU32(kWoff2Signature, &offset, result); + StoreU32(font.flavor, &offset, result); + StoreU32(woff2_length, &offset, result); + Store16(tables.size(), &offset, result); + Store16(0, &offset, result); // reserved + StoreU32(ComputeTTFLength(tables), &offset, result); + StoreU32(total_compressed_length, &offset, result); + StoreBytes(head_table->data + 4, 4, &offset, result); // font revision + StoreU32(0, &offset, result); // metaOffset + StoreU32(0, &offset, result); // metaLength + StoreU32(0, &offset, result); // metaOrigLength + StoreU32(0, &offset, result); // privOffset + StoreU32(0, &offset, result); // privLength + for (const auto& table : tables) { + StoreTableEntry(table, &offset, result); + } + for (const auto& table : tables) { + StoreBytes(table.dst_data, table.dst_length, &offset, result); + offset = Round4(offset); + } + if (*result_length != offset) { + fprintf(stderr, "Mismatch between computed and actual length " + "(%zd vs %zd)\n", *result_length, offset); + return false; + } + return true; +} + +} // namespace woff2 diff --git a/woff2/woff2.h b/woff2/woff2_enc.h index 2e4ee25..18adc0c 100644 --- a/woff2/woff2.h +++ b/woff2/woff2_enc.h @@ -1,4 +1,4 @@ -// Copyright 2013 Google Inc. All Rights Reserved. +// Copyright 2014 Google Inc. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -14,26 +14,14 @@ // // Library for converting WOFF2 format font files to their TTF versions. -#ifndef WOFF2_WOFF2_H_ -#define WOFF2_WOFF2_H_ +#ifndef WOFF2_WOFF2_ENC_H_ +#define WOFF2_WOFF2_ENC_H_ #include <stddef.h> #include <inttypes.h> -#include <string> namespace woff2 { -using std::string; - -// Compute the size of the final uncompressed font, or 0 on error. -size_t ComputeWOFF2FinalSize(const uint8_t *data, size_t length); - -// Decompresses the font into the target buffer. The result_length should -// be the same as determined by ComputeFinalSize(). Returns true on successful -// decompression. -bool ConvertWOFF2ToTTF(uint8_t *result, size_t result_length, - const uint8_t *data, size_t length); - // Returns an upper bound on the size of the compressed file. size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length); @@ -43,8 +31,6 @@ size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length); bool ConvertTTFToWOFF2(const uint8_t *data, size_t length, uint8_t *result, size_t *result_length); - - } // namespace woff2 -#endif // WOFF2_WOFF2_H_ +#endif // WOFF2_WOFF2_ENC_H_ |