feat(文件归并): 归并PDF适配与解析逻辑
build.yaml / build (push) Successful in 48s

This commit is contained in:
xiaoqidun committed 2026-10-14 09:22:47 +08:00
1 parent b3c4bbc844
commit 961bc52038
43 files changed
+2283 -2594

No files matched your search

+54
View File
@@ -22,6 +22,7 @@ import (
"math"
"reflect"
baseimage "github.com/xiaoqidun/ofdgo/internal/base/image"
codecpdf "github.com/xiaoqidun/ofdgo/internal/codec/pdf"
"github.com/xiaoqidun/pdfgo"
)
@@ -743,3 +744,56 @@ func (p *pdfImporter) maskStyle(style pdfgo.Style) (pdfgo.Style, error) {
style.SoftMask = nil
return style, nil
}
// rasterMaskAllowed 仅对不可矢量表达的蒙版启用显式局部合成,格式错误仍返回
// 入参: err 蒙版转换错误
// 返回: bool 是否允许局部合成
func (p *pdfImporter) rasterMaskAllowed(err error) bool {
var unsupported *pdfgo.UnsupportedError
return p.warning != nil && errors.As(err, &unsupported)
}
// applySoftMask 按原始颜色分量计算蒙版,逐块应用透明度及亮度传递
// 入参: source 源图像, box 页面区域, mask PDF软蒙版
// 返回: image.Image 合成图像, error 蒙版解释或渲染错误
func (p *pdfImporter) applySoftMask(source image.Image, box Box, mask *pdfgo.SoftMask) (image.Image, error) {
if err := p.ctx.Err(); err != nil {
return nil, err
}
inverse, ok := p.matrix.Inverse()
if !ok {
return nil, fmt.Errorf("invalid PDF mask transform")
}
out := image.NewNRGBA(source.Bounds())
w, h := out.Rect.Dx(), out.Rect.Dy()
step := 25.4 / p.rasterDPI
cache := p.compositingCache()
scratch := &pdfCompositeScratch{}
space := &pdfgo.ColorSpace{Model: "DeviceRGB"}
for y := 0; y < h; y += pdfCompositeTileSize {
for x := 0; x < w; x += pdfCompositeTileSize {
if err := p.ctx.Err(); err != nil {
return nil, err
}
width, height := min(pdfCompositeTileSize, w-x), min(pdfCompositeTileSize, h-y)
c := pdfCompositor{importer: p, box: Box{X: box.X + float64(x)*step, Y: box.Y + float64(y)*step, W: float64(width) * step, H: float64(height) * step}, width: width, height: height, inverse: inverse, cache: cache, scratch: scratch, masks: map[*pdfgo.SoftMask][]float64{}}
alpha, err := c.mask(mask, space)
if err != nil {
return nil, err
}
for i, opacity := range alpha {
if i%width == 0 {
if err := p.ctx.Err(); err != nil {
return nil, err
}
}
px, py := out.Rect.Min.X+x+i%width, out.Rect.Min.Y+y+i/width
pixel := baseimage.NRGBAAt(source, px, py)
pixel.A = uint8(math.Round(float64(pixel.A) * opacity))
out.SetNRGBA(px, py, pixel)
}
c.releaseMasks()
}
}
return out, nil
}