blob: d8e3e6e11d529a7e9993304fce3e9b5049684659 [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 Esfahbod194d4562010-10-27 23:09:10 -040039#define attach_lookback() var.u16[0] /* number of glyphs to go back to attach this glyph to its base */
40#define cursive_chain() var.i16[1] /* character to which this connects, may be positive or negative */
41
42
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040043/* Shared Tables: ValueRecord, Anchor Table, and MarkArray */
44
Behdad Esfahbodc2ddfd22010-05-06 13:06:15 -040045typedef USHORT Value;
Behdad Esfahbodc91facd2009-08-26 18:53:43 -040046
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -040047typedef Value ValueRecord[VAR];
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040048
Behdad Esfahbodfca6a0d2009-05-21 04:49:04 -040049struct ValueFormat : USHORT
50{
Behdad Esfahbodc6035cf2012-04-12 13:23:59 -040051 enum Flags {
Behdad Esfahbod76271002014-07-11 14:54:42 -040052 xPlacement = 0x0001u, /* Includes horizontal adjustment for placement */
53 yPlacement = 0x0002u, /* Includes vertical adjustment for placement */
54 xAdvance = 0x0004u, /* Includes horizontal adjustment for advance */
55 yAdvance = 0x0008u, /* Includes vertical adjustment for advance */
56 xPlaDevice = 0x0010u, /* Includes horizontal Device table for placement */
57 yPlaDevice = 0x0020u, /* Includes vertical Device table for placement */
58 xAdvDevice = 0x0040u, /* Includes horizontal Device table for advance */
59 yAdvDevice = 0x0080u, /* Includes vertical Device table for advance */
60 ignored = 0x0F00u, /* Was used in TrueType Open for MM fonts */
61 reserved = 0xF000u, /* For future use */
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -040062
Behdad Esfahbod76271002014-07-11 14:54:42 -040063 devices = 0x00F0u /* Mask for having any Device table */
Behdad Esfahbodfca6a0d2009-05-21 04:49:04 -040064 };
65
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -040066/* All fields are options. Only those available advance the value pointer. */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040067#if 0
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040068 SHORT xPlacement; /* Horizontal adjustment for
69 * placement--in design units */
70 SHORT yPlacement; /* Vertical adjustment for
71 * placement--in design units */
72 SHORT xAdvance; /* Horizontal adjustment for
73 * advance--in design units (only used
74 * for horizontal writing) */
75 SHORT yAdvance; /* Vertical adjustment for advance--in
76 * design units (only used for vertical
77 * writing) */
78 Offset xPlaDevice; /* Offset to Device table for
79 * horizontal placement--measured from
80 * beginning of PosTable (may be NULL) */
81 Offset yPlaDevice; /* Offset to Device table for vertical
82 * placement--measured from beginning
83 * of PosTable (may be NULL) */
84 Offset xAdvDevice; /* Offset to Device table for
85 * horizontal advance--measured from
86 * beginning of PosTable (may be NULL) */
87 Offset yAdvDevice; /* Offset to Device table for vertical
88 * advance--measured from beginning of
89 * PosTable (may be NULL) */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -040090#endif
91
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -040092 inline unsigned int get_len (void) const
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -040093 { return _hb_popcount32 ((unsigned int) *this); }
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -040094 inline unsigned int get_size (void) const
Behdad Esfahbode45d3f82010-05-06 19:33:31 -040095 { return get_len () * Value::static_size; }
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -040096
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -040097 void apply_value (hb_font_t *font,
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -040098 hb_direction_t direction,
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -040099 const void *base,
100 const Value *values,
101 hb_glyph_position_t &glyph_pos) const
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400102 {
103 unsigned int x_ppem, y_ppem;
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400104 unsigned int format = *this;
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400105 hb_bool_t horizontal = HB_DIRECTION_IS_HORIZONTAL (direction);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400106
107 if (!format) return;
108
Behdad Esfahbodb6f902a2011-05-11 00:04:15 -0400109 if (format & xPlacement) glyph_pos.x_offset += font->em_scale_x (get_short (values++));
110 if (format & yPlacement) glyph_pos.y_offset += font->em_scale_y (get_short (values++));
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400111 if (format & xAdvance) {
Konstantin Ritt08322592014-01-22 20:31:30 +0200112 if (likely (horizontal)) glyph_pos.x_advance += font->em_scale_x (get_short (values));
113 values++;
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400114 }
Behdad Esfahbodcc2086d2011-05-19 19:19:50 -0400115 /* y_advance values grow downward but font-space grows upward, hence negation */
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400116 if (format & yAdvance) {
Konstantin Ritt08322592014-01-22 20:31:30 +0200117 if (unlikely (!horizontal)) glyph_pos.y_advance -= font->em_scale_y (get_short (values));
118 values++;
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400119 }
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400120
Behdad Esfahboda8d960b2010-04-29 14:31:56 -0400121 if (!has_device ()) return;
122
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400123 x_ppem = font->x_ppem;
124 y_ppem = font->y_ppem;
Behdad Esfahboda8d960b2010-04-29 14:31:56 -0400125
126 if (!x_ppem && !y_ppem) return;
127
Behdad Esfahbod0090dc02009-07-30 16:28:45 -0400128 /* pixel -> fractional pixel */
129 if (format & xPlaDevice) {
Konstantin Ritt08322592014-01-22 20:31:30 +0200130 if (x_ppem) glyph_pos.x_offset += (base + get_device (values)).get_x_delta (font);
131 values++;
Behdad Esfahbod0090dc02009-07-30 16:28:45 -0400132 }
133 if (format & yPlaDevice) {
Konstantin Ritt08322592014-01-22 20:31:30 +0200134 if (y_ppem) glyph_pos.y_offset += (base + get_device (values)).get_y_delta (font);
135 values++;
Behdad Esfahbod0090dc02009-07-30 16:28:45 -0400136 }
137 if (format & xAdvDevice) {
Konstantin Ritt08322592014-01-22 20:31:30 +0200138 if (horizontal && x_ppem) glyph_pos.x_advance += (base + get_device (values)).get_x_delta (font);
139 values++;
Behdad Esfahbod0090dc02009-07-30 16:28:45 -0400140 }
141 if (format & yAdvDevice) {
Behdad Esfahbodcc2086d2011-05-19 19:19:50 -0400142 /* y_advance values grow downward but font-space grows upward, hence negation */
Konstantin Ritt08322592014-01-22 20:31:30 +0200143 if (!horizontal && y_ppem) glyph_pos.y_advance -= (base + get_device (values)).get_y_delta (font);
144 values++;
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400145 }
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400146 }
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400147
148 private:
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400149 inline bool sanitize_value_devices (hb_sanitize_context_t *c, void *base, Value *values) {
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400150 unsigned int format = *this;
151
152 if (format & xPlacement) values++;
153 if (format & yPlacement) values++;
154 if (format & xAdvance) values++;
155 if (format & yAdvance) values++;
156
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400157 if ((format & xPlaDevice) && !get_device (values++).sanitize (c, base)) return false;
158 if ((format & yPlaDevice) && !get_device (values++).sanitize (c, base)) return false;
159 if ((format & xAdvDevice) && !get_device (values++).sanitize (c, base)) return false;
160 if ((format & yAdvDevice) && !get_device (values++).sanitize (c, base)) return false;
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400161
162 return true;
163 }
164
Behdad Esfahbodc2ddfd22010-05-06 13:06:15 -0400165 static inline OffsetTo<Device>& get_device (Value* value)
166 { return *CastP<OffsetTo<Device> > (value); }
167 static inline const OffsetTo<Device>& get_device (const Value* value)
168 { return *CastP<OffsetTo<Device> > (value); }
169
170 static inline const SHORT& get_short (const Value* value)
171 { return *CastP<SHORT> (value); }
172
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400173 public:
174
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -0400175 inline bool has_device (void) const {
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400176 unsigned int format = *this;
177 return (format & devices) != 0;
178 }
179
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400180 inline bool sanitize_value (hb_sanitize_context_t *c, void *base, Value *values) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500181 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200182 return TRACE_RETURN (c->check_range (values, get_size ()) && (!has_device () || sanitize_value_devices (c, base, values)));
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400183 }
184
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400185 inline bool sanitize_values (hb_sanitize_context_t *c, void *base, Value *values, unsigned int count) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500186 TRACE_SANITIZE (this);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400187 unsigned int len = get_len ();
188
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200189 if (!c->check_array (values, get_size (), count)) return TRACE_RETURN (false);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400190
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200191 if (!has_device ()) return TRACE_RETURN (true);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400192
193 for (unsigned int i = 0; i < count; i++) {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400194 if (!sanitize_value_devices (c, base, values))
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200195 return TRACE_RETURN (false);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400196 values += len;
197 }
198
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200199 return TRACE_RETURN (true);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400200 }
201
Behdad Esfahbod278a91f2010-04-22 13:59:39 -0400202 /* Just sanitize referenced Device tables. Doesn't check the values themselves. */
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400203 inline bool sanitize_values_stride_unsafe (hb_sanitize_context_t *c, void *base, Value *values, unsigned int count, unsigned int stride) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500204 TRACE_SANITIZE (this);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400205
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200206 if (!has_device ()) return TRACE_RETURN (true);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400207
208 for (unsigned int i = 0; i < count; i++) {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400209 if (!sanitize_value_devices (c, base, values))
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200210 return TRACE_RETURN (false);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400211 values += stride;
212 }
213
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200214 return TRACE_RETURN (true);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400215 }
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400216};
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400217
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400218
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400219struct AnchorFormat1
220{
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400221 inline void get_anchor (hb_font_t *font, hb_codepoint_t glyph_id HB_UNUSED,
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400222 hb_position_t *x, hb_position_t *y) const
223 {
Behdad Esfahbodb6f902a2011-05-11 00:04:15 -0400224 *x = font->em_scale_x (xCoordinate);
225 *y = font->em_scale_y (yCoordinate);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400226 }
227
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400228 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500229 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200230 return TRACE_RETURN (c->check_struct (this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400231 }
232
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400233 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400234 USHORT format; /* Format identifier--format = 1 */
235 SHORT xCoordinate; /* Horizontal value--in design units */
236 SHORT yCoordinate; /* Vertical value--in design units */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400237 public:
238 DEFINE_SIZE_STATIC (6);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400239};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400240
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400241struct AnchorFormat2
242{
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400243 inline void get_anchor (hb_font_t *font, hb_codepoint_t glyph_id,
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400244 hb_position_t *x, hb_position_t *y) const
245 {
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400246 unsigned int x_ppem = font->x_ppem;
247 unsigned int y_ppem = font->y_ppem;
Behdad Esfahbod6f729b42010-04-29 03:59:06 -0400248 hb_position_t cx, cy;
Konstantin Ritt08322592014-01-22 20:31:30 +0200249 hb_bool_t ret;
Behdad Esfahbod6f729b42010-04-29 03:59:06 -0400250
Konstantin Ritt08322592014-01-22 20:31:30 +0200251 ret = (x_ppem || y_ppem) &&
252 font->get_glyph_contour_point_for_origin (glyph_id, anchorPoint, HB_DIRECTION_LTR, &cx, &cy);
253 *x = ret && x_ppem ? cx : font->em_scale_x (xCoordinate);
254 *y = ret && y_ppem ? cy : font->em_scale_y (yCoordinate);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400255 }
256
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400257 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500258 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200259 return TRACE_RETURN (c->check_struct (this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400260 }
261
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400262 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400263 USHORT format; /* Format identifier--format = 2 */
264 SHORT xCoordinate; /* Horizontal value--in design units */
265 SHORT yCoordinate; /* Vertical value--in design units */
266 USHORT anchorPoint; /* Index to glyph contour point */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400267 public:
268 DEFINE_SIZE_STATIC (8);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400269};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400270
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400271struct AnchorFormat3
272{
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400273 inline void get_anchor (hb_font_t *font, hb_codepoint_t glyph_id HB_UNUSED,
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400274 hb_position_t *x, hb_position_t *y) const
275 {
Behdad Esfahbodb6f902a2011-05-11 00:04:15 -0400276 *x = font->em_scale_x (xCoordinate);
277 *y = font->em_scale_y (yCoordinate);
Behdad Esfahbodc18ec2b2009-05-21 04:54:01 -0400278
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400279 if (font->x_ppem)
280 *x += (this+xDeviceTable).get_x_delta (font);
281 if (font->y_ppem)
282 *y += (this+yDeviceTable).get_x_delta (font);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400283 }
284
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400285 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500286 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200287 return TRACE_RETURN (c->check_struct (this) && xDeviceTable.sanitize (c, this) && yDeviceTable.sanitize (c, this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400288 }
289
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400290 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400291 USHORT format; /* Format identifier--format = 3 */
292 SHORT xCoordinate; /* Horizontal value--in design units */
293 SHORT yCoordinate; /* Vertical value--in design units */
294 OffsetTo<Device>
295 xDeviceTable; /* Offset to Device table for X
296 * coordinate-- from beginning of
297 * Anchor table (may be NULL) */
298 OffsetTo<Device>
299 yDeviceTable; /* Offset to Device table for Y
300 * coordinate-- from beginning of
301 * Anchor table (may be NULL) */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400302 public:
303 DEFINE_SIZE_STATIC (10);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400304};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400305
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400306struct Anchor
307{
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400308 inline void get_anchor (hb_font_t *font, hb_codepoint_t glyph_id,
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400309 hb_position_t *x, hb_position_t *y) const
310 {
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400311 *x = *y = 0;
312 switch (u.format) {
Behdad Esfahbod60fbb362011-05-19 18:46:15 -0400313 case 1: u.format1.get_anchor (font, glyph_id, x, y); return;
314 case 2: u.format2.get_anchor (font, glyph_id, x, y); return;
315 case 3: u.format3.get_anchor (font, glyph_id, x, y); return;
316 default: return;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400317 }
318 }
319
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400320 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500321 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200322 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400323 switch (u.format) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200324 case 1: return TRACE_RETURN (u.format1.sanitize (c));
325 case 2: return TRACE_RETURN (u.format2.sanitize (c));
326 case 3: return TRACE_RETURN (u.format3.sanitize (c));
327 default:return TRACE_RETURN (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400328 }
329 }
330
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400331 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400332 union {
333 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400334 AnchorFormat1 format1;
335 AnchorFormat2 format2;
336 AnchorFormat3 format3;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400337 } u;
Behdad Esfahboded074222010-05-10 18:08:46 -0400338 public:
Behdad Esfahbod596e4712010-05-10 18:47:48 -0400339 DEFINE_SIZE_UNION (2, format);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400340};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400341
342
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400343struct AnchorMatrix
344{
Behdad Esfahbode95e0312013-01-08 16:15:46 -0600345 inline const Anchor& get_anchor (unsigned int row, unsigned int col, unsigned int cols, bool *found) const {
346 *found = false;
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400347 if (unlikely (row >= rows || col >= cols)) return Null(Anchor);
Behdad Esfahbode95e0312013-01-08 16:15:46 -0600348 *found = !matrix[row * cols + col].is_null ();
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400349 return this+matrix[row * cols + col];
350 }
351
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400352 inline bool sanitize (hb_sanitize_context_t *c, unsigned int cols) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500353 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200354 if (!c->check_struct (this)) return TRACE_RETURN (false);
355 if (unlikely (rows > 0 && cols >= ((unsigned int) -1) / rows)) return TRACE_RETURN (false);
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400356 unsigned int count = rows * cols;
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200357 if (!c->check_array (matrix, matrix[0].static_size, count)) return TRACE_RETURN (false);
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400358 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200359 if (!matrix[i].sanitize (c, this)) return TRACE_RETURN (false);
360 return TRACE_RETURN (true);
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400361 }
362
363 USHORT rows; /* Number of rows */
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400364 protected:
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400365 OffsetTo<Anchor>
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500366 matrix[VAR]; /* Matrix of offsets to Anchor tables--
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400367 * from beginning of AnchorMatrix table */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400368 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400369 DEFINE_SIZE_ARRAY (2, matrix);
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400370};
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400371
372
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400373struct MarkRecord
374{
Behdad Esfahbod377bfc52009-05-21 04:58:24 -0400375 friend struct MarkArray;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400376
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400377 inline bool sanitize (hb_sanitize_context_t *c, void *base) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500378 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200379 return TRACE_RETURN (c->check_struct (this) && markAnchor.sanitize (c, base));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400380 }
381
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400382 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400383 USHORT klass; /* Class defined for this mark */
384 OffsetTo<Anchor>
385 markAnchor; /* Offset to Anchor table--from
386 * beginning of MarkArray table */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400387 public:
388 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400389};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400390
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400391struct MarkArray : ArrayOf<MarkRecord> /* Array of MarkRecords--in Coverage order */
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400392{
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400393 inline bool apply (hb_apply_context_t *c,
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400394 unsigned int mark_index, unsigned int glyph_index,
395 const AnchorMatrix &anchors, unsigned int class_count,
396 unsigned int glyph_pos) const
397 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500398 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200399 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400400 const MarkRecord &record = ArrayOf<MarkRecord>::operator[](mark_index);
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400401 unsigned int mark_class = record.klass;
402
403 const Anchor& mark_anchor = this + record.markAnchor;
Behdad Esfahbode95e0312013-01-08 16:15:46 -0600404 bool found;
405 const Anchor& glyph_anchor = anchors.get_anchor (glyph_index, mark_class, class_count, &found);
406 /* If this subtable doesn't have an anchor for this base and this class,
407 * return false such that the subsequent subtables have a chance at it. */
408 if (unlikely (!found)) return TRACE_RETURN (false);
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400409
410 hb_position_t mark_x, mark_y, base_x, base_y;
411
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200412 mark_anchor.get_anchor (c->font, buffer->cur().codepoint, &mark_x, &mark_y);
413 glyph_anchor.get_anchor (c->font, buffer->info[glyph_pos].codepoint, &base_x, &base_y);
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400414
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200415 hb_glyph_position_t &o = buffer->cur_pos();
Behdad Esfahbod194d4562010-10-27 23:09:10 -0400416 o.x_offset = base_x - mark_x;
417 o.y_offset = base_y - mark_y;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200418 o.attach_lookback() = buffer->idx - glyph_pos;
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400419
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200420 buffer->idx++;
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200421 return TRACE_RETURN (true);
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400422 }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400423
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400424 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500425 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200426 return TRACE_RETURN (ArrayOf<MarkRecord>::sanitize (c, this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400427 }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400428};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400429
430
431/* Lookups */
432
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400433struct SinglePosFormat1
434{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500435 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
436 {
437 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -0500438 (this+coverage).add_coverage (c->input);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500439 }
440
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400441 inline const Coverage &get_coverage (void) const
442 {
443 return this+coverage;
444 }
445
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400446 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400447 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500448 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200449 hb_buffer_t *buffer = c->buffer;
450 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200451 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400452
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400453 valueFormat.apply_value (c->font, c->direction, this,
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200454 values, buffer->cur_pos());
Behdad Esfahbodf53d4342009-05-30 22:17:32 -0400455
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200456 buffer->idx++;
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200457 return TRACE_RETURN (true);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400458 }
459
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400460 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500461 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200462 return TRACE_RETURN (c->check_struct (this) && coverage.sanitize (c, this) && valueFormat.sanitize_value (c, this, values));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400463 }
464
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400465 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400466 USHORT format; /* Format identifier--format = 1 */
467 OffsetTo<Coverage>
468 coverage; /* Offset to Coverage table--from
469 * beginning of subtable */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400470 ValueFormat valueFormat; /* Defines the types of data in the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400471 * ValueRecord */
472 ValueRecord values; /* Defines positioning
473 * value(s)--applied to all glyphs in
474 * the Coverage table */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400475 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400476 DEFINE_SIZE_ARRAY (6, values);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400477};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400478
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400479struct SinglePosFormat2
480{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500481 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
482 {
483 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -0500484 (this+coverage).add_coverage (c->input);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500485 }
486
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400487 inline const Coverage &get_coverage (void) const
488 {
489 return this+coverage;
490 }
491
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400492 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400493 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500494 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200495 hb_buffer_t *buffer = c->buffer;
496 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200497 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400498
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200499 if (likely (index >= valueCount)) return TRACE_RETURN (false);
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400500
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400501 valueFormat.apply_value (c->font, c->direction, this,
Behdad Esfahbod4b8487d2010-03-16 03:46:17 -0400502 &values[index * valueFormat.get_len ()],
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200503 buffer->cur_pos());
Behdad Esfahbodf53d4342009-05-30 22:17:32 -0400504
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200505 buffer->idx++;
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200506 return TRACE_RETURN (true);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400507 }
508
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400509 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500510 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200511 return TRACE_RETURN (c->check_struct (this) && coverage.sanitize (c, this) && valueFormat.sanitize_values (c, this, values, valueCount));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400512 }
513
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400514 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400515 USHORT format; /* Format identifier--format = 2 */
516 OffsetTo<Coverage>
517 coverage; /* Offset to Coverage table--from
518 * beginning of subtable */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400519 ValueFormat valueFormat; /* Defines the types of data in the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400520 * ValueRecord */
521 USHORT valueCount; /* Number of ValueRecords */
522 ValueRecord values; /* Array of ValueRecords--positioning
523 * values applied to glyphs */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400524 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400525 DEFINE_SIZE_ARRAY (8, values);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400526};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400527
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400528struct SinglePos
529{
Behdad Esfahbod2005fa52012-11-22 14:38:10 -0500530 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500531 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400532 {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500533 TRACE_DISPATCH (this);
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400534 switch (u.format) {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500535 case 1: return TRACE_RETURN (c->dispatch (u.format1));
536 case 2: return TRACE_RETURN (c->dispatch (u.format2));
Behdad Esfahbodf48ec0e2012-11-23 17:23:41 -0500537 default:return TRACE_RETURN (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400538 }
539 }
540
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400541 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500542 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200543 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400544 switch (u.format) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200545 case 1: return TRACE_RETURN (u.format1.sanitize (c));
546 case 2: return TRACE_RETURN (u.format2.sanitize (c));
547 default:return TRACE_RETURN (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400548 }
549 }
550
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400551 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400552 union {
553 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400554 SinglePosFormat1 format1;
555 SinglePosFormat2 format2;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400556 } u;
557};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400558
559
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400560struct PairValueRecord
561{
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400562 friend struct PairSet;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400563
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400564 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400565 GlyphID secondGlyph; /* GlyphID of second glyph in the
566 * pair--first glyph is listed in the
567 * Coverage table */
568 ValueRecord values; /* Positioning data for the first glyph
569 * followed by for second glyph */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400570 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400571 DEFINE_SIZE_ARRAY (2, values);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400572};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400573
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400574struct PairSet
575{
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400576 friend struct PairPosFormat1;
577
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500578 inline void collect_glyphs (hb_collect_glyphs_context_t *c,
579 const ValueFormat *valueFormats) const
580 {
581 TRACE_COLLECT_GLYPHS (this);
582 unsigned int len1 = valueFormats[0].get_len ();
583 unsigned int len2 = valueFormats[1].get_len ();
584 unsigned int record_size = USHORT::static_size * (1 + len1 + len2);
585
586 const PairValueRecord *record = CastP<PairValueRecord> (array);
587 unsigned int count = len;
588 for (unsigned int i = 0; i < count; i++)
589 {
Behdad Esfahbod83035932012-12-04 17:08:41 -0500590 c->input->add (record->secondGlyph);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500591 record = &StructAtOffset<PairValueRecord> (record, record_size);
592 }
593 }
594
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400595 inline bool apply (hb_apply_context_t *c,
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400596 const ValueFormat *valueFormats,
597 unsigned int pos) const
598 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500599 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200600 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400601 unsigned int len1 = valueFormats[0].get_len ();
602 unsigned int len2 = valueFormats[1].get_len ();
603 unsigned int record_size = USHORT::static_size * (1 + len1 + len2);
604
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400605 const PairValueRecord *record = CastP<PairValueRecord> (array);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500606 unsigned int count = len;
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400607 for (unsigned int i = 0; i < count; i++)
608 {
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500609 /* TODO bsearch */
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200610 if (buffer->info[pos].codepoint == record->secondGlyph)
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400611 {
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400612 valueFormats[0].apply_value (c->font, c->direction, this,
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200613 &record->values[0], buffer->cur_pos());
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400614 valueFormats[1].apply_value (c->font, c->direction, this,
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200615 &record->values[len1], buffer->pos[pos]);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400616 if (len2)
617 pos++;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200618 buffer->idx = pos;
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200619 return TRACE_RETURN (true);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400620 }
621 record = &StructAtOffset<PairValueRecord> (record, record_size);
622 }
623
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200624 return TRACE_RETURN (false);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400625 }
626
627 struct sanitize_closure_t {
628 void *base;
629 ValueFormat *valueFormats;
630 unsigned int len1; /* valueFormats[0].get_len() */
631 unsigned int stride; /* 1 + len1 + len2 */
632 };
633
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400634 inline bool sanitize (hb_sanitize_context_t *c, const sanitize_closure_t *closure) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500635 TRACE_SANITIZE (this);
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400636 if (!(c->check_struct (this)
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200637 && c->check_array (array, USHORT::static_size * closure->stride, len))) return TRACE_RETURN (false);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400638
639 unsigned int count = len;
640 PairValueRecord *record = CastP<PairValueRecord> (array);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200641 return TRACE_RETURN (closure->valueFormats[0].sanitize_values_stride_unsafe (c, closure->base, &record->values[0], count, closure->stride)
642 && closure->valueFormats[1].sanitize_values_stride_unsafe (c, closure->base, &record->values[closure->len1], count, closure->stride));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400643 }
644
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400645 protected:
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400646 USHORT len; /* Number of PairValueRecords */
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400647 USHORT array[VAR]; /* Array of PairValueRecords--ordered
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400648 * by GlyphID of the second glyph */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400649 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400650 DEFINE_SIZE_ARRAY (2, array);
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400651};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400652
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400653struct PairPosFormat1
654{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500655 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
656 {
657 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -0500658 (this+coverage).add_coverage (c->input);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500659 unsigned int count = pairSet.len;
660 for (unsigned int i = 0; i < count; i++)
661 (this+pairSet[i]).collect_glyphs (c, &valueFormat1);
662 }
663
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400664 inline const Coverage &get_coverage (void) const
665 {
666 return this+coverage;
667 }
668
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400669 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400670 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500671 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200672 hb_buffer_t *buffer = c->buffer;
673 hb_apply_context_t::skipping_forward_iterator_t skippy_iter (c, buffer->idx, 1);
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200674 if (skippy_iter.has_no_chance ()) return TRACE_RETURN (false);
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400675
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200676 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200677 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400678
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200679 if (!skippy_iter.next ()) return TRACE_RETURN (false);
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400680
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200681 return TRACE_RETURN ((this+pairSet[index]).apply (c, &valueFormat1, skippy_iter.idx));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400682 }
683
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400684 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500685 TRACE_SANITIZE (this);
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -0400686
687 unsigned int len1 = valueFormat1.get_len ();
688 unsigned int len2 = valueFormat2.get_len ();
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400689 PairSet::sanitize_closure_t closure = {
690 this,
691 &valueFormat1,
692 len1,
693 1 + len1 + len2
694 };
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -0400695
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200696 return TRACE_RETURN (c->check_struct (this) && coverage.sanitize (c, this) && pairSet.sanitize (c, this, &closure));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400697 }
698
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400699 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400700 USHORT format; /* Format identifier--format = 1 */
701 OffsetTo<Coverage>
702 coverage; /* Offset to Coverage table--from
703 * beginning of subtable */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400704 ValueFormat valueFormat1; /* Defines the types of data in
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400705 * ValueRecord1--for the first glyph
706 * in the pair--may be zero (0) */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400707 ValueFormat valueFormat2; /* Defines the types of data in
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400708 * ValueRecord2--for the second glyph
709 * in the pair--may be zero (0) */
710 OffsetArrayOf<PairSet>
711 pairSet; /* Array of PairSet tables
712 * ordered by Coverage Index */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400713 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400714 DEFINE_SIZE_ARRAY (10, pairSet);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400715};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400716
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400717struct PairPosFormat2
718{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500719 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
720 {
721 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -0500722 /* (this+coverage).add_coverage (c->input); // Don't need this. */
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500723
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500724 unsigned int count1 = class1Count;
725 const ClassDef &klass1 = this+classDef1;
726 for (unsigned int i = 0; i < count1; i++)
Behdad Esfahbod83035932012-12-04 17:08:41 -0500727 klass1.add_class (c->input, i);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500728
729 unsigned int count2 = class2Count;
730 const ClassDef &klass2 = this+classDef2;
731 for (unsigned int i = 0; i < count2; i++)
Behdad Esfahbod83035932012-12-04 17:08:41 -0500732 klass2.add_class (c->input, i);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500733 }
734
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400735 inline const Coverage &get_coverage (void) const
736 {
737 return this+coverage;
738 }
739
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400740 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400741 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500742 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200743 hb_buffer_t *buffer = c->buffer;
744 hb_apply_context_t::skipping_forward_iterator_t skippy_iter (c, buffer->idx, 1);
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200745 if (skippy_iter.has_no_chance ()) return TRACE_RETURN (false);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400746
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200747 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200748 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400749
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200750 if (!skippy_iter.next ()) return TRACE_RETURN (false);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400751
752 unsigned int len1 = valueFormat1.get_len ();
753 unsigned int len2 = valueFormat2.get_len ();
754 unsigned int record_len = len1 + len2;
755
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200756 unsigned int klass1 = (this+classDef1).get_class (buffer->cur().codepoint);
757 unsigned int klass2 = (this+classDef2).get_class (buffer->info[skippy_iter.idx].codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200758 if (unlikely (klass1 >= class1Count || klass2 >= class2Count)) return TRACE_RETURN (false);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400759
Behdad Esfahbod4b8487d2010-03-16 03:46:17 -0400760 const Value *v = &values[record_len * (klass1 * class2Count + klass2)];
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400761 valueFormat1.apply_value (c->font, c->direction, this,
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200762 v, buffer->cur_pos());
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400763 valueFormat2.apply_value (c->font, c->direction, this,
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200764 v + len1, buffer->pos[skippy_iter.idx]);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400765
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200766 buffer->idx = skippy_iter.idx;
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400767 if (len2)
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200768 buffer->idx++;
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400769
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200770 return TRACE_RETURN (true);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400771 }
772
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400773 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500774 TRACE_SANITIZE (this);
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400775 if (!(c->check_struct (this)
776 && coverage.sanitize (c, this)
777 && classDef1.sanitize (c, this)
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200778 && classDef2.sanitize (c, this))) return TRACE_RETURN (false);
Behdad Esfahbod815a73e2009-08-14 17:31:16 -0400779
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -0400780 unsigned int len1 = valueFormat1.get_len ();
781 unsigned int len2 = valueFormat2.get_len ();
782 unsigned int stride = len1 + len2;
Behdad Esfahbod4b8487d2010-03-16 03:46:17 -0400783 unsigned int record_size = valueFormat1.get_size () + valueFormat2.get_size ();
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -0400784 unsigned int count = (unsigned int) class1Count * (unsigned int) class2Count;
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200785 return TRACE_RETURN (c->check_array (values, record_size, count) &&
786 valueFormat1.sanitize_values_stride_unsafe (c, this, &values[0], count, stride) &&
787 valueFormat2.sanitize_values_stride_unsafe (c, this, &values[len1], count, stride));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400788 }
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400789
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400790 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400791 USHORT format; /* Format identifier--format = 2 */
792 OffsetTo<Coverage>
793 coverage; /* Offset to Coverage table--from
794 * beginning of subtable */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400795 ValueFormat valueFormat1; /* ValueRecord definition--for the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400796 * first glyph of the pair--may be zero
797 * (0) */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400798 ValueFormat valueFormat2; /* ValueRecord definition--for the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400799 * second glyph of the pair--may be
800 * zero (0) */
801 OffsetTo<ClassDef>
802 classDef1; /* Offset to ClassDef table--from
803 * beginning of PairPos subtable--for
804 * the first glyph of the pair */
805 OffsetTo<ClassDef>
806 classDef2; /* Offset to ClassDef table--from
807 * beginning of PairPos subtable--for
808 * the second glyph of the pair */
809 USHORT class1Count; /* Number of classes in ClassDef1
810 * table--includes Class0 */
811 USHORT class2Count; /* Number of classes in ClassDef2
812 * table--includes Class0 */
813 ValueRecord values; /* Matrix of value pairs:
814 * class1-major, class2-minor,
815 * Each entry has value1 and value2 */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400816 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400817 DEFINE_SIZE_ARRAY (16, values);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400818};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400819
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400820struct PairPos
821{
Behdad Esfahbod2005fa52012-11-22 14:38:10 -0500822 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500823 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400824 {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500825 TRACE_DISPATCH (this);
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400826 switch (u.format) {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500827 case 1: return TRACE_RETURN (c->dispatch (u.format1));
828 case 2: return TRACE_RETURN (c->dispatch (u.format2));
Behdad Esfahbodf48ec0e2012-11-23 17:23:41 -0500829 default:return TRACE_RETURN (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400830 }
831 }
832
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400833 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500834 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200835 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400836 switch (u.format) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200837 case 1: return TRACE_RETURN (u.format1.sanitize (c));
838 case 2: return TRACE_RETURN (u.format2.sanitize (c));
839 default:return TRACE_RETURN (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400840 }
841 }
842
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400843 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400844 union {
845 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400846 PairPosFormat1 format1;
847 PairPosFormat2 format2;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400848 } u;
849};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400850
851
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400852struct EntryExitRecord
853{
Behdad Esfahbod569da922010-05-10 16:38:32 -0400854 friend struct CursivePosFormat1;
855
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400856 inline bool sanitize (hb_sanitize_context_t *c, void *base) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500857 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200858 return TRACE_RETURN (entryAnchor.sanitize (c, base) && exitAnchor.sanitize (c, base));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400859 }
860
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400861 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400862 OffsetTo<Anchor>
863 entryAnchor; /* Offset to EntryAnchor table--from
864 * beginning of CursivePos
865 * subtable--may be NULL */
866 OffsetTo<Anchor>
867 exitAnchor; /* Offset to ExitAnchor table--from
868 * beginning of CursivePos
869 * subtable--may be NULL */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400870 public:
871 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400872};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400873
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400874struct CursivePosFormat1
875{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500876 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
877 {
878 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -0500879 (this+coverage).add_coverage (c->input);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500880 }
881
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400882 inline const Coverage &get_coverage (void) const
883 {
884 return this+coverage;
885 }
886
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400887 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -0400888 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500889 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200890 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbod0f7e6b22009-05-20 04:16:35 -0400891
892 /* We don't handle mark glyphs here. */
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200893 if (unlikely (_hb_glyph_info_is_mark (&buffer->cur()))) return TRACE_RETURN (false);
Behdad Esfahbod0f7e6b22009-05-20 04:16:35 -0400894
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200895 hb_apply_context_t::skipping_forward_iterator_t skippy_iter (c, buffer->idx, 1);
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200896 if (skippy_iter.has_no_chance ()) return TRACE_RETURN (false);
Behdad Esfahbod0f7e6b22009-05-20 04:16:35 -0400897
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200898 const EntryExitRecord &this_record = entryExitRecord[(this+coverage).get_coverage (buffer->cur().codepoint)];
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200899 if (!this_record.exitAnchor) return TRACE_RETURN (false);
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -0400900
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200901 if (!skippy_iter.next ()) return TRACE_RETURN (false);
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -0400902
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200903 const EntryExitRecord &next_record = entryExitRecord[(this+coverage).get_coverage (buffer->info[skippy_iter.idx].codepoint)];
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200904 if (!next_record.entryAnchor) return TRACE_RETURN (false);
Behdad Esfahbodaf5d02a2010-10-27 11:54:26 -0400905
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200906 unsigned int i = buffer->idx;
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500907 unsigned int j = skippy_iter.idx;
Behdad Esfahbodaf5d02a2010-10-27 11:54:26 -0400908
909 hb_position_t entry_x, entry_y, exit_x, exit_y;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200910 (this+this_record.exitAnchor).get_anchor (c->font, buffer->info[i].codepoint, &exit_x, &exit_y);
911 (this+next_record.entryAnchor).get_anchor (c->font, buffer->info[j].codepoint, &entry_x, &entry_y);
Behdad Esfahbodcc83ae12009-05-27 00:17:37 -0400912
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200913 hb_glyph_position_t *pos = buffer->pos;
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400914
915 hb_position_t d;
916 /* Main-direction adjustment */
917 switch (c->direction) {
918 case HB_DIRECTION_LTR:
919 pos[i].x_advance = exit_x + pos[i].x_offset;
920
921 d = entry_x + pos[j].x_offset;
922 pos[j].x_advance -= d;
923 pos[j].x_offset -= d;
924 break;
925 case HB_DIRECTION_RTL:
926 d = exit_x + pos[i].x_offset;
927 pos[i].x_advance -= d;
928 pos[i].x_offset -= d;
929
930 pos[j].x_advance = entry_x + pos[j].x_offset;
931 break;
932 case HB_DIRECTION_TTB:
933 pos[i].y_advance = exit_y + pos[i].y_offset;
934
935 d = entry_y + pos[j].y_offset;
936 pos[j].y_advance -= d;
937 pos[j].y_offset -= d;
938 break;
939 case HB_DIRECTION_BTT:
940 d = exit_y + pos[i].y_offset;
941 pos[i].y_advance -= d;
942 pos[i].y_offset -= d;
943
944 pos[j].y_advance = entry_y;
945 break;
946 case HB_DIRECTION_INVALID:
947 default:
948 break;
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -0400949 }
950
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400951 /* Cross-direction adjustment */
952 if (c->lookup_props & LookupFlag::RightToLeft) {
953 pos[i].cursive_chain() = j - i;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400954 if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction)))
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400955 pos[i].y_offset = entry_y - exit_y;
Behdad Esfahbod13528d02010-10-27 14:09:27 -0400956 else
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400957 pos[i].x_offset = entry_x - exit_x;
958 } else {
959 pos[j].cursive_chain() = i - j;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400960 if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction)))
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400961 pos[j].y_offset = exit_y - entry_y;
Behdad Esfahbod13528d02010-10-27 14:09:27 -0400962 else
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400963 pos[j].x_offset = exit_x - entry_x;
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -0400964 }
965
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200966 buffer->idx = j;
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200967 return TRACE_RETURN (true);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400968 }
969
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400970 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500971 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200972 return TRACE_RETURN (coverage.sanitize (c, this) && entryExitRecord.sanitize (c, this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400973 }
974
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400975 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400976 USHORT format; /* Format identifier--format = 1 */
977 OffsetTo<Coverage>
978 coverage; /* Offset to Coverage table--from
979 * beginning of subtable */
980 ArrayOf<EntryExitRecord>
981 entryExitRecord; /* Array of EntryExit records--in
982 * Coverage Index order */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400983 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400984 DEFINE_SIZE_ARRAY (6, entryExitRecord);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400985};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400986
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400987struct CursivePos
988{
Behdad Esfahbod2005fa52012-11-22 14:38:10 -0500989 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500990 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400991 {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500992 TRACE_DISPATCH (this);
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400993 switch (u.format) {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500994 case 1: return TRACE_RETURN (c->dispatch (u.format1));
Behdad Esfahbodf48ec0e2012-11-23 17:23:41 -0500995 default:return TRACE_RETURN (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400996 }
997 }
998
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400999 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001000 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001001 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001002 switch (u.format) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001003 case 1: return TRACE_RETURN (u.format1.sanitize (c));
1004 default:return TRACE_RETURN (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001005 }
1006 }
1007
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001008 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001009 union {
1010 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001011 CursivePosFormat1 format1;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001012 } u;
1013};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001014
1015
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -04001016typedef AnchorMatrix BaseArray; /* base-major--
1017 * in order of BaseCoverage Index--,
1018 * mark-minor--
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001019 * ordered by class--zero-based. */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001020
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001021struct MarkBasePosFormat1
1022{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001023 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1024 {
1025 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001026 (this+markCoverage).add_coverage (c->input);
1027 (this+baseCoverage).add_coverage (c->input);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001028 }
1029
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001030 inline const Coverage &get_coverage (void) const
1031 {
1032 return this+markCoverage;
1033 }
1034
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001035 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001036 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001037 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001038 hb_buffer_t *buffer = c->buffer;
1039 unsigned int mark_index = (this+markCoverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001040 if (likely (mark_index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbod357ccde2009-05-21 06:32:01 -04001041
1042 /* now we search backwards for a non-mark glyph */
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001043 hb_apply_context_t::skipping_backward_iterator_t skippy_iter (c, buffer->idx, 1);
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -05001044 skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
Behdad Esfahbod7b84c532012-06-08 22:04:23 -04001045 do {
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -05001046 if (!skippy_iter.prev ()) return TRACE_RETURN (false);
Behdad Esfahbod7b84c532012-06-08 22:04:23 -04001047 /* We only want to attach to the first of a MultipleSubst sequence. Reject others. */
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001048 if (0 == _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx])) break;
Behdad Esfahbod7b84c532012-06-08 22:04:23 -04001049 skippy_iter.reject ();
1050 } while (1);
Behdad Esfahbod0532ed12009-08-12 15:40:04 -04001051
Behdad Esfahbod101303d2013-10-18 00:42:39 +02001052 /* Checking that matched glyph is actually a base glyph by GDEF is too strong; disabled */
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001053 if (!_hb_glyph_info_is_base_glyph (&buffer->info[skippy_iter.idx])) { /*return TRACE_RETURN (false);*/ }
Behdad Esfahbod357ccde2009-05-21 06:32:01 -04001054
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001055 unsigned int base_index = (this+baseCoverage).get_coverage (buffer->info[skippy_iter.idx].codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001056 if (base_index == NOT_COVERED) return TRACE_RETURN (false);
Behdad Esfahbod357ccde2009-05-21 06:32:01 -04001057
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001058 return TRACE_RETURN ((this+markArray).apply (c, mark_index, base_index, this+baseArray, classCount, skippy_iter.idx));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001059 }
1060
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001061 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001062 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001063 return TRACE_RETURN (c->check_struct (this) && markCoverage.sanitize (c, this) && baseCoverage.sanitize (c, this) &&
1064 markArray.sanitize (c, this) && baseArray.sanitize (c, this, (unsigned int) classCount));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001065 }
1066
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001067 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001068 USHORT format; /* Format identifier--format = 1 */
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001069 OffsetTo<Coverage>
1070 markCoverage; /* Offset to MarkCoverage table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001071 * beginning of MarkBasePos subtable */
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001072 OffsetTo<Coverage>
1073 baseCoverage; /* Offset to BaseCoverage table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001074 * beginning of MarkBasePos subtable */
1075 USHORT classCount; /* Number of classes defined for marks */
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001076 OffsetTo<MarkArray>
1077 markArray; /* Offset to MarkArray table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001078 * beginning of MarkBasePos subtable */
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001079 OffsetTo<BaseArray>
1080 baseArray; /* Offset to BaseArray table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001081 * beginning of MarkBasePos subtable */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001082 public:
1083 DEFINE_SIZE_STATIC (12);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001084};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001085
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001086struct MarkBasePos
1087{
Behdad Esfahbod2005fa52012-11-22 14:38:10 -05001088 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001089 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001090 {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001091 TRACE_DISPATCH (this);
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001092 switch (u.format) {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001093 case 1: return TRACE_RETURN (c->dispatch (u.format1));
Behdad Esfahbodf48ec0e2012-11-23 17:23:41 -05001094 default:return TRACE_RETURN (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001095 }
1096 }
1097
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001098 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001099 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001100 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001101 switch (u.format) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001102 case 1: return TRACE_RETURN (u.format1.sanitize (c));
1103 default:return TRACE_RETURN (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001104 }
1105 }
1106
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001107 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001108 union {
1109 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001110 MarkBasePosFormat1 format1;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001111 } u;
1112};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001113
1114
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -04001115typedef AnchorMatrix LigatureAttach; /* component-major--
1116 * in order of writing direction--,
1117 * mark-minor--
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001118 * ordered by class--zero-based. */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001119
Behdad Esfahbod3564ee52009-08-14 18:32:56 -04001120typedef OffsetListOf<LigatureAttach> LigatureArray;
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001121 /* Array of LigatureAttach
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001122 * tables ordered by
1123 * LigatureCoverage Index */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001124
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001125struct MarkLigPosFormat1
1126{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001127 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1128 {
1129 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001130 (this+markCoverage).add_coverage (c->input);
1131 (this+ligatureCoverage).add_coverage (c->input);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001132 }
1133
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001134 inline const Coverage &get_coverage (void) const
1135 {
1136 return this+markCoverage;
1137 }
1138
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001139 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001140 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001141 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001142 hb_buffer_t *buffer = c->buffer;
1143 unsigned int mark_index = (this+markCoverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001144 if (likely (mark_index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001145
1146 /* now we search backwards for a non-mark glyph */
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001147 hb_apply_context_t::skipping_backward_iterator_t skippy_iter (c, buffer->idx, 1);
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -05001148 skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
1149 if (!skippy_iter.prev ()) return TRACE_RETURN (false);
Behdad Esfahbod0532ed12009-08-12 15:40:04 -04001150
Behdad Esfahbod101303d2013-10-18 00:42:39 +02001151 /* Checking that matched glyph is actually a ligature by GDEF is too strong; disabled */
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001152 if (!_hb_glyph_info_is_ligature (&buffer->info[skippy_iter.idx])) { /*return TRACE_RETURN (false);*/ }
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001153
Behdad Esfahbod4ab97312012-01-16 22:05:08 -05001154 unsigned int j = skippy_iter.idx;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001155 unsigned int lig_index = (this+ligatureCoverage).get_coverage (buffer->info[j].codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001156 if (lig_index == NOT_COVERED) return TRACE_RETURN (false);
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001157
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001158 const LigatureArray& lig_array = this+ligatureArray;
Behdad Esfahbod3564ee52009-08-14 18:32:56 -04001159 const LigatureAttach& lig_attach = lig_array[lig_index];
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -04001160
1161 /* Find component to attach to */
Behdad Esfahbodb2b18ef2009-08-14 19:37:18 -04001162 unsigned int comp_count = lig_attach.rows;
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001163 if (unlikely (!comp_count)) return TRACE_RETURN (false);
1164
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001165 /* We must now check whether the ligature ID of the current mark glyph
1166 * is identical to the ligature ID of the found ligature. If yes, we
1167 * can directly use the component index. If not, we attach the mark
1168 * glyph to the last component of the ligature. */
Behdad Esfahbod0aef4252012-07-30 00:55:15 -04001169 unsigned int comp_index;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001170 unsigned int lig_id = _hb_glyph_info_get_lig_id (&buffer->info[j]);
1171 unsigned int mark_id = _hb_glyph_info_get_lig_id (&buffer->cur());
1172 unsigned int mark_comp = _hb_glyph_info_get_lig_comp (&buffer->cur());
Behdad Esfahbod0aef4252012-07-30 00:55:15 -04001173 if (lig_id && lig_id == mark_id && mark_comp > 0)
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001174 comp_index = MIN (comp_count, _hb_glyph_info_get_lig_comp (&buffer->cur())) - 1;
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001175 else
Behdad Esfahbodb2b18ef2009-08-14 19:37:18 -04001176 comp_index = comp_count - 1;
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001177
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001178 return TRACE_RETURN ((this+markArray).apply (c, mark_index, comp_index, lig_attach, classCount, j));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001179 }
1180
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001181 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001182 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001183 return TRACE_RETURN (c->check_struct (this) && markCoverage.sanitize (c, this) && ligatureCoverage.sanitize (c, this) &&
1184 markArray.sanitize (c, this) && ligatureArray.sanitize (c, this, (unsigned int) classCount));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001185 }
1186
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001187 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001188 USHORT format; /* Format identifier--format = 1 */
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001189 OffsetTo<Coverage>
1190 markCoverage; /* Offset to Mark Coverage table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001191 * beginning of MarkLigPos subtable */
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001192 OffsetTo<Coverage>
1193 ligatureCoverage; /* Offset to Ligature Coverage
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001194 * table--from beginning of MarkLigPos
1195 * subtable */
1196 USHORT classCount; /* Number of defined mark classes */
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001197 OffsetTo<MarkArray>
1198 markArray; /* Offset to MarkArray table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001199 * beginning of MarkLigPos subtable */
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001200 OffsetTo<LigatureArray>
1201 ligatureArray; /* Offset to LigatureArray table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001202 * beginning of MarkLigPos subtable */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001203 public:
1204 DEFINE_SIZE_STATIC (12);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001205};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001206
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001207struct MarkLigPos
1208{
Behdad Esfahbod2005fa52012-11-22 14:38:10 -05001209 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001210 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001211 {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001212 TRACE_DISPATCH (this);
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001213 switch (u.format) {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001214 case 1: return TRACE_RETURN (c->dispatch (u.format1));
Behdad Esfahbodf48ec0e2012-11-23 17:23:41 -05001215 default:return TRACE_RETURN (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001216 }
1217 }
1218
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001219 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001220 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001221 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001222 switch (u.format) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001223 case 1: return TRACE_RETURN (u.format1.sanitize (c));
1224 default:return TRACE_RETURN (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001225 }
1226 }
1227
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001228 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001229 union {
1230 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001231 MarkLigPosFormat1 format1;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001232 } u;
1233};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001234
1235
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -04001236typedef AnchorMatrix Mark2Array; /* mark2-major--
1237 * in order of Mark2Coverage Index--,
1238 * mark1-minor--
1239 * ordered by class--zero-based. */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001240
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001241struct MarkMarkPosFormat1
1242{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001243 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1244 {
1245 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001246 (this+mark1Coverage).add_coverage (c->input);
1247 (this+mark2Coverage).add_coverage (c->input);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001248 }
1249
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001250 inline const Coverage &get_coverage (void) const
1251 {
1252 return this+mark1Coverage;
1253 }
1254
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001255 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001256 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001257 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001258 hb_buffer_t *buffer = c->buffer;
1259 unsigned int mark1_index = (this+mark1Coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001260 if (likely (mark1_index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001261
1262 /* now we search backwards for a suitable mark glyph until a non-mark glyph */
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001263 hb_apply_context_t::skipping_backward_iterator_t skippy_iter (c, buffer->idx, 1);
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -05001264 skippy_iter.set_lookup_props (c->lookup_props & ~LookupFlag::IgnoreFlags);
1265 if (!skippy_iter.prev ()) return TRACE_RETURN (false);
Behdad Esfahbod0532ed12009-08-12 15:40:04 -04001266
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001267 if (!_hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx])) { return TRACE_RETURN (false); }
Behdad Esfahbod80ea5bd2009-05-26 17:58:37 -04001268
Behdad Esfahbod4ab97312012-01-16 22:05:08 -05001269 unsigned int j = skippy_iter.idx;
1270
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001271 unsigned int id1 = _hb_glyph_info_get_lig_id (&buffer->cur());
1272 unsigned int id2 = _hb_glyph_info_get_lig_id (&buffer->info[j]);
1273 unsigned int comp1 = _hb_glyph_info_get_lig_comp (&buffer->cur());
1274 unsigned int comp2 = _hb_glyph_info_get_lig_comp (&buffer->info[j]);
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001275
Behdad Esfahbod5d874d52012-07-28 21:05:25 -04001276 if (likely (id1 == id2)) {
1277 if (id1 == 0) /* Marks belonging to the same base. */
1278 goto good;
1279 else if (comp1 == comp2) /* Marks belonging to the same ligature component. */
1280 goto good;
1281 } else {
1282 /* If ligature ids don't match, it may be the case that one of the marks
1283 * itself is a ligature. In which case match. */
1284 if ((id1 > 0 && !comp1) || (id2 > 0 && !comp2))
1285 goto good;
1286 }
1287
1288 /* Didn't match. */
1289 return TRACE_RETURN (false);
1290
1291 good:
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001292 unsigned int mark2_index = (this+mark2Coverage).get_coverage (buffer->info[j].codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001293 if (mark2_index == NOT_COVERED) return TRACE_RETURN (false);
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001294
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001295 return TRACE_RETURN ((this+mark1Array).apply (c, mark1_index, mark2_index, this+mark2Array, classCount, j));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001296 }
1297
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001298 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001299 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001300 return TRACE_RETURN (c->check_struct (this) && mark1Coverage.sanitize (c, this) &&
1301 mark2Coverage.sanitize (c, this) && mark1Array.sanitize (c, this)
1302 && mark2Array.sanitize (c, this, (unsigned int) classCount));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001303 }
1304
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001305 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001306 USHORT format; /* Format identifier--format = 1 */
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001307 OffsetTo<Coverage>
1308 mark1Coverage; /* Offset to Combining Mark1 Coverage
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001309 * table--from beginning of MarkMarkPos
1310 * subtable */
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001311 OffsetTo<Coverage>
1312 mark2Coverage; /* Offset to Combining Mark2 Coverage
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001313 * table--from beginning of MarkMarkPos
1314 * subtable */
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001315 USHORT classCount; /* Number of defined mark classes */
1316 OffsetTo<MarkArray>
1317 mark1Array; /* Offset to Mark1Array table--from
1318 * beginning of MarkMarkPos subtable */
1319 OffsetTo<Mark2Array>
1320 mark2Array; /* Offset to Mark2Array table--from
1321 * beginning of MarkMarkPos subtable */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001322 public:
1323 DEFINE_SIZE_STATIC (12);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001324};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001325
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001326struct MarkMarkPos
1327{
Behdad Esfahbod2005fa52012-11-22 14:38:10 -05001328 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001329 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001330 {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001331 TRACE_DISPATCH (this);
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001332 switch (u.format) {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001333 case 1: return TRACE_RETURN (c->dispatch (u.format1));
Behdad Esfahbodf48ec0e2012-11-23 17:23:41 -05001334 default:return TRACE_RETURN (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001335 }
1336 }
1337
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001338 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001339 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001340 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001341 switch (u.format) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001342 case 1: return TRACE_RETURN (u.format1.sanitize (c));
1343 default:return TRACE_RETURN (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001344 }
1345 }
1346
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001347 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001348 union {
1349 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001350 MarkMarkPosFormat1 format1;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001351 } u;
1352};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001353
1354
Behdad Esfahbod08f1eed2012-11-23 16:51:43 -05001355struct ContextPos : Context {};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001356
Behdad Esfahbod08f1eed2012-11-23 16:51:43 -05001357struct ChainContextPos : ChainContext {};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001358
Behdad Esfahbod653eeb22012-11-23 16:57:36 -05001359struct ExtensionPos : Extension<ExtensionPos>
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001360{
Behdad Esfahbod7dddd4e2012-11-23 17:04:55 -05001361 typedef struct PosLookupSubTable LookupSubTable;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001362};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001363
1364
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04001365
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001366/*
1367 * PosLookup
1368 */
1369
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001370
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001371struct PosLookupSubTable
1372{
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001373 friend struct PosLookup;
1374
Behdad Esfahbodc6035cf2012-04-12 13:23:59 -04001375 enum Type {
Behdad Esfahbodff05d252009-05-20 03:53:00 -04001376 Single = 1,
1377 Pair = 2,
1378 Cursive = 3,
1379 MarkBase = 4,
1380 MarkLig = 5,
1381 MarkMark = 6,
1382 Context = 7,
1383 ChainContext = 8,
Behdad Esfahbod8f034d52009-08-18 16:41:59 -04001384 Extension = 9
Behdad Esfahbodff05d252009-05-20 03:53:00 -04001385 };
1386
Behdad Esfahbod2005fa52012-11-22 14:38:10 -05001387 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001388 inline typename context_t::return_t dispatch (context_t *c, unsigned int lookup_type) const
Behdad Esfahbodfaf0f202012-06-09 03:02:36 -04001389 {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001390 TRACE_DISPATCH (this);
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001391 switch (lookup_type) {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001392 case Single: return TRACE_RETURN (u.single.dispatch (c));
1393 case Pair: return TRACE_RETURN (u.pair.dispatch (c));
1394 case Cursive: return TRACE_RETURN (u.cursive.dispatch (c));
1395 case MarkBase: return TRACE_RETURN (u.markBase.dispatch (c));
1396 case MarkLig: return TRACE_RETURN (u.markLig.dispatch (c));
1397 case MarkMark: return TRACE_RETURN (u.markMark.dispatch (c));
1398 case Context: return TRACE_RETURN (u.context.dispatch (c));
1399 case ChainContext: return TRACE_RETURN (u.chainContext.dispatch (c));
1400 case Extension: return TRACE_RETURN (u.extension.dispatch (c));
Behdad Esfahbodf48ec0e2012-11-23 17:23:41 -05001401 default: return TRACE_RETURN (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001402 }
Behdad Esfahbodfaf0f202012-06-09 03:02:36 -04001403 }
1404
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001405 inline bool sanitize (hb_sanitize_context_t *c, unsigned int lookup_type) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001406 TRACE_SANITIZE (this);
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001407 if (!u.header.sub_format.sanitize (c))
Behdad Esfahbodfaf0f202012-06-09 03:02:36 -04001408 return TRACE_RETURN (false);
Behdad Esfahbodfe9bc072010-05-10 21:39:24 -04001409 switch (lookup_type) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001410 case Single: return TRACE_RETURN (u.single.sanitize (c));
1411 case Pair: return TRACE_RETURN (u.pair.sanitize (c));
1412 case Cursive: return TRACE_RETURN (u.cursive.sanitize (c));
1413 case MarkBase: return TRACE_RETURN (u.markBase.sanitize (c));
1414 case MarkLig: return TRACE_RETURN (u.markLig.sanitize (c));
1415 case MarkMark: return TRACE_RETURN (u.markMark.sanitize (c));
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001416 case Context: return TRACE_RETURN (u.context.sanitize (c));
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001417 case ChainContext: return TRACE_RETURN (u.chainContext.sanitize (c));
1418 case Extension: return TRACE_RETURN (u.extension.sanitize (c));
1419 default: return TRACE_RETURN (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001420 }
1421 }
1422
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001423 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001424 union {
Behdad Esfahbod4e766ff2012-06-09 02:53:57 -04001425 struct {
Behdad Esfahbod2005fa52012-11-22 14:38:10 -05001426 USHORT sub_format;
Behdad Esfahbod4e766ff2012-06-09 02:53:57 -04001427 } header;
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001428 SinglePos single;
1429 PairPos pair;
1430 CursivePos cursive;
1431 MarkBasePos markBase;
1432 MarkLigPos markLig;
1433 MarkMarkPos markMark;
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001434 ContextPos context;
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001435 ChainContextPos chainContext;
1436 ExtensionPos extension;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001437 } u;
Behdad Esfahboded074222010-05-10 18:08:46 -04001438 public:
Behdad Esfahbod4e766ff2012-06-09 02:53:57 -04001439 DEFINE_SIZE_UNION (2, header.sub_format);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001440};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001441
1442
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001443struct PosLookup : Lookup
1444{
Behdad Esfahbod6d08c7f2012-07-11 18:01:27 -04001445 inline const PosLookupSubTable& get_subtable (unsigned int i) const
1446 { return this+CastR<OffsetArrayOf<PosLookupSubTable> > (subTable)[i]; }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001447
Behdad Esfahbode015b8f2013-05-03 17:34:29 -04001448 inline bool is_reverse (void) const
1449 {
1450 return false;
1451 }
1452
Behdad Esfahbod780cd932013-05-03 17:33:16 -04001453 inline hb_collect_glyphs_context_t::return_t collect_glyphs (hb_collect_glyphs_context_t *c) const
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001454 {
1455 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod1bcfa062012-12-04 16:58:09 -05001456 c->set_recurse_func (NULL);
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001457 return TRACE_RETURN (dispatch (c));
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001458 }
1459
Behdad Esfahboda878c582012-08-01 21:18:54 -04001460 template <typename set_t>
1461 inline void add_coverage (set_t *glyphs) const
1462 {
Behdad Esfahbod2005fa52012-11-22 14:38:10 -05001463 hb_get_coverage_context_t c;
Behdad Esfahboda878c582012-08-01 21:18:54 -04001464 const Coverage *last = NULL;
1465 unsigned int count = get_subtable_count ();
1466 for (unsigned int i = 0; i < count; i++) {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001467 const Coverage *coverage = &get_subtable (i).dispatch (&c, get_type ());
Behdad Esfahbod2005fa52012-11-22 14:38:10 -05001468 if (coverage != last) {
1469 coverage->add_coverage (glyphs);
1470 last = coverage;
Behdad Esfahboda878c582012-08-01 21:18:54 -04001471 }
1472 }
1473 }
1474
Behdad Esfahbod41ae6742012-04-11 17:11:05 -04001475 inline bool apply_once (hb_apply_context_t *c) const
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001476 {
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001477 TRACE_APPLY (this);
Behdad Esfahbod407fc122013-02-13 11:13:06 -05001478 if (!c->check_glyph_property (&c->buffer->cur(), c->lookup_props))
Behdad Esfahbod26514d52012-11-23 18:13:48 -05001479 return TRACE_RETURN (false);
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001480 return TRACE_RETURN (dispatch (c));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001481 }
1482
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001483 static bool apply_recurse_func (hb_apply_context_t *c, unsigned int lookup_index);
1484
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
1490 {
1491 TRACE_DISPATCH (this);
1492 unsigned int lookup_type = get_type ();
1493 unsigned int count = get_subtable_count ();
1494 for (unsigned int i = 0; i < count; i++) {
1495 typename context_t::return_t r = get_subtable (i).dispatch (c, lookup_type);
1496 if (c->stop_sublookup_iteration (r))
1497 return TRACE_RETURN (r);
1498 }
1499 return TRACE_RETURN (c->default_return_value ());
1500 }
1501
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001502 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001503 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001504 if (unlikely (!Lookup::sanitize (c))) return TRACE_RETURN (false);
Behdad Esfahbod6d08c7f2012-07-11 18:01:27 -04001505 OffsetArrayOf<PosLookupSubTable> &list = CastR<OffsetArrayOf<PosLookupSubTable> > (subTable);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001506 return TRACE_RETURN (list.sanitize (c, this, get_type ()));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001507 }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001508};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001509
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001510typedef OffsetListOf<PosLookup> PosLookupList;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001511
1512/*
Behdad Esfahbodae9877d2011-08-17 14:43:45 +02001513 * GPOS -- The Glyph Positioning Table
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001514 */
1515
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001516struct GPOS : GSUBGPOS
1517{
Behdad Esfahbod6c48f202013-09-09 15:43:10 -04001518 static const hb_tag_t tableTag = HB_OT_TAG_GPOS;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001519
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001520 inline const PosLookup& get_lookup (unsigned int i) const
Behdad Esfahbod187454c2010-04-23 16:35:01 -04001521 { return CastR<PosLookup> (GSUBGPOS::get_lookup (i)); }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001522
Behdad Esfahbod05bd1b62012-07-30 19:30:01 -04001523 static inline void position_start (hb_font_t *font, hb_buffer_t *buffer);
Behdad Esfahbod56800022013-02-12 09:44:57 -05001524 static inline void position_finish (hb_font_t *font, hb_buffer_t *buffer);
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04001525
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001526 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001527 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001528 if (unlikely (!GSUBGPOS::sanitize (c))) return TRACE_RETURN (false);
Behdad Esfahbod187454c2010-04-23 16:35:01 -04001529 OffsetTo<PosLookupList> &list = CastR<OffsetTo<PosLookupList> > (lookupList);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001530 return TRACE_RETURN (list.sanitize (c, this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001531 }
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001532 public:
1533 DEFINE_SIZE_STATIC (10);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001534};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001535
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001536
1537static void
1538fix_cursive_minor_offset (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction)
1539{
Behdad Esfahboddf5d5c62012-08-23 09:33:30 -04001540 unsigned int j = pos[i].cursive_chain();
1541 if (likely (!j))
1542 return;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001543
Behdad Esfahboddf5d5c62012-08-23 09:33:30 -04001544 j += i;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001545
Behdad Esfahboddf5d5c62012-08-23 09:33:30 -04001546 pos[i].cursive_chain() = 0;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001547
Behdad Esfahboddf5d5c62012-08-23 09:33:30 -04001548 fix_cursive_minor_offset (pos, j, direction);
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001549
Behdad Esfahboddf5d5c62012-08-23 09:33:30 -04001550 if (HB_DIRECTION_IS_HORIZONTAL (direction))
1551 pos[i].y_offset += pos[j].y_offset;
1552 else
1553 pos[i].x_offset += pos[j].x_offset;
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001554}
1555
1556static void
Behdad Esfahbod56800022013-02-12 09:44:57 -05001557fix_mark_attachment (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction)
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001558{
1559 if (likely (!(pos[i].attach_lookback())))
1560 return;
1561
1562 unsigned int j = i - pos[i].attach_lookback();
1563
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001564 pos[i].x_offset += pos[j].x_offset;
1565 pos[i].y_offset += pos[j].y_offset;
1566
1567 if (HB_DIRECTION_IS_FORWARD (direction))
Behdad Esfahbod21deab22011-05-30 11:08:40 -04001568 for (unsigned int k = j; k < i; k++) {
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001569 pos[i].x_offset -= pos[k].x_advance;
1570 pos[i].y_offset -= pos[k].y_advance;
1571 }
1572 else
Behdad Esfahbod21deab22011-05-30 11:08:40 -04001573 for (unsigned int k = j + 1; k < i + 1; k++) {
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001574 pos[i].x_offset += pos[k].x_advance;
1575 pos[i].y_offset += pos[k].y_advance;
1576 }
1577}
1578
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04001579void
Behdad Esfahbod05bd1b62012-07-30 19:30:01 -04001580GPOS::position_start (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
Behdad Esfahboda9ad3d32011-07-28 15:42:18 -04001581{
1582 buffer->clear_positions ();
Behdad Esfahbodb65c0602011-07-28 16:48:43 -04001583
1584 unsigned int count = buffer->len;
1585 for (unsigned int i = 0; i < count; i++)
1586 buffer->pos[i].attach_lookback() = buffer->pos[i].cursive_chain() = 0;
Behdad Esfahboda9ad3d32011-07-28 15:42:18 -04001587}
1588
1589void
Behdad Esfahbod56800022013-02-12 09:44:57 -05001590GPOS::position_finish (hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04001591{
Ryan Lortie70566be2011-04-15 18:32:36 -04001592 unsigned int len;
1593 hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, &len);
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04001594 hb_direction_t direction = buffer->props.direction;
1595
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001596 /* Handle cursive connections */
1597 for (unsigned int i = 0; i < len; i++)
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001598 fix_cursive_minor_offset (pos, i, direction);
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04001599
1600 /* Handle attachments */
Behdad Esfahbod7403e052011-05-24 21:04:15 -04001601 for (unsigned int i = 0; i < len; i++)
Behdad Esfahbod56800022013-02-12 09:44:57 -05001602 fix_mark_attachment (pos, i, direction);
Behdad Esfahbodb65c0602011-07-28 16:48:43 -04001603
Behdad Esfahbod101303d2013-10-18 00:42:39 +02001604 _hb_buffer_deallocate_gsubgpos_vars (buffer);
Behdad Esfahbod1e7c1fc2010-10-27 22:48:31 -04001605}
1606
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001607
1608/* Out-of-class implementation for methods recursing */
1609
Behdad Esfahbodc6fb8432012-11-23 18:04:08 -05001610template <typename context_t>
Behdad Esfahbod6faff8e2014-04-28 14:29:39 -07001611/*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 -05001612{
1613 const GPOS &gpos = *(hb_ot_layout_from_face (c->face)->gpos);
1614 const PosLookup &l = gpos.get_lookup (lookup_index);
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001615 return l.dispatch (c);
Behdad Esfahbodc6fb8432012-11-23 18:04:08 -05001616}
1617
Behdad Esfahbod6faff8e2014-04-28 14:29:39 -07001618/*static*/ inline bool PosLookup::apply_recurse_func (hb_apply_context_t *c, unsigned int lookup_index)
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001619{
Behdad Esfahbodea278d32012-07-27 02:12:28 -04001620 const GPOS &gpos = *(hb_ot_layout_from_face (c->face)->gpos);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001621 const PosLookup &l = gpos.get_lookup (lookup_index);
Behdad Esfahbod4c4e8f02012-11-24 01:13:20 -05001622 unsigned int saved_lookup_props = c->lookup_props;
Behdad Esfahbodec35a722012-11-22 16:05:59 -05001623 c->set_lookup (l);
Behdad Esfahbod4c4e8f02012-11-24 01:13:20 -05001624 bool ret = l.apply_once (c);
1625 c->lookup_props = saved_lookup_props;
Behdad Esfahbod4c4e8f02012-11-24 01:13:20 -05001626 return ret;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001627}
1628
1629
Behdad Esfahbod194d4562010-10-27 23:09:10 -04001630#undef attach_lookback
1631#undef cursive_chain
1632
1633
Behdad Esfahbod7d52e662012-11-16 18:49:54 -08001634} /* namespace OT */
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -04001635
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -04001636
Behdad Esfahbod7a750ac2011-08-17 14:19:59 +02001637#endif /* HB_OT_LAYOUT_GPOS_TABLE_HH */