...

Source file src/gonum.org/v1/plot/vg/vg.go

Documentation: gonum.org/v1/plot/vg

     1  // Copyright ©2015 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package vg defines an interface for drawing 2D vector graphics.
     6  // This package is designed with the hope that many different
     7  // vector graphics back-ends can conform to the interface.
     8  package vg // import "gonum.org/v1/plot/vg"
     9  
    10  import (
    11  	"image"
    12  	"image/color"
    13  	"io"
    14  
    15  	"gonum.org/v1/plot/font"
    16  )
    17  
    18  // A Canvas is the main drawing interface for 2D vector
    19  // graphics.
    20  // The origin is in the bottom left corner.
    21  type Canvas interface {
    22  	// SetLineWidth sets the width of stroked paths.
    23  	// If the width is not positive then stroked lines
    24  	// are not drawn.
    25  	//
    26  	// The initial line width is 1 point.
    27  	SetLineWidth(Length)
    28  
    29  	// SetLineDash sets the dash pattern for lines.
    30  	// The pattern slice specifies the lengths of
    31  	// alternating dashes and gaps, and the offset
    32  	// specifies the distance into the dash pattern
    33  	// to start the dash.
    34  	//
    35  	// The initial dash pattern is a solid line.
    36  	SetLineDash(pattern []Length, offset Length)
    37  
    38  	// SetColor sets the current drawing color.
    39  	// Note that fill color and stroke color are
    40  	// the same, so if you want different fill
    41  	// and stroke colors then you must set a color,
    42  	// draw fills, set a new color and then draw lines.
    43  	//
    44  	// The initial color is black.
    45  	// If SetColor is called with a nil color then black is used.
    46  	SetColor(color.Color)
    47  
    48  	// Rotate applies a rotation transform to the context.
    49  	// The parameter is specified in radians.
    50  	Rotate(rad float64)
    51  
    52  	// Translate applies a translational transform
    53  	// to the context.
    54  	Translate(pt Point)
    55  
    56  	// Scale applies a scaling transform to the
    57  	// context.
    58  	Scale(x, y float64)
    59  
    60  	// Push saves the current line width, the
    61  	// current dash pattern, the current
    62  	// transforms, and the current color
    63  	// onto a stack so that the state can later
    64  	// be restored by calling Pop().
    65  	Push()
    66  
    67  	// Pop restores the context saved by the
    68  	// corresponding call to Push().
    69  	Pop()
    70  
    71  	// Stroke strokes the given path.
    72  	Stroke(Path)
    73  
    74  	// Fill fills the given path.
    75  	Fill(Path)
    76  
    77  	// FillString fills in text at the specified
    78  	// location using the given font.
    79  	// If the font size is zero, the text is not drawn.
    80  	FillString(f font.Face, pt Point, text string)
    81  
    82  	// DrawImage draws the image, scaled to fit
    83  	// the destination rectangle.
    84  	DrawImage(rect Rectangle, img image.Image)
    85  }
    86  
    87  // CanvasSizer is a Canvas with a defined size.
    88  type CanvasSizer interface {
    89  	Canvas
    90  	Size() (x, y Length)
    91  }
    92  
    93  // CanvasWriterTo is a CanvasSizer with a WriteTo method.
    94  type CanvasWriterTo interface {
    95  	CanvasSizer
    96  	io.WriterTo
    97  }
    98  
    99  // Initialize sets all of the canvas's values to their
   100  // initial values.
   101  func Initialize(c Canvas) {
   102  	c.SetLineWidth(Points(1))
   103  	c.SetLineDash([]Length{}, 0)
   104  	c.SetColor(color.Black)
   105  }
   106  
   107  type Path []PathComp
   108  
   109  // Move moves the current location of the path to
   110  // the given point.
   111  func (p *Path) Move(pt Point) {
   112  	*p = append(*p, PathComp{Type: MoveComp, Pos: pt})
   113  }
   114  
   115  // Line draws a line from the current point to the
   116  // given point.
   117  func (p *Path) Line(pt Point) {
   118  	*p = append(*p, PathComp{Type: LineComp, Pos: pt})
   119  }
   120  
   121  // Arc adds an arc to the path defined by the center
   122  // point of the arc's circle, the radius of the circle
   123  // and the start and sweep angles.
   124  func (p *Path) Arc(pt Point, rad Length, s, a float64) {
   125  	*p = append(*p, PathComp{
   126  		Type:   ArcComp,
   127  		Pos:    pt,
   128  		Radius: rad,
   129  		Start:  s,
   130  		Angle:  a,
   131  	})
   132  }
   133  
   134  // QuadTo adds a quadratic curve element to the path,
   135  // given by the control point p1 and end point pt.
   136  func (p *Path) QuadTo(p1, pt Point) {
   137  	*p = append(*p, PathComp{Type: CurveComp, Pos: pt, Control: []Point{p1}})
   138  }
   139  
   140  // CubeTo adds a cubic curve element to the path,
   141  // given by the control points p1 and p2 and the end point pt.
   142  func (p *Path) CubeTo(p1, p2, pt Point) {
   143  	*p = append(*p, PathComp{Type: CurveComp, Pos: pt, Control: []Point{p1, p2}})
   144  }
   145  
   146  // Close closes the path by connecting the current
   147  // location to the start location with a line.
   148  func (p *Path) Close() {
   149  	*p = append(*p, PathComp{Type: CloseComp})
   150  }
   151  
   152  // Constants that tag the type of each path
   153  // component.
   154  const (
   155  	MoveComp = iota
   156  	LineComp
   157  	ArcComp
   158  	CurveComp
   159  	CloseComp
   160  )
   161  
   162  // A PathComp is a component of a path structure.
   163  type PathComp struct {
   164  	// Type is the type of a particluar component.
   165  	// Based on the type, each of the following
   166  	// fields may have a different meaning or may
   167  	// be meaningless.
   168  	Type int
   169  
   170  	// The Pos field is used as the destination
   171  	// of a MoveComp or LineComp and is the center
   172  	// point of an ArcComp.  It is not used in
   173  	// the CloseComp.
   174  	Pos Point
   175  
   176  	// Control is one or two intermediate points
   177  	// for a CurveComp used by QuadTo and CubeTo.
   178  	Control []Point
   179  
   180  	// Radius is only used for ArcComps, it is
   181  	// the radius of the circle defining the arc.
   182  	Radius Length
   183  
   184  	// Start and Angle are only used for ArcComps.
   185  	// They define the start angle and sweep angle of
   186  	// the arc around the circle.  The units of the
   187  	// angle are radians.
   188  	Start, Angle float64
   189  }
   190  

View as plain text