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, Calder Kitagawa
25 */
26
27#ifndef HB_OT_COLOR_CBDT_TABLE_HH
28#define HB_OT_COLOR_CBDT_TABLE_HH
29
30#include "hb-open-type.hh"
31
32/*
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
44namespace OT {
45
46struct cblc_bitmap_size_subset_context_t
47{
48 const char *cbdt;
49 unsigned int cbdt_length;
50 hb_vector_t<char> *cbdt_prime;
51 unsigned int size; /* INOUT
52 * Input: old size of IndexSubtable
53 * Output: new size of IndexSubtable
54 */
55 unsigned int num_tables; /* INOUT
56 * Input: old number of subtables.
57 * Output: new number of subtables.
58 */
59 hb_codepoint_t start_glyph; /* OUT */
60 hb_codepoint_t end_glyph; /* OUT */
61};
62
63static inline bool
64_copy_data_to_cbdt (hb_vector_t<char> *cbdt_prime,
65 const void *data,
66 unsigned length)
67{
68 unsigned int new_len = cbdt_prime->length + length;
69 if (unlikely (!cbdt_prime->alloc (new_len))) return false;
70 memcpy (cbdt_prime->arrayZ + cbdt_prime->length, data, length);
71 cbdt_prime->length = new_len;
72 return true;
73}
74
75struct SmallGlyphMetrics
76{
77 bool sanitize (hb_sanitize_context_t *c) const
78 {
79 TRACE_SANITIZE (this);
80 return_trace (c->check_struct (this));
81 }
82
83 void get_extents (hb_font_t *font, hb_glyph_extents_t *extents) const
84 {
85 extents->x_bearing = font->em_scale_x (bearingX);
86 extents->y_bearing = font->em_scale_y (bearingY);
87 extents->width = font->em_scale_x (width);
88 extents->height = font->em_scale_y (-static_cast<int>(height));
89 }
90
91 HBUINT8 height;
92 HBUINT8 width;
93 HBINT8 bearingX;
94 HBINT8 bearingY;
95 HBUINT8 advance;
96 public:
97 DEFINE_SIZE_STATIC (5);
98};
99
100struct BigGlyphMetrics : SmallGlyphMetrics
101{
102 HBINT8 vertBearingX;
103 HBINT8 vertBearingY;
104 HBUINT8 vertAdvance;
105 public:
106 DEFINE_SIZE_STATIC (8);
107};
108
109struct SBitLineMetrics
110{
111 bool sanitize (hb_sanitize_context_t *c) const
112 {
113 TRACE_SANITIZE (this);
114 return_trace (c->check_struct (this));
115 }
116
117 HBINT8 ascender;
118 HBINT8 decender;
119 HBUINT8 widthMax;
120 HBINT8 caretSlopeNumerator;
121 HBINT8 caretSlopeDenominator;
122 HBINT8 caretOffset;
123 HBINT8 minOriginSB;
124 HBINT8 minAdvanceSB;
125 HBINT8 maxBeforeBL;
126 HBINT8 minAfterBL;
127 HBINT8 padding1;
128 HBINT8 padding2;
129 public:
130 DEFINE_SIZE_STATIC (12);
131};
132
133
134/*
135 * Index Subtables.
136 */
137
138struct IndexSubtableHeader
139{
140 bool sanitize (hb_sanitize_context_t *c) const
141 {
142 TRACE_SANITIZE (this);
143 return_trace (c->check_struct (this));
144 }
145
146 HBUINT16 indexFormat;
147 HBUINT16 imageFormat;
148 HBUINT32 imageDataOffset;
149 public:
150 DEFINE_SIZE_STATIC (8);
151};
152
153template <typename OffsetType>
154struct IndexSubtableFormat1Or3
155{
156 bool sanitize (hb_sanitize_context_t *c, unsigned int glyph_count) const
157 {
158 TRACE_SANITIZE (this);
159 return_trace (c->check_struct (this) &&
160 offsetArrayZ.sanitize (c, glyph_count + 1));
161 }
162
163 bool get_image_data (unsigned int idx,
164 unsigned int *offset,
165 unsigned int *length) const
166 {
167 if (unlikely (offsetArrayZ[idx + 1] <= offsetArrayZ[idx]))
168 return false;
169
170 *offset = header.imageDataOffset + offsetArrayZ[idx];
171 *length = offsetArrayZ[idx + 1] - offsetArrayZ[idx];
172 return true;
173 }
174
175 bool add_offset (hb_serialize_context_t *c,
176 unsigned int offset,
177 unsigned int *size /* OUT (accumulated) */)
178 {
179 TRACE_SERIALIZE (this);
180 Offset<OffsetType> embedded_offset;
181 embedded_offset = offset;
182 *size += sizeof (OffsetType);
183 auto *o = c->embed (embedded_offset);
184 return_trace ((bool) o);
185 }
186
187 IndexSubtableHeader header;
188 UnsizedArrayOf<Offset<OffsetType>>
189 offsetArrayZ;
190 public:
191 DEFINE_SIZE_ARRAY (8, offsetArrayZ);
192};
193
194struct IndexSubtableFormat1 : IndexSubtableFormat1Or3<HBUINT32> {};
195struct IndexSubtableFormat3 : IndexSubtableFormat1Or3<HBUINT16> {};
196
197struct IndexSubtable
198{
199 bool sanitize (hb_sanitize_context_t *c, unsigned int glyph_count) const
200 {
201 TRACE_SANITIZE (this);
202 if (!u.header.sanitize (c)) return_trace (false);
203 switch (u.header.indexFormat)
204 {
205 case 1: return_trace (u.format1.sanitize (c, glyph_count));
206 case 3: return_trace (u.format3.sanitize (c, glyph_count));
207 default:return_trace (true);
208 }
209 }
210
211 bool
212 finish_subtable (hb_serialize_context_t *c,
213 unsigned int cbdt_prime_len,
214 unsigned int num_glyphs,
215 unsigned int *size /* OUT (accumulated) */)
216 {
217 TRACE_SERIALIZE (this);
218
219 unsigned int local_offset = cbdt_prime_len - u.header.imageDataOffset;
220 switch (u.header.indexFormat)
221 {
222 case 1: return_trace (u.format1.add_offset (c, local_offset, size));
223 case 3: {
224 if (!u.format3.add_offset (c, local_offset, size))
225 return_trace (false);
226 if (!(num_glyphs & 0x01)) // Pad to 32-bit alignment if needed.
227 return_trace (u.format3.add_offset (c, 0, size));
228 return_trace (true);
229 }
230 // TODO: implement 2, 4, 5.
231 case 2: case 4: // No-op.
232 case 5: // Pad to 32-bit aligned.
233 default: return_trace (false);
234 }
235 }
236
237 bool
238 fill_missing_glyphs (hb_serialize_context_t *c,
239 unsigned int cbdt_prime_len,
240 unsigned int num_missing,
241 unsigned int *size /* OUT (accumulated) */,
242 unsigned int *num_glyphs /* OUT (accumulated) */)
243 {
244 TRACE_SERIALIZE (this);
245
246 unsigned int local_offset = cbdt_prime_len - u.header.imageDataOffset;
247 switch (u.header.indexFormat)
248 {
249 case 1: {
250 for (unsigned int i = 0; i < num_missing; i++)
251 {
252 if (unlikely (!u.format1.add_offset (c, local_offset, size)))
253 return_trace (false);
254 *num_glyphs += 1;
255 }
256 return_trace (true);
257 }
258 case 3: {
259 for (unsigned int i = 0; i < num_missing; i++)
260 {
261 if (unlikely (!u.format3.add_offset (c, local_offset, size)))
262 return_trace (false);
263 *num_glyphs += 1;
264 }
265 return_trace (true);
266 }
267 // TODO: implement 2, 4, 5.
268 case 2: // Add empty space in cbdt_prime?.
269 case 4: case 5: // No-op as sparse is supported.
270 default: return_trace (false);
271 }
272 }
273
274 bool
275 copy_glyph_at_idx (hb_serialize_context_t *c, unsigned int idx,
276 const char *cbdt, unsigned int cbdt_length,
277 hb_vector_t<char> *cbdt_prime /* INOUT */,
278 IndexSubtable *subtable_prime /* INOUT */,
279 unsigned int *size /* OUT (accumulated) */) const
280 {
281 TRACE_SERIALIZE (this);
282
283 unsigned int offset, length, format;
284 if (unlikely (!get_image_data (idx, &offset, &length, &format))) return_trace (false);
285 if (unlikely (offset > cbdt_length || cbdt_length - offset < length)) return_trace (false);
286
287 auto *header_prime = subtable_prime->get_header ();
288 unsigned int new_local_offset = cbdt_prime->length - (unsigned int) header_prime->imageDataOffset;
289 if (unlikely (!_copy_data_to_cbdt (cbdt_prime, cbdt + offset, length))) return_trace (false);
290
291 return_trace (subtable_prime->add_offset (c, new_local_offset, size));
292 }
293
294 bool
295 add_offset (hb_serialize_context_t *c, unsigned int local_offset,
296 unsigned int *size /* OUT (accumulated) */)
297 {
298 TRACE_SERIALIZE (this);
299 switch (u.header.indexFormat)
300 {
301 case 1: return_trace (u.format1.add_offset (c, local_offset, size));
302 case 3: return_trace (u.format3.add_offset (c, local_offset, size));
303 // TODO: Implement tables 2, 4, 5
304 case 2: // Should be a no-op.
305 case 4: case 5: // Handle sparse cases.
306 default: return_trace (false);
307 }
308 }
309
310 bool get_extents (hb_glyph_extents_t *extents HB_UNUSED) const
311 {
312 switch (u.header.indexFormat)
313 {
314 case 2: case 5: /* TODO */
315 case 1: case 3: case 4: /* Variable-metrics formats do not have metrics here. */
316 default:return (false);
317 }
318 }
319
320 bool
321 get_image_data (unsigned int idx, unsigned int *offset,
322 unsigned int *length, unsigned int *format) const
323 {
324 *format = u.header.imageFormat;
325 switch (u.header.indexFormat)
326 {
327 case 1: return u.format1.get_image_data (idx, offset, length);
328 case 3: return u.format3.get_image_data (idx, offset, length);
329 default: return false;
330 }
331 }
332
333 const IndexSubtableHeader* get_header () const { return &u.header; }
334
335 void populate_header (unsigned index_format,
336 unsigned image_format,
337 unsigned int image_data_offset,
338 unsigned int *size)
339 {
340 u.header.indexFormat = index_format;
341 u.header.imageFormat = image_format;
342 u.header.imageDataOffset = image_data_offset;
343 switch (u.header.indexFormat)
344 {
345 case 1: *size += IndexSubtableFormat1::min_size; break;
346 case 3: *size += IndexSubtableFormat3::min_size; break;
347 }
348 }
349
350 protected:
351 union {
352 IndexSubtableHeader header;
353 IndexSubtableFormat1 format1;
354 IndexSubtableFormat3 format3;
355 /* TODO: Format 2, 4, 5. */
356 } u;
357 public:
358 DEFINE_SIZE_UNION (8, header);
359};
360
361struct IndexSubtableRecord
362{
363 bool sanitize (hb_sanitize_context_t *c, const void *base) const
364 {
365 TRACE_SANITIZE (this);
366 return_trace (c->check_struct (this) &&
367 firstGlyphIndex <= lastGlyphIndex &&
368 offsetToSubtable.sanitize (c, base, lastGlyphIndex - firstGlyphIndex + 1));
369 }
370
371 const IndexSubtable* get_subtable (const void *base) const
372 {
373 return &(base+offsetToSubtable);
374 }
375
376 bool add_new_subtable (hb_subset_context_t* c,
377 cblc_bitmap_size_subset_context_t *bitmap_size_context,
378 IndexSubtableRecord *record,
379 const hb_vector_t<hb_pair_t<hb_codepoint_t, const IndexSubtableRecord*>> *lookup, /* IN */
380 const void *base,
381 unsigned int *start /* INOUT */) const
382 {
383 TRACE_SERIALIZE (this);
384
385 auto *subtable = c->serializer->start_embed<IndexSubtable> ();
386 if (unlikely (!subtable)) return_trace (false);
387 if (unlikely (!c->serializer->extend_min (subtable))) return_trace (false);
388
389 auto *old_subtable = get_subtable (base);
390 auto *old_header = old_subtable->get_header ();
391
392 subtable->populate_header (old_header->indexFormat,
393 old_header->imageFormat,
394 bitmap_size_context->cbdt_prime->length,
395 &bitmap_size_context->size);
396
397 unsigned int num_glyphs = 0;
398 bool early_exit = false;
399 for (unsigned int i = *start; i < lookup->length; i++)
400 {
401 hb_codepoint_t new_gid = (*lookup)[i].first;
402 const IndexSubtableRecord *next_record = (*lookup)[i].second;
403 const IndexSubtable *next_subtable = next_record->get_subtable (base);
404 auto *next_header = next_subtable->get_header ();
405 if (next_header != old_header)
406 {
407 *start = i;
408 early_exit = true;
409 break;
410 }
411 unsigned int num_missing = record->add_glyph_for_subset (new_gid);
412 if (unlikely (!subtable->fill_missing_glyphs (c->serializer,
413 bitmap_size_context->cbdt_prime->length,
414 num_missing,
415 &bitmap_size_context->size,
416 &num_glyphs)))
417 return_trace (false);
418
419 hb_codepoint_t old_gid = 0;
420 c->plan->old_gid_for_new_gid (new_gid, &old_gid);
421 if (old_gid < next_record->firstGlyphIndex)
422 return_trace (false);
423
424 unsigned int old_idx = (unsigned int) old_gid - next_record->firstGlyphIndex;
425 if (unlikely (!next_subtable->copy_glyph_at_idx (c->serializer,
426 old_idx,
427 bitmap_size_context->cbdt,
428 bitmap_size_context->cbdt_length,
429 bitmap_size_context->cbdt_prime,
430 subtable,
431 &bitmap_size_context->size)))
432 return_trace (false);
433 num_glyphs += 1;
434 }
435 if (!early_exit)
436 *start = lookup->length;
437 if (unlikely (!subtable->finish_subtable (c->serializer,
438 bitmap_size_context->cbdt_prime->length,
439 num_glyphs,
440 &bitmap_size_context->size)))
441 return_trace (false);
442 return_trace (true);
443 }
444
445 bool add_new_record (hb_subset_context_t *c,
446 cblc_bitmap_size_subset_context_t *bitmap_size_context,
447 const hb_vector_t<hb_pair_t<hb_codepoint_t, const IndexSubtableRecord*>> *lookup, /* IN */
448 const void *base,
449 unsigned int *start, /* INOUT */
450 hb_vector_t<IndexSubtableRecord>* records /* INOUT */) const
451 {
452 TRACE_SERIALIZE (this);
453 auto snap = c->serializer->snapshot ();
454 unsigned int old_size = bitmap_size_context->size;
455 unsigned int old_cbdt_prime_length = bitmap_size_context->cbdt_prime->length;
456
457 // Set to invalid state to indicate filling glyphs is not yet started.
458 records->resize (records->length + 1);
459 (*records)[records->length - 1].firstGlyphIndex = 1;
460 (*records)[records->length - 1].lastGlyphIndex = 0;
461 bitmap_size_context->size += IndexSubtableRecord::min_size;
462
463 c->serializer->push ();
464
465 if (unlikely (!add_new_subtable (c, bitmap_size_context, &((*records)[records->length - 1]), lookup, base, start)))
466 {
467 c->serializer->pop_discard ();
468 c->serializer->revert (snap);
469 bitmap_size_context->cbdt_prime->shrink (old_cbdt_prime_length);
470 bitmap_size_context->size = old_size;
471 records->resize (records->length - 1);
472 return_trace (false);
473 }
474
475 bitmap_size_context->num_tables += 1;
476 return_trace (true);
477 }
478
479 unsigned int add_glyph_for_subset (hb_codepoint_t gid)
480 {
481 if (firstGlyphIndex > lastGlyphIndex)
482 {
483 firstGlyphIndex = gid;
484 lastGlyphIndex = gid;
485 return 0;
486 }
487 // TODO maybe assert? this shouldn't occur.
488 if (lastGlyphIndex > gid)
489 return 0;
490 unsigned int num_missing = (unsigned int) (gid - lastGlyphIndex - 1);
491 lastGlyphIndex = gid;
492 return num_missing;
493 }
494
495 bool get_extents (hb_glyph_extents_t *extents, const void *base) const
496 { return (base+offsetToSubtable).get_extents (extents); }
497
498 bool get_image_data (unsigned int gid,
499 const void *base,
500 unsigned int *offset,
501 unsigned int *length,
502 unsigned int *format) const
503 {
504 if (gid < firstGlyphIndex || gid > lastGlyphIndex) return false;
505 return (base+offsetToSubtable).get_image_data (gid - firstGlyphIndex,
506 offset, length, format);
507 }
508
509 HBGlyphID firstGlyphIndex;
510 HBGlyphID lastGlyphIndex;
511 LOffsetTo<IndexSubtable> offsetToSubtable;
512 public:
513 DEFINE_SIZE_STATIC (8);
514};
515
516struct IndexSubtableArray
517{
518 friend struct CBDT;
519
520 bool sanitize (hb_sanitize_context_t *c, unsigned int count) const
521 {
522 TRACE_SANITIZE (this);
523 return_trace (indexSubtablesZ.sanitize (c, count, this));
524 }
525
526 void
527 build_lookup (hb_subset_context_t *c, cblc_bitmap_size_subset_context_t *bitmap_size_context,
528 hb_vector_t<hb_pair_t<hb_codepoint_t,
529 const IndexSubtableRecord*>> *lookup /* OUT */) const
530 {
531 bool start_glyph_is_set = false;
532 for (hb_codepoint_t new_gid = 0; new_gid < c->plan->num_output_glyphs (); new_gid++)
533 {
534 hb_codepoint_t old_gid;
535 if (unlikely (!c->plan->old_gid_for_new_gid (new_gid, &old_gid))) continue;
536
537 const IndexSubtableRecord* record = find_table (old_gid, bitmap_size_context->num_tables);
538 if (unlikely (!record)) continue;
539
540 // Don't add gaps to the lookup. The best way to determine if a glyph is a
541 // gap is that it has no image data.
542 unsigned int offset, length, format;
543 if (unlikely (!record->get_image_data (old_gid, this, &offset, &length, &format))) continue;
544
545 lookup->push (hb_pair_t<hb_codepoint_t, const IndexSubtableRecord*> (new_gid, record));
546
547 if (!start_glyph_is_set)
548 {
549 bitmap_size_context->start_glyph = new_gid;
550 start_glyph_is_set = true;
551 }
552
553 bitmap_size_context->end_glyph = new_gid;
554 }
555 }
556
557 bool
558 subset (hb_subset_context_t *c,
559 cblc_bitmap_size_subset_context_t *bitmap_size_context) const
560 {
561 TRACE_SUBSET (this);
562
563 auto *dst = c->serializer->start_embed<IndexSubtableArray> ();
564 if (unlikely (!dst)) return_trace (false);
565
566 hb_vector_t<hb_pair_t<hb_codepoint_t, const IndexSubtableRecord*>> lookup;
567 build_lookup (c, bitmap_size_context, &lookup);
568
569 bitmap_size_context->size = 0;
570 bitmap_size_context->num_tables = 0;
571 hb_vector_t<IndexSubtableRecord> records;
572 for (unsigned int start = 0; start < lookup.length;)
573 {
574 if (unlikely (!lookup[start].second->add_new_record (c, bitmap_size_context, &lookup, this, &start, &records)))
575 {
576 // Discard any leftover pushes to the serializer from successful records.
577 for (unsigned int i = 0; i < records.length; i++)
578 c->serializer->pop_discard ();
579 return_trace (false);
580 }
581 }
582
583 /* Workaround to ensure offset ordering is from least to greatest when
584 * resolving links. */
585 hb_vector_t<hb_serialize_context_t::objidx_t> objidxs;
586 for (unsigned int i = 0; i < records.length; i++)
587 objidxs.push (c->serializer->pop_pack ());
588 for (unsigned int i = 0; i < records.length; i++)
589 {
590 IndexSubtableRecord* record = c->serializer->embed (records[i]);
591 if (unlikely (!record)) return_trace (false);
592 c->serializer->add_link (record->offsetToSubtable, objidxs[records.length - 1 - i]);
593 }
594 return_trace (true);
595 }
596
597 public:
598 const IndexSubtableRecord* find_table (hb_codepoint_t glyph, unsigned int numTables) const
599 {
600 for (unsigned int i = 0; i < numTables; ++i)
601 {
602 unsigned int firstGlyphIndex = indexSubtablesZ[i].firstGlyphIndex;
603 unsigned int lastGlyphIndex = indexSubtablesZ[i].lastGlyphIndex;
604 if (firstGlyphIndex <= glyph && glyph <= lastGlyphIndex)
605 return &indexSubtablesZ[i];
606 }
607 return nullptr;
608 }
609
610 protected:
611 UnsizedArrayOf<IndexSubtableRecord> indexSubtablesZ;
612};
613
614struct BitmapSizeTable
615{
616 friend struct CBLC;
617 friend struct CBDT;
618
619 bool sanitize (hb_sanitize_context_t *c, const void *base) const
620 {
621 TRACE_SANITIZE (this);
622 return_trace (c->check_struct (this) &&
623 indexSubtableArrayOffset.sanitize (c, base, numberOfIndexSubtables) &&
624 horizontal.sanitize (c) &&
625 vertical.sanitize (c));
626 }
627
628 const IndexSubtableRecord *
629 find_table (hb_codepoint_t glyph, const void *base, const void **out_base) const
630 {
631 *out_base = &(base+indexSubtableArrayOffset);
632 return (base+indexSubtableArrayOffset).find_table (glyph, numberOfIndexSubtables);
633 }
634
635 bool
636 subset (hb_subset_context_t *c, const void *base,
637 const char *cbdt, unsigned int cbdt_length,
638 hb_vector_t<char> *cbdt_prime /* INOUT */) const
639 {
640 TRACE_SUBSET (this);
641 auto *out_table = c->serializer->embed (this);
642 if (unlikely (!out_table)) return_trace (false);
643
644 cblc_bitmap_size_subset_context_t bitmap_size_context;
645 bitmap_size_context.cbdt = cbdt;
646 bitmap_size_context.cbdt_length = cbdt_length;
647 bitmap_size_context.cbdt_prime = cbdt_prime;
648 bitmap_size_context.size = indexTablesSize;
649 bitmap_size_context.num_tables = numberOfIndexSubtables;
650 bitmap_size_context.start_glyph = 1;
651 bitmap_size_context.end_glyph = 0;
652
653 if (!out_table->indexSubtableArrayOffset.serialize_subset (c,
654 indexSubtableArrayOffset,
655 base,
656 &bitmap_size_context))
657 return_trace (false);
658 if (!bitmap_size_context.size ||
659 !bitmap_size_context.num_tables ||
660 bitmap_size_context.start_glyph > bitmap_size_context.end_glyph)
661 return_trace (false);
662
663 out_table->indexTablesSize = bitmap_size_context.size;
664 out_table->numberOfIndexSubtables = bitmap_size_context.num_tables;
665 out_table->startGlyphIndex = bitmap_size_context.start_glyph;
666 out_table->endGlyphIndex = bitmap_size_context.end_glyph;
667 return_trace (true);
668 }
669
670 protected:
671 LNNOffsetTo<IndexSubtableArray>
672 indexSubtableArrayOffset;
673 HBUINT32 indexTablesSize;
674 HBUINT32 numberOfIndexSubtables;
675 HBUINT32 colorRef;
676 SBitLineMetrics horizontal;
677 SBitLineMetrics vertical;
678 HBGlyphID startGlyphIndex;
679 HBGlyphID endGlyphIndex;
680 HBUINT8 ppemX;
681 HBUINT8 ppemY;
682 HBUINT8 bitDepth;
683 HBINT8 flags;
684 public:
685 DEFINE_SIZE_STATIC (48);
686};
687
688
689/*
690 * Glyph Bitmap Data Formats.
691 */
692
693struct GlyphBitmapDataFormat17
694{
695 SmallGlyphMetrics glyphMetrics;
696 LArrayOf<HBUINT8> data;
697 public:
698 DEFINE_SIZE_ARRAY (9, data);
699};
700
701struct GlyphBitmapDataFormat18
702{
703 BigGlyphMetrics glyphMetrics;
704 LArrayOf<HBUINT8> data;
705 public:
706 DEFINE_SIZE_ARRAY (12, data);
707};
708
709struct GlyphBitmapDataFormat19
710{
711 LArrayOf<HBUINT8> data;
712 public:
713 DEFINE_SIZE_ARRAY (4, data);
714};
715
716struct CBLC
717{
718 friend struct CBDT;
719
720 static constexpr hb_tag_t tableTag = HB_OT_TAG_CBLC;
721
722 bool sanitize (hb_sanitize_context_t *c) const
723 {
724 TRACE_SANITIZE (this);
725 return_trace (c->check_struct (this) &&
726 likely (version.major == 2 || version.major == 3) &&
727 sizeTables.sanitize (c, this));
728 }
729
730 static bool
731 sink_cbdt (hb_subset_context_t *c, hb_vector_t<char>* cbdt_prime)
732 {
733 hb_blob_t *cbdt_prime_blob = hb_blob_create (cbdt_prime->arrayZ,
734 cbdt_prime->length,
735 HB_MEMORY_MODE_WRITABLE,
736 cbdt_prime->arrayZ,
737 free);
738 cbdt_prime->init (); // Leak arrayZ to the blob.
739 bool ret = c->plan->add_table (HB_OT_TAG_CBDT, cbdt_prime_blob);
740 hb_blob_destroy (cbdt_prime_blob);
741 return ret;
742 }
743
744 bool
745 subset_size_table (hb_subset_context_t *c, const BitmapSizeTable& table,
746 const char *cbdt /* IN */, unsigned int cbdt_length,
747 CBLC *cblc_prime /* INOUT */, hb_vector_t<char> *cbdt_prime /* INOUT */) const
748 {
749 TRACE_SUBSET (this);
750 cblc_prime->sizeTables.len++;
751
752 auto snap = c->serializer->snapshot ();
753 auto cbdt_prime_len = cbdt_prime->length;
754
755 if (!table.subset (c, this, cbdt, cbdt_length, cbdt_prime))
756 {
757 cblc_prime->sizeTables.len--;
758 c->serializer->revert (snap);
759 cbdt_prime->shrink (cbdt_prime_len);
760 return_trace (false);
761 }
762 return_trace (true);
763 }
764
765 // Implemented in cc file as it depends on definition of CBDT.
766 HB_INTERNAL bool subset (hb_subset_context_t *c) const;
767
768 protected:
769 const BitmapSizeTable &choose_strike (hb_font_t *font) const
770 {
771 unsigned count = sizeTables.len;
772 if (unlikely (!count))
773 return Null (BitmapSizeTable);
774
775 unsigned int requested_ppem = hb_max (font->x_ppem, font->y_ppem);
776 if (!requested_ppem)
777 requested_ppem = 1<<30; /* Choose largest strike. */
778 unsigned int best_i = 0;
779 unsigned int best_ppem = hb_max (sizeTables[0].ppemX, sizeTables[0].ppemY);
780
781 for (unsigned int i = 1; i < count; i++)
782 {
783 unsigned int ppem = hb_max (sizeTables[i].ppemX, sizeTables[i].ppemY);
784 if ((requested_ppem <= ppem && ppem < best_ppem) ||
785 (requested_ppem > best_ppem && ppem > best_ppem))
786 {
787 best_i = i;
788 best_ppem = ppem;
789 }
790 }
791
792 return sizeTables[best_i];
793 }
794
795 protected:
796 FixedVersion<> version;
797 LArrayOf<BitmapSizeTable> sizeTables;
798 public:
799 DEFINE_SIZE_ARRAY (8, sizeTables);
800};
801
802struct CBDT
803{
804 static constexpr hb_tag_t tableTag = HB_OT_TAG_CBDT;
805
806 struct accelerator_t
807 {
808 void init (hb_face_t *face)
809 {
810 cblc = hb_sanitize_context_t ().reference_table<CBLC> (face);
811 cbdt = hb_sanitize_context_t ().reference_table<CBDT> (face);
812
813 upem = hb_face_get_upem (face);
814 }
815
816 void fini ()
817 {
818 this->cblc.destroy ();
819 this->cbdt.destroy ();
820 }
821
822 bool
823 get_extents (hb_font_t *font, hb_codepoint_t glyph, hb_glyph_extents_t *extents) const
824 {
825 const void *base;
826 const BitmapSizeTable &strike = this->cblc->choose_strike (font);
827 const IndexSubtableRecord *subtable_record = strike.find_table (glyph, cblc, &base);
828 if (!subtable_record || !strike.ppemX || !strike.ppemY)
829 return false;
830
831 if (subtable_record->get_extents (extents, base))
832 return true;
833
834 unsigned int image_offset = 0, image_length = 0, image_format = 0;
835 if (!subtable_record->get_image_data (glyph, base, &image_offset, &image_length, &image_format))
836 return false;
837
838 unsigned int cbdt_len = cbdt.get_length ();
839 if (unlikely (image_offset > cbdt_len || cbdt_len - image_offset < image_length))
840 return false;
841
842 switch (image_format)
843 {
844 case 17: {
845 if (unlikely (image_length < GlyphBitmapDataFormat17::min_size))
846 return false;
847 auto &glyphFormat17 = StructAtOffset<GlyphBitmapDataFormat17> (this->cbdt, image_offset);
848 glyphFormat17.glyphMetrics.get_extents (font, extents);
849 break;
850 }
851 case 18: {
852 if (unlikely (image_length < GlyphBitmapDataFormat18::min_size))
853 return false;
854 auto &glyphFormat18 = StructAtOffset<GlyphBitmapDataFormat18> (this->cbdt, image_offset);
855 glyphFormat18.glyphMetrics.get_extents (font, extents);
856 break;
857 }
858 default: return false; /* TODO: Support other image formats. */
859 }
860
861 /* Convert to font units. */
862 float x_scale = upem / (float) strike.ppemX;
863 float y_scale = upem / (float) strike.ppemY;
864 extents->x_bearing = roundf (extents->x_bearing * x_scale);
865 extents->y_bearing = roundf (extents->y_bearing * y_scale);
866 extents->width = roundf (extents->width * x_scale);
867 extents->height = roundf (extents->height * y_scale);
868
869 return true;
870 }
871
872 hb_blob_t*
873 reference_png (hb_font_t *font, hb_codepoint_t glyph) const
874 {
875 const void *base;
876 const BitmapSizeTable &strike = this->cblc->choose_strike (font);
877 const IndexSubtableRecord *subtable_record = strike.find_table (glyph, cblc, &base);
878 if (!subtable_record || !strike.ppemX || !strike.ppemY)
879 return hb_blob_get_empty ();
880
881 unsigned int image_offset = 0, image_length = 0, image_format = 0;
882 if (!subtable_record->get_image_data (glyph, base, &image_offset, &image_length, &image_format))
883 return hb_blob_get_empty ();
884
885 unsigned int cbdt_len = cbdt.get_length ();
886 if (unlikely (image_offset > cbdt_len || cbdt_len - image_offset < image_length))
887 return hb_blob_get_empty ();
888
889 switch (image_format)
890 {
891 case 17:
892 {
893 if (unlikely (image_length < GlyphBitmapDataFormat17::min_size))
894 return hb_blob_get_empty ();
895 auto &glyphFormat17 = StructAtOffset<GlyphBitmapDataFormat17> (this->cbdt, image_offset);
896 return hb_blob_create_sub_blob (cbdt.get_blob (),
897 image_offset + GlyphBitmapDataFormat17::min_size,
898 glyphFormat17.data.len);
899 }
900 case 18:
901 {
902 if (unlikely (image_length < GlyphBitmapDataFormat18::min_size))
903 return hb_blob_get_empty ();
904 auto &glyphFormat18 = StructAtOffset<GlyphBitmapDataFormat18> (this->cbdt, image_offset);
905 return hb_blob_create_sub_blob (cbdt.get_blob (),
906 image_offset + GlyphBitmapDataFormat18::min_size,
907 glyphFormat18.data.len);
908 }
909 case 19:
910 {
911 if (unlikely (image_length < GlyphBitmapDataFormat19::min_size))
912 return hb_blob_get_empty ();
913 auto &glyphFormat19 = StructAtOffset<GlyphBitmapDataFormat19> (this->cbdt, image_offset);
914 return hb_blob_create_sub_blob (cbdt.get_blob (),
915 image_offset + GlyphBitmapDataFormat19::min_size,
916 glyphFormat19.data.len);
917 }
918 default: return hb_blob_get_empty (); /* TODO: Support other image formats. */
919 }
920 }
921
922 bool has_data () const { return cbdt.get_length (); }
923
924 private:
925 hb_blob_ptr_t<CBLC> cblc;
926 hb_blob_ptr_t<CBDT> cbdt;
927
928 unsigned int upem;
929 };
930
931 bool sanitize (hb_sanitize_context_t *c) const
932 {
933 TRACE_SANITIZE (this);
934 return_trace (c->check_struct (this) &&
935 likely (version.major == 2 || version.major == 3));
936 }
937
938 protected:
939 FixedVersion<> version;
940 UnsizedArrayOf<HBUINT8> dataZ;
941 public:
942 DEFINE_SIZE_ARRAY (4, dataZ);
943};
944
945inline bool
946CBLC::subset (hb_subset_context_t *c) const
947{
948 TRACE_SUBSET (this);
949
950 auto *cblc_prime = c->serializer->start_embed<CBLC> ();
951
952 // Use a vector as a secondary buffer as the tables need to be built in parallel.
953 hb_vector_t<char> cbdt_prime;
954
955 if (unlikely (!cblc_prime)) return_trace (false);
956 if (unlikely (!c->serializer->extend_min (cblc_prime))) return_trace (false);
957 cblc_prime->version = version;
958
959 hb_blob_t* cbdt_blob = hb_sanitize_context_t ().reference_table<CBDT> (c->plan->source);
960 unsigned int cbdt_length;
961 CBDT* cbdt = (CBDT *) hb_blob_get_data (cbdt_blob, &cbdt_length);
962 if (unlikely (cbdt_length < CBDT::min_size))
963 {
964 hb_blob_destroy (cbdt_blob);
965 return_trace (false);
966 }
967 _copy_data_to_cbdt (&cbdt_prime, cbdt, CBDT::min_size);
968
969 for (const BitmapSizeTable& table : + sizeTables.iter ())
970 subset_size_table (c, table, (const char *) cbdt, cbdt_length, cblc_prime, &cbdt_prime);
971
972 hb_blob_destroy (cbdt_blob);
973
974 return_trace (CBLC::sink_cbdt (c, &cbdt_prime));
975}
976
977struct CBDT_accelerator_t : CBDT::accelerator_t {};
978
979} /* namespace OT */
980
981#endif /* HB_OT_COLOR_CBDT_TABLE_HH */
982