feat(创建编辑): 新增对象旋转、翻转与图片裁剪,修复框选颜色及变换边界问题
build.yaml / build (push) Successful in 35s

This commit is contained in:
xiaoqidun committed 2026-09-16 00:19:49 +08:00
1 parent 105590d6d7
commit 827f8ab48f
15 files changed
+768 -64

No files matched your search

+36 -7
View File
@@ -65,7 +65,7 @@ func NewShape(kind ShapeKind, box Box) (PathObject, error) {
return object, nil
}
// Shape 识别 NewShape 生成的基本路径及其正向轴对齐缩放、平移,返回应用 Boundary 和 CTM 后的几何范围。
// Shape 识别 NewShape 生成的基本路径及其轴对齐缩放、直角旋转、镜像和平移。
// 直线范围以起点和有符号的端点位移表示;非基本路径或不支持的变换返回空类型。
// 返回: ShapeKind 图形类型, Box 几何范围(不含描边)
func (p PathObject) Shape() (ShapeKind, Box) {
@@ -110,11 +110,16 @@ func (p PathObject) Shape() (ShapeKind, Box) {
}
boundary, err := ParseBox(p.Boundary)
m := NewMatrix(p.CTM)
if err != nil || m.a <= 0 || m.d <= 0 || m.b != 0 || m.c != 0 {
if err != nil || !axisAlignedMatrix(m) {
return "", Box{}
}
x, y := m.Transform(box.X, box.Y)
box = Box{X: boundary.X + x, Y: boundary.Y + y, W: box.W * m.a, H: box.H * m.d}
if kind == ShapeLine {
x, y := m.Transform(box.X, box.Y)
w, h := matrixVector(m, box.W, box.H)
box = Box{X: boundary.X + x, Y: boundary.Y + y, W: w, H: h}
} else {
box = TranslationMatrix(boundary.X, boundary.Y).Multiply(m).TransformBox(box)
}
if !finite(box.X) || !finite(box.Y) || !finite(box.W) || !finite(box.H) || !finite(box.X+box.W) || !finite(box.Y+box.H) {
return "", Box{}
}
@@ -141,7 +146,7 @@ func sameShapePath(a, b []string) bool {
return true
}
// Reshape 调整基本路径在所在坐标系中的几何范围,保留对象标识、变换和绘制属性,不缩放线宽。
// Reshape 调整基本路径在所在坐标系中的几何范围,保留对象标识、方向、缩放和绘制属性,不缩放线宽。
// 返回对象可通过 Editor.UpdateObject 写入;仅支持 Shape 可识别的路径。
// 入参: box 新几何范围,直线使用起点和有符号的端点位移
// 返回: PathObject 调整后的对象, error 错误信息
@@ -157,12 +162,36 @@ func (p PathObject) Reshape(box Box) (PathObject, error) {
return PathObject{}, fmt.Errorf("shape coordinates must be finite")
}
m := NewMatrix(p.CTM)
shape, err := NewShape(kind, Box{W: box.W / m.a, H: box.H / m.d})
var w, h float64
if m.b == 0 {
w, h = box.W/m.a, box.H/m.d
} else {
w, h = box.H/m.b, box.W/m.c
}
if kind != ShapeLine {
w, h = math.Abs(w), math.Abs(h)
}
shape, err := NewShape(kind, Box{W: w, H: h})
if err != nil {
return PathObject{}, err
}
boundary, _ := ParseBox(shape.Boundary)
p.Boundary = fmt.Sprintf("%g %g %g %g", box.X+boundary.X*m.a-m.e, box.Y+boundary.Y*m.d-m.f, boundary.W*m.a, boundary.H*m.d)
if m.a > 0 && m.d > 0 && m.b == 0 && m.c == 0 {
p.Boundary = fmt.Sprintf("%g %g %g %g", box.X+boundary.X*m.a-m.e, box.Y+boundary.Y*m.d-m.f, boundary.W*m.a, boundary.H*m.d)
} else {
m.e, m.f = 0, 0
var x, y float64
if kind == ShapeLine {
x, y = m.Transform(-boundary.X, -boundary.Y)
} else {
bounds := m.TransformBox(boundary)
x, y = bounds.X, bounds.Y
}
m = TranslationMatrix(box.X-x, box.Y-y).Multiply(m)
bounds := m.TransformBox(Box{W: boundary.W, H: boundary.H})
p.Boundary = editorBoxString(bounds)
p.CTM = TranslationMatrix(-bounds.X, -bounds.Y).Multiply(m).String()
}
p.AbbreviatedData = shape.AbbreviatedData
return p, nil
}