HomeUniDoc
...

Package draw

Overview ▾

Package draw has handy features for defining paths which can be used to draw content on a PDF page. Handles defining paths as points, vector calculations and conversion to PDF content stream data which can be used in page content streams and XObject forms and thus also in annotation appearance streams.

Also defines utility functions for drawing common shapes such as rectangles, lines and circles (ovals).

Index ▾

func DrawBezierPathWithCreator(bpath CubicBezierPath, creator *pdfcontent.ContentCreator)
func DrawPathWithCreator(path Path, creator *pdfcontent.ContentCreator)
type BasicLine
    func (line BasicLine) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error)
type BoundingBox
    func (b BoundingBox) ToPdfRectangle() *model.PdfRectangle
type Circle
    func (c Circle) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error)
type CubicBezierCurve
    func NewCubicBezierCurve(x0, y0, x1, y1, x2, y2, x3, y3 float64) CubicBezierCurve
    func (curve CubicBezierCurve) AddOffsetXY(offX, offY float64) CubicBezierCurve
    func (curve CubicBezierCurve) GetBounds() model.PdfRectangle
type CubicBezierPath
    func NewCubicBezierPath() CubicBezierPath
    func (p CubicBezierPath) AppendCurve(curve CubicBezierCurve) CubicBezierPath
    func (p CubicBezierPath) Copy() CubicBezierPath
    func (p CubicBezierPath) GetBoundingBox() Rectangle
    func (p CubicBezierPath) Offset(offX, offY float64) CubicBezierPath
type CurvePolygon
    func (p CurvePolygon) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error)
type Line
    func (line Line) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error)
type LineEndingStyle
type LineStyle
type Path
    func NewPath() Path
    func (p Path) AppendPoint(point Point) Path
    func (p Path) Copy() Path
    func (p Path) GetBoundingBox() BoundingBox
    func (p Path) GetPointNumber(number int) Point
    func (p Path) Length() int
    func (p Path) Offset(offX, offY float64) Path
    func (p Path) RemovePoint(number int) Path
type Point
    func NewPoint(x, y float64) Point
    func (p Point) Add(dx, dy float64) Point
    func (p Point) AddVector(v Vector) Point
    func (p Point) Rotate(theta float64) Point
    func (p Point) String() string
type PolyBezierCurve
    func (p PolyBezierCurve) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error)
type Polygon
    func (p Polygon) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error)
type Polyline
    func (p Polyline) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error)
type Rectangle
    func (rect Rectangle) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error)
    func (rect Rectangle) ToPdfRectangle() *pdf.PdfRectangle
type Vector
    func NewVector(dx, dy float64) Vector
    func NewVectorBetween(a Point, b Point) Vector
    func NewVectorPolar(length float64, theta float64) Vector
    func (v Vector) Add(other Vector) Vector
    func (v Vector) Flip() Vector
    func (v Vector) FlipX() Vector
    func (v Vector) FlipY() Vector
    func (v Vector) GetPolarAngle() float64
    func (v Vector) Magnitude() float64
    func (v Vector) Rotate(phi float64) Vector
    func (v Vector) Scale(factor float64) Vector

Package files

bezier_curve.go doc.go path.go point.go shapes.go utils.go vector.go

func DrawBezierPathWithCreator

func DrawBezierPathWithCreator(bpath CubicBezierPath, creator *pdfcontent.ContentCreator)

DrawBezierPathWithCreator makes the bezier path with the content creator. Adds the PDF commands to draw the path to the creator instance.

func DrawPathWithCreator

func DrawPathWithCreator(path Path, creator *pdfcontent.ContentCreator)

DrawPathWithCreator makes the path with the content creator. Adds the PDF commands to draw the path to the creator instance.

type BasicLine

BasicLine defines a line between point 1 (X1,Y1) and point 2 (X2,Y2). The line has a specified width, color and opacity.

