/work/workdir/UnpackedTarball/harfbuzz/src/hb-ot-shape-fallback.cc
Line | Count | Source |
1 | | /* |
2 | | * Copyright © 2011,2012 Google, Inc. |
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 | | |
27 | | #include "hb.hh" |
28 | | |
29 | | #ifndef HB_NO_OT_SHAPE |
30 | | |
31 | | #include "hb-ot-shape-fallback.hh" |
32 | | #include "hb-kern.hh" |
33 | | |
34 | | static unsigned int |
35 | | recategorize_combining_class (hb_codepoint_t u, |
36 | | unsigned int klass) |
37 | 0 | { |
38 | 0 | if (klass >= 200) |
39 | 0 | return klass; |
40 | | |
41 | | /* Thai / Lao need some per-character work. */ |
42 | 0 | if ((u & ~0xFF) == 0x0E00u) |
43 | 0 | { |
44 | 0 | if (unlikely (klass == 0)) |
45 | 0 | { |
46 | 0 | switch (u) |
47 | 0 | { |
48 | 0 | case 0x0E31u: |
49 | 0 | case 0x0E34u: |
50 | 0 | case 0x0E35u: |
51 | 0 | case 0x0E36u: |
52 | 0 | case 0x0E37u: |
53 | 0 | case 0x0E47u: |
54 | 0 | case 0x0E4Cu: |
55 | 0 | case 0x0E4Du: |
56 | 0 | case 0x0E4Eu: |
57 | 0 | klass = HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT; |
58 | 0 | break; |
59 | | |
60 | 0 | case 0x0EB1u: |
61 | 0 | case 0x0EB4u: |
62 | 0 | case 0x0EB5u: |
63 | 0 | case 0x0EB6u: |
64 | 0 | case 0x0EB7u: |
65 | 0 | case 0x0EBBu: |
66 | 0 | case 0x0ECCu: |
67 | 0 | case 0x0ECDu: |
68 | 0 | klass = HB_UNICODE_COMBINING_CLASS_ABOVE; |
69 | 0 | break; |
70 | | |
71 | 0 | case 0x0EBCu: |
72 | 0 | klass = HB_UNICODE_COMBINING_CLASS_BELOW; |
73 | 0 | break; |
74 | 0 | } |
75 | 0 | } else { |
76 | | /* Thai virama is below-right */ |
77 | 0 | if (u == 0x0E3Au) |
78 | 0 | klass = HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT; |
79 | 0 | } |
80 | 0 | } |
81 | | |
82 | 0 | switch (klass) |
83 | 0 | { |
84 | | |
85 | | /* Hebrew */ |
86 | | |
87 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC10: /* sheva */ |
88 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC11: /* hataf segol */ |
89 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC12: /* hataf patah */ |
90 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC13: /* hataf qamats */ |
91 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC14: /* hiriq */ |
92 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC15: /* tsere */ |
93 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC16: /* segol */ |
94 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC17: /* patah */ |
95 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC18: /* qamats & qamats qatan */ |
96 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC20: /* qubuts */ |
97 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC22: /* meteg */ |
98 | 0 | return HB_UNICODE_COMBINING_CLASS_BELOW; |
99 | | |
100 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC23: /* rafe */ |
101 | 0 | return HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE; |
102 | | |
103 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC24: /* shin dot */ |
104 | 0 | return HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT; |
105 | | |
106 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC25: /* sin dot */ |
107 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC19: /* holam & holam haser for vav */ |
108 | 0 | return HB_UNICODE_COMBINING_CLASS_ABOVE_LEFT; |
109 | | |
110 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC26: /* point varika */ |
111 | 0 | return HB_UNICODE_COMBINING_CLASS_ABOVE; |
112 | | |
113 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC21: /* dagesh */ |
114 | 0 | break; |
115 | | |
116 | | |
117 | | /* Arabic and Syriac */ |
118 | | |
119 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC27: /* fathatan */ |
120 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC28: /* dammatan */ |
121 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC30: /* fatha */ |
122 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC31: /* damma */ |
123 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC33: /* shadda */ |
124 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC34: /* sukun */ |
125 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC35: /* superscript alef */ |
126 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC36: /* superscript alaph */ |
127 | 0 | return HB_UNICODE_COMBINING_CLASS_ABOVE; |
128 | | |
129 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC29: /* kasratan */ |
130 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC32: /* kasra */ |
131 | 0 | return HB_UNICODE_COMBINING_CLASS_BELOW; |
132 | | |
133 | | |
134 | | /* Thai */ |
135 | | |
136 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC103: /* sara u / sara uu */ |
137 | 0 | return HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT; |
138 | | |
139 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC107: /* mai */ |
140 | 0 | return HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT; |
141 | | |
142 | | |
143 | | /* Lao */ |
144 | | |
145 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC118: /* sign u / sign uu */ |
146 | 0 | return HB_UNICODE_COMBINING_CLASS_BELOW; |
147 | | |
148 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC122: /* mai */ |
149 | 0 | return HB_UNICODE_COMBINING_CLASS_ABOVE; |
150 | | |
151 | | |
152 | | /* Tibetan */ |
153 | | |
154 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC129: /* sign aa */ |
155 | 0 | return HB_UNICODE_COMBINING_CLASS_BELOW; |
156 | | |
157 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC130: /* sign i*/ |
158 | 0 | return HB_UNICODE_COMBINING_CLASS_ABOVE; |
159 | | |
160 | 0 | case HB_MODIFIED_COMBINING_CLASS_CCC132: /* sign u */ |
161 | 0 | return HB_UNICODE_COMBINING_CLASS_BELOW; |
162 | |
|
163 | 0 | } |
164 | | |
165 | 0 | return klass; |
166 | 0 | } |
167 | | |
168 | | void |
169 | | _hb_ot_shape_fallback_mark_position_recategorize_marks (const hb_ot_shape_plan_t *plan HB_UNUSED, |
170 | | hb_font_t *font HB_UNUSED, |
171 | | hb_buffer_t *buffer) |
172 | 0 | { |
173 | | #ifdef HB_NO_OT_SHAPE_FALLBACK |
174 | | return; |
175 | | #endif |
176 | |
|
177 | 0 | unsigned int count = buffer->len; |
178 | 0 | hb_glyph_info_t *info = buffer->info; |
179 | 0 | for (unsigned int i = 0; i < count; i++) |
180 | 0 | if (_hb_glyph_info_get_general_category (&info[i]) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK) { |
181 | 0 | unsigned int combining_class = _hb_glyph_info_get_modified_combining_class (&info[i]); |
182 | 0 | combining_class = recategorize_combining_class (info[i].codepoint, combining_class); |
183 | 0 | _hb_glyph_info_set_modified_combining_class (&info[i], combining_class); |
184 | 0 | } |
185 | 0 | } |
186 | | |
187 | | |
188 | | static void |
189 | | zero_mark_advances (hb_buffer_t *buffer, |
190 | | unsigned int start, |
191 | | unsigned int end, |
192 | | bool adjust_offsets_when_zeroing) |
193 | 0 | { |
194 | 0 | hb_glyph_info_t *info = buffer->info; |
195 | 0 | for (unsigned int i = start; i < end; i++) |
196 | 0 | if (_hb_glyph_info_get_general_category (&info[i]) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK) |
197 | 0 | { |
198 | 0 | if (adjust_offsets_when_zeroing) |
199 | 0 | { |
200 | 0 | buffer->pos[i].x_offset = hb_saturate_sub (buffer->pos[i].x_offset, |
201 | 0 | buffer->pos[i].x_advance); |
202 | 0 | buffer->pos[i].y_offset = hb_saturate_sub (buffer->pos[i].y_offset, |
203 | 0 | buffer->pos[i].y_advance); |
204 | 0 | } |
205 | 0 | buffer->pos[i].x_advance = 0; |
206 | 0 | buffer->pos[i].y_advance = 0; |
207 | 0 | } |
208 | 0 | } |
209 | | |
210 | | static inline void |
211 | | position_mark (const hb_ot_shape_plan_t *plan HB_UNUSED, |
212 | | hb_font_t *font, |
213 | | hb_buffer_t *buffer, |
214 | | hb_glyph_extents_t &base_extents, |
215 | | unsigned int i, |
216 | | unsigned int combining_class) |
217 | 0 | { |
218 | 0 | hb_glyph_extents_t mark_extents; |
219 | 0 | if (!font->get_glyph_extents (buffer->info[i].codepoint, &mark_extents)) |
220 | 0 | return; |
221 | | |
222 | 0 | hb_position_t y_gap = font->y_scale / 16; |
223 | |
|
224 | 0 | hb_glyph_position_t &pos = buffer->pos[i]; |
225 | 0 | pos.x_offset = pos.y_offset = 0; |
226 | | |
227 | | |
228 | | /* We don't position LEFT and RIGHT marks. */ |
229 | | |
230 | | /* X positioning */ |
231 | 0 | switch (combining_class) |
232 | 0 | { |
233 | 0 | case HB_UNICODE_COMBINING_CLASS_DOUBLE_BELOW: |
234 | 0 | case HB_UNICODE_COMBINING_CLASS_DOUBLE_ABOVE: |
235 | 0 | if (buffer->props.direction == HB_DIRECTION_LTR) { |
236 | 0 | pos.x_offset = hb_clamp_to<hb_position_t> ((int64_t) base_extents.x_bearing + base_extents.width - |
237 | 0 | mark_extents.width / 2 - mark_extents.x_bearing); |
238 | 0 | break; |
239 | 0 | } else if (buffer->props.direction == HB_DIRECTION_RTL) { |
240 | 0 | pos.x_offset = hb_clamp_to<hb_position_t> ((int64_t) base_extents.x_bearing - |
241 | 0 | mark_extents.width / 2 - mark_extents.x_bearing); |
242 | 0 | break; |
243 | 0 | } |
244 | 0 | HB_FALLTHROUGH; |
245 | |
|
246 | 0 | default: |
247 | 0 | case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW: |
248 | 0 | case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE: |
249 | 0 | case HB_UNICODE_COMBINING_CLASS_BELOW: |
250 | 0 | case HB_UNICODE_COMBINING_CLASS_ABOVE: |
251 | | /* Center align. */ |
252 | 0 | pos.x_offset = hb_clamp_to<hb_position_t> ((int64_t) base_extents.x_bearing + |
253 | 0 | ((int64_t) base_extents.width - mark_extents.width) / 2 - |
254 | 0 | mark_extents.x_bearing); |
255 | 0 | break; |
256 | | |
257 | 0 | case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW_LEFT: |
258 | 0 | case HB_UNICODE_COMBINING_CLASS_BELOW_LEFT: |
259 | 0 | case HB_UNICODE_COMBINING_CLASS_ABOVE_LEFT: |
260 | | /* Left align. */ |
261 | 0 | pos.x_offset = hb_saturate_sub (base_extents.x_bearing, mark_extents.x_bearing); |
262 | 0 | break; |
263 | | |
264 | 0 | case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE_RIGHT: |
265 | 0 | case HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT: |
266 | 0 | case HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT: |
267 | | /* Right align. */ |
268 | 0 | pos.x_offset = hb_clamp_to<hb_position_t> ((int64_t) base_extents.x_bearing + base_extents.width - |
269 | 0 | mark_extents.width - mark_extents.x_bearing); |
270 | 0 | break; |
271 | 0 | } |
272 | | |
273 | | /* Y positioning */ |
274 | 0 | switch (combining_class) |
275 | 0 | { |
276 | 0 | case HB_UNICODE_COMBINING_CLASS_DOUBLE_BELOW: |
277 | 0 | case HB_UNICODE_COMBINING_CLASS_BELOW_LEFT: |
278 | 0 | case HB_UNICODE_COMBINING_CLASS_BELOW: |
279 | 0 | case HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT: |
280 | | /* Add gap, fall-through. */ |
281 | 0 | base_extents.height = hb_saturate_sub (base_extents.height, y_gap); |
282 | 0 | HB_FALLTHROUGH; |
283 | |
|
284 | 0 | case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW_LEFT: |
285 | 0 | case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW: |
286 | 0 | pos.y_offset = hb_clamp_to<hb_position_t> ((int64_t) base_extents.y_bearing + |
287 | 0 | base_extents.height - mark_extents.y_bearing); |
288 | | /* Never shift up "below" marks. */ |
289 | 0 | if ((y_gap > 0) == (pos.y_offset > 0)) |
290 | 0 | { |
291 | 0 | base_extents.height = hb_saturate_sub (base_extents.height, pos.y_offset); |
292 | 0 | pos.y_offset = 0; |
293 | 0 | } |
294 | 0 | base_extents.height = hb_saturate_add (base_extents.height, mark_extents.height); |
295 | 0 | break; |
296 | | |
297 | 0 | case HB_UNICODE_COMBINING_CLASS_DOUBLE_ABOVE: |
298 | 0 | case HB_UNICODE_COMBINING_CLASS_ABOVE_LEFT: |
299 | 0 | case HB_UNICODE_COMBINING_CLASS_ABOVE: |
300 | 0 | case HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT: |
301 | | /* Add gap, fall-through. */ |
302 | 0 | base_extents.y_bearing = hb_saturate_add (base_extents.y_bearing, y_gap); |
303 | 0 | base_extents.height = hb_saturate_sub (base_extents.height, y_gap); |
304 | 0 | HB_FALLTHROUGH; |
305 | |
|
306 | 0 | case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE: |
307 | 0 | case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE_RIGHT: |
308 | 0 | pos.y_offset = hb_clamp_to<hb_position_t> ((int64_t) base_extents.y_bearing - |
309 | 0 | mark_extents.y_bearing - mark_extents.height); |
310 | | /* Don't shift down "above" marks too much. */ |
311 | 0 | if ((y_gap > 0) != (pos.y_offset > 0)) |
312 | 0 | { |
313 | 0 | hb_position_t correction = -(pos.y_offset / 2); |
314 | 0 | base_extents.y_bearing = hb_saturate_add (base_extents.y_bearing, correction); |
315 | 0 | base_extents.height = hb_saturate_sub (base_extents.height, correction); |
316 | 0 | pos.y_offset = hb_saturate_add (pos.y_offset, correction); |
317 | 0 | } |
318 | 0 | base_extents.y_bearing = hb_saturate_sub (base_extents.y_bearing, mark_extents.height); |
319 | 0 | base_extents.height = hb_saturate_add (base_extents.height, mark_extents.height); |
320 | 0 | break; |
321 | 0 | } |
322 | 0 | } |
323 | | |
324 | | static inline void |
325 | | position_around_base (const hb_ot_shape_plan_t *plan, |
326 | | hb_font_t *font, |
327 | | hb_buffer_t *buffer, |
328 | | unsigned int base, |
329 | | unsigned int end, |
330 | | bool adjust_offsets_when_zeroing) |
331 | 0 | { |
332 | 0 | hb_direction_t horiz_dir = HB_DIRECTION_INVALID; |
333 | |
|
334 | 0 | buffer->unsafe_to_break (base, end); |
335 | |
|
336 | 0 | hb_glyph_extents_t base_extents; |
337 | 0 | if (!font->get_glyph_extents (buffer->info[base].codepoint, |
338 | 0 | &base_extents)) |
339 | 0 | { |
340 | | /* If extents don't work, zero marks and go home. */ |
341 | 0 | zero_mark_advances (buffer, base + 1, end, adjust_offsets_when_zeroing); |
342 | 0 | return; |
343 | 0 | } |
344 | 0 | base_extents.y_bearing = hb_saturate_add (base_extents.y_bearing, |
345 | 0 | buffer->pos[base].y_offset); |
346 | | /* Use horizontal advance for horizontal positioning. |
347 | | * Generally a better idea. Also works for zero-ink glyphs. See: |
348 | | * https://github.com/harfbuzz/harfbuzz/issues/1532 */ |
349 | 0 | base_extents.x_bearing = 0; |
350 | 0 | base_extents.width = font->get_glyph_h_advance (buffer->info[base].codepoint); |
351 | |
|
352 | 0 | unsigned int lig_id = _hb_glyph_info_get_lig_id (&buffer->info[base]); |
353 | | /* Use integer for num_lig_components such that it doesn't convert to unsigned |
354 | | * when we divide or multiply by it. */ |
355 | 0 | int num_lig_components = _hb_glyph_info_get_lig_num_comps (&buffer->info[base]); |
356 | |
|
357 | 0 | hb_position_t x_offset = 0, y_offset = 0; |
358 | 0 | if (HB_DIRECTION_IS_FORWARD (buffer->props.direction)) { |
359 | 0 | x_offset = hb_saturate_neg (buffer->pos[base].x_advance); |
360 | 0 | y_offset = hb_saturate_neg (buffer->pos[base].y_advance); |
361 | 0 | } |
362 | |
|
363 | 0 | hb_glyph_extents_t component_extents = base_extents; |
364 | 0 | int last_lig_component = -1; |
365 | 0 | unsigned int last_combining_class = 255; |
366 | 0 | hb_glyph_extents_t cluster_extents = base_extents; /* Initialization is just to shut gcc up. */ |
367 | 0 | hb_glyph_info_t *info = buffer->info; |
368 | 0 | for (unsigned int i = base + 1; i < end; i++) |
369 | 0 | if (_hb_glyph_info_get_modified_combining_class (&info[i])) |
370 | 0 | { |
371 | 0 | if (num_lig_components > 1) { |
372 | 0 | unsigned int this_lig_id = _hb_glyph_info_get_lig_id (&info[i]); |
373 | 0 | int this_lig_component = _hb_glyph_info_get_lig_comp (&info[i]) - 1; |
374 | | /* Conditions for attaching to the last component. */ |
375 | 0 | if (!lig_id || lig_id != this_lig_id || this_lig_component >= num_lig_components) |
376 | 0 | this_lig_component = num_lig_components - 1; |
377 | 0 | if (last_lig_component != this_lig_component) |
378 | 0 | { |
379 | 0 | last_lig_component = this_lig_component; |
380 | 0 | last_combining_class = 255; |
381 | 0 | component_extents = base_extents; |
382 | 0 | if (unlikely (horiz_dir == HB_DIRECTION_INVALID)) { |
383 | 0 | if (HB_DIRECTION_IS_HORIZONTAL (plan->props.direction)) |
384 | 0 | horiz_dir = plan->props.direction; |
385 | 0 | else |
386 | 0 | horiz_dir = hb_script_get_horizontal_direction (plan->props.script); |
387 | 0 | } |
388 | 0 | if (horiz_dir == HB_DIRECTION_LTR) |
389 | 0 | component_extents.x_bearing = hb_clamp_to<hb_position_t> ( |
390 | 0 | (int64_t) component_extents.x_bearing + |
391 | 0 | (int64_t) this_lig_component * component_extents.width / num_lig_components); |
392 | 0 | else |
393 | 0 | component_extents.x_bearing = hb_clamp_to<hb_position_t> ( |
394 | 0 | (int64_t) component_extents.x_bearing + |
395 | 0 | (int64_t) (num_lig_components - 1 - this_lig_component) * component_extents.width / num_lig_components); |
396 | 0 | component_extents.width /= num_lig_components; |
397 | 0 | } |
398 | 0 | } |
399 | |
|
400 | 0 | unsigned int this_combining_class = _hb_glyph_info_get_modified_combining_class (&info[i]); |
401 | 0 | if (last_combining_class != this_combining_class) |
402 | 0 | { |
403 | 0 | last_combining_class = this_combining_class; |
404 | 0 | cluster_extents = component_extents; |
405 | 0 | } |
406 | |
|
407 | 0 | position_mark (plan, font, buffer, cluster_extents, i, this_combining_class); |
408 | |
|
409 | 0 | buffer->pos[i].x_advance = 0; |
410 | 0 | buffer->pos[i].y_advance = 0; |
411 | 0 | buffer->pos[i].x_offset = hb_saturate_add (buffer->pos[i].x_offset, x_offset); |
412 | 0 | buffer->pos[i].y_offset = hb_saturate_add (buffer->pos[i].y_offset, y_offset); |
413 | |
|
414 | 0 | } else { |
415 | 0 | if (HB_DIRECTION_IS_FORWARD (buffer->props.direction)) { |
416 | 0 | x_offset = hb_saturate_sub (x_offset, buffer->pos[i].x_advance); |
417 | 0 | y_offset = hb_saturate_sub (y_offset, buffer->pos[i].y_advance); |
418 | 0 | } else { |
419 | 0 | x_offset = hb_saturate_add (x_offset, buffer->pos[i].x_advance); |
420 | 0 | y_offset = hb_saturate_add (y_offset, buffer->pos[i].y_advance); |
421 | 0 | } |
422 | 0 | } |
423 | 0 | } |
424 | | |
425 | | static inline void |
426 | | position_cluster_impl (const hb_ot_shape_plan_t *plan, |
427 | | hb_font_t *font, |
428 | | hb_buffer_t *buffer, |
429 | | unsigned int start, |
430 | | unsigned int end, |
431 | | bool adjust_offsets_when_zeroing) |
432 | 0 | { |
433 | | /* Find the base glyph */ |
434 | 0 | hb_glyph_info_t *info = buffer->info; |
435 | 0 | for (unsigned int i = start; i < end; i++) |
436 | 0 | if (!_hb_glyph_info_is_unicode_mark (&info[i])) |
437 | 0 | { |
438 | | /* Find mark glyphs */ |
439 | 0 | unsigned int j; |
440 | 0 | for (j = i + 1; j < end; j++) |
441 | 0 | { |
442 | 0 | if (_hb_glyph_info_is_hidden (&info[j]) || |
443 | 0 | _hb_glyph_info_is_default_ignorable (&info[j])) |
444 | 0 | continue; |
445 | 0 | if (!_hb_glyph_info_is_unicode_mark (&info[j])) |
446 | 0 | break; |
447 | 0 | } |
448 | |
|
449 | 0 | position_around_base (plan, font, buffer, i, j, adjust_offsets_when_zeroing); |
450 | |
|
451 | 0 | i = j - 1; |
452 | 0 | } |
453 | 0 | } |
454 | | |
455 | | static HB_ALWAYS_INLINE void |
456 | | position_cluster (const hb_ot_shape_plan_t *plan, |
457 | | hb_font_t *font, |
458 | | hb_buffer_t *buffer, |
459 | | unsigned int start, |
460 | | unsigned int end, |
461 | | bool adjust_offsets_when_zeroing) |
462 | 0 | { |
463 | 0 | if (end - start < 2) |
464 | 0 | return; |
465 | | |
466 | 0 | position_cluster_impl (plan, font, buffer, start, end, adjust_offsets_when_zeroing); |
467 | 0 | } |
468 | | |
469 | | void |
470 | | _hb_ot_shape_fallback_mark_position (const hb_ot_shape_plan_t *plan, |
471 | | hb_font_t *font, |
472 | | hb_buffer_t *buffer, |
473 | | bool adjust_offsets_when_zeroing) |
474 | 0 | { |
475 | | #ifdef HB_NO_OT_SHAPE_FALLBACK |
476 | | return; |
477 | | #endif |
478 | |
|
479 | 0 | if (!buffer->message (font, "start fallback mark")) |
480 | 0 | return; |
481 | | |
482 | 0 | _hb_buffer_assert_gsubgpos_vars (buffer); |
483 | |
|
484 | 0 | unsigned int start = 0; |
485 | 0 | unsigned int count = buffer->len; |
486 | 0 | hb_glyph_info_t *info = buffer->info; |
487 | 0 | for (unsigned int i = 1; i < count; i++) |
488 | 0 | if (likely (!_hb_glyph_info_is_unicode_mark (&info[i]) && |
489 | 0 | !_hb_glyph_info_is_hidden (&info[i]) && |
490 | 0 | !_hb_glyph_info_is_default_ignorable (&info[i]))) { |
491 | 0 | position_cluster (plan, font, buffer, start, i, adjust_offsets_when_zeroing); |
492 | 0 | start = i; |
493 | 0 | } |
494 | 0 | position_cluster (plan, font, buffer, start, count, adjust_offsets_when_zeroing); |
495 | |
|
496 | 0 | (void) buffer->message (font, "end fallback mark"); |
497 | 0 | } |
498 | | |
499 | | |
500 | | #ifndef HB_DISABLE_DEPRECATED |
501 | | struct hb_ot_shape_fallback_kern_driver_t |
502 | | { |
503 | | hb_ot_shape_fallback_kern_driver_t (hb_font_t *font_, |
504 | | hb_buffer_t *buffer) : |
505 | 0 | font (font_), direction (buffer->props.direction) {} |
506 | | |
507 | | hb_position_t get_kerning (hb_codepoint_t first, hb_codepoint_t second) const |
508 | 0 | { |
509 | 0 | hb_position_t kern = 0; |
510 | 0 | font->get_glyph_kerning_for_direction (first, second, |
511 | 0 | direction, |
512 | 0 | &kern, &kern); |
513 | 0 | return kern; |
514 | 0 | } |
515 | | |
516 | | hb_font_t *font; |
517 | | hb_direction_t direction; |
518 | | }; |
519 | | #endif |
520 | | |
521 | | /* Performs font-assisted kerning. */ |
522 | | void |
523 | | _hb_ot_shape_fallback_kern (const hb_ot_shape_plan_t *plan, |
524 | | hb_font_t *font, |
525 | | hb_buffer_t *buffer) |
526 | 0 | { |
527 | | #ifdef HB_NO_OT_SHAPE_FALLBACK |
528 | | return; |
529 | | #endif |
530 | |
|
531 | 0 | #ifndef HB_DISABLE_DEPRECATED |
532 | 0 | if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction) ? |
533 | 0 | !font->has_glyph_h_kerning_func () : |
534 | 0 | !font->has_glyph_v_kerning_func ()) |
535 | 0 | return; |
536 | | |
537 | 0 | if (!buffer->message (font, "start fallback kern")) |
538 | 0 | return; |
539 | | |
540 | 0 | bool reverse = HB_DIRECTION_IS_BACKWARD (buffer->props.direction); |
541 | |
|
542 | 0 | if (reverse) |
543 | 0 | buffer->reverse (); |
544 | |
|
545 | 0 | hb_ot_shape_fallback_kern_driver_t driver (font, buffer); |
546 | 0 | OT::hb_kern_machine_t<hb_ot_shape_fallback_kern_driver_t> machine (driver); |
547 | 0 | machine.kern (font, buffer, plan->kern_mask, false); |
548 | |
|
549 | 0 | if (reverse) |
550 | 0 | buffer->reverse (); |
551 | |
|
552 | 0 | (void) buffer->message (font, "end fallback kern"); |
553 | 0 | #endif |
554 | 0 | } |
555 | | |
556 | | |
557 | | /* Adjusts width of various spaces. */ |
558 | | void |
559 | | _hb_ot_shape_fallback_spaces (const hb_ot_shape_plan_t *plan HB_UNUSED, |
560 | | hb_font_t *font, |
561 | | hb_buffer_t *buffer) |
562 | 6.53k | { |
563 | 6.53k | hb_glyph_info_t *info = buffer->info; |
564 | 6.53k | hb_glyph_position_t *pos = buffer->pos; |
565 | 6.53k | bool horizontal = HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction); |
566 | 6.53k | unsigned int count = buffer->len; |
567 | 77.1k | for (unsigned int i = 0; i < count; i++) |
568 | 70.6k | if (_hb_glyph_info_is_unicode_space (&info[i]) && !_hb_glyph_info_ligated (&info[i])) |
569 | 9.26k | { |
570 | | /* If font had no ASCII space and we used the invisible glyph, give it a 1/4 EM default advance. */ |
571 | 9.26k | if (buffer->invisible && info[i].codepoint == buffer->invisible) |
572 | 0 | { |
573 | 0 | if (horizontal) |
574 | 0 | pos[i].x_advance = +font->x_scale / 4; |
575 | 0 | else |
576 | 0 | pos[i].y_advance = -(int64_t) font->y_scale / 4; |
577 | 0 | } |
578 | | |
579 | 9.26k | hb_unicode_funcs_t::space_t space_type = _hb_glyph_info_get_unicode_space_fallback_type (&info[i]); |
580 | 9.26k | hb_codepoint_t glyph; |
581 | 9.26k | typedef hb_unicode_funcs_t t; |
582 | 9.26k | switch (space_type) |
583 | 9.26k | { |
584 | 2.42k | case t::NOT_SPACE: /* Shouldn't happen. */ |
585 | 2.42k | case t::SPACE: |
586 | 2.42k | break; |
587 | | |
588 | 6.44k | case t::SPACE_EM: |
589 | 6.44k | case t::SPACE_EM_2: |
590 | 6.44k | case t::SPACE_EM_3: |
591 | 6.44k | case t::SPACE_EM_4: |
592 | 6.44k | case t::SPACE_EM_5: |
593 | 6.44k | case t::SPACE_EM_6: |
594 | 6.44k | case t::SPACE_EM_16: |
595 | 6.44k | if (horizontal) |
596 | 6.25k | pos[i].x_advance = ((int64_t) font->x_scale + ((int) space_type)/2) / (int) space_type; |
597 | 193 | else |
598 | 193 | pos[i].y_advance = -((int64_t) font->y_scale + ((int) space_type)/2) / (int) space_type; |
599 | 6.44k | break; |
600 | | |
601 | 394 | case t::SPACE_4_EM_18: |
602 | 394 | if (horizontal) |
603 | 394 | pos[i].x_advance = (int64_t) font->x_scale * 4 / 18; |
604 | 0 | else |
605 | 0 | pos[i].y_advance = -(int64_t) font->y_scale * 4 / 18; |
606 | 394 | break; |
607 | | |
608 | 0 | case t::SPACE_FIGURE: |
609 | 0 | for (char u = '0'; u <= '9'; u++) |
610 | 0 | if (font->get_nominal_glyph (u, &glyph)) |
611 | 0 | { |
612 | 0 | if (horizontal) |
613 | 0 | pos[i].x_advance = font->get_glyph_h_advance (glyph); |
614 | 0 | else |
615 | 0 | pos[i].y_advance = font->get_glyph_v_advance (glyph); |
616 | 0 | break; |
617 | 0 | } |
618 | 0 | break; |
619 | | |
620 | 0 | case t::SPACE_PUNCTUATION: |
621 | 0 | if (font->get_nominal_glyph ('.', &glyph) || |
622 | 0 | font->get_nominal_glyph (',', &glyph)) |
623 | 0 | { |
624 | 0 | if (horizontal) |
625 | 0 | pos[i].x_advance = font->get_glyph_h_advance (glyph); |
626 | 0 | else |
627 | 0 | pos[i].y_advance = font->get_glyph_v_advance (glyph); |
628 | 0 | } |
629 | 0 | break; |
630 | | |
631 | 0 | case t::SPACE_NARROW: |
632 | | /* Half-space? |
633 | | * Unicode doc https://unicode.org/charts/PDF/U2000.pdf says ~1/4 or 1/5 of EM. |
634 | | * However, in my testing, many fonts have their regular space being about that |
635 | | * size. To me, a percentage of the space width makes more sense. Half is as |
636 | | * good as any. */ |
637 | 0 | if (horizontal) |
638 | 0 | pos[i].x_advance /= 2; |
639 | 0 | else |
640 | 0 | pos[i].y_advance /= 2; |
641 | 0 | break; |
642 | 9.26k | } |
643 | 9.26k | } |
644 | 6.53k | } |
645 | | |
646 | | |
647 | | #endif |