genflags.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright 2017 Vector Creations Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # genflags.sh - Generates pngs for use with CountryDropdown.js
  15. #
  16. # Dependencies:
  17. # - imagemagick --with-rsvg (because default imagemagick SVG
  18. # renderer does not produce accurate results)
  19. #
  20. # on macOS, this is most easily done with:
  21. # brew install imagemagick --with-librsvg
  22. #
  23. # This will clone the googlei18n flag repo before converting
  24. # all phonenumber.js-supported country flags (as SVGs) into
  25. # PNGs that can be used by CountryDropdown.js.
  26. set -e
  27. # Allow CTRL+C to terminate the script
  28. trap "echo Exited!; exit;" SIGINT SIGTERM
  29. # git clone the google repo to get flag SVGs
  30. git clone [email protected]:googlei18n/region-flags
  31. for f in region-flags/svg/*.svg; do
  32. # Skip state flags
  33. if [[ $f =~ [A-Z]{2}-[A-Z]{2,3}.svg ]] ; then
  34. echo "Skipping state flag "$f
  35. continue
  36. fi
  37. # Skip countries not included in phonenumber.js
  38. if [[ $f =~ (AC|CP|DG|EA|EU|IC|TA|UM|UN|XK).svg ]] ; then
  39. echo "Skipping non-phonenumber supported flag "$f
  40. continue
  41. fi
  42. # Run imagemagick convert
  43. # -background none : transparent background
  44. # -resize 50x30 : resize the flag to have a height of 15px (2x)
  45. # By default, aspect ratio is respected so the width will
  46. # be correct and not necessarily 25px.
  47. # -filter Lanczos : use sharper resampling to avoid muddiness
  48. # -gravity Center : keep the image central when adding an -extent
  49. # -border 1 : add a 1px border around the flag
  50. # -bordercolor : set the border colour
  51. # -extent 54x54 : surround the image with padding so that it
  52. # has the dimensions 27x27px (2x).
  53. convert $f -background none -filter Lanczos -resize 50x30 \
  54. -gravity Center -border 1 -bordercolor \#e0e0e0 \
  55. -extent 54x54 $f.png
  56. # $f.png will be region-flags/svg/XX.svg.png at this point
  57. # Extract filename from path $f
  58. newname=${f##*/}
  59. # Replace .svg with .png
  60. newname=${newname%.svg}.png
  61. # Move the file to flags directory
  62. mv $f.png ../res/flags/$newname
  63. echo "Generated res/flags/"$newname
  64. done