Coverage Report

Created: 2023-05-11 17:16

/src/harfbuzz/src/hb-ot-shaper-myanmar.cc
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright © 2011,2012,2013  Google, Inc.
3
 *
4
 *  This is part of HarfBuzz, a text shaping library.
5
 *
6
 * Permission is hereby granted, without written agreement and without
7
 * license or royalty fees, to use, copy, modify, and distribute this
8
 * software and its documentation for any purpose, provided that the
9
 * above copyright notice and the following two paragraphs appear in
10
 * all copies of this software.
11
 *
12
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16
 * DAMAGE.
17
 *
18
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23
 *
24
 * Google Author(s): Behdad Esfahbod
25
 */
26
27
#include "hb.hh"
28
29
#ifndef HB_NO_OT_SHAPE
30
31
#include "hb-ot-shaper-myanmar-machine.hh"
32
#include "hb-ot-shaper-indic.hh"
33
#include "hb-ot-layout.hh"
34
35
36
/*
37
 * Myanmar shaper.
38
 */
39
40
41
static const hb_tag_t
42
myanmar_basic_features[] =
43
{
44
  /*
45
   * Basic features.
46
   * These features are applied in order, one at a time, after reordering,
47
   * constrained to the syllable.
48
   */
49
  HB_TAG('r','p','h','f'),
50
  HB_TAG('p','r','e','f'),
51
  HB_TAG('b','l','w','f'),
52
  HB_TAG('p','s','t','f'),
53
};
54
static const hb_tag_t
55
myanmar_other_features[] =
56
{
57
  /*
58
   * Other features.
59
   * These features are applied all at once, after clearing syllables.
60
   */
61
  HB_TAG('p','r','e','s'),
62
  HB_TAG('a','b','v','s'),
63
  HB_TAG('b','l','w','s'),
64
  HB_TAG('p','s','t','s'),
65
};
66
67
static inline void
68
set_myanmar_properties (hb_glyph_info_t &info)
69
99.7k
{
70
99.7k
  hb_codepoint_t u = info.codepoint;
71
99.7k
  unsigned int type = hb_indic_get_categories (u);
72
73
99.7k
  info.myanmar_category() = (myanmar_category_t) (type & 0xFFu);
74
99.7k
}
75
76
77
static inline bool
78
is_one_of_myanmar (const hb_glyph_info_t &info, unsigned int flags)
79
170k
{
80
  /* If it ligated, all bets are off. */
81
170k
  if (_hb_glyph_info_ligated (&info)) return false;
82
170k
  return !!(FLAG_UNSAFE (info.myanmar_category()) & flags);
83
170k
}
84
85
/* Note:
86
 *
87
 * We treat Vowels and placeholders as if they were consonants.  This is safe because Vowels
88
 * cannot happen in a consonant syllable.  The plus side however is, we can call the
89
 * consonant syllable logic from the vowel syllable function and get it all right!
90
 *
91
 * Keep in sync with consonant_categories in the generator. */
92
170k
#define CONSONANT_FLAGS_MYANMAR (FLAG (M_Cat(C)) | FLAG (M_Cat(CS)) | FLAG (M_Cat(Ra)) | /* FLAG (M_Cat(CM)) | */ FLAG (M_Cat(IV)) | FLAG (M_Cat(GB)) | FLAG (M_Cat(DOTTEDCIRCLE)))
93
94
static inline bool
95
is_consonant_myanmar (const hb_glyph_info_t &info)
96
170k
{
97
170k
  return is_one_of_myanmar (info, CONSONANT_FLAGS_MYANMAR);
98
170k
}
99
100
101
static void
102
setup_syllables_myanmar (const hb_ot_shape_plan_t *plan,
103
       hb_font_t *font,
104
       hb_buffer_t *buffer);
105
static void
106
reorder_myanmar (const hb_ot_shape_plan_t *plan,
107
     hb_font_t *font,
108
     hb_buffer_t *buffer);
109
110
static void
111
collect_features_myanmar (hb_ot_shape_planner_t *plan)
112
6.22k
{
113
6.22k
  hb_ot_map_builder_t *map = &plan->map;
114
115
  /* Do this before any lookups have been applied. */
116
6.22k
  map->add_gsub_pause (setup_syllables_myanmar);
117
118
6.22k
  map->enable_feature (HB_TAG('l','o','c','l'), F_PER_SYLLABLE);
119
  /* The Indic specs do not require ccmp, but we apply it here since if
120
   * there is a use of it, it's typically at the beginning. */
121
6.22k
  map->enable_feature (HB_TAG('c','c','m','p'), F_PER_SYLLABLE);
122
123
124
6.22k
  map->add_gsub_pause (reorder_myanmar);
125
126
31.1k
  for (unsigned int i = 0; i < ARRAY_LENGTH (myanmar_basic_features); i++)
127
24.9k
  {
128
24.9k
    map->enable_feature (myanmar_basic_features[i], F_MANUAL_ZWJ | F_PER_SYLLABLE);
129
24.9k
    map->add_gsub_pause (nullptr);
130
24.9k
  }
131
6.22k
  map->add_gsub_pause (hb_syllabic_clear_var); // Don't need syllables anymore, use stop to free buffer var
132
133
31.1k
  for (unsigned int i = 0; i < ARRAY_LENGTH (myanmar_other_features); i++)
134
24.9k
    map->enable_feature (myanmar_other_features[i], F_MANUAL_ZWJ);
135
6.22k
}
136
137
static void
138
setup_masks_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
139
         hb_buffer_t              *buffer,
