87cdf47757fe505b7ba1a891ba3ed34b23e8e7bf
[profile/ivi/org.tizen.video-player.git] / src / hb-ot-shape.cc
1 /*
2  * Copyright (C) 2009,2010  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-open-type-private.hh"
32
33 #include "hb-ot-layout.h"
34
35 /* XXX vertical */
36 hb_tag_t default_features[] = {
37   HB_TAG('c','a','l','t'),
38   HB_TAG('c','c','m','p'),
39   HB_TAG('c','l','i','g'),
40   HB_TAG('c','s','w','h'),
41   HB_TAG('c','u','r','s'),
42   HB_TAG('k','e','r','n'),
43   HB_TAG('l','i','g','a'),
44   HB_TAG('l','o','c','l'),
45   HB_TAG('m','a','r','k'),
46   HB_TAG('m','k','m','k'),
47   HB_TAG('r','l','i','g')
48 };
49
50 enum {
51   MASK_ALWAYS_ON = 1 << 0,
52   MASK_RTLM      = 1 << 1
53 };
54 #define MASK_BITS_USED 2
55
56 struct lookup_map {
57   unsigned int index;
58   hb_mask_t mask;
59 };
60
61
62 static void
63 add_feature (hb_face_t    *face,
64              hb_tag_t      table_tag,
65              unsigned int  feature_index,
66              hb_mask_t     mask,
67              lookup_map   *lookups,
68              unsigned int *num_lookups,
69              unsigned int  room_lookups)
70 {
71   unsigned int i = room_lookups - *num_lookups;
72   lookups += *num_lookups;
73
74   unsigned int *lookup_indices = (unsigned int *) lookups;
75
76   hb_ot_layout_feature_get_lookup_indexes (face, table_tag, feature_index, 0,
77                                            &i,
78                                            lookup_indices);
79
80   *num_lookups += i;
81
82   while (i--) {
83     lookups[i].mask = mask;
84     lookups[i].index = lookup_indices[i];
85   }
86 }
87
88 static int
89 cmp_lookups (const void *p1, const void *p2)
90 {
91   const lookup_map *a = (const lookup_map *) p1;
92   const lookup_map *b = (const lookup_map *) p2;
93
94   return a->index - b->index;
95 }
96
97
98 #define MAX_FEATURES 100
99
100 struct hb_mask_allocator_t {
101
102   struct feature_info_t {
103     hb_tag_t tag;
104     unsigned int value;
105     bool global;
106
107     static int
108     cmp (const void *p1, const void *p2)
109     {
110       const feature_info_t *a = (const feature_info_t *) p1;
111       const feature_info_t *b = (const feature_info_t *) p2;
112
113       if (a->tag != b->tag)
114         return a->tag < b->tag ? -1 : 1;
115
116       return p1 < p2 ? -1 : 1;
117     }
118   };
119
120   struct feature_map_t {
121     hb_tag_t tag; /* should be first */
122     unsigned int index;
123     unsigned int shift;
124     hb_mask_t mask;
125
126     static int
127     cmp (const void *p1, const void *p2)
128     {
129       const feature_map_t *a = (const feature_map_t *) p1;
130       const feature_map_t *b = (const feature_map_t *) p2;
131
132       return a->tag < b->tag ? -1 : a->tag > b->tag ? 1 : 0;
133     }
134   };
135
136   hb_mask_allocator_t (hb_face_t *face,
137                        hb_tag_t table_tag,
138                        unsigned int script_index,
139                        unsigned int language_index) :
140     face (face),
141     table_tag (table_tag),
142     script_index (script_index),
143     language_index (language_index),
144     count (0) {}
145
146   void add_feature (hb_tag_t tag,
147                     unsigned int value,
148                     bool global)
149   {
150     feature_info_t *info = &infos[count++];
151     info->tag = tag;
152     info->value = value;
153     info->global = global;
154   }
155
156   void compile (void)
157   {
158     global_mask = 0;
159     next_bit = MASK_BITS_USED;
160
161     if (!count)
162       return;
163
164     qsort (infos, count, sizeof (infos[0]), feature_info_t::cmp);
165
166     unsigned int j = 0;
167     for (unsigned int i = 1; i < count; i++)
168       if (infos[i].tag != infos[j].tag)
169         infos[++j] = infos[i];
170       else {
171         if (infos[i].global)
172           infos[j] = infos[i];
173         else {
174           infos[j].global = infos[j].global && (infos[j].value == infos[i].value);
175           infos[j].value = MAX (infos[j].value, infos[i].value);
176         }
177       }
178     count = j + 1;
179
180     /* Allocate bits now */
181     j = 0;
182     for (unsigned int i = 0; i < count; i++) {
183       const feature_info_t *info = &infos[i];
184
185       unsigned int bits_needed;
186
187       if (info->global && info->value == 1)
188         /* Uses the global bit */
189         bits_needed = 0;
190       else
191         bits_needed = _hb_bit_storage (info->value);
192
193       if (!info->value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
194         continue; /* Feature disabled, or not enough bits. */
195
196       unsigned int feature_index;
197       if (!hb_ot_layout_language_find_feature (face, table_tag, script_index, language_index,
198                                                info->tag, &feature_index))
199         continue;
200
201       feature_map_t *map = &maps[j++];
202
203       map->tag = info->tag;
204       map->index = feature_index;
205       if (info->global && info->value == 1) {
206         /* Uses the global bit */
207         map->shift = 0;
208         map->mask = 1;
209       } else {
210         map->shift = next_bit;
211         map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
212         next_bit += bits_needed;
213       }
214
215       if (info->global && map->mask != 1)
216         global_mask |= map->mask;
217     }
218     count = j;
219   }
220
221   hb_mask_t get_global_mask (void) { return global_mask; }
222   const feature_map_t *find_feature (hb_tag_t tag) const {
223     static const feature_map_t off_map = { HB_TAG_NONE, Index::NOT_FOUND_INDEX, 0, 0 };
224     const feature_map_t *map = (const feature_map_t *) bsearch (&tag, maps, count, sizeof (maps[0]), feature_map_t::cmp);
225     return map ? map : &off_map;
226   }
227
228
229   private:
230   hb_face_t *face;
231   hb_tag_t table_tag;
232   unsigned int script_index;
233   unsigned int language_index;
234
235   unsigned int count;
236   feature_info_t infos[MAX_FEATURES];
237   feature_map_t maps[MAX_FEATURES];
238
239   hb_mask_t global_mask;
240   unsigned int next_bit;
241 };
242
243 static void
244 setup_lookups (hb_face_t    *face,
245                hb_buffer_t  *buffer,
246                hb_feature_t *features,
247                unsigned int  num_features,
248                hb_tag_t      table_tag,
249                lookup_map   *lookups,
250                unsigned int *num_lookups,
251                hb_direction_t original_direction)
252 {
253   unsigned int i, j, script_index, language_index, feature_index, room_lookups;
254
255   room_lookups = *num_lookups;
256   *num_lookups = 0;
257
258   hb_ot_layout_table_choose_script (face, table_tag,
259                                     hb_ot_tags_from_script (buffer->script),
260                                     &script_index);
261   hb_ot_layout_script_find_language (face, table_tag, script_index,
262                                      hb_ot_tag_from_language (buffer->language),
263                                      &language_index);
264
265   if (hb_ot_layout_language_get_required_feature_index (face, table_tag, script_index, language_index,
266                                                         &feature_index))
267     add_feature (face, table_tag, feature_index, 1, lookups, num_lookups, room_lookups);
268
269
270   hb_mask_allocator_t allocator (face, table_tag, script_index, language_index);
271
272   switch (original_direction) {
273     case HB_DIRECTION_LTR:
274       allocator.add_feature (HB_TAG ('l','t','r','a'), 1, true);
275       allocator.add_feature (HB_TAG ('l','t','r','m'), 1, true);
276       break;
277     case HB_DIRECTION_RTL:
278       allocator.add_feature (HB_TAG ('r','t','l','a'), 1, true);
279       //allocator.add_feature (HB_TAG ('r','t','l','m'), false);
280       allocator.add_feature (HB_TAG ('r','t','l','m'), 1, true);
281       break;
282     case HB_DIRECTION_TTB:
283     case HB_DIRECTION_BTT:
284     default:
285       break;
286   }
287
288   for (i = 0; i < ARRAY_LENGTH (default_features); i++)
289     allocator.add_feature (default_features[i], 1, true);
290
291   /* XXX complex-shaper features go here */
292
293   for (unsigned int i = 0; i < num_features; i++) {
294     const hb_feature_t *feature = &features[i];
295     allocator.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
296   }
297
298
299   /* Compile features */
300   allocator.compile ();
301
302
303   /* Gather lookup indices for features and set buffer masks at the same time */
304
305   const hb_mask_allocator_t::feature_map_t *map;
306
307   hb_mask_t global_mask = allocator.get_global_mask ();
308   if (global_mask)
309     buffer->set_masks (global_mask, global_mask, 0, (unsigned int) -1);
310
311   switch (original_direction) {
312     case HB_DIRECTION_LTR:
313       map = allocator.find_feature (HB_TAG ('l','t','r','a'));
314       add_feature (face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
315       map = allocator.find_feature (HB_TAG ('l','t','r','m'));
316       add_feature (face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
317       break;
318     case HB_DIRECTION_RTL:
319       map = allocator.find_feature (HB_TAG ('r','t','l','a'));
320       add_feature (face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
321       //map = allocator.find_feature (HB_TAG ('r','t','l','m'));
322       add_feature (face, table_tag, map->index, MASK_RTLM, lookups, num_lookups, room_lookups);
323       break;
324     case HB_DIRECTION_TTB:
325     case HB_DIRECTION_BTT:
326     default:
327       break;
328   }
329
330   for (i = 0; i < ARRAY_LENGTH (default_features); i++)
331   {
332     map = allocator.find_feature (default_features[i]);
333     add_feature (face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
334   }
335
336   for (i = 0; i < num_features; i++)
337   {
338     hb_feature_t *feature = &features[i];
339     map = allocator.find_feature (feature->tag);
340     add_feature (face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
341     if (!(feature->start == 0 && feature->end == (unsigned int)-1))
342       buffer->set_masks (features[i].value << map->shift, map->mask, feature->start, feature->end);
343   }
344
345
346   /* Sort lookups and merge duplicates */
347
348   qsort (lookups, *num_lookups, sizeof (lookups[0]), cmp_lookups);
349
350   if (*num_lookups)
351   {
352     for (i = 1, j = 0; i < *num_lookups; i++)
353       if (lookups[i].index != lookups[j].index)
354         lookups[++j] = lookups[i];
355       else
356         lookups[j].mask |= lookups[i].mask;
357     j++;
358     *num_lookups = j;
359   }
360 }
361
362
363 static hb_bool_t
364 hb_ot_substitute_complex (hb_font_t    *font HB_UNUSED,
365                           hb_face_t    *face,
366                           hb_buffer_t  *buffer,
367                           hb_feature_t *features,
368                           unsigned int  num_features,
369                           hb_direction_t original_direction)
370 {
371   lookup_map lookups[1000]; /* FIXME */
372   unsigned int num_lookups = ARRAY_LENGTH (lookups);
373   unsigned int i;
374
375   if (!hb_ot_layout_has_substitution (face))
376     return FALSE;
377
378   setup_lookups (face, buffer, features, num_features,
379                  HB_OT_TAG_GSUB,
380                  lookups, &num_lookups,
381                  original_direction);
382
383   for (i = 0; i < num_lookups; i++)
384     hb_ot_layout_substitute_lookup (face, buffer, lookups[i].index, lookups[i].mask);
385
386   return TRUE;
387 }
388
389 static hb_bool_t
390 hb_ot_position_complex (hb_font_t    *font,
391                         hb_face_t    *face,
392                         hb_buffer_t  *buffer,
393                         hb_feature_t *features,
394                         unsigned int  num_features,
395                         hb_direction_t original_direction)
396 {
397   lookup_map lookups[1000];
398   unsigned int num_lookups = ARRAY_LENGTH (lookups);
399   unsigned int i;
400
401   if (!hb_ot_layout_has_positioning (face))
402     return FALSE;
403
404   setup_lookups (face, buffer, features, num_features,
405                  HB_OT_TAG_GPOS,
406                  lookups, &num_lookups,
407                  original_direction);
408
409   for (i = 0; i < num_lookups; i++)
410     hb_ot_layout_position_lookup (font, face, buffer, lookups[i].index, lookups[i].mask);
411
412   hb_ot_layout_position_finish (font, face, buffer);
413
414   return TRUE;
415 }
416
417
418 /* Main shaper */
419
420 /* Prepare */
421
422 static inline hb_bool_t
423 is_variation_selector (hb_codepoint_t unicode)
424 {
425   return unlikely ((unicode >=  0x180B && unicode <=  0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
426                    (unicode >=  0xFE00 && unicode <=  0xFE0F) || /* VARIATION SELECTOR-1..16 */
427                    (unicode >= 0xE0100 && unicode <= 0xE01EF));  /* VARIATION SELECTOR-17..256 */
428 }
429
430 static void
431 hb_form_clusters (hb_buffer_t *buffer)
432 {
433   unsigned int count = buffer->len;
434   for (unsigned int i = 1; i < count; i++)
435     if (buffer->unicode->v.get_general_category (buffer->info[i].codepoint) == HB_CATEGORY_NON_SPACING_MARK)
436       buffer->info[i].cluster = buffer->info[i - 1].cluster;
437 }
438
439 static hb_direction_t
440 hb_ensure_native_direction (hb_buffer_t *buffer)
441 {
442   hb_direction_t original_direction = buffer->direction;
443
444   /* TODO vertical */
445   if (HB_DIRECTION_IS_HORIZONTAL (original_direction) &&
446       original_direction != _hb_script_get_horizontal_direction (buffer->script))
447   {
448     hb_buffer_reverse_clusters (buffer);
449     buffer->direction = HB_DIRECTION_REVERSE (buffer->direction);
450   }
451
452   return original_direction;
453 }
454
455
456 /* Substitute */
457
458 static void
459 hb_mirror_chars (hb_buffer_t *buffer)
460 {
461   hb_unicode_get_mirroring_func_t get_mirroring = buffer->unicode->v.get_mirroring;
462
463   if (HB_DIRECTION_IS_FORWARD (buffer->direction))
464     return;
465
466   unsigned int count = buffer->len;
467   for (unsigned int i = 0; i < count; i++) {
468     hb_codepoint_t codepoint = get_mirroring (buffer->info[i].codepoint);
469     if (likely (codepoint == buffer->info[i].codepoint))
470       buffer->info[i].mask |= MASK_RTLM;
471     else
472       buffer->info[i].codepoint = codepoint;
473   }
474 }
475
476 static void
477 hb_map_glyphs (hb_font_t    *font,
478                hb_face_t    *face,
479                hb_buffer_t  *buffer)
480 {
481   if (unlikely (!buffer->len))
482     return;
483
484   unsigned int count = buffer->len - 1;
485   for (unsigned int i = 0; i < count; i++) {
486     if (unlikely (is_variation_selector (buffer->info[i + 1].codepoint))) {
487       buffer->info[i].codepoint = hb_font_get_glyph (font, face, buffer->info[i].codepoint, buffer->info[i + 1].codepoint);
488       i++;
489     } else {
490       buffer->info[i].codepoint = hb_font_get_glyph (font, face, buffer->info[i].codepoint, 0);
491     }
492   }
493   buffer->info[count].codepoint = hb_font_get_glyph (font, face, buffer->info[count].codepoint, 0);
494 }
495
496 static void
497 hb_substitute_default (hb_font_t    *font,
498                        hb_face_t    *face,
499                        hb_buffer_t  *buffer,
500                        hb_feature_t *features HB_UNUSED,
501                        unsigned int  num_features HB_UNUSED)
502 {
503   hb_map_glyphs (font, face, buffer);
504 }
505
506 static void
507 hb_substitute_complex_fallback (hb_font_t    *font HB_UNUSED,
508                                 hb_face_t    *face HB_UNUSED,
509                                 hb_buffer_t  *buffer HB_UNUSED,
510                                 hb_feature_t *features HB_UNUSED,
511                                 unsigned int  num_features HB_UNUSED)
512 {
513   /* TODO Arabic */
514 }
515
516
517 /* Position */
518
519 static void
520 hb_position_default (hb_font_t    *font,
521                      hb_face_t    *face,
522                      hb_buffer_t  *buffer,
523                      hb_feature_t *features HB_UNUSED,
524                      unsigned int  num_features HB_UNUSED)
525 {
526   hb_buffer_clear_positions (buffer);
527
528   unsigned int count = buffer->len;
529   for (unsigned int i = 0; i < count; i++) {
530     hb_glyph_metrics_t metrics;
531     hb_font_get_glyph_metrics (font, face, buffer->info[i].codepoint, &metrics);
532     buffer->pos[i].x_advance = metrics.x_advance;
533     buffer->pos[i].y_advance = metrics.y_advance;
534   }
535 }
536
537 static void
538 hb_position_complex_fallback (hb_font_t    *font HB_UNUSED,
539                               hb_face_t    *face HB_UNUSED,
540                               hb_buffer_t  *buffer HB_UNUSED,
541                               hb_feature_t *features HB_UNUSED,
542                               unsigned int  num_features HB_UNUSED)
543 {
544   /* TODO Mark pos */
545 }
546
547 static void
548 hb_truetype_kern (hb_font_t    *font,
549                   hb_face_t    *face,
550                   hb_buffer_t  *buffer,
551                   hb_feature_t *features HB_UNUSED,
552                   unsigned int  num_features HB_UNUSED)
553 {
554   /* TODO Check for kern=0 */
555   unsigned int count = buffer->len;
556   for (unsigned int i = 1; i < count; i++) {
557     hb_position_t kern, kern1, kern2;
558     kern = hb_font_get_kerning (font, face, buffer->info[i - 1].codepoint, buffer->info[i].codepoint);
559     kern1 = kern >> 1;
560     kern2 = kern - kern1;
561     buffer->pos[i - 1].x_advance += kern1;
562     buffer->pos[i].x_advance += kern2;
563     buffer->pos[i].x_offset += kern2;
564   }
565 }
566
567 static void
568 hb_position_complex_fallback_visual (hb_font_t    *font,
569                                      hb_face_t    *face,
570                                      hb_buffer_t  *buffer,
571                                      hb_feature_t *features,
572                                      unsigned int  num_features)
573 {
574   hb_truetype_kern (font, face, buffer, features, num_features);
575 }
576
577
578 /* Do it! */
579
580 void
581 hb_ot_shape (hb_font_t    *font,
582              hb_face_t    *face,
583              hb_buffer_t  *buffer,
584              hb_feature_t *features,
585              unsigned int  num_features)
586 {
587   hb_direction_t original_direction;
588   hb_bool_t substitute_fallback, position_fallback;
589
590   hb_form_clusters (buffer);
591
592   /* SUBSTITUTE */
593
594   buffer->clear_masks ();
595
596   /* Mirroring needs to see the original direction */
597   hb_mirror_chars (buffer);
598
599   original_direction = hb_ensure_native_direction (buffer);
600
601   hb_substitute_default (font, face, buffer, features, num_features);
602
603   substitute_fallback = !hb_ot_substitute_complex (font, face, buffer, features, num_features, original_direction);
604
605   if (substitute_fallback)
606     hb_substitute_complex_fallback (font, face, buffer, features, num_features);
607
608
609   /* POSITION */
610
611   buffer->clear_masks ();
612
613   hb_position_default (font, face, buffer, features, num_features);
614
615   position_fallback = !hb_ot_position_complex (font, face, buffer, features, num_features, original_direction);
616
617   if (position_fallback)
618     hb_position_complex_fallback (font, face, buffer, features, num_features);
619
620   if (HB_DIRECTION_IS_BACKWARD (buffer->direction))
621     hb_buffer_reverse (buffer);
622
623   if (position_fallback)
624     hb_position_complex_fallback_visual (font, face, buffer, features, num_features);
625
626   buffer->direction = original_direction;
627 }