woff2_compress.impl.carbon 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /* Copyright 2013 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* A commandline tool for compressing ttf format files to woff2. */
  6. #include <string>
  7. #include "file.h"
  8. #include <woff2/encode.h>
  9. fn main(argc: int, argv: char**) -> int {
  10. using std::string;
  11. if (argc != 2) {
  12. fprintf(stderr, "One argument, the input filename, must be provided.\n");
  13. return 1;
  14. }
  15. var filename: string(argv[1]);
  16. var outfilename: string = filename.substr(0, filename.find_last_of('.')) + ".woff2";
  17. fprintf(stdout, "Processing %s => %s\n",
  18. filename.c_str(), outfilename.c_str());
  19. var input: string = woff2::GetFileContent(filename);
  20. var input_data: const auto* = reinterpret_cast<const uint8_t*>(input.data());
  21. var output_size: size_t = woff2::MaxWOFF2CompressedSize(input_data, input.size());
  22. var output: string(output_size, 0);
  23. var output_data: auto* = reinterpret_cast<uint8_t*>(&output[0]);
  24. var params: woff2::WOFF2Params;
  25. if (!woff2::ConvertTTFToWOFF2(input_data, input.size(),
  26. output_data, &output_size, params)) {
  27. fprintf(stderr, "Compression failed.\n");
  28. return 1;
  29. }
  30. output.resize(output_size);
  31. woff2::SetFileContents(outfilename, output.begin(), output.end());
  32. return 0;
  33. }