Behdad Esfahbod | 655586f | 2011-07-21 00:51:18 -0400 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 2 | * Copyright © 2011,2012 Google, Inc. |
Behdad Esfahbod | 655586f | 2011-07-21 00:51:18 -0400 | [diff] [blame] | 3 | * |
| 4 | * This is part of HarfBuzz, a text shaping library. |
| 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 | * Google Author(s): Behdad Esfahbod |
| 25 | */ |
| 26 | |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 27 | #include "hb-ot-shape-normalize-private.hh" |
Behdad Esfahbod | 655586f | 2011-07-21 00:51:18 -0400 | [diff] [blame] | 28 | #include "hb-ot-shape-private.hh" |
| 29 | |
Behdad Esfahbod | 655586f | 2011-07-21 00:51:18 -0400 | [diff] [blame] | 30 | |
Behdad Esfahbod | 5d90a34 | 2011-07-21 15:25:01 -0400 | [diff] [blame] | 31 | /* |
| 32 | * HIGHLEVEL DESIGN: |
| 33 | * |
| 34 | * This file exports one main function: _hb_ot_shape_normalize(). |
| 35 | * |
| 36 | * This function closely reflects the Unicode Normalization Algorithm, |
Behdad Esfahbod | c346671 | 2012-03-06 20:47:50 -0500 | [diff] [blame] | 37 | * yet it's different. |
| 38 | * |
| 39 | * Each shaper specifies whether it prefers decomposed (NFD) or composed (NFC). |
| 40 | * The logic however tries to use whatever the font can support. |
Behdad Esfahbod | 5d90a34 | 2011-07-21 15:25:01 -0400 | [diff] [blame] | 41 | * |
| 42 | * In general what happens is that: each grapheme is decomposed in a chain |
Behdad Esfahbod | 947c9a7 | 2011-09-16 16:33:18 -0400 | [diff] [blame] | 43 | * of 1:2 decompositions, marks reordered, and then recomposed if desired, |
Behdad Esfahbod | 5d90a34 | 2011-07-21 15:25:01 -0400 | [diff] [blame] | 44 | * so far it's like Unicode Normalization. However, the decomposition and |
| 45 | * recomposition only happens if the font supports the resulting characters. |
| 46 | * |
| 47 | * The goals are: |
| 48 | * |
| 49 | * - Try to render all canonically equivalent strings similarly. To really |
| 50 | * achieve this we have to always do the full decomposition and then |
| 51 | * selectively recompose from there. It's kinda too expensive though, so |
| 52 | * we skip some cases. For example, if composed is desired, we simply |
| 53 | * don't touch 1-character clusters that are supported by the font, even |
| 54 | * though their NFC may be different. |
| 55 | * |
| 56 | * - When a font has a precomposed character for a sequence but the 'ccmp' |
Behdad Esfahbod | 947c9a7 | 2011-09-16 16:33:18 -0400 | [diff] [blame] | 57 | * feature in the font is not adequate, use the precomposed character |
Behdad Esfahbod | 5d90a34 | 2011-07-21 15:25:01 -0400 | [diff] [blame] | 58 | * which typically has better mark positioning. |
| 59 | * |
Behdad Esfahbod | 55deff7 | 2011-09-28 16:20:09 -0400 | [diff] [blame] | 60 | * - When a font does not support a combining mark, but supports it precomposed |
Behdad Esfahbod | c346671 | 2012-03-06 20:47:50 -0500 | [diff] [blame] | 61 | * with previous base, use that. This needs the itemizer to have this |
Behdad Esfahbod | e3b2e07 | 2012-03-07 10:21:24 -0500 | [diff] [blame] | 62 | * knowledge too. We need to provide assistance to the itemizer. |
Behdad Esfahbod | 55deff7 | 2011-09-28 16:20:09 -0400 | [diff] [blame] | 63 | * |
Behdad Esfahbod | 5d90a34 | 2011-07-21 15:25:01 -0400 | [diff] [blame] | 64 | * - When a font does not support a character but supports its decomposition, |
| 65 | * well, use the decomposition. |
| 66 | * |
| 67 | * - The Indic shaper requests decomposed output. This will handle splitting |
| 68 | * matra for the Indic shaper. |
| 69 | */ |
| 70 | |
Behdad Esfahbod | c311d85 | 2011-07-23 23:43:54 -0400 | [diff] [blame] | 71 | static void |
Behdad Esfahbod | 9f377ed | 2012-05-13 16:13:44 +0200 | [diff] [blame] | 72 | output_glyph (hb_buffer_t *buffer, hb_codepoint_t glyph) |
Behdad Esfahbod | c311d85 | 2011-07-23 23:43:54 -0400 | [diff] [blame] | 73 | { |
Behdad Esfahbod | c311d85 | 2011-07-23 23:43:54 -0400 | [diff] [blame] | 74 | buffer->output_glyph (glyph); |
Behdad Esfahbod | 99c2695 | 2012-05-13 15:45:18 +0200 | [diff] [blame] | 75 | _hb_glyph_info_set_unicode_props (&buffer->prev(), buffer->unicode); |
Behdad Esfahbod | c311d85 | 2011-07-23 23:43:54 -0400 | [diff] [blame] | 76 | } |
Behdad Esfahbod | 5c6f598 | 2011-07-21 11:31:08 -0400 | [diff] [blame] | 77 | |
Behdad Esfahbod | 4541252 | 2011-07-22 11:07:05 -0400 | [diff] [blame] | 78 | static bool |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 79 | decompose (hb_font_t *font, hb_buffer_t *buffer, |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 80 | bool shortest, |
Behdad Esfahbod | 4541252 | 2011-07-22 11:07:05 -0400 | [diff] [blame] | 81 | hb_codepoint_t ab) |
| 82 | { |
| 83 | hb_codepoint_t a, b, glyph; |
Behdad Esfahbod | 4541252 | 2011-07-22 11:07:05 -0400 | [diff] [blame] | 84 | |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 85 | if (!hb_unicode_decompose (buffer->unicode, ab, &a, &b) || |
| 86 | (b && !hb_font_get_glyph (font, b, 0, &glyph))) |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 87 | return FALSE; |
Behdad Esfahbod | 4541252 | 2011-07-22 11:07:05 -0400 | [diff] [blame] | 88 | |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 89 | bool has_a = hb_font_get_glyph (font, a, 0, &glyph); |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 90 | if (shortest && has_a) { |
| 91 | /* Output a and b */ |
Behdad Esfahbod | 9f377ed | 2012-05-13 16:13:44 +0200 | [diff] [blame] | 92 | output_glyph (buffer, a); |
Behdad Esfahbod | dcdc51c | 2011-07-22 17:14:46 -0400 | [diff] [blame] | 93 | if (b) |
Behdad Esfahbod | 9f377ed | 2012-05-13 16:13:44 +0200 | [diff] [blame] | 94 | output_glyph (buffer, b); |
Behdad Esfahbod | 4541252 | 2011-07-22 11:07:05 -0400 | [diff] [blame] | 95 | return TRUE; |
| 96 | } |
| 97 | |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 98 | if (decompose (font, buffer, shortest, a)) { |
Behdad Esfahbod | dcdc51c | 2011-07-22 17:14:46 -0400 | [diff] [blame] | 99 | if (b) |
Behdad Esfahbod | 9f377ed | 2012-05-13 16:13:44 +0200 | [diff] [blame] | 100 | output_glyph (buffer, b); |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 101 | return TRUE; |
| 102 | } |
Behdad Esfahbod | 5c6f598 | 2011-07-21 11:31:08 -0400 | [diff] [blame] | 103 | |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 104 | if (has_a) { |
Behdad Esfahbod | 9f377ed | 2012-05-13 16:13:44 +0200 | [diff] [blame] | 105 | output_glyph (buffer, a); |
Behdad Esfahbod | dcdc51c | 2011-07-22 17:14:46 -0400 | [diff] [blame] | 106 | if (b) |
Behdad Esfahbod | 9f377ed | 2012-05-13 16:13:44 +0200 | [diff] [blame] | 107 | output_glyph (buffer, b); |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 108 | return TRUE; |
| 109 | } |
| 110 | |
Behdad Esfahbod | 5c6f598 | 2011-07-21 11:31:08 -0400 | [diff] [blame] | 111 | return FALSE; |
Behdad Esfahbod | 655586f | 2011-07-21 00:51:18 -0400 | [diff] [blame] | 112 | } |
| 113 | |
Behdad Esfahbod | c311d85 | 2011-07-23 23:43:54 -0400 | [diff] [blame] | 114 | static void |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 115 | decompose_current_glyph (hb_font_t *font, hb_buffer_t *buffer, |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 116 | bool shortest) |
Behdad Esfahbod | d6b9c6d | 2011-07-21 12:16:45 -0400 | [diff] [blame] | 117 | { |
Behdad Esfahbod | 99c2695 | 2012-05-13 15:45:18 +0200 | [diff] [blame] | 118 | if (decompose (font, buffer, shortest, buffer->cur().codepoint)) |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 119 | buffer->skip_glyph (); |
Behdad Esfahbod | c311d85 | 2011-07-23 23:43:54 -0400 | [diff] [blame] | 120 | else |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 121 | buffer->next_glyph (); |
Behdad Esfahbod | d6b9c6d | 2011-07-21 12:16:45 -0400 | [diff] [blame] | 122 | } |
| 123 | |
Behdad Esfahbod | c311d85 | 2011-07-23 23:43:54 -0400 | [diff] [blame] | 124 | static void |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 125 | decompose_single_char_cluster (hb_font_t *font, hb_buffer_t *buffer, |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 126 | bool will_recompose) |
| 127 | { |
| 128 | hb_codepoint_t glyph; |
| 129 | |
| 130 | /* If recomposing and font supports this, we're good to go */ |
Behdad Esfahbod | 99c2695 | 2012-05-13 15:45:18 +0200 | [diff] [blame] | 131 | if (will_recompose && hb_font_get_glyph (font, buffer->cur().codepoint, 0, &glyph)) { |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 132 | buffer->next_glyph (); |
Behdad Esfahbod | c311d85 | 2011-07-23 23:43:54 -0400 | [diff] [blame] | 133 | return; |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 136 | decompose_current_glyph (font, buffer, will_recompose); |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Behdad Esfahbod | c311d85 | 2011-07-23 23:43:54 -0400 | [diff] [blame] | 139 | static void |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 140 | decompose_multi_char_cluster (hb_font_t *font, hb_buffer_t *buffer, |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 141 | unsigned int end) |
Behdad Esfahbod | 655586f | 2011-07-21 00:51:18 -0400 | [diff] [blame] | 142 | { |
Behdad Esfahbod | 5d90a34 | 2011-07-21 15:25:01 -0400 | [diff] [blame] | 143 | /* TODO Currently if there's a variation-selector we give-up, it's just too hard. */ |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 144 | for (unsigned int i = buffer->idx; i < end; i++) |
| 145 | if (unlikely (_hb_unicode_is_variation_selector (buffer->info[i].codepoint))) { |
| 146 | while (buffer->idx < end) |
| 147 | buffer->next_glyph (); |
Behdad Esfahbod | c311d85 | 2011-07-23 23:43:54 -0400 | [diff] [blame] | 148 | return; |
Behdad Esfahbod | af913c5 | 2011-10-17 11:39:28 -0700 | [diff] [blame] | 149 | } |
Behdad Esfahbod | d6b9c6d | 2011-07-21 12:16:45 -0400 | [diff] [blame] | 150 | |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 151 | while (buffer->idx < end) |
| 152 | decompose_current_glyph (font, buffer, FALSE); |
Behdad Esfahbod | 655586f | 2011-07-21 00:51:18 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 155 | static int |
| 156 | compare_combining_class (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb) |
| 157 | { |
Behdad Esfahbod | d1deaa2 | 2012-05-09 15:04:13 +0200 | [diff] [blame] | 158 | unsigned int a = _hb_glyph_info_get_modified_combining_class (pa); |
| 159 | unsigned int b = _hb_glyph_info_get_modified_combining_class (pb); |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 160 | |
| 161 | return a < b ? -1 : a == b ? 0 : +1; |
| 162 | } |
| 163 | |
Behdad Esfahbod | 4541252 | 2011-07-22 11:07:05 -0400 | [diff] [blame] | 164 | void |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 165 | _hb_ot_shape_normalize (hb_font_t *font, hb_buffer_t *buffer, |
| 166 | hb_ot_shape_normalization_mode_t mode) |
Behdad Esfahbod | 655586f | 2011-07-21 00:51:18 -0400 | [diff] [blame] | 167 | { |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 168 | bool recompose = mode != HB_OT_SHAPE_NORMALIZATION_MODE_DECOMPOSED; |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 169 | bool has_multichar_clusters = FALSE; |
Behdad Esfahbod | 34c22f8 | 2011-07-22 17:04:20 -0400 | [diff] [blame] | 170 | unsigned int count; |
Behdad Esfahbod | 5c6f598 | 2011-07-21 11:31:08 -0400 | [diff] [blame] | 171 | |
Behdad Esfahbod | c311d85 | 2011-07-23 23:43:54 -0400 | [diff] [blame] | 172 | /* We do a fairly straightforward yet custom normalization process in three |
Behdad Esfahbod | 5389ff4 | 2011-07-22 20:22:49 -0400 | [diff] [blame] | 173 | * separate rounds: decompose, reorder, recompose (if desired). Currently |
| 174 | * this makes two buffer swaps. We can make it faster by moving the last |
| 175 | * two rounds into the inner loop for the first round, but it's more readable |
| 176 | * this way. */ |
Behdad Esfahbod | 5d90a34 | 2011-07-21 15:25:01 -0400 | [diff] [blame] | 177 | |
Behdad Esfahbod | 34c22f8 | 2011-07-22 17:04:20 -0400 | [diff] [blame] | 178 | |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 179 | /* First round, decompose */ |
| 180 | |
Behdad Esfahbod | 5389ff4 | 2011-07-22 20:22:49 -0400 | [diff] [blame] | 181 | buffer->clear_output (); |
Behdad Esfahbod | 34c22f8 | 2011-07-22 17:04:20 -0400 | [diff] [blame] | 182 | count = buffer->len; |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 183 | for (buffer->idx = 0; buffer->idx < count;) |
Behdad Esfahbod | 5d90a34 | 2011-07-21 15:25:01 -0400 | [diff] [blame] | 184 | { |
Behdad Esfahbod | 655586f | 2011-07-21 00:51:18 -0400 | [diff] [blame] | 185 | unsigned int end; |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 186 | for (end = buffer->idx + 1; end < count; end++) |
Behdad Esfahbod | 99c2695 | 2012-05-13 15:45:18 +0200 | [diff] [blame] | 187 | if (buffer->cur().cluster != buffer->info[end].cluster) |
Behdad Esfahbod | 655586f | 2011-07-21 00:51:18 -0400 | [diff] [blame] | 188 | break; |
Behdad Esfahbod | 5d90a34 | 2011-07-21 15:25:01 -0400 | [diff] [blame] | 189 | |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 190 | if (buffer->idx + 1 == end) |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 191 | decompose_single_char_cluster (font, buffer, recompose); |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 192 | else { |
Behdad Esfahbod | 11138cc | 2012-04-05 17:25:19 -0400 | [diff] [blame] | 193 | decompose_multi_char_cluster (font, buffer, end); |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 194 | has_multichar_clusters = TRUE; |
| 195 | } |
Behdad Esfahbod | 655586f | 2011-07-21 00:51:18 -0400 | [diff] [blame] | 196 | } |
Behdad Esfahbod | 468e9cb | 2011-07-22 11:28:07 -0400 | [diff] [blame] | 197 | buffer->swap_buffers (); |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 198 | |
Behdad Esfahbod | 34c22f8 | 2011-07-22 17:04:20 -0400 | [diff] [blame] | 199 | |
Behdad Esfahbod | 9683184 | 2012-04-07 14:57:21 -0400 | [diff] [blame] | 200 | if (mode != HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_FULL && !has_multichar_clusters) |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 201 | return; /* Done! */ |
| 202 | |
Behdad Esfahbod | 34c22f8 | 2011-07-22 17:04:20 -0400 | [diff] [blame] | 203 | |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 204 | /* Second round, reorder (inplace) */ |
| 205 | |
Behdad Esfahbod | 34c22f8 | 2011-07-22 17:04:20 -0400 | [diff] [blame] | 206 | count = buffer->len; |
| 207 | for (unsigned int i = 0; i < count; i++) |
| 208 | { |
Behdad Esfahbod | d1deaa2 | 2012-05-09 15:04:13 +0200 | [diff] [blame] | 209 | if (_hb_glyph_info_get_modified_combining_class (&buffer->info[i]) == 0) |
Behdad Esfahbod | 34c22f8 | 2011-07-22 17:04:20 -0400 | [diff] [blame] | 210 | continue; |
| 211 | |
| 212 | unsigned int end; |
| 213 | for (end = i + 1; end < count; end++) |
Behdad Esfahbod | d1deaa2 | 2012-05-09 15:04:13 +0200 | [diff] [blame] | 214 | if (_hb_glyph_info_get_modified_combining_class (&buffer->info[end]) == 0) |
Behdad Esfahbod | 34c22f8 | 2011-07-22 17:04:20 -0400 | [diff] [blame] | 215 | break; |
| 216 | |
| 217 | /* We are going to do a bubble-sort. Only do this if the |
| 218 | * sequence is short. Doing it on long sequences can result |
| 219 | * in an O(n^2) DoS. */ |
| 220 | if (end - i > 10) { |
| 221 | i = end; |
| 222 | continue; |
| 223 | } |
| 224 | |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 225 | hb_bubble_sort (buffer->info + i, end - i, compare_combining_class); |
Behdad Esfahbod | 34c22f8 | 2011-07-22 17:04:20 -0400 | [diff] [blame] | 226 | |
| 227 | i = end; |
| 228 | } |
| 229 | |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 230 | |
Behdad Esfahbod | 5389ff4 | 2011-07-22 20:22:49 -0400 | [diff] [blame] | 231 | if (!recompose) |
| 232 | return; |
| 233 | |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 234 | /* Third round, recompose */ |
Behdad Esfahbod | 34c22f8 | 2011-07-22 17:04:20 -0400 | [diff] [blame] | 235 | |
Behdad Esfahbod | 5389ff4 | 2011-07-22 20:22:49 -0400 | [diff] [blame] | 236 | /* As noted in the comment earlier, we don't try to combine |
| 237 | * ccc=0 chars with their previous Starter. */ |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 238 | |
Behdad Esfahbod | 5389ff4 | 2011-07-22 20:22:49 -0400 | [diff] [blame] | 239 | buffer->clear_output (); |
| 240 | count = buffer->len; |
| 241 | unsigned int starter = 0; |
| 242 | buffer->next_glyph (); |
| 243 | while (buffer->idx < count) |
| 244 | { |
Behdad Esfahbod | 5389ff4 | 2011-07-22 20:22:49 -0400 | [diff] [blame] | 245 | hb_codepoint_t composed, glyph; |
Behdad Esfahbod | 9683184 | 2012-04-07 14:57:21 -0400 | [diff] [blame] | 246 | if (/* If mode is NOT COMPOSED_FULL (ie. it's COMPOSED_DIACRITICS), we don't try to |
| 247 | * compose a CCC=0 character with it's preceding starter. */ |
| 248 | (mode == HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_FULL || |
Behdad Esfahbod | 99c2695 | 2012-05-13 15:45:18 +0200 | [diff] [blame] | 249 | _hb_glyph_info_get_modified_combining_class (&buffer->cur()) != 0) && |
Behdad Esfahbod | 9683184 | 2012-04-07 14:57:21 -0400 | [diff] [blame] | 250 | /* If there's anything between the starter and this char, they should have CCC |
| 251 | * smaller than this character's. */ |
| 252 | (starter == buffer->out_len - 1 || |
Behdad Esfahbod | 99c2695 | 2012-05-13 15:45:18 +0200 | [diff] [blame] | 253 | _hb_glyph_info_get_modified_combining_class (&buffer->prev()) < _hb_glyph_info_get_modified_combining_class (&buffer->cur())) && |
Behdad Esfahbod | 9683184 | 2012-04-07 14:57:21 -0400 | [diff] [blame] | 254 | /* And compose. */ |
Behdad Esfahbod | e02d925 | 2012-04-07 14:49:13 -0400 | [diff] [blame] | 255 | hb_unicode_compose (buffer->unicode, |
| 256 | buffer->out_info[starter].codepoint, |
Behdad Esfahbod | 99c2695 | 2012-05-13 15:45:18 +0200 | [diff] [blame] | 257 | buffer->cur().codepoint, |
Behdad Esfahbod | e02d925 | 2012-04-07 14:49:13 -0400 | [diff] [blame] | 258 | &composed) && |
Behdad Esfahbod | 9683184 | 2012-04-07 14:57:21 -0400 | [diff] [blame] | 259 | /* And the font has glyph for the composite. */ |
Behdad Esfahbod | e02d925 | 2012-04-07 14:49:13 -0400 | [diff] [blame] | 260 | hb_font_get_glyph (font, composed, 0, &glyph)) |
Behdad Esfahbod | 5389ff4 | 2011-07-22 20:22:49 -0400 | [diff] [blame] | 261 | { |
Behdad Esfahbod | e02d925 | 2012-04-07 14:49:13 -0400 | [diff] [blame] | 262 | /* Composes. Modify starter and carry on. */ |
| 263 | buffer->out_info[starter].codepoint = composed; |
Behdad Esfahbod | 29a7e30 | 2012-04-24 16:01:30 -0400 | [diff] [blame] | 264 | /* XXX update cluster */ |
Behdad Esfahbod | d1deaa2 | 2012-05-09 15:04:13 +0200 | [diff] [blame] | 265 | _hb_glyph_info_set_unicode_props (&buffer->out_info[starter], buffer->unicode); |
Behdad Esfahbod | e02d925 | 2012-04-07 14:49:13 -0400 | [diff] [blame] | 266 | |
| 267 | buffer->skip_glyph (); |
Behdad Esfahbod | 5389ff4 | 2011-07-22 20:22:49 -0400 | [diff] [blame] | 268 | continue; |
| 269 | } |
| 270 | |
Behdad Esfahbod | e02d925 | 2012-04-07 14:49:13 -0400 | [diff] [blame] | 271 | /* Blocked, or doesn't compose. */ |
| 272 | buffer->next_glyph (); |
Behdad Esfahbod | 9683184 | 2012-04-07 14:57:21 -0400 | [diff] [blame] | 273 | |
Behdad Esfahbod | 99c2695 | 2012-05-13 15:45:18 +0200 | [diff] [blame] | 274 | if (_hb_glyph_info_get_modified_combining_class (&buffer->prev()) == 0) |
Behdad Esfahbod | 9683184 | 2012-04-07 14:57:21 -0400 | [diff] [blame] | 275 | starter = buffer->out_len - 1; |
Behdad Esfahbod | 4ff0d2d | 2011-07-22 16:15:32 -0400 | [diff] [blame] | 276 | } |
Behdad Esfahbod | 5389ff4 | 2011-07-22 20:22:49 -0400 | [diff] [blame] | 277 | buffer->swap_buffers (); |
Behdad Esfahbod | 34c22f8 | 2011-07-22 17:04:20 -0400 | [diff] [blame] | 278 | |
Behdad Esfahbod | 655586f | 2011-07-21 00:51:18 -0400 | [diff] [blame] | 279 | } |