Coverage Report

Created: 2026-06-09 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/harfbuzz/src/hb-ot-shaper-hangul.cc
Line
Count
Source
1
/*
2
 * Copyright © 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.hh"
32
33
34
/* Hangul shaper */
35
36
37
/* Same order as the feature array below */
38
enum {
39
  _JMO,
40
41
  LJMO,
42
  VJMO,
43
  TJMO,
44
45
  FIRST_HANGUL_FEATURE = LJMO,
46
  HANGUL_FEATURE_COUNT = TJMO + 1
47
};
48
49
static const hb_tag_t hangul_features[HANGUL_FEATURE_COUNT] =
50
{
51
  HB_TAG_NONE,
52
  HB_TAG('l','j','m','o'),
53
  HB_TAG('v','j','m','o'),
54
  HB_TAG('t','j','m','o')
55
};
56
57
static void
58
collect_features_hangul (hb_ot_shape_planner_t *plan)
59
818
{
60
818
  hb_ot_map_builder_t *map = &plan->map;
61
62
3.27k
  for (unsigned int i = FIRST_HANGUL_FEATURE; i < HANGUL_FEATURE_COUNT; i++)
63
2.45k
    map->add_feature (hangul_features[i]);
64
818
}
65
66
static void
67
override_features_hangul (hb_ot_shape_planner_t *plan)
68
818
{
69
  /* Uniscribe does not apply 'calt' for Hangul, and certain fonts
70
   * (Noto Sans CJK, Source Sans Han, etc) apply all of jamo lookups
71
   * in calt, which is not desirable. */
72
818
  plan->map.disable_feature (HB_TAG('c','a','l','t'));
73
818
}
74
75
struct hangul_shape_plan_t
76
{
77
  hb_mask_t mask_array[HANGUL_FEATURE_COUNT];
78
};
79
80
static void *
81
data_create_hangul (const hb_ot_shape_plan_t *plan)
82
818
{
83
818
  hangul_shape_plan_t *hangul_plan = (hangul_shape_plan_t *) hb_calloc (1, sizeof (hangul_shape_plan_t));
84
818
  if (unlikely (!hangul_plan))
85
3
    return nullptr;
86
87
4.07k
  for (unsigned int i = 0; i < HANGUL_FEATURE_COUNT; i++)
88
3.26k
    hangul_plan->mask_array[i] = plan->map.get_1_mask (hangul_features[i]);
89
90
815
  return hangul_plan;
91
818
}
92
93
static void
94
data_destroy_hangul (void *data)
95
815
{
96
815
  hb_free (data);
97
815
}
98
99
/* Constants for algorithmic hangul syllable [de]composition. */
100
1.19k
#define LBase 0x1100u
101
1.09k
#define VBase 0x1161u
102
1.42k
#define TBase 0x11A7u
103
12.4k
#define LCount 19u
104
14.4k
#define VCount 21u
105
16.6k
#define TCount 28u
106
26.7k
#define SBase 0xAC00u
107
14.3k
#define NCount (VCount * TCount)
108
12.3k
#define SCount (LCount * NCount)
109
110
264
#define isCombiningL(u) (hb_in_range<hb_codepoint_t> ((u), LBase, LBase+LCount-1))
111
216
#define isCombiningV(u) (hb_in_range<hb_codepoint_t> ((u), VBase, VBase+VCount-1))
112
243
#define isCombiningT(u) (hb_in_range<hb_codepoint_t> ((u), TBase+1, TBase+TCount-1))
113
12.3k
#define isCombinedS(u) (hb_in_range<hb_codepoint_t> ((u), SBase, SBase+SCount-1))
114
115
25.2k
#define isL(u) (hb_in_ranges<hb_codepoint_t> ((u), 0x1100u, 0x115Fu, 0xA960u, 0xA97Cu))
116
248
#define isV(u) (hb_in_ranges<hb_codepoint_t> ((u), 0x1160u, 0x11A7u, 0xD7B0u, 0xD7C6u))
117
302
#define isT(u) (hb_in_ranges<hb_codepoint_t> ((u), 0x11A8u, 0x11FFu, 0xD7CBu, 0xD7FBu))
118
119
12.8k
#define isHangulTone(u) (hb_in_range<hb_codepoint_t> ((u), 0x302Eu, 0x302Fu))
120
121
/* buffer var allocations */
122
14.1k
#define hangul_shaping_feature() ot_shaper_var_u8_auxiliary() /* hangul jamo shaping feature */
123
124
static bool
125
is_zero_width_char (hb_font_t *font,
126
        hb_codepoint_t unicode)
