fix. preserve space of the end of line.
[chise/omega.git] / inCHISE
diff --git a/inCHISE b/inCHISE
index c5801cb..e2f1283 100755 (executable)
--- a/inCHISE
+++ b/inCHISE
@@ -22,8 +22,6 @@ use Fcntl ':flock';
 use Chise_utils ':all';
 require 5.008;
 
-my $omegadb_path="/usr/local/lib/chise/omega";
-
 ### Options ###
 
 #$opt_order{'UniMulti'}='jcgk';
@@ -62,10 +60,13 @@ my $forbidden_before
 my $slightly_forbidden_before
     = '\x{000a}\#\-‐−‰′″℃゛゜ゝゞヽヾ"%-゙゚';
 
-my $asian = '\x{1100}-\x{11FF}\x{2E80}-\x{D7AF}\x{F900}-\x{FAFF}\x{FE30}-\x{FE4F}\x{FF00}-\x{FFFFFF}';
+#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.
+my $asian = '\x{2E80}-\x{312f}\x{3190}-\x{ABFF}\x{F900}-\x{FAFF}\x{FE30}-\x{FE4F}\x{FF00}-\x{FFFFFF}'; # need to fix.
 
 my $space = '\x{0020}\x{0009}\x{000A}\x{000C}\x{000D}';
 
