plot: github.com/sksullivan/plot/vg Index | Files | Directories

package vg

import "github.com/sksullivan/plot/vg"

Package vg defines an interface for drawing 2D vector graphics. This package is designed with the hope that many different vector graphics back-ends can conform to the interface.

Index

Package Files

font.go len.go vg.go

Constants

const (
    MoveComp = iota
    LineComp
    ArcComp
    CloseComp
)

Constants that tag the type of each path component.

Variables

var FontDirs = initFontDirs()

FontDirs is a slice of directories searched for font data files. If the first font file found is unreadable or cannot be parsed, then subsequent directories are not tried, and the font will fail to load.

The default slice contains, in the following order, the values of the environment variable VGFONTPATH if it is defined, then the vg source fonts directory if it is found (i.e., if vg was installed by go get). If the resulting FontDirs slice is empty then the current directory is added to it. This slice may be changed to load fonts from different locations.

var (
    // FontMap maps Postscript/PDF font names to compatible
    // free fonts (TrueType converted ghostscript fonts).
    // Fonts that are not keys of this map are not supported.
    FontMap = map[string]string{

        "Courier":             "NimbusMonL-Regu",
        "Courier-Bold":        "NimbusMonL-Bold",
        "Courier-Oblique":     "NimbusMonL-ReguObli",
        "Courier-BoldOblique": "NimbusMonL-BoldObli",

        "Helvetica":             "NimbusSanL-Regu",
        "Helvetica-Bold":        "NimbusSanL-Bold",
        "Helvetica-Oblique":     "NimbusSanL-ReguItal",
        "Helvetica-BoldOblique": "NimbusSanL-BoldItal",

        "Times-Roman":      "NimbusRomNo9L-Regu",
        "Times-Bold":       "NimbusRomNo9L-Medi",
        "Times-Italic":     "NimbusRomNo9L-ReguItal",
        "Times-BoldItalic": "NimbusRomNo9L-MediItal",
    }
)

func AddFont Uses

func AddFont(name string, font *truetype.Font)

AddFont associates a truetype.Font with the given name.

func Initialize Uses

func Initialize(c Canvas)

Initialize sets all of the canvas's values to their initial values.

type Canvas Uses

type Canvas interface {
    // SetLineWidth sets the width of stroked paths.
    // If the width is not positive then stroked lines
    // are not drawn.
    //
    // The initial line width is 1 point.
    SetLineWidth(Length)

    // SetLineDash sets the dash pattern for lines.
    // The pattern slice specifies the lengths of
    // alternating dashes and gaps, and the offset
    // specifies the distance into the dash pattern
    // to start the dash.
    //
    // The initial dash pattern is a solid line.
    SetLineDash(pattern []Length, offset Length)

    // SetColor sets the current drawing color.
    // Note that fill color and stroke color are
    // the same so if you want different fill
    // and stroke colors then you must use two
    // separate calls to SetColor.
    //
    // The initial color is black.  If SetColor is
    // called with a nil color then black is used.
    SetColor(color.Color)

    // Rotate applies a rotation transform to the
    // context.  The parameter is specified in
    // radians.
    Rotate(rad float64)

    // Translate applies a translational transform
    // to the context.
    Translate(x, y Length)

    // Scale applies a scaling transform to the
    // context.
    Scale(x, y float64)

    // Push saves the current line width, the
    // current dash pattern, the current
    // transforms, and the current color
    // onto a stack so that the state can later
    // be restored by calling Pop().
    Push()

    // Pop restores the context saved by the
    // corresponding call to Push().
    Pop()

    // Stroke strokes the given path.
    Stroke(Path)

    // Fill fills the given path.
    Fill(Path)

    // FillString fills in text at the specified
    // location using the given font.
    FillString(f Font, x, y Length, text string)

    // DPI returns the number of canvas dots in
    // an inch.
    DPI() float64
}

A Canvas is the main drawing interface for 2D vector graphics. The origin is in the bottom left corner.

type CanvasSizer Uses

type CanvasSizer interface {
    Canvas
    Size() (x, y Length)
}

CanvasSizer is a Canvas with a defined size.

type Font Uses

