blob: 182c8d909843a849b3a09e61e8aef569c477e09b [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 Esfahbod5f5b24f2009-08-02 20:03:12 -040032#include "hb-ot-layout-gsubgpos-private.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 Esfahbod0eb9fc62010-05-10 19:01:17 -040056typedef Value ValueRecord[VAR];
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) */
87 Offset xPlaDevice; /* Offset to Device table for
88 * horizontal placement--measured from
89 * beginning of PosTable (may be NULL) */
90 Offset yPlaDevice; /* Offset to Device table for vertical
91 * placement--measured from beginning
92 * of PosTable (may be NULL) */
93 Offset xAdvDevice; /* Offset to Device table for
94 * horizontal advance--measured from
95 * beginning of PosTable (may be NULL) */
96 Offset yAdvDevice; /* Offset to Device table for vertical
97 * advance--measured from beginning of
98 * PosTable (may be NULL) */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040099#endif
100
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -0400101 inline unsigned int get_len (void) const
Behdad Esfahbod978ace62018-02-16 16:06:17 -0800102 { return _hb_popcount ((unsigned int) *this); }
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -0400103 inline unsigned int get_size (void) const
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400104 { return get_len () * Value::static_size; }
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400105
Behdad Esfahbodfd034492018-01-17 16:46:51 -0800106 void apply_value (hb_ot_apply_context_t *c,
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400107 const void *base,
108 const Value *values,
109 hb_glyph_position_t &glyph_pos) const
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400110 {
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400111 unsigned int format = *this;
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400112 if (!format) return;
113
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700114 hb_font_t *font = c->font;
115 hb_bool_t horizontal = HB_DIRECTION_IS_HORIZONTAL (c->direction);
116
Behdad Esfahbodb6f902a2011-05-11 00:04:15 -0400117 if (format & xPlacement) glyph_pos.x_offset += font->em_scale_x (get_short (values++));
118 if (format & yPlacement) glyph_pos.y_offset += font->em_scale_y (get_short (values++));
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400119 if (format & xAdvance) {
Konstantin Ritt08322592014-01-22 20:31:30 +0200120 if (likely (horizontal)) glyph_pos.x_advance += font->em_scale_x (get_short (values));
121 values++;
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400122 }
Behdad Esfahbodcc2086d2011-05-19 19:19:50 -0400123 /* y_advance values grow downward but font-space grows upward, hence negation */
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400124 if (format & yAdvance) {
Konstantin Ritt08322592014-01-22 20:31:30 +0200125 if (unlikely (!horizontal)) glyph_pos.y_advance -= font->em_scale_y (get_short (values));
126 values++;
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400127 }
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400128
Behdad Esfahboda8d960b2010-04-29 14:31:56 -0400129 if (!has_device ()) return;
130
Behdad Esfahbodca286702016-03-01 19:29:36 +0900131 bool use_x_device = font->x_ppem || font->num_coords;
132 bool use_y_device = font->y_ppem || font->num_coords;
Behdad Esfahboda8d960b2010-04-29 14:31:56 -0400133
Behdad Esfahbodca286702016-03-01 19:29:36 +0900134 if (!use_x_device && !use_y_device) return;
Behdad Esfahboda8d960b2010-04-29 14:31:56 -0400135
Behdad Esfahbodcf3de4d2016-09-10 00:22:24 -0700136 const VariationStore &store = c->var_store;
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700137
Behdad Esfahbod0090dc02009-07-30 16:28:45 -0400138 /* pixel -> fractional pixel */
139 if (format & xPlaDevice) {
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700140 if (use_x_device) glyph_pos.x_offset += (base + get_device (values)).get_x_delta (font, store);
Konstantin Ritt08322592014-01-22 20:31:30 +0200141 values++;
Behdad Esfahbod0090dc02009-07-30 16:28:45 -0400142 }
143 if (format & yPlaDevice) {
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700144 if (use_y_device) glyph_pos.y_offset += (base + get_device (values)).get_y_delta (font, store);
Konstantin Ritt08322592014-01-22 20:31:30 +0200145 values++;
Behdad Esfahbod0090dc02009-07-30 16:28:45 -0400146 }
147 if (format & xAdvDevice) {
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700148 if (horizontal && use_x_device) glyph_pos.x_advance += (base + get_device (values)).get_x_delta (font, store);
Konstantin Ritt08322592014-01-22 20:31:30 +0200149 values++;
Behdad Esfahbod0090dc02009-07-30 16:28:45 -0400150 }
151 if (format & yAdvDevice) {
Behdad Esfahbodcc2086d2011-05-19 19:19:50 -0400152 /* y_advance values grow downward but font-space grows upward, hence negation */
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700153 if (!horizontal && use_y_device) glyph_pos.y_advance -= (base + get_device (values)).get_y_delta (font, store);
Konstantin Ritt08322592014-01-22 20:31:30 +0200154 values++;
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400155 }
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400156 }
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400157
158 private:
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300159 inline bool sanitize_value_devices (hb_sanitize_context_t *c, const void *base, const Value *values) const
160 {
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 Esfahbodc2ddfd22010-05-06 13:06:15 -0400176 static inline OffsetTo<Device>& get_device (Value* value)
177 { return *CastP<OffsetTo<Device> > (value); }
178 static inline const OffsetTo<Device>& get_device (const Value* value)
179 { return *CastP<OffsetTo<Device> > (value); }
180
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100181 static inline const HBINT16& get_short (const Value* value)
182 { return *CastP<HBINT16> (value); }
Behdad Esfahbodc2ddfd22010-05-06 13:06:15 -0400183
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400184 public:
185
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -0400186 inline bool has_device (void) const {
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400187 unsigned int format = *this;
188 return (format & devices) != 0;
189 }
190
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300191 inline bool sanitize_value (hb_sanitize_context_t *c, const void *base, const Value *values) const
192 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500193 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100194 return_trace (c->check_range (values, get_size ()) && (!has_device () || sanitize_value_devices (c, base, values)));
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400195 }
196
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300197 inline bool sanitize_values (hb_sanitize_context_t *c, const void *base, const Value *values, unsigned int count) const
198 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500199 TRACE_SANITIZE (this);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400200 unsigned int len = get_len ();
201
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100202 if (!c->check_array (values, get_size (), count)) return_trace (false);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400203
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100204 if (!has_device ()) return_trace (true);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400205
206 for (unsigned int i = 0; i < count; i++) {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400207 if (!sanitize_value_devices (c, base, values))
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100208 return_trace (false);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400209 values += len;
210 }
211
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100212 return_trace (true);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400213 }
214
Behdad Esfahbod278a91f2010-04-22 13:59:39 -0400215 /* Just sanitize referenced Device tables. Doesn't check the values themselves. */
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300216 inline bool sanitize_values_stride_unsafe (hb_sanitize_context_t *c, const void *base, const Value *values, unsigned int count, unsigned int stride) const
217 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500218 TRACE_SANITIZE (this);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400219
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100220 if (!has_device ()) return_trace (true);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400221
222 for (unsigned int i = 0; i < count; i++) {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400223 if (!sanitize_value_devices (c, base, values))
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100224 return_trace (false);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400225 values += stride;
226 }
227
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100228 return_trace (true);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400229 }
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400230};
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400231
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400232
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400233struct AnchorFormat1
234{
Behdad Esfahbodfd034492018-01-17 16:46:51 -0800235 inline void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id HB_UNUSED,
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100236 float *x, float *y) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400237 {
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700238 hb_font_t *font = c->font;
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100239 *x = font->em_fscale_x (xCoordinate);
240 *y = font->em_fscale_y (yCoordinate);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400241 }
242
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300243 inline bool sanitize (hb_sanitize_context_t *c) const
244 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500245 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100246 return_trace (c->check_struct (this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400247 }
248
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400249 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100250 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbode347fb92018-02-26 01:08:00 -0800251 FWORD xCoordinate; /* Horizontal value--in design units */
252 FWORD yCoordinate; /* Vertical value--in design units */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400253 public:
254 DEFINE_SIZE_STATIC (6);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400255};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400256
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400257struct AnchorFormat2
258{
Behdad Esfahbodfd034492018-01-17 16:46:51 -0800259 inline void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id,
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100260 float *x, float *y) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400261 {
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700262 hb_font_t *font = c->font;
263 unsigned int x_ppem = font->x_ppem;
264 unsigned int y_ppem = font->y_ppem;
Ebrahim Byagowieaf64942018-04-23 18:39:40 +0430265 hb_position_t cx = 0, cy = 0;
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700266 hb_bool_t ret;
Behdad Esfahbod6f729b42010-04-29 03:59:06 -0400267
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700268 ret = (x_ppem || y_ppem) &&
269 font->get_glyph_contour_point_for_origin (glyph_id, anchorPoint, HB_DIRECTION_LTR, &cx, &cy);
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100270 *x = ret && x_ppem ? cx : font->em_fscale_x (xCoordinate);
271 *y = ret && y_ppem ? cy : font->em_fscale_y (yCoordinate);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400272 }
273
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300274 inline bool sanitize (hb_sanitize_context_t *c) const
275 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500276 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100277 return_trace (c->check_struct (this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400278 }
279
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400280 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100281 HBUINT16 format; /* Format identifier--format = 2 */
Behdad Esfahbode347fb92018-02-26 01:08:00 -0800282 FWORD xCoordinate; /* Horizontal value--in design units */
283 FWORD yCoordinate; /* Vertical value--in design units */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100284 HBUINT16 anchorPoint; /* Index to glyph contour point */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400285 public:
286 DEFINE_SIZE_STATIC (8);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400287};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400288
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400289struct AnchorFormat3
290{
Behdad Esfahbodfd034492018-01-17 16:46:51 -0800291 inline void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id HB_UNUSED,
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100292 float *x, float *y) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400293 {
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700294 hb_font_t *font = c->font;
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100295 *x = font->em_fscale_x (xCoordinate);
296 *y = font->em_fscale_y (yCoordinate);
Behdad Esfahbodc18ec2b2009-05-21 04:54:01 -0400297
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700298 if (font->x_ppem || font->num_coords)
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700299 *x += (this+xDeviceTable).get_x_delta (font, c->var_store);
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700300 if (font->y_ppem || font->num_coords)
Behdad Esfahbod1124d2e2016-09-09 22:48:27 -0700301 *y += (this+yDeviceTable).get_y_delta (font, c->var_store);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400302 }
303
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300304 inline bool sanitize (hb_sanitize_context_t *c) const
305 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500306 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100307 return_trace (c->check_struct (this) && xDeviceTable.sanitize (c, this) && yDeviceTable.sanitize (c, this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400308 }
309
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400310 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100311 HBUINT16 format; /* Format identifier--format = 3 */
Behdad Esfahbode347fb92018-02-26 01:08:00 -0800312 FWORD xCoordinate; /* Horizontal value--in design units */
313 FWORD yCoordinate; /* Vertical value--in design units */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400314 OffsetTo<Device>
315 xDeviceTable; /* Offset to Device table for X
316 * coordinate-- from beginning of
317 * Anchor table (may be NULL) */
318 OffsetTo<Device>
319 yDeviceTable; /* Offset to Device table for Y
320 * coordinate-- from beginning of
321 * Anchor table (may be NULL) */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400322 public:
323 DEFINE_SIZE_STATIC (10);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400324};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400325
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400326struct Anchor
327{
Behdad Esfahbodfd034492018-01-17 16:46:51 -0800328 inline void get_anchor (hb_ot_apply_context_t *c, hb_codepoint_t glyph_id,
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100329 float *x, float *y) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400330 {
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400331 *x = *y = 0;
332 switch (u.format) {
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700333 case 1: u.format1.get_anchor (c, glyph_id, x, y); return;
334 case 2: u.format2.get_anchor (c, glyph_id, x, y); return;
335 case 3: u.format3.get_anchor (c, glyph_id, x, y); return;
Behdad Esfahbod7cdd6ab2018-01-10 04:33:08 +0100336 default: return;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400337 }
338 }
339
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300340 inline bool sanitize (hb_sanitize_context_t *c) const
341 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500342 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100343 if (!u.format.sanitize (c)) return_trace (false);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400344 switch (u.format) {
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100345 case 1: return_trace (u.format1.sanitize (c));
346 case 2: return_trace (u.format2.sanitize (c));
347 case 3: return_trace (u.format3.sanitize (c));
348 default:return_trace (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400349 }
350 }
351
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400352 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400353 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100354 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400355 AnchorFormat1 format1;
356 AnchorFormat2 format2;
357 AnchorFormat3 format3;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400358 } u;
Behdad Esfahboded074222010-05-10 18:08:46 -0400359 public:
Behdad Esfahbod596e4712010-05-10 18:47:48 -0400360 DEFINE_SIZE_UNION (2, format);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400361};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400362
363
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400364struct AnchorMatrix
365{
Behdad Esfahbode95e0312013-01-08 16:15:46 -0600366 inline const Anchor& get_anchor (unsigned int row, unsigned int col, unsigned int cols, bool *found) const {
367 *found = false;
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400368 if (unlikely (row >= rows || col >= cols)) return Null(Anchor);
Behdad Esfahbod093c5202014-12-12 21:07:53 -0800369 *found = !matrixZ[row * cols + col].is_null ();
370 return this+matrixZ[row * cols + col];
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400371 }
372
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300373 inline bool sanitize (hb_sanitize_context_t *c, unsigned int cols) const
374 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500375 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100376 if (!c->check_struct (this)) return_trace (false);
Behdad Esfahboddc9f2292016-09-09 15:40:15 -0700377 if (unlikely (_hb_unsigned_int_mul_overflows (rows, cols))) return_trace (false);
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400378 unsigned int count = rows * cols;
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100379 if (!c->check_array (matrixZ, matrixZ[0].static_size, count)) return_trace (false);
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400380 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100381 if (!matrixZ[i].sanitize (c, this)) return_trace (false);
382 return_trace (true);
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400383 }
384
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100385 HBUINT16 rows; /* Number of rows */
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400386 protected:
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400387 OffsetTo<Anchor>
Behdad Esfahbod093c5202014-12-12 21:07:53 -0800388 matrixZ[VAR]; /* Matrix of offsets to Anchor tables--
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400389 * from beginning of AnchorMatrix table */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400390 public:
Behdad Esfahbod093c5202014-12-12 21:07:53 -0800391 DEFINE_SIZE_ARRAY (2, matrixZ);
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400392};
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400393
394
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400395struct MarkRecord
396{
Behdad Esfahbod377bfc52009-05-21 04:58:24 -0400397 friend struct MarkArray;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400398
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300399 inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
400 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500401 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100402 return_trace (c->check_struct (this) && markAnchor.sanitize (c, base));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400403 }
404
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400405 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100406 HBUINT16 klass; /* Class defined for this mark */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400407 OffsetTo<Anchor>
408 markAnchor; /* Offset to Anchor table--from
409 * beginning of MarkArray table */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400410 public:
411 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400412};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400413
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400414struct MarkArray : ArrayOf<MarkRecord> /* Array of MarkRecords--in Coverage order */
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400415{
Behdad Esfahbodfd034492018-01-17 16:46:51 -0800416 inline bool apply (hb_ot_apply_context_t *c,
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400417 unsigned int mark_index, unsigned int glyph_index,
418 const AnchorMatrix &anchors, unsigned int class_count,
419 unsigned int glyph_pos) const
420 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500421 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200422 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400423 const MarkRecord &record = ArrayOf<MarkRecord>::operator[](mark_index);
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400424 unsigned int mark_class = record.klass;
425
426 const Anchor& mark_anchor = this + record.markAnchor;
Behdad Esfahbode95e0312013-01-08 16:15:46 -0600427 bool found;
428 const Anchor& glyph_anchor = anchors.get_anchor (glyph_index, mark_class, class_count, &found);
429 /* If this subtable doesn't have an anchor for this base and this class,
430 * return false such that the subsequent subtables have a chance at it. */
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100431 if (unlikely (!found)) return_trace (false);
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400432
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100433 float mark_x, mark_y, base_x, base_y;
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400434
Behdad Esfahbodb65aa1c2017-08-10 20:32:02 -0700435 buffer->unsafe_to_break (glyph_pos, buffer->idx);
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700436 mark_anchor.get_anchor (c, buffer->cur().codepoint, &mark_x, &mark_y);
437 glyph_anchor.get_anchor (c, buffer->info[glyph_pos].codepoint, &base_x, &base_y);
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400438
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200439 hb_glyph_position_t &o = buffer->cur_pos();
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100440 o.x_offset = round (base_x - mark_x);
441 o.y_offset = round (base_y - mark_y);
Behdad Esfahbod686567b2016-02-11 15:25:28 +0700442 o.attach_type() = ATTACH_TYPE_MARK;
Behdad Esfahbod0f6278d2016-02-11 14:49:10 +0700443 o.attach_chain() = (int) glyph_pos - (int) buffer->idx;
Behdad Esfahbod550bd142015-11-04 22:58:58 -0800444 buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400445
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200446 buffer->idx++;
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100447 return_trace (true);
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400448 }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400449
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300450 inline bool sanitize (hb_sanitize_context_t *c) const
451 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500452 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100453 return_trace (ArrayOf<MarkRecord>::sanitize (c, this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400454 }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400455};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400456
457
458/* Lookups */
459
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400460struct SinglePosFormat1
461{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500462 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
463 {
464 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod87cc5a62017-12-16 06:18:07 -0800465 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500466 }
467
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400468 inline const Coverage &get_coverage (void) const
469 {
470 return this+coverage;
471 }
472
Behdad Esfahbodfd034492018-01-17 16:46:51 -0800473 inline bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400474 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500475 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200476 hb_buffer_t *buffer = c->buffer;
477 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100478 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400479
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700480 valueFormat.apply_value (c, this, values, buffer->cur_pos());
Behdad Esfahbodf53d4342009-05-30 22:17:32 -0400481
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200482 buffer->idx++;
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100483 return_trace (true);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400484 }
485
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300486 inline bool sanitize (hb_sanitize_context_t *c) const
487 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500488 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100489 return_trace (c->check_struct (this) &&
490 coverage.sanitize (c, this) &&
491 valueFormat.sanitize_value (c, this, values));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400492 }
493
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400494 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100495 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400496 OffsetTo<Coverage>
497 coverage; /* Offset to Coverage table--from
498 * beginning of subtable */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400499 ValueFormat valueFormat; /* Defines the types of data in the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400500 * ValueRecord */
501 ValueRecord values; /* Defines positioning
502 * value(s)--applied to all glyphs in
503 * the Coverage table */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400504 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400505 DEFINE_SIZE_ARRAY (6, values);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400506};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400507
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400508struct SinglePosFormat2
509{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500510 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
511 {
512 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod87cc5a62017-12-16 06:18:07 -0800513 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500514 }
515
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400516 inline const Coverage &get_coverage (void) const
517 {
518 return this+coverage;
519 }
520
Behdad Esfahbodfd034492018-01-17 16:46:51 -0800521 inline bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400522 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500523 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200524 hb_buffer_t *buffer = c->buffer;
525 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100526 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400527
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100528 if (likely (index >= valueCount)) return_trace (false);
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400529
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700530 valueFormat.apply_value (c, this,
Behdad Esfahbod4b8487d2010-03-16 03:46:17 -0400531 &values[index * valueFormat.get_len ()],
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200532 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
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300538 inline bool sanitize (hb_sanitize_context_t *c) const
539 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500540 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100541 return_trace (c->check_struct (this) &&
542 coverage.sanitize (c, this) &&
543 valueFormat.sanitize_values (c, this, values, valueCount));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400544 }
545
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400546 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100547 HBUINT16 format; /* Format identifier--format = 2 */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400548 OffsetTo<Coverage>
549 coverage; /* Offset to Coverage table--from
550 * beginning of subtable */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400551 ValueFormat valueFormat; /* Defines the types of data in the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400552 * ValueRecord */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100553 HBUINT16 valueCount; /* Number of ValueRecords */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400554 ValueRecord values; /* Array of ValueRecords--positioning
555 * values applied to glyphs */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400556 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400557 DEFINE_SIZE_ARRAY (8, values);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400558};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400559
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400560struct SinglePos
561{
Behdad Esfahbod2005fa52012-11-22 14:38:10 -0500562 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500563 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400564 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -0800565 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -0400566 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400567 switch (u.format) {
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100568 case 1: return_trace (c->dispatch (u.format1));
569 case 2: return_trace (c->dispatch (u.format2));
570 default:return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400571 }
572 }
573
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400574 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400575 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100576 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400577 SinglePosFormat1 format1;
578 SinglePosFormat2 format2;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400579 } u;
580};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400581
582
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400583struct PairValueRecord
584{
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400585 friend struct PairSet;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400586
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400587 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400588 GlyphID secondGlyph; /* GlyphID of second glyph in the
589 * pair--first glyph is listed in the
590 * Coverage table */
591 ValueRecord values; /* Positioning data for the first glyph
592 * followed by for second glyph */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400593 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400594 DEFINE_SIZE_ARRAY (2, values);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400595};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400596
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400597struct PairSet
598{
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400599 friend struct PairPosFormat1;
600
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500601 inline void collect_glyphs (hb_collect_glyphs_context_t *c,
602 const ValueFormat *valueFormats) const
603 {
604 TRACE_COLLECT_GLYPHS (this);
605 unsigned int len1 = valueFormats[0].get_len ();
606 unsigned int len2 = valueFormats[1].get_len ();
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100607 unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500608
Behdad Esfahbod093c5202014-12-12 21:07:53 -0800609 const PairValueRecord *record = CastP<PairValueRecord> (arrayZ);
Behdad Esfahbod81f27df2017-12-16 06:12:06 -0800610 c->input->add_array (&record->secondGlyph, len, record_size);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500611 }
612
Behdad Esfahbodfd034492018-01-17 16:46:51 -0800613 inline bool apply (hb_ot_apply_context_t *c,
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400614 const ValueFormat *valueFormats,
615 unsigned int pos) const
616 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500617 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200618 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400619 unsigned int len1 = valueFormats[0].get_len ();
620 unsigned int len2 = valueFormats[1].get_len ();
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100621 unsigned int record_size = HBUINT16::static_size * (1 + len1 + len2);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400622
Behdad Esfahbod67dfa8c2015-01-19 17:00:31 -0800623 const PairValueRecord *record_array = CastP<PairValueRecord> (arrayZ);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500624 unsigned int count = len;
Behdad Esfahbod67dfa8c2015-01-19 17:00:31 -0800625
626 /* Hand-coded bsearch. */
627 if (unlikely (!count))
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100628 return_trace (false);
Behdad Esfahbod67dfa8c2015-01-19 17:00:31 -0800629 hb_codepoint_t x = buffer->info[pos].codepoint;
630 int min = 0, max = (int) count - 1;
631 while (min <= max)
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400632 {
Behdad Esfahbod67dfa8c2015-01-19 17:00:31 -0800633 int mid = (min + max) / 2;
634 const PairValueRecord *record = &StructAtOffset<PairValueRecord> (record_array, record_size * mid);
635 hb_codepoint_t mid_x = record->secondGlyph;
636 if (x < mid_x)
637 max = mid - 1;
638 else if (x > mid_x)
639 min = mid + 1;
640 else
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400641 {
Behdad Esfahbod2d158ca2017-08-10 20:19:15 -0700642 buffer->unsafe_to_break (buffer->idx, pos + 1);
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700643 valueFormats[0].apply_value (c, this, &record->values[0], buffer->cur_pos());
644 valueFormats[1].apply_value (c, this, &record->values[len1], buffer->pos[pos]);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400645 if (len2)
646 pos++;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200647 buffer->idx = pos;
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100648 return_trace (true);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400649 }
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400650 }
651
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100652 return_trace (false);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400653 }
654
655 struct sanitize_closure_t {
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300656 const void *base;
657 const ValueFormat *valueFormats;
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400658 unsigned int len1; /* valueFormats[0].get_len() */
659 unsigned int stride; /* 1 + len1 + len2 */
660 };
661
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300662 inline bool sanitize (hb_sanitize_context_t *c, const sanitize_closure_t *closure) const
663 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500664 TRACE_SANITIZE (this);
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400665 if (!(c->check_struct (this)
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100666 && c->check_array (arrayZ, HBUINT16::static_size * closure->stride, len))) return_trace (false);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400667
668 unsigned int count = len;
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300669 const PairValueRecord *record = CastP<PairValueRecord> (arrayZ);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100670 return_trace (closure->valueFormats[0].sanitize_values_stride_unsafe (c, closure->base, &record->values[0], count, closure->stride) &&
671 closure->valueFormats[1].sanitize_values_stride_unsafe (c, closure->base, &record->values[closure->len1], count, closure->stride));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400672 }
673
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400674 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100675 HBUINT16 len; /* Number of PairValueRecords */
676 HBUINT16 arrayZ[VAR]; /* Array of PairValueRecords--ordered
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400677 * by GlyphID of the second glyph */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400678 public:
Behdad Esfahbod093c5202014-12-12 21:07:53 -0800679 DEFINE_SIZE_ARRAY (2, arrayZ);
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400680};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400681
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400682struct PairPosFormat1
683{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500684 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
685 {
686 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod81f27df2017-12-16 06:12:06 -0800687 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500688 unsigned int count = pairSet.len;
689 for (unsigned int i = 0; i < count; i++)
Volker H. Simonisad273882016-12-14 18:56:17 +0100690 (this+pairSet[i]).collect_glyphs (c, valueFormat);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500691 }
692
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400693 inline const Coverage &get_coverage (void) const
694 {
695 return this+coverage;
696 }
697
Behdad Esfahbodfd034492018-01-17 16:46:51 -0800698 inline bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400699 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500700 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200701 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200702 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100703 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400704
Behdad Esfahbodfd034492018-01-17 16:46:51 -0800705 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
Behdad Esfahbodb051be52015-01-29 13:40:39 +0100706 skippy_iter.reset (buffer->idx, 1);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100707 if (!skippy_iter.next ()) return_trace (false);
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400708
Volker H. Simonisad273882016-12-14 18:56:17 +0100709 return_trace ((this+pairSet[index]).apply (c, valueFormat, skippy_iter.idx));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400710 }
711
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300712 inline bool sanitize (hb_sanitize_context_t *c) const
713 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500714 TRACE_SANITIZE (this);
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -0400715
Behdad Esfahbodf9666492015-10-13 00:30:50 -0400716 if (!c->check_struct (this)) return_trace (false);
717
Volker H. Simonisad273882016-12-14 18:56:17 +0100718 unsigned int len1 = valueFormat[0].get_len ();
719 unsigned int len2 = valueFormat[1].get_len ();
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400720 PairSet::sanitize_closure_t closure = {
721 this,
Volker H. Simonisad273882016-12-14 18:56:17 +0100722 valueFormat,
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400723 len1,
724 1 + len1 + len2
725 };
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -0400726
Behdad Esfahbodf9666492015-10-13 00:30:50 -0400727 return_trace (coverage.sanitize (c, this) && pairSet.sanitize (c, this, &closure));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400728 }
729
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400730 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100731 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400732 OffsetTo<Coverage>
733 coverage; /* Offset to Coverage table--from
734 * beginning of subtable */
Volker H. Simonisad273882016-12-14 18:56:17 +0100735 ValueFormat valueFormat[2]; /* [0] Defines the types of data in
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400736 * ValueRecord1--for the first glyph
737 * in the pair--may be zero (0) */
Volker H. Simonisad273882016-12-14 18:56:17 +0100738 /* [1] Defines the types of data in
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400739 * ValueRecord2--for the second glyph
740 * in the pair--may be zero (0) */
741 OffsetArrayOf<PairSet>
742 pairSet; /* Array of PairSet tables
743 * ordered by Coverage Index */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400744 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400745 DEFINE_SIZE_ARRAY (10, pairSet);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400746};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400747
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400748struct PairPosFormat2
749{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500750 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
751 {
752 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod81f27df2017-12-16 06:12:06 -0800753 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
Behdad Esfahbod71e6adf2017-12-16 11:07:37 -0500754 if (unlikely (!(this+classDef2).add_coverage (c->input))) return;
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500755 }
756
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400757 inline const Coverage &get_coverage (void) const
758 {
759 return this+coverage;
760 }
761
Behdad Esfahbodfd034492018-01-17 16:46:51 -0800762 inline bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400763 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500764 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200765 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200766 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100767 if (likely (index == NOT_COVERED)) return_trace (false);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400768
Behdad Esfahbodfd034492018-01-17 16:46:51 -0800769 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
Behdad Esfahbodb051be52015-01-29 13:40:39 +0100770 skippy_iter.reset (buffer->idx, 1);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100771 if (!skippy_iter.next ()) return_trace (false);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400772
773 unsigned int len1 = valueFormat1.get_len ();
774 unsigned int len2 = valueFormat2.get_len ();
775 unsigned int record_len = len1 + len2;
776
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200777 unsigned int klass1 = (this+classDef1).get_class (buffer->cur().codepoint);
778 unsigned int klass2 = (this+classDef2).get_class (buffer->info[skippy_iter.idx].codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100779 if (unlikely (klass1 >= class1Count || klass2 >= class2Count)) return_trace (false);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400780
Behdad Esfahbod2d158ca2017-08-10 20:19:15 -0700781 buffer->unsafe_to_break (buffer->idx, skippy_iter.idx + 1);
Behdad Esfahbod4b8487d2010-03-16 03:46:17 -0400782 const Value *v = &values[record_len * (klass1 * class2Count + klass2)];
Behdad Esfahbod151d93d2016-09-09 17:03:11 -0700783 valueFormat1.apply_value (c, this, v, buffer->cur_pos());
784 valueFormat2.apply_value (c, this, v + len1, buffer->pos[skippy_iter.idx]);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400785
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200786 buffer->idx = skippy_iter.idx;
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400787 if (len2)
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200788 buffer->idx++;
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400789
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100790 return_trace (true);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400791 }
792
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300793 inline bool sanitize (hb_sanitize_context_t *c) const
794 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500795 TRACE_SANITIZE (this);
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400796 if (!(c->check_struct (this)
797 && coverage.sanitize (c, this)
798 && classDef1.sanitize (c, this)
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100799 && classDef2.sanitize (c, this))) return_trace (false);
Behdad Esfahbod815a73e2009-08-14 17:31:16 -0400800
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -0400801 unsigned int len1 = valueFormat1.get_len ();
802 unsigned int len2 = valueFormat2.get_len ();
803 unsigned int stride = len1 + len2;
Behdad Esfahbod4b8487d2010-03-16 03:46:17 -0400804 unsigned int record_size = valueFormat1.get_size () + valueFormat2.get_size ();
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -0400805 unsigned int count = (unsigned int) class1Count * (unsigned int) class2Count;
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100806 return_trace (c->check_array (values, record_size, count) &&
807 valueFormat1.sanitize_values_stride_unsafe (c, this, &values[0], count, stride) &&
808 valueFormat2.sanitize_values_stride_unsafe (c, this, &values[len1], count, stride));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400809 }
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400810
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400811 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100812 HBUINT16 format; /* Format identifier--format = 2 */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400813 OffsetTo<Coverage>
814 coverage; /* Offset to Coverage table--from
815 * beginning of subtable */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400816 ValueFormat valueFormat1; /* ValueRecord definition--for the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400817 * first glyph of the pair--may be zero
818 * (0) */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400819 ValueFormat valueFormat2; /* ValueRecord definition--for the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400820 * second glyph of the pair--may be
821 * zero (0) */
822 OffsetTo<ClassDef>
823 classDef1; /* Offset to ClassDef table--from
824 * beginning of PairPos subtable--for
825 * the first glyph of the pair */
826 OffsetTo<ClassDef>
827 classDef2; /* Offset to ClassDef table--from
828 * beginning of PairPos subtable--for
829 * the second glyph of the pair */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100830 HBUINT16 class1Count; /* Number of classes in ClassDef1
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400831 * table--includes Class0 */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100832 HBUINT16 class2Count; /* Number of classes in ClassDef2
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400833 * table--includes Class0 */
834 ValueRecord values; /* Matrix of value pairs:
835 * class1-major, class2-minor,
836 * Each entry has value1 and value2 */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400837 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400838 DEFINE_SIZE_ARRAY (16, values);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400839};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400840
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400841struct PairPos
842{
Behdad Esfahbod2005fa52012-11-22 14:38:10 -0500843 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500844 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400845 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -0800846 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -0400847 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400848 switch (u.format) {
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100849 case 1: return_trace (c->dispatch (u.format1));
850 case 2: return_trace (c->dispatch (u.format2));
851 default:return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400852 }
853 }
854
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400855 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400856 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100857 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400858 PairPosFormat1 format1;
859 PairPosFormat2 format2;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400860 } u;
861};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400862
863
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400864struct EntryExitRecord
865{
Behdad Esfahbod569da922010-05-10 16:38:32 -0400866 friend struct CursivePosFormat1;
867
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300868 inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
869 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500870 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100871 return_trace (entryAnchor.sanitize (c, base) && exitAnchor.sanitize (c, base));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400872 }
873
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400874 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400875 OffsetTo<Anchor>
876 entryAnchor; /* Offset to EntryAnchor table--from
877 * beginning of CursivePos
878 * subtable--may be NULL */
879 OffsetTo<Anchor>
880 exitAnchor; /* Offset to ExitAnchor table--from
881 * beginning of CursivePos
882 * subtable--may be NULL */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400883 public:
884 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400885};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400886
Behdad Esfahbod65785752015-08-25 20:24:59 +0100887static void
888reverse_cursive_minor_offset (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction, unsigned int new_parent);
889
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400890struct CursivePosFormat1
891{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500892 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
893 {
894 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod81f27df2017-12-16 06:12:06 -0800895 if (unlikely (!(this+coverage).add_coverage (c->input))) return;
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500896 }
897
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400898 inline const Coverage &get_coverage (void) const
899 {
900 return this+coverage;
901 }
902
Behdad Esfahbodfd034492018-01-17 16:46:51 -0800903 inline bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -0400904 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500905 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200906 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbod0f7e6b22009-05-20 04:16:35 -0400907
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200908 const EntryExitRecord &this_record = entryExitRecord[(this+coverage).get_coverage (buffer->cur().codepoint)];
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100909 if (!this_record.exitAnchor) return_trace (false);
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -0400910
Behdad Esfahbodfd034492018-01-17 16:46:51 -0800911 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
Behdad Esfahbodb051be52015-01-29 13:40:39 +0100912 skippy_iter.reset (buffer->idx, 1);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100913 if (!skippy_iter.next ()) return_trace (false);
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -0400914
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200915 const EntryExitRecord &next_record = entryExitRecord[(this+coverage).get_coverage (buffer->info[skippy_iter.idx].codepoint)];
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100916 if (!next_record.entryAnchor) return_trace (false);
Behdad Esfahbodaf5d02a2010-10-27 11:54:26 -0400917
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200918 unsigned int i = buffer->idx;
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500919 unsigned int j = skippy_iter.idx;
Behdad Esfahbodaf5d02a2010-10-27 11:54:26 -0400920
Behdad Esfahbod882ebda2017-08-10 20:23:17 -0700921 buffer->unsafe_to_break (i, j);
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100922 float entry_x, entry_y, exit_x, exit_y;
Behdad Esfahbod49cb8782016-09-09 16:27:03 -0700923 (this+this_record.exitAnchor).get_anchor (c, buffer->info[i].codepoint, &exit_x, &exit_y);
924 (this+next_record.entryAnchor).get_anchor (c, buffer->info[j].codepoint, &entry_x, &entry_y);
Behdad Esfahbodcc83ae12009-05-27 00:17:37 -0400925
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200926 hb_glyph_position_t *pos = buffer->pos;
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400927
928 hb_position_t d;
929 /* Main-direction adjustment */
930 switch (c->direction) {
931 case HB_DIRECTION_LTR:
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100932 pos[i].x_advance = round (exit_x) + pos[i].x_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400933
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100934 d = round (entry_x) + pos[j].x_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400935 pos[j].x_advance -= d;
936 pos[j].x_offset -= d;
937 break;
938 case HB_DIRECTION_RTL:
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100939 d = round (exit_x) + pos[i].x_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400940 pos[i].x_advance -= d;
941 pos[i].x_offset -= d;
942
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100943 pos[j].x_advance = round (entry_x) + pos[j].x_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400944 break;
945 case HB_DIRECTION_TTB:
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100946 pos[i].y_advance = round (exit_y) + pos[i].y_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400947
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100948 d = round (entry_y) + pos[j].y_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400949 pos[j].y_advance -= d;
950 pos[j].y_offset -= d;
951 break;
952 case HB_DIRECTION_BTT:
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100953 d = round (exit_y) + pos[i].y_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400954 pos[i].y_advance -= d;
955 pos[i].y_offset -= d;
956
Behdad Esfahbod188ee6e2018-01-10 05:13:47 +0100957 pos[j].y_advance = round (entry_y);
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400958 break;
959 case HB_DIRECTION_INVALID:
960 default:
961 break;
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -0400962 }
963
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400964 /* Cross-direction adjustment */
Behdad Esfahbod58f2a732015-08-25 18:55:34 +0100965
966 /* We attach child to parent (think graph theory and rooted trees whereas
967 * the root stays on baseline and each node aligns itself against its
968 * parent.
969 *
970 * Optimize things for the case of RightToLeft, as that's most common in
971 * Arabinc. */
972 unsigned int child = i;
973 unsigned int parent = j;
974 hb_position_t x_offset = entry_x - exit_x;
975 hb_position_t y_offset = entry_y - exit_y;
976 if (!(c->lookup_props & LookupFlag::RightToLeft))
977 {
978 unsigned int k = child;
979 child = parent;
980 parent = k;
981 x_offset = -x_offset;
982 y_offset = -y_offset;
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -0400983 }
984
Behdad Esfahbod65785752015-08-25 20:24:59 +0100985 /* If child was already connected to someone else, walk through its old
986 * chain and reverse the link direction, such that the whole tree of its
987 * previous connection now attaches to new parent. Watch out for case
988 * where new parent is on the path from old chain...
989 */
990 reverse_cursive_minor_offset (pos, child, c->direction, parent);
991
Behdad Esfahbod686567b2016-02-11 15:25:28 +0700992 pos[child].attach_type() = ATTACH_TYPE_CURSIVE;
993 pos[child].attach_chain() = (int) parent - (int) child;
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +0700994 buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
Behdad Esfahbod58f2a732015-08-25 18:55:34 +0100995 if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction)))
996 pos[child].y_offset = y_offset;
997 else
998 pos[child].x_offset = x_offset;
999
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001000 buffer->idx = j;
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001001 return_trace (true);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001002 }
1003
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001004 inline bool sanitize (hb_sanitize_context_t *c) const
1005 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001006 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001007 return_trace (coverage.sanitize (c, this) && entryExitRecord.sanitize (c, this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001008 }
1009
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001010 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001011 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001012 OffsetTo<Coverage>
1013 coverage; /* Offset to Coverage table--from
1014 * beginning of subtable */
1015 ArrayOf<EntryExitRecord>
1016 entryExitRecord; /* Array of EntryExit records--in
1017 * Coverage Index order */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001018 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -04001019 DEFINE_SIZE_ARRAY (6, entryExitRecord);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001020};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001021
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001022struct CursivePos
1023{
Behdad Esfahbod2005fa52012-11-22 14:38:10 -05001024 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001025 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001026 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08001027 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04001028 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001029 switch (u.format) {
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001030 case 1: return_trace (c->dispatch (u.format1));
1031 default:return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001032 }
1033 }
1034
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001035 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001036 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001037 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001038 CursivePosFormat1 format1;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001039 } u;
1040};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001041
1042
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -04001043typedef AnchorMatrix BaseArray; /* base-major--
1044 * in order of BaseCoverage Index--,
1045 * mark-minor--
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001046 * ordered by class--zero-based. */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001047
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001048struct MarkBasePosFormat1
1049{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001050 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1051 {
1052 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod81f27df2017-12-16 06:12:06 -08001053 if (unlikely (!(this+markCoverage).add_coverage (c->input))) return;
1054 if (unlikely (!(this+baseCoverage).add_coverage (c->input))) return;
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001055 }
1056
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001057 inline const Coverage &get_coverage (void) const
1058 {
1059 return this+markCoverage;
1060 }
1061
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001062 inline bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001063 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001064 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001065 hb_buffer_t *buffer = c->buffer;
1066 unsigned int mark_index = (this+markCoverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001067 if (likely (mark_index == NOT_COVERED)) return_trace (false);
Behdad Esfahbod357ccde2009-05-21 06:32:01 -04001068
Behdad Esfahbod6ab92022016-02-11 16:57:52 +07001069 /* Now we search backwards for a non-mark glyph */
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001070 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
Behdad Esfahbodb051be52015-01-29 13:40:39 +01001071 skippy_iter.reset (buffer->idx, 1);
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -05001072 skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
Behdad Esfahbod7b84c532012-06-08 22:04:23 -04001073 do {
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001074 if (!skippy_iter.prev ()) return_trace (false);
Behdad Esfahbod9b5af3e2018-02-10 21:50:26 -06001075 /* We only want to attach to the first of a MultipleSubst sequence.
1076 * https://github.com/harfbuzz/harfbuzz/issues/740
Behdad Esfahbodd3c09802018-06-30 15:59:25 +04301077 * Reject others...
1078 * ...but stop if we find a mark in the MultipleSubst sequence:
1079 * https://github.com/harfbuzz/harfbuzz/issues/1020 */
Behdad Esfahbod5d98de12017-10-03 13:46:25 +02001080 if (!_hb_glyph_info_multiplied (&buffer->info[skippy_iter.idx]) ||
Behdad Esfahbod9b5af3e2018-02-10 21:50:26 -06001081 0 == _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]) ||
1082 (skippy_iter.idx == 0 ||
Behdad Esfahbodd3c09802018-06-30 15:59:25 +04301083 _hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx - 1]) ||
Behdad Esfahbod9b5af3e2018-02-10 21:50:26 -06001084 _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx]) !=
Behdad Esfahbod1c50a882018-02-18 15:57:09 -08001085 _hb_glyph_info_get_lig_id (&buffer->info[skippy_iter.idx - 1]) ||
1086 _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx]) !=
1087 _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx - 1]) + 1
1088 ))
Behdad Esfahbod5d98de12017-10-03 13:46:25 +02001089 break;
Behdad Esfahbod7b84c532012-06-08 22:04:23 -04001090 skippy_iter.reject ();
1091 } while (1);
Behdad Esfahbod0532ed12009-08-12 15:40:04 -04001092
Behdad Esfahbod101303d2013-10-18 00:42:39 +02001093 /* Checking that matched glyph is actually a base glyph by GDEF is too strong; disabled */
Behdad Esfahbod6ab92022016-02-11 16:57:52 +07001094 //if (!_hb_glyph_info_is_base_glyph (&buffer->info[skippy_iter.idx])) { return_trace (false); }
Behdad Esfahbod357ccde2009-05-21 06:32:01 -04001095
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001096 unsigned int base_index = (this+baseCoverage).get_coverage (buffer->info[skippy_iter.idx].codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001097 if (base_index == NOT_COVERED) return_trace (false);
Behdad Esfahbod357ccde2009-05-21 06:32:01 -04001098
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001099 return_trace ((this+markArray).apply (c, mark_index, base_index, this+baseArray, classCount, skippy_iter.idx));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001100 }
1101
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001102 inline bool sanitize (hb_sanitize_context_t *c) const
1103 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001104 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001105 return_trace (c->check_struct (this) &&
1106 markCoverage.sanitize (c, this) &&
1107 baseCoverage.sanitize (c, this) &&
1108 markArray.sanitize (c, this) &&
1109 baseArray.sanitize (c, this, (unsigned int) classCount));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001110 }
1111
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001112 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001113 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001114 OffsetTo<Coverage>
1115 markCoverage; /* Offset to MarkCoverage table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001116 * beginning of MarkBasePos subtable */
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001117 OffsetTo<Coverage>
1118 baseCoverage; /* Offset to BaseCoverage table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001119 * beginning of MarkBasePos subtable */
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001120 HBUINT16 classCount; /* Number of classes defined for marks */
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001121 OffsetTo<MarkArray>
1122 markArray; /* Offset to MarkArray table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001123 * beginning of MarkBasePos subtable */
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001124 OffsetTo<BaseArray>
1125 baseArray; /* Offset to BaseArray table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001126 * beginning of MarkBasePos subtable */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001127 public:
1128 DEFINE_SIZE_STATIC (12);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001129};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001130
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001131struct MarkBasePos
1132{
Behdad Esfahbod2005fa52012-11-22 14:38:10 -05001133 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001134 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001135 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08001136 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04001137 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001138 switch (u.format) {
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001139 case 1: return_trace (c->dispatch (u.format1));
1140 default:return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001141 }
1142 }
1143
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001144 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001145 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001146 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001147 MarkBasePosFormat1 format1;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001148 } u;
1149};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001150
1151
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -04001152typedef AnchorMatrix LigatureAttach; /* component-major--
1153 * in order of writing direction--,
1154 * mark-minor--
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001155 * ordered by class--zero-based. */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001156
Behdad Esfahbod3564ee52009-08-14 18:32:56 -04001157typedef OffsetListOf<LigatureAttach> LigatureArray;
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001158 /* Array of LigatureAttach
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001159 * tables ordered by
1160 * LigatureCoverage Index */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001161
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001162struct MarkLigPosFormat1
1163{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001164 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1165 {
1166 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod81f27df2017-12-16 06:12:06 -08001167 if (unlikely (!(this+markCoverage).add_coverage (c->input))) return;
1168 if (unlikely (!(this+ligatureCoverage).add_coverage (c->input))) return;
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001169 }
1170
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001171 inline const Coverage &get_coverage (void) const
1172 {
1173 return this+markCoverage;
1174 }
1175
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001176 inline bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001177 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001178 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001179 hb_buffer_t *buffer = c->buffer;
1180 unsigned int mark_index = (this+markCoverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001181 if (likely (mark_index == NOT_COVERED)) return_trace (false);
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001182
Behdad Esfahbod6ab92022016-02-11 16:57:52 +07001183 /* Now we search backwards for a non-mark glyph */
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001184 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
Behdad Esfahbodb051be52015-01-29 13:40:39 +01001185 skippy_iter.reset (buffer->idx, 1);
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -05001186 skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001187 if (!skippy_iter.prev ()) return_trace (false);
Behdad Esfahbod0532ed12009-08-12 15:40:04 -04001188
Behdad Esfahbod101303d2013-10-18 00:42:39 +02001189 /* Checking that matched glyph is actually a ligature by GDEF is too strong; disabled */
Behdad Esfahbod6ab92022016-02-11 16:57:52 +07001190 //if (!_hb_glyph_info_is_ligature (&buffer->info[skippy_iter.idx])) { return_trace (false); }
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001191
Behdad Esfahbod4ab97312012-01-16 22:05:08 -05001192 unsigned int j = skippy_iter.idx;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001193 unsigned int lig_index = (this+ligatureCoverage).get_coverage (buffer->info[j].codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001194 if (lig_index == NOT_COVERED) return_trace (false);
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001195
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001196 const LigatureArray& lig_array = this+ligatureArray;
Behdad Esfahbod3564ee52009-08-14 18:32:56 -04001197 const LigatureAttach& lig_attach = lig_array[lig_index];
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -04001198
1199 /* Find component to attach to */
Behdad Esfahbodb2b18ef2009-08-14 19:37:18 -04001200 unsigned int comp_count = lig_attach.rows;
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001201 if (unlikely (!comp_count)) return_trace (false);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001202
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001203 /* We must now check whether the ligature ID of the current mark glyph
1204 * is identical to the ligature ID of the found ligature. If yes, we
1205 * can directly use the component index. If not, we attach the mark
1206 * glyph to the last component of the ligature. */
Behdad Esfahbod0aef4252012-07-30 00:55:15 -04001207 unsigned int comp_index;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001208 unsigned int lig_id = _hb_glyph_info_get_lig_id (&buffer->info[j]);
1209 unsigned int mark_id = _hb_glyph_info_get_lig_id (&buffer->cur());
1210 unsigned int mark_comp = _hb_glyph_info_get_lig_comp (&buffer->cur());
Behdad Esfahbod0aef4252012-07-30 00:55:15 -04001211 if (lig_id && lig_id == mark_id && mark_comp > 0)
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001212 comp_index = MIN (comp_count, _hb_glyph_info_get_lig_comp (&buffer->cur())) - 1;
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001213 else
Behdad Esfahbodb2b18ef2009-08-14 19:37:18 -04001214 comp_index = comp_count - 1;
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001215
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001216 return_trace ((this+markArray).apply (c, mark_index, comp_index, lig_attach, classCount, j));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001217 }
1218
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001219 inline bool sanitize (hb_sanitize_context_t *c) const
1220 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001221 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001222 return_trace (c->check_struct (this) &&
1223 markCoverage.sanitize (c, this) &&
1224 ligatureCoverage.sanitize (c, this) &&
1225 markArray.sanitize (c, this) &&
1226 ligatureArray.sanitize (c, this, (unsigned int) classCount));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001227 }
1228
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001229 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001230 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001231 OffsetTo<Coverage>
1232 markCoverage; /* Offset to Mark Coverage table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001233 * beginning of MarkLigPos subtable */
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001234 OffsetTo<Coverage>
1235 ligatureCoverage; /* Offset to Ligature Coverage
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001236 * table--from beginning of MarkLigPos
1237 * subtable */
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001238 HBUINT16 classCount; /* Number of defined mark classes */
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001239 OffsetTo<MarkArray>
1240 markArray; /* Offset to MarkArray table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001241 * beginning of MarkLigPos subtable */
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001242 OffsetTo<LigatureArray>
1243 ligatureArray; /* Offset to LigatureArray table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001244 * beginning of MarkLigPos subtable */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001245 public:
1246 DEFINE_SIZE_STATIC (12);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001247};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001248
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001249struct MarkLigPos
1250{
Behdad Esfahbod2005fa52012-11-22 14:38:10 -05001251 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001252 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001253 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08001254 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04001255 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001256 switch (u.format) {
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001257 case 1: return_trace (c->dispatch (u.format1));
1258 default:return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001259 }
1260 }
1261
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001262 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001263 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001264 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001265 MarkLigPosFormat1 format1;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001266 } u;
1267};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001268
1269
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -04001270typedef AnchorMatrix Mark2Array; /* mark2-major--
1271 * in order of Mark2Coverage Index--,
1272 * mark1-minor--
1273 * ordered by class--zero-based. */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001274
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001275struct MarkMarkPosFormat1
1276{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001277 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1278 {
1279 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod81f27df2017-12-16 06:12:06 -08001280 if (unlikely (!(this+mark1Coverage).add_coverage (c->input))) return;
1281 if (unlikely (!(this+mark2Coverage).add_coverage (c->input))) return;
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001282 }
1283
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001284 inline const Coverage &get_coverage (void) const
1285 {
1286 return this+mark1Coverage;
1287 }
1288
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001289 inline bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001290 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001291 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001292 hb_buffer_t *buffer = c->buffer;
1293 unsigned int mark1_index = (this+mark1Coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001294 if (likely (mark1_index == NOT_COVERED)) return_trace (false);
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001295
1296 /* now we search backwards for a suitable mark glyph until a non-mark glyph */
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001297 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
Behdad Esfahbodb051be52015-01-29 13:40:39 +01001298 skippy_iter.reset (buffer->idx, 1);
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -05001299 skippy_iter.set_lookup_props (c->lookup_props & ~LookupFlag::IgnoreFlags);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001300 if (!skippy_iter.prev ()) return_trace (false);
Behdad Esfahbod0532ed12009-08-12 15:40:04 -04001301
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001302 if (!_hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx])) { return_trace (false); }
Behdad Esfahbod80ea5bd2009-05-26 17:58:37 -04001303
Behdad Esfahbod4ab97312012-01-16 22:05:08 -05001304 unsigned int j = skippy_iter.idx;
1305
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001306 unsigned int id1 = _hb_glyph_info_get_lig_id (&buffer->cur());
1307 unsigned int id2 = _hb_glyph_info_get_lig_id (&buffer->info[j]);
1308 unsigned int comp1 = _hb_glyph_info_get_lig_comp (&buffer->cur());
1309 unsigned int comp2 = _hb_glyph_info_get_lig_comp (&buffer->info[j]);
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001310
Behdad Esfahbod5d874d52012-07-28 21:05:25 -04001311 if (likely (id1 == id2)) {
1312 if (id1 == 0) /* Marks belonging to the same base. */
1313 goto good;
1314 else if (comp1 == comp2) /* Marks belonging to the same ligature component. */
1315 goto good;
1316 } else {
1317 /* If ligature ids don't match, it may be the case that one of the marks
1318 * itself is a ligature. In which case match. */
1319 if ((id1 > 0 && !comp1) || (id2 > 0 && !comp2))
1320 goto good;
1321 }
1322
1323 /* Didn't match. */
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001324 return_trace (false);
Behdad Esfahbod5d874d52012-07-28 21:05:25 -04001325
1326 good:
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001327 unsigned int mark2_index = (this+mark2Coverage).get_coverage (buffer->info[j].codepoint);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001328 if (mark2_index == NOT_COVERED) return_trace (false);
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001329
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001330 return_trace ((this+mark1Array).apply (c, mark1_index, mark2_index, this+mark2Array, classCount, j));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001331 }
1332
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001333 inline bool sanitize (hb_sanitize_context_t *c) const
1334 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001335 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001336 return_trace (c->check_struct (this) &&
1337 mark1Coverage.sanitize (c, this) &&
1338 mark2Coverage.sanitize (c, this) &&
1339 mark1Array.sanitize (c, this) &&
1340 mark2Array.sanitize (c, this, (unsigned int) classCount));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001341 }
1342
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001343 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001344 HBUINT16 format; /* Format identifier--format = 1 */
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001345 OffsetTo<Coverage>
1346 mark1Coverage; /* Offset to Combining Mark1 Coverage
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001347 * table--from beginning of MarkMarkPos
1348 * subtable */
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001349 OffsetTo<Coverage>
1350 mark2Coverage; /* Offset to Combining Mark2 Coverage
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001351 * table--from beginning of MarkMarkPos
1352 * subtable */
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001353 HBUINT16 classCount; /* Number of defined mark classes */
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001354 OffsetTo<MarkArray>
1355 mark1Array; /* Offset to Mark1Array table--from
1356 * beginning of MarkMarkPos subtable */
1357 OffsetTo<Mark2Array>
1358 mark2Array; /* Offset to Mark2Array table--from
1359 * beginning of MarkMarkPos subtable */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001360 public:
1361 DEFINE_SIZE_STATIC (12);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001362};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001363
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001364struct MarkMarkPos
1365{
Behdad Esfahbod2005fa52012-11-22 14:38:10 -05001366 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001367 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001368 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08001369 TRACE_DISPATCH (this, u.format);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04001370 if (unlikely (!c->may_dispatch (this, &u.format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001371 switch (u.format) {
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001372 case 1: return_trace (c->dispatch (u.format1));
1373 default:return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001374 }
1375 }
1376
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001377 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001378 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001379 HBUINT16 format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001380 MarkMarkPosFormat1 format1;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001381 } u;
1382};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001383
1384
Behdad Esfahbod08f1eed2012-11-23 16:51:43 -05001385struct ContextPos : Context {};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001386
Behdad Esfahbod08f1eed2012-11-23 16:51:43 -05001387struct ChainContextPos : ChainContext {};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001388
Behdad Esfahbod653eeb22012-11-23 16:57:36 -05001389struct ExtensionPos : Extension<ExtensionPos>
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001390{
Behdad Esfahbod7dddd4e2012-11-23 17:04:55 -05001391 typedef struct PosLookupSubTable LookupSubTable;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001392};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001393
1394
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04001395
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001396/*
1397 * PosLookup
1398 */
1399
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001400
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001401struct PosLookupSubTable
1402{
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001403 friend struct PosLookup;
1404
Behdad Esfahbodc6035cf2012-04-12 13:23:59 -04001405 enum Type {
Behdad Esfahbodff05d252009-05-20 03:53:00 -04001406 Single = 1,
1407 Pair = 2,
1408 Cursive = 3,
1409 MarkBase = 4,
1410 MarkLig = 5,
1411 MarkMark = 6,
1412 Context = 7,
1413 ChainContext = 8,
Behdad Esfahbod8f034d52009-08-18 16:41:59 -04001414 Extension = 9
Behdad Esfahbodff05d252009-05-20 03:53:00 -04001415 };
1416
Behdad Esfahbod2005fa52012-11-22 14:38:10 -05001417 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001418 inline typename context_t::return_t dispatch (context_t *c, unsigned int lookup_type) const
Behdad Esfahbodfaf0f202012-06-09 03:02:36 -04001419 {
Behdad Esfahbod00f6a8e2014-12-12 20:36:49 -08001420 TRACE_DISPATCH (this, lookup_type);
Behdad Esfahbodf396fbb2015-10-09 12:25:55 -04001421 if (unlikely (!c->may_dispatch (this, &u.sub_format))) return_trace (c->no_dispatch_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001422 switch (lookup_type) {
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001423 case Single: return_trace (u.single.dispatch (c));
1424 case Pair: return_trace (u.pair.dispatch (c));
1425 case Cursive: return_trace (u.cursive.dispatch (c));
1426 case MarkBase: return_trace (u.markBase.dispatch (c));
1427 case MarkLig: return_trace (u.markLig.dispatch (c));
1428 case MarkMark: return_trace (u.markMark.dispatch (c));
1429 case Context: return_trace (u.context.dispatch (c));
1430 case ChainContext: return_trace (u.chainContext.dispatch (c));
1431 case Extension: return_trace (u.extension.dispatch (c));
1432 default: return_trace (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001433 }
Behdad Esfahbodfaf0f202012-06-09 03:02:36 -04001434 }
1435
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001436 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001437 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001438 HBUINT16 sub_format;
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001439 SinglePos single;
1440 PairPos pair;
1441 CursivePos cursive;
1442 MarkBasePos markBase;
1443 MarkLigPos markLig;
1444 MarkMarkPos markMark;
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001445 ContextPos context;
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001446 ChainContextPos chainContext;
1447 ExtensionPos extension;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001448 } u;
Behdad Esfahboded074222010-05-10 18:08:46 -04001449 public:
Behdad Esfahbod6759ed92015-02-17 16:05:30 +03001450 DEFINE_SIZE_UNION (2, sub_format);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001451};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001452
1453
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001454struct PosLookup : Lookup
1455{
Behdad Esfahbod6d08c7f2012-07-11 18:01:27 -04001456 inline const PosLookupSubTable& get_subtable (unsigned int i) const
Behdad Esfahbod70366f52015-02-18 13:09:54 +03001457 { return Lookup::get_subtable<PosLookupSubTable> (i); }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001458
Behdad Esfahbode015b8f2013-05-03 17:34:29 -04001459 inline bool is_reverse (void) const
1460 {
1461 return false;
1462 }
1463
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001464 inline bool apply (hb_ot_apply_context_t *c) const
Behdad Esfahbodbd047d32015-02-19 10:47:18 +03001465 {
1466 TRACE_APPLY (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001467 return_trace (dispatch (c));
Behdad Esfahbodbd047d32015-02-19 10:47:18 +03001468 }
1469
Behdad Esfahbod780cd932013-05-03 17:33:16 -04001470 inline hb_collect_glyphs_context_t::return_t collect_glyphs (hb_collect_glyphs_context_t *c) const
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001471 {
1472 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001473 return_trace (dispatch (c));
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001474 }
1475
Behdad Esfahboda878c582012-08-01 21:18:54 -04001476 template <typename set_t>
1477 inline void add_coverage (set_t *glyphs) const
1478 {
Behdad Esfahbod8e36ccf2015-02-17 19:15:34 +03001479 hb_add_coverage_context_t<set_t> c (glyphs);
1480 dispatch (&c);
Behdad Esfahboda878c582012-08-01 21:18:54 -04001481 }
1482
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001483 static bool apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index);
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001484
Behdad Esfahbodee5464d2013-03-09 01:59:30 -05001485 template <typename context_t>
1486 static inline typename context_t::return_t dispatch_recurse_func (context_t *c, unsigned int lookup_index);
1487
1488 template <typename context_t>
1489 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod40c58922015-02-18 13:18:46 +03001490 { return Lookup::dispatch<PosLookupSubTable> (c); }
Behdad Esfahbodee5464d2013-03-09 01:59:30 -05001491
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001492 inline bool sanitize (hb_sanitize_context_t *c) const
1493 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001494 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001495 if (unlikely (!Lookup::sanitize (c))) return_trace (false);
1496 return_trace (dispatch (c));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001497 }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001498};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001499
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001500typedef OffsetListOf<PosLookup> PosLookupList;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001501
1502/*
Ebrahim Byagowia02c3ee2018-04-12 13:38:19 +04301503 * GPOS -- Glyph Positioning
1504 * https://docs.microsoft.com/en-us/typography/opentype/spec/gpos
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001505 */
1506
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001507struct GPOS : GSUBGPOS
1508{
Behdad Esfahbod6c48f202013-09-09 15:43:10 -04001509 static const hb_tag_t tableTag = HB_OT_TAG_GPOS;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001510
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001511 inline const PosLookup& get_lookup (unsigned int i) const
Behdad Esfahbod187454c2010-04-23 16:35:01 -04001512 { return CastR<PosLookup> (GSUBGPOS::get_lookup (i)); }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001513
Behdad Esfahbod05bd1b62012-07-30 19:30:01 -04001514 static inline void position_start (hb_font_t *font, hb_buffer_t *buffer);
Behdad Esfahbod7d8d58a2016-02-11 16:34:28 +07001515 static inline void position_finish_advances (hb_font_t *font, hb_buffer_t *buffer);
1516 static inline void position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer);
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04001517
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001518 inline bool sanitize (hb_sanitize_context_t *c) const
1519 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001520 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001521 if (unlikely (!GSUBGPOS::sanitize (c))) return_trace (false);
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001522 const OffsetTo<PosLookupList> &list = CastR<OffsetTo<PosLookupList> > (lookupList);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001523 return_trace (list.sanitize (c, this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001524 }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001525};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001526
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001527
1528static void
Behdad Esfahbod65785752015-08-25 20:24:59 +01001529reverse_cursive_minor_offset (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction, unsigned int new_parent)
1530{
Behdad Esfahbod686567b2016-02-11 15:25:28 +07001531 int chain = pos[i].attach_chain(), type = pos[i].attach_type();
1532 if (likely (!chain || 0 == (type & ATTACH_TYPE_CURSIVE)))
Behdad Esfahbod65785752015-08-25 20:24:59 +01001533 return;
1534
Behdad Esfahbod686567b2016-02-11 15:25:28 +07001535 pos[i].attach_chain() = 0;
Behdad Esfahbod65785752015-08-25 20:24:59 +01001536
Behdad Esfahbod686567b2016-02-11 15:25:28 +07001537 unsigned int j = (int) i + chain;
Behdad Esfahbod806ad8d2016-02-11 14:53:11 +07001538
Behdad Esfahbod65785752015-08-25 20:24:59 +01001539 /* Stop if we see new parent in the chain. */
1540 if (j == new_parent)
1541 return;
1542
1543 reverse_cursive_minor_offset (pos, j, direction, new_parent);
1544
1545 if (HB_DIRECTION_IS_HORIZONTAL (direction))
1546 pos[j].y_offset = -pos[i].y_offset;
1547 else
1548 pos[j].x_offset = -pos[i].x_offset;
1549
Behdad Esfahbod686567b2016-02-11 15:25:28 +07001550 pos[j].attach_chain() = -chain;
1551 pos[j].attach_type() = type;
Behdad Esfahbod65785752015-08-25 20:24:59 +01001552}
1553static void
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07001554propagate_attachment_offsets (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction)
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001555{
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07001556 /* Adjusts offsets of attached glyphs (both cursive and mark) to accumulate
1557 * offset of glyph they are attached to. */
Behdad Esfahbod686567b2016-02-11 15:25:28 +07001558 int chain = pos[i].attach_chain(), type = pos[i].attach_type();
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07001559 if (likely (!chain))
Behdad Esfahboddf5d5c62012-08-23 09:33:30 -04001560 return;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001561
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07001562 unsigned int j = (int) i + chain;
1563
Behdad Esfahbod686567b2016-02-11 15:25:28 +07001564 pos[i].attach_chain() = 0;
1565
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07001566 propagate_attachment_offsets (pos, j, direction);
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001567
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07001568 assert (!!(type & ATTACH_TYPE_MARK) ^ !!(type & ATTACH_TYPE_CURSIVE));
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001569
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07001570 if (type & ATTACH_TYPE_CURSIVE)
1571 {
1572 if (HB_DIRECTION_IS_HORIZONTAL (direction))
1573 pos[i].y_offset += pos[j].y_offset;
1574 else
1575 pos[i].x_offset += pos[j].x_offset;
1576 }
1577 else /*if (type & ATTACH_TYPE_MARK)*/
1578 {
Behdad Esfahboddf5d5c62012-08-23 09:33:30 -04001579 pos[i].x_offset += pos[j].x_offset;
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07001580 pos[i].y_offset += pos[j].y_offset;
Behdad Esfahbodebd74312016-02-24 15:53:40 +09001581
1582 assert (j < i);
1583 if (HB_DIRECTION_IS_FORWARD (direction))
1584 for (unsigned int k = j; k < i; k++) {
1585 pos[i].x_offset -= pos[k].x_advance;
1586 pos[i].y_offset -= pos[k].y_advance;
1587 }
1588 else
1589 for (unsigned int k = j + 1; k < i + 1; k++) {
1590 pos[i].x_offset += pos[k].x_advance;
1591 pos[i].y_offset += pos[k].y_advance;
1592 }
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07001593 }
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001594}
1595
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04001596void
Behdad Esfahbod05bd1b62012-07-30 19:30:01 -04001597GPOS::position_start (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
Behdad Esfahboda9ad3d32011-07-28 15:42:18 -04001598{
Behdad Esfahbodb65c0602011-07-28 16:48:43 -04001599 unsigned int count = buffer->len;
1600 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod686567b2016-02-11 15:25:28 +07001601 buffer->pos[i].attach_chain() = buffer->pos[i].attach_type() = 0;
Behdad Esfahboda9ad3d32011-07-28 15:42:18 -04001602}
1603
1604void
Behdad Esfahbod7d8d58a2016-02-11 16:34:28 +07001605GPOS::position_finish_advances (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
1606{
1607 //_hb_buffer_assert_gsubgpos_vars (buffer);
1608}
1609
1610void
1611GPOS::position_finish_offsets (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04001612{
Behdad Esfahbod8f3eebf2014-08-02 17:18:46 -04001613 _hb_buffer_assert_gsubgpos_vars (buffer);
1614
Ryan Lortie70566be2011-04-15 18:32:36 -04001615 unsigned int len;
1616 hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, &len);
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04001617 hb_direction_t direction = buffer->props.direction;
1618
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04001619 /* Handle attachments */
Behdad Esfahbod550bd142015-11-04 22:58:58 -08001620 if (buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT)
1621 for (unsigned int i = 0; i < len; i++)
Behdad Esfahbodcbc3a762016-02-11 16:48:13 +07001622 propagate_attachment_offsets (pos, i, direction);
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04001623}
1624
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001625
1626/* Out-of-class implementation for methods recursing */
1627
Behdad Esfahbodc6fb8432012-11-23 18:04:08 -05001628template <typename context_t>
Behdad Esfahbod6faff8e2014-04-28 14:29:39 -07001629/*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 -05001630{
1631 const GPOS &gpos = *(hb_ot_layout_from_face (c->face)->gpos);
1632 const PosLookup &l = gpos.get_lookup (lookup_index);
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001633 return l.dispatch (c);
Behdad Esfahbodc6fb8432012-11-23 18:04:08 -05001634}
1635
Behdad Esfahbodfd034492018-01-17 16:46:51 -08001636/*static*/ inline bool PosLookup::apply_recurse_func (hb_ot_apply_context_t *c, unsigned int lookup_index)
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001637{
Behdad Esfahbodea278d32012-07-27 02:12:28 -04001638 const GPOS &gpos = *(hb_ot_layout_from_face (c->face)->gpos);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001639 const PosLookup &l = gpos.get_lookup (lookup_index);
Behdad Esfahbod4c4e8f02012-11-24 01:13:20 -05001640 unsigned int saved_lookup_props = c->lookup_props;
Behdad Esfahbod2c8b3b22015-08-18 14:36:43 +01001641 unsigned int saved_lookup_index = c->lookup_index;
1642 c->set_lookup_index (lookup_index);
1643 c->set_lookup_props (l.get_props ());
Behdad Esfahbod1a232212015-02-19 10:40:23 +03001644 bool ret = l.dispatch (c);
Behdad Esfahbod2c8b3b22015-08-18 14:36:43 +01001645 c->set_lookup_index (saved_lookup_index);
Behdad Esfahbod77889932015-01-28 23:01:12 -08001646 c->set_lookup_props (saved_lookup_props);
Behdad Esfahbod4c4e8f02012-11-24 01:13:20 -05001647 return ret;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001648}
1649
1650
Behdad Esfahbod0f6278d2016-02-11 14:49:10 +07001651#undef attach_chain
Behdad Esfahbod686567b2016-02-11 15:25:28 +07001652#undef attach_type
Behdad Esfahbod194d4562010-10-27 23:09:10 -04001653
1654
Behdad Esfahbod7d52e662012-11-16 18:49:54 -08001655} /* namespace OT */
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -04001656
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -04001657
Behdad Esfahbod7a750ac2011-08-17 14:19:59 +02001658#endif /* HB_OT_LAYOUT_GPOS_TABLE_HH */