+my $hangul='\x{1100}-\x{11ff}\x{3130}-\x{318f}\x{AC00}-\x{D7AF}';
+
 my %tex_meta=('#'=>'\#',
              '$'=>'\\textdollar{}',
              '%'=>'\%',
@@ -85,7 +86,7 @@ my $tex_meta_re=join('|',map {quotemeta($_)} keys %tex_meta);
 $usage=<<EOF;
 Usage: $0 -i <input coding system> -o <cmap encoding>
     input coding system:
-      Utf8mcs, Utf8cns, Utf8gb, Utf8jis, Utf8ks
+      Utf8mcs, Utf8cns, Utf8gb, Utf8jis, Utf8ks, Utf8big5
     cmap encoding:
       UniCNS, UniGB, UniJIS, UniKS, UniMulti
 EOF
@@ -98,7 +99,7 @@ if($opt_in_cs or $opt_out_cs){
 }
 
 # $in_cs:
-#   Utf8mcs,Utf8cns,Utf8gb,Utf8jis,Utf8ks,
+#   Utf8mcs,Utf8cns,Utf8gb,Utf8jis,Utf8ks,Utf8big5
 # $out_cs:
 #   UniCNS,UniGB,UniJIS,UniKS,UniMulti
 
@@ -172,11 +173,24 @@ foreach $out_cs ('UniCNS','UniGB','UniJIS','UniKS','UniMulti'){
 
 while(<>){
     utf8::decode($_);
+
     if($in_cs ne 'ucs@mcs'){
        s/(.)/&get_char_in_mcs($1,$in_cs)/ge;
     }
     s/(amp.+?;)/&de_tex_er($1)/ge;
 #    s/(&.+?;)/&de_tex_er($1)/ge;
+    s/([$asian])\s+/$1/go unless($out_cs eq 'UniKS');
+    s/\s+([$asian])/$1/go unless($out_cs eq 'UniKS');
+    s/([$asian])\s*([^$asian$space])/$1 $2/go;
+    s/([^$asian$idc])\s*([$asian])/$1 $2/go;
+    s/\-\-\-/pack("U",0x2014)/geo;# EM DASH
+    s/\-\-/pack("U",0x2013)/geo;# EN DASH
+#    s/\`\`/pack("U",0x201f)/geo;# DOUBLE HIGH-REVERSED-9 QUOTATION MARK
+    s/\`\`/pack("U",0x201c)/geo;# _TeX compatible_
+#    s/\`/pack("U",0x201b)/geo;# SINGLE HIGH-REVERSED-9 QUOTATION MARK
+    s/\`/pack("U",0x2018)/geo;# _TeX compatible_
+    s/\'\'/pack("U",0x201d)/geo;# RIGHT DOUBLE QUOTATION MARK
+    s/\'/pack("U",0x2019)/geo;# RIGHT DOUBLE QUOTATION MARK
     @chars=split(//);
   CHAR:
     for($i=0;$i<=$#chars;$i++){
@@ -184,17 +198,50 @@ while(<>){
        $char_id=unpack("U",$char);
 
        if($char_id<=0x20){
-           print $chars[$i];
-           next CHAR;
-       }elsif($char=~m/($tex_meta_re)/o){
-           print $tex_meta{$1};
+           # add quarter space between asian and non-asian.
+           if($i>0 and $i<$#chars){
+               if(($chars[$i-1]=~/[$asian]/
+                   and $chars[$i+1]=~/[^$asian]/
+                     and $chars[$i+1]=~/[$strictly_forbidden_before$slightly_forbidden_before]/)
+                  or($chars[$i-1]=~/[^$asian]/
+                     and $chars[$i+1]=~/[$asian]/
+                     and $chars[$i-1]=~/[$strictly_forbidden_after$slightly_forbidden_after]/)
+                  ){
+                   print '';
+               }elsif(($chars[$i-1]=~/[$asian]/
+                       and $chars[$i+1]=~/[^$asian]/)){
+                   print '\unskip\kern.25ex';
+               }elsif($chars[$i-1]=~/[^$asian]/
+                      and $chars[$i+1]=~/[$asian]/){
+                   print '\kern.25ex';
+               }else{
+                   print ' ';
+               }
+           }else{
+               print ' ';
+           }
            next CHAR;
-       }elsif($char_id>0x20 and $char_id<=0x02af){
-           # Basic Latin
-           # Latin-1 Supplement
-           # Latin Extended-A
-           # Latin Extended-B
-           # IPA Extensions
+       }elsif(($char_id>0x20 and $char_id<=0x021f)
+              # Basic Latin
+              # Latin-1 Supplement
+              # Latin Extended-A
+              # Latin Extended-B (not all)
+              or($char_id>=0x0250 and $char_id<=0x02af)
+              # IPA Extensions
+              or($char_id>=0x0300 and $char_id<=0x033f)
+              or($char_id>=0x0360 and $char_id<=0x036f)
+              # Combining Diacritical Marks
+              or($char_id>=0x1e00 and $char_id<=0x1eff)
+              # Latin Extended Additional
+              or($char_id>=0x0370 and $char_id<=0x03ff)
+               # Greek and Coptic
+              or($char_id>=0x0400 and $char_id<=0x04ff)
+               # Cyrillic
+              or($char_id>=0x0530 and $char_id<=0x058f)
+               # Armenian
+              or($char_id>=0x2010 and $char_id<=0x2046)
+              # General Punctuation (partial)
+              ){
            print &latin_parse();
            next CHAR;
        }elsif($char_id>=0x2ff0 and $char_id<=0x2fff){
@@ -229,8 +276,8 @@ while(<>){
                                $font_start++;
                            }
                        }
-                       print "{\\fontencoding{OT1}\\fontfamily{" .
-                           sprintf("chise%03d",$ids{$char}[0]) .
+                       print "{\\fontencoding{OT1}\\fontfamily{",
+                           sprintf("chise%03d",$ids{$char}[0]) ,
                                "}\\selectfont\\char$ids{$char}[1]}",&add_break($i);
                        next CHAR;
                    }
@@ -286,41 +333,46 @@ sub add_break{
     if($i<($#chars-1)){
        if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
           and($chars[$i+2]=~m/[$strictly_forbidden_before]/o)){
-           return "\\CJKunbreakablekernone ";
+           return "\\CJKunbreakablekernone{}";
        }elsif($opt_protrude){
            if(($chars[$i+1]=~m/[$strictly_forbidden_before]/o)
               and($chars[$i+2]=~m/[^$strictly_forbidden_before]/o)){
-               return "\\CJKunbreakablekernone \\CJKprotrude ";
+               return "\\CJKunbreakablekernone \\CJKprotrude{}";
            }
        }
     }
     if(($i<$#chars)
+       and($chars[$i+1]eq" ")){
+       # preserve space.
+       return "";
+    }
+    if(($i<$#chars)
        and($chars[$i+1]=~m/[$strictly_forbidden_before]/o)){
-       return "\\CJKunbreakablekernone ";
+       return "\\CJKunbreakablekernone{}";
     }
     if($chars[$i]=~m/[$strictly_forbidden_after]/o){
-       return "\\CJKunbreakablekernone ";
+       return "\\CJKunbreakablekernone{}";
     }
     if(($i<$#chars)
        and($chars[$i+1]=~m/[$forbidden_before]/o)){
-       return "\\CJKunbreakablekerntwo ";
+       return "\\CJKunbreakablekerntwo{}";
 
     }
     if($chars[$i]=~m/[$forbidden_after]/o){
-       return "\\CJKunbreakablekerntwo ";
+       return "\\CJKunbreakablekerntwo{}";
     }
     if(($i<$#chars)
        and($chars[$i+1]=~m/[$slightly_forbidden_before]/o)){
-       return "\\CJKunbreakablekernthree ";
+       return "\\CJKunbreakablekernthree{}";
     }
     if($chars[$i]=~m/[$slightly_forbidden_after]/o){
-       return "\\CJKunbreakablekernthree ";
+       return "\\CJKunbreakablekernthree{}";
     }
     if($chars[$i]=~m/[$asian]/o){
-       return "\\CJKbreakablekern ";
+       return "\\CJKbreakablekern{}";
     }
     if(($i<$#chars)and($chars[$i+1]=~m/[$asian]/o)){
-       return "\\CJKbreakablekern ";
+       return "\\CJKbreakablekern{}";
     }
 }
 
@@ -328,19 +380,34 @@ sub latin_parse{
     # arguments: none
     # return: string for output with TeX macro.
     my($char_id);
-    my $out_str=$chars[$i];
-    $i++;
+    my $out_str="";
     while($i<=$#chars){
        $char_id=unpack("U",$chars[$i]);
-       if($char_id>0x20 and $char_id<=0x02af){
+       if($chars[$i]=~m/($tex_meta_re)/o){
+           $out_str.=$tex_meta{$chars[$i]};
+       }elsif(($char_id>0x20 and $char_id<=0x021f)
+          or($char_id>=0x0250 and $char_id<=0x02af)# IPA Extensions
+          or($char_id>=0x0300 and $char_id<=0x033f)# Combining Diacritical Marks
+          or($char_id>=0x0360 and $char_id<=0x036f)
+          or($char_id>=0x0370 and $char_id<=0x03ff)# Greek and Coptic
+          or($char_id>=0x0400 and $char_id<=0x04ff)# Cyrillic
+          or($char_id>=0x0530 and $char_id<=0x058f)# Armenian
+          ){
            $out_str.=$chars[$i];
+       }elsif($char_id>=0x1e00 and $char_id<=0x1eff){
+           # Latin Extended Additional
+            # 0x1e00 -> 0x0600, etc.
+           $out_str.=pack("U",$char_id-0x1800);
+       }elsif($char_id>=0x2010 and $char_id<=0x2046){
+           # General Punctuation (partial)
+           $out_str.=pack("U",$char_id-0x1000);
        }else{
            $i--;
            last;
        }
        $i++;
     }
-    return '{\fontencoding{UT1}\fontfamily{omlgc}\selectfont '.$out_str.'}';
+    return '{\fontencoding{OT1}\fontfamily{omlgc}\selectfont '.$out_str.'}';###UT1?
 }
 
 sub ids_parse{
@@ -450,7 +517,7 @@ sub get_output_char{
               or $out_char_id=&get_char_attribute($char,'=>ucs')
               or $out_char_id=&get_char_attribute($char,'=>ucs*')
               ){
-               return '{\selectjisfont\char'.$out_char_id.'}';
+               return "{\\selectjisfont\\char$out_char_id}";
            }
        }elsif($out_cs eq 'UniGB'
               and &get_char_attribute($char,"vnd-adobe-cid-unigb-ucs2-h")){
@@ -460,7 +527,7 @@ sub get_output_char{
               or $out_char_id=&get_char_attribute($char,'=>ucs')
               or $out_char_id=&get_char_attribute($char,'=>ucs*')
               ){
-               return '{\selectgbsfont\char'.$out_char_id.'}';
+               return "{\\selectgbsfont\\char$out_char_id}";
            }
        }elsif($out_cs eq 'UniCNS'
               and &get_char_attribute($char,"vnd-adobe-cid-unicns-ucs2-h")){
@@ -470,7 +537,7 @@ sub get_output_char{
               or $out_char_id=&get_char_attribute($char,'=>ucs')
               or $out_char_id=&get_char_attribute($char,'=>ucs*')
               ){
-               return '{\selectcnsfont\char'.$out_char_id.'}';
+               return "{\\selectcnsfont\\char$out_char_id}";
            }
        }elsif($out_cs eq 'UniKS'
               and &get_char_attribute($char,"vnd-adobe-cid-uniks-ucs2-h")){
@@ -480,7 +547,7 @@ sub get_output_char{
               or $out_char_id=&get_char_attribute($char,'=>ucs')
               or $out_char_id=&get_char_attribute($char,'=>ucs*')
               ){
-               return '{\selectksxfont\char'.$out_char_id.'}';
+               return "{\\selectksxfont\\char$out_char_id}";
            }
        }elsif($out_cs eq 'GT'){
            return $gt if($gt=&get_macro_for_GT($char));
@@ -498,10 +565,10 @@ sub get_ids{
     # return: ids
     my($char)=@_;
     my $ids="";
-#    $ids=&get_char_attribute($char,"ids-aggregated")
-#      or $ids=&get_char_attribute($char,"ids");
-    $ids=&get_char_attribute($char,"ids-decomposed")
+    $ids=&get_char_attribute($char,"ids-aggregated")
        or $ids=&get_char_attribute($char,"ids");
+#    $ids=&get_char_attribute($char,"ids-decomposed")
+#      or $ids=&get_char_attribute($char,"ids");
 #        or $ids=&get_char_attribute($char,"ideographic-structure");
 #    $ids=~s/[? ()]//g;
     return $ids;
@@ -588,7 +655,9 @@ sub get_macro_for_HZK{
        }
     }
     if($hzk){
-       return "{\\fontencoding{OT1}\\fontfamily{".sprintf("hzk%02d",$HZK)."}\\selectfont\\char".$hzk."}";
+       return "{\\fontencoding{OT1}\\fontfamily{"
+           .sprintf("hzk%02d",$HZK)
+           ."}\\selectfont\\char".$hzk."}";
     }else{
        return undef;
     }
@@ -607,9 +676,7 @@ sub get_macro_for_CDP{
     if($cdp){
        $ucs=&get_char_attribute(&get_chars_matching("=big5-pua",$cdp),"=ucs");
        if($ucs){
-           return "{\\fontencoding{OT1}\\fontfamily{cdp}\\selectfont\\char"
-               .$ucs.
-                   "}";
+           return "{\\fontencoding{OT1}\\fontfamily{cdp}\\selectfont\\char$ucs}";
        }else{
            print STDERR "This should not happen.\n";
            print STDERR "ucs code point of CDP: $cdp not found.\n";