correct coding system to euc-jp.
[chise/omega.git] / inCHISE
1 #!/usr/bin/perl -w -CSD
2
3 # ver.0.2
4
5 use strict;
6 use vars qw($omegadb_path
7             $opt_protrude $opt_allow_unify
8             $opt_use_kage_for_Ext_B
9             %opt_order %order %order_map
10             $opt_in_cs $opt_out_cs
11             $opt_help $usage
12             $in_cs $out_cs $i @chars
13             $char $char_id $out_char
14             $char_unified @chars_unified
15             $ids $ids_argc %ids $idsdb $geta
16             $idsdata_file $ids_start $font_start
17             @CDP @HZK @GT
18             );
19 use Getopt::Long;
20 use utf8;
21 use Fcntl ':flock';
22 use Chise_utils ':all';
23 require 5.008;
24
25 ### Options ###
26
27 #$opt_order{'UniMulti'}='jcgk';
28 $opt_order{'UniMulti'}='jGcgkHC';
29 $opt_order{'UniCNS'}='c';
30 $opt_order{'UniGB'}='g';
31 $opt_order{'UniJIS'}='j';
32 $opt_order{'UniKS'}='k';
33
34 $opt_allow_unify=1; # 1=true, 0=false.
35 $opt_protrude=0;# 1=true, 0=false.
36
37 # currently does not work.
38 $opt_use_kage_for_Ext_B=0;# 1=true, 0=false.
39
40 ### End ###
41
42 my $strictly_forbidden_after = '「【『[(〈“‘‘(〔{《{\[\(\x{3016}{「';
43 #       \x{3016} | # white 【
44
45 my $forbidden_after = "\x{0000}";
46
47 # ¥¥$$〒♯##¢¢££@@§
48 my $slightly_forbidden_after = '¥¥$$〒♯##¢¢££@@§';
49
50 # $strictly_forbidden_before
51 # All these characters are allowed to protrude
52 # in the right margin
53 my $strictly_forbidden_before=
54     '!,.:;?、。!,.:;?。\)#}’”〉》」』】〕\x{3017})]}}」\]';
55 ###       \x{3017} | # white 】
56
57 my $forbidden_before
58     = 'ー々ぁぃぅぇぉゃゅょっゎァィゥェォャュョッヮヵヶ';
59
60 my $slightly_forbidden_before
61     = '\x{000a}\#\-‐−‰′″℃゛゜ゝゞヽヾ"%-゙゚';
62
63 my $asian = '\x{1100}-\x{11FF}\x{2E80}-\x{D7AF}\x{F900}-\x{FAFF}\x{FE30}-\x{FE4F}\x{FF00}-\x{FFFFFF}';
64
65 my $space = '\x{0020}\x{0009}\x{000A}\x{000C}\x{000D}';
66
67 my %tex_meta=('#'=>'\#',
68               '$'=>'\\textdollar{}',
69               '%'=>'\%',
70               '&'=>'\&',
71               '{'=>'\\textbraceleft{}',
72               '}'=>'\\textbraceright{}',
73               '\\'=>'\\textbackslash{}',
74               '_'=>'\\textunderscore',
75              );
76
77 my $tex_meta_re=join('|',map {quotemeta($_)} keys %tex_meta);
78
79 &GetOptions("in=s"=>\$opt_in_cs,
80             "out=s"=>\$opt_out_cs,
81             "help",\$opt_help);
82
83 $usage=<<EOF;
84 Usage: $0 -i <input coding system> -o <cmap encoding>
85     input coding system:
86       Utf8mcs, Utf8cns, Utf8gb, Utf8jis, Utf8ks, Utf8big5
87     cmap encoding:
88       UniCNS, UniGB, UniJIS, UniKS, UniMulti
89 EOF
90
91 if($opt_in_cs or $opt_out_cs){
92     $in_cs=$opt_in_cs;
93     $out_cs=$opt_out_cs;
94 }elsif(@ARGV==0){
95     ($in_cs,$out_cs)=($0=~/(Utf8.+)To(\w+)/);
96 }
97
98 # $in_cs:
99 #   Utf8mcs,Utf8cns,Utf8gb,Utf8jis,Utf8ks,Utf8big5
100 # $out_cs:
101 #   UniCNS,UniGB,UniJIS,UniKS,UniMulti
102
103 $in_cs=~s/Utf8/ucs\@/;
104
105 if($opt_help
106    or not defined($in_cs)
107    or not defined($out_cs)){
108     print $usage;
109     exit 1;
110 }
111
112 $omegadb_path=~s!/$!!;
113
114 $idsdata_file="$omegadb_path/idsdata.pl";
115 $ids_start=0x00; 
116 $font_start=0;
117
118 if(-e $idsdata_file){
119     open(IDSDATA,"+<:utf8",$idsdata_file) or die;
120     flock(IDSDATA,LOCK_EX);
121     seek(IDSDATA,0,0);
122     while(<IDSDATA>){
123         eval $_;
124     }
125     seek(IDSDATA,0,0);
126 #         require $idsdata_file;
127 }else{
128     open(IDSDATA,">:utf8",$idsdata_file) or die;
129     flock(IDSDATA,LOCK_EX);
130     seek(IDSDATA,0,0);
131 }
132
133 $ids_argc=0;
134 $ids="";
135
136 #$geta=pack("U",0x3013);
137 $geta=pack("U",0xfffd);
138
139 @GT=(#"=gt","=gt-k",
140      "=gt-pj-1","=gt-pj-2","=gt-pj-3","=gt-pj-4","=gt-pj-5",
141      "=gt-pj-6","=gt-pj-7","=gt-pj-8","=gt-pj-9","=gt-pj-10",
142      "=gt-pj-11"
143      #,"=gt-pj-k1","=gt-pj-k2"
144      );
145 @HZK=("=hanziku-1","=hanziku-2","=hanziku-3","=hanziku-4",
146       "=hanziku-5","=hanziku-6","=hanziku-7","=hanziku-8",
147       "=hanziku-9","=hanziku-10","=hanziku-11","=hanziku-12");
148 @CDP=("=big5-cdp");
149
150 %order_map=('c'=>'UniCNS',
151             'g'=>'UniGB',
152             'j'=>'UniJIS',
153             'k'=>'UniKS',
154             'G'=>'GT',
155             'H'=>'HZK',
156             'C'=>'CDP',
157             );
158
159 foreach $out_cs ('UniCNS','UniGB','UniJIS','UniKS','UniMulti'){
160     if(defined($opt_order{$out_cs})){
161         if($opt_order{$out_cs}=~/^[cgjkGHC]+$/){
162             @{$order{$out_cs}}=map {$order_map{$_}}
163             (split(//,$opt_order{$out_cs}));
164         }else{
165             print STDERR "Invalid order for $out_cs!\n";
166             exit 1;
167         }
168     }
169 }
170
171 while(<>){
172     utf8::decode($_);
173     if($in_cs ne 'ucs@mcs'){
174         s/(.)/&get_char_in_mcs($1,$in_cs)/ge;
175     }
176     s/(amp.+?;)/&de_tex_er($1)/ge;
177 #    s/(&.+?;)/&de_tex_er($1)/ge;
178     @chars=split(//);
179   CHAR:
180     for($i=0;$i<=$#chars;$i++){
181         $char=$chars[$i];
182         $char_id=unpack("U",$char);
183
184         if($char_id<=0x20){
185             print $chars[$i];
186             next CHAR;
187         }elsif($char=~m/($tex_meta_re)/o){
188             print $tex_meta{$1};
189             next CHAR;
190         }elsif($char_id>0x20 and $char_id<=0x02af){
191             # Basic Latin
192             # Latin-1 Supplement
193             # Latin Extended-A
194             # Latin Extended-B
195             # IPA Extensions
196             print &latin_parse();
197             next CHAR;
198         }elsif($char_id>=0x2ff0 and $char_id<=0x2fff){
199             # Ideographic Description Characters
200             print &ids_parse();
201             next CHAR;
202         }else{
203             if(($out_char=&get_output_char($char,$out_cs))){
204                 print $out_char,&add_break($i);
205             }else{
206                 if($opt_allow_unify){
207                     @chars_unified=&get_chars_unified($char);
208                     if(@chars_unified>0){
209                         foreach $char_unified (@chars_unified){
210                             if(($out_char
211                                 =&get_output_char($char_unified,$out_cs))){
212                                 print $out_char,&add_break($i);
213                                 next CHAR;
214                             }
215                         }
216                     }
217                 }
218                 if($opt_use_kage_for_Ext_B){
219                     if($char_id >= 0x20000 && $char_id <=0x2a6df){
220                         # CJK Unified Ideographs Extension B
221                         if(not defined($ids{$char}) and $ids{$char}[1]>=0){
222                             $ids{$char}[0]=$font_start;
223                             $ids{$char}[1]=$ids_start;
224                             $ids_start++;
225                             if($ids_start>255){
226                                 $ids_start=0;
227                                 $font_start++;
228                             }
229                         }
230                         print "{\\fontencoding{OT1}\\fontfamily{" .
231                             sprintf("chise%03d",$ids{$char}[0]) .
232                                 "}\\selectfont\\char$ids{$char}[1]}",&add_break($i);
233                         next CHAR;
234                     }
235                 }
236                 if($ids=&get_ids($char)){
237                     print &get_macro_for_ids($ids),&add_break($i);
238                 }else{
239                     print '\rule{1ex}{1ex}',&add_break($i);
240                 }
241             }
242         }
243     }
244 }
245
246 print IDSDATA 'use utf8;',"\n";
247 foreach $ids (keys %ids){
248     print IDSDATA '$ids{\'',$ids,'\'}='
249     ,'[',join ",",@{$ids{$ids}},"];\n";
250 }
251 print IDSDATA '$font_start=',$font_start,";\n";
252 print IDSDATA '$ids_start=',$ids_start,";\n";
253 print IDSDATA "1;";
254 flock(IDSDATA,LOCK_UN);
255
256 exit 0;
257
258 sub de_tex_er{
259     my($er)=@_;
260     my($prefix,$suffix);
261     my($output_char,$atr,$value);
262     $er=~/^(amp)(.*)(;)$/
263         and $prefix=$1,$er=$2,$suffix=$3;
264     $prefix or $prefix="",$suffix or $suffix="";
265     if($er=~/^U[\+|\-]([a-fA-F\d]+)/){
266         $output_char=pack("U",hex($1));
267     }elsif($er=~/^(?:I\-)?($er_prefix_re)\-?([0-9a-fA-F]+)$/){
268         ($atr,$value)=($1,$2);
269         unless($er_alias{$atr}=~/daikanwa|gt/){
270             $value=hex($value);
271         }
272         ($output_char)=&get_chars_matching($er_alias{$atr},$value);
273     }
274     if($output_char){
275         return $output_char;
276     }else{
277         return $prefix.$er.$suffix;
278     }
279 }
280
281 sub add_break{
282     my($i)=@_;
283
284     if($i<($#chars-1)){
285         if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
286            and($chars[$i+2]=~m/[$strictly_forbidden_before]/o)){
287             return "\\CJKunbreakablekernone ";
288         }elsif($opt_protrude){
289             if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
290                and($chars[$i+2]=~m/[^$strictly_forbidden_before]/o)){
291                 return "\\CJKunbreakablekernone \\CJKprotrude ";
292             }
293         }
294     }
295     if(($i<$#chars)
296        and($chars[$i+1]=~m/[$strictly_forbidden_before]/o)){
297         return "\\CJKunbreakablekernone ";
298     }
299     if($chars[$i]=~m/[$strictly_forbidden_after]/o){
300         return "\\CJKunbreakablekernone ";
301     }
302     if(($i<$#chars)
303        and($chars[$i+1]=~m/[$forbidden_before]/o)){
304         return "\\CJKunbreakablekerntwo ";
305
306     }
307     if($chars[$i]=~m/[$forbidden_after]/o){
308         return "\\CJKunbreakablekerntwo ";
309     }
310     if(($i<$#chars)
311        and($chars[$i+1]=~m/[$slightly_forbidden_before]/o)){
312         return "\\CJKunbreakablekernthree ";
313     }
314     if($chars[$i]=~m/[$slightly_forbidden_after]/o){
315         return "\\CJKunbreakablekernthree ";
316     }
317     if($chars[$i]=~m/[$asian]/o){
318         return "\\CJKbreakablekern ";
319     }
320     if(($i<$#chars)and($chars[$i+1]=~m/[$asian]/o)){
321         return "\\CJKbreakablekern ";
322     }
323 }
324
325 sub latin_parse{
326     # arguments: none
327     # return: string for output with TeX macro.
328     my($char_id);
329     my $out_str=$chars[$i];
330     $i++;
331     while($i<=$#chars){
332         $char_id=unpack("U",$chars[$i]);
333         if($char_id>0x20 and $char_id<=0x02af){
334             $out_str.=$chars[$i];
335         }else{
336             $i--;
337             last;
338         }
339         $i++;
340     }
341     return '{\fontencoding{UT1}\fontfamily{omlgc}\selectfont '.$out_str.'}';
342 }
343
344 sub ids_parse{
345     # arguments: none
346     # return: character for output,
347     #          TeX macro for ids,
348     #          or GETA character if ids is invalid.
349     my($ids,$ids_argc)=&ids_rest("",0,$chars[$i]);
350
351     while($ids_argc>0){
352         # We are in IDS.
353         $i++;
354         if($i>$#chars){
355             print STDERR "IDS parse error: $ids\n";
356             return $geta;
357         }
358
359         ($ids,$ids_argc)=&ids_rest($ids,$ids_argc,$chars[$i]);
360         if($ids_argc==0){
361             if(($char=&get_char_for_ids($ids))
362                and($out_char=&get_output_char($char,$out_cs))){
363                 return $out_char;
364             }else{
365                 return &get_macro_for_ids($ids);
366             }
367         }
368     }
369 }
370
371 sub ids_rest{
372     # arguments: <ids>, <rest number of arguments for ids>, <character>
373     # return: ids and rest number of arguments for ids.
374     my($ids,$ids_argc,$char)=@_;
375     my($argc);
376     $argc=&ids_argc($char);
377     if($argc){
378         $ids_argc+=$ids_argc==0?$argc:$argc-1;
379     }else{
380         $ids_argc--;
381     }
382     $ids.=$char;
383     return ($ids,$ids_argc);
384 }
385
386 sub get_macro_for_ids{
387     # argument: <ids>
388     # return: TeX macro for ids
389     #          or GETA character if ids is invalid for KAGE.
390     my($ids)=@_;
391     # $ids=&normalize_ids($ids,"UniJIS");
392     return $geta if(($ids!~/[$idc]/)
393                     or($ids=~/[\x{10000}-]/));
394                     #irregular for KAGE.
395     if(not defined($ids{$ids})){
396         $ids{$ids}[0]=$font_start;
397         $ids{$ids}[1]=$ids_start;
398         $ids_start++;
399     }
400     if($ids_start>255){
401         $ids_start=0;
402         $font_start++;
403     }
404     return "{\\fontencoding{OT1}\\fontfamily{"
405         .sprintf("chise%03d",$ids{$ids}[0])
406         ."}\\selectfont\\char$ids{$ids}[1]}";
407 }
408
409 sub normalize_ids{
410     # argument: <ids>, <output coding system>
411     # return: ids or GETA character if ids is invalid for KAGE.
412     my($ids,$out_cs)=@_;
413     $out_cs=~s/Uni(.+)/'ucs@'.lc($1)/e;
414
415     my $output_ids="";
416     my($char,$output_char_id);
417     while($ids=~m/(.)/g){
418         $char=$1;
419         if($char=~/[$idc]/){
420             $output_ids.=$char;
421         }elsif($output_char_id=&get_char_attribute($char,"=$out_cs")
422            or $output_char_id=&get_char_attribute($char,"=ucs")
423            or $output_char_id=&get_char_attribute($char,"=>$out_cs")
424            or $output_char_id=&get_char_attribute($char,"=>ucs")
425            or $output_char_id=&get_char_attribute($char,"=>ucs*")
426               ){
427             $output_ids.=pack("U",$output_char_id);
428         }else{
429             return $geta;
430         }
431     }
432     return $output_ids;
433 }
434
435 sub get_output_char{
436     # argument: <char>
437     # return: character in output coding system or TeX macro or undef.
438     my($char,$out_cs)=@_;
439     my($out_char_id,$suffix);
440     my($gt,$hzk,$cdp);
441
442     foreach $out_cs (@{$order{$out_cs}}){
443         if($out_cs eq 'UniJIS'
444            and &get_char_attribute($char,"vnd-adobe-cid-unijis-utf16-h")){
445             if($out_char_id=&get_char_attribute($char,'=ucs@jis')
446                or $out_char_id=&get_char_attribute($char,'=ucs')
447                or $out_char_id=&get_char_attribute($char,'=>ucs@jis')
448                or $out_char_id=&get_char_attribute($char,'=>ucs')
449                or $out_char_id=&get_char_attribute($char,'=>ucs*')
450                ){
451                 return '{\selectjisfont\char'.$out_char_id.'}';
452             }
453         }elsif($out_cs eq 'UniGB'
454                and &get_char_attribute($char,"vnd-adobe-cid-unigb-ucs2-h")){
455             if($out_char_id=&get_char_attribute($char,'=ucs@gb')
456                or $out_char_id=&get_char_attribute($char,'=ucs')
457                or $out_char_id=&get_char_attribute($char,'=>ucs@gb')
458                or $out_char_id=&get_char_attribute($char,'=>ucs')
459                or $out_char_id=&get_char_attribute($char,'=>ucs*')
460                ){
461                 return '{\selectgbsfont\char'.$out_char_id.'}';
462             }
463         }elsif($out_cs eq 'UniCNS'
464                and &get_char_attribute($char,"vnd-adobe-cid-unicns-ucs2-h")){
465             if($out_char_id=&get_char_attribute($char,'=ucs@cns')
466                or $out_char_id=&get_char_attribute($char,'=ucs')
467                or $out_char_id=&get_char_attribute($char,'=>ucs@cns')
468                or $out_char_id=&get_char_attribute($char,'=>ucs')
469                or $out_char_id=&get_char_attribute($char,'=>ucs*')
470                ){
471                 return '{\selectcnsfont\char'.$out_char_id.'}';
472             }
473         }elsif($out_cs eq 'UniKS'
474                and &get_char_attribute($char,"vnd-adobe-cid-uniks-ucs2-h")){
475             if($out_char_id=&get_char_attribute($char,'=ucs@ks')
476                or $out_char_id=&get_char_attribute($char,'=ucs')
477                or $out_char_id=&get_char_attribute($char,'=>ucs@ks')
478                or $out_char_id=&get_char_attribute($char,'=>ucs')
479                or $out_char_id=&get_char_attribute($char,'=>ucs*')
480                ){
481                 return '{\selectksxfont\char'.$out_char_id.'}';
482             }
483         }elsif($out_cs eq 'GT'){
484             return $gt if($gt=&get_macro_for_GT($char));
485         }elsif($out_cs eq 'HZK'){
486             return $hzk if($hzk=&get_macro_for_HZK($char));
487         }elsif($out_cs eq 'CDP'){
488             return $cdp if($cdp=&get_macro_for_CDP($char));
489         }
490     }
491     return undef;
492 }
493
494 sub get_ids{
495     # argument: <character>
496     # return: ids
497     my($char)=@_;
498     my $ids="";
499     $ids=&get_char_attribute($char,"ids-aggregated")
500         or $ids=&get_char_attribute($char,"ids");
501 #    $ids=&get_char_attribute($char,"ids-decomposed")
502 #       or $ids=&get_char_attribute($char,"ids");
503 #         or $ids=&get_char_attribute($char,"ideographic-structure");
504 #    $ids=~s/[? ()]//g;
505     return $ids;
506 }
507
508 sub get_char_for_ids{
509     # argument: <ideographic description sequence>
510     # return: char or undef.
511     my($ids)=@_;
512     my($output_char);
513
514     if(($output_char)=&get_chars_matching("ids",$ids)){
515         return $output_char;
516     }else{
517         return undef;
518     }
519 }
520
521 sub get_char_in_mcs{
522     # argument: <char>, <input coding system>
523     # return:   char in ucs@mcs.
524     my($char,$in_cs)=@_;
525     my($output_char);
526
527     return $char if($in_cs eq 'ucs@mcs');
528
529     if(($output_char)=&get_chars_matching("=$in_cs",unpack("U",$char))){
530         return $output_char;
531     }else{
532         return $char;
533     }
534 }
535
536 sub get_chars_unified{
537     my($char)=@_;
538     my($chars,$ucs,$char_ucs);
539     my(@chars);
540
541     if($chars=&get_char_attribute($char,'->ucs-unified')){
542         $chars=~s/^\((.*)\)$/$1/;
543         return (split(/\s*\?/,$chars));
544     }elsif($ucs=&get_char_attribute($char,'=>ucs*')
545           or $ucs=&get_char_attribute($char,'=>ucs')){
546         $char_ucs=pack("U",$ucs);
547         if($chars=&get_char_attribute($char_ucs,'->ucs-unified')){
548             $chars=~s/^\((.*)\)$/$1/;
549             @chars=grep {not /^$char$/}
550                 (split(/\s*\?/,$chars));
551             push(@chars,$char_ucs);
552             return @chars;
553         }
554     }
555 }
556
557 sub get_macro_for_GT{
558     # argument: <char>
559     # return: TeX macro for GT fonts or undef.
560     my($char)=@_;
561     my($gt,$GT);
562     foreach (@GT){
563         if($gt=&get_char_attribute($char,$_)){
564             m/gt\-pj\-(\d+)/ and $GT=$1;
565             last;
566         }
567     }
568     if($gt){
569         return "{\\fontencoding{OT1}\\fontfamily{"
570             .sprintf("gt%02d",$GT)
571             ."}\\selectfont\\char".($gt|0x8080)."}";
572     }else{
573         return undef;
574     }
575 }
576
577 sub get_macro_for_HZK{
578     # argument: <char>
579     # return: TeX macro for Hanziku fonts or undef.
580     my($char)=@_;
581     my($hzk,$HZK);
582     foreach (@HZK){
583         if($hzk=&get_char_attribute($char,$_)){
584             m/hanziku\-(\d+)/ and $HZK=$1;
585             last;
586         }
587     }
588     if($hzk){
589         return "{\\fontencoding{OT1}\\fontfamily{".sprintf("hzk%02d",$HZK)."}\\selectfont\\char".$hzk."}";
590     }else{
591         return undef;
592     }
593 }
594
595 sub get_macro_for_CDP{
596     # argument: <char>
597     # return: TeX macro for CDP fonts or undef.
598     my($char)=@_;
599     my($cdp,$ucs);
600     foreach (@CDP){
601         if($cdp=&get_char_attribute($char,$_)){
602             last;
603         }
604     }
605     if($cdp){
606         $ucs=&get_char_attribute(&get_chars_matching("=big5-pua",$cdp),"=ucs");
607         if($ucs){
608             return "{\\fontencoding{OT1}\\fontfamily{cdp}\\selectfont\\char"
609                 .$ucs.
610                     "}";
611         }else{
612             print STDERR "This should not happen.\n";
613             print STDERR "ucs code point of CDP: $cdp not found.\n";
614         }
615     }else{
616         return undef;
617     }
618 }