1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
/*
* Copyright © 2011,2012 Google, Inc.
*
* This is part of HarfBuzz, a text shaping library.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
* Google Author(s): Behdad Esfahbod
*/
#include "hb-ot-shape-fallback-private.hh"
static unsigned int
recategorize_combining_class (hb_codepoint_t u,
unsigned int klass)
{
if (klass >= 200)
return klass;
/* Thai / Lao need some per-character work. */
if ((u & ~0xFF) == 0x0E00)
{
if (unlikely (klass == 0))
{
switch (u)
{
case 0x0E31:
case 0x0E34:
case 0x0E35:
case 0x0E36:
case 0x0E37:
case 0x0E47:
case 0x0E4C:
case 0x0E4D:
case 0x0E4E:
klass = HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT;
break;
case 0x0EB1:
case 0x0EB4:
case 0x0EB5:
case 0x0EB6:
case 0x0EB7:
case 0x0EBB:
case 0x0ECC:
case 0x0ECD:
klass = HB_UNICODE_COMBINING_CLASS_ABOVE;
break;
case 0x0EBC:
klass = HB_UNICODE_COMBINING_CLASS_BELOW;
break;
}
} else {
/* Thai virama is below-right */
if (u == 0x0E3A)
klass = HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT;
}
}
switch (klass)
{
/* Hebrew */
case HB_MODIFIED_COMBINING_CLASS_CCC10: /* sheva */
case HB_MODIFIED_COMBINING_CLASS_CCC11: /* hataf segol */
case HB_MODIFIED_COMBINING_CLASS_CCC12: /* hataf patah */
case HB_MODIFIED_COMBINING_CLASS_CCC13: /* hataf qamats */
case HB_MODIFIED_COMBINING_CLASS_CCC14: /* hiriq */
case HB_MODIFIED_COMBINING_CLASS_CCC15: /* tsere */
case HB_MODIFIED_COMBINING_CLASS_CCC16: /* segol */
case HB_MODIFIED_COMBINING_CLASS_CCC17: /* patah */
case HB_MODIFIED_COMBINING_CLASS_CCC18: /* qamats */
case HB_MODIFIED_COMBINING_CLASS_CCC20: /* qubuts */
case HB_MODIFIED_COMBINING_CLASS_CCC22: /* meteg */
return HB_UNICODE_COMBINING_CLASS_BELOW;
case HB_MODIFIED_COMBINING_CLASS_CCC23: /* rafe */
return HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE;
case HB_MODIFIED_COMBINING_CLASS_CCC24: /* shin dot */
return HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT;
case HB_MODIFIED_COMBINING_CLASS_CCC25: /* sin dot */
case HB_MODIFIED_COMBINING_CLASS_CCC19: /* holam */
return HB_UNICODE_COMBINING_CLASS_ABOVE_LEFT;
case HB_MODIFIED_COMBINING_CLASS_CCC26: /* point varika */
return HB_UNICODE_COMBINING_CLASS_ABOVE;
case HB_MODIFIED_COMBINING_CLASS_CCC21: /* dagesh */
break;
/* Arabic and Syriac */
case HB_MODIFIED_COMBINING_CLASS_CCC27: /* fathatan */
case HB_MODIFIED_COMBINING_CLASS_CCC28: /* dammatan */
case HB_MODIFIED_COMBINING_CLASS_CCC30: /* fatha */
case HB_MODIFIED_COMBINING_CLASS_CCC31: /* damma */
case HB_MODIFIED_COMBINING_CLASS_CCC33: /* shadda */
case HB_MODIFIED_COMBINING_CLASS_CCC34: /* sukun */
case HB_MODIFIED_COMBINING_CLASS_CCC35: /* superscript alef */
case HB_MODIFIED_COMBINING_CLASS_CCC36: /* superscript alaph */
return HB_UNICODE_COMBINING_CLASS_ABOVE;
case HB_MODIFIED_COMBINING_CLASS_CCC29: /* kasratan */
case HB_MODIFIED_COMBINING_CLASS_CCC32: /* kasra */
return HB_UNICODE_COMBINING_CLASS_BELOW;
/* Thai */
case HB_MODIFIED_COMBINING_CLASS_CCC103: /* sara u / sara uu */
return HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT;
case HB_MODIFIED_COMBINING_CLASS_CCC107: /* mai */
return HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT;
/* Lao */
case HB_MODIFIED_COMBINING_CLASS_CCC118: /* sign u / sign uu */
return HB_UNICODE_COMBINING_CLASS_BELOW;
case HB_MODIFIED_COMBINING_CLASS_CCC122: /* mai */
return HB_UNICODE_COMBINING_CLASS_ABOVE;
/* Tibetan */
case HB_MODIFIED_COMBINING_CLASS_CCC129: /* sign aa */
return HB_UNICODE_COMBINING_CLASS_BELOW;
case HB_MODIFIED_COMBINING_CLASS_CCC130: /* sign i*/
return HB_UNICODE_COMBINING_CLASS_ABOVE;
case HB_MODIFIED_COMBINING_CLASS_CCC132: /* sign u */
return HB_UNICODE_COMBINING_CLASS_BELOW;
}
return klass;
}
void
_hb_ot_shape_fallback_position_recategorize_marks (const hb_ot_shape_plan_t *plan,
hb_font_t *font,
hb_buffer_t *buffer)
{
unsigned int count = buffer->len;
for (unsigned int i = 0; i < count; i++)
if (_hb_glyph_info_get_general_category (&buffer->info[i]) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK) {
unsigned int combining_class = _hb_glyph_info_get_modified_combining_class (&buffer->info[i]);
combining_class = recategorize_combining_class (buffer->info[i].codepoint, combining_class);
_hb_glyph_info_set_modified_combining_class (&buffer->info[i], combining_class);
}
}
static void
zero_mark_advances (hb_buffer_t *buffer,
unsigned int start,
unsigned int end)
{
for (unsigned int i = start; i < end; i++)
if (_hb_glyph_info_get_general_category (&buffer->info[i]) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
{
buffer->pos[i].x_advance = 0;
buffer->pos[i].y_advance = 0;
}
}
static inline void
position_mark (const hb_ot_shape_plan_t *plan,
hb_font_t *font,
hb_buffer_t *buffer,
hb_glyph_extents_t &base_extents,
unsigned int i,
unsigned int combining_class)
{
hb_glyph_extents_t mark_extents;
if (!font->get_glyph_extents (buffer->info[i].codepoint,
&mark_extents))
return;
hb_position_t y_gap = font->y_scale / 16;
hb_glyph_position_t &pos = buffer->pos[i];
pos.x_offset = pos.y_offset = 0;
/* We dont position LEFT and RIGHT marks. */
/* X positioning */
switch (combining_class)
{
case HB_UNICODE_COMBINING_CLASS_DOUBLE_BELOW:
case HB_UNICODE_COMBINING_CLASS_DOUBLE_ABOVE:
if (buffer->props.direction == HB_DIRECTION_LTR) {
pos.x_offset += base_extents.x_bearing - mark_extents.width / 2 - mark_extents.x_bearing;
break;
} else if (buffer->props.direction == HB_DIRECTION_RTL) {
pos.x_offset += base_extents.x_bearing + base_extents.width - mark_extents.width / 2 - mark_extents.x_bearing;
break;
}
/* Fall through */
default:
case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW:
case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE:
case HB_UNICODE_COMBINING_CLASS_BELOW:
case HB_UNICODE_COMBINING_CLASS_ABOVE:
/* Center align. */
pos.x_offset += base_extents.x_bearing + (base_extents.width - mark_extents.width) / 2 - mark_extents.x_bearing;
break;
case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW_LEFT:
case HB_UNICODE_COMBINING_CLASS_BELOW_LEFT:
case HB_UNICODE_COMBINING_CLASS_ABOVE_LEFT:
/* Left align. */
pos.x_offset += base_extents.x_bearing - mark_extents.x_bearing;
break;
case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE_RIGHT:
case HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT:
case HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT:
/* Right align. */
pos.x_offset += base_extents.x_bearing + base_extents.width - mark_extents.width - mark_extents.x_bearing;
break;
}
/* Y positioning */
switch (combining_class)
{
case HB_UNICODE_COMBINING_CLASS_DOUBLE_BELOW:
case HB_UNICODE_COMBINING_CLASS_BELOW_LEFT:
case HB_UNICODE_COMBINING_CLASS_BELOW:
case HB_UNICODE_COMBINING_CLASS_BELOW_RIGHT:
/* Add gap, fall-through. */
base_extents.height -= y_gap;
case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW_LEFT:
case HB_UNICODE_COMBINING_CLASS_ATTACHED_BELOW:
pos.y_offset += base_extents.y_bearing + base_extents.height - mark_extents.y_bearing;
base_extents.height += mark_extents.height;
break;
case HB_UNICODE_COMBINING_CLASS_DOUBLE_ABOVE:
case HB_UNICODE_COMBINING_CLASS_ABOVE_LEFT:
case HB_UNICODE_COMBINING_CLASS_ABOVE:
case HB_UNICODE_COMBINING_CLASS_ABOVE_RIGHT:
/* Add gap, fall-through. */
base_extents.y_bearing += y_gap;
base_extents.height -= y_gap;
case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE:
case HB_UNICODE_COMBINING_CLASS_ATTACHED_ABOVE_RIGHT:
pos.y_offset += base_extents.y_bearing - (mark_extents.y_bearing + mark_extents.height);
base_extents.y_bearing -= mark_extents.height;
base_extents.height += mark_extents.height;
break;
}
}
static inline void
position_around_base (const hb_ot_shape_plan_t *plan,
hb_font_t *font,
hb_buffer_t *buffer,
unsigned int base,
unsigned int end)
{
hb_direction_t horiz_dir = HB_DIRECTION_INVALID;
hb_glyph_extents_t base_extents;
if (!font->get_glyph_extents (buffer->info[base].codepoint,
&base_extents))
{
/* If extents don't work, zero marks and go home. */
zero_mark_advances (buffer, base + 1, end);
return;
}
base_extents.x_bearing += buffer->pos[base].x_offset;
base_extents.y_bearing += buffer->pos[base].y_offset;
unsigned int lig_id = get_lig_id (buffer->info[base]);
unsigned int num_lig_components = get_lig_num_comps (buffer->info[base]);
hb_position_t x_offset = 0, y_offset = 0;
if (HB_DIRECTION_IS_FORWARD (buffer->props.direction)) {
x_offset -= buffer->pos[base].x_advance;
y_offset -= buffer->pos[base].y_advance;
}
hb_glyph_extents_t component_extents = base_extents;
unsigned int last_lig_component = (unsigned int) -1;
unsigned int last_combining_class = 255;
hb_glyph_extents_t cluster_extents = base_extents; /* Initialization is just to shut gcc up. */
for (unsigned int i = base + 1; i < end; i++)
if (_hb_glyph_info_get_modified_combining_class (&buffer->info[i]))
{
if (num_lig_components > 1) {
unsigned int this_lig_id = get_lig_id (buffer->info[i]);
unsigned int this_lig_component = get_lig_comp (buffer->info[i]) - 1;
/* Conditions for attaching to the last component. */
if (!lig_id || lig_id != this_lig_id || this_lig_component >= num_lig_components)
this_lig_component = num_lig_components - 1;
if (last_lig_component != this_lig_component)
{
last_lig_component = this_lig_component;
last_combining_class = 255;
component_extents = base_extents;
if (unlikely (horiz_dir == HB_DIRECTION_INVALID)) {
if (HB_DIRECTION_IS_HORIZONTAL (plan->props.direction))
horiz_dir = plan->props.direction;
else
horiz_dir = hb_script_get_horizontal_direction (plan->props.script);
}
if (horiz_dir == HB_DIRECTION_LTR)
component_extents.x_bearing += (this_lig_component * component_extents.width) / num_lig_components;
else
component_extents.x_bearing += ((num_lig_components - 1 - this_lig_component) * component_extents.width) / num_lig_components;
component_extents.width /= num_lig_components;
}
}
unsigned int this_combining_class = _hb_glyph_info_get_modified_combining_class (&buffer->info[i]);
if (last_combining_class != this_combining_class)
{
last_combining_class = this_combining_class;
cluster_extents = component_extents;
}
position_mark (plan, font, buffer, cluster_extents, i, this_combining_class);
buffer->pos[i].x_advance = 0;
buffer->pos[i].y_advance = 0;
buffer->pos[i].x_offset += x_offset;
buffer->pos[i].y_offset += y_offset;
} else {
if (HB_DIRECTION_IS_FORWARD (buffer->props.direction)) {
x_offset -= buffer->pos[i].x_advance;
y_offset -= buffer->pos[i].y_advance;
} else {
x_offset += buffer->pos[i].x_advance;
y_offset += buffer->pos[i].y_advance;
}
}
}
static inline void
position_cluster (const hb_ot_shape_plan_t *plan,
hb_font_t *font,
hb_buffer_t *buffer,
unsigned int start,
unsigned int end)
{
if (end - start < 2)
return;
/* Find the base glyph */
for (unsigned int i = start; i < end; i++)
if (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&buffer->info[i])))
{
/* Find mark glyphs */
unsigned int j;
for (j = i + 1; j < end; j++)
if (!HB_UNICODE_GENERAL_CATEGORY_IS_MARK (_hb_glyph_info_get_general_category (&buffer->info[j])))
break;
position_around_base (plan, font, buffer, i, j);
i = j - 1;
}
}
void
_hb_ot_shape_fallback_position (const hb_ot_shape_plan_t *plan,
hb_font_t *font,
hb_buffer_t *buffer)
{
unsigned int start = 0;
unsigned int last_cluster = buffer->info[0].cluster;
unsigned int count = buffer->len;
for (unsigned int i = 1; i < count; i++)
if (buffer->info[i].cluster != last_cluster) {
position_cluster (plan, font, buffer, start, i);
start = i;
last_cluster = buffer->info[i].cluster;
}
position_cluster (plan, font, buffer, start, count);
}
|