From 5d3656e04c031ec71b92130bf3aae60ddb6a2e1d Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Fri, 15 Feb 2019 18:48:32 +1030 Subject: [PATCH] Revert "vg/vgsvg: add class style for compactness" This reverts commit bbc78a95e31f693ae360e101cd1a53ad3775fc87. --- plotter/testdata/polygon_holes_golden.svg | 79 +++++++++---------- vg/testdata/width_-1.svg | 82 ++++++++++---------- vg/testdata/width_0.svg | 82 ++++++++++---------- vg/testdata/width_1.svg | 85 +++++++++++---------- vg/vgsvg/testdata/scatter_golden.svg | 91 +++++++++++----------- vg/vgsvg/testdata/scatter_line_golden.svg | 93 ++++++++++++----------- vg/vgsvg/vgsvg.go | 66 +++++----------- vg/vgsvg/vgsvg_test.go | 14 ++-- 8 files changed, 285 insertions(+), 307 deletions(-) diff --git a/plotter/testdata/polygon_holes_golden.svg b/plotter/testdata/polygon_holes_golden.svg index dda2b700..f3eacad4 100644 --- a/plotter/testdata/polygon_holes_golden.svg +++ b/plotter/testdata/polygon_holes_golden.svg @@ -1,48 +1,49 @@ - -Polygon with holes -X -0 -2 -4 - - - - - - + +Polygon with holes +X +0 +2 +4 + + + + + + -Y +Y -0 -2 -4 - - - - - - - - - - - - -key +0 +2 +4 + + + + + + + + + + + + +key - diff --git a/vg/testdata/width_-1.svg b/vg/testdata/width_-1.svg index 85917e53..a553fa70 100644 --- a/vg/testdata/width_-1.svg +++ b/vg/testdata/width_-1.svg @@ -1,49 +1,51 @@ - -X label -0.0 -0.5 -1.0 - - - - - - - - - - - - + +X label +0.0 +0.5 +1.0 + + + + + + + + + + + + -Y label +Y label -0.0 -0.5 -1.0 - - - - - - - - - - - - +0.0 +0.5 +1.0 + + + + + + + + + + + + - diff --git a/vg/testdata/width_0.svg b/vg/testdata/width_0.svg index 85917e53..a553fa70 100644 --- a/vg/testdata/width_0.svg +++ b/vg/testdata/width_0.svg @@ -1,49 +1,51 @@ - -X label -0.0 -0.5 -1.0 - - - - - - - - - - - - + +X label +0.0 +0.5 +1.0 + + + + + + + + + + + + -Y label +Y label -0.0 -0.5 -1.0 - - - - - - - - - - - - +0.0 +0.5 +1.0 + + + + + + + + + + + + - diff --git a/vg/testdata/width_1.svg b/vg/testdata/width_1.svg index 4d641705..13e3ab70 100644 --- a/vg/testdata/width_1.svg +++ b/vg/testdata/width_1.svg @@ -1,51 +1,52 @@ - -X label -0.0 -0.5 -1.0 - - - - - - - - - - - - + +X label +0.0 +0.5 +1.0 + + + + + + + + + + + + -Y label +Y label -0.0 -0.5 -1.0 - - - - - - - - - - - - - +0.0 +0.5 +1.0 + + + + + + + + + + + + + - diff --git a/vg/vgsvg/testdata/scatter_golden.svg b/vg/vgsvg/testdata/scatter_golden.svg index b5006c52..ce4c1251 100644 --- a/vg/vgsvg/testdata/scatter_golden.svg +++ b/vg/vgsvg/testdata/scatter_golden.svg @@ -1,53 +1,56 @@ - -Scatter plot -X -0.0 -0.5 -1.0 - - - - - - - - - - - - + +Scatter plot +X +0.0 +0.5 +1.0 + + + + + + + + + + + + -Y +Y -0.0 -0.5 -1.0 - - - - - - - - - - - - - - - +0.0 +0.5 +1.0 + + + + + + + + + + + + + + + - diff --git a/vg/vgsvg/testdata/scatter_line_golden.svg b/vg/vgsvg/testdata/scatter_line_golden.svg index 1e74bde6..e9b57efb 100644 --- a/vg/vgsvg/testdata/scatter_line_golden.svg +++ b/vg/vgsvg/testdata/scatter_line_golden.svg @@ -1,54 +1,57 @@ - -Scatter & line plot -X -0.0 -0.5 -1.0 - - - - - - - - - - - - + +Scatter & line plot +X +0.0 +0.5 +1.0 + + + + + + + + + + + + -Y +Y -0.0 -0.5 -1.0 - - - - - - - - - - - - - - - - +0.0 +0.5 +1.0 + + + + + + + + + + + + + + + + - diff --git a/vg/vgsvg/vgsvg.go b/vg/vgsvg/vgsvg.go index 04939c56..3204e611 100644 --- a/vg/vgsvg/vgsvg.go +++ b/vg/vgsvg/vgsvg.go @@ -17,8 +17,6 @@ import ( "image/png" "io" "math" - "sort" - "strconv" svgo "github.com/ajstarks/svgo" @@ -35,15 +33,12 @@ const ( DefaultHeight = 4 * vg.Inch ) -const svgID = "svgID" - type Canvas struct { svg *svgo.SVG w, h vg.Length - buf *bytes.Buffer - stack []context - styles map[string]string + buf *bytes.Buffer + stack []context } type context struct { @@ -78,12 +73,11 @@ func New(w, h vg.Length) *Canvas { func NewWith(opts ...option) *Canvas { buf := new(bytes.Buffer) c := &Canvas{ - svg: svgo.New(buf), - w: DefaultWidth, - h: DefaultHeight, - buf: buf, - stack: make([]context, 1), - styles: map[string]string{}, + svg: svgo.New(buf), + w: DefaultWidth, + h: DefaultHeight, + buf: buf, + stack: []context{{}}, } for _, opt := range opts { @@ -95,13 +89,12 @@ func NewWith(opts ...option) *Canvas { fmt.Fprintf(c.buf, ` `+"\n", pr, c.w, pr, c.h, pr, c.w, pr, c.h, - svgID, ) // Swap the origin to the bottom left. @@ -168,7 +161,7 @@ func (c *Canvas) Stroke(path vg.Path) { return } c.svg.Path(c.pathData(path), - c.style(elm("fill", "#000000", "none"), + style(elm("fill", "#000000", "none"), elm("stroke", "none", colorString(c.context().color)), elm("stroke-opacity", "1", opacityString(c.context().color)), elm("stroke-width", "1", "%.*g", pr, c.context().lineWidth.Points()), @@ -178,7 +171,7 @@ func (c *Canvas) Stroke(path vg.Path) { func (c *Canvas) Fill(path vg.Path) { c.svg.Path(c.pathData(path), - c.style(elm("fill", "#000000", colorString(c.context().color)), + style(elm("fill", "#000000", colorString(c.context().color)), elm("fill-opacity", "1", opacityString(c.context().color)))) } @@ -305,10 +298,13 @@ func (c *Canvas) FillString(font vg.Font, pt vg.Point, str string) { if !ok { panic(fmt.Sprintf("Unknown font: %s", font.Name())) } - sty := c.style(fontStr, + sty := style(fontStr, elm("font-size", "medium", "%.*gpx", pr, font.Size.Points()), elm("fill", "#000000", colorString(c.context().color))) - fmt.Fprintf(c.buf, `%s`+"\n", + if sty != "" { + sty = "\n\t" + sty + } + fmt.Fprintf(c.buf, `%s`+"\n", pr, pt.X.Points(), pr, -pt.Y.Points(), sty, html.EscapeString(str)) } @@ -379,23 +375,6 @@ func (c *Canvas) WriteTo(w io.Writer) (int64, error) { } } - // write styles with names in ascending order - type styleInfo struct{ name, style string } - styles := make([]styleInfo, 0, len(c.styles)) - for style, name := range c.styles { - styles = append(styles, styleInfo{name: name, style: style}) - } - sort.Slice(styles, func(i, j int) bool { return styles[i].name < styles[j].name }) - fmt.Fprintln(b, "") - m, err := fmt.Fprintln(b, "") n += int64(m) if err != nil { @@ -419,7 +398,7 @@ func (c *Canvas) nEnds() int { // all of the given elements. If the elements // are all empty then the empty string is // returned. -func (c *Canvas) style(elms ...string) string { +func style(elms ...string) string { str := "" for _, e := range elms { if e == "" { @@ -433,7 +412,7 @@ func (c *Canvas) style(elms ...string) string { if str == "" { return "" } - return "class=\"" + c.classNameOf(str) + "\"" + return "style=\"" + str + "\"" } // elm returns a style element string with the @@ -482,14 +461,3 @@ func opacityString(clr color.Color) string { _, _, _, a := clr.RGBA() return fmt.Sprintf("%.*g", pr, float64(a)/math.MaxUint16) } - -// classNameOf returns the class name associated to the given style. -// A different class name is generated for each new style. -func (c *Canvas) classNameOf(style string) string { - if name, ok := c.styles[style]; ok { - return name - } - name := "st" + strconv.Itoa(len(c.styles)) - c.styles[style] = name - return name -} diff --git a/vg/vgsvg/vgsvg_test.go b/vg/vgsvg/vgsvg_test.go index bc5e278a..694a5ae0 100644 --- a/vg/vgsvg/vgsvg_test.go +++ b/vg/vgsvg/vgsvg_test.go @@ -102,13 +102,11 @@ func TestHtmlEscape(t *testing.T) { line.Width = 0.5 p.Add(line) - err = p.Save(5*vg.Centimeter, 5*vg.Centimeter, "testdata/scatter_line.svg") - if err != nil { - t.Fatal(err) - } + c := vgsvg.NewWith(vgsvg.UseWH(5*vg.Centimeter, 5*vg.Centimeter)) + p.Draw(draw.New(c)) - got, err := ioutil.ReadFile("testdata/scatter_line.svg") - if err != nil { + b := new(bytes.Buffer) + if _, err = c.WriteTo(b); err != nil { t.Fatal(err) } @@ -117,11 +115,11 @@ func TestHtmlEscape(t *testing.T) { t.Fatal(err) } - ok, err := cmpimg.Equal("svg", got, want) + ok, err := cmpimg.Equal("svg", b.Bytes(), want) if err != nil { t.Fatalf("could not compare images: %v", err) } if !ok { - t.Fatalf("images differ:\ngot:\n%s\nwant:\n%s\n", got, want) + t.Fatalf("images differ:\ngot:\n%s\nwant:\n%s\n", b.Bytes(), want) } }