feat(资源定位): 优化资源定位与原文汇总

This commit is contained in:
xiaoqidun committed 2026-10-13 20:14:15 +08:00
1 parent 2db0cbcb95
commit e4d38eb289
2 files changed
+28 -11

No files matched your search

+1 -1
View File
@@ -151,7 +151,7 @@ func (r *Renderer) ResourceLocations(ctx context.Context, kind, id string, refer
for id := range wanted { for id := range wanted {
targets[id] = &StructureTarget{ID: id} targets[id] = &StructureTarget{ID: id}
} }
visitor := &structureVisitor{ctx: ctx, renderer: &view, targets: targets} visitor := &structureVisitor{ctx: ctx, renderer: &view, targets: targets, boundsOnly: true}
if err := view.WalkPage(content, visitor); err != nil { if err := view.WalkPage(content, visitor); err != nil {
return nil, err return nil, err
} }
+27 -10
View File
@@ -14,14 +14,19 @@
package ofdgo package ofdgo
import "context" import (
"context"
"strings"
)
// structureVisitor 按引用收集图元及复合图元成员的原文和页面范围 // structureVisitor 按引用收集图元及复合图元成员的原文和页面范围
type structureVisitor struct { type structureVisitor struct {
ctx context.Context ctx context.Context
renderer *Renderer renderer *Renderer
targets map[string]*StructureTarget targets map[string]*StructureTarget
stack []*StructureTarget stack []*StructureTarget
text map[*StructureTarget]*strings.Builder
boundsOnly bool
} }
// BeginObject 记录当前图元的标引目标,使复合图元引用覆盖其成员 // BeginObject 记录当前图元的标引目标,使复合图元引用覆盖其成员
@@ -58,7 +63,7 @@ func (v *structureVisitor) IncludeHidden() bool { return true }
// 返回: bool 始终为false // 返回: bool 始终为false
func (v *structureVisitor) SkipClips() bool { return false } func (v *structureVisitor) SkipClips() bool { return false }
// DrawObject 按继承变换和裁剪度量被引用的图元,不解码图片像素 // DrawObject 按继承变换和裁剪度量被引用的图元,按需汇总原文,不解码图片像素
// 入参: object 页面图元, state 继承状态 // 入参: object 页面图元, state 继承状态
// 返回: error 度量或取消错误 // 返回: error 度量或取消错误
func (v *structureVisitor) DrawObject(object *GraphicObject, state RenderState) error { func (v *structureVisitor) DrawObject(object *GraphicObject, state RenderState) error {
@@ -81,7 +86,7 @@ func (v *structureVisitor) DrawObject(object *GraphicObject, state RenderState)
box = result.Bounds box = result.Bounds
} }
var text string var text string
if object.Type == "TextObject" { if !v.boundsOnly && object.Type == "TextObject" {
text = object.TextObject.Text() text = object.TextObject.Text()
} }
for _, target := range v.stack { for _, target := range v.stack {
@@ -90,10 +95,22 @@ func (v *structureVisitor) DrawObject(object *GraphicObject, state RenderState)
} }
target.Bounds = unionTextBox(target.Bounds, box) target.Bounds = unionTextBox(target.Bounds, box)
if text != "" { if text != "" {
if target.Text != "" { if target.Text == "" {
target.Text += "\n" target.Text = text
continue
} }
target.Text += text if v.text == nil {
v.text = make(map[*StructureTarget]*strings.Builder)
}
builder := v.text[target]
if builder == nil {
builder = new(strings.Builder)
builder.WriteString(target.Text)
v.text[target] = builder
}
builder.WriteByte('\n')
builder.WriteString(text)
target.Text = builder.String()
} }
} }
return nil return nil