Initial migration framework (#525)

What this does:

- Sets up a `migrate_cpp` tool which currently only runs `clang-tidy`.
  - This is intended to have more transformations in the future.
- Sets up a `migrate_cpp.sh` script.
  - This copies the original woff2 code into a `carbon` directory and runs the `migrate_cpp` tool on it there.
- Adds the initial `carbon` directory of woff2
  - To be clear, this is currently only updated via `clang-tidy`.
  - More transformations should be expected in the future.
- Minor related edits. For example:
  - Adjust pre-commit to skip the `carbon` directory, because it's third-party code and shouldn't be edited in the same way.
  - Adds `@brotli_carbon` as a local repository so that we can "build" outputs.
  - Makes clang-tidy from the bootstrap toolchain accessible for BUILD dependencies, as it's then used for `migrate_cpp`.

What this does not do:

- Any actual transformation of C++ code to Carbon

Co-authored-by: Matthew Riley <mdriley@gmail.com>
This commit is contained in:
Jon Meow
2021-05-11 10:13:47 -07:00
committed by GitHub
co-authored by Matthew Riley
parent e6d5efe7d9
commit 88a9d244b7
42 changed files with 766 additions and 331 deletions
+14 -3
View File
@@ -17,7 +17,11 @@ repos:
- id: check-executables-have-shebangs
- id: check-merge-conflict
- id: check-symlinks
exclude: '^(website/jekyll/site/_includes|bazel-(clang-toolchain|execroot))$'
exclude: |
(?x)^(
website/jekyll/site/_includes|
bazel-(clang-toolchain|execroot)
)$
- id: check-yaml
- id: detect-private-key
- id: end-of-file-fixer
@@ -25,7 +29,6 @@ repos:
args: ['--fix=lf']
- id: trailing-whitespace
exclude: '^(.*/testdata/.*\.golden)$'
- repo: https://github.com/google/pre-commit-tool-hooks
rev: 1d04a2848ac54d64bd6474ccec69aac45fa88414 # frozen: v1.1.1
hooks:
@@ -51,7 +54,15 @@ repos:
- ''
- '// '
- ''
exclude: '^(website/(firebase/.firebaserc|jekyll/(Gemfile.lock|theme/.*))|.bazelversion|compile_flags.txt|.*\.def|.*/testdata/.*\.golden)$'
exclude: |
(?x)^(
.bazelversion|
compile_flags.txt|
third_party/examples/.*/compile_flags.carbon.txt|
website/(firebase/.firebaserc|jekyll/(Gemfile.lock|theme/.*))|
.*\.def|
.*/testdata/.*\.golden
)$
- id: check-google-doc-style
- id: markdown-toc
- repo: https://github.com/codespell-project/codespell
+5
View File
@@ -122,3 +122,8 @@ new_local_repository(
path = "third_party/examples/woff2/original",
workspace_file = "third_party/examples/woff2/WORKSPACE.original",
)
local_repository(
name = "woff2_carbon",
path = "third_party/examples/woff2/carbon",
)
+4 -3
View File
@@ -260,9 +260,10 @@ def _bootstrap_clang_toolchain_impl(repository_ctx):
# Fallback to a local build.
_local_cmake_build_clang_toolchain(repository_ctx)
# Create an empty BUILD file to mark the package. The files are used without
# Bazel labels directly pointing at them.
repository_ctx.file("BUILD")
# Create a BUILD file to mark the package. The files are usually used
# without Bazel labels, but the export allows dependencies; for example, to
# use clang-tidy.
repository_ctx.file("BUILD", 'exports_files(glob(["bin/*"]))')
bootstrap_clang_toolchain = repository_rule(
implementation = _bootstrap_clang_toolchain_impl,
+15
View File
@@ -0,0 +1,15 @@
# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
# Exceptions. See /LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
load("@rules_python//python:defs.bzl", "py_binary")
py_binary(
name = "migrate_cpp",
srcs = ["migrate_cpp.py"],
data = [
":clang_tidy.yaml",
"@bootstrap_clang_toolchain//:bin/clang-tidy",
],
python_version = "PY3",
)
+20
View File
@@ -0,0 +1,20 @@
# C++ migration tooling
<!--
Part of the Carbon Language project, under the Apache License v2.0 with LLVM
Exceptions. See /LICENSE for license information.
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-->
<!-- toc -->
## Table of contents
- [Overview](#overview)
<!-- tocstop -->
## Overview
`migrate_cpp` assists in migration of C++ code to Carbon. It's currently being
assembled; more documentation will be added later.
+16
View File
@@ -0,0 +1,16 @@
# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
# Exceptions. See /LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
# Use clang-tidy to standardize syntax. This configuration focuses on
# modernizations, not casing-related choices, in order to avoid changes which
# would affect non-migrated callers.
---
Checks:
-*, bugprone-*, -bugprone-reserved-identifier, google-*,
-google-readability-todo, misc-definitions-in-headers, misc-misplaced-const,
misc-redundant-expression, misc-static-assert,
misc-unconventional-assign-operator, misc-uniqueptr-reset-release,
misc-unused-*, modernize-*, -modernize-avoid-c-arrays, performance-*,
readability-braces-around-statements
+72
View File
@@ -0,0 +1,72 @@
"""Migrates C++ code to Carbon."""
__copyright__ = """
Part of the Carbon Language project, under the Apache License v2.0 with LLVM
Exceptions. See /LICENSE for license information.
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
"""
import argparse
import glob
import os
import subprocess
import sys
_CLANG_TIDY = "../external/bootstrap_clang_toolchain/bin/clang-tidy"
_CPP_EXTS = {".h", ".c", ".cc", ".cpp", ".cxx"}
def _data_file(relative_path):
"""Returns the path to a data file."""
return os.path.join(os.path.dirname(sys.argv[0]), relative_path)
def _parse_args(args=None):
"""Parses command-line arguments and flags."""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"dir",
type=str,
help="A directory containing C++ files to migrate to Carbon.",
)
parsed_args = parser.parse_args(args=args)
return parsed_args
def _gather_files(parsed_args):
"""Returns the list of C++ files to convert."""
all_files = glob.glob(
os.path.join(parsed_args.dir, "**/*.*"), recursive=True
)
cpp_files = [f for f in all_files if os.path.splitext(f)[1] in _CPP_EXTS]
if not cpp_files:
sys.exit(
"%r doesn't contain any C++ files to convert." % parsed_args.dir
)
return sorted(cpp_files)
def _clang_tidy(parsed_args, cpp_files):
"""Runs clang-tidy to fix C++ files in a directory."""
print("Running clang-tidy...")
clang_tidy = _data_file(_CLANG_TIDY)
with open(_data_file("clang_tidy.yaml")) as f:
config = f.read()
subprocess.run([clang_tidy, "--fix", "--config", config] + cpp_files)
def _main():
"""Main program execution."""
parsed_args = _parse_args()
# Validate arguments.
if not os.path.isdir(parsed_args.dir):
sys.exit("%r must point to a directory." % parsed_args.dir)
cpp_files = _gather_files(parsed_args)
_clang_tidy(parsed_args, cpp_files)
print("Done!")
if __name__ == "__main__":
_main()
+106
View File
@@ -0,0 +1,106 @@
# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
# Exceptions. See /LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
# Rules are adapted from CMakeLists.txt.
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
package(
default_visibility = ["//visibility:public"],
)
# Common part used by decoder and encoder
cc_library(
name = "woff2common",
srcs = [
"src/table_tags.cc",
"src/variable_length.cc",
"src/woff2_common.cc",
],
hdrs = [
"src/buffer.h",
"src/file.h",
"src/port.h",
"src/round.h",
"src/store_bytes.h",
"src/table_tags.h",
"src/variable_length.h",
"src/woff2_common.h",
],
strip_include_prefix = "src/",
)
# WOFF2 Decoder
cc_library(
name = "woff2dec",
srcs = [
"src/woff2_dec.cc",
"src/woff2_out.cc",
],
hdrs = [
"include/woff2/decode.h",
"include/woff2/output.h",
],
copts = [
"-Wno-unused-variable",
"-Wno-unused-const-variable",
],
strip_include_prefix = "include/",
deps = [
":woff2common",
"@brotli//:brotlidec",
],
)
cc_binary(
name = "woff2_decompress",
srcs = ["src/woff2_decompress.cc"],
deps = [":woff2dec"],
)
# WOFF2 Encoder
cc_library(
name = "woff2enc",
srcs = [
"src/font.cc",
"src/font.h",
"src/glyph.cc",
"src/glyph.h",
"src/normalize.cc",
"src/normalize.h",
"src/transform.cc",
"src/transform.h",
"src/woff2_enc.cc",
],
hdrs = [
"include/woff2/encode.h",
],
copts = [
"-Wno-unused-variable",
"-Wno-unused-const-variable",
"-Wno-sign-compare",
],
strip_include_prefix = "include/",
deps = [
":woff2common",
"@brotli//:brotlienc",
],
)
cc_binary(
name = "woff2_compress",
srcs = ["src/woff2_compress.cc"],
deps = [":woff2enc"],
)
# WOFF2 info
cc_binary(
name = "woff2_info",
srcs = [
"src/font.h",
"src/woff2_info.cc",
],
copts = ["-Wno-sign-compare"],
deps = [":woff2common"],
)
+5
View File
@@ -0,0 +1,5 @@
# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
# Exceptions. See /LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
workspace(name = "woff2")
+53
View File
@@ -0,0 +1,53 @@
-xc++
-Wall
-Wextra
-Wthread-safety
-Wself-assign
-Wno-missing-field-initializers
-DLLVM_ON_UNIX=1
-DHAVE_BACKTRACE=1
-DBACKTRACE_HEADER=<execinfo.h>
-DLTDL_SHLIB_EXT=".so"
-DLLVM_ENABLE_THREADS=1
-DHAVE_SYSEXITS_H=1
-DHAVE_UNISTD_H=1
-DHAVE_STRERROR_R=1
-DHAVE_LIBPTHREAD=1
-DHAVE_PTHREAD_GETNAME_NP=1
-DHAVE_PTHREAD_SETNAME_NP=1
-DHAVE_PTHREAD_GETSPECIFIC=1
-D_GNU_SOURCE
-DHAVE_LINK_H=1
-DHAVE_LSEEK64=1
-DHAVE_MALLINFO=1
-DHAVE_POSIX_FALLOCATE=1
-DHAVE_SBRK=1
-DHAVE_STRUCT_STAT_ST_MTIM_TV_NSEC=1
-DLLVM_NATIVE_ARCH="X86"
-DLLVM_NATIVE_ASMPARSER=LLVMInitializeX86AsmParser
-DLLVM_NATIVE_ASMPRINTER=LLVMInitializeX86AsmPrinter
-DLLVM_NATIVE_DISASSEMBLER=LLVMInitializeX86Disassembler
-DLLVM_NATIVE_TARGET=LLVMInitializeX86Target
-DLLVM_NATIVE_TARGETINFO=LLVMInitializeX86TargetInfo
-DLLVM_NATIVE_TARGETMC=LLVMInitializeX86TargetMC
-DLLVM_HOST_TRIPLE="x86_64-unknown-linux-gnu"
-DLLVM_DEFAULT_TARGET_TRIPLE="x86_64-unknown-linux-gnu"
-DLLVM_ENABLE_TERMINFO=1
-DLLVM_ENABLE_ZLIB=1
-DGTEST_HAS_RTTI=0
-D__STDC_LIMIT_MACROS
-D__STDC_CONSTANT_MACROS
-DGTEST_USE_OWN_TR1_TUPLE=1
-std=c++17
-nostdinc++
-I../../brotli/original/c/include
-Iinclude
-isystem
../../../../bazel-clang-toolchain/include/c++/v1
-isystem
../../../../bazel-clang-toolchain/lib/clang/12.0.0/include
-no-canonical-prefixes
-Wno-builtin-macro-redefined
-D__DATE__="redacted"
-D__TIMESTAMP__="redacted"
-D__TIME__="redacted"
+7 -7
View File
@@ -9,27 +9,27 @@
#ifndef WOFF2_WOFF2_DEC_H_
#define WOFF2_WOFF2_DEC_H_
#include <stddef.h>
#include <inttypes.h>
#include <cstddef>
#include <cinttypes>
#include <woff2/output.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);
auto ComputeWOFF2FinalSize(const uint8_t *data, size_t length) -> size_t;
// Decompresses the font into the target buffer. The result_length should
// be the same as determined by ComputeFinalSize(). Returns true on successful
// decompression.
// DEPRECATED; please prefer the version that takes a WOFF2Out*
bool ConvertWOFF2ToTTF(uint8_t *result, size_t result_length,
const uint8_t *data, size_t length);
auto ConvertWOFF2ToTTF(uint8_t *result, size_t result_length,
const uint8_t *data, size_t length) -> bool;
// Decompresses the font into out. Returns true on success.
// Works even if WOFF2Header totalSfntSize is wrong.
// Please prefer this API.
bool ConvertWOFF2ToTTF(const uint8_t *data, size_t length,
WOFF2Out* out);
auto ConvertWOFF2ToTTF(const uint8_t *data, size_t length,
WOFF2Out* out) -> bool;
} // namespace woff2
+13 -13
View File
@@ -9,34 +9,34 @@
#ifndef WOFF2_WOFF2_ENC_H_
#define WOFF2_WOFF2_ENC_H_
#include <stddef.h>
#include <inttypes.h>
#include <cstddef>
#include <cinttypes>
#include <string>
namespace woff2 {
struct WOFF2Params {
WOFF2Params() : extended_metadata(""), brotli_quality(11),
allow_transforms(true) {}
WOFF2Params() : extended_metadata("")
{}
std::string extended_metadata;
int brotli_quality;
bool allow_transforms;
int brotli_quality{11};
bool allow_transforms{true};
};
// 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 std::string& extended_metadata);
auto MaxWOFF2CompressedSize(const uint8_t* data, size_t length) -> size_t;
auto MaxWOFF2CompressedSize(const uint8_t* data, size_t length,
const std::string& extended_metadata) -> size_t;
// 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,
auto ConvertTTFToWOFF2(const uint8_t *data, size_t length,
uint8_t *result, size_t *result_length) -> bool;
auto ConvertTTFToWOFF2(const uint8_t *data, size_t length,
uint8_t *result, size_t *result_length,
const WOFF2Params& params);
const WOFF2Params& params) -> bool;
} // namespace woff2
+11 -11
View File
@@ -30,17 +30,17 @@ const size_t kDefaultMaxSize = 30 * 1024 * 1024;
*/
class WOFF2Out {
public:
virtual ~WOFF2Out(void) {}
virtual ~WOFF2Out() = default;
// Append n bytes of data from buf.
// Return true if all written, false otherwise.
virtual bool Write(const void *buf, size_t n) = 0;
virtual auto Write(const void *buf, size_t n) -> bool = 0;
// Write n bytes of data from buf at offset.
// Return true if all written, false otherwise.
virtual bool Write(const void *buf, size_t offset, size_t n) = 0;
virtual auto Write(const void *buf, size_t offset, size_t n) -> bool = 0;
virtual size_t Size() = 0;
virtual auto Size() -> size_t = 0;
};
/**
@@ -53,10 +53,10 @@ class WOFF2StringOut : public WOFF2Out {
// buf may be sized (e.g. using EstimateWOFF2FinalSize) or empty.
explicit WOFF2StringOut(std::string* buf);
bool Write(const void *buf, size_t n) override;
bool Write(const void *buf, size_t offset, size_t n) override;
size_t Size() override { return offset_; }
size_t MaxSize() { return max_size_; }
auto Write(const void *buf, size_t n) -> bool override;
auto Write(const void *buf, size_t offset, size_t n) -> bool override;
auto Size() -> size_t override { return offset_; }
auto MaxSize() -> size_t { return max_size_; }
void SetMaxSize(size_t max_size);
private:
std::string* buf_;
@@ -72,9 +72,9 @@ class WOFF2MemoryOut : public WOFF2Out {
// Create a writer that writes its data to buf.
WOFF2MemoryOut(uint8_t* buf, size_t buf_size);
bool Write(const void *buf, size_t n) override;
bool Write(const void *buf, size_t offset, size_t n) override;
size_t Size() override { return offset_; }
auto Write(const void *buf, size_t n) -> bool override;
auto Write(const void *buf, size_t offset, size_t n) -> bool override;
auto Size() -> size_t override { return offset_; }
private:
uint8_t* buf_;
size_t buf_size_;
+15 -15
View File
@@ -26,7 +26,7 @@ typedef unsigned __int64 uint64_t;
#define htons(x) _byteswap_ushort (x)
#else
#include <arpa/inet.h>
#include <stdint.h>
#include <cstdint>
#endif
#include <cstdio>
@@ -62,11 +62,11 @@ class Buffer {
length_(len),
offset_(0) { }
bool Skip(size_t n_bytes) {
return Read(NULL, n_bytes);
auto Skip(size_t n_bytes) -> bool {
return Read(nullptr, n_bytes);
}
bool Read(uint8_t *data, size_t n_bytes) {
auto Read(uint8_t *data, size_t n_bytes) -> bool {
if (n_bytes > 1024 * 1024 * 1024) {
return FONT_COMPRESSION_FAILURE();
}
@@ -81,7 +81,7 @@ class Buffer {
return true;
}
inline bool ReadU8(uint8_t *value) {
inline auto ReadU8(uint8_t *value) -> bool {
if (offset_ + 1 > length_) {
return FONT_COMPRESSION_FAILURE();
}
@@ -90,7 +90,7 @@ class Buffer {
return true;
}
bool ReadU16(uint16_t *value) {
auto ReadU16(uint16_t *value) -> bool {
if (offset_ + 2 > length_) {
return FONT_COMPRESSION_FAILURE();
}
@@ -100,11 +100,11 @@ class Buffer {
return true;
}
bool ReadS16(int16_t *value) {
auto ReadS16(int16_t *value) -> bool {
return ReadU16(reinterpret_cast<uint16_t*>(value));
}
bool ReadU24(uint32_t *value) {
auto ReadU24(uint32_t *value) -> bool {
if (offset_ + 3 > length_) {
return FONT_COMPRESSION_FAILURE();
}
@@ -115,7 +115,7 @@ class Buffer {
return true;
}
bool ReadU32(uint32_t *value) {
auto ReadU32(uint32_t *value) -> bool {
if (offset_ + 4 > length_) {
return FONT_COMPRESSION_FAILURE();
}
@@ -125,11 +125,11 @@ class Buffer {
return true;
}
bool ReadS32(int32_t *value) {
auto ReadS32(int32_t *value) -> bool {
return ReadU32(reinterpret_cast<uint32_t*>(value));
}
bool ReadTag(uint32_t *value) {
auto ReadTag(uint32_t *value) -> bool {
if (offset_ + 4 > length_) {
return FONT_COMPRESSION_FAILURE();
}
@@ -138,7 +138,7 @@ class Buffer {
return true;
}
bool ReadR64(uint64_t *value) {
auto ReadR64(uint64_t *value) -> bool {
if (offset_ + 8 > length_) {
return FONT_COMPRESSION_FAILURE();
}
@@ -147,9 +147,9 @@ class Buffer {
return true;
}
const uint8_t *buffer() const { return buffer_; }
size_t offset() const { return offset_; }
size_t length() const { return length_; }
[[nodiscard]] auto buffer() const -> const uint8_t * { return buffer_; }
[[nodiscard]] auto offset() const -> size_t { return offset_; }
[[nodiscard]] auto length() const -> size_t { return length_; }
void set_offset(size_t newoffset) { offset_ = newoffset; }
@@ -1,10 +1,10 @@
#include <stddef.h>
#include <stdint.h>
#include <cstddef>
#include <cstdint>
#include <woff2/decode.h>
// Entry point for LibFuzzer.
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
extern "C" auto LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) -> int {
std::string buf;
woff2::WOFF2StringOut out(&buf);
out.SetMaxSize(30 * 1024 * 1024);
@@ -1,7 +1,7 @@
#include <string>
#include <woff2/decode.h>
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t data_size) {
extern "C" auto LLVMFuzzerTestOneInput(const uint8_t *data, size_t data_size) -> int {
// Decode using newer entry pattern.
// Same pattern as woff2_decompress.
std::string output(std::min(woff2::ComputeWOFF2FinalSize(data, data_size),
+2 -2
View File
@@ -17,14 +17,14 @@ namespace woff2 {
using std::string;
inline string GetFileContent(string filename) {
inline auto GetFileContent(const string& filename) -> string {
std::ifstream ifs(filename.c_str(), std::ios::binary);
return string(
std::istreambuf_iterator<char>(ifs.rdbuf()),
std::istreambuf_iterator<char>());
}
inline void SetFileContents(string filename, string::iterator start,
inline void SetFileContents(const string& filename, string::iterator start,
string::iterator end) {
std::ofstream ofs(filename.c_str(), std::ios::binary);
std::copy(start, end, std::ostream_iterator<char>(ofs));
+42 -43
View File
@@ -18,17 +18,17 @@
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;
auto Font::FindTable(uint32_t tag) -> Font::Table* {
auto it = tables.find(tag);
return it == tables.end() ? nullptr : &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;
auto Font::FindTable(uint32_t tag) const -> const Font::Table* {
auto it = tables.find(tag);
return it == tables.end() ? nullptr : &it->second;
}
std::vector<uint32_t> Font::OutputOrderedTags() const {
auto Font::OutputOrderedTags() const -> std::vector<uint32_t> {
std::vector<uint32_t> output_order;
for (const auto& i : tables) {
@@ -55,8 +55,8 @@ std::vector<uint32_t> Font::OutputOrderedTags() const {
return output_order;
}
bool ReadTrueTypeFont(Buffer* file, const uint8_t* data, size_t len,
Font* font) {
auto ReadTrueTypeFont(Buffer* file, const uint8_t* data, size_t len,
Font* font) -> bool {
// 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->ReadU16(&font->num_tables) ||
@@ -68,7 +68,7 @@ bool ReadTrueTypeFont(Buffer* file, const uint8_t* data, size_t len,
for (uint16_t i = 0; i < font->num_tables; ++i) {
Font::Table table;
table.flag_byte = 0;
table.reuse_of = NULL;
table.reuse_of = nullptr;
if (!file->ReadU32(&table.tag) ||
!file->ReadU32(&table.checksum) ||
!file->ReadU32(&table.offset) ||
@@ -99,16 +99,16 @@ bool ReadTrueTypeFont(Buffer* file, const uint8_t* data, size_t len,
// Sanity check key tables
const Font::Table* head_table = font->FindTable(kHeadTableTag);
if (head_table != NULL && head_table->length < 52) {
if (head_table != nullptr && head_table->length < 52) {
return FONT_COMPRESSION_FAILURE();
}
return true;
}
bool ReadCollectionFont(Buffer* file, const uint8_t* data, size_t len,
auto ReadCollectionFont(Buffer* file, const uint8_t* data, size_t len,
Font* font,
std::map<uint32_t, Font::Table*>* all_tables) {
std::map<uint32_t, Font::Table*>* all_tables) -> bool {
if (!file->ReadU32(&font->flavor)) {
return FONT_COMPRESSION_FAILURE();
}
@@ -132,8 +132,8 @@ bool ReadCollectionFont(Buffer* file, const uint8_t* data, size_t len,
return true;
}
bool ReadTrueTypeCollection(Buffer* file, const uint8_t* data, size_t len,
FontCollection* font_collection) {
auto ReadTrueTypeCollection(Buffer* file, const uint8_t* data, size_t len,
FontCollection* font_collection) -> bool {
uint32_t num_fonts;
if (!file->ReadU32(&font_collection->header_version) ||
@@ -151,7 +151,7 @@ bool ReadTrueTypeCollection(Buffer* file, const uint8_t* data, size_t len,
}
font_collection->fonts.resize(offsets.size());
std::vector<Font>::iterator font_it = font_collection->fonts.begin();
auto font_it = font_collection->fonts.begin();
std::map<uint32_t, Font::Table*> all_tables;
for (const auto offset : offsets) {
@@ -165,7 +165,7 @@ bool ReadTrueTypeCollection(Buffer* file, const uint8_t* data, size_t len,
return true;
}
bool ReadFont(const uint8_t* data, size_t len, Font* font) {
auto ReadFont(const uint8_t* data, size_t len, Font* font) -> bool {
Buffer file(data, len);
if (!file.ReadU32(&font->flavor)) {
@@ -178,8 +178,8 @@ bool ReadFont(const uint8_t* data, size_t len, Font* font) {
return ReadTrueTypeFont(&file, data, len, font);
}
bool ReadFontCollection(const uint8_t* data, size_t len,
FontCollection* font_collection) {
auto ReadFontCollection(const uint8_t* data, size_t len,
FontCollection* font_collection) -> bool {
Buffer file(data, len);
if (!file.ReadU32(&font_collection->flavor)) {
@@ -195,7 +195,7 @@ bool ReadFontCollection(const uint8_t* data, size_t len,
return ReadTrueTypeCollection(&file, data, len, font_collection);
}
size_t FontFileSize(const Font& font) {
auto FontFileSize(const Font& font) -> size_t {
size_t max_offset = 12ULL + 16ULL * font.num_tables;
for (const auto& i : font.tables) {
const Font::Table& table = i.second;
@@ -206,7 +206,7 @@ size_t FontFileSize(const Font& font) {
return max_offset;
}
size_t FontCollectionFileSize(const FontCollection& font_collection) {
auto FontCollectionFileSize(const FontCollection& font_collection) -> size_t {
size_t max_offset = 0;
for (auto& font : font_collection.fonts) {
// font file size actually just finds max offset
@@ -215,13 +215,13 @@ size_t FontCollectionFileSize(const FontCollection& font_collection) {
return max_offset;
}
bool WriteFont(const Font& font, uint8_t* dst, size_t dst_size) {
auto WriteFont(const Font& font, uint8_t* dst, size_t dst_size) -> bool {
size_t offset = 0;
return WriteFont(font, &offset, dst, dst_size);
}
bool WriteTableRecord(const Font::Table* table, size_t* offset, uint8_t* dst,
size_t dst_size) {
auto WriteTableRecord(const Font::Table* table, size_t* offset, uint8_t* dst,
size_t dst_size) -> bool {
if (dst_size < *offset + kSfntEntrySize) {
return FONT_COMPRESSION_FAILURE();
}
@@ -235,8 +235,8 @@ bool WriteTableRecord(const Font::Table* table, size_t* offset, uint8_t* dst,
return true;
}
bool WriteTable(const Font::Table& table, size_t* offset, uint8_t* dst,
size_t dst_size) {
auto WriteTable(const Font::Table& table, size_t* offset, uint8_t* dst,
size_t dst_size) -> bool {
if (!WriteTableRecord(&table, offset, dst, dst_size)) {
return false;
}
@@ -258,8 +258,8 @@ bool WriteTable(const Font::Table& table, size_t* offset, uint8_t* dst,
return true;
}
bool WriteFont(const Font& font, size_t* offset, uint8_t* dst,
size_t dst_size) {
auto WriteFont(const Font& font, size_t* offset, uint8_t* dst,
size_t dst_size) -> bool {
if (dst_size < 12ULL + 16ULL * font.num_tables) {
return FONT_COMPRESSION_FAILURE();
}
@@ -281,8 +281,8 @@ bool WriteFont(const Font& font, size_t* offset, uint8_t* dst,
return true;
}
bool WriteFontCollection(const FontCollection& font_collection, uint8_t* dst,
size_t dst_size) {
auto WriteFontCollection(const FontCollection& font_collection, uint8_t* dst,
size_t dst_size) -> bool {
size_t offset = 0;
// It's simpler if this just a simple sfnt
@@ -308,8 +308,7 @@ bool WriteFontCollection(const FontCollection& font_collection, uint8_t* dst,
}
// Write fonts and their offsets.
for (size_t i = 0; i < font_collection.fonts.size(); i++) {
const auto& font = font_collection.fonts[i];
for (const auto & font : font_collection.fonts) {
StoreU32(offset, &offset_table, dst);
if (!WriteFont(font, &offset, dst, dst_size)) {
return false;
@@ -319,10 +318,10 @@ bool WriteFontCollection(const FontCollection& font_collection, uint8_t* dst,
return true;
}
int NumGlyphs(const Font& font) {
auto NumGlyphs(const Font& font) -> int {
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) {
if (head_table == nullptr || loca_table == nullptr || head_table->length < 52) {
return 0;
}
int index_fmt = IndexFormat(font);
@@ -333,27 +332,27 @@ int NumGlyphs(const Font& font) {
return (loca_table->length / loca_record_size) - 1;
}
int IndexFormat(const Font& font) {
auto IndexFormat(const Font& font) -> int {
const Font::Table* head_table = font.FindTable(kHeadTableTag);
if (head_table == NULL) {
if (head_table == nullptr) {
return 0;
}
return head_table->data[51];
}
bool Font::Table::IsReused() const {
return this->reuse_of != NULL;
auto Font::Table::IsReused() const -> bool {
return this->reuse_of != nullptr;
}
bool GetGlyphData(const Font& font, int glyph_index,
const uint8_t** glyph_data, size_t* glyph_size) {
auto GetGlyphData(const Font& font, int glyph_index,
const uint8_t** glyph_data, size_t* glyph_size) -> bool {
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 ||
if (head_table == nullptr || loca_table == nullptr || glyf_table == nullptr ||
head_table->length < 52) {
return FONT_COMPRESSION_FAILURE();
}
@@ -387,8 +386,8 @@ bool GetGlyphData(const Font& font, int glyph_index,
return true;
}
bool RemoveDigitalSignature(Font* font) {
std::map<uint32_t, Font::Table>::iterator it =
auto RemoveDigitalSignature(Font* font) -> bool {
auto it =
font->tables.find(kDsigTableTag);
if (it != font->tables.end()) {
font->tables.erase(it);
+19 -19
View File
@@ -10,8 +10,8 @@
#ifndef WOFF2_FONT_H_
#define WOFF2_FONT_H_
#include <stddef.h>
#include <inttypes.h>
#include <cstddef>
#include <cinttypes>
#include <map>
#include <vector>
@@ -42,13 +42,13 @@ struct Font {
uint8_t flag_byte;
// Is this table reused by a TTC
bool IsReused() const;
[[nodiscard]] auto IsReused() const -> bool;
};
std::map<uint32_t, Table> tables;
std::vector<uint32_t> OutputOrderedTags() const;
[[nodiscard]] auto OutputOrderedTags() const -> std::vector<uint32_t>;
Table* FindTable(uint32_t tag);
const Table* FindTable(uint32_t tag) const;
auto FindTable(uint32_t tag) -> Table*;
[[nodiscard]] auto FindTable(uint32_t tag) const -> const Table*;
};
// Accomodates both singular (OTF, TTF) and collection (TTC) fonts
@@ -63,42 +63,42 @@ struct FontCollection {
// 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. Does NOT support collections.
bool ReadFont(const uint8_t* data, size_t len, Font* font);
auto ReadFont(const uint8_t* data, size_t len, Font* font) -> bool;
// 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. Supports collections.
bool ReadFontCollection(const uint8_t* data, size_t len, FontCollection* fonts);
auto ReadFontCollection(const uint8_t* data, size_t len, FontCollection* fonts) -> bool;
// Returns the file size of the font.
size_t FontFileSize(const Font& font);
size_t FontCollectionFileSize(const FontCollection& font);
auto FontFileSize(const Font& font) -> size_t;
auto FontCollectionFileSize(const FontCollection& font) -> size_t;
// 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);
auto WriteFont(const Font& font, uint8_t* dst, size_t dst_size) -> bool;
// Write the font at a specific offset
bool WriteFont(const Font& font, size_t* offset, uint8_t* dst, size_t dst_size);
auto WriteFont(const Font& font, size_t* offset, uint8_t* dst, size_t dst_size) -> bool;
bool WriteFontCollection(const FontCollection& font_collection, uint8_t* dst,
size_t dst_size);
auto WriteFontCollection(const FontCollection& font_collection, uint8_t* dst,
size_t dst_size) -> bool;
// 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);
auto NumGlyphs(const Font& font) -> int;
// Returns the index format of the font
int IndexFormat(const Font& font);
auto IndexFormat(const Font& font) -> int;
// 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);
auto GetGlyphData(const Font& font, int glyph_index,
const uint8_t** glyph_data, size_t* glyph_size) -> bool;
// Removes the digital signature (DSIG) table
bool RemoveDigitalSignature(Font* font);
auto RemoveDigitalSignature(Font* font) -> bool;
} // namespace woff2
+7 -7
View File
@@ -8,7 +8,7 @@
#include "./glyph.h"
#include <stdlib.h>
#include <cstdlib>
#include <limits>
#include "./buffer.h"
#include "./store_bytes.h"
@@ -28,7 +28,7 @@ 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(Buffer* buffer, Glyph* glyph) {
auto ReadCompositeGlyphData(Buffer* buffer, Glyph* glyph) -> bool {
glyph->have_instructions = false;
glyph->composite_data = buffer->buffer() + buffer->offset();
size_t start_offset = buffer->offset();
@@ -62,7 +62,7 @@ bool ReadCompositeGlyphData(Buffer* buffer, Glyph* glyph) {
return true;
}
bool ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) {
auto ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) -> bool {
Buffer buffer(data, len);
int16_t num_contours;
@@ -224,7 +224,7 @@ void StoreInstructions(const Glyph& glyph, size_t* offset, uint8_t* dst) {
StoreBytes(glyph.instructions_data, glyph.instructions_size, offset, dst);
}
bool StoreEndPtsOfContours(const Glyph& glyph, size_t* offset, uint8_t* dst) {
auto StoreEndPtsOfContours(const Glyph& glyph, size_t* offset, uint8_t* dst) -> bool {
int end_point = -1;
for (const auto& contour : glyph.contours) {
end_point += contour.size();
@@ -237,8 +237,8 @@ bool StoreEndPtsOfContours(const Glyph& glyph, size_t* offset, uint8_t* dst) {
return true;
}
bool StorePoints(const Glyph& glyph, size_t* offset,
uint8_t* dst, size_t dst_size) {
auto StorePoints(const Glyph& glyph, size_t* offset,
uint8_t* dst, size_t dst_size) -> bool {
int last_flag = -1;
int repeat_count = 0;
int last_x = 0;
@@ -333,7 +333,7 @@ bool StorePoints(const Glyph& glyph, size_t* offset,
} // namespace
bool StoreGlyph(const Glyph& glyph, uint8_t* dst, size_t* dst_size) {
auto StoreGlyph(const Glyph& glyph, uint8_t* dst, size_t* dst_size) -> bool {
size_t offset = 0;
if (glyph.composite_data_size > 0) {
// Composite glyph.
+7 -7
View File
@@ -10,8 +10,8 @@
#ifndef WOFF2_GLYPH_H_
#define WOFF2_GLYPH_H_
#include <stddef.h>
#include <inttypes.h>
#include <cstddef>
#include <cinttypes>
#include <vector>
namespace woff2 {
@@ -22,7 +22,7 @@ namespace woff2 {
// is around.
class Glyph {
public:
Glyph() : instructions_size(0), composite_data_size(0) {}
Glyph() {}
// Bounding box.
int16_t x_min;
@@ -31,7 +31,7 @@ class Glyph {
int16_t y_max;
// Instructions.
uint16_t instructions_size;
uint16_t instructions_size{0};
const uint8_t* instructions_data;
// Data model for simple glyphs.
@@ -44,19 +44,19 @@ class Glyph {
// Data for composite glyphs.
const uint8_t* composite_data;
uint32_t composite_data_size;
uint32_t composite_data_size{0};
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);
auto ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) -> bool;
// 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);
auto StoreGlyph(const Glyph& glyph, uint8_t* dst, size_t* dst_size) -> bool;
} // namespace woff2
+23 -23
View File
@@ -8,8 +8,8 @@
#include "./normalize.h"
#include <inttypes.h>
#include <stddef.h>
#include <cinttypes>
#include <cstddef>
#include "./buffer.h"
#include "./port.h"
@@ -36,7 +36,7 @@ void StoreLoca(int index_fmt, uint32_t value, size_t* offset, uint8_t* dst) {
namespace {
bool WriteNormalizedLoca(int index_fmt, int num_glyphs, Font* font) {
auto WriteNormalizedLoca(int index_fmt, int num_glyphs, Font* font) -> bool {
Font::Table* glyf_table = font->FindTable(kGlyfTableTag);
Font::Table* loca_table = font->FindTable(kLocaTableTag);
@@ -44,7 +44,7 @@ bool WriteNormalizedLoca(int index_fmt, int num_glyphs, Font* font) {
loca_table->buffer.resize(Round4(num_glyphs + 1) * glyph_sz);
loca_table->length = (num_glyphs + 1) * glyph_sz;
uint8_t* glyf_dst = num_glyphs ? &glyf_table->buffer[0] : NULL;
uint8_t* glyf_dst = num_glyphs ? &glyf_table->buffer[0] : nullptr;
uint8_t* loca_dst = &loca_table->buffer[0];
uint32_t glyf_offset = 0;
size_t loca_offset = 0;
@@ -74,9 +74,9 @@ bool WriteNormalizedLoca(int index_fmt, int num_glyphs, Font* font) {
StoreLoca(index_fmt, glyf_offset, &loca_offset, loca_dst);
glyf_table->buffer.resize(glyf_offset);
glyf_table->data = glyf_offset ? &glyf_table->buffer[0] : NULL;
glyf_table->data = glyf_offset ? &glyf_table->buffer[0] : nullptr;
glyf_table->length = glyf_offset;
loca_table->data = loca_offset ? &loca_table->buffer[0] : NULL;
loca_table->data = loca_offset ? &loca_table->buffer[0] : nullptr;
return true;
}
@@ -85,9 +85,9 @@ bool WriteNormalizedLoca(int index_fmt, int num_glyphs, Font* font) {
namespace {
bool MakeEditableBuffer(Font* font, int tableTag) {
auto MakeEditableBuffer(Font* font, int tableTag) -> bool {
Font::Table* table = font->FindTable(tableTag);
if (table == NULL) {
if (table == nullptr) {
return FONT_COMPRESSION_FAILURE();
}
if (table->IsReused()) {
@@ -106,19 +106,19 @@ bool MakeEditableBuffer(Font* font, int tableTag) {
} // namespace
bool NormalizeGlyphs(Font* font) {
auto NormalizeGlyphs(Font* font) -> bool {
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) {
if (head_table == nullptr) {
return FONT_COMPRESSION_FAILURE();
}
// If you don't have glyf/loca this transform isn't very interesting
if (loca_table == NULL && glyf_table == NULL) {
if (loca_table == nullptr && glyf_table == nullptr) {
return true;
}
// It would be best if you didn't have just one of glyf/loca
if ((glyf_table == NULL) != (loca_table == NULL)) {
if ((glyf_table == nullptr) != (loca_table == nullptr)) {
return FONT_COMPRESSION_FAILURE();
}
// Must share neither or both loca & glyf
@@ -163,7 +163,7 @@ bool NormalizeGlyphs(Font* font) {
return true;
}
bool NormalizeOffsets(Font* font) {
auto NormalizeOffsets(Font* font) -> bool {
uint32_t offset = 12 + 16 * font->num_tables;
for (auto tag : font->OutputOrderedTags()) {
auto& table = font->tables[tag];
@@ -175,7 +175,7 @@ bool NormalizeOffsets(Font* font) {
namespace {
uint32_t ComputeHeaderChecksum(const Font& font) {
auto ComputeHeaderChecksum(const Font& font) -> uint32_t {
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;
@@ -197,12 +197,12 @@ uint32_t ComputeHeaderChecksum(const Font& font) {
} // namespace
bool FixChecksums(Font* font) {
auto FixChecksums(Font* font) -> bool {
Font::Table* head_table = font->FindTable(kHeadTableTag);
if (head_table == NULL) {
if (head_table == nullptr) {
return FONT_COMPRESSION_FAILURE();
}
if (head_table->reuse_of != NULL) {
if (head_table->reuse_of != nullptr) {
head_table = head_table->reuse_of;
}
if (head_table->length < 12) {
@@ -235,12 +235,12 @@ bool FixChecksums(Font* font) {
}
namespace {
bool MarkTransformed(Font* font) {
auto MarkTransformed(Font* font) -> bool {
Font::Table* head_table = font->FindTable(kHeadTableTag);
if (head_table == NULL) {
if (head_table == nullptr) {
return FONT_COMPRESSION_FAILURE();
}
if (head_table->reuse_of != NULL) {
if (head_table->reuse_of != nullptr) {
head_table = head_table->reuse_of;
}
if (head_table->length < 17) {
@@ -255,7 +255,7 @@ bool MarkTransformed(Font* font) {
} // namespace
bool NormalizeWithoutFixingChecksums(Font* font) {
auto NormalizeWithoutFixingChecksums(Font* font) -> bool {
return (MakeEditableBuffer(font, kHeadTableTag) &&
RemoveDigitalSignature(font) &&
MarkTransformed(font) &&
@@ -263,12 +263,12 @@ bool NormalizeWithoutFixingChecksums(Font* font) {
NormalizeOffsets(font));
}
bool NormalizeFont(Font* font) {
auto NormalizeFont(Font* font) -> bool {
return (NormalizeWithoutFixingChecksums(font) &&
FixChecksums(font));
}
bool NormalizeFontCollection(FontCollection* font_collection) {
auto NormalizeFontCollection(FontCollection* font_collection) -> bool {
if (font_collection->fonts.size() == 1) {
return NormalizeFont(&font_collection->fonts[0]);
}
+5 -5
View File
@@ -19,20 +19,20 @@ struct FontCollection;
// 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);
auto NormalizeOffsets(Font* font) -> bool;
// 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);
auto FixChecksums(Font* font) -> bool;
// 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);
auto NormalizeGlyphs(Font* font) -> bool;
// Performs all of the normalization steps above.
bool NormalizeFont(Font* font);
bool NormalizeFontCollection(FontCollection* font_collection);
auto NormalizeFont(Font* font) -> bool;
auto NormalizeFontCollection(FontCollection* font_collection) -> bool;
} // namespace woff2
+3 -3
View File
@@ -9,13 +9,13 @@
#ifndef WOFF2_PORT_H_
#define WOFF2_PORT_H_
#include <assert.h>
#include <cassert>
namespace woff2 {
typedef unsigned int uint32;
using uint32 = unsigned int;
inline int Log2Floor(uint32 n) {
inline auto Log2Floor(uint32 n) -> int {
#if defined(__GNUC__)
return n == 0 ? -1 : 31 ^ __builtin_clz(n);
#else
+1 -1
View File
@@ -15,7 +15,7 @@ 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) {
template<typename T> auto Round4(T value) -> T {
if (std::numeric_limits<T>::max() - value < 3) {
return value;
}
+5 -5
View File
@@ -10,15 +10,15 @@
#ifndef WOFF2_STORE_BYTES_H_
#define WOFF2_STORE_BYTES_H_
#include <inttypes.h>
#include <stddef.h>
#include <string.h>
#include <cinttypes>
#include <cstddef>
#include <cstring>
#include "./port.h"
namespace woff2 {
inline size_t StoreU32(uint8_t* dst, size_t offset, uint32_t x) {
inline auto StoreU32(uint8_t* dst, size_t offset, uint32_t x) -> size_t {
dst[offset] = x >> 24;
dst[offset + 1] = x >> 16;
dst[offset + 2] = x >> 8;
@@ -26,7 +26,7 @@ inline size_t StoreU32(uint8_t* dst, size_t offset, uint32_t x) {
return offset + 4;
}
inline size_t Store16(uint8_t* dst, size_t offset, int x) {
inline auto Store16(uint8_t* dst, size_t offset, int x) -> size_t {
#if defined(WOFF_LITTLE_ENDIAN)
*reinterpret_cast<uint16_t*>(dst + offset) =
((x & 0xFF) << 8) | ((x & 0xFF00) >> 8);
+1 -1
View File
@@ -11,7 +11,7 @@
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)
#define TAG(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
const uint32_t kKnownTags[63] = {
TAG('c', 'm', 'a', 'p'), // 0
+1 -1
View File
@@ -9,7 +9,7 @@
#ifndef WOFF2_TABLE_TAGS_H_
#define WOFF2_TABLE_TAGS_H_
#include <inttypes.h>
#include <cinttypes>
namespace woff2 {
+30 -21
View File
@@ -24,15 +24,16 @@ 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;
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 (size_t i = 0; i < in.size(); ++i) {
out->push_back(in[i]);
for (unsigned char i : in) {
out->push_back(i);
}
}
@@ -57,7 +58,7 @@ class GlyfEncoder {
bbox_bitmap_.resize(((num_glyphs + 31) >> 5) << 2);
}
bool Encode(int glyph_id, const Glyph& glyph) {
auto Encode(int glyph_id, const Glyph& glyph) -> bool {
if (glyph.composite_data_size > 0) {
WriteCompositeGlyph(glyph_id, glyph);
} else if (glyph.contours.size() > 0) {
@@ -96,7 +97,7 @@ class GlyfEncoder {
glyph.instructions_data, glyph.instructions_size);
}
bool ShouldWriteSimpleGlyphBbox(const Glyph& glyph) {
auto ShouldWriteSimpleGlyphBbox(const Glyph& glyph) -> bool {
if (glyph.contours.empty() || glyph.contours[0].empty()) {
return glyph.x_min || glyph.y_min || glyph.x_max || glyph.y_max;
}
@@ -107,21 +108,29 @@ class GlyfEncoder {
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;
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;
}
}
}
if (glyph.x_min != x_min)
if (glyph.x_min != x_min) {
return true;
if (glyph.y_min != y_min)
}
if (glyph.y_min != y_min) {
return true;
if (glyph.x_max != x_max)
}
if (glyph.x_max != x_max) {
return true;
if (glyph.y_max != y_max)
}
if (glyph.y_max != y_max) {
return true;
}
return false;
}
@@ -227,17 +236,17 @@ class GlyfEncoder {
} // namespace
bool TransformGlyfAndLocaTables(Font* font) {
auto TransformGlyfAndLocaTables(Font* font) -> bool {
// no transform for CFF
const Font::Table* glyf_table = font->FindTable(kGlyfTableTag);
const Font::Table* loca_table = font->FindTable(kLocaTableTag);
// If you don't have glyf/loca this transform isn't very interesting
if (loca_table == NULL && glyf_table == NULL) {
if (loca_table == nullptr && glyf_table == nullptr) {
return true;
}
// It would be best if you didn't have just one of glyf/loca
if ((glyf_table == NULL) != (loca_table == NULL)) {
if ((glyf_table == nullptr) != (loca_table == nullptr)) {
return FONT_COMPRESSION_FAILURE();
}
// Must share neither or both loca & glyf
@@ -266,7 +275,7 @@ bool TransformGlyfAndLocaTables(Font* font) {
encoder.GetTransformedGlyfBytes(&transformed_glyf->buffer);
const Font::Table* head_table = font->FindTable(kHeadTableTag);
if (head_table == NULL || head_table->length < 52) {
if (head_table == nullptr || head_table->length < 52) {
return FONT_COMPRESSION_FAILURE();
}
transformed_glyf->buffer[7] = head_table->data[51]; // index_format
@@ -277,25 +286,25 @@ bool TransformGlyfAndLocaTables(Font* font) {
transformed_loca->tag = kLocaTableTag ^ 0x80808080;
transformed_loca->length = 0;
transformed_loca->data = NULL;
transformed_loca->data = nullptr;
return true;
}
// See https://www.microsoft.com/typography/otspec/hmtx.htm
// See WOFF2 spec, 5.4. Transformed hmtx table format
bool TransformHmtxTable(Font* font) {
auto TransformHmtxTable(Font* font) -> bool {
const Font::Table* glyf_table = font->FindTable(kGlyfTableTag);
const Font::Table* hmtx_table = font->FindTable(kHmtxTableTag);
const Font::Table* hhea_table = font->FindTable(kHheaTableTag);
// If you don't have hmtx or a glyf not much is going to happen here
if (hmtx_table == NULL || glyf_table == NULL) {
if (hmtx_table == nullptr || glyf_table == nullptr) {
return true;
}
// hmtx without hhea doesn't make sense
if (hhea_table == NULL) {
if (hhea_table == nullptr) {
return FONT_COMPRESSION_FAILURE();
}
+2 -2
View File
@@ -16,10 +16,10 @@ 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);
auto TransformGlyfAndLocaTables(Font* font) -> bool;
// Apply transformation to hmtx table if applicable for this font.
bool TransformHmtxTable(Font* font);
auto TransformHmtxTable(Font* font) -> bool;
} // namespace woff2
+6 -5
View File
@@ -10,7 +10,7 @@
namespace woff2 {
size_t Size255UShort(uint16_t value) {
auto Size255UShort(uint16_t value) -> size_t {
size_t result = 3;
if (value < 253) {
result = 1;
@@ -47,7 +47,7 @@ void Store255UShort(int val, size_t* offset, uint8_t* dst) {
}
// Based on section 6.1.1 of MicroType Express draft spec
bool Read255UShort(Buffer* buf, unsigned int* value) {
auto Read255UShort(Buffer* buf, unsigned int* value) -> bool {
static const int kWordCode = 253;
static const int kOneMoreByteCode2 = 254;
static const int kOneMoreByteCode1 = 255;
@@ -83,7 +83,7 @@ bool Read255UShort(Buffer* buf, unsigned int* value) {
}
}
bool ReadBase128(Buffer* buf, uint32_t* value) {
auto ReadBase128(Buffer* buf, uint32_t* value) -> bool {
uint32_t result = 0;
for (size_t i = 0; i < 5; ++i) {
uint8_t code = 0;
@@ -108,9 +108,10 @@ bool ReadBase128(Buffer* buf, uint32_t* value) {
return FONT_COMPRESSION_FAILURE();
}
size_t Base128Size(size_t n) {
auto Base128Size(size_t n) -> size_t {
size_t size = 1;
for (; n >= 128; n >>= 7) ++size;
for (; n >= 128; n >>= 7) { ++size;
}
return size;
}
+5 -5
View File
@@ -9,19 +9,19 @@
#ifndef WOFF2_VARIABLE_LENGTH_H_
#define WOFF2_VARIABLE_LENGTH_H_
#include <inttypes.h>
#include <cinttypes>
#include <vector>
#include "./buffer.h"
namespace woff2 {
size_t Size255UShort(uint16_t value);
bool Read255UShort(Buffer* buf, unsigned int* value);
auto Size255UShort(uint16_t value) -> size_t;
auto Read255UShort(Buffer* buf, unsigned int* value) -> bool;
void Write255UShort(std::vector<uint8_t>* out, int value);
void Store255UShort(int val, size_t* offset, uint8_t* dst);
size_t Base128Size(size_t n);
bool ReadBase128(Buffer* buf, uint32_t* value);
auto Base128Size(size_t n) -> size_t;
auto ReadBase128(Buffer* buf, uint32_t* value) -> bool;
void StoreBase128(size_t len, size_t* offset, uint8_t* dst);
} // namespace woff2
+2 -2
View File
@@ -15,7 +15,7 @@
namespace woff2 {
uint32_t ComputeULongSum(const uint8_t* buf, size_t size) {
auto ComputeULongSum(const uint8_t* buf, size_t size) -> uint32_t {
uint32_t checksum = 0;
size_t aligned_size = size & ~3;
for (size_t i = 0; i < aligned_size; i += 4) {
@@ -43,7 +43,7 @@ uint32_t ComputeULongSum(const uint8_t* buf, size_t size) {
return checksum;
}
size_t CollectionHeaderSize(uint32_t header_version, uint32_t num_fonts) {
auto CollectionHeaderSize(uint32_t header_version, uint32_t num_fonts) -> size_t {
size_t size = 0;
if (header_version == 0x00020000) {
size += 12; // ulDsig{Tag,Length,Offset}
+5 -5
View File
@@ -9,8 +9,8 @@
#ifndef WOFF2_WOFF2_COMMON_H_
#define WOFF2_WOFF2_COMMON_H_
#include <stddef.h>
#include <inttypes.h>
#include <cstddef>
#include <cinttypes>
#include <string>
@@ -45,7 +45,7 @@ struct Table {
uint32_t dst_length;
const uint8_t* dst_data;
bool operator<(const Table& other) const {
auto operator<(const Table& other) const -> bool {
return tag < other.tag;
}
};
@@ -54,10 +54,10 @@ struct Table {
// Size of the collection header. 0 if version indicates this isn't a
// collection. Ref http://www.microsoft.com/typography/otspec/otff.htm,
// True Type Collections
size_t CollectionHeaderSize(uint32_t header_version, uint32_t num_fonts);
auto CollectionHeaderSize(uint32_t header_version, uint32_t num_fonts) -> size_t;
// Compute checksum over size bytes of buf
uint32_t ComputeULongSum(const uint8_t* buf, size_t size);
auto ComputeULongSum(const uint8_t* buf, size_t size) -> uint32_t;
} // namespace woff2
+4 -4
View File
@@ -12,7 +12,7 @@
#include <woff2/encode.h>
int main(int argc, char **argv) {
auto main(int argc, char **argv) -> int {
using std::string;
if (argc != 2) {
@@ -21,15 +21,15 @@ int main(int argc, char **argv) {
}
string filename(argv[1]);
string outfilename = filename.substr(0, filename.find_last_of(".")) + ".woff2";
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());
const auto* 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]);
auto* output_data = reinterpret_cast<uint8_t*>(&output[0]);
woff2::WOFF2Params params;
if (!woff2::ConvertTTFToWOFF2(input_data, input.size(),
+45 -45
View File
@@ -8,11 +8,12 @@
#include <woff2/decode.h>
#include <stdlib.h>
#include <cstdlib>
#include <algorithm>
#include <complex>
#include <cstring>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <map>
@@ -106,12 +107,12 @@ struct RebuildMetadata {
std::map<std::pair<uint32_t, uint32_t>, uint32_t> checksums;
};
int WithSign(int flag, int baseval) {
auto WithSign(int flag, int baseval) -> int {
// Precondition: 0 <= baseval < 65536 (to avoid integer overflow)
return (flag & 1) ? baseval : -baseval;
}
bool _SafeIntAddition(int a, int b, int* result) {
auto _SafeIntAddition(int a, int b, int* result) -> bool {
if (PREDICT_FALSE(
((a > 0) && (b > std::numeric_limits<int>::max() - a)) ||
((a < 0) && (b < std::numeric_limits<int>::min() - a)))) {
@@ -121,8 +122,8 @@ bool _SafeIntAddition(int a, int b, int* result) {
return true;
}
bool TripletDecode(const uint8_t* flags_in, const uint8_t* in, size_t in_size,
unsigned int n_points, Point* result, size_t* in_bytes_consumed) {
auto TripletDecode(const uint8_t* flags_in, const uint8_t* in, size_t in_size,
unsigned int n_points, Point* result, size_t* in_bytes_consumed) -> bool {
int x = 0;
int y = 0;
@@ -190,9 +191,9 @@ bool TripletDecode(const uint8_t* flags_in, const uint8_t* in, size_t in_size,
// This function stores just the point data. On entry, dst points to the
// beginning of a simple glyph. Returns true on success.
bool StorePoints(unsigned int n_points, const Point* points,
auto StorePoints(unsigned int n_points, const Point* points,
unsigned int n_contours, unsigned int instruction_length,
uint8_t* dst, size_t dst_size, size_t* glyph_size) {
uint8_t* dst, size_t dst_size, size_t* glyph_size) -> bool {
// I believe that n_contours < 65536, in which case this is safe. However, a
// comment and/or an assert would be good.
unsigned int flag_offset = kEndPtsOfContoursOffset + 2 * n_contours + 2 +
@@ -320,8 +321,8 @@ void ComputeBbox(unsigned int n_points, const Point* points, uint8_t* dst) {
}
bool SizeOfComposite(Buffer composite_stream, size_t* size,
bool* have_instructions) {
auto SizeOfComposite(Buffer composite_stream, size_t* size,
bool* have_instructions) -> bool {
size_t start_offset = composite_stream.offset();
bool we_have_instructions = false;
@@ -355,7 +356,7 @@ bool SizeOfComposite(Buffer composite_stream, size_t* size,
return true;
}
bool Pad4(WOFF2Out* out) {
auto Pad4(WOFF2Out* out) -> bool {
uint8_t zeroes[] = {0, 0, 0};
if (PREDICT_FALSE(out->Size() + 3 < out->Size())) {
return FONT_COMPRESSION_FAILURE();
@@ -370,8 +371,8 @@ bool Pad4(WOFF2Out* out) {
}
// Build TrueType loca table
bool StoreLoca(const std::vector<uint32_t>& loca_values, int index_format,
uint32_t* checksum, WOFF2Out* out) {
auto StoreLoca(const std::vector<uint32_t>& loca_values, int index_format,
uint32_t* checksum, WOFF2Out* out) -> bool {
// TODO(user) figure out what index format to use based on whether max
// offset fits into uint16_t or not
const uint64_t loca_size = loca_values.size();
@@ -382,8 +383,7 @@ bool StoreLoca(const std::vector<uint32_t>& loca_values, int index_format,
std::vector<uint8_t> loca_content(loca_size * offset_size);
uint8_t* dst = &loca_content[0];
size_t offset = 0;
for (size_t i = 0; i < loca_values.size(); ++i) {
uint32_t value = loca_values[i];
for (unsigned int value : loca_values) {
if (index_format) {
offset = StoreU32(dst, offset, value);
} else {
@@ -398,10 +398,10 @@ bool StoreLoca(const std::vector<uint32_t>& loca_values, int index_format,
}
// Reconstruct entire glyf table based on transformed original
bool ReconstructGlyf(const uint8_t* data, Table* glyf_table,
auto ReconstructGlyf(const uint8_t* data, Table* glyf_table,
uint32_t* glyf_checksum, Table * loca_table,
uint32_t* loca_checksum, WOFF2FontInfo* info,
WOFF2Out* out) {
WOFF2Out* out) -> bool {
static const int kNumSubStreams = 7;
Buffer file(data, glyf_table->transform_length);
uint32_t version;
@@ -548,7 +548,7 @@ bool ReconstructGlyf(const uint8_t* data, Table* glyf_table,
size_t triplet_bytes_consumed = 0;
if (points_size < total_n_points) {
points_size = total_n_points;
points.reset(new Point[points_size]);
points = std::make_unique<Point[]>(points_size);
}
if (PREDICT_FALSE(!TripletDecode(flags_buf, triplet_buf, triplet_size,
total_n_points, points.get(), &triplet_bytes_consumed))) {
@@ -650,18 +650,18 @@ bool ReconstructGlyf(const uint8_t* data, Table* glyf_table,
return true;
}
Table* FindTable(std::vector<Table*>* tables, uint32_t tag) {
auto FindTable(std::vector<Table*>* tables, uint32_t tag) -> Table* {
for (Table* table : *tables) {
if (table->tag == tag) {
return table;
}
}
return NULL;
return nullptr;
}
// Get numberOfHMetrics, https://www.microsoft.com/typography/otspec/hhea.htm
bool ReadNumHMetrics(const uint8_t* data, size_t data_size,
uint16_t* num_hmetrics) {
auto ReadNumHMetrics(const uint8_t* data, size_t data_size,
uint16_t* num_hmetrics) -> bool {
// Skip 34 to reach 'hhea' numberOfHMetrics
Buffer buffer(data, data_size);
if (PREDICT_FALSE(!buffer.Skip(34) || !buffer.ReadU16(num_hmetrics))) {
@@ -671,13 +671,13 @@ bool ReadNumHMetrics(const uint8_t* data, size_t data_size,
}
// http://dev.w3.org/webfonts/WOFF2/spec/Overview.html#hmtx_table_format
bool ReconstructTransformedHmtx(const uint8_t* transformed_buf,
auto ReconstructTransformedHmtx(const uint8_t* transformed_buf,
size_t transformed_size,
uint16_t num_glyphs,
uint16_t num_hmetrics,
const std::vector<int16_t>& x_mins,
uint32_t* checksum,
WOFF2Out* out) {
WOFF2Out* out) -> bool {
Buffer hmtx_buff_in(transformed_buf, transformed_size);
uint8_t hmtx_flags;
@@ -768,8 +768,8 @@ bool ReconstructTransformedHmtx(const uint8_t* transformed_buf,
return true;
}
bool Woff2Uncompress(uint8_t* dst_buf, size_t dst_size,
const uint8_t* src_buf, size_t src_size) {
auto Woff2Uncompress(uint8_t* dst_buf, size_t dst_size,
const uint8_t* src_buf, size_t src_size) -> bool {
size_t uncompressed_size = dst_size;
BrotliDecoderResult result = BrotliDecoderDecompress(
src_size, src_buf, &uncompressed_size, dst_buf);
@@ -780,8 +780,8 @@ bool Woff2Uncompress(uint8_t* dst_buf, size_t dst_size,
return true;
}
bool ReadTableDirectory(Buffer* file, std::vector<Table>* tables,
size_t num_tables) {
auto ReadTableDirectory(Buffer* file, std::vector<Table>* tables,
size_t num_tables) -> bool {
uint32_t src_offset = 0;
for (size_t i = 0; i < num_tables; ++i) {
Table* table = &(*tables)[i];
@@ -839,8 +839,8 @@ bool ReadTableDirectory(Buffer* file, std::vector<Table>* tables,
}
// Writes a single Offset Table entry
size_t StoreOffsetTable(uint8_t* result, size_t offset, uint32_t flavor,
uint16_t num_tables) {
auto StoreOffsetTable(uint8_t* result, size_t offset, uint32_t flavor,
uint16_t num_tables) -> size_t {
offset = StoreU32(result, offset, flavor); // sfnt version
offset = Store16(result, offset, num_tables); // num_tables
unsigned max_pow2 = 0;
@@ -855,7 +855,7 @@ size_t StoreOffsetTable(uint8_t* result, size_t offset, uint32_t flavor,
return offset;
}
size_t StoreTableEntry(uint8_t* result, uint32_t offset, uint32_t tag) {
auto StoreTableEntry(uint8_t* result, uint32_t offset, uint32_t tag) -> size_t {
offset = StoreU32(result, offset, tag);
offset = StoreU32(result, offset, 0);
offset = StoreU32(result, offset, 0);
@@ -864,7 +864,7 @@ size_t StoreTableEntry(uint8_t* result, uint32_t offset, uint32_t tag) {
}
// First table goes after all the headers, table directory, etc
uint64_t ComputeOffsetToFirstTable(const WOFF2Header& hdr) {
auto ComputeOffsetToFirstTable(const WOFF2Header& hdr) -> uint64_t {
uint64_t offset = kSfntHeaderSize +
kSfntEntrySize * static_cast<uint64_t>(hdr.num_tables);
if (hdr.header_version) {
@@ -877,7 +877,7 @@ uint64_t ComputeOffsetToFirstTable(const WOFF2Header& hdr) {
return offset;
}
std::vector<Table*> Tables(WOFF2Header* hdr, size_t font_index) {
auto Tables(WOFF2Header* hdr, size_t font_index) -> std::vector<Table*> {
std::vector<Table*> tables;
if (PREDICT_FALSE(hdr->header_version)) {
for (auto index : hdr->ttc_fonts[font_index].table_indices) {
@@ -893,12 +893,12 @@ std::vector<Table*> Tables(WOFF2Header* hdr, size_t font_index) {
// Offset tables assumed to have been written in with 0's initially.
// WOFF2Header isn't const so we can use [] instead of at() (which upsets FF)
bool ReconstructFont(uint8_t* transformed_buf,
auto ReconstructFont(uint8_t* transformed_buf,
const uint32_t transformed_buf_size,
RebuildMetadata* metadata,
WOFF2Header* hdr,
size_t font_index,
WOFF2Out* out) {
WOFF2Out* out) -> bool {
size_t dest_offset = out->Size();
uint8_t table_entry[12];
WOFF2FontInfo* info = &metadata->font_infos[font_index];
@@ -915,7 +915,7 @@ bool ReconstructFont(uint8_t* transformed_buf,
return FONT_COMPRESSION_FAILURE();
}
if (glyf_table != NULL) {
if (glyf_table != nullptr) {
if (PREDICT_FALSE((glyf_table->flags & kWoff2FlagsTransform)
!= (loca_table->flags & kWoff2FlagsTransform))) {
#ifdef FONT_COMPRESSION_BIN
@@ -1043,7 +1043,7 @@ bool ReconstructFont(uint8_t* transformed_buf,
return true;
}
bool ReadWOFF2Header(const uint8_t* data, size_t length, WOFF2Header* hdr) {
auto ReadWOFF2Header(const uint8_t* data, size_t length, WOFF2Header* hdr) -> bool {
Buffer file(data, length);
uint32_t signature;
@@ -1226,8 +1226,8 @@ bool ReadWOFF2Header(const uint8_t* data, size_t length, WOFF2Header* hdr) {
}
// Write everything before the actual table data
bool WriteHeaders(const uint8_t* data, size_t length, RebuildMetadata* metadata,
WOFF2Header* hdr, WOFF2Out* out) {
auto WriteHeaders(RebuildMetadata* metadata,
WOFF2Header* hdr, WOFF2Out* out) -> bool {
std::vector<uint8_t> output(ComputeOffsetToFirstTable(*hdr), 0);
// Re-order tables in output (OTSpec) order
@@ -1309,7 +1309,7 @@ bool WriteHeaders(const uint8_t* data, size_t length, RebuildMetadata* metadata,
} // namespace
size_t ComputeWOFF2FinalSize(const uint8_t* data, size_t length) {
auto ComputeWOFF2FinalSize(const uint8_t* data, size_t length) -> size_t {
Buffer file(data, length);
uint32_t total_length;
@@ -1320,25 +1320,25 @@ size_t ComputeWOFF2FinalSize(const uint8_t* data, size_t length) {
return total_length;
}
bool ConvertWOFF2ToTTF(uint8_t *result, size_t result_length,
const uint8_t *data, size_t length) {
auto ConvertWOFF2ToTTF(uint8_t *result, size_t result_length,
const uint8_t *data, size_t length) -> bool {
WOFF2MemoryOut out(result, result_length);
return ConvertWOFF2ToTTF(data, length, &out);
}
bool ConvertWOFF2ToTTF(const uint8_t* data, size_t length,
WOFF2Out* out) {
auto ConvertWOFF2ToTTF(const uint8_t* data, size_t length,
WOFF2Out* out) -> bool {
RebuildMetadata metadata;
WOFF2Header hdr;
if (!ReadWOFF2Header(data, length, &hdr)) {
return FONT_COMPRESSION_FAILURE();
}
if (!WriteHeaders(data, length, &metadata, &hdr, out)) {
if (!WriteHeaders(&metadata, &hdr, out)) {
return FONT_COMPRESSION_FAILURE();
}
const float compression_ratio = (float) hdr.uncompressed_size / length;
const float compression_ratio = static_cast<float>(hdr.uncompressed_size) / length;
if (compression_ratio > kMaxPlausibleCompressionRatio) {
#ifdef FONT_COMPRESSION_BIN
fprintf(stderr, "Implausible compression ratio %.01f\n", compression_ratio);
+3 -3
View File
@@ -13,7 +13,7 @@
#include <woff2/decode.h>
int main(int argc, char **argv) {
auto main(int argc, char **argv) -> int {
using std::string;
if (argc != 2) {
@@ -22,11 +22,11 @@ int main(int argc, char **argv) {
}
string filename(argv[1]);
string outfilename = filename.substr(0, filename.find_last_of(".")) + ".ttf";
string outfilename = filename.substr(0, filename.find_last_of('.')) + ".ttf";
// Note: update woff2_dec_fuzzer_new_entry.cc if this pattern changes.
string input = woff2::GetFileContent(filename);
const uint8_t* raw_input = reinterpret_cast<const uint8_t*>(input.data());
const auto* raw_input = reinterpret_cast<const uint8_t*>(input.data());
string output(std::min(woff2::ComputeWOFF2FinalSize(raw_input, input.size()),
woff2::kDefaultMaxSize), 0);
woff2::WOFF2StringOut out(&output);
+43 -34
View File
@@ -8,7 +8,7 @@
#include <woff2/encode.h>
#include <stdlib.h>
#include <cstdlib>
#include <complex>
#include <cstring>
#include <limits>
@@ -38,8 +38,8 @@ using std::vector;
const size_t kWoff2HeaderSize = 48;
const size_t kWoff2EntrySize = 20;
bool Compress(const uint8_t* data, const size_t len, uint8_t* result,
uint32_t* result_len, BrotliEncoderMode mode, int quality) {
auto Compress(const uint8_t* data, const size_t len, uint8_t* result,
uint32_t* result_len, BrotliEncoderMode mode, int quality) -> bool {
size_t compressed_len = *result_len;
if (BrotliEncoderCompress(quality, BROTLI_DEFAULT_WINDOW, mode, len, data,
&compressed_len, result) == 0) {
@@ -49,23 +49,24 @@ bool Compress(const uint8_t* data, const size_t len, uint8_t* result,
return true;
}
bool Woff2Compress(const uint8_t* data, const size_t len,
auto Woff2Compress(const uint8_t* data, const size_t len,
uint8_t* result, uint32_t* result_len,
int quality) {
int quality) -> bool {
return Compress(data, len, result, result_len,
BROTLI_MODE_FONT, quality);
}
bool TextCompress(const uint8_t* data, const size_t len,
auto TextCompress(const uint8_t* data, const size_t len,
uint8_t* result, uint32_t* result_len,
int quality) {
int quality) -> bool {
return Compress(data, len, result, result_len,
BROTLI_MODE_TEXT, quality);
}
int KnownTableIndex(uint32_t tag) {
auto KnownTableIndex(uint32_t tag) -> int {
for (int i = 0; i < 63; ++i) {
if (tag == kKnownTags[i]) return i;
if (tag == kKnownTags[i]) { return i;
}
}
return 63;
}
@@ -85,7 +86,7 @@ void StoreTableEntry(const Table& table, size_t* offset, uint8_t* dst) {
}
}
size_t TableEntrySize(const Table& table) {
auto TableEntrySize(const Table& table) -> size_t {
uint8_t flag_byte = KnownTableIndex(table.tag);
size_t size = ((flag_byte & 0x3f) != 0x3f) ? 1 : 5;
size += Base128Size(table.src_length);
@@ -95,12 +96,12 @@ size_t TableEntrySize(const Table& table) {
return size;
}
size_t ComputeWoff2Length(const FontCollection& font_collection,
auto ComputeWoff2Length(const FontCollection& font_collection,
const std::vector<Table>& tables,
std::map<std::pair<uint32_t, uint32_t>, uint16_t>
index_by_tag_offset,
size_t compressed_data_length,
size_t extended_metadata_length) {
size_t extended_metadata_length) -> size_t {
size_t size = kWoff2HeaderSize;
for (const auto& table : tables) {
@@ -119,7 +120,8 @@ size_t ComputeWoff2Length(const FontCollection& font_collection,
for (const auto& entry : font.tables) {
const Font::Table& table = entry.second;
// no collection entry for xform table
if (table.tag & 0x80808080) continue;
if (table.tag & 0x80808080) { continue;
}
std::pair<uint32_t, uint32_t> tag_offset(table.tag, table.offset);
uint16_t table_index = index_by_tag_offset[tag_offset];
@@ -136,19 +138,21 @@ size_t ComputeWoff2Length(const FontCollection& font_collection,
return size;
}
size_t ComputeUncompressedLength(const Font& font) {
auto ComputeUncompressedLength(const Font& font) -> size_t {
// sfnt header + offset table
size_t size = 12 + 16 * font.num_tables;
for (const auto& entry : font.tables) {
const Font::Table& table = entry.second;
if (table.tag & 0x80808080) continue; // xform tables don't stay
if (table.IsReused()) continue; // don't have to pay twice
if (table.tag & 0x80808080) { continue; // xform tables don't stay
}
if (table.IsReused()) { continue; // don't have to pay twice
}
size += Round4(table.length);
}
return size;
}
size_t ComputeUncompressedLength(const FontCollection& font_collection) {
auto ComputeUncompressedLength(const FontCollection& font_collection) -> size_t {
if (font_collection.flavor != kTtcFontFlavor) {
return ComputeUncompressedLength(font_collection.fonts[0]);
}
@@ -160,7 +164,7 @@ size_t ComputeUncompressedLength(const FontCollection& font_collection) {
return size;
}
size_t ComputeTotalTransformLength(const Font& font) {
auto ComputeTotalTransformLength(const Font& font) -> size_t {
size_t total = 0;
for (const auto& i : font.tables) {
const Font::Table& table = i.second;
@@ -178,12 +182,12 @@ size_t ComputeTotalTransformLength(const Font& font) {
} // namespace
size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length) {
auto MaxWOFF2CompressedSize(const uint8_t* data, size_t length) -> size_t {
return MaxWOFF2CompressedSize(data, length, "");
}
size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length,
const string& extended_metadata) {
auto MaxWOFF2CompressedSize(const uint8_t* /*data*/, size_t length,
const string& extended_metadata) -> size_t {
// 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
@@ -191,11 +195,11 @@ size_t MaxWOFF2CompressedSize(const uint8_t* data, size_t length,
return length + 1024 + extended_metadata.length();
}
uint32_t CompressedBufferSize(uint32_t original_size) {
auto CompressedBufferSize(uint32_t original_size) -> uint32_t {
return 1.2 * original_size + 10240;
}
bool TransformFontCollection(FontCollection* font_collection) {
auto TransformFontCollection(FontCollection* font_collection) -> bool {
for (auto& font : font_collection->fonts) {
if (!TransformGlyfAndLocaTables(&font)) {
#ifdef FONT_COMPRESSION_BIN
@@ -208,16 +212,16 @@ bool TransformFontCollection(FontCollection* font_collection) {
return true;
}
bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
uint8_t *result, size_t *result_length) {
auto ConvertTTFToWOFF2(const uint8_t *data, size_t length,
uint8_t *result, size_t *result_length) -> bool {
WOFF2Params params;
return ConvertTTFToWOFF2(data, length, result, result_length,
params);
}
bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
auto ConvertTTFToWOFF2(const uint8_t *data, size_t length,
uint8_t *result, size_t *result_length,
const WOFF2Params& params) {
const WOFF2Params& params) -> bool {
FontCollection font_collection;
if (!ReadFontCollection(data, length, &font_collection)) {
#ifdef FONT_COMPRESSION_BIN
@@ -266,10 +270,13 @@ bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
for (const auto& font : font_collection.fonts) {
for (const auto tag : font.OutputOrderedTags()) {
const Font::Table& original = font.tables.at(tag);
if (original.IsReused()) continue;
if (tag & 0x80808080) continue;
if (original.IsReused()) { continue;
}
if (tag & 0x80808080) { continue;
}
const Font::Table* table_to_store = font.FindTable(tag ^ 0x80808080);
if (table_to_store == NULL) table_to_store = &original;
if (table_to_store == nullptr) { table_to_store = &original;
}
StoreBytes(table_to_store->data, table_to_store->length,
&transform_offset, &transform_buf[0]);
@@ -299,7 +306,7 @@ bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
std::vector<uint8_t> compressed_metadata_buf(compressed_metadata_buf_length);
if (params.extended_metadata.length() > 0) {
if (!TextCompress((const uint8_t*)params.extended_metadata.data(),
if (!TextCompress(reinterpret_cast<const uint8_t*>(params.extended_metadata.data()),
params.extended_metadata.length(),
compressed_metadata_buf.data(),
&compressed_metadata_buf_length,
@@ -339,7 +346,7 @@ bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
const uint8_t* transformed_data = src_table.data;
const Font::Table* transformed_table =
font.FindTable(src_table.tag ^ 0x80808080);
if (transformed_table != NULL) {
if (transformed_table != nullptr) {
table.flags = transformed_table->flag_byte;
table.flags |= kWoff2FlagsTransform;
table.transform_length = transformed_table->length;
@@ -410,7 +417,8 @@ bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
uint16_t num_tables = 0;
for (const auto& entry : font.tables) {
const Font::Table& table = entry.second;
if (table.tag & 0x80808080) continue; // don't write xform tables
if (table.tag & 0x80808080) { continue; // don't write xform tables
}
num_tables++;
}
Store255UShort(num_tables, &offset, result);
@@ -418,7 +426,8 @@ bool ConvertTTFToWOFF2(const uint8_t *data, size_t length,
StoreU32(font.flavor, &offset, result);
for (const auto& entry : font.tables) {
const Font::Table& table = entry.second;
if (table.tag & 0x80808080) continue; // don't write xform tables
if (table.tag & 0x80808080) { continue; // don't write xform tables
}
// for reused tables, only the original has an updated offset
uint32_t table_offset =
+53 -28
View File
@@ -15,7 +15,7 @@
#include "./table_tags.h"
#include "./variable_length.h"
std::string PrintTag(int tag) {
auto PrintTag(int tag) -> std::string {
if (tag & 0x80808080) {
return std::string("_xfm"); // print _xfm for xform tables (else garbage)
}
@@ -28,7 +28,7 @@ std::string PrintTag(int tag) {
return std::string(printable, 4);
}
int main(int argc, char **argv) {
auto main(int argc, char **argv) -> int {
using std::string;
if (argc != 2) {
@@ -37,7 +37,7 @@ int main(int argc, char **argv) {
}
string filename(argv[1]);
string outfilename = filename.substr(0, filename.find_last_of(".")) + ".woff2";
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);
@@ -49,20 +49,34 @@ int main(int argc, char **argv) {
uint32_t signature, flavor, length, totalSfntSize, totalCompressedSize;
uint32_t metaOffset, metaLength, metaOrigLength, privOffset, privLength;
uint16_t num_tables, reserved, major, minor;
if (!file.ReadU32(&signature)) return 1;
if (!file.ReadU32(&flavor)) return 1;
if (!file.ReadU32(&length)) return 1;
if (!file.ReadU16(&num_tables)) return 1;
if (!file.ReadU16(&reserved)) return 1;
if (!file.ReadU32(&totalSfntSize)) return 1;
if (!file.ReadU32(&totalCompressedSize)) return 1;
if (!file.ReadU16(&major)) return 1;
if (!file.ReadU16(&minor)) return 1;
if (!file.ReadU32(&metaOffset)) return 1;
if (!file.ReadU32(&metaLength)) return 1;
if (!file.ReadU32(&metaOrigLength)) return 1;
if (!file.ReadU32(&privOffset)) return 1;
if (!file.ReadU32(&privLength)) return 1;
if (!file.ReadU32(&signature)) { return 1;
}
if (!file.ReadU32(&flavor)) { return 1;
}
if (!file.ReadU32(&length)) { return 1;
}
if (!file.ReadU16(&num_tables)) { return 1;
}
if (!file.ReadU16(&reserved)) { return 1;
}
if (!file.ReadU32(&totalSfntSize)) { return 1;
}
if (!file.ReadU32(&totalCompressedSize)) { return 1;
}
if (!file.ReadU16(&major)) { return 1;
}
if (!file.ReadU16(&minor)) { return 1;
}
if (!file.ReadU32(&metaOffset)) { return 1;
}
if (!file.ReadU32(&metaLength)) { return 1;
}
if (!file.ReadU32(&metaOrigLength)) { return 1;
}
if (!file.ReadU32(&privOffset)) { return 1;
}
if (!file.ReadU32(&privLength)) { return 1;
}
if (signature != 0x774F4632) {
printf("Invalid signature: %08x\n", signature);
@@ -90,14 +104,17 @@ int main(int argc, char **argv) {
size_t offset = file.offset();
uint8_t flags;
uint32_t tag, origLength, transformLength;
if (!file.ReadU8(&flags)) return 1;
if (!file.ReadU8(&flags)) { return 1;
}
if ((flags & 0x3f) == 0x3f) {
if (!file.ReadU32(&tag)) return 1;
if (!file.ReadU32(&tag)) { return 1;
}
} else {
tag = woff2::kKnownTags[flags & 0x3f];
}
table_tags.push_back(tag);
if (!ReadBase128(&file, &origLength)) return 1;
if (!ReadBase128(&file, &origLength)) { return 1;
}
printf("%5d %6zu 0x%02x %s %10d", i, offset, flags,
PrintTag(tag).c_str(), origLength);
@@ -105,11 +122,13 @@ int main(int argc, char **argv) {
uint8_t xform_version = (flags >> 6) & 0x3;
if (tag == woff2::kGlyfTableTag || tag == woff2::kLocaTableTag) {
if (xform_version == 0) {
if (!ReadBase128(&file, &transformLength)) return 1;
if (!ReadBase128(&file, &transformLength)) { return 1;
}
printf(" %8d", transformLength);
}
} else if (xform_version > 0) {
if (!ReadBase128(&file, &transformLength)) return 1;
if (!ReadBase128(&file, &transformLength)) { return 1;
}
printf(" %8d", transformLength);
}
printf("\n");
@@ -118,20 +137,26 @@ int main(int argc, char **argv) {
// Collection header
if (flavor == woff2::kTtcFontFlavor) {
uint32_t version, numFonts;
if (!file.ReadU32(&version)) return 1;
if (!woff2::Read255UShort(&file, &numFonts)) return 1;
if (!file.ReadU32(&version)) { return 1;
}
if (!woff2::Read255UShort(&file, &numFonts)) { return 1;
}
printf("CollectionHeader 0x%08x %d fonts\n", version, numFonts);
for (auto i = 0; i < numFonts; i++) {
uint32_t numTables, flavor;
if (!woff2::Read255UShort(&file, &numTables)) return 1;
if (!file.ReadU32(&flavor)) return 1;
if (!woff2::Read255UShort(&file, &numTables)) { return 1;
}
if (!file.ReadU32(&flavor)) { return 1;
}
printf("CollectionFontEntry %d flavor 0x%08x %d tables\n", i, flavor,
numTables);
for (auto j = 0; j < numTables; j++) {
uint32_t table_idx;
if (!woff2::Read255UShort(&file, &table_idx)) return 1;
if (table_idx >= table_tags.size()) return 1;
if (!woff2::Read255UShort(&file, &table_idx)) { return 1;
}
if (table_idx >= table_tags.size()) { return 1;
}
printf(" %d %s (idx %d)\n", j,
PrintTag(table_tags[table_idx]).c_str(), table_idx);
}
+4 -4
View File
@@ -17,11 +17,11 @@ WOFF2StringOut::WOFF2StringOut(string* buf)
max_size_(kDefaultMaxSize),
offset_(0) {}
bool WOFF2StringOut::Write(const void *buf, size_t n) {
auto WOFF2StringOut::Write(const void *buf, size_t n) -> bool {
return Write(buf, offset_, n);
}
bool WOFF2StringOut::Write(const void *buf, size_t offset, size_t n) {
auto WOFF2StringOut::Write(const void *buf, size_t offset, size_t n) -> bool {
if (offset > max_size_ || n > max_size_ - offset) {
return false;
}
@@ -50,11 +50,11 @@ WOFF2MemoryOut::WOFF2MemoryOut(uint8_t* buf, size_t buf_size)
buf_size_(buf_size),
offset_(0) {}
bool WOFF2MemoryOut::Write(const void *buf, size_t n) {
auto WOFF2MemoryOut::Write(const void *buf, size_t n) -> bool {
return Write(buf, offset_, n);
}
bool WOFF2MemoryOut::Write(const void *buf, size_t offset, size_t n) {
auto WOFF2MemoryOut::Write(const void *buf, size_t offset, size_t n) -> bool {
if (offset > buf_size_ || n > buf_size_ - offset) {
return false;
}
+53
View File
@@ -0,0 +1,53 @@
-xc++
-Wall
-Wextra
-Wthread-safety
-Wself-assign
-Wno-missing-field-initializers
-DLLVM_ON_UNIX=1
-DHAVE_BACKTRACE=1
-DBACKTRACE_HEADER=<execinfo.h>
-DLTDL_SHLIB_EXT=".so"
-DLLVM_ENABLE_THREADS=1
-DHAVE_SYSEXITS_H=1
-DHAVE_UNISTD_H=1
-DHAVE_STRERROR_R=1
-DHAVE_LIBPTHREAD=1
-DHAVE_PTHREAD_GETNAME_NP=1
-DHAVE_PTHREAD_SETNAME_NP=1
-DHAVE_PTHREAD_GETSPECIFIC=1
-D_GNU_SOURCE
-DHAVE_LINK_H=1
-DHAVE_LSEEK64=1
-DHAVE_MALLINFO=1
-DHAVE_POSIX_FALLOCATE=1
-DHAVE_SBRK=1
-DHAVE_STRUCT_STAT_ST_MTIM_TV_NSEC=1
-DLLVM_NATIVE_ARCH="X86"
-DLLVM_NATIVE_ASMPARSER=LLVMInitializeX86AsmParser
-DLLVM_NATIVE_ASMPRINTER=LLVMInitializeX86AsmPrinter
-DLLVM_NATIVE_DISASSEMBLER=LLVMInitializeX86Disassembler
-DLLVM_NATIVE_TARGET=LLVMInitializeX86Target
-DLLVM_NATIVE_TARGETINFO=LLVMInitializeX86TargetInfo
-DLLVM_NATIVE_TARGETMC=LLVMInitializeX86TargetMC
-DLLVM_HOST_TRIPLE="x86_64-unknown-linux-gnu"
-DLLVM_DEFAULT_TARGET_TRIPLE="x86_64-unknown-linux-gnu"
-DLLVM_ENABLE_TERMINFO=1
-DLLVM_ENABLE_ZLIB=1
-DGTEST_HAS_RTTI=0
-D__STDC_LIMIT_MACROS
-D__STDC_CONSTANT_MACROS
-DGTEST_USE_OWN_TR1_TUPLE=1
-std=c++17
-nostdinc++
-I../../brotli/original/c/include
-Iinclude
-isystem
../../../../bazel-clang-toolchain/include/c++/v1
-isystem
../../../../bazel-clang-toolchain/lib/clang/12.0.0/include
-no-canonical-prefixes
-Wno-builtin-macro-redefined
-D__DATE__="redacted"
-D__TIMESTAMP__="redacted"
-D__TIME__="redacted"
+35
View File
@@ -0,0 +1,35 @@
#!/bin/bash -eux
#
# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
# Exceptions. See /LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# Runs an example migration of woff2 C++ code.
# cd to the carbon-lang root.
cd "$(dirname "$0")/../../.."
EXAMPLE=third_party/examples/woff2
# Remove any previous conversion. Each time this is run, it should demonstrate
# on a fresh copy of woff2.
rm -rf "${EXAMPLE}/carbon/"
# Initialize the carbon directory with C++ code only.
mkdir -p "${EXAMPLE}/carbon/"
for x in LICENSE include src; do
cp -R "${EXAMPLE}/original/${x}" "${EXAMPLE}/carbon/${x}"
done
# Copy files into the carbon directory to simplify the setup.
cp "${EXAMPLE}/BUILD.original" \
"${EXAMPLE}/carbon/BUILD"
cp "${EXAMPLE}/WORKSPACE.original" \
"${EXAMPLE}/carbon/WORKSPACE"
cp "${EXAMPLE}/compile_flags.carbon.txt" \
"${EXAMPLE}/carbon/compile_flags.txt"
# Run the migration tool.
bazel build //migrate_cpp
./bazel-bin/migrate_cpp/migrate_cpp \
"${EXAMPLE}/carbon"