Mercurial > mplayer.hg
annotate libass/ass_render.c @ 20164:b064d49b08f9
Use MOUSE_BTN0 instead of MOUSE_BASE for consistency with other vo drivers.
patch by Laurent Wozniak, laurent.wozniak laposte net
author | diego |
---|---|
date | Thu, 12 Oct 2006 00:39:47 +0000 |
parents | fa122b7c71c6 |
children | 4a21653e69d5 |
rev | line source |
---|---|
20008
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*- |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
2 // vim:ts=8:sw=8:noet:ai: |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
3 /* |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
4 Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com> |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
5 |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
6 This program is free software; you can redistribute it and/or modify |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
7 it under the terms of the GNU General Public License as published by |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
8 the Free Software Foundation; either version 2 of the License, or |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
9 (at your option) any later version. |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
10 |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
11 This program is distributed in the hope that it will be useful, |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
14 GNU General Public License for more details. |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
15 |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
16 You should have received a copy of the GNU General Public License |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
17 along with this program; if not, write to the Free Software |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
19 */ |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
20 |
18937 | 21 #include "config.h" |
22 | |
23 #include <assert.h> | |
24 #include <math.h> | |
19378 | 25 #include <inttypes.h> |
18937 | 26 #include <ft2build.h> |
27 #include FT_FREETYPE_H | |
28 #include FT_STROKER_H | |
29 #include FT_GLYPH_H | |
30 #include FT_SYNTHESIS_H | |
31 | |
32 #include "mp_msg.h" | |
33 | |
34 #include "ass.h" | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
35 #include "ass_bitmap.h" |
18937 | 36 #include "ass_cache.h" |
37 #include "ass_utils.h" | |
38 #include "ass_fontconfig.h" | |
39 | |
40 #include "libvo/sub.h" // for utf8_get_char | |
41 | |
42 #define MAX_GLYPHS 1000 | |
43 #define MAX_LINES 100 | |
44 | |
45 char *get_path(char *); | |
46 | |
47 extern char *font_name; | |
48 #ifdef HAVE_FONTCONFIG | |
49 extern int font_fontconfig; | |
50 #else | |
51 static int font_fontconfig = 0; | |
52 #endif | |
53 | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
54 static int last_render_id = 0; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
55 |
18937 | 56 struct ass_instance_s { |
57 FT_Library library; | |
58 fc_instance_t* fontconfig_priv; | |
59 ass_settings_t settings; | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
60 int render_id; |
19848 | 61 ass_synth_priv_t* synth_priv; |
18937 | 62 |
63 ass_image_t* images_root; // rendering result is stored here | |
64 }; | |
65 | |
66 int no_more_font_messages = 0; // don't print font warnings | |
67 | |
68 typedef enum {EF_NONE = 0, EF_KARAOKE, EF_KARAOKE_KF, EF_KARAOKE_KO} effect_t; | |
69 | |
70 // describes a glyph | |
71 // glyph_info_t and text_info_t are used for text centering and word-wrapping operations | |
72 typedef struct glyph_info_s { | |
73 unsigned symbol; | |
74 FT_Glyph glyph; | |
75 FT_Glyph outline_glyph; | |
19965 | 76 bitmap_t* bm; // glyph bitmap |
77 bitmap_t* bm_o; // outline bitmap | |
78 bitmap_t* bm_s; // shadow bitmap | |
18937 | 79 FT_BBox bbox; |
80 FT_Vector pos; | |
81 char linebreak; // the first (leading) glyph of some line ? | |
19691 | 82 uint32_t c[4]; // colors |
18937 | 83 FT_Vector advance; // 26.6 |
84 effect_t effect_type; | |
19716
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
85 int effect_timing; // time duration of current karaoke word |
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
86 // after process_karaoke_effects: distance in pixels from the glyph origin. |
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
87 // part of the glyph to the left of it is displayed in a different color. |
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
88 int effect_skip_timing; // delay after the end of last karaoke word |
18937 | 89 int asc, desc; // font max ascender and descender |
90 // int height; | |
19848 | 91 int be; // blur edges |
19965 | 92 int shadow; |
18937 | 93 |
94 glyph_hash_key_t hash_key; | |
95 } glyph_info_t; | |
96 | |
97 typedef struct line_info_s { | |
98 int asc, desc; | |
99 } line_info_t; | |
100 | |
101 typedef struct text_info_s { | |
102 glyph_info_t* glyphs; | |
103 int length; | |
104 line_info_t lines[MAX_LINES]; | |
105 int n_lines; | |
106 int height; | |
107 } text_info_t; | |
108 | |
109 | |
110 // Renderer state. | |
111 // Values like current font face, color, screen position, clipping and so on are stored here. | |
112 typedef struct render_context_s { | |
113 ass_event_t* event; | |
114 ass_style_t* style; | |
115 | |
116 FT_Face face; | |
117 char* font_path; | |
118 int font_size; | |
119 | |
120 FT_Stroker stroker; | |
121 int alignment; // alignment overrides go here; if zero, style value will be used | |
122 double rotation; | |
123 enum { EVENT_NORMAL, // "normal" top-, sub- or mid- title | |
19556 | 124 EVENT_POSITIONED, // happens after pos(,), margins are ignored |
125 EVENT_HSCROLL, // "Banner" transition effect, text_width is unlimited | |
126 EVENT_VSCROLL // "Scroll up", "Scroll down" transition effects | |
18937 | 127 } evt_type; |
128 int pos_x, pos_y; // position | |
129 int org_x, org_y; // origin | |
130 double scale_x, scale_y; | |
131 int hspacing; // distance between letters, in pixels | |
132 double border; // outline width | |
19691 | 133 uint32_t c[4]; // colors(Primary, Secondary, so on) in RGBA |
18937 | 134 int clip_x0, clip_y0, clip_x1, clip_y1; |
135 char detect_collisions; | |
19692
5b40e87b9619
Change \fad behaviour so that it does not get cancelled by \r.
eugeni
parents:
19691
diff
changeset
|
136 uint32_t fade; // alpha from \fad |
19848 | 137 char be; // blur edges |
19965 | 138 int shadow; |
18937 | 139 |
140 effect_t effect_type; | |
141 int effect_timing; | |
19716
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
142 int effect_skip_timing; |
18937 | 143 |
19556 | 144 enum { SCROLL_LR, // left-to-right |
145 SCROLL_RL, | |
146 SCROLL_TB, // top-to-bottom | |
147 SCROLL_BT | |
148 } scroll_direction; // for EVENT_HSCROLL, EVENT_VSCROLL | |
149 int scroll_shift; | |
150 | |
18937 | 151 // face properties |
152 char* family; | |
153 unsigned bold; | |
154 unsigned italic; | |
155 | |
156 } render_context_t; | |
157 | |
158 // frame-global data | |
159 typedef struct frame_context_s { | |
160 ass_instance_t* ass_priv; | |
161 int width, height; // screen dimensions | |
162 int orig_height; // frame height ( = screen height - margins ) | |
19538 | 163 int orig_width; // frame width ( = screen width - margins ) |
18937 | 164 ass_track_t* track; |
165 long long time; // frame's timestamp, ms | |
19825
f351a3fc3e42
Make font outline width proportional to movie resolution.
eugeni
parents:
19716
diff
changeset
|
166 double font_scale; |
18937 | 167 double font_scale_x; // x scale applied to all glyphs to preserve text aspect ratio |
19917
6a31849e2b80
In r19831, font size multiplier was mistakenly applied to border width.
eugeni
parents:
19906
diff
changeset
|
168 double border_scale; |
18937 | 169 } frame_context_t; |
170 | |
171 static ass_instance_t* ass_instance; | |
172 static ass_settings_t* global_settings; | |
173 static text_info_t text_info; | |
174 static render_context_t render_context; | |
175 static frame_context_t frame_context; | |
176 | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
177 // a rendered event |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
178 typedef struct event_images_s { |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
179 ass_image_t* imgs; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
180 int top, height; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
181 int detect_collisions; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
182 int shift_direction; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
183 ass_event_t* event; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
184 } event_images_t; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
185 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
186 struct render_priv_s { |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
187 int top, height; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
188 int render_id; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
189 }; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
190 |
18937 | 191 static void ass_lazy_track_init(void) |
192 { | |
193 ass_track_t* track = frame_context.track; | |
194 if (track->PlayResX && track->PlayResY) | |
195 return; | |
196 if (!track->PlayResX && !track->PlayResY) { | |
197 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Neither PlayResX nor PlayResY defined. Assuming 384x288. \n"); | |
198 track->PlayResX = 384; | |
199 track->PlayResY = 288; | |
200 } else { | |
19538 | 201 double orig_aspect = (global_settings->aspect * frame_context.height * frame_context.orig_width) / |
202 frame_context.orig_height / frame_context.width; | |
18937 | 203 if (!track->PlayResY) { |
204 track->PlayResY = track->PlayResX / orig_aspect + .5; | |
205 mp_msg(MSGT_GLOBAL, MSGL_WARN, "PlayResY undefined, setting %d \n", track->PlayResY); | |
206 } else if (!track->PlayResX) { | |
207 track->PlayResX = track->PlayResY * orig_aspect + .5; | |
208 mp_msg(MSGT_GLOBAL, MSGL_WARN, "PlayResX undefined, setting %d \n", track->PlayResX); | |
209 } | |
210 } | |
211 } | |
212 | |
213 ass_instance_t* ass_init(void) | |
214 { | |
215 char* family = 0; | |
216 char* path = 0; | |
217 char* fonts_path = 0; | |
218 int error; | |
219 fc_instance_t* fc_priv; | |
220 FT_Library ft; | |
19518 | 221 ass_instance_t* priv = 0; |
18937 | 222 |
223 if (font_fontconfig && font_name) | |
224 family = strdup(font_name); | |
225 | |
226 if (!font_fontconfig && font_name) | |
227 path = strdup(font_name); | |
228 else | |
229 path = get_path("subfont.ttf"); | |
230 | |
231 fonts_path = get_path("fonts"); | |
232 | |
233 fc_priv = fontconfig_init(fonts_path, family, path); | |
234 | |
235 free(fonts_path); | |
236 if (path) free(path); | |
237 if (family) free(family); | |
238 | |
239 if (!fc_priv) | |
19517 | 240 goto ass_init_exit; |
18937 | 241 |
242 error = FT_Init_FreeType( &ft ); | |
243 if ( error ) { | |
244 mp_msg(MSGT_GLOBAL, MSGL_FATAL, "FT_Init_FreeType failed\n"); | |
245 fontconfig_done(fc_priv); | |
19517 | 246 goto ass_init_exit; |
18937 | 247 } |
248 | |
249 priv = calloc(1, sizeof(ass_instance_t)); | |
250 if (!priv) { | |
251 FT_Done_FreeType(ft); | |
252 fontconfig_done(fc_priv); | |
19517 | 253 goto ass_init_exit; |
18937 | 254 } |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
255 |
19848 | 256 priv->synth_priv = ass_synth_init(); |
257 | |
18937 | 258 priv->library = ft; |
259 priv->fontconfig_priv = fc_priv; | |
260 // images_root and related stuff is zero-filled in calloc | |
261 | |
262 ass_face_cache_init(); | |
263 ass_glyph_cache_init(); | |
264 | |
265 text_info.glyphs = calloc(MAX_GLYPHS, sizeof(glyph_info_t)); | |
266 | |
19517 | 267 ass_init_exit: |
268 if (priv) mp_msg(MSGT_GLOBAL, MSGL_INFO, "[ass] Init\n"); | |
269 else mp_msg(MSGT_GLOBAL, MSGL_ERR, "[ass] Init failed\n"); | |
270 | |
18937 | 271 return priv; |
272 } | |
273 | |
274 void ass_done(ass_instance_t* priv) | |
275 { | |
276 ass_face_cache_done(); | |
277 ass_glyph_cache_done(); | |
278 if (priv && priv->library) FT_Done_FreeType(priv->library); | |
279 if (priv && priv->fontconfig_priv) fontconfig_done(priv->fontconfig_priv); | |
19848 | 280 if (priv && priv->synth_priv) ass_synth_done(priv->synth_priv); |
18937 | 281 if (priv) free(priv); |
282 if (text_info.glyphs) free(text_info.glyphs); | |
283 } | |
284 | |
285 /** | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
286 * \brief Create a new ass_image_t |
18937 | 287 * Parameters are the same as ass_image_t fields. |
288 */ | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
289 static ass_image_t* my_draw_bitmap(unsigned char* bitmap, int bitmap_w, int bitmap_h, int stride, int dst_x, int dst_y, uint32_t color) |
18937 | 290 { |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
291 ass_image_t* img = calloc(1, sizeof(ass_image_t)); |
18937 | 292 |
293 img->w = bitmap_w; | |
294 img->h = bitmap_h; | |
295 img->stride = stride; | |
296 img->bitmap = bitmap; | |
297 img->color = color; | |
298 img->dst_x = dst_x; | |
299 img->dst_y = dst_y; | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
300 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
301 return img; |
18937 | 302 } |
303 | |
304 /** | |
305 * \brief convert bitmap glyph into ass_image_t struct(s) | |
306 * \param bit freetype bitmap glyph, FT_PIXEL_MODE_GRAY | |
307 * \param dst_x bitmap x coordinate in video frame | |
308 * \param dst_y bitmap y coordinate in video frame | |
309 * \param color first color, RGBA | |
310 * \param color2 second color, RGBA | |
311 * \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
|
312 * \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
|
313 * \return pointer to the new list tail |
18937 | 314 * Performs clipping. Uses my_draw_bitmap for actual bitmap convertion. |
315 */ | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
316 static ass_image_t** render_glyph(bitmap_t* bm, int dst_x, int dst_y, uint32_t color, uint32_t color2, int brk, ass_image_t** tail) |
18937 | 317 { |
318 // brk is relative to dst_x | |
319 // color = color left of brk | |
320 // color2 = color right of brk | |
321 int b_x0, b_y0, b_x1, b_y1; // visible part of the bitmap | |
322 int clip_x0, clip_y0, clip_x1, clip_y1; | |
323 int tmp; | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
324 ass_image_t* img; |
18937 | 325 |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
326 dst_x += bm->left; |
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
327 dst_y += bm->top; |
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
328 brk -= bm->left; |
18937 | 329 |
330 // clipping | |
331 clip_x0 = render_context.clip_x0; | |
332 clip_y0 = render_context.clip_y0; | |
333 clip_x1 = render_context.clip_x1; | |
334 clip_y1 = render_context.clip_y1; | |
335 b_x0 = 0; | |
336 b_y0 = 0; | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
337 b_x1 = bm->w; |
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
338 b_y1 = bm->h; |
18937 | 339 |
340 tmp = dst_x - clip_x0; | |
341 if (tmp < 0) { | |
342 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "clip left\n"); | |
343 b_x0 = - tmp; | |
344 } | |
345 tmp = dst_y - clip_y0; | |
346 if (tmp < 0) { | |
347 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "clip top\n"); | |
348 b_y0 = - tmp; | |
349 } | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
350 tmp = clip_x1 - dst_x - bm->w; |
18937 | 351 if (tmp < 0) { |
352 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "clip right\n"); | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
353 b_x1 = bm->w + tmp; |
18937 | 354 } |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
355 tmp = clip_y1 - dst_y - bm->h; |
18937 | 356 if (tmp < 0) { |
357 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "clip bottom\n"); | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
358 b_y1 = bm->h + tmp; |
18937 | 359 } |
360 | |
361 if ((b_y0 >= b_y1) || (b_x0 >= b_x1)) | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
362 return tail; |
18937 | 363 |
364 if (brk > b_x0) { // draw left part | |
365 if (brk > b_x1) brk = b_x1; | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
366 img = my_draw_bitmap(bm->buffer + bm->w * b_y0 + b_x0, |
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
367 brk - b_x0, b_y1 - b_y0, bm->w, |
18937 | 368 dst_x + b_x0, dst_y + b_y0, color); |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
369 *tail = img; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
370 tail = &img->next; |
18937 | 371 } |
372 if (brk < b_x1) { // draw right part | |
373 if (brk < b_x0) brk = b_x0; | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
374 img = my_draw_bitmap(bm->buffer + bm->w * b_y0 + brk, |
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
375 b_x1 - brk, b_y1 - b_y0, bm->w, |
18937 | 376 dst_x + brk, dst_y + b_y0, color2); |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
377 *tail = img; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
378 tail = &img->next; |
18937 | 379 } |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
380 return tail; |
18937 | 381 } |
382 | |
383 /** | |
384 * \brief Render text_info_t struct into ass_images_t list | |
385 * Rasterize glyphs and put them in glyph cache. | |
386 */ | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
387 static ass_image_t* render_text(text_info_t* text_info, int dst_x, int dst_y) |
18937 | 388 { |
389 int pen_x, pen_y; | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
390 int i, error; |
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
391 bitmap_t* bm; |
18937 | 392 glyph_hash_val_t hash_val; |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
393 ass_image_t* head; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
394 ass_image_t** tail = &head; |
18937 | 395 |
396 for (i = 0; i < text_info->length; ++i) { | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
397 if (text_info->glyphs[i].glyph) { |
18937 | 398 if ((text_info->glyphs[i].symbol == '\n') || (text_info->glyphs[i].symbol == 0)) |
399 continue; | |
19848 | 400 error = glyph_to_bitmap(ass_instance->synth_priv, |
401 text_info->glyphs[i].glyph, text_info->glyphs[i].outline_glyph, | |
19965 | 402 &text_info->glyphs[i].bm, &text_info->glyphs[i].bm_o, |
403 &text_info->glyphs[i].bm_s, text_info->glyphs[i].be); | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
404 if (error) |
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
405 text_info->glyphs[i].symbol = 0; |
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
406 FT_Done_Glyph(text_info->glyphs[i].glyph); |
19873 | 407 if (text_info->glyphs[i].outline_glyph) |
408 FT_Done_Glyph(text_info->glyphs[i].outline_glyph); | |
18937 | 409 |
410 // cache | |
411 hash_val.bbox_scaled = text_info->glyphs[i].bbox; | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
412 hash_val.bm_o = text_info->glyphs[i].bm_o; |
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
413 hash_val.bm = text_info->glyphs[i].bm; |
19965 | 414 hash_val.bm_s = text_info->glyphs[i].bm_s; |
18937 | 415 hash_val.advance.x = text_info->glyphs[i].advance.x; |
416 hash_val.advance.y = text_info->glyphs[i].advance.y; | |
417 cache_add_glyph(&(text_info->glyphs[i].hash_key), &hash_val); | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
418 |
18937 | 419 } |
420 } | |
421 | |
422 for (i = 0; i < text_info->length; ++i) { | |
423 glyph_info_t* info = text_info->glyphs + i; | |
19965 | 424 if ((info->symbol == 0) || (info->symbol == '\n') || !info->bm_s || (info->shadow == 0)) |
425 continue; | |
426 | |
427 pen_x = dst_x + info->pos.x + info->shadow; | |
428 pen_y = dst_y + info->pos.y + info->shadow; | |
429 bm = info->bm_s; | |
430 | |
431 tail = render_glyph(bm, pen_x, pen_y, info->c[3], 0, 1000000, tail); | |
432 } | |
433 | |
434 for (i = 0; i < text_info->length; ++i) { | |
435 glyph_info_t* info = text_info->glyphs + i; | |
19873 | 436 if ((info->symbol == 0) || (info->symbol == '\n') || !info->bm_o) |
18937 | 437 continue; |
438 | |
439 pen_x = dst_x + info->pos.x; | |
440 pen_y = dst_y + info->pos.y; | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
441 bm = info->bm_o; |
18937 | 442 |
443 if ((info->effect_type == EF_KARAOKE_KO) && (info->effect_timing <= info->bbox.xMax)) { | |
444 // do nothing | |
445 } else | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
446 tail = render_glyph(bm, pen_x, pen_y, info->c[2], 0, 1000000, tail); |
18937 | 447 } |
448 for (i = 0; i < text_info->length; ++i) { | |
449 glyph_info_t* info = text_info->glyphs + i; | |
19873 | 450 if ((info->symbol == 0) || (info->symbol == '\n') || !info->bm) |
18937 | 451 continue; |
452 | |
453 pen_x = dst_x + info->pos.x; | |
454 pen_y = dst_y + info->pos.y; | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
455 bm = info->bm; |
18937 | 456 |
457 if ((info->effect_type == EF_KARAOKE) || (info->effect_type == EF_KARAOKE_KO)) { | |
458 if (info->effect_timing > info->bbox.xMax) | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
459 tail = render_glyph(bm, pen_x, pen_y, info->c[0], 0, 1000000, tail); |
18937 | 460 else |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
461 tail = render_glyph(bm, pen_x, pen_y, info->c[1], 0, 1000000, tail); |
18937 | 462 } else if (info->effect_type == EF_KARAOKE_KF) { |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
463 tail = render_glyph(bm, pen_x, pen_y, info->c[0], info->c[1], info->effect_timing, tail); |
18937 | 464 } else |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
465 tail = render_glyph(bm, pen_x, pen_y, info->c[0], 0, 1000000, tail); |
18937 | 466 } |
467 | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
468 *tail = 0; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
469 return head; |
18937 | 470 } |
471 | |
472 /** | |
473 * \brief Mapping between script and screen coordinates | |
474 */ | |
475 static int x2scr(int x) { | |
19940
fb2063eaa445
Fix width -> orig_width typo causing subtitles to be too far to the right
reimar
parents:
19932
diff
changeset
|
476 return x*frame_context.orig_width / frame_context.track->PlayResX + global_settings->left_margin; |
18937 | 477 } |
478 /** | |
479 * \brief Mapping between script and screen coordinates | |
480 */ | |
481 static int y2scr(int y) { | |
482 return y * frame_context.orig_height / frame_context.track->PlayResY + global_settings->top_margin; | |
483 } | |
484 // the same for toptitles | |
485 static int y2scr_top(int y) { | |
19538 | 486 if (global_settings->use_margins) |
487 return y * frame_context.orig_height / frame_context.track->PlayResY; | |
488 else | |
489 return y * frame_context.orig_height / frame_context.track->PlayResY + global_settings->top_margin; | |
18937 | 490 } |
491 // the same for subtitles | |
492 static int y2scr_sub(int y) { | |
19538 | 493 if (global_settings->use_margins) |
494 return y * frame_context.orig_height / frame_context.track->PlayResY + | |
495 global_settings->top_margin + global_settings->bottom_margin; | |
496 else | |
497 return y * frame_context.orig_height / frame_context.track->PlayResY + global_settings->top_margin; | |
18937 | 498 } |
499 | |
500 static void vmirror_bbox(FT_BBox* orig, FT_BBox* pbbox) { | |
501 pbbox->xMin = orig->xMin; | |
502 pbbox->xMax = orig->xMax; | |
503 pbbox->yMin = - orig->yMax; | |
504 pbbox->yMax = - orig->yMin; | |
505 } | |
506 | |
507 static void compute_string_bbox( text_info_t* info, FT_BBox *abbox ) { | |
508 FT_BBox bbox; | |
509 int n; | |
510 | |
511 /* initialize string bbox to "empty" values */ | |
512 bbox.xMin = bbox.yMin = 32000; | |
513 bbox.xMax = bbox.yMax = -32000; | |
514 | |
515 /* for each glyph image, compute its bounding box, */ | |
516 /* translate it, and grow the string bbox */ | |
517 for ( n = 0; n < info->length; n++ ) { | |
518 FT_BBox glyph_bbox; | |
519 vmirror_bbox( &(info->glyphs[n].bbox), &glyph_bbox ); | |
520 glyph_bbox.xMin += info->glyphs[n].pos.x; | |
521 glyph_bbox.xMax += info->glyphs[n].pos.x; | |
522 glyph_bbox.yMin += info->glyphs[n].pos.y; | |
523 glyph_bbox.yMax += info->glyphs[n].pos.y; | |
524 if ( glyph_bbox.xMin < bbox.xMin ) bbox.xMin = glyph_bbox.xMin; | |
525 if ( glyph_bbox.yMin < bbox.yMin ) bbox.yMin = glyph_bbox.yMin; | |
526 if ( glyph_bbox.xMax > bbox.xMax ) bbox.xMax = glyph_bbox.xMax; | |
527 if ( glyph_bbox.yMax > bbox.yMax ) bbox.yMax = glyph_bbox.yMax; | |
528 } | |
529 | |
530 /* check that we really grew the string bbox */ | |
531 if ( bbox.xMin > bbox.xMax ) { | |
532 bbox.xMin = 0; | |
533 bbox.yMin = 0; | |
534 bbox.xMax = 0; | |
535 bbox.yMax = 0; | |
536 } | |
537 | |
538 /* return string bbox */ | |
539 *abbox = bbox; | |
540 } | |
541 | |
542 | |
543 /** | |
544 * \brief Check if starting part of (*p) matches sample. If true, shift p to the first symbol after the matching part. | |
545 */ | |
19104
2ec2301183cd
marks several read-only string parameters which aren't modified inside the called function as const. Patch by Stefan Huehner, stefan AT huehner-org
reynaldo
parents:
19066
diff
changeset
|
546 static inline int mystrcmp(char** p, const char* sample) { |
18937 | 547 int len = strlen(sample); |
548 if (strncmp(*p, sample, len) == 0) { | |
549 (*p) += len; | |
550 return 1; | |
551 } else | |
552 return 0; | |
553 } | |
554 | |
19401
c0c3a2f8bb32
Add subdata to ass_track conversion for external subtitles.
eugeni
parents:
19399
diff
changeset
|
555 double ass_internal_font_size_coeff = 0.8; |
c0c3a2f8bb32
Add subdata to ass_track conversion for external subtitles.
eugeni
parents:
19399
diff
changeset
|
556 |
18937 | 557 static void change_font_size(int sz) |
558 { | |
19825
f351a3fc3e42
Make font outline width proportional to movie resolution.
eugeni
parents:
19716
diff
changeset
|
559 double size = sz * frame_context.font_scale; |
18937 | 560 |
561 if (size < 1) | |
562 size = 1; | |
563 else if (size > frame_context.height * 2) | |
564 size = frame_context.height * 2; | |
565 | |
566 FT_Set_Pixel_Sizes(render_context.face, 0, size); | |
567 | |
568 render_context.font_size = sz; | |
569 } | |
570 | |
571 /** | |
572 * \brief Change current font, using setting from render_context. | |
573 */ | |
574 static void update_font(void) | |
575 { | |
576 int error; | |
577 unsigned val; | |
578 ass_instance_t* priv = frame_context.ass_priv; | |
579 face_desc_t desc; | |
580 desc.family = strdup(render_context.family); | |
581 | |
582 val = render_context.bold; | |
583 // 0 = normal, 1 = bold, >1 = exact weight | |
584 if (val == 0) val = 80; // normal | |
585 else if (val == 1) val = 200; // bold | |
586 desc.bold = val; | |
587 | |
588 val = render_context.italic; | |
589 if (val == 0) val = 0; // normal | |
590 else if (val == 1) val = 110; //italic | |
591 desc.italic = val; | |
592 | |
593 error = ass_new_face(priv->library, priv->fontconfig_priv, &desc, &(render_context.face)); | |
594 if (error) { | |
595 render_context.face = 0; | |
596 } | |
597 | |
598 if (render_context.face) | |
599 change_font_size(render_context.font_size); | |
600 } | |
601 | |
602 /** | |
603 * \brief Change border width | |
19873 | 604 * negative value resets border to style value |
18937 | 605 */ |
606 static void change_border(double border) | |
607 { | |
19918
1de67089c19a
Fix FT_Stroker use in libass. The previous variant ended up with 0-width
eugeni
parents:
19917
diff
changeset
|
608 int b; |
1de67089c19a
Fix FT_Stroker use in libass. The previous variant ended up with 0-width
eugeni
parents:
19917
diff
changeset
|
609 if (!render_context.face) return; |
1de67089c19a
Fix FT_Stroker use in libass. The previous variant ended up with 0-width
eugeni
parents:
19917
diff
changeset
|
610 |
19919 | 611 if (border < 0) { |
612 if (render_context.style->BorderStyle == 1) { | |
613 if (render_context.style->Outline == 0 && render_context.style->Shadow > 0) | |
19873 | 614 border = 1.; |
19919 | 615 else |
616 border = render_context.style->Outline; | |
617 } else | |
618 border = 1.; | |
619 } | |
620 render_context.border = border; | |
19873 | 621 |
19919 | 622 b = 64 * border * frame_context.border_scale; |
623 if (b > 0) { | |
19918
1de67089c19a
Fix FT_Stroker use in libass. The previous variant ended up with 0-width
eugeni
parents:
19917
diff
changeset
|
624 if (!render_context.stroker) { |
1de67089c19a
Fix FT_Stroker use in libass. The previous variant ended up with 0-width
eugeni
parents:
19917
diff
changeset
|
625 int error; |
1de67089c19a
Fix FT_Stroker use in libass. The previous variant ended up with 0-width
eugeni
parents:
19917
diff
changeset
|
626 #if (FREETYPE_MAJOR > 2) || ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR > 1)) |
1de67089c19a
Fix FT_Stroker use in libass. The previous variant ended up with 0-width
eugeni
parents:
19917
diff
changeset
|
627 error = FT_Stroker_New( ass_instance->library, &render_context.stroker ); |
1de67089c19a
Fix FT_Stroker use in libass. The previous variant ended up with 0-width
eugeni
parents:
19917
diff
changeset
|
628 #else // < 2.2 |
1de67089c19a
Fix FT_Stroker use in libass. The previous variant ended up with 0-width
eugeni
parents:
19917
diff
changeset
|
629 error = FT_Stroker_New( render_context.face->memory, &render_context.stroker ); |
1de67089c19a
Fix FT_Stroker use in libass. The previous variant ended up with 0-width
eugeni
parents:
19917
diff
changeset
|
630 #endif |
1de67089c19a
Fix FT_Stroker use in libass. The previous variant ended up with 0-width
eugeni
parents:
19917
diff
changeset
|
631 if (error) { |
1de67089c19a
Fix FT_Stroker use in libass. The previous variant ended up with 0-width
eugeni
parents:
19917
diff
changeset
|
632 mp_msg(MSGT_GLOBAL, MSGL_V, "failed to get stroker\n"); |
1de67089c19a
Fix FT_Stroker use in libass. The previous variant ended up with 0-width
eugeni
parents:
19917
diff
changeset
|
633 render_context.stroker = 0; |
1de67089c19a
Fix FT_Stroker use in libass. The previous variant ended up with 0-width
eugeni
parents:
19917
diff
changeset
|
634 } |
1de67089c19a
Fix FT_Stroker use in libass. The previous variant ended up with 0-width
eugeni
parents:
19917
diff
changeset
|
635 } |
1de67089c19a
Fix FT_Stroker use in libass. The previous variant ended up with 0-width
eugeni
parents:
19917
diff
changeset
|
636 if (render_context.stroker) |
19873 | 637 FT_Stroker_Set( render_context.stroker, b, |
19919 | 638 FT_STROKER_LINECAP_ROUND, |
639 FT_STROKER_LINEJOIN_ROUND, | |
640 0 ); | |
641 } else { | |
642 FT_Stroker_Done(render_context.stroker); | |
643 render_context.stroker = 0; | |
644 } | |
18937 | 645 } |
646 | |
647 #define _r(c) ((c)>>24) | |
648 #define _g(c) (((c)>>16)&0xFF) | |
649 #define _b(c) (((c)>>8)&0xFF) | |
650 #define _a(c) ((c)&0xFF) | |
651 | |
652 /** | |
653 * \brief Calculate a weighted average of two colors | |
654 * calculates c1*(1-a) + c2*a, but separately for each component except alpha | |
655 */ | |
656 static void change_color(uint32_t* var, uint32_t new, double pwr) | |
657 { | |
658 (*var)= ((uint32_t)(_r(*var) * (1 - pwr) + _r(new) * pwr) << 24) + | |
659 ((uint32_t)(_g(*var) * (1 - pwr) + _g(new) * pwr) << 16) + | |
660 ((uint32_t)(_b(*var) * (1 - pwr) + _b(new) * pwr) << 8) + | |
661 _a(*var); | |
662 } | |
663 | |
664 // like change_color, but for alpha component only | |
665 static void change_alpha(uint32_t* var, uint32_t new, double pwr) | |
666 { | |
667 *var = (_r(*var) << 24) + (_g(*var) << 16) + (_b(*var) << 8) + (_a(*var) * (1 - pwr) + _a(new) * pwr); | |
668 } | |
669 | |
19406
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
670 /** |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
671 * \brief Multiply two alpha values |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
672 * \param a first value |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
673 * \param b second value |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
674 * \return result of multiplication |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
675 * Parameters and result are limited by 0xFF. |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
676 */ |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
677 static uint32_t mult_alpha(uint32_t a, uint32_t b) |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
678 { |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
679 return 0xFF - (0xFF - a) * (0xFF - b) / 0xFF; |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
680 } |
18937 | 681 |
682 /** | |
683 * \brief Calculate alpha value by piecewise linear function | |
684 * Used for \fad, \fade implementation. | |
685 */ | |
19692
5b40e87b9619
Change \fad behaviour so that it does not get cancelled by \r.
eugeni
parents:
19691
diff
changeset
|
686 static unsigned interpolate_alpha(long long now, |
18937 | 687 long long t1, long long t2, long long t3, long long t4, |
688 unsigned a1, unsigned a2, unsigned a3) | |
689 { | |
690 unsigned a; | |
691 double cf; | |
692 if (now <= t1) { | |
693 a = a1; | |
694 } else if (now >= t4) { | |
695 a = a3; | |
696 } else if (now < t2) { // and > t1 | |
697 cf = ((double)(now - t1)) / (t2 - t1); | |
698 a = a1 * (1 - cf) + a2 * cf; | |
699 } else if (now > t3) { | |
700 cf = ((double)(now - t3)) / (t4 - t3); | |
701 a = a2 * (1 - cf) + a3 * cf; | |
702 } else { // t2 <= now <= t3 | |
703 a = a2; | |
704 } | |
705 | |
19692
5b40e87b9619
Change \fad behaviour so that it does not get cancelled by \r.
eugeni
parents:
19691
diff
changeset
|
706 return a; |
18937 | 707 } |
708 | |
19873 | 709 static void reset_render_context(); |
710 | |
18937 | 711 /** |
712 * \brief Parse style override tag. | |
713 * \param p string to parse | |
714 * \param pwr multiplier for some tag effects (comes from \t tags) | |
715 */ | |
716 static char* parse_tag(char* p, double pwr) { | |
717 #define skip_all(x) if (*p == (x)) ++p; else { \ | |
718 while ((*p != (x)) && (*p != '}') && (*p != 0)) {++p;} } | |
719 #define skip(x) if (*p == (x)) ++p; else { return p; } | |
720 | |
721 skip_all('\\'); | |
722 if ((*p == '}') || (*p == 0)) | |
723 return p; | |
724 | |
725 if (mystrcmp(&p, "fsc")) { | |
726 char tp = *p++; | |
727 double val; | |
728 if (tp == 'x') { | |
729 if (mystrtod(&p, &val)) { | |
730 val /= 100; | |
731 render_context.scale_x = (val - 1.) * pwr + 1.; | |
732 } else | |
733 render_context.scale_x = render_context.style->ScaleX; | |
734 } else if (tp == 'y') { | |
735 if (mystrtod(&p, &val)) { | |
736 val /= 100; | |
737 render_context.scale_y = (val - 1.) * pwr + 1.; | |
738 } else | |
739 render_context.scale_y = render_context.style->ScaleY; | |
740 } | |
741 } else if (mystrcmp(&p, "fsp")) { | |
742 int val; | |
743 if (mystrtoi(&p, 10, &val)) | |
744 render_context.hspacing = val * pwr; | |
745 else | |
746 render_context.hspacing = 0; | |
747 } else if (mystrcmp(&p, "fs")) { | |
748 int val; | |
749 if (mystrtoi(&p, 10, &val)) | |
750 val = render_context.font_size * ( 1 - pwr ) + val * pwr; | |
751 else | |
752 val = render_context.style->FontSize; | |
753 if (render_context.face) | |
754 change_font_size(val); | |
755 } else if (mystrcmp(&p, "bord")) { | |
756 double val; | |
757 if (mystrtod(&p, &val)) | |
758 val = render_context.border * ( 1 - pwr ) + val * pwr; | |
759 else | |
19873 | 760 val = -1.; // reset to default |
18937 | 761 change_border(val); |
762 } else if (mystrcmp(&p, "move")) { | |
763 int x1, x2, y1, y2; | |
19044 | 764 long long t1, t2, delta_t, t; |
18937 | 765 int x, y; |
766 double k; | |
767 skip('('); | |
768 x1 = strtol(p, &p, 10); | |
769 skip(','); | |
770 y1 = strtol(p, &p, 10); | |
771 skip(','); | |
772 x2 = strtol(p, &p, 10); | |
773 skip(','); | |
774 y2 = strtol(p, &p, 10); | |
19044 | 775 if (*p == ',') { |
776 skip(','); | |
777 t1 = strtoll(p, &p, 10); | |
778 skip(','); | |
779 t2 = strtoll(p, &p, 10); | |
19378 | 780 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "movement6: (%d, %d) -> (%d, %d), (%" PRId64 " .. %" PRId64 ")\n", |
781 x1, y1, x2, y2, (int64_t)t1, (int64_t)t2); | |
19044 | 782 } else { |
783 t1 = 0; | |
784 t2 = render_context.event->Duration; | |
785 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "movement: (%d, %d) -> (%d, %d)\n", x1, y1, x2, y2); | |
786 } | |
787 skip(')'); | |
788 delta_t = t2 - t1; | |
789 t = frame_context.time - render_context.event->Start; | |
790 if (t < t1) | |
791 k = 0.; | |
792 else if (t > t2) | |
793 k = 1.; | |
794 else k = ((double)(t - t1)) / delta_t; | |
18937 | 795 x = k * (x2 - x1) + x1; |
796 y = k * (y2 - y1) + y1; | |
797 render_context.pos_x = x; | |
798 render_context.pos_y = y; | |
799 render_context.detect_collisions = 0; | |
800 render_context.evt_type = EVENT_POSITIONED; | |
801 } else if (mystrcmp(&p, "frx") || mystrcmp(&p, "fry")) { | |
19622 | 802 double val; |
803 mystrtod(&p, &val); | |
18937 | 804 mp_msg(MSGT_GLOBAL, MSGL_V, "frx/fry unimplemented \n"); |
805 } else if (mystrcmp(&p, "frz") || mystrcmp(&p, "fr")) { | |
806 double angle; | |
19622 | 807 double val; |
808 mystrtod(&p, &val); | |
18937 | 809 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "setting rotation to %.2f\n", val * pwr); |
810 angle = M_PI * val / 180; | |
811 render_context.rotation = angle * pwr; | |
812 } else if (mystrcmp(&p, "fn")) { | |
813 char* start = p; | |
814 char* family; | |
815 skip_all('\\'); | |
816 family = malloc(p - start + 1); | |
817 strncpy(family, start, p - start); | |
818 family[p - start] = '\0'; | |
819 if (render_context.family) | |
820 free(render_context.family); | |
821 render_context.family = family; | |
822 update_font(); | |
823 } else if (mystrcmp(&p, "alpha")) { | |
824 uint32_t val; | |
19691 | 825 int i; |
18937 | 826 if (strtocolor(&p, &val)) { |
827 unsigned char a = val >> 24; | |
19691 | 828 for (i = 0; i < 4; ++i) |
829 change_alpha(&render_context.c[i], a, pwr); | |
18937 | 830 } else { |
19691 | 831 change_alpha(&render_context.c[0], render_context.style->PrimaryColour, pwr); |
832 change_alpha(&render_context.c[1], render_context.style->SecondaryColour, pwr); | |
833 change_alpha(&render_context.c[2], render_context.style->OutlineColour, pwr); | |
834 change_alpha(&render_context.c[3], render_context.style->BackColour, pwr); | |
18937 | 835 } |
836 // FIXME: simplify | |
837 } else if (mystrcmp(&p, "an")) { | |
838 int val = strtol(p, &p, 10); | |
839 int v = (val - 1) / 3; // 0, 1 or 2 for vertical alignment | |
840 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "an %d\n", val); | |
841 if (v != 0) v = 3 - v; | |
842 val = ((val - 1) % 3) + 1; // horizontal alignment | |
843 val += v*4; | |
844 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "align %d\n", val); | |
845 render_context.alignment = val; | |
846 } else if (mystrcmp(&p, "a")) { | |
847 int val = strtol(p, &p, 10); | |
848 render_context.alignment = val; | |
849 } else if (mystrcmp(&p, "pos")) { | |
850 int v1, v2; | |
851 skip('('); | |
852 v1 = strtol(p, &p, 10); | |
853 skip(','); | |
854 v2 = strtol(p, &p, 10); | |
855 skip(')'); | |
856 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "pos(%d, %d)\n", v1, v2); | |
857 render_context.evt_type = EVENT_POSITIONED; | |
858 render_context.detect_collisions = 0; | |
859 render_context.pos_x = v1; | |
860 render_context.pos_y = v2; | |
19398
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
861 } else if (mystrcmp(&p, "fad")) { |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
862 int a1, a2, a3; |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
863 long long t1, t2, t3, t4; |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
864 if (*p == 'e') ++p; // either \fad or \fade |
18937 | 865 skip('('); |
866 a1 = strtol(p, &p, 10); | |
867 skip(','); | |
868 a2 = strtol(p, &p, 10); | |
19398
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
869 if (*p == ')') { |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
870 // 2-argument version (\fad, according to specs) |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
871 // a1 and a2 are fade-in and fade-out durations |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
872 t1 = 0; |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
873 t4 = render_context.event->Duration; |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
874 t2 = a1; |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
875 t3 = t4 - a2; |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
876 a1 = 0xFF; |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
877 a2 = 0; |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
878 a3 = 0xFF; |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
879 } else { |
19399 | 880 // 6-argument version (\fade) |
881 // a1 and a2 (and a3) are opacity values | |
882 skip(','); | |
883 a3 = strtol(p, &p, 10); | |
884 skip(','); | |
885 t1 = strtoll(p, &p, 10); | |
886 skip(','); | |
887 t2 = strtoll(p, &p, 10); | |
888 skip(','); | |
889 t3 = strtoll(p, &p, 10); | |
890 skip(','); | |
891 t4 = strtoll(p, &p, 10); | |
19398
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
892 } |
18937 | 893 skip(')'); |
19692
5b40e87b9619
Change \fad behaviour so that it does not get cancelled by \r.
eugeni
parents:
19691
diff
changeset
|
894 render_context.fade = interpolate_alpha(frame_context.time - render_context.event->Start, t1, t2, t3, t4, a1, a2, a3); |
18937 | 895 } else if (mystrcmp(&p, "org")) { |
896 int v1, v2; | |
897 skip('('); | |
898 v1 = strtol(p, &p, 10); | |
899 skip(','); | |
900 v2 = strtol(p, &p, 10); | |
901 skip(')'); | |
902 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "org(%d, %d)\n", v1, v2); | |
903 // render_context.evt_type = EVENT_POSITIONED; | |
904 render_context.org_x = v1; | |
905 render_context.org_y = v2; | |
906 } else if (mystrcmp(&p, "t")) { | |
907 double v[3]; | |
908 int v1, v2; | |
909 double v3; | |
910 int cnt; | |
911 long long t1, t2, t, delta_t; | |
912 double k; | |
913 skip('('); | |
914 for (cnt = 0; cnt < 3; ++cnt) { | |
915 if (*p == '\\') | |
916 break; | |
917 v[cnt] = strtod(p, &p); | |
918 skip(','); | |
919 } | |
920 if (cnt == 3) { | |
921 v1 = v[0]; v2 = v[1]; v3 = v[2]; | |
922 } else if (cnt == 2) { | |
923 v1 = v[0]; v2 = v[1]; v3 = 1.; | |
924 } else if (cnt == 1) { | |
925 v1 = 0; v2 = render_context.event->Duration; v3 = v[0]; | |
926 } else { // cnt == 0 | |
927 v1 = 0; v2 = render_context.event->Duration; v3 = 1.; | |
928 } | |
929 render_context.detect_collisions = 0; | |
930 t1 = v1; | |
931 t2 = v2; | |
932 delta_t = v2 - v1; | |
933 t = frame_context.time - render_context.event->Start; // FIXME: move to render_context | |
934 if (t < t1) | |
935 k = 0.; | |
936 else if (t > t2) | |
937 k = 1.; | |
938 else k = ((double)(t - t1)) / delta_t; | |
939 while (*p == '\\') | |
940 p = parse_tag(p, k); // maybe k*pwr ? no, specs forbid nested \t's | |
941 skip_all(')'); // FIXME: better skip(')'), but much more tags support required | |
942 } else if (mystrcmp(&p, "clip")) { | |
943 int x0, y0, x1, y1; | |
944 int res = 1; | |
945 skip('('); | |
946 res &= mystrtoi(&p, 10, &x0); | |
947 skip(','); | |
948 res &= mystrtoi(&p, 10, &y0); | |
949 skip(','); | |
950 res &= mystrtoi(&p, 10, &x1); | |
951 skip(','); | |
952 res &= mystrtoi(&p, 10, &y1); | |
953 skip(')'); | |
954 if (res) { | |
955 render_context.clip_x0 = render_context.clip_x0 * (1-pwr) + x0 * pwr; | |
956 render_context.clip_x1 = render_context.clip_x1 * (1-pwr) + x1 * pwr; | |
957 render_context.clip_y0 = render_context.clip_y0 * (1-pwr) + y0 * pwr; | |
958 render_context.clip_y1 = render_context.clip_y1 * (1-pwr) + y1 * pwr; | |
959 } else { | |
960 render_context.clip_x0 = 0; | |
961 render_context.clip_y0 = 0; | |
962 render_context.clip_x1 = frame_context.track->PlayResX; | |
963 render_context.clip_y1 = frame_context.track->PlayResY; | |
964 } | |
965 } else if (mystrcmp(&p, "c")) { | |
966 uint32_t val; | |
967 if (!strtocolor(&p, &val)) | |
968 val = render_context.style->PrimaryColour; | |
969 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "color: %X\n", val); | |
19691 | 970 change_color(&render_context.c[0], val, pwr); |
18937 | 971 } else if ((*p >= '1') && (*p <= '4') && (++p) && (mystrcmp(&p, "c") || mystrcmp(&p, "a"))) { |
972 char n = *(p-2); | |
19691 | 973 int cidx = n - '1'; |
18937 | 974 char cmd = *(p-1); |
975 uint32_t val; | |
976 assert((n >= '1') && (n <= '4')); | |
977 if (!strtocolor(&p, &val)) | |
978 switch(n) { | |
979 case '1': val = render_context.style->PrimaryColour; break; | |
980 case '2': val = render_context.style->SecondaryColour; break; | |
981 case '3': val = render_context.style->OutlineColour; break; | |
982 case '4': val = render_context.style->BackColour; break; | |
983 default : val = 0; break; // impossible due to assert; avoid compilation warning | |
984 } | |
985 switch (cmd) { | |
19691 | 986 case 'c': change_color(render_context.c + cidx, val, pwr); break; |
987 case 'a': change_alpha(render_context.c + cidx, val >> 24, pwr); break; | |
18937 | 988 default: mp_msg(MSGT_GLOBAL, MSGL_WARN, "Bad command: %c%c\n", n, cmd); break; |
989 } | |
19691 | 990 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "single c/a at %f: %c%c = %X \n", pwr, n, cmd, render_context.c[cidx]); |
18937 | 991 } else if (mystrcmp(&p, "r")) { |
19873 | 992 reset_render_context(); |
18937 | 993 } else if (mystrcmp(&p, "be")) { |
994 int val; | |
19848 | 995 if (mystrtoi(&p, 10, &val)) |
996 render_context.be = val ? 1 : 0; | |
997 else | |
998 render_context.be = 0; | |
18937 | 999 mp_msg(MSGT_GLOBAL, MSGL_V, "be unimplemented \n"); |
1000 } else if (mystrcmp(&p, "b")) { | |
1001 int b; | |
1002 if (mystrtoi(&p, 10, &b)) | |
1003 render_context.bold = b; | |
1004 else | |
1005 render_context.bold = - render_context.style->Bold; | |
1006 update_font(); | |
1007 } else if (mystrcmp(&p, "i")) { | |
1008 int i; | |
1009 if (mystrtoi(&p, 10, &i)) | |
1010 render_context.italic = i; | |
1011 else | |
1012 render_context.italic = - render_context.style->Italic; | |
1013 update_font(); | |
1014 } else if (mystrcmp(&p, "kf") || mystrcmp(&p, "K")) { | |
1015 int val = strtol(p, &p, 10); | |
1016 render_context.effect_type = EF_KARAOKE_KF; | |
19716
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
1017 if (render_context.effect_timing) |
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
1018 render_context.effect_skip_timing += render_context.effect_timing; |
18937 | 1019 render_context.effect_timing = val * 10; |
1020 } else if (mystrcmp(&p, "ko")) { | |
1021 int val = strtol(p, &p, 10); | |
1022 render_context.effect_type = EF_KARAOKE_KO; | |
19716
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
1023 if (render_context.effect_timing) |
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
1024 render_context.effect_skip_timing += render_context.effect_timing; |
18937 | 1025 render_context.effect_timing = val * 10; |
1026 } else if (mystrcmp(&p, "k")) { | |
1027 int val = strtol(p, &p, 10); | |
1028 render_context.effect_type = EF_KARAOKE; | |
19716
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
1029 if (render_context.effect_timing) |
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
1030 render_context.effect_skip_timing += render_context.effect_timing; |
18937 | 1031 render_context.effect_timing = val * 10; |
19965 | 1032 } else if (mystrcmp(&p, "shad")) { |
1033 int val; | |
1034 if (mystrtoi(&p, 10, &val)) | |
1035 render_context.shadow = val; | |
1036 else | |
1037 render_context.shadow = render_context.style->Shadow; | |
18937 | 1038 } |
1039 | |
1040 return p; | |
1041 | |
1042 #undef skip | |
1043 #undef skip_all | |
1044 } | |
1045 | |
1046 /** | |
1047 * \brief Get next ucs4 char from string, parsing and executing style overrides | |
1048 * \param str string pointer | |
1049 * \return ucs4 code of the next char | |
1050 * On return str points to the unparsed part of the string | |
1051 */ | |
1052 static unsigned get_next_char(char** str) | |
1053 { | |
1054 char* p = *str; | |
1055 unsigned chr; | |
1056 if (*p == '{') { // '\0' goes here | |
1057 p++; | |
1058 while (1) { | |
1059 p = parse_tag(p, 1.); | |
1060 if (*p == '}') { // end of tag | |
1061 p++; | |
1062 if (*p == '{') { | |
1063 p++; | |
1064 continue; | |
1065 } else | |
1066 break; | |
1067 } else if (*p != '\\') | |
1068 mp_msg(MSGT_GLOBAL, MSGL_V, "Unable to parse: \"%s\" \n", p); | |
1069 if (*p == 0) | |
1070 break; | |
1071 } | |
1072 } | |
1073 if (*p == '\t') { | |
1074 ++p; | |
1075 *str = p; | |
1076 return ' '; | |
1077 } | |
1078 if (*p == '\\') { | |
1079 if ((*(p+1) == 'N') || ((*(p+1) == 'n') && (frame_context.track->WrapStyle == 2))) { | |
1080 p += 2; | |
1081 *str = p; | |
1082 return '\n'; | |
1083 } else if (*(p+1) == 'n') { | |
1084 p += 2; | |
1085 *str = p; | |
1086 return ' '; | |
1087 } | |
1088 } | |
1089 chr = utf8_get_char(&p); | |
1090 *str = p; | |
1091 return chr; | |
1092 } | |
1093 | |
19556 | 1094 static void apply_transition_effects(ass_event_t* event) |
1095 { | |
1096 int v[4]; | |
1097 int cnt; | |
1098 char* p = event->Effect; | |
1099 | |
1100 if (!p || !*p) return; | |
1101 | |
1102 cnt = 0; | |
1103 while (cnt < 4 && (p = strchr(p, ';'))) { | |
1104 v[cnt++] = atoi(++p); | |
1105 } | |
1106 | |
1107 if (strncmp(event->Effect, "Banner;", 7) == 0) { | |
1108 int delay; | |
1109 if (cnt < 1) { | |
1110 mp_msg(MSGT_GLOBAL, MSGL_V, "Error parsing effect: %s \n", event->Effect); | |
1111 return; | |
1112 } | |
1113 if (cnt >= 2 && v[1] == 0) // right-to-left | |
1114 render_context.scroll_direction = SCROLL_RL; | |
1115 else // left-to-right | |
1116 render_context.scroll_direction = SCROLL_LR; | |
1117 | |
1118 delay = v[0]; | |
1119 if (delay == 0) delay = 1; // ? | |
1120 render_context.scroll_shift = (frame_context.time - render_context.event->Start) / delay; | |
1121 render_context.evt_type = EVENT_HSCROLL; | |
1122 return; | |
1123 } | |
1124 | |
1125 if (strncmp(event->Effect, "Scroll up;", 10) == 0) { | |
1126 render_context.scroll_direction = SCROLL_BT; | |
1127 } else if (strncmp(event->Effect, "Scroll down;", 12) == 0) { | |
1128 render_context.scroll_direction = SCROLL_TB; | |
1129 } else { | |
1130 mp_msg(MSGT_GLOBAL, MSGL_V, "Unknown transition effect: %s \n", event->Effect); | |
1131 return; | |
1132 } | |
1133 // parse scroll up/down parameters | |
1134 { | |
1135 int delay; | |
1136 int y0, y1; | |
1137 if (cnt < 3) { | |
1138 mp_msg(MSGT_GLOBAL, MSGL_V, "Error parsing effect: %s \n", event->Effect); | |
1139 return; | |
1140 } | |
1141 delay = v[2]; | |
1142 if (delay == 0) delay = 1; // ? | |
1143 render_context.scroll_shift = (frame_context.time - render_context.event->Start) / delay; | |
1144 if (v[0] < v[1]) { | |
1145 y0 = v[0]; y1 = v[1]; | |
1146 } else { | |
1147 y0 = v[1]; y1 = v[0]; | |
1148 } | |
1149 if (y1 == 0) | |
1150 y1 = frame_context.track->PlayResY; // y0=y1=0 means fullscreen scrolling | |
1151 render_context.clip_y0 = y0; | |
1152 render_context.clip_y1 = y1; | |
1153 render_context.evt_type = EVENT_VSCROLL; | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1154 render_context.detect_collisions = 0; |
19556 | 1155 } |
1156 | |
1157 } | |
1158 | |
18937 | 1159 /** |
19873 | 1160 * \brief partially reset render_context to style values |
1161 * Works like {\r}: resets some style overrides | |
1162 */ | |
1163 static void reset_render_context(void) | |
1164 { | |
1165 render_context.c[0] = render_context.style->PrimaryColour; | |
1166 render_context.c[1] = render_context.style->SecondaryColour; | |
1167 render_context.c[2] = render_context.style->OutlineColour; | |
1168 render_context.c[3] = render_context.style->BackColour; | |
1169 render_context.font_size = render_context.style->FontSize; | |
1170 | |
1171 if (render_context.family) | |
1172 free(render_context.family); | |
1173 render_context.family = strdup(render_context.style->FontName); | |
1174 render_context.bold = - render_context.style->Bold; | |
1175 render_context.italic = - render_context.style->Italic; | |
1176 update_font(); | |
1177 | |
1178 change_border(-1.); | |
1179 render_context.scale_x = render_context.style->ScaleX; | |
1180 render_context.scale_y = render_context.style->ScaleY; | |
1181 render_context.hspacing = 0; // FIXME | |
1182 render_context.be = 0; | |
19965 | 1183 render_context.shadow = render_context.style->Shadow; |
19873 | 1184 |
1185 // FIXME: does not reset unsupported attributes. | |
1186 } | |
1187 | |
1188 /** | |
18937 | 1189 * \brief Start new event. Reset render_context. |
1190 */ | |
19873 | 1191 static void init_render_context(ass_event_t* event) |
18937 | 1192 { |
1193 render_context.event = event; | |
1194 render_context.style = frame_context.track->styles + event->Style; | |
19873 | 1195 |
1196 reset_render_context(); | |
1197 | |
18937 | 1198 render_context.evt_type = EVENT_NORMAL; |
1199 render_context.alignment = 0; | |
19404 | 1200 render_context.rotation = M_PI * render_context.style->Angle / 180.; |
18937 | 1201 render_context.pos_x = 0; |
1202 render_context.pos_y = 0; | |
1203 render_context.org_x = 0; | |
1204 render_context.org_y = 0; | |
1205 render_context.clip_x0 = 0; | |
1206 render_context.clip_y0 = 0; | |
1207 render_context.clip_x1 = frame_context.track->PlayResX; | |
1208 render_context.clip_y1 = frame_context.track->PlayResY; | |
1209 render_context.detect_collisions = 1; | |
19692
5b40e87b9619
Change \fad behaviour so that it does not get cancelled by \r.
eugeni
parents:
19691
diff
changeset
|
1210 render_context.fade = 0; |
19716
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
1211 render_context.effect_type = EF_NONE; |
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
1212 render_context.effect_timing = 0; |
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
1213 render_context.effect_skip_timing = 0; |
18937 | 1214 |
19556 | 1215 apply_transition_effects(event); |
18937 | 1216 } |
1217 | |
19873 | 1218 static void free_render_context(void) |
18937 | 1219 { |
1220 } | |
1221 | |
1222 /** | |
1223 * \brief Get normal and outline glyphs from cache (if possible) or font face | |
1224 * \param index face glyph index | |
1225 * \param symbol ucs4 char | |
1226 * \param info out: struct filled with extracted data | |
1227 * \param advance advance vector of the extracted glyph | |
1228 * \return 0 on success | |
1229 */ | |
1230 static int get_glyph(int index, int symbol, glyph_info_t* info, FT_Vector* advance) | |
1231 { | |
1232 int error; | |
1233 glyph_hash_val_t* val; | |
1234 glyph_hash_key_t* key = &(info->hash_key); | |
1235 | |
1236 key->face = render_context.face; | |
1237 key->size = render_context.font_size; | |
1238 key->index = index; | |
1239 key->outline = (render_context.border * 0xFFFF); // convert to 16.16 | |
1240 key->scale_x = (render_context.scale_x * 0xFFFF); | |
1241 key->scale_y = (render_context.scale_y * 0xFFFF); | |
1242 key->angle = (render_context.rotation * 0xFFFF); | |
1243 key->advance = *advance; | |
1244 key->bold = render_context.bold; | |
1245 key->italic = render_context.italic; | |
19848 | 1246 key->be = render_context.be; |
18937 | 1247 |
1248 val = cache_find_glyph(key); | |
1249 // val = 0; | |
1250 | |
1251 if (val) { | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
1252 info->glyph = info->outline_glyph = 0; |
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
1253 info->bm = val->bm; |
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
1254 info->bm_o = val->bm_o; |
19965 | 1255 info->bm_s = val->bm_s; |
18937 | 1256 info->bbox = val->bbox_scaled; |
1257 info->advance.x = val->advance.x; | |
1258 info->advance.y = val->advance.y; | |
1259 | |
1260 return 0; | |
1261 } | |
1262 | |
1263 // not found, get a new outline glyph from face | |
1264 // mp_msg(MSGT_GLOBAL, MSGL_INFO, "miss, index = %d, symbol = %c, adv = (%d, %d)\n", index, symbol, advance->x, advance->y); | |
1265 | |
1266 error = FT_Load_Glyph(render_context.face, index, FT_LOAD_NO_BITMAP ); | |
1267 if (error) { | |
1268 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Error loading glyph\n"); | |
1269 return error; | |
1270 } | |
1271 | |
1272 #if (FREETYPE_MAJOR > 2) || \ | |
1273 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR >= 2)) || \ | |
1274 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR == 1) && (FREETYPE_PATCH >= 10)) | |
1275 // FreeType >= 2.1.10 required | |
1276 if (!(render_context.face->style_flags & FT_STYLE_FLAG_ITALIC) && | |
1277 ((render_context.italic == 1) || (render_context.italic > 55))) { | |
1278 FT_GlyphSlot_Oblique(render_context.face->glyph); | |
1279 } | |
1280 #endif | |
1281 error = FT_Get_Glyph(render_context.face->glyph, &(info->glyph)); | |
1282 if (error) { | |
1283 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Error getting glyph\n"); | |
1284 return error; | |
1285 } | |
1286 | |
1287 info->advance.x = info->glyph->advance.x >> 10; | |
1288 info->advance.y = info->glyph->advance.y >> 10; | |
1289 | |
19873 | 1290 if (render_context.stroker) { |
1291 info->outline_glyph = info->glyph; | |
1292 error = FT_Glyph_Stroke( &(info->outline_glyph), render_context.stroker, 0 ); // don't destroy original | |
1293 if (error) { | |
1294 mp_msg(MSGT_GLOBAL, MSGL_WARN, "FT_Glyph_Stroke error %d \n", error); | |
1295 } | |
1296 } else { | |
1297 info->outline_glyph = 0; | |
19002
da05237044c2
Ignoring FT_Glyph_Stroke() errors can potentially lead to double free().
eugeni
parents:
18937
diff
changeset
|
1298 } |
18937 | 1299 |
19965 | 1300 info->bm = info->bm_o = info->bm_s = 0; |
18937 | 1301 |
1302 return 0; | |
1303 } | |
1304 | |
1305 /** | |
19932
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1306 * 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
|
1307 * The following text_info fields are filled: |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1308 * n_lines |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1309 * height |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1310 * lines[].height |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1311 * lines[].asc |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1312 * lines[].desc |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1313 */ |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1314 static void measure_text() |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1315 { |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1316 int cur_line = 0, max_asc = 0, max_desc = 0; |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1317 int i; |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1318 text_info.height = 0; |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1319 for (i = 0; i < text_info.length + 1; ++i) { |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1320 if ((i == text_info.length) || text_info.glyphs[i].linebreak) { |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1321 text_info.lines[cur_line].asc = max_asc; |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1322 text_info.lines[cur_line].desc = max_desc; |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1323 text_info.height += max_asc + max_desc; |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1324 cur_line ++; |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1325 max_asc = max_desc = 0; |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1326 } |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1327 if (i < text_info.length) { |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1328 glyph_info_t* cur = text_info.glyphs + i; |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1329 if (cur->asc > max_asc) |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1330 max_asc = cur->asc * render_context.scale_y; |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1331 if (cur->desc > max_desc) |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1332 max_desc = cur->desc * render_context.scale_y; |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1333 } |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1334 } |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1335 } |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1336 |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1337 /** |
18937 | 1338 * \brief rearrange text between lines |
1339 * \param max_text_width maximal text line width in pixels | |
1340 * The algo is similar to the one in libvo/sub.c: | |
1341 * 1. Place text, wrapping it when current line is full | |
1342 * 2. Try moving words from the end of a line to the beginning of the next one while it reduces | |
1343 * the difference in lengths between this two lines. | |
1344 * The result may not be optimal, but usually is good enough. | |
1345 */ | |
1346 static void wrap_lines_smart(int max_text_width) | |
1347 { | |
1348 int i, j; | |
1349 glyph_info_t *cur, *s1, *e1, *s2, *s3, *w; | |
1350 int last_space; | |
1351 int break_type; | |
1352 int exit; | |
1353 int pen_shift_x; | |
1354 int pen_shift_y; | |
1355 int cur_line; | |
1356 | |
1357 last_space = -1; | |
1358 text_info.n_lines = 1; | |
1359 break_type = 0; | |
1360 s1 = text_info.glyphs; // current line start | |
1361 for (i = 0; i < text_info.length; ++i) { | |
19370 | 1362 int break_at, s_offset, len; |
18937 | 1363 cur = text_info.glyphs + i; |
19370 | 1364 break_at = -1; |
1365 s_offset = s1->bbox.xMin + s1->pos.x; | |
1366 len = (cur->bbox.xMax + cur->pos.x) - s_offset; | |
18937 | 1367 |
1368 if (cur->symbol == '\n') { | |
1369 break_type = 2; | |
1370 break_at = i; | |
1371 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "forced line break at %d\n", break_at); | |
1372 } | |
1373 | |
1374 if (len >= max_text_width) { | |
1375 break_type = 1; | |
1376 break_at = last_space; | |
1377 if (break_at == -1) | |
1378 break_at = i - 1; | |
1379 if (break_at == -1) | |
1380 break_at = 0; | |
1381 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "overfill at %d\n", i); | |
1382 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "line break at %d\n", break_at); | |
1383 } | |
1384 | |
1385 if (break_at != -1) { | |
1386 // need to use one more line | |
1387 // marking break_at+1 as start of a new line | |
1388 int lead = break_at + 1; // the first symbol of the new line | |
1389 if (text_info.n_lines >= MAX_LINES) { | |
1390 // to many lines ! | |
1391 // no more linebreaks | |
1392 for (j = lead; j < text_info.length; ++j) | |
1393 text_info.glyphs[j].linebreak = 0; | |
1394 break; | |
1395 } | |
1396 if (lead < text_info.length) | |
1397 text_info.glyphs[lead].linebreak = break_type; | |
1398 last_space = -1; | |
1399 s1 = text_info.glyphs + lead; | |
1400 s_offset = s1->bbox.xMin + s1->pos.x; | |
1401 text_info.n_lines ++; | |
1402 } | |
1403 | |
1404 if (cur->symbol == ' ') | |
1405 last_space = i; | |
1406 } | |
1407 #define DIFF(x,y) (((x) < (y)) ? (y - x) : (x - y)) | |
1408 exit = 0; | |
1409 while (!exit) { | |
1410 exit = 1; | |
1411 w = s3 = text_info.glyphs; | |
1412 s1 = s2 = 0; | |
1413 for (i = 0; i <= text_info.length; ++i) { | |
1414 cur = text_info.glyphs + i; | |
1415 if ((i == text_info.length) || cur->linebreak) { | |
1416 s1 = s2; | |
1417 s2 = s3; | |
1418 s3 = cur; | |
1419 if (s1 && (s2->linebreak == 1)) { // have at least 2 lines, and linebreak is 'soft' | |
1420 int l1, l2, l1_new, l2_new; | |
1421 | |
1422 w = s2; | |
1423 do { --w; } while ((w > s1) && (w->symbol == ' ')); | |
19663
042680f89e7d
Fix possible unallocated memory read in libass line wrapping code.
eugeni
parents:
19651
diff
changeset
|
1424 while ((w > s1) && (w->symbol != ' ')) { --w; } |
18937 | 1425 e1 = w; |
19663
042680f89e7d
Fix possible unallocated memory read in libass line wrapping code.
eugeni
parents:
19651
diff
changeset
|
1426 while ((e1 > s1) && (e1->symbol == ' ')) { --e1; } |
18937 | 1427 if (w->symbol == ' ') ++w; |
1428 | |
1429 l1 = ((s2-1)->bbox.xMax + (s2-1)->pos.x) - (s1->bbox.xMin + s1->pos.x); | |
1430 l2 = ((s3-1)->bbox.xMax + (s3-1)->pos.x) - (s2->bbox.xMin + s2->pos.x); | |
1431 l1_new = (e1->bbox.xMax + e1->pos.x) - (s1->bbox.xMin + s1->pos.x); | |
1432 l2_new = ((s3-1)->bbox.xMax + (s3-1)->pos.x) - (w->bbox.xMin + w->pos.x); | |
1433 | |
1434 if (DIFF(l1_new, l2_new) < DIFF(l1, l2)) { | |
1435 w->linebreak = 1; | |
1436 s2->linebreak = 0; | |
1437 exit = 0; | |
1438 } | |
1439 } | |
1440 } | |
1441 if (i == text_info.length) | |
1442 break; | |
1443 } | |
1444 | |
1445 } | |
1446 assert(text_info.n_lines >= 1); | |
1447 #undef DIFF | |
1448 | |
19932
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1449 measure_text(); |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1450 |
18937 | 1451 pen_shift_x = 0; |
1452 pen_shift_y = 0; | |
1453 cur_line = 1; | |
1454 for (i = 0; i < text_info.length; ++i) { | |
1455 cur = text_info.glyphs + i; | |
1456 if (cur->linebreak) { | |
1457 int height = text_info.lines[cur_line - 1].desc + text_info.lines[cur_line].asc; | |
1458 cur_line ++; | |
1459 pen_shift_x = - cur->pos.x; | |
1460 pen_shift_y += (height >> 6) + global_settings->line_spacing; | |
1461 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "shifting from %d to %d by (%d, %d)\n", i, text_info.length - 1, pen_shift_x, pen_shift_y); | |
1462 } | |
1463 cur->pos.x += pen_shift_x; | |
1464 cur->pos.y += pen_shift_y; | |
1465 } | |
1466 } | |
1467 | |
1468 /** | |
1469 * \brief determine karaoke effects | |
1470 * Karaoke effects cannot be calculated during parse stage (get_next_char()), | |
1471 * so they are done in a separate step. | |
1472 * Parse stage: when karaoke style override is found, its parameters are stored in the next glyph's | |
1473 * (the first glyph of the karaoke word)'s effect_type and effect_timing. | |
1474 * This function: | |
1475 * 1. sets effect_type for all glyphs in the word (_karaoke_ word) | |
1476 * 2. sets effect_timing for all glyphs to x coordinate of the border line between the left and right karaoke parts | |
1477 * (left part is filled with PrimaryColour, right one - with SecondaryColour). | |
1478 */ | |
1479 static void process_karaoke_effects(void) | |
1480 { | |
1481 glyph_info_t *cur, *cur2; | |
1482 glyph_info_t *s1, *e1; // start and end of the current word | |
1483 glyph_info_t *s2; // start of the next word | |
1484 int i; | |
1485 int timing; // current timing | |
1486 int tm_start, tm_end; // timings at start and end of the current word | |
1487 int tm_current; | |
1488 double dt; | |
1489 int x; | |
1490 int x_start, x_end; | |
1491 | |
1492 tm_current = frame_context.time - render_context.event->Start; | |
1493 timing = 0; | |
1494 s1 = s2 = 0; | |
1495 for (i = 0; i <= text_info.length; ++i) { | |
1496 cur = text_info.glyphs + i; | |
1497 if ((i == text_info.length) || (cur->effect_type != EF_NONE)) { | |
1498 s1 = s2; | |
1499 s2 = cur; | |
1500 if (s1) { | |
1501 e1 = s2 - 1; | |
19716
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
1502 tm_start = timing + s1->effect_skip_timing; |
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
1503 tm_end = tm_start + s1->effect_timing; |
18937 | 1504 timing = tm_end; |
1505 x_start = s1->bbox.xMin + s1->pos.x; | |
1506 x_end = e1->bbox.xMax + e1->pos.x; | |
1507 | |
1508 dt = (tm_current - tm_start); | |
1509 if ((s1->effect_type == EF_KARAOKE) || (s1->effect_type == EF_KARAOKE_KO)) { | |
1510 if (dt > 0) | |
1511 x = x_end + 1; | |
1512 else | |
1513 x = x_start; | |
1514 } else if (s1->effect_type == EF_KARAOKE_KF) { | |
1515 dt /= (tm_end - tm_start); | |
1516 x = x_start + (x_end - x_start) * dt; | |
1517 } else { | |
1518 mp_msg(MSGT_GLOBAL, MSGL_ERR, "Unknown effect type (internal error) \n"); | |
1519 continue; | |
1520 } | |
1521 | |
1522 for (cur2 = s1; cur2 <= e1; ++cur2) { | |
1523 cur2->effect_type = s1->effect_type; | |
1524 cur2->effect_timing = x - cur2->pos.x; | |
1525 } | |
1526 } | |
1527 } | |
1528 } | |
1529 } | |
1530 | |
19066
26a30496ec96
marks several function without a prototype which arent used outside its sourcefile as static, Patch by Stefan Huehner - stefan AT huehner-org
reynaldo
parents:
19044
diff
changeset
|
1531 static int get_face_ascender(FT_Face face) |
18937 | 1532 { |
1533 int v = face->size->metrics.ascender; | |
19693
3fc2235ff062
Workaround for broken fonts with bad ascender/descender.
eugeni
parents:
19692
diff
changeset
|
1534 int v2 = FT_MulFix(face->bbox.yMax, face->size->metrics.y_scale); |
3fc2235ff062
Workaround for broken fonts with bad ascender/descender.
eugeni
parents:
19692
diff
changeset
|
1535 if (v > v2 * 0.9) |
3fc2235ff062
Workaround for broken fonts with bad ascender/descender.
eugeni
parents:
19692
diff
changeset
|
1536 return v; |
3fc2235ff062
Workaround for broken fonts with bad ascender/descender.
eugeni
parents:
19692
diff
changeset
|
1537 else |
3fc2235ff062
Workaround for broken fonts with bad ascender/descender.
eugeni
parents:
19692
diff
changeset
|
1538 return v2; |
18937 | 1539 } |
1540 | |
19066
26a30496ec96
marks several function without a prototype which arent used outside its sourcefile as static, Patch by Stefan Huehner - stefan AT huehner-org
reynaldo
parents:
19044
diff
changeset
|
1541 static int get_face_descender(FT_Face face) |
18937 | 1542 { |
19693
3fc2235ff062
Workaround for broken fonts with bad ascender/descender.
eugeni
parents:
19692
diff
changeset
|
1543 int v = - face->size->metrics.descender; |
3fc2235ff062
Workaround for broken fonts with bad ascender/descender.
eugeni
parents:
19692
diff
changeset
|
1544 int v2 = - FT_MulFix(face->bbox.yMin, face->size->metrics.y_scale); |
3fc2235ff062
Workaround for broken fonts with bad ascender/descender.
eugeni
parents:
19692
diff
changeset
|
1545 if (v > v2 * 0.9) |
3fc2235ff062
Workaround for broken fonts with bad ascender/descender.
eugeni
parents:
19692
diff
changeset
|
1546 return v; |
3fc2235ff062
Workaround for broken fonts with bad ascender/descender.
eugeni
parents:
19692
diff
changeset
|
1547 else |
3fc2235ff062
Workaround for broken fonts with bad ascender/descender.
eugeni
parents:
19692
diff
changeset
|
1548 return v2; |
18937 | 1549 } |
1550 | |
1551 /** | |
1552 * \brief Main ass rendering function, glues everything together | |
1553 * \param event event to render | |
1554 * Process event, appending resulting ass_image_t's to images_root. | |
1555 */ | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1556 static int ass_render_event(ass_event_t* event, event_images_t* event_images) |
18937 | 1557 { |
1558 char* p; | |
1559 FT_UInt glyph_index; | |
1560 FT_Bool use_kerning; | |
1561 FT_UInt previous; | |
1562 FT_UInt num_glyphs; | |
1563 FT_Vector pen; | |
1564 int error; | |
1565 unsigned code; | |
1566 FT_BBox bbox; | |
1567 int i, j; | |
1568 FT_Vector shift; | |
1569 int MarginL, MarginR, MarginV; | |
1570 int last_break; | |
1571 int alignment, halign, valign; | |
19556 | 1572 int device_x = 0, device_y = 0; |
18937 | 1573 |
19651 | 1574 if (event->Style >= frame_context.track->n_styles) { |
19650 | 1575 mp_msg(MSGT_GLOBAL, MSGL_WARN, "No style found!\n"); |
1576 return 1; | |
1577 } | |
1578 if (!event->Text) { | |
1579 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Empty event!\n"); | |
1580 return 1; | |
1581 } | |
1582 | |
18937 | 1583 init_render_context(event); |
1584 | |
1585 text_info.length = 0; | |
1586 pen.x = 0; | |
1587 pen.y = 0; | |
1588 previous = 0; | |
1589 num_glyphs = 0; | |
1590 p = event->Text; | |
1591 // Event parsing. | |
1592 while (1) { | |
1593 // get next char, executing style override | |
1594 // this affects render_context | |
1595 code = get_next_char(&p); | |
1596 | |
1597 // face could have been changed in get_next_char | |
1598 if (!render_context.face) { | |
1599 free_render_context(); | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1600 return 1; |
18937 | 1601 } |
1602 | |
1603 if (code == 0) | |
1604 break; | |
1605 | |
1606 use_kerning = FT_HAS_KERNING(render_context.face); | |
1607 | |
1608 if (text_info.length >= MAX_GLYPHS) { | |
1609 mp_msg(MSGT_GLOBAL, MSGL_WARN, "\nMAX_GLYPHS reached: event %d, start = %llu, duration = %llu\n Text = %s\n", | |
1610 (int)(event - frame_context.track->events), event->Start, event->Duration, event->Text); | |
1611 break; | |
1612 } | |
1613 | |
1614 glyph_index = FT_Get_Char_Index( render_context.face, code); | |
1615 | |
1616 if ( use_kerning && previous && glyph_index ) { | |
1617 FT_Vector delta; | |
1618 FT_Get_Kerning( render_context.face, previous, glyph_index, FT_KERNING_DEFAULT, &delta ); | |
1619 pen.x += delta.x; | |
1620 pen.y += delta.y; | |
1621 } | |
1622 | |
1623 shift.x = pen.x & 63; | |
1624 shift.y = pen.y & 63; | |
1625 | |
1626 if ((render_context.scale_x != 1.) || (render_context.scale_y != 1.) || | |
1627 (frame_context.font_scale_x != 1.)) { | |
1628 FT_Matrix matrix; | |
1629 matrix.xx = (FT_Fixed)( render_context.scale_x * frame_context.font_scale_x * 0x10000L ); | |
1630 matrix.xy = (FT_Fixed)( 0 * 0x10000L ); | |
1631 matrix.yx = (FT_Fixed)( 0 * 0x10000L ); | |
1632 matrix.yy = (FT_Fixed)( render_context.scale_y * 0x10000L ); | |
1633 | |
1634 FT_Set_Transform( render_context.face, &matrix, &shift ); | |
1635 } else { | |
1636 FT_Set_Transform(render_context.face, 0, &shift); | |
1637 } | |
1638 | |
1639 error = get_glyph(glyph_index, code, text_info.glyphs + text_info.length, &shift); | |
1640 | |
1641 if (error) { | |
1642 continue; | |
1643 } | |
1644 | |
1645 text_info.glyphs[text_info.length].pos.x = pen.x >> 6; | |
1646 text_info.glyphs[text_info.length].pos.y = pen.y >> 6; | |
1647 | |
1648 pen.x += text_info.glyphs[text_info.length].advance.x; | |
1649 pen.x += render_context.hspacing; | |
1650 pen.y += text_info.glyphs[text_info.length].advance.y; | |
1651 | |
1652 // if it's an outline glyph, we still need to fill the bbox | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19825
diff
changeset
|
1653 if (text_info.glyphs[text_info.length].glyph) { |
18937 | 1654 FT_Glyph_Get_CBox( text_info.glyphs[text_info.length].glyph, FT_GLYPH_BBOX_PIXELS, &(text_info.glyphs[text_info.length].bbox) ); |
1655 } | |
1656 | |
1657 | |
1658 previous = glyph_index; | |
1659 | |
1660 text_info.glyphs[text_info.length].symbol = code; | |
1661 text_info.glyphs[text_info.length].linebreak = 0; | |
19692
5b40e87b9619
Change \fad behaviour so that it does not get cancelled by \r.
eugeni
parents:
19691
diff
changeset
|
1662 for (i = 0; i < 4; ++i) { |
5b40e87b9619
Change \fad behaviour so that it does not get cancelled by \r.
eugeni
parents:
19691
diff
changeset
|
1663 uint32_t clr = render_context.c[i]; |
5b40e87b9619
Change \fad behaviour so that it does not get cancelled by \r.
eugeni
parents:
19691
diff
changeset
|
1664 change_alpha(&clr, mult_alpha(_a(clr), render_context.fade), 1.); |
5b40e87b9619
Change \fad behaviour so that it does not get cancelled by \r.
eugeni
parents:
19691
diff
changeset
|
1665 text_info.glyphs[text_info.length].c[i] = clr; |
5b40e87b9619
Change \fad behaviour so that it does not get cancelled by \r.
eugeni
parents:
19691
diff
changeset
|
1666 } |
18937 | 1667 text_info.glyphs[text_info.length].effect_type = render_context.effect_type; |
1668 text_info.glyphs[text_info.length].effect_timing = render_context.effect_timing; | |
19716
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
1669 text_info.glyphs[text_info.length].effect_skip_timing = render_context.effect_skip_timing; |
18937 | 1670 text_info.glyphs[text_info.length].asc = get_face_ascender(render_context.face); |
1671 text_info.glyphs[text_info.length].desc = get_face_descender(render_context.face); | |
19848 | 1672 text_info.glyphs[text_info.length].be = render_context.be; |
19965 | 1673 text_info.glyphs[text_info.length].shadow = render_context.shadow; |
18937 | 1674 |
1675 text_info.length++; | |
19716
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
1676 |
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
1677 render_context.effect_type = EF_NONE; |
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
1678 render_context.effect_timing = 0; |
e4e492fcc2f7
Bugfix: timing for empty karaoke words was lost, resulting
eugeni
parents:
19693
diff
changeset
|
1679 render_context.effect_skip_timing = 0; |
18937 | 1680 } |
1681 | |
1682 if (text_info.length == 0) { | |
1683 // no valid symbols in the event; this can be smth like {comment} | |
1684 free_render_context(); | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1685 return 1; |
18937 | 1686 } |
1687 | |
1688 // depends on glyph x coordinates being monotonous, so it should be done before line wrap | |
1689 process_karaoke_effects(); | |
1690 | |
1691 // alignments | |
1692 alignment = render_context.alignment; | |
1693 if (!alignment) | |
1694 alignment = render_context.style->Alignment; | |
1695 halign = alignment & 3; | |
1696 valign = alignment & 12; | |
1697 | |
19649 | 1698 MarginL = (event->MarginL) ? event->MarginL : render_context.style->MarginL; |
1699 MarginR = (event->MarginR) ? event->MarginR : render_context.style->MarginR; | |
1700 MarginV = (event->MarginV) ? event->MarginV : render_context.style->MarginV; | |
19556 | 1701 |
1702 if (render_context.evt_type != EVENT_HSCROLL) { | |
19931
399bb1fcdc94
Move variable declaration to a more deeply nested block. It is not used outside of it.
eugeni
parents:
19919
diff
changeset
|
1703 int max_text_width; |
399bb1fcdc94
Move variable declaration to a more deeply nested block. It is not used outside of it.
eugeni
parents:
19919
diff
changeset
|
1704 |
19557 | 1705 // calculate max length of a line |
1706 max_text_width = x2scr(frame_context.track->PlayResX - MarginR) - x2scr(MarginL); | |
19556 | 1707 |
19557 | 1708 // rearrange text in several lines |
1709 wrap_lines_smart(max_text_width); | |
19556 | 1710 |
19557 | 1711 // align text |
1712 last_break = -1; | |
1713 for (i = 1; i < text_info.length + 1; ++i) { // (text_info.length + 1) is the end of the last line | |
1714 if ((i == text_info.length) || text_info.glyphs[i].linebreak) { | |
1715 int width, shift; | |
1716 glyph_info_t* first_glyph = text_info.glyphs + last_break + 1; | |
1717 glyph_info_t* last_glyph = text_info.glyphs + i - 1; | |
18937 | 1718 |
19557 | 1719 while ((last_glyph >= first_glyph) && ((last_glyph->symbol == '\n') || (last_glyph->symbol == 0))) |
1720 last_glyph --; | |
18937 | 1721 |
19557 | 1722 width = last_glyph->pos.x + last_glyph->bbox.xMax - first_glyph->pos.x - first_glyph->bbox.xMin; |
1723 shift = - first_glyph->bbox.xMin; // now text line starts exactly at 0 (left margin) | |
1724 if (halign == HALIGN_LEFT) { // left aligned, no action | |
1725 } else if (halign == HALIGN_RIGHT) { // right aligned | |
1726 shift = max_text_width - width; | |
1727 } else if (halign == HALIGN_CENTER) { // centered | |
1728 shift = (max_text_width - width) / 2; | |
1729 } | |
1730 for (j = last_break + 1; j < i; ++j) { | |
1731 text_info.glyphs[j].pos.x += shift; | |
1732 } | |
1733 last_break = i - 1; | |
18937 | 1734 } |
1735 } | |
19932
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1736 } else { // render_context.evt_type == EVENT_HSCROLL |
0b5b9cbbc74e
Move calculation of text parameters (number of lines, height, etc.) from
eugeni
parents:
19931
diff
changeset
|
1737 measure_text(); |
18937 | 1738 } |
1739 | |
1740 // determing text bounding box | |
1741 compute_string_bbox(&text_info, &bbox); | |
19904
640093581779
Calculate text bounding box in such way that it does not depend on the height
eugeni
parents:
19891
diff
changeset
|
1742 bbox.yMin = - (text_info.lines[0].asc >> 6); |
640093581779
Calculate text bounding box in such way that it does not depend on the height
eugeni
parents:
19891
diff
changeset
|
1743 bbox.yMax = (text_info.height - text_info.lines[0].asc) >> 6; |
18937 | 1744 |
1745 // determine device coordinates for text | |
19556 | 1746 |
1747 // x coordinate for everything except positioned events | |
1748 if (render_context.evt_type == EVENT_NORMAL || | |
1749 render_context.evt_type == EVENT_VSCROLL) { | |
1750 device_x = x2scr(MarginL); | |
1751 } else if (render_context.evt_type == EVENT_HSCROLL) { | |
1752 if (render_context.scroll_direction == SCROLL_RL) | |
1753 device_x = x2scr(frame_context.track->PlayResX - render_context.scroll_shift); | |
1754 else if (render_context.scroll_direction == SCROLL_LR) | |
1755 device_x = x2scr(render_context.scroll_shift) - (bbox.xMax - bbox.xMin); | |
1756 } | |
18937 | 1757 |
19556 | 1758 // y coordinate for everything except positioned events |
1759 if (render_context.evt_type == EVENT_NORMAL || | |
1760 render_context.evt_type == EVENT_HSCROLL) { | |
18937 | 1761 if (valign == VALIGN_TOP) { // toptitle |
1762 device_y = y2scr_top(MarginV) + (text_info.lines[0].asc >> 6); | |
1763 } else if (valign == VALIGN_CENTER) { // midtitle | |
1764 int scr_y = y2scr(frame_context.track->PlayResY / 2); | |
1765 device_y = scr_y - (bbox.yMax - bbox.yMin) / 2; | |
1766 } else { // subtitle | |
1767 int scr_y; | |
1768 if (valign != VALIGN_SUB) | |
1769 mp_msg(MSGT_GLOBAL, MSGL_V, "Invalid valign, supposing 0 (subtitle)\n"); | |
1770 scr_y = y2scr_sub(frame_context.track->PlayResY - MarginV); | |
1771 device_y = scr_y; | |
1772 device_y -= (text_info.height >> 6); | |
1773 device_y += (text_info.lines[0].asc >> 6); | |
1774 } | |
19556 | 1775 } else if (render_context.evt_type == EVENT_VSCROLL) { |
1776 if (render_context.scroll_direction == SCROLL_TB) | |
1777 device_y = y2scr(render_context.clip_y0 + render_context.scroll_shift) - (bbox.yMax - bbox.yMin); | |
1778 else if (render_context.scroll_direction == SCROLL_BT) | |
1779 device_y = y2scr(render_context.clip_y1 - render_context.scroll_shift); | |
1780 } | |
1781 | |
1782 // positioned events are totally different | |
1783 if (render_context.evt_type == EVENT_POSITIONED) { | |
18937 | 1784 int align_shift_x = 0; |
1785 int align_shift_y = 0; | |
1786 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "positioned event at %d, %d\n", render_context.pos_x, render_context.pos_y); | |
1787 switch(halign) { | |
1788 case HALIGN_LEFT: | |
1789 align_shift_x = - bbox.xMin; | |
1790 break; | |
1791 case HALIGN_CENTER: | |
1792 align_shift_x = - (bbox.xMax + bbox.xMin) /2; | |
1793 break; | |
1794 case HALIGN_RIGHT: | |
1795 align_shift_x = - bbox.xMax; | |
1796 break; | |
1797 } | |
1798 switch(valign) { | |
1799 case VALIGN_TOP: | |
1800 align_shift_y = - bbox.yMin; | |
1801 break; | |
1802 case VALIGN_CENTER: | |
1803 align_shift_y = - (bbox.yMax + bbox.yMin) /2; | |
1804 break; | |
1805 case VALIGN_SUB: | |
1806 align_shift_y = - bbox.yMax; | |
1807 break; | |
1808 } | |
1809 device_x = x2scr(render_context.pos_x) + align_shift_x; | |
1810 device_y = y2scr(render_context.pos_y) + align_shift_y; | |
1811 } | |
1812 | |
1813 // fix clip coordinates (they depend on alignment) | |
1814 render_context.clip_x0 = x2scr(render_context.clip_x0); | |
1815 render_context.clip_x1 = x2scr(render_context.clip_x1); | |
19556 | 1816 if (render_context.evt_type == EVENT_NORMAL || |
1817 render_context.evt_type == EVENT_HSCROLL || | |
1818 render_context.evt_type == EVENT_VSCROLL) { | |
18937 | 1819 if (valign == VALIGN_TOP) { |
1820 render_context.clip_y0 = y2scr_top(render_context.clip_y0); | |
1821 render_context.clip_y1 = y2scr_top(render_context.clip_y1); | |
1822 } else if (valign == VALIGN_CENTER) { | |
1823 render_context.clip_y0 = y2scr(render_context.clip_y0); | |
1824 render_context.clip_y1 = y2scr(render_context.clip_y1); | |
1825 } else if (valign == VALIGN_SUB) { | |
1826 render_context.clip_y0 = y2scr_sub(render_context.clip_y0); | |
1827 render_context.clip_y1 = y2scr_sub(render_context.clip_y1); | |
1828 } | |
1829 } else if (render_context.evt_type == EVENT_POSITIONED) { | |
1830 render_context.clip_y0 = y2scr(render_context.clip_y0); | |
1831 render_context.clip_y1 = y2scr(render_context.clip_y1); | |
1832 } | |
1833 | |
1834 // rotate glyphs if needed | |
1835 if (render_context.rotation != 0.) { | |
1836 double angle = render_context.rotation; | |
1837 FT_Vector center; | |
1838 FT_Matrix matrix_rotate; | |
1839 | |
1840 matrix_rotate.xx = (FT_Fixed)( cos( angle ) * 0x10000L ); | |
1841 matrix_rotate.xy = (FT_Fixed)( -sin( angle ) * 0x10000L ); | |
1842 matrix_rotate.yx = (FT_Fixed)( sin( angle ) * 0x10000L ); | |
1843 matrix_rotate.yy = (FT_Fixed)( cos( angle ) * 0x10000L ); | |
1844 | |
1845 if (((render_context.org_x != 0) || (render_context.org_y != 0)) && (render_context.evt_type == EVENT_POSITIONED)) { | |
1846 center.x = render_context.org_x; | |
1847 center.y = render_context.org_y; | |
1848 } else { | |
1849 FT_BBox str_bbox; | |
1850 | |
1851 center.x = text_info.glyphs[0].pos.x + device_x; | |
1852 center.y = text_info.glyphs[0].pos.y + device_y; | |
1853 | |
1854 compute_string_bbox(&text_info, &str_bbox); | |
1855 center.x += (str_bbox.xMax - str_bbox.xMin) / 2; | |
1856 center.y += (str_bbox.yMax - str_bbox.yMin) / 2; | |
1857 } | |
1858 // mp_msg(MSGT_GLOBAL, MSGL_DBG2, "\ncenter: %d, %d\n", center.x, center.y); | |
1859 | |
1860 for (i = 0; i < text_info.length; ++i) { | |
1861 glyph_info_t* info = text_info.glyphs + i; | |
1862 | |
1863 // calculating shift vector | |
1864 // shift = (position - center)*M - (position - center) | |
1865 FT_Vector start; | |
1866 FT_Vector start_old; | |
1867 // mp_msg(MSGT_GLOBAL, MSGL_INFO, "start: (%d, %d) + (%d, %d) - (%d, %d) = (%d, %d)\n", info->pos.x, info->pos.y, device_x, device_y, center.x, center.y, | |
1868 // info->pos.x + device_x - center.x, info->pos.y + device_y - center.y); | |
1869 start.x = (info->pos.x + device_x - center.x) << 6; | |
1870 start.y = - (info->pos.y + device_y - center.y) << 6; | |
1871 start_old.x = start.x; | |
1872 start_old.y = start.y; | |
1873 // mp_msg(MSGT_GLOBAL, MSGL_INFO, "start: %d, %d\n", start.x / 64, start.y / 64); | |
1874 | |
1875 FT_Vector_Transform(&start, &matrix_rotate); | |
1876 | |
1877 start.x -= start_old.x; | |
1878 start.y -= start_old.y; | |
1879 | |
1880 info->pos.x += start.x >> 6; | |
1881 info->pos.y -= start.y >> 6; | |
1882 | |
1883 // mp_msg(MSGT_GLOBAL, MSGL_DBG2, "shift: %d, %d\n", start.x / 64, start.y / 64); | |
19873 | 1884 if (info->glyph) |
18937 | 1885 FT_Glyph_Transform( info->glyph, &matrix_rotate, 0 ); |
19873 | 1886 if (info->outline_glyph) |
18937 | 1887 FT_Glyph_Transform( info->outline_glyph, &matrix_rotate, 0 ); |
1888 } | |
1889 } | |
1890 | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1891 event_images->top = device_y - (text_info.lines[0].asc >> 6); |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1892 event_images->height = text_info.height >> 6; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1893 event_images->detect_collisions = render_context.detect_collisions; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1894 event_images->shift_direction = (valign == VALIGN_TOP) ? 1 : -1; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1895 event_images->event = event; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1896 event_images->imgs = render_text(&text_info, device_x, device_y); |
18937 | 1897 |
1898 free_render_context(); | |
1899 | |
1900 return 0; | |
1901 } | |
1902 | |
1903 void ass_configure(ass_instance_t* priv, const ass_settings_t* config) | |
1904 { | |
19539 | 1905 if (memcmp(&priv->settings, config, sizeof(ass_settings_t)) != 0) { |
1906 mp_msg(MSGT_GLOBAL, MSGL_V, "ass_configure: %d x %d; margins: l: %d, r: %d, t: %d, b: %d \n", | |
1907 config->frame_width, config->frame_height, | |
1908 config->left_margin, config->right_margin, config->top_margin, config->bottom_margin); | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1909 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1910 priv->render_id = ++last_render_id; |
19539 | 1911 memcpy(&priv->settings, config, sizeof(ass_settings_t)); |
1912 ass_glyph_cache_reset(); | |
1913 } | |
18937 | 1914 } |
1915 | |
1916 /** | |
1917 * \brief Start a new frame | |
1918 */ | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1919 static int ass_start_frame(ass_instance_t *priv, ass_track_t* track, long long now) |
18937 | 1920 { |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1921 ass_image_t* img; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1922 |
18937 | 1923 ass_instance = priv; |
1924 global_settings = &priv->settings; | |
1925 | |
1926 if (!priv->settings.frame_width && !priv->settings.frame_height) | |
1927 return 1; // library not initialized | |
1928 | |
1929 frame_context.ass_priv = priv; | |
1930 frame_context.width = global_settings->frame_width; | |
1931 frame_context.height = global_settings->frame_height; | |
19538 | 1932 frame_context.orig_width = global_settings->frame_width - global_settings->left_margin - global_settings->right_margin; |
18937 | 1933 frame_context.orig_height = global_settings->frame_height - global_settings->top_margin - global_settings->bottom_margin; |
1934 frame_context.track = track; | |
1935 frame_context.time = now; | |
1936 | |
1937 ass_lazy_track_init(); | |
1938 | |
19825
f351a3fc3e42
Make font outline width proportional to movie resolution.
eugeni
parents:
19716
diff
changeset
|
1939 frame_context.font_scale = global_settings->font_size_coeff * ass_internal_font_size_coeff * |
f351a3fc3e42
Make font outline width proportional to movie resolution.
eugeni
parents:
19716
diff
changeset
|
1940 frame_context.orig_height / frame_context.track->PlayResY; |
19917
6a31849e2b80
In r19831, font size multiplier was mistakenly applied to border width.
eugeni
parents:
19906
diff
changeset
|
1941 frame_context.border_scale = ((double)frame_context.orig_height) / frame_context.track->PlayResY; |
19825
f351a3fc3e42
Make font outline width proportional to movie resolution.
eugeni
parents:
19716
diff
changeset
|
1942 |
18937 | 1943 if (frame_context.width * track->PlayResY == frame_context.height * track->PlayResX) |
1944 frame_context.font_scale_x = 1.; | |
1945 else | |
19566 | 1946 frame_context.font_scale_x = ((double)(frame_context.orig_width * track->PlayResY)) / (frame_context.orig_height * track->PlayResX); |
18937 | 1947 |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1948 img = priv->images_root; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1949 while (img) { |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1950 ass_image_t* next = img->next; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1951 free(img); |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1952 img = next; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1953 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1954 priv->images_root = 0; |
18937 | 1955 |
1956 return 0; | |
1957 } | |
1958 | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1959 static int cmp_event_layer(const void* p1, const void* p2) |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1960 { |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1961 ass_event_t* e1 = ((event_images_t*)p1)->event; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1962 ass_event_t* e2 = ((event_images_t*)p2)->event; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1963 if (e1->Layer < e2->Layer) |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1964 return -1; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1965 if (e1->Layer > e2->Layer) |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1966 return 1; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1967 if (e1->Start < e2->Start) |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1968 return -1; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1969 if (e1->Start > e2->Start) |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1970 return 1; |
19906
f72c694ee566
Sort events by ReadOrder (if both layer and start time are equal).
eugeni
parents:
19904
diff
changeset
|
1971 if (e1->ReadOrder < e2->ReadOrder) |
f72c694ee566
Sort events by ReadOrder (if both layer and start time are equal).
eugeni
parents:
19904
diff
changeset
|
1972 return -1; |
f72c694ee566
Sort events by ReadOrder (if both layer and start time are equal).
eugeni
parents:
19904
diff
changeset
|
1973 if (e1->ReadOrder > e2->ReadOrder) |
f72c694ee566
Sort events by ReadOrder (if both layer and start time are equal).
eugeni
parents:
19904
diff
changeset
|
1974 return 1; |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1975 return 0; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1976 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1977 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1978 #define MAX_EVENTS 100 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1979 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1980 static render_priv_t* get_render_priv(ass_event_t* event) |
18937 | 1981 { |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1982 if (!event->render_priv) |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1983 event->render_priv = calloc(1, sizeof(render_priv_t)); |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1984 // FIXME: check render_id |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1985 if (ass_instance->render_id != event->render_priv->render_id) { |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1986 memset(event->render_priv, 0, sizeof(render_priv_t)); |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1987 event->render_priv->render_id = ass_instance->render_id; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1988 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1989 return event->render_priv; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1990 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1991 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1992 typedef struct segment_s { |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1993 int a, b; // top and height |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1994 } segment_t; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1995 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1996 static int overlap(segment_t* s1, segment_t* s2) |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1997 { |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1998 if (s1->a >= s2->b || s2->a >= s1->b) |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
1999 return 0; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2000 return 1; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2001 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2002 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2003 static int cmp_segment(const void* p1, const void* p2) |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2004 { |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2005 return ((segment_t*)p1)->a - ((segment_t*)p1)->b; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2006 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2007 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2008 static void shift_event(event_images_t* ei, int shift) |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2009 { |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2010 ass_image_t* cur = ei->imgs; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2011 while (cur) { |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2012 cur->dst_y += shift; |
19664
f6badf921e82
Add vertical clipping for subtitles that were moved because of a collision.
eugeni
parents:
19663
diff
changeset
|
2013 // clip top and bottom |
f6badf921e82
Add vertical clipping for subtitles that were moved because of a collision.
eugeni
parents:
19663
diff
changeset
|
2014 if (cur->dst_y < 0) { |
f6badf921e82
Add vertical clipping for subtitles that were moved because of a collision.
eugeni
parents:
19663
diff
changeset
|
2015 int clip = - cur->dst_y; |
f6badf921e82
Add vertical clipping for subtitles that were moved because of a collision.
eugeni
parents:
19663
diff
changeset
|
2016 cur->h -= clip; |
f6badf921e82
Add vertical clipping for subtitles that were moved because of a collision.
eugeni
parents:
19663
diff
changeset
|
2017 cur->bitmap += clip * cur->stride; |
f6badf921e82
Add vertical clipping for subtitles that were moved because of a collision.
eugeni
parents:
19663
diff
changeset
|
2018 cur->dst_y = 0; |
f6badf921e82
Add vertical clipping for subtitles that were moved because of a collision.
eugeni
parents:
19663
diff
changeset
|
2019 } |
f6badf921e82
Add vertical clipping for subtitles that were moved because of a collision.
eugeni
parents:
19663
diff
changeset
|
2020 if (cur->dst_y + cur->h >= frame_context.height) { |
f6badf921e82
Add vertical clipping for subtitles that were moved because of a collision.
eugeni
parents:
19663
diff
changeset
|
2021 int clip = cur->dst_y + cur->h - frame_context.height; |
f6badf921e82
Add vertical clipping for subtitles that were moved because of a collision.
eugeni
parents:
19663
diff
changeset
|
2022 cur->h -= clip; |
f6badf921e82
Add vertical clipping for subtitles that were moved because of a collision.
eugeni
parents:
19663
diff
changeset
|
2023 } |
f6badf921e82
Add vertical clipping for subtitles that were moved because of a collision.
eugeni
parents:
19663
diff
changeset
|
2024 if (cur->h <= 0) { |
f6badf921e82
Add vertical clipping for subtitles that were moved because of a collision.
eugeni
parents:
19663
diff
changeset
|
2025 cur->h = 0; |
f6badf921e82
Add vertical clipping for subtitles that were moved because of a collision.
eugeni
parents:
19663
diff
changeset
|
2026 cur->dst_y = 0; |
f6badf921e82
Add vertical clipping for subtitles that were moved because of a collision.
eugeni
parents:
19663
diff
changeset
|
2027 } |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2028 cur = cur->next; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2029 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2030 ei->top += shift; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2031 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2032 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2033 // dir: 1 - move down |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2034 // -1 - move up |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2035 static int fit_segment(segment_t* s, segment_t* fixed, int* cnt, int dir) |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2036 { |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2037 int i; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2038 int shift; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2039 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2040 if (*cnt == 0) { |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2041 *cnt = 1; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2042 fixed[0].a = s->a; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2043 fixed[0].b = s->b; |
18937 | 2044 return 0; |
2045 } | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2046 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2047 if (dir == 1) { // move down |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2048 if (s->b <= fixed[0].a) // all ok |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2049 return 0; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2050 for (i = 0; i < *cnt; ++i) { |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2051 shift = fixed[i].b - s->a; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2052 if (i == *cnt - 1 || fixed[i+1].a >= shift + s->b) { // here is a good place |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2053 fixed[i].b += s->b - s->a; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2054 return shift; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2055 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2056 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2057 } else { // dir == -1, move up |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2058 if (s->a >= fixed[*cnt-1].b) // all ok |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2059 return 0; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2060 for (i = *cnt-1; i >= 0; --i) { |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2061 shift = fixed[i].a - s->b; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2062 if (i == 0 || fixed[i-1].b <= shift + s->a) { // here is a good place |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2063 fixed[i].a -= s->b - s->a; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2064 return shift; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2065 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2066 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2067 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2068 assert(0); // unreachable |
18937 | 2069 } |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2070 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2071 static void fix_collisions(event_images_t* imgs, int cnt) |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2072 { |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2073 segment_t used[MAX_EVENTS]; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2074 int cnt_used = 0; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2075 int i, j; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2076 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2077 // fill used[] with fixed events |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2078 for (i = 0; i < cnt; ++i) { |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2079 render_priv_t* priv; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2080 if (!imgs[i].detect_collisions) break; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2081 priv = get_render_priv(imgs[i].event); |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2082 if (priv->height > 0) { // it's a fixed event |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2083 segment_t s; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2084 s.a = priv->top; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2085 s.b = priv->top + priv->height; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2086 if (priv->height != imgs[i].height) { // no, it's not |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2087 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Achtung! Event height has changed! \n"); |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2088 priv->top = 0; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2089 priv->height = 0; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2090 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2091 for (j = 0; j < cnt_used; ++j) |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2092 if (overlap(&s, used + j)) { // no, it's not |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2093 priv->top = 0; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2094 priv->height = 0; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2095 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2096 if (priv->height > 0) { // still a fixed event |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2097 used[cnt_used].a = priv->top; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2098 used[cnt_used].b = priv->top + priv->height; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2099 cnt_used ++; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2100 shift_event(imgs + i, priv->top - imgs[i].top); |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2101 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2102 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2103 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2104 qsort(used, cnt_used, sizeof(segment_t), cmp_segment); |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2105 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2106 // try to fit other events in free spaces |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2107 for (i = 0; i < cnt; ++i) { |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2108 render_priv_t* priv; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2109 if (!imgs[i].detect_collisions) break; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2110 priv = get_render_priv(imgs[i].event); |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2111 if (priv->height == 0) { // not a fixed event |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2112 int shift; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2113 segment_t s; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2114 s.a = imgs[i].top; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2115 s.b = imgs[i].top + imgs[i].height; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2116 shift = fit_segment(&s, used, &cnt_used, imgs[i].shift_direction); |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2117 if (shift) shift_event(imgs + i, shift); |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2118 // make it fixed |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2119 priv->top = imgs[i].top; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2120 priv->height = imgs[i].height; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2121 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2122 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2123 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2124 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2125 |
18937 | 2126 /** |
2127 * \brief render a frame | |
2128 * \param priv library handle | |
2129 * \param track track | |
2130 * \param now current video timestamp (ms) | |
2131 */ | |
2132 ass_image_t* ass_render_frame(ass_instance_t *priv, ass_track_t* track, long long now) | |
2133 { | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2134 int i, cnt, rc; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2135 event_images_t eimg[MAX_EVENTS]; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2136 event_images_t* last; |
19891 | 2137 ass_image_t** tail; |
18937 | 2138 |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2139 // init frame |
18937 | 2140 rc = ass_start_frame(priv, track, now); |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2141 if (rc != 0) |
18937 | 2142 return 0; |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2143 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2144 // render events separately |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2145 cnt = 0; |
18937 | 2146 for (i = 0; i < track->n_events; ++i) { |
2147 ass_event_t* event = track->events + i; | |
2148 if ( (event->Start <= now) && (now < (event->Start + event->Duration)) ) { | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2149 if (cnt < MAX_EVENTS) { |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2150 rc = ass_render_event(event, eimg + cnt); |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2151 if (!rc) ++cnt; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2152 } else { |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2153 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Too many simultaneous events \n"); |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2154 break; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2155 } |
18937 | 2156 } |
2157 } | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2158 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2159 // sort by layer |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2160 qsort(eimg, cnt, sizeof(event_images_t), cmp_event_layer); |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2161 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2162 // call fix_collisions for each group of events with the same layer |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2163 last = eimg; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2164 for (i = 1; i < cnt; ++i) |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2165 if (last->event->Layer != eimg[i].event->Layer) { |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2166 fix_collisions(last, eimg + i - last); |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2167 last = eimg + i; |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2168 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2169 if (cnt > 0) |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2170 fix_collisions(last, eimg + cnt - last); |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2171 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2172 // concat lists |
19891 | 2173 tail = &ass_instance->images_root; |
2174 for (i = 0; i < cnt; ++i) { | |
2175 ass_image_t* cur = eimg[i].imgs; | |
2176 while (cur) { | |
2177 *tail = cur; | |
2178 tail = &cur->next; | |
2179 cur = cur->next; | |
2180 } | |
19638
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2181 } |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2182 |
a3473d990fed
Better collision detection algorithm. The idea is to keep a subtitle in place
eugeni
parents:
19636
diff
changeset
|
2183 return ass_instance->images_root; |
18937 | 2184 } |
2185 |