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