type Font struct {
    // Size is the size of the font.  The font size can
    // be used as a reasonable value for the vertical
    // distance between two successive lines of text.
    Size Length
    // contains filtered or unexported fields
}

A Font represents one of the supported font faces.

func MakeFont Uses

func MakeFont(name string, size Length) (font Font, err error)

MakeFont returns a font object. The name of the font must be a key of the FontMap. The font file is located by searching the FontDirs slice for a directory containing the relevant font file. The font file name is name mapped by FontMap with the .ttf extension. For example, the font file for the font name Courier is NimbusMonL-Regu.ttf.

func (*Font) Extents Uses

func (f *Font) Extents() FontExtents

Extents returns the FontExtents for a font.

func (*Font) Font Uses

func (f *Font) Font() *truetype.Font

Font returns the corresponding truetype.Font.

func (*Font) Name Uses

func (f *Font) Name() string

Name returns the name of the font.

func (*Font) SetName Uses

func (f *Font) SetName(name string) error

SetName sets the name of the font, effectively changing the font. If an error is returned then the font is left unchanged.

func (*Font) Width Uses

func (f *Font) Width(s string) Length

Width returns width of a string when drawn using the font.

type FontExtents Uses

type FontExtents struct {
    // Ascent is the distance that the text
    // extends above the baseline.
    Ascent Length

    // Descent is the distance that the text
    // extends below the baseline.  The descent
    // is given as a negative value.
    Descent Length

    // Height is the distance from the lowest
    // descending point to the highest ascending
    // point.
    Height Length
}

FontExtents contains font metric information.

type Length Uses

type Length float64

A Length is a unit-independent representation of length. Internally, the length is stored in postscript points.

const (
    Inch       Length = 72
    Centimeter        = Inch / 2.54
    Millimeter        = Centimeter / 10
)

Common lengths.

func Points Uses

func Points(pt float64) Length

Points returns a length for the given number of points.

func (Length) Dots Uses

func (l Length) Dots(c Canvas) float64

Dots returns the length in dots for the given Canvas.

func (Length) Points Uses

func (l Length) Points() float64

Points returns the length in postscript points.

type Path Uses

type Path []PathComp

func (*Path) Arc Uses

func (p *Path) Arc(x, y, rad Length, s, a float64)

Arc adds an arc to the path defined by the center point of the arc's circle, the radius of the circle and the start and sweep angles.

func (*Path) Close Uses

func (p *Path) Close()

Close closes the path by connecting the current location to the start location with a line.

func (*Path) Line Uses

func (p *Path) Line(x, y Length)

Line draws a line from the current point to the given point.

func (*Path) Move Uses

func (p *Path) Move(x, y Length)

Move moves the current location of the path to the given point.

type PathComp Uses

type PathComp struct {
    // Type is the type of a particluar component.
    // Based on the type, each of the following
    // fields may have a different meaning or may
    // be meaningless.
    Type int

    // The X and Y fields are used as the destination
    // of a MoveComp or LineComp and are the center
    // point of an ArcComp.  They are not used in
    // the CloseComp.
    X, Y Length

    // Radius is only used for ArcComps, it is
    // the radius of the circle defining the arc.
    Radius Length

    // Start and Angle are only used for ArcComps.
    // They define the start angle and sweep angle of
    // the arc around the circle.  The units of the
    // angle are radians.
    Start, Angle float64
}

A PathComp is a component of a path structure.

Directories

PathSynopsis
draw
recorderPackage recorder provides support for vector graphics serialization.
vgepsPackage vgeps implements the vg.Canvas interface using encapsulated postscript.
vgimgPackage vgimg implements the vg.Canvas interface using draw2d (github.com/sksullivan/draw2d) as a backend to output raster images.
vgpdfPackage vgpdf implements the vg.Canvas interface using gopdf (bitbucket.org/zombiezen/gopdf/pdf).
vgsvgPackage vgsvg uses svgo (github.com/ajstarks/svgo) as a backend for vg.
vgx11Package vgx11 implements X-Window vg support.

Package vg imports 9 packages (graph) and is imported by 10 packages. Updated 2016-08-04. Refresh now. Tools for package owners.