-#!/usr/bin/perl
+#!/usr/bin/perl -CSD
# ver.0.2
use strict;
use vars qw($omegadb_path
- $opt_protrude $opt_order
+ $opt_protrude $opt_allow_unify
+ %opt_order %order %order_map
$opt_in_cs $opt_out_cs
$opt_help $usage
$in_cs $out_cs $i @chars
- @order %order
$char $char_id $out_char
+ $char_id_unified @char_id_unified
$ids $ids_argc %ids $idsdb
$idsdata_file $ids_start $font_start
- $inotp $perl56 $perl58
@CDP @HZK @GT
);
use Getopt::Long;
use utf8;
use Fcntl ':flock';
use Chise_utils ':all';
+require 5.008;
my $omegadb_path="/usr/local/lib/chise/omega";
### Options ###
-$opt_order='jcgk';
-#$opt_order='jtcgkhd';
+#$opt_order{'UniMulti'}='jcgk';
+$opt_order{'UniMulti'}='jGcgkHC';
+$opt_order{'UniGB'}='g';
+$opt_order{'UniCNS'}='c';
+$opt_order{'UniJIS'}='j';
+$opt_order{'UniKS'}='k';
+
+$opt_allow_unify=0; # 1=true, 0=false.
$opt_protrude=0;# 1=true, 0=false.
-###
+### End ###
+
my $strictly_forbidden_after = '「【『[(〈“‘‘(〔{《{\[\(\x{3016}{「';
# \x{3016} | # white 【
my $space = '\x{0020}\x{0009}\x{000A}\x{000C}\x{000D}';
-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.";
- exit 1;
-}
-if($perl58){
- eval "use Encode";
- binmode(STDIN, ':encoding(utf8)');
- binmode(STDOUT, ':encoding(utf8)');
-}
-
&GetOptions("in=s"=>\$opt_in_cs,
"out=s"=>\$opt_out_cs,
"help",\$opt_help);
$out_cs=$opt_out_cs;
}elsif(@ARGV==0){
($in_cs,$out_cs)=($0=~/(Utf8.+)To(\w+)/);
- $inotp=1;
}
# $in_cs:
$font_start=0;
if(-e $idsdata_file){
- open(IDSDATA,"+<$idsdata_file") or die;
+ open(IDSDATA,"+<:utf8",$idsdata_file) or die;
flock(IDSDATA,LOCK_EX);
seek(IDSDATA,0,0);
while(<IDSDATA>){
seek(IDSDATA,0,0);
# require $idsdata_file;
}else{
- open(IDSDATA,">$idsdata_file") or die;
+ open(IDSDATA,">:utf8",$idsdata_file) or die;
flock(IDSDATA,LOCK_EX);
seek(IDSDATA,0,0);
}
$ids="";
@GT=(#"=gt","=gt-k",
- "=gt-pj-1","=gt-pj-2","=gt-pj-3","=gt-pj-4","=gt-pj-5","=gt-pj-6","=gt-pj-7","=gt-pj-8","=gt-pj-9","=gt-pj-10","=gt-pj-11"
+ "=gt-pj-1","=gt-pj-2","=gt-pj-3","=gt-pj-4","=gt-pj-5",
+ "=gt-pj-6","=gt-pj-7","=gt-pj-8","=gt-pj-9","=gt-pj-10",
+ "=gt-pj-11"
#,"=gt-pj-k1","=gt-pj-k2"
);
-@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");
+@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");
-%order=('c'=>'UniCNS',
- 'g'=>'UniGB',
- 'j'=>'UniJIS',
- 'k'=>'UniKS',
- 't'=>'GT',
- 'h'=>'HZK',
- 'd'=>'CDP',
- );
-
-if(defined($opt_order)){
- if($opt_order=~/^[cgjkthd]*$/){
- @order=split(//,$opt_order);
- @order=map {$order{$_}} @order;
- }else{
- print STDERR "Invalid order!\n";
- exit 1;
+%order_map=('c'=>'UniCNS',
+ 'g'=>'UniGB',
+ 'j'=>'UniJIS',
+ 'k'=>'UniKS',
+ 'G'=>'GT',
+ 'H'=>'HZK',
+ 'C'=>'CDP',
+ );
+
+foreach $out_cs ('UniGB','UniCNS','UniJIS','UniKS','UniMulti'){
+ if(defined($opt_order{$out_cs})){
+ if($opt_order{$out_cs}=~/^[cgjkGHC]+$/){
+ @{$order{$out_cs}}=map {$order_map{$_}}
+ (split(//,$opt_order{$out_cs}));
+ }else{
+ print STDERR "Invalid order for $out_cs!\n";
+ exit 1;
+ }
}
}
while(<>){
- if($perl56){
- # for perl 5.6
- if($inotp){
- s/(.)/pack("c",&get_char_id(unpack("U",$1),$in_cs))/ge;
- }else{
- s/(.)/pack("U",&get_char_id(unpack("U",$1),$in_cs))/ge;
- }
- }elsif($perl58){
- # for perl 5.8.
- $_=decode('utf8', $_);
+ utf8::decode($_);
+ if($in_cs ne 'ucs@mcs'){
s/(.)/pack("U",&get_char_id(unpack("U",$1),$in_cs))/ge;
}
- s/(amp.+?;)/&de_er($1)/ge;
-# s/(&.+?;)/&de_er($1)/ge;
+ s/(amp.+?;)/&de_tex_er($1)/ge;
+# s/(&.+?;)/&de_tex_er($1)/ge;
@chars=split(//);
+ CHAR:
for($i=0;$i<=$#chars;$i++){
$char=$chars[$i];
$char_id=unpack("U",$char);
if($char_id<=0x20){
print $chars[$i];
- next;
+ next CHAR;
}elsif($char_id>0x20 and $char_id<=0x02af){
# Basic Latin
# Latin-1 Supplement
# Latin Extended-B
# IPA Extensions
print &latin_parse();
- next;
+ next CHAR;
}elsif($char_id>=0x2ff0 and $char_id<=0x2fff){
# Ideographic Description Characters
print &ids_parse();
- next;
+ next CHAR;
}else{
if(($out_char=&get_output_char($char_id,$out_cs))){
print $out_char,&add_break($i);
print "{\\fontencoding{OT1}\\fontfamily{" .
sprintf("chise%03d",$ids{$char}[0]) .
"}\\selectfont\\char$ids{$char}[1]}",&add_break($i);
- next;
+ next CHAR;
}else{
+ if($opt_allow_unify){
+ @char_id_unified=&get_char_id_unified($char_id);
+ if(@char_id_unified>0){
+ foreach $char_id_unified (@char_id_unified){
+ if(($out_char
+ =&get_output_char($char_id_unified,$out_cs))){
+ print $out_char,&add_break($i);
+ next CHAR;
+ }
+ }
+ }
+ }
if($ids=&get_ids($char)){
print &get_macro_for_ids($ids),&add_break($i);
}else{
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);
+ print IDSDATA '$ids{\'',$ids,'\'}='
+ ,'[',join ",",@{$ids{$ids}},"];\n";
}
print IDSDATA '$font_start=',$font_start,";\n";
print IDSDATA '$ids_start=',$ids_start,";\n";
exit 0;
+sub de_tex_er{
+ my($er)=@_;
+ my($prefix,$suffix);
+ my($output_char,$atr,$value);
+ $er=~/^(amp)(.*)(;)$/
+ 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);
+ utf8::decode($output_char);
+ }
+ if($output_char){
+ return $output_char;
+ }else{
+ return $prefix.$er.$suffix;
+ }
+}
+
sub add_break{
my($i)=@_;
while($i<=$#chars){
$char_id=unpack("U",$chars[$i]);
if($char_id>0x20 and $char_id<=0x02af){
- $out_str.=pack("U",$char_id);
+ $out_str.=$chars[$i];
}else{
$i--;
last;
and($out_char=&get_output_char($char_id,$out_cs))){
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);
+# return encode('utf8', &get_macro_for_ids($ids)) if($perl58);
}
}
}
}else{
$ids_argc--;
}
- $ids.=$char if($perl56);
- $ids.=encode('utf8',$char) if($perl58);
+ $ids.=$char;
+# $ids.=encode('utf8',$char) if($perl58);
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="";
sub get_output_char{
# argument: <char-id>
- # return: character in output coding system or TeX macro.
+ # return: character in output coding system or TeX macro or undef.
my($char_id,$out_cs)=@_;
my($char,$out_char_id,$suffix);
my($gt,$hzk,$cdp);
$char=pack('U',$char_id);
- if($out_cs eq 'UniJIS'
- 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')
- ){
- return pack("U",$out_char_id);
- }
- }elsif($out_cs eq 'UniGB'
- 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')
- ){
- return pack("U",$out_char_id);
- }
- }elsif($out_cs eq 'UniCNS'
- 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')
- ){
- return pack("U",$out_char_id);
- }
- }elsif($out_cs eq 'UniKS'
- 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')
- ){
- return pack("U",$out_char_id);
- }
- }elsif($out_cs eq 'UniMulti'){
- foreach $out_cs (@order){
-
- if($out_cs eq 'UniJIS'
- 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')
- ){
- return '{\selectjisfont\char'.$out_char_id.'}';
- }
- }elsif($out_cs eq 'UniGB'
- 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')
- ){
- return '{\selectgbsfont\char'.$out_char_id.'}';
- }
- }elsif($out_cs eq 'UniCNS'
- 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')
- ){
- return '{\selectcnsfont\char'.$out_char_id.'}';
- }
- }elsif($out_cs eq 'UniKS'
- 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')
- ){
- return '{\selectksxfont\char'.$out_char_id.'}';
- }
- }elsif($out_cs eq 'GT'){
- return $gt if($gt=&get_macro_for_GT($char_id));
- }elsif($out_cs eq 'HZK'){
- return $hzk if($hzk=&get_macro_for_HZK($char_id));
- }elsif($out_cs eq 'CDP'){
- return $cdp if($cdp=&get_macro_for_CDP($char_id));
+ foreach $out_cs (@{$order{$out_cs}}){
+ if($out_cs eq 'UniJIS'
+ 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')
+ ){
+ return '{\selectjisfont\char'.$out_char_id.'}';
+ }
+ }elsif($out_cs eq 'UniGB'
+ 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')
+ ){
+ return '{\selectgbsfont\char'.$out_char_id.'}';
+ }
+ }elsif($out_cs eq 'UniCNS'
+ 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')
+ ){
+ return '{\selectcnsfont\char'.$out_char_id.'}';
}
+ }elsif($out_cs eq 'UniKS'
+ 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')
+ ){
+ return '{\selectksxfont\char'.$out_char_id.'}';
+ }
+ }elsif($out_cs eq 'GT'){
+ return $gt if($gt=&get_macro_for_GT($char_id));
+ }elsif($out_cs eq 'HZK'){
+ return $hzk if($hzk=&get_macro_for_HZK($char_id));
+ }elsif($out_cs eq 'CDP'){
+ return $cdp if($cdp=&get_macro_for_CDP($char_id));
}
}
return undef;
$ids=&get_char_attribute($char,"ids-aggregated")
or $ids=&get_char_attribute($char,"ids");
# or $ids=&get_char_attribute($char,"ideographic-structure");
- $ids=decode('utf8', $ids) if($perl58);
+ utf8::decode($ids);
# $ids=~s/[? ()]//g;
return $ids;
}
sub get_char_id_for_ids{
# argument: <ideographic description sequence>
- # return: char-id
+ # return: char-id or undef.
my($ids)=@_;
my($output_char);
- $ids=decode('utf8', $ids) if($perl58);
if(($output_char)=&get_chars_matching("ids",$ids)){
+ utf8::decode($output_char);
return unpack("U",$output_char);
}else{
return undef;
return $char_id if($in_cs eq 'ucs@mcs');
if(($output_char)=&get_chars_matching("=$in_cs",$char_id)){
+ utf8::decode($output_char);
return unpack("U",$output_char);
}else{
return $char_id;
}
}
+sub get_char_id_unified{
+ my($char_id)=@_;
+ my($char,$chars,$ucs);
+ my(@char_id);
+ $char=pack("U",$char_id);
+
+ if($chars=&get_char_attribute($char,'->ucs-unified')){
+ utf8::decode($chars);
+ $chars=~s/^\((.*)\)$/$1/;
+ return map {unpack("U",$_)} (split(/\s*\?/,$chars));
+ }elsif($ucs=&get_char_attribute(pack("U",$char_id),'=>ucs')){
+ if($chars=&get_char_attribute(pack("U",$ucs),'->ucs-unified')){
+ utf8::decode($chars);
+ $chars=~s/^\((.*)\)$/$1/;
+ @char_id=grep {$char_id!=$_}
+ map {unpack("U",$_)}
+ (split(/\s*\?/,$chars));
+ push(@char_id,$ucs);
+ return @char_id;
+ }
+ }
+}
+
sub get_macro_for_GT{
# argument: <char-id>
- # return: TeX macro for GT fonts.
+ # return: TeX macro for GT fonts or undef.
my($char_id)=@_;
my($char,$gt,$GT);
$char=pack("U",$char_id);
}
sub get_macro_for_HZK{
+ # argument: <char-id>
+ # return: TeX macro for Hanziku fonts or undef.
my($char_id)=@_;
my($char,$hzk,$HZK);
$char=pack("U",$char_id);
}
sub get_macro_for_CDP{
+ # argument: <char-id>
+ # return: TeX macro for CDP fonts or undef.
my($char_id)=@_;
my($char,$cdp,$ucs);
$char=pack("U",$char_id);
}
}
if($cdp){
- $ucs=&get_char_attribute(&get_chars_matching("=big5-pua",$cdp),"=ucs");
+ $ucs=&get_char_attribute(utf8::decode(&get_chars_matching("=big5-pua",$cdp)),"=ucs");
if($ucs){
return "{\\fontencoding{OT1}\\fontfamily{cdp}\\selectfont\\char"
.$ucs.