Post Scriptum language

// Example 12

wordbreak = " ";

procedure BreakIntoLines( textstring, linelength, proc )
{
        sw = [ stringwidth(wordbreak) ];
        breakwidth = sw[ 0 ];
        curwidth = 0;
        lastwordbreak = 0;
        startchar = 0;
        restoftext = textstring;

        loop {
                sresult = [ search( restoftext, wordbreak ) ];
                if ( sresult[ length(sresult)-1 ] ) {
                        nextword = sresult[ 2 ];
                        restoftext = sresult[ 0 ];
                        sw = [ stringwidth(nextword) ];
                        wordwidth = sw[ 0 ];

                        if ( curwidth + wordwidth > linelength ) {
                                proc( getinterval( textstring, 
                                      startchar,
                                      lastwordbreak-startchar ));

                                startchar = lastwordbreak;
                                curwidth = wordwidth + breakwidth;
                        }
                        else 
                                curwidth += wordwidth + breakwidth;

                        lastwordbreak += length(nextword) + 1;
                }
                else break;
        }

        lastchar = length(textstring);
        proc( getinterval( textstring, startchar,
                                lastchar-startchar ));
}

setfont( scalefont( findfont('Times-Roman'), 16 ));

glo = [ yline: 650 ];

procedure line_proc( str ) {
        moveto( 72, glo.yline );
        show( str );
        glo.yline -= 18;
}

// Literal strings are glued.
// Octal character code is escaped with backslash
// (as in PostScript)
BreakIntoLines( 
"In every period there have been better or worse types "
"employed in better or worse ways. The better types "
"employed in better ways have been used by the educated "
"printer acquainted with standards and history, "
"directed by taste and a sense of the fitness of things,"
" and facing the industrial conditions and the needs of "
"his time.  Such men have made of printing an art. "
"The poorer types and methods have been employed by "
"printers ignorant of standards and caring alone for "
"commercial success. To these, printing has been simply"
" a trade. The typography of a nation has been good or "
"bad as one or other of these classes had the supremacy. "
"And to-day any intelligent printer can educate his taste, "
"so to choose types for his work and so to use them, that "
"he will help printing to be an art rather than a trade. "
"\261Daniel Berkeley Updike.",
300, line_proc );

showpage();