PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/gfx/ots/src/ots.h

https://bitbucket.org/MeeGoAdmin/mozilla-central/
C Header | 219 lines | 174 code | 29 blank | 16 comment | 10 complexity | e4ac772d4d1a641d9d355fca2cf955e1 MD5 | raw file
Possible License(s): AGPL-1.0, MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, 0BSD, LGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0, JSON
  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. #ifndef OTS_H_
  5. #define OTS_H_
  6. #include <cstdarg>
  7. #include <cstdio>
  8. #include <cstdlib>
  9. #include <cstring>
  10. #include "opentype-sanitiser.h"
  11. namespace ots {
  12. #if defined(_MSC_VER) || !defined(OTS_DEBUG)
  13. #define OTS_FAILURE() false
  14. #else
  15. #define OTS_FAILURE() ots::Failure(__FILE__, __LINE__, __PRETTY_FUNCTION__)
  16. bool Failure(const char *f, int l, const char *fn);
  17. #endif
  18. #if defined(_MSC_VER)
  19. // MSVC supports C99 style variadic macros.
  20. #define OTS_WARNING(format, ...)
  21. #else
  22. // GCC
  23. #if defined(OTS_DEBUG)
  24. #define OTS_WARNING(format, args...) \
  25. ots::Warning(__FILE__, __LINE__, format, ##args)
  26. void Warning(const char *f, int l, const char *format, ...)
  27. __attribute__((format(printf, 3, 4)));
  28. #else
  29. #define OTS_WARNING(format, args...)
  30. #endif
  31. #endif
  32. // Define OTS_NO_TRANSCODE_HINTS (i.e., g++ -DOTS_NO_TRANSCODE_HINTS) if you
  33. // want to omit TrueType hinting instructions and variables in glyf, fpgm, prep,
  34. // and cvt tables.
  35. #if defined(OTS_NO_TRANSCODE_HINTS)
  36. const bool g_transcode_hints = false;
  37. #else
  38. const bool g_transcode_hints = true;
  39. #endif
  40. // -----------------------------------------------------------------------------
  41. // Buffer helper class
  42. //
  43. // This class perform some trival buffer operations while checking for
  44. // out-of-bounds errors. As a family they return false if anything is amiss,
  45. // updating the current offset otherwise.
  46. // -----------------------------------------------------------------------------
  47. class Buffer {
  48. public:
  49. Buffer(const uint8_t *buffer, size_t len)
  50. : buffer_(buffer),
  51. length_(len),
  52. offset_(0) { }
  53. bool Skip(size_t n_bytes) {
  54. return Read(NULL, n_bytes);
  55. }
  56. bool Read(uint8_t *buffer, size_t n_bytes) {
  57. if (n_bytes > 1024 * 1024 * 1024) {
  58. return OTS_FAILURE();
  59. }
  60. if ((offset_ + n_bytes > length_) ||
  61. (offset_ > length_ - n_bytes)) {
  62. return OTS_FAILURE();
  63. }
  64. if (buffer) {
  65. std::memcpy(buffer, buffer_ + offset_, n_bytes);
  66. }
  67. offset_ += n_bytes;
  68. return true;
  69. }
  70. inline bool ReadU8(uint8_t *value) {
  71. if (offset_ + 1 > length_) {
  72. return OTS_FAILURE();
  73. }
  74. *value = buffer_[offset_];
  75. ++offset_;
  76. return true;
  77. }
  78. bool ReadU16(uint16_t *value) {
  79. if (offset_ + 2 > length_) {
  80. return OTS_FAILURE();
  81. }
  82. std::memcpy(value, buffer_ + offset_, sizeof(uint16_t));
  83. *value = ntohs(*value);
  84. offset_ += 2;
  85. return true;
  86. }
  87. bool ReadS16(int16_t *value) {
  88. return ReadU16(reinterpret_cast<uint16_t*>(value));
  89. }
  90. bool ReadU24(uint32_t *value) {
  91. if (offset_ + 3 > length_) {
  92. return OTS_FAILURE();
  93. }
  94. *value = static_cast<uint32_t>(buffer_[offset_]) << 16 |
  95. static_cast<uint32_t>(buffer_[offset_ + 1]) << 8 |
  96. static_cast<uint32_t>(buffer_[offset_ + 2]);
  97. offset_ += 3;
  98. return true;
  99. }
  100. bool ReadU32(uint32_t *value) {
  101. if (offset_ + 4 > length_) {
  102. return OTS_FAILURE();
  103. }
  104. std::memcpy(value, buffer_ + offset_, sizeof(uint32_t));
  105. *value = ntohl(*value);
  106. offset_ += 4;
  107. return true;
  108. }
  109. bool ReadS32(int32_t *value) {
  110. return ReadU32(reinterpret_cast<uint32_t*>(value));
  111. }
  112. bool ReadTag(uint32_t *value) {
  113. if (offset_ + 4 > length_) {
  114. return OTS_FAILURE();
  115. }
  116. std::memcpy(value, buffer_ + offset_, sizeof(uint32_t));
  117. offset_ += 4;
  118. return true;
  119. }
  120. bool ReadR64(uint64_t *value) {
  121. if (offset_ + 8 > length_) {
  122. return OTS_FAILURE();
  123. }
  124. std::memcpy(value, buffer_ + offset_, sizeof(uint64_t));
  125. offset_ += 8;
  126. return true;
  127. }
  128. const uint8_t *buffer() const { return buffer_; }
  129. size_t offset() const { return offset_; }
  130. size_t length() const { return length_; }
  131. void set_offset(size_t newoffset) { offset_ = newoffset; }
  132. private:
  133. const uint8_t * const buffer_;
  134. const size_t length_;
  135. size_t offset_;
  136. };
  137. #define FOR_EACH_TABLE_TYPE \
  138. F(cff, CFF) \
  139. F(cmap, CMAP) \
  140. F(cvt, CVT) \
  141. F(fpgm, FPGM) \
  142. F(gasp, GASP) \
  143. F(gdef, GDEF) \
  144. F(glyf, GLYF) \
  145. F(gpos, GPOS) \
  146. F(gsub, GSUB) \
  147. F(hdmx, HDMX) \
  148. F(head, HEAD) \
  149. F(hhea, HHEA) \
  150. F(hmtx, HMTX) \
  151. F(kern, KERN) \
  152. F(loca, LOCA) \
  153. F(ltsh, LTSH) \
  154. F(maxp, MAXP) \
  155. F(name, NAME) \
  156. F(os2, OS2) \
  157. F(post, POST) \
  158. F(prep, PREP) \
  159. F(vdmx, VDMX) \
  160. F(vorg, VORG) \
  161. F(vhea, VHEA) \
  162. F(vmtx, VMTX)
  163. #define F(name, capname) struct OpenType##capname;
  164. FOR_EACH_TABLE_TYPE
  165. #undef F
  166. struct OpenTypeFile {
  167. OpenTypeFile() {
  168. #define F(name, capname) name = NULL;
  169. FOR_EACH_TABLE_TYPE
  170. #undef F
  171. }
  172. uint32_t version;
  173. uint16_t num_tables;
  174. uint16_t search_range;
  175. uint16_t entry_selector;
  176. uint16_t range_shift;
  177. #define F(name, capname) OpenType##capname *name;
  178. FOR_EACH_TABLE_TYPE
  179. #undef F
  180. };
  181. #define F(name, capname) \
  182. bool ots_##name##_parse(OpenTypeFile *f, const uint8_t *d, size_t l); \
  183. bool ots_##name##_should_serialise(OpenTypeFile *f); \
  184. bool ots_##name##_serialise(OTSStream *s, OpenTypeFile *f); \
  185. void ots_##name##_free(OpenTypeFile *f);
  186. // TODO(yusukes): change these function names to follow Chromium coding rule.
  187. FOR_EACH_TABLE_TYPE
  188. #undef F
  189. } // namespace ots
  190. #endif // OTS_H_