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