feat(内存占用): 复用SVG字体资源,减少多页阅读的传输量和内存开销
build.yaml / build (push) Successful in 41s

This commit is contained in:
xiaoqidun committed 2026-09-13 16:13:51 +08:00
1 parent 481b21c7ce
commit 78b34713cc
5 files changed
+165 -2

No files matched your search

+28
View File
@@ -53,6 +53,7 @@ const state = {
continuous: false, continuous: false,
renderAnnotations: true, renderAnnotations: true,
pageCache: new Map(), pageCache: new Map(),
svgFonts: new Map(),
selectedPages: new Set(), selectedPages: new Set(),
pageInFlight: new Map(), pageInFlight: new Map(),
pageRenderQueue: [], pageRenderQueue: [],
@@ -1509,6 +1510,10 @@ function renderPageFlow() {
} }
function resetPageFlow() { function resetPageFlow() {
for (const font of state.svgFonts.values()) {
document.fonts.delete(font);
}
state.svgFonts.clear();
state.pageCache.clear(); state.pageCache.clear();
state.selectedPages.clear(); state.selectedPages.clear();
state.visiblePages.clear(); state.visiblePages.clear();
@@ -1657,6 +1662,7 @@ async function processPageRenderQueue() {
continue; continue;
} }
const page = await callWASM("ofdgoRenderPage", task.index); const page = await callWASM("ofdgoRenderPage", task.index);
await loadSVGFonts(page.fonts, task.openSeq);
if (task.openSeq === state.openSeq) { if (task.openSeq === state.openSeq) {
state.pageCache.set(task.index, page); state.pageCache.set(task.index, page);
} }
@@ -1672,6 +1678,28 @@ async function processPageRenderQueue() {
} }
} }
async function loadSVGFonts(fonts, openSeq) {
for (const font of fonts) {
if (openSeq !== state.openSeq) {
return;
}
if (state.svgFonts.has(font.name)) {
continue;
}
const data = await callWASM("ofdgoSVGFontData", font.name);
if (openSeq !== state.openSeq) {
return;
}
const face = new FontFace(font.name, data.bytes, { weight: String(font.weight), style: font.style });
await face.load();
if (openSeq !== state.openSeq) {
return;
}
document.fonts.add(face);
state.svgFonts.set(font.name, face);
}
}
function comparePageRenderTask(a, b) { function comparePageRenderTask(a, b) {
return a.priority - b.priority || Math.abs(a.index - state.pageIndex) - Math.abs(b.index - state.pageIndex); return a.priority - b.priority || Math.abs(a.index - state.pageIndex) - Math.abs(b.index - state.pageIndex);
} }
+20
View File
@@ -39,6 +39,7 @@ func RunWASM() {
registerCallback("ofdgoDocumentInfo", documentInfo) registerCallback("ofdgoDocumentInfo", documentInfo)
registerCallback("ofdgoAttachmentData", attachmentData) registerCallback("ofdgoAttachmentData", attachmentData)
registerCallback("ofdgoRenderPage", renderPage) registerCallback("ofdgoRenderPage", renderPage)
registerCallback("ofdgoSVGFontData", svgFontData)
registerCallback("ofdgoSearchPage", searchPage) registerCallback("ofdgoSearchPage", searchPage)
registerCallback("ofdgoExportFormats", exportFormats) registerCallback("ofdgoExportFormats", exportFormats)
registerCallback("ofdgoExportPage", exportPage) registerCallback("ofdgoExportPage", exportPage)
@@ -150,6 +151,20 @@ func attachmentData(args []js.Value) (any, error) {
return successResult(map[string]any{"bytes": bytesToJS(data)}), nil return successResult(map[string]any{"bytes": bytesToJS(data)}), nil
} }
// svgFontData 读取SVG字体资源
// 入参: args 浏览器参数
// 返回: any 字体数据, error 错误信息
func svgFontData(args []js.Value) (any, error) {
if currentSession == nil {
return nil, fmt.Errorf("ofd document is not opened")
}
data, err := currentSession.SVGFontData(args[0].String())
if err != nil {
return nil, err
}
return successResult(map[string]any{"bytes": bytesToJS(data)}), nil
}
// renderPage 渲染OFD页面 // renderPage 渲染OFD页面
// 入参: args 浏览器参数 // 入参: args 浏览器参数
// 返回: any 页面SVG结果, error 错误信息 // 返回: any 页面SVG结果, error 错误信息
@@ -182,6 +197,10 @@ func renderPage(args []js.Value) (any, error) {
"height": link.Box.H, "height": link.Box.H,
} }
} }
fonts := make([]any, len(page.Fonts))
for i, font := range page.Fonts {
fonts[i] = map[string]any{"name": font.Name, "weight": font.Weight, "style": font.Style}
}
return successResult(map[string]any{ return successResult(map[string]any{
"index": page.Index, "index": page.Index,
"number": page.Number, "number": page.Number,
@@ -190,6 +209,7 @@ func renderPage(args []js.Value) (any, error) {
"height": page.Height, "height": page.Height,
"svg": page.SVG, "svg": page.SVG,
"links": links, "links": links,
"fonts": fonts,
"text": js.Global().Get("JSON").Call("parse", string(textData)), "text": js.Global().Get("JSON").Call("parse", string(textData)),
}), nil }), nil
} }
+22 -2
View File
@@ -108,6 +108,7 @@ type Session struct {
pageCache map[int]*ofdgo.PageContent pageCache map[int]*ofdgo.PageContent
boxCache map[int]ofdgo.Box boxCache map[int]ofdgo.Box
textCache map[int]*ofdgo.PageText textCache map[int]*ofdgo.PageText
svgFonts map[string][]byte
signatures []SignatureInfo signatures []SignatureInfo
signatureError error signatureError error
signaturesRead bool signaturesRead bool
@@ -165,6 +166,7 @@ type PageSVG struct {
Height float64 `json:"height"` Height float64 `json:"height"`
SVG string `json:"svg"` SVG string `json:"svg"`
Links []ofdgo.PageLink `json:"-"` Links []ofdgo.PageLink `json:"-"`
Fonts []ofdgo.SVGFont `json:"-"`
} }
// ExportFormat 导出格式 // ExportFormat 导出格式
@@ -241,6 +243,7 @@ func Open(data []byte, opts OpenOptions) (*Session, error) {
pageCache: make(map[int]*ofdgo.PageContent), pageCache: make(map[int]*ofdgo.PageContent),
boxCache: make(map[int]ofdgo.Box), boxCache: make(map[int]ofdgo.Box),
textCache: make(map[int]*ofdgo.PageText), textCache: make(map[int]*ofdgo.PageText),
svgFonts: make(map[string][]byte),
}, nil }, nil
} }
@@ -267,6 +270,7 @@ func (s *Session) SetFonts(fonts []FontFile) error {
s.Renderer.SetFontFS() s.Renderer.SetFontFS()
} }
clear(s.textCache) clear(s.textCache)
clear(s.svgFonts)
return nil return nil
} }
@@ -415,14 +419,30 @@ func (s *Session) RenderPageSVG(index int) (PageSVG, error) {
return PageSVG{}, err return PageSVG{}, err
} }
var buf bytes.Buffer var buf bytes.Buffer
if err := s.Renderer.RenderToSVG(page, &buf); err != nil { fonts, err := s.Renderer.RenderToSVGWithFonts(page, &buf)
if err != nil {
return PageSVG{}, err return PageSVG{}, err
} }
links, err := s.Renderer.PageLinks(page) links, err := s.Renderer.PageLinks(page)
if err != nil { if err != nil {
return PageSVG{}, err return PageSVG{}, err
} }
return PageSVG{Index: index, Number: index + 1, ID: pageRef.ID, Width: box.W, Height: box.H, SVG: buf.String(), Links: links}, nil for i, font := range fonts {
s.svgFonts[font.Name] = font.Data
fonts[i].Data = nil
}
return PageSVG{Index: index, Number: index + 1, ID: pageRef.ID, Width: box.W, Height: box.H, SVG: buf.String(), Links: links, Fonts: fonts}, nil
}
// SVGFontData 获取已渲染页面引用的字体数据
// 入参: name 字体资源标识
// 返回: []byte 字体数据, error 错误信息
func (s *Session) SVGFontData(name string) ([]byte, error) {
data, ok := s.svgFonts[name]
if !ok {
return nil, fmt.Errorf("svg font not found: %s", name)
}
return data, nil
} }
// ExportPage 导出单页 // ExportPage 导出单页
+2
View File
@@ -35,6 +35,7 @@ type Renderer struct {
FontGIDMap map[string]map[uint16]rune FontGIDMap map[string]map[uint16]rune
FontCIDMap map[string]map[uint16]rune FontCIDMap map[string]map[uint16]rune
fontCache map[fontCacheKey]*canvas.FontFamily fontCache map[fontCacheKey]*canvas.FontFamily
svgFontCache map[*canvas.Font]SVGFont
fontSourceCache map[string][]fontSource fontSourceCache map[string][]fontSource
fontSourceUsed map[string]fontSource fontSourceUsed map[string]fontSource
fontDirCandidates map[string][]fontFileCandidate fontDirCandidates map[string][]fontFileCandidate
@@ -64,6 +65,7 @@ func (r *Renderer) resetFontCache() {
r.FontGIDMap = make(map[string]map[uint16]rune) r.FontGIDMap = make(map[string]map[uint16]rune)
r.FontCIDMap = make(map[string]map[uint16]rune) r.FontCIDMap = make(map[string]map[uint16]rune)
r.fontCache = make(map[fontCacheKey]*canvas.FontFamily) r.fontCache = make(map[fontCacheKey]*canvas.FontFamily)
r.svgFontCache = make(map[*canvas.Font]SVGFont)
r.fontSourceCache = make(map[string][]fontSource) r.fontSourceCache = make(map[string][]fontSource)
r.fontSourceUsed = make(map[string]fontSource) r.fontSourceUsed = make(map[string]fontSource)
r.fontDirCandidates = make(map[string][]fontFileCandidate) r.fontDirCandidates = make(map[string][]fontFileCandidate)
+93
View File
@@ -0,0 +1,93 @@
// 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 (
"crypto/sha256"
"fmt"
"io"
"strings"
"github.com/tdewolff/canvas"
"github.com/tdewolff/canvas/renderers/svg"
)
// SVGFont SVG引用的字体资源,Name为CSS字体族标识,Data为只读字体数据
type SVGFont struct {
Name string
Weight int
Style string
Data []byte
}
// svgFontRenderer 分离字体资源的SVG渲染器
type svgFontRenderer struct {
*svg.SVG
renderer *Renderer
fonts []SVGFont
seen map[string]bool
styles strings.Builder
}
// RenderToSVGWithFonts 渲染为SVG并返回页面引用的字体资源,不在SVG中嵌入字体
// 入参: page 页面内容, writer 输出流
// 返回: []SVGFont 字体资源,可按Name复用并按Weight、Style注册,error 错误信息
func (r *Renderer) RenderToSVGWithFonts(page *PageContent, writer io.Writer) ([]SVGFont, error) {
c, err := r.renderPage(page)
if err != nil {
return nil, err
}
options := svg.DefaultOptions
options.EmbedFonts = false
s := &svgFontRenderer{
SVG: svg.New(writer, c.W, c.H, &options),
renderer: r,
seen: make(map[string]bool),
}
c.RenderTo(s)
s.SetCustomStyle(s.styles.String())
return s.fonts, s.Close()
}
// RenderText 绘制文字并记录实际使用的字体资源
// 入参: text 文字对象, m 变换矩阵
func (s *svgFontRenderer) RenderText(text *canvas.Text, m canvas.Matrix) {
if text.Empty() {
return
}
font := text.MostCommonFontFace().Font
resource, ok := s.renderer.svgFontCache[font]
if !ok {
data := font.SFNT.Write()
resource = SVGFont{
Name: fmt.Sprintf("ofdgo-%x-%d", sha256.Sum256(data), font.Style()),
Weight: font.Style().CSS(),
Style: "normal",
Data: data,
}
if font.Style().Italic() {
resource.Style = "italic"
}
s.renderer.svgFontCache[font] = resource
}
if !s.seen[resource.Name] {
s.seen[resource.Name] = true
s.fonts = append(s.fonts, resource)
fmt.Fprintf(&s.styles, ".%s{font-family:%s!important}", resource.Name, resource.Name)
}
s.SetClass(resource.Name)
s.SVG.RenderText(text, m)
s.SetClass()
}