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