forked from mozilla-b2g/gaia
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1133402 - Add a script to optimise SVG images
- Loading branch information
Showing
1 changed file
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#!/bin/sh | ||
|
||
############################################################################### | ||
# Compress SVG files using svgo (https://github.com/svg/svgo) | ||
# | ||
# The following svgo plugins are disabled: | ||
# * convertPathData | ||
# Though the resulting files are OK in any browsers, they're rendered in a | ||
# weird way on Ubuntu files explorer. | ||
# * mergePaths | ||
# Can result on wrong odd/even filling if some paths are overlapping. | ||
|
||
############################################################################### | ||
# Option variables | ||
|
||
opt_help=no | ||
opt_paths="" | ||
|
||
############################################################################### | ||
# Print the help message | ||
|
||
print_help() { | ||
printf "Usage: `basename $0` [OPTION...] [PATH...] | ||
Recompresses SVG files using 'svgo' tools. | ||
Help options: | ||
-h, --help Show this help message | ||
" | ||
} | ||
|
||
suggest_help() { | ||
printf "Try \``basename $0` --help' for more information\n" | ||
} | ||
|
||
############################################################################### | ||
# Parse the program arguments | ||
|
||
parse_args() { | ||
# Set the options variables depending on the passed arguments | ||
while [ $# -gt 0 ]; do | ||
if [ `expr "$1" : "^-"` -eq 1 ]; then | ||
if [ $1 = "-h" ] || [ $1 = "--help" ]; then | ||
opt_help=yes | ||
shift 1 | ||
else | ||
printf "error: Unknown option $1\n" | ||
exit 1 | ||
fi | ||
else | ||
# No more arguments, the following parameters will be paths | ||
break | ||
fi | ||
done | ||
|
||
if [ $# -eq 0 ] && [ $opt_help != yes ]; then | ||
printf "error: no files specified\n" | ||
suggest_help | ||
exit 1 | ||
fi | ||
|
||
# Gather all the paths | ||
while [ $# -gt 0 ]; do | ||
opt_paths="$opt_paths$1\n" | ||
shift | ||
done | ||
} | ||
|
||
############################################################################### | ||
# Check for program | ||
|
||
check_program() { | ||
which svgo 1>/dev/null 2>/dev/null | ||
|
||
if [ $? -ne 0 ]; then | ||
printf "error: svgo not found\n" | ||
exit 1; | ||
fi | ||
} | ||
|
||
############################################################################### | ||
# Main script | ||
|
||
parse_args "$@" | ||
check_program | ||
|
||
if [ $opt_help = yes ] || [ $# -eq 0 ]; then | ||
print_help | ||
exit 0 | ||
fi | ||
|
||
# Process all the specified paths | ||
while [ $# -gt 0 ]; do | ||
svgo --disable=convertPathData --disable=mergePaths --multipass --folder="$1" | ||
shift | ||
done |