140
         hb_font_t                *font HB_UNUSED)
141
6.22k
{
142
6.22k
  HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_category);
143
6.22k
  HB_BUFFER_ALLOCATE_VAR (buffer, myanmar_position);
144
145
  /* No masks, we just save information about characters. */
146
147
6.22k
  unsigned int count = buffer->len;
148
6.22k
  hb_glyph_info_t *info = buffer->info;
149
106k
  for (unsigned int i = 0; i < count; i++)
150
99.7k
    set_myanmar_properties (info[i]);
151
6.22k
}
152
153
static void
154
setup_syllables_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
155
       hb_font_t *font HB_UNUSED,
156
       hb_buffer_t *buffer)
157
6.05k
{
158
6.05k
  HB_BUFFER_ALLOCATE_VAR (buffer, syllable);
159
6.05k
  find_syllables_myanmar (buffer);
160
6.05k
  foreach_syllable (buffer, start, end)
161
1.52M
    buffer->unsafe_to_break (start, end);
162
6.05k
}
163
164
static int
165
compare_myanmar_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
166
58.3k
{
167
58.3k
  int a = pa->myanmar_position();
168
58.3k
  int b = pb->myanmar_position();
169
170
58.3k
  return (int) a - (int) b;
171
58.3k
}
172
173
174
/* Rules from:
175
 * https://docs.microsoft.com/en-us/typography/script-development/myanmar */
176
177
static void
178
initial_reordering_consonant_syllable (hb_buffer_t *buffer,
179
               unsigned int start, unsigned int end)
180
126k
{
181
126k
  hb_glyph_info_t *info = buffer->info;
182
183
126k
  unsigned int base = end;
184
126k
  bool has_reph = false;
185
186
126k
  {
187
126k
    unsigned int limit = start;
188
126k
    if (start + 3 <= end &&
189
126k
  info[start  ].myanmar_category() == M_Cat(Ra) &&
190
126k
  info[start+1].myanmar_category() == M_Cat(As) &&
191
126k
  info[start+2].myanmar_category() == M_Cat(H))
192
0
    {
193
0
      limit += 3;
194
0
      base = start;
195
0
      has_reph = true;
196
0
    }
197
198
126k
    {
199
126k
      if (!has_reph)
200
126k
  base = limit;
201
202
191k
      for (unsigned int i = limit; i < end; i++)
203
170k
  if (is_consonant_myanmar (info[i]))
204
106k
  {
205
106k
    base = i;
206
106k
    break;
207
106k
  }
208
126k
    }
209
126k
  }
210
211
  /* Reorder! */
212
126k
  {
213
126k
    unsigned int i = start;
214
126k
    for (; i < start + (has_reph ? 3 : 0); i++)
215
0
      info[i].myanmar_position() = POS_AFTER_MAIN;
216
126k
    for (; i < base; i++)
217
93
      info[i].myanmar_position() = POS_PRE_C;
218
126k
    if (i < end)
219
126k
    {
220
126k
      info[i].myanmar_position() = POS_BASE_C;
221
126k
      i++;
222
126k
    }
223
126k
    myanmar_position_t pos = POS_AFTER_MAIN;
224
    /* The following loop may be ugly, but it implements all of
225
     * Myanmar reordering! */
226
180k
    for (; i < end; i++)
227
53.3k
    {
228
53.3k
      if (info[i].myanmar_category() == M_Cat(MR)) /* Pre-base reordering */
229
56
      {
230
56
  info[i].myanmar_position() = POS_PRE_C;
231
56
  continue;
232
56
      }
233
53.2k
      if (info[i].myanmar_category() == M_Cat(VPre)) /* Left matra */
234
5.04k
      {
235
5.04k
  info[i].myanmar_position() = POS_PRE_M;
236
5.04k
  continue;
237
5.04k
      }
238
48.2k
      if (info[i].myanmar_category() == M_Cat(VS))
239
149
      {
240
149
  info[i].myanmar_position() = info[i - 1].myanmar_position();
241
149
  continue;
242
149
      }
243
244
48.0k
      if (pos == POS_AFTER_MAIN && info[i].myanmar_category() == M_Cat(VBlw))
245
228
      {
246
228
  pos = POS_BELOW_C;
247
228
  info[i].myanmar_position() = pos;
248
228
  continue;
249
228
      }
250
251
47.8k
      if (pos == POS_BELOW_C && info[i].myanmar_category() == M_Cat(A))
252
0
      {
253
0
  info[i].myanmar_position() = POS_BEFORE_SUB;
254
0
  continue;
255
0
      }
256
47.8k
      if (pos == POS_BELOW_C && info[i].myanmar_category() == M_Cat(VBlw))
257
9.33k
      {
258
9.33k
  info[i].myanmar_position() = pos;
259
9.33k
  continue;
260
9.33k
      }
261
38.5k
      if (pos == POS_BELOW_C && info[i].myanmar_category() != M_Cat(A))
262
75
      {
263
75
  pos = POS_AFTER_SUB;
264
75
  info[i].myanmar_position() = pos;
265
75
  continue;
266
75
      }
267
38.4k
      info[i].myanmar_position() = pos;
268
38.4k
    }
269
126k
  }
270
271
  /* Sit tight, rock 'n roll! */
272
126k
  buffer->sort (start, end, compare_myanmar_order);
273
126k
}
274
275
static void
276
reorder_syllable_myanmar (const hb_ot_shape_plan_t *plan HB_UNUSED,
277
        hb_face_t *face HB_UNUSED,
278
        hb_buffer_t *buffer,
279
        unsigned int start, unsigned int end)
