2 * Copyright © 2016 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Seigo Nonaka, Calder Kitagawa
27 #ifndef HB_OT_COLOR_CBDT_TABLE_HH
28 #define HB_OT_COLOR_CBDT_TABLE_HH
30 #include "hb-open-type.hh"
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
40 #define HB_OT_TAG_CBLC HB_TAG('C','B','L','C')
41 #define HB_OT_TAG_CBDT HB_TAG('C','B','D','T')
46 struct cblc_bitmap_size_subset_context_t
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
55 unsigned int num_tables; /* INOUT
56 * Input: old number of subtables.
57 * Output: new number of subtables.
59 hb_codepoint_t start_glyph; /* OUT */
60 hb_codepoint_t end_glyph; /* OUT */
64 _copy_data_to_cbdt (hb_vector_t<char> *cbdt_prime,
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;
75 struct SmallGlyphMetrics
77 bool sanitize (hb_sanitize_context_t *c) const
79 TRACE_SANITIZE (this);
80 return_trace (c->check_struct (this));
83 void get_extents (hb_font_t *font, hb_glyph_extents_t *extents) const
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));
97 DEFINE_SIZE_STATIC (5);
100 struct BigGlyphMetrics : SmallGlyphMetrics
106 DEFINE_SIZE_STATIC (8);
109 struct SBitLineMetrics
111 bool sanitize (hb_sanitize_context_t *c) const
113 TRACE_SANITIZE (this);
114 return_trace (c->check_struct (this));
120 HBINT8 caretSlopeNumerator;
121 HBINT8 caretSlopeDenominator;
130 DEFINE_SIZE_STATIC (12);
138 struct IndexSubtableHeader
140 bool sanitize (hb_sanitize_context_t *c) const
142 TRACE_SANITIZE (this);
143 return_trace (c->check_struct (this));
146 HBUINT16 indexFormat;
147 HBUINT16 imageFormat;
148 HBUINT32 imageDataOffset;
150 DEFINE_SIZE_STATIC (8);
153 template <typename OffsetType>
154 struct IndexSubtableFormat1Or3
156 bool sanitize (hb_sanitize_context_t *c, unsigned int glyph_count) const
158 TRACE_SANITIZE (this);
159 return_trace (c->check_struct (this) &&
160 offsetArrayZ.sanitize (c, glyph_count + 1));
163 bool get_image_data (unsigned int idx,
164 unsigned int *offset,
165 unsigned int *length) const
167 if (unlikely (offsetArrayZ[idx + 1] <= offsetArrayZ[idx]))
170 *offset = header.imageDataOffset + offsetArrayZ[idx];
171 *length = offsetArrayZ[idx + 1] - offsetArrayZ[idx];
175 bool add_offset (hb_serialize_context_t *c,
177 unsigned int *size /* OUT (accumulated) */)
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);
187 IndexSubtableHeader header;
188 UnsizedArrayOf<Offset<OffsetType>>
191 DEFINE_SIZE_ARRAY (8, offsetArrayZ);
194 struct IndexSubtableFormat1 : IndexSubtableFormat1Or3<HBUINT32> {};
195 struct IndexSubtableFormat3 : IndexSubtableFormat1Or3<HBUINT16> {};
199 bool sanitize (hb_sanitize_context_t *c, unsigned int glyph_count) const
201 TRACE_SANITIZE (this);
202 if (!u.header.sanitize (c)) return_trace (false);
203 switch (u.header.indexFormat)
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);
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) */)
217 TRACE_SERIALIZE (this);
219 unsigned int local_offset = cbdt_prime_len - u.header.imageDataOffset;
220 switch (u.header.indexFormat)
222 case 1: return_trace (u.format1.add_offset (c, local_offset, size));
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));
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);
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) */)
244 TRACE_SERIALIZE (this);
246 unsigned int local_offset = cbdt_prime_len - u.header.imageDataOffset;
247 switch (u.header.indexFormat)
250 for (unsigned int i = 0; i < num_missing; i++)
252 if (unlikely (!u.format1.add_offset (c, local_offset, size)))
253 return_trace (false);
259 for (unsigned int i = 0; i < num_missing; i++)
261 if (unlikely (!u.format3.add_offset (c, local_offset, size)))
262 return_trace (false);
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);
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
281 TRACE_SERIALIZE (this);
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);
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);
291 return_trace (subtable_prime->add_offset (c, new_local_offset, size));
295 add_offset (hb_serialize_context_t *c, unsigned int local_offset,
296 unsigned int *size /* OUT (accumulated) */)
298 TRACE_SERIALIZE (this);
299 switch (u.header.indexFormat)
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);
310 bool get_extents (hb_glyph_extents_t *extents HB_UNUSED) const
312 switch (u.header.indexFormat)
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);
321 get_image_data (unsigned int idx, unsigned int *offset,
322 unsigned int *length, unsigned int *format) const
324 *format = u.header.imageFormat;
325 switch (u.header.indexFormat)
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;
333 const IndexSubtableHeader* get_header () const { return &u.header; }
335 void populate_header (unsigned index_format,
336 unsigned image_format,
337 unsigned int image_data_offset,
340 u.header.indexFormat = index_format;
341 u.header.imageFormat = image_format;
342 u.header.imageDataOffset = image_data_offset;
343 switch (u.header.indexFormat)
345 case 1: *size += IndexSubtableFormat1::min_size; break;
346 case 3: *size += IndexSubtableFormat3::min_size; break;
352 IndexSubtableHeader header;
353 IndexSubtableFormat1 format1;
354 IndexSubtableFormat3 format3;
355 /* TODO: Format 2, 4, 5. */
358 DEFINE_SIZE_UNION (8, header);
361 struct IndexSubtableRecord
363 bool sanitize (hb_sanitize_context_t *c, const void *base) const
365 TRACE_SANITIZE (this);
366 return_trace (c->check_struct (this) &&
367 firstGlyphIndex <= lastGlyphIndex &&
368 offsetToSubtable.sanitize (c, base, lastGlyphIndex - firstGlyphIndex + 1));
371 const IndexSubtable* get_subtable (const void *base) const
373 return &(base+offsetToSubtable);
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 */
381 unsigned int *start /* INOUT */) const
383 TRACE_SERIALIZE (this);
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);
389 auto *old_subtable = get_subtable (base);
390 auto *old_header = old_subtable->get_header ();
392 subtable->populate_header (old_header->indexFormat,
393 old_header->imageFormat,
394 bitmap_size_context->cbdt_prime->length,
395 &bitmap_size_context->size);
397 unsigned int num_glyphs = 0;
398 bool early_exit = false;
399 for (unsigned int i = *start; i < lookup->length; i++)
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)
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,
415 &bitmap_size_context->size,
417 return_trace (false);
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);
424 unsigned int old_idx = (unsigned int) old_gid - next_record->firstGlyphIndex;
425 if (unlikely (!next_subtable->copy_glyph_at_idx (c->serializer,
427 bitmap_size_context->cbdt,
428 bitmap_size_context->cbdt_length,
429 bitmap_size_context->cbdt_prime,
431 &bitmap_size_context->size)))
432 return_trace (false);
436 *start = lookup->length;
437 if (unlikely (!subtable->finish_subtable (c->serializer,
438 bitmap_size_context->cbdt_prime->length,
440 &bitmap_size_context->size)))
441 return_trace (false);
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 */
449 unsigned int *start, /* INOUT */
450 hb_vector_t<IndexSubtableRecord>* records /* INOUT */) const
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;
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;
463 c->serializer->push ();
465 if (unlikely (!add_new_subtable (c, bitmap_size_context, &((*records)[records->length - 1]), lookup, base, start)))
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);
475 bitmap_size_context->num_tables += 1;
479 unsigned int add_glyph_for_subset (hb_codepoint_t gid)
481 if (firstGlyphIndex > lastGlyphIndex)
483 firstGlyphIndex = gid;
484 lastGlyphIndex = gid;
487 // TODO maybe assert? this shouldn't occur.
488 if (lastGlyphIndex > gid)
490 unsigned int num_missing = (unsigned int) (gid - lastGlyphIndex - 1);
491 lastGlyphIndex = gid;
495 bool get_extents (hb_glyph_extents_t *extents, const void *base) const
496 { return (base+offsetToSubtable).get_extents (extents); }
498 bool get_image_data (unsigned int gid,
500 unsigned int *offset,
501 unsigned int *length,
502 unsigned int *format) const
504 if (gid < firstGlyphIndex || gid > lastGlyphIndex) return false;
505 return (base+offsetToSubtable).get_image_data (gid - firstGlyphIndex,
506 offset, length, format);
509 HBGlyphID firstGlyphIndex;
510 HBGlyphID lastGlyphIndex;
511 LOffsetTo<IndexSubtable> offsetToSubtable;
513 DEFINE_SIZE_STATIC (8);
516 struct IndexSubtableArray
520 bool sanitize (hb_sanitize_context_t *c, unsigned int count) const
522 TRACE_SANITIZE (this);
523 return_trace (indexSubtablesZ.sanitize (c, count, this));
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
531 bool start_glyph_is_set = false;
532 for (hb_codepoint_t new_gid = 0; new_gid < c->plan->num_output_glyphs (); new_gid++)
534 hb_codepoint_t old_gid;
535 if (unlikely (!c->plan->old_gid_for_new_gid (new_gid, &old_gid))) continue;
537 const IndexSubtableRecord* record = find_table (old_gid, bitmap_size_context->num_tables);
538 if (unlikely (!record)) continue;
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;
545 lookup->push (hb_pair_t<hb_codepoint_t, const IndexSubtableRecord*> (new_gid, record));
547 if (!start_glyph_is_set)
549 bitmap_size_context->start_glyph = new_gid;
550 start_glyph_is_set = true;
553 bitmap_size_context->end_glyph = new_gid;
558 subset (hb_subset_context_t *c,
559 cblc_bitmap_size_subset_context_t *bitmap_size_context) const
563 auto *dst = c->serializer->start_embed<IndexSubtableArray> ();
564 if (unlikely (!dst)) return_trace (false);
566 hb_vector_t<hb_pair_t<hb_codepoint_t, const IndexSubtableRecord*>> lookup;
567 build_lookup (c, bitmap_size_context, &lookup);
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;)
574 if (unlikely (!lookup[start].second->add_new_record (c, bitmap_size_context, &lookup, this, &start, &records)))
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);
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++)
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]);
598 const IndexSubtableRecord* find_table (hb_codepoint_t glyph, unsigned int numTables) const
600 for (unsigned int i = 0; i < numTables; ++i)
602 unsigned int firstGlyphIndex = indexSubtablesZ[i].firstGlyphIndex;
603 unsigned int lastGlyphIndex = indexSubtablesZ[i].lastGlyphIndex;
604 if (firstGlyphIndex <= glyph && glyph <= lastGlyphIndex)
605 return &indexSubtablesZ[i];
611 UnsizedArrayOf<IndexSubtableRecord> indexSubtablesZ;
614 struct BitmapSizeTable
619 bool sanitize (hb_sanitize_context_t *c, const void *base) const
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));
628 const IndexSubtableRecord *
629 find_table (hb_codepoint_t glyph, const void *base, const void **out_base) const
631 *out_base = &(base+indexSubtableArrayOffset);
632 return (base+indexSubtableArrayOffset).find_table (glyph, numberOfIndexSubtables);
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
641 auto *out_table = c->serializer->embed (this);
642 if (unlikely (!out_table)) return_trace (false);
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;
653 if (!out_table->indexSubtableArrayOffset.serialize_subset (c,
654 indexSubtableArrayOffset,
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);
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;
671 LNNOffsetTo<IndexSubtableArray>
672 indexSubtableArrayOffset;
673 HBUINT32 indexTablesSize;
674 HBUINT32 numberOfIndexSubtables;
676 SBitLineMetrics horizontal;
677 SBitLineMetrics vertical;
678 HBGlyphID startGlyphIndex;
679 HBGlyphID endGlyphIndex;
685 DEFINE_SIZE_STATIC (48);
690 * Glyph Bitmap Data Formats.
693 struct GlyphBitmapDataFormat17
695 SmallGlyphMetrics glyphMetrics;
696 LArrayOf<HBUINT8> data;
698 DEFINE_SIZE_ARRAY (9, data);
701 struct GlyphBitmapDataFormat18
703 BigGlyphMetrics glyphMetrics;
704 LArrayOf<HBUINT8> data;
706 DEFINE_SIZE_ARRAY (12, data);
709 struct GlyphBitmapDataFormat19
711 LArrayOf<HBUINT8> data;
713 DEFINE_SIZE_ARRAY (4, data);
720 static constexpr hb_tag_t tableTag = HB_OT_TAG_CBLC;
722 bool sanitize (hb_sanitize_context_t *c) const
724 TRACE_SANITIZE (this);
725 return_trace (c->check_struct (this) &&
726 likely (version.major == 2 || version.major == 3) &&
727 sizeTables.sanitize (c, this));
731 sink_cbdt (hb_subset_context_t *c, hb_vector_t<char>* cbdt_prime)
733 hb_blob_t *cbdt_prime_blob = hb_blob_create (cbdt_prime->arrayZ,
735 HB_MEMORY_MODE_WRITABLE,
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);
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
750 cblc_prime->sizeTables.len++;
752 auto snap = c->serializer->snapshot ();
753 auto cbdt_prime_len = cbdt_prime->length;
755 if (!table.subset (c, this, cbdt, cbdt_length, cbdt_prime))
757 cblc_prime->sizeTables.len--;
758 c->serializer->revert (snap);
759 cbdt_prime->shrink (cbdt_prime_len);
760 return_trace (false);
765 // Implemented in cc file as it depends on definition of CBDT.
766 HB_INTERNAL bool subset (hb_subset_context_t *c) const;
769 const BitmapSizeTable &choose_strike (hb_font_t *font) const
771 unsigned count = sizeTables.len;
772 if (unlikely (!count))
773 return Null (BitmapSizeTable);
775 unsigned int requested_ppem = hb_max (font->x_ppem, font->y_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);
781 for (unsigned int i = 1; i < count; i++)
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))
792 return sizeTables[best_i];
796 FixedVersion<> version;
797 LArrayOf<BitmapSizeTable> sizeTables;
799 DEFINE_SIZE_ARRAY (8, sizeTables);
804 static constexpr hb_tag_t tableTag = HB_OT_TAG_CBDT;
808 void init (hb_face_t *face)
810 cblc = hb_sanitize_context_t ().reference_table<CBLC> (face);
811 cbdt = hb_sanitize_context_t ().reference_table<CBDT> (face);
813 upem = hb_face_get_upem (face);
818 this->cblc.destroy ();
819 this->cbdt.destroy ();
823 get_extents (hb_font_t *font, hb_codepoint_t glyph, hb_glyph_extents_t *extents) const
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)
831 if (subtable_record->get_extents (extents, base))
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))
838 unsigned int cbdt_len = cbdt.get_length ();
839 if (unlikely (image_offset > cbdt_len || cbdt_len - image_offset < image_length))
842 switch (image_format)
845 if (unlikely (image_length < GlyphBitmapDataFormat17::min_size))
847 auto &glyphFormat17 = StructAtOffset<GlyphBitmapDataFormat17> (this->cbdt, image_offset);
848 glyphFormat17.glyphMetrics.get_extents (font, extents);
852 if (unlikely (image_length < GlyphBitmapDataFormat18::min_size))
854 auto &glyphFormat18 = StructAtOffset<GlyphBitmapDataFormat18> (this->cbdt, image_offset);
855 glyphFormat18.glyphMetrics.get_extents (font, extents);
858 default: return false; /* TODO: Support other image formats. */
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);
873 reference_png (hb_font_t *font, hb_codepoint_t glyph) const
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 ();
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 ();
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 ();
889 switch (image_format)
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);
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);
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);
918 default: return hb_blob_get_empty (); /* TODO: Support other image formats. */
922 bool has_data () const { return cbdt.get_length (); }
925 hb_blob_ptr_t<CBLC> cblc;
926 hb_blob_ptr_t<CBDT> cbdt;
931 bool sanitize (hb_sanitize_context_t *c) const
933 TRACE_SANITIZE (this);
934 return_trace (c->check_struct (this) &&
935 likely (version.major == 2 || version.major == 3));
939 FixedVersion<> version;
940 UnsizedArrayOf<HBUINT8> dataZ;
942 DEFINE_SIZE_ARRAY (4, dataZ);
946 CBLC::subset (hb_subset_context_t *c) const
950 auto *cblc_prime = c->serializer->start_embed<CBLC> ();
952 // Use a vector as a secondary buffer as the tables need to be built in parallel.
953 hb_vector_t<char> cbdt_prime;
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;
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))
964 hb_blob_destroy (cbdt_blob);
965 return_trace (false);
967 _copy_data_to_cbdt (&cbdt_prime, cbdt, CBDT::min_size);
969 for (const BitmapSizeTable& table : + sizeTables.iter ())
970 subset_size_table (c, table, (const char *) cbdt, cbdt_length, cblc_prime, &cbdt_prime);
972 hb_blob_destroy (cbdt_blob);
974 return_trace (CBLC::sink_cbdt (c, &cbdt_prime));
977 struct CBDT_accelerator_t : CBDT::accelerator_t {};
981 #endif /* HB_OT_COLOR_CBDT_TABLE_HH */