-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathsvg-export
executable file
Β·134 lines (112 loc) Β· 2.85 KB
/
svg-export
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
#!/usr/bin/env bash
# @todo merge into [convert-helper]
function svg_export() (
source "$DOROTHY/sources/bash.bash"
local formats=(
png
pdf
svg
eps
ps
)
# =====================================
# Arguments
function help {
cat <<-EOF >/dev/stderr
ABOUT:
Convert SVGs into PNGs.
USAGE:
svg-export [...options] -- ...<input>
OPTIONS:
--dpi=<dpi>
Set the display pixels per inch, e.g. [150].
--scale=<scale>
Set the scale factor, e.g. [2] for 2x.
--background=<background-color>
Set the background color, defaults to [white].
To keep transparency, use [--background=].
--format=$(
echo-join '|' -- "${formats[@]}"
)]
EXAMPLE:
svg-export -- *.svg
EOF
if [[ $# -ne 0 ]]; then
echo-error "$@"
fi
return 22 # EINVAL 22 Invalid argument
}
# process
local item files=() dpi='' scale='' background='white' format=''
while [[ $# -ne 0 ]]; do
item="$1"
shift
case "$item" in
'--help' | '-h') help ;;
'--dpi='*) dpi="${item#*=}" ;;
'--scale='*) scale="${item#*=}" ;;
'--background='* | '--bg='*) background="${item#*=}" ;;
'--format='*) format="${item#*=}" ;;
'--')
files+=("$@")
shift $#
break
;;
'--'*) help "An unrecognised flag was provided: $item" ;;
*) help "An unrecognised argument was provided: $item" ;;
esac
done
# if no files, fail
if [[ ${#files[@]} -eq 0 ]]; then
help 'No <input>s provided.'
fi
# ensure correct format
format="$(
choose --required \
--question='Which format to export to?' \
--default="$format" -- "${formats[@]}"
)"
# =====================================
# Dependencies
# https://github.com/sharkdp/bat
# cat alternative
setup-util --quiet --cli='cairosvg' \
PIP='cairosvg'
# =====================================
# Act
# adjustments
local suffix='' args=(
--format "$format"
)
if [[ -n $scale ]]; then
suffix+=" [scale=$scale]"
args+=(--scale "$scale")
fi
if [[ -n $dpi ]]; then
suffix+=" [dpi=$dpi]"
args+=(--dpi "$dpi")
fi
if [[ -n $background ]]; then
suffix+=" [bg=$background]"
args+=(--background "$background")
fi
suffix+=".$format"
# convert
local input output prefix
for input in "${files[@]}"; do
prefix="$(fs-filename -- "$input")"
output="${prefix}${suffix}"
eval-helper --quiet \
--pending="$(echo-style --bold="Converting " --code="$input" --bold=" to " --code="$output")" \
--success="$(echo-style --bold+green="Converted " --code="$input" --bold=" to " --code="$output")" \
--failure="$(echo-style --bold+red="Failed to convert " --code="$input" --bold=" to " --code="$output")" \
-- cairosvg "${args[@]}" --output "$output" "$input" || :
# svg2png doesn't work well
# https://github.com/domenic/svg2png
# svg2png --scale="$scale" "$input" "$output"
done
)
# fire if invoked standalone
if [[ $0 == "${BASH_SOURCE[0]}" ]]; then
svg_export "$@"
fi