2 * Copyright (C) 2009,2010 Red Hat, Inc.
3 * Copyright (C) 2010 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
29 #include "hb-ot-shape-private.h"
31 #include "hb-open-type-private.hh"
33 #include "hb-ot-layout.h"
39 hb_tag_t default_features[] = {
40 HB_TAG('c','a','l','t'),
41 HB_TAG('c','c','m','p'),
42 HB_TAG('c','l','i','g'),
43 HB_TAG('c','s','w','h'),
44 HB_TAG('c','u','r','s'),
45 HB_TAG('k','e','r','n'),
46 HB_TAG('l','i','g','a'),
47 HB_TAG('l','o','c','l'),
48 HB_TAG('m','a','r','k'),
49 HB_TAG('m','k','m','k'),
50 HB_TAG('r','l','i','g')
60 add_lookups (hb_face_t *face,
62 unsigned int feature_index,
65 unsigned int *num_lookups,
66 unsigned int room_lookups)
68 unsigned int i = room_lookups - *num_lookups;
69 lookups += *num_lookups;
71 unsigned int *lookup_indices = (unsigned int *) lookups;
73 hb_ot_layout_feature_get_lookup_indexes (face, table_tag, feature_index, 0,
80 lookups[i].mask = mask;
81 lookups[i].index = lookup_indices[i];
86 cmp_lookups (const void *p1, const void *p2)
88 const lookup_map *a = (const lookup_map *) p1;
89 const lookup_map *b = (const lookup_map *) p2;
91 return a->index - b->index;
95 #define MAX_FEATURES 100 /* FIXME */
97 struct hb_mask_allocator_t {
99 struct feature_info_t {
106 cmp (const void *p1, const void *p2)
108 const feature_info_t *a = reinterpret_cast<const feature_info_t *>(p1);
109 const feature_info_t *b = reinterpret_cast<const feature_info_t *>(p2);
111 if (a->tag != b->tag)
112 return a->tag < b->tag ? -1 : 1;
114 return a->seq < b->seq ? -1 : 1;
118 struct feature_map_t {
119 hb_tag_t tag; /* should be first */
125 cmp (const void *p1, const void *p2)
127 const feature_map_t *a = reinterpret_cast<const feature_map_t *>(p1);
128 const feature_map_t *b = reinterpret_cast<const feature_map_t *>(p2);
130 return a->tag < b->tag ? -1 : a->tag > b->tag ? 1 : 0;
134 hb_mask_allocator_t (void) : count (0) {}
136 void add_feature (hb_tag_t tag,
140 feature_info_t *info = &infos[count++];
144 info->global = global;
147 void compile (hb_face_t *face,
149 unsigned int script_index,
150 unsigned int language_index)
153 unsigned int next_bit = 1;
158 qsort (infos, count, sizeof (infos[0]), feature_info_t::cmp);
161 for (unsigned int i = 1; i < count; i++)
162 if (infos[i].tag != infos[j].tag)
163 infos[++j] = infos[i];
168 infos[j].global = infos[j].global && (infos[j].value == infos[i].value);
169 infos[j].value = MAX (infos[j].value, infos[i].value);
174 /* Allocate bits now */
176 for (unsigned int i = 0; i < count; i++) {
177 const feature_info_t *info = &infos[i];
179 unsigned int bits_needed;
181 if (info->global && info->value == 1)
182 /* Uses the global bit */
185 bits_needed = _hb_bit_storage (info->value);
187 if (!info->value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
188 continue; /* Feature disabled, or not enough bits. */
190 unsigned int feature_index;
191 if (!hb_ot_layout_language_find_feature (face, table_tag, script_index, language_index,
192 info->tag, &feature_index))
195 feature_map_t *map = &maps[j++];
197 map->tag = info->tag;
198 map->index = feature_index;
199 if (info->global && info->value == 1) {
200 /* Use the global bit */
204 map->shift = next_bit;
205 map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
206 next_bit += bits_needed;
209 if (info->global && map->mask != 1)
210 global_mask |= map->mask;
215 hb_mask_t get_global_mask (void) { return global_mask; }
216 const feature_map_t *find_feature (hb_tag_t tag) const {
217 static const feature_map_t off_map = { HB_TAG_NONE, Index::NOT_FOUND_INDEX, 0, 0 };
218 const feature_map_t *map = (const feature_map_t *) bsearch (&tag, maps, count, sizeof (maps[0]), feature_map_t::cmp);
219 return map ? map : &off_map;
226 feature_info_t infos[MAX_FEATURES];
228 feature_map_t maps[MAX_FEATURES];
229 hb_mask_t global_mask;
233 hb_ot_shape_setup_lookups_complex (hb_ot_shape_context_t *c,
235 unsigned int *num_lookups)
241 hb_ot_shape_setup_lookups (hb_ot_shape_context_t *c,
243 unsigned int *num_lookups)
245 unsigned int i, j, script_index, language_index, feature_index, room_lookups;
247 room_lookups = *num_lookups;
250 hb_ot_layout_table_choose_script (c->face, c->table_tag,
251 hb_ot_tags_from_script (c->buffer->props.script),
253 hb_ot_layout_script_find_language (c->face, c->table_tag, script_index,
254 hb_ot_tag_from_language (c->buffer->props.language),
258 hb_mask_allocator_t allocator;
260 switch (c->original_direction) {
261 case HB_DIRECTION_LTR:
262 allocator.add_feature (HB_TAG ('l','t','r','a'), 1, true);
263 allocator.add_feature (HB_TAG ('l','t','r','m'), 1, true);
265 case HB_DIRECTION_RTL:
266 allocator.add_feature (HB_TAG ('r','t','l','a'), 1, true);
267 allocator.add_feature (HB_TAG ('r','t','l','m'), 1, false);
269 case HB_DIRECTION_TTB:
270 case HB_DIRECTION_BTT:
275 for (i = 0; i < ARRAY_LENGTH (default_features); i++)
276 allocator.add_feature (default_features[i], 1, true);
278 hb_ot_shape_setup_lookups_complex (c, lookups, num_lookups);
280 for (unsigned int i = 0; i < c->num_features; i++) {
281 const hb_feature_t *feature = &c->features[i];
282 allocator.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
286 /* Compile features */
287 allocator.compile (c->face, c->table_tag, script_index, language_index);
290 /* Gather lookup indices for features and set buffer masks at the same time */
292 if (hb_ot_layout_language_get_required_feature_index (c->face, c->table_tag, script_index, language_index,
294 add_lookups (c->face, c->table_tag, feature_index, 1, lookups, num_lookups, room_lookups);
296 const hb_mask_allocator_t::feature_map_t *map;
298 hb_mask_t global_mask = allocator.get_global_mask ();
300 c->buffer->set_masks (global_mask, global_mask, 0, (unsigned int) -1);
302 switch (c->original_direction) {
303 case HB_DIRECTION_LTR:
304 map = allocator.find_feature (HB_TAG ('l','t','r','a'));
305 add_lookups (c->face, c->table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
306 map = allocator.find_feature (HB_TAG ('l','t','r','m'));
307 add_lookups (c->face, c->table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
309 case HB_DIRECTION_RTL:
310 map = allocator.find_feature (HB_TAG ('r','t','l','a'));
311 add_lookups (c->face, c->table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
312 map = allocator.find_feature (HB_TAG ('r','t','l','m'));
313 add_lookups (c->face, c->table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
315 case HB_DIRECTION_TTB:
316 case HB_DIRECTION_BTT:
321 for (i = 0; i < ARRAY_LENGTH (default_features); i++)
323 map = allocator.find_feature (default_features[i]);
324 add_lookups (c->face, c->table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
327 for (i = 0; i < c->num_features; i++)
329 hb_feature_t *feature = &c->features[i];
330 map = allocator.find_feature (feature->tag);
331 add_lookups (c->face, c->table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
332 if (!(feature->start == 0 && feature->end == (unsigned int)-1))
333 c->buffer->set_masks (feature->value << map->shift, map->mask, feature->start, feature->end);
337 /* Sort lookups and merge duplicates */
339 qsort (lookups, *num_lookups, sizeof (lookups[0]), cmp_lookups);
343 for (i = 1, j = 0; i < *num_lookups; i++)
344 if (lookups[i].index != lookups[j].index)
345 lookups[++j] = lookups[i];
347 lookups[j].mask |= lookups[i].mask;
355 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
357 lookup_map lookups[1000]; /* FIXME */
358 unsigned int num_lookups = ARRAY_LENGTH (lookups);
361 if (!hb_ot_layout_has_substitution (c->face))
364 c->table_tag = HB_OT_TAG_GSUB;
366 hb_ot_shape_setup_lookups (c, lookups, &num_lookups);
368 for (i = 0; i < num_lookups; i++)
369 hb_ot_layout_substitute_lookup (c->face, c->buffer, lookups[i].index, lookups[i].mask);
371 c->applied_substitute_complex = TRUE;
376 hb_ot_position_complex (hb_ot_shape_context_t *c)
378 lookup_map lookups[1000]; /* FIXME */
379 unsigned int num_lookups = ARRAY_LENGTH (lookups);
382 if (!hb_ot_layout_has_positioning (c->face))
385 c->table_tag = HB_OT_TAG_GPOS;
387 hb_ot_shape_setup_lookups (c, lookups, &num_lookups);
389 for (i = 0; i < num_lookups; i++)
390 hb_ot_layout_position_lookup (c->font, c->face, c->buffer, lookups[i].index, lookups[i].mask);
392 hb_ot_layout_position_finish (c->font, c->face, c->buffer);
394 c->applied_position_complex = TRUE;
403 static inline hb_bool_t
404 is_variation_selector (hb_codepoint_t unicode)
406 return unlikely ((unicode >= 0x180B && unicode <= 0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
407 (unicode >= 0xFE00 && unicode <= 0xFE0F) || /* VARIATION SELECTOR-1..16 */
408 (unicode >= 0xE0100 && unicode <= 0xE01EF)); /* VARIATION SELECTOR-17..256 */
412 hb_form_clusters (hb_buffer_t *buffer)
414 unsigned int count = buffer->len;
415 for (unsigned int i = 1; i < count; i++)
416 if (buffer->unicode->v.get_general_category (buffer->info[i].codepoint) == HB_CATEGORY_NON_SPACING_MARK)
417 buffer->info[i].cluster = buffer->info[i - 1].cluster;
421 hb_ensure_native_direction (hb_buffer_t *buffer)
423 hb_direction_t direction = buffer->props.direction;
426 if (HB_DIRECTION_IS_HORIZONTAL (direction) &&
427 direction != _hb_script_get_horizontal_direction (buffer->props.script))
429 hb_buffer_reverse_clusters (buffer);
430 buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
438 hb_mirror_chars (hb_buffer_t *buffer)
440 hb_unicode_get_mirroring_func_t get_mirroring = buffer->unicode->v.get_mirroring;
442 if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
445 // map = allocator.find_feature (HB_TAG ('r','t','l','m'));
447 unsigned int count = buffer->len;
448 for (unsigned int i = 0; i < count; i++) {
449 hb_codepoint_t codepoint = get_mirroring (buffer->info[i].codepoint);
450 if (likely (codepoint == buffer->info[i].codepoint))
451 ;// buffer->info[i].mask |= map->mask;
453 buffer->info[i].codepoint = codepoint;
458 hb_map_glyphs (hb_font_t *font,
462 if (unlikely (!buffer->len))
465 buffer->clear_output ();
466 unsigned int count = buffer->len - 1;
467 for (buffer->i = 0; buffer->i < count;) {
468 if (unlikely (is_variation_selector (buffer->info[buffer->i + 1].codepoint))) {
469 buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, buffer->info[buffer->i + 1].codepoint));
472 buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
475 if (likely (buffer->i < buffer->len))
476 buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
481 hb_substitute_default (hb_ot_shape_context_t *c)
483 hb_map_glyphs (c->font, c->face, c->buffer);
487 hb_substitute_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
496 hb_position_default (hb_ot_shape_context_t *c)
498 hb_buffer_clear_positions (c->buffer);
500 unsigned int count = c->buffer->len;
501 for (unsigned int i = 0; i < count; i++) {
502 hb_glyph_metrics_t metrics;
503 hb_font_get_glyph_metrics (c->font, c->face, c->buffer->info[i].codepoint, &metrics);
504 c->buffer->pos[i].x_advance = metrics.x_advance;
505 c->buffer->pos[i].y_advance = metrics.y_advance;
510 hb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
516 hb_truetype_kern (hb_ot_shape_context_t *c)
518 /* TODO Check for kern=0 */
519 unsigned int count = c->buffer->len;
520 for (unsigned int i = 1; i < count; i++) {
521 hb_position_t kern, kern1, kern2;
522 kern = hb_font_get_kerning (c->font, c->face, c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint);
524 kern2 = kern - kern1;
525 c->buffer->pos[i - 1].x_advance += kern1;
526 c->buffer->pos[i].x_advance += kern2;
527 c->buffer->pos[i].x_offset += kern2;
532 hb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
534 hb_truetype_kern (c);
541 hb_ot_shape_internal (hb_ot_shape_context_t *c)
543 hb_form_clusters (c->buffer);
547 c->buffer->clear_masks ();
549 /* Mirroring needs to see the original direction */
550 hb_mirror_chars (c->buffer);
552 hb_ensure_native_direction (c->buffer);
554 hb_substitute_default (c);
556 hb_ot_substitute_complex (c);
558 if (!c->applied_substitute_complex)
559 hb_substitute_complex_fallback (c);
564 c->buffer->clear_masks ();
566 hb_position_default (c);
568 hb_ot_position_complex (c);
570 hb_bool_t position_fallback = !c->applied_position_complex;
571 if (position_fallback)
572 hb_position_complex_fallback (c);
574 if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
575 hb_buffer_reverse (c->buffer);
577 if (position_fallback)
578 hb_position_complex_fallback_visual (c);
581 c->buffer->props.direction = c->original_direction;
585 hb_ot_shape (hb_font_t *font,
588 hb_feature_t *features,
589 unsigned int num_features)
591 hb_ot_shape_context_t c = {font, face, buffer, features, num_features};
593 /* Setup transient context members */
594 c.original_direction = buffer->props.direction;
596 hb_ot_shape_internal (&c);