From c3d8c0e8a0ca533eddbe771e09ae1463f8027cfc Mon Sep 17 00:00:00 2001 From: xiaoqidun Date: Thu, 10 Sep 2026 10:37:56 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=B8=B2=E6=9F=93=E8=83=BD=E5=8A=9B):=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=96=87=E5=AD=97=E8=A3=81=E5=89=AA=EF=BC=8C?= =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=AD=97=E4=BD=93=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ofdgo_font_info.go | 33 +++++++++++++++++++++ ofdgo_page.go | 7 +++-- ofdgo_renderer_clip.go | 65 ++++++++++++++++++++++++++++++++++++++++++ ofdgo_renderer_path.go | 32 ++++++++++++++------- 4 files changed, 123 insertions(+), 14 deletions(-) create mode 100644 ofdgo_renderer_clip.go diff --git a/ofdgo_font_info.go b/ofdgo_font_info.go index 0053ef6..8627859 100644 --- a/ofdgo_font_info.go +++ b/ofdgo_font_info.go @@ -240,6 +240,12 @@ func (r *Renderer) countLayerFonts(layer Layer, usage map[string]int) { for _, text := range layer.TextObject { r.countTextFont(text, usage) } + for _, path := range layer.PathObject { + r.countClipFonts(path.Clips, usage) + } + for _, image := range layer.ImageObject { + r.countClipFonts(image.Clips, usage) + } for _, cgu := range layer.CompositeGraphicUnit { r.countCompositeFonts(cgu, usage, nil) } @@ -251,6 +257,10 @@ func (r *Renderer) countObjectFonts(obj GraphicObject, usage map[string]int) { switch obj.Type { case "TextObject": r.countTextFont(obj.TextObject, usage) + case "PathObject": + r.countClipFonts(obj.PathObject.Clips, usage) + case "ImageObject": + r.countClipFonts(obj.ImageObject.Clips, usage) case "CompositeGraphicUnit", "CompositeObject": r.countCompositeFonts(obj.CompositeGraphicUnit, usage, nil) } @@ -259,6 +269,7 @@ func (r *Renderer) countObjectFonts(obj GraphicObject, usage map[string]int) { // countCompositeFonts 统计复合图元字体使用次数 // 入参: cgu 复合图元, usage 字体使用次数, visited 已访问资源 func (r *Renderer) countCompositeFonts(cgu CompositeGraphicUnit, usage map[string]int, visited map[string]bool) { + r.countClipFonts(cgu.Clips, usage) if cgu.ResourceID != "" { if visited == nil { visited = make(map[string]bool) @@ -280,6 +291,12 @@ func (r *Renderer) countCompositeFonts(cgu CompositeGraphicUnit, usage map[strin for _, text := range cgu.TextObject { r.countTextFont(text, usage) } + for _, path := range cgu.PathObject { + r.countClipFonts(path.Clips, usage) + } + for _, image := range cgu.ImageObject { + r.countClipFonts(image.Clips, usage) + } for _, sub := range cgu.CompositeGraphicUnit { r.countCompositeFonts(sub, usage, visited) } @@ -291,4 +308,20 @@ func (r *Renderer) countTextFont(text TextObject, usage map[string]int) { if fontID := r.textObjectFontID(text); fontID != "" { usage[fontID]++ } + r.countClipFonts(text.Clips, usage) +} + +// countClipFonts 统计裁剪文字字体使用次数 +// 入参: clips 裁剪区域集合, usage 字体使用次数 +func (r *Renderer) countClipFonts(clips *Clips, usage map[string]int) { + if clips == nil { + return + } + for _, clip := range clips.Clip { + for _, area := range clip.Area { + for _, text := range area.Text { + r.countTextFont(text, usage) + } + } + } } diff --git a/ofdgo_page.go b/ofdgo_page.go index 9e5a04d..94c6f21 100644 --- a/ofdgo_page.go +++ b/ofdgo_page.go @@ -72,9 +72,10 @@ type Clip struct { // ClipArea 裁剪区域 type ClipArea struct { - CTM string `xml:"CTM,attr"` - Path []PathObject `xml:"Path"` - Text []TextObject `xml:"Text"` + DrawParam string `xml:"DrawParam,attr"` + CTM string `xml:"CTM,attr"` + Path []PathObject `xml:"Path"` + Text []TextObject `xml:"Text"` } // TextObject 文本对象 diff --git a/ofdgo_renderer_clip.go b/ofdgo_renderer_clip.go new file mode 100644 index 0000000..acacc84 --- /dev/null +++ b/ofdgo_renderer_clip.go @@ -0,0 +1,65 @@ +// 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 ofdgo + +import ( + "image" + + "github.com/tdewolff/canvas" +) + +// clipRenderer 收集图形和文字的绘制轮廓 +type clipRenderer struct { + path *canvas.Path + clip *canvas.Path +} + +// Size 返回不限定边界的裁剪画布尺寸 +// 返回: float64 宽度, float64 高度 +func (r *clipRenderer) Size() (float64, float64) { + return 0, 0 +} + +// RenderPath 合并路径填充及描边轮廓 +// 入参: path 路径, style 绘制样式, m 变换矩阵 +func (r *clipRenderer) RenderPath(path *canvas.Path, style canvas.Style, m canvas.Matrix) { + if style.HasFill() { + r.add(applyClipPath(path.Copy().Transform(m), r.clip)) + } + if style.HasStroke() { + p := path.Dash(style.DashOffset, style.Dashes...).Stroke(style.StrokeWidth, style.StrokeCapper, style.StrokeJoiner, canvas.Tolerance) + r.add(applyClipPath(p.Transform(m), r.clip)) + } +} + +// add 合并裁剪轮廓 +// 入参: path 裁剪路径 +func (r *clipRenderer) add(path *canvas.Path) { + if r.path == nil { + r.path = path + } else { + r.path = unionClipPath(r.path, path) + } +} + +// RenderText 合并文字字形轮廓 +// 入参: text 文字, m 变换矩阵 +func (r *clipRenderer) RenderText(text *canvas.Text, m canvas.Matrix) { + text.RenderTo(r, m, 0) +} + +// RenderImage 裁剪区不包含图像对象 +// 入参: img 图像, m 变换矩阵 +func (r *clipRenderer) RenderImage(img image.Image, m canvas.Matrix) {} diff --git a/ofdgo_renderer_path.go b/ofdgo_renderer_path.go index 8a360f6..fa98dfd 100644 --- a/ofdgo_renderer_path.go +++ b/ofdgo_renderer_path.go @@ -467,7 +467,7 @@ func (r *Renderer) buildClipPath(clips *Clips, pageH float64, bx, by float64, ob } var p *canvas.Path for _, clip := range clips.Clip { - var clipPath *canvas.Path + renderer := &clipRenderer{} for _, area := range clip.Area { areaCTM := NewMatrix(area.CTM) if clips.TransFlag == nil || *clips.TransFlag { @@ -481,20 +481,30 @@ func (r *Renderer) buildClipPath(clips *Clips, pageH float64, bx, by float64, ob if pathObj.Rule == "Even-Odd" { cp = cp.Settle(canvas.EvenOdd) } - if clipPath == nil { - clipPath = cp - } else { - clipPath = unionClipPath(clipPath, cp) + renderer.add(cp) + } + for _, textObj := range area.Text { + box, _ := ParseBox(textObj.Boundary) + ctm := NewMatrix(textObj.CTM) + m := areaCTM.Multiply(TranslationMatrix(box.X, box.Y)).Multiply(ctm) + ctx := canvas.NewContext(renderer) + ctx.SetView(canvas.Matrix{{m.a, -m.c, bx + m.e}, {-m.b, m.d, pageH - by - m.f}}) + renderer.clip = r.buildObjectClipPath(textObj.Clips, pageH, box.X, box.Y, ctm, &areaCTM, true) + if renderer.clip != nil { + renderer.clip = renderer.clip.Translate(bx, -by) } + textObj.Boundary, textObj.CTM = "", "" + textObj.Clips = nil + textObj.Alpha = nil + textObj.FillColor = &FillColor{Value: "0 0 0"} + textObj.StrokeColor = &StrokeColor{Value: "0 0 0"} + r.renderText(ctx, textObj, 0, r.drawParamDefaults(area.DrawParam, nil), nil, false, nil) } } - if clipPath != nil { - if p == nil { - p = clipPath - } else { - p = intersectClipPath(p, clipPath) - } + if renderer.path == nil { + renderer.path = &canvas.Path{} } + p = intersectClipPath(p, renderer.path) } return p }