2 # This script requires 't1asm' program, which is part of t1utils package,
10 my $omegadb_path="/usr/local/lib/chise/omega"; # set to the path of your db and script.
12 my $kageaddr="kage2.fonts.jp:80"; # Specify port number!
13 my $kagecgi="/~kamichi/test/kagecgi.cgi"; #
15 my $t1asm = "/usr/bin/env t1asm"; # point to 't1asm' executable.
16 #die "Cannot execute $t1asm. Abort.\n" unless -x $t1asm;
17 my $pfaedit="/usr/bin/env pfaedit"; # point to 'pfaedit' executable.
18 #die "Cannot execute $pfaedit. Abort.\n" unless -x $pfaedit;
20 $omegadb_path=~s!/$!!;
22 my $cleanup_script="$omegadb_path/svg2t1.pfe"; # Check the path of pfaedit script.
23 my $dbpath="$omegadb_path/glyph.db"; # check your DB path.
26 my $db = new BerkeleyDB::Hash
27 -Filename => $dbpath, -Flags => DB_CREATE
28 || print STDERR "Cannot open $dbpath. Do not use glyph database.\n";
35 my ($fontname, $fullname, $familyname,
36 $weight, $version, $uniqueID,
37 $numchars, $encoding) = @_;
39 %!PS-Adobe-Font-1.0: $fontname
43 /FontMatrix [0.001 0 0 0.001 0 0] readonly def
44 /UniqueID $uniqueID def
45 /FontBBox [0 -100 1000 900 ]readonly def
46 /FontInfo 8 dict dup begin
47 /version ($version) readonly def
48 /Notice (Copyright (C) Chise Project; Glyphs generated by KAGE server) readonly def
49 /FullName ($fullname) readonly def
50 /FamilyName ($familyname) readonly def
51 /Weight ($weight) readonly def
53 /isFixedPitch true def
54 /UnderlinePosition -200 def
59 /Private 9 dict dup begin
60 /-|{string currentfile exch readstring pop}executeonly def
61 /|-{noaccess def}executeonly def
62 /|{noaccess put}executeonly def
63 /BlueValues [] noaccess ND
64 /UniqueID $uniqueID def
70 2 index /CharStrings $numchars dict dup begin
84 dup/FontName get exch definefont pop
85 mark currentfile closefile
90 # Convert svg to Type1 charstring.
91 # Return: Type1 charstring.
94 my @paths= split(/\n/, $svg);
95 my ($x1, $y1, $x2, $y2);
96 my $glyph= "\{\n0 1000 hsbw\n";
101 next unless /\<path d=\"M([0-9, ]+)Z\"\/\>/;
102 my $path = $1; $path =~ s/^ +//; $path =~ s/ +$//;
103 my @point_pair = split(/ /, $path);
104 ($x1, $y1) = split(/,/, shift(@point_pair));
107 $glyph.=sprintf("%d %d rmoveto\n", $x1-$x2, $y1-$y2);
109 $glyph.= "$x1 $y1 rmoveto\n";
111 foreach my $pair (@point_pair) {
112 ($x2, $y2) = split(/,/, $pair);
114 $glyph.=sprintf("%d %d rlineto\n", $x2-$x1, $y2-$y1);
117 $glyph.= "closepath\n";
119 return $glyph.= "endchar\n\} |-\n";
122 # Query KAGE server and generate Type1 charstrings.
123 # Return: charstrings, encoding vector, and number of chars.
125 my ($requests, $suffix) = @_; # Receive REF for @requests array.
126 my $charstrings = "";
128 my $encoding=""; # /Encoding vector
129 my $blackbox=<<'BLACKBOX';
140 foreach my $req (@$requests){
142 # Referene passing destroys the original array!
143 my $request="$req.$suffix";
144 my ($svg, $charstring);
145 my $char = sprintf("ch%03d", $charnum);
146 if(defined $db && $db->db_get($request, $svg)==0){
147 # If glyph is already in DB, then use it.
148 $charstring = svg2charstring($svg);
149 print STDERR "Use cached glyph for $request.\n";
151 # If glyph is not yet in DB, query KAGE server.
153 my $location; # For redirection
154 if($kageserver = IO::Socket::INET->new("$kageaddr")){
155 print $kageserver "HEAD /$request.svg HTTP/1.1\r\n";
156 print $kageserver "Host: $kageaddr\r\n\r\n";
157 #Get redirection info.
159 while(<$kageserver>){
161 next unless m|^location:\s+http://([a-z0-9.:]+)/|i;
165 print STDERR "Connecting $location...\n";
167 and $kageserver=IO::Socket::INET->new($location)){
168 print $kageserver "GET /$request.svg HTTP/1.1\r\n";
169 print $kageserver "Host: $location\r\n\r\n";
170 local $/; $svg=<$kageserver>;
172 $svg =~ s/\r//gm; # remove CR.
173 $svg =~ s/^.+\n\n//ms; # remove HTTP header.
178 print STDERR "Cannot connect to KAGE server at $kageaddr.\n";
181 if($charstring = svg2charstring($svg)){
182 if(defined $db && $db->db_put($request, $svg)==0){
183 # If glyph request is successful, then store it to DB.
184 print STDERR "Glyph for $request cached.\n";
187 # If glyph request failed, then print a black box.
188 # Do not store glyph to DB.
189 print STDERR "Glyph request for $request failed.\n";
190 $charstring = $blackbox;
193 $charstrings.= "/$char $charstring";
194 $encoding.= "dup $charnum/$char put\n";
197 return ($charstrings, $encoding, $charnum);
204 # Read ids data generated by outCMAP
205 my $idsdata="$omegadb_path/idsdata.pl";
206 my ($font_start, $ids_start, %ids);
208 # "require" doesn't work well.
209 # I don't know why...
210 open (my $data, "<$idsdata");
216 die "Cannot read $idsdata\n";
219 # Create (a nested) list of requests.
220 # $Request[font-number][char-code]='uhhhh...'
222 foreach my $key (keys %ids){
225 foreach my $elem (split(//,$key)){
226 $elem=unpack('U', $elem);
227 if($elem < 0x10000){ # BMP
228 $code.=sprintf("u%04x", $elem);
229 }elsif($elem < 0x110000){ # needs surrogate pair
230 my $high_surrogate=($elem-0x10000) / 0x400 + 0xd800;
231 my $low_surrogate=($elem-0x10000) % 0x400 + 0xdc00;
232 $code.=sprintf("u%04xu%04x",
233 $high_surrogate, $low_surrogate);
234 }else{ #out of range.
235 $code="uffff"; # this generates KAGE server error.
239 $Requests[$ids{$key}[0]]->[$ids{$key}[1]]=$code;
243 foreach my $fontnum (0 .. $#Requests){
244 my @faces=(['Mincho', 'min', 'mincho'],
245 ['Gothic', 'got', 'gothic']
247 foreach my $i (0 .. 1){
248 my $fontname=sprintf("Chise%s%03d", $faces[$i][0], $fontnum);
249 my $pfbname=sprintf("chisesub%03d%s.pfb", $fontnum, $faces[$i][1]);
250 my $suffix=$faces[$i][2]; # .mincho or .gothic
252 # mincho= 430000..., gothic 440000...
253 my $unique_id=430000+$fontnum+$i*10000;
255 my($charstrings, $encoding, $charnum)=
256 makefont($Requests[$fontnum], $suffix);
258 $encoding = sprintf("/Encoding %d array\n%sreadonly def",
259 $charnum, $encoding);
262 printheader($fontname, # FontName
263 $fontname, # FullName
264 'Chise', # FamilyName
266 '001.001', # version (whatever)
267 $unique_id, # UniqueID (should be >= 4000000)
268 $charnum+1, # Number of chars (.notdef included)
269 $encoding # Encoding vector
270 ) . $charstrings . printfooter();
271 # Convert font to PFB
272 open(my $asm, '|-', "$t1asm -b -o $pfbname");
276 system("$pfaedit -script $cleanup_script $pfbname");