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
27 #include "hb-ot-shape-complex-private.hh"
33 /* Same order as the feature array below */
41 FIRST_HANGUL_FEATURE
= LJMO
,
42 HANGUL_FEATURE_COUNT
= TJMO
+ 1
45 static const hb_tag_t hangul_features
[HANGUL_FEATURE_COUNT
] =
48 HB_TAG('l','j','m','o'),
49 HB_TAG('v','j','m','o'),
50 HB_TAG('t','j','m','o')
54 collect_features_hangul (hb_ot_shape_planner_t
*plan
)
56 hb_ot_map_builder_t
*map
= &plan
->map
;
58 for (unsigned int i
= FIRST_HANGUL_FEATURE
; i
< HANGUL_FEATURE_COUNT
; i
++)
59 map
->add_feature (hangul_features
[i
], 1, F_NONE
);
63 override_features_hangul (hb_ot_shape_planner_t
*plan
)
65 /* Uniscribe does not apply 'calt' for Hangul, and certain fonts
66 * (Noto Sans CJK, Source Sans Han, etc) apply all of jamo lookups
67 * in calt, which is not desirable. */
68 plan
->map
.add_feature (HB_TAG('c','a','l','t'), 0, F_GLOBAL
);
71 struct hangul_shape_plan_t
75 hb_mask_t mask_array
[HANGUL_FEATURE_COUNT
];
79 data_create_hangul (const hb_ot_shape_plan_t
*plan
)
81 hangul_shape_plan_t
*hangul_plan
= (hangul_shape_plan_t
*) calloc (1, sizeof (hangul_shape_plan_t
));
82 if (unlikely (!hangul_plan
))
85 for (unsigned int i
= 0; i
< HANGUL_FEATURE_COUNT
; i
++)
86 hangul_plan
->mask_array
[i
] = plan
->map
.get_1_mask (hangul_features
[i
]);
92 data_destroy_hangul (void *data
)
97 /* Constants for algorithmic hangul syllable [de]composition. */
100 #define TBase 0x11A7u
104 #define SBase 0xAC00u
105 #define NCount (VCount * TCount)
106 #define SCount (LCount * NCount)
108 #define isCombiningL(u) (hb_in_range ((u), LBase, LBase+LCount-1))
109 #define isCombiningV(u) (hb_in_range ((u), VBase, VBase+VCount-1))
110 #define isCombiningT(u) (hb_in_range ((u), TBase+1, TBase+TCount-1))
111 #define isCombinedS(u) (hb_in_range ((u), SBase, SBase+SCount-1))
113 #define isL(u) (hb_in_ranges ((u), 0x1100u, 0x115Fu, 0xA960u, 0xA97Cu))
114 #define isV(u) (hb_in_ranges ((u), 0x1160u, 0x11A7u, 0xD7B0u, 0xD7C6u))
115 #define isT(u) (hb_in_ranges ((u), 0x11A8u, 0x11FFu, 0xD7CBu, 0xD7FBu))
117 #define isHangulTone(u) (hb_in_range ((u), 0x302Eu, 0x302Fu))
119 /* buffer var allocations */
120 #define hangul_shaping_feature() complex_var_u8_0() /* hangul jamo shaping feature */
123 is_zero_width_char (hb_font_t
*font
,
124 hb_codepoint_t unicode
)
126 hb_codepoint_t glyph
;
127 return hb_font_get_glyph (font
, unicode
, 0, &glyph
) && hb_font_get_glyph_h_advance (font
, glyph
) == 0;
131 preprocess_text_hangul (const hb_ot_shape_plan_t
*plan
,
135 HB_BUFFER_ALLOCATE_VAR (buffer
, hangul_shaping_feature
);
137 /* Hangul syllables come in two shapes: LV, and LVT. Of those:
139 * - LV can be precomposed, or decomposed. Lets call those
141 * - LVT can be fully precomposed, partically precomposed, or
142 * fully decomposed. Ie. <LVT>, <LV,T>, or <L,V,T>.
144 * The composition / decomposition is mechanical. However, not
145 * all <L,V> sequences compose, and not all <LV,T> sequences
148 * Here are the specifics:
150 * - <L>: U+1100..115F, U+A960..A97F
151 * - <V>: U+1160..11A7, U+D7B0..D7C7
152 * - <T>: U+11A8..11FF, U+D7CB..D7FB
154 * - Only the <L,V> sequences for the 11xx ranges combine.
155 * - Only <LV,T> sequences for T in U+11A8..11C3 combine.
157 * Here is what we want to accomplish in this shaper:
159 * - If the whole syllable can be precomposed, do that,
160 * - Otherwise, fully decompose and apply ljmo/vjmo/tjmo features.
161 * - If a valid syllable is followed by a Hangul tone mark, reorder the tone
162 * mark to precede the whole syllable - unless it is a zero-width glyph, in
163 * which case we leave it untouched, assuming it's designed to overstrike.
165 * That is, of the different possible syllables:
174 * - <L> needs no work.
176 * - <LV> and <LVT> can stay the way they are if the font supports them, otherwise we
177 * should fully decompose them if font supports.
179 * - <L,V> and <L,V,T> we should compose if the whole thing can be composed.
181 * - <LV,T> we should compose if the whole thing can be composed, otherwise we should
185 buffer
->clear_output ();
186 unsigned int start
= 0, end
= 0; /* Extent of most recently seen syllable;
187 * valid only if start < end
189 unsigned int count
= buffer
->len
;
191 for (buffer
->idx
= 0; buffer
->idx
< count
;)
193 hb_codepoint_t u
= buffer
->cur().codepoint
;
195 if (isHangulTone (u
))
198 * We could cache the width of the tone marks and the existence of dotted-circle,
199 * but the use of the Hangul tone mark characters seems to be rare enough that
200 * I didn't bother for now.
202 if (start
< end
&& end
== buffer
->out_len
)
204 /* Tone mark follows a valid syllable; move it in front, unless it's zero width. */
205 buffer
->next_glyph ();
206 if (!is_zero_width_char (font
, u
))
208 hb_glyph_info_t
*info
= buffer
->out_info
;
209 hb_glyph_info_t tone
= info
[end
];
210 memmove (&info
[start
+ 1], &info
[start
], (end
- start
) * sizeof (hb_glyph_info_t
));
212 buffer
->merge_out_clusters (start
, end
+ 1);
217 /* No valid syllable as base for tone mark; try to insert dotted circle. */
218 if (font
->has_glyph (0x25CCu
))
220 hb_codepoint_t chars
[2];
221 if (!is_zero_width_char (font
, u
)) {
228 buffer
->replace_glyphs (1, 2, chars
);
232 /* No dotted circle available in the font; just leave tone mark untouched. */
233 buffer
->next_glyph ();
236 start
= end
= buffer
->out_len
;
240 start
= buffer
->out_len
; /* Remember current position as a potential syllable start;
241 * will only be used if we set end to a later position.
244 if (isL (u
) && buffer
->idx
+ 1 < count
)
246 hb_codepoint_t l
= u
;
247 hb_codepoint_t v
= buffer
->cur(+1).codepoint
;
250 /* Have <L,V> or <L,V,T>. */
251 hb_codepoint_t t
= 0;
252 unsigned int tindex
= 0;
253 if (buffer
->idx
+ 2 < count
)
255 t
= buffer
->cur(+2).codepoint
;
257 tindex
= t
- TBase
; /* Only used if isCombiningT (t); otherwise invalid. */
259 t
= 0; /* The next character was not a trailing jamo. */
262 /* We've got a syllable <L,V,T?>; see if it can potentially be composed. */
263 if (isCombiningL (l
) && isCombiningV (v
) && (t
== 0 || isCombiningT (t
)))
265 /* Try to compose; if this succeeds, end is set to start+1. */
266 hb_codepoint_t s
= SBase
+ (l
- LBase
) * NCount
+ (v
- VBase
) * TCount
+ tindex
;
267 if (font
->has_glyph (s
))
269 buffer
->replace_glyphs (t
? 3 : 2, 1, &s
);
270 if (unlikely (buffer
->in_error
))
277 /* We didn't compose, either because it's an Old Hangul syllable without a
278 * precomposed character in Unicode, or because the font didn't support the
279 * necessary precomposed glyph.
280 * Set jamo features on the individual glyphs, and advance past them.
282 buffer
->cur().hangul_shaping_feature() = LJMO
;
283 buffer
->next_glyph ();
284 buffer
->cur().hangul_shaping_feature() = VJMO
;
285 buffer
->next_glyph ();
288 buffer
->cur().hangul_shaping_feature() = TJMO
;
289 buffer
->next_glyph ();
294 if (buffer
->cluster_level
== HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES
)
295 buffer
->merge_out_clusters (start
, end
);
300 else if (isCombinedS (u
))
302 /* Have <LV>, <LVT>, or <LV,T> */
303 hb_codepoint_t s
= u
;
304 bool has_glyph
= font
->has_glyph (s
);
305 unsigned int lindex
= (s
- SBase
) / NCount
;
306 unsigned int nindex
= (s
- SBase
) % NCount
;
307 unsigned int vindex
= nindex
/ TCount
;
308 unsigned int tindex
= nindex
% TCount
;
311 buffer
->idx
+ 1 < count
&&
312 isCombiningT (buffer
->cur(+1).codepoint
))
314 /* <LV,T>, try to combine. */
315 unsigned int new_tindex
= buffer
->cur(+1).codepoint
- TBase
;
316 hb_codepoint_t new_s
= s
+ new_tindex
;
317 if (font
->has_glyph (new_s
))
319 buffer
->replace_glyphs (2, 1, &new_s
);
320 if (unlikely (buffer
->in_error
))
327 /* Otherwise, decompose if font doesn't support <LV> or <LVT>,
328 * or if having non-combining <LV,T>. Note that we already handled
329 * combining <LV,T> above. */
332 buffer
->idx
+ 1 < count
&&
333 isT (buffer
->cur(+1).codepoint
)))
335 hb_codepoint_t decomposed
[3] = {LBase
+ lindex
,
338 if (font
->has_glyph (decomposed
[0]) &&
339 font
->has_glyph (decomposed
[1]) &&
340 (!tindex
|| font
->has_glyph (decomposed
[2])))
342 unsigned int s_len
= tindex
? 3 : 2;
343 buffer
->replace_glyphs (1, s_len
, decomposed
);
344 if (unlikely (buffer
->in_error
))
347 /* We decomposed S: apply jamo features to the individual glyphs
348 * that are now in buffer->out_info.
350 hb_glyph_info_t
*info
= buffer
->out_info
;
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 buffer
->next_glyph ();
362 unsigned int i
= start
;
363 info
[i
++].hangul_shaping_feature() = LJMO
;
364 info
[i
++].hangul_shaping_feature() = VJMO
;
366 info
[i
++].hangul_shaping_feature() = TJMO
;
367 if (buffer
->cluster_level
== HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES
)
368 buffer
->merge_out_clusters (start
, end
);
375 /* We didn't decompose the S, so just advance past it. */
377 buffer
->next_glyph ();
382 /* Didn't find a recognizable syllable, so we leave end <= start;
383 * this will prevent tone-mark reordering happening.
385 buffer
->next_glyph ();
387 buffer
->swap_buffers ();
391 setup_masks_hangul (const hb_ot_shape_plan_t
*plan
,
393 hb_font_t
*font HB_UNUSED
)
395 const hangul_shape_plan_t
*hangul_plan
= (const hangul_shape_plan_t
*) plan
->data
;
397 if (likely (hangul_plan
))
399 unsigned int count
= buffer
->len
;
400 hb_glyph_info_t
*info
= buffer
->info
;
401 for (unsigned int i
= 0; i
< count
; i
++, info
++)
402 info
->mask
|= hangul_plan
->mask_array
[info
->hangul_shaping_feature()];
405 HB_BUFFER_DEALLOCATE_VAR (buffer
, hangul_shaping_feature
);
409 const hb_ot_complex_shaper_t _hb_ot_complex_shaper_hangul
=
412 collect_features_hangul
,
413 override_features_hangul
,
414 data_create_hangul
, /* data_create */
415 data_destroy_hangul
, /* data_destroy */
416 preprocess_text_hangul
,
417 HB_OT_SHAPE_NORMALIZATION_MODE_NONE
,
418 NULL
, /* decompose */
420 setup_masks_hangul
, /* setup_masks */
421 HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE
,
422 false, /* fallback_position */