From: jgm@cs.brown.edu (Jonathan Monsarrat)
Subject: Re: Justifying strings, how??
Organization: Brown University Department of Computer Science
Date: Wed, 16 Dec 1992 14:54:05 GMT

> I want to be able to justify strings; left, right and middle.

Something like this should work. Use the "stringwidth" command to
get the length of the string you want to print. stringwidth returns
two values -- a X and a Y displacement. The Y is usually zero, so
"stringwidth pop" is a nice combination which will give you X displacement.

The below program is a bitty example of what you want to do.

If you want something a little more sophisticated and fancy, try
out the Rect.ps library for PostScript programmers. You can get it
and example files by ftp to wilma.cs.brown.edu:pub/postscript/Rect.

If you want something even fancier, try a drawing program that
produces Encapsulated PostScript.

-Jon
%! Jon Monsarrat   jgm@cs.brown.edu   Brown University %! Obfuscated PostScript
/f{dup 0 1 getinterval cvn exch cvx def}def<73657467726179>f<6D6F7665746F>f<63
6C6F736570617468>f(translate)f<6C696E65746F>f/p{45 rotate}def<726375727665746F
>f 144 36 m 360 36 l 360 396 l 144 396 l c 0.2 s fill 201.6 216 m 144 0 288 0
230 216 curveto c 1 s fill 216 dup t p 1 1 50{0 0 m 1 1 6{pop{rand 1073741824
div 2 div} cvx dup 1 exch exec sub s exec 288 mul -72 add } for r currentpoint
stroke gsave t .5 3 {setlinewidth s newpath 0 0 m 1 1 8{pop 3.6 0 18 13.68 0
7.2 r p}for}.8 10 2 index exec fill exec stroke grestore} for showpage


-----------------------  ex_justify.ps  ---------------------
%! PostScript quickie by Jon Monsarrat, jgm@cs.brown.edu
/SIZE 50 def                                   % What size will my font be?
/Times-Roman findfont SIZE scalefont setfont   % What will my font be?
/inch {72 mul} def                             % For convenience
/cm {28.34} def                                % For those who like metric
/rightmargin 7 inch def   % The right margin of the text
/leftmargin 1 inch def    % The left margin of the text
/bottommargin 1 inch def  % (do you really need a comment here?)
/topmargin 10 inch def    % (this comment intentionally left blank)

/drop      % Drop down one line from the current position.
{
  /height height SIZE 1.2 mul sub def    % Why 1.2? It usually works nicely.
  % if the height of the current text is below the bottommargin, new page!!
  height bottommargin lt { showpage /height topmargin def drop } if
} bind def

/rightj    % Takes a string off the stack and prints it right flushed
{
  dup stringwidth pop rightmargin exch sub height moveto show
  drop
} bind def

/leftj     % Takes a string off the stack and prints it left flushed
{
  leftmargin height moveto show
  drop
} bind def

/center    % Takes a string off the stack and prints it centered
{
  dup stringwidth pop rightmargin exch sub leftmargin sub 2 div
  leftmargin add height moveto show
  drop
} bind def

% The "height" variable is the current height of the text about to be
% printed.
/height topmargin def drop

(right)      rightj
(justified)  rightj
(text)       rightj

(left)       leftj
(justified)  leftj
(text)       leftj

(center)     center
(justified)  center
(text)       center

showpage
Newsgroups: comp.lang.postscript