...

Package context

Overview ▾

type BlendMode

BlendMode represents an ExtGState blend mode applied when compositing.

type BlendMode int
const (
    // BlendModeNormal performs standard source-over compositing.
    BlendModeNormal BlendMode = iota

    // BlendModeMultiply multiplies the source and destination colors.
    BlendModeMultiply
)

type Context

Context defines operations for rendering to a particular target.

type Context interface {

    // Push adds the current context state on the stack.
    Push()

    // Pop removes the most recent context state from the stack.
    Pop()

    // Matrix returns the current transformation matrix.
    Matrix() transform.Matrix

    // SetMatrix modifies the transformation matrix.
    SetMatrix(m transform.Matrix)

    // Translate updates the current matrix with a translation.
    Translate(x, y float64)

    // Scale updates the current matrix with a scaling factor.
    // Scaling occurs about the origin.
    Scale(x, y float64)

    // Rotate updates the current matrix with a anticlockwise rotation.
    // Rotation occurs about the origin. Angle is specified in radians.
    Rotate(angle float64)

    // MoveTo starts a new subpath within the current path starting at
    // the specified point.
    MoveTo(x, y float64)

    // LineTo adds a line segment to the current path starting at the current
    // point.
    LineTo(x, y float64)

    // CubicTo adds a cubic bezier curve to the current path starting at the
    // current point.
    CubicTo(x1, y1, x2, y2, x3, y3 float64)

    // QuadraticTo adds a quadratic bezier curve to the current path starting
    // at the current point.
    QuadraticTo(x1, y1, x2, y2 float64)

    // NewSubPath starts a new subpath within the current path.
    NewSubPath()

    // ClosePath adds a line segment from the current point to the beginning
    // of the current subpath.
    ClosePath()

    // ClearPath clears the current path.
    ClearPath()

    // Clip updates the clipping region by intersecting the current
    // clipping region with the current path as it would be filled by Fill().
    // The path is cleared after this operation.
    Clip()

    // ClipPreserve updates the clipping region by intersecting the current
    // clipping region with the current path as it would be filled by Fill().
    // The path is preserved after this operation.
    ClipPreserve()

    // ResetClip clears the clipping region.
    ResetClip()

    // LineWidth returns the current line width.
    LineWidth() float64

    // SetLineWidth sets the line width.
    SetLineWidth(lineWidth float64)

    // SetLineCap sets the line cap style.
    SetLineCap(lineCap LineCap)

    // SetLineJoin sets the line join style.
    SetLineJoin(lineJoin LineJoin)

    // SetDash sets the line dash pattern.
    SetDash(dashes ...float64)

    // SetDashOffset sets the initial offset into the dash pattern to use when
    // stroking dashed paths.
    SetDashOffset(offset float64)

    // Fill fills the current path with the current color. Open subpaths
    // are implicitly closed.
    Fill()

    // FillPreserve fills the current path with the current color. Open subpaths
    // are implicitly closed. The path is preserved after this operation.
    FillPreserve()

    // Stroke strokes the current path with the current color, line width,
    // line cap, line join and dash settings. The path is cleared after this
    // operation.
    Stroke()

    // StrokePreserve strokes the current path with the current color,
    // line width, line cap, line join and dash settings. The path is preserved
    // after this operation.
    StrokePreserve()

    // SetRGBA sets the both the fill and stroke colors.
    // r, g, b, a values should be in range 0-1.
    SetRGBA(r, g, b, a float64)

    // SetFillRGBA sets the fill color.
    // r, g, b, a values should be in range 0-1.
    SetFillRGBA(r, g, b, a float64)

    // SetFillStyle sets current fill pattern.
    SetFillStyle(pattern Pattern)

    // SetFillRule sets the fill rule.
    SetFillRule(fillRule FillRule)

    // SetStrokeRGBA sets the stroke color.
    // r, g, b, a values should be in range 0-1.
    SetStrokeRGBA(r, g, b, a float64)

    // SetStrokeStyle sets current stroke pattern.
    SetStrokeStyle(pattern Pattern)

    // FillPattern returns the current fill pattern.
    FillPattern() Pattern

    // StrokePattern returns the current stroke pattern.
    StrokePattern() Pattern

    // TextState returns the current text state.
    TextState() *TextState

    // DrawString renders the specified string and the specified position.
    DrawString(s string, face font.Face, x, y float64)

    // MeasureString returns the width and height of the specified string.
    MeasureString(s string, face font.Face) (w, h float64)

    // DrawRectangle draws the specified rectangle.
    DrawRectangle(x, y, w, h float64)

    // DrawImage draws the specified image at the specified point.
    DrawImage(image image.Image, x, y int)

    // DrawImageAnchored draws the specified image at the specified anchor point.
    // The anchor point is x - w * ax, y - h * ay, where w, h is the size of the
    // image. Use ax=0.5, ay=0.5 to center the image at the specified point.
    DrawImageAnchored(image image.Image, x, y int, ax, ay float64)

    // Height returns the width of the rendering area.
    Height() int

    // Width returns the height of the rendering area.
    Width() int

    // BlendMode returns the current ExtGState blend mode.
    BlendMode() BlendMode

    // SetBlendMode sets the current ExtGState blend mode.
    SetBlendMode(mode BlendMode)

    // FillAlpha returns the current ExtGState non-stroking alpha (ca).
    FillAlpha() float64

    // SetFillAlpha sets the current ExtGState non-stroking alpha (ca).
    SetFillAlpha(a float64)

    // StrokeAlpha returns the current ExtGState stroking alpha (CA).
    StrokeAlpha() float64

    // SetStrokeAlpha sets the current ExtGState stroking alpha (CA).
    SetStrokeAlpha(a float64)

    // BeginTransparencyGroup redirects subsequent drawing into a fresh
    // off-screen buffer the same size as the current backing image. The
    // blend mode and alpha that were active when this call was made are
    // remembered and used by EndTransparencyGroup to composite the buffer
    // back. Inside the group, BlendMode is reset to Normal and the
    // fill/stroke alphas are reset to 1.
    //
    // preserveAlpha tells the group to keep the legacy gs Multiply hack's
    // alpha leak in dc.color across the group boundary instead of resetting
    // to opaque. The renderer sets this when a form's content mixes an image
    // with a nested form (e.g. an opaque image plus a black-rect overlay
    // from an inner /Group), where the legacy preservation guard is needed
    // to keep the nested form's fills faint so the image stays visible.
    BeginTransparencyGroup(preserveAlpha bool)

    // EndTransparencyGroup composites the off-screen group buffer into the
    // parent buffer using the blend mode and alpha that were active when
    // BeginTransparencyGroup was called, then restores the parent buffer
    // and state.
    EndTransparencyGroup()
}

