Paste: justify

Author: psiloveyou
Mode: postscript
Date: Sat, 16 Jun 2012 01:14:21
Plain Text |
/concat % (a) (b) -> (ab)
{ exch dup length
  2 index length add string
  dup dup 4 2 roll copy length
  4 -1 roll putinterval
} def
% /name ( -- str) concat! (stuff in name -- str)
/concat! { exch dup load 3 -1 roll concat def } def

% Page setup.
/pagenumber 0 def
/newpage {
  pagenumber 0 gt { grestore showpage } if gsave
  6 6 6 pop translate
  /pagenumber 1 incr
  /lineheight gridh def
  /textwidth  gridv 30 mul def
  /textheight gridh 42 mul def
  % Automagic text position.
  /posx gridh 5 mul def
  /posy textheight gridv 4 mul add def
  posx posy moveto
} def

/munchword  { /TXTQ exch spacechar concat concat! } def
/resetbuf   { /TXTQ () def } def
/resetspace { /SPACELEFT textwidth def } def
/resetline  { resetbuf resetspace } def resetline

/eatspace   { /SPACELEFT exch decr } def
/spaceleft? { SPACELEFT 0 gt } def
/newline    { resetline /posy lineheight decr posx posy moveto } def
/putline    { TXTQ              % (Buncha fine words. ) putline -
  dup dup spacecount 1 sub      % 2x(Buncha fine words. ) 2
  exch stringwidth pop          % (Buncha fine words. ) 2 123     % Example width
  spacewidth sub                % (Buncha fine words. ) 2 120
  textwidth exch sub            % (Buncha fine words. ) 2 234     % String width subtracted from line width.
  exch div 0 spaceint           % (Buncha fine words. ) 117 0 ( ) % Leftover space divided between words.
  4 -1 roll widthshow newline   % 117 0 ( ) (Buncha fine words. )  % BOOM!
} def
% Last line can get special treatment.
/putlast { 0 0 spaceint TXTQ widthshow newline } def

/spacechar ( ) def
/spaceint  32  def
/spacewidth { spacechar stringwidth pop } def
/spacecount { 0 exch  % (a b c d) spacecount 3
  { dup spaceint eq { exch 1 add exch } if pop }
  forall } def
/measure { dup stringwidth pop spacewidth add } def  % (forty two wide) measure 42
/nextword { spacechar search } def    % (two words) nextword (words) ( ) (two) true / (single) nextword (single) false
/justify {
  { nextword exch measure dup eatspace spaceleft? not
    { putline eatspace } { pop } ifelse
    munchword { pop } { putlast exit } ifelse } loop
} def

New Annotation

Summary:
Author:
Mode:
Body: