-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen.sh
executable file
·113 lines (94 loc) · 3.71 KB
/
gen.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
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
#!/bin/bash
# Convert one or more SVG to PNGs and PDFs.
# Will recursively find all SVG files in the specified dir or file and
# convert them to PNGs and PDFs in a new "export" dir in the same dir as the SVG file.
# Make sure the provided paths don't overlap.
# Dependencies: bc parallel [python-]cairosvg
# Usage: ./gen.sh <input-dirs-or-files>
# Example usage: ./gen.sh trondelan-2021
set -eu
export png_width=8000
if (( $# < 1 )); then
echo "Usage: $0 <input-dirs-or-files ...>" 2>&1
echo "Example: $0 *" 2>&1
exit 1
fi
# Check if required commands are present
command -v bc &> /dev/null || (echo "bc not found, please install it." >&2 ; exit 1)
command -v parallel &> /dev/null || (echo "parallel not found, please install it." >&2 ; exit 1)
command -v cairosvg &> /dev/null || (echo "cairosvg not found, please install it." >&2 ; exit 1)
echo "(If no files are getting converted, you may be using the wrong variant of parallel.)"
# Process a single SVG file
# Arg 1: Full file path
function gen_file {
input_file="$1"
file_basename="$(basename "$1")"
file_basename="${file_basename%.svg}"
input_dir="$(dirname "$input_file")"
export_dir="$input_dir/export"
echo "Converting file: $input_file"
if [[ ! -f $input_file ]]; then
echo "Input file isn't a file, skipping: $input_file" 2>&1
continue
fi
# Setup tmp-file for temporary processing later
tmp_file="$(mktemp)"
trap "rm $tmp_file" EXIT
# Figure out dims
# Example: viewBox="-0.080299786 0 1000.101 1116"
dims="$(grep -m1 -oP '(?<=viewBox=")[]+ +[0-9-.]+ +[0-9-.]+ +[0-9-.]+(?=")' "$input_file" || true)"
x1="$(echo "$dims" | cut -d' ' -f1)"
y1="$(echo "$dims" | cut -d' ' -f2)"
x2="$(echo "$dims" | cut -d' ' -f3)"
y2="$(echo "$dims" | cut -d' ' -f4)"
width=
height=
if [[ $x1 != "" && $y1 != "" && $x2 != "" && $y2 != "" ]]; then
width=$(echo "$x2 - $x1" | bc -l)
height=$(echo "$y2 - $y1" | bc -l)
fi
if [[ $width == "" || $height == "" ]]; then
# Example: <svg height="1000" width="1000">
dims="$(grep -m1 '<svg ' "$input_file" || true)"
width="$(echo "$dims" | grep -oP '(?<=width=")[0-9-.]+(?=(px)?")' || true)"
height="$(echo "$dims" | grep -oP '(?<=height=")[0-9-.]+(?=(px)?")' || true)"
fi
if [[ $width == "" || $height == "" ]]; then
echo "SVG dims not found for image, skipping: $input_file" 2>&1
continue
fi
#echo "WIDTH=$width HEIGHT=$height"
png_height=$(echo "$png_width / $width * $height" | bc -l)
png_height=${png_height%.*}
#echo "PNG_WIDTH=$png_width PNG_HEIGHT=$png_height"
if [[ $png_height == "" ]] || (( png_height <= 0 )); then
echo "Unable to determine output height, skipping: $input_file" 2>&1
continue
fi
# Create temporary file to filter bad content
cat < "$input_file" > "$tmp_file"
sed -i 's/xlink://g' "$tmp_file"
# Generate PDF
cairosvg "$tmp_file" -o "$export_dir/$file_basename.pdf" -f pdf
# Generate PNG
cairosvg "$tmp_file" -o "$export_dir/$file_basename.png" -f png --output-width "$png_width" --output-height "$png_height"
}
export -f gen_file
# Delete old exports
for input_element in "$@"; do
for export_dir in $(find "$input_element" -type d -name 'export'); do
echo "Deleting old export dir: $export_dir"
rm -rf "$export_dir"
done
done
# Create new export dirs and record input files
declare -a input_files
for input_element in "$@"; do
for input_file in $(find "$input_element" -type f -name '*.svg'); do
input_files+=("$input_file")
mkdir -p "$(dirname "$input_file")/export"
done
done
# Process files in parallel
parallel gen_file ::: "${input_files[@]}"
echo "Done!"