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-private.h"
29 #include "hb-open-type-private.hh"
31 #include "hb-ot-layout.h"
37 hb_tag_t default_features[] = {
38 HB_TAG('c','a','l','t'),
39 HB_TAG('c','c','m','p'),
40 HB_TAG('c','l','i','g'),
41 HB_TAG('c','s','w','h'),
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')
58 add_feature (hb_face_t *face,
60 unsigned int feature_index,
63 unsigned int *num_lookups,
64 unsigned int room_lookups)
66 unsigned int i = room_lookups - *num_lookups;
67 lookups += *num_lookups;
69 unsigned int *lookup_indices = (unsigned int *) lookups;
71 hb_ot_layout_feature_get_lookup_indexes (face, table_tag, feature_index, 0,
78 lookups[i].mask = mask;
79 lookups[i].index = lookup_indices[i];
84 cmp_lookups (const void *p1, const void *p2)
86 const lookup_map *a = (const lookup_map *) p1;
87 const lookup_map *b = (const lookup_map *) p2;
89 return a->index - b->index;
93 #define MAX_FEATURES 100 /* FIXME */
95 struct hb_mask_allocator_t {
97 struct feature_info_t {
104 cmp (const void *p1, const void *p2)
106 const feature_info_t *a = reinterpret_cast<const feature_info_t *>(p1);
107 const feature_info_t *b = reinterpret_cast<const feature_info_t *>(p2);
109 if (a->tag != b->tag)
110 return a->tag < b->tag ? -1 : 1;
112 return a->seq < b->seq ? -1 : 1;
116 struct feature_map_t {
117 hb_tag_t tag; /* should be first */
123 cmp (const void *p1, const void *p2)
125 const feature_map_t *a = reinterpret_cast<const feature_map_t *>(p1);
126 const feature_map_t *b = reinterpret_cast<const feature_map_t *>(p2);
128 return a->tag < b->tag ? -1 : a->tag > b->tag ? 1 : 0;
132 hb_mask_allocator_t (void) : count (0) {}
134 void add_feature (hb_tag_t tag,
138 feature_info_t *info = &infos[count++];
142 info->global = global;
145 void compile (hb_face_t *face,
147 unsigned int script_index,
148 unsigned int language_index)
151 unsigned int next_bit = 1;
156 qsort (infos, count, sizeof (infos[0]), feature_info_t::cmp);
159 for (unsigned int i = 1; i < count; i++)
160 if (infos[i].tag != infos[j].tag)
161 infos[++j] = infos[i];
166 infos[j].global = infos[j].global && (infos[j].value == infos[i].value);
167 infos[j].value = MAX (infos[j].value, infos[i].value);
172 /* Allocate bits now */
174 for (unsigned int i = 0; i < count; i++) {
175 const feature_info_t *info = &infos[i];
177 unsigned int bits_needed;
179 if (info->global && info->value == 1)
180 /* Uses the global bit */
183 bits_needed = _hb_bit_storage (info->value);
185 if (!info->value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
186 continue; /* Feature disabled, or not enough bits. */
188 unsigned int feature_index;
189 if (!hb_ot_layout_language_find_feature (face, table_tag, script_index, language_index,
190 info->tag, &feature_index))
193 feature_map_t *map = &maps[j++];
195 map->tag = info->tag;
196 map->index = feature_index;
197 if (info->global && info->value == 1) {
198 /* Use the global bit */
202 map->shift = next_bit;
203 map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
204 next_bit += bits_needed;
207 if (info->global && map->mask != 1)
208 global_mask |= map->mask;
213 hb_mask_t get_global_mask (void) { return global_mask; }
214 const feature_map_t *find_feature (hb_tag_t tag) const {
215 static const feature_map_t off_map = { HB_TAG_NONE, Index::NOT_FOUND_INDEX, 0, 0 };
216 const feature_map_t *map = (const feature_map_t *) bsearch (&tag, maps, count, sizeof (maps[0]), feature_map_t::cmp);
217 return map ? map : &off_map;
224 feature_info_t infos[MAX_FEATURES];
226 feature_map_t maps[MAX_FEATURES];
227 hb_mask_t global_mask;
231 setup_lookups (hb_ot_shape_context_t *c,
234 unsigned int *num_lookups)
236 unsigned int i, j, script_index, language_index, feature_index, room_lookups;
238 room_lookups = *num_lookups;
241 hb_ot_layout_table_choose_script (c->face, table_tag,
242 hb_ot_tags_from_script (c->buffer->props.script),
244 hb_ot_layout_script_find_language (c->face, table_tag, script_index,
245 hb_ot_tag_from_language (c->buffer->props.language),
249 hb_mask_allocator_t allocator;
251 switch (c->original_direction) {
252 case HB_DIRECTION_LTR:
253 allocator.add_feature (HB_TAG ('l','t','r','a'), 1, true);
254 allocator.add_feature (HB_TAG ('l','t','r','m'), 1, true);
256 case HB_DIRECTION_RTL:
257 allocator.add_feature (HB_TAG ('r','t','l','a'), 1, true);
258 allocator.add_feature (HB_TAG ('r','t','l','m'), 1, false);
260 case HB_DIRECTION_TTB:
261 case HB_DIRECTION_BTT:
266 for (i = 0; i < ARRAY_LENGTH (default_features); i++)
267 allocator.add_feature (default_features[i], 1, true);
269 /* XXX complex-shaper features go here */
271 for (unsigned int i = 0; i < c->num_features; i++) {
272 const hb_feature_t *feature = &c->features[i];
273 allocator.add_feature (feature->tag, feature->value, (feature->start == 0 && feature->end == (unsigned int) -1));
277 /* Compile features */
278 allocator.compile (c->face, table_tag, script_index, language_index);
281 /* Gather lookup indices for features and set buffer masks at the same time */
283 if (hb_ot_layout_language_get_required_feature_index (c->face, table_tag, script_index, language_index,
285 add_feature (c->face, table_tag, feature_index, 1, lookups, num_lookups, room_lookups);
287 const hb_mask_allocator_t::feature_map_t *map;
289 hb_mask_t global_mask = allocator.get_global_mask ();
291 c->buffer->set_masks (global_mask, global_mask, 0, (unsigned int) -1);
293 switch (c->original_direction) {
294 case HB_DIRECTION_LTR:
295 map = allocator.find_feature (HB_TAG ('l','t','r','a'));
296 add_feature (c->face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
297 map = allocator.find_feature (HB_TAG ('l','t','r','m'));
298 add_feature (c->face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
300 case HB_DIRECTION_RTL:
301 map = allocator.find_feature (HB_TAG ('r','t','l','a'));
302 add_feature (c->face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
303 map = allocator.find_feature (HB_TAG ('r','t','l','m'));
304 add_feature (c->face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
306 case HB_DIRECTION_TTB:
307 case HB_DIRECTION_BTT:
312 for (i = 0; i < ARRAY_LENGTH (default_features); i++)
314 map = allocator.find_feature (default_features[i]);
315 add_feature (c->face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
318 for (i = 0; i < c->num_features; i++)
320 hb_feature_t *feature = &c->features[i];
321 map = allocator.find_feature (feature->tag);
322 add_feature (c->face, table_tag, map->index, map->mask, lookups, num_lookups, room_lookups);
323 if (!(feature->start == 0 && feature->end == (unsigned int)-1))
324 c->buffer->set_masks (feature->value << map->shift, map->mask, feature->start, feature->end);
328 /* Sort lookups and merge duplicates */
330 qsort (lookups, *num_lookups, sizeof (lookups[0]), cmp_lookups);
334 for (i = 1, j = 0; i < *num_lookups; i++)
335 if (lookups[i].index != lookups[j].index)
336 lookups[++j] = lookups[i];
338 lookups[j].mask |= lookups[i].mask;
346 hb_ot_substitute_complex (hb_ot_shape_context_t *c)
348 lookup_map lookups[1000]; /* FIXME */
349 unsigned int num_lookups = ARRAY_LENGTH (lookups);
352 if (!hb_ot_layout_has_substitution (c->face))
355 setup_lookups (c, HB_OT_TAG_GSUB, lookups, &num_lookups);
357 for (i = 0; i < num_lookups; i++)
358 hb_ot_layout_substitute_lookup (c->face, c->buffer, lookups[i].index, lookups[i].mask);
360 c->applied_substitute_complex = TRUE;
365 hb_ot_position_complex (hb_ot_shape_context_t *c)
367 lookup_map lookups[1000]; /* FIXME */
368 unsigned int num_lookups = ARRAY_LENGTH (lookups);
371 if (!hb_ot_layout_has_positioning (c->face))
374 setup_lookups (c, HB_OT_TAG_GPOS, lookups, &num_lookups);
376 for (i = 0; i < num_lookups; i++)
377 hb_ot_layout_position_lookup (c->font, c->face, c->buffer, lookups[i].index, lookups[i].mask);
379 hb_ot_layout_position_finish (c->font, c->face, c->buffer);
381 c->applied_position_complex = TRUE;
390 static inline hb_bool_t
391 is_variation_selector (hb_codepoint_t unicode)
393 return unlikely ((unicode >= 0x180B && unicode <= 0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
394 (unicode >= 0xFE00 && unicode <= 0xFE0F) || /* VARIATION SELECTOR-1..16 */
395 (unicode >= 0xE0100 && unicode <= 0xE01EF)); /* VARIATION SELECTOR-17..256 */
399 hb_form_clusters (hb_buffer_t *buffer)
401 unsigned int count = buffer->len;
402 for (unsigned int i = 1; i < count; i++)
403 if (buffer->unicode->v.get_general_category (buffer->info[i].codepoint) == HB_CATEGORY_NON_SPACING_MARK)
404 buffer->info[i].cluster = buffer->info[i - 1].cluster;
408 hb_ensure_native_direction (hb_buffer_t *buffer)
410 hb_direction_t direction = buffer->props.direction;
413 if (HB_DIRECTION_IS_HORIZONTAL (direction) &&
414 direction != _hb_script_get_horizontal_direction (buffer->props.script))
416 hb_buffer_reverse_clusters (buffer);
417 buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
425 hb_mirror_chars (hb_buffer_t *buffer)
427 hb_unicode_get_mirroring_func_t get_mirroring = buffer->unicode->v.get_mirroring;
429 if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
432 // map = allocator.find_feature (HB_TAG ('r','t','l','m'));
434 unsigned int count = buffer->len;
435 for (unsigned int i = 0; i < count; i++) {
436 hb_codepoint_t codepoint = get_mirroring (buffer->info[i].codepoint);
437 if (likely (codepoint == buffer->info[i].codepoint))
438 ;// buffer->info[i].mask |= map->mask;
440 buffer->info[i].codepoint = codepoint;
445 hb_map_glyphs (hb_font_t *font,
449 if (unlikely (!buffer->len))
452 buffer->clear_output ();
453 unsigned int count = buffer->len - 1;
454 for (buffer->i = 0; buffer->i < count;) {
455 if (unlikely (is_variation_selector (buffer->info[buffer->i + 1].codepoint))) {
456 buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, buffer->info[buffer->i + 1].codepoint));
459 buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
462 if (likely (buffer->i < buffer->len))
463 buffer->add_output_glyph (hb_font_get_glyph (font, face, buffer->info[buffer->i].codepoint, 0));
468 hb_substitute_default (hb_ot_shape_context_t *c)
470 hb_map_glyphs (c->font, c->face, c->buffer);
474 hb_substitute_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
483 hb_position_default (hb_ot_shape_context_t *c)
485 hb_buffer_clear_positions (c->buffer);
487 unsigned int count = c->buffer->len;
488 for (unsigned int i = 0; i < count; i++) {
489 hb_glyph_metrics_t metrics;
490 hb_font_get_glyph_metrics (c->font, c->face, c->buffer->info[i].codepoint, &metrics);
491 c->buffer->pos[i].x_advance = metrics.x_advance;
492 c->buffer->pos[i].y_advance = metrics.y_advance;
497 hb_position_complex_fallback (hb_ot_shape_context_t *c HB_UNUSED)
503 hb_truetype_kern (hb_ot_shape_context_t *c)
505 /* TODO Check for kern=0 */
506 unsigned int count = c->buffer->len;
507 for (unsigned int i = 1; i < count; i++) {
508 hb_position_t kern, kern1, kern2;
509 kern = hb_font_get_kerning (c->font, c->face, c->buffer->info[i - 1].codepoint, c->buffer->info[i].codepoint);
511 kern2 = kern - kern1;
512 c->buffer->pos[i - 1].x_advance += kern1;
513 c->buffer->pos[i].x_advance += kern2;
514 c->buffer->pos[i].x_offset += kern2;
519 hb_position_complex_fallback_visual (hb_ot_shape_context_t *c)
521 hb_truetype_kern (c);
528 hb_ot_shape_internal (hb_ot_shape_context_t *c)
530 hb_form_clusters (c->buffer);
534 c->buffer->clear_masks ();
536 /* Mirroring needs to see the original direction */
537 hb_mirror_chars (c->buffer);
539 hb_ensure_native_direction (c->buffer);
541 hb_substitute_default (c);
543 hb_ot_substitute_complex (c);
545 if (!c->applied_substitute_complex)
546 hb_substitute_complex_fallback (c);
551 c->buffer->clear_masks ();
553 hb_position_default (c);
555 hb_ot_position_complex (c);
557 hb_bool_t position_fallback = !c->applied_position_complex;
558 if (position_fallback)
559 hb_position_complex_fallback (c);
561 if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
562 hb_buffer_reverse (c->buffer);
564 if (position_fallback)
565 hb_position_complex_fallback_visual (c);
568 c->buffer->props.direction = c->original_direction;
572 hb_ot_shape (hb_font_t *font,
575 hb_feature_t *features,
576 unsigned int num_features)
578 hb_ot_shape_context_t c = {font, face, buffer, features, num_features};
580 /* Setup transient context members */
581 c.original_direction = buffer->props.direction;
583 hb_ot_shape_internal (&c);