blob: 73107f490b5a117d7b8defa23bbf3f3cb8b5d766 [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
Behdad Esfahbod94a23aa2010-05-05 01:13:09 -040037
Behdad Esfahbodb65c0602011-07-28 16:48:43 -040038/* buffer **position** var allocations */
Behdad Esfahbod686567b2016-02-11 15:25:28 +070039#define attach_chain() var.i16[0] /* glyph to which this attaches to, relative to current glyphs; negative for going back, positive for forward. */
40#define attach_type() var.u8[2] /* attachment type */
41/* Note! if attach_chain() is zero, the value of attach_type() is irrelevant. */
42
43enum attach_type_t {
44 ATTACH_TYPE_NONE = 0X00,
45
46 /* Each attachment should be either a mark or a cursive; can't be both. */
47 ATTACH_TYPE_MARK = 0X01,
48 ATTACH_TYPE_CURSIVE = 0X02,
49};
Behdad Esfahbod194d4562010-10-27 23:09:10 -040050
51
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040052/* Shared Tables: ValueRecord, Anchor Table, and MarkArray */
53
Behdad Esfahbod6b191782018-01-10 03:07:30 +010054typedef HBUINT16 Value;
Behdad Esfahbodc91facd2009-08-26 18:53:43 -040055
Behdad Esfahbod1d66cdc2018-11-10 19:54:08 -050056typedef UnsizedArrayOf<Value> ValueRecord;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040057
Behdad Esfahbod6b191782018-01-10 03:07:30 +010058struct ValueFormat : HBUINT16
Behdad Esfahbodfca6a0d2009-05-21 04:49:04 -040059{
Behdad Esfahbodc6035cf2012-04-12 13:23:59 -040060 enum Flags {
Behdad Esfahbod76271002014-07-11 14:54:42 -040061 xPlacement = 0x0001u, /* Includes horizontal adjustment for placement */
62 yPlacement = 0x0002u, /* Includes vertical adjustment for placement */
63 xAdvance = 0x0004u, /* Includes horizontal adjustment for advance */
64 yAdvance = 0x0008u, /* Includes vertical adjustment for advance */
65 xPlaDevice = 0x0010u, /* Includes horizontal Device table for placement */
66 yPlaDevice = 0x0020u, /* Includes vertical Device table for placement */
67 xAdvDevice = 0x0040u, /* Includes horizontal Device table for advance */
68 yAdvDevice = 0x0080u, /* Includes vertical Device table for advance */
69 ignored = 0x0F00u, /* Was used in TrueType Open for MM fonts */
70 reserved = 0xF000u, /* For future use */
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -040071
Behdad Esfahbod76271002014-07-11 14:54:42 -040072 devices = 0x00F0u /* Mask for having any Device table */
Behdad Esfahbodfca6a0d2009-05-21 04:49:04 -040073 };
74
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -040075/* All fields are options. Only those available advance the value pointer. */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040076#if 0
Behdad Esfahbod6b191782018-01-10 03:07:30 +010077 HBINT16 xPlacement; /* Horizontal adjustment for
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040078 * placement--in design units */
Behdad Esfahbod6b191782018-01-10 03:07:30 +010079 HBINT16 yPlacement; /* Vertical adjustment for
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040080 * placement--in design units */
Behdad Esfahbod6b191782018-01-10 03:07:30 +010081 HBINT16 xAdvance; /* Horizontal adjustment for
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040082 * advance--in design units (only used
83 * for horizontal writing) */
Behdad Esfahbod6b191782018-01-10 03:07:30 +010084 HBINT16 yAdvance; /* Vertical adjustment for advance--in
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040085 * design units (only used for vertical
86 * writing) */
Behdad Esfahbodf39b5602019-01-17 17:49:55 -050087 OffsetTo<Device> xPlaDevice; /* Offset to Device table for
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040088 * horizontal placement--measured from
89 * beginning of PosTable (may be NULL) */
Behdad Esfahbodf39b5602019-01-17 17:49:55 -050090 OffsetTo<Device> yPlaDevice; /* Offset to Device table for vertical
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040091 * placement--measured from beginning
92 * of PosTable (may be NULL) */
Behdad Esfahbodf39b5602019-01-17 17:49:55 -050093 OffsetTo<Device> xAdvDevice; /* Offset to Device table for
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040094 * horizontal advance--measured from
95 * beginning of PosTable (may be NULL) */
Behdad Esfahbodf39b5602019-01-17 17:49:55 -050096 OffsetTo<Device> yAdvDevice; /* Offset to Device table for vertical
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040097 * advance--measured from beginning of
98 * PosTable (may be NULL) */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040099#endif
100
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330101 unsigned int get_len () const { return hb_popcount ((unsigned int) *this); }
102 unsigned int get_size () const { return get_len () * Value::static_size; }
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400103
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500104 bool apply_value (hb_ot_apply_context_t *c,
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400105 const void *base,
106 const Value *values,
107 hb_glyph_position_t &glyph_pos) const
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400108 {
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500109 bool ret = false;
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400110 unsigned int format = *this;
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500111 if (!format) return ret;
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400112
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700113 hb_font_t *font = c->font;
Behdad Esfahbod385f78b2018-11-07 17:19:21 -0500114 bool horizontal = HB_DIRECTION_IS_HORIZONTAL (c->direction);
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700115
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500116 if (format & xPlacement) glyph_pos.x_offset += font->em_scale_x (get_short (values++, &ret));
117 if (format & yPlacement) glyph_pos.y_offset += font->em_scale_y (get_short (values++, &ret));
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400118 if (format & xAdvance) {
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500119 if (likely (horizontal)) glyph_pos.x_advance += font->em_scale_x (get_short (values, &ret));
Konstantin Ritt08322592014-01-22 20:31:30 +0200120 values++;
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400121 }
Behdad Esfahbodcc2086d2011-05-19 19:19:50 -0400122 /* y_advance values grow downward but font-space grows upward, hence negation */
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400123 if (format & yAdvance) {
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500124 if (unlikely (!horizontal)) glyph_pos.y_advance -= font->em_scale_y (get_short (values, &ret));
Konstantin Ritt08322592014-01-22 20:31:30 +0200125 values++;
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400126 }
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400127
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500128 if (!has_device ()) return ret;
Behdad Esfahboda8d960b2010-04-29 14:31:56 -0400129
Behdad Esfahbodca286702016-03-01 19:29:36 +0900130 bool use_x_device = font->x_ppem || font->num_coords;
131 bool use_y_device = font->y_ppem || font->num_coords;
Behdad Esfahboda8d960b2010-04-29 14:31:56 -0400132
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500133 if (!use_x_device && !use_y_device) return ret;
Behdad Esfahboda8d960b2010-04-29 14:31:56 -0400134
Behdad Esfahbodcf3de4d2016-09-10 00:22:24 -0700135 const VariationStore &store = c->var_store;
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700136
Behdad Esfahbod0090dc02009-07-30 16:28:45 -0400137 /* pixel -> fractional pixel */
138 if (format & xPlaDevice) {
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500139 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 +0200140 values++;
Behdad Esfahbod0090dc02009-07-30 16:28:45 -0400141 }
142 if (format & yPlaDevice) {
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500143 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 +0200144 values++;
Behdad Esfahbod0090dc02009-07-30 16:28:45 -0400145 }
146 if (format & xAdvDevice) {
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500147 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 +0200148 values++;
Behdad Esfahbod0090dc02009-07-30 16:28:45 -0400149 }
150 if (format & yAdvDevice) {
Behdad Esfahbodcc2086d2011-05-19 19:19:50 -0400151 /* y_advance values grow downward but font-space grows upward, hence negation */
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500152 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 +0200153 values++;
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400154 }
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500155 return ret;
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400156 }
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400157
158 private:
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330159 bool sanitize_value_devices (hb_sanitize_context_t *c, const void *base, const Value *values) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300160 {
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400161 unsigned int format = *this;
162
163 if (format & xPlacement) values++;
164 if (format & yPlacement) values++;
165 if (format & xAdvance) values++;
166 if (format & yAdvance) values++;
167
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400168 if ((format & xPlaDevice) && !get_device (values++).sanitize (c, base)) return false;
169 if ((format & yPlaDevice) && !get_device (values++).sanitize (c, base)) return false;
170 if ((format & xAdvDevice) && !get_device (values++).sanitize (c, base)) return false;
171 if ((format & yAdvDevice) && !get_device (values++).sanitize (c, base)) return false;
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400172
173 return true;
174 }
175
Behdad Esfahbod95df00a2019-04-12 17:50:03 -0400176 HB_INTERNAL static OffsetTo<Device>& get_device (Value* value)
Ebrahim Byagowi92588782019-04-30 13:05:10 -0700177 { return *CastP<OffsetTo<Device>> (value); }
Behdad Esfahbod95df00a2019-04-12 17:50:03 -0400178 HB_INTERNAL static const OffsetTo<Device>& get_device (const Value* value, bool *worked=nullptr)
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500179 {
Behdad Esfahbodb9a51f52018-12-21 15:05:00 -0500180 if (worked) *worked |= bool (*value);
Ebrahim Byagowi92588782019-04-30 13:05:10 -0700181 return *CastP<OffsetTo<Device>> (value);
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500182 }
Behdad Esfahbodc2ddfd22010-05-06 13:06:15 -0400183
Behdad Esfahbod95df00a2019-04-12 17:50:03 -0400184 HB_INTERNAL static const HBINT16& get_short (const Value* value, bool *worked=nullptr)
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500185 {
Behdad Esfahbodb9a51f52018-12-21 15:05:00 -0500186 if (worked) *worked |= bool (*value);
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500187 return *CastP<HBINT16> (value);
188 }
Behdad Esfahbodc2ddfd22010-05-06 13:06:15 -0400189
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400190 public:
191
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330192 bool has_device () const
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330193 {
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400194 unsigned int format = *this;
195 return (format & devices) != 0;
196 }
197
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330198 bool sanitize_value (hb_sanitize_context_t *c, const void *base, const Value *values) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300199 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500200 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100201 return_trace (c->check_range (values, get_size ()) && (!has_device () || sanitize_value_devices (c, base, values)));
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400202 }
203
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330204 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 +0300205 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500206 TRACE_SANITIZE (this);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400207 unsigned int len = get_len ();
208
Behdad Esfahbode0144052018-11-12 14:23:31 -0500209 if (!c->check_range (values, count, get_size ())) return_trace (false);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400210
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100211 if (!has_device ()) return_trace (true);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400212
213 for (unsigned int i = 0; i < count; i++) {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400214 if (!sanitize_value_devices (c, base, values))
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330215 return_trace (false);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400216 values += len;
217 }
218
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100219 return_trace (true);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400220 }
221
Behdad Esfahbod278a91f2010-04-22 13:59:39 -0400222 /* Just sanitize referenced Device tables. Doesn't check the values themselves. */
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330223 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 +0300224 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500225 TRACE_SANITIZE (this);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400226
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100227 if (!has_device ()) return_trace (true);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400228
229 for (unsigned int i = 0; i < count; i++) {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400230 if (!sanitize_value_devices (c, base, values))
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330231 return_trace (false);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400232 values += stride;
233 }
234
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100235 return_trace (true);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400236 }
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400237};
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400238
Qunxin Liu321d5582019-07-02 16:30:57 -0700239template<typename Iterator>
240static inline void SinglePos_serialize (hb_serialize_context_t *c,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430241 Iterator it,
242 ValueFormat valFormat);
Qunxin Liu321d5582019-07-02 16:30:57 -0700243
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400244
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400245struct AnchorFormat1
246{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330247 void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id HB_UNUSED,
248 float *x, float *y) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400249 {
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700250 hb_font_t *font = c->font;
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100251 *x = font->em_fscale_x (xCoordinate);
252 *y = font->em_fscale_y (yCoordinate);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400253 }
254
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330255 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300256 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500257 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100258 return_trace (c->check_struct (this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400259 }
Qunxin Liub0c3eb02019-09-18 16:11:44 -0700260
261 AnchorFormat1* copy (hb_serialize_context_t *c) const
262 {
263 TRACE_SERIALIZE (this);
264 return_trace (c->embed<AnchorFormat1> (this));
265 }
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400266
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400267 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100268 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbode347fb92018-02-26 01:08:00 -0800269 FWORD xCoordinate; /* Horizontal value--in design units */
270 FWORD yCoordinate; /* Vertical value--in design units */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400271 public:
272 DEFINE_SIZE_STATIC (6);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400273};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400274
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400275struct AnchorFormat2
276{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330277 void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id,
278 float *x, float *y) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400279 {
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700280 hb_font_t *font = c->font;
Behdad Esfahbodc8f529a2019-06-19 19:33:15 -0700281
282#ifdef HB_NO_HINTING
283 *x = font->em_fscale_x (xCoordinate);
284 *y = font->em_fscale_y (yCoordinate);
285 return;
286#endif
287
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700288 unsigned int x_ppem = font->x_ppem;
289 unsigned int y_ppem = font->y_ppem;
Ebrahim Byagowieaf64942018-04-23 18:39:40 +0430290 hb_position_t cx = 0, cy = 0;
Behdad Esfahbod385f78b2018-11-07 17:19:21 -0500291 bool ret;
Behdad Esfahbod6f729b42010-04-29 03:59:06 -0400292
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700293 ret = (x_ppem || y_ppem) &&
Behdad Esfahbod385f78b2018-11-07 17:19:21 -0500294 font->get_glyph_contour_point_for_origin (glyph_id, anchorPoint, HB_DIRECTION_LTR, &cx, &cy);
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100295 *x = ret && x_ppem ? cx : font->em_fscale_x (xCoordinate);
296 *y = ret && y_ppem ? cy : font->em_fscale_y (yCoordinate);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400297 }
298
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330299 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300300 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500301 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100302 return_trace (c->check_struct (this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400303 }
304
Qunxin Liub0c3eb02019-09-18 16:11:44 -0700305 AnchorFormat2* copy (hb_serialize_context_t *c) const
306 {
307 TRACE_SERIALIZE (this);
308 return_trace (c->embed<AnchorFormat2> (this));
309 }
310
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400311 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100312 HBUINT16 format; /* Format identifier--format = 2 */
Behdad Esfahbode347fb92018-02-26 01:08:00 -0800313 FWORD xCoordinate; /* Horizontal value--in design units */
314 FWORD yCoordinate; /* Vertical value--in design units */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100315 HBUINT16 anchorPoint; /* Index to glyph contour point */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400316 public:
317 DEFINE_SIZE_STATIC (8);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400318};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400319
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400320struct AnchorFormat3
321{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330322 void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id HB_UNUSED,
323 float *x, float *y) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400324 {
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700325 hb_font_t *font = c->font;
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100326 *x = font->em_fscale_x (xCoordinate);
327 *y = font->em_fscale_y (yCoordinate);
Behdad Esfahbodc18ec2b2009-05-21 04:54:01 -0400328
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700329 if (font->x_ppem || font->num_coords)
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700330 *x += (this+xDeviceTable).get_x_delta (font, c->var_store);
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700331 if (font->y_ppem || font->num_coords)
Behdad Esfahbod1124d2e2016-09-09 22:48:27 -0700332 *y += (this+yDeviceTable).get_y_delta (font, c->var_store);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400333 }
334
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330335 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300336 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500337 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100338 return_trace (c->check_struct (this) && xDeviceTable.sanitize (c, this) && yDeviceTable.sanitize (c, this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400339 }
340
Qunxin Liub0c3eb02019-09-18 16:11:44 -0700341 AnchorFormat3* copy (hb_serialize_context_t *c) const
342 {
343 TRACE_SERIALIZE (this);
344 auto *out = c->embed<AnchorFormat3> (this);
345 if (unlikely (!out)) return_trace (nullptr);
346
347 out->xDeviceTable.serialize_copy (c, xDeviceTable, this, out);
348 out->yDeviceTable.serialize_copy (c, yDeviceTable, this, out);
349 return_trace (out);
350 }
351
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400352 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100353 HBUINT16 format; /* Format identifier--format = 3 */
Behdad Esfahbode347fb92018-02-26 01:08:00 -0800354 FWORD xCoordinate; /* Horizontal value--in design units */
355 FWORD yCoordinate; /* Vertical value--in design units */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400356 OffsetTo<Device>
357 xDeviceTable; /* Offset to Device table for X
358 * coordinate-- from beginning of
359 * Anchor table (may be NULL) */
360 OffsetTo<Device>
361 yDeviceTable; /* Offset to Device table for Y
362 * coordinate-- from beginning of
363 * Anchor table (may be NULL) */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400364 public:
365 DEFINE_SIZE_STATIC (10);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400366};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400367
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400368struct Anchor
369{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330370 void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id,
371 float *x, float *y) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400372 {
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400373 *x = *y = 0;
374 switch (u.format) {
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700375 case 1: u.format1.get_anchor (c, glyph_id, x, y); return;
376 case 2: u.format2.get_anchor (c, glyph_id, x, y); return;
377 case 3: u.format3.get_anchor (c, glyph_id, x, y); return;
Behdad Esfahbod7cdd6ab2018-01-10 04:33:08 +0100378 default: return;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400379 }
380 }
381
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330382 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300383 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500384 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100385 if (!u.format.sanitize (c)) return_trace (false);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400386 switch (u.format) {
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100387 case 1: return_trace (u.format1.sanitize (c));
388 case 2: return_trace (u.format2.sanitize (c));
389 case 3: return_trace (u.format3.sanitize (c));
390 default:return_trace (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400391 }
392 }
393
Qunxin Liub0c3eb02019-09-18 16:11:44 -0700394 Anchor* copy (hb_serialize_context_t *c) const
395 {
396 TRACE_SERIALIZE (this);
397 switch (u.format) {
398 case 1: return_trace (reinterpret_cast<Anchor *> (u.format1.copy (c)));
399 case 2: return_trace (reinterpret_cast<Anchor *> (u.format2.copy (c)));
400 case 3: return_trace (reinterpret_cast<Anchor *> (u.format3.copy (c)));
401 default:return_trace (nullptr);
402 }
403 }
404
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400405 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400406 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100407 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400408 AnchorFormat1 format1;
409 AnchorFormat2 format2;
410 AnchorFormat3 format3;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400411 } u;
Behdad Esfahboded074222010-05-10 18:08:46 -0400412 public:
Behdad Esfahbod596e4712010-05-10 18:47:48 -0400413 DEFINE_SIZE_UNION (2, format);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400414};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400415
416
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400417struct AnchorMatrix
418{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330419 const Anchor& get_anchor (unsigned int row, unsigned int col,
420 unsigned int cols, bool *found) const
421 {
Behdad Esfahbode95e0312013-01-08 16:15:46 -0600422 *found = false;
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400423 if (unlikely (row >= rows || col >= cols)) return Null(Anchor);
Behdad Esfahbod093c5202014-12-12 21:07:53 -0800424 *found = !matrixZ[row * cols + col].is_null ();
425 return this+matrixZ[row * cols + col];
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400426 }
427
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330428 bool sanitize (hb_sanitize_context_t *c, unsigned int cols) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300429 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500430 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100431 if (!c->check_struct (this)) return_trace (false);
Behdad Esfahbodbddeb2b2018-07-10 14:12:37 +0200432 if (unlikely (hb_unsigned_mul_overflows (rows, cols))) return_trace (false);
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400433 unsigned int count = rows * cols;
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200434 if (!c->check_array (matrixZ.arrayZ, count)) return_trace (false);
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400435 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100436 if (!matrixZ[i].sanitize (c, this)) return_trace (false);
437 return_trace (true);
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400438 }
439
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100440 HBUINT16 rows; /* Number of rows */
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400441 protected:
Ebrahim Byagowi92588782019-04-30 13:05:10 -0700442 UnsizedArrayOf<OffsetTo<Anchor>>
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200443 matrixZ; /* Matrix of offsets to Anchor tables--
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400444 * from beginning of AnchorMatrix table */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400445 public:
Behdad Esfahbod093c5202014-12-12 21:07:53 -0800446 DEFINE_SIZE_ARRAY (2, matrixZ);
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400447};
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400448
449
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400450struct MarkRecord
451{
Behdad Esfahbod377bfc52009-05-21 04:58:24 -0400452 friend struct MarkArray;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400453
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330454 bool sanitize (hb_sanitize_context_t *c, const void *base) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300455 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500456 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100457 return_trace (c->check_struct (this) && markAnchor.sanitize (c, base));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400458 }
459
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400460 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100461 HBUINT16 klass; /* Class defined for this mark */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400462 OffsetTo<Anchor>
463 markAnchor; /* Offset to Anchor table--from
464 * beginning of MarkArray table */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400465 public:
466 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400467};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400468
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400469struct MarkArray : ArrayOf<MarkRecord> /* Array of MarkRecords--in Coverage order */
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400470{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330471 bool apply (hb_ot_apply_context_t *c,
472 unsigned int mark_index, unsigned int glyph_index,
473 const AnchorMatrix &anchors, unsigned int class_count,
474 unsigned int glyph_pos) const
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400475 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500476 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200477 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400478 const MarkRecord &record = ArrayOf<MarkRecord>::operator[](mark_index);
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400479 unsigned int mark_class = record.klass;
480
481 const Anchor& mark_anchor = this + record.markAnchor;
Behdad Esfahbode95e0312013-01-08 16:15:46 -0600482 bool found;
483 const Anchor& glyph_anchor = anchors.get_anchor (glyph_index, mark_class, class_count, &found);
484 /* If this subtable doesn't have an anchor for this base and this class,
485 * return false such that the subsequent subtables have a chance at it. */
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100486 if (unlikely (!found)) return_trace (false);
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400487
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100488 float mark_x, mark_y, base_x, base_y;
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400489
Behdad Esfahbodb65aa1c2017-08-10 20:32:02 -0700490 buffer->unsafe_to_break (glyph_pos, buffer->idx);
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700491 mark_anchor.get_anchor (c, buffer->cur().codepoint, &mark_x, &mark_y);
492 glyph_anchor.get_anchor (c, buffer->info[glyph_pos].codepoint, &base_x, &base_y);
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400493
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200494 hb_glyph_position_t &o = buffer->cur_pos();
Behdad Esfahboddfc57802019-05-07 23:26:09 -0700495 o.x_offset = roundf (base_x - mark_x);
496 o.y_offset = roundf (base_y - mark_y);
Behdad Esfahbod686567b2016-02-11 15:25:28 +0700497 o.attach_type() = ATTACH_TYPE_MARK;
Behdad Esfahbod0f6278d2016-02-11 14:49:10 +0700498 o.attach_chain() = (int) glyph_pos - (int) buffer->idx;
Behdad Esfahbod550bd142015-11-04 22:58:58 -0800499 buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400500
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200501 buffer->idx++;
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100502 return_trace (true);
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400503 }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400504
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330505 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300506 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500507 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100508 return_trace (ArrayOf<MarkRecord>::sanitize (c, this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400509 }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400510};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400511
512
513/* Lookups */
514
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400515struct SinglePosFormat1
516{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330517 bool intersects (const hb_set_t *glyphs) const
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -0700518 { return (this+coverage).intersects (glyphs); }
519
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330520 void collect_glyphs (hb_collect_glyphs_context_t *c) const
Behdad Esfahbod89bcfb22019-01-18 14:59:18 -0500521 { if (unlikely (!(this+coverage).add_coverage (c->input))) return; }
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500522
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330523 const Coverage &get_coverage () const { return this+coverage; }
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400524
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330525 bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400526 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500527 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200528 hb_buffer_t *buffer = c->buffer;
529 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100530 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400531
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700532 valueFormat.apply_value (c, this, values, buffer->cur_pos());
Behdad Esfahbodf53d4342009-05-30 22:17:32 -0400533
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200534 buffer->idx++;
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100535 return_trace (true);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400536 }
537
Qunxin Liu321d5582019-07-02 16:30:57 -0700538 template<typename Iterator,
539 hb_requires (hb_is_iterator (Iterator))>
540 void serialize (hb_serialize_context_t *c,
541 Iterator it,
542 ValueFormat valFormat)
543 {
544 if (unlikely (!c->extend_min (*this))) return;
545 if (unlikely (!c->check_assign (valueFormat, valFormat))) return;
546
Ebrahim Byagowicbc06462019-09-24 12:08:12 +0330547 for (const auto &_ : hb_second (*it))
Ebrahim Byagowi6e42a412019-09-21 16:25:54 +0430548 c->copy (_);
Qunxin Liu321d5582019-07-02 16:30:57 -0700549
550 auto glyphs =
551 + it
552 | hb_map_retains_sorting (hb_first)
553 ;
554
555 coverage.serialize (c, this).serialize (c, glyphs);
556 }
557
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330558 bool subset (hb_subset_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -0700559 {
560 TRACE_SUBSET (this);
Qunxin Liu321d5582019-07-02 16:30:57 -0700561 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
562 const hb_map_t &glyph_map = *c->plan->glyph_map;
563
Qunxin Liu321d5582019-07-02 16:30:57 -0700564 auto it =
565 + hb_iter (this+coverage)
566 | hb_filter (glyphset)
Behdad Esfahbod3c812462019-09-01 19:25:50 -0500567 | hb_map_retains_sorting (glyph_map)
568 | hb_zip (hb_repeat (values.as_array (valueFormat.get_len ())))
Qunxin Liu321d5582019-07-02 16:30:57 -0700569 ;
570
571 bool ret = bool (it);
572 SinglePos_serialize (c->serializer, it, valueFormat);
573 return_trace (ret);
Behdad Esfahbod339d3602018-09-03 17:33:34 -0700574 }
575
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330576 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300577 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500578 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100579 return_trace (c->check_struct (this) &&
580 coverage.sanitize (c, this) &&
581 valueFormat.sanitize_value (c, this, values));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400582 }
583
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400584 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100585 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400586 OffsetTo<Coverage>
587 coverage; /* Offset to Coverage table--from
588 * beginning of subtable */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400589 ValueFormat valueFormat; /* Defines the types of data in the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400590 * ValueRecord */
591 ValueRecord values; /* Defines positioning
592 * value(s)--applied to all glyphs in
593 * the Coverage table */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400594 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400595 DEFINE_SIZE_ARRAY (6, values);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400596};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400597
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400598struct SinglePosFormat2
599{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330600 bool intersects (const hb_set_t *glyphs) const
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -0700601 { return (this+coverage).intersects (glyphs); }
602
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330603 void collect_glyphs (hb_collect_glyphs_context_t *c) const
Behdad Esfahbod89bcfb22019-01-18 14:59:18 -0500604 { if (unlikely (!(this+coverage).add_coverage (c->input))) return; }
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500605
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330606 const Coverage &get_coverage () const { return this+coverage; }
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400607
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330608 bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400609 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500610 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200611 hb_buffer_t *buffer = c->buffer;
612 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100613 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400614
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100615 if (likely (index >= valueCount)) return_trace (false);
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400616
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700617 valueFormat.apply_value (c, this,
Behdad Esfahbod4b8487d2010-03-16 03:46:17 -0400618 &values[index * valueFormat.get_len ()],
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200619 buffer->cur_pos());
Behdad Esfahbodf53d4342009-05-30 22:17:32 -0400620
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200621 buffer->idx++;
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100622 return_trace (true);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400623 }
624
Qunxin Liu321d5582019-07-02 16:30:57 -0700625 template<typename Iterator,
626 hb_requires (hb_is_iterator (Iterator))>
627 void serialize (hb_serialize_context_t *c,
628 Iterator it,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430629 ValueFormat valFormat)
Qunxin Liu321d5582019-07-02 16:30:57 -0700630 {
631 if (unlikely (!c->extend_min (*this))) return;
632 if (unlikely (!c->check_assign (valueFormat, valFormat))) return;
633 if (unlikely (!c->check_assign (valueCount, it.len ()))) return;
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430634
Ebrahim Byagowi68d39ea2019-09-24 13:52:46 +0330635 for (const auto iter : it)
Ebrahim Byagowicbc06462019-09-24 12:08:12 +0330636 for (const auto &_ : iter.second)
Ebrahim Byagowi6e42a412019-09-21 16:25:54 +0430637 c->copy (_);
Qunxin Liu321d5582019-07-02 16:30:57 -0700638
639 auto glyphs =
640 + it
641 | hb_map_retains_sorting (hb_first)
642 ;
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430643
Qunxin Liu321d5582019-07-02 16:30:57 -0700644 coverage.serialize (c, this).serialize (c, glyphs);
645 }
646
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330647 bool subset (hb_subset_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -0700648 {
649 TRACE_SUBSET (this);
Qunxin Liu321d5582019-07-02 16:30:57 -0700650 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
651 const hb_map_t &glyph_map = *c->plan->glyph_map;
652
653 unsigned sub_length = valueFormat.get_len ();
Behdad Esfahbode0e0c8c2019-08-29 14:58:16 -0700654 auto values_array = values.as_array (valueCount * sub_length);
Qunxin Liu321d5582019-07-02 16:30:57 -0700655
656 auto it =
657 + hb_zip (this+coverage, hb_range ((unsigned) valueCount))
658 | hb_filter (glyphset, hb_first)
659 | hb_map_retains_sorting ([&] (const hb_pair_t<hb_codepoint_t, unsigned>& _)
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430660 {
Behdad Esfahbode0e0c8c2019-08-29 14:58:16 -0700661 return hb_pair (glyph_map[_.first],
662 values_array.sub_array (_.second * sub_length,
663 sub_length));
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430664 })
Qunxin Liu321d5582019-07-02 16:30:57 -0700665 ;
666
667 bool ret = bool (it);
668 SinglePos_serialize (c->serializer, it, valueFormat);
669 return_trace (ret);
Behdad Esfahbod339d3602018-09-03 17:33:34 -0700670 }
671
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330672 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300673 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500674 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100675 return_trace (c->check_struct (this) &&
676 coverage.sanitize (c, this) &&
677 valueFormat.sanitize_values (c, this, values, valueCount));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400678 }
679
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400680 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100681 HBUINT16 format; /* Format identifier--format = 2 */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400682 OffsetTo<Coverage>
683 coverage; /* Offset to Coverage table--from
684 * beginning of subtable */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400685 ValueFormat valueFormat; /* Defines the types of data in the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400686 * ValueRecord */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100687 HBUINT16 valueCount; /* Number of ValueRecords */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400688 ValueRecord values; /* Array of ValueRecords--positioning
689 * values applied to glyphs */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400690 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400691 DEFINE_SIZE_ARRAY (8, values);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400692};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400693
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400694struct SinglePos
695{
Qunxin Liu321d5582019-07-02 16:30:57 -0700696 template<typename Iterator,
697 hb_requires (hb_is_iterator (Iterator))>
698 unsigned get_format (Iterator glyph_val_iter_pairs)
699 {
Qunxin Liu321d5582019-07-02 16:30:57 -0700700 hb_array_t<const Value> first_val_iter = hb_second (*glyph_val_iter_pairs);
701
Ebrahim Byagowi68d39ea2019-09-24 13:52:46 +0330702 for (const auto iter : glyph_val_iter_pairs)
703 for (const auto _ : hb_zip (iter.second, first_val_iter))
Ebrahim Byagowi6e42a412019-09-21 16:25:54 +0430704 if (_.first != _.second)
Ebrahim Byagowic1a585f2019-09-21 23:30:52 +0430705 return 2;
Qunxin Liu321d5582019-07-02 16:30:57 -0700706
Ebrahim Byagowic1a585f2019-09-21 23:30:52 +0430707 return 1;
Qunxin Liu321d5582019-07-02 16:30:57 -0700708 }
709
710
711 template<typename Iterator,
712 hb_requires (hb_is_iterator (Iterator))>
713 void serialize (hb_serialize_context_t *c,
714 Iterator glyph_val_iter_pairs,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430715 ValueFormat valFormat)
Qunxin Liu321d5582019-07-02 16:30:57 -0700716 {
717 if (unlikely (!c->extend_min (u.format))) return;
718 unsigned format = 2;
719
720 if (glyph_val_iter_pairs) format = get_format (glyph_val_iter_pairs);
721
722 u.format = format;
723 switch (u.format) {
724 case 1: u.format1.serialize (c, glyph_val_iter_pairs, valFormat);
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430725 return;
Qunxin Liu321d5582019-07-02 16:30:57 -0700726 case 2: u.format2.serialize (c, glyph_val_iter_pairs, valFormat);
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430727 return;
Qunxin Liu321d5582019-07-02 16:30:57 -0700728 default:return;
729 }
730 }
731
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -0700732 template <typename context_t, typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -0700733 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400734 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -0800735 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -0400736 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400737 switch (u.format) {
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -0700738 case 1: return_trace (c->dispatch (u.format1, hb_forward<Ts> (ds)...));
739 case 2: return_trace (c->dispatch (u.format2, hb_forward<Ts> (ds)...));
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100740 default:return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400741 }
742 }
743
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400744 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400745 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100746 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400747 SinglePosFormat1 format1;
748 SinglePosFormat2 format2;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400749 } u;
750};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400751
Qunxin Liu321d5582019-07-02 16:30:57 -0700752template<typename Iterator>
753static inline void
754SinglePos_serialize (hb_serialize_context_t *c,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430755 Iterator it,
756 ValueFormat valFormat)
Qunxin Liu321d5582019-07-02 16:30:57 -0700757{ c->start_embed<SinglePos> ()->serialize (c, it, valFormat); }
758
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400759
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400760struct PairValueRecord
761{
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400762 friend struct PairSet;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400763
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400764 protected:
Ebrahim Byagowid5120872019-09-14 10:36:29 +0430765 HBGlyphID secondGlyph; /* GlyphID of second glyph in the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400766 * pair--first glyph is listed in the
767 * Coverage table */
768 ValueRecord values; /* Positioning data for the first glyph
769 * followed by for second glyph */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400770 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400771 DEFINE_SIZE_ARRAY (2, values);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400772};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400773
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400774struct PairSet
775{
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400776 friend struct PairPosFormat1;
777
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330778 bool intersects (const hb_set_t *glyphs,
Behdad Esfahbod9d8c7202019-03-29 21:59:28 -0700779 const ValueFormat *valueFormats) const
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -0700780 {
Behdad Esfahbodfb059082018-11-30 20:45:40 -0500781 unsigned int len1 = valueFormats[0].get_len ();
782 unsigned int len2 = valueFormats[1].get_len ();
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -0700783 unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);
784
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200785 const PairValueRecord *record = &firstPairValueRecord;
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -0700786 unsigned int count = len;
787 for (unsigned int i = 0; i < count; i++)
788 {
789 if (glyphs->has (record->secondGlyph))
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330790 return true;
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -0700791 record = &StructAtOffset<const PairValueRecord> (record, record_size);
792 }
793 return false;
794 }
795
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330796 void collect_glyphs (hb_collect_glyphs_context_t *c,
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500797 const ValueFormat *valueFormats) const
798 {
Behdad Esfahbodfb059082018-11-30 20:45:40 -0500799 unsigned int len1 = valueFormats[0].get_len ();
800 unsigned int len2 = valueFormats[1].get_len ();
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100801 unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500802
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200803 const PairValueRecord *record = &firstPairValueRecord;
Behdad Esfahbod81f27df2017-12-16 06:12:06 -0800804 c->input->add_array (&record->secondGlyph, len, record_size);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500805 }
806
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330807 bool apply (hb_ot_apply_context_t *c,
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400808 const ValueFormat *valueFormats,
809 unsigned int pos) const
810 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500811 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200812 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbodfb059082018-11-30 20:45:40 -0500813 unsigned int len1 = valueFormats[0].get_len ();
814 unsigned int len2 = valueFormats[1].get_len ();
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100815 unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400816
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500817 unsigned int count = len;
Behdad Esfahbod67dfa8c2015-01-19 17:00:31 -0800818
819 /* Hand-coded bsearch. */
820 if (unlikely (!count))
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100821 return_trace (false);
Behdad Esfahbod67dfa8c2015-01-19 17:00:31 -0800822 hb_codepoint_t x = buffer->info[pos].codepoint;
823 int min = 0, max = (int) count - 1;
824 while (min <= max)
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400825 {
Behdad Esfahbod21ede862018-10-25 13:19:34 -0700826 int mid = ((unsigned int) min + (unsigned int) max) / 2;
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200827 const PairValueRecord *record = &StructAtOffset<PairValueRecord> (&firstPairValueRecord, record_size * mid);
Behdad Esfahbod67dfa8c2015-01-19 17:00:31 -0800828 hb_codepoint_t mid_x = record->secondGlyph;
829 if (x < mid_x)
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330830 max = mid - 1;
Behdad Esfahbod67dfa8c2015-01-19 17:00:31 -0800831 else if (x > mid_x)
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330832 min = mid + 1;
Behdad Esfahbod67dfa8c2015-01-19 17:00:31 -0800833 else
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400834 {
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500835 /* Note the intentional use of "|" instead of short-circuit "||". */
Behdad Esfahbodfb059082018-11-30 20:45:40 -0500836 if (valueFormats[0].apply_value (c, this, &record->values[0], buffer->cur_pos()) |
837 valueFormats[1].apply_value (c, this, &record->values[len1], buffer->pos[pos]))
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500838 buffer->unsafe_to_break (buffer->idx, pos + 1);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400839 if (len2)
840 pos++;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200841 buffer->idx = pos;
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100842 return_trace (true);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400843 }
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400844 }
845
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100846 return_trace (false);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400847 }
848
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -0700849 struct sanitize_closure_t
850 {
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300851 const void *base;
852 const ValueFormat *valueFormats;
Behdad Esfahbodfb059082018-11-30 20:45:40 -0500853 unsigned int len1; /* valueFormats[0].get_len() */
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400854 unsigned int stride; /* 1 + len1 + len2 */
855 };
856
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330857 bool sanitize (hb_sanitize_context_t *c, const sanitize_closure_t *closure) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300858 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500859 TRACE_SANITIZE (this);
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400860 if (!(c->check_struct (this)
Behdad Esfahbode0144052018-11-12 14:23:31 -0500861 && c->check_range (&firstPairValueRecord,
862 len,
863 HBUINT16::static_size,
864 closure->stride))) return_trace (false);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400865
866 unsigned int count = len;
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200867 const PairValueRecord *record = &firstPairValueRecord;
Behdad Esfahbodfb059082018-11-30 20:45:40 -0500868 return_trace (closure->valueFormats[0].sanitize_values_stride_unsafe (c, closure->base, &record->values[0], count, closure->stride) &&
869 closure->valueFormats[1].sanitize_values_stride_unsafe (c, closure->base, &record->values[closure->len1], count, closure->stride));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400870 }
871
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400872 protected:
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200873 HBUINT16 len; /* Number of PairValueRecords */
874 PairValueRecord firstPairValueRecord;
875 /* Array of PairValueRecords--ordered
876 * by GlyphID of the second glyph */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400877 public:
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200878 DEFINE_SIZE_MIN (2);
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400879};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400880
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400881struct PairPosFormat1
882{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330883 bool intersects (const hb_set_t *glyphs) const
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -0700884 {
Behdad Esfahbodf5ef8a72019-03-29 21:57:17 -0700885 return
886 + hb_zip (this+coverage, pairSet)
887 | hb_filter (*glyphs, hb_first)
888 | hb_map (hb_second)
Ali Javadic1841802019-07-16 22:10:24 +0430889 | hb_map ([glyphs, this] (const OffsetTo<PairSet> &_)
Behdad Esfahbodf5ef8a72019-03-29 21:57:17 -0700890 { return (this+_).intersects (glyphs, valueFormat); })
891 | hb_any
892 ;
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -0700893 }
894
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330895 void collect_glyphs (hb_collect_glyphs_context_t *c) const
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500896 {
Behdad Esfahbod81f27df2017-12-16 06:12:06 -0800897 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500898 unsigned int count = pairSet.len;
899 for (unsigned int i = 0; i < count; i++)
Volker H. Simonisad273882016-12-14 18:56:17 +0100900 (this+pairSet[i]).collect_glyphs (c, valueFormat);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500901 }
902
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330903 const Coverage &get_coverage () const { return this+coverage; }
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400904
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330905 bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400906 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500907 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200908 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200909 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100910 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400911
Behdad Esfahbodfd034492018-01-17 16:46:51 -0800912 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
Behdad Esfahbodb051be52015-01-29 13:40:39 +0100913 skippy_iter.reset (buffer->idx, 1);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100914 if (!skippy_iter.next ()) return_trace (false);
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400915
Volker H. Simonisad273882016-12-14 18:56:17 +0100916 return_trace ((this+pairSet[index]).apply (c, valueFormat, skippy_iter.idx));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400917 }
918
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330919 bool subset (hb_subset_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -0700920 {
921 TRACE_SUBSET (this);
922 // TODO(subset)
923 return_trace (false);
924 }
925
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330926 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300927 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500928 TRACE_SANITIZE (this);
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -0400929
Behdad Esfahbodf9666492015-10-13 00:30:50 -0400930 if (!c->check_struct (this)) return_trace (false);
931
Behdad Esfahbodfb059082018-11-30 20:45:40 -0500932 unsigned int len1 = valueFormat[0].get_len ();
933 unsigned int len2 = valueFormat[1].get_len ();
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -0700934 PairSet::sanitize_closure_t closure =
935 {
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400936 this,
Volker H. Simonisad273882016-12-14 18:56:17 +0100937 valueFormat,
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400938 len1,
939 1 + len1 + len2
940 };
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -0400941
Behdad Esfahbodf9666492015-10-13 00:30:50 -0400942 return_trace (coverage.sanitize (c, this) && pairSet.sanitize (c, this, &closure));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400943 }
944
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400945 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100946 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400947 OffsetTo<Coverage>
948 coverage; /* Offset to Coverage table--from
949 * beginning of subtable */
Behdad Esfahbodfb059082018-11-30 20:45:40 -0500950 ValueFormat valueFormat[2]; /* [0] Defines the types of data in
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400951 * ValueRecord1--for the first glyph
952 * in the pair--may be zero (0) */
Behdad Esfahbodfb059082018-11-30 20:45:40 -0500953 /* [1] Defines the types of data in
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400954 * ValueRecord2--for the second glyph
955 * in the pair--may be zero (0) */
956 OffsetArrayOf<PairSet>
957 pairSet; /* Array of PairSet tables
958 * ordered by Coverage Index */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400959 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400960 DEFINE_SIZE_ARRAY (10, pairSet);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400961};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400962
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400963struct PairPosFormat2
964{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330965 bool intersects (const hb_set_t *glyphs) const
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -0700966 {
967 return (this+coverage).intersects (glyphs) &&
968 (this+classDef2).intersects (glyphs);
969 }
970
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330971 void collect_glyphs (hb_collect_glyphs_context_t *c) const
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500972 {
Behdad Esfahbod81f27df2017-12-16 06:12:06 -0800973 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
Behdad Esfahbod71e6adf2017-12-16 11:07:37 -0500974 if (unlikely (!(this+classDef2).add_coverage (c->input))) return;
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500975 }
976
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330977 const Coverage &get_coverage () const { return this+coverage; }
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400978
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330979 bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400980 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500981 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200982 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200983 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100984 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400985
Behdad Esfahbodfd034492018-01-17 16:46:51 -0800986 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
Behdad Esfahbodb051be52015-01-29 13:40:39 +0100987 skippy_iter.reset (buffer->idx, 1);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100988 if (!skippy_iter.next ()) return_trace (false);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400989
990 unsigned int len1 = valueFormat1.get_len ();
991 unsigned int len2 = valueFormat2.get_len ();
992 unsigned int record_len = len1 + len2;
993
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200994 unsigned int klass1 = (this+classDef1).get_class (buffer->cur().codepoint);
995 unsigned int klass2 = (this+classDef2).get_class (buffer->info[skippy_iter.idx].codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100996 if (unlikely (klass1 >= class1Count || klass2 >= class2Count)) return_trace (false);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400997
Behdad Esfahbod4b8487d2010-03-16 03:46:17 -0400998 const Value *v = &values[record_len * (klass1 * class2Count + klass2)];
Behdad Esfahbod6ee6cd92018-11-07 15:40:55 -0500999 /* Note the intentional use of "|" instead of short-circuit "||". */
1000 if (valueFormat1.apply_value (c, this, v, buffer->cur_pos()) |
1001 valueFormat2.apply_value (c, this, v + len1, buffer->pos[skippy_iter.idx]))
1002 buffer->unsafe_to_break (buffer->idx, skippy_iter.idx + 1);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -04001003
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001004 buffer->idx = skippy_iter.idx;
Behdad Esfahbod70632ad2009-05-19 22:30:09 -04001005 if (len2)
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001006 buffer->idx++;
Behdad Esfahbod70632ad2009-05-19 22:30:09 -04001007
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001008 return_trace (true);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001009 }
1010
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301011 bool subset (hb_subset_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -07001012 {
1013 TRACE_SUBSET (this);
1014 // TODO(subset)
1015 return_trace (false);
1016 }
1017
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301018 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001019 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001020 TRACE_SANITIZE (this);
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001021 if (!(c->check_struct (this)
1022 && coverage.sanitize (c, this)
1023 && classDef1.sanitize (c, this)
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001024 && classDef2.sanitize (c, this))) return_trace (false);
Behdad Esfahbod815a73e2009-08-14 17:31:16 -04001025
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -04001026 unsigned int len1 = valueFormat1.get_len ();
1027 unsigned int len2 = valueFormat2.get_len ();
1028 unsigned int stride = len1 + len2;
Behdad Esfahbod4b8487d2010-03-16 03:46:17 -04001029 unsigned int record_size = valueFormat1.get_size () + valueFormat2.get_size ();
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -04001030 unsigned int count = (unsigned int) class1Count * (unsigned int) class2Count;
Behdad Esfahbode0144052018-11-12 14:23:31 -05001031 return_trace (c->check_range ((const void *) values,
1032 count,
1033 record_size) &&
Behdad Esfahbodfb059082018-11-30 20:45:40 -05001034 valueFormat1.sanitize_values_stride_unsafe (c, this, &values[0], count, stride) &&
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001035 valueFormat2.sanitize_values_stride_unsafe (c, this, &values[len1], count, stride));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001036 }
Behdad Esfahbod70632ad2009-05-19 22:30:09 -04001037
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001038 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001039 HBUINT16 format; /* Format identifier--format = 2 */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001040 OffsetTo<Coverage>
1041 coverage; /* Offset to Coverage table--from
1042 * beginning of subtable */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -04001043 ValueFormat valueFormat1; /* ValueRecord definition--for the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001044 * first glyph of the pair--may be zero
1045 * (0) */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -04001046 ValueFormat valueFormat2; /* ValueRecord definition--for the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001047 * second glyph of the pair--may be
1048 * zero (0) */
1049 OffsetTo<ClassDef>
1050 classDef1; /* Offset to ClassDef table--from
1051 * beginning of PairPos subtable--for
1052 * the first glyph of the pair */
1053 OffsetTo<ClassDef>
1054 classDef2; /* Offset to ClassDef table--from
1055 * beginning of PairPos subtable--for
1056 * the second glyph of the pair */
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001057 HBUINT16 class1Count; /* Number of classes in ClassDef1
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001058 * table--includes Class0 */
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001059 HBUINT16 class2Count; /* Number of classes in ClassDef2
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001060 * table--includes Class0 */
1061 ValueRecord values; /* Matrix of value pairs:
1062 * class1-major, class2-minor,
1063 * Each entry has value1 and value2 */
Behdad Esfahbod569da922010-05-10 16:38:32 -04001064 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001065 DEFINE_SIZE_ARRAY (16, values);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001066};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001067
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001068struct PairPos
1069{
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07001070 template <typename context_t, typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -07001071 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001072 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08001073 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04001074 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001075 switch (u.format) {
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07001076 case 1: return_trace (c->dispatch (u.format1, hb_forward<Ts> (ds)...));
1077 case 2: return_trace (c->dispatch (u.format2, hb_forward<Ts> (ds)...));
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001078 default:return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001079 }
1080 }
1081
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001082 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001083 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001084 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001085 PairPosFormat1 format1;
1086 PairPosFormat2 format2;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001087 } u;
1088};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001089
1090
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001091struct EntryExitRecord
1092{
Behdad Esfahbod569da922010-05-10 16:38:32 -04001093 friend struct CursivePosFormat1;
1094
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301095 bool sanitize (hb_sanitize_context_t *c, const void *base) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001096 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001097 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001098 return_trace (entryAnchor.sanitize (c, base) && exitAnchor.sanitize (c, base));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001099 }
1100
Qunxin Liub0c3eb02019-09-18 16:11:44 -07001101 EntryExitRecord* copy (hb_serialize_context_t *c,
1102 const void *src_base,
1103 const void *dst_base) const
1104 {
1105 TRACE_SERIALIZE (this);
1106 auto *out = c->embed (this);
1107 if (unlikely (!out)) return_trace (nullptr);
1108
1109 out->entryAnchor.serialize_copy (c, entryAnchor, src_base, dst_base);
1110 out->exitAnchor.serialize_copy (c, exitAnchor, src_base, dst_base);
1111 return_trace (out);
1112 }
1113
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001114 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001115 OffsetTo<Anchor>
1116 entryAnchor; /* Offset to EntryAnchor table--from
1117 * beginning of CursivePos
1118 * subtable--may be NULL */
1119 OffsetTo<Anchor>
1120 exitAnchor; /* Offset to ExitAnchor table--from
1121 * beginning of CursivePos
1122 * subtable--may be NULL */
Behdad Esfahbod569da922010-05-10 16:38:32 -04001123 public:
1124 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001125};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001126
Behdad Esfahbod65785752015-08-25 20:24:59 +01001127static void
1128reverse_cursive_minor_offset (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction, unsigned int new_parent);
1129
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001130struct CursivePosFormat1
1131{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301132 bool intersects (const hb_set_t *glyphs) const
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -07001133 { return (this+coverage).intersects (glyphs); }
1134
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301135 void collect_glyphs (hb_collect_glyphs_context_t *c) const
Behdad Esfahbod89bcfb22019-01-18 14:59:18 -05001136 { if (unlikely (!(this+coverage).add_coverage (c->input))) return; }
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001137
Ebrahim Byagowie4120082018-12-17 21:31:01 +03301138 const Coverage &get_coverage () const { return this+coverage; }
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001139
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301140 bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -04001141 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001142 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001143 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbod0f7e6b22009-05-20 04:16:35 -04001144
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001145 const EntryExitRecord &this_record = entryExitRecord[(this+coverage).get_coverage (buffer->cur().codepoint)];
Behdad Esfahbodbf8469b2018-10-11 20:45:00 -04001146 if (!this_record.entryAnchor) return_trace (false);
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -04001147
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001148 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
Behdad Esfahbodb051be52015-01-29 13:40:39 +01001149 skippy_iter.reset (buffer->idx, 1);
Behdad Esfahbodbf8469b2018-10-11 20:45:00 -04001150 if (!skippy_iter.prev ()) return_trace (false);
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -04001151
Behdad Esfahbodbf8469b2018-10-11 20:45:00 -04001152 const EntryExitRecord &prev_record = entryExitRecord[(this+coverage).get_coverage (buffer->info[skippy_iter.idx].codepoint)];
1153 if (!prev_record.exitAnchor) return_trace (false);
Behdad Esfahbodaf5d02a2010-10-27 11:54:26 -04001154
Behdad Esfahbodbf8469b2018-10-11 20:45:00 -04001155 unsigned int i = skippy_iter.idx;
1156 unsigned int j = buffer->idx;
Behdad Esfahbodaf5d02a2010-10-27 11:54:26 -04001157
Behdad Esfahbod882ebda2017-08-10 20:23:17 -07001158 buffer->unsafe_to_break (i, j);
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +01001159 float entry_x, entry_y, exit_x, exit_y;
Behdad Esfahbodbf8469b2018-10-11 20:45:00 -04001160 (this+prev_record.exitAnchor).get_anchor (c, buffer->info[i].codepoint, &exit_x, &exit_y);
1161 (this+this_record.entryAnchor).get_anchor (c, buffer->info[j].codepoint, &entry_x, &entry_y);
Behdad Esfahbodcc83ae12009-05-27 00:17:37 -04001162
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001163 hb_glyph_position_t *pos = buffer->pos;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001164
1165 hb_position_t d;
1166 /* Main-direction adjustment */
1167 switch (c->direction) {
1168 case HB_DIRECTION_LTR:
Behdad Esfahboddfc57802019-05-07 23:26:09 -07001169 pos[i].x_advance = roundf (exit_x) + pos[i].x_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001170
Behdad Esfahboddfc57802019-05-07 23:26:09 -07001171 d = roundf (entry_x) + pos[j].x_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001172 pos[j].x_advance -= d;
1173 pos[j].x_offset -= d;
1174 break;
1175 case HB_DIRECTION_RTL:
Behdad Esfahboddfc57802019-05-07 23:26:09 -07001176 d = roundf (exit_x) + pos[i].x_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001177 pos[i].x_advance -= d;
1178 pos[i].x_offset -= d;
1179
Behdad Esfahboddfc57802019-05-07 23:26:09 -07001180 pos[j].x_advance = roundf (entry_x) + pos[j].x_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001181 break;
1182 case HB_DIRECTION_TTB:
Behdad Esfahboddfc57802019-05-07 23:26:09 -07001183 pos[i].y_advance = roundf (exit_y) + pos[i].y_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001184
Behdad Esfahboddfc57802019-05-07 23:26:09 -07001185 d = roundf (entry_y) + pos[j].y_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001186 pos[j].y_advance -= d;
1187 pos[j].y_offset -= d;
1188 break;
1189 case HB_DIRECTION_BTT:
Behdad Esfahboddfc57802019-05-07 23:26:09 -07001190 d = roundf (exit_y) + pos[i].y_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001191 pos[i].y_advance -= d;
1192 pos[i].y_offset -= d;
1193
Behdad Esfahboddfc57802019-05-07 23:26:09 -07001194 pos[j].y_advance = roundf (entry_y);
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001195 break;
1196 case HB_DIRECTION_INVALID:
1197 default:
1198 break;
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -04001199 }
1200
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001201 /* Cross-direction adjustment */
Behdad Esfahbod58f2a732015-08-25 18:55:34 +01001202
1203 /* We attach child to parent (think graph theory and rooted trees whereas
1204 * the root stays on baseline and each node aligns itself against its
1205 * parent.
1206 *
1207 * Optimize things for the case of RightToLeft, as that's most common in
Behdad Esfahbodbf8469b2018-10-11 20:45:00 -04001208 * Arabic. */
Behdad Esfahbod58f2a732015-08-25 18:55:34 +01001209 unsigned int child = i;
1210 unsigned int parent = j;
1211 hb_position_t x_offset = entry_x - exit_x;
1212 hb_position_t y_offset = entry_y - exit_y;
1213 if (!(c->lookup_props & LookupFlag::RightToLeft))
1214 {
1215 unsigned int k = child;
1216 child = parent;
1217 parent = k;
1218 x_offset = -x_offset;
1219 y_offset = -y_offset;
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -04001220 }
1221
Behdad Esfahbod65785752015-08-25 20:24:59 +01001222 /* If child was already connected to someone else, walk through its old
1223 * chain and reverse the link direction, such that the whole tree of its
1224 * previous connection now attaches to new parent. Watch out for case
1225 * where new parent is on the path from old chain...
1226 */
1227 reverse_cursive_minor_offset (pos, child, c->direction, parent);
1228
Behdad Esfahbod686567b2016-02-11 15:25:28 +07001229 pos[child].attach_type() = ATTACH_TYPE_CURSIVE;
1230 pos[child].attach_chain() = (int) parent - (int) child;
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07001231 buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
Behdad Esfahbod58f2a732015-08-25 18:55:34 +01001232 if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction)))
1233 pos[child].y_offset = y_offset;
1234 else
1235 pos[child].x_offset = x_offset;
1236
Behdad Esfahbodbf8469b2018-10-11 20:45:00 -04001237 buffer->idx++;
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001238 return_trace (true);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001239 }
1240
Qunxin Liub0c3eb02019-09-18 16:11:44 -07001241 template <typename Iterator,
1242 hb_requires (hb_is_iterator (Iterator))>
1243 void serialize (hb_serialize_context_t *c,
1244 Iterator it,
1245 const void *src_base)
1246 {
1247 if (unlikely (!c->extend_min ((*this)))) return;
1248 this->format = 1;
1249 this->entryExitRecord.len = it.len ();
1250
1251 for (const EntryExitRecord& entry_record : + it
1252 | hb_map (hb_second))
1253 c->copy (entry_record, src_base, this);
1254
1255 auto glyphs =
1256 + it
1257 | hb_map_retains_sorting (hb_first)
1258 ;
1259
1260 coverage.serialize (c, this).serialize (c, glyphs);
1261 }
1262
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301263 bool subset (hb_subset_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -07001264 {
1265 TRACE_SUBSET (this);
Qunxin Liub0c3eb02019-09-18 16:11:44 -07001266 const hb_set_t &glyphset = *c->plan->glyphset ();
1267 const hb_map_t &glyph_map = *c->plan->glyph_map;
1268
1269 auto *out = c->serializer->start_embed (*this);
1270 if (unlikely (!out)) return_trace (false);
1271
1272 auto it =
1273 + hb_zip (this+coverage, entryExitRecord)
1274 | hb_filter (glyphset, hb_first)
1275 | hb_map_retains_sorting ([&] (hb_pair_t<hb_codepoint_t, const EntryExitRecord&> p) -> hb_pair_t<hb_codepoint_t, const EntryExitRecord&>
1276 { return hb_pair (glyph_map[p.first], p.second);})
1277 ;
1278
1279 bool ret = bool (it);
1280 out->serialize (c->serializer, it, this);
1281 return_trace (ret);
Behdad Esfahbod339d3602018-09-03 17:33:34 -07001282 }
1283
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301284 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001285 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001286 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001287 return_trace (coverage.sanitize (c, this) && entryExitRecord.sanitize (c, this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001288 }
1289
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001290 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001291 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001292 OffsetTo<Coverage>
1293 coverage; /* Offset to Coverage table--from
1294 * beginning of subtable */
1295 ArrayOf<EntryExitRecord>
1296 entryExitRecord; /* Array of EntryExit records--in
1297 * Coverage Index order */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001298 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001299 DEFINE_SIZE_ARRAY (6, entryExitRecord);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001300};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001301
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001302struct CursivePos
1303{
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07001304 template <typename context_t, typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -07001305 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001306 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08001307 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04001308 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001309 switch (u.format) {
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07001310 case 1: return_trace (c->dispatch (u.format1, hb_forward<Ts> (ds)...));
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001311 default:return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001312 }
1313 }
1314
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001315 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001316 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001317 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001318 CursivePosFormat1 format1;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001319 } u;
1320};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001321
1322
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -04001323typedef AnchorMatrix BaseArray; /* base-major--
1324 * in order of BaseCoverage Index--,
1325 * mark-minor--
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001326 * ordered by class--zero-based. */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001327
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001328struct MarkBasePosFormat1
1329{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301330 bool intersects (const hb_set_t *glyphs) const
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -07001331 { return (this+markCoverage).intersects (glyphs) &&
1332 (this+baseCoverage).intersects (glyphs); }
1333
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301334 void collect_glyphs (hb_collect_glyphs_context_t *c) const
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001335 {
Behdad Esfahbod81f27df2017-12-16 06:12:06 -08001336 if (unlikely (!(this+markCoverage).add_coverage (c->input))) return;
1337 if (unlikely (!(this+baseCoverage).add_coverage (c->input))) return;
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001338 }
1339
Ebrahim Byagowie4120082018-12-17 21:31:01 +03301340 const Coverage &get_coverage () const { return this+markCoverage; }
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001341
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301342 bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001343 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001344 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001345 hb_buffer_t *buffer = c->buffer;
1346 unsigned int mark_index = (this+markCoverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001347 if (likely (mark_index == NOT_COVERED)) return_trace (false);
Behdad Esfahbod357ccde2009-05-21 06:32:01 -04001348
Behdad Esfahbod6ab92022016-02-11 16:57:52 +07001349 /* Now we search backwards for a non-mark glyph */
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001350 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
Behdad Esfahbodb051be52015-01-29 13:40:39 +01001351 skippy_iter.reset (buffer->idx, 1);
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -05001352 skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
Behdad Esfahbod7b84c532012-06-08 22:04:23 -04001353 do {
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001354 if (!skippy_iter.prev ()) return_trace (false);
Behdad Esfahbod9b5af3e2018-02-10 21:50:26 -06001355 /* We only want to attach to the first of a MultipleSubst sequence.
1356 * https://github.com/harfbuzz/harfbuzz/issues/740
Behdad Esfahbodd3c09802018-06-30 15:59:25 +04301357 * Reject others...
1358 * ...but stop if we find a mark in the MultipleSubst sequence:
1359 * https://github.com/harfbuzz/harfbuzz/issues/1020 */
Behdad Esfahbod5d98de12017-10-03 13:46:25 +02001360 if (!_hb_glyph_info_multiplied (&buffer->info[skippy_iter.idx]) ||
Behdad Esfahbod9b5af3e2018-02-10 21:50:26 -06001361 0 == _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]) ||
1362 (skippy_iter.idx == 0 ||
Behdad Esfahbodd3c09802018-06-30 15:59:25 +04301363 _hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx - 1]) ||
Behdad Esfahbod9b5af3e2018-02-10 21:50:26 -06001364 _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx]) !=
Behdad Esfahbod1c50a882018-02-18 15:57:09 -08001365 _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx - 1]) ||
1366 _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]) !=
1367 _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx - 1]) + 1
1368 ))
Behdad Esfahbod5d98de12017-10-03 13:46:25 +02001369 break;
Behdad Esfahbod7b84c532012-06-08 22:04:23 -04001370 skippy_iter.reject ();
Bruce Mitchener8d1e4792018-10-18 22:18:42 +07001371 } while (true);
Behdad Esfahbod0532ed12009-08-12 15:40:04 -04001372
Behdad Esfahbod101303d2013-10-18 00:42:39 +02001373 /* Checking that matched glyph is actually a base glyph by GDEF is too strong; disabled */
Behdad Esfahbod6ab92022016-02-11 16:57:52 +07001374 //if (!_hb_glyph_info_is_base_glyph (&buffer->info[skippy_iter.idx])) { return_trace (false); }
Behdad Esfahbod357ccde2009-05-21 06:32:01 -04001375
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001376 unsigned int base_index = (this+baseCoverage).get_coverage (buffer->info[skippy_iter.idx].codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001377 if (base_index == NOT_COVERED) return_trace (false);
Behdad Esfahbod357ccde2009-05-21 06:32:01 -04001378
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001379 return_trace ((this+markArray).apply (c, mark_index, base_index, this+baseArray, classCount, skippy_iter.idx));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001380 }
1381
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301382 bool subset (hb_subset_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -07001383 {
1384 TRACE_SUBSET (this);
1385 // TODO(subset)
1386 return_trace (false);
1387 }
1388
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301389 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001390 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001391 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001392 return_trace (c->check_struct (this) &&
1393 markCoverage.sanitize (c, this) &&
1394 baseCoverage.sanitize (c, this) &&
1395 markArray.sanitize (c, this) &&
1396 baseArray.sanitize (c, this, (unsigned int) classCount));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001397 }
1398
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001399 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001400 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001401 OffsetTo<Coverage>
1402 markCoverage; /* Offset to MarkCoverage table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001403 * beginning of MarkBasePos subtable */
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001404 OffsetTo<Coverage>
1405 baseCoverage; /* Offset to BaseCoverage table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001406 * beginning of MarkBasePos subtable */
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001407 HBUINT16 classCount; /* Number of classes defined for marks */
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001408 OffsetTo<MarkArray>
1409 markArray; /* Offset to MarkArray table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001410 * beginning of MarkBasePos subtable */
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001411 OffsetTo<BaseArray>
1412 baseArray; /* Offset to BaseArray table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001413 * beginning of MarkBasePos subtable */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001414 public:
1415 DEFINE_SIZE_STATIC (12);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001416};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001417
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001418struct MarkBasePos
1419{
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07001420 template <typename context_t, typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -07001421 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001422 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08001423 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04001424 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001425 switch (u.format) {
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07001426 case 1: return_trace (c->dispatch (u.format1, hb_forward<Ts> (ds)...));
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001427 default:return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001428 }
1429 }
1430
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001431 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001432 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001433 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001434 MarkBasePosFormat1 format1;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001435 } u;
1436};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001437
1438
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -04001439typedef AnchorMatrix LigatureAttach; /* component-major--
1440 * in order of writing direction--,
1441 * mark-minor--
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001442 * ordered by class--zero-based. */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001443
Behdad Esfahbod3564ee52009-08-14 18:32:56 -04001444typedef OffsetListOf<LigatureAttach> LigatureArray;
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001445 /* Array of LigatureAttach
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001446 * tables ordered by
1447 * LigatureCoverage Index */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001448
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001449struct MarkLigPosFormat1
1450{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301451 bool intersects (const hb_set_t *glyphs) const
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -07001452 { return (this+markCoverage).intersects (glyphs) &&
1453 (this+ligatureCoverage).intersects (glyphs); }
1454
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301455 void collect_glyphs (hb_collect_glyphs_context_t *c) const
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001456 {
Behdad Esfahbod81f27df2017-12-16 06:12:06 -08001457 if (unlikely (!(this+markCoverage).add_coverage (c->input))) return;
1458 if (unlikely (!(this+ligatureCoverage).add_coverage (c->input))) return;
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001459 }
1460
Ebrahim Byagowie4120082018-12-17 21:31:01 +03301461 const Coverage &get_coverage () const { return this+markCoverage; }
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001462
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301463 bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001464 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001465 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001466 hb_buffer_t *buffer = c->buffer;
1467 unsigned int mark_index = (this+markCoverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001468 if (likely (mark_index == NOT_COVERED)) return_trace (false);
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001469
Behdad Esfahbod6ab92022016-02-11 16:57:52 +07001470 /* Now we search backwards for a non-mark glyph */
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001471 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
Behdad Esfahbodb051be52015-01-29 13:40:39 +01001472 skippy_iter.reset (buffer->idx, 1);
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -05001473 skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001474 if (!skippy_iter.prev ()) return_trace (false);
Behdad Esfahbod0532ed12009-08-12 15:40:04 -04001475
Behdad Esfahbod101303d2013-10-18 00:42:39 +02001476 /* Checking that matched glyph is actually a ligature by GDEF is too strong; disabled */
Behdad Esfahbod6ab92022016-02-11 16:57:52 +07001477 //if (!_hb_glyph_info_is_ligature (&buffer->info[skippy_iter.idx])) { return_trace (false); }
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001478
Behdad Esfahbod4ab97312012-01-16 22:05:08 -05001479 unsigned int j = skippy_iter.idx;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001480 unsigned int lig_index = (this+ligatureCoverage).get_coverage (buffer->info[j].codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001481 if (lig_index == NOT_COVERED) return_trace (false);
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001482
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001483 const LigatureArray& lig_array = this+ligatureArray;
Behdad Esfahbod3564ee52009-08-14 18:32:56 -04001484 const LigatureAttach& lig_attach = lig_array[lig_index];
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -04001485
1486 /* Find component to attach to */
Behdad Esfahbodb2b18ef2009-08-14 19:37:18 -04001487 unsigned int comp_count = lig_attach.rows;
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001488 if (unlikely (!comp_count)) return_trace (false);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001489
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001490 /* We must now check whether the ligature ID of the current mark glyph
1491 * is identical to the ligature ID of the found ligature. If yes, we
1492 * can directly use the component index. If not, we attach the mark
1493 * glyph to the last component of the ligature. */
Behdad Esfahbod0aef4252012-07-30 00:55:15 -04001494 unsigned int comp_index;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001495 unsigned int lig_id = _hb_glyph_info_get_lig_id (&buffer->info[j]);
1496 unsigned int mark_id = _hb_glyph_info_get_lig_id (&buffer->cur());
1497 unsigned int mark_comp = _hb_glyph_info_get_lig_comp (&buffer->cur());
Behdad Esfahbod0aef4252012-07-30 00:55:15 -04001498 if (lig_id && lig_id == mark_id && mark_comp > 0)
Behdad Esfahbod41248cc2019-05-07 20:54:31 -07001499 comp_index = hb_min (comp_count, _hb_glyph_info_get_lig_comp (&buffer->cur())) - 1;
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001500 else
Behdad Esfahbodb2b18ef2009-08-14 19:37:18 -04001501 comp_index = comp_count - 1;
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001502
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001503 return_trace ((this+markArray).apply (c, mark_index, comp_index, lig_attach, classCount, j));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001504 }
1505
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301506 bool subset (hb_subset_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -07001507 {
1508 TRACE_SUBSET (this);
1509 // TODO(subset)
1510 return_trace (false);
1511 }
1512
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301513 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001514 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001515 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001516 return_trace (c->check_struct (this) &&
1517 markCoverage.sanitize (c, this) &&
1518 ligatureCoverage.sanitize (c, this) &&
1519 markArray.sanitize (c, this) &&
1520 ligatureArray.sanitize (c, this, (unsigned int) classCount));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001521 }
1522
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001523 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001524 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001525 OffsetTo<Coverage>
1526 markCoverage; /* Offset to Mark Coverage table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001527 * beginning of MarkLigPos subtable */
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001528 OffsetTo<Coverage>
1529 ligatureCoverage; /* Offset to Ligature Coverage
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001530 * table--from beginning of MarkLigPos
1531 * subtable */
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001532 HBUINT16 classCount; /* Number of defined mark classes */
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001533 OffsetTo<MarkArray>
1534 markArray; /* Offset to MarkArray table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001535 * beginning of MarkLigPos subtable */
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001536 OffsetTo<LigatureArray>
1537 ligatureArray; /* Offset to LigatureArray table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001538 * beginning of MarkLigPos subtable */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001539 public:
1540 DEFINE_SIZE_STATIC (12);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001541};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001542
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001543struct MarkLigPos
1544{
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07001545 template <typename context_t, typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -07001546 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001547 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08001548 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04001549 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001550 switch (u.format) {
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07001551 case 1: return_trace (c->dispatch (u.format1, hb_forward<Ts> (ds)...));
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001552 default:return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001553 }
1554 }
1555
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001556 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001557 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001558 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001559 MarkLigPosFormat1 format1;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001560 } u;
1561};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001562
1563
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -04001564typedef AnchorMatrix Mark2Array; /* mark2-major--
1565 * in order of Mark2Coverage Index--,
1566 * mark1-minor--
1567 * ordered by class--zero-based. */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001568
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001569struct MarkMarkPosFormat1
1570{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301571 bool intersects (const hb_set_t *glyphs) const
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -07001572 { return (this+mark1Coverage).intersects (glyphs) &&
1573 (this+mark2Coverage).intersects (glyphs); }
1574
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301575 void collect_glyphs (hb_collect_glyphs_context_t *c) const
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001576 {
Behdad Esfahbod81f27df2017-12-16 06:12:06 -08001577 if (unlikely (!(this+mark1Coverage).add_coverage (c->input))) return;
1578 if (unlikely (!(this+mark2Coverage).add_coverage (c->input))) return;
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001579 }
1580
Ebrahim Byagowie4120082018-12-17 21:31:01 +03301581 const Coverage &get_coverage () const { return this+mark1Coverage; }
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001582
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301583 bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001584 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001585 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001586 hb_buffer_t *buffer = c->buffer;
1587 unsigned int mark1_index = (this+mark1Coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001588 if (likely (mark1_index == NOT_COVERED)) return_trace (false);
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001589
1590 /* now we search backwards for a suitable mark glyph until a non-mark glyph */
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001591 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
Behdad Esfahbodb051be52015-01-29 13:40:39 +01001592 skippy_iter.reset (buffer->idx, 1);
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -05001593 skippy_iter.set_lookup_props (c->lookup_props & ~LookupFlag::IgnoreFlags);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001594 if (!skippy_iter.prev ()) return_trace (false);
Behdad Esfahbod0532ed12009-08-12 15:40:04 -04001595
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001596 if (!_hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx])) { return_trace (false); }
Behdad Esfahbod80ea5bd2009-05-26 17:58:37 -04001597
Behdad Esfahbod4ab97312012-01-16 22:05:08 -05001598 unsigned int j = skippy_iter.idx;
1599
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001600 unsigned int id1 = _hb_glyph_info_get_lig_id (&buffer->cur());
1601 unsigned int id2 = _hb_glyph_info_get_lig_id (&buffer->info[j]);
1602 unsigned int comp1 = _hb_glyph_info_get_lig_comp (&buffer->cur());
1603 unsigned int comp2 = _hb_glyph_info_get_lig_comp (&buffer->info[j]);
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001604
Behdad Esfahbod5d874d52012-07-28 21:05:25 -04001605 if (likely (id1 == id2)) {
1606 if (id1 == 0) /* Marks belonging to the same base. */
1607 goto good;
1608 else if (comp1 == comp2) /* Marks belonging to the same ligature component. */
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +03301609 goto good;
Behdad Esfahbod5d874d52012-07-28 21:05:25 -04001610 } else {
1611 /* If ligature ids don't match, it may be the case that one of the marks
1612 * itself is a ligature. In which case match. */
1613 if ((id1 > 0 && !comp1) || (id2 > 0 && !comp2))
1614 goto good;
1615 }
1616
1617 /* Didn't match. */
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001618 return_trace (false);
Behdad Esfahbod5d874d52012-07-28 21:05:25 -04001619
1620 good:
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001621 unsigned int mark2_index = (this+mark2Coverage).get_coverage (buffer->info[j].codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001622 if (mark2_index == NOT_COVERED) return_trace (false);
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001623
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001624 return_trace ((this+mark1Array).apply (c, mark1_index, mark2_index, this+mark2Array, classCount, j));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001625 }
1626
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301627 bool subset (hb_subset_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -07001628 {
1629 TRACE_SUBSET (this);
1630 // TODO(subset)
1631 return_trace (false);
1632 }
1633
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301634 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001635 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001636 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001637 return_trace (c->check_struct (this) &&
1638 mark1Coverage.sanitize (c, this) &&
1639 mark2Coverage.sanitize (c, this) &&
1640 mark1Array.sanitize (c, this) &&
1641 mark2Array.sanitize (c, this, (unsigned int) classCount));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001642 }
1643
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001644 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001645 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001646 OffsetTo<Coverage>
1647 mark1Coverage; /* Offset to Combining Mark1 Coverage
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001648 * table--from beginning of MarkMarkPos
1649 * subtable */
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001650 OffsetTo<Coverage>
1651 mark2Coverage; /* Offset to Combining Mark2 Coverage
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001652 * table--from beginning of MarkMarkPos
1653 * subtable */
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001654 HBUINT16 classCount; /* Number of defined mark classes */
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001655 OffsetTo<MarkArray>
1656 mark1Array; /* Offset to Mark1Array table--from
1657 * beginning of MarkMarkPos subtable */
1658 OffsetTo<Mark2Array>
1659 mark2Array; /* Offset to Mark2Array table--from
1660 * beginning of MarkMarkPos subtable */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001661 public:
1662 DEFINE_SIZE_STATIC (12);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001663};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001664
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001665struct MarkMarkPos
1666{
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07001667 template <typename context_t, typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -07001668 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001669 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08001670 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04001671 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001672 switch (u.format) {
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07001673 case 1: return_trace (c->dispatch (u.format1, hb_forward<Ts> (ds)...));
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001674 default:return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001675 }
1676 }
1677
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001678 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001679 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001680 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001681 MarkMarkPosFormat1 format1;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001682 } u;
1683};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001684
1685
Behdad Esfahbod08f1eed2012-11-23 16:51:43 -05001686struct ContextPos : Context {};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001687
Behdad Esfahbod08f1eed2012-11-23 16:51:43 -05001688struct ChainContextPos : ChainContext {};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001689
Behdad Esfahbod653eeb22012-11-23 16:57:36 -05001690struct ExtensionPos : Extension<ExtensionPos>
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001691{
Behdad Esfahbod9c3747c2018-09-03 16:53:03 -07001692 typedef struct PosLookupSubTable SubTable;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001693};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001694
1695
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04001696
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001697/*
1698 * PosLookup
1699 */
1700
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001701
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001702struct PosLookupSubTable
1703{
Behdad Esfahbod7b2ef552018-09-03 17:16:09 -07001704 friend struct Lookup;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001705 friend struct PosLookup;
1706
Behdad Esfahbodc6035cf2012-04-12 13:23:59 -04001707 enum Type {
Behdad Esfahbodff05d252009-05-20 03:53:00 -04001708 Single = 1,
1709 Pair = 2,
1710 Cursive = 3,
1711 MarkBase = 4,
1712 MarkLig = 5,
1713 MarkMark = 6,
1714 Context = 7,
1715 ChainContext = 8,
Behdad Esfahbod8f034d52009-08-18 16:41:59 -04001716 Extension = 9
Behdad Esfahbodff05d252009-05-20 03:53:00 -04001717 };
1718
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07001719 template <typename context_t, typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -07001720 typename context_t::return_t dispatch (context_t *c, unsigned int lookup_type, Ts&&... ds) const
Behdad Esfahbodfaf0f202012-06-09 03:02:36 -04001721 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08001722 TRACE_DISPATCH (this, lookup_type);
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001723 switch (lookup_type) {
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07001724 case Single: return_trace (u.single.dispatch (c, hb_forward<Ts> (ds)...));
1725 case Pair: return_trace (u.pair.dispatch (c, hb_forward<Ts> (ds)...));
1726 case Cursive: return_trace (u.cursive.dispatch (c, hb_forward<Ts> (ds)...));
1727 case MarkBase: return_trace (u.markBase.dispatch (c, hb_forward<Ts> (ds)...));
1728 case MarkLig: return_trace (u.markLig.dispatch (c, hb_forward<Ts> (ds)...));
1729 case MarkMark: return_trace (u.markMark.dispatch (c, hb_forward<Ts> (ds)...));
1730 case Context: return_trace (u.context.dispatch (c, hb_forward<Ts> (ds)...));
1731 case ChainContext: return_trace (u.chainContext.dispatch (c, hb_forward<Ts> (ds)...));
1732 case Extension: return_trace (u.extension.dispatch (c, hb_forward<Ts> (ds)...));
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001733 default: return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001734 }
Behdad Esfahbodfaf0f202012-06-09 03:02:36 -04001735 }
1736
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001737 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001738 union {
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001739 SinglePos single;
1740 PairPos pair;
1741 CursivePos cursive;
1742 MarkBasePos markBase;
1743 MarkLigPos markLig;
1744 MarkMarkPos markMark;
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001745 ContextPos context;
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001746 ChainContextPos chainContext;
1747 ExtensionPos extension;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001748 } u;
Behdad Esfahboded074222010-05-10 18:08:46 -04001749 public:
Behdad Esfahbod2cc993e2018-12-12 10:07:38 -05001750 DEFINE_SIZE_MIN (0);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001751};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001752
1753
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001754struct PosLookup : Lookup
1755{
Behdad Esfahbod9c3747c2018-09-03 16:53:03 -07001756 typedef struct PosLookupSubTable SubTable;
1757
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301758 const SubTable& get_subtable (unsigned int i) const
Behdad Esfahbod9c3747c2018-09-03 16:53:03 -07001759 { return Lookup::get_subtable<SubTable> (i); }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001760
Ebrahim Byagowie4120082018-12-17 21:31:01 +03301761 bool is_reverse () const
Behdad Esfahbode015b8f2013-05-03 17:34:29 -04001762 {
1763 return false;
1764 }
1765
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301766 bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbodbd047d32015-02-19 10:47:18 +03001767 {
1768 TRACE_APPLY (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001769 return_trace (dispatch (c));
Behdad Esfahbodbd047d32015-02-19 10:47:18 +03001770 }
1771
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301772 bool intersects (const hb_set_t *glyphs) const
Behdad Esfahbod7c9cfa22018-09-02 19:47:50 -07001773 {
1774 hb_intersects_context_t c (glyphs);
1775 return dispatch (&c);
1776 }
1777
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301778 hb_collect_glyphs_context_t::return_t collect_glyphs (hb_collect_glyphs_context_t *c) const
Behdad Esfahbod89bcfb22019-01-18 14:59:18 -05001779 { return dispatch (c); }
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001780
Behdad Esfahboda878c582012-08-01 21:18:54 -04001781 template <typename set_t>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301782 void add_coverage (set_t *glyphs) const
Behdad Esfahboda878c582012-08-01 21:18:54 -04001783 {
Behdad Esfahbod8e36ccf2015-02-17 19:15:34 +03001784 hb_add_coverage_context_t<set_t> c (glyphs);
1785 dispatch (&c);
Behdad Esfahboda878c582012-08-01 21:18:54 -04001786 }
1787
Behdad Esfahbod95df00a2019-04-12 17:50:03 -04001788 HB_INTERNAL static bool apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index);
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001789
Behdad Esfahbodee5464d2013-03-09 01:59:30 -05001790 template <typename context_t>
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301791 static typename context_t::return_t dispatch_recurse_func (context_t *c, unsigned int lookup_index);
Behdad Esfahbodee5464d2013-03-09 01:59:30 -05001792
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07001793 template <typename context_t, typename ...Ts>
Behdad Esfahbod83e3eab2019-05-07 20:58:43 -07001794 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
Behdad Esfahbod36bb24f2019-05-05 10:14:17 -07001795 { return Lookup::dispatch<SubTable> (c, hb_forward<Ts> (ds)...); }
Behdad Esfahbodee5464d2013-03-09 01:59:30 -05001796
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301797 bool subset (hb_subset_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -07001798 { return Lookup::subset<SubTable> (c); }
1799
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301800 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod7b2ef552018-09-03 17:16:09 -07001801 { return Lookup::sanitize<SubTable> (c); }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001802};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001803
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001804/*
Ebrahim Byagowia02c3ee2018-04-12 13:38:19 +04301805 * GPOS -- Glyph Positioning
1806 * https://docs.microsoft.com/en-us/typography/opentype/spec/gpos
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001807 */
1808
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001809struct GPOS : GSUBGPOS
1810{
Behdad Esfahbodef006542019-01-22 12:08:57 +01001811 static constexpr hb_tag_t tableTag = HB_OT_TAG_GPOS;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001812
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301813 const PosLookup& get_lookup (unsigned int i) const
Behdad Esfahbod187454c2010-04-23 16:35:01 -04001814 { return CastR<PosLookup> (GSUBGPOS::get_lookup (i)); }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001815
Behdad Esfahbod05bd1b62012-07-30 19:30:01 -04001816 static inline void position_start (hb_font_t *font, hb_buffer_t *buffer);
Behdad Esfahbod7d8d58a2016-02-11 16:34:28 +07001817 static inline void position_finish_advances (hb_font_t *font, hb_buffer_t *buffer);
1818 static inline void position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer);
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04001819
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301820 bool subset (hb_subset_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -07001821 { return GSUBGPOS::subset<PosLookup> (c); }
1822
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301823 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod339d3602018-09-03 17:33:34 -07001824 { return GSUBGPOS::sanitize<PosLookup> (c); }
Behdad Esfahbod963413f2018-08-26 00:47:55 -07001825
Behdad Esfahbod574d8882018-11-25 16:51:22 -05001826 HB_INTERNAL bool is_blacklisted (hb_blob_t *blob,
1827 hb_face_t *face) const;
1828
Behdad Esfahbod963413f2018-08-26 00:47:55 -07001829 typedef GSUBGPOS::accelerator_t<GPOS> accelerator_t;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001830};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001831
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001832
1833static void
Behdad Esfahbod65785752015-08-25 20:24:59 +01001834reverse_cursive_minor_offset (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction, unsigned int new_parent)
1835{
Behdad Esfahbod686567b2016-02-11 15:25:28 +07001836 int chain = pos[i].attach_chain(), type = pos[i].attach_type();
1837 if (likely (!chain || 0 == (type & ATTACH_TYPE_CURSIVE)))
Behdad Esfahbod65785752015-08-25 20:24:59 +01001838 return;
1839
Behdad Esfahbod686567b2016-02-11 15:25:28 +07001840 pos[i].attach_chain() = 0;
Behdad Esfahbod65785752015-08-25 20:24:59 +01001841
Behdad Esfahbod686567b2016-02-11 15:25:28 +07001842 unsigned int j = (int) i + chain;
Behdad Esfahbod806ad8d2016-02-11 14:53:11 +07001843
Behdad Esfahbod65785752015-08-25 20:24:59 +01001844 /* Stop if we see new parent in the chain. */
1845 if (j == new_parent)
1846 return;
1847
1848 reverse_cursive_minor_offset (pos, j, direction, new_parent);
1849
1850 if (HB_DIRECTION_IS_HORIZONTAL (direction))
1851 pos[j].y_offset = -pos[i].y_offset;
1852 else
1853 pos[j].x_offset = -pos[i].x_offset;
1854
Behdad Esfahbod686567b2016-02-11 15:25:28 +07001855 pos[j].attach_chain() = -chain;
1856 pos[j].attach_type() = type;
Behdad Esfahbod65785752015-08-25 20:24:59 +01001857}
1858static void
Behdad Esfahbod36f38ea2018-10-16 16:24:03 -07001859propagate_attachment_offsets (hb_glyph_position_t *pos,
1860 unsigned int len,
1861 unsigned int i,
1862 hb_direction_t direction)
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001863{
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07001864 /* Adjusts offsets of attached glyphs (both cursive and mark) to accumulate
1865 * offset of glyph they are attached to. */
Behdad Esfahbod686567b2016-02-11 15:25:28 +07001866 int chain = pos[i].attach_chain(), type = pos[i].attach_type();
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07001867 if (likely (!chain))
Behdad Esfahboddf5d5c62012-08-23 09:33:30 -04001868 return;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001869
Behdad Esfahbod686567b2016-02-11 15:25:28 +07001870 pos[i].attach_chain() = 0;
1871
Behdad Esfahbod36f38ea2018-10-16 16:24:03 -07001872 unsigned int j = (int) i + chain;
1873
1874 if (unlikely (j >= len))
1875 return;
1876
1877 propagate_attachment_offsets (pos, len, j, direction);
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001878
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07001879 assert (!!(type & ATTACH_TYPE_MARK) ^ !!(type & ATTACH_TYPE_CURSIVE));
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001880
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07001881 if (type & ATTACH_TYPE_CURSIVE)
1882 {
1883 if (HB_DIRECTION_IS_HORIZONTAL (direction))
1884 pos[i].y_offset += pos[j].y_offset;
1885 else
1886 pos[i].x_offset += pos[j].x_offset;
1887 }
1888 else /*if (type & ATTACH_TYPE_MARK)*/
1889 {
Behdad Esfahboddf5d5c62012-08-23 09:33:30 -04001890 pos[i].x_offset += pos[j].x_offset;
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07001891 pos[i].y_offset += pos[j].y_offset;
Behdad Esfahbodebd74312016-02-24 15:53:40 +09001892
1893 assert (j < i);
1894 if (HB_DIRECTION_IS_FORWARD (direction))
1895 for (unsigned int k = j; k < i; k++) {
1896 pos[i].x_offset -= pos[k].x_advance;
1897 pos[i].y_offset -= pos[k].y_advance;
1898 }
1899 else
1900 for (unsigned int k = j + 1; k < i + 1; k++) {
1901 pos[i].x_offset += pos[k].x_advance;
1902 pos[i].y_offset += pos[k].y_advance;
1903 }
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07001904 }
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001905}
1906
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04001907void
Behdad Esfahbod05bd1b62012-07-30 19:30:01 -04001908GPOS::position_start (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
Behdad Esfahboda9ad3d32011-07-28 15:42:18 -04001909{
Behdad Esfahbodb65c0602011-07-28 16:48:43 -04001910 unsigned int count = buffer->len;
1911 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod686567b2016-02-11 15:25:28 +07001912 buffer->pos[i].attach_chain() = buffer->pos[i].attach_type() = 0;
Behdad Esfahboda9ad3d32011-07-28 15:42:18 -04001913}
1914
1915void
Behdad Esfahbod39bd07a2018-10-26 21:01:11 -07001916GPOS::position_finish_advances (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer HB_UNUSED)
Behdad Esfahbod7d8d58a2016-02-11 16:34:28 +07001917{
1918 //_hb_buffer_assert_gsubgpos_vars (buffer);
1919}
1920
1921void
1922GPOS::position_finish_offsets (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04001923{
Behdad Esfahbod8f3eebf2014-08-02 17:18:46 -04001924 _hb_buffer_assert_gsubgpos_vars (buffer);
1925
Ryan Lortie70566be2011-04-15 18:32:36 -04001926 unsigned int len;
1927 hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, &len);
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04001928 hb_direction_t direction = buffer->props.direction;
1929
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04001930 /* Handle attachments */
Behdad Esfahbod550bd142015-11-04 22:58:58 -08001931 if (buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT)
1932 for (unsigned int i = 0; i < len; i++)
Behdad Esfahbod36f38ea2018-10-16 16:24:03 -07001933 propagate_attachment_offsets (pos, len, i, direction);
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04001934}
1935
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001936
Behdad Esfahbod33b006c2018-11-05 23:19:04 -05001937struct GPOS_accelerator_t : GPOS::accelerator_t {};
1938
1939
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001940/* Out-of-class implementation for methods recursing */
1941
Behdad Esfahbod7dcf8e12019-06-26 13:44:10 -07001942#ifndef HB_NO_OT_LAYOUT
Behdad Esfahbodc6fb8432012-11-23 18:04:08 -05001943template <typename context_t>
Behdad Esfahbod6faff8e2014-04-28 14:29:39 -07001944/*static*/ inline typename context_t::return_t PosLookup::dispatch_recurse_func (context_t *c, unsigned int lookup_index)
Behdad Esfahbodc6fb8432012-11-23 18:04:08 -05001945{
Behdad Esfahbod33b006c2018-11-05 23:19:04 -05001946 const PosLookup &l = c->face->table.GPOS.get_relaxed ()->table->get_lookup (lookup_index);
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001947 return l.dispatch (c);
Behdad Esfahbodc6fb8432012-11-23 18:04:08 -05001948}
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001949/*static*/ inline bool PosLookup::apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index)
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001950{
Behdad Esfahbod33b006c2018-11-05 23:19:04 -05001951 const PosLookup &l = c->face->table.GPOS.get_relaxed ()->table->get_lookup (lookup_index);
Behdad Esfahbod4c4e8f02012-11-24 01:13:20 -05001952 unsigned int saved_lookup_props = c->lookup_props;
Behdad Esfahbod2c8b3b22015-08-18 14:36:43 +01001953 unsigned int saved_lookup_index = c->lookup_index;
1954 c->set_lookup_index (lookup_index);
1955 c->set_lookup_props (l.get_props ());
Behdad Esfahbod1a232212015-02-19 10:40:23 +03001956 bool ret = l.dispatch (c);
Behdad Esfahbod2c8b3b22015-08-18 14:36:43 +01001957 c->set_lookup_index (saved_lookup_index);
Behdad Esfahbod77889932015-01-28 23:01:12 -08001958 c->set_lookup_props (saved_lookup_props);
Behdad Esfahbod4c4e8f02012-11-24 01:13:20 -05001959 return ret;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001960}
Behdad Esfahbod7dcf8e12019-06-26 13:44:10 -07001961#endif
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001962
1963
Behdad Esfahbod7d52e662012-11-16 18:49:54 -08001964} /* namespace OT */
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -04001965
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -04001966
Behdad Esfahbod7a750ac2011-08-17 14:19:59 +02001967#endif /* HB_OT_LAYOUT_GPOS_TABLE_HH */