summaryrefslogtreecommitdiffstats
path: root/libass/ass_parse.c
blob: 47d22e6643103dc73a436f8e80b30474c793f09f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
/*
 * Copyright (C) 2009 Grigori Goronzy <greg@geekmind.org>
 *
 * This file is part of libass.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "ass_render.h"
#include "ass_parse.h"

#define MAX_VALID_NARGS 7
#define MAX_BE 127
#define NBSP 0xa0   // unicode non-breaking space character

struct arg {
    char *start, *end;
};

static inline int argtoi(struct arg arg)
{
    int value;
    mystrtoi(&arg.start, &value);
    return value;
}

static inline long long argtoll(struct arg arg)
{
    long long value;
    mystrtoll(&arg.start, &value);
    return value;
}

static inline double argtod(struct arg arg)
{
    double value;
    mystrtod(&arg.start, &value);
    return value;
}

static inline void push_arg(struct arg *args, int *nargs, char *start, char *end)
{
    if (*nargs <= MAX_VALID_NARGS) {
        rskip_spaces(&end, start);
        if (end > start) {
            args[*nargs] = (struct arg) {start, end};
            ++*nargs;
        }
    }
}

/**
 * \brief Check if starting part of (*p) matches sample.
 * If true, shift p to the first symbol after the matching part.
 */
static inline int mystrcmp(char **p, const char *sample)
{
    int len = strlen(sample);
    if (strncmp(*p, sample, len) == 0) {
        (*p) += len;
        return 1;
    } else
        return 0;
}

double ensure_font_size(ASS_Renderer *priv, double size)
{
    if (size < 1)
        size = 1;
    else if (size > priv->height * 2)
        size = priv->height * 2;

    return size;
}

static void change_font_size(ASS_Renderer *render_priv, double sz)
{
    render_priv->state.font_size = sz;
}

/**
 * \brief Change current font, using setting from render_priv->state.
 */
void update_font(ASS_Renderer *render_priv)
{
    unsigned val;
    ASS_FontDesc desc;
    desc.treat_family_as_pattern = render_priv->state.treat_family_as_pattern;

    if (render_priv->state.family[0] == '@') {
        desc.vertical = 1;
        desc.family = strdup(render_priv->state.family + 1);
    } else {
        desc.vertical = 0;
        desc.family = strdup(render_priv->state.family);
    }

    val = render_priv->state.bold;
    // 0 = normal, 1 = bold, >1 = exact weight
    if (val == 1)
        val = 200;              // bold
    else if (val <= 0)
        val = 80;               // normal
    desc.bold = val;

    val = render_priv->state.italic;
    if (val == 1)
        val = 110;              // italic
    else if (val <= 0)
        val = 0;                // normal
    desc.italic = val;

    render_priv->state.font =
        ass_font_new(render_priv->cache.font_cache, render_priv->library,
                     render_priv->ftlibrary, render_priv->fontconfig_priv,
                     &desc);
    free(desc.family);

    if (render_priv->state.font)
        change_font_size(render_priv, render_priv->state.font_size);
}

/**
 * \brief Change border width
 *
 * \param render_priv renderer state object
 * \param info glyph state object
 */
void change_border(ASS_Renderer *render_priv, double border_x, double border_y)
{
    int bord = 64 * border_x * render_priv->border_scale;

    if (bord > 0 && border_x == border_y) {
        if (!render_priv->state.stroker) {
            int error;
            error =
                FT_Stroker_New(render_priv->ftlibrary,
                               &render_priv->state.stroker);
            if (error) {
                ass_msg(render_priv->library, MSGL_V,
                        "failed to get stroker");
                render_priv->state.stroker = 0;
            }
            render_priv->state.stroker_radius = -1.0;
        }
        if (render_priv->state.stroker && render_priv->state.stroker_radius != bord) {
            FT_Stroker_Set(render_priv->state.stroker, bord,
                           FT_STROKER_LINECAP_ROUND,
                           FT_STROKER_LINEJOIN_ROUND, 0);
            render_priv->state.stroker_radius = bord;
        }
    } else {
        FT_Stroker_Done(render_priv->state.stroker);
        render_priv->state.stroker = 0;
    }
}

/**
 * \brief Calculate a weighted average of two colors
 * calculates c1*(1-a) + c2*a, but separately for each component except alpha
 */
