# $NetBSD: Makefile.tnflogo,v 1.3 2013/01/02 00:56:35 mspo Exp $
#
# Please be sure you have installed the following packages:
#
#	print/ghostscript or similar with "pngalpha" device support
#	graphics/ImageMagick (for PNGs optimized for Web, see web* targets)
#	graphics/optipng (for PNGs optimized for Web, see web* targets)
#
# The following variables affects on resulting PNG images:
#
#	RESOLUTIONS	- a width of the output images (in pixels)

NBLOGOSRC?=	NetBSD-flag.eps
RESOLUTIONS?=	90 200 300 1200
INSTALL_FILES= \
	black-tb-1200.png 	NetBSD-bw.png \
	color-wb-90.gif 	NetBSD-flag.gif \
	color-wb-90.jpg 	NetBSD-flag.jpg \
	color-wb-90.png 	NetBSD-flag.png \
	color-bb-1200.png 	NetBSD-inv.png \
	black-wb-200.jpg 	NetBSD-smaller-bw.png \
	color-bb-200.jpg 	NetBSD-smaller-inv.png \
	color-wb-200.gif 	NetBSD-smaller.gif \
	color-wb-200.jpg 	NetBSD-smaller.jpg \
	color-wb-200.png 	NetBSD-smaller.png \
	color-wb-1200.gif 	NetBSD.gif \
	color-wb-1200.jpg 	NetBSD.jpg \
	color-wb-1200.png 	NetBSD.png \
	black-tb-1200.png 	NetBSD-bw-tb.png \
	color-tb-1200.png 	NetBSD-tb.png \
	white-tb-1200.png 	NetBSD-ww-tb.png \
	color-tb-200.png 	NetBSD-smaller-tb.png \
	black-tb-200.png 	NetBSD-smaller-bw-tb.png \
	white-bb-200.png 	NetBSD-smaller-ww-bb.png


######## Setup build framework

GS_FLAGS=	-q -dSAFER -dBATCH -dNOPAUSE -dEPSFitPage \
		-sDEVICE=pngalpha -dGraphicsAlphaBits=4
IM_FLAGS.redc=	-colors 32
IM_FLAGS.jpgq=	-quality 85
OPTIPNG_FLAGS=	-q -o4 -nc
SED_FLAGS=	-E

SED_DESATURATE=	-e "s/[[:blank:]]setgray/ pop $$subclr setgray/g" \
		-e "s/[[:blank:]]setrgbcolor/ pop pop pop $$subclr setgray/g"
SRE_BGSET.tb=	% TRANSPARENT BG %
SRE_BGSET.wb=	1 setgray clippath fill
SRE_BGSET.bb=	0 setgray clippath fill

BORDERWIDTH=	10
_SED_SETBB!=	awk -v bw=${BORDERWIDTH} ' \
		    /^%%BoundingBox:/ { \
			printf " -e \"s/^%s.*/%s %i %i %i %i/\"", \
			    $$1, $$1, $$2 - bw, $$3 - bw, $$4 + bw, $$5 + bw} \
		    /^%%HiResBoundingBox:/ { \
			printf " -e \"s/^%s.*/%s %.6f %.6f %.6f %.6f/\"", \
			    $$1, $$1, $$2 - bw, $$3 - bw, $$4 + bw, $$5 + bw} \
		    ' ${NBLOGOSRC}
