func TestFontsPDF() { pdf := vgpdf.New(vg.Inches(4), vg.Inches(4)) DrawFonts(pdf) if err := pdf.Save("fonts.pdf"); err != nil { panic(err) } }
func TestArcsPDF() { pdf := vgpdf.New(vg.Inches(4), vg.Inches(4)) DrawArcs(pdf) if err := pdf.Save("arcs.pdf"); err != nil { panic(err) } }
//Save saves the board (i.e. all subplots, appropriately laid out) to the specified filename. //It basically rips off the implementation of Save in plotinum to support various file formats. func (b *Board) Save(width, height float64, file string) (err error) { w, h := vg.Inches(width), vg.Inches(height) var c interface { vg.Canvas Size() (w, h vg.Length) io.WriterTo } switch ext := strings.ToLower(filepath.Ext(file)); ext { case ".eps": c = vgeps.NewTitle(w, h, file) case ".jpg", ".jpeg": c = vgimg.JpegCanvas{Canvas: vgimg.New(w, h)} case ".pdf": c = vgpdf.New(w, h) case ".png": c = vgimg.PngCanvas{Canvas: vgimg.New(w, h)} case ".svg": c = vgsvg.New(w, h) case ".tiff": c = vgimg.TiffCanvas{Canvas: vgimg.New(w, h)} default: return fmt.Errorf("Unsupported file extension: %s", ext) } for _, subplot := range b.SubPlots { w, h := c.Size() drawArea := plot.DrawArea{ Canvas: c, Rect: subplot.ScaledRect(float64(w), float64(h)), } subplot.Plot.Draw(drawArea) } f, err := os.Create(file) if err != nil { return err } if _, err = c.WriteTo(f); err != nil { return err } say.Println(0, say.Yellow("Saved %s", file)) return f.Close() }
// Save saves the plot to an image file. Width and height // are specified in inches, and the file format is determined // by the extension. Supported extensions are // .eps, .jpg, .jpeg, .pdf, .png, .svg, and .tiff. func (p *Plot) Save(width, height float64, file string) (err error) { w, h := vg.Inches(width), vg.Inches(height) var c interface { vg.Canvas Size() (w, h vg.Length) io.WriterTo } switch ext := strings.ToLower(filepath.Ext(file)); ext { case ".eps": c = vgeps.NewTitle(w, h, file) case ".jpg", ".jpeg": c = vgimg.JpegCanvas{Canvas: vgimg.New(w, h)} case ".pdf": c = vgpdf.New(w, h) case ".png": c = vgimg.PngCanvas{Canvas: vgimg.New(w, h)} case ".svg": c = vgsvg.New(w, h) case ".tiff": c = vgimg.TiffCanvas{Canvas: vgimg.New(w, h)} default: return fmt.Errorf("Unsupported file extension: %s", ext) } p.Draw(MakeDrawArea(c)) f, err := os.Create(file) if err != nil { return err } if _, err = c.WriteTo(f); err != nil { return err } return f.Close() }