Help us shape the future of Go! Take the 2026 Go Developer Survey

rastercore

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package rastercore 提供可切换栅格后端共用的渲染上下文状态机。

gg、ftgg、tinyskia 三个后端只有「画笔/路径提交/图像合成」随光栅库不同, 其余语义完全一致:Push/Pop 样式镜像、逻辑矩阵累计、毫米→设备像素换算、 描边造型缩放、路径扫描、DrawImage 矩阵拼装、CopyStrokeToFill 等。 这些共享逻辑集中在本包,各后端只需实现 Hooks。

Core 直接满足 render.DrawContext(并带 Raster),且全部使用与绘制库无关 的 geom 类型,因此本包与各后端都不依赖 tdewolff/canvas。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatrixScale

func MatrixScale(m geom.Matrix) float64

MatrixScale 返回矩阵的不变缩放因子(行列式开方),用于线宽/虚线换算。

Types

type Core

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

Core 是 DrawContext 的共享实现。像素尺寸与 canvas 的 Rasterize 取整 规则一致(int(w*dpmm+0.5))。

func New

func New(width, height float64, resolution geom.Resolution, factory func(wpx, hpx int, dpmm, hpix float64) Hooks) (*Core, error)

New 计算像素尺寸并调用 factory 构造光栅库的 Hooks。factory 收到已经过 取整校验的 wpx/hpx(像素)与 dpmm/hpix,返回库相关的 Hooks 实现。

func (*Core) ClearFill

func (c *Core) ClearFill()

ClearFill 清除填充。

func (*Core) ClearStroke

func (c *Core) ClearStroke()

ClearStroke 清除描边。

func (*Core) CopyStrokeToFill

func (c *Core) CopyStrokeToFill()

CopyStrokeToFill 把当前描边画笔真正复制为填充画笔(含纯色、渐变)。

func (*Core) CurrentMatrix

func (c *Core) CurrentMatrix() geom.Matrix

CurrentMatrix 返回当前逻辑矩阵(CoordSystem·View)。

func (*Core) DrawImage

func (c *Core) DrawImage(img image.Image, x, y float64, dpmm float64)

DrawImage 绘制图片:dpmm 是源图像素到画布单位(mm)的换算。

func (*Core) DrawPath

func (c *Core) DrawPath(x, y float64, p *geom.Path)

DrawPath 按当前样式绘制路径(x,y 为路径平移到画布的偏移)。

func (*Core) Pop

func (c *Core) Pop()

Pop 恢复最近一次 Push 保存的样式与变换。

func (*Core) Push

func (c *Core) Push()

Push 保存当前样式与变换。

func (*Core) Raster

func (c *Core) Raster() *image.RGBA

Raster 返回当前光栅库绘制的 RGBA 图像。

func (*Core) RenderImage

func (c *Core) RenderImage(img image.Image, m geom.Matrix)

RenderImage 按绝对设备矩阵绘制预渲染图片。

func (*Core) Rotate

func (c *Core) Rotate(deg float64)

Rotate 追加旋转变换(设备换算在 DrawPath 执行)。

func (*Core) Scale

func (c *Core) Scale(sx, sy float64)

Scale 追加缩放变换(设备换算在 DrawPath 执行)。

func (*Core) SetDashes

func (c *Core) SetDashes(offset float64, dashes ...float64)

SetDashes 设置虚线数组与相位。

func (*Core) SetFillColor

func (c *Core) SetFillColor(col color.Color)

SetFillColor 设置纯色填充。

func (*Core) SetFillGradient

func (c *Core) SetFillGradient(g geom.Gradient)

SetFillGradient 设置渐变填充(延迟到 DrawPath 时按矩阵采样)。

func (*Core) SetFillPaint

func (c *Core) SetFillPaint(paint geom.Paint)

SetFillPaint 设置填充画笔(颜色、渐变或清除)。

func (*Core) SetFillRule

func (c *Core) SetFillRule(rule geom.FillRule)

SetFillRule 设置填充规则。

func (*Core) SetStrokeCapper

func (c *Core) SetStrokeCapper(cap geom.Capper)

SetStrokeCapper 设置线帽。

func (*Core) SetStrokeColor

func (c *Core) SetStrokeColor(col color.Color)

SetStrokeColor 设置纯色描边。

func (*Core) SetStrokeGradient

func (c *Core) SetStrokeGradient(g geom.Gradient)

SetStrokeGradient 设置描边渐变(延迟到 DrawPath 时按矩阵采样)。

func (*Core) SetStrokeJoiner

func (c *Core) SetStrokeJoiner(join geom.Joiner)

SetStrokeJoiner 设置连接器,并记录斜接限制。

func (*Core) SetStrokePaint

func (c *Core) SetStrokePaint(paint geom.Paint)

SetStrokePaint 设置描边画笔(颜色、渐变或清除)。

func (*Core) SetStrokeWidth

func (c *Core) SetStrokeWidth(w float64)

