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 #ifndef OTS_H_ | 5 #ifndef OTS_H_ |
6 #define OTS_H_ | 6 #define OTS_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <cstdarg> | 9 #include <cstdarg> |
10 #include <cstddef> | 10 #include <cstddef> |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 namespace ots { | 23 namespace ots { |
24 | 24 |
25 #if defined(_MSC_VER) || !defined(OTS_DEBUG) | 25 #if defined(_MSC_VER) || !defined(OTS_DEBUG) |
26 #define OTS_FAILURE() false | 26 #define OTS_FAILURE() false |
27 #else | 27 #else |
28 #define OTS_FAILURE() ots::Failure(__FILE__, __LINE__, __PRETTY_FUNCTION__) | 28 #define OTS_FAILURE() ots::Failure(__FILE__, __LINE__, __PRETTY_FUNCTION__) |
29 bool Failure(const char *f, int l, const char *fn); | 29 bool Failure(const char *f, int l, const char *fn); |
30 #endif | 30 #endif |
31 | 31 |
32 #if defined(_MSC_VER) | 32 // All OTS_FAILURE_* macros ultimately evaluate to 'false', just like the origin
al |
33 // MSVC supports C99 style variadic macros. | 33 // message-less OTS_FAILURE(), so that the current parser will return 'false' as |
34 #define OTS_WARNING(format, ...) | 34 // its result (indicating a failure). |
| 35 |
| 36 #if defined(_MSC_VER) || !defined(OTS_DEBUG) |
| 37 #define OTS_MESSAGE_(level,otf_,...) \ |
| 38 (otf_)->context->Message(level,__VA_ARGS__) |
35 #else | 39 #else |
36 // GCC | 40 #define OTS_MESSAGE_(level,otf_,...) \ |
37 #if defined(OTS_DEBUG) | 41 OTS_FAILURE(), \ |
38 #define OTS_WARNING(format, args...) \ | 42 (otf_)->context->Message(level,__VA_ARGS__) |
39 ots::Warning(__FILE__, __LINE__, format, ##args) | |
40 void Warning(const char *f, int l, const char *format, ...) | |
41 __attribute__((format(printf, 3, 4))); | |
42 #else | |
43 #define OTS_WARNING(format, args...) | |
44 #endif | |
45 #endif | 43 #endif |
46 | 44 |
47 // Define OTS_NO_TRANSCODE_HINTS (i.e., g++ -DOTS_NO_TRANSCODE_HINTS) if you | 45 // Generate a simple message |
48 // want to omit TrueType hinting instructions and variables in glyf, fpgm, prep, | 46 #define OTS_FAILURE_MSG_(otf_,...) \ |
49 // and cvt tables. | 47 (OTS_MESSAGE_(0,otf_,__VA_ARGS__), false) |
50 #if defined(OTS_NO_TRANSCODE_HINTS) | 48 |
51 const bool g_transcode_hints = false; | 49 #define OTS_WARNING_MSG_(otf_,...) \ |
52 #else | 50 OTS_MESSAGE_(1,otf_,__VA_ARGS__) |
53 const bool g_transcode_hints = true; | 51 |
54 #endif | 52 // Generate a message with an associated table tag |
| 53 #define OTS_FAILURE_MSG_TAG_(otf_,msg_,tag_) \ |
| 54 (OTS_MESSAGE_(0,otf_,"%4.4s: %s", tag_, msg_), false) |
| 55 |
| 56 // Convenience macros for use in files that only handle a single table tag, |
| 57 // defined as TABLE_NAME at the top of the file; the 'file' variable is |
| 58 // expected to be the current OpenTypeFile pointer. |
| 59 #define OTS_FAILURE_MSG(...) OTS_FAILURE_MSG_(file, TABLE_NAME ": " __VA_ARGS__) |
| 60 |
| 61 #define OTS_WARNING(...) OTS_WARNING_MSG_(file, TABLE_NAME ": " __VA_ARGS__) |
55 | 62 |
56 // ----------------------------------------------------------------------------- | 63 // ----------------------------------------------------------------------------- |
57 // Buffer helper class | 64 // Buffer helper class |
58 // | 65 // |
59 // This class perform some trival buffer operations while checking for | 66 // This class perform some trival buffer operations while checking for |
60 // out-of-bounds errors. As a family they return false if anything is amiss, | 67 // out-of-bounds errors. As a family they return false if anything is amiss, |
61 // updating the current offset otherwise. | 68 // updating the current offset otherwise. |
62 // ----------------------------------------------------------------------------- | 69 // ----------------------------------------------------------------------------- |
63 class Buffer { | 70 class Buffer { |
64 public: | 71 public: |
65 Buffer(const uint8_t *buffer, size_t len) | 72 Buffer(const uint8_t *buf, size_t len) |
66 : buffer_(buffer), | 73 : buffer_(buf), |
67 length_(len), | 74 length_(len), |
68 offset_(0) { } | 75 offset_(0) { } |
69 | 76 |
70 bool Skip(size_t n_bytes) { | 77 bool Skip(size_t n_bytes) { |
71 return Read(NULL, n_bytes); | 78 return Read(NULL, n_bytes); |
72 } | 79 } |
73 | 80 |
74 bool Read(uint8_t *buffer, size_t n_bytes) { | 81 bool Read(uint8_t *buf, size_t n_bytes) { |
75 if (n_bytes > 1024 * 1024 * 1024) { | 82 if (n_bytes > 1024 * 1024 * 1024) { |
76 return OTS_FAILURE(); | 83 return OTS_FAILURE(); |
77 } | 84 } |
78 if ((offset_ + n_bytes > length_) || | 85 if ((offset_ + n_bytes > length_) || |
79 (offset_ > length_ - n_bytes)) { | 86 (offset_ > length_ - n_bytes)) { |
80 return OTS_FAILURE(); | 87 return OTS_FAILURE(); |
81 } | 88 } |
82 if (buffer) { | 89 if (buf) { |
83 std::memcpy(buffer, buffer_ + offset_, n_bytes); | 90 std::memcpy(buf, buffer_ + offset_, n_bytes); |
84 } | 91 } |
85 offset_ += n_bytes; | 92 offset_ += n_bytes; |
86 return true; | 93 return true; |
87 } | 94 } |
88 | 95 |
89 inline bool ReadU8(uint8_t *value) { | 96 inline bool ReadU8(uint8_t *value) { |
90 if (offset_ + 1 > length_) { | 97 if (offset_ + 1 > length_) { |
91 return OTS_FAILURE(); | 98 return OTS_FAILURE(); |
92 } | 99 } |
93 *value = buffer_[offset_]; | 100 *value = buffer_[offset_]; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 template<typename T> T Round2(T value) { | 183 template<typename T> T Round2(T value) { |
177 if (value == std::numeric_limits<T>::max()) { | 184 if (value == std::numeric_limits<T>::max()) { |
178 return value; | 185 return value; |
179 } | 186 } |
180 return (value + 1) & ~1; | 187 return (value + 1) & ~1; |
181 } | 188 } |
182 | 189 |
183 bool IsValidVersionTag(uint32_t tag); | 190 bool IsValidVersionTag(uint32_t tag); |
184 | 191 |
185 #define FOR_EACH_TABLE_TYPE \ | 192 #define FOR_EACH_TABLE_TYPE \ |
186 F(cbdt, CBDT) \ | |
187 F(cblc, CBLC) \ | |
188 F(cff, CFF) \ | 193 F(cff, CFF) \ |
189 F(cmap, CMAP) \ | 194 F(cmap, CMAP) \ |
190 F(cvt, CVT) \ | 195 F(cvt, CVT) \ |
191 F(fpgm, FPGM) \ | 196 F(fpgm, FPGM) \ |
192 F(gasp, GASP) \ | 197 F(gasp, GASP) \ |
193 F(gdef, GDEF) \ | 198 F(gdef, GDEF) \ |
194 F(glyf, GLYF) \ | 199 F(glyf, GLYF) \ |
195 F(gpos, GPOS) \ | 200 F(gpos, GPOS) \ |
196 F(gsub, GSUB) \ | 201 F(gsub, GSUB) \ |
197 F(hdmx, HDMX) \ | 202 F(hdmx, HDMX) \ |
(...skipping 24 matching lines...) Expand all Loading... |
222 FOR_EACH_TABLE_TYPE | 227 FOR_EACH_TABLE_TYPE |
223 #undef F | 228 #undef F |
224 } | 229 } |
225 | 230 |
226 uint32_t version; | 231 uint32_t version; |
227 uint16_t num_tables; | 232 uint16_t num_tables; |
228 uint16_t search_range; | 233 uint16_t search_range; |
229 uint16_t entry_selector; | 234 uint16_t entry_selector; |
230 uint16_t range_shift; | 235 uint16_t range_shift; |
231 | 236 |
| 237 OTSContext *context; |
| 238 |
232 #define F(name, capname) OpenType##capname *name; | 239 #define F(name, capname) OpenType##capname *name; |
233 FOR_EACH_TABLE_TYPE | 240 FOR_EACH_TABLE_TYPE |
234 #undef F | 241 #undef F |
235 }; | 242 }; |
236 | 243 |
237 #define F(name, capname) \ | 244 #define F(name, capname) \ |
238 bool ots_##name##_parse(OpenTypeFile *f, const uint8_t *d, size_t l); \ | 245 bool ots_##name##_parse(OpenTypeFile *f, const uint8_t *d, size_t l); \ |
239 bool ots_##name##_should_serialise(OpenTypeFile *f); \ | 246 bool ots_##name##_should_serialise(OpenTypeFile *f); \ |
240 bool ots_##name##_serialise(OTSStream *s, OpenTypeFile *f); \ | 247 bool ots_##name##_serialise(OTSStream *s, OpenTypeFile *f); \ |
241 void ots_##name##_free(OpenTypeFile *f); | 248 void ots_##name##_free(OpenTypeFile *f); |
242 // TODO(yusukes): change these function names to follow Chromium coding rule. | 249 // TODO(yusukes): change these function names to follow Chromium coding rule. |
243 FOR_EACH_TABLE_TYPE | 250 FOR_EACH_TABLE_TYPE |
244 #undef F | 251 #undef F |
245 | 252 |
246 } // namespace ots | 253 } // namespace ots |
247 | 254 |
248 #endif // OTS_H_ | 255 #endif // OTS_H_ |
OLD | NEW |