hb-ot-shape.cc revision d96838ef951ce6170eb2dc576ebcba2262cf7008
1/*
2 * Copyright © 2009,2010  Red Hat, Inc.
3 * Copyright © 2010,2011  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-normalize-private.hh"
31
32#include "hb-font-private.hh"
33#include "hb-set-private.hh"
34
35
36
37hb_tag_t common_features[] = {
38  HB_TAG('c','c','m','p'),
39  HB_TAG('l','i','g','a'),
40  HB_TAG('l','o','c','l'),
41  HB_TAG('m','a','r','k'),
42  HB_TAG('m','k','m','k'),
43  HB_TAG('r','l','i','g'),
44};
45
46
47hb_tag_t horizontal_features[] = {
48  HB_TAG('c','a','l','t'),
49  HB_TAG('c','l','i','g'),
50  HB_TAG('c','u','r','s'),
51  HB_TAG('k','e','r','n'),
52};
53
54/* Note:
55 * Technically speaking, vrt2 and vert are mutually exclusive.
56 * According to the spec, valt and vpal are also mutually exclusive.
57 * But we apply them all for now.
58 */
59hb_tag_t vertical_features[] = {
60  HB_TAG('v','a','l','t'),
61  HB_TAG('v','e','r','t'),
62  HB_TAG('v','k','r','n'),
63  HB_TAG('v','p','a','l'),
64  HB_TAG('v','r','t','2'),
65};
66
67
68
69struct hb_ot_shape_planner_t
70{
71  hb_ot_map_builder_t map;
72  hb_ot_complex_shaper_t shaper;
73
74  hb_ot_shape_planner_t (void) : map () {}
75  ~hb_ot_shape_planner_t (void) { map.finish (); }
76
77  inline void compile (hb_face_t *face,
78		       const hb_segment_properties_t *props,
79		       struct hb_ot_shape_plan_t &plan)
80  {
81    plan.shaper = shaper;
82    map.compile (face, props, plan.map);
83  }
84
85  private:
86  NO_COPY (hb_ot_shape_planner_t);
87};
88
89static void
90hb_ot_shape_collect_features (hb_ot_shape_planner_t          *planner,
91			      const hb_segment_properties_t  *props,
92			      const hb_feature_t             *user_features,
93			      unsigned int                    num_user_features)
94{
95  switch (props->direction) {
96    case HB_DIRECTION_LTR:
97      planner->map.add_bool_feature (HB_TAG ('l','t','r','a'));
98      planner->map.add_bool_feature (HB_TAG ('l','t','r','m'));
99      break;
100    case HB_DIRECTION_RTL:
101      planner->map.add_bool_feature (HB_TAG ('r','t','l','a'));
102      planner->map.add_bool_feature (HB_TAG ('r','t','l','m'), false);
103      break;
104    case HB_DIRECTION_TTB:
105    case HB_DIRECTION_BTT:
106    case HB_DIRECTION_INVALID:
107    default:
108      break;
109  }
110
111#define ADD_FEATURES(array) \
112  HB_STMT_START { \
113    for (unsigned int i = 0; i < ARRAY_LENGTH (array); i++) \
114      planner->map.add_bool_feature (array[i]); \
115  } HB_STMT_END
116
117  hb_ot_shape_complex_collect_features (planner->shaper, &planner->map, props);
118
119  ADD_FEATURES (common_features);
120
121  if (HB_DIRECTION_IS_HORIZONTAL (props->direction))
122    ADD_FEATURES (horizontal_features);
123  else
124    ADD_FEATURES (vertical_features);
125
126  hb_ot_shape_complex_override_features (planner->shaper, &planner->map, props);
127
128#undef ADD_FEATURES
129
130  for (unsigned int i = 0; i < num_user_features; i++) {
131    const hb_feature_t *feature = &user_features[i];
132    planner->map.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
133  }
134}
135
136
137struct hb_ot_shape_context_t
138{
139  /* Input to hb_ot_shape_execute() */
140  hb_ot_shape_plan_t *plan;
141  hb_font_t *font;
142  hb_face_t *face;
143  hb_buffer_t  *buffer;
144  const hb_feature_t *user_features;
145  unsigned int        num_user_features;
146
147  /* Transient stuff */
148  hb_direction_t target_direction;
149  hb_bool_t applied_position_complex;
150};
151
152static void
153hb_ot_shape_setup_masks (hb_ot_shape_context_t *c)
154{
155  hb_mask_t global_mask = c->plan->map.get_global_mask ();
156  c->buffer->reset_masks (global_mask);
157
158  hb_ot_shape_complex_setup_masks (c->plan->shaper, &c->plan->map, c->buffer, c->font);
159
160  for (unsigned int i = 0; i < c->num_user_features; i++)
161  {
162    const hb_feature_t *feature = &c->user_features[i];
163    if (!(feature->start == 0 && feature->end == (unsigned int)-1)) {
164      unsigned int shift;
165      hb_mask_t mask = c->plan->map.get_mask (feature->tag, &shift);
166      c->buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
167    }
168  }
169}
170
171
172/* Main shaper */
173
174/* Prepare */
175
176static void
177hb_set_unicode_props (hb_buffer_t *buffer)
178{
179  unsigned int count = buffer->len;
180  for (unsigned int i = 0; i < count; i++)
181    _hb_glyph_info_set_unicode_props (&buffer->info[i], buffer->unicode);
182}
183
184static void
185hb_form_clusters (hb_buffer_t *buffer)
186{
187  unsigned int count = buffer->len;
188  for (unsigned int i = 1; i < count; i++)
189    if (FLAG (_hb_glyph_info_get_general_category (&buffer->info[i])) &
190	(FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) |
191	 FLAG (HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) |
192	 FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))
193      buffer->merge_clusters (i - 1, i + 1);
194}
195
196static void
197hb_ensure_native_direction (hb_buffer_t *buffer)
198{
199  hb_direction_t direction = buffer->props.direction;
200
201  /* TODO vertical:
202   * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
203   * Ogham fonts are supposed to be implemented BTT or not.  Need to research that
204   * first. */
205  if ((HB_DIRECTION_IS_HORIZONTAL (direction) && direction != hb_script_get_horizontal_direction (buffer->props.script)) ||
206      (HB_DIRECTION_IS_VERTICAL   (direction) && direction != HB_DIRECTION_TTB))
207  {
208    hb_buffer_reverse_clusters (buffer);
209    buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
210  }
211}
212
213
214/* Substitute */
215
216static void
217hb_mirror_chars (hb_ot_shape_context_t *c)
218{
219  hb_unicode_funcs_t *unicode = c->buffer->unicode;
220
221  if (HB_DIRECTION_IS_FORWARD (c->target_direction))
222    return;
223
224  hb_mask_t rtlm_mask = c->plan->map.get_1_mask (HB_TAG ('r','t','l','m'));
225
226  unsigned int count = c->buffer->len;
227  for (unsigned int i = 0; i < count; i++) {
228    hb_codepoint_t codepoint = hb_unicode_mirroring (unicode, c->buffer->info[i].codepoint);
229    if (likely (codepoint == c->buffer->info[i].codepoint))
230      c->buffer->info[i].mask |= rtlm_mask; /* XXX this should be moved to before setting user-feature masks */
231    else
232      c->buffer->info[i].codepoint = codepoint;
233  }
234}
235
236static void
237hb_map_glyphs (hb_font_t    *font,
238	       hb_buffer_t  *buffer)
239{
240  hb_codepoint_t glyph;
241
242  if (unlikely (!buffer->len))
243    return;
244
245  buffer->clear_output ();
246
247  unsigned int count = buffer->len - 1;
248  for (buffer->idx = 0; buffer->idx < count;) {
249    if (unlikely (_hb_unicode_is_variation_selector (buffer->cur(+1).codepoint))) {
250      hb_font_get_glyph (font, buffer->cur().codepoint, buffer->cur(+1).codepoint, &glyph);
251      buffer->replace_glyphs (2, 1, &glyph);
252    } else {
253      hb_font_get_glyph (font, buffer->cur().codepoint, 0, &glyph);
254      buffer->replace_glyph (glyph);
255    }
256  }
257  if (likely (buffer->idx < buffer->len)) {
258    hb_font_get_glyph (font, buffer->cur().codepoint, 0, &glyph);
259    buffer->replace_glyph (glyph);
260  }
261  buffer->swap_buffers ();
262}
263
264static void
265hb_substitute_default (hb_ot_shape_context_t *c)
266{
267  hb_ot_layout_substitute_start (c->buffer);
268
269  hb_mirror_chars (c);
270
271  hb_map_glyphs (c->font, c->buffer);
272}
273
274static void
275hb_ot_substitute_complex (hb_ot_shape_context_t *c)
276{
277  if (hb_ot_layout_has_substitution (c->face)) {
278    c->plan->map.substitute (c->face, c->buffer);
279  }
280
281  hb_ot_layout_substitute_finish (c->buffer);
282
283  return;
284}
285
286
287/* Position */
288
289static void
290hb_position_default (hb_ot_shape_context_t *c)
291{
292  hb_ot_layout_position_start (c->buffer);
293
294  unsigned int count = c->buffer->len;
295  for (unsigned int i = 0; i < count; i++) {
296    hb_font_get_glyph_advance_for_direction (c->font, c->buffer->info[i].codepoint,
297					     c->buffer->props.direction,
298					     &c->buffer->pos[i].x_advance,
299					     &c->buffer->pos[i].y_advance);
300    hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
301						 c->buffer->props.direction,
302						 &c->buffer->pos[i].x_offset,
303						 &c->buffer->pos[i].y_offset);
304  }
305}
306
307static void
308hb_ot_position_complex (hb_ot_shape_context_t *c)
309{
310
311  if (hb_ot_layout_has_positioning (c->face))
312  {
313    /* Change glyph origin to what GPOS expects, apply GPOS, change it back. */
314
315    unsigned int count = c->buffer->len;
316    for (unsigned int i = 0; i < count; i++) {
317      hb_font_add_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
318					      HB_DIRECTION_LTR,
319					      &c->buffer->pos[i].x_offset,
320					      &c->buffer->pos[i].y_offset);
321    }
322
323    c->plan->map.position (c->font, c->buffer);
324
325    for (unsigned int i = 0; i < count; i++) {
326      hb_font_subtract_glyph_origin_for_direction (c->font, c->buffer->info[i].codepoint,
327						   HB_DIRECTION_LTR,
328						   &c->buffer->pos[i].x_offset,
329						   &c->buffer->pos[i].y_offset);
330    }
331
332    c->applied_position_complex = true;
333  }
334
335  hb_ot_layout_position_finish (c->buffer);
336
337  return;
338}
339
340static void
341hb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
342{
343  /* TODO Mark pos */
344}
345
346static void
347hb_truetype_kern (hb_ot_shape_context_t *c)
348{
349  /* TODO Check for kern=0 */
350  unsigned int count = c->buffer->len;
351  for (unsigned int i = 1; i < count; i++) {
352    hb_position_t x_kern, y_kern, kern1, kern2;
353    hb_font_get_glyph_kerning_for_direction (c->font,
354					     c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint,
355					     c->buffer->props.direction,
356					     &x_kern, &y_kern);
357
358    kern1 = x_kern >> 1;
359    kern2 = x_kern - kern1;
360    c->buffer->pos[i - 1].x_advance += kern1;
361    c->buffer->pos[i].x_advance += kern2;
362    c->buffer->pos[i].x_offset += kern2;
363
364    kern1 = y_kern >> 1;
365    kern2 = y_kern - kern1;
366    c->buffer->pos[i - 1].y_advance += kern1;
367    c->buffer->pos[i].y_advance += kern2;
368    c->buffer->pos[i].y_offset += kern2;
369  }
370}
371
372static void
373hb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
374{
375  hb_truetype_kern (c);
376}
377
378static void
379hb_hide_zerowidth (hb_ot_shape_context_t *c)
380{
381  /* TODO Save the space character in the font? */
382  hb_codepoint_t space;
383  if (!hb_font_get_glyph (c->font, ' ', 0, &space))
384    return; /* No point! */
385
386  unsigned int count = c->buffer->len;
387  for (unsigned int i = 0; i < count; i++)
388    /* TODO Do this if no ligature was formed? */
389    if (unlikely (_hb_glyph_info_is_zero_width (&c->buffer->info[i]))) {
390      c->buffer->info[i].codepoint = space;
391      c->buffer->pos[i].x_advance = 0;
392      c->buffer->pos[i].y_advance = 0;
393    }
394}
395
396
397/* Do it! */
398
399static void
400hb_ot_shape_execute_internal (hb_ot_shape_context_t *c)
401{
402  c->buffer->deallocate_var_all ();
403
404  /* Save the original direction, we use it later. */
405  c->target_direction = c->buffer->props.direction;
406
407  HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props0);
408  HB_BUFFER_ALLOCATE_VAR (c->buffer, unicode_props1);
409
410  c->buffer->clear_output ();
411
412  hb_set_unicode_props (c->buffer);
413
414  hb_form_clusters (c->buffer);
415
416  hb_ensure_native_direction (c->buffer);
417
418  _hb_ot_shape_normalize (c->font, c->buffer, hb_ot_shape_complex_normalization_preference (c->plan->shaper));
419
420  hb_ot_shape_setup_masks (c);
421
422  /* SUBSTITUTE */
423  {
424    hb_substitute_default (c);
425
426    hb_ot_substitute_complex (c);
427  }
428
429  /* POSITION */
430  {
431    hb_position_default (c);
432
433    hb_ot_position_complex (c);
434
435    hb_bool_t position_fallback = !c->applied_position_complex;
436    if (position_fallback)
437      hb_position_complex_fallback (c);
438
439    if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
440      hb_buffer_reverse (c->buffer);
441
442    if (position_fallback)
443      hb_position_complex_fallback_visual (c);
444  }
445
446  hb_hide_zerowidth (c);
447
448  HB_BUFFER_DEALLOCATE_VAR (c->buffer, unicode_props1);
449  HB_BUFFER_DEALLOCATE_VAR (c->buffer, unicode_props0);
450
451  c->buffer->props.direction = c->target_direction;
452
453  c->buffer->deallocate_var_all ();
454}
455
456static void
457hb_ot_shape_plan_internal (hb_ot_shape_plan_t       *plan,
458			   hb_face_t                *face,
459			   const hb_segment_properties_t  *props,
460			   const hb_feature_t       *user_features,
461			   unsigned int              num_user_features)
462{
463  hb_ot_shape_planner_t planner;
464
465  assert (HB_DIRECTION_IS_VALID (props->direction));
466
467  planner.shaper = hb_ot_shape_complex_categorize (props);
468
469  hb_ot_shape_collect_features (&planner, props, user_features, num_user_features);
470
471  planner.compile (face, props, *plan);
472}
473
474static void
475hb_ot_shape_execute (hb_ot_shape_plan_t *plan,
476		     hb_font_t          *font,
477		     hb_buffer_t        *buffer,
478		     const hb_feature_t *user_features,
479		     unsigned int        num_user_features)
480{
481  hb_ot_shape_context_t c = {plan, font, font->face, buffer, user_features, num_user_features};
482  hb_ot_shape_execute_internal (&c);
483}
484
485hb_bool_t
486_hb_ot_shape (hb_font_t          *font,
487	      hb_buffer_t        *buffer,
488	      const hb_feature_t *features,
489	      unsigned int        num_features)
490{
491  hb_ot_shape_plan_t plan;
492
493  buffer->guess_properties ();
494
495  hb_ot_shape_plan_internal (&plan, font->face, &buffer->props, features, num_features);
496  hb_ot_shape_execute (&plan, font, buffer, features, num_features);
497
498  return true;
499}
500
501
502void
503hb_ot_shape_glyphs_closure (hb_font_t          *font,
504			    hb_buffer_t        *buffer,
505			    const hb_feature_t *features,
506			    unsigned int        num_features,
507			    hb_set_t           *glyphs)
508{
509  hb_ot_shape_plan_t plan;
510
511  buffer->guess_properties ();
512
513  hb_ot_shape_plan_internal (&plan, font->face, &buffer->props, features, num_features);
514
515  /* TODO: normalization? have shapers do closure()? */
516  /* TODO: Deal with mirrored chars? */
517  hb_map_glyphs (font, buffer);
518
519  /* Seed it.  It's user's responsibility to have cleard glyphs
520   * if that's what they desire. */
521  unsigned int count = buffer->len;
522  for (unsigned int i = 0; i < count; i++)
523    hb_set_add (glyphs, buffer->info[i].codepoint);
524
525  /* And find transitive closure. */
526  hb_set_t copy;
527  copy.init ();
528
529  do {
530    copy.set (glyphs);
531    plan.map.substitute_closure (font->face, glyphs);
532  } while (!copy.equal (glyphs));
533}
534