SetStrokeWidth 设置描边宽度(逻辑毫米)。

func (*Core) StrokeCapper

func (c *Core) StrokeCapper() geom.Capper

StrokeCapper 返回当前线帽。

func (*Core) StrokeJoiner

func (c *Core) StrokeJoiner() geom.Joiner

StrokeJoiner 返回当前连接器。

func (*Core) StrokeWidth

func (c *Core) StrokeWidth() float64

StrokeWidth 返回当前线宽。

func (*Core) TextPath

func (c *Core) TextPath(p *geom.Path, x, y float64)

TextPath 绘制字形轮廓路径(同 DrawPath)。

func (*Core) Transform added in v0.1.4

func (c *Core) Transform(m geom.Matrix)

Transform 右乘当前逻辑矩阵,与 Translate/Scale/Rotate 的追加语义一致。

func (*Core) Translate

func (c *Core) Translate(x, y float64)

Translate 追加平移变换(设备换算在 DrawPath 执行)。

type DeviceOp

type DeviceOp byte

DeviceOp 是设备路径的一段。

const (
	// MoveOp/LineOp/QuadOp/CubeOp/CloseOp 对应路径段类型。
	MoveOp DeviceOp = iota
	LineOp
	QuadOp
	CubeOp
	CloseOp
)

type DevicePath

type DevicePath struct {
	Segs []DeviceSeg
}

DevicePath 是复用缓冲的设备路径,避免每次绘制都分配新切片。

func (*DevicePath) Close

func (p *DevicePath) Close()

Close 追加一个闭合段。

func (*DevicePath) CubeTo

func (p *DevicePath) CubeTo(c1x, c1y, c2x, c2y, x, y float64)

CubeTo 追加一个三次贝塞尔段。

func (*DevicePath) LineTo

func (p *DevicePath) LineTo(x, y float64)

LineTo 追加一个直线段。

func (*DevicePath) MoveTo

func (p *DevicePath) MoveTo(x, y float64)

MoveTo 追加一个移动到点。

func (*DevicePath) QuadTo

func (p *DevicePath) QuadTo(cx, cy, x, y float64)

QuadTo 追加一个二次贝塞尔段。

func (*DevicePath) Reset

func (p *DevicePath) Reset()

Reset 清空路径段,保留底层容量。

type DeviceSeg

type DeviceSeg struct {
	Op DeviceOp
	P  [3]geom.Point
}

DeviceSeg 是一段设备像素坐标的路径。P 按段类型使用前 1~3 个点。

type Hooks

type Hooks interface {
	// Push 保存光栅库自身的画笔与变换。
	Push()
	// Pop 恢复最近一次 Push 保存的光栅库画笔与变换。
	Pop()

	// SetFillSolid 设置纯色填充画笔。
	SetFillSolid(c color.Color)
	// SetStrokeSolid 设置纯色描边画笔。
	SetStrokeSolid(c color.Color)
	// ClearFill 清除填充画笔。
	ClearFill()
	// ClearStroke 清除描边画笔。
	ClearStroke()

	// SetFillRule 设置光栅库的填充规则。
	SetFillRule(rule geom.FillRule)

	// DrawDevicePath 提交已换算到设备像素坐标的路径,并按需填充/描边。
	// m 是当次绘制的完整逻辑矩阵(渐变采样/几何换算用);fillGrad 与
	// strokeGrad 为 nil 表示使用当前已设置的纯色画笔。Core 已保证 fill 与
	// stroke 至少一个为真,且 stroke 为真时 style.Width>0。
	DrawDevicePath(path *DevicePath, m geom.Matrix, fill, stroke bool, fillGrad, strokeGrad geom.Gradient, style StrokeStyle)

	// RenderImage 按绝对设备矩阵合成预渲染图像(离屏合成/mesh/文字图层)。
	RenderImage(img image.Image, m geom.Matrix)

	// Raster 返回当前绘制的全部内容对应的 RGBA 图像。
	Raster() *image.RGBA
}

Hooks 是共享状态机与具体光栅库之间的桥。

Core 负责渲染上下文语义,Hooks 只做与库相关的画笔设置、路径提交与图像 合成,全部使用 geom 类型。

type StrokeStyle

type StrokeStyle struct {
	Width      float64 // 设备像素线宽
	Cap        geom.Capper
	Join       geom.Joiner
	MiterLimit float64
	Dashes     []float64 // 设备像素虚线数组
	DashOffset float64   // 设备像素相位
}

StrokeStyle 是换算到设备像素后的描边造型,交给 Hooks 写入光栅库。

虚线长度与相位和线宽一样是用户空间长度,按 dpmm 与矩阵缩放换算成设备 像素即可。OFD 的 DashPattern/DashOffset 以毫米为单位、与线宽无关, PDF 32000-1 §9.3.6 与 SVG 规范也都规定虚线不随线宽缩放,所以换算因子 里不能带线宽。

Jump to

Keyboard shortcuts

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