[comp.lang.postscript] Determine string-length in program

buys@pttrnl.nl (01/24/91)

Does anyone have a solution for the following problem:

I want to generate PostScript code from a program that prints multiple lines on
a printer-label in a proportional spaced font. 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.)
Any suggestions are welcome.

J.P. Buys
Neher Laboratories
Leidschendam

JP_Buys@pttrnl.nl

Bruce.Hoult@bbs.actrix.gen.nz (01/29/91)

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."

Bruce.Hoult@bbs.actrix.gen.nz (01/29/91)

I should have mentioned that you could use the example line breaking
algorithm on P179 of the Blue Book as a start for what you want to do.
-- 
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."

sun@me.utoronto.ca (Andy Sun Anu-guest) (01/30/91)

Bruce.Hoult@bbs.actrix.gen.nz writes:

>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.)

I think the blue book (cook book) on PostScript has an example of
doing a crude line wrapper. What it does is looking for spaces
and break the string into one word at a time using "search" (every
search will give rise to (word) ( ) (the rest of string)). The
length of the word is computed (using stringwidth) and add to current
line length. If the line length does not exceed the limit, it will
keep going; otherwise, start a newline and reset the linelength couter.
The final output is left-justified only (it's easy to make it
right-justified too, I don't know of a simple way to do left-right-
justification...) and it doesn't do word-wrap.

Andy