Article View: comp.lang.postscript
Article #4149Re: FAQ? Text centered in a box!
From: Paul Heckbert
Date: Wed, 14 Oct 1992 21:05
Date: Wed, 14 Oct 1992 21:05
129 lines
5094 bytes
5094 bytes
In article <1992Oct13.181507.4697@rhrk.uni-kl.de> staudt@physik.uni-kl.de (Willi Staudt AG-Linder) writes:
>
>% All I want is a text printed in the center of a box!
Here is a very versatile postscript routine that could help you.
-Paul
Paul Heckbert ph@cs.cmu.edu
Computer Science Dept., Carnegie Mellon University
5000 Forbes Ave, Pittsburgh PA 15213-3891, USA
--------------------------- showposdemo.ps
%!
% Demonstration of text justification routine "showpos"
% This postscript subroutine can left-justify, right-justify, and center
% text at any orientation, with any font transformation matrix.
%
% The essential definitions are defaultfontmatrixinv and showpos.
%
% Paul Heckbert, ph@cs.cmu.edu, Jan 1989
% The following routine may be needed if you're previewing this file using
% SGI's psview program.
%
% % invertmatrix: invert matrix m1 into matrix m2
% % this is a standard Postscript operator, but SGI's NEWS ain't got it.
% %
% % [a d 0] [ e -d 0]
% % m1 = [b e 0] m1^-1 = [ -b a 0]/(ae-bd)
% % [c f 1] [bf-ce cd-af 1]
%
% /invertmatrix { % m1 m2
% 7 dict begin
% exch % m2 m1
% aload pop % m2 a d b e c f
% /f exch def
% /c exch def
% /e exch def
% /b exch def
% /d exch def
% /a exch def % m2
% /x a e mul b d mul sub % m2 /x det(m1)
% def
% e x div
% d neg x div
% b neg x div
% a x div
% b f mul c e mul sub x div
% c d mul a f mul sub x div
% % m2 e/x -d/x -b/x a/x (bf-ce)/x (cd-af)/x
% 7 -1 roll
% % e/x -d/x -b/x a/x (bf-ce)/x (cd-af)/x m2
% astore % m2
% end
% } def
/defaultfontmatrixinv % inverse of matrix for unscaled font
/Helvetica findfont /FontMatrix get matrix invertmatrix
def
% showpos: draw string with character-relative positioning
% input: x y fx fy str
%
% where (x,y) is point in user space at which to position (fx,fy) of char space
% where char space has (0,0) at origin of string,
% (0,1) is interline spacing vector
% and (length(str),0) is end of string vector
%
% e.g. (fx,fy)=(0,0) lower-left justifies,
% (.5*length(str),.5) centers,
% (length(str),1) upper-right justifies
%
/showpos {
% x y fx fy str
dup 6 1 roll % str x y fx fy str
dup length % str x y fx fy str len
4 -1 roll exch div % str x y fy str fx/len (relative fx)
3 1 roll % str x y fx' fy str
% find vector (hx,hy) along width of string and scale it by fx':
stringwidth % str x y fx' fy hx hy
3 index mul exch % str x y fx' fy fx'*hy hx
4 -1 roll mul % str x y fy fx'*hy fx'*hx
% find vector (vx,vy) along height of string and scale it by fy:
0 1 % str x y fy hy' hx' 0 1
currentfont /FontMatrix get % .. FD (font to device transform)
defaultfontmatrixinv matrix % .. FD DU mat (times device to user transform)
concatmatrix % .. FU (equals relative,font to user transform)
% str x y fy hy' hx' 0 1 FU
transform % str x y fy hy' hx' vx vy
4 index mul exch % str x y fy hy' hx' fy*vy vx
5 -1 roll mul % str x y hy' hx' fy*vy fy*vx
% str x y hy hx vy vx (drop the primes)
% add the displacement vectors (hx,hy) and (vx,vy):
3 -1 roll add % str x y hy vy hx+vx
3 1 roll add % str x y hx+vx hy+vy
% str x y dx dy (abbrev)
% subtract from reference point:
3 -1 roll exch sub % str x dx y-dy
3 1 roll sub exch % str x-dx y-dy
moveto show % (yay!)
} def
% --------------------------------------------------------------------
% and here's a little demo
72 dup scale % user space is inches
.02 setlinewidth % cross-hairs
.25 .25 moveto 8.25 10.75 lineto stroke
8.25 .25 moveto .25 10.75 lineto stroke
/Times-Roman findfont .5 scalefont setfont
.25 .25 0 0 (lower left) showpos % lower-left justify
% (fx,fy)=(0,0) is "char space" coords of lower left of string
8.25 10.75 11 1 (upper right) showpos % upper-right justify
% (fx,fy)=(11,1) is "char space" coords of upper right of 11-char string
8.25 10.25 23 1 (note how these line up!) showpos
% (fx,fy)=(23,1) is "char space" coords of upper right of 23-char string
/Helvetica findfont [.5 .5 -2 2 0 0] makefont setfont
4.25 5.5 3 .4 (CENTER) showpos % centered, rotated with scaling
% (fx,fy)=(3,.4) is "char space" coords of approx center of 6-char string
showpage
Technical Details
Message-ID:
<Bw4qKI.6qy.1@cs.cmu.edu>Path:
arc1-ca.pugleaf.net!sparky!uunet!kithrup!hoptoad!pacbell.com!ames!agate!usenet.ins.cwru.edu!magnus.acs.ohio-state.edu!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!news.sei.cmu.edu!fs7.ece.cmu.edu!crabapple.srv.cs.cmu.edu!phReferences:
<1992Oct13.181507.4697@rhrk.uni-kl.de>Show Full Headers
Complete Article Headers
Newsgroups: comp.lang.postscript Path: arc1-ca.pugleaf.net!sparky!uunet!kithrup!hoptoad!pacbell.com!ames!agate!usenet.ins.cwru.edu!magnus.acs.ohio-state.edu!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!news.sei.cmu.edu!fs7.ece.cmu.edu!crabapple.srv.cs.cmu.edu!ph From: ph+@cs.cmu.edu (Paul Heckbert) Subject: Re: FAQ? Text centered in a box! Message-ID: <Bw4qKI.6qy.1@cs.cmu.edu> Sender: news@cs.cmu.edu (Usenet News System) Nntp-Posting-Host: hostess.graphics.cs.cmu.edu Organization: School of Computer Science, Carnegie Mellon References: <1992Oct13.181507.4697@rhrk.uni-kl.de> Date: Wed, 14 Oct 1992 21:05:02 GMT Lines: 129