-#!/usr/bin/perl
+#!/usr/bin/perl -w -CID
use strict;
use vars qw($opt_in_cs $opt_order $opt_kage $opt_replace
+ $opt_use_kage_for_Ext_B $opt_allow_unification
$opt_help $usage
$in_cs $out_cs $i @chars
@order $order %order
@texmacro
$char $char_id $out_char
+ $char_unified @chars_unified
$ids $ids_argc %ids $idsdb
$idsdata_file $ids_start $font_start
$perl56 $perl58
@CDP @HZK @GT
);
use Getopt::Long;
+use Fcntl ':flock';
use utf8;
use Chise_utils ':all';
+require 5.008;
-my $perl="/usr/bin/perl";
-my $makefonts="/Users/izumi/.chise/makefonts.pl";
+# currently does not work, so...
+$opt_use_kage_for_Ext_B=0;
+
+my $makefonts="/usr/local/share/texmf/omega/ocp/local/chise/makefonts.pl";
my $exec_makefonts=0;
my $geta=pack("S",8750|0x8080);
-if($^V and $^V ge v5.8){
- $perl58=1;
-}elsif($^V and $^V ge v5.6){
- $perl56=1;
-}else{
- print STDERR "This versin is not supported.";
-}
-if($perl58){
- eval "use Encode";
- binmode(STDIN, ':encoding(utf8)');
- binmode(STDOUT, ':encoding(utf8)');
-}
-
&GetOptions("in=s"=>\$opt_in_cs,
"order=s"=>\$opt_order,
"replace",\$opt_replace,
"kage",\$opt_kage,
+ "unify",\$opt_allow_unification,
"help",\$opt_help);
$usage=<<EOF;
-Usage: $0 [-i <input coding system>] [-o <order of kanji>] [-k] <filename>
- input coding system: (default: ucs\@mcs)
- ucs\@mcs, ucs\@cns, ucs\@gb, ucs\@jis, ucs\@ks
- order of kanji: (default: j)
+Usage: $0 [-i <input coding system>] [-o <order of kanji>] [-kru] <filename>
+ -i: input coding system: (default: ucs\@mcs)
+ ucs\@mcs, ucs\@cns, ucs\@gb, ucs\@jis, ucs\@ks
+ -o: order of kanji: (default: j)
c: CNS
g: GB
j: JIS
k: KS
- t: GT
+ G: GT
m: Multi, use \\UTFM of otf.sty
You can also combine them, ex. jtcgkm
- k: use Kage server.
- r: replace r and l with dot below to those with circle below.
+ -k: use Kage server.
+ -r: replace r and l with dot below to those with circle below.
+ -u: allow unification.
EOF
%order=('c'=>'UniCNS',
'g'=>'UniGB',
'j'=>'UniJIS',
'k'=>'UniKS',
- 't'=>'GT',
+ 'G'=>'GT',
'm'=>'Multi',
);
}
if(defined($opt_order)){
- if($opt_order=~/^[cgjktm]+$/){
+ if($opt_order=~/^[cgjkGm]+$/){
@order=split(//,$opt_order);
@order=map {$order{$_}} @order;
}else{
$texmacro[0x1E5D]='{\ifmmode \ucirc{\bar{r}}\else \ucirc{\={r}}\fi}';
}
-$idsdata_file="idsdata.pl";
+$idsdata_file="$omegadb_path/idsdata.pl";
$ids_start=0x00;
$font_start=0;
if(-e $idsdata_file){
- require $idsdata_file;
+ open(IDSDATA,"+<$idsdata_file") or die;
+ flock(IDSDATA,LOCK_EX);
+ seek(IDSDATA,0,0);
+ while(<IDSDATA>){
+ eval $_;
+ }
+ seek(IDSDATA,0,0);
+# require $idsdata_file;
+}else{
+ open(IDSDATA,">$idsdata_file") or die;
+ flock(IDSDATA,LOCK_EX);
+ seek(IDSDATA,0,0);
}
$ids_argc=0;
"=gt-pj-11"
#,"=gt-pj-k1","=gt-pj-k2"
);
-@HZK=("=hanziku-1","=hanziku-10","=hanziku-11","=hanziku-12",
- "=hanziku-2","=hanziku-3","=hanziku-4","=hanziku-5",
- "=hanziku-6","=hanziku-7","=hanziku-8","=hanziku-9");
+@HZK=("=hanziku-1","=hanziku-2","=hanziku-3","=hanziku-4",
+ "=hanziku-5","=hanziku-6","=hanziku-7","=hanziku-8",
+ "=hanziku-9","=hanziku-10","=hanziku-11","=hanziku-12");
@CDP=("=big5-cdp");
while(<>){
if($in_cs ne 'ucs@mcs'){
- s/(.)/pack("U",&get_char_id(unpack("U",$1),$in_cs))/ge;
+ s/(.)/&get_char_in_mcs($1,$in_cs)/ge;
}
- s/&(.*?);/&de_er($1)/ge;
+ s/((?:^|[^\\])(?:\\\\)*)(&.*?;)/&de_tex_er($1,$2)/ge;
+ s/((?:^|[^\\])(?:\\\\)*)\\([$idc])/$1.'\UTFM{'.sprintf("%X",unpack("U",$2)).'}'/ge;
@chars=split(//);
+ CHAR:
for($i=0;$i<=$#chars;$i++){
$char=$chars[$i];
$char_id=unpack("U",$chars[$i]);
if($char_id<=0x7f){
# Basic Latin
print $char;
- next;
+ next CHAR;
}elsif(defined($texmacro[$char_id]) and $texmacro[$char_id]){
# already defined for
# 0080..00FF; Latin-1 Supplement
# 0100..017F; Latin Extended-A
# 1E00..1EFF; Latin Extended Additional
print $texmacro[$char_id];
- next;
+ next CHAR;
}elsif($char_id>=0x2ff0 and $char_id<=0x2fff){
# Ideographic Description Characters
print &ids_parse();
- next;
- }elsif($char_id >= 0x20000 && $char_id <=0x2a6df){
- # CJK Unified Ideographs Extension B
- if(not defined($ids{$char}) and $ids{$char}[1]>=0){
- $ids{$char}[0]=$font_start;
- $ids{$char}[1]=$ids_start;
- $ids_start++;
- if($ids_start>255){
- $ids_start=0;
- $font_start++;
- }
- }
- print "{\\fontencoding{OT1}\\fontfamily{" .
- sprintf("chise%03d",$ids{$char}[0]) .
- "}\\selectfont\\char$ids{$char}[1]}";
- next;
+ next CHAR;
}else{
- if(($out_char=&get_output_char($char_id))){
+ if(($out_char=&get_output_char($char))){
print $out_char;
+ next CHAR;
}else{
- print &get_macro_for_ids(&get_ids($char));
+ if($opt_allow_unification){
+ @chars_unified=&get_chars_unified($char);
+ if(@chars_unified>0){
+ foreach $char_unified (@chars_unified){
+ if(($out_char=&get_output_char($char_unified))){
+ print $out_char;
+ next CHAR;
+ }
+ }
+ }
+ }
+ if($opt_use_kage_for_Ext_B
+ and $char_id >= 0x20000 and $char_id <=0x2a6df){
+ # CJK Unified Ideographs Extension B
+ if(not defined($ids{$char}) and $ids{$char}[1]>=0){
+ $ids{$char}[0]=$font_start;
+ $ids{$char}[1]=$ids_start;
+ $ids_start++;
+ if($ids_start>255){
+ $ids_start=0;
+ $font_start++;
+ }
+ }
+ print "{\\fontencoding{OT1}\\fontfamily{" .
+ sprintf("chise%03d",$ids{$char}[0]) .
+ "}\\selectfont\\char$ids{$char}[1]}";
+ next CHAR;
+ }
+ if($ids=&get_ids($char)){
+ print &get_macro_for_ids($ids);
+ next CHAR;
+ }else{
+ print $geta;
+ }
}
}
}
}
-open(IDSDATA,">$idsdata_file") or die;
print IDSDATA 'use utf8;',"\n";
foreach $ids (keys %ids){
print IDSDATA '$ids{\'',$ids,'\'}='
- ,'[',join ",",@{$ids{$ids}},"];\n" if($perl56);
- print IDSDATA '$ids{\'',encode('utf8',$ids),'\'}='
- ,'[',join ",",@{$ids{$ids}},"];\n" if($perl58);
+ ,'[',join ",",@{$ids{$ids}},"];\n";
}
print IDSDATA '$font_start=',$font_start,";\n";
print IDSDATA '$ids_start=',$ids_start,";\n";
print IDSDATA "1;";
+flock(IDSDATA,LOCK_UN);
if($exec_makefonts){
if(-f $makefonts){
- system($perl,$makefonts) ==0
+ system("/usr/bin/env","perl",$makefonts) ==0
or die "Could not make fonts: $?";
}else{
print STDERR "cannot find $makefonts\n";
exit 0;
+sub de_tex_er{
+ my($before_er,$er)=@_;
+ my($prefix,$suffix);
+ my($output_char,$atr,$value);
+ $er=~/^(\&)(.*)(;)$/
+ and $prefix=$1,$er=$2,$suffix=$3;
+ $prefix or $prefix="",$suffix or $suffix="";
+ if($er=~/^U[\+|\-]([a-fA-F\d]+)/){
+ $output_char=pack("U",hex($1));
+ }elsif($er=~/^(?:I\-)?($er_prefix_re)\-?([0-9a-fA-F]+)$/){
+ ($atr,$value)=($1,$2);
+ unless($er_alias{$atr}=~/daikanwa|gt/){
+ $value=hex($value);
+ }
+ ($output_char)=&get_chars_matching($er_alias{$atr},$value);
+ }
+ if($output_char){
+ return $before_er.$output_char;
+ }else{
+ return $before_er.$prefix.$er.$suffix;
+ }
+}
+
sub ids_parse{
# arguments: none
# return: return character for output,
($ids,$ids_argc)=&ids_rest($ids,$ids_argc,$chars[$i]);
if($ids_argc==0){
- if(($char_id=&get_char_id_for_ids($ids))
- and($out_char=&get_output_char($char_id))){
+ if(($char=&get_char_for_ids($ids))
+ and($out_char=&get_output_char($char))){
return $out_char;
}else{
- return &get_macro_for_ids($ids) if($perl56);
- return encode('utf8', &get_macro_for_ids($ids)) if($perl58);
+ return &get_macro_for_ids($ids);
}
}
}
}else{
$ids_argc--;
}
- $ids.=$char if($perl56);
- $ids.=encode('utf8',$char) if($perl58);
+ $ids.=$char;
return ($ids,$ids_argc);
}
# argument: <ids>, <output coding system>
# return: ids or GETA character if ids is invalid for KAGE.
my($ids,$out_cs)=@_;
- $ids = decode('utf8', $ids) if $perl58;
$out_cs=~s/Uni(.+)/'ucs@'.lc($1)/e;
my $output_ids="";
- my($char,$char_id,$output_char_id);
+ my($char,$output_char_id);
while($ids=~m/(.)/g){
$char=$1;
- $char_id=unpack("U",$char);
if($char=~/[$idc]/){
$output_ids.=$char;
- }elsif($output_char_id=&get_char_attribute($char,"=$out_cs")){
- $output_ids.=pack("U",$output_char_id);
- }elsif($output_char_id=&get_char_attribute($char,"=ucs")){
+ }elsif($output_char_id=&get_char_attribute($char,"=$out_cs")
+ or $output_char_id=&get_char_attribute($char,"=ucs")
+ or $output_char_id=&get_char_attribute($char,"=>$out_cs")
+ or $output_char_id=&get_char_attribute($char,"=>ucs")
+ or $output_char_id=&get_char_attribute($char,"=>ucs*")
+ ){
$output_ids.=pack("U",$output_char_id);
}else{
return $geta;
}
sub get_output_char{
- # argument: <char-id>
+ # argument: <char>
# return: character in EUC-JP or TeX macro for pTeX.
- my($char_id)=@_;
- my($char,$out_char,$out_char_id,$gt);
+ my($char)=@_;
+ my($out_char,$out_char_id,$gt);
- $char=pack('U',$char_id);
-
- if($out_char=&get_char_attribute($char,'=jis-x0208')){
+ if($out_char=&get_char_attribute($char,'=jis-x0208')
+ or $out_char=&get_char_attribute($char,'=jis-x0208-1983')
+ or $out_char=&get_char_attribute($char,'=jis-x0208-1990')
+ ){
return pack("S",$out_char|0x8080);
}else{
foreach $out_cs (@order){
if($out_cs eq 'UniJIS'
- and &get_char_attribute($char,"adobe-unijis-utf16-h")){
+ and &get_char_attribute($char,"vnd-adobe-cid-unijis-utf16-h")){
if($out_char_id=&get_char_attribute($char,'=ucs@jis')
or $out_char_id=&get_char_attribute($char,'=ucs')
or $out_char_id=&get_char_attribute($char,'=>ucs@jis')
or $out_char_id=&get_char_attribute($char,'=>ucs')
+ or $out_char_id=&get_char_attribute($char,'=>ucs*')
){
return "\\UTF{".sprintf("%X",$out_char_id)."}";
}
}elsif($out_cs eq 'UniGB'
- and &get_char_attribute($char,"adobe-unigb-ucs2-h")){
+ and &get_char_attribute($char,"vnd-adobe-cid-unigb-ucs2-h")){
if($out_char_id=&get_char_attribute($char,'=ucs@gb')
or $out_char_id=&get_char_attribute($char,'=ucs')
or $out_char_id=&get_char_attribute($char,'=>ucs@gb')
or $out_char_id=&get_char_attribute($char,'=>ucs')
+ or $out_char_id=&get_char_attribute($char,'=>ucs*')
){
return "\\UTFC{".sprintf("%X",$out_char_id)."}";
}
}elsif($out_cs eq 'UniCNS'
- and &get_char_attribute($char,"adobe-unicns-ucs2-h")){
+ and &get_char_attribute($char,"vnd-adobe-cid-unicns-ucs2-h")){
if($out_char_id=&get_char_attribute($char,'=ucs@cns')
or $out_char_id=&get_char_attribute($char,'=ucs')
or $out_char_id=&get_char_attribute($char,'=>ucs@cns')
or $out_char_id=&get_char_attribute($char,'=>ucs')
+ or $out_char_id=&get_char_attribute($char,'=>ucs*')
){
return "\\UTFT{".sprintf("%X",$out_char_id)."}";
}
}elsif($out_cs eq 'UniKS'
- and &get_char_attribute($char,"adobe-uniks-ucs2-h")){
+ and &get_char_attribute($char,"vnd-adobe-cid-uniks-ucs2-h")){
if($out_char_id=&get_char_attribute($char,'=ucs@ks')
or $out_char_id=&get_char_attribute($char,'=ucs')
or $out_char_id=&get_char_attribute($char,'=>ucs@ks')
or $out_char_id=&get_char_attribute($char,'=>ucs')
+ or $out_char_id=&get_char_attribute($char,'=>ucs*')
){
return "\\UTFK{".sprintf("%X",$out_char_id)."}";
}
}elsif($out_cs eq 'GT'){
- return $gt if($gt=&get_macro_for_GT($char_id));
+ return $gt if($gt=&get_macro_for_GT($char));
}elsif($out_cs eq 'Multi'){
if($out_char_id=&get_char_attribute($char,'=ucs')){
return "\\UTFM{".sprintf("%X",$out_char_id)."}";
my $ids="";
$ids=&get_char_attribute($char,"ids-aggregated")
or &get_char_attribute($char,"ids");
- $ids=decode('utf8', $ids) if($perl58);
return $ids;
}
-sub get_char_id_for_ids{
+sub get_char_for_ids{
# argument: <ideographic description sequence>
- # return: char-id
+ # return: char or undef
my($ids)=@_;
my($output_char);
- $ids=decode('utf8', $ids) if($perl58);
if(($output_char)=&get_chars_matching("ids",$ids)){
- return unpack("U",$output_char);
+ return $output_char;
}else{
return undef;
}
}
-sub get_char_id{
- # argument: <char-id>, <input coding system>
- # return: char-id.
- my($char_id,$in_cs)=@_;
+sub get_char_in_mcs{
+ # argument: <char>, <input coding system>
+ # return: char in ucs@mcs.
+ my($char,$in_cs)=@_;
my($output_char);
- if(($output_char)=&get_chars_matching("=$in_cs",$char_id)){
- return unpack("U",$output_char);
+ if(($output_char)=&get_chars_matching("=$in_cs",unpack("U",$char))){
+ return $output_char;
}else{
- return $char_id;
+ return $char;
+ }
+}
+
+sub get_chars_unified{
+ my($char)=@_;
+ my($chars,$ucs,$char_ucs);
+ my(@chars);
+
+ if($chars=&get_char_attribute($char,'->ucs-unified')){
+ $chars=~s/^\((.*)\)$/$1/;
+ return (split(/\s*\?/,$chars));
+ }elsif($ucs=&get_char_attribute($char,'=>ucs*')
+ or $ucs=&get_char_attribute($char,'=>ucs')){
+ $char_ucs=pack("U",$ucs);
+ if($chars=&get_char_attribute($char_ucs,'->ucs-unified')){
+ $chars=~s/^\((.*)\)$/$1/;
+ @chars=grep {not /^$char$/}
+ (split(/\s*\?/,$chars));
+ push(@chars,$char_ucs);
+ return @chars;
+ }
}
}
sub get_macro_for_GT{
- # argument: <char-id>
- # return: TeX macro for GT fonts.
- my($char_id)=@_;
- my($char,$gt,$GT);
- $char=pack("U",$char_id);
+ # argument: <char>
+ # return: TeX macro for GT fonts or undef.
+ my($char)=@_;
+ my($gt,$GT);
foreach (@GT){
if($gt=&get_char_attribute($char,$_)){
m/gt\-pj\-(\d+)/ and $GT=$1;