30200
|
1 /*
|
|
2 * Copyright (C) 2009 Grigori Goronzy <greg@geekmind.org>
|
|
3 *
|
|
4 * This file is part of libass.
|
|
5 *
|
|
6 * Permission to use, copy, modify, and distribute this software for any
|
|
7 * purpose with or without fee is hereby granted, provided that the above
|
|
8 * copyright notice and this permission notice appear in all copies.
|
|
9 *
|
|
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
17 */
|
|
18
|
|
19 #include "config.h"
|
|
20
|
|
21 #include <stdio.h>
|
|
22 #include <stdlib.h>
|
|
23 #include <string.h>
|
|
24 #include <math.h>
|
|
25
|
|
26 #include "ass_render.h"
|
|
27 #include "ass_parse.h"
|
|
28
|
|
29 #define MAX_BE 127
|
|
30 #define NBSP 0xa0 // unicode non-breaking space character
|
|
31
|
|
32 #define skip_to(x) while ((*p != (x)) && (*p != '}') && (*p != 0)) { ++p;}
|
|
33 #define skip(x) if (*p == (x)) ++p; else { return p; }
|
|
34 #define skipopt(x) if (*p == (x)) { ++p; }
|
|
35
|
|
36 /**
|
|
37 * \brief Check if starting part of (*p) matches sample.
|
|
38 * If true, shift p to the first symbol after the matching part.
|
|
39 */
|
|
40 static inline int mystrcmp(char **p, const char *sample)
|
|
41 {
|
|
42 int len = strlen(sample);
|
|
43 if (strncmp(*p, sample, len) == 0) {
|
|
44 (*p) += len;
|
|
45 return 1;
|
|
46 } else
|
|
47 return 0;
|
|
48 }
|
|
49
|
|
50 static void change_font_size(ASS_Renderer *render_priv, double sz)
|
|
51 {
|
|
52 double size = sz * render_priv->font_scale;
|
|
53
|
|
54 if (size < 1)
|
|
55 size = 1;
|
|
56 else if (size > render_priv->height * 2)
|
|
57 size = render_priv->height * 2;
|
|
58
|
|
59 ass_font_set_size(render_priv->state.font, size);
|
|
60
|
|
61 render_priv->state.font_size = sz;
|
|
62 }
|
|
63
|
|
64 /**
|
|
65 * \brief Change current font, using setting from render_priv->state.
|
|
66 */
|
|
67 void update_font(ASS_Renderer *render_priv)
|
|
68 {
|
|
69 unsigned val;
|
|
70 ASS_FontDesc desc;
|
31853
|
71 desc.treat_family_as_pattern = render_priv->state.treat_family_as_pattern;
|
|
72
|
|
73 if (render_priv->state.family[0] == '@') {
|
|
74 desc.vertical = 1;
|
|
75 desc.family = strdup(render_priv->state.family + 1);
|
|
76 } else {
|
|
77 desc.vertical = 0;
|
|
78 desc.family = strdup(render_priv->state.family);
|
|
79 }
|
30200
|
80
|
|
81 val = render_priv->state.bold;
|
|
82 // 0 = normal, 1 = bold, >1 = exact weight
|
|
83 if (val == 1 || val == -1)
|
|
84 val = 200; // bold
|
|
85 else if (val <= 0)
|
|
86 val = 80; // normal
|
|
87 desc.bold = val;
|
|
88
|
|
89 val = render_priv->state.italic;
|
|
90 if (val == 1 || val == -1)
|
|
91 val = 110; // italic
|
|
92 else if (val <= 0)
|
|
93 val = 0; // normal
|
|
94 desc.italic = val;
|
|
95
|
|
96 render_priv->state.font =
|
|
97 ass_font_new(render_priv->cache.font_cache, render_priv->library,
|
|
98 render_priv->ftlibrary, render_priv->fontconfig_priv,
|
|
99 &desc);
|
|
100 free(desc.family);
|
|
101
|
|
102 if (render_priv->state.font)
|
|
103 change_font_size(render_priv, render_priv->state.font_size);
|
|
104 }
|
|
105
|
|
106 /**
|
|
107 * \brief Change border width
|
|
108 * negative value resets border to style value
|
|
109 */
|
|
110 void change_border(ASS_Renderer *render_priv, double border_x,
|
|
111 double border_y)
|
|
112 {
|
|
113 int bord;
|
|
114 if (!render_priv->state.font)
|
|
115 return;
|
|
116
|
|
117 if (border_x < 0 && border_y < 0) {
|
|
118 if (render_priv->state.style->BorderStyle == 1 ||
|
|
119 render_priv->state.style->BorderStyle == 3)
|
|
120 border_x = border_y = render_priv->state.style->Outline;
|
|
121 else
|
|
122 border_x = border_y = 1.;
|
|
123 }
|
|
124
|
|
125 render_priv->state.border_x = border_x;
|
|
126 render_priv->state.border_y = border_y;
|
|
127
|
|
128 bord = 64 * border_x * render_priv->border_scale;
|
|
129 if (bord > 0 && border_x == border_y) {
|
|
130 if (!render_priv->state.stroker) {
|
|
131 int error;
|
|
132 error =
|
|
133 FT_Stroker_New(render_priv->ftlibrary,
|
|
134 &render_priv->state.stroker);
|
|
135 if (error) {
|
|
136 ass_msg(render_priv->library, MSGL_V,
|
|
137 "failed to get stroker");
|
|
138 render_priv->state.stroker = 0;
|
|
139 }
|
|
140 }
|
|
141 if (render_priv->state.stroker)
|
|
142 FT_Stroker_Set(render_priv->state.stroker, bord,
|
|
143 FT_STROKER_LINECAP_ROUND,
|
|
144 FT_STROKER_LINEJOIN_ROUND, 0);
|
|
145 } else {
|
|
146 FT_Stroker_Done(render_priv->state.stroker);
|
|
147 render_priv->state.stroker = 0;
|
|
148 }
|
|
149 }
|
|
150
|
|
151 /**
|
|
152 * \brief Calculate a weighted average of two colors
|
|
153 * calculates c1*(1-a) + c2*a, but separately for each component except alpha
|
|
154 */
|
|
155 static void change_color(uint32_t *var, uint32_t new, double pwr)
|
|
156 {
|
|
157 (*var) = ((uint32_t) (_r(*var) * (1 - pwr) + _r(new) * pwr) << 24) +
|
|
158 ((uint32_t) (_g(*var) * (1 - pwr) + _g(new) * pwr) << 16) +
|
|
159 ((uint32_t) (_b(*var) * (1 - pwr) + _b(new) * pwr) << 8) + _a(*var);
|
|
160 }
|
|
161
|
|
162 // like change_color, but for alpha component only
|
|
163 inline void change_alpha(uint32_t *var, uint32_t new, double pwr)
|
|
164 {
|
|
165 *var =
|
|
166 (_r(*var) << 24) + (_g(*var) << 16) + (_b(*var) << 8) +
|
|
167 (uint32_t) (_a(*var) * (1 - pwr) + _a(new) * pwr);
|
|
168 }
|
|
169
|
|
170 /**
|
|
171 * \brief Multiply two alpha values
|
|
172 * \param a first value
|
|
173 * \param b second value
|
|
174 * \return result of multiplication
|
|
175 * Parameters and result are limited by 0xFF.
|
|
176 */
|
|
177 inline uint32_t mult_alpha(uint32_t a, uint32_t b)
|
|
178 {
|
|
179 return 0xFF - (0xFF - a) * (0xFF - b) / 0xFF;
|
|
180 }
|
|
181
|
|
182 /**
|
|
183 * \brief Calculate alpha value by piecewise linear function
|
|
184 * Used for \fad, \fade implementation.
|
|
185 */
|
|
186 static unsigned
|
|
187 interpolate_alpha(long long now, long long t1, long long t2, long long t3,
|
|
188 long long t4, unsigned a1, unsigned a2, unsigned a3)
|
|
189 {
|
|
190 unsigned a;
|
|
191 double cf;
|
|
192 if (now <= t1) {
|
|
193 a = a1;
|
|
194 } else if (now >= t4) {
|
|
195 a = a3;
|
|
196 } else if (now < t2) { // and > t1
|
|
197 cf = ((double) (now - t1)) / (t2 - t1);
|
|
198 a = a1 * (1 - cf) + a2 * cf;
|
|
199 } else if (now > t3) {
|
|
200 cf = ((double) (now - t3)) / (t4 - t3);
|
|
201 a = a2 * (1 - cf) + a3 * cf;
|
|
202 } else { // t2 <= now <= t3
|
|
203 a = a2;
|
|
204 }
|
|
205
|
|
206 return a;
|
|
207 }
|
|
208
|
|
209 /**
|
|
210 * Parse a vector clip into an outline, using the proper scaling
|
|
211 * parameters. Translate it to correct for screen borders, if needed.
|
|
212 */
|
|
213 static char *parse_vector_clip(ASS_Renderer *render_priv, char *p)
|
|
214 {
|
|
215 int scale = 1;
|
|
216 int res = 0;
|
31875
|
217 ASS_Drawing *drawing = render_priv->state.clip_drawing;
|
30200
|
218
|
31875
|
219 if (drawing && drawing->glyph)
|
|
220 FT_Done_Glyph((FT_Glyph) drawing->glyph);
|
|
221 ass_drawing_free(drawing);
|
30200
|
222 render_priv->state.clip_drawing = ass_drawing_new(
|
|
223 render_priv->fontconfig_priv,
|
|
224 render_priv->state.font,
|
|
225 render_priv->ftlibrary);
|
|
226 drawing = render_priv->state.clip_drawing;
|
|
227 skipopt('(');
|
|
228 res = mystrtoi(&p, &scale);
|
|
229 skipopt(',')
|
|
230 if (!res)
|
|
231 scale = 1;
|
|
232 drawing->scale = scale;
|
|
233 drawing->scale_x = render_priv->font_scale_x * render_priv->font_scale;
|
|
234 drawing->scale_y = render_priv->font_scale;
|
|
235 while (*p != ')' && *p != '}' && p != 0)
|
|
236 ass_drawing_add_char(drawing, *p++);
|
|
237 skipopt(')');
|
|
238
|
|
239 return p;
|
|
240 }
|
|
241
|
|
242 /**
|
|
243 * \brief Parse style override tag.
|
|
244 * \param p string to parse
|
|
245 * \param pwr multiplier for some tag effects (comes from \t tags)
|
|
246 */
|
|
247 static char *parse_tag(ASS_Renderer *render_priv, char *p, double pwr)
|
|
248 {
|
|
249 skip_to('\\');
|
|
250 skip('\\');
|
|
251 if ((*p == '}') || (*p == 0))
|
|
252 return p;
|
|
253
|
|
254 // New tags introduced in vsfilter 2.39
|
|
255 if (mystrcmp(&p, "xbord")) {
|
|
256 double val;
|
|
257 if (mystrtod(&p, &val))
|
|
258 val = render_priv->state.border_x * (1 - pwr) + val * pwr;
|
|
259 else
|
|
260 val = -1.;
|
|
261 change_border(render_priv, val, render_priv->state.border_y);
|
|
262 } else if (mystrcmp(&p, "ybord")) {
|
|
263 double val;
|
|
264 if (mystrtod(&p, &val))
|
|
265 val = render_priv->state.border_y * (1 - pwr) + val * pwr;
|
|
266 else
|
|
267 val = -1.;
|
|
268 change_border(render_priv, render_priv->state.border_x, val);
|
|
269 } else if (mystrcmp(&p, "xshad")) {
|
|
270 double val;
|
|
271 if (mystrtod(&p, &val))
|
|
272 val = render_priv->state.shadow_x * (1 - pwr) + val * pwr;
|
|
273 else
|
|
274 val = 0.;
|
|
275 render_priv->state.shadow_x = val;
|
|
276 } else if (mystrcmp(&p, "yshad")) {
|
|
277 double val;
|
|
278 if (mystrtod(&p, &val))
|
|
279 val = render_priv->state.shadow_y * (1 - pwr) + val * pwr;
|
|
280 else
|
|
281 val = 0.;
|
|
282 render_priv->state.shadow_y = val;
|
|
283 } else if (mystrcmp(&p, "fax")) {
|
|
284 double val;
|
|
285 if (mystrtod(&p, &val))
|
|
286 render_priv->state.fax =
|
|
287 val * pwr + render_priv->state.fax * (1 - pwr);
|
|
288 else
|
|
289 render_priv->state.fax = 0.;
|
|
290 } else if (mystrcmp(&p, "fay")) {
|
|
291 double val;
|
|
292 if (mystrtod(&p, &val))
|
|
293 render_priv->state.fay =
|
|
294 val * pwr + render_priv->state.fay * (1 - pwr);
|
|
295 else
|
|
296 render_priv->state.fay = 0.;
|
|
297 } else if (mystrcmp(&p, "iclip")) {
|
|
298 int x0, y0, x1, y1;
|
|
299 int res = 1;
|
|
300 char *start = p;
|
|
301 skipopt('(');
|
|
302 res &= mystrtoi(&p, &x0);
|
|
303 skipopt(',');
|
|
304 res &= mystrtoi(&p, &y0);
|
|
305 skipopt(',');
|
|
306 res &= mystrtoi(&p, &x1);
|
|
307 skipopt(',');
|
|
308 res &= mystrtoi(&p, &y1);
|
|
309 skipopt(')');
|
|
310 if (res) {
|
|
311 render_priv->state.clip_x0 =
|
|
312 render_priv->state.clip_x0 * (1 - pwr) + x0 * pwr;
|
|
313 render_priv->state.clip_x1 =
|
|
314 render_priv->state.clip_x1 * (1 - pwr) + x1 * pwr;
|
|
315 render_priv->state.clip_y0 =
|
|
316 render_priv->state.clip_y0 * (1 - pwr) + y0 * pwr;
|
|
317 render_priv->state.clip_y1 =
|
|
318 render_priv->state.clip_y1 * (1 - pwr) + y1 * pwr;
|
|
319 render_priv->state.clip_mode = 1;
|
|
320 } else if (!render_priv->state.clip_drawing) {
|
|
321 p = parse_vector_clip(render_priv, start);
|
|
322 render_priv->state.clip_drawing_mode = 1;
|
|
323 } else
|
|
324 render_priv->state.clip_mode = 0;
|
|
325 } else if (mystrcmp(&p, "blur")) {
|
|
326 double val;
|
|
327 if (mystrtod(&p, &val)) {
|
|
328 val = render_priv->state.blur * (1 - pwr) + val * pwr;
|
|
329 val = (val < 0) ? 0 : val;
|
|
330 val = (val > BLUR_MAX_RADIUS) ? BLUR_MAX_RADIUS : val;
|
|
331 render_priv->state.blur = val;
|
|
332 } else
|
|
333 render_priv->state.blur = 0.0;
|
|
334 // ASS standard tags
|
|
335 } else if (mystrcmp(&p, "fsc")) {
|
|
336 char tp = *p++;
|
|
337 double val;
|
|
338 if (tp == 'x') {
|
|
339 if (mystrtod(&p, &val)) {
|
|
340 val /= 100;
|
|
341 render_priv->state.scale_x =
|
|
342 render_priv->state.scale_x * (1 - pwr) + val * pwr;
|
|
343 } else
|
|
344 render_priv->state.scale_x =
|
|
345 render_priv->state.style->ScaleX;
|
|
346 } else if (tp == 'y') {
|
|
347 if (mystrtod(&p, &val)) {
|
|
348 val /= 100;
|
|
349 render_priv->state.scale_y =
|
|
350 render_priv->state.scale_y * (1 - pwr) + val * pwr;
|
|
351 } else
|
|
352 render_priv->state.scale_y =
|
|
353 render_priv->state.style->ScaleY;
|
|
354 }
|
|
355 } else if (mystrcmp(&p, "fsp")) {
|
|
356 double val;
|
|
357 if (mystrtod(&p, &val))
|
|
358 render_priv->state.hspacing =
|
|
359 render_priv->state.hspacing * (1 - pwr) + val * pwr;
|
|
360 else
|
|
361 render_priv->state.hspacing = render_priv->state.style->Spacing;
|
34011
|
362 } else if (mystrcmp(&p, "fs+")) {
|
|
363 double val;
|
|
364 if (mystrtod(&p, &val)) {
|
|
365 val = render_priv->state.font_size + pwr * val;
|
|
366 } else
|
|
367 val = render_priv->state.style->FontSize;
|
|
368 if (render_priv->state.font)
|
|
369 change_font_size(render_priv, val);
|
|
370 } else if (mystrcmp(&p, "fs-")) {
|
|
371 double val;
|
|
372 if (mystrtod(&p, &val))
|
|
373 val = render_priv->state.font_size - pwr * val;
|
|
374 else
|
|
375 val = render_priv->state.style->FontSize;
|
|
376 if (render_priv->state.font)
|
|
377 change_font_size(render_priv, val);
|
30200
|
378 } else if (mystrcmp(&p, "fs")) {
|
|
379 double val;
|
|
380 if (mystrtod(&p, &val))
|
|
381 val = render_priv->state.font_size * (1 - pwr) + val * pwr;
|
|
382 else
|
|
383 val = render_priv->state.style->FontSize;
|
|
384 if (render_priv->state.font)
|
|
385 change_font_size(render_priv, val);
|
|
386 } else if (mystrcmp(&p, "bord")) {
|
|
387 double val;
|
|
388 if (mystrtod(&p, &val)) {
|
|
389 if (render_priv->state.border_x == render_priv->state.border_y)
|
|
390 val = render_priv->state.border_x * (1 - pwr) + val * pwr;
|
|
391 } else
|
|
392 val = -1.; // reset to default
|
|
393 change_border(render_priv, val, val);
|
|
394 } else if (mystrcmp(&p, "move")) {
|
|
395 double x1, x2, y1, y2;
|
|
396 long long t1, t2, delta_t, t;
|
|
397 double x, y;
|
|
398 double k;
|
|
399 skip('(');
|
|
400 mystrtod(&p, &x1);
|
|
401 skip(',');
|
|
402 mystrtod(&p, &y1);
|
|
403 skip(',');
|
|
404 mystrtod(&p, &x2);
|
|
405 skip(',');
|
|
406 mystrtod(&p, &y2);
|
|
407 if (*p == ',') {
|
|
408 skip(',');
|
|
409 mystrtoll(&p, &t1);
|
|
410 skip(',');
|
|
411 mystrtoll(&p, &t2);
|
|
412 ass_msg(render_priv->library, MSGL_DBG2,
|
|
413 "movement6: (%f, %f) -> (%f, %f), (%" PRId64 " .. %"
|
|
414 PRId64 ")\n", x1, y1, x2, y2, (int64_t) t1,
|
|
415 (int64_t) t2);
|
|
416 } else {
|
|
417 t1 = 0;
|
|
418 t2 = render_priv->state.event->Duration;
|
|
419 ass_msg(render_priv->library, MSGL_DBG2,
|
|
420 "movement: (%f, %f) -> (%f, %f)", x1, y1, x2, y2);
|
|
421 }
|
|
422 skip(')');
|
|
423 delta_t = t2 - t1;
|
|
424 t = render_priv->time - render_priv->state.event->Start;
|
|
425 if (t < t1)
|
|
426 k = 0.;
|
|
427 else if (t > t2)
|
|
428 k = 1.;
|
|
429 else
|
|
430 k = ((double) (t - t1)) / delta_t;
|
|
431 x = k * (x2 - x1) + x1;
|
|
432 y = k * (y2 - y1) + y1;
|
|
433 if (render_priv->state.evt_type != EVENT_POSITIONED) {
|
|
434 render_priv->state.pos_x = x;
|
|
435 render_priv->state.pos_y = y;
|
|
436 render_priv->state.detect_collisions = 0;
|
|
437 render_priv->state.evt_type = EVENT_POSITIONED;
|
|
438 }
|
|
439 } else if (mystrcmp(&p, "frx")) {
|
|
440 double val;
|
|
441 if (mystrtod(&p, &val)) {
|
|
442 val *= M_PI / 180;
|
|
443 render_priv->state.frx =
|
|
444 val * pwr + render_priv->state.frx * (1 - pwr);
|
|
445 } else
|
|
446 render_priv->state.frx = 0.;
|
|
447 } else if (mystrcmp(&p, "fry")) {
|
|
448 double val;
|
|
449 if (mystrtod(&p, &val)) {
|
|
450 val *= M_PI / 180;
|
|
451 render_priv->state.fry =
|
|
452 val * pwr + render_priv->state.fry * (1 - pwr);
|
|
453 } else
|
|
454 render_priv->state.fry = 0.;
|
|
455 } else if (mystrcmp(&p, "frz") || mystrcmp(&p, "fr")) {
|
|
456 double val;
|
|
457 if (mystrtod(&p, &val)) {
|
|
458 val *= M_PI / 180;
|
|
459 render_priv->state.frz =
|
|
460 val * pwr + render_priv->state.frz * (1 - pwr);
|
|
461 } else
|
|
462 render_priv->state.frz =
|
|
463 M_PI * render_priv->state.style->Angle / 180.;
|
|
464 } else if (mystrcmp(&p, "fn")) {
|
|
465 char *start = p;
|
|
466 char *family;
|
|
467 skip_to('\\');
|
|
468 if (p > start) {
|
|
469 family = malloc(p - start + 1);
|
|
470 strncpy(family, start, p - start);
|
|
471 family[p - start] = '\0';
|
|
472 } else
|
|
473 family = strdup(render_priv->state.style->FontName);
|
31875
|
474 free(render_priv->state.family);
|
30200
|
475 render_priv->state.family = family;
|
|
476 update_font(render_priv);
|
|
477 } else if (mystrcmp(&p, "alpha")) {
|
|
478 uint32_t val;
|
|
479 int i;
|
|
480 int hex = render_priv->track->track_type == TRACK_TYPE_ASS;
|
|
481 if (strtocolor(render_priv->library, &p, &val, hex)) {
|
|
482 unsigned char a = val >> 24;
|
|
483 for (i = 0; i < 4; ++i)
|
|
484 change_alpha(&render_priv->state.c[i], a, pwr);
|
|
485 } else {
|
|
486 change_alpha(&render_priv->state.c[0],
|
|
487 render_priv->state.style->PrimaryColour, pwr);
|
|
488 change_alpha(&render_priv->state.c[1],
|
|
489 render_priv->state.style->SecondaryColour, pwr);
|
|
490 change_alpha(&render_priv->state.c[2],
|
|
491 render_priv->state.style->OutlineColour, pwr);
|
|
492 change_alpha(&render_priv->state.c[3],
|
|
493 render_priv->state.style->BackColour, pwr);
|
|
494 }
|
|
495 // FIXME: simplify
|
|
496 } else if (mystrcmp(&p, "an")) {
|
|
497 int val;
|
|
498 if (mystrtoi(&p, &val) && val) {
|
|
499 int v = (val - 1) / 3; // 0, 1 or 2 for vertical alignment
|
|
500 ass_msg(render_priv->library, MSGL_DBG2, "an %d", val);
|
|
501 if (v != 0)
|
|
502 v = 3 - v;
|
|
503 val = ((val - 1) % 3) + 1; // horizontal alignment
|
|
504 val += v * 4;
|
|
505 ass_msg(render_priv->library, MSGL_DBG2, "align %d", val);
|
34011
|
506 if ((render_priv->state.parsed_tags & PARSED_A) == 0) {
|
|
507 render_priv->state.alignment = val;
|
|
508 render_priv->state.parsed_tags |= PARSED_A;
|
|
509 }
|
30200
|
510 } else
|
|
511 render_priv->state.alignment =
|
|
512 render_priv->state.style->Alignment;
|
|
513 } else if (mystrcmp(&p, "a")) {
|
|
514 int val;
|
34011
|
515 if (mystrtoi(&p, &val) && val) {
|
|
516 if ((render_priv->state.parsed_tags & PARSED_A) == 0) {
|
|
517 // take care of a vsfilter quirk: handle illegal \a8 like \a5
|
|
518 render_priv->state.alignment = (val == 8) ? 5 : val;
|
|
519 render_priv->state.parsed_tags |= PARSED_A;
|
|
520 }
|
|
521 } else
|
30200
|
522 render_priv->state.alignment =
|
|
523 render_priv->state.style->Alignment;
|
|
524 } else if (mystrcmp(&p, "pos")) {
|
|
525 double v1, v2;
|
|
526 skip('(');
|
|
527 mystrtod(&p, &v1);
|
|
528 skip(',');
|
|
529 mystrtod(&p, &v2);
|
|
530 skip(')');
|
|
531 ass_msg(render_priv->library, MSGL_DBG2, "pos(%f, %f)", v1, v2);
|
|
532 if (render_priv->state.evt_type == EVENT_POSITIONED) {
|
|
533 ass_msg(render_priv->library, MSGL_V, "Subtitle has a new \\pos "
|
|
534 "after \\move or \\pos, ignoring");
|
|
535 } else {
|
|
536 render_priv->state.evt_type = EVENT_POSITIONED;
|
|
537 render_priv->state.detect_collisions = 0;
|
|
538 render_priv->state.pos_x = v1;
|
|
539 render_priv->state.pos_y = v2;
|
|
540 }
|
|
541 } else if (mystrcmp(&p, "fad")) {
|
|
542 int a1, a2, a3;
|
|
543 long long t1, t2, t3, t4;
|
|
544 if (*p == 'e')
|
|
545 ++p; // either \fad or \fade
|
|
546 skip('(');
|
|
547 mystrtoi(&p, &a1);
|
|
548 skip(',');
|
|
549 mystrtoi(&p, &a2);
|
|
550 if (*p == ')') {
|
|
551 // 2-argument version (\fad, according to specs)
|
|
552 // a1 and a2 are fade-in and fade-out durations
|
|
553 t1 = 0;
|
|
554 t4 = render_priv->state.event->Duration;
|
|
555 t2 = a1;
|
|
556 t3 = t4 - a2;
|
|
557 a1 = 0xFF;
|
|
558 a2 = 0;
|
|
559 a3 = 0xFF;
|
|
560 } else {
|
|
561 // 6-argument version (\fade)
|
|
562 // a1 and a2 (and a3) are opacity values
|
|
563 skip(',');
|
|
564 mystrtoi(&p, &a3);
|
|
565 skip(',');
|
|
566 mystrtoll(&p, &t1);
|
|
567 skip(',');
|
|
568 mystrtoll(&p, &t2);
|
|
569 skip(',');
|
|
570 mystrtoll(&p, &t3);
|
|
571 skip(',');
|
|
572 mystrtoll(&p, &t4);
|
|
573 }
|
|
574 skip(')');
|
34011
|
575 if ((render_priv->state.parsed_tags & PARSED_FADE) == 0) {
|
|
576 render_priv->state.fade =
|
|
577 interpolate_alpha(render_priv->time -
|
|
578 render_priv->state.event->Start, t1, t2,
|
|
579 t3, t4, a1, a2, a3);
|
|
580 render_priv->state.parsed_tags |= PARSED_FADE;
|
|
581 }
|
30200
|
582 } else if (mystrcmp(&p, "org")) {
|
|
583 int v1, v2;
|
|
584 skip('(');
|
|
585 mystrtoi(&p, &v1);
|
|
586 skip(',');
|
|
587 mystrtoi(&p, &v2);
|
|
588 skip(')');
|
|
589 ass_msg(render_priv->library, MSGL_DBG2, "org(%d, %d)", v1, v2);
|
|
590 if (!render_priv->state.have_origin) {
|
|
591 render_priv->state.org_x = v1;
|
|
592 render_priv->state.org_y = v2;
|
|
593 render_priv->state.have_origin = 1;
|
|
594 render_priv->state.detect_collisions = 0;
|
|
595 }
|
|
596 } else if (mystrcmp(&p, "t")) {
|
|
597 double v[3];
|
|
598 int v1, v2;
|
|
599 double v3;
|
|
600 int cnt;
|
|
601 long long t1, t2, t, delta_t;
|
|
602 double k;
|
|
603 skip('(');
|
|
604 for (cnt = 0; cnt < 3; ++cnt) {
|
|
605 if (*p == '\\')
|
|
606 break;
|
31853
|
607 mystrtod(&p, &v[cnt]);
|
30200
|
608 skip(',');
|
|
609 }
|
|
610 if (cnt == 3) {
|
|
611 v1 = v[0];
|
|
612 v2 = (v[1] < v1) ? render_priv->state.event->Duration : v[1];
|
|
613 v3 = v[2];
|
|
614 } else if (cnt == 2) {
|
|
615 v1 = v[0];
|
|
616 v2 = (v[1] < v1) ? render_priv->state.event->Duration : v[1];
|
|
617 v3 = 1.;
|
|
618 } else if (cnt == 1) {
|
|
619 v1 = 0;
|
|
620 v2 = render_priv->state.event->Duration;
|
|
621 v3 = v[0];
|
|
622 } else { // cnt == 0
|
|
623 v1 = 0;
|
|
624 v2 = render_priv->state.event->Duration;
|
|
625 v3 = 1.;
|
|
626 }
|
|
627 render_priv->state.detect_collisions = 0;
|
|
628 t1 = v1;
|
|
629 t2 = v2;
|
|
630 delta_t = v2 - v1;
|
|
631 if (v3 < 0.)
|
|
632 v3 = 0.;
|
|
633 t = render_priv->time - render_priv->state.event->Start; // FIXME: move to render_context
|
|
634 if (t <= t1)
|
|
635 k = 0.;
|
|
636 else if (t >= t2)
|
|
637 k = 1.;
|
|
638 else {
|
|
639 assert(delta_t != 0.);
|
|
640 k = pow(((double) (t - t1)) / delta_t, v3);
|
|
641 }
|
|
642 while (*p == '\\')
|
|
643 p = parse_tag(render_priv, p, k); // maybe k*pwr ? no, specs forbid nested \t's
|
|
644 skip_to(')'); // in case there is some unknown tag or a comment
|
|
645 skip(')');
|
|
646 } else if (mystrcmp(&p, "clip")) {
|
|
647 char *start = p;
|
|
648 int x0, y0, x1, y1;
|
|
649 int res = 1;
|
|
650 skipopt('(');
|
|
651 res &= mystrtoi(&p, &x0);
|
|
652 skipopt(',');
|
|
653 res &= mystrtoi(&p, &y0);
|
|
654 skipopt(',');
|
|
655 res &= mystrtoi(&p, &x1);
|
|
656 skipopt(',');
|
|
657 res &= mystrtoi(&p, &y1);
|
|
658 skipopt(')');
|
|
659 if (res) {
|
|
660 render_priv->state.clip_x0 =
|
|
661 render_priv->state.clip_x0 * (1 - pwr) + x0 * pwr;
|
|
662 render_priv->state.clip_x1 =
|
|
663 render_priv->state.clip_x1 * (1 - pwr) + x1 * pwr;
|
|
664 render_priv->state.clip_y0 =
|
|
665 render_priv->state.clip_y0 * (1 - pwr) + y0 * pwr;
|
|
666 render_priv->state.clip_y1 =
|
|
667 render_priv->state.clip_y1 * (1 - pwr) + y1 * pwr;
|
|
668 // Might be a vector clip
|
|
669 } else if (!render_priv->state.clip_drawing) {
|
|
670 p = parse_vector_clip(render_priv, start);
|
|
671 render_priv->state.clip_drawing_mode = 0;
|
|
672 } else {
|
|
673 render_priv->state.clip_x0 = 0;
|
|
674 render_priv->state.clip_y0 = 0;
|
|
675 render_priv->state.clip_x1 = render_priv->track->PlayResX;
|
|
676 render_priv->state.clip_y1 = render_priv->track->PlayResY;
|
|
677 }
|
|
678 } else if (mystrcmp(&p, "c")) {
|
|
679 uint32_t val;
|
|
680 int hex = render_priv->track->track_type == TRACK_TYPE_ASS;
|
|
681 if (!strtocolor(render_priv->library, &p, &val, hex))
|
|
682 val = render_priv->state.style->PrimaryColour;
|
|
683 ass_msg(render_priv->library, MSGL_DBG2, "color: %X", val);
|
|
684 change_color(&render_priv->state.c[0], val, pwr);
|
|
685 } else if ((*p >= '1') && (*p <= '4') && (++p)
|
|
686 && (mystrcmp(&p, "c") || mystrcmp(&p, "a"))) {
|
|
687 char n = *(p - 2);
|
|
688 int cidx = n - '1';
|
|
689 char cmd = *(p - 1);
|
|
690 uint32_t val;
|
|
691 int hex = render_priv->track->track_type == TRACK_TYPE_ASS;
|
|
692 assert((n >= '1') && (n <= '4'));
|
|
693 if (!strtocolor(render_priv->library, &p, &val, hex))
|
|
694 switch (n) {
|
|
695 case '1':
|
|
696 val = render_priv->state.style->PrimaryColour;
|
|
697 break;
|
|
698 case '2':
|
|
699 val = render_priv->state.style->SecondaryColour;
|
|
700 break;
|
|
701 case '3':
|
|
702 val = render_priv->state.style->OutlineColour;
|
|
703 break;
|
|
704 case '4':
|
|
705 val = render_priv->state.style->BackColour;
|
|
706 break;
|
|
707 default:
|
|
708 val = 0;
|
|
709 break; // impossible due to assert; avoid compilation warning
|
|
710 }
|
|
711 switch (cmd) {
|
|
712 case 'c':
|
|
713 change_color(render_priv->state.c + cidx, val, pwr);
|
|
714 break;
|
|
715 case 'a':
|
|
716 change_alpha(render_priv->state.c + cidx, val >> 24, pwr);
|
|
717 break;
|
|
718 default:
|
|
719 ass_msg(render_priv->library, MSGL_WARN, "Bad command: %c%c",
|
|
720 n, cmd);
|
|
721 break;
|
|
722 }
|
|
723 ass_msg(render_priv->library, MSGL_DBG2, "single c/a at %f: %c%c = %X",
|
|
724 pwr, n, cmd, render_priv->state.c[cidx]);
|
|
725 } else if (mystrcmp(&p, "r")) {
|
|
726 reset_render_context(render_priv);
|
|
727 } else if (mystrcmp(&p, "be")) {
|
|
728 int val;
|
|
729 if (mystrtoi(&p, &val)) {
|
|
730 // Clamp to a safe upper limit, since high values need excessive CPU
|
|
731 val = (val < 0) ? 0 : val;
|
|
732 val = (val > MAX_BE) ? MAX_BE : val;
|
|
733 render_priv->state.be = val;
|
|
734 } else
|
|
735 render_priv->state.be = 0;
|
|
736 } else if (mystrcmp(&p, "b")) {
|
|
737 int b;
|
|
738 if (mystrtoi(&p, &b)) {
|
|
739 if (pwr >= .5)
|
|
740 render_priv->state.bold = b;
|
|
741 } else
|
|
742 render_priv->state.bold = render_priv->state.style->Bold;
|
|
743 update_font(render_priv);
|
|
744 } else if (mystrcmp(&p, "i")) {
|
|
745 int i;
|
|
746 if (mystrtoi(&p, &i)) {
|
|
747 if (pwr >= .5)
|
|
748 render_priv->state.italic = i;
|
|
749 } else
|
|
750 render_priv->state.italic = render_priv->state.style->Italic;
|
|
751 update_font(render_priv);
|
|
752 } else if (mystrcmp(&p, "kf") || mystrcmp(&p, "K")) {
|
|
753 int val = 0;
|
|
754 mystrtoi(&p, &val);
|
|
755 render_priv->state.effect_type = EF_KARAOKE_KF;
|
|
756 if (render_priv->state.effect_timing)
|
|
757 render_priv->state.effect_skip_timing +=
|
|
758 render_priv->state.effect_timing;
|
|
759 render_priv->state.effect_timing = val * 10;
|
|
760 } else if (mystrcmp(&p, "ko")) {
|
|
761 int val = 0;
|
|
762 mystrtoi(&p, &val);
|
|
763 render_priv->state.effect_type = EF_KARAOKE_KO;
|
|
764 if (render_priv->state.effect_timing)
|
|
765 render_priv->state.effect_skip_timing +=
|
|
766 render_priv->state.effect_timing;
|
|
767 render_priv->state.effect_timing = val * 10;
|
|
768 } else if (mystrcmp(&p, "k")) {
|
|
769 int val = 0;
|
|
770 mystrtoi(&p, &val);
|
|
771 render_priv->state.effect_type = EF_KARAOKE;
|
|
772 if (render_priv->state.effect_timing)
|
|
773 render_priv->state.effect_skip_timing +=
|
|
774 render_priv->state.effect_timing;
|
|
775 render_priv->state.effect_timing = val * 10;
|
|
776 } else if (mystrcmp(&p, "shad")) {
|
|
777 double val;
|
|
778 if (mystrtod(&p, &val)) {
|
|
779 if (render_priv->state.shadow_x == render_priv->state.shadow_y)
|
|
780 val = render_priv->state.shadow_x * (1 - pwr) + val * pwr;
|
|
781 } else
|
|
782 val = 0.;
|
|
783 render_priv->state.shadow_x = render_priv->state.shadow_y = val;
|
|
784 } else if (mystrcmp(&p, "s")) {
|
|
785 int val;
|
|
786 if (mystrtoi(&p, &val) && val)
|
|
787 render_priv->state.flags |= DECO_STRIKETHROUGH;
|
|
788 else
|
|
789 render_priv->state.flags &= ~DECO_STRIKETHROUGH;
|
|
790 } else if (mystrcmp(&p, "u")) {
|
|
791 int val;
|
|
792 if (mystrtoi(&p, &val) && val)
|
|
793 render_priv->state.flags |= DECO_UNDERLINE;
|
|
794 else
|
|
795 render_priv->state.flags &= ~DECO_UNDERLINE;
|
|
796 } else if (mystrcmp(&p, "pbo")) {
|
|
797 double val = 0;
|
|
798 if (mystrtod(&p, &val))
|
|
799 render_priv->state.drawing->pbo = val;
|
|
800 } else if (mystrcmp(&p, "p")) {
|
|
801 int val;
|
|
802 if (!mystrtoi(&p, &val))
|
|
803 val = 0;
|
|
804 if (val)
|
|
805 render_priv->state.drawing->scale = val;
|
|
806 render_priv->state.drawing_mode = !!val;
|
|
807 } else if (mystrcmp(&p, "q")) {
|
|
808 int val;
|
|
809 if (!mystrtoi(&p, &val))
|
|
810 val = render_priv->track->WrapStyle;
|
|
811 render_priv->state.wrap_style = val;
|
|
812 }
|
|
813
|
|
814 return p;
|
|
815 }
|
|
816
|
|
817 void apply_transition_effects(ASS_Renderer *render_priv, ASS_Event *event)
|
|
818 {
|
|
819 int v[4];
|
|
820 int cnt;
|
|
821 char *p = event->Effect;
|
|
822
|
|
823 if (!p || !*p)
|
|
824 return;
|
|
825
|
|
826 cnt = 0;
|
|
827 while (cnt < 4 && (p = strchr(p, ';'))) {
|
|
828 v[cnt++] = atoi(++p);
|
|
829 }
|
|
830
|
|
831 if (strncmp(event->Effect, "Banner;", 7) == 0) {
|
|
832 int delay;
|
|
833 if (cnt < 1) {
|
|
834 ass_msg(render_priv->library, MSGL_V,
|
|
835 "Error parsing effect: '%s'", event->Effect);
|
|
836 return;
|
|
837 }
|
|
838 if (cnt >= 2 && v[1] == 0) // right-to-left
|
|
839 render_priv->state.scroll_direction = SCROLL_RL;
|
|
840 else // left-to-right
|
|
841 render_priv->state.scroll_direction = SCROLL_LR;
|
|
842
|
|
843 delay = v[0];
|
|
844 if (delay == 0)
|
|
845 delay = 1; // ?
|
|
846 render_priv->state.scroll_shift =
|
|
847 (render_priv->time - render_priv->state.event->Start) / delay;
|
|
848 render_priv->state.evt_type = EVENT_HSCROLL;
|
|
849 return;
|
|
850 }
|
|
851
|
|
852 if (strncmp(event->Effect, "Scroll up;", 10) == 0) {
|
|
853 render_priv->state.scroll_direction = SCROLL_BT;
|
|
854 } else if (strncmp(event->Effect, "Scroll down;", 12) == 0) {
|
|
855 render_priv->state.scroll_direction = SCROLL_TB;
|
|
856 } else {
|
31853
|
857 ass_msg(render_priv->library, MSGL_DBG2,
|
30200
|
858 "Unknown transition effect: '%s'", event->Effect);
|
|
859 return;
|
|
860 }
|
|
861 // parse scroll up/down parameters
|
|
862 {
|
|
863 int delay;
|
|
864 int y0, y1;
|
|
865 if (cnt < 3) {
|
|
866 ass_msg(render_priv->library, MSGL_V,
|
|
867 "Error parsing effect: '%s'", event->Effect);
|
|
868 return;
|
|
869 }
|
|
870 delay = v[2];
|
|
871 if (delay == 0)
|
|
872 delay = 1; // ?
|
|
873 render_priv->state.scroll_shift =
|
|
874 (render_priv->time - render_priv->state.event->Start) / delay;
|
|
875 if (v[0] < v[1]) {
|
|
876 y0 = v[0];
|
|
877 y1 = v[1];
|
|
878 } else {
|
|
879 y0 = v[1];
|
|
880 y1 = v[0];
|
|
881 }
|
|
882 if (y1 == 0)
|
|
883 y1 = render_priv->track->PlayResY; // y0=y1=0 means fullscreen scrolling
|
|
884 render_priv->state.clip_y0 = y0;
|
|
885 render_priv->state.clip_y1 = y1;
|
|
886 render_priv->state.evt_type = EVENT_VSCROLL;
|
|
887 render_priv->state.detect_collisions = 0;
|
|
888 }
|
|
889
|
|
890 }
|
|
891
|
|
892 /**
|
|
893 * \brief Get next ucs4 char from string, parsing and executing style overrides
|
|
894 * \param str string pointer
|
|
895 * \return ucs4 code of the next char
|
|
896 * On return str points to the unparsed part of the string
|
|
897 */
|
|
898 unsigned get_next_char(ASS_Renderer *render_priv, char **str)
|
|
899 {
|
|
900 char *p = *str;
|
|
901 unsigned chr;
|
|
902 if (*p == '{') { // '\0' goes here
|
|
903 p++;
|
|
904 while (1) {
|
|
905 p = parse_tag(render_priv, p, 1.);
|
|
906 if (*p == '}') { // end of tag
|
|
907 p++;
|
|
908 if (*p == '{') {
|
|
909 p++;
|
|
910 continue;
|
|
911 } else
|
|
912 break;
|
|
913 } else if (*p != '\\')
|
|
914 ass_msg(render_priv->library, MSGL_V,
|
31853
|
915 "Unable to parse: '%.30s'", p);
|
30200
|
916 if (*p == 0)
|
|
917 break;
|
|
918 }
|
|
919 }
|
|
920 if (*p == '\t') {
|
|
921 ++p;
|
|
922 *str = p;
|
|
923 return ' ';
|
|
924 }
|
|
925 if (*p == '\\') {
|
|
926 if ((p[1] == 'N') || ((p[1] == 'n') &&
|
|
927 (render_priv->state.wrap_style == 2))) {
|
|
928 p += 2;
|
|
929 *str = p;
|
|
930 return '\n';
|
|
931 } else if (p[1] == 'n') {
|
|
932 p += 2;
|
|
933 *str = p;
|
|
934 return ' ';
|
|
935 } else if (p[1] == 'h') {
|
|
936 p += 2;
|
|
937 *str = p;
|
|
938 return NBSP;
|
31853
|
939 } else if (p[1] == '{') {
|
|
940 p += 2;
|
|
941 *str = p;
|
|
942 return '{';
|
|
943 } else if (p[1] == '}') {
|
|
944 p += 2;
|
|
945 *str = p;
|
|
946 return '}';
|
30200
|
947 }
|
|
948 }
|
|
949 chr = ass_utf8_get_char((char **) &p);
|
|
950 *str = p;
|
|
951 return chr;
|
|
952 }
|