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