OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // A very simple driver program while sanitises the file given as argv[1] and | 5 // A very simple driver program while sanitises the file given as argv[1] and |
6 // writes the sanitised version to stdout. | 6 // writes the sanitised version to stdout. |
7 | 7 |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #if defined(_WIN32) | 10 #if defined(_WIN32) |
11 #include <io.h> | 11 #include <io.h> |
12 #else | 12 #else |
13 #include <unistd.h> | 13 #include <unistd.h> |
14 #endif // defined(_WIN32) | 14 #endif // defined(_WIN32) |
15 | 15 |
| 16 #include <cstdarg> |
16 #include <cstdio> | 17 #include <cstdio> |
17 #include <cstdlib> | 18 #include <cstdlib> |
18 | 19 |
19 #include "file-stream.h" | 20 #include "file-stream.h" |
20 #include "opentype-sanitiser.h" | 21 #include "opentype-sanitiser.h" |
21 | 22 |
22 #if defined(_WIN32) | 23 #if defined(_WIN32) |
23 #define ADDITIONAL_OPEN_FLAGS O_BINARY | 24 #define ADDITIONAL_OPEN_FLAGS O_BINARY |
24 #else | 25 #else |
25 #define ADDITIONAL_OPEN_FLAGS 0 | 26 #define ADDITIONAL_OPEN_FLAGS 0 |
26 #endif | 27 #endif |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 int Usage(const char *argv0) { | 31 int Usage(const char *argv0) { |
31 std::fprintf(stderr, "Usage: %s ttf_file > dest_ttf_file\n", argv0); | 32 std::fprintf(stderr, "Usage: %s ttf_file [dest_ttf_file]\n", argv0); |
32 return 1; | 33 return 1; |
33 } | 34 } |
34 | 35 |
| 36 class Context: public ots::OTSContext { |
| 37 public: |
| 38 virtual void Message(int level, const char *format, ...) { |
| 39 va_list va; |
| 40 |
| 41 if (level == 0) |
| 42 std::fprintf(stderr, "ERROR: "); |
| 43 else |
| 44 std::fprintf(stderr, "WARNING: "); |
| 45 va_start(va, format); |
| 46 std::vfprintf(stderr, format, va); |
| 47 std::fprintf(stderr, "\n"); |
| 48 va_end(va); |
| 49 } |
| 50 |
| 51 virtual ots::TableAction GetTableAction(uint32_t tag) { |
| 52 #define TAG(a, b, c, d) ((a) << 24 | (b) << 16 | (c) << 8 | (d)) |
| 53 switch (tag) { |
| 54 case TAG('S','i','l','f'): |
| 55 case TAG('S','i','l','l'): |
| 56 case TAG('G','l','o','c'): |
| 57 case TAG('G','l','a','t'): |
| 58 case TAG('F','e','a','t'): |
| 59 return ots::TABLE_ACTION_PASSTHRU; |
| 60 default: |
| 61 return ots::TABLE_ACTION_DEFAULT; |
| 62 } |
| 63 #undef TAG |
| 64 } |
| 65 }; |
| 66 |
35 } // namespace | 67 } // namespace |
36 | 68 |
37 int main(int argc, char **argv) { | 69 int main(int argc, char **argv) { |
38 if (argc != 2) return Usage(argv[0]); | 70 if (argc < 2 || argc > 3) return Usage(argv[0]); |
39 if (::isatty(1)) return Usage(argv[0]); | |
40 | 71 |
41 ots::EnableWOFF2(); | 72 ots::EnableWOFF2(); |
42 | 73 |
43 const int fd = ::open(argv[1], O_RDONLY | ADDITIONAL_OPEN_FLAGS); | 74 const int fd = ::open(argv[1], O_RDONLY | ADDITIONAL_OPEN_FLAGS); |
44 if (fd < 0) { | 75 if (fd < 0) { |
45 ::perror("open"); | 76 ::perror("open"); |
46 return 1; | 77 return 1; |
47 } | 78 } |
48 | 79 |
49 struct stat st; | 80 struct stat st; |
50 ::fstat(fd, &st); | 81 ::fstat(fd, &st); |
51 | 82 |
52 uint8_t *data = new uint8_t[st.st_size]; | 83 uint8_t *data = new uint8_t[st.st_size]; |
53 if (::read(fd, data, st.st_size) != st.st_size) { | 84 if (::read(fd, data, st.st_size) != st.st_size) { |
54 ::perror("read"); | 85 ::perror("read"); |
55 return 1; | 86 return 1; |
56 } | 87 } |
57 ::close(fd); | 88 ::close(fd); |
58 | 89 |
59 ots::FILEStream output(stdout); | 90 Context context; |
60 #if defined(_WIN32) | 91 |
61 ::setmode(fileno(stdout), O_BINARY); | 92 FILE* out = NULL; |
62 #endif | 93 if (argc == 3) |
63 const bool result = ots::Process(&output, data, st.st_size); | 94 out = fopen(argv[2], "wb"); |
| 95 |
| 96 ots::FILEStream output(out); |
| 97 const bool result = context.Process(&output, data, st.st_size); |
64 | 98 |
65 if (!result) { | 99 if (!result) { |
66 std::fprintf(stderr, "Failed to sanitise file!\n"); | 100 std::fprintf(stderr, "Failed to sanitise file!\n"); |
67 } | 101 } |
68 return !result; | 102 return !result; |
69 } | 103 } |
OLD | NEW |