🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.postscript
2 messages
2 total messages Started by Willi Staudt AG-Linder Tue, 13 Oct 1992 18:15
FAQ? Text centered in a box!
#4143
Author: Willi Staudt AG-Linder
Date: Tue, 13 Oct 1992 18:15
68 lines
3217 bytes
% Hello all!

% I hope I don't bother you with an frequently asked question, but I'm a
beginner
% in postscript and I can't find the (maybe simple?) solution to the problem in
% the handbooks I have access to.

% All I want is a text printed in the center of a box!
% I included a fragment of my program, just to illustrate the problem:
% 'Box' and 'CenterShow' work fine and the text is left/right centered.
% Obviously the problem is the font height!
% When I calculated the y-coordinate I started with 'y-start + half-box-height'
% and the text showed above the middle. So I substracted 'half-font-height',
% but now the small letters 'g' and 'j' are partially outside the box!

% My question now is threefold:
% a. Why is the starting point of a letter not the lower left corner of the
%    matrix?
% b. How can I calculate the right 'offset' when I know the font height?
% c. It appears to me as if the font height 50 is not equal to my definition
%    of the box height 50 ?? Why is that so?

% Thanks a lot for any help!
%                              Willi


%!

/Box {                                 %---------------------------------------
  4 2 roll                             % draw a box
  newpath                              % stack:  x-start y-start width height
    moveto                             %---------------------------------------
    dup 0 exch rlineto
    exch 0 rlineto
    neg 0 exch rlineto
  closepath
} def

/CenterShow {                          %---------------------------------------
  dup                                  % print a string centered
  stringwidth pop 2 div neg            %---------------------------------------
  0 rmoveto
  show
} def

/x 100 def                 % lower left corner of box
/y 100 def
/w 200 def                 % width of box
/h  50 def                 % height of box
/f  50 def                 % font height

/Helvetica findfont f scalefont setfont

x y w h Box stroke         % print the box

x w 2 div add              % x-start + half-box-width
y h 2 div add f 2 div sub  % y-start + half-box-height - half-font-height
moveto

(Agthijkl) CenterShow      % show arbitrary text
showpage

%  <>==============================<>=============================<>========<>
%  || Wilhelm F.Staudt             || E-Mail:                     ||  \     ||
%  || Department of Physics        ||                             ||   \    ||
%  || University of Kaiserslautern || staudt@physik.uni-kl.de     ||   /\   ||
%  || FRG - 6750 Kaiserslautern    || staudt@rhrk.uni-kl.de       ||  /  \  ||
%  <>==============================<>=============================<>========<>
Re: FAQ? Text centered in a box!
#4149
Author: Paul Heckbert
Date: Wed, 14 Oct 1992 21:05
129 lines
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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads