blob: e85e4ad3c7a498664b21be1664b76f2ece312042 [file] [log] [blame]
Seigo Nonaka83185252016-12-02 15:03:50 -08001/*
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 Byagowi0ef6ab22018-03-04 02:47:26 +033027#ifndef HB_OT_COLOR_CBDT_TABLE_HH
28#define HB_OT_COLOR_CBDT_TABLE_HH
Seigo Nonaka83185252016-12-02 15:03:50 -080029
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070030#include "hb-open-type.hh"
Seigo Nonaka83185252016-12-02 15:03:50 -080031
Ebrahim Byagowia02c3ee2018-04-12 13:38:19 +043032/*
33 * CBLC -- Color Bitmap Location
34 * https://docs.microsoft.com/en-us/typography/opentype/spec/cblc
35 * https://docs.microsoft.com/en-us/typography/opentype/spec/eblc
36 * CBDT -- Color Bitmap Data
37 * https://docs.microsoft.com/en-us/typography/opentype/spec/cbdt
38 * https://docs.microsoft.com/en-us/typography/opentype/spec/ebdt
39 */
40#define HB_OT_TAG_CBLC HB_TAG('C','B','L','C')
41#define HB_OT_TAG_CBDT HB_TAG('C','B','D','T')
42
43
Seigo Nonaka83185252016-12-02 15:03:50 -080044namespace OT {
45
46struct SmallGlyphMetrics
47{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033048 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod4b58c9e2016-12-02 19:25:54 -080049 {
50 TRACE_SANITIZE (this);
51 return_trace (c->check_struct (this));
52 }
53
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033054 void get_extents (hb_glyph_extents_t *extents) const
Behdad Esfahbodd495fc52016-12-02 21:36:42 -080055 {
56 extents->x_bearing = bearingX;
57 extents->y_bearing = bearingY;
58 extents->width = width;
Behdad Esfahbod9db7a7d2019-01-28 14:09:59 -050059 extents->height = - (hb_position_t) height;
Behdad Esfahbodd495fc52016-12-02 21:36:42 -080060 }
61
Behdad Esfahbod9e337342018-03-14 15:52:53 +010062 HBUINT8 height;
63 HBUINT8 width;
64 HBINT8 bearingX;
65 HBINT8 bearingY;
66 HBUINT8 advance;
67 public:
Seigo Nonaka83185252016-12-02 15:03:50 -080068 DEFINE_SIZE_STATIC(5);
69};
70
Behdad Esfahbode4bfccf2016-12-04 19:43:33 -080071struct BigGlyphMetrics : SmallGlyphMetrics
72{
Behdad Esfahbod9e337342018-03-14 15:52:53 +010073 HBINT8 vertBearingX;
74 HBINT8 vertBearingY;
75 HBUINT8 vertAdvance;
76 public:
Behdad Esfahbode4bfccf2016-12-04 19:43:33 -080077 DEFINE_SIZE_STATIC(8);
78};
79
Behdad Esfahbod4b58c9e2016-12-02 19:25:54 -080080struct SBitLineMetrics
81{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033082 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod4b58c9e2016-12-02 19:25:54 -080083 {
84 TRACE_SANITIZE (this);
85 return_trace (c->check_struct (this));
86 }
87
Behdad Esfahbod9e337342018-03-14 15:52:53 +010088 HBINT8 ascender;
89 HBINT8 decender;
90 HBUINT8 widthMax;
91 HBINT8 caretSlopeNumerator;
92 HBINT8 caretSlopeDenominator;
93 HBINT8 caretOffset;
94 HBINT8 minOriginSB;
95 HBINT8 minAdvanceSB;
96 HBINT8 maxBeforeBL;
97 HBINT8 minAfterBL;
98 HBINT8 padding1;
99 HBINT8 padding2;
100 public:
Seigo Nonaka83185252016-12-02 15:03:50 -0800101 DEFINE_SIZE_STATIC(12);
102};
103
Behdad Esfahbod9eda74c2016-12-04 19:12:52 -0800104
Seigo Nonaka83185252016-12-02 15:03:50 -0800105/*
106 * Index Subtables.
107 */
Behdad Esfahbod9eda74c2016-12-04 19:12:52 -0800108
Behdad Esfahbodd495fc52016-12-02 21:36:42 -0800109struct IndexSubtableHeader
Seigo Nonaka83185252016-12-02 15:03:50 -0800110{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330111 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodd495fc52016-12-02 21:36:42 -0800112 {
113 TRACE_SANITIZE (this);
114 return_trace (c->check_struct (this));
115 }
Seigo Nonaka83185252016-12-02 15:03:50 -0800116
Behdad Esfahbod9e337342018-03-14 15:52:53 +0100117 HBUINT16 indexFormat;
118 HBUINT16 imageFormat;
119 HBUINT32 imageDataOffset;
120 public:
Behdad Esfahbod4b58c9e2016-12-02 19:25:54 -0800121 DEFINE_SIZE_STATIC(8);
Seigo Nonaka83185252016-12-02 15:03:50 -0800122};
123
Behdad Esfahboda141d1d2016-12-04 19:26:54 -0800124template <typename OffsetType>
125struct IndexSubtableFormat1Or3
Seigo Nonaka83185252016-12-02 15:03:50 -0800126{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330127 bool sanitize (hb_sanitize_context_t *c, unsigned int glyph_count) const
Behdad Esfahbodd495fc52016-12-02 21:36:42 -0800128 {
129 TRACE_SANITIZE (this);
130 return_trace (c->check_struct (this) &&
Behdad Esfahbodda744c62018-10-14 20:49:21 -0700131 offsetArrayZ.sanitize (c, glyph_count + 1));
Behdad Esfahbodd495fc52016-12-02 21:36:42 -0800132 }
133
134 bool get_image_data (unsigned int idx,
135 unsigned int *offset,
136 unsigned int *length) const
137 {
138 if (unlikely (offsetArrayZ[idx + 1] <= offsetArrayZ[idx]))
139 return false;
140
141 *offset = header.imageDataOffset + offsetArrayZ[idx];
142 *length = offsetArrayZ[idx + 1] - offsetArrayZ[idx];
143 return true;
144 }
145
Behdad Esfahbod9e337342018-03-14 15:52:53 +0100146 IndexSubtableHeader header;
Ebrahim Byagowi92588782019-04-30 13:05:10 -0700147 UnsizedArrayOf<Offset<OffsetType>>
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200148 offsetArrayZ;
Behdad Esfahbod9e337342018-03-14 15:52:53 +0100149 public:
Behdad Esfahbod4b58c9e2016-12-02 19:25:54 -0800150 DEFINE_SIZE_ARRAY(8, offsetArrayZ);
Seigo Nonaka83185252016-12-02 15:03:50 -0800151};
152
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100153struct IndexSubtableFormat1 : IndexSubtableFormat1Or3<HBUINT32> {};
154struct IndexSubtableFormat3 : IndexSubtableFormat1Or3<HBUINT16> {};
Behdad Esfahboda141d1d2016-12-04 19:26:54 -0800155
Behdad Esfahbodd495fc52016-12-02 21:36:42 -0800156struct IndexSubtable
157{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330158 bool sanitize (hb_sanitize_context_t *c, unsigned int glyph_count) const
Behdad Esfahbodd495fc52016-12-02 21:36:42 -0800159 {
160 TRACE_SANITIZE (this);
161 if (!u.header.sanitize (c)) return_trace (false);
162 switch (u.header.indexFormat) {
163 case 1: return_trace (u.format1.sanitize (c, glyph_count));
Behdad Esfahboda141d1d2016-12-04 19:26:54 -0800164 case 3: return_trace (u.format3.sanitize (c, glyph_count));
Behdad Esfahbodd495fc52016-12-02 21:36:42 -0800165 default:return_trace (true);
166 }
167 }
168
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330169 bool get_extents (hb_glyph_extents_t *extents HB_UNUSED) const
Behdad Esfahbodd495fc52016-12-02 21:36:42 -0800170 {
171 switch (u.header.indexFormat) {
172 case 2: case 5: /* TODO */
173 case 1: case 3: case 4: /* Variable-metrics formats do not have metrics here. */
174 default:return (false);
175 }
176 }
177
178 bool get_image_data (unsigned int idx,
179 unsigned int *offset,
180 unsigned int *length,
181 unsigned int *format) const
182 {
183 *format = u.header.imageFormat;
184 switch (u.header.indexFormat) {
185 case 1: return u.format1.get_image_data (idx, offset, length);
Behdad Esfahboda141d1d2016-12-04 19:26:54 -0800186 case 3: return u.format3.get_image_data (idx, offset, length);
Behdad Esfahbodd495fc52016-12-02 21:36:42 -0800187 default: return false;
188 }
189 }
190
191 protected:
192 union {
193 IndexSubtableHeader header;
194 IndexSubtableFormat1 format1;
Behdad Esfahboda141d1d2016-12-04 19:26:54 -0800195 IndexSubtableFormat3 format3;
Behdad Esfahbode4bfccf2016-12-04 19:43:33 -0800196 /* TODO: Format 2, 4, 5. */
Behdad Esfahbodd495fc52016-12-02 21:36:42 -0800197 } u;
198 public:
199 DEFINE_SIZE_UNION (8, header);
200};
201
202struct IndexSubtableRecord
203{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330204 bool sanitize (hb_sanitize_context_t *c, const void *base) const
Behdad Esfahbodd495fc52016-12-02 21:36:42 -0800205 {
206 TRACE_SANITIZE (this);
207 return_trace (c->check_struct (this) &&
208 firstGlyphIndex <= lastGlyphIndex &&
Behdad Esfahbod6aee3bb2018-10-14 21:08:42 -0700209 offsetToSubtable.sanitize (c, base, lastGlyphIndex - firstGlyphIndex + 1));
Behdad Esfahbodd495fc52016-12-02 21:36:42 -0800210 }
211
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330212 bool get_extents (hb_glyph_extents_t *extents,
213 const void *base) const
Behdad Esfahbodd495fc52016-12-02 21:36:42 -0800214 {
Behdad Esfahbod4c27da72018-10-15 19:46:45 -0700215 return (base+offsetToSubtable).get_extents (extents);
Behdad Esfahbodd495fc52016-12-02 21:36:42 -0800216 }
217
Behdad Esfahbodfc812fa2018-10-14 21:32:09 -0700218 bool get_image_data (unsigned int gid,
219 const void *base,
Behdad Esfahbodd495fc52016-12-02 21:36:42 -0800220 unsigned int *offset,
221 unsigned int *length,
222 unsigned int *format) const
223 {
Behdad Esfahbodfc812fa2018-10-14 21:32:09 -0700224 if (gid < firstGlyphIndex || gid > lastGlyphIndex) return false;
225 return (base+offsetToSubtable).get_image_data (gid - firstGlyphIndex,
Behdad Esfahbodd495fc52016-12-02 21:36:42 -0800226 offset, length, format);
227 }
228
Behdad Esfahbod9e337342018-03-14 15:52:53 +0100229 GlyphID firstGlyphIndex;
230 GlyphID lastGlyphIndex;
231 LOffsetTo<IndexSubtable> offsetToSubtable;
232 public:
Behdad Esfahbodd495fc52016-12-02 21:36:42 -0800233 DEFINE_SIZE_STATIC(8);
234};
235
Seigo Nonaka83185252016-12-02 15:03:50 -0800236struct IndexSubtableArray
237{
Ebrahim Byagowi8fd55422018-03-27 16:57:09 +0430238 friend struct CBDT;
239
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330240 bool sanitize (hb_sanitize_context_t *c, unsigned int count) const
Behdad Esfahbodce09e902016-12-02 20:12:57 -0800241 {
242 TRACE_SANITIZE (this);
Behdad Esfahbod2995b442018-10-14 20:37:57 -0700243 return_trace (indexSubtablesZ.sanitize (c, count, this));
Behdad Esfahbodce09e902016-12-02 20:12:57 -0800244 }
245
Seigo Nonaka83185252016-12-02 15:03:50 -0800246 public:
Behdad Esfahbodd495fc52016-12-02 21:36:42 -0800247 const IndexSubtableRecord* find_table (hb_codepoint_t glyph, unsigned int numTables) const
Seigo Nonaka83185252016-12-02 15:03:50 -0800248 {
Behdad Esfahbod4b58c9e2016-12-02 19:25:54 -0800249 for (unsigned int i = 0; i < numTables; ++i)
250 {
251 unsigned int firstGlyphIndex = indexSubtablesZ[i].firstGlyphIndex;
252 unsigned int lastGlyphIndex = indexSubtablesZ[i].lastGlyphIndex;
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200253 if (firstGlyphIndex <= glyph && glyph <= lastGlyphIndex)
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430254 return &indexSubtablesZ[i];
Seigo Nonaka83185252016-12-02 15:03:50 -0800255 }
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200256 return nullptr;
Seigo Nonaka83185252016-12-02 15:03:50 -0800257 }
258
259 protected:
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200260 UnsizedArrayOf<IndexSubtableRecord> indexSubtablesZ;
Behdad Esfahbodce09e902016-12-02 20:12:57 -0800261};
262
263struct BitmapSizeTable
264{
Behdad Esfahbod9eda74c2016-12-04 19:12:52 -0800265 friend struct CBLC;
Ebrahim Byagowi8fd55422018-03-27 16:57:09 +0430266 friend struct CBDT;
Behdad Esfahbod9eda74c2016-12-04 19:12:52 -0800267
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330268 bool sanitize (hb_sanitize_context_t *c, const void *base) const
Behdad Esfahbodce09e902016-12-02 20:12:57 -0800269 {
270 TRACE_SANITIZE (this);
271 return_trace (c->check_struct (this) &&
272 indexSubtableArrayOffset.sanitize (c, base, numberOfIndexSubtables) &&
Behdad Esfahbodce09e902016-12-02 20:12:57 -0800273 horizontal.sanitize (c) &&
274 vertical.sanitize (c));
275 }
276
Behdad Esfahbodfc812fa2018-10-14 21:32:09 -0700277 const IndexSubtableRecord *find_table (hb_codepoint_t glyph,
278 const void *base,
279 const void **out_base) const
Behdad Esfahbod9eda74c2016-12-04 19:12:52 -0800280 {
Behdad Esfahbodfc812fa2018-10-14 21:32:09 -0700281 *out_base = &(base+indexSubtableArrayOffset);
Behdad Esfahbod9eda74c2016-12-04 19:12:52 -0800282 return (base+indexSubtableArrayOffset).find_table (glyph, numberOfIndexSubtables);
283 }
284
285 protected:
Behdad Esfahbodb1152d52019-01-17 18:17:04 -0500286 LNNOffsetTo<IndexSubtableArray>
Behdad Esfahbod9e337342018-03-14 15:52:53 +0100287 indexSubtableArrayOffset;
288 HBUINT32 indexTablesSize;
289 HBUINT32 numberOfIndexSubtables;
290 HBUINT32 colorRef;
291 SBitLineMetrics horizontal;
292 SBitLineMetrics vertical;
293 GlyphID startGlyphIndex;
294 GlyphID endGlyphIndex;
295 HBUINT8 ppemX;
296 HBUINT8 ppemY;
297 HBUINT8 bitDepth;
298 HBINT8 flags;
Behdad Esfahbodf00a94e2017-11-14 19:54:48 -0800299 public:
Behdad Esfahbodce09e902016-12-02 20:12:57 -0800300 DEFINE_SIZE_STATIC(48);
Seigo Nonaka83185252016-12-02 15:03:50 -0800301};
302
Behdad Esfahbod9eda74c2016-12-04 19:12:52 -0800303
304/*
305 * Glyph Bitmap Data Formats.
306 */
307
308struct GlyphBitmapDataFormat17
309{
Ebrahim Byagowif8bb5822018-04-11 17:13:20 +0430310 SmallGlyphMetrics glyphMetrics;
311 LArrayOf<HBUINT8> data;
Behdad Esfahbod9e337342018-03-14 15:52:53 +0100312 public:
313 DEFINE_SIZE_ARRAY(9, data);
Behdad Esfahbod9eda74c2016-12-04 19:12:52 -0800314};
315
Ebrahim Byagowi8fd55422018-03-27 16:57:09 +0430316struct GlyphBitmapDataFormat18
317{
Ebrahim Byagowif8bb5822018-04-11 17:13:20 +0430318 BigGlyphMetrics glyphMetrics;
319 LArrayOf<HBUINT8> data;
Ebrahim Byagowi8fd55422018-03-27 16:57:09 +0430320 public:
321 DEFINE_SIZE_ARRAY(12, data);
322};
323
324struct GlyphBitmapDataFormat19
325{
Ebrahim Byagowif8bb5822018-04-11 17:13:20 +0430326 LArrayOf<HBUINT8> data;
Ebrahim Byagowi8fd55422018-03-27 16:57:09 +0430327 public:
328 DEFINE_SIZE_ARRAY(4, data);
329};
330
Seigo Nonaka83185252016-12-02 15:03:50 -0800331struct CBLC
332{
Behdad Esfahbodf00a94e2017-11-14 19:54:48 -0800333 friend struct CBDT;
334
Behdad Esfahbodef006542019-01-22 12:08:57 +0100335 static constexpr hb_tag_t tableTag = HB_OT_TAG_CBLC;
Seigo Nonaka83185252016-12-02 15:03:50 -0800336
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330337 bool sanitize (hb_sanitize_context_t *c) const
Seigo Nonaka83185252016-12-02 15:03:50 -0800338 {
339 TRACE_SANITIZE (this);
Behdad Esfahbod4b58c9e2016-12-02 19:25:54 -0800340 return_trace (c->check_struct (this) &&
341 likely (version.major == 2 || version.major == 3) &&
Behdad Esfahbodce09e902016-12-02 20:12:57 -0800342 sizeTables.sanitize (c, this));
Seigo Nonaka83185252016-12-02 15:03:50 -0800343 }
344
Behdad Esfahbodf00a94e2017-11-14 19:54:48 -0800345 protected:
Behdad Esfahbod8cffee02018-10-28 23:07:59 -0700346 const BitmapSizeTable &choose_strike (hb_font_t *font) const
Seigo Nonaka83185252016-12-02 15:03:50 -0800347 {
Behdad Esfahbod5eb251a2018-10-28 23:16:13 -0700348 unsigned count = sizeTables.len;
349 if (unlikely (!count))
350 return Null(BitmapSizeTable);
Behdad Esfahbod9eda74c2016-12-04 19:12:52 -0800351
Behdad Esfahbod41248cc2019-05-07 20:54:31 -0700352 unsigned int requested_ppem = hb_max (font->x_ppem, font->y_ppem);
Behdad Esfahbod5eb251a2018-10-28 23:16:13 -0700353 if (!requested_ppem)
354 requested_ppem = 1<<30; /* Choose largest strike. */
355 unsigned int best_i = 0;
Behdad Esfahbod41248cc2019-05-07 20:54:31 -0700356 unsigned int best_ppem = hb_max (sizeTables[0].ppemX, sizeTables[0].ppemY);
Behdad Esfahbod9eda74c2016-12-04 19:12:52 -0800357
Behdad Esfahbod5eb251a2018-10-28 23:16:13 -0700358 for (unsigned int i = 1; i < count; i++)
359 {
Behdad Esfahbod41248cc2019-05-07 20:54:31 -0700360 unsigned int ppem = hb_max (sizeTables[i].ppemX, sizeTables[i].ppemY);
Behdad Esfahbod5eb251a2018-10-28 23:16:13 -0700361 if ((requested_ppem <= ppem && ppem < best_ppem) ||
362 (requested_ppem > best_ppem && ppem > best_ppem))
363 {
364 best_i = i;
365 best_ppem = ppem;
366 }
367 }
368
369 return sizeTables[best_i];
Seigo Nonaka83185252016-12-02 15:03:50 -0800370 }
371
372 protected:
Behdad Esfahbodb732c532017-01-22 20:31:53 -0800373 FixedVersion<> version;
374 LArrayOf<BitmapSizeTable> sizeTables;
Behdad Esfahbod4b58c9e2016-12-02 19:25:54 -0800375 public:
376 DEFINE_SIZE_ARRAY(8, sizeTables);
Seigo Nonaka83185252016-12-02 15:03:50 -0800377};
378
Seigo Nonaka83185252016-12-02 15:03:50 -0800379struct CBDT
380{
Behdad Esfahbodef006542019-01-22 12:08:57 +0100381 static constexpr hb_tag_t tableTag = HB_OT_TAG_CBDT;
Seigo Nonaka83185252016-12-02 15:03:50 -0800382
Behdad Esfahbodc4e18e52017-11-14 19:47:31 -0800383 struct accelerator_t
384 {
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330385 void init (hb_face_t *face)
Behdad Esfahbodc4e18e52017-11-14 19:47:31 -0800386 {
Behdad Esfahbod0b0fad32018-11-11 00:26:55 -0500387 cblc = hb_sanitize_context_t().reference_table<CBLC> (face);
388 cbdt = hb_sanitize_context_t().reference_table<CBDT> (face);
389
Behdad Esfahbod96f1f522018-02-12 18:48:51 -0800390 upem = hb_face_get_upem (face);
Behdad Esfahbodc4e18e52017-11-14 19:47:31 -0800391 }
392
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330393 void fini ()
Behdad Esfahbodc4e18e52017-11-14 19:47:31 -0800394 {
Behdad Esfahbodda6aa3b2018-11-11 11:40:57 -0500395 this->cblc.destroy ();
396 this->cbdt.destroy ();
Behdad Esfahbodc4e18e52017-11-14 19:47:31 -0800397 }
398
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330399 bool get_extents (hb_font_t *font, hb_codepoint_t glyph,
400 hb_glyph_extents_t *extents) const
Behdad Esfahbodc4e18e52017-11-14 19:47:31 -0800401 {
Behdad Esfahbodfc812fa2018-10-14 21:32:09 -0700402 const void *base;
Behdad Esfahbod8cffee02018-10-28 23:07:59 -0700403 const BitmapSizeTable &strike = this->cblc->choose_strike (font);
Behdad Esfahbode2ba96d2018-10-28 23:01:57 -0700404 const IndexSubtableRecord *subtable_record = strike.find_table (glyph, cblc, &base);
Behdad Esfahbod8cffee02018-10-28 23:07:59 -0700405 if (!subtable_record || !strike.ppemX || !strike.ppemY)
Behdad Esfahbodc4e18e52017-11-14 19:47:31 -0800406 return false;
407
Behdad Esfahbod4c27da72018-10-15 19:46:45 -0700408 if (subtable_record->get_extents (extents, base))
Behdad Esfahbodc4e18e52017-11-14 19:47:31 -0800409 return true;
410
411 unsigned int image_offset = 0, image_length = 0, image_format = 0;
Behdad Esfahbodfc812fa2018-10-14 21:32:09 -0700412 if (!subtable_record->get_image_data (glyph, base, &image_offset, &image_length, &image_format))
Behdad Esfahbodc4e18e52017-11-14 19:47:31 -0800413 return false;
414
415 {
Behdad Esfahbod0b0fad32018-11-11 00:26:55 -0500416 unsigned int cbdt_len = cbdt.get_length ();
Behdad Esfahbodc4e18e52017-11-14 19:47:31 -0800417 if (unlikely (image_offset > cbdt_len || cbdt_len - image_offset < image_length))
418 return false;
419
420 switch (image_format)
421 {
422 case 17: {
423 if (unlikely (image_length < GlyphBitmapDataFormat17::min_size))
424 return false;
Behdad Esfahbodc4e18e52017-11-14 19:47:31 -0800425 const GlyphBitmapDataFormat17& glyphFormat17 =
426 StructAtOffset<GlyphBitmapDataFormat17> (this->cbdt, image_offset);
427 glyphFormat17.glyphMetrics.get_extents (extents);
Behdad Esfahboddc9bd292018-10-30 13:16:07 -0700428 break;
Behdad Esfahbodc4e18e52017-11-14 19:47:31 -0800429 }
Behdad Esfahboddc9bd292018-10-30 13:16:07 -0700430 case 18: {
431 if (unlikely (image_length < GlyphBitmapDataFormat18::min_size))
432 return false;
433 const GlyphBitmapDataFormat18& glyphFormat18 =
434 StructAtOffset<GlyphBitmapDataFormat18> (this->cbdt, image_offset);
435 glyphFormat18.glyphMetrics.get_extents (extents);
436 break;
437 }
Behdad Esfahbodc4e18e52017-11-14 19:47:31 -0800438 default:
439 // TODO: Support other image formats.
440 return false;
441 }
442 }
443
Behdad Esfahbode998fb92018-10-28 22:45:53 -0700444 /* Convert to font units. */
Behdad Esfahboddfc57802019-05-07 23:26:09 -0700445 float x_scale = upem / (float) strike.ppemX;
446 float y_scale = upem / (float) strike.ppemY;
447 extents->x_bearing = roundf (extents->x_bearing * x_scale);
448 extents->y_bearing = roundf (extents->y_bearing * y_scale);
449 extents->width = roundf (extents->width * x_scale);
450 extents->height = roundf (extents->height * y_scale);
Behdad Esfahbodc4e18e52017-11-14 19:47:31 -0800451
452 return true;
453 }
454
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330455 hb_blob_t* reference_png (hb_font_t *font,
Behdad Esfahbode2ba96d2018-10-28 23:01:57 -0700456 hb_codepoint_t glyph) const
Ebrahim Byagowi265ad402018-10-26 23:55:11 +0330457 {
Ebrahim Byagowi9435fb22018-10-27 13:32:14 +0330458 const void *base;
Behdad Esfahbod8cffee02018-10-28 23:07:59 -0700459 const BitmapSizeTable &strike = this->cblc->choose_strike (font);
Behdad Esfahbode2ba96d2018-10-28 23:01:57 -0700460 const IndexSubtableRecord *subtable_record = strike.find_table (glyph, cblc, &base);
Behdad Esfahbod8cffee02018-10-28 23:07:59 -0700461 if (!subtable_record || !strike.ppemX || !strike.ppemY)
Ebrahim Byagowi9435fb22018-10-27 13:32:14 +0330462 return hb_blob_get_empty ();
463
464 unsigned int image_offset = 0, image_length = 0, image_format = 0;
Behdad Esfahbode2ba96d2018-10-28 23:01:57 -0700465 if (!subtable_record->get_image_data (glyph, base, &image_offset, &image_length, &image_format))
Ebrahim Byagowi9435fb22018-10-27 13:32:14 +0330466 return hb_blob_get_empty ();
467
Ebrahim Byagowi9435fb22018-10-27 13:32:14 +0330468 {
Behdad Esfahbod0b0fad32018-11-11 00:26:55 -0500469 unsigned int cbdt_len = cbdt.get_length ();
Behdad Esfahboda2a74222018-10-30 13:14:56 -0700470 if (unlikely (image_offset > cbdt_len || cbdt_len - image_offset < image_length))
471 return hb_blob_get_empty ();
472
473 switch (image_format)
474 {
475 case 17: {
476 if (unlikely (image_length < GlyphBitmapDataFormat17::min_size))
477 return hb_blob_get_empty ();
478 const GlyphBitmapDataFormat17& glyphFormat17 =
479 StructAtOffset<GlyphBitmapDataFormat17> (this->cbdt, image_offset);
Behdad Esfahbod0b0fad32018-11-11 00:26:55 -0500480 return hb_blob_create_sub_blob (cbdt.get_blob (),
Behdad Esfahboda2a74222018-10-30 13:14:56 -0700481 image_offset + GlyphBitmapDataFormat17::min_size,
482 glyphFormat17.data.len);
483 }
484 case 18: {
485 if (unlikely (image_length < GlyphBitmapDataFormat18::min_size))
486 return hb_blob_get_empty ();
487 const GlyphBitmapDataFormat18& glyphFormat18 =
488 StructAtOffset<GlyphBitmapDataFormat18> (this->cbdt, image_offset);
Behdad Esfahbod0b0fad32018-11-11 00:26:55 -0500489 return hb_blob_create_sub_blob (cbdt.get_blob (),
Behdad Esfahboda2a74222018-10-30 13:14:56 -0700490 image_offset + GlyphBitmapDataFormat18::min_size,
491 glyphFormat18.data.len);
492 }
493 case 19: {
494 if (unlikely (image_length < GlyphBitmapDataFormat19::min_size))
495 return hb_blob_get_empty ();
496 const GlyphBitmapDataFormat19& glyphFormat19 =
497 StructAtOffset<GlyphBitmapDataFormat19> (this->cbdt, image_offset);
Behdad Esfahbod0b0fad32018-11-11 00:26:55 -0500498 return hb_blob_create_sub_blob (cbdt.get_blob (),
Behdad Esfahboda2a74222018-10-30 13:14:56 -0700499 image_offset + GlyphBitmapDataFormat19::min_size,
500 glyphFormat19.data.len);
501 }
502 }
Ebrahim Byagowi9435fb22018-10-27 13:32:14 +0330503 }
Behdad Esfahboda2a74222018-10-30 13:14:56 -0700504
Ebrahim Byagowi6a38fd62018-10-27 10:50:53 +0330505 return hb_blob_get_empty ();
Ebrahim Byagowi265ad402018-10-26 23:55:11 +0330506 }
507
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330508 bool has_data () const { return cbdt.get_length (); }
Ebrahim Byagowi265ad402018-10-26 23:55:11 +0330509
Behdad Esfahbodc4e18e52017-11-14 19:47:31 -0800510 private:
Behdad Esfahbod0b0fad32018-11-11 00:26:55 -0500511 hb_blob_ptr_t<CBLC> cblc;
512 hb_blob_ptr_t<CBDT> cbdt;
Behdad Esfahbodc4e18e52017-11-14 19:47:31 -0800513
Behdad Esfahbodc4e18e52017-11-14 19:47:31 -0800514 unsigned int upem;
515 };
516
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330517 bool sanitize (hb_sanitize_context_t *c) const
Ebrahim Byagowi265ad402018-10-26 23:55:11 +0330518 {
519 TRACE_SANITIZE (this);
520 return_trace (c->check_struct (this) &&
521 likely (version.major == 2 || version.major == 3));
522 }
Behdad Esfahbodc4e18e52017-11-14 19:47:31 -0800523
Seigo Nonaka83185252016-12-02 15:03:50 -0800524 protected:
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200525 FixedVersion<> version;
526 UnsizedArrayOf<HBUINT8> dataZ;
Behdad Esfahbod4b58c9e2016-12-02 19:25:54 -0800527 public:
528 DEFINE_SIZE_ARRAY(4, dataZ);
Seigo Nonaka83185252016-12-02 15:03:50 -0800529};
530
Behdad Esfahbod3a0b3a22018-08-26 15:11:24 -0700531struct CBDT_accelerator_t : CBDT::accelerator_t {};
532
Seigo Nonaka83185252016-12-02 15:03:50 -0800533} /* namespace OT */
534
Ebrahim Byagowi0ef6ab22018-03-04 02:47:26 +0330535#endif /* HB_OT_COLOR_CBDT_TABLE_HH */