gfx

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 19 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package gfx is the graphics model a document is drawn through: the Device interface, the devices behind it, color spaces, and the text, image and shading a device is handed.

It knows no file format. A PDF interpreter drives a Device and so does an HTML layout engine, and both reach the same renderer through it. What a device is given is already resolved: a path in user space, a run of glyphs with their transforms, an image that decodes on demand.

DrawDevice renders into a raster.Pixmap, TextDevice extracts, SVGDevice writes SVG, BBoxDevice measures, and ListDevice records what was drawn so that it can be drawn again. Embed BaseDevice to write another.

Index

Constants

View Source
const (
	BlendNormal     = raster.BlendNormal
	BlendMultiply   = raster.BlendMultiply
	BlendScreen     = raster.BlendScreen
	BlendOverlay    = raster.BlendOverlay
	BlendDarken     = raster.BlendDarken
	BlendLighten    = raster.BlendLighten
	BlendColorDodge = raster.BlendColorDodge
	BlendColorBurn  = raster.BlendColorBurn
	BlendHardLight  = raster.BlendHardLight
	BlendSoftLight  = raster.BlendSoftLight
	BlendDifference = raster.BlendDifference
	BlendExclusion  = raster.BlendExclusion
	BlendHue        = raster.BlendHue
	BlendSaturation = raster.BlendSaturation
	BlendColor      = raster.BlendColor
	BlendLuminosity = raster.BlendLuminosity
)

The separable blend modes, then the four non-separable ones.

View Source
const BookCropArea = 0.25

BookCropArea is CropArea for a page of a book.

View Source
const CropArea = 0.5

CropArea is the share of a page a picture covers before the page renders at its resolution, and before a page holding nothing else is taken to be that picture.

View Source
const DefaultDPI = 300

DefaultDPI is the resolution a page renders at when no image covers enough of it.

View Source
const DefaultPictureCacheBytes = 1 << 26

DefaultPictureCacheBytes is how many decoded samples a PictureCache keeps when its Bytes is zero.

View Source
const MinDPI = 72

MinDPI is the lowest resolution a page renders at for the image on it, one pixel a point.

View Source
const NaturalPixels = 1 << 26

NaturalPixels bounds the pixel area of a page rendered at its natural resolution.

Variables

View Source
var (
	DeviceGray    = &ColorSpace{Name: "DeviceGray", Kind: KindGray, N: 1}
	DeviceRGB     = &ColorSpace{Name: "DeviceRGB", Kind: KindRGB, N: 3}
	DeviceCMYK    = &ColorSpace{Name: "DeviceCMYK", Kind: KindCMYK, N: 4}
	DevicePattern = &ColorSpace{Name: "Pattern", Kind: KindPattern, N: 1}
)

The device spaces, which every content stream can use without declaring.

View Source
var DefaultColorParams = ColorParams{Intent: 1, BlackPoint: true}

DefaultColorParams is what a page starts with.

View Source
var ErrUnsupported = errors.New("gfx: unsupported")

ErrUnsupported is a picture in a form this cannot decode.

Functions

func BandRows added in v0.3.0

func BandRows(w, h, rows, limit int) int

BandRows returns the height of a band w pixels wide within limit pixels: rows, at most h, or as many as the limit allows when rows is zero or less, and zero when one row is over the limit.

func CropRect added in v0.3.0

func CropRect(box raster.Rect, dpi float64) image.Rectangle

CropRect is the part of an image rendered at dpi that CropTo keeps.

func CropTo added in v0.2.3

func CropTo(img *image.RGBA, box raster.Rect, dpi float64) *image.RGBA

CropTo cuts out the part of a rendered page box covers.

func DecodeRegistered added in v0.2.4

func DecodeRegistered(b []byte, limit int) (image.Image, bool, error)

DecodeRegistered decodes b with a registered decoder and reports whether one matched. A picture whose header gives more than limit pixels is refused.

func HTMLDocument added in v0.2.0

func HTMLDocument(title, body string) string

HTMLDocument wraps one page's division in the markup that makes it a page a browser will show on its own.

func NamedColor

func NamedColor(name string) (r, g, b uint8, ok bool)

NamedColor is the color a keyword stands for, false for one that names none.

func NaturalDPI added in v0.2.3

func NaturalDPI(st *TextPage, page raster.Rect, minArea float64, limit int) (dpi float64, box raster.Rect, crop bool)

NaturalDPI is the resolution the largest image on the page is drawn at, and at least MinDPI, when it covers minArea of the page, DefaultDPI otherwise, lowered until the page fits in NaturalPixels and in limit pixels, and the box of that image, cropped to when no text is drawn. A negative limit is none.

func RGBABands added in v0.3.0

func RGBABands(origin image.Point, fn func(*image.RGBA) error) func(*raster.Pixmap) error

RGBABands adapts a callback taking RGBA bands to DrawBands, converting each band into one reused image whose Rect is the band's place in device space less origin.

func RegisterPictureDecoder added in v0.2.0

func RegisterPictureDecoder(name, magic string, dec PictureDecoder)

RegisterPictureDecoder installs a decoder for data beginning with magic, where "?" matches any byte.

gfx.RegisterPictureDecoder("jpeg", "\xff\xd8", func(b []byte) (image.Image, error) {
	return jpegn.Decode(bytes.NewReader(b), &jpegn.Options{ToRGBA: true})
})

func ToRGBA

func ToRGBA(px *raster.Pixmap) *image.RGBA

ToRGBA copies a pixmap into the image type the standard library draws with, converting whatever color space it was composited in. This is the boundary the conversion happens at, and it happens once.

Types

type BBoxDevice

type BBoxDevice struct {
	BaseDevice
	// contains filtered or unexported fields
}

