update.
[chise/omega.git] / makefonts.pl
index 8c0146e..c37e802 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/perl -w
+#!/usr/bin/perl -w -CSD
 # This script requires 't1asm' program, which is part of t1utils package,
 # and 'pfaedit'.
 
@@ -6,15 +6,64 @@ use strict;
 use utf8;
 use IO::Socket::INET;
 use BerkeleyDB;
+require 5.008001;
 
-my $kageaddr="kage2.fonts.jp:80"; # Specify port number!
-my $t1asm = "/sw/bin/t1asm"; # point to 't1asm' executable.
-die "Cannot execute $t1asm. Abort.\n" unless -x $t1asm;
-my $pfaedit="/Users/izumi/work/fonteditor/pfaedit/pfaedit/pfaedit"; # point to 'pfaedit' executable.
-die "Cannot execute $pfaedit. Abort.\n" unless -x $pfaedit;
-my $cleanup_script="svg2t1.pfe"; # Set the name of pfaedit script.
+#my $kageaddr="home.fonts.jp:5100"; # Specify port number!
+#my $kageaddr="fonts.jp:80"; # Specify port number!
+#my $kagecgi="/~kamichi/test/kagecgi.cgi"; #
+#my $kagecgi="v0.4/engine/kage.cgi"; #
+my $kageaddr="localhost:80"; # Specify port number!
+my $kagecgi="cgi-bin/kage.cgi"; #
+
+my $t1asm = "/usr/bin/env t1asm"; # point to 't1asm' executable.
+#die "Cannot execute $t1asm. Abort.\n" unless -x $t1asm;
+#my $pfaedit="/usr/bin/env pfaedit"; # point to 'pfaedit' executable.
+my $pfaedit="/usr/bin/env fontforge"; # point to 'fontforge' executable.
+#die "Cannot execute $pfaedit. Abort.\n" unless -x $pfaedit;
+
+my $omegadb_path="";   # Check the path of omegadb, if needed.
+my $cleanup_script=""; # Check the path of pfaedit script, if needed.
+
+unless($omegadb_path){
+    if(-w '/usr/local/share/chise/omega'){
+       $omegadb_path='/usr/local/share/chise/omega';
+    }elsif(-w '/usr/share/chise/omega'){
+       $omegadb_path='/usr/share/chise/omega';
+    }elsif(-w '/sw/share/chise/omega'){
+       $omegadb_path='/sw/share/chise/omega';
+    }elsif(-w '/usr/local/lib/chise/omega'){
+       $omegadb_path='/usr/local/lib/chise/omega';
+    }else{
+       $omegadb_path=".";
+    }
+}
+$omegadb_path=~s!/$!!;
+
+my $makefonts_dir;
+($makefonts_dir=$0)=~s!^(.*)/.*$!$1!;
+
+unless($cleanup_script){
+    if(-e "$makefonts_dir/eps2t1.pfe"){
+       $cleanup_script="$makefonts_dir/eps2t1.pfe";
+    }elsif(-e "/usr/local/share/texmf/omega/ocp/chise_rqd/chise/eps2t1.pfe"){
+       $cleanup_script="/usr/local/share/texmf/omega/ocp/chise_rqd/chise/eps2t1.pfe";
+    }elsif(-e "/usr/local/share/texmf/omega/ocp/local/chise/eps2t1.pfe"){
+       $cleanup_script="/usr/local/share/texmf/omega/ocp/local/chise/eps2t1.pfe";
+    }elsif(-e "/usr/share/texmf/omega/ocp/chise_rqd/chise/eps2t1.pfe"){
+       $cleanup_script="/usr/share/texmf/omega/ocp/local/chise/eps2t1.pfe";
+    }elsif(-e "/sw/share/texmf/omega/ocp/local/chise/eps2t1.pfe"){
+       $cleanup_script="/sw/share/texmf/omega/ocp/local/chise/eps2t1.pfe";
+    }
+}
+
+unless($cleanup_script and -e $cleanup_script){
+    print STDERR "eps2t1.pfe not found!\n",
+    "please check \$cleanup_script in $0.\n";
+    exit 1;
+}
+
+my $dbpath="$omegadb_path/glyph.db"; # check your DB path.
 
-my $dbpath="/Users/izumi/.chise/glyph.db"; # Set to your DB path.
 # Open Glyph Database
 my $db = new BerkeleyDB::Hash
     -Filename => $dbpath, -Flags => DB_CREATE
@@ -80,34 +129,34 @@ cleartomark
 FOOTER
 }
 
-# Convert svg to Type1 charstring.
+# Convert eps to Type1 charstring.
 # Return: Type1 charstring.
