Behdad Esfahbod | 15232e2 | 2009-08-13 17:13:25 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 Red Hat, Inc. |
| 3 | * |
Behdad Esfahbod | c755cb3 | 2010-04-22 00:11:43 -0400 | [diff] [blame^] | 4 | * This is part of HarfBuzz, a text shaping library. |
Behdad Esfahbod | 15232e2 | 2009-08-13 17:13:25 -0400 | [diff] [blame] | 5 | * |
| 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. |
| 11 | * |
| 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 |
| 16 | * DAMAGE. |
| 17 | * |
| 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. |
| 23 | * |
| 24 | * Red Hat Author(s): Behdad Esfahbod |
| 25 | */ |
| 26 | |
| 27 | #include "hb-private.h" |
| 28 | |
| 29 | #include "hb-shape.h" |
| 30 | |
Behdad Esfahbod | 2f50d87 | 2009-11-04 21:07:03 -0500 | [diff] [blame] | 31 | #include "hb-buffer-private.h" |
| 32 | |
Behdad Esfahbod | 2014b8d | 2009-12-20 20:58:26 +0100 | [diff] [blame] | 33 | #include "hb-ot-shape-private.h" |
| 34 | |
Behdad Esfahbod | 26d7a75 | 2009-12-20 17:58:25 +0100 | [diff] [blame] | 35 | |
| 36 | /* Prepare */ |
| 37 | |
Behdad Esfahbod | 2f50d87 | 2009-11-04 21:07:03 -0500 | [diff] [blame] | 38 | static inline hb_bool_t |
| 39 | is_variation_selector (hb_codepoint_t unicode) |
| 40 | { |
Behdad Esfahbod | d33f674 | 2009-11-18 09:47:44 -0500 | [diff] [blame] | 41 | return HB_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 */ |
Behdad Esfahbod | 2f50d87 | 2009-11-04 21:07:03 -0500 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | static void |
Behdad Esfahbod | ff44f88 | 2009-11-06 19:48:16 -0500 | [diff] [blame] | 47 | hb_form_clusters (hb_buffer_t *buffer) |
| 48 | { |
| 49 | unsigned int count; |
| 50 | |
| 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 | IN_CLUSTER (buffer->in_pos) = IN_CLUSTER (buffer->in_pos - 1); |
| 55 | } |
| 56 | |
Behdad Esfahbod | 001fc2d | 2009-12-20 17:24:05 +0100 | [diff] [blame] | 57 | static hb_direction_t |
| 58 | hb_ensure_native_direction (hb_buffer_t *buffer) |
| 59 | { |
| 60 | hb_direction_t original_direction = buffer->direction; |
| 61 | |
| 62 | /* TODO vertical */ |
| 63 | if (HB_DIRECTION_IS_HORIZONTAL (original_direction) && |
| 64 | original_direction != _hb_script_get_horizontal_direction (buffer->script)) |
| 65 | { |
| 66 | hb_buffer_reverse_clusters (buffer); |
| 67 | buffer->direction ^= 1; |
| 68 | } |
| 69 | |
| 70 | return original_direction; |
| 71 | } |
| 72 | |
Behdad Esfahbod | 26d7a75 | 2009-12-20 17:58:25 +0100 | [diff] [blame] | 73 | |
| 74 | /* Substitute */ |
| 75 | |
Behdad Esfahbod | 001fc2d | 2009-12-20 17:24:05 +0100 | [diff] [blame] | 76 | static void |
| 77 | hb_mirror_chars (hb_buffer_t *buffer) |
| 78 | { |
| 79 | unsigned int count; |
| 80 | hb_unicode_get_mirroring_func_t get_mirroring = buffer->unicode->get_mirroring; |
| 81 | |
| 82 | if (HB_DIRECTION_IS_FORWARD (buffer->direction)) |
| 83 | return; |
| 84 | |
| 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()); |
| 88 | } |
| 89 | } |
| 90 | |
Behdad Esfahbod | ff44f88 | 2009-11-06 19:48:16 -0500 | [diff] [blame] | 91 | static void |
Behdad Esfahbod | 2027f74 | 2009-11-05 16:34:47 -0500 | [diff] [blame] | 92 | hb_map_glyphs (hb_font_t *font, |
| 93 | hb_face_t *face, |
| 94 | hb_buffer_t *buffer) |
Behdad Esfahbod | 2f50d87 | 2009-11-04 21:07:03 -0500 | [diff] [blame] | 95 | { |
| 96 | unsigned int count; |
| 97 | |
Behdad Esfahbod | 51f141a | 2009-12-20 18:22:28 +0100 | [diff] [blame] | 98 | if (HB_UNLIKELY (!buffer->in_length)) |
| 99 | return; |
Behdad Esfahbod | 2f50d87 | 2009-11-04 21:07:03 -0500 | [diff] [blame] | 100 | count = buffer->in_length - 1; |
Behdad Esfahbod | 2f50d87 | 2009-11-04 21:07:03 -0500 | [diff] [blame] | 101 | for (buffer->in_pos = 0; buffer->in_pos < count; buffer->in_pos++) { |
| 102 | if (HB_UNLIKELY (is_variation_selector (IN_NEXTGLYPH()))) { |
Behdad Esfahbod | 9bef361 | 2009-11-05 12:20:11 -0500 | [diff] [blame] | 103 | IN_CURGLYPH() = hb_font_get_glyph (font, face, IN_CURGLYPH(), IN_NEXTGLYPH()); |
Behdad Esfahbod | 2f50d87 | 2009-11-04 21:07:03 -0500 | [diff] [blame] | 104 | buffer->in_pos++; |
| 105 | } else { |
Behdad Esfahbod | 9bef361 | 2009-11-05 12:20:11 -0500 | [diff] [blame] | 106 | IN_CURGLYPH() = hb_font_get_glyph (font, face, IN_CURGLYPH(), 0); |
Behdad Esfahbod | 2f50d87 | 2009-11-04 21:07:03 -0500 | [diff] [blame] | 107 | } |
| 108 | } |
Behdad Esfahbod | 9bef361 | 2009-11-05 12:20:11 -0500 | [diff] [blame] | 109 | IN_CURGLYPH() = hb_font_get_glyph (font, face, IN_CURGLYPH(), 0); |
Behdad Esfahbod | 2f50d87 | 2009-11-04 21:07:03 -0500 | [diff] [blame] | 110 | } |
| 111 | |
Behdad Esfahbod | 2027f74 | 2009-11-05 16:34:47 -0500 | [diff] [blame] | 112 | static void |
Behdad Esfahbod | 26d7a75 | 2009-12-20 17:58:25 +0100 | [diff] [blame] | 113 | hb_substitute_default (hb_font_t *font, |
| 114 | hb_face_t *face, |
Behdad Esfahbod | 196610b | 2009-12-20 19:01:14 +0100 | [diff] [blame] | 115 | hb_buffer_t *buffer, |
| 116 | hb_feature_t *features, |
| 117 | unsigned int num_features) |
Behdad Esfahbod | 26d7a75 | 2009-12-20 17:58:25 +0100 | [diff] [blame] | 118 | { |
| 119 | hb_mirror_chars (buffer); |
| 120 | hb_map_glyphs (font, face, buffer); |
| 121 | } |
| 122 | |
Behdad Esfahbod | 2f78c17 | 2009-12-20 21:03:11 +0100 | [diff] [blame] | 123 | static hb_bool_t |
Behdad Esfahbod | 26d7a75 | 2009-12-20 17:58:25 +0100 | [diff] [blame] | 124 | hb_substitute_complex (hb_font_t *font, |
| 125 | hb_face_t *face, |
Behdad Esfahbod | 196610b | 2009-12-20 19:01:14 +0100 | [diff] [blame] | 126 | hb_buffer_t *buffer, |
| 127 | hb_feature_t *features, |
| 128 | unsigned int num_features) |
Behdad Esfahbod | 26d7a75 | 2009-12-20 17:58:25 +0100 | [diff] [blame] | 129 | { |
Behdad Esfahbod | 2014b8d | 2009-12-20 20:58:26 +0100 | [diff] [blame] | 130 | return _hb_ot_substitute_complex (font, face, buffer, features, num_features); |
Behdad Esfahbod | 26d7a75 | 2009-12-20 17:58:25 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static void |
| 134 | hb_substitute_fallback (hb_font_t *font, |
| 135 | hb_face_t *face, |
Behdad Esfahbod | 196610b | 2009-12-20 19:01:14 +0100 | [diff] [blame] | 136 | hb_buffer_t *buffer, |
| 137 | hb_feature_t *features, |
| 138 | unsigned int num_features) |
Behdad Esfahbod | 26d7a75 | 2009-12-20 17:58:25 +0100 | [diff] [blame] | 139 | { |
| 140 | /* TODO Arabic */ |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /* Position */ |
| 145 | |
| 146 | static void |
Behdad Esfahbod | 2027f74 | 2009-11-05 16:34:47 -0500 | [diff] [blame] | 147 | hb_position_default (hb_font_t *font, |
| 148 | hb_face_t *face, |
Behdad Esfahbod | 196610b | 2009-12-20 19:01:14 +0100 | [diff] [blame] | 149 | hb_buffer_t *buffer, |
| 150 | hb_feature_t *features, |
| 151 | unsigned int num_features) |
Behdad Esfahbod | 2027f74 | 2009-11-05 16:34:47 -0500 | [diff] [blame] | 152 | { |
| 153 | unsigned int count; |
| 154 | |
| 155 | hb_buffer_clear_positions (buffer); |
| 156 | |
| 157 | count = buffer->in_length; |
Behdad Esfahbod | 2027f74 | 2009-11-05 16:34:47 -0500 | [diff] [blame] | 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 | CURPOSITION()->x_advance = metrics.x_advance; |
| 162 | CURPOSITION()->y_advance = metrics.y_advance; |
| 163 | } |
| 164 | } |
| 165 | |
Behdad Esfahbod | 2f78c17 | 2009-12-20 21:03:11 +0100 | [diff] [blame] | 166 | static hb_bool_t |
Behdad Esfahbod | 26d7a75 | 2009-12-20 17:58:25 +0100 | [diff] [blame] | 167 | hb_position_complex (hb_font_t *font, |
| 168 | hb_face_t *face, |
Behdad Esfahbod | 196610b | 2009-12-20 19:01:14 +0100 | [diff] [blame] | 169 | hb_buffer_t *buffer, |
| 170 | hb_feature_t *features, |
| 171 | unsigned int num_features) |
Behdad Esfahbod | 26d7a75 | 2009-12-20 17:58:25 +0100 | [diff] [blame] | 172 | { |
Behdad Esfahbod | 2014b8d | 2009-12-20 20:58:26 +0100 | [diff] [blame] | 173 | return _hb_ot_position_complex (font, face, buffer, features, num_features); |
Behdad Esfahbod | 26d7a75 | 2009-12-20 17:58:25 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static void |
| 177 | hb_position_fallback (hb_font_t *font, |
| 178 | hb_face_t *face, |
Behdad Esfahbod | 196610b | 2009-12-20 19:01:14 +0100 | [diff] [blame] | 179 | hb_buffer_t *buffer, |
| 180 | hb_feature_t *features, |
| 181 | unsigned int num_features) |
Behdad Esfahbod | 26d7a75 | 2009-12-20 17:58:25 +0100 | [diff] [blame] | 182 | { |
| 183 | /* TODO Mark pos */ |
| 184 | } |
| 185 | |
Behdad Esfahbod | 6a2ef5a | 2009-12-20 16:28:01 +0100 | [diff] [blame] | 186 | static void |
Behdad Esfahbod | 001fc2d | 2009-12-20 17:24:05 +0100 | [diff] [blame] | 187 | hb_truetype_kern (hb_font_t *font, |
| 188 | hb_face_t *face, |
Behdad Esfahbod | 196610b | 2009-12-20 19:01:14 +0100 | [diff] [blame] | 189 | hb_buffer_t *buffer, |
| 190 | hb_feature_t *features, |
| 191 | unsigned int num_features) |
Behdad Esfahbod | 6a2ef5a | 2009-12-20 16:28:01 +0100 | [diff] [blame] | 192 | { |
| 193 | unsigned int count; |
Behdad Esfahbod | 6a2ef5a | 2009-12-20 16:28:01 +0100 | [diff] [blame] | 194 | |
Behdad Esfahbod | 196610b | 2009-12-20 19:01:14 +0100 | [diff] [blame] | 195 | /* TODO Check for kern=0 */ |
Behdad Esfahbod | 6a2ef5a | 2009-12-20 16:28:01 +0100 | [diff] [blame] | 196 | count = buffer->in_length; |
Behdad Esfahbod | 001fc2d | 2009-12-20 17:24:05 +0100 | [diff] [blame] | 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, IN_GLYPH(buffer->in_pos - 1), IN_CURGLYPH()); |
| 200 | kern1 = kern >> 1; |
| 201 | kern2 = kern - kern1; |
| 202 | POSITION(buffer->in_pos - 1)->x_advance += kern1; |
| 203 | CURPOSITION()->x_advance += kern2; |
| 204 | CURPOSITION()->x_offset += kern2; |
Behdad Esfahbod | 6a2ef5a | 2009-12-20 16:28:01 +0100 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Behdad Esfahbod | 26d7a75 | 2009-12-20 17:58:25 +0100 | [diff] [blame] | 208 | static void |
| 209 | hb_position_fallback_visual (hb_font_t *font, |
| 210 | hb_face_t *face, |
Behdad Esfahbod | 196610b | 2009-12-20 19:01:14 +0100 | [diff] [blame] | 211 | hb_buffer_t *buffer, |
| 212 | hb_feature_t *features, |
| 213 | unsigned int num_features) |
Behdad Esfahbod | 26d7a75 | 2009-12-20 17:58:25 +0100 | [diff] [blame] | 214 | { |
Behdad Esfahbod | 196610b | 2009-12-20 19:01:14 +0100 | [diff] [blame] | 215 | hb_truetype_kern (font, face, buffer, features, num_features); |
Behdad Esfahbod | 26d7a75 | 2009-12-20 17:58:25 +0100 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | |
| 219 | /* Shape */ |
| 220 | |
Behdad Esfahbod | 15232e2 | 2009-08-13 17:13:25 -0400 | [diff] [blame] | 221 | void |
Behdad Esfahbod | 9bef361 | 2009-11-05 12:20:11 -0500 | [diff] [blame] | 222 | hb_shape (hb_font_t *font, |
| 223 | hb_face_t *face, |
Behdad Esfahbod | 15232e2 | 2009-08-13 17:13:25 -0400 | [diff] [blame] | 224 | hb_buffer_t *buffer, |
| 225 | hb_feature_t *features, |
| 226 | unsigned int num_features) |
| 227 | { |
Behdad Esfahbod | ff44f88 | 2009-11-06 19:48:16 -0500 | [diff] [blame] | 228 | hb_direction_t original_direction; |
Behdad Esfahbod | 26d7a75 | 2009-12-20 17:58:25 +0100 | [diff] [blame] | 229 | hb_bool_t substitute_fallback, position_fallback; |
Behdad Esfahbod | ff44f88 | 2009-11-06 19:48:16 -0500 | [diff] [blame] | 230 | |
| 231 | hb_form_clusters (buffer); |
| 232 | original_direction = hb_ensure_native_direction (buffer); |
| 233 | |
Behdad Esfahbod | 196610b | 2009-12-20 19:01:14 +0100 | [diff] [blame] | 234 | hb_substitute_default (font, face, buffer, features, num_features); |
Behdad Esfahbod | 6a2ef5a | 2009-12-20 16:28:01 +0100 | [diff] [blame] | 235 | |
Behdad Esfahbod | 196610b | 2009-12-20 19:01:14 +0100 | [diff] [blame] | 236 | substitute_fallback = !hb_substitute_complex (font, face, buffer, features, num_features); |
Behdad Esfahbod | 15232e2 | 2009-08-13 17:13:25 -0400 | [diff] [blame] | 237 | |
Behdad Esfahbod | 26d7a75 | 2009-12-20 17:58:25 +0100 | [diff] [blame] | 238 | if (substitute_fallback) |
Behdad Esfahbod | 196610b | 2009-12-20 19:01:14 +0100 | [diff] [blame] | 239 | hb_substitute_fallback (font, face, buffer, features, num_features); |
Behdad Esfahbod | 1ff7775 | 2009-11-06 13:52:57 -0500 | [diff] [blame] | 240 | |
Behdad Esfahbod | 196610b | 2009-12-20 19:01:14 +0100 | [diff] [blame] | 241 | hb_position_default (font, face, buffer, features, num_features); |
Behdad Esfahbod | 1ff7775 | 2009-11-06 13:52:57 -0500 | [diff] [blame] | 242 | |
Behdad Esfahbod | 196610b | 2009-12-20 19:01:14 +0100 | [diff] [blame] | 243 | position_fallback = !hb_position_complex (font, face, buffer, features, num_features); |
Behdad Esfahbod | 26d7a75 | 2009-12-20 17:58:25 +0100 | [diff] [blame] | 244 | |
| 245 | if (position_fallback) |
Behdad Esfahbod | 196610b | 2009-12-20 19:01:14 +0100 | [diff] [blame] | 246 | hb_position_fallback (font, face, buffer, features, num_features); |
Behdad Esfahbod | ff44f88 | 2009-11-06 19:48:16 -0500 | [diff] [blame] | 247 | |
Behdad Esfahbod | 314b460 | 2009-12-20 13:58:50 +0100 | [diff] [blame] | 248 | if (HB_DIRECTION_IS_BACKWARD (buffer->direction)) |
Behdad Esfahbod | 4a86053 | 2009-11-06 19:52:01 -0500 | [diff] [blame] | 249 | hb_buffer_reverse (buffer); |
| 250 | |
Behdad Esfahbod | 26d7a75 | 2009-12-20 17:58:25 +0100 | [diff] [blame] | 251 | if (position_fallback) |
Behdad Esfahbod | 196610b | 2009-12-20 19:01:14 +0100 | [diff] [blame] | 252 | hb_position_fallback_visual (font, face, buffer, features, num_features); |
Behdad Esfahbod | 001fc2d | 2009-12-20 17:24:05 +0100 | [diff] [blame] | 253 | |
Behdad Esfahbod | 4a86053 | 2009-11-06 19:52:01 -0500 | [diff] [blame] | 254 | buffer->direction = original_direction; |
Behdad Esfahbod | 2f50d87 | 2009-11-04 21:07:03 -0500 | [diff] [blame] | 255 | } |