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