BBoxDevice accumulates the bounding box of everything drawn.

func NewBBoxDevice

func NewBBoxDevice() *BBoxDevice

NewBBoxDevice returns a device that measures a page.

func (*BBoxDevice) BeginMask

func (d *BBoxDevice) BeginMask(area raster.Rect, luminosity bool, cs *ColorSpace, backdrop []float32, cp ColorParams)

BeginMask implements Device.

func (*BBoxDevice) BeginTile

func (d *BBoxDevice) BeginTile(area, view raster.Rect, xstep, ystep float32, ctm raster.Matrix) int

BeginTile implements Device.

func (*BBoxDevice) Bounds

func (d *BBoxDevice) Bounds() raster.Rect

Bounds returns what has been drawn so far.

func (*BBoxDevice) ClipImageMask

func (d *BBoxDevice) ClipImageMask(img Image, ctm raster.Matrix, scissor raster.Rect)

ClipImageMask implements Device.

func (*BBoxDevice) ClipPath

func (d *BBoxDevice) ClipPath(p *raster.Path, evenOdd bool, ctm raster.Matrix, scissor raster.Rect)

ClipPath implements Device.

func (*BBoxDevice) ClipStrokePath

func (d *BBoxDevice) ClipStrokePath(p *raster.Path, s *raster.Stroke, ctm raster.Matrix, scissor raster.Rect)

ClipStrokePath implements Device.

func (*BBoxDevice) ClipStrokeText

func (d *BBoxDevice) ClipStrokeText(t *Text, s *raster.Stroke, ctm raster.Matrix, scissor raster.Rect)

ClipStrokeText implements Device.

func (*BBoxDevice) ClipText

func (d *BBoxDevice) ClipText(t *Text, ctm raster.Matrix, scissor raster.Rect)

ClipText implements Device.

func (*BBoxDevice) EndMask

func (d *BBoxDevice) EndMask(*[256]uint8)

EndMask implements Device.

func (*BBoxDevice) EndTile

func (d *BBoxDevice) EndTile()

EndTile implements Device.

func (*BBoxDevice) FillImage

func (d *BBoxDevice) FillImage(img Image, ctm raster.Matrix, alpha float32, cp ColorParams)

FillImage implements Device.

func (*BBoxDevice) FillImageMask

func (d *BBoxDevice) FillImageMask(img Image, ctm raster.Matrix, cs *ColorSpace, c []float32, alpha float32, cp ColorParams)

FillImageMask implements Device.

func (*BBoxDevice) FillPath

func (d *BBoxDevice) FillPath(p *raster.Path, evenOdd bool, ctm raster.Matrix, cs *ColorSpace, c []float32, alpha float32, cp ColorParams)

FillPath implements Device.

func (*BBoxDevice) FillShade

func (d *BBoxDevice) FillShade(sh Shade, ctm raster.Matrix, alpha float32, cp ColorParams)

FillShade implements Device.

func (*BBoxDevice) FillText

func (d *BBoxDevice) FillText(t *Text, ctm raster.Matrix, cs *ColorSpace, c []float32, alpha float32, cp ColorParams)

FillText implements Device.

func (*BBoxDevice) PopClip

func (d *BBoxDevice) PopClip()

PopClip implements Device.

func (*BBoxDevice) StrokePath

func (d *BBoxDevice) StrokePath(p *raster.Path, s *raster.Stroke, ctm raster.Matrix, cs *ColorSpace, c []float32, alpha float32, cp ColorParams)

StrokePath implements Device.

func (*BBoxDevice) StrokeText

func (d *BBoxDevice) StrokeText(t *Text, s *raster.Stroke, ctm raster.Matrix, cs *ColorSpace, c []float32, alpha float32, cp ColorParams)

StrokeText implements Device.

type BaseDevice

type BaseDevice struct {
	// contains filtered or unexported fields
}

BaseDevice implements Device with no-ops.

func (BaseDevice) BeginGroup

func (BaseDevice) BeginLayer

func (BaseDevice) BeginLayer(string)

func (BaseDevice) BeginMask

func (BaseDevice) BeginTile

func (BaseDevice) ClipImageMask

func (BaseDevice) ClipImageMask(Image, raster.Matrix, raster.Rect)

func (BaseDevice) ClipPath

func (BaseDevice) ClipStrokePath

func (BaseDevice) ClipStrokePath(*raster.Path, *raster.Stroke, raster.Matrix, raster.Rect)

func (BaseDevice) ClipStrokeText

func (BaseDevice) ClipStrokeText(*Text, *raster.Stroke, raster.Matrix, raster.Rect)

func (BaseDevice) ClipText

func (BaseDevice) ClipText(*Text, raster.Matrix, raster.Rect)

func (BaseDevice) Close

func (BaseDevice) Close() error

func (BaseDevice) EndGroup

func (BaseDevice) EndGroup()

func (BaseDevice) EndLayer

func (BaseDevice) EndLayer()

func (BaseDevice) EndMask

func (BaseDevice) EndMask(*[256]uint8)

func (BaseDevice) EndTile

func (BaseDevice) EndTile()

func (*BaseDevice) EnterGlyph

func (b *BaseDevice) EnterGlyph(key any) bool

EnterGlyph reports whether a glyph procedure may run and pushes it; LeaveGlyph pops it.

func (BaseDevice) FillImage

func (BaseDevice) FillImageMask

func (BaseDevice) FillPath

func (BaseDevice) FillShade

func (BaseDevice) FillText

func (*BaseDevice) GlyphOps added in v0.2.8

func (b *BaseDevice) GlyphOps() *int64

GlyphOps is the count every glyph procedure run through the device adds to.

func (BaseDevice) IgnoreText

