forked from imagico/jsdotpattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg_pattern.sh
executable file
·62 lines (49 loc) · 2.06 KB
/
svg_pattern.sh
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
#!/bin/sh
# ------------------------------------------------------------
# sanitize a jsdotpattern produced SVG for use with Mapnik
# ------------------------------------------------------------
SRC="$1"
TARGET="$2"
CLIP="$3"
if [ -z "$1" ] || [ -z "$2" ] ; then
echo "Usage: $0 <input> <output> [clip_width]"
echo "Warning: do not use this on SVGs not generated with jsdotpattern,"
echo " it likely don't work"
exit
fi
SIZE=`grep "width" "$SRC" | head -n 1 | sed "s?\"??g;s?.*width=\([0-9]*\).*?\1?"`
if [ -z "$SIZE" ] ; then
echo "Error: pattern size could not be read from source SVG."
fi
if [ -z "$CLIP" ] ; then
CLIP="$SIZE"
fi
cp "$SRC" "$TARGET"
# this is for normal jsdotpattern SVGs with <use>
if grep "<use" "$TARGET" > /dev/null ; then
echo "processing non-inlined SVG..."
inkscape -g --verb EditSelectAll --verb SelectionUnGroup \
--verb EditSelectAll --verb EditUnlinkClone \
--verb EditSelectAll --verb SelectionUnGroup \
--verb EditSelectAll --verb SelectionUnGroup \
--verb EditSelectAll --verb ObjectToPath \
--verb EditSelectAll --verb SelectionCombine \
--verb FileSave --verb FileClose \
--verb FileQuit "$TARGET"
else # this is for inline SVGs
echo "processing inlined SVG..."
inkscape -g --verb EditSelectAll --verb SelectionUnGroup \
--verb EditSelectAll --verb SelectionUnGroup \
--verb EditSelectAll --verb SelectionUnGroup \
--verb EditSelectAll --verb SelectionUnGroup \
--verb EditSelectAll --verb ObjectToPath \
--verb EditSelectAll --verb SelectionCombine \
--verb FileSave --verb FileClose \
--verb FileQuit "$TARGET"
fi
# this is pretty whacky since it depends on the specific structure of the file (specifically the presence of a <desc> tag at the very end)
sed -i -e "s?<desc?<rect width=\"$SIZE\" height=\"$CLIP\" rx=\"0\" x=\"0\" y=\"0\" id=\"rect8566\" style=\"fill:none;fill-opacity:1;stroke:none\" /><desc?" "$TARGET"
inkscape -g --verb EditSelectAll --verb SelectionIntersect \
--verb FileSave --verb FileClose \
--verb FileQuit "$TARGET"
inkscape -z --vacuum-defs -l "$TARGET" -f "$TARGET"