mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-29 15:06:46 +08:00
This commit is contained in:
1 parent
7f79d1e70a
commit
3a867a7cac
15 files changed
+437
-74
No files matched your search
@@ -770,10 +770,9 @@ button:not(:disabled):hover {
|
|||||||
|
|
||||||
.text-font {
|
.text-font {
|
||||||
display: grid;
|
display: grid;
|
||||||
width: 196px;
|
min-width: 0;
|
||||||
grid-template-columns: minmax(0, 1fr) auto;
|
grid-template-columns: subgrid;
|
||||||
grid-column: 1 / -1;
|
grid-column: 1 / -1;
|
||||||
gap: 4px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.text-font .icon-button {
|
.text-font .icon-button {
|
||||||
|
|||||||
@@ -981,7 +981,7 @@ function openObjectBounds() {
|
|||||||
state.boundsOriginal = { x: box.x, y: box.y, width: Math.abs(box.width), height: Math.abs(box.height) };
|
state.boundsOriginal = { x: box.x, y: box.y, width: Math.abs(box.width), height: Math.abs(box.height) };
|
||||||
for (const [input, key] of [[el.objectX,"x"], [el.objectY,"y"], [el.objectWidth,"width"], [el.objectHeight,"height"]]) setObjectDimension(input, state.boundsOriginal[key]);
|
for (const [input, key] of [[el.objectX,"x"], [el.objectY,"y"], [el.objectWidth,"width"], [el.objectHeight,"height"]]) setObjectDimension(input, state.boundsOriginal[key]);
|
||||||
el.objectAspect.checked = true;
|
el.objectAspect.checked = true;
|
||||||
el.objectAspect.disabled = !item.shape && (item.items || [item]).some(member => member.type !== "ImageObject") || lineShape(item.shape) && (!box.width || !box.height);
|
el.objectAspect.disabled = !item.shape && (item.items || [item]).some(member => member.type !== "ImageObject" || member.imageBorder) || lineShape(item.shape) && (!box.width || !box.height);
|
||||||
el.objectWidth.disabled = lineShape(item.shape) && !box.width;
|
el.objectWidth.disabled = lineShape(item.shape) && !box.width;
|
||||||
el.objectHeight.disabled = lineShape(item.shape) && !box.height;
|
el.objectHeight.disabled = lineShape(item.shape) && !box.height;
|
||||||
el.objectBoundsStatus.textContent = "";
|
el.objectBoundsStatus.textContent = "";
|
||||||
|
|||||||
@@ -281,15 +281,24 @@ export class CanvasEditor {
|
|||||||
node.setAttribute("aria-label", { ImageObject: "图片对象", TextObject: "文字对象", PathObject: "图形对象", CompositeObject: "复合对象", CompositeGraphicUnit: "复合对象" }[object.type]);
|
node.setAttribute("aria-label", { ImageObject: "图片对象", TextObject: "文字对象", PathObject: "图形对象", CompositeObject: "复合对象", CompositeGraphicUnit: "复合对象" }[object.type]);
|
||||||
const item = { ...object, index, page: { width: page.width, height: page.height }, node, surface };
|
const item = { ...object, index, page: { width: page.width, height: page.height }, node, surface };
|
||||||
item.artwork = artwork.get(object.id)?.node;
|
item.artwork = artwork.get(object.id)?.node;
|
||||||
if (object.type === "PathObject" && (object.shape || object.outline)) {
|
if (object.type === "PathObject" && (object.shape || object.outline) || (object.type === "CompositeObject" || object.type === "CompositeGraphicUnit") && object.contours?.length) {
|
||||||
node.classList.add("edit-contour");
|
node.classList.add("edit-contour");
|
||||||
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
||||||
svg.classList.add("edit-outline");
|
svg.classList.add("edit-outline");
|
||||||
svg.setAttribute("aria-hidden", "true");
|
svg.setAttribute("aria-hidden", "true");
|
||||||
svg.setAttribute("preserveAspectRatio", "none");
|
svg.setAttribute("preserveAspectRatio", "none");
|
||||||
const contour = document.createElementNS("http://www.w3.org/2000/svg", object.shape === "line" ? "line" : object.shape === "ellipse" ? "ellipse" : object.shape === "rectangle" ? "rect" : "path");
|
let contour;
|
||||||
|
if (object.type === "PathObject") {
|
||||||
|
contour = document.createElementNS("http://www.w3.org/2000/svg", object.shape === "line" ? "line" : object.shape === "ellipse" ? "ellipse" : object.shape === "rectangle" ? "rect" : "path");
|
||||||
if (!object.shape) contour.setAttribute("d", object.outline);
|
if (!object.shape) contour.setAttribute("d", object.outline);
|
||||||
svg.append(contour);
|
svg.append(contour);
|
||||||
|
} else {
|
||||||
|
for (const path of object.contours) {
|
||||||
|
const contour = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
||||||
|
contour.setAttribute("d", path.path);
|
||||||
|
svg.append(contour);
|
||||||
|
}
|
||||||
|
}
|
||||||
node.append(svg);
|
node.append(svg);
|
||||||
item.contour = { svg, path: contour };
|
item.contour = { svg, path: contour };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -737,11 +737,12 @@ func editorObjects(index int, text *ofdgo.PageText) ([]any, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var box ofdgo.Box
|
var box ofdgo.Box
|
||||||
|
var contours []ofdgo.ObjectContour
|
||||||
switch object.Type {
|
switch object.Type {
|
||||||
case "TextObject":
|
case "TextObject":
|
||||||
box = textBoxes[id]
|
box = textBoxes[id]
|
||||||
default:
|
default:
|
||||||
box, err = currentSession.Renderer.ObjectBounds(object, layer.DrawParam)
|
box, contours, err = currentSession.Renderer.ObjectGeometry(object, layer.DrawParam)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
@@ -762,6 +763,7 @@ func editorObjects(index int, text *ofdgo.PageText) ([]any, error) {
|
|||||||
alpha = object.TextObject.Alpha
|
alpha = object.TextObject.Alpha
|
||||||
case "ImageObject":
|
case "ImageObject":
|
||||||
alpha = object.ImageObject.Alpha
|
alpha = object.ImageObject.Alpha
|
||||||
|
item["imageBorder"] = object.ImageObject.Border != nil
|
||||||
case "PathObject":
|
case "PathObject":
|
||||||
alpha = object.PathObject.Alpha
|
alpha = object.PathObject.Alpha
|
||||||
case "CompositeObject", "CompositeGraphicUnit":
|
case "CompositeObject", "CompositeGraphicUnit":
|
||||||
@@ -793,10 +795,6 @@ func editorObjects(index int, text *ofdgo.PageText) ([]any, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if object.Type == "PathObject" || object.Type == "ImageObject" || object.Type == "CompositeObject" || object.Type == "CompositeGraphicUnit" {
|
if object.Type == "PathObject" || object.Type == "ImageObject" || object.Type == "CompositeObject" || object.Type == "CompositeGraphicUnit" {
|
||||||
contours, err := currentSession.Renderer.ObjectContours(object, layer.DrawParam)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
paths := make([]any, len(contours))
|
paths := make([]any, len(contours))
|
||||||
for i, contour := range contours {
|
for i, contour := range contours {
|
||||||
paths[i] = map[string]any{"path": contour.Path, "evenOdd": contour.EvenOdd}
|
paths[i] = map[string]any{"path": contour.Path, "evenOdd": contour.EvenOdd}
|
||||||
|
|||||||
+6
-1
@@ -64,6 +64,7 @@ type editorResource struct {
|
|||||||
font *Font
|
font *Font
|
||||||
image *MultiMedia
|
image *MultiMedia
|
||||||
space *ColorSpace
|
space *ColorSpace
|
||||||
|
composite string
|
||||||
subset *editorFontSubset
|
subset *editorFontSubset
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -609,6 +610,7 @@ func (e *Editor) AddObject(page int, object GraphicObject) (string, error) {
|
|||||||
|
|
||||||
// Object 获取对象的独立副本,修改副本不影响文档
|
// Object 获取对象的独立副本,修改副本不影响文档
|
||||||
// 可编辑的继承样式解析为直接属性;仅在对象修改或复制时写入独立样式
|
// 可编辑的继承样式解析为直接属性;仅在对象修改或复制时写入独立样式
|
||||||
|
// 副本保留捕获时的原文来源,后续变换与撤销不改变其复制语义
|
||||||
// 入参: page 页面索引, id 对象标识
|
// 入参: page 页面索引, id 对象标识
|
||||||
// 返回: GraphicObject 对象内容, error 错误信息
|
// 返回: GraphicObject 对象内容, error 错误信息
|
||||||
func (e *Editor) Object(page int, id string) (GraphicObject, error) {
|
func (e *Editor) Object(page int, id string) (GraphicObject, error) {
|
||||||
@@ -616,7 +618,9 @@ func (e *Editor) Object(page int, id string) (GraphicObject, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return GraphicObject{}, err
|
return GraphicObject{}, err
|
||||||
}
|
}
|
||||||
return cloneEditorObject(layer.Objects[index])
|
object, err := cloneEditorObject(layer.Objects[index])
|
||||||
|
object.origin = e.objectOrigin(id)
|
||||||
|
return object, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateObject 替换对象内容,保留原对象标识和绘制顺序,校验规则与AddObject相同
|
// UpdateObject 替换对象内容,保留原对象标识和绘制顺序,校验规则与AddObject相同
|
||||||
@@ -881,6 +885,7 @@ func (e *Editor) UpdateText(page int, id, value, fontID string, size float64) er
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TransformObject 以页面原点等比缩放后平移对象,文字同步缩放字号和字距,保留原有排版
|
// TransformObject 以页面原点等比缩放后平移对象,文字同步缩放字号和字距,保留原有排版
|
||||||
|
// 带边框图片缩放时转为保留原图片与边框的复合对象,标识不变
|
||||||
// 入参: page 页面索引, id 对象标识, dx 横向位移, dy 纵向位移, scale 正缩放比例
|
// 入参: page 页面索引, id 对象标识, dx 横向位移, dy 纵向位移, scale 正缩放比例
|
||||||
// 返回: error 错误信息
|
// 返回: error 错误信息
|
||||||
func (e *Editor) TransformObject(page int, id string, dx, dy, scale float64) error {
|
func (e *Editor) TransformObject(page int, id string, dx, dy, scale float64) error {
|
||||||
|
|||||||
+53
-22
@@ -63,6 +63,7 @@ func (e *Editor) Objects(page int, ids []string) ([]GraphicObject, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
objects[i].origin = e.objectOrigin(editorObjectID(object))
|
||||||
}
|
}
|
||||||
return objects, nil
|
return objects, nil
|
||||||
}
|
}
|
||||||
@@ -86,7 +87,9 @@ func (e *Editor) CopyObjects(page int, objects []GraphicObject, dx, dy float64)
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
prepared := make([]GraphicObject, len(objects))
|
prepared := make([]GraphicObject, len(objects))
|
||||||
|
origins := make([]*editorObjectOrigin, len(objects))
|
||||||
result := make([]string, len(objects))
|
result := make([]string, len(objects))
|
||||||
|
maximum := e.maxID + len(objects)
|
||||||
for i, object := range objects {
|
for i, object := range objects {
|
||||||
result[i] = strconv.Itoa(e.maxID + i + 1)
|
result[i] = strconv.Itoa(e.maxID + i + 1)
|
||||||
object, err := e.prepareCopiedObject(result[i], object)
|
object, err := e.prepareCopiedObject(result[i], object)
|
||||||
@@ -102,11 +105,14 @@ func (e *Editor) CopyObjects(page int, objects []GraphicObject, dx, dy float64)
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
prepared[i] = object
|
prepared[i], origins[i], err = e.copyObjectOrigin(objects[i], object, &maximum)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
e.maxID += len(objects)
|
}
|
||||||
for i, object := range objects {
|
e.maxID = maximum
|
||||||
if origin := e.objectOrigin(editorObjectID(object)); origin != nil {
|
for i, origin := range origins {
|
||||||
|
if origin != nil {
|
||||||
e.source.origins[result[i]] = origin
|
e.source.origins[result[i]] = origin
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -115,10 +121,17 @@ func (e *Editor) CopyObjects(page int, objects []GraphicObject, dx, dy float64)
|
|||||||
if e.originalPage(page) {
|
if e.originalPage(page) {
|
||||||
target = len(e.source.pages[e.pages[page].ID].original.Content.Layer)
|
target = len(e.source.pages[e.pages[page].ID].original.Content.Layer)
|
||||||
}
|
}
|
||||||
if target == len(layers) {
|
if target < len(layers) {
|
||||||
layers = append(layers, Layer{ID: e.nextID(), Type: "Body"})
|
target = len(layers) - 1
|
||||||
|
}
|
||||||
|
for i, object := range prepared {
|
||||||
|
style := e.copiedLayerStyle(objects[i])
|
||||||
|
if target == len(layers) || layers[target].DrawParam != style {
|
||||||
|
layers = append(layers, Layer{ID: e.nextID(), Type: "Body", DrawParam: style})
|
||||||
|
target = len(layers) - 1
|
||||||
|
}
|
||||||
|
layers[target].Objects = append(layers[target].Objects, object)
|
||||||
}
|
}
|
||||||
layers[target].Objects = append(layers[target].Objects, prepared...)
|
|
||||||
e.replaceLayers(page, layers)
|
e.replaceLayers(page, layers)
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
@@ -346,7 +359,9 @@ func (e *Editor) updateObjects(page int, objects []GraphicObject, preserveConten
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
after := make([]GraphicObject, len(objects))
|
after := make([]GraphicObject, len(objects))
|
||||||
|
var beforeOrigins, afterOrigins map[string]*editorObjectOrigin
|
||||||
for i, object := range objects {
|
for i, object := range objects {
|
||||||
|
object.origin = nil
|
||||||
preserved := preserveContent
|
preserved := preserveContent
|
||||||
origin := e.objectOrigin(ids[i])
|
origin := e.objectOrigin(ids[i])
|
||||||
if origin != nil {
|
if origin != nil {
|
||||||
@@ -372,6 +387,23 @@ func (e *Editor) updateObjects(page int, objects []GraphicObject, preserveConten
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
after[i] = cloneEditorData(object)
|
after[i] = cloneEditorData(object)
|
||||||
|
if before[i].Type != object.Type {
|
||||||
|
data, err := editorObjectXML(object)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
root, err := parseEditorXML(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
root.parent = origin.node.parent
|
||||||
|
if beforeOrigins == nil {
|
||||||
|
beforeOrigins = make(map[string]*editorObjectOrigin)
|
||||||
|
afterOrigins = make(map[string]*editorObjectOrigin)
|
||||||
|
}
|
||||||
|
beforeOrigins[ids[i]] = origin
|
||||||
|
afterOrigins[ids[i]] = &editorObjectOrigin{page: origin.page, data: data, node: root, object: object}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
after[i], err = e.prepareObject(ids[i], object)
|
after[i], err = e.prepareObject(ids[i], object)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -388,15 +420,18 @@ func (e *Editor) updateObjects(page int, objects []GraphicObject, preserveConten
|
|||||||
if reflect.DeepEqual(before, after) {
|
if reflect.DeepEqual(before, after) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
apply := func(e *Editor, objects []GraphicObject) {
|
apply := func(e *Editor, objects []GraphicObject, origins map[string]*editorObjectOrigin) {
|
||||||
for i, index := range indexes {
|
for i, index := range indexes {
|
||||||
e.pages[page].Content.Layer[index.layer].Objects[index.index] = objects[i]
|
e.pages[page].Content.Layer[index.layer].Objects[index.index] = objects[i]
|
||||||
}
|
}
|
||||||
|
for id, origin := range origins {
|
||||||
|
e.source.origins[id] = origin
|
||||||
}
|
}
|
||||||
apply(e, after)
|
}
|
||||||
|
apply(e, after, afterOrigins)
|
||||||
if change := e.recordChange(); change != nil {
|
if change := e.recordChange(); change != nil {
|
||||||
change.undo = func(e *Editor) { apply(e, before) }
|
change.undo = func(e *Editor) { apply(e, before, beforeOrigins) }
|
||||||
change.redo = func(e *Editor) { apply(e, after) }
|
change.redo = func(e *Editor) { apply(e, after, afterOrigins) }
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -415,25 +450,21 @@ func (e *Editor) TransformObjects(page int, ids []string, dx, dy, scale float64)
|
|||||||
if dx == 0 && dy == 0 && scale == 1 {
|
if dx == 0 && dy == 0 && scale == 1 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
for i, object := range objects {
|
return e.transformObjects(page, objects, func(object GraphicObject) (GraphicObject, error) {
|
||||||
if e.originalPage(page) {
|
if e.originalPage(page) {
|
||||||
if err := validateEditorGeometry(object); err != nil {
|
if err := validateEditorGeometry(object); err != nil {
|
||||||
return err
|
return GraphicObject{}, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
object, err = cloneEditorObject(object)
|
after, err := e.transformObject(object, dx, dy, scale)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return GraphicObject{}, err
|
||||||
}
|
|
||||||
objects[i], err = e.transformObject(object, dx, dy, scale)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
if origin := e.objectOrigin(editorObjectID(object)); origin != nil && origin.node.attr("LineWidth") == "0" && object.Type == "TextObject" && object.TextObject.LineWidth == 0 {
|
if origin := e.objectOrigin(editorObjectID(object)); origin != nil && origin.node.attr("LineWidth") == "0" && object.Type == "TextObject" && object.TextObject.LineWidth == 0 {
|
||||||
objects[i].TextObject.LineWidth = 0
|
after.TextObject.LineWidth = 0
|
||||||
}
|
}
|
||||||
}
|
return after, nil
|
||||||
return e.updateObjects(page, objects, true)
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// AlignObjects 单对象对齐页面,多对象相互对齐至选区边界,文字采用实际字形范围
|
// AlignObjects 单对象对齐页面,多对象相互对齐至选区边界,文字采用实际字形范围
|
||||||
|
|||||||
@@ -0,0 +1,214 @@
|
|||||||
|
// Copyright 2025-2026 肖其顿 (XIAO QI DUN)
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package ofdgo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// snapshotOrigin 获取当前文档内捕获时的原文来源,外部快照按普通对象校验
|
||||||
|
// 入参: object 对象快照
|
||||||
|
// 返回: *editorObjectOrigin 原文来源
|
||||||
|
func (e *Editor) snapshotOrigin(object GraphicObject) *editorObjectOrigin {
|
||||||
|
if object.origin != nil {
|
||||||
|
if e.source != nil && e.source.pages[object.origin.page.ref.ID] == object.origin.page {
|
||||||
|
return object.origin
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return e.objectOrigin(editorObjectID(object))
|
||||||
|
}
|
||||||
|
|
||||||
|
// copyObjectOrigin 复制原XML并重映射内部标识和引用,不修改共享资源与原对象
|
||||||
|
// 入参: before 输入快照, after 副本快照, maximum 本批次最大标识
|
||||||
|
// 返回: GraphicObject 副本, *editorObjectOrigin 副本原文, error 错误信息
|
||||||
|
func (e *Editor) copyObjectOrigin(before, after GraphicObject, maximum *int) (GraphicObject, *editorObjectOrigin, error) {
|
||||||
|
origin := e.snapshotOrigin(before)
|
||||||
|
if origin == nil {
|
||||||
|
return after, nil, nil
|
||||||
|
}
|
||||||
|
rootID := editorResourceID(origin.node.attr("ID"))
|
||||||
|
ids := map[string]string{rootID: editorObjectID(after)}
|
||||||
|
selfReference := false
|
||||||
|
var collect func(*editorXML) error
|
||||||
|
collect = func(node *editorXML) error {
|
||||||
|
for _, attr := range node.attrs {
|
||||||
|
if attr.Name.Space == "" && editorObjectReference(attr.Name.Local) && editorResourceID(attr.Value) == rootID {
|
||||||
|
selfReference = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (node.name.Local == "Font" || node.name.Local == "Substitution") && editorResourceID(editorImportText(origin.data, node)) == rootID {
|
||||||
|
selfReference = true
|
||||||
|
}
|
||||||
|
for _, child := range node.children {
|
||||||
|
if id := editorResourceID(child.attr("ID")); id != "" {
|
||||||
|
if ids[id] != "" {
|
||||||
|
return fmt.Errorf("duplicate object ID %q", id)
|
||||||
|
}
|
||||||
|
*maximum += 1
|
||||||
|
ids[id] = strconv.Itoa(*maximum)
|
||||||
|
}
|
||||||
|
if err := collect(child); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err := collect(origin.node); err != nil {
|
||||||
|
return GraphicObject{}, nil, err
|
||||||
|
}
|
||||||
|
if len(ids) == 1 && !selfReference {
|
||||||
|
return after, origin, nil
|
||||||
|
}
|
||||||
|
data, err := editorXMLObject(origin.data, origin.node, origin.object, after)
|
||||||
|
if err != nil {
|
||||||
|
return GraphicObject{}, nil, err
|
||||||
|
}
|
||||||
|
data, err = editorXMLStandalone(data, origin.node)
|
||||||
|
if err != nil {
|
||||||
|
return GraphicObject{}, nil, err
|
||||||
|
}
|
||||||
|
root, err := parseEditorXML(data)
|
||||||
|
if err != nil {
|
||||||
|
return GraphicObject{}, nil, err
|
||||||
|
}
|
||||||
|
data, err = editorXMLRemapIDs(data, root, ids)
|
||||||
|
if err != nil {
|
||||||
|
return GraphicObject{}, nil, err
|
||||||
|
}
|
||||||
|
root, err = parseEditorXML(data)
|
||||||
|
if err != nil {
|
||||||
|
return GraphicObject{}, nil, err
|
||||||
|
}
|
||||||
|
result := GraphicObject{Type: after.Type}
|
||||||
|
var target any
|
||||||
|
switch result.Type {
|
||||||
|
case "TextObject":
|
||||||
|
target = &result.TextObject
|
||||||
|
case "PathObject":
|
||||||
|
target = &result.PathObject
|
||||||
|
case "ImageObject":
|
||||||
|
target = &result.ImageObject
|
||||||
|
case "CompositeObject", "CompositeGraphicUnit":
|
||||||
|
target = &result.CompositeGraphicUnit
|
||||||
|
}
|
||||||
|
if err := xml.Unmarshal(data, target); err != nil {
|
||||||
|
return GraphicObject{}, nil, err
|
||||||
|
}
|
||||||
|
result.TextObject.layout = after.TextObject.layout
|
||||||
|
root.parent = origin.node.parent
|
||||||
|
return result, &editorObjectOrigin{page: origin.page, data: data, node: root, object: result}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// editorXMLRemapIDs 仅重写标准标识和指向副本内部的引用,保留扩展、文字和XML结构
|
||||||
|
// 入参: data 独立片段, root 根节点, ids 原标识与新标识
|
||||||
|
// 返回: []byte 修改后的片段, error 错误信息
|
||||||
|
func editorXMLRemapIDs(data []byte, root *editorXML, ids map[string]string) ([]byte, error) {
|
||||||
|
var patches []editorXMLPatch
|
||||||
|
var visit func(*editorXML) error
|
||||||
|
visit = func(node *editorXML) error {
|
||||||
|
if node.name.Space == "" || node.name.Space == ofdNamespace || node.name.Space == "http://www.ofdspec.org" {
|
||||||
|
decoder := xml.NewDecoder(bytes.NewReader(data[node.start:node.open]))
|
||||||
|
token, err := decoder.RawToken()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
start := token.(xml.StartElement)
|
||||||
|
changed := false
|
||||||
|
for i := range start.Attr {
|
||||||
|
attr := &start.Attr[i]
|
||||||
|
if attr.Name.Space != "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if attr.Name.Local == "ID" || editorObjectReference(attr.Name.Local) {
|
||||||
|
if value := ids[editorResourceID(attr.Value)]; value != "" {
|
||||||
|
attr.Value, changed = value, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if changed {
|
||||||
|
if start.Name.Space != "" {
|
||||||
|
start.Name.Local = start.Name.Space + ":" + start.Name.Local
|
||||||
|
start.Name.Space = ""
|
||||||
|
}
|
||||||
|
for i := range start.Attr {
|
||||||
|
attr := &start.Attr[i]
|
||||||
|
if attr.Name.Space != "" {
|
||||||
|
attr.Name.Local = attr.Name.Space + ":" + attr.Name.Local
|
||||||
|
attr.Name.Space = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var header bytes.Buffer
|
||||||
|
encoder := xml.NewEncoder(&header)
|
||||||
|
if err := encoder.EncodeToken(start); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := encoder.Flush(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
value := header.Bytes()
|
||||||
|
if node.open == node.end {
|
||||||
|
value = append(value[:len(value)-1], '/', '>')
|
||||||
|
}
|
||||||
|
patches = append(patches, editorXMLPatch{node.start, node.open, value})
|
||||||
|
}
|
||||||
|
if node.name.Local == "Font" || node.name.Local == "Substitution" {
|
||||||
|
if value := ids[editorResourceID(editorImportText(data, node))]; value != "" {
|
||||||
|
patches = append(patches, editorXMLContent(data, node, []byte(value)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, child := range node.children {
|
||||||
|
if err := visit(child); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err := visit(root); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return editorPatchXML(data, patches), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// editorObjectReference 判断对象内可按ST_RefID重映射的标准属性
|
||||||
|
// 入参: name 属性名
|
||||||
|
// 返回: bool 是否为标识引用
|
||||||
|
func editorObjectReference(name string) bool {
|
||||||
|
switch name {
|
||||||
|
case "Font", "ResourceID", "Substitution", "ImageMask", "Relative", "DrawParam", "ColorSpace", "Thumbnail", "TemplateID", "PageID", "PageRef", "RefId":
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// copiedLayerStyle 获取复合对象需保留的图层绘制参数,不向基本对象重新施加继承样式
|
||||||
|
// 入参: object 对象
|
||||||
|
// 返回: string 绘制参数标识
|
||||||
|
func (e *Editor) copiedLayerStyle(object GraphicObject) string {
|
||||||
|
if object.Type == "CompositeObject" || object.Type == "CompositeGraphicUnit" {
|
||||||
|
if origin := e.snapshotOrigin(object); origin != nil {
|
||||||
|
for parent := origin.node.parent; parent != nil; parent = parent.parent {
|
||||||
|
if parent.name.Local == "Layer" {
|
||||||
|
return parent.attr("DrawParam")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
+7
-15
@@ -43,6 +43,7 @@ type editorSource struct {
|
|||||||
// editorObjectOrigin 保留原对象的XML语义,供复制和局部更新复用
|
// editorObjectOrigin 保留原对象的XML语义,供复制和局部更新复用
|
||||||
type editorObjectOrigin struct {
|
type editorObjectOrigin struct {
|
||||||
page *editorSourcePage
|
page *editorSourcePage
|
||||||
|
data []byte
|
||||||
node *editorXML
|
node *editorXML
|
||||||
object GraphicObject
|
object GraphicObject
|
||||||
reason error
|
reason error
|
||||||
@@ -322,7 +323,7 @@ func (e *Editor) loadSourcePage(index int) error {
|
|||||||
for j, object := range layer.Objects {
|
for j, object := range layer.Objects {
|
||||||
id := editorObjectID(object)
|
id := editorObjectID(object)
|
||||||
if node := nodes[id]; node != nil {
|
if node := nodes[id]; node != nil {
|
||||||
origin := &editorObjectOrigin{page: source, node: node, object: object}
|
origin := &editorObjectOrigin{page: source, data: data, node: node, object: object}
|
||||||
e.source.origins[id] = origin
|
e.source.origins[id] = origin
|
||||||
if editorXMLTransformable(node) {
|
if editorXMLTransformable(node) {
|
||||||
resolved, err := e.resolveEditorStyle(object, layer.DrawParam)
|
resolved, err := e.resolveEditorStyle(object, layer.DrawParam)
|
||||||
@@ -371,13 +372,6 @@ func (e *Editor) ObjectCapabilities(page int, id string) (ObjectCapabilities, er
|
|||||||
}
|
}
|
||||||
all.Order = editorContainerOrderable(node.parent)
|
all.Order = editorContainerOrderable(node.parent)
|
||||||
all.Copy = editorXMLCopyable(node) && origin.reason == nil
|
all.Copy = editorXMLCopyable(node) && origin.reason == nil
|
||||||
if object.Type == "CompositeObject" || object.Type == "CompositeGraphicUnit" {
|
|
||||||
for parent := node.parent; parent != nil; parent = parent.parent {
|
|
||||||
if parent.name.Local == "Layer" && parent.attr("DrawParam") != "" {
|
|
||||||
all.Copy = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if origin.reason != nil {
|
if origin.reason != nil {
|
||||||
all.Update, all.Reflow, all.ReplaceFont, all.Paint = false, false, false, false
|
all.Update, all.Reflow, all.ReplaceFont, all.Paint = false, false, false, false
|
||||||
all.Reason, all.ReasonCode = origin.reason.Error(), editReason(origin.reason)
|
all.Reason, all.ReasonCode = origin.reason.Error(), editReason(origin.reason)
|
||||||
@@ -390,9 +384,9 @@ func (e *Editor) ObjectCapabilities(page int, id string) (ObjectCapabilities, er
|
|||||||
errors.As(err, &all.MissingGlyphs)
|
errors.As(err, &all.MissingGlyphs)
|
||||||
}
|
}
|
||||||
all.Paint = all.Paint && e.editorPaintable(object)
|
all.Paint = all.Paint && e.editorPaintable(object)
|
||||||
if object.Type == "ImageObject" && object.ImageObject.Border != nil {
|
if object.Type == "ImageObject" && object.ImageObject.Border != nil && len(object.ImageObject.Actions) != 0 {
|
||||||
all.Transform, all.Arrange = false, false
|
all.Transform, all.Arrange = false, false
|
||||||
all.Reason, all.ReasonCode = "image border transformations are not supported", EditUnsupportedObject
|
all.Reason, all.ReasonCode = "image actions cannot be transformed together with the border", EditUnsupportedObject
|
||||||
}
|
}
|
||||||
if object.Type == "TextObject" {
|
if object.Type == "TextObject" {
|
||||||
all.Arrange = all.Update || e.editorTextMeasurable(object.TextObject)
|
all.Arrange = all.Update || e.editorTextMeasurable(object.TextObject)
|
||||||
@@ -460,7 +454,8 @@ func editorPreservedObject(before, after GraphicObject) bool {
|
|||||||
// 入参: id 新标识, object 对象快照
|
// 入参: id 新标识, object 对象快照
|
||||||
// 返回: GraphicObject 独立副本, error 错误信息
|
// 返回: GraphicObject 独立副本, error 错误信息
|
||||||
func (e *Editor) prepareCopiedObject(id string, object GraphicObject) (GraphicObject, error) {
|
func (e *Editor) prepareCopiedObject(id string, object GraphicObject) (GraphicObject, error) {
|
||||||
origin := e.objectOrigin(editorObjectID(object))
|
origin := e.snapshotOrigin(object)
|
||||||
|
object.origin = nil
|
||||||
if origin == nil {
|
if origin == nil {
|
||||||
return e.prepareObject(id, object)
|
return e.prepareObject(id, object)
|
||||||
}
|
}
|
||||||
@@ -478,9 +473,6 @@ func (e *Editor) prepareCopiedObject(id string, object GraphicObject) (GraphicOb
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return GraphicObject{}, err
|
return GraphicObject{}, err
|
||||||
}
|
}
|
||||||
if drawParam != "" && (object.Type == "CompositeObject" || object.Type == "CompositeGraphicUnit") {
|
|
||||||
return GraphicObject{}, fmt.Errorf("copy requires preserving the composite layer draw parameters")
|
|
||||||
}
|
|
||||||
if !editorPreservedObject(before, object) {
|
if !editorPreservedObject(before, object) {
|
||||||
if !editorXMLSupported(origin.node) {
|
if !editorXMLSupported(origin.node) {
|
||||||
return GraphicObject{}, fmt.Errorf("copy would replace unsupported object content")
|
return GraphicObject{}, fmt.Errorf("copy would replace unsupported object content")
|
||||||
@@ -664,7 +656,7 @@ func (e *Editor) editorImage(id string) (image.Point, error) {
|
|||||||
return size, nil
|
return size, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// cloneEditorData 深复制编辑快照,未导出的排版信息保持不可变共享
|
// cloneEditorData 深复制编辑快照,未导出的排版及原文来源保持不可变共享
|
||||||
// 入参: value 原值
|
// 入参: value 原值
|
||||||
// 返回: T 独立副本
|
// 返回: T 独立副本
|
||||||
func cloneEditorData[T any](value T) T {
|
func cloneEditorData[T any](value T) T {
|
||||||
|
|||||||
@@ -67,6 +67,11 @@ func (e *Editor) sourceParts(progress editorProgress) (map[string][]byte, error)
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
fonts, images, spaces, resourceFiles := e.usedResources()
|
fonts, images, spaces, resourceFiles := e.usedResources()
|
||||||
|
for _, resource := range e.resources {
|
||||||
|
if resource.composite != "" && slices.Contains(resourceFiles, resource.name) {
|
||||||
|
parts[resource.name] = resource.data
|
||||||
|
}
|
||||||
|
}
|
||||||
commonData := source.document.CommonData
|
commonData := source.document.CommonData
|
||||||
for _, name := range append(slices.Clone(commonData.DocumentRes), commonData.PublicRes...) {
|
for _, name := range append(slices.Clone(commonData.DocumentRes), commonData.PublicRes...) {
|
||||||
resourceFiles = slices.DeleteFunc(resourceFiles, func(file string) bool {
|
resourceFiles = slices.DeleteFunc(resourceFiles, func(file string) bool {
|
||||||
@@ -325,7 +330,7 @@ func (e *Editor) sourceLayerXML(layer Layer) ([]byte, error) {
|
|||||||
var data []byte
|
var data []byte
|
||||||
var err error
|
var err error
|
||||||
if origin := e.objectOrigin(editorObjectID(object)); origin != nil {
|
if origin := e.objectOrigin(editorObjectID(object)); origin != nil {
|
||||||
data, err = editorXMLObject(origin.page.data, origin.node, origin.object, object)
|
data, err = editorXMLObject(origin.data, origin.node, origin.object, object)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
data, err = editorXMLStandalone(data, origin.node)
|
data, err = editorXMLStandalone(data, origin.node)
|
||||||
}
|
}
|
||||||
@@ -337,7 +342,9 @@ func (e *Editor) sourceLayerXML(layer Layer) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
content = append(content, data...)
|
content = append(content, data...)
|
||||||
}
|
}
|
||||||
return editorXMLContainer("Layer", ofdAttrs{{Name: xml.Name{Local: "ID"}, Value: layer.ID}, {Name: xml.Name{Local: "Type"}, Value: layer.Type}}, content)
|
attrs := ofdAttrs{{Name: xml.Name{Local: "ID"}, Value: layer.ID}, {Name: xml.Name{Local: "Type"}, Value: layer.Type}}
|
||||||
|
attrs.add("DrawParam", layer.DrawParam)
|
||||||
|
return editorXMLContainer("Layer", attrs, content)
|
||||||
}
|
}
|
||||||
|
|
||||||
// sourceNewPageXML 编码新增页面,同时保留复制对象的原文语义
|
// sourceNewPageXML 编码新增页面,同时保留复制对象的原文语义
|
||||||
|
|||||||
@@ -49,6 +49,13 @@ func (e *Editor) resolveEditorStyle(object GraphicObject, layer string) (Graphic
|
|||||||
id = object.TextObject.DrawParam
|
id = object.TextObject.DrawParam
|
||||||
case "PathObject":
|
case "PathObject":
|
||||||
id = object.PathObject.DrawParam
|
id = object.PathObject.DrawParam
|
||||||
|
case "CompositeObject", "CompositeGraphicUnit":
|
||||||
|
for _, id := range []string{layer, object.CompositeGraphicUnit.DrawParam} {
|
||||||
|
if _, err := e.editorDrawParam(id, make(map[string]bool)); err != nil {
|
||||||
|
return GraphicObject{}, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return object, nil
|
||||||
default:
|
default:
|
||||||
return object, nil
|
return object, nil
|
||||||
}
|
}
|
||||||
|
|||||||
+101
-17
@@ -15,6 +15,8 @@
|
|||||||
package ofdgo
|
package ofdgo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"slices"
|
"slices"
|
||||||
@@ -29,6 +31,7 @@ func (e *Editor) RotateObject(page int, id string, degrees int) error {
|
|||||||
|
|
||||||
// RotateObjects 绕同页选区的可见范围中心旋转
|
// RotateObjects 绕同页选区的可见范围中心旋转
|
||||||
// 保留相对位置及资源,一次撤销恢复全部
|
// 保留相对位置及资源,一次撤销恢复全部
|
||||||
|
// 带边框图片转为保留原内容的复合对象,标识不变
|
||||||
// 入参: page 页面索引, ids 对象标识, degrees 顺时针角度,仅支持90度的整数倍
|
// 入参: page 页面索引, ids 对象标识, degrees 顺时针角度,仅支持90度的整数倍
|
||||||
// 返回: error 错误信息
|
// 返回: error 错误信息
|
||||||
func (e *Editor) RotateObjects(page int, ids []string, degrees int) error {
|
func (e *Editor) RotateObjects(page int, ids []string, degrees int) error {
|
||||||
@@ -64,7 +67,7 @@ func (e *Editor) FlipObjects(page int, ids []string, axis string) error {
|
|||||||
return e.orientObjects(page, ids, m)
|
return e.orientObjects(page, ids, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResizeObjects 将选区可见范围移动并缩放到指定矩形,文字与路径保持比例,图片支持独立宽高
|
// ResizeObjects 将选区可见范围移动并缩放到指定矩形,文字、路径和带边框图片保持比例,无边框图片支持独立宽高
|
||||||
// 不重排文字或重采样图片,一次撤销恢复全部;基本图形独立调整宽高可使用PathObject.Reshape
|
// 不重排文字或重采样图片,一次撤销恢复全部;基本图形独立调整宽高可使用PathObject.Reshape
|
||||||
// 入参: page 页面索引, ids 对象标识, box 页面毫米坐标中的目标范围
|
// 入参: page 页面索引, ids 对象标识, box 页面毫米坐标中的目标范围
|
||||||
// 返回: error 错误信息
|
// 返回: error 错误信息
|
||||||
@@ -90,27 +93,23 @@ func (e *Editor) ResizeObjects(page int, ids []string, box Box) error {
|
|||||||
sx, sy := box.W/before.W, box.H/before.H
|
sx, sy := box.W/before.W, box.H/before.H
|
||||||
uniform := math.Abs(sx-sy) <= 1e-9*math.Max(sx, sy)
|
uniform := math.Abs(sx-sy) <= 1e-9*math.Max(sx, sy)
|
||||||
matrix := Matrix{a: sx, d: sy, e: box.X - before.X*sx, f: box.Y - before.Y*sy}
|
matrix := Matrix{a: sx, d: sy, e: box.X - before.X*sx, f: box.Y - before.Y*sy}
|
||||||
for i, object := range objects {
|
return e.transformObjects(page, objects, func(object GraphicObject) (GraphicObject, error) {
|
||||||
object = cloneEditorData(object)
|
|
||||||
if uniform {
|
if uniform {
|
||||||
object, err = e.transformObject(object, matrix.e, matrix.f, sx)
|
return e.transformObject(object, matrix.e, matrix.f, sx)
|
||||||
} else if object.Type == "ImageObject" {
|
} else if object.Type == "ImageObject" && object.ImageObject.Border == nil {
|
||||||
object = transformEditorMatrix(object, matrix)
|
return e.transformMatrix(object, matrix)
|
||||||
} else {
|
|
||||||
return fmt.Errorf("nonuniform resizing requires image objects")
|
|
||||||
}
|
}
|
||||||
if err != nil {
|
return GraphicObject{}, fmt.Errorf("nonuniform resizing requires images without borders")
|
||||||
return err
|
})
|
||||||
}
|
|
||||||
objects[i] = object
|
|
||||||
}
|
|
||||||
return e.updateObjects(page, objects, true)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// transformObject 对原有复杂对象仅更新变换,不重建其文字、画刷或其他局部属性
|
// transformObject 对原有复杂对象仅更新变换,不重建其文字、画刷或其他局部属性
|
||||||
// 入参: object 对象, dx、dy 页面位移, scale 缩放比例
|
// 入参: object 对象, dx、dy 页面位移, scale 缩放比例
|
||||||
// 返回: GraphicObject 新对象, error 错误信息
|
// 返回: GraphicObject 新对象, error 错误信息
|
||||||
func (e *Editor) transformObject(object GraphicObject, dx, dy, scale float64) (GraphicObject, error) {
|
func (e *Editor) transformObject(object GraphicObject, dx, dy, scale float64) (GraphicObject, error) {
|
||||||
|
if object.Type == "ImageObject" && object.ImageObject.Border != nil && scale != 1 {
|
||||||
|
return e.transformMatrix(object, Matrix{a: scale, d: scale, e: dx, f: dy})
|
||||||
|
}
|
||||||
if origin := e.objectOrigin(editorObjectID(object)); origin != nil && (!editorXMLSupported(origin.node) || origin.reason != nil) {
|
if origin := e.objectOrigin(editorObjectID(object)); origin != nil && (!editorXMLSupported(origin.node) || origin.reason != nil) {
|
||||||
return transformEditorMatrix(object, Matrix{a: scale, d: scale, e: dx, f: dy}), nil
|
return transformEditorMatrix(object, Matrix{a: scale, d: scale, e: dx, f: dy}), nil
|
||||||
}
|
}
|
||||||
@@ -135,10 +134,95 @@ func (e *Editor) orientObjects(page int, ids []string, matrix Matrix) error {
|
|||||||
}
|
}
|
||||||
x, y := box.X+box.W/2, box.Y+box.H/2
|
x, y := box.X+box.W/2, box.Y+box.H/2
|
||||||
matrix = TranslationMatrix(x, y).Multiply(matrix).Multiply(TranslationMatrix(-x, -y))
|
matrix = TranslationMatrix(x, y).Multiply(matrix).Multiply(TranslationMatrix(-x, -y))
|
||||||
for i, object := range objects {
|
return e.transformObjects(page, objects, func(object GraphicObject) (GraphicObject, error) {
|
||||||
objects[i] = transformEditorMatrix(object, matrix)
|
return e.transformMatrix(object, matrix)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return e.updateObjects(page, objects, true)
|
|
||||||
|
// transformObjects 原子变换对象副本,失败不保留新复合资源及其标识
|
||||||
|
// 入参: page 页面索引, objects 选区快照, transform 单对象变换
|
||||||
|
// 返回: error 错误信息
|
||||||
|
func (e *Editor) transformObjects(page int, objects []GraphicObject, transform func(GraphicObject) (GraphicObject, error)) (err error) {
|
||||||
|
count, maximum := len(e.resources), e.maxID
|
||||||
|
idsReady := e.source != nil && e.source.idsReady
|
||||||
|
defer func() {
|
||||||
|
if err != nil {
|
||||||
|
clear(e.resources[count:])
|
||||||
|
e.resources, e.maxID = e.resources[:count], maximum
|
||||||
|
if e.source != nil {
|
||||||
|
e.source.idsReady = idsReady
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
after := make([]GraphicObject, len(objects))
|
||||||
|
for i, object := range objects {
|
||||||
|
object, err = cloneEditorObject(object)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
after[i], err = transform(object)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return e.updateObjects(page, after, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// transformMatrix 将带边框图片封装为标准复合资源后整体变换,不改变原边框及裁剪语义
|
||||||
|
// 入参: object 对象, matrix 页面变换
|
||||||
|
// 返回: GraphicObject 变换后的对象, error 错误信息
|
||||||
|
func (e *Editor) transformMatrix(object GraphicObject, matrix Matrix) (GraphicObject, error) {
|
||||||
|
if object.Type != "ImageObject" || object.ImageObject.Border == nil {
|
||||||
|
return transformEditorMatrix(object, matrix), nil
|
||||||
|
}
|
||||||
|
origin := e.objectOrigin(object.ImageObject.ID)
|
||||||
|
if origin == nil {
|
||||||
|
return GraphicObject{}, fmt.Errorf("bordered image requires an original object")
|
||||||
|
}
|
||||||
|
if err := e.prepareSourceIDs(); err != nil {
|
||||||
|
return GraphicObject{}, err
|
||||||
|
}
|
||||||
|
box, err := creationBox(object.ImageObject.Boundary)
|
||||||
|
if err != nil {
|
||||||
|
return GraphicObject{}, err
|
||||||
|
}
|
||||||
|
visible, err := NewRenderer(e.source.reader).ObjectBounds(object, "")
|
||||||
|
if err != nil {
|
||||||
|
return GraphicObject{}, err
|
||||||
|
}
|
||||||
|
extent := unionTextBox(box, visible)
|
||||||
|
child := cloneEditorData(object)
|
||||||
|
child.ImageObject.ID = e.nextID()
|
||||||
|
child.ImageObject.Boundary = editorBoxString(Box{X: box.X - extent.X, Y: box.Y - extent.Y, W: box.W, H: box.H})
|
||||||
|
data, err := editorXMLObject(origin.data, origin.node, origin.object, child)
|
||||||
|
if err != nil {
|
||||||
|
return GraphicObject{}, err
|
||||||
|
}
|
||||||
|
data, err = editorXMLStandalone(data, origin.node)
|
||||||
|
if err != nil {
|
||||||
|
return GraphicObject{}, err
|
||||||
|
}
|
||||||
|
content, err := editorXMLContainer("Content", nil, data)
|
||||||
|
if err != nil {
|
||||||
|
return GraphicObject{}, err
|
||||||
|
}
|
||||||
|
id := e.nextID()
|
||||||
|
attrs := ofdAttrs{{Name: xml.Name{Local: "ID"}, Value: id}, {Name: xml.Name{Local: "Width"}, Value: ofdNumber(extent.W)}, {Name: xml.Name{Local: "Height"}, Value: ofdNumber(extent.H)}}
|
||||||
|
data, err = editorXMLContainer("CompositeGraphicUnit", attrs, bytes.TrimPrefix(content, []byte(xml.Header)))
|
||||||
|
if err != nil {
|
||||||
|
return GraphicObject{}, err
|
||||||
|
}
|
||||||
|
data, err = editorXMLContainer("CompositeGraphicUnits", nil, bytes.TrimPrefix(data, []byte(xml.Header)))
|
||||||
|
if err != nil {
|
||||||
|
return GraphicObject{}, err
|
||||||
|
}
|
||||||
|
data, err = editorXMLContainer("Res", ofdAttrs{{Name: xml.Name{Local: "BaseLoc"}, Value: "."}}, bytes.TrimPrefix(data, []byte(xml.Header)))
|
||||||
|
if err != nil {
|
||||||
|
return GraphicObject{}, err
|
||||||
|
}
|
||||||
|
e.resources = append(e.resources, editorResource{name: e.resourceDirectory() + "/Composite_" + id + ".xml", data: data, composite: id})
|
||||||
|
object = GraphicObject{Type: "CompositeObject", CompositeGraphicUnit: CompositeGraphicUnit{ID: object.ImageObject.ID, ResourceID: id, Boundary: editorBoxString(extent)}}
|
||||||
|
return transformEditorMatrix(object, matrix), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// transformEditorMatrix 在页面坐标中变换基本对象,保留局部绘制数据和裁剪
|
// transformEditorMatrix 在页面坐标中变换基本对象,保留局部绘制数据和裁剪
|
||||||
|
|||||||
+5
-2
@@ -302,12 +302,15 @@ func editorXMLContainersSupported(node *editorXML) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// editorXMLCopyable 判断独立复制是否无需重写内部标识或动作引用
|
// editorXMLCopyable 判断内部标识能否按标准重映射,扩展节点的标识不作推断
|
||||||
// 入参: node 原始对象节点
|
// 入参: node 原始对象节点
|
||||||
// 返回: bool 是否支持保真复制
|
// 返回: bool 是否支持保真复制
|
||||||
func editorXMLCopyable(node *editorXML) bool {
|
func editorXMLCopyable(node *editorXML) bool {
|
||||||
|
if node.attr("ID") != "" && (editorResourceID(node.attr("ID")) == "" || node.name.Space != "" && node.name.Space != ofdNamespace && node.name.Space != "http://www.ofdspec.org") {
|
||||||
|
return false
|
||||||
|
}
|
||||||
for _, child := range node.children {
|
for _, child := range node.children {
|
||||||
if child.attr("ID") != "" || child.name.Local == "Actions" || !editorXMLCopyable(child) {
|
if !editorXMLCopyable(child) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ type GraphicObject struct {
|
|||||||
PathObject PathObject
|
PathObject PathObject
|
||||||
ImageObject ImageObject
|
ImageObject ImageObject
|
||||||
CompositeGraphicUnit CompositeGraphicUnit
|
CompositeGraphicUnit CompositeGraphicUnit
|
||||||
|
origin *editorObjectOrigin
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clips 裁剪区域集合
|
// Clips 裁剪区域集合
|
||||||
|
|||||||
@@ -42,12 +42,20 @@ type ObjectContour struct {
|
|||||||
// 入参: object 路径、图片或复合对象, drawParam 图层绘制参数标识
|
// 入参: object 路径、图片或复合对象, drawParam 图层绘制参数标识
|
||||||
// 返回: []ObjectContour 可用于点选的轮廓, error 错误信息
|
// 返回: []ObjectContour 可用于点选的轮廓, error 错误信息
|
||||||
func (r *Renderer) ObjectContours(object GraphicObject, drawParam string) ([]ObjectContour, error) {
|
func (r *Renderer) ObjectContours(object GraphicObject, drawParam string) ([]ObjectContour, error) {
|
||||||
|
_, contours, err := r.ObjectGeometry(object, drawParam)
|
||||||
|
return contours, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ObjectGeometry 一次度量路径、图片或复合对象的范围与轮廓,语义与ObjectBounds和ObjectContours一致
|
||||||
|
// 入参: object 图形对象, drawParam 图层绘制参数标识
|
||||||
|
// 返回: Box 毫米范围, []ObjectContour 绘制轮廓, error 错误信息
|
||||||
|
func (r *Renderer) ObjectGeometry(object GraphicObject, drawParam string) (Box, []ObjectContour, error) {
|
||||||
if object.Type != "PathObject" && object.Type != "ImageObject" && object.Type != "CompositeObject" && object.Type != "CompositeGraphicUnit" {
|
if object.Type != "PathObject" && object.Type != "ImageObject" && object.Type != "CompositeObject" && object.Type != "CompositeGraphicUnit" {
|
||||||
return nil, fmt.Errorf("contours require a path, image or composite object")
|
return Box{}, nil, fmt.Errorf("contours require a path, image or composite object")
|
||||||
}
|
}
|
||||||
bounds := &boundsRenderer{collect: true}
|
bounds := &boundsRenderer{collect: true}
|
||||||
_, err := r.measureObject(object, drawParam, bounds)
|
box, err := r.measureObject(object, drawParam, bounds)
|
||||||
return bounds.contours, err
|
return box, bounds.contours, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// measureObject 复用渲染逻辑收集对象范围与可选轮廓
|
// measureObject 复用渲染逻辑收集对象范围与可选轮廓
|
||||||
|
|||||||
+6
-1
@@ -320,7 +320,7 @@ func (e *Editor) usedResources() (fonts, images []editorResource, spaces []Color
|
|||||||
for _, layer := range page.Content.Layer {
|
for _, layer := range page.Content.Layer {
|
||||||
for _, object := range layer.Objects {
|
for _, object := range layer.Objects {
|
||||||
before, exists := original[editorObjectID(object)]
|
before, exists := original[editorObjectID(object)]
|
||||||
if origin := e.objectOrigin(editorObjectID(object)); origin != nil && !exists {
|
if origin := e.objectOrigin(editorObjectID(object)); origin != nil && (!exists || before.Type != object.Type) {
|
||||||
for _, name := range origin.page.original.PageRes {
|
for _, name := range origin.page.original.PageRes {
|
||||||
files[e.source.reader.ResPath(resolveResourcePath(origin.page.ref.BaseLoc, "", name))] = true
|
files[e.source.reader.ResPath(resolveResourcePath(origin.page.ref.BaseLoc, "", name))] = true
|
||||||
}
|
}
|
||||||
@@ -334,6 +334,8 @@ func (e *Editor) usedResources() (fonts, images []editorResource, spaces []Color
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch object.Type {
|
switch object.Type {
|
||||||
|
case "CompositeObject", "CompositeGraphicUnit":
|
||||||
|
used[object.CompositeGraphicUnit.ResourceID] = true
|
||||||
case "TextObject":
|
case "TextObject":
|
||||||
used[object.TextObject.Font] = true
|
used[object.TextObject.Font] = true
|
||||||
if e.source != nil && (!exists || before.Type != object.Type || before.TextObject.Font != object.TextObject.Font) {
|
if e.source != nil && (!exists || before.Type != object.Type || before.TextObject.Font != object.TextObject.Font) {
|
||||||
@@ -355,6 +357,9 @@ func (e *Editor) usedResources() (fonts, images []editorResource, spaces []Color
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, resource := range e.resources {
|
for _, resource := range e.resources {
|
||||||
|
if resource.composite != "" && used[resource.composite] {
|
||||||
|
files[resource.name] = true
|
||||||
|
}
|
||||||
if resource.font != nil && used[resource.font.ID] {
|
if resource.font != nil && used[resource.font.ID] {
|
||||||
fonts = append(fonts, resource)
|
fonts = append(fonts, resource)
|
||||||
delete(promoted, resource.font.ID)
|
delete(promoted, resource.font.ID)
|
||||||
|
|||||||
Reference in new issue
Block a user