2 * Copyright (C) 2009 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.hh"
29 #include "hb-buffer-private.hh"
31 #include "hb-ot-layout.h"
34 hb_tag_t default_features[] = {
35 HB_TAG('c','a','l','t'),
36 HB_TAG('c','c','m','p'),
37 HB_TAG('c','l','i','g'),
38 HB_TAG('c','s','w','h'),
39 HB_TAG('c','u','r','s'),
40 HB_TAG('k','e','r','n'),
41 HB_TAG('l','i','g','a'),
42 HB_TAG('l','o','c','l'),
43 HB_TAG('m','a','r','k'),
44 HB_TAG('m','k','m','k'),
45 HB_TAG('r','l','i','g')
49 MASK_ALWAYS_ON = 1 << 0,
52 #define MASK_BITS_USED 2
61 add_feature (hb_face_t *face,
63 unsigned int feature_index,
66 unsigned int *num_lookups,
67 unsigned int room_lookups)
69 unsigned int i = room_lookups - *num_lookups;
70 lookups += *num_lookups;
72 unsigned int *lookup_indices = (unsigned int *) lookups;
74 hb_ot_layout_feature_get_lookup_indexes (face, table_tag, feature_index, 0,
81 lookups[i].mask = mask;
82 lookups[i].index = lookup_indices[i];
87 maybe_add_feature (hb_face_t *face,
89 unsigned int script_index,
90 unsigned int language_index,
94 unsigned int *num_lookups,
95 unsigned int room_lookups)
97 unsigned int feature_index;
98 if (hb_ot_layout_language_find_feature (face, table_tag, script_index, language_index,
102 add_feature (face, table_tag, feature_index, mask, lookups, num_lookups, room_lookups);
109 cmp_lookups (const void *p1, const void *p2)
111 const lookup_map *a = (const lookup_map *) p1;
112 const lookup_map *b = (const lookup_map *) p2;
114 return a->index - b->index;
118 setup_lookups (hb_face_t *face,
120 hb_feature_t *features,
121 unsigned int num_features,
124 unsigned int *num_lookups,
125 hb_direction_t original_direction)
127 unsigned int i, j, script_index, language_index, feature_index, room_lookups;
129 room_lookups = *num_lookups;
132 hb_ot_layout_table_choose_script (face, table_tag,
133 hb_ot_tags_from_script (buffer->script),
135 hb_ot_layout_script_find_language (face, table_tag, script_index,
136 hb_ot_tag_from_language (buffer->language),
139 if (hb_ot_layout_language_get_required_feature_index (face, table_tag, script_index, language_index,
141 add_feature (face, table_tag, feature_index, 1, lookups, num_lookups, room_lookups);
143 for (i = 0; i < ARRAY_LENGTH (default_features); i++)
144 maybe_add_feature (face, table_tag, script_index, language_index, default_features[i], 1, lookups, num_lookups, room_lookups);
146 switch (original_direction) {
147 case HB_DIRECTION_LTR:
148 maybe_add_feature (face, table_tag, script_index, language_index, HB_TAG ('l','t','r','a'), 1, lookups, num_lookups, room_lookups);
149 maybe_add_feature (face, table_tag, script_index, language_index, HB_TAG ('l','t','r','m'), 1, lookups, num_lookups, room_lookups);
151 case HB_DIRECTION_RTL:
152 maybe_add_feature (face, table_tag, script_index, language_index, HB_TAG ('r','t','l','a'), 1, lookups, num_lookups, room_lookups);
154 case HB_DIRECTION_TTB:
155 case HB_DIRECTION_BTT:
160 unsigned int next_bit = MASK_BITS_USED;
161 hb_mask_t global_mask = 0;
162 for (i = 0; i < num_features; i++)
164 hb_feature_t *feature = &features[i];
165 if (!hb_ot_layout_language_find_feature (face, table_tag, script_index, language_index,
170 if (feature->value == 1 && feature->start == 0 && feature->end == (unsigned int) -1) {
171 add_feature (face, table_tag, feature_index, 1, lookups, num_lookups, room_lookups);
175 /* Allocate bits for the features */
177 unsigned int bits_needed = _hb_bit_storage (feature->value);
179 continue; /* Feature disabled */
181 if (next_bit + bits_needed > 8 * sizeof (hb_mask_t))
182 continue; /* Oh well... */
184 unsigned int mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
185 unsigned int value = feature->value << next_bit;
186 next_bit += bits_needed;
188 add_feature (face, table_tag, feature_index, mask, lookups, num_lookups, room_lookups);
190 if (feature->start == 0 && feature->end == (unsigned int) -1)
191 global_mask |= value;
193 buffer->or_masks (mask, feature->start, feature->end);
197 buffer->or_masks (global_mask, 0, (unsigned int) -1);
199 qsort (lookups, *num_lookups, sizeof (lookups[0]), cmp_lookups);
203 for (i = 1, j = 0; i < *num_lookups; i++)
204 if (lookups[i].index != lookups[j].index)
205 lookups[++j] = lookups[i];
207 lookups[j].mask |= lookups[i].mask;
215 hb_ot_substitute_complex (hb_font_t *font HB_UNUSED,
218 hb_feature_t *features,
219 unsigned int num_features,
220 hb_direction_t original_direction)
222 lookup_map lookups[1000];
223 unsigned int num_lookups = ARRAY_LENGTH (lookups);
226 if (!hb_ot_layout_has_substitution (face))
229 setup_lookups (face, buffer, features, num_features,
231 lookups, &num_lookups,
234 for (i = 0; i < num_lookups; i++)
235 hb_ot_layout_substitute_lookup (face, buffer, lookups[i].index, lookups[i].mask);
241 hb_ot_position_complex (hb_font_t *font,
244 hb_feature_t *features,
245 unsigned int num_features,
246 hb_direction_t original_direction)
248 lookup_map lookups[1000];
249 unsigned int num_lookups = ARRAY_LENGTH (lookups);
252 if (!hb_ot_layout_has_positioning (face))
255 setup_lookups (face, buffer, features, num_features,
257 lookups, &num_lookups,
260 for (i = 0; i < num_lookups; i++)
261 hb_ot_layout_position_lookup (font, face, buffer, lookups[i].index, lookups[i].mask);
263 hb_ot_layout_position_finish (font, face, buffer);
273 static inline hb_bool_t
274 is_variation_selector (hb_codepoint_t unicode)
276 return unlikely ((unicode >= 0x180B && unicode <= 0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
277 (unicode >= 0xFE00 && unicode <= 0xFE0F) || /* VARIATION SELECTOR-1..16 */
278 (unicode >= 0xE0100 && unicode <= 0xE01EF)); /* VARIATION SELECTOR-17..256 */
282 hb_form_clusters (hb_buffer_t *buffer)
284 unsigned int count = buffer->len;
285 for (unsigned int i = 1; i < count; i++)
286 if (buffer->unicode->v.get_general_category (buffer->info[i].codepoint) == HB_CATEGORY_NON_SPACING_MARK)
287 buffer->info[i].cluster = buffer->info[i - 1].cluster;
290 static hb_direction_t
291 hb_ensure_native_direction (hb_buffer_t *buffer)
293 hb_direction_t original_direction = buffer->direction;
296 if (HB_DIRECTION_IS_HORIZONTAL (original_direction) &&
297 original_direction != _hb_script_get_horizontal_direction (buffer->script))
299 hb_buffer_reverse_clusters (buffer);
300 buffer->direction = HB_DIRECTION_REVERSE (buffer->direction);
303 return original_direction;
310 hb_mirror_chars (hb_buffer_t *buffer)
312 hb_unicode_get_mirroring_func_t get_mirroring = buffer->unicode->v.get_mirroring;
314 if (HB_DIRECTION_IS_FORWARD (buffer->direction))
317 unsigned int count = buffer->len;
318 for (unsigned int i = 0; i < count; i++) {
319 hb_codepoint_t codepoint = get_mirroring (buffer->info[i].codepoint);
320 if (likely (codepoint == buffer->info[i].codepoint))
321 buffer->info[i].mask |= MASK_RTLM;
323 buffer->info[i].codepoint = codepoint;
328 hb_map_glyphs (hb_font_t *font,
332 if (unlikely (!buffer->len))
335 unsigned int count = buffer->len - 1;
336 for (unsigned int i = 0; i < count; i++) {
337 if (unlikely (is_variation_selector (buffer->info[i + 1].codepoint))) {
338 buffer->info[i].codepoint = hb_font_get_glyph (font, face, buffer->info[i].codepoint, buffer->info[i + 1].codepoint);
341 buffer->info[i].codepoint = hb_font_get_glyph (font, face, buffer->info[i].codepoint, 0);
344 buffer->info[count].codepoint = hb_font_get_glyph (font, face, buffer->info[count].codepoint, 0);
348 hb_substitute_default (hb_font_t *font,
351 hb_feature_t *features HB_UNUSED,
352 unsigned int num_features HB_UNUSED)
354 hb_map_glyphs (font, face, buffer);
358 hb_substitute_complex_fallback (hb_font_t *font HB_UNUSED,
359 hb_face_t *face HB_UNUSED,
360 hb_buffer_t *buffer HB_UNUSED,
361 hb_feature_t *features HB_UNUSED,
362 unsigned int num_features HB_UNUSED)
371 hb_position_default (hb_font_t *font,
374 hb_feature_t *features HB_UNUSED,
375 unsigned int num_features HB_UNUSED)
377 hb_buffer_clear_positions (buffer);
379 unsigned int count = buffer->len;
380 for (unsigned int i = 0; i < count; i++) {
381 hb_glyph_metrics_t metrics;
382 hb_font_get_glyph_metrics (font, face, buffer->info[i].codepoint, &metrics);
383 buffer->pos[i].x_advance = metrics.x_advance;
384 buffer->pos[i].y_advance = metrics.y_advance;
389 hb_position_complex_fallback (hb_font_t *font HB_UNUSED,
390 hb_face_t *face HB_UNUSED,
391 hb_buffer_t *buffer HB_UNUSED,
392 hb_feature_t *features HB_UNUSED,
393 unsigned int num_features HB_UNUSED)
399 hb_truetype_kern (hb_font_t *font,
402 hb_feature_t *features HB_UNUSED,
403 unsigned int num_features HB_UNUSED)
405 /* TODO Check for kern=0 */
406 unsigned int count = buffer->len;
407 for (unsigned int i = 1; i < count; i++) {
408 hb_position_t kern, kern1, kern2;
409 kern = hb_font_get_kerning (font, face, buffer->info[i - 1].codepoint, buffer->info[i].codepoint);
411 kern2 = kern - kern1;
412 buffer->pos[i - 1].x_advance += kern1;
413 buffer->pos[i].x_advance += kern2;
414 buffer->pos[i].x_offset += kern2;
419 hb_position_complex_fallback_visual (hb_font_t *font,
422 hb_feature_t *features,
423 unsigned int num_features)
425 hb_truetype_kern (font, face, buffer, features, num_features);
432 hb_ot_shape (hb_font_t *font,
435 hb_feature_t *features,
436 unsigned int num_features)
438 hb_direction_t original_direction;
439 hb_bool_t substitute_fallback, position_fallback;
441 hb_form_clusters (buffer);
445 buffer->clear_masks ();
447 /* Mirroring needs to see the original direction */
448 hb_mirror_chars (buffer);
450 original_direction = hb_ensure_native_direction (buffer);
452 hb_substitute_default (font, face, buffer, features, num_features);
454 substitute_fallback = !hb_ot_substitute_complex (font, face, buffer, features, num_features, original_direction);
456 if (substitute_fallback)
457 hb_substitute_complex_fallback (font, face, buffer, features, num_features);
462 buffer->clear_masks ();
464 hb_position_default (font, face, buffer, features, num_features);
466 position_fallback = !hb_ot_position_complex (font, face, buffer, features, num_features, original_direction);
468 if (position_fallback)
469 hb_position_complex_fallback (font, face, buffer, features, num_features);
471 if (HB_DIRECTION_IS_BACKWARD (buffer->direction))
472 hb_buffer_reverse (buffer);
474 if (position_fallback)
475 hb_position_complex_fallback_visual (font, face, buffer, features, num_features);
477 buffer->direction = original_direction;