Documentation
¶
Overview ¶
Package compare 提供 OFD 包内文件、XML 结构,以及 OFD/PDF 页面文字和画面的对比能力。
Index ¶
- Constants
- func WriteReport(w io.Writer, report *Report, format string) error
- type Comparison
- type DocumentInfo
- type Environment
- type File
- type FileChange
- type FontEnvironment
- type FontFace
- type IgnoreRegion
- type Input
- type Options
- type PDFOptions
- type PDFProgramDiff
- type PDFProgress
- type PDFResourceBinding
- type PDFResourceChange
- type PDFSettings
- type PDFSourceUse
- type PDFStreamSource
- type PDFStructurePage
- type PDFStructureResult
- type Page
- type PageChange
- type PageMatchInfo
- type PagePair
- type PageRange
- type Report
- type Summary
- type TextRegion
- type TextSpan
- type ValueChange
- type VisualChange
- type VisualRegion
- type XMLView
- type XMLViewBlock
- type XMLViewLine
Examples ¶
Constants ¶
const SchemaVersion = "1"
SchemaVersion 标识 JSON 报告格式,与工具发行版本相互独立。
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Comparison ¶
type Comparison struct {
PDFStructure *PDFStructureResult `json:"pdf_structure,omitempty"`
AllChecksEqual *bool `json:"all_checks_equal,omitempty"`
Target int `json:"target"`
Equal bool `json:"equal"`
PagesEqual bool `json:"pages_equal"`
Summary Summary `json:"summary"`
DirectoriesAdded []string `json:"directories_added"`
DirectoriesRemoved []string `json:"directories_removed"`
Files []FileChange `json:"files"`
Pages []PageChange `json:"pages"`
// CoverageWarnings 说明 PDF 比较未覆盖的内容,不改变当前规则下的相同判定。
CoverageWarnings []string `json:"coverage_warnings,omitempty"`
}
type DocumentInfo ¶
type Environment ¶ added in v0.2.4
type Environment struct {
Platform string `json:"platform"`
GoVersion string `json:"go_version"`
BuildRevision string `json:"build_revision,omitempty"`
BuildModified *bool `json:"build_modified,omitempty"`
Renderers map[string]string `json:"renderers"`
Tools map[string]string `json:"tools"`
Fonts FontEnvironment `json:"fonts"`
}
Environment 保存本次比较的工具版本及字体目录信息,供各格式报告复现环境。 字体清单描述 Fontconfig 可见的字体,不表示每页实际使用的字体。
type FileChange ¶
type FileChange struct {
Status string `json:"status"`
Before *File `json:"before,omitempty"`
After *File `json:"after,omitempty"`
XMLChanges []ValueChange `json:"xml_changes,omitempty"`
XMLTruncated bool `json:"xml_truncated,omitempty"`
// XMLView 保存有大小限制的规范化 XML 行对照;字段差异仍由 XMLChanges 提供。
XMLView *XMLView `json:"xml_view,omitempty"`
BeforePreview string `json:"before_preview,omitempty"`
AfterPreview string `json:"after_preview,omitempty"`
}
type FontEnvironment ¶ added in v0.2.4
type FontEnvironment struct {
Source string `json:"source"`
Status string `json:"status"`
CatalogSHA256 string `json:"catalog_sha256,omitempty"`
Catalog []FontFace `json:"catalog,omitempty"`
Note string `json:"note"`
}
FontEnvironment 的摘要只覆盖按顺序排列的家族与样式,不包含字体文件字节。
type IgnoreRegion ¶ added in v0.2.4
type IgnoreRegion struct {
Page int `json:"page"`
X float64 `json:"x_mm"`
Y float64 `json:"y_mm"`
Width float64 `json:"width_mm"`
Height float64 `json:"height_mm"`
}
IgnoreRegion 指定忽略区域。Page 为 0 时应用于所有页,否则为原始页码。
type Options ¶
type Options struct {
// Language selects report language (zh-CN or en); unsupported tags fall back to zh-CN.
Language string `json:"language,omitempty"`
// Baseline 是从 0 开始的输入索引,其他文件分别与该基准比较。
Baseline int `json:"baseline_index"`
// PageMatch 为 auto(默认)或 number。自动匹配优先关联相同页面内容或
// 唯一的相同非空文字,再结合定位点和文字相似度配对修改页。
PageMatch string `json:"page_match"`
// PageMap 手工指定各目标相对基准的原始页码,例如 "2:3,4:5"。
// 两侧页码不能重复,必须在所选范围内;仅适用于 auto 匹配。
PageMap string `json:"page_map,omitempty"`
// Pages 指定各输入中从 1 开始的全局页码,例如 "1-3,5"。
// 空值选择全部页面;包内文件对比始终覆盖所有条目。
Pages string `json:"pages,omitempty"`
// Visual 启用像素对比及页面预览,默认内嵌图片。
Visual bool `json:"visual"`
// DPI 默认为 72;PixelTolerance 是每个颜色通道允许的差值,范围为 0–255。
DPI float64 `json:"dpi"`
PixelTolerance int `json:"pixel_tolerance"`
// MinRegionPixels 过滤较小的视觉区域,不改变原始像素统计和相同判定。
// 零值按 1 处理,即保留所有变化区域。
MinRegionPixels int `json:"min_region_pixels"`
// MaxXMLBytes 限制每个 XML 的结构分析大小,默认为 8 MiB。超过上限时
// 仍计算文件摘要,并明确记录未进行结构比较。
MaxXMLBytes int64 `json:"max_xml_bytes"`
// MaxXMLStructureBytes 限制单个 XML 字段表的估算大小,默认 32 MiB。
// 每次只保留当前文件两侧的字段表,比较后释放。
MaxXMLStructureBytes int64 `json:"max_xml_structure_bytes"`
// MaxChanges 限制每个 XML 的差异明细条数,默认为 200。
MaxChanges int `json:"max_changes"`
// MaxPagePixels 和 MaxTotalPixels 限制单页与累计视觉工作量,默认分别为 1000 万和 10 亿。
MaxPagePixels int64 `json:"max_page_pixels"`
MaxTotalPixels int64 `json:"max_total_pixels"`
// MaxPreviewBytes 限制所有预览图累计编码后的 PNG 字节数,默认 64 MiB。
// MaxReportBytes 限制保留的报告数据和最终输出字节数,默认 256 MiB。
MaxPreviewBytes int64 `json:"max_preview_bytes"`
MaxReportBytes int64 `json:"max_report_bytes"`
// BaselineCacheBytes 限制基准页 RGBA 缓存,默认 64 MiB;-1 禁用。
// 缓存命中不重复扣渲染像素预算,超过容量的单页不会缓存。
BaselineCacheBytes int64 `json:"baseline_cache_bytes"`
// ImageWriter 可将每张 PNG 写入外部存储并返回相对图片 URL。
// 返回地址必须为安全的相对路径;零值将图片以 base64 内嵌。
// 外部图片的创建和失败清理由调用方负责,写入错误会终止比较。
ImageWriter func([]byte) (string, error) `json:"-"`
// contains filtered or unexported fields
}
Options 控制对比范围。零值启用文件、结构和文字对比。
type PDFOptions ¶ added in v0.2.4
type PDFOptions struct {
Options
// Structure 检查所选页面的绘图指令及其使用的资源。
Structure bool
// PreviewUnchanged 在画面一致的配对页保存一张宽高均不超过 640 像素的基准预览。
PreviewUnchanged bool
// Profile 为 review(默认)或 regression,分别默认按内容或页序配对。
// 显式指定的 PageMatch 和 DPI 优先于模式默认值。
Profile string
// IgnoreRegions 同时过滤区域内的文字和画面,使用旋转后可见页面的毫米坐标。
IgnoreRegions []IgnoreRegion
// MaxInputBytes、MaxTextBytes 分别限制每份 PDF 和每页文字提取输出。
// 默认分别为 512 MiB 和 8 MiB;MaxPages 默认 10000。
MaxInputBytes int64
MaxTextBytes int64
MaxPages int
// TempDir 是临时快照目录的父目录,空值使用系统临时目录。
TempDir string
// OnProgress 在比较协程中同步通知当前阶段;回调应快速返回。
// 多次并行比较共用回调时,调用方负责同步共享数据。
OnProgress func(PDFProgress)
}
PDFOptions 控制 PDF 比较。零值使用审阅模式,仅比较文字与页面尺寸; Visual 为 true 时同时渲染画面。嵌入的 Options 中 XML 选项不适用于 PDF。
func NormalizePDFOptions ¶ added in v0.2.4
func NormalizePDFOptions(options PDFOptions) (PDFOptions, error)
NormalizePDFOptions 校验并补全规则,不读取输入或运行外部工具。 CLI 可在进入业务处理前调用它,区分参数错误与运行失败。
type PDFProgramDiff ¶ added in v0.2.4
type PDFProgress ¶ added in v0.2.4
PDFProgress 描述即将执行的工作。Stage 为 tools、snapshot、inspect、 text、structure、compare 或 render;Path 为原始输入路径,工具检查阶段为空。 Page 使用原始页码,0 表示整个文件;Pages 为文档总页数,未知时为 0。
type PDFResourceBinding ¶ added in v0.2.4
type PDFResourceBinding struct {
Slot string `json:"slot"`
Before string `json:"before,omitempty"`
After string `json:"after,omitempty"`
}
PDFResourceBinding 将规范化指令中的资源编号映射到两个输入的原始名称和对象引用。
type PDFResourceChange ¶ added in v0.2.4
type PDFResourceChange struct {
ValueChange
BeforeRef string `json:"before_ref,omitempty"`
AfterRef string `json:"after_ref,omitempty"`
Pages []PagePair `json:"pages"`
}
PDFResourceChange 将同一对资源中的同一字段差异关联到各引用页。
type PDFSettings ¶ added in v0.2.4
type PDFSettings struct {
Structure bool `json:"structure,omitempty"`
Profile string `json:"profile"`
TextNormalization string `json:"text_normalization"`
IgnoreRegions []IgnoreRegion `json:"ignore_regions,omitempty"`
MaxInputBytes int64 `json:"max_input_bytes"`
MaxTextBytes int64 `json:"max_text_bytes"`
MaxPages int `json:"max_pages"`
Tools map[string]string `json:"tools"`
Platform string `json:"platform"`
FontCatalogSHA256 string `json:"font_catalog_sha256,omitempty"`
FontNote string `json:"font_note"`
Warnings []string `json:"warnings,omitempty"`
}
PDFSettings 保存实际生效的规则和环境,随 JSON、HTML 和文本报告输出。
type PDFSourceUse ¶ added in v0.2.4
PDFSourceUse 指向本目标报告中从 1 开始的流编号。
type PDFStreamSource ¶ added in v0.2.4
type PDFStreamSource struct {
Path string `json:"path"`
Reference string `json:"reference,omitempty"`
Filters []string `json:"filters,omitempty"`
StoredBytes int `json:"stored_bytes"`
StoredSHA256 string `json:"stored_sha256"`
PayloadBytes int `json:"payload_bytes"`
Preview string `json:"preview,omitempty"`
Truncated bool `json:"truncated,omitempty"`
Pages []int `json:"pages,omitempty"`
Side string `json:"side,omitempty"`
}
PDFStreamSource 记录存储信息。绘图流额外保留带字节转义的有界原文。
type PDFStructurePage ¶ added in v0.2.4
type PDFStructurePage struct {
Bindings []PDFResourceBinding `json:"bindings,omitempty"`
Equal bool `json:"equal"`
Fields []ValueChange `json:"fields,omitempty"`
Programs []PDFProgramDiff `json:"programs,omitempty"`
BeforeSources []PDFSourceUse `json:"before_sources,omitempty"`
AfterSources []PDFSourceUse `json:"after_sources,omitempty"`
ResourceChanges []int `json:"resource_changes,omitempty"`
Truncated bool `json:"truncated,omitempty"`
}
PDFStructurePage 的字段路径以页或 Form 的有效资源作用域为起点。
type PDFStructureResult ¶ added in v0.2.4
type PDFStructureResult struct {
// CreationDateOnly 仅在核实文件其余字节完全相同时提供。
CreationDateOnly *ValueChange `json:"creation_date_only,omitempty"`
Sources []PDFStreamSource `json:"sources,omitempty"`
ResourceChanges []PDFResourceChange `json:"resource_changes,omitempty"`
Equal bool `json:"equal"`
FileBytesEqual bool `json:"file_bytes_equal"`
ChangedPages int `json:"changed_pages"`
DetailsHidden bool `json:"details_hidden,omitempty"`
// contains filtered or unexported fields
}
PDFStructureResult 描述选中页面的结构比较与整份文件的字节比较。
type Page ¶
type Page struct {
Number int `json:"number"`
Document int `json:"document"`
PageInDocument int `json:"page_in_document"`
Width float64 `json:"width_mm"`
Height float64 `json:"height_mm"`
Text string `json:"text"`
ContentSHA256 string `json:"content_sha256"`
Rotation int `json:"rotation,omitempty"`
// Selected 标记是否属于 Options.Pages。未选中的页面只保留序号,
// 不提取文字、尺寸或内容摘要,也不参与配对和渲染。
Selected bool `json:"selected"`
}
type PageChange ¶
type PageChange struct {
PDFStructure *PDFStructurePage `json:"pdf_structure,omitempty"`
Status string `json:"status"`
Before int `json:"before,omitempty"`
After int `json:"after,omitempty"`
Moved bool `json:"moved"`
// Match 记录实际采用的配对依据,独立于页面是否发生变化。
Match PageMatchInfo `json:"match"`
TextChanged bool `json:"text_changed"`
SizeChanged bool `json:"size_changed"`
TextDiff []TextSpan `json:"text_diff,omitempty"`
TextDiffCoarse bool `json:"text_diff_coarse,omitempty"`
Visual *VisualChange `json:"visual,omitempty"`
// TextRegions 定位 PDF 中发生文字变化的词,坐标单位为毫米。
TextRegions []TextRegion `json:"text_regions,omitempty"`
}
type PageMatchInfo ¶
type PageMatchInfo struct {
Method string `json:"method"`
// TextSimilarity 仅用于 text_similarity,范围为 0–1,是文字特征相似度,
// 不代表页面相同的概率或视觉相似度。
TextSimilarity float64 `json:"text_similarity,omitempty"`
// SearchLimited 表示相似度搜索达到工作量上限;只标记随后顺序配对或未匹配的页面。
SearchLimited bool `json:"search_limited,omitempty"`
}
PageMatchInfo 描述配对依据:manual、content、text、text_similarity、 sequence(自动匹配的顺序兜底)、number(显式顺序匹配)、unmatched。
Example ¶
package main
import (
"context"
"fmt"
"github.com/zc310/ofd/pkg/compare"
)
func main() {
file := "../../testdata/ofd-creator/helloworld.ofd"
report, err := compare.Files(context.Background(), []string{file, file}, compare.Options{})
if err != nil {
panic(err)
}
fmt.Println(report.Comparisons[0].Pages[0].Match.Method)
}
Output: content
type PagePair ¶
PagePair 指定基准和目标中从 1 开始的原始全局页码。
func ParsePageMap ¶
ParsePageMap 解析 "2:3,4:5" 格式的手工页映射,拒绝重复或无效页码。
Example ¶
package main
import (
"fmt"
"github.com/zc310/ofd/pkg/compare"
)
func main() {
pairs, err := compare.ParsePageMap("2:3,4:5")
if err != nil {
panic(err)
}
for _, pair := range pairs {
fmt.Printf("%d → %d\n", pair.Before, pair.After)
}
}
Output: 2 → 3 4 → 5
type PageRange ¶
type PageRange struct{ First, Last int }
PageRange 是从 1 开始的全局页码闭区间。
func ParsePageRanges ¶
ParsePageRanges 将 "1-3,5" 解析为排序并合并后的区间,空值表示全部页面。 区间不展开为逐页列表,避免极大端点造成无界分配或页码循环溢出。
type Report ¶
type Report struct {
CrossFormat bool `json:"cross_format,omitempty"`
SchemaVersion string `json:"schema_version"`
ToolVersion string `json:"tool_version"`
Baseline int `json:"baseline"`
Options Options `json:"options"`
Inputs []Input `json:"inputs"`
Comparisons []Comparison `json:"comparisons"`
// Environment 记录 OFD、PDF 和跨格式比较实际使用的环境。
Environment *Environment `json:"environment,omitempty"`
// PDF 仅在 PDFFiles 生成的报告中存在,记录模式和实际比较环境。
PDF *PDFSettings `json:"pdf,omitempty"`
}
Report 记录各目标相对 Inputs[Baseline-1] 的差异。报告中的输入和页面序号 从 1 开始;页面或文件在一侧不存在时,省略对应字段。
func DocumentPair ¶ added in v0.2.4
func DocumentPair(ctx context.Context, baseline, target string, progress func(PDFProgress)) (*Report, error)
DocumentPair 按页序以 144 DPI 比较两份 OFD 或 PDF,并生成可离线审阅的预览。 同格式检查对应的内部结构,跨格式只比较可见页面和提取文字。 每份输入先复制为独立快照,后续源文件更新不影响已复制的输入。
func Files ¶ added in v0.2.4
Files 对比两份或多份本地 OFD 文件,源文件只读。 每份输入先复制为独立临时快照,解析、包内摘要、XML 和渲染均读取该快照。 输入、解析、渲染或取消失败均返回错误;选中页面不会因失败而被静默跳过。
Example ¶
package main
import (
"context"
"fmt"
"github.com/zc310/ofd/pkg/compare"
)
func main() {
file := "../../testdata/ofd-creator/helloworld.ofd"
report, err := compare.Files(context.Background(), []string{file, file}, compare.Options{})
if err != nil {
panic(err)
}
fmt.Println(report.Comparisons[0].Equal)
}
Output: true
type Summary ¶
type Summary struct {
FilesAdded int `json:"files_added"`
FilesRemoved int `json:"files_removed"`
FilesModified int `json:"files_modified"`
FilesMoved int `json:"files_moved"`
PagesAdded int `json:"pages_added"`
PagesRemoved int `json:"pages_removed"`
PagesModified int `json:"pages_modified"`
PagesMoved int `json:"pages_moved"`
VisualPagesChanged int `json:"visual_pages_changed"`
}
type TextRegion ¶ added in v0.2.4
TextRegion 标出变化词的完整边界;Side 为 before 或 after,坐标单位为毫米。
type TextSpan ¶
TextSpan 描述按 Unicode 字符计算的编辑操作:equal 为共同文字,delete 只属于基准,insert 只属于目标。拼接 equal+delete 可还原基准原文, 拼接 equal+insert 可还原目标原文,空白字符完整保留。
type ValueChange ¶
type ValueChange struct {
Path string `json:"path"`
Before *string `json:"before,omitempty"`
After *string `json:"after,omitempty"`
}
ValueChange 使用命名空间展开名标识 XML 元素或属性。 nil 表示该值不存在,非 nil 的空字符串表示值存在但为空。
type VisualChange ¶
type VisualChange struct {
ChangedPixels int64 `json:"changed_pixels"`
TotalPixels int64 `json:"total_pixels"`
Ratio float64 `json:"ratio"`
// Bounds 是变化像素的整体矩形,采用半开区间 [x0,y0,x1,y1)。
Bounds [4]int `json:"bounds"`
BeforePNG string `json:"before_png,omitempty"`
AfterPNG string `json:"after_png,omitempty"`
DiffPNG string `json:"diff_png,omitempty"`
Width int `json:"width"`
Height int `json:"height"`
Regions []VisualRegion `json:"regions,omitempty"`
RegionCount int `json:"region_count"`
RegionsTruncated bool `json:"regions_truncated,omitempty"`
IgnoredRegionPixels int64 `json:"ignored_region_pixels"`
}
type VisualRegion ¶
type VisualRegion struct {
Bounds [4]int `json:"bounds"`
ChangedPixels int64 `json:"changed_pixels"`
}
VisualRegion 聚合相邻的变化网格。Bounds 以页面像素坐标覆盖这些网格, ChangedPixels 保存实际变化像素数量。
type XMLView ¶
type XMLView struct {
Kind string `json:"kind"`
Blocks []XMLViewBlock `json:"blocks,omitempty"`
Coarse bool `json:"coarse,omitempty"`
Note string `json:"note,omitempty"`
}
XMLView 区分 format(仅格式变化)、structure、added、removed、unavailable。 规范化显示统一前缀、属性顺序和缩进,忽略注释;行号属于显示内容。
type XMLViewBlock ¶
type XMLViewBlock struct {
Equal bool `json:"equal"`
Folded bool `json:"folded"`
Lines []XMLViewLine `json:"lines"`
}
XMLViewBlock 将相邻的相同行或变化行聚合;Folded 表示默认折叠相同部分。