Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.postscript > #3515

Re: Challenge: clean up this old script

Newsgroups comp.lang.postscript
Date 2020-05-13 17:34 -0700
References <cb697609-7ed7-463c-b7ab-87e15cb147b1@googlegroups.com>
Message-ID <dfbf4f4d-4b1b-4acb-8b4e-362cc46fc8cc@googlegroups.com> (permalink)
Subject Re: Challenge: clean up this old script
From luser droog <luser.droog@gmail.com>

Show all headers | View raw


On Tuesday, May 12, 2020 at 4:34:26 PM UTC-5, luser droog wrote:
> I found this post on stackoverflow where someone was trying to use some 
> old postscript code from the internet and it wasn't quite doing what he 
> wanted due to portability issues in the code.
> 
> I'm busy with finals week in school so I can't put in the time to work
> on it. But if anyone is looking for a (light) PS programming effort,
> I the results will be entertaining for the group.
> 
> SO question:
> https://stackoverflow.com/q/59950318/733077/why-does-this-postscript-ps-file-create-way-more-top-margin-than-specified
> 
> 
> %!
> %
> % From: Jonathan Monsarrat (jgm@cs.brown.edu)
> % Subject: PostScript -> ASCII *and* ASCII -> PostScript programs
> % Newsgroups: comp.lang.postscript
> % Date: 1992-10-01 04:45:38 PST 
> %
> % "If anyone is interested, here is an interesting program written by
> % Professor John Hughes here at Brown University that formats ASCII
> % in PostScript without a machine generator of any kind."
> %
> %%%
> %%% Plan:
> %%% Start with an empty string.
> %%% For each character in the input stream, 
> %%%    check to see if it's a carriage return.
> %%%    if so, show the current string and reset it to empty
> %%%    if not, add it to the current string.
> 

I got both my finals turned in, so I looked closer at this code.
It's actually pretty decent overall. I did not make a lot of changes.
Changed some names, factored out the ++ operation, simplify calculation
of the leading (nee lineheight). And most importantly replace the
explicit 
  /pageheight 11 inch def
with your choice between taking the maximum Y value from the page device
or the clipping path.

The bizarre "dup 500 gt" I left alone. I can't see that it hurts anything,
but I can't imagine a situation where it would accomplish anything either.

Modified version:


/Courier 10 selectfont
/leading gsave
 currentfont
 dup /FontMatrix get matrix currentmatrix matrix invertmatrix concat concat
 /FontBBox get aload pop % llx lly urx ury
 dtransform 3 2 roll dtransform % urx' ury' llx' lly'
 exch pop sub exch pop % ury-lly
grestore def

/buf 500 string def
/empty 500 string def
/ptr 0 def
/inch {72 mul} def
/pageheight currentpagedevice /PageSize get 1 get def
%/pageheight newpath clippath pathbbox 4 1 roll pop pop pop def
/topmargin 1 inch def
/botmargin 1 inch def
/leftmargin 1 inch def
/linesperpage pageheight topmargin sub botmargin sub leading div cvi def
/linenumber 1 def % the line we're about to write on
/++ {dup load 1 add store} def

/newline {
    linenumber linesperpage gt {/linenumber 1 def showpage} if
    leftmargin pageheight topmargin sub linenumber leading mul sub moveto
    /linenumber ++
} def

/cleanup {
    buf show showpage
} def

/startstring {
    buf 0 empty putinterval
    /ptr 0 def
} def

/showstring {
    newline
    buf show
    startstring
} def

/addtostring {
    dup 500 gt {pop}{
        buf exch ptr exch put
	/ptr ++
    } ifelse
} def

{
    currentfile read {}{cleanup exit} ifelse
    dup 10 eq 
        {pop showstring}
        {dup 0 eq   
	    {exit}
            {addtostring}
            ifelse
        }
    ifelse
} loop

Show me the money!

Back to comp.lang.postscript | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Challenge: clean up this old script luser droog <luser.droog@gmail.com> - 2020-05-12 14:34 -0700
  Re: Challenge: clean up this old script luser droog <luser.droog@gmail.com> - 2020-05-13 17:34 -0700
    Re: Challenge: clean up this old script ken <ken@spamcop.net> - 2020-05-14 07:53 +0100
      Re: Challenge: clean up this old script luser droog <luser.droog@gmail.com> - 2020-05-14 07:35 -0700
    Re: Challenge: clean up this old script luser droog <luser.droog@gmail.com> - 2020-05-15 09:34 -0700

csiph-web