xref: /aosp_15_r20/external/harfbuzz_ng/src/OT/Layout/GPOS/PairPosFormat2.hh (revision 2d1272b857b1f7575e6e246373e1cb218663db8a)
1 #ifndef OT_LAYOUT_GPOS_PAIRPOSFORMAT2_HH
2 #define OT_LAYOUT_GPOS_PAIRPOSFORMAT2_HH
3 
4 #include "ValueFormat.hh"
5 
6 namespace OT {
7 namespace Layout {
8 namespace GPOS_impl {
9 
10 template <typename Types>
11 struct PairPosFormat2_4 : ValueBase
12 {
13   protected:
14   HBUINT16      format;                 /* Format identifier--format = 2 */
15   typename Types::template OffsetTo<Coverage>
16                 coverage;               /* Offset to Coverage table--from
17                                          * beginning of subtable */
18   ValueFormat   valueFormat1;           /* ValueRecord definition--for the
19                                          * first glyph of the pair--may be zero
20                                          * (0) */
21   ValueFormat   valueFormat2;           /* ValueRecord definition--for the
22                                          * second glyph of the pair--may be
23                                          * zero (0) */
24   typename Types::template OffsetTo<ClassDef>
25                 classDef1;              /* Offset to ClassDef table--from
26                                          * beginning of PairPos subtable--for
27                                          * the first glyph of the pair */
28   typename Types::template OffsetTo<ClassDef>
29                 classDef2;              /* Offset to ClassDef table--from
30                                          * beginning of PairPos subtable--for
31                                          * the second glyph of the pair */
32   HBUINT16      class1Count;            /* Number of classes in ClassDef1
33                                          * table--includes Class0 */
34   HBUINT16      class2Count;            /* Number of classes in ClassDef2
35                                          * table--includes Class0 */
36   ValueRecord   values;                 /* Matrix of value pairs:
37                                          * class1-major, class2-minor,
38                                          * Each entry has value1 and value2 */
39   public:
40   DEFINE_SIZE_ARRAY (10 + 3 * Types::size, values);
41 
sanitizeOT::Layout::GPOS_impl::PairPosFormat2_442   bool sanitize (hb_sanitize_context_t *c) const
43   {
44     TRACE_SANITIZE (this);
45     if (!(c->check_struct (this)
46        && coverage.sanitize (c, this)
47        && classDef1.sanitize (c, this)
48        && classDef2.sanitize (c, this))) return_trace (false);
49 
50     unsigned int len1 = valueFormat1.get_len ();
51     unsigned int len2 = valueFormat2.get_len ();
52     unsigned int stride = HBUINT16::static_size * (len1 + len2);
53     unsigned int count = (unsigned int) class1Count * (unsigned int) class2Count;
54     return_trace (c->check_range ((const void *) values,
55                                   count,
56                                   stride) &&
57 		  (c->lazy_some_gpos ||
58 		   (valueFormat1.sanitize_values_stride_unsafe (c, this, &values[0], count, stride) &&
59 		    valueFormat2.sanitize_values_stride_unsafe (c, this, &values[len1], count, stride))));
60   }
61 
intersectsOT::Layout::GPOS_impl::PairPosFormat2_462   bool intersects (const hb_set_t *glyphs) const
63   {
64     return (this+coverage).intersects (glyphs) &&
65            (this+classDef2).intersects (glyphs);
66   }
67 
closure_lookupsOT::Layout::GPOS_impl::PairPosFormat2_468   void closure_lookups (hb_closure_lookups_context_t *c) const {}
collect_variation_indicesOT::Layout::GPOS_impl::PairPosFormat2_469   void collect_variation_indices (hb_collect_variation_indices_context_t *c) const
70   {
71     if (!intersects (c->glyph_set)) return;
72     if ((!valueFormat1.has_device ()) && (!valueFormat2.has_device ())) return;
73 
74     hb_set_t klass1_glyphs, klass2_glyphs;
75     if (!(this+classDef1).collect_coverage (&klass1_glyphs)) return;
76     if (!(this+classDef2).collect_coverage (&klass2_glyphs)) return;
77 
78     hb_set_t class1_set, class2_set;
79     for (const unsigned cp : + c->glyph_set->iter () | hb_filter (this + coverage))
80     {
81       if (!klass1_glyphs.has (cp)) class1_set.add (0);
82       else
83       {
84         unsigned klass1 = (this+classDef1).get (cp);
85         class1_set.add (klass1);
86       }
87     }
88 
89     class2_set.add (0);
90     for (const unsigned cp : + c->glyph_set->iter () | hb_filter (klass2_glyphs))
91     {
92       unsigned klass2 = (this+classDef2).get (cp);
93       class2_set.add (klass2);
94     }
95 
96     if (class1_set.is_empty ()
97         || class2_set.is_empty ()
98         || (class2_set.get_population() == 1 && class2_set.has(0)))
99       return;
100 
101     unsigned len1 = valueFormat1.get_len ();
102     unsigned len2 = valueFormat2.get_len ();
103     const hb_array_t<const Value> values_array = values.as_array ((unsigned)class1Count * (unsigned) class2Count * (len1 + len2));
104     for (const unsigned class1_idx : class1_set.iter ())
105     {
106       for (const unsigned class2_idx : class2_set.iter ())
107       {
108         unsigned start_offset = (class1_idx * (unsigned) class2Count + class2_idx) * (len1 + len2);
109         if (valueFormat1.has_device ())
110           valueFormat1.collect_variation_indices (c, this, values_array.sub_array (start_offset, len1));
111 
112         if (valueFormat2.has_device ())
113           valueFormat2.collect_variation_indices (c, this, values_array.sub_array (start_offset+len1, len2));
114       }
115     }
116   }
117 
collect_glyphsOT::Layout::GPOS_impl::PairPosFormat2_4118   void collect_glyphs (hb_collect_glyphs_context_t *c) const
119   {
120     if (unlikely (!(this+coverage).collect_coverage (c->input))) return;
121     if (unlikely (!(this+classDef2).collect_coverage (c->input))) return;
122   }
123 
get_coverageOT::Layout::GPOS_impl::PairPosFormat2_4124   const Coverage &get_coverage () const { return this+coverage; }
125 
applyOT::Layout::GPOS_impl::PairPosFormat2_4126   bool apply (hb_ot_apply_context_t *c) const
127   {
128     TRACE_APPLY (this);
129     hb_buffer_t *buffer = c->buffer;
130     unsigned int index = (this+coverage).get_coverage  (buffer->cur().codepoint);
131     if (likely (index == NOT_COVERED)) return_trace (false);
132 
133     hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
134     skippy_iter.reset_fast (buffer->idx);
135     unsigned unsafe_to;
136     if (unlikely (!skippy_iter.next (&unsafe_to)))
137     {
138       buffer->unsafe_to_concat (buffer->idx, unsafe_to);
139       return_trace (false);
140     }
141 
142     unsigned int klass1 = (this+classDef1).get_class (buffer->cur().codepoint);
143     unsigned int klass2 = (this+classDef2).get_class (buffer->info[skippy_iter.idx].codepoint);
144     if (unlikely (klass1 >= class1Count || klass2 >= class2Count))
145     {
146       buffer->unsafe_to_concat (buffer->idx, skippy_iter.idx + 1);
147       return_trace (false);
148     }
149 
150     unsigned int len1 = valueFormat1.get_len ();
151     unsigned int len2 = valueFormat2.get_len ();
152     unsigned int record_len = len1 + len2;
153 
154     const Value *v = &values[record_len * (klass1 * class2Count + klass2)];
155 
156     bool applied_first = false, applied_second = false;
157 
158 
159     /* Isolate simple kerning and apply it half to each side.
160      * Results in better cursor positioning / underline drawing.
161      *
162      * Disabled, because causes issues... :-(
163      * https://github.com/harfbuzz/harfbuzz/issues/3408
164      * https://github.com/harfbuzz/harfbuzz/pull/3235#issuecomment-1029814978
165      */
166 #ifndef HB_SPLIT_KERN
167     if (false)
168 #endif
169     {
170       if (!len2)
171       {
172         const hb_direction_t dir = buffer->props.direction;
173         const bool horizontal = HB_DIRECTION_IS_HORIZONTAL (dir);
174         const bool backward = HB_DIRECTION_IS_BACKWARD (dir);
175         unsigned mask = horizontal ? ValueFormat::xAdvance : ValueFormat::yAdvance;
176         if (backward)
177           mask |= mask >> 2; /* Add eg. xPlacement in RTL. */
178         /* Add Devices. */
179         mask |= mask << 4;
180 
181         if (valueFormat1 & ~mask)
182           goto bail;
183 
184         /* Is simple kern. Apply value on an empty position slot,
185          * then split it between sides. */
186 
187         hb_glyph_position_t pos{};
188         if (valueFormat1.apply_value (c, this, v, pos))
189         {
190           hb_position_t *src  = &pos.x_advance;
191           hb_position_t *dst1 = &buffer->cur_pos().x_advance;
192           hb_position_t *dst2 = &buffer->pos[skippy_iter.idx].x_advance;
193           unsigned i = horizontal ? 0 : 1;
194 
195           hb_position_t kern  = src[i];
196           hb_position_t kern1 = kern >> 1;
197           hb_position_t kern2 = kern - kern1;
198 
199           if (!backward)
200           {
201             dst1[i] += kern1;
202             dst2[i] += kern2;
203             dst2[i + 2] += kern2;
204           }
205           else
206           {
207             dst1[i] += kern1;
208             dst1[i + 2] += src[i + 2] - kern2;
209             dst2[i] += kern2;
210           }
211 
212           applied_first = applied_second = kern != 0;
213           goto success;
214         }
215         goto boring;
216       }
217     }
218     bail:
219 
220     if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
221     {
222       c->buffer->message (c->font,
223 			  "try kerning glyphs at %u,%u",
224 			  c->buffer->idx, skippy_iter.idx);
225     }
226 
227     applied_first = len1 && valueFormat1.apply_value (c, this, v, buffer->cur_pos());
228     applied_second = len2 && valueFormat2.apply_value (c, this, v + len1, buffer->pos[skippy_iter.idx]);
229 
230     if (applied_first || applied_second)
231       if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
232       {
233 	c->buffer->message (c->font,
234 			    "kerned glyphs at %u,%u",
235 			    c->buffer->idx, skippy_iter.idx);
236       }
237 
238     if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
239     {
240       c->buffer->message (c->font,
241 			  "tried kerning glyphs at %u,%u",
242 			  c->buffer->idx, skippy_iter.idx);
243     }
244 
245     success:
246     if (applied_first || applied_second)
247       buffer->unsafe_to_break (buffer->idx, skippy_iter.idx + 1);
248     else
249     boring:
250       buffer->unsafe_to_concat (buffer->idx, skippy_iter.idx + 1);
251 
252     if (len2)
253     {
254       skippy_iter.idx++;
255       // https://github.com/harfbuzz/harfbuzz/issues/3824
256       // https://github.com/harfbuzz/harfbuzz/issues/3888#issuecomment-1326781116
257       buffer->unsafe_to_break (buffer->idx, skippy_iter.idx + 1);
258     }
259 
260     buffer->idx = skippy_iter.idx;
261 
262     return_trace (true);
263   }
264 
subsetOT::Layout::GPOS_impl::PairPosFormat2_4265   bool subset (hb_subset_context_t *c) const
266   {
267     TRACE_SUBSET (this);
268     auto *out = c->serializer->start_embed (*this);
269     if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
270     out->format = format;
271 
272     hb_map_t klass1_map;
273     out->classDef1.serialize_subset (c, classDef1, this, &klass1_map, true, true, &(this + coverage));
274     out->class1Count = klass1_map.get_population ();
275 
276     hb_map_t klass2_map;
277     out->classDef2.serialize_subset (c, classDef2, this, &klass2_map, true, false);
278     out->class2Count = klass2_map.get_population ();
279 
280     unsigned len1 = valueFormat1.get_len ();
281     unsigned len2 = valueFormat2.get_len ();
282 
283     hb_pair_t<unsigned, unsigned> newFormats = hb_pair (valueFormat1, valueFormat2);
284 
285     if (c->plan->normalized_coords)
286     {
287       /* in case of full instancing, all var device flags will be dropped so no
288        * need to strip hints here */
289       newFormats = compute_effective_value_formats (klass1_map, klass2_map, false, false, &c->plan->layout_variation_idx_delta_map);
290     }
291     /* do not strip hints for VF */
292     else if (c->plan->flags & HB_SUBSET_FLAGS_NO_HINTING)
293     {
294       hb_blob_t* blob = hb_face_reference_table (c->plan->source, HB_TAG ('f','v','a','r'));
295       bool has_fvar = (blob != hb_blob_get_empty ());
296       hb_blob_destroy (blob);
297 
298       bool strip = !has_fvar;
299       /* special case: strip hints when a VF has no GDEF varstore after
300        * subsetting*/
301       if (has_fvar && !c->plan->has_gdef_varstore)
302         strip = true;
303       newFormats = compute_effective_value_formats (klass1_map, klass2_map, strip, true);
304     }
305 
306     out->valueFormat1 = newFormats.first;
307     out->valueFormat2 = newFormats.second;
308 
309     unsigned total_len = len1 + len2;
310     hb_vector_t<unsigned> class2_idxs (+ hb_range ((unsigned) class2Count) | hb_filter (klass2_map));
311     for (unsigned class1_idx : + hb_range ((unsigned) class1Count) | hb_filter (klass1_map))
312     {
313       for (unsigned class2_idx : class2_idxs)
314       {
315         unsigned idx = (class1_idx * (unsigned) class2Count + class2_idx) * total_len;
316         valueFormat1.copy_values (c->serializer, out->valueFormat1, this, &values[idx], &c->plan->layout_variation_idx_delta_map);
317         valueFormat2.copy_values (c->serializer, out->valueFormat2, this, &values[idx + len1], &c->plan->layout_variation_idx_delta_map);
318       }
319     }
320 
321     bool ret = out->coverage.serialize_subset(c, coverage, this);
322     return_trace (out->class1Count && out->class2Count && ret);
323   }
324 
325 
compute_effective_value_formatsOT::Layout::GPOS_impl::PairPosFormat2_4326   hb_pair_t<unsigned, unsigned> compute_effective_value_formats (const hb_map_t& klass1_map,
327                                                                  const hb_map_t& klass2_map,
328                                                                  bool strip_hints, bool strip_empty,
329                                                                  const hb_hashmap_t<unsigned, hb_pair_t<unsigned, int>> *varidx_delta_map = nullptr) const
330   {
331     unsigned len1 = valueFormat1.get_len ();
332     unsigned len2 = valueFormat2.get_len ();
333     unsigned record_size = len1 + len2;
334 
335     unsigned format1 = 0;
336     unsigned format2 = 0;
337 
338     for (unsigned class1_idx : + hb_range ((unsigned) class1Count) | hb_filter (klass1_map))
339     {
340       for (unsigned class2_idx : + hb_range ((unsigned) class2Count) | hb_filter (klass2_map))
341       {
342         unsigned idx = (class1_idx * (unsigned) class2Count + class2_idx) * record_size;
343         format1 = format1 | valueFormat1.get_effective_format (&values[idx], strip_hints, strip_empty, this, varidx_delta_map);
344         format2 = format2 | valueFormat2.get_effective_format (&values[idx + len1], strip_hints, strip_empty, this, varidx_delta_map);
345       }
346 
347       if (format1 == valueFormat1 && format2 == valueFormat2)
348         break;
349     }
350 
351     return hb_pair (format1, format2);
352   }
353 };
354 
355 }
356 }
357 }
358 
359 #endif  // OT_LAYOUT_GPOS_PAIRPOSFORMAT2_HH
360