127
129
{
128
129
  hb_codepoint_t glyph;
129
129
  return hb_font_get_glyph (font, unicode, 0, &glyph) && hb_font_get_glyph_h_advance (font, glyph) == 0;
130
129
}
131
132
static void
133
preprocess_text_hangul (const hb_ot_shape_plan_t *plan HB_UNUSED,
134
      hb_buffer_t              *buffer,
135
      hb_font_t                *font)
136
815
{
137
815
  HB_BUFFER_ALLOCATE_VAR (buffer, hangul_shaping_feature);
138
139
  /* Hangul syllables come in two shapes: LV, and LVT.  Of those:
140
   *
141
   *   - LV can be precomposed, or decomposed.  Lets call those
142
   *     <LV> and <L,V>,
143
   *   - LVT can be fully precomposed, partially precomposed, or
144
   *     fully decomposed.  Ie. <LVT>, <LV,T>, or <L,V,T>.
145
   *
146
   * The composition / decomposition is mechanical.  However, not
147
   * all <L,V> sequences compose, and not all <LV,T> sequences
148
   * compose.
149
   *
150
   * Here are the specifics:
151
   *
152
   *   - <L>: U+1100..115F, U+A960..A97F
153
   *   - <V>: U+1160..11A7, U+D7B0..D7C7
154
   *   - <T>: U+11A8..11FF, U+D7CB..D7FB
155
   *
156
   *   - Only the <L,V> sequences for some of the U+11xx ranges combine.
157
   *   - Only <LV,T> sequences for some of the Ts in U+11xx range combine.
158
   *
159
   * Here is what we want to accomplish in this shaper:
160
   *
161
   *   - If the whole syllable can be precomposed, do that,
162
   *   - Otherwise, fully decompose and apply ljmo/vjmo/tjmo features.
163
   *   - If a valid syllable is followed by a Hangul tone mark, reorder the tone
164
   *     mark to precede the whole syllable - unless it is a zero-width glyph, in
165
   *     which case we leave it untouched, assuming it's designed to overstrike.
166
   *
167
   * That is, of the different possible syllables:
168
   *
169
   *   <L>
170
   *   <L,V>
171
   *   <L,V,T>
172
   *   <LV>
173
   *   <LVT>
174
   *   <LV, T>
175
   *
176
   * - <L> needs no work.
177
   *
178
   * - <LV> and <LVT> can stay the way they are if the font supports them, otherwise we
179
   *   should fully decompose them if font supports.
180
   *
181
   * - <L,V> and <L,V,T> we should compose if the whole thing can be composed.
182
   *
183
   * - <LV,T> we should compose if the whole thing can be composed, otherwise we should
184
   *   decompose.
185
   */
186
187
815
  buffer->clear_output ();
188
815
  unsigned int start = 0, end = 0; /* Extent of most recently seen syllable;
189
            * valid only if start < end
190
            */
191
815
  unsigned int count = buffer->len;
192
193
13.6k
  for (buffer->idx = 0; buffer->idx < count && buffer->successful;)
194
12.8k
  {
195
12.8k
    hb_codepoint_t u = buffer->cur().codepoint;
196
197
12.8k
    if (isHangulTone (u))
198
217
    {
199
      /*
200
       * We could cache the width of the tone marks and the existence of dotted-circle,
201
       * but the use of the Hangul tone mark characters seems to be rare enough that
202
       * I didn't bother for now.
203
       */
204
217
      if (start < end && end == buffer->out_len)
205
59
      {
206
  /* Tone mark follows a valid syllable; move it in front, unless it's zero width. */
207
59
  buffer->unsafe_to_break_from_outbuffer (start, buffer->idx);
208
59
  if (unlikely (!buffer->next_glyph ())) break;
209
58
  if (!is_zero_width_char (font, u))
210
48
  {
211
48
    buffer->merge_out_clusters (start, end + 1);
212
48
    hb_glyph_info_t *info = buffer->out_info;
213
48
    hb_glyph_info_t tone = info[end];
214
48
    memmove (&info[start + 1], &info[start], (end - start) * sizeof (hb_glyph_info_t));
215
48
    info[start] = tone;
216
48
  }
217
58
      }
218
158
      else
219
158
      {
220
  /* No valid syllable as base for tone mark; try to insert dotted circle. */
221
158
  if (!(buffer->flags & HB_BUFFER_FLAG_DO_NOT_INSERT_DOTTED_CIRCLE) &&
222
158
      font->has_glyph (0x25CCu))
223
71
  {
224
71
    hb_codepoint_t chars[2];
225
71
    if (!is_zero_width_char (font, u))
226
44
    {
227
44
      chars[0] = u;
228
44
      chars[1] = 0x25CCu;
229
44
    } else
230
27
    {
231
27
      chars[0] = 0x25CCu;
232
27
      chars[1] = u;
233
27
    }
234
71
    (void) buffer->replace_glyphs (1, 2, chars);
235
71
  }
236
87
  else
237
87
  {
238
    /* No dotted circle available in the font; just leave tone mark untouched. */
239
87
    (void) buffer->next_glyph ();
240
87
  }
241
158
      }
242
216
      start = end = buffer->out_len;
243
216
      continue;
244
217
    }
245
246
12.6k
    start = buffer->out_len; /* Remember current position as a potential syllable start;
247
            * will only be used if we set end to a later position.
248
            */
249
250
12.6k
    if (isL (u) && buffer->idx + 1 < count)
251
248
    {
252
248
      hb_codepoint_t l = u;
253
248
      hb_codepoint_t v = buffer->cur(+1).codepoint;
254
248
      if (isV (v))
255
132
      {
256
  /* Have <L,V> or <L,V,T>. */
257
132
  hb_codepoint_t t = 0;
258
132
  unsigned int tindex = 0;
259
132
  if (buffer->idx + 2 < count)
260
131
  {
261
131
    t = buffer->cur(+2).codepoint;
262
131
    if (isT (t))
263
31
      tindex = t - TBase; /* Only used if isCombiningT (t); otherwise invalid. */
264
100
    else
265
100
      t = 0; /* The next character was not a trailing jamo. */
266
131
  }
267
132
  buffer->unsafe_to_break (buffer->idx, buffer->idx + (t ? 3 : 2));
268
269
  /* We've got a syllable <L,V,T?>; see if it can potentially be composed. */
270
132
  if (isCombiningL (l) && isCombiningV (v) && (t == 0 || isCombiningT (t)))
271
62
  {
272
    /* Try to compose; if this succeeds, end is set to start+1. */
273
62
    hb_codepoint_t s = SBase + (l - LBase) * NCount + (v - VBase) * TCount + tindex;
274
62
    if (font->has_glyph (s))
275
11
    {
276
11
      (void) buffer->replace_glyphs (t ? 3 : 2, 1, &s);
277
11
      end = start + 1;
278
11
      continue;
279
11
    }
280
62
  }
281
282
  /* We didn't compose, either because it's an Old Hangul syllable without a
283
   * precomposed character in Unicode, or because the font didn't support the
284
   * necessary precomposed glyph.
285
   * Set jamo features on the individual glyphs, and advance past them.
286
   */
287
121
  buffer->cur().hangul_shaping_feature() = LJMO;
288
121
  (void) buffer->next_glyph ();
289
121
  buffer->cur().hangul_shaping_feature() = VJMO;
290
121
  (void) buffer->next_glyph ();
291
121
  if (t)
292
31
  {
293
31
    buffer->cur().hangul_shaping_feature() = TJMO;
294
31
    (void) buffer->next_glyph ();
295
31
    end = start + 3;
296
31
  }
297
90
  else
298
90
    end = start + 2;
299
121
  if (unlikely (!buffer->successful))
300
0
    break;
301
121
  buffer->merge_out_grapheme_clusters (start, end);
302
121
  continue;
303
121
      }
304
248
    }
305
306
12.3k
    else if (isCombinedS (u))
307
978
    {
308
      /* Have <LV>, <LVT>, or <LV,T> */
309
978
      hb_codepoint_t s = u;
310
978
      bool has_glyph = font->has_glyph (s);
311
978
      unsigned int lindex = (s - SBase) / NCount;
312
978
      unsigned int nindex = (s - SBase) % NCount;
313
978
      unsigned int vindex = nindex / TCount;
314
978
      unsigned int tindex = nindex % TCount;
315
316
978
      if (!tindex &&
317
240
    buffer->idx + 1 < count &&
318
227
    isCombiningT (buffer->cur(+1).codepoint))
319
40
      {
320
  /* <LV,T>, try to combine. */
321
40
  unsigned int new_tindex = buffer->cur(+1).codepoint - TBase;
322
40
  hb_codepoint_t new_s = s + new_tindex;
323
40
  if (font->has_glyph (new_s))
324
26
  {
325
26
    (void) buffer->replace_glyphs (2, 1, &new_s);
326
26
    end = start + 1;
327
26
    continue;
328
26
  }
329
14
  else
330
14
    buffer->unsafe_to_break (buffer->idx, buffer->idx + 2); /* Mark unsafe between LV and T. */
331
40
      }
332
333
      /* Otherwise, decompose if font doesn't support <LV> or <LVT>,
334
       * or if having non-combining <LV,T>.  Note that we already handled
335
       * combining <LV,T> above. */
336
952
      if (!has_glyph ||
337
109
    (!tindex &&
338
60
     buffer->idx + 1 < count &&
339
59
     isT (buffer->cur(+1).codepoint)))
340
867
      {
341
867
  hb_codepoint_t decomposed[3] = {LBase + lindex,
342
867
          VBase + vindex,
343
867
          TBase + tindex};
344
867
  if (font->has_glyph (decomposed[0]) &&
345
234
      font->has_glyph (decomposed[1]) &&
346
214
      (!tindex || font->has_glyph (decomposed[2])))
347
195
  {
348
195
    unsigned int s_len = tindex ? 3 : 2;
349
195
    (void) buffer->replace_glyphs (1, s_len, decomposed);
350
351
    /* If we decomposed an LV because of a non-combining T following,
352
     * we want to include this T in the syllable.
353
     */
354
195
    if (has_glyph && !tindex)
355
24
    {
356
24
      (void) buffer->next_glyph ();
357
24
      s_len++;
358
24
    }
359
195
    if (unlikely (!buffer->successful))
360
1
      break;
361
362
    /* We decomposed S: apply jamo features to the individual glyphs
363
     * that are now in buffer->out_info.
364
     */
365
194
    hb_glyph_info_t *info = buffer->out_info;
366
194
    end = start + s_len;
367
368
194
    unsigned int i = start;
369
194
    info[i++].hangul_shaping_feature() = LJMO;
370
194
    info[i++].hangul_shaping_feature() = VJMO;
371
194
    if (i < end)
372
157
      info[i++].hangul_shaping_feature() = TJMO;
373
374
194
    buffer->merge_out_grapheme_clusters (start, end);
375
194
    continue;
376
195
  }
377
672
  else if ((!tindex && buffer->idx + 1 < count && isT (buffer->cur(+1).codepoint)))
378
17
    buffer->unsafe_to_break (buffer->idx, buffer->idx + 2); /* Mark unsafe between LV and T. */
379
867
      }
380
381
757
      if (has_glyph)
382
85
      {
383
  /* We didn't decompose the S, so just advance past it and fall through. */
384
85
  end = start + 1;
385
85
      }
386
757
    }
387
388
    /* Didn't find a recognizable syllable, so we leave end <= start;
389
     * this will prevent tone-mark reordering happening.
390
     */
391
12.2k
    (void) buffer->next_glyph ();
392
12.2k
  }
393
815
  buffer->sync ();
394
815
}
395
396
static void
397
setup_masks_hangul (const hb_ot_shape_plan_t *plan,
398
        hb_buffer_t              *buffer,
399
        hb_font_t                *font HB_UNUSED)
