2 * Copyright © 2018 Ebrahim Byagowi
3 * Copyright © 2020 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Google Author(s): Calder Kitagawa
28 #ifndef HB_OT_COLOR_SBIX_TABLE_HH
29 #define HB_OT_COLOR_SBIX_TABLE_HH
31 #include "hb-open-type.hh"
32 #include "hb-ot-layout-common.hh"
35 * sbix -- Standard Bitmap Graphics
36 * https://docs.microsoft.com/en-us/typography/opentype/spec/sbix
37 * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6sbix.html
39 #define HB_OT_TAG_sbix HB_TAG('s','b','i','x')
47 SBIXGlyph* copy (hb_serialize_context_t *c, unsigned int data_length) const
49 TRACE_SERIALIZE (this);
50 SBIXGlyph* new_glyph = c->start_embed<SBIXGlyph> ();
51 if (unlikely (!new_glyph)) return_trace (nullptr);
52 if (unlikely (!c->extend_min (new_glyph))) return_trace (nullptr);
54 new_glyph->xOffset = xOffset;
55 new_glyph->yOffset = yOffset;
56 new_glyph->graphicType = graphicType;
57 data.copy (c, data_length);
58 return_trace (new_glyph);
61 HBINT16 xOffset; /* The horizontal (x-axis) offset from the left
62 * edge of the graphic to the glyph’s origin.
63 * That is, the x-coordinate of the point on the
64 * baseline at the left edge of the glyph. */
65 HBINT16 yOffset; /* The vertical (y-axis) offset from the bottom
66 * edge of the graphic to the glyph’s origin.
67 * That is, the y-coordinate of the point on the
68 * baseline at the left edge of the glyph. */
69 Tag graphicType; /* Indicates the format of the embedded graphic
70 * data: one of 'jpg ', 'png ' or 'tiff', or the
71 * special format 'dupe'. */
72 UnsizedArrayOf<HBUINT8>
73 data; /* The actual embedded graphic data. The total
74 * length is inferred from sequential entries in
75 * the glyphDataOffsets array and the fixed size
76 * (8 bytes) of the preceding fields. */
78 DEFINE_SIZE_ARRAY (8, data);
83 static unsigned int get_size (unsigned num_glyphs)
84 { return min_size + num_glyphs * HBUINT32::static_size; }
86 bool sanitize (hb_sanitize_context_t *c) const
88 TRACE_SANITIZE (this);
89 return_trace (c->check_struct (this) &&
90 imageOffsetsZ.sanitize_shallow (c, c->get_num_glyphs () + 1));
93 hb_blob_t *get_glyph_blob (unsigned int glyph_id,
98 unsigned int num_glyphs,
99 unsigned int *strike_ppem) const
101 if (unlikely (!ppem)) return hb_blob_get_empty (); /* To get Null() object out of the way. */
103 unsigned int retry_count = 8;
104 unsigned int sbix_len = sbix_blob->length;
105 unsigned int strike_offset = (const char *) this - (const char *) sbix_blob->data;
106 assert (strike_offset < sbix_len);
109 if (unlikely (glyph_id >= num_glyphs ||
110 imageOffsetsZ[glyph_id + 1] <= imageOffsetsZ[glyph_id] ||
111 imageOffsetsZ[glyph_id + 1] - imageOffsetsZ[glyph_id] <= SBIXGlyph::min_size ||
112 (unsigned int) imageOffsetsZ[glyph_id + 1] > sbix_len - strike_offset))
113 return hb_blob_get_empty ();
115 unsigned int glyph_offset = strike_offset + (unsigned int) imageOffsetsZ[glyph_id] + SBIXGlyph::min_size;
116 unsigned int glyph_length = imageOffsetsZ[glyph_id + 1] - imageOffsetsZ[glyph_id] - SBIXGlyph::min_size;
118 const SBIXGlyph *glyph = &(this+imageOffsetsZ[glyph_id]);
120 if (glyph->graphicType == HB_TAG ('d','u','p','e'))
122 if (glyph_length >= 2)
124 glyph_id = *((HBUINT16 *) &glyph->data);
128 return hb_blob_get_empty ();
131 if (unlikely (file_type != glyph->graphicType))
132 return hb_blob_get_empty ();
134 if (strike_ppem) *strike_ppem = ppem;
135 if (x_offset) *x_offset = glyph->xOffset;
136 if (y_offset) *y_offset = glyph->yOffset;
137 return hb_blob_create_sub_blob (sbix_blob, glyph_offset, glyph_length);
140 bool subset (hb_subset_context_t *c, unsigned int available_len) const
143 unsigned int num_output_glyphs = c->plan->num_output_glyphs ();
145 auto* out = c->serializer->start_embed<SBIXStrike> ();
146 if (unlikely (!out)) return_trace (false);
147 auto snap = c->serializer->snapshot ();
148 if (unlikely (!c->serializer->extend (*out, num_output_glyphs + 1))) return_trace (false);
150 out->resolution = resolution;
152 head = get_size (num_output_glyphs + 1);
154 bool has_glyphs = false;
155 for (unsigned new_gid = 0; new_gid < num_output_glyphs; new_gid++)
157 hb_codepoint_t old_gid;
158 if (!c->plan->old_gid_for_new_gid (new_gid, &old_gid) ||
159 unlikely (imageOffsetsZ[old_gid].is_null () ||
160 imageOffsetsZ[old_gid + 1].is_null () ||
161 imageOffsetsZ[old_gid + 1] <= imageOffsetsZ[old_gid] ||
162 imageOffsetsZ[old_gid + 1] - imageOffsetsZ[old_gid] <= SBIXGlyph::min_size) ||
163 (unsigned int) imageOffsetsZ[old_gid + 1] > available_len)
165 out->imageOffsetsZ[new_gid] = head;
169 unsigned int delta = imageOffsetsZ[old_gid + 1] - imageOffsetsZ[old_gid];
170 unsigned int glyph_data_length = delta - SBIXGlyph::min_size;
171 if (!(this+imageOffsetsZ[old_gid]).copy (c->serializer, glyph_data_length))
172 return_trace (false);
173 out->imageOffsetsZ[new_gid] = head;
177 out->imageOffsetsZ[num_output_glyphs] = head;
179 c->serializer->revert (snap);
180 return_trace (has_glyphs);
184 HBUINT16 ppem; /* The PPEM size for which this strike was designed. */
185 HBUINT16 resolution; /* The device pixel density (in PPI) for which this
186 * strike was designed. (E.g., 96 PPI, 192 PPI.) */
188 UnsizedArrayOf<LOffsetTo<SBIXGlyph>>
189 imageOffsetsZ; /* Offset from the beginning of the strike data header
190 * to bitmap data for an individual glyph ID. */
192 DEFINE_SIZE_ARRAY (4, imageOffsetsZ);
197 static constexpr hb_tag_t tableTag = HB_OT_TAG_sbix;
199 bool has_data () const { return version; }
201 const SBIXStrike &get_strike (unsigned int i) const { return this+strikes[i]; }
205 void init (hb_face_t *face)
207 table = hb_sanitize_context_t ().reference_table<sbix> (face);
208 num_glyphs = face->get_num_glyphs ();
210 void fini () { table.destroy (); }
212 bool has_data () const { return table->has_data (); }
214 bool get_extents (hb_font_t *font,
215 hb_codepoint_t glyph,
216 hb_glyph_extents_t *extents) const
218 /* We only support PNG right now, and following function checks type. */
219 return get_png_extents (font, glyph, extents);
222 hb_blob_t *reference_png (hb_font_t *font,
223 hb_codepoint_t glyph_id,
226 unsigned int *available_ppem) const
228 return choose_strike (font).get_glyph_blob (glyph_id, table.get_blob (),
229 HB_TAG ('p','n','g',' '),
231 num_glyphs, available_ppem);
236 const SBIXStrike &choose_strike (hb_font_t *font) const
238 unsigned count = table->strikes.len;
239 if (unlikely (!count))
240 return Null (SBIXStrike);
242 unsigned int requested_ppem = hb_max (font->x_ppem, font->y_ppem);
244 requested_ppem = 1<<30; /* Choose largest strike. */
245 /* TODO Add DPI sensitivity as well? */
246 unsigned int best_i = 0;
247 unsigned int best_ppem = table->get_strike (0).ppem;
249 for (unsigned int i = 1; i < count; i++)
251 unsigned int ppem = (table->get_strike (i)).ppem;
252 if ((requested_ppem <= ppem && ppem < best_ppem) ||
253 (requested_ppem > best_ppem && ppem > best_ppem))
260 return table->get_strike (best_i);
265 HBUINT8 signature[8];
277 HBUINT8 compressionMethod;
278 HBUINT8 filterMethod;
279 HBUINT8 interlaceMethod;
283 DEFINE_SIZE_STATIC (29);
286 bool get_png_extents (hb_font_t *font,
287 hb_codepoint_t glyph,
288 hb_glyph_extents_t *extents) const
290 /* Following code is safe to call even without data.
291 * But faster to short-circuit. */
295 int x_offset = 0, y_offset = 0;
296 unsigned int strike_ppem = 0;
297 hb_blob_t *blob = reference_png (font, glyph, &x_offset, &y_offset, &strike_ppem);
299 const PNGHeader &png = *blob->as<PNGHeader>();
301 extents->x_bearing = x_offset;
302 extents->y_bearing = png.IHDR.height + y_offset;
303 extents->width = png.IHDR.width;
304 extents->height = -1 * png.IHDR.height;
306 /* Convert to font units. */
309 float scale = font->face->get_upem () / (float) strike_ppem;
310 extents->x_bearing = font->em_scalef_x (extents->x_bearing * scale);
311 extents->y_bearing = font->em_scalef_y (extents->y_bearing * scale);
312 extents->width = font->em_scalef_x (extents->width * scale);
313 extents->height = font->em_scalef_y (extents->height * scale);
317 extents->x_bearing = font->em_scale_x (extents->x_bearing);
318 extents->y_bearing = font->em_scale_y (extents->y_bearing);
319 extents->width = font->em_scale_x (extents->width);
320 extents->height = font->em_scale_y (extents->height);
323 hb_blob_destroy (blob);
329 hb_blob_ptr_t<sbix> table;
331 unsigned int num_glyphs;
334 bool sanitize (hb_sanitize_context_t *c) const
336 TRACE_SANITIZE (this);
337 return_trace (likely (c->check_struct (this) &&
339 strikes.sanitize (c, this)));
343 add_strike (hb_subset_context_t *c, unsigned i) const
345 if (strikes[i].is_null () || c->source_blob->length < (unsigned) strikes[i])
348 return (this+strikes[i]).subset (c, c->source_blob->length - (unsigned) strikes[i]);
351 bool serialize_strike_offsets (hb_subset_context_t *c) const
353 TRACE_SERIALIZE (this);
355 auto *out = c->serializer->start_embed<LOffsetLArrayOf<SBIXStrike>> ();
356 if (unlikely (!out)) return_trace (false);
357 if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
359 hb_vector_t<LOffsetTo<SBIXStrike>*> new_strikes;
360 hb_vector_t<hb_serialize_context_t::objidx_t> objidxs;
361 for (int i = strikes.len - 1; i >= 0; --i)
363 auto* o = out->serialize_append (c->serializer);
364 if (unlikely (!o)) return_trace (false);
366 auto snap = c->serializer->snapshot ();
367 c->serializer->push ();
368 bool ret = add_strike (c, i);
371 c->serializer->pop_discard ();
373 c->serializer->revert (snap);
377 objidxs.push (c->serializer->pop_pack ());
378 new_strikes.push (o);
381 for (unsigned int i = 0; i < new_strikes.length; ++i)
382 c->serializer->add_link (*new_strikes[i], objidxs[new_strikes.length - 1 - i]);
387 bool subset (hb_subset_context_t* c) const
391 sbix *sbix_prime = c->serializer->start_embed<sbix> ();
392 if (unlikely (!sbix_prime)) return_trace (false);
393 if (unlikely (!c->serializer->embed (this->version))) return_trace (false);
394 if (unlikely (!c->serializer->embed (this->flags))) return_trace (false);
396 return_trace (serialize_strike_offsets (c));
400 HBUINT16 version; /* Table version number — set to 1 */
401 HBUINT16 flags; /* Bit 0: Set to 1. Bit 1: Draw outlines.
402 * Bits 2 to 15: reserved (set to 0). */
403 LOffsetLArrayOf<SBIXStrike>
404 strikes; /* Offsets from the beginning of the 'sbix'
405 * table to data for each individual bitmap strike. */
407 DEFINE_SIZE_ARRAY (8, strikes);
410 struct sbix_accelerator_t : sbix::accelerator_t {};
414 #endif /* HB_OT_COLOR_SBIX_TABLE_HH */