mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-05-20 19:10:49 +08:00
🎨 Clean code
This commit is contained in:
parent
ecf908ae33
commit
f213d55fa9
@ -20,7 +20,6 @@ import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Masterminds/sprig/v3"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
@ -41,7 +40,9 @@ import (
|
||||
"github.com/88250/lute/parse"
|
||||
"github.com/88250/lute/render"
|
||||
"github.com/88250/pdfcpu/pkg/api"
|
||||
"github.com/88250/pdfcpu/pkg/font"
|
||||
"github.com/88250/pdfcpu/pkg/pdfcpu"
|
||||
"github.com/Masterminds/sprig/v3"
|
||||
"github.com/emirpasic/gods/sets/hashset"
|
||||
"github.com/emirpasic/gods/stacks/linkedliststack"
|
||||
"github.com/imroc/req/v3"
|
||||
@ -994,11 +995,23 @@ func processPDFFooter(pdfCtx *pdfcpu.Context) {
|
||||
}
|
||||
footer := buf.String()
|
||||
|
||||
fontName := "Times-Roman"
|
||||
fontName := "Helvetica"
|
||||
names := font.UserFontNames()
|
||||
if 1 > len(names) {
|
||||
preferredFont := util.GetPreferredFontFilePath(Conf.Lang)
|
||||
if err = api.InstallFonts([]string{preferredFont.Path}); nil != err {
|
||||
logging.LogErrorf("install font failed: %s", err)
|
||||
} else {
|
||||
names = font.UserFontNames()
|
||||
logging.LogInfof("install pdf font: %s", names)
|
||||
}
|
||||
}
|
||||
if 0 < len(names) {
|
||||
fontName = names[0]
|
||||
}
|
||||
|
||||
pos := "bc"
|
||||
dx := 10
|
||||
fillCol := "#000000"
|
||||
desc := fmt.Sprintf("font:%s, points:12, sc:1 abs, pos:%s, off:%d 10, fillcol:%s, rot:0", fontName, pos, dx, fillCol)
|
||||
desc := fmt.Sprintf("font:%s, points:8, sc:1 abs, pos:%s, off:10 10, fillc: 0.5 0.5 0.5, rot:0", fontName, pos)
|
||||
footer = strings.ReplaceAll(footer, "%pages", strconv.Itoa(pdfCtx.PageCount))
|
||||
m := map[int]*pdfcpu.Watermark{}
|
||||
for i := 1; i <= pdfCtx.PageCount; i++ {
|
||||
|
@ -30,9 +30,28 @@ import (
|
||||
"golang.org/x/text/transform"
|
||||
)
|
||||
|
||||
var (
|
||||
preferredFonts = []string{"Microsoft YaHei", "SimSun", "微软雅黑", "宋体", "仿宋", "Helvetica Neue", "Luxi Sans", "DejaVu Sans", "sans-serif", "Arial"}
|
||||
)
|
||||
|
||||
func GetPreferredFontFilePath(currentLanguage string) *Font {
|
||||
fonts := loadFonts(currentLanguage)
|
||||
sort.Slice(fonts, func(i, j int) bool { return len(fonts[i].Family) > len(fonts[j].Family) })
|
||||
for _, font := range fonts {
|
||||
if gulu.Str.Contains(font.Family, preferredFonts) {
|
||||
return font
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetSysFonts(currentLanguage string) (ret []string) {
|
||||
fonts := loadFonts(currentLanguage)
|
||||
ret = gulu.Str.RemoveDuplicatedElem(fonts)
|
||||
ret = []string{}
|
||||
for _, font := range fonts {
|
||||
ret = append(ret, font.Family)
|
||||
}
|
||||
ret = gulu.Str.RemoveDuplicatedElem(ret)
|
||||
ret = removeUnusedFonts(ret)
|
||||
sort.Strings(ret)
|
||||
return
|
||||
@ -49,47 +68,52 @@ func removeUnusedFonts(fonts []string) (ret []string) {
|
||||
return
|
||||
}
|
||||
|
||||
func loadFonts(currentLanguage string) (ret []string) {
|
||||
ret = []string{}
|
||||
for _, f := range findfont.List() {
|
||||
if strings.HasSuffix(strings.ToLower(f), ".ttc") {
|
||||
data, err := os.ReadFile(f)
|
||||
type Font struct {
|
||||
Path string
|
||||
Family string
|
||||
}
|
||||
|
||||
func loadFonts(currentLanguage string) (ret []*Font) {
|
||||
ret = []*Font{}
|
||||
for _, fontPath := range findfont.List() {
|
||||
if strings.HasSuffix(strings.ToLower(fontPath), ".ttc") {
|
||||
data, err := os.ReadFile(fontPath)
|
||||
if nil != err {
|
||||
logging.LogErrorf("read font file [%s] failed: %s", f, err)
|
||||
logging.LogErrorf("read font file [%s] failed: %s", fontPath, err)
|
||||
continue
|
||||
}
|
||||
collection, err := ttc.ParseCollection(data)
|
||||
if nil != err {
|
||||
//LogErrorf("parse font collection [%s] failed: %s", f, err)
|
||||
//LogErrorf("parse font collection [%s] failed: %s", fontPath, err)
|
||||
continue
|
||||
}
|
||||
|
||||
for i := 0; i < collection.NumFonts(); i++ {
|
||||
font, err := collection.Font(i)
|
||||
if nil != err {
|
||||
//LogErrorf("get font [%s] failed: %s", f, err)
|
||||
//LogErrorf("get font [%s] failed: %s", fontPath, err)
|
||||
continue
|
||||
}
|
||||
if family := parseFontFamily(font); "" != family {
|
||||
ret = append(ret, family)
|
||||
//LogInfof("[%s] [%s]", f, family)
|
||||
ret = append(ret, &Font{fontPath, family})
|
||||
//LogInfof("[%s] [%s]", fontPath, family)
|
||||
}
|
||||
}
|
||||
} else if strings.HasSuffix(strings.ToLower(f), ".otf") || strings.HasSuffix(strings.ToLower(f), ".ttf") {
|
||||
fontFile, err := os.Open(f)
|
||||
} else if strings.HasSuffix(strings.ToLower(fontPath), ".otf") || strings.HasSuffix(strings.ToLower(fontPath), ".ttf") {
|
||||
fontFile, err := os.Open(fontPath)
|
||||
if nil != err {
|
||||
//LogErrorf("open font file [%s] failed: %s", f, err)
|
||||
//LogErrorf("open font file [%s] failed: %s", fontPath, err)
|
||||
continue
|
||||
}
|
||||
font, err := sfnt.Parse(fontFile)
|
||||
if nil != err {
|
||||
//LogErrorf("parse font [%s] failed: %s", f, err)
|
||||
//LogErrorf("parse font [%s] failed: %s", fontPath, err)
|
||||
continue
|
||||
}
|
||||
|
||||
t, err := font.NameTable()
|
||||
if nil != err {
|
||||
//LogErrorf("parse font name table [%s] failed: %s", f, err)
|
||||
//LogErrorf("parse font name table [%s] failed: %s", fontPath, err)
|
||||
return
|
||||
}
|
||||
fontFile.Close()
|
||||
@ -102,7 +126,7 @@ func loadFonts(currentLanguage string) (ret []string) {
|
||||
if sfnt.PlatformLanguageID(1033) == e.LanguageID {
|
||||
v, _, err := transform.Bytes(textUnicode.UTF16(textUnicode.BigEndian, textUnicode.IgnoreBOM).NewDecoder(), e.Value)
|
||||
if nil != err {
|
||||
//LogErrorf("decode font family [%s] failed: %s", f, err)
|
||||
//LogErrorf("decode font family [%s] failed: %s", fontPath, err)
|
||||
continue
|
||||
}
|
||||
val := string(v)
|
||||
@ -119,7 +143,7 @@ func loadFonts(currentLanguage string) (ret []string) {
|
||||
|
||||
v, _, err := transform.Bytes(textUnicode.UTF16(textUnicode.BigEndian, textUnicode.IgnoreBOM).NewDecoder(), e.Value)
|
||||
if nil != err {
|
||||
//LogErrorf("decode font family [%s] failed: %s", f, err)
|
||||
//LogErrorf("decode font family [%s] failed: %s", fontPath, err)
|
||||
continue
|
||||
}
|
||||
val := string(v)
|
||||
@ -132,12 +156,12 @@ func loadFonts(currentLanguage string) (ret []string) {
|
||||
}
|
||||
}
|
||||
if "" != family && !strings.HasPrefix(family, ".") {
|
||||
ret = append(ret, family)
|
||||
//LogInfof("[%s] [%s]", f, family)
|
||||
ret = append(ret, &Font{fontPath, family})
|
||||
//LogInfof("[%s] [%s]", fontPath, family)
|
||||
}
|
||||
if "" != familyChinese && !strings.HasPrefix(familyChinese, ".") {
|
||||
ret = append(ret, familyChinese)
|
||||
//LogInfof("[%s] [%s]", f, family)
|
||||
ret = append(ret, &Font{fontPath, familyChinese})
|
||||
//LogInfof("[%s] [%s]", fontPath, family)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user