static void change_color(uint32_t *var, uint32_t new, double pwr)
{
    (*var) = ((uint32_t) (_r(*var) * (1 - pwr) + _r(new) * pwr) << 24) +
        ((uint32_t) (_g(*var) * (1 - pwr) + _g(new) * pwr) << 16) +
        ((uint32_t) (_b(*var) * (1 - pwr) + _b(new) * pwr) << 8) + _a(*var);
}

// like change_color, but for alpha component only
inline void change_alpha(uint32_t *var, uint32_t new, double pwr)
{
    *var =
        (_r(*var) << 24) + (_g(*var) << 16) + (_b(*var) << 8) +
        (uint32_t) (_a(*var) * (1 - pwr) + _a(new) * pwr);
}

/**
 * \brief Multiply two alpha values
 * \param a first value
 * \param b second value
 * \return result of multiplication
 * Parameters and result are limited by 0xFF.
 */
inline uint32_t mult_alpha(uint32_t a, uint32_t b)
{
    return 0xFF - (0xFF - a) * (0xFF - b) / 0xFF;
}

/**
 * \brief Calculate alpha value by piecewise linear function
 * Used for \fad, \fade implementation.
 */
static unsigned
interpolate_alpha(long long now, long long t1, long long t2, long long t3,
                  long long t4, unsigned a1, unsigned a2, unsigned a3)
{
    unsigned a;
    double cf;

    if (now < t1) {
        a = a1;
    } else if (now < t2) {
        cf = ((double) (now - t1)) / (t2 - t1);
        a = a1 * (1 - cf) + a2 * cf;
    } else if (now < t3) {
        a = a2;
    } else if (now < t4) {
        cf = ((double) (now - t3)) / (t4 - t3);
        a = a2 * (1 - cf) + a3 * cf;
    } else {                    // now >= t4
        a = a3;
    }

    return a;
}

/**
 * Parse a vector clip into an outline, using the proper scaling
 * parameters.  Translate it to correct for screen borders, if needed.
 */
static int parse_vector_clip(ASS_Renderer *render_priv,
                             struct arg *args, int nargs)
{
    int scale = 1;
    int res = 0;
    ASS_Drawing *drawing = render_priv->state.clip_drawing;
    struct arg text;

    if (nargs != 1 && nargs != 2)
        return 0;

    ass_drawing_free(drawing);
    render_priv->state.clip_drawing =
        ass_drawing_new(render_priv->library, render_priv->ftlibrary);
    drawing = render_priv->state.clip_drawing;
    if (nargs == 2)
        scale = argtoi(args[0]);
    drawing->scale = scale;
    drawing->scale_x = render_priv->font_scale_x * render_priv->font_scale;
    drawing->scale_y = render_priv->font_scale;
    text = args[nargs - 1];
    ass_drawing_add_chars(drawing, text.start, text.end - text.start);

    return 1;
}

/**
 * \brief Parse style override tag.
 * \param p string to parse
 * \param end end of string to parse, which must be '}' or ')'
 * \param pwr multiplier for some tag effects (comes from \t tags)
 */
