// Copyright 2015 The draw2d Authors. All rights reserved. // created: 16/12/2017 by Drahoslav Bednář package draw2dsvg import ( "encoding/xml" ) /* svg elements */ type FontMode int const ( SysFontMode FontMode = 1 << iota LinkFontMode SvgFontMode CssFontMode PathFontMode ) type Svg struct { XMLName xml.Name `xml:"svg"` Xmlns string `xml:"xmlns,attr"` Fonts []*Font `xml:"defs>font"` Groups []*Group `xml:"g"` fontMode FontMode FillStroke } func NewSvg() *Svg { return &Svg{ Xmlns: "http://www.w3.org/2000/svg", FillStroke: FillStroke{Fill: "none", Stroke: "none"}, fontMode: SvgFontMode, } } type Group struct { FillStroke Transform string `xml:"transform,attr,omitempty"` Groups []*Group `xml:"g"` Paths []*Path `xml:"path"` Texts []*Text `xml:"text"` } type Path struct { FillStroke Desc string `xml:"d,attr"` } type Text struct { FillStroke Position FontSize float64 `xml:"font-size,attr,omitempty"` FontFamily string `xml:"font-family,attr,omitempty"` Text string `xml:",innerxml"` Style string `xml:"style,attr,omitempty"` } type Font struct { Identity Face *Face `xml:"font-face"` Glyphs []*Glyph `xml:"glyph"` } type Face struct { Family string `xml:"font-family,attr"` Units int `xml:"units-per-em,attr"` HorizAdvX float64 `xml:"horiz-adv-x,attr"` // TODO add other attrs, like style, variant, weight... } type Glyph struct { Rune Rune `xml:"unicode,attr"` Desc string `xml:"d,attr"` HorizAdvX float64 `xml:"horiz-adv-x,attr"` } type Rune rune func (r Rune) MarshalXMLAttr(name xml.Name) (xml.Attr, error) { return xml.Attr{ Name: name, Value: string(rune(r)), }, nil } /* shared attrs */ type Identity struct { Id string `xml:"id,attr"` Name string `xml:"name,attr"` } type Position struct { X float64 `xml:"x,attr,omitempty"` Y float64 `xml:"y,attr,omitempty"` } type FillStroke struct { Fill string `xml:"fill,attr,omitempty"` FillRule string `xml:"fill-rule,attr,omitempty"` Stroke string `xml:"stroke,attr,omitempty"` StrokeWidth string `xml:"stroke-width,attr,omitempty"` StrokeLinecap string `xml:"stroke-linecap,attr,omitempty"` StrokeLinejoin string `xml:"stroke-linejoin,attr,omitempty"` StrokeDasharray string `xml:"stroke-dasharray,attr,omitempty"` StrokeDashoffset string `xml:"stroke-dashoffset,attr,omitempty"` }