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
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
23 use Chise_utils ':all';
28 $opt_order{'UniMulti'}='jGcgk';
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';
35 $opt_allow_unify=1; # 1=true, 0=false.
36 $opt_protrude=0;# 1=true, 0=false.
38 # currently does not work.
39 $opt_use_kage_for_Ext_B=0;# 1=true, 0=false.
45 my $strictly_forbidden_after = '「【『[(〈‘‛“‟(〔{《{\[\(〖{「';
47 my $forbidden_after = "\x{0000}";
50 my $slightly_forbidden_after = '¥¥$$〒♯##¢¢££@@§';
52 # $strictly_forbidden_before
53 # All these characters are allowed to protrude
55 my $strictly_forbidden_before=
56 '!,.:;?、。!,.:;?。\)\]}’”〉》」』】〕〗)]}」';
59 = 'ー々ぁぃぅぇぉゃゅょっゎァィゥェォャュョッヮヵヶ';
61 my $slightly_forbidden_before
62 = '\x{000a}\#\-‐−‰′″℃゛゜ゝゞヽヾ"%-゙゚';
64 #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.
65 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.
67 my $space = '\x{0020}\x{0009}\x{000A}\x{000C}\x{000D}';
69 my $hangul='\x{1100}-\x{11ff}\x{3130}-\x{318f}\x{AC00}-\x{D7AF}';
71 my %tex_meta=('#'=>'\#',
72 '$'=>'\\textdollar{}',
75 '{'=>'\\textbraceleft{}',
76 '}'=>'\\textbraceright{}',
77 '\\'=>'\\textbackslash{}',
78 '_'=>'\\textunderscore',
81 my $tex_meta_re=join('|',map {quotemeta($_)} keys %tex_meta);
83 &GetOptions("in=s"=>\$opt_in_cs,
84 "out=s"=>\$opt_out_cs,
88 Usage: $0 -i <input coding system> -o <cmap encoding>
90 Utf8mcs, Utf8cns, Utf8gb, Utf8jis, Utf8ks, Utf8big5
92 UniCNS, UniGB, UniJIS, UniKS, UniMulti
95 if($opt_in_cs or $opt_out_cs){
101 ,$opt_preserve_spaces)
102 =($0=~/(Utf8mcs|Utf8cns|Utf8gb|Utf8jis|Utf8ks|Utf8big5)
103 To (UniCNS|UniGB|UniJIS|UniKS|UniMulti)
108 # Utf8mcs,Utf8cns,Utf8gb,Utf8jis,Utf8ks,Utf8big5
110 # UniCNS,UniGB,UniJIS,UniKS,UniMulti
112 $in_cs=~s/Utf8/ucs\@/;
115 or not defined($in_cs)
116 or not defined($out_cs)){
121 $omegadb_path=~s!/$!!;
123 $idsdata_file="$omegadb_path/idsdata.pl";
127 if(-e $idsdata_file){
128 open(IDSDATA,"+<:utf8",$idsdata_file) or die;
129 flock(IDSDATA,LOCK_EX);
135 # require $idsdata_file;
137 open(IDSDATA,">:utf8",$idsdata_file) or die;
138 flock(IDSDATA,LOCK_EX);
145 #$geta=pack("U",0x3013);
146 $geta=pack("U",0xfffd);
149 "=gt-pj-1","=gt-pj-2","=gt-pj-3","=gt-pj-4","=gt-pj-5",
150 "=gt-pj-6","=gt-pj-7","=gt-pj-8","=gt-pj-9","=gt-pj-10",
152 #,"=gt-pj-k1","=gt-pj-k2"
154 @HZK=("=hanziku-1","=hanziku-2","=hanziku-3","=hanziku-4",
155 "=hanziku-5","=hanziku-6","=hanziku-7","=hanziku-8",
156 "=hanziku-9","=hanziku-10","=hanziku-11","=hanziku-12");
159 %order_map=('c'=>'UniCNS',
168 foreach $out_cs ('UniCNS','UniGB','UniJIS','UniKS','UniMulti'){
169 if(defined($opt_order{$out_cs})){
170 if($opt_order{$out_cs}=~/^[cgjkGHC]+$/){
171 @{$order{$out_cs}}=map {$order_map{$_}}
172 (split(//,$opt_order{$out_cs}));
174 print STDERR "Invalid order for $out_cs!\n";
185 if($in_cs ne 'ucs@mcs'){
186 s/(.)/&get_char_in_mcs($1,$in_cs)/ge;
188 s/(amp.+?;)/&de_tex_er($1)/ge;
189 # s/(&.+?;)/&de_tex_er($1)/ge;
190 s/([$asian])[$space]+/$1/go unless($opt_preserve_spaces);
191 s/[$space]+([$asian])/$1/go unless($opt_preserve_spaces);
192 s/([$asian])\s*([^$asian$space])/$1 $2/go;
193 s/([^$asian$idc])\s*([$asian])/$1 $2/go;
194 s/\-\-\-/pack("U",0x2014)/geo;# EM DASH
195 s/\-\-/pack("U",0x2013)/geo;# EN DASH
196 # s/\`\`/pack("U",0x201f)/geo;# DOUBLE HIGH-REVERSED-9 QUOTATION MARK
197 s/\`\`/pack("U",0x201c)/geo;# _TeX compatible_
198 # s/\`/pack("U",0x201b)/geo;# SINGLE HIGH-REVERSED-9 QUOTATION MARK
199 s/\`/pack("U",0x2018)/geo;# _TeX compatible_
200 s/\'\'/pack("U",0x201d)/geo;# RIGHT DOUBLE QUOTATION MARK
201 s/\'/pack("U",0x2019)/geo;# RIGHT DOUBLE QUOTATION MARK
205 for($i=0;$i<=$#chars;$i++){
207 if($char_id=&get_char_attribute($char,'=ucs@unicode')){
208 $char=pack("U",$char_id);
210 $char_id=unpack("U",$char);
214 # add quarter space between asian and non-asian.
215 if($i>0 and $i<$#chars){
216 if(($chars[$i-1]=~/[$asian]/
217 and $chars[$i+1]=~/[^$asian]/
218 and $chars[$i+1]=~/[$strictly_forbidden_before$slightly_forbidden_before]/)
219 or($chars[$i-1]=~/[^$asian]/
220 and $chars[$i+1]=~/[$asian]/
221 and $chars[$i-1]=~/[$strictly_forbidden_after$slightly_forbidden_after]/)
224 }elsif(($chars[$i-1]=~/[$asian]/
225 and $chars[$i+1]=~/[^$asian]/)){
226 print '\unskip\kern.25ex';
227 }elsif($chars[$i-1]=~/[^$asian]/
228 and $chars[$i+1]=~/[$asian]/){
237 }elsif(($char_id>0x20 and $char_id<=0x021f)
241 # Latin Extended-B (not all)
242 or($char_id>=0x0250 and $char_id<=0x02af)
244 or($char_id>=0x0300 and $char_id<=0x033f)
245 or($char_id>=0x0360 and $char_id<=0x036f)
246 # Combining Diacritical Marks
247 or($char_id>=0x1e00 and $char_id<=0x1eff)
248 # Latin Extended Additional
249 or($char_id>=0x0370 and $char_id<=0x03ff)
251 or($char_id>=0x0400 and $char_id<=0x04ff)
253 or($char_id>=0x0530 and $char_id<=0x058f)
255 or($char_id>=0x2010 and $char_id<=0x2046)
256 # General Punctuation (partial)
258 print &latin_parse();
260 }elsif($char_id>=0x2ff0 and $char_id<=0x2fff){
261 # Ideographic Description Characters
265 if(($out_char=&get_output_char($char,$out_cs))){
266 print $out_char,&add_break($i);
268 if($opt_allow_unify){
269 @chars_unified=&get_chars_unified($char);
270 if(@chars_unified>0){
271 foreach $char_unified (@chars_unified){
273 =&get_output_char($char_unified,$out_cs))){
274 print $out_char,&add_break($i);
280 if($opt_use_kage_for_Ext_B){
281 if($char_id >= 0x20000 && $char_id <=0x2a6df){
282 # CJK Unified Ideographs Extension B
283 if(not defined($ids{$char}) and $ids{$char}[1]>=0){
284 $ids{$char}[0]=$font_start;
285 $ids{$char}[1]=$ids_start;
292 print "{\\fontencoding{OT1}\\fontfamily{",
293 sprintf("chise%03d",$ids{$char}[0]) ,
294 "}\\selectfont\\char$ids{$char}[1]}",&add_break($i);
298 if($ids=&get_ids($char)){
299 print &get_macro_for_ids($ids),&add_break($i);
301 print '\rule{1ex}{1ex}',&add_break($i);
309 print IDSDATA 'use utf8;',"\n";
310 foreach $ids (keys %ids){
311 print IDSDATA '$ids{\'',$ids,'\'}='
312 ,'[',join ",",@{$ids{$ids}},"];\n";
314 print IDSDATA '$font_start=',$font_start,";\n";
315 print IDSDATA '$ids_start=',$ids_start,";\n";
317 flock(IDSDATA,LOCK_UN);
324 my($output_char,$atr,$value);
325 $er=~/^(amp)(.*)(;)$/
326 and $prefix=$1,$er=$2,$suffix=$3;
327 $prefix or $prefix="",$suffix or $suffix="";
328 if($er=~/^U[\+|\-]([a-fA-F\d]+)/){
329 $output_char=pack("U",hex($1));
330 }elsif($er=~/^(?:I\-)?($er_prefix_re)\-?([0-9a-fA-F]+)$/){
331 ($atr,$value)=($1,$2);
332 unless($er_alias{$atr}=~/daikanwa|gt/){
335 ($output_char)=&get_chars_matching($er_alias{$atr},$value);
340 return $prefix.$er.$suffix;
348 if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
349 and($chars[$i+2]=~m/[$strictly_forbidden_before]/o)){
350 return "\\CJKunbreakablekernone{}";
351 }elsif($opt_protrude){
352 if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
353 and($chars[$i+2]=~m/[^$strictly_forbidden_before]/o)){
354 return "\\CJKunbreakablekernone \\CJKprotrude{}";
359 and($chars[$i+1]eq" ")){
364 and($chars[$i+1]=~m/[$strictly_forbidden_before]/o)){
365 return "\\CJKunbreakablekernone{}";
367 if($chars[$i]=~m/[$strictly_forbidden_after]/o){
368 return "\\CJKunbreakablekernone{}";
371 and($chars[$i+1]=~m/[$forbidden_before]/o)){
372 return "\\CJKunbreakablekerntwo{}";
375 if($chars[$i]=~m/[$forbidden_after]/o){
376 return "\\CJKunbreakablekerntwo{}";
379 and($chars[$i+1]=~m/[$slightly_forbidden_before]/o)){
380 return "\\CJKunbreakablekernthree{}";
382 if($chars[$i]=~m/[$slightly_forbidden_after]/o){
383 return "\\CJKunbreakablekernthree{}";
385 if($chars[$i]=~m/[$asian]/o){
386 return "\\CJKbreakablekern{}";
388 if(($i<$#chars)and($chars[$i+1]=~m/[$asian]/o)){
389 return "\\CJKbreakablekern{}";
395 # return: string for output with TeX macro.
399 $char_id=unpack("U",$chars[$i]);
400 if($chars[$i]=~m/($tex_meta_re)/o){
401 $out_str.=$tex_meta{$chars[$i]};
402 }elsif(($char_id>0x20 and $char_id<=0x021f)
403 or($char_id>=0x0250 and $char_id<=0x02af)# IPA Extensions
404 or($char_id>=0x0300 and $char_id<=0x033f)# Combining Diacritical Marks
405 or($char_id>=0x0360 and $char_id<=0x036f)
406 or($char_id>=0x0370 and $char_id<=0x03ff)# Greek and Coptic
407 or($char_id>=0x0400 and $char_id<=0x04ff)# Cyrillic
408 or($char_id>=0x0530 and $char_id<=0x058f)# Armenian
410 $out_str.=$chars[$i];
411 }elsif($char_id>=0x1e00 and $char_id<=0x1eff){
412 # Latin Extended Additional
413 # 0x1e00 -> 0x0600, etc.
414 $out_str.=pack("U",$char_id-0x1800);
415 }elsif($char_id>=0x2010 and $char_id<=0x2046){
416 # General Punctuation (partial)
417 $out_str.=pack("U",$char_id-0x1000);
424 return '{\fontencoding{OT1}\fontfamily{omlgc}\selectfont '.$out_str.'}';###UT1?
429 # return: character for output,
431 # or GETA character if ids is invalid.
432 my($ids,$ids_argc)=&ids_rest("",0,$chars[$i]);
438 print STDERR "IDS parse error: $ids\n";
442 ($ids,$ids_argc)=&ids_rest($ids,$ids_argc,$chars[$i]);
444 if(($char=&get_char_for_ids($ids))
445 and($out_char=&get_output_char($char,$out_cs))){
448 return &get_macro_for_ids($ids);
455 # arguments: <ids>, <rest number of arguments for ids>, <character>
456 # return: ids and rest number of arguments for ids.
457 my($ids,$ids_argc,$char)=@_;
459 $argc=&ids_argc($char);
461 $ids_argc+=$ids_argc==0?$argc:$argc-1;
466 return ($ids,$ids_argc);
469 sub get_macro_for_ids{
471 # return: TeX macro for ids
472 # or GETA character if ids is invalid for KAGE.
474 # $ids=&normalize_ids($ids,"UniJIS");
475 return $geta if(($ids!~/[$idc]/)
476 or($ids=~/[\x{10000}-]/));
478 if(not defined($ids{$ids})){
479 $ids{$ids}[0]=$font_start;
480 $ids{$ids}[1]=$ids_start;
487 return "{\\fontencoding{OT1}\\fontfamily{"
488 .sprintf("chise%03d",$ids{$ids}[0])
489 ."}\\selectfont\\char$ids{$ids}[1]}\\relax{}";
493 # argument: <ids>, <output coding system>
494 # return: ids or GETA character if ids is invalid for KAGE.
496 $out_cs=~s/Uni(.+)/'ucs@'.lc($1)/e;
499 my($char,$output_char_id);
500 while($ids=~m/(.)/g){
504 }elsif($output_char_id=&get_char_attribute($char,"=$out_cs")
505 or $output_char_id=&get_char_attribute($char,"=ucs")
506 or $output_char_id=&get_char_attribute($char,"=>$out_cs")
507 or $output_char_id=&get_char_attribute($char,"=>ucs")
508 or $output_char_id=&get_char_attribute($char,"=>ucs*")
510 $output_ids.=pack("U",$output_char_id);
520 # return: character in output coding system or TeX macro or undef.
521 my($char,$out_cs)=@_;
522 my($out_char_id,$suffix);
525 foreach $out_cs (@{$order{$out_cs}}){
526 if($out_cs eq 'UniJIS'
527 and &get_char_attribute($char,"vnd-adobe-cid-unijis-utf16-h")){
528 if($out_char_id=&get_char_attribute($char,'=ucs@jis')
529 or $out_char_id=&get_char_attribute($char,'=ucs')
530 or $out_char_id=&get_char_attribute($char,'=>ucs@jis')
531 or $out_char_id=&get_char_attribute($char,'=>ucs')
532 or $out_char_id=&get_char_attribute($char,'=>ucs*')
534 return "{\\selectjisfont\\char$out_char_id}";
536 }elsif($out_cs eq 'UniGB'
537 and &get_char_attribute($char,"vnd-adobe-cid-unigb-ucs2-h")){
538 if($out_char_id=&get_char_attribute($char,'=ucs@gb')
539 or $out_char_id=&get_char_attribute($char,'=ucs')
540 or $out_char_id=&get_char_attribute($char,'=>ucs@gb')
541 or $out_char_id=&get_char_attribute($char,'=>ucs')
542 or $out_char_id=&get_char_attribute($char,'=>ucs*')
544 return "{\\selectgbsfont\\char$out_char_id}";
546 }elsif($out_cs eq 'UniCNS'
547 and &get_char_attribute($char,"vnd-adobe-cid-unicns-ucs2-h")){
548 if($out_char_id=&get_char_attribute($char,'=ucs@cns')
549 or $out_char_id=&get_char_attribute($char,'=ucs')
550 or $out_char_id=&get_char_attribute($char,'=>ucs@cns')
551 or $out_char_id=&get_char_attribute($char,'=>ucs')
552 or $out_char_id=&get_char_attribute($char,'=>ucs*')
554 return "{\\selectcnsfont\\char$out_char_id}";
556 }elsif($out_cs eq 'UniKS'
557 and &get_char_attribute($char,"vnd-adobe-cid-uniks-ucs2-h")){
558 if($out_char_id=&get_char_attribute($char,'=ucs@ks')
559 or $out_char_id=&get_char_attribute($char,'=ucs')
560 or $out_char_id=&get_char_attribute($char,'=>ucs@ks')
561 or $out_char_id=&get_char_attribute($char,'=>ucs')
562 or $out_char_id=&get_char_attribute($char,'=>ucs*')
564 return "{\\selectksxfont\\char$out_char_id}";
566 }elsif($out_cs eq 'GT'){
567 return $gt if($gt=&get_macro_for_GT($char));
568 }elsif($out_cs eq 'HZK'){
569 return $hzk if($hzk=&get_macro_for_HZK($char));
570 }elsif($out_cs eq 'CDP'){
571 return $cdp if($cdp=&get_macro_for_CDP($char));
578 # argument: <character>
582 # $ids=&get_char_attribute($char,"ids-aggregated")
583 # or $ids=&get_char_attribute($char,"ids");
584 $ids=&get_char_attribute($char,"ids-decomposed")
585 or $ids=&get_char_attribute($char,"ids")
586 or $ids=&get_char_attribute($char,"ideographic-structure");
591 sub get_char_for_ids{
592 # argument: <ideographic description sequence>
593 # return: char or undef.
597 if(($output_char)=&get_chars_matching("ids",$ids)){
605 # argument: <char>, <input coding system>
606 # return: char in ucs@mcs.
610 return $char if($in_cs eq 'ucs@mcs');
612 if(($output_char)=&get_chars_matching("=$in_cs",unpack("U",$char))){
619 sub get_chars_unified{
621 my($chars,$ucs,$char_ucs,$char_sub);
624 if($chars=&get_char_attribute($char,'->ucs-unified')){
625 $chars=~s/^\((.*)\)$/$1/;
626 return (split(/\s*\?/,$chars));
627 }elsif($ucs=&get_char_attribute($char,'=>ucs*')
628 or $ucs=&get_char_attribute($char,'=>ucs')){
629 $char_ucs=pack("U",$ucs);
630 if($chars=&get_char_attribute($char_ucs,'->ucs-unified')){
631 $chars=~s/^\((.*)\)$/$1/;
632 @chars=grep {not /^$char$/}
633 (split(/\s*\?/,$chars));
634 push(@chars,$char_ucs);
637 }elsif($char_sub=&get_char_attribute($char,'<-subsumptive')){
638 $char_sub=~s/[? ()]//g;
639 $chars=&get_char_attribute($char_sub,'->subsumptive');
640 @chars=grep {not /^$char$/} (split(/\s*\?/,$chars));
641 push(@chars,$char_sub);
648 sub get_macro_for_GT{
650 # return: TeX macro for GT fonts or undef.
654 if($gt=&get_char_attribute($char,$_)){
655 m/gt\-pj\-(\d+)/ and $GT=$1;
660 return "{\\fontencoding{OT1}\\fontfamily{"
661 .sprintf("gt%02d",$GT)
662 ."}\\selectfont\\char".($gt|0x8080)."}";
668 sub get_macro_for_HZK{
670 # return: TeX macro for Hanziku fonts or undef.
674 if($hzk=&get_char_attribute($char,$_)){
675 m/hanziku\-(\d+)/ and $HZK=$1;
680 return "{\\fontencoding{OT1}\\fontfamily{"
681 .sprintf("hzk%02d",$HZK)
682 ."}\\selectfont\\char".$hzk."}";
688 sub get_macro_for_CDP{
690 # return: TeX macro for CDP fonts or undef.
694 if($cdp=&get_char_attribute($char,$_)){
699 $ucs=&get_char_attribute(&get_chars_matching("=big5-pua",$cdp),"=ucs");
701 return "{\\fontencoding{OT1}\\fontfamily{cdp}\\selectfont\\char$ucs}";
703 print STDERR "This should not happen.\n";
704 print STDERR "ucs code point of CDP: $cdp not found.\n";