Re: Question about "getting" font width values for same-font characters
Re: Question about "getting" font width values for same-font characters
- Subject: Re: Question about "getting" font width values for same-font characters
- From: steve harley <email@hidden>
- Date: Sun, 8 Feb 2004 16:52:13 -0700
on 7 Feb 2004, at 3:31 PM, Macintosh info wrote:
With an eye toward automating a variety of typesetting processes in
QuarkXPress and/or InDesign, I'd like to know if it is possible through
AppleScript to discern the absolute width (in inches, or millimeters,
or
points) of any character of a given typeface at any particular size,
and
doing so (hopefully) by accessing some kind of constant value for the
character, preferably from the font itself, and preferably without
setting
up scripts to run through numerous tests that would discern these
values?
i've done this, for exactly the kind of purpose you describe (i wanted
detailed control of linebreaks in directory publishing).. what i did
was build tables (in Visual FoxPro) of each field's typeface & point
size; plus the width of each character in each font used at a size of
one point.. i could then compute the width of arbitrary text.. i got
the nominal widths directly from each PostScript font's AFM (Adobe Font
Metrics) file.. Adobe type libraries supply AFM files.. AFMs give the
values in thousandths of a point at a type size of one point.. if you
don't have AFMs, you can derive them with Fontographer or other means,
such as the free font developer kit Adobe offers (written in Python,
btw)
my task used exclusively PostScript fonts with single-byte
(non-Unicode) character sets.. i've started to look for a more flexible
method.. here's one attempt using character offsets in InDesign.. as
configured (just for a quick test), it derives values for most
low-ASCII characters.. the script would have to be significantly
enhanced to do a full Unicode glyph set.. it also doesn't account for
ligatures, optical margin alignment, and probably some other things
on get_metrics(char_set, the_font, the_style)
set set_len to length of char_set
set metrics to {}
tell application "InDesign 2.0.2"
make new document at front
tell front document
-- clean document properties
tell view preferences
set horizontal measurement units to inches decimal
set vertical measurement units to inches decimal
set typographic measurement units to points
-- can't set text size measurement units ???
end tell
make new text frame with properties {geometric bounds:{1.0, 1.0,
1.25, 1.25}}
set selection to text frame 1
end tell
-- zoom in on tiny frame for entertainment
tell front window to set zoom percentage to 1600
tell story 1 of front document
-- clean text properties
tell text 1
set {applied font, font style} to {the_font, the_style}
set {kerning method, kerning value} to {"None", 0}
set {tracking, horizontal scale} to {100, 100}
set {point size, leading} to {1, 2}
set justification to left
end tell
repeat with c from 1 to set_len
set char_to_measure to item c of char_set
-- prepend a character to avoid margin issues
set text of it to "_" & char_to_measure & "_"
set offset1 to horizontal offset of character 2
set offset2 to horizontal offset of character 3
set the_width to offset2 - offset1
set metrics to metrics & {{char_to_measure, the_width}}
end repeat
end tell
end tell
end get_metrics
set test_char_set to
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
,./;'[]\\`-=<>?:\"{}|~!@#$%^&*()_+ "
my get_metrics(test_char_set, "Futura", "Book")
for some reason, the values it gives are significantly larger than the
AFM values.. for example an upper case A in Futura Book is 0.699 point,
or 0.0097", per the AFM, but 0.0111" per this script.. for a reality
check i used InDesign at 4000% magnification to manually measure the
location of the insertion point before & after a 100pt Futura Book
"A".. i got 0.01097" -- very close to the script value, so i suspect
either i'm missing a factor or InDesign does some kind of
transformation on the metrics in the font.. more investigation needed
...
meanwhile, i also tried Emmanuel's suggestion with this Smile script:
on get_metrics_smile(char_set, the_font)
set test_font to {text font:the_font, text size:1, justification:left}
set set_len to length of char_set
set metrics to {}
repeat with c from 1 to set_len
set char_to_measure to item c of char_set
set the_width to width of MeasureTextBox(char_to_measure, test_font)
set metrics to metrics & {{char_to_measure, the_width}
end repeat
end get_metrics_smile
set test_char_set to
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890
,./;'[]\\`-=<>?:\"{}|~!@#$%^&*()_+ "
my get_metrics_smile(test_char_set, "Futura Book")
and for "A" got the result 0.698989868164, in points.. this is almost
identical to the AFM value.. the AFM values have proved very accurate
when used to compose Quark XPress 4.x documents, but i haven't used
those values yet to compose InDesign documents
eventually, i'd rather derive these font metrics in Frontier or Python,
but i don't offhand know a way to use Smile's MeasureTextBox from
outside of Smile, so i may be reduced to working with system frameworks
in Python
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.