Advice sought on ps module

Eddie Corns eddie at
Thu Jun 20 16:28:32 CEST 2002

hjwidmaier at (Hans-Joachim Widmaier) writes:

>About 2 years ago I wrote a primitive PostScript module to help me
>create various things like CD inlays or diagrams. It served that
>purpose quite well, despite its primitiveness.
>Since long I wanted to rewrite the whole thing from scratch and do it
>(at least kind of) "right." But I'm still not so sure as which way to
>go. At one time I thought I might employ ghostscript, which would have
>enabled me to, e. g. get the string width for some text, so I can make
>adjustments (not only to the text). Apart from that I couldn't get a
>sensible communication with gs through popen2, the PostScript output
>is created somewhat nonlinear, making it harder to ensure that the gs
>context is correct (in that query case).
>I'm convinced that's the wrong way, but the alternative doesn't look
>too good either. If you want to do some optimizations, you have to
>double some functionality of the ps interpreter.

>  - To calculate the with of a string, you have to read the AFM file,
>add the character widths, aplly kerning, transform it with the CTM and
>the fonts TM.
>  - To know whether a font must be reset or is already the current
>font you have to keep track of all the save/restore, gsave/grestore
>and the like.

>I do not want to write a "do all, end all" PostScript module. I do not
>even want to give it away (I'd be simply too embarrassed).

>Still I hope that some of you might give me some good advice on how to
>best do it.

I'm not sure that I understand completely what you're trying to do so maybe
I'm barking up the wrong gum tree here but ...

Surely you want to use postscript itself to do all the work.  PS already has
an operator called stringwidth and a complete set of programming constructs so
the trick usually is to generate the appropriate PS commands.  As an example,
to draw a border around some arbitrary text I guess you could generate code to
do something like:  save current position, for each string you draw calculate
the bounding box and remember the highest X value and at the end use the new
position along with the saved position and highest X value to define the four
corners of a rectangle.  It does require a fair degree of competence in PS but
you wouldn't tackle such a project without it (or a desire to achieve it)
would you :)


More information about the Python-list mailing list