From ceabd83dcf6c97d71fe407fb453f0449d6ef3a56 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Mon, 10 Apr 2017 15:21:04 -0700 Subject: [PATCH] doc: fix fonts where the scale (unitsPerEm) is not 1000 AFM metrics always have 1000 font units to a scaled PostScript point, but TTF/OTF doesn't have to. The easiest way (and the one which best avoids unnecessary rounding) is to store the scale in the metrics, and change pswidth.ph to return the width in PostScript points instead of font units. Signed-off-by: H. Peter Anvin --- doc/afmmetrics.ph | 1 + doc/genps.pl | 10 +++++----- doc/pswidth.ph | 7 +++---- doc/ttfmetrics.ph | 12 ++++-------- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/doc/afmmetrics.ph b/doc/afmmetrics.ph index 52dfd7d2..c6f6d612 100644 --- a/doc/afmmetrics.ph +++ b/doc/afmmetrics.ph @@ -52,6 +52,7 @@ sub parse_afm_file($$) { $fontdata->{file} = $fontfile; $fontdata->{type} = $filetype; + $fontdata->{scale} = 1000; # AFM metrics always have scale 1000 return undef unless (open(my $fh, '<', $filename.'.afm')); diff --git a/doc/genps.pl b/doc/genps.pl index 3cacdb9a..84059c07 100755 --- a/doc/genps.pl +++ b/doc/genps.pl @@ -359,12 +359,12 @@ sub ps_flow_lines($$$@) { } else { my $ew = ps_width($$e[1], $fontset->{fonts}->[$$e[0]][1], \@NASMEncoding) * - ($fontset->{fonts}->[$$e[0]][0]/1000); + ($fontset->{fonts}->[$$e[0]][0]); my $sp = $$e[1]; $sp =~ tr/[^ ]//d; # Delete nonspaces my $esw = ps_width($sp, $fontset->{fonts}->[$$e[0]][1], \@NASMEncoding) * - ($fontset->{fonts}->[$$e[0]][0]/1000); + ($fontset->{fonts}->[$$e[0]][0]); if ( ($w+$ew) - $ps_space_squeeze*($sw+$esw) > $wid ) { # Begin new line @@ -423,13 +423,13 @@ sub ps_flow_lines($$$@) { my $xew = ps_width($$le[1], $fontset->{fonts}->[$$le[0]][1], \@NASMEncoding) * - ($fontset->{fonts}->[$$le[0]][0]/1000); + ($fontset->{fonts}->[$$le[0]][0]); my $xsp = $$le[1]; $xsp =~ tr/[^ ]//d; # Delete nonspaces my $xsw = ps_width($xsp, $fontset->{fonts}->[$$le[0]][1], \@NASMEncoding) * - ($fontset->{fonts}->[$$le[0]][0]/1000); + ($fontset->{fonts}->[$$le[0]][0]); $w += $xew; $sw += $xsw; } } @@ -738,7 +738,7 @@ sub ps_break_lines($$) { my $ntoc = substr($ptype,3,1)+0; my $refwidth = ps_width($refname, $BodyFont{fonts}->[0][1], \@NASMEncoding) * - ($BodyFont{fonts}->[0][0]/1000); + ($BodyFont{fonts}->[0][0]); @ls = ps_flow_lines($linewidth-$ntoc*$psconf{tocind}- $psconf{tocpnz}-$refwidth, diff --git a/doc/pswidth.ph b/doc/pswidth.ph index 36fc07aa..a2a90856 100644 --- a/doc/pswidth.ph +++ b/doc/pswidth.ph @@ -1,8 +1,7 @@ #!/usr/bin/perl # -# Get the width of a PostScript string in font units -# (1000 font units == the font point height) given a set of -# font metrics and an encoding vector. +# Get the width of a PostScript string in PostScript points (1/72") +# given a set of font metrics and an encoding vector. # sub ps_width($$$) { my($str, $met, $encoding) = @_; @@ -19,7 +18,7 @@ sub ps_width($$$) { $p = $c; } - return $w; + return $w / $met->{scale}; } # OK diff --git a/doc/ttfmetrics.ph b/doc/ttfmetrics.ph index 493ac853..0eec5504 100644 --- a/doc/ttfmetrics.ph +++ b/doc/ttfmetrics.ph @@ -26,15 +26,11 @@ sub parse_ttf_file($) { $fontdata->{type} = defined($f->{' CFF'}) ? 'otf' : 'ttf'; $f->{head}->read(); - #printf "unitsPerEm: %d\n", $f->{head}{unitsPerEm}; - #printf "xMin yMin xMax yMax: %d %d %d %d\n", - #$f->{head}{xMin}, - #$f->{head}{yMin}, - #$f->{head}{xMin}, - #$f->{head}{yMax}; - #$f->{maxp}->read(); + $fontdata->{scale} = $f->{head}{unitsPerEm}; + + $f->{maxp}->read(); my $glyphs = $f->{maxp}{numGlyphs}; - #printf "Total glyphs: %d\n", $glyphs; + $f->{cmap}->read(); $f->{hmtx}->read(); $f->{name}->read();