34300
|
1 /*
|
|
2 * Copyright (C) 2011 Grigori Goronzy <greg@chown.ath.cx>
|
|
3 *
|
|
4 * This file is part of libass.
|
|
5 *
|
|
6 * Permission to use, copy, modify, and distribute this software for any
|
|
7 * purpose with or without fee is hereby granted, provided that the above
|
|
8 * copyright notice and this permission notice appear in all copies.
|
|
9 *
|
|
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
17 */
|
|
18
|
|
19 #include "config.h"
|
|
20
|
34342
|
21 #ifdef CONFIG_FRIBIDI
|
34300
|
22 #include <fribidi/fribidi.h>
|
34342
|
23 #endif
|
34300
|
24
|
|
25 #include "ass_shaper.h"
|
|
26 #include "ass_render.h"
|
|
27 #include "ass_font.h"
|
|
28 #include "ass_parse.h"
|
|
29 #include "ass_cache.h"
|
|
30
|
|
31 #define MAX_RUNS 50
|
|
32
|
|
33 #ifdef CONFIG_HARFBUZZ
|
|
34 #include <hb-ft.h>
|
|
35 enum {
|
|
36 VERT = 0,
|
|
37 VKNA,
|
|
38 KERN
|
|
39 };
|
|
40 #define NUM_FEATURES 3
|
|
41 #endif
|
|
42
|
|
43 struct ass_shaper {
|
|
44 ASS_ShapingLevel shaping_level;
|
|
45
|
|
46 // FriBidi log2vis
|
|
47 int n_glyphs;
|
34342
|
48 #ifdef CONFIG_FRIBIDI
|
34300
|
49 FriBidiChar *event_text;
|
|
50 FriBidiCharType *ctypes;
|
|
51 FriBidiLevel *emblevels;
|
34342
|
52 #endif
|
34300
|
53 FriBidiStrIndex *cmap;
|
|
54 FriBidiParType base_direction;
|
|
55
|
|
56 #ifdef CONFIG_HARFBUZZ
|
|
57 // OpenType features
|
|
58 int n_features;
|
|
59 hb_feature_t *features;
|
|
60 hb_language_t language;
|
|
61
|
|
62 // Glyph metrics cache, to speed up shaping
|
|
63 Cache *metrics_cache;
|
|
64 #endif
|
|
65 };
|
|
66
|
|
67 #ifdef CONFIG_HARFBUZZ
|
|
68 struct ass_shaper_metrics_data {
|
|
69 Cache *metrics_cache;
|
|
70 GlyphMetricsHashKey hash_key;
|
|
71 int vertical;
|
|
72 };
|
|
73
|
|
74 struct ass_shaper_font_data {
|
|
75 hb_font_t *fonts[ASS_FONT_MAX_FACES];
|
|
76 hb_font_funcs_t *font_funcs[ASS_FONT_MAX_FACES];
|
|
77 struct ass_shaper_metrics_data *metrics_data[ASS_FONT_MAX_FACES];
|
|
78 };
|
|
79 #endif
|
|
80
|
|
81 /**
|
|
82 * \brief Print version information
|
|
83 */
|
|
84 void ass_shaper_info(ASS_Library *lib)
|
|
85 {
|
34342
|
86 ass_msg(lib, MSGL_V, "Shaper:"
|
|
87 #ifdef CONFIG_FRIBIDI
|
|
88 " FriBidi " FRIBIDI_VERSION " (SIMPLE)"
|
|
89 #endif
|
34300
|
90 #ifdef CONFIG_HARFBUZZ
|
|
91 " HarfBuzz-ng %s (COMPLEX)", hb_version_string()
|
|
92 #endif
|
|
93 );
|
|
94 }
|
|
95
|
|
96 /**
|
|
97 * \brief grow arrays, if needed
|
|
98 * \param new_size requested size
|
|
99 */
|
|
100 static void check_allocations(ASS_Shaper *shaper, size_t new_size)
|
|
101 {
|
|
102 if (new_size > shaper->n_glyphs) {
|
34342
|
103 #ifdef CONFIG_FRIBIDI
|
34300
|
104 shaper->event_text = realloc(shaper->event_text, sizeof(FriBidiChar) * new_size);
|
|
105 shaper->ctypes = realloc(shaper->ctypes, sizeof(FriBidiCharType) * new_size);
|
|
106 shaper->emblevels = realloc(shaper->emblevels, sizeof(FriBidiLevel) * new_size);
|
34342
|
107 #endif
|
34300
|
108 shaper->cmap = realloc(shaper->cmap, sizeof(FriBidiStrIndex) * new_size);
|
|
109 }
|
|
110 }
|
|
111
|
|
112 /**
|
|
113 * \brief Free shaper and related data
|
|
114 */
|
|
115 void ass_shaper_free(ASS_Shaper *shaper)
|
|
116 {
|
|
117 #ifdef CONFIG_HARFBUZZ
|
|
118 ass_cache_done(shaper->metrics_cache);
|
|
119 free(shaper->features);
|
|
120 #endif
|
34342
|
121 #ifdef CONFIG_FRIBIDI
|
34300
|
122 free(shaper->event_text);
|
|
123 free(shaper->ctypes);
|
|
124 free(shaper->emblevels);
|
34342
|
125 #endif
|
34300
|
126 free(shaper->cmap);
|
|
127 free(shaper);
|
|
128 }
|
|
129
|
|
130 void ass_shaper_font_data_free(ASS_ShaperFontData *priv)
|
|
131 {
|
|
132 #ifdef CONFIG_HARFBUZZ
|
|
133 int i;
|
|
134 for (i = 0; i < ASS_FONT_MAX_FACES; i++)
|
|
135 if (priv->fonts[i]) {
|
|
136 free(priv->metrics_data[i]);
|
|
137 hb_font_destroy(priv->fonts[i]);
|
|
138 hb_font_funcs_destroy(priv->font_funcs[i]);
|
|
139 }
|
|
140 free(priv);
|
|
141 #endif
|
|
142 }
|
|
143
|
|
144 #ifdef CONFIG_HARFBUZZ
|
|
145 /**
|
|
146 * \brief set up the HarfBuzz OpenType feature list with some
|
|
147 * standard features.
|
|
148 */
|
|
149 static void init_features(ASS_Shaper *shaper)
|
|
150 {
|
|
151 shaper->features = calloc(sizeof(hb_feature_t), NUM_FEATURES);
|
|
152
|
|
153 shaper->n_features = NUM_FEATURES;
|
|
154 shaper->features[VERT].tag = HB_TAG('v', 'e', 'r', 't');
|
|
155 shaper->features[VERT].end = INT_MAX;
|
|
156 shaper->features[VKNA].tag = HB_TAG('v', 'k', 'n', 'a');
|
|
157 shaper->features[VKNA].end = INT_MAX;
|
|
158 shaper->features[KERN].tag = HB_TAG('k', 'e', 'r', 'n');
|
|
159 shaper->features[KERN].end = INT_MAX;
|
|
160 }
|
|
161
|
|
162 /**
|
|
163 * \brief Set features depending on properties of the run
|
|
164 */
|
|
165 static void set_run_features(ASS_Shaper *shaper, GlyphInfo *info)
|
|
166 {
|
|
167 // enable vertical substitutions for @font runs
|
|
168 if (info->font->desc.vertical)
|
|
169 shaper->features[VERT].value = shaper->features[VKNA].value = 1;
|
|
170 else
|
|
171 shaper->features[VERT].value = shaper->features[VKNA].value = 0;
|
|
172 }
|
|
173
|
|
174 /**
|
|
175 * \brief Update HarfBuzz's idea of font metrics
|
|
176 * \param hb_font HarfBuzz font
|
|
177 * \param face associated FreeType font face
|
|
178 */
|
|
179 static void update_hb_size(hb_font_t *hb_font, FT_Face face)
|
|
180 {
|
|
181 hb_font_set_scale (hb_font,
|
|
182 ((uint64_t) face->size->metrics.x_scale * (uint64_t) face->units_per_EM) >> 16,
|
|
183 ((uint64_t) face->size->metrics.y_scale * (uint64_t) face->units_per_EM) >> 16);
|
|
184 hb_font_set_ppem (hb_font, face->size->metrics.x_ppem,
|
|
185 face->size->metrics.y_ppem);
|
|
186 }
|
|
187
|
|
188
|
|
189 /*
|
|
190 * Cached glyph metrics getters follow
|
|
191 *
|
|
192 * These functions replace HarfBuzz' standard FreeType font functions
|
|
193 * and provide cached access to essential glyph metrics. This usually
|
|
194 * speeds up shaping a lot. It also allows us to use custom load flags.
|
|
195 *
|
|
196 */
|
|
197
|
|
198 GlyphMetricsHashValue *
|
|
199 get_cached_metrics(struct ass_shaper_metrics_data *metrics, FT_Face face,
|
|
200 hb_codepoint_t glyph)
|
|
201 {
|
|
202 GlyphMetricsHashValue *val;
|
|
203
|
|
204 metrics->hash_key.glyph_index = glyph;
|
|
205 val = ass_cache_get(metrics->metrics_cache, &metrics->hash_key);
|
|
206
|
|
207 if (!val) {
|
|
208 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH
|
|
209 | FT_LOAD_IGNORE_TRANSFORM;
|
|
210 GlyphMetricsHashValue new_val;
|
|
211
|
|
212 if (FT_Load_Glyph(face, glyph, load_flags))
|
|
213 return NULL;
|
|
214
|
|
215 memcpy(&new_val.metrics, &face->glyph->metrics, sizeof(FT_Glyph_Metrics));
|
|
216 val = ass_cache_put(metrics->metrics_cache, &metrics->hash_key, &new_val);
|
|
217 }
|
|
218
|
|
219 return val;
|
|
220 }
|
|
221
|
|
222 static hb_bool_t
|
|
223 get_glyph(hb_font_t *font, void *font_data, hb_codepoint_t unicode,
|
|
224 hb_codepoint_t variation, hb_codepoint_t *glyph, void *user_data)
|
|
225 {
|
|
226 FT_Face face = font_data;
|
|
227
|
|
228 if (variation)
|
|
229 *glyph = FT_Face_GetCharVariantIndex(face, unicode, variation);
|
|
230 else
|
|
231 *glyph = FT_Get_Char_Index(face, unicode);
|
|
232
|
|
233 return *glyph != 0;
|
|
234 }
|
|
235
|
|
236 static hb_position_t
|
|
237 cached_h_advance(hb_font_t *font, void *font_data, hb_codepoint_t glyph,
|
|
238 void *user_data)
|
|
239 {
|
|
240 FT_Face face = font_data;
|
|
241 struct ass_shaper_metrics_data *metrics_priv = user_data;
|
|
242 GlyphMetricsHashValue *metrics = get_cached_metrics(metrics_priv, face, glyph);
|
|
243
|
|
244 if (!metrics)
|
|
245 return 0;
|
|
246
|
|
247 if (metrics_priv->vertical && glyph > VERTICAL_LOWER_BOUND)
|
|
248 return metrics->metrics.vertAdvance;
|
|
249
|
|
250 return metrics->metrics.horiAdvance;
|
|
251 }
|
|
252
|
|
253 static hb_position_t
|
|
254 cached_v_advance(hb_font_t *font, void *font_data, hb_codepoint_t glyph,
|
|
255 void *user_data)
|
|
256 {
|
|
257 FT_Face face = font_data;
|
|
258 struct ass_shaper_metrics_data *metrics_priv = user_data;
|
|
259 GlyphMetricsHashValue *metrics = get_cached_metrics(metrics_priv, face, glyph);
|
|
260
|
|
261 if (!metrics)
|
|
262 return 0;
|
|
263
|
|
264 return metrics->metrics.vertAdvance;
|
|
265
|
|
266 }
|
|
267
|
|
268 static hb_bool_t
|
|
269 cached_h_origin(hb_font_t *font, void *font_data, hb_codepoint_t glyph,
|
|
270 hb_position_t *x, hb_position_t *y, void *user_data)
|
|
271 {
|
|
272 return 1;
|
|
273 }
|
|
274
|
|
275 static hb_bool_t
|
|
276 cached_v_origin(hb_font_t *font, void *font_data, hb_codepoint_t glyph,
|
|
277 hb_position_t *x, hb_position_t *y, void *user_data)
|
|
278 {
|
|
279 FT_Face face = font_data;
|
|
280 struct ass_shaper_metrics_data *metrics_priv = user_data;
|
|
281 GlyphMetricsHashValue *metrics = get_cached_metrics(metrics_priv, face, glyph);
|
|
282
|
|
283 if (!metrics)
|
|
284 return 0;
|
|
285
|
|
286 *x = metrics->metrics.horiBearingX - metrics->metrics.vertBearingX;
|
|
287 *y = metrics->metrics.horiBearingY - (-metrics->metrics.vertBearingY);
|
|
288
|
|
289 return 1;
|
|
290 }
|
|
291
|
|
292 static hb_position_t
|
|
293 get_h_kerning(hb_font_t *font, void *font_data, hb_codepoint_t first,
|
|
294 hb_codepoint_t second, void *user_data)
|
|
295 {
|
|
296 FT_Face face = font_data;
|
|
297 FT_Vector kern;
|
|
298
|
|
299 if (FT_Get_Kerning (face, first, second, FT_KERNING_DEFAULT, &kern))
|
|
300 return 0;
|
|
301
|
|
302 return kern.x;
|
|
303 }
|
|
304
|
|
305 static hb_position_t
|
|
306 get_v_kerning(hb_font_t *font, void *font_data, hb_codepoint_t first,
|
|
307 hb_codepoint_t second, void *user_data)
|
|
308 {
|
|
309 return 0;
|
|
310 }
|
|
311
|
|
312 static hb_bool_t
|
|
313 cached_extents(hb_font_t *font, void *font_data, hb_codepoint_t glyph,
|
|
314 hb_glyph_extents_t *extents, void *user_data)
|
|
315 {
|
|
316 FT_Face face = font_data;
|
|
317 struct ass_shaper_metrics_data *metrics_priv = user_data;
|
|
318 GlyphMetricsHashValue *metrics = get_cached_metrics(metrics_priv, face, glyph);
|
|
319
|
|
320 if (!metrics)
|
|
321 return 0;
|
|
322
|
|
323 extents->x_bearing = metrics->metrics.horiBearingX;
|
|
324 extents->y_bearing = metrics->metrics.horiBearingY;
|
|
325 extents->width = metrics->metrics.width;
|
|
326 extents->height = metrics->metrics.height;
|
|
327
|
|
328 return 1;
|
|
329 }
|
|
330
|
|
331 static hb_bool_t
|
|
332 get_contour_point(hb_font_t *font, void *font_data, hb_codepoint_t glyph,
|
|
333 unsigned int point_index, hb_position_t *x,
|
|
334 hb_position_t *y, void *user_data)
|
|
335 {
|
|
336 FT_Face face = font_data;
|
|
337 int load_flags = FT_LOAD_DEFAULT | FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH
|
|
338 | FT_LOAD_IGNORE_TRANSFORM;
|
|
339
|
|
340 if (FT_Load_Glyph(face, glyph, load_flags))
|
|
341 return 0;
|
|
342
|
|
343 if (point_index >= (unsigned)face->glyph->outline.n_points)
|
|
344 return 0;
|
|
345
|
|
346 *x = face->glyph->outline.points[point_index].x;
|
|
347 *y = face->glyph->outline.points[point_index].y;
|
|
348
|
|
349 return 1;
|
|
350 }
|
|
351
|
|
352 /**
|
|
353 * \brief Retrieve HarfBuzz font from cache.
|
|
354 * Create it from FreeType font, if needed.
|
|
355 * \param info glyph cluster
|
|
356 * \return HarfBuzz font
|
|
357 */
|
|
358 static hb_font_t *get_hb_font(ASS_Shaper *shaper, GlyphInfo *info)
|
|
359 {
|
|
360 ASS_Font *font = info->font;
|
|
361 hb_font_t **hb_fonts;
|
|
362
|
|
363 if (!font->shaper_priv)
|
|
364 font->shaper_priv = calloc(sizeof(ASS_ShaperFontData), 1);
|
|
365
|
|
366
|
|
367 hb_fonts = font->shaper_priv->fonts;
|
|
368 if (!hb_fonts[info->face_index]) {
|
|
369 hb_fonts[info->face_index] =
|
|
370 hb_ft_font_create(font->faces[info->face_index], NULL);
|
|
371
|
|
372 // set up cached metrics access
|
|
373 font->shaper_priv->metrics_data[info->face_index] =
|
|
374 calloc(sizeof(struct ass_shaper_metrics_data), 1);
|
|
375 struct ass_shaper_metrics_data *metrics =
|
|
376 font->shaper_priv->metrics_data[info->face_index];
|
|
377 metrics->metrics_cache = shaper->metrics_cache;
|
|
378 metrics->vertical = info->font->desc.vertical;
|
|
379
|
|
380 hb_font_funcs_t *funcs = hb_font_funcs_create();
|
|
381 font->shaper_priv->font_funcs[info->face_index] = funcs;
|
|
382 hb_font_funcs_set_glyph_func(funcs, get_glyph,
|
|
383 metrics, NULL);
|
|
384 hb_font_funcs_set_glyph_h_advance_func(funcs, cached_h_advance,
|
|
385 metrics, NULL);
|
|
386 hb_font_funcs_set_glyph_v_advance_func(funcs, cached_v_advance,
|
|
387 metrics, NULL);
|
|
388 hb_font_funcs_set_glyph_h_origin_func(funcs, cached_h_origin,
|
|
389 metrics, NULL);
|
|
390 hb_font_funcs_set_glyph_v_origin_func(funcs, cached_v_origin,
|
|
391 metrics, NULL);
|
|
392 hb_font_funcs_set_glyph_h_kerning_func(funcs, get_h_kerning,
|
|
393 metrics, NULL);
|
|
394 hb_font_funcs_set_glyph_v_kerning_func(funcs, get_v_kerning,
|
|
395 metrics, NULL);
|
|
396 hb_font_funcs_set_glyph_extents_func(funcs, cached_extents,
|
|
397 metrics, NULL);
|
|
398 hb_font_funcs_set_glyph_contour_point_func(funcs, get_contour_point,
|
|
399 metrics, NULL);
|
|
400 hb_font_set_funcs(hb_fonts[info->face_index], funcs,
|
|
401 font->faces[info->face_index], NULL);
|
|
402 }
|
|
403
|
|
404 ass_face_set_size(font->faces[info->face_index], info->font_size);
|
|
405 update_hb_size(hb_fonts[info->face_index], font->faces[info->face_index]);
|
|
406
|
|
407 // update hash key for cached metrics
|
|
408 struct ass_shaper_metrics_data *metrics =
|
|
409 font->shaper_priv->metrics_data[info->face_index];
|
|
410 metrics->hash_key.font = info->font;
|
|
411 metrics->hash_key.face_index = info->face_index;
|
|
412 metrics->hash_key.size = info->font_size;
|
|
413 metrics->hash_key.scale_x = double_to_d6(info->scale_x);
|
|
414 metrics->hash_key.scale_y = double_to_d6(info->scale_y);
|
|
415
|
|
416 return hb_fonts[info->face_index];
|
|
417 }
|
|
418
|
|
419 /**
|
|
420 * \brief Shape event text with HarfBuzz. Full OpenType shaping.
|
|
421 * \param glyphs glyph clusters
|
|
422 * \param len number of clusters
|
|
423 */
|
|
424 static void shape_harfbuzz(ASS_Shaper *shaper, GlyphInfo *glyphs, size_t len)
|
|
425 {
|
|
426 int i, j;
|
|
427 int run = 0;
|
|
428 struct {
|
|
429 int offset;
|
|
430 int end;
|
|
431 hb_buffer_t *buf;
|
|
432 hb_font_t *font;
|
|
433 } runs[MAX_RUNS];
|
|
434
|
|
435
|
|
436 for (i = 0; i < len && run < MAX_RUNS; i++, run++) {
|
|
437 // get length and level of the current run
|
|
438 int k = i;
|
|
439 int level = glyphs[i].shape_run_id;
|
|
440 int direction = shaper->emblevels[k] % 2;
|
|
441 while (i < (len - 1) && level == glyphs[i+1].shape_run_id)
|
|
442 i++;
|
|
443 runs[run].offset = k;
|
|
444 runs[run].end = i;
|
|
445 runs[run].buf = hb_buffer_create();
|
|
446 runs[run].font = get_hb_font(shaper, glyphs + k);
|
|
447 set_run_features(shaper, glyphs + k);
|
|
448 hb_buffer_pre_allocate(runs[run].buf, i - k + 1);
|
|
449 hb_buffer_set_direction(runs[run].buf, direction ? HB_DIRECTION_RTL :
|
|
450 HB_DIRECTION_LTR);
|
|
451 hb_buffer_set_language(runs[run].buf, shaper->language);
|
|
452 hb_buffer_add_utf32(runs[run].buf, shaper->event_text + k, i - k + 1,
|
|
453 0, i - k + 1);
|
|
454 hb_shape(runs[run].font, runs[run].buf, shaper->features,
|
|
455 shaper->n_features);
|
|
456 }
|
|
457
|
|
458 // Initialize: skip all glyphs, this is undone later as needed
|
|
459 for (i = 0; i < len; i++)
|
|
460 glyphs[i].skip = 1;
|
|
461
|
|
462 // Update glyph indexes, positions and advances from the shaped runs
|
|
463 for (i = 0; i < run; i++) {
|
|
464 int num_glyphs = hb_buffer_get_length(runs[i].buf);
|
|
465 hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(runs[i].buf, NULL);
|
|
466 hb_glyph_position_t *pos = hb_buffer_get_glyph_positions(runs[i].buf, NULL);
|
|
467
|
|
468 for (j = 0; j < num_glyphs; j++) {
|
|
469 int idx = glyph_info[j].cluster + runs[i].offset;
|
|
470 GlyphInfo *info = glyphs + idx;
|
|
471 GlyphInfo *root = info;
|
|
472
|
|
473 // if we have more than one glyph per cluster, allocate a new one
|
|
474 // and attach to the root glyph
|
|
475 if (info->skip == 0) {
|
|
476 while (info->next)
|
|
477 info = info->next;
|
|
478 info->next = malloc(sizeof(GlyphInfo));
|
|
479 memcpy(info->next, info, sizeof(GlyphInfo));
|
|
480 info = info->next;
|
|
481 info->next = NULL;
|
|
482 }
|
|
483
|
|
484 // set position and advance
|
|
485 info->skip = 0;
|
|
486 info->glyph_index = glyph_info[j].codepoint;
|
|
487 info->offset.x = pos[j].x_offset * info->scale_x;
|
|
488 info->offset.y = -pos[j].y_offset * info->scale_y;
|
|
489 info->advance.x = pos[j].x_advance * info->scale_x;
|
|
490 info->advance.y = -pos[j].y_advance * info->scale_y;
|
|
491
|
|
492 // accumulate advance in the root glyph
|
|
493 root->cluster_advance.x += info->advance.x;
|
|
494 root->cluster_advance.y += info->advance.y;
|
|
495 }
|
|
496 }
|
|
497
|
|
498 // Free runs and associated data
|
|
499 for (i = 0; i < run; i++) {
|
|
500 hb_buffer_destroy(runs[i].buf);
|
|
501 }
|
|
502
|
|
503 }
|
|
504 #endif
|
|
505
|
34342
|
506 #ifdef CONFIG_FRIBIDI
|
34300
|
507 /**
|
|
508 * \brief Shape event text with FriBidi. Does mirroring and simple
|
|
509 * Arabic shaping.
|
|
510 * \param len number of clusters
|
|
511 */
|
|
512 static void shape_fribidi(ASS_Shaper *shaper, GlyphInfo *glyphs, size_t len)
|
|
513 {
|
|
514 int i;
|
|
515 FriBidiJoiningType *joins = calloc(sizeof(*joins), len);
|
|
516
|
|
517 // shape on codepoint level
|
|
518 fribidi_get_joining_types(shaper->event_text, len, joins);
|
|
519 fribidi_join_arabic(shaper->ctypes, len, shaper->emblevels, joins);
|
|
520 fribidi_shape(FRIBIDI_FLAGS_DEFAULT | FRIBIDI_FLAGS_ARABIC,
|
|
521 shaper->emblevels, len, joins, shaper->event_text);
|
|
522
|
|
523 // update indexes
|
|
524 for (i = 0; i < len; i++) {
|
|
525 GlyphInfo *info = glyphs + i;
|
|
526 FT_Face face = info->font->faces[info->face_index];
|
|
527 info->symbol = shaper->event_text[i];
|
|
528 info->glyph_index = FT_Get_Char_Index(face, shaper->event_text[i]);
|
|
529 }
|
|
530
|
|
531 free(joins);
|
|
532 }
|
34342
|
533 #endif
|
34300
|
534
|
|
535 /**
|
|
536 * \brief Toggle kerning for HarfBuzz shaping.
|
|
537 * NOTE: currently only works with OpenType fonts, the TrueType fallback *always*
|
|
538 * kerns. It's a bug in HarfBuzz.
|
|
539 */
|
|
540 void ass_shaper_set_kerning(ASS_Shaper *shaper, int kern)
|
|
541 {
|
|
542 #ifdef CONFIG_HARFBUZZ
|
|
543 shaper->features[KERN].value = !!kern;
|
|
544 #endif
|
|
545 }
|
|
546
|
|
547 /**
|
|
548 * \brief Find shape runs according to the event's selected fonts
|
|
549 */
|
|
550 void ass_shaper_find_runs(ASS_Shaper *shaper, ASS_Renderer *render_priv,
|
|
551 GlyphInfo *glyphs, size_t len)
|
|
552 {
|
|
553 int i;
|
|
554 int shape_run = 0;
|
|
555
|
|
556 for (i = 0; i < len; i++) {
|
|
557 GlyphInfo *last = glyphs + i - 1;
|
|
558 GlyphInfo *info = glyphs + i;
|
|
559 // skip drawings
|
|
560 if (info->symbol == 0xfffc)
|
|
561 continue;
|
|
562 // set size and get glyph index
|
|
563 ass_font_get_index(render_priv->fontconfig_priv, info->font,
|
|
564 info->symbol, &info->face_index, &info->glyph_index);
|
|
565 // shape runs share the same font face and size
|
|
566 if (i > 0 && (last->font != info->font ||
|
|
567 last->font_size != info->font_size ||
|
|
568 last->face_index != info->face_index))
|
|
569 shape_run++;
|
|
570 info->shape_run_id = shape_run;
|
|
571 }
|
|
572
|
|
573 }
|
|
574
|
|
575 /**
|
|
576 * \brief Set base direction (paragraph direction) of the text.
|
|
577 * \param dir base direction
|
|
578 */
|
|
579 void ass_shaper_set_base_direction(ASS_Shaper *shaper, FriBidiParType dir)
|
|
580 {
|
|
581 shaper->base_direction = dir;
|
|
582 }
|
|
583
|
|
584 /**
|
|
585 * \brief Set language hint. Some languages have specific character variants,
|
|
586 * like Serbian Cyrillic.
|
|
587 * \param lang ISO 639-1 two-letter language code
|
|
588 */
|
|
589 void ass_shaper_set_language(ASS_Shaper *shaper, const char *code)
|
|
590 {
|
|
591 #ifdef CONFIG_HARFBUZZ
|
|
592 shaper->language = hb_language_from_string(code, -1);
|
|
593 #endif
|
|
594 }
|
|
595
|
|
596 /**
|
|
597 * Set shaping level. Essentially switches between FriBidi and HarfBuzz.
|
|
598 */
|
|
599 void ass_shaper_set_level(ASS_Shaper *shaper, ASS_ShapingLevel level)
|
|
600 {
|
|
601 shaper->shaping_level = level;
|
|
602 }
|
|
603
|
|
604 /**
|
|
605 * \brief Shape an event's text. Calculates directional runs and shapes them.
|
|
606 * \param text_info event's text
|
|
607 */
|
|
608 void ass_shaper_shape(ASS_Shaper *shaper, TextInfo *text_info)
|
|
609 {
|
34342
|
610 #ifndef CONFIG_FRIBIDI
|
|
611 check_allocations(shaper, text_info->length);
|
|
612 #else
|
34300
|
613 int i, last_break;
|
|
614 FriBidiParType dir;
|
|
615 GlyphInfo *glyphs = text_info->glyphs;
|
|
616
|
|
617 check_allocations(shaper, text_info->length);
|
|
618
|
|
619 // Get bidi character types and embedding levels
|
|
620 last_break = 0;
|
|
621 for (i = 0; i < text_info->length; i++) {
|
|
622 shaper->event_text[i] = glyphs[i].symbol;
|
|
623 // embedding levels should be calculated paragraph by paragraph
|
|
624 if (glyphs[i].symbol == '\n' || i == text_info->length - 1) {
|
|
625 dir = shaper->base_direction;
|
|
626 fribidi_get_bidi_types(shaper->event_text + last_break,
|
|
627 i - last_break + 1, shaper->ctypes + last_break);
|
|
628 fribidi_get_par_embedding_levels(shaper->ctypes + last_break,
|
|
629 i - last_break + 1, &dir, shaper->emblevels + last_break);
|
|
630 last_break = i + 1;
|
|
631 }
|
|
632 }
|
|
633
|
|
634 // add embedding levels to shape runs for final runs
|
|
635 for (i = 0; i < text_info->length; i++) {
|
|
636 glyphs[i].shape_run_id += shaper->emblevels[i];
|
|
637 }
|
|
638
|
|
639 #ifdef CONFIG_HARFBUZZ
|
|
640 switch (shaper->shaping_level) {
|
|
641 case ASS_SHAPING_SIMPLE:
|
|
642 shape_fribidi(shaper, glyphs, text_info->length);
|
|
643 break;
|
|
644 case ASS_SHAPING_COMPLEX:
|
|
645 shape_harfbuzz(shaper, glyphs, text_info->length);
|
|
646 break;
|
|
647 }
|
|
648 #else
|
|
649 shape_fribidi(shaper, glyphs, text_info->length);
|
|
650 #endif
|
|
651
|
|
652
|
|
653 // clean up
|
|
654 for (i = 0; i < text_info->length; i++) {
|
|
655 // Skip direction override control characters
|
|
656 // NOTE: Behdad said HarfBuzz is supposed to remove these, but this hasn't
|
|
657 // been implemented yet
|
|
658 if (glyphs[i].symbol <= 0x202F && glyphs[i].symbol >= 0x202a) {
|
|
659 glyphs[i].symbol = 0;
|
|
660 glyphs[i].skip++;
|
|
661 }
|
|
662 }
|
34342
|
663 #endif
|
34300
|
664 }
|
|
665
|
|
666 /**
|
|
667 * \brief Create a new shaper instance and preallocate data structures
|
|
668 * \param prealloc preallocation size
|
|
669 */
|
|
670 ASS_Shaper *ass_shaper_new(size_t prealloc)
|
|
671 {
|
|
672 ASS_Shaper *shaper = calloc(sizeof(*shaper), 1);
|
|
673
|
34342
|
674 #ifdef CONFIG_FRIBIDI
|
34300
|
675 shaper->base_direction = FRIBIDI_PAR_ON;
|
34342
|
676 #endif
|
34300
|
677 check_allocations(shaper, prealloc);
|
|
678
|
|
679 #ifdef CONFIG_HARFBUZZ
|
|
680 init_features(shaper);
|
|
681 shaper->metrics_cache = ass_glyph_metrics_cache_create();
|
|
682 #endif
|
|
683
|
|
684 return shaper;
|
|
685 }
|
|
686
|
|
687
|
|
688 /**
|
|
689 * \brief clean up additional data temporarily needed for shaping and
|
|
690 * (e.g. additional glyphs allocated)
|
|
691 */
|
|
692 void ass_shaper_cleanup(ASS_Shaper *shaper, TextInfo *text_info)
|
|
693 {
|
|
694 int i;
|
|
695
|
|
696 for (i = 0; i < text_info->length; i++) {
|
|
697 GlyphInfo *info = text_info->glyphs + i;
|
|
698 info = info->next;
|
|
699 while (info) {
|
|
700 GlyphInfo *next = info->next;
|
|
701 free(info);
|
|
702 info = next;
|
|
703 }
|
|
704 }
|
|
705 }
|
|
706
|
|
707 /**
|
|
708 * \brief Calculate reorder map to render glyphs in visual order
|
|
709 */
|
|
710 FriBidiStrIndex *ass_shaper_reorder(ASS_Shaper *shaper, TextInfo *text_info)
|
|
711 {
|
|
712 int i;
|
|
713
|
|
714 // Initialize reorder map
|
|
715 for (i = 0; i < text_info->length; i++)
|
|
716 shaper->cmap[i] = i;
|
|
717
|
34342
|
718 #ifdef CONFIG_FRIBIDI
|
34300
|
719 // Create reorder map line-by-line
|
|
720 for (i = 0; i < text_info->n_lines; i++) {
|
|
721 LineInfo *line = text_info->lines + i;
|
|
722 int level;
|
|
723 FriBidiParType dir = FRIBIDI_PAR_ON;
|
|
724
|
|
725 level = fribidi_reorder_line(0,
|
|
726 shaper->ctypes + line->offset, line->len, 0, dir,
|
|
727 shaper->emblevels + line->offset, NULL,
|
|
728 shaper->cmap + line->offset);
|
|
729 }
|
34342
|
730 #endif
|
34300
|
731
|
|
732 return shaper->cmap;
|
|
733 }
|
|
734
|
|
735 /**
|
|
736 * \brief Resolve a Windows font encoding number to a suitable
|
|
737 * base direction. 177 and 178 are Hebrew and Arabic respectively, and
|
|
738 * they map to RTL. 1 is autodetection and is mapped to just that.
|
|
739 * Everything else is mapped to LTR.
|
|
740 * \param enc Windows font encoding
|
|
741 */
|
|
742 FriBidiParType resolve_base_direction(int enc)
|
|
743 {
|
34342
|
744 #ifdef CONFIG_FRIBIDI
|
34300
|
745 switch (enc) {
|
|
746 case 1:
|
|
747 return FRIBIDI_PAR_ON;
|
|
748 case 177:
|
|
749 case 178:
|
|
750 return FRIBIDI_PAR_RTL;
|
|
751 default:
|
|
752 return FRIBIDI_PAR_LTR;
|
|
753 }
|
34342
|
754 #else
|
|
755 return 0;
|
|
756 #endif
|
34300
|
757 }
|