mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-29 16:59:27 +08:00
This commit is contained in:
1 parent
225f60919b
commit
19dc79e8e3
23 files changed
+836
-289
No files matched your search
@@ -16,7 +16,9 @@ package ofdgo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"maps"
|
||||
"slices"
|
||||
@@ -38,6 +40,56 @@ type editorFontSubset struct {
|
||||
mapped bool
|
||||
}
|
||||
|
||||
// editorSourceFontKey 区分原字体内容及完整用字集合
|
||||
type editorSourceFontKey struct {
|
||||
source [32]byte
|
||||
usage [32]byte
|
||||
}
|
||||
|
||||
// editorSourceFontCache 在单次资源处理中复用裁剪结果,空值表示无体积收益
|
||||
type editorSourceFontCache map[editorSourceFontKey][]byte
|
||||
|
||||
// subset 按原字体和用字复用安全子集,不缓存错误、取消结果或原始字体
|
||||
// 入参: ctx 取消上下文, backend 字体资源后端, data 原字体, usage 用字记录
|
||||
// 返回: []byte 更小的字体子集,无收益时为nil, error 裁剪或取消错误
|
||||
func (c *editorSourceFontCache) subset(ctx context.Context, backend FontResourceBackend, data []byte, usage *editorFontUsage) ([]byte, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if usage == nil || usage.unsafe || backend == nil {
|
||||
return nil, nil
|
||||
}
|
||||
characters, glyphs := slices.Sorted(maps.Keys(usage.chars)), slices.Sorted(maps.Keys(usage.glyphs))
|
||||
encoded := make([]byte, 0, 8+4*len(characters)+2*len(glyphs))
|
||||
encoded = binary.LittleEndian.AppendUint32(encoded, uint32(len(characters)))
|
||||
for _, char := range characters {
|
||||
encoded = binary.LittleEndian.AppendUint32(encoded, uint32(char))
|
||||
}
|
||||
encoded = binary.LittleEndian.AppendUint32(encoded, uint32(len(glyphs)))
|
||||
for _, glyph := range glyphs {
|
||||
encoded = binary.LittleEndian.AppendUint16(encoded, glyph)
|
||||
}
|
||||
key := editorSourceFontKey{source: sha256.Sum256(data), usage: sha256.Sum256(encoded)}
|
||||
if result, ok := (*c)[key]; ok {
|
||||
return result, ctx.Err()
|
||||
}
|
||||
result, err := backend.SubsetSourceFont(data, FontUsage{Characters: characters, Glyphs: glyphs})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(result) == 0 || len(result) >= len(data) {
|
||||
result = nil
|
||||
}
|
||||
if *c == nil {
|
||||
*c = make(editorSourceFontCache)
|
||||
}
|
||||
(*c)[key] = result
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// AddFont 注册OpenType字体,集合字体按索引提取,重复资源复用标识,引用后写入文档
|
||||
// 入参: file 字体文件, index 集合内字体索引,单字体为0
|
||||
// 返回: string 字体资源标识, error 错误信息
|
||||
|
||||
Reference in new issue
Block a user