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

imageasset

package
v0.1.7 Latest Latest
Warning

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

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

Documentation

Overview

Package imageasset 管理渲染期图片和 SVG 场景的加载、懒解码与有界缓存。

每个文档视图持有独立的 Store,零值可用且支持并发访问。同一资源的并发加载 按键合并,图片与内存印章共用位图预算,SVG 场景使用独立预算;结束时调用 Close,关闭前已开始的加载可完成,但其结果不再回填缓存。

调用方提供资源读取器和 SVG 解析函数,本包不依赖 render、drawing 或 parser。 缓存返回的图片、原始编码字节及 SVG 场景均视为只读;场景的矢量嵌入与栅格化 由场景内部的同一把锁保护。页面放置、裁剪及印章预处理策略由调用方负责。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

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

Decode 对 JPEG/PNG 使用懒解码并保留原始编码字节,供 PDF 写入器直接内嵌。 其他格式经 media 解码,调色板图像转换为可快速采样的 RGBA。

func IsSVG

func IsSVG(format string, file models.StLoc) bool

IsSVG 判断声明格式或资源扩展名是否表示 SVG。

Types

type EncodedImage

type EncodedImage struct {
	Format string // "jpeg" 或 "png"
	Data   []byte
	// contains filtered or unexported fields
}

EncodedImage 是保留原始编码字节的懒解码图片:以标准 image.Image 暴露, 首次访问时才解码;同时保留编码数据,供 canvas PDF 写入器等按原字节内嵌。 它与绘制库无关;canvas 适配层可把编码数据再包装成 canvas/image.Image。

func NewEncodedImage

func NewEncodedImage(format string, data []byte) *EncodedImage

NewEncodedImage 返回保留编码数据的懒解码图片;data 在使用期间必须保持只读。

func (*EncodedImage) At

func (e *EncodedImage) At(x, y int) color.Color

At 返回像素颜色(会触发解码)。

func (*EncodedImage) Bounds

func (e *EncodedImage) Bounds() image.Rectangle

Bounds 返回图片边界。只读取图片头(DecodeConfig)获取尺寸,不触发完整解码。

func (*EncodedImage) CanvasImageOnce

func (e *EncodedImage) CanvasImageOnce(build func() image.Image) image.Image

CanvasImageOnce 返回 canvas 适配层构造的对象,首次调用时用 build 构造并缓存, 之后直接返回同一对象。并发调用只会构造一次,因此可以安全地在多个页面同时 渲染时使用。build 返回 nil 表示该图片不走原字节内嵌快路径(例如灰度图或 编码无法识别),此时结果同样被缓存,避免每次访问都重新解码。

func (*EncodedImage) ColorModel

func (e *EncodedImage) ColorModel() color.Model

ColorModel 返回颜色模型。只读取图片头,不触发完整解码。

func (*EncodedImage) Image

func (e *EncodedImage) Image() (image.Image, error)

Image 解码并返回底层原生图像,结果会缓存。它同时满足各后端用于“延迟解码 包装器”的最小接口(Image() (image.Image, error))。

func (*EncodedImage) Weight

func (e *EncodedImage) Weight() int64

Weight 估算该图片在缓存中的内存占用:编码字节 + 解码后的像素。只读取 图片头(DecodeConfig)获取尺寸与颜色模型,不触发完整解码。

type Reader

type Reader interface {
	Read(name string) ([]byte, error)
}

Reader 按文档内的规范化路径读取资源字节。

type SVGParser

type SVGParser func([]byte) (SVGScene, error)

SVGParser 将 SVG 数据解析为可栅格化或矢量嵌入的场景。

type SVGScene

type SVGScene interface {
	// Width/Height 返回 SVG 的原始画布尺寸(用户单位)。
	Width() float64
	Height() float64
	// Rasterize 按分辨率栅格化为标准图像。
	Rasterize(resolution geom.Resolution) image.Image
	// RenderVector 尝试以矢量方式嵌入当前绘制上下文,返回是否完成。
	RenderVector(ctx any, matrix geom.Matrix) bool
}

SVGScene 是与绘制库无关的已解析 SVG 场景。

func ParseSVG

func ParseSVG(data []byte, parse SVGParser) (SVGScene, error)

ParseSVG 解析场景,并将其栅格化和矢量嵌入操作串行化,保护共享字体和渐变状态。

type Store

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

Store 持有单个文档视图的图片和 SVG 场景缓存,零值可用且支持并发调用。 首次使用后不可复制,同一实例始终使用同一份文档资源。

func (*Store) Close

func (s *Store) Close()

Close 清空图片和场景缓存,并阻止在途加载重新发布结果;可重复调用。 正在使用的加载锁在最后一个调用结束后移除。

func (*Store) Image

func (s *Store) Image(key string, load func() (image.Image, error)) (image.Image, error)

Image 按键合并并发加载,资源图片和已预处理的内存印章共用有界 LRU。 load 在缓存未命中时执行,执行期间只持该键的锁,不阻塞其他图片。

func (*Store) Load

func (s *Store) Load(file models.StLoc, format string, source Reader, parse SVGParser) (image.Image, error)

Load 返回缓存图片。JPEG/PNG 保留原编码并懒解码;SVG 的位图回退使用 96 DPI。

func (*Store) LoadSVG

func (s *Store) LoadSVG(file models.StLoc, format string, source Reader, parse SVGParser) (SVGScene, error)

LoadSVG 返回规范化资源路径对应的缓存场景。

func (*Store) SVG

func (s *Store) SVG(key string, load func() ([]byte, error), parse SVGParser) (SVGScene, error)

SVG 复用已解析场景,场景解析在缓存锁外执行。 SVG 场景和同名图片使用不同的加载锁,允许图片回退复用场景而不自锁。

Jump to

Keyboard shortcuts

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