func (BaseDevice) IgnoreText(*Text, raster.Matrix)

func (*BaseDevice) LeaveGlyph

func (b *BaseDevice) LeaveGlyph()

func (BaseDevice) PopClip

func (BaseDevice) PopClip()

func (BaseDevice) SetDefaultColorSpaces

func (BaseDevice) SetDefaultColorSpaces(*DefaultColorSpaces)

func (BaseDevice) StrokePath

func (BaseDevice) StrokeText

type BlendMode

type BlendMode = raster.BlendMode

BlendMode is one of the sixteen blend modes of ISO 32000-1 11.3.5.

type ColorParams

type ColorParams struct {
	Intent          int  // 0 perceptual, 1 relative, 2 saturation, 3 absolute
	BlackPoint      bool // black point compensation
	Overprint       bool
	OverprintMode   int
	OverprintStroke bool
}

ColorParams are the rendering intent and the flags of every painting operation.

type ColorSpace

type ColorSpace struct {
	Name string
	Kind Kind
	N    int

	// Base is the underlying space of an Indexed space and the underlying
	// space of a Pattern space, when it has one.
	Base *ColorSpace
	// Alternate is the space a Separation or DeviceN falls back to.
	Alternate *ColorSpace
	// Lookup is the palette of an Indexed space: HiVal+1 entries of Base.N bytes.
	Lookup []byte
	// HiVal is the largest index of an Indexed space.
	HiVal int
	// Colorants names the inks of a Separation or DeviceN space.
	Colorants []string
	// Range bounds the a and b components of a Lab space.
	Range []float64
	// WhitePoint is the reference white of a CIE based space.
	WhitePoint []float64
	// Gamma and Matrix are the transfer values of a CalRGB or CalGray space.
	Gamma  []float64
	Matrix []float64
	// Tint maps colorant values to the alternate space.
	Tint Tint
	// ICC is the profile an ICCBased space embedded, when it is one this can
	// read and one that says anything sRGB does not.
	ICC *ICC
}

ColorSpace is a color space in the model of ISO 32000-1 8.6. What is here is what a device needs to know about one: how many components a color has, what it is called, and how to reach RGB.

func (*ColorSpace) CMYK

func (cs *ColorSpace) CMYK(c []float32) (cyan, magenta, yellow, black float32)

CMYK converts a color in this space to CMYK.

func (*ColorSpace) Clamp

func (cs *ColorSpace) Clamp(c []float32)

Clamp holds a color to the range its space allows.

func (*ColorSpace) Convert

func (cs *ColorSpace) Convert(c []float32, out []uint8)

Convert writes a color of this space into out, in the space out's length implies: gray for one component, CMYK for four, RGB otherwise. A nil space converts black.

func (*ColorSpace) Gray

func (cs *ColorSpace) Gray(c []float32) float32

Gray converts a color in this space to a single grey level.

func (*ColorSpace) Initial

func (cs *ColorSpace) Initial() []float32

Initial returns the color a space starts at, ISO 32000-1 8.6.8.

func (*ColorSpace) IsPattern

func (cs *ColorSpace) IsPattern() bool

IsPattern reports whether colors in this space name a pattern rather than carry component values.

func (*ColorSpace) LabRange

func (cs *ColorSpace) LabRange() [4]float32

LabRange bounds the a and b components of a Lab space, from its /Range or from the default when it has none.

func (*ColorSpace) Model

func (cs *ColorSpace) Model() raster.Model

Model returns the space as a raster.Model, which is all a pixmap needs to know about it: how many components it has, and how to reach RGB. The space has to be one a pixmap can hold, so one, three or four components whose values run from zero to one.

func (*ColorSpace) RGB

func (cs *ColorSpace) RGB(c []float32) (r, g, b float32)

RGB converts a color in this space to RGB, each component in 0 to 1.

type DefaultColorSpaces

type DefaultColorSpaces struct {
	Gray, RGB, CMYK *ColorSpace
	OutputIntent    *ColorSpace
}

DefaultColorSpaces are the spaces the device spaces stand for and the output intent, ISO 32000-1 8.6.5.6.

func (*DefaultColorSpaces) Clone

Clone returns a copy, and an empty one for a nil receiver.

func (*DefaultColorSpaces) Default

func (d *DefaultColorSpaces) Default(cs *ColorSpace) *ColorSpace

Default returns the space a device color space stands for.

type Device

type Device interface {
	FillPath(path *raster.Path, evenOdd bool, ctm raster.Matrix, cs *ColorSpace, color []float32, alpha float32, cp ColorParams)
	StrokePath(path *raster.Path, stroke *raster.Stroke, ctm raster.Matrix, cs *ColorSpace, color []float32, alpha float32, cp ColorParams)
	ClipPath(path *raster.Path, evenOdd bool, ctm raster.Matrix, scissor raster.Rect)
	ClipStrokePath(path *raster.Path, stroke *raster.Stroke, ctm raster.Matrix, scissor raster.Rect)

	FillText(text *Text, ctm raster.Matrix, cs *ColorSpace, color []float32, alpha float32, cp ColorParams)
	StrokeText(text *Text, stroke *raster.Stroke, ctm raster.Matrix, cs *ColorSpace, color []float32, alpha float32, cp ColorParams)
	ClipText(text *Text, ctm raster.Matrix, scissor raster.Rect)
	ClipStrokeText(text *Text, stroke *raster.Stroke, ctm raster.Matrix, scissor raster.Rect)
	IgnoreText(text *Text, ctm raster.Matrix)

	FillShade(shade Shade, ctm raster.Matrix, alpha float32, cp ColorParams)
	FillImage(img Image, ctm raster.Matrix, alpha float32, cp ColorParams)
	FillImageMask(img Image, ctm raster.Matrix, cs *ColorSpace, color []float32, alpha float32, cp ColorParams)
	ClipImageMask(img Image, ctm raster.Matrix, scissor raster.Rect)

	PopClip()

	// SetDefaultColorSpaces reports the spaces device colors are interpreted in.
	SetDefaultColorSpaces(d *DefaultColorSpaces)

	// BeginMask opens a soft mask group that EndMask turns into a clip until popped.
	BeginMask(area raster.Rect, luminosity bool, cs *ColorSpace, backdrop []float32, cp ColorParams)
	EndMask(transfer *[256]uint8)
	BeginGroup(area raster.Rect, cs *ColorSpace, isolated, knockout bool, blend BlendMode, alpha float32)
	EndGroup()
	BeginTile(area, view raster.Rect, xstep, ystep float32, ctm raster.Matrix) int
	EndTile()
	BeginLayer(name string)
	EndLayer()

	// Close is called when the page is finished.
	Close() error
}

