hb-ot-shape.cc revision 8b38faeede41e64eb0f6ac2e12ce51dd7138d50a
1/*
2 * Copyright © 2009,2010  Red Hat, Inc.
3 * Copyright © 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#include "hb-font-private.hh"
33
34HB_BEGIN_DECLS
35
36
37/* XXX vertical */
38hb_tag_t default_features[] = {
39  HB_TAG('c','a','l','t'),
40  HB_TAG('c','c','m','p'),
41  HB_TAG('c','l','i','g'),
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
51static void
52hb_ot_shape_collect_features (hb_ot_shape_plan_t       *plan,
53			      const hb_segment_properties_t  *props,
54			      const hb_feature_t       *user_features,
55			      unsigned int              num_user_features)
56{
57  switch (props->direction) {
58    case HB_DIRECTION_LTR:
59      plan->map.add_bool_feature (HB_TAG ('l','t','r','a'));
60      plan->map.add_bool_feature (HB_TAG ('l','t','r','m'));
61      break;
62    case HB_DIRECTION_RTL:
63      plan->map.add_bool_feature (HB_TAG ('r','t','l','a'));
64      plan->map.add_bool_feature (HB_TAG ('r','t','l','m'), false);
65      break;
66    case HB_DIRECTION_TTB:
67    case HB_DIRECTION_BTT:
68    case HB_DIRECTION_INVALID:
69    default:
70      break;
71  }
72
73  for (unsigned int i = 0; i < ARRAY_LENGTH (default_features); i++)
74    plan->map.add_bool_feature (default_features[i]);
75
76  hb_ot_shape_complex_collect_features (plan, props);
77
78  for (unsigned int i = 0; i < num_user_features; i++) {
79    const hb_feature_t *feature = &user_features[i];
80    plan->map.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
81  }
82}
83
84
85static void
86hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
87{
88  hb_mask_t global_mask = c->plan->map.get_global_mask ();
89  c->buffer->reset_masks (global_mask);
90
91  hb_ot_shape_complex_setup_masks (c); /* BUFFER: Clobbers var2 */
92
93  for (unsigned int i = 0; i < c->num_user_features; i++)
94  {
95    const hb_feature_t *feature = &c->user_features[i];
96    if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
97      unsigned int shift;
98      hb_mask_t mask = c->plan->map.get_mask (feature->tag, &shift);
99      c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
100    }
101  }
102}
103
104
105static void
106hb_ot_substitute_complex (hb_ot_shape_context_t *c)
107{
108  if (!hb_ot_layout_has_substitution (c->face))
109    return;
110
111  c->plan->map.substitute (c->face, c->buffer);
112
113  c->applied_substitute_complex = TRUE;
114  return;
115}
116
117static void
118hb_ot_position_complex (hb_ot_shape_context_t *c)
119{
120
121  if (!hb_ot_layout_has_positioning (c->face))
122    return;
123
124  c->plan->map.position (c->font, c->face, c->buffer);
125
126  hb_ot_layout_position_finish (c->buffer);
127
128  c->applied_position_complex = TRUE;
129  return;
130}
131
132
133/* Main shaper */
134
135/* Prepare */
136
137static inline hb_bool_t
138is_variation_selector (hb_codepoint_t unicode)
139{
140  return unlikely ((unicode >=  0x180B && unicode <=  0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
141		   (unicode >=  0xFE00 && unicode <=  0xFE0F) || /* VARIATION SELECTOR-1..16 */
142		   (unicode >= 0xE0100 && unicode <= 0xE01EF));  /* VARIATION SELECTOR-17..256 */
143}
144
145static void
146hb_set_unicode_props (hb_ot_shape_context_t *c)
147{
148  hb_unicode_funcs_t *unicode = c->buffer->unicode;
149  hb_glyph_info_t *info = c->buffer->info;
150
151  unsigned int count = c->buffer->len;
152  for (unsigned int i = 1; i < count; i++) {
153    info[i].general_category() = unicode->get_general_category (info[i].codepoint);
154    info[i].combining_class() = unicode->get_combining_class (info[i].codepoint);
155  }
156}
157
158static void
159hb_form_clusters (hb_ot_shape_context_t *c)
160{
161  unsigned int count = c->buffer->len;
162  for (unsigned int i = 1; i < count; i++)
163    if (c->buffer->info[i].general_category() == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
164      c->buffer->info[i].cluster = c->buffer->info[i - 1].cluster;
165}
166
167static void
168hb_ensure_native_direction (hb_ot_shape_context_t *c)
169{
170  hb_direction_t direction = c->buffer->props.direction;
171
172  /* XXX vertical */
173  if (HB_DIRECTION_IS_HORIZONTAL (direction) &&
174      direction != hb_script_get_horizontal_direction (c->buffer->props.script))
175  {
176    hb_buffer_reverse_clusters (c->buffer);
177    c->buffer->props.direction = HB_DIRECTION_REVERSE (c->buffer->props.direction);
178  }
179}
180
181static void
182hb_reset_glyph_infos (hb_ot_shape_context_t *c)
183{
184  unsigned int count = c->buffer->len;
185  for (unsigned int i = 0; i < count; i++)
186    c->buffer->info[i].var1.u32 = c->buffer->info[i].var2.u32 = 0;
187}
188
189
190/* Substitute */
191
192static void
193hb_mirror_chars (hb_ot_shape_context_t *c)
194{
195  hb_unicode_funcs_t *unicode = c->buffer->unicode;
196
197  if (HB_DIRECTION_IS_FORWARD (c->target_direction))
198    return;
199
200  hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m'));
201
202  unsigned int count = c->buffer->len;
203  for (unsigned int i = 0; i < count; i++) {
204    hb_codepoint_t codepoint = unicode->get_mirroring (c->buffer->info[i].codepoint);
205    if (likely (codepoint == c->buffer->info[i].codepoint))
206      c->buffer->info[i].mask |= rtlm_mask; /* XXX this should be moved to before setting user-feature masks */
207    else
208      c->buffer->info[i].codepoint = codepoint;
209  }
210}
211
212static void
213hb_map_glyphs (hb_font_t    *font,
214	       hb_buffer_t  *buffer)
215{
216  if (unlikely (!buffer->len))
217    return;
218
219  hb_codepoint_t glyph;
220  buffer->clear_output ();
221  unsigned int count = buffer->len - 1;
222  for (buffer->i = 0; buffer->i < count;) {
223    if (unlikely (is_variation_selector (buffer->info[buffer->i + 1].codepoint))) {
224      hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, buffer->info[buffer->i + 1].codepoint, &glyph);
225      buffer->replace_glyph (glyph);
226      buffer->i++;
227    } else {
228      hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, 0, &glyph);
229      buffer->replace_glyph (glyph);
230    }
231  }
232  if (likely (buffer->i < buffer->len)) {
233    hb_font_get_glyph (font, buffer->info[buffer->i].codepoint, 0, &glyph);
234    buffer->replace_glyph (glyph);
235  }
236  buffer->swap ();
237}
238
239static void
240hb_substitute_default (hb_ot_shape_context_t *c)
241{
242  hb_map_glyphs (c->font, c->buffer);
243}
244
245static void
246hb_substitute_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
247{
248  /* TODO Arabic */
249}
250
251
252/* Position */
253
254static void
255hb_position_default (hb_ot_shape_context_t *c)
256{
257  hb_ot_layout_position_start (c->buffer);
258
259  unsigned int count = c->buffer->len;
260  for (unsigned int i = 0; i < count; i++) {
261    hb_font_get_glyph_advance_for_direction (c->font, c->buffer->info[i].codepoint,
262					     c->buffer->props.direction,
263					     &c->buffer->pos[i].x_advance,
264					     &c->buffer->pos[i].y_advance);
265    hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
266						 c->buffer->props.direction,
267						 &c->buffer->pos[i].x_offset,
268						 &c->buffer->pos[i].y_offset);
269  }
270}
271
272static void
273hb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
274{
275  /* TODO Mark pos */
276}
277
278static void
279hb_truetype_kern (hb_ot_shape_context_t *c)
280{
281  /* TODO Check for kern=0 */
282  unsigned int count = c->buffer->len;
283  for (unsigned int i = 1; i < count; i++) {
284    hb_position_t x_kern, y_kern, kern1, kern2;
285    hb_font_get_glyph_kerning_for_direction (c->font,
286					     c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint,
287					     c->buffer->props.direction,
288					     &x_kern, &y_kern);
289
290    kern1 = x_kern >> 1;
291    kern2 = x_kern - kern1;
292    c->buffer->pos[i - 1].x_advance += kern1;
293    c->buffer->pos[i].x_advance += kern2;
294    c->buffer->pos[i].x_offset += kern2;
295
296    kern1 = y_kern >> 1;
297    kern2 = y_kern - kern1;
298    c->buffer->pos[i - 1].y_advance += kern1;
299    c->buffer->pos[i].y_advance += kern2;
300    c->buffer->pos[i].y_offset += kern2;
301  }
302}
303
304static void
305hb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
306{
307  hb_truetype_kern (c);
308}
309
310
311/* Do it! */
312
313static void
314hb_ot_shape_execute_internal (hb_ot_shape_context_t *c)
315{
316  /* Save the original direction, we use it later. */
317  c->target_direction = c->buffer->props.direction;
318
319  hb_reset_glyph_infos (c); /* BUFFER: Clear buffer var1 and var2 */
320
321  hb_set_unicode_props (c); /* BUFFER: Set general_category and combining_class in var1 */
322
323  hb_ensure_native_direction (c);
324
325  hb_form_clusters (c);
326
327  hb_ot_shape_setup_masks (c); /* BUFFER: Clobbers var2 */
328
329  /* SUBSTITUTE */
330  {
331    /* Mirroring needs to see the original direction */
332    hb_mirror_chars (c);
333
334    hb_substitute_default (c);
335
336    hb_ot_substitute_complex (c);
337
338    if (!c->applied_substitute_complex)
339      hb_substitute_complex_fallback (c);
340  }
341
342  /* POSITION */
343  {
344    hb_position_default (c);
345
346    hb_ot_position_complex (c);
347
348    hb_bool_t position_fallback = !c->applied_position_complex;
349    if (position_fallback)
350      hb_position_complex_fallback (c);
351
352    if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
353      hb_buffer_reverse (c->buffer);
354
355    if (position_fallback)
356      hb_position_complex_fallback_visual (c);
357  }
358
359  c->buffer->props.direction = c->target_direction;
360}
361
362void
363hb_ot_shape_plan_internal (hb_ot_shape_plan_t       *plan,
364			   hb_face_t                *face,
365			   const hb_segment_properties_t  *props,
366			   const hb_feature_t       *user_features,
367			   unsigned int              num_user_features)
368{
369  plan->shaper = hb_ot_shape_complex_categorize (props);
370
371  hb_ot_shape_collect_features (plan, props, user_features, num_user_features);
372
373  plan->map.compile (face, props);
374}
375
376void
377hb_ot_shape_execute (hb_ot_shape_plan_t *plan,
378		     hb_font_t          *font,
379		     hb_buffer_t        *buffer,
380		     const hb_feature_t *user_features,
381		     unsigned int        num_user_features)
382{
383  hb_ot_shape_context_t c = {plan, font, font->face, buffer, user_features, num_user_features};
384  hb_ot_shape_execute_internal (&c);
385}
386
387void
388hb_ot_shape (hb_font_t          *font,
389	     hb_buffer_t        *buffer,
390	     const hb_feature_t *features,
391	     unsigned int        num_features)
392{
393  hb_ot_shape_plan_t plan;
394
395  hb_ot_shape_plan_internal (&plan, font->face, &buffer->props, features, num_features);
396  hb_ot_shape_execute (&plan, font, buffer, features, num_features);
397}
398
399
400HB_END_DECLS
401