type FillRule

FillRule represents the fill style used by a context instance.

type FillRule int

Fill rules.

const (
    FillRuleWinding FillRule = iota
    FillRuleEvenOdd
)

type Gradient

Gradient represents a gradient pattern which can be rendered by a context instance.

type Gradient interface {
    Pattern
    AddColorStop(offset float64, color color.Color)
}

type LineCap

LineCap represents the line cap style used by a context instance.

type LineCap int

Line cap styles.

const (
    LineCapRound LineCap = iota
    LineCapButt
    LineCapSquare
)

type LineJoin

LineJoin represents the line join style used by a context instance.

type LineJoin int

Line join styles.

const (
    LineJoinRound LineJoin = iota
    LineJoinBevel
)

type Pattern

Pattern represents a pattern which can be rendered by a context instance.

type Pattern interface {
    ColorAt(x, y int) color.Color
}

type TextFont

TextFont represents a font used to draw text to a target, through a rendering context.

type TextFont struct {
    Font *model.PdfFont
    Size float64
    // contains filtered or unexported fields
}

func NewTextFont

func NewTextFont(pdfFont *model.PdfFont, size float64) (*TextFont, error)

NewTextFont returns a new text font instance based on the specified PDF font and the specified font size.

func NewTextFontFromPath

func NewTextFontFromPath(filePath string, size float64) (*TextFont, error)

NewTextFontFromPath returns a new text font instance based on the specified font file and the specified font size.

func (*TextFont) BytesToCharcodes

func (tf *TextFont) BytesToCharcodes(data []byte) []textencoding.CharCode

BytesToCharcodes converts the specified byte data to character codes, using the encapsulated PDF font instance.

func (*TextFont) CharcodeToRunes

func (tf *TextFont) CharcodeToRunes(charcode textencoding.CharCode) (textencoding.CharCode, []rune)

CharcodeToRunes translates the specified character code into runes and returns the result. The method also returns the equivalent character code, which may be different from the provided one if the original font has been substituted. If a counterpart character code exists in the new font, the method returns it. Otherwise, the original character code is returned.

func (*TextFont) Clone

func (tf *TextFont) Clone() *TextFont

Clone returns a copy of the text font with a detached face cache. Use it when a TextFont crosses a renderer or goroutine boundary: font faces are not safe for concurrent use, so instances shared process-wide must not share face caches.

func (*TextFont) FaceID

func (tf *TextFont) FaceID(face font.Face) (uint64, bool)

FaceID returns the process-stable serial identifying the specified face for glyph-cache keying, or false if the face is not currently tracked (never produced by this TextFont, or produced earlier but since dropped by cache eviction). The serial is stable while the face is tracked and is never reused, so it is safe to key a cache by even after the face is GC'd.

func (*TextFont) GetCharMetrics

func (tf *TextFont) GetCharMetrics(code textencoding.CharCode) (float64, float64, bool)

GetCharMetrics returns the metrics of the specified character code. The character metrics are calculated by the internal PDF font.

func (*TextFont) NewFace

func (tf *TextFont) NewFace(size float64) font.Face

