Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2016 Google, Inc. |
| 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 | * Google Author(s): Seigo Nonaka |
| 25 | */ |
| 26 | |
Ebrahim Byagowi | 0ef6ab2 | 2018-03-04 02:47:26 +0330 | [diff] [blame] | 27 | #ifndef HB_OT_COLOR_CBDT_TABLE_HH |
| 28 | #define HB_OT_COLOR_CBDT_TABLE_HH |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 29 | |
| 30 | #include "hb-open-type-private.hh" |
| 31 | |
| 32 | namespace OT { |
| 33 | |
| 34 | struct SmallGlyphMetrics |
| 35 | { |
Behdad Esfahbod | 4b58c9e | 2016-12-02 19:25:54 -0800 | [diff] [blame] | 36 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 37 | { |
| 38 | TRACE_SANITIZE (this); |
| 39 | return_trace (c->check_struct (this)); |
| 40 | } |
| 41 | |
Behdad Esfahbod | d495fc5 | 2016-12-02 21:36:42 -0800 | [diff] [blame] | 42 | inline void get_extents (hb_glyph_extents_t *extents) const |
| 43 | { |
| 44 | extents->x_bearing = bearingX; |
| 45 | extents->y_bearing = bearingY; |
| 46 | extents->width = width; |
Behdad Esfahbod | 654f9ab | 2016-12-04 18:56:24 -0800 | [diff] [blame] | 47 | extents->height = -height; |
Behdad Esfahbod | d495fc5 | 2016-12-02 21:36:42 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Behdad Esfahbod | 9e33734 | 2018-03-14 15:52:53 +0100 | [diff] [blame] | 50 | HBUINT8 height; |
| 51 | HBUINT8 width; |
| 52 | HBINT8 bearingX; |
| 53 | HBINT8 bearingY; |
| 54 | HBUINT8 advance; |
| 55 | public: |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 56 | DEFINE_SIZE_STATIC(5); |
| 57 | }; |
| 58 | |
Behdad Esfahbod | e4bfccf | 2016-12-04 19:43:33 -0800 | [diff] [blame] | 59 | struct BigGlyphMetrics : SmallGlyphMetrics |
| 60 | { |
Behdad Esfahbod | 9e33734 | 2018-03-14 15:52:53 +0100 | [diff] [blame] | 61 | HBINT8 vertBearingX; |
| 62 | HBINT8 vertBearingY; |
| 63 | HBUINT8 vertAdvance; |
| 64 | public: |
Behdad Esfahbod | e4bfccf | 2016-12-04 19:43:33 -0800 | [diff] [blame] | 65 | DEFINE_SIZE_STATIC(8); |
| 66 | }; |
| 67 | |
Behdad Esfahbod | 4b58c9e | 2016-12-02 19:25:54 -0800 | [diff] [blame] | 68 | struct SBitLineMetrics |
| 69 | { |
| 70 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 71 | { |
| 72 | TRACE_SANITIZE (this); |
| 73 | return_trace (c->check_struct (this)); |
| 74 | } |
| 75 | |
Behdad Esfahbod | 9e33734 | 2018-03-14 15:52:53 +0100 | [diff] [blame] | 76 | HBINT8 ascender; |
| 77 | HBINT8 decender; |
| 78 | HBUINT8 widthMax; |
| 79 | HBINT8 caretSlopeNumerator; |
| 80 | HBINT8 caretSlopeDenominator; |
| 81 | HBINT8 caretOffset; |
| 82 | HBINT8 minOriginSB; |
| 83 | HBINT8 minAdvanceSB; |
| 84 | HBINT8 maxBeforeBL; |
| 85 | HBINT8 minAfterBL; |
| 86 | HBINT8 padding1; |
| 87 | HBINT8 padding2; |
| 88 | public: |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 89 | DEFINE_SIZE_STATIC(12); |
| 90 | }; |
| 91 | |
Behdad Esfahbod | 9eda74c | 2016-12-04 19:12:52 -0800 | [diff] [blame] | 92 | |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 93 | /* |
| 94 | * Index Subtables. |
| 95 | */ |
Behdad Esfahbod | 9eda74c | 2016-12-04 19:12:52 -0800 | [diff] [blame] | 96 | |
Behdad Esfahbod | d495fc5 | 2016-12-02 21:36:42 -0800 | [diff] [blame] | 97 | struct IndexSubtableHeader |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 98 | { |
Behdad Esfahbod | d495fc5 | 2016-12-02 21:36:42 -0800 | [diff] [blame] | 99 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 100 | { |
| 101 | TRACE_SANITIZE (this); |
| 102 | return_trace (c->check_struct (this)); |
| 103 | } |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 104 | |
Behdad Esfahbod | 9e33734 | 2018-03-14 15:52:53 +0100 | [diff] [blame] | 105 | HBUINT16 indexFormat; |
| 106 | HBUINT16 imageFormat; |
| 107 | HBUINT32 imageDataOffset; |
| 108 | public: |
Behdad Esfahbod | 4b58c9e | 2016-12-02 19:25:54 -0800 | [diff] [blame] | 109 | DEFINE_SIZE_STATIC(8); |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 110 | }; |
| 111 | |
Behdad Esfahbod | a141d1d | 2016-12-04 19:26:54 -0800 | [diff] [blame] | 112 | template <typename OffsetType> |
| 113 | struct IndexSubtableFormat1Or3 |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 114 | { |
Behdad Esfahbod | d495fc5 | 2016-12-02 21:36:42 -0800 | [diff] [blame] | 115 | inline bool sanitize (hb_sanitize_context_t *c, unsigned int glyph_count) const |
| 116 | { |
| 117 | TRACE_SANITIZE (this); |
| 118 | return_trace (c->check_struct (this) && |
| 119 | c->check_array (offsetArrayZ, offsetArrayZ[0].static_size, glyph_count + 1)); |
| 120 | } |
| 121 | |
| 122 | bool get_image_data (unsigned int idx, |
| 123 | unsigned int *offset, |
| 124 | unsigned int *length) const |
| 125 | { |
| 126 | if (unlikely (offsetArrayZ[idx + 1] <= offsetArrayZ[idx])) |
| 127 | return false; |
| 128 | |
| 129 | *offset = header.imageDataOffset + offsetArrayZ[idx]; |
| 130 | *length = offsetArrayZ[idx + 1] - offsetArrayZ[idx]; |
| 131 | return true; |
| 132 | } |
| 133 | |
Behdad Esfahbod | 9e33734 | 2018-03-14 15:52:53 +0100 | [diff] [blame] | 134 | IndexSubtableHeader header; |
| 135 | Offset<OffsetType> offsetArrayZ[VAR]; |
| 136 | public: |
Behdad Esfahbod | 4b58c9e | 2016-12-02 19:25:54 -0800 | [diff] [blame] | 137 | DEFINE_SIZE_ARRAY(8, offsetArrayZ); |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 138 | }; |
| 139 | |
Behdad Esfahbod | 6b19178 | 2018-01-10 03:07:30 +0100 | [diff] [blame] | 140 | struct IndexSubtableFormat1 : IndexSubtableFormat1Or3<HBUINT32> {}; |
| 141 | struct IndexSubtableFormat3 : IndexSubtableFormat1Or3<HBUINT16> {}; |
Behdad Esfahbod | a141d1d | 2016-12-04 19:26:54 -0800 | [diff] [blame] | 142 | |
Behdad Esfahbod | d495fc5 | 2016-12-02 21:36:42 -0800 | [diff] [blame] | 143 | struct IndexSubtable |
| 144 | { |
| 145 | inline bool sanitize (hb_sanitize_context_t *c, unsigned int glyph_count) const |
| 146 | { |
| 147 | TRACE_SANITIZE (this); |
| 148 | if (!u.header.sanitize (c)) return_trace (false); |
| 149 | switch (u.header.indexFormat) { |
| 150 | case 1: return_trace (u.format1.sanitize (c, glyph_count)); |
Behdad Esfahbod | a141d1d | 2016-12-04 19:26:54 -0800 | [diff] [blame] | 151 | case 3: return_trace (u.format3.sanitize (c, glyph_count)); |
Behdad Esfahbod | d495fc5 | 2016-12-02 21:36:42 -0800 | [diff] [blame] | 152 | default:return_trace (true); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | inline bool get_extents (hb_glyph_extents_t *extents) const |
| 157 | { |
| 158 | switch (u.header.indexFormat) { |
| 159 | case 2: case 5: /* TODO */ |
| 160 | case 1: case 3: case 4: /* Variable-metrics formats do not have metrics here. */ |
| 161 | default:return (false); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | bool get_image_data (unsigned int idx, |
| 166 | unsigned int *offset, |
| 167 | unsigned int *length, |
| 168 | unsigned int *format) const |
| 169 | { |
| 170 | *format = u.header.imageFormat; |
| 171 | switch (u.header.indexFormat) { |
| 172 | case 1: return u.format1.get_image_data (idx, offset, length); |
Behdad Esfahbod | a141d1d | 2016-12-04 19:26:54 -0800 | [diff] [blame] | 173 | case 3: return u.format3.get_image_data (idx, offset, length); |
Behdad Esfahbod | d495fc5 | 2016-12-02 21:36:42 -0800 | [diff] [blame] | 174 | default: return false; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | protected: |
| 179 | union { |
| 180 | IndexSubtableHeader header; |
| 181 | IndexSubtableFormat1 format1; |
Behdad Esfahbod | a141d1d | 2016-12-04 19:26:54 -0800 | [diff] [blame] | 182 | IndexSubtableFormat3 format3; |
Behdad Esfahbod | e4bfccf | 2016-12-04 19:43:33 -0800 | [diff] [blame] | 183 | /* TODO: Format 2, 4, 5. */ |
Behdad Esfahbod | d495fc5 | 2016-12-02 21:36:42 -0800 | [diff] [blame] | 184 | } u; |
| 185 | public: |
| 186 | DEFINE_SIZE_UNION (8, header); |
| 187 | }; |
| 188 | |
| 189 | struct IndexSubtableRecord |
| 190 | { |
| 191 | inline bool sanitize (hb_sanitize_context_t *c, const void *base) const |
| 192 | { |
| 193 | TRACE_SANITIZE (this); |
| 194 | return_trace (c->check_struct (this) && |
| 195 | firstGlyphIndex <= lastGlyphIndex && |
| 196 | offsetToSubtable.sanitize (c, this, lastGlyphIndex - firstGlyphIndex + 1)); |
| 197 | } |
| 198 | |
| 199 | inline bool get_extents (hb_glyph_extents_t *extents) const |
| 200 | { |
| 201 | return (this+offsetToSubtable).get_extents (extents); |
| 202 | } |
| 203 | |
| 204 | bool get_image_data (unsigned int gid, |
| 205 | unsigned int *offset, |
| 206 | unsigned int *length, |
| 207 | unsigned int *format) const |
| 208 | { |
| 209 | if (gid < firstGlyphIndex || gid > lastGlyphIndex) |
| 210 | { |
| 211 | return false; |
| 212 | } |
| 213 | return (this+offsetToSubtable).get_image_data (gid - firstGlyphIndex, |
| 214 | offset, length, format); |
| 215 | } |
| 216 | |
Behdad Esfahbod | 9e33734 | 2018-03-14 15:52:53 +0100 | [diff] [blame] | 217 | GlyphID firstGlyphIndex; |
| 218 | GlyphID lastGlyphIndex; |
| 219 | LOffsetTo<IndexSubtable> offsetToSubtable; |
| 220 | public: |
Behdad Esfahbod | d495fc5 | 2016-12-02 21:36:42 -0800 | [diff] [blame] | 221 | DEFINE_SIZE_STATIC(8); |
| 222 | }; |
| 223 | |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 224 | struct IndexSubtableArray |
| 225 | { |
Ebrahim Byagowi | 8fd5542 | 2018-03-27 16:57:09 +0430 | [diff] [blame] | 226 | friend struct CBDT; |
| 227 | |
Behdad Esfahbod | ce09e90 | 2016-12-02 20:12:57 -0800 | [diff] [blame] | 228 | inline bool sanitize (hb_sanitize_context_t *c, unsigned int count) const |
| 229 | { |
| 230 | TRACE_SANITIZE (this); |
Behdad Esfahbod | b7068af | 2016-12-04 19:36:30 -0800 | [diff] [blame] | 231 | if (unlikely (!c->check_array (&indexSubtablesZ, indexSubtablesZ[0].static_size, count))) |
Behdad Esfahbod | d495fc5 | 2016-12-02 21:36:42 -0800 | [diff] [blame] | 232 | return_trace (false); |
| 233 | for (unsigned int i = 0; i < count; i++) |
| 234 | if (unlikely (!indexSubtablesZ[i].sanitize (c, this))) |
| 235 | return_trace (false); |
| 236 | return_trace (true); |
Behdad Esfahbod | ce09e90 | 2016-12-02 20:12:57 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 239 | public: |
Behdad Esfahbod | d495fc5 | 2016-12-02 21:36:42 -0800 | [diff] [blame] | 240 | const IndexSubtableRecord* find_table (hb_codepoint_t glyph, unsigned int numTables) const |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 241 | { |
Behdad Esfahbod | 4b58c9e | 2016-12-02 19:25:54 -0800 | [diff] [blame] | 242 | for (unsigned int i = 0; i < numTables; ++i) |
| 243 | { |
| 244 | unsigned int firstGlyphIndex = indexSubtablesZ[i].firstGlyphIndex; |
| 245 | unsigned int lastGlyphIndex = indexSubtablesZ[i].lastGlyphIndex; |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 246 | if (firstGlyphIndex <= glyph && glyph <= lastGlyphIndex) { |
Behdad Esfahbod | 4b58c9e | 2016-12-02 19:25:54 -0800 | [diff] [blame] | 247 | return &indexSubtablesZ[i]; |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 250 | return nullptr; |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | protected: |
Behdad Esfahbod | 9e33734 | 2018-03-14 15:52:53 +0100 | [diff] [blame] | 254 | IndexSubtableRecord indexSubtablesZ[VAR]; |
Behdad Esfahbod | ce09e90 | 2016-12-02 20:12:57 -0800 | [diff] [blame] | 255 | public: |
| 256 | DEFINE_SIZE_ARRAY(0, indexSubtablesZ); |
| 257 | }; |
| 258 | |
| 259 | struct BitmapSizeTable |
| 260 | { |
Behdad Esfahbod | 9eda74c | 2016-12-04 19:12:52 -0800 | [diff] [blame] | 261 | friend struct CBLC; |
Ebrahim Byagowi | 8fd5542 | 2018-03-27 16:57:09 +0430 | [diff] [blame] | 262 | friend struct CBDT; |
Behdad Esfahbod | 9eda74c | 2016-12-04 19:12:52 -0800 | [diff] [blame] | 263 | |
Behdad Esfahbod | ce09e90 | 2016-12-02 20:12:57 -0800 | [diff] [blame] | 264 | inline bool sanitize (hb_sanitize_context_t *c, const void *base) const |
| 265 | { |
| 266 | TRACE_SANITIZE (this); |
| 267 | return_trace (c->check_struct (this) && |
| 268 | indexSubtableArrayOffset.sanitize (c, base, numberOfIndexSubtables) && |
| 269 | c->check_range (&(base+indexSubtableArrayOffset), indexTablesSize) && |
| 270 | horizontal.sanitize (c) && |
| 271 | vertical.sanitize (c)); |
| 272 | } |
| 273 | |
Behdad Esfahbod | 9eda74c | 2016-12-04 19:12:52 -0800 | [diff] [blame] | 274 | const IndexSubtableRecord *find_table (hb_codepoint_t glyph, const void *base) const |
| 275 | { |
| 276 | return (base+indexSubtableArrayOffset).find_table (glyph, numberOfIndexSubtables); |
| 277 | } |
| 278 | |
| 279 | protected: |
Behdad Esfahbod | 9e33734 | 2018-03-14 15:52:53 +0100 | [diff] [blame] | 280 | LOffsetTo<IndexSubtableArray> |
| 281 | indexSubtableArrayOffset; |
| 282 | HBUINT32 indexTablesSize; |
| 283 | HBUINT32 numberOfIndexSubtables; |
| 284 | HBUINT32 colorRef; |
| 285 | SBitLineMetrics horizontal; |
| 286 | SBitLineMetrics vertical; |
| 287 | GlyphID startGlyphIndex; |
| 288 | GlyphID endGlyphIndex; |
| 289 | HBUINT8 ppemX; |
| 290 | HBUINT8 ppemY; |
| 291 | HBUINT8 bitDepth; |
| 292 | HBINT8 flags; |
Behdad Esfahbod | f00a94e | 2017-11-14 19:54:48 -0800 | [diff] [blame] | 293 | public: |
Behdad Esfahbod | ce09e90 | 2016-12-02 20:12:57 -0800 | [diff] [blame] | 294 | DEFINE_SIZE_STATIC(48); |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 295 | }; |
| 296 | |
Behdad Esfahbod | 9eda74c | 2016-12-04 19:12:52 -0800 | [diff] [blame] | 297 | |
| 298 | /* |
| 299 | * Glyph Bitmap Data Formats. |
| 300 | */ |
| 301 | |
| 302 | struct GlyphBitmapDataFormat17 |
| 303 | { |
Behdad Esfahbod | 9e33734 | 2018-03-14 15:52:53 +0100 | [diff] [blame] | 304 | SmallGlyphMetrics glyphMetrics; |
| 305 | ArrayOf<HBUINT8, HBUINT32> data; |
| 306 | public: |
| 307 | DEFINE_SIZE_ARRAY(9, data); |
Behdad Esfahbod | 9eda74c | 2016-12-04 19:12:52 -0800 | [diff] [blame] | 308 | }; |
| 309 | |
Ebrahim Byagowi | 8fd5542 | 2018-03-27 16:57:09 +0430 | [diff] [blame] | 310 | struct GlyphBitmapDataFormat18 |
| 311 | { |
| 312 | BigGlyphMetrics glyphMetrics; |
| 313 | ArrayOf<HBUINT8, HBUINT32> data; |
| 314 | public: |
| 315 | DEFINE_SIZE_ARRAY(12, data); |
| 316 | }; |
| 317 | |
| 318 | struct GlyphBitmapDataFormat19 |
| 319 | { |
| 320 | ArrayOf<HBUINT8, HBUINT32> data; |
| 321 | public: |
| 322 | DEFINE_SIZE_ARRAY(4, data); |
| 323 | }; |
| 324 | |
Behdad Esfahbod | 9eda74c | 2016-12-04 19:12:52 -0800 | [diff] [blame] | 325 | |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 326 | /* |
| 327 | * CBLC -- Color Bitmap Location Table |
| 328 | */ |
| 329 | |
| 330 | #define HB_OT_TAG_CBLC HB_TAG('C','B','L','C') |
| 331 | |
| 332 | struct CBLC |
| 333 | { |
Behdad Esfahbod | f00a94e | 2017-11-14 19:54:48 -0800 | [diff] [blame] | 334 | friend struct CBDT; |
| 335 | |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 336 | static const hb_tag_t tableTag = HB_OT_TAG_CBLC; |
| 337 | |
| 338 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 339 | { |
| 340 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 4b58c9e | 2016-12-02 19:25:54 -0800 | [diff] [blame] | 341 | return_trace (c->check_struct (this) && |
| 342 | likely (version.major == 2 || version.major == 3) && |
Behdad Esfahbod | ce09e90 | 2016-12-02 20:12:57 -0800 | [diff] [blame] | 343 | sizeTables.sanitize (c, this)); |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Behdad Esfahbod | f00a94e | 2017-11-14 19:54:48 -0800 | [diff] [blame] | 346 | protected: |
Behdad Esfahbod | 9eda74c | 2016-12-04 19:12:52 -0800 | [diff] [blame] | 347 | const IndexSubtableRecord *find_table (hb_codepoint_t glyph, |
| 348 | unsigned int *x_ppem, unsigned int *y_ppem) const |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 349 | { |
Behdad Esfahbod | 9eda74c | 2016-12-04 19:12:52 -0800 | [diff] [blame] | 350 | /* TODO: Make it possible to select strike. */ |
| 351 | |
Behdad Esfahbod | 4b58c9e | 2016-12-02 19:25:54 -0800 | [diff] [blame] | 352 | unsigned int count = sizeTables.len; |
Behdad Esfahbod | 9eda74c | 2016-12-04 19:12:52 -0800 | [diff] [blame] | 353 | for (uint32_t i = 0; i < count; ++i) |
| 354 | { |
Behdad Esfahbod | 4b58c9e | 2016-12-02 19:25:54 -0800 | [diff] [blame] | 355 | unsigned int startGlyphIndex = sizeTables.array[i].startGlyphIndex; |
| 356 | unsigned int endGlyphIndex = sizeTables.array[i].endGlyphIndex; |
Behdad Esfahbod | 9eda74c | 2016-12-04 19:12:52 -0800 | [diff] [blame] | 357 | if (startGlyphIndex <= glyph && glyph <= endGlyphIndex) |
| 358 | { |
Behdad Esfahbod | e4bfccf | 2016-12-04 19:43:33 -0800 | [diff] [blame] | 359 | *x_ppem = sizeTables[i].ppemX; |
| 360 | *y_ppem = sizeTables[i].ppemY; |
| 361 | return sizeTables[i].find_table (glyph, this); |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 362 | } |
| 363 | } |
Behdad Esfahbod | 9eda74c | 2016-12-04 19:12:52 -0800 | [diff] [blame] | 364 | |
Behdad Esfahbod | dbdbfe3 | 2017-10-15 12:11:08 +0200 | [diff] [blame] | 365 | return nullptr; |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | protected: |
Behdad Esfahbod | b732c53 | 2017-01-22 20:31:53 -0800 | [diff] [blame] | 369 | FixedVersion<> version; |
| 370 | LArrayOf<BitmapSizeTable> sizeTables; |
Behdad Esfahbod | 4b58c9e | 2016-12-02 19:25:54 -0800 | [diff] [blame] | 371 | public: |
| 372 | DEFINE_SIZE_ARRAY(8, sizeTables); |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 373 | }; |
| 374 | |
| 375 | /* |
| 376 | * CBDT -- Color Bitmap Data Table |
| 377 | */ |
| 378 | #define HB_OT_TAG_CBDT HB_TAG('C','B','D','T') |
| 379 | |
| 380 | struct CBDT |
| 381 | { |
| 382 | static const hb_tag_t tableTag = HB_OT_TAG_CBDT; |
| 383 | |
| 384 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 385 | { |
| 386 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 4b58c9e | 2016-12-02 19:25:54 -0800 | [diff] [blame] | 387 | return_trace (c->check_struct (this) && |
| 388 | likely (version.major == 2 || version.major == 3)); |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Behdad Esfahbod | c4e18e5 | 2017-11-14 19:47:31 -0800 | [diff] [blame] | 391 | struct accelerator_t |
| 392 | { |
| 393 | inline void init (hb_face_t *face) |
| 394 | { |
Behdad Esfahbod | 96f1f52 | 2018-02-12 18:48:51 -0800 | [diff] [blame] | 395 | upem = hb_face_get_upem (face); |
Behdad Esfahbod | c4e18e5 | 2017-11-14 19:47:31 -0800 | [diff] [blame] | 396 | |
Behdad Esfahbod | 470fe5b | 2018-01-09 15:48:51 +0100 | [diff] [blame] | 397 | cblc_blob = Sanitizer<CBLC>().sanitize (face->reference_table (HB_OT_TAG_CBLC)); |
| 398 | cbdt_blob = Sanitizer<CBDT>().sanitize (face->reference_table (HB_OT_TAG_CBDT)); |
Behdad Esfahbod | c4e18e5 | 2017-11-14 19:47:31 -0800 | [diff] [blame] | 399 | cbdt_len = hb_blob_get_length (cbdt_blob); |
| 400 | |
| 401 | if (hb_blob_get_length (cblc_blob) == 0) { |
| 402 | cblc = nullptr; |
| 403 | cbdt = nullptr; |
| 404 | return; /* Not a bitmap font. */ |
| 405 | } |
| 406 | cblc = Sanitizer<CBLC>::lock_instance (cblc_blob); |
| 407 | cbdt = Sanitizer<CBDT>::lock_instance (cbdt_blob); |
| 408 | |
| 409 | } |
| 410 | |
| 411 | inline void fini (void) |
| 412 | { |
| 413 | hb_blob_destroy (this->cblc_blob); |
| 414 | hb_blob_destroy (this->cbdt_blob); |
| 415 | } |
| 416 | |
| 417 | inline bool get_extents (hb_codepoint_t glyph, hb_glyph_extents_t *extents) const |
| 418 | { |
| 419 | unsigned int x_ppem = upem, y_ppem = upem; /* TODO Use font ppem if available. */ |
| 420 | |
| 421 | if (!cblc) |
| 422 | return false; // Not a color bitmap font. |
| 423 | |
| 424 | const IndexSubtableRecord *subtable_record = this->cblc->find_table(glyph, &x_ppem, &y_ppem); |
| 425 | if (!subtable_record || !x_ppem || !y_ppem) |
| 426 | return false; |
| 427 | |
| 428 | if (subtable_record->get_extents (extents)) |
| 429 | return true; |
| 430 | |
| 431 | unsigned int image_offset = 0, image_length = 0, image_format = 0; |
| 432 | if (!subtable_record->get_image_data (glyph, &image_offset, &image_length, &image_format)) |
| 433 | return false; |
| 434 | |
| 435 | { |
| 436 | if (unlikely (image_offset > cbdt_len || cbdt_len - image_offset < image_length)) |
| 437 | return false; |
| 438 | |
| 439 | switch (image_format) |
| 440 | { |
| 441 | case 17: { |
| 442 | if (unlikely (image_length < GlyphBitmapDataFormat17::min_size)) |
| 443 | return false; |
| 444 | |
| 445 | const GlyphBitmapDataFormat17& glyphFormat17 = |
| 446 | StructAtOffset<GlyphBitmapDataFormat17> (this->cbdt, image_offset); |
| 447 | glyphFormat17.glyphMetrics.get_extents (extents); |
| 448 | } |
| 449 | break; |
| 450 | default: |
| 451 | // TODO: Support other image formats. |
| 452 | return false; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /* Convert to the font units. */ |
| 457 | extents->x_bearing *= upem / (float) x_ppem; |
| 458 | extents->y_bearing *= upem / (float) y_ppem; |
| 459 | extents->width *= upem / (float) x_ppem; |
| 460 | extents->height *= upem / (float) y_ppem; |
| 461 | |
| 462 | return true; |
| 463 | } |
| 464 | |
Ebrahim Byagowi | 8fd5542 | 2018-03-27 16:57:09 +0430 | [diff] [blame] | 465 | inline void dump (void (*callback) (const uint8_t* data, unsigned int length, |
| 466 | unsigned int group, unsigned int gid)) const |
| 467 | { |
| 468 | if (!cblc) |
| 469 | return; // Not a color bitmap font. |
| 470 | |
| 471 | for (unsigned int i = 0; i < cblc->sizeTables.len; ++i) |
| 472 | { |
| 473 | const BitmapSizeTable &sizeTable = cblc->sizeTables[i]; |
| 474 | const IndexSubtableArray &subtable_array = cblc+sizeTable.indexSubtableArrayOffset; |
| 475 | for (unsigned int j = 0; j < sizeTable.numberOfIndexSubtables; ++j) |
| 476 | { |
| 477 | const IndexSubtableRecord &subtable_record = subtable_array.indexSubtablesZ[j]; |
| 478 | for (unsigned int gid = subtable_record.firstGlyphIndex; |
| 479 | gid <= subtable_record.lastGlyphIndex; ++gid) |
| 480 | { |
| 481 | unsigned int image_offset = 0, image_length = 0, image_format = 0; |
| 482 | |
| 483 | if (!subtable_record.get_image_data (gid, |
| 484 | &image_offset, &image_length, &image_format)) |
| 485 | continue; |
| 486 | |
| 487 | switch (image_format) |
| 488 | { |
| 489 | case 17: { |
| 490 | const GlyphBitmapDataFormat17& glyphFormat17 = |
| 491 | StructAtOffset<GlyphBitmapDataFormat17> (this->cbdt, image_offset); |
| 492 | callback ((const uint8_t *) &glyphFormat17.data.array, |
| 493 | glyphFormat17.data.len, i, gid); |
| 494 | } |
| 495 | break; |
| 496 | case 18: { |
| 497 | const GlyphBitmapDataFormat18& glyphFormat18 = |
| 498 | StructAtOffset<GlyphBitmapDataFormat18> (this->cbdt, image_offset); |
| 499 | callback ((const uint8_t *) &glyphFormat18.data.array, |
| 500 | glyphFormat18.data.len, i, gid); |
| 501 | } |
| 502 | break; |
| 503 | case 19: { |
| 504 | const GlyphBitmapDataFormat19& glyphFormat19 = |
| 505 | StructAtOffset<GlyphBitmapDataFormat19> (this->cbdt, image_offset); |
| 506 | callback ((const uint8_t *) &glyphFormat19.data.array, |
| 507 | glyphFormat19.data.len, i, gid); |
| 508 | } |
| 509 | break; |
| 510 | default: |
| 511 | continue; |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
Behdad Esfahbod | c4e18e5 | 2017-11-14 19:47:31 -0800 | [diff] [blame] | 518 | private: |
| 519 | hb_blob_t *cblc_blob; |
| 520 | hb_blob_t *cbdt_blob; |
| 521 | const CBLC *cblc; |
| 522 | const CBDT *cbdt; |
| 523 | |
| 524 | unsigned int cbdt_len; |
| 525 | unsigned int upem; |
| 526 | }; |
| 527 | |
| 528 | |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 529 | protected: |
Behdad Esfahbod | 9e33734 | 2018-03-14 15:52:53 +0100 | [diff] [blame] | 530 | FixedVersion<> version; |
| 531 | HBUINT8 dataZ[VAR]; |
Behdad Esfahbod | 4b58c9e | 2016-12-02 19:25:54 -0800 | [diff] [blame] | 532 | public: |
| 533 | DEFINE_SIZE_ARRAY(4, dataZ); |
Seigo Nonaka | 8318525 | 2016-12-02 15:03:50 -0800 | [diff] [blame] | 534 | }; |
| 535 | |
| 536 | } /* namespace OT */ |
| 537 | |
Ebrahim Byagowi | 0ef6ab2 | 2018-03-04 02:47:26 +0330 | [diff] [blame] | 538 | #endif /* HB_OT_COLOR_CBDT_TABLE_HH */ |