blob: cf5ae8799a156d72fbb2b815d829a56201914218 [file] [log] [blame]
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +01001/*
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -04002 * Copyright © 2009,2010 Red Hat, Inc.
Behdad Esfahbod07d68282012-08-10 03:28:50 -04003 * Copyright © 2010,2011,2012 Google, Inc.
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +01004 *
Behdad Esfahbodc755cb32010-04-22 00:11:43 -04005 * This is part of HarfBuzz, a text shaping library.
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +01006 *
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 Esfahbod476c9422010-10-07 17:47:33 -040026 * Google Author(s): Behdad Esfahbod
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +010027 */
28
Behdad Esfahbod027857d2012-07-26 17:34:25 -040029#define HB_SHAPER ot
30#define hb_ot_shaper_face_data_t hb_ot_layout_t
Behdad Esfahbode82061e2012-07-27 02:29:32 -040031#define hb_ot_shaper_shape_plan_data_t hb_ot_shape_plan_t
Behdad Esfahbod027857d2012-07-26 17:34:25 -040032#include "hb-shaper-impl-private.hh"
33
Behdad Esfahbod4924aff2010-10-08 19:18:16 -040034#include "hb-ot-shape-private.hh"
Behdad Esfahbod16c6a272012-08-02 09:38:28 -040035#include "hb-ot-shape-complex-private.hh"
Behdad Esfahbod9c929ab2012-08-08 14:33:37 -040036#include "hb-ot-shape-fallback-private.hh"
Behdad Esfahbodaffaf8a2012-08-07 22:41:38 -040037#include "hb-ot-shape-normalize-private.hh"
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +010038
Behdad Esfahbodb70021f2012-07-23 20:18:17 -040039#include "hb-ot-layout-private.hh"
Behdad Esfahbod878a2532014-07-16 13:21:26 -040040#include "hb-unicode-private.hh"
Behdad Esfahbod1827dc22012-04-24 16:56:37 -040041#include "hb-set-private.hh"
Behdad Esfahbod72657e42011-05-02 20:46:32 -040042
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -040043
Behdad Esfahboda02d8642012-08-08 18:04:29 -040044static hb_tag_t common_features[] = {
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +010045 HB_TAG('c','c','m','p'),
Behdad Esfahbod89a2bc92011-05-31 15:18:13 -040046 HB_TAG('l','o','c','l'),
Behdad Esfahbod89a2bc92011-05-31 15:18:13 -040047 HB_TAG('m','a','r','k'),
48 HB_TAG('m','k','m','k'),
49 HB_TAG('r','l','i','g'),
50};
51
Behdad Esfahbodd1deaa22012-05-09 15:04:13 +020052
Behdad Esfahboda02d8642012-08-08 18:04:29 -040053static hb_tag_t horizontal_features[] = {
Behdad Esfahbod89a2bc92011-05-31 15:18:13 -040054 HB_TAG('c','a','l','t'),
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +010055 HB_TAG('c','l','i','g'),
Behdad Esfahbodbd098732010-05-21 18:06:35 +010056 HB_TAG('c','u','r','s'),
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +010057 HB_TAG('k','e','r','n'),
Behdad Esfahbod2053f362014-08-02 16:31:16 -040058 HB_TAG('l','i','g','a'),
Behdad Esfahbodf4e48ad2012-08-07 21:12:49 -040059 HB_TAG('r','c','l','t'),
Behdad Esfahbod89a2bc92011-05-31 15:18:13 -040060};
61
Behdad Esfahboda02d8642012-08-08 18:04:29 -040062static hb_tag_t vertical_features[] = {
Behdad Esfahbod89a2bc92011-05-31 15:18:13 -040063 HB_TAG('v','e','r','t'),
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +010064};
65
Behdad Esfahbodb9f199c2012-04-14 20:23:58 -040066
67
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +010068static void
Behdad Esfahbod90645fb2011-05-27 18:13:31 -040069hb_ot_shape_collect_features (hb_ot_shape_planner_t *planner,
Behdad Esfahbod49baa1f2010-10-12 16:50:36 -040070 const hb_segment_properties_t *props,
Behdad Esfahbod90645fb2011-05-27 18:13:31 -040071 const hb_feature_t *user_features,
72 unsigned int num_user_features)
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +010073{
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -040074 hb_ot_map_builder_t *map = &planner->map;
75
Behdad Esfahbod895fb5d2010-10-12 16:00:21 -040076 switch (props->direction) {
Behdad Esfahbod074ea782010-05-21 17:53:10 +010077 case HB_DIRECTION_LTR:
Behdad Esfahbode7ffcfa2013-02-14 11:05:56 -050078 map->add_global_bool_feature (HB_TAG ('l','t','r','a'));
79 map->add_global_bool_feature (HB_TAG ('l','t','r','m'));
Behdad Esfahbod074ea782010-05-21 17:53:10 +010080 break;
81 case HB_DIRECTION_RTL:
Behdad Esfahbode7ffcfa2013-02-14 11:05:56 -050082 map->add_global_bool_feature (HB_TAG ('r','t','l','a'));
Behdad Esfahbodec544862013-02-14 11:25:10 -050083 map->add_feature (HB_TAG ('r','t','l','m'), 1, F_NONE);
Behdad Esfahbod0e235d02010-05-28 20:21:47 -040084 break;
85 case HB_DIRECTION_TTB:
86 case HB_DIRECTION_BTT:
Behdad Esfahbod3286fc02011-03-16 14:53:32 -030087 case HB_DIRECTION_INVALID:
Behdad Esfahbod0e235d02010-05-28 20:21:47 -040088 default:
89 break;
90 }
91
Behdad Esfahbod3aeee512013-12-22 16:17:54 -050092 map->add_feature (HB_TAG ('f','r','a','c'), 1, F_NONE);
93 map->add_feature (HB_TAG ('n','u','m','r'), 1, F_NONE);
94 map->add_feature (HB_TAG ('d','n','o','m'), 1, F_NONE);
95
Behdad Esfahbod693918e2012-07-30 21:08:51 -040096 if (planner->shaper->collect_features)
Behdad Esfahbod16c6a272012-08-02 09:38:28 -040097 planner->shaper->collect_features (planner);
Behdad Esfahbode04685e2010-05-28 20:37:06 -040098
Behdad Esfahbodee9c3a12013-02-15 06:22:26 -050099 for (unsigned int i = 0; i < ARRAY_LENGTH (common_features); i++)
100 map->add_global_bool_feature (common_features[i]);
Behdad Esfahbod89a2bc92011-05-31 15:18:13 -0400101
102 if (HB_DIRECTION_IS_HORIZONTAL (props->direction))
Behdad Esfahbodee9c3a12013-02-15 06:22:26 -0500103 for (unsigned int i = 0; i < ARRAY_LENGTH (horizontal_features); i++)
Behdad Esfahbod038c98f2013-02-15 07:41:07 -0500104 map->add_feature (horizontal_features[i], 1, F_GLOBAL |
105 (horizontal_features[i] == HB_TAG('k','e','r','n') ?
106 F_HAS_FALLBACK : F_NONE));
Behdad Esfahbod89a2bc92011-05-31 15:18:13 -0400107 else
Behdad Esfahbodee9c3a12013-02-15 06:22:26 -0500108 for (unsigned int i = 0; i < ARRAY_LENGTH (vertical_features); i++)
Behdad Esfahbod038c98f2013-02-15 07:41:07 -0500109 map->add_feature (vertical_features[i], 1, F_GLOBAL |
110 (vertical_features[i] == HB_TAG('v','k','r','n') ?
111 F_HAS_FALLBACK : F_NONE));
Behdad Esfahbod89a2bc92011-05-31 15:18:13 -0400112
Behdad Esfahbod693918e2012-07-30 21:08:51 -0400113 if (planner->shaper->override_features)
Behdad Esfahbod16c6a272012-08-02 09:38:28 -0400114 planner->shaper->override_features (planner);
Behdad Esfahbodd96838e2012-07-16 20:26:57 -0400115
Behdad Esfahbod895fb5d2010-10-12 16:00:21 -0400116 for (unsigned int i = 0; i < num_user_features; i++) {
117 const hb_feature_t *feature = &user_features[i];
Behdad Esfahbodec544862013-02-14 11:25:10 -0500118 map->add_feature (feature->tag, feature->value,
119 (feature->start == 0 && feature->end == (unsigned int) -1) ?
120 F_GLOBAL : F_NONE);
Behdad Esfahbode04685e2010-05-28 20:37:06 -0400121 }
Behdad Esfahbode89b7d22010-10-08 12:29:59 -0400122}
Behdad Esfahbod0e235d02010-05-28 20:21:47 -0400123
124
Behdad Esfahbode82061e2012-07-27 02:29:32 -0400125/*
126 * shaper face data
127 */
128
129hb_ot_shaper_face_data_t *
130_hb_ot_shaper_face_data_create (hb_face_t *face)
131{
132 return _hb_ot_layout_create (face);
133}
134
135void
136_hb_ot_shaper_face_data_destroy (hb_ot_shaper_face_data_t *data)
137{
138 _hb_ot_layout_destroy (data);
139}
140
141
142/*
143 * shaper font data
144 */
145
146struct hb_ot_shaper_font_data_t {};
147
148hb_ot_shaper_font_data_t *
149_hb_ot_shaper_font_data_create (hb_font_t *font)
150{
151 return (hb_ot_shaper_font_data_t *) HB_SHAPER_DATA_SUCCEEDED;
152}
153
154void
155_hb_ot_shaper_font_data_destroy (hb_ot_shaper_font_data_t *data)
156{
157}
158
159
160/*
161 * shaper shape_plan data
162 */
163
164hb_ot_shaper_shape_plan_data_t *
165_hb_ot_shaper_shape_plan_data_create (hb_shape_plan_t *shape_plan,
166 const hb_feature_t *user_features,
167 unsigned int num_user_features)
168{
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400169 hb_ot_shape_plan_t *plan = (hb_ot_shape_plan_t *) calloc (1, sizeof (hb_ot_shape_plan_t));
170 if (unlikely (!plan))
Behdad Esfahbode82061e2012-07-27 02:29:32 -0400171 return NULL;
172
Behdad Esfahbod5393e3a2012-08-02 09:24:35 -0400173 hb_ot_shape_planner_t planner (shape_plan);
Behdad Esfahbode82061e2012-07-27 02:29:32 -0400174
Behdad Esfahbod9b37b4c2012-11-12 18:23:38 -0800175 planner.shaper = hb_ot_shape_complex_categorize (&planner);
Behdad Esfahbode82061e2012-07-27 02:29:32 -0400176
177 hb_ot_shape_collect_features (&planner, &shape_plan->props, user_features, num_user_features);
178
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400179 planner.compile (*plan);
Behdad Esfahbode82061e2012-07-27 02:29:32 -0400180
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400181 if (plan->shaper->data_create) {
182 plan->data = plan->shaper->data_create (plan);
183 if (unlikely (!plan->data))
184 return NULL;
185 }
186
187 return plan;
Behdad Esfahbode82061e2012-07-27 02:29:32 -0400188}
189
190void
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400191_hb_ot_shaper_shape_plan_data_destroy (hb_ot_shaper_shape_plan_data_t *plan)
Behdad Esfahbode82061e2012-07-27 02:29:32 -0400192{
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400193 if (plan->shaper->data_destroy)
194 plan->shaper->data_destroy (const_cast<void *> (plan->data));
Behdad Esfahbod4ba647e2012-07-30 09:53:06 -0400195
Behdad Esfahboda8c6da92012-08-02 10:46:34 -0400196 plan->finish ();
197
198 free (plan);
Behdad Esfahbode82061e2012-07-27 02:29:32 -0400199}
200
201
202/*
203 * shaper
204 */
205
Behdad Esfahbodb9f199c2012-04-14 20:23:58 -0400206struct hb_ot_shape_context_t
207{
Behdad Esfahbodb9f199c2012-04-14 20:23:58 -0400208 hb_ot_shape_plan_t *plan;
209 hb_font_t *font;
210 hb_face_t *face;
211 hb_buffer_t *buffer;
212 const hb_feature_t *user_features;
213 unsigned int num_user_features;
214
215 /* Transient stuff */
216 hb_direction_t target_direction;
Behdad Esfahbodb9f199c2012-04-14 20:23:58 -0400217};
218
Behdad Esfahbod2014b8d2009-12-20 20:58:26 +0100219
220
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100221/* Main shaper */
222
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400223
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100224/* Prepare */
225
Behdad Esfahbodb9f199c2012-04-14 20:23:58 -0400226static void
227hb_set_unicode_props (hb_buffer_t *buffer)
Behdad Esfahbod3a852ae2010-11-03 16:37:24 -0400228{
Behdad Esfahbodaa726412011-07-21 11:34:59 -0400229 unsigned int count = buffer->len;
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400230 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbod0e336142012-04-12 10:06:52 -0400231 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400232 _hb_glyph_info_set_unicode_props (&info[i], buffer->unicode);
Behdad Esfahbod3a852ae2010-11-03 16:37:24 -0400233}
234
235static void
Behdad Esfahbod6912e472012-09-01 20:38:45 -0400236hb_insert_dotted_circle (hb_buffer_t *buffer, hb_font_t *font)
237{
Behdad Esfahbod0c7df222012-11-13 14:42:35 -0800238 if (!(buffer->flags & HB_BUFFER_FLAG_BOT) ||
Behdad Esfahbod763e5462014-08-02 16:17:44 -0400239 buffer->context_len[0] ||
Behdad Esfahbodf2eb3fa2012-09-25 21:35:35 -0400240 _hb_glyph_info_get_general_category (&buffer->info[0]) !=
Behdad Esfahbod6912e472012-09-01 20:38:45 -0400241 HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
242 return;
243
Behdad Esfahbod76271002014-07-11 14:54:42 -0400244 if (!font->has_glyph (0x25CCu))
Behdad Esfahbod6912e472012-09-01 20:38:45 -0400245 return;
246
Behdad Esfahbod595d2b92014-07-26 18:44:15 -0400247 hb_glyph_info_t dottedcircle = {0};
Behdad Esfahbod76271002014-07-11 14:54:42 -0400248 dottedcircle.codepoint = 0x25CCu;
Behdad Esfahbod6912e472012-09-01 20:38:45 -0400249 _hb_glyph_info_set_unicode_props (&dottedcircle, buffer->unicode);
250
251 buffer->clear_output ();
252
253 buffer->idx = 0;
254 hb_glyph_info_t info = dottedcircle;
255 info.cluster = buffer->cur().cluster;
256 info.mask = buffer->cur().mask;
257 buffer->output_info (info);
258 while (buffer->idx < buffer->len)
259 buffer->next_glyph ();
260
261 buffer->swap_buffers ();
262}
263
264static void
Behdad Esfahbodaa726412011-07-21 11:34:59 -0400265hb_form_clusters (hb_buffer_t *buffer)
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100266{
Behdad Esfahbod376d5872015-07-22 16:51:12 +0100267 if (buffer->cluster_level != HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
268 return;
269
Behdad Esfahboda60e2cf2015-07-22 15:49:08 +0100270 /* Loop duplicated in hb_ensure_native_direction(). */
Behdad Esfahbod701112d2015-07-22 15:42:20 +0100271 unsigned int base = 0;
Behdad Esfahbodaa726412011-07-21 11:34:59 -0400272 unsigned int count = buffer->len;
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400273 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100274 for (unsigned int i = 1; i < count; i++)
Behdad Esfahbod701112d2015-07-22 15:42:20 +0100275 {
276 if (likely (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i]))))
277 {
278 buffer->merge_clusters (base, i);
279 base = i;
280 }
281 }
282 buffer->merge_clusters (base, count);
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100283}
284
Behdad Esfahbod967240d2010-10-05 23:00:05 -0400285static void
Behdad Esfahbodaa726412011-07-21 11:34:59 -0400286hb_ensure_native_direction (hb_buffer_t *buffer)
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100287{
Behdad Esfahbodaa726412011-07-21 11:34:59 -0400288 hb_direction_t direction = buffer->props.direction;
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100289
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400290 /* TODO vertical:
291 * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
292 * Ogham fonts are supposed to be implemented BTT or not. Need to research that
293 * first. */
Behdad Esfahbodaa726412011-07-21 11:34:59 -0400294 if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) ||
Behdad Esfahbod7403e052011-05-24 21:04:15 -0400295 (HB_DIRECTION_IS_VERTICAL (direction) && direction != HB_DIRECTION_TTB))
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100296 {
Behdad Esfahboda60e2cf2015-07-22 15:49:08 +0100297 /* Same loop as hb_form_clusters().
298 * Since form_clusters() merged clusters already, we don't merge. */
299 unsigned int base = 0;
300 unsigned int count = buffer->len;
301 hb_glyph_info_t *info = buffer->info;
302 for (unsigned int i = 1; i < count; i++)
303 {
304 if (likely (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&info[i]))))
305 {
306 buffer->reverse_range (base, i);
Behdad Esfahbod376d5872015-07-22 16:51:12 +0100307 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
308 buffer->merge_clusters (base, i);
Behdad Esfahboda60e2cf2015-07-22 15:49:08 +0100309 base = i;
310 }
311 }
312 buffer->reverse_range (base, count);
Behdad Esfahbod376d5872015-07-22 16:51:12 +0100313 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
314 buffer->merge_clusters (base, count);
Behdad Esfahboda60e2cf2015-07-22 15:49:08 +0100315
316 buffer->reverse ();
317
Behdad Esfahbodaa726412011-07-21 11:34:59 -0400318 buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100319 }
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100320}
321
322
323/* Substitute */
324
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400325static inline void
326hb_ot_mirror_chars (hb_ot_shape_context_t *c)
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100327{
Behdad Esfahbodbbbbe802010-12-07 16:22:02 -0500328 if (HB_DIRECTION_IS_FORWARD (c->target_direction))
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100329 return;
330
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200331 hb_buffer_t *buffer = c->buffer;
332 hb_unicode_funcs_t *unicode = buffer->unicode;
Behdad Esfahbod5497a8a2013-12-22 20:48:53 -0500333 hb_mask_t rtlm_mask = c->plan->rtlm_mask;
Behdad Esfahbod4e4ef242010-07-23 17:22:11 -0400334
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200335 unsigned int count = buffer->len;
336 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100337 for (unsigned int i = 0; i < count; i++) {
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200338 hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint);
Behdad Esfahbod7b8b63a2015-07-22 15:24:26 +0100339 if (likely (codepoint == info[i].codepoint || !c->font->has_glyph (codepoint)))
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200340 info[i].mask |= rtlm_mask;
Behdad Esfahbod1094a292010-05-21 17:58:20 +0100341 else
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200342 info[i].codepoint = codepoint;
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100343 }
344}
345
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400346static inline void
Behdad Esfahbod3aeee512013-12-22 16:17:54 -0500347hb_ot_shape_setup_masks_fraction (hb_ot_shape_context_t *c)
348{
Behdad Esfahbod5497a8a2013-12-22 20:48:53 -0500349 if (!c->plan->has_frac)
350 return;
351
Behdad Esfahbod3aeee512013-12-22 16:17:54 -0500352 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbod3aeee512013-12-22 16:17:54 -0500353
354 /* TODO look in pre/post context text also. */
355 unsigned int count = buffer->len;
356 hb_glyph_info_t *info = buffer->info;
357 for (unsigned int i = 0; i < count; i++)
358 {
Behdad Esfahbod76271002014-07-11 14:54:42 -0400359 if (info[i].codepoint == 0x2044u) /* FRACTION SLASH */
Behdad Esfahbod3aeee512013-12-22 16:17:54 -0500360 {
Behdad Esfahbod3aeee512013-12-22 16:17:54 -0500361 unsigned int start = i, end = i + 1;
362 while (start &&
363 _hb_glyph_info_get_general_category (&info[start - 1]) ==
364 HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
365 start--;
366 while (end < count &&
367 _hb_glyph_info_get_general_category (&info[end]) ==
368 HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
369 end++;
370
Behdad Esfahboda7e8bbb2013-12-22 19:33:35 -0500371 for (unsigned int j = start; j < i; j++)
Behdad Esfahbod5497a8a2013-12-22 20:48:53 -0500372 info[j].mask |= c->plan->numr_mask | c->plan->frac_mask;
373 info[i].mask |= c->plan->frac_mask;
Behdad Esfahboda7e8bbb2013-12-22 19:33:35 -0500374 for (unsigned int j = i + 1; j < end; j++)
Behdad Esfahbod5497a8a2013-12-22 20:48:53 -0500375 info[j].mask |= c->plan->frac_mask | c->plan->dnom_mask;
Behdad Esfahbod3aeee512013-12-22 16:17:54 -0500376
377 i = end - 1;
378 }
379 }
380}
381
382static inline void
Behdad Esfahbod73932512013-12-21 00:18:18 -0500383hb_ot_shape_initialize_masks (hb_ot_shape_context_t *c)
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400384{
385 hb_ot_map_t *map = &c->plan->map;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200386 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400387
388 hb_mask_t global_mask = map->get_global_mask ();
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200389 buffer->reset_masks (global_mask);
Behdad Esfahbod73932512013-12-21 00:18:18 -0500390}
391
392static inline void
393hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
394{
395 hb_ot_map_t *map = &c->plan->map;
396 hb_buffer_t *buffer = c->buffer;
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400397
Behdad Esfahbod3aeee512013-12-22 16:17:54 -0500398 hb_ot_shape_setup_masks_fraction (c);
399
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400400 if (c->plan->shaper->setup_masks)
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200401 c->plan->shaper->setup_masks (c->plan, buffer, c->font);
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400402
403 for (unsigned int i = 0; i < c->num_user_features; i++)
404 {
405 const hb_feature_t *feature = &c->user_features[i];
406 if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
407 unsigned int shift;
408 hb_mask_t mask = map->get_mask (feature->tag, &shift);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200409 buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400410 }
411 }
412}
413
Behdad Esfahbodf0010df2015-07-22 17:36:23 +0100414
415static void
416hb_ot_hide_default_ignorables (hb_ot_shape_context_t *c)
417{
418 hb_buffer_t *buffer = c->buffer;
419
420 if (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES)
421 return;
422
423 unsigned int count = buffer->len;
424 hb_glyph_info_t *info = buffer->info;
425 hb_glyph_position_t *pos = buffer->pos;
426 unsigned int i = 0;
427 for (i = 0; i < count; i++)
428 {
Behdad Esfahbod4ba796b2015-07-22 17:41:31 +0100429 if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
Behdad Esfahbodf0010df2015-07-22 17:36:23 +0100430 break;
431 }
432
433 /* No default-ignorables found; return. */
434 if (i == count)
435 return;
436
437 hb_codepoint_t space;
438 if (c->font->get_glyph (' ', 0, &space))
439 {
440 /* Replace default-ignorables with a zero-advance space glyph. */
441 for (/*continue*/; i < count; i++)
442 {
Behdad Esfahbod4ba796b2015-07-22 17:41:31 +0100443 if (_hb_glyph_info_is_default_ignorable (&info[i]))
Behdad Esfahbodf0010df2015-07-22 17:36:23 +0100444 {
445 info[i].codepoint = space;
446 pos[i].x_advance = pos[i].y_advance = pos[i].x_offset = pos[i].y_offset = 0;
447 }
448 }
449 }
450 else
451 {
Behdad Esfahbod2dbd3d22015-07-22 18:28:39 +0100452 /* Merge clusters and delete default-ignorables.
453 * NOTE! We can't use out-buffer as we have positioning data. */
454 unsigned int j = i;
455 for (; i < count; i++)
Behdad Esfahbodf0010df2015-07-22 17:36:23 +0100456 {
Behdad Esfahbod2dbd3d22015-07-22 18:28:39 +0100457 if (_hb_glyph_info_is_default_ignorable (&info[i]))
Behdad Esfahbodf0010df2015-07-22 17:36:23 +0100458 {
Behdad Esfahbod2dbd3d22015-07-22 18:28:39 +0100459 /* Merge clusters.
460 * Same logic as buffer->delete_glyph(), but for in-place removal. */
461
462 unsigned int cluster = info[i].cluster;
463 if (i + 1 < count && cluster == info[i + 1].cluster)
464 continue; /* Cluster survives; do nothing. */
465
466 if (j)
467 {
468 /* Merge cluster backward. */
469 if (cluster < info[j - 1].cluster)
470 {
471 unsigned int old_cluster = info[j - 1].cluster;
472 for (unsigned k = j; k && info[k - 1].cluster == old_cluster; k--)
473 info[k - 1].cluster = cluster;
474 }
475 continue;
476 }
477
478 if (i + 1 < count)
479 buffer->merge_clusters (i, i + 2); /* Merge cluster forward. */
480
Behdad Esfahbodf0010df2015-07-22 17:36:23 +0100481 continue;
482 }
Behdad Esfahbod2dbd3d22015-07-22 18:28:39 +0100483
484 if (j != i)
485 {
486 info[j] = info[i];
487 pos[j] = pos[i];
488 }
489 j++;
Behdad Esfahbodf0010df2015-07-22 17:36:23 +0100490 }
Behdad Esfahbod2dbd3d22015-07-22 18:28:39 +0100491 buffer->len = j;
Behdad Esfahbodf0010df2015-07-22 17:36:23 +0100492 }
493}
494
495
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400496static inline void
Behdad Esfahbodb00321e2012-08-09 22:33:32 -0400497hb_ot_map_glyphs_fast (hb_buffer_t *buffer)
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100498{
Behdad Esfahbodb00321e2012-08-09 22:33:32 -0400499 /* Normalization process sets up glyph_index(), we just copy it. */
500 unsigned int count = buffer->len;
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400501 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbodb00321e2012-08-09 22:33:32 -0400502 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400503 info[i].codepoint = info[i].glyph_index();
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100504}
505
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400506static inline void
Behdad Esfahbod0ccf9b62012-08-29 11:53:26 -0400507hb_synthesize_glyph_classes (hb_ot_shape_context_t *c)
508{
509 unsigned int count = c->buffer->len;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200510 hb_glyph_info_t *info = c->buffer->info;
Behdad Esfahbod0ccf9b62012-08-29 11:53:26 -0400511 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodec181e52014-07-16 13:10:03 -0400512 {
513 hb_ot_layout_glyph_class_mask_t klass;
514
Behdad Esfahbod3b861422014-07-16 13:22:05 -0400515 /* Never mark default-ignorables as marks.
516 * They won't get in the way of lookups anyway,
517 * but having them as mark will cause them to be skipped
518 * over if the lookup-flag says so, but at least for the
519 * Mongolian variation selectors, looks like Uniscribe
520 * marks them as non-mark. Some Mongolian fonts without
521 * GDEF rely on this. Another notable character that
522 * this applies to is COMBINING GRAPHEME JOINER. */
523 klass = (_hb_glyph_info_get_general_category (&info[i]) !=
524 HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ||
525 _hb_glyph_info_is_default_ignorable (&info[i])) ?
Behdad Esfahbodec181e52014-07-16 13:10:03 -0400526 HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH :
527 HB_OT_LAYOUT_GLYPH_PROPS_MARK;
528 _hb_glyph_info_set_glyph_props (&info[i], klass);
529 }
Behdad Esfahbod0ccf9b62012-08-29 11:53:26 -0400530}
531
532static inline void
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400533hb_ot_substitute_default (hb_ot_shape_context_t *c)
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100534{
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200535 hb_buffer_t *buffer = c->buffer;
536
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400537 if (c->plan->shaper->preprocess_text)
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200538 c->plan->shaper->preprocess_text (c->plan, buffer, c->font);
Behdad Esfahbod9f9f04c2012-08-11 18:34:13 -0400539
Behdad Esfahbod73932512013-12-21 00:18:18 -0500540 hb_ot_shape_initialize_masks (c);
541
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400542 hb_ot_mirror_chars (c);
Behdad Esfahbodb65c0602011-07-28 16:48:43 -0400543
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200544 HB_BUFFER_ALLOCATE_VAR (buffer, glyph_index);
Behdad Esfahbodb00321e2012-08-09 22:33:32 -0400545
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200546 _hb_ot_shape_normalize (c->plan, buffer, c->font);
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400547
548 hb_ot_shape_setup_masks (c);
549
Behdad Esfahbod3992b5e2012-09-01 19:20:41 -0400550 /* This is unfortunate to go here, but necessary... */
551 if (!hb_ot_layout_has_positioning (c->face))
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200552 _hb_ot_shape_fallback_position_recategorize_marks (c->plan, c->font, buffer);
Behdad Esfahbod3992b5e2012-09-01 19:20:41 -0400553
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200554 hb_ot_map_glyphs_fast (buffer);
Behdad Esfahbodb00321e2012-08-09 22:33:32 -0400555
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200556 HB_BUFFER_DEALLOCATE_VAR (buffer, glyph_index);
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100557}
558
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400559static inline void
Behdad Esfahbodc86f9322011-07-25 00:44:50 -0400560hb_ot_substitute_complex (hb_ot_shape_context_t *c)
561{
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200562 hb_buffer_t *buffer = c->buffer;
563
Behdad Esfahbod8f3eebf2014-08-02 17:18:46 -0400564 _hb_buffer_allocate_gsubgpos_vars (buffer);
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200565 hb_ot_layout_substitute_start (c->font, buffer);
Behdad Esfahbod05bd1b62012-07-30 19:30:01 -0400566
Behdad Esfahbod5fecd8b2012-07-31 22:48:38 -0400567 if (!hb_ot_layout_has_glyph_classes (c->face))
568 hb_synthesize_glyph_classes (c);
569
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200570 c->plan->substitute (c->font, buffer);
Behdad Esfahbodc86f9322011-07-25 00:44:50 -0400571
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200572 hb_ot_layout_substitute_finish (c->font, buffer);
Behdad Esfahbodc86f9322011-07-25 00:44:50 -0400573
Behdad Esfahbodc86f9322011-07-25 00:44:50 -0400574 return;
575}
576
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400577static inline void
578hb_ot_substitute (hb_ot_shape_context_t *c)
579{
580 hb_ot_substitute_default (c);
581 hb_ot_substitute_complex (c);
582}
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100583
584/* Position */
585
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400586static inline void
Jonathan Kew798e4182014-06-10 13:10:30 +0100587adjust_mark_offsets (hb_glyph_position_t *pos)
588{
589 pos->x_offset -= pos->x_advance;
590 pos->y_offset -= pos->y_advance;
591}
592
593static inline void
594zero_mark_width (hb_glyph_position_t *pos)
595{
596 pos->x_advance = 0;
597 pos->y_advance = 0;
598}
599
600static inline void
601zero_mark_widths_by_unicode (hb_buffer_t *buffer, bool adjust_offsets)
Behdad Esfahbodd785fa02013-05-20 09:18:52 -0400602{
603 unsigned int count = buffer->len;
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400604 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbodd785fa02013-05-20 09:18:52 -0400605 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400606 if (_hb_glyph_info_get_general_category (&info[i]) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
Behdad Esfahbodd785fa02013-05-20 09:18:52 -0400607 {
Jonathan Kew798e4182014-06-10 13:10:30 +0100608 if (adjust_offsets)
609 adjust_mark_offsets (&buffer->pos[i]);
610 zero_mark_width (&buffer->pos[i]);
Behdad Esfahbodd785fa02013-05-20 09:18:52 -0400611 }
612}
613
614static inline void
Jonathan Kew798e4182014-06-10 13:10:30 +0100615zero_mark_widths_by_gdef (hb_buffer_t *buffer, bool adjust_offsets)
Behdad Esfahbodd785fa02013-05-20 09:18:52 -0400616{
617 unsigned int count = buffer->len;
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400618 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbodd785fa02013-05-20 09:18:52 -0400619 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400620 if (_hb_glyph_info_is_mark (&info[i]))
Behdad Esfahbodd785fa02013-05-20 09:18:52 -0400621 {
Jonathan Kew798e4182014-06-10 13:10:30 +0100622 if (adjust_offsets)
623 adjust_mark_offsets (&buffer->pos[i]);
624 zero_mark_width (&buffer->pos[i]);
Behdad Esfahbodd785fa02013-05-20 09:18:52 -0400625 }
626}
627
628static inline void
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400629hb_ot_position_default (hb_ot_shape_context_t *c)
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100630{
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200631 hb_direction_t direction = c->buffer->props.direction;
Behdad Esfahbod967240d2010-10-05 23:00:05 -0400632 unsigned int count = c->buffer->len;
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200633 hb_glyph_info_t *info = c->buffer->info;
634 hb_glyph_position_t *pos = c->buffer->pos;
Behdad Esfahbod56800022013-02-12 09:44:57 -0500635 for (unsigned int i = 0; i < count; i++)
636 {
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200637 c->font->get_glyph_advance_for_direction (info[i].codepoint,
638 direction,
639 &pos[i].x_advance,
640 &pos[i].y_advance);
641 c->font->subtract_glyph_origin_for_direction (info[i].codepoint,
642 direction,
643 &pos[i].x_offset,
644 &pos[i].y_offset);
Behdad Esfahbod56800022013-02-12 09:44:57 -0500645
646 }
Behdad Esfahbod4b011092013-09-13 20:17:42 -0400647}
648
649static inline bool
650hb_ot_position_complex (hb_ot_shape_context_t *c)
651{
652 bool ret = false;
653 unsigned int count = c->buffer->len;
Jonathan Kew798e4182014-06-10 13:10:30 +0100654 bool has_positioning = hb_ot_layout_has_positioning (c->face);
655 /* If the font has no GPOS, AND, no fallback positioning will
656 * happen, AND, direction is forward, then when zeroing mark
657 * widths, we shift the mark with it, such that the mark
658 * is positioned hanging over the previous glyph. When
659 * direction is backward we don't shift and it will end up
660 * hanging over the next glyph after the final reordering.
661 * If fallback positinoing happens or GPOS is present, we don't
662 * care.
663 */
664 bool adjust_offsets_when_zeroing = !(has_positioning || c->plan->shaper->fallback_position ||
665 HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction));
Behdad Esfahbod56800022013-02-12 09:44:57 -0500666
Behdad Esfahbod71b4c992013-10-28 00:20:59 +0100667 switch (c->plan->shaper->zero_width_marks)
Behdad Esfahbod56800022013-02-12 09:44:57 -0500668 {
Behdad Esfahbod127daf12013-05-20 09:11:35 -0400669 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
Jonathan Kew798e4182014-06-10 13:10:30 +0100670 zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
Behdad Esfahbod56800022013-02-12 09:44:57 -0500671 break;
672
Behdad Esfahbod127daf12013-05-20 09:11:35 -0400673 /* Not currently used for any shaper:
674 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE_EARLY:
Jonathan Kew798e4182014-06-10 13:10:30 +0100675 zero_mark_widths_by_unicode (c->buffer, adjust_offsets_when_zeroing);
Behdad Esfahbod127daf12013-05-20 09:11:35 -0400676 break;
677 */
678
Behdad Esfahbod56800022013-02-12 09:44:57 -0500679 default:
680 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
Behdad Esfahbod127daf12013-05-20 09:11:35 -0400681 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE_LATE:
682 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
Behdad Esfahbod56800022013-02-12 09:44:57 -0500683 break;
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100684 }
Behdad Esfahbodc86f9322011-07-25 00:44:50 -0400685
Jonathan Kew798e4182014-06-10 13:10:30 +0100686 if (has_positioning)
Behdad Esfahbodb65c0602011-07-28 16:48:43 -0400687 {
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200688 hb_glyph_info_t *info = c->buffer->info;
689 hb_glyph_position_t *pos = c->buffer->pos;
690
Behdad Esfahbodb65c0602011-07-28 16:48:43 -0400691 /* Change glyph origin to what GPOS expects, apply GPOS, change it back. */
Behdad Esfahbodc86f9322011-07-25 00:44:50 -0400692
Behdad Esfahbodb65c0602011-07-28 16:48:43 -0400693 for (unsigned int i = 0; i < count; i++) {
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200694 c->font->add_glyph_origin_for_direction (info[i].codepoint,
Behdad Esfahbod8fbfda92012-08-01 19:03:46 -0400695 HB_DIRECTION_LTR,
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200696 &pos[i].x_offset,
697 &pos[i].y_offset);
Behdad Esfahbodb65c0602011-07-28 16:48:43 -0400698 }
Behdad Esfahbodc86f9322011-07-25 00:44:50 -0400699
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400700 c->plan->position (c->font, c->buffer);
Behdad Esfahbodc86f9322011-07-25 00:44:50 -0400701
Behdad Esfahbodb65c0602011-07-28 16:48:43 -0400702 for (unsigned int i = 0; i < count; i++) {
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200703 c->font->subtract_glyph_origin_for_direction (info[i].codepoint,
Behdad Esfahbod8fbfda92012-08-01 19:03:46 -0400704 HB_DIRECTION_LTR,
Behdad Esfahbodac8cd512013-10-18 19:33:09 +0200705 &pos[i].x_offset,
706 &pos[i].y_offset);
Behdad Esfahbodb65c0602011-07-28 16:48:43 -0400707 }
Behdad Esfahbod2118fdb2011-08-02 14:06:51 -0400708
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400709 ret = true;
Behdad Esfahbodaffaf8a2012-08-07 22:41:38 -0400710 }
Behdad Esfahbod2bc3b9a2012-07-31 23:08:25 -0400711
Behdad Esfahbod71b4c992013-10-28 00:20:59 +0100712 switch (c->plan->shaper->zero_width_marks)
Behdad Esfahbod0291a652013-02-13 05:57:24 -0500713 {
Behdad Esfahbod127daf12013-05-20 09:11:35 -0400714 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE_LATE:
Jonathan Kew798e4182014-06-10 13:10:30 +0100715 zero_mark_widths_by_unicode (c->buffer, adjust_offsets_when_zeroing);
Behdad Esfahbod0291a652013-02-13 05:57:24 -0500716 break;
717
Behdad Esfahbod127daf12013-05-20 09:11:35 -0400718 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
Jonathan Kew798e4182014-06-10 13:10:30 +0100719 zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
Behdad Esfahbod127daf12013-05-20 09:11:35 -0400720 break;
721
Behdad Esfahbod0291a652013-02-13 05:57:24 -0500722 default:
723 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
Behdad Esfahbod127daf12013-05-20 09:11:35 -0400724 //case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_UNICODE_EARLY:
725 case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
Behdad Esfahbod0291a652013-02-13 05:57:24 -0500726 break;
727 }
728
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400729 return ret;
Behdad Esfahbodc86f9322011-07-25 00:44:50 -0400730}
731
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400732static inline void
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400733hb_ot_position (hb_ot_shape_context_t *c)
734{
Behdad Esfahbod4b011092013-09-13 20:17:42 -0400735 hb_ot_layout_position_start (c->font, c->buffer);
736
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400737 hb_ot_position_default (c);
738
739 hb_bool_t fallback = !hb_ot_position_complex (c);
740
Behdad Esfahbodf0010df2015-07-22 17:36:23 +0100741 /* Need to do this here, since position_finish and fallback positioning
742 * might be affected by width of default_ignorables. */
743 hb_ot_hide_default_ignorables (c);
744
Behdad Esfahbod4b011092013-09-13 20:17:42 -0400745 hb_ot_layout_position_finish (c->font, c->buffer);
746
Behdad Esfahbod865745b2012-11-14 13:48:26 -0800747 if (fallback && c->plan->shaper->fallback_position)
748 _hb_ot_shape_fallback_position (c->plan, c->font, c->buffer);
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400749
750 if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
751 hb_buffer_reverse (c->buffer);
752
Behdad Esfahbod865745b2012-11-14 13:48:26 -0800753 /* Visual fallback goes here. */
754
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400755 if (fallback)
Behdad Esfahbodba87b8f2013-02-21 15:23:39 -0500756 _hb_ot_shape_fallback_kern (c->plan, c->font, c->buffer);
Behdad Esfahbod8f3eebf2014-08-02 17:18:46 -0400757
758 _hb_buffer_deallocate_gsubgpos_vars (c->buffer);
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400759}
760
761
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400762/* Pull it all together! */
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100763
Behdad Esfahbod967240d2010-10-05 23:00:05 -0400764static void
Behdad Esfahbode82061e2012-07-27 02:29:32 -0400765hb_ot_shape_internal (hb_ot_shape_context_t *c)
Behdad Esfahbod967240d2010-10-05 23:00:05 -0400766{
Behdad Esfahbodb65c0602011-07-28 16:48:43 -0400767 c->buffer->deallocate_var_all ();
768
Behdad Esfahbodd2ba0162010-10-12 15:35:45 -0400769 /* Save the original direction, we use it later. */
Behdad Esfahbodbbbbe802010-12-07 16:22:02 -0500770 c->target_direction = c->buffer->props.direction;
Behdad Esfahbodd2ba0162010-10-12 15:35:45 -0400771
Behdad Esfahbod101303d2013-10-18 00:42:39 +0200772 _hb_buffer_allocate_unicode_vars (c->buffer);
Behdad Esfahbodb65c0602011-07-28 16:48:43 -0400773
Behdad Esfahbod330a2af2012-06-08 20:40:02 -0400774 c->buffer->clear_output ();
775
Behdad Esfahbodd1deaa22012-05-09 15:04:13 +0200776 hb_set_unicode_props (c->buffer);
Behdad Esfahbod6912e472012-09-01 20:38:45 -0400777 hb_insert_dotted_circle (c->buffer, c->font);
Behdad Esfahbod4ff0d2d2011-07-22 16:15:32 -0400778 hb_form_clusters (c->buffer);
779
Behdad Esfahbodaa726412011-07-21 11:34:59 -0400780 hb_ensure_native_direction (c->buffer);
Behdad Esfahbod54d1a0d2011-07-21 01:11:09 -0400781
Behdad Esfahbod5c60b702012-08-09 21:58:07 -0400782 hb_ot_substitute (c);
783 hb_ot_position (c);
Behdad Esfahbod655586f2011-07-21 00:51:18 -0400784
Behdad Esfahbod101303d2013-10-18 00:42:39 +0200785 _hb_buffer_deallocate_unicode_vars (c->buffer);
Behdad Esfahbodb65c0602011-07-28 16:48:43 -0400786
Behdad Esfahbodbbbbe802010-12-07 16:22:02 -0500787 c->buffer->props.direction = c->target_direction;
Behdad Esfahbod9f9bcce2011-07-28 17:06:46 -0400788
789 c->buffer->deallocate_var_all ();
Behdad Esfahbod967240d2010-10-05 23:00:05 -0400790}
791
Behdad Esfahbodfd3d32d2010-10-12 16:57:47 -0400792
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400793hb_bool_t
Behdad Esfahbodbd26b4d2012-07-26 22:05:39 -0400794_hb_ot_shape (hb_shape_plan_t *shape_plan,
795 hb_font_t *font,
Behdad Esfahbod6bd9b472012-04-12 14:53:53 -0400796 hb_buffer_t *buffer,
797 const hb_feature_t *features,
798 unsigned int num_features)
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100799{
Behdad Esfahbode82061e2012-07-27 02:29:32 -0400800 hb_ot_shape_context_t c = {HB_SHAPER_DATA_GET (shape_plan), font, font->face, buffer, features, num_features};
801 hb_ot_shape_internal (&c);
Behdad Esfahbod02aeca92011-08-04 22:31:05 -0400802
Behdad Esfahbod0594a242012-06-05 20:35:40 -0400803 return true;
Behdad Esfahbodca663bb2010-05-21 14:34:23 +0100804}
Behdad Esfahbod1827dc22012-04-24 16:56:37 -0400805
806
Sascha Brawer01c3a882015-06-01 13:22:01 +0200807/**
808 * Since: 0.9.7
809 **/
Behdad Esfahbodf3064102012-11-15 18:39:46 -0800810void
811hb_ot_shape_plan_collect_lookups (hb_shape_plan_t *shape_plan,
812 hb_tag_t table_tag,
813 hb_set_t *lookup_indexes /* OUT */)
Behdad Esfahbodb00321e2012-08-09 22:33:32 -0400814{
Behdad Esfahbod51bb4982012-11-16 14:08:05 -0800815 /* XXX Does the first part always succeed? */
Behdad Esfahbodf3064102012-11-15 18:39:46 -0800816 HB_SHAPER_DATA_GET (shape_plan)->collect_lookups (table_tag, lookup_indexes);
Behdad Esfahbodb00321e2012-08-09 22:33:32 -0400817}
818
Behdad Esfahbodf3064102012-11-15 18:39:46 -0800819
820/* TODO Move this to hb-ot-shape-normalize, make it do decompose, and make it public. */
821static void
822add_char (hb_font_t *font,
823 hb_unicode_funcs_t *unicode,
824 hb_bool_t mirror,
825 hb_codepoint_t u,
826 hb_set_t *glyphs)
827{
828 hb_codepoint_t glyph;
829 if (font->get_glyph (u, 0, &glyph))
830 glyphs->add (glyph);
831 if (mirror)
832 {
833 hb_codepoint_t m = unicode->mirroring (u);
834 if (m != u && font->get_glyph (m, 0, &glyph))
835 glyphs->add (glyph);
836 }
837}
838
839
Sascha Brawer01c3a882015-06-01 13:22:01 +0200840/**
841 * Since: 0.9.2
842 **/
Behdad Esfahbod1827dc22012-04-24 16:56:37 -0400843void
844hb_ot_shape_glyphs_closure (hb_font_t *font,
845 hb_buffer_t *buffer,
846 const hb_feature_t *features,
847 unsigned int num_features,
848 hb_set_t *glyphs)
849{
850 hb_ot_shape_plan_t plan;
851
Behdad Esfahbodf3064102012-11-15 18:39:46 -0800852 const char *shapers[] = {"ot", NULL};
853 hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
854 features, num_features, shapers);
Behdad Esfahbod1827dc22012-04-24 16:56:37 -0400855
Behdad Esfahbodf3064102012-11-15 18:39:46 -0800856 bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL;
Behdad Esfahbod1827dc22012-04-24 16:56:37 -0400857
Behdad Esfahbod1827dc22012-04-24 16:56:37 -0400858 unsigned int count = buffer->len;
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400859 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbod1827dc22012-04-24 16:56:37 -0400860 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod7cd33f22014-07-17 14:22:11 -0400861 add_char (font, buffer->unicode, mirror, info[i].codepoint, glyphs);
Behdad Esfahbodf3064102012-11-15 18:39:46 -0800862
863 hb_set_t lookups;
864 lookups.init ();
865 hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, &lookups);
Behdad Esfahbod1827dc22012-04-24 16:56:37 -0400866
867 /* And find transitive closure. */
868 hb_set_t copy;
869 copy.init ();
Behdad Esfahbod1827dc22012-04-24 16:56:37 -0400870 do {
871 copy.set (glyphs);
Behdad Esfahbodf3064102012-11-15 18:39:46 -0800872 for (hb_codepoint_t lookup_index = -1; hb_set_next (&lookups, &lookup_index);)
873 hb_ot_layout_lookup_substitute_closure (font->face, lookup_index, glyphs);
Behdad Esfahbodaec89de2012-11-15 16:15:42 -0800874 } while (!copy.is_equal (glyphs));
Behdad Esfahbode82061e2012-07-27 02:29:32 -0400875
876 hb_shape_plan_destroy (shape_plan);
Behdad Esfahbod1827dc22012-04-24 16:56:37 -0400877}