feat(渲染能力): 支持文字裁剪,完善字体统计
build.yaml / build (push) Successful in 40s

This commit is contained in:
xiaoqidun committed 2026-09-10 10:37:56 +08:00
1 parent 93bdc0ce1d
commit c3d8c0e8a0
4 files changed
+121 -12

No files matched your search

+33
View File
@@ -240,6 +240,12 @@ func (r *Renderer) countLayerFonts(layer Layer, usage map[string]int) {
for _, text := range layer.TextObject { for _, text := range layer.TextObject {
r.countTextFont(text, usage) 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 { for _, cgu := range layer.CompositeGraphicUnit {
r.countCompositeFonts(cgu, usage, nil) r.countCompositeFonts(cgu, usage, nil)
} }
@@ -251,6 +257,10 @@ func (r *Renderer) countObjectFonts(obj GraphicObject, usage map[string]int) {
switch obj.Type { switch obj.Type {
case "TextObject": case "TextObject":
r.countTextFont(obj.TextObject, usage) r.countTextFont(obj.TextObject, usage)
case "PathObject":
r.countClipFonts(obj.PathObject.Clips, usage)
case "ImageObject":
r.countClipFonts(obj.ImageObject.Clips, usage)
case "CompositeGraphicUnit", "CompositeObject": case "CompositeGraphicUnit", "CompositeObject":
r.countCompositeFonts(obj.CompositeGraphicUnit, usage, nil) r.countCompositeFonts(obj.CompositeGraphicUnit, usage, nil)
} }
@@ -259,6 +269,7 @@ func (r *Renderer) countObjectFonts(obj GraphicObject, usage map[string]int) {
// countCompositeFonts 统计复合图元字体使用次数 // countCompositeFonts 统计复合图元字体使用次数
// 入参: cgu 复合图元, usage 字体使用次数, visited 已访问资源 // 入参: cgu 复合图元, usage 字体使用次数, visited 已访问资源
func (r *Renderer) countCompositeFonts(cgu CompositeGraphicUnit, usage map[string]int, visited map[string]bool) { func (r *Renderer) countCompositeFonts(cgu CompositeGraphicUnit, usage map[string]int, visited map[string]bool) {
r.countClipFonts(cgu.Clips, usage)
if cgu.ResourceID != "" { if cgu.ResourceID != "" {
if visited == nil { if visited == nil {
visited = make(map[string]bool) visited = make(map[string]bool)
@@ -280,6 +291,12 @@ func (r *Renderer) countCompositeFonts(cgu CompositeGraphicUnit, usage map[strin
for _, text := range cgu.TextObject { for _, text := range cgu.TextObject {
r.countTextFont(text, usage) 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 { for _, sub := range cgu.CompositeGraphicUnit {
r.countCompositeFonts(sub, usage, visited) 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 != "" { if fontID := r.textObjectFontID(text); fontID != "" {
usage[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)
}
}
}
} }
+1
View File
@@ -72,6 +72,7 @@ type Clip struct {
// ClipArea 裁剪区域 // ClipArea 裁剪区域
type ClipArea struct { type ClipArea struct {
DrawParam string `xml:"DrawParam,attr"`
CTM string `xml:"CTM,attr"` CTM string `xml:"CTM,attr"`
Path []PathObject `xml:"Path"` Path []PathObject `xml:"Path"`
Text []TextObject `xml:"Text"` Text []TextObject `xml:"Text"`
+65
View File
@@ -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) {}
+22 -12
View File
@@ -467,7 +467,7 @@ func (r *Renderer) buildClipPath(clips *Clips, pageH float64, bx, by float64, ob
} }
var p *canvas.Path var p *canvas.Path
for _, clip := range clips.Clip { for _, clip := range clips.Clip {
var clipPath *canvas.Path renderer := &clipRenderer{}
for _, area := range clip.Area { for _, area := range clip.Area {
areaCTM := NewMatrix(area.CTM) areaCTM := NewMatrix(area.CTM)
if clips.TransFlag == nil || *clips.TransFlag { 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" { if pathObj.Rule == "Even-Odd" {
cp = cp.Settle(canvas.EvenOdd) cp = cp.Settle(canvas.EvenOdd)
} }
if clipPath == nil { renderer.add(cp)
clipPath = cp }
} else { for _, textObj := range area.Text {
clipPath = unionClipPath(clipPath, cp) 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 renderer.path == nil {
renderer.path = &canvas.Path{}
} }
if clipPath != nil { p = intersectClipPath(p, renderer.path)
if p == nil {
p = clipPath
} else {
p = intersectClipPath(p, clipPath)
}
}
} }
return p return p
} }