summaryrefslogtreecommitdiffstats
path: root/share/extensions/synfig_output.py
blob: fdc6c87bb727a5b3a9edf56e28fc864c57b08c56 (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
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
#!/usr/bin/env python
# coding=utf-8
#
# Copyright (C) 2011 Nikita Kitaev
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
#
"""
An Inkscape extension for exporting Synfig files (.sif)
"""
import math
import uuid
from copy import deepcopy

from lxml import etree

import inkex
from inkex import Group, Layer, Anchor, Switch, PathElement, \
                  Metadata, NamedView, Gradient, SvgDocumentElement, \
                  Path, Transform

import synfig_fileformat as sif
from synfig_prepare import MalformedSVGError, SynfigPrep, get_dimension


# ##### Utility Classes ####################################
class UnsupportedException(Exception):
    """When part of an element is not supported, this exception is raised to invalidate the whole element"""
    pass


class SynfigDocument(object):
    """A synfig document, with commands for adding layers and layer parameters"""

    def __init__(self, width=1024, height=768, name="Synfig Animation 1"):
        self.root_canvas = etree.fromstring(
                """
    <canvas
        version="0.5"
        width="{:f}"
        height="{:f}"
        xres="2834.645752"
        yres="2834.645752"
        view-box="0 0 0 0"
        >
      <name>{}</name>
    </canvas>
    """.format(width, height, name)
        )

        self._update_viewbox()

        self.gradients = {}
        self.filters = {}

    # ## Properties

    def get_root_canvas(self):
        return self.root_canvas

    def get_root_tree(self):
        return self.root_canvas.getroottree()

    def _update_viewbox(self):
        """Update the viewbox to match document width and height"""
        attr_viewbox = "{:f} {:f} {:f} {:f}".format(-self.width / 2.0 / sif.kux,
                                                    self.height / 2.0 / sif.kux,
                                                    self.width / 2.0 / sif.kux,
                                                    -self.height / 2.0 / sif.kux)
        self.root_canvas.set("view-box", attr_viewbox)

    def get_width(self):
        return float(self.root_canvas.get("width", "0"))

    def set_width(self, value):
        self.root_canvas.set("width", str(value))
        self._update_viewbox()

    def get_height(self):
        return float(self.root_canvas.get("height", "0"))

    def set_height(self, value):
        self.root_canvas.set("height", str(value))
        self._update_viewbox()

    def get_name(self):
        return self.root_canvas.get("name", "")

    def set_name(self, value):
        self.root_canvas.set("name", value)
        self._update_viewbox()

    width = property(get_width, set_width)
    height = property(get_height, set_height)
    name = property(get_name, set_name)

    # ## Public utility functions

    def new_guid(self):
        """Generate a new GUID"""
        return uuid.uuid4().hex

    # ## Coordinate system conversions

    def distance_svg2sif(self, distance):
        """Convert distance from SVG to Synfig units"""
        return distance / sif.kux

    def distance_sif2svg(self, distance):
        """Convert distance from Synfig to SVG units"""
        return distance * sif.kux

    def coor_svg2sif(self, vector):
        """Convert SVG coordinate [x, y] to Synfig units"""
        x = vector[0]
        y = self.height - vector[1]

        x -= self.width / 2.0
        y -= self.height / 2.0
        x /= sif.kux
        y /= sif.kux

        return [x, y]

    def coor_sif2svg(self, vector):
        """Convert Synfig coordinate [x, y] to SVG units"""
        x = vector[0] * sif.kux + self.width / 2.0
        y = vector[1] * sif.kux + self.height / 2.0

        y = self.height - y

        assert self.coor_svg2sif([x, y]) == vector, "sif to svg coordinate conversion error"

        return [x, y]

    def list_coor_svg2sif(self, l):
        """Scan a list for coordinate pairs and convert them to Synfig units"""
        # If list has two numerical elements,
        # treat it as a coordinate pair
        if type(l) == list and len(l) == 2:
            if type(l[0]) == int or type(l[0]) == float:
                if type(l[1]) == int or type(l[1]) == float:
                    l_sif = self.coor_svg2sif(l)
                    l[0] = l_sif[0]
                    l[1] = l_sif[1]
                    return

        # Otherwise recursively iterate over the list
        for x in l:
            if type(x) == list:
                self.list_coor_svg2sif(x)

    def list_coor_sif2svg(self, l):
        """Scan a list for coordinate pairs and convert them to SVG units"""
        # If list has two numerical elements,
        # treat it as a coordinate pair
        if type(l) == list and len(l) == 2:
            if type(l[0]) == int or type(l[0]) == float:
                if type(l[1]) == int or type(l[1]) == float:
                    l_sif = self.coor_sif2svg(l)
                    l[0] = l_sif[0]
                    l[1] = l_sif[1]
                    return

        # Otherwise recursively iterate over the list
        for x in l:
            if type(x) == list:
                self.list_coor_sif2svg(x)

    def bline_coor_svg2sif(self, b):
        """Convert a BLine from SVG to Synfig coordinate units"""
        self.list_coor_svg2sif(b["points"])

    def bline_coor_sif2svg(self, b):
        """Convert a BLine from Synfig to SVG coordinate units"""
        self.list_coor_sif2svg(b["points"])

    # ## XML Builders -- private
    # ##  used to create XML elements in the Synfig document

    def build_layer(self, layer_type, desc, canvas=None, active=True, version="auto"):
        """Build an empty layer"""
        if canvas is None:
            layer = self.root_canvas.makeelement("layer")
        else:
            layer = etree.SubElement(canvas, "layer")

        layer.set("type", layer_type)
        layer.set("desc", desc)
        if active:
            layer.set("active", "true")
        else:
            layer.set("active", "false")

        if version == "auto":
            version = sif.defaultLayerVersion(layer_type)

        if type(version) == float:
            version = str(version)

        layer.set("version", version)

        return layer

    def _calc_radius(self, p1x, p1y, p2x, p2y):
        """Calculate radius of a tangent given two points"""
        # Synfig tangents are scaled by a factor of 3
        return sif.tangent_scale * math.sqrt((p2x - p1x) ** 2 + (p2y - p1y) ** 2)

    def _calc_angle(self, p1x, p1y, p2x, p2y):
        """Calculate angle (in radians) of a tangent given two points"""
        dx = p2x - p1x
        dy = p2y - p1y
        if dx > 0 and dy > 0:
            ag = math.pi + math.atan(dy / dx)
        elif dx > 0 > dy:
            ag = math.pi + math.atan(dy / dx)
        elif dx < 0 and dy < 0:
            ag = math.atan(dy / dx)
        elif dx < 0 < dy:
            ag = 2 * math.pi + math.atan(dy / dx)
        elif dx == 0 and dy > 0:
            ag = -1 * math.pi / 2
        elif dx == 0 and dy < 0:
            ag = math.pi / 2
        elif dx == 0 and dy == 0:
            ag = 0
        elif dx < 0 and dy == 0:
            ag = 0
        elif dx > 0 and dy == 0:
            ag = math.pi

        return (ag * 180) / math.pi

    def build_param(self, layer, name, value, param_type="auto", guid=None):
        """Add a parameter node to a layer"""
        if layer is None:
            param = self.root_canvas.makeelement("param")
        else:
            param = etree.SubElement(layer, "param")
        param.set("name", name)

        # Automatically detect param_type
        if param_type == "auto":
            if layer is not None:
                layer_type = layer.get("type")
                param_type = sif.paramType(layer_type, name)
            else:
                param_type = sif.paramType(None, name, value)

        if param_type == "real":
            el = etree.SubElement(param, "real")
            el.set("value", str(float(value)))
        elif param_type == "integer":
            el = etree.SubElement(param, "integer")
            el.set("value", str(int(value)))
        elif param_type == "vector":
            el = etree.SubElement(param, "vector")
            x = etree.SubElement(el, "x")
            x.text = str(float(value[0]))
            y = etree.SubElement(el, "y")
            y.text = str(float(value[1]))
        elif param_type == "color":
            el = etree.SubElement(param, "color")
            r = etree.SubElement(el, "r")
            r.text = str(float(value[0]))
            g = etree.SubElement(el, "g")
            g.text = str(float(value[1]))
            b = etree.SubElement(el, "b")
            b.text = str(float(value[2]))
            a = etree.SubElement(el, "a")
            a.text = str(float(value[3])) if len(value) > 3 else "1.0"
        elif param_type == "gradient":
            el = etree.SubElement(param, "gradient")
            # Value is a dictionary of color stops
            #  see get_gradient()
            for pos in value.keys():
                color = etree.SubElement(el, "color")
                color.set("pos", str(float(pos)))

                c = value[pos]

                r = etree.SubElement(color, "r")
                r.text = str(float(c[0]))
                g = etree.SubElement(color, "g")
                g.text = str(float(c[1]))
                b = etree.SubElement(color, "b")
                b.text = str(float(c[2]))
                a = etree.SubElement(color, "a")
                a.text = str(float(c[3])) if len(c) > 3 else "1.0"
        elif param_type == "bool":
            el = etree.SubElement(param, "bool")
            if value:
                el.set("value", "true")
            else:
                el.set("value", "false")
        elif param_type == "time":
            el = etree.SubElement(param, "time")
            if type(value) == int:
                el.set("value", "{:d}s".format(value))
            elif type(value) == float:
                el.set("value", "{:f}s".format(value))
            elif type(value) == str:
                el.set("value", value)
        elif param_type == "bline":
            el = etree.SubElement(param, "bline")
            el.set("type", "bline_point")

            # value is a bline (dictionary type), see path_to_bline_list
            if value["loop"]:
                el.set("loop", "true")
            else:
                el.set("loop", "false")

            for vertex in value["points"]:
                x = float(vertex[1][0])
                y = float(vertex[1][1])

                tg1x = float(vertex[0][0])
                tg1y = float(vertex[0][1])

                tg2x = float(vertex[2][0])
                tg2y = float(vertex[2][1])

                tg1_radius = self._calc_radius(x, y, tg1x, tg1y)
                tg1_angle = self._calc_angle(x, y, tg1x, tg1y)

                tg2_radius = self._calc_radius(x, y, tg2x, tg2y)
                tg2_angle = self._calc_angle(x, y, tg2x, tg2y) - 180.0

                if vertex[3]:
                    split = "true"
                else:
                    split = "false"

                entry = etree.SubElement(el, "entry")
                composite = etree.SubElement(entry, "composite")
                composite.set("type", "bline_point")

                point = etree.SubElement(composite, "point")
                vector = etree.SubElement(point, "vector")
                etree.SubElement(vector, "x").text = str(x)
                etree.SubElement(vector, "y").text = str(y)

                width = etree.SubElement(composite, "width")
                etree.SubElement(width, "real").set("value", "1.0")

                origin = etree.SubElement(composite, "origin")
                etree.SubElement(origin, "real").set("value", "0.5")

                split_el = etree.SubElement(composite, "split")
                etree.SubElement(split_el, "bool").set("value", split)

                t1 = etree.SubElement(composite, "t1")
                t2 = etree.SubElement(composite, "t2")

                t1_rc = etree.SubElement(t1, "radial_composite")
                t1_rc.set("type", "vector")

                t2_rc = etree.SubElement(t2, "radial_composite")
                t2_rc.set("type", "vector")

                t1_r = etree.SubElement(t1_rc, "radius")
                t2_r = etree.SubElement(t2_rc, "radius")
                t1_radius = etree.SubElement(t1_r, "real")
                t2_radius = etree.SubElement(t2_r, "real")
                t1_radius.set("value", str(tg1_radius))
                t2_radius.set("value", str(tg2_radius))

                t1_t = etree.SubElement(t1_rc, "theta")
                t2_t = etree.SubElement(t2_rc, "theta")
                t1_angle = etree.SubElement(t1_t, "angle")
                t2_angle = etree.SubElement(t2_t, "angle")
                t1_angle.set("value", str(tg1_angle))
                t2_angle.set("value", str(tg2_angle))
        elif param_type == "canvas":
            el = etree.SubElement(param, "canvas")
            el.set("xres", "10.0")
            el.set("yres", "10.0")

            # "value" is a list of layers
            if value is not None:
                for layer in value:
                    el.append(layer)
        else:
            raise AssertionError("Unsupported param type {}".format(param_type))

        if guid:
            el.set("guid", guid)
        else:
            el.set("guid", self.new_guid())

        return param

    # ## Public layer API
    # ##  Should be used by outside functions to create layers and set layer parameters

    def create_layer(self, layer_type, desc, params={}, guids={}, canvas=None, active=True, version="auto"):
        """Create a new layer

        Keyword arguments:
        layer_type -- layer type string used internally by Synfig
        desc -- layer description
        params -- a dictionary of parameter names and their values
        guids -- a dictionary of parameter types and their guids (optional)
        active -- set to False to create a hidden layer
        """
        layer = self.build_layer(layer_type, desc, canvas, active, version)
        default_layer_params = sif.defaultLayerParams(layer_type)

        for param_name in default_layer_params.keys():
            param_type = default_layer_params[param_name][0]
            if param_name in params.keys():
                param_value = params[param_name]
            else:
                param_value = default_layer_params[param_name][1]

            if param_name in guids.keys():
                param_guid = guids[param_name]
            else:
                param_guid = None

            if param_value is not None:
                self.build_param(layer, param_name, param_value, param_type, guid=param_guid)

        return layer

    def set_param(self, layer, name, value, param_type="auto", guid=None, modify_linked=False):
        """Set a layer parameter

        Keyword arguments:
        layer -- the layer to set the parameter for
        name -- parameter name
        value -- parameter value
        param_type -- parameter type (default "auto")
        guid -- guid of the parameter value
        """
        if modify_linked:
            raise AssertionError("Modifying linked parameters is not supported")

        layer_type = layer.get("type")
        assert layer_type, "Layer does not have a type"

        if param_type == "auto":
            param_type = sif.paramType(layer_type, name)

        # Remove existing parameters with this name
        existing = []
        for param in layer.iterchildren():
            if param.get("name") == name:
                existing.append(param)

        if len(existing) == 0:
            self.build_param(layer, name, value, param_type, guid)
        elif len(existing) > 1:
            raise AssertionError("Found multiple parameters with the same name")
        else:
            new_param = self.build_param(None, name, value, param_type, guid)
            layer.replace(existing[0], new_param)

    def set_params(self, layer, params={}, guids={}, modify_linked=False):
        """Set layer parameters

        Keyword arguments:
        layer -- the layer to set the parameter for
        params -- a dictionary of parameter names and their values
        guids -- a dictionary of parameter types and their guids (optional)
        """
        for param_name in params.keys():
            if param_name in guids.keys():
                self.set_param(layer, param_name, params[param_name], guid=guids[param_name], modify_linked=modify_linked)
            else:
                self.set_param(layer, param_name, params[param_name], modify_linked=modify_linked)

    def get_param(self, layer, name, param_type="auto"):
        """Get the value of a layer parameter

        Keyword arguments:
        layer -- the layer to get the parameter from
        name -- param name
        param_type -- parameter type (default "auto")

        NOT FULLY IMPLEMENTED
        """
        layer_type = layer.get("type")
        assert layer_type, "Layer does not have a type"

        if param_type == "auto":
            param_type = sif.paramType(layer_type, name)

        for param in layer.iterchildren():
            if param.get("name") == name:
                if param_type == "real":
                    return float(param[0].get("value", "0"))
                elif param_type == "integer":
                    return int(param[0].get("integer", "0"))
                else:
                    raise Exception("Getting this type of parameter not yet implemented")

    # ## Global defs, and related

    # SVG Filters
    def add_filter(self, filter_id, f):
        """Register a filter"""
        self.filters[filter_id] = f

    # SVG Gradients
    def add_linear_gradient(self, gradient_id, p1, p2, mtx=[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]], stops=[], link="", spread_method="pad"):
        """Register a linear gradient definition"""
        gradient = {
            "type": "linear",
            "p1": p1,
            "p2": p2,
            "mtx": mtx,
            "spreadMethod": spread_method
        }
        if stops:
            gradient["stops"] = stops
            gradient["stops_guid"] = self.new_guid()
        elif link != "":
            gradient["link"] = link
        else:
            raise MalformedSVGError("Gradient has neither stops nor link")
        self.gradients[gradient_id] = gradient

    def add_radial_gradient(self, gradient_id, center, radius, focus, mtx=[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]], stops=[], link="", spread_method="pad"):
        """Register a radial gradient definition"""
        gradient = {
            "type": "radial",
            "center": center,
            "radius": radius,
            "focus": focus,
            "mtx": mtx,
            "spreadMethod": spread_method
        }
        if stops:
            gradient["stops"] = stops
            gradient["stops_guid"] = self.new_guid()
        elif link != "":
            gradient["link"] = link
        else:
            raise MalformedSVGError("Gradient has neither stops nor link")
        self.gradients[gradient_id] = gradient

    def get_gradient(self, gradient_id):
        """
        Return a gradient with a given id

        Linear gradient format:
        {
        "type"      : "linear",
        "p1"        : [x, y],
        "p2"        : [x, y],
        "mtx"       : mtx,
        "stops"     : color stops,
        "stops_guid": color stops guid,
        "spreadMethod": "pad", "reflect", or "repeat"
        }

        Radial gradient format:
        {
        "type"      : "radial",
        "center"    : [x, y],
        "radius"    : r,
        "focus"     : [x, y],
        "mtx"       : mtx,
        "stops"     : color stops,
        "stops_guid": color stops guid,
        "spreadMethod": "pad", "reflect", or "repeat"
        }

        Color stops format
        {
        0.0         : color ([r,g,b,a] or [r,g,b]) at start,
        [a number]  : color at that position,
        1.0         : color at end
        }
        """

        if gradient_id not in self.gradients.keys():
            return None

        gradient = self.gradients[gradient_id]

        # If the gradient has no link, we are done
        if "link" not in gradient.keys() or gradient["link"] == "":
            return gradient

        # If the gradient does have a link, find the color stops recursively
        if gradient["link"] not in self.gradients.keys():
            raise MalformedSVGError("Linked gradient ID not found")

        linked_gradient = self.get_gradient(gradient["link"])
        gradient["stops"] = linked_gradient["stops"]
        gradient["stops_guid"] = linked_gradient["stops_guid"]
        del gradient["link"]

        # Update the gradient in our listing
        # (so recursive lookup only happens once)
        self.gradients[gradient_id] = gradient

        return gradient

    def gradient_to_params(self, gradient):
        """Transform gradient to a list of parameters to pass to a Synfig layer"""
        # Create a copy of the gradient
        g = gradient.copy()

        # Set synfig-only attribs
        if g["spreadMethod"] == "repeat":
            g["loop"] = True
        elif g["spreadMethod"] == "reflect":
            g["loop"] = True
            # Reflect the gradient
            # Original: 0.0 [A . B . C] 1.0
            # New:      0.0 [A . B . C . B . A] 1.0
            #           (with gradient size doubled)
            new_stops = {}

            # reflect the stops
            for pos in g["stops"]:
                val = g["stops"][pos]
                if pos == 1.0:
                    new_stops[pos / 2.0] = val
                else:
                    new_stops[pos / 2.0] = val
                    new_stops[1 - pos / 2.0] = val
            g["stops"] = new_stops

            # double the gradient size
            if g["type"] == "linear":
                g["p2"] = [g["p1"][0] + 2.0 * (g["p2"][0] - g["p1"][0]),
                           g["p1"][1] + 2.0 * (g["p2"][1] - g["p1"][1])]
            if g["type"] == "radial":
                g["radius"] *= 2.0

        # Rename "stops" to "gradient"
        g["gradient"] = g["stops"]

        # Convert coordinates
        if g["type"] == "linear":
            g["p1"] = self.coor_svg2sif(g["p1"])
            g["p2"] = self.coor_svg2sif(g["p2"])

        if g["type"] == "radial":
            g["center"] = self.coor_svg2sif(g["center"])
            g["radius"] = self.distance_svg2sif(g["radius"])

        # Delete extra attribs
        removed_attribs = ["type",
                           "stops",
                           "stops_guid",
                           "mtx",
                           "focus",
                           "spreadMethod"]
        for x in removed_attribs:
            if x in g.keys():
                del g[x]
        return g

    # ## Public operations API
    # Operations act on a series of layers, and (optionally) on a series of named parameters
    # The "is_end" attribute should be set to true when the layers are at the end of a canvas
    # (i.e. when adding transform layers on top of them does not require encapsulation)

    def op_blur(self, layers, x, y, name="Blur", is_end=False):
        """Gaussian blur the given layers by the given x and y amounts

        Keyword arguments:
        layers -- list of layers
        x -- x-amount of blur
        y -- x-amount of blur
        is_end -- set to True if layers are at the end of a canvas

        Returns: list of layers
        """
        blur = self.create_layer("blur", name, params={
            "blend_method": sif.blend_methods["straight"],
            "size": [x, y]
        })

        if is_end:
            return layers + [blur]
        else:
            return self.op_encapsulate(layers + [blur])

    def op_color(self, layers, overlay, is_end=False):
        """Apply a color overlay to the given layers

        Should be used to apply a gradient or pattern to a shape

        Keyword arguments:
        layers -- list of layers
        overlay -- color layer to apply
        is_end -- set to True if layers are at the end of a canvas

        Returns: list of layers
        """
        if not layers:
            return layers
        if overlay is None:
            return layers

        overlay_enc = self.op_encapsulate([overlay])
        self.set_param(overlay_enc[0], "blend_method", sif.blend_methods["straight onto"])
        ret = layers + overlay_enc

        if is_end:
            return ret
        else:
            return self.op_encapsulate(ret)

    def op_encapsulate(self, layers, name="Inline Canvas", is_end=False):
        """Encapsulate the given layers

        Keyword arguments:
        layers -- list of layers
        name -- Name of the PasteCanvas layer that is created
        is_end -- set to True if layers are at the end of a canvas

        Returns: list of one layer
        """

        if not layers:
            return layers

        layer = self.create_layer("PasteCanvas", name, params={"canvas": layers})
        return [layer]

    def op_fade(self, layers, opacity, is_end=False):
        """Increase the opacity of the given layers by a certain amount

        Keyword arguments:
        layers -- list of layers
        opacity -- the opacity to apply (float between 0.0 to 1.0)
        name -- name of the Transform layer that is added
        is_end -- set to True if layers are at the end of a canvas

        Returns: list of layers
        """
        # If there is blending involved, first encapsulate the layers
        for layer in layers:
            if self.get_param(layer, "blend_method") != sif.blend_methods["composite"]:
                return self.op_fade(self.op_encapsulate(layers), opacity, is_end)

        # Otherwise, set their amount
        for layer in layers:
            amount = self.get_param(layer, "amount")
            self.set_param(layer, "amount", amount * opacity)

        return layers

    def op_filter(self, layers, filter_id, is_end=False):
        """Apply a filter to the given layers

        Keyword arguments:
        layers -- list of layers
        filter_id -- id of the filter
        is_end -- set to True if layers are at the end of a canvas

        Returns: list of layers
        """
        if filter_id not in self.filters.keys():
            raise MalformedSVGError("Filter {} not found".format(filter_id))

        try:
            ret = self.filters[filter_id](self, layers, is_end)
            assert type(ret) == list
            return ret
        except UnsupportedException:
            # If the filter is not supported, ignore it.
            return layers

    def op_set_blend(self, layers, blend_method, is_end=False):
        """Set the blend method of the given group of layers

        If more than one layer is supplied, they will be encapsulated.

        Keyword arguments:
        layers -- list of layers
        blend_method -- blend method to give the layers
        is_end -- set to True if layers are at the end of a canvas

        Returns: list of layers
        """
        if not layers:
            return layers
        if blend_method == "composite":
            return layers

        layer = layers[0]
        if len(layers) > 1 or self.get_param(layers[0], "amount") != 1.0:
            layer = self.op_encapsulate(layers)[0]

        layer = deepcopy(layer)

        self.set_param(layer, "blend_method", sif.blend_methods[blend_method])

        return [layer]

    def op_transform(self, layers, mtx, name="Transform", is_end=False):
        """Apply a matrix transformation to the given layers

        Keyword arguments:
        layers -- list of layers
        mtx -- transformation matrix
        name -- name of the Transform layer that is added
        is_end -- set to True if layers are at the end of a canvas

        Returns: list of layers
        """
        if not layers:
            return layers
        if mtx is None or mtx == [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]:
            return layers

        src_tl = [100, 100]
        src_br = [200, 200]

        dest_tl = [100, 100]
        dest_tr = [200, 100]
        dest_br = [200, 200]
        dest_bl = [100, 200]

        dest_tl = Transform(mtx).apply_to_point(dest_tl)
        dest_tr = Transform(mtx).apply_to_point(dest_tr)
        dest_br = Transform(mtx).apply_to_point(dest_br)
        dest_bl = Transform(mtx).apply_to_point(dest_bl)

        warp = self.create_layer("warp", name, params={
            "src_tl": self.coor_svg2sif(src_tl),
            "src_br": self.coor_svg2sif(src_br),
            "dest_tl": self.coor_svg2sif(dest_tl),
            "dest_tr": self.coor_svg2sif(dest_tr),
            "dest_br": self.coor_svg2sif(dest_br),
            "dest_bl": self.coor_svg2sif(dest_bl)
        })

        if is_end:
            return layers + [warp]
        else:
            return self.op_encapsulate(layers + [warp])


