add support for kanziku.
[chise/omega.git] / outCMAP
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             %utf8mcs_map_from
16             $inotp $perl56 $perl58
17             @CDP @HZK @GT
18             );
19 use Getopt::Long;
20 use utf8;
21 use Fcntl ':flock';
22 use Chise_utils ':all';
23
24 my $omegadb_path="/usr/local/lib/chise/omega";
25
26 ### Options ###
27
28 $opt_order='jtcgkh';
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         # not implemented yet.
143         # 'd'=>'CDP',
144         );
145
146 if(defined($opt_order)){
147     if($opt_order=~/^[cgjkth]*$/){
148         @order=split(//,$opt_order);
149         @order=map {$order{$_}} @order;
150     }else{
151         print STDERR "Invalid order!\n";
152         exit 1;
153     }
154 }
155
156 while(<>){
157     if($perl56){
158         # for perl 5.6
159         if($inotp){
160             s/(.)/pack("c",&get_char_id(unpack("U",$1),$in_cs))/ge;
161         }else{
162             s/(.)/pack("U",&get_char_id(unpack("U",$1),$in_cs))/ge;
163         }
164     }elsif($perl58){
165         # for perl 5.8.
166         $_=decode('utf8', $_);
167         s/(.)/pack("U",&get_char_id(unpack("U",$1),$in_cs))/ge;
168     }
169     s/(amp.+?;)/&de_er($1)/ge;
170 #    s/(&.+?;)/&de_er($1)/ge;
171     @chars=split(//);
172     for($i=0;$i<=$#chars;$i++){
173         $char=$chars[$i];
174         $char_id=unpack("U",$char);
175
176         if($char_id<=0x20){
177             print $chars[$i];
178             next;
179         }elsif($char_id>0x20 and $char_id<=0x02ff){
180             # Basic Latin
181             # Latin-1 Supplement
182             # Latin Extended-A
183             # Latin Extended-B
184             # IPA Extensions
185             print &latin_parse();
186             next;
187         }elsif($char_id>=0x2ff0 and $char_id<=0x2fff){
188             # Ideographic Description Characters
189             print &ids_parse();
190             next;
191         }else{
192             if(($out_char=&get_output_char($char_id,$out_cs))){
193                 print $out_char,&add_break($i);
194             }elsif($char_id >= 0x20000 && $char_id <=0x2a6df){
195                 # CJK Unified Ideographs Extension B
196                 if(not defined($ids{$char}) and $ids{$char}[1]>=0){
197                     $ids{$char}[0]=$font_start;
198                     $ids{$char}[1]=$ids_start;
199                     $ids_start++;
200                     if($ids_start>255){
201                         $ids_start=0;
202                         $font_start++;
203                     }
204                 }
205                 print "{\\fontencoding{OT1}\\fontfamily{" .
206                     sprintf("chise%03d",$ids{$char}[0]) .
207                     "}\\selectfont\\char$ids{$char}[1]}",&add_break($i);
208                 next;
209             }else{
210                 print &get_macro_for_ids(&get_ids($char)),&add_break($i);
211             }
212         }
213     }
214 }
215
216 print IDSDATA 'use utf8;',"\n";
217 foreach $ids (keys %ids){
218     print IDSDATA '$ids{\'',$ids,'\'}=[',join ",",@{$ids{$ids}},"];\n" if($perl56);
219     print IDSDATA '$ids{\'',encode('utf8',$ids),'\'}=[',join ",",@{$ids{$ids}},"];\n" if($perl58);
220 }
221 print IDSDATA '$font_start=',$font_start,";\n";
222 print IDSDATA '$ids_start=',$ids_start,";\n";
223 print IDSDATA "1;";
224 flock(IDSDATA,LOCK_UN);
225
226 exit 0;
227
228 sub add_break{
229     my($i)=@_;
230
231     if($i<($#chars-1)){
232         if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
233            and($chars[$i+2]=~m/[$strictly_forbidden_before]/o)){
234             return "\\CJKunbreakablekernone ";
235         }elsif($opt_protrude){
236             if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
237                and($chars[$i+2]=~m/[^$strictly_forbidden_before]/o)){
238                 return "\\CJKunbreakablekernone \\CJKprotrude ";
239             }
240         }
241     }
242     if(($i<$#chars)
243        and($chars[$i+1]=~m/[$strictly_forbidden_before]/o)){
244         return "\\CJKunbreakablekernone ";
245     }
246     if($chars[$i]=~m/[$strictly_forbidden_after]/o){
247         return "\\CJKunbreakablekernone ";
248     }
249     if(($i<$#chars)
250        and($chars[$i+1]=~m/[$forbidden_before]/o)){
251         return "\\CJKunbreakablekerntwo ";
252
253     }
254     if($chars[$i]=~m/[$forbidden_after]/o){
255         return "\\CJKunbreakablekerntwo ";
256     }
257     if(($i<$#chars)
258        and($chars[$i+1]=~m/[$slightly_forbidden_before]/o)){
259         return "\\CJKunbreakablekernthree ";
260     }
261     if($chars[$i]=~m/[$slightly_forbidden_after]/o){
262         return "\\CJKunbreakablekernthree ";
263     }
264     if($chars[$i]=~m/[$asian]/o){
265         return "\\CJKbreakablekern ";
266     }
267     if(($i<$#chars)and($chars[$i+1]=~m/[$asian]/o)){
268         return "\\CJKbreakablekern ";
269     }
270 }
271
272 sub latin_parse{
273     # arguments: none
274     # return: string for output with TeX macro.
275     my($char_id);
276     my $out_str=$chars[$i];
277     $i++;
278     while($i<=$#chars){
279         $char_id=unpack("U",$chars[$i]);
280         if($char_id<=0xff){
281             $out_str.=pack("U",$char_id);
282         }else{
283             $i--;
284             last;
285         }
286         $i++;
287     }
288     return '{\normalfont {'.$out_str.'}}';
289 }
290
291 sub ids_parse{
292     # arguments: none
293     # return: character for output,
294     #          TeX macro for ids,
295     #          or GETA character if ids is invalid.
296     my($ids,$ids_argc)=&ids_rest("",0,$chars[$i]);
297
298     while($ids_argc>0){
299         # We are in IDS.
300         $i++;
301         if($i>$#chars){
302             print STDERR "IDS parse error: $ids\n";
303 #           return pack("U",0xfffd);
304             return pack("U",0x3013);
305         }
306
307         ($ids,$ids_argc)=&ids_rest($ids,$ids_argc,$chars[$i]);
308         if($ids_argc==0){
309             if(($char_id=&get_char_id_for_ids($ids))
310                and($out_char=&get_output_char($char_id,$out_cs))){
311                 return $out_char;
312             }else{
313                 return &get_macro_for_ids($ids) if($perl56);
314                 return encode('utf8', &get_macro_for_ids($ids)) if($perl58);
315             }
316         }
317     }
318 }
319
320 sub ids_rest{
321     # arguments: <ids>, <rest number of arguments for ids>, <character>
322     # return: ids and rest number of arguments for ids.
323     my($ids,$ids_argc,$char)=@_;
324     my($argc);
325     $argc=&ids_argc($char);
326     if($argc){
327         $ids_argc+= $ids_argc==0 ? $argc : $argc-1;
328     }else{
329         $ids_argc--;
330     }
331     $ids.=$char if($perl56);
332     $ids.=encode('utf8',$char) if($perl58);
333     return ($ids,$ids_argc);
334 }
335
336 sub get_macro_for_ids{
337     # argument: <ids>
338     # return: TeX macro for ids
339     #          or GETA character if ids is invalid for KAGE.
340     my($ids)=@_;
341     $ids=&normalize_ids($ids,"UniJIS");
342 #    return pack("U",0xfffd) if(($ids!~/[$idc]/)
343     return pack("U",0x3013) if(($ids!~/[$idc]/)
344                                or($ids=~/[\x{10000}-]/));
345                     #irregular for KAGE.
346     if(not defined($ids{$ids}) and $ids{$ids}[1]>=0){
347         $ids{$ids}[0]=$font_start;
348         $ids{$ids}[1]=$ids_start;
349         $ids_start++;
350     }
351     if($ids_start>255){
352         $ids_start=0;
353         $font_start++;
354     }
355     return "{\\fontencoding{OT1}\\fontfamily{"
356         .sprintf("chise%03d",$ids{$ids}[0])
357         ."}\\selectfont\\char$ids{$ids}[1]}";
358 }
359
360 sub normalize_ids{
361     # argument: <ids>, <output coding system>
362     # return: ids or GETA character if ids is invalid for KAGE.
363     my($ids,$out_cs)=@_;
364     $ids = decode('utf8', $ids) if $perl58;
365     $out_cs=~s/Uni(.+)/'ucs@'.lc($1)/e;
366
367     my $output_ids="";
368     my($char,$char_id,$output_char_id);
369     while($ids=~m/(.)/g){
370         $char=$1;
371         $char_id=unpack("U",$char);
372         if($char=~/[$idc]/){
373             $output_ids.=$char;
374         }elsif($output_char_id=&get_char_attribute($char,"=$out_cs")){
375             $output_ids.=pack("U",$output_char_id);
376         }elsif($output_char_id=&get_char_attribute($char,"=ucs")){
377             $output_ids.=pack("U",$output_char_id);
378         }else{
379 #           return pack("U",0xfffd);
380             return pack("U",0x3013);
381         }
382     }
383     return $output_ids;
384 }
385
386 sub get_output_char{
387     # argument: <char-id>
388     # return: character in output coding system or TeX macro.
389     my($char_id,$out_cs)=@_;
390     my($char,$out_char_id,$suffix);
391     my($gt,$hzk,$cdp);
392
393     $char=pack('U',$char_id);
394
395     if($out_cs eq 'UniJIS'
396        and &get_char_attribute($char,"adobe-unijis-utf16-h")){
397         if($out_char_id=&get_char_attribute($char,'=ucs@jis')
398            or $out_char_id=&get_char_attribute($char,'=ucs')
399            or $out_char_id=&get_char_attribute($char,'=>ucs@jis')
400            or $out_char_id=&get_char_attribute($char,'=>ucs')
401            ){
402             return pack("U",$out_char_id);
403         }
404     }elsif($out_cs eq 'UniGB'
405            and &get_char_attribute($char,"adobe-unigb-ucs2-h")){
406         if($out_char_id=&get_char_attribute($char,'=ucs@gb')
407            or $out_char_id=&get_char_attribute($char,'=ucs')
408            or $out_char_id=&get_char_attribute($char,'=>ucs@gb')
409            or $out_char_id=&get_char_attribute($char,'=>ucs')
410            ){
411             return pack("U",$out_char_id);
412         }
413     }elsif($out_cs eq 'UniCNS'
414            and &get_char_attribute($char,"adobe-unicns-ucs2-h")){
415         if($out_char_id=&get_char_attribute($char,'=ucs@cns')
416            or $out_char_id=&get_char_attribute($char,'=ucs')
417            or $out_char_id=&get_char_attribute($char,'=>ucs@cns')
418            or $out_char_id=&get_char_attribute($char,'=>ucs')
419            ){
420             return pack("U",$out_char_id);
421         }
422     }elsif($out_cs eq 'UniKS'
423            and &get_char_attribute($char,"adobe-uniks-ucs2-h")){
424         if($out_char_id=&get_char_attribute($char,'=ucs@ks')
425            or $out_char_id=&get_char_attribute($char,'=ucs')
426            or $out_char_id=&get_char_attribute($char,'=>ucs@ks')
427            or $out_char_id=&get_char_attribute($char,'=>ucs')
428            ){
429             return pack("U",$out_char_id);
430         }
431     }elsif($out_cs eq 'UniMulti'){
432         foreach $out_cs (@order){
433     
434             if($out_cs eq 'UniJIS'
435                and &get_char_attribute($char,"adobe-unijis-utf16-h")){
436                 if($out_char_id=&get_char_attribute($char,'=ucs@jis')
437                    or $out_char_id=&get_char_attribute($char,'=ucs')
438                    or $out_char_id=&get_char_attribute($char,'=>ucs@jis')
439                    or $out_char_id=&get_char_attribute($char,'=>ucs')
440                    ){
441                     return '{\selectjisfont\char'.$out_char_id.'}';
442                 }
443             }elsif($out_cs eq 'UniGB'
444                    and &get_char_attribute($char,"adobe-unigb-ucs2-h")){
445                 if($out_char_id=&get_char_attribute($char,'=ucs@gb')
446                    or $out_char_id=&get_char_attribute($char,'=ucs')
447                    or $out_char_id=&get_char_attribute($char,'=>ucs@gb')
448                    or $out_char_id=&get_char_attribute($char,'=>ucs')
449                    ){
450                     return '{\selectgbsfont\char'.$out_char_id.'}';
451                 }
452             }elsif($out_cs eq 'UniCNS'
453                    and &get_char_attribute($char,"adobe-unicns-ucs2-h")){
454                 if($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@cns')
457                    or $out_char_id=&get_char_attribute($char,'=>ucs')
458                    ){
459                     return '{\selectcnsfont\char'.$out_char_id.'}';
460                 }
461             }elsif($out_cs eq 'UniKS'
462                    and &get_char_attribute($char,"adobe-uniks-ucs2-h")){
463                 if($out_char_id=&get_char_attribute($char,'=ucs@ks')
464                    or $out_char_id=&get_char_attribute($char,'=ucs')
465                    or $out_char_id=&get_char_attribute($char,'=>ucs@ks')
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_id));
472             }elsif($out_cs eq 'HZK'){
473                 return $hzk if($hzk=&get_macro_for_HZK($char_id));
474             }elsif($out_cs eq 'CDP'){
475                 return $cdp if($cdp=&get_macro_for_CDP($char_id));
476             }
477         }
478     }
479     return undef;
480 }
481
482 sub get_ids{
483     # argument: <character>
484     # return: ids
485     my($char)=@_;
486     my $ids="";
487     $ids=&get_char_attribute($char,"ids-aggregated")
488         or &get_char_attribute($char,"ids");
489 #       or &get_char_attribute($char,"ideographic-structure");
490     $ids=decode('utf8', $ids) if($perl58);
491 #    $ids=~s/[? ()]//g;
492     return $ids;
493 }
494
495 sub get_char_id_for_ids{
496     # argument: <ideographic description sequence>
497     # return: char-id
498     my($ids)=@_;
499     my($output_char);
500     $ids=decode('utf8', $ids) if($perl58);
501
502     if(($output_char)=&get_chars_matching("ids",$ids)){
503         return unpack("U",$output_char);
504     }else{
505         return undef;
506     }
507 }
508
509 sub get_char_id{
510     # argument: <char-id>, <input coding system>
511     # return:   char-id.
512     my($char_id,$in_cs)=@_;
513     my($output_char);
514
515     return $char_id if($in_cs eq 'ucs@mcs');
516
517     if(($output_char)=&get_chars_matching("=$in_cs",$char_id)){
518         return unpack("U",$output_char);
519     }else{
520         return $char_id;
521     }
522 }
523
524 sub get_macro_for_GT{
525     # argument: <char-id>
526     # return: TeX macro for GT fonts.
527     my($char_id)=@_;
528     my($char,$gt,$GT);
529     $char=pack("U",$char_id);
530     foreach (@GT){
531         if($gt=&get_char_attribute($char,$_)){
532             m/gt\-pj\-(\d+)/ and $GT=$1;
533             last;
534         }
535     }
536     if($gt){
537         return "{\\fontencoding{OT1}\\fontfamily{"
538             .sprintf("gt%02d",$GT)
539             ."}\\selectfont\\char".($gt|0x8080)."}";
540     }else{
541         return undef;
542     }
543 }
544
545 sub get_macro_for_HZK{
546     my($char_id)=@_;
547     my($char,$hzk,$HZK);
548     $char=pack("U",$char_id);
549     foreach (@HZK){
550         if($hzk=&get_char_attribute($char,$_)){
551             m/hanziku\-(\d+)/ and $HZK=$1;
552             last;
553         }
554     }
555     if($hzk){
556         return "{\\fontencoding{OT1}\\fontfamily{".sprintf("hzk%02d",$HZK)."}\\selectfont\\char".$hzk."}";
557     }else{
558         return undef;
559     }
560 }
561
562 sub get_macro_for_CDP{
563     my($char_id)=@_;
564     my($char,$cdp);
565     $char=pack("U",$char_id);
566     foreach (@CDP){
567         if($cdp=&get_char_attribute($char,$_)){
568             last;
569         }
570     }
571     if($cdp){
572         return "{\\fontencoding{OT1}\\fontfamily{cdp}\\selectfont\\char".$cdp."}";
573     }else{
574         return undef;
575     }
576 }