4 use vars qw($opt_in_cs $opt_order $opt_kage $opt_help $usage
5 $in_cs $out_cs $i @chars
7 $char $char_id $out_char
8 $ids $ids_argc %ids $idsdb
9 $idsdata_file $ids_start $font_start
15 use Chise_utils ':all';
17 my $perl="/usr/bin/perl";
18 my $makefonts="/Users/izumi/.chise/makefonts.pl";
20 my $geta=pack("S",8750|0x8080);
22 if($^V and $^V ge v5.8){
24 }elsif($^V and $^V ge v5.6){
27 print STDERR "This versin is not supported.";
31 binmode(STDIN, ':encoding(utf8)');
32 binmode(STDOUT, ':encoding(utf8)');
35 &GetOptions("in=s"=>\$opt_in_cs,
36 "order=s"=>\$opt_order,
41 Usage: $0 [-i <input coding system>] [-o <order of kanji>] [-k] <filename>
42 input coding system: (default: ucs\@mcs)
43 ucs\@mcs, ucs\@cns, ucs\@gb, ucs\@jis, ucs\@ks
44 order of kanji: (default: j)
50 m: Multi, use \\UTFM of otf.sty
51 You can also combine them, ex. jtcgkm
55 %order=('c'=>'UniCNS',
68 if(not defined($opt_in_cs)){
69 print STDERR "Assuming input coding system is ucs\@mcs.\n";
72 if(($opt_in_cs eq 'ucs@mcs')or
73 ($opt_in_cs eq 'ucs@cns')or
74 ($opt_in_cs eq 'ucs@gb')or
75 ($opt_in_cs eq 'ucs@jis')or
76 ($opt_in_cs eq 'ucs@ks')){
79 print STDERR "Invalid arguments.\n";
85 if(not defined($opt_order)){
86 print STDERR "Using only UniJIS.\n";
90 if(defined($opt_order)){
91 if($opt_order=~/^[cgjktm]+$/){
92 @order=split(//,$opt_order);
93 @order=map {$order{$_}} @order;
95 print STDERR "Invalid option!\n";
105 $idsdata_file="idsdata.pl";
109 if(-e $idsdata_file){
110 require $idsdata_file;
117 "=gt-pj-1","=gt-pj-2","=gt-pj-3","=gt-pj-4","=gt-pj-5",
118 "=gt-pj-6","=gt-pj-7","=gt-pj-8","=gt-pj-9","=gt-pj-10",
120 #,"=gt-pj-k1","=gt-pj-k2"
122 @HZK=("=hanziku-1","=hanziku-10","=hanziku-11","=hanziku-12",
123 "=hanziku-2","=hanziku-3","=hanziku-4","=hanziku-5",
124 "=hanziku-6","=hanziku-7","=hanziku-8","=hanziku-9");
128 s/&(.*?);/&de_er($1)/ge;
129 if($in_cs ne 'ucs@mcs'){
130 s/(.)/pack("U",&get_char_id(unpack("U",$1),$in_cs))/ge;
133 for($i=0;$i<=$#chars;$i++){
135 $char_id=unpack("U",$chars[$i]);
141 }elsif($char_id>=0x2ff0 and $char_id<=0x2fff){
142 # Ideographic Description Characters
145 }elsif($char_id >= 0x20000 && $char_id <=0x2a6df){
146 # CJK Unified Ideographs Extension B
147 if(not defined($ids{$char}) and $ids{$char}[1]>=0){
148 $ids{$char}[0]=$font_start;
149 $ids{$char}[1]=$ids_start;
156 print "{\\fontencoding{OT1}\\fontfamily{" .
157 sprintf("chise%03d",$ids{$char}[0]) .
158 "}\\selectfont\\char$ids{$char}[1]}";
161 if(($out_char=&get_output_char($char_id))){
164 print &get_macro_for_ids(&get_ids($char));
170 open(IDSDATA,">$idsdata_file") or die;
171 print IDSDATA 'use utf8;',"\n";
172 foreach $ids (keys %ids){
173 print IDSDATA '$ids{\'',$ids,'\'}='
174 ,'[',join ",",@{$ids{$ids}},"];\n" if($perl56);
175 print IDSDATA '$ids{\'',encode('utf8',$ids),'\'}='
176 ,'[',join ",",@{$ids{$ids}},"];\n" if($perl58);
178 print IDSDATA '$font_start=',$font_start,";\n";
179 print IDSDATA '$ids_start=',$ids_start,";\n";
184 system($perl,$makefonts) ==0
185 or die "Could not make fonts: $?";
187 print STDERR "cannot find $makefonts\n";
195 # return: return character for output,
197 # or GETA character if ids is invalid.
198 my($ids,$ids_argc)=&ids_rest("",0,$chars[$i]);
204 print STDERR "IDS parse error: $ids\n";
208 ($ids,$ids_argc)=&ids_rest($ids,$ids_argc,$chars[$i]);
210 if(($char_id=&get_char_id_for_ids($ids))
211 and($out_char=&get_output_char($char_id))){
214 return &get_macro_for_ids($ids) if($perl56);
215 return encode('utf8', &get_macro_for_ids($ids)) if($perl58);
222 # arguments: <ids>, <rest number of arguments for ids>, <character>
223 # return: ids and rest number of arguments for ids.
224 my($ids,$ids_argc,$char)=@_;
226 $argc=&ids_argc($char);
228 $ids_argc+= $ids_argc==0 ? $argc : $argc-1;
232 $ids.=$char if($perl56);
233 $ids.=encode('utf8',$char) if($perl58);
234 return ($ids,$ids_argc);
237 sub get_macro_for_ids{
239 # return: TeX macro for ids
240 # or GETA character if ids is invalid for KAGE.
242 # return $geta if(not $exec_makefonts);
243 $ids=&normalize_ids($ids,"UniJIS");
244 return $geta if(($ids!~/[$idc]/)
245 or($ids=~/[\x{10000}-]/));
247 if(not defined($ids{$ids}) and $ids{$ids}[1]>=0){
248 $ids{$ids}[0]=$font_start;
249 $ids{$ids}[1]=$ids_start;
256 return "{\\fontencoding{T1}\\fontfamily{"
257 .sprintf("chisesub%03d",$ids{$ids}[0])
258 ."}\\selectfont\\char$ids{$ids}[1]}";
262 # argument: <ids>, <output coding system>
263 # return: ids or GETA character if ids is invalid for KAGE.
265 $ids = decode('utf8', $ids) if $perl58;
266 $out_cs=~s/Uni(.+)/"ucs\@".lc($1)/e;
269 my($char,$char_id,$output_char_id);
270 while($ids=~m/(.)/g){
272 $char_id=unpack("U",$char);
275 }elsif($output_char_id=&get_char_attribute($char,"=$out_cs")){
276 $output_ids.=pack("U",$output_char_id);
277 }elsif($output_char_id=&get_char_attribute($char,"=ucs")){
278 $output_ids.=pack("U",$output_char_id);
287 # argument: <char-id>
288 # return: character in EUC-JP or TeX macro for pTeX.
290 my($char,$out_char,$out_char_id,$gt);
292 $char=pack('U',$char_id);
294 if($out_char=&get_char_attribute($char,'=jis-x0208')){
295 return pack("S",$out_char|0x8080);
297 foreach $out_cs (@order){
298 if($out_cs eq 'UniJIS'
299 and &get_char_attribute($char,"adobe-unijis-utf16-h")){
300 if($out_char_id=&get_char_attribute($char,'=ucs@jis')
301 or $out_char_id=&get_char_attribute($char,'=ucs')){
302 return "\\UTF{".sprintf("%X",$out_char_id)."}";
304 }elsif($out_cs eq 'UniGB'
305 and &get_char_attribute($char,"adobe-unigb-ucs2-h")){
306 if($out_char_id=&get_char_attribute($char,'=ucs@gb')
307 or $out_char_id=&get_char_attribute($char,'=ucs')){
308 return "\\UTFC{".sprintf("%X",$out_char_id)."}";
310 }elsif($out_cs eq 'UniCNS'
311 and &get_char_attribute($char,"adobe-unicns-ucs2-h")){
312 if($out_char_id=&get_char_attribute($char,'=ucs@cns')
313 or $out_char_id=&get_char_attribute($char,'=ucs')){
314 return "\\UTFT{".sprintf("%X",$out_char_id)."}";
316 }elsif($out_cs eq 'UniKS'
317 and &get_char_attribute($char,"adobe-uniks-ucs2-h")){
318 if($out_char_id=&get_char_attribute($char,'=ucs@ks')
319 or $out_char_id=&get_char_attribute($char,'=ucs')){
320 return "\\UTFK{".sprintf("%X",$out_char_id)."}";
322 }elsif($out_cs eq 'GT'){
323 return $gt if($gt=&get_macro_for_GT($char_id));
324 }elsif($out_cs eq 'Multi'){
325 if($out_char_id=&get_char_attribute($char,'=ucs')){
326 return "\\UTFM{".sprintf("%X",$out_char_id)."}";
335 # argument: <character>
339 $ids=&get_char_attribute($char,"ids-aggregated")
340 or &get_char_attribute($char,"ids");
341 $ids=decode('utf8', $ids) if($perl58);
345 sub get_char_id_for_ids{
346 # argument: <character>
350 $ids=decode('utf8', $ids) if($perl58);
352 if(($output_char)=&get_chars_matching("ids",$ids)){
353 return unpack("U",$output_char);
360 # argument: <char-id>, <input coding system>
362 my($char_id,$in_cs)=@_;
365 if(($output_char)=&get_chars_matching("=$in_cs",$char_id)){
366 return unpack("U",$output_char);
372 sub get_macro_for_GT{
373 # argument: <char-id>
374 # return: TeX macro for GT fonts.
377 $char=pack("U",$char_id);
379 if($gt=&get_char_attribute($char,$_)){
380 m/gt\-pj\-(\d+)/ and $GT=$1;
385 return "{\\fontfamily{"
386 .sprintf("gto%02d",$GT)
387 ."}\\selectfont\\char".($gt|0x8080)."}";