Render PostScript to SVG

SVG is the other graphical format for the web. While the HTML Canvas renders to a bitmap, SVG is a vector based format and rendered directly by the browser. SVG graphics can be scaled after creation and they can also be used as independent files, for example to be inserted in a Microsoft Word document.

The structure of SVG is an XML file. Therefore it is a file you can edit in any text editor and manually modify at some extend. You can change the color of objects, make them invisible and modify also the text. The Swiss canton maps on the other pages of this website (https://belle-nuit.com/karten-nationalratswahlen-1971-2023 for instance) are open source SVG files where I only kept the borders and added CSS styles to color the cantons.

SVG can do a lot and interact with Javascript itself, being a DOM object like any other object of HTML. To render PostScript to SVG, we will use only a small subset of the SVG elements. We consider the SVG file as a collection of path which can be filled or stroked. We do not set implicit layers even if SVG has layers. We use the painting model of PostScript, that every object is drawn on the page one after the other. The last object is on top.

So we create a new device rpnSVGdevice. The path node has as a d property which is quite straightforward.

  • M x y (moveto)
  • C x1 y1 x2 y2 x3 y3 (curveto)
  • L x1 y1 (lineto)
  • Z (closepath)

For the SVG file, an XML header must been set.

For some reason I do not understand, the elements are created in the DOM by Javascript but the page is not updated. It took me hours to understand, because the copied node in a new page rendered well. The workaround to refresh the page is

this.node.outerHTML = this.node.outerHTML // force refresh

Javascript Editor

Javascript Editor

And our justified text

Javascript Editor

The codebase has now 2637 lines ps20240915.js

My Journey to PostScript