diff --git a/ofdgo_compression_fonts.go b/ofdgo_compression_fonts.go index b6361c0..e30cc36 100644 --- a/ofdgo_compression_fonts.go +++ b/ofdgo_compression_fonts.go @@ -21,9 +21,9 @@ import ( ) // compressionFontUsage 按文档合并共享字体用字,版本编辑仅统计选中入口 -// 入参: ctx 取消上下文, parts 输出改动, generated 自产页面引用, documents 文档入口, resources 资源索引, references 各文件引用, read 输出文件读取方法 +// 入参: ctx 取消上下文, documents 文档入口, resources 资源索引, references 各文件引用, read 输出文件读取方法 // 返回: map[string]*editorFontUsage 各字体文件的用字,无法确定作用域时为空, error 读取或取消错误 -func (r *Reader) compressionFontUsage(ctx context.Context, parts map[string][]byte, generated map[string]editorGeneratedReferences, documents map[string]bool, resources []string, references map[string]*editorResourceRefs, read func(string) ([]byte, error)) (map[string]*editorFontUsage, error) { +func (r *Reader) compressionFontUsage(ctx context.Context, documents map[string]bool, resources []string, references map[string]*editorResourceRefs, read func(string) ([]byte, error)) (map[string]*editorFontUsage, error) { selected := "" if r.OFD.DocBody[r.documentIndex].versioned { var err error @@ -32,7 +32,7 @@ func (r *Reader) compressionFontUsage(ctx context.Context, parts map[string][]by return nil, err } } - scoped, err := r.resourceReferenceScopes(ctx, parts, generated, documents, resources, references, selected) + scoped, err := resourceReferenceScopes(ctx, documents, resources, references, selected) if err != nil || scoped == nil { return nil, err } diff --git a/ofdgo_compression_resources.go b/ofdgo_compression_resources.go index 4abded0..fed45bf 100644 --- a/ofdgo_compression_resources.go +++ b/ofdgo_compression_resources.go @@ -92,7 +92,7 @@ func (e *Editor) compressResourceReferences(parts map[string][]byte, reader *Rea } } collect := reader != nil && reader.DocumentCount() > 1 && e.backends.FontResources != nil - refs, resources, fontReferences, safe, err := scanEditorResources(e.output.ctx, reader, parts, generated, names, nil, collect) + refs, resources, references, safe, err := scanEditorResources(e.output.ctx, reader, parts, generated, names, nil, collect) if err != nil || !safe { return err } @@ -143,8 +143,8 @@ func (e *Editor) compressResourceReferences(parts map[string][]byte, reader *Rea } } var fontUsage map[string]*editorFontUsage - if fontReferences != nil { - fontUsage, err = reader.compressionFontUsage(e.output.ctx, parts, generated, refs.documents, resources, fontReferences, read) + if references != nil { + fontUsage, err = reader.compressionFontUsage(e.output.ctx, refs.documents, resources, references, read) if err != nil { return err } diff --git a/ofdgo_editor_resource_scan.go b/ofdgo_editor_resource_scan.go index 1739292..88141c9 100644 --- a/ofdgo_editor_resource_scan.go +++ b/ofdgo_editor_resource_scan.go @@ -133,7 +133,7 @@ func scanEditorResources(ctx context.Context, reader *Reader, parts map[string][ if resource { resources = append(resources, name) } - if collect && (resource || len(local.ids) != 0 || len(local.fonts) != 0) { + if collect { references[name] = local } } diff --git a/ofdgo_editor_resource_scope.go b/ofdgo_editor_resource_scope.go index 0079e02..c866886 100644 --- a/ofdgo_editor_resource_scope.go +++ b/ofdgo_editor_resource_scope.go @@ -20,10 +20,10 @@ import ( "slices" ) -// resourceReferenceScopes 按文档归集资源标识和字体用字,同文档索引共用只读结果,跨文档索引独立合并 -// 入参: ctx 取消上下文, parts 输出改动, generated 自产页面引用, documents 文档入口, resources 资源索引, references 文件引用, selected 限定的文档入口,空值统计全部文档 -// 返回: map[string]*editorResourceRefs 各资源索引的引用,存在无法归属的引用时为空, error 读取或取消错误 -func (r *Reader) resourceReferenceScopes(ctx context.Context, parts map[string][]byte, generated map[string]editorGeneratedReferences, documents map[string]bool, resources []string, references map[string]*editorResourceRefs, selected string) (map[string]*editorResourceRefs, error) { +// resourceReferenceScopes 复用结构依赖归集文档引用,同文档索引共用只读结果,跨文档索引独立合并 +// 入参: ctx 取消上下文, documents 文档入口, resources 资源索引, references 文件引用, selected 限定的文档入口,空值统计全部文档 +// 返回: map[string]*editorResourceRefs 各资源索引的引用,依赖不完整或引用无法归属时为空, error 取消错误 +func resourceReferenceScopes(ctx context.Context, documents map[string]bool, resources []string, references map[string]*editorResourceRefs, selected string) (map[string]*editorResourceRefs, error) { indexes := make(map[string]bool, len(resources)) for _, name := range resources { indexes[name] = true @@ -32,19 +32,26 @@ func (r *Reader) resourceReferenceScopes(ctx context.Context, parts map[string][ result := make(map[string]*editorResourceRefs) shared := make(map[string]bool) for _, entry := range slices.Sorted(maps.Keys(documents)) { - scope, err := r.versionFileDependencies(ctx, entry, parts, generated) - if err != nil { - return nil, err - } + scope := map[string]bool{entry: true} + queue := []string{entry} refs := &editorResourceRefs{ids: make(map[string]bool), fonts: make(map[string]*editorFontUsage)} - for name := range scope { + for position := 0; position < len(queue); position++ { if err := ctx.Err(); err != nil { return nil, err } - if source := references[name]; source != nil { - used[name] = true - if selected == "" || selected == entry { - refs.merge(&editorResourceRefs{ids: source.ids, fonts: source.fonts}) + name := queue[position] + source := references[name] + if source == nil { + return nil, ctx.Err() + } + used[name] = true + if selected == "" || selected == entry { + refs.merge(&editorResourceRefs{ids: source.ids, fonts: source.fonts}) + } + for target, role := range source.roles { + if role&packageReferenceXML != 0 && !scope[target] { + scope[target] = true + queue = append(queue, target) } } } diff --git a/ofdgo_editor_resources.go b/ofdgo_editor_resources.go index 541f9d4..10c6169 100644 --- a/ofdgo_editor_resources.go +++ b/ofdgo_editor_resources.go @@ -101,7 +101,7 @@ func (e *Editor) compactSourceReferences(parts map[string][]byte, progress edito return nil, err } } - scoped, err = reader.resourceReferenceScopes(ctx, parts, generated, refs.documents, resources, references, selected) + scoped, err = resourceReferenceScopes(ctx, refs.documents, resources, references, selected) if err != nil || scoped == nil { return nil, err }