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.h"
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 cmp_lookups (const void *p1, const void *p2)
89 const lookup_map *a = (const lookup_map *) p1;
90 const lookup_map *b = (const lookup_map *) p2;
92 return a->index - b->index;
96 #define MAX_FEATURES 100
105 struct feature_setup_state {
106 hb_mask_t global_mask;
107 unsigned int next_bit;
109 feature_info info[MAX_FEATURES];
113 collect_feature_info(hb_buffer_t *buffer,
116 unsigned int script_index,
117 unsigned int language_index,
118 const hb_feature_t &feature,
119 feature_setup_state &fss,
120 const hb_feature_t *features,
121 unsigned int num_features)
123 if (fss.count == MAX_FEATURES)
124 return; // FIXME - make the feature_info array growable?
126 unsigned int i, feature_index;
127 if (!hb_ot_layout_language_find_feature (face, table_tag, script_index, language_index,
128 feature.tag, &feature_index))
131 fss.info[fss.count].tag = feature.tag;
132 fss.info[fss.count].index = feature_index;
134 // check if we have already allocated mask bits for this feature tag
135 for (i = 0; i < fss.count; ++i)
137 if (fss.info[i].tag == feature.tag)
139 fss.info[fss.count].mask = fss.info[i].mask;
140 fss.info[fss.count].value = feature.value << _hb_ctz(fss.info[fss.count].mask);
141 if (feature.start == 0 && feature.end == (unsigned int) -1)
142 fss.global_mask |= fss.info[fss.count].value;
144 buffer->set_masks(fss.info[fss.count].value, fss.info[fss.count].mask, feature.start, feature.end);
150 // check if the feature occurs in remaining user features, as it might have a larger value there
151 hb_bool_t always_on = (feature.value == 1 && feature.start == 0 && feature.end == (unsigned int)-1);
152 unsigned int max_value = feature.value;
153 for (i = 0; i < num_features; ++i)
155 if (features[i].tag != feature.tag)
157 if (features[i].value > max_value)
158 max_value = features[i].value;
159 else if (features[i].value == 0 && features[i].start == 0 && features[i].end == (unsigned int)-1)
163 if (always_on && max_value == 1)
165 fss.info[fss.count].value = 1;
166 fss.info[fss.count].mask = 1;
167 fss.global_mask |= 1;
172 // need to allocate specific mask bit(s) for this feature
173 unsigned int bits_needed = _hb_bit_storage(max_value);
174 if (!bits_needed || fss.next_bit + bits_needed > 8 * sizeof (hb_mask_t))
176 fss.info[fss.count].value = 0;
177 fss.info[fss.count].mask = 0;
179 return; // feature is disabled, just omit it; or
180 // not enough bits available in the mask, ignore this feature :(
183 // store the newly-allocated mask (in case further feature requests use the same tag)
184 // and shift the value into the right position
185 fss.info[fss.count].value = feature.value << fss.next_bit;
186 fss.info[fss.count].mask = (1 << (fss.next_bit + bits_needed)) - (1 << fss.next_bit);
187 if (feature.start == 0 && feature.end == (unsigned int) -1)
188 fss.global_mask |= fss.info[fss.count].value;
190 buffer->set_masks(fss.info[fss.count].value, fss.info[fss.count].mask, feature.start, feature.end);
191 fss.next_bit += bits_needed;
196 setup_lookups (hb_face_t *face,
198 hb_feature_t *features,
199 unsigned int num_features,
202 unsigned int *num_lookups,
203 hb_direction_t original_direction)
205 unsigned int i, j, script_index, language_index, feature_index, room_lookups;
207 room_lookups = *num_lookups;
210 hb_ot_layout_table_choose_script (face, table_tag,
211 hb_ot_tags_from_script (buffer->script),
213 hb_ot_layout_script_find_language (face, table_tag, script_index,
214 hb_ot_tag_from_language (buffer->language),
217 if (hb_ot_layout_language_get_required_feature_index (face, table_tag, script_index, language_index,
219 add_feature (face, table_tag, feature_index, 1, lookups, num_lookups, room_lookups);
221 // for features that may be turned on/off or have value > 1,
222 // we need to allocate bits in the mask
224 feature_setup_state fss;
226 fss.next_bit = MASK_BITS_USED;
229 hb_feature_t feature = { 0, 1, 0, (unsigned int)-1 };
231 switch (original_direction) {
232 case HB_DIRECTION_LTR:
233 feature.tag = HB_TAG ('l','t','r','a');
234 collect_feature_info(buffer, face, table_tag, script_index, language_index,
235 feature, fss, features, num_features);
236 feature.tag = HB_TAG ('l','t','r','m');
237 collect_feature_info(buffer, face, table_tag, script_index, language_index,
238 feature, fss, features, num_features);
240 case HB_DIRECTION_RTL:
241 feature.tag = HB_TAG ('r','t','l','a');
242 collect_feature_info(buffer, face, table_tag, script_index, language_index,
243 feature, fss, features, num_features);
245 case HB_DIRECTION_TTB:
246 case HB_DIRECTION_BTT:
251 for (i = 0; i < ARRAY_LENGTH (default_features); i++)
253 feature.tag = default_features[i];
254 collect_feature_info(buffer, face, table_tag, script_index, language_index,
255 feature, fss, features, num_features);
258 for (i = 0; i < num_features; i++)
260 collect_feature_info(buffer, face, table_tag, script_index, language_index,
261 features[i], fss, features + i + 1, num_features - i - 1);
264 for (i = 0; i < fss.count; ++i)
265 if (fss.info[i].mask)
266 add_feature (face, table_tag, fss.info[i].index, fss.info[i].mask, lookups, num_lookups, room_lookups);
268 if (fss.global_mask > 1) // the 0x01 bit was set by clear_masks()
269 buffer->set_masks (fss.global_mask, fss.global_mask, 0, (unsigned int) -1);
271 qsort (lookups, *num_lookups, sizeof (lookups[0]), cmp_lookups);
275 for (i = 1, j = 0; i < *num_lookups; i++)
276 if (lookups[i].index != lookups[j].index)
277 lookups[++j] = lookups[i];
279 lookups[j].mask |= lookups[i].mask;
287 hb_ot_substitute_complex (hb_font_t *font HB_UNUSED,
290 hb_feature_t *features,
291 unsigned int num_features,
292 hb_direction_t original_direction)
294 lookup_map lookups[1000]; /* FIXME */
295 unsigned int num_lookups = ARRAY_LENGTH (lookups);
298 if (!hb_ot_layout_has_substitution (face))
301 setup_lookups (face, buffer, features, num_features,
303 lookups, &num_lookups,
306 for (i = 0; i < num_lookups; i++)
307 hb_ot_layout_substitute_lookup (face, buffer, lookups[i].index, lookups[i].mask);
313 hb_ot_position_complex (hb_font_t *font,
316 hb_feature_t *features,
317 unsigned int num_features,
318 hb_direction_t original_direction)
320 lookup_map lookups[1000];
321 unsigned int num_lookups = ARRAY_LENGTH (lookups);
324 if (!hb_ot_layout_has_positioning (face))
327 setup_lookups (face, buffer, features, num_features,
329 lookups, &num_lookups,
332 for (i = 0; i < num_lookups; i++)
333 hb_ot_layout_position_lookup (font, face, buffer, lookups[i].index, lookups[i].mask);
335 hb_ot_layout_position_finish (font, face, buffer);
345 static inline hb_bool_t
346 is_variation_selector (hb_codepoint_t unicode)
348 return unlikely ((unicode >= 0x180B && unicode <= 0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
349 (unicode >= 0xFE00 && unicode <= 0xFE0F) || /* VARIATION SELECTOR-1..16 */
350 (unicode >= 0xE0100 && unicode <= 0xE01EF)); /* VARIATION SELECTOR-17..256 */
354 hb_form_clusters (hb_buffer_t *buffer)
356 unsigned int count = buffer->len;
357 for (unsigned int i = 1; i < count; i++)
358 if (buffer->unicode->v.get_general_category (buffer->info[i].codepoint) == HB_CATEGORY_NON_SPACING_MARK)
359 buffer->info[i].cluster = buffer->info[i - 1].cluster;
362 static hb_direction_t
363 hb_ensure_native_direction (hb_buffer_t *buffer)
365 hb_direction_t original_direction = buffer->direction;
368 if (HB_DIRECTION_IS_HORIZONTAL (original_direction) &&
369 original_direction != _hb_script_get_horizontal_direction (buffer->script))
371 hb_buffer_reverse_clusters (buffer);
372 buffer->direction = HB_DIRECTION_REVERSE (buffer->direction);
375 return original_direction;
382 hb_mirror_chars (hb_buffer_t *buffer)
384 hb_unicode_get_mirroring_func_t get_mirroring = buffer->unicode->v.get_mirroring;
386 if (HB_DIRECTION_IS_FORWARD (buffer->direction))
389 unsigned int count = buffer->len;
390 for (unsigned int i = 0; i < count; i++) {
391 hb_codepoint_t codepoint = get_mirroring (buffer->info[i].codepoint);
392 if (likely (codepoint == buffer->info[i].codepoint))
393 buffer->info[i].mask |= MASK_RTLM;
395 buffer->info[i].codepoint = codepoint;
400 hb_map_glyphs (hb_font_t *font,
404 if (unlikely (!buffer->len))
407 unsigned int count = buffer->len - 1;
408 for (unsigned int i = 0; i < count; i++) {
409 if (unlikely (is_variation_selector (buffer->info[i + 1].codepoint))) {
410 buffer->info[i].codepoint = hb_font_get_glyph (font, face, buffer->info[i].codepoint, buffer->info[i + 1].codepoint);
413 buffer->info[i].codepoint = hb_font_get_glyph (font, face, buffer->info[i].codepoint, 0);
416 buffer->info[count].codepoint = hb_font_get_glyph (font, face, buffer->info[count].codepoint, 0);
420 hb_substitute_default (hb_font_t *font,
423 hb_feature_t *features HB_UNUSED,
424 unsigned int num_features HB_UNUSED)
426 hb_map_glyphs (font, face, buffer);
430 hb_substitute_complex_fallback (hb_font_t *font HB_UNUSED,
431 hb_face_t *face HB_UNUSED,
432 hb_buffer_t *buffer HB_UNUSED,
433 hb_feature_t *features HB_UNUSED,
434 unsigned int num_features HB_UNUSED)
443 hb_position_default (hb_font_t *font,
446 hb_feature_t *features HB_UNUSED,
447 unsigned int num_features HB_UNUSED)
449 hb_buffer_clear_positions (buffer);
451 unsigned int count = buffer->len;
452 for (unsigned int i = 0; i < count; i++) {
453 hb_glyph_metrics_t metrics;
454 hb_font_get_glyph_metrics (font, face, buffer->info[i].codepoint, &metrics);
455 buffer->pos[i].x_advance = metrics.x_advance;
456 buffer->pos[i].y_advance = metrics.y_advance;
461 hb_position_complex_fallback (hb_font_t *font HB_UNUSED,
462 hb_face_t *face HB_UNUSED,
463 hb_buffer_t *buffer HB_UNUSED,
464 hb_feature_t *features HB_UNUSED,
465 unsigned int num_features HB_UNUSED)
471 hb_truetype_kern (hb_font_t *font,
474 hb_feature_t *features HB_UNUSED,
475 unsigned int num_features HB_UNUSED)
477 /* TODO Check for kern=0 */
478 unsigned int count = buffer->len;
479 for (unsigned int i = 1; i < count; i++) {
480 hb_position_t kern, kern1, kern2;
481 kern = hb_font_get_kerning (font, face, buffer->info[i - 1].codepoint, buffer->info[i].codepoint);
483 kern2 = kern - kern1;
484 buffer->pos[i - 1].x_advance += kern1;
485 buffer->pos[i].x_advance += kern2;
486 buffer->pos[i].x_offset += kern2;
491 hb_position_complex_fallback_visual (hb_font_t *font,
494 hb_feature_t *features,
495 unsigned int num_features)
497 hb_truetype_kern (font, face, buffer, features, num_features);
504 hb_ot_shape (hb_font_t *font,
507 hb_feature_t *features,
508 unsigned int num_features)
510 hb_direction_t original_direction;
511 hb_bool_t substitute_fallback, position_fallback;
513 hb_form_clusters (buffer);
517 buffer->clear_masks ();
519 /* Mirroring needs to see the original direction */
520 hb_mirror_chars (buffer);
522 original_direction = hb_ensure_native_direction (buffer);
524 hb_substitute_default (font, face, buffer, features, num_features);
526 substitute_fallback = !hb_ot_substitute_complex (font, face, buffer, features, num_features, original_direction);
528 if (substitute_fallback)
529 hb_substitute_complex_fallback (font, face, buffer, features, num_features);
534 buffer->clear_masks ();
536 hb_position_default (font, face, buffer, features, num_features);
538 position_fallback = !hb_ot_position_complex (font, face, buffer, features, num_features, original_direction);
540 if (position_fallback)
541 hb_position_complex_fallback (font, face, buffer, features, num_features);
543 if (HB_DIRECTION_IS_BACKWARD (buffer->direction))
544 hb_buffer_reverse (buffer);
546 if (position_fallback)
547 hb_position_complex_fallback_visual (font, face, buffer, features, num_features);
549 buffer->direction = original_direction;