mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-30 01:27:26 +08:00
This commit is contained in:
1 parent
cfbaa8a177
commit
2c06e5ff69
43 files changed
+3211
-397
No files matched your search
+63
-16
@@ -23,15 +23,22 @@ import (
|
||||
|
||||
// compressionImages 记录图片各处使用的最大像素需求
|
||||
type compressionImages struct {
|
||||
ctx context.Context
|
||||
dpi int
|
||||
sizes map[string]image.Point
|
||||
ctx context.Context
|
||||
dpi int
|
||||
sizes map[string]image.Point
|
||||
renderer *Renderer
|
||||
patternPage *Matrix
|
||||
patterns map[*Pattern]bool
|
||||
patternVisits map[compressionPatternKey]bool
|
||||
}
|
||||
|
||||
// compressionImagePlan 分文档分析标识,按共享路径合并最大尺寸和保护条件
|
||||
// compressionImagePlan 为有损输出分文档分析图片,按共享路径合并最大尺寸和保护条件
|
||||
// 入参: ctx 取消上下文, options 压缩配置
|
||||
// 返回: map[string]bool 可有损处理的路径, map[string]image.Point 像素需求, error 读取或取消错误
|
||||
func (r *Reader) compressionImagePlan(ctx context.Context, options CompressionOptions) (map[string]bool, map[string]image.Point, error) {
|
||||
if options.Mode != CompressionLossy {
|
||||
return nil, nil, ctx.Err()
|
||||
}
|
||||
images := make(map[string]bool)
|
||||
sizes := make(map[string]image.Point)
|
||||
blocked := make(map[string]bool)
|
||||
@@ -54,14 +61,10 @@ func (r *Reader) compressionImagePlan(ctx context.Context, options CompressionOp
|
||||
}
|
||||
return nil, nil, err
|
||||
}
|
||||
var masks map[string]bool
|
||||
var demands map[string]image.Point
|
||||
if options.Mode == CompressionLossy {
|
||||
var geometry bool
|
||||
masks, geometry, err = document.compressionImageSafety(ctx, catalog)
|
||||
if err == nil && geometry {
|
||||
demands, err = document.compressionImageSizes(ctx, catalog, options.ImageDPI())
|
||||
}
|
||||
masks, geometry, err := document.compressionImageSafety(ctx, catalog)
|
||||
if err == nil && geometry {
|
||||
demands, err = document.compressionImageSizes(ctx, catalog, options.ImageDPI())
|
||||
}
|
||||
if document != r {
|
||||
document.Close()
|
||||
@@ -102,7 +105,7 @@ func compressionImageSize(width, height float64, dpi int) image.Point {
|
||||
return image.Pt(int(w), int(h))
|
||||
}
|
||||
|
||||
// compressionImageSizes 按正文、模板及注解的实际变换汇总共享图片尺寸
|
||||
// compressionImageSizes 按正文、模板、注解及底纹的实际变换汇总图片尺寸,包括隐藏内容
|
||||
// 入参: ctx 取消上下文, images 图片资源, dpi 分辨率上限
|
||||
// 返回: map[string]image.Point 资源像素需求, error 取消错误
|
||||
func (r *Reader) compressionImageSizes(ctx context.Context, images []ImageInfo, dpi int) (map[string]image.Point, error) {
|
||||
@@ -113,9 +116,9 @@ func (r *Reader) compressionImageSizes(ctx context.Context, images []ImageInfo,
|
||||
if err != nil {
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
renderer := NewRenderer(r)
|
||||
renderer := NewRenderer(r, WithRenderBackends(RenderBackends{}))
|
||||
defer renderer.ClearCache()
|
||||
visitor := &compressionImages{ctx: ctx, dpi: dpi, sizes: make(map[string]image.Point)}
|
||||
visitor := &compressionImages{ctx: ctx, dpi: dpi, sizes: make(map[string]image.Point), renderer: renderer}
|
||||
for index := 0; index < count; index++ {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
@@ -157,14 +160,21 @@ func (r *Reader) compressionImageSizes(ctx context.Context, images []ImageInfo,
|
||||
return result, ctx.Err()
|
||||
}
|
||||
|
||||
// DrawObject 汇总原图和替换图像的完整显示尺寸,不使用裁剪后的局部面积
|
||||
// DrawObject 汇总图片、替换图及文字和路径底纹的尺寸,不使用裁剪后的局部面积
|
||||
// 入参: object 图形对象, state 继承状态
|
||||
// 返回: error 取消或几何错误
|
||||
func (v *compressionImages) DrawObject(object *GraphicObject, state RenderState) error {
|
||||
if err := v.ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if object.Type != "ImageObject" {
|
||||
switch object.Type {
|
||||
case "PathObject":
|
||||
return v.objectPatterns(object.PathObject, state)
|
||||
case "TextObject":
|
||||
text := object.TextObject
|
||||
return v.objectPatterns(PathObject{Boundary: text.Boundary, CTM: text.CTM, DrawParam: text.DrawParam, FillColor: text.FillColor, StrokeColor: text.StrokeColor}, state)
|
||||
case "ImageObject":
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
img := object.ImageObject
|
||||
@@ -182,6 +192,35 @@ func (v *compressionImages) DrawObject(object *GraphicObject, state RenderState)
|
||||
}
|
||||
matrix, _ := renderObjectMatrix(img.Boundary, local, state)
|
||||
size := compressionImageSize(math.Hypot(matrix.a, matrix.b), math.Hypot(matrix.c, matrix.d), v.dpi)
|
||||
return v.recordSize(ids, size)
|
||||
}
|
||||
|
||||
// VisitComposite 按矢量资源的完整变换统计缩略图和替换图,不以裁剪区域缩减尺寸
|
||||
// 入参: object 矢量资源或复合图元, state 已包含当前图元变换的状态
|
||||
// 返回: error 取消、资源标识或几何错误
|
||||
func (v *compressionImages) VisitComposite(object *CompositeGraphicUnit, state RenderState) error {
|
||||
if err := v.ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if object.Thumbnail == "" && object.Substitution == "" {
|
||||
return nil
|
||||
}
|
||||
ids := [2]string{editorResourceID(object.Thumbnail), editorResourceID(object.Substitution)}
|
||||
if object.Thumbnail != "" && ids[0] == "" || object.Substitution != "" && ids[1] == "" {
|
||||
return fmt.Errorf("invalid composite image resource")
|
||||
}
|
||||
matrix := IdentityMatrix
|
||||
if state.Parent != nil {
|
||||
matrix = *state.Parent
|
||||
}
|
||||
size := compressionImageSize(object.Width*math.Hypot(matrix.a, matrix.b), object.Height*math.Hypot(matrix.c, matrix.d), v.dpi)
|
||||
return v.recordSize(ids, size)
|
||||
}
|
||||
|
||||
// recordSize 合并同一图像资源在不同位置的最大像素需求
|
||||
// 入参: ids 图像标识,空值不统计, size 当前像素需求
|
||||
// 返回: error 尺寸无效时返回错误
|
||||
func (v *compressionImages) recordSize(ids [2]string, size image.Point) error {
|
||||
if size.X == 0 || size.Y == 0 {
|
||||
return fmt.Errorf("invalid image placement")
|
||||
}
|
||||
@@ -199,3 +238,11 @@ func (v *compressionImages) DrawObject(object *GraphicObject, state RenderState)
|
||||
// 入参: stamp 印章
|
||||
// 返回: error 始终为空
|
||||
func (v *compressionImages) DrawStamp(stamp Stamp) error { return nil }
|
||||
|
||||
// IncludeHidden 将隐藏内容纳入尺寸分析,保留图片在所有使用位置的精度
|
||||
// 返回: bool 始终为真
|
||||
func (v *compressionImages) IncludeHidden() bool { return true }
|
||||
|
||||
// SkipClips 按完整图片尺寸分析,不加载裁剪几何或文字轮廓
|
||||
// 返回: bool 始终为真
|
||||
func (v *compressionImages) SkipClips() bool { return true }
|
||||
Reference in new issue
Block a user