Commit ca992267 authored by ksakamoto's avatar ksakamoto Committed by Commit bot

Add woff2 to src/third_party

This patch adds the woff2 library [1] which is used to decode WOFF 2.0
web font file format. It's going to replace the old version of WOFF 2.0
decoder included in third_party/ots.

[1] https://github.com/google/woff2 (revision 445f5419)

BUG=434596
TEST=trybots

Review URL: https://codereview.chromium.org/954163002

Cr-Commit-Position: refs/heads/master@{#322517}
parent 0c24b120
*.o
/woff2_compress
/woff2_decompress
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
source_set("woff2_dec") {
sources = [
"src/buffer.h",
"src/round.h",
"src/store_bytes.h",
"src/table_tags.cc",
"src/table_tags.h",
"src/woff2_common.h",
"src/woff2_dec.cc",
"src/woff2_dec.h",
]
configs -= [ "//build/config/compiler:chromium_code" ]
configs += [ "//build/config/compiler:no_chromium_code" ]
deps = [
"//third_party/brotli",
]
include_dirs = [
"src",
"//third_party/brotli/dec",
]
# TODO(ksakamoto): http://crbug.com/167187
if (is_win) {
cflags = [ "/wd4267" ] # Conversion from size_t to 'type'.
}
}
This diff is collapsed.
OS := $(shell uname)
IDIRS=-I./brotli/dec/ -I./brotli/enc/ -I./src
CXX = g++
LFLAGS =
GFLAGS=-no-canonical-prefixes -fno-omit-frame-pointer -m64
CXXFLAGS = -c $(IDIRS) -std=c++0x $(GFLAGS)
ifeq ($(OS), Darwin)
CXXFLAGS += -DOS_MACOSX
else
CXXFLAGS += -fno-tree-vrp
endif
SRCDIR = src
OUROBJ = font.o glyph.o normalize.o table_tags.o transform.o \
woff2_dec.o woff2_enc.o
BROTLI = brotli
ENCOBJ = $(BROTLI)/enc/*.o
DECOBJ = $(BROTLI)/dec/*.o
OBJS = $(patsubst %, $(SRCDIR)/%, $(OUROBJ))
EXECUTABLES=woff2_compress woff2_decompress
EXE_OBJS=$(patsubst %, $(SRCDIR)/%.o, $(EXECUTABLES))
all : $(OBJS) $(EXECUTABLES)
$(EXECUTABLES) : $(EXE_OBJS) deps
$(CXX) $(LFLAGS) $(OBJS) $(ENCOBJ) $(DECOBJ) $(SRCDIR)/$@.o -o $@
deps :
make -C $(BROTLI)/dec
make -C $(BROTLI)/enc
clean :
rm -f $(OBJS) $(EXE_OBJS) $(EXECUTABLES)
make -C $(BROTLI)/dec clean
make -C $(BROTLI)/enc clean
ksakamoto@chromium.org
bashi@chromium.org
Name: woff2
URL: https://github.com/google/woff2
Version: 445f541996fe8376f3976d35692fd2b9a6eedf2d
License: Apache 2.0
License File: LICENSE
Security Critical: yes
Description:
The reference implementation of the WOFF 2.0 web font compression
format (http://www.w3.org/TR/WOFF2/).
Local Modifications:
- BUILD.gn: Added.
- woff2.gyp: Added.
- Cherry-picked https://github.com/google/woff2/commit/0c1ec8895bce7c7b2da66ebc6e9887ef2e0c1a55.
This is a README for the font compression reference code. There are several
compression related modules in this repository.
brotli/ contains reference code for the Brotli byte-level compression
algorithm. Note that it is licensed under an Apache 2 license.
src/ contains the C++ code for compressing and decompressing fonts.
# Build & Run
This document documents how to run the compression reference code. At this
writing, the code, while it is intended to produce a bytestream that can be
reconstructed into a working font, the reference decompression code is not
done, and the exact format of that bytestream is subject to change.
## Build
On a standard Unix-style environment:
```
git clone https://github.com/google/woff2.git
cd woff2
git submodule init
git submodule update
make clean all
```
## Run
```
woff2_compress myfont.ttf
woff2_decompress myfont.woff2
```
# References
http://www.w3.org/TR/WOFF2/
http://www.w3.org/Submission/MTX/
Also please refer to documents (currently Google Docs):
WOFF Ultra Condensed file format: proposals and discussion of wire format
issues (PDF is in docs/ directory)
WIFF Ultra Condensed: more discussion of results and compression techniques.
This tool was used to prepare the data in that document.
// Copyright 2013 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.
//
// The parts of ots.h & opentype-sanitiser.h that we need, taken from the
// https://code.google.com/p/ots/ project.
#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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
namespace woff2 {
#if defined(_MSC_VER) || !defined(FONT_COMPRESSION_DEBUG)
#define FONT_COMPRESSION_FAILURE() false
#else
#define FONT_COMPRESSION_FAILURE() \
util::compression::font::Failure(__FILE__, __LINE__, __PRETTY_FUNCTION__)
inline bool Failure(const char *f, int l, const char *fn) {
fprintf(stderr, "ERROR at %s:%d (%s)\n", f, l, fn);
fflush(stderr);
return false;
}
#endif
// -----------------------------------------------------------------------------
// Buffer helper class
//
// This class perform some trival buffer operations while checking for
// out-of-bounds errors. As a family they return false if anything is amiss,
// updating the current offset otherwise.
// -----------------------------------------------------------------------------
class Buffer {
public:
Buffer(const uint8_t *buffer, size_t len)
: buffer_(buffer),
length_(len),
offset_(0) { }
bool Skip(size_t n_bytes) {
return Read(NULL, n_bytes);
}
bool Read(uint8_t *buffer, size_t n_bytes) {
if (n_bytes > 1024 * 1024 * 1024) {
return FONT_COMPRESSION_FAILURE();
}
if ((offset_ + n_bytes > length_) ||
(offset_ > length_ - n_bytes)) {
return FONT_COMPRESSION_FAILURE();
}
if (buffer) {
std::memcpy(buffer, buffer_ + offset_, n_bytes);
}
offset_ += n_bytes;
return true;
}
inline bool ReadU8(uint8_t *value) {
if (offset_ + 1 > length_) {
return FONT_COMPRESSION_FAILURE();
}
*value = buffer_[offset_];
++offset_;
return true;
}
bool ReadU16(uint16_t *value) {
if (offset_ + 2 > length_) {
return FONT_COMPRESSION_FAILURE();
}
std::memcpy(value, buffer_ + offset_, sizeof(uint16_t));
*value = ntohs(*value);
offset_ += 2;
return true;
}
bool ReadS16(int16_t *value) {
return ReadU16(reinterpret_cast<uint16_t*>(value));
}
bool ReadU24(uint32_t *value) {
if (offset_ + 3 > length_) {
return FONT_COMPRESSION_FAILURE();
}
*value = static_cast<uint32_t>(buffer_[offset_]) << 16 |
static_cast<uint32_t>(buffer_[offset_ + 1]) << 8 |
static_cast<uint32_t>(buffer_[offset_ + 2]);
offset_ += 3;
return true;
}
bool ReadU32(uint32_t *value) {
if (offset_ + 4 > length_) {
return FONT_COMPRESSION_FAILURE();
}
std::memcpy(value, buffer_ + offset_, sizeof(uint32_t));
*value = ntohl(*value);
offset_ += 4;
return true;
}
bool ReadS32(int32_t *value) {
return ReadU32(reinterpret_cast<uint32_t*>(value));
}
bool ReadTag(uint32_t *value) {
if (offset_ + 4 > length_) {
return FONT_COMPRESSION_FAILURE();
}
std::memcpy(value, buffer_ + offset_, sizeof(uint32_t));
offset_ += 4;
return true;
}
bool ReadR64(uint64_t *value) {
if (offset_ + 8 > length_) {
return FONT_COMPRESSION_FAILURE();
}
std::memcpy(value, buffer_ + offset_, sizeof(uint64_t));
offset_ += 8;
return true;
}
const uint8_t *buffer() const { return buffer_; }
size_t offset() const { return offset_; }
size_t length() const { return length_; }
void set_offset(size_t newoffset) { offset_ = newoffset; }
private:
const uint8_t * const buffer_;
const size_t length_;
size_t offset_;
};
} // namespace woff2
#endif // WOFF2_BUFFER_H_
// Copyright 2013 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.
//
// File IO helpers
#ifndef WOFF2_FILE_H_
#define WOFF2_FILE_H_
#include <fstream>
#include <iterator>
namespace woff2 {
inline std::string GetFileContent(std::string filename) {
std::ifstream ifs(filename.c_str(), std::ios::binary);
return std::string(
std::istreambuf_iterator<char>(ifs.rdbuf()),
std::istreambuf_iterator<char>());
}
inline void SetFileContents(std::string filename, std::string content) {
std::ofstream ofs(filename.c_str(), std::ios::binary);
std::copy(content.begin(),
content.end(),
std::ostream_iterator<char>(ofs));
}
} // namespace woff2
#endif // WOFF2_FILE_H_
// Copyright 2013 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 management utilities
#include "./font.h"
#include <algorithm>
#include "./buffer.h"
#include "./port.h"
#include "./store_bytes.h"
#include "./table_tags.h"
namespace woff2 {
Font::Table* Font::FindTable(uint32_t tag) {
std::map<uint32_t, Font::Table>::iterator it = tables.find(tag);
return it == tables.end() ? 0 : &it->second;
}
const Font::Table* Font::FindTable(uint32_t tag) const {
std::map<uint32_t, Font::Table>::const_iterator it = tables.find(tag);
return it == tables.end() ? 0 : &it->second;
}
bool ReadFont(const uint8_t* data, size_t len, Font* font) {
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 FONT_COMPRESSION_FAILURE();
}
std::map<uint32_t, uint32_t> intervals;
for (uint16_t i = 0; i < font->num_tables; ++i) {
Font::Table table;
if (!file.ReadU32(&table.tag) ||
!file.ReadU32(&table.checksum) ||
!file.ReadU32(&table.offset) ||
!file.ReadU32(&table.length)) {
return FONT_COMPRESSION_FAILURE();
}
if ((table.offset & 3) != 0 ||
table.length > len ||
len - table.length < table.offset) {
return FONT_COMPRESSION_FAILURE();
}
intervals[table.offset] = table.length;
table.data = data + table.offset;
if (font->tables.find(table.tag) != font->tables.end()) {
return FONT_COMPRESSION_FAILURE();
}
font->tables[table.tag] = table;
}
// Check that tables are non-overlapping.
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 FONT_COMPRESSION_FAILURE();
}
last_offset = i.first + i.second;
}
return true;
}
size_t FontFileSize(const Font& font) {
size_t max_offset = 12ULL + 16ULL * font.num_tables;
for (const auto& i : font.tables) {
const Font::Table& table = i.second;
size_t padding_size = (4 - (table.length & 3)) & 3;
size_t end_offset = (padding_size + table.offset) + table.length;
max_offset = std::max(max_offset, end_offset);
}
return max_offset;
}
bool WriteFont(const Font& font, uint8_t* dst, size_t dst_size) {
if (dst_size < 12ULL + 16ULL * font.num_tables) {
return FONT_COMPRESSION_FAILURE();
}
size_t offset = 0;
StoreU32(font.flavor, &offset, dst);
Store16(font.num_tables, &offset, dst);
uint16_t max_pow2 = font.num_tables ? Log2Floor(font.num_tables) : 0;
uint16_t search_range = max_pow2 ? 1 << (max_pow2 + 4) : 0;
uint16_t range_shift = (font.num_tables << 4) - search_range;
Store16(search_range, &offset, dst);
Store16(max_pow2, &offset, dst);
Store16(range_shift, &offset, dst);
for (const auto& i : font.tables) {
const Font::Table& table = i.second;
StoreU32(table.tag, &offset, dst);
StoreU32(table.checksum, &offset, dst);
StoreU32(table.offset, &offset, dst);
StoreU32(table.length, &offset, dst);
if (table.offset + table.length < table.offset ||
dst_size < table.offset + table.length) {
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 FONT_COMPRESSION_FAILURE();
}
memset(dst + table.offset + table.length, 0, padding_size);
}
return true;
}
int NumGlyphs(const Font& font) {
const Font::Table* head_table = font.FindTable(kHeadTableTag);
const Font::Table* loca_table = font.FindTable(kLocaTableTag);
if (head_table == NULL || loca_table == NULL || head_table->length < 52) {
return 0;
}
int index_fmt = IndexFormat(font);
int num_glyphs = (loca_table->length / (index_fmt == 0 ? 2 : 4)) - 1;
return num_glyphs;
}
int IndexFormat(const Font& font) {
const Font::Table* head_table = font.FindTable(kHeadTableTag);
if (head_table == NULL) {
return 0;
}
return head_table->data[51];
}
bool GetGlyphData(const Font& font, int glyph_index,
const uint8_t** glyph_data, size_t* glyph_size) {
if (glyph_index < 0) {
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 FONT_COMPRESSION_FAILURE();
}
int index_fmt = IndexFormat(font);
Buffer loca_buf(loca_table->data, loca_table->length);
if (index_fmt == 0) {
uint16_t offset1, offset2;
if (!loca_buf.Skip(2 * glyph_index) ||
!loca_buf.ReadU16(&offset1) ||
!loca_buf.ReadU16(&offset2) ||
offset2 < offset1 ||
2 * offset2 > glyf_table->length) {
return FONT_COMPRESSION_FAILURE();
}
*glyph_data = glyf_table->data + 2 * offset1;
*glyph_size = 2 * (offset2 - offset1);
} else {
uint32_t offset1, offset2;
if (!loca_buf.Skip(4 * glyph_index) ||
!loca_buf.ReadU32(&offset1) ||
!loca_buf.ReadU32(&offset2) ||
offset2 < offset1 ||
offset2 > glyf_table->length) {
return FONT_COMPRESSION_FAILURE();
}
*glyph_data = glyf_table->data + offset1;
*glyph_size = offset2 - offset1;
}
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
// Copyright 2013 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.
//
// Data model for a font file in sfnt format, reading and writing functions and
// accessors for the glyph data.
#ifndef WOFF2_FONT_H_
#define WOFF2_FONT_H_
#include <stddef.h>
#include <inttypes.h>
#include <map>
#include <vector>
namespace woff2 {
// 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.
struct Font {
uint32_t flavor;
uint16_t num_tables;
struct Table {
uint32_t tag;
uint32_t checksum;
uint32_t offset;
uint32_t length;
const uint8_t* data;
// Buffer used to mutate the data before writing out.
std::vector<uint8_t> buffer;
};
std::map<uint32_t, Table> tables;
Table* FindTable(uint32_t tag);
const Table* FindTable(uint32_t tag) const;
};
// Parses the font from the given data. Returns false on parsing failure or
// buffer overflow. The font is valid only so long the input data pointer is
// valid.
bool ReadFont(const uint8_t* data, size_t len, Font* font);
// Returns the file size of the font.
size_t FontFileSize(const Font& font);
// Writes the font into the specified dst buffer. The dst_size should be the
// same as returned by FontFileSize(). Returns false upon buffer overflow (which
// should not happen if dst_size was computed by FontFileSize()).
bool WriteFont(const Font& font, uint8_t* dst, size_t dst_size);
// Returns the number of glyphs in the font.
// NOTE: Currently this works only for TrueType-flavored fonts, will return
// zero for CFF-flavored fonts.
int NumGlyphs(const Font& font);
// Returns the index format of the font
int IndexFormat(const Font& font);
// Sets *glyph_data and *glyph_size to point to the location of the glyph data
// with the given index. Returns false if the glyph is not found.
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_
This diff is collapsed.
// Copyright 2013 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.
//
// Data model and I/O for glyph data within sfnt format files for the purpose of
// performing the preprocessing step of the WOFF 2.0 conversion.
#ifndef WOFF2_GLYPH_H_
#define WOFF2_GLYPH_H_
#include <stddef.h>
#include <inttypes.h>
#include <vector>
namespace woff2 {
// Represents a parsed simple or composite glyph. The composite glyph data and
// instructions are un-parsed and we keep only pointers to the raw data,
// therefore the glyph is valid only so long the data from which it was parsed
// is around.
class Glyph {
public:
Glyph() : instructions_size(0), composite_data_size(0) {}
// Bounding box.
int16_t x_min;
int16_t x_max;
int16_t y_min;
int16_t y_max;
// Instructions.
uint16_t instructions_size;
const uint8_t* instructions_data;
// Data model for simple glyphs.
struct Point {
int x;
int y;
bool on_curve;
};
std::vector<std::vector<Point> > contours;
// Data for composite glyphs.
const uint8_t* composite_data;
uint32_t composite_data_size;
bool have_instructions;
};
// Parses the glyph from the given data. Returns false on parsing failure or
// buffer overflow. The glyph is valid only so long the input data pointer is
// valid.
bool ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph);
// Stores the glyph into the specified dst buffer. The *dst_size is the buffer
// size on entry and is set to the actual (unpadded) stored size on exit.
// Returns false on buffer overflow.
bool StoreGlyph(const Glyph& glyph, uint8_t* dst, size_t* dst_size);
} // namespace woff2
#endif // WOFF2_GLYPH_H_
// Copyright 2013 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.
//
// Glyph normalization
#include "./normalize.h"
#include <inttypes.h>
#include <stddef.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 {
namespace {
void StoreLoca(int index_fmt, uint32_t value, size_t* offset, uint8_t* dst) {
if (index_fmt == 0) {
Store16(value >> 1, offset, dst);
} else {
StoreU32(value, offset, dst);
}
}
void NormalizeSimpleGlyphBoundingBox(Glyph* glyph) {
if (glyph->contours.empty() || glyph->contours[0].empty()) {
return;
}
int16_t x_min = glyph->contours[0][0].x;
int16_t y_min = glyph->contours[0][0].y;
int16_t x_max = x_min;
int16_t y_max = y_min;
for (const auto& contour : glyph->contours) {
for (const auto& point : contour) {
if (point.x < x_min) x_min = point.x;
if (point.x > x_max) x_max = point.x;
if (point.y < y_min) y_min = point.y;
if (point.y > y_max) y_max = point.y;
}
}
glyph->x_min = x_min;
glyph->y_min = y_min;
glyph->x_max = x_max;
glyph->y_max = y_max;
}
} // namespace
namespace {
bool WriteNormalizedLoca(int index_fmt, int num_glyphs, Font* font) {
Font::Table* glyf_table = font->FindTable(kGlyfTableTag);
Font::Table* loca_table = font->FindTable(kLocaTableTag);
int glyph_sz = index_fmt == 0 ? 2 : 4;
loca_table->buffer.resize(Round4(num_glyphs + 1) * glyph_sz);
loca_table->length = (num_glyphs + 1) * glyph_sz;
uint8_t* glyf_dst = &glyf_table->buffer[0];
uint8_t* loca_dst = &loca_table->buffer[0];
uint32_t glyf_offset = 0;
size_t loca_offset = 0;
for (int i = 0; i < num_glyphs; ++i) {
StoreLoca(index_fmt, glyf_offset, &loca_offset, loca_dst);
Glyph glyph;
const uint8_t* glyph_data;
size_t glyph_size;
if (!GetGlyphData(*font, i, &glyph_data, &glyph_size) ||
(glyph_size > 0 && !ReadGlyph(glyph_data, glyph_size, &glyph))) {
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 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 FONT_COMPRESSION_FAILURE();
}
glyf_offset += glyf_dst_size;
}
if (glyf_offset == 0) {
return false;
}
StoreLoca(index_fmt, glyf_offset, &loca_offset, loca_dst);
glyf_table->buffer.resize(glyf_offset);
glyf_table->data = &glyf_table->buffer[0];
glyf_table->length = glyf_offset;
loca_table->data = &loca_table->buffer[0];
return true;
}
} // namespace
namespace {
bool MakeEditableBuffer(Font* font, int tableTag) {
Font::Table* table = font->FindTable(tableTag);
if (table == NULL) {
return FONT_COMPRESSION_FAILURE();
}
int sz = Round4(table->length);
table->buffer.resize(sz);
uint8_t* buf = &table->buffer[0];
memcpy(buf, table->data, sz);
table->data = buf;
return true;
}
} // namespace
bool NormalizeGlyphs(Font* font) {
Font::Table* cff_table = font->FindTable(kCffTableTag);
Font::Table* head_table = font->FindTable(kHeadTableTag);
Font::Table* glyf_table = font->FindTable(kGlyfTableTag);
Font::Table* loca_table = font->FindTable(kLocaTableTag);
if (head_table == NULL) {
return FONT_COMPRESSION_FAILURE();
}
// CFF, no loca, no glyf is OK for CFF. If so, don't normalize.
if (cff_table != NULL && loca_table == NULL && glyf_table == NULL) {
return true;
}
if (loca_table == NULL || glyf_table == NULL) {
return FONT_COMPRESSION_FAILURE();
}
int index_fmt = head_table->data[51];
int num_glyphs = NumGlyphs(*font);
// We need to allocate a bit more than its original length for the normalized
// glyf table, since it can happen that the glyphs in the original table are
// 2-byte aligned, while in the normalized table they are 4-byte aligned.
// That gives a maximum of 2 bytes increase per glyph. However, there is no
// theoretical guarantee that the total size of the flags plus the coordinates
// is the smallest possible in the normalized version, so we have to allow
// some general overhead.
// TODO(user) Figure out some more precise upper bound on the size of
// the overhead.
size_t max_normalized_glyf_size = 1.1 * glyf_table->length + 2 * num_glyphs;
glyf_table->buffer.resize(max_normalized_glyf_size);
// if we can't write a loca using short's (index_fmt 0)
// try again using longs (index_fmt 1)
if (!WriteNormalizedLoca(index_fmt, num_glyphs, font)) {
if (index_fmt != 0) {
return FONT_COMPRESSION_FAILURE();
}
// Rewrite loca with 4-byte entries & update head to match
index_fmt = 1;
if (!WriteNormalizedLoca(index_fmt, num_glyphs, font)) {
return FONT_COMPRESSION_FAILURE();
}
head_table->buffer[51] = 1;
}
return true;
}
bool NormalizeOffsets(Font* font) {
uint32_t offset = 12 + 16 * font->num_tables;
for (auto& i : font->tables) {
i.second.offset = offset;
offset += Round4(i.second.length);
}
return true;
}
namespace {
uint32_t ComputeChecksum(const uint8_t* buf, size_t size) {
uint32_t checksum = 0;
for (size_t i = 0; i < size; i += 4) {
checksum += ((buf[i] << 24) |
(buf[i + 1] << 16) |
(buf[i + 2] << 8) |
buf[i + 3]);
}
return checksum;
}
uint32_t ComputeHeaderChecksum(const Font& font) {
uint32_t checksum = font.flavor;
uint16_t max_pow2 = font.num_tables ? Log2Floor(font.num_tables) : 0;
uint16_t search_range = max_pow2 ? 1 << (max_pow2 + 4) : 0;
uint16_t range_shift = (font.num_tables << 4) - search_range;
checksum += (font.num_tables << 16 | search_range);
checksum += (max_pow2 << 16 | range_shift);
for (const auto& i : font.tables) {
checksum += i.second.tag;
checksum += i.second.checksum;
checksum += i.second.offset;
checksum += i.second.length;
}
return checksum;
}
} // namespace
bool FixChecksums(Font* font) {
Font::Table* head_table = font->FindTable(kHeadTableTag);
if (head_table == NULL || head_table->length < 12) {
return FONT_COMPRESSION_FAILURE();
}
uint8_t* head_buf = &head_table->buffer[0];
size_t offset = 8;
StoreU32(0, &offset, head_buf);
uint32_t file_checksum = 0;
for (auto& i : font->tables) {
Font::Table* table = &i.second;
table->checksum = ComputeChecksum(table->data, table->length);
file_checksum += table->checksum;
}
file_checksum += ComputeHeaderChecksum(*font);
offset = 8;
StoreU32(0xb1b0afba - file_checksum, &offset, head_buf);
return true;
}
bool NormalizeFont(Font* font) {
return (MakeEditableBuffer(font, kHeadTableTag) &&
RemoveDigitalSignature(font) &&
NormalizeGlyphs(font) &&
NormalizeOffsets(font) &&
FixChecksums(font));
}
} // namespace woff2
// Copyright 2013 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.
//
// Functions for normalizing fonts. Since the WOFF 2.0 decoder creates font
// files in normalized form, the WOFF 2.0 conversion is guaranteed to be
// lossless (in a bitwise sense) only for normalized font files.
#ifndef WOFF2_NORMALIZE_H_
#define WOFF2_NORMALIZE_H_
namespace woff2 {
struct Font;
// Changes the offset fields of the table headers so that the data for the
// tables will be written in order of increasing tag values, without any gaps
// other than the 4-byte padding.
bool NormalizeOffsets(Font* font);
// Changes the checksum fields of the table headers and the checksum field of
// the head table so that it matches the current data.
bool FixChecksums(Font* font);
// Parses each of the glyphs in the font and writes them again to the glyf
// table in normalized form, as defined by the StoreGlyph() function. Changes
// the loca table accordigly.
bool NormalizeGlyphs(Font* font);
// Performs all of the normalization steps above.
bool NormalizeFont(Font* font);
} // namespace woff2
#endif // WOFF2_NORMALIZE_H_
// Copyright 2013 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.
//
// Helper function for bit twiddling
#ifndef WOFF2_PORT_H_
#define WOFF2_PORT_H_
namespace woff2 {
typedef unsigned int uint32;
inline int Log2Floor(uint32 n) {
#if defined(__GNUC__)
return n == 0 ? -1 : 31 ^ __builtin_clz(n);
#else
if (n == 0)
return -1;
int log = 0;
uint32 value = n;
for (int i = 4; i >= 0; --i) {
int shift = (1 << i);
uint32 x = value >> shift;
if (x != 0) {
value = x;
log += shift;
}
}
assert(value == 1);
return log;
#endif
}
} // namespace woff2
#endif // WOFF2_PORT_H_
// Copyright 2013 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.
//
// Helper for rounding
#ifndef WOFF2_ROUND_H_
#define WOFF2_ROUND_H_
#include <limits>
namespace woff2 {
// Round a value up to the nearest multiple of 4. Don't round the value in the
// case that rounding up overflows.
template<typename T> T Round4(T value) {
if (std::numeric_limits<T>::max() - value < 3) {
return value;
}
return (value + 3) & ~3;
}
} // namespace woff2
#endif // WOFF2_ROUND_H_
// Copyright 2013 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.
//
// Helper functions for storing integer values into byte streams.
// No bounds checking is performed, that is the responsibility of the caller.
#ifndef WOFF2_STORE_BYTES_H_
#define WOFF2_STORE_BYTES_H_
#include <inttypes.h>
#include <stddef.h>
#include <string.h>
namespace woff2 {
inline size_t StoreU32(uint8_t* dst, size_t offset, uint32_t x) {
dst[offset] = x >> 24;
dst[offset + 1] = x >> 16;
dst[offset + 2] = x >> 8;
dst[offset + 3] = x;
return offset + 4;
}
inline size_t Store16(uint8_t* dst, size_t offset, int x) {
dst[offset] = x >> 8;
dst[offset + 1] = x;
return offset + 2;
}
inline void StoreU32(uint32_t val, size_t* offset, uint8_t* dst) {
dst[(*offset)++] = val >> 24;
dst[(*offset)++] = val >> 16;
dst[(*offset)++] = val >> 8;
dst[(*offset)++] = val;
}
inline void Store16(int val, size_t* offset, uint8_t* dst) {
dst[(*offset)++] = val >> 8;
dst[(*offset)++] = val;
}
inline void StoreBytes(const uint8_t* data, size_t len,
size_t* offset, uint8_t* dst) {
memcpy(&dst[*offset], data, len);
*offset += len;
}
} // namespace woff2
#endif // WOFF2_STORE_BYTES_H_
// 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
// 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;
static const uint32_t kCffTableTag = 0x43464620;
extern const uint32_t kKnownTags[];
} // namespace woff2
#endif // WOFF2_TABLE_TAGS_H_
// Copyright 2013 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 preprocessing fonts as part of the WOFF 2.0 conversion.
#include "./transform.h"
#include <complex> // for std::abs
#include "./buffer.h"
#include "./font.h"
#include "./glyph.h"
#include "./table_tags.h"
namespace woff2 {
namespace {
const int FLAG_ARG_1_AND_2_ARE_WORDS = 1 << 0;
const int FLAG_WE_HAVE_INSTRUCTIONS = 1 << 8;
void WriteBytes(std::vector<uint8_t>* out, const uint8_t* data, size_t len) {
if (len == 0) return;
size_t offset = out->size();
out->resize(offset + len);
memcpy(&(*out)[offset], data, len);
}
void WriteBytes(std::vector<uint8_t>* out, const std::vector<uint8_t>& in) {
for (int i = 0; i < in.size(); ++i) {
out->push_back(in[i]);
}
}
void WriteUShort(std::vector<uint8_t>* out, int value) {
out->push_back(value >> 8);
out->push_back(value & 255);
}
void WriteLong(std::vector<uint8_t>* out, int value) {
out->push_back((value >> 24) & 255);
out->push_back((value >> 16) & 255);
out->push_back((value >> 8) & 255);
out->push_back(value & 255);
}
void Write255UShort(std::vector<uint8_t>* out, int value) {
if (value < 253) {
out->push_back(value);
} else if (value < 506) {
out->push_back(255);
out->push_back(value - 253);
} else if (value < 762) {
out->push_back(254);
out->push_back(value - 506);
} else {
out->push_back(253);
out->push_back(value >> 8);
out->push_back(value & 0xff);
}
}
// Glyf table preprocessing, based on
// GlyfEncoder.java
// but only the "sbbox" and "cbbox" options are supported.
class GlyfEncoder {
public:
explicit GlyfEncoder(int num_glyphs)
: sbbox_(false), cbbox_(true), n_glyphs_(num_glyphs) {
bbox_bitmap_.resize(((num_glyphs + 31) >> 5) << 2);
}
bool Encode(int glyph_id, const Glyph& glyph) {
if (glyph.composite_data_size > 0) {
WriteCompositeGlyph(glyph_id, glyph);
} else if (glyph.contours.size() > 0) {
WriteSimpleGlyph(glyph_id, glyph);
} else {
WriteUShort(&n_contour_stream_, 0);
}
return true;
}
void GetTransformedGlyfBytes(std::vector<uint8_t>* result) {
WriteLong(result, 0); // version
WriteUShort(result, n_glyphs_);
WriteUShort(result, 0); // index_format, will be set later
WriteLong(result, n_contour_stream_.size());
WriteLong(result, n_points_stream_.size());
WriteLong(result, flag_byte_stream_.size());
WriteLong(result, glyph_stream_.size());
WriteLong(result, composite_stream_.size());
WriteLong(result, bbox_bitmap_.size() + bbox_stream_.size());
WriteLong(result, instruction_stream_.size());
WriteBytes(result, n_contour_stream_);
WriteBytes(result, n_points_stream_);
WriteBytes(result, flag_byte_stream_);
WriteBytes(result, glyph_stream_);
WriteBytes(result, composite_stream_);
WriteBytes(result, bbox_bitmap_);
WriteBytes(result, bbox_stream_);
WriteBytes(result, instruction_stream_);
}
private:
void WriteInstructions(const Glyph& glyph) {
Write255UShort(&glyph_stream_, glyph.instructions_size);
WriteBytes(&instruction_stream_,
glyph.instructions_data, glyph.instructions_size);
}
void WriteSimpleGlyph(int glyph_id, const Glyph& glyph) {
int num_contours = glyph.contours.size();
WriteUShort(&n_contour_stream_, num_contours);
if (sbbox_) {
WriteBbox(glyph_id, glyph);
}
// TODO: check that bbox matches, write bbox if not
for (int i = 0; i < num_contours; i++) {
Write255UShort(&n_points_stream_, glyph.contours[i].size());
}
int lastX = 0;
int lastY = 0;
for (int i = 0; i < num_contours; i++) {
int num_points = glyph.contours[i].size();
for (int j = 0; j < num_points; j++) {
int x = glyph.contours[i][j].x;
int y = glyph.contours[i][j].y;
int dx = x - lastX;
int dy = y - lastY;
WriteTriplet(glyph.contours[i][j].on_curve, dx, dy);
lastX = x;
lastY = y;
}
}
if (num_contours > 0) {
WriteInstructions(glyph);
}
}
void WriteCompositeGlyph(int glyph_id, const Glyph& glyph) {
WriteUShort(&n_contour_stream_, -1);
if (cbbox_) {
WriteBbox(glyph_id, glyph);
}
WriteBytes(&composite_stream_,
glyph.composite_data,
glyph.composite_data_size);
if (glyph.have_instructions) {
WriteInstructions(glyph);
}
}
void WriteBbox(int glyph_id, const Glyph& glyph) {
bbox_bitmap_[glyph_id >> 3] |= 0x80 >> (glyph_id & 7);
WriteUShort(&bbox_stream_, glyph.x_min);
WriteUShort(&bbox_stream_, glyph.y_min);
WriteUShort(&bbox_stream_, glyph.x_max);
WriteUShort(&bbox_stream_, glyph.y_max);
}
void WriteTriplet(bool on_curve, int x, int y) {
int abs_x = std::abs(x);
int abs_y = std::abs(y);
int on_curve_bit = on_curve ? 0 : 128;
int x_sign_bit = (x < 0) ? 0 : 1;
int y_sign_bit = (y < 0) ? 0 : 1;
int xy_sign_bits = x_sign_bit + 2 * y_sign_bit;
if (x == 0 && abs_y < 1280) {
flag_byte_stream_.push_back(on_curve_bit +
((abs_y & 0xf00) >> 7) + y_sign_bit);
glyph_stream_.push_back(abs_y & 0xff);
} else if (y == 0 && abs_x < 1280) {
flag_byte_stream_.push_back(on_curve_bit + 10 +
((abs_x & 0xf00) >> 7) + x_sign_bit);
glyph_stream_.push_back(abs_x & 0xff);
} else if (abs_x < 65 && abs_y < 65) {
flag_byte_stream_.push_back(on_curve_bit + 20 +
((abs_x - 1) & 0x30) +
(((abs_y - 1) & 0x30) >> 2) +
xy_sign_bits);
glyph_stream_.push_back((((abs_x - 1) & 0xf) << 4) | ((abs_y - 1) & 0xf));
} else if (abs_x < 769 && abs_y < 769) {
flag_byte_stream_.push_back(on_curve_bit + 84 +
12 * (((abs_x - 1) & 0x300) >> 8) +
(((abs_y - 1) & 0x300) >> 6) + xy_sign_bits);
glyph_stream_.push_back((abs_x - 1) & 0xff);
glyph_stream_.push_back((abs_y - 1) & 0xff);
} else if (abs_x < 4096 && abs_y < 4096) {
flag_byte_stream_.push_back(on_curve_bit + 120 + xy_sign_bits);
glyph_stream_.push_back(abs_x >> 4);
glyph_stream_.push_back(((abs_x & 0xf) << 4) | (abs_y >> 8));
glyph_stream_.push_back(abs_y & 0xff);
} else {
flag_byte_stream_.push_back(on_curve_bit + 124 + xy_sign_bits);
glyph_stream_.push_back(abs_x >> 8);
glyph_stream_.push_back(abs_x & 0xff);
glyph_stream_.push_back(abs_y >> 8);
glyph_stream_.push_back(abs_y & 0xff);
}
}
std::vector<uint8_t> n_contour_stream_;
std::vector<uint8_t> n_points_stream_;
std::vector<uint8_t> flag_byte_stream_;
std::vector<uint8_t> composite_stream_;
std::vector<uint8_t> bbox_bitmap_;
std::vector<uint8_t> bbox_stream_;
std::vector<uint8_t> glyph_stream_;
std::vector<uint8_t> instruction_stream_;
bool sbbox_;
bool cbbox_;
int n_glyphs_;
};
} // namespace
bool TransformGlyfAndLocaTables(Font* font) {
// no transform for CFF
if (font->FindTable(kCffTableTag) != NULL
&& font->FindTable(kGlyfTableTag) == NULL
&& font->FindTable(kLocaTableTag) == NULL) {
return true;
}
Font::Table* transformed_glyf = &font->tables[kGlyfTableTag ^ 0x80808080];
Font::Table* transformed_loca = &font->tables[kLocaTableTag ^ 0x80808080];
int num_glyphs = NumGlyphs(*font);
GlyfEncoder encoder(num_glyphs);
for (int i = 0; i < num_glyphs; ++i) {
Glyph glyph;
const uint8_t* glyph_data;
size_t glyph_size;
if (!GetGlyphData(*font, i, &glyph_data, &glyph_size) ||
(glyph_size > 0 && !ReadGlyph(glyph_data, glyph_size, &glyph))) {
return FONT_COMPRESSION_FAILURE();
}
encoder.Encode(i, glyph);
}
encoder.GetTransformedGlyfBytes(&transformed_glyf->buffer);
const Font::Table* head_table = font->FindTable(kHeadTableTag);
if (head_table == NULL || head_table->length < 52) {
return FONT_COMPRESSION_FAILURE();
}
transformed_glyf->buffer[7] = head_table->data[51]; // index_format
transformed_glyf->tag = kGlyfTableTag ^ 0x80808080;
transformed_glyf->length = transformed_glyf->buffer.size();
transformed_glyf->data = transformed_glyf->buffer.data();
transformed_loca->tag = kLocaTableTag ^ 0x80808080;
transformed_loca->length = 0;
transformed_loca->data = NULL;
return true;
}
} // namespace woff2
// Copyright 2013 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 preprocessing fonts as part of the WOFF 2.0 conversion.
#ifndef WOFF2_TRANSFORM_H_
#define WOFF2_TRANSFORM_H_
#include "./font.h"
namespace woff2 {
// Adds the transformed versions of the glyf and loca tables to the font. The
// transformed loca table has zero length. The tag of the transformed tables is
// derived from the original tag by flipping the MSBs of every byte.
bool TransformGlyfAndLocaTables(Font* font);
} // namespace woff2
#endif // WOFF2_TRANSFORM_H_
// 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;
bool operator<(const Table& other) const {
return tag < other.tag;
}
};
} // namespace woff2
#endif // WOFF2_WOFF2_COMMON_H_
// Copyright 2013 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.
//
// A commandline tool for compressing ttf format files to woff2.
#include <string>
#include "file.h"
#include "./woff2_enc.h"
int main(int argc, char **argv) {
using std::string;
if (argc != 2) {
fprintf(stderr, "One argument, the input filename, must be provided.\n");
return 1;
}
string filename(argv[1]);
string outfilename = filename.substr(0, filename.find_last_of(".")) + ".woff2";
fprintf(stdout, "Processing %s => %s\n",
filename.c_str(), outfilename.c_str());
string input = woff2::GetFileContent(filename);
const uint8_t* input_data = reinterpret_cast<const uint8_t*>(input.data());
size_t output_size = woff2::MaxWOFF2CompressedSize(input_data, input.size());
string output(output_size, 0);
uint8_t* output_data = reinterpret_cast<uint8_t*>(&output[0]);
if (!woff2::ConvertTTFToWOFF2(input_data, input.size(),
output_data, &output_size)) {
fprintf(stderr, "Compression failed.\n");
return 1;
}
output.resize(output_size);
woff2::SetFileContents(outfilename, output);
return 0;
}
This diff is collapsed.
// 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_
// Copyright 2013 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.
//
// A very simple commandline tool for decompressing woff2 format files to true
// type font files.
#include <string>
#include "file.h"
#include "./woff2_dec.h"
int main(int argc, char **argv) {
using std::string;
if (argc != 2) {
fprintf(stderr, "One argument, the input filename, must be provided.\n");
return 1;
}
string filename(argv[1]);
string outfilename = filename.substr(0, filename.find_last_of(".")) + ".ttf";
fprintf(stdout, "Processing %s => %s\n",
filename.c_str(), outfilename.c_str());
string input = woff2::GetFileContent(filename);
size_t decompressed_size = woff2::ComputeWOFF2FinalSize(
reinterpret_cast<const uint8_t*>(input.data()), input.size());
string output(decompressed_size, 0);
const bool ok = woff2::ConvertWOFF2ToTTF(
reinterpret_cast<uint8_t*>(&output[0]), decompressed_size,
reinterpret_cast<const uint8_t*>(input.data()), input.size());
if (!ok) {
fprintf(stderr, "Decompression failed\n");
return 1;
}
woff2::SetFileContents(outfilename, output);
return 0;
}
This diff is collapsed.
// 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_ENC_H_
#define WOFF2_WOFF2_ENC_H_
#include <stddef.h>
#include <inttypes.h>
#include <string>
using std::string;
namespace woff2 {
// Returns an upper bound on the size of the compressed file.
size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length);
size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length,
const string& extended_metadata);
// Compresses the font into the target buffer. *result_length should be at least
// the value returned by MaxWOFF2CompressedSize(), upon return, it is set to the
// actual compressed size. Returns true on successful compression.
bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
uint8_t *result, size_t *result_length);
bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
uint8_t *result, size_t *result_length,
const string& extended_metadata);
} // namespace woff2
#endif // WOFF2_WOFF2_ENC_H_
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
{
'targets': [
{
'target_name': 'woff2_dec',
'type': 'static_library',
'include_dirs': [
'src',
'<(DEPTH)/third_party/brotli/dec',
],
'dependencies': [
'<(DEPTH)/third_party/brotli/brotli.gyp:brotli',
],
'sources': [
'src/buffer.h',
'src/round.h',
'src/store_bytes.h',
'src/table_tags.cc',
'src/table_tags.h',
'src/woff2_common.h',
'src/woff2_dec.cc',
'src/woff2_dec.h',
],
# TODO(ksakamoto): http://crbug.com/167187
'msvs_disabled_warnings': [
4267,
],
},
],
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment