...
1
2
3
4
5 package vgsvg_test
6
7 import (
8 "log"
9 "os"
10
11 lmit "github.com/go-fonts/latin-modern/lmroman10italic"
12 lreg "github.com/go-fonts/liberation/liberationserifregular"
13 xfnt "golang.org/x/image/font"
14 "golang.org/x/image/font/opentype"
15
16 "gonum.org/v1/plot"
17 "gonum.org/v1/plot/font"
18 "gonum.org/v1/plot/plotter"
19 "gonum.org/v1/plot/vg"
20 "gonum.org/v1/plot/vg/draw"
21 "gonum.org/v1/plot/vg/vgsvg"
22 )
23
24 func Example_embedFonts() {
25
26 cmi10 := font.Font{Typeface: "Latin-Modern", Style: xfnt.StyleItalic}
27 fnt, err := opentype.Parse(lmit.TTF)
28 if err != nil {
29 log.Fatalf("could not parse Latin-Modern fonts: %+v", err)
30 }
31
32 font.DefaultCache.Add([]font.Face{{
33 Font: cmi10,
34 Face: fnt,
35 }})
36 plot.DefaultFont = cmi10
37
38 p := plot.New()
39 p.Title.Text = "Scatter plot"
40 p.X.Label.Text = "x-Axis"
41 p.Y.Label.Text = "y-Axis"
42
43 scatter, err := plotter.NewScatter(plotter.XYs{{X: 1, Y: 1}, {X: 0, Y: 1}, {X: 0, Y: 0}})
44 if err != nil {
45 log.Fatalf("could not create scatter: %v", err)
46 }
47 p.Add(scatter)
48
49 c := vgsvg.NewWith(
50 vgsvg.UseWH(5*vg.Centimeter, 5*vg.Centimeter),
51 vgsvg.EmbedFonts(true),
52 )
53 p.Draw(draw.New(c))
54
55 f, err := os.Create("testdata/embed_fonts.svg")
56 if err != nil {
57 log.Fatalf("could not create output SVG file: %+v", err)
58 }
59 defer f.Close()
60
61 _, err = c.WriteTo(f)
62 if err != nil {
63 log.Fatalf("could not write output SVG plot: %+v", err)
64 }
65
66 err = f.Close()
67 if err != nil {
68 log.Fatalf("could not close output SVG file: %v", err)
69 }
70 }
71
72 func Example_standardFonts() {
73
74 tms := font.Font{Typeface: "Times"}
75 fnt, err := opentype.Parse(lreg.TTF)
76 if err != nil {
77 log.Fatalf("could not parse Times fonts: %+v", err)
78 }
79
80 font.DefaultCache.Add([]font.Face{{
81 Font: tms,
82 Face: fnt,
83 }})
84 plot.DefaultFont = tms
85
86 p := plot.New()
87 p.Title.Text = "Scatter plot"
88 p.X.Label.Text = "x-Axis"
89 p.Y.Label.Text = "y-Axis"
90
91 scatter, err := plotter.NewScatter(plotter.XYs{{X: 1, Y: 1}, {X: 0, Y: 1}, {X: 0, Y: 0}})
92 if err != nil {
93 log.Fatalf("could not create scatter: %v", err)
94 }
95 p.Add(scatter)
96
97 err = p.Save(5*vg.Centimeter, 5*vg.Centimeter, "testdata/standard_fonts.svg")
98 if err != nil {
99 log.Fatalf("could not save SVG plot: %+v", err)
100 }
101 }
102
View as plain text