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]/){
222 }elsif(($char_id>0x20 and $char_id<=0x021f)
226 # Latin Extended-B (not all)
227 or($char_id>=0x0250 and $char_id<=0x02af)
229 or($char_id>=0x0300 and $char_id<=0x033f)
230 or($char_id>=0x0360 and $char_id<=0x036f)
231 # Combining Diacritical Marks
232 or($char_id>=0x1e00 and $char_id<=0x1eff)
233 # Latin Extended Additional
234 or($char_id>=0x0370 and $char_id<=0x03ff)
236 or($char_id>=0x0400 and $char_id<=0x04ff)
238 or($char_id>=0x0530 and $char_id<=0x058f)
240 or($char_id>=0x2010 and $char_id<=0x2046)
241 # General Punctuation (partial)
243 print &latin_parse();
245 }elsif($char_id>=0x2ff0 and $char_id<=0x2fff){
246 # Ideographic Description Characters
250 if(($out_char=&get_output_char($char,$out_cs))){
251 print $out_char,&add_break($i);
253 if($opt_allow_unify){
254 @chars_unified=&get_chars_unified($char);
255 if(@chars_unified>0){
256 foreach $char_unified (@chars_unified){
258 =&get_output_char($char_unified,$out_cs))){
259 print $out_char,&add_break($i);
265 if($opt_use_kage_for_Ext_B){
266 if($char_id >= 0x20000 && $char_id <=0x2a6df){
267 # CJK Unified Ideographs Extension B
268 if(not defined($ids{$char}) and $ids{$char}[1]>=0){
269 $ids{$char}[0]=$font_start;
270 $ids{$char}[1]=$ids_start;
277 print "{\\fontencoding{OT1}\\fontfamily{",
278 sprintf("chise%03d",$ids{$char}[0]) ,
279 "}\\selectfont\\char$ids{$char}[1]}",&add_break($i);
283 if($ids=&get_ids($char)){
284 print &get_macro_for_ids($ids),&add_break($i);
286 print '\rule{1ex}{1ex}',&add_break($i);
293 print IDSDATA 'use utf8;',"\n";
294 foreach $ids (keys %ids){
295 print IDSDATA '$ids{\'',$ids,'\'}='
296 ,'[',join ",",@{$ids{$ids}},"];\n";
298 print IDSDATA '$font_start=',$font_start,";\n";
299 print IDSDATA '$ids_start=',$ids_start,";\n";
301 flock(IDSDATA,LOCK_UN);
308 my($output_char,$atr,$value);
309 $er=~/^(amp)(.*)(;)$/
310 and $prefix=$1,$er=$2,$suffix=$3;
311 $prefix or $prefix="",$suffix or $suffix="";
312 if($er=~/^U[\+|\-]([a-fA-F\d]+)/){
313 $output_char=pack("U",hex($1));
314 }elsif($er=~/^(?:I\-)?($er_prefix_re)\-?([0-9a-fA-F]+)$/){
315 ($atr,$value)=($1,$2);
316 unless($er_alias{$atr}=~/daikanwa|gt/){
319 ($output_char)=&get_chars_matching($er_alias{$atr},$value);
324 return $prefix.$er.$suffix;
332 if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
333 and($chars[$i+2]=~m/[$strictly_forbidden_before]/o)){
334 return "\\CJKunbreakablekernone{}";
335 }elsif($opt_protrude){
336 if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
337 and($chars[$i+2]=~m/[^$strictly_forbidden_before]/o)){
338 return "\\CJKunbreakablekernone \\CJKprotrude{}";
343 and($chars[$i+1]eq" ")){
348 and($chars[$i+1]=~m/[$strictly_forbidden_before]/o)){
349 return "\\CJKunbreakablekernone{}";
351 if($chars[$i]=~m/[$strictly_forbidden_after]/o){
352 return "\\CJKunbreakablekernone{}";
355 and($chars[$i+1]=~m/[$forbidden_before]/o)){
356 return "\\CJKunbreakablekerntwo{}";
359 if($chars[$i]=~m/[$forbidden_after]/o){
360 return "\\CJKunbreakablekerntwo{}";
363 and($chars[$i+1]=~m/[$slightly_forbidden_before]/o)){
364 return "\\CJKunbreakablekernthree{}";
366 if($chars[$i]=~m/[$slightly_forbidden_after]/o){
367 return "\\CJKunbreakablekernthree{}";
369 if($chars[$i]=~m/[$asian]/o){
370 return "\\CJKbreakablekern{}";
372 if(($i<$#chars)and($chars[$i+1]=~m/[$asian]/o)){
373 return "\\CJKbreakablekern{}";
379 # return: string for output with TeX macro.
383 $char_id=unpack("U",$chars[$i]);
384 if($chars[$i]=~m/($tex_meta_re)/o){
385 $out_str.=$tex_meta{$chars[$i]};
386 }elsif(($char_id>0x20 and $char_id<=0x021f)
387 or($char_id>=0x0250 and $char_id<=0x02af)# IPA Extensions
388 or($char_id>=0x0300 and $char_id<=0x033f)# Combining Diacritical Marks
389 or($char_id>=0x0360 and $char_id<=0x036f)
390 or($char_id>=0x0370 and $char_id<=0x03ff)# Greek and Coptic
391 or($char_id>=0x0400 and $char_id<=0x04ff)# Cyrillic
392 or($char_id>=0x0530 and $char_id<=0x058f)# Armenian
394 $out_str.=$chars[$i];
395 }elsif($char_id>=0x1e00 and $char_id<=0x1eff){
396 # Latin Extended Additional
397 # 0x1e00 -> 0x0600, etc.
398 $out_str.=pack("U",$char_id-0x1800);
399 }elsif($char_id>=0x2010 and $char_id<=0x2046){
400 # General Punctuation (partial)
401 $out_str.=pack("U",$char_id-0x1000);
408 return '{\fontencoding{OT1}\fontfamily{omlgc}\selectfont '.$out_str.'}';###UT1?
413 # return: character for output,
415 # or GETA character if ids is invalid.
416 my($ids,$ids_argc)=&ids_rest("",0,$chars[$i]);
422 print STDERR "IDS parse error: $ids\n";
426 ($ids,$ids_argc)=&ids_rest($ids,$ids_argc,$chars[$i]);
428 if(($char=&get_char_for_ids($ids))
429 and($out_char=&get_output_char($char,$out_cs))){
432 return &get_macro_for_ids($ids);
439 # arguments: <ids>, <rest number of arguments for ids>, <character>
440 # return: ids and rest number of arguments for ids.
441 my($ids,$ids_argc,$char)=@_;
443 $argc=&ids_argc($char);
445 $ids_argc+=$ids_argc==0?$argc:$argc-1;
450 return ($ids,$ids_argc);
453 sub get_macro_for_ids{
455 # return: TeX macro for ids
456 # or GETA character if ids is invalid for KAGE.
458 # $ids=&normalize_ids($ids,"UniJIS");
459 return $geta if(($ids!~/[$idc]/)
460 or($ids=~/[\x{10000}-]/));
462 if(not defined($ids{$ids})){
463 $ids{$ids}[0]=$font_start;
464 $ids{$ids}[1]=$ids_start;
471 return "{\\fontencoding{OT1}\\fontfamily{"
472 .sprintf("chise%03d",$ids{$ids}[0])
473 ."}\\selectfont\\char$ids{$ids}[1]}";
477 # argument: <ids>, <output coding system>
478 # return: ids or GETA character if ids is invalid for KAGE.
480 $out_cs=~s/Uni(.+)/'ucs@'.lc($1)/e;
483 my($char,$output_char_id);
484 while($ids=~m/(.)/g){
488 }elsif($output_char_id=&get_char_attribute($char,"=$out_cs")
489 or $output_char_id=&get_char_attribute($char,"=ucs")
490 or $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,"=>ucs*")
494 $output_ids.=pack("U",$output_char_id);
504 # return: character in output coding system or TeX macro or undef.
505 my($char,$out_cs)=@_;
506 my($out_char_id,$suffix);
509 foreach $out_cs (@{$order{$out_cs}}){
510 if($out_cs eq 'UniJIS'
511 and &get_char_attribute($char,"vnd-adobe-cid-unijis-utf16-h")){
512 if($out_char_id=&get_char_attribute($char,'=ucs@jis')
513 or $out_char_id=&get_char_attribute($char,'=ucs')
514 or $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*')
518 return "{\\selectjisfont\\char$out_char_id}";
520 }elsif($out_cs eq 'UniGB'
521 and &get_char_attribute($char,"vnd-adobe-cid-unigb-ucs2-h")){
522 if($out_char_id=&get_char_attribute($char,'=ucs@gb')
523 or $out_char_id=&get_char_attribute($char,'=ucs')
524 or $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*')
528 return "{\\selectgbsfont\\char$out_char_id}";
530 }elsif($out_cs eq 'UniCNS'
531 and &get_char_attribute($char,"vnd-adobe-cid-unicns-ucs2-h")){
532 if($out_char_id=&get_char_attribute($char,'=ucs@cns')
533 or $out_char_id=&get_char_attribute($char,'=ucs')
534 or $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*')
538 return "{\\selectcnsfont\\char$out_char_id}";
540 }elsif($out_cs eq 'UniKS'
541 and &get_char_attribute($char,"vnd-adobe-cid-uniks-ucs2-h")){
542 if($out_char_id=&get_char_attribute($char,'=ucs@ks')
543 or $out_char_id=&get_char_attribute($char,'=ucs')
544 or $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*')
548 return "{\\selectksxfont\\char$out_char_id}";
550 }elsif($out_cs eq 'GT'){
551 return $gt if($gt=&get_macro_for_GT($char));
552 }elsif($out_cs eq 'HZK'){
553 return $hzk if($hzk=&get_macro_for_HZK($char));
554 }elsif($out_cs eq 'CDP'){
555 return $cdp if($cdp=&get_macro_for_CDP($char));
562 # argument: <character>
566 $ids=&get_char_attribute($char,"ids-aggregated")
567 or $ids=&get_char_attribute($char,"ids");
568 # $ids=&get_char_attribute($char,"ids-decomposed")
569 # or $ids=&get_char_attribute($char,"ids");
570 # or $ids=&get_char_attribute($char,"ideographic-structure");
575 sub get_char_for_ids{
576 # argument: <ideographic description sequence>
577 # return: char or undef.
581 if(($output_char)=&get_chars_matching("ids",$ids)){
589 # argument: <char>, <input coding system>
590 # return: char in ucs@mcs.
594 return $char if($in_cs eq 'ucs@mcs');
596 if(($output_char)=&get_chars_matching("=$in_cs",unpack("U",$char))){
603 sub get_chars_unified{
605 my($chars,$ucs,$char_ucs);
608 if($chars=&get_char_attribute($char,'->ucs-unified')){
609 $chars=~s/^\((.*)\)$/$1/;
610 return (split(/\s*\?/,$chars));
611 }elsif($ucs=&get_char_attribute($char,'=>ucs*')
612 or $ucs=&get_char_attribute($char,'=>ucs')){
613 $char_ucs=pack("U",$ucs);
614 if($chars=&get_char_attribute($char_ucs,'->ucs-unified')){
615 $chars=~s/^\((.*)\)$/$1/;
616 @chars=grep {not /^$char$/}
617 (split(/\s*\?/,$chars));
618 push(@chars,$char_ucs);
624 sub get_macro_for_GT{
626 # return: TeX macro for GT fonts or undef.
630 if($gt=&get_char_attribute($char,$_)){
631 m/gt\-pj\-(\d+)/ and $GT=$1;
636 return "{\\fontencoding{OT1}\\fontfamily{"
637 .sprintf("gt%02d",$GT)
638 ."}\\selectfont\\char".($gt|0x8080)."}";
644 sub get_macro_for_HZK{
646 # return: TeX macro for Hanziku fonts or undef.
650 if($hzk=&get_char_attribute($char,$_)){
651 m/hanziku\-(\d+)/ and $HZK=$1;
656 return "{\\fontencoding{OT1}\\fontfamily{"
657 .sprintf("hzk%02d",$HZK)
658 ."}\\selectfont\\char".$hzk."}";
664 sub get_macro_for_CDP{
666 # return: TeX macro for CDP fonts or undef.
670 if($cdp=&get_char_attribute($char,$_)){
675 $ucs=&get_char_attribute(&get_chars_matching("=big5-pua",$cdp),"=ucs");
677 return "{\\fontencoding{OT1}\\fontfamily{cdp}\\selectfont\\char$ucs}";
679 print STDERR "This should not happen.\n";
680 print STDERR "ucs code point of CDP: $cdp not found.\n";