More separation
[framework/uifw/harfbuzz.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
31 #include "hb-ot-map-private.hh"
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 static void
52 hb_ot_shape_collect_features (hb_ot_shape_context_t *c)
53 {
54   switch (c->original_direction) {
55     case HB_DIRECTION_LTR:
56       c->map->add_bool_feature (HB_TAG ('l','t','r','a'));
57       c->map->add_bool_feature (HB_TAG ('l','t','r','m'));
58       break;
59     case HB_DIRECTION_RTL:
60       c->map->add_bool_feature (HB_TAG ('r','t','l','a'));
61       c->map->add_bool_feature (HB_TAG ('r','t','l','m'), false);
62       break;
63     case HB_DIRECTION_TTB:
64     case HB_DIRECTION_BTT:
65     default:
66       break;
67   }
68
69   for (unsigned int i = 0; i < ARRAY_LENGTH (default_features); i++)
70     c->map->add_bool_feature (default_features[i]);
71
72   /* complex */
73
74   for (unsigned int i = 0; i < c->num_features; i++) {
75     const hb_feature_t *feature = &c->features[i];
76     c->map->add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
77   }
78 }
79
80
81 static void
82 hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
83 {
84   hb_mask_t global_mask = c->map->get_global_mask ();
85   if (global_mask)
86     c->buffer->set_masks (global_mask, global_mask, 0, (unsigned int) -1);
87
88   for (unsigned int i = 0; i < c->num_features; i++)
89   {
90     hb_feature_t *feature = &c->features[i];
91     if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
92       unsigned int shift;
93       hb_mask_t mask = c->map->get_mask (feature->tag, &shift);
94       c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
95     }
96   }
97
98   /* complex */
99 }
100
101
102 static void
103 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
104 {
105   if (!hb_ot_layout_has_substitution (c->face))
106     return;
107
108   c->map->substitute (c);
109
110   c->applied_substitute_complex = TRUE;
111   return;
112 }
113
114 static void
115 hb_ot_position_complex (hb_ot_shape_context_t *c)
116 {
117
118   if (!hb_ot_layout_has_positioning (c->face))
119     return;
120
121   c->map->position (c);
122
123   hb_ot_layout_position_finish (c->font, c->face, c->buffer);
124
125   c->applied_position_complex = TRUE;
126   return;
127 }
128
129
130 /* Main shaper */
131
132 /* Prepare */
133
134 static inline hb_bool_t
135 is_variation_selector (hb_codepoint_t unicode)
136 {
137   return unlikely ((unicode >=  0x180B && unicode <=  0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
138                    (unicode >=  0xFE00 && unicode <=  0xFE0F) || /* VARIATION SELECTOR-1..16 */
139                    (unicode >= 0xE0100 && unicode <= 0xE01EF));  /* VARIATION SELECTOR-17..256 */
140 }
141
142 static void
143 hb_form_clusters (hb_ot_shape_context_t *c)
144 {
145   unsigned int count = c->buffer->len;
146   for (unsigned int i = 1; i < count; i++)
147     if (c->buffer->unicode->v.get_general_category (c->buffer->info[i].codepoint) == HB_CATEGORY_NON_SPACING_MARK)
148       c->buffer->info[i].cluster = c->buffer->info[i - 1].cluster;
149 }
150
151 static void
152 hb_ensure_native_direction (hb_ot_shape_context_t *c)
153 {
154   hb_direction_t direction = c->buffer->props.direction;
155
156   /* TODO vertical */
157   if (HB_DIRECTION_IS_HORIZONTAL (direction) &&
158       direction != _hb_script_get_horizontal_direction (c->buffer->props.script))
159   {
160     hb_buffer_reverse_clusters (c->buffer);
161     c->buffer->props.direction = HB_DIRECTION_REVERSE (c->buffer->props.direction);
162   }
163 }
164
165
166 /* Substitute */
167
168 static void
169 hb_mirror_chars (hb_ot_shape_context_t *c)
170 {
171   hb_unicode_get_mirroring_func_t get_mirroring = c->buffer->unicode->v.get_mirroring;
172
173   if (HB_DIRECTION_IS_FORWARD (c->buffer->props.direction))
174     return;
175
176   hb_mask_t rtlm_mask = c->map->get_mask (HB_TAG ('r','t','l','m'));
177
178   unsigned int count = c->buffer->len;
179   for (unsigned int i = 0; i < count; i++) {
180     hb_codepoint_t codepoint = get_mirroring (c->buffer->info[i].codepoint);
181     if (likely (codepoint == c->buffer->info[i].codepoint))
182       c->buffer->info[i].mask |= rtlm_mask;
183     else
184       c->buffer->info[i].codepoint = codepoint;
185   }
186 }
187
188 static void
189 hb_map_glyphs (hb_font_t    *font,
190                hb_face_t    *face,
191                hb_buffer_t  *buffer)
192 {
193   if (unlikely (!buffer->len))
194     return;
195
196   buffer->clear_output ();
197   unsigned int count = buffer->len - 1;
198   for (buffer->i = 0; buffer->i < count;) {
199     if (unlikely (is_variation_selector (buffer->info[buffer->i + 1].codepoint))) {
200       buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, buffer->info[buffer->i + 1].codepoint));
201       buffer->i++;
202     } else {
203       buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
204     }
205   }
206   if (likely (buffer->i < buffer->len))
207     buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
208   buffer->swap ();
209 }
210
211 static void
212 hb_substitute_default (hb_ot_shape_context_t *c)
213 {
214   hb_map_glyphs (c->font, c->face, c->buffer);
215 }
216
217 static void
218 hb_substitute_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
219 {
220   /* TODO Arabic */
221 }
222
223
224 /* Position */
225
226 static void
227 hb_position_default (hb_ot_shape_context_t *c)
228 {
229   hb_buffer_clear_positions (c->buffer);
230
231   unsigned int count = c->buffer->len;
232   for (unsigned int i = 0; i < count; i++) {
233     hb_glyph_metrics_t metrics;
234     hb_font_get_glyph_metrics (c->font, c->face, c->buffer->info[i].codepoint, &metrics);
235     c->buffer->pos[i].x_advance = metrics.x_advance;
236     c->buffer->pos[i].y_advance = metrics.y_advance;
237   }
238 }
239
240 static void
241 hb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
242 {
243   /* TODO Mark pos */
244 }
245
246 static void
247 hb_truetype_kern (hb_ot_shape_context_t *c)
248 {
249   /* TODO Check for kern=0 */
250   unsigned int count = c->buffer->len;
251   for (unsigned int i = 1; i < count; i++) {
252     hb_position_t kern, kern1, kern2;
253     kern = hb_font_get_kerning (c->font, c->face, c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint);
254     kern1 = kern >> 1;
255     kern2 = kern - kern1;
256     c->buffer->pos[i - 1].x_advance += kern1;
257     c->buffer->pos[i].x_advance += kern2;
258     c->buffer->pos[i].x_offset += kern2;
259   }
260 }
261
262 static void
263 hb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
264 {
265   hb_truetype_kern (c);
266 }
267
268
269 /* Do it! */
270
271 static void
272 hb_ot_shape_internal (hb_ot_shape_context_t *c)
273 {
274   hb_ot_shape_setup_masks (c);
275
276   hb_form_clusters (c);
277
278   /* SUBSTITUTE */
279   {
280     c->buffer->clear_masks ();
281
282     /* Mirroring needs to see the original direction */
283     hb_mirror_chars (c);
284
285     hb_ensure_native_direction (c);
286
287     hb_substitute_default (c);
288
289     hb_ot_substitute_complex (c);
290
291     if (!c->applied_substitute_complex)
292       hb_substitute_complex_fallback (c);
293   }
294
295   /* POSITION */
296   {
297     c->buffer->clear_masks ();
298
299     hb_position_default (c);
300
301     hb_ot_position_complex (c);
302
303     hb_bool_t position_fallback = !c->applied_position_complex;
304     if (position_fallback)
305       hb_position_complex_fallback (c);
306
307     if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
308       hb_buffer_reverse (c->buffer);
309
310     if (position_fallback)
311       hb_position_complex_fallback_visual (c);
312   }
313
314   c->buffer->props.direction = c->original_direction;
315 }
316
317 void
318 hb_ot_shape (hb_font_t    *font,
319              hb_face_t    *face,
320              hb_buffer_t  *buffer,
321              hb_feature_t *features,
322              unsigned int  num_features)
323 {
324   hb_ot_shape_context_t c = {font, face, buffer, features, num_features};
325   hb_ot_map_t map;
326
327   /* Setup transient context members */
328   c.original_direction = buffer->props.direction;
329   c.map = &map;
330
331   hb_ot_shape_collect_features (&c);
332   c.map->compile (&c);
333
334   hb_ot_shape_internal (&c);
335 }
336
337
338 HB_END_DECLS