1 #!/usr/bin/perl -w -CSD
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
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
22 use Chise_utils ':all';
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';
34 $opt_allow_unify=1; # 1=true, 0=false.
35 $opt_protrude=0;# 1=true, 0=false.
37 # currently does not work.
38 $opt_use_kage_for_Ext_B=0;# 1=true, 0=false.
42 my $strictly_forbidden_after = '「【『[(〈“‘‘(〔{《{\[\(\x{3016}{「';
43 # \x{3016} | # white 【
45 my $forbidden_after = "\x{0000}";
48 my $slightly_forbidden_after = '¥¥$$〒♯##¢¢££@@§';
50 # $strictly_forbidden_before
51 # All these characters are allowed to protrude
53 my $strictly_forbidden_before=
54 '!,.:;?、。!,.:;?。\)#}’”〉》」』】〕\x{3017})]}}」\]';
55 ### \x{3017} | # white 】
58 = 'ー々ぁぃぅぇぉゃゅょっゎァィゥェォャュョッヮヵヶ';
60 my $slightly_forbidden_before
61 = '\x{000a}\#\-‐−‰′″℃゛゜ゝゞヽヾ"%-゙゚';
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.
65 my $space = '\x{0020}\x{0009}\x{000A}\x{000C}\x{000D}';
67 my %tex_meta=('#'=>'\#',
68 '$'=>'\\textdollar{}',
71 '{'=>'\\textbraceleft{}',
72 '}'=>'\\textbraceright{}',
73 '\\'=>'\\textbackslash{}',
74 '_'=>'\\textunderscore',
77 my $tex_meta_re=join('|',map {quotemeta($_)} keys %tex_meta);
79 &GetOptions("in=s"=>\$opt_in_cs,
80 "out=s"=>\$opt_out_cs,
84 Usage: $0 -i <input coding system> -o <cmap encoding>
86 Utf8mcs, Utf8cns, Utf8gb, Utf8jis, Utf8ks, Utf8big5
88 UniCNS, UniGB, UniJIS, UniKS, UniMulti
91 if($opt_in_cs or $opt_out_cs){
95 ($in_cs,$out_cs)=($0=~/(Utf8.+)To(\w+)/);
99 # Utf8mcs,Utf8cns,Utf8gb,Utf8jis,Utf8ks,Utf8big5
101 # UniCNS,UniGB,UniJIS,UniKS,UniMulti
103 $in_cs=~s/Utf8/ucs\@/;
106 or not defined($in_cs)
107 or not defined($out_cs)){
112 $omegadb_path=~s!/$!!;
114 $idsdata_file="$omegadb_path/idsdata.pl";
118 if(-e $idsdata_file){
119 open(IDSDATA,"+<:utf8",$idsdata_file) or die;
120 flock(IDSDATA,LOCK_EX);
126 # require $idsdata_file;
128 open(IDSDATA,">:utf8",$idsdata_file) or die;
129 flock(IDSDATA,LOCK_EX);
136 #$geta=pack("U",0x3013);
137 $geta=pack("U",0xfffd);
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",
143 #,"=gt-pj-k1","=gt-pj-k2"
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");
150 %order_map=('c'=>'UniCNS',
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}));
165 print STDERR "Invalid order for $out_cs!\n";
174 s/([$asian])\s+([$asian])/$1$2/g unless($out_cs eq 'UniKS');
175 s/([$asian])\s*([^$asian])/$1 $2/g;
176 s/([^$asian])\s*([$asian])/$1 $2/g;
177 if($in_cs ne 'ucs@mcs'){
178 s/(.)/&get_char_in_mcs($1,$in_cs)/ge;
180 s/(amp.+?;)/&de_tex_er($1)/ge;
181 # s/(&.+?;)/&de_tex_er($1)/ge;
184 for($i=0;$i<=$#chars;$i++){
186 $char_id=unpack("U",$char);
189 # add quarter space between asian and non-asian.
190 if($i>0 and $i<$#chars){
191 if((($chars[$i-1]=~/[$asian]/)
192 and($chars[$i+1]=~/[^$asian]/))
193 or(($chars[$i-1]=~/[^$asian]/)
194 and($chars[$i+1]=~/[$asian]/))){
195 print '{\selectjisfont\hspace{.25ex}}';
201 }elsif($char=~m/($tex_meta_re)/o){
204 }elsif(($char_id>0x20 and $char_id<=0x021f)
208 # Latin Extended-B (not all)
209 or($char_id>=0x0250 and $char_id<=0x02af)
211 or($char_id>=0x0300 and $char_id<=0x033f)
212 or($char_id>=0x0360 and $char_id<=0x036f)
213 # Combining Diacritical Marks
214 or($char_id>=0x1e00 and $char_id<=0x1eff)
215 # Latin Extended Additional
216 or($char_id>=0x0370 and $char_id<=0x03ff)
218 or($char_id>=0x0400 and $char_id<=0x04ff)
220 or($char_id>=0x0530 and $char_id<=0x058f)
223 print &latin_parse();
225 }elsif($char_id>=0x2ff0 and $char_id<=0x2fff){
226 # Ideographic Description Characters
230 if(($out_char=&get_output_char($char,$out_cs))){
231 print $out_char,&add_break($i);
233 if($opt_allow_unify){
234 @chars_unified=&get_chars_unified($char);
235 if(@chars_unified>0){
236 foreach $char_unified (@chars_unified){
238 =&get_output_char($char_unified,$out_cs))){
239 print $out_char,&add_break($i);
245 if($opt_use_kage_for_Ext_B){
246 if($char_id >= 0x20000 && $char_id <=0x2a6df){
247 # CJK Unified Ideographs Extension B
248 if(not defined($ids{$char}) and $ids{$char}[1]>=0){
249 $ids{$char}[0]=$font_start;
250 $ids{$char}[1]=$ids_start;
257 print "{\\fontencoding{OT1}\\fontfamily{" .
258 sprintf("chise%03d",$ids{$char}[0]) .
259 "}\\selectfont\\char$ids{$char}[1]}",&add_break($i);
263 if($ids=&get_ids($char)){
264 print &get_macro_for_ids($ids),&add_break($i);
266 print '\rule{1ex}{1ex}',&add_break($i);
273 print IDSDATA 'use utf8;',"\n";
274 foreach $ids (keys %ids){
275 print IDSDATA '$ids{\'',$ids,'\'}='
276 ,'[',join ",",@{$ids{$ids}},"];\n";
278 print IDSDATA '$font_start=',$font_start,";\n";
279 print IDSDATA '$ids_start=',$ids_start,";\n";
281 flock(IDSDATA,LOCK_UN);
288 my($output_char,$atr,$value);
289 $er=~/^(amp)(.*)(;)$/
290 and $prefix=$1,$er=$2,$suffix=$3;
291 $prefix or $prefix="",$suffix or $suffix="";
292 if($er=~/^U[\+|\-]([a-fA-F\d]+)/){
293 $output_char=pack("U",hex($1));
294 }elsif($er=~/^(?:I\-)?($er_prefix_re)\-?([0-9a-fA-F]+)$/){
295 ($atr,$value)=($1,$2);
296 unless($er_alias{$atr}=~/daikanwa|gt/){
299 ($output_char)=&get_chars_matching($er_alias{$atr},$value);
304 return $prefix.$er.$suffix;
312 if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
313 and($chars[$i+2]=~m/[$strictly_forbidden_before]/o)){
314 return "\\CJKunbreakablekernone ";
315 }elsif($opt_protrude){
316 if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
317 and($chars[$i+2]=~m/[^$strictly_forbidden_before]/o)){
318 return "\\CJKunbreakablekernone \\CJKprotrude ";
323 and($chars[$i+1]eq" ")){
328 and($chars[$i+1]=~m/[$strictly_forbidden_before]/o)){
329 return "\\CJKunbreakablekernone ";
331 if($chars[$i]=~m/[$strictly_forbidden_after]/o){
332 return "\\CJKunbreakablekernone ";
335 and($chars[$i+1]=~m/[$forbidden_before]/o)){
336 return "\\CJKunbreakablekerntwo ";
339 if($chars[$i]=~m/[$forbidden_after]/o){
340 return "\\CJKunbreakablekerntwo ";
343 and($chars[$i+1]=~m/[$slightly_forbidden_before]/o)){
344 return "\\CJKunbreakablekernthree ";
346 if($chars[$i]=~m/[$slightly_forbidden_after]/o){
347 return "\\CJKunbreakablekernthree ";
349 if($chars[$i]=~m/[$asian]/o){
350 return "\\CJKbreakablekern ";
352 if(($i<$#chars)and($chars[$i+1]=~m/[$asian]/o)){
353 return "\\CJKbreakablekern ";
359 # return: string for output with TeX macro.
361 my $out_str=$chars[$i];
364 $char_id=unpack("U",$chars[$i]);
365 if(($char_id>0x20 and $char_id<=0x021f)
366 or($char_id>=0x0250 and $char_id<=0x02af)# IPA Extensions
367 or($char_id>=0x0300 and $char_id<=0x033f)# Combining Diacritical Marks
368 or($char_id>=0x0360 and $char_id<=0x036f)
369 or($char_id>=0x0370 and $char_id<=0x03ff)# Greek and Coptic
370 or($char_id>=0x0400 and $char_id<=0x04ff)# Cyrillic
371 or($char_id>=0x0530 and $char_id<=0x058f)# Armenian
373 $out_str.=$chars[$i];
374 }elsif($char_id>=0x1e00 and $char_id<=0x1eff){
375 # Latin Extended Additional
376 # 0x1e00 -> 0x0600, etc.
377 $out_str.=pack("U",$char_id-0x1800);
384 return '{\fontencoding{UT1}\fontfamily{omlgc}\selectfont '.$out_str.'}';
389 # return: character for output,
391 # or GETA character if ids is invalid.
392 my($ids,$ids_argc)=&ids_rest("",0,$chars[$i]);
398 print STDERR "IDS parse error: $ids\n";
402 ($ids,$ids_argc)=&ids_rest($ids,$ids_argc,$chars[$i]);
404 if(($char=&get_char_for_ids($ids))
405 and($out_char=&get_output_char($char,$out_cs))){
408 return &get_macro_for_ids($ids);
415 # arguments: <ids>, <rest number of arguments for ids>, <character>
416 # return: ids and rest number of arguments for ids.
417 my($ids,$ids_argc,$char)=@_;
419 $argc=&ids_argc($char);
421 $ids_argc+=$ids_argc==0?$argc:$argc-1;
426 return ($ids,$ids_argc);
429 sub get_macro_for_ids{
431 # return: TeX macro for ids
432 # or GETA character if ids is invalid for KAGE.
434 # $ids=&normalize_ids($ids,"UniJIS");
435 return $geta if(($ids!~/[$idc]/)
436 or($ids=~/[\x{10000}-]/));
438 if(not defined($ids{$ids})){
439 $ids{$ids}[0]=$font_start;
440 $ids{$ids}[1]=$ids_start;
447 return "{\\fontencoding{OT1}\\fontfamily{"
448 .sprintf("chise%03d",$ids{$ids}[0])
449 ."}\\selectfont\\char$ids{$ids}[1]}";
453 # argument: <ids>, <output coding system>
454 # return: ids or GETA character if ids is invalid for KAGE.
456 $out_cs=~s/Uni(.+)/'ucs@'.lc($1)/e;
459 my($char,$output_char_id);
460 while($ids=~m/(.)/g){
464 }elsif($output_char_id=&get_char_attribute($char,"=$out_cs")
465 or $output_char_id=&get_char_attribute($char,"=ucs")
466 or $output_char_id=&get_char_attribute($char,"=>$out_cs")
467 or $output_char_id=&get_char_attribute($char,"=>ucs")
468 or $output_char_id=&get_char_attribute($char,"=>ucs*")
470 $output_ids.=pack("U",$output_char_id);
480 # return: character in output coding system or TeX macro or undef.
481 my($char,$out_cs)=@_;
482 my($out_char_id,$suffix);
485 foreach $out_cs (@{$order{$out_cs}}){
486 if($out_cs eq 'UniJIS'
487 and &get_char_attribute($char,"vnd-adobe-cid-unijis-utf16-h")){
488 if($out_char_id=&get_char_attribute($char,'=ucs@jis')
489 or $out_char_id=&get_char_attribute($char,'=ucs')
490 or $out_char_id=&get_char_attribute($char,'=>ucs@jis')
491 or $out_char_id=&get_char_attribute($char,'=>ucs')
492 or $out_char_id=&get_char_attribute($char,'=>ucs*')
494 return '{\selectjisfont\char'.$out_char_id.'}';
496 }elsif($out_cs eq 'UniGB'
497 and &get_char_attribute($char,"vnd-adobe-cid-unigb-ucs2-h")){
498 if($out_char_id=&get_char_attribute($char,'=ucs@gb')
499 or $out_char_id=&get_char_attribute($char,'=ucs')
500 or $out_char_id=&get_char_attribute($char,'=>ucs@gb')
501 or $out_char_id=&get_char_attribute($char,'=>ucs')
502 or $out_char_id=&get_char_attribute($char,'=>ucs*')
504 return '{\selectgbsfont\char'.$out_char_id.'}';
506 }elsif($out_cs eq 'UniCNS'
507 and &get_char_attribute($char,"vnd-adobe-cid-unicns-ucs2-h")){
508 if($out_char_id=&get_char_attribute($char,'=ucs@cns')
509 or $out_char_id=&get_char_attribute($char,'=ucs')
510 or $out_char_id=&get_char_attribute($char,'=>ucs@cns')
511 or $out_char_id=&get_char_attribute($char,'=>ucs')
512 or $out_char_id=&get_char_attribute($char,'=>ucs*')
514 return '{\selectcnsfont\char'.$out_char_id.'}';
516 }elsif($out_cs eq 'UniKS'
517 and &get_char_attribute($char,"vnd-adobe-cid-uniks-ucs2-h")){
518 if($out_char_id=&get_char_attribute($char,'=ucs@ks')
519 or $out_char_id=&get_char_attribute($char,'=ucs')
520 or $out_char_id=&get_char_attribute($char,'=>ucs@ks')
521 or $out_char_id=&get_char_attribute($char,'=>ucs')
522 or $out_char_id=&get_char_attribute($char,'=>ucs*')
524 return '{\selectksxfont\char'.$out_char_id.'}';
526 }elsif($out_cs eq 'GT'){
527 return $gt if($gt=&get_macro_for_GT($char));
528 }elsif($out_cs eq 'HZK'){
529 return $hzk if($hzk=&get_macro_for_HZK($char));
530 }elsif($out_cs eq 'CDP'){
531 return $cdp if($cdp=&get_macro_for_CDP($char));
538 # argument: <character>
542 $ids=&get_char_attribute($char,"ids-aggregated")
543 or $ids=&get_char_attribute($char,"ids");
544 # $ids=&get_char_attribute($char,"ids-decomposed")
545 # or $ids=&get_char_attribute($char,"ids");
546 # or $ids=&get_char_attribute($char,"ideographic-structure");
551 sub get_char_for_ids{
552 # argument: <ideographic description sequence>
553 # return: char or undef.
557 if(($output_char)=&get_chars_matching("ids",$ids)){
565 # argument: <char>, <input coding system>
566 # return: char in ucs@mcs.
570 return $char if($in_cs eq 'ucs@mcs');
572 if(($output_char)=&get_chars_matching("=$in_cs",unpack("U",$char))){
579 sub get_chars_unified{
581 my($chars,$ucs,$char_ucs);
584 if($chars=&get_char_attribute($char,'->ucs-unified')){
585 $chars=~s/^\((.*)\)$/$1/;
586 return (split(/\s*\?/,$chars));
587 }elsif($ucs=&get_char_attribute($char,'=>ucs*')
588 or $ucs=&get_char_attribute($char,'=>ucs')){
589 $char_ucs=pack("U",$ucs);
590 if($chars=&get_char_attribute($char_ucs,'->ucs-unified')){
591 $chars=~s/^\((.*)\)$/$1/;
592 @chars=grep {not /^$char$/}
593 (split(/\s*\?/,$chars));
594 push(@chars,$char_ucs);
600 sub get_macro_for_GT{
602 # return: TeX macro for GT fonts or undef.
606 if($gt=&get_char_attribute($char,$_)){
607 m/gt\-pj\-(\d+)/ and $GT=$1;
612 return "{\\fontencoding{OT1}\\fontfamily{"
613 .sprintf("gt%02d",$GT)
614 ."}\\selectfont\\char".($gt|0x8080)."}";
620 sub get_macro_for_HZK{
622 # return: TeX macro for Hanziku fonts or undef.
626 if($hzk=&get_char_attribute($char,$_)){
627 m/hanziku\-(\d+)/ and $HZK=$1;
632 return "{\\fontencoding{OT1}\\fontfamily{".sprintf("hzk%02d",$HZK)."}\\selectfont\\char".$hzk."}";
638 sub get_macro_for_CDP{
640 # return: TeX macro for CDP fonts or undef.
644 if($cdp=&get_char_attribute($char,$_)){
649 $ucs=&get_char_attribute(&get_chars_matching("=big5-pua",$cdp),"=ucs");
651 return "{\\fontencoding{OT1}\\fontfamily{cdp}\\selectfont\\char"
655 print STDERR "This should not happen.\n";
656 print STDERR "ucs code point of CDP: $cdp not found.\n";