Mercurial > mplayer.hg
annotate libass/ass_render.c @ 35173:95ad1e516f03
mp_property_deinterlace: Signalize control() failure
Always having to do a GET after a SET to check if the state was
really changed, seems not very friendly from the perspective of
the client application that changes the property.
Return M_PROPERTY_UNAVAILABLE to signalize that the control() was
not positively consumed by the filter/vo chain.
Based on a patch by Vicente Sendra >visenri yahoo.es<
author | al |
---|---|
date | Thu, 25 Oct 2012 18:26:45 +0000 |
parents | da8125c9ecad |
children | 49fc594fda43 |
rev | line source |
---|---|
20008
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
1 /* |
26723 | 2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com> |
3 * | |
26738
588ce97b44f2
Speak of libass instead of MPlayer in the libass license headers.
diego
parents:
26723
diff
changeset
|
4 * This file is part of libass. |
26723 | 5 * |
34011 | 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. | |
26723 | 9 * |
34011 | 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. | |
26723 | 17 */ |
20008
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
18 |
18937 | 19 #include "config.h" |
20 | |
21 #include <assert.h> | |
22 #include <math.h> | |
23 | |
30200 | 24 #include "ass_render.h" |
25 #include "ass_parse.h" | |
34295 | 26 #include "ass_shaper.h" |
22258
9c1160622400
Reallocate event_images_t, removing limit on simultanious events count.
eugeni
parents:
22221
diff
changeset
|
27 |
30200 | 28 #define MAX_GLYPHS_INITIAL 1024 |
29 #define MAX_LINES_INITIAL 64 | |
30 #define SUBPIXEL_MASK 63 | |
31853 | 31 #define SUBPIXEL_ACCURACY 7 |
18937 | 32 |
30200 | 33 ASS_Renderer *ass_renderer_init(ASS_Library *library) |
18937 | 34 { |
30200 | 35 int error; |
36 FT_Library ft; | |
37 ASS_Renderer *priv = 0; | |
38 int vmajor, vminor, vpatch; | |
20202
9b67ed06f721
Zerofill libass static variables during initialization.
eugeni
parents:
20201
diff
changeset
|
39 |
30200 | 40 error = FT_Init_FreeType(&ft); |
41 if (error) { | |
42 ass_msg(library, MSGL_FATAL, "%s failed", "FT_Init_FreeType"); | |
43 goto ass_init_exit; | |
44 } | |
18937 | 45 |
30200 | 46 FT_Library_Version(ft, &vmajor, &vminor, &vpatch); |
34295 | 47 ass_msg(library, MSGL_V, "Raster: FreeType %d.%d.%d", |
30200 | 48 vmajor, vminor, vpatch); |
49 | |
50 priv = calloc(1, sizeof(ASS_Renderer)); | |
51 if (!priv) { | |
52 FT_Done_FreeType(ft); | |
53 goto ass_init_exit; | |
54 } | |
26032
93dcb01491cf
Print FreeType version in libass init. Makes error logs slightly more helpful.
eugeni
parents:
25513
diff
changeset
|
55 |
30200 | 56 priv->synth_priv = ass_synth_init(BLUR_MAX_RADIUS); |
57 | |
58 priv->library = library; | |
59 priv->ftlibrary = ft; | |
60 // images_root and related stuff is zero-filled in calloc | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
61 |
34295 | 62 priv->cache.font_cache = ass_font_cache_create(); |
63 priv->cache.bitmap_cache = ass_bitmap_cache_create(); | |
64 priv->cache.composite_cache = ass_composite_cache_create(); | |
65 priv->cache.outline_cache = ass_outline_cache_create(); | |
30200 | 66 priv->cache.glyph_max = GLYPH_CACHE_MAX; |
67 priv->cache.bitmap_max_size = BITMAP_CACHE_MAX_SIZE; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29178
diff
changeset
|
68 |
30200 | 69 priv->text_info.max_glyphs = MAX_GLYPHS_INITIAL; |
70 priv->text_info.max_lines = MAX_LINES_INITIAL; | |
31853 | 71 priv->text_info.glyphs = calloc(MAX_GLYPHS_INITIAL, sizeof(GlyphInfo)); |
30200 | 72 priv->text_info.lines = calloc(MAX_LINES_INITIAL, sizeof(LineInfo)); |
18937 | 73 |
31853 | 74 priv->settings.font_size_coeff = 1.; |
75 | |
34295 | 76 priv->shaper = ass_shaper_new(0); |
77 ass_shaper_info(library); | |
78 #ifdef CONFIG_HARFBUZZ | |
79 priv->settings.shaper = ASS_SHAPING_COMPLEX; | |
80 #else | |
81 priv->settings.shaper = ASS_SHAPING_SIMPLE; | |
82 #endif | |
83 | |
30200 | 84 ass_init_exit: |
85 if (priv) | |
34295 | 86 ass_msg(library, MSGL_V, "Initialized"); |
30200 | 87 else |
34295 | 88 ass_msg(library, MSGL_ERR, "Initialization failed"); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29178
diff
changeset
|
89 |
30200 | 90 return priv; |
18937 | 91 } |
92 | |
30200 | 93 static void free_list_clear(ASS_Renderer *render_priv) |
94 { | |
95 if (render_priv->free_head) { | |
96 FreeList *item = render_priv->free_head; | |
97 while(item) { | |
98 FreeList *oi = item; | |
99 free(item->object); | |
100 item = item->next; | |
101 free(oi); | |
102 } | |
103 render_priv->free_head = NULL; | |
104 } | |
105 } | |
106 | |
107 void ass_renderer_done(ASS_Renderer *render_priv) | |
18937 | 108 { |
34295 | 109 ass_cache_done(render_priv->cache.font_cache); |
110 ass_cache_done(render_priv->cache.bitmap_cache); | |
111 ass_cache_done(render_priv->cache.composite_cache); | |
112 ass_cache_done(render_priv->cache.outline_cache); | |
30200 | 113 |
114 ass_free_images(render_priv->images_root); | |
115 ass_free_images(render_priv->prev_images_root); | |
116 | |
117 if (render_priv->state.stroker) { | |
118 FT_Stroker_Done(render_priv->state.stroker); | |
119 render_priv->state.stroker = 0; | |
120 } | |
31875 | 121 if (render_priv->ftlibrary) |
30200 | 122 FT_Done_FreeType(render_priv->ftlibrary); |
31875 | 123 if (render_priv->fontconfig_priv) |
30200 | 124 fontconfig_done(render_priv->fontconfig_priv); |
31875 | 125 if (render_priv->synth_priv) |
30200 | 126 ass_synth_done(render_priv->synth_priv); |
34295 | 127 ass_shaper_free(render_priv->shaper); |
31875 | 128 free(render_priv->eimg); |
30200 | 129 free(render_priv->text_info.glyphs); |
130 free(render_priv->text_info.lines); | |
131 | |
132 free(render_priv->settings.default_font); | |
133 free(render_priv->settings.default_family); | |
134 | |
135 free_list_clear(render_priv); | |
136 free(render_priv); | |
18937 | 137 } |
138 | |
139 /** | |
30200 | 140 * \brief Create a new ASS_Image |
141 * Parameters are the same as ASS_Image fields. | |
18937 | 142 */ |
30200 | 143 static ASS_Image *my_draw_bitmap(unsigned char *bitmap, int bitmap_w, |
144 int bitmap_h, int stride, int dst_x, | |
145 int dst_y, uint32_t color) | |
18937 | 146 { |
31853 | 147 ASS_Image *img = malloc(sizeof(ASS_Image)); |
30200 | 148 |
31853 | 149 if (img) { |
150 img->w = bitmap_w; | |
151 img->h = bitmap_h; | |
152 img->stride = stride; | |
153 img->bitmap = bitmap; | |
154 img->color = color; | |
155 img->dst_x = dst_x; | |
156 img->dst_y = dst_y; | |
157 } | |
30200 | 158 |
159 return img; | |
160 } | |
161 | |
31853 | 162 /** |
163 * \brief Mapping between script and screen coordinates | |
164 */ | |
165 static double x2scr(ASS_Renderer *render_priv, double x) | |
166 { | |
167 return x * render_priv->orig_width_nocrop / render_priv->font_scale_x / | |
168 render_priv->track->PlayResX + | |
169 FFMAX(render_priv->settings.left_margin, 0); | |
170 } | |
171 static double x2scr_pos(ASS_Renderer *render_priv, double x) | |
172 { | |
173 return x * render_priv->orig_width / render_priv->font_scale_x / render_priv->track->PlayResX + | |
174 render_priv->settings.left_margin; | |
175 } | |
176 static double x2scr_scaled(ASS_Renderer *render_priv, double x) | |
177 { | |
178 return x * render_priv->orig_width_nocrop / | |
179 render_priv->track->PlayResX + | |
180 FFMAX(render_priv->settings.left_margin, 0); | |
181 } | |
182 static double x2scr_pos_scaled(ASS_Renderer *render_priv, double x) | |
183 { | |
184 return x * render_priv->orig_width / render_priv->track->PlayResX + | |
185 render_priv->settings.left_margin; | |
186 } | |
187 /** | |
188 * \brief Mapping between script and screen coordinates | |
189 */ | |
190 static double y2scr(ASS_Renderer *render_priv, double y) | |
191 { | |
192 return y * render_priv->orig_height_nocrop / | |
193 render_priv->track->PlayResY + | |
194 FFMAX(render_priv->settings.top_margin, 0); | |
195 } | |
196 static double y2scr_pos(ASS_Renderer *render_priv, double y) | |
197 { | |
198 return y * render_priv->orig_height / render_priv->track->PlayResY + | |
199 render_priv->settings.top_margin; | |
200 } | |
201 | |
202 // the same for toptitles | |
203 static double y2scr_top(ASS_Renderer *render_priv, double y) | |
204 { | |
205 if (render_priv->settings.use_margins) | |
206 return y * render_priv->orig_height_nocrop / | |
207 render_priv->track->PlayResY; | |
208 else | |
209 return y * render_priv->orig_height_nocrop / | |
210 render_priv->track->PlayResY + | |
211 FFMAX(render_priv->settings.top_margin, 0); | |
212 } | |
213 // the same for subtitles | |
214 static double y2scr_sub(ASS_Renderer *render_priv, double y) | |
215 { | |
216 if (render_priv->settings.use_margins) | |
217 return y * render_priv->orig_height_nocrop / | |
218 render_priv->track->PlayResY + | |
219 FFMAX(render_priv->settings.top_margin, 0) | |
220 + FFMAX(render_priv->settings.bottom_margin, 0); | |
221 else | |
222 return y * render_priv->orig_height_nocrop / | |
223 render_priv->track->PlayResY + | |
224 FFMAX(render_priv->settings.top_margin, 0); | |
225 } | |
30200 | 226 |
227 /* | |
228 * \brief Convert bitmap glyphs into ASS_Image list with inverse clipping | |
229 * | |
230 * Inverse clipping with the following strategy: | |
231 * - find rectangle from (x0, y0) to (cx0, y1) | |
232 * - find rectangle from (cx0, y0) to (cx1, cy0) | |
233 * - find rectangle from (cx0, cy1) to (cx1, y1) | |
234 * - find rectangle from (cx1, y0) to (x1, y1) | |
235 * These rectangles can be invalid and in this case are discarded. | |
236 * Afterwards, they are clipped against the screen coordinates. | |
237 * In an additional pass, the rectangles need to be split up left/right for | |
238 * karaoke effects. This can result in a lot of bitmaps (6 to be exact). | |
239 */ | |
240 static ASS_Image **render_glyph_i(ASS_Renderer *render_priv, | |
241 Bitmap *bm, int dst_x, int dst_y, | |
242 uint32_t color, uint32_t color2, int brk, | |
243 ASS_Image **tail) | |
244 { | |
245 int i, j, x0, y0, x1, y1, cx0, cy0, cx1, cy1, sx, sy, zx, zy; | |
246 Rect r[4]; | |
247 ASS_Image *img; | |
248 | |
249 dst_x += bm->left; | |
250 dst_y += bm->top; | |
251 | |
252 // we still need to clip against screen boundaries | |
31853 | 253 zx = x2scr_pos_scaled(render_priv, 0); |
30200 | 254 zy = y2scr_pos(render_priv, 0); |
31853 | 255 sx = x2scr_pos_scaled(render_priv, render_priv->track->PlayResX); |
30200 | 256 sy = y2scr_pos(render_priv, render_priv->track->PlayResY); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29178
diff
changeset
|
257 |
30200 | 258 x0 = 0; |
259 y0 = 0; | |
260 x1 = bm->w; | |
261 y1 = bm->h; | |
262 cx0 = render_priv->state.clip_x0 - dst_x; | |
263 cy0 = render_priv->state.clip_y0 - dst_y; | |
264 cx1 = render_priv->state.clip_x1 - dst_x; | |
265 cy1 = render_priv->state.clip_y1 - dst_y; | |
266 | |
267 // calculate rectangles and discard invalid ones while we're at it. | |
268 i = 0; | |
269 r[i].x0 = x0; | |
270 r[i].y0 = y0; | |
271 r[i].x1 = (cx0 > x1) ? x1 : cx0; | |
272 r[i].y1 = y1; | |
273 if (r[i].x1 > r[i].x0 && r[i].y1 > r[i].y0) i++; | |
274 r[i].x0 = (cx0 < 0) ? x0 : cx0; | |
275 r[i].y0 = y0; | |
276 r[i].x1 = (cx1 > x1) ? x1 : cx1; | |
277 r[i].y1 = (cy0 > y1) ? y1 : cy0; | |
278 if (r[i].x1 > r[i].x0 && r[i].y1 > r[i].y0) i++; | |
279 r[i].x0 = (cx0 < 0) ? x0 : cx0; | |
280 r[i].y0 = (cy1 < 0) ? y0 : cy1; | |
281 r[i].x1 = (cx1 > x1) ? x1 : cx1; | |
282 r[i].y1 = y1; | |
283 if (r[i].x1 > r[i].x0 && r[i].y1 > r[i].y0) i++; | |
284 r[i].x0 = (cx1 < 0) ? x0 : cx1; | |
285 r[i].y0 = y0; | |
286 r[i].x1 = x1; | |
287 r[i].y1 = y1; | |
288 if (r[i].x1 > r[i].x0 && r[i].y1 > r[i].y0) i++; | |
29383
e9cab9f6ed62
Make sure clip coordinates are inside the screen area.
eugeni
parents:
29382
diff
changeset
|
289 |
30200 | 290 // clip each rectangle to screen coordinates |
291 for (j = 0; j < i; j++) { | |
292 r[j].x0 = (r[j].x0 + dst_x < zx) ? zx - dst_x : r[j].x0; | |
293 r[j].y0 = (r[j].y0 + dst_y < zy) ? zy - dst_y : r[j].y0; | |
294 r[j].x1 = (r[j].x1 + dst_x > sx) ? sx - dst_x : r[j].x1; | |
295 r[j].y1 = (r[j].y1 + dst_y > sy) ? sy - dst_y : r[j].y1; | |
296 } | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
297 |
30200 | 298 // draw the rectangles |
299 for (j = 0; j < i; j++) { | |
300 int lbrk = brk; | |
301 // kick out rectangles that are invalid now | |
302 if (r[j].x1 <= r[j].x0 || r[j].y1 <= r[j].y0) | |
303 continue; | |
304 // split up into left and right for karaoke, if needed | |
305 if (lbrk > r[j].x0) { | |
306 if (lbrk > r[j].x1) lbrk = r[j].x1; | |
34295 | 307 img = my_draw_bitmap(bm->buffer + r[j].y0 * bm->stride + r[j].x0, |
30200 | 308 lbrk - r[j].x0, r[j].y1 - r[j].y0, |
34295 | 309 bm->stride, dst_x + r[j].x0, dst_y + r[j].y0, color); |
31853 | 310 if (!img) break; |
30200 | 311 *tail = img; |
312 tail = &img->next; | |
313 } | |
314 if (lbrk < r[j].x1) { | |
315 if (lbrk < r[j].x0) lbrk = r[j].x0; | |
34295 | 316 img = my_draw_bitmap(bm->buffer + r[j].y0 * bm->stride + lbrk, |
30200 | 317 r[j].x1 - lbrk, r[j].y1 - r[j].y0, |
34295 | 318 bm->stride, dst_x + lbrk, dst_y + r[j].y0, color2); |
31853 | 319 if (!img) break; |
30200 | 320 *tail = img; |
321 tail = &img->next; | |
322 } | |
323 } | |
324 | |
325 return tail; | |
18937 | 326 } |
327 | |
328 /** | |
30200 | 329 * \brief convert bitmap glyph into ASS_Image struct(s) |
18937 | 330 * \param bit freetype bitmap glyph, FT_PIXEL_MODE_GRAY |
331 * \param dst_x bitmap x coordinate in video frame | |
332 * \param dst_y bitmap y coordinate in video frame | |
333 * \param color first color, RGBA | |
334 * \param color2 second color, RGBA | |
335 * \param brk x coordinate relative to glyph origin, color is used to the left of brk, color2 - to the right | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
336 * \param tail pointer to the last image's next field, head of the generated list should be stored here |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
337 * \return pointer to the new list tail |
18937 | 338 * Performs clipping. Uses my_draw_bitmap for actual bitmap convertion. |
339 */ | |
30200 | 340 static ASS_Image ** |
341 render_glyph(ASS_Renderer *render_priv, Bitmap *bm, int dst_x, int dst_y, | |
342 uint32_t color, uint32_t color2, int brk, ASS_Image **tail) | |
18937 | 343 { |
30200 | 344 // Inverse clipping in use? |
345 if (render_priv->state.clip_mode) | |
346 return render_glyph_i(render_priv, bm, dst_x, dst_y, color, color2, | |
347 brk, tail); | |
18937 | 348 |
30200 | 349 // brk is relative to dst_x |
350 // color = color left of brk | |
351 // color2 = color right of brk | |
352 int b_x0, b_y0, b_x1, b_y1; // visible part of the bitmap | |
353 int clip_x0, clip_y0, clip_x1, clip_y1; | |
354 int tmp; | |
355 ASS_Image *img; | |
29382
363310571aae
Cosmetics: make some variables constant to signify their intended use and,
eugeni
parents:
29381
diff
changeset
|
356 |
30200 | 357 dst_x += bm->left; |
358 dst_y += bm->top; | |
359 brk -= bm->left; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29178
diff
changeset
|
360 |
30200 | 361 // clipping |
362 clip_x0 = FFMINMAX(render_priv->state.clip_x0, 0, render_priv->width); | |
363 clip_y0 = FFMINMAX(render_priv->state.clip_y0, 0, render_priv->height); | |
364 clip_x1 = FFMINMAX(render_priv->state.clip_x1, 0, render_priv->width); | |
365 clip_y1 = FFMINMAX(render_priv->state.clip_y1, 0, render_priv->height); | |
366 b_x0 = 0; | |
367 b_y0 = 0; | |
368 b_x1 = bm->w; | |
369 b_y1 = bm->h; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29178
diff
changeset
|
370 |
30200 | 371 tmp = dst_x - clip_x0; |
372 if (tmp < 0) { | |
373 ass_msg(render_priv->library, MSGL_DBG2, "clip left"); | |
374 b_x0 = -tmp; | |
375 } | |
376 tmp = dst_y - clip_y0; | |
377 if (tmp < 0) { | |
378 ass_msg(render_priv->library, MSGL_DBG2, "clip top"); | |
379 b_y0 = -tmp; | |
380 } | |
381 tmp = clip_x1 - dst_x - bm->w; | |
382 if (tmp < 0) { | |
383 ass_msg(render_priv->library, MSGL_DBG2, "clip right"); | |
384 b_x1 = bm->w + tmp; | |
385 } | |
386 tmp = clip_y1 - dst_y - bm->h; | |
387 if (tmp < 0) { | |
388 ass_msg(render_priv->library, MSGL_DBG2, "clip bottom"); | |
389 b_y1 = bm->h + tmp; | |
390 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29178
diff
changeset
|
391 |
30200 | 392 if ((b_y0 >= b_y1) || (b_x0 >= b_x1)) |
393 return tail; | |
18937 | 394 |
30200 | 395 if (brk > b_x0) { // draw left part |
396 if (brk > b_x1) | |
397 brk = b_x1; | |
34295 | 398 img = my_draw_bitmap(bm->buffer + bm->stride * b_y0 + b_x0, |
399 brk - b_x0, b_y1 - b_y0, bm->stride, | |
30200 | 400 dst_x + b_x0, dst_y + b_y0, color); |
31853 | 401 if (!img) return tail; |
30200 | 402 *tail = img; |
403 tail = &img->next; | |
404 } | |
405 if (brk < b_x1) { // draw right part | |
406 if (brk < b_x0) | |
407 brk = b_x0; | |
34295 | 408 img = my_draw_bitmap(bm->buffer + bm->stride * b_y0 + brk, |
409 b_x1 - brk, b_y1 - b_y0, bm->stride, | |
30200 | 410 dst_x + brk, dst_y + b_y0, color2); |
31853 | 411 if (!img) return tail; |
30200 | 412 *tail = img; |
413 tail = &img->next; | |
414 } | |
415 return tail; | |
18937 | 416 } |
417 | |
418 /** | |
30200 | 419 * \brief Replace the bitmap buffer in ASS_Image with a copy |
420 * \param img ASS_Image to operate on | |
421 * \return pointer to old bitmap buffer | |
29381 | 422 */ |
30200 | 423 static unsigned char *clone_bitmap_buffer(ASS_Image *img) |
29381 | 424 { |
30200 | 425 unsigned char *old_bitmap = img->bitmap; |
426 int size = img->stride * (img->h - 1) + img->w; | |
427 img->bitmap = malloc(size); | |
428 memcpy(img->bitmap, old_bitmap, size); | |
429 return old_bitmap; | |
29381 | 430 } |
431 | |
432 /** | |
28789
a0ce88ba2557
Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents:
28788
diff
changeset
|
433 * \brief Calculate overlapping area of two consecutive bitmaps and in case they |
30200 | 434 * overlap, blend them together |
28789
a0ce88ba2557
Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents:
28788
diff
changeset
|
435 * Mainly useful for translucent glyphs and especially borders, to avoid the |
a0ce88ba2557
Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents:
28788
diff
changeset
|
436 * luminance adding up where they overlap (which looks ugly) |
a0ce88ba2557
Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents:
28788
diff
changeset
|
437 */ |
30200 | 438 static void |
439 render_overlap(ASS_Renderer *render_priv, ASS_Image **last_tail, | |
440 ASS_Image **tail) | |
441 { | |
442 int left, top, bottom, right; | |
443 int old_left, old_top, w, h, cur_left, cur_top; | |
444 int x, y, opos, cpos; | |
445 char m; | |
446 CompositeHashKey hk; | |
447 CompositeHashValue *hv; | |
448 CompositeHashValue chv; | |
449 int ax = (*last_tail)->dst_x; | |
450 int ay = (*last_tail)->dst_y; | |
451 int aw = (*last_tail)->w; | |
452 int as = (*last_tail)->stride; | |
453 int ah = (*last_tail)->h; | |
454 int bx = (*tail)->dst_x; | |
455 int by = (*tail)->dst_y; | |
456 int bw = (*tail)->w; | |
457 int bs = (*tail)->stride; | |
458 int bh = (*tail)->h; | |
459 unsigned char *a; | |
460 unsigned char *b; | |
28789
a0ce88ba2557
Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents:
28788
diff
changeset
|
461 |
30200 | 462 if ((*last_tail)->bitmap == (*tail)->bitmap) |
463 return; | |
28789
a0ce88ba2557
Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents:
28788
diff
changeset
|
464 |
30200 | 465 if ((*last_tail)->color != (*tail)->color) |
466 return; | |
28840
7d0ea9013974
Add a proper color check to the overlap compositing.
greg
parents:
28839
diff
changeset
|
467 |
30200 | 468 // Calculate overlap coordinates |
469 left = (ax > bx) ? ax : bx; | |
470 top = (ay > by) ? ay : by; | |
471 right = ((ax + aw) < (bx + bw)) ? (ax + aw) : (bx + bw); | |
472 bottom = ((ay + ah) < (by + bh)) ? (ay + ah) : (by + bh); | |
473 if ((right <= left) || (bottom <= top)) | |
474 return; | |
475 old_left = left - ax; | |
476 old_top = top - ay; | |
477 w = right - left; | |
478 h = bottom - top; | |
479 cur_left = left - bx; | |
480 cur_top = top - by; | |
28789
a0ce88ba2557
Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents:
28788
diff
changeset
|
481 |
30200 | 482 // Query cache |
483 hk.a = (*last_tail)->bitmap; | |
484 hk.b = (*tail)->bitmap; | |
485 hk.aw = aw; | |
486 hk.ah = ah; | |
487 hk.bw = bw; | |
488 hk.bh = bh; | |
489 hk.ax = ax; | |
490 hk.ay = ay; | |
491 hk.bx = bx; | |
492 hk.by = by; | |
493 hk.as = as; | |
494 hk.bs = bs; | |
34295 | 495 hv = ass_cache_get(render_priv->cache.composite_cache, &hk); |
30200 | 496 if (hv) { |
497 (*last_tail)->bitmap = hv->a; | |
498 (*tail)->bitmap = hv->b; | |
499 return; | |
500 } | |
501 // Allocate new bitmaps and copy over data | |
502 a = clone_bitmap_buffer(*last_tail); | |
503 b = clone_bitmap_buffer(*tail); | |
28789
a0ce88ba2557
Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents:
28788
diff
changeset
|
504 |
30200 | 505 // Blend overlapping area |
506 for (y = 0; y < h; y++) | |
507 for (x = 0; x < w; x++) { | |
508 opos = (old_top + y) * (as) + (old_left + x); | |
509 cpos = (cur_top + y) * (bs) + (cur_left + x); | |
510 m = FFMIN(a[opos] + b[cpos], 0xff); | |
511 (*last_tail)->bitmap[opos] = 0; | |
512 (*tail)->bitmap[cpos] = m; | |
513 } | |
28789
a0ce88ba2557
Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents:
28788
diff
changeset
|
514 |
30200 | 515 // Insert bitmaps into the cache |
516 chv.a = (*last_tail)->bitmap; | |
517 chv.b = (*tail)->bitmap; | |
34295 | 518 ass_cache_put(render_priv->cache.composite_cache, &hk, &chv); |
30200 | 519 } |
28789
a0ce88ba2557
Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents:
28788
diff
changeset
|
520 |
30200 | 521 static void free_list_add(ASS_Renderer *render_priv, void *object) |
522 { | |
523 if (!render_priv->free_head) { | |
524 render_priv->free_head = calloc(1, sizeof(FreeList)); | |
525 render_priv->free_head->object = object; | |
526 render_priv->free_tail = render_priv->free_head; | |
527 } else { | |
528 FreeList *l = calloc(1, sizeof(FreeList)); | |
529 l->object = object; | |
530 render_priv->free_tail->next = l; | |
531 render_priv->free_tail = render_priv->free_tail->next; | |
532 } | |
28789
a0ce88ba2557
Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents:
28788
diff
changeset
|
533 } |
a0ce88ba2557
Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents:
28788
diff
changeset
|
534 |
a0ce88ba2557
Combine adjacent overlapping, translucent glyph borders and shadows to
greg
parents:
28788
diff
changeset
|
535 /** |
30200 | 536 * Iterate through a list of bitmaps and blend with clip vector, if |
537 * applicable. The blended bitmaps are added to a free list which is freed | |
538 * at the start of a new frame. | |
18937 | 539 */ |
30200 | 540 static void blend_vector_clip(ASS_Renderer *render_priv, |
541 ASS_Image *head) | |
18937 | 542 { |
34295 | 543 FT_Outline *outline; |
544 Bitmap *clip_bm = NULL; | |
30200 | 545 ASS_Image *cur; |
546 ASS_Drawing *drawing = render_priv->state.clip_drawing; | |
34295 | 547 BitmapHashKey key; |
548 BitmapHashValue *val; | |
30200 | 549 int error; |
550 | |
551 if (!drawing) | |
552 return; | |
18937 | 553 |
31853 | 554 // Try to get mask from cache |
555 memset(&key, 0, sizeof(key)); | |
34295 | 556 key.type = BITMAP_CLIP; |
557 key.u.clip.text = drawing->text; | |
558 val = ass_cache_get(render_priv->cache.bitmap_cache, &key); | |
31853 | 559 |
560 if (val) { | |
34295 | 561 clip_bm = val->bm; |
31853 | 562 } else { |
34295 | 563 BitmapHashValue v; |
31853 | 564 |
565 // Not found in cache, parse and rasterize it | |
34295 | 566 outline = ass_drawing_parse(drawing, 1); |
567 if (!outline) { | |
31853 | 568 ass_msg(render_priv->library, MSGL_WARN, |
569 "Clip vector parsing failed. Skipping."); | |
570 goto blend_vector_error; | |
571 } | |
572 | |
573 // We need to translate the clip according to screen borders | |
574 if (render_priv->settings.left_margin != 0 || | |
575 render_priv->settings.top_margin != 0) { | |
576 FT_Vector trans = { | |
577 .x = int_to_d6(render_priv->settings.left_margin), | |
578 .y = -int_to_d6(render_priv->settings.top_margin), | |
579 }; | |
34295 | 580 FT_Outline_Translate(outline, trans.x, trans.y); |
31853 | 581 } |
582 | |
583 ass_msg(render_priv->library, MSGL_DBG2, | |
584 "Parsed vector clip: scales (%f, %f) string [%s]\n", | |
585 drawing->scale_x, drawing->scale_y, drawing->text); | |
586 | |
34295 | 587 clip_bm = outline_to_bitmap(render_priv->library, |
588 render_priv->ftlibrary, outline, 0); | |
589 if (clip_bm == NULL) { | |
31853 | 590 ass_msg(render_priv->library, MSGL_WARN, |
591 "Clip vector rasterization failed: %d. Skipping.", error); | |
592 } | |
593 | |
594 // Add to cache | |
595 memset(&v, 0, sizeof(v)); | |
34295 | 596 key.u.clip.text = strdup(drawing->text); |
597 v.bm = clip_bm; | |
598 ass_cache_put(render_priv->cache.bitmap_cache, &key, &v); | |
30200 | 599 } |
34295 | 600 blend_vector_error: |
19965 | 601 |
31853 | 602 if (!clip_bm) goto blend_vector_exit; |
19965 | 603 |
30200 | 604 // Iterate through bitmaps and blend/clip them |
605 for (cur = head; cur; cur = cur->next) { | |
606 int left, top, right, bottom, apos, bpos, y, x, w, h; | |
607 int ax, ay, aw, ah, as; | |
608 int bx, by, bw, bh, bs; | |
609 int aleft, atop, bleft, btop; | |
610 unsigned char *abuffer, *bbuffer, *nbuffer; | |
19965 | 611 |
30200 | 612 abuffer = cur->bitmap; |
34295 | 613 bbuffer = clip_bm->buffer; |
30200 | 614 ax = cur->dst_x; |
615 ay = cur->dst_y; | |
616 aw = cur->w; | |
617 ah = cur->h; | |
618 as = cur->stride; | |
619 bx = clip_bm->left; | |
34295 | 620 by = clip_bm->top; |
621 bw = clip_bm->w; | |
622 bh = clip_bm->h; | |
623 bs = clip_bm->stride; | |
18937 | 624 |
30200 | 625 // Calculate overlap coordinates |
626 left = (ax > bx) ? ax : bx; | |
627 top = (ay > by) ? ay : by; | |
628 right = ((ax + aw) < (bx + bw)) ? (ax + aw) : (bx + bw); | |
629 bottom = ((ay + ah) < (by + bh)) ? (ay + ah) : (by + bh); | |
630 aleft = left - ax; | |
631 atop = top - ay; | |
632 w = right - left; | |
633 h = bottom - top; | |
634 bleft = left - bx; | |
635 btop = top - by; | |
636 | |
637 if (render_priv->state.clip_drawing_mode) { | |
638 // Inverse clip | |
639 if (ax + aw < bx || ay + ah < by || ax > bx + bw || | |
640 ay > by + bh) { | |
641 continue; | |
642 } | |
643 | |
644 // Allocate new buffer and add to free list | |
645 nbuffer = malloc(as * ah); | |
31853 | 646 if (!nbuffer) goto blend_vector_exit; |
30200 | 647 free_list_add(render_priv, nbuffer); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29178
diff
changeset
|
648 |
30200 | 649 // Blend together |
650 memcpy(nbuffer, abuffer, as * (ah - 1) + aw); | |
651 for (y = 0; y < h; y++) | |
652 for (x = 0; x < w; x++) { | |
653 apos = (atop + y) * as + aleft + x; | |
654 bpos = (btop + y) * bs + bleft + x; | |
655 nbuffer[apos] = FFMAX(0, abuffer[apos] - bbuffer[bpos]); | |
656 } | |
657 } else { | |
658 // Regular clip | |
659 if (ax + aw < bx || ay + ah < by || ax > bx + bw || | |
660 ay > by + bh) { | |
661 cur->w = cur->h = 0; | |
662 continue; | |
663 } | |
18937 | 664 |
30200 | 665 // Allocate new buffer and add to free list |
666 nbuffer = calloc(as, ah); | |
31853 | 667 if (!nbuffer) goto blend_vector_exit; |
30200 | 668 free_list_add(render_priv, nbuffer); |
18937 | 669 |
30200 | 670 // Blend together |
671 for (y = 0; y < h; y++) | |
672 for (x = 0; x < w; x++) { | |
673 apos = (atop + y) * as + aleft + x; | |
674 bpos = (btop + y) * bs + bleft + x; | |
675 nbuffer[apos] = (abuffer[apos] * bbuffer[bpos] + 255) >> 8; | |
676 } | |
677 } | |
678 cur->bitmap = nbuffer; | |
679 } | |
18937 | 680 |
30200 | 681 blend_vector_exit: |
682 ass_drawing_free(render_priv->state.clip_drawing); | |
683 render_priv->state.clip_drawing = 0; | |
18937 | 684 } |
685 | |
34549 | 686 #define SKIP_SYMBOL(x) ((x) == 0 || (x) == '\n' || (x) == '\r') |
687 | |
18937 | 688 /** |
30200 | 689 * \brief Convert TextInfo struct to ASS_Image list |
690 * Splits glyphs in halves when needed (for \kf karaoke). | |
18937 | 691 */ |
31853 | 692 static ASS_Image *render_text(ASS_Renderer *render_priv, int dst_x, int dst_y) |
30200 | 693 { |
694 int pen_x, pen_y; | |
695 int i; | |
696 Bitmap *bm; | |
697 ASS_Image *head; | |
698 ASS_Image **tail = &head; | |
699 ASS_Image **last_tail = 0; | |
700 ASS_Image **here_tail = 0; | |
701 TextInfo *text_info = &render_priv->text_info; | |
702 | |
703 for (i = 0; i < text_info->length; ++i) { | |
704 GlyphInfo *info = text_info->glyphs + i; | |
34549 | 705 if (SKIP_SYMBOL(info->symbol) || !info->bm_s |
30200 | 706 || (info->shadow_x == 0 && info->shadow_y == 0) || info->skip) |
707 continue; | |
708 | |
34295 | 709 while (info) { |
710 if (!info->bm_s) { | |
711 info = info->next; | |
712 continue; | |
713 } | |
30200 | 714 |
34295 | 715 pen_x = |
716 dst_x + (info->pos.x >> 6) + | |
717 (int) (info->shadow_x * render_priv->border_scale); | |
718 pen_y = | |
719 dst_y + (info->pos.y >> 6) + | |
720 (int) (info->shadow_y * render_priv->border_scale); | |
721 bm = info->bm_s; | |
30200 | 722 |
34295 | 723 here_tail = tail; |
724 tail = | |
725 render_glyph(render_priv, bm, pen_x, pen_y, info->c[3], 0, | |
726 1000000, tail); | |
727 | |
728 if (last_tail && tail != here_tail && ((info->c[3] & 0xff) > 0)) | |
729 render_overlap(render_priv, last_tail, here_tail); | |
730 last_tail = here_tail; | |
731 | |
732 info = info->next; | |
733 } | |
30200 | 734 } |
735 | |
736 last_tail = 0; | |
737 for (i = 0; i < text_info->length; ++i) { | |
738 GlyphInfo *info = text_info->glyphs + i; | |
34549 | 739 if (SKIP_SYMBOL(info->symbol) || !info->bm_o |
30200 | 740 || info->skip) |
741 continue; | |
742 | |
34295 | 743 while (info) { |
744 if (!info->bm_o) { | |
745 info = info->next; | |
746 continue; | |
747 } | |
748 | |
749 pen_x = dst_x + (info->pos.x >> 6); | |
750 pen_y = dst_y + (info->pos.y >> 6); | |
751 bm = info->bm_o; | |
30200 | 752 |
34295 | 753 if ((info->effect_type == EF_KARAOKE_KO) |
754 && (info->effect_timing <= (info->bbox.xMax >> 6))) { | |
755 // do nothing | |
756 } else { | |
757 here_tail = tail; | |
758 tail = | |
759 render_glyph(render_priv, bm, pen_x, pen_y, info->c[2], | |
760 0, 1000000, tail); | |
761 if (last_tail && tail != here_tail && ((info->c[2] & 0xff) > 0)) | |
762 render_overlap(render_priv, last_tail, here_tail); | |
30200 | 763 |
34295 | 764 last_tail = here_tail; |
765 } | |
766 info = info->next; | |
30200 | 767 } |
768 } | |
769 | |
770 for (i = 0; i < text_info->length; ++i) { | |
771 GlyphInfo *info = text_info->glyphs + i; | |
34549 | 772 if (SKIP_SYMBOL(info->symbol) || !info->bm |
30200 | 773 || info->skip) |
774 continue; | |
775 | |
34295 | 776 while (info) { |
777 if (!info->bm) { | |
778 info = info->next; | |
779 continue; | |
780 } | |
781 | |
782 pen_x = dst_x + (info->pos.x >> 6); | |
783 pen_y = dst_y + (info->pos.y >> 6); | |
784 bm = info->bm; | |
30200 | 785 |
34295 | 786 if ((info->effect_type == EF_KARAOKE) |
787 || (info->effect_type == EF_KARAOKE_KO)) { | |
788 if (info->effect_timing > (info->bbox.xMax >> 6)) | |
789 tail = | |
790 render_glyph(render_priv, bm, pen_x, pen_y, | |
791 info->c[0], 0, 1000000, tail); | |
792 else | |
793 tail = | |
794 render_glyph(render_priv, bm, pen_x, pen_y, | |
795 info->c[1], 0, 1000000, tail); | |
796 } else if (info->effect_type == EF_KARAOKE_KF) { | |
30200 | 797 tail = |
34295 | 798 render_glyph(render_priv, bm, pen_x, pen_y, info->c[0], |
799 info->c[1], info->effect_timing, tail); | |
800 } else | |
30200 | 801 tail = |
34295 | 802 render_glyph(render_priv, bm, pen_x, pen_y, info->c[0], |
803 0, 1000000, tail); | |
804 info = info->next; | |
805 } | |
30200 | 806 } |
807 | |
808 *tail = 0; | |
809 blend_vector_clip(render_priv, head); | |
810 | |
811 return head; | |
28748
06983ef189b9
With pan-and-scan, keep positioned events in their original positions
eugeni
parents:
28719
diff
changeset
|
812 } |
29383
e9cab9f6ed62
Make sure clip coordinates are inside the screen area.
eugeni
parents:
29382
diff
changeset
|
813 |
34295 | 814 static void compute_string_bbox(TextInfo *text, DBBox *bbox) |
18937 | 815 { |
30200 | 816 int i; |
19556 | 817 |
34295 | 818 if (text->length > 0) { |
30200 | 819 bbox->xMin = 32000; |
820 bbox->xMax = -32000; | |
34295 | 821 bbox->yMin = -1 * text->lines[0].asc + d6_to_double(text->glyphs[0].pos.y); |
822 bbox->yMax = text->height - text->lines[0].asc + | |
823 d6_to_double(text->glyphs[0].pos.y); | |
19556 | 824 |
34295 | 825 for (i = 0; i < text->length; ++i) { |
826 GlyphInfo *info = text->glyphs + i; | |
827 if (info->skip) continue; | |
828 while (info) { | |
829 double s = d6_to_double(info->pos.x); | |
830 double e = s + d6_to_double(info->advance.x); | |
831 bbox->xMin = FFMIN(bbox->xMin, s); | |
832 bbox->xMax = FFMAX(bbox->xMax, e); | |
833 info = info->next; | |
834 } | |
30200 | 835 } |
836 } else | |
837 bbox->xMin = bbox->xMax = bbox->yMin = bbox->yMax = 0.; | |
19556 | 838 } |
839 | |
18937 | 840 /** |
19873 | 841 * \brief partially reset render_context to style values |
842 * Works like {\r}: resets some style overrides | |
843 */ | |
30200 | 844 void reset_render_context(ASS_Renderer *render_priv) |
19873 | 845 { |
30200 | 846 render_priv->state.c[0] = render_priv->state.style->PrimaryColour; |
847 render_priv->state.c[1] = render_priv->state.style->SecondaryColour; | |
848 render_priv->state.c[2] = render_priv->state.style->OutlineColour; | |
849 render_priv->state.c[3] = render_priv->state.style->BackColour; | |
850 render_priv->state.flags = | |
851 (render_priv->state.style->Underline ? DECO_UNDERLINE : 0) | | |
852 (render_priv->state.style->StrikeOut ? DECO_STRIKETHROUGH : 0); | |
853 render_priv->state.font_size = render_priv->state.style->FontSize; | |
854 | |
855 free(render_priv->state.family); | |
856 render_priv->state.family = NULL; | |
857 render_priv->state.family = strdup(render_priv->state.style->FontName); | |
858 render_priv->state.treat_family_as_pattern = | |
859 render_priv->state.style->treat_fontname_as_pattern; | |
860 render_priv->state.bold = render_priv->state.style->Bold; | |
861 render_priv->state.italic = render_priv->state.style->Italic; | |
862 update_font(render_priv); | |
863 | |
864 change_border(render_priv, -1., -1.); | |
865 render_priv->state.scale_x = render_priv->state.style->ScaleX; | |
866 render_priv->state.scale_y = render_priv->state.style->ScaleY; | |
867 render_priv->state.hspacing = render_priv->state.style->Spacing; | |
868 render_priv->state.be = 0; | |
869 render_priv->state.blur = 0.0; | |
870 render_priv->state.shadow_x = render_priv->state.style->Shadow; | |
871 render_priv->state.shadow_y = render_priv->state.style->Shadow; | |
872 render_priv->state.frx = render_priv->state.fry = 0.; | |
873 render_priv->state.frz = M_PI * render_priv->state.style->Angle / 180.; | |
874 render_priv->state.fax = render_priv->state.fay = 0.; | |
875 render_priv->state.wrap_style = render_priv->track->WrapStyle; | |
34295 | 876 render_priv->state.font_encoding = render_priv->state.style->Encoding; |
30200 | 877 } |
878 | |
879 /** | |
880 * \brief Start new event. Reset render_priv->state. | |
881 */ | |
882 static void | |
883 init_render_context(ASS_Renderer *render_priv, ASS_Event *event) | |
884 { | |
885 render_priv->state.event = event; | |
886 render_priv->state.style = render_priv->track->styles + event->Style; | |
34011 | 887 render_priv->state.parsed_tags = 0; |
30200 | 888 |
889 reset_render_context(render_priv); | |
19873 | 890 |
30200 | 891 render_priv->state.evt_type = EVENT_NORMAL; |
892 render_priv->state.alignment = render_priv->state.style->Alignment; | |
893 render_priv->state.pos_x = 0; | |
894 render_priv->state.pos_y = 0; | |
895 render_priv->state.org_x = 0; | |
896 render_priv->state.org_y = 0; | |
897 render_priv->state.have_origin = 0; | |
898 render_priv->state.clip_x0 = 0; | |
899 render_priv->state.clip_y0 = 0; | |
900 render_priv->state.clip_x1 = render_priv->track->PlayResX; | |
901 render_priv->state.clip_y1 = render_priv->track->PlayResY; | |
902 render_priv->state.clip_mode = 0; | |
903 render_priv->state.detect_collisions = 1; | |
904 render_priv->state.fade = 0; | |
905 render_priv->state.drawing_mode = 0; | |
906 render_priv->state.effect_type = EF_NONE; | |
907 render_priv->state.effect_timing = 0; | |
908 render_priv->state.effect_skip_timing = 0; | |
34295 | 909 render_priv->state.bm_run_id = 0; |
31875 | 910 ass_drawing_free(render_priv->state.drawing); |
34295 | 911 render_priv->state.drawing = ass_drawing_new(render_priv->library, |
912 render_priv->ftlibrary); | |
19873 | 913 |
30200 | 914 apply_transition_effects(render_priv, event); |
915 } | |
916 | |
917 static void free_render_context(ASS_Renderer *render_priv) | |
918 { | |
919 free(render_priv->state.family); | |
920 ass_drawing_free(render_priv->state.drawing); | |
921 | |
922 render_priv->state.family = NULL; | |
923 render_priv->state.drawing = NULL; | |
924 } | |
19873 | 925 |
30200 | 926 /* |
927 * Replace the outline of a glyph by a contour which makes up a simple | |
928 * opaque rectangle. | |
929 */ | |
34295 | 930 static void draw_opaque_box(ASS_Renderer *render_priv, int asc, int desc, |
931 FT_Outline *ol, FT_Vector advance, int sx, int sy) | |
18937 | 932 { |
30200 | 933 int i; |
34295 | 934 int adv = advance.x; |
30200 | 935 double scale_y = render_priv->state.scale_y; |
31853 | 936 double scale_x = render_priv->state.scale_x; |
30200 | 937 |
938 // to avoid gaps | |
939 sx = FFMAX(64, sx); | |
940 sy = FFMAX(64, sy); | |
941 | |
942 // Emulate the WTFish behavior of VSFilter, i.e. double-scale | |
943 // the sizes of the opaque box. | |
944 adv += double_to_d6(render_priv->state.hspacing * render_priv->font_scale | |
945 * scale_x); | |
946 adv *= scale_x; | |
947 sx *= scale_x; | |
948 sy *= scale_y; | |
949 desc *= scale_y; | |
950 desc += asc * (scale_y - 1.0); | |
951 | |
952 FT_Vector points[4] = { | |
953 { .x = -sx, .y = asc + sy }, | |
954 { .x = adv + sx, .y = asc + sy }, | |
955 { .x = adv + sx, .y = -desc - sy }, | |
956 { .x = -sx, .y = -desc - sy }, | |
957 }; | |
958 | |
34295 | 959 FT_Outline_New(render_priv->ftlibrary, 4, 1, ol); |
30200 | 960 |
961 ol->n_points = ol->n_contours = 0; | |
962 for (i = 0; i < 4; i++) { | |
963 ol->points[ol->n_points] = points[i]; | |
964 ol->tags[ol->n_points++] = 1; | |
965 } | |
966 ol->contours[ol->n_contours++] = ol->n_points - 1; | |
967 } | |
968 | |
969 /* | |
970 * Stroke an outline glyph in x/y direction. Applies various fixups to get | |
971 * around limitations of the FreeType stroker. | |
972 */ | |
34295 | 973 static void stroke_outline(ASS_Renderer *render_priv, FT_Outline *outline, |
974 int sx, int sy) | |
30200 | 975 { |
976 if (sx <= 0 && sy <= 0) | |
977 return; | |
978 | |
34295 | 979 fix_freetype_stroker(outline, sx, sy); |
30200 | 980 |
981 // Borders are equal; use the regular stroker | |
982 if (sx == sy && render_priv->state.stroker) { | |
983 int error; | |
34295 | 984 unsigned n_points, n_contours; |
985 | |
986 FT_StrokerBorder border = FT_Outline_GetOutsideBorder(outline); | |
987 error = FT_Stroker_ParseOutline(render_priv->state.stroker, outline, 0); | |
988 if (error) { | |
30200 | 989 ass_msg(render_priv->library, MSGL_WARN, |
34295 | 990 "FT_Stroker_ParseOutline failed, error: %d", error); |
991 } | |
992 error = FT_Stroker_GetBorderCounts(render_priv->state.stroker, border, | |
993 &n_points, &n_contours); | |
994 if (error) { | |
995 ass_msg(render_priv->library, MSGL_WARN, | |
996 "FT_Stroker_GetBorderCounts failed, error: %d", error); | |
997 } | |
998 FT_Outline_Done(render_priv->ftlibrary, outline); | |
999 FT_Outline_New(render_priv->ftlibrary, n_points, n_contours, outline); | |
1000 outline->n_points = outline->n_contours = 0; | |
1001 FT_Stroker_ExportBorder(render_priv->state.stroker, border, outline); | |
30200 | 1002 |
1003 // "Stroke" with the outline emboldener in two passes. | |
1004 // The outlines look uglier, but the emboldening never adds any points | |
1005 } else { | |
1006 int i; | |
1007 FT_Outline nol; | |
1008 | |
34295 | 1009 FT_Outline_New(render_priv->ftlibrary, outline->n_points, |
1010 outline->n_contours, &nol); | |
1011 FT_Outline_Copy(outline, &nol); | |
1012 | |
1013 FT_Outline_Embolden(outline, sx * 2); | |
1014 FT_Outline_Translate(outline, -sx, -sx); | |
30200 | 1015 FT_Outline_Embolden(&nol, sy * 2); |
1016 FT_Outline_Translate(&nol, -sy, -sy); | |
1017 | |
34295 | 1018 for (i = 0; i < outline->n_points; i++) |
1019 outline->points[i].y = nol.points[i].y; | |
30200 | 1020 |
1021 FT_Outline_Done(render_priv->ftlibrary, &nol); | |
1022 } | |
18937 | 1023 } |
1024 | |
23179 | 1025 /** |
31853 | 1026 * \brief Prepare glyph hash |
1027 */ | |
1028 static void | |
34295 | 1029 fill_glyph_hash(ASS_Renderer *priv, OutlineHashKey *outline_key, |
1030 GlyphInfo *info) | |
31853 | 1031 { |
34295 | 1032 if (info->drawing) { |
1033 DrawingHashKey *key = &outline_key->u.drawing; | |
1034 outline_key->type = OUTLINE_DRAWING; | |
1035 key->scale_x = double_to_d16(info->scale_x); | |
1036 key->scale_y = double_to_d16(info->scale_y); | |
1037 key->outline.x = double_to_d16(info->border_x); | |
1038 key->outline.y = double_to_d16(info->border_y); | |
31853 | 1039 key->border_style = priv->state.style->BorderStyle; |
34295 | 1040 key->hash = info->drawing->hash; |
1041 key->text = info->drawing->text; | |
1042 key->pbo = info->drawing->pbo; | |
1043 key->scale = info->drawing->scale; | |
31853 | 1044 } else { |
34295 | 1045 GlyphHashKey *key = &outline_key->u.glyph; |
1046 outline_key->type = OUTLINE_GLYPH; | |
1047 key->font = info->font; | |
1048 key->size = info->font_size; | |
1049 key->face_index = info->face_index; | |
1050 key->glyph_index = info->glyph_index; | |
1051 key->bold = info->bold; | |
1052 key->italic = info->italic; | |
1053 key->scale_x = double_to_d16(info->scale_x); | |
1054 key->scale_y = double_to_d16(info->scale_y); | |
1055 key->outline.x = double_to_d16(info->border_x); | |
1056 key->outline.y = double_to_d16(info->border_y); | |
1057 key->flags = info->flags; | |
31853 | 1058 key->border_style = priv->state.style->BorderStyle; |
1059 } | |
1060 } | |
1061 | |
1062 /** | |
23179 | 1063 * \brief Get normal and outline (border) glyphs |
1064 * \param info out: struct filled with extracted data | |
1065 * Tries to get both glyphs from cache. | |
1066 * If they can't be found, gets a glyph from font face, generates outline with FT_Stroker, | |
1067 * and add them to cache. | |
1068 * The glyphs are returned in info->glyph and info->outline_glyph | |
1069 */ | |
30200 | 1070 static void |
34295 | 1071 get_outline_glyph(ASS_Renderer *priv, GlyphInfo *info) |
23021
a81c390d4a22
Move outline glyph generation to a separate function, using outline glyph
eugeni
parents:
23017
diff
changeset
|
1072 { |
34295 | 1073 OutlineHashValue *val; |
1074 OutlineHashKey key; | |
31853 | 1075 |
34295 | 1076 memset(&info->hash_key, 0, sizeof(key)); |
23021
a81c390d4a22
Move outline glyph generation to a separate function, using outline glyph
eugeni
parents:
23017
diff
changeset
|
1077 |
34295 | 1078 fill_glyph_hash(priv, &key, info); |
1079 val = ass_cache_get(priv->cache.outline_cache, &key); | |
1080 | |
1081 if (!val) { | |
1082 OutlineHashValue v; | |
1083 memset(&v, 0, sizeof(v)); | |
1084 | |
1085 if (info->drawing) { | |
1086 ASS_Drawing *drawing = info->drawing; | |
1087 ass_drawing_hash(drawing); | |
30200 | 1088 if(!ass_drawing_parse(drawing, 0)) |
1089 return; | |
34295 | 1090 outline_copy(priv->ftlibrary, &drawing->outline, |
1091 &v.outline); | |
1092 v.advance.x = drawing->advance.x; | |
1093 v.advance.y = drawing->advance.y; | |
1094 v.asc = drawing->asc; | |
1095 v.desc = drawing->desc; | |
1096 key.u.drawing.text = strdup(drawing->text); | |
30200 | 1097 } else { |
34295 | 1098 ass_face_set_size(info->font->faces[info->face_index], |
1099 info->font_size); | |
1100 ass_font_set_transform(info->font, info->scale_x, | |
1101 info->scale_y, NULL); | |
1102 FT_Glyph glyph = | |
1103 ass_font_get_glyph(priv->fontconfig_priv, info->font, | |
1104 info->symbol, info->face_index, info->glyph_index, | |
1105 priv->settings.hinting, info->flags); | |
1106 if (glyph != NULL) { | |
1107 outline_copy(priv->ftlibrary, | |
1108 &((FT_OutlineGlyph)glyph)->outline, &v.outline); | |
1109 if (priv->settings.shaper == ASS_SHAPING_SIMPLE) { | |
1110 v.advance.x = d16_to_d6(glyph->advance.x); | |
1111 v.advance.y = d16_to_d6(glyph->advance.y); | |
1112 } | |
1113 FT_Done_Glyph(glyph); | |
1114 ass_font_get_asc_desc(info->font, info->symbol, | |
1115 &v.asc, &v.desc); | |
1116 v.asc *= info->scale_y; | |
1117 v.desc *= info->scale_y; | |
1118 } | |
30200 | 1119 } |
34295 | 1120 |
1121 if (!v.outline) | |
30200 | 1122 return; |
31853 | 1123 |
34295 | 1124 FT_Outline_Get_CBox(v.outline, &v.bbox_scaled); |
1125 | |
1126 if (priv->state.style->BorderStyle == 3 && | |
1127 (info->border_x > 0 || info->border_y > 0)) { | |
1128 FT_Vector advance; | |
1129 | |
1130 v.border = calloc(1, sizeof(FT_Outline)); | |
23021
a81c390d4a22
Move outline glyph generation to a separate function, using outline glyph
eugeni
parents:
23017
diff
changeset
|
1131 |
34295 | 1132 if (priv->settings.shaper == ASS_SHAPING_SIMPLE || info->drawing) |
1133 advance = v.advance; | |
1134 else | |
1135 advance = info->advance; | |
23025
ab0943242d1a
Store outline_glyph (glyph border) in glyph cache.
eugeni
parents:
23024
diff
changeset
|
1136 |
34295 | 1137 draw_opaque_box(priv, v.asc, v.desc, v.border, advance, |
1138 double_to_d6(info->border_x * priv->border_scale), | |
1139 double_to_d6(info->border_y * priv->border_scale)); | |
1140 | |
1141 } else if ((info->border_x > 0 || info->border_y > 0) | |
1142 && double_to_d6(info->scale_x) && double_to_d6(info->scale_y)) { | |
1143 | |
1144 outline_copy(priv->ftlibrary, v.outline, &v.border); | |
1145 stroke_outline(priv, v.border, | |
1146 double_to_d6(info->border_x * priv->border_scale), | |
1147 double_to_d6(info->border_y * priv->border_scale)); | |
30200 | 1148 } |
1149 | |
34295 | 1150 v.lib = priv->ftlibrary; |
1151 val = ass_cache_put(priv->cache.outline_cache, &key, &v); | |
30200 | 1152 } |
34295 | 1153 |
1154 info->hash_key.u.outline.outline = val; | |
1155 info->outline = val->outline; | |
1156 info->border = val->border; | |
1157 info->bbox = val->bbox_scaled; | |
1158 if (info->drawing || priv->settings.shaper == ASS_SHAPING_SIMPLE) { | |
1159 info->cluster_advance.x = info->advance.x = val->advance.x; | |
1160 info->cluster_advance.y = info->advance.y = val->advance.y; | |
1161 } | |
1162 info->asc = val->asc; | |
1163 info->desc = val->desc; | |
1164 | |
1165 ass_drawing_free(info->drawing); | |
23021
a81c390d4a22
Move outline glyph generation to a separate function, using outline glyph
eugeni
parents:
23017
diff
changeset
|
1166 } |
a81c390d4a22
Move outline glyph generation to a separate function, using outline glyph
eugeni
parents:
23017
diff
changeset
|
1167 |
31853 | 1168 /** |
1169 * \brief Apply transformation to outline points of a glyph | |
1170 * Applies rotations given by frx, fry and frz and projects the points back | |
1171 * onto the screen plane. | |
1172 */ | |
1173 static void | |
34295 | 1174 transform_3d_points(FT_Vector shift, FT_Outline *outline, double frx, double fry, |
31853 | 1175 double frz, double fax, double fay, double scale, |
1176 int yshift) | |
1177 { | |
1178 double sx = sin(frx); | |
1179 double sy = sin(fry); | |
1180 double sz = sin(frz); | |
1181 double cx = cos(frx); | |
1182 double cy = cos(fry); | |
1183 double cz = cos(frz); | |
1184 FT_Vector *p = outline->points; | |
1185 double x, y, z, xx, yy, zz; | |
1186 int i, dist; | |
1187 | |
1188 dist = 20000 * scale; | |
1189 for (i = 0; i < outline->n_points; i++) { | |
1190 x = (double) p[i].x + shift.x + (fax * (yshift - p[i].y)); | |
1191 y = (double) p[i].y + shift.y + (-fay * p[i].x); | |
1192 z = 0.; | |
1193 | |
1194 xx = x * cz + y * sz; | |
1195 yy = -(x * sz - y * cz); | |
1196 zz = z; | |
1197 | |
1198 x = xx; | |
1199 y = yy * cx + zz * sx; | |
1200 z = yy * sx - zz * cx; | |
1201 | |
1202 xx = x * cy + z * sy; | |
1203 yy = y; | |
1204 zz = x * sy - z * cy; | |
1205 | |
1206 zz = FFMAX(zz, 1000 - dist); | |
1207 | |
1208 x = (xx * dist) / (zz + dist); | |
1209 y = (yy * dist) / (zz + dist); | |
1210 p[i].x = x - shift.x + 0.5; | |
1211 p[i].y = y - shift.y + 0.5; | |
1212 } | |
1213 } | |
1214 | |
1215 /** | |
1216 * \brief Apply 3d transformation to several objects | |
1217 * \param shift FreeType vector | |
1218 * \param glyph FreeType glyph | |
1219 * \param glyph2 FreeType glyph | |
1220 * \param frx x-axis rotation angle | |
1221 * \param fry y-axis rotation angle | |
1222 * \param frz z-axis rotation angle | |
1223 * Rotates both glyphs by frx, fry and frz. Shift vector is added before rotation and subtracted after it. | |
1224 */ | |
1225 static void | |
34295 | 1226 transform_3d(FT_Vector shift, FT_Outline *outline, FT_Outline *border, |
31853 | 1227 double frx, double fry, double frz, double fax, double fay, |
1228 double scale, int yshift) | |
1229 { | |
1230 frx = -frx; | |
1231 frz = -frz; | |
1232 if (frx != 0. || fry != 0. || frz != 0. || fax != 0. || fay != 0.) { | |
34295 | 1233 if (outline) |
1234 transform_3d_points(shift, outline, frx, fry, frz, | |
31853 | 1235 fax, fay, scale, yshift); |
1236 | |
34295 | 1237 if (border) |
1238 transform_3d_points(shift, border, frx, fry, frz, | |
31853 | 1239 fax, fay, scale, yshift); |
1240 } | |
1241 } | |
23173 | 1242 |
18937 | 1243 /** |
23179 | 1244 * \brief Get bitmaps for a glyph |
1245 * \param info glyph info | |
1246 * Tries to get glyph bitmaps from bitmap cache. | |
1247 * If they can't be found, they are generated by rotating and rendering the glyph. | |
1248 * After that, bitmaps are added to the cache. | |
1249 * They are returned in info->bm (glyph), info->bm_o (outline) and info->bm_s (shadow). | |
18937 | 1250 */ |
30200 | 1251 static void |
1252 get_bitmap_glyph(ASS_Renderer *render_priv, GlyphInfo *info) | |
18937 | 1253 { |
30200 | 1254 BitmapHashValue *val; |
34295 | 1255 OutlineBitmapHashKey *key = &info->hash_key.u.outline; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29178
diff
changeset
|
1256 |
34295 | 1257 if (!info->outline || info->symbol == '\n' || info->symbol == 0 || info->skip) |
1258 return; | |
1259 | |
1260 val = ass_cache_get(render_priv->cache.bitmap_cache, &info->hash_key); | |
1261 | |
1262 if (!val) { | |
30200 | 1263 FT_Vector shift; |
1264 BitmapHashValue hash_val; | |
1265 int error; | |
1266 double fax_scaled, fay_scaled; | |
34295 | 1267 FT_Outline *outline, *border; |
1268 double scale_x = render_priv->font_scale_x; | |
1269 | |
1270 hash_val.bm = hash_val.bm_o = hash_val.bm_s = 0; | |
1271 | |
1272 outline_copy(render_priv->ftlibrary, info->outline, &outline); | |
1273 outline_copy(render_priv->ftlibrary, info->border, &border); | |
31853 | 1274 |
34295 | 1275 // calculating rotation shift vector (from rotation origin to the glyph basepoint) |
1276 shift.x = key->shift_x; | |
1277 shift.y = key->shift_y; | |
1278 fax_scaled = info->fax * render_priv->state.scale_x; | |
1279 fay_scaled = info->fay * render_priv->state.scale_y; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29178
diff
changeset
|
1280 |
34295 | 1281 // apply rotation |
1282 transform_3d(shift, outline, border, | |
1283 info->frx, info->fry, info->frz, fax_scaled, | |
1284 fay_scaled, render_priv->font_scale, info->asc); | |
1285 | |
1286 // PAR correction scaling | |
1287 FT_Matrix m = { double_to_d16(scale_x), 0, | |
1288 0, double_to_d16(1.0) }; | |
31853 | 1289 |
34295 | 1290 // subpixel shift |
1291 if (outline) { | |
1292 if (scale_x != 1.0) | |
1293 FT_Outline_Transform(outline, &m); | |
1294 FT_Outline_Translate(outline, key->advance.x, -key->advance.y); | |
1295 } | |
1296 if (border) { | |
1297 if (scale_x != 1.0) | |
1298 FT_Outline_Transform(border, &m); | |
1299 FT_Outline_Translate(border, key->advance.x, -key->advance.y); | |
1300 } | |
23177
134a2baca452
Move glyph_to_bitmap() call and outline glyph deallocation to
eugeni
parents:
23175
diff
changeset
|
1301 |
34295 | 1302 // render glyph |
1303 error = outline_to_bitmap3(render_priv->library, | |
1304 render_priv->synth_priv, | |
1305 render_priv->ftlibrary, | |
1306 outline, border, | |
1307 &hash_val.bm, &hash_val.bm_o, | |
1308 &hash_val.bm_s, info->be, | |
1309 info->blur * render_priv->border_scale, | |
1310 key->shadow_offset, | |
1311 render_priv->state.style->BorderStyle); | |
1312 if (error) | |
1313 info->symbol = 0; | |
31853 | 1314 |
34295 | 1315 val = ass_cache_put(render_priv->cache.bitmap_cache, &info->hash_key, |
1316 &hash_val); | |
1317 | |
1318 outline_free(render_priv->ftlibrary, outline); | |
1319 outline_free(render_priv->ftlibrary, border); | |
30200 | 1320 } |
34011 | 1321 |
34295 | 1322 info->bm = val->bm; |
1323 info->bm_o = val->bm_o; | |
1324 info->bm_s = val->bm_s; | |
1325 | |
34011 | 1326 // VSFilter compatibility: invisible fill and no border? |
1327 // In this case no shadow is supposed to be rendered. | |
34295 | 1328 if (!info->border && (info->c[0] & 0xFF) == 0xFF) |
34011 | 1329 info->bm_s = 0; |
18937 | 1330 } |
1331 | |
1332 /** | |
19932
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1333 * This function goes through text_info and calculates text parameters. |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1334 * The following text_info fields are filled: |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1335 * height |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1336 * lines[].height |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1337 * lines[].asc |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1338 * lines[].desc |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1339 */ |
30200 | 1340 static void measure_text(ASS_Renderer *render_priv) |
1341 { | |
1342 TextInfo *text_info = &render_priv->text_info; | |
1343 int cur_line = 0; | |
1344 double max_asc = 0., max_desc = 0.; | |
1345 GlyphInfo *last = NULL; | |
1346 int i; | |
1347 int empty_line = 1; | |
1348 text_info->height = 0.; | |
1349 for (i = 0; i < text_info->length + 1; ++i) { | |
1350 if ((i == text_info->length) || text_info->glyphs[i].linebreak) { | |
1351 if (empty_line && cur_line > 0 && last && i < text_info->length) { | |
1352 max_asc = d6_to_double(last->asc) / 2.0; | |
1353 max_desc = d6_to_double(last->desc) / 2.0; | |
1354 } | |
1355 text_info->lines[cur_line].asc = max_asc; | |
1356 text_info->lines[cur_line].desc = max_desc; | |
1357 text_info->height += max_asc + max_desc; | |
1358 cur_line++; | |
1359 max_asc = max_desc = 0.; | |
1360 empty_line = 1; | |
1361 } else | |
1362 empty_line = 0; | |
1363 if (i < text_info->length) { | |
1364 GlyphInfo *cur = text_info->glyphs + i; | |
1365 if (d6_to_double(cur->asc) > max_asc) | |
1366 max_asc = d6_to_double(cur->asc); | |
1367 if (d6_to_double(cur->desc) > max_desc) | |
1368 max_desc = d6_to_double(cur->desc); | |
1369 if (cur->symbol != '\n' && cur->symbol != 0) | |
1370 last = cur; | |
1371 } | |
1372 } | |
1373 text_info->height += | |
1374 (text_info->n_lines - | |
1375 1) * render_priv->settings.line_spacing; | |
1376 } | |
1377 | |
1378 /** | |
1379 * Mark extra whitespace for later removal. | |
1380 */ | |
1381 #define IS_WHITESPACE(x) ((x->symbol == ' ' || x->symbol == '\n') \ | |
1382 && !x->linebreak) | |
1383 static void trim_whitespace(ASS_Renderer *render_priv) | |
19932
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1384 { |
30200 | 1385 int i, j; |
1386 GlyphInfo *cur; | |
1387 TextInfo *ti = &render_priv->text_info; | |
1388 | |
1389 // Mark trailing spaces | |
1390 i = ti->length - 1; | |
1391 cur = ti->glyphs + i; | |
1392 while (i && IS_WHITESPACE(cur)) { | |
1393 cur->skip++; | |
1394 cur = ti->glyphs + --i; | |
1395 } | |
1396 | |
1397 // Mark leading whitespace | |
1398 i = 0; | |
1399 cur = ti->glyphs; | |
1400 while (i < ti->length && IS_WHITESPACE(cur)) { | |
1401 cur->skip++; | |
1402 cur = ti->glyphs + ++i; | |
1403 } | |
1404 | |
1405 // Mark all extraneous whitespace inbetween | |
1406 for (i = 0; i < ti->length; ++i) { | |
1407 cur = ti->glyphs + i; | |
1408 if (cur->linebreak) { | |
1409 // Mark whitespace before | |
1410 j = i - 1; | |
1411 cur = ti->glyphs + j; | |
1412 while (j && IS_WHITESPACE(cur)) { | |
1413 cur->skip++; | |
1414 cur = ti->glyphs + --j; | |
1415 } | |
1416 // A break itself can contain a whitespace, too | |
1417 cur = ti->glyphs + i; | |
34011 | 1418 if (cur->symbol == ' ') { |
30200 | 1419 cur->skip++; |
34011 | 1420 // Mark whitespace after |
1421 j = i + 1; | |
1422 cur = ti->glyphs + j; | |
1423 while (j < ti->length && IS_WHITESPACE(cur)) { | |
1424 cur->skip++; | |
1425 cur = ti->glyphs + ++j; | |
1426 } | |
1427 i = j - 1; | |
30200 | 1428 } |
1429 } | |
1430 } | |
19932
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1431 } |
30200 | 1432 #undef IS_WHITESPACE |
19932
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1433 |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1434 /** |
18937 | 1435 * \brief rearrange text between lines |
1436 * \param max_text_width maximal text line width in pixels | |
1437 * The algo is similar to the one in libvo/sub.c: | |
1438 * 1. Place text, wrapping it when current line is full | |
1439 * 2. Try moving words from the end of a line to the beginning of the next one while it reduces | |
1440 * the difference in lengths between this two lines. | |
1441 * The result may not be optimal, but usually is good enough. | |
30200 | 1442 * |
31853 | 1443 * FIXME: implement style 0 and 3 correctly |
18937 | 1444 */ |
30200 | 1445 static void |
1446 wrap_lines_smart(ASS_Renderer *render_priv, double max_text_width) | |
18937 | 1447 { |
30200 | 1448 int i; |
1449 GlyphInfo *cur, *s1, *e1, *s2, *s3, *w; | |
1450 int last_space; | |
1451 int break_type; | |
1452 int exit; | |
1453 double pen_shift_x; | |
1454 double pen_shift_y; | |
1455 int cur_line; | |
34295 | 1456 int run_offset; |
30200 | 1457 TextInfo *text_info = &render_priv->text_info; |
18937 | 1458 |
30200 | 1459 last_space = -1; |
1460 text_info->n_lines = 1; | |
1461 break_type = 0; | |
1462 s1 = text_info->glyphs; // current line start | |
1463 for (i = 0; i < text_info->length; ++i) { | |
34011 | 1464 int break_at = -1; |
30200 | 1465 double s_offset, len; |
1466 cur = text_info->glyphs + i; | |
1467 s_offset = d6_to_double(s1->bbox.xMin + s1->pos.x); | |
1468 len = d6_to_double(cur->bbox.xMax + cur->pos.x) - s_offset; | |
18937 | 1469 |
30200 | 1470 if (cur->symbol == '\n') { |
1471 break_type = 2; | |
1472 break_at = i; | |
1473 ass_msg(render_priv->library, MSGL_DBG2, | |
1474 "forced line break at %d", break_at); | |
34011 | 1475 } else if (cur->symbol == ' ') { |
1476 last_space = i; | |
1477 } else if (len >= max_text_width | |
1478 && (render_priv->state.wrap_style != 2)) { | |
30200 | 1479 break_type = 1; |
1480 break_at = last_space; | |
34011 | 1481 if (break_at >= 0) |
1482 ass_msg(render_priv->library, MSGL_DBG2, "line break at %d", | |
1483 break_at); | |
30200 | 1484 } |
18937 | 1485 |
30200 | 1486 if (break_at != -1) { |
1487 // need to use one more line | |
1488 // marking break_at+1 as start of a new line | |
1489 int lead = break_at + 1; // the first symbol of the new line | |
1490 if (text_info->n_lines >= text_info->max_lines) { | |
1491 // Raise maximum number of lines | |
1492 text_info->max_lines *= 2; | |
1493 text_info->lines = realloc(text_info->lines, | |
1494 sizeof(LineInfo) * | |
1495 text_info->max_lines); | |
1496 } | |
34295 | 1497 if (lead < text_info->length) { |
30200 | 1498 text_info->glyphs[lead].linebreak = break_type; |
34295 | 1499 last_space = -1; |
1500 s1 = text_info->glyphs + lead; | |
1501 s_offset = d6_to_double(s1->bbox.xMin + s1->pos.x); | |
1502 text_info->n_lines++; | |
1503 } | |
30200 | 1504 } |
1505 } | |
18937 | 1506 #define DIFF(x,y) (((x) < (y)) ? (y - x) : (x - y)) |
30200 | 1507 exit = 0; |
1508 while (!exit && render_priv->state.wrap_style != 1) { | |
1509 exit = 1; | |
1510 w = s3 = text_info->glyphs; | |
1511 s1 = s2 = 0; | |
1512 for (i = 0; i <= text_info->length; ++i) { | |
1513 cur = text_info->glyphs + i; | |
1514 if ((i == text_info->length) || cur->linebreak) { | |
1515 s1 = s2; | |
1516 s2 = s3; | |
1517 s3 = cur; | |
1518 if (s1 && (s2->linebreak == 1)) { // have at least 2 lines, and linebreak is 'soft' | |
1519 double l1, l2, l1_new, l2_new; | |
18937 | 1520 |
30200 | 1521 w = s2; |
1522 do { | |
1523 --w; | |
1524 } while ((w > s1) && (w->symbol == ' ')); | |
1525 while ((w > s1) && (w->symbol != ' ')) { | |
1526 --w; | |
1527 } | |
1528 e1 = w; | |
1529 while ((e1 > s1) && (e1->symbol == ' ')) { | |
1530 --e1; | |
1531 } | |
1532 if (w->symbol == ' ') | |
1533 ++w; | |
18937 | 1534 |
30200 | 1535 l1 = d6_to_double(((s2 - 1)->bbox.xMax + (s2 - 1)->pos.x) - |
1536 (s1->bbox.xMin + s1->pos.x)); | |
1537 l2 = d6_to_double(((s3 - 1)->bbox.xMax + (s3 - 1)->pos.x) - | |
1538 (s2->bbox.xMin + s2->pos.x)); | |
1539 l1_new = d6_to_double( | |
1540 (e1->bbox.xMax + e1->pos.x) - | |
1541 (s1->bbox.xMin + s1->pos.x)); | |
1542 l2_new = d6_to_double( | |
1543 ((s3 - 1)->bbox.xMax + (s3 - 1)->pos.x) - | |
1544 (w->bbox.xMin + w->pos.x)); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29178
diff
changeset
|
1545 |
30200 | 1546 if (DIFF(l1_new, l2_new) < DIFF(l1, l2)) { |
1547 w->linebreak = 1; | |
1548 s2->linebreak = 0; | |
1549 exit = 0; | |
1550 } | |
1551 } | |
1552 } | |
1553 if (i == text_info->length) | |
1554 break; | |
1555 } | |
1556 | |
1557 } | |
1558 assert(text_info->n_lines >= 1); | |
18937 | 1559 #undef DIFF |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29178
diff
changeset
|
1560 |
30200 | 1561 measure_text(render_priv); |
1562 trim_whitespace(render_priv); | |
1563 | |
1564 pen_shift_x = 0.; | |
1565 pen_shift_y = 0.; | |
1566 cur_line = 1; | |
34295 | 1567 run_offset = 0; |
30200 | 1568 |
1569 i = 0; | |
1570 cur = text_info->glyphs + i; | |
1571 while (i < text_info->length && cur->skip) | |
1572 cur = text_info->glyphs + ++i; | |
1573 pen_shift_x = d6_to_double(-cur->pos.x); | |
19932
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1574 |
30200 | 1575 for (i = 0; i < text_info->length; ++i) { |
1576 cur = text_info->glyphs + i; | |
1577 if (cur->linebreak) { | |
1578 while (i < text_info->length && cur->skip && cur->symbol != '\n') | |
1579 cur = text_info->glyphs + ++i; | |
1580 double height = | |
1581 text_info->lines[cur_line - 1].desc + | |
1582 text_info->lines[cur_line].asc; | |
34295 | 1583 text_info->lines[cur_line - 1].len = i - |
1584 text_info->lines[cur_line - 1].offset; | |
1585 text_info->lines[cur_line].offset = i; | |
30200 | 1586 cur_line++; |
34295 | 1587 run_offset++; |
30200 | 1588 pen_shift_x = d6_to_double(-cur->pos.x); |
1589 pen_shift_y += height + render_priv->settings.line_spacing; | |
1590 ass_msg(render_priv->library, MSGL_DBG2, | |
1591 "shifting from %d to %d by (%f, %f)", i, | |
1592 text_info->length - 1, pen_shift_x, pen_shift_y); | |
1593 } | |
34295 | 1594 cur->bm_run_id += run_offset; |
30200 | 1595 cur->pos.x += double_to_d6(pen_shift_x); |
1596 cur->pos.y += double_to_d6(pen_shift_y); | |
1597 } | |
34295 | 1598 text_info->lines[cur_line - 1].len = |
1599 text_info->length - text_info->lines[cur_line - 1].offset; | |
18937 | 1600 |
34295 | 1601 #if 0 |
1602 // print line info | |
1603 for (i = 0; i < text_info->n_lines; i++) { | |
1604 printf("line %d offset %d length %d\n", i, text_info->lines[i].offset, | |
1605 text_info->lines[i].len); | |
30200 | 1606 } |
34295 | 1607 #endif |
18937 | 1608 } |
1609 | |
1610 /** | |
20294
4d7c8478e523
Move base point calculation to a separate function. Will be reused soon.
eugeni
parents:
20293
diff
changeset
|
1611 * \brief Calculate base point for positioning and rotation |
4d7c8478e523
Move base point calculation to a separate function. Will be reused soon.
eugeni
parents:
20293
diff
changeset
|
1612 * \param bbox text bbox |
4d7c8478e523
Move base point calculation to a separate function. Will be reused soon.
eugeni
parents:
20293
diff
changeset
|
1613 * \param alignment alignment |
4d7c8478e523
Move base point calculation to a separate function. Will be reused soon.
eugeni
parents:
20293
diff
changeset
|
1614 * \param bx, by out: base point coordinates |
4d7c8478e523
Move base point calculation to a separate function. Will be reused soon.
eugeni
parents:
20293
diff
changeset
|
1615 */ |
30200 | 1616 static void get_base_point(DBBox *bbox, int alignment, double *bx, double *by) |
20294
4d7c8478e523
Move base point calculation to a separate function. Will be reused soon.
eugeni
parents:
20293
diff
changeset
|
1617 { |
30200 | 1618 const int halign = alignment & 3; |
1619 const int valign = alignment & 12; | |
1620 if (bx) | |
1621 switch (halign) { | |
1622 case HALIGN_LEFT: | |
1623 *bx = bbox->xMin; | |
1624 break; | |
1625 case HALIGN_CENTER: | |
1626 *bx = (bbox->xMax + bbox->xMin) / 2.0; | |
1627 break; | |
1628 case HALIGN_RIGHT: | |
1629 *bx = bbox->xMax; | |
1630 break; | |
1631 } | |
1632 if (by) | |
1633 switch (valign) { | |
1634 case VALIGN_TOP: | |
1635 *by = bbox->yMin; | |
1636 break; | |
1637 case VALIGN_CENTER: | |
1638 *by = (bbox->yMax + bbox->yMin) / 2.0; | |
1639 break; | |
1640 case VALIGN_SUB: | |
1641 *by = bbox->yMax; | |
1642 break; | |
1643 } | |
20294
4d7c8478e523
Move base point calculation to a separate function. Will be reused soon.
eugeni
parents:
20293
diff
changeset
|
1644 } |
4d7c8478e523
Move base point calculation to a separate function. Will be reused soon.
eugeni
parents:
20293
diff
changeset
|
1645 |
4d7c8478e523
Move base point calculation to a separate function. Will be reused soon.
eugeni
parents:
20293
diff
changeset
|
1646 /** |
31853 | 1647 * Prepare bitmap hash key of a glyph |
22215
fb365c2b3d05
Implement \frx and \fry (and reimplement \frz) as 3d rotations.
eugeni
parents:
22214
diff
changeset
|
1648 */ |
30200 | 1649 static void |
34295 | 1650 fill_bitmap_hash(ASS_Renderer *priv, GlyphInfo *info, |
1651 OutlineBitmapHashKey *hash_key) | |
30200 | 1652 { |
34295 | 1653 hash_key->frx = rot_key(info->frx); |
1654 hash_key->fry = rot_key(info->fry); | |
1655 hash_key->frz = rot_key(info->frz); | |
1656 hash_key->fax = double_to_d16(info->fax); | |
1657 hash_key->fay = double_to_d16(info->fay); | |
1658 hash_key->be = info->be; | |
1659 hash_key->blur = info->blur; | |
31853 | 1660 hash_key->shadow_offset.x = double_to_d6( |
34295 | 1661 info->shadow_x * priv->border_scale - |
1662 (int) (info->shadow_x * priv->border_scale)); | |
31853 | 1663 hash_key->shadow_offset.y = double_to_d6( |
34295 | 1664 info->shadow_y * priv->border_scale - |
1665 (int) (info->shadow_y * priv->border_scale)); | |
22215
fb365c2b3d05
Implement \frx and \fry (and reimplement \frz) as 3d rotations.
eugeni
parents:
22214
diff
changeset
|
1666 } |
fb365c2b3d05
Implement \frx and \fry (and reimplement \frz) as 3d rotations.
eugeni
parents:
22214
diff
changeset
|
1667 |
fb365c2b3d05
Implement \frx and \fry (and reimplement \frz) as 3d rotations.
eugeni
parents:
22214
diff
changeset
|
1668 /** |
18937 | 1669 * \brief Main ass rendering function, glues everything together |
1670 * \param event event to render | |
29048
584ff003cce9
Document the ass_render_event event_images parameter.
reimar
parents:
29047
diff
changeset
|
1671 * \param event_images struct containing resulting images, will also be initialized |
30200 | 1672 * Process event, appending resulting ASS_Image's to images_root. |
18937 | 1673 */ |
30200 | 1674 static int |
1675 ass_render_event(ASS_Renderer *render_priv, ASS_Event *event, | |
1676 EventImages *event_images) | |
18937 | 1677 { |
30200 | 1678 char *p; |
1679 FT_UInt previous; | |
1680 FT_UInt num_glyphs; | |
1681 FT_Vector pen; | |
1682 unsigned code; | |
1683 DBBox bbox; | |
1684 int i, j; | |
1685 int MarginL, MarginR, MarginV; | |
1686 int last_break; | |
1687 int alignment, halign, valign; | |
1688 double device_x = 0; | |
1689 double device_y = 0; | |
1690 TextInfo *text_info = &render_priv->text_info; | |
31853 | 1691 GlyphInfo *glyphs = render_priv->text_info.glyphs; |
30200 | 1692 ASS_Drawing *drawing; |
18937 | 1693 |
30200 | 1694 if (event->Style >= render_priv->track->n_styles) { |
1695 ass_msg(render_priv->library, MSGL_WARN, "No style found"); | |
1696 return 1; | |
1697 } | |
1698 if (!event->Text) { | |
1699 ass_msg(render_priv->library, MSGL_WARN, "Empty event"); | |
1700 return 1; | |
1701 } | |
19650 | 1702 |
30200 | 1703 init_render_context(render_priv, event); |
18937 | 1704 |
30200 | 1705 drawing = render_priv->state.drawing; |
1706 text_info->length = 0; | |
1707 num_glyphs = 0; | |
1708 p = event->Text; | |
34295 | 1709 |
30200 | 1710 // Event parsing. |
1711 while (1) { | |
1712 // get next char, executing style override | |
1713 // this affects render_context | |
1714 do { | |
1715 code = get_next_char(render_priv, &p); | |
1716 if (render_priv->state.drawing_mode && code) | |
1717 ass_drawing_add_char(drawing, (char) code); | |
1718 } while (code && render_priv->state.drawing_mode); // skip everything in drawing mode | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29178
diff
changeset
|
1719 |
34295 | 1720 if (text_info->length >= text_info->max_glyphs) { |
1721 // Raise maximum number of glyphs | |
1722 text_info->max_glyphs *= 2; | |
1723 text_info->glyphs = glyphs = | |
1724 realloc(text_info->glyphs, | |
1725 sizeof(GlyphInfo) * text_info->max_glyphs); | |
1726 } | |
1727 | |
1728 // Clear current GlyphInfo | |
1729 memset(&glyphs[text_info->length], 0, sizeof(GlyphInfo)); | |
1730 | |
30200 | 1731 // Parse drawing |
1732 if (drawing->i) { | |
1733 drawing->scale_x = render_priv->state.scale_x * | |
1734 render_priv->font_scale; | |
1735 drawing->scale_y = render_priv->state.scale_y * | |
1736 render_priv->font_scale; | |
1737 p--; | |
34295 | 1738 code = 0xfffc; // object replacement character |
1739 glyphs[text_info->length].drawing = drawing; | |
30200 | 1740 } |
18937 | 1741 |
30200 | 1742 // face could have been changed in get_next_char |
1743 if (!render_priv->state.font) { | |
1744 free_render_context(render_priv); | |
1745 return 1; | |
1746 } | |
18937 | 1747 |
30200 | 1748 if (code == 0) |
1749 break; | |
1750 | |
34295 | 1751 // Fill glyph information |
31853 | 1752 glyphs[text_info->length].symbol = code; |
34295 | 1753 glyphs[text_info->length].font = render_priv->state.font; |
30200 | 1754 for (i = 0; i < 4; ++i) { |
1755 uint32_t clr = render_priv->state.c[i]; | |
1756 change_alpha(&clr, | |
1757 mult_alpha(_a(clr), render_priv->state.fade), 1.); | |
31853 | 1758 glyphs[text_info->length].c[i] = clr; |
30200 | 1759 } |
31853 | 1760 glyphs[text_info->length].effect_type = render_priv->state.effect_type; |
1761 glyphs[text_info->length].effect_timing = | |
30200 | 1762 render_priv->state.effect_timing; |
31853 | 1763 glyphs[text_info->length].effect_skip_timing = |
30200 | 1764 render_priv->state.effect_skip_timing; |
34295 | 1765 glyphs[text_info->length].font_size = ensure_font_size(render_priv, |
1766 render_priv->state.font_size * render_priv->font_scale); | |
31853 | 1767 glyphs[text_info->length].be = render_priv->state.be; |
1768 glyphs[text_info->length].blur = render_priv->state.blur; | |
1769 glyphs[text_info->length].shadow_x = render_priv->state.shadow_x; | |
1770 glyphs[text_info->length].shadow_y = render_priv->state.shadow_y; | |
34295 | 1771 glyphs[text_info->length].scale_x= render_priv->state.scale_x; |
1772 glyphs[text_info->length].scale_y = render_priv->state.scale_y; | |
1773 glyphs[text_info->length].border_x= render_priv->state.border_x; | |
1774 glyphs[text_info->length].border_y = render_priv->state.border_y; | |
1775 glyphs[text_info->length].bold = render_priv->state.bold; | |
1776 glyphs[text_info->length].italic = render_priv->state.italic; | |
1777 glyphs[text_info->length].flags = render_priv->state.flags; | |
31853 | 1778 glyphs[text_info->length].frx = render_priv->state.frx; |
1779 glyphs[text_info->length].fry = render_priv->state.fry; | |
1780 glyphs[text_info->length].frz = render_priv->state.frz; | |
1781 glyphs[text_info->length].fax = render_priv->state.fax; | |
1782 glyphs[text_info->length].fay = render_priv->state.fay; | |
34295 | 1783 glyphs[text_info->length].bm_run_id = render_priv->state.bm_run_id; |
21614
5d2ca7ca18b5
Move ascender, descender, and kerning computation to ass_font.c.
eugeni
parents:
21506
diff
changeset
|
1784 |
34295 | 1785 if (glyphs[text_info->length].drawing) { |
1786 drawing = render_priv->state.drawing = | |
1787 ass_drawing_new(render_priv->library, render_priv->ftlibrary); | |
30200 | 1788 } |
19716
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
1789 |
30200 | 1790 text_info->length++; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29178
diff
changeset
|
1791 |
30200 | 1792 render_priv->state.effect_type = EF_NONE; |
1793 render_priv->state.effect_timing = 0; | |
1794 render_priv->state.effect_skip_timing = 0; | |
19931
399bb1fcdc94
Move variable declaration to a more deeply nested block. It is not used outside of it.
eugeni
parents:
19919
diff
changeset
|
1795 |
30200 | 1796 } |
19556 | 1797 |
30200 | 1798 if (text_info->length == 0) { |
1799 // no valid symbols in the event; this can be smth like {comment} | |
1800 free_render_context(render_priv); | |
1801 return 1; | |
1802 } | |
31853 | 1803 |
34295 | 1804 // Find shape runs and shape text |
1805 ass_shaper_set_base_direction(render_priv->shaper, | |
1806 resolve_base_direction(render_priv->state.font_encoding)); | |
1807 ass_shaper_find_runs(render_priv->shaper, render_priv, glyphs, | |
1808 text_info->length); | |
1809 ass_shaper_shape(render_priv->shaper, text_info); | |
1810 | |
1811 // Retrieve glyphs | |
1812 for (i = 0; i < text_info->length; i++) { | |
1813 GlyphInfo *info = glyphs + i; | |
1814 while (info) { | |
1815 get_outline_glyph(render_priv, info); | |
1816 info = info->next; | |
1817 } | |
1818 info = glyphs + i; | |
1819 | |
1820 // Add additional space after italic to non-italic style changes | |
1821 if (i && glyphs[i - 1].italic && !info->italic) { | |
1822 int back = i - 1; | |
1823 GlyphInfo *og = &glyphs[back]; | |
1824 while (back && og->bbox.xMax - og->bbox.xMin == 0 | |
1825 && og->italic) | |
1826 og = &glyphs[--back]; | |
1827 if (og->bbox.xMax > og->cluster_advance.x) | |
1828 og->cluster_advance.x = og->bbox.xMax; | |
1829 } | |
1830 | |
1831 // add horizontal letter spacing | |
1832 info->cluster_advance.x += double_to_d6(render_priv->state.hspacing * | |
1833 render_priv->font_scale * info->scale_x); | |
1834 | |
1835 // add displacement for vertical shearing | |
1836 info->cluster_advance.y += (info->fay * info->scale_y) * info->cluster_advance.x; | |
1837 | |
1838 } | |
1839 | |
1840 // Preliminary layout (for line wrapping) | |
1841 previous = 0; | |
1842 pen.x = 0; | |
1843 pen.y = 0; | |
1844 for (i = 0; i < text_info->length; i++) { | |
1845 GlyphInfo *info = glyphs + i; | |
1846 FT_Vector cluster_pen = pen; | |
1847 while (info) { | |
1848 info->pos.x = cluster_pen.x; | |
1849 info->pos.y = cluster_pen.y; | |
1850 | |
1851 cluster_pen.x += info->advance.x; | |
1852 cluster_pen.y += info->advance.y; | |
1853 | |
1854 // fill bitmap hash | |
1855 info->hash_key.type = BITMAP_OUTLINE; | |
1856 fill_bitmap_hash(render_priv, info, &info->hash_key.u.outline); | |
1857 | |
1858 info = info->next; | |
1859 } | |
1860 info = glyphs + i; | |
1861 pen.x += info->cluster_advance.x; | |
1862 pen.y += info->cluster_advance.y; | |
1863 previous = info->symbol; | |
1864 } | |
1865 | |
1866 | |
30200 | 1867 // depends on glyph x coordinates being monotonous, so it should be done before line wrap |
1868 process_karaoke_effects(render_priv); | |
18937 | 1869 |
30200 | 1870 // alignments |
1871 alignment = render_priv->state.alignment; | |
1872 halign = alignment & 3; | |
1873 valign = alignment & 12; | |
1874 | |
1875 MarginL = | |
31853 | 1876 (event->MarginL) ? event->MarginL : render_priv->state.style->MarginL; |
30200 | 1877 MarginR = |
31853 | 1878 (event->MarginR) ? event->MarginR : render_priv->state.style->MarginR; |
30200 | 1879 MarginV = |
31853 | 1880 (event->MarginV) ? event->MarginV : render_priv->state.style->MarginV; |
30200 | 1881 |
34295 | 1882 // calculate max length of a line |
1883 double max_text_width = | |
1884 x2scr(render_priv, render_priv->track->PlayResX - MarginR) - | |
1885 x2scr(render_priv, MarginL); | |
30200 | 1886 |
34295 | 1887 // wrap lines |
1888 if (render_priv->state.evt_type != EVENT_HSCROLL) { | |
30200 | 1889 // rearrange text in several lines |
1890 wrap_lines_smart(render_priv, max_text_width); | |
34295 | 1891 } else { |
1892 // no breaking or wrapping, everything in a single line | |
1893 text_info->lines[0].offset = 0; | |
1894 text_info->lines[0].len = text_info->length; | |
1895 text_info->n_lines = 1; | |
1896 measure_text(render_priv); | |
1897 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29178
diff
changeset
|
1898 |
34295 | 1899 // Reorder text into visual order |
1900 FriBidiStrIndex *cmap = ass_shaper_reorder(render_priv->shaper, text_info); | |
30200 | 1901 |
34295 | 1902 // Reposition according to the map |
1903 pen.x = 0; | |
1904 pen.y = 0; | |
1905 int lineno = 1; | |
1906 for (i = 0; i < text_info->length; i++) { | |
1907 GlyphInfo *info = glyphs + cmap[i]; | |
1908 if (glyphs[i].linebreak) { | |
1909 pen.x = 0; | |
1910 pen.y += double_to_d6(text_info->lines[lineno-1].desc); | |
1911 pen.y += double_to_d6(text_info->lines[lineno].asc); | |
1912 pen.y += double_to_d6(render_priv->settings.line_spacing); | |
1913 lineno++; | |
1914 } | |
1915 if (info->skip) continue; | |
1916 FT_Vector cluster_pen = pen; | |
1917 while (info) { | |
1918 info->pos.x = info->offset.x + cluster_pen.x; | |
1919 info->pos.y = info->offset.y + cluster_pen.y; | |
1920 cluster_pen.x += info->advance.x; | |
1921 cluster_pen.y += info->advance.y; | |
1922 info = info->next; | |
1923 } | |
1924 info = glyphs + cmap[i]; | |
1925 pen.x += info->cluster_advance.x; | |
1926 pen.y += info->cluster_advance.y; | |
1927 } | |
30200 | 1928 |
34295 | 1929 // align lines |
1930 if (render_priv->state.evt_type != EVENT_HSCROLL) { | |
1931 last_break = -1; | |
1932 double width = 0; | |
1933 for (i = 0; i <= text_info->length; ++i) { // (text_info->length + 1) is the end of the last line | |
1934 if ((i == text_info->length) || glyphs[i].linebreak) { | |
1935 // remove letter spacing (which is included in cluster_advance) | |
1936 if (i > 0) | |
1937 width -= render_priv->state.hspacing * render_priv->font_scale * | |
1938 glyphs[i-1].scale_x; | |
1939 double shift = 0; | |
30200 | 1940 if (halign == HALIGN_LEFT) { // left aligned, no action |
1941 shift = 0; | |
1942 } else if (halign == HALIGN_RIGHT) { // right aligned | |
1943 shift = max_text_width - width; | |
1944 } else if (halign == HALIGN_CENTER) { // centered | |
1945 shift = (max_text_width - width) / 2.0; | |
1946 } | |
1947 for (j = last_break + 1; j < i; ++j) { | |
34295 | 1948 GlyphInfo *info = glyphs + j; |
1949 while (info) { | |
1950 info->pos.x += double_to_d6(shift); | |
1951 info = info->next; | |
1952 } | |
30200 | 1953 } |
1954 last_break = i - 1; | |
34295 | 1955 width = 0; |
1956 } | |
1957 if (i < text_info->length && !glyphs[i].skip && | |
1958 glyphs[i].symbol != '\n' && glyphs[i].symbol != 0) { | |
1959 width += d6_to_double(glyphs[i].cluster_advance.x); | |
30200 | 1960 } |
1961 } | |
1962 } | |
1963 | |
1964 // determing text bounding box | |
1965 compute_string_bbox(text_info, &bbox); | |
1966 | |
1967 // determine device coordinates for text | |
19556 | 1968 |
30200 | 1969 // x coordinate for everything except positioned events |
1970 if (render_priv->state.evt_type == EVENT_NORMAL || | |
1971 render_priv->state.evt_type == EVENT_VSCROLL) { | |
1972 device_x = x2scr(render_priv, MarginL); | |
1973 } else if (render_priv->state.evt_type == EVENT_HSCROLL) { | |
1974 if (render_priv->state.scroll_direction == SCROLL_RL) | |
1975 device_x = | |
1976 x2scr(render_priv, | |
1977 render_priv->track->PlayResX - | |
1978 render_priv->state.scroll_shift); | |
1979 else if (render_priv->state.scroll_direction == SCROLL_LR) | |
1980 device_x = | |
1981 x2scr(render_priv, | |
1982 render_priv->state.scroll_shift) - (bbox.xMax - | |
1983 bbox.xMin); | |
1984 } | |
31853 | 1985 |
30200 | 1986 // y coordinate for everything except positioned events |
1987 if (render_priv->state.evt_type == EVENT_NORMAL || | |
1988 render_priv->state.evt_type == EVENT_HSCROLL) { | |
1989 if (valign == VALIGN_TOP) { // toptitle | |
1990 device_y = | |
1991 y2scr_top(render_priv, | |
1992 MarginV) + text_info->lines[0].asc; | |
1993 } else if (valign == VALIGN_CENTER) { // midtitle | |
1994 double scr_y = | |
1995 y2scr(render_priv, render_priv->track->PlayResY / 2.0); | |
1996 device_y = scr_y - (bbox.yMax + bbox.yMin) / 2.0; | |
1997 } else { // subtitle | |
1998 double scr_y; | |
1999 if (valign != VALIGN_SUB) | |
2000 ass_msg(render_priv->library, MSGL_V, | |
31853 | 2001 "Invalid valign, assuming 0 (subtitle)"); |
30200 | 2002 scr_y = |
2003 y2scr_sub(render_priv, | |
2004 render_priv->track->PlayResY - MarginV); | |
2005 device_y = scr_y; | |
2006 device_y -= text_info->height; | |
2007 device_y += text_info->lines[0].asc; | |
2008 } | |
2009 } else if (render_priv->state.evt_type == EVENT_VSCROLL) { | |
2010 if (render_priv->state.scroll_direction == SCROLL_TB) | |
2011 device_y = | |
2012 y2scr(render_priv, | |
2013 render_priv->state.clip_y0 + | |
2014 render_priv->state.scroll_shift) - (bbox.yMax - | |
2015 bbox.yMin); | |
2016 else if (render_priv->state.scroll_direction == SCROLL_BT) | |
2017 device_y = | |
2018 y2scr(render_priv, | |
2019 render_priv->state.clip_y1 - | |
2020 render_priv->state.scroll_shift); | |
2021 } | |
31853 | 2022 |
30200 | 2023 // positioned events are totally different |
2024 if (render_priv->state.evt_type == EVENT_POSITIONED) { | |
2025 double base_x = 0; | |
2026 double base_y = 0; | |
2027 ass_msg(render_priv->library, MSGL_DBG2, "positioned event at %f, %f", | |
2028 render_priv->state.pos_x, render_priv->state.pos_y); | |
2029 get_base_point(&bbox, alignment, &base_x, &base_y); | |
2030 device_x = | |
2031 x2scr_pos(render_priv, render_priv->state.pos_x) - base_x; | |
2032 device_y = | |
2033 y2scr_pos(render_priv, render_priv->state.pos_y) - base_y; | |
2034 } | |
31853 | 2035 |
30200 | 2036 // fix clip coordinates (they depend on alignment) |
2037 if (render_priv->state.evt_type == EVENT_NORMAL || | |
2038 render_priv->state.evt_type == EVENT_HSCROLL || | |
2039 render_priv->state.evt_type == EVENT_VSCROLL) { | |
2040 render_priv->state.clip_x0 = | |
31853 | 2041 x2scr_scaled(render_priv, render_priv->state.clip_x0); |
30200 | 2042 render_priv->state.clip_x1 = |
31853 | 2043 x2scr_scaled(render_priv, render_priv->state.clip_x1); |
30200 | 2044 if (valign == VALIGN_TOP) { |
2045 render_priv->state.clip_y0 = | |
2046 y2scr_top(render_priv, render_priv->state.clip_y0); | |
2047 render_priv->state.clip_y1 = | |
2048 y2scr_top(render_priv, render_priv->state.clip_y1); | |
2049 } else if (valign == VALIGN_CENTER) { | |
2050 render_priv->state.clip_y0 = | |
2051 y2scr(render_priv, render_priv->state.clip_y0); | |
2052 render_priv->state.clip_y1 = | |
2053 y2scr(render_priv, render_priv->state.clip_y1); | |
2054 } else if (valign == VALIGN_SUB) { | |
2055 render_priv->state.clip_y0 = | |
2056 y2scr_sub(render_priv, render_priv->state.clip_y0); | |
2057 render_priv->state.clip_y1 = | |
2058 y2scr_sub(render_priv, render_priv->state.clip_y1); | |
2059 } | |
2060 } else if (render_priv->state.evt_type == EVENT_POSITIONED) { | |
2061 render_priv->state.clip_x0 = | |
31853 | 2062 x2scr_pos_scaled(render_priv, render_priv->state.clip_x0); |
30200 | 2063 render_priv->state.clip_x1 = |
31853 | 2064 x2scr_pos_scaled(render_priv, render_priv->state.clip_x1); |
30200 | 2065 render_priv->state.clip_y0 = |
2066 y2scr_pos(render_priv, render_priv->state.clip_y0); | |
2067 render_priv->state.clip_y1 = | |
2068 y2scr_pos(render_priv, render_priv->state.clip_y1); | |
2069 } | |
31853 | 2070 |
30200 | 2071 // calculate rotation parameters |
2072 { | |
2073 DVector center; | |
29383
e9cab9f6ed62
Make sure clip coordinates are inside the screen area.
eugeni
parents:
29382
diff
changeset
|
2074 |
30200 | 2075 if (render_priv->state.have_origin) { |
2076 center.x = x2scr(render_priv, render_priv->state.org_x); | |
2077 center.y = y2scr(render_priv, render_priv->state.org_y); | |
2078 } else { | |
2079 double bx = 0., by = 0.; | |
2080 get_base_point(&bbox, alignment, &bx, &by); | |
2081 center.x = device_x + bx; | |
2082 center.y = device_y + by; | |
2083 } | |
2084 | |
2085 for (i = 0; i < text_info->length; ++i) { | |
31853 | 2086 GlyphInfo *info = glyphs + i; |
34295 | 2087 while (info) { |
2088 OutlineBitmapHashKey *key = &info->hash_key.u.outline; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29178
diff
changeset
|
2089 |
34295 | 2090 if (key->frx || key->fry || key->frz || key->fax || key->fay) { |
2091 key->shift_x = info->pos.x + double_to_d6(device_x - center.x); | |
2092 key->shift_y = -(info->pos.y + double_to_d6(device_y - center.y)); | |
2093 } else { | |
2094 key->shift_x = 0; | |
2095 key->shift_y = 0; | |
2096 } | |
2097 info = info->next; | |
30200 | 2098 } |
2099 } | |
2100 } | |
18937 | 2101 |
30200 | 2102 // convert glyphs to bitmaps |
34295 | 2103 int left = render_priv->settings.left_margin; |
2104 device_x = (device_x - left) * render_priv->font_scale_x + left; | |
30200 | 2105 for (i = 0; i < text_info->length; ++i) { |
34295 | 2106 GlyphInfo *info = glyphs + i; |
2107 while (info) { | |
2108 OutlineBitmapHashKey *key = &info->hash_key.u.outline; | |
2109 info->pos.x *= render_priv->font_scale_x; | |
2110 key->advance.x = | |
2111 double_to_d6(device_x - (int) device_x + | |
2112 d6_to_double(info->pos.x & SUBPIXEL_MASK)) & ~SUBPIXEL_ACCURACY; | |
2113 key->advance.y = | |
2114 double_to_d6(device_y - (int) device_y + | |
2115 d6_to_double(info->pos.y & SUBPIXEL_MASK)) & ~SUBPIXEL_ACCURACY; | |
2116 get_bitmap_glyph(render_priv, info); | |
2117 info = info->next; | |
2118 } | |
30200 | 2119 } |
18937 | 2120 |
30200 | 2121 memset(event_images, 0, sizeof(*event_images)); |
2122 event_images->top = device_y - text_info->lines[0].asc; | |
2123 event_images->height = text_info->height; | |
31853 | 2124 event_images->left = |
2125 (device_x + bbox.xMin * render_priv->font_scale_x) + 0.5; | |
2126 event_images->width = | |
2127 (bbox.xMax - bbox.xMin) * render_priv->font_scale_x + 0.5; | |
30200 | 2128 event_images->detect_collisions = render_priv->state.detect_collisions; |
2129 event_images->shift_direction = (valign == VALIGN_TOP) ? 1 : -1; | |
2130 event_images->event = event; | |
2131 event_images->imgs = render_text(render_priv, (int) device_x, (int) device_y); | |
23174 | 2132 |
34295 | 2133 ass_shaper_cleanup(render_priv->shaper, text_info); |
30200 | 2134 free_render_context(render_priv); |
18937 | 2135 |
30200 | 2136 return 0; |
18937 | 2137 } |
2138 | |
21506
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2139 /** |
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2140 * \brief deallocate image list |
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2141 * \param img list pointer |
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2142 */ |
31853 | 2143 void ass_free_images(ASS_Image *img) |
21506
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2144 { |
30200 | 2145 while (img) { |
2146 ASS_Image *next = img->next; | |
2147 free(img); | |
2148 img = next; | |
2149 } | |
21506
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2150 } |
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2151 |
31853 | 2152 /** |
2153 * \brief Check cache limits and reset cache if they are exceeded | |
2154 */ | |
2155 static void check_cache_limits(ASS_Renderer *priv, CacheStore *cache) | |
20446
e8adc3778348
Split ass_configure() into several smaller functions.
eugeni
parents:
20320
diff
changeset
|
2156 { |
34295 | 2157 if (ass_cache_empty(cache->bitmap_cache, cache->bitmap_max_size)) { |
2158 ass_cache_empty(cache->composite_cache, 0); | |
31853 | 2159 ass_free_images(priv->prev_images_root); |
2160 priv->prev_images_root = 0; | |
30200 | 2161 } |
34295 | 2162 if (ass_cache_empty(cache->outline_cache, cache->glyph_max)) { |
2163 ass_cache_empty(cache->bitmap_cache, 0); | |
2164 ass_cache_empty(cache->composite_cache, 0); | |
2165 ass_free_images(priv->prev_images_root); | |
2166 priv->prev_images_root = 0; | |
31853 | 2167 } |
26582
62ac4f8062ee
Remove libass dependency on global font_fontconfig variable.
eugeni
parents:
26033
diff
changeset
|
2168 } |
62ac4f8062ee
Remove libass dependency on global font_fontconfig variable.
eugeni
parents:
26033
diff
changeset
|
2169 |
18937 | 2170 /** |
2171 * \brief Start a new frame | |
2172 */ | |
30200 | 2173 static int |
2174 ass_start_frame(ASS_Renderer *render_priv, ASS_Track *track, | |
2175 long long now) | |
18937 | 2176 { |
30200 | 2177 ASS_Settings *settings_priv = &render_priv->settings; |
2178 | |
2179 if (!render_priv->settings.frame_width | |
2180 && !render_priv->settings.frame_height) | |
2181 return 1; // library not initialized | |
2182 | |
2183 if (render_priv->library != track->library) | |
2184 return 1; | |
2185 | |
31853 | 2186 if (!render_priv->fontconfig_priv) |
2187 return 1; | |
2188 | |
30200 | 2189 free_list_clear(render_priv); |
2190 | |
2191 if (track->n_events == 0) | |
2192 return 1; // nothing to do | |
18937 | 2193 |
30200 | 2194 render_priv->track = track; |
2195 render_priv->time = now; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29178
diff
changeset
|
2196 |
34295 | 2197 ass_lazy_track_init(render_priv->library, render_priv->track); |
30200 | 2198 |
2199 render_priv->font_scale = settings_priv->font_size_coeff * | |
2200 render_priv->orig_height / render_priv->track->PlayResY; | |
2201 if (render_priv->track->ScaledBorderAndShadow) | |
2202 render_priv->border_scale = | |
2203 ((double) render_priv->orig_height) / | |
2204 render_priv->track->PlayResY; | |
2205 else | |
2206 render_priv->border_scale = 1.; | |
2207 | |
34295 | 2208 ass_shaper_set_kerning(render_priv->shaper, track->Kerning); |
2209 if (track->Language) | |
2210 ass_shaper_set_language(render_priv->shaper, track->Language); | |
2211 ass_shaper_set_level(render_priv->shaper, render_priv->settings.shaper); | |
2212 | |
30200 | 2213 // PAR correction |
2214 render_priv->font_scale_x = render_priv->settings.aspect / | |
2215 render_priv->settings.storage_aspect; | |
2216 | |
2217 render_priv->prev_images_root = render_priv->images_root; | |
2218 render_priv->images_root = 0; | |
18937 | 2219 |
31853 | 2220 check_cache_limits(render_priv, &render_priv->cache); |
19825
f351a3fc3e42
Make font outline width proportional to movie resolution.
eugeni
parents:
19716
diff
changeset
|
2221 |
30200 | 2222 return 0; |
18937 | 2223 } |
2224 | |
30200 | 2225 static int cmp_event_layer(const void *p1, const void *p2) |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2226 { |
30200 | 2227 ASS_Event *e1 = ((EventImages *) p1)->event; |
2228 ASS_Event *e2 = ((EventImages *) p2)->event; | |
2229 if (e1->Layer < e2->Layer) | |
2230 return -1; | |
2231 if (e1->Layer > e2->Layer) | |
2232 return 1; | |
2233 if (e1->ReadOrder < e2->ReadOrder) | |
2234 return -1; | |
2235 if (e1->ReadOrder > e2->ReadOrder) | |
2236 return 1; | |
2237 return 0; | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2238 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2239 |
30200 | 2240 static ASS_RenderPriv *get_render_priv(ASS_Renderer *render_priv, |
2241 ASS_Event *event) | |
18937 | 2242 { |
30200 | 2243 if (!event->render_priv) |
2244 event->render_priv = calloc(1, sizeof(ASS_RenderPriv)); | |
2245 if (render_priv->render_id != event->render_priv->render_id) { | |
2246 memset(event->render_priv, 0, sizeof(ASS_RenderPriv)); | |
2247 event->render_priv->render_id = render_priv->render_id; | |
2248 } | |
2249 | |
2250 return event->render_priv; | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2251 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2252 |
30200 | 2253 static int overlap(Segment *s1, Segment *s2) |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2254 { |
30200 | 2255 if (s1->a >= s2->b || s2->a >= s1->b || |
2256 s1->ha >= s2->hb || s2->ha >= s1->hb) | |
2257 return 0; | |
2258 return 1; | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2259 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2260 |
30200 | 2261 static int cmp_segment(const void *p1, const void *p2) |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2262 { |
30200 | 2263 return ((Segment *) p1)->a - ((Segment *) p2)->a; |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2264 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2265 |
30200 | 2266 static void |
2267 shift_event(ASS_Renderer *render_priv, EventImages *ei, int shift) | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2268 { |
30200 | 2269 ASS_Image *cur = ei->imgs; |
2270 while (cur) { | |
2271 cur->dst_y += shift; | |
2272 // clip top and bottom | |
2273 if (cur->dst_y < 0) { | |
2274 int clip = -cur->dst_y; | |
2275 cur->h -= clip; | |
2276 cur->bitmap += clip * cur->stride; | |
2277 cur->dst_y = 0; | |
2278 } | |
2279 if (cur->dst_y + cur->h >= render_priv->height) { | |
2280 int clip = cur->dst_y + cur->h - render_priv->height; | |
2281 cur->h -= clip; | |
2282 } | |
2283 if (cur->h <= 0) { | |
2284 cur->h = 0; | |
2285 cur->dst_y = 0; | |
2286 } | |
2287 cur = cur->next; | |
2288 } | |
2289 ei->top += shift; | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2290 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2291 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2292 // dir: 1 - move down |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2293 // -1 - move up |
30200 | 2294 static int fit_segment(Segment *s, Segment *fixed, int *cnt, int dir) |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2295 { |
30200 | 2296 int i; |
2297 int shift = 0; | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2298 |
30200 | 2299 if (dir == 1) // move down |
2300 for (i = 0; i < *cnt; ++i) { | |
2301 if (s->b + shift <= fixed[i].a || s->a + shift >= fixed[i].b || | |
2302 s->hb <= fixed[i].ha || s->ha >= fixed[i].hb) | |
2303 continue; | |
2304 shift = fixed[i].b - s->a; | |
2305 } else // dir == -1, move up | |
2306 for (i = *cnt - 1; i >= 0; --i) { | |
2307 if (s->b + shift <= fixed[i].a || s->a + shift >= fixed[i].b || | |
2308 s->hb <= fixed[i].ha || s->ha >= fixed[i].hb) | |
2309 continue; | |
2310 shift = fixed[i].a - s->b; | |
2311 } | |
21097
77316737de63
Fix collision detection. The old method tried to avoid gaps between subtitles
eugeni
parents:
21066
diff
changeset
|
2312 |
30200 | 2313 fixed[*cnt].a = s->a + shift; |
2314 fixed[*cnt].b = s->b + shift; | |
2315 fixed[*cnt].ha = s->ha; | |
2316 fixed[*cnt].hb = s->hb; | |
2317 (*cnt)++; | |
2318 qsort(fixed, *cnt, sizeof(Segment), cmp_segment); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29178
diff
changeset
|
2319 |
30200 | 2320 return shift; |
18937 | 2321 } |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2322 |
30200 | 2323 static void |
2324 fix_collisions(ASS_Renderer *render_priv, EventImages *imgs, int cnt) | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2325 { |
30200 | 2326 Segment *used = malloc(cnt * sizeof(*used)); |
2327 int cnt_used = 0; | |
2328 int i, j; | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2329 |
30200 | 2330 // fill used[] with fixed events |
2331 for (i = 0; i < cnt; ++i) { | |
2332 ASS_RenderPriv *priv; | |
2333 if (!imgs[i].detect_collisions) | |
2334 continue; | |
2335 priv = get_render_priv(render_priv, imgs[i].event); | |
2336 if (priv->height > 0) { // it's a fixed event | |
2337 Segment s; | |
2338 s.a = priv->top; | |
2339 s.b = priv->top + priv->height; | |
2340 s.ha = priv->left; | |
2341 s.hb = priv->left + priv->width; | |
2342 if (priv->height != imgs[i].height) { // no, it's not | |
2343 ass_msg(render_priv->library, MSGL_WARN, | |
31853 | 2344 "Event height has changed"); |
30200 | 2345 priv->top = 0; |
2346 priv->height = 0; | |
2347 priv->left = 0; | |
2348 priv->width = 0; | |
2349 } | |
2350 for (j = 0; j < cnt_used; ++j) | |
2351 if (overlap(&s, used + j)) { // no, it's not | |
2352 priv->top = 0; | |
2353 priv->height = 0; | |
2354 priv->left = 0; | |
2355 priv->width = 0; | |
2356 } | |
2357 if (priv->height > 0) { // still a fixed event | |
2358 used[cnt_used].a = priv->top; | |
2359 used[cnt_used].b = priv->top + priv->height; | |
2360 used[cnt_used].ha = priv->left; | |
2361 used[cnt_used].hb = priv->left + priv->width; | |
2362 cnt_used++; | |
2363 shift_event(render_priv, imgs + i, priv->top - imgs[i].top); | |
2364 } | |
2365 } | |
2366 } | |
2367 qsort(used, cnt_used, sizeof(Segment), cmp_segment); | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2368 |
30200 | 2369 // try to fit other events in free spaces |
2370 for (i = 0; i < cnt; ++i) { | |
2371 ASS_RenderPriv *priv; | |
2372 if (!imgs[i].detect_collisions) | |
2373 continue; | |
2374 priv = get_render_priv(render_priv, imgs[i].event); | |
2375 if (priv->height == 0) { // not a fixed event | |
2376 int shift; | |
2377 Segment s; | |
2378 s.a = imgs[i].top; | |
2379 s.b = imgs[i].top + imgs[i].height; | |
2380 s.ha = imgs[i].left; | |
2381 s.hb = imgs[i].left + imgs[i].width; | |
2382 shift = fit_segment(&s, used, &cnt_used, imgs[i].shift_direction); | |
2383 if (shift) | |
2384 shift_event(render_priv, imgs + i, shift); | |
2385 // make it fixed | |
2386 priv->top = imgs[i].top; | |
2387 priv->height = imgs[i].height; | |
2388 priv->left = imgs[i].left; | |
2389 priv->width = imgs[i].width; | |
2390 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29178
diff
changeset
|
2391 |
30200 | 2392 } |
2393 | |
2394 free(used); | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2395 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2396 |
18937 | 2397 /** |
21506
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2398 * \brief compare two images |
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2399 * \param i1 first image |
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2400 * \param i2 second image |
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2401 * \return 0 if identical, 1 if different positions, 2 if different content |
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2402 */ |
30200 | 2403 static int ass_image_compare(ASS_Image *i1, ASS_Image *i2) |
21506
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2404 { |
30200 | 2405 if (i1->w != i2->w) |
2406 return 2; | |
2407 if (i1->h != i2->h) | |
2408 return 2; | |
2409 if (i1->stride != i2->stride) | |
2410 return 2; | |
2411 if (i1->color != i2->color) | |
2412 return 2; | |
2413 if (i1->bitmap != i2->bitmap) | |
2414 return 2; | |
2415 if (i1->dst_x != i2->dst_x) | |
2416 return 1; | |
2417 if (i1->dst_y != i2->dst_y) | |
2418 return 1; | |
2419 return 0; | |
21506
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2420 } |
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2421 |
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2422 /** |
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2423 * \brief compare current and previous image list |
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2424 * \param priv library handle |
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2425 * \return 0 if identical, 1 if different positions, 2 if different content |
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2426 */ |
30200 | 2427 static int ass_detect_change(ASS_Renderer *priv) |
21506
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2428 { |
30200 | 2429 ASS_Image *img, *img2; |
2430 int diff; | |
21506
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2431 |
30200 | 2432 img = priv->prev_images_root; |
2433 img2 = priv->images_root; | |
2434 diff = 0; | |
2435 while (img && diff < 2) { | |
2436 ASS_Image *next, *next2; | |
2437 next = img->next; | |
2438 if (img2) { | |
2439 int d = ass_image_compare(img, img2); | |
2440 if (d > diff) | |
2441 diff = d; | |
2442 next2 = img2->next; | |
2443 } else { | |
2444 // previous list is shorter | |
2445 diff = 2; | |
2446 break; | |
2447 } | |
2448 img = next; | |
2449 img2 = next2; | |
2450 } | |
21506
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2451 |
30200 | 2452 // is the previous list longer? |
2453 if (img2) | |
2454 diff = 2; | |
21506
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2455 |
30200 | 2456 return diff; |
21506
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2457 } |
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2458 |
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2459 /** |
18937 | 2460 * \brief render a frame |
2461 * \param priv library handle | |
2462 * \param track track | |
2463 * \param now current video timestamp (ms) | |
21506
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2464 * \param detect_change a value describing how the new images differ from the previous ones will be written here: |
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2465 * 0 if identical, 1 if different positions, 2 if different content. |
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2466 * Can be NULL, in that case no detection is performed. |
18937 | 2467 */ |
30200 | 2468 ASS_Image *ass_render_frame(ASS_Renderer *priv, ASS_Track *track, |
2469 long long now, int *detect_change) | |
18937 | 2470 { |
30200 | 2471 int i, cnt, rc; |
2472 EventImages *last; | |
2473 ASS_Image **tail; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29178
diff
changeset
|
2474 |
30200 | 2475 // init frame |
2476 rc = ass_start_frame(priv, track, now); | |
2477 if (rc != 0) | |
2478 return 0; | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2479 |
30200 | 2480 // render events separately |
2481 cnt = 0; | |
2482 for (i = 0; i < track->n_events; ++i) { | |
2483 ASS_Event *event = track->events + i; | |
2484 if ((event->Start <= now) | |
2485 && (now < (event->Start + event->Duration))) { | |
2486 if (cnt >= priv->eimg_size) { | |
2487 priv->eimg_size += 100; | |
2488 priv->eimg = | |
2489 realloc(priv->eimg, | |
2490 priv->eimg_size * sizeof(EventImages)); | |
2491 } | |
2492 rc = ass_render_event(priv, event, priv->eimg + cnt); | |
2493 if (!rc) | |
2494 ++cnt; | |
2495 } | |
2496 } | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2497 |
30200 | 2498 // sort by layer |
2499 qsort(priv->eimg, cnt, sizeof(EventImages), cmp_event_layer); | |
2500 | |
2501 // call fix_collisions for each group of events with the same layer | |
2502 last = priv->eimg; | |
2503 for (i = 1; i < cnt; ++i) | |
2504 if (last->event->Layer != priv->eimg[i].event->Layer) { | |
2505 fix_collisions(priv, last, priv->eimg + i - last); | |
2506 last = priv->eimg + i; | |
2507 } | |
2508 if (cnt > 0) | |
2509 fix_collisions(priv, last, priv->eimg + cnt - last); | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2510 |
30200 | 2511 // concat lists |
2512 tail = &priv->images_root; | |
2513 for (i = 0; i < cnt; ++i) { | |
2514 ASS_Image *cur = priv->eimg[i].imgs; | |
2515 while (cur) { | |
2516 *tail = cur; | |
2517 tail = &cur->next; | |
2518 cur = cur->next; | |
2519 } | |
2520 } | |
21506
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2521 |
30200 | 2522 if (detect_change) |
2523 *detect_change = ass_detect_change(priv); | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29178
diff
changeset
|
2524 |
30200 | 2525 // free the previous image list |
2526 ass_free_images(priv->prev_images_root); | |
2527 priv->prev_images_root = 0; | |
21506
8174acbf0633
Speed up ASS subtitles display by detecting changes between two consecutive
eugeni
parents:
21460
diff
changeset
|
2528 |
30200 | 2529 return priv->images_root; |
18937 | 2530 } |