#!/bin/sh
# -------------------------------------
# 2014.07.27
# utf82ps ver 0.1
# -------------------------------------
# -------------------------------------
# パラメータ変数
# -------------------------------------
c_Usage="Usage:	utf82ps [-p A4|A3|B5|B4|15i] [-L] [-m margin-left,right,top,bottom] [-f fontsize ] [ -l line-space% ] [ -w font-width% ] [ -g ] file"
c_PaperSize="A4"
c_PaperMargin=""
c_Landscape="no" 
d_FontSize="12" # point
d_LineSpace="10" # % d_FontSize / 100 * d_LineSpace
d_FontWidth="100" # % d_FontSize / 100 * d_FontWidth

c_MincyoFont="/Ryumin-Light-UniJIS-UTF8-H"
c_GothicFont="/GothicBBB-Medium-UniJIS-UTF8-H"
c_DefaultFont=$c_MincyoFont
#c_DefaultFont=$c_GothicFont
# -------------------------------------
# 用紙のルックアップテーブル
# -------------------------------------
# 15iはおそらく使わないと思いますがサンプルとして。15iは用紙方向を変えたくないので二つともPortになってます。
# paper	margi				paper size	orientation
# 	left	right	top	bottom	x	y	default	swich
c_PaperTable="
A4	25.4	12.7	25.4	12.7	210	297	Port	Land
A3	25.4	12.7	25.4	12.7	297	420	Port	Land
B5	25.4	12.7	25.4	12.7	182	257	Port	Land
B4	25.4	12.7	25.4	12.7	257	364	Port	Land
15i	0	0	0	0	381	279.4	Port	Port
"
# -------------------------------------
# パラメータ処理
# -------------------------------------
while getopts "p:Lm:f:l:w:gh" OPT
do
	case ${OPT} in
		p) c_PaperSize=${OPTARG} ;;
		L) c_Landscape="yes" ;;
		m) c_PaperMargin=${OPTARG} ;;
		f) d_FontSize=${OPTARG} ;;
		l) d_LineSpace=${OPTARG} ;;
		w) d_FontWidth=${OPTARG} ;;
		g) c_DefaultFont=$c_GothicFont ;;
		h|\?) echo "$c_Usage"; exit 1;;
	esac
done
shift `expr ${OPTIND} - 1`
# -------------------------------------
# 入力の統一
# -------------------------------------
if [ "${1}" != "" ]; then
	exec <"${1}"
fi
# -------------------------------------
# マージン取得
# -------------------------------------
if [ "${c_PaperMargin}" = "" ]; then
	c_Table=`echo "${c_PaperTable}" | grep "${c_PaperSize}"`
else
	c_tab=`printf "\t"`
	c_Temp=`echo "${c_PaperMargin}" | sed -e s/,/"${c_tab}"/g`
	c_Table="${c_PaperSize}$c_tab${c_Temp}"
fi

for i in `seq 2 5`
do
	d_Temp=`echo "${c_Table}" | cut -f${i}`

	case ${i} in
		2) d_MarginLeft=${d_Temp} ;;
		3) d_MarginRight=${d_Temp} ;;
		4) d_MarginTop=${d_Temp} ;;
		5) d_MarginBottom=${d_Temp} ;;
	esac
done
# -------------------------------------
# 用紙サイズ取得
# -------------------------------------
c_Table=`echo "${c_PaperTable}" | grep "${c_PaperSize}"`

for i in `seq 6 7`
do
	d_Temp=`echo "${c_Table}" | cut -f${i}`

	case ${i} in
		6) d_PaperSizeX=${d_Temp} ;;
		7) d_PaperSizeY=${d_Temp} ;;
	esac
done
# -------------------------------------
# 用紙方向取得
# -------------------------------------
c_Table=`echo "${c_PaperTable}" | grep "${c_PaperSize}"`

for i in `seq 8 9`
do
	d_Temp=`echo "${c_Table}" | cut -f${i}`

	case ${i} in
		8) c_PaperOrientation=${d_Temp} ;;
		9) if [ "${c_Landscape}" = "yes" ]; then
		       	c_PaperOrientation=${d_Temp}
		fi
		;;
	esac
done

if [ "${c_PaperOrientation}" = "Land" ]; then
	d_Temp=${d_PaperSizeX}
	d_PaperSizeX=${d_PaperSizeY}
	d_PaperSizeY=${d_Temp}