SED_SETBB=	${_SED_SETBB:tW:S/"/'/g}

AWK_GSPS=	'/^%%BoundingBox:/ { \
			w = $$4 - $$2; h = $$5 - $$3; \
			scale = w / xres; \
			print int(0.5 + w / scale), int(0.5 + h / scale)}'
AWK_BBOX=	'/^%%BoundingBox:/ { \
			w = $$4 - $$2; h = $$5 - $$3; \
			print "<< /PageSize [", w, h, "] >> setpagedevice"}'

.for res in ${RESOLUTIONS}
GSPS.${res}!=	awk -v xres=${res} ${AWK_GSPS} ${NBLOGOSRC}
WEBGSPS.${res}!=sed ${SED_FLAGS} ${SED_SETBB} ${NBLOGOSRC} | \
			awk -v xres=${res} ${AWK_GSPS}

PNGS+=		color-tb-${res}.png color-wb-${res}.png color-bb-${res}.png \
		black-tb-${res}.png black-wb-${res}.png \
		white-tb-${res}.png white-bb-${res}.png
.endfor
EPSES=		color.eps black.eps white.eps
PDFS=		${NBLOGOSRC:S/.eps/.pdf/g}
WEBPNGS=	${PNGS:S/^/web-/g}
JPGS=		${PNGS:M*-[wb]b-*:S/.png/.jpg/g} \
		${WEBPNGS:M*-[wb]b-*:S/.png/.jpg/g}
GIFS=		${PNGS:M*-[wb]b-*:S/.png/.gif/g} \
		${WEBPNGS:M*-[wb]b-*:S/.png/.gif/g}

######## Meta targets

all:		eps pdf png jpg gif
 
world:		eps pdf png webpng jpg gif

eps:		${EPSES}

pdf:		${PDFS}

png:		${PNGS}

webpng:		${WEBPNGS}

jpg:		${JPGS}

gif:		${GIFS}

.for col in color black white
${col}:		${EPSES:M*${col}*} ${PNGS:M*${col}*}
.endfor

######## Real targets

.SUFFIXES:	.eps .pdf .png .gif .jpg

.png.gif:
		convert ${.ALLSRC} ${.TARGET}

.png.jpg:
		convert ${IM_FLAGS.jpgq} ${.ALLSRC} ${.TARGET}

color.eps:	${NBLOGOSRC}
		cat ${.ALLSRC} > ${.TARGET}

black.eps:	${NBLOGOSRC}
		subclr=0 ;   sed ${SED_FLAGS} ${SED_DESATURATE} \
			${.ALLSRC} > ${.TARGET}

white.eps:	${NBLOGOSRC}
		subclr=255 ; sed ${SED_FLAGS} ${SED_DESATURATE} \
			${.ALLSRC} > ${.TARGET}

.eps.pdf:
		gs -q -dBATCH -dNOPAUSE -dSAFER -sDEVICE=pdfwrite \
			-sOutputFile=${.TARGET} -c `awk ${AWK_BBOX} ${.ALLSRC}`\
			-f ${.ALLSRC}

.for col in color black white		# primary colors
.  for bgcol in tb bb wb		# background color
.    for res in ${RESOLUTIONS}

PNG_CMD.${col}.${bgcol}.${res}=	\
		sed ${SED_FLAGS} @SED_SETBB@ \
			-e 's/%%BACKGROUND_SETTINGS%%/${SRE_BGSET.${bgcol}}/' \
			${.ALLSRC} | \
		gs ${GS_FLAGS} -sOutputFile=${.TARGET} \
			-c "<< /PageSize [@GSPS@] >> setpagedevice" -f -

${col}-${bgcol}-${res}.png:	${col}.eps
		${PNG_CMD.${col}.${bgcol}.${res}:tW:S/@SED_SETBB@//g:S/@GSPS@/${GSPS.${res}}/g}

web-${col}-${bgcol}-${res}.png:	${col}.eps
		${PNG_CMD.${col}.${bgcol}.${res}:tW:S/@SED_SETBB@/${SED_SETBB}/g:S/@GSPS@/${WEBGSPS.${res}}/g}
.      if "${bgcol}" != "tb"	# preserve RGBA transparency
		mogrify ${IM_FLAGS.redc} ${.TARGET}
.      endif
		optipng ${OPTIPNG_FLAGS} ${.TARGET}

.    endfor # by resolutions
.  endfor # by background colors
.endfor # by colors

clean:
		rm -f ${EPSES}
		rm -f ${PNGS}
		rm -f ${WEBPNGS}
		rm -f ${JPGS}
		rm -f ${GIFS}

cleandir:	clean

install:
.for s d in ${INSTALL_FILES}
	mv $d $d.orig
	cp $s $d
.endfor

uninstall:
.for s d in ${INSTALL_FILES}
	rm $s $d
	mv ${d}.orig $d
.endfor