type BasicLine struct {
    X1        float64
    Y1        float64
    X2        float64
    Y2        float64
    LineColor pdf.PdfColor
    Opacity   float64 // Alpha value (0-1).
    LineWidth float64
    LineStyle LineStyle
}

func (BasicLine) Draw

func (line BasicLine) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error)

Draw draws the basic line to PDF. Generates the content stream which can be used in page contents or appearance stream of annotation. Returns the stream content, XForm bounding box (local), bounding box and an error if one occurred.

type BoundingBox

BoundingBox represents the smallest rectangular area that encapsulates an object.

type BoundingBox struct {
    X      float64
    Y      float64
    Width  float64
    Height float64
}

func (BoundingBox) ToPdfRectangle

func (b BoundingBox) ToPdfRectangle() *model.PdfRectangle

ToPdfRectangle returns the bounding box as a PDF rectangle.

type Circle

Circle represents a circle shape with fill and border properties that can be drawn to a PDF content stream.

type Circle struct {
    X             float64
    Y             float64
    Width         float64
    Height        float64
    FillEnabled   bool // Show fill?
    FillColor     pdf.PdfColor
    BorderEnabled bool // Show border?
    BorderWidth   float64
    BorderColor   pdf.PdfColor
    Opacity       float64 // Alpha value (0-1).
}

func (Circle) Draw

func (c Circle) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error)

Draw draws the circle. Can specify a graphics state (gsName) for setting opacity etc. Otherwise leave empty (""). Returns the content stream as a byte array, the bounding box and an error on failure.

type CubicBezierCurve

CubicBezierCurve is defined by: R(t) = P0*(1-t)^3 + P1*3*t*(1-t)^2 + P2*3*t^2*(1-t) + P3*t^3 where P0 is the current point, P1, P2 control points and P3 the final point.

type CubicBezierCurve struct {
    P0 Point // Starting point.
    P1 Point // Control point 1.
    P2 Point // Control point 2.
    P3 Point // Final point.
}

func NewCubicBezierCurve

func NewCubicBezierCurve(x0, y0, x1, y1, x2, y2, x3, y3 float64) CubicBezierCurve

NewCubicBezierCurve returns a new cubic Bezier curve.

func (CubicBezierCurve) AddOffsetXY

func (curve CubicBezierCurve) AddOffsetXY(offX, offY float64) CubicBezierCurve

AddOffsetXY adds X,Y offset to all points on a curve.

func (CubicBezierCurve) GetBounds

func (curve CubicBezierCurve) GetBounds() model.PdfRectangle

GetBounds returns the bounding box of the Bezier curve.

type CubicBezierPath

CubicBezierPath represents a collection of cubic Bezier curves.

type CubicBezierPath struct {
    Curves []CubicBezierCurve
}

func NewCubicBezierPath

func NewCubicBezierPath() CubicBezierPath

NewCubicBezierPath returns a new empty cubic Bezier path.

func (CubicBezierPath) AppendCurve

func (p CubicBezierPath) AppendCurve(curve CubicBezierCurve) CubicBezierPath

AppendCurve appends the specified Bezier curve to the path.

func (CubicBezierPath) Copy

func (p CubicBezierPath) Copy() CubicBezierPath

Copy returns a clone of the Bezier path.

func (CubicBezierPath) GetBoundingBox

func (p CubicBezierPath) GetBoundingBox() Rectangle

GetBoundingBox returns the bounding box of the Bezier path.

func (CubicBezierPath) Offset

func (p CubicBezierPath) Offset(offX, offY float64) CubicBezierPath

Offset shifts the Bezier path with the specified offsets.

type CurvePolygon

CurvePolygon is a multi-point shape with rings containing curves that can be drawn to a PDF content stream.

type CurvePolygon struct {
    Rings         [][]CubicBezierCurve
    FillEnabled   bool
    FillColor     pdf.PdfColor
    BorderEnabled bool
    BorderColor   pdf.PdfColor
    BorderWidth   float64
}

