2 * Copyright (C) 2009,2010 Red Hat, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Red Hat Author(s): Behdad Esfahbod
27 #include "hb-ot-shape.h"
29 #include "hb-buffer-private.hh"
31 #include "hb-open-type-private.hh"
33 #include "hb-ot-layout.h"
36 hb_tag_t default_features[] = {
37 HB_TAG('c','a','l','t'),
38 HB_TAG('c','c','m','p'),
39 HB_TAG('c','l','i','g'),
40 HB_TAG('c','s','w','h'),
41 HB_TAG('c','u','r','s'),
42 HB_TAG('k','e','r','n'),
43 HB_TAG('l','i','g','a'),
44 HB_TAG('l','o','c','l'),
45 HB_TAG('m','a','r','k'),
46 HB_TAG('m','k','m','k'),
47 HB_TAG('r','l','i','g')
51 MASK_ALWAYS_ON = 1 << 0,
54 #define MASK_BITS_USED 2
63 add_feature (hb_face_t *face,
65 unsigned int feature_index,
68 unsigned int *num_lookups,
69 unsigned int room_lookups)
71 unsigned int i = room_lookups - *num_lookups;
72 lookups += *num_lookups;
74 unsigned int *lookup_indices = (unsigned int *) lookups;
76 hb_ot_layout_feature_get_lookup_indexes (face, table_tag, feature_index, 0,
83 lookups[i].mask = mask;
84 lookups[i].index = lookup_indices[i];
89 cmp_lookups (const void *p1, const void *p2)
91 const lookup_map *a = (const lookup_map *) p1;
92 const lookup_map *b = (const lookup_map *) p2;
94 return a->index - b->index;
98 #define MAX_FEATURES 100
100 struct hb_mask_allocator_t {
102 struct feature_info_t {
108 cmp (const void *p1, const void *p2)
110 const feature_info_t *a = (const feature_info_t *) p1;
111 const feature_info_t *b = (const feature_info_t *) p2;
113 if (a->tag != b->tag)
114 return a->tag < b->tag ? -1 : 1;
116 return p1 < p2 ? -1 : 1;
120 struct feature_map_t {
121 hb_tag_t tag; /* should be first */
127 cmp (const void *p1, const void *p2)
129 const feature_map_t *a = (const feature_map_t *) p1;
130 const feature_map_t *b = (const feature_map_t *) p2;
132 return a->tag < b->tag ? -1 : a->tag > b->tag ? 1 : 0;
136 hb_mask_allocator_t (hb_face_t *face,
138 unsigned int script_index,
139 unsigned int language_index) :
141 table_tag (table_tag),
142 script_index (script_index),
143 language_index (language_index),
146 void add_feature (hb_tag_t tag,
150 feature_info_t *info = &infos[count++];
153 info->global = global;
159 next_bit = MASK_BITS_USED;
164 qsort (infos, count, sizeof (infos[0]), feature_info_t::cmp);
167 for (unsigned int i = 1; i < count; i++)
168 if (infos[i].tag != infos[j].tag)
169 infos[++j] = infos[i];
171 if (!infos[j].global)
172 infos[j].value = MAX (infos[j].value, infos[i].value);
176 /* Allocate bits now */
178 for (unsigned int i = 0; i < count; i++) {
179 const feature_info_t *info = &infos[i];
181 unsigned int bits_needed;
183 if (info->global && info->value == 1)
184 /* Uses the global bit */
187 bits_needed = _hb_bit_storage (info->value);
189 if (!info->value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
190 continue; /* Feature disabled, or not enough bits. */
192 unsigned int feature_index;
193 if (!hb_ot_layout_language_find_feature (face, table_tag, script_index, language_index,
194 info->tag, &feature_index))
197 feature_map_t *map = &maps[j++];
199 map->tag = info->tag;
200 map->index = feature_index;
201 if (info->global && info->value == 1) {
202 /* Uses the global bit */
206 map->shift = next_bit;
207 map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
208 next_bit += bits_needed;
211 if (info->global && map->mask != 1)
212 global_mask |= map->mask;
217 hb_mask_t get_global_mask (void) { return global_mask; }
218 const feature_map_t *find_feature (hb_tag_t tag) const {
219 static const feature_map_t off_map = { HB_TAG_NONE, Index::NOT_FOUND_INDEX, 0, 0 };
220 const feature_map_t *map = (const feature_map_t *) bsearch (&tag, maps, count, sizeof (maps[0]), feature_map_t::cmp);
221 return map ? map : &off_map;
228 unsigned int script_index;
229 unsigned int language_index;
232 feature_info_t infos[MAX_FEATURES];
233 feature_map_t maps[MAX_FEATURES];
235 hb_mask_t global_mask;
236 unsigned int next_bit;
240 setup_lookups (hb_face_t *face,
242 hb_feature_t *features,
243 unsigned int num_features,
246 unsigned int *num_lookups,
247 hb_direction_t original_direction)
249 unsigned int i, j, script_index, language_index, feature_index, room_lookups;
251 room_lookups = *num_lookups;
254 hb_ot_layout_table_choose_script (face, table_tag,
255 hb_ot_tags_from_script (buffer->script),
257 hb_ot_layout_script_find_language (face, table_tag, script_index,
258 hb_ot_tag_from_language (buffer->language),
261 if (hb_ot_layout_language_get_required_feature_index (face, table_tag, script_index, language_index,
263 add_feature (face, table_tag, feature_index, 1, lookups, num_lookups, room_lookups);
266 hb_mask_allocator_t allocator (face, table_tag, script_index, language_index);
268 switch (original_direction) {
269 case HB_DIRECTION_LTR:
270 allocator.add_feature (HB_TAG ('l','t','r','a'), 1, true);
271 allocator.add_feature (HB_TAG ('l','t','r','m'), 1, true);
273 case HB_DIRECTION_RTL:
274 allocator.add_feature (HB_TAG ('r','t','l','a'), 1, true);
275 //allocator.add_feature (HB_TAG ('r','t','l','m'), false);
276 allocator.add_feature (HB_TAG ('r','t','l','m'), 1, true);
278 case HB_DIRECTION_TTB:
279 case HB_DIRECTION_BTT:
284 for (i = 0; i < ARRAY_LENGTH (default_features); i++)
285 allocator.add_feature (default_features[i], 1, true);
287 /* XXX complex-shaper features go here */
289 for (unsigned int i = 0; i < num_features; i++) {
290 const hb_feature_t *feature = &features[i];
291 allocator.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
295 /* Compile features */
296 allocator.compile ();
299 /* Gather lookup indices for features and set buffer masks at the same time */
301 const hb_mask_allocator_t::feature_map_t *map;
303 hb_mask_t global_mask = allocator.get_global_mask ();
305 buffer->set_masks (global_mask, global_mask, 0, (unsigned int) -1);
307 switch (original_direction) {
308 case HB_DIRECTION_LTR:
309 map = allocator.find_feature (HB_TAG ('l','t','r','a'));
310 add_feature (face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
311 map = allocator.find_feature (HB_TAG ('l','t','r','m'));
312 add_feature (face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
314 case HB_DIRECTION_RTL:
315 map = allocator.find_feature (HB_TAG ('r','t','l','a'));
316 add_feature (face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
317 //map = allocator.find_feature (HB_TAG ('r','t','l','m'));
318 add_feature (face, table_tag, map->index, MASK_RTLM, lookups, num_lookups, room_lookups);
320 case HB_DIRECTION_TTB:
321 case HB_DIRECTION_BTT:
326 for (i = 0; i < ARRAY_LENGTH (default_features); i++)
328 map = allocator.find_feature (default_features[i]);
329 add_feature (face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
332 for (i = 0; i < num_features; i++)
334 hb_feature_t *feature = &features[i];
335 map = allocator.find_feature (feature->tag);
336 add_feature (face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
337 if (!(feature->start == 0 && feature->end == (unsigned int)-1))
338 buffer->set_masks (features[i].value << map->shift, map->mask, feature->start, feature->end);
342 /* Sort lookups and merge duplicates */
344 qsort (lookups, *num_lookups, sizeof (lookups[0]), cmp_lookups);
348 for (i = 1, j = 0; i < *num_lookups; i++)
349 if (lookups[i].index != lookups[j].index)
350 lookups[++j] = lookups[i];
352 lookups[j].mask |= lookups[i].mask;
360 hb_ot_substitute_complex (hb_font_t *font HB_UNUSED,
363 hb_feature_t *features,
364 unsigned int num_features,
365 hb_direction_t original_direction)
367 lookup_map lookups[1000]; /* FIXME */
368 unsigned int num_lookups = ARRAY_LENGTH (lookups);
371 if (!hb_ot_layout_has_substitution (face))
374 setup_lookups (face, buffer, features, num_features,
376 lookups, &num_lookups,
379 for (i = 0; i < num_lookups; i++)
380 hb_ot_layout_substitute_lookup (face, buffer, lookups[i].index, lookups[i].mask);
386 hb_ot_position_complex (hb_font_t *font,
389 hb_feature_t *features,
390 unsigned int num_features,
391 hb_direction_t original_direction)
393 lookup_map lookups[1000];
394 unsigned int num_lookups = ARRAY_LENGTH (lookups);
397 if (!hb_ot_layout_has_positioning (face))
400 setup_lookups (face, buffer, features, num_features,
402 lookups, &num_lookups,
405 for (i = 0; i < num_lookups; i++)
406 hb_ot_layout_position_lookup (font, face, buffer, lookups[i].index, lookups[i].mask);
408 hb_ot_layout_position_finish (font, face, buffer);
418 static inline hb_bool_t
419 is_variation_selector (hb_codepoint_t unicode)
421 return unlikely ((unicode >= 0x180B && unicode <= 0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
422 (unicode >= 0xFE00 && unicode <= 0xFE0F) || /* VARIATION SELECTOR-1..16 */
423 (unicode >= 0xE0100 && unicode <= 0xE01EF)); /* VARIATION SELECTOR-17..256 */
427 hb_form_clusters (hb_buffer_t *buffer)
429 unsigned int count = buffer->len;
430 for (unsigned int i = 1; i < count; i++)
431 if (buffer->unicode->v.get_general_category (buffer->info[i].codepoint) == HB_CATEGORY_NON_SPACING_MARK)
432 buffer->info[i].cluster = buffer->info[i - 1].cluster;
435 static hb_direction_t
436 hb_ensure_native_direction (hb_buffer_t *buffer)
438 hb_direction_t original_direction = buffer->direction;
441 if (HB_DIRECTION_IS_HORIZONTAL (original_direction) &&
442 original_direction != _hb_script_get_horizontal_direction (buffer->script))
444 hb_buffer_reverse_clusters (buffer);
445 buffer->direction = HB_DIRECTION_REVERSE (buffer->direction);
448 return original_direction;
455 hb_mirror_chars (hb_buffer_t *buffer)
457 hb_unicode_get_mirroring_func_t get_mirroring = buffer->unicode->v.get_mirroring;
459 if (HB_DIRECTION_IS_FORWARD (buffer->direction))
462 unsigned int count = buffer->len;
463 for (unsigned int i = 0; i < count; i++) {
464 hb_codepoint_t codepoint = get_mirroring (buffer->info[i].codepoint);
465 if (likely (codepoint == buffer->info[i].codepoint))
466 buffer->info[i].mask |= MASK_RTLM;
468 buffer->info[i].codepoint = codepoint;
473 hb_map_glyphs (hb_font_t *font,
477 if (unlikely (!buffer->len))
480 unsigned int count = buffer->len - 1;
481 for (unsigned int i = 0; i < count; i++) {
482 if (unlikely (is_variation_selector (buffer->info[i + 1].codepoint))) {
483 buffer->info[i].codepoint = hb_font_get_glyph (font, face, buffer->info[i].codepoint, buffer->info[i + 1].codepoint);
486 buffer->info[i].codepoint = hb_font_get_glyph (font, face, buffer->info[i].codepoint, 0);
489 buffer->info[count].codepoint = hb_font_get_glyph (font, face, buffer->info[count].codepoint, 0);
493 hb_substitute_default (hb_font_t *font,
496 hb_feature_t *features HB_UNUSED,
497 unsigned int num_features HB_UNUSED)
499 hb_map_glyphs (font, face, buffer);
503 hb_substitute_complex_fallback (hb_font_t *font HB_UNUSED,
504 hb_face_t *face HB_UNUSED,
505 hb_buffer_t *buffer HB_UNUSED,
506 hb_feature_t *features HB_UNUSED,
507 unsigned int num_features HB_UNUSED)
516 hb_position_default (hb_font_t *font,
519 hb_feature_t *features HB_UNUSED,
520 unsigned int num_features HB_UNUSED)
522 hb_buffer_clear_positions (buffer);
524 unsigned int count = buffer->len;
525 for (unsigned int i = 0; i < count; i++) {
526 hb_glyph_metrics_t metrics;
527 hb_font_get_glyph_metrics (font, face, buffer->info[i].codepoint, &metrics);
528 buffer->pos[i].x_advance = metrics.x_advance;
529 buffer->pos[i].y_advance = metrics.y_advance;
534 hb_position_complex_fallback (hb_font_t *font HB_UNUSED,
535 hb_face_t *face HB_UNUSED,
536 hb_buffer_t *buffer HB_UNUSED,
537 hb_feature_t *features HB_UNUSED,
538 unsigned int num_features HB_UNUSED)
544 hb_truetype_kern (hb_font_t *font,
547 hb_feature_t *features HB_UNUSED,
548 unsigned int num_features HB_UNUSED)
550 /* TODO Check for kern=0 */
551 unsigned int count = buffer->len;
552 for (unsigned int i = 1; i < count; i++) {
553 hb_position_t kern, kern1, kern2;
554 kern = hb_font_get_kerning (font, face, buffer->info[i - 1].codepoint, buffer->info[i].codepoint);
556 kern2 = kern - kern1;
557 buffer->pos[i - 1].x_advance += kern1;
558 buffer->pos[i].x_advance += kern2;
559 buffer->pos[i].x_offset += kern2;
564 hb_position_complex_fallback_visual (hb_font_t *font,
567 hb_feature_t *features,
568 unsigned int num_features)
570 hb_truetype_kern (font, face, buffer, features, num_features);
577 hb_ot_shape (hb_font_t *font,
580 hb_feature_t *features,
581 unsigned int num_features)
583 hb_direction_t original_direction;
584 hb_bool_t substitute_fallback, position_fallback;
586 hb_form_clusters (buffer);
590 buffer->clear_masks ();
592 /* Mirroring needs to see the original direction */
593 hb_mirror_chars (buffer);
595 original_direction = hb_ensure_native_direction (buffer);
597 hb_substitute_default (font, face, buffer, features, num_features);
599 substitute_fallback = !hb_ot_substitute_complex (font, face, buffer, features, num_features, original_direction);
601 if (substitute_fallback)
602 hb_substitute_complex_fallback (font, face, buffer, features, num_features);
607 buffer->clear_masks ();
609 hb_position_default (font, face, buffer, features, num_features);
611 position_fallback = !hb_ot_position_complex (font, face, buffer, features, num_features, original_direction);
613 if (position_fallback)
614 hb_position_complex_fallback (font, face, buffer, features, num_features);
616 if (HB_DIRECTION_IS_BACKWARD (buffer->direction))
617 hb_buffer_reverse (buffer);
619 if (position_fallback)
620 hb_position_complex_fallback_visual (font, face, buffer, features, num_features);
622 buffer->direction = original_direction;