From: de19@umail.umd.edu (Dana S Emery)
Newsgroups: comp.sys.mac.programmer
Subject: Re: Measuring text in different fonts
Date: 1 Oct 92 02:10:44 GMT
Followup-To: comp.sys.mac.programmer
Organization: Personal

In article <2ACA0CD9.12346@news.service.uci.edu>, mwu@orion.oac.uci.edu
(Matt Wu) wrote:
> 
> 
> I'm currently writing a program that will need to measure the width of
> text as it will appear on a written page.  I've been using the stringwidth
> function to do this.  However, as the Macintosh uses bit-mapped fonts,
> I was wondering how accurate this would be as compared to, say, final
> output out of a PostScript laserprinter?  If this is not a very accurate way
> of measuring text width, can you offer any suggestions as to how I could
> get better results?

Matt, this is a rather large can of worms.

First, it is difficult for a program to know if a PS font will be used for
imageing.  Second, the bitmap width tables are nowhere near as precise as
the actual PS glyph tables, so that brings up the Q of obtaining better
information (assuming you actually need it).  Third, all of this assumes
that your code is aware of whatever kerning is going to be imposed.  One
impractical strategy would be to attempt to debreif the actual PS font, but
that involves access to it (printer/printer disk residency, maybe even
remote print site) and considerable greif deencodeing it.  

A better strategy would be to reference the fonts AFM file which is
available for all adobe (and many other) type 1 fonts.  I beleive adobe
makes the afm and the bitmaps available essentially as freeware for all its
fonts (but dont quote me), so it is not unreasonable to require that your
users supply them.  One could also depend on the ATM programatic hooks,
with the afm (and QD) as backup.

Currently, QD, TT, and PS fonts are supported.  PS differ from QD fonts in
that they have an associated 'LWFN' file whose name is described by an
optional table in the 'FOND' resource.  TT fonts are defined by an 'sfnt'
resource which bundles both the bitmaps and the PS code.  Conceivably, a
similar wrapper could be designed for PS fonts, but that is vaporware at
the present.

So, your code has a text run which uses a given font/face/size, and you
want to calculate its width.  You need to get access to the FOND resource,
and debreif it to see if the PS name table exists (If not, use existing QD
technology).  If PS, then check for ATM, and get it to measure the text for
you.  If no ATM, then look for the relevant afm file, and parse it for the
characters used.  A great deal of this is implemented by the LW driver,
which you could explore with your fav disassembler.

Ref: IM 1, 4, 5, 6; revIM font chapter, in 'text' (i think); Adobe AFM
specification;  Adobe ATM programmers hook stuf (sorry, I forget the
official title).

You might want to read the comp.lang.postscript FAQ for info on dealing
with the automated adobe technical document server, and the revIM font
chapter can be gotten from ftp.apple.com, or from the ETO/develop CD-roms.

--

Dana S Emery <de19@umail.umd.edu> | "Novo, de Novo,
                                  |     de novo, de no-o-o-o-o---, 
                                  | Novemba come an' dey gonna go home."
Newsgroups: comp.sys.mac.programmer