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
27 #ifndef HB_OT_COLOR_CBDT_TABLE_HH
28 #define HB_OT_COLOR_CBDT_TABLE_HH
30 #include "hb-open-type-private.hh"
34 struct SmallGlyphMetrics
36 inline bool sanitize (hb_sanitize_context_t *c) const
38 TRACE_SANITIZE (this);
39 return_trace (c->check_struct (this));
42 inline void get_extents (hb_glyph_extents_t *extents) const
44 extents->x_bearing = bearingX;
45 extents->y_bearing = bearingY;
46 extents->width = width;
47 extents->height = -height;
56 DEFINE_SIZE_STATIC(5);
59 struct BigGlyphMetrics : SmallGlyphMetrics
65 DEFINE_SIZE_STATIC(8);
68 struct SBitLineMetrics
70 inline bool sanitize (hb_sanitize_context_t *c) const
72 TRACE_SANITIZE (this);
73 return_trace (c->check_struct (this));
79 HBINT8 caretSlopeNumerator;
80 HBINT8 caretSlopeDenominator;
89 DEFINE_SIZE_STATIC(12);
97 struct IndexSubtableHeader
99 inline bool sanitize (hb_sanitize_context_t *c) const
101 TRACE_SANITIZE (this);
102 return_trace (c->check_struct (this));
105 HBUINT16 indexFormat;
106 HBUINT16 imageFormat;
107 HBUINT32 imageDataOffset;
109 DEFINE_SIZE_STATIC(8);
112 template <typename OffsetType>
113 struct IndexSubtableFormat1Or3
115 inline bool sanitize (hb_sanitize_context_t *c, unsigned int glyph_count) const
117 TRACE_SANITIZE (this);
118 return_trace (c->check_struct (this) &&
119 c->check_array (offsetArrayZ, offsetArrayZ[0].static_size, glyph_count + 1));
122 bool get_image_data (unsigned int idx,
123 unsigned int *offset,
124 unsigned int *length) const
126 if (unlikely (offsetArrayZ[idx + 1] <= offsetArrayZ[idx]))
129 *offset = header.imageDataOffset + offsetArrayZ[idx];
130 *length = offsetArrayZ[idx + 1] - offsetArrayZ[idx];
134 IndexSubtableHeader header;
135 Offset<OffsetType> offsetArrayZ[VAR];
137 DEFINE_SIZE_ARRAY(8, offsetArrayZ);
140 struct IndexSubtableFormat1 : IndexSubtableFormat1Or3<HBUINT32> {};
141 struct IndexSubtableFormat3 : IndexSubtableFormat1Or3<HBUINT16> {};
145 inline bool sanitize (hb_sanitize_context_t *c, unsigned int glyph_count) const
147 TRACE_SANITIZE (this);
148 if (!u.header.sanitize (c)) return_trace (false);
149 switch (u.header.indexFormat) {
150 case 1: return_trace (u.format1.sanitize (c, glyph_count));
151 case 3: return_trace (u.format3.sanitize (c, glyph_count));
152 default:return_trace (true);
156 inline bool get_extents (hb_glyph_extents_t *extents) const
158 switch (u.header.indexFormat) {
159 case 2: case 5: /* TODO */
160 case 1: case 3: case 4: /* Variable-metrics formats do not have metrics here. */
161 default:return (false);
165 bool get_image_data (unsigned int idx,
166 unsigned int *offset,
167 unsigned int *length,
168 unsigned int *format) const
170 *format = u.header.imageFormat;
171 switch (u.header.indexFormat) {
172 case 1: return u.format1.get_image_data (idx, offset, length);
173 case 3: return u.format3.get_image_data (idx, offset, length);
174 default: return false;
180 IndexSubtableHeader header;
181 IndexSubtableFormat1 format1;
182 IndexSubtableFormat3 format3;
183 /* TODO: Format 2, 4, 5. */
186 DEFINE_SIZE_UNION (8, header);
189 struct IndexSubtableRecord
191 inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
193 TRACE_SANITIZE (this);
194 return_trace (c->check_struct (this) &&
195 firstGlyphIndex <= lastGlyphIndex &&
196 offsetToSubtable.sanitize (c, this, lastGlyphIndex - firstGlyphIndex + 1));
199 inline bool get_extents (hb_glyph_extents_t *extents) const
201 return (this+offsetToSubtable).get_extents (extents);
204 bool get_image_data (unsigned int gid,
205 unsigned int *offset,
206 unsigned int *length,
207 unsigned int *format) const
209 if (gid < firstGlyphIndex || gid > lastGlyphIndex)
213 return (this+offsetToSubtable).get_image_data (gid - firstGlyphIndex,
214 offset, length, format);
217 HBUINT16 firstGlyphIndex;
218 HBUINT16 lastGlyphIndex;
219 LOffsetTo<IndexSubtable> offsetToSubtable;
221 DEFINE_SIZE_STATIC(8);
224 struct IndexSubtableArray
226 inline bool sanitize (hb_sanitize_context_t *c, unsigned int count) const
228 TRACE_SANITIZE (this);
229 if (unlikely (!c->check_array (&indexSubtablesZ, indexSubtablesZ[0].static_size, count)))
230 return_trace (false);
231 for (unsigned int i = 0; i < count; i++)
232 if (unlikely (!indexSubtablesZ[i].sanitize (c, this)))
233 return_trace (false);
238 const IndexSubtableRecord* find_table (hb_codepoint_t glyph, unsigned int numTables) const
240 for (unsigned int i = 0; i < numTables; ++i)
242 unsigned int firstGlyphIndex = indexSubtablesZ[i].firstGlyphIndex;
243 unsigned int lastGlyphIndex = indexSubtablesZ[i].lastGlyphIndex;
244 if (firstGlyphIndex <= glyph && glyph <= lastGlyphIndex) {
245 return &indexSubtablesZ[i];
252 IndexSubtableRecord indexSubtablesZ[VAR];
255 DEFINE_SIZE_ARRAY(0, indexSubtablesZ);
258 struct BitmapSizeTable
262 inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
264 TRACE_SANITIZE (this);
265 return_trace (c->check_struct (this) &&
266 indexSubtableArrayOffset.sanitize (c, base, numberOfIndexSubtables) &&
267 c->check_range (&(base+indexSubtableArrayOffset), indexTablesSize) &&
268 horizontal.sanitize (c) &&
269 vertical.sanitize (c));
272 const IndexSubtableRecord *find_table (hb_codepoint_t glyph, const void *base) const
274 return (base+indexSubtableArrayOffset).find_table (glyph, numberOfIndexSubtables);
278 LOffsetTo<IndexSubtableArray> indexSubtableArrayOffset;
279 HBUINT32 indexTablesSize;
280 HBUINT32 numberOfIndexSubtables;
282 SBitLineMetrics horizontal;
283 SBitLineMetrics vertical;
284 HBUINT16 startGlyphIndex;
285 HBUINT16 endGlyphIndex;
292 DEFINE_SIZE_STATIC(48);
297 * Glyph Bitmap Data Formats.
300 struct GlyphBitmapDataFormat17
302 SmallGlyphMetrics glyphMetrics;
306 DEFINE_SIZE_ARRAY(9, dataZ);
311 * CBLC -- Color Bitmap Location Table
314 #define HB_OT_TAG_CBLC HB_TAG('C','B','L','C')
320 static const hb_tag_t tableTag = HB_OT_TAG_CBLC;
322 inline bool sanitize (hb_sanitize_context_t *c) const
324 TRACE_SANITIZE (this);
325 return_trace (c->check_struct (this) &&
326 likely (version.major == 2 || version.major == 3) &&
327 sizeTables.sanitize (c, this));
331 const IndexSubtableRecord *find_table (hb_codepoint_t glyph,
332 unsigned int *x_ppem, unsigned int *y_ppem) const
334 /* TODO: Make it possible to select strike. */
336 unsigned int count = sizeTables.len;
337 for (uint32_t i = 0; i < count; ++i)
339 unsigned int startGlyphIndex = sizeTables.array[i].startGlyphIndex;
340 unsigned int endGlyphIndex = sizeTables.array[i].endGlyphIndex;
341 if (startGlyphIndex <= glyph && glyph <= endGlyphIndex)
343 *x_ppem = sizeTables[i].ppemX;
344 *y_ppem = sizeTables[i].ppemY;
345 return sizeTables[i].find_table (glyph, this);
353 FixedVersion<> version;
354 LArrayOf<BitmapSizeTable> sizeTables;
357 DEFINE_SIZE_ARRAY(8, sizeTables);
361 * CBDT -- Color Bitmap Data Table
363 #define HB_OT_TAG_CBDT HB_TAG('C','B','D','T')
367 static const hb_tag_t tableTag = HB_OT_TAG_CBDT;
369 inline bool sanitize (hb_sanitize_context_t *c) const
371 TRACE_SANITIZE (this);
372 return_trace (c->check_struct (this) &&
373 likely (version.major == 2 || version.major == 3));
378 inline void init (hb_face_t *face)
380 upem = hb_face_get_upem (face);
382 cblc_blob = Sanitizer<CBLC>().sanitize (face->reference_table (HB_OT_TAG_CBLC));
383 cbdt_blob = Sanitizer<CBDT>().sanitize (face->reference_table (HB_OT_TAG_CBDT));
384 cbdt_len = hb_blob_get_length (cbdt_blob);
386 if (hb_blob_get_length (cblc_blob) == 0) {
389 return; /* Not a bitmap font. */
391 cblc = Sanitizer<CBLC>::lock_instance (cblc_blob);
392 cbdt = Sanitizer<CBDT>::lock_instance (cbdt_blob);
396 inline void fini (void)
398 hb_blob_destroy (this->cblc_blob);
399 hb_blob_destroy (this->cbdt_blob);
402 inline bool get_extents (hb_codepoint_t glyph, hb_glyph_extents_t *extents) const
404 unsigned int x_ppem = upem, y_ppem = upem; /* TODO Use font ppem if available. */
407 return false; // Not a color bitmap font.
409 const IndexSubtableRecord *subtable_record = this->cblc->find_table(glyph, &x_ppem, &y_ppem);
410 if (!subtable_record || !x_ppem || !y_ppem)
413 if (subtable_record->get_extents (extents))
416 unsigned int image_offset = 0, image_length = 0, image_format = 0;
417 if (!subtable_record->get_image_data (glyph, &image_offset, &image_length, &image_format))
421 if (unlikely (image_offset > cbdt_len || cbdt_len - image_offset < image_length))
424 switch (image_format)
427 if (unlikely (image_length < GlyphBitmapDataFormat17::min_size))
430 const GlyphBitmapDataFormat17& glyphFormat17 =
431 StructAtOffset<GlyphBitmapDataFormat17> (this->cbdt, image_offset);
432 glyphFormat17.glyphMetrics.get_extents (extents);
436 // TODO: Support other image formats.
441 /* Convert to the font units. */
442 extents->x_bearing *= upem / (float) x_ppem;
443 extents->y_bearing *= upem / (float) y_ppem;
444 extents->width *= upem / (float) x_ppem;
445 extents->height *= upem / (float) y_ppem;
451 hb_blob_t *cblc_blob;
452 hb_blob_t *cbdt_blob;
456 unsigned int cbdt_len;
462 FixedVersion<>version;
466 DEFINE_SIZE_ARRAY(4, dataZ);
471 #endif /* HB_OT_COLOR_CBDT_TABLE_HH */