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"
34 * gvar -- Glyph Variation Table
35 * https://docs.microsoft.com/en-us/typography/opentype/spec/gvar
37 #define HB_OT_TAG_gvar HB_TAG('g','v','a','r')
41 struct contour_point_t
43 void init (float x_ = 0.f, float y_ = 0.f, bool is_end_point_ = false)
44 { flag = 0; x = x_; y = y_; is_end_point = is_end_point_; }
46 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 /* https://docs.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#tuplevariationheader */
82 struct TupleVariationHeader
84 unsigned get_size (unsigned axis_count) const
85 { return min_size + get_all_tuples (axis_count).get_size (); }
87 unsigned get_data_size () const { return varDataSize; }
89 const TupleVariationHeader &get_next (unsigned axis_count) const
90 { return StructAtOffset<TupleVariationHeader> (this, get_size (axis_count)); }
92 float calculate_scalar (const int *coords, unsigned int coord_count,
93 const hb_array_t<const F2DOT14> shared_tuples) const
95 hb_array_t<const F2DOT14> peak_tuple;
98 peak_tuple = get_peak_tuple (coord_count);
101 unsigned int index = get_index ();
102 if (unlikely (index * coord_count >= shared_tuples.length))
104 peak_tuple = shared_tuples.sub_array (coord_count * index, coord_count);
107 hb_array_t<const F2DOT14> start_tuple;
108 hb_array_t<const F2DOT14> end_tuple;
109 if (has_intermediate ())
111 start_tuple = get_start_tuple (coord_count);
112 end_tuple = get_end_tuple (coord_count);
116 for (unsigned int i = 0; i < coord_count; i++)
119 int peak = peak_tuple[i];
120 if (!peak || v == peak) continue;
122 if (has_intermediate ())
124 int start = start_tuple[i];
125 int end = end_tuple[i];
126 if (unlikely (start > peak || peak > end ||
127 (start < 0 && end > 0 && peak))) continue;
128 if (v < start || v > end) return 0.f;
130 { if (peak != start) scalar *= (float) (v - start) / (peak - start); }
132 { if (peak != end) scalar *= (float) (end - v) / (end - peak); }
134 else if (!v || v < hb_min (0, peak) || v > hb_max (0, peak)) return 0.f;
136 scalar *= (float) v / peak;
141 bool has_peak () const { return (tupleIndex & TuppleIndex::EmbeddedPeakTuple); }
142 bool has_intermediate () const { return (tupleIndex & TuppleIndex::IntermediateRegion); }
143 bool has_private_points () const { return (tupleIndex & TuppleIndex::PrivatePointNumbers); }
144 unsigned int get_index () const { return (tupleIndex & TuppleIndex::TupleIndexMask); }
147 struct TuppleIndex : HBUINT16
150 EmbeddedPeakTuple = 0x8000u,
151 IntermediateRegion = 0x4000u,
152 PrivatePointNumbers = 0x2000u,
153 TupleIndexMask = 0x0FFFu
156 DEFINE_SIZE_STATIC (2);
159 hb_array_t<const F2DOT14> get_all_tuples (unsigned axis_count) const
160 { return StructAfter<UnsizedArrayOf<F2DOT14>> (tupleIndex).as_array ((has_peak () + has_intermediate () * 2) * axis_count); }
161 hb_array_t<const F2DOT14> get_peak_tuple (unsigned axis_count) const
162 { return get_all_tuples (axis_count).sub_array (0, axis_count); }
163 hb_array_t<const F2DOT14> get_start_tuple (unsigned axis_count) const
164 { return get_all_tuples (axis_count).sub_array (has_peak () * axis_count, axis_count); }
165 hb_array_t<const F2DOT14> get_end_tuple (unsigned axis_count) const
166 { return get_all_tuples (axis_count).sub_array (has_peak () * axis_count + axis_count, axis_count); }
168 HBUINT16 varDataSize; /* The size in bytes of the serialized
169 * data for this tuple variation table. */
170 TuppleIndex tupleIndex; /* A packed field. The high 4 bits are flags (see below).
171 The low 12 bits are an index into a shared tuple
173 /* UnsizedArrayOf<F2DOT14> peakTuple - optional */
174 /* Peak tuple record for this tuple variation table — optional,
175 * determined by flags in the tupleIndex value.
177 * Note that this must always be included in the 'cvar' table. */
178 /* UnsizedArrayOf<F2DOT14> intermediateStartTuple - optional */
179 /* Intermediate start tuple record for this tuple variation table — optional,
180 determined by flags in the tupleIndex value. */
181 /* UnsizedArrayOf<F2DOT14> intermediateEndTuple - optional */
182 /* Intermediate end tuple record for this tuple variation table — optional,
183 * determined by flags in the tupleIndex value. */
188 struct GlyphVariationData
190 const TupleVariationHeader &get_tuple_var_header (void) const
191 { return StructAfter<TupleVariationHeader> (data); }
193 struct tuple_iterator_t
195 void init (hb_bytes_t var_data_bytes_, unsigned int axis_count_)
197 var_data_bytes = var_data_bytes_;
198 var_data = var_data_bytes_.as<GlyphVariationData> ();
200 axis_count = axis_count_;
201 current_tuple = &var_data->get_tuple_var_header ();
205 bool get_shared_indices (hb_vector_t<unsigned int> &shared_indices /* OUT */)
207 if (var_data->has_shared_point_numbers ())
209 const HBUINT8 *base = &(var_data+var_data->data);
210 const HBUINT8 *p = base;
211 if (!unpack_points (p, shared_indices, var_data_bytes)) return false;
212 data_offset = p - base;
217 bool is_valid () const
219 return (index < var_data->tupleVarCount.get_count ()) &&
220 var_data_bytes.check_range (current_tuple, TupleVariationHeader::min_size) &&
221 var_data_bytes.check_range (current_tuple, hb_max (current_tuple->get_data_size (), current_tuple->get_size (axis_count))) &&
222 current_tuple->get_size (axis_count);
227 data_offset += current_tuple->get_data_size ();
228 current_tuple = ¤t_tuple->get_next (axis_count);
233 const HBUINT8 *get_serialized_data () const
234 { return &(var_data+var_data->data) + data_offset; }
237 const GlyphVariationData *var_data;
239 unsigned int axis_count;
240 unsigned int data_offset;
243 hb_bytes_t var_data_bytes;
244 const TupleVariationHeader *current_tuple;
247 static bool get_tuple_iterator (hb_bytes_t var_data_bytes, unsigned axis_count,
248 hb_vector_t<unsigned int> &shared_indices /* OUT */,
249 tuple_iterator_t *iterator /* OUT */)
251 iterator->init (var_data_bytes, axis_count);
252 if (!iterator->get_shared_indices (shared_indices))
254 return iterator->is_valid ();
257 bool has_shared_point_numbers () const { return tupleVarCount.has_shared_point_numbers (); }
259 static bool unpack_points (const HBUINT8 *&p /* IN/OUT */,
260 hb_vector_t<unsigned int> &points /* OUT */,
261 const hb_bytes_t &bytes)
263 enum packed_point_flag_t
265 POINTS_ARE_WORDS = 0x80,
266 POINT_RUN_COUNT_MASK = 0x7F
269 if (unlikely (!bytes.check_range (p))) return false;
271 uint16_t count = *p++;
272 if (count & POINTS_ARE_WORDS)
274 if (unlikely (!bytes.check_range (p))) return false;
275 count = ((count & POINT_RUN_COUNT_MASK) << 8) | *p++;
277 points.resize (count);
283 if (unlikely (!bytes.check_range (p))) return false;
285 uint8_t control = *p++;
286 uint16_t run_count = (control & POINT_RUN_COUNT_MASK) + 1;
287 if (control & POINTS_ARE_WORDS)
289 for (j = 0; j < run_count && i < count; j++, i++)
291 if (unlikely (!bytes.check_range ((const HBUINT16 *) p)))
293 n += *(const HBUINT16 *)p;
295 p += HBUINT16::static_size;
300 for (j = 0; j < run_count && i < count; j++, i++)
302 if (unlikely (!bytes.check_range (p))) return false;
307 if (j < run_count) return false;
312 static bool unpack_deltas (const HBUINT8 *&p /* IN/OUT */,
313 hb_vector_t<int> &deltas /* IN/OUT */,
314 const hb_bytes_t &bytes)
316 enum packed_delta_flag_t
318 DELTAS_ARE_ZERO = 0x80,
319 DELTAS_ARE_WORDS = 0x40,
320 DELTA_RUN_COUNT_MASK = 0x3F
324 unsigned int count = deltas.length;
327 if (unlikely (!bytes.check_range (p))) return false;
328 uint8_t control = *p++;
329 unsigned int run_count = (control & DELTA_RUN_COUNT_MASK) + 1;
331 if (control & DELTAS_ARE_ZERO)
332 for (j = 0; j < run_count && i < count; j++, i++)
334 else if (control & DELTAS_ARE_WORDS)
335 for (j = 0; j < run_count && i < count; j++, i++)
337 if (unlikely (!bytes.check_range ((const HBUINT16 *) p)))
339 deltas[i] = *(const HBINT16 *) p;
340 p += HBUINT16::static_size;
343 for (j = 0; j < run_count && i < count; j++, i++)
345 if (unlikely (!bytes.check_range (p)))
347 deltas[i] = *(const HBINT8 *) p++;
355 bool has_data () const { return tupleVarCount; }
358 struct TupleVarCount : HBUINT16
360 bool has_shared_point_numbers () const { return ((*this) & SharedPointNumbers); }
361 unsigned int get_count () const { return (*this) & CountMask; }
366 SharedPointNumbers= 0x8000u,
370 DEFINE_SIZE_STATIC (2);
373 TupleVarCount tupleVarCount; /* A packed field. The high 4 bits are flags, and the
374 * low 12 bits are the number of tuple variation tables
375 * for this glyph. The number of tuple variation tables
376 * can be any number between 1 and 4095. */
378 data; /* Offset from the start of the GlyphVariationData table
379 * to the serialized data. */
380 /* TupleVariationHeader tupleVariationHeaders[] *//* Array of tuple variation headers. */
387 static constexpr hb_tag_t tableTag = HB_OT_TAG_gvar;
389 bool sanitize_shallow (hb_sanitize_context_t *c) const
391 TRACE_SANITIZE (this);
392 return_trace (c->check_struct (this) && (version.major == 1) &&
393 (glyphCount == c->get_num_glyphs ()) &&
394 sharedTuples.sanitize (c, this, axisCount * sharedTupleCount) &&
396 c->check_array (get_long_offset_array (), glyphCount+1) :
397 c->check_array (get_short_offset_array (), glyphCount+1)) &&
398 c->check_array (((const HBUINT8*)&(this+dataZ)) + get_offset (0),
399 get_offset (glyphCount) - get_offset (0)));
402 /* GlyphVariationData not sanitized here; must be checked while accessing each glyph varation data */
403 bool sanitize (hb_sanitize_context_t *c) const
404 { return sanitize_shallow (c); }
406 bool subset (hb_subset_context_t *c) const
410 gvar *out = c->serializer->allocate_min<gvar> ();
411 if (unlikely (!out)) return_trace (false);
413 out->version.major = 1;
414 out->version.minor = 0;
415 out->axisCount = axisCount;
416 out->sharedTupleCount = sharedTupleCount;
418 unsigned int num_glyphs = c->plan->num_output_glyphs ();
419 out->glyphCount = num_glyphs;
421 unsigned int subset_data_size = 0;
422 for (hb_codepoint_t gid = 0; gid < num_glyphs; gid++)
424 hb_codepoint_t old_gid;
425 if (!c->plan->old_gid_for_new_gid (gid, &old_gid)) continue;
426 subset_data_size += get_glyph_var_data_bytes (c->source_blob, old_gid).length;
429 bool long_offset = subset_data_size & ~0xFFFFu;
430 out->flags = long_offset ? 1 : 0;
432 HBUINT8 *subset_offsets = c->serializer->allocate_size<HBUINT8> ((long_offset ? 4 : 2) * (num_glyphs + 1));
433 if (!subset_offsets) return_trace (false);
436 if (!sharedTupleCount || !sharedTuples)
437 out->sharedTuples = 0;
440 unsigned int shared_tuple_size = F2DOT14::static_size * axisCount * sharedTupleCount;
441 F2DOT14 *tuples = c->serializer->allocate_size<F2DOT14> (shared_tuple_size);
442 if (!tuples) return_trace (false);
443 out->sharedTuples = (char *) tuples - (char *) out;
444 memcpy (tuples, this+sharedTuples, shared_tuple_size);
447 char *subset_data = c->serializer->allocate_size<char> (subset_data_size);
448 if (!subset_data) return_trace (false);
449 out->dataZ = subset_data - (char *) out;
451 unsigned int glyph_offset = 0;
452 for (hb_codepoint_t gid = 0; gid < num_glyphs; gid++)
454 hb_codepoint_t old_gid;
455 hb_bytes_t var_data_bytes = c->plan->old_gid_for_new_gid (gid, &old_gid)
456 ? get_glyph_var_data_bytes (c->source_blob, old_gid)
460 ((HBUINT32 *) subset_offsets)[gid] = glyph_offset;
462 ((HBUINT16 *) subset_offsets)[gid] = glyph_offset / 2;
464 if (var_data_bytes.length > 0)
465 memcpy (subset_data, var_data_bytes.arrayZ, var_data_bytes.length);
466 subset_data += var_data_bytes.length;
467 glyph_offset += var_data_bytes.length;
470 ((HBUINT32 *) subset_offsets)[num_glyphs] = glyph_offset;
472 ((HBUINT16 *) subset_offsets)[num_glyphs] = glyph_offset / 2;
478 const hb_bytes_t get_glyph_var_data_bytes (hb_blob_t *blob, hb_codepoint_t glyph) const
480 unsigned start_offset = get_offset (glyph);
481 unsigned length = get_offset (glyph+1) - start_offset;
482 hb_bytes_t var_data = blob->as_bytes ().sub_array (((unsigned) dataZ) + start_offset, length);
483 return likely (var_data.length >= GlyphVariationData::min_size) ? var_data : hb_bytes_t ();
486 bool is_long_offset () const { return (flags & 1) != 0; }
488 unsigned int get_offset (unsigned int i) const
490 if (is_long_offset ())
491 return get_long_offset_array ()[i];
493 return get_short_offset_array ()[i] * 2;
496 const HBUINT32 * get_long_offset_array () const { return (const HBUINT32 *) &offsetZ; }
497 const HBUINT16 *get_short_offset_array () const { return (const HBUINT16 *) &offsetZ; }
502 void init (hb_face_t *face)
503 { table = hb_sanitize_context_t ().reference_table<gvar> (face); }
504 void fini () { table.destroy (); }
507 struct x_getter { static float get (const contour_point_t &p) { return p.x; } };
508 struct y_getter { static float get (const contour_point_t &p) { return p.y; } };
510 template <typename T>
511 static float infer_delta (const hb_array_t<contour_point_t> points,
512 const hb_array_t<contour_point_t> deltas,
513 unsigned int target, unsigned int prev, unsigned int next)
515 float target_val = T::get (points[target]);
516 float prev_val = T::get (points[prev]);
517 float next_val = T::get (points[next]);
518 float prev_delta = T::get (deltas[prev]);
519 float next_delta = T::get (deltas[next]);
521 if (prev_val == next_val)
522 return (prev_delta == next_delta) ? prev_delta : 0.f;
523 else if (target_val <= hb_min (prev_val, next_val))
524 return (prev_val < next_val) ? prev_delta : next_delta;
525 else if (target_val >= hb_max (prev_val, next_val))
526 return (prev_val > next_val) ? prev_delta : next_delta;
528 /* linear interpolation */
529 float r = (target_val - prev_val) / (next_val - prev_val);
530 return (1.f - r) * prev_delta + r * next_delta;
533 static unsigned int next_index (unsigned int i, unsigned int start, unsigned int end)
534 { return (i >= end) ? start : (i + 1); }
537 bool apply_deltas_to_points (hb_codepoint_t glyph, hb_font_t *font,
538 const hb_array_t<contour_point_t> points) const
540 /* num_coords should exactly match gvar's axisCount due to how GlyphVariationData tuples are aligned */
541 if (!font->num_coords || font->num_coords != table->axisCount) return true;
543 hb_bytes_t var_data_bytes = table->get_glyph_var_data_bytes (table.get_blob (), glyph);
544 if (!var_data_bytes.as<GlyphVariationData> ()->has_data ()) return true;
545 hb_vector_t<unsigned int> shared_indices;
546 GlyphVariationData::tuple_iterator_t iterator;
547 if (!GlyphVariationData::get_tuple_iterator (var_data_bytes, table->axisCount,
548 shared_indices, &iterator))
549 return true; /* so isn't applied at all */
551 /* Save original points for inferred delta calculation */
552 contour_point_vector_t orig_points;
553 orig_points.resize (points.length);
554 for (unsigned int i = 0; i < orig_points.length; i++)
555 orig_points[i] = points[i];
557 contour_point_vector_t deltas; /* flag is used to indicate referenced point */
558 deltas.resize (points.length);
560 hb_vector_t<unsigned> end_points;
561 for (unsigned i = 0; i < points.length; ++i)
562 if (points[i].is_end_point)
565 int *coords = font->coords;
566 unsigned num_coords = font->num_coords;
567 hb_array_t<const F2DOT14> shared_tuples = (table+table->sharedTuples).as_array (table->sharedTupleCount * table->axisCount);
570 float scalar = iterator.current_tuple->calculate_scalar (coords, num_coords, shared_tuples);
571 if (scalar == 0.f) continue;
572 const HBUINT8 *p = iterator.get_serialized_data ();
573 unsigned int length = iterator.current_tuple->get_data_size ();
574 if (unlikely (!iterator.var_data_bytes.check_range (p, length)))
577 hb_bytes_t bytes ((const char *) p, length);
578 hb_vector_t<unsigned int> private_indices;
579 if (iterator.current_tuple->has_private_points () &&
580 !GlyphVariationData::unpack_points (p, private_indices, bytes))
582 const hb_array_t<unsigned int> &indices = private_indices.length ? private_indices : shared_indices;
584 bool apply_to_all = (indices.length == 0);
585 unsigned int num_deltas = apply_to_all ? points.length : indices.length;
586 hb_vector_t<int> x_deltas;
587 x_deltas.resize (num_deltas);
588 if (!GlyphVariationData::unpack_deltas (p, x_deltas, bytes))
590 hb_vector_t<int> y_deltas;
591 y_deltas.resize (num_deltas);
592 if (!GlyphVariationData::unpack_deltas (p, y_deltas, bytes))
595 for (unsigned int i = 0; i < deltas.length; i++)
597 for (unsigned int i = 0; i < num_deltas; i++)
599 unsigned int pt_index = apply_to_all ? i : indices[i];
600 deltas[pt_index].flag = 1; /* this point is referenced, i.e., explicit deltas specified */
601 deltas[pt_index].x += x_deltas[i] * scalar;
602 deltas[pt_index].y += y_deltas[i] * scalar;
605 /* infer deltas for unreferenced points */
606 unsigned start_point = 0;
607 for (unsigned c = 0; c < end_points.length; c++)
609 unsigned end_point = end_points[c];
611 /* Check the number of unreferenced points in a contour. If no unref points or no ref points, nothing to do. */
612 unsigned unref_count = 0;
613 for (unsigned i = start_point; i <= end_point; i++)
614 if (!deltas[i].flag) unref_count++;
616 unsigned j = start_point;
617 if (unref_count == 0 || unref_count > end_point - start_point)
622 /* Locate the next gap of unreferenced points between two referenced points prev and next.
623 * Note that a gap may wrap around at left (start_point) and/or at right (end_point).
625 unsigned int prev, next, i;
629 j = next_index (i, start_point, end_point);
630 if (deltas[i].flag && !deltas[j].flag) break;
636 j = next_index (i, start_point, end_point);
637 if (!deltas[i].flag && deltas[j].flag) break;
640 /* Infer deltas for all unref points in the gap between prev and next */
644 i = next_index (i, start_point, end_point);
645 if (i == next) break;
646 deltas[i].x = infer_delta<x_getter> (orig_points.as_array (), deltas.as_array (), i, prev, next);
647 deltas[i].y = infer_delta<y_getter> (orig_points.as_array (), deltas.as_array (), i, prev, next);
648 if (--unref_count == 0) goto no_more_gaps;
652 start_point = end_point + 1;
655 /* apply specified / inferred deltas to points */
656 for (unsigned int i = 0; i < points.length; i++)
658 points[i].x += (float) roundf (deltas[i].x);
659 points[i].y += (float) roundf (deltas[i].y);
661 } while (iterator.move_to_next ());
666 unsigned int get_axis_count () const { return table->axisCount; }
669 hb_blob_ptr_t<gvar> table;
673 FixedVersion<>version; /* Version number of the glyph variations table
674 * Set to 0x00010000u. */
675 HBUINT16 axisCount; /* The number of variation axes for this font. This must be
676 * the same number as axisCount in the 'fvar' table. */
677 HBUINT16 sharedTupleCount;
678 /* The number of shared tuple records. Shared tuple records
679 * can be referenced within glyph variation data tables for
680 * multiple glyphs, as opposed to other tuple records stored
681 * directly within a glyph variation data table. */
682 LNNOffsetTo<UnsizedArrayOf<F2DOT14>>
683 sharedTuples; /* Offset from the start of this table to the shared tuple records.
684 * Array of tuple records shared across all glyph variation data tables. */
685 HBUINT16 glyphCount; /* The number of glyphs in this font. This must match the number of
686 * glyphs stored elsewhere in the font. */
687 HBUINT16 flags; /* Bit-field that gives the format of the offset array that follows.
688 * If bit 0 is clear, the offsets are uint16; if bit 0 is set, the
689 * offsets are uint32. */
690 LOffsetTo<GlyphVariationData>
691 dataZ; /* Offset from the start of this table to the array of
692 * GlyphVariationData tables. */
693 UnsizedArrayOf<HBUINT8>
694 offsetZ; /* Offsets from the start of the GlyphVariationData array
695 * to each GlyphVariationData table. */
697 DEFINE_SIZE_MIN (20);
700 struct gvar_accelerator_t : gvar::accelerator_t {};
704 #endif /* HB_OT_VAR_GVAR_TABLE_HH */