blob: a8fb5c7acb9242dc123befe42ff31610d5ce69ff [file] [log] [blame]
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001/*
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -04002 * Copyright © 2007,2008,2009,2010 Red Hat, Inc.
Behdad Esfahbod2e0c44f2013-04-24 16:42:05 -04003 * Copyright © 2010,2012,2013 Google, Inc.
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04004 *
Behdad Esfahbodc755cb32010-04-22 00:11:43 -04005 * This is part of HarfBuzz, a text shaping library.
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04006 *
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 * Red Hat Author(s): Behdad Esfahbod
Behdad Esfahbod98370e82010-10-27 17:39:01 -040026 * Google Author(s): Behdad Esfahbod
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040027 */
28
Behdad Esfahbod7a750ac2011-08-17 14:19:59 +020029#ifndef HB_OT_LAYOUT_GPOS_TABLE_HH
30#define HB_OT_LAYOUT_GPOS_TABLE_HH
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040031
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070032#include "hb-ot-layout-gsubgpos.hh"
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040033
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040034
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -040035namespace OT {
36
Qunxin Liu82afc752020-02-04 13:24:37 -080037struct MarkArray;
38static void Markclass_closure_and_remap_indexes (const Coverage &mark_coverage,
39 const MarkArray &mark_array,
40 const hb_set_t &glyphset,
41 hb_map_t* klass_mapping /* INOUT */);
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -040042
Behdad Esfahbodb65c0602011-07-28 16:48:43 -040043/* buffer **position** var allocations */
Behdad Esfahbod686567b2016-02-11 15:25:28 +070044#define attach_chain() var.i16[0] /* glyph to which this attaches to, relative to current glyphs; negative for going back, positive for forward. */
45#define attach_type() var.u8[2] /* attachment type */
46/* Note! if attach_chain() is zero, the value of attach_type() is irrelevant. */
47
48enum attach_type_t {
49 ATTACH_TYPE_NONE = 0X00,
50
51 /* Each attachment should be either a mark or a cursive; can't be both. */
52 ATTACH_TYPE_MARK = 0X01,
53 ATTACH_TYPE_CURSIVE = 0X02,
54};
Behdad Esfahbod194d4562010-10-27 23:09:10 -040055
56
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040057/* Shared Tables: ValueRecord, Anchor Table, and MarkArray */
58
Behdad Esfahbod6b191782018-01-10 03:07:30 +010059typedef HBUINT16 Value;
Behdad Esfahbodc91facd2009-08-26 18:53:43 -040060
Behdad Esfahbod1d66cdc2018-11-10 19:54:08 -050061typedef UnsizedArrayOf<Value> ValueRecord;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040062
Behdad Esfahbod6b191782018-01-10 03:07:30 +010063struct ValueFormat : HBUINT16
Behdad Esfahbodfca6a0d2009-05-21 04:49:04 -040064{
Behdad Esfahbodc6035cf2012-04-12 13:23:59 -040065 enum Flags {
Behdad Esfahbod76271002014-07-11 14:54:42 -040066 xPlacement = 0x0001u, /* Includes horizontal adjustment for placement */
67 yPlacement = 0x0002u, /* Includes vertical adjustment for placement */
68 xAdvance = 0x0004u, /* Includes horizontal adjustment for advance */
69 yAdvance = 0x0008u, /* Includes vertical adjustment for advance */
70 xPlaDevice = 0x0010u, /* Includes horizontal Device table for placement */
71 yPlaDevice = 0x0020u, /* Includes vertical Device table for placement */
72 xAdvDevice = 0x0040u, /* Includes horizontal Device table for advance */
73 yAdvDevice = 0x0080u, /* Includes vertical Device table for advance */
74 ignored = 0x0F00u, /* Was used in TrueType Open for MM fonts */
75 reserved = 0xF000u, /* For future use */
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -040076
Behdad Esfahbod76271002014-07-11 14:54:42 -040077 devices = 0x00F0u /* Mask for having any Device table */
Behdad Esfahbodfca6a0d2009-05-21 04:49:04 -040078 };
79
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -040080/* All fields are options. Only those available advance the value pointer. */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040081#if 0
Ebrahim Byagowid0e2add2020-07-18 22:14:52 +043082 HBINT16 xPlacement; /* Horizontal adjustment for
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040083 * placement--in design units */
Ebrahim Byagowid0e2add2020-07-18 22:14:52 +043084 HBINT16 yPlacement; /* Vertical adjustment for
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040085 * placement--in design units */
Ebrahim Byagowid0e2add2020-07-18 22:14:52 +043086 HBINT16 xAdvance; /* Horizontal adjustment for
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040087 * advance--in design units (only used
88 * for horizontal writing) */
Ebrahim Byagowid0e2add2020-07-18 22:14:52 +043089 HBINT16 yAdvance; /* Vertical adjustment for advance--in
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040090 * design units (only used for vertical
91 * writing) */
Behdad Esfahbodad28f972021-03-31 12:49:14 -060092 Offset16To<Device> xPlaDevice; /* Offset to Device table for
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040093 * horizontal placement--measured from
94 * beginning of PosTable (may be NULL) */
Behdad Esfahbodad28f972021-03-31 12:49:14 -060095 Offset16To<Device> yPlaDevice; /* Offset to Device table for vertical
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040096 * placement--measured from beginning
97 * of PosTable (may be NULL) */
Behdad Esfahbodad28f972021-03-31 12:49:14 -060098 Offset16To<Device> xAdvDevice; /* Offset to Device table for
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040099 * horizontal advance--measured from
100 * beginning of PosTable (may be NULL) */
Behdad Esfahbodad28f972021-03-31 12:49:14 -0600101 Offset16To<Device> yAdvDevice; /* Offset to Device table for vertical
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400102 * advance--measured from beginning of
103 * PosTable (may be NULL) */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400104#endif
105
Garret Rieger5df7c662021-04-22 16:45:14 -0700106 IntType& operator = (uint16_t i) { v = i; return *this; }
107
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330108 unsigned int get_len () const { return hb_popcount ((unsigned int) *this); }
109 unsigned int get_size () const { return get_len () * Value::static_size; }
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400110
Ebrahim Byagowi05584132019-10-01 13:49:55 +0330111 bool apply_value (hb_ot_apply_context_t *c,
112 const void *base,
113 const Value *values,
114 hb_glyph_position_t &glyph_pos) const
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400115 {
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500116 bool ret = false;
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400117 unsigned int format = *this;
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500118 if (!format) return ret;
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400119
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700120 hb_font_t *font = c->font;
Behdad Esfahbod385f78b2018-11-07 17:19:21 -0500121 bool horizontal = HB_DIRECTION_IS_HORIZONTAL (c->direction);
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700122
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500123 if (format & xPlacement) glyph_pos.x_offset += font->em_scale_x (get_short (values++, &ret));
124 if (format & yPlacement) glyph_pos.y_offset += font->em_scale_y (get_short (values++, &ret));
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400125 if (format & xAdvance) {
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500126 if (likely (horizontal)) glyph_pos.x_advance += font->em_scale_x (get_short (values, &ret));
Konstantin Ritt08322592014-01-22 20:31:30 +0200127 values++;
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400128 }
Behdad Esfahbodcc2086d2011-05-19 19:19:50 -0400129 /* y_advance values grow downward but font-space grows upward, hence negation */
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400130 if (format & yAdvance) {
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500131 if (unlikely (!horizontal)) glyph_pos.y_advance -= font->em_scale_y (get_short (values, &ret));
Konstantin Ritt08322592014-01-22 20:31:30 +0200132 values++;
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400133 }
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400134
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500135 if (!has_device ()) return ret;
Behdad Esfahboda8d960b2010-04-29 14:31:56 -0400136
Behdad Esfahbodca286702016-03-01 19:29:36 +0900137 bool use_x_device = font->x_ppem || font->num_coords;
138 bool use_y_device = font->y_ppem || font->num_coords;
Behdad Esfahboda8d960b2010-04-29 14:31:56 -0400139
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500140 if (!use_x_device && !use_y_device) return ret;
Behdad Esfahboda8d960b2010-04-29 14:31:56 -0400141
Behdad Esfahbodcf3de4d2016-09-10 00:22:24 -0700142 const VariationStore &store = c->var_store;
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700143
Behdad Esfahbod0090dc02009-07-30 16:28:45 -0400144 /* pixel -> fractional pixel */
145 if (format & xPlaDevice) {
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500146 if (use_x_device) glyph_pos.x_offset += (base + get_device (values, &ret)).get_x_delta (font, store);
Konstantin Ritt08322592014-01-22 20:31:30 +0200147 values++;
Behdad Esfahbod0090dc02009-07-30 16:28:45 -0400148 }
149 if (format & yPlaDevice) {
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500150 if (use_y_device) glyph_pos.y_offset += (base + get_device (values, &ret)).get_y_delta (font, store);
Konstantin Ritt08322592014-01-22 20:31:30 +0200151 values++;
Behdad Esfahbod0090dc02009-07-30 16:28:45 -0400152 }
153 if (format & xAdvDevice) {
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500154 if (horizontal && use_x_device) glyph_pos.x_advance += (base + get_device (values, &ret)).get_x_delta (font, store);
Konstantin Ritt08322592014-01-22 20:31:30 +0200155 values++;
Behdad Esfahbod0090dc02009-07-30 16:28:45 -0400156 }
157 if (format & yAdvDevice) {
Behdad Esfahbodcc2086d2011-05-19 19:19:50 -0400158 /* y_advance values grow downward but font-space grows upward, hence negation */
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500159 if (!horizontal && use_y_device) glyph_pos.y_advance -= (base + get_device (values, &ret)).get_y_delta (font, store);
Konstantin Ritt08322592014-01-22 20:31:30 +0200160 values++;
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400161 }
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500162 return ret;
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400163 }
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400164
Garret Rieger2f502832021-04-23 15:21:30 -0700165 unsigned int get_effective_format (const Value *values) const
166 {
167 unsigned int format = *this;
Garret Riegere25efb32021-04-23 19:09:37 -0700168 for (unsigned flag = xPlacement; flag <= yAdvDevice; flag = flag << 1) {
169 if (format & flag) should_drop (*values++, (Flags) flag, &format);
170 }
171
Garret Rieger2f502832021-04-23 15:21:30 -0700172 return format;
173 }
174
175 template<typename Iterator,
176 hb_requires (hb_is_iterator (Iterator))>
Garret Riegerbadb8e42021-04-23 16:32:56 -0700177 unsigned int get_effective_format (Iterator it) const {
Garret Rieger2f502832021-04-23 15:21:30 -0700178 unsigned int new_format = 0;
179
180 for (const hb_array_t<const Value>& values : it)
181 new_format = new_format | get_effective_format (&values);
182
183 return new_format;
184 }
185
186 void copy_values (hb_serialize_context_t *c,
187 unsigned int new_format,
188 const void *base,
189 const Value *values,
190 const hb_map_t *layout_variation_idx_map) const
Michiharu Arizac828d7b2020-01-15 15:32:44 -0800191 {
192 unsigned int format = *this;
193 if (!format) return;
194
Garret Rieger2f502832021-04-23 15:21:30 -0700195 if (format & xPlacement) copy_value (c, new_format, xPlacement, *values++);
Garret Riegerbadb8e42021-04-23 16:32:56 -0700196 if (format & yPlacement) copy_value (c, new_format, yPlacement, *values++);
197 if (format & xAdvance) copy_value (c, new_format, xAdvance, *values++);
198 if (format & yAdvance) copy_value (c, new_format, yAdvance, *values++);
Michiharu Arizac828d7b2020-01-15 15:32:44 -0800199
Qunxin Liud7c012a2020-02-26 13:11:42 -0800200 if (format & xPlaDevice) copy_device (c, base, values++, layout_variation_idx_map);
201 if (format & yPlaDevice) copy_device (c, base, values++, layout_variation_idx_map);
202 if (format & xAdvDevice) copy_device (c, base, values++, layout_variation_idx_map);
203 if (format & yAdvDevice) copy_device (c, base, values++, layout_variation_idx_map);
Michiharu Arizac828d7b2020-01-15 15:32:44 -0800204 }
205
Garret Rieger2f502832021-04-23 15:21:30 -0700206 void copy_value (hb_serialize_context_t *c,
207 unsigned int new_format,
208 Flags flag,
209 Value value) const
210 {
211 // Filter by new format.
212 if (!(new_format & flag)) return;
213 c->copy (value);
214 }
215
Qunxin Liu8200e482020-02-26 13:11:42 -0800216 void collect_variation_indices (hb_collect_variation_indices_context_t *c,
Ebrahim Byagowi5a7cc7f2020-07-29 08:33:32 +0430217 const void *base,
218 const hb_array_t<const Value>& values) const
Qunxin Liu8200e482020-02-26 13:11:42 -0800219 {
220 unsigned format = *this;
221 unsigned i = 0;
222 if (format & xPlacement) i++;
223 if (format & yPlacement) i++;
224 if (format & xAdvance) i++;
225 if (format & yAdvance) i++;
226 if (format & xPlaDevice)
227 {
228 (base + get_device (&(values[i]))).collect_variation_indices (c->layout_variation_indices);
229 i++;
230 }
231
232 if (format & ValueFormat::yPlaDevice)
233 {
234 (base + get_device (&(values[i]))).collect_variation_indices (c->layout_variation_indices);
235 i++;
236 }
237
238 if (format & ValueFormat::xAdvDevice)
239 {
240
241 (base + get_device (&(values[i]))).collect_variation_indices (c->layout_variation_indices);
242 i++;
243 }
244
245 if (format & ValueFormat::yAdvDevice)
246 {
247
248 (base + get_device (&(values[i]))).collect_variation_indices (c->layout_variation_indices);
249 i++;
250 }
251 }
252
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400253 private:
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330254 bool sanitize_value_devices (hb_sanitize_context_t *c, const void *base, const Value *values) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300255 {
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400256 unsigned int format = *this;
257
258 if (format & xPlacement) values++;
259 if (format & yPlacement) values++;
260 if (format & xAdvance) values++;
261 if (format & yAdvance) values++;
262
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400263 if ((format & xPlaDevice) && !get_device (values++).sanitize (c, base)) return false;
264 if ((format & yPlaDevice) && !get_device (values++).sanitize (c, base)) return false;
265 if ((format & xAdvDevice) && !get_device (values++).sanitize (c, base)) return false;
266 if ((format & yAdvDevice) && !get_device (values++).sanitize (c, base)) return false;
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400267
268 return true;
269 }
270
Behdad Esfahbodad28f972021-03-31 12:49:14 -0600271 static inline Offset16To<Device>& get_device (Value* value)
Qunxin Liu8200e482020-02-26 13:11:42 -0800272 {
Behdad Esfahbodad28f972021-03-31 12:49:14 -0600273 return *static_cast<Offset16To<Device> *> (value);
Qunxin Liu8200e482020-02-26 13:11:42 -0800274 }
Behdad Esfahbodad28f972021-03-31 12:49:14 -0600275 static inline const Offset16To<Device>& get_device (const Value* value, bool *worked=nullptr)
Qunxin Liu8200e482020-02-26 13:11:42 -0800276 {
277 if (worked) *worked |= bool (*value);
Behdad Esfahbodad28f972021-03-31 12:49:14 -0600278 return *static_cast<const Offset16To<Device> *> (value);
Qunxin Liu8200e482020-02-26 13:11:42 -0800279 }
280
Qunxin Liud7c012a2020-02-26 13:11:42 -0800281 bool copy_device (hb_serialize_context_t *c, const void *base,
Ebrahim Byagowi5a7cc7f2020-07-29 08:33:32 +0430282 const Value *src_value, const hb_map_t *layout_variation_idx_map) const
Michiharu Arizac828d7b2020-01-15 15:32:44 -0800283 {
284 Value *dst_value = c->copy (*src_value);
285
286 if (!dst_value) return false;
287 if (*dst_value == 0) return true;
288
289 *dst_value = 0;
290 c->push ();
Qunxin Liud7c012a2020-02-26 13:11:42 -0800291 if ((base + get_device (src_value)).copy (c, layout_variation_idx_map))
Michiharu Arizac828d7b2020-01-15 15:32:44 -0800292 {
ariza188a0a42020-03-07 11:02:36 -0800293 c->add_link (*dst_value, c->pop_pack ());
Michiharu Arizac828d7b2020-01-15 15:32:44 -0800294 return true;
295 }
296 else
297 {
298 c->pop_discard ();
299 return false;
300 }
301 }
302
Behdad Esfahboda061e472019-12-10 13:31:50 -0600303 static inline const HBINT16& get_short (const Value* value, bool *worked=nullptr)
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500304 {
Behdad Esfahbodb9a51f52018-12-21 15:05:00 -0500305 if (worked) *worked |= bool (*value);
Behdad Esfahbodb84ceb22019-12-10 13:02:48 -0600306 return *reinterpret_cast<const HBINT16 *> (value);
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500307 }
Behdad Esfahbodc2ddfd22010-05-06 13:06:15 -0400308
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400309 public:
310
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330311 bool has_device () const
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330312 {
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400313 unsigned int format = *this;
314 return (format & devices) != 0;
315 }
316
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330317 bool sanitize_value (hb_sanitize_context_t *c, const void *base, const Value *values) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300318 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500319 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100320 return_trace (c->check_range (values, get_size ()) && (!has_device () || sanitize_value_devices (c, base, values)));
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400321 }
322
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330323 bool sanitize_values (hb_sanitize_context_t *c, const void *base, const Value *values, unsigned int count) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300324 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500325 TRACE_SANITIZE (this);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400326 unsigned int len = get_len ();
327
Behdad Esfahbode0144052018-11-12 14:23:31 -0500328 if (!c->check_range (values, count, get_size ())) return_trace (false);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400329
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100330 if (!has_device ()) return_trace (true);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400331
332 for (unsigned int i = 0; i < count; i++) {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400333 if (!sanitize_value_devices (c, base, values))
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330334 return_trace (false);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400335 values += len;
336 }
337
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100338 return_trace (true);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400339 }
340
Behdad Esfahbod278a91f2010-04-22 13:59:39 -0400341 /* Just sanitize referenced Device tables. Doesn't check the values themselves. */
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330342 bool sanitize_values_stride_unsafe (hb_sanitize_context_t *c, const void *base, const Value *values, unsigned int count, unsigned int stride) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300343 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500344 TRACE_SANITIZE (this);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400345
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100346 if (!has_device ()) return_trace (true);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400347
348 for (unsigned int i = 0; i < count; i++) {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400349 if (!sanitize_value_devices (c, base, values))
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330350 return_trace (false);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400351 values += stride;
352 }
353
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100354 return_trace (true);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400355 }
Garret Rieger2f502832021-04-23 15:21:30 -0700356
357 private:
358
359 void should_drop (Value value, Flags flag, unsigned int* format) const
360 {
361 if (value) return;
362 *format = *format & ~flag;
363 }
364
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400365};
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400366
Garret Riegerbadb8e42021-04-23 16:32:56 -0700367template<typename Iterator, typename SrcLookup>
Behdad Esfahboddd3972a2019-12-10 13:21:26 -0600368static void SinglePos_serialize (hb_serialize_context_t *c,
Garret Riegerbadb8e42021-04-23 16:32:56 -0700369 const SrcLookup *src,
Behdad Esfahboddd3972a2019-12-10 13:21:26 -0600370 Iterator it,
Ebrahim Byagowi5a7cc7f2020-07-29 08:33:32 +0430371 const hb_map_t *layout_variation_idx_map);
Qunxin Liu321d5582019-07-02 16:30:57 -0700372
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400373
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400374struct AnchorFormat1
375{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330376 void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id HB_UNUSED,
377 float *x, float *y) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400378 {
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700379 hb_font_t *font = c->font;
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100380 *x = font->em_fscale_x (xCoordinate);
381 *y = font->em_fscale_y (yCoordinate);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400382 }
383
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330384 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300385 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500386 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100387 return_trace (c->check_struct (this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400388 }
Ebrahim Byagowi05584132019-10-01 13:49:55 +0330389
Qunxin Liub0c3eb02019-09-18 16:11:44 -0700390 AnchorFormat1* copy (hb_serialize_context_t *c) const
391 {
392 TRACE_SERIALIZE (this);
Garret Rieger6f98a8e2021-04-02 14:37:10 -0700393 AnchorFormat1* out = c->embed<AnchorFormat1> (this);
Qunxin Liu553ffaf2021-04-07 17:15:32 -0700394 if (!out) return_trace (out);
Garret Rieger6f98a8e2021-04-02 14:37:10 -0700395 out->format = 1;
396 return_trace (out);
Qunxin Liub0c3eb02019-09-18 16:11:44 -0700397 }
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400398
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400399 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100400 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbode347fb92018-02-26 01:08:00 -0800401 FWORD xCoordinate; /* Horizontal value--in design units */
402 FWORD yCoordinate; /* Vertical value--in design units */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400403 public:
404 DEFINE_SIZE_STATIC (6);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400405};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400406
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400407struct AnchorFormat2
408{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330409 void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id,
410 float *x, float *y) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400411 {
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700412 hb_font_t *font = c->font;
Behdad Esfahbodc8f529a2019-06-19 19:33:15 -0700413
414#ifdef HB_NO_HINTING
415 *x = font->em_fscale_x (xCoordinate);
416 *y = font->em_fscale_y (yCoordinate);
417 return;
418#endif
419
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700420 unsigned int x_ppem = font->x_ppem;
421 unsigned int y_ppem = font->y_ppem;
Ebrahim Byagowieaf64942018-04-23 18:39:40 +0430422 hb_position_t cx = 0, cy = 0;
Behdad Esfahbod385f78b2018-11-07 17:19:21 -0500423 bool ret;
Behdad Esfahbod6f729b42010-04-29 03:59:06 -0400424
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700425 ret = (x_ppem || y_ppem) &&
Behdad Esfahbod385f78b2018-11-07 17:19:21 -0500426 font->get_glyph_contour_point_for_origin (glyph_id, anchorPoint, HB_DIRECTION_LTR, &cx, &cy);
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100427 *x = ret && x_ppem ? cx : font->em_fscale_x (xCoordinate);
428 *y = ret && y_ppem ? cy : font->em_fscale_y (yCoordinate);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400429 }
430
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330431 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300432 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500433 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100434 return_trace (c->check_struct (this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400435 }
436
Qunxin Liub0c3eb02019-09-18 16:11:44 -0700437 AnchorFormat2* copy (hb_serialize_context_t *c) const
438 {
439 TRACE_SERIALIZE (this);
440 return_trace (c->embed<AnchorFormat2> (this));
441 }
442
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400443 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100444 HBUINT16 format; /* Format identifier--format = 2 */
Behdad Esfahbode347fb92018-02-26 01:08:00 -0800445 FWORD xCoordinate; /* Horizontal value--in design units */
446 FWORD yCoordinate; /* Vertical value--in design units */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100447 HBUINT16 anchorPoint; /* Index to glyph contour point */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400448 public:
449 DEFINE_SIZE_STATIC (8);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400450};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400451
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400452struct AnchorFormat3
453{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330454 void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id HB_UNUSED,
455 float *x, float *y) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400456 {
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700457 hb_font_t *font = c->font;
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100458 *x = font->em_fscale_x (xCoordinate);
459 *y = font->em_fscale_y (yCoordinate);
Behdad Esfahbodc18ec2b2009-05-21 04:54:01 -0400460
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700461 if (font->x_ppem || font->num_coords)
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700462 *x += (this+xDeviceTable).get_x_delta (font, c->var_store);
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700463 if (font->y_ppem || font->num_coords)
Behdad Esfahbod1124d2e2016-09-09 22:48:27 -0700464 *y += (this+yDeviceTable).get_y_delta (font, c->var_store);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400465 }
466
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330467 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300468 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500469 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100470 return_trace (c->check_struct (this) && xDeviceTable.sanitize (c, this) && yDeviceTable.sanitize (c, this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400471 }
472
Qunxin Liud7c012a2020-02-26 13:11:42 -0800473 AnchorFormat3* copy (hb_serialize_context_t *c,
474 const hb_map_t *layout_variation_idx_map) const
Qunxin Liub0c3eb02019-09-18 16:11:44 -0700475 {
476 TRACE_SERIALIZE (this);
Qunxin Liud7c012a2020-02-26 13:11:42 -0800477 if (!layout_variation_idx_map) return_trace (nullptr);
478
Qunxin Liub0c3eb02019-09-18 16:11:44 -0700479 auto *out = c->embed<AnchorFormat3> (this);
480 if (unlikely (!out)) return_trace (nullptr);
481
Qunxin Liud7c012a2020-02-26 13:11:42 -0800482 out->xDeviceTable.serialize_copy (c, xDeviceTable, this, 0, hb_serialize_context_t::Head, layout_variation_idx_map);
483 out->yDeviceTable.serialize_copy (c, yDeviceTable, this, 0, hb_serialize_context_t::Head, layout_variation_idx_map);
Qunxin Liub0c3eb02019-09-18 16:11:44 -0700484 return_trace (out);
485 }
486
Qunxin Liu8200e482020-02-26 13:11:42 -0800487 void collect_variation_indices (hb_collect_variation_indices_context_t *c) const
488 {
489 (this+xDeviceTable).collect_variation_indices (c->layout_variation_indices);
490 (this+yDeviceTable).collect_variation_indices (c->layout_variation_indices);
491 }
492
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400493 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100494 HBUINT16 format; /* Format identifier--format = 3 */
Behdad Esfahbode347fb92018-02-26 01:08:00 -0800495 FWORD xCoordinate; /* Horizontal value--in design units */
496 FWORD yCoordinate; /* Vertical value--in design units */
Behdad Esfahbodad28f972021-03-31 12:49:14 -0600497 Offset16To<Device>
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400498 xDeviceTable; /* Offset to Device table for X
499 * coordinate-- from beginning of
500 * Anchor table (may be NULL) */
Behdad Esfahbodad28f972021-03-31 12:49:14 -0600501 Offset16To<Device>
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400502 yDeviceTable; /* Offset to Device table for Y
503 * coordinate-- from beginning of
504 * Anchor table (may be NULL) */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400505 public:
506 DEFINE_SIZE_STATIC (10);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400507};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400508
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400509struct Anchor
510{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330511 void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id,
512 float *x, float *y) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400513 {
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400514 *x = *y = 0;
515 switch (u.format) {
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700516 case 1: u.format1.get_anchor (c, glyph_id, x, y); return;
517 case 2: u.format2.get_anchor (c, glyph_id, x, y); return;
518 case 3: u.format3.get_anchor (c, glyph_id, x, y); return;
Behdad Esfahbod7cdd6ab2018-01-10 04:33:08 +0100519 default: return;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400520 }
521 }
522
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330523 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300524 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500525 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100526 if (!u.format.sanitize (c)) return_trace (false);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400527 switch (u.format) {
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100528 case 1: return_trace (u.format1.sanitize (c));
529 case 2: return_trace (u.format2.sanitize (c));
530 case 3: return_trace (u.format3.sanitize (c));
531 default:return_trace (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400532 }
533 }
534
Garret Rieger6f98a8e2021-04-02 14:37:10 -0700535 bool subset (hb_subset_context_t *c) const
Qunxin Liub0c3eb02019-09-18 16:11:44 -0700536 {
Garret Rieger6f98a8e2021-04-02 14:37:10 -0700537 TRACE_SUBSET (this);
Qunxin Liub0c3eb02019-09-18 16:11:44 -0700538 switch (u.format) {
Garret Rieger6f98a8e2021-04-02 14:37:10 -0700539 case 1: return_trace (bool (reinterpret_cast<Anchor *> (u.format1.copy (c->serializer))));
Garret Rieger92757f62021-04-22 15:18:35 -0700540 case 2:
Garret Rieger46d4a5e2021-07-29 15:07:13 -0700541 if (c->plan->flags & HB_SUBSET_FLAGS_NO_HINTING)
Garret Rieger92757f62021-04-22 15:18:35 -0700542 {
543 // AnchorFormat 2 just containins extra hinting information, so
544 // if hints are being dropped convert to format 1.
545 return_trace (bool (reinterpret_cast<Anchor *> (u.format1.copy (c->serializer))));
546 }
547 return_trace (bool (reinterpret_cast<Anchor *> (u.format2.copy (c->serializer))));
Garret Rieger6f98a8e2021-04-02 14:37:10 -0700548 case 3: return_trace (bool (reinterpret_cast<Anchor *> (u.format3.copy (c->serializer,
Garret Rieger92757f62021-04-22 15:18:35 -0700549 c->plan->layout_variation_idx_map))));
Garret Rieger6f98a8e2021-04-02 14:37:10 -0700550 default:return_trace (false);
Qunxin Liub0c3eb02019-09-18 16:11:44 -0700551 }
552 }
553
Qunxin Liu8200e482020-02-26 13:11:42 -0800554 void collect_variation_indices (hb_collect_variation_indices_context_t *c) const
555 {
556 switch (u.format) {
557 case 1: case 2:
558 return;
559 case 3:
560 u.format3.collect_variation_indices (c);
561 return;
562 default: return;
563 }
564 }
565
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400566 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400567 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100568 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400569 AnchorFormat1 format1;
570 AnchorFormat2 format2;
571 AnchorFormat3 format3;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400572 } u;
Behdad Esfahboded074222010-05-10 18:08:46 -0400573 public:
Behdad Esfahbod596e4712010-05-10 18:47:48 -0400574 DEFINE_SIZE_UNION (2, format);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400575};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400576
577
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400578struct AnchorMatrix
579{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330580 const Anchor& get_anchor (unsigned int row, unsigned int col,
581 unsigned int cols, bool *found) const
582 {
Behdad Esfahbode95e0312013-01-08 16:15:46 -0600583 *found = false;
Ebrahim Byagowi2dda6dd2020-04-20 14:12:45 +0430584 if (unlikely (row >= rows || col >= cols)) return Null (Anchor);
Behdad Esfahbod093c5202014-12-12 21:07:53 -0800585 *found = !matrixZ[row * cols + col].is_null ();
586 return this+matrixZ[row * cols + col];
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400587 }
588
Qunxin Liu82afc752020-02-04 13:24:37 -0800589 template <typename Iterator,
590 hb_requires (hb_is_iterator (Iterator))>
Qunxin Liu8200e482020-02-26 13:11:42 -0800591 void collect_variation_indices (hb_collect_variation_indices_context_t *c,
592 Iterator index_iter) const
593 {
594 for (unsigned i : index_iter)
595 (this+matrixZ[i]).collect_variation_indices (c);
596 }
597
598 template <typename Iterator,
Garret Rieger6f98a8e2021-04-02 14:37:10 -0700599 hb_requires (hb_is_iterator (Iterator))>
Qunxin Liu3a0b05f2020-02-06 15:08:26 -0800600 bool subset (hb_subset_context_t *c,
Garret Rieger6f98a8e2021-04-02 14:37:10 -0700601 unsigned num_rows,
602 Iterator index_iter) const
Qunxin Liu3a0b05f2020-02-06 15:08:26 -0800603 {
604 TRACE_SUBSET (this);
Qunxin Liu3a0b05f2020-02-06 15:08:26 -0800605
Garret Rieger6f98a8e2021-04-02 14:37:10 -0700606 auto *out = c->serializer->start_embed (this);
Qunxin Liu3a0b05f2020-02-06 15:08:26 -0800607
Garret Rieger6f98a8e2021-04-02 14:37:10 -0700608 if (!index_iter) return_trace (false);
609 if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
610
611 out->rows = num_rows;
612 for (const unsigned i : index_iter)
613 {
614 auto *offset = c->serializer->embed (matrixZ[i]);
615 if (!offset) return_trace (false);
616 offset->serialize_subset (c, matrixZ[i], this);
617 }
618
Qunxin Liu3a0b05f2020-02-06 15:08:26 -0800619 return_trace (true);
620 }
621
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330622 bool sanitize (hb_sanitize_context_t *c, unsigned int cols) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300623 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500624 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100625 if (!c->check_struct (this)) return_trace (false);
Behdad Esfahbodbddeb2b2018-07-10 14:12:37 +0200626 if (unlikely (hb_unsigned_mul_overflows (rows, cols))) return_trace (false);
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400627 unsigned int count = rows * cols;
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200628 if (!c->check_array (matrixZ.arrayZ, count)) return_trace (false);
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400629 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100630 if (!matrixZ[i].sanitize (c, this)) return_trace (false);
631 return_trace (true);
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400632 }
633
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100634 HBUINT16 rows; /* Number of rows */
Behdad Esfahbodad28f972021-03-31 12:49:14 -0600635 UnsizedArrayOf<Offset16To<Anchor>>
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200636 matrixZ; /* Matrix of offsets to Anchor tables--
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400637 * from beginning of AnchorMatrix table */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400638 public:
Behdad Esfahbod093c5202014-12-12 21:07:53 -0800639 DEFINE_SIZE_ARRAY (2, matrixZ);
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400640};
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400641
642
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400643struct MarkRecord
644{
Behdad Esfahbod377bfc52009-05-21 04:58:24 -0400645 friend struct MarkArray;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400646
Qunxin Liu82afc752020-02-04 13:24:37 -0800647 unsigned get_class () const { return (unsigned) klass; }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330648 bool sanitize (hb_sanitize_context_t *c, const void *base) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300649 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500650 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100651 return_trace (c->check_struct (this) && markAnchor.sanitize (c, base));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400652 }
653
Garret Rieger6f98a8e2021-04-02 14:37:10 -0700654 MarkRecord *subset (hb_subset_context_t *c,
655 const void *src_base,
656 const hb_map_t *klass_mapping) const
Qunxin Liu82afc752020-02-04 13:24:37 -0800657 {
Garret Rieger6f98a8e2021-04-02 14:37:10 -0700658 TRACE_SUBSET (this);
659 auto *out = c->serializer->embed (this);
Qunxin Liu82afc752020-02-04 13:24:37 -0800660 if (unlikely (!out)) return_trace (nullptr);
Ebrahim Byagowi07acd1a2020-03-08 23:39:24 +0330661
Qunxin Liu82afc752020-02-04 13:24:37 -0800662 out->klass = klass_mapping->get (klass);
Garret Rieger6f98a8e2021-04-02 14:37:10 -0700663 out->markAnchor.serialize_subset (c, markAnchor, src_base);
Qunxin Liu82afc752020-02-04 13:24:37 -0800664 return_trace (out);
665 }
666
Qunxin Liu8200e482020-02-26 13:11:42 -0800667 void collect_variation_indices (hb_collect_variation_indices_context_t *c,
668 const void *src_base) const
669 {
670 (src_base+markAnchor).collect_variation_indices (c);
671 }
672
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400673 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100674 HBUINT16 klass; /* Class defined for this mark */
Behdad Esfahbodad28f972021-03-31 12:49:14 -0600675 Offset16To<Anchor>
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400676 markAnchor; /* Offset to Anchor table--from
677 * beginning of MarkArray table */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400678 public:
679 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400680};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400681
Behdad Esfahbod5639e252021-03-31 16:04:43 -0600682struct MarkArray : Array16Of<MarkRecord> /* Array of MarkRecords--in Coverage order */
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400683{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330684 bool apply (hb_ot_apply_context_t *c,
685 unsigned int mark_index, unsigned int glyph_index,
686 const AnchorMatrix &anchors, unsigned int class_count,
687 unsigned int glyph_pos) const
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400688 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500689 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200690 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbod5639e252021-03-31 16:04:43 -0600691 const MarkRecord &record = Array16Of<MarkRecord>::operator[](mark_index);
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400692 unsigned int mark_class = record.klass;
693
694 const Anchor& mark_anchor = this + record.markAnchor;
Behdad Esfahbode95e0312013-01-08 16:15:46 -0600695 bool found;
696 const Anchor& glyph_anchor = anchors.get_anchor (glyph_index, mark_class, class_count, &found);
697 /* If this subtable doesn't have an anchor for this base and this class,
698 * return false such that the subsequent subtables have a chance at it. */
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100699 if (unlikely (!found)) return_trace (false);
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400700
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100701 float mark_x, mark_y, base_x, base_y;
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400702
Behdad Esfahbodb65aa1c2017-08-10 20:32:02 -0700703 buffer->unsafe_to_break (glyph_pos, buffer->idx);
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700704 mark_anchor.get_anchor (c, buffer->cur().codepoint, &mark_x, &mark_y);
705 glyph_anchor.get_anchor (c, buffer->info[glyph_pos].codepoint, &base_x, &base_y);
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400706
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200707 hb_glyph_position_t &o = buffer->cur_pos();
Behdad Esfahboddfc57802019-05-07 23:26:09 -0700708 o.x_offset = roundf (base_x - mark_x);
709 o.y_offset = roundf (base_y - mark_y);
Behdad Esfahbod686567b2016-02-11 15:25:28 +0700710 o.attach_type() = ATTACH_TYPE_MARK;
Behdad Esfahbod0f6278d2016-02-11 14:49:10 +0700711 o.attach_chain() = (int) glyph_pos - (int) buffer->idx;
Behdad Esfahbod550bd142015-11-04 22:58:58 -0800712 buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400713
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200714 buffer->idx++;
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100715 return_trace (true);
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400716 }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400717
Garret Rieger6f98a8e2021-04-02 14:37:10 -0700718 template <typename Iterator,
719 hb_requires (hb_is_iterator (Iterator))>
720 bool subset (hb_subset_context_t *c,
721 Iterator coverage,
722 const hb_map_t *klass_mapping) const
Qunxin Liu82afc752020-02-04 13:24:37 -0800723 {
Garret Rieger6f98a8e2021-04-02 14:37:10 -0700724 TRACE_SUBSET (this);
725 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
726
727 auto* out = c->serializer->start_embed (this);
728 if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
729
730 auto mark_iter =
731 + hb_zip (coverage, this->iter ())
732 | hb_filter (glyphset, hb_first)
733 | hb_map (hb_second)
734 ;
735
736 unsigned new_length = 0;
737 for (const auto& mark_record : mark_iter) {
738 if (unlikely (!mark_record.subset (c, this, klass_mapping)))
739 return_trace (false);
740 new_length++;
741 }
742
743 if (unlikely (!c->serializer->check_assign (out->len, new_length,
744 HB_SERIALIZE_ERROR_ARRAY_OVERFLOW)))
745 return_trace (false);
746
Qunxin Liu82afc752020-02-04 13:24:37 -0800747 return_trace (true);
748 }
749
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330750 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300751 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500752 TRACE_SANITIZE (this);
Behdad Esfahbod5639e252021-03-31 16:04:43 -0600753 return_trace (Array16Of<MarkRecord>::sanitize (c, this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400754 }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400755};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400756
757
758/* Lookups */
759
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400760struct SinglePosFormat1
761{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330762 bool intersects (const hb_set_t *glyphs) const
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -0700763 { return (this+coverage).intersects (glyphs); }
764
Qunxin Liu0b39c482019-10-22 16:00:43 -0700765 void closure_lookups (hb_closure_lookups_context_t *c) const {}
Qunxin Liu8200e482020-02-26 13:11:42 -0800766 void collect_variation_indices (hb_collect_variation_indices_context_t *c) const
767 {
768 if (!valueFormat.has_device ()) return;
Ebrahim Byagowid0e2add2020-07-18 22:14:52 +0430769
Qunxin Liu8200e482020-02-26 13:11:42 -0800770 auto it =
771 + hb_iter (this+coverage)
772 | hb_filter (c->glyph_set)
773 ;
774
775 if (!it) return;
776 valueFormat.collect_variation_indices (c, this, values.as_array (valueFormat.get_len ()));
777 }
Qunxin Liu0b39c482019-10-22 16:00:43 -0700778
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330779 void collect_glyphs (hb_collect_glyphs_context_t *c) const
Behdad Esfahbod5cf53c02020-04-23 10:55:41 -0700780 { if (unlikely (!(this+coverage).collect_coverage (c->input))) return; }
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500781
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330782 const Coverage &get_coverage () const { return this+coverage; }
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400783
Garret Riegerbadb8e42021-04-23 16:32:56 -0700784 ValueFormat get_value_format () const { return valueFormat; }
785
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330786 bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400787 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500788 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200789 hb_buffer_t *buffer = c->buffer;
790 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100791 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400792
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700793 valueFormat.apply_value (c, this, values, buffer->cur_pos());
Behdad Esfahbodf53d4342009-05-30 22:17:32 -0400794
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200795 buffer->idx++;
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100796 return_trace (true);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400797 }
798
Qunxin Liu321d5582019-07-02 16:30:57 -0700799 template<typename Iterator,
Garret Riegerbadb8e42021-04-23 16:32:56 -0700800 typename SrcLookup,
801 hb_requires (hb_is_iterator (Iterator))>
Qunxin Liu321d5582019-07-02 16:30:57 -0700802 void serialize (hb_serialize_context_t *c,
Garret Riegerbadb8e42021-04-23 16:32:56 -0700803 const SrcLookup *src,
Qunxin Liu321d5582019-07-02 16:30:57 -0700804 Iterator it,
Garret Rieger2f502832021-04-23 15:21:30 -0700805 ValueFormat newFormat,
Ebrahim Byagowi5a7cc7f2020-07-29 08:33:32 +0430806 const hb_map_t *layout_variation_idx_map)
Qunxin Liu321d5582019-07-02 16:30:57 -0700807 {
Behdad Esfahbodf0a18922021-07-28 17:36:22 -0600808 if (unlikely (!c->extend_min (this))) return;
Garret Rieger5df7c662021-04-22 16:45:14 -0700809 if (unlikely (!c->check_assign (valueFormat,
Garret Rieger2f502832021-04-23 15:21:30 -0700810 newFormat,
Garret Rieger5df7c662021-04-22 16:45:14 -0700811 HB_SERIALIZE_ERROR_INT_OVERFLOW))) return;
Qunxin Liu321d5582019-07-02 16:30:57 -0700812
Garret Rieger5df7c662021-04-22 16:45:14 -0700813 for (const hb_array_t<const Value>& _ : + it | hb_map (hb_second))
814 {
Garret Riegerbadb8e42021-04-23 16:32:56 -0700815 src->get_value_format ().copy_values (c, newFormat, src, &_, layout_variation_idx_map);
Garret Rieger5df7c662021-04-22 16:45:14 -0700816 // Only serialize the first entry in the iterator, the rest are assumed to
817 // be the same.
818 break;
819 }
Qunxin Liu321d5582019-07-02 16:30:57 -0700820
821 auto glyphs =
822 + it
823 | hb_map_retains_sorting (hb_first)
824 ;
825
Garret Rieger8443cc82021-06-11 13:40:39 -0700826 coverage.serialize_serialize (c, glyphs);
Qunxin Liu321d5582019-07-02 16:30:57 -0700827 }
828
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330829 bool subset (hb_subset_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -0700830 {
831 TRACE_SUBSET (this);
Garret Riegere5835052020-09-29 11:05:08 -0700832 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
Qunxin Liu321d5582019-07-02 16:30:57 -0700833 const hb_map_t &glyph_map = *c->plan->glyph_map;
834
Qunxin Liu321d5582019-07-02 16:30:57 -0700835 auto it =
836 + hb_iter (this+coverage)
837 | hb_filter (glyphset)
Behdad Esfahbod3c812462019-09-01 19:25:50 -0500838 | hb_map_retains_sorting (glyph_map)
839 | hb_zip (hb_repeat (values.as_array (valueFormat.get_len ())))
Qunxin Liu321d5582019-07-02 16:30:57 -0700840 ;
841
842 bool ret = bool (it);
Garret Riegerbadb8e42021-04-23 16:32:56 -0700843 SinglePos_serialize (c->serializer, this, it, c->plan->layout_variation_idx_map);
Qunxin Liu321d5582019-07-02 16:30:57 -0700844 return_trace (ret);
Behdad Esfahbod339d3602018-09-03 17:33:34 -0700845 }
846
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330847 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300848 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500849 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100850 return_trace (c->check_struct (this) &&
851 coverage.sanitize (c, this) &&
852 valueFormat.sanitize_value (c, this, values));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400853 }
854
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400855 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100856 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbodad28f972021-03-31 12:49:14 -0600857 Offset16To<Coverage>
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400858 coverage; /* Offset to Coverage table--from
859 * beginning of subtable */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400860 ValueFormat valueFormat; /* Defines the types of data in the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400861 * ValueRecord */
862 ValueRecord values; /* Defines positioning
863 * value(s)--applied to all glyphs in
864 * the Coverage table */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400865 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400866 DEFINE_SIZE_ARRAY (6, values);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400867};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400868
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400869struct SinglePosFormat2
870{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330871 bool intersects (const hb_set_t *glyphs) const
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -0700872 { return (this+coverage).intersects (glyphs); }
873
Qunxin Liu0b39c482019-10-22 16:00:43 -0700874 void closure_lookups (hb_closure_lookups_context_t *c) const {}
Qunxin Liu8200e482020-02-26 13:11:42 -0800875 void collect_variation_indices (hb_collect_variation_indices_context_t *c) const
876 {
877 if (!valueFormat.has_device ()) return;
Ebrahim Byagowid0e2add2020-07-18 22:14:52 +0430878
Qunxin Liu8200e482020-02-26 13:11:42 -0800879 auto it =
880 + hb_zip (this+coverage, hb_range ((unsigned) valueCount))
881 | hb_filter (c->glyph_set, hb_first)
882 ;
883
884 if (!it) return;
885
886 unsigned sub_length = valueFormat.get_len ();
887 const hb_array_t<const Value> values_array = values.as_array (valueCount * sub_length);
888
889 for (unsigned i : + it
Ebrahim Byagowi5a7cc7f2020-07-29 08:33:32 +0430890 | hb_map (hb_second))
Qunxin Liu8200e482020-02-26 13:11:42 -0800891 valueFormat.collect_variation_indices (c, this, values_array.sub_array (i * sub_length, sub_length));
892
893 }
Qunxin Liu0b39c482019-10-22 16:00:43 -0700894
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330895 void collect_glyphs (hb_collect_glyphs_context_t *c) const
Behdad Esfahbod5cf53c02020-04-23 10:55:41 -0700896 { if (unlikely (!(this+coverage).collect_coverage (c->input))) return; }
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500897
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330898 const Coverage &get_coverage () const { return this+coverage; }
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400899
Garret Riegerbadb8e42021-04-23 16:32:56 -0700900 ValueFormat get_value_format () const { return valueFormat; }
901
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330902 bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400903 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500904 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200905 hb_buffer_t *buffer = c->buffer;
906 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100907 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400908
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100909 if (likely (index >= valueCount)) return_trace (false);
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400910
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700911 valueFormat.apply_value (c, this,
Behdad Esfahbod4b8487d2010-03-16 03:46:17 -0400912 &values[index * valueFormat.get_len ()],
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200913 buffer->cur_pos());
Behdad Esfahbodf53d4342009-05-30 22:17:32 -0400914
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200915 buffer->idx++;
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100916 return_trace (true);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400917 }
918
Qunxin Liu321d5582019-07-02 16:30:57 -0700919 template<typename Iterator,
Garret Riegerbadb8e42021-04-23 16:32:56 -0700920 typename SrcLookup,
921 hb_requires (hb_is_iterator (Iterator))>
Qunxin Liu321d5582019-07-02 16:30:57 -0700922 void serialize (hb_serialize_context_t *c,
Garret Riegerbadb8e42021-04-23 16:32:56 -0700923 const SrcLookup *src,
Qunxin Liu321d5582019-07-02 16:30:57 -0700924 Iterator it,
Garret Rieger2f502832021-04-23 15:21:30 -0700925 ValueFormat newFormat,
Ebrahim Byagowi5a7cc7f2020-07-29 08:33:32 +0430926 const hb_map_t *layout_variation_idx_map)
Qunxin Liu321d5582019-07-02 16:30:57 -0700927 {
Behdad Esfahbodf0a18922021-07-28 17:36:22 -0600928 auto out = c->extend_min (this);
Michiharu Arizac828d7b2020-01-15 15:32:44 -0800929 if (unlikely (!out)) return;
Garret Rieger2f502832021-04-23 15:21:30 -0700930 if (unlikely (!c->check_assign (valueFormat, newFormat, HB_SERIALIZE_ERROR_INT_OVERFLOW))) return;
Garret Riegerb14475d2021-03-18 10:51:26 -0700931 if (unlikely (!c->check_assign (valueCount, it.len (), HB_SERIALIZE_ERROR_ARRAY_OVERFLOW))) return;
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430932
Michiharu Arizac828d7b2020-01-15 15:32:44 -0800933 + it
934 | hb_map (hb_second)
935 | hb_apply ([&] (hb_array_t<const Value> _)
Garret Riegerbadb8e42021-04-23 16:32:56 -0700936 { src->get_value_format ().copy_values (c, newFormat, src, &_, layout_variation_idx_map); })
Michiharu Arizac828d7b2020-01-15 15:32:44 -0800937 ;
Qunxin Liu321d5582019-07-02 16:30:57 -0700938
939 auto glyphs =
940 + it
941 | hb_map_retains_sorting (hb_first)
942 ;
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430943
Garret Rieger8443cc82021-06-11 13:40:39 -0700944 coverage.serialize_serialize (c, glyphs);
Qunxin Liu321d5582019-07-02 16:30:57 -0700945 }
946
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330947 bool subset (hb_subset_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -0700948 {
949 TRACE_SUBSET (this);
Garret Riegere5835052020-09-29 11:05:08 -0700950 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
Qunxin Liu321d5582019-07-02 16:30:57 -0700951 const hb_map_t &glyph_map = *c->plan->glyph_map;
952
953 unsigned sub_length = valueFormat.get_len ();
Behdad Esfahbode0e0c8c2019-08-29 14:58:16 -0700954 auto values_array = values.as_array (valueCount * sub_length);
Qunxin Liu321d5582019-07-02 16:30:57 -0700955
956 auto it =
957 + hb_zip (this+coverage, hb_range ((unsigned) valueCount))
958 | hb_filter (glyphset, hb_first)
959 | hb_map_retains_sorting ([&] (const hb_pair_t<hb_codepoint_t, unsigned>& _)
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430960 {
Behdad Esfahbode0e0c8c2019-08-29 14:58:16 -0700961 return hb_pair (glyph_map[_.first],
962 values_array.sub_array (_.second * sub_length,
963 sub_length));
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430964 })
Qunxin Liu321d5582019-07-02 16:30:57 -0700965 ;
966
967 bool ret = bool (it);
Garret Riegerbadb8e42021-04-23 16:32:56 -0700968 SinglePos_serialize (c->serializer, this, it, c->plan->layout_variation_idx_map);
Qunxin Liu321d5582019-07-02 16:30:57 -0700969 return_trace (ret);
Behdad Esfahbod339d3602018-09-03 17:33:34 -0700970 }
971
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330972 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300973 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500974 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100975 return_trace (c->check_struct (this) &&
976 coverage.sanitize (c, this) &&
977 valueFormat.sanitize_values (c, this, values, valueCount));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400978 }
979
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400980 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100981 HBUINT16 format; /* Format identifier--format = 2 */
Behdad Esfahbodad28f972021-03-31 12:49:14 -0600982 Offset16To<Coverage>
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400983 coverage; /* Offset to Coverage table--from
984 * beginning of subtable */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400985 ValueFormat valueFormat; /* Defines the types of data in the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400986 * ValueRecord */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100987 HBUINT16 valueCount; /* Number of ValueRecords */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400988 ValueRecord values; /* Array of ValueRecords--positioning
989 * values applied to glyphs */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400990 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400991 DEFINE_SIZE_ARRAY (8, values);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400992};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400993
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400994struct SinglePos
995{
Qunxin Liu321d5582019-07-02 16:30:57 -0700996 template<typename Iterator,
997 hb_requires (hb_is_iterator (Iterator))>
998 unsigned get_format (Iterator glyph_val_iter_pairs)
999 {
Qunxin Liu321d5582019-07-02 16:30:57 -07001000 hb_array_t<const Value> first_val_iter = hb_second (*glyph_val_iter_pairs);
1001
Ebrahim Byagowi68d39ea2019-09-24 13:52:46 +03301002 for (const auto iter : glyph_val_iter_pairs)
1003 for (const auto _ : hb_zip (iter.second, first_val_iter))
Ebrahim Byagowi6e42a412019-09-21 16:25:54 +04301004 if (_.first != _.second)
Ebrahim Byagowic1a585f2019-09-21 23:30:52 +04301005 return 2;
Qunxin Liu321d5582019-07-02 16:30:57 -07001006
Ebrahim Byagowic1a585f2019-09-21 23:30:52 +04301007 return 1;
Qunxin Liu321d5582019-07-02 16:30:57 -07001008 }
1009
1010
1011 template<typename Iterator,
Garret Riegerbadb8e42021-04-23 16:32:56 -07001012 typename SrcLookup,
1013 hb_requires (hb_is_iterator (Iterator))>
Qunxin Liu321d5582019-07-02 16:30:57 -07001014 void serialize (hb_serialize_context_t *c,
Garret Riegerbadb8e42021-04-23 16:32:56 -07001015 const SrcLookup* src,
Qunxin Liu321d5582019-07-02 16:30:57 -07001016 Iterator glyph_val_iter_pairs,
Ebrahim Byagowi5a7cc7f2020-07-29 08:33:32 +04301017 const hb_map_t *layout_variation_idx_map)
Qunxin Liu321d5582019-07-02 16:30:57 -07001018 {
1019 if (unlikely (!c->extend_min (u.format))) return;
1020 unsigned format = 2;
Garret Riegerbadb8e42021-04-23 16:32:56 -07001021 ValueFormat new_format = src->get_value_format ();
Qunxin Liu321d5582019-07-02 16:30:57 -07001022
Garret Rieger2f502832021-04-23 15:21:30 -07001023 if (glyph_val_iter_pairs)
1024 {
1025 format = get_format (glyph_val_iter_pairs);
Garret Riegerbadb8e42021-04-23 16:32:56 -07001026 new_format = src->get_value_format ().get_effective_format (+ glyph_val_iter_pairs | hb_map (hb_second));
Garret Rieger2f502832021-04-23 15:21:30 -07001027 }
Qunxin Liu321d5582019-07-02 16:30:57 -07001028
1029 u.format = format;
1030 switch (u.format) {
Garret Riegerbadb8e42021-04-23 16:32:56 -07001031 case 1: u.format1.serialize (c,
Garret Rieger2f502832021-04-23 15:21:30 -07001032 src,
1033 glyph_val_iter_pairs,
1034 new_format,
1035 layout_variation_idx_map);
Garret Riegerbadb8e42021-04-23 16:32:56 -07001036 return;
1037 case 2: u.format2.serialize (c,
1038 src,
1039 glyph_val_iter_pairs,
1040 new_format,
1041 layout_variation_idx_map);
1042 return;
Qunxin Liu321d5582019-07-02 16:30:57 -07001043 default:return;
1044 }
1045 }
1046
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07001047 template <typename context_t, typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -07001048 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001049 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08001050 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04001051 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001052 switch (u.format) {
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -06001053 case 1: return_trace (c->dispatch (u.format1, std::forward<Ts> (ds)...));
1054 case 2: return_trace (c->dispatch (u.format2, std::forward<Ts> (ds)...));
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001055 default:return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001056 }
1057 }
1058
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001059 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001060 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001061 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001062 SinglePosFormat1 format1;
1063 SinglePosFormat2 format2;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001064 } u;
1065};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001066
Garret Riegerbadb8e42021-04-23 16:32:56 -07001067template<typename Iterator, typename SrcLookup>
Behdad Esfahboddd3972a2019-12-10 13:21:26 -06001068static void
Qunxin Liu321d5582019-07-02 16:30:57 -07001069SinglePos_serialize (hb_serialize_context_t *c,
Garret Riegerbadb8e42021-04-23 16:32:56 -07001070 const SrcLookup *src,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +04301071 Iterator it,
Ebrahim Byagowi5a7cc7f2020-07-29 08:33:32 +04301072 const hb_map_t *layout_variation_idx_map)
Garret Riegerbadb8e42021-04-23 16:32:56 -07001073{ c->start_embed<SinglePos> ()->serialize (c, src, it, layout_variation_idx_map); }
Qunxin Liu321d5582019-07-02 16:30:57 -07001074
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001075
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001076struct PairValueRecord
1077{
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -04001078 friend struct PairSet;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001079
Behdad Esfahbod39afe602019-12-10 12:04:44 -06001080 int cmp (hb_codepoint_t k) const
1081 { return secondGlyph.cmp (k); }
1082
Garret Riegerbadb8e42021-04-23 16:32:56 -07001083 struct context_t
Michiharu Arizac828d7b2020-01-15 15:32:44 -08001084 {
Ebrahim Byagowi07acd1a2020-03-08 23:39:24 +03301085 const void *base;
Michiharu Arizac828d7b2020-01-15 15:32:44 -08001086 const ValueFormat *valueFormats;
Garret Riegerbadb8e42021-04-23 16:32:56 -07001087 const ValueFormat *newFormats;
Michiharu Arizac828d7b2020-01-15 15:32:44 -08001088 unsigned len1; /* valueFormats[0].get_len() */
1089 const hb_map_t *glyph_map;
Qunxin Liud7c012a2020-02-26 13:11:42 -08001090 const hb_map_t *layout_variation_idx_map;
Michiharu Arizac828d7b2020-01-15 15:32:44 -08001091 };
1092
Garret Riegerbadb8e42021-04-23 16:32:56 -07001093 bool subset (hb_subset_context_t *c,
1094 context_t *closure) const
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001095 {
1096 TRACE_SERIALIZE (this);
Garret Riegerbadb8e42021-04-23 16:32:56 -07001097 auto *s = c->serializer;
1098 auto *out = s->start_embed (*this);
1099 if (unlikely (!s->extend_min (out))) return_trace (false);
Behdad Esfahbod39afe602019-12-10 12:04:44 -06001100
Michiharu Arizac828d7b2020-01-15 15:32:44 -08001101 out->secondGlyph = (*closure->glyph_map)[secondGlyph];
1102
Garret Riegerbadb8e42021-04-23 16:32:56 -07001103 closure->valueFormats[0].copy_values (s,
1104 closure->newFormats[0],
1105 closure->base, &values[0],
1106 closure->layout_variation_idx_map);
1107 closure->valueFormats[1].copy_values (s,
1108 closure->newFormats[1],
1109 closure->base,
1110 &values[closure->len1],
1111 closure->layout_variation_idx_map);
Michiharu Arizac828d7b2020-01-15 15:32:44 -08001112
1113 return_trace (true);
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001114 }
1115
Qunxin Liu8200e482020-02-26 13:11:42 -08001116 void collect_variation_indices (hb_collect_variation_indices_context_t *c,
Ebrahim Byagowi5a7cc7f2020-07-29 08:33:32 +04301117 const ValueFormat *valueFormats,
1118 const void *base) const
Qunxin Liu8200e482020-02-26 13:11:42 -08001119 {
1120 unsigned record1_len = valueFormats[0].get_len ();
1121 unsigned record2_len = valueFormats[1].get_len ();
1122 const hb_array_t<const Value> values_array = values.as_array (record1_len + record2_len);
1123
1124 if (valueFormats[0].has_device ())
1125 valueFormats[0].collect_variation_indices (c, base, values_array.sub_array (0, record1_len));
1126
1127 if (valueFormats[1].has_device ())
1128 valueFormats[1].collect_variation_indices (c, base, values_array.sub_array (record1_len, record2_len));
1129 }
1130
Garret Riegerd356d0f2021-04-23 17:41:09 -07001131 bool intersects (const hb_set_t& glyphset) const
1132 {
1133 return glyphset.has(secondGlyph);
1134 }
1135
1136 const Value* get_values_1 () const
1137 {
1138 return &values[0];
1139 }
1140
1141 const Value* get_values_2 (ValueFormat format1) const
1142 {
1143 return &values[format1.get_len ()];
1144 }
1145
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001146 protected:
Behdad Esfahbodc852b862021-09-19 16:30:12 -04001147 HBGlyphID16 secondGlyph; /* GlyphID of second glyph in the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001148 * pair--first glyph is listed in the
1149 * Coverage table */
1150 ValueRecord values; /* Positioning data for the first glyph
1151 * followed by for second glyph */
Behdad Esfahbod569da922010-05-10 16:38:32 -04001152 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001153 DEFINE_SIZE_ARRAY (2, values);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001154};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001155
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001156struct PairSet
1157{
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -04001158 friend struct PairPosFormat1;
1159
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301160 bool intersects (const hb_set_t *glyphs,
Behdad Esfahbod9d8c7202019-03-29 21:59:28 -07001161 const ValueFormat *valueFormats) const
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -07001162 {
Behdad Esfahbodfb059082018-11-30 20:45:40 -05001163 unsigned int len1 = valueFormats[0].get_len ();
1164 unsigned int len2 = valueFormats[1].get_len ();
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -07001165 unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);
1166
Behdad Esfahboddff2c452018-09-10 23:29:26 +02001167 const PairValueRecord *record = &firstPairValueRecord;
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -07001168 unsigned int count = len;
1169 for (unsigned int i = 0; i < count; i++)
1170 {
1171 if (glyphs->has (record->secondGlyph))
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +03301172 return true;
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -07001173 record = &StructAtOffset<const PairValueRecord> (record, record_size);
1174 }
1175 return false;
1176 }
1177
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301178 void collect_glyphs (hb_collect_glyphs_context_t *c,
Ebrahim Byagowid0e2add2020-07-18 22:14:52 +04301179 const ValueFormat *valueFormats) const
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001180 {
Behdad Esfahbodfb059082018-11-30 20:45:40 -05001181 unsigned int len1 = valueFormats[0].get_len ();
1182 unsigned int len2 = valueFormats[1].get_len ();
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001183 unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001184
Behdad Esfahboddff2c452018-09-10 23:29:26 +02001185 const PairValueRecord *record = &firstPairValueRecord;
Behdad Esfahbod81f27df2017-12-16 06:12:06 -08001186 c->input->add_array (&record->secondGlyph, len, record_size);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001187 }
1188
Qunxin Liu8200e482020-02-26 13:11:42 -08001189 void collect_variation_indices (hb_collect_variation_indices_context_t *c,
Ebrahim Byagowi5a7cc7f2020-07-29 08:33:32 +04301190 const ValueFormat *valueFormats) const
Qunxin Liu8200e482020-02-26 13:11:42 -08001191 {
1192 unsigned len1 = valueFormats[0].get_len ();
1193 unsigned len2 = valueFormats[1].get_len ();
1194 unsigned record_size = HBUINT16::static_size * (1 + len1 + len2);
1195
1196 const PairValueRecord *record = &firstPairValueRecord;
1197 unsigned count = len;
1198 for (unsigned i = 0; i < count; i++)
1199 {
1200 if (c->glyph_set->has (record->secondGlyph))
1201 { record->collect_variation_indices (c, valueFormats, this); }
1202
1203 record = &StructAtOffset<const PairValueRecord> (record, record_size);
1204 }
1205 }
1206
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301207 bool apply (hb_ot_apply_context_t *c,
Behdad Esfahbod39afe602019-12-10 12:04:44 -06001208 const ValueFormat *valueFormats,
1209 unsigned int pos) const
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -04001210 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001211 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001212 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbodfb059082018-11-30 20:45:40 -05001213 unsigned int len1 = valueFormats[0].get_len ();
1214 unsigned int len2 = valueFormats[1].get_len ();
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001215 unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -04001216
Behdad Esfahbod39afe602019-12-10 12:04:44 -06001217 const PairValueRecord *record = hb_bsearch (buffer->info[pos].codepoint,
1218 &firstPairValueRecord,
1219 len,
1220 record_size);
1221 if (record)
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -04001222 {
Nico Weber96299d72021-10-12 10:42:43 -04001223 bool applied_first = valueFormats[0].apply_value (c, this, &record->values[0], buffer->cur_pos());
1224 bool applied_second = valueFormats[1].apply_value (c, this, &record->values[len1], buffer->pos[pos]);
1225 if (applied_first || applied_second)
Behdad Esfahbod39afe602019-12-10 12:04:44 -06001226 buffer->unsafe_to_break (buffer->idx, pos + 1);
1227 if (len2)
1228 pos++;
1229 buffer->idx = pos;
1230 return_trace (true);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -04001231 }
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001232 return_trace (false);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -04001233 }
1234
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001235 bool subset (hb_subset_context_t *c,
Garret Riegerbadb8e42021-04-23 16:32:56 -07001236 const ValueFormat valueFormats[2],
1237 const ValueFormat newFormats[2]) const
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001238 {
1239 TRACE_SUBSET (this);
1240 auto snap = c->serializer->snapshot ();
1241
1242 auto *out = c->serializer->start_embed (*this);
1243 if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
1244 out->len = 0;
1245
Garret Riegere5835052020-09-29 11:05:08 -07001246 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001247 const hb_map_t &glyph_map = *c->plan->glyph_map;
1248
Qunxin Liudefe9b62019-10-25 10:07:26 -07001249 unsigned len1 = valueFormats[0].get_len ();
1250 unsigned len2 = valueFormats[1].get_len ();
1251 unsigned record_size = HBUINT16::static_size + Value::static_size * (len1 + len2);
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001252
Garret Riegerbadb8e42021-04-23 16:32:56 -07001253 PairValueRecord::context_t context =
Michiharu Arizac828d7b2020-01-15 15:32:44 -08001254 {
1255 this,
Michiharu Arizac828d7b2020-01-15 15:32:44 -08001256 valueFormats,
Garret Riegerbadb8e42021-04-23 16:32:56 -07001257 newFormats,
Michiharu Arizac828d7b2020-01-15 15:32:44 -08001258 len1,
Qunxin Liud7c012a2020-02-26 13:11:42 -08001259 &glyph_map,
1260 c->plan->layout_variation_idx_map
Michiharu Arizac828d7b2020-01-15 15:32:44 -08001261 };
1262
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001263 const PairValueRecord *record = &firstPairValueRecord;
1264 unsigned count = len, num = 0;
1265 for (unsigned i = 0; i < count; i++)
1266 {
ariza1ab39242020-01-22 11:20:56 -08001267 if (glyphset.has (record->secondGlyph)
Garret Riegerbadb8e42021-04-23 16:32:56 -07001268 && record->subset (c, &context)) num++;
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001269 record = &StructAtOffset<const PairValueRecord> (record, record_size);
1270 }
1271
1272 out->len = num;
1273 if (!num) c->serializer->revert (snap);
1274 return_trace (num);
1275 }
1276
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -07001277 struct sanitize_closure_t
1278 {
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001279 const ValueFormat *valueFormats;
Behdad Esfahbodfb059082018-11-30 20:45:40 -05001280 unsigned int len1; /* valueFormats[0].get_len() */
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -04001281 unsigned int stride; /* 1 + len1 + len2 */
1282 };
1283
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301284 bool sanitize (hb_sanitize_context_t *c, const sanitize_closure_t *closure) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001285 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001286 TRACE_SANITIZE (this);
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001287 if (!(c->check_struct (this)
Behdad Esfahbode0144052018-11-12 14:23:31 -05001288 && c->check_range (&firstPairValueRecord,
1289 len,
1290 HBUINT16::static_size,
1291 closure->stride))) return_trace (false);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -04001292
1293 unsigned int count = len;
Behdad Esfahboddff2c452018-09-10 23:29:26 +02001294 const PairValueRecord *record = &firstPairValueRecord;
ariza1ab39242020-01-22 11:20:56 -08001295 return_trace (closure->valueFormats[0].sanitize_values_stride_unsafe (c, this, &record->values[0], count, closure->stride) &&
1296 closure->valueFormats[1].sanitize_values_stride_unsafe (c, this, &record->values[closure->len1], count, closure->stride));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001297 }
1298
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001299 protected:
Behdad Esfahboddff2c452018-09-10 23:29:26 +02001300 HBUINT16 len; /* Number of PairValueRecords */
1301 PairValueRecord firstPairValueRecord;
1302 /* Array of PairValueRecords--ordered
1303 * by GlyphID of the second glyph */
Behdad Esfahbod569da922010-05-10 16:38:32 -04001304 public:
Behdad Esfahboddff2c452018-09-10 23:29:26 +02001305 DEFINE_SIZE_MIN (2);
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -04001306};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001307
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001308struct PairPosFormat1
1309{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301310 bool intersects (const hb_set_t *glyphs) const
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -07001311 {
Behdad Esfahbodf5ef8a72019-03-29 21:57:17 -07001312 return
1313 + hb_zip (this+coverage, pairSet)
1314 | hb_filter (*glyphs, hb_first)
1315 | hb_map (hb_second)
Behdad Esfahbodad28f972021-03-31 12:49:14 -06001316 | hb_map ([glyphs, this] (const Offset16To<PairSet> &_)
Behdad Esfahbodf5ef8a72019-03-29 21:57:17 -07001317 { return (this+_).intersects (glyphs, valueFormat); })
1318 | hb_any
1319 ;
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -07001320 }
1321
Qunxin Liu0b39c482019-10-22 16:00:43 -07001322 void closure_lookups (hb_closure_lookups_context_t *c) const {}
Qunxin Liu8200e482020-02-26 13:11:42 -08001323 void collect_variation_indices (hb_collect_variation_indices_context_t *c) const
1324 {
1325 if ((!valueFormat[0].has_device ()) && (!valueFormat[1].has_device ())) return;
1326
1327 auto it =
1328 + hb_zip (this+coverage, pairSet)
1329 | hb_filter (c->glyph_set, hb_first)
1330 | hb_map (hb_second)
1331 ;
1332
1333 if (!it) return;
1334 + it
1335 | hb_map (hb_add (this))
1336 | hb_apply ([&] (const PairSet& _) { _.collect_variation_indices (c, valueFormat); })
1337 ;
1338 }
Qunxin Liu0b39c482019-10-22 16:00:43 -07001339
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301340 void collect_glyphs (hb_collect_glyphs_context_t *c) const
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001341 {
Behdad Esfahbod5cf53c02020-04-23 10:55:41 -07001342 if (unlikely (!(this+coverage).collect_coverage (c->input))) return;
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001343 unsigned int count = pairSet.len;
1344 for (unsigned int i = 0; i < count; i++)
Volker H. Simonisad273882016-12-14 18:56:17 +01001345 (this+pairSet[i]).collect_glyphs (c, valueFormat);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001346 }
1347
Ebrahim Byagowie4120082018-12-17 21:31:01 +03301348 const Coverage &get_coverage () const { return this+coverage; }
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001349
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301350 bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -04001351 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001352 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001353 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001354 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001355 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -04001356
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001357 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
Behdad Esfahbodb051be52015-01-29 13:40:39 +01001358 skippy_iter.reset (buffer->idx, 1);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001359 if (!skippy_iter.next ()) return_trace (false);
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -04001360
Volker H. Simonisad273882016-12-14 18:56:17 +01001361 return_trace ((this+pairSet[index]).apply (c, valueFormat, skippy_iter.idx));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001362 }
1363
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301364 bool subset (hb_subset_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -07001365 {
1366 TRACE_SUBSET (this);
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001367
Garret Riegere5835052020-09-29 11:05:08 -07001368 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001369 const hb_map_t &glyph_map = *c->plan->glyph_map;
1370
1371 auto *out = c->serializer->start_embed (*this);
1372 if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
1373 out->format = format;
1374 out->valueFormat[0] = valueFormat[0];
1375 out->valueFormat[1] = valueFormat[1];
Garret Rieger46d4a5e2021-07-29 15:07:13 -07001376 if (c->plan->flags & HB_SUBSET_FLAGS_NO_HINTING)
Garret Riegerd356d0f2021-04-23 17:41:09 -07001377 {
1378 hb_pair_t<unsigned, unsigned> newFormats = compute_effective_value_formats (glyphset);
1379 out->valueFormat[0] = newFormats.first;
1380 out->valueFormat[1] = newFormats.second;
1381 }
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001382
1383 hb_sorted_vector_t<hb_codepoint_t> new_coverage;
1384
1385 + hb_zip (this+coverage, pairSet)
1386 | hb_filter (glyphset, hb_first)
Behdad Esfahbodad28f972021-03-31 12:49:14 -06001387 | hb_filter ([this, c, out] (const Offset16To<PairSet>& _)
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001388 {
Garret Riegerc2cc5662021-09-22 14:15:55 -07001389 auto snap = c->serializer->snapshot ();
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001390 auto *o = out->pairSet.serialize_append (c->serializer);
1391 if (unlikely (!o)) return false;
Garret Riegerd356d0f2021-04-23 17:41:09 -07001392 bool ret = o->serialize_subset (c, _, this, valueFormat, out->valueFormat);
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001393 if (!ret)
1394 {
1395 out->pairSet.pop ();
1396 c->serializer->revert (snap);
1397 }
1398 return ret;
1399 },
1400 hb_second)
1401 | hb_map (hb_first)
1402 | hb_map (glyph_map)
1403 | hb_sink (new_coverage)
1404 ;
1405
Garret Rieger8443cc82021-06-11 13:40:39 -07001406 out->coverage.serialize_serialize (c->serializer, new_coverage.iter ());
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001407
1408 return_trace (bool (new_coverage));
Behdad Esfahbod339d3602018-09-03 17:33:34 -07001409 }
1410
Garret Riegerd356d0f2021-04-23 17:41:09 -07001411
1412 hb_pair_t<unsigned, unsigned> compute_effective_value_formats (const hb_set_t& glyphset) const
1413 {
1414 unsigned len1 = valueFormat[0].get_len ();
1415 unsigned len2 = valueFormat[1].get_len ();
1416 unsigned record_size = HBUINT16::static_size + Value::static_size * (len1 + len2);
1417
1418 unsigned format1 = 0;
1419 unsigned format2 = 0;
1420 for (const Offset16To<PairSet>& _ :
1421 + hb_zip (this+coverage, pairSet) | hb_filter (glyphset, hb_first) | hb_map (hb_second))
1422 {
1423 const PairSet& set = (this + _);
1424 const PairValueRecord *record = &set.firstPairValueRecord;
1425
1426 for (unsigned i = 0; i < set.len; i++)
1427 {
1428 if (record->intersects (glyphset))
1429 {
1430 format1 = format1 | valueFormat[0].get_effective_format (record->get_values_1 ());
1431 format2 = format2 | valueFormat[1].get_effective_format (record->get_values_2 (valueFormat[0]));
1432 }
1433 record = &StructAtOffset<const PairValueRecord> (record, record_size);
1434 }
1435 }
1436
1437 return hb_pair (format1, format2);
1438 }
1439
1440
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301441 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001442 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001443 TRACE_SANITIZE (this);
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -04001444
Behdad Esfahbodf9666492015-10-13 00:30:50 -04001445 if (!c->check_struct (this)) return_trace (false);
1446
Behdad Esfahbodfb059082018-11-30 20:45:40 -05001447 unsigned int len1 = valueFormat[0].get_len ();
1448 unsigned int len2 = valueFormat[1].get_len ();
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -07001449 PairSet::sanitize_closure_t closure =
1450 {
Volker H. Simonisad273882016-12-14 18:56:17 +01001451 valueFormat,
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -04001452 len1,
1453 1 + len1 + len2
1454 };
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -04001455
Behdad Esfahbodf9666492015-10-13 00:30:50 -04001456 return_trace (coverage.sanitize (c, this) && pairSet.sanitize (c, this, &closure));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001457 }
1458
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001459 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001460 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbodad28f972021-03-31 12:49:14 -06001461 Offset16To<Coverage>
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001462 coverage; /* Offset to Coverage table--from
1463 * beginning of subtable */
Behdad Esfahbodfb059082018-11-30 20:45:40 -05001464 ValueFormat valueFormat[2]; /* [0] Defines the types of data in
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001465 * ValueRecord1--for the first glyph
1466 * in the pair--may be zero (0) */
Behdad Esfahbodfb059082018-11-30 20:45:40 -05001467 /* [1] Defines the types of data in
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001468 * ValueRecord2--for the second glyph
1469 * in the pair--may be zero (0) */
Behdad Esfahbod6c4e0492021-03-31 15:31:32 -06001470 Array16OfOffset16To<PairSet>
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001471 pairSet; /* Array of PairSet tables
1472 * ordered by Coverage Index */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001473 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001474 DEFINE_SIZE_ARRAY (10, pairSet);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001475};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001476
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001477struct PairPosFormat2
1478{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301479 bool intersects (const hb_set_t *glyphs) const
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -07001480 {
1481 return (this+coverage).intersects (glyphs) &&
1482 (this+classDef2).intersects (glyphs);
1483 }
1484
Qunxin Liu0b39c482019-10-22 16:00:43 -07001485 void closure_lookups (hb_closure_lookups_context_t *c) const {}
Qunxin Liu8200e482020-02-26 13:11:42 -08001486 void collect_variation_indices (hb_collect_variation_indices_context_t *c) const
1487 {
Qunxin Liu84dbfac2021-04-15 16:52:11 -07001488 if (!intersects (c->glyph_set)) return;
Qunxin Liu8200e482020-02-26 13:11:42 -08001489 if ((!valueFormat1.has_device ()) && (!valueFormat2.has_device ())) return;
1490
Qunxin Liu84dbfac2021-04-15 16:52:11 -07001491 hb_set_t klass1_glyphs, klass2_glyphs;
1492 if (!(this+classDef1).collect_coverage (&klass1_glyphs)) return;
1493 if (!(this+classDef2).collect_coverage (&klass2_glyphs)) return;
1494
Qunxin Liu8200e482020-02-26 13:11:42 -08001495 hb_set_t class1_set, class2_set;
Garret Riegerbb54e102021-03-16 13:19:26 -07001496 for (const unsigned cp : + c->glyph_set->iter () | hb_filter (this + coverage))
Qunxin Liu8200e482020-02-26 13:11:42 -08001497 {
Qunxin Liu84dbfac2021-04-15 16:52:11 -07001498 if (!klass1_glyphs.has (cp)) class1_set.add (0);
1499 else
1500 {
1501 unsigned klass1 = (this+classDef1).get (cp);
1502 class1_set.add (klass1);
1503 }
1504 }
1505
1506 class2_set.add (0);
1507 for (const unsigned cp : + c->glyph_set->iter () | hb_filter (klass2_glyphs))
1508 {
Qunxin Liu8200e482020-02-26 13:11:42 -08001509 unsigned klass2 = (this+classDef2).get (cp);
Qunxin Liu8200e482020-02-26 13:11:42 -08001510 class2_set.add (klass2);
1511 }
1512
Garret Rieger190b7a92020-10-08 14:44:54 -07001513 if (class1_set.is_empty ()
1514 || class2_set.is_empty ()
1515 || (class2_set.get_population() == 1 && class2_set.has(0)))
1516 return;
Ebrahim Byagowid0e2add2020-07-18 22:14:52 +04301517
Qunxin Liu8200e482020-02-26 13:11:42 -08001518 unsigned len1 = valueFormat1.get_len ();
1519 unsigned len2 = valueFormat2.get_len ();
1520 const hb_array_t<const Value> values_array = values.as_array ((unsigned)class1Count * (unsigned) class2Count * (len1 + len2));
1521 for (const unsigned class1_idx : class1_set.iter ())
1522 {
1523 for (const unsigned class2_idx : class2_set.iter ())
1524 {
Ebrahim Byagowi5a7cc7f2020-07-29 08:33:32 +04301525 unsigned start_offset = (class1_idx * (unsigned) class2Count + class2_idx) * (len1 + len2);
1526 if (valueFormat1.has_device ())
1527 valueFormat1.collect_variation_indices (c, this, values_array.sub_array (start_offset, len1));
Ebrahim Byagowid0e2add2020-07-18 22:14:52 +04301528
Ebrahim Byagowi5a7cc7f2020-07-29 08:33:32 +04301529 if (valueFormat2.has_device ())
1530 valueFormat2.collect_variation_indices (c, this, values_array.sub_array (start_offset+len1, len2));
Qunxin Liu8200e482020-02-26 13:11:42 -08001531 }
1532 }
1533 }
Qunxin Liu0b39c482019-10-22 16:00:43 -07001534
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301535 void collect_glyphs (hb_collect_glyphs_context_t *c) const
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001536 {
Behdad Esfahbod5cf53c02020-04-23 10:55:41 -07001537 if (unlikely (!(this+coverage).collect_coverage (c->input))) return;
1538 if (unlikely (!(this+classDef2).collect_coverage (c->input))) return;
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001539 }
1540
Ebrahim Byagowie4120082018-12-17 21:31:01 +03301541 const Coverage &get_coverage () const { return this+coverage; }
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001542
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301543 bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbod70632ad2009-05-19 22:30:09 -04001544 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001545 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001546 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001547 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001548 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -04001549
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001550 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
Behdad Esfahbodb051be52015-01-29 13:40:39 +01001551 skippy_iter.reset (buffer->idx, 1);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001552 if (!skippy_iter.next ()) return_trace (false);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -04001553
1554 unsigned int len1 = valueFormat1.get_len ();
1555 unsigned int len2 = valueFormat2.get_len ();
1556 unsigned int record_len = len1 + len2;
1557
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001558 unsigned int klass1 = (this+classDef1).get_class (buffer->cur().codepoint);
1559 unsigned int klass2 = (this+classDef2).get_class (buffer->info[skippy_iter.idx].codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001560 if (unlikely (klass1 >= class1Count || klass2 >= class2Count)) return_trace (false);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -04001561
Behdad Esfahbod4b8487d2010-03-16 03:46:17 -04001562 const Value *v = &values[record_len * (klass1 * class2Count + klass2)];
Nico Weber96299d72021-10-12 10:42:43 -04001563 bool applied_first = valueFormat1.apply_value (c, this, v, buffer->cur_pos());
1564 bool applied_second = valueFormat2.apply_value (c, this, v + len1, buffer->pos[skippy_iter.idx]);
1565 if (applied_first || applied_second)
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -05001566 buffer->unsafe_to_break (buffer->idx, skippy_iter.idx + 1);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -04001567
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001568 buffer->idx = skippy_iter.idx;
Behdad Esfahbod70632ad2009-05-19 22:30:09 -04001569 if (len2)
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001570 buffer->idx++;
Behdad Esfahbod70632ad2009-05-19 22:30:09 -04001571
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001572 return_trace (true);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001573 }
1574
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301575 bool subset (hb_subset_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -07001576 {
1577 TRACE_SUBSET (this);
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001578 auto *out = c->serializer->start_embed (*this);
1579 if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
1580 out->format = format;
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001581
1582 hb_map_t klass1_map;
Qunxin Liu84dbfac2021-04-15 16:52:11 -07001583 out->classDef1.serialize_subset (c, classDef1, this, &klass1_map, true, true, &(this + coverage));
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001584 out->class1Count = klass1_map.get_population ();
1585
1586 hb_map_t klass2_map;
Qunxin Liu84dbfac2021-04-15 16:52:11 -07001587 out->classDef2.serialize_subset (c, classDef2, this, &klass2_map, true, false);
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001588 out->class2Count = klass2_map.get_population ();
1589
Michiharu Arizac828d7b2020-01-15 15:32:44 -08001590 unsigned len1 = valueFormat1.get_len ();
1591 unsigned len2 = valueFormat2.get_len ();
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001592
Garret Riegerf9fbe482021-04-23 17:06:06 -07001593 hb_pair_t<unsigned, unsigned> newFormats = hb_pair (valueFormat1, valueFormat2);
Garret Rieger46d4a5e2021-07-29 15:07:13 -07001594 if (c->plan->flags & HB_SUBSET_FLAGS_NO_HINTING)
Garret Riegerf9fbe482021-04-23 17:06:06 -07001595 newFormats = compute_effective_value_formats (klass1_map, klass2_map);
1596
1597 out->valueFormat1 = newFormats.first;
1598 out->valueFormat2 = newFormats.second;
1599
1600 for (unsigned class1_idx : + hb_range ((unsigned) class1Count) | hb_filter (klass1_map))
1601 {
1602 for (unsigned class2_idx : + hb_range ((unsigned) class2Count) | hb_filter (klass2_map))
1603 {
1604 unsigned idx = (class1_idx * (unsigned) class2Count + class2_idx) * (len1 + len2);
1605 valueFormat1.copy_values (c->serializer, newFormats.first, this, &values[idx], c->plan->layout_variation_idx_map);
1606 valueFormat2.copy_values (c->serializer, newFormats.second, this, &values[idx + len1], c->plan->layout_variation_idx_map);
1607 }
1608 }
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001609
Garret Riegere5835052020-09-29 11:05:08 -07001610 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001611 const hb_map_t &glyph_map = *c->plan->glyph_map;
1612
1613 auto it =
1614 + hb_iter (this+coverage)
1615 | hb_filter (glyphset)
1616 | hb_map_retains_sorting (glyph_map)
1617 ;
1618
Garret Rieger8443cc82021-06-11 13:40:39 -07001619 out->coverage.serialize_serialize (c->serializer, it);
Qunxin Liu1f0a9d92019-09-17 11:10:08 -07001620 return_trace (out->class1Count && out->class2Count && bool (it));
Behdad Esfahbod339d3602018-09-03 17:33:34 -07001621 }
1622
Garret Riegerf9fbe482021-04-23 17:06:06 -07001623
1624 hb_pair_t<unsigned, unsigned> compute_effective_value_formats (const hb_map_t& klass1_map,
1625 const hb_map_t& klass2_map) const
1626 {
1627 unsigned len1 = valueFormat1.get_len ();
1628 unsigned len2 = valueFormat2.get_len ();
1629
1630 unsigned format1 = 0;
1631 unsigned format2 = 0;
1632
1633 for (unsigned class1_idx : + hb_range ((unsigned) class1Count) | hb_filter (klass1_map))
1634 {
1635 for (unsigned class2_idx : + hb_range ((unsigned) class2Count) | hb_filter (klass2_map))
1636 {
1637 unsigned idx = (class1_idx * (unsigned) class2Count + class2_idx) * (len1 + len2);
1638 format1 = format1 | valueFormat1.get_effective_format (&values[idx]);
1639 format2 = format2 | valueFormat2.get_effective_format (&values[idx + len1]);
1640 }
1641 }
1642
1643 return hb_pair (format1, format2);
1644 }
1645
1646
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301647 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001648 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001649 TRACE_SANITIZE (this);
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001650 if (!(c->check_struct (this)
1651 && coverage.sanitize (c, this)
1652 && classDef1.sanitize (c, this)
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001653 && classDef2.sanitize (c, this))) return_trace (false);
Behdad Esfahbod815a73e2009-08-14 17:31:16 -04001654
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -04001655 unsigned int len1 = valueFormat1.get_len ();
1656 unsigned int len2 = valueFormat2.get_len ();
1657 unsigned int stride = len1 + len2;
Behdad Esfahbod4b8487d2010-03-16 03:46:17 -04001658 unsigned int record_size = valueFormat1.get_size () + valueFormat2.get_size ();
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -04001659 unsigned int count = (unsigned int) class1Count * (unsigned int) class2Count;
Behdad Esfahbode0144052018-11-12 14:23:31 -05001660 return_trace (c->check_range ((const void *) values,
1661 count,
1662 record_size) &&
Behdad Esfahbodfb059082018-11-30 20:45:40 -05001663 valueFormat1.sanitize_values_stride_unsafe (c, this, &values[0], count, stride) &&
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001664 valueFormat2.sanitize_values_stride_unsafe (c, this, &values[len1], count, stride));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001665 }
Behdad Esfahbod70632ad2009-05-19 22:30:09 -04001666
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001667 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001668 HBUINT16 format; /* Format identifier--format = 2 */
Behdad Esfahbodad28f972021-03-31 12:49:14 -06001669 Offset16To<Coverage>
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001670 coverage; /* Offset to Coverage table--from
1671 * beginning of subtable */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -04001672 ValueFormat valueFormat1; /* ValueRecord definition--for the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001673 * first glyph of the pair--may be zero
1674 * (0) */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -04001675 ValueFormat valueFormat2; /* ValueRecord definition--for the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001676 * second glyph of the pair--may be
1677 * zero (0) */
Behdad Esfahbodad28f972021-03-31 12:49:14 -06001678 Offset16To<ClassDef>
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001679 classDef1; /* Offset to ClassDef table--from
1680 * beginning of PairPos subtable--for
1681 * the first glyph of the pair */
Behdad Esfahbodad28f972021-03-31 12:49:14 -06001682 Offset16To<ClassDef>
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001683 classDef2; /* Offset to ClassDef table--from
1684 * beginning of PairPos subtable--for
1685 * the second glyph of the pair */
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001686 HBUINT16 class1Count; /* Number of classes in ClassDef1
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001687 * table--includes Class0 */
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001688 HBUINT16 class2Count; /* Number of classes in ClassDef2
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001689 * table--includes Class0 */
1690 ValueRecord values; /* Matrix of value pairs:
1691 * class1-major, class2-minor,
1692 * Each entry has value1 and value2 */
Behdad Esfahbod569da922010-05-10 16:38:32 -04001693 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001694 DEFINE_SIZE_ARRAY (16, values);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001695};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001696
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001697struct PairPos
1698{
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07001699 template <typename context_t, typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -07001700 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001701 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08001702 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04001703 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001704 switch (u.format) {
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -06001705 case 1: return_trace (c->dispatch (u.format1, std::forward<Ts> (ds)...));
1706 case 2: return_trace (c->dispatch (u.format2, std::forward<Ts> (ds)...));
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001707 default:return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001708 }
1709 }
1710
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001711 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001712 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001713 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001714 PairPosFormat1 format1;
1715 PairPosFormat2 format2;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001716 } u;
1717};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001718
1719
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001720struct EntryExitRecord
1721{
Behdad Esfahbod569da922010-05-10 16:38:32 -04001722 friend struct CursivePosFormat1;
1723
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301724 bool sanitize (hb_sanitize_context_t *c, const void *base) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001725 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001726 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001727 return_trace (entryAnchor.sanitize (c, base) && exitAnchor.sanitize (c, base));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001728 }
1729
Qunxin Liu8200e482020-02-26 13:11:42 -08001730 void collect_variation_indices (hb_collect_variation_indices_context_t *c,
1731 const void *src_base) const
1732 {
1733 (src_base+entryAnchor).collect_variation_indices (c);
1734 (src_base+exitAnchor).collect_variation_indices (c);
1735 }
1736
Garret Rieger6f98a8e2021-04-02 14:37:10 -07001737 EntryExitRecord* subset (hb_subset_context_t *c,
1738 const void *src_base) const
Qunxin Liub0c3eb02019-09-18 16:11:44 -07001739 {
1740 TRACE_SERIALIZE (this);
Garret Rieger6f98a8e2021-04-02 14:37:10 -07001741 auto *out = c->serializer->embed (this);
Qunxin Liub0c3eb02019-09-18 16:11:44 -07001742 if (unlikely (!out)) return_trace (nullptr);
1743
Garret Rieger6f98a8e2021-04-02 14:37:10 -07001744 out->entryAnchor.serialize_subset (c, entryAnchor, src_base);
1745 out->exitAnchor.serialize_subset (c, exitAnchor, src_base);
Qunxin Liub0c3eb02019-09-18 16:11:44 -07001746 return_trace (out);
1747 }
1748
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001749 protected:
Behdad Esfahbodad28f972021-03-31 12:49:14 -06001750 Offset16To<Anchor>
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001751 entryAnchor; /* Offset to EntryAnchor table--from
1752 * beginning of CursivePos
1753 * subtable--may be NULL */
Behdad Esfahbodad28f972021-03-31 12:49:14 -06001754 Offset16To<Anchor>
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001755 exitAnchor; /* Offset to ExitAnchor table--from
1756 * beginning of CursivePos
1757 * subtable--may be NULL */
Behdad Esfahbod569da922010-05-10 16:38:32 -04001758 public:
1759 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001760};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001761
Behdad Esfahbod65785752015-08-25 20:24:59 +01001762static void
1763reverse_cursive_minor_offset (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction, unsigned int new_parent);
1764
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001765struct CursivePosFormat1
1766{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301767 bool intersects (const hb_set_t *glyphs) const
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -07001768 { return (this+coverage).intersects (glyphs); }
1769
Qunxin Liu0b39c482019-10-22 16:00:43 -07001770 void closure_lookups (hb_closure_lookups_context_t *c) const {}
1771
Qunxin Liu8200e482020-02-26 13:11:42 -08001772 void collect_variation_indices (hb_collect_variation_indices_context_t *c) const
1773 {
1774 + hb_zip (this+coverage, entryExitRecord)
1775 | hb_filter (c->glyph_set, hb_first)
1776 | hb_map (hb_second)
1777 | hb_apply ([&] (const EntryExitRecord& record) { record.collect_variation_indices (c, this); })
1778 ;
1779 }
1780
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301781 void collect_glyphs (hb_collect_glyphs_context_t *c) const
Behdad Esfahbod5cf53c02020-04-23 10:55:41 -07001782 { if (unlikely (!(this+coverage).collect_coverage (c->input))) return; }
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001783
Ebrahim Byagowie4120082018-12-17 21:31:01 +03301784 const Coverage &get_coverage () const { return this+coverage; }
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001785
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301786 bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -04001787 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001788 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001789 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbod0f7e6b22009-05-20 04:16:35 -04001790
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001791 const EntryExitRecord &this_record = entryExitRecord[(this+coverage).get_coverage (buffer->cur().codepoint)];
Behdad Esfahbodbf8469b2018-10-11 20:45:00 -04001792 if (!this_record.entryAnchor) return_trace (false);
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -04001793
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001794 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
Behdad Esfahbodb051be52015-01-29 13:40:39 +01001795 skippy_iter.reset (buffer->idx, 1);
Behdad Esfahbodbf8469b2018-10-11 20:45:00 -04001796 if (!skippy_iter.prev ()) return_trace (false);
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -04001797
Behdad Esfahbodbf8469b2018-10-11 20:45:00 -04001798 const EntryExitRecord &prev_record = entryExitRecord[(this+coverage).get_coverage (buffer->info[skippy_iter.idx].codepoint)];
1799 if (!prev_record.exitAnchor) return_trace (false);
Behdad Esfahbodaf5d02a2010-10-27 11:54:26 -04001800
Behdad Esfahbodbf8469b2018-10-11 20:45:00 -04001801 unsigned int i = skippy_iter.idx;
1802 unsigned int j = buffer->idx;
Behdad Esfahbodaf5d02a2010-10-27 11:54:26 -04001803
Behdad Esfahbod882ebda2017-08-10 20:23:17 -07001804 buffer->unsafe_to_break (i, j);
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +01001805 float entry_x, entry_y, exit_x, exit_y;
Behdad Esfahbodbf8469b2018-10-11 20:45:00 -04001806 (this+prev_record.exitAnchor).get_anchor (c, buffer->info[i].codepoint, &exit_x, &exit_y);
1807 (this+this_record.entryAnchor).get_anchor (c, buffer->info[j].codepoint, &entry_x, &entry_y);
Behdad Esfahbodcc83ae12009-05-27 00:17:37 -04001808
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001809 hb_glyph_position_t *pos = buffer->pos;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001810
1811 hb_position_t d;
1812 /* Main-direction adjustment */
1813 switch (c->direction) {
1814 case HB_DIRECTION_LTR:
Behdad Esfahboddfc57802019-05-07 23:26:09 -07001815 pos[i].x_advance = roundf (exit_x) + pos[i].x_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001816
Behdad Esfahboddfc57802019-05-07 23:26:09 -07001817 d = roundf (entry_x) + pos[j].x_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001818 pos[j].x_advance -= d;
1819 pos[j].x_offset -= d;
1820 break;
1821 case HB_DIRECTION_RTL:
Behdad Esfahboddfc57802019-05-07 23:26:09 -07001822 d = roundf (exit_x) + pos[i].x_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001823 pos[i].x_advance -= d;
1824 pos[i].x_offset -= d;
1825
Behdad Esfahboddfc57802019-05-07 23:26:09 -07001826 pos[j].x_advance = roundf (entry_x) + pos[j].x_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001827 break;
1828 case HB_DIRECTION_TTB:
Behdad Esfahboddfc57802019-05-07 23:26:09 -07001829 pos[i].y_advance = roundf (exit_y) + pos[i].y_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001830
Behdad Esfahboddfc57802019-05-07 23:26:09 -07001831 d = roundf (entry_y) + pos[j].y_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001832 pos[j].y_advance -= d;
1833 pos[j].y_offset -= d;
1834 break;
1835 case HB_DIRECTION_BTT:
Behdad Esfahboddfc57802019-05-07 23:26:09 -07001836 d = roundf (exit_y) + pos[i].y_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001837 pos[i].y_advance -= d;
1838 pos[i].y_offset -= d;
1839
Behdad Esfahboddfc57802019-05-07 23:26:09 -07001840 pos[j].y_advance = roundf (entry_y);
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001841 break;
1842 case HB_DIRECTION_INVALID:
1843 default:
1844 break;
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -04001845 }
1846
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001847 /* Cross-direction adjustment */
Behdad Esfahbod58f2a732015-08-25 18:55:34 +01001848
1849 /* We attach child to parent (think graph theory and rooted trees whereas
1850 * the root stays on baseline and each node aligns itself against its
1851 * parent.
1852 *
1853 * Optimize things for the case of RightToLeft, as that's most common in
Behdad Esfahbodbf8469b2018-10-11 20:45:00 -04001854 * Arabic. */
Behdad Esfahbod58f2a732015-08-25 18:55:34 +01001855 unsigned int child = i;
1856 unsigned int parent = j;
1857 hb_position_t x_offset = entry_x - exit_x;
1858 hb_position_t y_offset = entry_y - exit_y;
1859 if (!(c->lookup_props & LookupFlag::RightToLeft))
1860 {
1861 unsigned int k = child;
1862 child = parent;
1863 parent = k;
1864 x_offset = -x_offset;
1865 y_offset = -y_offset;
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -04001866 }
1867
Behdad Esfahbod65785752015-08-25 20:24:59 +01001868 /* If child was already connected to someone else, walk through its old
1869 * chain and reverse the link direction, such that the whole tree of its
1870 * previous connection now attaches to new parent. Watch out for case
1871 * where new parent is on the path from old chain...
1872 */
1873 reverse_cursive_minor_offset (pos, child, c->direction, parent);
1874
Behdad Esfahbod686567b2016-02-11 15:25:28 +07001875 pos[child].attach_type() = ATTACH_TYPE_CURSIVE;
1876 pos[child].attach_chain() = (int) parent - (int) child;
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07001877 buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
Behdad Esfahbod58f2a732015-08-25 18:55:34 +01001878 if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction)))
1879 pos[child].y_offset = y_offset;
1880 else
1881 pos[child].x_offset = x_offset;
1882
Behdad Esfahbodbb48bf52021-07-08 10:53:45 -06001883 /* If parent was attached to child, separate them.
Behdad Esfahbod9ef9ac32020-06-17 17:41:15 -07001884 * https://github.com/harfbuzz/harfbuzz/issues/2469
1885 */
1886 if (unlikely (pos[parent].attach_chain() == -pos[child].attach_chain()))
1887 pos[parent].attach_chain() = 0;
1888
Behdad Esfahbodbf8469b2018-10-11 20:45:00 -04001889 buffer->idx++;
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001890 return_trace (true);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001891 }
1892
Qunxin Liub0c3eb02019-09-18 16:11:44 -07001893 template <typename Iterator,
1894 hb_requires (hb_is_iterator (Iterator))>
Garret Rieger6f98a8e2021-04-02 14:37:10 -07001895 void serialize (hb_subset_context_t *c,
Qunxin Liud7c012a2020-02-26 13:11:42 -08001896 Iterator it,
Garret Rieger6f98a8e2021-04-02 14:37:10 -07001897 const void *src_base)
Qunxin Liub0c3eb02019-09-18 16:11:44 -07001898 {
Garret Rieger6f98a8e2021-04-02 14:37:10 -07001899 if (unlikely (!c->serializer->extend_min ((*this)))) return;
Qunxin Liub0c3eb02019-09-18 16:11:44 -07001900 this->format = 1;
1901 this->entryExitRecord.len = it.len ();
1902
1903 for (const EntryExitRecord& entry_record : + it
1904 | hb_map (hb_second))
Garret Rieger6f98a8e2021-04-02 14:37:10 -07001905 entry_record.subset (c, src_base);
Qunxin Liub0c3eb02019-09-18 16:11:44 -07001906
1907 auto glyphs =
1908 + it
1909 | hb_map_retains_sorting (hb_first)
1910 ;
1911
Garret Rieger8443cc82021-06-11 13:40:39 -07001912 coverage.serialize_serialize (c->serializer, glyphs);
Qunxin Liub0c3eb02019-09-18 16:11:44 -07001913 }
1914
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301915 bool subset (hb_subset_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -07001916 {
1917 TRACE_SUBSET (this);
Garret Riegere5835052020-09-29 11:05:08 -07001918 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
Qunxin Liub0c3eb02019-09-18 16:11:44 -07001919 const hb_map_t &glyph_map = *c->plan->glyph_map;
1920
1921 auto *out = c->serializer->start_embed (*this);
1922 if (unlikely (!out)) return_trace (false);
1923
1924 auto it =
1925 + hb_zip (this+coverage, entryExitRecord)
1926 | hb_filter (glyphset, hb_first)
1927 | hb_map_retains_sorting ([&] (hb_pair_t<hb_codepoint_t, const EntryExitRecord&> p) -> hb_pair_t<hb_codepoint_t, const EntryExitRecord&>
Ebrahim Byagowi05584132019-10-01 13:49:55 +03301928 { return hb_pair (glyph_map[p.first], p.second);})
Qunxin Liub0c3eb02019-09-18 16:11:44 -07001929 ;
1930
1931 bool ret = bool (it);
Garret Rieger6f98a8e2021-04-02 14:37:10 -07001932 out->serialize (c, it, this);
Qunxin Liub0c3eb02019-09-18 16:11:44 -07001933 return_trace (ret);
Behdad Esfahbod339d3602018-09-03 17:33:34 -07001934 }
1935
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301936 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001937 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001938 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001939 return_trace (coverage.sanitize (c, this) && entryExitRecord.sanitize (c, this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001940 }
1941
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001942 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001943 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbodad28f972021-03-31 12:49:14 -06001944 Offset16To<Coverage>
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001945 coverage; /* Offset to Coverage table--from
1946 * beginning of subtable */
Behdad Esfahbod5639e252021-03-31 16:04:43 -06001947 Array16Of<EntryExitRecord>
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001948 entryExitRecord; /* Array of EntryExit records--in
1949 * Coverage Index order */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001950 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001951 DEFINE_SIZE_ARRAY (6, entryExitRecord);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001952};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001953
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001954struct CursivePos
1955{
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07001956 template <typename context_t, typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -07001957 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001958 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08001959 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04001960 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001961 switch (u.format) {
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -06001962 case 1: return_trace (c->dispatch (u.format1, std::forward<Ts> (ds)...));
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001963 default:return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001964 }
1965 }
1966
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001967 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001968 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001969 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001970 CursivePosFormat1 format1;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001971 } u;
1972};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001973
1974
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -04001975typedef AnchorMatrix BaseArray; /* base-major--
1976 * in order of BaseCoverage Index--,
1977 * mark-minor--
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001978 * ordered by class--zero-based. */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001979
Qunxin Liu82afc752020-02-04 13:24:37 -08001980static void Markclass_closure_and_remap_indexes (const Coverage &mark_coverage,
1981 const MarkArray &mark_array,
1982 const hb_set_t &glyphset,
1983 hb_map_t* klass_mapping /* INOUT */)
1984{
1985 hb_set_t orig_classes;
1986
1987 + hb_zip (mark_coverage, mark_array)
1988 | hb_filter (glyphset, hb_first)
1989 | hb_map (hb_second)
1990 | hb_map (&MarkRecord::get_class)
1991 | hb_sink (orig_classes)
1992 ;
1993
1994 unsigned idx = 0;
1995 for (auto klass : orig_classes.iter ())
1996 {
1997 if (klass_mapping->has (klass)) continue;
1998 klass_mapping->set (klass, idx);
1999 idx++;
2000 }
2001}
2002
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04002003struct MarkBasePosFormat1
2004{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302005 bool intersects (const hb_set_t *glyphs) const
Ebrahim Byagowid0e2add2020-07-18 22:14:52 +04302006 {
2007 return (this+markCoverage).intersects (glyphs) &&
2008 (this+baseCoverage).intersects (glyphs);
2009 }
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -07002010
Qunxin Liu0b39c482019-10-22 16:00:43 -07002011 void closure_lookups (hb_closure_lookups_context_t *c) const {}
2012
Qunxin Liu8200e482020-02-26 13:11:42 -08002013 void collect_variation_indices (hb_collect_variation_indices_context_t *c) const
2014 {
2015 + hb_zip (this+markCoverage, this+markArray)
2016 | hb_filter (c->glyph_set, hb_first)
2017 | hb_map (hb_second)
2018 | hb_apply ([&] (const MarkRecord& record) { record.collect_variation_indices (c, &(this+markArray)); })
2019 ;
2020
2021 hb_map_t klass_mapping;
2022 Markclass_closure_and_remap_indexes (this+markCoverage, this+markArray, *c->glyph_set, &klass_mapping);
2023
2024 unsigned basecount = (this+baseArray).rows;
2025 auto base_iter =
2026 + hb_zip (this+baseCoverage, hb_range (basecount))
2027 | hb_filter (c->glyph_set, hb_first)
2028 | hb_map (hb_second)
2029 ;
2030
2031 hb_sorted_vector_t<unsigned> base_indexes;
2032 for (const unsigned row : base_iter)
2033 {
2034 + hb_range ((unsigned) classCount)
2035 | hb_filter (klass_mapping)
2036 | hb_map ([&] (const unsigned col) { return row * (unsigned) classCount + col; })
2037 | hb_sink (base_indexes)
2038 ;
2039 }
2040 (this+baseArray).collect_variation_indices (c, base_indexes.iter ());
2041 }
2042
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302043 void collect_glyphs (hb_collect_glyphs_context_t *c) const
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05002044 {
Behdad Esfahbod5cf53c02020-04-23 10:55:41 -07002045 if (unlikely (!(this+markCoverage).collect_coverage (c->input))) return;
2046 if (unlikely (!(this+baseCoverage).collect_coverage (c->input))) return;
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05002047 }
2048
Ebrahim Byagowie4120082018-12-17 21:31:01 +03302049 const Coverage &get_coverage () const { return this+markCoverage; }
Behdad Esfahbod0b994292012-07-28 17:31:01 -04002050
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302051 bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04002052 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002053 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02002054 hb_buffer_t *buffer = c->buffer;
2055 unsigned int mark_index = (this+markCoverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002056 if (likely (mark_index == NOT_COVERED)) return_trace (false);
Behdad Esfahbod357ccde2009-05-21 06:32:01 -04002057
Behdad Esfahbod6ab92022016-02-11 16:57:52 +07002058 /* Now we search backwards for a non-mark glyph */
Behdad Esfahbodfd034492018-01-17 16:46:51 -08002059 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
Behdad Esfahbodb051be52015-01-29 13:40:39 +01002060 skippy_iter.reset (buffer->idx, 1);
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -05002061 skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
Behdad Esfahbod7b84c532012-06-08 22:04:23 -04002062 do {
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002063 if (!skippy_iter.prev ()) return_trace (false);
Behdad Esfahbod9b5af3e2018-02-10 21:50:26 -06002064 /* We only want to attach to the first of a MultipleSubst sequence.
2065 * https://github.com/harfbuzz/harfbuzz/issues/740
Behdad Esfahbodd3c09802018-06-30 15:59:25 +04302066 * Reject others...
2067 * ...but stop if we find a mark in the MultipleSubst sequence:
2068 * https://github.com/harfbuzz/harfbuzz/issues/1020 */
Behdad Esfahbod5d98de12017-10-03 13:46:25 +02002069 if (!_hb_glyph_info_multiplied (&buffer->info[skippy_iter.idx]) ||
Behdad Esfahbod9b5af3e2018-02-10 21:50:26 -06002070 0 == _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]) ||
2071 (skippy_iter.idx == 0 ||
Behdad Esfahbodd3c09802018-06-30 15:59:25 +04302072 _hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx - 1]) ||
Behdad Esfahbod9b5af3e2018-02-10 21:50:26 -06002073 _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx]) !=
Behdad Esfahbod1c50a882018-02-18 15:57:09 -08002074 _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx - 1]) ||
2075 _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]) !=
2076 _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx - 1]) + 1
2077 ))
Behdad Esfahbod5d98de12017-10-03 13:46:25 +02002078 break;
Behdad Esfahbod7b84c532012-06-08 22:04:23 -04002079 skippy_iter.reject ();
Bruce Mitchener8d1e4792018-10-18 22:18:42 +07002080 } while (true);
Behdad Esfahbod0532ed12009-08-12 15:40:04 -04002081
Behdad Esfahbod101303d2013-10-18 00:42:39 +02002082 /* Checking that matched glyph is actually a base glyph by GDEF is too strong; disabled */
Behdad Esfahbod6ab92022016-02-11 16:57:52 +07002083 //if (!_hb_glyph_info_is_base_glyph (&buffer->info[skippy_iter.idx])) { return_trace (false); }
Behdad Esfahbod357ccde2009-05-21 06:32:01 -04002084
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02002085 unsigned int base_index = (this+baseCoverage).get_coverage (buffer->info[skippy_iter.idx].codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002086 if (base_index == NOT_COVERED) return_trace (false);
Behdad Esfahbod357ccde2009-05-21 06:32:01 -04002087
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002088 return_trace ((this+markArray).apply (c, mark_index, base_index, this+baseArray, classCount, skippy_iter.idx));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002089 }
2090
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302091 bool subset (hb_subset_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -07002092 {
2093 TRACE_SUBSET (this);
Garret Riegere5835052020-09-29 11:05:08 -07002094 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
Qunxin Liu82afc752020-02-04 13:24:37 -08002095 const hb_map_t &glyph_map = *c->plan->glyph_map;
2096
2097 auto *out = c->serializer->start_embed (*this);
2098 if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
2099 out->format = format;
2100
2101 hb_map_t klass_mapping;
2102 Markclass_closure_and_remap_indexes (this+markCoverage, this+markArray, glyphset, &klass_mapping);
Ebrahim Byagowi07acd1a2020-03-08 23:39:24 +03302103
Qunxin Liu82afc752020-02-04 13:24:37 -08002104 if (!klass_mapping.get_population ()) return_trace (false);
2105 out->classCount = klass_mapping.get_population ();
2106
2107 auto mark_iter =
2108 + hb_zip (this+markCoverage, this+markArray)
2109 | hb_filter (glyphset, hb_first)
2110 ;
2111
2112 hb_sorted_vector_t<hb_codepoint_t> new_coverage;
2113 + mark_iter
2114 | hb_map (hb_first)
2115 | hb_map (glyph_map)
2116 | hb_sink (new_coverage)
2117 ;
2118
Garret Rieger8443cc82021-06-11 13:40:39 -07002119 if (!out->markCoverage.serialize_serialize (c->serializer, new_coverage.iter ()))
Qunxin Liu82afc752020-02-04 13:24:37 -08002120 return_trace (false);
2121
Garret Rieger6f98a8e2021-04-02 14:37:10 -07002122 out->markArray.serialize_subset (c, markArray, this,
2123 (this+markCoverage).iter (),
2124 &klass_mapping);
Qunxin Liu82afc752020-02-04 13:24:37 -08002125
2126 unsigned basecount = (this+baseArray).rows;
2127 auto base_iter =
2128 + hb_zip (this+baseCoverage, hb_range (basecount))
2129 | hb_filter (glyphset, hb_first)
2130 ;
2131
2132 new_coverage.reset ();
2133 + base_iter
2134 | hb_map (hb_first)
2135 | hb_map (glyph_map)
2136 | hb_sink (new_coverage)
2137 ;
2138
Garret Rieger8443cc82021-06-11 13:40:39 -07002139 if (!out->baseCoverage.serialize_serialize (c->serializer, new_coverage.iter ()))
Qunxin Liu82afc752020-02-04 13:24:37 -08002140 return_trace (false);
2141
2142 hb_sorted_vector_t<unsigned> base_indexes;
2143 for (const unsigned row : + base_iter
2144 | hb_map (hb_second))
2145 {
2146 + hb_range ((unsigned) classCount)
2147 | hb_filter (klass_mapping)
2148 | hb_map ([&] (const unsigned col) { return row * (unsigned) classCount + col; })
2149 | hb_sink (base_indexes)
2150 ;
2151 }
Garret Rieger6f98a8e2021-04-02 14:37:10 -07002152
2153 out->baseArray.serialize_subset (c, baseArray, this,
2154 base_iter.len (),
2155 base_indexes.iter ());
Qunxin Liu82afc752020-02-04 13:24:37 -08002156
2157 return_trace (true);
Behdad Esfahbod339d3602018-09-03 17:33:34 -07002158 }
2159
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302160 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03002161 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002162 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002163 return_trace (c->check_struct (this) &&
2164 markCoverage.sanitize (c, this) &&
2165 baseCoverage.sanitize (c, this) &&
2166 markArray.sanitize (c, this) &&
2167 baseArray.sanitize (c, this, (unsigned int) classCount));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04002168 }
2169
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002170 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +01002171 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbodad28f972021-03-31 12:49:14 -06002172 Offset16To<Coverage>
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04002173 markCoverage; /* Offset to MarkCoverage table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002174 * beginning of MarkBasePos subtable */
Behdad Esfahbodad28f972021-03-31 12:49:14 -06002175 Offset16To<Coverage>
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04002176 baseCoverage; /* Offset to BaseCoverage table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002177 * beginning of MarkBasePos subtable */
Behdad Esfahbod6b191782018-01-10 03:07:30 +01002178 HBUINT16 classCount; /* Number of classes defined for marks */
Behdad Esfahbodad28f972021-03-31 12:49:14 -06002179 Offset16To<MarkArray>
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04002180 markArray; /* Offset to MarkArray table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002181 * beginning of MarkBasePos subtable */
Behdad Esfahbodad28f972021-03-31 12:49:14 -06002182 Offset16To<BaseArray>
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04002183 baseArray; /* Offset to BaseArray table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002184 * beginning of MarkBasePos subtable */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04002185 public:
2186 DEFINE_SIZE_STATIC (12);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002187};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002188
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04002189struct MarkBasePos
2190{
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07002191 template <typename context_t, typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -07002192 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04002193 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08002194 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04002195 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04002196 switch (u.format) {
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -06002197 case 1: return_trace (c->dispatch (u.format1, std::forward<Ts> (ds)...));
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002198 default:return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04002199 }
2200 }
2201
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002202 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002203 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +01002204 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04002205 MarkBasePosFormat1 format1;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002206 } u;
2207};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002208
2209
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -04002210typedef AnchorMatrix LigatureAttach; /* component-major--
2211 * in order of writing direction--,
2212 * mark-minor--
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04002213 * ordered by class--zero-based. */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002214
Qunxin Liu3a0b05f2020-02-06 15:08:26 -08002215/* Array of LigatureAttach tables ordered by LigatureCoverage Index */
Behdad Esfahbod5efe3602021-03-31 15:33:22 -06002216struct LigatureArray : List16OfOffset16To<LigatureAttach>
Qunxin Liu3a0b05f2020-02-06 15:08:26 -08002217{
2218 template <typename Iterator,
2219 hb_requires (hb_is_iterator (Iterator))>
Garret Rieger093909b2020-10-05 13:14:53 -07002220 bool subset (hb_subset_context_t *c,
Garret Rieger6f98a8e2021-04-02 14:37:10 -07002221 Iterator coverage,
Garret Rieger093909b2020-10-05 13:14:53 -07002222 unsigned class_count,
2223 const hb_map_t *klass_mapping) const
Qunxin Liu3a0b05f2020-02-06 15:08:26 -08002224 {
Garret Rieger093909b2020-10-05 13:14:53 -07002225 TRACE_SUBSET (this);
2226 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
Garret Rieger147e93b2020-10-01 16:44:16 -07002227
Garret Rieger093909b2020-10-05 13:14:53 -07002228 auto *out = c->serializer->start_embed (this);
2229 if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
Qunxin Liu3a0b05f2020-02-06 15:08:26 -08002230
Behdad Esfahbod103ed7d2021-02-19 16:48:30 -07002231 for (const auto _ : + hb_zip (coverage, *this)
2232 | hb_filter (glyphset, hb_first))
Qunxin Liu3a0b05f2020-02-06 15:08:26 -08002233 {
Garret Rieger093909b2020-10-05 13:14:53 -07002234 auto *matrix = out->serialize_append (c->serializer);
2235 if (unlikely (!matrix)) return_trace (false);
2236
Garret Rieger6f98a8e2021-04-02 14:37:10 -07002237 const LigatureAttach& src = (this + _.second);
2238 auto indexes =
2239 + hb_range (src.rows * class_count)
2240 | hb_filter ([=] (unsigned index) { return klass_mapping->has (index % class_count); })
2241 ;
Garret Rieger093909b2020-10-05 13:14:53 -07002242 matrix->serialize_subset (c,
Behdad Esfahbod103ed7d2021-02-19 16:48:30 -07002243 _.second,
Garret Rieger093909b2020-10-05 13:14:53 -07002244 this,
Garret Rieger6f98a8e2021-04-02 14:37:10 -07002245 src.rows,
2246 indexes);
Qunxin Liu3a0b05f2020-02-06 15:08:26 -08002247 }
2248 return_trace (this->len);
2249 }
2250};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002251
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04002252struct MarkLigPosFormat1
2253{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302254 bool intersects (const hb_set_t *glyphs) const
Ebrahim Byagowid0e2add2020-07-18 22:14:52 +04302255 {
2256 return (this+markCoverage).intersects (glyphs) &&
2257 (this+ligatureCoverage).intersects (glyphs);
2258 }
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -07002259
Qunxin Liu0b39c482019-10-22 16:00:43 -07002260 void closure_lookups (hb_closure_lookups_context_t *c) const {}
2261
Qunxin Liu8200e482020-02-26 13:11:42 -08002262 void collect_variation_indices (hb_collect_variation_indices_context_t *c) const
2263 {
2264 + hb_zip (this+markCoverage, this+markArray)
2265 | hb_filter (c->glyph_set, hb_first)
2266 | hb_map (hb_second)
2267 | hb_apply ([&] (const MarkRecord& record) { record.collect_variation_indices (c, &(this+markArray)); })
2268 ;
2269
2270 hb_map_t klass_mapping;
2271 Markclass_closure_and_remap_indexes (this+markCoverage, this+markArray, *c->glyph_set, &klass_mapping);
2272
2273 unsigned ligcount = (this+ligatureArray).len;
2274 auto lig_iter =
2275 + hb_zip (this+ligatureCoverage, hb_range (ligcount))
2276 | hb_filter (c->glyph_set, hb_first)
2277 | hb_map (hb_second)
2278 ;
2279
2280 const LigatureArray& lig_array = this+ligatureArray;
2281 for (const unsigned i : lig_iter)
2282 {
2283 hb_sorted_vector_t<unsigned> lig_indexes;
2284 unsigned row_count = lig_array[i].rows;
2285 for (unsigned row : + hb_range (row_count))
2286 {
Ebrahim Byagowi5a7cc7f2020-07-29 08:33:32 +04302287 + hb_range ((unsigned) classCount)
2288 | hb_filter (klass_mapping)
2289 | hb_map ([&] (const unsigned col) { return row * (unsigned) classCount + col; })
2290 | hb_sink (lig_indexes)
2291 ;
Qunxin Liu8200e482020-02-26 13:11:42 -08002292 }
2293
2294 lig_array[i].collect_variation_indices (c, lig_indexes.iter ());
2295 }
2296 }
2297
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302298 void collect_glyphs (hb_collect_glyphs_context_t *c) const
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05002299 {
Behdad Esfahbod5cf53c02020-04-23 10:55:41 -07002300 if (unlikely (!(this+markCoverage).collect_coverage (c->input))) return;
2301 if (unlikely (!(this+ligatureCoverage).collect_coverage (c->input))) return;
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05002302 }
2303
Ebrahim Byagowie4120082018-12-17 21:31:01 +03302304 const Coverage &get_coverage () const { return this+markCoverage; }
Behdad Esfahbod0b994292012-07-28 17:31:01 -04002305
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302306 bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04002307 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002308 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02002309 hb_buffer_t *buffer = c->buffer;
2310 unsigned int mark_index = (this+markCoverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002311 if (likely (mark_index == NOT_COVERED)) return_trace (false);
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04002312
Behdad Esfahbod6ab92022016-02-11 16:57:52 +07002313 /* Now we search backwards for a non-mark glyph */
Behdad Esfahbodfd034492018-01-17 16:46:51 -08002314 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
Behdad Esfahbodb051be52015-01-29 13:40:39 +01002315 skippy_iter.reset (buffer->idx, 1);
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -05002316 skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002317 if (!skippy_iter.prev ()) return_trace (false);
Behdad Esfahbod0532ed12009-08-12 15:40:04 -04002318
Behdad Esfahbod101303d2013-10-18 00:42:39 +02002319 /* Checking that matched glyph is actually a ligature by GDEF is too strong; disabled */
Behdad Esfahbod6ab92022016-02-11 16:57:52 +07002320 //if (!_hb_glyph_info_is_ligature (&buffer->info[skippy_iter.idx])) { return_trace (false); }
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04002321
Behdad Esfahbod4ab97312012-01-16 22:05:08 -05002322 unsigned int j = skippy_iter.idx;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02002323 unsigned int lig_index = (this+ligatureCoverage).get_coverage (buffer->info[j].codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002324 if (lig_index == NOT_COVERED) return_trace (false);
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04002325
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04002326 const LigatureArray& lig_array = this+ligatureArray;
Behdad Esfahbod3564ee52009-08-14 18:32:56 -04002327 const LigatureAttach& lig_attach = lig_array[lig_index];
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -04002328
2329 /* Find component to attach to */
Behdad Esfahbodb2b18ef2009-08-14 19:37:18 -04002330 unsigned int comp_count = lig_attach.rows;
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002331 if (unlikely (!comp_count)) return_trace (false);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02002332
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04002333 /* We must now check whether the ligature ID of the current mark glyph
2334 * is identical to the ligature ID of the found ligature. If yes, we
2335 * can directly use the component index. If not, we attach the mark
2336 * glyph to the last component of the ligature. */
Behdad Esfahbod0aef4252012-07-30 00:55:15 -04002337 unsigned int comp_index;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02002338 unsigned int lig_id = _hb_glyph_info_get_lig_id (&buffer->info[j]);
2339 unsigned int mark_id = _hb_glyph_info_get_lig_id (&buffer->cur());
2340 unsigned int mark_comp = _hb_glyph_info_get_lig_comp (&buffer->cur());
Behdad Esfahbod0aef4252012-07-30 00:55:15 -04002341 if (lig_id && lig_id == mark_id && mark_comp > 0)
Behdad Esfahbod41248cc2019-05-07 20:54:31 -07002342 comp_index = hb_min (comp_count, _hb_glyph_info_get_lig_comp (&buffer->cur())) - 1;
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04002343 else
Behdad Esfahbodb2b18ef2009-08-14 19:37:18 -04002344 comp_index = comp_count - 1;
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04002345
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002346 return_trace ((this+markArray).apply (c, mark_index, comp_index, lig_attach, classCount, j));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002347 }
2348
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302349 bool subset (hb_subset_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -07002350 {
2351 TRACE_SUBSET (this);
Garret Rieger093909b2020-10-05 13:14:53 -07002352 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
Qunxin Liu3a0b05f2020-02-06 15:08:26 -08002353 const hb_map_t &glyph_map = *c->plan->glyph_map;
2354
2355 auto *out = c->serializer->start_embed (*this);
2356 if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
2357 out->format = format;
2358
2359 hb_map_t klass_mapping;
2360 Markclass_closure_and_remap_indexes (this+markCoverage, this+markArray, glyphset, &klass_mapping);
Garret Rieger147e93b2020-10-01 16:44:16 -07002361
Qunxin Liu3a0b05f2020-02-06 15:08:26 -08002362 if (!klass_mapping.get_population ()) return_trace (false);
2363 out->classCount = klass_mapping.get_population ();
2364
2365 auto mark_iter =
2366 + hb_zip (this+markCoverage, this+markArray)
2367 | hb_filter (glyphset, hb_first)
2368 ;
2369
Garret Rieger093909b2020-10-05 13:14:53 -07002370 auto new_mark_coverage =
Qunxin Liu3a0b05f2020-02-06 15:08:26 -08002371 + mark_iter
Garret Rieger093909b2020-10-05 13:14:53 -07002372 | hb_map_retains_sorting (hb_first)
2373 | hb_map_retains_sorting (glyph_map)
Qunxin Liu3a0b05f2020-02-06 15:08:26 -08002374 ;
2375
Garret Rieger8443cc82021-06-11 13:40:39 -07002376 if (!out->markCoverage.serialize_serialize (c->serializer, new_mark_coverage))
Qunxin Liu3a0b05f2020-02-06 15:08:26 -08002377 return_trace (false);
2378
Garret Rieger6f98a8e2021-04-02 14:37:10 -07002379 out->markArray.serialize_subset (c, markArray, this,
2380 (this+markCoverage).iter (),
2381 &klass_mapping);
Garret Rieger147e93b2020-10-01 16:44:16 -07002382
Garret Rieger093909b2020-10-05 13:14:53 -07002383 auto new_ligature_coverage =
2384 + hb_iter (this + ligatureCoverage)
2385 | hb_filter (glyphset)
2386 | hb_map_retains_sorting (glyph_map)
Qunxin Liu3a0b05f2020-02-06 15:08:26 -08002387 ;
2388
Garret Rieger8443cc82021-06-11 13:40:39 -07002389 if (!out->ligatureCoverage.serialize_serialize (c->serializer, new_ligature_coverage))
Qunxin Liu3a0b05f2020-02-06 15:08:26 -08002390 return_trace (false);
2391
Garret Rieger093909b2020-10-05 13:14:53 -07002392 out->ligatureArray.serialize_subset (c, ligatureArray, this,
2393 hb_iter (this+ligatureCoverage), classCount, &klass_mapping);
Qunxin Liu3a0b05f2020-02-06 15:08:26 -08002394
2395 return_trace (true);
Behdad Esfahbod339d3602018-09-03 17:33:34 -07002396 }
2397
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302398 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03002399 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002400 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002401 return_trace (c->check_struct (this) &&
2402 markCoverage.sanitize (c, this) &&
2403 ligatureCoverage.sanitize (c, this) &&
2404 markArray.sanitize (c, this) &&
2405 ligatureArray.sanitize (c, this, (unsigned int) classCount));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04002406 }
2407
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002408 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +01002409 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbodad28f972021-03-31 12:49:14 -06002410 Offset16To<Coverage>
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04002411 markCoverage; /* Offset to Mark Coverage table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002412 * beginning of MarkLigPos subtable */
Behdad Esfahbodad28f972021-03-31 12:49:14 -06002413 Offset16To<Coverage>
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04002414 ligatureCoverage; /* Offset to Ligature Coverage
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002415 * table--from beginning of MarkLigPos
2416 * subtable */
Behdad Esfahbod6b191782018-01-10 03:07:30 +01002417 HBUINT16 classCount; /* Number of defined mark classes */
Behdad Esfahbodad28f972021-03-31 12:49:14 -06002418 Offset16To<MarkArray>
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04002419 markArray; /* Offset to MarkArray table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002420 * beginning of MarkLigPos subtable */
Behdad Esfahbodad28f972021-03-31 12:49:14 -06002421 Offset16To<LigatureArray>
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04002422 ligatureArray; /* Offset to LigatureArray table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002423 * beginning of MarkLigPos subtable */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04002424 public:
2425 DEFINE_SIZE_STATIC (12);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002426};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002427
Garret Rieger093909b2020-10-05 13:14:53 -07002428
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04002429struct MarkLigPos
2430{
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07002431 template <typename context_t, typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -07002432 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04002433 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08002434 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04002435 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04002436 switch (u.format) {
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -06002437 case 1: return_trace (c->dispatch (u.format1, std::forward<Ts> (ds)...));
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002438 default:return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04002439 }
2440 }
2441
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002442 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002443 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +01002444 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04002445 MarkLigPosFormat1 format1;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002446 } u;
2447};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002448
2449
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -04002450typedef AnchorMatrix Mark2Array; /* mark2-major--
2451 * in order of Mark2Coverage Index--,
2452 * mark1-minor--
2453 * ordered by class--zero-based. */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002454
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04002455struct MarkMarkPosFormat1
2456{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302457 bool intersects (const hb_set_t *glyphs) const
Ebrahim Byagowid0e2add2020-07-18 22:14:52 +04302458 {
2459 return (this+mark1Coverage).intersects (glyphs) &&
2460 (this+mark2Coverage).intersects (glyphs);
2461 }
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -07002462
Qunxin Liu0b39c482019-10-22 16:00:43 -07002463 void closure_lookups (hb_closure_lookups_context_t *c) const {}
2464
Qunxin Liu8200e482020-02-26 13:11:42 -08002465 void collect_variation_indices (hb_collect_variation_indices_context_t *c) const
2466 {
2467 + hb_zip (this+mark1Coverage, this+mark1Array)
2468 | hb_filter (c->glyph_set, hb_first)
2469 | hb_map (hb_second)
2470 | hb_apply ([&] (const MarkRecord& record) { record.collect_variation_indices (c, &(this+mark1Array)); })
2471 ;
2472
2473 hb_map_t klass_mapping;
2474 Markclass_closure_and_remap_indexes (this+mark1Coverage, this+mark1Array, *c->glyph_set, &klass_mapping);
2475
2476 unsigned mark2_count = (this+mark2Array).rows;
2477 auto mark2_iter =
2478 + hb_zip (this+mark2Coverage, hb_range (mark2_count))
2479 | hb_filter (c->glyph_set, hb_first)
2480 | hb_map (hb_second)
2481 ;
2482
2483 hb_sorted_vector_t<unsigned> mark2_indexes;
2484 for (const unsigned row : mark2_iter)
2485 {
2486 + hb_range ((unsigned) classCount)
2487 | hb_filter (klass_mapping)
2488 | hb_map ([&] (const unsigned col) { return row * (unsigned) classCount + col; })
2489 | hb_sink (mark2_indexes)
2490 ;
2491 }
2492 (this+mark2Array).collect_variation_indices (c, mark2_indexes.iter ());
2493 }
2494
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302495 void collect_glyphs (hb_collect_glyphs_context_t *c) const
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05002496 {
Behdad Esfahbod5cf53c02020-04-23 10:55:41 -07002497 if (unlikely (!(this+mark1Coverage).collect_coverage (c->input))) return;
2498 if (unlikely (!(this+mark2Coverage).collect_coverage (c->input))) return;
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05002499 }
2500
Ebrahim Byagowie4120082018-12-17 21:31:01 +03302501 const Coverage &get_coverage () const { return this+mark1Coverage; }
Behdad Esfahbod0b994292012-07-28 17:31:01 -04002502
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302503 bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04002504 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002505 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02002506 hb_buffer_t *buffer = c->buffer;
2507 unsigned int mark1_index = (this+mark1Coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002508 if (likely (mark1_index == NOT_COVERED)) return_trace (false);
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04002509
2510 /* now we search backwards for a suitable mark glyph until a non-mark glyph */
Behdad Esfahbodfd034492018-01-17 16:46:51 -08002511 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
Behdad Esfahbodb051be52015-01-29 13:40:39 +01002512 skippy_iter.reset (buffer->idx, 1);
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -05002513 skippy_iter.set_lookup_props (c->lookup_props & ~LookupFlag::IgnoreFlags);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002514 if (!skippy_iter.prev ()) return_trace (false);
Behdad Esfahbod0532ed12009-08-12 15:40:04 -04002515
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002516 if (!_hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx])) { return_trace (false); }
Behdad Esfahbod80ea5bd2009-05-26 17:58:37 -04002517
Behdad Esfahbod4ab97312012-01-16 22:05:08 -05002518 unsigned int j = skippy_iter.idx;
2519
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02002520 unsigned int id1 = _hb_glyph_info_get_lig_id (&buffer->cur());
2521 unsigned int id2 = _hb_glyph_info_get_lig_id (&buffer->info[j]);
2522 unsigned int comp1 = _hb_glyph_info_get_lig_comp (&buffer->cur());
2523 unsigned int comp2 = _hb_glyph_info_get_lig_comp (&buffer->info[j]);
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04002524
Ebrahim Byagowid0e2add2020-07-18 22:14:52 +04302525 if (likely (id1 == id2))
2526 {
Behdad Esfahbod5d874d52012-07-28 21:05:25 -04002527 if (id1 == 0) /* Marks belonging to the same base. */
2528 goto good;
2529 else if (comp1 == comp2) /* Marks belonging to the same ligature component. */
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +03302530 goto good;
Ebrahim Byagowid0e2add2020-07-18 22:14:52 +04302531 }
2532 else
2533 {
Behdad Esfahbod5d874d52012-07-28 21:05:25 -04002534 /* If ligature ids don't match, it may be the case that one of the marks
2535 * itself is a ligature. In which case match. */
2536 if ((id1 > 0 && !comp1) || (id2 > 0 && !comp2))
2537 goto good;
2538 }
2539
2540 /* Didn't match. */
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002541 return_trace (false);
Behdad Esfahbod5d874d52012-07-28 21:05:25 -04002542
2543 good:
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02002544 unsigned int mark2_index = (this+mark2Coverage).get_coverage (buffer->info[j].codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002545 if (mark2_index == NOT_COVERED) return_trace (false);
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04002546
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002547 return_trace ((this+mark1Array).apply (c, mark1_index, mark2_index, this+mark2Array, classCount, j));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002548 }
2549
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302550 bool subset (hb_subset_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -07002551 {
2552 TRACE_SUBSET (this);
Garret Riegere5835052020-09-29 11:05:08 -07002553 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
Qunxin Liueb7849a2020-02-05 15:29:03 -08002554 const hb_map_t &glyph_map = *c->plan->glyph_map;
2555
2556 auto *out = c->serializer->start_embed (*this);
2557 if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
2558 out->format = format;
2559
2560 hb_map_t klass_mapping;
2561 Markclass_closure_and_remap_indexes (this+mark1Coverage, this+mark1Array, glyphset, &klass_mapping);
Ebrahim Byagowi07acd1a2020-03-08 23:39:24 +03302562
Qunxin Liueb7849a2020-02-05 15:29:03 -08002563 if (!klass_mapping.get_population ()) return_trace (false);
2564 out->classCount = klass_mapping.get_population ();
2565
2566 auto mark1_iter =
2567 + hb_zip (this+mark1Coverage, this+mark1Array)
2568 | hb_filter (glyphset, hb_first)
2569 ;
2570
2571 hb_sorted_vector_t<hb_codepoint_t> new_coverage;
2572 + mark1_iter
2573 | hb_map (hb_first)
2574 | hb_map (glyph_map)
2575 | hb_sink (new_coverage)
2576 ;
2577
Garret Rieger8443cc82021-06-11 13:40:39 -07002578 if (!out->mark1Coverage.serialize_serialize (c->serializer, new_coverage.iter ()))
Qunxin Liueb7849a2020-02-05 15:29:03 -08002579 return_trace (false);
2580
Garret Rieger6f98a8e2021-04-02 14:37:10 -07002581 out->mark1Array.serialize_subset (c, mark1Array, this,
2582 (this+mark1Coverage).iter (),
2583 &klass_mapping);
Ebrahim Byagowid0e2add2020-07-18 22:14:52 +04302584
Qunxin Liueb7849a2020-02-05 15:29:03 -08002585 unsigned mark2count = (this+mark2Array).rows;
2586 auto mark2_iter =
2587 + hb_zip (this+mark2Coverage, hb_range (mark2count))
2588 | hb_filter (glyphset, hb_first)
2589 ;
2590
2591 new_coverage.reset ();
2592 + mark2_iter
2593 | hb_map (hb_first)
2594 | hb_map (glyph_map)
2595 | hb_sink (new_coverage)
2596 ;
2597
Garret Rieger8443cc82021-06-11 13:40:39 -07002598 if (!out->mark2Coverage.serialize_serialize (c->serializer, new_coverage.iter ()))
Qunxin Liueb7849a2020-02-05 15:29:03 -08002599 return_trace (false);
2600
2601 hb_sorted_vector_t<unsigned> mark2_indexes;
2602 for (const unsigned row : + mark2_iter
2603 | hb_map (hb_second))
2604 {
2605 + hb_range ((unsigned) classCount)
2606 | hb_filter (klass_mapping)
2607 | hb_map ([&] (const unsigned col) { return row * (unsigned) classCount + col; })
2608 | hb_sink (mark2_indexes)
2609 ;
2610 }
Garret Rieger6f98a8e2021-04-02 14:37:10 -07002611
2612 out->mark2Array.serialize_subset (c, mark2Array, this, mark2_iter.len (), mark2_indexes.iter ());
Qunxin Liueb7849a2020-02-05 15:29:03 -08002613
2614 return_trace (true);
Behdad Esfahbod339d3602018-09-03 17:33:34 -07002615 }
2616
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302617 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03002618 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05002619 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002620 return_trace (c->check_struct (this) &&
2621 mark1Coverage.sanitize (c, this) &&
2622 mark2Coverage.sanitize (c, this) &&
2623 mark1Array.sanitize (c, this) &&
2624 mark2Array.sanitize (c, this, (unsigned int) classCount));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04002625 }
2626
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002627 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +01002628 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbodad28f972021-03-31 12:49:14 -06002629 Offset16To<Coverage>
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04002630 mark1Coverage; /* Offset to Combining Mark1 Coverage
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002631 * table--from beginning of MarkMarkPos
2632 * subtable */
Behdad Esfahbodad28f972021-03-31 12:49:14 -06002633 Offset16To<Coverage>
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04002634 mark2Coverage; /* Offset to Combining Mark2 Coverage
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002635 * table--from beginning of MarkMarkPos
2636 * subtable */
Behdad Esfahbod6b191782018-01-10 03:07:30 +01002637 HBUINT16 classCount; /* Number of defined mark classes */
Behdad Esfahbodad28f972021-03-31 12:49:14 -06002638 Offset16To<MarkArray>
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04002639 mark1Array; /* Offset to Mark1Array table--from
2640 * beginning of MarkMarkPos subtable */
Behdad Esfahbodad28f972021-03-31 12:49:14 -06002641 Offset16To<Mark2Array>
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04002642 mark2Array; /* Offset to Mark2Array table--from
2643 * beginning of MarkMarkPos subtable */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04002644 public:
2645 DEFINE_SIZE_STATIC (12);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002646};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002647
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04002648struct MarkMarkPos
2649{
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07002650 template <typename context_t, typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -07002651 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04002652 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08002653 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04002654 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04002655 switch (u.format) {
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -06002656 case 1: return_trace (c->dispatch (u.format1, std::forward<Ts> (ds)...));
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002657 default:return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04002658 }
2659 }
2660
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002661 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002662 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +01002663 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04002664 MarkMarkPosFormat1 format1;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002665 } u;
2666};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002667
2668
Behdad Esfahbod08f1eed2012-11-23 16:51:43 -05002669struct ContextPos : Context {};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002670
Behdad Esfahbod08f1eed2012-11-23 16:51:43 -05002671struct ChainContextPos : ChainContext {};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002672
Behdad Esfahbod653eeb22012-11-23 16:57:36 -05002673struct ExtensionPos : Extension<ExtensionPos>
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04002674{
Behdad Esfahbod9c3747c2018-09-03 16:53:03 -07002675 typedef struct PosLookupSubTable SubTable;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002676};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002677
2678
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04002679
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002680/*
2681 * PosLookup
2682 */
2683
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002684
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04002685struct PosLookupSubTable
2686{
Behdad Esfahbod7b2ef552018-09-03 17:16:09 -07002687 friend struct Lookup;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002688 friend struct PosLookup;
2689
Behdad Esfahbodc6035cf2012-04-12 13:23:59 -04002690 enum Type {
Behdad Esfahbodff05d252009-05-20 03:53:00 -04002691 Single = 1,
2692 Pair = 2,
2693 Cursive = 3,
2694 MarkBase = 4,
2695 MarkLig = 5,
2696 MarkMark = 6,
2697 Context = 7,
2698 ChainContext = 8,
Behdad Esfahbod8f034d52009-08-18 16:41:59 -04002699 Extension = 9
Behdad Esfahbodff05d252009-05-20 03:53:00 -04002700 };
2701
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07002702 template <typename context_t, typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -07002703 typename context_t::return_t dispatch (context_t *c, unsigned int lookup_type, Ts&&... ds) const
Behdad Esfahbodfaf0f202012-06-09 03:02:36 -04002704 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08002705 TRACE_DISPATCH (this, lookup_type);
Behdad Esfahbod0b994292012-07-28 17:31:01 -04002706 switch (lookup_type) {
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -06002707 case Single: return_trace (u.single.dispatch (c, std::forward<Ts> (ds)...));
2708 case Pair: return_trace (u.pair.dispatch (c, std::forward<Ts> (ds)...));
2709 case Cursive: return_trace (u.cursive.dispatch (c, std::forward<Ts> (ds)...));
2710 case MarkBase: return_trace (u.markBase.dispatch (c, std::forward<Ts> (ds)...));
2711 case MarkLig: return_trace (u.markLig.dispatch (c, std::forward<Ts> (ds)...));
2712 case MarkMark: return_trace (u.markMark.dispatch (c, std::forward<Ts> (ds)...));
2713 case Context: return_trace (u.context.dispatch (c, std::forward<Ts> (ds)...));
2714 case ChainContext: return_trace (u.chainContext.dispatch (c, std::forward<Ts> (ds)...));
2715 case Extension: return_trace (u.extension.dispatch (c, std::forward<Ts> (ds)...));
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002716 default: return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04002717 }
Behdad Esfahbodfaf0f202012-06-09 03:02:36 -04002718 }
2719
ariza72cbfb92020-01-18 16:35:52 -08002720 bool intersects (const hb_set_t *glyphs, unsigned int lookup_type) const
2721 {
2722 hb_intersects_context_t c (glyphs);
2723 return dispatch (&c, lookup_type);
2724 }
2725
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04002726 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002727 union {
Behdad Esfahboddacebca2010-05-10 19:45:41 -04002728 SinglePos single;
2729 PairPos pair;
2730 CursivePos cursive;
2731 MarkBasePos markBase;
2732 MarkLigPos markLig;
2733 MarkMarkPos markMark;
Behdad Esfahbode72b3602012-07-19 14:35:23 -04002734 ContextPos context;
Behdad Esfahboddacebca2010-05-10 19:45:41 -04002735 ChainContextPos chainContext;
2736 ExtensionPos extension;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002737 } u;
Behdad Esfahboded074222010-05-10 18:08:46 -04002738 public:
Behdad Esfahbod2cc993e2018-12-12 10:07:38 -05002739 DEFINE_SIZE_MIN (0);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002740};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002741
2742
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04002743struct PosLookup : Lookup
2744{
Behdad Esfahbod9c3747c2018-09-03 16:53:03 -07002745 typedef struct PosLookupSubTable SubTable;
2746
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302747 const SubTable& get_subtable (unsigned int i) const
Behdad Esfahbod9c3747c2018-09-03 16:53:03 -07002748 { return Lookup::get_subtable<SubTable> (i); }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002749
Ebrahim Byagowie4120082018-12-17 21:31:01 +03302750 bool is_reverse () const
Behdad Esfahbode015b8f2013-05-03 17:34:29 -04002751 {
2752 return false;
2753 }
2754
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302755 bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbodbd047d32015-02-19 10:47:18 +03002756 {
2757 TRACE_APPLY (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01002758 return_trace (dispatch (c));
Behdad Esfahbodbd047d32015-02-19 10:47:18 +03002759 }
2760
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302761 bool intersects (const hb_set_t *glyphs) const
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -07002762 {
2763 hb_intersects_context_t c (glyphs);
2764 return dispatch (&c);
2765 }
2766
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302767 hb_collect_glyphs_context_t::return_t collect_glyphs (hb_collect_glyphs_context_t *c) const
Behdad Esfahbod89bcfb22019-01-18 14:59:18 -05002768 { return dispatch (c); }
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05002769
Qunxin Liu0b39c482019-10-22 16:00:43 -07002770 hb_closure_lookups_context_t::return_t closure_lookups (hb_closure_lookups_context_t *c, unsigned this_index) const
2771 {
2772 if (c->is_lookup_visited (this_index))
2773 return hb_closure_lookups_context_t::default_return_value ();
2774
2775 c->set_lookup_visited (this_index);
2776 if (!intersects (c->glyphs))
2777 {
2778 c->set_lookup_inactive (this_index);
2779 return hb_closure_lookups_context_t::default_return_value ();
2780 }
2781 c->set_recurse_func (dispatch_closure_lookups_recurse_func);
2782
2783 hb_closure_lookups_context_t::return_t ret = dispatch (c);
2784 return ret;
2785 }
2786
Behdad Esfahboda878c582012-08-01 21:18:54 -04002787 template <typename set_t>
Behdad Esfahbod5cf53c02020-04-23 10:55:41 -07002788 void collect_coverage (set_t *glyphs) const
Behdad Esfahboda878c582012-08-01 21:18:54 -04002789 {
Behdad Esfahbod5cf53c02020-04-23 10:55:41 -07002790 hb_collect_coverage_context_t<set_t> c (glyphs);
Behdad Esfahbod8e36ccf2015-02-17 19:15:34 +03002791 dispatch (&c);
Behdad Esfahboda878c582012-08-01 21:18:54 -04002792 }
2793
Behdad Esfahboda061e472019-12-10 13:31:50 -06002794 static inline bool apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index);
Behdad Esfahbodec35a722012-11-22 16:05:59 -05002795
Behdad Esfahbodee5464d2013-03-09 01:59:30 -05002796 template <typename context_t>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302797 static typename context_t::return_t dispatch_recurse_func (context_t *c, unsigned int lookup_index);
Behdad Esfahbodee5464d2013-03-09 01:59:30 -05002798
Qunxin Liu0b39c482019-10-22 16:00:43 -07002799 HB_INTERNAL static hb_closure_lookups_context_t::return_t dispatch_closure_lookups_recurse_func (hb_closure_lookups_context_t *c, unsigned this_index);
2800
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07002801 template <typename context_t, typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -07002802 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
Behdad Esfahbod6d555ce2021-11-02 00:18:22 -06002803 { return Lookup::dispatch<SubTable> (c, std::forward<Ts> (ds)...); }
Behdad Esfahbodee5464d2013-03-09 01:59:30 -05002804
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302805 bool subset (hb_subset_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -07002806 { return Lookup::subset<SubTable> (c); }
2807
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302808 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod7b2ef552018-09-03 17:16:09 -07002809 { return Lookup::sanitize<SubTable> (c); }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002810};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002811
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002812/*
Ebrahim Byagowia02c3ee2018-04-12 13:38:19 +04302813 * GPOS -- Glyph Positioning
2814 * https://docs.microsoft.com/en-us/typography/opentype/spec/gpos
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002815 */
2816
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04002817struct GPOS : GSUBGPOS
2818{
Behdad Esfahbodef006542019-01-22 12:08:57 +01002819 static constexpr hb_tag_t tableTag = HB_OT_TAG_GPOS;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002820
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302821 const PosLookup& get_lookup (unsigned int i) const
Behdad Esfahbod858b6272019-12-10 13:18:32 -06002822 { return static_cast<const PosLookup &> (GSUBGPOS::get_lookup (i)); }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002823
Behdad Esfahbod05bd1b62012-07-30 19:30:01 -04002824 static inline void position_start (hb_font_t *font, hb_buffer_t *buffer);
Behdad Esfahbod7d8d58a2016-02-11 16:34:28 +07002825 static inline void position_finish_advances (hb_font_t *font, hb_buffer_t *buffer);
2826 static inline void position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer);
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04002827
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302828 bool subset (hb_subset_context_t *c) const
Qunxin Liue565d1f2019-11-01 10:21:36 -07002829 {
Qunxin Liu56ca4352021-01-28 15:21:26 -08002830 hb_subset_layout_context_t l (c, tableTag, c->plan->gpos_lookups, c->plan->gpos_langsys, c->plan->gpos_features);
Qunxin Liue565d1f2019-11-01 10:21:36 -07002831 return GSUBGPOS::subset<PosLookup> (&l);
2832 }
Behdad Esfahbod339d3602018-09-03 17:33:34 -07002833
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03302834 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -07002835 { return GSUBGPOS::sanitize<PosLookup> (c); }
Behdad Esfahbod963413f2018-08-26 00:47:55 -07002836
Behdad Esfahbod56719472020-06-05 12:57:23 -07002837 HB_INTERNAL bool is_blocklisted (hb_blob_t *blob,
Behdad Esfahbod574d8882018-11-25 16:51:22 -05002838 hb_face_t *face) const;
2839
Qunxin Liu8200e482020-02-26 13:11:42 -08002840 void collect_variation_indices (hb_collect_variation_indices_context_t *c) const
2841 {
2842 for (unsigned i = 0; i < GSUBGPOS::get_lookup_count (); i++)
2843 {
2844 if (!c->gpos_lookups->has (i)) continue;
2845 const PosLookup &l = get_lookup (i);
2846 l.dispatch (c);
2847 }
2848 }
2849
Qunxin Liu973c47f2020-06-11 11:27:57 -07002850 void closure_lookups (hb_face_t *face,
2851 const hb_set_t *glyphs,
2852 hb_set_t *lookup_indexes /* IN/OUT */) const
2853 { GSUBGPOS::closure_lookups<PosLookup> (face, glyphs, lookup_indexes); }
2854
Behdad Esfahbod963413f2018-08-26 00:47:55 -07002855 typedef GSUBGPOS::accelerator_t<GPOS> accelerator_t;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002856};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002857
Behdad Esfahbod7403e052011-05-24 21:04:15 -04002858
2859static void
Behdad Esfahbod65785752015-08-25 20:24:59 +01002860reverse_cursive_minor_offset (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction, unsigned int new_parent)
2861{
Behdad Esfahbod686567b2016-02-11 15:25:28 +07002862 int chain = pos[i].attach_chain(), type = pos[i].attach_type();
2863 if (likely (!chain || 0 == (type & ATTACH_TYPE_CURSIVE)))
Behdad Esfahbod65785752015-08-25 20:24:59 +01002864 return;
2865
Behdad Esfahbod686567b2016-02-11 15:25:28 +07002866 pos[i].attach_chain() = 0;
Behdad Esfahbod65785752015-08-25 20:24:59 +01002867
Behdad Esfahbod686567b2016-02-11 15:25:28 +07002868 unsigned int j = (int) i + chain;
Behdad Esfahbod806ad8d2016-02-11 14:53:11 +07002869
Behdad Esfahbod65785752015-08-25 20:24:59 +01002870 /* Stop if we see new parent in the chain. */
2871 if (j == new_parent)
2872 return;
2873
2874 reverse_cursive_minor_offset (pos, j, direction, new_parent);
2875
2876 if (HB_DIRECTION_IS_HORIZONTAL (direction))
2877 pos[j].y_offset = -pos[i].y_offset;
2878 else
2879 pos[j].x_offset = -pos[i].x_offset;
2880
Behdad Esfahbod686567b2016-02-11 15:25:28 +07002881 pos[j].attach_chain() = -chain;
2882 pos[j].attach_type() = type;
Behdad Esfahbod65785752015-08-25 20:24:59 +01002883}
2884static void
Behdad Esfahbod36f38ea2018-10-16 16:24:03 -07002885propagate_attachment_offsets (hb_glyph_position_t *pos,
2886 unsigned int len,
2887 unsigned int i,
2888 hb_direction_t direction)
Behdad Esfahbod7403e052011-05-24 21:04:15 -04002889{
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07002890 /* Adjusts offsets of attached glyphs (both cursive and mark) to accumulate
2891 * offset of glyph they are attached to. */
Behdad Esfahbod686567b2016-02-11 15:25:28 +07002892 int chain = pos[i].attach_chain(), type = pos[i].attach_type();
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07002893 if (likely (!chain))
Behdad Esfahboddf5d5c62012-08-23 09:33:30 -04002894 return;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04002895
Behdad Esfahbod686567b2016-02-11 15:25:28 +07002896 pos[i].attach_chain() = 0;
2897
Behdad Esfahbod36f38ea2018-10-16 16:24:03 -07002898 unsigned int j = (int) i + chain;
2899
2900 if (unlikely (j >= len))
2901 return;
2902
2903 propagate_attachment_offsets (pos, len, j, direction);
Behdad Esfahbod7403e052011-05-24 21:04:15 -04002904
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07002905 assert (!!(type & ATTACH_TYPE_MARK) ^ !!(type & ATTACH_TYPE_CURSIVE));
Behdad Esfahbod7403e052011-05-24 21:04:15 -04002906
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07002907 if (type & ATTACH_TYPE_CURSIVE)
2908 {
2909 if (HB_DIRECTION_IS_HORIZONTAL (direction))
2910 pos[i].y_offset += pos[j].y_offset;
2911 else
2912 pos[i].x_offset += pos[j].x_offset;
2913 }
2914 else /*if (type & ATTACH_TYPE_MARK)*/
2915 {
Behdad Esfahboddf5d5c62012-08-23 09:33:30 -04002916 pos[i].x_offset += pos[j].x_offset;
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07002917 pos[i].y_offset += pos[j].y_offset;
Behdad Esfahbodebd74312016-02-24 15:53:40 +09002918
2919 assert (j < i);
2920 if (HB_DIRECTION_IS_FORWARD (direction))
2921 for (unsigned int k = j; k < i; k++) {
2922 pos[i].x_offset -= pos[k].x_advance;
2923 pos[i].y_offset -= pos[k].y_advance;
2924 }
2925 else
2926 for (unsigned int k = j + 1; k < i + 1; k++) {
2927 pos[i].x_offset += pos[k].x_advance;
2928 pos[i].y_offset += pos[k].y_advance;
2929 }
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07002930 }
Behdad Esfahbod7403e052011-05-24 21:04:15 -04002931}
2932
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04002933void
Behdad Esfahbod05bd1b62012-07-30 19:30:01 -04002934GPOS::position_start (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
Behdad Esfahboda9ad3d32011-07-28 15:42:18 -04002935{
Behdad Esfahbodb65c0602011-07-28 16:48:43 -04002936 unsigned int count = buffer->len;
2937 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod686567b2016-02-11 15:25:28 +07002938 buffer->pos[i].attach_chain() = buffer->pos[i].attach_type() = 0;
Behdad Esfahboda9ad3d32011-07-28 15:42:18 -04002939}
2940
2941void
Behdad Esfahbod39bd07a2018-10-26 21:01:11 -07002942GPOS::position_finish_advances (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer HB_UNUSED)
Behdad Esfahbod7d8d58a2016-02-11 16:34:28 +07002943{
2944 //_hb_buffer_assert_gsubgpos_vars (buffer);
2945}
2946
2947void
2948GPOS::position_finish_offsets (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04002949{
Behdad Esfahbod8f3eebf2014-08-02 17:18:46 -04002950 _hb_buffer_assert_gsubgpos_vars (buffer);
2951
Ryan Lortie70566be2011-04-15 18:32:36 -04002952 unsigned int len;
2953 hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, &len);
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04002954 hb_direction_t direction = buffer->props.direction;
2955
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04002956 /* Handle attachments */
Behdad Esfahbod550bd142015-11-04 22:58:58 -08002957 if (buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT)
2958 for (unsigned int i = 0; i < len; i++)
Behdad Esfahbod36f38ea2018-10-16 16:24:03 -07002959 propagate_attachment_offsets (pos, len, i, direction);
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04002960}
2961
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002962
Behdad Esfahbod33b006c2018-11-05 23:19:04 -05002963struct GPOS_accelerator_t : GPOS::accelerator_t {};
2964
2965
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002966/* Out-of-class implementation for methods recursing */
2967
Behdad Esfahbod7dcf8e12019-06-26 13:44:10 -07002968#ifndef HB_NO_OT_LAYOUT
Behdad Esfahbodc6fb8432012-11-23 18:04:08 -05002969template <typename context_t>
Behdad Esfahboddd3972a2019-12-10 13:21:26 -06002970/*static*/ typename context_t::return_t PosLookup::dispatch_recurse_func (context_t *c, unsigned int lookup_index)
Behdad Esfahbodc6fb8432012-11-23 18:04:08 -05002971{
Behdad Esfahbod33b006c2018-11-05 23:19:04 -05002972 const PosLookup &l = c->face->table.GPOS.get_relaxed ()->table->get_lookup (lookup_index);
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05002973 return l.dispatch (c);
Behdad Esfahbodc6fb8432012-11-23 18:04:08 -05002974}
Qunxin Liu0b39c482019-10-22 16:00:43 -07002975
2976/*static*/ inline hb_closure_lookups_context_t::return_t PosLookup::dispatch_closure_lookups_recurse_func (hb_closure_lookups_context_t *c, unsigned this_index)
2977{
2978 const PosLookup &l = c->face->table.GPOS.get_relaxed ()->table->get_lookup (this_index);
2979 return l.closure_lookups (c, this_index);
2980}
2981
Behdad Esfahboda061e472019-12-10 13:31:50 -06002982/*static*/ bool PosLookup::apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index)
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04002983{
Behdad Esfahbod33b006c2018-11-05 23:19:04 -05002984 const PosLookup &l = c->face->table.GPOS.get_relaxed ()->table->get_lookup (lookup_index);
Behdad Esfahbod4c4e8f02012-11-24 01:13:20 -05002985 unsigned int saved_lookup_props = c->lookup_props;
Behdad Esfahbod2c8b3b22015-08-18 14:36:43 +01002986 unsigned int saved_lookup_index = c->lookup_index;
2987 c->set_lookup_index (lookup_index);
2988 c->set_lookup_props (l.get_props ());
Behdad Esfahbod1a232212015-02-19 10:40:23 +03002989 bool ret = l.dispatch (c);
Behdad Esfahbod2c8b3b22015-08-18 14:36:43 +01002990 c->set_lookup_index (saved_lookup_index);
Behdad Esfahbod77889932015-01-28 23:01:12 -08002991 c->set_lookup_props (saved_lookup_props);
Behdad Esfahbod4c4e8f02012-11-24 01:13:20 -05002992 return ret;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002993}
Behdad Esfahbod7dcf8e12019-06-26 13:44:10 -07002994#endif
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04002995
2996
Behdad Esfahbod7d52e662012-11-16 18:49:54 -08002997} /* namespace OT */
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -04002998
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -04002999
Behdad Esfahbod7a750ac2011-08-17 14:19:59 +02003000#endif /* HB_OT_LAYOUT_GPOS_TABLE_HH */