# ##### Utility Functions ##################################

# ## Path related

def path_to_bline_list(path_d, nodetypes=None, mtx=[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]):
    """
    Convert a path to a BLine List

    bline_list format:

    Vertex:
    [[tg1x, tg1y], [x,y], [tg2x, tg2y], split = T/F]
    Vertex list:
    [ vertex, vertex, vertex, ...]
    Bline:
    {
    "points"    : vertex_list,
    "loop"      : True / False
    }
    """

    # Exit on empty paths
    if not path_d:
        return []

    # Parse the path
    path = Path(path_d).to_arrays()

    # Append (more than) enough c's to the nodetypes
    if nodetypes is None:
        nt = ""
    else:
        nt = nodetypes

    for _ in range(len(path)):
        nt += "c"

    # Create bline list
    #     borrows code from cubicsuperpath.py

    # bline_list := [bline, bline, ...]
    # bline := {
    #           "points":[vertex, vertex, ...],
    #           "loop":True/False,
    #          }

    bline_list = []

    subpathstart = []
    last = []
    lastctrl = []
    lastsplit = True

    for s in path:
        cmd, params = s
        if cmd != "M" and bline_list == []:
            raise MalformedSVGError("Bad path data: path doesn't start with moveto, {}, {}".format(s, path))
        elif cmd == "M":
            # Add previous point to subpath
            if last:
                bline_list[-1]["points"].append([lastctrl[:], last[:], last[:], lastsplit])
            # Start a new subpath
            bline_list.append({"nodetypes": "", "loop": False, "points": []})
            # Save coordinates of this point
            subpathstart = params[:]
            last = params[:]
            lastctrl = params[:]
            lastsplit = False if nt[0] == "z" else True
            nt = nt[1:]
        elif cmd in "LHV":
            bline_list[-1]["points"].append([lastctrl[:], last[:], last[:], lastsplit])
            if cmd == 'H':
                last = [params[0], last[1]]
                lastctrl = [params[0], last[1]]
            elif cmd == 'V':
                last = [last[0], params[0]]
                lastctrl = [last[0], params[0]]
            else:
                last = params[:]
                lastctrl = params[:]
            lastsplit = False if nt[0] == "z" else True
            nt = nt[1:]
        elif cmd == 'C':
            bline_list[-1]["points"].append([lastctrl[:], last[:], params[:2], lastsplit])
            last = params[-2:]
            lastctrl = params[2:4]
            lastsplit = False if nt[0] == "z" else True
            nt = nt[1:]
        elif cmd == 'Q':
            q0 = last[:]
            q1 = params[0:2]
            q2 = params[2:4]
            x0 = q0[0]
            x1 = 1. / 3 * q0[0] + 2. / 3 * q1[0]
            x2 = 2. / 3 * q1[0] + 1. / 3 * q2[0]
            x3 = q2[0]
            y0 = q0[1]
            y1 = 1. / 3 * q0[1] + 2. / 3 * q1[1]
            y2 = 2. / 3 * q1[1] + 1. / 3 * q2[1]
            y3 = q2[1]
            bline_list[-1]["points"].append([lastctrl[:], [x0, y0], [x1, y1], lastsplit])
            last = [x3, y3]
            lastctrl = [x2, y2]
            lastsplit = False if nt[0] == "z" else True
            nt = nt[1:]
        elif cmd == 'A':
            from inkex.paths import arc_to_path
            arcp = arc_to_path(last[:], params[:])
            arcp[0][0] = lastctrl[:]
            last = arcp[-1][1]
            lastctrl = arcp[-1][0]
            lastsplit = False if nt[0] == "z" else True
            nt = nt[1:]
            for el in arcp[:-1]:
                el.append(True)
                bline_list[-1]["points"].append(el)
        elif cmd == "Z":
            if len(bline_list[-1]["points"]) == 0:
                # If the path "loops" after only one point
                #  e.g. "M 0 0 Z"
                bline_list[-1]["points"].append([lastctrl[:], last[:], last[:], False])
            elif last == subpathstart:
                # If we are back to the original position
                # merge our tangent into the first point
                bline_list[-1]["points"][0][0] = lastctrl[:]
            else:
                # Otherwise draw a line to the starting point
                bline_list[-1]["points"].append([lastctrl[:], last[:], last[:], lastsplit])

            # Clear the variables (no more points need to be added)
            last = []
            lastctrl = []
            lastsplit = True

            # Loop the subpath
            bline_list[-1]["loop"] = True
    # Append final superpoint, if needed
    if last:
        bline_list[-1]["points"].append([lastctrl[:], last[:], last[:], lastsplit])

    # Apply the transformation
    if mtx != [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]:
        for bline in bline_list:
            for vertex in bline["points"]:
                for point in vertex:
                    if not isinstance(point, bool):
                        pnt = Transform(mtx).apply_to_point(point)
                        point[0], point[1] = pnt[0], pnt[1]

    return bline_list


