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.
43 my $strictly_forbidden_after = '「【『[(〈‘‛“‟(〔{《{\[\(〖{「';
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 '!,.:;?、。!,.:;?。\)\]}’”〉》」』】〕〗)]}」';
57 = 'ー々ぁぃぅぇぉゃゅょっゎァィゥェォャュョッヮヵヶ';
59 my $slightly_forbidden_before
60 = '\x{000a}\#\-‐−‰′″℃゛゜ゝゞヽヾ"%-゙゚';
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.
65 my $space = '\x{0020}\x{0009}\x{000A}\x{000C}\x{000D}';
67 my $hangul='\x{1100}-\x{11ff}\x{3130}-\x{318f}\x{AC00}-\x{D7AF}';
69 my %tex_meta=('#'=>'\#',
70 '$'=>'\\textdollar{}',
73 '{'=>'\\textbraceleft{}',
74 '}'=>'\\textbraceright{}',
75 '\\'=>'\\textbackslash{}',
76 '_'=>'\\textunderscore',
79 my $tex_meta_re=join('|',map {quotemeta($_)} keys %tex_meta);
81 &GetOptions("in=s"=>\$opt_in_cs,
82 "out=s"=>\$opt_out_cs,
86 Usage: $0 -i <input coding system> -o <cmap encoding>
88 Utf8mcs, Utf8cns, Utf8gb, Utf8jis, Utf8ks, Utf8big5
90 UniCNS, UniGB, UniJIS, UniKS, UniMulti
93 if($opt_in_cs or $opt_out_cs){
99 ,$opt_preserve_spaces)
100 =($0=~/(Utf8mcs|Utf8cns|Utf8gb|Utf8jis|Utf8ks|Utf8big5)
101 To (UniCNS|UniGB|UniJIS|UniKS|UniMulti)
106 # Utf8mcs,Utf8cns,Utf8gb,Utf8jis,Utf8ks,Utf8big5
108 # UniCNS,UniGB,UniJIS,UniKS,UniMulti
110 $in_cs=~s/Utf8/ucs\@/;
113 or not defined($in_cs)
114 or not defined($out_cs)){
119 $omegadb_path=~s!/$!!;
121 $idsdata_file="$omegadb_path/idsdata.txt";
125 if(-e $idsdata_file){
126 open(IDSDATA,"+<:utf8",$idsdata_file) or die;
127 flock(IDSDATA,LOCK_EX);
131 if(m/^START\t(\d+)\t(\d+)/){
132 $font_start=$1,$ids_start=$2;
133 }elsif(m/^(.*?)\t(\d+)\t(\d+)/){
140 open(IDSDATA,">:utf8",$idsdata_file) or die;
141 flock(IDSDATA,LOCK_EX);
148 #$geta=pack("U",0x3013);
149 $geta=pack("U",0xfffd);
152 "=gt-pj-1","=gt-pj-2","=gt-pj-3","=gt-pj-4","=gt-pj-5",
153 "=gt-pj-6","=gt-pj-7","=gt-pj-8","=gt-pj-9","=gt-pj-10",
155 #,"=gt-pj-k1","=gt-pj-k2"
157 @HZK=("=hanziku-1","=hanziku-2","=hanziku-3","=hanziku-4",
158 "=hanziku-5","=hanziku-6","=hanziku-7","=hanziku-8",
159 "=hanziku-9","=hanziku-10","=hanziku-11","=hanziku-12");
162 %order_map=('c'=>'UniCNS',
171 foreach $out_cs ('UniCNS','UniGB','UniJIS','UniKS','UniMulti'){
172 if(defined($opt_order{$out_cs})){
173 if($opt_order{$out_cs}=~/^[cgjkGHC]+$/){
174 @{$order{$out_cs}}=map {$order_map{$_}}
175 (split(//,$opt_order{$out_cs}));
177 print STDERR "Invalid order for $out_cs!\n";
190 if($in_cs ne 'ucs@mcs'){
191 s/(.)/&get_char_in_mcs($1,$in_cs)/ge;
193 s/(amp.+?;)/&de_tex_er($1)/ge;
194 # s/(&.+?;)/&de_tex_er($1)/ge;
195 s/([$asian])[$space]+/$1/go unless($opt_preserve_spaces);
196 s/[$space]+([$asian])/$1/go unless($opt_preserve_spaces);
197 s/([$asian])\s*([^$asian$space])/$1 $2/go;
198 s/([^$asian$idc])\s*([$asian])/$1 $2/go;
199 s/\-\-\-/pack("U",0x2014)/geo;# EM DASH
200 s/\-\-/pack("U",0x2013)/geo;# EN DASH
201 # s/\`\`/pack("U",0x201f)/geo;# DOUBLE HIGH-REVERSED-9 QUOTATION MARK
202 s/\`\`/pack("U",0x201c)/geo;# _TeX compatible_
203 # s/\`/pack("U",0x201b)/geo;# SINGLE HIGH-REVERSED-9 QUOTATION MARK
204 s/\`/pack("U",0x2018)/geo;# _TeX compatible_
205 s/\'\'/pack("U",0x201d)/geo;# RIGHT DOUBLE QUOTATION MARK
206 s/\'/pack("U",0x2019)/geo;# RIGHT DOUBLE QUOTATION MARK
210 for($i=0;$i<=$#chars;$i++){
212 if($char_id=&get_char_attribute($char,'=ucs@unicode')){
213 $char=pack("U",$char_id);
215 $char_id=unpack("U",$char);
219 # add quarter space between asian and non-asian.
220 if($i>0 and $i<$#chars){
221 if(($chars[$i-1]=~/[$asian]/
222 and $chars[$i+1]=~/[^$asian]/
223 and $chars[$i+1]=~/[$strictly_forbidden_before$slightly_forbidden_before]/)
224 or($chars[$i-1]=~/[^$asian]/
225 and $chars[$i+1]=~/[$asian]/
226 and $chars[$i-1]=~/[$strictly_forbidden_after$slightly_forbidden_after]/)
229 }elsif(($chars[$i-1]=~/[$asian]/
230 and $chars[$i+1]=~/[^$asian]/)){
231 print '\unskip\kern.25ex';
232 }elsif($chars[$i-1]=~/[^$asian]/
233 and $chars[$i+1]=~/[$asian]/){
242 }elsif(($char_id>0x20 and $char_id<=0x021f)
246 # Latin Extended-B (not all)
247 or($char_id>=0x0250 and $char_id<=0x02af)
249 or($char_id>=0x0300 and $char_id<=0x033f)
250 or($char_id>=0x0360 and $char_id<=0x036f)
251 # Combining Diacritical Marks
252 or($char_id>=0x1e00 and $char_id<=0x1eff)
253 # Latin Extended Additional
254 or($char_id>=0x0370 and $char_id<=0x03ff)
256 or($char_id>=0x0400 and $char_id<=0x04ff)
258 or($char_id>=0x0530 and $char_id<=0x058f)
260 or($char_id>=0x2010 and $char_id<=0x2046)
261 # General Punctuation (partial)
263 print &latin_parse();
265 }elsif($char_id>=0x2ff0 and $char_id<=0x2fff){
266 # Ideographic Description Characters
270 if(($out_char=&get_output_char($char,$out_cs))){
271 print $out_char,&add_break($i);
273 if($opt_allow_unify){
274 @chars_unified=&get_chars_unified($char);
275 if(@chars_unified>0){
276 foreach $char_unified (@chars_unified){
278 =&get_output_char($char_unified,$out_cs))){
279 print $out_char,&add_break($i);
285 if($opt_use_kage_for_Ext_B){
286 if($char_id >= 0x20000 && $char_id <=0x2a6df){
287 # CJK Unified Ideographs Extension B
288 if(not defined($ids{$char}) and $ids{$char}[1]>=0){
289 $ids{$char}[0]=$font_start;
290 $ids{$char}[1]=$ids_start;
297 print "{\\fontencoding{OT1}\\fontfamily{",
298 sprintf("chise%03d",$ids{$char}[0]) ,
299 "}\\selectfont\\char$ids{$char}[1]}",&add_break($i);
303 if($ids=&get_ids($char)){
304 print &get_macro_for_ids($ids),&add_break($i);
306 print '\rule{1ex}{1ex}',&add_break($i);
314 print IDSDATA 'START',"\t",$font_start,"\t",$ids_start,"\n";
315 foreach $ids (keys %ids){
316 print IDSDATA $ids,"\t",join("\t",@{$ids{$ids}}),"\n";
318 flock(IDSDATA,LOCK_UN);
325 my($output_char,$atr,$value);
326 $er=~/^(amp)(.*)(;)$/
327 and $prefix=$1,$er=$2,$suffix=$3;
328 $prefix or $prefix="",$suffix or $suffix="";
329 if($er=~/^U[\+|\-]([a-fA-F\d]+)/){
330 $output_char=pack("U",hex($1));
331 }elsif($er=~/^(?:I\-)?($er_prefix_re)\-?([0-9a-fA-F]+)$/){
332 ($atr,$value)=($1,$2);
333 unless($er_alias{$atr}=~/daikanwa|gt/){
336 ($output_char)=&get_chars_matching($er_alias{$atr},$value);
341 return $prefix.$er.$suffix;
349 if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
350 and($chars[$i+2]=~m/[$strictly_forbidden_before]/o)){
351 return "\\CJKunbreakablekernone{}";
352 }elsif($opt_protrude){
353 if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
354 and($chars[$i+2]=~m/[^$strictly_forbidden_before]/o)){
355 return "\\CJKunbreakablekernone \\CJKprotrude{}";
360 and($chars[$i+1]eq" ")){
365 and($chars[$i+1]=~m/[$strictly_forbidden_before]/o)){
366 return "\\CJKunbreakablekernone{}";
368 if($chars[$i]=~m/[$strictly_forbidden_after]/o){
369 return "\\CJKunbreakablekernone{}";
372 and($chars[$i+1]=~m/[$forbidden_before]/o)){
373 return "\\CJKunbreakablekerntwo{}";
376 if($chars[$i]=~m/[$forbidden_after]/o){
377 return "\\CJKunbreakablekerntwo{}";
380 and($chars[$i+1]=~m/[$slightly_forbidden_before]/o)){
381 return "\\CJKunbreakablekernthree{}";
383 if($chars[$i]=~m/[$slightly_forbidden_after]/o){
384 return "\\CJKunbreakablekernthree{}";
386 if($chars[$i]=~m/[$asian]/o){
387 return "\\CJKbreakablekern{}";
389 if(($i<$#chars)and($chars[$i+1]=~m/[$asian]/o)){
390 return "\\CJKbreakablekern{}";
396 # return: string for output with TeX macro.
400 $char_id=unpack("U",$chars[$i]);
401 if($chars[$i]=~m/($tex_meta_re)/o){
402 $out_str.=$tex_meta{$chars[$i]};
403 }elsif(($char_id>0x20 and $char_id<=0x021f)
404 or($char_id>=0x0250 and $char_id<=0x02af)# IPA Extensions
405 or($char_id>=0x0300 and $char_id<=0x033f)# Combining Diacritical Marks
406 or($char_id>=0x0360 and $char_id<=0x036f)
407 or($char_id>=0x0370 and $char_id<=0x03ff)# Greek and Coptic
408 or($char_id>=0x0400 and $char_id<=0x04ff)# Cyrillic
409 or($char_id>=0x0530 and $char_id<=0x058f)# Armenian
411 $out_str.=$chars[$i];
412 }elsif($char_id>=0x1e00 and $char_id<=0x1eff){
413 # Latin Extended Additional
414 # 0x1e00 -> 0x0600, etc.
415 $out_str.=pack("U",$char_id-0x1800);
416 }elsif($char_id>=0x2010 and $char_id<=0x2046){
417 # General Punctuation (partial)
418 $out_str.=pack("U",$char_id-0x1000);
425 return '{\fontencoding{OT1}\fontfamily{omlgc}\selectfont '.$out_str.'}';###UT1?
430 # return: character for output,
432 # or GETA character if ids is invalid.
433 my($ids,$ids_argc)=&ids_rest("",0,$chars[$i]);
439 print STDERR "IDS parse error: $ids\n";
443 ($ids,$ids_argc)=&ids_rest($ids,$ids_argc,$chars[$i]);
445 if(($char=&get_char_for_ids($ids))
446 and($out_char=&get_output_char($char,$out_cs))){
449 return &get_macro_for_ids($ids);
456 # arguments: <ids>, <rest number of arguments for ids>, <character>
457 # return: ids and rest number of arguments for ids.
458 my($ids,$ids_argc,$char)=@_;
460 $argc=&ids_argc($char);
462 $ids_argc+=$ids_argc==0?$argc:$argc-1;
467 return ($ids,$ids_argc);
470 sub get_macro_for_ids{
472 # return: TeX macro for ids
473 # or GETA character if ids is invalid for KAGE.
475 # $ids=&normalize_ids($ids,"UniJIS");
476 return $geta if(($ids!~/[$idc]/)
477 or($ids=~/[\x{10000}-]/));
479 if(not defined($ids{$ids})){
480 $ids{$ids}[0]=$font_start;
481 $ids{$ids}[1]=$ids_start;
488 return "{\\fontencoding{OT1}\\fontfamily{"
489 .sprintf("chise%03d",$ids{$ids}[0])
490 ."}\\selectfont\\char$ids{$ids}[1]}\\relax{}";
494 # argument: <ids>, <output coding system>
495 # return: ids or GETA character if ids is invalid for KAGE.
497 $out_cs=~s/Uni(.+)/'ucs@'.lc($1)/e;
500 my($char,$output_char_id);
501 while($ids=~m/(.)/g){
505 }elsif($output_char_id=&get_char_attribute($char,"=$out_cs")
506 or $output_char_id=&get_char_attribute($char,"=ucs")
507 or $output_char_id=&get_char_attribute($char,"=>$out_cs")
508 or $output_char_id=&get_char_attribute($char,"=>ucs")
509 or $output_char_id=&get_char_attribute($char,"=>ucs*")
511 $output_ids.=pack("U",$output_char_id);
521 # return: character in output coding system or TeX macro or undef.
522 my($char,$out_cs)=@_;
523 my($out_char_id,$suffix);
526 foreach $out_cs (@{$order{$out_cs}}){
527 if($out_cs eq 'UniJIS'
528 and &get_char_attribute($char,"vnd-adobe-cid-unijis-utf16-h")){
529 if($out_char_id=&get_char_attribute($char,'=ucs@jis')
530 or $out_char_id=&get_char_attribute($char,'=ucs')
531 or $out_char_id=&get_char_attribute($char,'=>ucs@jis')
532 or $out_char_id=&get_char_attribute($char,'=>ucs')
533 or $out_char_id=&get_char_attribute($char,'=>ucs*')
535 return "{\\selectjisfont\\char$out_char_id}";
537 }elsif($out_cs eq 'UniGB'
538 and &get_char_attribute($char,"vnd-adobe-cid-unigb-ucs2-h")){
539 if($out_char_id=&get_char_attribute($char,'=ucs@gb')
540 or $out_char_id=&get_char_attribute($char,'=ucs')
541 or $out_char_id=&get_char_attribute($char,'=>ucs@gb')
542 or $out_char_id=&get_char_attribute($char,'=>ucs')
543 or $out_char_id=&get_char_attribute($char,'=>ucs*')
545 return "{\\selectgbsfont\\char$out_char_id}";
547 }elsif($out_cs eq 'UniCNS'
548 and &get_char_attribute($char,"vnd-adobe-cid-unicns-ucs2-h")){
549 if($out_char_id=&get_char_attribute($char,'=ucs@cns')
550 or $out_char_id=&get_char_attribute($char,'=ucs')
551 or $out_char_id=&get_char_attribute($char,'=>ucs@cns')
552 or $out_char_id=&get_char_attribute($char,'=>ucs')
553 or $out_char_id=&get_char_attribute($char,'=>ucs*')
555 return "{\\selectcnsfont\\char$out_char_id}";
557 }elsif($out_cs eq 'UniKS'
558 and &get_char_attribute($char,"vnd-adobe-cid-uniks-ucs2-h")){
559 if($out_char_id=&get_char_attribute($char,'=ucs@ks')
560 or $out_char_id=&get_char_attribute($char,'=ucs')
561 or $out_char_id=&get_char_attribute($char,'=>ucs@ks')
562 or $out_char_id=&get_char_attribute($char,'=>ucs')
563 or $out_char_id=&get_char_attribute($char,'=>ucs*')
565 return "{\\selectksxfont\\char$out_char_id}";
567 }elsif($out_cs eq 'GT'){
568 return $gt if($gt=&get_macro_for_GT($char));
569 }elsif($out_cs eq 'HZK'){
570 return $hzk if($hzk=&get_macro_for_HZK($char));
571 }elsif($out_cs eq 'CDP'){
572 return $cdp if($cdp=&get_macro_for_CDP($char));
579 # argument: <character>
583 # $ids=&get_char_attribute($char,"ids-aggregated")
584 # or $ids=&get_char_attribute($char,"ids");
585 $ids=&get_char_attribute($char,"ids-decomposed")
586 or $ids=&get_char_attribute($char,"ids")
587 or $ids=&get_char_attribute($char,"ideographic-structure");
592 sub get_char_for_ids{
593 # argument: <ideographic description sequence>
594 # return: char or undef.
598 if(($output_char)=&get_chars_matching("ids",$ids)){
606 # argument: <char>, <input coding system>
607 # return: char in ucs@mcs.
611 return $char if($in_cs eq 'ucs@mcs');
613 if(($output_char)=&get_chars_matching("=$in_cs",unpack("U",$char))){
620 sub get_chars_unified{
622 my($chars,$ucs,$char_ucs,$char_sub);
625 if($chars=&get_char_attribute($char,'->ucs-unified')){
626 $chars=~s/^\((.*)\)$/$1/;
627 return (split(/\s*\?/,$chars));
628 }elsif($ucs=&get_char_attribute($char,'=>ucs*')
629 or $ucs=&get_char_attribute($char,'=>ucs')){
630 $char_ucs=pack("U",$ucs);
631 if($chars=&get_char_attribute($char_ucs,'->ucs-unified')){
632 $chars=~s/^\((.*)\)$/$1/;
633 @chars=grep {not /^$char$/}
634 (split(/\s*\?/,$chars));
635 push(@chars,$char_ucs);
638 }elsif($char_sub=&get_char_attribute($char,'<-subsumptive')){
639 $char_sub=~s/[? ()]//g;
640 $chars=&get_char_attribute($char_sub,'->subsumptive');
641 @chars=grep {not /^$char$/} (split(/\s*\?/,$chars));
642 push(@chars,$char_sub);
649 sub get_macro_for_GT{
651 # return: TeX macro for GT fonts or undef.
655 if($gt=&get_char_attribute($char,$_)){
656 m/gt\-pj\-(\d+)/ and $GT=$1;
661 return "{\\fontencoding{OT1}\\fontfamily{"
662 .sprintf("gt%02d",$GT)
663 ."}\\selectfont\\char".($gt|0x8080)."}";
669 sub get_macro_for_HZK{
671 # return: TeX macro for Hanziku fonts or undef.
675 if($hzk=&get_char_attribute($char,$_)){
676 m/hanziku\-(\d+)/ and $HZK=$1;
681 return "{\\fontencoding{OT1}\\fontfamily{"
682 .sprintf("hzk%02d",$HZK)
683 ."}\\selectfont\\char".$hzk."}";
689 sub get_macro_for_CDP{
691 # return: TeX macro for CDP fonts or undef.
695 if($cdp=&get_char_attribute($char,$_)){
700 $ucs=&get_char_attribute(&get_chars_matching("=big5-pua",$cdp),"=ucs");
702 return "{\\fontencoding{OT1}\\fontfamily{cdp}\\selectfont\\char$ucs}";
704 print STDERR "This should not happen.\n";
705 print STDERR "ucs code point of CDP: $cdp not found.\n";