#! /bin/sh
#
# module :
# add frame and callouts image to an existing one
#
# !! exists only because graphics callouts are not yet available
# with xsl processing on XML/Docbook files (xsl stylesheet 1.70 and
# DocBook DTD 4.2)
#
# thanks to the authors of the following web pages
# http://www.win.tue.nl/~aeb/linux/misc/convert/convert-draw-text.html
# http://www.cit.gu.edu.au/~anthony/graphics/imagick6/draw/
# http://www.cit.gu.edu.au/~anthony/graphics/imagick6/annotating/
#
# update :
# $Id$
# fplod 2006-07-20T16:05:19Z aedon.locean-ipsl.upmc.fr (Darwin)
# add a parameter = the number of figpng/xxx_n.png
# fplod 2006-07-19T12:40:02Z aedon.locean-ipsl.upmc.fr (Darwin)
# first release
#
#
set -u
set -o posix
#
case "${1}" in
   0101|0201|mouse)
      # screen capture to be commented by frames and callouts images
      capture="figpng/xxx_${1}_org.png"
   ;;
   *)
      echo "eee : unknown parameter ${1}"
      exit 1
   ;;
esac
#
if [ ! -f ${capture} ]
then
   echo " eee : ${capture} not found"
   exit 1
fi
#
#
callout_path=images/callouts/
callout_xsize=12 # ++ should be obtain by parsing identify result
callout_ysize=12 # ++ should be obtain by parsing identify result
#
case ${capture} in
   figpng/xxx_0101_org.png)
      capture_xsize=520 #++ should be obtain by parsing identify result
      capture_ysize=309 #++ should be obtain by parsing identify result
      #
      # description of frames (title, xmin, ymin, xmax,ymax)
      # nb : title and label are not yet used but they may be added in a kind of
      # comments
      # later in xml as id or label
      # now they are only here to remind us what we do
      zone_nb=3
      zone_t[1]="Data file"
      zone_l[1]="datafile"
      zone_rect[1]="26,53, 485,95"
      zone_t[2]="Init method"
      zone_l[2]="initmethod"
      zone_rect[2]="78,101, 433,152"
      zone_t[3]="Init program"
      zone_l[3]="initprogram"
      zone_rect[3]="3,159, 518,278"
      #zone_t[1]="Data file"
      #zone_l[1]="datafile"
      #zone_rect[1]="5,55 515,90"
      #zone_t[2]="Init file"
      #zone_l[2]="initfile"
      #zone_rect[2]="5,150 515,200"
   ;;
   figpng/xxx_0201_org.png)
      capture_xsize=429 #++ should be obtain by parsing identify result
      capture_ysize=630 #++ should be obtain by parsing identify result
      #
      # description of frames (title, xmin, ymin, xmax,ymax)
      # nb : title and label are not yet used but they may be added in a kind of
      # comments
      # later in xml as id or label
      # now they are only here to remind us what we do
      zone_nb=10
      #
      zone_t[1]="Plot type"
      zone_l[1]="plottype"
      zone_rect[1]="3,24, 97,51"
      zone_t[2]="Menu"
      zone_l[2]="menu"
      zone_rect[2]="108,24, 325,53"
      zone_t[3]="OK"
      zone_l[3]="ok"
      zone_rect[3]="5,59, 44,86"
      zone_t[4]="Page layout"
      zone_l[4]="pagelayout"
      zone_rect[4]="70,59, 193,86"
      zone_t[5]="Variables list"
      zone_l[5]="variableslist"
      zone_rect[5]="209,60, 323,85"
      zone_t[6]="Files list"
      zone_l[6]="fileslist"
      zone_rect[6]="2,95, 344,117"
      zone_t[7]="Command text"
      zone_l[7]="commandtext"
      zone_rect[7]="2,126, 346,160"
      zone_t[8]="Calendar"
      zone_l[8]="calendar"
      zone_rect[8]="2,169, 212,216"
      zone_t[9]="Domdef"
      zone_l[9]="domdef"
      zone_rect[9]="16,227, 308,447"
      zone_t[10]="Specifications"
      zone_l[10]="specifications"
      zone_rect[10]="1,458, 345,580"
      # the next three lines are here to check calcultation of CALSPAIR units
      # see http://www.docbook.org/tdg/en/html/area.html
      # if you want to use it, please add one to zone_nb
      zone_t[11]="just to check full size rectangle"
      zone_l[11]="check"
      zone_rect[11]="0,0 ${capture_xsize},${capture_ysize}"
   ;;
   figpng/xxx_mouse_org.png)
      capture_xsize=57 #++ should be obtain by parsing identify result
      capture_ysize=82 #++ should be obtain by parsing identify result
      #
      # description of frames (title, xmin, ymin, xmax,ymax)
      # nb : title and label are not yet used but they may be added in a kind of
      # comments
      # later in xml as id or label
      # now they are only here to remind us what we do
      zone_nb=3
      zone_t[1]="Left"
      zone_l[1]="left"
      zone_rect[1]="3,14, 11,21"
      zone_t[2]="Middle"
      zone_l[2]="middle"
      zone_rect[2]="21,14, 28,19"
      zone_t[3]="Right"
      zone_l[3]="right"
      zone_rect[3]="39,14, 46,20"
   ;;
   *)
      echo " eee : ${capture} not implemented"
      exit 3
   ;;
