2 * Copyright © 2011,2012,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-indic-private.hh"
29 /* buffer var allocations */
30 #define sea_category() complex_var_u8_0() /* indic_category_t */
31 #define sea_position() complex_var_u8_1() /* indic_position_t */
35 * South-East Asian shaper.
36 * Loosely based on the Myanmar spec / shaper.
37 * There is no OpenType spec for this.
45 * These features are applied in order, one at a time, after initial_reordering.
47 HB_TAG('p','r','e','f'),
48 HB_TAG('a','b','v','f'),
49 HB_TAG('b','l','w','f'),
50 HB_TAG('p','s','t','f'),
57 * These features are applied all at once, after final_reordering.
59 HB_TAG('p','r','e','s'),
60 HB_TAG('a','b','v','s'),
61 HB_TAG('b','l','w','s'),
62 HB_TAG('p','s','t','s'),
63 /* Positioning features, though we don't care about the types. */
64 HB_TAG('d','i','s','t'),
68 setup_syllables (const hb_ot_shape_plan_t
*plan
,
72 initial_reordering (const hb_ot_shape_plan_t
*plan
,
76 final_reordering (const hb_ot_shape_plan_t
*plan
,
81 collect_features_sea (hb_ot_shape_planner_t
*plan
)
83 hb_ot_map_builder_t
*map
= &plan
->map
;
85 /* Do this before any lookups have been applied. */
86 map
->add_gsub_pause (setup_syllables
);
88 map
->add_global_bool_feature (HB_TAG('l','o','c','l'));
89 /* The Indic specs do not require ccmp, but we apply it here since if
90 * there is a use of it, it's typically at the beginning. */
91 map
->add_global_bool_feature (HB_TAG('c','c','m','p'));
93 map
->add_gsub_pause (initial_reordering
);
94 for (unsigned int i
= 0; i
< ARRAY_LENGTH (basic_features
); i
++)
96 map
->add_feature (basic_features
[i
], 1, F_GLOBAL
| F_MANUAL_ZWJ
);
97 map
->add_gsub_pause (NULL
);
99 map
->add_gsub_pause (final_reordering
);
100 for (unsigned int i
= 0; i
< ARRAY_LENGTH (other_features
); i
++)
101 map
->add_feature (other_features
[i
], 1, F_GLOBAL
| F_MANUAL_ZWJ
);
105 override_features_sea (hb_ot_shape_planner_t
*plan
)
107 plan
->map
.add_feature (HB_TAG('l','i','g','a'), 0, F_GLOBAL
);
111 enum syllable_type_t
{
117 #include "hb-ot-shape-complex-sea-machine.hh"
120 /* Note: This enum is duplicated in the -machine.rl source file.
121 * Not sure how to avoid duplication. */
122 enum sea_category_t
{
124 OT_GB
= 12, /* Generic Base XXX DOTTED CIRCLE only for now */
125 // OT_H = 4, /* Halant */
126 OT_IV
= 2, /* Independent Vowel */
127 OT_MR
= 22, /* Medial Ra */
128 // OT_CM = 17, /* Consonant Medial */
133 OT_T
= 3, /* Tone Marks */
134 // OT_A = 10, /* Anusvara */
138 set_sea_properties (hb_glyph_info_t
&info
)
140 hb_codepoint_t u
= info
.codepoint
;
141 unsigned int type
= hb_indic_get_categories (u
);
142 indic_category_t cat
= (indic_category_t
) (type
& 0x7Fu
);
143 indic_position_t pos
= (indic_position_t
) (type
>> 8);
146 if (u
== 0x1A55u
|| u
== 0xAA34u
)
147 cat
= (indic_category_t
) OT_MR
;
153 case POS_PRE_C
: cat
= (indic_category_t
) OT_VPre
; break;
154 case POS_ABOVE_C
: cat
= (indic_category_t
) OT_VAbv
; break;
155 case POS_BELOW_C
: cat
= (indic_category_t
) OT_VBlw
; break;
156 case POS_POST_C
: cat
= (indic_category_t
) OT_VPst
; break;
160 info
.sea_category() = (sea_category_t
) cat
;
161 info
.sea_position() = pos
;
166 setup_masks_sea (const hb_ot_shape_plan_t
*plan HB_UNUSED
,
168 hb_font_t
*font HB_UNUSED
)
170 HB_BUFFER_ALLOCATE_VAR (buffer
, sea_category
);
171 HB_BUFFER_ALLOCATE_VAR (buffer
, sea_position
);
173 /* We cannot setup masks here. We save information about characters
174 * and setup masks later on in a pause-callback. */
176 unsigned int count
= buffer
->len
;
177 hb_glyph_info_t
*info
= buffer
->info
;
178 for (unsigned int i
= 0; i
< count
; i
++)
179 set_sea_properties (info
[i
]);
183 setup_syllables (const hb_ot_shape_plan_t
*plan HB_UNUSED
,
184 hb_font_t
*font HB_UNUSED
,
187 find_syllables (buffer
);
191 compare_sea_order (const hb_glyph_info_t
*pa
, const hb_glyph_info_t
*pb
)
193 int a
= pa
->sea_position();
194 int b
= pb
->sea_position();
196 return a
< b
? -1 : a
== b
? 0 : +1;
201 initial_reordering_consonant_syllable (const hb_ot_shape_plan_t
*plan
,
204 unsigned int start
, unsigned int end
)
206 hb_glyph_info_t
*info
= buffer
->info
;
207 unsigned int base
= start
;
210 unsigned int i
= start
;
211 for (; i
< base
; i
++)
212 info
[i
].sea_position() = POS_PRE_C
;
215 info
[i
].sea_position() = POS_BASE_C
;
220 if (info
[i
].sea_category() == OT_MR
) /* Pre-base reordering */
222 info
[i
].sea_position() = POS_PRE_C
;
225 if (info
[i
].sea_category() == OT_VPre
) /* Left matra */
227 info
[i
].sea_position() = POS_PRE_M
;
231 info
[i
].sea_position() = POS_AFTER_MAIN
;
234 buffer
->merge_clusters (start
, end
);
235 /* Sit tight, rock 'n roll! */
236 hb_bubble_sort (info
+ start
, end
- start
, compare_sea_order
);
240 initial_reordering_broken_cluster (const hb_ot_shape_plan_t
*plan
,
243 unsigned int start
, unsigned int end
)
245 /* We already inserted dotted-circles, so just call the consonant_syllable. */
246 initial_reordering_consonant_syllable (plan
, face
, buffer
, start
, end
);
250 initial_reordering_non_sea_cluster (const hb_ot_shape_plan_t
*plan HB_UNUSED
,
251 hb_face_t
*face HB_UNUSED
,
252 hb_buffer_t
*buffer HB_UNUSED
,
253 unsigned int start HB_UNUSED
, unsigned int end HB_UNUSED
)
255 /* Nothing to do right now. If we ever switch to using the output
256 * buffer in the reordering process, we'd need to next_glyph() here. */
261 initial_reordering_syllable (const hb_ot_shape_plan_t
*plan
,
264 unsigned int start
, unsigned int end
)
266 syllable_type_t syllable_type
= (syllable_type_t
) (buffer
->info
[start
].syllable() & 0x0F);
267 switch (syllable_type
) {
268 case consonant_syllable
: initial_reordering_consonant_syllable (plan
, face
, buffer
, start
, end
); return;
269 case broken_cluster
: initial_reordering_broken_cluster (plan
, face
, buffer
, start
, end
); return;
270 case non_sea_cluster
: initial_reordering_non_sea_cluster (plan
, face
, buffer
, start
, end
); return;
275 insert_dotted_circles (const hb_ot_shape_plan_t
*plan HB_UNUSED
,
279 /* Note: This loop is extra overhead, but should not be measurable. */
280 bool has_broken_syllables
= false;
281 unsigned int count
= buffer
->len
;
282 hb_glyph_info_t
*info
= buffer
->info
;
283 for (unsigned int i
= 0; i
< count
; i
++)
284 if ((info
[i
].syllable() & 0x0F) == broken_cluster
)
286 has_broken_syllables
= true;
289 if (likely (!has_broken_syllables
))
293 hb_codepoint_t dottedcircle_glyph
;
294 if (!font
->get_glyph (0x25CCu
, 0, &dottedcircle_glyph
))
297 hb_glyph_info_t dottedcircle
= {0};
298 dottedcircle
.codepoint
= 0x25CCu
;
299 set_sea_properties (dottedcircle
);
300 dottedcircle
.codepoint
= dottedcircle_glyph
;
302 buffer
->clear_output ();
305 unsigned int last_syllable
= 0;
306 while (buffer
->idx
< buffer
->len
)
308 unsigned int syllable
= buffer
->cur().syllable();
309 syllable_type_t syllable_type
= (syllable_type_t
) (syllable
& 0x0F);
310 if (unlikely (last_syllable
!= syllable
&& syllable_type
== broken_cluster
))
312 last_syllable
= syllable
;
314 hb_glyph_info_t info
= dottedcircle
;
315 info
.cluster
= buffer
->cur().cluster
;
316 info
.mask
= buffer
->cur().mask
;
317 info
.syllable() = buffer
->cur().syllable();
319 buffer
->output_info (info
);
322 buffer
->next_glyph ();
325 buffer
->swap_buffers ();
329 initial_reordering (const hb_ot_shape_plan_t
*plan
,
333 insert_dotted_circles (plan
, font
, buffer
);
335 hb_glyph_info_t
*info
= buffer
->info
;
336 unsigned int count
= buffer
->len
;
337 if (unlikely (!count
)) return;
338 unsigned int last
= 0;
339 unsigned int last_syllable
= info
[0].syllable();
340 for (unsigned int i
= 1; i
< count
; i
++)
341 if (last_syllable
!= info
[i
].syllable()) {
342 initial_reordering_syllable (plan
, font
->face
, buffer
, last
, i
);
344 last_syllable
= info
[last
].syllable();
346 initial_reordering_syllable (plan
, font
->face
, buffer
, last
, count
);
350 final_reordering (const hb_ot_shape_plan_t
*plan
,
351 hb_font_t
*font HB_UNUSED
,
354 hb_glyph_info_t
*info
= buffer
->info
;
355 unsigned int count
= buffer
->len
;
357 /* Zero syllables now... */
358 for (unsigned int i
= 0; i
< count
; i
++)
359 info
[i
].syllable() = 0;
361 HB_BUFFER_DEALLOCATE_VAR (buffer
, sea_category
);
362 HB_BUFFER_DEALLOCATE_VAR (buffer
, sea_position
);
366 const hb_ot_complex_shaper_t _hb_ot_complex_shaper_sea
=
369 collect_features_sea
,
370 override_features_sea
,
371 NULL
, /* data_create */
372 NULL
, /* data_destroy */
373 NULL
, /* preprocess_text */
374 HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT
,
375 NULL
, /* decompose */
378 HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE
,
379 false, /* fallback_position */