| 1 | /* |
| 2 | * Copyright © 2018 Ebrahim Byagowi |
| 3 | * Copyright © 2020 Google, Inc. |
| 4 | * |
| 5 | * This is part of HarfBuzz, a text shaping library. |
| 6 | * |
| 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. |
| 12 | * |
| 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 |
| 17 | * DAMAGE. |
| 18 | * |
| 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. |
| 24 | * |
| 25 | * Google Author(s): Calder Kitagawa |
| 26 | */ |
| 27 | |
| 28 | #ifndef OT_COLOR_SBIX_SBIX_HH |
| 29 | #define OT_COLOR_SBIX_SBIX_HH |
| 30 | |
| 31 | #include "../../../hb-open-type.hh" |
| 32 | #include "../../../hb-paint.hh" |
| 33 | |
| 34 | /* |
| 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 |
| 38 | */ |
| 39 | #define HB_OT_TAG_sbix HB_TAG('s','b','i','x') |
| 40 | |
| 41 | |
| 42 | namespace OT { |
| 43 | |
| 44 | |
| 45 | struct SBIXGlyph |
| 46 | { |
| 47 | SBIXGlyph* copy (hb_serialize_context_t *c, unsigned int data_length) const |
| 48 | { |
| 49 | TRACE_SERIALIZE (this); |
| 50 | SBIXGlyph* new_glyph = c->start_embed<SBIXGlyph> (); |
| 51 | if (unlikely (!c->extend_min (new_glyph))) return_trace (nullptr); |
| 52 | |
| 53 | new_glyph->xOffset = xOffset; |
| 54 | new_glyph->yOffset = yOffset; |
| 55 | new_glyph->graphicType = graphicType; |
| 56 | data.copy (c, data_length); |
| 57 | return_trace (new_glyph); |
| 58 | } |
| 59 | |
| 60 | HBINT16 xOffset; /* The horizontal (x-axis) offset from the left |
| 61 | * edge of the graphic to the glyph’s origin. |
| 62 | * That is, the x-coordinate of the point on the |
| 63 | * baseline at the left edge of the glyph. */ |
| 64 | HBINT16 yOffset; /* The vertical (y-axis) offset from the bottom |
| 65 | * edge of the graphic to the glyph’s origin. |
| 66 | * That is, the y-coordinate of the point on the |
| 67 | * baseline at the left edge of the glyph. */ |
| 68 | Tag graphicType; /* Indicates the format of the embedded graphic |
| 69 | * data: one of 'jpg ', 'png ' or 'tiff', or the |
| 70 | * special format 'dupe'. */ |
| 71 | UnsizedArrayOf<HBUINT8> |
| 72 | data; /* The actual embedded graphic data. The total |
| 73 | * length is inferred from sequential entries in |
| 74 | * the glyphDataOffsets array and the fixed size |
| 75 | * (8 bytes) of the preceding fields. */ |
| 76 | public: |
| 77 | DEFINE_SIZE_ARRAY (8, data); |
| 78 | }; |
| 79 | |
| 80 | struct SBIXStrike |
| 81 | { |
| 82 | static unsigned int get_size (unsigned num_glyphs) |
| 83 | { return min_size + num_glyphs * HBUINT32::static_size; } |
| 84 | |
| 85 | bool sanitize (hb_sanitize_context_t *c) const |
| 86 | { |
| 87 | TRACE_SANITIZE (this); |
| 88 | return_trace (c->check_struct (this) && |
| 89 | imageOffsetsZ.sanitize_shallow (c, c->get_num_glyphs () + 1)); |
| 90 | } |
| 91 | |
| 92 | hb_blob_t *get_glyph_blob (unsigned int glyph_id, |
| 93 | hb_blob_t *sbix_blob, |
| 94 | hb_tag_t file_type, |
| 95 | int *x_offset, |
| 96 | int *y_offset, |
| 97 | unsigned int num_glyphs, |
| 98 | unsigned int *strike_ppem) const |
| 99 | { |
| 100 | if (unlikely (!ppem)) return hb_blob_get_empty (); /* To get Null() object out of the way. */ |
| 101 | |
| 102 | unsigned int retry_count = 8; |
| 103 | unsigned int sbix_len = sbix_blob->length; |
| 104 | unsigned int strike_offset = (const char *) this - (const char *) sbix_blob->data; |
| 105 | assert (strike_offset < sbix_len); |
| 106 | |
| 107 | retry: |
| 108 | if (unlikely (glyph_id >= num_glyphs || |
| 109 | imageOffsetsZ[glyph_id + 1] <= imageOffsetsZ[glyph_id] || |
| 110 | imageOffsetsZ[glyph_id + 1] - imageOffsetsZ[glyph_id] <= SBIXGlyph::min_size || |
| 111 | (unsigned int) imageOffsetsZ[glyph_id + 1] > sbix_len - strike_offset)) |
| 112 | return hb_blob_get_empty (); |
| 113 | |
| 114 | unsigned int glyph_offset = strike_offset + (unsigned int) imageOffsetsZ[glyph_id] + SBIXGlyph::min_size; |
| 115 | unsigned int glyph_length = imageOffsetsZ[glyph_id + 1] - imageOffsetsZ[glyph_id] - SBIXGlyph::min_size; |
| 116 | |
| 117 | const SBIXGlyph *glyph = &(this+imageOffsetsZ[glyph_id]); |
| 118 | |
| 119 | if (glyph->graphicType == HB_TAG ('d','u','p','e')) |
| 120 | { |
| 121 | if (glyph_length >= 2) |
| 122 | { |
| 123 | glyph_id = *((HBUINT16 *) &glyph->data); |
| 124 | if (retry_count--) |
| 125 | goto retry; |
| 126 | } |
| 127 | return hb_blob_get_empty (); |
| 128 | } |
| 129 | |
| 130 | if (unlikely (file_type != glyph->graphicType)) |
| 131 | return hb_blob_get_empty (); |
| 132 | |
| 133 | if (strike_ppem) *strike_ppem = ppem; |
| 134 | if (x_offset) *x_offset = glyph->xOffset; |
| 135 | if (y_offset) *y_offset = glyph->yOffset; |
| 136 | return hb_blob_create_sub_blob (sbix_blob, glyph_offset, glyph_length); |
| 137 | } |
| 138 | |
| 139 | bool subset (hb_subset_context_t *c, unsigned int available_len) const |
| 140 | { |
| 141 | TRACE_SUBSET (this); |
| 142 | unsigned int num_output_glyphs = c->plan->num_output_glyphs (); |
| 143 | |
| 144 | auto* out = c->serializer->start_embed<SBIXStrike> (); |
| 145 | auto snap = c->serializer->snapshot (); |
| 146 | if (unlikely (!c->serializer->extend (out, num_output_glyphs + 1))) return_trace (false); |
| 147 | out->ppem = ppem; |
| 148 | out->resolution = resolution; |
| 149 | HBUINT32 head; |
| 150 | head = get_size (num_output_glyphs + 1); |
| 151 | |
| 152 | bool has_glyphs = false; |
| 153 | for (unsigned new_gid = 0; new_gid < num_output_glyphs; new_gid++) |
| 154 | { |
| 155 | hb_codepoint_t old_gid; |
| 156 | if (!c->plan->old_gid_for_new_gid (new_gid, &old_gid) || |
| 157 | unlikely (imageOffsetsZ[old_gid].is_null () || |
| 158 | imageOffsetsZ[old_gid + 1].is_null () || |
| 159 | imageOffsetsZ[old_gid + 1] <= imageOffsetsZ[old_gid] || |
| 160 | imageOffsetsZ[old_gid + 1] - imageOffsetsZ[old_gid] <= SBIXGlyph::min_size) || |
| 161 | (unsigned int) imageOffsetsZ[old_gid + 1] > available_len) |
| 162 | { |
| 163 | out->imageOffsetsZ[new_gid] = head; |
| 164 | continue; |
| 165 | } |
| 166 | has_glyphs = true; |
| 167 | unsigned int delta = imageOffsetsZ[old_gid + 1] - imageOffsetsZ[old_gid]; |
| 168 | unsigned int glyph_data_length = delta - SBIXGlyph::min_size; |
| 169 | if (!(this+imageOffsetsZ[old_gid]).copy (c->serializer, glyph_data_length)) |
| 170 | return_trace (false); |
| 171 | out->imageOffsetsZ[new_gid] = head; |
| 172 | head += delta; |
| 173 | } |
| 174 | if (has_glyphs) |
| 175 | out->imageOffsetsZ[num_output_glyphs] = head; |
| 176 | else |
| 177 | c->serializer->revert (snap); |
| 178 | return_trace (has_glyphs); |
| 179 | } |
| 180 | |
| 181 | public: |
| 182 | HBUINT16 ppem; /* The PPEM size for which this strike was designed. */ |
| 183 | HBUINT16 resolution; /* The device pixel density (in PPI) for which this |
| 184 | * strike was designed. (E.g., 96 PPI, 192 PPI.) */ |
| 185 | protected: |
| 186 | UnsizedArrayOf<Offset32To<SBIXGlyph>> |
| 187 | imageOffsetsZ; /* Offset from the beginning of the strike data header |
| 188 | * to bitmap data for an individual glyph ID. */ |
| 189 | public: |
| 190 | DEFINE_SIZE_ARRAY (4, imageOffsetsZ); |
| 191 | }; |
| 192 | |
| 193 | struct sbix |
| 194 | { |
| 195 | static constexpr hb_tag_t tableTag = HB_OT_TAG_sbix; |
| 196 | |
| 197 | bool has_data () const { return version; } |
| 198 | |
| 199 | const SBIXStrike &get_strike (unsigned int i) const { return this+strikes[i]; } |
| 200 | |
| 201 | struct accelerator_t |
| 202 | { |
| 203 | accelerator_t (hb_face_t *face) |
| 204 | { |
| 205 | table = hb_sanitize_context_t ().reference_table<sbix> (face); |
| 206 | num_glyphs = face->get_num_glyphs (); |
| 207 | } |
| 208 | ~accelerator_t () { table.destroy (); } |
| 209 | |
| 210 | bool has_data () const { return table->has_data (); } |
| 211 | |
| 212 | bool get_extents (hb_font_t *font, |
| 213 | hb_codepoint_t glyph, |
| 214 | hb_glyph_extents_t *extents, |
| 215 | bool scale = true) const |
| 216 | { |
| 217 | /* We only support PNG right now, and following function checks type. */ |
| 218 | return get_png_extents (font, glyph, extents, scale); |
| 219 | } |
| 220 | |
| 221 | hb_blob_t *reference_png (hb_font_t *font, |
| 222 | hb_codepoint_t glyph_id, |
| 223 | int *x_offset, |
| 224 | int *y_offset, |
| 225 | unsigned int *available_ppem) const |
| 226 | { |
| 227 | return choose_strike (font).get_glyph_blob (glyph_id, table.get_blob (), |
| 228 | HB_TAG ('p','n','g',' '), |
| 229 | x_offset, y_offset, |
| 230 | num_glyphs, available_ppem); |
| 231 | } |
| 232 | |
| 233 | bool paint_glyph (hb_font_t *font, hb_codepoint_t glyph, hb_paint_funcs_t *funcs, void *data) const |
| 234 | { |
| 235 | if (!has_data ()) |
| 236 | return false; |
| 237 | |
| 238 | int x_offset = 0, y_offset = 0; |
| 239 | unsigned int strike_ppem = 0; |
| 240 | hb_blob_t *blob = reference_png (font, glyph, &x_offset, &y_offset, &strike_ppem); |
| 241 | hb_glyph_extents_t extents; |
| 242 | hb_glyph_extents_t pixel_extents; |
| 243 | |
| 244 | if (blob == hb_blob_get_empty ()) |
| 245 | return false; |
| 246 | |
| 247 | if (!hb_font_get_glyph_extents (font, glyph, &extents)) |
| 248 | return false; |
| 249 | |
| 250 | if (unlikely (!get_extents (font, glyph, &pixel_extents, false))) |
| 251 | return false; |
| 252 | |
| 253 | bool ret = funcs->image (data, |
| 254 | blob, |
| 255 | pixel_extents.width, -pixel_extents.height, |
| 256 | HB_PAINT_IMAGE_FORMAT_PNG, |
| 257 | font->slant_xy, |
| 258 | &extents); |
| 259 | |
| 260 | hb_blob_destroy (blob); |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | private: |
| 265 | |
| 266 | const SBIXStrike &choose_strike (hb_font_t *font) const |
| 267 | { |
| 268 | unsigned count = table->strikes.len; |
| 269 | if (unlikely (!count)) |
| 270 | return Null (SBIXStrike); |
| 271 | |
| 272 | unsigned int requested_ppem = hb_max (font->x_ppem, font->y_ppem); |
| 273 | if (!requested_ppem) |
| 274 | requested_ppem = 1<<30; /* Choose largest strike. */ |
| 275 | /* TODO Add DPI sensitivity as well? */ |
| 276 | unsigned int best_i = 0; |
| 277 | unsigned int best_ppem = table->get_strike (0).ppem; |
| 278 | |
| 279 | for (unsigned int i = 1; i < count; i++) |
| 280 | { |
| 281 | unsigned int ppem = (table->get_strike (i)).ppem; |
| 282 | if ((requested_ppem <= ppem && ppem < best_ppem) || |
| 283 | (requested_ppem > best_ppem && ppem > best_ppem)) |
| 284 | { |
| 285 | best_i = i; |
| 286 | best_ppem = ppem; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return table->get_strike (best_i); |
| 291 | } |
| 292 | |
| 293 | struct |
| 294 | { |
| 295 | HBUINT8 [8]; |
| 296 | struct |
| 297 | { |
| 298 | struct |
| 299 | { |
| 300 | HBUINT32 ; |
| 301 | Tag ; |
| 302 | } ; |
| 303 | HBUINT32 ; |
| 304 | HBUINT32 ; |
| 305 | HBUINT8 ; |
| 306 | HBUINT8 ; |
| 307 | HBUINT8 ; |
| 308 | HBUINT8 ; |
| 309 | HBUINT8 ; |
| 310 | } ; |
| 311 | |
| 312 | public: |
| 313 | DEFINE_SIZE_STATIC (29); |
| 314 | }; |
| 315 | |
| 316 | bool get_png_extents (hb_font_t *font, |
| 317 | hb_codepoint_t glyph, |
| 318 | hb_glyph_extents_t *extents, |
| 319 | bool scale = true) const |
| 320 | { |
| 321 | /* Following code is safe to call even without data. |
| 322 | * But faster to short-circuit. */ |
| 323 | if (!has_data ()) |
| 324 | return false; |
| 325 | |
| 326 | int x_offset = 0, y_offset = 0; |
| 327 | unsigned int strike_ppem = 0; |
| 328 | hb_blob_t *blob = reference_png (font, glyph, &x_offset, &y_offset, &strike_ppem); |
| 329 | |
| 330 | const PNGHeader &png = *blob->as<PNGHeader>(); |
| 331 | |
| 332 | if (png.IHDR.height >= 65536 || png.IHDR.width >= 65536) |
| 333 | { |
| 334 | hb_blob_destroy (blob); |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | extents->x_bearing = x_offset; |
| 339 | extents->y_bearing = png.IHDR.height + y_offset; |
| 340 | extents->width = png.IHDR.width; |
| 341 | extents->height = -1 * png.IHDR.height; |
| 342 | |
| 343 | /* Convert to font units. */ |
| 344 | if (strike_ppem && scale) |
| 345 | { |
| 346 | float scale = font->face->get_upem () / (float) strike_ppem; |
| 347 | extents->x_bearing = roundf (extents->x_bearing * scale); |
| 348 | extents->y_bearing = roundf (extents->y_bearing * scale); |
| 349 | extents->width = roundf (extents->width * scale); |
| 350 | extents->height = roundf (extents->height * scale); |
| 351 | } |
| 352 | |
| 353 | if (scale) |
| 354 | font->scale_glyph_extents (extents); |
| 355 | |
| 356 | hb_blob_destroy (blob); |
| 357 | |
| 358 | return strike_ppem; |
| 359 | } |
| 360 | |
| 361 | private: |
| 362 | hb_blob_ptr_t<sbix> table; |
| 363 | |
| 364 | unsigned int num_glyphs; |
| 365 | }; |
| 366 | |
| 367 | bool sanitize (hb_sanitize_context_t *c) const |
| 368 | { |
| 369 | TRACE_SANITIZE (this); |
| 370 | return_trace (likely (c->check_struct (this) && |
| 371 | version >= 1 && |
| 372 | strikes.sanitize (c, this))); |
| 373 | } |
| 374 | |
| 375 | bool |
| 376 | add_strike (hb_subset_context_t *c, unsigned i) const |
| 377 | { |
| 378 | if (strikes[i].is_null () || c->source_blob->length < (unsigned) strikes[i]) |
| 379 | return false; |
| 380 | |
| 381 | return (this+strikes[i]).subset (c, c->source_blob->length - (unsigned) strikes[i]); |
| 382 | } |
| 383 | |
| 384 | bool serialize_strike_offsets (hb_subset_context_t *c) const |
| 385 | { |
| 386 | TRACE_SERIALIZE (this); |
| 387 | |
| 388 | auto *out = c->serializer->start_embed<Array32OfOffset32To<SBIXStrike>> (); |
| 389 | if (unlikely (!c->serializer->extend_min (out))) return_trace (false); |
| 390 | |
| 391 | hb_vector_t<Offset32To<SBIXStrike>*> new_strikes; |
| 392 | hb_vector_t<hb_serialize_context_t::objidx_t> objidxs; |
| 393 | for (int i = strikes.len - 1; i >= 0; --i) |
| 394 | { |
| 395 | auto* o = out->serialize_append (c->serializer); |
| 396 | if (unlikely (!o)) return_trace (false); |
| 397 | *o = 0; |
| 398 | auto snap = c->serializer->snapshot (); |
| 399 | c->serializer->push (); |
| 400 | bool ret = add_strike (c, i); |
| 401 | if (!ret) |
| 402 | { |
| 403 | c->serializer->pop_discard (); |
| 404 | out->pop (); |
| 405 | c->serializer->revert (snap); |
| 406 | } |
| 407 | else |
| 408 | { |
| 409 | objidxs.push (c->serializer->pop_pack ()); |
| 410 | new_strikes.push (o); |
| 411 | } |
| 412 | } |
| 413 | for (unsigned int i = 0; i < new_strikes.length; ++i) |
| 414 | c->serializer->add_link (*new_strikes[i], objidxs[new_strikes.length - 1 - i]); |
| 415 | |
| 416 | return_trace (true); |
| 417 | } |
| 418 | |
| 419 | bool subset (hb_subset_context_t* c) const |
| 420 | { |
| 421 | TRACE_SUBSET (this); |
| 422 | |
| 423 | if (unlikely (!c->serializer->embed (this->version))) return_trace (false); |
| 424 | if (unlikely (!c->serializer->embed (this->flags))) return_trace (false); |
| 425 | |
| 426 | return_trace (serialize_strike_offsets (c)); |
| 427 | } |
| 428 | |
| 429 | protected: |
| 430 | HBUINT16 version; /* Table version number — set to 1 */ |
| 431 | HBUINT16 flags; /* Bit 0: Set to 1. Bit 1: Draw outlines. |
| 432 | * Bits 2 to 15: reserved (set to 0). */ |
| 433 | Array32OfOffset32To<SBIXStrike> |
| 434 | strikes; /* Offsets from the beginning of the 'sbix' |
| 435 | * table to data for each individual bitmap strike. */ |
| 436 | public: |
| 437 | DEFINE_SIZE_ARRAY (8, strikes); |
| 438 | }; |
| 439 | |
| 440 | struct sbix_accelerator_t : sbix::accelerator_t { |
| 441 | sbix_accelerator_t (hb_face_t *face) : sbix::accelerator_t (face) {} |
| 442 | }; |
| 443 | |
| 444 | |
| 445 | } /* namespace OT */ |
| 446 | |
| 447 | #endif /* OT_COLOR_SBIX_SBIX_HH */ |
| 448 | |