webreader

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: Unlicense Imports: 16 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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 {
	FallbackFonts []FontSource
}

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) AddFallbackFont

func (r *Reader) AddFallbackFont(source FontSource) error

AddFallbackFont 为没有内嵌 FontFile 的文档字体添加外部字体。 文字和搜索快照会失效,因为它们的字形度量可能使用了不同的回退字体。

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) Page

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

Page 返回指定页面的尺寸信息。

func (*Reader) PageCount

func (r *Reader) PageCount() int

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

func (*Reader) Pages

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

Pages 返回所有页面的尺寸快照,尺寸单位为毫米。

func (*Reader) RenderPDF

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

RenderPDF 将多个页面按传入顺序栅格化后写入一个 PDF 文档。

func (*Reader) RenderPage

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

RenderPage 将指定页面渲染为 PNG 数据。

func (*Reader) RenderPages

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

RenderPages 将多个页面按传入顺序渲染为 PNG 数据。 所有页面共享一次 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 页面绘制顺序一致。

type Rect

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

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

type RenderOptions

type RenderOptions struct {
	DPI        float64
	Background color.Color
}

RenderOptions 控制页面输出。DPI 控制 PNG 和 PDF 中页面图像的分辨率。

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