Pure-Ruby PostScript (PS) / EPS parser, typed domain model, and serializer. The lower layer of the postsvg converter. Independently reusable for any tool that needs to read, write, or transform PostScript source.
require "postscript"
# Parse PS source into a typed AST
program = Postscript::Source.parse(<<~PS)
%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 0 0 100 100
newpath
10 10 moveto
90 10 lineto
90 90 lineto
10 90 lineto
closepath
fill
showpage
PS
# Walk the AST
program.body.each do |node|
puts node.class
end
# Re-serialize back to PS source
ps_text = Postscript::Serializer.call(program)
puts ps_textThree layers, each with a single responsibility:
| Layer | Owns |
|---|---|
|
PS source → tokens → |
|
Typed PS records: |
|
|
Adding a new PS operator = writing one subclass + one visit_*
method. No switch edits (OCP).
This gem also hosts three general-purpose value types that postsvg depends on:
-
Postscript::Matrix— 2D affine transform. -
Postscript::Color— RGB / Gray / CMYK immutable value. -
Postscript::FormatNumber— float → PS-safe text.