🎨 Clean code

This commit is contained in:
Liang Ding 2023-04-01 12:26:14 +08:00
parent 7b34eb6c6d
commit eddcdaedcd
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
2 changed files with 64 additions and 27 deletions

View file

@ -20,7 +20,6 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"github.com/Masterminds/sprig/v3"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -41,7 +40,9 @@ import (
"github.com/88250/lute/parse" "github.com/88250/lute/parse"
"github.com/88250/lute/render" "github.com/88250/lute/render"
"github.com/88250/pdfcpu/pkg/api" "github.com/88250/pdfcpu/pkg/api"
"github.com/88250/pdfcpu/pkg/font"
"github.com/88250/pdfcpu/pkg/pdfcpu" "github.com/88250/pdfcpu/pkg/pdfcpu"
"github.com/Masterminds/sprig/v3"
"github.com/emirpasic/gods/sets/hashset" "github.com/emirpasic/gods/sets/hashset"
"github.com/emirpasic/gods/stacks/linkedliststack" "github.com/emirpasic/gods/stacks/linkedliststack"
"github.com/imroc/req/v3" "github.com/imroc/req/v3"
@ -994,11 +995,23 @@ func processPDFFooter(pdfCtx *pdfcpu.Context) {
} }
footer := buf.String() 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" pos := "bc"
dx := 10 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)
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)
footer = strings.ReplaceAll(footer, "%pages", strconv.Itoa(pdfCtx.PageCount)) footer = strings.ReplaceAll(footer, "%pages", strconv.Itoa(pdfCtx.PageCount))
m := map[int]*pdfcpu.Watermark{} m := map[int]*pdfcpu.Watermark{}
for i := 1; i <= pdfCtx.PageCount; i++ { for i := 1; i <= pdfCtx.PageCount; i++ {

View file

@ -30,9 +30,28 @@ import (
"golang.org/x/text/transform" "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) { func GetSysFonts(currentLanguage string) (ret []string) {
fonts := loadFonts(currentLanguage) 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) ret = removeUnusedFonts(ret)
sort.Strings(ret) sort.Strings(ret)
return return
@ -49,47 +68,52 @@ func removeUnusedFonts(fonts []string) (ret []string) {
return return
} }
func loadFonts(currentLanguage string) (ret []string) { type Font struct {
ret = []string{} Path string
for _, f := range findfont.List() { Family string
if strings.HasSuffix(strings.ToLower(f), ".ttc") { }
data, err := os.ReadFile(f)
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 { if nil != err {
logging.LogErrorf("read font file [%s] failed: %s", f, err) logging.LogErrorf("read font file [%s] failed: %s", fontPath, err)
continue continue
} }
collection, err := ttc.ParseCollection(data) collection, err := ttc.ParseCollection(data)
if nil != err { if nil != err {
//LogErrorf("parse font collection [%s] failed: %s", f, err) //LogErrorf("parse font collection [%s] failed: %s", fontPath, err)
continue continue
} }
for i := 0; i < collection.NumFonts(); i++ { for i := 0; i < collection.NumFonts(); i++ {
font, err := collection.Font(i) font, err := collection.Font(i)
if nil != err { if nil != err {
//LogErrorf("get font [%s] failed: %s", f, err) //LogErrorf("get font [%s] failed: %s", fontPath, err)
continue continue
} }
if family := parseFontFamily(font); "" != family { if family := parseFontFamily(font); "" != family {
ret = append(ret, family) ret = append(ret, &Font{fontPath, family})
//LogInfof("[%s] [%s]", f, family) //LogInfof("[%s] [%s]", fontPath, family)
} }
} }
} else if strings.HasSuffix(strings.ToLower(f), ".otf") || strings.HasSuffix(strings.ToLower(f), ".ttf") { } else if strings.HasSuffix(strings.ToLower(fontPath), ".otf") || strings.HasSuffix(strings.ToLower(fontPath), ".ttf") {
fontFile, err := os.Open(f) fontFile, err := os.Open(fontPath)
if nil != err { if nil != err {
//LogErrorf("open font file [%s] failed: %s", f, err) //LogErrorf("open font file [%s] failed: %s", fontPath, err)
continue continue
} }
font, err := sfnt.Parse(fontFile) font, err := sfnt.Parse(fontFile)
if nil != err { if nil != err {
//LogErrorf("parse font [%s] failed: %s", f, err) //LogErrorf("parse font [%s] failed: %s", fontPath, err)
continue continue
} }
t, err := font.NameTable() t, err := font.NameTable()
if nil != err { if nil != err {
//LogErrorf("parse font name table [%s] failed: %s", f, err) //LogErrorf("parse font name table [%s] failed: %s", fontPath, err)
return return
} }
fontFile.Close() fontFile.Close()
@ -102,7 +126,7 @@ func loadFonts(currentLanguage string) (ret []string) {
if sfnt.PlatformLanguageID(1033) == e.LanguageID { if sfnt.PlatformLanguageID(1033) == e.LanguageID {
v, _, err := transform.Bytes(textUnicode.UTF16(textUnicode.BigEndian, textUnicode.IgnoreBOM).NewDecoder(), e.Value) v, _, err := transform.Bytes(textUnicode.UTF16(textUnicode.BigEndian, textUnicode.IgnoreBOM).NewDecoder(), e.Value)
if nil != err { if nil != err {
//LogErrorf("decode font family [%s] failed: %s", f, err) //LogErrorf("decode font family [%s] failed: %s", fontPath, err)
continue continue
} }
val := string(v) 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) v, _, err := transform.Bytes(textUnicode.UTF16(textUnicode.BigEndian, textUnicode.IgnoreBOM).NewDecoder(), e.Value)
if nil != err { if nil != err {
//LogErrorf("decode font family [%s] failed: %s", f, err) //LogErrorf("decode font family [%s] failed: %s", fontPath, err)
continue continue
} }
val := string(v) val := string(v)
@ -132,12 +156,12 @@ func loadFonts(currentLanguage string) (ret []string) {
} }
} }
if "" != family && !strings.HasPrefix(family, ".") { if "" != family && !strings.HasPrefix(family, ".") {
ret = append(ret, family) ret = append(ret, &Font{fontPath, family})
//LogInfof("[%s] [%s]", f, family) //LogInfof("[%s] [%s]", fontPath, family)
} }
if "" != familyChinese && !strings.HasPrefix(familyChinese, ".") { if "" != familyChinese && !strings.HasPrefix(familyChinese, ".") {
ret = append(ret, familyChinese) ret = append(ret, &Font{fontPath, familyChinese})
//LogInfof("[%s] [%s]", f, family) //LogInfof("[%s] [%s]", fontPath, family)
} }
} }
} }