Coverage Report

Created: 2023-12-13 20:01

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