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