2 * Copyright © 2013 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Behdad Esfahbod
29 #ifndef HB_NO_OT_SHAPE
31 #include "hb-ot-shaper.hh"
37 /* Same order as the feature array below */
45 FIRST_HANGUL_FEATURE = LJMO,
46 HANGUL_FEATURE_COUNT = TJMO + 1
49 static const hb_tag_t hangul_features[HANGUL_FEATURE_COUNT] =
52 HB_TAG('l','j','m','o'),
53 HB_TAG('v','j','m','o'),
54 HB_TAG('t','j','m','o')
58 collect_features_hangul (hb_ot_shape_planner_t *plan)
60 hb_ot_map_builder_t *map = &plan->map;
62 for (unsigned int i = FIRST_HANGUL_FEATURE; i < HANGUL_FEATURE_COUNT; i++)
63 map->add_feature (hangul_features[i]);
67 override_features_hangul (hb_ot_shape_planner_t *plan)
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 plan->map.disable_feature (HB_TAG('c','a','l','t'));
75 struct hangul_shape_plan_t
77 hb_mask_t mask_array[HANGUL_FEATURE_COUNT];
81 data_create_hangul (const hb_ot_shape_plan_t *plan)
83 hangul_shape_plan_t *hangul_plan = (hangul_shape_plan_t *) hb_calloc (1, sizeof (hangul_shape_plan_t));
84 if (unlikely (!hangul_plan))
87 for (unsigned int i = 0; i < HANGUL_FEATURE_COUNT; i++)
88 hangul_plan->mask_array[i] = plan->map.get_1_mask (hangul_features[i]);
94 data_destroy_hangul (void *data)
99 /* Constants for algorithmic hangul syllable [de]composition. */
100 #define LBase 0x1100u
101 #define VBase 0x1161u
102 #define TBase 0x11A7u
106 #define SBase 0xAC00u
107 #define NCount (VCount * TCount)
108 #define SCount (LCount * NCount)
110 #define isCombiningL(u) (hb_in_range<hb_codepoint_t> ((u), LBase, LBase+LCount-1))
111 #define isCombiningV(u) (hb_in_range<hb_codepoint_t> ((u), VBase, VBase+VCount-1))
112 #define isCombiningT(u) (hb_in_range<hb_codepoint_t> ((u), TBase+1, TBase+TCount-1))
113 #define isCombinedS(u) (hb_in_range<hb_codepoint_t> ((u), SBase, SBase+SCount-1))
115 #define isL(u) (hb_in_ranges<hb_codepoint_t> ((u), 0x1100u, 0x115Fu, 0xA960u, 0xA97Cu))
116 #define isV(u) (hb_in_ranges<hb_codepoint_t> ((u), 0x1160u, 0x11A7u, 0xD7B0u, 0xD7C6u))
117 #define isT(u) (hb_in_ranges<hb_codepoint_t> ((u), 0x11A8u, 0x11FFu, 0xD7CBu, 0xD7FBu))
119 #define isHangulTone(u) (hb_in_range<hb_codepoint_t> ((u), 0x302Eu, 0x302Fu))
121 /* buffer var allocations */
122 #define hangul_shaping_feature() ot_shaper_var_u8_auxiliary() /* hangul jamo shaping feature */
125 is_zero_width_char (hb_font_t *font,
126 hb_codepoint_t unicode)
128 hb_codepoint_t glyph;
129 return hb_font_get_glyph (font, unicode, 0, &glyph) && hb_font_get_glyph_h_advance (font, glyph) == 0;
133 preprocess_text_hangul (const hb_ot_shape_plan_t *plan HB_UNUSED,
137 HB_BUFFER_ALLOCATE_VAR (buffer, hangul_shaping_feature);
139 /* Hangul syllables come in two shapes: LV, and LVT. Of those:
141 * - LV can be precomposed, or decomposed. Lets call those
143 * - LVT can be fully precomposed, partially precomposed, or
144 * fully decomposed. Ie. <LVT>, <LV,T>, or <L,V,T>.
146 * The composition / decomposition is mechanical. However, not
147 * all <L,V> sequences compose, and not all <LV,T> sequences
150 * Here are the specifics:
152 * - <L>: U+1100..115F, U+A960..A97F
153 * - <V>: U+1160..11A7, U+D7B0..D7C7
154 * - <T>: U+11A8..11FF, U+D7CB..D7FB
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.
159 * Here is what we want to accomplish in this shaper:
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.
167 * That is, of the different possible syllables:
176 * - <L> needs no work.
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.
181 * - <L,V> and <L,V,T> we should compose if the whole thing can be composed.
183 * - <LV,T> we should compose if the whole thing can be composed, otherwise we should
187 buffer->clear_output ();
188 unsigned int start = 0, end = 0; /* Extent of most recently seen syllable;
189 * valid only if start < end
191 unsigned int count = buffer->len;
193 for (buffer->idx = 0; buffer->idx < count && buffer->successful;)
195 hb_codepoint_t u = buffer->cur().codepoint;
197 if (isHangulTone (u))
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.
204 if (start < end && end == buffer->out_len)
206 /* Tone mark follows a valid syllable; move it in front, unless it's zero width. */
207 buffer->unsafe_to_break_from_outbuffer (start, buffer->idx);
208 if (unlikely (!buffer->next_glyph ())) break;
209 if (!is_zero_width_char (font, u))
211 buffer->merge_out_clusters (start, end + 1);
212 hb_glyph_info_t *info = buffer->out_info;
213 hb_glyph_info_t tone = info[end];
214 memmove (&info[start + 1], &info[start], (end - start) * sizeof (hb_glyph_info_t));
220 /* No valid syllable as base for tone mark; try to insert dotted circle. */
221 if (!(buffer->flags & HB_BUFFER_FLAG_DO_NOT_INSERT_DOTTED_CIRCLE) &&
222 font->has_glyph (0x25CCu))
224 hb_codepoint_t chars[2];
225 if (!is_zero_width_char (font, u))
234 (void) buffer->replace_glyphs (1, 2, chars);
238 /* No dotted circle available in the font; just leave tone mark untouched. */
239 (void) buffer->next_glyph ();
242 start = end = buffer->out_len;
246 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.
250 if (isL (u) && buffer->idx + 1 < count)
252 hb_codepoint_t l = u;
253 hb_codepoint_t v = buffer->cur(+1).codepoint;
256 /* Have <L,V> or <L,V,T>. */
257 hb_codepoint_t t = 0;
258 unsigned int tindex = 0;
259 if (buffer->idx + 2 < count)
261 t = buffer->cur(+2).codepoint;
263 tindex = t - TBase; /* Only used if isCombiningT (t); otherwise invalid. */
265 t = 0; /* The next character was not a trailing jamo. */
267 buffer->unsafe_to_break (buffer->idx, buffer->idx + (t ? 3 : 2));
269 /* We've got a syllable <L,V,T?>; see if it can potentially be composed. */
270 if (isCombiningL (l) && isCombiningV (v) && (t == 0 || isCombiningT (t)))
272 /* Try to compose; if this succeeds, end is set to start+1. */
273 hb_codepoint_t s = SBase + (l - LBase) * NCount + (v - VBase) * TCount + tindex;
274 if (font->has_glyph (s))
276 (void) buffer->replace_glyphs (t ? 3 : 2, 1, &s);
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.
287 buffer->cur().hangul_shaping_feature() = LJMO;
288 (void) buffer->next_glyph ();
289 buffer->cur().hangul_shaping_feature() = VJMO;
290 (void) buffer->next_glyph ();
293 buffer->cur().hangul_shaping_feature() = TJMO;
294 (void) buffer->next_glyph ();
299 if (unlikely (!buffer->successful))
301 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
302 buffer->merge_out_clusters (start, end);
307 else if (isCombinedS (u))
309 /* Have <LV>, <LVT>, or <LV,T> */
310 hb_codepoint_t s = u;
311 bool has_glyph = font->has_glyph (s);
312 unsigned int lindex = (s - SBase) / NCount;
313 unsigned int nindex = (s - SBase) % NCount;
314 unsigned int vindex = nindex / TCount;
315 unsigned int tindex = nindex % TCount;
318 buffer->idx + 1 < count &&
319 isCombiningT (buffer->cur(+1).codepoint))
321 /* <LV,T>, try to combine. */
322 unsigned int new_tindex = buffer->cur(+1).codepoint - TBase;
323 hb_codepoint_t new_s = s + new_tindex;
324 if (font->has_glyph (new_s))
326 (void) buffer->replace_glyphs (2, 1, &new_s);
331 buffer->unsafe_to_break (buffer->idx, buffer->idx + 2); /* Mark unsafe between LV and T. */
334 /* Otherwise, decompose if font doesn't support <LV> or <LVT>,
335 * or if having non-combining <LV,T>. Note that we already handled
336 * combining <LV,T> above. */
339 buffer->idx + 1 < count &&
340 isT (buffer->cur(+1).codepoint)))
342 hb_codepoint_t decomposed[3] = {LBase + lindex,
345 if (font->has_glyph (decomposed[0]) &&
346 font->has_glyph (decomposed[1]) &&
347 (!tindex || font->has_glyph (decomposed[2])))
349 unsigned int s_len = tindex ? 3 : 2;
350 (void) buffer->replace_glyphs (1, s_len, decomposed);
352 /* If we decomposed an LV because of a non-combining T following,
353 * we want to include this T in the syllable.
355 if (has_glyph && !tindex)
357 (void) buffer->next_glyph ();
360 if (unlikely (!buffer->successful))
363 /* We decomposed S: apply jamo features to the individual glyphs
364 * that are now in buffer->out_info.
366 hb_glyph_info_t *info = buffer->out_info;
369 unsigned int i = start;
370 info[i++].hangul_shaping_feature() = LJMO;
371 info[i++].hangul_shaping_feature() = VJMO;
373 info[i++].hangul_shaping_feature() = TJMO;
375 if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
376 buffer->merge_out_clusters (start, end);
379 else if ((!tindex && buffer->idx + 1 < count && isT (buffer->cur(+1).codepoint)))
380 buffer->unsafe_to_break (buffer->idx, buffer->idx + 2); /* Mark unsafe between LV and T. */
385 /* We didn't decompose the S, so just advance past it and fall through. */
390 /* Didn't find a recognizable syllable, so we leave end <= start;
391 * this will prevent tone-mark reordering happening.
393 (void) buffer->next_glyph ();
399 setup_masks_hangul (const hb_ot_shape_plan_t *plan,
401 hb_font_t *font HB_UNUSED)
403 const hangul_shape_plan_t *hangul_plan = (const hangul_shape_plan_t *) plan->data;
405 if (likely (hangul_plan))
407 unsigned int count = buffer->len;
408 hb_glyph_info_t *info = buffer->info;
409 for (unsigned int i = 0; i < count; i++, info++)
410 info->mask |= hangul_plan->mask_array[info->hangul_shaping_feature()];
413 HB_BUFFER_DEALLOCATE_VAR (buffer, hangul_shaping_feature);
417 const hb_ot_shaper_t _hb_ot_shaper_hangul =
419 collect_features_hangul,
420 override_features_hangul,
423 preprocess_text_hangul,
424 nullptr, /* postprocess_glyphs */
425 nullptr, /* decompose */
426 nullptr, /* compose */
428 nullptr, /* reorder_marks */
429 HB_TAG_NONE, /* gpos_tag */
430 HB_OT_SHAPE_NORMALIZATION_MODE_NONE,
431 HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
432 false, /* fallback_position */