[Gllug] Making menu images from text: Solution

tet at accucard.com tet at accucard.com
Mon Feb 4 16:22:52 UTC 2002


>Changes required:-
>
>1. Use getopt to read input parameters
>2. Check for errors
>3. Check that text is not bigger than menu picture
>
>Any one feeling bored enough to fix these changes?

Yep, here you go. The fonts still suck, but that could be cured by using
pbmtext rather than ppmlabel, assuming you can find a bitmapped font you
like.

If the text you use is very long, then the program runs very slowly,
due to the fact that it's exceedingly generous when allowing for maximum
button widths, and manipulating large images is slow...

Pipe the output into cjpeg or pnmquant/pnmtopng etc to get a suitable
final image.

Tet

-------------- next part --------------
#!/bin/sh
# This script is called makegraphic

################################################################
################################################################

### Print a usage message then exit

pusage()
{
	(
	echo "usage: $prog [-b colour] [-f colour] [-w width] [-h height]"
	echo "         [-p pad] [-s size] text"
	echo "  -b -- use \"colour\" for the background of the button"
	echo "  -f -- use \"colour\" for the text on the button"
	echo "  -w -- make the button \"width\" pixels wide"
	echo "  -h -- make the button \"height\" pixels high"
	echo "  -p -- allow \"pad\" pixels padding around the button text"
	echo "  -s -- button text should be \"size\" pixels high where possible"
	) 1>&2

	exit 1
}

################################################################
################################################################

prog=`basename "$0"`
bgcolour='#8899cc'
fgcolour='#111133'
width=100
height=22
padding=3

textfile="/var/tmp/$prog.text.$$"
bgfile="/var/tmp/$prog.bg.$$"

trap "/bin/rm -f $textfile $bgfile" 0 HUP INT QUIT TERM

while getopts b:f:w:h:p:s: c
do
	case "$c" in
		b)	bgcolour="$OPTARG" ;;
		f)	fgcolour="$OPTARG" ;;
		w)	width="$OPTARG" ;;
		h)	height="$OPTARG" ;;
		p)	padding="$OPTARG" ;;
		s)	size="$OPTARG" ;;
		*)	pusage ;;
	esac
done
shift `expr $OPTIND - 1`

################################################################
################################################################

### If the user hasn't specified a text size, calculate a default
### as well as values for the largest possible text that can fit in
### the button size we've asked for.

text="$*"
stringlen=`echo "$text" | wc -c`

[ -z "$size" ] && size=`expr $height - $padding - $padding`

maxwidth=`expr $width - $padding - $padding`
maxheight=`expr $height - $padding - $padding`

if [ "$maxheight" -gt "$size" ]
then
	maxheight="$size"
fi

################################################################
################################################################

### Render text at 3 times requested size, and then scale down to get
### optimum quality. Since latin characters are generally taller than
### they are wide, assuming each character is twice as wide as it
### is high gives us enough leeway to ensure we create a big enough
### background image.

renderheight=`expr "$size" \* 3`
renderwidth=`expr $stringlen \* "$size" \* 2`

ppmmake "$bgcolour" "$renderwidth" "$renderheight" | \
	ppmlabel -colour "$fgcolour" -text "$*" -size "$renderheight" | \
	pnmcrop | \
	pnmscale -xysize "$maxwidth" "$maxheight" \
	> "$textfile"

################################################################
################################################################

### Now we've got a text image suitable to fit in the given button size,
### calculate its actual size, so we can offset it correctly withing
### the button

xsize=`pnmfile "$textfile" | cut -d' ' -f3`
ysize=`pnmfile "$textfile" | cut -d' ' -f5`

xoffset=`echo $width $xsize - 2 / p | dc 2>/dev/null`
yoffset=`echo $height $ysize - 2 / p | dc 2>/dev/null`

################################################################
################################################################

### Lastly, create the background image, and slap the text in the
### middle to create the final button.

ppmmake "$bgcolour" "$width" "$height" > "$bgfile"
pnmpaste -replace "$textfile" "$xoffset" "$yoffset" "$bgfile"


More information about the GLLUG mailing list