hb-aat-layout-opbd-table.hh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright © 2019 Ebrahim Byagowi
  3. *
  4. * This is part of HarfBuzz, a text shaping library.
  5. *
  6. * Permission is hereby granted, without written agreement and without
  7. * license or royalty fees, to use, copy, modify, and distribute this
  8. * software and its documentation for any purpose, provided that the
  9. * above copyright notice and the following two paragraphs appear in
  10. * all copies of this software.
  11. *
  12. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  13. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  14. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  15. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  16. * DAMAGE.
  17. *
  18. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  19. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  21. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  22. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  23. */
  24. #ifndef HB_AAT_LAYOUT_OPBD_TABLE_HH
  25. #define HB_AAT_LAYOUT_OPBD_TABLE_HH
  26. #include "hb-aat-layout-common.hh"
  27. #include "hb-open-type.hh"
  28. /*
  29. * opbd -- Optical Bounds
  30. * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6opbd.html
  31. */
  32. #define HB_AAT_TAG_opbd HB_TAG('o','p','b','d')
  33. namespace AAT {
  34. struct OpticalBounds
  35. {
  36. bool sanitize (hb_sanitize_context_t *c) const
  37. {
  38. TRACE_SANITIZE (this);
  39. return_trace (c->check_struct (this));
  40. }
  41. FWORD leftSide;
  42. FWORD topSide;
  43. FWORD rightSide;
  44. FWORD bottomSide;
  45. public:
  46. DEFINE_SIZE_STATIC (8);
  47. };
  48. struct opbdFormat0
  49. {
  50. bool get_bounds (hb_font_t *font, hb_codepoint_t glyph_id,
  51. hb_glyph_extents_t *extents, const void *base) const
  52. {
  53. const Offset16To<OpticalBounds> *bounds_offset = lookupTable.get_value (glyph_id, font->face->get_num_glyphs ());
  54. if (!bounds_offset) return false;
  55. const OpticalBounds &bounds = base+*bounds_offset;
  56. if (extents)
  57. *extents = {
  58. font->em_scale_x (bounds.leftSide),
  59. font->em_scale_y (bounds.topSide),
  60. font->em_scale_x (bounds.rightSide),
  61. font->em_scale_y (bounds.bottomSide)
  62. };
  63. return true;
  64. }
  65. bool sanitize (hb_sanitize_context_t *c, const void *base) const
  66. {
  67. TRACE_SANITIZE (this);
  68. return_trace (likely (c->check_struct (this) && lookupTable.sanitize (c, base)));
  69. }
  70. protected:
  71. Lookup<Offset16To<OpticalBounds>>
  72. lookupTable; /* Lookup table associating glyphs with the four
  73. * int16 values for the left-side, top-side,
  74. * right-side, and bottom-side optical bounds. */
  75. public:
  76. DEFINE_SIZE_MIN (2);
  77. };
  78. struct opbdFormat1
  79. {
  80. bool get_bounds (hb_font_t *font, hb_codepoint_t glyph_id,
  81. hb_glyph_extents_t *extents, const void *base) const
  82. {
  83. const Offset16To<OpticalBounds> *bounds_offset = lookupTable.get_value (glyph_id, font->face->get_num_glyphs ());
  84. if (!bounds_offset) return false;
  85. const OpticalBounds &bounds = base+*bounds_offset;
  86. hb_position_t left = 0, top = 0, right = 0, bottom = 0, ignore;
  87. if (font->get_glyph_contour_point (glyph_id, bounds.leftSide, &left, &ignore) ||
  88. font->get_glyph_contour_point (glyph_id, bounds.topSide, &ignore, &top) ||
  89. font->get_glyph_contour_point (glyph_id, bounds.rightSide, &right, &ignore) ||
  90. font->get_glyph_contour_point (glyph_id, bounds.bottomSide, &ignore, &bottom))
  91. {
  92. if (extents)
  93. *extents = {left, top, right, bottom};
  94. return true;
  95. }
  96. return false;
  97. }
  98. bool sanitize (hb_sanitize_context_t *c, const void *base) const
  99. {
  100. TRACE_SANITIZE (this);
  101. return_trace (likely (c->check_struct (this) && lookupTable.sanitize (c, base)));
  102. }
  103. protected:
  104. Lookup<Offset16To<OpticalBounds>>
  105. lookupTable; /* Lookup table associating glyphs with the four
  106. * int16 values for the left-side, top-side,
  107. * right-side, and bottom-side optical bounds. */
  108. public:
  109. DEFINE_SIZE_MIN (2);
  110. };
  111. struct opbd
  112. {
  113. static constexpr hb_tag_t tableTag = HB_AAT_TAG_opbd;
  114. bool get_bounds (hb_font_t *font, hb_codepoint_t glyph_id,
  115. hb_glyph_extents_t *extents) const
  116. {
  117. switch (format)
  118. {
  119. case 0: return u.format0.get_bounds (font, glyph_id, extents, this);
  120. case 1: return u.format1.get_bounds (font, glyph_id, extents, this);
  121. default:return false;
  122. }
  123. }
  124. bool sanitize (hb_sanitize_context_t *c) const
  125. {
  126. TRACE_SANITIZE (this);
  127. if (unlikely (!c->check_struct (this) || version.major != 1))
  128. return_trace (false);
  129. switch (format)
  130. {
  131. case 0: return_trace (u.format0.sanitize (c, this));
  132. case 1: return_trace (u.format1.sanitize (c, this));
  133. default:return_trace (true);
  134. }
  135. }
  136. protected:
  137. FixedVersion<>version; /* Version number of the optical bounds
  138. * table (0x00010000 for the current version). */
  139. HBUINT16 format; /* Format of the optical bounds table.
  140. * Format 0 indicates distance and Format 1 indicates
  141. * control point. */
  142. union {
  143. opbdFormat0 format0;
  144. opbdFormat1 format1;
  145. } u;
  146. public:
  147. DEFINE_SIZE_MIN (8);
  148. };
  149. } /* namespace AAT */
  150. #endif /* HB_AAT_LAYOUT_OPBD_TABLE_HH */