| 1 |
#! /bin/bash |
| 2 |
# |
| 3 |
# bin/render-cursor-image |
| 4 |
# Part of ComixCursors, a desktop cursor theme. |
| 5 |
# |
| 6 |
# Copyright © 2010–2013 Ben Finney <[email protected]> |
| 7 |
# Copyright © 2006–2013 Jens Luetkens <[email protected]> |
| 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 |
|
| 22 |
# Generate a PNG cursor image from SVG source. |
| 23 |
# |
| 24 |
# Required tools: |
| 25 |
# ImageMagick: http://www.imagemagick.org/ |
| 26 |
# librsvg: http://librsvg.sourceforge.net/ |
| 27 |
|
| 28 |
function show_usage_message { |
| 29 |
cat <<_EOT_ |
| 30 |
Usage: $0 [options] <in name> <in file> <out file> |
| 31 |
|
| 32 |
Generate a PNG cursor image from SVG source. |
| 33 |
|
| 34 |
Take a simple SVG file, export it to PNG. |
| 35 |
|
| 36 |
Options: |
| 37 |
--help Show this help text, then exit. |
| 38 |
--orientation <facing> Specify the orientation of this cursor. |
| 39 |
--frame <frame num> Specify frame number of animated cursor. |
| 40 |
--duration <duration> Duration (in milliseconds) for this frame. |
| 41 |
|
| 42 |
_EOT_ |
| 43 |
} |
| 44 |
|
| 45 |
# source the theme config file |
| 46 |
THEMENAME="${THEMENAME:-Custom}" |
| 47 |
configfile="ComixCursorsConfigs/${THEMENAME}.CONFIG" |
| 48 |
source "${configfile}" |
| 49 |
|
| 50 |
# don't do transparency post-processing by default |
| 51 |
# used for ComixCursors but not for flatbedcursors |
| 52 |
CURSORTRANS=0 |
| 53 |
|
| 54 |
# some initialisation before argument processing |
| 55 |
orientation="RightHanded" |
| 56 |
frame=0 |
| 57 |
|
| 58 |
# parse argument list |
| 59 |
while [ "${1::1}" == "-" ] ; do |
| 60 |
case "$1" in |
| 61 |
--help) |
| 62 |
show_usage_message |
| 63 |
exit |
| 64 |
;; |
| 65 |
--orientation) |
| 66 |
shift |
| 67 |
orientation="$1" |
| 68 |
;; |
| 69 |
--frame) |
| 70 |
shift |
| 71 |
frame=$1 |
| 72 |
;; |
| 73 |
--duration) |
| 74 |
shift |
| 75 |
duration=$1 |
| 76 |
;; |
| 77 |
*) |
| 78 |
printf "Unexpected option: %q\n" "$1" >&2 |
| 79 |
show_usage_message |
| 80 |
exit 2 |
| 81 |
;; |
| 82 |
esac |
| 83 |
shift |
| 84 |
done |
| 85 |
|
| 86 |
if [ $# -lt 3 ] ; then |
| 87 |
show_usage_message |
| 88 |
exit 2 |
| 89 |
fi |
| 90 |
|
| 91 |
NAME="$1" |
| 92 |
infile="$2" |
| 93 |
outfile="$3" |
| 94 |
|
| 95 |
if [ $VERBOSE ] ; then |
| 96 |
if [ "$frame" -lt 1 ]; then |
| 97 |
echo "processing $NAME..." |
| 98 |
else |
| 99 |
echo "processing $NAME frame $frame..." |
| 100 |
fi |
| 101 |
fi |
| 102 |
|
| 103 |
xcursor_config="${builddir}/${NAME}.conf" |
| 104 |
|
| 105 |
# write the hotspot config file |
| 106 |
hotspots_file="$(dirname $infile)/HOTSPOTS" |
| 107 |
hotspot_name="$NAME" |
| 108 |
hotspot=( $(grep "^$hotspot_name" "$hotspots_file") ) |
| 109 |
|
| 110 |
function reset_xcursor_config { |
| 111 |
# Delete the config file if it exists. |
| 112 |
local xcursor_config="$1" |
| 113 |
local frame="$2" |
| 114 |
if [ "$frame" -lt 2 ] ; then |
| 115 |
if [ -e "${xcursor_config}" ] ; then |
| 116 |
rm "${xcursor_config}" |
| 117 |
fi |
| 118 |
fi |
| 119 |
} |
| 120 |
|
| 121 |
function svg2png { |
| 122 |
# Convert a single SVG image to PNG. |
| 123 |
local infile="$1" |
| 124 |
local outfile="$2" |
| 125 |
local size=$3 |
| 126 |
|
| 127 |
# Write hotspots to config file. |
| 128 |
hotx=$(echo "${hotspot[1]} * $size / 500" | bc) |
| 129 |
hoty=$(echo "${hotspot[2]} * $size / 500" | bc) |
| 130 |
|
| 131 |
if [ "$frame" -gt 0 ] ; then |
| 132 |
echo "$size $hotx $hoty $outfile $duration" >> "${xcursor_config}" |
| 133 |
else |
| 134 |
echo "$size $hotx $hoty $outfile" >> "${xcursor_config}" |
| 135 |
fi |
| 136 |
|
| 137 |
# Render png image. |
| 138 |
rsvg-convert --format png \ |
| 139 |
--dpi-x 72 --dpi-y 72 \ |
| 140 |
--width $size --height $size \ |
| 141 |
"$infile" > "$outfile" |
| 142 |
} |
| 143 |
|
| 144 |
# Render the cursor image/s. |
| 145 |
|
| 146 |
xcursor_config="${builddir}/${NAME}.conf" |
| 147 |
|
| 148 |
if [ ${MULTISIZE} ]; then |
| 149 |
# Render multiple sizes combined cursors per default. |
| 150 |
# Reset the config file (hotspots). |
| 151 |
reset_xcursor_config "$xcursor_config" "$frame" |
| 152 |
# Render multiple cursor sizes. |
| 153 |
for SIZENAMES in ${SIZES[@]} ; do |
| 154 |
SIZENAME=${SIZENAMES%%=*} |
| 155 |
SIZE=${SIZENAMES##*=} |
| 156 |
# Render the cursor image. |
| 157 |
svg2png "$infile" "${outfile%.*}.${SIZE}.png" "${SIZE}" |
| 158 |
done |
| 159 |
else |
| 160 |
# Render single sized cursors.# |
| 161 |
export CURSORSIZE="${CURSORSIZE:-32}" |
| 162 |
# Reset the config file (hotspots). |
| 163 |
reset_xcursor_config "$xcursor_config" "$frame" "${CURSORSIZE}" |
| 164 |
# Render the cursor image. |
| 165 |
svg2png "$infile" "${outfile%.*}.${SIZE}.png" "${CURSORSIZE}" |
| 166 |
fi |
| 167 |
|