400
815
{
401
815
  const hangul_shape_plan_t *hangul_plan = (const hangul_shape_plan_t *) plan->data;
402
403
815
  if (likely (hangul_plan))
404
815
  {
405
815
    unsigned int count = buffer->len;
406
815
    hb_glyph_info_t *info = buffer->info;
407
14.1k
    for (unsigned int i = 0; i < count; i++, info++)
408
13.3k
      info->mask |= hangul_plan->mask_array[info->hangul_shaping_feature()];
409
815
  }
410
411
  HB_BUFFER_DEALLOCATE_VAR (buffer, hangul_shaping_feature);
412
815
}
413
414
415
const hb_ot_shaper_t _hb_ot_shaper_hangul =
416
{
417
  collect_features_hangul,
418
  override_features_hangul,
419
  data_create_hangul,
420
  data_destroy_hangul,
421
  preprocess_text_hangul,
422
  nullptr, /* postprocess_glyphs */
423
  nullptr, /* decompose */
424
  nullptr, /* compose */
425
  setup_masks_hangul,
426
  nullptr, /* reorder_marks */
427
  HB_TAG_NONE, /* gpos_tag */
428
  HB_OT_SHAPE_NORMALIZATION_MODE_NONE,
429
  HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
430
  true, /* fallback_position */
431
};
432
433
434
#endif