fi
# -------------------------------------
# Postscript出力
# -------------------------------------
echo "%!PS-Adobe-3.0"
echo "/mm {2.834645669 mul} def"
echo "/d_MarginLeft ${d_MarginLeft} mm def"
echo "/d_MarginRight ${d_PaperSizeX} mm ${d_MarginRight} mm sub def"
echo "/d_MarginTop ${d_PaperSizeY} mm ${d_MarginTop} mm sub def"
echo "/d_MarginBottom ${d_MarginBottom} mm def"
echo "/d_FontSize ${d_FontSize} def"
echo "/d_LineSpace d_FontSize 100 div ${d_LineSpace} mul def"
echo "/d_FontWidth d_FontSize 100 div ${d_FontWidth} mul def"
echo "/c_DefaltFont ${c_DefaultFont} findfont [ d_FontWidth 0 0 d_FontSize 0 0 ] makefont def"
echo "%"
echo "/d_1ByteCh { 16#7f le } def"
echo "/d_2ByteCh { dup 16#cf ge exch 16#df le and } def"
echo "/d_3ByteCh { 16#ef le } def"
echo "/d_4ByteCh { dup 16#f0 ge exch 16#f7 le and } def"
echo "/d_5ByteCh { dup 16#f8 ge exch 16#fb le and } def"
echo "/d_6ByteCh { dup 16#fc ge exch 16#fd le and } def"
echo "%"
echo "/c_BeforSt { 0 exch getinterval } def"
echo "/d_AfterStDic 1 dict def"
echo "/c_AfterSt {
	d_AfterStDic begin
		/d_Start exch def
		/c_PrintSt exch def
		/d_Length c_PrintSt length d_Start sub def
		c_PrintSt d_Start d_Length getinterval
	end
} def"
echo "/d_StringCutDic 1 dict def"
echo "/c_StringCut {
	d_StringCutDic begin
		/c_PrintSt exch def
		1 {
			c_PrintSt 0 get d_1ByteCh { 1 /d_Byte exch def exit } if
			c_PrintSt 0 get d_2ByteCh { 2 /d_Byte exch def exit } if
			c_PrintSt 0 get d_3ByteCh { 3 /d_Byte exch def exit } if
			c_PrintSt 0 get d_4ByteCh { 4 /d_Byte exch def exit } if
			c_PrintSt 0 get d_5ByteCh { 5 /d_Byte exch def exit } if
			c_PrintSt 0 get d_6ByteCh { 6 /d_Byte exch def exit } if
		} repeat
		c_PrintSt d_Byte c_AfterSt c_PrintSt d_Byte c_BeforSt
	end

} def"
echo "%"
echo -n "/f_NewPage { "
if [ "${c_PaperOrientation}" = "Land" ]; then
	echo -n "90 rotate 0 ${d_PaperSizeY} mm -1 mul translate "
fi
echo "d_MarginLeft d_MarginTop d_FontSize sub moveto } def"
echo "/f_NewLine { d_MarginLeft currentpoint exch pop d_FontSize sub d_LineSpace sub moveto } def"
echo "/f_NextString { dup length 0 gt { f_ShowStrings } { pop } ifelse } def"
echo "/f_ShowStrings {
	c_StringCut
	dup stringwidth pop currentpoint pop add
	d_MarginRight gt { f_NewLine } if
	currentpoint exch pop d_FontSize sub
	d_MarginBottom lt { showpage f_NewPage } if
	show f_NextString
} def"
echo "c_DefaltFont setfont"
echo "f_NewPage"
# -------------------------------------
# データ読み込み
# -------------------------------------
c_x0a=`printf "\x0a"`		# 改行
c_x0c=`printf "\x0c"`		# 改ページ 
c_x09=`printf "\x09"`		# タブ
c_Tab=`printf "        "`	# 置換後タブ 空白８文字
while IFS= read -r c_Line
do
	if [ "${c_Line}" = "${c_x0a}" ]; then
		echo "f_NewLine"
	elif [ "${c_Line}" = "${c_x0c}" ]; then
		echo "showpage f_NewPage"
	else
		echo -n "("
		echo -n "${c_Line}" | sed -e "s/\\\/\\\\\\\/g" -e "s/(/\\\(/g" -e "s/)/\\\)/g" -e "s/${c_x09}/${c_Tab}/g"
		echo " ) f_ShowStrings f_NewLine"
	fi
done
# -------------------------------------
#
# -------------------------------------
echo "showpage"
