Ebrahim Byagowi | 60d9f00 | 2019-08-11 16:15:19 +0430 | [diff] [blame] | 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 | |
| 25 | #ifndef HB_AAT_LAYOUT_OPBD_TABLE_HH |
| 26 | #define HB_AAT_LAYOUT_OPBD_TABLE_HH |
| 27 | |
| 28 | #include "hb-aat-layout-common.hh" |
| 29 | #include "hb-open-type.hh" |
| 30 | |
| 31 | /* |
| 32 | * opbd -- Optical Bounds |
| 33 | * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6opbd.html |
| 34 | */ |
| 35 | #define HB_AAT_TAG_opbd HB_TAG('o','p','b','d') |
| 36 | |
| 37 | |
| 38 | namespace AAT { |
| 39 | |
| 40 | struct OpticalBounds |
| 41 | { |
| 42 | bool sanitize (hb_sanitize_context_t *c) const |
| 43 | { |
| 44 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 7c4e908 | 2022-07-11 14:01:52 -0600 | [diff] [blame] | 45 | return_trace (c->check_struct (this)); |
Ebrahim Byagowi | 60d9f00 | 2019-08-11 16:15:19 +0430 | [diff] [blame] | 46 | } |
| 47 | |
Ebrahim Byagowi | d6206db | 2019-08-14 11:24:06 +0430 | [diff] [blame] | 48 | FWORD leftSide; |
| 49 | FWORD topSide; |
| 50 | FWORD rightSide; |
| 51 | FWORD bottomSide; |
Ebrahim Byagowi | 60d9f00 | 2019-08-11 16:15:19 +0430 | [diff] [blame] | 52 | public: |
| 53 | DEFINE_SIZE_STATIC (8); |
| 54 | }; |
| 55 | |
Ebrahim Byagowi | bfffe85 | 2019-08-14 13:55:49 +0430 | [diff] [blame] | 56 | struct opbdFormat0 |
| 57 | { |
| 58 | bool get_bounds (hb_font_t *font, hb_codepoint_t glyph_id, |
| 59 | hb_glyph_extents_t *extents, const void *base) const |
| 60 | { |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 61 | const Offset16To<OpticalBounds> *bounds_offset = lookupTable.get_value (glyph_id, font->face->get_num_glyphs ()); |
Ebrahim Byagowi | bfffe85 | 2019-08-14 13:55:49 +0430 | [diff] [blame] | 62 | if (!bounds_offset) return false; |
| 63 | const OpticalBounds &bounds = base+*bounds_offset; |
| 64 | |
| 65 | if (extents) |
| 66 | *extents = { |
| 67 | font->em_scale_x (bounds.leftSide), |
| 68 | font->em_scale_y (bounds.topSide), |
| 69 | font->em_scale_x (bounds.rightSide), |
| 70 | font->em_scale_y (bounds.bottomSide) |
| 71 | }; |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | bool sanitize (hb_sanitize_context_t *c, const void *base) const |
| 76 | { |
| 77 | TRACE_SANITIZE (this); |
| 78 | return_trace (likely (c->check_struct (this) && lookupTable.sanitize (c, base))); |
| 79 | } |
| 80 | |
| 81 | protected: |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 82 | Lookup<Offset16To<OpticalBounds>> |
Ebrahim Byagowi | bfffe85 | 2019-08-14 13:55:49 +0430 | [diff] [blame] | 83 | lookupTable; /* Lookup table associating glyphs with the four |
| 84 | * int16 values for the left-side, top-side, |
| 85 | * right-side, and bottom-side optical bounds. */ |
| 86 | public: |
| 87 | DEFINE_SIZE_MIN (2); |
| 88 | }; |
| 89 | |
| 90 | struct opbdFormat1 |
| 91 | { |
| 92 | bool get_bounds (hb_font_t *font, hb_codepoint_t glyph_id, |
| 93 | hb_glyph_extents_t *extents, const void *base) const |
| 94 | { |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 95 | const Offset16To<OpticalBounds> *bounds_offset = lookupTable.get_value (glyph_id, font->face->get_num_glyphs ()); |
Ebrahim Byagowi | bfffe85 | 2019-08-14 13:55:49 +0430 | [diff] [blame] | 96 | if (!bounds_offset) return false; |
| 97 | const OpticalBounds &bounds = base+*bounds_offset; |
| 98 | |
| 99 | hb_position_t left = 0, top = 0, right = 0, bottom = 0, ignore; |
| 100 | if (font->get_glyph_contour_point (glyph_id, bounds.leftSide, &left, &ignore) || |
| 101 | font->get_glyph_contour_point (glyph_id, bounds.topSide, &ignore, &top) || |
| 102 | font->get_glyph_contour_point (glyph_id, bounds.rightSide, &right, &ignore) || |
| 103 | font->get_glyph_contour_point (glyph_id, bounds.bottomSide, &ignore, &bottom)) |
| 104 | { |
| 105 | if (extents) |
| 106 | *extents = {left, top, right, bottom}; |
| 107 | return true; |
| 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | bool sanitize (hb_sanitize_context_t *c, const void *base) const |
| 113 | { |
| 114 | TRACE_SANITIZE (this); |
| 115 | return_trace (likely (c->check_struct (this) && lookupTable.sanitize (c, base))); |
| 116 | } |
| 117 | |
| 118 | protected: |
Behdad Esfahbod | ad28f97 | 2021-03-31 12:49:14 -0600 | [diff] [blame] | 119 | Lookup<Offset16To<OpticalBounds>> |
Ebrahim Byagowi | bfffe85 | 2019-08-14 13:55:49 +0430 | [diff] [blame] | 120 | lookupTable; /* Lookup table associating glyphs with the four |
| 121 | * int16 values for the left-side, top-side, |
| 122 | * right-side, and bottom-side optical bounds. */ |
| 123 | public: |
| 124 | DEFINE_SIZE_MIN (2); |
| 125 | }; |
| 126 | |
Ebrahim Byagowi | 60d9f00 | 2019-08-11 16:15:19 +0430 | [diff] [blame] | 127 | struct opbd |
| 128 | { |
| 129 | static constexpr hb_tag_t tableTag = HB_AAT_TAG_opbd; |
| 130 | |
Ebrahim Byagowi | bfffe85 | 2019-08-14 13:55:49 +0430 | [diff] [blame] | 131 | bool get_bounds (hb_font_t *font, hb_codepoint_t glyph_id, |
| 132 | hb_glyph_extents_t *extents) const |
Ebrahim Byagowi | 60d9f00 | 2019-08-11 16:15:19 +0430 | [diff] [blame] | 133 | { |
Ebrahim Byagowi | 60d9f00 | 2019-08-11 16:15:19 +0430 | [diff] [blame] | 134 | switch (format) |
| 135 | { |
Behdad Esfahbod | 7a890c2 | 2024-09-22 13:19:02 -0600 | [diff] [blame] | 136 | case 0: hb_barrier (); return u.format0.get_bounds (font, glyph_id, extents, this); |
| 137 | case 1: hb_barrier (); return u.format1.get_bounds (font, glyph_id, extents, this); |
Ebrahim Byagowi | bfffe85 | 2019-08-14 13:55:49 +0430 | [diff] [blame] | 138 | default:return false; |
Ebrahim Byagowi | 60d9f00 | 2019-08-11 16:15:19 +0430 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | bool sanitize (hb_sanitize_context_t *c) const |
| 143 | { |
| 144 | TRACE_SANITIZE (this); |
Ebrahim Byagowi | bfffe85 | 2019-08-14 13:55:49 +0430 | [diff] [blame] | 145 | if (unlikely (!c->check_struct (this) || version.major != 1)) |
| 146 | return_trace (false); |
Behdad Esfahbod | 3a9262c | 2023-11-04 12:52:46 -0600 | [diff] [blame] | 147 | hb_barrier (); |
Ebrahim Byagowi | bfffe85 | 2019-08-14 13:55:49 +0430 | [diff] [blame] | 148 | |
| 149 | switch (format) |
| 150 | { |
Behdad Esfahbod | 7a890c2 | 2024-09-22 13:19:02 -0600 | [diff] [blame] | 151 | case 0: hb_barrier (); return_trace (u.format0.sanitize (c, this)); |
| 152 | case 1: hb_barrier (); return_trace (u.format1.sanitize (c, this)); |
Ebrahim Byagowi | bfffe85 | 2019-08-14 13:55:49 +0430 | [diff] [blame] | 153 | default:return_trace (true); |
| 154 | } |
Ebrahim Byagowi | 60d9f00 | 2019-08-11 16:15:19 +0430 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | protected: |
| 158 | FixedVersion<>version; /* Version number of the optical bounds |
| 159 | * table (0x00010000 for the current version). */ |
| 160 | HBUINT16 format; /* Format of the optical bounds table. |
| 161 | * Format 0 indicates distance and Format 1 indicates |
| 162 | * control point. */ |
Ebrahim Byagowi | bfffe85 | 2019-08-14 13:55:49 +0430 | [diff] [blame] | 163 | union { |
Ebrahim Byagowi | 08428a1 | 2020-04-24 23:45:17 +0430 | [diff] [blame] | 164 | opbdFormat0 format0; |
| 165 | opbdFormat1 format1; |
Ebrahim Byagowi | bfffe85 | 2019-08-14 13:55:49 +0430 | [diff] [blame] | 166 | } u; |
Ebrahim Byagowi | 60d9f00 | 2019-08-11 16:15:19 +0430 | [diff] [blame] | 167 | public: |
| 168 | DEFINE_SIZE_MIN (8); |
| 169 | }; |
| 170 | |
| 171 | } /* namespace AAT */ |
| 172 | |
| 173 | |
| 174 | #endif /* HB_AAT_LAYOUT_OPBD_TABLE_HH */ |