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;
|
|
362 } else if (mystrcmp(&p, "fs")) {
|
|
363 double val;
|
|
364 if (mystrtod(&p, &val))
|
|
365 val = render_priv->state.font_size * (1 - pwr) + val * pwr;
|
|
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, "bord")) {
|
|
371 double val;
|
|
372 if (mystrtod(&p, &val)) {
|
|
373 if (render_priv->state.border_x == render_priv->state.border_y)
|
|
374 val = render_priv->state.border_x * (1 - pwr) + val * pwr;
|
|
375 } else
|
|
376 val = -1.; // reset to default
|
|
377 change_border(render_priv, val, val);
|
|
378 } else if (mystrcmp(&p, "move")) {
|
|
379 double x1, x2, y1, y2;
|
|
380 long long t1, t2, delta_t, t;
|
|
381 double x, y;
|
|
382 double k;
|
|
383 skip('(');
|
|
384 mystrtod(&p, &x1);
|
|
385 skip(',');
|
|
386 mystrtod(&p, &y1);
|
|
387 skip(',');
|
|
388 mystrtod(&p, &x2);
|
|
389 skip(',');
|
|
390 mystrtod(&p, &y2);
|
|
391 if (*p == ',') {
|
|
392 skip(',');
|
|
393 mystrtoll(&p, &t1);
|
|
394 skip(',');
|
|
395 mystrtoll(&p, &t2);
|
|
396 ass_msg(render_priv->library, MSGL_DBG2,
|
|
397 "movement6: (%f, %f) -> (%f, %f), (%" PRId64 " .. %"
|
|
398 PRId64 ")\n", x1, y1, x2, y2, (int64_t) t1,
|
|
399 (int64_t) t2);
|
|
400 } else {
|
|
401 t1 = 0;
|
|
402 t2 = render_priv->state.event->Duration;
|
|
403 ass_msg(render_priv->library, MSGL_DBG2,
|
|
404 "movement: (%f, %f) -> (%f, %f)", x1, y1, x2, y2);
|
|
405 }
|
|
406 skip(')');
|
|
407 delta_t = t2 - t1;
|
|
408 t = render_priv->time - render_priv->state.event->Start;
|
|
409 if (t < t1)
|
|
410 k = 0.;
|
|
411 else if (t > t2)
|
|
412 k = 1.;
|
|
413 else
|
|
414 k = ((double) (t - t1)) / delta_t;
|
|
415 x = k * (x2 - x1) + x1;
|
|
416 y = k * (y2 - y1) + y1;
|
|
417 if (render_priv->state.evt_type != EVENT_POSITIONED) {
|
|
418 render_priv->state.pos_x = x;
|
|
419 render_priv->state.pos_y = y;
|
|
420 render_priv->state.detect_collisions = 0;
|
|
421 render_priv->state.evt_type = EVENT_POSITIONED;
|
|
422 }
|
|
423 } else if (mystrcmp(&p, "frx")) {
|
|
424 double val;
|
|
425 if (mystrtod(&p, &val)) {
|
|
426 val *= M_PI / 180;
|
|
427 render_priv->state.frx =
|
|
428 val * pwr + render_priv->state.frx * (1 - pwr);
|
|
429 } else
|
|
430 render_priv->state.frx = 0.;
|
|
431 } else if (mystrcmp(&p, "fry")) {
|
|
432 double val;
|
|
433 if (mystrtod(&p, &val)) {
|
|
434 val *= M_PI / 180;
|
|
435 render_priv->state.fry =
|
|
436 val * pwr + render_priv->state.fry * (1 - pwr);
|
|
437 } else
|
|
438 render_priv->state.fry = 0.;
|
|
439 } else if (mystrcmp(&p, "frz") || mystrcmp(&p, "fr")) {
|
|
440 double val;
|
|
441 if (mystrtod(&p, &val)) {
|
|
442 val *= M_PI / 180;
|
|
443 render_priv->state.frz =
|
|
444 val * pwr + render_priv->state.frz * (1 - pwr);
|
|
445 } else
|
|
446 render_priv->state.frz =
|
|
447 M_PI * render_priv->state.style->Angle / 180.;
|
|
448 } else if (mystrcmp(&p, "fn")) {
|
|
449 char *start = p;
|
|
450 char *family;
|
|
451 skip_to('\\');
|
|
452 if (p > start) {
|
|
453 family = malloc(p - start + 1);
|
|
454 strncpy(family, start, p - start);
|
|
455 family[p - start] = '\0';
|
|
456 } else
|
|
457 family = strdup(render_priv->state.style->FontName);
|
31875
|
458 free(render_priv->state.family);
|
30200
|
459 render_priv->state.family = family;
|
|
460 update_font(render_priv);
|
|
461 } else if (mystrcmp(&p, "alpha")) {
|
|
462 uint32_t val;
|
|
463 int i;
|
|
464 int hex = render_priv->track->track_type == TRACK_TYPE_ASS;
|
|
465 if (strtocolor(render_priv->library, &p, &val, hex)) {
|
|
466 unsigned char a = val >> 24;
|
|
467 for (i = 0; i < 4; ++i)
|
|
468 change_alpha(&render_priv->state.c[i], a, pwr);
|
|
469 } else {
|
|
470 change_alpha(&render_priv->state.c[0],
|
|
471 render_priv->state.style->PrimaryColour, pwr);
|
|
472 change_alpha(&render_priv->state.c[1],
|
|
473 render_priv->state.style->SecondaryColour, pwr);
|
|
474 change_alpha(&render_priv->state.c[2],
|
|
475 render_priv->state.style->OutlineColour, pwr);
|
|
476 change_alpha(&render_priv->state.c[3],
|
|
477 render_priv->state.style->BackColour, pwr);
|
|
478 }
|
|
479 // FIXME: simplify
|
|
480 } else if (mystrcmp(&p, "an")) {
|
|
481 int val;
|
|
482 if (mystrtoi(&p, &val) && val) {
|
|
483 int v = (val - 1) / 3; // 0, 1 or 2 for vertical alignment
|
|
484 ass_msg(render_priv->library, MSGL_DBG2, "an %d", val);
|
|
485 if (v != 0)
|
|
486 v = 3 - v;
|
|
487 val = ((val - 1) % 3) + 1; // horizontal alignment
|
|
488 val += v * 4;
|
|
489 ass_msg(render_priv->library, MSGL_DBG2, "align %d", val);
|
|
490 render_priv->state.alignment = val;
|
|
491 } else
|
|
492 render_priv->state.alignment =
|
|
493 render_priv->state.style->Alignment;
|
|
494 } else if (mystrcmp(&p, "a")) {
|
|
495 int val;
|
|
496 if (mystrtoi(&p, &val) && val)
|
|
497 // take care of a vsfilter quirk: handle illegal \a8 like \a5
|
|
498 render_priv->state.alignment = (val == 8) ? 5 : val;
|
|
499 else
|
|
500 render_priv->state.alignment =
|
|
501 render_priv->state.style->Alignment;
|
|
502 } else if (mystrcmp(&p, "pos")) {
|
|
503 double v1, v2;
|
|
504 skip('(');
|
|
505 mystrtod(&p, &v1);
|
|
506 skip(',');
|
|
507 mystrtod(&p, &v2);
|
|
508 skip(')');
|
|
509 ass_msg(render_priv->library, MSGL_DBG2, "pos(%f, %f)", v1, v2);
|
|
510 if (render_priv->state.evt_type == EVENT_POSITIONED) {
|
|
511 ass_msg(render_priv->library, MSGL_V, "Subtitle has a new \\pos "
|
|
512 "after \\move or \\pos, ignoring");
|
|
513 } else {
|
|
514 render_priv->state.evt_type = EVENT_POSITIONED;
|
|
515 render_priv->state.detect_collisions = 0;
|
|
516 render_priv->state.pos_x = v1;
|
|
517 render_priv->state.pos_y = v2;
|
|
518 }
|
|
519 } else if (mystrcmp(&p, "fad")) {
|
|
520 int a1, a2, a3;
|
|
521 long long t1, t2, t3, t4;
|
|
522 if (*p == 'e')
|
|
523 ++p; // either \fad or \fade
|
|
524 skip('(');
|
|
525 mystrtoi(&p, &a1);
|
|
526 skip(',');
|
|
527 mystrtoi(&p, &a2);
|
|
528 if (*p == ')') {
|
|
529 // 2-argument version (\fad, according to specs)
|
|
530 // a1 and a2 are fade-in and fade-out durations
|
|
531 t1 = 0;
|
|
532 t4 = render_priv->state.event->Duration;
|
|
533 t2 = a1;
|
|
534 t3 = t4 - a2;
|
|
535 a1 = 0xFF;
|
|
536 a2 = 0;
|
|
537 a3 = 0xFF;
|
|
538 } else {
|
|
539 // 6-argument version (\fade)
|
|
540 // a1 and a2 (and a3) are opacity values
|
|
541 skip(',');
|
|
542 mystrtoi(&p, &a3);
|
|
543 skip(',');
|
|
544 mystrtoll(&p, &t1);
|
|
545 skip(',');
|
|
546 mystrtoll(&p, &t2);
|
|
547 skip(',');
|
|
548 mystrtoll(&p, &t3);
|
|
549 skip(',');
|
|
550 mystrtoll(&p, &t4);
|
|
551 }
|
|
552 skip(')');
|
|
553 render_priv->state.fade =
|
|
554 interpolate_alpha(render_priv->time -
|
|
555 render_priv->state.event->Start, t1, t2,
|
|
556 t3, t4, a1, a2, a3);
|
|
557 } else if (mystrcmp(&p, "org")) {
|
|
558 int v1, v2;
|
|
559 skip('(');
|
|
560 mystrtoi(&p, &v1);
|
|
561 skip(',');
|
|
562 mystrtoi(&p, &v2);
|
|
563 skip(')');
|
|
564 ass_msg(render_priv->library, MSGL_DBG2, "org(%d, %d)", v1, v2);
|
|
565 if (!render_priv->state.have_origin) {
|
|
566 render_priv->state.org_x = v1;
|
|
567 render_priv->state.org_y = v2;
|
|
568 render_priv->state.have_origin = 1;
|
|
569 render_priv->state.detect_collisions = 0;
|
|
570 }
|
|
571 } else if (mystrcmp(&p, "t")) {
|
|
572 double v[3];
|
|
573 int v1, v2;
|
|
574 double v3;
|
|
575 int cnt;
|
|
576 long long t1, t2, t, delta_t;
|
|
577 double k;
|
|
578 skip('(');
|
|
579 for (cnt = 0; cnt < 3; ++cnt) {
|
|
580 if (*p == '\\')
|
|
581 break;
|
31853
|
582 mystrtod(&p, &v[cnt]);
|
30200
|
583 skip(',');
|
|
584 }
|
|
585 if (cnt == 3) {
|
|
586 v1 = v[0];
|
|
587 v2 = (v[1] < v1) ? render_priv->state.event->Duration : v[1];
|
|
588 v3 = v[2];
|
|
589 } else if (cnt == 2) {
|
|
590 v1 = v[0];
|
|
591 v2 = (v[1] < v1) ? render_priv->state.event->Duration : v[1];
|
|
592 v3 = 1.;
|
|
593 } else if (cnt == 1) {
|
|
594 v1 = 0;
|
|
595 v2 = render_priv->state.event->Duration;
|
|
596 v3 = v[0];
|
|
597 } else { // cnt == 0
|
|
598 v1 = 0;
|
|
599 v2 = render_priv->state.event->Duration;
|
|
600 v3 = 1.;
|
|
601 }
|
|
602 render_priv->state.detect_collisions = 0;
|
|
603 t1 = v1;
|
|
604 t2 = v2;
|
|
605 delta_t = v2 - v1;
|
|
606 if (v3 < 0.)
|
|
607 v3 = 0.;
|
|
608 t = render_priv->time - render_priv->state.event->Start; // FIXME: move to render_context
|
|
609 if (t <= t1)
|
|
610 k = 0.;
|
|
611 else if (t >= t2)
|
|
612 k = 1.;
|
|
613 else {
|
|
614 assert(delta_t != 0.);
|
|
615 k = pow(((double) (t - t1)) / delta_t, v3);
|
|
616 }
|
|
617 while (*p == '\\')
|
|
618 p = parse_tag(render_priv, p, k); // maybe k*pwr ? no, specs forbid nested \t's
|
|
619 skip_to(')'); // in case there is some unknown tag or a comment
|
|
620 skip(')');
|
|
621 } else if (mystrcmp(&p, "clip")) {
|
|
622 char *start = p;
|
|
623 int x0, y0, x1, y1;
|
|
624 int res = 1;
|
|
625 skipopt('(');
|
|
626 res &= mystrtoi(&p, &x0);
|
|
627 skipopt(',');
|
|
628 res &= mystrtoi(&p, &y0);
|
|
629 skipopt(',');
|
|
630 res &= mystrtoi(&p, &x1);
|
|
631 skipopt(',');
|
|
632 res &= mystrtoi(&p, &y1);
|
|
633 skipopt(')');
|
|
634 if (res) {
|
|
635 render_priv->state.clip_x0 =
|
|
636 render_priv->state.clip_x0 * (1 - pwr) + x0 * pwr;
|
|
637 render_priv->state.clip_x1 =
|
|
638 render_priv->state.clip_x1 * (1 - pwr) + x1 * pwr;
|
|
639 render_priv->state.clip_y0 =
|
|
640 render_priv->state.clip_y0 * (1 - pwr) + y0 * pwr;
|
|
641 render_priv->state.clip_y1 =
|
|
642 render_priv->state.clip_y1 * (1 - pwr) + y1 * pwr;
|
|
643 // Might be a vector clip
|
|
644 } else if (!render_priv->state.clip_drawing) {
|
|
645 p = parse_vector_clip(render_priv, start);
|
|
646 render_priv->state.clip_drawing_mode = 0;
|
|
647 } else {
|
|
648 render_priv->state.clip_x0 = 0;
|
|
649 render_priv->state.clip_y0 = 0;
|
|
650 render_priv->state.clip_x1 = render_priv->track->PlayResX;
|
|
651 render_priv->state.clip_y1 = render_priv->track->PlayResY;
|
|
652 }
|
|
653 } else if (mystrcmp(&p, "c")) {
|
|
654 uint32_t val;
|
|
655 int hex = render_priv->track->track_type == TRACK_TYPE_ASS;
|
|
656 if (!strtocolor(render_priv->library, &p, &val, hex))
|
|
657 val = render_priv->state.style->PrimaryColour;
|
|
658 ass_msg(render_priv->library, MSGL_DBG2, "color: %X", val);
|
|
659 change_color(&render_priv->state.c[0], val, pwr);
|
|
660 } else if ((*p >= '1') && (*p <= '4') && (++p)
|
|
661 && (mystrcmp(&p, "c") || mystrcmp(&p, "a"))) {
|
|
662 char n = *(p - 2);
|
|
663 int cidx = n - '1';
|
|
664 char cmd = *(p - 1);
|
|
665 uint32_t val;
|
|
666 int hex = render_priv->track->track_type == TRACK_TYPE_ASS;
|
|
667 assert((n >= '1') && (n <= '4'));
|
|
668 if (!strtocolor(render_priv->library, &p, &val, hex))
|
|
669 switch (n) {
|
|
670 case '1':
|
|
671 val = render_priv->state.style->PrimaryColour;
|
|
672 break;
|
|
673 case '2':
|
|
674 val = render_priv->state.style->SecondaryColour;
|
|
675 break;
|
|
676 case '3':
|
|
677 val = render_priv->state.style->OutlineColour;
|
|
678 break;
|
|
679 case '4':
|
|
680 val = render_priv->state.style->BackColour;
|
|
681 break;
|
|
682 default:
|
|
683 val = 0;
|
|
684 break; // impossible due to assert; avoid compilation warning
|
|
685 }
|
|
686 switch (cmd) {
|
|
687 case 'c':
|
|
688 change_color(render_priv->state.c + cidx, val, pwr);
|
|
689 break;
|
|
690 case 'a':
|
|
691 change_alpha(render_priv->state.c + cidx, val >> 24, pwr);
|
|
692 break;
|
|
693 default:
|
|
694 ass_msg(render_priv->library, MSGL_WARN, "Bad command: %c%c",
|
|
695 n, cmd);
|
|
696 break;
|
|
697 }
|
|
698 ass_msg(render_priv->library, MSGL_DBG2, "single c/a at %f: %c%c = %X",
|
|
699 pwr, n, cmd, render_priv->state.c[cidx]);
|
|
700 } else if (mystrcmp(&p, "r")) {
|
|
701 reset_render_context(render_priv);
|
|
702 } else if (mystrcmp(&p, "be")) {
|
|
703 int val;
|
|
704 if (mystrtoi(&p, &val)) {
|
|
705 // Clamp to a safe upper limit, since high values need excessive CPU
|
|
706 val = (val < 0) ? 0 : val;
|
|
707 val = (val > MAX_BE) ? MAX_BE : val;
|
|
708 render_priv->state.be = val;
|
|
709 } else
|
|
710 render_priv->state.be = 0;
|
|
711 } else if (mystrcmp(&p, "b")) {
|
|
712 int b;
|
|
713 if (mystrtoi(&p, &b)) {
|
|
714 if (pwr >= .5)
|
|
715 render_priv->state.bold = b;
|
|
716 } else
|
|
717 render_priv->state.bold = render_priv->state.style->Bold;
|
|
718 update_font(render_priv);
|
|
719 } else if (mystrcmp(&p, "i")) {
|
|
720 int i;
|
|
721 if (mystrtoi(&p, &i)) {
|
|
722 if (pwr >= .5)
|
|
723 render_priv->state.italic = i;
|
|
724 } else
|
|
725 render_priv->state.italic = render_priv->state.style->Italic;
|
|
726 update_font(render_priv);
|
|
727 } else if (mystrcmp(&p, "kf") || mystrcmp(&p, "K")) {
|
|
728 int val = 0;
|
|
729 mystrtoi(&p, &val);
|
|
730 render_priv->state.effect_type = EF_KARAOKE_KF;
|
|
731 if (render_priv->state.effect_timing)
|
|
732 render_priv->state.effect_skip_timing +=
|
|
733 render_priv->state.effect_timing;
|
|
734 render_priv->state.effect_timing = val * 10;
|
|
735 } else if (mystrcmp(&p, "ko")) {
|
|
736 int val = 0;
|
|
737 mystrtoi(&p, &val);
|
|
738 render_priv->state.effect_type = EF_KARAOKE_KO;
|
|
739 if (render_priv->state.effect_timing)
|
|
740 render_priv->state.effect_skip_timing +=
|
|
741 render_priv->state.effect_timing;
|
|
742 render_priv->state.effect_timing = val * 10;
|
|
743 } else if (mystrcmp(&p, "k")) {
|
|
744 int val = 0;
|
|
745 mystrtoi(&p, &val);
|
|
746 render_priv->state.effect_type = EF_KARAOKE;
|
|
747 if (render_priv->state.effect_timing)
|
|
748 render_priv->state.effect_skip_timing +=
|
|
749 render_priv->state.effect_timing;
|
|
750 render_priv->state.effect_timing = val * 10;
|
|
751 } else if (mystrcmp(&p, "shad")) {
|
|
752 double val;
|
|
753 if (mystrtod(&p, &val)) {
|
|
754 if (render_priv->state.shadow_x == render_priv->state.shadow_y)
|
|
755 val = render_priv->state.shadow_x * (1 - pwr) + val * pwr;
|
|
756 } else
|
|
757 val = 0.;
|
|
758 render_priv->state.shadow_x = render_priv->state.shadow_y = val;
|
|
759 } else if (mystrcmp(&p, "s")) {
|
|
760 int val;
|
|
761 if (mystrtoi(&p, &val) && val)
|
|
762 render_priv->state.flags |= DECO_STRIKETHROUGH;
|
|
763 else
|
|
764 render_priv->state.flags &= ~DECO_STRIKETHROUGH;
|
|
765 } else if (mystrcmp(&p, "u")) {
|
|
766 int val;
|
|
767 if (mystrtoi(&p, &val) && val)
|
|
768 render_priv->state.flags |= DECO_UNDERLINE;
|
|
769 else
|
|
770 render_priv->state.flags &= ~DECO_UNDERLINE;
|
|
771 } else if (mystrcmp(&p, "pbo")) {
|
|
772 double val = 0;
|
|
773 if (mystrtod(&p, &val))
|
|
774 render_priv->state.drawing->pbo = val;
|
|
775 } else if (mystrcmp(&p, "p")) {
|
|
776 int val;
|
|
777 if (!mystrtoi(&p, &val))
|
|
778 val = 0;
|
|
779 if (val)
|
|
780 render_priv->state.drawing->scale = val;
|
|
781 render_priv->state.drawing_mode = !!val;
|
|
782 } else if (mystrcmp(&p, "q")) {
|
|
783 int val;
|
|
784 if (!mystrtoi(&p, &val))
|
|
785 val = render_priv->track->WrapStyle;
|
|
786 render_priv->state.wrap_style = val;
|
|
787 }
|
|
788
|
|
789 return p;
|
|
790 }
|
|
791
|
|
792 void apply_transition_effects(ASS_Renderer *render_priv, ASS_Event *event)
|
|
793 {
|
|
794 int v[4];
|
|
795 int cnt;
|
|
796 char *p = event->Effect;
|
|
797
|
|
798 if (!p || !*p)
|
|
799 return;
|
|
800
|
|
801 cnt = 0;
|
|
802 while (cnt < 4 && (p = strchr(p, ';'))) {
|
|
803 v[cnt++] = atoi(++p);
|
|
804 }
|
|
805
|
|
806 if (strncmp(event->Effect, "Banner;", 7) == 0) {
|
|
807 int delay;
|
|
808 if (cnt < 1) {
|
|
809 ass_msg(render_priv->library, MSGL_V,
|
|
810 "Error parsing effect: '%s'", event->Effect);
|
|
811 return;
|
|
812 }
|
|
813 if (cnt >= 2 && v[1] == 0) // right-to-left
|
|
814 render_priv->state.scroll_direction = SCROLL_RL;
|
|
815 else // left-to-right
|
|
816 render_priv->state.scroll_direction = SCROLL_LR;
|
|
817
|
|
818 delay = v[0];
|
|
819 if (delay == 0)
|
|
820 delay = 1; // ?
|
|
821 render_priv->state.scroll_shift =
|
|
822 (render_priv->time - render_priv->state.event->Start) / delay;
|
|
823 render_priv->state.evt_type = EVENT_HSCROLL;
|
|
824 return;
|
|
825 }
|
|
826
|
|
827 if (strncmp(event->Effect, "Scroll up;", 10) == 0) {
|
|
828 render_priv->state.scroll_direction = SCROLL_BT;
|
|
829 } else if (strncmp(event->Effect, "Scroll down;", 12) == 0) {
|
|
830 render_priv->state.scroll_direction = SCROLL_TB;
|
|
831 } else {
|
31853
|
832 ass_msg(render_priv->library, MSGL_DBG2,
|
30200
|
833 "Unknown transition effect: '%s'", event->Effect);
|
|
834 return;
|
|
835 }
|
|
836 // parse scroll up/down parameters
|
|
837 {
|
|
838 int delay;
|
|
839 int y0, y1;
|
|
840 if (cnt < 3) {
|
|
841 ass_msg(render_priv->library, MSGL_V,
|
|
842 "Error parsing effect: '%s'", event->Effect);
|
|
843 return;
|
|
844 }
|
|
845 delay = v[2];
|
|
846 if (delay == 0)
|
|
847 delay = 1; // ?
|
|
848 render_priv->state.scroll_shift =
|
|
849 (render_priv->time - render_priv->state.event->Start) / delay;
|
|
850 if (v[0] < v[1]) {
|
|
851 y0 = v[0];
|
|
852 y1 = v[1];
|
|
853 } else {
|
|
854 y0 = v[1];
|
|
855 y1 = v[0];
|
|
856 }
|
|
857 if (y1 == 0)
|
|
858 y1 = render_priv->track->PlayResY; // y0=y1=0 means fullscreen scrolling
|
|
859 render_priv->state.clip_y0 = y0;
|
|
860 render_priv->state.clip_y1 = y1;
|
|
861 render_priv->state.evt_type = EVENT_VSCROLL;
|
|
862 render_priv->state.detect_collisions = 0;
|
|
863 }
|
|
864
|
|
865 }
|
|
866
|
|
867 /**
|
|
868 * \brief Get next ucs4 char from string, parsing and executing style overrides
|
|
869 * \param str string pointer
|
|
870 * \return ucs4 code of the next char
|
|
871 * On return str points to the unparsed part of the string
|
|
872 */
|
|
873 unsigned get_next_char(ASS_Renderer *render_priv, char **str)
|
|
874 {
|
|
875 char *p = *str;
|
|
876 unsigned chr;
|
|
877 if (*p == '{') { // '\0' goes here
|
|
878 p++;
|
|
879 while (1) {
|
|
880 p = parse_tag(render_priv, p, 1.);
|
|
881 if (*p == '}') { // end of tag
|
|
882 p++;
|
|
883 if (*p == '{') {
|
|
884 p++;
|
|
885 continue;
|
|
886 } else
|
|
887 break;
|
|
888 } else if (*p != '\\')
|
|
889 ass_msg(render_priv->library, MSGL_V,
|
31853
|
890 "Unable to parse: '%.30s'", p);
|
30200
|
891 if (*p == 0)
|
|
892 break;
|
|
893 }
|
|
894 }
|
|
895 if (*p == '\t') {
|
|
896 ++p;
|
|
897 *str = p;
|
|
898 return ' ';
|
|
899 }
|
|
900 if (*p == '\\') {
|
|
901 if ((p[1] == 'N') || ((p[1] == 'n') &&
|
|
902 (render_priv->state.wrap_style == 2))) {
|
|
903 p += 2;
|
|
904 *str = p;
|
|
905 return '\n';
|
|
906 } else if (p[1] == 'n') {
|
|
907 p += 2;
|
|
908 *str = p;
|
|
909 return ' ';
|
|
910 } else if (p[1] == 'h') {
|
|
911 p += 2;
|
|
912 *str = p;
|
|
913 return NBSP;
|
31853
|
914 } else if (p[1] == '{') {
|
|
915 p += 2;
|
|
916 *str = p;
|
|
917 return '{';
|
|
918 } else if (p[1] == '}') {
|
|
919 p += 2;
|
|
920 *str = p;
|
|
921 return '}';
|
30200
|
922 }
|
|
923 }
|
|
924 chr = ass_utf8_get_char((char **) &p);
|
|
925 *str = p;
|
|
926 return chr;
|
|
927 }
|