18th Dec 2003

advent on 2003-12-17T21:00:04

Comments for Perl Advent Calendar Entry 18th Dec 2003. Comments posted below may be displayed on perladvent.org.


Proportional font text widths

eric.t.f.bat on 2003-12-18T01:22:46

I have discovered a truly remarkable method of finding the width of text in proportional fonts in PDFs, which this comment field is too small to contain.

I can probably bundle up a general explanation and pass it on to the authors; is this worth doing, or is the problem already solved elsewhere?

Re:Proportional font text widths

grantm on 2003-12-19T09:25:32

I have discovered a truly remarkable method of finding the width of text in proportional fonts in PDFs, which this comment field is too small to contain.

Interested Perl/PDF hackers want to know! Perhaps you could write it up as a node on Perl Monks.

Re:Proportional font text widths

2shortplanks on 2003-12-21T21:38:45

Well I'd be really interested in this. Let me know (driectyly, or via a reply to this post) where you're posting it to.

Re:Proportional font text widths

eric.t.f.bat on 2003-12-22T05:10:17

I'll give you some more information and see if you're still interested.

Factoid #1: Positioning in PDF is given in thousandths of a point (ie 72000ths of an inch), as far as I can recall.

Factoid #2: The basic PDF fonts (Times, Courier, etc) have their exact metrics, including each character's width in 72000ths of an inch, defined in special files that are freely distributable and included in every installation of Adobe Reader (and presumably Ghostscript et al).

Factoid #3: The font metrics files are easily readable.

My replacement for 'TextWidth($font,$size,$string)' basically boils down to:

$width = 0;
foreach (split //, $string) {
    $width += $size*$metrics{$font}->{$_};
}

However (and this is the bit you'll hate) I've written it in Delphi (ie Pascal for Windows). I can convert it to bog-standard Perl if you like, but it's probably readable enough that I don't even need to.

What do you think. Still interested?

Re:Proportional font text widths

eric.t.f.bat on 2003-12-22T05:21:40

Ah, someone's done the work already. $width = $size * $font->width($text);. There you go!

Re:Proportional font text widths

merlyn on 2003-12-22T05:48:49

You're ignoring kerning, of course.

Re:Proportional font text widths

eric.t.f.bat on 2003-12-22T22:06:09

The kerning information is in there too - I tend to ignore it cos the difference is so tiny, but basically it's represented as a huge set of letter pairs with the associated positive or negative offsets. So yes, it's more complex than just multiplying the sum of widths by the font size, but not by much.

I wrote a PDF generator in Delphi; it was quite an educational experience. I recommend the intellectual challenge.

Of points and millimeters...

bart on 2003-12-18T11:08:38

d there are 2.83 milimeters to each postscipt point
OK, something's backwards here.

A point is 1/72 of an inch, so there's no way a point can measure 2.83mm. Dividing 25.4mm/72, I get 0.3527mm for a point. Oh, you got them reversed, a mm is 2.834pt.

Re:Of points and millimeters...

2shortplanks on 2003-12-21T21:37:03

Ooops, I suck. And You're right