2 * Copyright © 2019 Adobe Inc.
3 * Copyright © 2019 Ebrahim Byagowi
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 * Adobe Author(s): Michiharu Ariza
28 #ifndef HB_OT_VAR_GVAR_TABLE_HH
29 #define HB_OT_VAR_GVAR_TABLE_HH
31 #include "hb-open-type.hh"
32 #include "hb-ot-glyf-table.hh"
33 #include "hb-ot-var-fvar-table.hh"
36 * gvar -- Glyph Variation Table
37 * https://docs.microsoft.com/en-us/typography/opentype/spec/gvar
39 #define HB_OT_TAG_gvar HB_TAG('g','v','a','r')
43 struct contour_point_t
45 void init (float x_=0.f, float y_=0.f) { flag = 0; x = x_; y = y_; }
47 void translate (const contour_point_t &p) { x += p.x; y += p.y; }
53 struct contour_point_vector_t : hb_vector_t<contour_point_t>
55 void extend (const hb_array_t<contour_point_t> &a)
57 unsigned int old_len = length;
58 resize (old_len + a.length);
59 for (unsigned int i = 0; i < a.length; i++)
60 (*this)[old_len + i] = a[i];
63 void transform (const float (&matrix)[4])
65 for (unsigned int i = 0; i < length; i++)
67 contour_point_t &p = (*this)[i];
68 float x_ = p.x * matrix[0] + p.y * matrix[2];
69 p.y = p.x * matrix[1] + p.y * matrix[3];
74 void translate (const contour_point_t& delta)
76 for (unsigned int i = 0; i < length; i++)
77 (*this)[i].translate (delta);
81 struct Tuple : UnsizedArrayOf<F2DOT14> {};
83 struct TuppleIndex : HBUINT16
86 EmbeddedPeakTuple = 0x8000u,
87 IntermediateRegion = 0x4000u,
88 PrivatePointNumbers = 0x2000u,
89 TupleIndexMask = 0x0FFFu
92 DEFINE_SIZE_STATIC (2);
97 unsigned int get_size (unsigned int axis_count) const
100 (has_peak () ? get_peak_tuple ().get_size (axis_count) : 0) +
101 (has_intermediate () ? (get_start_tuple (axis_count).get_size (axis_count) +
102 get_end_tuple (axis_count).get_size (axis_count)) : 0);
105 const TupleVarHeader &get_next (unsigned int axis_count) const
106 { return StructAtOffset<TupleVarHeader> (this, get_size (axis_count)); }
108 float calculate_scalar (const int *coords, unsigned int coord_count,
109 const hb_array_t<const F2DOT14> shared_tuples) const
111 const F2DOT14 *peak_tuple;
114 peak_tuple = &(get_peak_tuple ()[0]);
117 unsigned int index = get_index ();
118 if (unlikely (index * coord_count >= shared_tuples.length))
120 peak_tuple = &shared_tuples[coord_count * index];
123 const F2DOT14 *start_tuple = nullptr;
124 const F2DOT14 *end_tuple = nullptr;
125 if (has_intermediate ())
127 start_tuple = get_start_tuple (coord_count);
128 end_tuple = get_end_tuple (coord_count);
132 for (unsigned int i = 0; i < coord_count; i++)
135 int peak = peak_tuple[i];
136 if (!peak || v == peak) continue;
138 if (has_intermediate ())
140 int start = start_tuple[i];
141 int end = end_tuple[i];
142 if (unlikely (start > peak || peak > end ||
143 (start < 0 && end > 0 && peak))) continue;
144 if (v < start || v > end) return 0.f;
146 { if (peak != start) scalar *= (float) (v - start) / (peak - start); }
148 { if (peak != end) scalar *= (float) (end - v) / (end - peak); }
150 else if (!v || v < hb_min (0, peak) || v > hb_max (0, peak)) return 0.f;
152 scalar *= (float) v / peak;
157 unsigned int get_data_size () const { return varDataSize; }
159 bool has_peak () const { return (tupleIndex & TuppleIndex::EmbeddedPeakTuple); }
160 bool has_intermediate () const { return (tupleIndex & TuppleIndex::IntermediateRegion); }
161 bool has_private_points () const { return (tupleIndex & TuppleIndex::PrivatePointNumbers); }
162 unsigned int get_index () const { return (tupleIndex & TuppleIndex::TupleIndexMask); }
165 const Tuple &get_peak_tuple () const
166 { return StructAfter<Tuple> (tupleIndex); }
167 const Tuple &get_start_tuple (unsigned int axis_count) const
168 { return *(const Tuple *) &get_peak_tuple ()[has_peak () ? axis_count : 0]; }
169 const Tuple &get_end_tuple (unsigned int axis_count) const
170 { return *(const Tuple *) &get_peak_tuple ()[has_peak () ? (axis_count * 2) : axis_count]; }
172 HBUINT16 varDataSize;
173 TuppleIndex tupleIndex;
174 /* UnsizedArrayOf<F2DOT14> peakTuple - optional */
175 /* UnsizedArrayOf<F2DOT14> intermediateStartTuple - optional */
176 /* UnsizedArrayOf<F2DOT14> intermediateEndTuple - optional */
182 struct TupleVarCount : HBUINT16
184 bool has_shared_point_numbers () const { return ((*this) & SharedPointNumbers); }
185 unsigned int get_count () const { return (*this) & CountMask; }
190 SharedPointNumbers = 0x8000u,
195 DEFINE_SIZE_STATIC (2);
200 const TupleVarHeader &get_tuple_var_header (void) const
201 { return StructAfter<TupleVarHeader> (data); }
203 struct tuple_iterator_t
205 void init (const GlyphVarData *var_data_, unsigned int length_, unsigned int axis_count_)
207 var_data = var_data_;
210 axis_count = axis_count_;
211 current_tuple = &var_data->get_tuple_var_header ();
215 bool get_shared_indices (hb_vector_t<unsigned int> &shared_indices /* OUT */)
217 if (var_data->has_shared_point_numbers ())
219 hb_bytes_t bytes ((const char *) var_data, length);
220 const HBUINT8 *base = &(var_data+var_data->data);
221 const HBUINT8 *p = base;
222 if (!unpack_points (p, shared_indices, bytes)) return false;
223 data_offset = p - base;
228 bool is_valid () const
230 return (index < var_data->tupleVarCount.get_count ()) &&
231 in_range (current_tuple) &&
232 current_tuple->get_size (axis_count);
237 data_offset += current_tuple->get_data_size ();
238 current_tuple = ¤t_tuple->get_next (axis_count);
243 bool in_range (const void *p, unsigned int l) const
244 { return (const char*) p >= (const char*) var_data && (const char*) p+l <= (const char*) var_data + length; }
246 template <typename T> bool in_range (const T *p) const { return in_range (p, sizeof (*p)); }
248 const HBUINT8 *get_serialized_data () const
249 { return &(var_data+var_data->data) + data_offset; }
252 const GlyphVarData *var_data;
255 unsigned int axis_count;
256 unsigned int data_offset;
259 const TupleVarHeader *current_tuple;
262 static bool get_tuple_iterator (const GlyphVarData *var_data,
264 unsigned int axis_count,
265 hb_vector_t<unsigned int> &shared_indices /* OUT */,
266 tuple_iterator_t *iterator /* OUT */)
268 iterator->init (var_data, length, axis_count);
269 if (!iterator->get_shared_indices (shared_indices))
271 return iterator->is_valid ();
274 bool has_shared_point_numbers () const { return tupleVarCount.has_shared_point_numbers (); }
276 static bool unpack_points (const HBUINT8 *&p /* IN/OUT */,
277 hb_vector_t<unsigned int> &points /* OUT */,
278 const hb_bytes_t &bytes)
280 enum packed_point_flag_t
282 POINTS_ARE_WORDS = 0x80,
283 POINT_RUN_COUNT_MASK = 0x7F
286 if (unlikely (!bytes.in_range (p))) return false;
288 uint16_t count = *p++;
289 if (count & POINTS_ARE_WORDS)
291 if (unlikely (!bytes.in_range (p))) return false;
292 count = ((count & POINT_RUN_COUNT_MASK) << 8) | *p++;
294 points.resize (count);
300 if (unlikely (!bytes.in_range (p))) return false;
302 uint8_t control = *p++;
303 uint16_t run_count = (control & POINT_RUN_COUNT_MASK) + 1;
304 if (control & POINTS_ARE_WORDS)
306 for (j = 0; j < run_count && i < count; j++, i++)
308 if (unlikely (!bytes.in_range ((const HBUINT16 *) p)))
310 n += *(const HBUINT16 *)p;
312 p += HBUINT16::static_size;
317 for (j = 0; j < run_count && i < count; j++, i++)
319 if (unlikely (!bytes.in_range (p))) return false;
324 if (j < run_count) return false;
329 static bool unpack_deltas (const HBUINT8 *&p /* IN/OUT */,
330 hb_vector_t<int> &deltas /* IN/OUT */,
331 const hb_bytes_t &bytes)
333 enum packed_delta_flag_t
335 DELTAS_ARE_ZERO = 0x80,
336 DELTAS_ARE_WORDS = 0x40,
337 DELTA_RUN_COUNT_MASK = 0x3F
341 unsigned int count = deltas.length;
344 if (unlikely (!bytes.in_range (p))) return false;
345 uint8_t control = *p++;
346 unsigned int run_count = (control & DELTA_RUN_COUNT_MASK) + 1;
348 if (control & DELTAS_ARE_ZERO)
349 for (j = 0; j < run_count && i < count; j++, i++)
351 else if (control & DELTAS_ARE_WORDS)
352 for (j = 0; j < run_count && i < count; j++, i++)
354 if (unlikely (!bytes.in_range ((const HBUINT16 *) p)))
356 deltas[i] = *(const HBINT16 *) p;
357 p += HBUINT16::static_size;
360 for (j = 0; j < run_count && i < count; j++, i++)
362 if (unlikely (!bytes.in_range (p)))
364 deltas[i] = *(const HBINT8 *) p++;
373 TupleVarCount tupleVarCount;
374 OffsetTo<HBUINT8> data;
375 /* TupleVarHeader tupleVarHeaders[] */
382 static constexpr hb_tag_t tableTag = HB_OT_TAG_gvar;
384 bool sanitize_shallow (hb_sanitize_context_t *c) const
386 TRACE_SANITIZE (this);
387 return_trace (c->check_struct (this) && (version.major == 1) &&
388 (glyphCount == c->get_num_glyphs ()) &&
389 c->check_array (&(this+sharedTuples), axisCount * sharedTupleCount) &&
391 c->check_array (get_long_offset_array (), glyphCount+1) :
392 c->check_array (get_short_offset_array (), glyphCount+1)) &&
393 c->check_array (((const HBUINT8*)&(this+dataZ)) + get_offset (0),
394 get_offset (glyphCount) - get_offset (0)));
397 /* GlyphVarData not sanitized here; must be checked while accessing each glyph varation data */
398 bool sanitize (hb_sanitize_context_t *c) const
399 { return sanitize_shallow (c); }
401 bool subset (hb_subset_context_t *c) const
405 gvar *out = c->serializer->allocate_min<gvar> ();
406 if (unlikely (!out)) return_trace (false);
408 out->version.major = 1;
409 out->version.minor = 0;
410 out->axisCount = axisCount;
411 out->sharedTupleCount = sharedTupleCount;
413 unsigned int num_glyphs = c->plan->num_output_glyphs ();
414 out->glyphCount = num_glyphs;
416 unsigned int subset_data_size = 0;
417 for (hb_codepoint_t gid = 0; gid < num_glyphs; gid++)
419 hb_codepoint_t old_gid;
420 if (!c->plan->old_gid_for_new_gid (gid, &old_gid)) continue;
421 subset_data_size += get_glyph_var_data_length (old_gid);
424 bool long_offset = subset_data_size & ~0xFFFFu;
425 out->flags = long_offset ? 1 : 0;
427 HBUINT8 *subset_offsets = c->serializer->allocate_size<HBUINT8> ((long_offset ? 4 : 2) * (num_glyphs + 1));
428 if (!subset_offsets) return_trace (false);
431 if (!sharedTupleCount || !sharedTuples)
432 out->sharedTuples = 0;
435 unsigned int shared_tuple_size = F2DOT14::static_size * axisCount * sharedTupleCount;
436 F2DOT14 *tuples = c->serializer->allocate_size<F2DOT14> (shared_tuple_size);
437 if (!tuples) return_trace (false);
438 out->sharedTuples = (char *) tuples - (char *) out;
439 memcpy (tuples, &(this+sharedTuples), shared_tuple_size);
442 char *subset_data = c->serializer->allocate_size<char> (subset_data_size);
443 if (!subset_data) return_trace (false);
444 out->dataZ = subset_data - (char *)out;
446 unsigned int glyph_offset = 0;
447 for (hb_codepoint_t gid = 0; gid < num_glyphs; gid++)
449 hb_codepoint_t old_gid;
450 unsigned int length = c->plan->old_gid_for_new_gid (gid, &old_gid) ? get_glyph_var_data_length (old_gid) : 0;
453 ((HBUINT32 *) subset_offsets)[gid] = glyph_offset;
455 ((HBUINT16 *) subset_offsets)[gid] = glyph_offset / 2;
457 if (length > 0) memcpy (subset_data, get_glyph_var_data (old_gid), length);
458 subset_data += length;
459 glyph_offset += length;
462 ((HBUINT32 *) subset_offsets)[num_glyphs] = glyph_offset;
464 ((HBUINT16 *) subset_offsets)[num_glyphs] = glyph_offset / 2;
470 const GlyphVarData *get_glyph_var_data (hb_codepoint_t glyph) const
472 unsigned int start_offset = get_offset (glyph);
473 unsigned int end_offset = get_offset (glyph+1);
475 if ((start_offset == end_offset) ||
476 unlikely ((start_offset > get_offset (glyphCount)) ||
477 (start_offset + GlyphVarData::min_size > end_offset)))
478 return &Null (GlyphVarData);
479 return &(((unsigned char *) this + start_offset) + dataZ);
482 bool is_long_offset () const { return (flags & 1) != 0; }
484 unsigned int get_offset (unsigned int i) const
486 if (is_long_offset ())
487 return get_long_offset_array ()[i];
489 return get_short_offset_array ()[i] * 2;
492 unsigned int get_glyph_var_data_length (unsigned int glyph) const
494 unsigned int end_offset = get_offset (glyph + 1);
495 unsigned int start_offset = get_offset (glyph);
496 if (unlikely (start_offset > end_offset || end_offset > get_offset (glyphCount)))
498 return end_offset - start_offset;
501 const HBUINT32 * get_long_offset_array () const { return (const HBUINT32 *) &offsetZ; }
502 const HBUINT16 *get_short_offset_array () const { return (const HBUINT16 *) &offsetZ; }
507 void init (hb_face_t *face)
509 gvar_table = hb_sanitize_context_t ().reference_table<gvar> (face);
510 hb_blob_ptr_t<fvar> fvar_table = hb_sanitize_context_t ().reference_table<fvar> (face);
511 unsigned int axis_count = fvar_table->get_axis_count ();
512 fvar_table.destroy ();
514 if (unlikely ((gvar_table->glyphCount != face->get_num_glyphs ()) ||
515 (gvar_table->axisCount != axis_count)))
518 unsigned int num_shared_coord = gvar_table->sharedTupleCount * gvar_table->axisCount;
519 shared_tuples.resize (num_shared_coord);
520 for (unsigned int i = 0; i < num_shared_coord; i++)
521 shared_tuples[i] = (&(gvar_table + gvar_table->sharedTuples))[i];
526 gvar_table.destroy ();
527 shared_tuples.fini ();
531 struct x_getter { static float get (const contour_point_t &p) { return p.x; } };
532 struct y_getter { static float get (const contour_point_t &p) { return p.y; } };
534 template <typename T>
535 static float infer_delta (const hb_array_t<contour_point_t> points,
536 const hb_array_t<contour_point_t> deltas,
537 unsigned int target, unsigned int prev, unsigned int next)
539 float target_val = T::get (points[target]);
540 float prev_val = T::get (points[prev]);
541 float next_val = T::get (points[next]);
542 float prev_delta = T::get (deltas[prev]);
543 float next_delta = T::get (deltas[next]);
545 if (prev_val == next_val)
546 return (prev_delta == next_delta) ? prev_delta : 0.f;
547 else if (target_val <= hb_min (prev_val, next_val))
548 return (prev_val < next_val) ? prev_delta : next_delta;
549 else if (target_val >= hb_max (prev_val, next_val))
550 return (prev_val > next_val) ? prev_delta : next_delta;
552 /* linear interpolation */
553 float r = (target_val - prev_val) / (next_val - prev_val);
554 return (1.f - r) * prev_delta + r * next_delta;
557 static unsigned int next_index (unsigned int i, unsigned int start, unsigned int end)
558 { return (i >= end) ? start : (i + 1); }
561 bool apply_deltas_to_points (hb_codepoint_t glyph,
562 const int *coords, unsigned int coord_count,
563 const hb_array_t<contour_point_t> points,
564 const hb_array_t<unsigned int> end_points) const
566 if (unlikely (coord_count != gvar_table->axisCount)) return false;
568 const GlyphVarData *var_data = gvar_table->get_glyph_var_data (glyph);
569 if (var_data == &Null (GlyphVarData)) return true;
570 hb_vector_t<unsigned int> shared_indices;
571 GlyphVarData::tuple_iterator_t iterator;
572 if (!GlyphVarData::get_tuple_iterator (var_data,
573 gvar_table->get_glyph_var_data_length (glyph),
574 gvar_table->axisCount,
579 /* Save original points for inferred delta calculation */
580 contour_point_vector_t orig_points;
581 orig_points.resize (points.length);
582 for (unsigned int i = 0; i < orig_points.length; i++)
583 orig_points[i] = points[i];
585 contour_point_vector_t deltas; /* flag is used to indicate referenced point */
586 deltas.resize (points.length);
590 float scalar = iterator.current_tuple->calculate_scalar (coords, coord_count, shared_tuples.as_array ());
591 if (scalar == 0.f) continue;
592 const HBUINT8 *p = iterator.get_serialized_data ();
593 unsigned int length = iterator.current_tuple->get_data_size ();
594 if (unlikely (!iterator.in_range (p, length)))
597 hb_bytes_t bytes ((const char *) p, length);
598 hb_vector_t<unsigned int> private_indices;
599 if (iterator.current_tuple->has_private_points () &&
600 !GlyphVarData::unpack_points (p, private_indices, bytes))
602 const hb_array_t<unsigned int> &indices = private_indices.length ? private_indices : shared_indices;
604 bool apply_to_all = (indices.length == 0);
605 unsigned int num_deltas = apply_to_all ? points.length : indices.length;
606 hb_vector_t<int> x_deltas;
607 x_deltas.resize (num_deltas);
608 if (!GlyphVarData::unpack_deltas (p, x_deltas, bytes))
610 hb_vector_t<int> y_deltas;
611 y_deltas.resize (num_deltas);
612 if (!GlyphVarData::unpack_deltas (p, y_deltas, bytes))
615 for (unsigned int i = 0; i < deltas.length; i++)
617 for (unsigned int i = 0; i < num_deltas; i++)
619 unsigned int pt_index = apply_to_all ? i : indices[i];
620 deltas[pt_index].flag = 1; /* this point is referenced, i.e., explicit deltas specified */
621 deltas[pt_index].x += x_deltas[i] * scalar;
622 deltas[pt_index].y += y_deltas[i] * scalar;
625 /* infer deltas for unreferenced points */
626 unsigned int start_point = 0;
627 for (unsigned int c = 0; c < end_points.length; c++)
629 unsigned int end_point = end_points[c];
632 /* Check the number of unreferenced points in a contour. If no unref points or no ref points, nothing to do. */
633 unsigned int unref_count = 0;
634 for (i = start_point; i <= end_point; i++)
635 if (!deltas[i].flag) unref_count++;
636 if (unref_count == 0 || unref_count > end_point - start_point)
642 /* Locate the next gap of unreferenced points between two referenced points prev and next.
643 * Note that a gap may wrap around at left (start_point) and/or at right (end_point).
645 unsigned int prev, next;
649 j = next_index (i, start_point, end_point);
650 if (deltas[i].flag && !deltas[j].flag) break;
656 j = next_index (i, start_point, end_point);
657 if (!deltas[i].flag && deltas[j].flag) break;
660 /* Infer deltas for all unref points in the gap between prev and next */
664 i = next_index (i, start_point, end_point);
665 if (i == next) break;
666 deltas[i].x = infer_delta<x_getter> (orig_points.as_array (), deltas.as_array (), i, prev, next);
667 deltas[i].y = infer_delta<y_getter> (orig_points.as_array (), deltas.as_array (), i, prev, next);
668 if (--unref_count == 0) goto no_more_gaps;
672 start_point = end_point + 1;
675 /* apply specified / inferred deltas to points */
676 for (unsigned int i = 0; i < points.length; i++)
678 points[i].x += (float) roundf (deltas[i].x);
679 points[i].y += (float) roundf (deltas[i].y);
681 } while (iterator.move_to_next ());
686 unsigned int get_axis_count () const { return gvar_table->axisCount; }
689 const GlyphVarData *get_glyph_var_data (hb_codepoint_t glyph) const
690 { return gvar_table->get_glyph_var_data (glyph); }
693 hb_blob_ptr_t<gvar> gvar_table;
694 hb_vector_t<F2DOT14> shared_tuples;
698 FixedVersion<>version; /* Version of gvar table. Set to 0x00010000u. */
700 HBUINT16 sharedTupleCount;
702 sharedTuples; /* LOffsetTo<UnsizedArrayOf<Tupple>> */
705 LOffsetTo<GlyphVarData>
706 dataZ; /* Array of GlyphVarData */
707 UnsizedArrayOf<HBUINT8>
708 offsetZ; /* Array of 16-bit or 32-bit (glyphCount+1) offsets */
710 DEFINE_SIZE_MIN (20);
713 struct gvar_accelerator_t : gvar::accelerator_t {};
717 #endif /* HB_OT_VAR_GVAR_TABLE_HH */