Device receives the drawing operations an interpreted document produces. Embed BaseDevice to pick up no-op implementations of everything.

type DrawDevice

type DrawDevice struct {
	BaseDevice
	// contains filtered or unexported fields
}

DrawDevice renders into a raster.Pixmap, in the pixmap's own coordinates.

func NewDrawDevice

func NewDrawDevice(dst *raster.Pixmap) *DrawDevice

NewDrawDevice returns a device that renders into dst.

func (*DrawDevice) BeginGroup

func (d *DrawDevice) BeginGroup(area raster.Rect, cs *ColorSpace, isolated, knockout bool, blend BlendMode, alpha float32)

BeginGroup implements Device.

func (*DrawDevice) BeginMask

func (d *DrawDevice) BeginMask(area raster.Rect, luminosity bool, cs *ColorSpace, backdrop []float32, cp ColorParams)

BeginMask implements Device.

func (*DrawDevice) BeginTile

func (d *DrawDevice) BeginTile(area, view raster.Rect, xstep, ystep float32, ctm raster.Matrix) int

BeginTile implements Device. Returning zero asks the interpreter to run the pattern's content once for every repetition.

func (*DrawDevice) Clip

func (d *DrawDevice) Clip() raster.Rect

Clip returns the rectangle drawing is confined to.

func (*DrawDevice) ClipImageMask

func (d *DrawDevice) ClipImageMask(img Image, ctm raster.Matrix, scissor raster.Rect)

ClipImageMask implements Device.

func (*DrawDevice) ClipPath

func (d *DrawDevice) ClipPath(p *raster.Path, evenOdd bool, ctm raster.Matrix, scissor raster.Rect)

ClipPath implements Device.

func (*DrawDevice) ClipStrokePath

func (d *DrawDevice) ClipStrokePath(p *raster.Path, s *raster.Stroke, ctm raster.Matrix, scissor raster.Rect)

ClipStrokePath implements Device.

func (*DrawDevice) ClipStrokeText

func (d *DrawDevice) ClipStrokeText(t *Text, s *raster.Stroke, ctm raster.Matrix, scissor raster.Rect)

ClipStrokeText implements Device.

func (*DrawDevice) ClipText

func (d *DrawDevice) ClipText(t *Text, ctm raster.Matrix, scissor raster.Rect)

ClipText implements Device.

func (*DrawDevice) EndGroup

func (d *DrawDevice) EndGroup()

EndGroup implements Device.

func (*DrawDevice) EndMask

func (d *DrawDevice) EndMask(transfer *[256]uint8)

EndMask implements Device. The frame stays on the stack for PopClip.

func (*DrawDevice) EndTile

func (d *DrawDevice) EndTile()

EndTile implements Device.

func (*DrawDevice) FillImage

func (d *DrawDevice) FillImage(img Image, ctm raster.Matrix, alpha float32, cp ColorParams)

FillImage implements Device.

func (*DrawDevice) FillImageMask

func (d *DrawDevice) FillImageMask(img Image, ctm raster.Matrix, cs *ColorSpace, col []float32, alpha float32, cp ColorParams)

FillImageMask implements Device.

func (*DrawDevice) FillPath

func (d *DrawDevice) FillPath(p *raster.Path, evenOdd bool, ctm raster.Matrix, cs *ColorSpace, col []float32, alpha float32, cp ColorParams)

FillPath implements Device.

func (*DrawDevice) FillShade

func (d *DrawDevice) FillShade(sh Shade, ctm raster.Matrix, alpha float32, cp ColorParams)

FillShade implements Device.

func (*DrawDevice) FillText

func (d *DrawDevice) FillText(t *Text, ctm raster.Matrix, cs *ColorSpace, col []float32, alpha float32, cp ColorParams)

FillText implements Device.

func (*DrawDevice) Pixmap

func (d *DrawDevice) Pixmap() *raster.Pixmap

Pixmap returns what has been drawn.

func (*DrawDevice) PopClip

func (d *DrawDevice) PopClip()

PopClip implements Device. It also closes a group.

func (*DrawDevice) SetErrorFunc

func (d *DrawDevice) SetErrorFunc(f func(error))

SetErrorFunc says where to report what went wrong on the way.

func (*DrawDevice) SetFlatness

func (d *DrawDevice) SetFlatness(tol float32)

SetFlatness sets the distance a flattened curve may stray, in device pixels.

func (*DrawDevice) SetGlyphCache

func (d *DrawDevice) SetGlyphCache(c *raster.GlyphCache)

SetGlyphCache gives the device somewhere to keep rendered glyph masks.

func (*DrawDevice) StrokePath