NewFace returns a font face with the specified size, reusing a previously created face for the same (quantized) size. Faces returned by the same TextFont or its WithSize derivatives must be used from one goroutine only.

func (*TextFont) WithSize

func (tf *TextFont) WithSize(size float64, originalFont *model.PdfFont) *TextFont

WithSize returns a new text font instance based on the current text font, with the specified font size. It shares the receiver's face cache.

type TextRenderingMode

TextRenderingMode determines whether showing text shall cause glyph outlines to be stroked, filled, used as a clipping boundary, or some combination of the three. See section 9.3 "Text State Parameters and Operators" and Table 106 (pp. 254-255 PDF32000_2008).

type TextRenderingMode int
const (
    // TextRenderingModeFill (default) - Fill text.
    TextRenderingModeFill TextRenderingMode = iota

    // TextRenderingModeStroke - Stroke text.
    TextRenderingModeStroke

    // TextRenderingModeFillStroke - Fill, then stroke text.
    TextRenderingModeFillStroke

    // TextRenderingModeInvisible - Neither fill nor stroke text (invisible).
    TextRenderingModeInvisible

    // TextRenderingModeFillClip - Fill text and add to path for clipping.
    TextRenderingModeFillClip

    // TextRenderingModeStrokeClip - Stroke text and add to path for clipping.
    TextRenderingModeStrokeClip

    // TextRenderingModeFillStrokeClip - Fill, then stroke text and add to path for clipping.
    TextRenderingModeFillStrokeClip

    // TextRenderingModeClip - Add text to path for clipping.
    TextRenderingModeClip
)

type TextState

TextState holds a representation of a PDF text state. The text state processes different text related operations which may occur in PDF content streams. It is used as a part of a renderding context in order to manipulate and display text.

type TextState struct {
    Tc  float64           // Character spacing.
    Tw  float64           // Word spacing.
    Th  float64           // Horizontal scaling.
    Tl  float64           // Leading.
    Tf  *TextFont         // Font
    Ts  float64           // Text rise.
    Tm  transform.Matrix  // Text matrix.
    Tlm transform.Matrix  // Text line matrix.
    Tr  TextRenderingMode // Text rendering mode.

    GlobalScale float64 // Global text scale.
}

func NewTextState

func NewTextState() TextState

NewTextState returns a new TextState instance.

func (*TextState) ProcDQ

func (ts *TextState) ProcDQ(data []byte, aw, ac float64, ctx Context)

ProcDQ processes a `"` operation, which advances the text state to a new line and then displays a text string using aw and ac as word and character spacing.

See section 9.4.3 "Text Showing Operators" and Table 209 (pp. 258-259 PDF32000_2008).

func (*TextState) ProcQ

func (ts *TextState) ProcQ(data []byte, ctx Context)

ProcQ processes a `'` operation, which advances the text state to a new line and then displays a text string.

See section 9.4.3 "Text Showing Operators" and Table 209 (pp. 258-259 PDF32000_2008).

func (*TextState) ProcTD

func (ts *TextState) ProcTD(tx, ty float64)

ProcTD processes a `TD` operation, which advances the text state to a new line with offsets `tx`,`ty`.

See section 9.4.2 "Text Positioning Operators" and Table 108 (pp. 257-258 PDF32000_2008).

func (*TextState) ProcTStar

func (ts *TextState) ProcTStar()

ProcTStar processes a `T*` operation, which advances the text state to a new line.

See section 9.4.2 "Text Positioning Operators" and Table 108 (pp. 257-258 PDF32000_2008).

func (*TextState) ProcTd

func (ts *TextState) ProcTd(tx, ty float64)

ProcTd processes a `Td` operation, which advances the text state to a new line with offsets `tx`,`ty`.

See section 9.4.2 "Text Positioning Operators" and Table 108 (pp. 257-258 PDF32000_2008).

func (*TextState) ProcTf

func (ts *TextState) ProcTf(font *TextFont)

ProcTf processes a `Tf` operation which sets the font and its size.

See section 9.3 "Text State Parameters and Operators" and Table 105 (pp. 251-252 PDF32000_2008).

func (*TextState) ProcTj

func (ts *TextState) ProcTj(data []byte, ctx Context)

ProcTj processes a `Tj` operation, which displays a text string.

See section 9.4.3 "Text Showing Operators" and Table 209 (pp. 258-259 PDF32000_2008).

func (*TextState) ProcTm

func (ts *TextState) ProcTm(a, b, c, d, e, f float64)

ProcTm processes a `Tm` operation, which sets the current text matrix.

See section 9.4.2 "Text Positioning Operators" and Table 108 (pp. 257-258 PDF32000_2008).

func (*TextState) Reset

func (ts *TextState) Reset()

Reset resets both the text matrix and the line matrix.

func (*TextState) Translate

func (ts *TextState) Translate(tx, ty float64)

Translate translates the current text matrix with `tx`,`ty`.

Subdirectories

Name Synopsis
..