blob: d7138c8c2a469541fb3dba3553b2be51a0b71b00 [file] [log] [blame]
Behdad Esfahboda9663952015-07-20 14:24:55 +01001/*
2 * Copyright © 2015 Mozilla Foundation.
3 * Copyright © 2015 Google, Inc.
4 *
5 * This is part of HarfBuzz, a text shaping library.
6 *
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 * Mozilla Author(s): Jonathan Kew
26 * Google Author(s): Behdad Esfahbod
27 */
28
29#include "hb-ot-shape-complex-use-private.hh"
Behdad Esfahbod8ba9e682015-07-22 11:16:01 +010030#include "hb-ot-shape-complex-arabic-private.hh"
Behdad Esfahboda9663952015-07-20 14:24:55 +010031
32/* buffer var allocations */
33#define use_category() complex_var_u8_0()
34
35
36/*
37 * Universal Shaping Engine.
38 * https://www.microsoft.com/typography/OpenTypeDev/USE/intro.htm
39 */
40
41static const hb_tag_t
42basic_features[] =
43{
44 /*
45 * Basic features.
Behdad Esfahbod4febed62015-07-21 10:24:32 +010046 * These features are applied all at once, before reordering.
Behdad Esfahboda9663952015-07-20 14:24:55 +010047 */
Behdad Esfahbod4febed62015-07-21 10:24:32 +010048 HB_TAG('r','k','r','f'),
Behdad Esfahboda9663952015-07-20 14:24:55 +010049 HB_TAG('a','b','v','f'),
50 HB_TAG('b','l','w','f'),
Behdad Esfahbod4febed62015-07-21 10:24:32 +010051 HB_TAG('h','a','l','f'),
Behdad Esfahboda9663952015-07-20 14:24:55 +010052 HB_TAG('p','s','t','f'),
Behdad Esfahbod4febed62015-07-21 10:24:32 +010053 HB_TAG('v','a','t','u'),
54 HB_TAG('c','j','c','t'),
Behdad Esfahboda9663952015-07-20 14:24:55 +010055};
56static const hb_tag_t
Behdad Esfahbod8ba9e682015-07-22 11:16:01 +010057arabic_features[] =
58{
59 HB_TAG('i','s','o','l'),
60 HB_TAG('i','n','i','t'),
61 HB_TAG('m','e','d','i'),
62 HB_TAG('f','i','n','a'),
63 /* The spec doesn't specify these but we apply anyway, since our Arabic shaper
64 * does. These are only used in Syriac spec. */
65 HB_TAG('m','e','d','2'),
66 HB_TAG('f','i','n','2'),
67 HB_TAG('f','i','n','3'),
68};
Behdad Esfahbod9cd59db2015-07-22 13:27:06 +010069/* Same order as arabic_features. Don't need Syriac stuff.*/
70enum joining_form_t {
71 ISOL,
72 INIT,
73 MEDI,
74 FINA,
75 _NONE
76};
Behdad Esfahbod8ba9e682015-07-22 11:16:01 +010077static const hb_tag_t
Behdad Esfahboda9663952015-07-20 14:24:55 +010078other_features[] =
79{
80 /*
81 * Other features.
Behdad Esfahbod4febed62015-07-21 10:24:32 +010082 * These features are applied all at once, after reordering.
Behdad Esfahboda9663952015-07-20 14:24:55 +010083 */
Behdad Esfahboda9663952015-07-20 14:24:55 +010084 HB_TAG('a','b','v','s'),
85 HB_TAG('b','l','w','s'),
Behdad Esfahbod4febed62015-07-21 10:24:32 +010086 HB_TAG('h','a','l','n'),
87 HB_TAG('p','r','e','s'),
Behdad Esfahboda9663952015-07-20 14:24:55 +010088 HB_TAG('p','s','t','s'),
89 /* Positioning features, though we don't care about the types. */
90 HB_TAG('d','i','s','t'),
Behdad Esfahbod4febed62015-07-21 10:24:32 +010091 HB_TAG('a','b','v','m'),
92 HB_TAG('b','l','w','m'),
Behdad Esfahboda9663952015-07-20 14:24:55 +010093};
94
95static void
96setup_syllables (const hb_ot_shape_plan_t *plan,
97 hb_font_t *font,
98 hb_buffer_t *buffer);
99static void
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100100clear_substitution_flags (const hb_ot_shape_plan_t *plan,
101 hb_font_t *font,
102 hb_buffer_t *buffer);
103static void
104record_rphf (const hb_ot_shape_plan_t *plan,
105 hb_font_t *font,
106 hb_buffer_t *buffer);
107static void
108record_pref (const hb_ot_shape_plan_t *plan,
109 hb_font_t *font,
110 hb_buffer_t *buffer);
111static void
Behdad Esfahbod4febed62015-07-21 10:24:32 +0100112reorder (const hb_ot_shape_plan_t *plan,
113 hb_font_t *font,
114 hb_buffer_t *buffer);
Behdad Esfahboda9663952015-07-20 14:24:55 +0100115
116static void
117collect_features_use (hb_ot_shape_planner_t *plan)
118{
119 hb_ot_map_builder_t *map = &plan->map;
120
121 /* Do this before any lookups have been applied. */
122 map->add_gsub_pause (setup_syllables);
123
Behdad Esfahbod4febed62015-07-21 10:24:32 +0100124 /* "Default glyph pre-processing group" */
Behdad Esfahboda9663952015-07-20 14:24:55 +0100125 map->add_global_bool_feature (HB_TAG('l','o','c','l'));
Behdad Esfahboda9663952015-07-20 14:24:55 +0100126 map->add_global_bool_feature (HB_TAG('c','c','m','p'));
Behdad Esfahbod4febed62015-07-21 10:24:32 +0100127 map->add_global_bool_feature (HB_TAG('n','u','k','t'));
128 map->add_global_bool_feature (HB_TAG('a','k','h','n'));
Behdad Esfahboda9663952015-07-20 14:24:55 +0100129
Behdad Esfahbod4febed62015-07-21 10:24:32 +0100130 /* "Reordering group" */
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100131 map->add_gsub_pause (clear_substitution_flags);
132 map->add_feature (HB_TAG('r','p','h','f'), 1, F_MANUAL_ZWJ);
133 map->add_gsub_pause (record_rphf);
Behdad Esfahboda85c4da2015-07-21 16:07:10 +0100134 map->add_gsub_pause (clear_substitution_flags);
Behdad Esfahbodcf59c752015-07-21 14:51:45 +0100135 map->add_feature (HB_TAG('p','r','e','f'), 1, F_GLOBAL | F_MANUAL_ZWJ);
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100136 map->add_gsub_pause (record_pref);
Behdad Esfahbod4febed62015-07-21 10:24:32 +0100137
138 /* "Orthographic unit shaping group" */
Behdad Esfahboda9663952015-07-20 14:24:55 +0100139 for (unsigned int i = 0; i < ARRAY_LENGTH (basic_features); i++)
Behdad Esfahboda9663952015-07-20 14:24:55 +0100140 map->add_feature (basic_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ);
Behdad Esfahbod4febed62015-07-21 10:24:32 +0100141
142 map->add_gsub_pause (reorder);
143
144 /* "Topographical features" */
Behdad Esfahbodf1c20e12015-07-27 12:16:02 +0200145 for (unsigned int i = 0; i < ARRAY_LENGTH (arabic_features); i++)
Behdad Esfahbod8ba9e682015-07-22 11:16:01 +0100146 map->add_feature (arabic_features[i], 1, F_NONE);
Behdad Esfahbod8ba9e682015-07-22 11:16:01 +0100147 map->add_gsub_pause (NULL);
Behdad Esfahbod4febed62015-07-21 10:24:32 +0100148
149 /* "Standard typographic presentation" and "Positional feature application" */
Behdad Esfahboda9663952015-07-20 14:24:55 +0100150 for (unsigned int i = 0; i < ARRAY_LENGTH (other_features); i++)
151 map->add_feature (other_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ);
152}
153
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100154struct use_shape_plan_t
155{
156 ASSERT_POD ();
157
158 hb_mask_t rphf_mask;
Behdad Esfahbod8ba9e682015-07-22 11:16:01 +0100159
160 arabic_shape_plan_t *arabic_plan;
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100161};
162
Behdad Esfahbod8ba9e682015-07-22 11:16:01 +0100163static bool
164has_arabic_joining (hb_script_t script)
165{
166 /* List of scripts that have data in arabic-table. */
167 switch ((int) script)
168 {
169 /* Unicode-1.1 additions */
170 case HB_SCRIPT_ARABIC:
171
172 /* Unicode-3.0 additions */
173 case HB_SCRIPT_MONGOLIAN:
174 case HB_SCRIPT_SYRIAC:
175
176 /* Unicode-5.0 additions */
177 case HB_SCRIPT_NKO:
178 case HB_SCRIPT_PHAGS_PA:
179
180 /* Unicode-6.0 additions */
181 case HB_SCRIPT_MANDAIC:
182
183 /* Unicode-7.0 additions */
184 case HB_SCRIPT_MANICHAEAN:
185 case HB_SCRIPT_PSALTER_PAHLAVI:
186
187 return true;
188
189 default:
190 return false;
191 }
192}
193
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100194static void *
195data_create_use (const hb_ot_shape_plan_t *plan)
196{
197 use_shape_plan_t *use_plan = (use_shape_plan_t *) calloc (1, sizeof (use_shape_plan_t));
198 if (unlikely (!use_plan))
199 return NULL;
200
201 use_plan->rphf_mask = plan->map.get_1_mask (HB_TAG('r','p','h','f'));
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100202
Behdad Esfahbod8ba9e682015-07-22 11:16:01 +0100203 if (has_arabic_joining (plan->props.script))
204 {
205 use_plan->arabic_plan = (arabic_shape_plan_t *) data_create_arabic (plan);
206 if (unlikely (!use_plan->arabic_plan))
207 {
208 free (use_plan);
209 return NULL;
210 }
211 }
212
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100213 return use_plan;
214}
215
216static void
217data_destroy_use (void *data)
218{
Behdad Esfahbod8ba9e682015-07-22 11:16:01 +0100219 use_shape_plan_t *use_plan = (use_shape_plan_t *) data;
220
221 if (use_plan->arabic_plan)
222 data_destroy_arabic (use_plan->arabic_plan);
223
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100224 free (data);
225}
Behdad Esfahboda9663952015-07-20 14:24:55 +0100226
227enum syllable_type_t {
228 independent_cluster,
229 virama_terminated_cluster,
230 consonant_cluster,
231 vowel_cluster,
232 number_joiner_terminated_cluster,
233 numeral_cluster,
234 symbol_cluster,
Behdad Esfahbod40c4a992015-07-21 17:14:54 +0100235 broken_cluster,
Behdad Esfahboda9663952015-07-20 14:24:55 +0100236};
237
238#include "hb-ot-shape-complex-use-machine.hh"
239
240
241static inline void
242set_use_properties (hb_glyph_info_t &info)
243{
244 hb_codepoint_t u = info.codepoint;
Behdad Esfahbodb4c08292015-07-21 09:31:19 +0100245 info.use_category() = hb_use_get_categories (u);
Behdad Esfahboda9663952015-07-20 14:24:55 +0100246}
247
248
249static void
Behdad Esfahbod8ba9e682015-07-22 11:16:01 +0100250setup_masks_use (const hb_ot_shape_plan_t *plan,
Behdad Esfahboda9663952015-07-20 14:24:55 +0100251 hb_buffer_t *buffer,
252 hb_font_t *font HB_UNUSED)
253{
Behdad Esfahbod8ba9e682015-07-22 11:16:01 +0100254 const use_shape_plan_t *use_plan = (const use_shape_plan_t *) plan->data;
255
256 /* Do this before allocating use_category(). */
257 if (use_plan->arabic_plan)
258 {
259 setup_masks_arabic_plan (use_plan->arabic_plan, buffer, plan->props.script);
260 }
261
Behdad Esfahboda9663952015-07-20 14:24:55 +0100262 HB_BUFFER_ALLOCATE_VAR (buffer, use_category);
263
264 /* We cannot setup masks here. We save information about characters
265 * and setup masks later on in a pause-callback. */
266
267 unsigned int count = buffer->len;
268 hb_glyph_info_t *info = buffer->info;
269 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodb4c08292015-07-21 09:31:19 +0100270 info[i].use_category() = hb_use_get_categories (info[i].codepoint);
Behdad Esfahboda9663952015-07-20 14:24:55 +0100271}
272
273static void
Behdad Esfahbod9cd59db2015-07-22 13:27:06 +0100274setup_rphf_mask (const hb_ot_shape_plan_t *plan,
Behdad Esfahboda9663952015-07-20 14:24:55 +0100275 hb_buffer_t *buffer)
276{
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100277 const use_shape_plan_t *use_plan = (const use_shape_plan_t *) plan->data;
Behdad Esfahbodac596512015-07-22 11:54:02 +0100278
279 hb_mask_t mask = use_plan->rphf_mask;
280 if (!mask) return;
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100281
282 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbodac596512015-07-22 11:54:02 +0100283
284 foreach_syllable (buffer, start, end)
285 {
286 unsigned int limit = info[start].use_category() == USE_R ? 1 : MIN (3u, end - start);
287 for (unsigned int i = start; i < start + limit; i++)
288 info[i].mask |= mask;
289 }
Behdad Esfahboda9663952015-07-20 14:24:55 +0100290}
291
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100292static void
Behdad Esfahbod9cd59db2015-07-22 13:27:06 +0100293setup_topographical_masks (const hb_ot_shape_plan_t *plan,
294 hb_buffer_t *buffer)
295{
296
297 ASSERT_STATIC (INIT < 4 && ISOL < 4 && MEDI < 4 && FINA < 4);
298 hb_mask_t masks[4], all_masks = 0;
299 for (unsigned int i = 0; i < 4; i++)
300 {
301 masks[i] = plan->map.get_1_mask (arabic_features[i]);
302 if (masks[i] == plan->map.get_global_mask ())
303 masks[i] = 0;
304 all_masks |= masks[i];
305 }
306 if (!all_masks)
307 return;
308 hb_mask_t other_masks = ~all_masks;
309
310 unsigned int last_start = 0;
311 joining_form_t last_form = _NONE;
312 hb_glyph_info_t *info = buffer->info;
313 foreach_syllable (buffer, start, end)
314 {
315 syllable_type_t syllable_type = (syllable_type_t) (info[start].syllable() & 0x0F);
316 switch (syllable_type)
317 {
318 case independent_cluster:
319 case symbol_cluster:
320 /* These don't join. Nothing to do. */
321 last_form = _NONE;
322 break;
323
324 case virama_terminated_cluster:
325 case consonant_cluster:
326 case vowel_cluster:
327 case number_joiner_terminated_cluster:
328 case numeral_cluster:
329 case broken_cluster:
330
331 bool join = last_form == FINA || last_form == ISOL;
332
333 if (join)
334 {
335 /* Fixup previous syllable's form. */
336 last_form = last_form == FINA ? MEDI : INIT;
337 for (unsigned int i = last_start; i < start; i++)
338 info[i].mask = (info[i].mask & other_masks) | masks[last_form];
339 }
340
341 /* Form for this syllable. */
342 last_form = join ? FINA : ISOL;
343 for (unsigned int i = start; i < end; i++)
344 info[i].mask = (info[i].mask & other_masks) | masks[last_form];
345
346 break;
347 }
348
349 last_start = start;
350 }
351}
352
353static void
354setup_syllables (const hb_ot_shape_plan_t *plan,
355 hb_font_t *font HB_UNUSED,
356 hb_buffer_t *buffer)
357{
358 find_syllables (buffer);
359 setup_rphf_mask (plan, buffer);
360 setup_topographical_masks (plan, buffer);
361}
362
363static void
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100364clear_substitution_flags (const hb_ot_shape_plan_t *plan,
365 hb_font_t *font HB_UNUSED,
366 hb_buffer_t *buffer)
367{
368 hb_glyph_info_t *info = buffer->info;
369 unsigned int count = buffer->len;
370 for (unsigned int i = 0; i < count; i++)
371 _hb_glyph_info_clear_substituted_and_ligated_and_multiplied (&info[i]);
372}
373
374static void
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100375record_rphf (const hb_ot_shape_plan_t *plan,
376 hb_font_t *font,
377 hb_buffer_t *buffer)
378{
379 const use_shape_plan_t *use_plan = (const use_shape_plan_t *) plan->data;
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100380
Behdad Esfahbodac596512015-07-22 11:54:02 +0100381 hb_mask_t mask = use_plan->rphf_mask;
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100382 if (!mask) return;
383 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100384
Behdad Esfahbodac596512015-07-22 11:54:02 +0100385 foreach_syllable (buffer, start, end)
386 {
387 /* Mark a substituted repha as USE_R. */
388 for (unsigned int i = start; i < end && (info[i].mask & mask); i++)
389 if (_hb_glyph_info_substituted (&info[i]))
390 {
391 info[i].use_category() = USE_R;
392 break;
393 }
394 }
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100395}
396
397static void
398record_pref (const hb_ot_shape_plan_t *plan,
399 hb_font_t *font,
400 hb_buffer_t *buffer)
401{
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100402 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbodac596512015-07-22 11:54:02 +0100403
404 foreach_syllable (buffer, start, end)
405 {
406 /* Mark a substituted pref as VPre, as they behave the same way. */
407 for (unsigned int i = start; i < end; i++)
408 if (_hb_glyph_info_substituted (&info[i]))
409 {
410 info[i].use_category() = USE_VPre;
411 break;
412 }
413 }
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100414}
Behdad Esfahboda9663952015-07-20 14:24:55 +0100415
416static void
Behdad Esfahbodac596512015-07-22 11:54:02 +0100417reorder_syllable (hb_buffer_t *buffer, unsigned int start, unsigned int end)
Behdad Esfahboda9663952015-07-20 14:24:55 +0100418{
419 syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F);
Behdad Esfahboda85c4da2015-07-21 16:07:10 +0100420 /* Only a few syllable types need reordering. */
421 if (unlikely (!(FLAG_SAFE (syllable_type) &
422 (FLAG (virama_terminated_cluster) |
423 FLAG (consonant_cluster) |
Behdad Esfahbod40c4a992015-07-21 17:14:54 +0100424 FLAG (vowel_cluster) |
425 FLAG (broken_cluster) |
426 0))))
Behdad Esfahboda85c4da2015-07-21 16:07:10 +0100427 return;
428
429 hb_glyph_info_t *info = buffer->info;
430
431#define HALANT_FLAGS FLAG(USE_H)
432#define BASE_FLAGS (FLAG (USE_B) | FLAG (USE_GB) | FLAG (USE_IV))
433
434 /* Move things forward. */
435 if (info[start].use_category() == USE_R && end - start > 1)
436 {
437 /* Got a repha. Reorder it to after first base, before first halant. */
438 for (unsigned int i = start + 1; i < end; i++)
439 if (FLAG_UNSAFE (info[i].use_category()) & (HALANT_FLAGS | BASE_FLAGS))
440 {
441 /* If we hit a halant, move before it; otherwise it's a base: move to it's
442 * place, and shift things in between backward. */
443
444 if (info[i].use_category() == USE_H)
445 i--;
446
447 hb_glyph_info_t t = info[start];
448 memmove (&info[start], &info[start + 1], (i - start) * sizeof (info[0]));
449 info[i] = t;
450 buffer->merge_clusters (start, i + 1);
451
452 break;
453 }
Behdad Esfahboda9663952015-07-20 14:24:55 +0100454 }
Behdad Esfahboda85c4da2015-07-21 16:07:10 +0100455
Behdad Esfahbod7ce03eb2015-07-21 16:55:26 +0100456 /* Move things back. */
457 unsigned int j = end;
458 for (unsigned int i = start; i < end; i++)
459 {
460 uint32_t flag = FLAG_UNSAFE (info[i].use_category());
461 if (flag & (HALANT_FLAGS | BASE_FLAGS))
462 {
463 /* If we hit a halant, move before it; otherwise it's a base: move to it's
464 * place, and shift things in between backward. */
465 if (info[i].use_category() == USE_H)
466 j = i + 1;
467 else
468 j = i;
469 }
Behdad Esfahboda51a6612015-07-21 18:24:21 +0100470 else if (((flag) & (FLAG (USE_VPre) | FLAG (USE_VMPre))) &&
471 /* Only move the first component of a MultipleSubst. */
472 0 == _hb_glyph_info_get_lig_comp (&info[i]) &&
473 j < i)
Behdad Esfahbod7ce03eb2015-07-21 16:55:26 +0100474 {
475 hb_glyph_info_t t = info[i];
476 memmove (&info[j + 1], &info[j], (i - j) * sizeof (info[0]));
477 info[j] = t;
478 buffer->merge_clusters (j, i + 1);
479 }
480 }
Behdad Esfahboda9663952015-07-20 14:24:55 +0100481}
482
483static inline void
484insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED,
485 hb_font_t *font,
486 hb_buffer_t *buffer)
487{
Behdad Esfahboda9663952015-07-20 14:24:55 +0100488 /* Note: This loop is extra overhead, but should not be measurable. */
489 bool has_broken_syllables = false;
490 unsigned int count = buffer->len;
491 hb_glyph_info_t *info = buffer->info;
492 for (unsigned int i = 0; i < count; i++)
493 if ((info[i].syllable() & 0x0F) == broken_cluster)
494 {
495 has_broken_syllables = true;
496 break;
497 }
498 if (likely (!has_broken_syllables))
499 return;
500
501
Behdad Esfahbod40c4a992015-07-21 17:14:54 +0100502 hb_codepoint_t dottedcircle_glyph;
503 if (!font->get_glyph (0x25CCu, 0, &dottedcircle_glyph))
504 return;
505
Behdad Esfahboda9663952015-07-20 14:24:55 +0100506 hb_glyph_info_t dottedcircle = {0};
Behdad Esfahbod4febed62015-07-21 10:24:32 +0100507 if (!font->get_glyph (0x25CCu, 0, &dottedcircle.codepoint))
508 return;
509 dottedcircle.use_category() = hb_use_get_categories (0x25CC);
Behdad Esfahboda9663952015-07-20 14:24:55 +0100510
511 buffer->clear_output ();
512
513 buffer->idx = 0;
Behdad Esfahbodac596512015-07-22 11:54:02 +0100514
Behdad Esfahboda9663952015-07-20 14:24:55 +0100515 unsigned int last_syllable = 0;
516 while (buffer->idx < buffer->len)
517 {
518 unsigned int syllable = buffer->cur().syllable();
519 syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F);
520 if (unlikely (last_syllable != syllable && syllable_type == broken_cluster))
521 {
522 last_syllable = syllable;
523
524 hb_glyph_info_t info = dottedcircle;
525 info.cluster = buffer->cur().cluster;
526 info.mask = buffer->cur().mask;
527 info.syllable() = buffer->cur().syllable();
Behdad Esfahbod40c4a992015-07-21 17:14:54 +0100528 /* TODO Set glyph_props? */
529
530 /* Insert dottedcircle after possible Repha. */
531 while (buffer->idx < buffer->len &&
532 last_syllable == buffer->cur().syllable() &&
533 buffer->cur().use_category() == USE_R)
534 buffer->next_glyph ();
Behdad Esfahboda9663952015-07-20 14:24:55 +0100535
536 buffer->output_info (info);
537 }
538 else
539 buffer->next_glyph ();
540 }
541
542 buffer->swap_buffers ();
Behdad Esfahboda9663952015-07-20 14:24:55 +0100543}
544
545static void
Behdad Esfahbod4febed62015-07-21 10:24:32 +0100546reorder (const hb_ot_shape_plan_t *plan,
547 hb_font_t *font,
548 hb_buffer_t *buffer)
Behdad Esfahboda9663952015-07-20 14:24:55 +0100549{
550 insert_dotted_circles (plan, font, buffer);
551
552 hb_glyph_info_t *info = buffer->info;
Behdad Esfahbodac596512015-07-22 11:54:02 +0100553
554 foreach_syllable (buffer, start, end)
555 reorder_syllable (buffer, start, end);
Behdad Esfahboda9663952015-07-20 14:24:55 +0100556
557 /* Zero syllables now... */
Behdad Esfahbodac596512015-07-22 11:54:02 +0100558 unsigned int count = buffer->len;
Behdad Esfahboda9663952015-07-20 14:24:55 +0100559 for (unsigned int i = 0; i < count; i++)
560 info[i].syllable() = 0;
561
562 HB_BUFFER_DEALLOCATE_VAR (buffer, use_category);
563}
564
Behdad Esfahboda08a2782015-07-21 18:09:40 +0100565static bool
566compose_use (const hb_ot_shape_normalize_context_t *c,
567 hb_codepoint_t a,
568 hb_codepoint_t b,
569 hb_codepoint_t *ab)
570{
571 /* Avoid recomposing split matras. */
572 if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (c->unicode->general_category (a)))
573 return false;
574
ThePhDf798b8e2015-11-21 16:57:26 -0500575 return (bool)c->unicode->compose (a, b, ab);
Behdad Esfahboda08a2782015-07-21 18:09:40 +0100576}
577
Behdad Esfahboda9663952015-07-20 14:24:55 +0100578
579const hb_ot_complex_shaper_t _hb_ot_complex_shaper_use =
580{
581 "use",
582 collect_features_use,
Behdad Esfahbod4febed62015-07-21 10:24:32 +0100583 NULL, /* override_features */
Behdad Esfahbod595936e2015-07-21 14:15:35 +0100584 data_create_use,
585 data_destroy_use,
Behdad Esfahboda9663952015-07-20 14:24:55 +0100586 NULL, /* preprocess_text */
587 HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT,
588 NULL, /* decompose */
Behdad Esfahboda08a2782015-07-21 18:09:40 +0100589 compose_use,
Behdad Esfahboda9663952015-07-20 14:24:55 +0100590 setup_masks_use,
591 HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
592 false, /* fallback_position */
593};