func (d *DrawDevice) StrokePath(p *raster.Path, s *raster.Stroke, ctm raster.Matrix, cs *ColorSpace, col []float32, alpha float32, cp ColorParams)

StrokePath implements Device.

func (*DrawDevice) StrokeText

func (d *DrawDevice) StrokeText(t *Text, s *raster.Stroke, ctm raster.Matrix, cs *ColorSpace, col []float32, alpha float32, cp ColorParams)

StrokeText implements Device.

type Font

type Font interface {
	// FontName is the name the font goes by.
	FontName() string
	// Program is the font program the glyphs come from, and nil for a font
	// that draws its glyphs by running procedures instead.
	Program() *font.Font
	// EmBox is how far the em box reaches above and below the baseline, in
	// text space where one unit is the font size.
	EmBox() (ascent, descent float32)
	// RunGlyph draws one glyph into dev under ctm, which a font with no
	// program has to do for itself. depth is how deep dev already is inside
	// procedures that re-entered it.
	RunGlyph(dev Device, gid int, ctm raster.Matrix, cs *ColorSpace, color []float32, alpha float32, depth int)
}

Font is the glyph source a text span names.

func FontOf

func FontOf(prog *font.Font) Font

FontOf wraps a font program as a Font a device can draw glyphs out of; two wrappers of one program compare equal.

type GlyphRunner

type GlyphRunner interface {
	EnterGlyph(key any) bool
	LeaveGlyph()
}

GlyphRunner is the part of a device a font uses to draw its own glyphs through it.

type ICC

type ICC struct {
	// contains filtered or unexported fields
}

ICC is what an embedded profile says about the numbers of a color space: the curve each channel is encoded with, and the matrix that takes the linear channels to the connection space. ISO 15076-1.

Only the matrix/TRC shape is read. A profile that maps through a table is left to the alternate space the file names.

func ParseICC

func ParseICC(data []byte) *ICC

ParseICC reads a profile, and returns nil for one this cannot use or one whose transform is the one sRGB already is.

func (*ICC) Components

func (p *ICC) Components() int

Components is how many channels the profile takes.

func (*ICC) ToRGB

func (p *ICC) ToRGB(c []float32) (r, g, b float32)

ToRGB converts one color, which is the whole of what a device asks of a profile: the curves make the channels linear, the matrix takes them to linear sRGB and the transfer function encodes them again.

type Image

type Image interface {
	// Size is the image's own size in pixels.
	Size() (w, h int)
	// ColorSpace is the space the samples are in, and nil for a stencil.
	ColorSpace() *ColorSpace
	// Stencil reports a one bit mask, which paints the fill color through it.
	Stencil() bool
	// Smooth reports that the image asks to be interpolated.
	Smooth() bool
	// Pixels decodes the image into cs, halved shrink times. A nil cs asks
	// for the coverage of a stencil, one byte a pixel.
	Pixels(cs *ColorSpace, shrink int) (*raster.Pixmap, error)
}

Image is a picture a device draws, decoded only when it asks.

func DecodeImage

func DecodeImage(b []byte) (Image, error)

DecodeImage decodes a raster picture into an Image.

type Kind

type Kind int

Kind is the family a color space belongs to.

const (
	KindGray Kind = iota
	KindRGB
	KindCMYK
	KindLab
	KindCalRGB
	KindCalGray
	KindIndexed
	KindSeparation
	KindDeviceN
	KindPattern
)

Color space families, ISO 32000-1 8.6.

type ListDevice

type ListDevice struct {
	// contains filtered or unexported fields
}

ListDevice records what a page draws so that it can be drawn again. One recording replays into as many destinations as a caller wants, which is what lets a page be rasterized in horizontal bands from several goroutines: every operation a raster renderer performs is local to the pixels it touches, so a band is the same page with a smaller destination.

func NewListDevice

func NewListDevice() *ListDevice

NewListDevice returns an empty display list.

func (*ListDevice) BeginGroup

func (d *ListDevice) BeginGroup(area raster.Rect, cs *ColorSpace, isolated, knockout bool, blend BlendMode, alpha float32)

BeginGroup implements Device.

func (*ListDevice) BeginLayer

func (d *ListDevice) BeginLayer(name string)

BeginLayer implements Device.

func (*ListDevice) BeginMask

func (d *ListDevice) BeginMask(area raster.Rect, luminosity bool, cs *ColorSpace, backdrop []float32, cp ColorParams)

BeginMask implements Device.

func (*ListDevice) BeginTile

func (d *ListDevice) BeginTile(area, view raster.Rect, xstep, ystep float32, ctm raster.Matrix) int

BeginTile implements Device. A list records what the interpreter drew, and the interpreter only repeats a tile itself when the device says it will not, so the recording always holds every repetition.

func (*ListDevice) Bounds

func (d *ListDevice) Bounds() raster.Rect

Bounds returns the union of what the list draws, in device space.

func (*ListDevice) ClipImageMask

func (d *ListDevice) ClipImageMask(img Image, ctm raster.Matrix, scissor raster.Rect)

ClipImageMask implements Device.

func (*ListDevice) ClipPath

func (d *ListDevice) ClipPath(path *raster.Path, evenOdd bool, ctm raster.Matrix, scissor raster.Rect)

ClipPath implements Device.

func (*ListDevice) ClipStrokePath

func (d *ListDevice) ClipStrokePath(path *raster.Path, stroke *raster.Stroke, ctm raster.Matrix, scissor raster.Rect)

ClipStrokePath implements Device.

func (*ListDevice) ClipStrokeText

func (d *ListDevice) ClipStrokeText(text *Text, stroke *raster.Stroke, ctm raster.Matrix, scissor raster.Rect)

ClipStrokeText implements Device.

func (*ListDevice) ClipText

