Home | History | Annotate | Download | only in test
      1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include <fcntl.h>
      6 #include <sys/stat.h>
      7 #include <sys/time.h>  // for timersub macro.
      8 #include <unistd.h>
      9 #include <time.h>
     10 
     11 #include <cstdio>
     12 #include <cstdlib>
     13 #include <cstring>
     14 
     15 #include "opentype-sanitiser.h"
     16 #include "ots-memory-stream.h"
     17 
     18 namespace {
     19 
     20 int Usage(const char *argv0) {
     21   std::fprintf(stderr, "Usage: %s <ttf file>\n", argv0);
     22   return 1;
     23 }
     24 
     25 }  // namespace
     26 
     27 int main(int argc, char **argv) {
     28   ots::DisableDebugOutput();  // turn off ERROR and WARNING outputs.
     29 
     30   if (argc != 2) return Usage(argv[0]);
     31 
     32   const int fd = ::open(argv[1], O_RDONLY);
     33   if (fd < 0) {
     34     ::perror("open");
     35     return 1;
     36   }
     37 
     38   struct stat st;
     39   ::fstat(fd, &st);
     40 
     41   uint8_t *data = new uint8_t[st.st_size];
     42   if (::read(fd, data, st.st_size) != st.st_size) {
     43     std::fprintf(stderr, "Failed to read file!\n");
     44     return 1;
     45   }
     46 
     47   // A transcoded font is usually smaller than an original font.
     48   // However, it can be slightly bigger than the original one due to
     49   // name table replacement and/or padding for glyf table.
     50   static const size_t kPadLen = 20 * 1024;
     51   uint8_t *result = new uint8_t[st.st_size + kPadLen];
     52 
     53   int num_repeat = 250;
     54   if (st.st_size < 1024 * 1024) {
     55     num_repeat = 2500;
     56   }
     57   if (st.st_size < 1024 * 100) {
     58     num_repeat = 5000;
     59   }
     60 
     61   struct timeval start, end, elapsed;
     62   ::gettimeofday(&start, 0);
     63   for (int i = 0; i < num_repeat; ++i) {
     64     ots::MemoryStream output(result, st.st_size + kPadLen);
     65     bool r = ots::Process(&output, data, st.st_size);
     66     if (!r) {
     67       std::fprintf(stderr, "Failed to sanitise file!\n");
     68       return 1;
     69     }
     70   }
     71   ::gettimeofday(&end, 0);
     72   timersub(&end, &start, &elapsed);
     73 
     74   long long unsigned us
     75       = ((elapsed.tv_sec * 1000 * 1000) + elapsed.tv_usec) / num_repeat;
     76   std::fprintf(stderr, "%llu [us] %s (%llu bytes, %llu [byte/us])\n",
     77                us, argv[1], static_cast<long long>(st.st_size),
     78                (us ? st.st_size / us : 0));
     79 
     80   return 0;
     81 }
     82