mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-30 01:27:26 +08:00
1 parent
b3c4bbc844
commit
961bc52038
43 files changed
+2283
-2594
No files matched your search
+184
-50
@@ -15,7 +15,7 @@
|
||||
package ofdgo
|
||||
|
||||
import (
|
||||
"math"
|
||||
"reflect"
|
||||
|
||||
codecpdf "github.com/xiaoqidun/ofdgo/internal/codec/pdf"
|
||||
"github.com/xiaoqidun/pdfgo"
|
||||
@@ -28,55 +28,6 @@ type pdfClipPath struct {
|
||||
path *[1]PathObject
|
||||
}
|
||||
|
||||
// pdfClipContainsBoundary 判断单一矩形裁剪是否完整覆盖对象边界,不近似曲线或斜边
|
||||
// 入参: path PDF裁剪路径, matrix 页面变换, boundary OFD对象边界
|
||||
// 返回: bool 是否可由对象边界代替裁剪
|
||||
func pdfClipContainsBoundary(path pdfgo.Path, matrix pdfgo.Matrix, boundary Box) bool {
|
||||
if len(path.Text) != 0 || len(path.Segments) < 4 || len(path.Segments) > 6 || !finite(boundary.X) || !finite(boundary.Y) || !finite(boundary.W) || !finite(boundary.H) || boundary.W <= 0 || boundary.H <= 0 {
|
||||
return false
|
||||
}
|
||||
var points [4]pdfgo.Point
|
||||
for index := range 4 {
|
||||
segment := path.Segments[index]
|
||||
operator := "L"
|
||||
if index == 0 {
|
||||
operator = "M"
|
||||
}
|
||||
if segment.Operator != operator || len(segment.Points) != 1 {
|
||||
return false
|
||||
}
|
||||
points[index] = matrix.Apply(segment.Points[0])
|
||||
if !finite(points[index].X) || !finite(points[index].Y) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
end := 4
|
||||
if end < len(path.Segments) && path.Segments[end].Operator == "L" {
|
||||
segment := path.Segments[end]
|
||||
if len(segment.Points) != 1 || matrix.Apply(segment.Points[0]) != points[0] {
|
||||
return false
|
||||
}
|
||||
end++
|
||||
}
|
||||
if end < len(path.Segments) {
|
||||
segment := path.Segments[end]
|
||||
if segment.Operator != "C" || len(segment.Points) != 0 {
|
||||
return false
|
||||
}
|
||||
end++
|
||||
}
|
||||
if end != len(path.Segments) {
|
||||
return false
|
||||
}
|
||||
a, b, c, d := points[0], points[1], points[2], points[3]
|
||||
if !(a.Y == b.Y && b.X == c.X && c.Y == d.Y && d.X == a.X || a.X == b.X && b.Y == c.Y && c.X == d.X && d.Y == a.Y) {
|
||||
return false
|
||||
}
|
||||
left, right := math.Min(a.X, c.X), math.Max(a.X, c.X)
|
||||
top, bottom := math.Min(a.Y, c.Y), math.Max(a.Y, c.Y)
|
||||
return left < right && top < bottom && boundary.X >= left && boundary.Y >= top && boundary.X+boundary.W <= right && boundary.Y+boundary.H <= bottom
|
||||
}
|
||||
|
||||
// clipPath 复用页面内重复路径编码,按估算字节预算淘汰且不缓存错误
|
||||
// 入参: path 裁剪路径
|
||||
// 返回: pdfClipPath 变换后的边界及编码, error 取消错误
|
||||
@@ -109,3 +60,186 @@ func (p *pdfImporter) clipPath(path pdfgo.Path) (pdfClipPath, error) {
|
||||
p.clipPaths.put(key, result, len(data)+len(boundary)+512)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// sharedClipObjects 将连续重复的大裁剪区提取为复合对象,保留页面坐标和绘制顺序
|
||||
// 入参: objects 本库生成且尚未分配标识的对象
|
||||
// 返回: []GraphicObject 提取后的对象, error 资源创建或取消错误
|
||||
func (p *pdfImporter) sharedClipObjects(objects []GraphicObject) ([]GraphicObject, error) {
|
||||
var output []GraphicObject
|
||||
for start := 0; start < len(objects); {
|
||||
if err := p.ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
clips := pdfPageClip(objects[start])
|
||||
end := start + 1
|
||||
if clips != nil {
|
||||
for end < len(objects) && pdfMatchesPageClip(objects[end], clips) {
|
||||
if err := p.ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
end++
|
||||
}
|
||||
}
|
||||
if end-start < 2 {
|
||||
if output != nil {
|
||||
output = append(output, objects[start])
|
||||
}
|
||||
start = end
|
||||
continue
|
||||
}
|
||||
if output == nil {
|
||||
output = append(make([]GraphicObject, 0, len(objects)), objects[:start]...)
|
||||
}
|
||||
members := objects[start:end:end]
|
||||
for index := range members {
|
||||
*editorObjectClips(&members[index]) = nil
|
||||
}
|
||||
id, err := p.objectValidation().addOwnedVector(p.ctx, members, p.pageWidth, p.pageHeight)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
output = append(output, GraphicObject{Type: "CompositeObject", CompositeGraphicUnit: CompositeGraphicUnit{
|
||||
Boundary: pdfBoundary(Box{W: p.pageWidth, H: p.pageHeight}), ResourceID: id, Clips: clips,
|
||||
}})
|
||||
start = end
|
||||
}
|
||||
if output == nil {
|
||||
return objects, nil
|
||||
}
|
||||
return output, nil
|
||||
}
|
||||
|
||||
// pdfPageClip 将导入裁剪区还原到页面坐标,仅提取包含大量重复路径数据的裁剪
|
||||
// 入参: object 导入对象
|
||||
// 返回: *Clips 页面坐标裁剪,不满足提取条件时为空
|
||||
func pdfPageClip(object GraphicObject) *Clips {
|
||||
if object.Type != "TextObject" && object.Type != "PathObject" && object.Type != "ImageObject" {
|
||||
return nil
|
||||
}
|
||||
clips := *editorObjectClips(&object)
|
||||
if clips == nil || clips.usesObjectMatrix() || len(clips.Clip) == 0 {
|
||||
return nil
|
||||
}
|
||||
boundary, _ := editorGeometry(object)
|
||||
box, err := ParseBox(boundary)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
translation := ""
|
||||
if box.X != 0 || box.Y != 0 {
|
||||
translation = pdfNumbers(1, 0, 0, 1, -box.X, -box.Y)
|
||||
}
|
||||
bytes := 0
|
||||
result := &Clips{Clip: make([]Clip, len(clips.Clip))}
|
||||
for index, clip := range clips.Clip {
|
||||
if len(clip.Area) != 1 || clip.Area[0].CTM != translation {
|
||||
return nil
|
||||
}
|
||||
area := clip.Area[0]
|
||||
area.CTM = ""
|
||||
result.Clip[index].Area = []ClipArea{area}
|
||||
for _, path := range area.Path {
|
||||
bytes += len(path.AbbreviatedData)
|
||||
}
|
||||
}
|
||||
if bytes < 2048 {
|
||||
return nil
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// pdfMatchesPageClip 比较页面坐标裁剪,不将相同外接矩形视为相同路径
|
||||
// 入参: object 导入对象, pageClip 已还原的裁剪区
|
||||
// 返回: bool 是否可共享裁剪区
|
||||
func pdfMatchesPageClip(object GraphicObject, pageClip *Clips) bool {
|
||||
if object.Type != "TextObject" && object.Type != "PathObject" && object.Type != "ImageObject" {
|
||||
return false
|
||||
}
|
||||
clips := *editorObjectClips(&object)
|
||||
if clips == nil || clips.usesObjectMatrix() || len(clips.Clip) != len(pageClip.Clip) {
|
||||
return false
|
||||
}
|
||||
boundary, _ := editorGeometry(object)
|
||||
box, err := ParseBox(boundary)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
translation := ""
|
||||
if box.X != 0 || box.Y != 0 {
|
||||
translation = pdfNumbers(1, 0, 0, 1, -box.X, -box.Y)
|
||||
}
|
||||
for index, clip := range clips.Clip {
|
||||
if len(clip.Area) != 1 || clip.Area[0].CTM != translation {
|
||||
return false
|
||||
}
|
||||
area, expected := clip.Area[0], pageClip.Clip[index].Area[0]
|
||||
if area.DrawParam != expected.DrawParam || !reflect.DeepEqual(area.Path, expected.Path) || !reflect.DeepEqual(area.Text, expected.Text) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// clips 省略覆盖对象边界的矩形裁剪,复用其余几何并保留位移及路径交集
|
||||
// 入参: paths 裁剪路径, origin 对象边界
|
||||
// 返回: *Clips OFD裁剪区域,无裁剪时为空, error 未解析的裁剪字形
|
||||
func (p *pdfImporter) clips(paths []pdfgo.Path, origin Box) (*Clips, error) {
|
||||
if len(paths) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
var clips *Clips
|
||||
var areas []ClipArea
|
||||
translation := ""
|
||||
for _, path := range paths {
|
||||
if err := p.ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if codecpdf.ClipContainsBoundary(path, p.matrix, origin) {
|
||||
continue
|
||||
}
|
||||
if clips == nil {
|
||||
clips = &Clips{Clip: make([]Clip, 0, len(paths))}
|
||||
areas = make([]ClipArea, len(paths))
|
||||
if origin.X != 0 || origin.Y != 0 {
|
||||
translation = pdfNumbers(1, 0, 0, 1, -origin.X, -origin.Y)
|
||||
}
|
||||
}
|
||||
index := len(clips.Clip)
|
||||
area := &areas[index]
|
||||
area.CTM = translation
|
||||
if len(path.Segments) != 0 {
|
||||
encoded, err := p.clipPath(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
area.Path = encoded.path[:1:1]
|
||||
}
|
||||
for _, mark := range path.Text {
|
||||
objects, ok := p.clipTexts[mark]
|
||||
if !ok {
|
||||
if err := p.flushPath(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
count := p.objects.len()
|
||||
textCount := p.report.TextObjects
|
||||
if err := p.text(pdfgo.TextMark{Font: mark.Font, Glyphs: mark.Glyphs, Positions: mark.Positions, Matrix: mark.Matrix, Size: mark.Size, HorizontalScale: mark.HorizontalScale, Mode: 7, Clip: mark, Style: pdfgo.Style{Fill: pdfgo.Paint{Alpha: 1}}}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.objects.truncate(count)
|
||||
p.report.TextObjects = textCount
|
||||
objects = p.clipTexts[mark]
|
||||
}
|
||||
if len(path.Text) == 1 {
|
||||
area.Text = objects[:len(objects):len(objects)]
|
||||
} else {
|
||||
area.Text = append(area.Text, objects...)
|
||||
}
|
||||
}
|
||||
if len(area.Path)+len(area.Text) == 0 {
|
||||
fill, stroke := true, false
|
||||
area.Path = []PathObject{{Boundary: "0 0 1 1", AbbreviatedData: "M 0 0 C", Fill: &fill, Stroke: &stroke}}
|
||||
}
|
||||
clips.Clip = append(clips.Clip, Clip{Area: areas[index : index+1 : index+1]})
|
||||
}
|
||||
return clips, nil
|
||||
}
|
||||
Reference in new issue
Block a user