mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-29 15:06:46 +08:00
This commit is contained in:
1 parent
a5c6ac8f03
commit
9bde9eec64
3 files changed
+40
-33
No files matched your search
+29
-30
@@ -16,6 +16,7 @@ package ofdgo
|
||||
|
||||
import (
|
||||
"path"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -35,7 +36,7 @@ const (
|
||||
fontMatchFuzzy
|
||||
)
|
||||
|
||||
var fontMatchRules = []fontMatchRule{
|
||||
var fontMatchRules = normalizeFontMatchRules([]fontMatchRule{
|
||||
{
|
||||
Keys: []string{"小标宋", "方正小标宋", "xiaobiaosong", "fzxiaobiaosong", "fzxbs"},
|
||||
Names: []string{"小标宋体", "方正小标宋简体", "FZXiaoBiaoSong-B05", "FZXiaoBiaoSong-B05S"},
|
||||
@@ -109,10 +110,28 @@ var fontMatchRules = []fontMatchRule{
|
||||
Files: []string{"times.ttf", "times new roman*.ttf", "times*.ttf"},
|
||||
System: "Times New Roman",
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
var fontNameReplacer = strings.NewReplacer(" ", "", "-", "", "_", "", "(", "", ")", "", "(", "", ")", "")
|
||||
|
||||
// normalizeFontMatchRules 预处理固定字体规则的匹配名称,保留候选名称及顺序
|
||||
// 入参: rules 字体匹配规则
|
||||
// 返回: []fontMatchRule 预处理后的规则
|
||||
func normalizeFontMatchRules(rules []fontMatchRule) []fontMatchRule {
|
||||
for i := range rules {
|
||||
rule := &rules[i]
|
||||
keys := append(append(rule.Keys, rule.Names...), rule.System)
|
||||
rule.Keys = nil
|
||||
for _, key := range keys {
|
||||
key = fontNormalizeName(key)
|
||||
if key != "" && !slices.Contains(rule.Keys, key) {
|
||||
rule.Keys = append(rule.Keys, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
return rules
|
||||
}
|
||||
|
||||
// FontNormalizeName 规范化字体名称
|
||||
// 入参: name 字体名称
|
||||
// 返回: string 规范化后的字体名称
|
||||
@@ -180,12 +199,9 @@ func fontQualifiedNames(name string) []string {
|
||||
continue
|
||||
}
|
||||
prefix := ""
|
||||
for _, group := range [][]string{rule.Keys, rule.Names, {rule.System}} {
|
||||
for _, alias := range group {
|
||||
alias = fontNormalizeName(alias)
|
||||
if len(alias) > len(prefix) && strings.HasPrefix(name, alias) {
|
||||
prefix = alias
|
||||
}
|
||||
for _, alias := range rule.Keys {
|
||||
if len(alias) > len(prefix) && strings.HasPrefix(name, alias) {
|
||||
prefix = alias
|
||||
}
|
||||
}
|
||||
suffix := strings.TrimPrefix(name, prefix)
|
||||
@@ -351,32 +367,15 @@ func fontRuleMatchLevel(rule fontMatchRule, name string) int {
|
||||
return fontMatchNone
|
||||
}
|
||||
level := fontMatchNone
|
||||
match := func(item string) bool {
|
||||
item = fontNormalizeName(item)
|
||||
if item != "" && name == item {
|
||||
level = fontMatchExact
|
||||
return true
|
||||
for _, item := range rule.Keys {
|
||||
if name == item {
|
||||
return fontMatchExact
|
||||
}
|
||||
if item != "" && (level == fontMatchNone || level > fontMatchPartial) && strings.HasPrefix(name, item) {
|
||||
if strings.HasPrefix(name, item) {
|
||||
level = fontMatchPartial
|
||||
}
|
||||
if item != "" && level == fontMatchNone && strings.Contains(name, item) {
|
||||
} else if level == fontMatchNone && strings.Contains(name, item) {
|
||||
level = fontMatchFuzzy
|
||||
}
|
||||
return false
|
||||
}
|
||||
for _, item := range rule.Keys {
|
||||
if match(item) {
|
||||
return level
|
||||
}
|
||||
}
|
||||
for _, item := range rule.Names {
|
||||
if match(item) {
|
||||
return level
|
||||
}
|
||||
}
|
||||
if rule.System != "" {
|
||||
match(rule.System)
|
||||
}
|
||||
return level
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
package ofdgo
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
@@ -130,9 +131,13 @@ func (r *Renderer) RenderToEPS(page *PageContent, writer io.Writer) error {
|
||||
}
|
||||
options := ps.DefaultOptions
|
||||
options.Format = ps.EncapsulatedPostScript
|
||||
renderer := ps.New(writer, c.W, c.H, &options)
|
||||
buffer := bufio.NewWriter(writer)
|
||||
renderer := ps.New(buffer, c.W, c.H, &options)
|
||||
c.RenderTo(renderer)
|
||||
return renderer.Close()
|
||||
if err := renderer.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return buffer.Flush()
|
||||
}
|
||||
|
||||
// RenderToMultiPagePDF 将整个文档导出为多页PDF
|
||||
|
||||
+4
-1
@@ -84,13 +84,16 @@ func (p *PageText) Search(query string) []TextMatch {
|
||||
length := utf8.RuneCountInString(query)
|
||||
for index, run := range p.Runs {
|
||||
text := foldText(run.Text)
|
||||
runes := []rune(run.Text)
|
||||
var runes []rune
|
||||
codeOffset := 0
|
||||
for offset := 0; offset < len(text); {
|
||||
found := strings.Index(text[offset:], query)
|
||||
if found < 0 {
|
||||
break
|
||||
}
|
||||
if runes == nil {
|
||||
runes = []rune(run.Text)
|
||||
}
|
||||
start := codeOffset + utf8.RuneCountInString(text[offset:offset+found])
|
||||
offset += found
|
||||
end := start + length
|
||||
|
||||
Reference in new issue
Block a user