-
-
Notifications
You must be signed in to change notification settings - Fork 351
/
svg2json.pl
executable file
·125 lines (108 loc) · 3.29 KB
/
svg2json.pl
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
#!/usr/bin/perl -w
# $Id$
use strict;
use POSIX;
use File::Basename;
use Data::Dumper;
use XML::Parser;
{
die "usage: " . basename($0) . " source_path\n" unless ($#ARGV == 0);
my $input = $ARGV[0];
my ($filename, $directories, $suffix) = fileparse ($input, qr/\.[^.]*/);
my $indent = 0;
my $verbose = 0; # 0 1 2
my $indent_delta = 0;
my %histogram;
my $message = '';
my %filter = (
"g" => ["id"],
"path" => ["style", "d"],
"rect" => ["style", "width", "height", "x", "y"]
);
sub hdl_start {
my $p = shift;
my $elt = shift;
if (defined $filter{$elt}) {
my $ret;
if ($verbose) {
$ret = sprintf (",\n%*s[\"%s\"", $indent, '', $elt);
} else {
$ret = sprintf (",%*s[\"%s\"", $indent, '', $elt);
}
$indent += $indent_delta;
my $spaces = sprintf ("%*s", $indent, '');
my @A;
while (defined (my $a = shift)) {
my $b = shift;
for (@{$filter{$elt}}) {
if ($_ eq $a) {
if ($a eq 'style') {
my $index;
$b = join(';', sort(split(';', $b)));
if (!defined $histogram{$b}) {
$histogram{$b}[0] = scalar (keys %histogram) + 1;
}
$histogram{$b}[1]++;
$a = 'class';
$b = 's' . $histogram{$b}[0];
}
push (@A, "\"$a\":\"$b\"");
}
}
}
@A = sort (@A);
if (scalar(@A)) {
if ($verbose > 1) {
my $attr = join(",\n$spaces", @A);
$ret .= sprintf (",{\n%s%s\n%s}", $spaces, $attr, $spaces);
} else {
my $attr = join(",", @A);
$ret .= sprintf (",{%s}", $attr);
}
}
$message .= $ret;
}
}
sub hdl_end {
my ($p, $elt) = @_;
if (defined $filter{$elt}) {
$indent -= $indent_delta;
$message .= "]";
}
}
sub hdl_char {
my ($p, $str) = @_;
$str =~ s/\s+//;
if ($str) {
$message .= "\"$str\"";
}
}
my $a = XML::Parser->new(
Style => 'Tree',
Handlers => {
Start => \&hdl_start,
# Char => \&hdl_char,
End => \&hdl_end
}
);
$a->parsefile($input);
my $css = '';
for (keys %histogram) {
my $a = $histogram{$_}->[0];
my $b = $_;
$css .= ".s$a\{$b\}";
}
print <<EOM;
var WaveSkin = WaveSkin || {};
WaveSkin.$filename = ["svg",{"id":"svg","xmlns":"http://www.w3.org/2000/svg","xmlns:xlink":"http://www.w3.org/1999/xlink","height":"0"}
,["style",{"type":"text/css"},"text{font-size:11pt;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:center;fill-opacity:1;font-family:Helvetica}.muted{fill:#aaa}.warning{fill:#f6b900}.error{fill:#f60000}.info{fill:#0041c4}.success{fill:#00ab00}.h1{font-size:33pt;font-weight:bold}.h2{font-size:27pt;font-weight:bold}.h3{font-size:20pt;font-weight:bold}.h4{font-size:14pt;font-weight:bold}.h5{font-size:11pt;font-weight:bold}.h6{font-size:8pt;font-weight:bold}$css"]
,["defs"
$message
,["marker",{"id":"arrowhead", "style":"fill:#0041c4", "markerHeight":"7", "markerWidth":"10", "markerUnits":"strokeWidth", "viewBox":"0 -4 11 8", "refX":"15","refY":"0","orient":"auto"},["path",{"d":"M0 -4 11 0 0 4z"}]]
,["marker",{"id":"arrowtail", "style":"fill:#0041c4", "markerHeight":"7", "markerWidth":"10", "markerUnits":"strokeWidth", "viewBox":"-11 -4 11 8", "refX":"-15","refY":"0","orient":"auto"},["path",{"d":"M0 -4 -11 0 0 4z"}]]
],["g",{"id":"waves"},["g",{"id":"lanes"}],["g",{"id":"groups"}]]]
EOM
}
#,["g",{"id":"wavetemps","style":"display:none"}
#$message
#]