Set font mode for the brotli call in the woff2 encoder.

This commit is contained in:
Zoltan Szabadka
2014-03-20 14:35:41 +01:00
parent 278b89484f
commit 494c85cebb
11 changed files with 40 additions and 54 deletions
+3 -3
View File
@@ -14,8 +14,8 @@
// //
// File IO helpers // File IO helpers
#ifndef BROTLI_WOFF2_FILE_H_ #ifndef WOFF2_FILE_H_
#define BROTLI_WOFF2_FILE_H_ #define WOFF2_FILE_H_
#include <fstream> #include <fstream>
#include <iterator> #include <iterator>
@@ -37,4 +37,4 @@ inline void SetFileContents(std::string filename, std::string content) {
} }
} // namespace woff2 } // namespace woff2
#endif // BROTLI_WOFF2_FILE_H_ #endif // WOFF2_FILE_H_
+3 -3
View File
@@ -15,8 +15,8 @@
// Data model for a font file in sfnt format, reading and writing functions and // Data model for a font file in sfnt format, reading and writing functions and
// accessors for the glyph data. // accessors for the glyph data.
#ifndef BROTLI_WOFF2_FONT_H_ #ifndef WOFF2_FONT_H_
#define BROTLI_WOFF2_FONT_H_ #define WOFF2_FONT_H_
#include <stddef.h> #include <stddef.h>
#include <inttypes.h> #include <inttypes.h>
@@ -78,4 +78,4 @@ bool GetGlyphData(const Font& font, int glyph_index,
} // namespace woff2 } // namespace woff2
#endif // BROTLI_WOFF2_FONT_H_ #endif // WOFF2_FONT_H_
+3 -3
View File
@@ -15,8 +15,8 @@
// Data model and I/O for glyph data within sfnt format files for the purpose of // 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. // performing the preprocessing step of the WOFF 2.0 conversion.
#ifndef BROTLI_WOFF2_GLYPH_H_ #ifndef WOFF2_GLYPH_H_
#define BROTLI_WOFF2_GLYPH_H_ #define WOFF2_GLYPH_H_
#include <stddef.h> #include <stddef.h>
#include <inttypes.h> #include <inttypes.h>
@@ -68,4 +68,4 @@ bool StoreGlyph(const Glyph& glyph, uint8_t* dst, size_t* dst_size);
} // namespace woff2 } // namespace woff2
#endif // BROTLI_WOFF2_GLYPH_H_ #endif // WOFF2_GLYPH_H_
+3 -3
View File
@@ -16,8 +16,8 @@
// files in normalized form, the WOFF 2.0 conversion is guaranteed to be // files in normalized form, the WOFF 2.0 conversion is guaranteed to be
// lossless (in a bitwise sense) only for normalized font files. // lossless (in a bitwise sense) only for normalized font files.
#ifndef BROTLI_WOFF2_NORMALIZE_H_ #ifndef WOFF2_NORMALIZE_H_
#define BROTLI_WOFF2_NORMALIZE_H_ #define WOFF2_NORMALIZE_H_
namespace woff2 { namespace woff2 {
@@ -42,4 +42,4 @@ bool NormalizeFont(Font* font);
} // namespace woff2 } // namespace woff2
#endif // BROTLI_WOFF2_NORMALIZE_H_ #endif // WOFF2_NORMALIZE_H_
+3 -3
View File
@@ -15,8 +15,8 @@
// The parts of ots.h & opentype-sanitiser.h that we need, taken from the // The parts of ots.h & opentype-sanitiser.h that we need, taken from the
// https://code.google.com/p/ots/ project. // https://code.google.com/p/ots/ project.
#ifndef BROTLI_WOFF2_OTS_H_ #ifndef WOFF2_OTS_H_
#define BROTLI_WOFF2_OTS_H_ #define WOFF2_OTS_H_
#include <stdint.h> #include <stdint.h>
#include <arpa/inet.h> #include <arpa/inet.h>
@@ -150,4 +150,4 @@ class Buffer {
} // namespace ots } // namespace ots
#endif // BROTLI_WOFF2_OTS_H_ #endif // WOFF2_OTS_H_
+3 -3
View File
@@ -14,8 +14,8 @@
// //
// Helper function for bit twiddling // Helper function for bit twiddling
#ifndef BROTLI_WOFF2_PORT_H_ #ifndef WOFF2_PORT_H_
#define BROTLI_WOFF2_PORT_H_ #define WOFF2_PORT_H_
namespace woff2 { namespace woff2 {
@@ -43,4 +43,4 @@ inline int Log2Floor(uint32 n) {
} }
} // namespace woff2 } // namespace woff2
#endif // BROTLI_WOFF2_PORT_H_ #endif // WOFF2_PORT_H_
+3 -3
View File
@@ -14,8 +14,8 @@
// //
// Helper for rounding // Helper for rounding
#ifndef BROTLI_WOFF2_ROUND_H_ #ifndef WOFF2_ROUND_H_
#define BROTLI_WOFF2_ROUND_H_ #define WOFF2_ROUND_H_
namespace woff2 { namespace woff2 {
@@ -30,4 +30,4 @@ template<typename T> T Round4(T value) {
} // namespace woff2 } // namespace woff2
#endif // BROTLI_WOFF2_ROUND_H_ #endif // WOFF2_ROUND_H_
+3 -3
View File
@@ -15,8 +15,8 @@
// Helper functions for storing integer values into byte streams. // Helper functions for storing integer values into byte streams.
// No bounds checking is performed, that is the responsibility of the caller. // No bounds checking is performed, that is the responsibility of the caller.
#ifndef BROTLI_WOFF2_STORE_BYTES_H_ #ifndef WOFF2_STORE_BYTES_H_
#define BROTLI_WOFF2_STORE_BYTES_H_ #define WOFF2_STORE_BYTES_H_
#include <inttypes.h> #include <inttypes.h>
#include <stddef.h> #include <stddef.h>
@@ -58,4 +58,4 @@ inline void StoreBytes(const uint8_t* data, size_t len,
} // namespace woff2 } // namespace woff2
#endif // BROTLI_WOFF2_STORE_BYTES_H_ #endif // WOFF2_STORE_BYTES_H_
+3 -3
View File
@@ -14,8 +14,8 @@
// //
// Library for preprocessing fonts as part of the WOFF 2.0 conversion. // Library for preprocessing fonts as part of the WOFF 2.0 conversion.
#ifndef BROTLI_WOFF2_TRANSFORM_H_ #ifndef WOFF2_TOOLS_TRANSFORM_H_
#define BROTLI_WOFF2_TRANSFORM_H_ #define WOFF2_TOOLS_TRANSFORM_H_
#include "./font.h" #include "./font.h"
@@ -28,4 +28,4 @@ bool TransformGlyfAndLocaTables(Font* font);
} // namespace woff2 } // namespace woff2
#endif // BROTLI_WOFF2_TRANSFORM_H_ #endif // WOFF2_TRANSFORM_H_
+10 -24
View File
@@ -40,7 +40,6 @@ using std::string;
using std::vector; using std::vector;
// simple glyph flags // simple glyph flags
const int kGlyfOnCurve = 1 << 0; const int kGlyfOnCurve = 1 << 0;
const int kGlyfXShort = 1 << 1; const int kGlyfXShort = 1 << 1;
@@ -88,9 +87,7 @@ const size_t kLzmaHeaderSize = 13;
const uint32_t kCompressionTypeMask = 0xf; const uint32_t kCompressionTypeMask = 0xf;
const uint32_t kCompressionTypeNone = 0; const uint32_t kCompressionTypeNone = 0;
const uint32_t kCompressionTypeGzip = 1; const uint32_t kCompressionTypeGzip = 1;
const uint32_t kCompressionTypeLzma = 2; const uint32_t kCompressionTypeBrotli = 2;
const uint32_t kCompressionTypeBrotli = 3;
const uint32_t kCompressionTypeLzham = 4;
// This is a special value for the short format only, as described in // This is a special value for the short format only, as described in
// "Design for compressed header format" in draft doc. // "Design for compressed header format" in draft doc.
@@ -736,8 +733,9 @@ bool Woff2Compress(const uint8_t* data, const size_t len,
uint8_t* result, uint32_t* result_len) { uint8_t* result, uint32_t* result_len) {
if (compression_type == kCompressionTypeBrotli) { if (compression_type == kCompressionTypeBrotli) {
size_t compressed_len = *result_len; size_t compressed_len = *result_len;
brotli::BrotliParams params;
brotli::BrotliCompressBuffer(len, data, &compressed_len, result); params.mode = brotli::BrotliParams::MODE_FONT;
brotli::BrotliCompressBuffer(params, len, data, &compressed_len, result);
*result_len = compressed_len; *result_len = compressed_len;
return true; return true;
} }
@@ -839,7 +837,7 @@ bool ReadShortDirectory(ots::Buffer* file, std::vector<Table>* tables,
} else { } else {
if (flags == kCompressionTypeNone || if (flags == kCompressionTypeNone ||
flags == kCompressionTypeGzip || flags == kCompressionTypeGzip ||
flags == kCompressionTypeLzma) { flags == kCompressionTypeBrotli) {
last_compression_type = flags; last_compression_type = flags;
} else { } else {
return OTS_FAILURE(); return OTS_FAILURE();
@@ -911,19 +909,13 @@ bool ConvertWOFF2ToTTF(uint8_t* result, size_t result_length,
if (!file.ReadU16(&num_tables) || !num_tables) { if (!file.ReadU16(&num_tables) || !num_tables) {
return OTS_FAILURE(); return OTS_FAILURE();
} }
// These reserved bits will be always zero in the final format, but they
// temporarily indicate the use of brotli, so that we can evaluate gzip, lzma
// and brotli side-by-side.
uint16_t reserved;
if (!file.ReadU16(&reserved)) {
return OTS_FAILURE();
}
// We don't care about these fields of the header: // We don't care about these fields of the header:
// uint16_t reserved
// uint32_t total_sfnt_size // uint32_t total_sfnt_size
// uint16_t major_version, minor_version // uint16_t major_version, minor_version
// uint32_t meta_offset, meta_length, meta_orig_length // uint32_t meta_offset, meta_length, meta_orig_length
// uint32_t priv_offset, priv_length // uint32_t priv_offset, priv_length
if (!file.Skip(28)) { if (!file.Skip(30)) {
return OTS_FAILURE(); return OTS_FAILURE();
} }
std::vector<Table> tables(num_tables); std::vector<Table> tables(num_tables);
@@ -996,9 +988,6 @@ bool ConvertWOFF2ToTTF(uint8_t* result, size_t result_length,
uint32_t flags = table->flags; uint32_t flags = table->flags;
const uint8_t* src_buf = data + table->src_offset; const uint8_t* src_buf = data + table->src_offset;
uint32_t compression_type = flags & kCompressionTypeMask; uint32_t compression_type = flags & kCompressionTypeMask;
if (compression_type == kCompressionTypeLzma && reserved > 0) {
compression_type = kCompressionTypeLzma + reserved;
}
size_t transform_length = table->transform_length; size_t transform_length = table->transform_length;
if ((flags & kWoff2FlagsContinueStream) != 0) { if ((flags & kWoff2FlagsContinueStream) != 0) {
if (!continue_valid) { if (!continue_valid) {
@@ -1090,7 +1079,7 @@ size_t TableEntrySize(const Table& table) {
} }
if ((table.flags & kWoff2FlagsContinueStream) == 0 && if ((table.flags & kWoff2FlagsContinueStream) == 0 &&
((table.flags & 3) == kCompressionTypeGzip || ((table.flags & 3) == kCompressionTypeGzip ||
(table.flags & 3) == kCompressionTypeLzma)) { (table.flags & 3) == kCompressionTypeBrotli)) {
size += Base128Size(table.dst_length); size += Base128Size(table.dst_length);
} }
return size; return size;
@@ -1226,7 +1215,7 @@ bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
} }
Table table; Table table;
table.tag = src_table.tag; table.tag = src_table.tag;
table.flags = std::min(options.compression_type, kCompressionTypeLzma); table.flags = options.compression_type;
table.src_length = src_table.length; table.src_length = src_table.length;
table.transform_length = src_table.length; table.transform_length = src_table.length;
const uint8_t* transformed_data = src_table.data; const uint8_t* transformed_data = src_table.data;
@@ -1278,16 +1267,13 @@ bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
return false; return false;
} }
*result_length = woff2_length; *result_length = woff2_length;
uint16_t reserved =
(options.compression_type > kCompressionTypeLzma) ?
options.compression_type - kCompressionTypeLzma : 0;
size_t offset = 0; size_t offset = 0;
StoreU32(kWoff2Signature, &offset, result); StoreU32(kWoff2Signature, &offset, result);
StoreU32(font.flavor, &offset, result); StoreU32(font.flavor, &offset, result);
StoreU32(woff2_length, &offset, result); StoreU32(woff2_length, &offset, result);
Store16(tables.size(), &offset, result); Store16(tables.size(), &offset, result);
Store16(reserved, &offset, result); Store16(0, &offset, result); // reserved
StoreU32(ComputeTTFLength(tables), &offset, result); StoreU32(ComputeTTFLength(tables), &offset, result);
StoreBytes(head_table->data + 4, 4, &offset, result); // font revision StoreBytes(head_table->data + 4, 4, &offset, result); // font revision
StoreU32(0, &offset, result); // metaOffset StoreU32(0, &offset, result); // metaOffset
+3 -3
View File
@@ -14,8 +14,8 @@
// //
// Library for converting WOFF2 format font files to their TTF versions. // Library for converting WOFF2 format font files to their TTF versions.
#ifndef BROTLI_WOFF2_WOFF2_H_ #ifndef WOFF2_WOFF2_H_
#define BROTLI_WOFF2_WOFF2_H_ #define WOFF2_WOFF2_H_
#include <stddef.h> #include <stddef.h>
#include <inttypes.h> #include <inttypes.h>
@@ -47,4 +47,4 @@ bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
} // namespace woff2 } // namespace woff2
#endif // BROTLI_WOFF2_WOFF2_H_ #endif // WOFF2_WOFF2_H_