Skip to content

Commit

Permalink
vg/vgsvg: add switch to embed fonts to SVG plot
Browse files Browse the repository at this point in the history
Fixes #703.
  • Loading branch information
sbinet committed Jul 1, 2021
1 parent 9d1968c commit ce77c31
Show file tree
Hide file tree
Showing 14 changed files with 642 additions and 227 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
gioui.org v0.0.0-20210308172011-57750fc8a0a6
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af
github.com/fogleman/gg v1.3.0
github.com/go-fonts/latin-modern v0.2.0
github.com/go-fonts/liberation v0.1.1
github.com/go-latex/latex v0.0.0-20210118124228-b3d85cf34e07
github.com/go-pdf/fpdf v0.4.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/go-fonts/dejavu v0.1.0 h1:JSajPXURYqpr+Cu8U9bt8K+XcACIHWqWrvWCKyeFmVQ=
github.com/go-fonts/dejavu v0.1.0/go.mod h1:4Wt4I4OU2Nq9asgDCteaAaWZOV24E+0/Pwo0gppep4g=
github.com/go-fonts/latin-modern v0.2.0 h1:5/Tv1Ek/QCr20C6ZOz15vw3g7GELYL98KWr8Hgo+3vk=
github.com/go-fonts/latin-modern v0.2.0/go.mod h1:rQVLdDMK+mK1xscDwsqM5J8U2jrRa3T0ecnM9pNujks=
github.com/go-fonts/liberation v0.1.1 h1:wBrPaMkrXFBW3qXpXAjiKljdVUMxn9bX2ia3XjPHoik=
github.com/go-fonts/liberation v0.1.1/go.mod h1:K6qoJYypsmfVjWg8KOVDQhLc8UDgIK2HYqyqAO9z7GY=
Expand Down
20 changes: 10 additions & 10 deletions plotter/testdata/polygon_holes_golden.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 8 additions & 8 deletions vg/testdata/width_-1_golden.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 8 additions & 8 deletions vg/testdata/width_0_golden.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 8 additions & 8 deletions vg/testdata/width_1_golden.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
101 changes: 101 additions & 0 deletions vg/vgsvg/font_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright ©2021 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package vgsvg_test

import (
"log"
"os"

lmit "github.com/go-fonts/latin-modern/lmroman10italic"
lreg "github.com/go-fonts/liberation/liberationserifregular"
xfnt "golang.org/x/image/font"
"golang.org/x/image/font/opentype"

"gonum.org/v1/plot"
"gonum.org/v1/plot/font"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/draw"
"gonum.org/v1/plot/vg/vgsvg"
)

func Example_embedFonts() {
// Use Latin-Modern fonts.
cmi10 := font.Font{Typeface: "Latin-Modern", Style: xfnt.StyleItalic}
fnt, err := opentype.Parse(lmit.TTF)
if err != nil {
log.Fatalf("could not parse Latin-Modern fonts: %+v", err)
}

font.DefaultCache.Add([]font.Face{{
Font: cmi10,
Face: fnt,
}})
plot.DefaultFont = cmi10

p := plot.New()
p.Title.Text = "Scatter plot"
p.X.Label.Text = "x-Axis"
p.Y.Label.Text = "y-Axis"

scatter, err := plotter.NewScatter(plotter.XYs{{X: 1, Y: 1}, {X: 0, Y: 1}, {X: 0, Y: 0}})
if err != nil {
log.Fatalf("could not create scatter: %v", err)
}
p.Add(scatter)

c := vgsvg.NewWith(
vgsvg.UseWH(5*vg.Centimeter, 5*vg.Centimeter),
vgsvg.EmbedFonts(true),
)
p.Draw(draw.New(c))

f, err := os.Create("testdata/embed_fonts.svg")
if err != nil {
log.Fatalf("could not create output SVG file: %+v", err)
}
defer f.Close()

_, err = c.WriteTo(f)
if err != nil {
log.Fatalf("could not write output SVG plot: %+v", err)
}

err = f.Close()
if err != nil {
log.Fatalf("could not close output SVG file: %v", err)
}
}

func Example_standardFonts() {
// Use standard fonts.
tms := font.Font{Typeface: "Times"}
fnt, err := opentype.Parse(lreg.TTF)
if err != nil {
log.Fatalf("could not parse Times fonts: %+v", err)
}

font.DefaultCache.Add([]font.Face{{
Font: tms,
Face: fnt,
}})
plot.DefaultFont = tms

p := plot.New()
p.Title.Text = "Scatter plot"
p.X.Label.Text = "x-Axis"
p.Y.Label.Text = "y-Axis"

scatter, err := plotter.NewScatter(plotter.XYs{{X: 1, Y: 1}, {X: 0, Y: 1}, {X: 0, Y: 0}})
if err != nil {
log.Fatalf("could not create scatter: %v", err)
}
p.Add(scatter)

err = p.Save(5*vg.Centimeter, 5*vg.Centimeter, "testdata/standard_fonts.svg")
if err != nil {
log.Fatalf("could not save SVG plot: %+v", err)
}
}
Loading

0 comments on commit ce77c31

Please sign in to comment.