Browse Source

I dont like SVG, let's try MVG.

Beoran 2 years ago
parent
commit
3b855b54cb
3 changed files with 1165 additions and 0 deletions
  1. 184 0
      mvg/graphic-context.go
  2. 514 0
      mvg/mvg_spec.txt
  3. 467 0
      mvg/parser.go

+ 184 - 0
mvg/graphic-context.go

@@ -0,0 +1,184 @@
+package mvg
+
+import "image/color"
+import "github.com/hajimehoshi/ebiten/v2"
+
+type Color color.RGBA
+
+func (c Color) Eval(gc *GraphicContext) Value {
+	return c
+}
+
+type Compliance int
+
+const ComplianceSVG = Compliance(1)
+const ComplianceMVG = Compliance(2)
+
+type Stroke int
+
+const StrokeNone = 0
+const StrokeSolid = 1
+
+type FillRule = ebiten.FillRule
+
+const FillRuleAll = FillRule(0)
+const FillRuleEvenOdd = FillRule(1)
+
+type GradientUnits int
+
+const (
+	GradientUnitUserSpace         = GradientUnits(iota)
+	GradientUnitUserSpaceOnUse    = GradientUnits(iota)
+	GradientUnitObjectBoundingBox = GradientUnits(iota)
+)
+
+func (LinearGradient) IsDef() {
+}
+
+type Encoding int
+
+// only utf-8 is supported for now.
+const EncodingUtf8 = Encoding(0)
+
+type Box struct {
+	X, Y, W, H float32
+}
+
+type Coord struct {
+	X, Y float32
+}
+
+type GraphicContext struct {
+	Parent    *GraphicContext
+	Children  []*GraphicContext
+	Variables map[string]Value
+	Name      string
+	Compliance
+	Fill        Color
+	FillOpacity float32
+	Stroke
+	StrokeWidth float32
+	FillRule
+	Encoding
+	ViewBox    Box
+	Affine     ebiten.GeoM
+	Defs       map[string]DefValue
+	FontSize   float32
+	FontStyle  string
+	FontWeight string
+	FontFamily string
+}
+
+type Def interface {
+	IsDef()
+}
+
+type DefValue struct {
+	Def
+}
+
+func (d DefValue) Eval(gc *GraphicContext) Value {
+	return d
+}
+
+type Stop []struct {
+	Name  string
+	Index int
+	Color Color
+}
+
+type CommonGradient struct {
+	Name  string
+	Stops []Stop
+	GradientUnits
+	Affine ebiten.GeoM
+}
+
+func (CommonGradient) IsDef() {
+}
+
+type LinearGradient struct {
+	From Coord
+	To   Coord
+	CommonGradient
+}
+
+type RadialGradient struct {
+	CommonGradient
+	Center Coord
+	Focus  Coord
+	Radius float32
+}
+
+/*
+push graphic-context "svg5"
+compliance "SVG"
+fill "black"
+fill-opacity 1
+stroke "none"
+stroke-width 1
+stroke-opacity 1
+fill-rule nonzero
+encoding "UTF-8"
+viewbox 0 0 794 1123
+affine 3.78095 0 0 3.78114 0 0
+push defs
+push gradient "linearGradient5432" linear 0,0 0,0
+stop-color "#ffff00" 0
+stop-color "#ffff00" 1
+pop gradient
+push gradient "linearGradient5344" linear 0,0 0,0
+stop-color "#0000ff" 0
+stop-color "#0000ff" 1
+pop gradient
+push gradient "linearGradient5346" linear 89.1931,92.1651 177.979,92.1651
+gradient-units "userSpaceOnUse"
+pop gradient
+push gradient "radialGradient5434" radial 70.2149,52.3642 70.2149,52.3642 39.4705
+affine 1 0 0 0.954439 0 2.38575
+gradient-units "userSpaceOnUse"
+pop gradient
+pop defs
+push graphic-context "layer1"
+push graphic-context "path49"
+fill "url(#radialGradient5434)"
+stroke-width 0.264583
+fill-opacity "1"
+class "ellipse"
+ellipse 70.2149,52.3642 39.4705,37.6722 0,360
+pop graphic-context
+push graphic-context "text1574"
+class "text"
+translate 59.1728,104.719
+font-size 10.5833
+font-style "normal"
+font-weight "normal"
+font-size 10.5833
+font-family "sans-serif"
+fill "#000000"
+fill-opacity "1"
+stroke "none"
+stroke-width 0.264583
+text 0,0 "     "
+push graphic-context "tspan1576"
+stroke-width 0.264583
+class "tspan"
+text 0,0 "hello"
+pop graphic-context
+pop graphic-context
+push graphic-context "rect4440"
+fill "url(#linearGradient5346)"
+stroke-width 0.264583
+fill-opacity "1"
+class "rect"
+rectangle 89.1931,51.9326 177.979,132.398
+pop graphic-context
+push graphic-context "rect5458"
+fill "#0000ff"
+stroke-width 0.264583
+class "rect"
+rectangle 31.8962,133.123 68.7693,155.595
+pop graphic-context
+pop graphic-context
+pop graphic-context
+*/

