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