mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-29 08:46:55 +08:00
1 parent
b3c4bbc844
commit
961bc52038
43 files changed
+2283
-2594
No files matched your search
+10
-575
@@ -18,9 +18,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
codecpdf "github.com/xiaoqidun/ofdgo/internal/codec/pdf"
|
||||
"github.com/xiaoqidun/pdfgo"
|
||||
@@ -64,12 +61,6 @@ type PDFImportReport struct {
|
||||
Warnings []pdfgo.Diagnostic
|
||||
}
|
||||
|
||||
// pdfPointBounds 逐点汇总边界,不保留文字或路径的临时坐标数组
|
||||
type pdfPointBounds struct {
|
||||
minX, minY, maxX, maxY float64
|
||||
found bool
|
||||
}
|
||||
|
||||
// pdfImporter 保存单次转换的对象与资源状态
|
||||
type pdfImporter struct {
|
||||
ctx context.Context
|
||||
@@ -119,9 +110,7 @@ type pdfImporter struct {
|
||||
annotationData []byte
|
||||
resolveFile pdfgo.FileResolver
|
||||
resolveReference pdfgo.ReferenceResolver
|
||||
referenceReaders map[[32]byte]*pdfgo.Reader
|
||||
referenceStreams map[*pdfgo.Stream]*pdfgo.Reader
|
||||
referencePages map[pdfReferencePageKey]*pdfgo.Page
|
||||
references *codecpdf.ReferenceCache
|
||||
halftoneWarnings map[*pdfgo.Halftone]bool
|
||||
halftones map[string]*pdfgo.Halftone
|
||||
transferBackdrop bool
|
||||
@@ -181,10 +170,8 @@ func ImportPDF(ctx context.Context, source io.ReaderAt, size int64, options PDFI
|
||||
importer.resolveReference = options.ResolveReference
|
||||
importer.halftones = options.Halftones
|
||||
importer.compression = options.Compression
|
||||
importer.referenceReaders = make(map[[32]byte]*pdfgo.Reader)
|
||||
importer.referenceStreams = make(map[*pdfgo.Stream]*pdfgo.Reader)
|
||||
importer.referencePages = make(map[pdfReferencePageKey]*pdfgo.Page)
|
||||
defer importer.closeReferenceReaders()
|
||||
importer.references = &codecpdf.ReferenceCache{}
|
||||
defer importer.references.Close()
|
||||
importer.halftoneWarnings = make(map[*pdfgo.Halftone]bool)
|
||||
if security := reader.Encryption(); security != nil && !security.Owner && security.Permissions&0xf3c != 0xf3c {
|
||||
if options.Strict {
|
||||
@@ -325,564 +312,12 @@ func ConvertPDF(ctx context.Context, source io.ReaderAt, size int64, output io.W
|
||||
return report, err
|
||||
}
|
||||
|
||||
// commitObjects 将当前页面待转换对象写入编辑文档
|
||||
// 返回: error 对象写入错误
|
||||
func (p *pdfImporter) commitObjects() error {
|
||||
if p.objects.len() == 0 {
|
||||
return nil
|
||||
// referencePage 衔接PDF访问器与当前导入会话的引用文档缓存
|
||||
// 入参: ctx 取消上下文, source 来源文档, reference 引用目标
|
||||
// 返回: *pdfgo.Page 目标页面,不可用时为空, error 取消或调用方解析错误
|
||||
func (p *pdfImporter) referencePage(ctx context.Context, source *pdfgo.Reader, reference pdfgo.ReferenceXObject) (*pdfgo.Page, error) {
|
||||
if p.references == nil {
|
||||
p.references = &codecpdf.ReferenceCache{}
|
||||
}
|
||||
objects, err := p.objects.data(p.ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
objects, err = p.sharedClipObjects(objects)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.objectValidation().appendOwnedObjects(p.ctx, p.page, objects); err != nil {
|
||||
return err
|
||||
}
|
||||
p.objects = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// pdfNumbers 按OFD数值格式序列化坐标
|
||||
// 入参: values 数值列表
|
||||
// 返回: string 空格分隔的数值
|
||||
func pdfNumbers(values ...float64) string {
|
||||
var buffer [128]byte
|
||||
data := buffer[:0]
|
||||
for n, v := range values {
|
||||
if n != 0 {
|
||||
data = append(data, ' ')
|
||||
}
|
||||
data = strconv.AppendFloat(data, v, 'g', -1, 64)
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
// pdfDashPattern 换算PDF虚线长度,将单值数组展开为等长线段与间隔
|
||||
// 入参: dash PDF虚线数组, scale 坐标缩放比例
|
||||
// 返回: string OFD虚线数组
|
||||
func pdfDashPattern(dash []float64, scale float64) string {
|
||||
if len(dash) == 0 {
|
||||
return ""
|
||||
}
|
||||
values := make([]float64, max(2, len(dash)))
|
||||
for i, value := range dash {
|
||||
values[i] = value * scale
|
||||
}
|
||||
if len(dash) == 1 {
|
||||
values[1] = values[0]
|
||||
}
|
||||
return pdfNumbers(values...)
|
||||
}
|
||||
|
||||
// pdfBoundary 序列化对象边界
|
||||
// 入参: b 对象边界
|
||||
// 返回: string OFD边界数据
|
||||
func pdfBoundary(b Box) string { return pdfNumbers(b.X, b.Y, b.W, b.H) }
|
||||
|
||||
// pdfBounds 计算点集合的包围框
|
||||
// 入参: points 坐标点集合
|
||||
// 返回: Box 包围框
|
||||
func pdfBounds(points []pdfgo.Point) Box {
|
||||
var bounds pdfPointBounds
|
||||
for _, point := range points {
|
||||
bounds.add(point)
|
||||
}
|
||||
return bounds.box()
|
||||
}
|
||||
|
||||
// add 按原顺序累计一个控制点的最小和最大坐标
|
||||
// 入参: point 坐标点
|
||||
func (b *pdfPointBounds) add(point pdfgo.Point) {
|
||||
if !b.found {
|
||||
b.minX, b.minY, b.maxX, b.maxY = point.X, point.Y, point.X, point.Y
|
||||
b.found = true
|
||||
}
|
||||
b.minX, b.minY = math.Min(b.minX, point.X), math.Min(b.minY, point.Y)
|
||||
b.maxX, b.maxY = math.Max(b.maxX, point.X), math.Max(b.maxY, point.Y)
|
||||
}
|
||||
|
||||
// box 取得原精度边界,空集合仍返回零值
|
||||
// 返回: Box 坐标范围
|
||||
func (b *pdfPointBounds) box() Box {
|
||||
if !b.found {
|
||||
return Box{}
|
||||
}
|
||||
return Box{X: b.minX, Y: b.minY, W: math.Max(b.maxX-b.minX, 1e-6), H: math.Max(b.maxY-b.minY, 1e-6)}
|
||||
}
|
||||
|
||||
// pathBoundsContext 逐段计算变换后的控制点边界,不创建临时点集合
|
||||
// 入参: ctx 取消上下文, path PDF路径
|
||||
// 返回: Box 控制点边界,空路径返回零值, error 取消错误
|
||||
func (p *pdfImporter) pathBoundsContext(ctx context.Context, path pdfgo.Path) (Box, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return 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 Box{}, err
|
||||
}
|
||||
for index, point := range segment.Points {
|
||||
if index&255 == 0 {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return Box{}, err
|
||||
}
|
||||
}
|
||||
point = p.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 Box{}, err
|
||||
}
|
||||
if !found {
|
||||
return Box{}, nil
|
||||
}
|
||||
return Box{X: minX, Y: minY, W: math.Max(maxX-minX, 1e-6), H: math.Max(maxY-minY, 1e-6)}, nil
|
||||
}
|
||||
|
||||
// color 保留设备色彩分量并复用文档颜色空间
|
||||
// 入参: paint PDF颜色与不透明度
|
||||
// 返回: *FillColor OFD颜色
|
||||
func (p *pdfImporter) color(paint pdfgo.Paint) *FillColor {
|
||||
alpha := int(math.Round(paint.Alpha * 255))
|
||||
if paint.CMYK != nil && (paint.Space == nil || !paint.Space.Calibrated()) {
|
||||
if p.cmykSpace == "" {
|
||||
p.cmykSpace = p.editor.nextID()
|
||||
p.editor.resources = append(p.editor.resources, editorResource{space: &ColorSpace{ID: p.cmykSpace, Type: "CMYK", BitsPerComponent: 16}})
|
||||
}
|
||||
v := paint.CMYK
|
||||
return &FillColor{ColorSpace: p.cmykSpace, Value: pdfNumbers(math.Round(v[0]*65535), math.Round(v[1]*65535), math.Round(v[2]*65535), math.Round(v[3]*65535)), Alpha: &alpha}
|
||||
}
|
||||
return &FillColor{Value: pdfNumbers(math.Round(paint.RGB[0]*255), math.Round(paint.RGB[1]*255), math.Round(paint.RGB[2]*255)), Alpha: &alpha}
|
||||
}
|
||||
|
||||
// pathDataContext 转换路径并在分段处理时检查取消
|
||||
// 入参: ctx 取消上下文, path PDF路径, origin 对象边界
|
||||
// 返回: string OFD路径数据, error 取消错误
|
||||
func (p *pdfImporter) pathDataContext(ctx context.Context, path pdfgo.Path, origin 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 = p.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
|
||||
}
|
||||
|
||||
// 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 pdfClipContainsBoundary(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
|
||||
}
|
||||
|
||||
// path 合并相同状态下的不透明描边,保留独立子路径与绘制顺序
|
||||
// 入参: mark PDF路径绘制信息
|
||||
// 返回: error 错误信息
|
||||
func (p *pdfImporter) path(mark pdfgo.PathMark) error {
|
||||
if mark.Style.BlendMode != "" && mark.Style.BlendMode != "Normal" && mark.Style.BlendMode != "Compatible" {
|
||||
return &pdfgo.UnsupportedError{Feature: "path blend mode " + string(mark.Style.BlendMode)}
|
||||
}
|
||||
if !mark.Stroke || mark.Fill || mark.Style.Stroke.Alpha != 1 {
|
||||
if err := p.flushPath(); err != nil {
|
||||
return err
|
||||
}
|
||||
return p.appendPath(mark)
|
||||
}
|
||||
if p.pendingPath != nil && (p.pendingPath.StrokeMatrix != mark.StrokeMatrix || !reflect.DeepEqual(p.pendingPath.Style, mark.Style)) {
|
||||
if err := p.flushPath(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if p.pendingPath == nil {
|
||||
p.pendingPath = &mark
|
||||
} else {
|
||||
p.pendingPath.Path.Segments = append(p.pendingPath.Path.Segments, mark.Path.Segments...)
|
||||
}
|
||||
if len(p.pendingPath.Path.Segments) >= 512 {
|
||||
return p.flushPath()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// flushPath 写入连续描边批次,保持后续图元的叠放位置
|
||||
// 返回: error 转换错误
|
||||
func (p *pdfImporter) flushPath() error {
|
||||
if p.pendingPath == nil {
|
||||
return nil
|
||||
}
|
||||
mark := *p.pendingPath
|
||||
p.pendingPath = nil
|
||||
return p.appendPath(mark)
|
||||
}
|
||||
|
||||
// appendPath 添加独立可编辑路径
|
||||
// 入参: mark PDF路径绘制信息
|
||||
// 返回: error 错误信息
|
||||
func (p *pdfImporter) appendPath(mark pdfgo.PathMark) error {
|
||||
var err error
|
||||
mark.Style, err = p.maskStyle(mark.Style)
|
||||
if err != nil {
|
||||
if p.rasterMaskAllowed(err) {
|
||||
mask := mark.Style.SoftMask
|
||||
mark.Style.SoftMask = nil
|
||||
return p.rasterGroup(pdfgo.GroupMark{Alpha: 1, SoftMask: mask}, func(v pdfgo.Visitor) error { return v.Path(mark) })
|
||||
}
|
||||
return err
|
||||
}
|
||||
if mark.Fill && mark.Style.FillOverprint && pdfOverprintNeedsSeparation(mark.Style.Fill) || mark.Stroke && mark.Style.StrokeOverprint && pdfOverprintNeedsSeparation(mark.Style.Stroke) {
|
||||
return &pdfgo.UnsupportedError{Feature: "color separation overprint"}
|
||||
}
|
||||
if p.warning != nil && (mark.Fill && p.gradientError(mark.Style.Fill) != nil || mark.Stroke && p.gradientError(mark.Style.Stroke) != nil) {
|
||||
if err := p.ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
return p.gradientPath(mark)
|
||||
}
|
||||
box, err := p.pathBoundsContext(p.ctx, mark.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if box == (Box{}) {
|
||||
return fmt.Errorf("empty PDF painted path")
|
||||
}
|
||||
scale := math.Hypot(p.matrix[0], p.matrix[1])
|
||||
drawing, origin, ctm := *p, box, ""
|
||||
marginScale := scale
|
||||
if mark.StrokeMatrix != (pdfgo.Matrix{}) {
|
||||
inverse, ok := mark.StrokeMatrix.Inverse()
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid PDF stroke matrix")
|
||||
}
|
||||
drawing.matrix, origin, scale = inverse, Box{}, 1
|
||||
m := p.matrix.Mul(mark.StrokeMatrix)
|
||||
marginScale = math.Max(math.Hypot(m[0], m[2]), math.Hypot(m[1], m[3]))
|
||||
}
|
||||
if mark.Stroke {
|
||||
margin := mark.Style.LineWidth * marginScale / 2 * math.Max(1, mark.Style.MiterLimit)
|
||||
box = Box{X: box.X - margin, Y: box.Y - margin, W: box.W + 2*margin, H: box.H + 2*margin}
|
||||
}
|
||||
if mark.StrokeMatrix == (pdfgo.Matrix{}) {
|
||||
origin = box
|
||||
} else {
|
||||
m := p.matrix.Mul(mark.StrokeMatrix)
|
||||
m[4], m[5] = m[4]-box.X, m[5]-box.Y
|
||||
ctm = pdfNumbers(m[:]...)
|
||||
}
|
||||
paint := func(value pdfgo.Paint) (*FillColor, error) {
|
||||
if value.Tiling != nil || pdfGradientPattern(value) {
|
||||
return p.paintColor(value, box)
|
||||
}
|
||||
return drawing.paintColor(value, origin)
|
||||
}
|
||||
fillColor, strokePaint := p.color(mark.Style.Fill), p.color(mark.Style.Stroke)
|
||||
if mark.Fill {
|
||||
fillColor, err = paint(mark.Style.Fill)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if mark.Stroke {
|
||||
strokePaint, err = paint(mark.Style.Stroke)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
strokeColor := StrokeColor(*strokePaint)
|
||||
clips, err := p.clips(mark.Style.Clips, box)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := drawing.pathDataContext(p.ctx, mark.Path, origin)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
object := PathObject{Boundary: pdfBoundary(box), CTM: ctm, AbbreviatedData: data, Fill: &mark.Fill, Stroke: &mark.Stroke, FillColor: fillColor, StrokeColor: &strokeColor, LineWidth: mark.Style.LineWidth * scale, Cap: []string{"Butt", "Round", "Square"}[mark.Style.Cap], Join: []string{"Miter", "Round", "Bevel"}[mark.Style.Join], MiterLimit: mark.Style.MiterLimit, Clips: clips}
|
||||
object.LineWidthSet = object.LineWidth == 0
|
||||
if mark.Path.EvenOdd {
|
||||
object.Rule = "Even-Odd"
|
||||
}
|
||||
if len(mark.Style.Dash) > 0 {
|
||||
object.DashPattern = pdfDashPattern(mark.Style.Dash, scale)
|
||||
phase := mark.Style.DashPhase * scale
|
||||
object.DashOffset = &phase
|
||||
}
|
||||
p.appendObject(GraphicObject{Type: "PathObject", PathObject: object})
|
||||
p.report.PathObjects++
|
||||
return nil
|
||||
}
|
||||
|
||||
// paintColor 将页面渐变转换为对象局部坐标
|
||||
// 入参: paint PDF画刷, box 对象边界
|
||||
// 返回: *FillColor OFD颜色或渐变, error 图案转换错误
|
||||
func (p *pdfImporter) paintColor(paint pdfgo.Paint, box Box) (*FillColor, error) {
|
||||
if err := p.gradientError(paint); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if pdfGradientPattern(paint) {
|
||||
return p.gradientPattern(paint, box)
|
||||
}
|
||||
if paint.Mesh != nil {
|
||||
return p.meshColor(paint, box)
|
||||
}
|
||||
color := p.color(paint)
|
||||
if paint.Tiling != nil {
|
||||
pattern, err := p.tilingPattern(paint.Tiling, paint)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
color.Pattern = pattern
|
||||
return color, nil
|
||||
}
|
||||
if paint.Radial != nil {
|
||||
gradient := paint.Radial
|
||||
matrix := p.matrix
|
||||
if gradient.Matrix != (pdfgo.Matrix{}) {
|
||||
matrix = matrix.Mul(gradient.Matrix)
|
||||
}
|
||||
start, end := matrix.Apply(gradient.Start), matrix.Apply(gradient.End)
|
||||
xx, yy, xy := matrix[0]*matrix[0]+matrix[2]*matrix[2], matrix[1]*matrix[1]+matrix[3]*matrix[3], matrix[0]*matrix[1]+matrix[2]*matrix[3]
|
||||
major := (xx + yy + math.Hypot(xx-yy, 2*xy)) / 2
|
||||
determinant := matrix[0]*matrix[3] - matrix[1]*matrix[2]
|
||||
if major <= 0 || determinant == 0 {
|
||||
return nil, fmt.Errorf("invalid PDF radial gradient matrix")
|
||||
}
|
||||
scale := math.Sqrt(major)
|
||||
eccentricity := math.Sqrt(math.Max(0, 1-determinant*determinant/(major*major)))
|
||||
angle := math.Atan2(2*xy, xx-yy) * 90 / math.Pi
|
||||
extend := 0
|
||||
if gradient.Extend[0] {
|
||||
extend |= 1
|
||||
}
|
||||
if gradient.Extend[1] {
|
||||
extend |= 2
|
||||
}
|
||||
shading := &RadialShd{StartPoint: pdfNumbers(start.X-box.X, start.Y-box.Y), EndPoint: pdfNumbers(end.X-box.X, end.Y-box.Y), StartRadius: gradient.StartRadius * scale, EndRadius: gradient.EndRadius * scale, Eccentricity: eccentricity, Angle: angle, Extend: strconv.Itoa(extend)}
|
||||
var err error
|
||||
shading.Segment, err = p.gradientSegments(gradient.Stops, gradient.Space, gradient.Intent)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
color.Value = ""
|
||||
color.RadialShd = shading
|
||||
return color, nil
|
||||
}
|
||||
if paint.Axial == nil {
|
||||
return color, nil
|
||||
}
|
||||
gradient := paint.Axial
|
||||
start := p.matrix.Apply(gradient.Start)
|
||||
inverse, ok := p.matrix.Inverse()
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid PDF gradient matrix")
|
||||
}
|
||||
dx, dy := gradient.End.X-gradient.Start.X, gradient.End.Y-gradient.Start.Y
|
||||
gx, gy := inverse[0]*dx+inverse[1]*dy, inverse[2]*dx+inverse[3]*dy
|
||||
length := gx*gx + gy*gy
|
||||
if length == 0 {
|
||||
return nil, fmt.Errorf("invalid PDF axial gradient axis")
|
||||
}
|
||||
factor := (dx*dx + dy*dy) / length
|
||||
end := pdfgo.Point{X: start.X + gx*factor, Y: start.Y + gy*factor}
|
||||
extend := 0
|
||||
if gradient.Extend[0] {
|
||||
extend |= 1
|
||||
}
|
||||
if gradient.Extend[1] {
|
||||
extend |= 2
|
||||
}
|
||||
shading := &AxialShd{StartPoint: pdfNumbers(start.X-box.X, start.Y-box.Y), EndPoint: pdfNumbers(end.X-box.X, end.Y-box.Y), Extend: strconv.Itoa(extend)}
|
||||
var err error
|
||||
shading.Segment, err = p.gradientSegments(gradient.Stops, gradient.Space, gradient.Intent)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
color.Value = ""
|
||||
color.AxialShd = shading
|
||||
return color, nil
|
||||
}
|
||||
|
||||
// buildTilingPattern 保留PDF图案单元与页面坐标之间的仿射关系
|
||||
// 入参: source 平铺图案, base 无色图案基色
|
||||
// 返回: *Pattern OFD图案, error 转换错误
|
||||
func (p *pdfImporter) buildTilingPattern(source *pdfgo.TilingPattern, base pdfgo.Paint) (*Pattern, error) {
|
||||
const unit = 25.4 / 72
|
||||
box := source.BBox
|
||||
width, height := box.XMax-box.XMin, box.YMax-box.YMin
|
||||
xstep, ystep := math.Abs(source.XStep), math.Abs(source.YStep)
|
||||
if width <= 0 || height <= 0 || !finite(xstep) || !finite(ystep) || xstep == 0 || ystep == 0 {
|
||||
return nil, fmt.Errorf("invalid PDF tiling pattern dimensions")
|
||||
}
|
||||
localToPDF := pdfgo.Matrix{1 / unit, 0, 0, -1 / unit, box.XMin, box.YMax}
|
||||
m := p.matrix.Mul(source.Matrix).Mul(localToPDF)
|
||||
pattern := &Pattern{Width: math.Min(width, xstep) * unit, Height: math.Min(height, ystep) * unit, XStep: xstep * unit, YStep: ystep * unit, RelativeTo: "Page", CTM: pdfNumbers(m[:]...)}
|
||||
cell := *p
|
||||
cell.matrix = pdfgo.Matrix{unit, 0, 0, -unit, -box.XMin * unit, box.YMax * unit}
|
||||
cell.pageWidth, cell.pageHeight = width*unit, height*unit
|
||||
cell.rasterDPI *= m.MaxScale()
|
||||
if !finite(cell.rasterDPI) || cell.rasterDPI < 0 {
|
||||
return nil, fmt.Errorf("invalid PDF tiling pattern raster scale")
|
||||
}
|
||||
cell.objects, cell.pendingPath = nil, nil
|
||||
if err := source.Walk(p.ctx, base, cell.visitor()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := cell.flushPath(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
objects, err := cell.objects.data(p.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pattern.CellContent.Objects = objects
|
||||
if len(objects) != 0 && (width > xstep || height > ystep) {
|
||||
columns, rows := math.Ceil(width/xstep), math.Ceil(height/ystep)
|
||||
if !finite(columns*rows) || columns*rows >= float64(uint(^uint(0)>>1)+1) {
|
||||
return nil, fmt.Errorf("PDF tiling pattern instance count overflow")
|
||||
}
|
||||
bounds := make([]Box, len(objects))
|
||||
for index, object := range objects {
|
||||
boundary := ""
|
||||
if object.Type == "PathObject" && object.PathObject.Stroke != nil && !*object.PathObject.Stroke {
|
||||
boundary = object.PathObject.Boundary
|
||||
} else if object.Type == "ImageObject" && object.ImageObject.Border == nil {
|
||||
boundary = object.ImageObject.Boundary
|
||||
}
|
||||
if boundary != "" {
|
||||
var err error
|
||||
bounds[index], err = ParseBox(boundary)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
left, right, top, bottom := pdfPatternInstanceRange(columns, rows, bounds, pattern.Width, pattern.Height, xstep*unit, ystep*unit)
|
||||
var expanded pdfObjectBuffer
|
||||
for row := top; row <= bottom; row++ {
|
||||
for column := left; column <= right; column++ {
|
||||
if err := p.ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dx, dy := float64(column)*xstep*unit, float64(row)*ystep*unit
|
||||
for index, object := range objects {
|
||||
if index%pdfObjectChunkLimit == 0 {
|
||||
if err := p.ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
b := bounds[index]
|
||||
if b.W > 0 && b.H > 0 && (b.X+dx+b.W < 0 || b.Y+dy+b.H < 0 || b.X+dx > pattern.Width || b.Y+dy > pattern.Height) {
|
||||
continue
|
||||
}
|
||||
translated, err := transformEditorObject(object, dx, dy, 1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
expanded.append(translated)
|
||||
}
|
||||
}
|
||||
}
|
||||
pattern.CellContent.Objects, err = expanded.data(p.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return pattern, nil
|
||||
}
|
||||
|
||||
// pdfOpaqueBlack 判断不受底层分色影响的全黑设备色
|
||||
// 入参: paint PDF颜色与不透明度
|
||||
// 返回: bool 是否为不透明全黑CMYK颜色
|
||||
func pdfOpaqueBlack(paint pdfgo.Paint) bool {
|
||||
return paint.Alpha == 1 && paint.CMYK != nil && paint.CMYK[3] == 1
|
||||
}
|
||||
|
||||
// pdfOverprintNeedsSeparation 判断彩色CMYK套印是否依赖分色输出
|
||||
// 入参: paint PDF颜色与不透明度
|
||||
// 返回: bool 是否需要保留分色语义
|
||||
func pdfOverprintNeedsSeparation(paint pdfgo.Paint) bool {
|
||||
_, process := paint.Process.CMYKMask()
|
||||
return (paint.CMYK != nil || process) && !pdfOpaqueBlack(paint)
|
||||
return p.references.Resolve(ctx, source, reference, p.resolveFile, p.resolveReference, p.warning)
|
||||
}
|
||||
Reference in new issue
Block a user