1 /*
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.hh"
30
31 #ifndef HB_NO_OT_SHAPE
32
33 #include "hb-ot-shape-complex-use.hh"
34 #include "hb-ot-shape-complex-arabic.hh"
35 #include "hb-ot-shape-complex-vowel-constraints.hh"
36
37 /* buffer var allocations */
38 #define use_category() complex_var_u8_0()
39
40 /*
41 * Universal Shaping Engine.
42 * https://docs.microsoft.com/en-us/typography/script-development/use
43 */
44
45 static const hb_tag_t use_basic_features[] = {
46 /*
47 * Basic features.
48 * These features are applied all at once, before reordering.
49 */
50 HB_TAG('r', 'k', 'r', 'f'),
51 HB_TAG('a', 'b', 'v', 'f'),
52 HB_TAG('b', 'l', 'w', 'f'),
53 HB_TAG('h', 'a', 'l', 'f'),
54 HB_TAG('p', 's', 't', 'f'),
55 HB_TAG('v', 'a', 't', 'u'),
56 HB_TAG('c', 'j', 'c', 't'),
57 };
58 static const hb_tag_t use_topographical_features[] = {
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 };
64 /* Same order as use_topographical_features. */
65 enum joining_form_t { USE_ISOL, USE_INIT, USE_MEDI, USE_FINA, _USE_NONE };
66 static const hb_tag_t use_other_features[] = {
67 /*
68 * Other features.
69 * These features are applied all at once, after reordering and
70 * clearing syllables.
71 */
72 HB_TAG('a', 'b', 'v', 's'),
73 HB_TAG('b', 'l', 'w', 's'),
74 HB_TAG('h', 'a', 'l', 'n'),
75 HB_TAG('p', 'r', 'e', 's'),
76 HB_TAG('p', 's', 't', 's'),
77 };
78
79 static void setup_syllables_use(const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer);
80 static void record_rphf_use(const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer);
81 static void record_pref_use(const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer);
82 static void reorder_use(const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer);
83
collect_features_use(hb_ot_shape_planner_t * plan)84 static void collect_features_use(hb_ot_shape_planner_t *plan)
85 {
86 hb_ot_map_builder_t *map = &plan->map;
87
88 /* Do this before any lookups have been applied. */
89 map->add_gsub_pause(setup_syllables_use);
90
91 /* "Default glyph pre-processing group" */
92 map->enable_feature(HB_TAG('l', 'o', 'c', 'l'));
93 map->enable_feature(HB_TAG('c', 'c', 'm', 'p'));
94 map->enable_feature(HB_TAG('n', 'u', 'k', 't'));
95 map->enable_feature(HB_TAG('a', 'k', 'h', 'n'), F_MANUAL_ZWJ);
96
97 /* "Reordering group" */
98 map->add_gsub_pause(_hb_clear_substitution_flags);
99 map->add_feature(HB_TAG('r', 'p', 'h', 'f'), F_MANUAL_ZWJ);
100 map->add_gsub_pause(record_rphf_use);
101 map->add_gsub_pause(_hb_clear_substitution_flags);
102 map->enable_feature(HB_TAG('p', 'r', 'e', 'f'), F_MANUAL_ZWJ);
103 map->add_gsub_pause(record_pref_use);
104
105 /* "Orthographic unit shaping group" */
106 for (unsigned int i = 0; i < ARRAY_LENGTH(use_basic_features); i++)
107 map->enable_feature(use_basic_features[i], F_MANUAL_ZWJ);
108
109 map->add_gsub_pause(reorder_use);
110 map->add_gsub_pause(_hb_clear_syllables);
111
112 /* "Topographical features" */
113 for (unsigned int i = 0; i < ARRAY_LENGTH(use_topographical_features); i++)
114 map->add_feature(use_topographical_features[i]);
115 map->add_gsub_pause(nullptr);
116
117 /* "Standard typographic presentation" */
118 for (unsigned int i = 0; i < ARRAY_LENGTH(use_other_features); i++)
119 map->enable_feature(use_other_features[i], F_MANUAL_ZWJ);
120 }
121
122 struct use_shape_plan_t
123 {
124 hb_mask_t rphf_mask;
125
126 arabic_shape_plan_t *arabic_plan;
127 };
128
has_arabic_joining(hb_script_t script)129 static bool has_arabic_joining(hb_script_t script)
130 {
131 /* List of scripts that have data in arabic-table. */
132 switch ((int)script) {
133 /* Unicode-1.1 additions */
134 case HB_SCRIPT_ARABIC:
135
136 /* Unicode-3.0 additions */
137 case HB_SCRIPT_MONGOLIAN:
138 case HB_SCRIPT_SYRIAC:
139
140 /* Unicode-5.0 additions */
141 case HB_SCRIPT_NKO:
142 case HB_SCRIPT_PHAGS_PA:
143
144 /* Unicode-6.0 additions */
145 case HB_SCRIPT_MANDAIC:
146
147 /* Unicode-7.0 additions */
148 case HB_SCRIPT_MANICHAEAN:
149 case HB_SCRIPT_PSALTER_PAHLAVI:
150
151 /* Unicode-9.0 additions */
152 case HB_SCRIPT_ADLAM:
153
154 return true;
155
156 default:
157 return false;
158 }
159 }
160
data_create_use(const hb_ot_shape_plan_t * plan)161 static void *data_create_use(const hb_ot_shape_plan_t *plan)
162 {
163 use_shape_plan_t *use_plan = (use_shape_plan_t *)calloc(1, sizeof(use_shape_plan_t));
164 if (unlikely(!use_plan))
165 return nullptr;
166
167 use_plan->rphf_mask = plan->map.get_1_mask(HB_TAG('r', 'p', 'h', 'f'));
168
169 if (has_arabic_joining(plan->props.script)) {
170 use_plan->arabic_plan = (arabic_shape_plan_t *)data_create_arabic(plan);
171 if (unlikely(!use_plan->arabic_plan)) {
172 free(use_plan);
173 return nullptr;
174 }
175 }
176
177 return use_plan;
178 }
179
data_destroy_use(void * data)180 static void data_destroy_use(void *data)
181 {
182 use_shape_plan_t *use_plan = (use_shape_plan_t *)data;
183
184 if (use_plan->arabic_plan)
185 data_destroy_arabic(use_plan->arabic_plan);
186
187 free(data);
188 }
189
190 enum use_syllable_type_t {
191 use_independent_cluster,
192 use_virama_terminated_cluster,
193 use_sakot_terminated_cluster,
194 use_standard_cluster,
195 use_number_joiner_terminated_cluster,
196 use_numeral_cluster,
197 use_symbol_cluster,
198 use_broken_cluster,
199 use_non_cluster,
200 };
201
202 #include "hb-ot-shape-complex-use-machine.hh"
203
setup_masks_use(const hb_ot_shape_plan_t * plan,hb_buffer_t * buffer,hb_font_t * font HB_UNUSED)204 static void setup_masks_use(const hb_ot_shape_plan_t *plan, hb_buffer_t *buffer, hb_font_t *font HB_UNUSED)
205 {
206 const use_shape_plan_t *use_plan = (const use_shape_plan_t *)plan->data;
207
208 /* Do this before allocating use_category(). */
209 if (use_plan->arabic_plan) {
210 setup_masks_arabic_plan(use_plan->arabic_plan, buffer, plan->props.script);
211 }
212
213 HB_BUFFER_ALLOCATE_VAR(buffer, use_category);
214
215 /* We cannot setup masks here. We save information about characters
216 * and setup masks later on in a pause-callback. */
217
218 unsigned int count = buffer->len;
219 hb_glyph_info_t *info = buffer->info;
220 for (unsigned int i = 0; i < count; i++)
221 info[i].use_category() = hb_use_get_category(info[i].codepoint);
222 }
223
setup_rphf_mask(const hb_ot_shape_plan_t * plan,hb_buffer_t * buffer)224 static void setup_rphf_mask(const hb_ot_shape_plan_t *plan, hb_buffer_t *buffer)
225 {
226 const use_shape_plan_t *use_plan = (const use_shape_plan_t *)plan->data;
227
228 hb_mask_t mask = use_plan->rphf_mask;
229 if (!mask)
230 return;
231
232 hb_glyph_info_t *info = buffer->info;
233
234 foreach_syllable(buffer, start, end)
235 {
236 unsigned int limit = info[start].use_category() == USE_R ? 1 : hb_min(3u, end - start);
237 for (unsigned int i = start; i < start + limit; i++)
238 info[i].mask |= mask;
239 }
240 }
241
setup_topographical_masks(const hb_ot_shape_plan_t * plan,hb_buffer_t * buffer)242 static void setup_topographical_masks(const hb_ot_shape_plan_t *plan, hb_buffer_t *buffer)
243 {
244 const use_shape_plan_t *use_plan = (const use_shape_plan_t *)plan->data;
245 if (use_plan->arabic_plan)
246 return;
247
248 static_assert((USE_INIT < 4 && USE_ISOL < 4 && USE_MEDI < 4 && USE_FINA < 4), "");
249 hb_mask_t masks[4], all_masks = 0;
250 for (unsigned int i = 0; i < 4; i++) {
251 masks[i] = plan->map.get_1_mask(use_topographical_features[i]);
252 if (masks[i] == plan->map.get_global_mask())
253 masks[i] = 0;
254 all_masks |= masks[i];
255 }
256 if (!all_masks)
257 return;
258 hb_mask_t other_masks = ~all_masks;
259
260 unsigned int last_start = 0;
261 joining_form_t last_form = _USE_NONE;
262 hb_glyph_info_t *info = buffer->info;
263 foreach_syllable(buffer, start, end)
264 {
265 use_syllable_type_t syllable_type = (use_syllable_type_t)(info[start].syllable() & 0x0F);
266 switch (syllable_type) {
267 case use_independent_cluster:
268 case use_symbol_cluster:
269 case use_non_cluster:
270 /* These don't join. Nothing to do. */
271 last_form = _USE_NONE;
272 break;
273
274 case use_virama_terminated_cluster:
275 case use_sakot_terminated_cluster:
276 case use_standard_cluster:
277 case use_number_joiner_terminated_cluster:
278 case use_numeral_cluster:
279 case use_broken_cluster:
280
281 bool join = last_form == USE_FINA || last_form == USE_ISOL;
282
283 if (join) {
284 /* Fixup previous syllable's form. */
285 last_form = last_form == USE_FINA ? USE_MEDI : USE_INIT;
286 for (unsigned int i = last_start; i < start; i++)
287 info[i].mask = (info[i].mask & other_masks) | masks[last_form];
288 }
289
290 /* Form for this syllable. */
291 last_form = join ? USE_FINA : USE_ISOL;
292 for (unsigned int i = start; i < end; i++)
293 info[i].mask = (info[i].mask & other_masks) | masks[last_form];
294
295 break;
296 }
297
298 last_start = start;
299 }
300 }
301
setup_syllables_use(const hb_ot_shape_plan_t * plan,hb_font_t * font HB_UNUSED,hb_buffer_t * buffer)302 static void setup_syllables_use(const hb_ot_shape_plan_t *plan, hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
303 {
304 find_syllables_use(buffer);
305 foreach_syllable(buffer, start, end) buffer->unsafe_to_break(start, end);
306 setup_rphf_mask(plan, buffer);
307 setup_topographical_masks(plan, buffer);
308 }
309
record_rphf_use(const hb_ot_shape_plan_t * plan,hb_font_t * font HB_UNUSED,hb_buffer_t * buffer)310 static void record_rphf_use(const hb_ot_shape_plan_t *plan, hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
311 {
312 const use_shape_plan_t *use_plan = (const use_shape_plan_t *)plan->data;
313
314 hb_mask_t mask = use_plan->rphf_mask;
315 if (!mask)
316 return;
317 hb_glyph_info_t *info = buffer->info;
318
319 foreach_syllable(buffer, start, end)
320 {
321 /* Mark a substituted repha as USE_R. */
322 for (unsigned int i = start; i < end && (info[i].mask & mask); i++)
323 if (_hb_glyph_info_substituted(&info[i])) {
324 info[i].use_category() = USE_R;
325 break;
326 }
327 }
328 }
329
record_pref_use(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_font_t * font HB_UNUSED,hb_buffer_t * buffer)330 static void record_pref_use(const hb_ot_shape_plan_t *plan HB_UNUSED, hb_font_t *font HB_UNUSED, hb_buffer_t *buffer)
331 {
332 hb_glyph_info_t *info = buffer->info;
333
334 foreach_syllable(buffer, start, end)
335 {
336 /* Mark a substituted pref as VPre, as they behave the same way. */
337 for (unsigned int i = start; i < end; i++)
338 if (_hb_glyph_info_substituted(&info[i])) {
339 info[i].use_category() = USE_VPre;
340 break;
341 }
342 }
343 }
344
is_halant_use(const hb_glyph_info_t & info)345 static inline bool is_halant_use(const hb_glyph_info_t &info)
346 {
347 return (info.use_category() == USE_H || info.use_category() == USE_HVM) && !_hb_glyph_info_ligated(&info);
348 }
349
reorder_syllable_use(hb_buffer_t * buffer,unsigned int start,unsigned int end)350 static void reorder_syllable_use(hb_buffer_t *buffer, unsigned int start, unsigned int end)
351 {
352 use_syllable_type_t syllable_type = (use_syllable_type_t)(buffer->info[start].syllable() & 0x0F);
353 /* Only a few syllable types need reordering. */
354 if (unlikely(
355 !(FLAG_UNSAFE(syllable_type) & (FLAG(use_virama_terminated_cluster) | FLAG(use_sakot_terminated_cluster) |
356 FLAG(use_standard_cluster) | FLAG(use_broken_cluster) | 0))))
357 return;
358
359 hb_glyph_info_t *info = buffer->info;
360
361 #define POST_BASE_FLAGS64 \
362 (FLAG64(USE_FM) | FLAG64(USE_FAbv) | FLAG64(USE_FBlw) | FLAG64(USE_FPst) | FLAG64(USE_MAbv) | FLAG64(USE_MBlw) | \
363 FLAG64(USE_MPst) | FLAG64(USE_MPre) | FLAG64(USE_VAbv) | FLAG64(USE_VBlw) | FLAG64(USE_VPst) | FLAG64(USE_VPre) | \
364 FLAG64(USE_VMAbv) | FLAG64(USE_VMBlw) | FLAG64(USE_VMPst) | FLAG64(USE_VMPre))
365
366 /* Move things forward. */
367 if (info[start].use_category() == USE_R && end - start > 1) {
368 /* Got a repha. Reorder it towards the end, but before the first post-base
369 * glyph. */
370 for (unsigned int i = start + 1; i < end; i++) {
371 bool is_post_base_glyph =
372 (FLAG64_UNSAFE(info[i].use_category()) & POST_BASE_FLAGS64) || is_halant_use(info[i]);
373 if (is_post_base_glyph || i == end - 1) {
374 /* If we hit a post-base glyph, move before it; otherwise move to the
375 * end. Shift things in between backward. */
376
377 if (is_post_base_glyph)
378 i--;
379
380 buffer->merge_clusters(start, i + 1);
381 hb_glyph_info_t t = info[start];
382 memmove(&info[start], &info[start + 1], (i - start) * sizeof(info[0]));
383 info[i] = t;
384
385 break;
386 }
387 }
388 }
389
390 /* Move things back. */
391 unsigned int j = start;
392 for (unsigned int i = start; i < end; i++) {
393 uint32_t flag = FLAG_UNSAFE(info[i].use_category());
394 if (is_halant_use(info[i])) {
395 /* If we hit a halant, move after it; otherwise move to the beginning, and
396 * shift things in between forward. */
397 j = i + 1;
398 } else if (((flag) & (FLAG(USE_VPre) | FLAG(USE_VMPre))) &&
399 /* Only move the first component of a MultipleSubst. */
400 0 == _hb_glyph_info_get_lig_comp(&info[i]) && j < i) {
401 buffer->merge_clusters(j, i + 1);
402 hb_glyph_info_t t = info[i];
403 memmove(&info[j + 1], &info[j], (i - j) * sizeof(info[0]));
404 info[j] = t;
405 }
406 }
407 }
408
409 static inline void
insert_dotted_circles_use(const hb_ot_shape_plan_t * plan HB_UNUSED,hb_font_t * font,hb_buffer_t * buffer)410 insert_dotted_circles_use(const hb_ot_shape_plan_t *plan HB_UNUSED, hb_font_t *font, hb_buffer_t *buffer)
411 {
412 if (unlikely(buffer->flags & HB_BUFFER_FLAG_DO_NOT_INSERT_DOTTED_CIRCLE))
413 return;
414
415 /* Note: This loop is extra overhead, but should not be measurable.
416 * TODO Use a buffer scratch flag to remove the loop. */
417 bool has_broken_syllables = false;
418 unsigned int count = buffer->len;
419 hb_glyph_info_t *info = buffer->info;
420 for (unsigned int i = 0; i < count; i++)
421 if ((info[i].syllable() & 0x0F) == use_broken_cluster) {
422 has_broken_syllables = true;
423 break;
424 }
425 if (likely(!has_broken_syllables))
426 return;
427
428 hb_glyph_info_t dottedcircle = {0};
429 if (!font->get_nominal_glyph(0x25CCu, &dottedcircle.codepoint))
430 return;
431 dottedcircle.use_category() = hb_use_get_category(0x25CC);
432
433 buffer->clear_output();
434
435 buffer->idx = 0;
436 unsigned int last_syllable = 0;
437 while (buffer->idx < buffer->len && buffer->successful) {
438 unsigned int syllable = buffer->cur().syllable();
439 use_syllable_type_t syllable_type = (use_syllable_type_t)(syllable & 0x0F);
440 if (unlikely(last_syllable != syllable && syllable_type == use_broken_cluster)) {
441 last_syllable = syllable;
442
443 hb_glyph_info_t ginfo = dottedcircle;
444 ginfo.cluster = buffer->cur().cluster;
445 ginfo.mask = buffer->cur().mask;
446 ginfo.syllable() = buffer->cur().syllable();
447
448 /* Insert dottedcircle after possible Repha. */
449 while (buffer->idx < buffer->len && buffer->successful && last_syllable == buffer->cur().syllable() &&
450 buffer->cur().use_category() == USE_R)
451 buffer->next_glyph();
452
453 buffer->output_info(ginfo);
454 } else
455 buffer->next_glyph();
456 }
457 buffer->swap_buffers();
458 }
459
reorder_use(const hb_ot_shape_plan_t * plan,hb_font_t * font,hb_buffer_t * buffer)460 static void reorder_use(const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer)
461 {
462 insert_dotted_circles_use(plan, font, buffer);
463
464 foreach_syllable(buffer, start, end) reorder_syllable_use(buffer, start, end);
465
466 HB_BUFFER_DEALLOCATE_VAR(buffer, use_category);
467 }
468
preprocess_text_use(const hb_ot_shape_plan_t * plan,hb_buffer_t * buffer,hb_font_t * font)469 static void preprocess_text_use(const hb_ot_shape_plan_t *plan, hb_buffer_t *buffer, hb_font_t *font)
470 {
471 _hb_preprocess_text_vowel_constraints(plan, buffer, font);
472 }
473
474 static bool
compose_use(const hb_ot_shape_normalize_context_t * c,hb_codepoint_t a,hb_codepoint_t b,hb_codepoint_t * ab)475 compose_use(const hb_ot_shape_normalize_context_t *c, hb_codepoint_t a, hb_codepoint_t b, hb_codepoint_t *ab)
476 {
477 /* Avoid recomposing split matras. */
478 if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK(c->unicode->general_category(a)))
479 return false;
480
481 return (bool)c->unicode->compose(a, b, ab);
482 }
483
484 const hb_ot_complex_shaper_t _hb_ot_complex_shaper_use = {
485 collect_features_use,
486 nullptr, /* override_features */
487 data_create_use,
488 data_destroy_use,
489 preprocess_text_use,
490 nullptr, /* postprocess_glyphs */
491 HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT,
492 nullptr, /* decompose */
493 compose_use,
494 setup_masks_use,
495 HB_TAG_NONE, /* gpos_tag */
496 nullptr, /* reorder_marks */
497 HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY,
498 false, /* fallback_position */
499 };
500
501 #endif
502