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",0x201b)/geo;# SINGLE HIGH-REVERSED-9 QUOTATION MARK
190 s/\'\'/pack("U",0x201d)/geo;# RIGHT DOUBLE QUOTATION MARK
191 s/\'/pack("U",0x2019)/geo;# RIGHT DOUBLE QUOTATION MARK
194 for($i=0;$i<=$#chars;$i++){
196 $char_id=unpack("U",$char);
199 # add quarter space between asian and non-asian.
200 if($i>0 and $i<$#chars){
201 if(($chars[$i-1]=~/[$asian]/
202 and $chars[$i+1]=~/[^$asian]/
203 and $chars[$i+1]=~/[$strictly_forbidden_before$slightly_forbidden_before]/)
204 or($chars[$i-1]=~/[^$asian]/
205 and $chars[$i+1]=~/[$asian]/
206 and $chars[$i-1]=~/[$strictly_forbidden_after$slightly_forbidden_after]/)
209 }elsif(($chars[$i-1]=~/[$asian]/
210 and $chars[$i+1]=~/[^$asian]/)){
211 print '\unskip\kern.25ex';
212 }elsif($chars[$i-1]=~/[^$asian]/
213 and $chars[$i+1]=~/[$asian]/){
220 }elsif($char=~m/($tex_meta_re)/o){
223 }elsif(($char_id>0x20 and $char_id<=0x021f)
227 # Latin Extended-B (not all)
228 or($char_id>=0x0250 and $char_id<=0x02af)
230 or($char_id>=0x0300 and $char_id<=0x033f)
231 or($char_id>=0x0360 and $char_id<=0x036f)
232 # Combining Diacritical Marks
233 or($char_id>=0x1e00 and $char_id<=0x1eff)
234 # Latin Extended Additional
235 or($char_id>=0x0370 and $char_id<=0x03ff)
237 or($char_id>=0x0400 and $char_id<=0x04ff)
239 or($char_id>=0x0530 and $char_id<=0x058f)
241 or($char_id>=0x2010 and $char_id<=0x2046)
242 # General Punctuation (partial)
244 print &latin_parse();
246 }elsif($char_id>=0x2ff0 and $char_id<=0x2fff){
247 # Ideographic Description Characters
251 if(($out_char=&get_output_char($char,$out_cs))){
252 print $out_char,&add_break($i);
254 if($opt_allow_unify){
255 @chars_unified=&get_chars_unified($char);
256 if(@chars_unified>0){
257 foreach $char_unified (@chars_unified){
259 =&get_output_char($char_unified,$out_cs))){
260 print $out_char,&add_break($i);
266 if($opt_use_kage_for_Ext_B){
267 if($char_id >= 0x20000 && $char_id <=0x2a6df){
268 # CJK Unified Ideographs Extension B
269 if(not defined($ids{$char}) and $ids{$char}[1]>=0){
270 $ids{$char}[0]=$font_start;
271 $ids{$char}[1]=$ids_start;
278 print "{\\fontencoding{OT1}\\fontfamily{",
279 sprintf("chise%03d",$ids{$char}[0]) ,
280 "}\\selectfont\\char$ids{$char}[1]}",&add_break($i);
284 if($ids=&get_ids($char)){
285 print &get_macro_for_ids($ids),&add_break($i);
287 print '\rule{1ex}{1ex}',&add_break($i);
294 print IDSDATA 'use utf8;',"\n";
295 foreach $ids (keys %ids){
296 print IDSDATA '$ids{\'',$ids,'\'}='
297 ,'[',join ",",@{$ids{$ids}},"];\n";
299 print IDSDATA '$font_start=',$font_start,";\n";
300 print IDSDATA '$ids_start=',$ids_start,";\n";
302 flock(IDSDATA,LOCK_UN);
309 my($output_char,$atr,$value);
310 $er=~/^(amp)(.*)(;)$/
311 and $prefix=$1,$er=$2,$suffix=$3;
312 $prefix or $prefix="",$suffix or $suffix="";
313 if($er=~/^U[\+|\-]([a-fA-F\d]+)/){
314 $output_char=pack("U",hex($1));
315 }elsif($er=~/^(?:I\-)?($er_prefix_re)\-?([0-9a-fA-F]+)$/){
316 ($atr,$value)=($1,$2);
317 unless($er_alias{$atr}=~/daikanwa|gt/){
320 ($output_char)=&get_chars_matching($er_alias{$atr},$value);
325 return $prefix.$er.$suffix;
333 if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
334 and($chars[$i+2]=~m/[$strictly_forbidden_before]/o)){
335 return "\\CJKunbreakablekernone{}";
336 }elsif($opt_protrude){
337 if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
338 and($chars[$i+2]=~m/[^$strictly_forbidden_before]/o)){
339 return "\\CJKunbreakablekernone \\CJKprotrude{}";
344 and($chars[$i+1]eq" ")){
349 and($chars[$i+1]=~m/[$strictly_forbidden_before]/o)){
350 return "\\CJKunbreakablekernone{}";
352 if($chars[$i]=~m/[$strictly_forbidden_after]/o){
353 return "\\CJKunbreakablekernone{}";
356 and($chars[$i+1]=~m/[$forbidden_before]/o)){
357 return "\\CJKunbreakablekerntwo{}";
360 if($chars[$i]=~m/[$forbidden_after]/o){
361 return "\\CJKunbreakablekerntwo{}";
364 and($chars[$i+1]=~m/[$slightly_forbidden_before]/o)){
365 return "\\CJKunbreakablekernthree{}";
367 if($chars[$i]=~m/[$slightly_forbidden_after]/o){
368 return "\\CJKunbreakablekernthree{}";
370 if($chars[$i]=~m/[$asian]/o){
371 return "\\CJKbreakablekern{}";
373 if(($i<$#chars)and($chars[$i+1]=~m/[$asian]/o)){
374 return "\\CJKbreakablekern{}";
380 # return: string for output with TeX macro.
384 $char_id=unpack("U",$chars[$i]);
385 if(($char_id>0x20 and $char_id<=0x021f)
386 or($char_id>=0x0250 and $char_id<=0x02af)# IPA Extensions
387 or($char_id>=0x0300 and $char_id<=0x033f)# Combining Diacritical Marks
388 or($char_id>=0x0360 and $char_id<=0x036f)
389 or($char_id>=0x0370 and $char_id<=0x03ff)# Greek and Coptic
390 or($char_id>=0x0400 and $char_id<=0x04ff)# Cyrillic
391 or($char_id>=0x0530 and $char_id<=0x058f)# Armenian
393 $out_str.=$chars[$i];
394 }elsif($char_id>=0x1e00 and $char_id<=0x1eff){
395 # Latin Extended Additional
396 # 0x1e00 -> 0x0600, etc.
397 $out_str.=pack("U",$char_id-0x1800);
398 }elsif($char_id>=0x2010 and $char_id<=0x2046){
399 # General Punctuation (partial)
400 $out_str.=pack("U",$char_id-0x1000);
407 return '{\fontencoding{OT1}\fontfamily{omlgc}\selectfont '.$out_str.'}';###UT1?
412 # return: character for output,
414 # or GETA character if ids is invalid.
415 my($ids,$ids_argc)=&ids_rest("",0,$chars[$i]);
421 print STDERR "IDS parse error: $ids\n";
425 ($ids,$ids_argc)=&ids_rest($ids,$ids_argc,$chars[$i]);
427 if(($char=&get_char_for_ids($ids))
428 and($out_char=&get_output_char($char,$out_cs))){
431 return &get_macro_for_ids($ids);
438 # arguments: <ids>, <rest number of arguments for ids>, <character>
439 # return: ids and rest number of arguments for ids.
440 my($ids,$ids_argc,$char)=@_;
442 $argc=&ids_argc($char);
444 $ids_argc+=$ids_argc==0?$argc:$argc-1;
449 return ($ids,$ids_argc);
452 sub get_macro_for_ids{
454 # return: TeX macro for ids
455 # or GETA character if ids is invalid for KAGE.
457 # $ids=&normalize_ids($ids,"UniJIS");
458 return $geta if(($ids!~/[$idc]/)
459 or($ids=~/[\x{10000}-]/));
461 if(not defined($ids{$ids})){
462 $ids{$ids}[0]=$font_start;
463 $ids{$ids}[1]=$ids_start;
470 return "{\\fontencoding{OT1}\\fontfamily{"
471 .sprintf("chise%03d",$ids{$ids}[0])
472 ."}\\selectfont\\char$ids{$ids}[1]}";
476 # argument: <ids>, <output coding system>
477 # return: ids or GETA character if ids is invalid for KAGE.
479 $out_cs=~s/Uni(.+)/'ucs@'.lc($1)/e;
482 my($char,$output_char_id);
483 while($ids=~m/(.)/g){
487 }elsif($output_char_id=&get_char_attribute($char,"=$out_cs")
488 or $output_char_id=&get_char_attribute($char,"=ucs")
489 or $output_char_id=&get_char_attribute($char,"=>$out_cs")
490 or $output_char_id=&get_char_attribute($char,"=>ucs")
491 or $output_char_id=&get_char_attribute($char,"=>ucs*")
493 $output_ids.=pack("U",$output_char_id);
503 # return: character in output coding system or TeX macro or undef.
504 my($char,$out_cs)=@_;
505 my($out_char_id,$suffix);
508 foreach $out_cs (@{$order{$out_cs}}){
509 if($out_cs eq 'UniJIS'
510 and &get_char_attribute($char,"vnd-adobe-cid-unijis-utf16-h")){
511 if($out_char_id=&get_char_attribute($char,'=ucs@jis')
512 or $out_char_id=&get_char_attribute($char,'=ucs')
513 or $out_char_id=&get_char_attribute($char,'=>ucs@jis')
514 or $out_char_id=&get_char_attribute($char,'=>ucs')
515 or $out_char_id=&get_char_attribute($char,'=>ucs*')
517 return "{\\selectjisfont\\char$out_char_id}";
519 }elsif($out_cs eq 'UniGB'
520 and &get_char_attribute($char,"vnd-adobe-cid-unigb-ucs2-h")){
521 if($out_char_id=&get_char_attribute($char,'=ucs@gb')
522 or $out_char_id=&get_char_attribute($char,'=ucs')
523 or $out_char_id=&get_char_attribute($char,'=>ucs@gb')
524 or $out_char_id=&get_char_attribute($char,'=>ucs')
525 or $out_char_id=&get_char_attribute($char,'=>ucs*')
527 return "{\\selectgbsfont\\char$out_char_id}";
529 }elsif($out_cs eq 'UniCNS'
530 and &get_char_attribute($char,"vnd-adobe-cid-unicns-ucs2-h")){
531 if($out_char_id=&get_char_attribute($char,'=ucs@cns')
532 or $out_char_id=&get_char_attribute($char,'=ucs')
533 or $out_char_id=&get_char_attribute($char,'=>ucs@cns')
534 or $out_char_id=&get_char_attribute($char,'=>ucs')
535 or $out_char_id=&get_char_attribute($char,'=>ucs*')
537 return "{\\selectcnsfont\\char$out_char_id}";
539 }elsif($out_cs eq 'UniKS'
540 and &get_char_attribute($char,"vnd-adobe-cid-uniks-ucs2-h")){
541 if($out_char_id=&get_char_attribute($char,'=ucs@ks')
542 or $out_char_id=&get_char_attribute($char,'=ucs')
543 or $out_char_id=&get_char_attribute($char,'=>ucs@ks')
544 or $out_char_id=&get_char_attribute($char,'=>ucs')
545 or $out_char_id=&get_char_attribute($char,'=>ucs*')
547 return "{\\selectksxfont\\char$out_char_id}";
549 }elsif($out_cs eq 'GT'){
550 return $gt if($gt=&get_macro_for_GT($char));
551 }elsif($out_cs eq 'HZK'){
552 return $hzk if($hzk=&get_macro_for_HZK($char));
553 }elsif($out_cs eq 'CDP'){
554 return $cdp if($cdp=&get_macro_for_CDP($char));
561 # argument: <character>
565 $ids=&get_char_attribute($char,"ids-aggregated")
566 or $ids=&get_char_attribute($char,"ids");
567 # $ids=&get_char_attribute($char,"ids-decomposed")
568 # or $ids=&get_char_attribute($char,"ids");
569 # or $ids=&get_char_attribute($char,"ideographic-structure");
574 sub get_char_for_ids{
575 # argument: <ideographic description sequence>
576 # return: char or undef.
580 if(($output_char)=&get_chars_matching("ids",$ids)){
588 # argument: <char>, <input coding system>
589 # return: char in ucs@mcs.
593 return $char if($in_cs eq 'ucs@mcs');
595 if(($output_char)=&get_chars_matching("=$in_cs",unpack("U",$char))){
602 sub get_chars_unified{
604 my($chars,$ucs,$char_ucs);
607 if($chars=&get_char_attribute($char,'->ucs-unified')){
608 $chars=~s/^\((.*)\)$/$1/;
609 return (split(/\s*\?/,$chars));
610 }elsif($ucs=&get_char_attribute($char,'=>ucs*')
611 or $ucs=&get_char_attribute($char,'=>ucs')){
612 $char_ucs=pack("U",$ucs);
613 if($chars=&get_char_attribute($char_ucs,'->ucs-unified')){
614 $chars=~s/^\((.*)\)$/$1/;
615 @chars=grep {not /^$char$/}
616 (split(/\s*\?/,$chars));
617 push(@chars,$char_ucs);
623 sub get_macro_for_GT{
625 # return: TeX macro for GT fonts or undef.
629 if($gt=&get_char_attribute($char,$_)){
630 m/gt\-pj\-(\d+)/ and $GT=$1;
635 return "{\\fontencoding{OT1}\\fontfamily{"
636 .sprintf("gt%02d",$GT)
637 ."}\\selectfont\\char".($gt|0x8080)."}";
643 sub get_macro_for_HZK{
645 # return: TeX macro for Hanziku fonts or undef.
649 if($hzk=&get_char_attribute($char,$_)){
650 m/hanziku\-(\d+)/ and $HZK=$1;
655 return "{\\fontencoding{OT1}\\fontfamily{"
656 .sprintf("hzk%02d",$HZK)
657 ."}\\selectfont\\char".$hzk."}";
663 sub get_macro_for_CDP{
665 # return: TeX macro for CDP fonts or undef.
669 if($cdp=&get_char_attribute($char,$_)){
674 $ucs=&get_char_attribute(&get_chars_matching("=big5-pua",$cdp),"=ucs");
676 return "{\\fontencoding{OT1}\\fontfamily{cdp}\\selectfont\\char$ucs}";
678 print STDERR "This should not happen.\n";
679 print STDERR "ucs code point of CDP: $cdp not found.\n";