textdoc

package
v0.1.3 Latest Latest
Warning

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

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

Documentation

Overview

Package textdoc 把 OFD 页面中的文字对象提取为带位置的条目,供文本、Markdown 等编码器复用。它只依赖 OFD 模型与解析器,不感知任何输出格式。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Count

func Count(documents []*parser.Document) int

Count 统计所有文档体的非空页面总数。

func DisplayWidth

func DisplayWidth(text string) int

DisplayWidth 返回按终端显示列数计算的宽度:CJK 等全角字符占两列,其余字符 占一列,保证多栏文字的列位置对齐。

func EnsureUTF8

func EnsureUTF8(value string) string

EnsureUTF8 保证输出文本是合法 UTF-8。OFD 规范要求文字为 UTF-8,但部分第三方 文档会写入 GBK 等本地编码的原始字节;这里先尝试按 GBK 解码,失败时再把无法 识别的字节替换为 U+FFFD,避免 .txt/.md 出现非法 UTF-8 导致乱码。

func Finite

func Finite(value float64) bool

Finite 判断浮点数是否为有限值(非 NaN、非 Inf)。

func JoinText

func JoinText(row []Entry) string

JoinText 用空格连接一行中的所有文字。

func RowLeft

func RowLeft(row []Entry) float64

RowLeft 返回一行的最小 X 坐标。

func RowRight

func RowRight(row []Entry) float64

RowRight 返回一行的最大右边界。

func RowSize

func RowSize(row []Entry) float64

RowSize 返回一行中的最大字号。

func RowTolerance

func RowTolerance(a, b, unit float64) float64

RowTolerance 返回聚行时允许的纵向容差。

func RowTop

func RowTop(row []Entry) float64

RowTop 返回一行的最小 Y 坐标。

func Rows

func Rows(entries []Entry) [][]Entry

Rows 按 Y 坐标把文字条目聚成行,行内按 X 排序。

Types

type Entry

type Entry struct {
	Text  string
	X     float64
	Y     float64
	Size  float64
	Width float64
}

Entry 是一个待输出的文字对象及其在页面中的位置与尺寸(毫米,原点在页面左上角)。

type Page

type Page struct {
	Entries []Entry
	Width   float64
	Height  float64
}

Page 保存单页的文字条目以及该页物理尺寸,用于按列对齐和页眉页脚判定。

func Collect

func Collect(documents []*parser.Document, start, end int) []Page

Collect 提取全局页索引范围 [start, end) 内的页面。调用方需保证 start/end 合法(通常先经页码校验);end 超出总页数时按总页数截断。

func ExtractPage

func ExtractPage(doc *parser.Document, page *parser.Page) Page

ExtractPage 提取单个页面的文字条目和物理尺寸。

type Table

type Table struct {
	Start  int
	End    int
	Header []string
	Rows   [][]string
}

Table 描述一个已识别的表格。Start/End 是页内行索引(含),Header 为表头 单元格文本,Rows 为数据行。

func DetectTables

func DetectTables(rows [][]Entry) []Table

DetectTables 在已按行分组的文字条目中识别表格。算法:

  1. 行内按水平间距切分单元格,含两个及以上单元格的行为候选网格行;
  2. 用候选网格行的单元格区间求并,间隔(白槽)处切分得到列;
  3. 每个候选行的单元格必须各自落在唯一列内且覆盖至少两列,才认定为表格行;
  4. 只有一个单元格且落在唯一列内的行作为续行,合并到最近的表格行;
  5. 被非表格行(标题、页脚等)隔开的多行网格行组成一个表格。

Jump to

Keyboard shortcuts

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