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