Start handling variable declarations (#571)

TODOs in the test for known issues.

I may switch the approach to getting the variable type (on examination, this isn't working quite as well as I'd thought) but for now I think it's okay. I had an earlier approach though that may work better overall -- I'd been thinking this would work better, but as you can see in the null check for type information, I think I missed a key point.

Anyways, what'd really been vexing me was `int i, j` which I think I handle passably well now. There's obviously room for improvement, but given I've been going at this for a couple days now, I thought it best to checkpoint where I was.

This also includes some related framework changes to fix bumps I was running into. Overall the tool should operate a bit more smoothly with these changes. There are still issues with overlapping replacements, but I think it's primarily with range-based for loops which I just need to take some time to fix.
This commit is contained in:
Jon Meow
2021-06-23 10:58:32 -07:00
committed by GitHub
parent b897e9e38b
commit 2344a52e24
38 changed files with 1103 additions and 952 deletions
+39 -18
View File
@@ -11,28 +11,11 @@ cc_binary(
srcs = ["main.cpp"],
deps = [
":fn_inserter",
":var_decl",
"@llvm-project//clang:tooling",
],
)
cc_library(
name = "fn_inserter",
srcs = ["fn_inserter.cpp"],
hdrs = ["fn_inserter.h"],
deps = [":matcher"],
)
cc_test(
name = "fn_inserter_test",
srcs = ["fn_inserter_test.cpp"],
deps = [
":fn_inserter",
":matcher_test_base",
"@llvm-project//clang:tooling",
"@llvm-project//llvm:gtest_main",
],
)
cc_library(
name = "matcher",
srcs = ["matcher.cpp"],
@@ -55,3 +38,41 @@ cc_library(
"@llvm-project//llvm:gtest",
],
)
# Individual matchers
cc_library(
name = "fn_inserter",
srcs = ["fn_inserter.cpp"],
hdrs = ["fn_inserter.h"],
deps = [":matcher"],
)
cc_test(
name = "fn_inserter_test",
srcs = ["fn_inserter_test.cpp"],
deps = [
":fn_inserter",
":matcher_test_base",
"@llvm-project//clang:tooling",
"@llvm-project//llvm:gtest_main",
],
)
cc_library(
name = "var_decl",
srcs = ["var_decl.cpp"],
hdrs = ["var_decl.h"],
deps = [":matcher"],
)
cc_test(
name = "var_decl_test",
srcs = ["var_decl_test.cpp"],
deps = [
":matcher_test_base",
":var_decl",
"@llvm-project//clang:tooling",
"@llvm-project//llvm:gtest_main",
],
)
+4 -1
View File
@@ -5,6 +5,7 @@
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Refactoring.h"
#include "migrate_cpp/cpp_refactoring/fn_inserter.h"
#include "migrate_cpp/cpp_refactoring/var_decl.h"
namespace cam = ::clang::ast_matchers;
namespace ct = ::clang::tooling;
@@ -32,8 +33,10 @@ auto main(int argc, const char** argv) -> int {
InitReplacements(&tool);
// Set up AST matcher callbacks.
auto& repl = tool.getReplacements();
cam::MatchFinder finder;
Carbon::FnInserter fn_inserter(tool.getReplacements(), &finder);
Carbon::FnInserter fn_inserter(repl, &finder);
Carbon::VarDecl var_decl(repl, &finder);
return tool.runAndSave(
clang::tooling::newFrontendActionFactory(&finder).get());
+5 -8
View File
@@ -12,19 +12,16 @@ void Matcher::AddReplacement(const clang::SourceManager& sm,
clang::CharSourceRange range,
llvm::StringRef replacement_text) {
if (!range.isValid()) {
llvm::errs() << "Invalid range: " << range.getAsRange().printToString(sm)
<< "\n";
// Invalid range.
return;
}
if (sm.getDecomposedLoc(range.getBegin()).first !=
sm.getDecomposedLoc(range.getEnd()).first) {
llvm::errs() << "Range spans macro expansions: "
<< range.getAsRange().printToString(sm) << "\n";
// Range spans macro expansions.
return;
}
if (sm.getFileID(range.getBegin()) != sm.getFileID(range.getEnd())) {
llvm::errs() << "Range spans files: "
<< range.getAsRange().printToString(sm) << "\n";
// Range spans files.
return;
}
@@ -38,8 +35,8 @@ void Matcher::AddReplacement(const clang::SourceManager& sm,
auto err = entry->second.add(rep);
if (err) {
llvm::report_fatal_error("Error with replacement `" + rep.toString() +
"`: " + llvm::toString(std::move(err)) + "\n");
llvm::errs() << "Error with replacement `" << rep.toString()
<< "`: " << llvm::toString(std::move(err)) << "\n";
}
}
@@ -24,12 +24,16 @@ void MatcherTestBase::ExpectReplacement(llvm::StringRef before,
ct::FileContentMappings()));
EXPECT_THAT(replacements, testing::ElementsAre(testing::Key(Filename)));
auto actual = ct::applyAllReplacements(before, replacements[Filename]);
// Split lines to get gmock to get an easier-to-read error.
llvm::SmallVector<llvm::StringRef, 0> actual_lines;
llvm::SplitString(*actual, actual_lines, "\n");
llvm::SmallVector<llvm::StringRef, 0> after_lines;
llvm::SplitString(after, after_lines, "\n");
EXPECT_THAT(actual_lines, testing::ContainerEq(after_lines));
if (after.find('\n') == std::string::npos) {
EXPECT_THAT(*actual, testing::Eq(after.str()));
} else {
// Split lines to get gmock to get an easier-to-read error.
llvm::SmallVector<llvm::StringRef, 0> actual_lines;
llvm::SplitString(*actual, actual_lines, "\n");
llvm::SmallVector<llvm::StringRef, 0> after_lines;
llvm::SplitString(after, after_lines, "\n");
EXPECT_THAT(actual_lines, testing::ContainerEq(after_lines));
}
}
} // namespace Carbon
+61
View File
@@ -0,0 +1,61 @@
// 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
#include "migrate_cpp/cpp_refactoring/var_decl.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Lex/Lexer.h"
namespace cam = ::clang::ast_matchers;
namespace Carbon {
VarDecl::VarDecl(std::map<std::string, Replacements>& in_replacements,
cam::MatchFinder* finder)
: Matcher(in_replacements) {
finder->addMatcher(cam::varDecl().bind(Label), this);
}
void VarDecl::run(const cam::MatchFinder::MatchResult& result) {
const auto* decl = result.Nodes.getNodeAs<clang::VarDecl>(Label);
if (!decl) {
llvm::report_fatal_error(std::string("getNodeAs failed for ") + Label);
}
auto& sm = *(result.SourceManager);
auto lang_opts = result.Context->getLangOpts();
std::string after;
// Start the replacement with "var" unless it's a parameter.
if (result.Nodes.getNodeAs<clang::ParmVarDecl>(Label) == nullptr) {
after = "var ";
}
// Finish the "type: name" replacement.
after += decl->getNameAsString() + ": " +
clang::QualType::getAsString(decl->getType().split(), lang_opts);
if (decl->getTypeSourceInfo() == nullptr) {
// TODO: Need to understand what's happening in this case. Not sure if we
// need to address it.
return;
}
// This decides the range to replace. Normally the entire decl is replaced,
// but for code like `int i, j` we need to detect the comma between the
// declared names. That case currently results in `var i: int, var j: int`.
auto type_loc = decl->getTypeSourceInfo()->getTypeLoc();
auto after_type_loc =
clang::Lexer::getLocForEndOfToken(type_loc.getEndLoc(), 0, sm, lang_opts);
// If there's a comma, this range will be non-empty.
auto comma_source_text = clang::Lexer::getSourceText(
clang::CharSourceRange::getCharRange(after_type_loc, decl->getLocation()),
sm, lang_opts);
bool has_comma = !comma_source_text.trim().empty();
clang::CharSourceRange replace_range = clang::CharSourceRange::getTokenRange(
has_comma ? decl->getLocation() : decl->getBeginLoc(), decl->getEndLoc());
AddReplacement(sm, replace_range, after);
}
} // namespace Carbon
+26
View File
@@ -0,0 +1,26 @@
// 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
#ifndef MIGRATE_CPP_CPP_REFACTORING_VAR_DECL_H_
#define MIGRATE_CPP_CPP_REFACTORING_VAR_DECL_H_
#include "migrate_cpp/cpp_refactoring/matcher.h"
namespace Carbon {
// Updates variable declarations for `var name: Type`.
class VarDecl : public Matcher {
public:
explicit VarDecl(std::map<std::string, Replacements>& in_replacements,
MatchFinder* finder);
void run(const MatchFinder::MatchResult& result) override;
private:
static constexpr char Label[] = "VarDecl";
};
} // namespace Carbon
#endif // MIGRATE_CPP_CPP_REFACTORING_VAR_DECL_H_
@@ -0,0 +1,125 @@
// 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
#include "migrate_cpp/cpp_refactoring/var_decl.h"
#include "migrate_cpp/cpp_refactoring/matcher_test_base.h"
namespace Carbon {
namespace {
class VarDeclTest : public MatcherTestBase {
protected:
VarDeclTest() : var_decl(replacements, &finder) {}
Carbon::VarDecl var_decl;
};
TEST_F(VarDeclTest, Declaration) {
constexpr char Before[] = "int i;";
constexpr char After[] = "var i: int;";
ExpectReplacement(Before, After);
}
TEST_F(VarDeclTest, DeclarationArray) {
constexpr char Before[] = "int i[4];";
constexpr char After[] = "var i: int [4];";
ExpectReplacement(Before, After);
}
TEST_F(VarDeclTest, DeclarationComma) {
// TODO: Maybe replace the comma with a `;`.
constexpr char Before[] = "int i, j;";
constexpr char After[] = "var i: int, var j: int;";
ExpectReplacement(Before, After);
}
TEST_F(VarDeclTest, Assignment) {
constexpr char Before[] = "int i = 0;";
// TODO: Include init.
constexpr char After[] = "var i: int;";
ExpectReplacement(Before, After);
}
TEST_F(VarDeclTest, Auto) {
constexpr char Before[] = "auto i = 0;";
// TODO: Keep auto.
constexpr char After[] = "var i: int;";
ExpectReplacement(Before, After);
}
TEST_F(VarDeclTest, Const) {
// TODO: Include init, have `const` indicate `let`.
constexpr char Before[] = "const int i = 0;";
constexpr char After[] = "var i: const int;";
ExpectReplacement(Before, After);
}
TEST_F(VarDeclTest, Params) {
constexpr char Before[] = "auto Foo(int i) -> int;";
constexpr char After[] = "auto Foo(i: int) -> int;";
ExpectReplacement(Before, After);
}
TEST_F(VarDeclTest, ParamsDefault) {
// TODO: Include init.
constexpr char Before[] = "auto Foo(int i = 0) -> int;";
constexpr char After[] = "auto Foo(i: int) -> int;";
ExpectReplacement(Before, After);
}
TEST_F(VarDeclTest, ParamsConst) {
constexpr char Before[] = "auto Foo(const int i) -> int;";
constexpr char After[] = "auto Foo(i: const int) -> int;";
ExpectReplacement(Before, After);
}
TEST_F(VarDeclTest, ParamStruct) {
// This is to ensure the 'struct' keyword doesn't get added to the call type.
constexpr char Before[] = R"cpp(
struct Circle {};
auto Draw(int times, const Circle& circle) -> bool;
)cpp";
constexpr char After[] = R"(
struct Circle {};
auto Draw(times: int, circle: const Circle &) -> bool;
)";
ExpectReplacement(Before, After);
}
TEST_F(VarDeclTest, Member) {
// TODO: Handle member variables.
constexpr char Before[] = R"cpp(
struct Circle {
Circle() : x(0), y(0), radius(1) {}
int x;
int y;
int radius;
};
)cpp";
ExpectReplacement(Before, Before);
}
TEST_F(VarDeclTest, RangeFor) {
// TODO: Handle range based for loops.
constexpr char Before[] = R"cpp(
void Foo() {
int items[] = {1};
for (int i : items) {
}
}
)cpp";
constexpr char After[] = R"(
void Foo() {
var items: int [1];
for (int i var __begin1: int * var __range1: int (&)[1]) {
}
}
)";
ExpectReplacement(Before, After);
}
} // namespace
} // namespace Carbon
+12 -7
View File
@@ -37,11 +37,15 @@ class _Workflow(object):
def run(self):
"""Runs the migration workflow."""
self._gather_files()
self._clang_tidy()
self._cpp_refactoring()
self._rename_files()
self._print_header("Done!")
try:
self._gather_files()
self._clang_tidy()
self._cpp_refactoring()
self._rename_files()
self._print_header("Done!")
except subprocess.CalledProcessError as e:
# Discard the stack for subprocess errors.
sys.exit(e)
def _data_file(self, relative_path):
"""Returns the path to a data file."""
@@ -75,14 +79,15 @@ class _Workflow(object):
with open(self._data_file("clang_tidy.yaml")) as f:
config = f.read()
subprocess.run(
["clang-tidy", "--fix", "--config", config] + self._cpp_files
["clang-tidy", "--fix", "--config", config] + self._cpp_files,
check=True,
)
def _cpp_refactoring(self):
"""Runs cpp_refactoring to migrate C++ files towards Carbon syntax."""
self._print_header("Running cpp_refactoring...")
cpp_refactoring = self._data_file(_CPP_REFACTORING)
subprocess.run([cpp_refactoring] + self._cpp_files)
subprocess.run([cpp_refactoring] + self._cpp_files, check=True)
def _rename_files(self):
"""Renames C++ files to the destination Carbon filenames."""
@@ -16,20 +16,20 @@
namespace woff2 {
// Compute the size of the final uncompressed font, or 0 on error.
fn ComputeWOFF2FinalSize(const uint8_t *data, size_t length) -> size_t;
fn ComputeWOFF2FinalSize(data: const uint8_t *, length: size_t) -> 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*
fn ConvertWOFF2ToTTF(uint8_t *result, size_t result_length,
const uint8_t *data, size_t length) -> bool;
fn ConvertWOFF2ToTTF(result: uint8_t *, result_length: size_t,
data: const uint8_t *, length: size_t) -> bool;
// Decompresses the font into out. Returns true on success.
// Works even if WOFF2Header totalSfntSize is wrong.
// Please prefer this API.
fn ConvertWOFF2ToTTF(const uint8_t *data, size_t length,
WOFF2Out* out) -> bool;
fn ConvertWOFF2ToTTF(data: const uint8_t *, length: size_t,
out: woff2::WOFF2Out *) -> bool;
} // namespace woff2
@@ -25,18 +25,18 @@ struct WOFF2Params {
};
// Returns an upper bound on the size of the compressed file.
fn MaxWOFF2CompressedSize(const uint8_t* data, size_t length) -> size_t;
fn MaxWOFF2CompressedSize(const uint8_t* data, size_t length,
const std::string& extended_metadata) -> size_t;
fn MaxWOFF2CompressedSize(data: const uint8_t *, length: size_t) -> size_t;
fn MaxWOFF2CompressedSize(data: const uint8_t *, length: size_t,
extended_metadata: const std::string &) -> 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.
fn ConvertTTFToWOFF2(const uint8_t *data, size_t length,
uint8_t *result, size_t *result_length) -> bool;
fn ConvertTTFToWOFF2(const uint8_t *data, size_t length,
uint8_t *result, size_t *result_length,
const WOFF2Params& params) -> bool;
fn ConvertTTFToWOFF2(data: const uint8_t *, length: size_t,
result: uint8_t *, result_length: size_t *) -> bool;
fn ConvertTTFToWOFF2(data: const uint8_t *, length: size_t,
result: uint8_t *, result_length: size_t *,
params: const woff2::WOFF2Params &) -> bool;
} // namespace woff2
+10 -10
View File
@@ -17,7 +17,7 @@
namespace woff2 {
// Suggested max size for output.
const size_t kDefaultMaxSize = 30 * 1024 * 1024;
var kDefaultMaxSize: const size_t;
/**
* Output interface for the woff2 decoding.
@@ -34,11 +34,11 @@ class WOFF2Out {
// Append n bytes of data from buf.
// Return true if all written, false otherwise.
fn auto Write(const void *buf, size_t n) -> bool = 0;
fn auto Write(buf: const void *, n: size_t) -> bool = 0;
// Write n bytes of data from buf at offset.
// Return true if all written, false otherwise.
fn auto Write(const void *buf, size_t offset, size_t n) -> bool = 0;
fn auto Write(buf: const void *, offset: size_t, n: size_t) -> bool = 0;
fn auto Size() -> size_t = 0;
};
@@ -51,13 +51,13 @@ class WOFF2StringOut : public WOFF2Out {
// Create a writer that writes its data to buf.
// buf->size() will grow to at most max_size
// buf may be sized (e.g. using EstimateWOFF2FinalSize) or empty.
explicit WOFF2StringOut(std::string* buf);
explicit WOFF2StringOut(buf: std::string *);
fn Write(const void *buf, size_t n) -> bool override;
fn Write(const void *buf, size_t offset, size_t n) -> bool override;
fn Write(buf: const void *, n: size_t) -> bool override;
fn Write(buf: const void *, offset: size_t, n: size_t) -> bool override;
fn Size() -> size_t override { return offset_; }
fn MaxSize() -> size_t { return max_size_; }
void SetMaxSize(size_t max_size);
void SetMaxSize(max_size: size_t);
private:
std::string* buf_;
size_t max_size_;
@@ -70,10 +70,10 @@ class WOFF2StringOut : public WOFF2Out {
class WOFF2MemoryOut : public WOFF2Out {
public:
// Create a writer that writes its data to buf.
WOFF2MemoryOut(uint8_t* buf, size_t buf_size);
WOFF2MemoryOut(buf: uint8_t *, buf_size: size_t);
fn Write(const void *buf, size_t n) -> bool override;
fn Write(const void *buf, size_t offset, size_t n) -> bool override;
fn Write(buf: const void *, n: size_t) -> bool override;
fn Write(buf: const void *, offset: size_t, n: size_t) -> bool override;
fn Size() -> size_t override { return offset_; }
private:
uint8_t* buf_;
+12 -12
View File
@@ -57,16 +57,16 @@ inline bool Failure(const char *f, int l, const char *fn) {
// -----------------------------------------------------------------------------
class Buffer {
public:
Buffer(const uint8_t *data, size_t len)
Buffer(data: const uint8_t *, len: size_t)
: buffer_(data),
length_(len),
offset_(0) { }
fn Skip(size_t n_bytes) -> bool {
fn Skip(n_bytes: size_t) -> bool {
return Read(nullptr, n_bytes);
}
fn Read(uint8_t *data, size_t n_bytes) -> bool {
fn Read(data: uint8_t *, n_bytes: size_t) -> bool {
if (n_bytes > 1024 * 1024 * 1024) {
return FONT_COMPRESSION_FAILURE();
}
@@ -81,7 +81,7 @@ class Buffer {
return true;
}
fn auto ReadU8(uint8_t *value) -> bool {
fn auto ReadU8(value: uint8_t *) -> bool {
if (offset_ + 1 > length_) {
return FONT_COMPRESSION_FAILURE();
}
@@ -90,7 +90,7 @@ class Buffer {
return true;
}
fn ReadU16(uint16_t *value) -> bool {
fn ReadU16(value: uint16_t *) -> bool {
if (offset_ + 2 > length_) {
return FONT_COMPRESSION_FAILURE();
}
@@ -100,11 +100,11 @@ class Buffer {
return true;
}
fn ReadS16(int16_t *value) -> bool {
fn ReadS16(value: int16_t *) -> bool {
return ReadU16(reinterpret_cast<uint16_t*>(value));
}
fn ReadU24(uint32_t *value) -> bool {
fn ReadU24(value: uint32_t *) -> bool {
if (offset_ + 3 > length_) {
return FONT_COMPRESSION_FAILURE();
}
@@ -115,7 +115,7 @@ class Buffer {
return true;
}
fn ReadU32(uint32_t *value) -> bool {
fn ReadU32(value: uint32_t *) -> bool {
if (offset_ + 4 > length_) {
return FONT_COMPRESSION_FAILURE();
}
@@ -125,11 +125,11 @@ class Buffer {
return true;
}
fn ReadS32(int32_t *value) -> bool {
fn ReadS32(value: int32_t *) -> bool {
return ReadU32(reinterpret_cast<uint32_t*>(value));
}
fn ReadTag(uint32_t *value) -> bool {
fn ReadTag(value: uint32_t *) -> bool {
if (offset_ + 4 > length_) {
return FONT_COMPRESSION_FAILURE();
}
@@ -138,7 +138,7 @@ class Buffer {
return true;
}
fn ReadR64(uint64_t *value) -> bool {
fn ReadR64(value: uint64_t *) -> bool {
if (offset_ + 8 > length_) {
return FONT_COMPRESSION_FAILURE();
}
@@ -151,7 +151,7 @@ class Buffer {
[[nodiscard]] fn offset() const -> size_t { return offset_; }
[[nodiscard]] fn length() const -> size_t { return length_; }
void set_offset(size_t newoffset) { offset_ = newoffset; }
void set_offset(newoffset: size_t) { offset_ = newoffset; }
private:
const uint8_t * const buffer_;
@@ -4,9 +4,9 @@
#include <woff2/decode.h>
// Entry point for LibFuzzer.
extern "C" fn LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) -> int {
std::string buf;
woff2::WOFF2StringOut out(&buf);
extern "C" fn LLVMFuzzerTestOneInput(data: const uint8_t *, size: size_t) -> int {
var buf: std::string;
var out: woff2::WOFF2StringOut;
out.SetMaxSize(30 * 1024 * 1024);
woff2::ConvertWOFF2ToTTF(data, size, &out);
return 0;
@@ -1,12 +1,11 @@
#include <string>
#include <woff2/decode.h>
extern "C" fn LLVMFuzzerTestOneInput(const uint8_t *data, size_t data_size) -> int {
extern "C" fn LLVMFuzzerTestOneInput(data: const uint8_t *, data_size: size_t) -> int {
// Decode using newer entry pattern.
// Same pattern as woff2_decompress.
std::string output(std::min(woff2::ComputeWOFF2FinalSize(data, data_size),
woff2::kDefaultMaxSize), 0);
woff2::WOFF2StringOut out(&output);
var output: std::string;
var out: woff2::WOFF2StringOut;
woff2::ConvertWOFF2ToTTF(data, data_size, &out);
return 0;
}
+5 -5
View File
@@ -17,16 +17,16 @@ namespace woff2 {
using std::string;
fn auto GetFileContent(const string& filename) -> string {
std::ifstream ifs(filename.c_str(), std::ios::binary);
fn auto GetFileContent(filename: const std::string &) -> string {
var ifs: std::ifstream;
return string(
std::istreambuf_iterator<char>(ifs.rdbuf()),
std::istreambuf_iterator<char>());
}
inline void SetFileContents(const string& filename, string::iterator start,
string::iterator end) {
std::ofstream ofs(filename.c_str(), std::ios::binary);
inline void SetFileContents(filename: const std::string &, start: string::iterator,
end: string::iterator) {
var ofs: std::ofstream;
std::copy(start, end, std::ostream_iterator<char>(ofs));
}
+15 -15
View File
@@ -47,8 +47,8 @@ struct Font {
std::map<uint32_t, Table> tables;
[[nodiscard]] fn OutputOrderedTags() const -> std::vector<uint32_t>;
fn FindTable(uint32_t tag) -> Table*;
[[nodiscard]] fn FindTable(uint32_t tag) const -> const Table*;
fn FindTable(tag: uint32_t) -> Table*;
[[nodiscard]] fn FindTable(tag: uint32_t) 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.
fn ReadFont(const uint8_t* data, size_t len, Font* font) -> bool;
fn ReadFont(data: const uint8_t *, len: size_t, font: woff2::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.
fn ReadFontCollection(const uint8_t* data, size_t len, FontCollection* fonts) -> bool;
fn ReadFontCollection(data: const uint8_t *, len: size_t, fonts: woff2::FontCollection *) -> bool;
// Returns the file size of the font.
fn FontFileSize(const Font& font) -> size_t;
fn FontCollectionFileSize(const FontCollection& font) -> size_t;
fn FontFileSize(font: const woff2::Font &) -> size_t;
fn FontCollectionFileSize(font: const woff2::FontCollection &) -> 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()).
fn WriteFont(const Font& font, uint8_t* dst, size_t dst_size) -> bool;
fn WriteFont(font: const woff2::Font &, dst: uint8_t *, dst_size: size_t) -> bool;
// Write the font at a specific offset
fn WriteFont(const Font& font, size_t* offset, uint8_t* dst, size_t dst_size) -> bool;
fn WriteFont(font: const woff2::Font &, offset: size_t *, dst: uint8_t *, dst_size: size_t) -> bool;
fn WriteFontCollection(const FontCollection& font_collection, uint8_t* dst,
size_t dst_size) -> bool;
fn WriteFontCollection(font_collection: const woff2::FontCollection &, dst: uint8_t *,
dst_size: size_t) -> 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.
fn NumGlyphs(const Font& font) -> int;
fn NumGlyphs(font: const woff2::Font &) -> int;
// Returns the index format of the font
fn IndexFormat(const Font& font) -> int;
fn IndexFormat(font: const woff2::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.
fn GetGlyphData(const Font& font, int glyph_index,
const uint8_t** glyph_data, size_t* glyph_size) -> bool;
fn GetGlyphData(font: const woff2::Font &, glyph_index: int,
glyph_data: const uint8_t **, glyph_size: size_t *) -> bool;
// Removes the digital signature (DSIG) table
fn RemoveDigitalSignature(Font* font) -> bool;
fn RemoveDigitalSignature(font: woff2::Font *) -> bool;
} // namespace woff2
+84 -87
View File
@@ -18,21 +18,21 @@
namespace woff2 {
fn Font::FindTable(uint32_t tag) -> Font::Table* {
auto it = tables.find(tag);
fn Font::FindTable(tag: uint32_t) -> Font::Table* {
var it: std::__map_iterator<std::__tree_iterator<std::__value_type<unsigned int, woff2::Font::Table>, std::__tree_node<std::__value_type<unsigned int, woff2::Font::Table>, void *> *, long>>;
return it == tables.end() ? nullptr : &it->second;
}
fn Font::FindTable(uint32_t tag) const -> const Font::Table* {
auto it = tables.find(tag);
fn Font::FindTable(tag: uint32_t) const -> const Font::Table* {
var it: std::__map_const_iterator<std::__tree_const_iterator<std::__value_type<unsigned int, woff2::Font::Table>, std::__tree_node<std::__value_type<unsigned int, woff2::Font::Table>, void *> *, long>>;
return it == tables.end() ? nullptr : &it->second;
}
fn Font::OutputOrderedTags() const -> std::vector<uint32_t> {
std::vector<uint32_t> output_order;
var output_order: std::vector<uint32_t>;
for (const auto& i : tables) {
const Font::Table& table = i.second;
for (const auto& i var __begin1: std::__map_const_iterator<std::__tree_const_iterator<std::__value_type<unsigned int, woff2::Font::Table>, std::__tree_node<std::__value_type<unsigned int, woff2::Font::Table>, void *> *, long>> var __range1: const std::map<unsigned int, woff2::Font::Table> &) {
var table: const Font::Table &;
// This is a transformed table, we will write it together with the
// original version.
if (table.tag & 0x80808080) {
@@ -42,10 +42,8 @@ fn Font::OutputOrderedTags() const -> std::vector<uint32_t> {
}
// Alphabetize then put loca immediately after glyf
auto glyf_loc = std::find(output_order.begin(), output_order.end(),
kGlyfTableTag);
auto loca_loc = std::find(output_order.begin(), output_order.end(),
kLocaTableTag);
var glyf_loc: std::__wrap_iter<unsigned int *>;
var loca_loc: std::__wrap_iter<unsigned int *>;
if (glyf_loc != output_order.end() && loca_loc != output_order.end()) {
output_order.erase(loca_loc);
output_order.insert(std::find(output_order.begin(), output_order.end(),
@@ -55,8 +53,8 @@ fn Font::OutputOrderedTags() const -> std::vector<uint32_t> {
return output_order;
}
fn ReadTrueTypeFont(Buffer* file, const uint8_t* data, size_t len,
Font* font) -> bool {
fn ReadTrueTypeFont(file: woff2::Buffer *, data: const uint8_t *, len: size_t,
font: woff2::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) ||
@@ -64,9 +62,9 @@ fn ReadTrueTypeFont(Buffer* file, const uint8_t* data, size_t len,
return FONT_COMPRESSION_FAILURE();
}
std::map<uint32_t, uint32_t> intervals;
for (uint16_t i = 0; i < font->num_tables; ++i) {
Font::Table table;
var intervals: std::map<uint32_t, uint32_t>;
for (var i: uint16_t; i < font->num_tables; ++i) {
var table: Font::Table;
table.flag_byte = 0;
table.reuse_of = nullptr;
if (!file->ReadU32(&table.tag) ||
@@ -89,8 +87,8 @@ fn ReadTrueTypeFont(Buffer* file, const uint8_t* data, size_t len,
}
// Check that tables are non-overlapping.
uint32_t last_offset = 12UL + 16UL * font->num_tables;
for (const auto& i : intervals) {
var last_offset: uint32_t;
for (const auto& i var __begin1: std::__map_iterator<std::__tree_iterator<std::__value_type<unsigned int, unsigned int>, std::__tree_node<std::__value_type<unsigned int, unsigned int>, void *> *, long>> var __range1: std::map<unsigned int, unsigned int> &) {
if (i.first < last_offset || i.first + i.second < i.first) {
return FONT_COMPRESSION_FAILURE();
}
@@ -98,7 +96,7 @@ fn ReadTrueTypeFont(Buffer* file, const uint8_t* data, size_t len,
}
// Sanity check key tables
const Font::Table* head_table = font->FindTable(kHeadTableTag);
var head_table: const Font::Table *;
if (head_table != nullptr && head_table->length < 52) {
return FONT_COMPRESSION_FAILURE();
}
@@ -106,9 +104,9 @@ fn ReadTrueTypeFont(Buffer* file, const uint8_t* data, size_t len,
return true;
}
fn ReadCollectionFont(Buffer* file, const uint8_t* data, size_t len,
Font* font,
std::map<uint32_t, Font::Table*>* all_tables) -> bool {
fn ReadCollectionFont(file: woff2::Buffer *, data: const uint8_t *, len: size_t,
font: woff2::Font *,
all_tables: std::map<uint32_t, Font::Table *> *) -> bool {
if (!file->ReadU32(&font->flavor)) {
return FONT_COMPRESSION_FAILURE();
}
@@ -116,8 +114,8 @@ fn ReadCollectionFont(Buffer* file, const uint8_t* data, size_t len,
return FONT_COMPRESSION_FAILURE();
}
for (auto& entry : font->tables) {
Font::Table& table = entry.second;
for (auto& entry var __begin1: std::__map_iterator<std::__tree_iterator<std::__value_type<unsigned int, woff2::Font::Table>, std::__tree_node<std::__value_type<unsigned int, woff2::Font::Table>, void *> *, long>> var __range1: std::map<unsigned int, woff2::Font::Table> &) {
var table: Font::Table &;
if (all_tables->find(table.offset) == all_tables->end()) {
(*all_tables)[table.offset] = font->FindTable(table.tag);
@@ -132,18 +130,18 @@ fn ReadCollectionFont(Buffer* file, const uint8_t* data, size_t len,
return true;
}
fn ReadTrueTypeCollection(Buffer* file, const uint8_t* data, size_t len,
FontCollection* font_collection) -> bool {
uint32_t num_fonts;
fn ReadTrueTypeCollection(file: woff2::Buffer *, data: const uint8_t *, len: size_t,
font_collection: woff2::FontCollection *) -> bool {
var num_fonts: uint32_t;
if (!file->ReadU32(&font_collection->header_version) ||
!file->ReadU32(&num_fonts)) {
return FONT_COMPRESSION_FAILURE();
}
std::vector<uint32_t> offsets;
for (size_t i = 0; i < num_fonts; i++) {
uint32_t offset;
var offsets: std::vector<uint32_t>;
for (var i: size_t; i < num_fonts; i++) {
var offset: uint32_t;
if (!file->ReadU32(&offset)) {
return FONT_COMPRESSION_FAILURE();
}
@@ -151,12 +149,12 @@ fn ReadTrueTypeCollection(Buffer* file, const uint8_t* data, size_t len,
}
font_collection->fonts.resize(offsets.size());
auto font_it = font_collection->fonts.begin();
var font_it: std::__wrap_iter<woff2::Font *>;
std::map<uint32_t, Font::Table*> all_tables;
for (const auto offset : offsets) {
var all_tables: std::map<uint32_t, Font::Table *>;
for (const auto offset var __begin1: std::__wrap_iter<unsigned int *> var __range1: std::vector<unsigned int> &) {
file->set_offset(offset);
Font& font = *font_it++;
var font: woff2::Font &;
if (!ReadCollectionFont(file, data, len, &font, &all_tables)) {
return FONT_COMPRESSION_FAILURE();
}
@@ -165,8 +163,8 @@ fn ReadTrueTypeCollection(Buffer* file, const uint8_t* data, size_t len,
return true;
}
fn ReadFont(const uint8_t* data, size_t len, Font* font) -> bool {
Buffer file(data, len);
fn ReadFont(data: const uint8_t *, len: size_t, font: woff2::Font *) -> bool {
var file: woff2::Buffer;
if (!file.ReadU32(&font->flavor)) {
return FONT_COMPRESSION_FAILURE();
@@ -178,9 +176,9 @@ fn ReadFont(const uint8_t* data, size_t len, Font* font) -> bool {
return ReadTrueTypeFont(&file, data, len, font);
}
fn ReadFontCollection(const uint8_t* data, size_t len,
FontCollection* font_collection) -> bool {
Buffer file(data, len);
fn ReadFontCollection(data: const uint8_t *, len: size_t,
font_collection: woff2::FontCollection *) -> bool {
var file: woff2::Buffer;
if (!file.ReadU32(&font_collection->flavor)) {
return FONT_COMPRESSION_FAILURE();
@@ -188,40 +186,40 @@ fn ReadFontCollection(const uint8_t* data, size_t len,
if (font_collection->flavor != kTtcFontFlavor) {
font_collection->fonts.resize(1);
Font& font = font_collection->fonts[0];
var font: woff2::Font &;
font.flavor = font_collection->flavor;
return ReadTrueTypeFont(&file, data, len, &font);
}
return ReadTrueTypeCollection(&file, data, len, font_collection);
}
fn 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;
size_t padding_size = (4 - (table.length & 3)) & 3;
size_t end_offset = (padding_size + table.offset) + table.length;
fn FontFileSize(font: const woff2::Font &) -> size_t {
var max_offset: size_t;
for (const auto& i var __begin1: std::__map_const_iterator<std::__tree_const_iterator<std::__value_type<unsigned int, woff2::Font::Table>, std::__tree_node<std::__value_type<unsigned int, woff2::Font::Table>, void *> *, long>> var __range1: const std::map<unsigned int, woff2::Font::Table> &) {
var table: const Font::Table &;
var padding_size: size_t;
var end_offset: size_t;
max_offset = std::max(max_offset, end_offset);
}
return max_offset;
}
fn FontCollectionFileSize(const FontCollection& font_collection) -> size_t {
size_t max_offset = 0;
for (auto& font : font_collection.fonts) {
fn FontCollectionFileSize(font_collection: const woff2::FontCollection &) -> size_t {
var max_offset: size_t;
for (auto& font var __begin1: std::__wrap_iter<const woff2::Font *> var __range1: const std::vector<woff2::Font> &) {
// font file size actually just finds max offset
max_offset = std::max(max_offset, FontFileSize(font));
}
return max_offset;
}
fn WriteFont(const Font& font, uint8_t* dst, size_t dst_size) -> bool {
size_t offset = 0;
fn WriteFont(font: const woff2::Font &, dst: uint8_t *, dst_size: size_t) -> bool {
var offset: size_t;
return WriteFont(font, &offset, dst, dst_size);
}
fn WriteTableRecord(const Font::Table* table, size_t* offset, uint8_t* dst,
size_t dst_size) -> bool {
fn WriteTableRecord(table: const Font::Table *, offset: size_t *, dst: uint8_t *,
dst_size: size_t) -> bool {
if (dst_size < *offset + kSfntEntrySize) {
return FONT_COMPRESSION_FAILURE();
}
@@ -235,8 +233,8 @@ fn WriteTableRecord(const Font::Table* table, size_t* offset, uint8_t* dst,
return true;
}
fn WriteTable(const Font::Table& table, size_t* offset, uint8_t* dst,
size_t dst_size) -> bool {
fn WriteTable(table: const Font::Table &, offset: size_t *, dst: uint8_t *,
dst_size: size_t) -> bool {
if (!WriteTableRecord(&table, offset, dst, dst_size)) {
return false;
}
@@ -248,7 +246,7 @@ fn WriteTable(const Font::Table& table, size_t* offset, uint8_t* dst,
return FONT_COMPRESSION_FAILURE();
}
memcpy(dst + table.offset, table.data, table.length);
size_t padding_size = (4 - (table.length & 3)) & 3;
var padding_size: size_t;
if (table.offset + table.length + padding_size < padding_size ||
dst_size < table.offset + table.length + padding_size) {
return FONT_COMPRESSION_FAILURE();
@@ -258,21 +256,21 @@ fn WriteTable(const Font::Table& table, size_t* offset, uint8_t* dst,
return true;
}
fn WriteFont(const Font& font, size_t* offset, uint8_t* dst,
size_t dst_size) -> bool {
fn WriteFont(font: const woff2::Font &, offset: size_t *, dst: uint8_t *,
dst_size: size_t) -> bool {
if (dst_size < 12ULL + 16ULL * font.num_tables) {
return FONT_COMPRESSION_FAILURE();
}
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;
var max_pow2: uint16_t;
var search_range: uint16_t;
var range_shift: uint16_t;
Store16(search_range, offset, dst);
Store16(max_pow2, offset, dst);
Store16(range_shift, offset, dst);
for (const auto& i : font.tables) {
for (const auto& i var __begin1: std::__map_const_iterator<std::__tree_const_iterator<std::__value_type<unsigned int, woff2::Font::Table>, std::__tree_node<std::__value_type<unsigned int, woff2::Font::Table>, void *> *, long>> var __range1: const std::map<unsigned int, woff2::Font::Table> &) {
if (!WriteTable(i.second, offset, dst, dst_size)) {
return false;
}
@@ -281,9 +279,9 @@ fn WriteFont(const Font& font, size_t* offset, uint8_t* dst,
return true;
}
fn WriteFontCollection(const FontCollection& font_collection, uint8_t* dst,
size_t dst_size) -> bool {
size_t offset = 0;
fn WriteFontCollection(font_collection: const woff2::FontCollection &, dst: uint8_t *,
dst_size: size_t) -> bool {
var offset: size_t;
// It's simpler if this just a simple sfnt
if (font_collection.flavor != kTtcFontFlavor) {
@@ -296,8 +294,8 @@ fn WriteFontCollection(const FontCollection& font_collection, uint8_t* dst,
StoreU32(font_collection.fonts.size(), &offset, dst);
// Offset Table, zeroed for now
size_t offset_table = offset; // where to write offsets later
for (size_t i = 0; i < font_collection.fonts.size(); i++) {
var offset_table: size_t; // where to write offsets later
for (var i: size_t; i < font_collection.fonts.size(); i++) {
StoreU32(0, &offset, dst);
}
@@ -308,7 +306,7 @@ fn WriteFontCollection(const FontCollection& font_collection, uint8_t* dst,
}
// Write fonts and their offsets.
for (const auto & font : font_collection.fonts) {
for (const auto & font var __begin1: std::__wrap_iter<const woff2::Font *> var __range1: const std::vector<woff2::Font> &) {
StoreU32(offset, &offset_table, dst);
if (!WriteFont(font, &offset, dst, dst_size)) {
return false;
@@ -318,22 +316,22 @@ fn WriteFontCollection(const FontCollection& font_collection, uint8_t* dst,
return true;
}
fn NumGlyphs(const Font& font) -> int {
const Font::Table* head_table = font.FindTable(kHeadTableTag);
const Font::Table* loca_table = font.FindTable(kLocaTableTag);
fn NumGlyphs(font: const woff2::Font &) -> int {
var head_table: const Font::Table *;
var loca_table: const Font::Table *;
if (head_table == nullptr || loca_table == nullptr || head_table->length < 52) {
return 0;
}
int index_fmt = IndexFormat(font);
int loca_record_size = (index_fmt == 0 ? 2 : 4);
var index_fmt: int;
var loca_record_size: int;
if (loca_table->length < loca_record_size) {
return 0;
}
return (loca_table->length / loca_record_size) - 1;
}
fn IndexFormat(const Font& font) -> int {
const Font::Table* head_table = font.FindTable(kHeadTableTag);
fn IndexFormat(font: const woff2::Font &) -> int {
var head_table: const Font::Table *;
if (head_table == nullptr) {
return 0;
}
@@ -344,24 +342,24 @@ fn Font::Table::IsReused() const -> bool {
return this->reuse_of != nullptr;
}
fn GetGlyphData(const Font& font, int glyph_index,
const uint8_t** glyph_data, size_t* glyph_size) -> bool {
fn GetGlyphData(font: const woff2::Font &, glyph_index: int,
glyph_data: const uint8_t **, glyph_size: size_t *) -> 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);
var head_table: const Font::Table *;
var loca_table: const Font::Table *;
var glyf_table: const Font::Table *;
if (head_table == nullptr || loca_table == nullptr || glyf_table == nullptr ||
head_table->length < 52) {
return FONT_COMPRESSION_FAILURE();
}
int index_fmt = IndexFormat(font);
var index_fmt: int;
Buffer loca_buf(loca_table->data, loca_table->length);
var loca_buf: woff2::Buffer;
if (index_fmt == 0) {
uint16_t offset1, offset2;
var offset1: uint16_t, var offset2: uint16_t;
if (!loca_buf.Skip(2 * glyph_index) ||
!loca_buf.ReadU16(&offset1) ||
!loca_buf.ReadU16(&offset2) ||
@@ -372,7 +370,7 @@ fn GetGlyphData(const Font& font, int glyph_index,
*glyph_data = glyf_table->data + 2 * offset1;
*glyph_size = 2 * (offset2 - offset1);
} else {
uint32_t offset1, offset2;
var offset1: uint32_t, var offset2: uint32_t;
if (!loca_buf.Skip(4 * glyph_index) ||
!loca_buf.ReadU32(&offset1) ||
!loca_buf.ReadU32(&offset2) ||
@@ -386,9 +384,8 @@ fn GetGlyphData(const Font& font, int glyph_index,
return true;
}
fn RemoveDigitalSignature(Font* font) -> bool {
auto it =
font->tables.find(kDsigTableTag);
fn RemoveDigitalSignature(font: woff2::Font *) -> bool {
var it: std::__map_iterator<std::__tree_iterator<std::__value_type<unsigned int, woff2::Font::Table>, std::__tree_node<std::__value_type<unsigned int, woff2::Font::Table>, void *> *, long>>;
if (it != font->tables.end()) {
font->tables.erase(it);
font->num_tables = font->tables.size();
+2 -2
View File
@@ -51,12 +51,12 @@ class Glyph {
// 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.
fn ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) -> bool;
fn ReadGlyph(data: const uint8_t *, len: size_t, glyph: woff2::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.
fn StoreGlyph(const Glyph& glyph, uint8_t* dst, size_t* dst_size) -> bool;
fn StoreGlyph(glyph: const woff2::Glyph &, dst: uint8_t *, dst_size: size_t *) -> bool;
} // namespace woff2
+68 -68
View File
@@ -15,30 +15,30 @@
namespace woff2 {
static const int32_t kFLAG_ONCURVE = 1;
static const int32_t kFLAG_XSHORT = 1 << 1;
static const int32_t kFLAG_YSHORT = 1 << 2;
static const int32_t kFLAG_REPEAT = 1 << 3;
static const int32_t kFLAG_XREPEATSIGN = 1 << 4;
static const int32_t kFLAG_YREPEATSIGN = 1 << 5;
static const int32_t kFLAG_ARG_1_AND_2_ARE_WORDS = 1 << 0;
static const int32_t kFLAG_WE_HAVE_A_SCALE = 1 << 3;
static const int32_t kFLAG_MORE_COMPONENTS = 1 << 5;
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;
var kFLAG_ONCURVE: const int32_t;
var kFLAG_XSHORT: const int32_t;
var kFLAG_YSHORT: const int32_t;
var kFLAG_REPEAT: const int32_t;
var kFLAG_XREPEATSIGN: const int32_t;
var kFLAG_YREPEATSIGN: const int32_t;
var kFLAG_ARG_1_AND_2_ARE_WORDS: const int32_t;
var kFLAG_WE_HAVE_A_SCALE: const int32_t;
var kFLAG_MORE_COMPONENTS: const int32_t;
var kFLAG_WE_HAVE_AN_X_AND_Y_SCALE: const int32_t;
var kFLAG_WE_HAVE_A_TWO_BY_TWO: const int32_t;
var kFLAG_WE_HAVE_INSTRUCTIONS: const int32_t;
fn ReadCompositeGlyphData(Buffer* buffer, Glyph* glyph) -> bool {
fn ReadCompositeGlyphData(buffer: woff2::Buffer *, glyph: woff2::Glyph *) -> bool {
glyph->have_instructions = false;
glyph->composite_data = buffer->buffer() + buffer->offset();
size_t start_offset = buffer->offset();
uint16_t flags = kFLAG_MORE_COMPONENTS;
var start_offset: size_t;
var flags: uint16_t;
while (flags & kFLAG_MORE_COMPONENTS) {
if (!buffer->ReadU16(&flags)) {
return FONT_COMPRESSION_FAILURE();
}
glyph->have_instructions |= (flags & kFLAG_WE_HAVE_INSTRUCTIONS) != 0;
size_t arg_size = 2; // glyph index
var arg_size: size_t; // glyph index
if (flags & kFLAG_ARG_1_AND_2_ARE_WORDS) {
arg_size += 4;
} else {
@@ -62,10 +62,10 @@ fn ReadCompositeGlyphData(Buffer* buffer, Glyph* glyph) -> bool {
return true;
}
fn ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) -> bool {
Buffer buffer(data, len);
fn ReadGlyph(data: const uint8_t *, len: size_t, glyph: woff2::Glyph *) -> bool {
var buffer: woff2::Buffer;
int16_t num_contours;
var num_contours: int16_t;
if (!buffer.ReadS16(&num_contours)) {
return FONT_COMPRESSION_FAILURE();
}
@@ -88,13 +88,13 @@ fn ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) -> bool {
glyph->contours.resize(num_contours);
// Read the number of points per contour.
uint16_t last_point_index = 0;
for (int i = 0; i < num_contours; ++i) {
uint16_t point_index;
var last_point_index: uint16_t;
for (var i: int; i < num_contours; ++i) {
var point_index: uint16_t;
if (!buffer.ReadU16(&point_index)) {
return FONT_COMPRESSION_FAILURE();
}
uint16_t num_points = point_index - last_point_index + (i == 0 ? 1 : 0);
var num_points: uint16_t;
glyph->contours[i].resize(num_points);
last_point_index = point_index;
}
@@ -109,13 +109,13 @@ fn ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) -> bool {
}
// Read the run-length coded flags.
std::vector<std::vector<uint8_t> > flags(num_contours);
var flags: std::vector<std::vector<uint8_t>>;
{
uint8_t flag = 0;
uint8_t flag_repeat = 0;
for (int i = 0; i < num_contours; ++i) {
var flag: uint8_t;
var flag_repeat: uint8_t;
for (var i: int; i < num_contours; ++i) {
flags[i].resize(glyph->contours[i].size());
for (size_t j = 0; j < glyph->contours[i].size(); ++j) {
for (var j: size_t; j < glyph->contours[i].size(); ++j) {
if (flag_repeat == 0) {
if (!buffer.ReadU8(&flag)) {
return FONT_COMPRESSION_FAILURE();
@@ -135,21 +135,21 @@ fn ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) -> bool {
}
// Read the x coordinates.
int prev_x = 0;
for (int i = 0; i < num_contours; ++i) {
for (size_t j = 0; j < glyph->contours[i].size(); ++j) {
uint8_t flag = flags[i][j];
var prev_x: int;
for (var i: int; i < num_contours; ++i) {
for (var j: size_t; j < glyph->contours[i].size(); ++j) {
var flag: uint8_t;
if (flag & kFLAG_XSHORT) {
// single byte x-delta coord value
uint8_t x_delta;
var x_delta: uint8_t;
if (!buffer.ReadU8(&x_delta)) {
return FONT_COMPRESSION_FAILURE();
}
int sign = (flag & kFLAG_XREPEATSIGN) ? 1 : -1;
var sign: int;
glyph->contours[i][j].x = prev_x + sign * x_delta;
} else {
// double byte x-delta coord value
int16_t x_delta = 0;
var x_delta: int16_t;
if (!(flag & kFLAG_XREPEATSIGN)) {
if (!buffer.ReadS16(&x_delta)) {
return FONT_COMPRESSION_FAILURE();
@@ -162,21 +162,21 @@ fn ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) -> bool {
}
// Read the y coordinates.
int prev_y = 0;
for (int i = 0; i < num_contours; ++i) {
for (size_t j = 0; j < glyph->contours[i].size(); ++j) {
uint8_t flag = flags[i][j];
var prev_y: int;
for (var i: int; i < num_contours; ++i) {
for (var j: size_t; j < glyph->contours[i].size(); ++j) {
var flag: uint8_t;
if (flag & kFLAG_YSHORT) {
// single byte y-delta coord value
uint8_t y_delta;
var y_delta: uint8_t;
if (!buffer.ReadU8(&y_delta)) {
return FONT_COMPRESSION_FAILURE();
}
int sign = (flag & kFLAG_YREPEATSIGN) ? 1 : -1;
var sign: int;
glyph->contours[i][j].y = prev_y + sign * y_delta;
} else {
// double byte y-delta coord value
int16_t y_delta = 0;
var y_delta: int16_t;
if (!(flag & kFLAG_YREPEATSIGN)) {
if (!buffer.ReadS16(&y_delta)) {
return FONT_COMPRESSION_FAILURE();
@@ -212,21 +212,21 @@ fn ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) -> bool {
namespace {
void StoreBbox(const Glyph& glyph, size_t* offset, uint8_t* dst) {
void StoreBbox(glyph: const woff2::Glyph &, offset: size_t *, dst: uint8_t *) {
Store16(glyph.x_min, offset, dst);
Store16(glyph.y_min, offset, dst);
Store16(glyph.x_max, offset, dst);
Store16(glyph.y_max, offset, dst);
}
void StoreInstructions(const Glyph& glyph, size_t* offset, uint8_t* dst) {
void StoreInstructions(glyph: const woff2::Glyph &, offset: size_t *, dst: uint8_t *) {
Store16(glyph.instructions_size, offset, dst);
StoreBytes(glyph.instructions_data, glyph.instructions_size, offset, dst);
}
fn StoreEndPtsOfContours(const Glyph& glyph, size_t* offset, uint8_t* dst) -> bool {
int end_point = -1;
for (const auto& contour : glyph.contours) {
fn StoreEndPtsOfContours(glyph: const woff2::Glyph &, offset: size_t *, dst: uint8_t *) -> bool {
var end_point: int;
for (const auto& contour var __begin2: std::__wrap_iter<const std::vector<woff2::Glyph::Point> *> var __range2: const std::vector<std::vector<woff2::Glyph::Point>> &) {
end_point += contour.size();
if (contour.size() > std::numeric_limits<uint16_t>::max() ||
end_point > std::numeric_limits<uint16_t>::max()) {
@@ -237,21 +237,21 @@ fn StoreEndPtsOfContours(const Glyph& glyph, size_t* offset, uint8_t* dst) -> bo
return true;
}
fn 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;
int last_y = 0;
size_t x_bytes = 0;
size_t y_bytes = 0;
fn StorePoints(glyph: const woff2::Glyph &, offset: size_t *,
dst: uint8_t *, dst_size: size_t) -> bool {
var last_flag: int;
var repeat_count: int;
var last_x: int;
var last_y: int;
var x_bytes: size_t;
var y_bytes: size_t;
// Store the flags and calculate the total size of the x and y coordinates.
for (const auto& contour : glyph.contours) {
for (const auto& point : contour) {
int flag = point.on_curve ? kFLAG_ONCURVE : 0;
int dx = point.x - last_x;
int dy = point.y - last_y;
for (const auto& contour var __begin2: std::__wrap_iter<const std::vector<woff2::Glyph::Point> *> var __range2: const std::vector<std::vector<woff2::Glyph::Point>> &) {
for (const auto& point var __begin3: std::__wrap_iter<const woff2::Glyph::Point *> var __range3: const std::vector<woff2::Glyph::Point> &) {
var flag: int;
var dx: int;
var dy: int;
if (dx == 0) {
flag |= kFLAG_XREPEATSIGN;
} else if (dx > -256 && dx < 256) {
@@ -301,14 +301,14 @@ fn StorePoints(const Glyph& glyph, size_t* offset,
}
// Store the x and y coordinates.
size_t x_offset = *offset;
size_t y_offset = *offset + x_bytes;
var x_offset: size_t;
var y_offset: size_t;
last_x = 0;
last_y = 0;
for (const auto& contour : glyph.contours) {
for (const auto& point : contour) {
int dx = point.x - last_x;
int dy = point.y - last_y;
for (const auto& contour var __begin2: std::__wrap_iter<const std::vector<woff2::Glyph::Point> *> var __range2: const std::vector<std::vector<woff2::Glyph::Point>> &) {
for (const auto& point var __begin3: std::__wrap_iter<const woff2::Glyph::Point *> var __range3: const std::vector<woff2::Glyph::Point> &) {
var dx: int;
var dy: int;
if (dx == 0) {
// pass
} else if (dx > -256 && dx < 256) {
@@ -333,8 +333,8 @@ fn StorePoints(const Glyph& glyph, size_t* offset,
} // namespace
fn StoreGlyph(const Glyph& glyph, uint8_t* dst, size_t* dst_size) -> bool {
size_t offset = 0;
fn StoreGlyph(glyph: const woff2::Glyph &, dst: uint8_t *, dst_size: size_t *) -> bool {
var offset: size_t;
if (glyph.composite_data_size > 0) {
// Composite glyph.
if (*dst_size < ((10ULL + glyph.composite_data_size) +
+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.
fn NormalizeOffsets(Font* font) -> bool;
fn NormalizeOffsets(font: woff2::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.
fn FixChecksums(Font* font) -> bool;
fn FixChecksums(font: woff2::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.
fn NormalizeGlyphs(Font* font) -> bool;
fn NormalizeGlyphs(font: woff2::Font *) -> bool;
// Performs all of the normalization steps above.
fn NormalizeFont(Font* font) -> bool;
fn NormalizeFontCollection(FontCollection* font_collection) -> bool;
fn NormalizeFont(font: woff2::Font *) -> bool;
fn NormalizeFontCollection(font_collection: woff2::FontCollection *) -> bool;
} // namespace woff2
+56 -57
View File
@@ -24,7 +24,7 @@ namespace woff2 {
namespace {
void StoreLoca(int index_fmt, uint32_t value, size_t* offset, uint8_t* dst) {
void StoreLoca(index_fmt: int, value: uint32_t, offset: size_t *, dst: uint8_t *) {
if (index_fmt == 0) {
Store16(value >> 1, offset, dst);
} else {
@@ -36,29 +36,29 @@ void StoreLoca(int index_fmt, uint32_t value, size_t* offset, uint8_t* dst) {
namespace {
fn WriteNormalizedLoca(int index_fmt, int num_glyphs, Font* font) -> bool {
Font::Table* glyf_table = font->FindTable(kGlyfTableTag);
Font::Table* loca_table = font->FindTable(kLocaTableTag);
fn WriteNormalizedLoca(index_fmt: int, num_glyphs: int, font: woff2::Font *) -> bool {
var glyf_table: Font::Table *;
var loca_table: Font::Table *;
int glyph_sz = index_fmt == 0 ? 2 : 4;
var glyph_sz: int;
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] : nullptr;
uint8_t* loca_dst = &loca_table->buffer[0];
uint32_t glyf_offset = 0;
size_t loca_offset = 0;
var glyf_dst: uint8_t *;
var loca_dst: uint8_t *;
var glyf_offset: uint32_t;
var loca_offset: size_t;
for (int i = 0; i < num_glyphs; ++i) {
for (var i: int; i < num_glyphs; ++i) {
StoreLoca(index_fmt, glyf_offset, &loca_offset, loca_dst);
Glyph glyph;
const uint8_t* glyph_data;
size_t glyph_size;
var glyph: woff2::Glyph;
var glyph_data: const uint8_t *;
var glyph_size: size_t;
if (!GetGlyphData(*font, i, &glyph_data, &glyph_size) ||
(glyph_size > 0 && !ReadGlyph(glyph_data, glyph_size, &glyph))) {
return FONT_COMPRESSION_FAILURE();
}
size_t glyf_dst_size = glyf_table->buffer.size() - glyf_offset;
var glyf_dst_size: size_t;
if (!StoreGlyph(glyph, glyf_dst + glyf_offset, &glyf_dst_size)) {
return FONT_COMPRESSION_FAILURE();
}
@@ -85,17 +85,17 @@ fn WriteNormalizedLoca(int index_fmt, int num_glyphs, Font* font) -> bool {
namespace {
fn MakeEditableBuffer(Font* font, int tableTag) -> bool {
Font::Table* table = font->FindTable(tableTag);
fn MakeEditableBuffer(font: woff2::Font *, tableTag: int) -> bool {
var table: Font::Table *;
if (table == nullptr) {
return FONT_COMPRESSION_FAILURE();
}
if (table->IsReused()) {
return true;
}
int sz = Round4(table->length);
var sz: int;
table->buffer.resize(sz);
uint8_t* buf = &table->buffer[0];
var buf: uint8_t *;
memcpy(buf, table->data, table->length);
if (PREDICT_FALSE(sz > table->length)) {
memset(buf + table->length, 0, sz - table->length);
@@ -106,10 +106,10 @@ fn MakeEditableBuffer(Font* font, int tableTag) -> bool {
} // namespace
fn 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);
fn NormalizeGlyphs(font: woff2::Font *) -> bool {
var head_table: Font::Table *;
var glyf_table: Font::Table *;
var loca_table: Font::Table *;
if (head_table == nullptr) {
return FONT_COMPRESSION_FAILURE();
}
@@ -129,8 +129,8 @@ fn NormalizeGlyphs(Font* font) -> bool {
return true;
}
int index_fmt = head_table->data[51];
int num_glyphs = NumGlyphs(*font);
var index_fmt: int;
var num_glyphs: int;
// 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
@@ -141,7 +141,7 @@ fn NormalizeGlyphs(Font* font) -> bool {
// 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;
var max_normalized_glyf_size: size_t;
glyf_table->buffer.resize(max_normalized_glyf_size);
@@ -163,10 +163,10 @@ fn NormalizeGlyphs(Font* font) -> bool {
return true;
}
fn NormalizeOffsets(Font* font) -> bool {
uint32_t offset = 12 + 16 * font->num_tables;
for (auto tag : font->OutputOrderedTags()) {
auto& table = font->tables[tag];
fn NormalizeOffsets(font: woff2::Font *) -> bool {
var offset: uint32_t;
for (auto tag var __begin1: std::__wrap_iter<unsigned int *> var __range1: std::vector<unsigned int> &&) {
var table: woff2::Font::Table &;
table.offset = offset;
offset += Round4(table.length);
}
@@ -175,15 +175,15 @@ fn NormalizeOffsets(Font* font) -> bool {
namespace {
fn 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;
uint16_t range_shift = (font.num_tables << 4) - search_range;
fn ComputeHeaderChecksum(font: const woff2::Font &) -> uint32_t {
var checksum: uint32_t;
var max_pow2: uint16_t;
var search_range: uint16_t;
var range_shift: uint16_t;
checksum += (font.num_tables << 16 | search_range);
checksum += (max_pow2 << 16 | range_shift);
for (const auto& i : font.tables) {
const Font::Table* table = &i.second;
for (const auto& i var __begin2: std::__map_const_iterator<std::__tree_const_iterator<std::__value_type<unsigned int, woff2::Font::Table>, std::__tree_node<std::__value_type<unsigned int, woff2::Font::Table>, void *> *, long>> var __range2: const std::map<unsigned int, woff2::Font::Table> &) {
var table: const Font::Table *;
if (table->IsReused()) {
table = table->reuse_of;
}
@@ -197,8 +197,8 @@ fn ComputeHeaderChecksum(const Font& font) -> uint32_t {
} // namespace
fn FixChecksums(Font* font) -> bool {
Font::Table* head_table = font->FindTable(kHeadTableTag);
fn FixChecksums(font: woff2::Font *) -> bool {
var head_table: Font::Table *;
if (head_table == nullptr) {
return FONT_COMPRESSION_FAILURE();
}
@@ -209,13 +209,13 @@ fn FixChecksums(Font* font) -> bool {
return FONT_COMPRESSION_FAILURE();
}
uint8_t* head_buf = &head_table->buffer[0];
size_t offset = 8;
var head_buf: uint8_t *;
var offset: size_t;
StoreU32(0, &offset, head_buf);
uint32_t file_checksum = 0;
uint32_t head_checksum = 0;
for (auto& i : font->tables) {
Font::Table* table = &i.second;
var file_checksum: uint32_t;
var head_checksum: uint32_t;
for (auto& i var __begin1: std::__map_iterator<std::__tree_iterator<std::__value_type<unsigned int, woff2::Font::Table>, std::__tree_node<std::__value_type<unsigned int, woff2::Font::Table>, void *> *, long>> var __range1: std::map<unsigned int, woff2::Font::Table> &) {
var table: Font::Table *;
if (table->IsReused()) {
table = table->reuse_of;
}
@@ -235,8 +235,8 @@ fn FixChecksums(Font* font) -> bool {
}
namespace {
fn MarkTransformed(Font* font) -> bool {
Font::Table* head_table = font->FindTable(kHeadTableTag);
fn MarkTransformed(font: woff2::Font *) -> bool {
var head_table: Font::Table *;
if (head_table == nullptr) {
return FONT_COMPRESSION_FAILURE();
}
@@ -248,14 +248,14 @@ fn MarkTransformed(Font* font) -> bool {
}
// set bit 11 of head table 'flags' to indicate that font has undergone
// lossless modifying transform
int head_flags = head_table->data[16];
var head_flags: int;
head_table->buffer[16] = head_flags | 0x08;
return true;
}
} // namespace
fn NormalizeWithoutFixingChecksums(Font* font) -> bool {
fn NormalizeWithoutFixingChecksums(font: woff2::Font *) -> bool {
return (MakeEditableBuffer(font, kHeadTableTag) &&
RemoveDigitalSignature(font) &&
MarkTransformed(font) &&
@@ -263,19 +263,18 @@ fn NormalizeWithoutFixingChecksums(Font* font) -> bool {
NormalizeOffsets(font));
}
fn NormalizeFont(Font* font) -> bool {
fn NormalizeFont(font: woff2::Font *) -> bool {
return (NormalizeWithoutFixingChecksums(font) &&
FixChecksums(font));
}
fn NormalizeFontCollection(FontCollection* font_collection) -> bool {
fn NormalizeFontCollection(font_collection: woff2::FontCollection *) -> bool {
if (font_collection->fonts.size() == 1) {
return NormalizeFont(&font_collection->fonts[0]);
}
uint32_t offset = CollectionHeaderSize(font_collection->header_version,
font_collection->fonts.size());
for (auto& font : font_collection->fonts) {
var offset: uint32_t;
for (auto& font var __begin1: std::__wrap_iter<woff2::Font *> var __range1: std::vector<woff2::Font> &) {
if (!NormalizeWithoutFixingChecksums(&font)) {
#ifdef FONT_COMPRESSION_BIN
fprintf(stderr, "Font normalization failed.\n");
@@ -286,9 +285,9 @@ fn NormalizeFontCollection(FontCollection* font_collection) -> bool {
}
// Start table offsets after TTC Header and Sfnt Headers
for (auto& font : font_collection->fonts) {
for (auto tag : font.OutputOrderedTags()) {
Font::Table& table = font.tables[tag];
for (auto& font var __begin1: std::__wrap_iter<woff2::Font *> var __range1: std::vector<woff2::Font> &) {
for (auto tag var __begin2: std::__wrap_iter<unsigned int *> var __range2: std::vector<unsigned int> &&) {
var table: Font::Table &;
if (table.IsReused()) {
table.offset = table.reuse_of->offset;
} else {
@@ -299,7 +298,7 @@ fn NormalizeFontCollection(FontCollection* font_collection) -> bool {
}
// Now we can fix the checksums
for (auto& font : font_collection->fonts) {
for (auto& font var __begin1: std::__wrap_iter<woff2::Font *> var __range1: std::vector<woff2::Font> &) {
if (!FixChecksums(&font)) {
#ifdef FONT_COMPRESSION_BIN
fprintf(stderr, "Failed to fix checksums\n");
+1 -1
View File
@@ -15,7 +15,7 @@ namespace woff2 {
using uint32 = unsigned int;
fn auto Log2Floor(uint32 n) -> int {
fn auto Log2Floor(n: woff2::uint32) -> 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> fn Round4(T value) -> T {
template<typename T> fn Round4(value: T) -> T {
if (std::numeric_limits<T>::max() - value < 3) {
return value;
}
+6 -6
View File
@@ -18,7 +18,7 @@
namespace woff2 {
fn auto StoreU32(uint8_t* dst, size_t offset, uint32_t x) -> size_t {
fn auto StoreU32(dst: uint8_t *, offset: size_t, x: uint32_t) -> size_t {
dst[offset] = x >> 24;
dst[offset + 1] = x >> 16;
dst[offset + 2] = x >> 8;
@@ -26,7 +26,7 @@ fn auto StoreU32(uint8_t* dst, size_t offset, uint32_t x) -> size_t {
return offset + 4;
}
fn auto Store16(uint8_t* dst, size_t offset, int x) -> size_t {
fn auto Store16(dst: uint8_t *, offset: size_t, x: int) -> size_t {
#if defined(WOFF_LITTLE_ENDIAN)
*reinterpret_cast<uint16_t*>(dst + offset) =
((x & 0xFF) << 8) | ((x & 0xFF00) >> 8);
@@ -39,14 +39,14 @@ fn auto Store16(uint8_t* dst, size_t offset, int x) -> size_t {
return offset + 2;
}
inline void StoreU32(uint32_t val, size_t* offset, uint8_t* dst) {
inline void StoreU32(val: uint32_t, offset: size_t *, dst: uint8_t *) {
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) {
inline void Store16(val: int, offset: size_t *, dst: uint8_t *) {
#if defined(WOFF_LITTLE_ENDIAN)
*reinterpret_cast<uint16_t*>(dst + *offset) =
((val & 0xFF) << 8) | ((val & 0xFF00) >> 8);
@@ -60,8 +60,8 @@ inline void Store16(int val, size_t* offset, uint8_t* dst) {
#endif
}
inline void StoreBytes(const uint8_t* data, size_t len,
size_t* offset, uint8_t* dst) {
inline void StoreBytes(data: const uint8_t *, len: size_t,
offset: size_t *, dst: uint8_t *) {
memcpy(&dst[*offset], data, len);
*offset += len;
}
+9 -9
View File
@@ -14,16 +14,16 @@
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;
static const uint32_t kHmtxTableTag = 0x686d7478;
static const uint32_t kHheaTableTag = 0x68686561;
static const uint32_t kMaxpTableTag = 0x6d617870;
var kGlyfTableTag: const uint32_t;
var kHeadTableTag: const uint32_t;
var kLocaTableTag: const uint32_t;
var kDsigTableTag: const uint32_t;
var kCffTableTag: const uint32_t;
var kHmtxTableTag: const uint32_t;
var kHheaTableTag: const uint32_t;
var kMaxpTableTag: const uint32_t;
extern const uint32_t kKnownTags[];
var kKnownTags: const uint32_t [];
} // namespace woff2
@@ -13,70 +13,6 @@ 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
};
var kKnownTags: const uint32_t [63];
} // namespace woff2
+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.
fn TransformGlyfAndLocaTables(Font* font) -> bool;
fn TransformGlyfAndLocaTables(font: woff2::Font *) -> bool;
// Apply transformation to hmtx table if applicable for this font.
fn TransformHmtxTable(Font* font) -> bool;
fn TransformHmtxTable(font: woff2::Font *) -> bool;
} // namespace woff2
+78 -78
View File
@@ -20,29 +20,29 @@ namespace woff2 {
namespace {
const int FLAG_ARG_1_AND_2_ARE_WORDS = 1 << 0;
const int FLAG_WE_HAVE_INSTRUCTIONS = 1 << 8;
var FLAG_ARG_1_AND_2_ARE_WORDS: const int;
var FLAG_WE_HAVE_INSTRUCTIONS: const int;
void WriteBytes(std::vector<uint8_t>* out, const uint8_t* data, size_t len) {
void WriteBytes(out: std::vector<uint8_t> *, data: const uint8_t *, len: size_t) {
if (len == 0) { return;
}
size_t offset = out->size();
var offset: size_t;
out->resize(offset + len);
memcpy(&(*out)[offset], data, len);
}
void WriteBytes(std::vector<uint8_t>* out, const std::vector<uint8_t>& in) {
for (unsigned char i : in) {
void WriteBytes(out: std::vector<uint8_t> *, in: const std::vector<uint8_t> &) {
for (unsigned char i var __begin2: std::__wrap_iter<const unsigned char *> var __range2: const std::vector<unsigned char> &) {
out->push_back(i);
}
}
void WriteUShort(std::vector<uint8_t>* out, int value) {
void WriteUShort(out: std::vector<uint8_t> *, value: int) {
out->push_back(value >> 8);
out->push_back(value & 255);
}
void WriteLong(std::vector<uint8_t>* out, int value) {
void WriteLong(out: std::vector<uint8_t> *, value: int) {
out->push_back((value >> 24) & 255);
out->push_back((value >> 16) & 255);
out->push_back((value >> 8) & 255);
@@ -53,12 +53,12 @@ void WriteLong(std::vector<uint8_t>* out, int value) {
// GlyfEncoder.java
class GlyfEncoder {
public:
explicit GlyfEncoder(int num_glyphs)
explicit GlyfEncoder(num_glyphs: int)
: n_glyphs_(num_glyphs) {
bbox_bitmap_.resize(((num_glyphs + 31) >> 5) << 2);
}
fn Encode(int glyph_id, const Glyph& glyph) -> bool {
fn Encode(glyph_id: int, glyph: const woff2::Glyph &) -> bool {
if (glyph.composite_data_size > 0) {
WriteCompositeGlyph(glyph_id, glyph);
} else if (glyph.contours.size() > 0) {
@@ -69,7 +69,7 @@ class GlyfEncoder {
return true;
}
void GetTransformedGlyfBytes(std::vector<uint8_t>* result) {
void GetTransformedGlyfBytes(result: std::vector<uint8_t> *) {
WriteLong(result, 0); // version
WriteUShort(result, n_glyphs_);
WriteUShort(result, 0); // index_format, will be set later
@@ -91,23 +91,23 @@ class GlyfEncoder {
}
private:
void WriteInstructions(const Glyph& glyph) {
void WriteInstructions(glyph: const woff2::Glyph &) {
Write255UShort(&glyph_stream_, glyph.instructions_size);
WriteBytes(&instruction_stream_,
glyph.instructions_data, glyph.instructions_size);
}
fn ShouldWriteSimpleGlyphBbox(const Glyph& glyph) -> bool {
fn ShouldWriteSimpleGlyphBbox(glyph: const woff2::Glyph &) -> bool {
if (glyph.contours.empty() || glyph.contours[0].empty()) {
return glyph.x_min || glyph.y_min || glyph.x_max || glyph.y_max;
}
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) {
var x_min: int16_t;
var y_min: int16_t;
var x_max: int16_t;
var y_max: int16_t;
for (const auto& contour var __begin2: std::__wrap_iter<const std::vector<woff2::Glyph::Point> *> var __range2: const std::vector<std::vector<woff2::Glyph::Point>> &) {
for (const auto& point var __begin3: std::__wrap_iter<const woff2::Glyph::Point *> var __range3: const std::vector<woff2::Glyph::Point> &) {
if (point.x < x_min) { x_min = point.x;
}
if (point.x > x_max) { x_max = point.x;
@@ -135,24 +135,24 @@ class GlyfEncoder {
return false;
}
void WriteSimpleGlyph(int glyph_id, const Glyph& glyph) {
int num_contours = glyph.contours.size();
void WriteSimpleGlyph(glyph_id: int, glyph: const woff2::Glyph &) {
var num_contours: int;
WriteUShort(&n_contour_stream_, num_contours);
if (ShouldWriteSimpleGlyphBbox(glyph)) {
WriteBbox(glyph_id, glyph);
}
for (int i = 0; i < num_contours; i++) {
for (var i: int; 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;
var lastX: int;
var lastY: int;
for (var i: int; i < num_contours; i++) {
var num_points: int;
for (var j: int; j < num_points; j++) {
var x: int;
var y: int;
var dx: int;
var dy: int;
WriteTriplet(glyph.contours[i][j].on_curve, dx, dy);
lastX = x;
lastY = y;
@@ -163,7 +163,7 @@ class GlyfEncoder {
}
}
void WriteCompositeGlyph(int glyph_id, const Glyph& glyph) {
void WriteCompositeGlyph(glyph_id: int, glyph: const woff2::Glyph &) {
WriteUShort(&n_contour_stream_, -1);
WriteBbox(glyph_id, glyph);
WriteBytes(&composite_stream_,
@@ -174,7 +174,7 @@ class GlyfEncoder {
}
}
void WriteBbox(int glyph_id, const Glyph& glyph) {
void WriteBbox(glyph_id: int, glyph: const woff2::Glyph &) {
bbox_bitmap_[glyph_id >> 3] |= 0x80 >> (glyph_id & 7);
WriteUShort(&bbox_stream_, glyph.x_min);
WriteUShort(&bbox_stream_, glyph.y_min);
@@ -182,13 +182,13 @@ class GlyfEncoder {
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;
void WriteTriplet(on_curve: bool, x: int, y: int) {
var abs_x: int;
var abs_y: int;
var on_curve_bit: int;
var x_sign_bit: int;
var y_sign_bit: int;
var xy_sign_bits: int;
if (x == 0 && abs_y < 1280) {
flag_byte_stream_.push_back(on_curve_bit +
((abs_y & 0xf00) >> 7) + y_sign_bit);
@@ -236,10 +236,10 @@ class GlyfEncoder {
} // namespace
fn TransformGlyfAndLocaTables(Font* font) -> bool {
fn TransformGlyfAndLocaTables(font: woff2::Font *) -> bool {
// no transform for CFF
const Font::Table* glyf_table = font->FindTable(kGlyfTableTag);
const Font::Table* loca_table = font->FindTable(kLocaTableTag);
var glyf_table: const Font::Table *;
var loca_table: const Font::Table *;
// If you don't have glyf/loca this transform isn't very interesting
if (loca_table == nullptr && glyf_table == nullptr) {
@@ -257,15 +257,15 @@ fn TransformGlyfAndLocaTables(Font* font) -> bool {
return true;
}
Font::Table* transformed_glyf = &font->tables[kGlyfTableTag ^ 0x80808080];
Font::Table* transformed_loca = &font->tables[kLocaTableTag ^ 0x80808080];
var transformed_glyf: Font::Table *;
var transformed_loca: Font::Table *;
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;
var num_glyphs: int;
var encoder: woff2::(anonymous namespace)::GlyfEncoder;
for (var i: int; i < num_glyphs; ++i) {
var glyph: woff2::Glyph;
var glyph_data: const uint8_t *;
var glyph_size: size_t;
if (!GetGlyphData(*font, i, &glyph_data, &glyph_size) ||
(glyph_size > 0 && !ReadGlyph(glyph_data, glyph_size, &glyph))) {
return FONT_COMPRESSION_FAILURE();
@@ -274,7 +274,7 @@ fn TransformGlyfAndLocaTables(Font* font) -> bool {
}
encoder.GetTransformedGlyfBytes(&transformed_glyf->buffer);
const Font::Table* head_table = font->FindTable(kHeadTableTag);
var head_table: const Font::Table *;
if (head_table == nullptr || head_table->length < 52) {
return FONT_COMPRESSION_FAILURE();
}
@@ -293,10 +293,10 @@ fn TransformGlyfAndLocaTables(Font* font) -> bool {
// See https://www.microsoft.com/typography/otspec/hmtx.htm
// See WOFF2 spec, 5.4. Transformed hmtx table format
fn 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);
fn TransformHmtxTable(font: woff2::Font *) -> bool {
var glyf_table: const Font::Table *;
var hmtx_table: const Font::Table *;
var hhea_table: const Font::Table *;
// If you don't have hmtx or a glyf not much is going to happen here
if (hmtx_table == nullptr || glyf_table == nullptr) {
@@ -309,8 +309,8 @@ fn TransformHmtxTable(Font* font) -> bool {
}
// Skip 34 to reach 'hhea' numberOfHMetrics
Buffer hhea_buf(hhea_table->data, hhea_table->length);
uint16_t num_hmetrics;
var hhea_buf: woff2::Buffer;
var num_hmetrics: uint16_t;
if (!hhea_buf.Skip(34) || !hhea_buf.ReadU16(&num_hmetrics)) {
return FONT_COMPRESSION_FAILURE();
}
@@ -320,28 +320,28 @@ fn TransformHmtxTable(Font* font) -> bool {
return FONT_COMPRESSION_FAILURE();
}
int num_glyphs = NumGlyphs(*font);
var num_glyphs: int;
// Most fonts can be transformed; assume it's a go until proven otherwise
std::vector<uint16_t> advance_widths;
std::vector<int16_t> proportional_lsbs;
std::vector<int16_t> monospace_lsbs;
var advance_widths: std::vector<uint16_t>;
var proportional_lsbs: std::vector<int16_t>;
var monospace_lsbs: std::vector<int16_t>;
bool remove_proportional_lsb = true;
bool remove_monospace_lsb = (num_glyphs - num_hmetrics) > 0;
var remove_proportional_lsb: bool;
var remove_monospace_lsb: bool;
Buffer hmtx_buf(hmtx_table->data, hmtx_table->length);
for (int i = 0; i < num_glyphs; i++) {
Glyph glyph;
const uint8_t* glyph_data;
size_t glyph_size;
var hmtx_buf: woff2::Buffer;
for (var i: int; i < num_glyphs; i++) {
var glyph: woff2::Glyph;
var glyph_data: const uint8_t *;
var glyph_size: size_t;
if (!GetGlyphData(*font, i, &glyph_data, &glyph_size) ||
(glyph_size > 0 && !ReadGlyph(glyph_data, glyph_size, &glyph))) {
return FONT_COMPRESSION_FAILURE();
}
uint16_t advance_width = 0;
int16_t lsb = 0;
var advance_width: uint16_t;
var lsb: int16_t;
if (i < num_hmetrics) {
// [0, num_hmetrics) are proportional hMetrics
@@ -376,10 +376,10 @@ fn TransformHmtxTable(Font* font) -> bool {
}
}
Font::Table* transformed_hmtx = &font->tables[kHmtxTableTag ^ 0x80808080];
var transformed_hmtx: Font::Table *;
uint8_t flags = 0;
size_t transformed_size = 1 + 2 * advance_widths.size();
var flags: uint8_t;
var transformed_size: size_t;
if (remove_proportional_lsb) {
flags |= 1;
} else {
@@ -392,19 +392,19 @@ fn TransformHmtxTable(Font* font) -> bool {
}
transformed_hmtx->buffer.reserve(transformed_size);
std::vector<uint8_t>* out = &transformed_hmtx->buffer;
var out: std::vector<uint8_t> *;
WriteBytes(out, &flags, 1);
for (uint16_t advance_width : advance_widths) {
for (uint16_t advance_width var __begin1: std::__wrap_iter<unsigned short *> var __range1: std::vector<unsigned short> &) {
WriteUShort(out, advance_width);
}
if (!remove_proportional_lsb) {
for (int16_t lsb : proportional_lsbs) {
for (int16_t lsb var __begin2: std::__wrap_iter<short *> var __range2: std::vector<short> &) {
WriteUShort(out, lsb);
}
}
if (!remove_monospace_lsb) {
for (int16_t lsb : monospace_lsbs) {
for (int16_t lsb var __begin2: std::__wrap_iter<short *> var __range2: std::vector<short> &) {
WriteUShort(out, lsb);
}
}
@@ -15,14 +15,14 @@
namespace woff2 {
fn Size255UShort(uint16_t value) -> size_t;
fn 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);
fn Size255UShort(value: uint16_t) -> size_t;
fn Read255UShort(buf: woff2::Buffer *, value: unsigned int *) -> bool;
void Write255UShort(out: std::vector<uint8_t> *, value: int);
void Store255UShort(val: int, offset: size_t *, dst: uint8_t *);
fn Base128Size(size_t n) -> size_t;
fn ReadBase128(Buffer* buf, uint32_t* value) -> bool;
void StoreBase128(size_t len, size_t* offset, uint8_t* dst);
fn Base128Size(n: size_t) -> size_t;
fn ReadBase128(buf: woff2::Buffer *, value: uint32_t *) -> bool;
void StoreBase128(len: size_t, offset: size_t *, dst: uint8_t *);
} // namespace woff2
@@ -10,8 +10,8 @@
namespace woff2 {
fn Size255UShort(uint16_t value) -> size_t {
size_t result = 3;
fn Size255UShort(value: uint16_t) -> size_t {
var result: size_t;
if (value < 253) {
result = 1;
} else if (value < 762) {
@@ -22,7 +22,7 @@ fn Size255UShort(uint16_t value) -> size_t {
return result;
}
void Write255UShort(std::vector<uint8_t>* out, int value) {
void Write255UShort(out: std::vector<uint8_t> *, value: int) {
if (value < 253) {
out->push_back(value);
} else if (value < 506) {
@@ -38,40 +38,40 @@ void Write255UShort(std::vector<uint8_t>* out, int value) {
}
}
void Store255UShort(int val, size_t* offset, uint8_t* dst) {
std::vector<uint8_t> packed;
void Store255UShort(val: int, offset: size_t *, dst: uint8_t *) {
var packed: std::vector<uint8_t>;
Write255UShort(&packed, val);
for (uint8_t packed_byte : packed) {
for (uint8_t packed_byte var __begin1: std::__wrap_iter<unsigned char *> var __range1: std::vector<unsigned char> &) {
dst[(*offset)++] = packed_byte;
}
}
// Based on section 6.1.1 of MicroType Express draft spec
fn Read255UShort(Buffer* buf, unsigned int* value) -> bool {
static const int kWordCode = 253;
static const int kOneMoreByteCode2 = 254;
static const int kOneMoreByteCode1 = 255;
static const int kLowestUCode = 253;
uint8_t code = 0;
fn Read255UShort(buf: woff2::Buffer *, value: unsigned int *) -> bool {
var kWordCode: const int;
var kOneMoreByteCode2: const int;
var kOneMoreByteCode1: const int;
var kLowestUCode: const int;
var code: uint8_t;
if (!buf->ReadU8(&code)) {
return FONT_COMPRESSION_FAILURE();
}
if (code == kWordCode) {
uint16_t result = 0;
var result: uint16_t;
if (!buf->ReadU16(&result)) {
return FONT_COMPRESSION_FAILURE();
}
*value = result;
return true;
} else if (code == kOneMoreByteCode1) {
uint8_t result = 0;
var result: uint8_t;
if (!buf->ReadU8(&result)) {
return FONT_COMPRESSION_FAILURE();
}
*value = result + kLowestUCode;
return true;
} else if (code == kOneMoreByteCode2) {
uint8_t result = 0;
var result: uint8_t;
if (!buf->ReadU8(&result)) {
return FONT_COMPRESSION_FAILURE();
}
@@ -83,10 +83,10 @@ fn Read255UShort(Buffer* buf, unsigned int* value) -> bool {
}
}
fn ReadBase128(Buffer* buf, uint32_t* value) -> bool {
uint32_t result = 0;
for (size_t i = 0; i < 5; ++i) {
uint8_t code = 0;
fn ReadBase128(buf: woff2::Buffer *, value: uint32_t *) -> bool {
var result: uint32_t;
for (var i: size_t; i < 5; ++i) {
var code: uint8_t;
if (!buf->ReadU8(&code)) {
return FONT_COMPRESSION_FAILURE();
}
@@ -108,17 +108,17 @@ fn ReadBase128(Buffer* buf, uint32_t* value) -> bool {
return FONT_COMPRESSION_FAILURE();
}
fn Base128Size(size_t n) -> size_t {
size_t size = 1;
fn Base128Size(n: size_t) -> size_t {
var size: size_t;
for (; n >= 128; n >>= 7) { ++size;
}
return size;
}
void StoreBase128(size_t len, size_t* offset, uint8_t* dst) {
size_t size = Base128Size(len);
for (size_t i = 0; i < size; ++i) {
int b = static_cast<int>((len >> (7 * (size - i - 1))) & 0x7f);
void StoreBase128(len: size_t, offset: size_t *, dst: uint8_t *) {
var size: size_t;
for (var i: size_t; i < size; ++i) {
var b: int;
if (i < size - 1) {
b |= 0x80;
}
+8 -8
View File
@@ -16,16 +16,16 @@
namespace woff2 {
static const uint32_t kWoff2Signature = 0x774f4632; // "wOF2"
var kWoff2Signature: const uint32_t; // "wOF2"
// Leave the first byte open to store flag_byte
const unsigned int kWoff2FlagsTransform = 1 << 8;
var kWoff2FlagsTransform: const unsigned int;
// TrueType Collection ID string: 'ttcf'
static const uint32_t kTtcFontFlavor = 0x74746366;
var kTtcFontFlavor: const uint32_t;
static const size_t kSfntHeaderSize = 12;
static const size_t kSfntEntrySize = 16;
var kSfntHeaderSize: const size_t;
var kSfntEntrySize: const size_t;
struct Point {
int x;
@@ -45,7 +45,7 @@ struct Table {
uint32_t dst_length;
const uint8_t* dst_data;
fn operator<(const Table& other) const -> bool {
fn operator<(other: const woff2::Table &) 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
fn CollectionHeaderSize(uint32_t header_version, uint32_t num_fonts) -> size_t;
fn CollectionHeaderSize(header_version: uint32_t, num_fonts: uint32_t) -> size_t;
// Compute checksum over size bytes of buf
fn ComputeULongSum(const uint8_t* buf, size_t size) -> uint32_t;
fn ComputeULongSum(buf: const uint8_t *, size: size_t) -> uint32_t;
} // namespace woff2
@@ -15,12 +15,12 @@
namespace woff2 {
fn 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) {
fn ComputeULongSum(buf: const uint8_t *, size: size_t) -> uint32_t {
var checksum: uint32_t;
var aligned_size: size_t;
for (var i: size_t; i < aligned_size; i += 4) {
#if defined(WOFF_LITTLE_ENDIAN)
uint32_t v = *reinterpret_cast<const uint32_t*>(buf + i);
var v: uint32_t;
checksum += (((v & 0xFF) << 24) | ((v & 0xFF00) << 8) |
((v & 0xFF0000) >> 8) | ((v & 0xFF000000) >> 24));
#elif defined(WOFF_BIG_ENDIAN)
@@ -33,8 +33,8 @@ fn ComputeULongSum(const uint8_t* buf, size_t size) -> uint32_t {
// treat size not aligned on 4 as if it were padded to 4 with 0's
if (size != aligned_size) {
uint32_t v = 0;
for (size_t i = aligned_size; i < size; ++i) {
var v: uint32_t;
for (var i: size_t; i < size; ++i) {
v |= buf[i] << (24 - 8 * (i & 3));
}
checksum += v;
@@ -43,8 +43,8 @@ fn ComputeULongSum(const uint8_t* buf, size_t size) -> uint32_t {
return checksum;
}
fn CollectionHeaderSize(uint32_t header_version, uint32_t num_fonts) -> size_t {
size_t size = 0;
fn CollectionHeaderSize(header_version: uint32_t, num_fonts: uint32_t) -> size_t {
var size: size_t;
if (header_version == 0x00020000) {
size += 12; // ulDsig{Tag,Length,Offset}
}
@@ -12,7 +12,7 @@
#include <woff2/encode.h>
fn main(int argc, char **argv) -> int {
fn main(argc: int, argv: char **) -> int {
using std::string;
if (argc != 2) {
@@ -20,18 +20,18 @@ fn main(int argc, char **argv) -> int {
return 1;
}
string filename(argv[1]);
string outfilename = filename.substr(0, filename.find_last_of('.')) + ".woff2";
var filename: std::string;
var outfilename: std::string;
fprintf(stdout, "Processing %s => %s\n",
filename.c_str(), outfilename.c_str());
string input = woff2::GetFileContent(filename);
var input: std::string;
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);
auto* output_data = reinterpret_cast<uint8_t*>(&output[0]);
var input_data: const unsigned char *;
var output_size: size_t;
var output: std::string;
var output_data: unsigned char *;
woff2::WOFF2Params params;
var params: woff2::WOFF2Params;
if (!woff2::ConvertTTFToWOFF2(input_data, input.size(),
output_data, &output_size, params)) {
fprintf(stderr, "Compression failed.\n");
File diff suppressed because it is too large Load Diff
@@ -13,7 +13,7 @@
#include <woff2/decode.h>
fn main(int argc, char **argv) -> int {
fn main(argc: int, argv: char **) -> int {
using std::string;
if (argc != 2) {
@@ -21,17 +21,16 @@ fn main(int argc, char **argv) -> int {
return 1;
}
string filename(argv[1]);
string outfilename = filename.substr(0, filename.find_last_of('.')) + ".ttf";
var filename: std::string;
var outfilename: std::string;
// Note: update woff2_dec_fuzzer_new_entry.cc if this pattern changes.
string input = woff2::GetFileContent(filename);
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);
var input: std::string;
var raw_input: const unsigned char *;
var output: std::string;
var out: woff2::WOFF2StringOut;
const bool ok = woff2::ConvertWOFF2ToTTF(raw_input, input.size(), &out);
var ok: const bool;
if (ok) {
woff2::SetFileContents(outfilename, output.begin(),
+92 -100
View File
@@ -35,12 +35,12 @@ using std::string;
using std::vector;
const size_t kWoff2HeaderSize = 48;
const size_t kWoff2EntrySize = 20;
var kWoff2HeaderSize: const size_t;
var kWoff2EntrySize: const size_t;
fn 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;
fn Compress(data: const uint8_t *, len: const size_t, result: uint8_t *,
result_len: uint32_t *, mode: BrotliEncoderMode, quality: int) -> bool {
var compressed_len: size_t;
if (BrotliEncoderCompress(quality, BROTLI_DEFAULT_WINDOW, mode, len, data,
&compressed_len, result) == 0) {
return false;
@@ -49,30 +49,30 @@ fn Compress(const uint8_t* data, const size_t len, uint8_t* result,
return true;
}
fn Woff2Compress(const uint8_t* data, const size_t len,
uint8_t* result, uint32_t* result_len,
int quality) -> bool {
fn Woff2Compress(data: const uint8_t *, len: const size_t,
result: uint8_t *, result_len: uint32_t *,
quality: int) -> bool {
return Compress(data, len, result, result_len,
BROTLI_MODE_FONT, quality);
}
fn TextCompress(const uint8_t* data, const size_t len,
uint8_t* result, uint32_t* result_len,
int quality) -> bool {
fn TextCompress(data: const uint8_t *, len: const size_t,
result: uint8_t *, result_len: uint32_t *,
quality: int) -> bool {
return Compress(data, len, result, result_len,
BROTLI_MODE_TEXT, quality);
}
fn KnownTableIndex(uint32_t tag) -> int {
for (int i = 0; i < 63; ++i) {
fn KnownTableIndex(tag: uint32_t) -> int {
for (var i: int; i < 63; ++i) {
if (tag == kKnownTags[i]) { return i;
}
}
return 63;
}
void StoreTableEntry(const Table& table, size_t* offset, uint8_t* dst) {
uint8_t flag_byte = (table.flags & 0xC0) | KnownTableIndex(table.tag);
void StoreTableEntry(table: const woff2::Table &, offset: size_t *, dst: uint8_t *) {
var flag_byte: uint8_t;
dst[(*offset)++] = flag_byte;
// The index here is treated as a set of flag bytes because
// bits 6 and 7 of the byte are reserved for future use as flags.
@@ -86,9 +86,9 @@ void StoreTableEntry(const Table& table, size_t* offset, uint8_t* dst) {
}
}
fn TableEntrySize(const Table& table) -> size_t {
uint8_t flag_byte = KnownTableIndex(table.tag);
size_t size = ((flag_byte & 0x3f) != 0x3f) ? 1 : 5;
fn TableEntrySize(table: const woff2::Table &) -> size_t {
var flag_byte: uint8_t;
var size: size_t;
size += Base128Size(table.src_length);
if ((table.flags & kWoff2FlagsTransform) != 0) {
size += Base128Size(table.transform_length);
@@ -96,15 +96,14 @@ fn TableEntrySize(const Table& table) -> size_t {
return size;
}
fn 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 {
size_t size = kWoff2HeaderSize;
fn ComputeWoff2Length(font_collection: const woff2::FontCollection &,
tables: const std::vector<Table> &,
index_by_tag_offset: std::map<std::pair<uint32_t, uint32_t>, uint16_t>,
compressed_data_length: size_t,
extended_metadata_length: size_t) -> size_t {
var size: size_t;
for (const auto& table : tables) {
for (const auto& table var __begin2: std::__wrap_iter<const woff2::Table *> var __range2: const std::vector<woff2::Table> &) {
size += TableEntrySize(table);
}
@@ -115,16 +114,16 @@ fn ComputeWoff2Length(const FontCollection& font_collection,
size += 4 * font_collection.fonts.size(); // UInt32 flavor for each
for (const auto& font : font_collection.fonts) {
for (const auto& font var __begin3: std::__wrap_iter<const woff2::Font *> var __range3: const std::vector<woff2::Font> &) {
size += Size255UShort(font.tables.size()); // 255UInt16 numTables
for (const auto& entry : font.tables) {
const Font::Table& table = entry.second;
for (const auto& entry var __begin4: std::__map_const_iterator<std::__tree_const_iterator<std::__value_type<unsigned int, woff2::Font::Table>, std::__tree_node<std::__value_type<unsigned int, woff2::Font::Table>, void *> *, long>> var __range4: const std::map<unsigned int, woff2::Font::Table> &) {
var table: const Font::Table &;
// no collection entry for xform table
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];
var tag_offset: std::pair<uint32_t, uint32_t>;
var table_index: uint16_t;
size += Size255UShort(table_index); // 255UInt16 index entry
}
}
@@ -138,11 +137,11 @@ fn ComputeWoff2Length(const FontCollection& font_collection,
return size;
}
fn ComputeUncompressedLength(const Font& font) -> size_t {
fn ComputeUncompressedLength(font: const woff2::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;
var size: size_t;
for (const auto& entry var __begin2: std::__map_const_iterator<std::__tree_const_iterator<std::__value_type<unsigned int, woff2::Font::Table>, std::__tree_node<std::__value_type<unsigned int, woff2::Font::Table>, void *> *, long>> var __range2: const std::map<unsigned int, woff2::Font::Table> &) {
var table: const Font::Table &;
if (table.tag & 0x80808080) { continue; // xform tables don't stay
}
if (table.IsReused()) { continue; // don't have to pay twice
@@ -152,22 +151,21 @@ fn ComputeUncompressedLength(const Font& font) -> size_t {
return size;
}
fn ComputeUncompressedLength(const FontCollection& font_collection) -> size_t {
fn ComputeUncompressedLength(font_collection: const woff2::FontCollection &) -> size_t {
if (font_collection.flavor != kTtcFontFlavor) {
return ComputeUncompressedLength(font_collection.fonts[0]);
}
size_t size = CollectionHeaderSize(font_collection.header_version,
font_collection.fonts.size());
for (const auto& font : font_collection.fonts) {
var size: size_t;
for (const auto& font var __begin2: std::__wrap_iter<const woff2::Font *> var __range2: const std::vector<woff2::Font> &) {
size += ComputeUncompressedLength(font);
}
return size;
}
fn ComputeTotalTransformLength(const Font& font) -> size_t {
size_t total = 0;
for (const auto& i : font.tables) {
const Font::Table& table = i.second;
fn ComputeTotalTransformLength(font: const woff2::Font &) -> size_t {
var total: size_t;
for (const auto& i var __begin2: std::__map_const_iterator<std::__tree_const_iterator<std::__value_type<unsigned int, woff2::Font::Table>, std::__tree_node<std::__value_type<unsigned int, woff2::Font::Table>, void *> *, long>> var __range2: const std::map<unsigned int, woff2::Font::Table> &) {
var table: const Font::Table &;
if (table.IsReused()) {
continue;
}
@@ -182,12 +180,12 @@ fn ComputeTotalTransformLength(const Font& font) -> size_t {
} // namespace
fn MaxWOFF2CompressedSize(const uint8_t* data, size_t length) -> size_t {
fn MaxWOFF2CompressedSize(data: const uint8_t *, length: size_t) -> size_t {
return MaxWOFF2CompressedSize(data, length, "");
}
fn MaxWOFF2CompressedSize(const uint8_t* /*data*/, size_t length,
const string& extended_metadata) -> size_t {
fn MaxWOFF2CompressedSize(const uint8_t* /*data*/: const uint8_t * /*data*/, length: size_t,
extended_metadata: const std::string &) -> 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
@@ -195,12 +193,12 @@ fn MaxWOFF2CompressedSize(const uint8_t* /*data*/, size_t length,
return length + 1024 + extended_metadata.length();
}
fn CompressedBufferSize(uint32_t original_size) -> uint32_t {
fn CompressedBufferSize(original_size: uint32_t) -> uint32_t {
return 1.2 * original_size + 10240;
}
fn TransformFontCollection(FontCollection* font_collection) -> bool {
for (auto& font : font_collection->fonts) {
fn TransformFontCollection(font_collection: woff2::FontCollection *) -> bool {
for (auto& font var __begin1: std::__wrap_iter<woff2::Font *> var __range1: std::vector<woff2::Font> &) {
if (!TransformGlyfAndLocaTables(&font)) {
#ifdef FONT_COMPRESSION_BIN
fprintf(stderr, "glyf/loca transformation failed.\n");
@@ -212,17 +210,17 @@ fn TransformFontCollection(FontCollection* font_collection) -> bool {
return true;
}
fn ConvertTTFToWOFF2(const uint8_t *data, size_t length,
uint8_t *result, size_t *result_length) -> bool {
WOFF2Params params;
fn ConvertTTFToWOFF2(data: const uint8_t *, length: size_t,
result: uint8_t *, result_length: size_t *) -> bool {
var params: woff2::WOFF2Params;
return ConvertTTFToWOFF2(data, length, result, result_length,
params);
}
fn ConvertTTFToWOFF2(const uint8_t *data, size_t length,
uint8_t *result, size_t *result_length,
const WOFF2Params& params) -> bool {
FontCollection font_collection;
fn ConvertTTFToWOFF2(data: const uint8_t *, length: size_t,
result: uint8_t *, result_length: size_t *,
params: const woff2::WOFF2Params &) -> bool {
var font_collection: woff2::FontCollection;
if (!ReadFontCollection(data, length, &font_collection)) {
#ifdef FONT_COMPRESSION_BIN
fprintf(stderr, "Parsing of the input font failed.\n");
@@ -238,9 +236,9 @@ fn ConvertTTFToWOFF2(const uint8_t *data, size_t length,
return FONT_COMPRESSION_FAILURE();
} else {
// glyf/loca use 11 to flag "not transformed"
for (auto& font : font_collection.fonts) {
Font::Table* glyf_table = font.FindTable(kGlyfTableTag);
Font::Table* loca_table = font.FindTable(kLocaTableTag);
for (auto& font var __begin2: std::__wrap_iter<woff2::Font *> var __range2: std::vector<woff2::Font> &) {
var glyf_table: Font::Table *;
var loca_table: Font::Table *;
if (glyf_table) {
glyf_table->flag_byte |= 0xc0;
}
@@ -256,25 +254,25 @@ fn ConvertTTFToWOFF2(const uint8_t *data, size_t length,
// the size. If the compressor overflows this, it should return false and
// then this function will also return false.
size_t total_transform_length = 0;
for (const auto& font : font_collection.fonts) {
var total_transform_length: size_t;
for (const auto& font var __begin1: std::__wrap_iter<woff2::Font *> var __range1: std::vector<woff2::Font> &) {
total_transform_length += ComputeTotalTransformLength(font);
}
size_t compression_buffer_size = CompressedBufferSize(total_transform_length);
std::vector<uint8_t> compression_buf(compression_buffer_size);
uint32_t total_compressed_length = compression_buffer_size;
var compression_buffer_size: size_t;
var compression_buf: std::vector<uint8_t>;
var total_compressed_length: uint32_t;
// Collect all transformed data into one place in output order.
std::vector<uint8_t> transform_buf(total_transform_length);
size_t transform_offset = 0;
for (const auto& font : font_collection.fonts) {
for (const auto tag : font.OutputOrderedTags()) {
const Font::Table& original = font.tables.at(tag);
var transform_buf: std::vector<uint8_t>;
var transform_offset: size_t;
for (const auto& font var __begin1: std::__wrap_iter<woff2::Font *> var __range1: std::vector<woff2::Font> &) {
for (const auto tag var __begin2: std::__wrap_iter<unsigned int *> var __range2: std::vector<unsigned int> &&) {
var original: const Font::Table &;
if (original.IsReused()) { continue;
}
if (tag & 0x80808080) { continue;
}
const Font::Table* table_to_store = font.FindTable(tag ^ 0x80808080);
var table_to_store: const Font::Table *;
if (table_to_store == nullptr) { table_to_store = &original;
}
@@ -301,9 +299,8 @@ fn ConvertTTFToWOFF2(const uint8_t *data, size_t length,
// Compress the extended metadata
// TODO(user): how does this apply to collections
uint32_t compressed_metadata_buf_length =
CompressedBufferSize(params.extended_metadata.length());
std::vector<uint8_t> compressed_metadata_buf(compressed_metadata_buf_length);
var compressed_metadata_buf_length: uint32_t;
var compressed_metadata_buf: std::vector<uint8_t>;
if (params.extended_metadata.length() > 0) {
if (!TextCompress(reinterpret_cast<const uint8_t*>(params.extended_metadata.data()),
@@ -320,32 +317,31 @@ fn ConvertTTFToWOFF2(const uint8_t *data, size_t length,
compressed_metadata_buf_length = 0;
}
std::vector<Table> tables;
std::map<std::pair<uint32_t, uint32_t>, uint16_t> index_by_tag_offset;
var tables: std::vector<Table>;
var index_by_tag_offset: std::map<std::pair<uint32_t, uint32_t>, uint16_t>;
for (const auto& font : font_collection.fonts) {
for (const auto& font var __begin1: std::__wrap_iter<woff2::Font *> var __range1: std::vector<woff2::Font> &) {
for (const auto tag : font.OutputOrderedTags()) {
const Font::Table& src_table = font.tables.at(tag);
for (const auto tag var __begin2: std::__wrap_iter<unsigned int *> var __range2: std::vector<unsigned int> &&) {
var src_table: const Font::Table &;
if (src_table.IsReused()) {
continue;
}
std::pair<uint32_t, uint32_t> tag_offset(src_table.tag, src_table.offset);
var tag_offset: std::pair<uint32_t, uint32_t>;
if (index_by_tag_offset.find(tag_offset) == index_by_tag_offset.end()) {
index_by_tag_offset[tag_offset] = tables.size();
} else {
return false;
}
Table table;
var table: woff2::Table;
table.tag = src_table.tag;
table.flags = src_table.flag_byte;
table.src_length = src_table.length;
table.transform_length = src_table.length;
const uint8_t* transformed_data = src_table.data;
const Font::Table* transformed_table =
font.FindTable(src_table.tag ^ 0x80808080);
var transformed_data: const uint8_t *;
var transformed_table: const Font::Table *;
if (transformed_table != nullptr) {
table.flags = transformed_table->flag_byte;
table.flags |= kWoff2FlagsTransform;
@@ -357,9 +353,7 @@ fn ConvertTTFToWOFF2(const uint8_t *data, size_t length,
}
}
size_t woff2_length = ComputeWoff2Length(font_collection, tables,
index_by_tag_offset, total_compressed_length,
compressed_metadata_buf_length);
var woff2_length: size_t;
if (woff2_length > *result_length) {
#ifdef FONT_COMPRESSION_BIN
fprintf(stderr, "Result allocation was too small (%zd vs %zd bytes).\n",
@@ -369,7 +363,7 @@ fn ConvertTTFToWOFF2(const uint8_t *data, size_t length,
}
*result_length = woff2_length;
size_t offset = 0;
var offset: size_t;
// start of woff2 header (http://www.w3.org/TR/WOFF2/#woff20Header)
StoreU32(kWoff2Signature, &offset, result);
@@ -404,7 +398,7 @@ fn ConvertTTFToWOFF2(const uint8_t *data, size_t length,
// end of woff2 header
// table directory (http://www.w3.org/TR/WOFF2/#table_dir_format)
for (const auto& table : tables) {
for (const auto& table var __begin1: std::__wrap_iter<woff2::Table *> var __range1: std::vector<woff2::Table> &) {
StoreTableEntry(table, &offset, result);
}
@@ -412,11 +406,11 @@ fn ConvertTTFToWOFF2(const uint8_t *data, size_t length,
if (font_collection.flavor == kTtcFontFlavor) {
StoreU32(font_collection.header_version, &offset, result);
Store255UShort(font_collection.fonts.size(), &offset, result);
for (const Font& font : font_collection.fonts) {
for (const Font& font var __begin2: std::__wrap_iter<woff2::Font *> var __range2: std::vector<woff2::Font> &) {
uint16_t num_tables = 0;
for (const auto& entry : font.tables) {
const Font::Table& table = entry.second;
var num_tables: uint16_t;
for (const auto& entry var __begin3: std::__map_const_iterator<std::__tree_const_iterator<std::__value_type<unsigned int, woff2::Font::Table>, std::__tree_node<std::__value_type<unsigned int, woff2::Font::Table>, void *> *, long>> var __range3: const std::map<unsigned int, woff2::Font::Table> &) {
var table: const Font::Table &;
if (table.tag & 0x80808080) { continue; // don't write xform tables
}
num_tables++;
@@ -424,17 +418,15 @@ fn ConvertTTFToWOFF2(const uint8_t *data, size_t length,
Store255UShort(num_tables, &offset, result);
StoreU32(font.flavor, &offset, result);
for (const auto& entry : font.tables) {
const Font::Table& table = entry.second;
for (const auto& entry var __begin3: std::__map_const_iterator<std::__tree_const_iterator<std::__value_type<unsigned int, woff2::Font::Table>, std::__tree_node<std::__value_type<unsigned int, woff2::Font::Table>, void *> *, long>> var __range3: const std::map<unsigned int, woff2::Font::Table> &) {
var table: const Font::Table &;
if (table.tag & 0x80808080) { continue; // don't write xform tables
}
// for reused tables, only the original has an updated offset
uint32_t table_offset =
table.IsReused() ? table.reuse_of->offset : table.offset;
uint32_t table_length =
table.IsReused() ? table.reuse_of->length : table.length;
std::pair<uint32_t, uint32_t> tag_offset(table.tag, table_offset);
var table_offset: uint32_t;
var table_length: uint32_t;
var tag_offset: std::pair<uint32_t, uint32_t>;
if (index_by_tag_offset.find(tag_offset) == index_by_tag_offset.end()) {
#ifdef FONT_COMPRESSION_BIN
fprintf(stderr, "Missing table index for offset 0x%08x\n",
@@ -442,7 +434,7 @@ fprintf(stderr, "Missing table index for offset 0x%08x\n",
#endif
return FONT_COMPRESSION_FAILURE();
}
uint16_t index = index_by_tag_offset[tag_offset];
var index: uint16_t;
Store255UShort(index, &offset, result);
}
+21 -27
View File
@@ -15,20 +15,15 @@
#include "./table_tags.h"
#include "./variable_length.h"
fn PrintTag(int tag) -> std::string {
fn PrintTag(tag: int) -> std::string {
if (tag & 0x80808080) {
return std::string("_xfm"); // print _xfm for xform tables (else garbage)
}
char printable[] = {
static_cast<char>((tag >> 24) & 0xFF),
static_cast<char>((tag >> 16) & 0xFF),
static_cast<char>((tag >> 8) & 0xFF),
static_cast<char>(tag & 0xFF)
};
var printable: char [4];
return std::string(printable, 4);
}
fn main(int argc, char **argv) -> int {
fn main(argc: int, argv: char **) -> int {
using std::string;
if (argc != 2) {
@@ -36,19 +31,18 @@ fn main(int argc, char **argv) -> int {
return 1;
}
string filename(argv[1]);
string outfilename = filename.substr(0, filename.find_last_of('.')) + ".woff2";
var filename: std::string;
var outfilename: std::string;
fprintf(stdout, "Processing %s => %s\n",
filename.c_str(), outfilename.c_str());
string input = woff2::GetFileContent(filename);
var input: std::string;
woff2::Buffer file(reinterpret_cast<const uint8_t*>(input.data()),
input.size());
var file: woff2::Buffer;
printf("WOFF2Header\n");
uint32_t signature, flavor, length, totalSfntSize, totalCompressedSize;
uint32_t metaOffset, metaLength, metaOrigLength, privOffset, privLength;
uint16_t num_tables, reserved, major, minor;
var signature: uint32_t, var flavor: uint32_t, var length: uint32_t, var totalSfntSize: uint32_t, var totalCompressedSize: uint32_t;
var metaOffset: uint32_t, var metaLength: uint32_t, var metaOrigLength: uint32_t, var privOffset: uint32_t, var privLength: uint32_t;
var num_tables: uint16_t, var reserved: uint16_t, var major: uint16_t, var minor: uint16_t;
if (!file.ReadU32(&signature)) { return 1;
}
if (!file.ReadU32(&flavor)) { return 1;
@@ -97,13 +91,13 @@ fn main(int argc, char **argv) -> int {
printf("privOffset %d\n", privOffset);
printf("privLength %d\n", privLength);
std::vector<uint32_t> table_tags;
var table_tags: std::vector<uint32_t>;
printf("TableDirectory starts at +%zu\n", file.offset());
printf("Entry offset flags tag origLength txLength\n");
for (auto i = 0; i < num_tables; i++) {
size_t offset = file.offset();
uint8_t flags;
uint32_t tag, origLength, transformLength;
for (var i: int; i < num_tables; i++) {
var offset: size_t;
var flags: uint8_t;
var tag: uint32_t, var origLength: uint32_t, var transformLength: uint32_t;
if (!file.ReadU8(&flags)) { return 1;
}
if ((flags & 0x3f) == 0x3f) {
@@ -119,7 +113,7 @@ fn main(int argc, char **argv) -> int {
printf("%5d %6zu 0x%02x %s %10d", i, offset, flags,
PrintTag(tag).c_str(), origLength);
uint8_t xform_version = (flags >> 6) & 0x3;
var xform_version: uint8_t;
if (tag == woff2::kGlyfTableTag || tag == woff2::kLocaTableTag) {
if (xform_version == 0) {
if (!ReadBase128(&file, &transformLength)) { return 1;
@@ -136,23 +130,23 @@ fn main(int argc, char **argv) -> int {
// Collection header
if (flavor == woff2::kTtcFontFlavor) {
uint32_t version, numFonts;
var version: uint32_t, var numFonts: uint32_t;
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;
for (var i: int; i < numFonts; i++) {
var numTables: uint32_t, var flavor: uint32_t;
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;
for (var j: int; j < numTables; j++) {
var table_idx: uint32_t;
if (!woff2::Read255UShort(&file, &table_idx)) { return 1;
}
if (table_idx >= table_tags.size()) { return 1;
@@ -12,16 +12,16 @@ using std::string;
namespace woff2 {
WOFF2StringOut::WOFF2StringOut(string* buf)
WOFF2StringOut::WOFF2StringOut(buf: std::string *)
: buf_(buf),
max_size_(kDefaultMaxSize),
offset_(0) {}
fn WOFF2StringOut::Write(const void *buf, size_t n) -> bool {
fn WOFF2StringOut::Write(buf: const void *, n: size_t) -> bool {
return Write(buf, offset_, n);
}
fn WOFF2StringOut::Write(const void *buf, size_t offset, size_t n) -> bool {
fn WOFF2StringOut::Write(buf: const void *, offset: size_t, n: size_t) -> bool {
if (offset > max_size_ || n > max_size_ - offset) {
return false;
}
@@ -38,23 +38,23 @@ fn WOFF2StringOut::Write(const void *buf, size_t offset, size_t n) -> bool {
return true;
}
void WOFF2StringOut::SetMaxSize(size_t max_size) {
void WOFF2StringOut::SetMaxSize(max_size: size_t) {
max_size_ = max_size;
if (offset_ > max_size_) {
offset_ = max_size_;
}
}
WOFF2MemoryOut::WOFF2MemoryOut(uint8_t* buf, size_t buf_size)
WOFF2MemoryOut::WOFF2MemoryOut(buf: uint8_t *, buf_size: size_t)
: buf_(buf),
buf_size_(buf_size),
offset_(0) {}
fn WOFF2MemoryOut::Write(const void *buf, size_t n) -> bool {
fn WOFF2MemoryOut::Write(buf: const void *, n: size_t) -> bool {
return Write(buf, offset_, n);
}
fn WOFF2MemoryOut::Write(const void *buf, size_t offset, size_t n) -> bool {
fn WOFF2MemoryOut::Write(buf: const void *, offset: size_t, n: size_t) -> bool {
if (offset > buf_size_ || n > buf_size_ - offset) {
return false;
}