package vgsvg
import "gonum.org/v1/plot/vg/vgsvg"
Package vgsvg uses svgo (github.com/ajstarks/svgo) as a backend for vg.
By default, gonum/plot uses the Liberation fonts. When embedding was not requested during plot creation, it may happen that the generated SVG plot may not display well if the Liberation fonts are not available to the program displaying the SVG plot. See gonum.org/v1/plot/vg/vgsvg#Example_standardFonts for how to work around this issue.
Alternatively, users may want to install the Liberation fonts on their system:
Example¶
Code:play
package main import ( "log" "gonum.org/v1/plot" "gonum.org/v1/plot/plotter" "gonum.org/v1/plot/vg" ) func main() { p := plot.New() p.Title.Text = "Scatter plot" p.X.Label.Text = "X" p.Y.Label.Text = "Y" 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/scatter.svg") if err != nil { log.Fatalf("could not save SVG plot: %v", err) } }
Example (EmbedFonts)¶
Code:play
package main
import (
"log"
"os"
lmit "github.com/go-fonts/latin-modern/lmroman10italic"
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 main() {
// 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)
}
}
Example (StandardFonts)¶
Code:play
package main
import (
"log"
lreg "github.com/go-fonts/liberation/liberationserifregular"
"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"
)
func main() {
// 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)
}
}
Index ¶
- Constants
- func EmbedFonts(v bool) option
- func UseWH(w, h vg.Length) option
- type Canvas
- func New(w, h vg.Length) *Canvas
- func NewWith(opts ...option) *Canvas
- func (c *Canvas) DrawImage(rect vg.Rectangle, img image.Image)
- func (c *Canvas) Fill(path vg.Path)
- func (c *Canvas) FillString(font font.Face, pt vg.Point, str string)
- func (c *Canvas) Pop()
- func (c *Canvas) Push()
- func (c *Canvas) Rotate(rot float64)
- func (c *Canvas) Scale(x, y float64)
- func (c *Canvas) SetColor(clr color.Color)
- func (c *Canvas) SetLineDash(dashes []vg.Length, offs vg.Length)
- func (c *Canvas) SetLineWidth(w vg.Length)
- func (c *Canvas) Size() (w, h vg.Length)
- func (c *Canvas) Stroke(path vg.Path)
- func (c *Canvas) Translate(pt vg.Point)
- func (c *Canvas) WriteTo(w io.Writer) (int64, error)
Examples ¶
Constants ¶
const ( // DefaultWidth and DefaultHeight are the default canvas // dimensions. DefaultWidth = 4 * vg.Inch DefaultHeight = 4 * vg.Inch )
Functions ¶
func EmbedFonts ¶
func EmbedFonts(v bool) option
EmbedFonts specifies whether fonts should be embedded inside the SVG canvas.
func UseWH ¶
UseWH specifies the width and height of the canvas.
Types ¶
type Canvas ¶
type Canvas struct {
// contains filtered or unexported fields
}
Canvas implements the vg.Canvas interface, drawing to a SVG document.
By default, fonts used by the canvas are not embedded in the produced SVG document. This results in smaller but less portable SVG plots. Users wanting completely portable SVG documents should create SVG canvases with the EmbedFonts function.
func New ¶
New returns a new image canvas.
func NewWith ¶
func NewWith(opts ...option) *Canvas
NewWith returns a new image canvas created according to the specified options. The currently accepted options is UseWH. If size is not specified, the default is used.
func (*Canvas) DrawImage ¶
DrawImage implements the vg.Canvas.DrawImage method.
func (*Canvas) Fill ¶
func (*Canvas) FillString ¶
FillString draws str at position pt using the specified font. Text passed to FillString is escaped with html.EscapeString.
func (*Canvas) Pop ¶
func (c *Canvas) Pop()
func (*Canvas) Push ¶
func (c *Canvas) Push()
func (*Canvas) Rotate ¶
func (*Canvas) Scale ¶
func (*Canvas) SetColor ¶
func (*Canvas) SetLineDash ¶
func (*Canvas) SetLineWidth ¶
func (*Canvas) Size ¶
func (*Canvas) Stroke ¶
func (*Canvas) Translate ¶
func (*Canvas) WriteTo ¶
WriteTo writes the canvas to an io.Writer.
Source Files ¶
vgsvg.go
- Version
- v0.15.0 (latest)
- Published
- Oct 22, 2024
- Platform
- linux/amd64
- Imports
- 17 packages
- Last checked
- 1 week ago –
Tools for package owners.