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 OT_COLOR_CBDT_CBDT_HH
28 #define OT_COLOR_CBDT_CBDT_HH
30 #include "../../../hb-open-type.hh"
31 #include "../../../hb-paint.hh"
34 * CBLC -- Color Bitmap Location
35 * https://docs.microsoft.com/en-us/typography/opentype/spec/cblc
36 * https://docs.microsoft.com/en-us/typography/opentype/spec/eblc
37 * CBDT -- Color Bitmap Data
38 * https://docs.microsoft.com/en-us/typography/opentype/spec/cbdt
39 * https://docs.microsoft.com/en-us/typography/opentype/spec/ebdt
41 #define HB_OT_TAG_CBLC HB_TAG('C','B','L','C')
42 #define HB_OT_TAG_CBDT HB_TAG('C','B','D','T')
47 struct cblc_bitmap_size_subset_context_t
50 unsigned int cbdt_length;
51 hb_vector_t<char> *cbdt_prime;
52 unsigned int size; /* INOUT
53 * Input: old size of IndexSubtable
54 * Output: new size of IndexSubtable
56 unsigned int num_tables; /* INOUT
57 * Input: old number of subtables.
58 * Output: new number of subtables.
60 hb_codepoint_t start_glyph; /* OUT */
61 hb_codepoint_t end_glyph; /* OUT */
65 _copy_data_to_cbdt (hb_vector_t<char> *cbdt_prime,
69 unsigned int new_len = cbdt_prime->length + length;
70 if (unlikely (!cbdt_prime->alloc (new_len))) return false;
71 hb_memcpy (cbdt_prime->arrayZ + cbdt_prime->length, data, length);
72 cbdt_prime->length = new_len;
76 struct SmallGlyphMetrics
78 bool sanitize (hb_sanitize_context_t *c) const
80 TRACE_SANITIZE (this);
81 return_trace (c->check_struct (this));
84 void get_extents (hb_font_t *font, hb_glyph_extents_t *extents, bool scale) const
86 extents->x_bearing = bearingX;
87 extents->y_bearing = bearingY;
88 extents->width = width;
89 extents->height = -static_cast<int> (height);
92 font->scale_glyph_extents (extents);
101 DEFINE_SIZE_STATIC (5);
104 struct BigGlyphMetrics : SmallGlyphMetrics
110 DEFINE_SIZE_STATIC (8);
113 struct SBitLineMetrics
115 bool sanitize (hb_sanitize_context_t *c) const
117 TRACE_SANITIZE (this);
118 return_trace (c->check_struct (this));
124 HBINT8 caretSlopeNumerator;
125 HBINT8 caretSlopeDenominator;
134 DEFINE_SIZE_STATIC (12);
142 struct IndexSubtableHeader
144 bool sanitize (hb_sanitize_context_t *c) const
146 TRACE_SANITIZE (this);
147 return_trace (c->check_struct (this));
150 HBUINT16 indexFormat;
151 HBUINT16 imageFormat;
152 HBUINT32 imageDataOffset;
154 DEFINE_SIZE_STATIC (8);
157 template <typename OffsetType>
158 struct IndexSubtableFormat1Or3
160 bool sanitize (hb_sanitize_context_t *c, unsigned int glyph_count) const
162 TRACE_SANITIZE (this);
163 return_trace (c->check_struct (this) &&
164 offsetArrayZ.sanitize (c, glyph_count + 1));
167 bool get_image_data (unsigned int idx,
168 unsigned int *offset,
169 unsigned int *length) const
171 if (unlikely (offsetArrayZ[idx + 1] <= offsetArrayZ[idx]))
174 *offset = header.imageDataOffset + offsetArrayZ[idx];
175 *length = offsetArrayZ[idx + 1] - offsetArrayZ[idx];
179 bool add_offset (hb_serialize_context_t *c,
181 unsigned int *size /* OUT (accumulated) */)
183 TRACE_SERIALIZE (this);
184 Offset<OffsetType> embedded_offset;
185 embedded_offset = offset;
186 *size += sizeof (OffsetType);
187 auto *o = c->embed (embedded_offset);
188 return_trace ((bool) o);
191 IndexSubtableHeader header;
192 UnsizedArrayOf<Offset<OffsetType>>
195 DEFINE_SIZE_ARRAY (8, offsetArrayZ);
198 struct IndexSubtableFormat1 : IndexSubtableFormat1Or3<HBUINT32> {};
199 struct IndexSubtableFormat3 : IndexSubtableFormat1Or3<HBUINT16> {};
203 bool sanitize (hb_sanitize_context_t *c, unsigned int glyph_count) const
205 TRACE_SANITIZE (this);
206 if (!u.header.sanitize (c)) return_trace (false);
207 switch (u.header.indexFormat)
209 case 1: return_trace (u.format1.sanitize (c, glyph_count));
210 case 3: return_trace (u.format3.sanitize (c, glyph_count));
211 default:return_trace (true);
216 finish_subtable (hb_serialize_context_t *c,
217 unsigned int cbdt_prime_len,
218 unsigned int num_glyphs,
219 unsigned int *size /* OUT (accumulated) */)
221 TRACE_SERIALIZE (this);
223 unsigned int local_offset = cbdt_prime_len - u.header.imageDataOffset;
224 switch (u.header.indexFormat)
226 case 1: return_trace (u.format1.add_offset (c, local_offset, size));
228 if (!u.format3.add_offset (c, local_offset, size))
229 return_trace (false);
230 if (!(num_glyphs & 0x01)) // Pad to 32-bit alignment if needed.
231 return_trace (u.format3.add_offset (c, 0, size));
234 // TODO: implement 2, 4, 5.
235 case 2: case 4: // No-op.
236 case 5: // Pad to 32-bit aligned.
237 default: return_trace (false);
242 fill_missing_glyphs (hb_serialize_context_t *c,
243 unsigned int cbdt_prime_len,
244 unsigned int num_missing,
245 unsigned int *size /* OUT (accumulated) */,
246 unsigned int *num_glyphs /* OUT (accumulated) */)
248 TRACE_SERIALIZE (this);
250 unsigned int local_offset = cbdt_prime_len - u.header.imageDataOffset;
251 switch (u.header.indexFormat)
254 for (unsigned int i = 0; i < num_missing; i++)
256 if (unlikely (!u.format1.add_offset (c, local_offset, size)))
257 return_trace (false);
263 for (unsigned int i = 0; i < num_missing; i++)
265 if (unlikely (!u.format3.add_offset (c, local_offset, size)))
266 return_trace (false);
271 // TODO: implement 2, 4, 5.
272 case 2: // Add empty space in cbdt_prime?.
273 case 4: case 5: // No-op as sparse is supported.
274 default: return_trace (false);
279 copy_glyph_at_idx (hb_serialize_context_t *c, unsigned int idx,
280 const char *cbdt, unsigned int cbdt_length,
281 hb_vector_t<char> *cbdt_prime /* INOUT */,
282 IndexSubtable *subtable_prime /* INOUT */,
283 unsigned int *size /* OUT (accumulated) */) const
285 TRACE_SERIALIZE (this);
287 unsigned int offset, length, format;
288 if (unlikely (!get_image_data (idx, &offset, &length, &format))) return_trace (false);
289 if (unlikely (offset > cbdt_length || cbdt_length - offset < length)) return_trace (false);
291 auto *header_prime = subtable_prime->get_header ();
292 unsigned int new_local_offset = cbdt_prime->length - (unsigned int) header_prime->imageDataOffset;
293 if (unlikely (!_copy_data_to_cbdt (cbdt_prime, cbdt + offset, length))) return_trace (false);
295 return_trace (subtable_prime->add_offset (c, new_local_offset, size));
299 add_offset (hb_serialize_context_t *c, unsigned int local_offset,
300 unsigned int *size /* OUT (accumulated) */)
302 TRACE_SERIALIZE (this);
303 switch (u.header.indexFormat)
305 case 1: return_trace (u.format1.add_offset (c, local_offset, size));
306 case 3: return_trace (u.format3.add_offset (c, local_offset, size));
307 // TODO: Implement tables 2, 4, 5
308 case 2: // Should be a no-op.
309 case 4: case 5: // Handle sparse cases.
310 default: return_trace (false);
314 bool get_extents (hb_glyph_extents_t *extents HB_UNUSED, bool scale HB_UNUSED) const
316 switch (u.header.indexFormat)
318 case 2: case 5: /* TODO */
319 case 1: case 3: case 4: /* Variable-metrics formats do not have metrics here. */
320 default:return (false);
325 get_image_data (unsigned int idx, unsigned int *offset,
326 unsigned int *length, unsigned int *format) const
328 *format = u.header.imageFormat;
329 switch (u.header.indexFormat)
331 case 1: return u.format1.get_image_data (idx, offset, length);
332 case 3: return u.format3.get_image_data (idx, offset, length);
333 default: return false;
337 const IndexSubtableHeader* get_header () const { return &u.header; }
339 void populate_header (unsigned index_format,
340 unsigned image_format,
341 unsigned int image_data_offset,
344 u.header.indexFormat = index_format;
345 u.header.imageFormat = image_format;
346 u.header.imageDataOffset = image_data_offset;
347 switch (u.header.indexFormat)
349 case 1: *size += IndexSubtableFormat1::min_size; break;
350 case 3: *size += IndexSubtableFormat3::min_size; break;
356 IndexSubtableHeader header;
357 IndexSubtableFormat1 format1;
358 IndexSubtableFormat3 format3;
359 /* TODO: Format 2, 4, 5. */
362 DEFINE_SIZE_UNION (8, header);
365 struct IndexSubtableRecord
367 /* XXX Remove this and fix by not inserting it into vector. */
368 IndexSubtableRecord& operator = (const IndexSubtableRecord &o)
370 firstGlyphIndex = o.firstGlyphIndex;
371 lastGlyphIndex = o.lastGlyphIndex;
372 offsetToSubtable = (unsigned) o.offsetToSubtable;
373 assert (offsetToSubtable.is_null ());
377 bool sanitize (hb_sanitize_context_t *c, const void *base) const
379 TRACE_SANITIZE (this);
380 return_trace (c->check_struct (this) &&
381 firstGlyphIndex <= lastGlyphIndex &&
382 offsetToSubtable.sanitize (c, base, lastGlyphIndex - firstGlyphIndex + 1));
385 const IndexSubtable* get_subtable (const void *base) const
387 return &(base+offsetToSubtable);
390 bool add_new_subtable (hb_subset_context_t* c,
391 cblc_bitmap_size_subset_context_t *bitmap_size_context,
392 IndexSubtableRecord *record,
393 const hb_vector_t<hb_pair_t<hb_codepoint_t, const IndexSubtableRecord*>> *lookup, /* IN */
395 unsigned int *start /* INOUT */) const
397 TRACE_SERIALIZE (this);
399 auto *subtable = c->serializer->start_embed<IndexSubtable> ();
400 if (unlikely (!c->serializer->extend_min (subtable))) return_trace (false);
402 auto *old_subtable = get_subtable (base);
403 auto *old_header = old_subtable->get_header ();
405 subtable->populate_header (old_header->indexFormat,
406 old_header->imageFormat,
407 bitmap_size_context->cbdt_prime->length,
408 &bitmap_size_context->size);
410 unsigned int num_glyphs = 0;
411 bool early_exit = false;
412 for (unsigned int i = *start; i < lookup->length; i++)
414 hb_codepoint_t new_gid = (*lookup)[i].first;
415 const IndexSubtableRecord *next_record = (*lookup)[i].second;
416 const IndexSubtable *next_subtable = next_record->get_subtable (base);
417 auto *next_header = next_subtable->get_header ();
418 if (next_header != old_header)
424 unsigned int num_missing = record->add_glyph_for_subset (new_gid);
425 if (unlikely (!subtable->fill_missing_glyphs (c->serializer,
426 bitmap_size_context->cbdt_prime->length,
428 &bitmap_size_context->size,
430 return_trace (false);
432 hb_codepoint_t old_gid = 0;
433 c->plan->old_gid_for_new_gid (new_gid, &old_gid);
434 if (old_gid < next_record->firstGlyphIndex)
435 return_trace (false);
437 unsigned int old_idx = (unsigned int) old_gid - next_record->firstGlyphIndex;
438 if (unlikely (!next_subtable->copy_glyph_at_idx (c->serializer,
440 bitmap_size_context->cbdt,
441 bitmap_size_context->cbdt_length,
442 bitmap_size_context->cbdt_prime,
444 &bitmap_size_context->size)))
445 return_trace (false);
449 *start = lookup->length;
450 if (unlikely (!subtable->finish_subtable (c->serializer,
451 bitmap_size_context->cbdt_prime->length,
453 &bitmap_size_context->size)))
454 return_trace (false);
458 bool add_new_record (hb_subset_context_t *c,
459 cblc_bitmap_size_subset_context_t *bitmap_size_context,
460 const hb_vector_t<hb_pair_t<hb_codepoint_t, const IndexSubtableRecord*>> *lookup, /* IN */
462 unsigned int *start, /* INOUT */
463 hb_vector_t<IndexSubtableRecord>* records /* INOUT */) const
465 TRACE_SERIALIZE (this);
466 auto snap = c->serializer->snapshot ();
467 unsigned int old_size = bitmap_size_context->size;
468 unsigned int old_cbdt_prime_length = bitmap_size_context->cbdt_prime->length;
470 // Set to invalid state to indicate filling glyphs is not yet started.
471 if (unlikely (!c->serializer->check_success (records->resize (records->length + 1))))
472 return_trace (false);
474 records->tail ().firstGlyphIndex = 1;
475 records->tail ().lastGlyphIndex = 0;
476 bitmap_size_context->size += IndexSubtableRecord::min_size;
478 c->serializer->push ();
480 if (unlikely (!add_new_subtable (c, bitmap_size_context, &(records->tail ()), lookup, base, start)))
482 c->serializer->pop_discard ();
483 c->serializer->revert (snap);
484 bitmap_size_context->cbdt_prime->shrink (old_cbdt_prime_length);
485 bitmap_size_context->size = old_size;
486 records->resize (records->length - 1);
487 return_trace (false);
490 bitmap_size_context->num_tables += 1;
494 unsigned int add_glyph_for_subset (hb_codepoint_t gid)
496 if (firstGlyphIndex > lastGlyphIndex)
498 firstGlyphIndex = gid;
499 lastGlyphIndex = gid;
502 // TODO maybe assert? this shouldn't occur.
503 if (lastGlyphIndex > gid)
505 unsigned int num_missing = (unsigned int) (gid - lastGlyphIndex - 1);
506 lastGlyphIndex = gid;
510 bool get_extents (hb_glyph_extents_t *extents, const void *base, bool scale) const
511 { return (base+offsetToSubtable).get_extents (extents, scale); }
513 bool get_image_data (unsigned int gid,
515 unsigned int *offset,
516 unsigned int *length,
517 unsigned int *format) const
519 if (gid < firstGlyphIndex || gid > lastGlyphIndex) return false;
520 return (base+offsetToSubtable).get_image_data (gid - firstGlyphIndex,
521 offset, length, format);
524 HBGlyphID16 firstGlyphIndex;
525 HBGlyphID16 lastGlyphIndex;
526 Offset32To<IndexSubtable> offsetToSubtable;
528 DEFINE_SIZE_STATIC (8);
531 struct IndexSubtableArray
535 bool sanitize (hb_sanitize_context_t *c, unsigned int count) const
537 TRACE_SANITIZE (this);
538 return_trace (indexSubtablesZ.sanitize (c, count, this));
542 build_lookup (hb_subset_context_t *c, cblc_bitmap_size_subset_context_t *bitmap_size_context,
543 hb_vector_t<hb_pair_t<hb_codepoint_t,
544 const IndexSubtableRecord*>> *lookup /* OUT */) const
546 bool start_glyph_is_set = false;
547 unsigned num_glyphs = c->plan->num_output_glyphs ();
548 for (hb_codepoint_t new_gid = 0; new_gid < num_glyphs; new_gid++)
550 hb_codepoint_t old_gid;
551 if (unlikely (!c->plan->old_gid_for_new_gid (new_gid, &old_gid))) continue;
553 const IndexSubtableRecord* record = find_table (old_gid, bitmap_size_context->num_tables);
554 if (unlikely (!record)) continue;
556 // Don't add gaps to the lookup. The best way to determine if a glyph is a
557 // gap is that it has no image data.
558 unsigned int offset, length, format;
559 if (unlikely (!record->get_image_data (old_gid, this, &offset, &length, &format))) continue;
561 lookup->push (hb_pair_t<hb_codepoint_t, const IndexSubtableRecord*> (new_gid, record));
563 if (!start_glyph_is_set)
565 bitmap_size_context->start_glyph = new_gid;
566 start_glyph_is_set = true;
569 bitmap_size_context->end_glyph = new_gid;
574 subset (hb_subset_context_t *c,
575 cblc_bitmap_size_subset_context_t *bitmap_size_context) const
579 hb_vector_t<hb_pair_t<hb_codepoint_t, const IndexSubtableRecord*>> lookup;
580 build_lookup (c, bitmap_size_context, &lookup);
581 if (unlikely (!c->serializer->propagate_error (lookup)))
584 bitmap_size_context->size = 0;
585 bitmap_size_context->num_tables = 0;
586 hb_vector_t<IndexSubtableRecord> records;
587 for (unsigned int start = 0; start < lookup.length;)
589 if (unlikely (!lookup[start].second->add_new_record (c, bitmap_size_context, &lookup, this, &start, &records)))
591 // Discard any leftover pushes to the serializer from successful records.
592 for (unsigned int i = 0; i < records.length; i++)
593 c->serializer->pop_discard ();
594 return_trace (false);
598 /* Workaround to ensure offset ordering is from least to greatest when
599 * resolving links. */
600 hb_vector_t<hb_serialize_context_t::objidx_t> objidxs;
601 for (unsigned int i = 0; i < records.length; i++)
602 objidxs.push (c->serializer->pop_pack ());
603 for (unsigned int i = 0; i < records.length; i++)
605 IndexSubtableRecord* record = c->serializer->embed (records[i]);
606 if (unlikely (!record)) return_trace (false);
607 c->serializer->add_link (record->offsetToSubtable, objidxs[records.length - 1 - i]);
613 const IndexSubtableRecord* find_table (hb_codepoint_t glyph, unsigned int numTables) const
615 for (unsigned int i = 0; i < numTables; ++i)
617 unsigned int firstGlyphIndex = indexSubtablesZ[i].firstGlyphIndex;
618 unsigned int lastGlyphIndex = indexSubtablesZ[i].lastGlyphIndex;
619 if (firstGlyphIndex <= glyph && glyph <= lastGlyphIndex)
620 return &indexSubtablesZ[i];
626 UnsizedArrayOf<IndexSubtableRecord> indexSubtablesZ;
629 struct BitmapSizeTable
634 bool sanitize (hb_sanitize_context_t *c, const void *base) const
636 TRACE_SANITIZE (this);
637 return_trace (c->check_struct (this) &&
638 indexSubtableArrayOffset.sanitize (c, base, numberOfIndexSubtables) &&
639 horizontal.sanitize (c) &&
640 vertical.sanitize (c));
643 const IndexSubtableRecord *
644 find_table (hb_codepoint_t glyph, const void *base, const void **out_base) const
646 *out_base = &(base+indexSubtableArrayOffset);
647 return (base+indexSubtableArrayOffset).find_table (glyph, numberOfIndexSubtables);
651 subset (hb_subset_context_t *c, const void *base,
652 const char *cbdt, unsigned int cbdt_length,
653 hb_vector_t<char> *cbdt_prime /* INOUT */) const
656 auto *out_table = c->serializer->embed (this);
657 if (unlikely (!out_table)) return_trace (false);
659 cblc_bitmap_size_subset_context_t bitmap_size_context;
660 bitmap_size_context.cbdt = cbdt;
661 bitmap_size_context.cbdt_length = cbdt_length;
662 bitmap_size_context.cbdt_prime = cbdt_prime;
663 bitmap_size_context.size = indexTablesSize;
664 bitmap_size_context.num_tables = numberOfIndexSubtables;
665 bitmap_size_context.start_glyph = 1;
666 bitmap_size_context.end_glyph = 0;
668 if (!out_table->indexSubtableArrayOffset.serialize_subset (c,
669 indexSubtableArrayOffset,
671 &bitmap_size_context))
672 return_trace (false);
673 if (!bitmap_size_context.size ||
674 !bitmap_size_context.num_tables ||
675 bitmap_size_context.start_glyph > bitmap_size_context.end_glyph)
676 return_trace (false);
678 out_table->indexTablesSize = bitmap_size_context.size;
679 out_table->numberOfIndexSubtables = bitmap_size_context.num_tables;
680 out_table->startGlyphIndex = bitmap_size_context.start_glyph;
681 out_table->endGlyphIndex = bitmap_size_context.end_glyph;
686 NNOffset32To<IndexSubtableArray>
687 indexSubtableArrayOffset;
688 HBUINT32 indexTablesSize;
689 HBUINT32 numberOfIndexSubtables;
691 SBitLineMetrics horizontal;
692 SBitLineMetrics vertical;
693 HBGlyphID16 startGlyphIndex;
694 HBGlyphID16 endGlyphIndex;
700 DEFINE_SIZE_STATIC (48);
705 * Glyph Bitmap Data Formats.
708 struct GlyphBitmapDataFormat17
710 SmallGlyphMetrics glyphMetrics;
711 Array32Of<HBUINT8> data;
713 DEFINE_SIZE_ARRAY (9, data);
716 struct GlyphBitmapDataFormat18
718 BigGlyphMetrics glyphMetrics;
719 Array32Of<HBUINT8> data;
721 DEFINE_SIZE_ARRAY (12, data);
724 struct GlyphBitmapDataFormat19
726 Array32Of<HBUINT8> data;
728 DEFINE_SIZE_ARRAY (4, data);
735 static constexpr hb_tag_t tableTag = HB_OT_TAG_CBLC;
737 bool sanitize (hb_sanitize_context_t *c) const
739 TRACE_SANITIZE (this);
740 return_trace (c->check_struct (this) &&
741 likely (version.major == 2 || version.major == 3) &&
742 sizeTables.sanitize (c, this));
746 sink_cbdt (hb_subset_context_t *c, hb_vector_t<char>* cbdt_prime)
748 hb_blob_t *cbdt_prime_blob = hb_blob_create (cbdt_prime->arrayZ,
750 HB_MEMORY_MODE_WRITABLE,
753 cbdt_prime->init (); // Leak arrayZ to the blob.
754 bool ret = c->plan->add_table (HB_OT_TAG_CBDT, cbdt_prime_blob);
755 hb_blob_destroy (cbdt_prime_blob);
760 subset_size_table (hb_subset_context_t *c, const BitmapSizeTable& table,
761 const char *cbdt /* IN */, unsigned int cbdt_length,
762 CBLC *cblc_prime /* INOUT */, hb_vector_t<char> *cbdt_prime /* INOUT */) const
765 cblc_prime->sizeTables.len++;
767 auto snap = c->serializer->snapshot ();
768 auto cbdt_prime_len = cbdt_prime->length;
770 if (!table.subset (c, this, cbdt, cbdt_length, cbdt_prime))
772 cblc_prime->sizeTables.len--;
773 c->serializer->revert (snap);
774 cbdt_prime->shrink (cbdt_prime_len);
775 return_trace (false);
780 // Implemented in cc file as it depends on definition of CBDT.
781 HB_INTERNAL bool subset (hb_subset_context_t *c) const;
784 const BitmapSizeTable &choose_strike (hb_font_t *font) const
786 unsigned count = sizeTables.len;
787 if (unlikely (!count))
788 return Null (BitmapSizeTable);
790 unsigned int requested_ppem = hb_max (font->x_ppem, font->y_ppem);
792 requested_ppem = 1<<30; /* Choose largest strike. */
793 unsigned int best_i = 0;
794 unsigned int best_ppem = hb_max (sizeTables[0].ppemX, sizeTables[0].ppemY);
796 for (unsigned int i = 1; i < count; i++)
798 unsigned int ppem = hb_max (sizeTables[i].ppemX, sizeTables[i].ppemY);
799 if ((requested_ppem <= ppem && ppem < best_ppem) ||
800 (requested_ppem > best_ppem && ppem > best_ppem))
807 return sizeTables[best_i];
811 FixedVersion<> version;
812 Array32Of<BitmapSizeTable> sizeTables;
814 DEFINE_SIZE_ARRAY (8, sizeTables);
819 static constexpr hb_tag_t tableTag = HB_OT_TAG_CBDT;
823 accelerator_t (hb_face_t *face)
825 this->cblc = hb_sanitize_context_t ().reference_table<CBLC> (face);
826 this->cbdt = hb_sanitize_context_t ().reference_table<CBDT> (face);
828 upem = hb_face_get_upem (face);
832 this->cblc.destroy ();
833 this->cbdt.destroy ();
837 get_extents (hb_font_t *font, hb_codepoint_t glyph, hb_glyph_extents_t *extents, bool scale = true) const
840 const BitmapSizeTable &strike = this->cblc->choose_strike (font);
841 const IndexSubtableRecord *subtable_record = strike.find_table (glyph, cblc, &base);
842 if (!subtable_record || !strike.ppemX || !strike.ppemY)
845 if (subtable_record->get_extents (extents, base, scale))
848 unsigned int image_offset = 0, image_length = 0, image_format = 0;
849 if (!subtable_record->get_image_data (glyph, base, &image_offset, &image_length, &image_format))
852 unsigned int cbdt_len = cbdt.get_length ();
853 if (unlikely (image_offset > cbdt_len || cbdt_len - image_offset < image_length))
856 switch (image_format)
859 if (unlikely (image_length < GlyphBitmapDataFormat17::min_size))
861 auto &glyphFormat17 = StructAtOffset<GlyphBitmapDataFormat17> (this->cbdt, image_offset);
862 glyphFormat17.glyphMetrics.get_extents (font, extents, scale);
866 if (unlikely (image_length < GlyphBitmapDataFormat18::min_size))
868 auto &glyphFormat18 = StructAtOffset<GlyphBitmapDataFormat18> (this->cbdt, image_offset);
869 glyphFormat18.glyphMetrics.get_extents (font, extents, scale);
872 default: return false; /* TODO: Support other image formats. */
875 /* Convert to font units. */
878 float x_scale = upem / (float) strike.ppemX;
879 float y_scale = upem / (float) strike.ppemY;
880 extents->x_bearing = roundf (extents->x_bearing * x_scale);
881 extents->y_bearing = roundf (extents->y_bearing * y_scale);
882 extents->width = roundf (extents->width * x_scale);
883 extents->height = roundf (extents->height * y_scale);
890 reference_png (hb_font_t *font, hb_codepoint_t glyph) const
893 const BitmapSizeTable &strike = this->cblc->choose_strike (font);
894 const IndexSubtableRecord *subtable_record = strike.find_table (glyph, cblc, &base);
895 if (!subtable_record || !strike.ppemX || !strike.ppemY)
896 return hb_blob_get_empty ();
898 unsigned int image_offset = 0, image_length = 0, image_format = 0;
899 if (!subtable_record->get_image_data (glyph, base, &image_offset, &image_length, &image_format))
900 return hb_blob_get_empty ();
902 unsigned int cbdt_len = cbdt.get_length ();
903 if (unlikely (image_offset > cbdt_len || cbdt_len - image_offset < image_length))
904 return hb_blob_get_empty ();
906 switch (image_format)
910 if (unlikely (image_length < GlyphBitmapDataFormat17::min_size))
911 return hb_blob_get_empty ();
912 auto &glyphFormat17 = StructAtOffset<GlyphBitmapDataFormat17> (this->cbdt, image_offset);
913 return hb_blob_create_sub_blob (cbdt.get_blob (),
914 image_offset + GlyphBitmapDataFormat17::min_size,
915 glyphFormat17.data.len);
919 if (unlikely (image_length < GlyphBitmapDataFormat18::min_size))
920 return hb_blob_get_empty ();
921 auto &glyphFormat18 = StructAtOffset<GlyphBitmapDataFormat18> (this->cbdt, image_offset);
922 return hb_blob_create_sub_blob (cbdt.get_blob (),
923 image_offset + GlyphBitmapDataFormat18::min_size,
924 glyphFormat18.data.len);
928 if (unlikely (image_length < GlyphBitmapDataFormat19::min_size))
929 return hb_blob_get_empty ();
930 auto &glyphFormat19 = StructAtOffset<GlyphBitmapDataFormat19> (this->cbdt, image_offset);
931 return hb_blob_create_sub_blob (cbdt.get_blob (),
932 image_offset + GlyphBitmapDataFormat19::min_size,
933 glyphFormat19.data.len);
935 default: return hb_blob_get_empty (); /* TODO: Support other image formats. */
939 bool has_data () const { return cbdt.get_length (); }
941 bool paint_glyph (hb_font_t *font, hb_codepoint_t glyph, hb_paint_funcs_t *funcs, void *data) const
943 hb_glyph_extents_t extents;
944 hb_glyph_extents_t pixel_extents;
945 hb_blob_t *blob = reference_png (font, glyph);
947 if (unlikely (blob == hb_blob_get_empty ()))
950 if (unlikely (!hb_font_get_glyph_extents (font, glyph, &extents)))
953 if (unlikely (!get_extents (font, glyph, &pixel_extents, false)))
956 bool ret = funcs->image (data,
958 pixel_extents.width, -pixel_extents.height,
959 HB_PAINT_IMAGE_FORMAT_PNG,
963 hb_blob_destroy (blob);
968 hb_blob_ptr_t<CBLC> cblc;
969 hb_blob_ptr_t<CBDT> cbdt;
974 bool sanitize (hb_sanitize_context_t *c) const
976 TRACE_SANITIZE (this);
977 return_trace (c->check_struct (this) &&
978 likely (version.major == 2 || version.major == 3));
982 FixedVersion<> version;
983 UnsizedArrayOf<HBUINT8> dataZ;
985 DEFINE_SIZE_ARRAY (4, dataZ);
989 CBLC::subset (hb_subset_context_t *c) const
993 // Use a vector as a secondary buffer as the tables need to be built in parallel.
994 hb_vector_t<char> cbdt_prime;
996 auto *cblc_prime = c->serializer->start_embed<CBLC> ();
997 if (unlikely (!c->serializer->extend_min (cblc_prime))) return_trace (false);
998 cblc_prime->version = version;
1000 hb_blob_t* cbdt_blob = hb_sanitize_context_t ().reference_table<CBDT> (c->plan->source);
1001 unsigned int cbdt_length;
1002 CBDT* cbdt = (CBDT *) hb_blob_get_data (cbdt_blob, &cbdt_length);
1003 if (unlikely (cbdt_length < CBDT::min_size))
1005 hb_blob_destroy (cbdt_blob);
1006 return_trace (false);
1008 _copy_data_to_cbdt (&cbdt_prime, cbdt, CBDT::min_size);
1010 for (const BitmapSizeTable& table : + sizeTables.iter ())
1011 subset_size_table (c, table, (const char *) cbdt, cbdt_length, cblc_prime, &cbdt_prime);
1013 hb_blob_destroy (cbdt_blob);
1015 return_trace (CBLC::sink_cbdt (c, &cbdt_prime));
1018 struct CBDT_accelerator_t : CBDT::accelerator_t {
1019 CBDT_accelerator_t (hb_face_t *face) : CBDT::accelerator_t (face) {}
1023 } /* namespace OT */
1025 #endif /* OT_COLOR_CBDT_CBDT_HH */