Files
ofdgo/internal/codec/pdf/geometry.go
T
2026-10-14 09:02:24 +08:00

102 lines
3.8 KiB
Go

// 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 pdf
import (
"context"
"fmt"
basegeom "github.com/xiaoqidun/ofdgo/internal/base/geom"
"github.com/xiaoqidun/pdfgo"
)
// GeometryPath 直接转换PDF路径坐标,不经文本编码或曲线展平
// 入参: ctx 取消上下文, source PDF路径, matrix 坐标变换
// 返回: basegeom.Path 独立几何路径, error 指令、坐标或取消错误
func GeometryPath(ctx context.Context, source pdfgo.Path, matrix pdfgo.Matrix) (basegeom.Path, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
path := make(basegeom.Path, len(source.Segments))
for i, segment := range source.Segments {
if err := ctx.Err(); err != nil {
return nil, err
}
var count int
switch segment.Operator {
case "M":
path[i].Verb, count = basegeom.Move, 1
case "L":
path[i].Verb, count = basegeom.Line, 1
case "B":
path[i].Verb, count = basegeom.Cubic, 3
case "C":
path[i].Verb = basegeom.ClosePath
default:
return nil, fmt.Errorf("invalid PDF path operator %q", segment.Operator)
}
if len(segment.Points) != count {
return nil, fmt.Errorf("invalid PDF path operands for %s", segment.Operator)
}
var points [3]basegeom.Point
for j, point := range segment.Points {
point = matrix.Apply(point)
points[j] = basegeom.Point{X: point.X, Y: point.Y}
}
if count != 0 {
path[i].End = points[count-1]
}
if count == 3 {
path[i].Control1, path[i].Control2 = points[0], points[1]
}
}
if err := path.Validate(); err != nil {
return nil, err
}
return path, ctx.Err()
}
// GlyphPath 将字形轮廓变换到PDF页面坐标,二次曲线精确转换为三次曲线
// 入参: outline 字形轮廓, matrix 字形到页面的变换
// 返回: pdfgo.Path 页面路径, error 非字体轮廓指令
func GlyphPath(outline basegeom.Path, matrix pdfgo.Matrix) (pdfgo.Path, error) {
path := pdfgo.Path{Segments: make([]pdfgo.Segment, 0, len(outline))}
var current, start basegeom.Point
point := func(p basegeom.Point) pdfgo.Point { return matrix.Apply(pdfgo.Point{X: p.X, Y: p.Y}) }
for _, segment := range outline {
switch segment.Verb {
case basegeom.Move:
start = segment.End
path.Segments = append(path.Segments, pdfgo.Segment{Operator: "M", Points: []pdfgo.Point{point(segment.End)}})
case basegeom.Line:
path.Segments = append(path.Segments, pdfgo.Segment{Operator: "L", Points: []pdfgo.Point{point(segment.End)}})
case basegeom.Quad:
c1 := basegeom.Point{X: current.X + (segment.Control1.X-current.X)*2/3, Y: current.Y + (segment.Control1.Y-current.Y)*2/3}
c2 := basegeom.Point{X: segment.End.X + (segment.Control1.X-segment.End.X)*2/3, Y: segment.End.Y + (segment.Control1.Y-segment.End.Y)*2/3}
path.Segments = append(path.Segments, pdfgo.Segment{Operator: "B", Points: []pdfgo.Point{point(c1), point(c2), point(segment.End)}})
case basegeom.Cubic:
path.Segments = append(path.Segments, pdfgo.Segment{Operator: "B", Points: []pdfgo.Point{point(segment.Control1), point(segment.Control2), point(segment.End)}})
case basegeom.ClosePath:
path.Segments = append(path.Segments, pdfgo.Segment{Operator: "C"})
current = start
continue
default:
return pdfgo.Path{}, fmt.Errorf("unsupported PDF glyph geometry verb %d", segment.Verb)
}
current = segment.End
}
return path, nil
}