OSDN Git Service

最初のリリース
[utf82ps/utf82ps.git] / utf82ps
1 #!/bin/sh
2 # -------------------------------------
3 # 2014.07.27
4 # utf82ps ver 0.1
5 # -------------------------------------
6 # -------------------------------------
7 # パラメータ変数
8 # -------------------------------------
9 c_Usage="Usage: utf82ps.sh [-p A4|A3|B5|B4|15i] [-L] [-m margin-left,right,top,bottom] [-f fontsize ] [ -l line-space% ] [ -w font-width% ] [ -g ] file"
10 c_PaperSize="A4"
11 c_PaperMargin=""
12 c_Landscape="no" 
13 d_FontSize="12" # point
14 d_LineSpace="10" # % d_FontSize / 100 * d_LineSpace
15 d_FontWidth="100" # % d_FontSize / 100 * d_FontWidth
16
17 c_MincyoFont="/Ryumin-Light-UniJIS-UTF8-H"
18 c_GothicFont="/GothicBBB-Medium-UniJIS-UTF8-H"
19 c_DefaultFont=$c_MincyoFont
20 #c_DefaultFont=$c_GothicFont
21 # -------------------------------------
22 # 用紙のルックアップテーブル
23 # -------------------------------------
24 # 15iはおそらく使わないと思いますがサンプルとして。15iは用紙方向を変えたくないので二つともPortになってます。
25 # paper margi                           paper size      orientation
26 #       left    right   top     bottom  x       y       default swich
27 c_PaperTable="
28 A4      25.4    12.7    25.4    12.7    210     297     Port    Land
29 A3      25.4    12.7    25.4    12.7    297     420     Port    Land
30 B5      25.4    12.7    25.4    12.7    182     257     Port    Land
31 B4      25.4    12.7    25.4    12.7    257     364     Port    Land
32 15i     0       0       0       0       381     279.4   Port    Port
33 "
34 # -------------------------------------
35 # パラメータ処理
36 # -------------------------------------
37 while getopts "p:Lm:f:l:w:gh" OPT
38 do
39         case ${OPT} in
40                 p) c_PaperSize=${OPTARG} ;;
41                 L) c_Landscape="yes" ;;
42                 m) c_PaperMargin=${OPTARG} ;;
43                 f) d_FontSize=${OPTARG} ;;
44                 l) d_LineSpace=${OPTARG} ;;
45                 w) d_FontWidth=${OPTARG} ;;
46                 g) c_DefaultFont=$c_GothicFont ;;
47                 h|\?) echo "$c_Usage"; exit 1;;
48         esac
49 done
50 shift `expr ${OPTIND} - 1`
51 # -------------------------------------
52 # 入力の統一
53 # -------------------------------------
54 if [ "${1}" != "" ]; then
55         exec <"${1}"
56 fi
57 # -------------------------------------
58 # マージン取得
59 # -------------------------------------
60 if [ "${c_PaperMargin}" = "" ]; then
61         c_Table=`echo "${c_PaperTable}" | grep "${c_PaperSize}"`
62 else
63         c_tab=`printf "\t"`
64         c_Temp=`echo "${c_PaperMargin}" | sed -e s/,/"${c_tab}"/g`
65         c_Table="${c_PaperSize}$c_tab${c_Temp}"
66 fi
67
68 for i in `seq 2 5`
69 do
70         d_Temp=`echo "${c_Table}" | cut -f${i}`
71
72         case ${i} in
73                 2) d_MarginLeft=${d_Temp} ;;
74                 3) d_MarginRight=${d_Temp} ;;
75                 4) d_MarginTop=${d_Temp} ;;
76                 5) d_MarginBottom=${d_Temp} ;;
77         esac
78 done
79 # -------------------------------------
80 # 用紙サイズ取得
81 # -------------------------------------
82 c_Table=`echo "${c_PaperTable}" | grep "${c_PaperSize}"`
83
84 for i in `seq 6 7`
85 do
86         d_Temp=`echo "${c_Table}" | cut -f${i}`
87
88         case ${i} in
89                 6) d_PaperSizeX=${d_Temp} ;;
90                 7) d_PaperSizeY=${d_Temp} ;;
91         esac
92 done
93 # -------------------------------------
94 # 用紙方向取得
95 # -------------------------------------
96 c_Table=`echo "${c_PaperTable}" | grep "${c_PaperSize}"`
97
98 for i in `seq 8 9`
99 do
100         d_Temp=`echo "${c_Table}" | cut -f${i}`
101
102         case ${i} in
103                 8) c_PaperOrientation=${d_Temp} ;;
104                 9) if [ "${c_Landscape}" = "yes" ]; then
105                         c_PaperOrientation=${d_Temp}
106                 fi
107                 ;;
108         esac
109 done
110
111 if [ "${c_PaperOrientation}" = "Land" ]; then
112         d_Temp=${d_PaperSizeX}
113         d_PaperSizeX=${d_PaperSizeY}
114         d_PaperSizeY=${d_Temp}
115 fi
116 # -------------------------------------
117 # Postscript出力
118 # -------------------------------------
119 echo "%!PS-Adobe-3.0"
120 echo "/mm {2.834645669 mul} def"
121 echo "/d_MarginLeft ${d_MarginLeft} mm def"
122 echo "/d_MarginRight ${d_PaperSizeX} mm ${d_MarginRight} mm sub def"
123 echo "/d_MarginTop ${d_PaperSizeY} mm ${d_MarginTop} mm sub def"
124 echo "/d_MarginBottom ${d_MarginBottom} mm def"
125 echo "/d_FontSize ${d_FontSize} def"
126 echo "/d_LineSpace d_FontSize 100 div ${d_LineSpace} mul def"
127 echo "/d_FontWidth d_FontSize 100 div ${d_FontWidth} mul def"
128 echo "/c_DefaltFont ${c_DefaultFont} findfont [ d_FontWidth 0 0 d_FontSize 0 0 ] makefont def"
129 echo "%"
130 echo "/d_1ByteCh { 16#7f le } def"
131 echo "/d_2ByteCh { dup 16#cf ge exch 16#df le and } def"
132 echo "/d_3ByteCh { 16#ef le } def"
133 echo "/d_4ByteCh { dup 16#f0 ge exch 16#f7 le and } def"
134 echo "/d_5ByteCh { dup 16#f8 ge exch 16#fb le and } def"
135 echo "/d_6ByteCh { dup 16#fc ge exch 16#fd le and } def"
136 echo "%"
137 echo "/c_BeforSt { 0 exch getinterval } def"
138 echo "/d_AfterStDic 1 dict def"
139 echo "/c_AfterSt {
140         d_AfterStDic begin
141                 /d_Start exch def
142                 /c_PrintSt exch def
143                 /d_Length c_PrintSt length d_Start sub def
144                 c_PrintSt d_Start d_Length getinterval
145         end
146 } def"
147 echo "/d_StringCutDic 1 dict def"
148 echo "/c_StringCut {
149         d_StringCutDic begin
150                 /c_PrintSt exch def
151                 1 {
152                         c_PrintSt 0 get d_1ByteCh { 1 /d_Byte exch def exit } if
153                         c_PrintSt 0 get d_2ByteCh { 2 /d_Byte exch def exit } if
154                         c_PrintSt 0 get d_3ByteCh { 3 /d_Byte exch def exit } if
155                         c_PrintSt 0 get d_4ByteCh { 4 /d_Byte exch def exit } if
156                         c_PrintSt 0 get d_5ByteCh { 5 /d_Byte exch def exit } if
157                         c_PrintSt 0 get d_6ByteCh { 6 /d_Byte exch def exit } if
158                 } repeat
159                 c_PrintSt d_Byte c_AfterSt c_PrintSt d_Byte c_BeforSt
160         end
161
162 } def"
163 echo "%"
164 echo -n "/f_NewPage { "
165 if [ "${c_PaperOrientation}" = "Land" ]; then
166         echo -n "90 rotate 0 ${d_PaperSizeY} mm -1 mul translate "
167 fi
168 echo "d_MarginLeft d_MarginTop d_FontSize sub moveto } def"
169 echo "/f_NewLine { d_MarginLeft currentpoint exch pop d_FontSize sub d_LineSpace sub moveto } def"
170 echo "/f_NextString { dup length 0 gt { f_ShowStrings } { pop } ifelse } def"
171 echo "/f_ShowStrings {
172         c_StringCut
173         dup stringwidth pop currentpoint pop add
174         d_MarginRight gt { f_NewLine } if
175         currentpoint exch pop d_FontSize sub
176         d_MarginBottom lt { showpage f_NewPage } if
177         show f_NextString
178 } def"
179 echo "c_DefaltFont setfont"
180 echo "f_NewPage"
181 # -------------------------------------
182 # データ読み込み
183 # -------------------------------------
184 c_x0a=`printf "\x0a"`           # 改行
185 c_x0c=`printf "\x0c"`           # 改ページ 
186 c_x09=`printf "\x09"`           # タブ
187 c_Tab=`printf "        "`       # 置換後タブ 空白8文字
188 while IFS= read -r c_Line
189 do
190         if [ "${c_Line}" = "${c_x0a}" ]; then
191                 echo "f_NewLine"
192         elif [ "${c_Line}" = "${c_x0c}" ]; then
193                 echo "showpage f_NewPage"
194         else
195                 echo -n "("
196                 echo -n "${c_Line}" | sed -e "s/\\\/\\\\\\\/g" -e "s/(/\\\(/g" -e "s/)/\\\)/g" -e "s/${c_x09}/${c_Tab}/g"
197                 echo " ) f_ShowStrings f_NewLine"
198         fi
199 done
200 # -------------------------------------
201 #
202 # -------------------------------------
203 echo "showpage"