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

text

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: 7 Imported by: 0

Documentation

Overview

Package text 提供 OFD 文字绘制与文字提取共用的字形映射和布局计算。

本包只依赖 OFD 模型、字体修复映射与几何类型,通过 Face 读取字体度量,不持有 文档、字体引擎或绘制上下文。字体选择、字体锁、页面租约和内容遍历由 render 负责;调用本包期间,调用方须按字体实现的约定持锁。

字符位置以毫米为单位,采用 OFD 的左上原点坐标系;CTMLinearMatrix 用于将 同一文字 CTM 应用到 y 向上的字体轮廓。输入模型和定位数组均视为只读,连字 的字符框只是搜索所需的近似分配,不替换实际绘制使用的字形定位队列。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Advance

func Advance(glyphWidth float64, object models.TextObject, code models.TextCode, index int) (float64, float64)

Advance 返回源字符到下一字符的增量。显式 DeltaX/DeltaY 优先,两者均未指定 时,按 ReadDirection 使用字体字宽和 HScale 计算。

func CTMLinearMatrix

func CTMLinearMatrix(ctm *models.CTM) *geom.Matrix

CTMLinearMatrix 返回图元 CTM 的线性部分,供 y 向上的字形变换使用。 OFD 的 CTM 定义在 y 向下的对象空间,因此进行 F·L·F 共轭以保留倾斜与旋转方向。 CTM 为 nil、单位变换或含非有限分量时返回 nil。

func CanRenderRune

func CanRenderRune(face Face, r rune) bool

CanRenderRune 判断字体是否有指定 Unicode 码位的字形。

func CanRenderText

func CanRenderText(face Face, value string) bool

CanRenderText 判断文本是否可按 Unicode 整形:不含字形私有码位,且每个可见 字符都能在字体中找到字形。控制字符不参与覆盖查询。

func CharDirection

func CharDirection(object models.TextObject) float64

CharDirection 返回归一化的字符旋转角度。

func DirectionsZero

func DirectionsZero(object models.TextObject) bool

DirectionsZero 判断阅读方向和字符方向是否均为水平方向。

func HScale

func HScale(object models.TextObject) float64

HScale 返回文字的水平缩放比例,缺省值为 1。

func NeedsRunBreak

func NeedsRunBreak(object models.TextObject, deltaX, deltaY, naturalX float64) bool

NeedsRunBreak 仅在源步进能由字体自然步进表达时合并字形。 GB/T 33190—2016 §11.2 表46、图62规定 DeltaX/DeltaY 是相邻绘制点的精确偏移; 小幅字距也不能忽略,否则误差逐字累积,放大 CTM 后可能把行末标点推到 Boundary 外。 这里只容纳字号换算造成的十万分之一以内数值误差,不扩大任何裁剪边界。

func Outline

func Outline(face OutlineFace, object models.TextObject, transform func(x, y float64) geom.Matrix) *geom.Path

Outline 按与正文相同的定位和分段规则合并可见字形轮廓,不包含语义补偿文字。 transform 将每个 run 的 Y 向上字形空间映射到目标空间,字体锁由调用方持有。

func PositionedAdvance

func PositionedAdvance(width float64, object models.TextObject, code models.TextCode, previous, current Glyph, glyphDeltas bool) (float64, float64)

PositionedAdvance 按字符或字形定位队列返回相邻绘制字形之间的增量。

func ReadAdvance

func ReadAdvance(advance float64, direction int) (float64, float64)

ReadAdvance 将字宽按阅读方向转换为 OFD 坐标增量。

func Renderable

func Renderable(value string) bool

Renderable 判断文本是否包含可见字符。C0/C1、DEL 和 U+FFFD 不参与字形绘制。

func RenderableRune

func RenderableRune(r rune) bool

RenderableRune 判断码位是否可交给字体绘制,避免控制字符显示为 .notdef。

func UsesGlyphDeltas

func UsesGlyphDeltas(code models.TextCode, glyphs []Glyph) bool

UsesGlyphDeltas 判断定位数组是否明确对应变换后的字形数量。 GB/T 33190 表 46 按绘制字形定位,部分文档则保留变换前的字符增量。

func VisibleText

func VisibleText(value string) string

VisibleText 过滤交给整串整形的文字,不改动源 TextCode 和定位队列。

func WalkOutlines

func WalkOutlines(face OutlineFace, object models.TextObject, transform func(x, y float64) geom.Matrix, visit func(*geom.Path))

WalkOutlines 按绘制顺序访问每个 run 的独立轮廓,供描边等必须在合并前处理 的操作使用。回调取得路径所有权,可以修改它;字体缓存中的原始轮廓保持只读。

func WalkRuns

func WalkRuns(face Face, object models.TextObject, code models.TextCode, codePosition int, visit func(Run))

WalkRuns 按正文绘制规则定位并合并一个 TextCode 的字形,保持原来的分段顺序。 object.CGTransform 应已展开对象内的位置;调用方负责字体锁,输入均视为只读。

Types

type Character

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

Character 是源字符在 OFD 坐标下的近似区域,单位为毫米。

type Face

type Face interface {
	TextWidth(value string) float64
	GlyphIndex(r rune) uint16
	IsCFF() bool
	VerticalMetrics() (float64, float64)
}

Face 提供布局所需的字体查询。调用方负责字体选择和必要的锁保护。

type Glyph

type Glyph struct {
	Value         string
	Semantic      string
	CodePosition  int
	GlyphPosition int
	GlyphCount    int
}

Glyph 记录实际绘制值及其对应的源字符、字形定位范围。 Value 可以是 Unicode 文本或 fontrepair 字形私有码位,Semantic 保留其源文字。

func CodeGlyphs

func CodeGlyphs(face Face, runes []rune, transforms []models.CTCGTransform, codePosition int) []Glyph

CodeGlyphs 将一个 TextCode 映射为绘制字形,保留连字与一字符多字形的源文字。 transforms 应已通过 models.ExpandCGTransformPositions 展开对象内的位置。

type Layout

type Layout struct {
	Source Source
	Text   string
	X      float64
	Y      float64
	Width  float64
	// Height 为字体上伸与下延之和,无度量时使用 EM 高度。
	Height        float64
	Font          uint64
	Size          float64
	Weight        int
	ReadDirection int
	CharDirection int
	Bold          bool
	Italic        bool
	Glyphs        []Character
}

Layout 描述一个 TextCode 及其源字符级几何信息。

func Build

func Build(face Face, object models.TextObject, code models.TextCode, codePosition int) Layout

Build 根据字体度量和对象定位构造源字符布局。face 的字号应已计入 CTM.YScale, nil 表示无字体,使用 Boundary 宽度和 EM 高度兜底。输入对象和增量均不修改。

type OutlineFace

type OutlineFace interface {
	Face
	DirectPath(value string) *geom.Path
	ToPath(value string) *geom.Path
}

OutlineFace 在定位度量之外提供只读字形轮廓。轮廓可以来自共享字体缓存。

type Run

type Run struct {
	Value        string
	X, Y         float64
	SemanticOnly bool
}

Run 是同一原点绘制的文字串。SemanticOnly 只保留映射字形对应的源文字, 不参与可见轮廓;PDF 输出可用它补充复制与搜索所需的不可见文字。

type Source

type Source string

Source 标记文字来源,具体来源及收集策略由页面遍历层决定。

Jump to

Keyboard shortcuts

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