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';
25 my $omegadb_path="/usr/local/lib/chise/omega";
29 #$opt_order{'UniMulti'}='jcgk';
30 $opt_order{'UniMulti'}='jGcgkHC';
31 $opt_order{'UniCNS'}='c';
32 $opt_order{'UniGB'}='g';
33 $opt_order{'UniJIS'}='j';
34 $opt_order{'UniKS'}='k';
36 $opt_allow_unify=0; # 1=true, 0=false.
37 $opt_protrude=0;# 1=true, 0=false.
39 # currently does not work.
40 $opt_use_kage_for_Ext_B=0;# 1=true, 0=false.
44 my $strictly_forbidden_after = '「【『[(〈“‘‘(〔{《{\[\(\x{3016}{「';
45 # \x{3016} | # white 【
47 my $forbidden_after = "\x{0000}";
50 my $slightly_forbidden_after = '¥¥$$〒♯##¢¢££@@§';
52 # $strictly_forbidden_before
53 # All these characters are allowed to protrude
55 my $strictly_forbidden_before=
56 '!,.:;?、。!,.:;?。\)#}’”〉》」』】〕\x{3017})]}}」\]';
57 ### \x{3017} | # white 】
60 = 'ー々ぁぃぅぇぉゃゅょっゎァィゥェォャュョッヮヵヶ';
62 my $slightly_forbidden_before
63 = '\x{000a}\#\-‐−‰′″℃゛゜ゝゞヽヾ"%-゙゚';
65 my $asian = '\x{1100}-\x{11FF}\x{2E80}-\x{D7AF}\x{F900}-\x{FAFF}\x{FE30}-\x{FE4F}\x{FF00}-\x{FFFFFF}';
67 my $space = '\x{0020}\x{0009}\x{000A}\x{000C}\x{000D}';
69 &GetOptions("in=s"=>\$opt_in_cs,
70 "out=s"=>\$opt_out_cs,
74 Usage: $0 -i <input coding system> -o <cmap encoding>
76 Utf8mcs, Utf8cns, Utf8gb, Utf8jis, Utf8ks
78 UniCNS, UniGB, UniJIS, UniKS, UniMulti
81 if($opt_in_cs or $opt_out_cs){
85 ($in_cs,$out_cs)=($0=~/(Utf8.+)To(\w+)/);
89 # Utf8mcs,Utf8cns,Utf8gb,Utf8jis,Utf8ks,
91 # UniCNS,UniGB,UniJIS,UniKS,UniMulti
93 $in_cs=~s/Utf8/ucs\@/;
96 or not defined($in_cs)
97 or not defined($out_cs)){
102 $omegadb_path=~s!/$!!;
104 $idsdata_file="$omegadb_path/idsdata.pl";
108 if(-e $idsdata_file){
109 open(IDSDATA,"+<:utf8",$idsdata_file) or die;
110 flock(IDSDATA,LOCK_EX);
116 # require $idsdata_file;
118 open(IDSDATA,">:utf8",$idsdata_file) or die;
119 flock(IDSDATA,LOCK_EX);
126 $geta=pack("U",0x3013);
127 #$geta=pack("U",0xfffd);
130 "=gt-pj-1","=gt-pj-2","=gt-pj-3","=gt-pj-4","=gt-pj-5",
131 "=gt-pj-6","=gt-pj-7","=gt-pj-8","=gt-pj-9","=gt-pj-10",
133 #,"=gt-pj-k1","=gt-pj-k2"
135 @HZK=("=hanziku-1","=hanziku-2","=hanziku-3","=hanziku-4",
136 "=hanziku-5","=hanziku-6","=hanziku-7","=hanziku-8",
137 "=hanziku-9","=hanziku-10","=hanziku-11","=hanziku-12");
140 %order_map=('c'=>'UniCNS',
149 foreach $out_cs ('UniCNS','UniGB','UniJIS','UniKS','UniMulti'){
150 if(defined($opt_order{$out_cs})){
151 if($opt_order{$out_cs}=~/^[cgjkGHC]+$/){
152 @{$order{$out_cs}}=map {$order_map{$_}}
153 (split(//,$opt_order{$out_cs}));
155 print STDERR "Invalid order for $out_cs!\n";
163 if($in_cs ne 'ucs@mcs'){
164 s/(.)/&get_char_in_mcs($1,$in_cs)/ge;
166 s/(amp.+?;)/&de_tex_er($1)/ge;
167 # s/(&.+?;)/&de_tex_er($1)/ge;
170 for($i=0;$i<=$#chars;$i++){
172 $char_id=unpack("U",$char);
177 }elsif($char_id>0x20 and $char_id<=0x02af){
183 print &latin_parse();
185 }elsif($char_id>=0x2ff0 and $char_id<=0x2fff){
186 # Ideographic Description Characters
190 if(($out_char=&get_output_char($char,$out_cs))){
191 print $out_char,&add_break($i);
193 if($opt_allow_unify){
194 @chars_unified=&get_chars_unified($char);
195 if(@chars_unified>0){
196 foreach $char_unified (@chars_unified){
198 =&get_output_char($char_unified,$out_cs))){
199 print $out_char,&add_break($i);
205 if($opt_use_kage_for_Ext_B){
206 if($char_id >= 0x20000 && $char_id <=0x2a6df){
207 # CJK Unified Ideographs Extension B
208 if(not defined($ids{$char}) and $ids{$char}[1]>=0){
209 $ids{$char}[0]=$font_start;
210 $ids{$char}[1]=$ids_start;
217 print "{\\fontencoding{OT1}\\fontfamily{" .
218 sprintf("chise%03d",$ids{$char}[0]) .
219 "}\\selectfont\\char$ids{$char}[1]}",&add_break($i);
223 if($ids=&get_ids($char)){
224 print &get_macro_for_ids($ids),&add_break($i);
226 print '\rule{1ex}{1ex}',&add_break($i);
233 print IDSDATA 'use utf8;',"\n";
234 foreach $ids (keys %ids){
235 print IDSDATA '$ids{\'',$ids,'\'}='
236 ,'[',join ",",@{$ids{$ids}},"];\n";
238 print IDSDATA '$font_start=',$font_start,";\n";
239 print IDSDATA '$ids_start=',$ids_start,";\n";
241 flock(IDSDATA,LOCK_UN);
248 my($output_char,$atr,$value);
249 $er=~/^(amp)(.*)(;)$/
250 and $prefix=$1,$er=$2,$suffix=$3;
251 $prefix or $prefix="",$suffix or $suffix="";
252 if($er=~/^U[\+|\-]([a-fA-F\d]+)/){
253 $output_char=pack("U",hex($1));
254 }elsif($er=~/^(?:I\-)?($er_prefix_re)\-?([0-9a-fA-F]+)$/){
255 ($atr,$value)=($1,$2);
256 unless($er_alias{$atr}=~/daikanwa|gt/){
259 ($output_char)=&get_chars_matching($er_alias{$atr},$value);
264 return $prefix.$er.$suffix;
272 if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
273 and($chars[$i+2]=~m/[$strictly_forbidden_before]/o)){
274 return "\\CJKunbreakablekernone ";
275 }elsif($opt_protrude){
276 if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
277 and($chars[$i+2]=~m/[^$strictly_forbidden_before]/o)){
278 return "\\CJKunbreakablekernone \\CJKprotrude ";
283 and($chars[$i+1]=~m/[$strictly_forbidden_before]/o)){
284 return "\\CJKunbreakablekernone ";
286 if($chars[$i]=~m/[$strictly_forbidden_after]/o){
287 return "\\CJKunbreakablekernone ";
290 and($chars[$i+1]=~m/[$forbidden_before]/o)){
291 return "\\CJKunbreakablekerntwo ";
294 if($chars[$i]=~m/[$forbidden_after]/o){
295 return "\\CJKunbreakablekerntwo ";
298 and($chars[$i+1]=~m/[$slightly_forbidden_before]/o)){
299 return "\\CJKunbreakablekernthree ";
301 if($chars[$i]=~m/[$slightly_forbidden_after]/o){
302 return "\\CJKunbreakablekernthree ";
304 if($chars[$i]=~m/[$asian]/o){
305 return "\\CJKbreakablekern ";
307 if(($i<$#chars)and($chars[$i+1]=~m/[$asian]/o)){
308 return "\\CJKbreakablekern ";
314 # return: string for output with TeX macro.
316 my $out_str=$chars[$i];
319 $char_id=unpack("U",$chars[$i]);
320 if($char_id>0x20 and $char_id<=0x02af){
321 $out_str.=$chars[$i];
328 return '{\normalfont {'.$out_str.'}}';
333 # return: character for output,
335 # or GETA character if ids is invalid.
336 my($ids,$ids_argc)=&ids_rest("",0,$chars[$i]);
342 print STDERR "IDS parse error: $ids\n";
346 ($ids,$ids_argc)=&ids_rest($ids,$ids_argc,$chars[$i]);
348 if(($char=&get_char_for_ids($ids))
349 and($out_char=&get_output_char($char,$out_cs))){
352 return &get_macro_for_ids($ids);
359 # arguments: <ids>, <rest number of arguments for ids>, <character>
360 # return: ids and rest number of arguments for ids.
361 my($ids,$ids_argc,$char)=@_;
363 $argc=&ids_argc($char);
365 $ids_argc+=$ids_argc==0?$argc:$argc-1;
370 return ($ids,$ids_argc);
373 sub get_macro_for_ids{
375 # return: TeX macro for ids
376 # or GETA character if ids is invalid for KAGE.
378 $ids=&normalize_ids($ids,"UniJIS");
379 return $geta if(($ids!~/[$idc]/)
380 or($ids=~/[\x{10000}-]/));
382 if(not defined($ids{$ids})){
383 $ids{$ids}[0]=$font_start;
384 $ids{$ids}[1]=$ids_start;
391 return "{\\fontencoding{OT1}\\fontfamily{"
392 .sprintf("chise%03d",$ids{$ids}[0])
393 ."}\\selectfont\\char$ids{$ids}[1]}";
397 # argument: <ids>, <output coding system>
398 # return: ids or GETA character if ids is invalid for KAGE.
400 $out_cs=~s/Uni(.+)/'ucs@'.lc($1)/e;
403 my($char,$output_char_id);
404 while($ids=~m/(.)/g){
408 }elsif($output_char_id=&get_char_attribute($char,"=$out_cs")
409 or $output_char_id=&get_char_attribute($char,"=ucs")
410 or $output_char_id=&get_char_attribute($char,"=>$out_cs")
411 or $output_char_id=&get_char_attribute($char,"=>ucs")
412 or $output_char_id=&get_char_attribute($char,"=>ucs*")
414 $output_ids.=pack("U",$output_char_id);
424 # return: character in output coding system or TeX macro or undef.
425 my($char,$out_cs)=@_;
426 my($out_char_id,$suffix);
429 foreach $out_cs (@{$order{$out_cs}}){
430 if($out_cs eq 'UniJIS'
431 and &get_char_attribute($char,"vnd-adobe-cid-unijis-utf16-h")){
432 if($out_char_id=&get_char_attribute($char,'=ucs@jis')
433 or $out_char_id=&get_char_attribute($char,'=ucs')
434 or $out_char_id=&get_char_attribute($char,'=>ucs@jis')
435 or $out_char_id=&get_char_attribute($char,'=>ucs')
436 or $out_char_id=&get_char_attribute($char,'=>ucs*')
438 return '{\selectjisfont\char'.$out_char_id.'}';
440 }elsif($out_cs eq 'UniGB'
441 and &get_char_attribute($char,"vnd-adobe-cid-unigb-ucs2-h")){
442 if($out_char_id=&get_char_attribute($char,'=ucs@gb')
443 or $out_char_id=&get_char_attribute($char,'=ucs')
444 or $out_char_id=&get_char_attribute($char,'=>ucs@gb')
445 or $out_char_id=&get_char_attribute($char,'=>ucs')
446 or $out_char_id=&get_char_attribute($char,'=>ucs*')
448 return '{\selectgbsfont\char'.$out_char_id.'}';
450 }elsif($out_cs eq 'UniCNS'
451 and &get_char_attribute($char,"vnd-adobe-cid-unicns-ucs2-h")){
452 if($out_char_id=&get_char_attribute($char,'=ucs@cns')
453 or $out_char_id=&get_char_attribute($char,'=ucs')
454 or $out_char_id=&get_char_attribute($char,'=>ucs@cns')
455 or $out_char_id=&get_char_attribute($char,'=>ucs')
456 or $out_char_id=&get_char_attribute($char,'=>ucs*')
458 return '{\selectcnsfont\char'.$out_char_id.'}';
460 }elsif($out_cs eq 'UniKS'
461 and &get_char_attribute($char,"vnd-adobe-cid-uniks-ucs2-h")){
462 if($out_char_id=&get_char_attribute($char,'=ucs@ks')
463 or $out_char_id=&get_char_attribute($char,'=ucs')
464 or $out_char_id=&get_char_attribute($char,'=>ucs@ks')
465 or $out_char_id=&get_char_attribute($char,'=>ucs')
466 or $out_char_id=&get_char_attribute($char,'=>ucs*')
468 return '{\selectksxfont\char'.$out_char_id.'}';
470 }elsif($out_cs eq 'GT'){
471 return $gt if($gt=&get_macro_for_GT($char));
472 }elsif($out_cs eq 'HZK'){
473 return $hzk if($hzk=&get_macro_for_HZK($char));
474 }elsif($out_cs eq 'CDP'){
475 return $cdp if($cdp=&get_macro_for_CDP($char));
482 # argument: <character>
486 # $ids=&get_char_attribute($char,"ids-aggregated")
487 # or $ids=&get_char_attribute($char,"ids");
488 $ids=&get_char_attribute($char,"ids-decomposed")
489 or $ids=&get_char_attribute($char,"ids");
490 # or $ids=&get_char_attribute($char,"ideographic-structure");
495 sub get_char_for_ids{
496 # argument: <ideographic description sequence>
497 # return: char or undef.
501 if(($output_char)=&get_chars_matching("ids",$ids)){
509 # argument: <char>, <input coding system>
510 # return: char in ucs@mcs.
514 return $char if($in_cs eq 'ucs@mcs');
516 if(($output_char)=&get_chars_matching("=$in_cs",unpack("U",$char))){
523 sub get_chars_unified{
525 my($chars,$ucs,$char_ucs);
528 if($chars=&get_char_attribute($char,'->ucs-unified')){
529 $chars=~s/^\((.*)\)$/$1/;
530 return (split(/\s*\?/,$chars));
531 }elsif($ucs=&get_char_attribute($char,'=>ucs*')
532 or $ucs=&get_char_attribute($char,'=>ucs')){
533 $char_ucs=pack("U",$ucs);
534 if($chars=&get_char_attribute($char_ucs,'->ucs-unified')){
535 $chars=~s/^\((.*)\)$/$1/;
536 @chars=grep {not /^$char$/}
537 (split(/\s*\?/,$chars));
538 push(@chars,$char_ucs);
544 sub get_macro_for_GT{
546 # return: TeX macro for GT fonts or undef.
550 if($gt=&get_char_attribute($char,$_)){
551 m/gt\-pj\-(\d+)/ and $GT=$1;
556 return "{\\fontencoding{OT1}\\fontfamily{"
557 .sprintf("gt%02d",$GT)
558 ."}\\selectfont\\char".($gt|0x8080)."}";
564 sub get_macro_for_HZK{
566 # return: TeX macro for Hanziku fonts or undef.
570 if($hzk=&get_char_attribute($char,$_)){
571 m/hanziku\-(\d+)/ and $HZK=$1;
576 return "{\\fontencoding{OT1}\\fontfamily{".sprintf("hzk%02d",$HZK)."}\\selectfont\\char".$hzk."}";
582 sub get_macro_for_CDP{
584 # return: TeX macro for CDP fonts or undef.
588 if($cdp=&get_char_attribute($char,$_)){
593 $ucs=&get_char_attribute(&get_chars_matching("=big5-pua",$cdp),"=ucs");
595 return "{\\fontencoding{OT1}\\fontfamily{cdp}\\selectfont\\char"
599 print STDERR "This should not happen.\n";
600 print STDERR "ucs code point of CDP: $cdp not found.\n";