|
 |
CS 3723/3721
Programming Languages
Right Justifying Text
|
|
Left Justifying: In Postscript it
is trivial to print text that is left justified
(aligned on the left), because you need only move to the same
x coordinate and start printing (with show).
Right Justifying: For various
applications it is desirable to right
justify text (aligned on the right). Right justification is presented in
the Bluebook on pages 42-45. This web page has another explanation
that may help you understand.
Right and Left Justifying: Actually,
the most common justification
is left and right at the same time, often used in books and magazines.
You might think that this is the task of a word processor, but typical word
processors use something like Postscript as their low-level engine
to implement the desired results. Postscript has sophisticated
features to accomplish this, but we won't study them.
(The kshow command can be used for this purpose in a
complicated way.)
The stringwidth operator is what we use for right justification.
It returns the (horizontal) width of a string, and it also returns
the (vertical) "width", what one might call the height. The second
number returned is usually not needed, so most programs use a
pop to get rid of it.
In the first example below, we want to align a text string s
against the line x = 500. Suppose w is the width
of s, returned as the first number from
We will assume a constant height y on the page for
a given line. So first one does a to move
the current point over against the desired right margin. Then do an
to move w units horizontally and to the left
to the point where the string should start, if it is to
end at x coordinate 500. Then just do s show. This
is what the program below does for the first two lines that it
prints. There is a lot of repetition in handling the two lines,
so after the row of % chars, I tried to put many of the
common things done into a single procedure. This procedure is
used to paint 7 more lines to output. Notice that the lines are
not painted in order from top to bottom.
Centering: Placing text
in the center of a page
is similar to the right justifying above. Assume you stay at the
same y coordinate. Move along the x coordinate to the vertical
center line. Then move to the left a distance equal to half the
length of the string to be printed. (Divide the distance given
by stringwidth by 2.) Then print the string with show.
(All the programs here show right justification. See also
the Bluebook, pages 58-59, for centering.)
Initial Justify Program |
%!PS-Adobe-2.0
% text that is right-justified against the line x = 500
% draw vertical line at right
500 340 moveto 500 620 lineto stroke
500 540 moveto ( x = 500) show
% right justify first line
/Helvetica-Bold findfont 15 scalefont setfont
500 600 moveto % move to right end
(Text that is right-justified against the line x = 500.)
stringwidth pop
neg 0 rmoveto % move to left end and then print
(Text that is right-justified against the line x = 500.)
show
500 580 moveto % right justify second line
(More text aligned the same way, placed below.)
stringwidth pop
neg 0 rmoveto
(More text aligned the same way, placed below.) show
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% start over, putting common stuff into a procedure
/right-justify { % stack: string y
/y exch def % handle top of stack: y coord of line
dup stringwidth pop % stack: width string
500 y moveto % move to right end
neg 0 rmoveto % move to left end
show % print string
} def
(Bring me my bow of burning gold!) 420 right-justify
(Bring me my arrows of desire!) 400 right-justify
(Bring me my spear! O clouds, unfold!) 380 right-justify
(Bring me my chariot of fire!) 360 right-justify
% header
/Times-BoldItalic findfont 25 scalefont setfont
(JERUSALEM) 500 right-justify
/Times-Bold findfont 12 scalefont setfont
((third stanze)) 486 right-justify
/Times-Bold findfont 16 scalefont setfont
(William Blake (1757-1827)) 460 right-justify
showpage |
| |
|
Next I'm putting an entire poem onto a page, arranged right-justified.
(Left-justified is more common, but this looks interesting.)
I rewrote the procedure "right-justify", calling it "rj",
and employed 4 numerical parameters and 3 font parameters
for the output. Below the code are two different versions of
the poem that come from two sets of parameter settings.
You should note that Postscript is very precise, giving you exactly
what you ask for. Of course, it would be much easier to use a
normal word processor, but then you wouldn't have the arbitrary
flexibility and exact control that Postscript provides.
First I give the Postscript code that paints the page on
the left at the bottom of this document.
More Flexible Justify Program,
with 7 Parameters Easy to Change |
%!PS-Adobe-2.0
% print a poem
/x 450 def % right end of every line
/y 680 def % initial height of line
/incr 20 def % increment between lines
/incr2 25 def % extra incr between stanzas
/next { /y y incr sub def} def % line incr
/next2 { /y y incr2 sub def} def % stanza
/headfont {/Times-BoldItalic findfont
25 scalefont setfont} def
/authorfont {/Times-Bold findfont
16 scalefont setfont} def
/bodyfont {/Helvetica-Bold findfont
15 scalefont setfont} def
% procedure to right justify against x
/rj { % stack: string
dup stringwidth pop % stack: str width
x y moveto % move to right end
neg 0 rmoveto % move to left end
show % print string
next % increment y
} def
|
headfont
(JERUSALEM) rj
authorfont
(William Blake (1757-1827)) rj next2
bodyfont
(And did those feet in ancient time) rj
(Walk upon England's mountains green?) rj
(And was the holy Lamb of God) rj
(On England's pleasant pastures seen?) rj next2
(And did the Countenance Divine) rj
(Shine forth upon our clouded hills?) rj
(And was Jerusalem builded here) rj
(Among these dark Satanic Mills? ) rj next2
(Bring me my bow of burning gold!) rj
(Bring me my arrows of desire!) rj
(Bring me my spear! O clouds, unfold!) rj
(Bring me my chariot of fire!) rj next2
(I will not cease from mental fight,) rj
(Nor shall my sword sleep in my hand,) rj
(Till we have built Jerusalem) rj
(In England's green and pleasant land.) rj
showpage
|
Below on the left are the same parameter setting as above.
On the right are a different set of parameter settings, giving
the version of the poem shown on the right below.
Small changes in global variables
change the appearance of the poem
(Differences shown in red |
First Version of Poem |
Second Version of Poem |
/x 450 def % right end of every line
/y 680 def % initial height of line
/incr 20 def % increment between lines
/incr2 25 def % extra incr between stanzas
/next { /y y incr sub def} def % line incr
/next2 { /y y incr2 sub def} def % stanza
/headfont {/Times-BoldItalic findfont
25 scalefont setfont} def
/authorfont {/Times-Bold findfont
16 scalefont setfont} def
/bodyfont {/Helvetica-Bold findfont
15 scalefont setfont} def
| /x 510 def % right end of every line
/y 720 def % initial height of line
/incr 27 def % increment between lines
/incr2 27 def % extra incr between stanzas
/next { /y y incr sub def} def % line incr
/next2 { /y y incr2 sub def} def % stanza
/headfont {/Helvetica-BoldItalic findfont
35 scalefont setfont} def
/authorfont {/Helvetica-Bold findfont
20 scalefont setfont} def
/bodyfont {/Times-Bold findfont
25 scalefont setfont} def
|
|
|
( Revision date: 2015-01-03.
Please use ISO 8601,
the International Standard Date and Time Notation.)
|