Mercurial > mplayer.hg
annotate libass/ass_render.c @ 19484:6eb79b2384f8
use of malloc without prototype.. very bad on 64bit archs
author | rfelker |
---|---|
date | Mon, 21 Aug 2006 22:02:49 +0000 |
parents | 747a5c394a69 |
children | 2f5e66e7bdcc |
rev | line source |
---|---|
18937 | 1 #include "config.h" |
2 | |
3 #include <assert.h> | |
4 #include <math.h> | |
19378 | 5 #include <inttypes.h> |
18937 | 6 #include <ft2build.h> |
7 #include FT_FREETYPE_H | |
8 #include FT_STROKER_H | |
9 #include FT_GLYPH_H | |
10 #include FT_SYNTHESIS_H | |
11 | |
12 #include "mp_msg.h" | |
13 | |
14 #include "ass.h" | |
15 #include "ass_cache.h" | |
16 #include "ass_utils.h" | |
17 #include "ass_fontconfig.h" | |
18 | |
19 #include "libvo/sub.h" // for utf8_get_char | |
20 | |
21 #define MAX_GLYPHS 1000 | |
22 #define MAX_LINES 100 | |
23 | |
24 char *get_path(char *); | |
25 | |
26 extern char *font_name; | |
27 #ifdef HAVE_FONTCONFIG | |
28 extern int font_fontconfig; | |
29 #else | |
30 static int font_fontconfig = 0; | |
31 #endif | |
32 | |
33 struct ass_instance_s { | |
34 FT_Library library; | |
35 fc_instance_t* fontconfig_priv; | |
36 ass_settings_t settings; | |
37 | |
38 ass_image_t* images_root; // rendering result is stored here | |
39 int n_images; | |
40 int max_images; | |
41 }; | |
42 | |
43 int no_more_font_messages = 0; // don't print font warnings | |
44 | |
45 typedef enum {EF_NONE = 0, EF_KARAOKE, EF_KARAOKE_KF, EF_KARAOKE_KO} effect_t; | |
46 | |
47 // describes a glyph | |
48 // glyph_info_t and text_info_t are used for text centering and word-wrapping operations | |
49 typedef struct glyph_info_s { | |
50 unsigned symbol; | |
51 FT_Glyph glyph; | |
52 FT_Glyph outline_glyph; | |
53 FT_BBox bbox; | |
54 FT_Vector pos; | |
55 char linebreak; // the first (leading) glyph of some line ? | |
56 uint32_t c1, c2, c3, c4; // colors | |
57 char bitmap; // bool | |
58 FT_Vector advance; // 26.6 | |
59 effect_t effect_type; | |
60 int effect_timing; | |
61 int asc, desc; // font max ascender and descender | |
62 // int height; | |
63 | |
64 glyph_hash_key_t hash_key; | |
65 } glyph_info_t; | |
66 | |
67 typedef struct line_info_s { | |
68 int asc, desc; | |
69 } line_info_t; | |
70 | |
71 typedef struct text_info_s { | |
72 glyph_info_t* glyphs; | |
73 int length; | |
74 line_info_t lines[MAX_LINES]; | |
75 int n_lines; | |
76 int height; | |
77 } text_info_t; | |
78 | |
79 | |
80 // Renderer state. | |
81 // Values like current font face, color, screen position, clipping and so on are stored here. | |
82 typedef struct render_context_s { | |
83 ass_event_t* event; | |
84 ass_style_t* style; | |
85 | |
86 FT_Face face; | |
87 char* font_path; | |
88 int font_size; | |
89 | |
90 FT_Stroker stroker; | |
91 int alignment; // alignment overrides go here; if zero, style value will be used | |
92 double rotation; | |
93 enum { EVENT_NORMAL, // "normal" top-, sub- or mid- title | |
94 EVENT_POSITIONED // happens after pos(,), margins are ignored | |
95 } evt_type; | |
96 int pos_x, pos_y; // position | |
97 int org_x, org_y; // origin | |
98 double scale_x, scale_y; | |
99 int hspacing; // distance between letters, in pixels | |
100 double border; // outline width | |
101 uint32_t c1, c2, c3, c4; // colors(Primary, Secondary, so on) in RGBA | |
102 int clip_x0, clip_y0, clip_x1, clip_y1; | |
103 char detect_collisions; | |
104 | |
105 effect_t effect_type; | |
106 int effect_timing; | |
107 | |
108 // face properties | |
109 char* family; | |
110 unsigned bold; | |
111 unsigned italic; | |
112 | |
113 } render_context_t; | |
114 | |
115 // frame-global data | |
116 typedef struct frame_context_s { | |
117 ass_instance_t* ass_priv; | |
118 int width, height; // screen dimensions | |
119 int orig_height; // frame height ( = screen height - margins ) | |
120 ass_track_t* track; | |
121 int add_bottom_margin; // additional margin, used to shift subtitles in case of collision | |
122 int add_top_margin; | |
123 long long time; // frame's timestamp, ms | |
124 double font_scale_x; // x scale applied to all glyphs to preserve text aspect ratio | |
125 } frame_context_t; | |
126 | |
127 static ass_instance_t* ass_instance; | |
128 static ass_settings_t* global_settings; | |
129 static text_info_t text_info; | |
130 static render_context_t render_context; | |
131 static frame_context_t frame_context; | |
132 | |
133 static void ass_lazy_track_init(void) | |
134 { | |
135 ass_track_t* track = frame_context.track; | |
136 if (track->PlayResX && track->PlayResY) | |
137 return; | |
138 if (!track->PlayResX && !track->PlayResY) { | |
139 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Neither PlayResX nor PlayResY defined. Assuming 384x288. \n"); | |
140 track->PlayResX = 384; | |
141 track->PlayResY = 288; | |
142 } else { | |
143 double orig_aspect = (global_settings->aspect * frame_context.height) / frame_context.orig_height; | |
144 if (!track->PlayResY) { | |
145 track->PlayResY = track->PlayResX / orig_aspect + .5; | |
146 mp_msg(MSGT_GLOBAL, MSGL_WARN, "PlayResY undefined, setting %d \n", track->PlayResY); | |
147 } else if (!track->PlayResX) { | |
148 track->PlayResX = track->PlayResY * orig_aspect + .5; | |
149 mp_msg(MSGT_GLOBAL, MSGL_WARN, "PlayResX undefined, setting %d \n", track->PlayResX); | |
150 } | |
151 } | |
152 } | |
153 | |
154 ass_instance_t* ass_init(void) | |
155 { | |
156 char* family = 0; | |
157 char* path = 0; | |
158 char* fonts_path = 0; | |
159 int error; | |
160 fc_instance_t* fc_priv; | |
161 FT_Library ft; | |
162 ass_instance_t* priv; | |
163 | |
164 if (font_fontconfig && font_name) | |
165 family = strdup(font_name); | |
166 | |
167 if (!font_fontconfig && font_name) | |
168 path = strdup(font_name); | |
169 else | |
170 path = get_path("subfont.ttf"); | |
171 | |
172 fonts_path = get_path("fonts"); | |
173 | |
174 fc_priv = fontconfig_init(fonts_path, family, path); | |
175 | |
176 free(fonts_path); | |
177 if (path) free(path); | |
178 if (family) free(family); | |
179 | |
180 if (!fc_priv) | |
181 return 0; | |
182 | |
183 error = FT_Init_FreeType( &ft ); | |
184 if ( error ) { | |
185 mp_msg(MSGT_GLOBAL, MSGL_FATAL, "FT_Init_FreeType failed\n"); | |
186 fontconfig_done(fc_priv); | |
187 return 0; | |
188 } | |
189 | |
190 priv = calloc(1, sizeof(ass_instance_t)); | |
191 if (!priv) { | |
192 FT_Done_FreeType(ft); | |
193 fontconfig_done(fc_priv); | |
194 return 0; | |
195 } | |
196 priv->library = ft; | |
197 priv->fontconfig_priv = fc_priv; | |
198 // images_root and related stuff is zero-filled in calloc | |
199 | |
200 ass_face_cache_init(); | |
201 ass_glyph_cache_init(); | |
202 | |
203 text_info.glyphs = calloc(MAX_GLYPHS, sizeof(glyph_info_t)); | |
204 | |
205 return priv; | |
206 } | |
207 | |
208 void ass_done(ass_instance_t* priv) | |
209 { | |
210 ass_face_cache_done(); | |
211 ass_glyph_cache_done(); | |
212 if (priv && priv->library) FT_Done_FreeType(priv->library); | |
213 if (priv && priv->fontconfig_priv) fontconfig_done(priv->fontconfig_priv); | |
214 if (priv) free(priv); | |
215 if (text_info.glyphs) free(text_info.glyphs); | |
216 } | |
217 | |
218 /** | |
219 * \brief Create a new ass_image_t and append it to images_root | |
220 * Parameters are the same as ass_image_t fields. | |
221 */ | |
222 static void my_draw_bitmap(unsigned char* bitmap, int bitmap_w, int bitmap_h, int stride, int dst_x, int dst_y, uint32_t color) | |
223 { | |
224 ass_instance_t* priv = ass_instance; | |
225 ass_image_t* img; | |
226 | |
227 assert(priv->n_images <= priv->max_images); | |
228 if (priv->n_images == priv->max_images) { | |
229 if (!priv->max_images) priv->max_images = 100; | |
230 else priv->max_images *= 2; | |
231 priv->images_root = (ass_image_t*)realloc(priv->images_root, priv->max_images * sizeof(ass_image_t)); | |
232 } | |
233 assert(priv->images_root); | |
234 img = priv->images_root + priv->n_images; | |
235 | |
236 img->w = bitmap_w; | |
237 img->h = bitmap_h; | |
238 img->stride = stride; | |
239 img->bitmap = bitmap; | |
240 img->color = color; | |
241 img->dst_x = dst_x; | |
242 img->dst_y = dst_y; | |
243 | |
244 priv->n_images++; | |
245 } | |
246 | |
247 /** | |
248 * \brief convert bitmap glyph into ass_image_t struct(s) | |
249 * \param bit freetype bitmap glyph, FT_PIXEL_MODE_GRAY | |
250 * \param dst_x bitmap x coordinate in video frame | |
251 * \param dst_y bitmap y coordinate in video frame | |
252 * \param color first color, RGBA | |
253 * \param color2 second color, RGBA | |
254 * \param brk x coordinate relative to glyph origin, color is used to the left of brk, color2 - to the right | |
255 * Performs clipping. Uses my_draw_bitmap for actual bitmap convertion. | |
256 */ | |
257 static int render_glyph(FT_BitmapGlyph bit, int dst_x, int dst_y, uint32_t color, uint32_t color2, int brk) | |
258 { | |
259 // brk is relative to dst_x | |
260 // color = color left of brk | |
261 // color2 = color right of brk | |
262 int b_x0, b_y0, b_x1, b_y1; // visible part of the bitmap | |
263 int clip_x0, clip_y0, clip_x1, clip_y1; | |
264 int tmp; | |
265 FT_Bitmap* bitmap; | |
266 | |
267 bitmap = &(bit->bitmap); | |
268 dst_x += bit->left; | |
269 dst_y -= bit->top; | |
270 brk -= bit->left; | |
271 | |
272 if (bitmap->pixel_mode != FT_PIXEL_MODE_GRAY) { | |
273 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Unsupported pixel mode: %d\n", (int)(bitmap->pixel_mode)); | |
274 return 1; | |
275 } | |
276 | |
277 // clipping | |
278 clip_x0 = render_context.clip_x0; | |
279 clip_y0 = render_context.clip_y0; | |
280 clip_x1 = render_context.clip_x1; | |
281 clip_y1 = render_context.clip_y1; | |
282 b_x0 = 0; | |
283 b_y0 = 0; | |
284 b_x1 = bitmap->width; | |
285 b_y1 = bitmap->rows; | |
286 | |
287 tmp = dst_x - clip_x0; | |
288 if (tmp < 0) { | |
289 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "clip left\n"); | |
290 b_x0 = - tmp; | |
291 } | |
292 tmp = dst_y - clip_y0; | |
293 if (tmp < 0) { | |
294 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "clip top\n"); | |
295 b_y0 = - tmp; | |
296 } | |
297 tmp = clip_x1 - dst_x - bitmap->width; | |
298 if (tmp < 0) { | |
299 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "clip right\n"); | |
300 b_x1 = bitmap->width + tmp; | |
301 } | |
302 tmp = clip_y1 - dst_y - bitmap->rows; | |
303 if (tmp < 0) { | |
304 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "clip bottom\n"); | |
305 b_y1 = bitmap->rows + tmp; | |
306 } | |
307 | |
308 if ((b_y0 >= b_y1) || (b_x0 >= b_x1)) | |
309 return 0; | |
310 | |
311 if (brk > b_x0) { // draw left part | |
312 if (brk > b_x1) brk = b_x1; | |
313 my_draw_bitmap(bitmap->buffer + bitmap->pitch * b_y0 + b_x0, | |
314 brk - b_x0, b_y1 - b_y0, bitmap->pitch, | |
315 dst_x + b_x0, dst_y + b_y0, color); | |
316 } | |
317 if (brk < b_x1) { // draw right part | |
318 if (brk < b_x0) brk = b_x0; | |
319 my_draw_bitmap(bitmap->buffer + bitmap->pitch * b_y0 + brk, | |
320 b_x1 - brk, b_y1 - b_y0, bitmap->pitch, | |
321 dst_x + brk, dst_y + b_y0, color2); | |
322 | |
323 } | |
324 return 0; | |
325 } | |
326 | |
327 /** | |
328 * \brief Render text_info_t struct into ass_images_t list | |
329 * Rasterize glyphs and put them in glyph cache. | |
330 */ | |
331 static int render_text(text_info_t* text_info, int dst_x, int dst_y) | |
332 { | |
333 int pen_x, pen_y; | |
334 int error, error2; | |
335 int i; | |
336 FT_Glyph image; | |
337 FT_BitmapGlyph bit; | |
338 glyph_hash_val_t hash_val; | |
339 | |
340 for (i = 0; i < text_info->length; ++i) { | |
341 if (text_info->glyphs[i].bitmap != 1) { | |
342 if ((text_info->glyphs[i].symbol == '\n') || (text_info->glyphs[i].symbol == 0)) | |
343 continue; | |
344 error = FT_Glyph_To_Bitmap( &(text_info->glyphs[i].outline_glyph), FT_RENDER_MODE_NORMAL, 0, 1); | |
345 error2 = FT_Glyph_To_Bitmap( &(text_info->glyphs[i].glyph), FT_RENDER_MODE_NORMAL, 0, 1); | |
346 | |
347 if (error || error2) { | |
348 FT_Done_Glyph(text_info->glyphs[i].outline_glyph); | |
349 FT_Done_Glyph(text_info->glyphs[i].glyph); | |
350 mp_msg(MSGT_GLOBAL, MSGL_WARN, "FT_Glyph_To_Bitmap error %d %d, symbol %d, index %d\n", | |
351 error, error2, text_info->glyphs[i].symbol, text_info->glyphs[i].hash_key.index); | |
352 text_info->glyphs[i].symbol = 0; // do not render | |
353 continue; | |
354 } | |
355 // cache | |
356 text_info->glyphs[i].hash_key.bitmap = 1; // other hash_key fields were set in get_glyph() | |
357 hash_val.bbox_scaled = text_info->glyphs[i].bbox; | |
358 hash_val.outline_glyph = text_info->glyphs[i].outline_glyph; | |
359 hash_val.glyph = text_info->glyphs[i].glyph; | |
360 hash_val.advance.x = text_info->glyphs[i].advance.x; | |
361 hash_val.advance.y = text_info->glyphs[i].advance.y; | |
362 cache_add_glyph(&(text_info->glyphs[i].hash_key), &hash_val); | |
363 } | |
364 } | |
365 | |
366 for (i = 0; i < text_info->length; ++i) { | |
367 glyph_info_t* info = text_info->glyphs + i; | |
368 if ((info->symbol == 0) || (info->symbol == '\n')) | |
369 continue; | |
370 | |
371 pen_x = dst_x + info->pos.x; | |
372 pen_y = dst_y + info->pos.y; | |
373 image = info->outline_glyph; | |
374 bit = (FT_BitmapGlyph)image; | |
375 | |
376 if ((info->effect_type == EF_KARAOKE_KO) && (info->effect_timing <= info->bbox.xMax)) { | |
377 // do nothing | |
378 } else | |
379 render_glyph(bit, pen_x, pen_y, info->c3, 0, 1000000); | |
380 } | |
381 for (i = 0; i < text_info->length; ++i) { | |
382 glyph_info_t* info = text_info->glyphs + i; | |
383 if ((info->symbol == 0) || (info->symbol == '\n')) | |
384 continue; | |
385 | |
386 pen_x = dst_x + info->pos.x; | |
387 pen_y = dst_y + info->pos.y; | |
388 image = info->glyph; | |
389 bit = (FT_BitmapGlyph)image; | |
390 | |
391 if ((info->effect_type == EF_KARAOKE) || (info->effect_type == EF_KARAOKE_KO)) { | |
392 if (info->effect_timing > info->bbox.xMax) | |
393 render_glyph(bit, pen_x, pen_y, info->c1, 0, 1000000); | |
394 else | |
395 render_glyph(bit, pen_x, pen_y, info->c2, 0, 1000000); | |
396 } else if (info->effect_type == EF_KARAOKE_KF) { | |
397 render_glyph(bit, pen_x, pen_y, info->c1, info->c2, info->effect_timing); | |
398 } else | |
399 render_glyph(bit, pen_x, pen_y, info->c1, 0, 1000000); | |
400 } | |
401 | |
402 return 0; | |
403 } | |
404 | |
405 /** | |
406 * \brief Mapping between script and screen coordinates | |
407 */ | |
408 static int x2scr(int x) { | |
409 return x*frame_context.width / frame_context.track->PlayResX; | |
410 } | |
411 /** | |
412 * \brief Mapping between script and screen coordinates | |
413 */ | |
414 static int y2scr(int y) { | |
415 return y * frame_context.orig_height / frame_context.track->PlayResY + global_settings->top_margin; | |
416 } | |
417 // the same for toptitles | |
418 static int y2scr_top(int y) { | |
419 return y * frame_context.orig_height / frame_context.track->PlayResY; | |
420 } | |
421 // the same for subtitles | |
422 static int y2scr_sub(int y) { | |
423 return y * frame_context.orig_height / frame_context.track->PlayResY + global_settings->top_margin + global_settings->bottom_margin; | |
424 } | |
425 | |
426 static void vmirror_bbox(FT_BBox* orig, FT_BBox* pbbox) { | |
427 pbbox->xMin = orig->xMin; | |
428 pbbox->xMax = orig->xMax; | |
429 pbbox->yMin = - orig->yMax; | |
430 pbbox->yMax = - orig->yMin; | |
431 } | |
432 | |
433 static void compute_string_bbox( text_info_t* info, FT_BBox *abbox ) { | |
434 FT_BBox bbox; | |
435 int n; | |
436 | |
437 /* initialize string bbox to "empty" values */ | |
438 bbox.xMin = bbox.yMin = 32000; | |
439 bbox.xMax = bbox.yMax = -32000; | |
440 | |
441 /* for each glyph image, compute its bounding box, */ | |
442 /* translate it, and grow the string bbox */ | |
443 for ( n = 0; n < info->length; n++ ) { | |
444 FT_BBox glyph_bbox; | |
445 vmirror_bbox( &(info->glyphs[n].bbox), &glyph_bbox ); | |
446 glyph_bbox.xMin += info->glyphs[n].pos.x; | |
447 glyph_bbox.xMax += info->glyphs[n].pos.x; | |
448 glyph_bbox.yMin += info->glyphs[n].pos.y; | |
449 glyph_bbox.yMax += info->glyphs[n].pos.y; | |
450 if ( glyph_bbox.xMin < bbox.xMin ) bbox.xMin = glyph_bbox.xMin; | |
451 if ( glyph_bbox.yMin < bbox.yMin ) bbox.yMin = glyph_bbox.yMin; | |
452 if ( glyph_bbox.xMax > bbox.xMax ) bbox.xMax = glyph_bbox.xMax; | |
453 if ( glyph_bbox.yMax > bbox.yMax ) bbox.yMax = glyph_bbox.yMax; | |
454 } | |
455 | |
456 /* check that we really grew the string bbox */ | |
457 if ( bbox.xMin > bbox.xMax ) { | |
458 bbox.xMin = 0; | |
459 bbox.yMin = 0; | |
460 bbox.xMax = 0; | |
461 bbox.yMax = 0; | |
462 } | |
463 | |
464 /* return string bbox */ | |
465 *abbox = bbox; | |
466 } | |
467 | |
468 | |
469 /** | |
470 * \brief Check if starting part of (*p) matches sample. If true, shift p to the first symbol after the matching part. | |
471 */ | |
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
|
472 static inline int mystrcmp(char** p, const char* sample) { |
18937 | 473 int len = strlen(sample); |
474 if (strncmp(*p, sample, len) == 0) { | |
475 (*p) += len; | |
476 return 1; | |
477 } else | |
478 return 0; | |
479 } | |
480 | |
19401
c0c3a2f8bb32
Add subdata to ass_track conversion for external subtitles.
eugeni
parents:
19399
diff
changeset
|
481 double ass_internal_font_size_coeff = 0.8; |
c0c3a2f8bb32
Add subdata to ass_track conversion for external subtitles.
eugeni
parents:
19399
diff
changeset
|
482 |
18937 | 483 static void change_font_size(int sz) |
484 { | |
19401
c0c3a2f8bb32
Add subdata to ass_track conversion for external subtitles.
eugeni
parents:
19399
diff
changeset
|
485 double size = (double)sz * global_settings->font_size_coeff * ass_internal_font_size_coeff; |
18937 | 486 size *= frame_context.height; |
487 size /= frame_context.track->PlayResY; | |
488 | |
489 if (size < 1) | |
490 size = 1; | |
491 else if (size > frame_context.height * 2) | |
492 size = frame_context.height * 2; | |
493 | |
494 FT_Set_Pixel_Sizes(render_context.face, 0, size); | |
495 | |
496 render_context.font_size = sz; | |
497 } | |
498 | |
499 /** | |
500 * \brief Change current font, using setting from render_context. | |
501 */ | |
502 static void update_font(void) | |
503 { | |
504 int error; | |
505 unsigned val; | |
506 ass_instance_t* priv = frame_context.ass_priv; | |
507 face_desc_t desc; | |
508 desc.family = strdup(render_context.family); | |
509 | |
510 val = render_context.bold; | |
511 // 0 = normal, 1 = bold, >1 = exact weight | |
512 if (val == 0) val = 80; // normal | |
513 else if (val == 1) val = 200; // bold | |
514 desc.bold = val; | |
515 | |
516 val = render_context.italic; | |
517 if (val == 0) val = 0; // normal | |
518 else if (val == 1) val = 110; //italic | |
519 desc.italic = val; | |
520 | |
521 error = ass_new_face(priv->library, priv->fontconfig_priv, &desc, &(render_context.face)); | |
522 if (error) { | |
523 render_context.face = 0; | |
524 } | |
525 | |
526 if (render_context.face) | |
527 { | |
528 change_font_size(render_context.font_size); | |
529 } | |
530 } | |
531 | |
532 /** | |
533 * \brief Change border width | |
534 */ | |
535 static void change_border(double border) | |
536 { | |
537 if (!render_context.stroker) { | |
538 if (!no_more_font_messages) | |
539 mp_msg(MSGT_GLOBAL, MSGL_WARN, "No stroker!\n"); | |
540 } else { | |
541 render_context.border = border; | |
542 FT_Stroker_Set( render_context.stroker, | |
543 (int)(64 * border), | |
544 FT_STROKER_LINECAP_ROUND, | |
545 FT_STROKER_LINEJOIN_ROUND, | |
546 0 ); | |
547 } | |
548 } | |
549 | |
550 #define _r(c) ((c)>>24) | |
551 #define _g(c) (((c)>>16)&0xFF) | |
552 #define _b(c) (((c)>>8)&0xFF) | |
553 #define _a(c) ((c)&0xFF) | |
554 | |
555 /** | |
556 * \brief Calculate a weighted average of two colors | |
557 * calculates c1*(1-a) + c2*a, but separately for each component except alpha | |
558 */ | |
559 static void change_color(uint32_t* var, uint32_t new, double pwr) | |
560 { | |
561 (*var)= ((uint32_t)(_r(*var) * (1 - pwr) + _r(new) * pwr) << 24) + | |
562 ((uint32_t)(_g(*var) * (1 - pwr) + _g(new) * pwr) << 16) + | |
563 ((uint32_t)(_b(*var) * (1 - pwr) + _b(new) * pwr) << 8) + | |
564 _a(*var); | |
565 } | |
566 | |
567 // like change_color, but for alpha component only | |
568 static void change_alpha(uint32_t* var, uint32_t new, double pwr) | |
569 { | |
570 *var = (_r(*var) << 24) + (_g(*var) << 16) + (_b(*var) << 8) + (_a(*var) * (1 - pwr) + _a(new) * pwr); | |
571 } | |
572 | |
19406
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
573 /** |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
574 * \brief Multiply two alpha values |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
575 * \param a first value |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
576 * \param b second value |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
577 * \return result of multiplication |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
578 * Parameters and result are limited by 0xFF. |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
579 */ |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
580 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
|
581 { |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
582 return 0xFF - (0xFF - a) * (0xFF - b) / 0xFF; |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
583 } |
18937 | 584 |
585 /** | |
586 * \brief Calculate alpha value by piecewise linear function | |
587 * Used for \fad, \fade implementation. | |
588 */ | |
589 static void interpolate_alpha(long long now, | |
590 long long t1, long long t2, long long t3, long long t4, | |
591 unsigned a1, unsigned a2, unsigned a3) | |
592 { | |
593 unsigned a; | |
594 double cf; | |
595 if (now <= t1) { | |
596 a = a1; | |
597 } else if (now >= t4) { | |
598 a = a3; | |
599 } else if (now < t2) { // and > t1 | |
600 cf = ((double)(now - t1)) / (t2 - t1); | |
601 a = a1 * (1 - cf) + a2 * cf; | |
602 } else if (now > t3) { | |
603 cf = ((double)(now - t3)) / (t4 - t3); | |
604 a = a2 * (1 - cf) + a3 * cf; | |
605 } else { // t2 <= now <= t3 | |
606 a = a2; | |
607 } | |
608 | |
609 | |
19406
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
610 change_alpha(&render_context.c1, mult_alpha(_a(render_context.c1), a), 1.); |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
611 change_alpha(&render_context.c2, mult_alpha(_a(render_context.c2), a), 1.); |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
612 change_alpha(&render_context.c3, mult_alpha(_a(render_context.c3), a), 1.); |
747a5c394a69
Fix wrong handling of transparency in \fad(\fade).
eugeni
parents:
19405
diff
changeset
|
613 change_alpha(&render_context.c4, mult_alpha(_a(render_context.c4), a), 1.); |
18937 | 614 } |
615 | |
616 /** | |
617 * \brief Parse style override tag. | |
618 * \param p string to parse | |
619 * \param pwr multiplier for some tag effects (comes from \t tags) | |
620 */ | |
621 static char* parse_tag(char* p, double pwr) { | |
622 #define skip_all(x) if (*p == (x)) ++p; else { \ | |
623 while ((*p != (x)) && (*p != '}') && (*p != 0)) {++p;} } | |
624 #define skip(x) if (*p == (x)) ++p; else { return p; } | |
625 | |
626 skip_all('\\'); | |
627 if ((*p == '}') || (*p == 0)) | |
628 return p; | |
629 | |
630 if (mystrcmp(&p, "fsc")) { | |
631 char tp = *p++; | |
632 double val; | |
633 if (tp == 'x') { | |
634 if (mystrtod(&p, &val)) { | |
635 val /= 100; | |
636 render_context.scale_x = (val - 1.) * pwr + 1.; | |
637 } else | |
638 render_context.scale_x = render_context.style->ScaleX; | |
639 } else if (tp == 'y') { | |
640 if (mystrtod(&p, &val)) { | |
641 val /= 100; | |
642 render_context.scale_y = (val - 1.) * pwr + 1.; | |
643 } else | |
644 render_context.scale_y = render_context.style->ScaleY; | |
645 } | |
646 } else if (mystrcmp(&p, "fsp")) { | |
647 int val; | |
648 if (mystrtoi(&p, 10, &val)) | |
649 render_context.hspacing = val * pwr; | |
650 else | |
651 render_context.hspacing = 0; | |
652 } else if (mystrcmp(&p, "fs")) { | |
653 int val; | |
654 if (mystrtoi(&p, 10, &val)) | |
655 val = render_context.font_size * ( 1 - pwr ) + val * pwr; | |
656 else | |
657 val = render_context.style->FontSize; | |
658 if (render_context.face) | |
659 change_font_size(val); | |
660 } else if (mystrcmp(&p, "bord")) { | |
661 double val; | |
662 if (mystrtod(&p, &val)) | |
663 val = render_context.border * ( 1 - pwr ) + val * pwr; | |
664 else | |
665 val = (render_context.style->BorderStyle == 1) ? render_context.style->Outline : 1.; | |
666 change_border(val); | |
667 } else if (mystrcmp(&p, "move")) { | |
668 int x1, x2, y1, y2; | |
19044 | 669 long long t1, t2, delta_t, t; |
18937 | 670 int x, y; |
671 double k; | |
672 skip('('); | |
673 x1 = strtol(p, &p, 10); | |
674 skip(','); | |
675 y1 = strtol(p, &p, 10); | |
676 skip(','); | |
677 x2 = strtol(p, &p, 10); | |
678 skip(','); | |
679 y2 = strtol(p, &p, 10); | |
19044 | 680 if (*p == ',') { |
681 skip(','); | |
682 t1 = strtoll(p, &p, 10); | |
683 skip(','); | |
684 t2 = strtoll(p, &p, 10); | |
19378 | 685 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "movement6: (%d, %d) -> (%d, %d), (%" PRId64 " .. %" PRId64 ")\n", |
686 x1, y1, x2, y2, (int64_t)t1, (int64_t)t2); | |
19044 | 687 } else { |
688 t1 = 0; | |
689 t2 = render_context.event->Duration; | |
690 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "movement: (%d, %d) -> (%d, %d)\n", x1, y1, x2, y2); | |
691 } | |
692 skip(')'); | |
693 delta_t = t2 - t1; | |
694 t = frame_context.time - render_context.event->Start; | |
695 if (t < t1) | |
696 k = 0.; | |
697 else if (t > t2) | |
698 k = 1.; | |
699 else k = ((double)(t - t1)) / delta_t; | |
18937 | 700 x = k * (x2 - x1) + x1; |
701 y = k * (y2 - y1) + y1; | |
702 render_context.pos_x = x; | |
703 render_context.pos_y = y; | |
704 render_context.detect_collisions = 0; | |
705 render_context.evt_type = EVENT_POSITIONED; | |
706 } else if (mystrcmp(&p, "frx") || mystrcmp(&p, "fry")) { | |
707 int val; | |
708 mystrtoi(&p, 10, &val); | |
709 mp_msg(MSGT_GLOBAL, MSGL_V, "frx/fry unimplemented \n"); | |
710 } else if (mystrcmp(&p, "frz") || mystrcmp(&p, "fr")) { | |
711 double angle; | |
712 int val; | |
713 val = strtol(p, &p, 10); | |
714 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "setting rotation to %.2f\n", val * pwr); | |
715 angle = M_PI * val / 180; | |
716 render_context.rotation = angle * pwr; | |
717 } else if (mystrcmp(&p, "fn")) { | |
718 char* start = p; | |
719 char* family; | |
720 skip_all('\\'); | |
721 family = malloc(p - start + 1); | |
722 strncpy(family, start, p - start); | |
723 family[p - start] = '\0'; | |
724 if (render_context.family) | |
725 free(render_context.family); | |
726 render_context.family = family; | |
727 update_font(); | |
728 } else if (mystrcmp(&p, "alpha")) { | |
729 uint32_t val; | |
730 if (strtocolor(&p, &val)) { | |
731 unsigned char a = val >> 24; | |
732 change_alpha(&render_context.c1, a, pwr); | |
733 change_alpha(&render_context.c2, a, pwr); | |
734 change_alpha(&render_context.c3, a, pwr); | |
735 change_alpha(&render_context.c4, a, pwr); | |
736 } else { | |
737 change_alpha(&render_context.c1, render_context.style->PrimaryColour, pwr); | |
738 change_alpha(&render_context.c2, render_context.style->SecondaryColour, pwr); | |
739 change_alpha(&render_context.c3, render_context.style->OutlineColour, pwr); | |
740 change_alpha(&render_context.c4, render_context.style->BackColour, pwr); | |
741 } | |
742 // FIXME: simplify | |
743 } else if (mystrcmp(&p, "an")) { | |
744 int val = strtol(p, &p, 10); | |
745 int v = (val - 1) / 3; // 0, 1 or 2 for vertical alignment | |
746 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "an %d\n", val); | |
747 if (v != 0) v = 3 - v; | |
748 val = ((val - 1) % 3) + 1; // horizontal alignment | |
749 val += v*4; | |
750 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "align %d\n", val); | |
751 render_context.alignment = val; | |
752 } else if (mystrcmp(&p, "a")) { | |
753 int val = strtol(p, &p, 10); | |
754 render_context.alignment = val; | |
755 } else if (mystrcmp(&p, "pos")) { | |
756 int v1, v2; | |
757 skip('('); | |
758 v1 = strtol(p, &p, 10); | |
759 skip(','); | |
760 v2 = strtol(p, &p, 10); | |
761 skip(')'); | |
762 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "pos(%d, %d)\n", v1, v2); | |
763 render_context.evt_type = EVENT_POSITIONED; | |
764 render_context.detect_collisions = 0; | |
765 render_context.pos_x = v1; | |
766 render_context.pos_y = v2; | |
19398
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
767 } else if (mystrcmp(&p, "fad")) { |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
768 int a1, a2, a3; |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
769 long long t1, t2, t3, t4; |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
770 if (*p == 'e') ++p; // either \fad or \fade |
18937 | 771 skip('('); |
772 a1 = strtol(p, &p, 10); | |
773 skip(','); | |
774 a2 = strtol(p, &p, 10); | |
19398
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
775 if (*p == ')') { |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
776 // 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
|
777 // 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
|
778 t1 = 0; |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
779 t4 = render_context.event->Duration; |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
780 t2 = a1; |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
781 t3 = t4 - a2; |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
782 a1 = 0xFF; |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
783 a2 = 0; |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
784 a3 = 0xFF; |
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
785 } else { |
19399 | 786 // 6-argument version (\fade) |
787 // a1 and a2 (and a3) are opacity values | |
788 skip(','); | |
789 a3 = strtol(p, &p, 10); | |
790 skip(','); | |
791 t1 = strtoll(p, &p, 10); | |
792 skip(','); | |
793 t2 = strtoll(p, &p, 10); | |
794 skip(','); | |
795 t3 = strtoll(p, &p, 10); | |
796 skip(','); | |
797 t4 = strtoll(p, &p, 10); | |
19398
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
798 } |
18937 | 799 skip(')'); |
19398
bb5164e0f70f
Allow \fade to be used in place of \fad and vice versa.
eugeni
parents:
19378
diff
changeset
|
800 interpolate_alpha(frame_context.time - render_context.event->Start, t1, t2, t3, t4, a1, a2, a3); |
18937 | 801 } else if (mystrcmp(&p, "org")) { |
802 int v1, v2; | |
803 skip('('); | |
804 v1 = strtol(p, &p, 10); | |
805 skip(','); | |
806 v2 = strtol(p, &p, 10); | |
807 skip(')'); | |
808 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "org(%d, %d)\n", v1, v2); | |
809 // render_context.evt_type = EVENT_POSITIONED; | |
810 render_context.org_x = v1; | |
811 render_context.org_y = v2; | |
812 } else if (mystrcmp(&p, "t")) { | |
813 double v[3]; | |
814 int v1, v2; | |
815 double v3; | |
816 int cnt; | |
817 long long t1, t2, t, delta_t; | |
818 double k; | |
819 skip('('); | |
820 for (cnt = 0; cnt < 3; ++cnt) { | |
821 if (*p == '\\') | |
822 break; | |
823 v[cnt] = strtod(p, &p); | |
824 skip(','); | |
825 } | |
826 if (cnt == 3) { | |
827 v1 = v[0]; v2 = v[1]; v3 = v[2]; | |
828 } else if (cnt == 2) { | |
829 v1 = v[0]; v2 = v[1]; v3 = 1.; | |
830 } else if (cnt == 1) { | |
831 v1 = 0; v2 = render_context.event->Duration; v3 = v[0]; | |
832 } else { // cnt == 0 | |
833 v1 = 0; v2 = render_context.event->Duration; v3 = 1.; | |
834 } | |
835 render_context.detect_collisions = 0; | |
836 t1 = v1; | |
837 t2 = v2; | |
838 delta_t = v2 - v1; | |
839 t = frame_context.time - render_context.event->Start; // FIXME: move to render_context | |
840 if (t < t1) | |
841 k = 0.; | |
842 else if (t > t2) | |
843 k = 1.; | |
844 else k = ((double)(t - t1)) / delta_t; | |
845 while (*p == '\\') | |
846 p = parse_tag(p, k); // maybe k*pwr ? no, specs forbid nested \t's | |
847 skip_all(')'); // FIXME: better skip(')'), but much more tags support required | |
848 } else if (mystrcmp(&p, "clip")) { | |
849 int x0, y0, x1, y1; | |
850 int res = 1; | |
851 skip('('); | |
852 res &= mystrtoi(&p, 10, &x0); | |
853 skip(','); | |
854 res &= mystrtoi(&p, 10, &y0); | |
855 skip(','); | |
856 res &= mystrtoi(&p, 10, &x1); | |
857 skip(','); | |
858 res &= mystrtoi(&p, 10, &y1); | |
859 skip(')'); | |
860 if (res) { | |
861 render_context.clip_x0 = render_context.clip_x0 * (1-pwr) + x0 * pwr; | |
862 render_context.clip_x1 = render_context.clip_x1 * (1-pwr) + x1 * pwr; | |
863 render_context.clip_y0 = render_context.clip_y0 * (1-pwr) + y0 * pwr; | |
864 render_context.clip_y1 = render_context.clip_y1 * (1-pwr) + y1 * pwr; | |
865 } else { | |
866 render_context.clip_x0 = 0; | |
867 render_context.clip_y0 = 0; | |
868 render_context.clip_x1 = frame_context.track->PlayResX; | |
869 render_context.clip_y1 = frame_context.track->PlayResY; | |
870 } | |
871 } else if (mystrcmp(&p, "c")) { | |
872 uint32_t val; | |
873 if (!strtocolor(&p, &val)) | |
874 val = render_context.style->PrimaryColour; | |
875 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "color: %X\n", val); | |
876 change_color(&render_context.c1, val, pwr); | |
877 } else if ((*p >= '1') && (*p <= '4') && (++p) && (mystrcmp(&p, "c") || mystrcmp(&p, "a"))) { | |
878 char n = *(p-2); | |
879 char cmd = *(p-1); | |
880 uint32_t* pcolor; | |
881 uint32_t val; | |
882 assert((n >= '1') && (n <= '4')); | |
883 if (!strtocolor(&p, &val)) | |
884 switch(n) { | |
885 case '1': val = render_context.style->PrimaryColour; break; | |
886 case '2': val = render_context.style->SecondaryColour; break; | |
887 case '3': val = render_context.style->OutlineColour; break; | |
888 case '4': val = render_context.style->BackColour; break; | |
889 default : val = 0; break; // impossible due to assert; avoid compilation warning | |
890 } | |
891 switch (n) { | |
892 case '1': pcolor = &render_context.c1; break; | |
893 case '2': pcolor = &render_context.c2; break; | |
894 case '3': pcolor = &render_context.c3; break; | |
895 case '4': pcolor = &render_context.c4; break; | |
896 default : pcolor = 0; break; | |
897 } | |
898 switch (cmd) { | |
899 case 'c': change_color(pcolor, val, pwr); break; | |
900 case 'a': change_alpha(pcolor, val >> 24, pwr); break; | |
901 default: mp_msg(MSGT_GLOBAL, MSGL_WARN, "Bad command: %c%c\n", n, cmd); break; | |
902 } | |
903 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "single c/a at %f: %c%c = %X \n", pwr, n, cmd, *pcolor); | |
904 } else if (mystrcmp(&p, "r")) { | |
905 render_context.c1 = render_context.style->PrimaryColour; | |
906 render_context.c2 = render_context.style->SecondaryColour; | |
907 render_context.c3 = render_context.style->OutlineColour; | |
908 render_context.c4 = render_context.style->BackColour; | |
909 render_context.font_size = render_context.style->FontSize; | |
910 | |
911 if (render_context.family) | |
912 free(render_context.family); | |
913 render_context.family = strdup(render_context.style->FontName); | |
914 render_context.bold = - render_context.style->Bold; | |
915 render_context.italic = - render_context.style->Italic; | |
916 update_font(); | |
917 | |
918 if (render_context.stroker) { | |
919 double border = (render_context.style->BorderStyle == 1) ? render_context.style->Outline : 1.; | |
920 change_border(border); | |
921 } | |
922 render_context.scale_x = render_context.style->ScaleX; | |
923 render_context.scale_y = render_context.style->ScaleY; | |
924 render_context.hspacing = 0; // FIXME | |
925 | |
926 // FIXME: does not reset unsupported attributes. | |
927 } else if (mystrcmp(&p, "be")) { | |
928 int val; | |
929 mystrtoi(&p, 10, &val); | |
930 mp_msg(MSGT_GLOBAL, MSGL_V, "be unimplemented \n"); | |
931 } else if (mystrcmp(&p, "b")) { | |
932 int b; | |
933 if (mystrtoi(&p, 10, &b)) | |
934 render_context.bold = b; | |
935 else | |
936 render_context.bold = - render_context.style->Bold; | |
937 update_font(); | |
938 } else if (mystrcmp(&p, "i")) { | |
939 int i; | |
940 if (mystrtoi(&p, 10, &i)) | |
941 render_context.italic = i; | |
942 else | |
943 render_context.italic = - render_context.style->Italic; | |
944 update_font(); | |
945 } else if (mystrcmp(&p, "kf") || mystrcmp(&p, "K")) { | |
946 int val = strtol(p, &p, 10); | |
947 render_context.effect_type = EF_KARAOKE_KF; | |
948 render_context.effect_timing = val * 10; | |
949 } else if (mystrcmp(&p, "ko")) { | |
950 int val = strtol(p, &p, 10); | |
951 render_context.effect_type = EF_KARAOKE_KO; | |
952 render_context.effect_timing = val * 10; | |
953 } else if (mystrcmp(&p, "k")) { | |
954 int val = strtol(p, &p, 10); | |
955 render_context.effect_type = EF_KARAOKE; | |
956 render_context.effect_timing = val * 10; | |
957 } | |
958 | |
959 return p; | |
960 | |
961 #undef skip | |
962 #undef skip_all | |
963 } | |
964 | |
965 /** | |
966 * \brief Get next ucs4 char from string, parsing and executing style overrides | |
967 * \param str string pointer | |
968 * \return ucs4 code of the next char | |
969 * On return str points to the unparsed part of the string | |
970 */ | |
971 static unsigned get_next_char(char** str) | |
972 { | |
973 char* p = *str; | |
974 unsigned chr; | |
975 if (*p == '{') { // '\0' goes here | |
976 p++; | |
977 while (1) { | |
978 p = parse_tag(p, 1.); | |
979 if (*p == '}') { // end of tag | |
980 p++; | |
981 if (*p == '{') { | |
982 p++; | |
983 continue; | |
984 } else | |
985 break; | |
986 } else if (*p != '\\') | |
987 mp_msg(MSGT_GLOBAL, MSGL_V, "Unable to parse: \"%s\" \n", p); | |
988 if (*p == 0) | |
989 break; | |
990 } | |
991 } | |
992 if (*p == '\t') { | |
993 ++p; | |
994 *str = p; | |
995 return ' '; | |
996 } | |
997 if (*p == '\\') { | |
998 if ((*(p+1) == 'N') || ((*(p+1) == 'n') && (frame_context.track->WrapStyle == 2))) { | |
999 p += 2; | |
1000 *str = p; | |
1001 return '\n'; | |
1002 } else if (*(p+1) == 'n') { | |
1003 p += 2; | |
1004 *str = p; | |
1005 return ' '; | |
1006 } | |
1007 } | |
1008 chr = utf8_get_char(&p); | |
1009 *str = p; | |
1010 return chr; | |
1011 } | |
1012 | |
1013 /** | |
1014 * \brief Start new event. Reset render_context. | |
1015 */ | |
1016 static int init_render_context(ass_event_t* event) | |
1017 { | |
1018 int error; | |
1019 | |
1020 // init render_context | |
1021 render_context.event = event; | |
1022 render_context.style = frame_context.track->styles + event->Style; | |
1023 | |
1024 render_context.font_size = render_context.style->FontSize; | |
1025 render_context.evt_type = EVENT_NORMAL; | |
1026 render_context.alignment = 0; | |
19404 | 1027 render_context.rotation = M_PI * render_context.style->Angle / 180.; |
18937 | 1028 render_context.pos_x = 0; |
1029 render_context.pos_y = 0; | |
1030 render_context.org_x = 0; | |
1031 render_context.org_y = 0; | |
1032 render_context.scale_x = render_context.style->ScaleX; | |
1033 render_context.scale_y = render_context.style->ScaleY; | |
1034 render_context.hspacing = 0; | |
1035 render_context.c1 = render_context.style->PrimaryColour; | |
1036 render_context.c2 = render_context.style->SecondaryColour; | |
1037 render_context.c3 = render_context.style->OutlineColour; | |
1038 render_context.c4 = render_context.style->BackColour; | |
1039 render_context.clip_x0 = 0; | |
1040 render_context.clip_y0 = 0; | |
1041 render_context.clip_x1 = frame_context.track->PlayResX; | |
1042 render_context.clip_y1 = frame_context.track->PlayResY; | |
1043 render_context.detect_collisions = 1; | |
1044 | |
1045 if (render_context.family) | |
1046 free(render_context.family); | |
1047 render_context.family = strdup(render_context.style->FontName); | |
1048 render_context.bold = - render_context.style->Bold; // style value for bold text is -1 | |
1049 render_context.italic = - render_context.style->Italic; | |
1050 | |
1051 update_font(); | |
1052 | |
1053 if (render_context.face) { | |
1054 #if (FREETYPE_MAJOR > 2) || ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR > 1)) | |
1055 error = FT_Stroker_New( ass_instance->library, &render_context.stroker ); | |
1056 #else // < 2.2 | |
1057 error = FT_Stroker_New( render_context.face->memory, &render_context.stroker ); | |
1058 #endif | |
1059 if ( error ) { | |
1060 mp_msg(MSGT_GLOBAL, MSGL_V, "failed to get stroker\n"); | |
1061 render_context.stroker = 0; | |
1062 } else { | |
1063 // FIXME: probably wrong when render_context.Border == 3 | |
1064 double border = (render_context.style->BorderStyle == 1) ? render_context.style->Outline : 1.; | |
1065 change_border(border); | |
1066 } | |
1067 } | |
1068 | |
1069 return 0; | |
1070 } | |
1071 | |
1072 static int free_render_context(void) | |
1073 { | |
1074 if (render_context.stroker != 0) { | |
1075 FT_Stroker_Done(render_context.stroker); | |
1076 render_context.stroker = 0; | |
1077 } | |
1078 return 0; | |
1079 } | |
1080 | |
1081 /** | |
1082 * \brief Get normal and outline glyphs from cache (if possible) or font face | |
1083 * \param index face glyph index | |
1084 * \param symbol ucs4 char | |
1085 * \param info out: struct filled with extracted data | |
1086 * \param advance advance vector of the extracted glyph | |
1087 * \return 0 on success | |
1088 */ | |
1089 static int get_glyph(int index, int symbol, glyph_info_t* info, FT_Vector* advance) | |
1090 { | |
1091 int error; | |
1092 glyph_hash_val_t* val; | |
1093 glyph_hash_key_t* key = &(info->hash_key); | |
1094 | |
1095 key->face = render_context.face; | |
1096 key->size = render_context.font_size; | |
1097 key->index = index; | |
1098 key->outline = (render_context.border * 0xFFFF); // convert to 16.16 | |
1099 key->scale_x = (render_context.scale_x * 0xFFFF); | |
1100 key->scale_y = (render_context.scale_y * 0xFFFF); | |
1101 key->angle = (render_context.rotation * 0xFFFF); | |
1102 key->advance = *advance; | |
1103 key->bold = render_context.bold; | |
1104 key->italic = render_context.italic; | |
1105 | |
1106 key->bitmap = 1; // looking for bitmap glyph | |
1107 | |
1108 | |
1109 val = cache_find_glyph(key); | |
1110 // val = 0; | |
1111 | |
1112 if (val) { | |
1113 // bitmap glyph found, no need for FT_Glyph_Copy | |
1114 info->glyph = val->glyph; | |
1115 info->outline_glyph = val->outline_glyph; | |
1116 info->bbox = val->bbox_scaled; | |
1117 info->advance.x = val->advance.x; | |
1118 info->advance.y = val->advance.y; | |
1119 info->bitmap = 1; // bitmap glyph | |
1120 | |
1121 return 0; | |
1122 } | |
1123 | |
1124 // not found, get a new outline glyph from face | |
1125 // mp_msg(MSGT_GLOBAL, MSGL_INFO, "miss, index = %d, symbol = %c, adv = (%d, %d)\n", index, symbol, advance->x, advance->y); | |
1126 | |
1127 error = FT_Load_Glyph(render_context.face, index, FT_LOAD_NO_BITMAP ); | |
1128 if (error) { | |
1129 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Error loading glyph\n"); | |
1130 return error; | |
1131 } | |
1132 | |
1133 #if (FREETYPE_MAJOR > 2) || \ | |
1134 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR >= 2)) || \ | |
1135 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR == 1) && (FREETYPE_PATCH >= 10)) | |
1136 // FreeType >= 2.1.10 required | |
1137 if (!(render_context.face->style_flags & FT_STYLE_FLAG_ITALIC) && | |
1138 ((render_context.italic == 1) || (render_context.italic > 55))) { | |
1139 FT_GlyphSlot_Oblique(render_context.face->glyph); | |
1140 } | |
1141 #endif | |
1142 error = FT_Get_Glyph(render_context.face->glyph, &(info->glyph)); | |
1143 if (error) { | |
1144 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Error getting glyph\n"); | |
1145 return error; | |
1146 } | |
1147 | |
1148 info->advance.x = info->glyph->advance.x >> 10; | |
1149 info->advance.y = info->glyph->advance.y >> 10; | |
1150 | |
1151 info->outline_glyph = info->glyph; | |
19002
da05237044c2
Ignoring FT_Glyph_Stroke() errors can potentially lead to double free().
eugeni
parents:
18937
diff
changeset
|
1152 error = FT_Glyph_Stroke( &(info->outline_glyph), render_context.stroker, 0 ); // don't destroy original |
da05237044c2
Ignoring FT_Glyph_Stroke() errors can potentially lead to double free().
eugeni
parents:
18937
diff
changeset
|
1153 if (error) { |
da05237044c2
Ignoring FT_Glyph_Stroke() errors can potentially lead to double free().
eugeni
parents:
18937
diff
changeset
|
1154 mp_msg(MSGT_GLOBAL, MSGL_WARN, "FT_Glyph_Stroke error %d \n", error); |
da05237044c2
Ignoring FT_Glyph_Stroke() errors can potentially lead to double free().
eugeni
parents:
18937
diff
changeset
|
1155 FT_Glyph_Copy(info->glyph, &info->outline_glyph); |
da05237044c2
Ignoring FT_Glyph_Stroke() errors can potentially lead to double free().
eugeni
parents:
18937
diff
changeset
|
1156 } |
18937 | 1157 |
1158 info->bitmap = 0; // outline glyph | |
1159 | |
1160 return 0; | |
1161 } | |
1162 | |
1163 /** | |
1164 * \brief rearrange text between lines | |
1165 * \param max_text_width maximal text line width in pixels | |
1166 * The algo is similar to the one in libvo/sub.c: | |
1167 * 1. Place text, wrapping it when current line is full | |
1168 * 2. Try moving words from the end of a line to the beginning of the next one while it reduces | |
1169 * the difference in lengths between this two lines. | |
1170 * The result may not be optimal, but usually is good enough. | |
1171 */ | |
1172 static void wrap_lines_smart(int max_text_width) | |
1173 { | |
1174 int i, j; | |
1175 glyph_info_t *cur, *s1, *e1, *s2, *s3, *w; | |
1176 int last_space; | |
1177 int break_type; | |
1178 int exit; | |
1179 int pen_shift_x; | |
1180 int pen_shift_y; | |
1181 int max_asc, max_desc; | |
1182 int cur_line; | |
1183 | |
1184 last_space = -1; | |
1185 text_info.n_lines = 1; | |
1186 break_type = 0; | |
1187 s1 = text_info.glyphs; // current line start | |
1188 for (i = 0; i < text_info.length; ++i) { | |
19370 | 1189 int break_at, s_offset, len; |
18937 | 1190 cur = text_info.glyphs + i; |
19370 | 1191 break_at = -1; |
1192 s_offset = s1->bbox.xMin + s1->pos.x; | |
1193 len = (cur->bbox.xMax + cur->pos.x) - s_offset; | |
18937 | 1194 |
1195 if (cur->symbol == '\n') { | |
1196 break_type = 2; | |
1197 break_at = i; | |
1198 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "forced line break at %d\n", break_at); | |
1199 } | |
1200 | |
1201 if (len >= max_text_width) { | |
1202 break_type = 1; | |
1203 break_at = last_space; | |
1204 if (break_at == -1) | |
1205 break_at = i - 1; | |
1206 if (break_at == -1) | |
1207 break_at = 0; | |
1208 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "overfill at %d\n", i); | |
1209 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "line break at %d\n", break_at); | |
1210 } | |
1211 | |
1212 if (break_at != -1) { | |
1213 // need to use one more line | |
1214 // marking break_at+1 as start of a new line | |
1215 int lead = break_at + 1; // the first symbol of the new line | |
1216 if (text_info.n_lines >= MAX_LINES) { | |
1217 // to many lines ! | |
1218 // no more linebreaks | |
1219 for (j = lead; j < text_info.length; ++j) | |
1220 text_info.glyphs[j].linebreak = 0; | |
1221 break; | |
1222 } | |
1223 if (lead < text_info.length) | |
1224 text_info.glyphs[lead].linebreak = break_type; | |
1225 last_space = -1; | |
1226 s1 = text_info.glyphs + lead; | |
1227 s_offset = s1->bbox.xMin + s1->pos.x; | |
1228 text_info.n_lines ++; | |
1229 } | |
1230 | |
1231 if (cur->symbol == ' ') | |
1232 last_space = i; | |
1233 } | |
1234 #define DIFF(x,y) (((x) < (y)) ? (y - x) : (x - y)) | |
1235 exit = 0; | |
1236 while (!exit) { | |
1237 exit = 1; | |
1238 w = s3 = text_info.glyphs; | |
1239 s1 = s2 = 0; | |
1240 for (i = 0; i <= text_info.length; ++i) { | |
1241 cur = text_info.glyphs + i; | |
1242 if ((i == text_info.length) || cur->linebreak) { | |
1243 s1 = s2; | |
1244 s2 = s3; | |
1245 s3 = cur; | |
1246 // printf("line: %d, %d, %d \n", s1 - text_info.glyphs, s2 - text_info.glyphs, s3 - text_info.glyphs); | |
1247 if (s1 && (s2->linebreak == 1)) { // have at least 2 lines, and linebreak is 'soft' | |
1248 int l1, l2, l1_new, l2_new; | |
1249 | |
1250 w = s2; | |
1251 do { --w; } while ((w > s1) && (w->symbol == ' ')); | |
1252 do { --w; } while ((w > s1) && (w->symbol != ' ')); | |
1253 e1 = w; | |
1254 do { --e1; } while ((e1 > s1) && (e1->symbol == ' ')); | |
1255 if (w->symbol == ' ') ++w; | |
1256 | |
1257 // printf("check: %d, %d, %d, w: %d, e: %d \n", s1 - text_info.glyphs, s2 - text_info.glyphs, s3 - text_info.glyphs, | |
1258 // w - text_info.glyphs, e1 - text_info.glyphs); | |
1259 | |
1260 l1 = ((s2-1)->bbox.xMax + (s2-1)->pos.x) - (s1->bbox.xMin + s1->pos.x); | |
1261 l2 = ((s3-1)->bbox.xMax + (s3-1)->pos.x) - (s2->bbox.xMin + s2->pos.x); | |
1262 l1_new = (e1->bbox.xMax + e1->pos.x) - (s1->bbox.xMin + s1->pos.x); | |
1263 l2_new = ((s3-1)->bbox.xMax + (s3-1)->pos.x) - (w->bbox.xMin + w->pos.x); | |
1264 | |
1265 if (DIFF(l1_new, l2_new) < DIFF(l1, l2)) { | |
1266 w->linebreak = 1; | |
1267 s2->linebreak = 0; | |
1268 exit = 0; | |
1269 } | |
1270 } | |
1271 } | |
1272 if (i == text_info.length) | |
1273 break; | |
1274 } | |
1275 | |
1276 } | |
1277 assert(text_info.n_lines >= 1); | |
1278 #undef DIFF | |
1279 | |
1280 text_info.height = 0; | |
1281 max_asc = max_desc = 0; | |
1282 cur_line = 0; | |
1283 for (i = 0; i < text_info.length + 1; ++i) { | |
1284 if ((i == text_info.length) || text_info.glyphs[i].linebreak) { | |
1285 text_info.lines[cur_line].asc = max_asc; | |
1286 text_info.lines[cur_line].desc = max_desc; | |
1287 text_info.height += max_asc + max_desc; | |
1288 cur_line ++; | |
1289 max_asc = max_desc = 0; | |
1290 } | |
1291 if (i < text_info.length) { | |
1292 cur = text_info.glyphs + i; | |
1293 if (cur->asc > max_asc) | |
1294 max_asc = cur->asc * render_context.scale_y; | |
1295 if (cur->desc > max_desc) | |
1296 max_desc = cur->desc * render_context.scale_y; | |
1297 } | |
1298 } | |
1299 | |
1300 pen_shift_x = 0; | |
1301 pen_shift_y = 0; | |
1302 cur_line = 1; | |
1303 for (i = 0; i < text_info.length; ++i) { | |
1304 cur = text_info.glyphs + i; | |
1305 if (cur->linebreak) { | |
1306 int height = text_info.lines[cur_line - 1].desc + text_info.lines[cur_line].asc; | |
1307 cur_line ++; | |
1308 pen_shift_x = - cur->pos.x; | |
1309 pen_shift_y += (height >> 6) + global_settings->line_spacing; | |
1310 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); | |
1311 } | |
1312 cur->pos.x += pen_shift_x; | |
1313 cur->pos.y += pen_shift_y; | |
1314 } | |
1315 // printf("============= \n"); | |
1316 } | |
1317 | |
1318 /** | |
1319 * \brief determine karaoke effects | |
1320 * Karaoke effects cannot be calculated during parse stage (get_next_char()), | |
1321 * so they are done in a separate step. | |
1322 * Parse stage: when karaoke style override is found, its parameters are stored in the next glyph's | |
1323 * (the first glyph of the karaoke word)'s effect_type and effect_timing. | |
1324 * This function: | |
1325 * 1. sets effect_type for all glyphs in the word (_karaoke_ word) | |
1326 * 2. sets effect_timing for all glyphs to x coordinate of the border line between the left and right karaoke parts | |
1327 * (left part is filled with PrimaryColour, right one - with SecondaryColour). | |
1328 */ | |
1329 static void process_karaoke_effects(void) | |
1330 { | |
1331 glyph_info_t *cur, *cur2; | |
1332 glyph_info_t *s1, *e1; // start and end of the current word | |
1333 glyph_info_t *s2; // start of the next word | |
1334 int i; | |
1335 int timing; // current timing | |
1336 int tm_start, tm_end; // timings at start and end of the current word | |
1337 int tm_current; | |
1338 double dt; | |
1339 int x; | |
1340 int x_start, x_end; | |
1341 | |
1342 tm_current = frame_context.time - render_context.event->Start; | |
1343 timing = 0; | |
1344 s1 = s2 = 0; | |
1345 for (i = 0; i <= text_info.length; ++i) { | |
1346 cur = text_info.glyphs + i; | |
1347 if ((i == text_info.length) || (cur->effect_type != EF_NONE)) { | |
1348 s1 = s2; | |
1349 s2 = cur; | |
1350 if (s1) { | |
1351 e1 = s2 - 1; | |
1352 tm_start = timing; | |
1353 tm_end = timing + s1->effect_timing; | |
1354 timing = tm_end; | |
1355 x_start = s1->bbox.xMin + s1->pos.x; | |
1356 x_end = e1->bbox.xMax + e1->pos.x; | |
1357 | |
1358 dt = (tm_current - tm_start); | |
1359 if ((s1->effect_type == EF_KARAOKE) || (s1->effect_type == EF_KARAOKE_KO)) { | |
1360 if (dt > 0) | |
1361 x = x_end + 1; | |
1362 else | |
1363 x = x_start; | |
1364 } else if (s1->effect_type == EF_KARAOKE_KF) { | |
1365 dt /= (tm_end - tm_start); | |
1366 x = x_start + (x_end - x_start) * dt; | |
1367 } else { | |
1368 mp_msg(MSGT_GLOBAL, MSGL_ERR, "Unknown effect type (internal error) \n"); | |
1369 continue; | |
1370 } | |
1371 | |
1372 for (cur2 = s1; cur2 <= e1; ++cur2) { | |
1373 cur2->effect_type = s1->effect_type; | |
1374 cur2->effect_timing = x - cur2->pos.x; | |
1375 } | |
1376 } | |
1377 } | |
1378 } | |
1379 } | |
1380 | |
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
|
1381 static int get_face_ascender(FT_Face face) |
18937 | 1382 { |
1383 int v = face->size->metrics.ascender; | |
1384 if (!v) | |
1385 v = FT_MulFix(face->bbox.yMax, face->size->metrics.y_scale); | |
1386 return v; | |
1387 } | |
1388 | |
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
|
1389 static int get_face_descender(FT_Face face) |
18937 | 1390 { |
1391 int v = face->size->metrics.descender; | |
1392 if (!v) | |
1393 v = FT_MulFix(face->bbox.yMin, face->size->metrics.y_scale); | |
1394 return -v; | |
1395 } | |
1396 | |
1397 /** | |
1398 * \brief Main ass rendering function, glues everything together | |
1399 * \param event event to render | |
1400 * Process event, appending resulting ass_image_t's to images_root. | |
1401 */ | |
1402 int ass_render_event(ass_event_t* event) | |
1403 { | |
1404 char* p; | |
1405 FT_UInt glyph_index; | |
1406 FT_Bool use_kerning; | |
1407 FT_UInt previous; | |
1408 FT_UInt num_glyphs; | |
1409 FT_Vector pen; | |
1410 int error; | |
1411 unsigned code; | |
1412 FT_BBox bbox; | |
1413 int i, j; | |
1414 FT_Vector shift; | |
1415 int MarginL, MarginR, MarginV; | |
1416 int max_text_width; | |
1417 ass_style_t* style = frame_context.track->styles + event->Style; | |
1418 int last_break; | |
1419 int alignment, halign, valign; | |
1420 int device_x, device_y; | |
1421 | |
1422 init_render_context(event); | |
1423 | |
1424 text_info.length = 0; | |
1425 pen.x = 0; | |
1426 pen.y = 0; | |
1427 previous = 0; | |
1428 num_glyphs = 0; | |
1429 | |
1430 | |
1431 p = event->Text; | |
1432 if (!p) { | |
1433 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Empty event!\n"); | |
1434 return 0; | |
1435 } | |
1436 | |
1437 // Event parsing. | |
1438 while (1) { | |
1439 render_context.effect_type = EF_NONE; | |
1440 | |
1441 // get next char, executing style override | |
1442 // this affects render_context | |
1443 code = get_next_char(&p); | |
1444 | |
1445 // face could have been changed in get_next_char | |
1446 if (!render_context.face) { | |
1447 free_render_context(); | |
1448 return 0; | |
1449 } | |
1450 | |
1451 if (code == 0) | |
1452 break; | |
1453 | |
1454 use_kerning = FT_HAS_KERNING(render_context.face); | |
1455 | |
1456 if (text_info.length >= MAX_GLYPHS) { | |
1457 mp_msg(MSGT_GLOBAL, MSGL_WARN, "\nMAX_GLYPHS reached: event %d, start = %llu, duration = %llu\n Text = %s\n", | |
1458 (int)(event - frame_context.track->events), event->Start, event->Duration, event->Text); | |
1459 break; | |
1460 } | |
1461 | |
1462 glyph_index = FT_Get_Char_Index( render_context.face, code); | |
1463 | |
1464 if ( use_kerning && previous && glyph_index ) { | |
1465 FT_Vector delta; | |
1466 FT_Get_Kerning( render_context.face, previous, glyph_index, FT_KERNING_DEFAULT, &delta ); | |
1467 pen.x += delta.x; | |
1468 pen.y += delta.y; | |
1469 } | |
1470 | |
1471 shift.x = pen.x & 63; | |
1472 shift.y = pen.y & 63; | |
1473 | |
1474 if ((render_context.scale_x != 1.) || (render_context.scale_y != 1.) || | |
1475 (frame_context.font_scale_x != 1.)) { | |
1476 FT_Matrix matrix; | |
1477 matrix.xx = (FT_Fixed)( render_context.scale_x * frame_context.font_scale_x * 0x10000L ); | |
1478 matrix.xy = (FT_Fixed)( 0 * 0x10000L ); | |
1479 matrix.yx = (FT_Fixed)( 0 * 0x10000L ); | |
1480 matrix.yy = (FT_Fixed)( render_context.scale_y * 0x10000L ); | |
1481 | |
1482 FT_Set_Transform( render_context.face, &matrix, &shift ); | |
1483 } else { | |
1484 FT_Set_Transform(render_context.face, 0, &shift); | |
1485 } | |
1486 | |
1487 error = get_glyph(glyph_index, code, text_info.glyphs + text_info.length, &shift); | |
1488 | |
1489 if (error) { | |
1490 continue; | |
1491 } | |
1492 | |
1493 text_info.glyphs[text_info.length].pos.x = pen.x >> 6; | |
1494 text_info.glyphs[text_info.length].pos.y = pen.y >> 6; | |
1495 | |
1496 pen.x += text_info.glyphs[text_info.length].advance.x; | |
1497 pen.x += render_context.hspacing; | |
1498 pen.y += text_info.glyphs[text_info.length].advance.y; | |
1499 | |
1500 // if it's an outline glyph, we still need to fill the bbox | |
1501 if (text_info.glyphs[text_info.length].bitmap != 1) { | |
1502 FT_Glyph_Get_CBox( text_info.glyphs[text_info.length].glyph, FT_GLYPH_BBOX_PIXELS, &(text_info.glyphs[text_info.length].bbox) ); | |
1503 } | |
1504 | |
1505 | |
1506 previous = glyph_index; | |
1507 | |
1508 text_info.glyphs[text_info.length].symbol = code; | |
1509 text_info.glyphs[text_info.length].linebreak = 0; | |
1510 text_info.glyphs[text_info.length].c1 = render_context.c1; | |
1511 text_info.glyphs[text_info.length].c2 = render_context.c2; | |
1512 text_info.glyphs[text_info.length].c3 = render_context.c3; | |
1513 text_info.glyphs[text_info.length].c4 = render_context.c4; | |
1514 text_info.glyphs[text_info.length].effect_type = render_context.effect_type; | |
1515 text_info.glyphs[text_info.length].effect_timing = render_context.effect_timing; | |
1516 text_info.glyphs[text_info.length].asc = get_face_ascender(render_context.face); | |
1517 text_info.glyphs[text_info.length].desc = get_face_descender(render_context.face); | |
1518 | |
1519 text_info.length++; | |
1520 } | |
1521 | |
1522 if (text_info.length == 0) { | |
1523 // no valid symbols in the event; this can be smth like {comment} | |
1524 free_render_context(); | |
1525 return 0; | |
1526 } | |
1527 | |
1528 // depends on glyph x coordinates being monotonous, so it should be done before line wrap | |
1529 process_karaoke_effects(); | |
1530 | |
1531 // calculate max length of a line | |
1532 MarginL = (event->MarginL) ? event->MarginL : style->MarginL; | |
1533 MarginR = (event->MarginR) ? event->MarginR : style->MarginR; | |
1534 MarginV = (event->MarginV) ? event->MarginV : style->MarginV; | |
1535 | |
1536 max_text_width = x2scr(frame_context.track->PlayResX - MarginR) - x2scr(MarginL); | |
1537 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "normal text width: %d\n", max_text_width); | |
1538 | |
1539 // rearrange text in several lines | |
1540 wrap_lines_smart(max_text_width); | |
1541 | |
1542 // alignments | |
1543 alignment = render_context.alignment; | |
1544 if (!alignment) | |
1545 alignment = render_context.style->Alignment; | |
1546 halign = alignment & 3; | |
1547 valign = alignment & 12; | |
1548 | |
1549 // align text | |
1550 last_break = -1; | |
1551 for (i = 1; i < text_info.length + 1; ++i) { // (text_info.length + 1) is the end of the last line | |
1552 if ((i == text_info.length) || text_info.glyphs[i].linebreak) { | |
19370 | 1553 int width, shift; |
18937 | 1554 glyph_info_t* first_glyph = text_info.glyphs + last_break + 1; |
1555 glyph_info_t* last_glyph = text_info.glyphs + i - 1; | |
1556 | |
1557 while ((last_glyph >= first_glyph) && ((last_glyph->symbol == '\n') || (last_glyph->symbol == 0))) | |
1558 last_glyph --; | |
1559 | |
19370 | 1560 width = last_glyph->pos.x + last_glyph->bbox.xMax - first_glyph->pos.x - first_glyph->bbox.xMin; |
1561 shift = - first_glyph->bbox.xMin; // now text line starts exactly at 0 (left margin) | |
18937 | 1562 if (halign == HALIGN_LEFT) { // left aligned, no action |
1563 } else if (halign == HALIGN_RIGHT) { // right aligned | |
1564 shift = max_text_width - width; | |
1565 } else if (halign == HALIGN_CENTER) { // centered | |
1566 shift = (max_text_width - width) / 2; | |
1567 } | |
1568 for (j = last_break + 1; j < i; ++j) { | |
1569 text_info.glyphs[j].pos.x += shift; | |
1570 } | |
1571 last_break = i - 1; | |
1572 } | |
1573 } | |
1574 | |
1575 // determing text bounding box | |
1576 compute_string_bbox(&text_info, &bbox); | |
1577 | |
1578 // determine device coordinates for text | |
1579 | |
1580 // FIXME: using current font descender, ascender and height here is wrong. | |
1581 // correct way is using max(descender) over all the fonts used in a line | |
1582 // the same for height and ascender | |
1583 if (render_context.evt_type == EVENT_NORMAL) { | |
1584 device_x = x2scr(MarginL); | |
1585 if (valign == VALIGN_TOP) { // toptitle | |
1586 device_y = y2scr_top(MarginV) + (text_info.lines[0].asc >> 6); | |
1587 if (render_context.detect_collisions) { | |
1588 device_y += frame_context.add_top_margin; | |
1589 frame_context.add_top_margin += (text_info.height >> 6); | |
1590 } | |
1591 } else if (valign == VALIGN_CENTER) { // midtitle | |
1592 int scr_y = y2scr(frame_context.track->PlayResY / 2); | |
1593 device_y = scr_y - (bbox.yMax - bbox.yMin) / 2; | |
1594 } else { // subtitle | |
1595 int scr_y; | |
1596 if (valign != VALIGN_SUB) | |
1597 mp_msg(MSGT_GLOBAL, MSGL_V, "Invalid valign, supposing 0 (subtitle)\n"); | |
1598 scr_y = y2scr_sub(frame_context.track->PlayResY - MarginV); | |
1599 device_y = scr_y; | |
1600 device_y -= (text_info.height >> 6); | |
1601 device_y += (text_info.lines[0].asc >> 6); | |
1602 if (render_context.detect_collisions) { | |
1603 device_y -= frame_context.add_bottom_margin; | |
1604 frame_context.add_bottom_margin += (text_info.height >> 6); | |
1605 } | |
1606 } | |
1607 } else if (render_context.evt_type == EVENT_POSITIONED) { | |
1608 int align_shift_x = 0; | |
1609 int align_shift_y = 0; | |
1610 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "positioned event at %d, %d\n", render_context.pos_x, render_context.pos_y); | |
1611 switch(halign) { | |
1612 case HALIGN_LEFT: | |
1613 align_shift_x = - bbox.xMin; | |
1614 break; | |
1615 case HALIGN_CENTER: | |
1616 align_shift_x = - (bbox.xMax + bbox.xMin) /2; | |
1617 break; | |
1618 case HALIGN_RIGHT: | |
1619 align_shift_x = - bbox.xMax; | |
1620 break; | |
1621 } | |
1622 switch(valign) { | |
1623 case VALIGN_TOP: | |
1624 align_shift_y = - bbox.yMin; | |
1625 break; | |
1626 case VALIGN_CENTER: | |
1627 align_shift_y = - (bbox.yMax + bbox.yMin) /2; | |
1628 break; | |
1629 case VALIGN_SUB: | |
1630 align_shift_y = - bbox.yMax; | |
1631 break; | |
1632 } | |
1633 device_x = x2scr(render_context.pos_x) + align_shift_x; | |
1634 device_y = y2scr(render_context.pos_y) + align_shift_y; | |
1635 } else { | |
1636 mp_msg(MSGT_GLOBAL, MSGL_V, "unknown evt_type\n"); | |
1637 device_x = 10; | |
1638 device_y = 10; | |
1639 } | |
1640 | |
1641 // fix clip coordinates (they depend on alignment) | |
1642 render_context.clip_x0 = x2scr(render_context.clip_x0); | |
1643 render_context.clip_x1 = x2scr(render_context.clip_x1); | |
1644 if (render_context.evt_type == EVENT_NORMAL) { | |
1645 if (valign == VALIGN_TOP) { | |
1646 render_context.clip_y0 = y2scr_top(render_context.clip_y0); | |
1647 render_context.clip_y1 = y2scr_top(render_context.clip_y1); | |
1648 } else if (valign == VALIGN_CENTER) { | |
1649 render_context.clip_y0 = y2scr(render_context.clip_y0); | |
1650 render_context.clip_y1 = y2scr(render_context.clip_y1); | |
1651 } else if (valign == VALIGN_SUB) { | |
1652 render_context.clip_y0 = y2scr_sub(render_context.clip_y0); | |
1653 render_context.clip_y1 = y2scr_sub(render_context.clip_y1); | |
1654 } | |
1655 } else if (render_context.evt_type == EVENT_POSITIONED) { | |
1656 render_context.clip_y0 = y2scr(render_context.clip_y0); | |
1657 render_context.clip_y1 = y2scr(render_context.clip_y1); | |
1658 } | |
1659 | |
1660 // rotate glyphs if needed | |
1661 if (render_context.rotation != 0.) { | |
1662 double angle = render_context.rotation; | |
1663 FT_Vector center; | |
1664 FT_Matrix matrix_rotate; | |
1665 | |
1666 matrix_rotate.xx = (FT_Fixed)( cos( angle ) * 0x10000L ); | |
1667 matrix_rotate.xy = (FT_Fixed)( -sin( angle ) * 0x10000L ); | |
1668 matrix_rotate.yx = (FT_Fixed)( sin( angle ) * 0x10000L ); | |
1669 matrix_rotate.yy = (FT_Fixed)( cos( angle ) * 0x10000L ); | |
1670 | |
1671 if (((render_context.org_x != 0) || (render_context.org_y != 0)) && (render_context.evt_type == EVENT_POSITIONED)) { | |
1672 center.x = render_context.org_x; | |
1673 center.y = render_context.org_y; | |
1674 } else { | |
1675 FT_BBox str_bbox; | |
1676 | |
1677 center.x = text_info.glyphs[0].pos.x + device_x; | |
1678 center.y = text_info.glyphs[0].pos.y + device_y; | |
1679 | |
1680 compute_string_bbox(&text_info, &str_bbox); | |
1681 center.x += (str_bbox.xMax - str_bbox.xMin) / 2; | |
1682 center.y += (str_bbox.yMax - str_bbox.yMin) / 2; | |
1683 } | |
1684 // mp_msg(MSGT_GLOBAL, MSGL_DBG2, "\ncenter: %d, %d\n", center.x, center.y); | |
1685 | |
1686 for (i = 0; i < text_info.length; ++i) { | |
1687 glyph_info_t* info = text_info.glyphs + i; | |
1688 | |
1689 // calculating shift vector | |
1690 // shift = (position - center)*M - (position - center) | |
1691 FT_Vector start; | |
1692 FT_Vector start_old; | |
1693 // 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, | |
1694 // info->pos.x + device_x - center.x, info->pos.y + device_y - center.y); | |
1695 start.x = (info->pos.x + device_x - center.x) << 6; | |
1696 start.y = - (info->pos.y + device_y - center.y) << 6; | |
1697 start_old.x = start.x; | |
1698 start_old.y = start.y; | |
1699 // mp_msg(MSGT_GLOBAL, MSGL_INFO, "start: %d, %d\n", start.x / 64, start.y / 64); | |
1700 | |
1701 FT_Vector_Transform(&start, &matrix_rotate); | |
1702 | |
1703 start.x -= start_old.x; | |
1704 start.y -= start_old.y; | |
1705 | |
1706 info->pos.x += start.x >> 6; | |
1707 info->pos.y -= start.y >> 6; | |
1708 | |
1709 // mp_msg(MSGT_GLOBAL, MSGL_DBG2, "shift: %d, %d\n", start.x / 64, start.y / 64); | |
1710 if (info->bitmap != 1) { | |
1711 FT_Glyph_Transform( info->glyph, &matrix_rotate, 0 ); | |
1712 FT_Glyph_Transform( info->outline_glyph, &matrix_rotate, 0 ); | |
1713 } | |
1714 } | |
1715 } | |
1716 | |
1717 // render | |
1718 render_text(&text_info, device_x, device_y); | |
1719 | |
1720 free_render_context(); | |
1721 | |
1722 return 0; | |
1723 } | |
1724 | |
1725 void ass_configure(ass_instance_t* priv, const ass_settings_t* config) | |
1726 { | |
1727 memcpy(&priv->settings, config, sizeof(ass_settings_t)); | |
1728 } | |
1729 | |
1730 /** | |
1731 * \brief Start a new frame | |
1732 */ | |
1733 int ass_start_frame(ass_instance_t *priv, ass_track_t* track, long long now) | |
1734 { | |
1735 ass_instance = priv; | |
1736 global_settings = &priv->settings; | |
1737 | |
1738 if (!priv->settings.frame_width && !priv->settings.frame_height) | |
1739 return 1; // library not initialized | |
1740 | |
1741 frame_context.ass_priv = priv; | |
1742 frame_context.width = global_settings->frame_width; | |
1743 frame_context.height = global_settings->frame_height; | |
1744 frame_context.orig_height = global_settings->frame_height - global_settings->top_margin - global_settings->bottom_margin; | |
1745 frame_context.track = track; | |
1746 frame_context.add_bottom_margin = 0; | |
1747 frame_context.add_top_margin = 0; | |
1748 frame_context.time = now; | |
1749 | |
1750 ass_lazy_track_init(); | |
1751 | |
1752 if (frame_context.width * track->PlayResY == frame_context.height * track->PlayResX) | |
1753 frame_context.font_scale_x = 1.; | |
1754 else | |
1755 frame_context.font_scale_x = ((double)(frame_context.width * track->PlayResY)) / (frame_context.height * track->PlayResX); | |
1756 | |
1757 priv->n_images = 0; | |
1758 | |
1759 return 0; | |
1760 } | |
1761 | |
1762 /** | |
1763 * \brief End a frame, give out rendering results | |
1764 * \return list of ass_image_t | |
1765 */ | |
1766 ass_image_t* ass_end_frame(void) | |
1767 { | |
1768 ass_instance_t* priv = ass_instance; | |
1769 if (priv->n_images) { | |
1770 int i; | |
1771 for (i = 0; i < priv->n_images - 1; ++i) | |
1772 priv->images_root[i].next = priv->images_root + i + 1; | |
1773 priv->images_root[priv->n_images - 1].next = 0; | |
1774 return priv->images_root; | |
1775 } else { | |
1776 return 0; | |
1777 } | |
1778 } | |
1779 /** | |
1780 * \brief render a frame | |
1781 * \param priv library handle | |
1782 * \param track track | |
1783 * \param now current video timestamp (ms) | |
1784 */ | |
1785 ass_image_t* ass_render_frame(ass_instance_t *priv, ass_track_t* track, long long now) | |
1786 { | |
1787 int i, rc; | |
1788 | |
1789 rc = ass_start_frame(priv, track, now); | |
1790 if (rc != 0) // some error | |
1791 return 0; | |
1792 for (i = 0; i < track->n_events; ++i) { | |
1793 ass_event_t* event = track->events + i; | |
1794 if ( (event->Start <= now) && (now < (event->Start + event->Duration)) ) { | |
1795 ass_render_event(event); | |
1796 } | |
1797 } | |
1798 return ass_end_frame(); | |
1799 } | |
1800 |