Is there a "Postscript IO file for dummies"?

Substantial part The ones played on the Stack Overflow included ASCII digits, and codegolf.SE is formed in the same way.

This is all good and good, but I would like to add a little variety to the output and wrote my last question. Draw a reproducible crossword puzzle grid to explicitly encourage recordings in graphic format.

To show desire, I started trying to hack a link implementation in postscript. Alas, I am completely unfamiliar with the language, and I have problems with the underlying IO file. Does anyone know of a basic resource that covers the topic?

I know

  • file
  • (%stdin)
  • token
  • readstring
  • readline

but I don’t quite understand what exactly returned the last three results and how they manipulate it after you get it.

, <value> <boolean> if . ?

+2
3

File IO , . - "". , ( ), ( integertype realtype) ( , , marktype, ).

postscript:

%!
{ currentfile token pop /END-COMMENT eq {exit} if } loop
Each word here is read from the current file and converted
into a name object and compared to "/END-COMMENT".
It can have but does not need a preceeding slash.

This program produces an image of a snowman in a blizzard,
suitable for use as stationary.
END-COMMENT

showpage

: , ... .

:

(Some
multi-line
text)
pop

, .

loop.

{
} loop

.

/src (datafile) (r) file def
{
    src readline
} loop

, . !

/src (datafile) (r) file def
{
    src readline {
    }{
        exit
    } ifelse
} loop

, true - , false (no-more-data) . readline . .

/src (datafile) (r) file def
/str 80 string def
{
    src str readline {
        processline
    }{
        pop exit
    } ifelse
} loop

, (80 ) . Postscript, token (, , , , , ). get ( forall) . - search.

, , stdout .

/processline { % (line)
     ( ) { % (line) ( )
         search { % (post) ( ) (pre)
             =
         }{ % (no-match)
             =
             exit
         } ifelse
     } loop
} def

, , . , , boolean; . search - , . , . "" ( , "", ( )) ; " ".

, , ?

, cvi cvr . -, print show ; cvn .

+6

, ( ) PostScript, . , PostScript Turing , , , , PostScript .

PostScript - , . PostScript, PostScript, , , hardcoded. - , PostScript, .

, , . , , Python, , PostScript , PostScript.

. , PostScript . , "" PostScript , ; , .

This PostScript code does some calculations. Usually I will write functions in PostScript for things that I expect to do a lot, or for things that need data that only the PostScript interpreter will have, such as the width of the line in the current font, to center.

I didn’t clean anyone. Feel free to ask for clarification. NTN.

%!

% "Example"
% by (author)
% Generated 16 Apr 2005 22:19 by (program name)

% should really be eps

% unit conversions

/inch {72 mul} bind def
/cm {inch 2.54 div} bind def



%%%%%% start of adjustable parameters

% these may be fiddled with, within reason
% be careful not to change the syntax

% paper size
/page_width 8.5 inch def
/page_height 11 inch def

% page margins
/top_margin 1 inch def
/left_margin 1 inch def

% how many "cells" wide and high
/puzzle_width 9 def
/puzzle_height 9 def

% cell_size: height and width of a one-letter square, in points
/cell_size .25 inch def

% line width, in points. 1 is about maximum.
/line_width .375 def

% font and size for the numbers
/cell_font {/Helvetica 5} bind def

% top/left margin for numbers within a cell
/num_margin 1.5 def

%%%%%% end of adjustable parameters



<< /PageSize [page_width page_height] >> setpagedevice


/w_pts puzzle_width cell_size mul def
/h_pts puzzle_height cell_size mul def

/ulx left_margin def
/uly page_height top_margin sub def
/lrx ulx w_pts add def
/lry uly h_pts sub def


% draw grid

line_width setlinewidth
0 setgray

newpath ulx uly w_pts h_pts neg rectstroke

lry cell_size uly {
  newpath ulx exch moveto w_pts 0 rlineto stroke
} for

ulx cell_size lrx {
  newpath lry moveto 0 h_pts rlineto stroke
} for


% fill in black spaces

% ulx uly width height blackrect
% all in terms of cells; upper-left-most is 0,0
/blackrect
{
  << >> begin
  /h exch def /w exch def /y exch def /x exch def
  newpath
  ulx x cell_size mul add
  uly y cell_size mul sub
  w cell_size mul
  h cell_size mul neg
  rectfill
  end
} bind def

0 setgray
0 0 1 1 blackrect
3 0 2 1 blackrect
8 0 1 1 blackrect
4 1 1 1 blackrect
2 2 1 1 blackrect
6 2 1 1 blackrect
8 3 1 1 blackrect
0 4 2 1 blackrect
7 4 2 1 blackrect
0 5 1 1 blackrect
2 6 1 1 blackrect
6 6 1 1 blackrect
4 7 1 1 blackrect
0 8 1 1 blackrect
4 8 2 1 blackrect
8 8 1 1 blackrect


% draw numbers

% x y h s drawnum
% x and y in terms of cells; upper-left-most is 0,0. s is string.
% h is height of numbers. should never change, so compute once before any calls.
/drawnum
{
  << >> begin
  /s exch def /h exch def /y exch def /x exch def
  newpath
  ulx x cell_size mul add num_margin add
  uly y cell_size mul sub num_margin sub h sub
  moveto s show
  end
} bind def

0 setgray
cell_font selectfont

% compute font height
mark
newpath 0 0 moveto
(0) false charpath flattenpath pathbbox
/fh exch def
cleartomark newpath

1 0 fh (1) drawnum
2 0 fh (2) drawnum
5 0 fh (3) drawnum
6 0 fh (4) drawnum
7 0 fh (5) drawnum
0 1 fh (6) drawnum
3 1 fh (7) drawnum
5 1 fh (8) drawnum
8 1 fh (9) drawnum
0 2 fh (10) drawnum
3 2 fh (11) drawnum
4 2 fh (12) drawnum
7 2 fh (13) drawnum
0 3 fh (14) drawnum
2 3 fh (15) drawnum
6 3 fh (16) drawnum
2 4 fh (17) drawnum
1 5 fh (18) drawnum
7 5 fh (19) drawnum
8 5 fh (20) drawnum
0 6 fh (21) drawnum
3 6 fh (22) drawnum
7 6 fh (23) drawnum
0 7 fh (24) drawnum
2 7 fh (25) drawnum
5 7 fh (26) drawnum
6 7 fh (27) drawnum
1 8 fh (28) drawnum
6 8 fh (29) drawnum


showpage
+5
source

All Articles