mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-29 17:27:08 +08:00
1 parent
84808cda61
commit
ec24b6a17d
17 files changed
+1908
-2026
No files matched your search
@@ -15,16 +15,48 @@
|
||||
package ofdgo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"math"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
basefont "github.com/xiaoqidun/ofdgo/internal/base/font"
|
||||
codecpdf "github.com/xiaoqidun/ofdgo/internal/codec/pdf"
|
||||
"github.com/xiaoqidun/pdfgo"
|
||||
)
|
||||
|
||||
// pdfImportedFont 保存单次导入共用的字体资源、度量及源字形映射,不保存文档标识
|
||||
type pdfImportedFont struct {
|
||||
resource *FontResource
|
||||
checksum [32]byte
|
||||
metrics FontMetrics
|
||||
type1Glyphs map[string]uint16
|
||||
repairLimit uint16
|
||||
}
|
||||
|
||||
// pdfTextReplacement 记录一个完整替换区段的文字数量与转换状态
|
||||
type pdfTextReplacement struct {
|
||||
text *pdfgo.ReplacementText
|
||||
count int
|
||||
matched int
|
||||
offset int64
|
||||
blocked bool
|
||||
applied bool
|
||||
identity bool
|
||||
}
|
||||
|
||||
// pdfTextReplacements 按绘制顺序收集替换区段,避免拆分或重复写入原文
|
||||
type pdfTextReplacements struct {
|
||||
importer *pdfImporter
|
||||
spans map[*pdfgo.ReplacementText]*pdfTextReplacement
|
||||
order []*pdfTextReplacement
|
||||
widget bool
|
||||
}
|
||||
|
||||
// text 保留字形及逐字基线,外部字体优先复用同名内嵌资源
|
||||
// 入参: mark PDF文字绘制信息
|
||||
// 返回: error 错误信息
|
||||
@@ -509,3 +541,463 @@ func (p *pdfImporter) disjointText(node pdfCompositeNode) (bool, error) {
|
||||
cache.textDisjoint[node.group] = true
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// shapePDFText 使用导入字体整形注释文字,将毫米坐标约定转换为PDF文字空间方向
|
||||
// 入参: ctx 取消上下文, source 源字体, text 单行原文, rightToLeft 段落方向
|
||||
// 返回: []pdfgo.ShapedGlyph 千分之一字号的定位字形, error 字体、整形或取消错误
|
||||
func (p *pdfImporter) shapePDFText(ctx context.Context, source *pdfgo.Font, text string, rightToLeft bool) ([]pdfgo.ShapedGlyph, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(source.Program) == 0 {
|
||||
return nil, &pdfgo.UnsupportedError{Feature: "annotation shaping without embedded font"}
|
||||
}
|
||||
font, err := p.importedFont(source)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
shaper, ok := font.metrics.(FontShaperOptions)
|
||||
if !ok {
|
||||
return nil, &pdfgo.UnsupportedError{Feature: "annotation text shaping backend"}
|
||||
}
|
||||
options := TextShapeOptions{Bidi: true}
|
||||
if rightToLeft {
|
||||
options.Direction = "rtl"
|
||||
}
|
||||
glyphs, err := shaper.ShapeTextWithOptions(text, 1000, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := make([]pdfgo.ShapedGlyph, len(glyphs))
|
||||
units := float64(font.metrics.UnitsPerEm())
|
||||
if units <= 0 {
|
||||
return nil, fmt.Errorf("invalid annotation font units")
|
||||
}
|
||||
for i, glyph := range glyphs {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result[i] = pdfgo.ShapedGlyph{ID: glyph.Glyph, Cluster: glyph.Cluster, VisualOrder: glyph.VisualOrder, X: glyph.X, Y: -glyph.Y, Advance: glyph.Advance, Width: float64(font.metrics.GlyphAdvance(glyph.Glyph)) * 1000 / units}
|
||||
}
|
||||
return result, ctx.Err()
|
||||
}
|
||||
|
||||
// embeddedTextFont 匹配同名内嵌字体,仅复用唯一且覆盖全部字符的字形程序
|
||||
// 入参: source 外部字体, glyphs 当前文字字形
|
||||
// 返回: *pdfImportedFont 可复用字体,无可靠匹配时为空
|
||||
func (p *pdfImporter) embeddedTextFont(source *pdfgo.Font, glyphs []pdfgo.Glyph) *pdfImportedFont {
|
||||
name := codecpdf.EmbeddedFontName(source.Name)
|
||||
if name == "" || len(glyphs) == 0 {
|
||||
return nil
|
||||
}
|
||||
var matched *pdfImportedFont
|
||||
for _, imported := range p.fonts {
|
||||
if codecpdf.EmbeddedFontName(imported.resource.Font.FontName) != name {
|
||||
continue
|
||||
}
|
||||
if matched != nil && matched.checksum != imported.checksum {
|
||||
return nil
|
||||
}
|
||||
matched = imported
|
||||
}
|
||||
if matched == nil {
|
||||
return nil
|
||||
}
|
||||
for _, glyph := range glyphs {
|
||||
if glyph.Name == ".notdef" || utf8.RuneCountInString(glyph.Text) != 1 {
|
||||
return nil
|
||||
}
|
||||
char, _ := utf8.DecodeRuneInString(glyph.Text)
|
||||
id := matched.metrics.GlyphIndex(char)
|
||||
if id == 0 || id >= matched.metrics.NumGlyphs() || matched.repairLimit != 0 && id >= matched.repairLimit {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return matched
|
||||
}
|
||||
|
||||
// importedFont 按原PDF字体复用包装和解析结果,失败结果不进入缓存
|
||||
// 入参: source 源字体
|
||||
// 返回: *pdfImportedFont 只读导入字体, error 字体程序或后端错误
|
||||
func (p *pdfImporter) importedFont(source *pdfgo.Font) (*pdfImportedFont, error) {
|
||||
if cached := p.fonts[source]; cached != nil {
|
||||
return cached, nil
|
||||
}
|
||||
var type1 *basefont.Type1Program
|
||||
result := &pdfImportedFont{}
|
||||
if source.ProgramType == "FontFile" {
|
||||
parsed, err := basefont.ParseType1Program(source.Program)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("PDF font %s program: %w", source.Name, err)
|
||||
}
|
||||
type1 = &parsed
|
||||
result.type1Glyphs = parsed.GlyphIDs()
|
||||
}
|
||||
program, repair, err := codecpdf.FontProgram(source, type1)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("PDF font %s program: %w", source.Name, err)
|
||||
}
|
||||
if repair.Locations && p.warning == nil {
|
||||
return nil, fmt.Errorf("PDF font %s program: %w", source.Name, basefont.ErrTTInvalidLocations)
|
||||
}
|
||||
if p.editor.backends.FontResources == nil {
|
||||
return nil, fmt.Errorf("PDF font %s resource: font resources: %w", source.Name, ErrBackendUnavailable)
|
||||
}
|
||||
result.resource, err = p.editor.backends.FontResources.OpenFontResource(FontFile{Name: source.Name + ".ttf", Data: program}, 0)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("PDF font %s resource: %w", source.Name, err)
|
||||
}
|
||||
if p.editor.backends.Fonts == nil {
|
||||
return nil, fmt.Errorf("PDF font backend unavailable")
|
||||
}
|
||||
result.metrics, err = p.editor.backends.Fonts.OpenFont(result.resource.Data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("PDF font %s metrics: %w", source.Name, err)
|
||||
}
|
||||
if _, ok := result.metrics.(FontOutlines); !ok {
|
||||
return nil, fmt.Errorf("PDF font backend does not provide glyph outlines")
|
||||
}
|
||||
result.checksum, result.repairLimit = sha256.Sum256(result.resource.Data), repair.MetricsLimit
|
||||
if repair.Locations {
|
||||
p.warning(pdfgo.Diagnostic{Message: fmt.Sprintf("PDF font %s has invalid glyph locations; index rebuilt with original glyph IDs", source.Name)})
|
||||
}
|
||||
if p.fonts == nil {
|
||||
p.fonts = make(map[*pdfgo.Font]*pdfImportedFont)
|
||||
}
|
||||
p.fonts[source] = result
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// symbolFont 加载标准符号字体,保留原字形编号和源字形名称
|
||||
// 入参: source 未嵌入的PDF字体
|
||||
// 返回: *pdfImportedFont 可用的符号字体,无匹配时为空, error 字体或取消错误
|
||||
func (p *pdfImporter) symbolFont(source *pdfgo.Font) (*pdfImportedFont, error) {
|
||||
name := codecpdf.EmbeddedFontName(source.Name)
|
||||
if source.Subtype != "Type1" || name != "Symbol" && name != "ZapfDingbats" {
|
||||
return nil, nil
|
||||
}
|
||||
if imported, ok := p.symbolFonts[source]; ok {
|
||||
return imported, nil
|
||||
}
|
||||
if p.symbolFonts == nil {
|
||||
p.symbolFonts = make(map[*pdfgo.Font]*pdfImportedFont)
|
||||
}
|
||||
if err := p.ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
definition := &Font{ID: name, FontName: name, FamilyName: name}
|
||||
reader := &Reader{doc: &Document{}, fontResourcesRead: true, fontCache: map[string]*Font{name: definition}}
|
||||
resolved, err := p.editor.newRenderer(reader).ResolveFont(name, true)
|
||||
if err != nil || len(resolved.Data) == 0 || p.editor.backends.FontResources == nil {
|
||||
p.symbolFonts[source] = nil
|
||||
return nil, nil
|
||||
}
|
||||
program, names, err := basefont.SymbolProgram(p.ctx, resolved.Data, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(names) == 0 {
|
||||
p.symbolFonts[source] = nil
|
||||
return nil, nil
|
||||
}
|
||||
resource, err := p.editor.backends.FontResources.OpenFontResource(FontFile{Name: name + ".ttf", Data: program}, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
metrics, err := p.editor.backends.Fonts.OpenFont(resource.Data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, ok := metrics.(FontOutlines); !ok {
|
||||
p.symbolFonts[source] = nil
|
||||
return nil, nil
|
||||
}
|
||||
result := &pdfImportedFont{resource: resource, checksum: sha256.Sum256(resource.Data), metrics: metrics, type1Glyphs: names}
|
||||
p.symbolFonts[source] = result
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// span 查找最外层替换区段,相邻区段即使原文相同也分别保留
|
||||
// 入参: text 替换文本
|
||||
// 返回: *pdfTextReplacement 区段状态,未声明时为空
|
||||
func (r *pdfTextReplacements) span(text *pdfgo.ReplacementText) *pdfTextReplacement {
|
||||
if text == nil {
|
||||
return nil
|
||||
}
|
||||
if r.spans == nil {
|
||||
r.spans = make(map[*pdfgo.ReplacementText]*pdfTextReplacement)
|
||||
}
|
||||
first := text
|
||||
for text.Parent != nil && r.spans[text] == nil {
|
||||
text = text.Parent
|
||||
}
|
||||
span := r.spans[text]
|
||||
if span == nil {
|
||||
span = &pdfTextReplacement{text: text}
|
||||
r.spans[text] = span
|
||||
r.order = append(r.order, span)
|
||||
}
|
||||
for first != text {
|
||||
r.spans[first] = span
|
||||
first = first.Parent
|
||||
}
|
||||
return span
|
||||
}
|
||||
|
||||
// block 标记无法仅用字符与字形变换表达的区段
|
||||
// 入参: text 替换文本
|
||||
func (r *pdfTextReplacements) block(text *pdfgo.ReplacementText) {
|
||||
if span := r.span(text); span != nil {
|
||||
span.blocked = true
|
||||
}
|
||||
}
|
||||
|
||||
// markedContent 处理结构区段、可选内容及替换文本属性
|
||||
// 入参: mark 内容标记
|
||||
// 返回: error 未支持的语义错误
|
||||
func (r *pdfTextReplacements) markedContent(mark pdfgo.MarkedContentMark) error {
|
||||
if mark.Operator == "EMC" {
|
||||
return nil
|
||||
}
|
||||
if r.importer.structureBindings != nil {
|
||||
if err := r.importer.prepareSourceStructure(r.importer.ctx, mark.Reader); err != nil {
|
||||
return err
|
||||
}
|
||||
r.importer.structureBindings.observe(mark.Sequence)
|
||||
if mark.Sequence != nil && r.importer.structureBindings.marked != nil {
|
||||
key, ok, err := pdfStructureContentKey(mark.Sequence)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ok && r.importer.structureBindings.marked[key] {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if mark.Tag == "OC" {
|
||||
kind, err := mark.Reader.Resolve(mark.Properties["Type"])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if kind == pdfgo.Name("OCG") || kind == pdfgo.Name("OCMD") {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if r.widget && mark.Tag == "Tx" && len(mark.Properties) == 0 {
|
||||
return nil
|
||||
}
|
||||
if mark.Replacement != nil && len(mark.Properties) == 1 {
|
||||
return nil
|
||||
}
|
||||
if r.importer.warning == nil {
|
||||
return &pdfgo.UnsupportedError{Feature: "marked content requiring semantic preservation"}
|
||||
}
|
||||
r.importer.warning(pdfgo.Diagnostic{Offset: mark.Offset, Message: "marked content " + string(mark.Tag) + " not preserved"})
|
||||
return nil
|
||||
}
|
||||
|
||||
// apply 合并可等价表示的替换区段,原文相同但样式不同的区段保留原图元
|
||||
// 入参: nodes 原始图元
|
||||
// 返回: []pdfCompositeNode 转换图元, error 取消或语义转换错误
|
||||
func (r *pdfTextReplacements) apply(nodes []pdfCompositeNode) ([]pdfCompositeNode, error) {
|
||||
if len(r.order) == 0 {
|
||||
return nodes, nil
|
||||
}
|
||||
if err := r.count(nodes, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, span := range r.order {
|
||||
span.identity = !span.blocked && span.count > 0 && span.matched == len(span.text.Text)
|
||||
}
|
||||
var err error
|
||||
nodes, err = r.merge(nodes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, span := range r.order {
|
||||
if span.applied || span.identity || !span.blocked && span.count == 0 && span.text.Text == "" {
|
||||
continue
|
||||
}
|
||||
if r.importer.warning == nil {
|
||||
return nil, &pdfgo.UnsupportedError{Feature: "ActualText span without equivalent OFD glyph mapping"}
|
||||
}
|
||||
r.importer.warning(pdfgo.Diagnostic{Offset: span.offset, Message: "PDF ActualText span not preserved; original appearance retained"})
|
||||
}
|
||||
r.clear(nodes)
|
||||
return nodes, nil
|
||||
}
|
||||
|
||||
// count 统计区段文字并逐字核对原文,排除需要组级合成的内容
|
||||
// 入参: nodes 图元, blocked 上层组是否阻止直接转换
|
||||
// 返回: error 取消错误
|
||||
func (r *pdfTextReplacements) count(nodes []pdfCompositeNode, blocked bool) error {
|
||||
for _, node := range nodes {
|
||||
if err := r.importer.ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if node.text != nil {
|
||||
if span := r.span(node.text.Replacement); span != nil {
|
||||
span.count++
|
||||
span.blocked = span.blocked || blocked
|
||||
for i, glyph := range node.text.Glyphs {
|
||||
if span.matched < 0 {
|
||||
break
|
||||
}
|
||||
if i%256 == 0 {
|
||||
if err := r.importer.ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if glyph.Text == "" || !strings.HasPrefix(span.text.Text[span.matched:], glyph.Text) {
|
||||
span.matched = -1
|
||||
break
|
||||
}
|
||||
span.matched += len(glyph.Text)
|
||||
}
|
||||
}
|
||||
}
|
||||
childBlocked := blocked
|
||||
if group := node.group; group != nil && node.textObject == nil {
|
||||
opaque, err := r.importer.opaque(node, group.ColorSpace)
|
||||
if err := r.importer.ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
childBlocked = childBlocked || !opaque || err != nil || group.ColorSpace != nil && !group.ColorSpace.SRGBEquivalent()
|
||||
}
|
||||
if err := r.count(node.children, childBlocked); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// merge 合并同层连续图元,保留原始绘制顺序与字形坐标
|
||||
// 入参: nodes 图元
|
||||
// 返回: []pdfCompositeNode 合并结果, error 取消错误
|
||||
func (r *pdfTextReplacements) merge(nodes []pdfCompositeNode) ([]pdfCompositeNode, error) {
|
||||
result := nodes[:0]
|
||||
for i := 0; i < len(nodes); {
|
||||
if err := r.importer.ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node := nodes[i]
|
||||
var err error
|
||||
node.children, err = r.merge(node.children)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
end := i + 1
|
||||
combined := false
|
||||
first := node.text
|
||||
if first == nil && node.textObject != nil && len(node.children) != 0 {
|
||||
first = node.children[0].text
|
||||
}
|
||||
if first != nil {
|
||||
if span := r.span(first.Replacement); span != nil && !span.applied && !span.blocked && span.text.Text != "" && !(span.identity && span.count == 1) {
|
||||
var marks []*pdfgo.TextMark
|
||||
for end = i; end < len(nodes); end++ {
|
||||
current := nodes[end]
|
||||
children := current.children
|
||||
if current.text != nil {
|
||||
children = nodes[end : end+1]
|
||||
} else if current.textObject == nil {
|
||||
break
|
||||
}
|
||||
valid := len(children) != 0
|
||||
for _, child := range children {
|
||||
if child.text == nil || r.span(child.text.Replacement) != span {
|
||||
valid = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if !valid {
|
||||
break
|
||||
}
|
||||
for _, child := range children {
|
||||
marks = append(marks, child.text)
|
||||
}
|
||||
}
|
||||
end = max(end, i+1)
|
||||
if len(marks) == span.count {
|
||||
if mark, ok := codecpdf.MergeReplacement(marks, span.text); ok {
|
||||
var origins pdfOriginSet
|
||||
for _, source := range marks {
|
||||
if err := r.importer.ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
origin := r.importer.textOrigin(*source)
|
||||
origins.add(origin.sequence)
|
||||
for _, sequence := range origin.related {
|
||||
origins.add(sequence)
|
||||
}
|
||||
}
|
||||
if len(origins.origin.related) != 0 {
|
||||
if r.importer.textOrigins == nil {
|
||||
r.importer.textOrigins = make(map[*pdfgo.ReplacementText]pdfObjectOrigin)
|
||||
}
|
||||
r.importer.textOrigins[span.text] = origins.origin
|
||||
}
|
||||
if node.text != nil {
|
||||
node.text = mark
|
||||
} else {
|
||||
node.children = []pdfCompositeNode{{text: mark}}
|
||||
}
|
||||
span.applied = true
|
||||
combined = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
node.formCount = 0
|
||||
result = append(result, node)
|
||||
if !combined {
|
||||
for _, remaining := range nodes[i+1 : end] {
|
||||
remaining.formCount = 0
|
||||
result = append(result, remaining)
|
||||
}
|
||||
}
|
||||
i = end
|
||||
}
|
||||
clear(nodes[len(result):])
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// clear 清除无需替换及未转换的区段引用,防止局部绘制重复使用整段原文
|
||||
// 入参: nodes 图元
|
||||
func (r *pdfTextReplacements) clear(nodes []pdfCompositeNode) {
|
||||
for _, node := range nodes {
|
||||
if node.text != nil {
|
||||
if span := r.span(node.text.Replacement); span != nil && !span.applied {
|
||||
node.text.Replacement = nil
|
||||
}
|
||||
}
|
||||
r.clear(node.children)
|
||||
}
|
||||
}
|
||||
|
||||
// textOrigin 获取合并文字的完整来源,普通文字沿用原始区段
|
||||
// 入参: mark 文字图元
|
||||
// 返回: pdfObjectOrigin 来源区段
|
||||
func (p *pdfImporter) textOrigin(mark pdfgo.TextMark) pdfObjectOrigin {
|
||||
if origin, ok := p.textOrigins[mark.Replacement]; ok {
|
||||
return origin
|
||||
}
|
||||
return pdfObjectOrigin{sequence: mark.Sequence}
|
||||
}
|
||||
|
||||
// pdfApplyReplacement 将整段原文映射到全部字形,字形位移遵循GB/T 33190的文字定位规则
|
||||
// 入参: object 文字对象, text 整段原文, positions 原始字形位置
|
||||
func pdfApplyReplacement(object *TextObject, text string, positions []pdfgo.Point) {
|
||||
const unit = 25.4 / 72
|
||||
glyphs := make([]string, len(object.CGTransform))
|
||||
for i, transform := range object.CGTransform {
|
||||
glyphs[i] = transform.Glyphs
|
||||
}
|
||||
dx, dy := make([]float64, len(positions)-1), make([]float64, len(positions)-1)
|
||||
for i := 1; i < len(positions); i++ {
|
||||
dx[i-1] = (positions[i].X - positions[i-1].X) * unit
|
||||
dy[i-1] = -(positions[i].Y - positions[i-1].Y) * unit
|
||||
}
|
||||
object.TextCode = []TextCode{{X: pdfNumbers(positions[0].X * unit), Y: pdfNumbers(-positions[0].Y * unit), DeltaX: pdfNumbers(dx...), DeltaY: pdfNumbers(dy...), Value: escapeOFDText(text)}}
|
||||
object.CGTransform = []CGTransform{{CodeCount: utf8.RuneCountInString(text), GlyphCount: len(glyphs), Glyphs: strings.Join(glyphs, " ")}}
|
||||
}
|
||||
Reference in new issue
Block a user