char *parse_tag(ASS_Renderer *render_priv, char *p, char *end, double pwr)
{
    while (*p != '\\' && p != end)
        ++p;
    if (*p != '\\')
        return p;
    ++p;
    skip_spaces(&p);

    char *q = p;
    while (*q != '(' && *q != '\\' && q != end)
        ++q;
    if (q == p)
        return q;

    char *name_end = q;

    // Store one extra element to be able to detect excess arguments
    struct arg args[MAX_VALID_NARGS + 1];
    int nargs = 0;
    for (int i = 0; i <= MAX_VALID_NARGS; ++i)
        args[i].start = args[i].end = "";

    // Split parenthesized arguments. Do this for all tags and before
    // any non-parenthesized argument because that's what VSFilter does.
    if (*q == '(') {
        ++q;
        while (1) {
            skip_spaces(&q);

            // Split on commas. If there is a backslash, ignore any
            // commas following it and lump everything starting from
            // the last comma, through the backslash and all the way
            // to the end of the argument string into a single argument.

            char *r = q;
            while (*r != ',' && *r != '\\' && *r != ')' && r != end)
                ++r;

            if (*r == ',') {
                push_arg(args, &nargs, q, r);
                q = r + 1;
            } else {
                // Swallow the rest of the parenthesized string. This could
                // be either a backslash-argument or simply the last argument.
                while (*r != ')' && r != end)
                    ++r;
                push_arg(args, &nargs, q, r);
                q = r;
                // The closing parenthesis could be missing.
                if (*q == ')')
                    ++q;
                break;
            }
        }
    }

#define tag(name) (mystrcmp(&p, (name)) && (push_arg(args, &nargs, p, name_end), 1))
#define complex_tag(name) mystrcmp(&p, (name))

    // New tags introduced in vsfilter 2.39
    if (tag("xbord")) {
        double val;
        if (nargs) {
            val = argtod(*args);
            val = render_priv->state.border_x * (1 - pwr) + val * pwr;
            val = (val < 0) ? 0 : val;
        } else
            val = render_priv->state.style->Outline;
        render_priv->state.border_x = val;
    } else if (tag("ybord")) {
        double val;
        if (nargs) {
            val = argtod(*args);
            val = render_priv->state.border_y * (1 - pwr) + val * pwr;
            val = (val < 0) ? 0 : val;
        } else
            val = render_priv->state.style->Outline;
        render_priv->state.border_y = val;
    } else if (tag("xshad")) {
        double val;
        if (nargs) {
            val = argtod(*args);
            val = render_priv->state.shadow_x * (1 - pwr) + val * pwr;
        } else
            val = render_priv->state.style->Shadow;
        render_priv->state.shadow_x = val;
    } else if (tag("yshad")) {
        double val;
        if (nargs) {
            val = argtod(*args);
            val = render_priv->state.shadow_y * (1 - pwr) + val * pwr;
        } else
            val = render_priv->state.style->Shadow;
        render_priv->state.shadow_y = val;
    } else if (tag("fax")) {
        double val;
        if (nargs) {
            val = argtod(*args);
            render_priv->state.fax =
                val * pwr + render_priv->state.fax * (1 - pwr);
        } else
            render_priv->state.fax = 0.;
    } else if (tag("fay")) {
        double val;
        if (nargs) {
            val = argtod(*args);
            render_priv->state.fay =
                val * pwr + render_priv->state.fay * (1 - pwr);
        } else
            render_priv->state.fay = 0.;
    } else if (complex_tag("iclip")) {
        if (nargs == 4) {
            int x0, y0, x1, y1;
            x0 = argtoi(args[0]);
            y0 = argtoi(args[1]);
            x1 = argtoi(args[2]);
            y1 = argtoi(args[3]);
            render_priv->state.clip_x0 =
                render_priv->state.clip_x0 * (1 - pwr) + x0 * pwr;
            render_priv->state.clip_x1 =
                render_priv->state.clip_x1 * (1 - pwr) + x1 * pwr;
            render_priv->state.clip_y0 =
                render_priv->state.clip_y0 * (1 - pwr) + y0 * pwr;
            render_priv->state.clip_y1 =
                render_priv->state.clip_y1 * (1 - pwr) + y1 * pwr;
            render_priv->state.clip_mode = 1;
        } else if (!render_priv->state.clip_drawing) {
            if (parse_vector_clip(render_priv, args, nargs))
                render_priv->state.clip_drawing_mode = 1;
        }
    } else if (tag("blur")) {
        double val;
        if (nargs) {
            val = argtod(*args);
            val = render_priv->state.blur * (1 - pwr) + val * pwr;
            val = (val < 0) ? 0 : val;
            val = (val > BLUR_MAX_RADIUS) ? BLUR_MAX_RADIUS : val;
            render_priv->state.blur = val;
        } else
            render_priv->state.blur = 0.0;
        // ASS standard tags
    } else if (tag("fscx")) {
        double val;
        if (nargs) {
            val = argtod(*args) / 100;
            val = render_priv->state.scale_x * (1 - pwr) + val * pwr;
            val = (val < 0) ? 0 : val;
        } else
            val = render_priv->state.style->ScaleX;
        render_priv->state.scale_x = val;
    } else if (tag("fscy")) {
        double val;
        if (nargs) {
            val = argtod(*args) / 100;
            val = render_priv->state.scale_y * (1 - pwr) + val * pwr;
            val = (val < 0) ? 0 : val;
        } else
            val = render_priv->state.style->ScaleY;
        render_priv->state.scale_y = val;
    } else if (tag("fsc")) {
        render_priv->state.scale_x = render_priv->state.style->ScaleX;
        render_priv->state.scale_y = render_priv->state.style->ScaleY;
    } else if (tag("fsp")) {
        double val;
        if (nargs) {
            val = argtod(*args);
            render_priv->state.hspacing =
                render_priv->state.hspacing * (1 - pwr) + val * pwr;
        } else
            render_priv->state.hspacing = render_priv->state.style->Spacing;
    } else if (tag("fs")) {
        double val = 0;
        if (nargs) {
            val = argtod(*args);
            if (*args->start == '+' || *args->start == '-')
                val = render_priv->state.font_size * (1 + pwr * val / 10);
            else
                val = render_priv->state.font_size * (1 - pwr) + val * pwr;
        }
        if (val <= 0)
            val = render_priv->state.style->FontSize;
        if (render_priv->state.font)
            change_font_size(render_priv, val);
    } else if (tag("bord")) {
        double val, xval, yval;
        if (nargs) {
            val = argtod(*args);
            xval = render_priv->state.border_x * (1 - pwr) + val * pwr;
            yval = render_priv->state.border_y * (1 - pwr) + val * pwr;
            xval = (xval < 0) ? 0 : xval;
            yval = (yval < 0) ? 0 : yval;
        } else
            xval = yval = render_priv->state.style->Outline;
        render_priv->state.border_x = xval;
        render_priv->state.border_y = yval;
    } else if (complex_tag("move")) {
        double x1, x2, y1, y2;
        long long t1, t2, delta_t, t;
        double x, y;
        double k;
        if (nargs == 4 || nargs == 6) {
            x1 = argtod(args[0]);
            y1 = argtod(args[1]);
            x2 = argtod(args[2]);
            y2 = argtod(args[3]);
            t1 = t2 = 0;
            if (nargs == 6) {
                t1 = argtoll(args[4]);
                t2 = argtoll(args[5]);
                if (t1 > t2) {
                    long long tmp = t2;
                    t2 = t1;
                    t1 = tmp;
                }
            }
        } else
            return q;
        if (t1 <= 0 && t2 <= 0) {
            t1 = 0;
            t2 = render_priv->state.event->Duration;
        }
        delta_t = t2 - t1;
        t = render_priv->time - render_priv->state.event->Start;
        if (t <= t1)
            k = 0.;
        else if (t >= t2)
            k = 1.;
        else
            k = ((double) (t - t1)) / delta_t;
        x = k * (x2 - x1) + x1;
        y = k * (y2 - y1) + y1;
        if (render_priv->state.evt_type != EVENT_POSITIONED) {
            render_priv->state.pos_x = x;
            render_priv->state.pos_y = y;
            render_priv->state.detect_collisions = 0;
            render_priv->state.evt_type = EVENT_POSITIONED;
        }
    } else if (tag("frx")) {
        double val;
        if (nargs) {
            val = argtod(*args);
            val *= M_PI / 180;
            render_priv->state.frx =
                val * pwr + render_priv->state.frx * (1 - pwr);
        } else
            render_priv->state.frx = 0.;
    } else if (tag("fry")) {
        double val;
        if (nargs) {
            val = argtod(*args);
            val *= M_PI / 180;
            render_priv->state.fry =
                val * pwr + render_priv->state.fry * (1 - pwr);
        } else
            render_priv->state.fry = 0.;
    } else if (tag("frz") || tag("fr")) {
        double val;
        if (nargs) {
            val = argtod(*args);
            val *= M_PI / 180;
            render_priv->state.frz =
                val * pwr + render_priv->state.frz * (1 - pwr);
        } else
            render_priv->state.frz =
                M_PI * render_priv->state.style->Angle / 180.;
    } else if (tag("fn")) {
        char *family;
        char *start = args->start;
        end = args->end;
        if (nargs && strncmp(start, "0", end - start)) {
            skip_spaces(&start);
            family = malloc(end - start + 1);
            strncpy(family, start, end - start);
            family[end - start] = '\0';
        } else
            family = strdup(render_priv->state.style->FontName);
        free(render_priv->state.family);
        render_priv->state.family = family;
        update_font(render_priv);
    } else if (tag("alpha")) {
        uint32_t val;
        int i;
        int hex = render_priv->track->track_type == TRACK_TYPE_ASS;
        if (nargs) {
            val = string2color(render_priv->library, args->start, hex);
            unsigned char a = val >> 24;
            for (i = 0; i < 4; ++i)
                change_alpha(&render_priv->state.c[i], a, pwr);
        } else {
            change_alpha(&render_priv->state.c[0],
                         render_priv->state.style->PrimaryColour, 1);
            change_alpha(&render_priv->state.c[1],
                         render_priv->state.style->SecondaryColour, 1);
            change_alpha(&render_priv->state.c[2],
                         render_priv->state.style->OutlineColour, 1);
            change_alpha(&render_priv->state.c[3],
                         render_priv->state.style->BackColour, 1);
        }
        // FIXME: simplify
    } else if (tag("an")) {
        int val = argtoi(*args);
        if ((render_priv->state.parsed_tags & PARSED_A) == 0) {
            if (val >= 1 && val <= 9) {
                int v = (val - 1) / 3;      // 0, 1 or 2 for vertical alignment
                if (v != 0)
                    v = 3 - v;
                val = ((val - 1) % 3) + 1;  // horizontal alignment
                val += v * 4;
                render_priv->state.alignment = val;
            } else
                render_priv->state.alignment =
                    render_priv->state.style->Alignment;
            render_priv->state.parsed_tags |= PARSED_A;
        }
    } else if (tag("a")) {
        int val = argtoi(*args);
        if ((render_priv->state.parsed_tags & PARSED_A) == 0) {
            if (val >= 1 && val <= 11)
                // take care of a vsfilter quirk:
                // handle illegal \a8 and \a4 like \a5
                render_priv->state.alignment = ((val & 3) == 0) ? 5 : val;
            else
                render_priv->state.alignment =
                    render_priv->state.style->Alignment;
            render_priv->state.parsed_tags |= PARSED_A;
        }
    } else if (complex_tag("pos")) {
        double v1, v2;
        if (nargs == 2) {
            v1 = argtod(args[0]);
            v2 = argtod(args[1]);
        } else
            return q;
        if (render_priv->state.evt_type == EVENT_POSITIONED) {
            ass_msg(render_priv->library, MSGL_V, "Subtitle has a new \\pos "
                   "after \\move or \\pos, ignoring");
        } else {
            render_priv->state.evt_type = EVENT_POSITIONED;
            render_priv->state.detect_collisions = 0;
            render_priv->state.pos_x = v1;
            render_priv->state.pos_y = v2;
        }
    } else if (complex_tag("fade") || complex_tag("fad")) {
        int a1, a2, a3;
        long long t1, t2, t3, t4;
        if (nargs == 2) {
            // 2-argument version (\fad, according to specs)
            a1 = 0xFF;
            a2 = 0;
            a3 = 0xFF;
            t1 = -1;
            t2 = argtoll(args[0]);
            t3 = argtoll(args[1]);
            t4 = -1;
        } else if (nargs == 7) {
            // 7-argument version (\fade)
            a2 = argtoi(args[0]);
            a3 = argtoi(args[1]);
            a3 = argtoi(args[2]);
            t1 = argtoll(args[3]);
            t2 = argtoll(args[4]);
            t3 = argtoll(args[5]);
            t4 = argtoll(args[6]);
        } else
            return q;
        if (t1 == -1 && t4 == -1) {
            t1 = 0;
            t4 = render_priv->state.event->Duration;
            t3 = t4 - t3;
        }
        if ((render_priv->state.parsed_tags & PARSED_FADE) == 0) {
            render_priv->state.fade =
                interpolate_alpha(render_priv->time -
                        render_priv->state.event->Start, t1, t2,
                        t3, t4, a1, a2, a3);
            render_priv->state.parsed_tags |= PARSED_FADE;
        }
    } else if (complex_tag("org")) {
        double v1, v2;
        if (nargs == 2) {
            v1 = argtod(args[0]);
            v2 = argtod(args[1]);
        } else
            return q;
        if (!render_priv->state.have_origin) {
            render_priv->state.org_x = v1;
            render_priv->state.org_y = v2;
            render_priv->state.have_origin = 1;
            render_priv->state.detect_collisions = 0;
        }
    } else if (complex_tag("t")) {
        double v[3];
        double accel;
        int cnt = nargs - 1;
        long long t1, t2, t, delta_t;
        double k;
        if (cnt == 3) {
            t1 = argtoll(args[0]);
            t2 = argtoll(args[1]);
            accel = argtod(args[2]);
        } else if (cnt == 2) {
            t1 = argtoll(args[0]);
            t2 = argtoll(args[1]);
            accel = 1.;
        } else if (cnt == 1) {
            t1 = 0;
            t2 = 0;
            accel = argtod(args[0]);
        } else if (cnt == 0) {
            t1 = 0;
            t2 = 0;
            accel = 1.;
        } else
            return q;
        render_priv->state.detect_collisions = 0;
        if (t2 == 0)
            t2 = render_priv->state.event->Duration;
        delta_t = t2 - t1;
        t = render_priv->time - render_priv->state.event->Start;        // FIXME: move to render_context
        if (t <= t1)
            k = 0.;
        else if (t >= t2)
            k = 1.;
        else {
            assert(delta_t != 0.);
            k = pow(((double) (t - t1)) / delta_t, accel);
        }
        p = args[cnt].start;
        while (p < args[cnt]