fix spacing in Hangul.
[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",0x201b)/geo;# SINGLE HIGH-REVERSED-9 QUOTATION MARK
190     s/\'\'/pack("U",0x201d)/geo;# RIGHT DOUBLE QUOTATION MARK
191     s/\'/pack("U",0x2019)/geo;# RIGHT DOUBLE QUOTATION MARK
192     @chars=split(//);
193   CHAR:
194     for($i=0;$i<=$#chars;$i++){
195         $char=$chars[$i];
196         $char_id=unpack("U",$char);
197
198         if($char_id<=0x20){
199             # add quarter space between asian and non-asian.
200             if($i>0 and $i<$#chars){
201                 if(($chars[$i-1]=~/[$asian]/
202                     and $chars[$i+1]=~/[^$asian]/
203                       and $chars[$i+1]=~/[$strictly_forbidden_before$slightly_forbidden_before]/)
204                    or($chars[$i-1]=~/[^$asian]/
205                       and $chars[$i+1]=~/[$asian]/
206                       and $chars[$i-1]=~/[$strictly_forbidden_after$slightly_forbidden_after]/)
207                    ){
208                     print '';
209                 }elsif(($chars[$i-1]=~/[$asian]/
210                         and $chars[$i+1]=~/[^$asian]/)){
211                     print '\unskip\kern.25ex';
212                 }elsif($chars[$i-1]=~/[^$asian]/
213                        and $chars[$i+1]=~/[$asian]/){
214                     print '\kern.25ex';
215                 }else{
216                     print ' ';
217                 }
218             }
219             next CHAR;
220         }elsif($char=~m/($tex_meta_re)/o){
221             print $tex_meta{$1};
222             next CHAR;
223         }elsif(($char_id>0x20 and $char_id<=0x021f)
224                # Basic Latin
225                # Latin-1 Supplement
226                # Latin Extended-A
227                # Latin Extended-B (not all)
228                or($char_id>=0x0250 and $char_id<=0x02af)
229                # IPA Extensions
230                or($char_id>=0x0300 and $char_id<=0x033f)
231                or($char_id>=0x0360 and $char_id<=0x036f)
232                # Combining Diacritical Marks
233                or($char_id>=0x1e00 and $char_id<=0x1eff)
234                # Latin Extended Additional
235                or($char_id>=0x0370 and $char_id<=0x03ff)
236                # Greek and Coptic
237                or($char_id>=0x0400 and $char_id<=0x04ff)
238                # Cyrillic
239                or($char_id>=0x0530 and $char_id<=0x058f)
240                # Armenian
241                or($char_id>=0x2010 and $char_id<=0x2046)
242                # General Punctuation (partial)
243                ){
244             print &latin_parse();
245             next CHAR;
246         }elsif($char_id>=0x2ff0 and $char_id<=0x2fff){
247             # Ideographic Description Characters
248             print &ids_parse();
249             next CHAR;
250         }else{
251             if(($out_char=&get_output_char($char,$out_cs))){
252                 print $out_char,&add_break($i);
253             }else{
254                 if($opt_allow_unify){
255                     @chars_unified=&get_chars_unified($char);
256                     if(@chars_unified>0){
257                         foreach $char_unified (@chars_unified){
258                             if(($out_char
259                                 =&get_output_char($char_unified,$out_cs))){
260                                 print $out_char,&add_break($i);
261                                 next CHAR;
262                             }
263                         }
264                     }
265                 }
266                 if($opt_use_kage_for_Ext_B){
267                     if($char_id >= 0x20000 && $char_id <=0x2a6df){
268                         # CJK Unified Ideographs Extension B
269                         if(not defined($ids{$char}) and $ids{$char}[1]>=0){
270                             $ids{$char}[0]=$font_start;
271                             $ids{$char}[1]=$ids_start;
272                             $ids_start++;
273                             if($ids_start>255){
274                                 $ids_start=0;
275                                 $font_start++;
276                             }
277                         }
278                         print "{\\fontencoding{OT1}\\fontfamily{",
279                             sprintf("chise%03d",$ids{$char}[0]) ,
280                                 "}\\selectfont\\char$ids{$char}[1]}",&add_break($i);
281                         next CHAR;
282                     }
283                 }
284                 if($ids=&get_ids($char)){
285                     print &get_macro_for_ids($ids),&add_break($i);
286                 }else{
287                     print '\rule{1ex}{1ex}',&add_break($i);
288                 }
289             }
290         }
291     }
292 }
293
294 print IDSDATA 'use utf8;',"\n";
295 foreach $ids (keys %ids){
296     print IDSDATA '$ids{\'',$ids,'\'}='
297     ,'[',join ",",@{$ids{$ids}},"];\n";
298 }
299 print IDSDATA '$font_start=',$font_start,";\n";
300 print IDSDATA '$ids_start=',$ids_start,";\n";
301 print IDSDATA "1;";
302 flock(IDSDATA,LOCK_UN);
303
304 exit 0;
305
306 sub de_tex_er{
307     my($er)=@_;
308     my($prefix,$suffix);
309     my($output_char,$atr,$value);
310     $er=~/^(amp)(.*)(;)$/
311         and $prefix=$1,$er=$2,$suffix=$3;
312     $prefix or $prefix="",$suffix or $suffix="";
313     if($er=~/^U[\+|\-]([a-fA-F\d]+)/){
314         $output_char=pack("U",hex($1));
315     }elsif($er=~/^(?:I\-)?($er_prefix_re)\-?([0-9a-fA-F]+)$/){
316         ($atr,$value)=($1,$2);
317         unless($er_alias{$atr}=~/daikanwa|gt/){
318             $value=hex($value);
319         }
320         ($output_char)=&get_chars_matching($er_alias{$atr},$value);
321     }
322     if($output_char){
323         return $output_char;
324     }else{
325         return $prefix.$er.$suffix;
326     }
327 }
328
329 sub add_break{
330     my($i)=@_;
331
332     if($i<($#chars-1)){
333         if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
334            and($chars[$i+2]=~m/[$strictly_forbidden_before]/o)){
335             return "\\CJKunbreakablekernone{}";
336         }elsif($opt_protrude){
337             if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
338                and($chars[$i+2]=~m/[^$strictly_forbidden_before]/o)){
339                 return "\\CJKunbreakablekernone \\CJKprotrude{}";
340             }
341         }
342     }
343     if(($i<$#chars)
344        and($chars[$i+1]eq" ")){
345        # preserve space.
346         return "";
347     }
348     if(($i<$#chars)
349        and($chars[$i+1]=~m/[$strictly_forbidden_before]/o)){
350         return "\\CJKunbreakablekernone{}";
351     }
352     if($chars[$i]=~m/[$strictly_forbidden_after]/o){
353         return "\\CJKunbreakablekernone{}";
354     }
355     if(($i<$#chars)
356        and($chars[$i+1]=~m/[$forbidden_before]/o)){
357         return "\\CJKunbreakablekerntwo{}";
358
359     }
360     if($chars[$i]=~m/[$forbidden_after]/o){
361         return "\\CJKunbreakablekerntwo{}";
362     }
363     if(($i<$#chars)
364        and($chars[$i+1]=~m/[$slightly_forbidden_before]/o)){
365         return "\\CJKunbreakablekernthree{}";
366     }
367     if($chars[$i]=~m/[$slightly_forbidden_after]/o){
368         return "\\CJKunbreakablekernthree{}";
369     }
370     if($chars[$i]=~m/[$asian]/o){
371         return "\\CJKbreakablekern{}";
372     }
373     if(($i<$#chars)and($chars[$i+1]=~m/[$asian]/o)){
374         return "\\CJKbreakablekern{}";
375     }
376 }
377
378 sub latin_parse{
379     # arguments: none
380     # return: string for output with TeX macro.
381     my($char_id);
382     my $out_str="";
383     while($i<=$#chars){
384         $char_id=unpack("U",$chars[$i]);
385         if(($char_id>0x20 and $char_id<=0x021f)
386            or($char_id>=0x0250 and $char_id<=0x02af)# IPA Extensions
387            or($char_id>=0x0300 and $char_id<=0x033f)# Combining Diacritical Marks
388            or($char_id>=0x0360 and $char_id<=0x036f)
389            or($char_id>=0x0370 and $char_id<=0x03ff)# Greek and Coptic
390            or($char_id>=0x0400 and $char_id<=0x04ff)# Cyrillic
391            or($char_id>=0x0530 and $char_id<=0x058f)# Armenian
392            ){
393             $out_str.=$chars[$i];
394         }elsif($char_id>=0x1e00 and $char_id<=0x1eff){
395             # Latin Extended Additional
396             # 0x1e00 -> 0x0600, etc.
397             $out_str.=pack("U",$char_id-0x1800);
398         }elsif($char_id>=0x2010 and $char_id<=0x2046){
399             # General Punctuation (partial)
400             $out_str.=pack("U",$char_id-0x1000);
401         }else{
402             $i--;
403             last;
404         }
405         $i++;
406     }
407     return '{\fontencoding{OT1}\fontfamily{omlgc}\selectfont '.$out_str.'}';###UT1?
408 }
409
410 sub ids_parse{
411     # arguments: none
412     # return: character for output,
413     #          TeX macro for ids,
414     #          or GETA character if ids is invalid.
415     my($ids,$ids_argc)=&ids_rest("",0,$chars[$i]);
416
417     while($ids_argc>0){
418         # We are in IDS.
419         $i++;
420         if($i>$#chars){
421             print STDERR "IDS parse error: $ids\n";
422             return $geta;
423         }
424
425         ($ids,$ids_argc)=&ids_rest($ids,$ids_argc,$chars[$i]);
426         if($ids_argc==0){
427             if(($char=&get_char_for_ids($ids))
428                and($out_char=&get_output_char($char,$out_cs))){
429                 return $out_char;
430             }else{
431                 return &get_macro_for_ids($ids);
432             }
433         }
434     }
435 }
436
437 sub ids_rest{
438     # arguments: <ids>, <rest number of arguments for ids>, <character>
439     # return: ids and rest number of arguments for ids.
440     my($ids,$ids_argc,$char)=@_;
441     my($argc);
442     $argc=&ids_argc($char);
443     if($argc){
444         $ids_argc+=$ids_argc==0?$argc:$argc-1;
445     }else{
446         $ids_argc--;
447     }
448     $ids.=$char;
449     return ($ids,$ids_argc);
450 }
451
452 sub get_macro_for_ids{
453     # argument: <ids>
454     # return: TeX macro for ids
455     #          or GETA character if ids is invalid for KAGE.
456     my($ids)=@_;
457     # $ids=&normalize_ids($ids,"UniJIS");
458     return $geta if(($ids!~/[$idc]/)
459                     or($ids=~/[\x{10000}-]/));
460                     #irregular for KAGE.
461     if(not defined($ids{$ids})){
462         $ids{$ids}[0]=$font_start;
463         $ids{$ids}[1]=$ids_start;
464         $ids_start++;
465     }
466     if($ids_start>255){
467         $ids_start=0;
468         $font_start++;
469     }
470     return "{\\fontencoding{OT1}\\fontfamily{"
471         .sprintf("chise%03d",$ids{$ids}[0])
472         ."}\\selectfont\\char$ids{$ids}[1]}";
473 }
474
475 sub normalize_ids{
476     # argument: <ids>, <output coding system>
477     # return: ids or GETA character if ids is invalid for KAGE.
478     my($ids,$out_cs)=@_;
479     $out_cs=~s/Uni(.+)/'ucs@'.lc($1)/e;
480
481     my $output_ids="";
482     my($char,$output_char_id);
483     while($ids=~m/(.)/g){
484         $char=$1;
485         if($char=~/[$idc]/){
486             $output_ids.=$char;
487         }elsif($output_char_id=&get_char_attribute($char,"=$out_cs")
488            or $output_char_id=&get_char_attribute($char,"=ucs")
489            or $output_char_id=&get_char_attribute($char,"=>$out_cs")
490            or $output_char_id=&get_char_attribute($char,"=>ucs")
491            or $output_char_id=&get_char_attribute($char,"=>ucs*")
492               ){
493             $output_ids.=pack("U",$output_char_id);
494         }else{
495             return $geta;
496         }
497     }
498     return $output_ids;
499 }
500
501 sub get_output_char{
502     # argument: <char>
503     # return: character in output coding system or TeX macro or undef.
504     my($char,$out_cs)=@_;
505     my($out_char_id,$suffix);
506     my($gt,$hzk,$cdp);
507
508     foreach $out_cs (@{$order{$out_cs}}){
509         if($out_cs eq 'UniJIS'
510            and &get_char_attribute($char,"vnd-adobe-cid-unijis-utf16-h")){
511             if($out_char_id=&get_char_attribute($char,'=ucs@jis')
512                or $out_char_id=&get_char_attribute($char,'=ucs')
513                or $out_char_id=&get_char_attribute($char,'=>ucs@jis')
514                or $out_char_id=&get_char_attribute($char,'=>ucs')
515                or $out_char_id=&get_char_attribute($char,'=>ucs*')
516                ){
517                 return "{\\selectjisfont\\char$out_char_id}";
518             }
519         }elsif($out_cs eq 'UniGB'
520                and &get_char_attribute($char,"vnd-adobe-cid-unigb-ucs2-h")){
521             if($out_char_id=&get_char_attribute($char,'=ucs@gb')
522                or $out_char_id=&get_char_attribute($char,'=ucs')
523                or $out_char_id=&get_char_attribute($char,'=>ucs@gb')
524                or $out_char_id=&get_char_attribute($char,'=>ucs')
525                or $out_char_id=&get_char_attribute($char,'=>ucs*')
526                ){
527                 return "{\\selectgbsfont\\char$out_char_id}";
528             }
529         }elsif($out_cs eq 'UniCNS'
530                and &get_char_attribute($char,"vnd-adobe-cid-unicns-ucs2-h")){
531             if($out_char_id=&get_char_attribute($char,'=ucs@cns')
532                or $out_char_id=&get_char_attribute($char,'=ucs')
533                or $out_char_id=&get_char_attribute($char,'=>ucs@cns')
534                or $out_char_id=&get_char_attribute($char,'=>ucs')
535                or $out_char_id=&get_char_attribute($char,'=>ucs*')
536                ){
537                 return "{\\selectcnsfont\\char$out_char_id}";
538             }
539         }elsif($out_cs eq 'UniKS'
540                and &get_char_attribute($char,"vnd-adobe-cid-uniks-ucs2-h")){
541             if($out_char_id=&get_char_attribute($char,'=ucs@ks')
542                or $out_char_id=&get_char_attribute($char,'=ucs')
543                or $out_char_id=&get_char_attribute($char,'=>ucs@ks')
544                or $out_char_id=&get_char_attribute($char,'=>ucs')
545                or $out_char_id=&get_char_attribute($char,'=>ucs*')
546                ){
547                 return "{\\selectksxfont\\char$out_char_id}";
548             }
549         }elsif($out_cs eq 'GT'){
550             return $gt if($gt=&get_macro_for_GT($char));
551         }elsif($out_cs eq 'HZK'){
552             return $hzk if($hzk=&get_macro_for_HZK($char));
553         }elsif($out_cs eq 'CDP'){
554             return $cdp if($cdp=&get_macro_for_CDP($char));
555         }
556     }
557     return undef;
558 }
559
560 sub get_ids{
561     # argument: <character>
562     # return: ids
563     my($char)=@_;
564     my $ids="";
565     $ids=&get_char_attribute($char,"ids-aggregated")
566         or $ids=&get_char_attribute($char,"ids");
567 #    $ids=&get_char_attribute($char,"ids-decomposed")
568 #       or $ids=&get_char_attribute($char,"ids");
569 #         or $ids=&get_char_attribute($char,"ideographic-structure");
570 #    $ids=~s/[? ()]//g;
571     return $ids;
572 }
573
574 sub get_char_for_ids{
575     # argument: <ideographic description sequence>
576     # return: char or undef.
577     my($ids)=@_;
578     my($output_char);
579
580     if(($output_char)=&get_chars_matching("ids",$ids)){
581         return $output_char;
582     }else{
583         return undef;
584     }
585 }
586
587 sub get_char_in_mcs{
588     # argument: <char>, <input coding system>
589     # return:   char in ucs@mcs.
590     my($char,$in_cs)=@_;
591     my($output_char);
592
593     return $char if($in_cs eq 'ucs@mcs');
594
595     if(($output_char)=&get_chars_matching("=$in_cs",unpack("U",$char))){
596         return $output_char;
597     }else{
598         return $char;
599     }
600 }
601
602 sub get_chars_unified{
603     my($char)=@_;
604     my($chars,$ucs,$char_ucs);
605     my(@chars);
606
607     if($chars=&get_char_attribute($char,'->ucs-unified')){
608         $chars=~s/^\((.*)\)$/$1/;
609         return (split(/\s*\?/,$chars));
610     }elsif($ucs=&get_char_attribute($char,'=>ucs*')
611           or $ucs=&get_char_attribute($char,'=>ucs')){
612         $char_ucs=pack("U",$ucs);
613         if($chars=&get_char_attribute($char_ucs,'->ucs-unified')){
614             $chars=~s/^\((.*)\)$/$1/;
615             @chars=grep {not /^$char$/}
616                 (split(/\s*\?/,$chars));
617             push(@chars,$char_ucs);
618             return @chars;
619         }
620     }
621 }
622
623 sub get_macro_for_GT{
624     # argument: <char>
625     # return: TeX macro for GT fonts or undef.
626     my($char)=@_;
627     my($gt,$GT);
628     foreach (@GT){
629         if($gt=&get_char_attribute($char,$_)){
630             m/gt\-pj\-(\d+)/ and $GT=$1;
631             last;
632         }
633     }
634     if($gt){
635         return "{\\fontencoding{OT1}\\fontfamily{"
636             .sprintf("gt%02d",$GT)
637             ."}\\selectfont\\char".($gt|0x8080)."}";
638     }else{
639         return undef;
640     }
641 }
642
643 sub get_macro_for_HZK{
644     # argument: <char>
645     # return: TeX macro for Hanziku fonts or undef.
646     my($char)=@_;
647     my($hzk,$HZK);
648     foreach (@HZK){
649         if($hzk=&get_char_attribute($char,$_)){
650             m/hanziku\-(\d+)/ and $HZK=$1;
651             last;
652         }
653     }
654     if($hzk){
655         return "{\\fontencoding{OT1}\\fontfamily{"
656             .sprintf("hzk%02d",$HZK)
657             ."}\\selectfont\\char".$hzk."}";
658     }else{
659         return undef;
660     }
661 }
662
663 sub get_macro_for_CDP{
664     # argument: <char>
665     # return: TeX macro for CDP fonts or undef.
666     my($char)=@_;
667     my($cdp,$ucs);
668     foreach (@CDP){
669         if($cdp=&get_char_attribute($char,$_)){
670             last;
671         }
672     }
673     if($cdp){
674         $ucs=&get_char_attribute(&get_chars_matching("=big5-pua",$cdp),"=ucs");
675         if($ucs){
676             return "{\\fontencoding{OT1}\\fontfamily{cdp}\\selectfont\\char$ucs}";
677         }else{
678             print STDERR "This should not happen.\n";
679             print STDERR "ucs code point of CDP: $cdp not found.\n";
680         }
681     }else{
682         return undef;
683     }
684 }