func (d *ListDevice) ClipText(text *Text, ctm raster.Matrix, scissor raster.Rect)

ClipText implements Device.

func (*ListDevice) Close

func (d *ListDevice) Close() error

Close implements Device.

func (*ListDevice) DrawBands added in v0.3.0

func (d *ListDevice) DrawBands(r image.Rectangle, model raster.Model, alpha bool, rows, threads int,
	newDevice func(*raster.Pixmap) Device, fn func(*raster.Pixmap) error) error

DrawBands draws the list into the area of device space r, rows at a time, and hands each band to fn in order from the top. A band is a pixmap of model whose X and Y are its place in device space, valid only until fn returns. newDevice makes the device a band is drawn through, threads bands are drawn at once, and an error from fn stops the drawing and is returned.

func (*ListDevice) EndGroup

func (d *ListDevice) EndGroup()

EndGroup implements Device.

func (*ListDevice) EndLayer

func (d *ListDevice) EndLayer()

EndLayer implements Device.

func (*ListDevice) EndMask

func (d *ListDevice) EndMask(transfer *[256]uint8)

EndMask implements Device.

func (*ListDevice) EndTile

func (d *ListDevice) EndTile()

EndTile implements Device.

func (*ListDevice) FillImage

func (d *ListDevice) FillImage(img Image, ctm raster.Matrix, alpha float32, cp ColorParams)

FillImage implements Device.

func (*ListDevice) FillImageMask

func (d *ListDevice) FillImageMask(img Image, ctm raster.Matrix, cs *ColorSpace, color []float32, alpha float32, cp ColorParams)

FillImageMask implements Device.

func (*ListDevice) FillPath

func (d *ListDevice) FillPath(path *raster.Path, evenOdd bool, ctm raster.Matrix, cs *ColorSpace, color []float32, alpha float32, cp ColorParams)

FillPath implements Device.

func (*ListDevice) FillShade

func (d *ListDevice) FillShade(shade Shade, ctm raster.Matrix, alpha float32, cp ColorParams)

FillShade implements Device.

func (*ListDevice) FillText

func (d *ListDevice) FillText(text *Text, ctm raster.Matrix, cs *ColorSpace, color []float32, alpha float32, cp ColorParams)

FillText implements Device.

func (*ListDevice) IgnoreText

func (d *ListDevice) IgnoreText(text *Text, ctm raster.Matrix)

IgnoreText implements Device.

func (*ListDevice) Len

func (d *ListDevice) Len() int

Len returns how many device calls the list holds.

func (*ListDevice) PopClip

func (d *ListDevice) PopClip()

PopClip implements Device.

func (*ListDevice) Replay

func (d *ListDevice) Replay(dev Device)

Replay draws the list into another device. It may be called any number of times, and from several goroutines at once as long as each is given a device of its own: a list is read-only once the page has been run into it.

func (*ListDevice) ReplayClip

func (d *ListDevice) ReplayClip(dev Device, clip raster.Rect)

ReplayClip draws the list into another device, skipping what falls outside clip. What is skipped is only paint: a clip, a group or a layer runs wherever the list is replayed, because the calls after it depend on it.

func (*ListDevice) SetDefaultColorSpaces

func (d *ListDevice) SetDefaultColorSpaces(defs *DefaultColorSpaces)

SetDefaultColorSpaces implements Device.

func (*ListDevice) StrokePath

func (d *ListDevice) StrokePath(path *raster.Path, stroke *raster.Stroke, ctm raster.Matrix, cs *ColorSpace, color []float32, alpha float32, cp ColorParams)

StrokePath implements Device.

func (*ListDevice) StrokeText

func (d *ListDevice) StrokeText(text *Text, stroke *raster.Stroke, ctm raster.Matrix, cs *ColorSpace, color []float32, alpha float32, cp ColorParams)

StrokeText implements Device.

type Natural added in v0.2.8

type Natural struct {
	// contains filtered or unexported fields
}

Natural remembers what NaturalDPI gave one page; the zero value is ready to use.

func (*Natural) Get added in v0.2.8

func (n *Natural) Get(f func() (float64, raster.Rect, bool)) (float64, raster.Rect, bool)

Get returns what f gives, calling it once.

type Picture

type Picture struct {

	// W and H are the picture's size in pixels.
	W, H int
	// contains filtered or unexported fields
}

Picture is a decoded raster image.

func DecodePicture

func DecodePicture(b []byte) (*Picture, error)

DecodePicture decodes a raster picture with a registered or a standard decoder.

func NewPicture

func NewPicture(px *raster.Pixmap) *Picture

NewPicture wraps a premultiplied pixmap as a Picture without copying it.

func (*Picture) ColorSpace

func (p *Picture) ColorSpace() *ColorSpace

ColorSpace implements gfx.Image.

func (*Picture) Pixels

func (p *Picture) Pixels(cs *ColorSpace, shrink int) (*raster.Pixmap, error)

Pixels implements gfx.Image.

func (*Picture) Size

func (p *Picture) Size() (int, int)

Size implements gfx.Image.

func (*Picture) Smooth

func (p *Picture) Smooth() bool

Smooth implements gfx.Image.

func (*Picture) Stencil

func (p *Picture) Stencil() bool

Stencil implements gfx.Image.

type PictureCache added in v0.2.5

type PictureCache struct {
	// Bytes bounds the decoded samples kept. Zero is DefaultPictureCacheBytes,
	// negative is no cache at all.
	Bytes int
	// contains filtered or unexported fields
}

PictureCache holds the decoded pixels of the pictures opened through it, least recently drawn first to go. It is safe for concurrent use.

func (*PictureCache) Open added in v0.2.5

func (c *PictureCache) Open(b []byte) (*Picture, error)

