Article View: comp.lang.postscript
Article #4567Re: Determine string-length in program
From: Bruce.Hoult
Date: Tue, 29 Jan 1991 04:53
Date: Tue, 29 Jan 1991 04:53
45 lines
2124 bytes
2124 bytes
J.P. Buys writes:
>My problem is that I don't know how I can determine the length
>of a string so that I can break the line in multiple strings
>that fit within the borders of the printer label (including
>word-wrap etc.)
Unless you can get a copy of the font metric tables on your computer,
you'll just have to do it with PostScript code. It's pretty easy to
do string-handling stuff in PostScript, so long as you don't try to
concatenate strings.
I'd just step through the string a word at a time, for each word
deciding if it will fit in the remaining space on the line. If
it will then print it, otherwise move to the next line befoe printing
it.
If you're planning to adjust the font size dynamically to make the
string fit nicely then you could write an iterator function that is
passed an action procedure to called for each word, and another for
each line etc. You might end up using it something like this:
/myFont findfont someSize scalefont setfont
/linecount 0 def
theString {pop} {/linecount linecount 1 add def} findLineBreaks
... then after some calculations ...
/lineY labelTop def
labelLeft labelTop moveto
theString
{show}
{/lineY lineY linespacing sub def labelLeft lineY moveto}
findLineBreaks
... where findLineBreaks is your iterator function.
Hope this helps.
--
Bruce.Hoult@bbs.actrix.gen.nz Twisted pair: +64 4 772 116
BIX: brucehoult Last Resort: PO Box 4145 Wellington, NZ
"And they shall beat their swords into plowshares, for if you hit a man
with a plowshare, he's going to know he's been hit."
Technical Details
Message-ID:
<1991Jan29.045347.2535@actrix.gen.nz>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!comp.vuw.ac.nz!actrix!Bruce.HoultReferences:
<1991Jan24.090812.64950@pttrnl.nl>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!comp.vuw.ac.nz!actrix!Bruce.Hoult From: Bruce.Hoult@bbs.actrix.gen.nz Newsgroups: comp.lang.postscript Subject: Re: Determine string-length in program Message-ID: <1991Jan29.045347.2535@actrix.gen.nz> Date: 29 Jan 91 04:53:47 GMT References: <1991Jan24.090812.64950@pttrnl.nl> Sender: Bruce.Hoult@actrix.gen.nz (Bruce Hoult) Organization: Actrix Information Exchange, Wellington, New Zealand Lines: 45 Comment-To: buys@pttrnl.nl