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.
15 #include "opentype-sanitiser.h"
16 #include "ots-memory-stream.h"
20 int Usage(const char *argv0
) {
21 std::fprintf(stderr
, "Usage: %s <ttf file>\n", argv0
);
27 int main(int argc
, char **argv
) {
28 if (argc
!= 2) return Usage(argv
[0]);
30 const int fd
= ::open(argv
[1], O_RDONLY
);
39 uint8_t *data
= new uint8_t[st
.st_size
];
40 if (::read(fd
, data
, st
.st_size
) != st
.st_size
) {
41 std::fprintf(stderr
, "Failed to read file!\n");
45 // A transcoded font is usually smaller than an original font.
46 // However, it can be slightly bigger than the original one due to
47 // name table replacement and/or padding for glyf table.
48 static const size_t kPadLen
= 20 * 1024;
49 uint8_t *result
= new uint8_t[st
.st_size
+ kPadLen
];
52 if (st
.st_size
< 1024 * 1024) {
55 if (st
.st_size
< 1024 * 100) {
59 struct timeval start
, end
, elapsed
;
60 ::gettimeofday(&start
, 0);
61 for (int i
= 0; i
< num_repeat
; ++i
) {
62 ots::MemoryStream
output(result
, st
.st_size
+ kPadLen
);
63 ots::OTSContext context
;
64 bool r
= context
.Process(&output
, data
, st
.st_size
);
66 std::fprintf(stderr
, "Failed to sanitise file!\n");
70 ::gettimeofday(&end
, 0);
71 timersub(&end
, &start
, &elapsed
);
74 = ((elapsed
.tv_sec
* 1000 * 1000) + elapsed
.tv_usec
) / num_repeat
;
75 std::fprintf(stderr
, "%llu [us] %s (%llu bytes, %llu [byte/us])\n",
76 us
, argv
[1], static_cast<long long>(st
.st_size
),
77 (us
? st
.st_size
/ us
: 0));