2 * Copyright © 2010 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"
30 /* TODO Add kana, and other small shapers here */
32 /* When adding trivial shapers, eg. kana, hangul, etc, we can either
33 * add a full shaper enum value for them, or switch on the script in
34 * the default complex shaper. The former is faster, so I think that's
35 * what we would do, and hence the default complex shaper shall remain
40 _hb_ot_shape_complex_collect_features_default (hb_ot_map_builder_t *map HB_UNUSED,
41 const hb_segment_properties_t *props HB_UNUSED)
45 hb_ot_shape_normalization_mode_t
46 _hb_ot_shape_complex_normalization_preference_default (void)
48 return HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS;
52 _hb_ot_shape_complex_setup_masks_default (hb_ot_map_t *map HB_UNUSED,
53 hb_buffer_t *buffer HB_UNUSED,
54 hb_font_t *font HB_UNUSED)
62 static const hb_tag_t hangul_features[] =
64 HB_TAG('l','j','m','o'),
65 HB_TAG('v','j','m','o'),
66 HB_TAG('t','j','m','o'),
70 _hb_ot_shape_complex_collect_features_hangul (hb_ot_map_builder_t *map,
71 const hb_segment_properties_t *props HB_UNUSED)
73 for (unsigned int i = 0; i < ARRAY_LENGTH (hangul_features); i++)
74 map->add_bool_feature (hangul_features[i]);
77 hb_ot_shape_normalization_mode_t
78 _hb_ot_shape_complex_normalization_preference_hangul (void)
80 return HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_FULL;
84 _hb_ot_shape_complex_setup_masks_hangul (hb_ot_map_t *map HB_UNUSED,
85 hb_buffer_t *buffer HB_UNUSED,
86 hb_font_t *font HB_UNUSED)
92 /* Thai / Lao shaper */
95 _hb_ot_shape_complex_collect_features_thai (hb_ot_map_builder_t *map HB_UNUSED,
96 const hb_segment_properties_t *props HB_UNUSED)
100 hb_ot_shape_normalization_mode_t
101 _hb_ot_shape_complex_normalization_preference_thai (void)
103 return HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_FULL;
107 _hb_ot_shape_complex_setup_masks_thai (hb_ot_map_t *map HB_UNUSED,
109 hb_font_t *font HB_UNUSED)
111 /* The following is NOT specified in the MS OT Thai spec, however, it seems
112 * to be what Uniscribe and other engines implement. According to Eric Muller:
114 * When you have a sara am, decompose it in nikhahit + sara a, *and* mode the
115 * nihka hit backwards over any *tone* mark (0E48-0E4B).
117 * <0E14, 0E4B, 0E33> -> <0E14, 0E4D, 0E4B, 0E32>
119 * This reordering is legit only when the nikhahit comes from a sara am, not
120 * when it's there to start with. The string <0E14, 0E4B, 0E4D> is probably
121 * not what a u↪ser wanted, but the rendering is nevertheless nikhahit above
128 * Here are the characters of significance:
131 * SARA AM: U+0E33 U+0EB3
132 * SARA AA: U+0E32 U+0EB2
133 * Nikhahit: U+0E4D U+0ECD
136 * Thai: <0E48..0E4B> CCC=107
137 * Lao: <0EC8..0ECB> CCC=122
139 * Note how the Lao versions are the same as Thai + 0x80.
142 /* We only get one script at a time, so a script-agnostic implementation
143 * is adequate here. */
144 #define IS_SARA_AM(x) (((x) & ~0x0080) == 0x0E33)
145 #define NIKHAHIT_FROM_SARA_AM(x) ((x) - 0xE33 + 0xE4D)
146 #define SARA_AA_FROM_SARA_AM(x) ((x) - 1)
147 #define IS_TONE_MARK(x) (((x) & ~0x0083) == 0x0E48)
149 buffer->clear_output ();
150 unsigned int count = buffer->len;
151 for (buffer->idx = 0; buffer->idx < count;)
153 hb_codepoint_t u = buffer->cur().codepoint;
154 if (likely (!IS_SARA_AM (u))) {
155 buffer->next_glyph ();
159 /* Is SARA AM. Decompose and reorder. */
160 hb_codepoint_t decomposed[2] = {hb_codepoint_t (NIKHAHIT_FROM_SARA_AM (u)),
161 hb_codepoint_t (SARA_AA_FROM_SARA_AM (u))};
162 buffer->replace_glyphs (1, 2, decomposed);
163 if (unlikely (buffer->in_error))
166 /* Ok, let's see... */
167 unsigned int end = buffer->out_len;
168 unsigned int start = end - 2;
169 while (start > 0 && IS_TONE_MARK (buffer->out_info[start - 1].codepoint))
172 /* Move Nikhahit (end-2) to the beginning */
173 hb_glyph_info_t t = buffer->out_info[end - 2];
174 memmove (buffer->out_info + start + 1,
175 buffer->out_info + start,
176 sizeof (buffer->out_info[0]) * (end - start - 2));
177 buffer->out_info[start] = t;
179 /* XXX Make this easier! */
181 for (; start > 0 && buffer->out_info[start - 1].cluster == buffer->out_info[start].cluster; start--)
183 for (; buffer->idx < count;)
184 if (buffer->cur().cluster == buffer->prev().cluster)
185 buffer->next_glyph ();
188 end = buffer->out_len;
190 buffer->merge_out_clusters (start, end);
192 buffer->swap_buffers ();