diff --git a/ofdgo_renderer_output_canvas.go b/ofdgo_renderer_output_canvas.go index cd2d442..cd6b542 100644 --- a/ofdgo_renderer_output_canvas.go +++ b/ofdgo_renderer_output_canvas.go @@ -136,7 +136,7 @@ func (CanvasBackend) RenderPDF(r *Renderer, pages []RenderDocumentPage, writer i } start := buf.Len() p := pdf.New(buf, pages[0].Box.W, pages[0].Box.H, nil) - renderer := &pdfRenderer{PDF: p, glyphPaths: make(map[*canvas.Path]*canvas.Path), images: make([][]image.Image, 1), text: make([]pdfPageText, 1), navigation: navigation} + renderer := &pdfRenderer{PDF: p, glyphPaths: renderCache[*canvas.Path, *canvas.Path]{limit: 16 << 20}, images: make([][]image.Image, 1), text: make([]pdfPageText, 1), navigation: navigation} defer func() { if renderer.imageError != nil { r.canvasState().images = renderCache[*EncodedImage, image.Image]{limit: imageCacheLimit} diff --git a/ofdgo_renderer_pdf_canvas.go b/ofdgo_renderer_pdf_canvas.go index 622010c..28fe386 100644 --- a/ofdgo_renderer_pdf_canvas.go +++ b/ofdgo_renderer_pdf_canvas.go @@ -30,7 +30,7 @@ import ( // pdfRenderer PDF渲染器 type pdfRenderer struct { *pdf.PDF - glyphPaths map[*canvas.Path]*canvas.Path + glyphPaths renderCache[*canvas.Path, *canvas.Path] fonts map[*canvas.Font]*canvas.Font images [][]image.Image exactImages bool @@ -136,18 +136,22 @@ func (r *pdfRenderer) RenderImage(img image.Image, matrix canvas.Matrix) { r.PDF.RenderImage(img, matrix) } -// glyphPath 复用相似变换下的PDF字形圆弧转换 +// glyphPath 在缓存预算内复用相似变换下的PDF字形圆弧转换 // 入参: path 缓存字形路径, matrix 字形变换 // 返回: *canvas.Path PDF字形路径 func (r *pdfRenderer) glyphPath(path *canvas.Path, matrix canvas.Matrix) *canvas.Path { if !matrix.IsSimilarity() { return path } - if cached, ok := r.glyphPaths[path]; ok { + if cached, ok := r.glyphPaths.get(path); ok { return cached } converted := path.ReplaceArcs() - r.glyphPaths[path] = converted + cost := 256 + cap(path.Data())*8 + if converted != path { + cost += cap(converted.Data()) * 8 + } + r.glyphPaths.put(path, converted, cost) return converted }