forked from osmandapp/OsmAnd-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrasterize-resources.sh
executable file
·76 lines (65 loc) · 2.3 KB
/
rasterize-resources.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
#!/bin/bash
if [ -z "$BASH_VERSION" ]; then
echo "Invalid shell, re-running using bash..."
exec bash "$0" "$@"
exit $?
fi
SRCLOC="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Find rsvg-convert
if [ -z "$RSVG_CONVERT" ]; then
RSVG_CONVERT=`which rsvg-convert`
fi
if [ -z "$RSVG_CONVERT" ]; then
echo "rsvg-convert tool not found"
exit 1
fi
echo "Using $RSVG_CONVERT..."
export RSVG_CONVERT
# Remove previous
rm -rf "$SRCLOC/Resources/Rasterized"
# Rasterize resources
rasterize_resource() {
if [ -z "$BASH_VERSION" ]; then
echo "Invalid shell, re-running using bash..."
exec bash -c 'rasterize_resource "$0"'
exit $?
fi
ORIGIN="$(pwd)/.."
INPUT_FILENAME="${0##*/}"
INPUT_FORMAT="${INPUT_FILENAME##*.}"
FILENAME="${INPUT_FILENAME%.*}"
OUTPUT_FILENAME="${FILENAME}.png"
SUBPATH="${0%/*}"
INPUT="Resources.svg/$SUBPATH/$INPUT_FILENAME"
OUTPUT="Resources/Rasterized/$SUBPATH/$OUTPUT_FILENAME"
OUTPUT_PATH="$ORIGIN/Resources/Rasterized/$SUBPATH"
echo "Rasterizing '$FILENAME' (\"$SUBPATH\")"
# Ensure output path exists
mkdir -p "$OUTPUT_PATH"
# Rasterize this version as 1:1 version
(cd "$ORIGIN" && $RSVG_CONVERT -f png -o "$OUTPUT" "$INPUT")
# If original is 1x scale, ...
if ! [[ "$FILENAME" =~ @2x$ ]]; then
# ... and there's no source version that provides 2x, rasterize larger version
if [ ! -f "$ORIGIN/Resources.svg/$SUBPATH/${FILENAME}@2x.${INPUT_FORMAT}" ]; then
printf "\t+ 2.0x\n"
(cd "$ORIGIN" && $RSVG_CONVERT -z 2 -f png -o "$OUTPUT_PATH/${FILENAME}@2x.png" "$INPUT")
printf "\t+ 3.0x\n"
(cd "$ORIGIN" && $RSVG_CONVERT -z 3 -f png -o "$OUTPUT_PATH/${FILENAME}@3x.png" "$INPUT")
fi
fi
# If original is 2x scale, ...
if [[ "$FILENAME" =~ @2x$ ]]; then
# ... and there's no source version that provides 1x, rasterize smaller version
if [ ! -f "$ORIGIN/Resources.svg/$SUBPATH/${FILENAME%@2x}.${INPUT_FORMAT}" ]; then
printf "\t+ 2x * 0.5x = 1.0x\n"
(cd "$ORIGIN" && $RSVG_CONVERT -z 0.5 -f png -o "$OUTPUT_PATH/${FILENAME%@2x}.png" "$INPUT")
fi
printf "\t+ 2x * 1.5x = 3.0x\n"
(cd "$ORIGIN" && $RSVG_CONVERT -z 1.5 -f png -o "$OUTPUT_PATH/${FILENAME%@2x}@3x.png" "$INPUT")
fi
return $?
}
export -f rasterize_resource
(cd "$SRCLOC/Resources.svg" && \
find . -type f -name "*.svg" -exec sh -c 'rasterize_resource "$0"' {} \;)