WIP removing external synthesized GDEF support and implementing it internally
[profile/ivi/org.tizen.video-player.git] / src / hb-ot-shape.cc
1 /*
2  * Copyright (C) 2009,2010  Red Hat, Inc.
3  * Copyright (C) 2010  Google, Inc.
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
7  * Permission is hereby granted, without written agreement and without
8  * license or royalty fees, to use, copy, modify, and distribute this
9  * software and its documentation for any purpose, provided that the
10  * above copyright notice and the following two paragraphs appear in
11  * all copies of this software.
12  *
13  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17  * DAMAGE.
18  *
19  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24  *
25  * Red Hat Author(s): Behdad Esfahbod
26  * Google Author(s): Behdad Esfahbod
27  */
28
29 #include "hb-ot-shape-private.hh"
30 #include "hb-ot-shape-complex-private.hh"
31
32 HB_BEGIN_DECLS
33
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 static void
51 hb_ot_shape_collect_features (hb_ot_shape_plan_t       *plan,
52                               const hb_segment_properties_t  *props,
53                               const hb_feature_t       *user_features,
54                               unsigned int              num_user_features)
55 {
56   switch (props->direction) {
57     case HB_DIRECTION_LTR:
58       plan->map.add_bool_feature (HB_TAG ('l','t','r','a'));
59       plan->map.add_bool_feature (HB_TAG ('l','t','r','m'));
60       break;
61     case HB_DIRECTION_RTL:
62       plan->map.add_bool_feature (HB_TAG ('r','t','l','a'));
63       plan->map.add_bool_feature (HB_TAG ('r','t','l','m'), false);
64       break;
65     case HB_DIRECTION_TTB:
66     case HB_DIRECTION_BTT:
67     default:
68       break;
69   }
70
71   for (unsigned int i = 0; i < ARRAY_LENGTH (default_features); i++)
72     plan->map.add_bool_feature (default_features[i]);
73
74   hb_ot_shape_complex_collect_features (plan, props);
75
76   for (unsigned int i = 0; i < num_user_features; i++) {
77     const hb_feature_t *feature = &user_features[i];
78     plan->map.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
79   }
80 }
81
82
83 static void
84 hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
85 {
86   hb_mask_t global_mask = c->plan->map.get_global_mask ();
87   c->buffer->reset_masks (global_mask);
88
89   hb_ot_shape_complex_setup_masks (c);
90
91   for (unsigned int i = 0; i < c->num_user_features; i++)
92   {
93     const hb_feature_t *feature = &c->user_features[i];
94     if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
95       unsigned int shift;
96       hb_mask_t mask = c->plan->map.get_mask (feature->tag, &shift);
97       c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
98     }
99   }
100 }
101
102
103 static void
104 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
105 {
106   if (!hb_ot_layout_has_substitution (c->face))
107     return;
108
109   c->plan->map.substitute (c->face, c->buffer);
110
111   c->applied_substitute_complex = TRUE;
112   return;
113 }
114
115 static void
116 hb_ot_position_complex (hb_ot_shape_context_t *c)
117 {
118
119   if (!hb_ot_layout_has_positioning (c->face))
120     return;
121
122   c->plan->map.position (c->font, c->face, c->buffer);
123
124   hb_ot_layout_position_finish (c->font, c->face, c->buffer);
125
126   c->applied_position_complex = TRUE;
127   return;
128 }
129
130
131 /* Main shaper */
132
133 /* Prepare */
134
135 static inline hb_bool_t
136 is_variation_selector (hb_codepoint_t unicode)
137 {
138   return unlikely ((unicode >=  0x180B && unicode <=  0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
139                    (unicode >=  0xFE00 && unicode <=  0xFE0F) || /* VARIATION SELECTOR-1..16 */
140                    (unicode >= 0xE0100 && unicode <= 0xE01EF));  /* VARIATION SELECTOR-17..256 */
141 }
142
143 static void
144 hb_form_clusters (hb_ot_shape_context_t *c)
145 {
146   unsigned int count = c->buffer->len;
147   for (unsigned int i = 1; i < count; i++)
148     if (c->buffer->unicode->v.get_general_category (c->buffer->info[i].codepoint) == HB_CATEGORY_NON_SPACING_MARK)
149       c->buffer->info[i].cluster = c->buffer->info[i - 1].cluster;
150 }
151
152 static void
153 hb_ensure_native_direction (hb_ot_shape_context_t *c)
154 {
155   hb_direction_t direction = c->buffer->props.direction;
156
157   /* TODO vertical */
158   if (HB_DIRECTION_IS_HORIZONTAL (direction) &&
159       direction != _hb_script_get_horizontal_direction (c->buffer->props.script))
160   {
161     hb_buffer_reverse_clusters (c->buffer);
162     c->buffer->props.direction = HB_DIRECTION_REVERSE (c->buffer->props.direction);
163   }
164 }
165
166 static void
167 hb_reset_glyph_infos (hb_ot_shape_context_t *c)
168 {
169   unsigned int count = c->buffer->len;
170   for (unsigned int i = 0; i < count; i++)
171     c->buffer->info[i].var1.u32 = c->buffer->info[i].var2.u32 = 0;
172 }
173
174
175 /* Substitute */
176
177 static void
178 hb_mirror_chars (hb_ot_shape_context_t *c)
179 {
180   hb_unicode_get_mirroring_func_t get_mirroring = c->buffer->unicode->v.get_mirroring;
181
182   if (HB_DIRECTION_IS_FORWARD (c->buffer->props.direction))
183     return;
184
185   hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m'));
186
187   unsigned int count = c->buffer->len;
188   for (unsigned int i = 0; i < count; i++) {
189     hb_codepoint_t codepoint = get_mirroring (c->buffer->info[i].codepoint);
190     if (likely (codepoint == c->buffer->info[i].codepoint))
191       c->buffer->info[i].mask |= rtlm_mask; /* XXX this should be moved to before setting user-feature masks */
192     else
193       c->buffer->info[i].codepoint = codepoint;
194   }
195 }
196
197 static void
198 hb_map_glyphs (hb_font_t    *font,
199                hb_face_t    *face,
200                hb_buffer_t  *buffer)
201 {
202   if (unlikely (!buffer->len))
203     return;
204
205   buffer->clear_output ();
206   unsigned int count = buffer->len - 1;
207   for (buffer->i = 0; buffer->i < count;) {
208     if (unlikely (is_variation_selector (buffer->info[buffer->i + 1].codepoint))) {
209       buffer->replace_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, buffer->info[buffer->i + 1].codepoint));
210       buffer->i++;
211     } else {
212       buffer->replace_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
213     }
214   }
215   if (likely (buffer->i < buffer->len))
216     buffer->replace_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
217   buffer->swap ();
218 }
219
220 static void
221 hb_substitute_default (hb_ot_shape_context_t *c)
222 {
223   hb_map_glyphs (c->font, c->face, c->buffer);
224 }
225
226 static void
227 hb_substitute_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
228 {
229   /* TODO Arabic */
230 }
231
232
233 /* Position */
234
235 static void
236 hb_position_default (hb_ot_shape_context_t *c)
237 {
238   hb_buffer_clear_positions (c->buffer);
239
240   unsigned int count = c->buffer->len;
241   for (unsigned int i = 0; i < count; i++) {
242     hb_font_get_glyph_advance (c->font, c->face, c->buffer->info[i].codepoint,
243                                &c->buffer->pos[i].x_advance,
244                                &c->buffer->pos[i].y_advance);
245   }
246 }
247
248 static void
249 hb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
250 {
251   /* TODO Mark pos */
252 }
253
254 static void
255 hb_truetype_kern (hb_ot_shape_context_t *c)
256 {
257   /* TODO Check for kern=0 */
258   unsigned int count = c->buffer->len;
259   for (unsigned int i = 1; i < count; i++) {
260     hb_position_t kern, kern1, kern2;
261     kern = hb_font_get_kerning (c->font, c->face, c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint);
262     kern1 = kern >> 1;
263     kern2 = kern - kern1;
264     c->buffer->pos[i - 1].x_advance += kern1;
265     c->buffer->pos[i].x_advance += kern2;
266     c->buffer->pos[i].x_offset += kern2;
267   }
268 }
269
270 static void
271 hb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
272 {
273   hb_truetype_kern (c);
274 }
275
276
277 /* Do it! */
278
279 static void
280 hb_ot_shape_execute_internal (hb_ot_shape_context_t *c)
281 {
282   /* Save the original direction, we use it later. */
283   c->original_direction = c->buffer->props.direction;
284
285   hb_form_clusters (c);
286
287   hb_ot_shape_setup_masks (c);
288
289   hb_reset_glyph_infos (c);
290
291   /* SUBSTITUTE */
292   {
293     /* Mirroring needs to see the original direction */
294     hb_mirror_chars (c);
295
296     hb_ensure_native_direction (c);
297
298     hb_substitute_default (c);
299
300     hb_ot_substitute_complex (c);
301
302     if (!c->applied_substitute_complex)
303       hb_substitute_complex_fallback (c);
304   }
305
306   /* POSITION */
307   {
308     hb_position_default (c);
309
310     hb_ot_position_complex (c);
311
312     hb_bool_t position_fallback = !c->applied_position_complex;
313     if (position_fallback)
314       hb_position_complex_fallback (c);
315
316     if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
317       hb_buffer_reverse (c->buffer);
318
319     if (position_fallback)
320       hb_position_complex_fallback_visual (c);
321   }
322
323   c->buffer->props.direction = c->original_direction;
324 }
325
326 void
327 hb_ot_shape_plan_internal (hb_ot_shape_plan_t       *plan,
328                            hb_face_t                *face,
329                            const hb_segment_properties_t  *props,
330                            const hb_feature_t       *user_features,
331                            unsigned int              num_user_features)
332 {
333   plan->shaper = hb_ot_shape_complex_categorize (props);
334
335   hb_ot_shape_collect_features (plan, props, user_features, num_user_features);
336
337   plan->map.compile (face, props);
338 }
339
340 void
341 hb_ot_shape_execute (hb_ot_shape_plan_t *plan,
342                      hb_font_t          *font,
343                      hb_face_t          *face,
344                      hb_buffer_t        *buffer,
345                      const hb_feature_t *user_features,
346                      unsigned int        num_user_features)
347 {
348   hb_ot_shape_context_t c = {plan, font, face, buffer, user_features, num_user_features};
349   hb_ot_shape_execute_internal (&c);
350 }
351
352 void
353 hb_ot_shape (hb_font_t    *font,
354              hb_face_t    *face,
355              hb_buffer_t  *buffer,
356              const hb_feature_t *user_features,
357              unsigned int        num_user_features)
358 {
359   hb_ot_shape_plan_t plan;
360
361   hb_ot_shape_plan_internal (&plan, face, &buffer->props, user_features, num_user_features);
362   hb_ot_shape_execute (&plan, font, face, buffer, user_features, num_user_features);
363 }
364
365
366 HB_END_DECLS