render-cursor-image 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #! /bin/bash
  2. #
  3. # bin/render-cursor-image
  4. # Part of ComixCursors, a desktop cursor theme.
  5. #
  6. # Copyright © 2010–2013 Ben Finney <ben+opendesktop@benfinney.id.au>
  7. # Copyright © 2006–2013 Jens Luetkens <j.luetkens@limitland.de>
  8. #
  9. # This work is free software: you can redistribute it and/or modify it
  10. # under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This work is distributed in the hope that it will be useful, but
  15. # WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. # General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this work. If not, see <http://www.gnu.org/licenses/>.
  21. # Generate a PNG cursor image from SVG source.
  22. #
  23. # Required tools:
  24. # ImageMagick: http://www.imagemagick.org/
  25. # librsvg: http://librsvg.sourceforge.net/
  26. function show_usage_message {
  27. cat <<_EOT_
  28. Usage: $0 [options] <in name> <in file> <out file>
  29. Generate a PNG cursor image from SVG source.
  30. Take a simple SVG file, export it to PNG.
  31. Options:
  32. --help Show this help text, then exit.
  33. --orientation <facing> Specify the orientation of this cursor.
  34. --frame <frame num> Specify frame number of animated cursor.
  35. --duration <duration> Duration (in milliseconds) for this frame.
  36. _EOT_
  37. }
  38. # source the theme config file
  39. THEMENAME="${THEMENAME:-Custom}"
  40. configfile="ComixCursorsConfigs/${THEMENAME}.CONFIG"
  41. source "${configfile}"
  42. # don't do transparency post-processing by default
  43. # used for ComixCursors but not for flatbedcursors
  44. CURSORTRANS=0
  45. # some initialisation before argument processing
  46. orientation="RightHanded"
  47. frame=0
  48. # parse argument list
  49. while [ "${1::1}" == "-" ] ; do
  50. case "$1" in
  51. --help)
  52. show_usage_message
  53. exit
  54. ;;
  55. --orientation)
  56. shift
  57. orientation="$1"
  58. ;;
  59. --frame)
  60. shift
  61. frame=$1
  62. ;;
  63. --duration)
  64. shift
  65. duration=$1
  66. ;;
  67. *)
  68. printf "Unexpected option: %q\n" "$1" >&2
  69. show_usage_message
  70. exit 2
  71. ;;
  72. esac
  73. shift
  74. done
  75. if [ $# -lt 3 ] ; then
  76. show_usage_message
  77. exit 2
  78. fi
  79. NAME="$1"
  80. infile="$2"
  81. outfile="$3"
  82. if [ $VERBOSE ] ; then
  83. if [ "$frame" -lt 1 ]; then
  84. echo "processing $NAME..."
  85. else
  86. echo "processing $NAME frame $frame..."
  87. fi
  88. fi
  89. xcursor_config="${builddir}/${NAME}.conf"
  90. # write the hotspot config file
  91. hotspots_file="$(dirname $infile)/HOTSPOTS"
  92. hotspot_name="$NAME"
  93. hotspot=( $(grep "^$hotspot_name" "$hotspots_file") )
  94. function reset_xcursor_config {
  95. # Delete the config file if it exists.
  96. local xcursor_config="$1"
  97. local frame="$2"
  98. if [ "$frame" -lt 2 ] ; then
  99. if [ -e "${xcursor_config}" ] ; then
  100. rm "${xcursor_config}"
  101. fi
  102. fi
  103. }
  104. function svg2png {
  105. # Convert a single SVG image to PNG.
  106. local infile="$1"
  107. local outfile="$2"
  108. local size=$3
  109. # Write hotspots to config file.
  110. hotx=$(echo "${hotspot[1]} * $size / 500" | bc)
  111. hoty=$(echo "${hotspot[2]} * $size / 500" | bc)
  112. if [ "$frame" -gt 0 ] ; then
  113. echo "$size $hotx $hoty $outfile $duration" >> "${xcursor_config}"
  114. else
  115. echo "$size $hotx $hoty $outfile" >> "${xcursor_config}"
  116. fi
  117. # Render png image.
  118. rsvg-convert --format png \
  119. --dpi-x 72 --dpi-y 72 \
  120. --width $size --height $size \
  121. "$infile" > "$outfile"
  122. }
  123. # Render the cursor image/s.
  124. xcursor_config="${builddir}/${NAME}.conf"
  125. if [ ${MULTISIZE} ]; then
  126. # Render multiple sizes combined cursors per default.
  127. # Reset the config file (hotspots).
  128. reset_xcursor_config "$xcursor_config" "$frame"
  129. # Render multiple cursor sizes.
  130. for SIZENAMES in ${SIZES[@]} ; do
  131. SIZENAME=${SIZENAMES%%=*}
  132. SIZE=${SIZENAMES##*=}
  133. # Render the cursor image.
  134. svg2png "$infile" "${outfile%.*}.${SIZE}.png" "${SIZE}"
  135. done
  136. else
  137. # Render single sized cursors.#
  138. export CURSORSIZE="${CURSORSIZE:-32}"
  139. # Reset the config file (hotspots).
  140. reset_xcursor_config "$xcursor_config" "$frame" "${CURSORSIZE}"
  141. # Render the cursor image.
  142. svg2png "$infile" "${outfile%.*}.${SIZE}.png" "${CURSORSIZE}"
  143. fi