blob: 103676b05d35681dbb77437e22c5f08b7c7091ae [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 Esfahbodfca6a0d2009-05-21 04:49:04 -040052 xPlacement = 0x0001, /* Includes horizontal adjustment for placement */
53 yPlacement = 0x0002, /* Includes vertical adjustment for placement */
54 xAdvance = 0x0004, /* Includes horizontal adjustment for advance */
55 yAdvance = 0x0008, /* Includes vertical adjustment for advance */
56 xPlaDevice = 0x0010, /* Includes horizontal Device table for placement */
57 yPlaDevice = 0x0020, /* Includes vertical Device table for placement */
58 xAdvDevice = 0x0040, /* Includes horizontal Device table for advance */
59 yAdvDevice = 0x0080, /* Includes vertical Device table for advance */
Behdad Esfahbode4b92b82009-05-26 15:38:53 -040060 ignored = 0x0F00, /* Was used in TrueType Open for MM fonts */
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -040061 reserved = 0xF000, /* For future use */
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -040062
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -040063 devices = 0x00F0 /* 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) {
112 if (likely (horizontal)) glyph_pos.x_advance += font->em_scale_x (get_short (values++)); else values++;
113 }
Behdad Esfahbodcc2086d2011-05-19 19:19:50 -0400114 /* y_advance values grow downward but font-space grows upward, hence negation */
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400115 if (format & yAdvance) {
116 if (unlikely (!horizontal)) glyph_pos.y_advance -= font->em_scale_y (get_short (values++)); else values++;
117 }
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400118
Behdad Esfahboda8d960b2010-04-29 14:31:56 -0400119 if (!has_device ()) return;
120
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400121 x_ppem = font->x_ppem;
122 y_ppem = font->y_ppem;
Behdad Esfahboda8d960b2010-04-29 14:31:56 -0400123
124 if (!x_ppem && !y_ppem) return;
125
Behdad Esfahbod0090dc02009-07-30 16:28:45 -0400126 /* pixel -> fractional pixel */
127 if (format & xPlaDevice) {
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400128 if (x_ppem) glyph_pos.x_offset += (base + get_device (values++)).get_x_delta (font); else values++;
Behdad Esfahbod0090dc02009-07-30 16:28:45 -0400129 }
130 if (format & yPlaDevice) {
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400131 if (y_ppem) glyph_pos.y_offset += (base + get_device (values++)).get_y_delta (font); else values++;
Behdad Esfahbod0090dc02009-07-30 16:28:45 -0400132 }
133 if (format & xAdvDevice) {
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400134 if (horizontal && x_ppem) glyph_pos.x_advance += (base + get_device (values++)).get_x_delta (font); else values++;
Behdad Esfahbod0090dc02009-07-30 16:28:45 -0400135 }
136 if (format & yAdvDevice) {
Behdad Esfahbodcc2086d2011-05-19 19:19:50 -0400137 /* y_advance values grow downward but font-space grows upward, hence negation */
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400138 if (!horizontal && y_ppem) glyph_pos.y_advance -= (base + get_device (values++)).get_y_delta (font); else values++;
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400139 }
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400140 }
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400141
142 private:
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400143 inline bool sanitize_value_devices (hb_sanitize_context_t *c, void *base, Value *values) {
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400144 unsigned int format = *this;
145
146 if (format & xPlacement) values++;
147 if (format & yPlacement) values++;
148 if (format & xAdvance) values++;
149 if (format & yAdvance) values++;
150
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400151 if ((format & xPlaDevice) && !get_device (values++).sanitize (c, base)) return false;
152 if ((format & yPlaDevice) && !get_device (values++).sanitize (c, base)) return false;
153 if ((format & xAdvDevice) && !get_device (values++).sanitize (c, base)) return false;
154 if ((format & yAdvDevice) && !get_device (values++).sanitize (c, base)) return false;
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400155
156 return true;
157 }
158
Behdad Esfahbodc2ddfd22010-05-06 13:06:15 -0400159 static inline OffsetTo<Device>& get_device (Value* value)
160 { return *CastP<OffsetTo<Device> > (value); }
161 static inline const OffsetTo<Device>& get_device (const Value* value)
162 { return *CastP<OffsetTo<Device> > (value); }
163
164 static inline const SHORT& get_short (const Value* value)
165 { return *CastP<SHORT> (value); }
166
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400167 public:
168
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -0400169 inline bool has_device (void) const {
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400170 unsigned int format = *this;
171 return (format & devices) != 0;
172 }
173
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400174 inline bool sanitize_value (hb_sanitize_context_t *c, void *base, Value *values) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500175 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200176 return TRACE_RETURN (c->check_range (values, get_size ()) && (!has_device () || sanitize_value_devices (c, base, values)));
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400177 }
178
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400179 inline bool sanitize_values (hb_sanitize_context_t *c, void *base, Value *values, unsigned int count) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500180 TRACE_SANITIZE (this);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400181 unsigned int len = get_len ();
182
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200183 if (!c->check_array (values, get_size (), count)) return TRACE_RETURN (false);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400184
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200185 if (!has_device ()) return TRACE_RETURN (true);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400186
187 for (unsigned int i = 0; i < count; i++) {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400188 if (!sanitize_value_devices (c, base, values))
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200189 return TRACE_RETURN (false);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400190 values += len;
191 }
192
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200193 return TRACE_RETURN (true);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400194 }
195
Behdad Esfahbod278a91f2010-04-22 13:59:39 -0400196 /* Just sanitize referenced Device tables. Doesn't check the values themselves. */
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400197 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 -0500198 TRACE_SANITIZE (this);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400199
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200200 if (!has_device ()) return TRACE_RETURN (true);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400201
202 for (unsigned int i = 0; i < count; i++) {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400203 if (!sanitize_value_devices (c, base, values))
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200204 return TRACE_RETURN (false);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400205 values += stride;
206 }
207
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200208 return TRACE_RETURN (true);
Behdad Esfahbod673a4ef2010-04-21 02:02:57 -0400209 }
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400210};
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400211
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400212
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400213struct AnchorFormat1
214{
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400215 inline void get_anchor (hb_font_t *font, hb_codepoint_t glyph_id HB_UNUSED,
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400216 hb_position_t *x, hb_position_t *y) const
217 {
Behdad Esfahbodb6f902a2011-05-11 00:04:15 -0400218 *x = font->em_scale_x (xCoordinate);
219 *y = font->em_scale_y (yCoordinate);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400220 }
221
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400222 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500223 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200224 return TRACE_RETURN (c->check_struct (this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400225 }
226
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400227 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400228 USHORT format; /* Format identifier--format = 1 */
229 SHORT xCoordinate; /* Horizontal value--in design units */
230 SHORT yCoordinate; /* Vertical value--in design units */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400231 public:
232 DEFINE_SIZE_STATIC (6);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400233};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400234
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400235struct AnchorFormat2
236{
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400237 inline void get_anchor (hb_font_t *font, hb_codepoint_t glyph_id,
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400238 hb_position_t *x, hb_position_t *y) const
239 {
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400240 unsigned int x_ppem = font->x_ppem;
241 unsigned int y_ppem = font->y_ppem;
Behdad Esfahbod6f729b42010-04-29 03:59:06 -0400242 hb_position_t cx, cy;
Behdad Esfahbod03420342010-11-03 15:40:07 -0400243 hb_bool_t ret = false;
Behdad Esfahbod6f729b42010-04-29 03:59:06 -0400244
245 if (x_ppem || y_ppem)
Behdad Esfahbod8fbfda92012-08-01 19:03:46 -0400246 ret = font->get_glyph_contour_point_for_origin (glyph_id, anchorPoint, HB_DIRECTION_LTR, &cx, &cy);
Behdad Esfahbodb6f902a2011-05-11 00:04:15 -0400247 *x = x_ppem && ret ? cx : font->em_scale_x (xCoordinate);
248 *y = y_ppem && ret ? cy : font->em_scale_y (yCoordinate);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400249 }
250
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400251 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500252 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200253 return TRACE_RETURN (c->check_struct (this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400254 }
255
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400256 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400257 USHORT format; /* Format identifier--format = 2 */
258 SHORT xCoordinate; /* Horizontal value--in design units */
259 SHORT yCoordinate; /* Vertical value--in design units */
260 USHORT anchorPoint; /* Index to glyph contour point */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400261 public:
262 DEFINE_SIZE_STATIC (8);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400263};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400264
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400265struct AnchorFormat3
266{
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400267 inline void get_anchor (hb_font_t *font, hb_codepoint_t glyph_id HB_UNUSED,
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400268 hb_position_t *x, hb_position_t *y) const
269 {
Behdad Esfahbodb6f902a2011-05-11 00:04:15 -0400270 *x = font->em_scale_x (xCoordinate);
271 *y = font->em_scale_y (yCoordinate);
Behdad Esfahbodc18ec2b2009-05-21 04:54:01 -0400272
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400273 if (font->x_ppem)
274 *x += (this+xDeviceTable).get_x_delta (font);
275 if (font->y_ppem)
276 *y += (this+yDeviceTable).get_x_delta (font);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400277 }
278
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400279 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500280 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200281 return TRACE_RETURN (c->check_struct (this) && xDeviceTable.sanitize (c, this) && yDeviceTable.sanitize (c, this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400282 }
283
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400284 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400285 USHORT format; /* Format identifier--format = 3 */
286 SHORT xCoordinate; /* Horizontal value--in design units */
287 SHORT yCoordinate; /* Vertical value--in design units */
288 OffsetTo<Device>
289 xDeviceTable; /* Offset to Device table for X
290 * coordinate-- from beginning of
291 * Anchor table (may be NULL) */
292 OffsetTo<Device>
293 yDeviceTable; /* Offset to Device table for Y
294 * coordinate-- from beginning of
295 * Anchor table (may be NULL) */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400296 public:
297 DEFINE_SIZE_STATIC (10);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400298};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400299
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400300struct Anchor
301{
Behdad Esfahbodabcfe9b2011-05-11 00:02:02 -0400302 inline void get_anchor (hb_font_t *font, hb_codepoint_t glyph_id,
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400303 hb_position_t *x, hb_position_t *y) const
304 {
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400305 *x = *y = 0;
306 switch (u.format) {
Behdad Esfahbod60fbb362011-05-19 18:46:15 -0400307 case 1: u.format1.get_anchor (font, glyph_id, x, y); return;
308 case 2: u.format2.get_anchor (font, glyph_id, x, y); return;
309 case 3: u.format3.get_anchor (font, glyph_id, x, y); return;
310 default: return;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400311 }
312 }
313
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400314 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500315 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200316 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400317 switch (u.format) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200318 case 1: return TRACE_RETURN (u.format1.sanitize (c));
319 case 2: return TRACE_RETURN (u.format2.sanitize (c));
320 case 3: return TRACE_RETURN (u.format3.sanitize (c));
321 default:return TRACE_RETURN (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400322 }
323 }
324
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400325 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400326 union {
327 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400328 AnchorFormat1 format1;
329 AnchorFormat2 format2;
330 AnchorFormat3 format3;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400331 } u;
Behdad Esfahboded074222010-05-10 18:08:46 -0400332 public:
Behdad Esfahbod596e4712010-05-10 18:47:48 -0400333 DEFINE_SIZE_UNION (2, format);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400334};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400335
336
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400337struct AnchorMatrix
338{
Behdad Esfahbode95e0312013-01-08 16:15:46 -0600339 inline const Anchor& get_anchor (unsigned int row, unsigned int col, unsigned int cols, bool *found) const {
340 *found = false;
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400341 if (unlikely (row >= rows || col >= cols)) return Null(Anchor);
Behdad Esfahbode95e0312013-01-08 16:15:46 -0600342 *found = !matrix[row * cols + col].is_null ();
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400343 return this+matrix[row * cols + col];
344 }
345
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400346 inline bool sanitize (hb_sanitize_context_t *c, unsigned int cols) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500347 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200348 if (!c->check_struct (this)) return TRACE_RETURN (false);
349 if (unlikely (rows > 0 && cols >= ((unsigned int) -1) / rows)) return TRACE_RETURN (false);
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400350 unsigned int count = rows * cols;
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200351 if (!c->check_array (matrix, matrix[0].static_size, count)) return TRACE_RETURN (false);
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400352 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200353 if (!matrix[i].sanitize (c, this)) return TRACE_RETURN (false);
354 return TRACE_RETURN (true);
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400355 }
356
357 USHORT rows; /* Number of rows */
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400358 protected:
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400359 OffsetTo<Anchor>
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500360 matrix[VAR]; /* Matrix of offsets to Anchor tables--
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400361 * from beginning of AnchorMatrix table */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400362 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400363 DEFINE_SIZE_ARRAY (2, matrix);
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400364};
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -0400365
366
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400367struct MarkRecord
368{
Behdad Esfahbod377bfc52009-05-21 04:58:24 -0400369 friend struct MarkArray;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400370
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400371 inline bool sanitize (hb_sanitize_context_t *c, void *base) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500372 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200373 return TRACE_RETURN (c->check_struct (this) && markAnchor.sanitize (c, base));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400374 }
375
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400376 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400377 USHORT klass; /* Class defined for this mark */
378 OffsetTo<Anchor>
379 markAnchor; /* Offset to Anchor table--from
380 * beginning of MarkArray table */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400381 public:
382 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400383};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400384
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400385struct MarkArray : ArrayOf<MarkRecord> /* Array of MarkRecords--in Coverage order */
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400386{
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400387 inline bool apply (hb_apply_context_t *c,
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400388 unsigned int mark_index, unsigned int glyph_index,
389 const AnchorMatrix &anchors, unsigned int class_count,
390 unsigned int glyph_pos) const
391 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500392 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200393 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400394 const MarkRecord &record = ArrayOf<MarkRecord>::operator[](mark_index);
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400395 unsigned int mark_class = record.klass;
396
397 const Anchor& mark_anchor = this + record.markAnchor;
Behdad Esfahbode95e0312013-01-08 16:15:46 -0600398 bool found;
399 const Anchor& glyph_anchor = anchors.get_anchor (glyph_index, mark_class, class_count, &found);
400 /* If this subtable doesn't have an anchor for this base and this class,
401 * return false such that the subsequent subtables have a chance at it. */
402 if (unlikely (!found)) return TRACE_RETURN (false);
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400403
404 hb_position_t mark_x, mark_y, base_x, base_y;
405
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200406 mark_anchor.get_anchor (c->font, buffer->cur().codepoint, &mark_x, &mark_y);
407 glyph_anchor.get_anchor (c->font, buffer->info[glyph_pos].codepoint, &base_x, &base_y);
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400408
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200409 hb_glyph_position_t &o = buffer->cur_pos();
Behdad Esfahbod194d4562010-10-27 23:09:10 -0400410 o.x_offset = base_x - mark_x;
411 o.y_offset = base_y - mark_y;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200412 o.attach_lookback() = buffer->idx - glyph_pos;
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400413
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200414 buffer->idx++;
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200415 return TRACE_RETURN (true);
Behdad Esfahbodb41f2102009-08-14 19:33:24 -0400416 }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400417
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400418 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500419 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200420 return TRACE_RETURN (ArrayOf<MarkRecord>::sanitize (c, this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400421 }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400422};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400423
424
425/* Lookups */
426
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400427struct SinglePosFormat1
428{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500429 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
430 {
431 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -0500432 (this+coverage).add_coverage (c->input);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500433 }
434
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400435 inline const Coverage &get_coverage (void) const
436 {
437 return this+coverage;
438 }
439
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400440 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400441 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500442 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200443 hb_buffer_t *buffer = c->buffer;
444 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200445 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400446
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400447 valueFormat.apply_value (c->font, c->direction, this,
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200448 values, buffer->cur_pos());
Behdad Esfahbodf53d4342009-05-30 22:17:32 -0400449
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200450 buffer->idx++;
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200451 return TRACE_RETURN (true);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400452 }
453
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400454 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500455 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200456 return TRACE_RETURN (c->check_struct (this) && coverage.sanitize (c, this) && valueFormat.sanitize_value (c, this, values));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400457 }
458
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400459 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400460 USHORT format; /* Format identifier--format = 1 */
461 OffsetTo<Coverage>
462 coverage; /* Offset to Coverage table--from
463 * beginning of subtable */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400464 ValueFormat valueFormat; /* Defines the types of data in the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400465 * ValueRecord */
466 ValueRecord values; /* Defines positioning
467 * value(s)--applied to all glyphs in
468 * the Coverage table */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400469 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400470 DEFINE_SIZE_ARRAY (6, values);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400471};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400472
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400473struct SinglePosFormat2
474{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500475 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
476 {
477 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -0500478 (this+coverage).add_coverage (c->input);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500479 }
480
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400481 inline const Coverage &get_coverage (void) const
482 {
483 return this+coverage;
484 }
485
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400486 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400487 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500488 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200489 hb_buffer_t *buffer = c->buffer;
490 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200491 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400492
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200493 if (likely (index >= valueCount)) return TRACE_RETURN (false);
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400494
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400495 valueFormat.apply_value (c->font, c->direction, this,
Behdad Esfahbod4b8487d2010-03-16 03:46:17 -0400496 &values[index * valueFormat.get_len ()],
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200497 buffer->cur_pos());
Behdad Esfahbodf53d4342009-05-30 22:17:32 -0400498
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200499 buffer->idx++;
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200500 return TRACE_RETURN (true);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400501 }
502
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400503 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500504 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200505 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 -0400506 }
507
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400508 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400509 USHORT format; /* Format identifier--format = 2 */
510 OffsetTo<Coverage>
511 coverage; /* Offset to Coverage table--from
512 * beginning of subtable */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400513 ValueFormat valueFormat; /* Defines the types of data in the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400514 * ValueRecord */
515 USHORT valueCount; /* Number of ValueRecords */
516 ValueRecord values; /* Array of ValueRecords--positioning
517 * values applied to glyphs */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400518 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400519 DEFINE_SIZE_ARRAY (8, values);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400520};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400521
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400522struct SinglePos
523{
Behdad Esfahbod2005fa52012-11-22 14:38:10 -0500524 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500525 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400526 {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500527 TRACE_DISPATCH (this);
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400528 switch (u.format) {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500529 case 1: return TRACE_RETURN (c->dispatch (u.format1));
530 case 2: return TRACE_RETURN (c->dispatch (u.format2));
Behdad Esfahbodf48ec0e2012-11-23 17:23:41 -0500531 default:return TRACE_RETURN (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400532 }
533 }
534
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400535 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500536 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200537 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400538 switch (u.format) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200539 case 1: return TRACE_RETURN (u.format1.sanitize (c));
540 case 2: return TRACE_RETURN (u.format2.sanitize (c));
541 default:return TRACE_RETURN (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400542 }
543 }
544
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400545 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400546 union {
547 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400548 SinglePosFormat1 format1;
549 SinglePosFormat2 format2;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400550 } u;
551};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400552
553
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400554struct PairValueRecord
555{
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400556 friend struct PairSet;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400557
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400558 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400559 GlyphID secondGlyph; /* GlyphID of second glyph in the
560 * pair--first glyph is listed in the
561 * Coverage table */
562 ValueRecord values; /* Positioning data for the first glyph
563 * followed by for second glyph */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400564 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400565 DEFINE_SIZE_ARRAY (2, values);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400566};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400567
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400568struct PairSet
569{
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400570 friend struct PairPosFormat1;
571
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500572 inline void collect_glyphs (hb_collect_glyphs_context_t *c,
573 const ValueFormat *valueFormats) const
574 {
575 TRACE_COLLECT_GLYPHS (this);
576 unsigned int len1 = valueFormats[0].get_len ();
577 unsigned int len2 = valueFormats[1].get_len ();
578 unsigned int record_size = USHORT::static_size * (1 + len1 + len2);
579
580 const PairValueRecord *record = CastP<PairValueRecord> (array);
581 unsigned int count = len;
582 for (unsigned int i = 0; i < count; i++)
583 {
Behdad Esfahbod83035932012-12-04 17:08:41 -0500584 c->input->add (record->secondGlyph);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500585 record = &StructAtOffset<PairValueRecord> (record, record_size);
586 }
587 }
588
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400589 inline bool apply (hb_apply_context_t *c,
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400590 const ValueFormat *valueFormats,
591 unsigned int pos) const
592 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500593 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200594 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400595 unsigned int len1 = valueFormats[0].get_len ();
596 unsigned int len2 = valueFormats[1].get_len ();
597 unsigned int record_size = USHORT::static_size * (1 + len1 + len2);
598
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400599 const PairValueRecord *record = CastP<PairValueRecord> (array);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500600 unsigned int count = len;
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400601 for (unsigned int i = 0; i < count; i++)
602 {
Behdad Esfahbod607feb72013-02-14 07:43:13 -0500603 /* TODO bsearch */
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200604 if (buffer->info[pos].codepoint == record->secondGlyph)
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400605 {
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400606 valueFormats[0].apply_value (c->font, c->direction, this,
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200607 &record->values[0], buffer->cur_pos());
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400608 valueFormats[1].apply_value (c->font, c->direction, this,
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200609 &record->values[len1], buffer->pos[pos]);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400610 if (len2)
611 pos++;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200612 buffer->idx = pos;
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200613 return TRACE_RETURN (true);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400614 }
615 record = &StructAtOffset<PairValueRecord> (record, record_size);
616 }
617
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200618 return TRACE_RETURN (false);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400619 }
620
621 struct sanitize_closure_t {
622 void *base;
623 ValueFormat *valueFormats;
624 unsigned int len1; /* valueFormats[0].get_len() */
625 unsigned int stride; /* 1 + len1 + len2 */
626 };
627
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400628 inline bool sanitize (hb_sanitize_context_t *c, const sanitize_closure_t *closure) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500629 TRACE_SANITIZE (this);
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400630 if (!(c->check_struct (this)
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200631 && c->check_array (array, USHORT::static_size * closure->stride, len))) return TRACE_RETURN (false);
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400632
633 unsigned int count = len;
634 PairValueRecord *record = CastP<PairValueRecord> (array);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200635 return TRACE_RETURN (closure->valueFormats[0].sanitize_values_stride_unsafe (c, closure->base, &record->values[0], count, closure->stride)
636 && closure->valueFormats[1].sanitize_values_stride_unsafe (c, closure->base, &record->values[closure->len1], count, closure->stride));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400637 }
638
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400639 protected:
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400640 USHORT len; /* Number of PairValueRecords */
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400641 USHORT array[VAR]; /* Array of PairValueRecords--ordered
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400642 * by GlyphID of the second glyph */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400643 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400644 DEFINE_SIZE_ARRAY (2, array);
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400645};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400646
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400647struct PairPosFormat1
648{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500649 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
650 {
651 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -0500652 (this+coverage).add_coverage (c->input);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500653 unsigned int count = pairSet.len;
654 for (unsigned int i = 0; i < count; i++)
655 (this+pairSet[i]).collect_glyphs (c, &valueFormat1);
656 }
657
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400658 inline const Coverage &get_coverage (void) const
659 {
660 return this+coverage;
661 }
662
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400663 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400664 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500665 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200666 hb_buffer_t *buffer = c->buffer;
667 hb_apply_context_t::skipping_forward_iterator_t skippy_iter (c, buffer->idx, 1);
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200668 if (skippy_iter.has_no_chance ()) return TRACE_RETURN (false);
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400669
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200670 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200671 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400672
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200673 if (!skippy_iter.next ()) return TRACE_RETURN (false);
Behdad Esfahbodb24ecba2009-05-19 22:16:04 -0400674
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200675 return TRACE_RETURN ((this+pairSet[index]).apply (c, &valueFormat1, skippy_iter.idx));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400676 }
677
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400678 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500679 TRACE_SANITIZE (this);
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -0400680
681 unsigned int len1 = valueFormat1.get_len ();
682 unsigned int len2 = valueFormat2.get_len ();
Behdad Esfahbod70c9bfd2010-05-11 00:23:50 -0400683 PairSet::sanitize_closure_t closure = {
684 this,
685 &valueFormat1,
686 len1,
687 1 + len1 + len2
688 };
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -0400689
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200690 return TRACE_RETURN (c->check_struct (this) && coverage.sanitize (c, this) && pairSet.sanitize (c, this, &closure));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400691 }
692
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400693 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400694 USHORT format; /* Format identifier--format = 1 */
695 OffsetTo<Coverage>
696 coverage; /* Offset to Coverage table--from
697 * beginning of subtable */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400698 ValueFormat valueFormat1; /* Defines the types of data in
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400699 * ValueRecord1--for the first glyph
700 * in the pair--may be zero (0) */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400701 ValueFormat valueFormat2; /* Defines the types of data in
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400702 * ValueRecord2--for the second glyph
703 * in the pair--may be zero (0) */
704 OffsetArrayOf<PairSet>
705 pairSet; /* Array of PairSet tables
706 * ordered by Coverage Index */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400707 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400708 DEFINE_SIZE_ARRAY (10, pairSet);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400709};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400710
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400711struct PairPosFormat2
712{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500713 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
714 {
715 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -0500716 /* (this+coverage).add_coverage (c->input); // Don't need this. */
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500717
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500718 unsigned int count1 = class1Count;
719 const ClassDef &klass1 = this+classDef1;
720 for (unsigned int i = 0; i < count1; i++)
Behdad Esfahbod83035932012-12-04 17:08:41 -0500721 klass1.add_class (c->input, i);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500722
723 unsigned int count2 = class2Count;
724 const ClassDef &klass2 = this+classDef2;
725 for (unsigned int i = 0; i < count2; i++)
Behdad Esfahbod83035932012-12-04 17:08:41 -0500726 klass2.add_class (c->input, i);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500727 }
728
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400729 inline const Coverage &get_coverage (void) const
730 {
731 return this+coverage;
732 }
733
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400734 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400735 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500736 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200737 hb_buffer_t *buffer = c->buffer;
738 hb_apply_context_t::skipping_forward_iterator_t skippy_iter (c, buffer->idx, 1);
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200739 if (skippy_iter.has_no_chance ()) return TRACE_RETURN (false);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400740
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200741 unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200742 if (likely (index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400743
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200744 if (!skippy_iter.next ()) return TRACE_RETURN (false);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400745
746 unsigned int len1 = valueFormat1.get_len ();
747 unsigned int len2 = valueFormat2.get_len ();
748 unsigned int record_len = len1 + len2;
749
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200750 unsigned int klass1 = (this+classDef1).get_class (buffer->cur().codepoint);
751 unsigned int klass2 = (this+classDef2).get_class (buffer->info[skippy_iter.idx].codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200752 if (unlikely (klass1 >= class1Count || klass2 >= class2Count)) return TRACE_RETURN (false);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400753
Behdad Esfahbod4b8487d2010-03-16 03:46:17 -0400754 const Value *v = &values[record_len * (klass1 * class2Count + klass2)];
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400755 valueFormat1.apply_value (c->font, c->direction, this,
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200756 v, buffer->cur_pos());
Behdad Esfahbod3b0bb852011-05-20 15:59:59 -0400757 valueFormat2.apply_value (c->font, c->direction, this,
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200758 v + len1, buffer->pos[skippy_iter.idx]);
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400759
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200760 buffer->idx = skippy_iter.idx;
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400761 if (len2)
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200762 buffer->idx++;
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400763
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200764 return TRACE_RETURN (true);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400765 }
766
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400767 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500768 TRACE_SANITIZE (this);
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400769 if (!(c->check_struct (this)
770 && coverage.sanitize (c, this)
771 && classDef1.sanitize (c, this)
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200772 && classDef2.sanitize (c, this))) return TRACE_RETURN (false);
Behdad Esfahbod815a73e2009-08-14 17:31:16 -0400773
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -0400774 unsigned int len1 = valueFormat1.get_len ();
775 unsigned int len2 = valueFormat2.get_len ();
776 unsigned int stride = len1 + len2;
Behdad Esfahbod4b8487d2010-03-16 03:46:17 -0400777 unsigned int record_size = valueFormat1.get_size () + valueFormat2.get_size ();
Behdad Esfahbodeba8b4f2010-03-29 00:04:12 -0400778 unsigned int count = (unsigned int) class1Count * (unsigned int) class2Count;
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200779 return TRACE_RETURN (c->check_array (values, record_size, count) &&
780 valueFormat1.sanitize_values_stride_unsafe (c, this, &values[0], count, stride) &&
781 valueFormat2.sanitize_values_stride_unsafe (c, this, &values[len1], count, stride));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400782 }
Behdad Esfahbod70632ad2009-05-19 22:30:09 -0400783
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400784 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400785 USHORT format; /* Format identifier--format = 2 */
786 OffsetTo<Coverage>
787 coverage; /* Offset to Coverage table--from
788 * beginning of subtable */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400789 ValueFormat valueFormat1; /* ValueRecord definition--for the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400790 * first glyph of the pair--may be zero
791 * (0) */
Behdad Esfahbod056c7ec2009-05-18 19:47:52 -0400792 ValueFormat valueFormat2; /* ValueRecord definition--for the
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400793 * second glyph of the pair--may be
794 * zero (0) */
795 OffsetTo<ClassDef>
796 classDef1; /* Offset to ClassDef table--from
797 * beginning of PairPos subtable--for
798 * the first glyph of the pair */
799 OffsetTo<ClassDef>
800 classDef2; /* Offset to ClassDef table--from
801 * beginning of PairPos subtable--for
802 * the second glyph of the pair */
803 USHORT class1Count; /* Number of classes in ClassDef1
804 * table--includes Class0 */
805 USHORT class2Count; /* Number of classes in ClassDef2
806 * table--includes Class0 */
807 ValueRecord values; /* Matrix of value pairs:
808 * class1-major, class2-minor,
809 * Each entry has value1 and value2 */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400810 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400811 DEFINE_SIZE_ARRAY (16, values);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400812};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400813
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400814struct PairPos
815{
Behdad Esfahbod2005fa52012-11-22 14:38:10 -0500816 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500817 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400818 {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500819 TRACE_DISPATCH (this);
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400820 switch (u.format) {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500821 case 1: return TRACE_RETURN (c->dispatch (u.format1));
822 case 2: return TRACE_RETURN (c->dispatch (u.format2));
Behdad Esfahbodf48ec0e2012-11-23 17:23:41 -0500823 default:return TRACE_RETURN (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400824 }
825 }
826
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400827 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500828 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200829 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400830 switch (u.format) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200831 case 1: return TRACE_RETURN (u.format1.sanitize (c));
832 case 2: return TRACE_RETURN (u.format2.sanitize (c));
833 default:return TRACE_RETURN (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400834 }
835 }
836
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400837 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400838 union {
839 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -0400840 PairPosFormat1 format1;
841 PairPosFormat2 format2;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400842 } u;
843};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400844
845
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400846struct EntryExitRecord
847{
Behdad Esfahbod569da922010-05-10 16:38:32 -0400848 friend struct CursivePosFormat1;
849
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400850 inline bool sanitize (hb_sanitize_context_t *c, void *base) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500851 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200852 return TRACE_RETURN (entryAnchor.sanitize (c, base) && exitAnchor.sanitize (c, base));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400853 }
854
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400855 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400856 OffsetTo<Anchor>
857 entryAnchor; /* Offset to EntryAnchor table--from
858 * beginning of CursivePos
859 * subtable--may be NULL */
860 OffsetTo<Anchor>
861 exitAnchor; /* Offset to ExitAnchor table--from
862 * beginning of CursivePos
863 * subtable--may be NULL */
Behdad Esfahbod569da922010-05-10 16:38:32 -0400864 public:
865 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400866};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400867
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400868struct CursivePosFormat1
869{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500870 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
871 {
872 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -0500873 (this+coverage).add_coverage (c->input);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -0500874 }
875
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400876 inline const Coverage &get_coverage (void) const
877 {
878 return this+coverage;
879 }
880
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400881 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -0400882 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500883 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200884 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbod0f7e6b22009-05-20 04:16:35 -0400885
886 /* We don't handle mark glyphs here. */
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200887 if (unlikely (_hb_glyph_info_is_mark (&buffer->cur()))) return TRACE_RETURN (false);
Behdad Esfahbod0f7e6b22009-05-20 04:16:35 -0400888
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200889 hb_apply_context_t::skipping_forward_iterator_t skippy_iter (c, buffer->idx, 1);
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200890 if (skippy_iter.has_no_chance ()) return TRACE_RETURN (false);
Behdad Esfahbod0f7e6b22009-05-20 04:16:35 -0400891
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200892 const EntryExitRecord &this_record = entryExitRecord[(this+coverage).get_coverage (buffer->cur().codepoint)];
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200893 if (!this_record.exitAnchor) return TRACE_RETURN (false);
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -0400894
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200895 if (!skippy_iter.next ()) return TRACE_RETURN (false);
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -0400896
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200897 const EntryExitRecord &next_record = entryExitRecord[(this+coverage).get_coverage (buffer->info[skippy_iter.idx].codepoint)];
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200898 if (!next_record.entryAnchor) return TRACE_RETURN (false);
Behdad Esfahbodaf5d02a2010-10-27 11:54:26 -0400899
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200900 unsigned int i = buffer->idx;
Behdad Esfahbod4ab97312012-01-16 22:05:08 -0500901 unsigned int j = skippy_iter.idx;
Behdad Esfahbodaf5d02a2010-10-27 11:54:26 -0400902
903 hb_position_t entry_x, entry_y, exit_x, exit_y;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200904 (this+this_record.exitAnchor).get_anchor (c->font, buffer->info[i].codepoint, &exit_x, &exit_y);
905 (this+next_record.entryAnchor).get_anchor (c->font, buffer->info[j].codepoint, &entry_x, &entry_y);
Behdad Esfahbodcc83ae12009-05-27 00:17:37 -0400906
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200907 hb_glyph_position_t *pos = buffer->pos;
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400908
909 hb_position_t d;
910 /* Main-direction adjustment */
911 switch (c->direction) {
912 case HB_DIRECTION_LTR:
913 pos[i].x_advance = exit_x + pos[i].x_offset;
914
915 d = entry_x + pos[j].x_offset;
916 pos[j].x_advance -= d;
917 pos[j].x_offset -= d;
918 break;
919 case HB_DIRECTION_RTL:
920 d = exit_x + pos[i].x_offset;
921 pos[i].x_advance -= d;
922 pos[i].x_offset -= d;
923
924 pos[j].x_advance = entry_x + pos[j].x_offset;
925 break;
926 case HB_DIRECTION_TTB:
927 pos[i].y_advance = exit_y + pos[i].y_offset;
928
929 d = entry_y + pos[j].y_offset;
930 pos[j].y_advance -= d;
931 pos[j].y_offset -= d;
932 break;
933 case HB_DIRECTION_BTT:
934 d = exit_y + pos[i].y_offset;
935 pos[i].y_advance -= d;
936 pos[i].y_offset -= d;
937
938 pos[j].y_advance = entry_y;
939 break;
940 case HB_DIRECTION_INVALID:
941 default:
942 break;
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -0400943 }
944
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400945 /* Cross-direction adjustment */
946 if (c->lookup_props & LookupFlag::RightToLeft) {
947 pos[i].cursive_chain() = j - i;
Behdad Esfahbod744970a2011-05-16 18:15:37 -0400948 if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction)))
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400949 pos[i].y_offset = entry_y - exit_y;
Behdad Esfahbod13528d02010-10-27 14:09:27 -0400950 else
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400951 pos[i].x_offset = entry_x - exit_x;
952 } else {
953 pos[j].cursive_chain() = i - j;
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[j].y_offset = exit_y - entry_y;
Behdad Esfahbod13528d02010-10-27 14:09:27 -0400956 else
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400957 pos[j].x_offset = exit_x - entry_x;
Behdad Esfahbodd18fd8e2009-05-19 23:25:41 -0400958 }
959
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200960 buffer->idx = j;
Behdad Esfahbodacea1832012-05-11 02:33:11 +0200961 return TRACE_RETURN (true);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400962 }
963
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400964 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500965 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200966 return TRACE_RETURN (coverage.sanitize (c, this) && entryExitRecord.sanitize (c, this));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400967 }
968
Behdad Esfahbodec8d2492012-07-24 15:40:37 -0400969 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400970 USHORT format; /* Format identifier--format = 1 */
971 OffsetTo<Coverage>
972 coverage; /* Offset to Coverage table--from
973 * beginning of subtable */
974 ArrayOf<EntryExitRecord>
975 entryExitRecord; /* Array of EntryExit records--in
976 * Coverage Index order */
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400977 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400978 DEFINE_SIZE_ARRAY (6, entryExitRecord);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400979};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -0400980
Behdad Esfahbod4c44d832009-05-19 23:42:30 -0400981struct CursivePos
982{
Behdad Esfahbod2005fa52012-11-22 14:38:10 -0500983 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500984 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400985 {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500986 TRACE_DISPATCH (this);
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400987 switch (u.format) {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500988 case 1: return TRACE_RETURN (c->dispatch (u.format1));
Behdad Esfahbodf48ec0e2012-11-23 17:23:41 -0500989 default:return TRACE_RETURN (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -0400990 }
991 }
992
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400993 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500994 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200995 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400996 switch (u.format) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200997 case 1: return TRACE_RETURN (u.format1.sanitize (c));
998 default:return TRACE_RETURN (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400999 }
1000 }
1001
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001002 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001003 union {
1004 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001005 CursivePosFormat1 format1;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001006 } u;
1007};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001008
1009
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -04001010typedef AnchorMatrix BaseArray; /* base-major--
1011 * in order of BaseCoverage Index--,
1012 * mark-minor--
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001013 * ordered by class--zero-based. */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001014
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001015struct MarkBasePosFormat1
1016{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001017 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1018 {
1019 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001020 (this+markCoverage).add_coverage (c->input);
1021 (this+baseCoverage).add_coverage (c->input);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001022 }
1023
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001024 inline const Coverage &get_coverage (void) const
1025 {
1026 return this+markCoverage;
1027 }
1028
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001029 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001030 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001031 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001032 hb_buffer_t *buffer = c->buffer;
1033 unsigned int mark_index = (this+markCoverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001034 if (likely (mark_index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbod357ccde2009-05-21 06:32:01 -04001035
1036 /* now we search backwards for a non-mark glyph */
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001037 hb_apply_context_t::skipping_backward_iterator_t skippy_iter (c, buffer->idx, 1);
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -05001038 skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
Behdad Esfahbod7b84c532012-06-08 22:04:23 -04001039 do {
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -05001040 if (!skippy_iter.prev ()) return TRACE_RETURN (false);
Behdad Esfahbod7b84c532012-06-08 22:04:23 -04001041 /* We only want to attach to the first of a MultipleSubst sequence. Reject others. */
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001042 if (0 == _hb_glyph_info_get_lig_comp (&buffer->info[skippy_iter.idx])) break;
Behdad Esfahbod7b84c532012-06-08 22:04:23 -04001043 skippy_iter.reject ();
1044 } while (1);
Behdad Esfahbod0532ed12009-08-12 15:40:04 -04001045
Behdad Esfahbod101303d2013-10-18 00:42:39 +02001046 /* Checking that matched glyph is actually a base glyph by GDEF is too strong; disabled */
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001047 if (!_hb_glyph_info_is_base_glyph (&buffer->info[skippy_iter.idx])) { /*return TRACE_RETURN (false);*/ }
Behdad Esfahbod357ccde2009-05-21 06:32:01 -04001048
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001049 unsigned int base_index = (this+baseCoverage).get_coverage (buffer->info[skippy_iter.idx].codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001050 if (base_index == NOT_COVERED) return TRACE_RETURN (false);
Behdad Esfahbod357ccde2009-05-21 06:32:01 -04001051
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001052 return TRACE_RETURN ((this+markArray).apply (c, mark_index, base_index, this+baseArray, classCount, skippy_iter.idx));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001053 }
1054
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001055 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001056 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001057 return TRACE_RETURN (c->check_struct (this) && markCoverage.sanitize (c, this) && baseCoverage.sanitize (c, this) &&
1058 markArray.sanitize (c, this) && baseArray.sanitize (c, this, (unsigned int) classCount));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001059 }
1060
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001061 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001062 USHORT format; /* Format identifier--format = 1 */
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001063 OffsetTo<Coverage>
1064 markCoverage; /* Offset to MarkCoverage table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001065 * beginning of MarkBasePos subtable */
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001066 OffsetTo<Coverage>
1067 baseCoverage; /* Offset to BaseCoverage table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001068 * beginning of MarkBasePos subtable */
1069 USHORT classCount; /* Number of classes defined for marks */
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001070 OffsetTo<MarkArray>
1071 markArray; /* Offset to MarkArray table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001072 * beginning of MarkBasePos subtable */
Behdad Esfahbodfb3b5cc2009-05-21 04:47:05 -04001073 OffsetTo<BaseArray>
1074 baseArray; /* Offset to BaseArray table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001075 * beginning of MarkBasePos subtable */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001076 public:
1077 DEFINE_SIZE_STATIC (12);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001078};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001079
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001080struct MarkBasePos
1081{
Behdad Esfahbod2005fa52012-11-22 14:38:10 -05001082 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001083 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001084 {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001085 TRACE_DISPATCH (this);
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001086 switch (u.format) {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001087 case 1: return TRACE_RETURN (c->dispatch (u.format1));
Behdad Esfahbodf48ec0e2012-11-23 17:23:41 -05001088 default:return TRACE_RETURN (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001089 }
1090 }
1091
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001092 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001093 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001094 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001095 switch (u.format) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001096 case 1: return TRACE_RETURN (u.format1.sanitize (c));
1097 default:return TRACE_RETURN (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001098 }
1099 }
1100
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001101 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001102 union {
1103 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001104 MarkBasePosFormat1 format1;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001105 } u;
1106};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001107
1108
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -04001109typedef AnchorMatrix LigatureAttach; /* component-major--
1110 * in order of writing direction--,
1111 * mark-minor--
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001112 * ordered by class--zero-based. */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001113
Behdad Esfahbod3564ee52009-08-14 18:32:56 -04001114typedef OffsetListOf<LigatureAttach> LigatureArray;
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001115 /* Array of LigatureAttach
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001116 * tables ordered by
1117 * LigatureCoverage Index */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001118
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001119struct MarkLigPosFormat1
1120{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001121 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1122 {
1123 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001124 (this+markCoverage).add_coverage (c->input);
1125 (this+ligatureCoverage).add_coverage (c->input);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001126 }
1127
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001128 inline const Coverage &get_coverage (void) const
1129 {
1130 return this+markCoverage;
1131 }
1132
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001133 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001134 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001135 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001136 hb_buffer_t *buffer = c->buffer;
1137 unsigned int mark_index = (this+markCoverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001138 if (likely (mark_index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001139
1140 /* now we search backwards for a non-mark glyph */
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001141 hb_apply_context_t::skipping_backward_iterator_t skippy_iter (c, buffer->idx, 1);
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -05001142 skippy_iter.set_lookup_props (LookupFlag::IgnoreMarks);
1143 if (!skippy_iter.prev ()) return TRACE_RETURN (false);
Behdad Esfahbod0532ed12009-08-12 15:40:04 -04001144
Behdad Esfahbod101303d2013-10-18 00:42:39 +02001145 /* Checking that matched glyph is actually a ligature by GDEF is too strong; disabled */
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001146 if (!_hb_glyph_info_is_ligature (&buffer->info[skippy_iter.idx])) { /*return TRACE_RETURN (false);*/ }
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001147
Behdad Esfahbod4ab97312012-01-16 22:05:08 -05001148 unsigned int j = skippy_iter.idx;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001149 unsigned int lig_index = (this+ligatureCoverage).get_coverage (buffer->info[j].codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001150 if (lig_index == NOT_COVERED) return TRACE_RETURN (false);
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001151
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001152 const LigatureArray& lig_array = this+ligatureArray;
Behdad Esfahbod3564ee52009-08-14 18:32:56 -04001153 const LigatureAttach& lig_attach = lig_array[lig_index];
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -04001154
1155 /* Find component to attach to */
Behdad Esfahbodb2b18ef2009-08-14 19:37:18 -04001156 unsigned int comp_count = lig_attach.rows;
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001157 if (unlikely (!comp_count)) return TRACE_RETURN (false);
1158
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001159 /* We must now check whether the ligature ID of the current mark glyph
1160 * is identical to the ligature ID of the found ligature. If yes, we
1161 * can directly use the component index. If not, we attach the mark
1162 * glyph to the last component of the ligature. */
Behdad Esfahbod0aef4252012-07-30 00:55:15 -04001163 unsigned int comp_index;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001164 unsigned int lig_id = _hb_glyph_info_get_lig_id (&buffer->info[j]);
1165 unsigned int mark_id = _hb_glyph_info_get_lig_id (&buffer->cur());
1166 unsigned int mark_comp = _hb_glyph_info_get_lig_comp (&buffer->cur());
Behdad Esfahbod0aef4252012-07-30 00:55:15 -04001167 if (lig_id && lig_id == mark_id && mark_comp > 0)
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001168 comp_index = MIN (comp_count, _hb_glyph_info_get_lig_comp (&buffer->cur())) - 1;
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001169 else
Behdad Esfahbodb2b18ef2009-08-14 19:37:18 -04001170 comp_index = comp_count - 1;
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001171
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001172 return TRACE_RETURN ((this+markArray).apply (c, mark_index, comp_index, lig_attach, classCount, j));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001173 }
1174
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001175 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001176 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001177 return TRACE_RETURN (c->check_struct (this) && markCoverage.sanitize (c, this) && ligatureCoverage.sanitize (c, this) &&
1178 markArray.sanitize (c, this) && ligatureArray.sanitize (c, this, (unsigned int) classCount));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001179 }
1180
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001181 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001182 USHORT format; /* Format identifier--format = 1 */
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001183 OffsetTo<Coverage>
1184 markCoverage; /* Offset to Mark Coverage table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001185 * beginning of MarkLigPos subtable */
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001186 OffsetTo<Coverage>
1187 ligatureCoverage; /* Offset to Ligature Coverage
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001188 * table--from beginning of MarkLigPos
1189 * subtable */
1190 USHORT classCount; /* Number of defined mark classes */
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001191 OffsetTo<MarkArray>
1192 markArray; /* Offset to MarkArray table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001193 * beginning of MarkLigPos subtable */
Behdad Esfahbod9b006bc2009-05-22 18:29:45 -04001194 OffsetTo<LigatureArray>
1195 ligatureArray; /* Offset to LigatureArray table--from
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001196 * beginning of MarkLigPos subtable */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001197 public:
1198 DEFINE_SIZE_STATIC (12);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001199};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001200
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001201struct MarkLigPos
1202{
Behdad Esfahbod2005fa52012-11-22 14:38:10 -05001203 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001204 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001205 {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001206 TRACE_DISPATCH (this);
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001207 switch (u.format) {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001208 case 1: return TRACE_RETURN (c->dispatch (u.format1));
Behdad Esfahbodf48ec0e2012-11-23 17:23:41 -05001209 default:return TRACE_RETURN (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001210 }
1211 }
1212
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001213 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001214 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001215 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001216 switch (u.format) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001217 case 1: return TRACE_RETURN (u.format1.sanitize (c));
1218 default:return TRACE_RETURN (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001219 }
1220 }
1221
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001222 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001223 union {
1224 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001225 MarkLigPosFormat1 format1;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001226 } u;
1227};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001228
1229
Behdad Esfahbodcb71a2f2009-08-14 18:14:03 -04001230typedef AnchorMatrix Mark2Array; /* mark2-major--
1231 * in order of Mark2Coverage Index--,
1232 * mark1-minor--
1233 * ordered by class--zero-based. */
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001234
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001235struct MarkMarkPosFormat1
1236{
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001237 inline void collect_glyphs (hb_collect_glyphs_context_t *c) const
1238 {
1239 TRACE_COLLECT_GLYPHS (this);
Behdad Esfahbod83035932012-12-04 17:08:41 -05001240 (this+mark1Coverage).add_coverage (c->input);
1241 (this+mark2Coverage).add_coverage (c->input);
Behdad Esfahbodcdd756b2012-11-24 01:38:41 -05001242 }
1243
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001244 inline const Coverage &get_coverage (void) const
1245 {
1246 return this+mark1Coverage;
1247 }
1248
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001249 inline bool apply (hb_apply_context_t *c) const
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001250 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001251 TRACE_APPLY (this);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001252 hb_buffer_t *buffer = c->buffer;
1253 unsigned int mark1_index = (this+mark1Coverage).get_coverage (buffer->cur().codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001254 if (likely (mark1_index == NOT_COVERED)) return TRACE_RETURN (false);
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001255
1256 /* now we search backwards for a suitable mark glyph until a non-mark glyph */
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001257 hb_apply_context_t::skipping_backward_iterator_t skippy_iter (c, buffer->idx, 1);
Behdad Esfahbodc074ebc2013-02-13 11:22:42 -05001258 skippy_iter.set_lookup_props (c->lookup_props & ~LookupFlag::IgnoreFlags);
1259 if (!skippy_iter.prev ()) return TRACE_RETURN (false);
Behdad Esfahbod0532ed12009-08-12 15:40:04 -04001260
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001261 if (!_hb_glyph_info_is_mark (&buffer->info[skippy_iter.idx])) { return TRACE_RETURN (false); }
Behdad Esfahbod80ea5bd2009-05-26 17:58:37 -04001262
Behdad Esfahbod4ab97312012-01-16 22:05:08 -05001263 unsigned int j = skippy_iter.idx;
1264
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001265 unsigned int id1 = _hb_glyph_info_get_lig_id (&buffer->cur());
1266 unsigned int id2 = _hb_glyph_info_get_lig_id (&buffer->info[j]);
1267 unsigned int comp1 = _hb_glyph_info_get_lig_comp (&buffer->cur());
1268 unsigned int comp2 = _hb_glyph_info_get_lig_comp (&buffer->info[j]);
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001269
Behdad Esfahbod5d874d52012-07-28 21:05:25 -04001270 if (likely (id1 == id2)) {
1271 if (id1 == 0) /* Marks belonging to the same base. */
1272 goto good;
1273 else if (comp1 == comp2) /* Marks belonging to the same ligature component. */
1274 goto good;
1275 } else {
1276 /* If ligature ids don't match, it may be the case that one of the marks
1277 * itself is a ligature. In which case match. */
1278 if ((id1 > 0 && !comp1) || (id2 > 0 && !comp2))
1279 goto good;
1280 }
1281
1282 /* Didn't match. */
1283 return TRACE_RETURN (false);
1284
1285 good:
Behdad Esfahbodac8cd512013-10-18 19:33:09 +02001286 unsigned int mark2_index = (this+mark2Coverage).get_coverage (buffer->info[j].codepoint);
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001287 if (mark2_index == NOT_COVERED) return TRACE_RETURN (false);
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001288
Behdad Esfahbodacea1832012-05-11 02:33:11 +02001289 return TRACE_RETURN ((this+mark1Array).apply (c, mark1_index, mark2_index, this+mark2Array, classCount, j));
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001290 }
1291
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001292 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001293 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001294 return TRACE_RETURN (c->check_struct (this) && mark1Coverage.sanitize (c, this) &&
1295 mark2Coverage.sanitize (c, this) && mark1Array.sanitize (c, this)
1296 && mark2Array.sanitize (c, this, (unsigned int) classCount));
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001297 }
1298
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001299 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001300 USHORT format; /* Format identifier--format = 1 */
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001301 OffsetTo<Coverage>
1302 mark1Coverage; /* Offset to Combining Mark1 Coverage
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001303 * table--from beginning of MarkMarkPos
1304 * subtable */
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001305 OffsetTo<Coverage>
1306 mark2Coverage; /* Offset to Combining Mark2 Coverage
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001307 * table--from beginning of MarkMarkPos
1308 * subtable */
Behdad Esfahbodfe550f42009-05-21 08:27:07 -04001309 USHORT classCount; /* Number of defined mark classes */
1310 OffsetTo<MarkArray>
1311 mark1Array; /* Offset to Mark1Array table--from
1312 * beginning of MarkMarkPos subtable */
1313 OffsetTo<Mark2Array>
1314 mark2Array; /* Offset to Mark2Array table--from
1315 * beginning of MarkMarkPos subtable */
Behdad Esfahbodb3651232010-05-10 16:57:29 -04001316 public:
1317 DEFINE_SIZE_STATIC (12);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001318};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001319
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001320struct MarkMarkPos
1321{
Behdad Esfahbod2005fa52012-11-22 14:38:10 -05001322 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001323 inline typename context_t::return_t dispatch (context_t *c) const
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001324 {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001325 TRACE_DISPATCH (this);
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001326 switch (u.format) {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001327 case 1: return TRACE_RETURN (c->dispatch (u.format1));
Behdad Esfahbodf48ec0e2012-11-23 17:23:41 -05001328 default:return TRACE_RETURN (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001329 }
1330 }
1331
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001332 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001333 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001334 if (!u.format.sanitize (c)) return TRACE_RETURN (false);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001335 switch (u.format) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001336 case 1: return TRACE_RETURN (u.format1.sanitize (c));
1337 default:return TRACE_RETURN (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001338 }
1339 }
1340
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001341 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001342 union {
1343 USHORT format; /* Format identifier */
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001344 MarkMarkPosFormat1 format1;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001345 } u;
1346};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001347
1348
Behdad Esfahbod08f1eed2012-11-23 16:51:43 -05001349struct ContextPos : Context {};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001350
Behdad Esfahbod08f1eed2012-11-23 16:51:43 -05001351struct ChainContextPos : ChainContext {};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001352
Behdad Esfahbod653eeb22012-11-23 16:57:36 -05001353struct ExtensionPos : Extension<ExtensionPos>
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001354{
Behdad Esfahbod7dddd4e2012-11-23 17:04:55 -05001355 typedef struct PosLookupSubTable LookupSubTable;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001356};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001357
1358
Behdad Esfahbodd468f9a2009-05-21 22:31:33 -04001359
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001360/*
1361 * PosLookup
1362 */
1363
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001364
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001365struct PosLookupSubTable
1366{
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001367 friend struct PosLookup;
1368
Behdad Esfahbodc6035cf2012-04-12 13:23:59 -04001369 enum Type {
Behdad Esfahbodff05d252009-05-20 03:53:00 -04001370 Single = 1,
1371 Pair = 2,
1372 Cursive = 3,
1373 MarkBase = 4,
1374 MarkLig = 5,
1375 MarkMark = 6,
1376 Context = 7,
1377 ChainContext = 8,
Behdad Esfahbod8f034d52009-08-18 16:41:59 -04001378 Extension = 9
Behdad Esfahbodff05d252009-05-20 03:53:00 -04001379 };
1380
Behdad Esfahbod2005fa52012-11-22 14:38:10 -05001381 template <typename context_t>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001382 inline typename context_t::return_t dispatch (context_t *c, unsigned int lookup_type) const
Behdad Esfahbodfaf0f202012-06-09 03:02:36 -04001383 {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001384 TRACE_DISPATCH (this);
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001385 switch (lookup_type) {
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -05001386 case Single: return TRACE_RETURN (u.single.dispatch (c));
1387 case Pair: return TRACE_RETURN (u.pair.dispatch (c));
1388 case Cursive: return TRACE_RETURN (u.cursive.dispatch (c));
1389 case MarkBase: return TRACE_RETURN (u.markBase.dispatch (c));
1390 case MarkLig: return TRACE_RETURN (u.markLig.dispatch (c));
1391 case MarkMark: return TRACE_RETURN (u.markMark.dispatch (c));
1392 case Context: return TRACE_RETURN (u.context.dispatch (c));
1393 case ChainContext: return TRACE_RETURN (u.chainContext.dispatch (c));
1394 case Extension: return TRACE_RETURN (u.extension.dispatch (c));
Behdad Esfahbodf48ec0e2012-11-23 17:23:41 -05001395 default: return TRACE_RETURN (c->default_return_value ());
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001396 }
Behdad Esfahbodfaf0f202012-06-09 03:02:36 -04001397 }
1398
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -04001399 inline bool sanitize (hb_sanitize_context_t *c, unsigned int lookup_type) {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -05001400 TRACE_SANITIZE (this);
Behdad Esfahbod0b994292012-07-28 17:31:01 -04001401 if (!u.header.sub_format.sanitize (c))
Behdad Esfahbodfaf0f202012-06-09 03:02:36 -04001402 return TRACE_RETURN (false);
Behdad Esfahbodfe9bc072010-05-10 21:39:24 -04001403 switch (lookup_type) {
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001404 case Single: return TRACE_RETURN (u.single.sanitize (c));
1405 case Pair: return TRACE_RETURN (u.pair.sanitize (c));
1406 case Cursive: return TRACE_RETURN (u.cursive.sanitize (c));
1407 case MarkBase: return TRACE_RETURN (u.markBase.sanitize (c));
1408 case MarkLig: return TRACE_RETURN (u.markLig.sanitize (c));
1409 case MarkMark: return TRACE_RETURN (u.markMark.sanitize (c));
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001410 case Context: return TRACE_RETURN (u.context.sanitize (c));
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02001411 case ChainContext: return TRACE_RETURN (u.chainContext.sanitize (c));
1412 case Extension: return TRACE_RETURN (u.extension.sanitize (c));
1413 default: return TRACE_RETURN (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -04001414 }
1415 }
1416
Behdad Esfahbodec8d2492012-07-24 15:40:37 -04001417 protected:
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001418 union {
Behdad Esfahbod4e766ff2012-06-09 02:53:57 -04001419 struct {
Behdad Esfahbod2005fa52012-11-22 14:38:10 -05001420 USHORT sub_format;
Behdad Esfahbod4e766ff2012-06-09 02:53:57 -04001421 } header;
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001422 SinglePos single;
1423 PairPos pair;
1424 CursivePos cursive;
1425 MarkBasePos markBase;
1426 MarkLigPos markLig;
1427 MarkMarkPos markMark;
Behdad Esfahbode72b3602012-07-19 14:35:23 -04001428 ContextPos context;
Behdad Esfahboddacebca2010-05-10 19:45:41 -04001429 ChainContextPos chainContext;
1430 ExtensionPos extension;
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001431 } u;
Behdad Esfahboded074222010-05-10 18:08:46 -04001432 public:
Behdad Esfahbod4e766ff2012-06-09 02:53:57 -04001433 DEFINE_SIZE_UNION (2, header.sub_format);
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001434};
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001435
1436
Behdad Esfahbod4c44d832009-05-19 23:42:30 -04001437struct PosLookup : Lookup
1438{
Behdad Esfahbod6d08c7f2012-07-11 18:01:27 -04001439 inline const PosLookupSubTable& get_subtable (unsigned int i) const
1440 { return this+CastR<OffsetArrayOf<PosLookupSubTable> > (subTable)[i]; }
Behdad Esfahbod5e5eb052009-05-18 17:09:33 -04001441
Behdad Esfahbode015b8f2013-05-03 17:34:29 -04001442 inline bool is_reverse (void) const
1443 {
1444 return false;
1445 }
1446
Behdad Esfahbod2e0c44f2013-04-24 16:42:05 -04001447 inline hb_is_inplace_context_t::return_t is_inplace (hb_is_inplace_context_t *c) const
1448 {
1449 TRACE_IS_INPLACE (this);
1450 return TRACE_RETURN (true);
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 Esfahbod9c5a9ee2013-03-09 01:55:04 -05001611inline 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 Esfahbodec35a722012-11-22 16:05:59 -05001618inline 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 */