# ## Style related

def extract_color(style, color_attrib, *opacity_attribs):
    if color_attrib in style.keys():
        if style[color_attrib] == "none":
            return [1, 1, 1, 0]
        c = inkex.Color(style[color_attrib]).to_rgb()
    else:
        c = (0, 0, 0)

    # Convert color scales and adjust gamma
    color = [pow(c[0] / 255.0, sif.gamma), pow(c[1] / 255.0, sif.gamma), pow(c[2] / 255.0, sif.gamma), 1.0]

    for opacity in opacity_attribs:
        if opacity in style.keys():
            color[3] *= float(style[opacity])
    return color


def extract_opacity(style, *opacity_attribs):
    ret = 1.0
    for opacity in opacity_attribs:
        if opacity in style.keys():
            ret *= float(style[opacity])
    return ret


def extract_width(style, width_attrib, mtx):
    if width_attrib in style.keys():
        width = get_dimension(style[width_attrib])
    else:
        width = 1

    area_scale_factor = mtx[0][0] * mtx[1][1] - mtx[0][1] * mtx[1][0]
    linear_scale_factor = math.sqrt(abs(area_scale_factor))

    return width * linear_scale_factor / sif.kux


# ##### Main Class #########################################
class SynfigExport(SynfigPrep):
    def __init__(self):
        SynfigPrep.__init__(self)

    def effect(self):
        # Prepare the document for exporting
        SynfigPrep.effect(self)
        svg = self.document.getroot()
        width = get_dimension(svg.get("width", 1024))
        height = get_dimension(svg.get("height", 768))

        title = svg.getElement('svg:title')
        if title:
            name = title.text
        else:
            name = svg.get('sodipodi:docname', "Synfig Animation 1")

        doc = SynfigDocument(width, height, name)

        layers = []
        for node in svg.iterchildren():
            layers += self.convert_node(node, doc)

        root_canvas = doc.get_root_canvas()
        for layer in layers:
            root_canvas.append(layer)

        self.synfig_document = doc.get_root_tree()

    def save(self, stream):
        self.synfig_document.write(stream)

    def convert_node(self, node, d):
        """Convert an SVG node to a list of Synfig layers"""
        # Parse tags that don't draw any layers
        if isinstance(node, SvgDocumentElement):
            self.parse_defs(node, d)
            return []
        elif not isinstance(node, (Group, Anchor, Switch, PathElement, Metadata, NamedView)):
            # An unsupported element
            return []

        layers = []
        if isinstance(node, Group):
            for subnode in node:
                layers += self.convert_node(subnode, d)
            if isinstance(node, Layer):
                name = node.label or "Inline Canvas"
                layers = d.op_encapsulate(layers, name=name)

        elif isinstance(node, (Anchor, Switch)):
            # Treat anchor and switch as a group
            for subnode in node:
                layers += self.convert_node(subnode, d)
        elif isinstance(node, PathElement):
            layers = self.convert_path(node, d)

        style = node.style
        if "filter" in style.keys() and style["filter"].startswith("url"):
            filter_id = style["filter"][5:].split(")")[0]
            layers = d.op_filter(layers, filter_id)

        opacity = extract_opacity(style, "opacity")
        if opacity != 1.0:
            layers = d.op_fade(layers, opacity)

        return layers

    def parse_defs(self, node, d):
        for child in node.iterchildren():
            if isinstance(child, Gradient):
                self.parse_gradient(child, d)
            elif child.TAG == "filter":
                self.parse_filter(child, d)

    def parse_gradient(self, node, d):
        if node.TAG == "linearGradient":
            gradient_id = node.get("id", str(id(node)))
            x1 = float(node.get("x1", "0.0"))
            x2 = float(node.get("x2", "0.0"))
            y1 = float(node.get("y1", "0.0"))
            y2 = float(node.get("y2", "0.0"))

            mtx = node.gradientTransform.matrix

            link = node.get('xlink:href', "#")[1:]
            spread_method = node.get("spreadMethod", "pad")
            if link == "":
                stops = self.parse_stops(node, d)
                d.add_linear_gradient(gradient_id, [x1, y1], [x2, y2], mtx, stops=stops, spread_method=spread_method)
            else:
                d.add_linear_gradient(gradient_id, [x1, y1], [x2, y2], mtx, link=link, spread_method=spread_method)
        elif node.TAG == "radialGradient":
            gradient_id = node.get("id", str(id(node)))
            cx = float(node.get("cx", "0.0"))
            cy = float(node.get("cy", "0.0"))
            r = float(node.get("r", "0.0"))
            fx = float(node.get("fx", "0.0"))
            fy = float(node.get("fy", "0.0"))

            mtx = node.gradientTransform.matrix

            link = node.get('xlink:href', "#")[1:]
            spread_method = node.get("spreadMethod", "pad")
            if link == "":
                stops = self.parse_stops(node, d)
                d.add_radial_gradient(gradient_id, [cx, cy], r, [fx, fy], mtx, stops=stops, spread_method=spread_method)
            else:
                d.add_radial_gradient(gradient_id, [cx, cy], r, [fx, fy], mtx, link=link, spread_method=spread_method)

    def parse_stops(self, node, d):
        stops = {}
        for stop in node.iterchildren():
            if stop.TAG == "stop":
                offset = float(stop.get("offset"))
                style = stop.style
                stops[offset] = extract_color(style, "stop-color", "stop-opacity")
            else:
                raise MalformedSVGError("Child of gradient is not a stop")

        return stops

    def parse_filter(self, node, d):
        filter_id = node.get("id", str(id(node)))

        # A filter is just like an operator (the op_* functions),
        # except that it's created here
        def the_filter(d, layers, is_end=False):
            refs = {None: layers,  # default
                    "SourceGraphic": layers}
            encapsulate_result = not is_end

            for child in node.iterchildren():
                if child.get("in") not in refs:
                    # "SourceAlpha", "BackgroundImage",
                    # "BackgroundAlpha", "FillPaint", "StrokePaint"
                    # are not supported
                    raise UnsupportedException
                l_in = refs[child.get("in")]
                l_out = []
                if child.TAG == "feGaussianBlur":
                    std_dev = child.get("stdDeviation", "0")
                    std_dev = std_dev.replace(",", " ").split()
                    x = float(std_dev[0])
                    if len(std_dev) > 1:
                        y = float(std_dev[1])
                    else:
                        y = x

                    if x == 0 and y == 0:
                        l_out = l_in
                    else:
                        x = d.distance_svg2sif(x)
                        y = d.distance_svg2sif(y)
                        l_out = d.op_blur(l_in, x, y, is_end=True)
                elif child.TAG == "feBlend":
                    # Note: Blend methods are not an exact match
                    # because SVG uses alpha channel in places where
                    # Synfig does not
                    mode = child.get("mode", "normal")
                    if mode == "normal":
                        blend_method = "composite"
                    elif mode == "multiply":
                        blend_method = "multiply"
                    elif mode == "screen":
                        blend_method = "screen"
                    elif mode == "darken":
                        blend_method = "darken"
                    elif mode == "lighten":
                        blend_method = "brighten"
                    else:
                        raise MalformedSVGError("Invalid blend method")

                    if child.get("in2") == "BackgroundImage":
                        encapsulate_result = False
                        l_out = d.op_set_blend(l_in, blend_method) + d.op_set_blend(l_in, "behind")
                    elif child.get("in2") not in refs:
                        raise UnsupportedException
                    else:
                        l_in2 = refs[child.get("in2")]
                        l_out = l_in2 + d.op_set_blend(l_in, blend_method)

                else:
                    # This filter element is currently unsupported
                    raise UnsupportedException

                # Output the layers
                if child.get("result"):
                    refs[child.get("result")] = l_out

                # Set the default for the next filter element
                refs[None] = l_out

            # Return the output from the last element
            if len(refs[None]) > 1 and encapsulate_result:
                return d.op_encapsulate(refs[None])
            else:
                return refs[None]

        d.add_filter(filter_id, the_filter)

    def convert_path(self, node, d):
        """Convert an SVG path node to a list of Synfig layers"""
        layers = []

        node_id = node.get("id", str(id(node)))
        style = node.style

        mtx = node.transform.matrix
        blines = path_to_bline_list(node.get("d"), node.get('sodipodi:nodetypes'), mtx)
        for bline in blines:
            d.bline_coor_svg2sif(bline)
            bline_guid = d.new_guid()

            if style.setdefault("fill", "#000000") != "none":
                if style["fill"].startswith("url"):
                    # Set the color to black, so we can later overlay
                    # the shape with a gradient or pattern
                    color = [0, 0, 0, 1]
                else:
                    color = extract_color(style, "fill", "fill-opacity")

                layer = d.create_layer("region", node_id, {
                    "bline": bline,
                    "color": color,
                    "winding_style": 1 if style.setdefault("fill-rule", "nonzero") == "evenodd" else 0,
                }, guids={
                    "bline": bline_guid
                })

                if style["fill"].startswith("url"):
                    color_layer = self.convert_url(style["fill"][5:].split(")")[0], mtx, d)[0]
                    layer = d.op_color([layer], overlay=color_layer)[0]
                    layer = d.op_fade([layer], extract_opacity(style, "fill-opacity"))[0]

                layers.append(layer)

            if style.setdefault("stroke", "none") != "none":
                if style["stroke"].startswith("url"):
                    # Set the color to black, so we can later overlay
                    # the shape with a gradient or pattern
                    color = [0, 0, 0, 1]
                else:
                    color = extract_color(style, "stroke", "stroke-opacity")

                layer = d.create_layer("outline", node_id, {
                    "bline": bline,
                    "color": color,
                    "width": extract_width(style, "stroke-width", mtx),
                    "sharp_cusps": True if style.setdefault("stroke-linejoin", "miter") == "miter" else False,
                    "round_tip[0]": False if style.setdefault("stroke-linecap", "butt") == "butt" else True,
                    "round_tip[1]": False if style.setdefault("stroke-linecap", "butt") == "butt" else True
                }, guids={
                    "bline": bline_guid
                })

                if style["stroke"].startswith("url"):
                    color_layer = self.convert_url(style["stroke"][5:].split(")")[0], mtx, d)[0]
                    layer = d.op_color([layer], overlay=color_layer)[0]
                    layer = d.op_fade([layer], extract_opacity(style, "stroke-opacity"))[0]

                layers.append(layer)

        return layers

    def convert_url(self, url_id, mtx, d):
        """Return a list Synfig layers that represent the gradient with the given id"""
        gradient = d.get_gradient(url_id)
        if gradient is None:
            # Patterns and other URLs not supported
            return [None]

        if gradient["type"] == "linear":
            layer = d.create_layer("linear_gradient", url_id,
                                   d.gradient_to_params(gradient),
                                   guids={"gradient": gradient["stops_guid"]})

        if gradient["type"] == "radial":
            layer = d.create_layer("radial_gradient", url_id,
                                   d.gradient_to_params(gradient),
                                   guids={"gradient": gradient["stops_guid"]})

        trm = Transform(mtx) * Transform(gradient["mtx"])
        return d.op_transform([layer], trm.matrix)


if __name__ == '__main__':
    SynfigExport().run()