#!/bin/bash
#
# This script creates the .tbl file for a given T1 Chinese font
#
# This script requires the PfaEdit package to extract char bbox
#

CLIB256E=/usr/share/texmf/fonts/cct/hzfonts/clib256e.pps

if [ $# -ne 1 ]; then
    echo 1>&2 "Usage:  $0 fontname [>fontname.tbl]"
    echo 1>&2 "(fontname is the name of a gbk font, \"clib256e\", or \"default\")."
    echo 1>&2 "Examples:"
    echo 1>&2 "	$0 gbksong >gbksong.tbl"
    echo 1>&2 "	$0 clib256e >clib256e.tbl"
    echo 1>&2 "	$0 default >gbkdef.tbl"
    exit 1
fi

if ! fontforge --version >/dev/null 2>&1; then
  if ! pfaedit --version >/dev/null 2>&1; then
    echo 1>&2 "Cannot execute 'fontforge' or 'pfaedit'."
    echo 1>&2 "Are you sure you have installed FontForge (or PfaEdit)?"
    exit 1
  else
    font_prog=pfaedit
  fi
else
  font_prog=fontforge
fi

gbkfont=$1
pfascript=/tmp/GBKpunct.$$
trap "rm -f $pfascript" 0 1 2 3 15

Div1000() {
    local suffix minus
    number=$1
    suffix=$2
    minus=""
    if [ $number -lt 0 ]; then
	minus="-"
	suffix="."			# remove '0' in '0.'
	number=$((-number))
    fi
    number=$(((number+5)/10))	# round to 10's
    if [ $number -ge 100 ]; then
	suffix="$((number/100))."
	number=$((number%100))
    fi
    if [ $number -lt 10 ]; then
	echo $minus${suffix}0$number
    else
	echo $minus${suffix}$number
    fi
}

Process() {
    cat <<END
%& -no-cctspace
%
% Table of punctuation chars (generated by \`GBKpunct')
%
% Note: 
%    1) 'rule' is the width of the \\vrule prepended/appended to the char.
%    2) 'flag,sp,st,sh' means 'flag,space,stretch,shrink', the glue is
%	defined by:  'space'em plus 'stretch'em minus 'shrink'em
%	(it is ignored when flag is not 1).
%    3) 'nbflag' means 'nobreak flag'.
%    4) For recovering the previous behaviour of CCT.sty (0.5.0 or earlier), 
%	set rule to '0', pre glue to '0,0,0,0', post glue to '0,-0.2,0,0.2'.
%
%			      pre			    post
%		------------------------------	------------------------------
%				     glue			    glue
%	  char	nbflag	rule 	(flag,sp,st,sh)	nbflag	rule 	(flag,sp,st,sh)
%	  ----	------------------------------	------------------------------
END
    lineno=0
    i=0
    while [ 1 ]; do
	read wchar flag lmargin rmargin space min max || break
	lineno=$((lineno+1))
	[ "${wchar#\#}" != "$wchar" ] && continue	# '#' comment-line
	if [ -z "$wchar" -o "${wchar#\%}" != "$wchar" ]; then
	    [ $i -gt 0 ] && echo "%"
	    continue				# '%' comment or empty line
	fi
	if [ -z "$max" ]; then
	    echo 1>&2 "Invalid input on line $lineno ignored."
	    continue
	fi
	# Compute ASCII values of the two bytes of the character
	set -- `echo $wchar  | od -tu1 | awk '{print $2, $3}'`
	c1=$1
	c2=$2
	###	echo 0x`echo "obase=16; $((c1*256+c2))" | bc`
	# Compute fontno, charno
    if [ "$gbkfont" = "clib256e" ]; then	#========== CCT font
	n=$(((c1-161)*94 + c2-161))
	set -- `fonttest $CLIB256E $n $n 2>/dev/null | sed -e 's/[()]//g'`
	shift 3
	width=$7
	height=$8
	lb=$1
	rb=$((width-$3))
    elif [ "$gbkfont" = "default" ]; then	#========== default
	width=1000
	height=1000
	lb=$lmargin
	rb=$rmargin
    else 					#========== CJK font
	n=$(((c1-129)*190))
	if [ $c2 -ge 127 ]; then
	    n=$((n+c2-65))
	else
	    n=$((n+c2-64))
	fi
	fontno=$((n/256+1))
	charno=$((n%256))
	echo >$pfascript "
	    #--- CTeX
	    #Open(\"/dosc/ctex/localtexmf/fonts/type1/chinese/$gbkfont/$gbkfont$fontno.pfb\")
	    #--- Linux
	    Open(\"`kpsewhich $gbkfont$fontno.pfb`\")
	    #---
	    Select($charno, $charno)
	    wd = CharInfo(\"Width\")
	    lb = CharInfo(\"LBearing\")
	    rb = CharInfo(\"RBearing\")
	    Print(wd, ' ', lb, ' ', rb, ' ')"
	set -- `$font_prog -script $pfascript 2>/dev/null`
	width=$(($1+0))
	lb=$(($2+0))
	rb=$(($3+0))
    fi						#=====================
	# scale to 1000x1000
	lb=$(((lb*1000+width/2)/width))
	rb=$(((rb*1000+width/2)/width))
	width=1000

	w=$((width-lb-rb+lmargin+rmargin))	# effective width of the char
	if [ $flag -eq 1 ]; then
	    # '', '', '', etc.
	    /bin/echo -n "\\GBKpunct  $wchar	"
	    /bin/echo -n "1	"				# pre-nobreak
	    /bin/echo -n "`Div1000 $((lmargin-lb)) 0.`	"	# pre-rule
	    /bin/echo -n "0,0,0,0 	"			# pre-glue
	    /bin/echo -n "0	"				# post-nobreak
	    /bin/echo -n "`Div1000 $((rmargin-rb)) 0.`	"	# post-rule
	    # post-glue
	    /bin/echo -n "1,"					# - glue flag
	    /bin/echo -n "`Div1000 $((space-w)) .`,"		# - space
	    /bin/echo -n "`Div1000 $((max-space)) .`,"		# - stretch
	    /bin/echo -n "`Div1000 $((space-min)) .`"		# - shrink
	    /bin/echo ""
	else
	    # '', '', etc.
	    /bin/echo -n "\\GBKpunct  $wchar	"
	    /bin/echo -n "0	"				# pre-nobreak
	    /bin/echo -n "`Div1000 $((lmargin-lb)) 0.`	"	# pre-rule
	    # pre-glue
	    /bin/echo -n "1,"					# - glue flag
	    /bin/echo -n "`Div1000 $((space-w)) .`,"		# - space
	    /bin/echo -n "`Div1000 $((max-space)) .`,"		# - stretch
	    /bin/echo -n "`Div1000 $((space-min)) .`	"	# - shrink
	    /bin/echo -n "1	"				# post-nobreak
	    /bin/echo -n "`Div1000 $((rmargin-rb)) 0.`	"	# post-rule
	    /bin/echo -n "0,0,0,0"				# post-glue
	    /bin/echo ""
	fi
	i=$((i + 1))
	/bin/echo 1>&2 -n $wchar
    done
    echo 1>&2 " done."
}

Process <<END
% Table of punctuation chars (unit=1/1000 em)
%
% 'flag'
%	'0': nobreak at right, glue at left
%	'1': nobreak at left,  glue at right
%
% 'lmargin', 'rmargin'
%	unshrinkable margins at left and right side of the char
%	(used to compute width of the vrules)
%
% 'space'/'min'/'max'
%	normal/minimum/maximum width of the char (used to compute the glue)
%
%	flag	lmargin	rmargin	space	min	max
%	----	-------	-------	-----	---	---
	1	125	125	800	600	1000	
	1	125	125	800	600	1000	
	1	125	125	800	600	1000	
	1	125	125	800	600	1000	
	1	125	125	800	600	1000	
	1	125	125	800	600	1000	

	1	125	125	800	600	1000	
	1	125	125	800	600	1000	
	1	125	125	1000	900	1000	

	1	125	125	800	600	1000	
	1	125	125	800	600	1000	
	1	125	125	800	600	1000	
	1	125	125	800	600	1000	
	1	125	125	800	600	1000	
	1	125	125	800	600	1000	
	1	125	125	800	600	1000	
	1	125	125	800	600	1000	
	1	125	125	800	600	1000	
	1	125	125	800	600	1000	

	0	125	125	800	600	1000	
	0	125	125	800	600	1000	
	0	125	125	800	600	1000	
	0	125	125	800	600	1000	
	0	125	125	800	600	1000	
	0	125	125	800	600	1000	
	0	125	125	800	600	1000	
	0	125	125	800	600	1000	
	0	125	125	800	600	1000	
	0	125	125	800	600	1000	
END
