cache: unified bitmap cache
[libass.git] / libass / ass_render.c
blob39c41d2140a5721e59df53e2e1346e641f988f6a
1 /*
2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
4 * This file is part of libass.
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.
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.
19 #include "config.h"
21 #include <assert.h>
22 #include <math.h>
24 #include "ass_render.h"
25 #include "ass_parse.h"
27 #define MAX_GLYPHS_INITIAL 1024
28 #define MAX_LINES_INITIAL 64
29 #define SUBPIXEL_MASK 63
30 #define SUBPIXEL_ACCURACY 7
32 ASS_Renderer *ass_renderer_init(ASS_Library *library)
34 int error;
35 FT_Library ft;
36 ASS_Renderer *priv = 0;
37 int vmajor, vminor, vpatch;
39 error = FT_Init_FreeType(&ft);
40 if (error) {
41 ass_msg(library, MSGL_FATAL, "%s failed", "FT_Init_FreeType");
42 goto ass_init_exit;
45 FT_Library_Version(ft, &vmajor, &vminor, &vpatch);
46 ass_msg(library, MSGL_V, "FreeType library version: %d.%d.%d",
47 vmajor, vminor, vpatch);
48 ass_msg(library, MSGL_V, "FreeType headers version: %d.%d.%d",
49 FREETYPE_MAJOR, FREETYPE_MINOR, FREETYPE_PATCH);
51 priv = calloc(1, sizeof(ASS_Renderer));
52 if (!priv) {
53 FT_Done_FreeType(ft);
54 goto ass_init_exit;
57 priv->synth_priv = ass_synth_init(BLUR_MAX_RADIUS);
59 priv->library = library;
60 priv->ftlibrary = ft;
61 // images_root and related stuff is zero-filled in calloc
63 priv->cache.font_cache = ass_font_cache_create();
64 priv->cache.bitmap_cache = ass_bitmap_cache_create();
65 priv->cache.composite_cache = ass_composite_cache_create();
66 priv->cache.outline_cache = ass_outline_cache_create();
67 priv->cache.glyph_max = GLYPH_CACHE_MAX;
68 priv->cache.bitmap_max_size = BITMAP_CACHE_MAX_SIZE;
70 priv->text_info.max_glyphs = MAX_GLYPHS_INITIAL;
71 priv->text_info.max_lines = MAX_LINES_INITIAL;
72 priv->text_info.glyphs = calloc(MAX_GLYPHS_INITIAL, sizeof(GlyphInfo));
73 priv->text_info.lines = calloc(MAX_LINES_INITIAL, sizeof(LineInfo));
75 priv->settings.font_size_coeff = 1.;
77 ass_init_exit:
78 if (priv)
79 ass_msg(library, MSGL_V, "Init");
80 else
81 ass_msg(library, MSGL_ERR, "Init failed");
83 return priv;
86 static void free_list_clear(ASS_Renderer *render_priv)
88 if (render_priv->free_head) {
89 FreeList *item = render_priv->free_head;
90 while(item) {
91 FreeList *oi = item;
92 free(item->object);
93 item = item->next;
94 free(oi);
96 render_priv->free_head = NULL;
100 void ass_renderer_done(ASS_Renderer *render_priv)
102 ass_cache_done(render_priv->cache.font_cache);
103 ass_cache_done(render_priv->cache.bitmap_cache);
104 ass_cache_done(render_priv->cache.composite_cache);
105 ass_cache_done(render_priv->cache.outline_cache);
107 ass_free_images(render_priv->images_root);
108 ass_free_images(render_priv->prev_images_root);
110 if (render_priv->state.stroker) {
111 FT_Stroker_Done(render_priv->state.stroker);
112 render_priv->state.stroker = 0;
114 if (render_priv->ftlibrary)
115 FT_Done_FreeType(render_priv->ftlibrary);
116 if (render_priv->fontconfig_priv)
117 fontconfig_done(render_priv->fontconfig_priv);
118 if (render_priv->synth_priv)
119 ass_synth_done(render_priv->synth_priv);
120 free(render_priv->eimg);
121 free(render_priv->text_info.glyphs);
122 free(render_priv->text_info.lines);
124 free(render_priv->settings.default_font);
125 free(render_priv->settings.default_family);
127 free_list_clear(render_priv);
128 free(render_priv);
132 * \brief Create a new ASS_Image
133 * Parameters are the same as ASS_Image fields.
135 static ASS_Image *my_draw_bitmap(unsigned char *bitmap, int bitmap_w,
136 int bitmap_h, int stride, int dst_x,
137 int dst_y, uint32_t color)
139 ASS_Image *img = malloc(sizeof(ASS_Image));
141 if (img) {
142 img->w = bitmap_w;
143 img->h = bitmap_h;
144 img->stride = stride;
145 img->bitmap = bitmap;
146 img->color = color;
147 img->dst_x = dst_x;
148 img->dst_y = dst_y;
151 return img;
155 * \brief Mapping between script and screen coordinates
157 static double x2scr(ASS_Renderer *render_priv, double x)
159 return x * render_priv->orig_width_nocrop / render_priv->font_scale_x /
160 render_priv->track->PlayResX +
161 FFMAX(render_priv->settings.left_margin, 0);
163 static double x2scr_pos(ASS_Renderer *render_priv, double x)
165 return x * render_priv->orig_width / render_priv->font_scale_x / render_priv->track->PlayResX +
166 render_priv->settings.left_margin;
168 static double x2scr_scaled(ASS_Renderer *render_priv, double x)
170 return x * render_priv->orig_width_nocrop /
171 render_priv->track->PlayResX +
172 FFMAX(render_priv->settings.left_margin, 0);
174 static double x2scr_pos_scaled(ASS_Renderer *render_priv, double x)
176 return x * render_priv->orig_width / render_priv->track->PlayResX +
177 render_priv->settings.left_margin;
180 * \brief Mapping between script and screen coordinates
182 static double y2scr(ASS_Renderer *render_priv, double y)
184 return y * render_priv->orig_height_nocrop /
185 render_priv->track->PlayResY +
186 FFMAX(render_priv->settings.top_margin, 0);
188 static double y2scr_pos(ASS_Renderer *render_priv, double y)
190 return y * render_priv->orig_height / render_priv->track->PlayResY +
191 render_priv->settings.top_margin;
194 // the same for toptitles
195 static double y2scr_top(ASS_Renderer *render_priv, double y)
197 if (render_priv->settings.use_margins)
198 return y * render_priv->orig_height_nocrop /
199 render_priv->track->PlayResY;
200 else
201 return y * render_priv->orig_height_nocrop /
202 render_priv->track->PlayResY +
203 FFMAX(render_priv->settings.top_margin, 0);
205 // the same for subtitles
206 static double y2scr_sub(ASS_Renderer *render_priv, double y)
208 if (render_priv->settings.use_margins)
209 return y * render_priv->orig_height_nocrop /
210 render_priv->track->PlayResY +
211 FFMAX(render_priv->settings.top_margin, 0)
212 + FFMAX(render_priv->settings.bottom_margin, 0);
213 else
214 return y * render_priv->orig_height_nocrop /
215 render_priv->track->PlayResY +
216 FFMAX(render_priv->settings.top_margin, 0);
220 * \brief Convert bitmap glyphs into ASS_Image list with inverse clipping
222 * Inverse clipping with the following strategy:
223 * - find rectangle from (x0, y0) to (cx0, y1)
224 * - find rectangle from (cx0, y0) to (cx1, cy0)
225 * - find rectangle from (cx0, cy1) to (cx1, y1)
226 * - find rectangle from (cx1, y0) to (x1, y1)
227 * These rectangles can be invalid and in this case are discarded.
228 * Afterwards, they are clipped against the screen coordinates.
229 * In an additional pass, the rectangles need to be split up left/right for
230 * karaoke effects. This can result in a lot of bitmaps (6 to be exact).
232 static ASS_Image **render_glyph_i(ASS_Renderer *render_priv,
233 Bitmap *bm, int dst_x, int dst_y,
234 uint32_t color, uint32_t color2, int brk,
235 ASS_Image **tail)
237 int i, j, x0, y0, x1, y1, cx0, cy0, cx1, cy1, sx, sy, zx, zy;
238 Rect r[4];
239 ASS_Image *img;
241 dst_x += bm->left;
242 dst_y += bm->top;
244 // we still need to clip against screen boundaries
245 zx = x2scr_pos_scaled(render_priv, 0);
246 zy = y2scr_pos(render_priv, 0);
247 sx = x2scr_pos_scaled(render_priv, render_priv->track->PlayResX);
248 sy = y2scr_pos(render_priv, render_priv->track->PlayResY);
250 x0 = 0;
251 y0 = 0;
252 x1 = bm->w;
253 y1 = bm->h;
254 cx0 = render_priv->state.clip_x0 - dst_x;
255 cy0 = render_priv->state.clip_y0 - dst_y;
256 cx1 = render_priv->state.clip_x1 - dst_x;
257 cy1 = render_priv->state.clip_y1 - dst_y;
259 // calculate rectangles and discard invalid ones while we're at it.
260 i = 0;
261 r[i].x0 = x0;
262 r[i].y0 = y0;
263 r[i].x1 = (cx0 > x1) ? x1 : cx0;
264 r[i].y1 = y1;
265 if (r[i].x1 > r[i].x0 && r[i].y1 > r[i].y0) i++;
266 r[i].x0 = (cx0 < 0) ? x0 : cx0;
267 r[i].y0 = y0;
268 r[i].x1 = (cx1 > x1) ? x1 : cx1;
269 r[i].y1 = (cy0 > y1) ? y1 : cy0;
270 if (r[i].x1 > r[i].x0 && r[i].y1 > r[i].y0) i++;
271 r[i].x0 = (cx0 < 0) ? x0 : cx0;
272 r[i].y0 = (cy1 < 0) ? y0 : cy1;
273 r[i].x1 = (cx1 > x1) ? x1 : cx1;
274 r[i].y1 = y1;
275 if (r[i].x1 > r[i].x0 && r[i].y1 > r[i].y0) i++;
276 r[i].x0 = (cx1 < 0) ? x0 : cx1;
277 r[i].y0 = y0;
278 r[i].x1 = x1;
279 r[i].y1 = y1;
280 if (r[i].x1 > r[i].x0 && r[i].y1 > r[i].y0) i++;
282 // clip each rectangle to screen coordinates
283 for (j = 0; j < i; j++) {
284 r[j].x0 = (r[j].x0 + dst_x < zx) ? zx - dst_x : r[j].x0;
285 r[j].y0 = (r[j].y0 + dst_y < zy) ? zy - dst_y : r[j].y0;
286 r[j].x1 = (r[j].x1 + dst_x > sx) ? sx - dst_x : r[j].x1;
287 r[j].y1 = (r[j].y1 + dst_y > sy) ? sy - dst_y : r[j].y1;
290 // draw the rectangles
291 for (j = 0; j < i; j++) {
292 int lbrk = brk;
293 // kick out rectangles that are invalid now
294 if (r[j].x1 <= r[j].x0 || r[j].y1 <= r[j].y0)
295 continue;
296 // split up into left and right for karaoke, if needed
297 if (lbrk > r[j].x0) {
298 if (lbrk > r[j].x1) lbrk = r[j].x1;
299 img = my_draw_bitmap(bm->buffer + r[j].y0 * bm->stride + r[j].x0,
300 lbrk - r[j].x0, r[j].y1 - r[j].y0,
301 bm->stride, dst_x + r[j].x0, dst_y + r[j].y0, color);
302 if (!img) break;
303 *tail = img;
304 tail = &img->next;
306 if (lbrk < r[j].x1) {
307 if (lbrk < r[j].x0) lbrk = r[j].x0;
308 img = my_draw_bitmap(bm->buffer + r[j].y0 * bm->stride + lbrk,
309 r[j].x1 - lbrk, r[j].y1 - r[j].y0,
310 bm->stride, dst_x + lbrk, dst_y + r[j].y0, color2);
311 if (!img) break;
312 *tail = img;
313 tail = &img->next;
317 return tail;
321 * \brief convert bitmap glyph into ASS_Image struct(s)
322 * \param bit freetype bitmap glyph, FT_PIXEL_MODE_GRAY
323 * \param dst_x bitmap x coordinate in video frame
324 * \param dst_y bitmap y coordinate in video frame
325 * \param color first color, RGBA
326 * \param color2 second color, RGBA
327 * \param brk x coordinate relative to glyph origin, color is used to the left of brk, color2 - to the right
328 * \param tail pointer to the last image's next field, head of the generated list should be stored here
329 * \return pointer to the new list tail
330 * Performs clipping. Uses my_draw_bitmap for actual bitmap convertion.
332 static ASS_Image **
333 render_glyph(ASS_Renderer *render_priv, Bitmap *bm, int dst_x, int dst_y,
334 uint32_t color, uint32_t color2, int brk, ASS_Image **tail)
336 // Inverse clipping in use?
337 if (render_priv->state.clip_mode)
338 return render_glyph_i(render_priv, bm, dst_x, dst_y, color, color2,
339 brk, tail);
341 // brk is relative to dst_x
342 // color = color left of brk
343 // color2 = color right of brk
344 int b_x0, b_y0, b_x1, b_y1; // visible part of the bitmap
345 int clip_x0, clip_y0, clip_x1, clip_y1;
346 int tmp;
347 ASS_Image *img;
349 dst_x += bm->left;
350 dst_y += bm->top;
351 brk -= bm->left;
353 // clipping
354 clip_x0 = FFMINMAX(render_priv->state.clip_x0, 0, render_priv->width);
355 clip_y0 = FFMINMAX(render_priv->state.clip_y0, 0, render_priv->height);
356 clip_x1 = FFMINMAX(render_priv->state.clip_x1, 0, render_priv->width);
357 clip_y1 = FFMINMAX(render_priv->state.clip_y1, 0, render_priv->height);
358 b_x0 = 0;
359 b_y0 = 0;
360 b_x1 = bm->w;
361 b_y1 = bm->h;
363 tmp = dst_x - clip_x0;
364 if (tmp < 0) {
365 ass_msg(render_priv->library, MSGL_DBG2, "clip left");
366 b_x0 = -tmp;
368 tmp = dst_y - clip_y0;
369 if (tmp < 0) {
370 ass_msg(render_priv->library, MSGL_DBG2, "clip top");
371 b_y0 = -tmp;
373 tmp = clip_x1 - dst_x - bm->w;
374 if (tmp < 0) {
375 ass_msg(render_priv->library, MSGL_DBG2, "clip right");
376 b_x1 = bm->w + tmp;
378 tmp = clip_y1 - dst_y - bm->h;
379 if (tmp < 0) {
380 ass_msg(render_priv->library, MSGL_DBG2, "clip bottom");
381 b_y1 = bm->h + tmp;
384 if ((b_y0 >= b_y1) || (b_x0 >= b_x1))
385 return tail;
387 if (brk > b_x0) { // draw left part
388 if (brk > b_x1)
389 brk = b_x1;
390 img = my_draw_bitmap(bm->buffer + bm->stride * b_y0 + b_x0,
391 brk - b_x0, b_y1 - b_y0, bm->stride,
392 dst_x + b_x0, dst_y + b_y0, color);
393 if (!img) return tail;
394 *tail = img;
395 tail = &img->next;
397 if (brk < b_x1) { // draw right part
398 if (brk < b_x0)
399 brk = b_x0;
400 img = my_draw_bitmap(bm->buffer + bm->stride * b_y0 + brk,
401 b_x1 - brk, b_y1 - b_y0, bm->stride,
402 dst_x + brk, dst_y + b_y0, color2);
403 if (!img) return tail;
404 *tail = img;
405 tail = &img->next;
407 return tail;
411 * \brief Replace the bitmap buffer in ASS_Image with a copy
412 * \param img ASS_Image to operate on
413 * \return pointer to old bitmap buffer
415 static unsigned char *clone_bitmap_buffer(ASS_Image *img)
417 unsigned char *old_bitmap = img->bitmap;
418 int size = img->stride * (img->h - 1) + img->w;
419 img->bitmap = malloc(size);
420 memcpy(img->bitmap, old_bitmap, size);
421 return old_bitmap;
425 * \brief Calculate overlapping area of two consecutive bitmaps and in case they
426 * overlap, blend them together
427 * Mainly useful for translucent glyphs and especially borders, to avoid the
428 * luminance adding up where they overlap (which looks ugly)
430 static void
431 render_overlap(ASS_Renderer *render_priv, ASS_Image **last_tail,
432 ASS_Image **tail)
434 int left, top, bottom, right;
435 int old_left, old_top, w, h, cur_left, cur_top;
436 int x, y, opos, cpos;
437 char m;
438 CompositeHashKey hk;
439 CompositeHashValue *hv;
440 CompositeHashValue chv;
441 int ax = (*last_tail)->dst_x;
442 int ay = (*last_tail)->dst_y;
443 int aw = (*last_tail)->w;
444 int as = (*last_tail)->stride;
445 int ah = (*last_tail)->h;
446 int bx = (*tail)->dst_x;
447 int by = (*tail)->dst_y;
448 int bw = (*tail)->w;
449 int bs = (*tail)->stride;
450 int bh = (*tail)->h;
451 unsigned char *a;
452 unsigned char *b;
454 if ((*last_tail)->bitmap == (*tail)->bitmap)
455 return;
457 if ((*last_tail)->color != (*tail)->color)
458 return;
460 // Calculate overlap coordinates
461 left = (ax > bx) ? ax : bx;
462 top = (ay > by) ? ay : by;
463 right = ((ax + aw) < (bx + bw)) ? (ax + aw) : (bx + bw);
464 bottom = ((ay + ah) < (by + bh)) ? (ay + ah) : (by + bh);
465 if ((right <= left) || (bottom <= top))
466 return;
467 old_left = left - ax;
468 old_top = top - ay;
469 w = right - left;
470 h = bottom - top;
471 cur_left = left - bx;
472 cur_top = top - by;
474 // Query cache
475 hk.a = (*last_tail)->bitmap;
476 hk.b = (*tail)->bitmap;
477 hk.aw = aw;
478 hk.ah = ah;
479 hk.bw = bw;
480 hk.bh = bh;
481 hk.ax = ax;
482 hk.ay = ay;
483 hk.bx = bx;
484 hk.by = by;
485 hk.as = as;
486 hk.bs = bs;
487 hv = ass_cache_get(render_priv->cache.composite_cache, &hk);
488 if (hv) {
489 (*last_tail)->bitmap = hv->a;
490 (*tail)->bitmap = hv->b;
491 return;
493 // Allocate new bitmaps and copy over data
494 a = clone_bitmap_buffer(*last_tail);
495 b = clone_bitmap_buffer(*tail);
497 // Blend overlapping area
498 for (y = 0; y < h; y++)
499 for (x = 0; x < w; x++) {
500 opos = (old_top + y) * (as) + (old_left + x);
501 cpos = (cur_top + y) * (bs) + (cur_left + x);
502 m = FFMIN(a[opos] + b[cpos], 0xff);
503 (*last_tail)->bitmap[opos] = 0;
504 (*tail)->bitmap[cpos] = m;
507 // Insert bitmaps into the cache
508 chv.a = (*last_tail)->bitmap;
509 chv.b = (*tail)->bitmap;
510 ass_cache_put(render_priv->cache.composite_cache, &hk, &chv);
513 static void free_list_add(ASS_Renderer *render_priv, void *object)
515 if (!render_priv->free_head) {
516 render_priv->free_head = calloc(1, sizeof(FreeList));
517 render_priv->free_head->object = object;
518 render_priv->free_tail = render_priv->free_head;
519 } else {
520 FreeList *l = calloc(1, sizeof(FreeList));
521 l->object = object;
522 render_priv->free_tail->next = l;
523 render_priv->free_tail = render_priv->free_tail->next;
528 * Iterate through a list of bitmaps and blend with clip vector, if
529 * applicable. The blended bitmaps are added to a free list which is freed
530 * at the start of a new frame.
532 static void blend_vector_clip(ASS_Renderer *render_priv,
533 ASS_Image *head)
535 FT_Outline *outline;
536 Bitmap *clip_bm = NULL;
537 ASS_Image *cur;
538 ASS_Drawing *drawing = render_priv->state.clip_drawing;
539 BitmapHashKey key;
540 BitmapHashValue *val;
541 int error;
543 if (!drawing)
544 return;
546 // Try to get mask from cache
547 memset(&key, 0, sizeof(key));
548 key.type = BITMAP_CLIP;
549 key.u.clip.text = strdup(drawing->text);
550 val = ass_cache_get(render_priv->cache.bitmap_cache, &key);
552 if (val) {
553 clip_bm = val->bm;
554 } else {
555 BitmapHashValue v;
557 // Not found in cache, parse and rasterize it
558 outline = ass_drawing_parse(drawing, 1);
559 if (!outline) {
560 ass_msg(render_priv->library, MSGL_WARN,
561 "Clip vector parsing failed. Skipping.");
562 goto blend_vector_error;
565 // We need to translate the clip according to screen borders
566 if (render_priv->settings.left_margin != 0 ||
567 render_priv->settings.top_margin != 0) {
568 FT_Vector trans = {
569 .x = int_to_d6(render_priv->settings.left_margin),
570 .y = -int_to_d6(render_priv->settings.top_margin),
572 FT_Outline_Translate(outline, trans.x, trans.y);
575 ass_msg(render_priv->library, MSGL_DBG2,
576 "Parsed vector clip: scales (%f, %f) string [%s]\n",
577 drawing->scale_x, drawing->scale_y, drawing->text);
579 clip_bm = outline_to_bitmap(render_priv->library,
580 render_priv->ftlibrary, outline, 0);
581 if (clip_bm == NULL) {
582 ass_msg(render_priv->library, MSGL_WARN,
583 "Clip vector rasterization failed: %d. Skipping.", error);
586 // Add to cache
587 memset(&v, 0, sizeof(v));
588 v.bm = clip_bm;
589 ass_cache_put(render_priv->cache.bitmap_cache, &key, &v);
591 blend_vector_error:
593 if (!clip_bm) goto blend_vector_exit;
595 // Iterate through bitmaps and blend/clip them
596 for (cur = head; cur; cur = cur->next) {
597 int left, top, right, bottom, apos, bpos, y, x, w, h;
598 int ax, ay, aw, ah, as;
599 int bx, by, bw, bh, bs;
600 int aleft, atop, bleft, btop;
601 unsigned char *abuffer, *bbuffer, *nbuffer;
603 abuffer = cur->bitmap;
604 bbuffer = clip_bm->buffer;
605 ax = cur->dst_x;
606 ay = cur->dst_y;
607 aw = cur->w;
608 ah = cur->h;
609 as = cur->stride;
610 bx = clip_bm->left;
611 by = clip_bm->top;
612 bw = clip_bm->w;
613 bh = clip_bm->h;
614 bs = clip_bm->w; // XXX: add real stride support
616 // Calculate overlap coordinates
617 left = (ax > bx) ? ax : bx;
618 top = (ay > by) ? ay : by;
619 right = ((ax + aw) < (bx + bw)) ? (ax + aw) : (bx + bw);
620 bottom = ((ay + ah) < (by + bh)) ? (ay + ah) : (by + bh);
621 aleft = left - ax;
622 atop = top - ay;
623 w = right - left;
624 h = bottom - top;
625 bleft = left - bx;
626 btop = top - by;
628 if (render_priv->state.clip_drawing_mode) {
629 // Inverse clip
630 if (ax + aw < bx || ay + ah < by || ax > bx + bw ||
631 ay > by + bh) {
632 continue;
635 // Allocate new buffer and add to free list
636 nbuffer = malloc(as * ah);
637 if (!nbuffer) goto blend_vector_exit;
638 free_list_add(render_priv, nbuffer);
640 // Blend together
641 memcpy(nbuffer, abuffer, as * (ah - 1) + aw);
642 for (y = 0; y < h; y++)
643 for (x = 0; x < w; x++) {
644 apos = (atop + y) * as + aleft + x;
645 bpos = (btop + y) * bs + bleft + x;
646 nbuffer[apos] = FFMAX(0, abuffer[apos] - bbuffer[bpos]);
648 } else {
649 // Regular clip
650 if (ax + aw < bx || ay + ah < by || ax > bx + bw ||
651 ay > by + bh) {
652 cur->w = cur->h = 0;
653 continue;
656 // Allocate new buffer and add to free list
657 nbuffer = calloc(as, ah);
658 if (!nbuffer) goto blend_vector_exit;
659 free_list_add(render_priv, nbuffer);
661 // Blend together
662 for (y = 0; y < h; y++)
663 for (x = 0; x < w; x++) {
664 apos = (atop + y) * as + aleft + x;
665 bpos = (btop + y) * bs + bleft + x;
666 nbuffer[apos] = (abuffer[apos] * bbuffer[bpos] + 255) >> 8;
669 cur->bitmap = nbuffer;
672 blend_vector_exit:
673 ass_drawing_free(render_priv->state.clip_drawing);
674 render_priv->state.clip_drawing = 0;
678 * \brief Convert TextInfo struct to ASS_Image list
679 * Splits glyphs in halves when needed (for \kf karaoke).
681 static ASS_Image *render_text(ASS_Renderer *render_priv, int dst_x, int dst_y)
683 int pen_x, pen_y;
684 int i;
685 Bitmap *bm;
686 ASS_Image *head;
687 ASS_Image **tail = &head;
688 ASS_Image **last_tail = 0;
689 ASS_Image **here_tail = 0;
690 TextInfo *text_info = &render_priv->text_info;
692 for (i = 0; i < text_info->length; ++i) {
693 GlyphInfo *info = text_info->glyphs + i;
694 if ((info->symbol == 0) || (info->symbol == '\n') || !info->bm_s
695 || (info->shadow_x == 0 && info->shadow_y == 0) || info->skip)
696 continue;
698 pen_x =
699 dst_x + (info->pos.x >> 6) +
700 (int) (info->shadow_x * render_priv->border_scale);
701 pen_y =
702 dst_y + (info->pos.y >> 6) +
703 (int) (info->shadow_y * render_priv->border_scale);
704 bm = info->bm_s;
706 here_tail = tail;
707 tail =
708 render_glyph(render_priv, bm, pen_x, pen_y, info->c[3], 0,
709 1000000, tail);
710 if (last_tail && tail != here_tail && ((info->c[3] & 0xff) > 0))
711 render_overlap(render_priv, last_tail, here_tail);
713 last_tail = here_tail;
716 last_tail = 0;
717 for (i = 0; i < text_info->length; ++i) {
718 GlyphInfo *info = text_info->glyphs + i;
719 if ((info->symbol == 0) || (info->symbol == '\n') || !info->bm_o
720 || info->skip)
721 continue;
723 pen_x = dst_x + (info->pos.x >> 6);
724 pen_y = dst_y + (info->pos.y >> 6);
725 bm = info->bm_o;
727 if ((info->effect_type == EF_KARAOKE_KO)
728 && (info->effect_timing <= (info->bbox.xMax >> 6))) {
729 // do nothing
730 } else {
731 here_tail = tail;
732 tail =
733 render_glyph(render_priv, bm, pen_x, pen_y, info->c[2],
734 0, 1000000, tail);
735 if (last_tail && tail != here_tail && ((info->c[2] & 0xff) > 0))
736 render_overlap(render_priv, last_tail, here_tail);
738 last_tail = here_tail;
742 for (i = 0; i < text_info->length; ++i) {
743 GlyphInfo *info = text_info->glyphs + i;
744 if ((info->symbol == 0) || (info->symbol == '\n') || !info->bm
745 || info->skip)
746 continue;
748 pen_x = dst_x + (info->pos.x >> 6);
749 pen_y = dst_y + (info->pos.y >> 6);
750 bm = info->bm;
752 if ((info->effect_type == EF_KARAOKE)
753 || (info->effect_type == EF_KARAOKE_KO)) {
754 if (info->effect_timing > (info->bbox.xMax >> 6))
755 tail =
756 render_glyph(render_priv, bm, pen_x, pen_y,
757 info->c[0], 0, 1000000, tail);
758 else
759 tail =
760 render_glyph(render_priv, bm, pen_x, pen_y,
761 info->c[1], 0, 1000000, tail);
762 } else if (info->effect_type == EF_KARAOKE_KF) {
763 tail =
764 render_glyph(render_priv, bm, pen_x, pen_y, info->c[0],
765 info->c[1], info->effect_timing, tail);
766 } else
767 tail =
768 render_glyph(render_priv, bm, pen_x, pen_y, info->c[0],
769 0, 1000000, tail);
772 *tail = 0;
773 blend_vector_clip(render_priv, head);
775 return head;
778 static void compute_string_bbox(TextInfo *info, DBBox *bbox)
780 int i;
782 if (info->length > 0) {
783 bbox->xMin = 32000;
784 bbox->xMax = -32000;
785 bbox->yMin = -1 * info->lines[0].asc + d6_to_double(info->glyphs[0].pos.y);
786 bbox->yMax = info->height - info->lines[0].asc +
787 d6_to_double(info->glyphs[0].pos.y);
789 for (i = 0; i < info->length; ++i) {
790 if (info->glyphs[i].skip) continue;
791 double s = d6_to_double(info->glyphs[i].pos.x);
792 double e = s + d6_to_double(info->glyphs[i].advance.x);
793 bbox->xMin = FFMIN(bbox->xMin, s);
794 bbox->xMax = FFMAX(bbox->xMax, e);
796 } else
797 bbox->xMin = bbox->xMax = bbox->yMin = bbox->yMax = 0.;
801 * \brief partially reset render_context to style values
802 * Works like {\r}: resets some style overrides
804 void reset_render_context(ASS_Renderer *render_priv)
806 render_priv->state.c[0] = render_priv->state.style->PrimaryColour;
807 render_priv->state.c[1] = render_priv->state.style->SecondaryColour;
808 render_priv->state.c[2] = render_priv->state.style->OutlineColour;
809 render_priv->state.c[3] = render_priv->state.style->BackColour;
810 render_priv->state.flags =
811 (render_priv->state.style->Underline ? DECO_UNDERLINE : 0) |
812 (render_priv->state.style->StrikeOut ? DECO_STRIKETHROUGH : 0);
813 render_priv->state.font_size = render_priv->state.style->FontSize;
815 free(render_priv->state.family);
816 render_priv->state.family = NULL;
817 render_priv->state.family = strdup(render_priv->state.style->FontName);
818 render_priv->state.treat_family_as_pattern =
819 render_priv->state.style->treat_fontname_as_pattern;
820 render_priv->state.bold = render_priv->state.style->Bold;
821 render_priv->state.italic = render_priv->state.style->Italic;
822 update_font(render_priv);
824 change_border(render_priv, -1., -1.);
825 render_priv->state.scale_x = render_priv->state.style->ScaleX;
826 render_priv->state.scale_y = render_priv->state.style->ScaleY;
827 render_priv->state.hspacing = render_priv->state.style->Spacing;
828 render_priv->state.be = 0;
829 render_priv->state.blur = 0.0;
830 render_priv->state.shadow_x = render_priv->state.style->Shadow;
831 render_priv->state.shadow_y = render_priv->state.style->Shadow;
832 render_priv->state.frx = render_priv->state.fry = 0.;
833 render_priv->state.frz = M_PI * render_priv->state.style->Angle / 180.;
834 render_priv->state.fax = render_priv->state.fay = 0.;
835 render_priv->state.wrap_style = render_priv->track->WrapStyle;
839 * \brief Start new event. Reset render_priv->state.
841 static void
842 init_render_context(ASS_Renderer *render_priv, ASS_Event *event)
844 render_priv->state.event = event;
845 render_priv->state.style = render_priv->track->styles + event->Style;
846 render_priv->state.parsed_tags = 0;
848 reset_render_context(render_priv);
850 render_priv->state.evt_type = EVENT_NORMAL;
851 render_priv->state.alignment = render_priv->state.style->Alignment;
852 render_priv->state.pos_x = 0;
853 render_priv->state.pos_y = 0;
854 render_priv->state.org_x = 0;
855 render_priv->state.org_y = 0;
856 render_priv->state.have_origin = 0;
857 render_priv->state.clip_x0 = 0;
858 render_priv->state.clip_y0 = 0;
859 render_priv->state.clip_x1 = render_priv->track->PlayResX;
860 render_priv->state.clip_y1 = render_priv->track->PlayResY;
861 render_priv->state.clip_mode = 0;
862 render_priv->state.detect_collisions = 1;
863 render_priv->state.fade = 0;
864 render_priv->state.drawing_mode = 0;
865 render_priv->state.effect_type = EF_NONE;
866 render_priv->state.effect_timing = 0;
867 render_priv->state.effect_skip_timing = 0;
868 ass_drawing_free(render_priv->state.drawing);
869 render_priv->state.drawing = ass_drawing_new(render_priv->library,
870 render_priv->ftlibrary);
872 apply_transition_effects(render_priv, event);
875 static void free_render_context(ASS_Renderer *render_priv)
877 free(render_priv->state.family);
878 ass_drawing_free(render_priv->state.drawing);
880 render_priv->state.family = NULL;
881 render_priv->state.drawing = NULL;
885 * Replace the outline of a glyph by a contour which makes up a simple
886 * opaque rectangle.
888 static void draw_opaque_box(ASS_Renderer *render_priv, uint32_t ch,
889 FT_Outline *ol, FT_Vector advance, int sx, int sy)
891 int asc = 0, desc = 0;
892 int i;
893 int adv = advance.x;
894 double scale_y = render_priv->state.scale_y;
895 double scale_x = render_priv->state.scale_x;
897 // to avoid gaps
898 sx = FFMAX(64, sx);
899 sy = FFMAX(64, sy);
901 if (ch == -1) {
902 asc = render_priv->state.drawing->asc;
903 desc = render_priv->state.drawing->desc;
904 } else {
905 ass_font_get_asc_desc(render_priv->state.font, ch, &asc, &desc);
906 asc *= scale_y;
907 desc *= scale_y;
910 // Emulate the WTFish behavior of VSFilter, i.e. double-scale
911 // the sizes of the opaque box.
912 adv += double_to_d6(render_priv->state.hspacing * render_priv->font_scale
913 * scale_x);
914 adv *= scale_x;
915 sx *= scale_x;
916 sy *= scale_y;
917 desc *= scale_y;
918 desc += asc * (scale_y - 1.0);
920 FT_Vector points[4] = {
921 { .x = -sx, .y = asc + sy },
922 { .x = adv + sx, .y = asc + sy },
923 { .x = adv + sx, .y = -desc - sy },
924 { .x = -sx, .y = -desc - sy },
927 FT_Outline_Done(render_priv->ftlibrary, ol);
928 FT_Outline_New(render_priv->ftlibrary, 4, 1, ol);
930 ol->n_points = ol->n_contours = 0;
931 for (i = 0; i < 4; i++) {
932 ol->points[ol->n_points] = points[i];
933 ol->tags[ol->n_points++] = 1;
935 ol->contours[ol->n_contours++] = ol->n_points - 1;
939 * Stroke an outline glyph in x/y direction. Applies various fixups to get
940 * around limitations of the FreeType stroker.
942 static void stroke_outline(ASS_Renderer *render_priv, FT_Outline *outline,
943 int sx, int sy)
945 if (sx <= 0 && sy <= 0)
946 return;
948 fix_freetype_stroker(outline, sx, sy);
950 // Borders are equal; use the regular stroker
951 if (sx == sy && render_priv->state.stroker) {
952 int error;
953 unsigned n_points, n_contours;
955 FT_StrokerBorder border = FT_Outline_GetOutsideBorder(outline);
956 error = FT_Stroker_ParseOutline(render_priv->state.stroker, outline, 0);
957 if (error) {
958 ass_msg(render_priv->library, MSGL_WARN,
959 "FT_Stroker_ParseOutline failed, error: %d", error);
961 error = FT_Stroker_GetBorderCounts(render_priv->state.stroker, border,
962 &n_points, &n_contours);
963 if (error) {
964 ass_msg(render_priv->library, MSGL_WARN,
965 "FT_Stroker_GetBorderCounts failed, error: %d", error);
967 FT_Outline_Done(render_priv->ftlibrary, outline);
968 FT_Outline_New(render_priv->ftlibrary, n_points, n_contours, outline);
969 outline->n_points = outline->n_contours = 0;
970 FT_Stroker_ExportBorder(render_priv->state.stroker, border, outline);
972 // "Stroke" with the outline emboldener in two passes.
973 // The outlines look uglier, but the emboldening never adds any points
974 } else {
975 int i;
976 FT_Outline nol;
978 FT_Outline_New(render_priv->ftlibrary, outline->n_points,
979 outline->n_contours, &nol);
980 FT_Outline_Copy(outline, &nol);
982 FT_Outline_Embolden(outline, sx * 2);
983 FT_Outline_Translate(outline, -sx, -sx);
984 FT_Outline_Embolden(&nol, sy * 2);
985 FT_Outline_Translate(&nol, -sy, -sy);
987 for (i = 0; i < outline->n_points; i++)
988 outline->points[i].y = nol.points[i].y;
990 FT_Outline_Done(render_priv->ftlibrary, &nol);
995 * \brief Prepare glyph hash
997 static void
998 fill_glyph_hash(ASS_Renderer *priv, OutlineHashKey *outline_key,
999 ASS_Drawing *drawing, uint32_t ch)
1001 if (drawing->hash) {
1002 DrawingHashKey *key = &outline_key->u.drawing;
1003 outline_key->type = OUTLINE_DRAWING;
1004 key->scale_x = double_to_d16(priv->state.scale_x);
1005 key->scale_y = double_to_d16(priv->state.scale_y);
1006 key->outline.x = double_to_d16(priv->state.border_x);
1007 key->outline.y = double_to_d16(priv->state.border_y);
1008 key->border_style = priv->state.style->BorderStyle;
1009 key->hash = drawing->hash;
1010 key->text = strdup(drawing->text);
1011 key->pbo = drawing->pbo;
1012 key->scale = drawing->scale;
1013 } else {
1014 GlyphHashKey *key = &outline_key->u.glyph;
1015 outline_key->type = OUTLINE_GLYPH;
1016 key->font = priv->state.font;
1017 key->size = priv->state.font_size;
1018 key->ch = ch;
1019 key->bold = priv->state.bold;
1020 key->italic = priv->state.italic;
1021 key->scale_x = double_to_d16(priv->state.scale_x);
1022 key->scale_y = double_to_d16(priv->state.scale_y);
1023 key->outline.x = double_to_d16(priv->state.border_x);
1024 key->outline.y = double_to_d16(priv->state.border_y);
1025 key->flags = priv->state.flags;
1026 key->border_style = priv->state.style->BorderStyle;
1031 * \brief Get normal and outline (border) glyphs
1032 * \param symbol ucs4 char
1033 * \param info out: struct filled with extracted data
1034 * Tries to get both glyphs from cache.
1035 * If they can't be found, gets a glyph from font face, generates outline with FT_Stroker,
1036 * and add them to cache.
1037 * The glyphs are returned in info->glyph and info->outline_glyph
1039 static void
1040 get_outline_glyph(ASS_Renderer *render_priv, int symbol, GlyphInfo *info,
1041 ASS_Drawing *drawing)
1043 OutlineHashValue *val;
1044 OutlineHashKey key;
1046 memset(&key, 0, sizeof(key));
1047 memset(info, 0, sizeof(GlyphInfo));
1049 info->italic = render_priv->state.italic;
1050 fill_glyph_hash(render_priv, &key, drawing, symbol);
1051 val = ass_cache_get(render_priv->cache.outline_cache, &key);
1052 if (val) {
1053 info->hash_key.u.outline.outline = val;
1054 info->outline = val->outline;
1055 info->border = val->border;
1056 info->bbox = val->bbox_scaled;
1057 info->advance.x = val->advance.x;
1058 info->advance.y = val->advance.y;
1059 if (drawing->hash) {
1060 drawing->asc = val->asc;
1061 drawing->desc = val->desc;
1063 } else {
1064 OutlineHashValue v;
1065 if (drawing->hash) {
1066 if(!ass_drawing_parse(drawing, 0))
1067 return;
1068 outline_copy(render_priv->ftlibrary, &drawing->outline,
1069 &info->outline);
1070 info->advance.x = drawing->advance.x;
1071 info->advance.y = drawing->advance.y;
1072 } else {
1073 FT_Glyph glyph =
1074 ass_font_get_glyph(render_priv->fontconfig_priv,
1075 render_priv->state.font, symbol,
1076 render_priv->settings.hinting,
1077 render_priv->state.flags);
1078 if (glyph != NULL) {
1079 outline_copy(render_priv->ftlibrary,
1080 &((FT_OutlineGlyph)glyph)->outline, &info->outline);
1081 info->advance.x = d16_to_d6(glyph->advance.x);
1082 info->advance.y = d16_to_d6(glyph->advance.y);
1083 FT_Done_Glyph(glyph);
1086 if (!info->outline)
1087 return;
1089 FT_Outline_Get_CBox(info->outline, &info->bbox);
1091 if (render_priv->state.style->BorderStyle == 3 &&
1092 (render_priv->state.border_x > 0||
1093 render_priv->state.border_y > 0)) {
1094 outline_copy(render_priv->ftlibrary, info->outline, &info->border);
1095 draw_opaque_box(render_priv, symbol, info->border,
1096 info->advance,
1097 double_to_d6(render_priv->state.border_x *
1098 render_priv->border_scale),
1099 double_to_d6(render_priv->state.border_y *
1100 render_priv->border_scale));
1101 } else if ((render_priv->state.border_x > 0
1102 || render_priv->state.border_y > 0)
1103 && double_to_d6(render_priv->state.scale_x)
1104 && double_to_d6(render_priv->state.scale_y)) {
1106 outline_copy(render_priv->ftlibrary, info->outline, &info->border);
1107 stroke_outline(render_priv, info->border,
1108 double_to_d6(render_priv->state.border_x *
1109 render_priv->border_scale),
1110 double_to_d6(render_priv->state.border_y *
1111 render_priv->border_scale));
1114 memset(&v, 0, sizeof(v));
1115 v.lib = render_priv->ftlibrary;
1116 v.outline = info->outline;
1117 v.border = info->border;
1118 v.advance = info->advance;
1119 v.bbox_scaled = info->bbox;
1120 if (drawing->hash) {
1121 v.asc = drawing->asc;
1122 v.desc = drawing->desc;
1124 info->hash_key.u.outline.outline =
1125 ass_cache_put(render_priv->cache.outline_cache, &key, &v);
1130 * \brief Apply transformation to outline points of a glyph
1131 * Applies rotations given by frx, fry and frz and projects the points back
1132 * onto the screen plane.
1134 static void
1135 transform_3d_points(FT_Vector shift, FT_Outline *outline, double frx, double fry,
1136 double frz, double fax, double fay, double scale,
1137 int yshift)
1139 double sx = sin(frx);
1140 double sy = sin(fry);
1141 double sz = sin(frz);
1142 double cx = cos(frx);
1143 double cy = cos(fry);
1144 double cz = cos(frz);
1145 FT_Vector *p = outline->points;
1146 double x, y, z, xx, yy, zz;
1147 int i, dist;
1149 dist = 20000 * scale;
1150 for (i = 0; i < outline->n_points; i++) {
1151 x = (double) p[i].x + shift.x + (fax * (yshift - p[i].y));
1152 y = (double) p[i].y + shift.y + (-fay * p[i].x);
1153 z = 0.;
1155 xx = x * cz + y * sz;
1156 yy = -(x * sz - y * cz);
1157 zz = z;
1159 x = xx;
1160 y = yy * cx + zz * sx;
1161 z = yy * sx - zz * cx;
1163 xx = x * cy + z * sy;
1164 yy = y;
1165 zz = x * sy - z * cy;
1167 zz = FFMAX(zz, 1000 - dist);
1169 x = (xx * dist) / (zz + dist);
1170 y = (yy * dist) / (zz + dist);
1171 p[i].x = x - shift.x + 0.5;
1172 p[i].y = y - shift.y + 0.5;
1177 * \brief Apply 3d transformation to several objects
1178 * \param shift FreeType vector
1179 * \param glyph FreeType glyph
1180 * \param glyph2 FreeType glyph
1181 * \param frx x-axis rotation angle
1182 * \param fry y-axis rotation angle
1183 * \param frz z-axis rotation angle
1184 * Rotates both glyphs by frx, fry and frz. Shift vector is added before rotation and subtracted after it.
1186 static void
1187 transform_3d(FT_Vector shift, FT_Outline *outline, FT_Outline *border,
1188 double frx, double fry, double frz, double fax, double fay,
1189 double scale, int yshift)
1191 frx = -frx;
1192 frz = -frz;
1193 if (frx != 0. || fry != 0. || frz != 0. || fax != 0. || fay != 0.) {
1194 if (outline)
1195 transform_3d_points(shift, outline, frx, fry, frz,
1196 fax, fay, scale, yshift);
1198 if (border)
1199 transform_3d_points(shift, border, frx, fry, frz,
1200 fax, fay, scale, yshift);
1205 * \brief Get bitmaps for a glyph
1206 * \param info glyph info
1207 * Tries to get glyph bitmaps from bitmap cache.
1208 * If they can't be found, they are generated by rotating and rendering the glyph.
1209 * After that, bitmaps are added to the cache.
1210 * They are returned in info->bm (glyph), info->bm_o (outline) and info->bm_s (shadow).
1212 static void
1213 get_bitmap_glyph(ASS_Renderer *render_priv, GlyphInfo *info)
1215 BitmapHashValue *val;
1216 OutlineBitmapHashKey *key = &info->hash_key.u.outline;
1218 val = ass_cache_get(render_priv->cache.bitmap_cache, key);
1220 if (val) {
1221 info->bm = val->bm;
1222 info->bm_o = val->bm_o;
1223 info->bm_s = val->bm_s;
1224 } else {
1225 FT_Vector shift;
1226 BitmapHashValue hash_val;
1227 int error;
1228 double fax_scaled, fay_scaled;
1229 info->bm = info->bm_o = info->bm_s = 0;
1230 if (info->outline && info->symbol != '\n' && info->symbol != 0
1231 && !info->skip) {
1232 FT_Outline *outline, *border;
1233 double scale_x = render_priv->font_scale_x;
1235 outline_copy(render_priv->ftlibrary, info->outline, &outline);
1236 outline_copy(render_priv->ftlibrary, info->border, &border);
1237 // calculating rotation shift vector (from rotation origin to the glyph basepoint)
1238 shift.x = key->shift_x;
1239 shift.y = key->shift_y;
1240 fax_scaled = info->fax *
1241 render_priv->state.scale_x;
1242 fay_scaled = info->fay * render_priv->state.scale_y;
1243 // apply rotation
1244 transform_3d(shift, outline, border,
1245 info->frx, info->fry, info->frz, fax_scaled,
1246 fay_scaled, render_priv->font_scale, info->asc);
1248 // PAR correction scaling
1249 FT_Matrix m = { double_to_d16(scale_x), 0,
1250 0, double_to_d16(1.0) };
1252 // subpixel shift
1253 if (outline) {
1254 if (scale_x != 1.0)
1255 FT_Outline_Transform(outline, &m);
1256 FT_Outline_Translate(outline, key->advance.x, -key->advance.y);
1258 if (border) {
1259 if (scale_x != 1.0)
1260 FT_Outline_Transform(border, &m);
1261 FT_Outline_Translate(border, key->advance.x, -key->advance.y);
1263 // render glyph
1264 error = outline_to_bitmap3(render_priv->library,
1265 render_priv->synth_priv,
1266 render_priv->ftlibrary,
1267 outline, border,
1268 &info->bm, &info->bm_o,
1269 &info->bm_s, info->be,
1270 info->blur * render_priv->border_scale,
1271 key->shadow_offset,
1272 render_priv->state.style->BorderStyle);
1273 if (error)
1274 info->symbol = 0;
1276 // add bitmaps to cache
1277 hash_val.bm_o = info->bm_o;
1278 hash_val.bm = info->bm;
1279 hash_val.bm_s = info->bm_s;
1280 ass_cache_put(render_priv->cache.bitmap_cache, key, &hash_val);
1282 outline_free(render_priv->ftlibrary, outline);
1283 outline_free(render_priv->ftlibrary, border);
1287 // VSFilter compatibility: invisible fill and no border?
1288 // In this case no shadow is supposed to be rendered.
1289 if (!info->border && (info->c[0] >> 24) == 0xFF)
1290 info->bm_s = 0;
1294 * This function goes through text_info and calculates text parameters.
1295 * The following text_info fields are filled:
1296 * height
1297 * lines[].height
1298 * lines[].asc
1299 * lines[].desc
1301 static void measure_text(ASS_Renderer *render_priv)
1303 TextInfo *text_info = &render_priv->text_info;
1304 int cur_line = 0;
1305 double max_asc = 0., max_desc = 0.;
1306 GlyphInfo *last = NULL;
1307 int i;
1308 int empty_line = 1;
1309 text_info->height = 0.;
1310 for (i = 0; i < text_info->length + 1; ++i) {
1311 if ((i == text_info->length) || text_info->glyphs[i].linebreak) {
1312 if (empty_line && cur_line > 0 && last && i < text_info->length) {
1313 max_asc = d6_to_double(last->asc) / 2.0;
1314 max_desc = d6_to_double(last->desc) / 2.0;
1316 text_info->lines[cur_line].asc = max_asc;
1317 text_info->lines[cur_line].desc = max_desc;
1318 text_info->height += max_asc + max_desc;
1319 cur_line++;
1320 max_asc = max_desc = 0.;
1321 empty_line = 1;
1322 } else
1323 empty_line = 0;
1324 if (i < text_info->length) {
1325 GlyphInfo *cur = text_info->glyphs + i;
1326 if (d6_to_double(cur->asc) > max_asc)
1327 max_asc = d6_to_double(cur->asc);
1328 if (d6_to_double(cur->desc) > max_desc)
1329 max_desc = d6_to_double(cur->desc);
1330 if (cur->symbol != '\n' && cur->symbol != 0)
1331 last = cur;
1334 text_info->height +=
1335 (text_info->n_lines -
1336 1) * render_priv->settings.line_spacing;
1340 * Mark extra whitespace for later removal.
1342 #define IS_WHITESPACE(x) ((x->symbol == ' ' || x->symbol == '\n') \
1343 && !x->linebreak)
1344 static void trim_whitespace(ASS_Renderer *render_priv)
1346 int i, j;
1347 GlyphInfo *cur;
1348 TextInfo *ti = &render_priv->text_info;
1350 // Mark trailing spaces
1351 i = ti->length - 1;
1352 cur = ti->glyphs + i;
1353 while (i && IS_WHITESPACE(cur)) {
1354 cur->skip++;
1355 cur = ti->glyphs + --i;
1358 // Mark leading whitespace
1359 i = 0;
1360 cur = ti->glyphs;
1361 while (i < ti->length && IS_WHITESPACE(cur)) {
1362 cur->skip++;
1363 cur = ti->glyphs + ++i;
1366 // Mark all extraneous whitespace inbetween
1367 for (i = 0; i < ti->length; ++i) {
1368 cur = ti->glyphs + i;
1369 if (cur->linebreak) {
1370 // Mark whitespace before
1371 j = i - 1;
1372 cur = ti->glyphs + j;
1373 while (j && IS_WHITESPACE(cur)) {
1374 cur->skip++;
1375 cur = ti->glyphs + --j;
1377 // A break itself can contain a whitespace, too
1378 cur = ti->glyphs + i;
1379 if (cur->symbol == ' ') {
1380 cur->skip++;
1381 // Mark whitespace after
1382 j = i + 1;
1383 cur = ti->glyphs + j;
1384 while (j < ti->length && IS_WHITESPACE(cur)) {
1385 cur->skip++;
1386 cur = ti->glyphs + ++j;
1388 i = j - 1;
1393 #undef IS_WHITESPACE
1396 * \brief rearrange text between lines
1397 * \param max_text_width maximal text line width in pixels
1398 * The algo is similar to the one in libvo/sub.c:
1399 * 1. Place text, wrapping it when current line is full
1400 * 2. Try moving words from the end of a line to the beginning of the next one while it reduces
1401 * the difference in lengths between this two lines.
1402 * The result may not be optimal, but usually is good enough.
1404 * FIXME: implement style 0 and 3 correctly
1406 static void
1407 wrap_lines_smart(ASS_Renderer *render_priv, double max_text_width)
1409 int i;
1410 GlyphInfo *cur, *s1, *e1, *s2, *s3, *w;
1411 int last_space;
1412 int break_type;
1413 int exit;
1414 double pen_shift_x;
1415 double pen_shift_y;
1416 int cur_line;
1417 TextInfo *text_info = &render_priv->text_info;
1419 last_space = -1;
1420 text_info->n_lines = 1;
1421 break_type = 0;
1422 s1 = text_info->glyphs; // current line start
1423 for (i = 0; i < text_info->length; ++i) {
1424 int break_at = -1;
1425 double s_offset, len;
1426 cur = text_info->glyphs + i;
1427 s_offset = d6_to_double(s1->bbox.xMin + s1->pos.x);
1428 len = d6_to_double(cur->bbox.xMax + cur->pos.x) - s_offset;
1430 if (cur->symbol == '\n') {
1431 break_type = 2;
1432 break_at = i;
1433 ass_msg(render_priv->library, MSGL_DBG2,
1434 "forced line break at %d", break_at);
1435 } else if (cur->symbol == ' ') {
1436 last_space = i;
1437 } else if (len >= max_text_width
1438 && (render_priv->state.wrap_style != 2)) {
1439 break_type = 1;
1440 break_at = last_space;
1441 if (break_at >= 0)
1442 ass_msg(render_priv->library, MSGL_DBG2, "line break at %d",
1443 break_at);
1446 if (break_at != -1) {
1447 // need to use one more line
1448 // marking break_at+1 as start of a new line
1449 int lead = break_at + 1; // the first symbol of the new line
1450 if (text_info->n_lines >= text_info->max_lines) {
1451 // Raise maximum number of lines
1452 text_info->max_lines *= 2;
1453 text_info->lines = realloc(text_info->lines,
1454 sizeof(LineInfo) *
1455 text_info->max_lines);
1457 if (lead < text_info->length)
1458 text_info->glyphs[lead].linebreak = break_type;
1459 last_space = -1;
1460 s1 = text_info->glyphs + lead;
1461 s_offset = d6_to_double(s1->bbox.xMin + s1->pos.x);
1462 text_info->n_lines++;
1465 #define DIFF(x,y) (((x) < (y)) ? (y - x) : (x - y))
1466 exit = 0;
1467 while (!exit && render_priv->state.wrap_style != 1) {
1468 exit = 1;
1469 w = s3 = text_info->glyphs;
1470 s1 = s2 = 0;
1471 for (i = 0; i <= text_info->length; ++i) {
1472 cur = text_info->glyphs + i;
1473 if ((i == text_info->length) || cur->linebreak) {
1474 s1 = s2;
1475 s2 = s3;
1476 s3 = cur;
1477 if (s1 && (s2->linebreak == 1)) { // have at least 2 lines, and linebreak is 'soft'
1478 double l1, l2, l1_new, l2_new;
1480 w = s2;
1481 do {
1482 --w;
1483 } while ((w > s1) && (w->symbol == ' '));
1484 while ((w > s1) && (w->symbol != ' ')) {
1485 --w;
1487 e1 = w;
1488 while ((e1 > s1) && (e1->symbol == ' ')) {
1489 --e1;
1491 if (w->symbol == ' ')
1492 ++w;
1494 l1 = d6_to_double(((s2 - 1)->bbox.xMax + (s2 - 1)->pos.x) -
1495 (s1->bbox.xMin + s1->pos.x));
1496 l2 = d6_to_double(((s3 - 1)->bbox.xMax + (s3 - 1)->pos.x) -
1497 (s2->bbox.xMin + s2->pos.x));
1498 l1_new = d6_to_double(
1499 (e1->bbox.xMax + e1->pos.x) -
1500 (s1->bbox.xMin + s1->pos.x));
1501 l2_new = d6_to_double(
1502 ((s3 - 1)->bbox.xMax + (s3 - 1)->pos.x) -
1503 (w->bbox.xMin + w->pos.x));
1505 if (DIFF(l1_new, l2_new) < DIFF(l1, l2)) {
1506 w->linebreak = 1;
1507 s2->linebreak = 0;
1508 exit = 0;
1512 if (i == text_info->length)
1513 break;
1517 assert(text_info->n_lines >= 1);
1518 #undef DIFF
1520 measure_text(render_priv);
1521 trim_whitespace(render_priv);
1523 pen_shift_x = 0.;
1524 pen_shift_y = 0.;
1525 cur_line = 1;
1527 i = 0;
1528 cur = text_info->glyphs + i;
1529 while (i < text_info->length && cur->skip)
1530 cur = text_info->glyphs + ++i;
1531 pen_shift_x = d6_to_double(-cur->pos.x);
1533 for (i = 0; i < text_info->length; ++i) {
1534 cur = text_info->glyphs + i;
1535 if (cur->linebreak) {
1536 while (i < text_info->length && cur->skip && cur->symbol != '\n')
1537 cur = text_info->glyphs + ++i;
1538 double height =
1539 text_info->lines[cur_line - 1].desc +
1540 text_info->lines[cur_line].asc;
1541 cur_line++;
1542 pen_shift_x = d6_to_double(-cur->pos.x);
1543 pen_shift_y += height + render_priv->settings.line_spacing;
1544 ass_msg(render_priv->library, MSGL_DBG2,
1545 "shifting from %d to %d by (%f, %f)", i,
1546 text_info->length - 1, pen_shift_x, pen_shift_y);
1548 cur->pos.x += double_to_d6(pen_shift_x);
1549 cur->pos.y += double_to_d6(pen_shift_y);
1554 * \brief Calculate base point for positioning and rotation
1555 * \param bbox text bbox
1556 * \param alignment alignment
1557 * \param bx, by out: base point coordinates
1559 static void get_base_point(DBBox *bbox, int alignment, double *bx, double *by)
1561 const int halign = alignment & 3;
1562 const int valign = alignment & 12;
1563 if (bx)
1564 switch (halign) {
1565 case HALIGN_LEFT:
1566 *bx = bbox->xMin;
1567 break;
1568 case HALIGN_CENTER:
1569 *bx = (bbox->xMax + bbox->xMin) / 2.0;
1570 break;
1571 case HALIGN_RIGHT:
1572 *bx = bbox->xMax;
1573 break;
1575 if (by)
1576 switch (valign) {
1577 case VALIGN_TOP:
1578 *by = bbox->yMin;
1579 break;
1580 case VALIGN_CENTER:
1581 *by = (bbox->yMax + bbox->yMin) / 2.0;
1582 break;
1583 case VALIGN_SUB:
1584 *by = bbox->yMax;
1585 break;
1590 * Prepare bitmap hash key of a glyph
1592 static void
1593 fill_bitmap_hash(ASS_Renderer *priv, OutlineBitmapHashKey *hash_key)
1595 hash_key->frx = rot_key(priv->state.frx);
1596 hash_key->fry = rot_key(priv->state.fry);
1597 hash_key->frz = rot_key(priv->state.frz);
1598 hash_key->fax = double_to_d16(priv->state.fax);
1599 hash_key->fay = double_to_d16(priv->state.fay);
1600 hash_key->be = priv->state.be;
1601 hash_key->blur = priv->state.blur;
1602 hash_key->shadow_offset.x = double_to_d6(
1603 priv->state.shadow_x * priv->border_scale -
1604 (int) (priv->state.shadow_x * priv->border_scale));
1605 hash_key->shadow_offset.y = double_to_d6(
1606 priv->state.shadow_y * priv->border_scale -
1607 (int) (priv->state.shadow_y * priv->border_scale));
1611 * \brief Main ass rendering function, glues everything together
1612 * \param event event to render
1613 * \param event_images struct containing resulting images, will also be initialized
1614 * Process event, appending resulting ASS_Image's to images_root.
1616 static int
1617 ass_render_event(ASS_Renderer *render_priv, ASS_Event *event,
1618 EventImages *event_images)
1620 char *p;
1621 FT_UInt previous;
1622 FT_UInt num_glyphs;
1623 FT_Vector pen;
1624 unsigned code;
1625 DBBox bbox;
1626 int i, j;
1627 int MarginL, MarginR, MarginV;
1628 int last_break;
1629 int alignment, halign, valign;
1630 int kern = render_priv->track->Kerning;
1631 double device_x = 0;
1632 double device_y = 0;
1633 TextInfo *text_info = &render_priv->text_info;
1634 GlyphInfo *glyphs = render_priv->text_info.glyphs;
1635 ASS_Drawing *drawing;
1637 if (event->Style >= render_priv->track->n_styles) {
1638 ass_msg(render_priv->library, MSGL_WARN, "No style found");
1639 return 1;
1641 if (!event->Text) {
1642 ass_msg(render_priv->library, MSGL_WARN, "Empty event");
1643 return 1;
1646 init_render_context(render_priv, event);
1648 drawing = render_priv->state.drawing;
1649 text_info->length = 0;
1650 pen.x = 0;
1651 pen.y = 0;
1652 previous = 0;
1653 num_glyphs = 0;
1654 p = event->Text;
1655 // Event parsing.
1656 while (1) {
1657 // get next char, executing style override
1658 // this affects render_context
1659 do {
1660 code = get_next_char(render_priv, &p);
1661 if (render_priv->state.drawing_mode && code)
1662 ass_drawing_add_char(drawing, (char) code);
1663 } while (code && render_priv->state.drawing_mode); // skip everything in drawing mode
1665 // Parse drawing
1666 if (drawing->i) {
1667 drawing->scale_x = render_priv->state.scale_x *
1668 render_priv->font_scale;
1669 drawing->scale_y = render_priv->state.scale_y *
1670 render_priv->font_scale;
1671 ass_drawing_hash(drawing);
1672 p--;
1673 code = -1;
1676 // face could have been changed in get_next_char
1677 if (!render_priv->state.font) {
1678 free_render_context(render_priv);
1679 return 1;
1682 if (code == 0)
1683 break;
1685 if (text_info->length >= text_info->max_glyphs) {
1686 // Raise maximum number of glyphs
1687 text_info->max_glyphs *= 2;
1688 text_info->glyphs = glyphs =
1689 realloc(text_info->glyphs,
1690 sizeof(GlyphInfo) * text_info->max_glyphs);
1693 // Add kerning to pen
1694 if (kern && previous && code && !drawing->hash) {
1695 FT_Vector delta;
1696 delta =
1697 ass_font_get_kerning(render_priv->state.font, previous,
1698 code);
1699 pen.x += delta.x * render_priv->state.scale_x;
1700 pen.y += delta.y * render_priv->state.scale_y;
1703 ass_font_set_transform(render_priv->state.font,
1704 render_priv->state.scale_x,
1705 render_priv->state.scale_y, NULL);
1707 get_outline_glyph(render_priv, code,
1708 glyphs + text_info->length, drawing);
1710 // Add additional space after italic to non-italic style changes
1711 if (text_info->length &&
1712 glyphs[text_info->length - 1].italic &&
1713 !render_priv->state.italic) {
1714 int back = text_info->length - 1;
1715 GlyphInfo *og = &glyphs[back];
1716 while (back && og->bbox.xMax - og->bbox.xMin == 0
1717 && og->italic)
1718 og = &glyphs[--back];
1719 if (og->bbox.xMax > og->advance.x) {
1720 // The FreeType oblique slants by 6/16
1721 pen.x += og->bbox.yMax * 0.375;
1725 glyphs[text_info->length].pos.x = pen.x;
1726 glyphs[text_info->length].pos.y = pen.y;
1728 pen.x += glyphs[text_info->length].advance.x;
1729 pen.x += double_to_d6(render_priv->state.hspacing *
1730 render_priv->font_scale
1731 * render_priv->state.scale_x);
1732 pen.y += glyphs[text_info->length].advance.y;
1733 pen.y += (render_priv->state.fay * render_priv->state.scale_y) *
1734 glyphs[text_info->length].advance.x;
1736 previous = code;
1738 glyphs[text_info->length].symbol = code;
1739 glyphs[text_info->length].linebreak = 0;
1740 for (i = 0; i < 4; ++i) {
1741 uint32_t clr = render_priv->state.c[i];
1742 change_alpha(&clr,
1743 mult_alpha(_a(clr), render_priv->state.fade), 1.);
1744 glyphs[text_info->length].c[i] = clr;
1746 glyphs[text_info->length].effect_type = render_priv->state.effect_type;
1747 glyphs[text_info->length].effect_timing =
1748 render_priv->state.effect_timing;
1749 glyphs[text_info->length].effect_skip_timing =
1750 render_priv->state.effect_skip_timing;
1751 glyphs[text_info->length].be = render_priv->state.be;
1752 glyphs[text_info->length].blur = render_priv->state.blur;
1753 glyphs[text_info->length].shadow_x = render_priv->state.shadow_x;
1754 glyphs[text_info->length].shadow_y = render_priv->state.shadow_y;
1755 glyphs[text_info->length].frx = render_priv->state.frx;
1756 glyphs[text_info->length].fry = render_priv->state.fry;
1757 glyphs[text_info->length].frz = render_priv->state.frz;
1758 glyphs[text_info->length].fax = render_priv->state.fax;
1759 glyphs[text_info->length].fay = render_priv->state.fay;
1760 if (drawing->hash) {
1761 glyphs[text_info->length].asc = drawing->asc;
1762 glyphs[text_info->length].desc = drawing->desc;
1763 } else {
1764 ass_font_get_asc_desc(render_priv->state.font, code,
1765 &glyphs[text_info->length].asc,
1766 &glyphs[text_info->length].desc);
1768 glyphs[text_info->length].asc *= render_priv->state.scale_y;
1769 glyphs[text_info->length].desc *= render_priv->state.scale_y;
1772 // fill bitmap hash
1773 glyphs[text_info->length].hash_key.type = BITMAP_OUTLINE;
1774 fill_bitmap_hash(render_priv, &glyphs[text_info->length].hash_key.u.outline);
1776 text_info->length++;
1778 render_priv->state.effect_type = EF_NONE;
1779 render_priv->state.effect_timing = 0;
1780 render_priv->state.effect_skip_timing = 0;
1782 if (drawing->hash) {
1783 ass_drawing_free(drawing);
1784 drawing = render_priv->state.drawing =
1785 ass_drawing_new(render_priv->library, render_priv->ftlibrary);
1790 if (text_info->length == 0) {
1791 // no valid symbols in the event; this can be smth like {comment}
1792 free_render_context(render_priv);
1793 return 1;
1796 // depends on glyph x coordinates being monotonous, so it should be done before line wrap
1797 process_karaoke_effects(render_priv);
1799 // alignments
1800 alignment = render_priv->state.alignment;
1801 halign = alignment & 3;
1802 valign = alignment & 12;
1804 MarginL =
1805 (event->MarginL) ? event->MarginL : render_priv->state.style->MarginL;
1806 MarginR =
1807 (event->MarginR) ? event->MarginR : render_priv->state.style->MarginR;
1808 MarginV =
1809 (event->MarginV) ? event->MarginV : render_priv->state.style->MarginV;
1811 if (render_priv->state.evt_type != EVENT_HSCROLL) {
1812 double max_text_width;
1814 // calculate max length of a line
1815 max_text_width =
1816 x2scr(render_priv,
1817 render_priv->track->PlayResX - MarginR) -
1818 x2scr(render_priv, MarginL);
1820 // rearrange text in several lines
1821 wrap_lines_smart(render_priv, max_text_width);
1823 // align text
1824 last_break = -1;
1825 for (i = 1; i < text_info->length + 1; ++i) { // (text_info->length + 1) is the end of the last line
1826 if ((i == text_info->length)
1827 || glyphs[i].linebreak) {
1828 double width, shift = 0;
1829 GlyphInfo *first_glyph =
1830 glyphs + last_break + 1;
1831 GlyphInfo *last_glyph = glyphs + i - 1;
1833 while (first_glyph < last_glyph && first_glyph->skip)
1834 first_glyph++;
1836 while ((last_glyph > first_glyph)
1837 && ((last_glyph->symbol == '\n')
1838 || (last_glyph->symbol == 0)
1839 || (last_glyph->skip)))
1840 last_glyph--;
1842 width = d6_to_double(
1843 last_glyph->pos.x + last_glyph->advance.x -
1844 first_glyph->pos.x);
1845 if (halign == HALIGN_LEFT) { // left aligned, no action
1846 shift = 0;
1847 } else if (halign == HALIGN_RIGHT) { // right aligned
1848 shift = max_text_width - width;
1849 } else if (halign == HALIGN_CENTER) { // centered
1850 shift = (max_text_width - width) / 2.0;
1852 for (j = last_break + 1; j < i; ++j) {
1853 glyphs[j].pos.x += double_to_d6(shift);
1855 last_break = i - 1;
1858 } else { // render_priv->state.evt_type == EVENT_HSCROLL
1859 measure_text(render_priv);
1862 // determing text bounding box
1863 compute_string_bbox(text_info, &bbox);
1865 // determine device coordinates for text
1867 // x coordinate for everything except positioned events
1868 if (render_priv->state.evt_type == EVENT_NORMAL ||
1869 render_priv->state.evt_type == EVENT_VSCROLL) {
1870 device_x = x2scr(render_priv, MarginL);
1871 } else if (render_priv->state.evt_type == EVENT_HSCROLL) {
1872 if (render_priv->state.scroll_direction == SCROLL_RL)
1873 device_x =
1874 x2scr(render_priv,
1875 render_priv->track->PlayResX -
1876 render_priv->state.scroll_shift);
1877 else if (render_priv->state.scroll_direction == SCROLL_LR)
1878 device_x =
1879 x2scr(render_priv,
1880 render_priv->state.scroll_shift) - (bbox.xMax -
1881 bbox.xMin);
1884 // y coordinate for everything except positioned events
1885 if (render_priv->state.evt_type == EVENT_NORMAL ||
1886 render_priv->state.evt_type == EVENT_HSCROLL) {
1887 if (valign == VALIGN_TOP) { // toptitle
1888 device_y =
1889 y2scr_top(render_priv,
1890 MarginV) + text_info->lines[0].asc;
1891 } else if (valign == VALIGN_CENTER) { // midtitle
1892 double scr_y =
1893 y2scr(render_priv, render_priv->track->PlayResY / 2.0);
1894 device_y = scr_y - (bbox.yMax + bbox.yMin) / 2.0;
1895 } else { // subtitle
1896 double scr_y;
1897 if (valign != VALIGN_SUB)
1898 ass_msg(render_priv->library, MSGL_V,
1899 "Invalid valign, assuming 0 (subtitle)");
1900 scr_y =
1901 y2scr_sub(render_priv,
1902 render_priv->track->PlayResY - MarginV);
1903 device_y = scr_y;
1904 device_y -= text_info->height;
1905 device_y += text_info->lines[0].asc;
1907 } else if (render_priv->state.evt_type == EVENT_VSCROLL) {
1908 if (render_priv->state.scroll_direction == SCROLL_TB)
1909 device_y =
1910 y2scr(render_priv,
1911 render_priv->state.clip_y0 +
1912 render_priv->state.scroll_shift) - (bbox.yMax -
1913 bbox.yMin);
1914 else if (render_priv->state.scroll_direction == SCROLL_BT)
1915 device_y =
1916 y2scr(render_priv,
1917 render_priv->state.clip_y1 -
1918 render_priv->state.scroll_shift);
1921 // positioned events are totally different
1922 if (render_priv->state.evt_type == EVENT_POSITIONED) {
1923 double base_x = 0;
1924 double base_y = 0;
1925 ass_msg(render_priv->library, MSGL_DBG2, "positioned event at %f, %f",
1926 render_priv->state.pos_x, render_priv->state.pos_y);
1927 get_base_point(&bbox, alignment, &base_x, &base_y);
1928 device_x =
1929 x2scr_pos(render_priv, render_priv->state.pos_x) - base_x;
1930 device_y =
1931 y2scr_pos(render_priv, render_priv->state.pos_y) - base_y;
1934 // fix clip coordinates (they depend on alignment)
1935 if (render_priv->state.evt_type == EVENT_NORMAL ||
1936 render_priv->state.evt_type == EVENT_HSCROLL ||
1937 render_priv->state.evt_type == EVENT_VSCROLL) {
1938 render_priv->state.clip_x0 =
1939 x2scr_scaled(render_priv, render_priv->state.clip_x0);
1940 render_priv->state.clip_x1 =
1941 x2scr_scaled(render_priv, render_priv->state.clip_x1);
1942 if (valign == VALIGN_TOP) {
1943 render_priv->state.clip_y0 =
1944 y2scr_top(render_priv, render_priv->state.clip_y0);
1945 render_priv->state.clip_y1 =
1946 y2scr_top(render_priv, render_priv->state.clip_y1);
1947 } else if (valign == VALIGN_CENTER) {
1948 render_priv->state.clip_y0 =
1949 y2scr(render_priv, render_priv->state.clip_y0);
1950 render_priv->state.clip_y1 =
1951 y2scr(render_priv, render_priv->state.clip_y1);
1952 } else if (valign == VALIGN_SUB) {
1953 render_priv->state.clip_y0 =
1954 y2scr_sub(render_priv, render_priv->state.clip_y0);
1955 render_priv->state.clip_y1 =
1956 y2scr_sub(render_priv, render_priv->state.clip_y1);
1958 } else if (render_priv->state.evt_type == EVENT_POSITIONED) {
1959 render_priv->state.clip_x0 =
1960 x2scr_pos_scaled(render_priv, render_priv->state.clip_x0);
1961 render_priv->state.clip_x1 =
1962 x2scr_pos_scaled(render_priv, render_priv->state.clip_x1);
1963 render_priv->state.clip_y0 =
1964 y2scr_pos(render_priv, render_priv->state.clip_y0);
1965 render_priv->state.clip_y1 =
1966 y2scr_pos(render_priv, render_priv->state.clip_y1);
1969 // calculate rotation parameters
1971 DVector center;
1973 if (render_priv->state.have_origin) {
1974 center.x = x2scr(render_priv, render_priv->state.org_x);
1975 center.y = y2scr(render_priv, render_priv->state.org_y);
1976 } else {
1977 double bx = 0., by = 0.;
1978 get_base_point(&bbox, alignment, &bx, &by);
1979 center.x = device_x + bx;
1980 center.y = device_y + by;
1983 for (i = 0; i < text_info->length; ++i) {
1984 GlyphInfo *info = glyphs + i;
1985 OutlineBitmapHashKey *key = &info->hash_key.u.outline;
1987 if (key->frx || key->fry || key->frz || key->fax || key->fay) {
1988 key->shift_x = info->pos.x + double_to_d6(device_x - center.x);
1989 key->shift_y = -(info->pos.y + double_to_d6(device_y - center.y));
1990 } else {
1991 key->shift_x = 0;
1992 key->shift_y = 0;
1997 // convert glyphs to bitmaps
1998 device_x *= render_priv->font_scale_x;
1999 for (i = 0; i < text_info->length; ++i) {
2000 GlyphInfo *g = glyphs + i;
2001 OutlineBitmapHashKey *key = &g->hash_key.u.outline;
2002 g->pos.x *= render_priv->font_scale_x;
2003 key->advance.x =
2004 double_to_d6(device_x - (int) device_x +
2005 d6_to_double(g->pos.x & SUBPIXEL_MASK)) & ~SUBPIXEL_ACCURACY;
2006 key->advance.y =
2007 double_to_d6(device_y - (int) device_y +
2008 d6_to_double(g->pos.y & SUBPIXEL_MASK)) & ~SUBPIXEL_ACCURACY;
2009 get_bitmap_glyph(render_priv, glyphs + i);
2012 memset(event_images, 0, sizeof(*event_images));
2013 event_images->top = device_y - text_info->lines[0].asc;
2014 event_images->height = text_info->height;
2015 event_images->left =
2016 (device_x + bbox.xMin * render_priv->font_scale_x) + 0.5;
2017 event_images->width =
2018 (bbox.xMax - bbox.xMin) * render_priv->font_scale_x + 0.5;
2019 event_images->detect_collisions = render_priv->state.detect_collisions;
2020 event_images->shift_direction = (valign == VALIGN_TOP) ? 1 : -1;
2021 event_images->event = event;
2022 event_images->imgs = render_text(render_priv, (int) device_x, (int) device_y);
2024 free_render_context(render_priv);
2026 return 0;
2030 * \brief deallocate image list
2031 * \param img list pointer
2033 void ass_free_images(ASS_Image *img)
2035 while (img) {
2036 ASS_Image *next = img->next;
2037 free(img);
2038 img = next;
2043 * \brief Check cache limits and reset cache if they are exceeded
2045 static void check_cache_limits(ASS_Renderer *priv, CacheStore *cache)
2047 if (ass_cache_empty(cache->bitmap_cache, cache->bitmap_max_size)) {
2048 ass_cache_empty(cache->composite_cache, 0);
2049 ass_free_images(priv->prev_images_root);
2050 priv->prev_images_root = 0;
2052 if (ass_cache_empty(cache->outline_cache, cache->glyph_max)) {
2053 ass_cache_empty(cache->bitmap_cache, 0);
2054 ass_cache_empty(cache->composite_cache, 0);
2055 ass_free_images(priv->prev_images_root);
2056 priv->prev_images_root = 0;
2061 * \brief Start a new frame
2063 static int
2064 ass_start_frame(ASS_Renderer *render_priv, ASS_Track *track,
2065 long long now)
2067 ASS_Settings *settings_priv = &render_priv->settings;
2069 if (!render_priv->settings.frame_width
2070 && !render_priv->settings.frame_height)
2071 return 1; // library not initialized
2073 if (render_priv->library != track->library)
2074 return 1;
2076 if (!render_priv->fontconfig_priv)
2077 return 1;
2079 free_list_clear(render_priv);
2081 if (track->n_events == 0)
2082 return 1; // nothing to do
2084 render_priv->track = track;
2085 render_priv->time = now;
2087 ass_lazy_track_init(render_priv->library, render_priv->track);
2089 render_priv->font_scale = settings_priv->font_size_coeff *
2090 render_priv->orig_height / render_priv->track->PlayResY;
2091 if (render_priv->track->ScaledBorderAndShadow)
2092 render_priv->border_scale =
2093 ((double) render_priv->orig_height) /
2094 render_priv->track->PlayResY;
2095 else
2096 render_priv->border_scale = 1.;
2098 // PAR correction
2099 render_priv->font_scale_x = render_priv->settings.aspect /
2100 render_priv->settings.storage_aspect;
2102 render_priv->prev_images_root = render_priv->images_root;
2103 render_priv->images_root = 0;
2105 check_cache_limits(render_priv, &render_priv->cache);
2107 return 0;
2110 static int cmp_event_layer(const void *p1, const void *p2)
2112 ASS_Event *e1 = ((EventImages *) p1)->event;
2113 ASS_Event *e2 = ((EventImages *) p2)->event;
2114 if (e1->Layer < e2->Layer)
2115 return -1;
2116 if (e1->Layer > e2->Layer)
2117 return 1;
2118 if (e1->ReadOrder < e2->ReadOrder)
2119 return -1;
2120 if (e1->ReadOrder > e2->ReadOrder)
2121 return 1;
2122 return 0;
2125 static ASS_RenderPriv *get_render_priv(ASS_Renderer *render_priv,
2126 ASS_Event *event)
2128 if (!event->render_priv)
2129 event->render_priv = calloc(1, sizeof(ASS_RenderPriv));
2130 if (render_priv->render_id != event->render_priv->render_id) {
2131 memset(event->render_priv, 0, sizeof(ASS_RenderPriv));
2132 event->render_priv->render_id = render_priv->render_id;
2135 return event->render_priv;
2138 static int overlap(Segment *s1, Segment *s2)
2140 if (s1->a >= s2->b || s2->a >= s1->b ||
2141 s1->ha >= s2->hb || s2->ha >= s1->hb)
2142 return 0;
2143 return 1;
2146 static int cmp_segment(const void *p1, const void *p2)
2148 return ((Segment *) p1)->a - ((Segment *) p2)->a;
2151 static void
2152 shift_event(ASS_Renderer *render_priv, EventImages *ei, int shift)
2154 ASS_Image *cur = ei->imgs;
2155 while (cur) {
2156 cur->dst_y += shift;
2157 // clip top and bottom
2158 if (cur->dst_y < 0) {
2159 int clip = -cur->dst_y;
2160 cur->h -= clip;
2161 cur->bitmap += clip * cur->stride;
2162 cur->dst_y = 0;
2164 if (cur->dst_y + cur->h >= render_priv->height) {
2165 int clip = cur->dst_y + cur->h - render_priv->height;
2166 cur->h -= clip;
2168 if (cur->h <= 0) {
2169 cur->h = 0;
2170 cur->dst_y = 0;
2172 cur = cur->next;
2174 ei->top += shift;
2177 // dir: 1 - move down
2178 // -1 - move up
2179 static int fit_segment(Segment *s, Segment *fixed, int *cnt, int dir)
2181 int i;
2182 int shift = 0;
2184 if (dir == 1) // move down
2185 for (i = 0; i < *cnt; ++i) {
2186 if (s->b + shift <= fixed[i].a || s->a + shift >= fixed[i].b ||
2187 s->hb <= fixed[i].ha || s->ha >= fixed[i].hb)
2188 continue;
2189 shift = fixed[i].b - s->a;
2190 } else // dir == -1, move up
2191 for (i = *cnt - 1; i >= 0; --i) {
2192 if (s->b + shift <= fixed[i].a || s->a + shift >= fixed[i].b ||
2193 s->hb <= fixed[i].ha || s->ha >= fixed[i].hb)
2194 continue;
2195 shift = fixed[i].a - s->b;
2198 fixed[*cnt].a = s->a + shift;
2199 fixed[*cnt].b = s->b + shift;
2200 fixed[*cnt].ha = s->ha;
2201 fixed[*cnt].hb = s->hb;
2202 (*cnt)++;
2203 qsort(fixed, *cnt, sizeof(Segment), cmp_segment);
2205 return shift;
2208 static void
2209 fix_collisions(ASS_Renderer *render_priv, EventImages *imgs, int cnt)
2211 Segment *used = malloc(cnt * sizeof(*used));
2212 int cnt_used = 0;
2213 int i, j;
2215 // fill used[] with fixed events
2216 for (i = 0; i < cnt; ++i) {
2217 ASS_RenderPriv *priv;
2218 if (!imgs[i].detect_collisions)
2219 continue;
2220 priv = get_render_priv(render_priv, imgs[i].event);
2221 if (priv->height > 0) { // it's a fixed event
2222 Segment s;
2223 s.a = priv->top;
2224 s.b = priv->top + priv->height;
2225 s.ha = priv->left;
2226 s.hb = priv->left + priv->width;
2227 if (priv->height != imgs[i].height) { // no, it's not
2228 ass_msg(render_priv->library, MSGL_WARN,
2229 "Event height has changed");
2230 priv->top = 0;
2231 priv->height = 0;
2232 priv->left = 0;
2233 priv->width = 0;
2235 for (j = 0; j < cnt_used; ++j)
2236 if (overlap(&s, used + j)) { // no, it's not
2237 priv->top = 0;
2238 priv->height = 0;
2239 priv->left = 0;
2240 priv->width = 0;
2242 if (priv->height > 0) { // still a fixed event
2243 used[cnt_used].a = priv->top;
2244 used[cnt_used].b = priv->top + priv->height;
2245 used[cnt_used].ha = priv->left;
2246 used[cnt_used].hb = priv->left + priv->width;
2247 cnt_used++;
2248 shift_event(render_priv, imgs + i, priv->top - imgs[i].top);
2252 qsort(used, cnt_used, sizeof(Segment), cmp_segment);
2254 // try to fit other events in free spaces
2255 for (i = 0; i < cnt; ++i) {
2256 ASS_RenderPriv *priv;
2257 if (!imgs[i].detect_collisions)
2258 continue;
2259 priv = get_render_priv(render_priv, imgs[i].event);
2260 if (priv->height == 0) { // not a fixed event
2261 int shift;
2262 Segment s;
2263 s.a = imgs[i].top;
2264 s.b = imgs[i].top + imgs[i].height;
2265 s.ha = imgs[i].left;
2266 s.hb = imgs[i].left + imgs[i].width;
2267 shift = fit_segment(&s, used, &cnt_used, imgs[i].shift_direction);
2268 if (shift)
2269 shift_event(render_priv, imgs + i, shift);
2270 // make it fixed
2271 priv->top = imgs[i].top;
2272 priv->height = imgs[i].height;
2273 priv->left = imgs[i].left;
2274 priv->width = imgs[i].width;
2279 free(used);
2283 * \brief compare two images
2284 * \param i1 first image
2285 * \param i2 second image
2286 * \return 0 if identical, 1 if different positions, 2 if different content
2288 static int ass_image_compare(ASS_Image *i1, ASS_Image *i2)
2290 if (i1->w != i2->w)
2291 return 2;
2292 if (i1->h != i2->h)
2293 return 2;
2294 if (i1->stride != i2->stride)
2295 return 2;
2296 if (i1->color != i2->color)
2297 return 2;
2298 if (i1->bitmap != i2->bitmap)
2299 return 2;
2300 if (i1->dst_x != i2->dst_x)
2301 return 1;
2302 if (i1->dst_y != i2->dst_y)
2303 return 1;
2304 return 0;
2308 * \brief compare current and previous image list
2309 * \param priv library handle
2310 * \return 0 if identical, 1 if different positions, 2 if different content
2312 static int ass_detect_change(ASS_Renderer *priv)
2314 ASS_Image *img, *img2;
2315 int diff;
2317 img = priv->prev_images_root;
2318 img2 = priv->images_root;
2319 diff = 0;
2320 while (img && diff < 2) {
2321 ASS_Image *next, *next2;
2322 next = img->next;
2323 if (img2) {
2324 int d = ass_image_compare(img, img2);
2325 if (d > diff)
2326 diff = d;
2327 next2 = img2->next;
2328 } else {
2329 // previous list is shorter
2330 diff = 2;
2331 break;
2333 img = next;
2334 img2 = next2;
2337 // is the previous list longer?
2338 if (img2)
2339 diff = 2;
2341 return diff;
2345 * \brief render a frame
2346 * \param priv library handle
2347 * \param track track
2348 * \param now current video timestamp (ms)
2349 * \param detect_change a value describing how the new images differ from the previous ones will be written here:
2350 * 0 if identical, 1 if different positions, 2 if different content.
2351 * Can be NULL, in that case no detection is performed.
2353 ASS_Image *ass_render_frame(ASS_Renderer *priv, ASS_Track *track,
2354 long long now, int *detect_change)
2356 int i, cnt, rc;
2357 EventImages *last;
2358 ASS_Image **tail;
2360 // init frame
2361 rc = ass_start_frame(priv, track, now);
2362 if (rc != 0)
2363 return 0;
2365 // render events separately
2366 cnt = 0;
2367 for (i = 0; i < track->n_events; ++i) {
2368 ASS_Event *event = track->events + i;
2369 if ((event->Start <= now)
2370 && (now < (event->Start + event->Duration))) {
2371 if (cnt >= priv->eimg_size) {
2372 priv->eimg_size += 100;
2373 priv->eimg =
2374 realloc(priv->eimg,
2375 priv->eimg_size * sizeof(EventImages));
2377 rc = ass_render_event(priv, event, priv->eimg + cnt);
2378 if (!rc)
2379 ++cnt;
2383 // sort by layer
2384 qsort(priv->eimg, cnt, sizeof(EventImages), cmp_event_layer);
2386 // call fix_collisions for each group of events with the same layer
2387 last = priv->eimg;
2388 for (i = 1; i < cnt; ++i)
2389 if (last->event->Layer != priv->eimg[i].event->Layer) {
2390 fix_collisions(priv, last, priv->eimg + i - last);
2391 last = priv->eimg + i;
2393 if (cnt > 0)
2394 fix_collisions(priv, last, priv->eimg + cnt - last);
2396 // concat lists
2397 tail = &priv->images_root;
2398 for (i = 0; i < cnt; ++i) {
2399 ASS_Image *cur = priv->eimg[i].imgs;
2400 while (cur) {
2401 *tail = cur;
2402 tail = &cur->next;
2403 cur = cur->next;
2407 if (detect_change)
2408 *detect_change = ass_detect_change(priv);
2410 // free the previous image list
2411 ass_free_images(priv->prev_images_root);
2412 priv->prev_images_root = 0;
2414 return priv->images_root;