%!PS /nextline { y linedist sub /y exch def % (page wrap handling could go here...) } bind def /block_justify { % expected args: x, y, string, width of block, line distance % e.g.: 20 100 (...) 400 15 block_justify 30 dict begin % configurable params % (set those values to zero if you want to adjust % nothing but inter-word spacings) /maxcondense 0.1 def % max condense per glyph /maxstretch 0.3 def % max stretch per glyph % args /linedist exch def /blockwidth exch def dup length /txtlen exch def /txt exch def /y exch def /x exch def /j 0 def /_i 0 def /_w 0 def /_len 0 def /nspaces 0 def 0 1 txtlen 1 sub { % for each char/index of string /i exch def txt i get 32 eq { % word boundary? txt j i j sub getinterval % substring to evaluate dup length /len exch def % length of substring stringwidth pop /w exch def % width of substring w blockwidth ge { % would exceed box border? /dleft blockwidth _w sub def % distance to border from left side /dright w blockwidth sub def % distance to border from right side /maxcondense_tot maxcondense len mul def /maxstretch_tot maxstretch _len mul def dright maxcondense_tot le { /aw dright len div neg def % neg value to apply to 'ashow' /cw 0 def txt j i j sub getinterval /j i 1 add def } { dleft maxstretch_tot le { /aw dleft _len div def % pos value to apply to 'ashow' /cw 0 def } { /aw maxstretch def nspaces 1 gt { /cw dleft maxstretch_tot sub nspaces 1 sub div def % value to apply to 'widthshow' } { /cw 0 def } ifelse } ifelse txt j _i j sub getinterval /j _i 1 add def } ifelse x y moveto cw 0 32 aw 0 6 -1 roll awidthshow nextline /_i j def /_w 0 def /nspaces 0 def } { % doesn't yet exceed... /_w w def /_i i def /_len len def } ifelse /nspaces nspaces 1 add def } if } for txtlen j sub 0 gt { % remainder / last line -- left-justified x y moveto txt j txtlen j sub getinterval show nextline } if end } bind def % ----- /text (Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do \ eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad \ minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip \ ex ea commodo consequat. Duis aute irure dolor in reprehenderit in \ voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur \ sint occaecat cupidatat non proident, sunt in culpa qui officia \ deserunt mollit anim id est laborum. ) def /Helvetica findfont 15 scalefont setfont 50 700 text 500 20 block_justify /Helvetica-BoldOblique findfont 12 scalefont setfont 50 550 text 450 17 block_justify /Times-Roman findfont 15 scalefont setfont 50 400 text 450 18 block_justify /Times-Italic findfont 15 scalefont setfont 50 250 text 400 18 block_justify showpage