Post Scriptum language

// Example 19 

procedure ModifyWidths( basefontname, newfontname, newwidths, uniqueid )
{
        basefontdict = findfont( basefontname );
        numentries = maxlength( basefontdict ) + 1;
        if ( !known( basefontdict, 'UniqueID' ))
                numentries += 1;

        newfont = dict( numentries );

        for ( id:val in basefontdict ) {
                if ( id != 'FID' && id != 'FontBBox' )
                        newfont[ id ] = val;
        }

        oldFontBBox = basefontdict.FontBBox;
        newFontBBox = copy( oldFontBBox, array( length( oldFontBBox )));

        newfont.FontBBox = newFontBBox;
        newfont.FontName = newfontname;
        newfont.Metrics = newwidths;
        newfont.UniqueID = uniqueid;
        definefont( newfontname, newfont );
}


procedure roundwidths( fontname, resolution, ptsize )
{
	showstring = string( 1 );

        thefont = findfont( fontname );
        newwidths = dict( length( thefont.CharStrings ));
        pixelsperem = resolution * ptsize/72;
        unitsperpixel = 1000/pixelsperem;
        gsave();
        nulldevice();
        setfont( scalefont( thefont, 1 ));
        charcount = 0;
        for ( charname in thefont.Encoding ) {
                if ( charname != '.notdef' ) {
                        showstring[ 0 ] = charcount;
                        sw = [ stringwidth( showstring ) ];
                        charwidth = sw[0] * 1000;
                        multiples = cvi( round( charwidth/unitsperpixel ));
                        newcharwidth = unitsperpixel * multiples;
                        newwidths[ charname ] = newcharwidth;
                }

                charcount += 1;
        }

        grestore();
        return newwidths;
}

tempmatrix = matrix();
epsilon = 0.001;

procedure findresolution()
{
        xy = [ dtransform( 72, 0, defaultmatrix( tempmatrix ) ) ];
        x = xy[ 0 ];
        y = xy[ 1 ];

        if ( abs(x) > epsilon && abs(y) > epsilon )
                stop();
        else
                return sqrt( x*x + y*y );
}

procedure showstring() 
{
     show( "HOHOHOHO oaobocodoeofogohoiojoko" );
     show( "lomonopoqorosotouovowoxoyoz" );
}

res = findresolution();
if ( known( findfont( 'Times-Roman' ), 'UniqueID' ))
	uid = findfont( 'Times-Roman' ).UniqueID;
else 
	uid = 0;
	
	
rwid = roundwidths( 'Times-Roman', res, 6 );

ModifyWidths( 'Times-Roman', 'TR6', rwid, uid + 1 );
setfont( scalefont( findfont( 'Times-Roman' ), 6 ));
moveto( 130, 560 );
showstring();

setfont( scalefont( findfont( 'TR6' ), 6 ));
moveto( 130, 560 - 6 );
showstring();

rwid = roundwidths( 'Times-Roman', res, 7 );
ModifyWidths( 'Times-Roman', 'TR7', rwid, uid + 2 );
setfont( scalefont( findfont( 'Times-Roman' ), 7 ));

moveto( 130, 500 );
showstring();

setfont( scalefont( findfont( 'TR7' ), 7 ));
moveto( 130, 500 - 7 );
showstring();

rwid = roundwidths( 'Times-Roman', res, 8 );
ModifyWidths( 'Times-Roman', 'TR8', rwid, uid + 3 );
setfont( scalefont( findfont( 'Times-Roman' ), 8 ));

moveto( 130, 440 );
showstring();

setfont( scalefont( findfont( 'TR8' ), 8 ));
moveto( 130, 440 - 8 );
showstring();
showpage();