6 use vars qw($omegadb_path
7 $opt_protrude %opt_order
10 $in_cs $out_cs $i @chars
12 $char $char_id $out_char
13 $ids $ids_argc %ids $idsdb
14 $idsdata_file $ids_start $font_start
20 use Chise_utils ':all';
23 my $omegadb_path="/usr/local/lib/chise/omega";
27 #$opt_order{'UniMulti'}='jcgk';
28 $opt_order{'UniMulti'}='jGcgkHC';
29 $opt_order{'UniGB'}='g';
30 $opt_order{'UniCNS'}='c';
31 $opt_order{'UniJIS'}='j';
32 $opt_order{'UniKS'}='k';
34 $opt_protrude=0;# 1=true, 0=false.
38 my $strictly_forbidden_after = '「【『[(〈“‘‘(〔{《{\[\(\x{3016}{「';
39 # \x{3016} | # white 【
41 my $forbidden_after = "\x{0000}";
44 my $slightly_forbidden_after = '¥¥$$〒♯##¢¢££@@§';
46 # $strictly_forbidden_before
47 # All these characters are allowed to protrude
49 my $strictly_forbidden_before=
50 '!,.:;?、。!,.:;?。\)#}’”〉》」』】〕\x{3017})]}}」\]';
51 ### \x{3017} | # white 】
54 = 'ー々ぁぃぅぇぉゃゅょっゎァィゥェォャュョッヮヵヶ';
56 my $slightly_forbidden_before
57 = '\x{000a}\#\-‐−‰′″℃゛゜ゝゞヽヾ"%-゙゚';
59 my $asian = '\x{1100}-\x{11FF}\x{2E80}-\x{D7AF}\x{F900}-\x{FAFF}\x{FE30}-\x{FE4F}\x{FF00}-';
61 my $space = '\x{0020}\x{0009}\x{000A}\x{000C}\x{000D}';
63 &GetOptions("in=s"=>\$opt_in_cs,
64 "out=s"=>\$opt_out_cs,
68 Usage: $0 -i <input coding system> -o <cmap encoding>
70 Utf8mcs, Utf8cns, Utf8gb, Utf8jis, Utf8ks
72 UniCNS, UniGB, UniJIS, UniKS, UniMulti
75 if($opt_in_cs or $opt_out_cs){
79 ($in_cs,$out_cs)=($0=~/(Utf8.+)To(\w+)/);
83 # Utf8mcs,Utf8cns,Utf8gb,Utf8jis,Utf8ks,
85 # UniCNS,UniGB,UniJIS,UniKS,UniMulti
87 $in_cs=~s/Utf8/ucs\@/;
90 or not defined($in_cs)
91 or not defined($out_cs)){
96 $omegadb_path=~s!/$!!;
98 $idsdata_file="$omegadb_path/idsdata.pl";
102 if(-e $idsdata_file){
103 open(IDSDATA,"+<$idsdata_file") or die;
104 flock(IDSDATA,LOCK_EX);
110 # require $idsdata_file;
112 open(IDSDATA,">$idsdata_file") or die;
113 flock(IDSDATA,LOCK_EX);
121 "=gt-pj-1","=gt-pj-2","=gt-pj-3","=gt-pj-4","=gt-pj-5",
122 "=gt-pj-6","=gt-pj-7","=gt-pj-8","=gt-pj-9","=gt-pj-10",
124 #,"=gt-pj-k1","=gt-pj-k2"
126 @HZK=("=hanziku-1","=hanziku-2","=hanziku-3","=hanziku-4",
127 "=hanziku-5","=hanziku-6","=hanziku-7","=hanziku-8",
128 "=hanziku-9","=hanziku-10","=hanziku-11","=hanziku-12");
131 %order_map=('c'=>'UniCNS',
140 foreach $out_cs ('UniGB','UniCNS','UniJIS','UniKS','UniMulti'){
141 if(defined($opt_order{$out_cs})){
142 if($opt_order{$out_cs}=~/^[cgjkGHC]*$/){
143 @{$order{$out_cs}}=map {$order_map{$_}}
144 (split(//,$opt_order{$out_cs}));
146 print STDERR "Invalid order for $out_cs!\n";
154 if($in_cs ne 'ucs@mcs'){
155 s/(.)/pack("U",&get_char_id(unpack("U",$1),$in_cs))/ge;
157 s/(amp.+?;)/&de_tex_er($1)/ge;
158 # s/(&.+?;)/&de_tex_er($1)/ge;
160 for($i=0;$i<=$#chars;$i++){
162 $char_id=unpack("U",$char);
167 }elsif($char_id>0x20 and $char_id<=0x02af){
173 print &latin_parse();
175 }elsif($char_id>=0x2ff0 and $char_id<=0x2fff){
176 # Ideographic Description Characters
180 if(($out_char=&get_output_char($char_id,$out_cs))){
181 print $out_char,&add_break($i);
182 }elsif($char_id >= 0x20000 && $char_id <=0x2a6df){
183 # CJK Unified Ideographs Extension B
184 if(not defined($ids{$char}) and $ids{$char}[1]>=0){
185 $ids{$char}[0]=$font_start;
186 $ids{$char}[1]=$ids_start;
193 print "{\\fontencoding{OT1}\\fontfamily{" .
194 sprintf("chise%03d",$ids{$char}[0]) .
195 "}\\selectfont\\char$ids{$char}[1]}",&add_break($i);
198 if($ids=&get_ids($char)){
199 print &get_macro_for_ids($ids),&add_break($i);
201 print '\rule{1ex}{1ex}',&add_break($i);
208 print IDSDATA 'use utf8;',"\n";
209 foreach $ids (keys %ids){
210 print IDSDATA '$ids{\'',$ids,'\'}='
211 ,'[',join ",",@{$ids{$ids}},"];\n";
213 print IDSDATA '$font_start=',$font_start,";\n";
214 print IDSDATA '$ids_start=',$ids_start,";\n";
216 flock(IDSDATA,LOCK_UN);
223 my($output_char,$atr,$value);
224 $er=~/^(amp)(.*)(;)$/
225 and $prefix=$1,$er=$2,$suffix=$3;
226 $prefix or $prefix="",$suffix or $suffix="";
227 if($er=~/^U[\+|\-]([a-fA-F\d]+)/){
228 $output_char=pack("U",hex($1));
229 }elsif($er=~/^(?:I\-)?($er_prefix_re)\-?([0-9a-fA-F]+)$/){
230 ($atr,$value)=($1,$2);
231 unless($er_alias{$atr}=~/daikanwa|gt/){
234 ($output_char)=&get_chars_matching($er_alias{$atr},$value);
235 utf8::decode($output_char);
240 return $prefix.$er.$suffix;
248 if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
249 and($chars[$i+2]=~m/[$strictly_forbidden_before]/o)){
250 return "\\CJKunbreakablekernone ";
251 }elsif($opt_protrude){
252 if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
253 and($chars[$i+2]=~m/[^$strictly_forbidden_before]/o)){
254 return "\\CJKunbreakablekernone \\CJKprotrude ";
259 and($chars[$i+1]=~m/[$strictly_forbidden_before]/o)){
260 return "\\CJKunbreakablekernone ";
262 if($chars[$i]=~m/[$strictly_forbidden_after]/o){
263 return "\\CJKunbreakablekernone ";
266 and($chars[$i+1]=~m/[$forbidden_before]/o)){
267 return "\\CJKunbreakablekerntwo ";
270 if($chars[$i]=~m/[$forbidden_after]/o){
271 return "\\CJKunbreakablekerntwo ";
274 and($chars[$i+1]=~m/[$slightly_forbidden_before]/o)){
275 return "\\CJKunbreakablekernthree ";
277 if($chars[$i]=~m/[$slightly_forbidden_after]/o){
278 return "\\CJKunbreakablekernthree ";
280 if($chars[$i]=~m/[$asian]/o){
281 return "\\CJKbreakablekern ";
283 if(($i<$#chars)and($chars[$i+1]=~m/[$asian]/o)){
284 return "\\CJKbreakablekern ";
290 # return: string for output with TeX macro.
292 my $out_str=$chars[$i];
295 $char_id=unpack("U",$chars[$i]);
296 if($char_id>0x20 and $char_id<=0x02af){
297 $out_str.=pack("U",$char_id);
304 return '{\normalfont {'.$out_str.'}}';
309 # return: character for output,
311 # or GETA character if ids is invalid.
312 my($ids,$ids_argc)=&ids_rest("",0,$chars[$i]);
318 print STDERR "IDS parse error: $ids\n";
319 # return pack("U",0xfffd);
320 return pack("U",0x3013);
323 ($ids,$ids_argc)=&ids_rest($ids,$ids_argc,$chars[$i]);
325 if(($char_id=&get_char_id_for_ids($ids))
326 and($out_char=&get_output_char($char_id,$out_cs))){
329 return &get_macro_for_ids($ids);
330 # return encode('utf8', &get_macro_for_ids($ids)) if($perl58);
337 # arguments: <ids>, <rest number of arguments for ids>, <character>
338 # return: ids and rest number of arguments for ids.
339 my($ids,$ids_argc,$char)=@_;
341 $argc=&ids_argc($char);
343 $ids_argc+= $ids_argc==0 ? $argc : $argc-1;
348 # $ids.=encode('utf8',$char) if($perl58);
349 return ($ids,$ids_argc);
352 sub get_macro_for_ids{
354 # return: TeX macro for ids
355 # or GETA character if ids is invalid for KAGE.
357 $ids=&normalize_ids($ids,"UniJIS");
358 # return pack("U",0xfffd) if(($ids!~/[$idc]/)
359 return pack("U",0x3013) if(($ids!~/[$idc]/)
360 or($ids=~/[\x{10000}-]/));
362 if(not defined($ids{$ids}) and $ids{$ids}[1]>=0){
363 $ids{$ids}[0]=$font_start;
364 $ids{$ids}[1]=$ids_start;
371 return "{\\fontencoding{OT1}\\fontfamily{"
372 .sprintf("chise%03d",$ids{$ids}[0])
373 ."}\\selectfont\\char$ids{$ids}[1]}";
377 # argument: <ids>, <output coding system>
378 # return: ids or GETA character if ids is invalid for KAGE.
380 $out_cs=~s/Uni(.+)/'ucs@'.lc($1)/e;
383 my($char,$char_id,$output_char_id);
384 while($ids=~m/(.)/g){
386 $char_id=unpack("U",$char);
389 }elsif($output_char_id=&get_char_attribute($char,"=$out_cs")){
390 $output_ids.=pack("U",$output_char_id);
391 }elsif($output_char_id=&get_char_attribute($char,"=ucs")){
392 $output_ids.=pack("U",$output_char_id);
394 # return pack("U",0xfffd);
395 return pack("U",0x3013);
402 # argument: <char-id>
403 # return: character in output coding system or TeX macro or undef.
404 my($char_id,$out_cs)=@_;
405 my($char,$out_char_id,$suffix);
408 $char=pack('U',$char_id);
410 foreach $out_cs (@{$order{$out_cs}}){
411 if($out_cs eq 'UniJIS'
412 and &get_char_attribute($char,"vnd-adobe-cid-unijis-utf16-h")){
413 if($out_char_id=&get_char_attribute($char,'=ucs@jis')
414 or $out_char_id=&get_char_attribute($char,'=ucs')
415 or $out_char_id=&get_char_attribute($char,'=>ucs@jis')
416 or $out_char_id=&get_char_attribute($char,'=>ucs')
418 return '{\selectjisfont\char'.$out_char_id.'}';
420 }elsif($out_cs eq 'UniGB'
421 and &get_char_attribute($char,"vnd-adobe-cid-unigb-ucs2-h")){
422 if($out_char_id=&get_char_attribute($char,'=ucs@gb')
423 or $out_char_id=&get_char_attribute($char,'=ucs')
424 or $out_char_id=&get_char_attribute($char,'=>ucs@gb')
425 or $out_char_id=&get_char_attribute($char,'=>ucs')
427 return '{\selectgbsfont\char'.$out_char_id.'}';
429 }elsif($out_cs eq 'UniCNS'
430 and &get_char_attribute($char,"vnd-adobe-cid-unicns-ucs2-h")){
431 if($out_char_id=&get_char_attribute($char,'=ucs@cns')
432 or $out_char_id=&get_char_attribute($char,'=ucs')
433 or $out_char_id=&get_char_attribute($char,'=>ucs@cns')
434 or $out_char_id=&get_char_attribute($char,'=>ucs')
436 return '{\selectcnsfont\char'.$out_char_id.'}';
438 }elsif($out_cs eq 'UniKS'
439 and &get_char_attribute($char,"vnd-adobe-cid-uniks-ucs2-h")){
440 if($out_char_id=&get_char_attribute($char,'=ucs@ks')
441 or $out_char_id=&get_char_attribute($char,'=ucs')
442 or $out_char_id=&get_char_attribute($char,'=>ucs@ks')
443 or $out_char_id=&get_char_attribute($char,'=>ucs')
445 return '{\selectksxfont\char'.$out_char_id.'}';
447 }elsif($out_cs eq 'GT'){
448 return $gt if($gt=&get_macro_for_GT($char_id));
449 }elsif($out_cs eq 'HZK'){
450 return $hzk if($hzk=&get_macro_for_HZK($char_id));
451 }elsif($out_cs eq 'CDP'){
452 return $cdp if($cdp=&get_macro_for_CDP($char_id));
459 # argument: <character>
463 $ids=&get_char_attribute($char,"ids-aggregated")
464 or $ids=&get_char_attribute($char,"ids");
465 # or $ids=&get_char_attribute($char,"ideographic-structure");
471 sub get_char_id_for_ids{
472 # argument: <ideographic description sequence>
473 # return: char-id or undef.
477 if(($output_char)=&get_chars_matching("ids",$ids)){
478 utf8::decode($output_char);
479 return unpack("U",$output_char);
486 # argument: <char-id>, <input coding system>
488 my($char_id,$in_cs)=@_;
491 return $char_id if($in_cs eq 'ucs@mcs');
493 if(($output_char)=&get_chars_matching("=$in_cs",$char_id)){
494 utf8::decode($output_char);
495 return unpack("U",$output_char);
501 sub get_macro_for_GT{
502 # argument: <char-id>
503 # return: TeX macro for GT fonts or undef.
506 $char=pack("U",$char_id);
508 if($gt=&get_char_attribute($char,$_)){
509 m/gt\-pj\-(\d+)/ and $GT=$1;
514 return "{\\fontencoding{OT1}\\fontfamily{"
515 .sprintf("gt%02d",$GT)
516 ."}\\selectfont\\char".($gt|0x8080)."}";
522 sub get_macro_for_HZK{
523 # argument: <char-id>
524 # return: TeX macro for Hanziku fonts or undef.
527 $char=pack("U",$char_id);
529 if($hzk=&get_char_attribute($char,$_)){
530 m/hanziku\-(\d+)/ and $HZK=$1;
535 return "{\\fontencoding{OT1}\\fontfamily{".sprintf("hzk%02d",$HZK)."}\\selectfont\\char".$hzk."}";
541 sub get_macro_for_CDP{
542 # argument: <char-id>
543 # return: TeX macro for CDP fonts or undef.
546 $char=pack("U",$char_id);
548 if($cdp=&get_char_attribute($char,$_)){
553 $ucs=&get_char_attribute(utf8::decode(&get_chars_matching("=big5-pua",$cdp)),"=ucs");
555 return "{\\fontencoding{OT1}\\fontfamily{cdp}\\selectfont\\char"
559 print STDERR "This hould not happen.\n";
560 print STDERR "ucs code point of CDP: $cdp not found.\n";