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