Documentation
¶
Overview ¶
Package imageasset 管理渲染期图片和 SVG 场景的加载、懒解码与有界缓存。
每个文档视图持有独立的 Store,零值可用且支持并发访问。同一资源的并发加载 按键合并,图片与内存印章共用位图预算,SVG 场景使用独立预算;结束时调用 Close,关闭前已开始的加载可完成,但其结果不再回填缓存。
调用方提供资源读取器和 SVG 解析函数,本包不依赖 render、drawing 或 parser。 缓存返回的图片、原始编码字节及 SVG 场景均视为只读;场景的矢量嵌入与栅格化 由场景内部的同一把锁保护。页面放置、裁剪及印章预处理策略由调用方负责。
Index ¶
- func Decode(data []byte) (image.Image, error)
- func IsSVG(format string, file models.StLoc) bool
- type EncodedImage
- func (e *EncodedImage) At(x, y int) color.Color
- func (e *EncodedImage) Bounds() image.Rectangle
- func (e *EncodedImage) CanvasImageOnce(build func() image.Image) image.Image
- func (e *EncodedImage) ColorModel() color.Model
- func (e *EncodedImage) Image() (image.Image, error)
- func (e *EncodedImage) Weight() int64
- type Reader
- type SVGParser
- type SVGScene
- type Store
- func (s *Store) Close()
- func (s *Store) Image(key string, load func() (image.Image, error)) (image.Image, error)
- func (s *Store) Load(file models.StLoc, format string, source Reader, parse SVGParser) (image.Image, error)
- func (s *Store) LoadSVG(file models.StLoc, format string, source Reader, parse SVGParser) (SVGScene, error)
- func (s *Store) SVG(key string, load func() ([]byte, error), parse SVGParser) (SVGScene, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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) 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 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 场景。
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store 持有单个文档视图的图片和 SVG 场景缓存,零值可用且支持并发调用。 首次使用后不可复制,同一实例始终使用同一份文档资源。
func (*Store) Close ¶
func (s *Store) Close()
Close 清空图片和场景缓存,并阻止在途加载重新发布结果;可重复调用。 正在使用的加载锁在最后一个调用结束后移除。
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。