diff --git a/README.md b/README.md index cea91991..475e2d31 100644 --- a/README.md +++ b/README.md @@ -51,10 +51,10 @@ flags were resolved for the initial import: - The script `regions.py` lists all regions and some selected sub-regions with their metadata. - The script `regions-wp.py` shows the Wikipedia URL for the flag page. - The script `missing.sh` shows all such regions that we don't have flags for. -- The script `make-aliases.sh` makes symlinks for regions that use flag of another -region. -- The script `download-wp.py` downloads missing flags from Wikipedia and generating -optimized SVG and PNG versions. +- The script `make-aliases.sh` makes symlinks for regions that use flag of another region. +- The script `download-wp.sh` downloads missing flags from Wikipedia and optionally you can generate optimized SVG and PNG versions (e.g., download-wp.py true). +- The script `update.sh` automatically regenerates all files and outputs optimized png. +- The script `convert.sh` converts SVG to PNG with ability to specify Width or Height. You can use the [waveflag script from the Noto fonts project](https://code.google.com/p/noto/source/browse/color_emoji/waveflag.c) to _wave_ PNG flags. @@ -80,7 +80,8 @@ national laws), make a note of it in file `COPYING`. To download missing flags, run `download-wp.py`. To update to latest flags from Wikipedia, delete the `html`, `svg`, and `png` -directories, then run `make-aliases.sh` followed by `download-wp.py`. +directories, then run `make-aliases.sh` followed by `download-wp.py` or you can use `update.sh`. + # License diff --git a/convert.sh b/convert.sh new file mode 100644 index 00000000..101745ac --- /dev/null +++ b/convert.sh @@ -0,0 +1,117 @@ +#!/usr/bin/env sh + +echo "Options: w, width, h, height, n, normal, default" +echo -n "Enter your option: " +read OPTION + +echo "" + +echo "Options: yes, true, 1" +echo -n "Do you want to optimize the converted file? " +read OPTIPNGOP + +updatealiases(){ + echo "Updating aliases!" + IFS=' ' + while read dst src; do + for dir in png; do + s="$dir/$src.$dir" + d="$dir/$dst.$dir" + cp -f "$s" "$d" + done + done < data/ALIASES +} + +rsvgconvert() { + case $OPTIPNGOP in + "yes" | "true" | "1") + echo "Converting svg/$region.svg to png/$region.png.tmp" + if ! rsvg-convert $1 -f png -o "png/$region.png.tmp" "svg/$region.svg"; then + echo "ERROR rsvg-convert failed." + rm -f "png/$region.png.tmp" + else + echo "Optimizing png/$region.png!" + if ! optipng -quiet "png/$region.png.tmp"; then + echo "ERROR: optipng failed." + rm -f "png/$region.png.tmp" + continue + else + mv "png/$region.png.tmp" "png/$region.png" + fi + fi + echo "" + ;; + + *) + echo "Converting svg/$region.svg to png/$region.png" && rsvg-convert $1 -f png -o "png/$region.png" "svg/$region.svg" + ;; + esac +} + +usersvgconvert() { + + WIDTH=$1; + HEIGHT=$2; + + if [ $WIDTH -ne 0 ]; then + IFS=' ' + cat SOURCES | + while read region htmlurl ; do + if [ -f "svg/$region.svg" ]; then + rsvgconvert "-a -w ${WIDTH}" + else + echo "File svg/$region.svg doesn't exist!" + fi + done + fi + + if [ $HEIGHT -ne 0 ]; then + IFS=' ' + cat SOURCES | + while read region htmlurl ; do + if [ -f "svg/$region.svg" ]; then + rsvgconvert "-a -h ${HEIGHT}" + else + echo "File svg/$region.svg doesn't exist!" + fi + done + fi + + if [ $WIDTH -eq 0 ] && [ $HEIGHT -eq 0 ]; then + + IFS=' ' + cat SOURCES | + while read region htmlurl ; do + if [ -f "svg/$region.svg" ]; then + rsvgconvert "-a" + else + echo "File svg/$region.svg doesn't exist!" + fi + done + fi + + updatealiases +} + + +case $OPTION in + w | width) + echo -n "Enter the prefered width: " + read WIDTH + usersvgconvert ${WIDTH} 0 + ;; + + h | height) + echo -n "Enter the prefered height: " + read HEIGHT + usersvgconvert 0 ${HEIGHT} + ;; + + n | normal | default) + usersvgconvert 0 0 + ;; + + *) + echo "Unknown option!" + ;; +esac \ No newline at end of file diff --git a/download-wp.sh b/download-wp.sh index 98e18da7..046c6278 100755 --- a/download-wp.sh +++ b/download-wp.sh @@ -4,57 +4,68 @@ IFS=' ' cat SOURCES | while read region htmlurl ; do - - html="html/$region.html" - svg="svg/$region.svg" - png="png/$region.png" - - if ! test -s "$html"; then - rm -f "$html" "$svg" "$png" - echo "Downloading: $region $htmlurl" - if ! wget -q -O "$html" "${htmlurl}"; then - echo "ERROR: failed downloading html: ${htmlurl}" - continue - fi - if ! grep -q public_domain $html; then - echo "WARNING: flag NOT in public domain; check license" - fi - fi - - if ! test -s "$svg"; then - svgurl=`cat "$html" | LANG=C sed 's@.*href="\(https://upload.wikimedia.org/wikipedia/commons/[^"]*[.]svg\)".*@\1@' | grep '^https://upload.wikimedia.org' | grep -v '/archive/' | head -n1` - if test "x$svgurl" = x; then - echo "ERROR: cannot find SVG URL in $html" - continue - fi - svgdata="`wget -q -O - "$svgurl"`" - if test "x$svgdata" = x; then - echo "ERROR: failed downloading SVG: $svgurl" - continue - fi - echo "$svgdata" > $svg.dos - if ! (dos2unix -q $svg.dos || fromdos $svg.dos); then - echo "ERROR: dos2unix failed." - #XXX rm -f $svg.dos - continue - else - mv $svg.dos $svg - fi - fi - - if ! test -s "$png"; then - if ! rsvg-convert $svg > $png.tmp; then - echo "ERROR rsvg-convert failed." - rm -f $png.tmp - continue - fi - if ! optipng -quiet $png.tmp; then - echo "ERROR: optipng failed." - rm -f $png.tmp - continue - else - mv $png.tmp $png - fi - fi - + + html="html/$region.html" + svg="svg/$region.svg" + png="png/$region.png" + + if ! test -s "$html"; then + rm -f "$html" "$svg" "$png" + echo "Downloading: $region $htmlurl" + if ! wget -q -O "$html" "${htmlurl}"; then + echo "ERROR: failed downloading html: ${htmlurl}" + continue + fi + if ! grep -q public_domain $html; then + echo "WARNING: flag NOT in public domain; check license" + fi + fi + + if ! test -s "$svg"; then + svgurl=`cat "$html" | LANG=C sed 's@.*href="\(https://upload.wikimedia.org/wikipedia/commons/[^"]*[.]svg\)".*@\1@' | grep '^https://upload.wikimedia.org' | grep -v '/archive/' | head -n1` + if test "x$svgurl" = x; then + echo "ERROR: cannot find SVG URL in $html" + continue + fi + svgdata="`wget -q -O - "$svgurl"`" + if test "x$svgdata" = x; then + echo "ERROR: failed downloading SVG: $svgurl" + continue + fi + echo "$svgdata" > $svg.dos + if ! (dos2unix -q $svg.dos || fromdos $svg.dos); then + echo "ERROR: dos2unix failed." + #XXX rm -f $svg.dos + continue + else + mv $svg.dos $svg + fi + fi + + case $1 in + "true" | "yes") + if ! test -s "$png"; then + echo "Converting $svg to $png.tmp" + if ! rsvg-convert $svg > $png.tmp; then + echo "ERROR rsvg-convert failed." + rm -f $png.tmp + continue + else + echo "Optimizing $png!" + if ! optipng -quiet $png.tmp; then + echo "ERROR: optipng failed." + rm -f $png.tmp + continue + else + mv $png.tmp $png + fi + fi + fi + ;; + + *) + continue + ;; + esac + echo "" done diff --git a/update.sh b/update.sh index 8e5ec3d6..cc5a1bb9 100755 --- a/update.sh +++ b/update.sh @@ -6,5 +6,6 @@ python3 ./regions-wp.py > SOURCES rm -rf html svg png mkdir -p html svg png -bash ./download-wp.sh +bash ./download-wp.sh true bash ./make-aliases.sh +