+ 514 - 0
mvg/mvg_spec.txt

@@ -0,0 +1,514 @@
+Magick Vector Graphics
+
+MVG Overview • Drawing Primitives
+
+This specification defines the features and syntax for Magick Vector Graphics
+(MVG), a modularized language for describing two-dimensional vector and
+mixed vector/raster graphics in ImageMagick. You can use the language to draw
+from the command line, from an MVG file, from an SVG -- Scalable Vector Graphics
+file or from one of the ImageMagick program interfaces. Use this command,
+for example, to render an arc:
+
+    magick -size 100x60 canvas:skyblue -fill white -stroke black \
+      -draw "path 'M 30,40  A 30,20  20  0,0 70,20 A 30,20  20  1,0 30,40 Z '" \
+      arc.png
+
+and here is the result:
+
+    arc
+
+When the drawing gets sufficiently complex, we recommend you assemble the
+graphic primitives into a MVG file. For our example, we use piechart.mvg:
+
+    push graphic-context
+      viewbox 0 0 624 369
+      affine 0.283636 0 0 0.283846 -0 -0
+      push graphic-context
+        push graphic-context
+          fill 'darkslateblue'
+          stroke 'blue'
+          stroke-width 1
+          rectangle 1,1 2199,1299
+        pop graphic-context
+        push graphic-context
+          font-size 40
+          fill 'white'
+          stroke-width 1
+          text 600,1100 'Average: 20.0'
+        pop graphic-context
+        push graphic-context
+          fill 'red'
+          stroke 'black'
+          stroke-width 5
+          path 'M700.0,600.0 L340.0,600.0 A360.0,360.0 0 0,1 408.1452123287954,389.2376150414973 z'
+        pop graphic-context
+        push graphic-context
+          font-size 40
+          fill 'white'
+          stroke-width 1
+          text 1400,140 'MagickWand for PHP'
+        pop graphic-context
+        push graphic-context
+          font-size 30
+          fill 'white'
+          stroke-width 1
+          text 1800,140 '(10.0%)'
+        pop graphic-context
+        push graphic-context
+          fill 'red'
+          stroke 'black'
+          stroke-width 4
+          rectangle 1330,100 1370,140
+        pop graphic-context
+        push graphic-context
+          fill 'yellow'
+          stroke 'black'
+          stroke-width 5
+          path 'M700.0,600.0 L408.1452123287954,389.2376150414973 A360.0,360.0 0 0,1 976.5894480359858,369.56936567559273 z'
+        pop graphic-context
+        push graphic-context
+          font-size 40
+          fill 'white'
+          stroke-width 1
+          text 1400,220 'MagickCore'
+        pop graphic-context
+        push graphic-context
+          font-size 30
+          fill 'white'
+          stroke-width 1
+          text 1800,220 '(29.0%)'
+        pop graphic-context
+        push graphic-context
+          fill 'yellow'
+          stroke 'black'
+          stroke-width 4
+          rectangle 1330,180 1370,220
+        pop graphic-context
+        push graphic-context
+          fill 'fuchsia'
+          stroke 'black'
+          stroke-width 5
+          path 'M700.0,600.0 L976.5894480359858,369.56936567559273 A360.0,360.0 0 0,1 964.2680466142854,844.4634932636567 z'
+        pop graphic-context
+        push graphic-context
+          font-size 40
+          fill 'white'
+          stroke-width 1
+          text 1400,300 'MagickWand'
+        pop graphic-context
+        push graphic-context
+          font-size 30
+          fill 'white'
+          stroke-width 1
+          text 1800,300 '(22.9%)'
+        pop graphic-context
+        push graphic-context
+          fill 'fuchsia'
+          stroke 'black'
+          stroke-width 4
+          rectangle 1330,260 1370,300
+        pop graphic-context
+        push graphic-context
+          fill 'blue'
+          stroke 'black'
+          stroke-width 5
+          path 'M700.0,600.0 L964.2680466142854,844.4634932636567 A360.0,360.0 0 0,1 757.853099990584,955.3210081341651 z'
+        pop graphic-context
+        push graphic-context
+          font-size 40
+          fill 'white'
+          stroke-width 1
+          text 1400,380 'JMagick'
+        pop graphic-context
+        push graphic-context
+          font-size 30
+          fill 'white'
+          stroke-width 1
+          text 1800,380 '(10.6%)'
+        pop graphic-context
+        push graphic-context
+          fill 'blue'
+          stroke 'black'
+          stroke-width 4
+          rectangle 1330,340 1370,380
+        pop graphic-context
+        push graphic-context
+          fill 'lime'
+          stroke 'black'
+          stroke-width 5
+          path 'M700.0,600.0 L757.853099990584,955.3210081341651 A360.0,360.0 0 0,1 340.0,600.0 z'
+        pop graphic-context
+        push graphic-context
+          font-size 40
+          fill 'white'
+          stroke-width 1
+          text 1400,460 'Magick++'
+        pop graphic-context
+        push graphic-context
+          font-size 30
+          fill 'white'
+          stroke-width 1
+          text 1800,460 '(27.5%)'
+        pop graphic-context
+        push graphic-context
+          fill 'lime'
+          stroke 'black'
+          stroke-width 4
+          rectangle 1330,420 1370,460
+        pop graphic-context
+        push graphic-context
+          font-size 100
+          fill 'white'
+          stroke-width 1
+          text 100,150 'ImageMagick'
+        pop graphic-context
+        push graphic-context
+          fill 'none'
+          stroke 'black'
+          stroke-width 5
+          circle 700,600 700,960
+        pop graphic-context
+      pop graphic-context
+    pop graphic-context
+
+to render a pie chart with this command:
+
+    magick mvg:piechart.mvg piechart.png
+
+which produces this rendering:
+
+    piechart
+
+However, in general, MVG is sufficiently difficult to work with that you
+probably want to use a program to generate your graphics in the SVG format.
+ImageMagick automagically converts SVG to MVG and renders your image,
+for example, we render piechart.svg with this command:
+
+    magick mvg:piechart.svg piechart.jpg
+
+to produce the same pie chart we created with the MVG language.
+
+MVG Overview
+
+MVG ignores all white-space between commands.
+This allows multiple MVG commands per line.
+It is common convention to terminate each MVG command with a newline to make
+MVG easier to edit and read. This syntax description uses indentation in
+MVG sequences to aid with understanding. Indentation is supported but is not
+required.
+
+Metafile wrapper syntax (to support stand-alone MVG files):
+
+    push graphic-context
+      viewbox 0 0 width height
+      [ any other MVG commands ]
+    pop graphic-context
+
+Pattern syntax (saving and restoring context):
+
+    push pattern id x,y width,height
+     push graphic-context
+      [ drawing commands ]
+     pop graphic-context
+    pop pattern
+
+an example is (%s is a identifier string):
+
+    push defs
+     push pattern %s 10,10 20,20
+      push graphic-context
+       fill red
+       rectangle 5,5 15,15
+      pop graphic-context
+      push graphic-context
+       fill green
+       rectangle 10,10 20,20
+      pop graphic-context
+     pop pattern
+    pop defs
+
+For image tiling use:
+
+    push pattern id x,y width,height
+     image Copy ...
+    pop pattern
+
+Note you can use the pattern for either the fill or stroke like:
+    stroke url(#%s)
+or
+    fill url(#%s)
+
+The clip path defines a clipping area, where only the contained area to be
+drawn upon. Areas outside of the clipping areare masked.
+
+    push defs
+     push clip-path "myClipPath"
+      push graphic-context
+       rectangle 10,10 20,20
+      pop graphic-context
+     pop clip-path
+    pop defs
+    clip-path url(#myClipPath)
+
+Drawing Primitives
+
+Here is a complete description of the MVG drawing primitives:
+Primitive 	Description
+
+affine sx,rx,ry,sy,tx,ty
+
+arc x0,y0 x1,y1 a0,a1
+
+bezier x0,y0 ... xn,yn 	Bezier (spline) requires three or more x,y coordinates
+to define its shape. The first and last points are the knots
+(preserved coordinates) and any intermediate coordinates are the control points.
+If two control points are specified, the line between each end knot and its
+sequentially respective control point determines the tangent direction of the
+curve at that end. If one control point is specified, the lines from the end
+knots to the one control point determines the tangent directions of the curve
+at each end. If more than two control points are specified, then the
+additional control points act in combination to determine the intermediate
+shape of the curve. In order to draw complex curves, it is highly recommended
+either to use the Path primitive or to draw multiple four-point bezier segments
+with the start and end knots of each successive segment repeated.
+
+border-color color
+
+circle originx,originy perimeterx,perimetery
+
+clip-path url(name)
+
+clip-rule rule 	Choose from these rule types:
+
+    evenodd
+    nonzero
+
+clip-units units 	Choose from these unit types:
+
+    userSpace
+    userSpaceOnUse
+    objectBoundingBox
+
+color x,y method 	Choose from these method types:
+
+    point
+    replace
+    floodfill
+    filltoborder
+    reset
+
+compliance type 	Choose from these compliance types: MVG or SVG
+
+decorate type 	Choose from these types of decorations:
+
+    none
+    line-through
+    overline
+    underline
+
+ellipse centerx,centery radiusx,radiusy arcstart,arcstop
+
+fill color 	Choose from any of these colors.
+
+fill-opacity opacity 	The opacity ranges from 0.0 (fully transparent) to 1.0
+(fully opaque) or as a percentage (e.g. 50%).
+
+fill-rule rule 	Choose from these rule types:
+
+    evenodd
+    nonzero
+
+font name
+
+font-family family
+
+font-size point-size
+
+font-stretch type 	Choose from these stretch types:
+
+    all
+    normal
+    ultra-condensed
+    extra-condensed
+    condensed
+    semi-condensed
+    semi-expanded
+    expanded
+    extra-expanded
+    ultra-expanded
+
+font-style style 	Choose from these styles:
+
+    all
+    normal
+    italic
+    oblique
+
+font-weight weight 	Choose from these weights:
+
+    all
+    normal
+    bold
+    100
+    200
+    300
+    400
+    500
+    600
+    700
+    800
+    900
+
+gradient-units units 	Choose from these units:
+
+    userSpace
+    userSpaceOnUse
+    objectBoundingBox
+
+gravity type 	Choose from these gravity types:
+
+    NorthWest
+    North
+    NorthEast
+    West
+    Center
+    East
+    SouthWest
+    South
+    SouthEast
+
+image compose x,y width,height 'filename' 	Choose from these compose operations:
+
+Method 	Description
+clear 	Both the color and the alpha of the destination are cleared. Neither the source nor the destination are used as input.
+src 	The source is copied to the destination. The destination is not used as input.
+dst 	The destination is left untouched.
+src-over 	The source is composited over the destination.
+dst-over 	The destination is composited over the source and the result replaces the destination.
+src-in 	The part of the source lying inside of the destination replaces the destination.
+dst-in 	The part of the destination lying inside of the source replaces the destination.
+src-out 	The part of the source lying outside of the destination replaces the destination.
+dst-out 	The part of the destination lying outside of the source replaces the destination.
+src-atop 	The part of the source lying inside of the destination is composited onto the destination.
+dst-atop 	The part of the destination lying inside of the source is composited over the source and replaces the destination.
+multiply 	The source is multiplied by the destination and replaces the destination. The resultant color is always at least as dark as either of the two constituent colors. Multiplying any color with black produces black. Multiplying any color with white leaves the original color unchanged.
+screen 	The source and destination are complemented and then multiplied and then replace the destination. The resultant color is always at least as light as either of the two constituent colors. Screening any color with white produces white. Screening any color with black leaves the original color unchanged.
+overlay 	Multiplies or screens the colors, dependent on the destination color. Source colors overlay the destination whilst preserving its highlights and shadows. The destination color is not replaced, but is mixed with the source color to reflect the lightness or darkness of the destination.
+darken 	Selects the darker of the destination and source colors. The destination is replaced with the source when the source is darker, otherwise it is left unchanged.
+lighten 	Selects the lighter of the destination and source colors. The destination is replaced with the source when the source is lighter, otherwise it is left unchanged.
+linear-light 	Increase contrast slightly with an impact on the foreground's tonal values.
+color-dodge 	Brightens the destination color to reflect the source color. Painting with black produces no change.
+color-burn 	Darkens the destination color to reflect the source color. Painting with white produces no change.
+hard-light 	Multiplies or screens the colors, dependent on the source color value. If the source color is lighter than 0.5, the destination is lightened as if it were screened. If the source color is darker than 0.5, the destination is darkened, as if it were multiplied. The degree of lightening or darkening is proportional to the difference between the source color and 0.5. If it is equal to 0.5 the destination is unchanged. Painting with pure black or white produces black or white.
+soft-light 	Darkens or lightens the colors, dependent on the source color value. If the source color is lighter than 0.5, the destination is lightened. If the source color is darker than 0.5, the destination is darkened, as if it were burned in. The degree of darkening or lightening is proportional to the difference between the source color and 0.5. If it is equal to 0.5, the destination is unchanged. Painting with pure black or white produces a distinctly darker or lighter area, but does not result in pure black or white.
+plus 	The source is added to the destination and replaces the destination. This operator is useful for animating a dissolve between two images.
+add 	As per 'plus' but transparency data is treated as matte values. As such any transparent areas in either image remain transparent.
+minus 	Subtract the colors in the source image from the destination image. When transparency is involved, Opaque areas will be subtracted from any destination opaque areas.
+subtract 	Subtract the colors in the source image from the destination image. When transparency is involved transparent areas are subtracted, so only the opaque areas in the source remain opaque in the destination image.
+difference 	Subtracts the darker of the two constituent colors from the lighter. Painting with white inverts the destination color. Painting with black produces no change.
+exclusion 	Produces an effect similar to that of 'difference', but appears as lower contrast. Painting with white inverts the destination color. Painting with black produces no change.
+xor 	The part of the source that lies outside of the destination is combined with the part of the destination that lies outside of the source.
+copy-* 	Copy the specified channel in the source image to the same channel in the destination image. If the channel specified in the source image does not exist, (which can only happen for methods, 'copy-opacity' or 'copy-black') then it is assumed that the source image is a special grayscale channel image of the values to be copied.
+change-mask 	Replace any destination pixel that is the similar to the source images pixel (as defined by the current -fuzz factor), with transparency.
+
+interline-spacing pixels
+
+interword-spacing pixels
+
+kerning pixels
+
+line x,y x1,y1
+
+matte x,y method 	Choose from these methods:
+
+    point
+    replace
+    floodfill
+    filltoborder
+    reset
+
+offset offset
+
+opacity opacity 	Use percent (e.g. 50%).
+
+path path
+
+point x,y
+
+polygon x,y x1,y1, ..., xn,yn
+
+polyline x,y x1,y1, ..., xn,yn
+
+pop clip-path
+
+pop defs
+
+pop gradient
+
+pop graphic-context
+
+pop pattern
+
+push clip-path "name"
+
+push defs
+
+push gradient id linear x,y x1,y1
+
+push gradient id radial xc,cy xf,yf radius
+
+push graphic-context { "id" } 	the id is optional
+
+push pattern id radial x,y width,height
+
+rectangle x,y x1,y1
+
+rotate angle
+
+roundrectangle x,y x1,y1 width,height
+
+scale x,y
+
+skewX angle
+
+skewX angle
+
+stop-color color offset
+
+stroke color
+
+stroke-antialias 0 • 1
+
+stroke-dasharray none • numeric-list
+
+stroke-dashoffset offset
+
+stroke-linecap type 	Choose from these cap types:
+
+    butt
+    round
+    square
+
+stroke-linejoin type 	Choose from these join types:
+
+    bevel
+    miter
+    round
+
+stroke-miterlimit limit
+
+stroke-opacity opacity 	The opacity ranges from 0.0 (fully transparent) to 1.0 (fully opaque) or as a percentage (e.g. 50%).
+
+stroke-width width
+
+text "text"
+
+text-antialias 0 • 1
+
+text-undercolor color
+
+translate x,y
+
+use "url(#id)"
+
+viewbox x,y x1,y1

+ 467 - 0
mvg/parser.go

@@ -0,0 +1,467 @@
+package mvg
+
+import "fmt"
+import "strconv"
+
+/* LL(1) syntax.
+
+COMMANDS -> COMMAND COMMANDS | .
+COMMAND -> word PARAMS .
+PARAMS -> ws PARAM PARAMS | PARAM.
+PARAM -> LIST | ELEM | string | id | word | url.
+LIST -> ELEM comma LIST | ELEM .
+ELEM -> var | number.
+
+*/
+
+type Parser struct {
+	Name   string
+	Buffer []rune
+	Index  int
+	Line   int
+}
+
+func (p *Parser) Error(msg string, extra ...interface{}) Error {
+	form := "%s:%d:" + msg
+	args := []interface{}{}
+	args = append(args, p.Name, p.Line)
+	args = append(args, extra...)
+	return Error{fmt.Errorf(form, args...)}
+}
+
+func (p *Parser) Peek() rune {
+	if p.Index > len(p.Buffer) {
+		return -1
+	}
+	ru := p.Buffer[p.Index]
+	return ru
+}
+
+func (p *Parser) Next() rune {
+	ru := p.Peek()
+	if ru == -1 {
+		return ru
+	}
+	if ru == '\n' {
+		p.Line++
+	}
+	p.Index++
+	return ru
+}
+
+func (p *Parser) ParseWhile(ok func(r rune, i int) bool, found func(string) Value) Value {
+	var ru rune
+	val := []rune{}
+	start := p.Index
+	ru = p.Next()
+	if !ok(ru, p.Index-start) {
+		return nil
+	}
+	val = append(val, ru)
+	for ; ok(ru, p.Index-start); ru = p.Next() {
+		val = append(val, ru)
+	}
+
+	if ru == -1 {
+		return p.Error("Unexpected EOF")
+	}
+	return found(string(val))
+}
+
+func IsID(r rune, i int) bool {
+	if (i == 0) && (r == '%') {
+		return true
+	}
+	return IsWord(r, i)
+}
+
+func IsWord(r rune, i int) bool {
+	return (r >= 'a' && r <= 'z') ||
+		(r >= 'A' && r <= 'Z') ||
+		r == '_' || r == '-'
+}
+
+func IsNumber(r rune, i int) bool {
+	return (r >= '0' && r <= '9') ||
+		r == '.'
+}
+
+type Value interface {
+	Eval(gc *GraphicContext) Value
+}
+
+type Error struct {
+	error
+}
+
+func (e Error) Eval(gc *GraphicContext) Value {
+	return e
+}
+
+type Word string
+
+func (w Word) Eval(gc *GraphicContext) Value {
+	return w
+}
+
+type ID string
+
+func (i ID) Eval(gc *GraphicContext) Value {
+	return i
+}
+
+func (p *Parser) ParseID() Value {
+	return p.ParseWhile(IsID, func(res string) Value {
+		return ID(res[1:len(res)]) // chop off the %
+	})
+}
+
+type Variable string
+
+func IsVariable(r rune, i int) bool {
+	if (i == 0) && (r == '$') {
+		return true
+	}
+	return IsWord(r, i)
+}
+
+func (v Variable) Eval(gc *GraphicContext) Value {
+	var ok bool
+	var val Value
+	for val, ok = gc.Variables[string(v)]; !ok; val, ok = gc.Variables[string(v)] {
+		if gc.Parent == nil {
+			return Error{fmt.Errorf("Could not evaluate variable: %s", string(v))}
+		}
+		gc = gc.Parent
+	}
+	return val
+}
+
+func (p *Parser) ParseVariable() Value {
+	return p.ParseWhile(IsVariable, func(res string) Value {
+		return Variable(res[1:len(res)]) // chop off the $
+	})
+}
+
+type Number float32
+
+func (n Number) Eval(gc *GraphicContext) Value {
+	return n
+}
+
+func (p *Parser) ParseWord() Value {
+	return p.ParseWhile(IsWord, func(res string) Value {
+		return Word(res)
+	})
+}
+
+func (p *Parser) ParseNumber() Value {
+	return p.ParseWhile(IsNumber, func(res string) Value {
+		f, err := strconv.ParseFloat(res, 32)
+		if err != nil {
+			return p.Error("Cannot convert to number: %s, %s", res, err)
+		}
+		return Number(f)
+	})
+}
+
+type Ref string
+
+func (r Ref) Eval(gc *GraphicContext) Value {
+	var ok bool
+	var val DefValue
+	for val, ok = gc.Defs[string(r)]; !ok; val, ok = gc.Defs[string(r)] {
+		if gc.Parent == nil {
+			return Error{fmt.Errorf("Could not evaluate reference: %s", string(r))}
+		}
+		gc = gc.Parent
+	}
+	return val
+}
+
+type String string
+
+func (p Parser) IsString(r rune, i int) bool {
+	if (i == 0) && (r == '"') {
+		return true
+	} else if r == '"' {
+		return p.Buffer[p.Index+i-1] != '\\'
+	} else {
+		return true
+	}
+}
+
+/*
+
+COMMANDS -> COMMAND COMMANDS | .
+COMMAND -> word PARAMS .
+PARAMS -> ws PARAM PARAMS | PARAM.
+PARAM -> LIST | ELEM | string | id | word | ref.
+LIST -> ELEM comma LIST | ELEM .
+ELEM -> var | number.
+
+
+
+Drawing Primitives
+
+Here is a complete description of the MVG drawing primitives:
+Primitive 	Description
+
+affine sx,rx,ry,sy,tx,ty
+
+arc x0,y0 x1,y1 a0,a1
+
+bezier x0,y0 ... xn,yn 	Bezier (spline) requires three or more x,y coordinates
+to define its shape. The first and last points are the knots
+(preserved coordinates) and any intermediate coordinates are the control points.
+If two control points are specified, the line between each end knot and its
+sequentially respective control point determines the tangent direction of the
+curve at that end. If one control point is specified, the lines from the end
+knots to the one control point determines the tangent directions of the curve
+at each end. If more than two control points are specified, then the
+additional control points act in combination to determine the intermediate
+shape of the curve. In order to draw complex curves, it is highly recommended
+either to use the Path primitive or to draw multiple four-point bezier segments
+with the start and end knots of each successive segment repeated.
+
+border-color color
+
+circle originx,originy perimeterx,perimetery
+
+clip-path url(name)
+
+clip-rule rule 	Choose from these rule types:
+
+    evenodd
+    nonzero
+
+clip-units units 	Choose from these unit types:
+
+    userSpace
+    userSpaceOnUse
+    objectBoundingBox
+
+color x,y method 	Choose from these method types:
+
+    point
+    replace
+    floodfill
+    filltoborder
+    reset
+
+compliance type 	Choose from these compliance types: MVG or SVG
+
+decorate type 	Choose from these types of decorations:
+
+    none
+    line-through
+    overline
+    underline
+
+ellipse centerx,centery radiusx,radiusy arcstart,arcstop
+
+fill color 	Choose from any of these colors.
+
+fill-opacity opacity 	The opacity ranges from 0.0 (fully transparent) to 1.0
+(fully opaque) or as a percentage (e.g. 50%).
+
+fill-rule rule 	Choose from these rule types:
+
+    evenodd
+    nonzero
+
+font name
+
+font-family family
+
+font-size point-size
+
+font-stretch type 	Choose from these stretch types:
+
+    all
+    normal
+    ultra-condensed
+    extra-condensed
+    condensed
+    semi-condensed
+    semi-expanded
+    expanded
+    extra-expanded
+    ultra-expanded
+
+font-style style 	Choose from these styles:
+
+    all
+    normal
+    italic
+    oblique
+
+font-weight weight 	Choose from these weights:
+
+    all
+    normal
+    bold
+    100
+    200
+    300
+    400
+    500
+    600
+    700
+    800
+    900
+
+gradient-units units 	Choose from these units:
+
+    userSpace
+    userSpaceOnUse
+    objectBoundingBox
+
+gravity type 	Choose from these gravity types:
+
+    NorthWest
+    North
+    NorthEast
+    West
+    Center
+    East
+    SouthWest
+    South
+    SouthEast
+
+image compose x,y width,height 'filename' 	Choose from these compose operations:
+
+Method 	Description
+clear 	Both the color and the alpha of the destination are cleared. Neither the source nor the destination are used as input.
+src 	The source is copied to the destination. The destination is not used as input.
+dst 	The destination is left untouched.
+src-over 	The source is composited over the destination.
+dst-over 	The destination is composited over the source and the result replaces the destination.
+src-in 	The part of the source lying inside of the destination replaces the destination.
+dst-in 	The part of the destination lying inside of the source replaces the destination.
+src-out 	The part of the source lying outside of the destination replaces the destination.
+dst-out 	The part of the destination lying outside of the source replaces the destination.
+src-atop 	The part of the source lying inside of the destination is composited onto the destination.
+dst-atop 	The part of the destination lying inside of the source is composited over the source and replaces the destination.
+multiply 	The source is multiplied by the destination and replaces the destination. The resultant color is always at least as dark as either of the two constituent colors. Multiplying any color with black produces black. Multiplying any color with white leaves the original color unchanged.
+screen 	The source and destination are complemented and then multiplied and then replace the destination. The resultant color is always at least as light as either of the two constituent colors. Screening any color with white produces white. Screening any color with black leaves the original color unchanged.
+overlay 	Multiplies or screens the colors, dependent on the destination color. Source colors overlay the destination whilst preserving its highlights and shadows. The destination color is not replaced, but is mixed with the source color to reflect the lightness or darkness of the destination.
+darken 	Selects the darker of the destination and source colors. The destination is replaced with the source when the source is darker, otherwise it is left unchanged.
+lighten 	Selects the lighter of the destination and source colors. The destination is replaced with the source when the source is lighter, otherwise it is left unchanged.
+linear-light 	Increase contrast slightly with an impact on the foreground's tonal values.
+color-dodge 	Brightens the destination color to reflect the source color. Painting with black produces no change.
+color-burn 	Darkens the destination color to reflect the source color. Painting with white produces no change.
+hard-light 	Multiplies or screens the colors, dependent on the source color value. If the source color is lighter than 0.5, the destination is lightened as if it were screened. If the source color is darker than 0.5, the destination is darkened, as if it were multiplied. The degree of lightening or darkening is proportional to the difference between the source color and 0.5. If it is equal to 0.5 the destination is unchanged. Painting with pure black or white produces black or white.
+soft-light 	Darkens or lightens the colors, dependent on the source color value. If the source color is lighter than 0.5, the destination is lightened. If the source color is darker than 0.5, the destination is darkened, as if it were burned in. The degree of darkening or lightening is proportional to the difference between the source color and 0.5. If it is equal to 0.5, the destination is unchanged. Painting with pure black or white produces a distinctly darker or lighter area, but does not result in pure black or white.
+plus 	The source is added to the destination and replaces the destination. This operator is useful for animating a dissolve between two images.
+add 	As per 'plus' but transparency data is treated as matte values. As such any transparent areas in either image remain transparent.
+minus 	Subtract the colors in the source image from the destination image. When transparency is involved, Opaque areas will be subtracted from any destination opaque areas.
+subtract 	Subtract the colors in the source image from the destination image. When transparency is involved transparent areas are subtracted, so only the opaque areas in the source remain opaque in the destination image.
+difference 	Subtracts the darker of the two constituent colors from the lighter. Painting with white inverts the destination color. Painting with black produces no change.
+exclusion 	Produces an effect similar to that of 'difference', but appears as lower contrast. Painting with white inverts the destination color. Painting with black produces no change.
+xor 	The part of the source that lies outside of the destination is combined with the part of the destination that lies outside of the source.
+copy-* 	Copy the specified channel in the source image to the same channel in the destination image. If the channel specified in the source image does not exist, (which can only happen for methods, 'copy-opacity' or 'copy-black') then it is assumed that the source image is a special grayscale channel image of the values to be copied.
+change-mask 	Replace any destination pixel that is the similar to the source images pixel (as defined by the current -fuzz factor), with transparency.
+
+interline-spacing pixels
+
+interword-spacing pixels
+
+kerning pixels
+
+line x,y x1,y1
+
+matte x,y method 	Choose from these methods:
+
+    point
+    replace
+    floodfill
+    filltoborder
+    reset
+
+offset offset
+
+opacity opacity 	Use percent (e.g. 50%).
+
+path path
+
+point x,y
+
+polygon x,y x1,y1, ..., xn,yn
+
+polyline x,y x1,y1, ..., xn,yn
+
+pop clip-path
+
+pop defs
+
+pop gradient
+
+pop graphic-context
+
+pop pattern
+
+push clip-path "name"
+
+push defs
+
+push gradient id linear x,y x1,y1
+
+push gradient id radial xc,cy xf,yf radius
+
+push graphic-context { "id" } 	the id is optional
+
+push pattern id radial x,y width,height
+
+rectangle x,y x1,y1
+
+rotate angle
+
+roundrectangle x,y x1,y1 width,height
+
+scale x,y
+
+skewX angle
+
+skewX angle
+
+stop-color color offset
+
+stroke color
+
+stroke-antialias 0 • 1
+
+stroke-dasharray none • numeric-list
+
+stroke-dashoffset offset
+
+stroke-linecap type 	Choose from these cap types:
+
+    butt
+    round
+    square
+
+stroke-linejoin type 	Choose from these join types:
+
+    bevel
+    miter
+    round
+
+stroke-miterlimit limit
+
+stroke-opacity opacity 	The opacity ranges from 0.0 (fully transparent) to 1.0 (fully opaque) or as a percentage (e.g. 50%).
+
+stroke-width width
+
+text "text"
+
+text-antialias 0 • 1
+
+text-undercolor color
+
+translate x,y
+
+use "url(#id)"
+
+viewbox x,y x1,y1
+*/