esac
#
# calculation of % of the rectangle of each zone for Calspair
iz=1
while [ ${iz} -le ${zone_nb} ]
do
   xymin=$(echo ${zone_rect[${iz}]} | awk '{print $1}')
   xymax=$(echo ${zone_rect[${iz}]} | awk '{print $2}')
   xmin=$(echo ${xymin} | awk -F "," '{print $1}')
   xmax=$(echo ${xymax} | awk -F "," '{print $1}')
   ymin=$(echo ${xymin} | awk -F "," '{print $2}')
   ymax=$(echo ${xymax} | awk -F "," '{print $2}')
   xmin_pc=$(echo "(${xmin} / ${capture_xsize}) * 10000" | bc -l)
   xmin_pc=$(echo ${xmin_pc} } | awk -F "." '{print $1}')
   ymin_pc=$(echo "(${ymin} / ${capture_ysize}) * 10000" | bc -l)
   ymin_pc=$(echo ${ymin_pc} } | awk -F "." '{print $1}')
   xmax_pc=$(echo "(${xmax} / ${capture_xsize}) * 10000" | bc -l)
   xmax_pc=$(echo ${xmax_pc} } | awk -F "." '{print $1}')
   ymax_pc=$(echo "(${ymax} / ${capture_ysize}) * 10000" | bc -l)
   ymax_pc=$(echo ${ymax_pc} } | awk -F "." '{print $1}')
   #echo "xmin_pc zone ${iz} = ${xmin_pc}"
   #echo "ymin_pc zone ${iz} = ${ymin_pc}"
   #echo "xmax_pc zone ${iz} = ${xmax_pc}"
   #echo "ymax_pc zone ${iz} = ${ymax_pc}"
   #read a
   zone_rect_pc[${iz}]="${xmin_pc},${ymin_pc} ${xmax_pc},${ymax_pc}"
   iz=$(( ${iz} + 1 ))
done
# build the new image ie build the convert command to be run
# first part : use the capture image and prepare black border and white
# rectangles insertion
cmd="convert -size ${capture_xsize}x${capture_ysize} \
   ${capture} \
   -linewidth 2 \
   -stroke black \
   -fill transparent"
#
# then loop on the frames to be drawed
iz=1
while [ ${iz} -le ${zone_nb} ]
do
   leftcorner=$(echo ${zone_rect[${iz}]} | awk '{print $1}')
   cmd="${cmd} -draw \"rectangle ${zone_rect[${iz}]}\" \
   -draw \"image over ${leftcorner} ${callout_xsize},${callout_ysize} 'images/callouts/${iz}.png'\""
   iz=$(( ${iz} + 1 ))
done
#
#
# +++ verrue mal codee...
# dans le cas mouse, je ne veux pas les cadres
# a voir si on integre proprement ou si on vire...
if [ ${1} = mouse ]
then
    cmd="convert -size ${capture_xsize}x${capture_ysize} ${capture}"
    iz=1
    while [ ${iz} -le ${zone_nb} ]
    do
	leftcorner=$(echo ${zone_rect[${iz}]} | awk '{print $1}')
	cmd="${cmd} -draw \"image over ${leftcorner} ${callout_xsize},${callout_ysize} 'images/callouts/${iz}.png'\""
	iz=$(( ${iz} + 1 ))
    done
fi
# +++ verrue mal codee...
#
#
# last component of the convert command : the name of the output image
framecallouts=$(dirname ${capture})/$(basename ${capture} _org.png).png
cmd="${cmd} ${framecallouts}"
#
#echo "cmd = ${cmd}"
#read a
#
# run the convert command
eval ${cmd}
convert_status=${?}
if [ ${convert_status} -ne 0 ]
then
   echo " eee : convert doesn't work properly"  >> ${log}
   echo " eee : return status = ${return_convert}"  >> ${log}
   exit 2
fi
echo " iii : image ${framecallouts} is ready"
echo " iii : you can add the following lines in XML/Docbook"
cat << EOF
<imageobjectco>
<areaspec units="calspair">
EOF
iz=1
while [ ${iz} -le ${zone_nb} ]
do
   echo "<area id=\"${zone_l[${iz}]}\" coords=\"${zone_rect_pc[${iz}]}\"/>"
   iz=$(( ${iz} + 1 ))
done
cat << EOF
</areaspec>
<imageobject>
<imagedata fileref="${framecallouts}"/> <!-- ++ instead of ${capture} -->
</imageobject>
<calloutlist>
EOF
iz=1
while [ ${iz} -le ${zone_nb} ]
do
   cat << EOF
<callout arearefs="${zone_l[${iz}]}">
<para>${zone_t[${iz}]}</para>
</callout>
EOF
   iz=$(( ${iz} + 1 ))
done
cat << EOF
</calloutlist>
</imageobjectco>
EOF
#
exit 0
