Merge remote branch 'martin/master'
[framework/uifw/harfbuzz.git] / src / hb-ot-shape.cc
1 /*
2  * Copyright (C) 2009  Red Hat, 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  * Red Hat Author(s): Behdad Esfahbod
25  */
26
27 #include "hb-ot-shape-private.hh"
28
29 #include "hb-buffer-private.hh"
30
31 #include "hb-ot-layout.h"
32
33 /* XXX vertical */
34 hb_tag_t default_features[] = {
35   HB_TAG('c','a','l','t'),
36   HB_TAG('c','c','m','p'),
37   HB_TAG('c','l','i','g'),
38   HB_TAG('c','s','w','h'),
39   HB_TAG('c','u','r','s'),
40   HB_TAG('k','e','r','n'),
41   HB_TAG('l','i','g','a'),
42   HB_TAG('l','o','c','l'),
43   HB_TAG('m','a','r','k'),
44   HB_TAG('m','k','m','k'),
45   HB_TAG('r','l','i','g')
46 };
47
48 enum {
49   MASK_ALWAYS_ON = 1 << 0,
50   MASK_RTLM      = 1 << 1
51 };
52 #define MASK_BITS_USED 2
53
54 struct lookup_map {
55   unsigned int index;
56   hb_mask_t mask;
57 };
58
59
60 static void
61 add_feature (hb_face_t    *face,
62              hb_tag_t      table_tag,
63              unsigned int  feature_index,
64              hb_mask_t     mask,
65              lookup_map   *lookups,
66              unsigned int *num_lookups,
67              unsigned int  room_lookups)
68 {
69   unsigned int i = room_lookups - *num_lookups;
70   lookups += *num_lookups;
71
72   unsigned int *lookup_indices = (unsigned int *) lookups;
73
74   hb_ot_layout_feature_get_lookup_indexes (face, table_tag, feature_index, 0,
75                                            &i,
76                                            lookup_indices);
77
78   *num_lookups += i;
79
80   while (i--) {
81     lookups[i].mask = mask;
82     lookups[i].index = lookup_indices[i];
83   }
84 }
85
86 static hb_bool_t
87 maybe_add_feature (hb_face_t    *face,
88                    hb_tag_t      table_tag,
89                    unsigned int  script_index,
90                    unsigned int  language_index,
91                    hb_tag_t      feature_tag,
92                    hb_mask_t     mask,
93                    lookup_map   *lookups,
94                    unsigned int *num_lookups,
95                    unsigned int  room_lookups)
96 {
97   unsigned int feature_index;
98   if (hb_ot_layout_language_find_feature (face, table_tag, script_index, language_index,
99                                           feature_tag,
100                                           &feature_index))
101   {
102     add_feature (face, table_tag, feature_index, mask, lookups, num_lookups, room_lookups);
103     return TRUE;
104   }
105   return FALSE;
106 }
107
108 static int
109 cmp_lookups (const void *p1, const void *p2)
110 {
111   const lookup_map *a = (const lookup_map *) p1;
112   const lookup_map *b = (const lookup_map *) p2;
113
114   return a->index - b->index;
115 }
116
117 static void
118 setup_lookups (hb_face_t    *face,
119                hb_buffer_t  *buffer,
120                hb_feature_t *features,
121                unsigned int  num_features,
122                hb_tag_t      table_tag,
123                lookup_map   *lookups,
124                unsigned int *num_lookups,
125                hb_direction_t original_direction)
126 {
127   unsigned int i, j, script_index, language_index, feature_index, room_lookups;
128
129   room_lookups = *num_lookups;
130   *num_lookups = 0;
131
132   hb_ot_layout_table_choose_script (face, table_tag,
133                                     hb_ot_tags_from_script (buffer->script),
134                                     &script_index);
135   hb_ot_layout_script_find_language (face, table_tag, script_index,
136                                      hb_ot_tag_from_language (buffer->language),
137                                      &language_index);
138
139   if (hb_ot_layout_language_get_required_feature_index (face, table_tag, script_index, language_index,
140                                                         &feature_index))
141     add_feature (face, table_tag, feature_index, 1, lookups, num_lookups, room_lookups);
142
143   for (i = 0; i < ARRAY_LENGTH (default_features); i++)
144     maybe_add_feature (face, table_tag, script_index, language_index, default_features[i], 1, lookups, num_lookups, room_lookups);
145
146   switch (original_direction) {
147     case HB_DIRECTION_LTR:
148       maybe_add_feature (face, table_tag, script_index, language_index, HB_TAG ('l','t','r','a'), 1, lookups, num_lookups, room_lookups);
149       maybe_add_feature (face, table_tag, script_index, language_index, HB_TAG ('l','t','r','m'), 1, lookups, num_lookups, room_lookups);
150       break;
151     case HB_DIRECTION_RTL:
152       maybe_add_feature (face, table_tag, script_index, language_index, HB_TAG ('r','t','l','a'), 1, lookups, num_lookups, room_lookups);
153       break;
154     case HB_DIRECTION_TTB:
155     case HB_DIRECTION_BTT:
156     default:
157       break;
158   }
159
160   unsigned int next_bit = MASK_BITS_USED;
161   hb_mask_t global_mask = 0;
162   for (i = 0; i < num_features; i++)
163   {
164     hb_feature_t *feature = &features[i];
165     if (!hb_ot_layout_language_find_feature (face, table_tag, script_index, language_index,
166                                              feature->tag,
167                                              &feature_index))
168       continue;
169
170     if (feature->value == 1 && feature->start == 0 && feature->end == (unsigned int) -1) {
171       add_feature (face, table_tag, feature_index, 1, lookups, num_lookups, room_lookups);
172       continue;
173     }
174
175     /* Allocate bits for the features */
176
177     unsigned int bits_needed = _hb_bit_storage (feature->value);
178     if (!bits_needed)
179       continue; /* Feature disabled */
180
181     if (next_bit + bits_needed > 8 * sizeof (hb_mask_t))
182       continue; /* Oh well... */
183
184     unsigned int mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
185     unsigned int value = feature->value << next_bit;
186     next_bit += bits_needed;
187
188     add_feature (face, table_tag, feature_index, mask, lookups, num_lookups, room_lookups);
189
190     if (feature->start == 0 && feature->end == (unsigned int) -1)
191       global_mask |= value;
192     else
193       buffer->or_masks (mask, feature->start, feature->end);
194   }
195
196   if (global_mask)
197     buffer->or_masks (global_mask, 0, (unsigned int) -1);
198
199   qsort (lookups, *num_lookups, sizeof (lookups[0]), cmp_lookups);
200
201   if (*num_lookups)
202   {
203     for (i = 1, j = 0; i < *num_lookups; i++)
204       if (lookups[i].index != lookups[j].index)
205         lookups[++j] = lookups[i];
206       else
207         lookups[j].mask |= lookups[i].mask;
208     j++;
209     *num_lookups = j;
210   }
211 }
212
213
214 static hb_bool_t
215 hb_ot_substitute_complex (hb_font_t    *font HB_UNUSED,
216                           hb_face_t    *face,
217                           hb_buffer_t  *buffer,
218                           hb_feature_t *features,
219                           unsigned int  num_features,
220                           hb_direction_t original_direction)
221 {
222   lookup_map lookups[1000];
223   unsigned int num_lookups = ARRAY_LENGTH (lookups);
224   unsigned int i;
225
226   if (!hb_ot_layout_has_substitution (face))
227     return FALSE;
228
229   setup_lookups (face, buffer, features, num_features,
230                  HB_OT_TAG_GSUB,
231                  lookups, &num_lookups,
232                  original_direction);
233
234   for (i = 0; i < num_lookups; i++)
235     hb_ot_layout_substitute_lookup (face, buffer, lookups[i].index, lookups[i].mask);
236
237   return TRUE;
238 }
239
240 static hb_bool_t
241 hb_ot_position_complex (hb_font_t    *font,
242                         hb_face_t    *face,
243                         hb_buffer_t  *buffer,
244                         hb_feature_t *features,
245                         unsigned int  num_features,
246                         hb_direction_t original_direction)
247 {
248   lookup_map lookups[1000];
249   unsigned int num_lookups = ARRAY_LENGTH (lookups);
250   unsigned int i;
251
252   if (!hb_ot_layout_has_positioning (face))
253     return FALSE;
254
255   setup_lookups (face, buffer, features, num_features,
256                  HB_OT_TAG_GPOS,
257                  lookups, &num_lookups,
258                  original_direction);
259
260   for (i = 0; i < num_lookups; i++)
261     hb_ot_layout_position_lookup (font, face, buffer, lookups[i].index, lookups[i].mask);
262
263   hb_ot_layout_position_finish (font, face, buffer);
264
265   return TRUE;
266 }
267
268
269 /* Main shaper */
270
271 /* Prepare */
272
273 static inline hb_bool_t
274 is_variation_selector (hb_codepoint_t unicode)
275 {
276   return unlikely ((unicode >=  0x180B && unicode <=  0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
277                    (unicode >=  0xFE00 && unicode <=  0xFE0F) || /* VARIATION SELECTOR-1..16 */
278                    (unicode >= 0xE0100 && unicode <= 0xE01EF));  /* VARIATION SELECTOR-17..256 */
279 }
280
281 static void
282 hb_form_clusters (hb_buffer_t *buffer)
283 {
284   unsigned int count = buffer->len;
285   for (unsigned int i = 1; i < count; i++)
286     if (buffer->unicode->v.get_general_category (buffer->info[i].codepoint) == HB_CATEGORY_NON_SPACING_MARK)
287       buffer->info[i].cluster = buffer->info[i - 1].cluster;
288 }
289
290 static hb_direction_t
291 hb_ensure_native_direction (hb_buffer_t *buffer)
292 {
293   hb_direction_t original_direction = buffer->direction;
294
295   /* TODO vertical */
296   if (HB_DIRECTION_IS_HORIZONTAL (original_direction) &&
297       original_direction != _hb_script_get_horizontal_direction (buffer->script))
298   {
299     hb_buffer_reverse_clusters (buffer);
300     buffer->direction = HB_DIRECTION_REVERSE (buffer->direction);
301   }
302
303   return original_direction;
304 }
305
306
307 /* Substitute */
308
309 static void
310 hb_mirror_chars (hb_buffer_t *buffer)
311 {
312   hb_unicode_get_mirroring_func_t get_mirroring = buffer->unicode->v.get_mirroring;
313
314   if (HB_DIRECTION_IS_FORWARD (buffer->direction))
315     return;
316
317   unsigned int count = buffer->len;
318   for (unsigned int i = 0; i < count; i++) {
319     hb_codepoint_t codepoint = get_mirroring (buffer->info[i].codepoint);
320     if (likely (codepoint == buffer->info[i].codepoint))
321       buffer->info[i].mask |= MASK_RTLM;
322     else
323       buffer->info[i].codepoint = codepoint;
324   }
325 }
326
327 static void
328 hb_map_glyphs (hb_font_t    *font,
329                hb_face_t    *face,
330                hb_buffer_t  *buffer)
331 {
332   if (unlikely (!buffer->len))
333     return;
334
335   unsigned int count = buffer->len - 1;
336   for (unsigned int i = 0; i < count; i++) {
337     if (unlikely (is_variation_selector (buffer->info[i + 1].codepoint))) {
338       buffer->info[i].codepoint = hb_font_get_glyph (font, face, buffer->info[i].codepoint, buffer->info[i + 1].codepoint);
339       i++;
340     } else {
341       buffer->info[i].codepoint = hb_font_get_glyph (font, face, buffer->info[i].codepoint, 0);
342     }
343   }
344   buffer->info[count].codepoint = hb_font_get_glyph (font, face, buffer->info[count].codepoint, 0);
345 }
346
347 static void
348 hb_substitute_default (hb_font_t    *font,
349                        hb_face_t    *face,
350                        hb_buffer_t  *buffer,
351                        hb_feature_t *features HB_UNUSED,
352                        unsigned int  num_features HB_UNUSED)
353 {
354   hb_map_glyphs (font, face, buffer);
355 }
356
357 static void
358 hb_substitute_complex_fallback (hb_font_t    *font HB_UNUSED,
359                                 hb_face_t    *face HB_UNUSED,
360                                 hb_buffer_t  *buffer HB_UNUSED,
361                                 hb_feature_t *features HB_UNUSED,
362                                 unsigned int  num_features HB_UNUSED)
363 {
364   /* TODO Arabic */
365 }
366
367
368 /* Position */
369
370 static void
371 hb_position_default (hb_font_t    *font,
372                      hb_face_t    *face,
373                      hb_buffer_t  *buffer,
374                      hb_feature_t *features HB_UNUSED,
375                      unsigned int  num_features HB_UNUSED)
376 {
377   hb_buffer_clear_positions (buffer);
378
379   unsigned int count = buffer->len;
380   for (unsigned int i = 0; i < count; i++) {
381     hb_glyph_metrics_t metrics;
382     hb_font_get_glyph_metrics (font, face, buffer->info[i].codepoint, &metrics);
383     buffer->pos[i].x_advance = metrics.x_advance;
384     buffer->pos[i].y_advance = metrics.y_advance;
385   }
386 }
387
388 static void
389 hb_position_complex_fallback (hb_font_t    *font HB_UNUSED,
390                               hb_face_t    *face HB_UNUSED,
391                               hb_buffer_t  *buffer HB_UNUSED,
392                               hb_feature_t *features HB_UNUSED,
393                               unsigned int  num_features HB_UNUSED)
394 {
395   /* TODO Mark pos */
396 }
397
398 static void
399 hb_truetype_kern (hb_font_t    *font,
400                   hb_face_t    *face,
401                   hb_buffer_t  *buffer,
402                   hb_feature_t *features HB_UNUSED,
403                   unsigned int  num_features HB_UNUSED)
404 {
405   /* TODO Check for kern=0 */
406   unsigned int count = buffer->len;
407   for (unsigned int i = 1; i < count; i++) {
408     hb_position_t kern, kern1, kern2;
409     kern = hb_font_get_kerning (font, face, buffer->info[i - 1].codepoint, buffer->info[i].codepoint);
410     kern1 = kern >> 1;
411     kern2 = kern - kern1;
412     buffer->pos[i - 1].x_advance += kern1;
413     buffer->pos[i].x_advance += kern2;
414     buffer->pos[i].x_offset += kern2;
415   }
416 }
417
418 static void
419 hb_position_complex_fallback_visual (hb_font_t    *font,
420                                      hb_face_t    *face,
421                                      hb_buffer_t  *buffer,
422                                      hb_feature_t *features,
423                                      unsigned int  num_features)
424 {
425   hb_truetype_kern (font, face, buffer, features, num_features);
426 }
427
428
429 /* Do it! */
430
431 void
432 hb_ot_shape (hb_font_t    *font,
433              hb_face_t    *face,
434              hb_buffer_t  *buffer,
435              hb_feature_t *features,
436              unsigned int  num_features)
437 {
438   hb_direction_t original_direction;
439   hb_bool_t substitute_fallback, position_fallback;
440
441   hb_form_clusters (buffer);
442
443   /* SUBSTITUTE */
444
445   buffer->clear_masks ();
446
447   /* Mirroring needs to see the original direction */
448   hb_mirror_chars (buffer);
449
450   original_direction = hb_ensure_native_direction (buffer);
451
452   hb_substitute_default (font, face, buffer, features, num_features);
453
454   substitute_fallback = !hb_ot_substitute_complex (font, face, buffer, features, num_features, original_direction);
455
456   if (substitute_fallback)
457     hb_substitute_complex_fallback (font, face, buffer, features, num_features);
458
459
460   /* POSITION */
461
462   buffer->clear_masks ();
463
464   hb_position_default (font, face, buffer, features, num_features);
465
466   position_fallback = !hb_ot_position_complex (font, face, buffer, features, num_features, original_direction);
467
468   if (position_fallback)
469     hb_position_complex_fallback (font, face, buffer, features, num_features);
470
471   if (HB_DIRECTION_IS_BACKWARD (buffer->direction))
472     hb_buffer_reverse (buffer);
473
474   if (position_fallback)
475     hb_position_complex_fallback_visual (font, face, buffer, features, num_features);
476
477   buffer->direction = original_direction;
478 }