func (CurvePolygon) Draw

func (p CurvePolygon) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error)

Draw draws the composite curve polygon. A graphics state name can be specified for setting the curve properties (e.g. setting the opacity). Otherwise leave empty (""). Returns the content stream as a byte array and the bounding box of the polygon.

type Line

Line defines a line shape between point 1 (X1,Y1) and point 2 (X2,Y2). The line ending styles can be none (regular line), or arrows at either end. The line also has a specified width, color and opacity.

type Line struct {
    X1               float64
    Y1               float64
    X2               float64
    Y2               float64
    LineColor        pdf.PdfColor
    Opacity          float64 // Alpha value (0-1).
    LineWidth        float64
    LineEndingStyle1 LineEndingStyle // Line ending style of point 1.
    LineEndingStyle2 LineEndingStyle // Line ending style of point 2.
    LineStyle        LineStyle
}

func (Line) Draw

func (line Line) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error)

Draw draws the line to PDF contentstream. Generates the content stream which can be used in page contents or appearance stream of annotation. Returns the stream content, XForm bounding box (local), bounding box and an error if one occurred.

type LineEndingStyle

LineEndingStyle defines the line ending style for lines. The currently supported line ending styles are None, Arrow (ClosedArrow) and Butt.

type LineEndingStyle int

Line ending styles.

const (
    LineEndingStyleNone  LineEndingStyle = 0
    LineEndingStyleArrow LineEndingStyle = 1
    LineEndingStyleButt  LineEndingStyle = 2
)

type LineStyle

LineStyle refers to how the line will be created.

type LineStyle int

Line styles.

const (
    LineStyleSolid  LineStyle = 0
    LineStyleDashed LineStyle = 1
)

type Path

Path consists of straight line connections between each point defined in an array of points.

type Path struct {
    Points []Point
}

func NewPath

func NewPath() Path

NewPath returns a new empty path.

func (Path) AppendPoint

func (p Path) AppendPoint(point Point) Path

AppendPoint adds the specified point to the path.

func (Path) Copy

func (p Path) Copy() Path

Copy returns a clone of the path.

func (Path) GetBoundingBox

func (p Path) GetBoundingBox() BoundingBox

GetBoundingBox returns the bounding box of the path.

func (Path) GetPointNumber

func (p Path) GetPointNumber(number int) Point

GetPointNumber returns the path point at the index specified by number. The index is 1-based.

func (Path) Length

func (p Path) Length() int

Length returns the number of points in the path.

func (Path) Offset

func (p Path) Offset(offX, offY float64) Path

Offset shifts the path with the specified offsets.

func (Path) RemovePoint

func (p Path) RemovePoint(number int) Path

RemovePoint removes the point at the index specified by number from the path. The index is 1-based.

type Point

Point represents a two-dimensional point.

type Point struct {
    X float64
    Y float64
}

func NewPoint

func NewPoint(x, y float64) Point

NewPoint returns a new point with the coordinates x, y.

func (Point) Add

func (p Point) Add(dx, dy float64) Point

Add shifts the coordinates of the point with dx, dy and returns the result.

func (Point) AddVector

func (p Point) AddVector(v Vector) Point

AddVector adds vector to a point.

func (Point) Rotate

func (p Point) Rotate(theta float64) Point

Rotate returns a new Point at `p` rotated by `theta` degrees.

func (Point) String

func (p Point) String() string

type PolyBezierCurve

PolyBezierCurve represents a composite curve that is the result of joining multiple cubic Bezier curves.

type PolyBezierCurve struct {
    Curves      []CubicBezierCurve
    BorderWidth float64
    BorderColor pdf.PdfColor
    FillEnabled bool
    FillColor   pdf.PdfColor
}

func (PolyBezierCurve) Draw

func (p PolyBezierCurve) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error)

