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