From 2344a52e24399e8f96728efb97241ce09b8aa4e3 Mon Sep 17 00:00:00 2001 From: Jon Meow <46229924+jonmeow@users.noreply.github.com> Date: Wed, 23 Jun 2021 10:58:32 -0700 Subject: [PATCH] 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. --- migrate_cpp/cpp_refactoring/BUILD | 57 +- migrate_cpp/cpp_refactoring/main.cpp | 5 +- migrate_cpp/cpp_refactoring/matcher.cpp | 13 +- .../cpp_refactoring/matcher_test_base.cpp | 16 +- migrate_cpp/cpp_refactoring/var_decl.cpp | 61 ++ migrate_cpp/cpp_refactoring/var_decl.h | 26 + migrate_cpp/cpp_refactoring/var_decl_test.cpp | 125 +++++ migrate_cpp/migrate_cpp.py | 19 +- .../woff2/carbon/include/woff2/decode.carbon | 10 +- .../woff2/carbon/include/woff2/encode.carbon | 16 +- .../woff2/carbon/include/woff2/output.carbon | 20 +- .../examples/woff2/carbon/src/buffer.carbon | 24 +- .../src/convert_woff2ttf_fuzzer.impl.carbon | 6 +- ...vert_woff2ttf_fuzzer_new_entry.impl.carbon | 7 +- .../examples/woff2/carbon/src/file.carbon | 10 +- .../examples/woff2/carbon/src/font.carbon | 30 +- .../woff2/carbon/src/font.impl.carbon | 171 +++--- .../examples/woff2/carbon/src/glyph.carbon | 4 +- .../woff2/carbon/src/glyph.impl.carbon | 136 ++--- .../woff2/carbon/src/normalize.carbon | 10 +- .../woff2/carbon/src/normalize.impl.carbon | 113 ++-- .../examples/woff2/carbon/src/port.carbon | 2 +- .../examples/woff2/carbon/src/round.carbon | 2 +- .../woff2/carbon/src/store_bytes.carbon | 12 +- .../woff2/carbon/src/table_tags.carbon | 18 +- .../woff2/carbon/src/table_tags.impl.carbon | 66 +-- .../woff2/carbon/src/transform.carbon | 4 +- .../woff2/carbon/src/transform.impl.carbon | 156 +++--- .../woff2/carbon/src/variable_length.carbon | 14 +- .../carbon/src/variable_length.impl.carbon | 50 +- .../woff2/carbon/src/woff2_common.carbon | 16 +- .../woff2/carbon/src/woff2_common.impl.carbon | 18 +- .../carbon/src/woff2_compress.impl.carbon | 18 +- .../woff2/carbon/src/woff2_dec.impl.carbon | 529 +++++++++--------- .../carbon/src/woff2_decompress.impl.carbon | 17 +- .../woff2/carbon/src/woff2_enc.impl.carbon | 192 +++---- .../woff2/carbon/src/woff2_info.impl.carbon | 48 +- .../woff2/carbon/src/woff2_out.impl.carbon | 14 +- 38 files changed, 1103 insertions(+), 952 deletions(-) create mode 100644 migrate_cpp/cpp_refactoring/var_decl.cpp create mode 100644 migrate_cpp/cpp_refactoring/var_decl.h create mode 100644 migrate_cpp/cpp_refactoring/var_decl_test.cpp diff --git a/migrate_cpp/cpp_refactoring/BUILD b/migrate_cpp/cpp_refactoring/BUILD index 843fd918a60c..90155f4957ac 100644 --- a/migrate_cpp/cpp_refactoring/BUILD +++ b/migrate_cpp/cpp_refactoring/BUILD @@ -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", + ], +) diff --git a/migrate_cpp/cpp_refactoring/main.cpp b/migrate_cpp/cpp_refactoring/main.cpp index 93875875535b..8ee8d2e1869d 100644 --- a/migrate_cpp/cpp_refactoring/main.cpp +++ b/migrate_cpp/cpp_refactoring/main.cpp @@ -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()); diff --git a/migrate_cpp/cpp_refactoring/matcher.cpp b/migrate_cpp/cpp_refactoring/matcher.cpp index 1b612cc39372..21c12bddc34c 100644 --- a/migrate_cpp/cpp_refactoring/matcher.cpp +++ b/migrate_cpp/cpp_refactoring/matcher.cpp @@ -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"; } } diff --git a/migrate_cpp/cpp_refactoring/matcher_test_base.cpp b/migrate_cpp/cpp_refactoring/matcher_test_base.cpp index 0934f3f75b67..edbea70f4362 100644 --- a/migrate_cpp/cpp_refactoring/matcher_test_base.cpp +++ b/migrate_cpp/cpp_refactoring/matcher_test_base.cpp @@ -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 actual_lines; - llvm::SplitString(*actual, actual_lines, "\n"); - llvm::SmallVector 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 actual_lines; + llvm::SplitString(*actual, actual_lines, "\n"); + llvm::SmallVector after_lines; + llvm::SplitString(after, after_lines, "\n"); + EXPECT_THAT(actual_lines, testing::ContainerEq(after_lines)); + } } } // namespace Carbon diff --git a/migrate_cpp/cpp_refactoring/var_decl.cpp b/migrate_cpp/cpp_refactoring/var_decl.cpp new file mode 100644 index 000000000000..70a2457bdde2 --- /dev/null +++ b/migrate_cpp/cpp_refactoring/var_decl.cpp @@ -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& 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(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(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 diff --git a/migrate_cpp/cpp_refactoring/var_decl.h b/migrate_cpp/cpp_refactoring/var_decl.h new file mode 100644 index 000000000000..86ac1ff9719e --- /dev/null +++ b/migrate_cpp/cpp_refactoring/var_decl.h @@ -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& 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_ diff --git a/migrate_cpp/cpp_refactoring/var_decl_test.cpp b/migrate_cpp/cpp_refactoring/var_decl_test.cpp new file mode 100644 index 000000000000..3ad6abc1a21c --- /dev/null +++ b/migrate_cpp/cpp_refactoring/var_decl_test.cpp @@ -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 diff --git a/migrate_cpp/migrate_cpp.py b/migrate_cpp/migrate_cpp.py index fb3d6bdbb987..efc584668579 100644 --- a/migrate_cpp/migrate_cpp.py +++ b/migrate_cpp/migrate_cpp.py @@ -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.""" diff --git a/third_party/examples/woff2/carbon/include/woff2/decode.carbon b/third_party/examples/woff2/carbon/include/woff2/decode.carbon index 43df2af082c8..23961eb97ec2 100644 --- a/third_party/examples/woff2/carbon/include/woff2/decode.carbon +++ b/third_party/examples/woff2/carbon/include/woff2/decode.carbon @@ -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 diff --git a/third_party/examples/woff2/carbon/include/woff2/encode.carbon b/third_party/examples/woff2/carbon/include/woff2/encode.carbon index f68e9211c0cb..8d696c1a6bc7 100644 --- a/third_party/examples/woff2/carbon/include/woff2/encode.carbon +++ b/third_party/examples/woff2/carbon/include/woff2/encode.carbon @@ -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 diff --git a/third_party/examples/woff2/carbon/include/woff2/output.carbon b/third_party/examples/woff2/carbon/include/woff2/output.carbon index 157cd2e8c495..35cb56703b39 100644 --- a/third_party/examples/woff2/carbon/include/woff2/output.carbon +++ b/third_party/examples/woff2/carbon/include/woff2/output.carbon @@ -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_; diff --git a/third_party/examples/woff2/carbon/src/buffer.carbon b/third_party/examples/woff2/carbon/src/buffer.carbon index 61a4d3b5f927..baa26a59e105 100644 --- a/third_party/examples/woff2/carbon/src/buffer.carbon +++ b/third_party/examples/woff2/carbon/src/buffer.carbon @@ -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(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(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_; diff --git a/third_party/examples/woff2/carbon/src/convert_woff2ttf_fuzzer.impl.carbon b/third_party/examples/woff2/carbon/src/convert_woff2ttf_fuzzer.impl.carbon index 24c7926480cd..13d252b37de7 100644 --- a/third_party/examples/woff2/carbon/src/convert_woff2ttf_fuzzer.impl.carbon +++ b/third_party/examples/woff2/carbon/src/convert_woff2ttf_fuzzer.impl.carbon @@ -4,9 +4,9 @@ #include // 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; diff --git a/third_party/examples/woff2/carbon/src/convert_woff2ttf_fuzzer_new_entry.impl.carbon b/third_party/examples/woff2/carbon/src/convert_woff2ttf_fuzzer_new_entry.impl.carbon index 5eeda0ac3623..c7fe4fa15972 100644 --- a/third_party/examples/woff2/carbon/src/convert_woff2ttf_fuzzer_new_entry.impl.carbon +++ b/third_party/examples/woff2/carbon/src/convert_woff2ttf_fuzzer_new_entry.impl.carbon @@ -1,12 +1,11 @@ #include #include -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; } diff --git a/third_party/examples/woff2/carbon/src/file.carbon b/third_party/examples/woff2/carbon/src/file.carbon index bd6003b4d479..8e830b08d1f7 100644 --- a/third_party/examples/woff2/carbon/src/file.carbon +++ b/third_party/examples/woff2/carbon/src/file.carbon @@ -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(ifs.rdbuf()), std::istreambuf_iterator()); } -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(ofs)); } diff --git a/third_party/examples/woff2/carbon/src/font.carbon b/third_party/examples/woff2/carbon/src/font.carbon index f5a8c5beb737..b8dd0817f370 100644 --- a/third_party/examples/woff2/carbon/src/font.carbon +++ b/third_party/examples/woff2/carbon/src/font.carbon @@ -47,8 +47,8 @@ struct Font { std::map tables; [[nodiscard]] fn OutputOrderedTags() const -> std::vector; - 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 diff --git a/third_party/examples/woff2/carbon/src/font.impl.carbon b/third_party/examples/woff2/carbon/src/font.impl.carbon index 838c65a1ab7a..72f5e0aa5ada 100644 --- a/third_party/examples/woff2/carbon/src/font.impl.carbon +++ b/third_party/examples/woff2/carbon/src/font.impl.carbon @@ -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_node, 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_node, void *> *, long>>; return it == tables.end() ? nullptr : &it->second; } fn Font::OutputOrderedTags() const -> std::vector { - std::vector output_order; + var output_order: std::vector; - for (const auto& i : tables) { - const Font::Table& table = i.second; + for (const auto& i var __begin1: std::__map_const_iterator, std::__tree_node, void *> *, long>> var __range1: const std::map &) { + 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 { } // 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; + var loca_loc: std::__wrap_iter; 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 { 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 intervals; - for (uint16_t i = 0; i < font->num_tables; ++i) { - Font::Table table; + var intervals: std::map; + 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_node, void *> *, long>> var __range1: std::map &) { 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* all_tables) -> bool { +fn ReadCollectionFont(file: woff2::Buffer *, data: const uint8_t *, len: size_t, + font: woff2::Font *, + all_tables: std::map *) -> 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_node, void *> *, long>> var __range1: std::map &) { + 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 offsets; - for (size_t i = 0; i < num_fonts; i++) { - uint32_t offset; + var offsets: std::vector; + 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; - std::map all_tables; - for (const auto offset : offsets) { + var all_tables: std::map; + for (const auto offset var __begin1: std::__wrap_iter var __range1: std::vector &) { 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_node, void *> *, long>> var __range1: const std::map &) { + 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 var __range1: const std::vector &) { // 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_node, void *> *, long>> var __range1: const std::map &) { 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 var __range1: const std::vector &) { 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_node, void *> *, long>>; if (it != font->tables.end()) { font->tables.erase(it); font->num_tables = font->tables.size(); diff --git a/third_party/examples/woff2/carbon/src/glyph.carbon b/third_party/examples/woff2/carbon/src/glyph.carbon index cac7672f0731..45b7ffc579ee 100644 --- a/third_party/examples/woff2/carbon/src/glyph.carbon +++ b/third_party/examples/woff2/carbon/src/glyph.carbon @@ -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 diff --git a/third_party/examples/woff2/carbon/src/glyph.impl.carbon b/third_party/examples/woff2/carbon/src/glyph.impl.carbon index 253f59a1addd..03c8dac07dba 100644 --- a/third_party/examples/woff2/carbon/src/glyph.impl.carbon +++ b/third_party/examples/woff2/carbon/src/glyph.impl.carbon @@ -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 > flags(num_contours); + var flags: std::vector>; { - 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 *> var __range2: const std::vector> &) { end_point += contour.size(); if (contour.size() > std::numeric_limits::max() || end_point > std::numeric_limits::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 *> var __range2: const std::vector> &) { + for (const auto& point var __begin3: std::__wrap_iter var __range3: const std::vector &) { + 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 *> var __range2: const std::vector> &) { + for (const auto& point var __begin3: std::__wrap_iter var __range3: const std::vector &) { + 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) + diff --git a/third_party/examples/woff2/carbon/src/normalize.carbon b/third_party/examples/woff2/carbon/src/normalize.carbon index 1d80b030089f..9c5d57a37768 100644 --- a/third_party/examples/woff2/carbon/src/normalize.carbon +++ b/third_party/examples/woff2/carbon/src/normalize.carbon @@ -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 diff --git a/third_party/examples/woff2/carbon/src/normalize.impl.carbon b/third_party/examples/woff2/carbon/src/normalize.impl.carbon index bc6497ce32ed..458945900a83 100644 --- a/third_party/examples/woff2/carbon/src/normalize.impl.carbon +++ b/third_party/examples/woff2/carbon/src/normalize.impl.carbon @@ -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 var __range1: std::vector &&) { + 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_node, void *> *, long>> var __range2: const std::map &) { + 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_node, void *> *, long>> var __range1: std::map &) { + 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 var __range1: std::vector &) { 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 var __range1: std::vector &) { + for (auto tag var __begin2: std::__wrap_iter var __range2: std::vector &&) { + 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 var __range1: std::vector &) { if (!FixChecksums(&font)) { #ifdef FONT_COMPRESSION_BIN fprintf(stderr, "Failed to fix checksums\n"); diff --git a/third_party/examples/woff2/carbon/src/port.carbon b/third_party/examples/woff2/carbon/src/port.carbon index 9a56a1dc17ee..e4a04b15f81f 100644 --- a/third_party/examples/woff2/carbon/src/port.carbon +++ b/third_party/examples/woff2/carbon/src/port.carbon @@ -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 diff --git a/third_party/examples/woff2/carbon/src/round.carbon b/third_party/examples/woff2/carbon/src/round.carbon index 5e514ac28676..ad58321dcfef 100644 --- a/third_party/examples/woff2/carbon/src/round.carbon +++ b/third_party/examples/woff2/carbon/src/round.carbon @@ -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 fn Round4(T value) -> T { +template fn Round4(value: T) -> T { if (std::numeric_limits::max() - value < 3) { return value; } diff --git a/third_party/examples/woff2/carbon/src/store_bytes.carbon b/third_party/examples/woff2/carbon/src/store_bytes.carbon index 849771219bc4..d8d95ac1deec 100644 --- a/third_party/examples/woff2/carbon/src/store_bytes.carbon +++ b/third_party/examples/woff2/carbon/src/store_bytes.carbon @@ -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(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(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; } diff --git a/third_party/examples/woff2/carbon/src/table_tags.carbon b/third_party/examples/woff2/carbon/src/table_tags.carbon index 332b5f7821fe..469015308b04 100644 --- a/third_party/examples/woff2/carbon/src/table_tags.carbon +++ b/third_party/examples/woff2/carbon/src/table_tags.carbon @@ -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 diff --git a/third_party/examples/woff2/carbon/src/table_tags.impl.carbon b/third_party/examples/woff2/carbon/src/table_tags.impl.carbon index 78f3cbc863fb..4d252a2c0fa7 100644 --- a/third_party/examples/woff2/carbon/src/table_tags.impl.carbon +++ b/third_party/examples/woff2/carbon/src/table_tags.impl.carbon @@ -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 diff --git a/third_party/examples/woff2/carbon/src/transform.carbon b/third_party/examples/woff2/carbon/src/transform.carbon index 689ab9a8d561..d854c7f0257f 100644 --- a/third_party/examples/woff2/carbon/src/transform.carbon +++ b/third_party/examples/woff2/carbon/src/transform.carbon @@ -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 diff --git a/third_party/examples/woff2/carbon/src/transform.impl.carbon b/third_party/examples/woff2/carbon/src/transform.impl.carbon index 6e0ad17e5c2a..46fe015bb449 100644 --- a/third_party/examples/woff2/carbon/src/transform.impl.carbon +++ b/third_party/examples/woff2/carbon/src/transform.impl.carbon @@ -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* out, const uint8_t* data, size_t len) { +void WriteBytes(out: std::vector *, 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* out, const std::vector& in) { - for (unsigned char i : in) { +void WriteBytes(out: std::vector *, in: const std::vector &) { + for (unsigned char i var __begin2: std::__wrap_iter var __range2: const std::vector &) { out->push_back(i); } } -void WriteUShort(std::vector* out, int value) { +void WriteUShort(out: std::vector *, value: int) { out->push_back(value >> 8); out->push_back(value & 255); } -void WriteLong(std::vector* out, int value) { +void WriteLong(out: std::vector *, 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* 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* result) { + void GetTransformedGlyfBytes(result: std::vector *) { 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 *> var __range2: const std::vector> &) { + for (const auto& point var __begin3: std::__wrap_iter var __range3: const std::vector &) { 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 advance_widths; - std::vector proportional_lsbs; - std::vector monospace_lsbs; + var advance_widths: std::vector; + var proportional_lsbs: std::vector; + var monospace_lsbs: std::vector; - 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* out = &transformed_hmtx->buffer; + var out: std::vector *; WriteBytes(out, &flags, 1); - for (uint16_t advance_width : advance_widths) { + for (uint16_t advance_width var __begin1: std::__wrap_iter var __range1: std::vector &) { WriteUShort(out, advance_width); } if (!remove_proportional_lsb) { - for (int16_t lsb : proportional_lsbs) { + for (int16_t lsb var __begin2: std::__wrap_iter var __range2: std::vector &) { WriteUShort(out, lsb); } } if (!remove_monospace_lsb) { - for (int16_t lsb : monospace_lsbs) { + for (int16_t lsb var __begin2: std::__wrap_iter var __range2: std::vector &) { WriteUShort(out, lsb); } } diff --git a/third_party/examples/woff2/carbon/src/variable_length.carbon b/third_party/examples/woff2/carbon/src/variable_length.carbon index 23b00d711218..a1602537a795 100644 --- a/third_party/examples/woff2/carbon/src/variable_length.carbon +++ b/third_party/examples/woff2/carbon/src/variable_length.carbon @@ -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* 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 *, 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 diff --git a/third_party/examples/woff2/carbon/src/variable_length.impl.carbon b/third_party/examples/woff2/carbon/src/variable_length.impl.carbon index ff3f28572751..9a79655d5c19 100644 --- a/third_party/examples/woff2/carbon/src/variable_length.impl.carbon +++ b/third_party/examples/woff2/carbon/src/variable_length.impl.carbon @@ -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* out, int value) { +void Write255UShort(out: std::vector *, value: int) { if (value < 253) { out->push_back(value); } else if (value < 506) { @@ -38,40 +38,40 @@ void Write255UShort(std::vector* out, int value) { } } -void Store255UShort(int val, size_t* offset, uint8_t* dst) { - std::vector packed; +void Store255UShort(val: int, offset: size_t *, dst: uint8_t *) { + var packed: std::vector; Write255UShort(&packed, val); - for (uint8_t packed_byte : packed) { + for (uint8_t packed_byte var __begin1: std::__wrap_iter var __range1: std::vector &) { 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((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; } diff --git a/third_party/examples/woff2/carbon/src/woff2_common.carbon b/third_party/examples/woff2/carbon/src/woff2_common.carbon index e52c8e282edc..621b8ef19529 100644 --- a/third_party/examples/woff2/carbon/src/woff2_common.carbon +++ b/third_party/examples/woff2/carbon/src/woff2_common.carbon @@ -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 diff --git a/third_party/examples/woff2/carbon/src/woff2_common.impl.carbon b/third_party/examples/woff2/carbon/src/woff2_common.impl.carbon index 0333401f1505..68794426f2fc 100644 --- a/third_party/examples/woff2/carbon/src/woff2_common.impl.carbon +++ b/third_party/examples/woff2/carbon/src/woff2_common.impl.carbon @@ -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(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} } diff --git a/third_party/examples/woff2/carbon/src/woff2_compress.impl.carbon b/third_party/examples/woff2/carbon/src/woff2_compress.impl.carbon index e8fb2c1e2900..36ff11ed37ad 100644 --- a/third_party/examples/woff2/carbon/src/woff2_compress.impl.carbon +++ b/third_party/examples/woff2/carbon/src/woff2_compress.impl.carbon @@ -12,7 +12,7 @@ #include -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(input.data()); - size_t output_size = woff2::MaxWOFF2CompressedSize(input_data, input.size()); - string output(output_size, 0); - auto* output_data = reinterpret_cast(&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"); diff --git a/third_party/examples/woff2/carbon/src/woff2_dec.impl.carbon b/third_party/examples/woff2/carbon/src/woff2_dec.impl.carbon index befe05290dee..cf6cff2d97ec 100644 --- a/third_party/examples/woff2/carbon/src/woff2_dec.impl.carbon +++ b/third_party/examples/woff2/carbon/src/woff2_dec.impl.carbon @@ -38,34 +38,34 @@ using std::vector; // simple glyph flags -const int kGlyfOnCurve = 1 << 0; -const int kGlyfXShort = 1 << 1; -const int kGlyfYShort = 1 << 2; -const int kGlyfRepeat = 1 << 3; -const int kGlyfThisXIsSame = 1 << 4; -const int kGlyfThisYIsSame = 1 << 5; +var kGlyfOnCurve: const int; +var kGlyfXShort: const int; +var kGlyfYShort: const int; +var kGlyfRepeat: const int; +var kGlyfThisXIsSame: const int; +var kGlyfThisYIsSame: const int; // composite glyph flags // See CompositeGlyph.java in sfntly for full definitions -const int FLAG_ARG_1_AND_2_ARE_WORDS = 1 << 0; -const int FLAG_WE_HAVE_A_SCALE = 1 << 3; -const int FLAG_MORE_COMPONENTS = 1 << 5; -const int FLAG_WE_HAVE_AN_X_AND_Y_SCALE = 1 << 6; -const int FLAG_WE_HAVE_A_TWO_BY_TWO = 1 << 7; -const int FLAG_WE_HAVE_INSTRUCTIONS = 1 << 8; +var FLAG_ARG_1_AND_2_ARE_WORDS: const int; +var FLAG_WE_HAVE_A_SCALE: const int; +var FLAG_MORE_COMPONENTS: const int; +var FLAG_WE_HAVE_AN_X_AND_Y_SCALE: const int; +var FLAG_WE_HAVE_A_TWO_BY_TWO: const int; +var FLAG_WE_HAVE_INSTRUCTIONS: const int; -const size_t kCheckSumAdjustmentOffset = 8; +var kCheckSumAdjustmentOffset: const size_t; -const size_t kEndPtsOfContoursOffset = 10; -const size_t kCompositeGlyphBegin = 10; +var kEndPtsOfContoursOffset: const size_t; +var kCompositeGlyphBegin: const size_t; // 98% of Google Fonts have no glyph above 5k bytes // Largest glyph ever observed was 72k bytes -const size_t kDefaultGlyphBuf = 5120; +var kDefaultGlyphBuf: const size_t; // Over 14k test fonts the max compression ratio seen to date was ~20. // >100 suggests you wrote a bad uncompressed size. -const float kMaxPlausibleCompressionRatio = 100.0; +var kMaxPlausibleCompressionRatio: const float; // metadata for a TTC font entry struct TtcFont { @@ -107,12 +107,12 @@ struct RebuildMetadata { std::map, uint32_t> checksums; }; -fn WithSign(int flag, int baseval) -> int { +fn WithSign(flag: int, baseval: int) -> int { // Precondition: 0 <= baseval < 65536 (to avoid integer overflow) return (flag & 1) ? baseval : -baseval; } -fn _SafeIntAddition(int a, int b, int* result) -> bool { +fn _SafeIntAddition(a: int, b: int, result: int *) -> bool { if (PREDICT_FALSE( ((a > 0) && (b > std::numeric_limits::max() - a)) || ((a < 0) && (b < std::numeric_limits::min() - a)))) { @@ -122,21 +122,21 @@ fn _SafeIntAddition(int a, int b, int* result) -> bool { return true; } -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; +fn TripletDecode(flags_in: const uint8_t *, in: const uint8_t *, in_size: size_t, + n_points: unsigned int, result: woff2::Point *, in_bytes_consumed: size_t *) -> bool { + var x: int; + var y: int; if (PREDICT_FALSE(n_points > in_size)) { return FONT_COMPRESSION_FAILURE(); } - unsigned int triplet_index = 0; + var triplet_index: unsigned int; - for (unsigned int i = 0; i < n_points; ++i) { - uint8_t flag = flags_in[i]; - bool on_curve = !(flag >> 7); + for (var i: unsigned int; i < n_points; ++i) { + var flag: uint8_t; + var on_curve: bool; flag &= 0x7f; - unsigned int n_data_bytes; + var n_data_bytes: unsigned int; if (flag < 84) { n_data_bytes = 1; } else if (flag < 120) { @@ -150,7 +150,7 @@ fn TripletDecode(const uint8_t* flags_in, const uint8_t* in, size_t in_size, triplet_index + n_data_bytes < triplet_index)) { return FONT_COMPRESSION_FAILURE(); } - int dx, dy; + var dx: int, var dy: int; if (flag < 10) { dx = 0; dy = WithSign(flag, ((flag & 14) << 7) + in[triplet_index]); @@ -158,17 +158,17 @@ fn TripletDecode(const uint8_t* flags_in, const uint8_t* in, size_t in_size, dx = WithSign(flag, (((flag - 10) & 14) << 7) + in[triplet_index]); dy = 0; } else if (flag < 84) { - int b0 = flag - 20; - int b1 = in[triplet_index]; + var b0: int; + var b1: int; dx = WithSign(flag, 1 + (b0 & 0x30) + (b1 >> 4)); dy = WithSign(flag >> 1, 1 + ((b0 & 0x0c) << 2) + (b1 & 0x0f)); } else if (flag < 120) { - int b0 = flag - 84; + var b0: int; dx = WithSign(flag, 1 + ((b0 / 12) << 8) + in[triplet_index]); dy = WithSign(flag >> 1, 1 + (((b0 % 12) >> 2) << 8) + in[triplet_index + 1]); } else if (flag < 124) { - int b2 = in[triplet_index + 1]; + var b2: int; dx = WithSign(flag, (in[triplet_index] << 4) + (b2 >> 4)); dy = WithSign(flag >> 1, ((b2 & 0x0f) << 8) + in[triplet_index + 2]); } else { @@ -191,25 +191,24 @@ fn 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. -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 { +fn StorePoints(n_points: unsigned int, points: const woff2::Point *, + n_contours: unsigned int, instruction_length: unsigned int, + dst: uint8_t *, dst_size: size_t, glyph_size: size_t *) -> bool { // I believe that n_contours < 65536, in which case this is safe. However, a // comment and/or an assert would be good. - unsigned int flag_offset = kEndPtsOfContoursOffset + 2 * n_contours + 2 + - instruction_length; - int last_flag = -1; - int repeat_count = 0; - int last_x = 0; - int last_y = 0; - unsigned int x_bytes = 0; - unsigned int y_bytes = 0; + var flag_offset: unsigned int; + var last_flag: int; + var repeat_count: int; + var last_x: int; + var last_y: int; + var x_bytes: unsigned int; + var y_bytes: unsigned int; - for (unsigned int i = 0; i < n_points; ++i) { - const Point& point = points[i]; - int flag = point.on_curve ? kGlyfOnCurve : 0; - int dx = point.x - last_x; - int dy = point.y - last_y; + for (var i: unsigned int; i < n_points; ++i) { + var point: const woff2::Point &; + var flag: int; + var dx: int; + var dy: int; if (dx == 0) { flag |= kGlyfThisXIsSame; } else if (dx > -256 && dx < 256) { @@ -254,19 +253,19 @@ fn StorePoints(unsigned int n_points, const Point* points, } dst[flag_offset++] = repeat_count; } - unsigned int xy_bytes = x_bytes + y_bytes; + var xy_bytes: unsigned int; if (PREDICT_FALSE(xy_bytes < x_bytes || flag_offset + xy_bytes < flag_offset || flag_offset + xy_bytes > dst_size)) { return FONT_COMPRESSION_FAILURE(); } - int x_offset = flag_offset; - int y_offset = flag_offset + x_bytes; + var x_offset: int; + var y_offset: int; last_x = 0; last_y = 0; - for (unsigned int i = 0; i < n_points; ++i) { - int dx = points[i].x - last_x; + for (var i: unsigned int; i < n_points; ++i) { + var dx: int; if (dx == 0) { // pass } else if (dx > -256 && dx < 256) { @@ -276,7 +275,7 @@ fn StorePoints(unsigned int n_points, const Point* points, x_offset = Store16(dst, x_offset, dx); } last_x += dx; - int dy = points[i].y - last_y; + var dy: int; if (dy == 0) { // pass } else if (dy > -256 && dy < 256) { @@ -293,11 +292,11 @@ fn StorePoints(unsigned int n_points, const Point* points, // Compute the bounding box of the coordinates, and store into a glyf buffer. // A precondition is that there are at least 10 bytes available. // dst should point to the beginning of a 'glyf' record. -void ComputeBbox(unsigned int n_points, const Point* points, uint8_t* dst) { - int x_min = 0; - int y_min = 0; - int x_max = 0; - int y_max = 0; +void ComputeBbox(n_points: unsigned int, points: const woff2::Point *, dst: uint8_t *) { + var x_min: int; + var y_min: int; + var x_max: int; + var y_max: int; if (n_points > 0) { x_min = points[0].x; @@ -305,15 +304,15 @@ void ComputeBbox(unsigned int n_points, const Point* points, uint8_t* dst) { y_min = points[0].y; y_max = points[0].y; } - for (unsigned int i = 1; i < n_points; ++i) { - int x = points[i].x; - int y = points[i].y; + for (var i: unsigned int; i < n_points; ++i) { + var x: int; + var y: int; x_min = std::min(x, x_min); x_max = std::max(x, x_max); y_min = std::min(y, y_min); y_max = std::max(y, y_max); } - size_t offset = 2; + var offset: size_t; offset = Store16(dst, offset, x_min); offset = Store16(dst, offset, y_min); offset = Store16(dst, offset, x_max); @@ -321,18 +320,18 @@ void ComputeBbox(unsigned int n_points, const Point* points, uint8_t* dst) { } -fn SizeOfComposite(Buffer composite_stream, size_t* size, - bool* have_instructions) -> bool { - size_t start_offset = composite_stream.offset(); - bool we_have_instructions = false; +fn SizeOfComposite(composite_stream: woff2::Buffer, size: size_t *, + have_instructions: bool *) -> bool { + var start_offset: size_t; + var we_have_instructions: bool; - uint16_t flags = FLAG_MORE_COMPONENTS; + var flags: uint16_t; while (flags & FLAG_MORE_COMPONENTS) { if (PREDICT_FALSE(!composite_stream.ReadU16(&flags))) { return FONT_COMPRESSION_FAILURE(); } we_have_instructions |= (flags & FLAG_WE_HAVE_INSTRUCTIONS) != 0; - size_t arg_size = 2; // glyph index + var arg_size: size_t; // glyph index if (flags & FLAG_ARG_1_AND_2_ARE_WORDS) { arg_size += 4; } else { @@ -356,12 +355,12 @@ fn SizeOfComposite(Buffer composite_stream, size_t* size, return true; } -fn Pad4(WOFF2Out* out) -> bool { - uint8_t zeroes[] = {0, 0, 0}; +fn Pad4(out: woff2::WOFF2Out *) -> bool { + var zeroes: uint8_t [3]; if (PREDICT_FALSE(out->Size() + 3 < out->Size())) { return FONT_COMPRESSION_FAILURE(); } - uint32_t pad_bytes = Round4(out->Size()) - out->Size(); + var pad_bytes: uint32_t; if (pad_bytes > 0) { if (PREDICT_FALSE(!out->Write(&zeroes, pad_bytes))) { return FONT_COMPRESSION_FAILURE(); @@ -371,19 +370,19 @@ fn Pad4(WOFF2Out* out) -> bool { } // Build TrueType loca table -fn StoreLoca(const std::vector& loca_values, int index_format, - uint32_t* checksum, WOFF2Out* out) -> bool { +fn StoreLoca(loca_values: const std::vector &, index_format: int, + checksum: uint32_t *, out: woff2::WOFF2Out *) -> bool { // TODO(user) figure out what index format to use based on whether max // offset fits into uint16_t or not - const uint64_t loca_size = loca_values.size(); - const uint64_t offset_size = index_format ? 4 : 2; + var loca_size: const uint64_t; + var offset_size: const uint64_t; if (PREDICT_FALSE((loca_size << 2) >> 2 != loca_size)) { return FONT_COMPRESSION_FAILURE(); } - std::vector loca_content(loca_size * offset_size); - uint8_t* dst = &loca_content[0]; - size_t offset = 0; - for (unsigned int value : loca_values) { + var loca_content: std::vector; + var dst: uint8_t *; + var offset: size_t; + for (unsigned int value var __begin2: std::__wrap_iter var __range2: const std::vector &) { if (index_format) { offset = StoreU32(dst, offset, value); } else { @@ -398,15 +397,15 @@ fn StoreLoca(const std::vector& loca_values, int index_format, } // Reconstruct entire glyf table based on transformed original -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 { - static const int kNumSubStreams = 7; - Buffer file(data, glyf_table->transform_length); - uint32_t version; - std::vector > substreams(kNumSubStreams); - const size_t glyf_start = out->Size(); +fn ReconstructGlyf(data: const uint8_t *, glyf_table: woff2::Table *, + glyf_checksum: uint32_t *, loca_table: woff2::Table *, + loca_checksum: uint32_t *, info: woff2::(anonymous namespace)::WOFF2FontInfo *, + out: woff2::WOFF2Out *) -> bool { + var kNumSubStreams: const int; + var file: woff2::Buffer; + var version: uint32_t; + var substreams: std::vector>; + var glyf_start: const size_t; if (PREDICT_FALSE(!file.ReadU32(&version))) { return FONT_COMPRESSION_FAILURE(); @@ -418,19 +417,18 @@ fn ReconstructGlyf(const uint8_t* data, Table* glyf_table, // https://dev.w3.org/webfonts/WOFF2/spec/#conform-mustRejectLoca // dst_length here is origLength in the spec - uint32_t expected_loca_dst_length = (info->index_format ? 4 : 2) - * (static_cast(info->num_glyphs) + 1); + var expected_loca_dst_length: uint32_t; if (PREDICT_FALSE(loca_table->dst_length != expected_loca_dst_length)) { return FONT_COMPRESSION_FAILURE(); } - unsigned int offset = (2 + kNumSubStreams) * 4; + var offset: unsigned int; if (PREDICT_FALSE(offset > glyf_table->transform_length)) { return FONT_COMPRESSION_FAILURE(); } // Invariant from here on: data_size >= offset - for (int i = 0; i < kNumSubStreams; ++i) { - uint32_t substream_size; + for (var i: int; i < kNumSubStreams; ++i) { + var substream_size: uint32_t; if (PREDICT_FALSE(!file.ReadU32(&substream_size))) { return FONT_COMPRESSION_FAILURE(); } @@ -440,34 +438,34 @@ fn ReconstructGlyf(const uint8_t* data, Table* glyf_table, substreams[i] = std::make_pair(data + offset, substream_size); offset += substream_size; } - Buffer n_contour_stream(substreams[0].first, substreams[0].second); - Buffer n_points_stream(substreams[1].first, substreams[1].second); - Buffer flag_stream(substreams[2].first, substreams[2].second); - Buffer glyph_stream(substreams[3].first, substreams[3].second); - Buffer composite_stream(substreams[4].first, substreams[4].second); - Buffer bbox_stream(substreams[5].first, substreams[5].second); - Buffer instruction_stream(substreams[6].first, substreams[6].second); + var n_contour_stream: woff2::Buffer; + var n_points_stream: woff2::Buffer; + var flag_stream: woff2::Buffer; + var glyph_stream: woff2::Buffer; + var composite_stream: woff2::Buffer; + var bbox_stream: woff2::Buffer; + var instruction_stream: woff2::Buffer; - std::vector loca_values(info->num_glyphs + 1); - std::vector n_points_vec; - std::unique_ptr points; - size_t points_size = 0; - const uint8_t* bbox_bitmap = bbox_stream.buffer(); + var loca_values: std::vector; + var n_points_vec: std::vector; + var points: std::unique_ptr; + var points_size: size_t; + var bbox_bitmap: const uint8_t *; // Safe because num_glyphs is bounded - unsigned int bitmap_length = ((info->num_glyphs + 31) >> 5) << 2; + var bitmap_length: unsigned int; if (!bbox_stream.Skip(bitmap_length)) { return FONT_COMPRESSION_FAILURE(); } // Temp buffer for glyph's. - size_t glyph_buf_size = kDefaultGlyphBuf; - std::unique_ptr glyph_buf(new uint8_t[glyph_buf_size]); + var glyph_buf_size: size_t; + var glyph_buf: std::unique_ptr; info->x_mins.resize(info->num_glyphs); - for (unsigned int i = 0; i < info->num_glyphs; ++i) { - size_t glyph_size = 0; - uint16_t n_contours = 0; - bool have_bbox = false; + for (var i: unsigned int; i < info->num_glyphs; ++i) { + var glyph_size: size_t; + var n_contours: uint16_t; + var have_bbox: bool; if (bbox_bitmap[i >> 3] & (0x80 >> (i & 7))) { have_bbox = true; } @@ -477,14 +475,14 @@ fn ReconstructGlyf(const uint8_t* data, Table* glyf_table, if (n_contours == 0xffff) { // composite glyph - bool have_instructions = false; - unsigned int instruction_size = 0; + var have_instructions: bool; + var instruction_size: unsigned int; if (PREDICT_FALSE(!have_bbox)) { // composite glyphs must have an explicit bbox return FONT_COMPRESSION_FAILURE(); } - size_t composite_size; + var composite_size: size_t; if (PREDICT_FALSE(!SizeOfComposite(composite_stream, &composite_size, &have_instructions))) { return FONT_COMPRESSION_FAILURE(); @@ -495,7 +493,7 @@ fn ReconstructGlyf(const uint8_t* data, Table* glyf_table, } } - size_t size_needed = 12 + composite_size + instruction_size; + var size_needed: size_t; if (PREDICT_FALSE(glyph_buf_size < size_needed)) { glyph_buf.reset(new uint8_t[size_needed]); glyph_buf_size = size_needed; @@ -523,9 +521,9 @@ fn ReconstructGlyf(const uint8_t* data, Table* glyf_table, } else if (n_contours > 0) { // simple glyph n_points_vec.clear(); - unsigned int total_n_points = 0; - unsigned int n_points_contour; - for (unsigned int j = 0; j < n_contours; ++j) { + var total_n_points: unsigned int; + var n_points_contour: unsigned int; + for (var j: unsigned int; j < n_contours; ++j) { if (PREDICT_FALSE( !Read255UShort(&n_points_stream, &n_points_contour))) { return FONT_COMPRESSION_FAILURE(); @@ -536,16 +534,15 @@ fn ReconstructGlyf(const uint8_t* data, Table* glyf_table, } total_n_points += n_points_contour; } - unsigned int flag_size = total_n_points; + var flag_size: unsigned int; if (PREDICT_FALSE( flag_size > flag_stream.length() - flag_stream.offset())) { return FONT_COMPRESSION_FAILURE(); } - const uint8_t* flags_buf = flag_stream.buffer() + flag_stream.offset(); - const uint8_t* triplet_buf = glyph_stream.buffer() + - glyph_stream.offset(); - size_t triplet_size = glyph_stream.length() - glyph_stream.offset(); - size_t triplet_bytes_consumed = 0; + var flags_buf: const uint8_t *; + var triplet_buf: const uint8_t *; + var triplet_size: size_t; + var triplet_bytes_consumed: size_t; if (points_size < total_n_points) { points_size = total_n_points; points = std::make_unique(points_size); @@ -560,7 +557,7 @@ fn ReconstructGlyf(const uint8_t* data, Table* glyf_table, if (PREDICT_FALSE(!glyph_stream.Skip(triplet_bytes_consumed))) { return FONT_COMPRESSION_FAILURE(); } - unsigned int instruction_size; + var instruction_size: unsigned int; if (PREDICT_FALSE(!Read255UShort(&glyph_stream, &instruction_size))) { return FONT_COMPRESSION_FAILURE(); } @@ -569,8 +566,7 @@ fn ReconstructGlyf(const uint8_t* data, Table* glyf_table, || instruction_size >= (1 << 30))) { return FONT_COMPRESSION_FAILURE(); } - size_t size_needed = 12 + 2 * n_contours + 5 * total_n_points - + instruction_size; + var size_needed: size_t; if (PREDICT_FALSE(glyph_buf_size < size_needed)) { glyph_buf.reset(new uint8_t[size_needed]); glyph_buf_size = size_needed; @@ -585,8 +581,8 @@ fn ReconstructGlyf(const uint8_t* data, Table* glyf_table, ComputeBbox(total_n_points, points.get(), glyph_buf.get()); } glyph_size = kEndPtsOfContoursOffset; - int end_point = -1; - for (unsigned int contour_ix = 0; contour_ix < n_contours; ++contour_ix) { + var end_point: int; + for (var contour_ix: unsigned int; contour_ix < n_contours; ++contour_ix) { end_point += n_points_vec[contour_ix]; if (PREDICT_FALSE(end_point >= 65536)) { return FONT_COMPRESSION_FAILURE(); @@ -629,7 +625,7 @@ fn ReconstructGlyf(const uint8_t* data, Table* glyf_table, // We may need x_min to reconstruct 'hmtx' if (n_contours > 0) { - Buffer x_min_buf(glyph_buf.get() + 2, 2); + var x_min_buf: woff2::Buffer; if (PREDICT_FALSE(!x_min_buf.ReadS16(&info->x_mins[i]))) { return FONT_COMPRESSION_FAILURE(); } @@ -650,8 +646,8 @@ fn ReconstructGlyf(const uint8_t* data, Table* glyf_table, return true; } -fn FindTable(std::vector* tables, uint32_t tag) -> Table* { - for (Table* table : *tables) { +fn FindTable(tables: std::vector *, tag: uint32_t) -> Table* { + for (Table* table var __begin2: std::__wrap_iter var __range2: std::vector &) { if (table->tag == tag) { return table; } @@ -660,10 +656,10 @@ fn FindTable(std::vector* tables, uint32_t tag) -> Table* { } // Get numberOfHMetrics, https://www.microsoft.com/typography/otspec/hhea.htm -fn ReadNumHMetrics(const uint8_t* data, size_t data_size, - uint16_t* num_hmetrics) -> bool { +fn ReadNumHMetrics(data: const uint8_t *, data_size: size_t, + num_hmetrics: uint16_t *) -> bool { // Skip 34 to reach 'hhea' numberOfHMetrics - Buffer buffer(data, data_size); + var buffer: woff2::Buffer; if (PREDICT_FALSE(!buffer.Skip(34) || !buffer.ReadU16(num_hmetrics))) { return FONT_COMPRESSION_FAILURE(); } @@ -671,24 +667,24 @@ fn ReadNumHMetrics(const uint8_t* data, size_t data_size, } // http://dev.w3.org/webfonts/WOFF2/spec/Overview.html#hmtx_table_format -fn ReconstructTransformedHmtx(const uint8_t* transformed_buf, - size_t transformed_size, - uint16_t num_glyphs, - uint16_t num_hmetrics, - const std::vector& x_mins, - uint32_t* checksum, - WOFF2Out* out) -> bool { - Buffer hmtx_buff_in(transformed_buf, transformed_size); +fn ReconstructTransformedHmtx(transformed_buf: const uint8_t *, + transformed_size: size_t, + num_glyphs: uint16_t, + num_hmetrics: uint16_t, + x_mins: const std::vector &, + checksum: uint32_t *, + out: woff2::WOFF2Out *) -> bool { + var hmtx_buff_in: woff2::Buffer; - uint8_t hmtx_flags; + var hmtx_flags: uint8_t; if (PREDICT_FALSE(!hmtx_buff_in.ReadU8(&hmtx_flags))) { return FONT_COMPRESSION_FAILURE(); } - std::vector advance_widths; - std::vector lsbs; - bool has_proportional_lsbs = (hmtx_flags & 1) == 0; - bool has_monospace_lsbs = (hmtx_flags & 2) == 0; + var advance_widths: std::vector; + var lsbs: std::vector; + var has_proportional_lsbs: bool; + var has_monospace_lsbs: bool; // Bits 2-7 are reserved and MUST be zero. if ((hmtx_flags & 0xFC) != 0) { @@ -716,16 +712,16 @@ fn ReconstructTransformedHmtx(const uint8_t* transformed_buf, return FONT_COMPRESSION_FAILURE(); } - for (uint16_t i = 0; i < num_hmetrics; i++) { - uint16_t advance_width; + for (var i: uint16_t; i < num_hmetrics; i++) { + var advance_width: uint16_t; if (PREDICT_FALSE(!hmtx_buff_in.ReadU16(&advance_width))) { return FONT_COMPRESSION_FAILURE(); } advance_widths.push_back(advance_width); } - for (uint16_t i = 0; i < num_hmetrics; i++) { - int16_t lsb; + for (var i: uint16_t; i < num_hmetrics; i++) { + var lsb: int16_t; if (has_proportional_lsbs) { if (PREDICT_FALSE(!hmtx_buff_in.ReadS16(&lsb))) { return FONT_COMPRESSION_FAILURE(); @@ -736,8 +732,8 @@ fn ReconstructTransformedHmtx(const uint8_t* transformed_buf, lsbs.push_back(lsb); } - for (uint16_t i = num_hmetrics; i < num_glyphs; i++) { - int16_t lsb; + for (var i: uint16_t; i < num_glyphs; i++) { + var lsb: int16_t; if (has_monospace_lsbs) { if (PREDICT_FALSE(!hmtx_buff_in.ReadS16(&lsb))) { return FONT_COMPRESSION_FAILURE(); @@ -749,11 +745,11 @@ fn ReconstructTransformedHmtx(const uint8_t* transformed_buf, } // bake me a shiny new hmtx table - uint32_t hmtx_output_size = 2 * num_glyphs + 2 * num_hmetrics; - std::vector hmtx_table(hmtx_output_size); - uint8_t* dst = &hmtx_table[0]; - size_t dst_offset = 0; - for (uint32_t i = 0; i < num_glyphs; i++) { + var hmtx_output_size: uint32_t; + var hmtx_table: std::vector; + var dst: uint8_t *; + var dst_offset: size_t; + for (var i: uint32_t; i < num_glyphs; i++) { if (i < num_hmetrics) { Store16(advance_widths[i], &dst_offset, dst); } @@ -768,11 +764,10 @@ fn ReconstructTransformedHmtx(const uint8_t* transformed_buf, return true; } -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( - src_size, src_buf, &uncompressed_size, dst_buf); +fn Woff2Uncompress(dst_buf: uint8_t *, dst_size: size_t, + src_buf: const uint8_t *, src_size: size_t) -> bool { + var uncompressed_size: size_t; + var result: BrotliDecoderResult; if (PREDICT_FALSE(result != BROTLI_DECODER_RESULT_SUCCESS || uncompressed_size != dst_size)) { return FONT_COMPRESSION_FAILURE(); @@ -780,16 +775,16 @@ fn Woff2Uncompress(uint8_t* dst_buf, size_t dst_size, return true; } -fn ReadTableDirectory(Buffer* file, std::vector
* tables, - size_t num_tables) -> bool { - uint32_t src_offset = 0; - for (size_t i = 0; i < num_tables; ++i) { - Table* table = &(*tables)[i]; - uint8_t flag_byte; +fn ReadTableDirectory(file: woff2::Buffer *, tables: std::vector
*, + num_tables: size_t) -> bool { + var src_offset: uint32_t; + for (var i: size_t; i < num_tables; ++i) { + var table: woff2::Table *; + var flag_byte: uint8_t; if (PREDICT_FALSE(!file->ReadU8(&flag_byte))) { return FONT_COMPRESSION_FAILURE(); } - uint32_t tag; + var tag: uint32_t; if ((flag_byte & 0x3f) == 0x3f) { if (PREDICT_FALSE(!file->ReadU32(&tag))) { return FONT_COMPRESSION_FAILURE(); @@ -797,8 +792,8 @@ fn ReadTableDirectory(Buffer* file, std::vector
* tables, } else { tag = kKnownTags[flag_byte & 0x3f]; } - uint32_t flags = 0; - uint8_t xform_version = (flag_byte >> 6) & 0x03; + var flags: uint32_t; + var xform_version: uint8_t; // 0 means xform for glyph/loca, non-0 for others if (tag == kGlyfTableTag || tag == kLocaTableTag) { @@ -810,11 +805,11 @@ fn ReadTableDirectory(Buffer* file, std::vector
* tables, } flags |= xform_version; - uint32_t dst_length; + var dst_length: uint32_t; if (PREDICT_FALSE(!ReadBase128(file, &dst_length))) { return FONT_COMPRESSION_FAILURE(); } - uint32_t transform_length = dst_length; + var transform_length: uint32_t; if ((flags & kWoff2FlagsTransform) != 0) { if (PREDICT_FALSE(!ReadBase128(file, &transform_length))) { return FONT_COMPRESSION_FAILURE(); @@ -839,15 +834,15 @@ fn ReadTableDirectory(Buffer* file, std::vector
* tables, } // Writes a single Offset Table entry -fn StoreOffsetTable(uint8_t* result, size_t offset, uint32_t flavor, - uint16_t num_tables) -> size_t { +fn StoreOffsetTable(result: uint8_t *, offset: size_t, flavor: uint32_t, + num_tables: uint16_t) -> size_t { offset = StoreU32(result, offset, flavor); // sfnt version offset = Store16(result, offset, num_tables); // num_tables - unsigned max_pow2 = 0; + var max_pow2: unsigned int; while (1u << (max_pow2 + 1) <= num_tables) { max_pow2++; } - const uint16_t output_search_range = (1u << max_pow2) << 4; + var output_search_range: const uint16_t; offset = Store16(result, offset, output_search_range); // searchRange offset = Store16(result, offset, max_pow2); // entrySelector // rangeShift @@ -855,7 +850,7 @@ fn StoreOffsetTable(uint8_t* result, size_t offset, uint32_t flavor, return offset; } -fn StoreTableEntry(uint8_t* result, uint32_t offset, uint32_t tag) -> size_t { +fn StoreTableEntry(result: uint8_t *, offset: uint32_t, tag: uint32_t) -> size_t { offset = StoreU32(result, offset, tag); offset = StoreU32(result, offset, 0); offset = StoreU32(result, offset, 0); @@ -864,27 +859,26 @@ fn StoreTableEntry(uint8_t* result, uint32_t offset, uint32_t tag) -> size_t { } // First table goes after all the headers, table directory, etc -fn ComputeOffsetToFirstTable(const WOFF2Header& hdr) -> uint64_t { - uint64_t offset = kSfntHeaderSize + - kSfntEntrySize * static_cast(hdr.num_tables); +fn ComputeOffsetToFirstTable(hdr: const woff2::(anonymous namespace)::WOFF2Header &) -> uint64_t { + var offset: uint64_t; if (hdr.header_version) { offset = CollectionHeaderSize(hdr.header_version, hdr.ttc_fonts.size()) + kSfntHeaderSize * hdr.ttc_fonts.size(); - for (const auto& ttc_font : hdr.ttc_fonts) { + for (const auto& ttc_font var __begin3: std::__wrap_iter var __range3: const std::vector &) { offset += kSfntEntrySize * ttc_font.table_indices.size(); } } return offset; } -fn Tables(WOFF2Header* hdr, size_t font_index) -> std::vector { - std::vector tables; +fn Tables(hdr: woff2::(anonymous namespace)::WOFF2Header *, font_index: size_t) -> std::vector { + var tables: std::vector
; if (PREDICT_FALSE(hdr->header_version)) { - for (auto index : hdr->ttc_fonts[font_index].table_indices) { + for (auto index var __begin3: std::__wrap_iter var __range3: std::vector &) { tables.push_back(&hdr->tables[index]); } } else { - for (auto& table : hdr->tables) { + for (auto& table var __begin3: std::__wrap_iter var __range3: std::vector &) { tables.push_back(&table); } } @@ -893,20 +887,20 @@ fn Tables(WOFF2Header* hdr, size_t font_index) -> std::vector { // 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) -fn ReconstructFont(uint8_t* transformed_buf, - const uint32_t transformed_buf_size, - RebuildMetadata* metadata, - WOFF2Header* hdr, - size_t font_index, - WOFF2Out* out) -> bool { - size_t dest_offset = out->Size(); - uint8_t table_entry[12]; - WOFF2FontInfo* info = &metadata->font_infos[font_index]; - std::vector tables = Tables(hdr, font_index); +fn ReconstructFont(transformed_buf: uint8_t *, + transformed_buf_size: const uint32_t, + metadata: woff2::(anonymous namespace)::RebuildMetadata *, + hdr: woff2::(anonymous namespace)::WOFF2Header *, + font_index: size_t, + out: woff2::WOFF2Out *) -> bool { + var dest_offset: size_t; + var table_entry: uint8_t [12]; + var info: woff2::(anonymous namespace)::WOFF2FontInfo *; + var tables: std::vector
; // 'glyf' without 'loca' doesn't make sense - const Table* glyf_table = FindTable(&tables, kGlyfTableTag); - const Table* loca_table = FindTable(&tables, kLocaTableTag); + var glyf_table: const woff2::Table *; + var loca_table: const woff2::Table *; if (PREDICT_FALSE(static_cast(glyf_table) != static_cast(loca_table))) { #ifdef FONT_COMPRESSION_BIN @@ -925,18 +919,17 @@ fn ReconstructFont(uint8_t* transformed_buf, } } - uint32_t font_checksum = metadata->header_checksum; + var font_checksum: uint32_t; if (hdr->header_version) { font_checksum = hdr->ttc_fonts[font_index].header_checksum; } - uint32_t loca_checksum = 0; - for (size_t i = 0; i < tables.size(); i++) { - Table& table = *tables[i]; + var loca_checksum: uint32_t; + for (var i: size_t; i < tables.size(); i++) { + var table: woff2::Table &; - std::pair checksum_key = {table.tag, table.src_offset}; - bool reused = metadata->checksums.find(checksum_key) - != metadata->checksums.end(); + var checksum_key: std::pair; + var reused: bool; if (PREDICT_FALSE(font_index == 0 && reused)) { return FONT_COMPRESSION_FAILURE(); } @@ -955,7 +948,7 @@ fn ReconstructFont(uint8_t* transformed_buf, } } - uint32_t checksum = 0; + var checksum: uint32_t; if (!reused) { if ((table.flags & kWoff2FlagsTransform) != kWoff2FlagsTransform) { if (table.tag == kHeadTableTag) { @@ -976,7 +969,7 @@ fn ReconstructFont(uint8_t* transformed_buf, if (table.tag == kGlyfTableTag) { table.dst_offset = dest_offset; - Table* loca_table = FindTable(&tables, kLocaTableTag); + var loca_table: woff2::Table *; if (PREDICT_FALSE(!ReconstructGlyf(transformed_buf + table.src_offset, &table, &checksum, loca_table, &loca_checksum, info, out))) { return FONT_COMPRESSION_FAILURE(); @@ -1027,12 +1020,12 @@ fn ReconstructFont(uint8_t* transformed_buf, } // Update 'head' checkSumAdjustment. We already set it to 0 and summed font. - Table* head_table = FindTable(&tables, kHeadTableTag); + var head_table: woff2::Table *; if (head_table) { if (PREDICT_FALSE(head_table->dst_length < 12)) { return FONT_COMPRESSION_FAILURE(); } - uint8_t checksum_adjustment[4]; + var checksum_adjustment: uint8_t [4]; StoreU32(checksum_adjustment, 0, 0xB1B0AFBA - font_checksum); if (PREDICT_FALSE(!out->Write(checksum_adjustment, head_table->dst_offset + 8, 4))) { @@ -1043,10 +1036,10 @@ fn ReconstructFont(uint8_t* transformed_buf, return true; } -fn ReadWOFF2Header(const uint8_t* data, size_t length, WOFF2Header* hdr) -> bool { - Buffer file(data, length); +fn ReadWOFF2Header(data: const uint8_t *, length: size_t, hdr: woff2::(anonymous namespace)::WOFF2Header *) -> bool { + var file: woff2::Buffer; - uint32_t signature; + var signature: uint32_t; if (PREDICT_FALSE(!file.ReadU32(&signature) || signature != kWoff2Signature || !file.ReadU32(&hdr->flavor))) { return FONT_COMPRESSION_FAILURE(); @@ -1054,7 +1047,7 @@ fn ReadWOFF2Header(const uint8_t* data, size_t length, WOFF2Header* hdr) -> bool // TODO(user): Should call IsValidVersionTag() here. - uint32_t reported_length; + var reported_length: uint32_t; if (PREDICT_FALSE( !file.ReadU32(&reported_length) || length != reported_length)) { return FONT_COMPRESSION_FAILURE(); @@ -1077,9 +1070,9 @@ fn ReadWOFF2Header(const uint8_t* data, size_t length, WOFF2Header* hdr) -> bool if (PREDICT_FALSE(!file.Skip(2 * 2))) { return FONT_COMPRESSION_FAILURE(); } - uint32_t meta_offset; - uint32_t meta_length; - uint32_t meta_length_orig; + var meta_offset: uint32_t; + var meta_length: uint32_t; + var meta_length_orig: uint32_t; if (PREDICT_FALSE(!file.ReadU32(&meta_offset) || !file.ReadU32(&meta_length) || !file.ReadU32(&meta_length_orig))) { @@ -1091,8 +1084,8 @@ fn ReadWOFF2Header(const uint8_t* data, size_t length, WOFF2Header* hdr) -> bool return FONT_COMPRESSION_FAILURE(); } } - uint32_t priv_offset; - uint32_t priv_length; + var priv_offset: uint32_t; + var priv_length: uint32_t; if (PREDICT_FALSE(!file.ReadU32(&priv_offset) || !file.ReadU32(&priv_length))) { return FONT_COMPRESSION_FAILURE(); @@ -1110,7 +1103,7 @@ fn ReadWOFF2Header(const uint8_t* data, size_t length, WOFF2Header* hdr) -> bool } // Before we sort for output the last table end is the uncompressed size. - Table& last_table = hdr->tables.back(); + var last_table: woff2::Table &; hdr->uncompressed_size = last_table.src_offset + last_table.src_length; if (PREDICT_FALSE(hdr->uncompressed_size < last_table.src_offset)) { return FONT_COMPRESSION_FAILURE(); @@ -1126,15 +1119,15 @@ fn ReadWOFF2Header(const uint8_t* data, size_t length, WOFF2Header* hdr) -> bool && hdr->header_version != 0x00020000)) { return FONT_COMPRESSION_FAILURE(); } - uint32_t num_fonts; + var num_fonts: uint32_t; if (PREDICT_FALSE(!Read255UShort(&file, &num_fonts) || !num_fonts)) { return FONT_COMPRESSION_FAILURE(); } hdr->ttc_fonts.resize(num_fonts); - for (uint32_t i = 0; i < num_fonts; i++) { - TtcFont& ttc_font = hdr->ttc_fonts[i]; - uint32_t num_tables; + for (var i: uint32_t; i < num_fonts; i++) { + var ttc_font: woff2::(anonymous namespace)::TtcFont &; + var num_tables: uint32_t; if (PREDICT_FALSE(!Read255UShort(&file, &num_tables) || !num_tables)) { return FONT_COMPRESSION_FAILURE(); } @@ -1145,18 +1138,18 @@ fn ReadWOFF2Header(const uint8_t* data, size_t length, WOFF2Header* hdr) -> bool ttc_font.table_indices.resize(num_tables); - unsigned int glyf_idx = 0; - unsigned int loca_idx = 0; + var glyf_idx: unsigned int; + var loca_idx: unsigned int; - for (uint32_t j = 0; j < num_tables; j++) { - unsigned int table_idx; + for (var j: uint32_t; j < num_tables; j++) { + var table_idx: unsigned int; if (PREDICT_FALSE(!Read255UShort(&file, &table_idx)) || table_idx >= hdr->tables.size()) { return FONT_COMPRESSION_FAILURE(); } ttc_font.table_indices[j] = table_idx; - const Table& table = hdr->tables[table_idx]; + var table: const woff2::Table &; if (table.tag == kLocaTableTag) { loca_idx = table_idx; } @@ -1179,15 +1172,15 @@ fn ReadWOFF2Header(const uint8_t* data, size_t length, WOFF2Header* hdr) -> bool } } - const uint64_t first_table_offset = ComputeOffsetToFirstTable(*hdr); + var first_table_offset: const uint64_t; hdr->compressed_offset = file.offset(); if (PREDICT_FALSE(hdr->compressed_offset > std::numeric_limits::max())) { return FONT_COMPRESSION_FAILURE(); } - uint64_t src_offset = Round4(hdr->compressed_offset + hdr->compressed_length); - uint64_t dst_offset = first_table_offset; + var src_offset: uint64_t; + var dst_offset: uint64_t; if (PREDICT_FALSE(src_offset > length)) { @@ -1226,21 +1219,21 @@ fn ReadWOFF2Header(const uint8_t* data, size_t length, WOFF2Header* hdr) -> bool } // Write everything before the actual table data -fn WriteHeaders(RebuildMetadata* metadata, - WOFF2Header* hdr, WOFF2Out* out) -> bool { - std::vector output(ComputeOffsetToFirstTable(*hdr), 0); +fn WriteHeaders(metadata: woff2::(anonymous namespace)::RebuildMetadata *, + hdr: woff2::(anonymous namespace)::WOFF2Header *, out: woff2::WOFF2Out *) -> bool { + var output: std::vector; // Re-order tables in output (OTSpec) order - std::vector
sorted_tables(hdr->tables); + var sorted_tables: std::vector
; if (hdr->header_version) { // collection; we have to sort the table offset vector in each font - for (auto& ttc_font : hdr->ttc_fonts) { - std::map sorted_index_by_tag; - for (auto table_index : ttc_font.table_indices) { + for (auto& ttc_font var __begin3: std::__wrap_iter var __range3: std::vector &) { + var sorted_index_by_tag: std::map; + for (auto table_index var __begin4: std::__wrap_iter var __range4: std::vector &) { sorted_index_by_tag[hdr->tables[table_index].tag] = table_index; } - uint16_t index = 0; - for (auto& i : sorted_index_by_tag) { + var index: uint16_t; + for (auto& i var __begin4: std::__map_iterator, std::__tree_node, void *> *, long>> var __range4: std::map &) { ttc_font.table_indices[index++] = i.second; } } @@ -1250,16 +1243,16 @@ fn WriteHeaders(RebuildMetadata* metadata, } // Start building the font - uint8_t* result = &output[0]; - size_t offset = 0; + var result: uint8_t *; + var offset: size_t; if (hdr->header_version) { // TTC header offset = StoreU32(result, offset, hdr->flavor); // TAG TTCTag offset = StoreU32(result, offset, hdr->header_version); // FIXED Version offset = StoreU32(result, offset, hdr->ttc_fonts.size()); // ULONG numFonts // Space for ULONG OffsetTable[numFonts] (zeroed initially) - size_t offset_table = offset; // keep start of offset table for later - for (size_t i = 0; i < hdr->ttc_fonts.size(); i++) { + var offset_table: size_t; // keep start of offset table for later + for (var i: size_t; i < hdr->ttc_fonts.size(); i++) { offset = StoreU32(result, offset, 0); // will fill real values in later } // space for DSIG fields for header v2 @@ -1271,8 +1264,8 @@ fn WriteHeaders(RebuildMetadata* metadata, // write Offset Tables and store the location of each in TTC Header metadata->font_infos.resize(hdr->ttc_fonts.size()); - for (size_t i = 0; i < hdr->ttc_fonts.size(); i++) { - TtcFont& ttc_font = hdr->ttc_fonts[i]; + for (var i: size_t; i < hdr->ttc_fonts.size(); i++) { + var ttc_font: woff2::(anonymous namespace)::TtcFont &; // write Offset Table location into TTC Header offset_table = StoreU32(result, offset_table, offset); @@ -1282,8 +1275,8 @@ fn WriteHeaders(RebuildMetadata* metadata, offset = StoreOffsetTable(result, offset, ttc_font.flavor, ttc_font.table_indices.size()); - for (const auto table_index : ttc_font.table_indices) { - uint32_t tag = hdr->tables[table_index].tag; + for (const auto table_index var __begin4: std::__wrap_iter var __range4: std::vector &) { + var tag: uint32_t; metadata->font_infos[i].table_entry_by_tag[tag] = offset; offset = StoreTableEntry(result, offset, tag); } @@ -1294,7 +1287,7 @@ fn WriteHeaders(RebuildMetadata* metadata, } else { metadata->font_infos.resize(1); offset = StoreOffsetTable(result, offset, hdr->flavor, hdr->num_tables); - for (uint16_t i = 0; i < hdr->num_tables; ++i) { + for (var i: uint16_t; i < hdr->num_tables; ++i) { metadata->font_infos[0].table_entry_by_tag[sorted_tables[i].tag] = offset; offset = StoreTableEntry(result, offset, sorted_tables[i].tag); } @@ -1309,9 +1302,9 @@ fn WriteHeaders(RebuildMetadata* metadata, } // namespace -fn ComputeWOFF2FinalSize(const uint8_t* data, size_t length) -> size_t { - Buffer file(data, length); - uint32_t total_length; +fn ComputeWOFF2FinalSize(data: const uint8_t *, length: size_t) -> size_t { + var file: woff2::Buffer; + var total_length: uint32_t; if (!file.Skip(16) || !file.ReadU32(&total_length)) { @@ -1320,16 +1313,16 @@ fn ComputeWOFF2FinalSize(const uint8_t* data, size_t length) -> size_t { return total_length; } -fn ConvertWOFF2ToTTF(uint8_t *result, size_t result_length, - const uint8_t *data, size_t length) -> bool { - WOFF2MemoryOut out(result, result_length); +fn ConvertWOFF2ToTTF(result: uint8_t *, result_length: size_t, + data: const uint8_t *, length: size_t) -> bool { + var out: woff2::WOFF2MemoryOut; return ConvertWOFF2ToTTF(data, length, &out); } -fn ConvertWOFF2ToTTF(const uint8_t* data, size_t length, - WOFF2Out* out) -> bool { - RebuildMetadata metadata; - WOFF2Header hdr; +fn ConvertWOFF2ToTTF(data: const uint8_t *, length: size_t, + out: woff2::WOFF2Out *) -> bool { + var metadata: woff2::(anonymous namespace)::RebuildMetadata; + var hdr: woff2::(anonymous namespace)::WOFF2Header; if (!ReadWOFF2Header(data, length, &hdr)) { return FONT_COMPRESSION_FAILURE(); } @@ -1338,7 +1331,7 @@ fn ConvertWOFF2ToTTF(const uint8_t* data, size_t length, return FONT_COMPRESSION_FAILURE(); } - const float compression_ratio = static_cast(hdr.uncompressed_size) / length; + var compression_ratio: const float; if (compression_ratio > kMaxPlausibleCompressionRatio) { #ifdef FONT_COMPRESSION_BIN fprintf(stderr, "Implausible compression ratio %.01f\n", compression_ratio); @@ -1346,8 +1339,8 @@ fn ConvertWOFF2ToTTF(const uint8_t* data, size_t length, return FONT_COMPRESSION_FAILURE(); } - const uint8_t* src_buf = data + hdr.compressed_offset; - std::vector uncompressed_buf(hdr.uncompressed_size); + var src_buf: const uint8_t *; + var uncompressed_buf: std::vector; if (PREDICT_FALSE(hdr.uncompressed_size < 1)) { return FONT_COMPRESSION_FAILURE(); } @@ -1357,7 +1350,7 @@ fn ConvertWOFF2ToTTF(const uint8_t* data, size_t length, return FONT_COMPRESSION_FAILURE(); } - for (size_t i = 0; i < metadata.font_infos.size(); i++) { + for (var i: size_t; i < metadata.font_infos.size(); i++) { if (PREDICT_FALSE(!ReconstructFont(&uncompressed_buf[0], hdr.uncompressed_size, &metadata, &hdr, i, out))) { diff --git a/third_party/examples/woff2/carbon/src/woff2_decompress.impl.carbon b/third_party/examples/woff2/carbon/src/woff2_decompress.impl.carbon index d3e9184203fd..d317cbd96c07 100644 --- a/third_party/examples/woff2/carbon/src/woff2_decompress.impl.carbon +++ b/third_party/examples/woff2/carbon/src/woff2_decompress.impl.carbon @@ -13,7 +13,7 @@ #include -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(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(), diff --git a/third_party/examples/woff2/carbon/src/woff2_enc.impl.carbon b/third_party/examples/woff2/carbon/src/woff2_enc.impl.carbon index 94c1a1c1d234..0c82ac54121c 100644 --- a/third_party/examples/woff2/carbon/src/woff2_enc.impl.carbon +++ b/third_party/examples/woff2/carbon/src/woff2_enc.impl.carbon @@ -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
& tables, - std::map, 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
&, + index_by_tag_offset: std::map, 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 var __range2: const std::vector &) { 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 var __range3: const std::vector &) { 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_node, void *> *, long>> var __range4: const std::map &) { + var table: const Font::Table &; // no collection entry for xform table if (table.tag & 0x80808080) { continue; } - std::pair tag_offset(table.tag, table.offset); - uint16_t table_index = index_by_tag_offset[tag_offset]; + var tag_offset: std::pair; + 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_node, void *> *, long>> var __range2: const std::map &) { + 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 var __range2: const std::vector &) { 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_node, void *> *, long>> var __range2: const std::map &) { + 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 var __range1: std::vector &) { 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 var __range2: std::vector &) { + 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 var __range1: std::vector &) { total_transform_length += ComputeTotalTransformLength(font); } - size_t compression_buffer_size = CompressedBufferSize(total_transform_length); - std::vector compression_buf(compression_buffer_size); - uint32_t total_compressed_length = compression_buffer_size; + var compression_buffer_size: size_t; + var compression_buf: std::vector; + var total_compressed_length: uint32_t; // Collect all transformed data into one place in output order. - std::vector 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; + var transform_offset: size_t; + for (const auto& font var __begin1: std::__wrap_iter var __range1: std::vector &) { + for (const auto tag var __begin2: std::__wrap_iter var __range2: std::vector &&) { + 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 compressed_metadata_buf(compressed_metadata_buf_length); + var compressed_metadata_buf_length: uint32_t; + var compressed_metadata_buf: std::vector; if (params.extended_metadata.length() > 0) { if (!TextCompress(reinterpret_cast(params.extended_metadata.data()), @@ -320,32 +317,31 @@ fn ConvertTTFToWOFF2(const uint8_t *data, size_t length, compressed_metadata_buf_length = 0; } - std::vector
tables; - std::map, uint16_t> index_by_tag_offset; + var tables: std::vector
; + var index_by_tag_offset: std::map, uint16_t>; - for (const auto& font : font_collection.fonts) { + for (const auto& font var __begin1: std::__wrap_iter var __range1: std::vector &) { - for (const auto tag : font.OutputOrderedTags()) { - const Font::Table& src_table = font.tables.at(tag); + for (const auto tag var __begin2: std::__wrap_iter var __range2: std::vector &&) { + var src_table: const Font::Table &; if (src_table.IsReused()) { continue; } - std::pair tag_offset(src_table.tag, src_table.offset); + var tag_offset: std::pair; 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 var __range1: std::vector &) { 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 var __range2: std::vector &) { - 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_node, void *> *, long>> var __range3: const std::map &) { + 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_node, void *> *, long>> var __range3: const std::map &) { + 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 tag_offset(table.tag, table_offset); + var table_offset: uint32_t; + var table_length: uint32_t; + var tag_offset: std::pair; 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); } diff --git a/third_party/examples/woff2/carbon/src/woff2_info.impl.carbon b/third_party/examples/woff2/carbon/src/woff2_info.impl.carbon index c0bdeb773928..3e0199bfca6d 100644 --- a/third_party/examples/woff2/carbon/src/woff2_info.impl.carbon +++ b/third_party/examples/woff2/carbon/src/woff2_info.impl.carbon @@ -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((tag >> 24) & 0xFF), - static_cast((tag >> 16) & 0xFF), - static_cast((tag >> 8) & 0xFF), - static_cast(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(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 table_tags; + var table_tags: std::vector; 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; diff --git a/third_party/examples/woff2/carbon/src/woff2_out.impl.carbon b/third_party/examples/woff2/carbon/src/woff2_out.impl.carbon index e69f4983c1ba..2a07a39528c0 100644 --- a/third_party/examples/woff2/carbon/src/woff2_out.impl.carbon +++ b/third_party/examples/woff2/carbon/src/woff2_out.impl.carbon @@ -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; }