Mercurial > mplayer.hg
annotate libass/ass_render.c @ 19303:d9e836da5093
Forgotten part of the LIVE555 configure test simplification.
author | diego |
---|---|
date | Thu, 03 Aug 2006 22:18:21 +0000 |
parents | 2ec2301183cd |
children | cd9ece839751 |
rev | line source |
---|---|
18937 | 1 #include "config.h" |
2 | |
3 #include <assert.h> | |
4 #include <math.h> | |
5 #include <stdint.h> | |
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 | |
481 static void change_font_size(int sz) | |
482 { | |
483 double size = (double)sz * global_settings->font_size_coeff * 0.8; | |
484 size *= frame_context.height; | |
485 size /= frame_context.track->PlayResY; | |
486 | |
487 if (size < 1) | |
488 size = 1; | |
489 else if (size > frame_context.height * 2) | |
490 size = frame_context.height * 2; | |
491 | |
492 FT_Set_Pixel_Sizes(render_context.face, 0, size); | |
493 | |
494 render_context.font_size = sz; | |
495 } | |
496 | |
497 /** | |
498 * \brief Change current font, using setting from render_context. | |
499 */ | |
500 static void update_font(void) | |
501 { | |
502 int error; | |
503 unsigned val; | |
504 ass_instance_t* priv = frame_context.ass_priv; | |
505 face_desc_t desc; | |
506 desc.family = strdup(render_context.family); | |
507 | |
508 val = render_context.bold; | |
509 // 0 = normal, 1 = bold, >1 = exact weight | |
510 if (val == 0) val = 80; // normal | |
511 else if (val == 1) val = 200; // bold | |
512 desc.bold = val; | |
513 | |
514 val = render_context.italic; | |
515 if (val == 0) val = 0; // normal | |
516 else if (val == 1) val = 110; //italic | |
517 desc.italic = val; | |
518 | |
519 error = ass_new_face(priv->library, priv->fontconfig_priv, &desc, &(render_context.face)); | |
520 if (error) { | |
521 render_context.face = 0; | |
522 } | |
523 | |
524 if (render_context.face) | |
525 { | |
526 change_font_size(render_context.font_size); | |
527 } | |
528 } | |
529 | |
530 /** | |
531 * \brief Change border width | |
532 */ | |
533 static void change_border(double border) | |
534 { | |
535 if (!render_context.stroker) { | |
536 if (!no_more_font_messages) | |
537 mp_msg(MSGT_GLOBAL, MSGL_WARN, "No stroker!\n"); | |
538 } else { | |
539 render_context.border = border; | |
540 FT_Stroker_Set( render_context.stroker, | |
541 (int)(64 * border), | |
542 FT_STROKER_LINECAP_ROUND, | |
543 FT_STROKER_LINEJOIN_ROUND, | |
544 0 ); | |
545 } | |
546 } | |
547 | |
548 #define _r(c) ((c)>>24) | |
549 #define _g(c) (((c)>>16)&0xFF) | |
550 #define _b(c) (((c)>>8)&0xFF) | |
551 #define _a(c) ((c)&0xFF) | |
552 | |
553 /** | |
554 * \brief Calculate a weighted average of two colors | |
555 * calculates c1*(1-a) + c2*a, but separately for each component except alpha | |
556 */ | |
557 static void change_color(uint32_t* var, uint32_t new, double pwr) | |
558 { | |
559 (*var)= ((uint32_t)(_r(*var) * (1 - pwr) + _r(new) * pwr) << 24) + | |
560 ((uint32_t)(_g(*var) * (1 - pwr) + _g(new) * pwr) << 16) + | |
561 ((uint32_t)(_b(*var) * (1 - pwr) + _b(new) * pwr) << 8) + | |
562 _a(*var); | |
563 } | |
564 | |
565 // like change_color, but for alpha component only | |
566 static void change_alpha(uint32_t* var, uint32_t new, double pwr) | |
567 { | |
568 *var = (_r(*var) << 24) + (_g(*var) << 16) + (_b(*var) << 8) + (_a(*var) * (1 - pwr) + _a(new) * pwr); | |
569 } | |
570 | |
571 | |
572 /** | |
573 * \brief Calculate alpha value by piecewise linear function | |
574 * Used for \fad, \fade implementation. | |
575 */ | |
576 static void interpolate_alpha(long long now, | |
577 long long t1, long long t2, long long t3, long long t4, | |
578 unsigned a1, unsigned a2, unsigned a3) | |
579 { | |
580 unsigned a; | |
581 double cf; | |
582 if (now <= t1) { | |
583 a = a1; | |
584 } else if (now >= t4) { | |
585 a = a3; | |
586 } else if (now < t2) { // and > t1 | |
587 cf = ((double)(now - t1)) / (t2 - t1); | |
588 a = a1 * (1 - cf) + a2 * cf; | |
589 } else if (now > t3) { | |
590 cf = ((double)(now - t3)) / (t4 - t3); | |
591 a = a2 * (1 - cf) + a3 * cf; | |
592 } else { // t2 <= now <= t3 | |
593 a = a2; | |
594 } | |
595 | |
596 | |
597 change_alpha(&render_context.c1, a, 1.); | |
598 change_alpha(&render_context.c2, a, 1.); | |
599 change_alpha(&render_context.c3, a, 1.); | |
600 change_alpha(&render_context.c4, a, 1.); | |
601 } | |
602 | |
603 /** | |
604 * \brief Parse style override tag. | |
605 * \param p string to parse | |
606 * \param pwr multiplier for some tag effects (comes from \t tags) | |
607 */ | |
608 static char* parse_tag(char* p, double pwr) { | |
609 #define skip_all(x) if (*p == (x)) ++p; else { \ | |
610 while ((*p != (x)) && (*p != '}') && (*p != 0)) {++p;} } | |
611 #define skip(x) if (*p == (x)) ++p; else { return p; } | |
612 | |
613 skip_all('\\'); | |
614 if ((*p == '}') || (*p == 0)) | |
615 return p; | |
616 | |
617 if (mystrcmp(&p, "fsc")) { | |
618 char tp = *p++; | |
619 double val; | |
620 if (tp == 'x') { | |
621 if (mystrtod(&p, &val)) { | |
622 val /= 100; | |
623 render_context.scale_x = (val - 1.) * pwr + 1.; | |
624 } else | |
625 render_context.scale_x = render_context.style->ScaleX; | |
626 } else if (tp == 'y') { | |
627 if (mystrtod(&p, &val)) { | |
628 val /= 100; | |
629 render_context.scale_y = (val - 1.) * pwr + 1.; | |
630 } else | |
631 render_context.scale_y = render_context.style->ScaleY; | |
632 } | |
633 } else if (mystrcmp(&p, "fsp")) { | |
634 int val; | |
635 if (mystrtoi(&p, 10, &val)) | |
636 render_context.hspacing = val * pwr; | |
637 else | |
638 render_context.hspacing = 0; | |
639 } else if (mystrcmp(&p, "fs")) { | |
640 int val; | |
641 if (mystrtoi(&p, 10, &val)) | |
642 val = render_context.font_size * ( 1 - pwr ) + val * pwr; | |
643 else | |
644 val = render_context.style->FontSize; | |
645 if (render_context.face) | |
646 change_font_size(val); | |
647 } else if (mystrcmp(&p, "bord")) { | |
648 double val; | |
649 if (mystrtod(&p, &val)) | |
650 val = render_context.border * ( 1 - pwr ) + val * pwr; | |
651 else | |
652 val = (render_context.style->BorderStyle == 1) ? render_context.style->Outline : 1.; | |
653 change_border(val); | |
654 } else if (mystrcmp(&p, "move")) { | |
655 int x1, x2, y1, y2; | |
19044 | 656 long long t1, t2, delta_t, t; |
18937 | 657 int x, y; |
658 double k; | |
659 skip('('); | |
660 x1 = strtol(p, &p, 10); | |
661 skip(','); | |
662 y1 = strtol(p, &p, 10); | |
663 skip(','); | |
664 x2 = strtol(p, &p, 10); | |
665 skip(','); | |
666 y2 = strtol(p, &p, 10); | |
19044 | 667 if (*p == ',') { |
668 skip(','); | |
669 t1 = strtoll(p, &p, 10); | |
670 skip(','); | |
671 t2 = strtoll(p, &p, 10); | |
672 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "movement6: (%d, %d) -> (%d, %d), (%lld .. %lld)\n", x1, y1, x2, y2, t1, t2); | |
673 } else { | |
674 t1 = 0; | |
675 t2 = render_context.event->Duration; | |
676 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "movement: (%d, %d) -> (%d, %d)\n", x1, y1, x2, y2); | |
677 } | |
678 skip(')'); | |
679 delta_t = t2 - t1; | |
680 t = frame_context.time - render_context.event->Start; | |
681 if (t < t1) | |
682 k = 0.; | |
683 else if (t > t2) | |
684 k = 1.; | |
685 else k = ((double)(t - t1)) / delta_t; | |
18937 | 686 x = k * (x2 - x1) + x1; |
687 y = k * (y2 - y1) + y1; | |
688 render_context.pos_x = x; | |
689 render_context.pos_y = y; | |
690 render_context.detect_collisions = 0; | |
691 render_context.evt_type = EVENT_POSITIONED; | |
692 } else if (mystrcmp(&p, "frx") || mystrcmp(&p, "fry")) { | |
693 int val; | |
694 mystrtoi(&p, 10, &val); | |
695 mp_msg(MSGT_GLOBAL, MSGL_V, "frx/fry unimplemented \n"); | |
696 } else if (mystrcmp(&p, "frz") || mystrcmp(&p, "fr")) { | |
697 double angle; | |
698 int val; | |
699 val = strtol(p, &p, 10); | |
700 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "setting rotation to %.2f\n", val * pwr); | |
701 angle = M_PI * val / 180; | |
702 render_context.rotation = angle * pwr; | |
703 } else if (mystrcmp(&p, "fn")) { | |
704 char* start = p; | |
705 char* family; | |
706 skip_all('\\'); | |
707 family = malloc(p - start + 1); | |
708 strncpy(family, start, p - start); | |
709 family[p - start] = '\0'; | |
710 if (render_context.family) | |
711 free(render_context.family); | |
712 render_context.family = family; | |
713 update_font(); | |
714 } else if (mystrcmp(&p, "alpha")) { | |
715 uint32_t val; | |
716 if (strtocolor(&p, &val)) { | |
717 unsigned char a = val >> 24; | |
718 change_alpha(&render_context.c1, a, pwr); | |
719 change_alpha(&render_context.c2, a, pwr); | |
720 change_alpha(&render_context.c3, a, pwr); | |
721 change_alpha(&render_context.c4, a, pwr); | |
722 } else { | |
723 change_alpha(&render_context.c1, render_context.style->PrimaryColour, pwr); | |
724 change_alpha(&render_context.c2, render_context.style->SecondaryColour, pwr); | |
725 change_alpha(&render_context.c3, render_context.style->OutlineColour, pwr); | |
726 change_alpha(&render_context.c4, render_context.style->BackColour, pwr); | |
727 } | |
728 // FIXME: simplify | |
729 } else if (mystrcmp(&p, "an")) { | |
730 int val = strtol(p, &p, 10); | |
731 int v = (val - 1) / 3; // 0, 1 or 2 for vertical alignment | |
732 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "an %d\n", val); | |
733 if (v != 0) v = 3 - v; | |
734 val = ((val - 1) % 3) + 1; // horizontal alignment | |
735 val += v*4; | |
736 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "align %d\n", val); | |
737 render_context.alignment = val; | |
738 } else if (mystrcmp(&p, "a")) { | |
739 int val = strtol(p, &p, 10); | |
740 render_context.alignment = val; | |
741 } else if (mystrcmp(&p, "pos")) { | |
742 int v1, v2; | |
743 skip('('); | |
744 v1 = strtol(p, &p, 10); | |
745 skip(','); | |
746 v2 = strtol(p, &p, 10); | |
747 skip(')'); | |
748 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "pos(%d, %d)\n", v1, v2); | |
749 render_context.evt_type = EVENT_POSITIONED; | |
750 render_context.detect_collisions = 0; | |
751 render_context.pos_x = v1; | |
752 render_context.pos_y = v2; | |
753 } else if (mystrcmp(&p, "fade")) { | |
754 int a1, a2, a3, v1, v2, v3, v4; | |
755 skip('('); | |
756 a1 = strtol(p, &p, 10); | |
757 skip(','); | |
758 a2 = strtol(p, &p, 10); | |
759 skip(','); | |
760 a3 = strtol(p, &p, 10); | |
761 skip(','); | |
762 v1 = strtol(p, &p, 10); | |
763 skip(','); | |
764 v2 = strtol(p, &p, 10); | |
765 skip(','); | |
766 v3 = strtol(p, &p, 10); | |
767 skip(','); | |
768 v4 = strtol(p, &p, 10); | |
769 skip(')'); | |
770 interpolate_alpha(frame_context.time - render_context.event->Start, v1, v2, v3, v4, a1, a2, a3); | |
771 } else if (mystrcmp(&p, "fad")) { | |
772 int v1, v2; | |
773 long long now, t1, t2, t3, t4; | |
774 skip('('); | |
775 v1 = strtol(p, &p, 10); | |
776 skip(','); | |
777 v2 = strtol(p, &p, 10); | |
778 skip(')'); | |
779 now = frame_context.time; | |
780 t1 = render_context.event->Start; | |
781 t2 = t1 + v1; | |
782 t4 = render_context.event->Start + render_context.event->Duration; | |
783 t3 = t4 - v2; | |
784 interpolate_alpha(now, t1, t2, t3, t4, 0xFF, 0, 0xFF); | |
785 } else if (mystrcmp(&p, "org")) { | |
786 int v1, v2; | |
787 skip('('); | |
788 v1 = strtol(p, &p, 10); | |
789 skip(','); | |
790 v2 = strtol(p, &p, 10); | |
791 skip(')'); | |
792 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "org(%d, %d)\n", v1, v2); | |
793 // render_context.evt_type = EVENT_POSITIONED; | |
794 render_context.org_x = v1; | |
795 render_context.org_y = v2; | |
796 } else if (mystrcmp(&p, "t")) { | |
797 double v[3]; | |
798 int v1, v2; | |
799 double v3; | |
800 int cnt; | |
801 long long t1, t2, t, delta_t; | |
802 double k; | |
803 skip('('); | |
804 for (cnt = 0; cnt < 3; ++cnt) { | |
805 if (*p == '\\') | |
806 break; | |
807 v[cnt] = strtod(p, &p); | |
808 skip(','); | |
809 } | |
810 if (cnt == 3) { | |
811 v1 = v[0]; v2 = v[1]; v3 = v[2]; | |
812 } else if (cnt == 2) { | |
813 v1 = v[0]; v2 = v[1]; v3 = 1.; | |
814 } else if (cnt == 1) { | |
815 v1 = 0; v2 = render_context.event->Duration; v3 = v[0]; | |
816 } else { // cnt == 0 | |
817 v1 = 0; v2 = render_context.event->Duration; v3 = 1.; | |
818 } | |
819 render_context.detect_collisions = 0; | |
820 t1 = v1; | |
821 t2 = v2; | |
822 delta_t = v2 - v1; | |
823 t = frame_context.time - render_context.event->Start; // FIXME: move to render_context | |
824 if (t < t1) | |
825 k = 0.; | |
826 else if (t > t2) | |
827 k = 1.; | |
828 else k = ((double)(t - t1)) / delta_t; | |
829 while (*p == '\\') | |
830 p = parse_tag(p, k); // maybe k*pwr ? no, specs forbid nested \t's | |
831 skip_all(')'); // FIXME: better skip(')'), but much more tags support required | |
832 } else if (mystrcmp(&p, "clip")) { | |
833 int x0, y0, x1, y1; | |
834 int res = 1; | |
835 skip('('); | |
836 res &= mystrtoi(&p, 10, &x0); | |
837 skip(','); | |
838 res &= mystrtoi(&p, 10, &y0); | |
839 skip(','); | |
840 res &= mystrtoi(&p, 10, &x1); | |
841 skip(','); | |
842 res &= mystrtoi(&p, 10, &y1); | |
843 skip(')'); | |
844 if (res) { | |
845 render_context.clip_x0 = render_context.clip_x0 * (1-pwr) + x0 * pwr; | |
846 render_context.clip_x1 = render_context.clip_x1 * (1-pwr) + x1 * pwr; | |
847 render_context.clip_y0 = render_context.clip_y0 * (1-pwr) + y0 * pwr; | |
848 render_context.clip_y1 = render_context.clip_y1 * (1-pwr) + y1 * pwr; | |
849 } else { | |
850 render_context.clip_x0 = 0; | |
851 render_context.clip_y0 = 0; | |
852 render_context.clip_x1 = frame_context.track->PlayResX; | |
853 render_context.clip_y1 = frame_context.track->PlayResY; | |
854 } | |
855 } else if (mystrcmp(&p, "c")) { | |
856 uint32_t val; | |
857 if (!strtocolor(&p, &val)) | |
858 val = render_context.style->PrimaryColour; | |
859 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "color: %X\n", val); | |
860 change_color(&render_context.c1, val, pwr); | |
861 } else if ((*p >= '1') && (*p <= '4') && (++p) && (mystrcmp(&p, "c") || mystrcmp(&p, "a"))) { | |
862 char n = *(p-2); | |
863 char cmd = *(p-1); | |
864 uint32_t* pcolor; | |
865 uint32_t val; | |
866 assert((n >= '1') && (n <= '4')); | |
867 if (!strtocolor(&p, &val)) | |
868 switch(n) { | |
869 case '1': val = render_context.style->PrimaryColour; break; | |
870 case '2': val = render_context.style->SecondaryColour; break; | |
871 case '3': val = render_context.style->OutlineColour; break; | |
872 case '4': val = render_context.style->BackColour; break; | |
873 default : val = 0; break; // impossible due to assert; avoid compilation warning | |
874 } | |
875 switch (n) { | |
876 case '1': pcolor = &render_context.c1; break; | |
877 case '2': pcolor = &render_context.c2; break; | |
878 case '3': pcolor = &render_context.c3; break; | |
879 case '4': pcolor = &render_context.c4; break; | |
880 default : pcolor = 0; break; | |
881 } | |
882 switch (cmd) { | |
883 case 'c': change_color(pcolor, val, pwr); break; | |
884 case 'a': change_alpha(pcolor, val >> 24, pwr); break; | |
885 default: mp_msg(MSGT_GLOBAL, MSGL_WARN, "Bad command: %c%c\n", n, cmd); break; | |
886 } | |
887 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "single c/a at %f: %c%c = %X \n", pwr, n, cmd, *pcolor); | |
888 } else if (mystrcmp(&p, "r")) { | |
889 render_context.c1 = render_context.style->PrimaryColour; | |
890 render_context.c2 = render_context.style->SecondaryColour; | |
891 render_context.c3 = render_context.style->OutlineColour; | |
892 render_context.c4 = render_context.style->BackColour; | |
893 render_context.font_size = render_context.style->FontSize; | |
894 | |
895 if (render_context.family) | |
896 free(render_context.family); | |
897 render_context.family = strdup(render_context.style->FontName); | |
898 render_context.bold = - render_context.style->Bold; | |
899 render_context.italic = - render_context.style->Italic; | |
900 update_font(); | |
901 | |
902 if (render_context.stroker) { | |
903 double border = (render_context.style->BorderStyle == 1) ? render_context.style->Outline : 1.; | |
904 change_border(border); | |
905 } | |
906 render_context.scale_x = render_context.style->ScaleX; | |
907 render_context.scale_y = render_context.style->ScaleY; | |
908 render_context.hspacing = 0; // FIXME | |
909 | |
910 // FIXME: does not reset unsupported attributes. | |
911 } else if (mystrcmp(&p, "be")) { | |
912 int val; | |
913 mystrtoi(&p, 10, &val); | |
914 mp_msg(MSGT_GLOBAL, MSGL_V, "be unimplemented \n"); | |
915 } else if (mystrcmp(&p, "b")) { | |
916 int b; | |
917 if (mystrtoi(&p, 10, &b)) | |
918 render_context.bold = b; | |
919 else | |
920 render_context.bold = - render_context.style->Bold; | |
921 update_font(); | |
922 } else if (mystrcmp(&p, "i")) { | |
923 int i; | |
924 if (mystrtoi(&p, 10, &i)) | |
925 render_context.italic = i; | |
926 else | |
927 render_context.italic = - render_context.style->Italic; | |
928 update_font(); | |
929 } else if (mystrcmp(&p, "kf") || mystrcmp(&p, "K")) { | |
930 int val = strtol(p, &p, 10); | |
931 render_context.effect_type = EF_KARAOKE_KF; | |
932 render_context.effect_timing = val * 10; | |
933 } else if (mystrcmp(&p, "ko")) { | |
934 int val = strtol(p, &p, 10); | |
935 render_context.effect_type = EF_KARAOKE_KO; | |
936 render_context.effect_timing = val * 10; | |
937 } else if (mystrcmp(&p, "k")) { | |
938 int val = strtol(p, &p, 10); | |
939 render_context.effect_type = EF_KARAOKE; | |
940 render_context.effect_timing = val * 10; | |
941 } | |
942 | |
943 return p; | |
944 | |
945 #undef skip | |
946 #undef skip_all | |
947 } | |
948 | |
949 /** | |
950 * \brief Get next ucs4 char from string, parsing and executing style overrides | |
951 * \param str string pointer | |
952 * \return ucs4 code of the next char | |
953 * On return str points to the unparsed part of the string | |
954 */ | |
955 static unsigned get_next_char(char** str) | |
956 { | |
957 char* p = *str; | |
958 unsigned chr; | |
959 if (*p == '{') { // '\0' goes here | |
960 p++; | |
961 while (1) { | |
962 p = parse_tag(p, 1.); | |
963 if (*p == '}') { // end of tag | |
964 p++; | |
965 if (*p == '{') { | |
966 p++; | |
967 continue; | |
968 } else | |
969 break; | |
970 } else if (*p != '\\') | |
971 mp_msg(MSGT_GLOBAL, MSGL_V, "Unable to parse: \"%s\" \n", p); | |
972 if (*p == 0) | |
973 break; | |
974 } | |
975 } | |
976 if (*p == '\t') { | |
977 ++p; | |
978 *str = p; | |
979 return ' '; | |
980 } | |
981 if (*p == '\\') { | |
982 if ((*(p+1) == 'N') || ((*(p+1) == 'n') && (frame_context.track->WrapStyle == 2))) { | |
983 p += 2; | |
984 *str = p; | |
985 return '\n'; | |
986 } else if (*(p+1) == 'n') { | |
987 p += 2; | |
988 *str = p; | |
989 return ' '; | |
990 } | |
991 } | |
992 chr = utf8_get_char(&p); | |
993 *str = p; | |
994 return chr; | |
995 } | |
996 | |
997 /** | |
998 * \brief Start new event. Reset render_context. | |
999 */ | |
1000 static int init_render_context(ass_event_t* event) | |
1001 { | |
1002 int error; | |
1003 | |
1004 // init render_context | |
1005 render_context.event = event; | |
1006 render_context.style = frame_context.track->styles + event->Style; | |
1007 | |
1008 render_context.font_size = render_context.style->FontSize; | |
1009 render_context.evt_type = EVENT_NORMAL; | |
1010 render_context.alignment = 0; | |
1011 render_context.rotation = 0.; | |
1012 render_context.pos_x = 0; | |
1013 render_context.pos_y = 0; | |
1014 render_context.org_x = 0; | |
1015 render_context.org_y = 0; | |
1016 render_context.scale_x = render_context.style->ScaleX; | |
1017 render_context.scale_y = render_context.style->ScaleY; | |
1018 render_context.hspacing = 0; | |
1019 render_context.c1 = render_context.style->PrimaryColour; | |
1020 render_context.c2 = render_context.style->SecondaryColour; | |
1021 render_context.c3 = render_context.style->OutlineColour; | |
1022 render_context.c4 = render_context.style->BackColour; | |
1023 render_context.clip_x0 = 0; | |
1024 render_context.clip_y0 = 0; | |
1025 render_context.clip_x1 = frame_context.track->PlayResX; | |
1026 render_context.clip_y1 = frame_context.track->PlayResY; | |
1027 render_context.detect_collisions = 1; | |
1028 | |
1029 if (render_context.family) | |
1030 free(render_context.family); | |
1031 render_context.family = strdup(render_context.style->FontName); | |
1032 render_context.bold = - render_context.style->Bold; // style value for bold text is -1 | |
1033 render_context.italic = - render_context.style->Italic; | |
1034 | |
1035 update_font(); | |
1036 | |
1037 if (render_context.face) { | |
1038 #if (FREETYPE_MAJOR > 2) || ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR > 1)) | |
1039 error = FT_Stroker_New( ass_instance->library, &render_context.stroker ); | |
1040 #else // < 2.2 | |
1041 error = FT_Stroker_New( render_context.face->memory, &render_context.stroker ); | |
1042 #endif | |
1043 if ( error ) { | |
1044 mp_msg(MSGT_GLOBAL, MSGL_V, "failed to get stroker\n"); | |
1045 render_context.stroker = 0; | |
1046 } else { | |
1047 // FIXME: probably wrong when render_context.Border == 3 | |
1048 double border = (render_context.style->BorderStyle == 1) ? render_context.style->Outline : 1.; | |
1049 change_border(border); | |
1050 } | |
1051 } | |
1052 | |
1053 return 0; | |
1054 } | |
1055 | |
1056 static int free_render_context(void) | |
1057 { | |
1058 if (render_context.stroker != 0) { | |
1059 FT_Stroker_Done(render_context.stroker); | |
1060 render_context.stroker = 0; | |
1061 } | |
1062 return 0; | |
1063 } | |
1064 | |
1065 /** | |
1066 * \brief Get normal and outline glyphs from cache (if possible) or font face | |
1067 * \param index face glyph index | |
1068 * \param symbol ucs4 char | |
1069 * \param info out: struct filled with extracted data | |
1070 * \param advance advance vector of the extracted glyph | |
1071 * \return 0 on success | |
1072 */ | |
1073 static int get_glyph(int index, int symbol, glyph_info_t* info, FT_Vector* advance) | |
1074 { | |
1075 int error; | |
1076 glyph_hash_val_t* val; | |
1077 glyph_hash_key_t* key = &(info->hash_key); | |
1078 | |
1079 key->face = render_context.face; | |
1080 key->size = render_context.font_size; | |
1081 key->index = index; | |
1082 key->outline = (render_context.border * 0xFFFF); // convert to 16.16 | |
1083 key->scale_x = (render_context.scale_x * 0xFFFF); | |
1084 key->scale_y = (render_context.scale_y * 0xFFFF); | |
1085 key->angle = (render_context.rotation * 0xFFFF); | |
1086 key->advance = *advance; | |
1087 key->bold = render_context.bold; | |
1088 key->italic = render_context.italic; | |
1089 | |
1090 key->bitmap = 1; // looking for bitmap glyph | |
1091 | |
1092 | |
1093 val = cache_find_glyph(key); | |
1094 // val = 0; | |
1095 | |
1096 if (val) { | |
1097 // bitmap glyph found, no need for FT_Glyph_Copy | |
1098 info->glyph = val->glyph; | |
1099 info->outline_glyph = val->outline_glyph; | |
1100 info->bbox = val->bbox_scaled; | |
1101 info->advance.x = val->advance.x; | |
1102 info->advance.y = val->advance.y; | |
1103 info->bitmap = 1; // bitmap glyph | |
1104 | |
1105 return 0; | |
1106 } | |
1107 | |
1108 // not found, get a new outline glyph from face | |
1109 // mp_msg(MSGT_GLOBAL, MSGL_INFO, "miss, index = %d, symbol = %c, adv = (%d, %d)\n", index, symbol, advance->x, advance->y); | |
1110 | |
1111 error = FT_Load_Glyph(render_context.face, index, FT_LOAD_NO_BITMAP ); | |
1112 if (error) { | |
1113 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Error loading glyph\n"); | |
1114 return error; | |
1115 } | |
1116 | |
1117 #if (FREETYPE_MAJOR > 2) || \ | |
1118 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR >= 2)) || \ | |
1119 ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR == 1) && (FREETYPE_PATCH >= 10)) | |
1120 // FreeType >= 2.1.10 required | |
1121 if (!(render_context.face->style_flags & FT_STYLE_FLAG_ITALIC) && | |
1122 ((render_context.italic == 1) || (render_context.italic > 55))) { | |
1123 FT_GlyphSlot_Oblique(render_context.face->glyph); | |
1124 } | |
1125 #endif | |
1126 error = FT_Get_Glyph(render_context.face->glyph, &(info->glyph)); | |
1127 if (error) { | |
1128 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Error getting glyph\n"); | |
1129 return error; | |
1130 } | |
1131 | |
1132 info->advance.x = info->glyph->advance.x >> 10; | |
1133 info->advance.y = info->glyph->advance.y >> 10; | |
1134 | |
1135 info->outline_glyph = info->glyph; | |
19002
da05237044c2
Ignoring FT_Glyph_Stroke() errors can potentially lead to double free().
eugeni
parents:
18937
diff
changeset
|
1136 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
|
1137 if (error) { |
da05237044c2
Ignoring FT_Glyph_Stroke() errors can potentially lead to double free().
eugeni
parents:
18937
diff
changeset
|
1138 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
|
1139 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
|
1140 } |
18937 | 1141 |
1142 info->bitmap = 0; // outline glyph | |
1143 | |
1144 return 0; | |
1145 } | |
1146 | |
1147 /** | |
1148 * \brief rearrange text between lines | |
1149 * \param max_text_width maximal text line width in pixels | |
1150 * The algo is similar to the one in libvo/sub.c: | |
1151 * 1. Place text, wrapping it when current line is full | |
1152 * 2. Try moving words from the end of a line to the beginning of the next one while it reduces | |
1153 * the difference in lengths between this two lines. | |
1154 * The result may not be optimal, but usually is good enough. | |
1155 */ | |
1156 static void wrap_lines_smart(int max_text_width) | |
1157 { | |
1158 int i, j; | |
1159 glyph_info_t *cur, *s1, *e1, *s2, *s3, *w; | |
1160 int last_space; | |
1161 int break_type; | |
1162 int exit; | |
1163 int pen_shift_x; | |
1164 int pen_shift_y; | |
1165 int max_asc, max_desc; | |
1166 int cur_line; | |
1167 | |
1168 last_space = -1; | |
1169 text_info.n_lines = 1; | |
1170 break_type = 0; | |
1171 s1 = text_info.glyphs; // current line start | |
1172 for (i = 0; i < text_info.length; ++i) { | |
1173 cur = text_info.glyphs + i; | |
1174 int break_at = -1; | |
1175 int s_offset = s1->bbox.xMin + s1->pos.x; | |
1176 int len = (cur->bbox.xMax + cur->pos.x) - s_offset; | |
1177 | |
1178 if (cur->symbol == '\n') { | |
1179 break_type = 2; | |
1180 break_at = i; | |
1181 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "forced line break at %d\n", break_at); | |
1182 } | |
1183 | |
1184 if (len >= max_text_width) { | |
1185 break_type = 1; | |
1186 break_at = last_space; | |
1187 if (break_at == -1) | |
1188 break_at = i - 1; | |
1189 if (break_at == -1) | |
1190 break_at = 0; | |
1191 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "overfill at %d\n", i); | |
1192 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "line break at %d\n", break_at); | |
1193 } | |
1194 | |
1195 if (break_at != -1) { | |
1196 // need to use one more line | |
1197 // marking break_at+1 as start of a new line | |
1198 int lead = break_at + 1; // the first symbol of the new line | |
1199 if (text_info.n_lines >= MAX_LINES) { | |
1200 // to many lines ! | |
1201 // no more linebreaks | |
1202 for (j = lead; j < text_info.length; ++j) | |
1203 text_info.glyphs[j].linebreak = 0; | |
1204 break; | |
1205 } | |
1206 if (lead < text_info.length) | |
1207 text_info.glyphs[lead].linebreak = break_type; | |
1208 last_space = -1; | |
1209 s1 = text_info.glyphs + lead; | |
1210 s_offset = s1->bbox.xMin + s1->pos.x; | |
1211 text_info.n_lines ++; | |
1212 } | |
1213 | |
1214 if (cur->symbol == ' ') | |
1215 last_space = i; | |
1216 } | |
1217 #define DIFF(x,y) (((x) < (y)) ? (y - x) : (x - y)) | |
1218 exit = 0; | |
1219 while (!exit) { | |
1220 exit = 1; | |
1221 w = s3 = text_info.glyphs; | |
1222 s1 = s2 = 0; | |
1223 for (i = 0; i <= text_info.length; ++i) { | |
1224 cur = text_info.glyphs + i; | |
1225 if ((i == text_info.length) || cur->linebreak) { | |
1226 s1 = s2; | |
1227 s2 = s3; | |
1228 s3 = cur; | |
1229 // printf("line: %d, %d, %d \n", s1 - text_info.glyphs, s2 - text_info.glyphs, s3 - text_info.glyphs); | |
1230 if (s1 && (s2->linebreak == 1)) { // have at least 2 lines, and linebreak is 'soft' | |
1231 int l1, l2, l1_new, l2_new; | |
1232 | |
1233 w = s2; | |
1234 do { --w; } while ((w > s1) && (w->symbol == ' ')); | |
1235 do { --w; } while ((w > s1) && (w->symbol != ' ')); | |
1236 e1 = w; | |
1237 do { --e1; } while ((e1 > s1) && (e1->symbol == ' ')); | |
1238 if (w->symbol == ' ') ++w; | |
1239 | |
1240 // printf("check: %d, %d, %d, w: %d, e: %d \n", s1 - text_info.glyphs, s2 - text_info.glyphs, s3 - text_info.glyphs, | |
1241 // w - text_info.glyphs, e1 - text_info.glyphs); | |
1242 | |
1243 l1 = ((s2-1)->bbox.xMax + (s2-1)->pos.x) - (s1->bbox.xMin + s1->pos.x); | |
1244 l2 = ((s3-1)->bbox.xMax + (s3-1)->pos.x) - (s2->bbox.xMin + s2->pos.x); | |
1245 l1_new = (e1->bbox.xMax + e1->pos.x) - (s1->bbox.xMin + s1->pos.x); | |
1246 l2_new = ((s3-1)->bbox.xMax + (s3-1)->pos.x) - (w->bbox.xMin + w->pos.x); | |
1247 | |
1248 if (DIFF(l1_new, l2_new) < DIFF(l1, l2)) { | |
1249 w->linebreak = 1; | |
1250 s2->linebreak = 0; | |
1251 exit = 0; | |
1252 } | |
1253 } | |
1254 } | |
1255 if (i == text_info.length) | |
1256 break; | |
1257 } | |
1258 | |
1259 } | |
1260 assert(text_info.n_lines >= 1); | |
1261 #undef DIFF | |
1262 | |
1263 text_info.height = 0; | |
1264 max_asc = max_desc = 0; | |
1265 cur_line = 0; | |
1266 for (i = 0; i < text_info.length + 1; ++i) { | |
1267 if ((i == text_info.length) || text_info.glyphs[i].linebreak) { | |
1268 text_info.lines[cur_line].asc = max_asc; | |
1269 text_info.lines[cur_line].desc = max_desc; | |
1270 text_info.height += max_asc + max_desc; | |
1271 cur_line ++; | |
1272 max_asc = max_desc = 0; | |
1273 } | |
1274 if (i < text_info.length) { | |
1275 cur = text_info.glyphs + i; | |
1276 if (cur->asc > max_asc) | |
1277 max_asc = cur->asc * render_context.scale_y; | |
1278 if (cur->desc > max_desc) | |
1279 max_desc = cur->desc * render_context.scale_y; | |
1280 } | |
1281 } | |
1282 | |
1283 pen_shift_x = 0; | |
1284 pen_shift_y = 0; | |
1285 cur_line = 1; | |
1286 for (i = 0; i < text_info.length; ++i) { | |
1287 cur = text_info.glyphs + i; | |
1288 if (cur->linebreak) { | |
1289 int height = text_info.lines[cur_line - 1].desc + text_info.lines[cur_line].asc; | |
1290 cur_line ++; | |
1291 pen_shift_x = - cur->pos.x; | |
1292 pen_shift_y += (height >> 6) + global_settings->line_spacing; | |
1293 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); | |
1294 } | |
1295 cur->pos.x += pen_shift_x; | |
1296 cur->pos.y += pen_shift_y; | |
1297 } | |
1298 // printf("============= \n"); | |
1299 } | |
1300 | |
1301 /** | |
1302 * \brief determine karaoke effects | |
1303 * Karaoke effects cannot be calculated during parse stage (get_next_char()), | |
1304 * so they are done in a separate step. | |
1305 * Parse stage: when karaoke style override is found, its parameters are stored in the next glyph's | |
1306 * (the first glyph of the karaoke word)'s effect_type and effect_timing. | |
1307 * This function: | |
1308 * 1. sets effect_type for all glyphs in the word (_karaoke_ word) | |
1309 * 2. sets effect_timing for all glyphs to x coordinate of the border line between the left and right karaoke parts | |
1310 * (left part is filled with PrimaryColour, right one - with SecondaryColour). | |
1311 */ | |
1312 static void process_karaoke_effects(void) | |
1313 { | |
1314 glyph_info_t *cur, *cur2; | |
1315 glyph_info_t *s1, *e1; // start and end of the current word | |
1316 glyph_info_t *s2; // start of the next word | |
1317 int i; | |
1318 int timing; // current timing | |
1319 int tm_start, tm_end; // timings at start and end of the current word | |
1320 int tm_current; | |
1321 double dt; | |
1322 int x; | |
1323 int x_start, x_end; | |
1324 | |
1325 tm_current = frame_context.time - render_context.event->Start; | |
1326 timing = 0; | |
1327 s1 = s2 = 0; | |
1328 for (i = 0; i <= text_info.length; ++i) { | |
1329 cur = text_info.glyphs + i; | |
1330 if ((i == text_info.length) || (cur->effect_type != EF_NONE)) { | |
1331 s1 = s2; | |
1332 s2 = cur; | |
1333 if (s1) { | |
1334 e1 = s2 - 1; | |
1335 tm_start = timing; | |
1336 tm_end = timing + s1->effect_timing; | |
1337 timing = tm_end; | |
1338 x_start = s1->bbox.xMin + s1->pos.x; | |
1339 x_end = e1->bbox.xMax + e1->pos.x; | |
1340 | |
1341 dt = (tm_current - tm_start); | |
1342 if ((s1->effect_type == EF_KARAOKE) || (s1->effect_type == EF_KARAOKE_KO)) { | |
1343 if (dt > 0) | |
1344 x = x_end + 1; | |
1345 else | |
1346 x = x_start; | |
1347 } else if (s1->effect_type == EF_KARAOKE_KF) { | |
1348 dt /= (tm_end - tm_start); | |
1349 x = x_start + (x_end - x_start) * dt; | |
1350 } else { | |
1351 mp_msg(MSGT_GLOBAL, MSGL_ERR, "Unknown effect type (internal error) \n"); | |
1352 continue; | |
1353 } | |
1354 | |
1355 for (cur2 = s1; cur2 <= e1; ++cur2) { | |
1356 cur2->effect_type = s1->effect_type; | |
1357 cur2->effect_timing = x - cur2->pos.x; | |
1358 } | |
1359 } | |
1360 } | |
1361 } | |
1362 } | |
1363 | |
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
|
1364 static int get_face_ascender(FT_Face face) |
18937 | 1365 { |
1366 int v = face->size->metrics.ascender; | |
1367 if (!v) | |
1368 v = FT_MulFix(face->bbox.yMax, face->size->metrics.y_scale); | |
1369 return v; | |
1370 } | |
1371 | |
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
|
1372 static int get_face_descender(FT_Face face) |
18937 | 1373 { |
1374 int v = face->size->metrics.descender; | |
1375 if (!v) | |
1376 v = FT_MulFix(face->bbox.yMin, face->size->metrics.y_scale); | |
1377 return -v; | |
1378 } | |
1379 | |
1380 /** | |
1381 * \brief Main ass rendering function, glues everything together | |
1382 * \param event event to render | |
1383 * Process event, appending resulting ass_image_t's to images_root. | |
1384 */ | |
1385 int ass_render_event(ass_event_t* event) | |
1386 { | |
1387 char* p; | |
1388 FT_UInt glyph_index; | |
1389 FT_Bool use_kerning; | |
1390 FT_UInt previous; | |
1391 FT_UInt num_glyphs; | |
1392 FT_Vector pen; | |
1393 int error; | |
1394 unsigned code; | |
1395 FT_BBox bbox; | |
1396 int i, j; | |
1397 FT_Vector shift; | |
1398 int MarginL, MarginR, MarginV; | |
1399 int max_text_width; | |
1400 ass_style_t* style = frame_context.track->styles + event->Style; | |
1401 int last_break; | |
1402 int alignment, halign, valign; | |
1403 int device_x, device_y; | |
1404 | |
1405 init_render_context(event); | |
1406 | |
1407 text_info.length = 0; | |
1408 pen.x = 0; | |
1409 pen.y = 0; | |
1410 previous = 0; | |
1411 num_glyphs = 0; | |
1412 | |
1413 | |
1414 p = event->Text; | |
1415 if (!p) { | |
1416 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Empty event!\n"); | |
1417 return 0; | |
1418 } | |
1419 | |
1420 // Event parsing. | |
1421 while (1) { | |
1422 render_context.effect_type = EF_NONE; | |
1423 | |
1424 // get next char, executing style override | |
1425 // this affects render_context | |
1426 code = get_next_char(&p); | |
1427 | |
1428 // face could have been changed in get_next_char | |
1429 if (!render_context.face) { | |
1430 free_render_context(); | |
1431 return 0; | |
1432 } | |
1433 | |
1434 if (code == 0) | |
1435 break; | |
1436 | |
1437 use_kerning = FT_HAS_KERNING(render_context.face); | |
1438 | |
1439 if (text_info.length >= MAX_GLYPHS) { | |
1440 mp_msg(MSGT_GLOBAL, MSGL_WARN, "\nMAX_GLYPHS reached: event %d, start = %llu, duration = %llu\n Text = %s\n", | |
1441 (int)(event - frame_context.track->events), event->Start, event->Duration, event->Text); | |
1442 break; | |
1443 } | |
1444 | |
1445 glyph_index = FT_Get_Char_Index( render_context.face, code); | |
1446 | |
1447 if ( use_kerning && previous && glyph_index ) { | |
1448 FT_Vector delta; | |
1449 FT_Get_Kerning( render_context.face, previous, glyph_index, FT_KERNING_DEFAULT, &delta ); | |
1450 pen.x += delta.x; | |
1451 pen.y += delta.y; | |
1452 } | |
1453 | |
1454 shift.x = pen.x & 63; | |
1455 shift.y = pen.y & 63; | |
1456 | |
1457 if ((render_context.scale_x != 1.) || (render_context.scale_y != 1.) || | |
1458 (frame_context.font_scale_x != 1.)) { | |
1459 FT_Matrix matrix; | |
1460 matrix.xx = (FT_Fixed)( render_context.scale_x * frame_context.font_scale_x * 0x10000L ); | |
1461 matrix.xy = (FT_Fixed)( 0 * 0x10000L ); | |
1462 matrix.yx = (FT_Fixed)( 0 * 0x10000L ); | |
1463 matrix.yy = (FT_Fixed)( render_context.scale_y * 0x10000L ); | |
1464 | |
1465 FT_Set_Transform( render_context.face, &matrix, &shift ); | |
1466 } else { | |
1467 FT_Set_Transform(render_context.face, 0, &shift); | |
1468 } | |
1469 | |
1470 error = get_glyph(glyph_index, code, text_info.glyphs + text_info.length, &shift); | |
1471 | |
1472 if (error) { | |
1473 continue; | |
1474 } | |
1475 | |
1476 text_info.glyphs[text_info.length].pos.x = pen.x >> 6; | |
1477 text_info.glyphs[text_info.length].pos.y = pen.y >> 6; | |
1478 | |
1479 pen.x += text_info.glyphs[text_info.length].advance.x; | |
1480 pen.x += render_context.hspacing; | |
1481 pen.y += text_info.glyphs[text_info.length].advance.y; | |
1482 | |
1483 // if it's an outline glyph, we still need to fill the bbox | |
1484 if (text_info.glyphs[text_info.length].bitmap != 1) { | |
1485 FT_Glyph_Get_CBox( text_info.glyphs[text_info.length].glyph, FT_GLYPH_BBOX_PIXELS, &(text_info.glyphs[text_info.length].bbox) ); | |
1486 } | |
1487 | |
1488 | |
1489 previous = glyph_index; | |
1490 | |
1491 text_info.glyphs[text_info.length].symbol = code; | |
1492 text_info.glyphs[text_info.length].linebreak = 0; | |
1493 text_info.glyphs[text_info.length].c1 = render_context.c1; | |
1494 text_info.glyphs[text_info.length].c2 = render_context.c2; | |
1495 text_info.glyphs[text_info.length].c3 = render_context.c3; | |
1496 text_info.glyphs[text_info.length].c4 = render_context.c4; | |
1497 text_info.glyphs[text_info.length].effect_type = render_context.effect_type; | |
1498 text_info.glyphs[text_info.length].effect_timing = render_context.effect_timing; | |
1499 text_info.glyphs[text_info.length].asc = get_face_ascender(render_context.face); | |
1500 text_info.glyphs[text_info.length].desc = get_face_descender(render_context.face); | |
1501 | |
1502 text_info.length++; | |
1503 } | |
1504 | |
1505 if (text_info.length == 0) { | |
1506 // no valid symbols in the event; this can be smth like {comment} | |
1507 free_render_context(); | |
1508 return 0; | |
1509 } | |
1510 | |
1511 // depends on glyph x coordinates being monotonous, so it should be done before line wrap | |
1512 process_karaoke_effects(); | |
1513 | |
1514 // calculate max length of a line | |
1515 MarginL = (event->MarginL) ? event->MarginL : style->MarginL; | |
1516 MarginR = (event->MarginR) ? event->MarginR : style->MarginR; | |
1517 MarginV = (event->MarginV) ? event->MarginV : style->MarginV; | |
1518 | |
1519 max_text_width = x2scr(frame_context.track->PlayResX - MarginR) - x2scr(MarginL); | |
1520 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "normal text width: %d\n", max_text_width); | |
1521 | |
1522 // rearrange text in several lines | |
1523 wrap_lines_smart(max_text_width); | |
1524 | |
1525 // alignments | |
1526 alignment = render_context.alignment; | |
1527 if (!alignment) | |
1528 alignment = render_context.style->Alignment; | |
1529 halign = alignment & 3; | |
1530 valign = alignment & 12; | |
1531 | |
1532 // align text | |
1533 last_break = -1; | |
1534 for (i = 1; i < text_info.length + 1; ++i) { // (text_info.length + 1) is the end of the last line | |
1535 if ((i == text_info.length) || text_info.glyphs[i].linebreak) { | |
1536 | |
1537 glyph_info_t* first_glyph = text_info.glyphs + last_break + 1; | |
1538 glyph_info_t* last_glyph = text_info.glyphs + i - 1; | |
1539 | |
1540 while ((last_glyph >= first_glyph) && ((last_glyph->symbol == '\n') || (last_glyph->symbol == 0))) | |
1541 last_glyph --; | |
1542 | |
1543 int width = last_glyph->pos.x + last_glyph->bbox.xMax - first_glyph->pos.x - first_glyph->bbox.xMin; | |
1544 int shift = - first_glyph->bbox.xMin; // now text line starts exactly at 0 (left margin) | |
1545 if (halign == HALIGN_LEFT) { // left aligned, no action | |
1546 } else if (halign == HALIGN_RIGHT) { // right aligned | |
1547 shift = max_text_width - width; | |
1548 } else if (halign == HALIGN_CENTER) { // centered | |
1549 shift = (max_text_width - width) / 2; | |
1550 } | |
1551 for (j = last_break + 1; j < i; ++j) { | |
1552 text_info.glyphs[j].pos.x += shift; | |
1553 } | |
1554 last_break = i - 1; | |
1555 } | |
1556 } | |
1557 | |
1558 // determing text bounding box | |
1559 compute_string_bbox(&text_info, &bbox); | |
1560 | |
1561 // determine device coordinates for text | |
1562 | |
1563 // FIXME: using current font descender, ascender and height here is wrong. | |
1564 // correct way is using max(descender) over all the fonts used in a line | |
1565 // the same for height and ascender | |
1566 if (render_context.evt_type == EVENT_NORMAL) { | |
1567 device_x = x2scr(MarginL); | |
1568 if (valign == VALIGN_TOP) { // toptitle | |
1569 device_y = y2scr_top(MarginV) + (text_info.lines[0].asc >> 6); | |
1570 if (render_context.detect_collisions) { | |
1571 device_y += frame_context.add_top_margin; | |
1572 frame_context.add_top_margin += (text_info.height >> 6); | |
1573 } | |
1574 } else if (valign == VALIGN_CENTER) { // midtitle | |
1575 int scr_y = y2scr(frame_context.track->PlayResY / 2); | |
1576 device_y = scr_y - (bbox.yMax - bbox.yMin) / 2; | |
1577 } else { // subtitle | |
1578 int scr_y; | |
1579 if (valign != VALIGN_SUB) | |
1580 mp_msg(MSGT_GLOBAL, MSGL_V, "Invalid valign, supposing 0 (subtitle)\n"); | |
1581 scr_y = y2scr_sub(frame_context.track->PlayResY - MarginV); | |
1582 device_y = scr_y; | |
1583 device_y -= (text_info.height >> 6); | |
1584 device_y += (text_info.lines[0].asc >> 6); | |
1585 if (render_context.detect_collisions) { | |
1586 device_y -= frame_context.add_bottom_margin; | |
1587 frame_context.add_bottom_margin += (text_info.height >> 6); | |
1588 } | |
1589 } | |
1590 } else if (render_context.evt_type == EVENT_POSITIONED) { | |
1591 int align_shift_x = 0; | |
1592 int align_shift_y = 0; | |
1593 mp_msg(MSGT_GLOBAL, MSGL_DBG2, "positioned event at %d, %d\n", render_context.pos_x, render_context.pos_y); | |
1594 switch(halign) { | |
1595 case HALIGN_LEFT: | |
1596 align_shift_x = - bbox.xMin; | |
1597 break; | |
1598 case HALIGN_CENTER: | |
1599 align_shift_x = - (bbox.xMax + bbox.xMin) /2; | |
1600 break; | |
1601 case HALIGN_RIGHT: | |
1602 align_shift_x = - bbox.xMax; | |
1603 break; | |
1604 } | |
1605 switch(valign) { | |
1606 case VALIGN_TOP: | |
1607 align_shift_y = - bbox.yMin; | |
1608 break; | |
1609 case VALIGN_CENTER: | |
1610 align_shift_y = - (bbox.yMax + bbox.yMin) /2; | |
1611 break; | |
1612 case VALIGN_SUB: | |
1613 align_shift_y = - bbox.yMax; | |
1614 break; | |
1615 } | |
1616 device_x = x2scr(render_context.pos_x) + align_shift_x; | |
1617 device_y = y2scr(render_context.pos_y) + align_shift_y; | |
1618 } else { | |
1619 mp_msg(MSGT_GLOBAL, MSGL_V, "unknown evt_type\n"); | |
1620 device_x = 10; | |
1621 device_y = 10; | |
1622 } | |
1623 | |
1624 // fix clip coordinates (they depend on alignment) | |
1625 render_context.clip_x0 = x2scr(render_context.clip_x0); | |
1626 render_context.clip_x1 = x2scr(render_context.clip_x1); | |
1627 if (render_context.evt_type == EVENT_NORMAL) { | |
1628 if (valign == VALIGN_TOP) { | |
1629 render_context.clip_y0 = y2scr_top(render_context.clip_y0); | |
1630 render_context.clip_y1 = y2scr_top(render_context.clip_y1); | |
1631 } else if (valign == VALIGN_CENTER) { | |
1632 render_context.clip_y0 = y2scr(render_context.clip_y0); | |
1633 render_context.clip_y1 = y2scr(render_context.clip_y1); | |
1634 } else if (valign == VALIGN_SUB) { | |
1635 render_context.clip_y0 = y2scr_sub(render_context.clip_y0); | |
1636 render_context.clip_y1 = y2scr_sub(render_context.clip_y1); | |
1637 } | |
1638 } else if (render_context.evt_type == EVENT_POSITIONED) { | |
1639 render_context.clip_y0 = y2scr(render_context.clip_y0); | |
1640 render_context.clip_y1 = y2scr(render_context.clip_y1); | |
1641 } | |
1642 | |
1643 // rotate glyphs if needed | |
1644 if (render_context.rotation != 0.) { | |
1645 double angle = render_context.rotation; | |
1646 FT_Vector center; | |
1647 FT_Matrix matrix_rotate; | |
1648 | |
1649 matrix_rotate.xx = (FT_Fixed)( cos( angle ) * 0x10000L ); | |
1650 matrix_rotate.xy = (FT_Fixed)( -sin( angle ) * 0x10000L ); | |
1651 matrix_rotate.yx = (FT_Fixed)( sin( angle ) * 0x10000L ); | |
1652 matrix_rotate.yy = (FT_Fixed)( cos( angle ) * 0x10000L ); | |
1653 | |
1654 if (((render_context.org_x != 0) || (render_context.org_y != 0)) && (render_context.evt_type == EVENT_POSITIONED)) { | |
1655 center.x = render_context.org_x; | |
1656 center.y = render_context.org_y; | |
1657 } else { | |
1658 FT_BBox str_bbox; | |
1659 | |
1660 center.x = text_info.glyphs[0].pos.x + device_x; | |
1661 center.y = text_info.glyphs[0].pos.y + device_y; | |
1662 | |
1663 compute_string_bbox(&text_info, &str_bbox); | |
1664 center.x += (str_bbox.xMax - str_bbox.xMin) / 2; | |
1665 center.y += (str_bbox.yMax - str_bbox.yMin) / 2; | |
1666 } | |
1667 // mp_msg(MSGT_GLOBAL, MSGL_DBG2, "\ncenter: %d, %d\n", center.x, center.y); | |
1668 | |
1669 for (i = 0; i < text_info.length; ++i) { | |
1670 glyph_info_t* info = text_info.glyphs + i; | |
1671 | |
1672 // calculating shift vector | |
1673 // shift = (position - center)*M - (position - center) | |
1674 FT_Vector start; | |
1675 FT_Vector start_old; | |
1676 // 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, | |
1677 // info->pos.x + device_x - center.x, info->pos.y + device_y - center.y); | |
1678 start.x = (info->pos.x + device_x - center.x) << 6; | |
1679 start.y = - (info->pos.y + device_y - center.y) << 6; | |
1680 start_old.x = start.x; | |
1681 start_old.y = start.y; | |
1682 // mp_msg(MSGT_GLOBAL, MSGL_INFO, "start: %d, %d\n", start.x / 64, start.y / 64); | |
1683 | |
1684 FT_Vector_Transform(&start, &matrix_rotate); | |
1685 | |
1686 start.x -= start_old.x; | |
1687 start.y -= start_old.y; | |
1688 | |
1689 info->pos.x += start.x >> 6; | |
1690 info->pos.y -= start.y >> 6; | |
1691 | |
1692 // mp_msg(MSGT_GLOBAL, MSGL_DBG2, "shift: %d, %d\n", start.x / 64, start.y / 64); | |
1693 if (info->bitmap != 1) { | |
1694 FT_Glyph_Transform( info->glyph, &matrix_rotate, 0 ); | |
1695 FT_Glyph_Transform( info->outline_glyph, &matrix_rotate, 0 ); | |
1696 } | |
1697 } | |
1698 } | |
1699 | |
1700 // render | |
1701 render_text(&text_info, device_x, device_y); | |
1702 | |
1703 free_render_context(); | |
1704 | |
1705 return 0; | |
1706 } | |
1707 | |
1708 void ass_configure(ass_instance_t* priv, const ass_settings_t* config) | |
1709 { | |
1710 memcpy(&priv->settings, config, sizeof(ass_settings_t)); | |
1711 } | |
1712 | |
1713 /** | |
1714 * \brief Start a new frame | |
1715 */ | |
1716 int ass_start_frame(ass_instance_t *priv, ass_track_t* track, long long now) | |
1717 { | |
1718 ass_instance = priv; | |
1719 global_settings = &priv->settings; | |
1720 | |
1721 if (!priv->settings.frame_width && !priv->settings.frame_height) | |
1722 return 1; // library not initialized | |
1723 | |
1724 frame_context.ass_priv = priv; | |
1725 frame_context.width = global_settings->frame_width; | |
1726 frame_context.height = global_settings->frame_height; | |
1727 frame_context.orig_height = global_settings->frame_height - global_settings->top_margin - global_settings->bottom_margin; | |
1728 frame_context.track = track; | |
1729 frame_context.add_bottom_margin = 0; | |
1730 frame_context.add_top_margin = 0; | |
1731 frame_context.time = now; | |
1732 | |
1733 ass_lazy_track_init(); | |
1734 | |
1735 if (frame_context.width * track->PlayResY == frame_context.height * track->PlayResX) | |
1736 frame_context.font_scale_x = 1.; | |
1737 else | |
1738 frame_context.font_scale_x = ((double)(frame_context.width * track->PlayResY)) / (frame_context.height * track->PlayResX); | |
1739 | |
1740 priv->n_images = 0; | |
1741 | |
1742 return 0; | |
1743 } | |
1744 | |
1745 /** | |
1746 * \brief End a frame, give out rendering results | |
1747 * \return list of ass_image_t | |
1748 */ | |
1749 ass_image_t* ass_end_frame(void) | |
1750 { | |
1751 ass_instance_t* priv = ass_instance; | |
1752 if (priv->n_images) { | |
1753 int i; | |
1754 for (i = 0; i < priv->n_images - 1; ++i) | |
1755 priv->images_root[i].next = priv->images_root + i + 1; | |
1756 priv->images_root[priv->n_images - 1].next = 0; | |
1757 return priv->images_root; | |
1758 } else { | |
1759 return 0; | |
1760 } | |
1761 } | |
1762 /** | |
1763 * \brief render a frame | |
1764 * \param priv library handle | |
1765 * \param track track | |
1766 * \param now current video timestamp (ms) | |
1767 */ | |
1768 ass_image_t* ass_render_frame(ass_instance_t *priv, ass_track_t* track, long long now) | |
1769 { | |
1770 int i, rc; | |
1771 | |
1772 rc = ass_start_frame(priv, track, now); | |
1773 if (rc != 0) // some error | |
1774 return 0; | |
1775 for (i = 0; i < track->n_events; ++i) { | |
1776 ass_event_t* event = track->events + i; | |
1777 if ( (event->Start <= now) && (now < (event->Start + event->Duration)) ) { | |
1778 ass_render_event(event); | |
1779 } | |
1780 } | |
1781 return ass_end_frame(); | |
1782 } | |
1783 |