-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.c
1323 lines (1227 loc) · 32.2 KB
/
main.c
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
/*
* Project: svg2ass
* File: main.c
* Created: 2014-10-23
* Author: Urban Wallasch
*
* See LICENSE file for more details.
*
* TODO:
* - Use cubic Bezier curves for elliptical path arcs
* - Epsilon optimizations for path
* - Improve error handling/propagation
* - Implement clippath
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <float.h>
#include <math.h>
#include <strings.h>
#include <unistd.h>
#include "nxml.h"
#include "colors.h"
#include "vect.h"
#include "version.h"
#ifdef DEBUG
#include <assert.h>
#define WHOAMI() fprintf( stderr, "%s:%d:%s\n", __FILE__, __LINE__, __func__ )
#define DPRINT(...) do { fprintf( stderr, __VA_ARGS__ ); } while(0)
#define IPRINT(...) do { fprintf( stderr, "%*s", (int)(stacktop * 4), "" ); \
fprintf( stderr, __VA_ARGS__ ); } while(0)
#else
#define assert(...)
#define WHOAMI()
#define DPRINT(...)
#define IPRINT(...)
#endif
/************************************************************
* Math constants and helper
*/
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define DEG2RAD(D) ((double)(D)*M_PI/180.0)
#define RAD2DEG(R) ((double)(R)*180.0/M_PI)
/*
* Optimized cubic Bezier curve ellipse approximation factor.
* Ref: http://spencermortensen.com/articles/bezier-circle/
*/
#define BEZIER_CIRC 0.551915024494
/*
* For the elliptical arc approximation we generate one line segment
* per specified units of estimated arc length.
*/
#define DFLT_ARCLINE 4.0
/*
* Dimensions smaller than EPSILON are treated as zero by select
* operations to enable some trivial shortcuts and optimizations.
* Chose a value considerably smaller than 1 to allow for upscaling
* without introducing massive errors!
*/
#define DFLT_EPSILON 0.001
/************************************************************
* Config and helper
*/
#define MAX_FPREC 5
static struct {
int ass_mode;
int ass_fprec;
int ass_layer;
int ass_scale_exp;
int ass_scale;
const char *ass_style; // style name for event prefix
const char *ass_actor; // actor name for event prefix
const char *ass_start; // start time
const char *ass_end; // end time
double epsilon;
double arcline;
FILE *of;
const char *progname;
} config = {
1,
1,
0,
1,
1,
"Default",
"",
"0:00:00.00",
"0:00:01.00",
DFLT_EPSILON,
DFLT_ARCLINE,
NULL,
"svg2ass",
};
enum {
ELVL_INFO,
ELVL_WARNING,
ELVL_ERROR,
ELVL_FATAL,
};
static int usage( const char *progname, int version_only );
static int err( int lvl, int usg, const char *fmt, ... )
{
char *m = "";
va_list arglist;
va_start( arglist, fmt );
switch ( lvl )
{
case ELVL_INFO: break;
case ELVL_WARNING: m = "WARNING: "; break;
case ELVL_ERROR: /* no break */
default: m = "ERROR: "; break;
}
fputs( m, stderr );
vfprintf( stderr, fmt, arglist );
fputs( "\n", stderr );
if ( usg )
usage( config.progname, 0 );
va_end( arglist );
if ( ELVL_FATAL <= lvl )
exit( EXIT_FAILURE );
return 0;
}
/************************************************************
* Context stack
*/
#define CTX_STACKSZ_INC 100
typedef struct {
int in_svg;
vec_t org; // origin
mtx_t ctm; // current transformation matrix
unsigned f_col; // fill color
unsigned f_alpha; // fill aplpha
unsigned s_col; // stroke color
unsigned s_alpha; // stroke alpha
double s_width; // stroke width
} ctx_t;
static ctx_t *stack = NULL;
static size_t stacksz = 0;
static size_t stacktop = 0;
static int ctx_push( ctx_t *ctx )
{
if ( stacktop + 1 > stacksz )
{
ctx_t *p;
if ( NULL == ( p = realloc( stack, ( stacksz + CTX_STACKSZ_INC ) * sizeof *stack ) ) )
return -1;
stack = p;
stacksz += CTX_STACKSZ_INC;
}
stack[stacktop++] = *ctx;
return 0;
}
static int ctx_pop( ctx_t *ctx )
{
if ( stacktop < 1 )
return -1;
*ctx = stack[--stacktop];
return 0;
}
/************************************************************
* ASS output generator
*/
#if 0
int emit( const char *fmt, ... )
{
int r;
va_list arglist;
va_start( arglist, fmt );
r = vfprintf( config.of, fmt, arglist );
va_end( arglist );
return r < 0 ? -1 : 0;
}
#else
#define emit(...) (0>fprintf(config.of,__VA_ARGS__)?-1:0)
#endif
/*
* Round floating point number to specified precision,
* strip trailing zero fractional component.
*/
static inline char *ftoa( char *buf, int prec, double d )
{
char *b;
sprintf( buf, "%0.*f", prec, d );
if ( strchr( buf, '.' ) )
{
b = buf + strlen( buf ) - 1;
while ( '0' == *b )
*b-- = '\0';
if ( '.' == *b )
*b = '\0';
}
return buf;
}
/*
* Formatted FP output for scalars and vector components, which are
* transforned using the current transformation matrix.
*/
int emitf( ctx_t *ctx, const char *fmt, ... )
{
int r = 0;
const char *p;
vec_t v;
va_list arglist;
static char buf[3 + DBL_MANT_DIG - DBL_MIN_EXP + 1];
va_start( arglist, fmt );
for ( p = fmt; *p && 0 == r; ++p )
{
if ( '%' == *p )
{
++p;
switch( tolower( *p ) )
{
case 'f':
r = emit( "%s", ftoa( buf, config.ass_fprec, va_arg( arglist, double ) ) );
break;
case 'v':
v = va_arg( arglist, vec_t );
v = vec_mmul( ctx->ctm, v );
v = vec_scal( v, config.ass_scale );
r = emit( "%s ", ftoa( buf, config.ass_fprec, v.x ) );
r = emit( "%s", ftoa( buf, config.ass_fprec, v.y ) );
break;
case '%':
r = emit( "%%" );
break;
default:
r = -1;
assert( 1 == 0 );
break;
}
}
else
r = emit( "%c", *p );
}
va_end( arglist );
return r;
}
enum {
ASS_COMMENT = -1,
ASS_CLOSE = 0,
ASS_START = 1,
};
/*
* Start / finalize ASS drawing line
*/
static inline int ass_line( ctx_t *ctx, int mode )
{
static int is_open = 0;
static int did_comment = 0;
if ( ASS_COMMENT == mode && !did_comment )
{
#if 0
// TODO: Aegisub is unable to handle pasted comment lines???
emit( "Comment: 0,%s,%s,%s,,0,0,0,,Generated by svg2ass %s-%s\n",
config.ass_start, config.ass_end, config.ass_style,
VERSION, SVNVER );
#endif
did_comment = 1;
}
else if ( ASS_START == mode && !is_open ) // start a new ASS line
{
//Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
emit( "Dialogue: %d,%s,%s,%s,%s,0,0,0,,", config.ass_layer,
config.ass_start, config.ass_end,
config.ass_style, config.ass_actor );
emit( "{\\an7\\1c&H%06X&\\1a&H%02X&\\3c&H%06X&\\3a&H%02X&",
ctx->f_col, ctx->f_alpha, ctx->s_col, ctx->s_alpha );
emitf( ctx, "\\bord%f\\shad0", ctx->s_width );
emit( "\\p%d}", config.ass_scale_exp );
is_open = 1;
++config.ass_layer;
}
else if ( ASS_CLOSE == mode && is_open ) // close ASS line
{
emit( "{\\p0}\n" );
is_open = 0;
}
return is_open;
}
/************************************************************
* Attribute parser
*/
static inline int findAttr( const nxmlNode_t *node, const char *name )
{
int a;
for ( a = 0; a < (int)node->att_num; ++a )
if ( 0 == strcasecmp( node->att[a].name, name ) )
return a;
return -1;
}
static inline double getNumericAttr( const nxmlNode_t *node, char *attr )
{
int a = findAttr( node, attr );
return ( 0 <= a ) ? strtod( node->att[a].val, NULL ) : 0.0;
}
static inline const char *getStringAttr( const nxmlNode_t *node, char *attr )
{
int a = findAttr( node, attr );
return ( 0 <= a ) ? node->att[a].val : NULL;
}
static inline const char *skip( const char *str, const char *skip )
{
while ( *str && strchr( skip, *str ) )
++str;
return str;
}
static int parseStyles( ctx_t *ctx, const nxmlNode_t *node )
{
unsigned nocol = 0;
const char *s;
// parse presentation attributes
IPRINT( "style (presentation attribute)\n" );
if ( NULL != ( s = getStringAttr( node, "fill" ) ) )
{
IPRINT( " fill=%s\n", s );
if ( strstr( s, "none" ) )
nocol |= 1;
else
{
nocol &= ~1;
ctx->f_col = convColorBGR( s );
}
}
if ( NULL != ( s = getStringAttr( node, "stroke" ) ) )
{
IPRINT( " stroke=%s\n", s );
if ( strstr( s, "none" ) )
nocol |= 2;
else
{
nocol &= ~2;
ctx->s_col = convColorBGR( s );
}
}
if ( NULL != ( s = getStringAttr( node, "fill-opacity" ) ) )
{
IPRINT( " fill-opacity=%s\n", s );
ctx->f_alpha = 255 - atof( s ) * 255;
}
if ( NULL != ( s = getStringAttr( node, "stroke-opacity" ) ) )
{
IPRINT( " stroke-opacity=%s\n", s );
ctx->s_alpha = 255 - atof( s ) * 255;
}
if ( NULL != ( s = getStringAttr( node, "stroke-width" ) ) && *s )
{
IPRINT( " stroke-width=%s\n", s );
ctx->s_width = atof( s );
}
// parse inline CSS
IPRINT( "style (inline CSS)\n" );
if ( NULL != ( s = getStringAttr( node, "style" ) ) && *s )
{
int n = 0;
char name[100];
char value[100];
while ( sscanf( s, " %99[^ :] : %99[^ ;] %n", name, value, &n ) == 2 )
{
s = skip( s + n, ";" );
IPRINT( " %s=%s\n", name, value );
if ( 0 == strcasecmp( name, "fill" ) )
{
if ( 0 == strcasecmp( value, "none" ) )
nocol |= 1;
else
{
nocol &= ~1;
ctx->f_col = convColorBGR( value );
}
}
else if ( 0 == strcasecmp( name, "stroke" ) )
{
if ( 0 == strcasecmp( value, "none" ) )
nocol |= 2;
else
{
nocol &= ~2;
ctx->s_col = convColorBGR( value );
}
}
else if ( 0 == strcasecmp( name, "fill-opacity" ) )
ctx->f_alpha = 255 - atof( value ) * 255;
else if ( 0 == strcasecmp( name, "stroke-opacity" ) )
ctx->s_alpha = 255 - atof( value ) * 255;
else if ( 0 == strcasecmp( name, "stroke-width" ) )
ctx->s_width = atof( value );
}
}
// a color set to "none" is emulated by setting full transparency
if ( nocol & 1 )
ctx->f_alpha = 255;
if ( nocol & 2 )
ctx->s_alpha = 255;
//IPRINT( " fill #%06x %u; stroke #%06x %u %g\n", ctx->f_col, ctx->f_alpha, ctx->s_col, ctx->s_alpha, ctx->s_width );
return 0;
}
static int parseTransform( ctx_t *ctx, const char *trf )
{
int a = 0, n;
const char *s = trf;
char op[100];
double phi;
mtx_t m;
if ( !s || !*s )
return 0;
IPRINT( "transform\n" );
while ( *s )
{
if ( sscanf( s, " %99[^ (]%n", op, &n ) != 1 || !*op )
break;
s += n;
m = MTX_UNI;
if ( 0 == strcasecmp( op, "translate" ) )
{
a = sscanf( s, " ( %lf %n", &m.e, &n );
if ( 1 == a )
{
s = skip( s + n, "," );
if ( 1 == sscanf( s, " %lf%n", &m.f, &n ) )
s += n;
}
}
else if ( 0 == strcasecmp( op, "scale" ) )
{
a = sscanf( s, " ( %lf %n", &m.a, &n );
if ( 1 == a )
{
s = skip( s + n, "," );
m.d = m.a;
if ( 1 == sscanf( s, " %lf%n", &m.d, &n ) )
s += n;
}
}
else if ( 0 == strcasecmp( op, "rotate" ) )
{
mtx_t r = MTX_UNI;
a = sscanf( s, " ( %lf%n", &phi, &n );
if ( 1 == a )
{
s += n;
phi = DEG2RAD(phi);
r.a = r.d = cos( phi );
r.b = sin( phi );
r.c = -r.b;
if ( 2 == sscanf( s, " , %lf, %lf%n", &m.e, &m.f, &n )
|| 2 == sscanf( s, " %lf %lf%n", &m.e, &m.f, &n ) )
{
s += n;
ctx->ctm = mtx_mmul( ctx->ctm, m );
ctx->ctm = mtx_mmul( ctx->ctm, r );
m.e = -m.e;
m.f = -m.f;
}
else
m = r;
}
}
else if ( 0 == strcasecmp( op, "skewX" ) )
{
a = sscanf( s, " ( %lf%n", &phi, &n );
if ( 1 == a )
{
s += n;
m.c = tan( DEG2RAD(phi) );
}
}
else if ( 0 == strcasecmp( op, "skewY" ) )
{
a = sscanf( s, " ( %lf%n", &phi, &n );
if ( 1 == a )
{
s += n;
m.b = tan( DEG2RAD(phi) );
}
}
else if ( 0 == strcasecmp( op, "matrix" ) )
{
if ( 6 == sscanf( s, " ( %lf , %lf , %lf , %lf , %lf , %lf%n",
&m.a, &m.b, &m.c, &m.d, &m.e, &m.f, &n )
|| 6 == sscanf( s, " ( %lf %lf %lf %lf %lf %lf%n",
&m.a, &m.b, &m.c, &m.d, &m.e, &m.f, &n ) )
{
a = 1;
s += n;
}
}
if ( 0 < a )
{
ctx->ctm = mtx_mmul( ctx->ctm, m );
IPRINT( " %s(%g,%g,%g,%g,%g,%g) --> CTM(%g,%g,%g,%g,%g,%g)\n",
op, m.a, m.b, m.c, m.d, m.e, m.f,
ctx->ctm.a, ctx->ctm.b, ctx->ctm.c,
ctx->ctm.d, ctx->ctm.e, ctx->ctm.f );
}
sscanf( s, "%*[ 0-9.)]%n", &n );
s += n;
}
return 0;
}
/************************************************************
* SVG parsing and ASS drawing
*/
static int ass_roundrect( ctx_t *ctx, vec_t o, vec_t d, vec_t r )
{
int res = 0;
vec_t c, v0, v1, v2, v3, rq;
int h_edge, v_edge;
if ( d.x / 2 < r.x )
r.x = d.x / 2;
if ( d.y / 2 < r.y )
r.y = d.y / 2;
if ( config.epsilon > r.x && config.epsilon > r.y ) // square corner shortcut
{
if ( config.epsilon > d.x && config.epsilon > d.y ) // tiny extent optimization
res = emitf( ctx, "m %v l %v ", o, VEC( o.x+config.epsilon, o.y ) );
else
res = emitf( ctx, "m %v l %v %v %v ", o, (vec_t){o.x+d.x, o.y},
vec_add( o, d ), (vec_t){o.x, o.y+d.y} );
return res;
}
// prepare parameters, move to start position
rq = vec_scal( r, BEZIER_CIRC );
h_edge = config.epsilon < ( d.x - 2 * r.x );
v_edge = config.epsilon < ( d.y - 2 * r.y );
v0.x = o.x + r.x; v0.y = o.y;
res += emitf( ctx, "m %v ", v0 );
if ( h_edge )
{ // upper edge
v0.x = o.x + d.x - r.x; v0.y = o.y;
res += emitf( ctx, "l %v ", v0 );
}
c.x = v0.x; c.y = v0.y + r.y;
v1.x = c.x + rq.x; v1.y = c.y - r.y;
v2.x = c.x + r.x; v2.y = c.y - rq.y;
v3.x = c.x + r.x; v3.y = c.y;
res += emitf( ctx, "b %v %v %v ", v1, v2, v3 );
if ( v_edge )
{ // right edge
v0.x = o.x + d.x; v0.y = o.y + d.y - r.y;
res += emitf( ctx, "l %v ", v0 );
}
else
v0 = v3;
c.x = v0.x - r.x; c.y = v0.y;
v1.x = c.x + r.x; v1.y = c.y + rq.y;
v2.x = c.x + rq.x; v2.y = c.y + r.y;
v3.x = c.x; v3.y = c.y + r.y;
res += emitf( ctx, "b %v %v %v ", v1, v2, v3 );
if ( h_edge )
{ // lower edge
v0.x = o.x + r.x; v0.y = o.y + d.y;
res += emitf( ctx, "l %v ", v0 );
}
else
v0 = v3;
c.x = v0.x; c.y = v0.y - r.y;
v1.x = c.x - rq.x; v1.y = c.y + r.y;
v2.x = c.x - r.x; v2.y = c.y + rq.y;
v3.x = c.x - r.x; v3.y = c.y;
res += emitf( ctx, "b %v %v %v ", v1, v2, v3 );
if ( v_edge )
{ // left edge
v0.x = o.x; v0.y = o.y + r.y;
res += emitf( ctx, "l %v ", v0 );
}
else
v0 = v3;
c.x = v0.x + r.x; c.y = v0.y;
v1.x = c.x - r.x; v1.y = c.y - rq.y;
v2.x = c.x - rq.x; v2.y = c.y - r.y;
v3.x = c.x; v3.y = c.y - r.y;
res += emitf( ctx, "b %v %v %v ", v1, v2, v3 );
return res;
}
static int ass_ellipse( ctx_t *ctx, vec_t c, vec_t r )
{
if ( config.epsilon > r.x && config.epsilon > r.y )
{ // tiny radius shortcut
return emitf( ctx, "m %v l %v ", c, vec_add( c, VEC(1,0) ) );
}
// Ellipses are basically just degenerate rounded rects.
return ass_roundrect( ctx, vec_sub( c, r ), vec_scal( r, 2.0 ), r );
}
static int ass_arc( ctx_t *ctx, vec_t v0, vec_t r, double phi, int fa, int fs, vec_t v )
{
// Draw an elliptical arc, Ref:
// http://www.w3.org/TR/SVG/implnote.html#ArcSyntax
// Validate and normalize parameters:
// F.6.2 drop arc, if endpoints identical
if ( vec_eq( v0, v, 0 ) )
return 0;
// F.6.6 Step 1: Ensure radii are non-zero (otherwise draw straight line)
if ( config.epsilon > r.x || config.epsilon > r.y || vec_eq( v0, v, config.epsilon ) )
return emitf( ctx, "l %v ", v );
// F.6.6 Step 2: Ensure radii are positive
r.x = fabs( r.x );
r.y = fabs( r.y );
// F.6.6 Step 3: Ensure radii are large enough
//////////////
// TODO: If rx, ry and φ are such that there is no solution
// (basically, the ellipse is not big enough to reach from (x1, y1)
// to (x2, y2)) then the ellipse is scaled up uniformly until
// there is exactly one solution (until the ellipse is just big enough).
//////////////
// F.6.2 rotation angle mod 360
phi = DEG2RAD( fmod( phi, 360.0 ) );
// F.6.2 normalize flags
fa = !!fa;
fs = !!fs;
// F.6.5 Conversion from endpoint to center parameterization, Ref:
// http://www.w3.org/TR/SVG/implnote.html#ArcConversionEndpointToCenter
double f, t, t1, dt;
vec_t p, cp, h1, h2;
vec_t c;
mtx_t rot = MTX( cos(phi), sin(phi), 0, -sin(phi), cos(phi), 0 );
// F.6.5 Step 1: Compute (x1′, y1′)
p = vec_mmul( rot, vec_scal( vec_sub( v0, v ), 0.5 ) );
// F.6.5 Step 2: Compute (cx′, cy′)
f = sqrt( fabs( (r.x*r.x*r.y*r.y - r.x*r.x*p.y*p.y - r.y*r.y*p.x*p.x)
/ (r.x*r.x*p.y*p.y + r.y*r.y*p.x*p.x) ) );
cp = vec_scal( VEC( r.x*p.y/r.y, -r.y*p.x/r.x ), fa==fs?-f:f );
// F.6.5 Step 3: Compute (cx, cy) from (cx′, cy′)
rot.b = -rot.b;
rot.c = -rot.c;
c = vec_add( vec_mmul( rot, cp ), vec_scal( vec_add( v0, v ), 0.5 ) );
// F.6.5 Step 4: Compute θ1 and Δθ
h1 = VEC( (p.x-cp.x)/r.x, (p.y-cp.y)/r.y );
h2 = VEC( (-p.x-cp.x)/r.x, (-p.y-cp.y)/r.y );
t1 = vec_ang( VEC(1,0), h1 );
dt = vec_ang( h1, h2 );
// Perform the sweep in specified direction and draw arc segments
double step = config.arcline * 2 / ( r.x + r.y );
// TODO: use bezier curves instead of lines
emit( "l " );
if ( fs )
{
if ( 0.0 > dt )
dt += M_PI*2;
//DPRINT( "fa=%d, fs=%d, t1=%g, dt=%g, step=%g\n", fa, fs, RAD2DEG(t1), RAD2DEG(dt), RAD2DEG(step) );
for ( t = 0.0; t < dt; t += step )
{
p = vec_add( vec_mmul( rot, VEC( r.x*cos(t1+t), r.y*sin(t1+t) ) ), c );
emitf( ctx, "%v ", p );
}
}
else
{
if ( 0.0 < dt )
dt -= M_PI*2;
//DPRINT( "fa=%d, fs=%d, t1=%g, dt=%g, step=%g\n", fa, fs, RAD2DEG(t1), RAD2DEG(dt), RAD2DEG(step) );
for ( t = 0.0; t > dt; t -= step )
{
p = vec_add( vec_mmul( rot, VEC( r.x*cos(t1+t), r.y*sin(t1+t) ) ), c );
emitf( ctx, "%v ", p );
}
}
return emitf( ctx, "%v", v );
}
/*
* Path parser logic shamelessly stolen from libsvgtiny:
* http://www.netsurf-browser.org/projects/libsvgtiny/
*/
static int ass_path( ctx_t *ctx, const char *pd )
{
int res = 0;
char *s, *d;
vec_t last = ctx->org;
vec_t last_cubic = last;
vec_t last_quad = last;
vec_t subpath_first = last;
if ( !pd || !*pd )
return 0;
/* obtain a clean copy of path */
if ( NULL == ( d = strdup( pd ) ) )
return -1;
for ( s = d; *s; ++s )
if ( ',' == *s )
*s = ' ';
for ( s = d; *s; )
{
int n;
char svg_cmd[2] = "";
vec_t v, v1, v2, r;
double rot;
int larc, swp;
/* moveto (M, m), lineto (L, l) (2 arguments) */
if ( sscanf( s, " %1[MmLl]%lf%lf %n", svg_cmd, &v.x, &v.y, &n ) == 3 )
{
char ass_cmd;
if ( *svg_cmd == 'M' || *svg_cmd == 'm' )
{
IPRINT( "moveto\n" );
ass_cmd = 'm';
}
else
{
IPRINT( "lineto\n" );
ass_cmd = 'l';
}
do
{
emit( "%c ", ass_cmd );
if ( *svg_cmd == 'l' || *svg_cmd == 'm' )
v = vec_add( v, last );
if ( ass_cmd == 'm' )
subpath_first = v;
emitf( ctx, "%v ", v );
last_cubic = last_quad = last = v;
s += n;
ass_cmd = 'l';
}
while ( sscanf( s, "%lf%lf %n", &v.x, &v.y, &n ) == 2 );
}
/* closepath (Z, z) (no arguments) */
else if ( sscanf(s, " %1[Zz] %n", svg_cmd, &n) == 1 )
{
IPRINT( "closepath\n" );
// in ASS paths are automatically closed
// IOW: there are no "open" paths, only closed shapes!
s += n;
last_cubic = last_quad = last = subpath_first;
}
/* horizontal lineto (H, h) (1 argument) */
else if ( sscanf( s, " %1[Hh]%lf %n", svg_cmd, &v.x, &n ) == 2 )
{
IPRINT( "h-lineto\n" );
emit( "l " );
v.y = last.y;
do
{
if ( *svg_cmd == 'h' )
v.x += last.x;
emitf( ctx, "%v ", v );
last_cubic = last_quad = last = v;
s += n;
}
while ( sscanf( s, "%lf %n", &v.x, &n ) == 1 );
}
/* vertical lineto (V, v) (1 argument) */
else if ( sscanf( s, " %1[Vv]%lf %n", svg_cmd, &v.y, &n ) == 2 )
{
IPRINT( "v-lineto\n" );
emit( "l " );
v.x = last.x;
do
{
if ( *svg_cmd == 'v' )
v.y += last.y;
emitf( ctx, "%v ", v );
last_cubic = last_quad = last = v;
s += n;
}
while ( sscanf( s, "%lf %n", &v.x, &n ) == 1 );
}
/* cubic Bézier curveto (C, c) (6 arguments) */
else if ( sscanf( s, " %1[Cc]%lf%lf%lf%lf%lf%lf %n", svg_cmd,
&v1.x, &v1.y, &v2.x, &v2.y, &v.x, &v.y, &n ) == 7 )
{
IPRINT( "c-bezier\n" );
do
{
if ( *svg_cmd == 'c' )
{
v1 = vec_add( v1, last );
v2 = vec_add( v2, last );
v = vec_add( v , last );
}
emitf( ctx, "b %v %v %v ", v1, v2, v );
last_cubic = v2;
last_quad = last = v;
s += n;
}
while ( sscanf( s, "%lf%lf%lf%lf%lf%lf %n",
&v1.x, &v1.y, &v2.x, &v2.y, &v.x, &v.y, &n ) == 6 );
}
/* shorthand/smooth cubic curveto (S, s) (4 arguments) */
else if ( sscanf(s, " %1[Ss]%lf%lf%lf%lf %n", svg_cmd,
&v2.x, &v2.y, &v.x, &v.y, &n) == 5 )
{
IPRINT( "s-bezier\n" );
do
{
v1 = vec_add( last, vec_sub( last, last_cubic ) );
if ( *svg_cmd == 's' )
{
v2 = vec_add( v2, last );
v = vec_add( v, last );
}
emitf( ctx, "b %v %v %v ", v1, v2, v );
last_cubic = v2;
last_quad = last = v;
s += n;
}
while (sscanf(s, "%lf%lf%lf%lf %n",
&v2.x, &v2.y, &v.x, &v.y, &n) == 4);
}
/* quadratic Bezier curveto (Q, q) (4 arguments) */
else if ( sscanf( s, " %1[Qq]%lf%lf%lf%lf %n", svg_cmd,
&v1.x, &v1.y, &v.x, &v.y, &n ) == 5 )
{
IPRINT( "q-bezier\n" );
do
{
last_quad = v;
if ( *svg_cmd == 'q' )
{
v1 = vec_add( v1, last );
v = vec_add( v, last );
}
emitf( ctx, "b %v %v %v ",
vec_add( vec_scal( last, 1./3 ), vec_scal( v1, 2./3 ) ),
vec_add( vec_scal( v1, 2./3 ), vec_scal( v, 1./3 ) ),
v );
last_cubic = last = v;
s += n;
}
while ( sscanf( s, "%lf%lf%lf%lf %n", &v1.x, &v1.y, &v.x, &v.y, &n) == 4 );
}
/* shorthand/smooth quadratic curveto (T, t) (2 arguments) */
else if ( sscanf( s, " %1[Tt]%lf%lf %n", svg_cmd, &v.x, &v.y, &n ) == 3 )
{
IPRINT( "t-bezier\n" );
do
{
v1 = vec_add( last, vec_sub( last, last_quad ) );
last_quad = v1;
if ( *svg_cmd == 't' )
{
v1 = vec_add( v1, last );
v = vec_add( v, last );
}
emitf( ctx, "b %v %v %v ",
vec_add( vec_scal( last, 1./3 ), vec_scal( v1, 2./3 ) ),
vec_add( vec_scal( v1, 2./3 ), vec_scal( v, 1./3 ) ),
v ),
last_cubic = last = v;
s += n;
}
while ( sscanf(s, "%lf%lf %n", &v.x, &v.y, &n ) == 2 );
}
/* elliptical arc (A, a) (7 arguments) */
else if ( sscanf( s, " %1[Aa]%lf%lf%lf%d%d%lf%lf %n", svg_cmd,
&r.x, &r.y, &rot, &larc, &swp, &v.x, &v.y, &n ) == 8 )
{
IPRINT( "arc\n" );
do
{
if ( *svg_cmd == 'a' )
v = vec_add( v, last );
res = ass_arc( ctx, last, r, rot, larc, swp, v );
last_cubic = last_quad = last = v;
s += n;
}
while ( sscanf(s, "%lf%lf%lf%d%d%lf%lf %n", &r.x, &r.y,
&rot, &larc, &swp, &v.x, &v.y, &n ) == 7 );
}
/* invalid path syntax */
else {
err( ELVL_WARNING, 0, "parsePath failed at \"%s\"", s );
errno = EINVAL;
res = -1;
break;
}
}
free( d );
return res;
}
static int ass_polyline( ctx_t *ctx, const char *pt )
{
int res = -1;
int n;
char *d;
float dummy;
if ( pt && *pt
&& sscanf( pt, "%f ,%f %n", &dummy, &dummy, &n ) == 2
&& NULL != ( d = malloc( strlen( pt ) + 4 + 1 ) ) )
{
strcpy( d, "M " );
strncat( d, pt, n );
strcat( d, "L " );
strcat( d, pt + n );
res = ass_path( ctx, d ); // Ain't we sneaky?
free( d );
}
return res;
}
/*
* Callback function for XML parser
*/
static int svg2ass( nxmlEvent_t evt, const nxmlNode_t *node, void *usr )
{
int res = 0;
ctx_t *ctx = usr;
vec_t v1, v2, c, r;
if ( NXML_TYPE_PARENT != node->type
&& NXML_TYPE_SELF != node->type
&& NXML_TYPE_END != node->type )
return 0;
switch ( evt )
{
case NXML_EVT_OPEN:
if ( 0 == strcasecmp( node->name, "svg" ) )
{
if ( ctx->in_svg )
err( ELVL_WARNING, 0, "nested <svg> element!" );
ctx->in_svg++;
}
if ( !ctx->in_svg )
break;
IPRINT( "<%s \n", node->name );
if ( 0 != ctx_push( ctx ) )
err( ELVL_FATAL, 0, "context stack push: %s", strerror( errno ) );
if ( 0 == strcasecmp( node->name, "svg" ) )
{
parseStyles( ctx, node );