[comp.lang.postscript] X11 graphics to postscript

cjmchale@swift.cs.tcd.ie (09/08/90)

[ Note: Followups to comp.lang.postscript ]

Hi,
I have a C++ graphic library in development and want it to be able
to dump postscript. I've spent the last week or so reading
up on postscript and it seems intuitively easy.
If all I was dumping was graphics (no text) then I could get
a pretty good dump. Simply treat an X window pixel as a point
(1/72 inch). If that's too big (some X terminals may be 100 dots
per inch) then the resultant postscript file can be scaled appropiately.
There seems to be a one to one correspondence between an X11 GC
and a postscript graphic state (line width, join style, etc)
which helps. (I suppose X borrowed these from postscript.)

But I think that text might cause problems.

In answer to somebody's query on how to get hardcopy of X
window output fgreco@shearson.com writes:
>If you are writing your own routines, you typically have some sort of
>data structure that describes the things you're plotting (e.g., a
>display list).  In your repaint proc, you traverse your data structure
>and call the appropriate Xlib routines to display things.  So if you
>wanted to produce PostScript, then "all you have to do" is, when you're
>traversing your data structure(s), produce PostScript calls instead.

Fine so far.

>If you're going to use fonts for labeling axes and things....you're
>probably not going to get a perfect duplicate of your screen, but it
>should be close if you make some careful decisions on screen-fonts v.s.
>Postscript fonts.

This is the bit that interests (and worries) me.
There is a certain application which
I would like to write. It involves writing text and
enclosing it in a box or oval. There will be lines
connecting such boxes/ovals together. I've protoytped the X
display of such text and graphics; it looks fine.

I reckon that it might look rather awful if dumped to
postscript since the text might not fix inside the box/oval
or might not be centered etc. I suppose I could do some hacks
within the application to correct this but I'd rather put
the ability to dump postscript into the graphics library and
have it produce a good hardcopy with little or no
handholding from the application.

I'll describe the way I plan to approach this. Perhaps
somebody will be good enough to tell me if I'm
approaching this wrong or if you know of a better way.

1. Only use X11 fonts which have a good chance of mapping
accurately to postscript fonts i.e., only use the X11 fonts
which Adobe supplies (variations on Time, courier, symbol
etc) since these are likely to have a good correspondence to
the postscript fonts in a printer.
(When I say "only use such fonts" I mean: have them as
defaults and strongly discourage the arbitary overriding
of such defaults.)

2. Determine which screen resolution the X11 fonts are
designed for. (An application resourse settable in a
.Xdefaults file can do this.) For example, many fonts are
designed for 75 dpi screens and have counterparts for 100
dpi screens. Get this number (75 or 100 or whatever).

3. Use the X11 font resolution (determined in (2) above) to
scale everything down when dumping the graphics to
postscript. For example, if the screen font resolution was
100 dpi then an X window coordinate of (x, y) gets mapped to
a postscript coordinate of

	(x * scale, y * scale)

where scale = 72.0 / 100.0	/* postscript default resolution = 72 dpi */
This scaling would be preformed transparantly by the
graphics library. For example if I created a "line" graphical
object which went from (10,20) to (40,50) then
	line.X11_draw()
would execute code of the form
	XDrawLine(dpy, win, 10, 20, 40, 50)
while
	line.PS_draw()
might generate postscript code something like
	/scale { 72.0 mul 100.0 div } def
	newpath
	10 scale 20 scale moveto	% start coords
	40 scale 50 scale lineto	% end coords
	stroke

4. If I want to center some text in a rectangle/oval then I
subclass in my graphics class hierarchy and create a new
graphics object for this. This object will take extra care
when dumping postscript to ensure that the text is centered
inside the rectangle/oval on the postscript page.


Is this a good approach? Comments, flames, suggestions welcome.
Either reply by email and I'll summarise if there is interest
or followup to comp.lang.postscript (to keep the traffic off
comp.windows.x).

Thanks in advance,
Ciaran.
-------
Ciaran McHale
Department of Computer Science, Trinity College, Dublin 2, Ireland.
Telephone: +353-1-772941 ext 1538    FAX: +353-1-772204   Telex: 93782 TCD EI
email: cjmchale@cs.tcd.ie	or	cjmchale%cs.tcd.ie@cunyvm.cuny.edu
	My opinions are mine but you can share them too if you want.

jwz@lucid.com (Jamie Zawinski) (09/11/90)

> 4. If I want to center some text in a rectangle/oval then I
> subclass in my graphics class hierarchy and create a new
> graphics object for this. This object will take extra care
> when dumping postscript to ensure that the text is centered
> inside the rectangle/oval on the postscript page.

This sounds like the way to go to me.  One possible way to make sure things
fit is, compute the left and right pixel position which the string occupies
on the X display, map that area into PostScript page space, and scale the
PostScript font so that the string in question exactly fits in that rectangle.
This means that mapping "iiii" in some random X font to PostScript, and
mapping "wwww" in the same font to PostScript might actually use differently-
scaled PostScript fonts, but you would be guarenteed that the font which was
used would not cause the string to overlap its allotted space.  Depending on
the application, though, it might be noticeable that the aspect ratio of the
fonts of the various pieces of text was changing slightly, but for a graph-
like thing, that seems unlikely.

		-- Jamie