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'}='jcgk';
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 = '「【『[(〈“‘‘(〔{《{\[\(\x{3016}{「';
44 # \x{3016} | # white 【
46 my $forbidden_after = "\x{0000}";
49 my $slightly_forbidden_after = '¥¥$$〒♯##¢¢££@@§';
51 # $strictly_forbidden_before
52 # All these characters are allowed to protrude
54 my $strictly_forbidden_before=
55 '!,.:;?、。!,.:;?。\)#}’”〉》」』】〕\x{3017})]}}」\]';
56 ### \x{3017} | # white 】
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 fix.
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";
183 if($in_cs ne 'ucs@mcs'){
184 s/(.)/&get_char_in_mcs($1,$in_cs)/ge;
186 s/(amp.+?;)/&de_tex_er($1)/ge;
187 # s/(&.+?;)/&de_tex_er($1)/ge;
188 s/([$asian])\s+/$1/go unless($opt_preserve_spaces);
189 s/\s+([$asian])/$1/go unless($opt_preserve_spaces);
190 s/([$asian])\s*([^$asian$space])/$1 $2/go;
191 s/([^$asian$idc])\s*([$asian])/$1 $2/go;
192 s/\-\-\-/pack("U",0x2014)/geo;# EM DASH
193 s/\-\-/pack("U",0x2013)/geo;# EN DASH
194 # s/\`\`/pack("U",0x201f)/geo;# DOUBLE HIGH-REVERSED-9 QUOTATION MARK
195 s/\`\`/pack("U",0x201c)/geo;# _TeX compatible_
196 # s/\`/pack("U",0x201b)/geo;# SINGLE HIGH-REVERSED-9 QUOTATION MARK
197 s/\`/pack("U",0x2018)/geo;# _TeX compatible_
198 s/\'\'/pack("U",0x201d)/geo;# RIGHT DOUBLE QUOTATION MARK
199 s/\'/pack("U",0x2019)/geo;# RIGHT DOUBLE QUOTATION MARK
202 for($i=0;$i<=$#chars;$i++){
204 $char_id=unpack("U",$char);
207 # add quarter space between asian and non-asian.
208 if($i>0 and $i<$#chars){
209 if(($chars[$i-1]=~/[$asian]/
210 and $chars[$i+1]=~/[^$asian]/
211 and $chars[$i+1]=~/[$strictly_forbidden_before$slightly_forbidden_before]/)
212 or($chars[$i-1]=~/[^$asian]/
213 and $chars[$i+1]=~/[$asian]/
214 and $chars[$i-1]=~/[$strictly_forbidden_after$slightly_forbidden_after]/)
217 }elsif(($chars[$i-1]=~/[$asian]/
218 and $chars[$i+1]=~/[^$asian]/)){
219 print '\unskip\kern.25ex';
220 }elsif($chars[$i-1]=~/[^$asian]/
221 and $chars[$i+1]=~/[$asian]/){
230 }elsif(($char_id>0x20 and $char_id<=0x021f)
234 # Latin Extended-B (not all)
235 or($char_id>=0x0250 and $char_id<=0x02af)
237 or($char_id>=0x0300 and $char_id<=0x033f)
238 or($char_id>=0x0360 and $char_id<=0x036f)
239 # Combining Diacritical Marks
240 or($char_id>=0x1e00 and $char_id<=0x1eff)
241 # Latin Extended Additional
242 or($char_id>=0x0370 and $char_id<=0x03ff)
244 or($char_id>=0x0400 and $char_id<=0x04ff)
246 or($char_id>=0x0530 and $char_id<=0x058f)
248 or($char_id>=0x2010 and $char_id<=0x2046)
249 # General Punctuation (partial)
251 print &latin_parse();
253 }elsif($char_id>=0x2ff0 and $char_id<=0x2fff){
254 # Ideographic Description Characters
258 if(($out_char=&get_output_char($char,$out_cs))){
259 print $out_char,&add_break($i);
261 if($opt_allow_unify){
262 @chars_unified=&get_chars_unified($char);
263 if(@chars_unified>0){
264 foreach $char_unified (@chars_unified){
266 =&get_output_char($char_unified,$out_cs))){
267 print $out_char,&add_break($i);
273 if($opt_use_kage_for_Ext_B){
274 if($char_id >= 0x20000 && $char_id <=0x2a6df){
275 # CJK Unified Ideographs Extension B
276 if(not defined($ids{$char}) and $ids{$char}[1]>=0){
277 $ids{$char}[0]=$font_start;
278 $ids{$char}[1]=$ids_start;
285 print "{\\fontencoding{OT1}\\fontfamily{",
286 sprintf("chise%03d",$ids{$char}[0]) ,
287 "}\\selectfont\\char$ids{$char}[1]}",&add_break($i);
291 if($ids=&get_ids($char)){
292 print &get_macro_for_ids($ids),&add_break($i);
294 print '\rule{1ex}{1ex}',&add_break($i);
301 print IDSDATA 'use utf8;',"\n";
302 foreach $ids (keys %ids){
303 print IDSDATA '$ids{\'',$ids,'\'}='
304 ,'[',join ",",@{$ids{$ids}},"];\n";
306 print IDSDATA '$font_start=',$font_start,";\n";
307 print IDSDATA '$ids_start=',$ids_start,";\n";
309 flock(IDSDATA,LOCK_UN);
316 my($output_char,$atr,$value);
317 $er=~/^(amp)(.*)(;)$/
318 and $prefix=$1,$er=$2,$suffix=$3;
319 $prefix or $prefix="",$suffix or $suffix="";
320 if($er=~/^U[\+|\-]([a-fA-F\d]+)/){
321 $output_char=pack("U",hex($1));
322 }elsif($er=~/^(?:I\-)?($er_prefix_re)\-?([0-9a-fA-F]+)$/){
323 ($atr,$value)=($1,$2);
324 unless($er_alias{$atr}=~/daikanwa|gt/){
327 ($output_char)=&get_chars_matching($er_alias{$atr},$value);
332 return $prefix.$er.$suffix;
340 if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
341 and($chars[$i+2]=~m/[$strictly_forbidden_before]/o)){
342 return "\\CJKunbreakablekernone{}";
343 }elsif($opt_protrude){
344 if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
345 and($chars[$i+2]=~m/[^$strictly_forbidden_before]/o)){
346 return "\\CJKunbreakablekernone \\CJKprotrude{}";
351 and($chars[$i+1]eq" ")){
356 and($chars[$i+1]=~m/[$strictly_forbidden_before]/o)){
357 return "\\CJKunbreakablekernone{}";
359 if($chars[$i]=~m/[$strictly_forbidden_after]/o){
360 return "\\CJKunbreakablekernone{}";
363 and($chars[$i+1]=~m/[$forbidden_before]/o)){
364 return "\\CJKunbreakablekerntwo{}";
367 if($chars[$i]=~m/[$forbidden_after]/o){
368 return "\\CJKunbreakablekerntwo{}";
371 and($chars[$i+1]=~m/[$slightly_forbidden_before]/o)){
372 return "\\CJKunbreakablekernthree{}";
374 if($chars[$i]=~m/[$slightly_forbidden_after]/o){
375 return "\\CJKunbreakablekernthree{}";
377 if($chars[$i]=~m/[$asian]/o){
378 return "\\CJKbreakablekern{}";
380 if(($i<$#chars)and($chars[$i+1]=~m/[$asian]/o)){
381 return "\\CJKbreakablekern{}";
387 # return: string for output with TeX macro.
391 $char_id=unpack("U",$chars[$i]);
392 if($chars[$i]=~m/($tex_meta_re)/o){
393 $out_str.=$tex_meta{$chars[$i]};
394 }elsif(($char_id>0x20 and $char_id<=0x021f)
395 or($char_id>=0x0250 and $char_id<=0x02af)# IPA Extensions
396 or($char_id>=0x0300 and $char_id<=0x033f)# Combining Diacritical Marks
397 or($char_id>=0x0360 and $char_id<=0x036f)
398 or($char_id>=0x0370 and $char_id<=0x03ff)# Greek and Coptic
399 or($char_id>=0x0400 and $char_id<=0x04ff)# Cyrillic
400 or($char_id>=0x0530 and $char_id<=0x058f)# Armenian
402 $out_str.=$chars[$i];
403 }elsif($char_id>=0x1e00 and $char_id<=0x1eff){
404 # Latin Extended Additional
405 # 0x1e00 -> 0x0600, etc.
406 $out_str.=pack("U",$char_id-0x1800);
407 }elsif($char_id>=0x2010 and $char_id<=0x2046){
408 # General Punctuation (partial)
409 $out_str.=pack("U",$char_id-0x1000);
416 return '{\fontencoding{OT1}\fontfamily{omlgc}\selectfont '.$out_str.'}';###UT1?
421 # return: character for output,
423 # or GETA character if ids is invalid.
424 my($ids,$ids_argc)=&ids_rest("",0,$chars[$i]);
430 print STDERR "IDS parse error: $ids\n";
434 ($ids,$ids_argc)=&ids_rest($ids,$ids_argc,$chars[$i]);
436 if(($char=&get_char_for_ids($ids))
437 and($out_char=&get_output_char($char,$out_cs))){
440 return &get_macro_for_ids($ids);
447 # arguments: <ids>, <rest number of arguments for ids>, <character>
448 # return: ids and rest number of arguments for ids.
449 my($ids,$ids_argc,$char)=@_;
451 $argc=&ids_argc($char);
453 $ids_argc+=$ids_argc==0?$argc:$argc-1;
458 return ($ids,$ids_argc);
461 sub get_macro_for_ids{
463 # return: TeX macro for ids
464 # or GETA character if ids is invalid for KAGE.
466 # $ids=&normalize_ids($ids,"UniJIS");
467 return $geta if(($ids!~/[$idc]/)
468 or($ids=~/[\x{10000}-]/));
470 if(not defined($ids{$ids})){
471 $ids{$ids}[0]=$font_start;
472 $ids{$ids}[1]=$ids_start;
479 return "{\\fontencoding{OT1}\\fontfamily{"
480 .sprintf("chise%03d",$ids{$ids}[0])
481 ."}\\selectfont\\char$ids{$ids}[1]}";
485 # argument: <ids>, <output coding system>
486 # return: ids or GETA character if ids is invalid for KAGE.
488 $out_cs=~s/Uni(.+)/'ucs@'.lc($1)/e;
491 my($char,$output_char_id);
492 while($ids=~m/(.)/g){
496 }elsif($output_char_id=&get_char_attribute($char,"=$out_cs")
497 or $output_char_id=&get_char_attribute($char,"=ucs")
498 or $output_char_id=&get_char_attribute($char,"=>$out_cs")
499 or $output_char_id=&get_char_attribute($char,"=>ucs")
500 or $output_char_id=&get_char_attribute($char,"=>ucs*")
502 $output_ids.=pack("U",$output_char_id);
512 # return: character in output coding system or TeX macro or undef.
513 my($char,$out_cs)=@_;
514 my($out_char_id,$suffix);
517 foreach $out_cs (@{$order{$out_cs}}){
518 if($out_cs eq 'UniJIS'
519 and &get_char_attribute($char,"vnd-adobe-cid-unijis-utf16-h")){
520 if($out_char_id=&get_char_attribute($char,'=ucs@jis')
521 or $out_char_id=&get_char_attribute($char,'=ucs')
522 or $out_char_id=&get_char_attribute($char,'=>ucs@jis')
523 or $out_char_id=&get_char_attribute($char,'=>ucs')
524 or $out_char_id=&get_char_attribute($char,'=>ucs*')
526 return "{\\selectjisfont\\char$out_char_id}";
528 }elsif($out_cs eq 'UniGB'
529 and &get_char_attribute($char,"vnd-adobe-cid-unigb-ucs2-h")){
530 if($out_char_id=&get_char_attribute($char,'=ucs@gb')
531 or $out_char_id=&get_char_attribute($char,'=ucs')
532 or $out_char_id=&get_char_attribute($char,'=>ucs@gb')
533 or $out_char_id=&get_char_attribute($char,'=>ucs')
534 or $out_char_id=&get_char_attribute($char,'=>ucs*')
536 return "{\\selectgbsfont\\char$out_char_id}";
538 }elsif($out_cs eq 'UniCNS'
539 and &get_char_attribute($char,"vnd-adobe-cid-unicns-ucs2-h")){
540 if($out_char_id=&get_char_attribute($char,'=ucs@cns')
541 or $out_char_id=&get_char_attribute($char,'=ucs')
542 or $out_char_id=&get_char_attribute($char,'=>ucs@cns')
543 or $out_char_id=&get_char_attribute($char,'=>ucs')
544 or $out_char_id=&get_char_attribute($char,'=>ucs*')
546 return "{\\selectcnsfont\\char$out_char_id}";
548 }elsif($out_cs eq 'UniKS'
549 and &get_char_attribute($char,"vnd-adobe-cid-uniks-ucs2-h")){
550 if($out_char_id=&get_char_attribute($char,'=ucs@ks')
551 or $out_char_id=&get_char_attribute($char,'=ucs')
552 or $out_char_id=&get_char_attribute($char,'=>ucs@ks')
553 or $out_char_id=&get_char_attribute($char,'=>ucs')
554 or $out_char_id=&get_char_attribute($char,'=>ucs*')
556 return "{\\selectksxfont\\char$out_char_id}";
558 }elsif($out_cs eq 'GT'){
559 return $gt if($gt=&get_macro_for_GT($char));
560 }elsif($out_cs eq 'HZK'){
561 return $hzk if($hzk=&get_macro_for_HZK($char));
562 }elsif($out_cs eq 'CDP'){
563 return $cdp if($cdp=&get_macro_for_CDP($char));
570 # argument: <character>
574 $ids=&get_char_attribute($char,"ids-aggregated")
575 or $ids=&get_char_attribute($char,"ids");
576 # $ids=&get_char_attribute($char,"ids-decomposed")
577 # or $ids=&get_char_attribute($char,"ids");
578 # or $ids=&get_char_attribute($char,"ideographic-structure");
583 sub get_char_for_ids{
584 # argument: <ideographic description sequence>
585 # return: char or undef.
589 if(($output_char)=&get_chars_matching("ids",$ids)){
597 # argument: <char>, <input coding system>
598 # return: char in ucs@mcs.
602 return $char if($in_cs eq 'ucs@mcs');
604 if(($output_char)=&get_chars_matching("=$in_cs",unpack("U",$char))){
611 sub get_chars_unified{
613 my($chars,$ucs,$char_ucs);
616 if($chars=&get_char_attribute($char,'->ucs-unified')){
617 $chars=~s/^\((.*)\)$/$1/;
618 return (split(/\s*\?/,$chars));
619 }elsif($ucs=&get_char_attribute($char,'=>ucs*')
620 or $ucs=&get_char_attribute($char,'=>ucs')){
621 $char_ucs=pack("U",$ucs);
622 if($chars=&get_char_attribute($char_ucs,'->ucs-unified')){
623 $chars=~s/^\((.*)\)$/$1/;
624 @chars=grep {not /^$char$/}
625 (split(/\s*\?/,$chars));
626 push(@chars,$char_ucs);
632 sub get_macro_for_GT{
634 # return: TeX macro for GT fonts or undef.
638 if($gt=&get_char_attribute($char,$_)){
639 m/gt\-pj\-(\d+)/ and $GT=$1;
644 return "{\\fontencoding{OT1}\\fontfamily{"
645 .sprintf("gt%02d",$GT)
646 ."}\\selectfont\\char".($gt|0x8080)."}";
652 sub get_macro_for_HZK{
654 # return: TeX macro for Hanziku fonts or undef.
658 if($hzk=&get_char_attribute($char,$_)){
659 m/hanziku\-(\d+)/ and $HZK=$1;
664 return "{\\fontencoding{OT1}\\fontfamily{"
665 .sprintf("hzk%02d",$HZK)
666 ."}\\selectfont\\char".$hzk."}";
672 sub get_macro_for_CDP{
674 # return: TeX macro for CDP fonts or undef.
678 if($cdp=&get_char_attribute($char,$_)){
683 $ucs=&get_char_attribute(&get_chars_matching("=big5-pua",$cdp),"=ucs");
685 return "{\\fontencoding{OT1}\\fontfamily{cdp}\\selectfont\\char$ucs}";
687 print STDERR "This should not happen.\n";
688 print STDERR "ucs code point of CDP: $cdp not found.\n";