| 1 | #!/usr/bin/perl -w | ||
| 2 | |||
| 3 | # This tool takes an SVG file and an id on the command line. | ||
| 4 | # It extracts the id from the input and scales it appropriately for text-mapper: | ||
| 5 | # <svg viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg">...</svg> | ||
| 6 | |||
| 7 | # In Inkscape, you should select the icon you want, ungroup it | ||
| 8 | # entirely, group it once, look at the object properties and note the | ||
| 9 | # id. Then call it as follows, for example: | ||
| 10 | # perl extract-svg.pl ~/Downloads/Gnomeylandicons/Gnomeylandicons.svg g5496 | ||
| 11 | # The SVG at the end is what you are looking for. | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use SVG::Parser; | ||
| 15 | use List::Util qw(min max); | ||
| 16 | use lib '.'; | ||
| 17 | use Image::SVG::Path qw(extract_path_info create_path_string); | ||
| 18 | use Storable qw(dclone); | ||
| 19 | |||
| 20 | sub smallest { | ||
| 21 | my ($default, @rest) = @_; | ||
| 22 | return defined($default) ? min($default, @rest) : min(@rest); | ||
| 23 | } | ||
| 24 | |||
| 25 | sub biggest { | ||
| 26 | my ($default, @rest) = @_; | ||
| 27 | return defined($default) ? max($default, @rest) : max(@rest); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub add_point_to_bounding_box { | ||
| 31 | my ($point, $left, $top, $right, $bottom) = @_; | ||
| 32 | $left = smallest($left, $point->[0]); | ||
| 33 | $top = biggest($top, $point->[1]); | ||
| 34 | $right = biggest($right, $point->[0]); | ||
| 35 | $bottom = smallest($bottom, $point->[1]); | ||
| 36 | return ($left, $top, $right, $bottom); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub push_bounding_box { | ||
| 40 | my ($obj, $left, $top, $right, $bottom) = @_; | ||
| 41 | if ($obj->getElementName() eq "path") { | ||
| 42 | warn "<path id='" . $obj->getAttribute("id") . "'" | ||
| 43 | . " d='" . $obj->getAttribute("d") . "'>\n"; | ||
| 44 | foreach my $elem (extract_path_info ($obj->getAttribute("d"), | ||
| 45 | {absolute => 1})) { | ||
| 46 | if ($elem->{type} eq 'moveto' or $elem->{type} eq 'lineto') { | ||
| 47 | ($left, $top, $right, $bottom) = | ||
| 48 | add_point_to_bounding_box($elem->{point}, | ||
| 49 | $left, $top, $right, $bottom); | ||
| 50 | } elsif ($elem->{type} eq 'cubic-bezier') { | ||
| 51 | # ignore control points, just use the end point | ||
| 52 | ($left, $top, $right, $bottom) = | ||
| 53 | add_point_to_bounding_box($elem->{end}, | ||
| 54 | $left, $top, $right, $bottom); | ||
| 55 | } elsif ($elem->{type} eq 'closepath') { | ||
| 56 | # do nothing | ||
| 57 | } else { | ||
| 58 | warn "ERROR: unsupported path instruction '" . $elem->{type} . "'\n"; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | } elsif ($obj->getElementName() eq "line") { | ||
| 62 | warn "<line id='" . $obj->getAttribute("id") . "'" | ||
| 63 | . " x1='" . $obj->getAttribute("x1") . "'" | ||
| 64 | . " y1='" . $obj->getAttribute("y1") . "'" | ||
| 65 | . " x2='" . $obj->getAttribute("x2") . "'" | ||
| 66 | . " y2='" . $obj->getAttribute("y2") . "'>\n"; | ||
| 67 | $left = smallest($left, $obj->getAttribute("x1"), $obj->getAttribute("x2")); | ||
| 68 | $right = biggest($right, $obj->getAttribute("x1"), $obj->getAttribute("x2")); | ||
| 69 | $bottom = smallest($bottom, $obj->getAttribute("y1"), $obj->getAttribute("y2")); | ||
| 70 | $top = biggest($bottom, $obj->getAttribute("y1"), $obj->getAttribute("y2")); | ||
| 71 | } elsif ($obj->getElementName() eq "rect") { | ||
| 72 | warn "<rect id='" . $obj->getAttribute("id") . "'" | ||
| 73 | . " x='" . $obj->getAttribute("x") . "'" | ||
| 74 | . " y='" . $obj->getAttribute("y") . "'" | ||
| 75 | . " width='" . $obj->getAttribute("width") . "'" | ||
| 76 | . " height='" . $obj->getAttribute("height") . "'>\n"; | ||
| 77 | $left = smallest($left, $obj->getAttribute("x")); | ||
| 78 | $right = biggest ($right, $obj->getAttribute("x") + $obj->getAttribute("width")); | ||
| 79 | $bottom = smallest($bottom, $obj->getAttribute("y")); | ||
| 80 | $top = biggest($top, $obj->getAttribute("y") + $obj->getAttribute("height")); | ||
| 81 | } elsif ($obj->getElementName() eq "polyline") { | ||
| 82 | warn "<polyline id='" . $obj->getAttribute("id") . "'" | ||
| 83 | . " points='" . $obj->getAttribute("points") . "'>\n"; | ||
| 84 | my @numbers = split(/(?:,|(?=-)|\s+)/, $obj->getAttribute("points")); | ||
| 85 | for (my $i = 0; $i < @numbers / 2; $i++) { | ||
| 86 | my $offset = $i * 2; | ||
| 87 | # warn " (" . $numbers[$offset] . ", " . $numbers[$offset + 1] . ")\n"; | ||
| 88 | ($left, $top, $right, $bottom) = | ||
| 89 | add_point_to_bounding_box([@numbers[$offset, $offset + 1]], | ||
| 90 | $left, $top, $right, $bottom); | ||
| 91 | } | ||
| 92 | } elsif ($obj->getElementName() eq "g") { | ||
| 93 | # do nothing | ||
| 94 | } else { | ||
| 95 | warn "ERROR: unsupported element " . $obj->getElementName() . "\n"; | ||
| 96 | } | ||
| 97 | |||
| 98 | warn "Bounding box: (" | ||
| 99 | . join(", ", map { defined($_) ? $_ : "?"} $left, $top, $right, $bottom) . ")\n"; | ||
| 100 | |||
| 101 | foreach my $child ($obj->getChildren()) { | ||
| 102 | ($left, $top, $right, $bottom) = | ||
| 103 | push_bounding_box($child, $left, $top, $right, $bottom); | ||
| 104 | } | ||
| 105 | |||
| 106 | return ($left, $top, $right, $bottom); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub scale_object { | ||
| 110 | my ($obj, $dx1, $dy1, $scale, $dx2, $dy2) = @_; | ||
| 111 | if ($obj->getElementName() eq "path") { | ||
| 112 | # $point remains unscaled | ||
| 113 | my $point = [0, 0]; | ||
| 114 | my @points = extract_path_info ($obj->getAttribute("d"), {absolute => 1}); | ||
| 115 | foreach my $elem (@points) { | ||
| 116 | if ($elem->{type} eq 'moveto') { | ||
| 117 | $point->[0] = $elem->{point}->[0]; | ||
| 118 | $point->[1] = $elem->{point}->[1]; | ||
| 119 | $elem->{point}->[0] = ($point->[0] + $dx1) * $scale + $dx2; | ||
| 120 | $elem->{point}->[1] = ($point->[1] + $dy1) * $scale + $dy2 + $dy2; | ||
| 121 | } elsif ($elem->{type} eq 'cubic-bezier') { | ||
| 122 | # $c1 and $c2 remain unscaled | ||
| 123 | my $c1 = dclone($point); | ||
| 124 | my $c2 = dclone($point); | ||
| 125 | $point->[0] = $elem->{end}->[0]; | ||
| 126 | $point->[1] = $elem->{end}->[1]; | ||
| 127 | $c1->[0] = $elem->{control1}->[0]; | ||
| 128 | $c1->[1] = $elem->{control1}->[1]; | ||
| 129 | $c2->[0] = $elem->{control2}->[0]; | ||
| 130 | $c2->[1] = $elem->{control2}->[1]; | ||
| 131 | $elem->{end}->[0] = ($point->[0] + $dx1) * $scale + $dx2; | ||
| 132 | $elem->{end}->[1] = ($point->[1] + $dy1) * $scale + $dy2; | ||
| 133 | $elem->{control1}->[0] = ($c1->[0] + $dx1) * $scale + $dx2; | ||
| 134 | $elem->{control1}->[1] = ($c1->[1] + $dy1) * $scale + $dy2; | ||
| 135 | $elem->{control2}->[0] = ($c2->[0] + $dx1) * $scale + $dx2; | ||
| 136 | $elem->{control2}->[1] = ($c2->[1] + $dy1) * $scale + $dy2; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | $obj->setAttribute("d", create_path_string(\@points)); | ||
| 140 | warn "scaled: <path id='" . $obj->getAttribute("id") . "'" | ||
| 141 | . " d='" . $obj->getAttribute("d") . "'>\n"; | ||
| 142 | } elsif ($obj->getElementName() eq "line") { | ||
| 143 | $obj->setAttribute("x1", ($obj->getAttribute("x1") + $dx1) * $scale + $dx2); | ||
| 144 | $obj->setAttribute("y1", ($obj->getAttribute("y1") + $dy1) * $scale + $dy2); | ||
| 145 | $obj->setAttribute("x2", ($obj->getAttribute("x2") + $dx1) * $scale + $dx2); | ||
| 146 | $obj->setAttribute("y2", ($obj->getAttribute("y2") + $dy1) * $scale + $dy2); | ||
| 147 | warn "scaled: <line id='" . $obj->getAttribute("id") . "'" | ||
| 148 | . " x1='" . $obj->getAttribute("x1") . "'" | ||
| 149 | . " y1='" . $obj->getAttribute("y1") . "'" | ||
| 150 | . " x2='" . $obj->getAttribute("x2") . "'" | ||
| 151 | . " y2='" . $obj->getAttribute("y2") . "'>\n"; | ||
| 152 | } elsif ($obj->getElementName() eq "rect") { | ||
| 153 | $obj->setAttribute("x", ($obj->getAttribute("x") + $dx1) * $scale + $dx2); | ||
| 154 | $obj->setAttribute("y", ($obj->getAttribute("y") + $dy1) * $scale + $dy2); | ||
| 155 | $obj->setAttribute("width", $obj->getAttribute("width") * $scale); | ||
| 156 | $obj->setAttribute("height", $obj->getAttribute("height") * $scale); | ||
| 157 | warn "scaled: <rect id='" . $obj->getAttribute("id") . "'" | ||
| 158 | . " x='" . $obj->getAttribute("x") . "'" | ||
| 159 | . " y='" . $obj->getAttribute("y") . "'" | ||
| 160 | . " width='" . $obj->getAttribute("width") . "'" | ||
| 161 | . " height='" . $obj->getAttribute("height") . "'>\n"; | ||
| 162 | } elsif ($obj->getElementName() eq "polyline") { | ||
| 163 | my @points = (); | ||
| 164 | my @numbers = split(/(?:,|(?=-)|\s+)/, $obj->getAttribute("points")); | ||
| 165 | for (my $i = 0; $i < @numbers / 2; $i++) { | ||
| 166 | my $offset = $i * 2; | ||
| 167 | push(@points, (($numbers[$offset] + $dx1) * $scale + $dx2) | ||
| 168 | . "," . (($numbers[$offset + 1] + $dy1) * $scale + $dy2)); | ||
| 169 | } | ||
| 170 | $obj->setAttribute("points", join(" ", @points)); | ||
| 171 | warn "scaled <polyline id='" . $obj->getAttribute("id") . "'" | ||
| 172 | . " points='" . $obj->getAttribute("points") . "'>\n"; | ||
| 173 | } else { | ||
| 174 | warn "not scaled: " . $obj->getElementName() . "\n"; | ||
| 175 | } | ||
| 176 | |||
| 177 | # fix stroke-width | ||
| 178 | my $style = $obj->getAttribute("style"); | ||
| 179 | if ($style->{"stroke-width"}) { | ||
| 180 | $style->{"stroke-width"} *= $scale; | ||
| 181 | warn " setting stroke-width: " . $style->{"stroke-width"} . "\n"; | ||
| 182 | $obj->setAttribute("style", $style); | ||
| 183 | } | ||
| 184 | if ($obj->getAttribute("stroke-width")) { | ||
| 185 | $obj->setAttribute("stroke-width", $obj->getAttribute("stroke-width") * $scale); | ||
| 186 | } | ||
| 187 | return $obj; | ||
| 188 | } | ||
| 189 | |||
| 190 | sub transform { | ||
| 191 | my ($left, $top, $right, $bottom) = @_; | ||
| 192 | my $dx1 = -$left; | ||
| 193 | my $dy1 = -$bottom; | ||
| 194 | my $scale = min(512/abs($top-$bottom), 512/abs($right-$left)); | ||
| 195 | my $dx2 = (512 - ($right - $left) * $scale) / 2; | ||
| 196 | my $dy2 = (512 - ($top - $bottom) * $scale) / 2; | ||
| 197 | warn "dx1: $dx1; dy1: $dy1; scale: $scale; dx2: $dx2; dy2: $dy2\n"; | ||
| 198 | return ($dx1, $dy1, $scale, $dx2, $dy2); | ||
| 199 | } | ||
| 200 | |||
| 201 | sub fit_into_bounding_box { | ||
| 202 | my ($obj, $dx1, $dy1, $scale, $dx2, $dy2) = @_; | ||
| 203 | warn "fitting " . $obj->getElementName() . "\n"; | ||
| 204 | my $clone = scale_object($obj->cloneNode(), $dx1, $dy1, $scale, $dx2, $dy2); | ||
| 205 | foreach my $child ($obj->getChildren()) { | ||
| 206 | $clone->appendChild(fit_into_bounding_box($child, $dx1, $dy1, $scale, $dx2, $dy2)); | ||
| 207 | } | ||
| 208 | return $clone; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub clean { | ||
| 212 | my $obj = shift; | ||
| 213 | foreach my $attr ($obj->getAttributes()) { | ||
| 214 | if ($attr =~ /^(inkscape:|transform|id)/) { | ||
| 215 | $obj->setAttribute($attr, undef); | ||
| 216 | } | ||
| 217 | } | ||
| 218 | foreach my $child ($obj->getChildren()) { | ||
| 219 | clean($child); | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | sub main { | ||
| 224 | die "Usage: $0 <file> <id>\n" unless 2 == @ARGV; | ||
| 225 | my ($file, $id) = @ARGV; | ||
| 226 | my $parser = new SVG::Parser(); | ||
| 227 | my $svg = SVG::Parser->new()->parsefile($file); | ||
| 228 | my $ref = $svg->getElementByID($id); | ||
| 229 | my @bounding_box = push_bounding_box($ref, undef, undef, undef, undef); | ||
| 230 | my $clone = fit_into_bounding_box($ref, transform(@bounding_box)); | ||
| 231 | clean($clone); | ||
| 232 | my $xml = $clone->xmlify(); | ||
| 233 | warn "$xml\n"; | ||
| 234 | $xml =~ s/(^|>)\n\s*</$1</g; | ||
| 235 | warn "removing whitespace...\n"; | ||
| 236 | print qq{<svg viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg">$xml</svg>\n}; | ||
| 237 | # <rect x="0" y="0" width="512" height="512" /> | ||
| 238 | } | ||
| 239 | |||
| 240 | main(); |