-sub svg2charstring {
-    my ($svg) = @_;
-    my @paths= split(/\n/, $svg);
+sub eps2charstring {
+    my ($eps) = @_;
+    my @paths= split(/\n/, $eps);
     my ($x1, $y1, $x2, $y2);
     my $glyph= "\{\n0 1000 hsbw\n";
     for (@paths){
-       if(m/error/i){
+       if(m/error/i or m/bad/i){
            return undef;
        }
-       next unless /\<path d=\"M([0-9, ]+)Z\"\/\>/;
-       my $path = $1; $path =~ s/^ +//; $path =~ s/ +$//;
-       my @point_pair = split(/ /, $path);
-       ($x1, $y1) =  split(/,/, shift(@point_pair));
-       $y1=1000-$y1-100;
-       if(defined $x2){
-           $glyph.=sprintf("%d %d rmoveto\n", $x1-$x2, $y1-$y2);
-       }else{
-           $glyph.= "$x1 $y1 rmoveto\n";
+       next if(/^%/);
+       if(m/([\-\d]+)\s+([\-\d]+)\s+moveto/){
+           $x1=$1; $y1=$2; $y1=$y1+100;
+           if(defined $x2){
+               $glyph.=sprintf("%d %d rmoveto\n",$x1-$x2,$y1-$y2);
+           }else{
+               $glyph.="$x1 $y1 rmoveto\n";
+           }
+       }
+       elsif(m/([\-\d]+)\s+([\-\d]+)\s+lineto/){
+           $x2=$1; $y2=$2; $y2=$y2+100;
+           $glyph.=sprintf("%d %d rlineto\n",$x2-$x1,$y2-$y1);
+           $x1=$x2,$y1=$y2;
        }
-       foreach my $pair (@point_pair) {
-           ($x2, $y2) = split(/,/, $pair);
-           $y2=1000-$y2-100;
-           $glyph.=sprintf("%d %d rlineto\n", $x2-$x1, $y2-$y1);
-           $x1=$x2; $y1=$y2;
+       elsif(/closepath/){
+           $glyph.="closepath\n";
        }
-       $glyph.= "closepath\n";
     }
     return $glyph.= "endchar\n\} |-\n";
 }
@@ -134,45 +183,45 @@ BLACKBOX
         # Note:
         # Referene passing destroys the original array!
         my $request="$req.$suffix";
-        my ($svg, $charstring);
+        my ($eps, $charstring);
         my $char = sprintf("ch%03d", $charnum);
-        if(defined $db && $db->db_get($request, $svg)==0){
+        if(defined $db && $db->db_get($request, $eps)==0){
             # If glyph is already in DB, then use it.
-            $charstring = svg2charstring($svg);
+            $charstring = eps2charstring($eps);
             print STDERR "Use cached glyph for $request.\n";
         }else{
             # If glyph is not yet in DB, query KAGE server.
             my $kageserver;
             my $location; # For redirection
             if($kageserver = IO::Socket::INET->new("$kageaddr")){
-                print $kageserver "HEAD /$request.svg HTTP/1.1\r\n";
-                print $kageserver "Host: $kageaddr\r\n\r\n";
-                #Get redirection info.
-                local $/="\r\n";
-                while(<$kageserver>){
-                    chomp;
-                    next unless m|^location:\s+http://([a-z0-9.:]+)/|i;
-                    $location=$1;
-                }
-                close($kageserver);
-                print STDERR "Connecting $location...\n";
-                if($location
-                   and $kageserver=IO::Socket::INET->new($location)){
-                    print $kageserver "GET /$request.svg HTTP/1.1\r\n";
-                    print $kageserver "Host: $location\r\n\r\n";
-                    local $/; $svg=<$kageserver>;
+                print $kageserver "GET /$kagecgi?$req&shotai=$suffix&type=eps\r\n";
+#               print $kageserver "HEAD /$kagecgi?$req&shotai=$suffix&type=svg HTTP/1.1\r\n";
+#               print $kageserver "Host: $kageaddr\r\n\r\n";
+#               #Get redirection info.
+#               local $/="\r\n";
+#               while(<$kageserver>){
+#                   chomp;
+#                   next unless m|^location:\s+http://([a-z0-9.:]+)/|i;
+#                   $location=$1;
+#               }
+#               close($kageserver);
+#               if($location
+#                  and $kageserver=IO::Socket::INET->new($location)){
+#                   print $kageserver "GET /$request.svg HTTP/1.1\r\n";
+#                   print $kageserver "Host: $location\r\n\r\n";
+                    local $/; $eps=<$kageserver>;
                     close($kageserver);
-                    $svg =~ s/\r//gm; # remove CR.
-                    $svg =~ s/^.+\n\n//ms; # remove HTTP header.
-                }else{
-                    $svg="error";
-                }
+                    $eps =~ s/\r//gm; # remove CR.
+                    $eps =~ s/^.+\n\n//ms; # remove HTTP header.
+#               }else{
+#                   $eps="error";
+#               }
             }else{
                 print STDERR "Cannot connect to KAGE server at $kageaddr.\n";
-                $svg="error";
+                $eps="error";
             }
-            if($charstring = svg2charstring($svg)){
-                if(defined $db && $db->db_put($request, $svg)==0){
+            if($charstring = eps2charstring($eps)){
+                if(defined $db && $db->db_put($request, $eps)==0){
                     # If glyph request is successful, then store it to DB.
                     print STDERR "Glyph for $request cached.\n";
                 }
@@ -194,15 +243,20 @@ BLACKBOX
 #### main ####
 ##############
 
-# Read ids data generated by outCMAP
-my $idsdata="idsdata.pl"; 
+# Read ids data generated by inCHISE
+my $idsdata="$omegadb_path/idsdata.txt";
 my ($font_start, $ids_start, %ids);
 if(-e $idsdata){
     # "require" doesn't work well.
     # I don't know why...
-    open (my $data, "<$idsdata");
+    open (my $data, "<:utf8","$idsdata");
     while(<$data>){
-       eval $_;
+       utf8::decode($_);
+       if(m/^START\t(\d+)\t(\d+)/){
+           $font_start=$1,$ids_start=$2;
+       }elsif(m/^(.*?)\t(\d+)\t(\d+)/){
+           $ids{$1}=[$2,$3,];
+       }
     }
     close($data);
 }else{
@@ -229,9 +283,12 @@ foreach my $key (keys %ids){
            last;
        }
     }
+    $code=~s/(.)u/$1.u/g;
     $Requests[$ids{$key}[0]]->[$ids{$key}[1]]=$code;
 }
 
+print STDERR "Sending query to KAGE server at $kageaddr.\n";
+
 # Create fonts.
 foreach my $fontnum (0 .. $#Requests){
     my @faces=(['Mincho', 'min', 'mincho'],