PS:TinyPS

TinyPS is a Javascript interpreter of a subset of PostScript. It reads the page description programs and renders it to the Canvas (which can derive a PNG), to SVG and to PDF, all hand-coded. There is also a raw output to which implements a Bezier and a fill algorithm directly to ImageData.

TinyPS is an function in Sofawiki (PHP) creating mainly the HTML, and a scripts in 2 parts: main.js called on render and worker.js working asynchronously.

The function has several options:

  • -w width (default 595 for A4)
  • -h height (default 842 for A4)
  • -transparent (default 0) creates transparent PNG and SVG
  • -sandbox shows the editor
  • -wait waits for the user to start rendering
  • -canvas shows the Canvas
  • -svg shows the SVG inline
  • -raw shows the Canvas of raw rendering
  • -pnglink adds a link for the PNG file
  • -pngslink adds a link for the PNG files for multiple pages as ZIP
  • -svglink adds a link for the SVG file
  • -pdflink adds a link for the PDF file (PDF 1.1)
  • -stack shows the stack
  • -state shows the graphic state
  • -dict shows the dictionary

The following operators are implemented

  • x abs result
  • x y add result
  • x y and result
  • n array arr
  • cx cy r a1 a2 arc
  • x1 y1 x2 y2 r arcto
  • cx cy s ashow adds space between each character
  • num dem arctan result (360 degree angle)
  • cx cy c ax ay s awidthshow adds space between characters and words
  • dict begin pushes the dictionary on the dictionary stack making it the current one
  • s charpath adds the path of the string to the current path
  • clear clears the stack
  • clip
  • closepath
  • count n length of stack
  • var1 var2 copy copies array1 or string1 into array2 or string2
  • angle cos result
  • currentlinewidth result
  • currentmatrix result
  • currentoint x y
  • x2 y2 x3 y3 x4 y4 curveto
  • any n cvs string
  • s/name cvx procedure
  • key value def
  • n dict d
  • x y div result
  • any dup any any
  • end removes current dictionary from dictionary stack
  • x y exch y x
  • name/procedure exec
  • exit
  • false
  • filll
  • name findfont font
  • init incr lim proc for the value is pushed for each run
  • s/array proc foreach
  • s/array n get c/v
  • s/array n c getinterval s/array
  • grestore
  • gsave
  • x y gt result
  • array identmatrix result
  • bool proc if
  • bool proc1 proc2 ifelse
  • x y itransform xuser yuser
  • proc s kshow
  • s/array length result
  • x y lineto
  • proc loop (don't forget exit)
  • x y le result
  • x ln result
  • x y lt result
  • matrix identmatrix
  • x y moveto
  • x y max result
  • x y mod result
  • x y mul result
  • x y ne result
  • x neg result
  • newpath
  • x y or result
  • pop
  • s/array n c2 put (must be a named string or array)
  • s/array n s/array putinterval (must be a named string or array)
  • rand result (range 2 exp 31, divide by 2147483648 to get range 0-1)
  • x2 y2 x3 y3 x4 y4 rcurveto
  • n proc repeat
  • x rhlineto
  • x y rlineto
  • x y rmoveto
  • n roll roll (n length, roll movement pos/neg)
  • angle rotate
  • x round result
  • qx qy x4 y4 rqcurveto
  • x4 y4 rsqcurveto
  • y rvlineto
  • font n scalefont font
  • font setfont
  • array setmatrix
  • x y scale
  • seek s search
  • y setgray
  • x setlinewidth (range 0-1)
  • r g b setrgbcolor (range 0-1)
  • s show
  • showpage
  • x sin result
  • x sqrt result
  • stack (non standard)
  • s stringwidth result
  • n string s
  • stroke
  • stop (non standard)
  • x y sub result
  • s timestamp (non standard)
  • xuser yuser transform x y
  • x y translate
  • true
  • cx cy c s widthshow

PostScript® LANGUAGE REFERENCE third edition
https://www.adobe.com/jp/print/postscript/pdfs/PLRM.pdf

A good documentation on the operators can be found here
https://hepunx.rl.ac.uk/~adye/psdocs/ref/REF.html

My Journey to PostScript