1 #!/usr/local/bin/perl -w
6 # The glyps list can be downloaded from
7 # http://partners.adobe.com/asn/developer/type/glyphlist.txt
8 # This URL is from this page:
9 # http://partners.adobe.com/asn/developer/type/unicodegn.html
10 # which is refered from
11 # http://partners.adobe.com/asn/developer/technotes/fonts.html
13 my $onlyHelvetica = 0;
15 my %globalName2Unicode;
20 my $indent2 = $indent1 x 3;
27 my $fn ="glyphlist.txt";
29 || die "Can't read $fn\n";
33 next unless /^([0-9A-F]{4});(\w+);/;
34 my $unicode = 0 + hex($1);
36 next if ($globalName2Unicode{$name});
37 $globalName2Unicode{$name} = $unicode;
46 $wc = "Helvetica.afm" if $onlyHelvetica;
47 $wc = "ZapfDin.afm" if 0;
48 $wc = "Helve*.afm" if 0;
49 $wc = "Times-BoldItalic.afm" if 0;
50 foreach my $fn (glob("$dir/$wc")) {
58 print STDERR "Compiling afm file: $fn\n";
59 my %fi = (); # font info
63 $fi{FontSpecificUnicodeNameToChar} = {};
65 $fi{filename} =~ s/.*\///;
66 open(FH, $fn) || die "Can't open $fn\n";
67 print STDERR "Reads global font info\n" if $q;
70 next if /^\s*$/ || /^\s*#/;
71 last if /^StartCharMetrics/;
72 next unless (/^(\S+)\s+(\S(.*\S)?)/);
76 $fi{"Afm$id"} = $value;
78 my $fontName = $fi{AfmFontName};
79 $c .= "\n\n/* ". ("-" x 66) . "*/\n";
80 $c .= "/* FontName: $fontName */\n";
81 $c .= "/* FullName: $fi{AfmFullName} */\n";
82 $c .= "/* FamilyName: $fi{AfmFamilyName} */\n";
83 $fi{cName} = $fontName;
84 $fi{cName} =~ s/\W/_/g;
87 read_charmetrics(\%fi, \%charMetrics);
89 read_kerning(\%fi, \%kerning) if /^StartKernPairs/;
92 my @names = keys %charMetrics;
93 print STDERR "Did read ", ($#names + 1), " font metrics\n";
95 write_font(\%fi, \%charMetrics, \%kerning);
100 my ($fiR, $charMetricsR) = @_;
101 print STDERR "Reads char metric info\n" if $q;
102 my $isZapfDingbats = $$fiR{AfmFontName} eq "ZapfDingbats";
103 my $ligaturesR = $$fiR{ligaturesR};
105 my %seenUnicodes = ();
108 next if /^\s*$/ || /^\s*#/;
109 last if /^EndCharMetrics/;
110 #next unless /N S / || /N comma /;
111 #next unless /N ([sfil]|fi) /;
114 # C 102 ; WX 333 ; N f ; B -169 -205 446 698 ; L i fi ; L l fl ;
115 my ($width, $unicode, $name, @charLigatures);
116 foreach (split/\s*;\s*/, $line) {
117 if (/^C\s+(-?\d+)/) {
119 } elsif (/^N\s+(\w+)/) {
121 } elsif (/^WX?\s+(-?\d+)/) {
122 $width = normalize_width($1, 0);
123 } elsif (/^L\s+(\w+)\s+(\w+)/) {
124 push(@charLigatures, $1, $2);
128 unless (defined $name) {
129 print STDERR "Glyph missing name and code: $_\n";
132 $unicode = name2uni($fiR, $name);
133 print STDERR "name2uni: $name -> $unicode\n" if $qU && 0;
134 } elsif (defined $name) {
135 my $std = $globalName2Unicode{$name};
137 print STDERR "Adds unicode mapping: ",
138 "$name -> $unicode\n" if $qU;
139 ${$$fiR{FontSpecificUnicodeNameToChar}}{$name} = $unicode;
144 if (!defined($unicode) || $unicode <= 0) {
145 next if $isZapfDingbats && $name =~ /^a(\d+)$/;
146 next if $$fiR{AfmFontName} eq "Symbol" && $name eq "apple";
147 print STDERR "Glyph '$name' has unknown unicode: $_\n";
150 unless (defined $width) {
151 print STDERR "Glyph '$name' missing width: $_\n";
154 if ($seenUnicodes{$unicode}) {
155 print STDERR "Duplicate character: unicode = $unicode, ",
156 "$name and ", $seenUnicodes{$unicode},
157 " (might be due to Adobe charset remapping)\n";
160 $seenUnicodes{$unicode} = $name;
163 $c{unicode} = $unicode;
165 $$charMetricsR{$unicode} = \%c;
166 $ligatures{$unicode} = \@charLigatures if $#charLigatures >= 0;
168 foreach my $unicode (keys %ligatures) {
169 my $aR = $ligatures{$unicode};
170 my $unicode2 = name2uni($fiR, $$aR[0]);
171 my $unicode3 = name2uni($fiR, $$aR[1]);
173 print STDERR "Missing ligature char 1: $$aR[0]\n";
177 print STDERR "Missing ligature char 2: $$aR[1]\n";
180 my $key = sprintf("%04d;%04d", $unicode, $unicode2);
181 $$ligaturesR{$key} = $unicode3;
187 my ($fiR, $name) = @_;
188 my $fontMapR = $$fiR{FontSpecificUnicodeNameToChar};
189 return $globalName2Unicode{$name} || $$fontMapR{$name};
194 my ($fiR, $kerningR) = @_;
195 print STDERR "Reads kerning info\n" if $q;
198 next if /^\s*$/ || /^\s*#/;
199 last if /^EndKernPairs/;
200 unless (/^KPX\s+(\w+)\s+(\w+)\s+(-?\d+)\s*$/) {
201 print STDERR "Can't parse kern spec: $_\n";
206 my $delta = normalize_width($3, 1);
208 my $unicode1 = name2uni($fiR, $name1);
209 my $unicode2 = name2uni($fiR, $name2);
210 unless ($unicode1 && $unicode2) {
211 print "Unknown kern pair: $name1 and $name2\n";
214 my $charR = $$kerningR{$unicode1};
215 unless (defined $charR) {
218 $$kerningR{$unicode1} = $charR;
220 $$charR{$unicode2} = $delta;
226 my ($fiR, $charMetricsR, $kerningR) = @_;
227 print STDERR "Writes font\n" if $q;
229 $$fiR{widthsA} = make_array();
230 $$fiR{kerning_indexA} = make_array();
231 $$fiR{kerning_dataA} = make_array();
232 $$fiR{highchars_indexA} = make_array();
233 $$fiR{ligaturesA} = make_array();
234 write_font_metrics($fiR, $charMetricsR, $kerningR);
235 write_ligatures($fiR);
236 my $widths_count = array_size($$fiR{widthsA});
237 my $kerning_index_count = array_size($$fiR{kerning_indexA});
238 my $kerning_data_count = array_size($$fiR{kerning_dataA});
239 my $highchars_count = array_size($$fiR{highchars_indexA});
240 my $ligatures_count = array_size($$fiR{ligaturesA}) / 3;
243 my $packedSize = $widths_count + 2 * $kerning_index_count +
244 $kerning_data_count + 2 * $highchars_count +
245 3 * 2 * $ligatures_count;
246 $info_code .= $indent1 . "{ /* $$fiR{filename} $packedSize bytes */\n";
247 $info_code .= $i2 . "\"$$fiR{AfmFontName}\",";
248 $info_code .= " \"$$fiR{AfmFullName}\",\n";
249 $info_code .= $i2 . $$fiR{widthsACName} . ",\n";
250 $info_code .= $i2 . $$fiR{kerning_indexACName} . ",\n";
251 $info_code .= $i2 . $$fiR{kerning_dataACName} . ",\n";
252 $info_code .= $i2 . $$fiR{highchars_indexACName} . ", ";
253 $info_code .= $highchars_count . ",\n";
254 $info_code .= $i2 . $$fiR{ligaturesACName} . ", ";
255 $info_code .= $ligatures_count;
256 $info_code .= "},\n";
257 $font_code{$$fiR{AfmFullName}} = { TABLES => $$cR, INFO => $info_code};
260 sub write_font_metrics
262 my ($fiR, $charMetricsR, $kerningR) = @_;
263 print STDERR "Writes font metrics\n" if $q;
264 my $lastUnicode = 31;
266 my $widthsA = $$fiR{widthsA};
267 my $kerning_indexA = $$fiR{kerning_indexA};
268 my $kerning_dataA = $$fiR{kerning_dataA};
269 my $highchars_indexA = $$fiR{highchars_indexA};
270 my @uniArray = sort { $a <=> $b } keys %$charMetricsR;
271 my $highchars_count = 0;
275 if ($#uniArray < 0) {
276 last if $lastUnicode > 126;
278 } elsif ($lastUnicode < 126 && $uniArray[0] > $lastUnicode + 1) {
283 #print STDERR "fill for $lastUnicode, $#uniArray, $uniArray[0]\n";
284 append_to_array($widthsA, 0);
285 append_to_array($kerning_indexA, 0);
288 my $unicode = shift @uniArray;
289 next if $unicode < 32;
290 $lastUnicode = $unicode;
291 my $metricsR = $$charMetricsR{$unicode};
292 if ($unicode > 126) {
293 append_to_array($highchars_indexA, $unicode);
296 my $m = $$metricsR{width};
297 $m = "/* ".array_size($widthsA)."=$unicode */". $m if 0;
298 append_to_array($widthsA, $m);
299 my $kerningInfoR = $$kerningR{$unicode};
300 my $kerning_index = 0;
301 if (defined $kerningInfoR) {
304 foreach my $unicode2 (sort { $a <=> $b } keys %$kerningInfoR) {
305 my $delta = $$kerningInfoR{$unicode2};
307 append_escaped_16bit_int(\@kerns, $unicode2);
308 push(@kerns, $delta);
311 $kerning_index = append_8bit_subarray($kerning_dataA, $numKernings, @kerns);
313 append_to_array($kerning_indexA, $kerning_index);
315 $$fiR{kerning_indexA} = make_array() if !$had_kerning;
316 write_array($fiR, "widths", "afm_cuint8");
317 write_array($fiR, "kerning_index", "afm_sint16");
318 write_array($fiR, "kerning_data", "afm_cuint8");
319 write_array($fiR, "highchars_index", "afm_cuint16");
325 print STDERR "Writes font ligatures\n" if $q;
326 my $ligaturesA = $$fiR{ligaturesA};
327 my $ligaturesR = $$fiR{ligaturesR};
328 foreach (sort keys %$ligaturesR) {
329 unless (/^(\w{4});(\w{4})$/) {
330 die "Invalid ligature key: $_";
332 append_to_array($ligaturesA, $1 + 0, $2 + 0, $$ligaturesR{$_});
334 write_array($fiR, "ligatures", "afm_cunicode");
351 my ($aR, @newElements) = @_;
352 my $z1 = array_size($aR);
353 push(@$aR, @newElements);
354 my $z2 = array_size($aR);
355 my $zz = $#newElements +1;
358 sub append_8bit_subarray
360 my ($aR, $numItems, @newElements) = @_;
361 push(@$aR, 42) if !array_size($aR); # initial dummy value
362 die if $numItems > $#newElements + 1;
363 my $idx = $#{$aR} + 1;
364 #print "append_8bit_subarray ", ($#newElements+1), " = (", join(", ", @newElements), ") -> $idx\n";
365 append_escaped_16bit_int($aR, $numItems);
366 push(@$aR, @newElements);
367 die "Can't handle that big sub array, sorry...\n" if $idx > 50000;
371 sub append_escaped_16bit_int
373 my ($aR, $count) = @_;
374 die "Invalid count = 0\n" unless $count;
376 push(@$aR, 1, int($count / 256), int($count % 256));
377 print STDERR "full: $count\n" if 0;
378 } elsif ($count >= 254) {
379 push(@$aR, 0, $count - 254);
380 print STDERR "semi: $count\n" if 0;
382 push(@$aR, $count + 1);
394 my ($fiR, $name, $type) = @_;
395 my $aR = $$fiR{$name."A"};
396 my $cName = $$fiR{cName};
397 my $num = $#{$aR} + 1;
398 my $array_name_key = $name."ACName";
400 $$fiR{$array_name_key} = "NULL";
404 my $array_name = "afm_" . $cName . "_" . $name;
405 $$fiR{$array_name_key} = $array_name;
406 $$cR .= "static $type $array_name" . "[] = { /* $num */\n";
408 for (my $i = 0; $i < $num; $i++) {
409 $line .= "," if $i > 0;
410 if (length($line) > 65) {
424 my ($w, $signed) = @_;
425 my $n = int(($w + 3) / 6);
427 $n = -128 if $n < -128;
428 $n = 127 if $n > 127;
429 $n = 256 + $n if $n < 0; # make unsigned.
432 $n = 255 if $n > 255;
439 my $cfn = "../../src/rrd_afm_data.c";
442 my @fonts = sort keys %font_code;
443 unless ($#fonts >= 0) {
444 die "You must have at least 1 font.\n";
446 open(CFILE, ">$cfn") || die "Can't create $cfn\n";
447 print CFILE header($cfn);
448 print CFILE ${$font_code{$_}}{TABLES} foreach @fonts;
449 print CFILE "const afm_fontinfo afm_fontinfolist[] = {\n";
450 print CFILE ${$font_code{$_}}{INFO} foreach @fonts;
451 print CFILE $indent1 . "{ 0, 0, 0 }\n";
452 print CFILE $indent0 . "};\n";
453 print CFILE $indent0 . "const int afm_fontinfo_count = ",
454 ($#fonts + 1), ";\n";
456 print STDERR "Compiled ", ($#fonts+1), " fonts.\n";
466 /****************************************************************************
467 * RRDtool 1.1.x Copyright Tobias Oetiker, 1997 - 2002
468 ****************************************************************************
469 * $fn Encoded afm (Adobe Font Metrics) for selected fonts.
470 ****************************************************************************
472 * THIS FILE IS AUTOGENERATED BY PERL. DO NOT EDIT.
474 ****************************************************************************/