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