280
1.52M
{
281
1.52M
  myanmar_syllable_type_t syllable_type = (myanmar_syllable_type_t) (buffer->info[start].syllable() & 0x0F);
282
1.52M
  switch (syllable_type) {
283
284
20.7k
    case myanmar_broken_cluster: /* We already inserted dotted-circles, so just call the consonant_syllable. */
285
126k
    case myanmar_consonant_syllable:
286
126k
      initial_reordering_consonant_syllable  (buffer, start, end);
287
126k
      break;
288
289
1.40M
    case myanmar_non_myanmar_cluster:
290
1.40M
      break;
291
1.52M
  }
292
1.52M
}
293
294
static void
295
reorder_myanmar (const hb_ot_shape_plan_t *plan,
296
     hb_font_t *font,
297
     hb_buffer_t *buffer)
298
6.05k
{
299
6.05k
  if (buffer->message (font, "start reordering myanmar"))
300
6.05k
  {
301
6.05k
    hb_syllabic_insert_dotted_circles (font, buffer,
302
6.05k
               myanmar_broken_cluster,
303
6.05k
               M_Cat(DOTTEDCIRCLE));
304
305
6.05k
    foreach_syllable (buffer, start, end)
306
1.52M
      reorder_syllable_myanmar (plan, font->face, buffer, start, end);
307
6.05k
    (void) buffer->message (font, "end reordering myanmar");
308
6.05k
  }
309
310
6.05k
  HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_category);
311
6.05k
  HB_BUFFER_DEALLOCATE_VAR (buffer, myanmar_position);
312
6.05k
}
313
314
315
const hb_ot_shaper_t _hb_ot_shaper_myanmar =
316
{
317
  collect_features_myanmar,
318
  nullptr, /* override_features */
319
  nullptr, /* data_create */
320
  nullptr, /* data_destroy */
321
  nullptr, /* preprocess_text */
322
  nullptr, /* postprocess_glyphs */
323
  nullptr, /* decompose */
324
  nullptr, /* compose */
325
  setup_masks_myanmar,
326
  nullptr, /* reorder_marks */
327
  HB_TAG_NONE, /* gpos_tag */
328
  HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT,
329
  HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY,
330
  false, /* fallback_position */
331
};
332
333
334
/* Ugly Zawgyi encoding.
335
 * Disable all auto processing.
336
 * https://github.com/harfbuzz/harfbuzz/issues/1162 */
337
const hb_ot_shaper_t _hb_ot_shaper_myanmar_zawgyi =
338
{
339
  nullptr, /* collect_features */
340
  nullptr, /* override_features */
341
  nullptr, /* data_create */
342
  nullptr, /* data_destroy */
343
  nullptr, /* preprocess_text */
344
  nullptr, /* postprocess_glyphs */
345
  nullptr, /* decompose */
346
  nullptr, /* compose */
347
  nullptr, /* setup_masks */
348
  nullptr, /* reorder_marks */
349
  HB_TAG_NONE, /* gpos_tag */
350
  HB_OT_SHAPE_NORMALIZATION_MODE_NONE,
351
  HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
352
  false, /* fallback_position */
353
};
354
355
356
#endif