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
|
35262
|
404 // XXX: this is a rather crude hack
|
|
405 const double ft_size = 256.0;
|
|
406 ass_face_set_size(font->faces[info->face_index], ft_size);
|
34300
|
407 update_hb_size(hb_fonts[info->face_index], font->faces[info->face_index]);
|
|
408
|
|
409 // update hash key for cached metrics
|
|
410 struct ass_shaper_metrics_data *metrics =
|
|
411 font->shaper_priv->metrics_data[info->face_index];
|
|
412 metrics->hash_key.font = info->font;
|
|
413 metrics->hash_key.face_index = info->face_index;
|
|
414 metrics->hash_key.size = info->font_size;
|
|
415 metrics->hash_key.scale_x = double_to_d6(info->scale_x);
|
|
416 metrics->hash_key.scale_y = double_to_d6(info->scale_y);
|
|
417
|
|
418 return hb_fonts[info->face_index];
|
|
419 }
|
|
420
|
|
421 /**
|
|
422 * \brief Shape event text with HarfBuzz. Full OpenType shaping.
|
|
423 * \param glyphs glyph clusters
|
|
424 * \param len number of clusters
|
|
425 */
|
|
426 static void shape_harfbuzz(ASS_Shaper *shaper, GlyphInfo *glyphs, size_t len)
|
|
427 {
|
|
428 int i, j;
|
|
429 int run = 0;
|
|
430 struct {
|
|
431 int offset;
|
|
432 int end;
|
|
433 hb_buffer_t *buf;
|
|
434 hb_font_t *font;
|
|
435 } runs[MAX_RUNS];
|
35262
|
436 const double ft_size = 256.0;
|
34300
|
437
|
|
438 for (i = 0; i < len && run < MAX_RUNS; i++, run++) {
|
|
439 // get length and level of the current run
|
|
440 int k = i;
|
|
441 int level = glyphs[i].shape_run_id;
|
|
442 int direction = shaper->emblevels[k] % 2;
|
|
443 while (i < (len - 1) && level == glyphs[i+1].shape_run_id)
|
|
444 i++;
|
|
445 runs[run].offset = k;
|
|
446 runs[run].end = i;
|
|
447 runs[run].buf = hb_buffer_create();
|
|
448 runs[run].font = get_hb_font(shaper, glyphs + k);
|
|
449 set_run_features(shaper, glyphs + k);
|
|
450 hb_buffer_pre_allocate(runs[run].buf, i - k + 1);
|
|
451 hb_buffer_set_direction(runs[run].buf, direction ? HB_DIRECTION_RTL :
|
|
452 HB_DIRECTION_LTR);
|
|
453 hb_buffer_set_language(runs[run].buf, shaper->language);
|
|
454 hb_buffer_add_utf32(runs[run].buf, shaper->event_text + k, i - k + 1,
|
|
455 0, i - k + 1);
|
|
456 hb_shape(runs[run].font, runs[run].buf, shaper->features,
|
|
457 shaper->n_features);
|
|
458 }
|
|
459
|
|
460 // Initialize: skip all glyphs, this is undone later as needed
|
|
461 for (i = 0; i < len; i++)
|
|
462 glyphs[i].skip = 1;
|
|
463
|
|
464 // Update glyph indexes, positions and advances from the shaped runs
|
|
465 for (i = 0; i < run; i++) {
|
|
466 int num_glyphs = hb_buffer_get_length(runs[i].buf);
|
|
467 hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(runs[i].buf, NULL);
|
|
468 hb_glyph_position_t *pos = hb_buffer_get_glyph_positions(runs[i].buf, NULL);
|
|
469
|
|
470 for (j = 0; j < num_glyphs; j++) {
|
|
471 int idx = glyph_info[j].cluster + runs[i].offset;
|
|
472 GlyphInfo *info = glyphs + idx;
|
|
473 GlyphInfo *root = info;
|
|
474
|
|
475 // if we have more than one glyph per cluster, allocate a new one
|
|
476 // and attach to the root glyph
|
|
477 if (info->skip == 0) {
|
|
478 while (info->next)
|
|
479 info = info->next;
|
|
480 info->next = malloc(sizeof(GlyphInfo));
|
|
481 memcpy(info->next, info, sizeof(GlyphInfo));
|
|
482 info = info->next;
|
|
483 info->next = NULL;
|
|
484 }
|
|
485
|
|
486 // set position and advance
|
|
487 info->skip = 0;
|
|
488 info->glyph_index = glyph_info[j].codepoint;
|
35262
|
489 info->offset.x = pos[j].x_offset * info->scale_x * (info->font_size / ft_size);
|
|
490 info->offset.y = -pos[j].y_offset * info->scale_y * (info->font_size / ft_size);
|
|
491 info->advance.x = pos[j].x_advance * info->scale_x * (info->font_size / ft_size);
|
|
492 info->advance.y = -pos[j].y_advance * info->scale_y * (info->font_size / ft_size);
|
34300
|
493
|
|
494 // accumulate advance in the root glyph
|
|
495 root->cluster_advance.x += info->advance.x;
|
|
496 root->cluster_advance.y += info->advance.y;
|
|
497 }
|
|
498 }
|
|
499
|
|
500 // Free runs and associated data
|
|
501 for (i = 0; i < run; i++) {
|
|
502 hb_buffer_destroy(runs[i].buf);
|
|
503 }
|
|
504
|
|
505 }
|
|
506 #endif
|
|
507
|
34342
|
508 #ifdef CONFIG_FRIBIDI
|
34300
|
509 /**
|
|
510 * \brief Shape event text with FriBidi. Does mirroring and simple
|
|
511 * Arabic shaping.
|
|
512 * \param len number of clusters
|
|
513 */
|
|
514 static void shape_fribidi(ASS_Shaper *shaper, GlyphInfo *glyphs, size_t len)
|
|
515 {
|
|
516 int i;
|
|
517 FriBidiJoiningType *joins = calloc(sizeof(*joins), len);
|
|
518
|
|
519 // shape on codepoint level
|
|
520 fribidi_get_joining_types(shaper->event_text, len, joins);
|
|
521 fribidi_join_arabic(shaper->ctypes, len, shaper->emblevels, joins);
|
|
522 fribidi_shape(FRIBIDI_FLAGS_DEFAULT | FRIBIDI_FLAGS_ARABIC,
|
|
523 shaper->emblevels, len, joins, shaper->event_text);
|
|
524
|
|
525 // update indexes
|
|
526 for (i = 0; i < len; i++) {
|
|
527 GlyphInfo *info = glyphs + i;
|
|
528 FT_Face face = info->font->faces[info->face_index];
|
|
529 info->symbol = shaper->event_text[i];
|
|
530 info->glyph_index = FT_Get_Char_Index(face, shaper->event_text[i]);
|
|
531 }
|
|
532
|
|
533 free(joins);
|
|
534 }
|
34342
|
535 #endif
|
34300
|
536
|
|
537 /**
|
|
538 * \brief Toggle kerning for HarfBuzz shaping.
|
|
539 * NOTE: currently only works with OpenType fonts, the TrueType fallback *always*
|
|
540 * kerns. It's a bug in HarfBuzz.
|
|
541 */
|
|
542 void ass_shaper_set_kerning(ASS_Shaper *shaper, int kern)
|
|
543 {
|
|
544 #ifdef CONFIG_HARFBUZZ
|
|
545 shaper->features[KERN].value = !!kern;
|
|
546 #endif
|
|
547 }
|
|
548
|
|
549 /**
|
|
550 * \brief Find shape runs according to the event's selected fonts
|
|
551 */
|
|
552 void ass_shaper_find_runs(ASS_Shaper *shaper, ASS_Renderer *render_priv,
|
|
553 GlyphInfo *glyphs, size_t len)
|
|
554 {
|
|
555 int i;
|
|
556 int shape_run = 0;
|
|
557
|
|
558 for (i = 0; i < len; i++) {
|
|
559 GlyphInfo *last = glyphs + i - 1;
|
|
560 GlyphInfo *info = glyphs + i;
|
|
561 // skip drawings
|
|
562 if (info->symbol == 0xfffc)
|
|
563 continue;
|
|
564 // set size and get glyph index
|
|
565 ass_font_get_index(render_priv->fontconfig_priv, info->font,
|
|
566 info->symbol, &info->face_index, &info->glyph_index);
|
|
567 // shape runs share the same font face and size
|
|
568 if (i > 0 && (last->font != info->font ||
|
|
569 last->font_size != info->font_size ||
|
|
570 last->face_index != info->face_index))
|
|
571 shape_run++;
|
|
572 info->shape_run_id = shape_run;
|
|
573 }
|
|
574
|
|
575 }
|
|
576
|
|
577 /**
|
|
578 * \brief Set base direction (paragraph direction) of the text.
|
|
579 * \param dir base direction
|
|
580 */
|
|
581 void ass_shaper_set_base_direction(ASS_Shaper *shaper, FriBidiParType dir)
|
|
582 {
|
|
583 shaper->base_direction = dir;
|
|
584 }
|
|
585
|
|
586 /**
|
|
587 * \brief Set language hint. Some languages have specific character variants,
|
|
588 * like Serbian Cyrillic.
|
|
589 * \param lang ISO 639-1 two-letter language code
|
|
590 */
|
|
591 void ass_shaper_set_language(ASS_Shaper *shaper, const char *code)
|
|
592 {
|
|
593 #ifdef CONFIG_HARFBUZZ
|
|
594 shaper->language = hb_language_from_string(code, -1);
|
|
595 #endif
|
|
596 }
|
|
597
|
|
598 /**
|
|
599 * Set shaping level. Essentially switches between FriBidi and HarfBuzz.
|
|
600 */
|
|
601 void ass_shaper_set_level(ASS_Shaper *shaper, ASS_ShapingLevel level)
|
|
602 {
|
|
603 shaper->shaping_level = level;
|
|
604 }
|
|
605
|
|
606 /**
|
35262
|
607 * \brief Remove all zero-width invisible characters from the text.
|
|
608 * \param text_info text
|
|
609 */
|
|
610 static void ass_shaper_skip_characters(TextInfo *text_info)
|
|
611 {
|
|
612 int i;
|
|
613 GlyphInfo *glyphs = text_info->glyphs;
|
|
614
|
|
615 for (i = 0; i < text_info->length; i++) {
|
|
616 // Skip direction override control characters
|
|
617 if ((glyphs[i].symbol <= 0x202e && glyphs[i].symbol >= 0x202a)
|
|
618 || (glyphs[i].symbol <= 0x200f && glyphs[i].symbol >= 0x200b)
|
|
619 || (glyphs[i].symbol <= 0x2063 && glyphs[i].symbol >= 0x2060)
|
|
620 || glyphs[i].symbol == 0xfeff
|
|
621 || glyphs[i].symbol == 0x00ad
|
|
622 || glyphs[i].symbol == 0x034f) {
|
|
623 glyphs[i].symbol = 0;
|
|
624 glyphs[i].skip++;
|
|
625 }
|
|
626 }
|
|
627 }
|
|
628
|
|
629 /**
|
34300
|
630 * \brief Shape an event's text. Calculates directional runs and shapes them.
|
|
631 * \param text_info event's text
|
|
632 */
|
|
633 void ass_shaper_shape(ASS_Shaper *shaper, TextInfo *text_info)
|
|
634 {
|
34342
|
635 #ifndef CONFIG_FRIBIDI
|
|
636 check_allocations(shaper, text_info->length);
|
|
637 #else
|
34300
|
638 int i, last_break;
|
|
639 FriBidiParType dir;
|
|
640 GlyphInfo *glyphs = text_info->glyphs;
|
|
641
|
|
642 check_allocations(shaper, text_info->length);
|
|
643
|
|
644 // Get bidi character types and embedding levels
|
|
645 last_break = 0;
|
|
646 for (i = 0; i < text_info->length; i++) {
|
|
647 shaper->event_text[i] = glyphs[i].symbol;
|
|
648 // embedding levels should be calculated paragraph by paragraph
|
|
649 if (glyphs[i].symbol == '\n' || i == text_info->length - 1) {
|
|
650 dir = shaper->base_direction;
|
|
651 fribidi_get_bidi_types(shaper->event_text + last_break,
|
|
652 i - last_break + 1, shaper->ctypes + last_break);
|
|
653 fribidi_get_par_embedding_levels(shaper->ctypes + last_break,
|
|
654 i - last_break + 1, &dir, shaper->emblevels + last_break);
|
|
655 last_break = i + 1;
|
|
656 }
|
|
657 }
|
|
658
|
|
659 // add embedding levels to shape runs for final runs
|
|
660 for (i = 0; i < text_info->length; i++) {
|
|
661 glyphs[i].shape_run_id += shaper->emblevels[i];
|
|
662 }
|
|
663
|
|
664 #ifdef CONFIG_HARFBUZZ
|
|
665 switch (shaper->shaping_level) {
|
|
666 case ASS_SHAPING_SIMPLE:
|
35274
|
667 shape_fribidi(shaper, glyphs, text_info->length);
|
35262
|
668 ass_shaper_skip_characters(text_info);
|
34300
|
669 break;
|
|
670 case ASS_SHAPING_COMPLEX:
|
|
671 shape_harfbuzz(shaper, glyphs, text_info->length);
|
|
672 break;
|
|
673 }
|
|
674 #else
|
35274
|
675 shape_fribidi(shaper, glyphs, text_info->length);
|
35262
|
676 ass_shaper_skip_characters(text_info);
|
34300
|
677 #endif
|
34342
|
678 #endif
|
34300
|
679 }
|
|
680
|
|
681 /**
|
|
682 * \brief Create a new shaper instance and preallocate data structures
|
|
683 * \param prealloc preallocation size
|
|
684 */
|
|
685 ASS_Shaper *ass_shaper_new(size_t prealloc)
|
|
686 {
|
|
687 ASS_Shaper *shaper = calloc(sizeof(*shaper), 1);
|
|
688
|
34342
|
689 #ifdef CONFIG_FRIBIDI
|
34300
|
690 shaper->base_direction = FRIBIDI_PAR_ON;
|
34342
|
691 #endif
|
34300
|
692 check_allocations(shaper, prealloc);
|
|
693
|
|
694 #ifdef CONFIG_HARFBUZZ
|
|
695 init_features(shaper);
|
|
696 shaper->metrics_cache = ass_glyph_metrics_cache_create();
|
|
697 #endif
|
|
698
|
|
699 return shaper;
|
|
700 }
|
|
701
|
|
702
|
|
703 /**
|
|
704 * \brief clean up additional data temporarily needed for shaping and
|
|
705 * (e.g. additional glyphs allocated)
|
|
706 */
|
|
707 void ass_shaper_cleanup(ASS_Shaper *shaper, TextInfo *text_info)
|
|
708 {
|
|
709 int i;
|
|
710
|
|
711 for (i = 0; i < text_info->length; i++) {
|
|
712 GlyphInfo *info = text_info->glyphs + i;
|
|
713 info = info->next;
|
|
714 while (info) {
|
|
715 GlyphInfo *next = info->next;
|
|
716 free(info);
|
|
717 info = next;
|
|
718 }
|
|
719 }
|
|
720 }
|
|
721
|
|
722 /**
|
|
723 * \brief Calculate reorder map to render glyphs in visual order
|
|
724 */
|
|
725 FriBidiStrIndex *ass_shaper_reorder(ASS_Shaper *shaper, TextInfo *text_info)
|
|
726 {
|
|
727 int i;
|
|
728
|
|
729 // Initialize reorder map
|
|
730 for (i = 0; i < text_info->length; i++)
|
|
731 shaper->cmap[i] = i;
|
|
732
|
34342
|
733 #ifdef CONFIG_FRIBIDI
|
34300
|
734 // Create reorder map line-by-line
|
|
735 for (i = 0; i < text_info->n_lines; i++) {
|
|
736 LineInfo *line = text_info->lines + i;
|
|
737 int level;
|
|
738 FriBidiParType dir = FRIBIDI_PAR_ON;
|
|
739
|
|
740 level = fribidi_reorder_line(0,
|
|
741 shaper->ctypes + line->offset, line->len, 0, dir,
|
|
742 shaper->emblevels + line->offset, NULL,
|
|
743 shaper->cmap + line->offset);
|
|
744 }
|
34342
|
745 #endif
|
34300
|
746
|
|
747 return shaper->cmap;
|
|
748 }
|
|
749
|
|
750 /**
|
35262
|
751 * \brief Resolve a Windows font charset number to a suitable
|
34300
|
752 * base direction. 177 and 178 are Hebrew and Arabic respectively, and
|
35262
|
753 * they map to RTL. Everything else maps to LTR for compatibility
|
|
754 * reasons. The special value -1, which is not a legal Windows font charset
|
|
755 * number, can be used for autodetection.
|
34300
|
756 * \param enc Windows font encoding
|
|
757 */
|
|
758 FriBidiParType resolve_base_direction(int enc)
|
|
759 {
|
34342
|
760 #ifdef CONFIG_FRIBIDI
|
34300
|
761 switch (enc) {
|
35262
|
762 case -1:
|
34300
|
763 return FRIBIDI_PAR_ON;
|
|
764 case 177:
|
|
765 case 178:
|
|
766 return FRIBIDI_PAR_RTL;
|
|
767 default:
|
|
768 return FRIBIDI_PAR_LTR;
|
|
769 }
|
34342
|
770 #else
|
|
771 return 0;
|
|
772 #endif
|
34300
|
773 }
|