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-private.h"
31 #include "hb-buffer-private.hh"
33 #include "hb-ot-shape-private.hh"
38 static inline hb_bool_t
39 is_variation_selector (hb_codepoint_t unicode)
41 return unlikely ((unicode >= 0x180B && unicode <= 0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR ONE..THREE */
42 (unicode >= 0xFE00 && unicode <= 0xFE0F) || /* VARIATION SELECTOR-1..16 */
43 (unicode >= 0xE0100 && unicode <= 0xE01EF)); /* VARIATION SELECTOR-17..256 */
47 hb_form_clusters (hb_buffer_t *buffer)
51 count = buffer->in_length;
52 for (buffer->in_pos = 1; buffer->in_pos < count; buffer->in_pos++)
53 if (buffer->unicode->get_general_category (IN_CURGLYPH()) == HB_CATEGORY_NON_SPACING_MARK)
54 buffer->in_string[buffer->in_pos].cluster = buffer->in_string[buffer->in_pos - 1].cluster;
58 hb_ensure_native_direction (hb_buffer_t *buffer)
60 hb_direction_t original_direction = buffer->direction;
63 if (HB_DIRECTION_IS_HORIZONTAL (original_direction) &&
64 original_direction != _hb_script_get_horizontal_direction (buffer->script))
66 hb_buffer_reverse_clusters (buffer);
67 buffer->direction = HB_DIRECTION_REVERSE (buffer->direction);
70 return original_direction;
77 hb_mirror_chars (hb_buffer_t *buffer)
80 hb_unicode_get_mirroring_func_t get_mirroring = buffer->unicode->get_mirroring;
82 if (HB_DIRECTION_IS_FORWARD (buffer->direction))
85 count = buffer->in_length;
86 for (buffer->in_pos = 0; buffer->in_pos < count; buffer->in_pos++) {
87 IN_CURGLYPH() = get_mirroring (IN_CURGLYPH());
92 hb_map_glyphs (hb_font_t *font,
98 if (unlikely (!buffer->in_length))
100 count = buffer->in_length - 1;
101 for (buffer->in_pos = 0; buffer->in_pos < count; buffer->in_pos++) {
102 if (unlikely (is_variation_selector (buffer->in_string[buffer->in_pos + 1].codepoint))) {
103 IN_CURGLYPH() = hb_font_get_glyph (font, face, IN_CURGLYPH(), buffer->in_string[buffer->in_pos + 1].codepoint);
106 IN_CURGLYPH() = hb_font_get_glyph (font, face, IN_CURGLYPH(), 0);
109 IN_CURGLYPH() = hb_font_get_glyph (font, face, IN_CURGLYPH(), 0);
113 hb_substitute_default (hb_font_t *font,
116 hb_feature_t *features HB_UNUSED,
117 unsigned int num_features HB_UNUSED)
119 hb_mirror_chars (buffer);
120 hb_map_glyphs (font, face, buffer);
124 hb_substitute_complex (hb_font_t *font,
127 hb_feature_t *features,
128 unsigned int num_features)
130 return _hb_ot_substitute_complex (font, face, buffer, features, num_features);
134 hb_substitute_fallback (hb_font_t *font HB_UNUSED,
135 hb_face_t *face HB_UNUSED,
136 hb_buffer_t *buffer HB_UNUSED,
137 hb_feature_t *features HB_UNUSED,
138 unsigned int num_features HB_UNUSED)
147 hb_position_default (hb_font_t *font,
150 hb_feature_t *features HB_UNUSED,
151 unsigned int num_features HB_UNUSED)
155 hb_buffer_clear_positions (buffer);
157 count = buffer->in_length;
158 for (buffer->in_pos = 0; buffer->in_pos < count; buffer->in_pos++) {
159 hb_glyph_metrics_t metrics;
160 hb_font_get_glyph_metrics (font, face, IN_CURGLYPH(), &metrics);
161 buffer->positions[buffer->in_pos].x_advance = metrics.x_advance;
162 buffer->positions[buffer->in_pos].y_advance = metrics.y_advance;
167 hb_position_complex (hb_font_t *font,
170 hb_feature_t *features,
171 unsigned int num_features)
173 return _hb_ot_position_complex (font, face, buffer, features, num_features);
177 hb_position_fallback (hb_font_t *font HB_UNUSED,
178 hb_face_t *face HB_UNUSED,
179 hb_buffer_t *buffer HB_UNUSED,
180 hb_feature_t *features HB_UNUSED,
181 unsigned int num_features HB_UNUSED)
187 hb_truetype_kern (hb_font_t *font,
190 hb_feature_t *features HB_UNUSED,
191 unsigned int num_features HB_UNUSED)
195 /* TODO Check for kern=0 */
196 count = buffer->in_length;
197 for (buffer->in_pos = 1; buffer->in_pos < count; buffer->in_pos++) {
198 hb_position_t kern, kern1, kern2;
199 kern = hb_font_get_kerning (font, face, buffer->in_string[buffer->in_pos - 1].codepoint, IN_CURGLYPH());
201 kern2 = kern - kern1;
202 buffer->positions[buffer->in_pos - 1].x_advance += kern1;
203 buffer->positions[buffer->in_pos].x_advance += kern2;
204 buffer->positions[buffer->in_pos].x_offset += kern2;
209 hb_position_fallback_visual (hb_font_t *font,
212 hb_feature_t *features,
213 unsigned int num_features)
215 hb_truetype_kern (font, face, buffer, features, num_features);
222 hb_shape (hb_font_t *font,
225 hb_feature_t *features,
226 unsigned int num_features)
228 hb_direction_t original_direction;
229 hb_bool_t substitute_fallback, position_fallback;
231 hb_form_clusters (buffer);
233 hb_substitute_default (font, face, buffer, features, num_features);
235 /* We do this after substitute_default because mirroring needs to
236 * see the original direction. */
237 original_direction = hb_ensure_native_direction (buffer);
239 substitute_fallback = !hb_substitute_complex (font, face, buffer, features, num_features);
241 if (substitute_fallback)
242 hb_substitute_fallback (font, face, buffer, features, num_features);
244 hb_position_default (font, face, buffer, features, num_features);
246 position_fallback = !hb_position_complex (font, face, buffer, features, num_features);
248 if (position_fallback)
249 hb_position_fallback (font, face, buffer, features, num_features);
251 if (HB_DIRECTION_IS_BACKWARD (buffer->direction))
252 hb_buffer_reverse (buffer);
254 if (position_fallback)
255 hb_position_fallback_visual (font, face, buffer, features, num_features);
257 buffer->direction = original_direction;