webreader

package
v0.1.2 Latest Latest
Warning

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

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

Documentation

Overview

Package webreader 包提供适合浏览器调用的 OFD 文档访问接口。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterFallbackFont added in v0.1.2

func RegisterFallbackFont(source FontSource) error

RegisterFallbackFont 在进程内全局注册回退字体,与具体 Reader 无关。 同一字体族只注册一次(幂等)且不复制字体数据;之后可通过 Reader.UseFallbackFont 应用到某个文档。

Types

type DocumentInfo added in v0.1.1

type DocumentInfo struct {
	DocID        string
	Title        string
	Author       string
	Subject      string
	Abstract     string
	CreationDate string
	ModDate      string
	Creator      string
	Version      string
}

DocumentInfo 描述 OFD 文档的元数据信息。

type FontResource

type FontResource struct {
	ID     uint64
	Family string
	Name   string
	Bold   bool
	Italic bool
	Format string
	Data   []byte
}

FontResource 描述文档中可注入浏览器的嵌入字体。 Data 为空表示该字体只声明了名称,没有嵌入字体文件。

type FontSource

type FontSource struct {
	Family string
	Name   string
	Weight int
	Italic bool
	Data   []byte
}

FontSource 是由调用方提供给 WASM 渲染器的字体文件。 浏览器无法读取本机系统字体文件,因此无内嵌字体时应传入可访问的 TTF/OTF Web Font 数据,例如 Google Fonts 的 Noto Sans SC。

type Glyph

type Glyph struct {
	Text   string
	X      float64
	Y      float64
	Width  float64
	Height float64
	Angle  float64
}

Glyph 描述一个字符的页面区域,坐标单位为毫米。

type OpenOptions

type OpenOptions struct {
	// PageCacheCapacity 是页面缓存最多保留的页面数量,0 表示使用默认值。
	PageCacheCapacity int
	// PageCacheBytes 是页面缓存允许使用的估算最大字节数,0 表示使用默认值。
	PageCacheBytes int64
}

OpenOptions 配置 OFD 打开行为。

type PageInfo

type PageInfo struct {
	Index  int
	Width  float64
	Height float64
}

PageInfo 描述一个可渲染页面。尺寸单位为毫米。

type Reader

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

Reader 是一个已打开的 OFD 文档。 Reader 负责持有文档资源,使用完毕后必须调用 Close。

func Open

func Open(data []byte) (*Reader, error)

Open 从内存中的 OFD 数据创建浏览器文档引擎。

func OpenWithOptions

func OpenWithOptions(data []byte, options OpenOptions) (*Reader, error)

OpenWithOptions 从内存中的 OFD 数据创建浏览器文档引擎。

func (*Reader) Close

func (r *Reader) Close() error

Close 释放文档资源。Close 可以安全地重复调用。

func (*Reader) Fonts

func (r *Reader) Fonts() ([]FontResource, error)

Fonts 返回文档中声明的字体资源。只有包含 FontFile 的字体会带有 Data, 调用方可以将这些数据交给浏览器 FontFace 构造器进行注入。

func (*Reader) Info added in v0.1.1

func (r *Reader) Info() (DocumentInfo, error)

Info 返回 OFD 文档的元数据信息。

func (*Reader) Page

func (r *Reader) Page(index int) (PageInfo, error)

Page 返回指定页面的真实尺寸信息。 与 Pages 不同,查询单页信息会按需加载该页完整内容。

func (*Reader) PageCount

func (r *Reader) PageCount() int

PageCount 返回文档的总页数,页码从 0 开始。

func (*Reader) Pages

func (r *Reader) Pages() ([]PageInfo, error)

Pages 返回所有页面的尺寸快照,尺寸单位为毫米。 优先读取页面 XML 中的 Area/PhysicalBox,不加载页面内容或资源。

func (*Reader) RenderPDF

func (r *Reader) RenderPDF(indices []int, options RenderOptions) (outputBytes []byte, err error)

RenderPDF 将多个页面按传入顺序写入一个保留文字和矢量内容的 PDF 文档。 该接口会把完整 PDF 保存在内存中,需要导出大量页面时应使用 RenderPDFTo。

func (*Reader) RenderPDFTo added in v0.1.2

func (r *Reader) RenderPDFTo(output io.Writer, indices []int, options RenderOptions) (err error)

RenderPDFTo 将多个页面按传入顺序写入 output,保留文字和矢量内容。 output 会在 PDF 生成过程中接收数据,适合流式保存大 PDF,因此不限制页数。

func (*Reader) RenderPage

func (r *Reader) RenderPage(index int, options RenderOptions) ([]byte, error)

RenderPage 将指定页面渲染为 PNG、JPG 或 SVG 数据。

func (*Reader) RenderPages

func (r *Reader) RenderPages(indices []int, options RenderOptions) ([][]byte, error)

RenderPages 将多个页面按传入顺序渲染为 PNG、JPG 或 SVG 数据。 所有页面共享一次 Reader 锁和同一份文档状态;渲染本身仍按顺序执行。

func (*Reader) Search

func (r *Reader) Search(query string) ([]SearchResult, error)

Search 在所有页面的文字对象中查找 query,匹配不区分大小写。

func (*Reader) Text

func (r *Reader) Text(index int) ([]TextRun, error)

Text 返回指定页面的文字对象快照。文字顺序与 OFD 页面绘制顺序一致。

func (*Reader) UseFallbackFont added in v0.1.2

func (r *Reader) UseFallbackFont(family string) error

UseFallbackFont 使当前文档缺失字体时使用已全局注册的回退字体族。 该字体族必须先通过 RegisterFallbackFont 注册。文字和搜索快照会失效, 因为它们的字形度量可能使用了不同的回退字体。

type Rect

type Rect struct {
	X      float64
	Y      float64
	Width  float64
	Height float64
	Angle  float64
}

Rect 描述一个搜索命中的字符区域,坐标单位为毫米。

type RenderFormat added in v0.1.1

type RenderFormat string

RenderFormat 是页面输出格式。

const (
	// RenderPNG 输出 PNG 位图。为空时也使用该格式。
	RenderPNG RenderFormat = "png"
	// RenderSVG 输出 SVG 矢量文档。
	RenderSVG RenderFormat = "svg"
	// RenderJPG 输出 JPG 位图。JPG 不支持透明度,透明区域使用白色填充。
	RenderJPG RenderFormat = "jpg"
)

type RenderOptions

type RenderOptions struct {
	DPI        float64
	Background color.Color
	Format     RenderFormat
}

RenderOptions 控制页面输出。DPI 控制 PNG、JPG 以及 PDF 中复杂效果的内部栅格化分辨率; PDF 页面主体保留文字和矢量内容,SVG 复杂渐变等仍可能包含栅格回退。

type SearchResult

type SearchResult struct {
	Page  int
	Run   int
	Text  string
	Start int
	End   int
	Rects []Rect
}

SearchResult 描述一个页面文字命中。

type TextRun

type TextRun struct {
	Text          string
	X             float64
	Y             float64
	Width         float64
	Height        float64
	Font          uint64
	Size          float64
	Weight        int
	ReadDirection int
	CharDirection int
	FontFamily    string
	Bold          bool
	Italic        bool
	Glyphs        []Glyph
}

TextRun 描述页面中的一个文字对象。坐标和尺寸单位为毫米。 X/Y 是网页覆盖层使用的左上角坐标,不是 TextCode 的基线坐标。

Jump to

Keyboard shortcuts

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