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