Open reads how big a picture is and keeps b, which must not change afterwards, decoding the pixels only when a device draws the picture.

func (*PictureCache) Purge added in v0.2.5

func (c *PictureCache) Purge()

Purge drops every decoded picture the cache holds.

type PictureDecoder added in v0.2.0

type PictureDecoder func(data []byte) (image.Image, error)

PictureDecoder decodes one raster picture format.

type PixelRows added in v0.3.0

type PixelRows struct {
	W, H  int
	Model raster.Model
	Alpha bool
	// Next returns the next row, valid until the next call, and false at the end.
	Next func() ([]uint8, bool)
	// Stop ends the decode.
	Stop func()
}

PixelRows is an image's pixmap handed out a row at a time.

type Quad

type Quad struct{ UL, UR, LL, LR raster.Point }

Quad is the four corners of what a character occupies.

func (Quad) Bounds

func (q Quad) Bounds() raster.Rect

Bounds returns the smallest rectangle holding the quad.

type RowImage added in v0.3.0

type RowImage interface {
	Image
	// PixelRows starts decoding the rows Pixels would build into cs, halved
	// shrink times, top first; false when the image cannot be decoded so.
	PixelRows(cs *ColorSpace, shrink int) (*PixelRows, bool)
}

RowImage is an Image that can also hand out its pixmap a row at a time.

type SVGDevice

type SVGDevice struct {
	BaseDevice
	// contains filtered or unexported fields
}

SVGDevice writes what a page draws as SVG. Paths, text and images come out as themselves; a shading and a soft mask are rasterized.

func NewSVGDevice

func NewSVGDevice(w io.Writer, box raster.Rect) *SVGDevice

NewSVGDevice returns a device that writes the page bounded by box as SVG into w. Nothing reaches w until Close.

func (*SVGDevice) BeginGroup

func (d *SVGDevice) BeginGroup(area raster.Rect, cs *ColorSpace, isolated, knockout bool, blend BlendMode, alpha float32)

BeginGroup implements Device.

func (*SVGDevice) BeginLayer

func (d *SVGDevice) BeginLayer(name string)

BeginLayer implements Device.

func (*SVGDevice) BeginMask

func (d *SVGDevice) BeginMask(area raster.Rect, luminosity bool, cs *ColorSpace, backdrop []float32, cp ColorParams)

BeginMask implements Device. The mask itself is not written; the group it opens is, so that the clip it becomes still pops.

func (*SVGDevice) BeginTile

func (d *SVGDevice) BeginTile(area, view raster.Rect, xstep, ystep float32, ctm raster.Matrix) int

BeginTile implements Device. Returning zero asks for every repetition.

func (*SVGDevice) ClipImageMask

func (d *SVGDevice) ClipImageMask(img Image, ctm raster.Matrix, scissor raster.Rect)

ClipImageMask implements Device, clipping to what the stencil covers.

func (*SVGDevice) ClipPath

func (d *SVGDevice) ClipPath(p *raster.Path, evenOdd bool, ctm raster.Matrix, scissor raster.Rect)

ClipPath implements Device.

func (*SVGDevice) ClipStrokePath

func (d *SVGDevice) ClipStrokePath(p *raster.Path, s *raster.Stroke, ctm raster.Matrix, scissor raster.Rect)

ClipStrokePath implements Device, clipping to what the stroke bounds.

func (*SVGDevice) ClipStrokeText

func (d *SVGDevice) ClipStrokeText(t *Text, s *raster.Stroke, ctm raster.Matrix, scissor raster.Rect)

ClipStrokeText implements Device.

func (*SVGDevice) ClipText

func (d *SVGDevice) ClipText(t *Text, ctm raster.Matrix, scissor raster.Rect)

ClipText implements Device.

func (*SVGDevice) Close

func (d *SVGDevice) Close() error

Close implements Device. It writes the document.

func (*SVGDevice) EndGroup

func (d *SVGDevice) EndGroup()

EndGroup implements Device.

func (*SVGDevice) EndLayer

func (d *SVGDevice) EndLayer()

EndLayer implements Device.

func (*SVGDevice) EndMask

func (d *SVGDevice) EndMask(transfer *[256]uint8)

EndMask implements Device.

func (*SVGDevice) EndTile

func (d *SVGDevice) EndTile()

EndTile implements Device.

func (*SVGDevice) FillImage

func (d *SVGDevice) FillImage(img Image, ctm raster.Matrix, alpha float32, cp ColorParams)

FillImage implements Device.

func (*SVGDevice) FillImageMask

func (d *SVGDevice) FillImageMask(img Image, ctm raster.Matrix, cs *ColorSpace, color []float32, alpha float32, cp ColorParams)

FillImageMask implements Device, filling the stencil here: SVG has none.

func (*SVGDevice) FillPath

func (d *SVGDevice) FillPath(p *raster.Path, evenOdd bool, ctm raster.Matrix, cs *ColorSpace, color []float32, alpha float32, cp ColorParams)

FillPath implements Device.

func (*SVGDevice) FillShade

func (d *SVGDevice) FillShade(sh Shade, ctm raster.Matrix, alpha float32, cp ColorParams)

FillShade implements Device, rasterizing the shading.

func (*SVGDevice) FillText

func (d *SVGDevice) FillText(t *Text, ctm raster.Matrix, cs *ColorSpace, color []float32, alpha float32, cp ColorParams)

FillText implements Device.

func (*SVGDevice) PopClip

func (d *SVGDevice) PopClip()

PopClip implements Device.

func (*SVGDevice) StrokePath

func (d *SVGDevice) StrokePath(p *raster.Path, s *raster.Stroke, ctm raster.Matrix, cs *ColorSpace, color []float32, alpha float32, cp ColorParams)

