Separate woff2 enc/dec logic

This commit is contained in:
Kenichi Ishibashi
2014-05-30 09:06:32 +09:00
parent 2ec0cb8a78
commit 142d8881c1
16 changed files with 684 additions and 551 deletions
+2 -1
View File
@@ -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
+17 -16
View File
@@ -15,8 +15,8 @@
// 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_
#ifndef WOFF2_BUFFER_H_
#define WOFF2_BUFFER_H_
#include <stdint.h>
#include <arpa/inet.h>
@@ -24,12 +24,13 @@
#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 +58,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 +73,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 +82,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 +96,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 +107,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 +121,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 +130,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 +149,6 @@ class Buffer {
size_t offset_;
};
} // namespace ots
} // namespace woff2
#endif // WOFF2_OTS_H_
#endif // WOFF2_BUFFER_H_
+16 -15
View File
@@ -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;
-6
View File
@@ -25,12 +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;
static const uint32_t kDsigTableTag = 0x44534947;
// 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.
+31 -31
View File
@@ -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;
+7 -6
View File
@@ -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];
+90
View File
@@ -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
+34
View File
@@ -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_
+4 -3
View File
@@ -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
+50
View File
@@ -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_
+1 -1
View File
@@ -17,7 +17,7 @@
#include <string>
#include "file.h"
#include "./woff2.h"
#include "./woff2_enc.h"
int main(int argc, char **argv) {
File diff suppressed because it is too large Load Diff
+36
View File
@@ -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_
+1 -1
View File
@@ -19,7 +19,7 @@
#include "file.h"
#include "./woff2.h"
#include "./woff2_dec.h"
int main(int argc, char **argv) {
using std::string;
+281
View File
@@ -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
+4 -16
View File
@@ -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);
@@ -45,4 +33,4 @@ bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
} // namespace woff2
#endif // WOFF2_WOFF2_H_
#endif // WOFF2_WOFF2_ENC_H_