Use AST to start inserting fn (#530)

This commit is contained in:
Jon Meow
2021-05-13 14:58:32 -07:00
committed by GitHub
parent 2942c97424
commit 6f544494b3
36 changed files with 297 additions and 162 deletions
+4
View File
@@ -47,6 +47,10 @@ bazel-execroot/external/llvm-project
-iquote
bazel-bin/external/llvm-project
-iquote
bazel-execroot/external/llvm-project/clang/include
-iquote
bazel-bin/external/llvm-project/clang/include
-iquote
bazel-execroot/external/llvm_terminfo
-iquote
bazel-bin/external/llvm_terminfo
+1
View File
@@ -9,6 +9,7 @@ py_binary(
srcs = ["migrate_cpp.py"],
data = [
":clang_tidy.yaml",
"//migrate_cpp/cpp_refactoring",
"@bootstrap_clang_toolchain//:bin/clang-tidy",
],
python_version = "PY3",
+16
View File
@@ -0,0 +1,16 @@
# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
# Exceptions. See /LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
load("@rules_cc//cc:defs.bzl", "cc_binary")
package(default_visibility = ["//visibility:public"])
cc_binary(
name = "cpp_refactoring",
srcs = ["main.cpp"],
deps = [
"@llvm-project//clang:ast_matchers",
"@llvm-project//clang:tooling",
],
)
+96
View File
@@ -0,0 +1,96 @@
// 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 "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Refactoring.h"
namespace cam = ::clang::ast_matchers;
namespace ct = ::clang::tooling;
namespace carbon {
class Matcher : public cam::MatchFinder::MatchCallback {
public:
explicit Matcher(std::map<std::string, ct::Replacements>* in_replacements)
: replacements(in_replacements) {}
virtual ~Matcher() {}
void AddReplacement(const clang::SourceManager& sm,
clang::CharSourceRange range,
llvm::StringRef replacement_text) {
if (!range.isValid()) {
llvm::errs() << "Invalid range: " << range.getAsRange().printToString(sm)
<< "\n";
return;
}
if (sm.getDecomposedLoc(range.getBegin()).first !=
sm.getDecomposedLoc(range.getEnd()).first) {
llvm::errs() << "Range spans macro expansions: "
<< range.getAsRange().printToString(sm) << "\n";
return;
}
if (sm.getFileID(range.getBegin()) != sm.getFileID(range.getEnd())) {
llvm::errs() << "Range spans files: "
<< range.getAsRange().printToString(sm) << "\n";
return;
}
auto rep =
ct::Replacement(sm, sm.getExpansionRange(range), replacement_text);
auto err = (*replacements)[std::string(rep.getFilePath())].add(rep);
if (err) {
llvm::report_fatal_error("Error with replacement `" + rep.toString() +
"`: " + llvm::toString(std::move(err)) + "\n");
}
}
private:
std::map<std::string, ct::Replacements>* replacements;
};
class FnInserter : public Matcher {
public:
explicit FnInserter(std::map<std::string, ct::Replacements>* in_replacements,
cam::MatchFinder* finder)
: Matcher(in_replacements) {
finder->addMatcher(cam::functionDecl(cam::isExpansionInMainFile(),
cam::hasTrailingReturn())
.bind(Label),
this);
}
void run(const cam::MatchFinder::MatchResult& result) override {
// The matched 'if' statement was bound to 'ifStmt'.
const auto* decl = result.Nodes.getNodeAs<clang::FunctionDecl>(Label);
if (!decl) {
llvm::report_fatal_error(std::string("getNodeAs failed for ") + Label);
}
auto begin = decl->getBeginLoc();
// Replace the first token in the range, `auto`.
auto range = clang::CharSourceRange::getTokenRange(begin, begin);
AddReplacement(*(result.SourceManager), range, "fn");
}
private:
static constexpr char Label[] = "FnInserter";
};
} // namespace carbon
auto main(int argc, const char** argv) -> int {
llvm::cl::OptionCategory category("C++ refactoring options");
clang::tooling::CommonOptionsParser op(argc, argv, category);
clang::tooling::RefactoringTool tool(op.getCompilations(),
op.getSourcePathList());
// Set up AST matcher callbacks.
cam::MatchFinder finder;
carbon::FnInserter fn_inserter(&tool.getReplacements(), &finder);
return tool.runAndSave(
clang::tooling::newFrontendActionFactory(&finder).get());
}
+9
View File
@@ -13,6 +13,7 @@ import subprocess
import sys
_CLANG_TIDY = "../external/bootstrap_clang_toolchain/bin/clang-tidy"
_CPP_REFACTORING = "./cpp_refactoring/cpp_refactoring"
_CPP_EXTS = {".h", ".c", ".cc", ".cpp", ".cxx"}
@@ -55,6 +56,13 @@ def _clang_tidy(parsed_args, cpp_files):
subprocess.run([clang_tidy, "--fix", "--config", config] + cpp_files)
def _cpp_refactoring(parsed_args, cpp_files):
"""Runs cpp_refactoring to migrate C++ files towards Carbon syntax."""
print("Running cpp_refactoring...")
cpp_refactoring = _data_file(_CPP_REFACTORING)
subprocess.run([cpp_refactoring] + cpp_files)
def _main():
"""Main program execution."""
parsed_args = _parse_args()
@@ -65,6 +73,7 @@ def _main():
cpp_files = _gather_files(parsed_args)
_clang_tidy(parsed_args, cpp_files)
_cpp_refactoring(parsed_args, cpp_files)
print("Done!")
+3
View File
@@ -51,3 +51,6 @@
-D__DATE__="redacted"
-D__TIMESTAMP__="redacted"
-D__TIME__="redacted"
-Wno-unused-variable
-Wno-unused-const-variable
-Wno-sign-compare
+3 -3
View File
@@ -16,19 +16,19 @@
namespace woff2 {
// Compute the size of the final uncompressed font, or 0 on error.
auto ComputeWOFF2FinalSize(const uint8_t *data, size_t length) -> size_t;
fn ComputeWOFF2FinalSize(const uint8_t *data, size_t length) -> size_t;
// Decompresses the font into the target buffer. The result_length should
// be the same as determined by ComputeFinalSize(). Returns true on successful
// decompression.
// DEPRECATED; please prefer the version that takes a WOFF2Out*
auto ConvertWOFF2ToTTF(uint8_t *result, size_t result_length,
fn ConvertWOFF2ToTTF(uint8_t *result, size_t result_length,
const uint8_t *data, size_t length) -> bool;
// Decompresses the font into out. Returns true on success.
// Works even if WOFF2Header totalSfntSize is wrong.
// Please prefer this API.
auto ConvertWOFF2ToTTF(const uint8_t *data, size_t length,
fn ConvertWOFF2ToTTF(const uint8_t *data, size_t length,
WOFF2Out* out) -> bool;
} // namespace woff2
+4 -4
View File
@@ -25,16 +25,16 @@ struct WOFF2Params {
};
// Returns an upper bound on the size of the compressed file.
auto MaxWOFF2CompressedSize(const uint8_t* data, size_t length) -> size_t;
auto MaxWOFF2CompressedSize(const uint8_t* data, size_t length,
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;
// 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.
auto ConvertTTFToWOFF2(const uint8_t *data, size_t length,
fn ConvertTTFToWOFF2(const uint8_t *data, size_t length,
uint8_t *result, size_t *result_length) -> bool;
auto ConvertTTFToWOFF2(const uint8_t *data, size_t length,
fn ConvertTTFToWOFF2(const uint8_t *data, size_t length,
uint8_t *result, size_t *result_length,
const WOFF2Params& params) -> bool;
+10 -10
View File
@@ -34,13 +34,13 @@ class WOFF2Out {
// Append n bytes of data from buf.
// Return true if all written, false otherwise.
virtual auto Write(const void *buf, size_t n) -> bool = 0;
fn auto Write(const void *buf, size_t n) -> bool = 0;
// Write n bytes of data from buf at offset.
// Return true if all written, false otherwise.
virtual auto Write(const void *buf, size_t offset, size_t n) -> bool = 0;
fn auto Write(const void *buf, size_t offset, size_t n) -> bool = 0;
virtual auto Size() -> size_t = 0;
fn auto Size() -> size_t = 0;
};
/**
@@ -53,10 +53,10 @@ class WOFF2StringOut : public WOFF2Out {
// buf may be sized (e.g. using EstimateWOFF2FinalSize) or empty.
explicit WOFF2StringOut(std::string* buf);
auto Write(const void *buf, size_t n) -> bool override;
auto Write(const void *buf, size_t offset, size_t n) -> bool override;
auto Size() -> size_t override { return offset_; }
auto MaxSize() -> size_t { return max_size_; }
fn Write(const void *buf, size_t n) -> bool override;
fn Write(const void *buf, size_t offset, size_t n) -> bool override;
fn Size() -> size_t override { return offset_; }
fn MaxSize() -> size_t { return max_size_; }
void SetMaxSize(size_t max_size);
private:
std::string* buf_;
@@ -72,9 +72,9 @@ class WOFF2MemoryOut : public WOFF2Out {
// Create a writer that writes its data to buf.
WOFF2MemoryOut(uint8_t* buf, size_t buf_size);
auto Write(const void *buf, size_t n) -> bool override;
auto Write(const void *buf, size_t offset, size_t n) -> bool override;
auto Size() -> size_t override { return offset_; }
fn Write(const void *buf, size_t n) -> bool override;
fn Write(const void *buf, size_t offset, size_t n) -> bool override;
fn Size() -> size_t override { return offset_; }
private:
uint8_t* buf_;
size_t buf_size_;
+13 -13
View File
@@ -62,11 +62,11 @@ class Buffer {
length_(len),
offset_(0) { }
auto Skip(size_t n_bytes) -> bool {
fn Skip(size_t n_bytes) -> bool {
return Read(nullptr, n_bytes);
}
auto Read(uint8_t *data, size_t n_bytes) -> bool {
fn Read(uint8_t *data, size_t n_bytes) -> bool {
if (n_bytes > 1024 * 1024 * 1024) {
return FONT_COMPRESSION_FAILURE();
}
@@ -81,7 +81,7 @@ class Buffer {
return true;
}
inline auto ReadU8(uint8_t *value) -> bool {
fn auto ReadU8(uint8_t *value) -> bool {
if (offset_ + 1 > length_) {
return FONT_COMPRESSION_FAILURE();
}
@@ -90,7 +90,7 @@ class Buffer {
return true;
}
auto ReadU16(uint16_t *value) -> bool {
fn ReadU16(uint16_t *value) -> bool {
if (offset_ + 2 > length_) {
return FONT_COMPRESSION_FAILURE();
}
@@ -100,11 +100,11 @@ class Buffer {
return true;
}
auto ReadS16(int16_t *value) -> bool {
fn ReadS16(int16_t *value) -> bool {
return ReadU16(reinterpret_cast<uint16_t*>(value));
}
auto ReadU24(uint32_t *value) -> bool {
fn ReadU24(uint32_t *value) -> bool {
if (offset_ + 3 > length_) {
return FONT_COMPRESSION_FAILURE();
}
@@ -115,7 +115,7 @@ class Buffer {
return true;
}
auto ReadU32(uint32_t *value) -> bool {
fn ReadU32(uint32_t *value) -> bool {
if (offset_ + 4 > length_) {
return FONT_COMPRESSION_FAILURE();
}
@@ -125,11 +125,11 @@ class Buffer {
return true;
}
auto ReadS32(int32_t *value) -> bool {
fn ReadS32(int32_t *value) -> bool {
return ReadU32(reinterpret_cast<uint32_t*>(value));
}
auto ReadTag(uint32_t *value) -> bool {
fn ReadTag(uint32_t *value) -> bool {
if (offset_ + 4 > length_) {
return FONT_COMPRESSION_FAILURE();
}
@@ -138,7 +138,7 @@ class Buffer {
return true;
}
auto ReadR64(uint64_t *value) -> bool {
fn ReadR64(uint64_t *value) -> bool {
if (offset_ + 8 > length_) {
return FONT_COMPRESSION_FAILURE();
}
@@ -147,9 +147,9 @@ class Buffer {
return true;
}
[[nodiscard]] auto buffer() const -> const uint8_t * { return buffer_; }
[[nodiscard]] auto offset() const -> size_t { return offset_; }
[[nodiscard]] auto length() const -> size_t { return length_; }
[[nodiscard]] fn buffer() const -> const uint8_t * { return 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; }
@@ -4,7 +4,7 @@
#include <woff2/decode.h>
// Entry point for LibFuzzer.
extern "C" auto LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) -> int {
extern "C" fn LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) -> int {
std::string buf;
woff2::WOFF2StringOut out(&buf);
out.SetMaxSize(30 * 1024 * 1024);
@@ -1,7 +1,7 @@
#include <string>
#include <woff2/decode.h>
extern "C" auto LLVMFuzzerTestOneInput(const uint8_t *data, size_t data_size) -> int {
extern "C" fn LLVMFuzzerTestOneInput(const uint8_t *data, size_t data_size) -> int {
// Decode using newer entry pattern.
// Same pattern as woff2_decompress.
std::string output(std::min(woff2::ComputeWOFF2FinalSize(data, data_size),
+1 -1
View File
@@ -17,7 +17,7 @@ namespace woff2 {
using std::string;
inline auto GetFileContent(const string& filename) -> string {
fn auto GetFileContent(const string& filename) -> string {
std::ifstream ifs(filename.c_str(), std::ios::binary);
return string(
std::istreambuf_iterator<char>(ifs.rdbuf()),
+20 -20
View File
@@ -18,17 +18,17 @@
namespace woff2 {
auto Font::FindTable(uint32_t tag) -> Font::Table* {
fn Font::FindTable(uint32_t tag) -> Font::Table* {
auto it = tables.find(tag);
return it == tables.end() ? nullptr : &it->second;
}
auto Font::FindTable(uint32_t tag) const -> const Font::Table* {
fn Font::FindTable(uint32_t tag) const -> const Font::Table* {
auto it = tables.find(tag);
return it == tables.end() ? nullptr : &it->second;
}
auto Font::OutputOrderedTags() const -> std::vector<uint32_t> {
fn Font::OutputOrderedTags() const -> std::vector<uint32_t> {
std::vector<uint32_t> output_order;
for (const auto& i : tables) {
@@ -55,7 +55,7 @@ auto Font::OutputOrderedTags() const -> std::vector<uint32_t> {
return output_order;
}
auto ReadTrueTypeFont(Buffer* file, const uint8_t* data, size_t len,
fn ReadTrueTypeFont(Buffer* file, const uint8_t* data, size_t len,
Font* font) -> bool {
// We don't care about the search_range, entry_selector and range_shift
// fields, they will always be computed upon writing the font.
@@ -106,7 +106,7 @@ auto ReadTrueTypeFont(Buffer* file, const uint8_t* data, size_t len,
return true;
}
auto ReadCollectionFont(Buffer* file, const uint8_t* data, size_t len,
fn ReadCollectionFont(Buffer* file, const uint8_t* data, size_t len,
Font* font,
std::map<uint32_t, Font::Table*>* all_tables) -> bool {
if (!file->ReadU32(&font->flavor)) {
@@ -132,7 +132,7 @@ auto ReadCollectionFont(Buffer* file, const uint8_t* data, size_t len,
return true;
}
auto ReadTrueTypeCollection(Buffer* file, const uint8_t* data, size_t len,
fn ReadTrueTypeCollection(Buffer* file, const uint8_t* data, size_t len,
FontCollection* font_collection) -> bool {
uint32_t num_fonts;
@@ -165,7 +165,7 @@ auto ReadTrueTypeCollection(Buffer* file, const uint8_t* data, size_t len,
return true;
}
auto ReadFont(const uint8_t* data, size_t len, Font* font) -> bool {
fn ReadFont(const uint8_t* data, size_t len, Font* font) -> bool {
Buffer file(data, len);
if (!file.ReadU32(&font->flavor)) {
@@ -178,7 +178,7 @@ auto ReadFont(const uint8_t* data, size_t len, Font* font) -> bool {
return ReadTrueTypeFont(&file, data, len, font);
}
auto ReadFontCollection(const uint8_t* data, size_t len,
fn ReadFontCollection(const uint8_t* data, size_t len,
FontCollection* font_collection) -> bool {
Buffer file(data, len);
@@ -195,7 +195,7 @@ auto ReadFontCollection(const uint8_t* data, size_t len,
return ReadTrueTypeCollection(&file, data, len, font_collection);
}
auto FontFileSize(const Font& font) -> size_t {
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;
@@ -206,7 +206,7 @@ auto FontFileSize(const Font& font) -> size_t {
return max_offset;
}
auto FontCollectionFileSize(const FontCollection& font_collection) -> size_t {
fn FontCollectionFileSize(const FontCollection& font_collection) -> size_t {
size_t max_offset = 0;
for (auto& font : font_collection.fonts) {
// font file size actually just finds max offset
@@ -215,12 +215,12 @@ auto FontCollectionFileSize(const FontCollection& font_collection) -> size_t {
return max_offset;
}
auto WriteFont(const Font& font, uint8_t* dst, size_t dst_size) -> bool {
fn WriteFont(const Font& font, uint8_t* dst, size_t dst_size) -> bool {
size_t offset = 0;
return WriteFont(font, &offset, dst, dst_size);
}
auto WriteTableRecord(const Font::Table* table, size_t* offset, uint8_t* dst,
fn WriteTableRecord(const Font::Table* table, size_t* offset, uint8_t* dst,
size_t dst_size) -> bool {
if (dst_size < *offset + kSfntEntrySize) {
return FONT_COMPRESSION_FAILURE();
@@ -235,7 +235,7 @@ auto WriteTableRecord(const Font::Table* table, size_t* offset, uint8_t* dst,
return true;
}
auto WriteTable(const Font::Table& table, size_t* offset, uint8_t* dst,
fn WriteTable(const Font::Table& table, size_t* offset, uint8_t* dst,
size_t dst_size) -> bool {
if (!WriteTableRecord(&table, offset, dst, dst_size)) {
return false;
@@ -258,7 +258,7 @@ auto WriteTable(const Font::Table& table, size_t* offset, uint8_t* dst,
return true;
}
auto WriteFont(const Font& font, size_t* offset, uint8_t* dst,
fn WriteFont(const Font& font, size_t* offset, uint8_t* dst,
size_t dst_size) -> bool {
if (dst_size < 12ULL + 16ULL * font.num_tables) {
return FONT_COMPRESSION_FAILURE();
@@ -281,7 +281,7 @@ auto WriteFont(const Font& font, size_t* offset, uint8_t* dst,
return true;
}
auto WriteFontCollection(const FontCollection& font_collection, uint8_t* dst,
fn WriteFontCollection(const FontCollection& font_collection, uint8_t* dst,
size_t dst_size) -> bool {
size_t offset = 0;
@@ -318,7 +318,7 @@ auto WriteFontCollection(const FontCollection& font_collection, uint8_t* dst,
return true;
}
auto NumGlyphs(const Font& font) -> int {
fn NumGlyphs(const Font& font) -> int {
const Font::Table* head_table = font.FindTable(kHeadTableTag);
const Font::Table* loca_table = font.FindTable(kLocaTableTag);
if (head_table == nullptr || loca_table == nullptr || head_table->length < 52) {
@@ -332,7 +332,7 @@ auto NumGlyphs(const Font& font) -> int {
return (loca_table->length / loca_record_size) - 1;
}
auto IndexFormat(const Font& font) -> int {
fn IndexFormat(const Font& font) -> int {
const Font::Table* head_table = font.FindTable(kHeadTableTag);
if (head_table == nullptr) {
return 0;
@@ -340,11 +340,11 @@ auto IndexFormat(const Font& font) -> int {
return head_table->data[51];
}
auto Font::Table::IsReused() const -> bool {
fn Font::Table::IsReused() const -> bool {
return this->reuse_of != nullptr;
}
auto GetGlyphData(const Font& font, int glyph_index,
fn GetGlyphData(const Font& font, int glyph_index,
const uint8_t** glyph_data, size_t* glyph_size) -> bool {
if (glyph_index < 0) {
return FONT_COMPRESSION_FAILURE();
@@ -386,7 +386,7 @@ auto GetGlyphData(const Font& font, int glyph_index,
return true;
}
auto RemoveDigitalSignature(Font* font) -> bool {
fn RemoveDigitalSignature(Font* font) -> bool {
auto it =
font->tables.find(kDsigTableTag);
if (it != font->tables.end()) {
+15 -15
View File
@@ -42,13 +42,13 @@ struct Font {
uint8_t flag_byte;
// Is this table reused by a TTC
[[nodiscard]] auto IsReused() const -> bool;
[[nodiscard]] fn IsReused() const -> bool;
};
std::map<uint32_t, Table> tables;
[[nodiscard]] auto OutputOrderedTags() const -> std::vector<uint32_t>;
[[nodiscard]] fn OutputOrderedTags() const -> std::vector<uint32_t>;
auto FindTable(uint32_t tag) -> Table*;
[[nodiscard]] auto FindTable(uint32_t tag) const -> const Table*;
fn FindTable(uint32_t tag) -> Table*;
[[nodiscard]] fn FindTable(uint32_t tag) const -> const Table*;
};
// Accomodates both singular (OTF, TTF) and collection (TTC) fonts
@@ -63,42 +63,42 @@ struct FontCollection {
// Parses the font from the given data. Returns false on parsing failure or
// buffer overflow. The font is valid only so long the input data pointer is
// valid. Does NOT support collections.
auto ReadFont(const uint8_t* data, size_t len, Font* font) -> bool;
fn ReadFont(const uint8_t* data, size_t len, Font* font) -> bool;
// Parses the font from the given data. Returns false on parsing failure or
// buffer overflow. The font is valid only so long the input data pointer is
// valid. Supports collections.
auto ReadFontCollection(const uint8_t* data, size_t len, FontCollection* fonts) -> bool;
fn ReadFontCollection(const uint8_t* data, size_t len, FontCollection* fonts) -> bool;
// Returns the file size of the font.
auto FontFileSize(const Font& font) -> size_t;
auto FontCollectionFileSize(const FontCollection& font) -> size_t;
fn FontFileSize(const Font& font) -> size_t;
fn FontCollectionFileSize(const FontCollection& font) -> size_t;
// Writes the font into the specified dst buffer. The dst_size should be the
// same as returned by FontFileSize(). Returns false upon buffer overflow (which
// should not happen if dst_size was computed by FontFileSize()).
auto WriteFont(const Font& font, uint8_t* dst, size_t dst_size) -> bool;
fn WriteFont(const Font& font, uint8_t* dst, size_t dst_size) -> bool;
// Write the font at a specific offset
auto WriteFont(const Font& font, size_t* offset, uint8_t* dst, size_t dst_size) -> bool;
fn WriteFont(const Font& font, size_t* offset, uint8_t* dst, size_t dst_size) -> bool;
auto WriteFontCollection(const FontCollection& font_collection, uint8_t* dst,
fn WriteFontCollection(const FontCollection& font_collection, uint8_t* dst,
size_t dst_size) -> bool;
// Returns the number of glyphs in the font.
// NOTE: Currently this works only for TrueType-flavored fonts, will return
// zero for CFF-flavored fonts.
auto NumGlyphs(const Font& font) -> int;
fn NumGlyphs(const Font& font) -> int;
// Returns the index format of the font
auto IndexFormat(const Font& font) -> int;
fn IndexFormat(const Font& font) -> int;
// Sets *glyph_data and *glyph_size to point to the location of the glyph data
// with the given index. Returns false if the glyph is not found.
auto GetGlyphData(const Font& font, int glyph_index,
fn GetGlyphData(const Font& font, int glyph_index,
const uint8_t** glyph_data, size_t* glyph_size) -> bool;
// Removes the digital signature (DSIG) table
auto RemoveDigitalSignature(Font* font) -> bool;
fn RemoveDigitalSignature(Font* font) -> bool;
} // namespace woff2
+5 -5
View File
@@ -28,7 +28,7 @@ static const int32_t kFLAG_WE_HAVE_AN_X_AND_Y_SCALE = 1 << 6;
static const int32_t kFLAG_WE_HAVE_A_TWO_BY_TWO = 1 << 7;
static const int32_t kFLAG_WE_HAVE_INSTRUCTIONS = 1 << 8;
auto ReadCompositeGlyphData(Buffer* buffer, Glyph* glyph) -> bool {
fn ReadCompositeGlyphData(Buffer* buffer, Glyph* glyph) -> bool {
glyph->have_instructions = false;
glyph->composite_data = buffer->buffer() + buffer->offset();
size_t start_offset = buffer->offset();
@@ -62,7 +62,7 @@ auto ReadCompositeGlyphData(Buffer* buffer, Glyph* glyph) -> bool {
return true;
}
auto ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) -> bool {
fn ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) -> bool {
Buffer buffer(data, len);
int16_t num_contours;
@@ -224,7 +224,7 @@ void StoreInstructions(const Glyph& glyph, size_t* offset, uint8_t* dst) {
StoreBytes(glyph.instructions_data, glyph.instructions_size, offset, dst);
}
auto StoreEndPtsOfContours(const Glyph& glyph, size_t* offset, uint8_t* dst) -> bool {
fn StoreEndPtsOfContours(const Glyph& glyph, size_t* offset, uint8_t* dst) -> bool {
int end_point = -1;
for (const auto& contour : glyph.contours) {
end_point += contour.size();
@@ -237,7 +237,7 @@ auto StoreEndPtsOfContours(const Glyph& glyph, size_t* offset, uint8_t* dst) ->
return true;
}
auto StorePoints(const Glyph& glyph, size_t* offset,
fn StorePoints(const Glyph& glyph, size_t* offset,
uint8_t* dst, size_t dst_size) -> bool {
int last_flag = -1;
int repeat_count = 0;
@@ -333,7 +333,7 @@ auto StorePoints(const Glyph& glyph, size_t* offset,
} // namespace
auto StoreGlyph(const Glyph& glyph, uint8_t* dst, size_t* dst_size) -> bool {
fn StoreGlyph(const Glyph& glyph, uint8_t* dst, size_t* dst_size) -> bool {
size_t offset = 0;
if (glyph.composite_data_size > 0) {
// Composite glyph.
+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.
auto ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) -> bool;
fn ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) -> bool;
// Stores the glyph into the specified dst buffer. The *dst_size is the buffer
// size on entry and is set to the actual (unpadded) stored size on exit.
// Returns false on buffer overflow.
auto StoreGlyph(const Glyph& glyph, uint8_t* dst, size_t* dst_size) -> bool;
fn StoreGlyph(const Glyph& glyph, uint8_t* dst, size_t* dst_size) -> bool;
} // namespace woff2
+10 -10
View File
@@ -36,7 +36,7 @@ void StoreLoca(int index_fmt, uint32_t value, size_t* offset, uint8_t* dst) {
namespace {
auto WriteNormalizedLoca(int index_fmt, int num_glyphs, Font* font) -> bool {
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);
@@ -85,7 +85,7 @@ auto WriteNormalizedLoca(int index_fmt, int num_glyphs, Font* font) -> bool {
namespace {
auto MakeEditableBuffer(Font* font, int tableTag) -> bool {
fn MakeEditableBuffer(Font* font, int tableTag) -> bool {
Font::Table* table = font->FindTable(tableTag);
if (table == nullptr) {
return FONT_COMPRESSION_FAILURE();
@@ -106,7 +106,7 @@ auto MakeEditableBuffer(Font* font, int tableTag) -> bool {
} // namespace
auto NormalizeGlyphs(Font* font) -> bool {
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);
@@ -163,7 +163,7 @@ auto NormalizeGlyphs(Font* font) -> bool {
return true;
}
auto NormalizeOffsets(Font* font) -> bool {
fn NormalizeOffsets(Font* font) -> bool {
uint32_t offset = 12 + 16 * font->num_tables;
for (auto tag : font->OutputOrderedTags()) {
auto& table = font->tables[tag];
@@ -175,7 +175,7 @@ auto NormalizeOffsets(Font* font) -> bool {
namespace {
auto ComputeHeaderChecksum(const Font& font) -> uint32_t {
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;
@@ -197,7 +197,7 @@ auto ComputeHeaderChecksum(const Font& font) -> uint32_t {
} // namespace
auto FixChecksums(Font* font) -> bool {
fn FixChecksums(Font* font) -> bool {
Font::Table* head_table = font->FindTable(kHeadTableTag);
if (head_table == nullptr) {
return FONT_COMPRESSION_FAILURE();
@@ -235,7 +235,7 @@ auto FixChecksums(Font* font) -> bool {
}
namespace {
auto MarkTransformed(Font* font) -> bool {
fn MarkTransformed(Font* font) -> bool {
Font::Table* head_table = font->FindTable(kHeadTableTag);
if (head_table == nullptr) {
return FONT_COMPRESSION_FAILURE();
@@ -255,7 +255,7 @@ auto MarkTransformed(Font* font) -> bool {
} // namespace
auto NormalizeWithoutFixingChecksums(Font* font) -> bool {
fn NormalizeWithoutFixingChecksums(Font* font) -> bool {
return (MakeEditableBuffer(font, kHeadTableTag) &&
RemoveDigitalSignature(font) &&
MarkTransformed(font) &&
@@ -263,12 +263,12 @@ auto NormalizeWithoutFixingChecksums(Font* font) -> bool {
NormalizeOffsets(font));
}
auto NormalizeFont(Font* font) -> bool {
fn NormalizeFont(Font* font) -> bool {
return (NormalizeWithoutFixingChecksums(font) &&
FixChecksums(font));
}
auto NormalizeFontCollection(FontCollection* font_collection) -> bool {
fn NormalizeFontCollection(FontCollection* font_collection) -> bool {
if (font_collection->fonts.size() == 1) {
return NormalizeFont(&font_collection->fonts[0]);
}
+5 -5
View File
@@ -19,20 +19,20 @@ struct FontCollection;
// Changes the offset fields of the table headers so that the data for the
// tables will be written in order of increasing tag values, without any gaps
// other than the 4-byte padding.
auto NormalizeOffsets(Font* font) -> bool;
fn NormalizeOffsets(Font* font) -> bool;
// Changes the checksum fields of the table headers and the checksum field of
// the head table so that it matches the current data.
auto FixChecksums(Font* font) -> bool;
fn FixChecksums(Font* font) -> bool;
// Parses each of the glyphs in the font and writes them again to the glyf
// table in normalized form, as defined by the StoreGlyph() function. Changes
// the loca table accordigly.
auto NormalizeGlyphs(Font* font) -> bool;
fn NormalizeGlyphs(Font* font) -> bool;
// Performs all of the normalization steps above.
auto NormalizeFont(Font* font) -> bool;
auto NormalizeFontCollection(FontCollection* font_collection) -> bool;
fn NormalizeFont(Font* font) -> bool;
fn NormalizeFontCollection(FontCollection* font_collection) -> bool;
} // namespace woff2
+1 -1
View File
@@ -15,7 +15,7 @@ namespace woff2 {
using uint32 = unsigned int;
inline auto Log2Floor(uint32 n) -> int {
fn auto Log2Floor(uint32 n) -> int {
#if defined(__GNUC__)
return n == 0 ? -1 : 31 ^ __builtin_clz(n);
#else
+1 -1
View File
@@ -15,7 +15,7 @@ namespace woff2 {
// Round a value up to the nearest multiple of 4. Don't round the value in the
// case that rounding up overflows.
template<typename T> auto Round4(T value) -> T {
template<typename T> fn Round4(T value) -> T {
if (std::numeric_limits<T>::max() - value < 3) {
return value;
}
+2 -2
View File
@@ -18,7 +18,7 @@
namespace woff2 {
inline auto StoreU32(uint8_t* dst, size_t offset, uint32_t x) -> size_t {
fn auto StoreU32(uint8_t* dst, size_t offset, uint32_t x) -> size_t {
dst[offset] = x >> 24;
dst[offset + 1] = x >> 16;
dst[offset + 2] = x >> 8;
@@ -26,7 +26,7 @@ inline auto StoreU32(uint8_t* dst, size_t offset, uint32_t x) -> size_t {
return offset + 4;
}
inline auto Store16(uint8_t* dst, size_t offset, int x) -> size_t {
fn auto Store16(uint8_t* dst, size_t offset, int x) -> size_t {
#if defined(WOFF_LITTLE_ENDIAN)
*reinterpret_cast<uint16_t*>(dst + offset) =
((x & 0xFF) << 8) | ((x & 0xFF00) >> 8);
+4 -4
View File
@@ -58,7 +58,7 @@ class GlyfEncoder {
bbox_bitmap_.resize(((num_glyphs + 31) >> 5) << 2);
}
auto Encode(int glyph_id, const Glyph& glyph) -> bool {
fn Encode(int glyph_id, const Glyph& glyph) -> bool {
if (glyph.composite_data_size > 0) {
WriteCompositeGlyph(glyph_id, glyph);
} else if (glyph.contours.size() > 0) {
@@ -97,7 +97,7 @@ class GlyfEncoder {
glyph.instructions_data, glyph.instructions_size);
}
auto ShouldWriteSimpleGlyphBbox(const Glyph& glyph) -> bool {
fn ShouldWriteSimpleGlyphBbox(const Glyph& glyph) -> bool {
if (glyph.contours.empty() || glyph.contours[0].empty()) {
return glyph.x_min || glyph.y_min || glyph.x_max || glyph.y_max;
}
@@ -236,7 +236,7 @@ class GlyfEncoder {
} // namespace
auto TransformGlyfAndLocaTables(Font* font) -> bool {
fn TransformGlyfAndLocaTables(Font* font) -> bool {
// no transform for CFF
const Font::Table* glyf_table = font->FindTable(kGlyfTableTag);
const Font::Table* loca_table = font->FindTable(kLocaTableTag);
@@ -293,7 +293,7 @@ auto TransformGlyfAndLocaTables(Font* font) -> bool {
// See https://www.microsoft.com/typography/otspec/hmtx.htm
// See WOFF2 spec, 5.4. Transformed hmtx table format
auto TransformHmtxTable(Font* font) -> bool {
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);
+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.
auto TransformGlyfAndLocaTables(Font* font) -> bool;
fn TransformGlyfAndLocaTables(Font* font) -> bool;
// Apply transformation to hmtx table if applicable for this font.
auto TransformHmtxTable(Font* font) -> bool;
fn TransformHmtxTable(Font* font) -> bool;
} // namespace woff2
+4 -4
View File
@@ -10,7 +10,7 @@
namespace woff2 {
auto Size255UShort(uint16_t value) -> size_t {
fn Size255UShort(uint16_t value) -> size_t {
size_t result = 3;
if (value < 253) {
result = 1;
@@ -47,7 +47,7 @@ void Store255UShort(int val, size_t* offset, uint8_t* dst) {
}
// Based on section 6.1.1 of MicroType Express draft spec
auto Read255UShort(Buffer* buf, unsigned int* value) -> bool {
fn Read255UShort(Buffer* buf, unsigned int* value) -> bool {
static const int kWordCode = 253;
static const int kOneMoreByteCode2 = 254;
static const int kOneMoreByteCode1 = 255;
@@ -83,7 +83,7 @@ auto Read255UShort(Buffer* buf, unsigned int* value) -> bool {
}
}
auto ReadBase128(Buffer* buf, uint32_t* 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;
@@ -108,7 +108,7 @@ auto ReadBase128(Buffer* buf, uint32_t* value) -> bool {
return FONT_COMPRESSION_FAILURE();
}
auto Base128Size(size_t n) -> size_t {
fn Base128Size(size_t n) -> size_t {
size_t size = 1;
for (; n >= 128; n >>= 7) { ++size;
}
+4 -4
View File
@@ -15,13 +15,13 @@
namespace woff2 {
auto Size255UShort(uint16_t value) -> size_t;
auto Read255UShort(Buffer* buf, unsigned int* value) -> bool;
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);
auto Base128Size(size_t n) -> size_t;
auto ReadBase128(Buffer* buf, uint32_t* value) -> bool;
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);
} // namespace woff2
+2 -2
View File
@@ -15,7 +15,7 @@
namespace woff2 {
auto ComputeULongSum(const uint8_t* buf, size_t size) -> uint32_t {
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) {
@@ -43,7 +43,7 @@ auto ComputeULongSum(const uint8_t* buf, size_t size) -> uint32_t {
return checksum;
}
auto CollectionHeaderSize(uint32_t header_version, uint32_t num_fonts) -> size_t {
fn CollectionHeaderSize(uint32_t header_version, uint32_t num_fonts) -> size_t {
size_t size = 0;
if (header_version == 0x00020000) {
size += 12; // ulDsig{Tag,Length,Offset}
+3 -3
View File
@@ -45,7 +45,7 @@ struct Table {
uint32_t dst_length;
const uint8_t* dst_data;
auto operator<(const Table& other) const -> bool {
fn operator<(const Table& other) const -> bool {
return tag < other.tag;
}
};
@@ -54,10 +54,10 @@ struct Table {
// Size of the collection header. 0 if version indicates this isn't a
// collection. Ref http://www.microsoft.com/typography/otspec/otff.htm,
// True Type Collections
auto CollectionHeaderSize(uint32_t header_version, uint32_t num_fonts) -> size_t;
fn CollectionHeaderSize(uint32_t header_version, uint32_t num_fonts) -> size_t;
// Compute checksum over size bytes of buf
auto ComputeULongSum(const uint8_t* buf, size_t size) -> uint32_t;
fn ComputeULongSum(const uint8_t* buf, size_t size) -> uint32_t;
} // namespace woff2
+1 -1
View File
@@ -12,7 +12,7 @@
#include <woff2/encode.h>
auto main(int argc, char **argv) -> int {
fn main(int argc, char **argv) -> int {
using std::string;
if (argc != 2) {
+23 -23
View File
@@ -107,12 +107,12 @@ struct RebuildMetadata {
std::map<std::pair<uint32_t, uint32_t>, uint32_t> checksums;
};
auto WithSign(int flag, int baseval) -> int {
fn WithSign(int flag, int baseval) -> int {
// Precondition: 0 <= baseval < 65536 (to avoid integer overflow)
return (flag & 1) ? baseval : -baseval;
}
auto _SafeIntAddition(int a, int b, int* result) -> bool {
fn _SafeIntAddition(int a, int b, int* result) -> bool {
if (PREDICT_FALSE(
((a > 0) && (b > std::numeric_limits<int>::max() - a)) ||
((a < 0) && (b < std::numeric_limits<int>::min() - a)))) {
@@ -122,7 +122,7 @@ auto _SafeIntAddition(int a, int b, int* result) -> bool {
return true;
}
auto TripletDecode(const uint8_t* flags_in, const uint8_t* in, size_t in_size,
fn TripletDecode(const uint8_t* flags_in, const uint8_t* in, size_t in_size,
unsigned int n_points, Point* result, size_t* in_bytes_consumed) -> bool {
int x = 0;
int y = 0;
@@ -191,7 +191,7 @@ auto TripletDecode(const uint8_t* flags_in, const uint8_t* in, size_t in_size,
// This function stores just the point data. On entry, dst points to the
// beginning of a simple glyph. Returns true on success.
auto StorePoints(unsigned int n_points, const Point* points,
fn StorePoints(unsigned int n_points, const Point* points,
unsigned int n_contours, unsigned int instruction_length,
uint8_t* dst, size_t dst_size, size_t* glyph_size) -> bool {
// I believe that n_contours < 65536, in which case this is safe. However, a
@@ -321,7 +321,7 @@ void ComputeBbox(unsigned int n_points, const Point* points, uint8_t* dst) {
}
auto SizeOfComposite(Buffer composite_stream, size_t* size,
fn SizeOfComposite(Buffer composite_stream, size_t* size,
bool* have_instructions) -> bool {
size_t start_offset = composite_stream.offset();
bool we_have_instructions = false;
@@ -356,7 +356,7 @@ auto SizeOfComposite(Buffer composite_stream, size_t* size,
return true;
}
auto Pad4(WOFF2Out* out) -> bool {
fn Pad4(WOFF2Out* out) -> bool {
uint8_t zeroes[] = {0, 0, 0};
if (PREDICT_FALSE(out->Size() + 3 < out->Size())) {
return FONT_COMPRESSION_FAILURE();
@@ -371,7 +371,7 @@ auto Pad4(WOFF2Out* out) -> bool {
}
// Build TrueType loca table
auto StoreLoca(const std::vector<uint32_t>& loca_values, int index_format,
fn StoreLoca(const std::vector<uint32_t>& loca_values, int index_format,
uint32_t* checksum, WOFF2Out* out) -> bool {
// TODO(user) figure out what index format to use based on whether max
// offset fits into uint16_t or not
@@ -398,7 +398,7 @@ auto StoreLoca(const std::vector<uint32_t>& loca_values, int index_format,
}
// Reconstruct entire glyf table based on transformed original
auto ReconstructGlyf(const uint8_t* data, Table* glyf_table,
fn ReconstructGlyf(const uint8_t* data, Table* glyf_table,
uint32_t* glyf_checksum, Table * loca_table,
uint32_t* loca_checksum, WOFF2FontInfo* info,
WOFF2Out* out) -> bool {
@@ -650,7 +650,7 @@ auto ReconstructGlyf(const uint8_t* data, Table* glyf_table,
return true;
}
auto FindTable(std::vector<Table*>* tables, uint32_t tag) -> Table* {
fn FindTable(std::vector<Table*>* tables, uint32_t tag) -> Table* {
for (Table* table : *tables) {
if (table->tag == tag) {
return table;
@@ -660,7 +660,7 @@ auto FindTable(std::vector<Table*>* tables, uint32_t tag) -> Table* {
}
// Get numberOfHMetrics, https://www.microsoft.com/typography/otspec/hhea.htm
auto ReadNumHMetrics(const uint8_t* data, size_t data_size,
fn ReadNumHMetrics(const uint8_t* data, size_t data_size,
uint16_t* num_hmetrics) -> bool {
// Skip 34 to reach 'hhea' numberOfHMetrics
Buffer buffer(data, data_size);
@@ -671,7 +671,7 @@ auto ReadNumHMetrics(const uint8_t* data, size_t data_size,
}
// http://dev.w3.org/webfonts/WOFF2/spec/Overview.html#hmtx_table_format
auto ReconstructTransformedHmtx(const uint8_t* transformed_buf,
fn ReconstructTransformedHmtx(const uint8_t* transformed_buf,
size_t transformed_size,
uint16_t num_glyphs,
uint16_t num_hmetrics,
@@ -768,7 +768,7 @@ auto ReconstructTransformedHmtx(const uint8_t* transformed_buf,
return true;
}
auto Woff2Uncompress(uint8_t* dst_buf, size_t dst_size,
fn Woff2Uncompress(uint8_t* dst_buf, size_t dst_size,
const uint8_t* src_buf, size_t src_size) -> bool {
size_t uncompressed_size = dst_size;
BrotliDecoderResult result = BrotliDecoderDecompress(
@@ -780,7 +780,7 @@ auto Woff2Uncompress(uint8_t* dst_buf, size_t dst_size,
return true;
}
auto ReadTableDirectory(Buffer* file, std::vector<Table>* tables,
fn ReadTableDirectory(Buffer* file, std::vector<Table>* tables,
size_t num_tables) -> bool {
uint32_t src_offset = 0;
for (size_t i = 0; i < num_tables; ++i) {
@@ -839,7 +839,7 @@ auto ReadTableDirectory(Buffer* file, std::vector<Table>* tables,
}
// Writes a single Offset Table entry
auto StoreOffsetTable(uint8_t* result, size_t offset, uint32_t flavor,
fn StoreOffsetTable(uint8_t* result, size_t offset, uint32_t flavor,
uint16_t num_tables) -> size_t {
offset = StoreU32(result, offset, flavor); // sfnt version
offset = Store16(result, offset, num_tables); // num_tables
@@ -855,7 +855,7 @@ auto StoreOffsetTable(uint8_t* result, size_t offset, uint32_t flavor,
return offset;
}
auto StoreTableEntry(uint8_t* result, uint32_t offset, uint32_t tag) -> size_t {
fn StoreTableEntry(uint8_t* result, uint32_t offset, uint32_t tag) -> size_t {
offset = StoreU32(result, offset, tag);
offset = StoreU32(result, offset, 0);
offset = StoreU32(result, offset, 0);
@@ -864,7 +864,7 @@ auto StoreTableEntry(uint8_t* result, uint32_t offset, uint32_t tag) -> size_t {
}
// First table goes after all the headers, table directory, etc
auto ComputeOffsetToFirstTable(const WOFF2Header& hdr) -> uint64_t {
fn ComputeOffsetToFirstTable(const WOFF2Header& hdr) -> uint64_t {
uint64_t offset = kSfntHeaderSize +
kSfntEntrySize * static_cast<uint64_t>(hdr.num_tables);
if (hdr.header_version) {
@@ -877,7 +877,7 @@ auto ComputeOffsetToFirstTable(const WOFF2Header& hdr) -> uint64_t {
return offset;
}
auto Tables(WOFF2Header* hdr, size_t font_index) -> std::vector<Table*> {
fn Tables(WOFF2Header* hdr, size_t font_index) -> std::vector<Table*> {
std::vector<Table*> tables;
if (PREDICT_FALSE(hdr->header_version)) {
for (auto index : hdr->ttc_fonts[font_index].table_indices) {
@@ -893,7 +893,7 @@ auto Tables(WOFF2Header* hdr, size_t font_index) -> std::vector<Table*> {
// Offset tables assumed to have been written in with 0's initially.
// WOFF2Header isn't const so we can use [] instead of at() (which upsets FF)
auto ReconstructFont(uint8_t* transformed_buf,
fn ReconstructFont(uint8_t* transformed_buf,
const uint32_t transformed_buf_size,
RebuildMetadata* metadata,
WOFF2Header* hdr,
@@ -1043,7 +1043,7 @@ auto ReconstructFont(uint8_t* transformed_buf,
return true;
}
auto ReadWOFF2Header(const uint8_t* data, size_t length, WOFF2Header* hdr) -> bool {
fn ReadWOFF2Header(const uint8_t* data, size_t length, WOFF2Header* hdr) -> bool {
Buffer file(data, length);
uint32_t signature;
@@ -1226,7 +1226,7 @@ auto ReadWOFF2Header(const uint8_t* data, size_t length, WOFF2Header* hdr) -> bo
}
// Write everything before the actual table data
auto WriteHeaders(RebuildMetadata* metadata,
fn WriteHeaders(RebuildMetadata* metadata,
WOFF2Header* hdr, WOFF2Out* out) -> bool {
std::vector<uint8_t> output(ComputeOffsetToFirstTable(*hdr), 0);
@@ -1309,7 +1309,7 @@ auto WriteHeaders(RebuildMetadata* metadata,
} // namespace
auto ComputeWOFF2FinalSize(const uint8_t* data, size_t length) -> size_t {
fn ComputeWOFF2FinalSize(const uint8_t* data, size_t length) -> size_t {
Buffer file(data, length);
uint32_t total_length;
@@ -1320,13 +1320,13 @@ auto ComputeWOFF2FinalSize(const uint8_t* data, size_t length) -> size_t {
return total_length;
}
auto ConvertWOFF2ToTTF(uint8_t *result, size_t result_length,
fn ConvertWOFF2ToTTF(uint8_t *result, size_t result_length,
const uint8_t *data, size_t length) -> bool {
WOFF2MemoryOut out(result, result_length);
return ConvertWOFF2ToTTF(data, length, &out);
}
auto ConvertWOFF2ToTTF(const uint8_t* data, size_t length,
fn ConvertWOFF2ToTTF(const uint8_t* data, size_t length,
WOFF2Out* out) -> bool {
RebuildMetadata metadata;
WOFF2Header hdr;
+1 -1
View File
@@ -13,7 +13,7 @@
#include <woff2/decode.h>
auto main(int argc, char **argv) -> int {
fn main(int argc, char **argv) -> int {
using std::string;
if (argc != 2) {
+15 -15
View File
@@ -38,7 +38,7 @@ using std::vector;
const size_t kWoff2HeaderSize = 48;
const size_t kWoff2EntrySize = 20;
auto Compress(const uint8_t* data, const size_t len, uint8_t* result,
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;
if (BrotliEncoderCompress(quality, BROTLI_DEFAULT_WINDOW, mode, len, data,
@@ -49,21 +49,21 @@ auto Compress(const uint8_t* data, const size_t len, uint8_t* result,
return true;
}
auto Woff2Compress(const uint8_t* data, const size_t len,
fn Woff2Compress(const uint8_t* data, const size_t len,
uint8_t* result, uint32_t* result_len,
int quality) -> bool {
return Compress(data, len, result, result_len,
BROTLI_MODE_FONT, quality);
}
auto TextCompress(const uint8_t* data, const size_t len,
fn TextCompress(const uint8_t* data, const size_t len,
uint8_t* result, uint32_t* result_len,
int quality) -> bool {
return Compress(data, len, result, result_len,
BROTLI_MODE_TEXT, quality);
}
auto KnownTableIndex(uint32_t tag) -> int {
fn KnownTableIndex(uint32_t tag) -> int {
for (int i = 0; i < 63; ++i) {
if (tag == kKnownTags[i]) { return i;
}
@@ -86,7 +86,7 @@ void StoreTableEntry(const Table& table, size_t* offset, uint8_t* dst) {
}
}
auto TableEntrySize(const Table& table) -> size_t {
fn TableEntrySize(const Table& table) -> size_t {
uint8_t flag_byte = KnownTableIndex(table.tag);
size_t size = ((flag_byte & 0x3f) != 0x3f) ? 1 : 5;
size += Base128Size(table.src_length);
@@ -96,7 +96,7 @@ auto TableEntrySize(const Table& table) -> size_t {
return size;
}
auto ComputeWoff2Length(const FontCollection& font_collection,
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,
@@ -138,7 +138,7 @@ auto ComputeWoff2Length(const FontCollection& font_collection,
return size;
}
auto ComputeUncompressedLength(const Font& font) -> size_t {
fn ComputeUncompressedLength(const Font& font) -> size_t {
// sfnt header + offset table
size_t size = 12 + 16 * font.num_tables;
for (const auto& entry : font.tables) {
@@ -152,7 +152,7 @@ auto ComputeUncompressedLength(const Font& font) -> size_t {
return size;
}
auto ComputeUncompressedLength(const FontCollection& font_collection) -> size_t {
fn ComputeUncompressedLength(const FontCollection& font_collection) -> size_t {
if (font_collection.flavor != kTtcFontFlavor) {
return ComputeUncompressedLength(font_collection.fonts[0]);
}
@@ -164,7 +164,7 @@ auto ComputeUncompressedLength(const FontCollection& font_collection) -> size_t
return size;
}
auto ComputeTotalTransformLength(const Font& font) -> size_t {
fn ComputeTotalTransformLength(const Font& font) -> size_t {
size_t total = 0;
for (const auto& i : font.tables) {
const Font::Table& table = i.second;
@@ -182,11 +182,11 @@ auto ComputeTotalTransformLength(const Font& font) -> size_t {
} // namespace
auto MaxWOFF2CompressedSize(const uint8_t* data, size_t length) -> size_t {
fn MaxWOFF2CompressedSize(const uint8_t* data, size_t length) -> size_t {
return MaxWOFF2CompressedSize(data, length, "");
}
auto MaxWOFF2CompressedSize(const uint8_t* /*data*/, size_t length,
fn MaxWOFF2CompressedSize(const uint8_t* /*data*/, size_t length,
const string& extended_metadata) -> size_t {
// Except for the header size, which is 32 bytes larger in woff2 format,
// all other parts should be smaller (table header in short format,
@@ -195,11 +195,11 @@ auto MaxWOFF2CompressedSize(const uint8_t* /*data*/, size_t length,
return length + 1024 + extended_metadata.length();
}
auto CompressedBufferSize(uint32_t original_size) -> uint32_t {
fn CompressedBufferSize(uint32_t original_size) -> uint32_t {
return 1.2 * original_size + 10240;
}
auto TransformFontCollection(FontCollection* font_collection) -> bool {
fn TransformFontCollection(FontCollection* font_collection) -> bool {
for (auto& font : font_collection->fonts) {
if (!TransformGlyfAndLocaTables(&font)) {
#ifdef FONT_COMPRESSION_BIN
@@ -212,14 +212,14 @@ auto TransformFontCollection(FontCollection* font_collection) -> bool {
return true;
}
auto ConvertTTFToWOFF2(const uint8_t *data, size_t length,
fn ConvertTTFToWOFF2(const uint8_t *data, size_t length,
uint8_t *result, size_t *result_length) -> bool {
WOFF2Params params;
return ConvertTTFToWOFF2(data, length, result, result_length,
params);
}
auto ConvertTTFToWOFF2(const uint8_t *data, size_t length,
fn ConvertTTFToWOFF2(const uint8_t *data, size_t length,
uint8_t *result, size_t *result_length,
const WOFF2Params& params) -> bool {
FontCollection font_collection;
+2 -2
View File
@@ -15,7 +15,7 @@
#include "./table_tags.h"
#include "./variable_length.h"
auto PrintTag(int tag) -> std::string {
fn PrintTag(int tag) -> std::string {
if (tag & 0x80808080) {
return std::string("_xfm"); // print _xfm for xform tables (else garbage)
}
@@ -28,7 +28,7 @@ auto PrintTag(int tag) -> std::string {
return std::string(printable, 4);
}
auto main(int argc, char **argv) -> int {
fn main(int argc, char **argv) -> int {
using std::string;
if (argc != 2) {
+4 -4
View File
@@ -17,11 +17,11 @@ WOFF2StringOut::WOFF2StringOut(string* buf)
max_size_(kDefaultMaxSize),
offset_(0) {}
auto WOFF2StringOut::Write(const void *buf, size_t n) -> bool {
fn WOFF2StringOut::Write(const void *buf, size_t n) -> bool {
return Write(buf, offset_, n);
}
auto WOFF2StringOut::Write(const void *buf, size_t offset, size_t n) -> bool {
fn WOFF2StringOut::Write(const void *buf, size_t offset, size_t n) -> bool {
if (offset > max_size_ || n > max_size_ - offset) {
return false;
}
@@ -50,11 +50,11 @@ WOFF2MemoryOut::WOFF2MemoryOut(uint8_t* buf, size_t buf_size)
buf_size_(buf_size),
offset_(0) {}
auto WOFF2MemoryOut::Write(const void *buf, size_t n) -> bool {
fn WOFF2MemoryOut::Write(const void *buf, size_t n) -> bool {
return Write(buf, offset_, n);
}
auto WOFF2MemoryOut::Write(const void *buf, size_t offset, size_t n) -> bool {
fn WOFF2MemoryOut::Write(const void *buf, size_t offset, size_t n) -> bool {
if (offset > buf_size_ || n > buf_size_ - offset) {
return false;
}
+3
View File
@@ -51,3 +51,6 @@
-D__DATE__="redacted"
-D__TIMESTAMP__="redacted"
-D__TIME__="redacted"
-Wno-unused-variable
-Wno-unused-const-variable
-Wno-sign-compare
+6 -3
View File
@@ -30,6 +30,9 @@ cp "${EXAMPLE}/compile_flags.carbon.txt" \
"${EXAMPLE}/carbon/compile_flags.txt"
# Run the migration tool.
bazel build //migrate_cpp
./bazel-bin/migrate_cpp/migrate_cpp \
"${EXAMPLE}/carbon"
bazel build -c opt //migrate_cpp
# Not sure why, but execution of cpp_refactoring fails while saving refactorings
# if not in the directory. Ideally shouldn't be required, passing the path to
# migrate_cpp should work.
cd "${EXAMPLE}/carbon"
../../../../bazel-bin/migrate_cpp/migrate_cpp .