1/*
2 * Copyright © 2011,2012,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-ot-shape-complex-indic-private.hh"
28
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 */
32
33
34/*
35 * South-East Asian shaper.
36 * Loosely based on the Myanmar spec / shaper.
37 * There is no OpenType spec for this.
38 */
39
40static const hb_tag_t
41basic_features[] =
42{
43  /*
44   * Basic features.
45   * These features are applied in order, one at a time, after initial_reordering.
46   */
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'),
51};
52static const hb_tag_t
53other_features[] =
54{
55  /*
56   * Other features.
57   * These features are applied all at once, after final_reordering.
58   */
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'),
65};
66
67static void
68setup_syllables (const hb_ot_shape_plan_t *plan,
69		 hb_font_t *font,
70		 hb_buffer_t *buffer);
71static void
72initial_reordering (const hb_ot_shape_plan_t *plan,
73		    hb_font_t *font,
74		    hb_buffer_t *buffer);
75static void
76final_reordering (const hb_ot_shape_plan_t *plan,
77		  hb_font_t *font,
78		  hb_buffer_t *buffer);
79
80static void
81collect_features_sea (hb_ot_shape_planner_t *plan)
82{
83  hb_ot_map_builder_t *map = &plan->map;
84
85  /* Do this before any lookups have been applied. */
86  map->add_gsub_pause (setup_syllables);
87
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'));
92
93  map->add_gsub_pause (initial_reordering);
94  for (unsigned int i = 0; i < ARRAY_LENGTH (basic_features); i++)
95  {
96    map->add_feature (basic_features[i], 1, F_GLOBAL | F_MANUAL_ZWJ);
97    map->add_gsub_pause (NULL);
98  }
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);
102}
103
104static void
105override_features_sea (hb_ot_shape_planner_t *plan)
106{
107  plan->map.add_feature (HB_TAG('l','i','g','a'), 0, F_GLOBAL);
108}
109
110
111enum syllable_type_t {
112  consonant_syllable,
113  broken_cluster,
114  non_sea_cluster,
115};
116
117#include "hb-ot-shape-complex-sea-machine.hh"
118
119
120/* Note: This enum is duplicated in the -machine.rl source file.
121 * Not sure how to avoid duplication. */
122enum sea_category_t {
123//  OT_C    = 1,
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 */
129  OT_VAbv = 26,
130  OT_VBlw = 27,
131  OT_VPre = 28,
132  OT_VPst = 29,
133  OT_T    = 3,  /* Tone Marks */
134//  OT_A    = 10, /* Anusvara */
135};
136
137static inline void
138set_sea_properties (hb_glyph_info_t &info)
139{
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 & 0x7F);
143  indic_position_t pos = (indic_position_t) (type >> 8);
144
145  /* Medial Ra */
146  if (u == 0x1A55 || u == 0xAA34)
147    cat = (indic_category_t) OT_MR;
148
149  if (cat == OT_M)
150  {
151    switch ((int) pos)
152    {
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;
157    }
158  }
159
160  info.sea_category() = (sea_category_t) cat;
161  info.sea_position() = pos;
162}
163
164
165static void
166setup_masks_sea (const hb_ot_shape_plan_t *plan HB_UNUSED,
167		 hb_buffer_t              *buffer,
168		 hb_font_t                *font HB_UNUSED)
169{
170  HB_BUFFER_ALLOCATE_VAR (buffer, sea_category);
171  HB_BUFFER_ALLOCATE_VAR (buffer, sea_position);
172
173  /* We cannot setup masks here.  We save information about characters
174   * and setup masks later on in a pause-callback. */
175
176  unsigned int count = buffer->len;
177  for (unsigned int i = 0; i < count; i++)
178    set_sea_properties (buffer->info[i]);
179}
180
181static void
182setup_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED,
183		 hb_font_t *font HB_UNUSED,
184		 hb_buffer_t *buffer)
185{
186  find_syllables (buffer);
187}
188
189static int
190compare_sea_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
191{
192  int a = pa->sea_position();
193  int b = pb->sea_position();
194
195  return a < b ? -1 : a == b ? 0 : +1;
196}
197
198
199static void
200initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan,
201				       hb_face_t *face,
202				       hb_buffer_t *buffer,
203				       unsigned int start, unsigned int end)
204{
205  hb_glyph_info_t *info = buffer->info;
206  unsigned int base = start;
207
208  /* Reorder! */
209  unsigned int i = start;
210  for (; i < base; i++)
211    info[i].sea_position() = POS_PRE_C;
212  if (i < end)
213  {
214    info[i].sea_position() = POS_BASE_C;
215    i++;
216  }
217  for (; i < end; i++)
218  {
219    if (info[i].sea_category() == OT_MR) /* Pre-base reordering */
220    {
221      info[i].sea_position() = POS_PRE_C;
222      continue;
223    }
224    if (info[i].sea_category() == OT_VPre) /* Left matra */
225    {
226      info[i].sea_position() = POS_PRE_M;
227      continue;
228    }
229
230    info[i].sea_position() = POS_AFTER_MAIN;
231  }
232
233  buffer->merge_clusters (start, end);
234  /* Sit tight, rock 'n roll! */
235  hb_bubble_sort (info + start, end - start, compare_sea_order);
236}
237
238static void
239initial_reordering_broken_cluster (const hb_ot_shape_plan_t *plan,
240				   hb_face_t *face,
241				   hb_buffer_t *buffer,
242				   unsigned int start, unsigned int end)
243{
244  /* We already inserted dotted-circles, so just call the consonant_syllable. */
245  initial_reordering_consonant_syllable (plan, face, buffer, start, end);
246}
247
248static void
249initial_reordering_non_sea_cluster (const hb_ot_shape_plan_t *plan HB_UNUSED,
250				    hb_face_t *face HB_UNUSED,
251				    hb_buffer_t *buffer HB_UNUSED,
252				    unsigned int start HB_UNUSED, unsigned int end HB_UNUSED)
253{
254  /* Nothing to do right now.  If we ever switch to using the output
255   * buffer in the reordering process, we'd need to next_glyph() here. */
256}
257
258
259static void
260initial_reordering_syllable (const hb_ot_shape_plan_t *plan,
261			     hb_face_t *face,
262			     hb_buffer_t *buffer,
263			     unsigned int start, unsigned int end)
264{
265  syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F);
266  switch (syllable_type) {
267  case consonant_syllable:	initial_reordering_consonant_syllable  (plan, face, buffer, start, end); return;
268  case broken_cluster:		initial_reordering_broken_cluster      (plan, face, buffer, start, end); return;
269  case non_sea_cluster:	initial_reordering_non_sea_cluster (plan, face, buffer, start, end); return;
270  }
271}
272
273static inline void
274insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED,
275		       hb_font_t *font,
276		       hb_buffer_t *buffer)
277{
278  /* Note: This loop is extra overhead, but should not be measurable. */
279  bool has_broken_syllables = false;
280  unsigned int count = buffer->len;
281  for (unsigned int i = 0; i < count; i++)
282    if ((buffer->info[i].syllable() & 0x0F) == broken_cluster) {
283      has_broken_syllables = true;
284      break;
285    }
286  if (likely (!has_broken_syllables))
287    return;
288
289
290  hb_codepoint_t dottedcircle_glyph;
291  if (!font->get_glyph (0x25CC, 0, &dottedcircle_glyph))
292    return;
293
294  hb_glyph_info_t dottedcircle = {0};
295  dottedcircle.codepoint = 0x25CC;
296  set_sea_properties (dottedcircle);
297  dottedcircle.codepoint = dottedcircle_glyph;
298
299  buffer->clear_output ();
300
301  buffer->idx = 0;
302  unsigned int last_syllable = 0;
303  while (buffer->idx < buffer->len)
304  {
305    unsigned int syllable = buffer->cur().syllable();
306    syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F);
307    if (unlikely (last_syllable != syllable && syllable_type == broken_cluster))
308    {
309      last_syllable = syllable;
310
311      hb_glyph_info_t info = dottedcircle;
312      info.cluster = buffer->cur().cluster;
313      info.mask = buffer->cur().mask;
314      info.syllable() = buffer->cur().syllable();
315
316      buffer->output_info (info);
317    }
318    else
319      buffer->next_glyph ();
320  }
321
322  buffer->swap_buffers ();
323}
324
325static void
326initial_reordering (const hb_ot_shape_plan_t *plan,
327		    hb_font_t *font,
328		    hb_buffer_t *buffer)
329{
330  insert_dotted_circles (plan, font, buffer);
331
332  hb_glyph_info_t *info = buffer->info;
333  unsigned int count = buffer->len;
334  if (unlikely (!count)) return;
335  unsigned int last = 0;
336  unsigned int last_syllable = info[0].syllable();
337  for (unsigned int i = 1; i < count; i++)
338    if (last_syllable != info[i].syllable()) {
339      initial_reordering_syllable (plan, font->face, buffer, last, i);
340      last = i;
341      last_syllable = info[last].syllable();
342    }
343  initial_reordering_syllable (plan, font->face, buffer, last, count);
344}
345
346static void
347final_reordering (const hb_ot_shape_plan_t *plan,
348		  hb_font_t *font HB_UNUSED,
349		  hb_buffer_t *buffer)
350{
351  hb_glyph_info_t *info = buffer->info;
352  unsigned int count = buffer->len;
353
354  /* Zero syllables now... */
355  for (unsigned int i = 0; i < count; i++)
356    info[i].syllable() = 0;
357
358  HB_BUFFER_DEALLOCATE_VAR (buffer, sea_category);
359  HB_BUFFER_DEALLOCATE_VAR (buffer, sea_position);
360}
361
362
363static hb_ot_shape_normalization_mode_t
364normalization_preference_sea (const hb_segment_properties_t *props HB_UNUSED)
365{
366  return HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT;
367}
368
369
370const hb_ot_complex_shaper_t _hb_ot_complex_shaper_sea =
371{
372  "sea",
373  collect_features_sea,
374  override_features_sea,
375  NULL, /* data_create */
376  NULL, /* data_destroy */
377  NULL, /* preprocess_text */
378  normalization_preference_sea,
379  NULL, /* decompose */
380  NULL, /* compose */
381  setup_masks_sea,
382  HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
383  false, /* fallback_position */
384};
385