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.
64 my $asian = '\x{2E80}-\x{312f}\x{3190}-\x{ABFF}\x{F900}-\x{FAFF}\x{FE30}-\x{FE4F}\x{FF00}-\x{FFFFFF}'; # need to fix.
66 my $space = '\x{0020}\x{0009}\x{000A}\x{000C}\x{000D}';
68 my $hangul='\x{1100}-\x{11ff}\x{3130}-\x{318f}\x{AC00}-\x{D7AF}';
70 my %tex_meta=('#'=>'\#',
71 '$'=>'\\textdollar{}',
74 '{'=>'\\textbraceleft{}',
75 '}'=>'\\textbraceright{}',
76 '\\'=>'\\textbackslash{}',
77 '_'=>'\\textunderscore',
80 my $tex_meta_re=join('|',map {quotemeta($_)} keys %tex_meta);
82 &GetOptions("in=s"=>\$opt_in_cs,
83 "out=s"=>\$opt_out_cs,
87 Usage: $0 -i <input coding system> -o <cmap encoding>
89 Utf8mcs, Utf8cns, Utf8gb, Utf8jis, Utf8ks, Utf8big5
91 UniCNS, UniGB, UniJIS, UniKS, UniMulti
94 if($opt_in_cs or $opt_out_cs){
98 ($in_cs,$out_cs)=($0=~/(Utf8.+)To(\w+)/);
102 # Utf8mcs,Utf8cns,Utf8gb,Utf8jis,Utf8ks,Utf8big5
104 # UniCNS,UniGB,UniJIS,UniKS,UniMulti
106 $in_cs=~s/Utf8/ucs\@/;
109 or not defined($in_cs)
110 or not defined($out_cs)){
115 $omegadb_path=~s!/$!!;
117 $idsdata_file="$omegadb_path/idsdata.pl";
121 if(-e $idsdata_file){
122 open(IDSDATA,"+<:utf8",$idsdata_file) or die;
123 flock(IDSDATA,LOCK_EX);
129 # require $idsdata_file;
131 open(IDSDATA,">:utf8",$idsdata_file) or die;
132 flock(IDSDATA,LOCK_EX);
139 #$geta=pack("U",0x3013);
140 $geta=pack("U",0xfffd);
143 "=gt-pj-1","=gt-pj-2","=gt-pj-3","=gt-pj-4","=gt-pj-5",
144 "=gt-pj-6","=gt-pj-7","=gt-pj-8","=gt-pj-9","=gt-pj-10",
146 #,"=gt-pj-k1","=gt-pj-k2"
148 @HZK=("=hanziku-1","=hanziku-2","=hanziku-3","=hanziku-4",
149 "=hanziku-5","=hanziku-6","=hanziku-7","=hanziku-8",
150 "=hanziku-9","=hanziku-10","=hanziku-11","=hanziku-12");
153 %order_map=('c'=>'UniCNS',
162 foreach $out_cs ('UniCNS','UniGB','UniJIS','UniKS','UniMulti'){
163 if(defined($opt_order{$out_cs})){
164 if($opt_order{$out_cs}=~/^[cgjkGHC]+$/){
165 @{$order{$out_cs}}=map {$order_map{$_}}
166 (split(//,$opt_order{$out_cs}));
168 print STDERR "Invalid order for $out_cs!\n";
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;
182 s/([$asian])\s+/$1/go unless($out_cs eq 'UniKS');
183 s/\s+([$asian])/$1/go unless($out_cs eq 'UniKS');
184 s/([$asian])\s*([^$asian$space])/$1 $2/go;
185 s/([^$asian$idc])\s*([$asian])/$1 $2/go;
186 s/\-\-\-/pack("U",0x2014)/geo;# EM DASH
187 s/\-\-/pack("U",0x2013)/geo;# EN DASH
188 # s/\`\`/pack("U",0x201f)/geo;# DOUBLE HIGH-REVERSED-9 QUOTATION MARK
189 s/\`\`/pack("U",0x201c)/geo;# _TeX compatible_
190 # s/\`/pack("U",0x201b)/geo;# SINGLE HIGH-REVERSED-9 QUOTATION MARK
191 s/\`/pack("U",0x2018)/geo;# _TeX compatible_
192 s/\'\'/pack("U",0x201d)/geo;# RIGHT DOUBLE QUOTATION MARK
193 s/\'/pack("U",0x2019)/geo;# RIGHT DOUBLE QUOTATION MARK
196 for($i=0;$i<=$#chars;$i++){
198 $char_id=unpack("U",$char);
201 # add quarter space between asian and non-asian.
202 if($i>0 and $i<$#chars){
203 if(($chars[$i-1]=~/[$asian]/
204 and $chars[$i+1]=~/[^$asian]/
205 and $chars[$i+1]=~/[$strictly_forbidden_before$slightly_forbidden_before]/)
206 or($chars[$i-1]=~/[^$asian]/
207 and $chars[$i+1]=~/[$asian]/
208 and $chars[$i-1]=~/[$strictly_forbidden_after$slightly_forbidden_after]/)
211 }elsif(($chars[$i-1]=~/[$asian]/
212 and $chars[$i+1]=~/[^$asian]/)){
213 print '\unskip\kern.25ex';
214 }elsif($chars[$i-1]=~/[^$asian]/
215 and $chars[$i+1]=~/[$asian]/){
224 }elsif(($char_id>0x20 and $char_id<=0x021f)
228 # Latin Extended-B (not all)
229 or($char_id>=0x0250 and $char_id<=0x02af)
231 or($char_id>=0x0300 and $char_id<=0x033f)
232 or($char_id>=0x0360 and $char_id<=0x036f)
233 # Combining Diacritical Marks
234 or($char_id>=0x1e00 and $char_id<=0x1eff)
235 # Latin Extended Additional
236 or($char_id>=0x0370 and $char_id<=0x03ff)
238 or($char_id>=0x0400 and $char_id<=0x04ff)
240 or($char_id>=0x0530 and $char_id<=0x058f)
242 or($char_id>=0x2010 and $char_id<=0x2046)
243 # General Punctuation (partial)
245 print &latin_parse();
247 }elsif($char_id>=0x2ff0 and $char_id<=0x2fff){
248 # Ideographic Description Characters
252 if(($out_char=&get_output_char($char,$out_cs))){
253 print $out_char,&add_break($i);
255 if($opt_allow_unify){
256 @chars_unified=&get_chars_unified($char);
257 if(@chars_unified>0){
258 foreach $char_unified (@chars_unified){
260 =&get_output_char($char_unified,$out_cs))){
261 print $out_char,&add_break($i);
267 if($opt_use_kage_for_Ext_B){
268 if($char_id >= 0x20000 && $char_id <=0x2a6df){
269 # CJK Unified Ideographs Extension B
270 if(not defined($ids{$char}) and $ids{$char}[1]>=0){
271 $ids{$char}[0]=$font_start;
272 $ids{$char}[1]=$ids_start;
279 print "{\\fontencoding{OT1}\\fontfamily{",
280 sprintf("chise%03d",$ids{$char}[0]) ,
281 "}\\selectfont\\char$ids{$char}[1]}",&add_break($i);
285 if($ids=&get_ids($char)){
286 print &get_macro_for_ids($ids),&add_break($i);
288 print '\rule{1ex}{1ex}',&add_break($i);
295 print IDSDATA 'use utf8;',"\n";
296 foreach $ids (keys %ids){
297 print IDSDATA '$ids{\'',$ids,'\'}='
298 ,'[',join ",",@{$ids{$ids}},"];\n";
300 print IDSDATA '$font_start=',$font_start,";\n";
301 print IDSDATA '$ids_start=',$ids_start,";\n";
303 flock(IDSDATA,LOCK_UN);
310 my($output_char,$atr,$value);
311 $er=~/^(amp)(.*)(;)$/
312 and $prefix=$1,$er=$2,$suffix=$3;
313 $prefix or $prefix="",$suffix or $suffix="";
314 if($er=~/^U[\+|\-]([a-fA-F\d]+)/){
315 $output_char=pack("U",hex($1));
316 }elsif($er=~/^(?:I\-)?($er_prefix_re)\-?([0-9a-fA-F]+)$/){
317 ($atr,$value)=($1,$2);
318 unless($er_alias{$atr}=~/daikanwa|gt/){
321 ($output_char)=&get_chars_matching($er_alias{$atr},$value);
326 return $prefix.$er.$suffix;
334 if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
335 and($chars[$i+2]=~m/[$strictly_forbidden_before]/o)){
336 return "\\CJKunbreakablekernone{}";
337 }elsif($opt_protrude){
338 if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
339 and($chars[$i+2]=~m/[^$strictly_forbidden_before]/o)){
340 return "\\CJKunbreakablekernone \\CJKprotrude{}";
345 and($chars[$i+1]eq" ")){
350 and($chars[$i+1]=~m/[$strictly_forbidden_before]/o)){
351 return "\\CJKunbreakablekernone{}";
353 if($chars[$i]=~m/[$strictly_forbidden_after]/o){
354 return "\\CJKunbreakablekernone{}";
357 and($chars[$i+1]=~m/[$forbidden_before]/o)){
358 return "\\CJKunbreakablekerntwo{}";
361 if($chars[$i]=~m/[$forbidden_after]/o){
362 return "\\CJKunbreakablekerntwo{}";
365 and($chars[$i+1]=~m/[$slightly_forbidden_before]/o)){
366 return "\\CJKunbreakablekernthree{}";
368 if($chars[$i]=~m/[$slightly_forbidden_after]/o){
369 return "\\CJKunbreakablekernthree{}";
371 if($chars[$i]=~m/[$asian]/o){
372 return "\\CJKbreakablekern{}";
374 if(($i<$#chars)and($chars[$i+1]=~m/[$asian]/o)){
375 return "\\CJKbreakablekern{}";
381 # return: string for output with TeX macro.
385 $char_id=unpack("U",$chars[$i]);
386 if($chars[$i]=~m/($tex_meta_re)/o){
387 $out_str.=$tex_meta{$chars[$i]};
388 }elsif(($char_id>0x20 and $char_id<=0x021f)
389 or($char_id>=0x0250 and $char_id<=0x02af)# IPA Extensions
390 or($char_id>=0x0300 and $char_id<=0x033f)# Combining Diacritical Marks
391 or($char_id>=0x0360 and $char_id<=0x036f)
392 or($char_id>=0x0370 and $char_id<=0x03ff)# Greek and Coptic
393 or($char_id>=0x0400 and $char_id<=0x04ff)# Cyrillic
394 or($char_id>=0x0530 and $char_id<=0x058f)# Armenian
396 $out_str.=$chars[$i];
397 }elsif($char_id>=0x1e00 and $char_id<=0x1eff){
398 # Latin Extended Additional
399 # 0x1e00 -> 0x0600, etc.
400 $out_str.=pack("U",$char_id-0x1800);
401 }elsif($char_id>=0x2010 and $char_id<=0x2046){
402 # General Punctuation (partial)
403 $out_str.=pack("U",$char_id-0x1000);
410 return '{\fontencoding{OT1}\fontfamily{omlgc}\selectfont '.$out_str.'}';###UT1?
415 # return: character for output,
417 # or GETA character if ids is invalid.
418 my($ids,$ids_argc)=&ids_rest("",0,$chars[$i]);
424 print STDERR "IDS parse error: $ids\n";
428 ($ids,$ids_argc)=&ids_rest($ids,$ids_argc,$chars[$i]);
430 if(($char=&get_char_for_ids($ids))
431 and($out_char=&get_output_char($char,$out_cs))){
434 return &get_macro_for_ids($ids);
441 # arguments: <ids>, <rest number of arguments for ids>, <character>
442 # return: ids and rest number of arguments for ids.
443 my($ids,$ids_argc,$char)=@_;
445 $argc=&ids_argc($char);
447 $ids_argc+=$ids_argc==0?$argc:$argc-1;
452 return ($ids,$ids_argc);
455 sub get_macro_for_ids{
457 # return: TeX macro for ids
458 # or GETA character if ids is invalid for KAGE.
460 # $ids=&normalize_ids($ids,"UniJIS");
461 return $geta if(($ids!~/[$idc]/)
462 or($ids=~/[\x{10000}-]/));
464 if(not defined($ids{$ids})){
465 $ids{$ids}[0]=$font_start;
466 $ids{$ids}[1]=$ids_start;
473 return "{\\fontencoding{OT1}\\fontfamily{"
474 .sprintf("chise%03d",$ids{$ids}[0])
475 ."}\\selectfont\\char$ids{$ids}[1]}";
479 # argument: <ids>, <output coding system>
480 # return: ids or GETA character if ids is invalid for KAGE.
482 $out_cs=~s/Uni(.+)/'ucs@'.lc($1)/e;
485 my($char,$output_char_id);
486 while($ids=~m/(.)/g){
490 }elsif($output_char_id=&get_char_attribute($char,"=$out_cs")
491 or $output_char_id=&get_char_attribute($char,"=ucs")
492 or $output_char_id=&get_char_attribute($char,"=>$out_cs")
493 or $output_char_id=&get_char_attribute($char,"=>ucs")
494 or $output_char_id=&get_char_attribute($char,"=>ucs*")
496 $output_ids.=pack("U",$output_char_id);
506 # return: character in output coding system or TeX macro or undef.
507 my($char,$out_cs)=@_;
508 my($out_char_id,$suffix);
511 foreach $out_cs (@{$order{$out_cs}}){
512 if($out_cs eq 'UniJIS'
513 and &get_char_attribute($char,"vnd-adobe-cid-unijis-utf16-h")){
514 if($out_char_id=&get_char_attribute($char,'=ucs@jis')
515 or $out_char_id=&get_char_attribute($char,'=ucs')
516 or $out_char_id=&get_char_attribute($char,'=>ucs@jis')
517 or $out_char_id=&get_char_attribute($char,'=>ucs')
518 or $out_char_id=&get_char_attribute($char,'=>ucs*')
520 return "{\\selectjisfont\\char$out_char_id}";
522 }elsif($out_cs eq 'UniGB'
523 and &get_char_attribute($char,"vnd-adobe-cid-unigb-ucs2-h")){
524 if($out_char_id=&get_char_attribute($char,'=ucs@gb')
525 or $out_char_id=&get_char_attribute($char,'=ucs')
526 or $out_char_id=&get_char_attribute($char,'=>ucs@gb')
527 or $out_char_id=&get_char_attribute($char,'=>ucs')
528 or $out_char_id=&get_char_attribute($char,'=>ucs*')
530 return "{\\selectgbsfont\\char$out_char_id}";
532 }elsif($out_cs eq 'UniCNS'
533 and &get_char_attribute($char,"vnd-adobe-cid-unicns-ucs2-h")){
534 if($out_char_id=&get_char_attribute($char,'=ucs@cns')
535 or $out_char_id=&get_char_attribute($char,'=ucs')
536 or $out_char_id=&get_char_attribute($char,'=>ucs@cns')
537 or $out_char_id=&get_char_attribute($char,'=>ucs')
538 or $out_char_id=&get_char_attribute($char,'=>ucs*')
540 return "{\\selectcnsfont\\char$out_char_id}";
542 }elsif($out_cs eq 'UniKS'
543 and &get_char_attribute($char,"vnd-adobe-cid-uniks-ucs2-h")){
544 if($out_char_id=&get_char_attribute($char,'=ucs@ks')
545 or $out_char_id=&get_char_attribute($char,'=ucs')
546 or $out_char_id=&get_char_attribute($char,'=>ucs@ks')
547 or $out_char_id=&get_char_attribute($char,'=>ucs')
548 or $out_char_id=&get_char_attribute($char,'=>ucs*')
550 return "{\\selectksxfont\\char$out_char_id}";
552 }elsif($out_cs eq 'GT'){
553 return $gt if($gt=&get_macro_for_GT($char));
554 }elsif($out_cs eq 'HZK'){
555 return $hzk if($hzk=&get_macro_for_HZK($char));
556 }elsif($out_cs eq 'CDP'){
557 return $cdp if($cdp=&get_macro_for_CDP($char));
564 # argument: <character>
568 $ids=&get_char_attribute($char,"ids-aggregated")
569 or $ids=&get_char_attribute($char,"ids");
570 # $ids=&get_char_attribute($char,"ids-decomposed")
571 # or $ids=&get_char_attribute($char,"ids");
572 # or $ids=&get_char_attribute($char,"ideographic-structure");
577 sub get_char_for_ids{
578 # argument: <ideographic description sequence>
579 # return: char or undef.
583 if(($output_char)=&get_chars_matching("ids",$ids)){
591 # argument: <char>, <input coding system>
592 # return: char in ucs@mcs.
596 return $char if($in_cs eq 'ucs@mcs');
598 if(($output_char)=&get_chars_matching("=$in_cs",unpack("U",$char))){
605 sub get_chars_unified{
607 my($chars,$ucs,$char_ucs);
610 if($chars=&get_char_attribute($char,'->ucs-unified')){
611 $chars=~s/^\((.*)\)$/$1/;
612 return (split(/\s*\?/,$chars));
613 }elsif($ucs=&get_char_attribute($char,'=>ucs*')
614 or $ucs=&get_char_attribute($char,'=>ucs')){
615 $char_ucs=pack("U",$ucs);
616 if($chars=&get_char_attribute($char_ucs,'->ucs-unified')){
617 $chars=~s/^\((.*)\)$/$1/;
618 @chars=grep {not /^$char$/}
619 (split(/\s*\?/,$chars));
620 push(@chars,$char_ucs);
626 sub get_macro_for_GT{
628 # return: TeX macro for GT fonts or undef.
632 if($gt=&get_char_attribute($char,$_)){
633 m/gt\-pj\-(\d+)/ and $GT=$1;
638 return "{\\fontencoding{OT1}\\fontfamily{"
639 .sprintf("gt%02d",$GT)
640 ."}\\selectfont\\char".($gt|0x8080)."}";
646 sub get_macro_for_HZK{
648 # return: TeX macro for Hanziku fonts or undef.
652 if($hzk=&get_char_attribute($char,$_)){
653 m/hanziku\-(\d+)/ and $HZK=$1;
658 return "{\\fontencoding{OT1}\\fontfamily{"
659 .sprintf("hzk%02d",$HZK)
660 ."}\\selectfont\\char".$hzk."}";
666 sub get_macro_for_CDP{
668 # return: TeX macro for CDP fonts or undef.
672 if($cdp=&get_char_attribute($char,$_)){
677 $ucs=&get_char_attribute(&get_chars_matching("=big5-pua",$cdp),"=ucs");
679 return "{\\fontencoding{OT1}\\fontfamily{cdp}\\selectfont\\char$ucs}";
681 print STDERR "This should not happen.\n";
682 print STDERR "ucs code point of CDP: $cdp not found.\n";