Draw draws the composite Bezier curve. A graphics state name can be specified for setting the curve properties (e.g. setting the opacity). Otherwise leave empty (""). Returns the content stream as a byte array and the curve bounding box.

type Polygon

Polygon is a multi-point shape that can be drawn to a PDF content stream.

type Polygon struct {
    Points        [][]Point
    FillEnabled   bool
    FillColor     pdf.PdfColor
    BorderEnabled bool
    BorderColor   pdf.PdfColor
    BorderWidth   float64
}

func (Polygon) Draw

func (p Polygon) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error)

Draw draws the polygon. A graphics state name can be specified for setting the polygon properties (e.g. setting the opacity). Otherwise leave empty (""). Returns the content stream as a byte array and the polygon bounding box.

type Polyline

Polyline defines a slice of points that are connected as straight lines.

type Polyline struct {
    Points    []Point
    LineColor pdf.PdfColor
    LineWidth float64
}

func (Polyline) Draw

func (p Polyline) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error)

Draw draws the polyline. A graphics state name can be specified for setting the polyline properties (e.g. setting the opacity). Otherwise leave empty (""). Returns the content stream as a byte array and the polyline bounding box.

type Rectangle

Rectangle is a shape with a specified Width and Height and a lower left corner at (X,Y) that can be drawn to a PDF content stream. The rectangle can optionally have a border and a filling color. The Width/Height includes the border (if any specified), i.e. is positioned inside.

type Rectangle struct {
    X             float64
    Y             float64
    Width         float64
    Height        float64
    FillEnabled   bool // Show fill?
    FillColor     pdf.PdfColor
    BorderEnabled bool // Show border?
    BorderWidth   float64
    BorderColor   pdf.PdfColor
    Opacity       float64 // Alpha value (0-1).
}

func (Rectangle) Draw

func (rect Rectangle) Draw(gsName string) ([]byte, *pdf.PdfRectangle, error)

Draw draws the rectangle. Can specify a graphics state (gsName) for setting opacity etc. Otherwise leave empty (""). Returns the content stream as a byte array, bounding box and an error on failure.

func (Rectangle) ToPdfRectangle

func (rect Rectangle) ToPdfRectangle() *pdf.PdfRectangle

ToPdfRectangle returns the rectangle as a PDF rectangle.

type Vector

Vector represents a two-dimensional vector.

type Vector struct {
    Dx float64
    Dy float64
}

func NewVector

func NewVector(dx, dy float64) Vector

NewVector returns a new vector with the direction specified by dx and dy.

func NewVectorBetween

func NewVectorBetween(a Point, b Point) Vector

NewVectorBetween returns a new vector with the direction specified by the subtraction of point a from point b (b-a).

func NewVectorPolar

func NewVectorPolar(length float64, theta float64) Vector

NewVectorPolar returns a new vector calculated from the specified magnitude and angle.

func (Vector) Add

func (v Vector) Add(other Vector) Vector

Add adds the specified vector to the current one and returns the result.

func (Vector) Flip

func (v Vector) Flip() Vector

Flip changes the sign of the vector: -vector.

func (Vector) FlipX

func (v Vector) FlipX() Vector

FlipX flips the sign of the Dx component of the vector.

func (Vector) FlipY

func (v Vector) FlipY() Vector

FlipY flips the sign of the Dy component of the vector.

func (Vector) GetPolarAngle

func (v Vector) GetPolarAngle() float64

GetPolarAngle returns the angle the magnitude of the vector forms with the positive X-axis going counterclockwise.

func (Vector) Magnitude

func (v Vector) Magnitude() float64

Magnitude returns the magnitude of the vector.

func (Vector) Rotate

func (v Vector) Rotate(phi float64) Vector

Rotate rotates the vector by the specified angle.

func (Vector) Scale

func (v Vector) Scale(factor float64) Vector

Scale scales the vector by the specified factor.