Allow disabling default features
[profile/ivi/org.tizen.video-player.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.h"
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 int
87 cmp_lookups (const void *p1, const void *p2)
88 {
89   const lookup_map *a = (const lookup_map *) p1;
90   const lookup_map *b = (const lookup_map *) p2;
91
92   return a->index - b->index;
93 }
94
95
96 #define MAX_FEATURES 100
97
98 struct feature_info {
99   hb_tag_t tag;
100   unsigned int index;
101   unsigned int value;
102   hb_mask_t mask;
103 };
104
105 struct feature_setup_state {
106   hb_mask_t global_mask;
107   unsigned int next_bit;
108   unsigned int count;
109   feature_info info[MAX_FEATURES];
110 };
111
112 static void
113 collect_feature_info(hb_buffer_t *buffer,
114                      hb_face_t *face,
115                      hb_tag_t table_tag,
116                      unsigned int script_index,
117                      unsigned int language_index,
118                      const hb_feature_t &feature,
119                      feature_setup_state &fss,
120                      const hb_feature_t *features,
121                      unsigned int num_features)
122 {
123   if (fss.count == MAX_FEATURES)
124     return; // FIXME - make the feature_info array growable?
125
126   unsigned int i, feature_index;
127   if (!hb_ot_layout_language_find_feature (face, table_tag, script_index, language_index,
128                                            feature.tag, &feature_index))
129       return;
130
131   fss.info[fss.count].tag = feature.tag;
132   fss.info[fss.count].index = feature_index;
133
134   // check if we have already allocated mask bits for this feature tag
135   for (i = 0; i < fss.count; ++i)
136   {
137     if (fss.info[i].tag == feature.tag)
138     {
139       fss.info[fss.count].mask = fss.info[i].mask;
140       fss.info[fss.count].value = feature.value << _hb_ctz(fss.info[fss.count].mask);
141       if (feature.start == 0 && feature.end == (unsigned int) -1)
142         fss.global_mask |= fss.info[fss.count].value;
143       else
144         buffer->set_masks(fss.info[fss.count].value, fss.info[fss.count].mask, feature.start, feature.end);
145       ++fss.count;
146       return;
147     }
148   }
149
150   // check if the feature occurs in remaining user features, as it might have a larger value there
151   hb_bool_t always_on = (feature.value == 1 && feature.start == 0 && feature.end == (unsigned int)-1);
152   unsigned int max_value = feature.value;
153   for (i = 0; i < num_features; ++i)
154   {
155     if (features[i].tag != feature.tag)
156       continue;
157     if (features[i].value > max_value)
158       max_value = features[i].value;
159     else if (features[i].value == 0 && features[i].start == 0 && features[i].end == (unsigned int)-1)
160       max_value = 0;
161   }
162
163   if (always_on && max_value == 1)
164   {
165     fss.info[fss.count].value = 1;
166     fss.info[fss.count].mask = 1;
167     fss.global_mask |= 1;
168     ++fss.count;
169     return;
170   }
171
172   // need to allocate specific mask bit(s) for this feature
173   unsigned int bits_needed = _hb_bit_storage(max_value);
174   if (!bits_needed || fss.next_bit + bits_needed > 8 * sizeof (hb_mask_t))
175   {
176     fss.info[fss.count].value = 0;
177     fss.info[fss.count].mask = 0;
178     ++fss.count;
179     return; // feature is disabled, just omit it; or
180             // not enough bits available in the mask, ignore this feature :(
181   }
182
183   // store the newly-allocated mask (in case further feature requests use the same tag)
184   // and shift the value into the right position
185   fss.info[fss.count].value = feature.value << fss.next_bit;
186   fss.info[fss.count].mask = (1 << (fss.next_bit + bits_needed)) - (1 << fss.next_bit);
187   if (feature.start == 0 && feature.end == (unsigned int) -1)
188     fss.global_mask |= fss.info[fss.count].value;
189   else
190     buffer->set_masks(fss.info[fss.count].value, fss.info[fss.count].mask, feature.start, feature.end);
191   fss.next_bit += bits_needed;
192   ++fss.count;
193 }
194
195 static void
196 setup_lookups (hb_face_t    *face,
197                hb_buffer_t  *buffer,
198                hb_feature_t *features,
199                unsigned int  num_features,
200                hb_tag_t      table_tag,
201                lookup_map   *lookups,
202                unsigned int *num_lookups,
203                hb_direction_t original_direction)
204 {
205   unsigned int i, j, script_index, language_index, feature_index, room_lookups;
206
207   room_lookups = *num_lookups;
208   *num_lookups = 0;
209
210   hb_ot_layout_table_choose_script (face, table_tag,
211                                     hb_ot_tags_from_script (buffer->script),
212                                     &script_index);
213   hb_ot_layout_script_find_language (face, table_tag, script_index,
214                                      hb_ot_tag_from_language (buffer->language),
215                                      &language_index);
216
217   if (hb_ot_layout_language_get_required_feature_index (face, table_tag, script_index, language_index,
218                                                         &feature_index))
219     add_feature (face, table_tag, feature_index, 1, lookups, num_lookups, room_lookups);
220
221   // for features that may be turned on/off or have value > 1,
222   // we need to allocate bits in the mask
223
224   feature_setup_state fss;
225   fss.global_mask = 0;
226   fss.next_bit = MASK_BITS_USED;
227   fss.count = 0;
228
229   hb_feature_t feature = { 0, 1, 0, (unsigned int)-1 };
230
231   switch (original_direction) {
232     case HB_DIRECTION_LTR:
233       feature.tag = HB_TAG ('l','t','r','a');
234       collect_feature_info(buffer, face, table_tag, script_index, language_index,
235                            feature, fss, features, num_features);
236       feature.tag = HB_TAG ('l','t','r','m');
237       collect_feature_info(buffer, face, table_tag, script_index, language_index,
238                            feature, fss, features, num_features);
239       break;
240     case HB_DIRECTION_RTL:
241       feature.tag = HB_TAG ('r','t','l','a');
242       collect_feature_info(buffer, face, table_tag, script_index, language_index,
243                            feature, fss, features, num_features);
244       break;
245     case HB_DIRECTION_TTB:
246     case HB_DIRECTION_BTT:
247     default:
248       break;
249   }
250
251   for (i = 0; i < ARRAY_LENGTH (default_features); i++)
252   {
253     feature.tag = default_features[i];
254     collect_feature_info(buffer, face, table_tag, script_index, language_index,
255                          feature, fss, features, num_features);
256   }
257
258   for (i = 0; i < num_features; i++)
259   {
260     collect_feature_info(buffer, face, table_tag, script_index, language_index,
261                          features[i], fss, features + i + 1, num_features - i - 1);
262   }
263
264   for (i = 0; i < fss.count; ++i)
265     if (fss.info[i].mask)
266       add_feature (face, table_tag, fss.info[i].index, fss.info[i].mask, lookups, num_lookups, room_lookups);
267
268   if (fss.global_mask > 1) // the 0x01 bit was set by clear_masks()
269     buffer->set_masks (fss.global_mask, fss.global_mask, 0, (unsigned int) -1);
270
271   qsort (lookups, *num_lookups, sizeof (lookups[0]), cmp_lookups);
272
273   if (*num_lookups)
274   {
275     for (i = 1, j = 0; i < *num_lookups; i++)
276       if (lookups[i].index != lookups[j].index)
277         lookups[++j] = lookups[i];
278       else
279         lookups[j].mask |= lookups[i].mask;
280     j++;
281     *num_lookups = j;
282   }
283 }
284
285
286 static hb_bool_t
287 hb_ot_substitute_complex (hb_font_t    *font HB_UNUSED,
288                           hb_face_t    *face,
289                           hb_buffer_t  *buffer,
290                           hb_feature_t *features,
291                           unsigned int  num_features,
292                           hb_direction_t original_direction)
293 {
294   lookup_map lookups[1000]; /* FIXME */
295   unsigned int num_lookups = ARRAY_LENGTH (lookups);
296   unsigned int i;
297
298   if (!hb_ot_layout_has_substitution (face))
299     return FALSE;
300
301   setup_lookups (face, buffer, features, num_features,
302                  HB_OT_TAG_GSUB,
303                  lookups, &num_lookups,
304                  original_direction);
305
306   for (i = 0; i < num_lookups; i++)
307     hb_ot_layout_substitute_lookup (face, buffer, lookups[i].index, lookups[i].mask);
308
309   return TRUE;
310 }
311
312 static hb_bool_t
313 hb_ot_position_complex (hb_font_t    *font,
314                         hb_face_t    *face,
315                         hb_buffer_t  *buffer,
316                         hb_feature_t *features,
317                         unsigned int  num_features,
318                         hb_direction_t original_direction)
319 {
320   lookup_map lookups[1000];
321   unsigned int num_lookups = ARRAY_LENGTH (lookups);
322   unsigned int i;
323
324   if (!hb_ot_layout_has_positioning (face))
325     return FALSE;
326
327   setup_lookups (face, buffer, features, num_features,
328                  HB_OT_TAG_GPOS,
329                  lookups, &num_lookups,
330                  original_direction);
331
332   for (i = 0; i < num_lookups; i++)
333     hb_ot_layout_position_lookup (font, face, buffer, lookups[i].index, lookups[i].mask);
334
335   hb_ot_layout_position_finish (font, face, buffer);
336
337   return TRUE;
338 }
339
340
341 /* Main shaper */
342
343 /* Prepare */
344
345 static inline hb_bool_t
346 is_variation_selector (hb_codepoint_t unicode)
347 {
348   return unlikely ((unicode >=  0x180B && unicode <=  0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
349                    (unicode >=  0xFE00 && unicode <=  0xFE0F) || /* VARIATION SELECTOR-1..16 */
350                    (unicode >= 0xE0100 && unicode <= 0xE01EF));  /* VARIATION SELECTOR-17..256 */
351 }
352
353 static void
354 hb_form_clusters (hb_buffer_t *buffer)
355 {
356   unsigned int count = buffer->len;
357   for (unsigned int i = 1; i < count; i++)
358     if (buffer->unicode->v.get_general_category (buffer->info[i].codepoint) == HB_CATEGORY_NON_SPACING_MARK)
359       buffer->info[i].cluster = buffer->info[i - 1].cluster;
360 }
361
362 static hb_direction_t
363 hb_ensure_native_direction (hb_buffer_t *buffer)
364 {
365   hb_direction_t original_direction = buffer->direction;
366
367   /* TODO vertical */
368   if (HB_DIRECTION_IS_HORIZONTAL (original_direction) &&
369       original_direction != _hb_script_get_horizontal_direction (buffer->script))
370   {
371     hb_buffer_reverse_clusters (buffer);
372     buffer->direction = HB_DIRECTION_REVERSE (buffer->direction);
373   }
374
375   return original_direction;
376 }
377
378
379 /* Substitute */
380
381 static void
382 hb_mirror_chars (hb_buffer_t *buffer)
383 {
384   hb_unicode_get_mirroring_func_t get_mirroring = buffer->unicode->v.get_mirroring;
385
386   if (HB_DIRECTION_IS_FORWARD (buffer->direction))
387     return;
388
389   unsigned int count = buffer->len;
390   for (unsigned int i = 0; i < count; i++) {
391     hb_codepoint_t codepoint = get_mirroring (buffer->info[i].codepoint);
392     if (likely (codepoint == buffer->info[i].codepoint))
393       buffer->info[i].mask |= MASK_RTLM;
394     else
395       buffer->info[i].codepoint = codepoint;
396   }
397 }
398
399 static void
400 hb_map_glyphs (hb_font_t    *font,
401                hb_face_t    *face,
402                hb_buffer_t  *buffer)
403 {
404   if (unlikely (!buffer->len))
405     return;
406
407   unsigned int count = buffer->len - 1;
408   for (unsigned int i = 0; i < count; i++) {
409     if (unlikely (is_variation_selector (buffer->info[i + 1].codepoint))) {
410       buffer->info[i].codepoint = hb_font_get_glyph (font, face, buffer->info[i].codepoint, buffer->info[i + 1].codepoint);
411       i++;
412     } else {
413       buffer->info[i].codepoint = hb_font_get_glyph (font, face, buffer->info[i].codepoint, 0);
414     }
415   }
416   buffer->info[count].codepoint = hb_font_get_glyph (font, face, buffer->info[count].codepoint, 0);
417 }
418
419 static void
420 hb_substitute_default (hb_font_t    *font,
421                        hb_face_t    *face,
422                        hb_buffer_t  *buffer,
423                        hb_feature_t *features HB_UNUSED,
424                        unsigned int  num_features HB_UNUSED)
425 {
426   hb_map_glyphs (font, face, buffer);
427 }
428
429 static void
430 hb_substitute_complex_fallback (hb_font_t    *font HB_UNUSED,
431                                 hb_face_t    *face HB_UNUSED,
432                                 hb_buffer_t  *buffer HB_UNUSED,
433                                 hb_feature_t *features HB_UNUSED,
434                                 unsigned int  num_features HB_UNUSED)
435 {
436   /* TODO Arabic */
437 }
438
439
440 /* Position */
441
442 static void
443 hb_position_default (hb_font_t    *font,
444                      hb_face_t    *face,
445                      hb_buffer_t  *buffer,
446                      hb_feature_t *features HB_UNUSED,
447                      unsigned int  num_features HB_UNUSED)
448 {
449   hb_buffer_clear_positions (buffer);
450
451   unsigned int count = buffer->len;
452   for (unsigned int i = 0; i < count; i++) {
453     hb_glyph_metrics_t metrics;
454     hb_font_get_glyph_metrics (font, face, buffer->info[i].codepoint, &metrics);
455     buffer->pos[i].x_advance = metrics.x_advance;
456     buffer->pos[i].y_advance = metrics.y_advance;
457   }
458 }
459
460 static void
461 hb_position_complex_fallback (hb_font_t    *font HB_UNUSED,
462                               hb_face_t    *face HB_UNUSED,
463                               hb_buffer_t  *buffer HB_UNUSED,
464                               hb_feature_t *features HB_UNUSED,
465                               unsigned int  num_features HB_UNUSED)
466 {
467   /* TODO Mark pos */
468 }
469
470 static void
471 hb_truetype_kern (hb_font_t    *font,
472                   hb_face_t    *face,
473                   hb_buffer_t  *buffer,
474                   hb_feature_t *features HB_UNUSED,
475                   unsigned int  num_features HB_UNUSED)
476 {
477   /* TODO Check for kern=0 */
478   unsigned int count = buffer->len;
479   for (unsigned int i = 1; i < count; i++) {
480     hb_position_t kern, kern1, kern2;
481     kern = hb_font_get_kerning (font, face, buffer->info[i - 1].codepoint, buffer->info[i].codepoint);
482     kern1 = kern >> 1;
483     kern2 = kern - kern1;
484     buffer->pos[i - 1].x_advance += kern1;
485     buffer->pos[i].x_advance += kern2;
486     buffer->pos[i].x_offset += kern2;
487   }
488 }
489
490 static void
491 hb_position_complex_fallback_visual (hb_font_t    *font,
492                                      hb_face_t    *face,
493                                      hb_buffer_t  *buffer,
494                                      hb_feature_t *features,
495                                      unsigned int  num_features)
496 {
497   hb_truetype_kern (font, face, buffer, features, num_features);
498 }
499
500
501 /* Do it! */
502
503 void
504 hb_ot_shape (hb_font_t    *font,
505              hb_face_t    *face,
506              hb_buffer_t  *buffer,
507              hb_feature_t *features,
508              unsigned int  num_features)
509 {
510   hb_direction_t original_direction;
511   hb_bool_t substitute_fallback, position_fallback;
512
513   hb_form_clusters (buffer);
514
515   /* SUBSTITUTE */
516
517   buffer->clear_masks ();
518
519   /* Mirroring needs to see the original direction */
520   hb_mirror_chars (buffer);
521
522   original_direction = hb_ensure_native_direction (buffer);
523
524   hb_substitute_default (font, face, buffer, features, num_features);
525
526   substitute_fallback = !hb_ot_substitute_complex (font, face, buffer, features, num_features, original_direction);
527
528   if (substitute_fallback)
529     hb_substitute_complex_fallback (font, face, buffer, features, num_features);
530
531
532   /* POSITION */
533
534   buffer->clear_masks ();
535
536   hb_position_default (font, face, buffer, features, num_features);
537
538   position_fallback = !hb_ot_position_complex (font, face, buffer, features, num_features, original_direction);
539
540   if (position_fallback)
541     hb_position_complex_fallback (font, face, buffer, features, num_features);
542
543   if (HB_DIRECTION_IS_BACKWARD (buffer->direction))
544     hb_buffer_reverse (buffer);
545
546   if (position_fallback)
547     hb_position_complex_fallback_visual (font, face, buffer, features, num_features);
548
549   buffer->direction = original_direction;
550 }