feat(文件归并): 归并PDF封装并下沉计算逻辑
build.yaml / build (push) Successful in 45s

This commit is contained in:
xiaoqidun committed 2026-10-19 10:10:27 +08:00
1 parent 84808cda61
commit ec24b6a17d
17 files changed
+1908 -2026

No files matched your search

+66
View File
@@ -17,6 +17,8 @@ package pdf
import (
"context"
"fmt"
"math"
"strconv"
basegeom "github.com/xiaoqidun/ofdgo/internal/base/geom"
"github.com/xiaoqidun/pdfgo"
@@ -99,3 +101,67 @@ func GlyphPath(outline basegeom.Path, matrix pdfgo.Matrix) (pdfgo.Path, error) {
}
return path, nil
}
// PathBounds 逐段计算变换后的控制点边界,不创建临时点集合
// 入参: ctx 取消上下文, path PDF路径, matrix 坐标变换
// 返回: basegeom.Box 控制点边界,空路径返回零值, error 取消错误
func PathBounds(ctx context.Context, path pdfgo.Path, matrix pdfgo.Matrix) (basegeom.Box, error) {
if err := ctx.Err(); err != nil {
return basegeom.Box{}, err
}
minX, minY, maxX, maxY := math.Inf(1), math.Inf(1), math.Inf(-1), math.Inf(-1)
found := false
for _, segment := range path.Segments {
if err := ctx.Err(); err != nil {
return basegeom.Box{}, err
}
for index, point := range segment.Points {
if index&255 == 0 {
if err := ctx.Err(); err != nil {
return basegeom.Box{}, err
}
}
point = matrix.Apply(point)
minX, minY = math.Min(minX, point.X), math.Min(minY, point.Y)
maxX, maxY = math.Max(maxX, point.X), math.Max(maxY, point.Y)
found = true
}
}
if err := ctx.Err(); err != nil {
return basegeom.Box{}, err
}
if !found {
return basegeom.Box{}, nil
}
return basegeom.Box{X: minX, Y: minY, W: math.Max(maxX-minX, 1e-6), H: math.Max(maxY-minY, 1e-6)}, nil
}
// PathData 转换路径并在分段处理时检查取消
// 入参: ctx 取消上下文, path PDF路径, matrix 坐标变换, origin 对象边界
// 返回: string OFD路径数据, error 取消错误
func PathData(ctx context.Context, path pdfgo.Path, matrix pdfgo.Matrix, origin basegeom.Box) (string, error) {
if err := ctx.Err(); err != nil {
return "", err
}
data := make([]byte, 0, len(path.Segments)*48)
for index, segment := range path.Segments {
if err := ctx.Err(); err != nil {
return "", err
}
if index != 0 {
data = append(data, ' ')
}
data = append(data, segment.Operator...)
for _, point := range segment.Points {
point = matrix.Apply(point)
data = append(data, ' ')
data = strconv.AppendFloat(data, point.X-origin.X, 'g', -1, 64)
data = append(data, ' ')
data = strconv.AppendFloat(data, point.Y-origin.Y, 'g', -1, 64)
}
}
if err := ctx.Err(); err != nil {
return "", err
}
return string(data), nil
}
+51
View File
@@ -0,0 +1,51 @@
// 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 (
"fmt"
"strings"
"time"
"github.com/xiaoqidun/pdfgo"
)
// MetadataDate 保留当地日历日期,PDF省略时区按GMT,XMP省略日或时区时不推算
// 入参: text 来源日期, xmp 是否为XMP日期
// 返回: time.Time 解析值, bool 是否精确到日, bool 是否可比较绝对时间, error 日期错误
func MetadataDate(text string, xmp bool) (time.Time, bool, bool, error) {
if text == "" {
return time.Time{}, false, false, nil
}
if !xmp {
date, err := pdfgo.ParseDate(text)
return date, err == nil && date.Year() > 0, err == nil, err
}
if !strings.Contains(text, ",") {
for _, layout := range []string{time.RFC3339Nano, "2006-01-02T15:04Z07:00", "2006-01-02T15:04:05", "2006-01-02T15:04", "2006-01-02", "2006-01", "2006"} {
if date, err := time.Parse(layout, text); err == nil {
zoned := strings.Contains(layout, "Z07:00")
if zoned && !strings.HasSuffix(text, "Z") {
zone := text[len(text)-6:]
if zone[1:3] > "23" || zone[4:] > "59" {
return time.Time{}, false, false, fmt.Errorf("invalid XMP time zone")
}
}
return date, len(text) >= 10 && date.Year() > 0, zoned, nil
}
}
}
return time.Time{}, false, false, fmt.Errorf("invalid XMP date")
}
+102
View File
@@ -0,0 +1,102 @@
// 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 (
"math"
"reflect"
"github.com/xiaoqidun/pdfgo"
)
// MergeReplacement 统一同一替换区段内的坐标原点,不重新塑形或调整字距
// 入参: marks 连续文字图元, replacement 整段原文
// 返回: *pdfgo.TextMark 合并文字, bool 是否可等价合并
func MergeReplacement(marks []*pdfgo.TextMark, replacement *pdfgo.ReplacementText) (*pdfgo.TextMark, bool) {
first := marks[0]
style := first.Style
paint := style.Fill
if first.Font == nil || len(first.Font.Program) == 0 || first.Mode != 0 || first.Size <= 0 || first.HorizontalScale <= 0 || first.Clip != nil || style.SoftMask != nil || style.Transfer != nil || style.AlphaIsShape || style.FillOverprint || paint.Alpha != 1 || !NormalBlend(style.BlendMode) || paint.Tiling != nil || paint.Axial != nil || paint.Radial != nil || paint.Mesh != nil || paint.Function != nil {
return nil, false
}
inverse, ok := first.Matrix.Inverse()
if !ok {
return nil, false
}
count := 0
sequence := first.Sequence
for _, mark := range marks {
if mark.Font != first.Font || mark.Size != first.Size || mark.HorizontalScale != first.HorizontalScale || mark.Mode != first.Mode || mark.Clip != nil || len(mark.Glyphs) != len(mark.Positions) || !reflect.DeepEqual(mark.Style, style) {
return nil, false
}
if mark.Matrix[0] != first.Matrix[0] || mark.Matrix[1] != first.Matrix[1] || mark.Matrix[2] != first.Matrix[2] || mark.Matrix[3] != first.Matrix[3] {
return nil, false
}
count += len(mark.Glyphs)
sequence = CommonSequence(sequence, mark.Sequence)
}
if count == 0 {
return nil, false
}
merged := *first
merged.Replacement = replacement
merged.Sequence = sequence
merged.Glyphs = make([]pdfgo.Glyph, 0, count)
merged.Positions = make([]pdfgo.Point, 0, count)
for _, mark := range marks {
dx, dy := mark.Matrix[4]-first.Matrix[4], mark.Matrix[5]-first.Matrix[5]
offset := pdfgo.Point{X: inverse[0]*dx + inverse[2]*dy, Y: inverse[1]*dx + inverse[3]*dy}
for _, point := range mark.Positions {
point.X += offset.X
point.Y += offset.Y
if math.IsNaN(point.X) || math.IsNaN(point.Y) || math.IsInf(point.X, 0) || math.IsInf(point.Y, 0) {
return nil, false
}
merged.Positions = append(merged.Positions, point)
}
merged.Glyphs = append(merged.Glyphs, mark.Glyphs...)
}
return &merged, true
}
// CommonSequence 查找两个内容区段的最近共同外层,不将合并内容归入单个子区段
// 入参: first 首个区段, second 后续区段
// 返回: *pdfgo.ContentSequence 共同区段,没有共同区段时为空
func CommonSequence(first, second *pdfgo.ContentSequence) *pdfgo.ContentSequence {
if first == second {
return first
}
if first == nil || second == nil {
return nil
}
depth := func(sequence *pdfgo.ContentSequence) int {
count := 0
for ; sequence != nil; sequence = sequence.Parent {
count++
}
return count
}
a, b := depth(first), depth(second)
for ; a > b; a-- {
first = first.Parent
}
for ; b > a; b-- {
second = second.Parent
}
for first != second {
first, second = first.Parent, second.Parent
}
return first
}