StrokePath implements Device.

func (*SVGDevice) StrokeText

func (d *SVGDevice) StrokeText(t *Text, s *raster.Stroke, ctm raster.Matrix, cs *ColorSpace, color []float32, alpha float32, cp ColorParams)

StrokeText implements Device.

type Shade

type Shade interface {
	// Transform maps the shading's own space into the space ctm starts from.
	Transform() raster.Matrix
	// Bounds is the shading's bounding box in its own space, empty for none.
	Bounds() raster.Rect
	// ColorSpace is the space the shading's colors are in.
	ColorSpace() *ColorSpace
	// Shader returns what paints the shading into a destination of the given
	// model, under ctm and over box, and nil when nothing is painted.
	Shader(model raster.Model, ctm raster.Matrix, box raster.Rect) raster.Shader
}

Shade is a smooth gradation of color over an area, a shading or a gradient.

type Text

type Text struct {
	Spans []TextSpan
}

Text is what one text showing operator, or a run of them, hands to a device.

func (*Text) Bounds

func (t *Text) Bounds(ctm raster.Matrix) raster.Rect

Bounds returns a conservative bounding box for the text under ctm. An item carries its position in text space, so the span matrix supplies only the shape; without glyph outlines the extent is an em box around the advance, which is wider and taller than any real glyph.

type TextBlock

type TextBlock struct {
	Bounds raster.Rect
	// Lines are the block's lines, and nil for an image block.
	Lines []TextLine
	// Image is what an image block holds, and nil for a text block; Matrix
	// maps the unit square onto the page.
	Image  Image
	Matrix raster.Matrix
}

TextBlock is a paragraph of text, or one image.

type TextChar

type TextChar struct {
	Rune rune
	// Origin is where the pen was, and Quad the em box over the advance.
	Origin raster.Point
	Quad   Quad
	Size   float32
	Font   Font
	Color  [3]uint8
}

TextChar is one character where it was drawn.

type TextDevice

type TextDevice struct {
	BaseDevice
	// contains filtered or unexported fields
}

TextDevice collects what a page draws into a TextPage.

func NewTextDevice

func NewTextDevice(st *TextPage, opt *TextOptions) *TextDevice

NewTextDevice returns a device that collects the text of a page into st.

func (*TextDevice) ClipStrokeText

func (d *TextDevice) ClipStrokeText(t *Text, stroke *raster.Stroke, ctm raster.Matrix, scissor raster.Rect)

ClipStrokeText implements Device.

func (*TextDevice) ClipText

func (d *TextDevice) ClipText(t *Text, ctm raster.Matrix, scissor raster.Rect)

ClipText implements Device.

func (*TextDevice) Close

func (d *TextDevice) Close() error

Close implements Device.

func (*TextDevice) FillImage

func (d *TextDevice) FillImage(img Image, ctm raster.Matrix, alpha float32, cp ColorParams)

FillImage implements Device.

func (*TextDevice) FillImageMask

func (d *TextDevice) FillImageMask(img Image, ctm raster.Matrix, cs *ColorSpace, color []float32, alpha float32, cp ColorParams)

FillImageMask implements Device.

func (*TextDevice) FillText

func (d *TextDevice) FillText(t *Text, ctm raster.Matrix, cs *ColorSpace, color []float32, alpha float32, cp ColorParams)

FillText implements Device.

func (*TextDevice) IgnoreText

func (d *TextDevice) IgnoreText(t *Text, ctm raster.Matrix)

IgnoreText implements Device. Text that paints nothing is still text.

func (*TextDevice) StrokeText

func (d *TextDevice) StrokeText(t *Text, stroke *raster.Stroke, ctm raster.Matrix, cs *ColorSpace, color []float32, alpha float32, cp ColorParams)

StrokeText implements Device.

type TextItem

type TextItem struct {
	X, Y float32
	// GID is the glyph index in the font program, or -1 when the font engine
	// has not resolved one.
	GID int
	// Rune is the Unicode value the character code maps to, or -1.
	Rune rune
	// Name is what the font program calls the glyph, when it names it.
	Name string
	// Code is the character code the string held, and CID what the font's
	// encoding turned it into.
	Code, CID uint32
	// Adv is how far the pen moves, in text space units of the font size.
	Adv float32
}

TextItem is one glyph placed by a text showing operator.

type TextLine

type TextLine struct {
	Bounds raster.Rect
	// Dir is the unit vector the baseline runs along.
	Dir   raster.Point
	WMode int
	Chars []TextChar
}

TextLine is a run of characters along one baseline.

type TextOptions

type TextOptions struct {
	// Images records a block for every image drawn.
	Images bool
	// SkipHidden drops text drawn in a rendering mode that paints nothing.
	SkipHidden bool
}

TextOptions configure a TextDevice.

type TextPage

type TextPage struct {
	Bounds raster.Rect
	Blocks []TextBlock
}

TextPage is a page's text: blocks of lines of characters, each with its box, in the order the page drew them.

func (*TextPage) HTML added in v0.2.0

func (p *TextPage) HTML(id string) string

HTML writes the page as absolutely positioned text, the words where they were drawn. id names the division the page becomes.

func (*TextPage) Text

func (p *TextPage) Text() string

Text is the page's text: a newline after a line, a blank line after a block.

type TextSpan

type TextSpan struct {
	Font  Font
	WMode int
	Trm   raster.Matrix
	Items []TextItem
}

TextSpan is a run of glyphs from one font under one text matrix.

type Tint

type Tint interface {
	Eval(out []float64, in ...float64) []float64
}

Tint is the transform a Separation or DeviceN space runs its colorant values through to reach the space it falls back to.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL