]> git.ipfire.org Git - thirdparty/cups.git/blame - tools/makebuttons
Load cups into easysw/current.
[thirdparty/cups.git] / tools / makebuttons
CommitLineData
7594b224 1#!/bin/sh
2#
3# "$Id$"
4#
5# Button generation script for the Common UNIX Printing System (CUPS).
6#
7# Originally created and donated by Philippe Combes (see STR #2231).
8#
bc44d920 9# Copyright 2007 by Apple Inc.
7594b224 10# Copyright 2007 by Easy Software Products, all rights reserved.
11#
12# These coded instructions, statements, and computer programs are the
bc44d920 13# property of Apple Inc. and are protected by Federal copyright
14# law. Distribution and use rights are outlined in the file "LICENSE.txt"
15# which should have been included with this file. If this file is
16# file is missing or damaged, see the license at "http://www.cups.org/".
7594b224 17#
18
19#
20# This little script uses convert from ImageMagick to generate CUPS buttons.
21# It requires ImageMagick version 6.2.4 or higher.
22#
23# It can generate 20-pixel high one-line buttons and 40-pixel high two-line
24# buttons.
25#
26# Usage:
27#
3d8365b8 28# tools/makebuttons [--font filename] locale buttons.txt
7594b224 29#
30# "Locale" is the locale name, either "ll" or "ll_CC" where "ll" is the
31# 2-letter language abbreviation and "CC" is the 2-letter ISO country
32# code. The new "ll-region" syntax is *not* supported at this time
33# and should not be used!
34#
35# "Buttons.txt" is a file containing the buttons to be created and must
36# be formatted as follows:
37#
38# filename.gif color text for button
39#
40# "filename.gif" is the name of the image file to create.
41#
42# "color" is the color of the button, one of "std" (the normal olive
43# background), "black", "green", or "red".
44#
45# The remainder of the line following the color is used as the text
46# for the button. Use "@UP" and "@DOWN" to include up and down arrows,
47# and "\\n" to break the text into 2 lines (not generally recommended).
48#
49# Lines starting with '#' are treated as comments. See the file
50# "tools/buttons.txt" for inspiration...
51#
52
3d8365b8 53# Bitstream Vera font...
54font="fonts/Vera.ttf"
55
56# Colors
57background="#d4d4a4"
58black="#000000"
59green="#009900"
60red="#cc0000"
61standard="#666633"
62
63
64if test "x$1" == x--font; then
65 shift
66 font="$1"
67 shift
68fi
69
7594b224 70if test $# -lt 2; then
3d8365b8 71 echo Usage: tools/makebuttons "[--font filename]" locale buttons.txt
7594b224 72 exit 1
73elif test ! -d tools; then
74 echo ERROR: You MUST run the makebuttons script from the main CUPS source directory!
75 exit 1
76elif test "x`convert --help | grep extent`" = x; then
77 echo ERROR: This script requires ImageMagick 6.2.4 or higher.
78 exit 1
79else
80 locale=$1
81 list=$2
82fi
83
84if test ! -f $list; then
85 echo "ERROR: $list: file not found."
86 exit 1
87elif test ! -r $list; then
88 echo "ERROR: $list: cannot read file."
89 exit 1
90fi
91
92
7594b224 93# 'generate_button()' - Create a button image.
94#
95# Arg 1: button filename (WITH .gif extension!)
96# Arg 2: button color: black, green, red or std (only the initial letter matters)
97# Arg 3+: the text !
98
99function generate_button()
100{
101 # Collect arguments...
102 filename=$1
103 shift
104
105 color=$1
106 shift
107
108 txt="$*"
109
110 # Show progress...
111 echo Creating $filename...
112
113 # Figure out the foreground and background colors...
114 bgclr=$background
115
116 case x$color in
117 xb*)
118 fgclr=$black
119 fuzz="10%"
120 ;;
121
122 xg*)
123 fgclr=$green
124 fuzz="10%"
125 ;;
126
127 xr*)
128 fgclr=$red
129 fuzz="1%"
130 ;;
131
132 xs*)
133 fgclr=$standard
134 fuzz="10%"
135 ;;
136
137 *)
138 echo "ERROR: Unknown color $color for $filename!"
139 exit 1
140 ;;
141 esac
142
143 # See if we need to add the up or down arrows...
144 add_arrows=0 # 1: up arrows, 2: down arrows
145
146 if echo $txt | grep '@UP' > /dev/null 2>&1; then
147 add_arrows=1
148 txt="`echo $txt | sed 's|@UP||g'`"
149 elif echo $txt | grep '@DOWN' > /dev/null 2>&1; then
150 add_arrows=2
151 txt="`echo $txt | sed 's|@DOWN||g'`"
152 fi
153
154 tmp_btn=/tmp/cups-btn-$$.bmp
155
156 # First step: generate an image trimmed to the text.
157 # -> annotate a 40x400 rectangle with the provided text
158 # -> trim to the text
f42414bf 159 convert xc:transparent -extent 400x40 -fill "$fgclr" \
7594b224 160 -draw "rectangle 0,0 399,39" \
161 -fill "#ffffff" -encoding Unicode -pointsize 13 -font "$font" \
162 -gravity Center -annotate 0x0+0+0 "$txt" -trim $tmp_btn
163
164 # From the 1st step, we get text width and height
165 txt_h=`identify -format "%h" $tmp_btn`
166 txt_w=`identify -format "%w" $tmp_btn`
167
3d8365b8 168 if test $txt_h -gt 40; then
7594b224 169 echo "ERROR: 2 lines maximum for the button text"
170 exit 1
171 fi
172
173 # With the default font, one line is less than 16 pixels high, and a
174 # two lines of text is at least 20 pixels high. So, from the text
175 # height we guess if we need a one- or two-line button.
176 if test $txt_h -ge 18; then
177 btn_h=40
178 else
179 btn_h=20
180 fi
181
182 if test $add_arrows -gt 0; then
183 if test $btn_h -eq 20; then
184 txt_w=`expr $txt_w + 36`
185 elif test $btn_h -eq 40; then
186 txt_w=`expr $txt_w + 58`
187 fi
188 fi
189
190 # Second step: generate the button.
191 #
192 # Procedure:
193 #
194 # - Draw a rectangle with the background color (for correct
195 # button borders)
196 # - Draw a roundRectangle (args are coordinates, not lengths!)
197 # - Make the background transparent (with correct fuzz feature)
198 # - Annotate centered text (in height, it is necessary to have
199 # -1 in y parameter so that the text is not too low)
200 rad=`expr $btn_h / 2`
201 btn_w=`expr $txt_w + $rad + $rad`
202 btn_top=`expr $btn_h - 1`
203
f42414bf 204 convert xc:transparent \
7594b224 205 -extent $btn_w'x'$btn_h -fill "$bgclr" \
206 -draw "rectangle 0,0 $btn_w,$btn_h" -fill "$fgclr" \
207 -draw "roundRectangle 0,0 `expr $btn_w - 1`,$btn_top `expr $rad - 1`,$rad" \
208 -fuzz $fuzz -transparent "$bgclr" -write $filename \
209 -fill "#ffffff" -encoding Unicode -pointsize 13 \
210 -font "$font" -gravity Center -annotate 0x0+0-1 "$txt" \
211 $filename
212
213 if test $add_arrows -gt 0; then
214 if test $add_arrows -eq 1; then
215 # UP arrows
216 if test $btn_h -eq 20; then
217 # 1-line buttons
218 pts1="9,15 21,15 15,4"
219 pts2="`expr $btn_w - 10`,15 `expr $btn_w - 22`,15 `expr $btn_w - 16`,4"
220 else
221 # 2-line buttons
222 pts1="16,30 34,30 25,10"
223 pts2="`expr $btn_w - 17`,30 `expr $btn_w - 35`,30 `expr $btn_w - 26`,10"
224 fi
225 else
226 # DOWN arrows
227 if [ $btn_h -eq 20 ]; then
228 # 1-line buttons
229 pts1="9,4 21,4 15,15"
230 pts2="`expr $btn_w - 10`,4 `expr $btn_w - 22`,4 `expr $btn_w - 16`,15"
231 else
232 # 2-line buttons
233 pts1="16,10 34,10 25,30"
234 pts2="`expr $btn_w - 17`,10 `expr $btn_w - 35`,10 `expr $btn_w - 26`,30"
235 fi
236 fi
237
238 convert $filename -fill "#ffffff" -draw "polygon $pts1" \
239 -draw "polygon $pts2" $filename
240 fi
241
242 rm -f $tmp_btn
243}
244
245# Make sure the locale-specific destination directory exists...
246if test ! -d doc/$locale/images; then
247 echo Creating doc/$locale/images...
248 mkdir -p doc/$locale/images
249fi
250
251# Process each line in the file...
252cat $list | while read line; do
253 if test "x`echo $line | cut -c1`" = "x#"; then
254 continue
255 fi
256
257 generate_button doc/$locale/images/$line
258done
259
260#
261# End of "$Id$".
262#