mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-09-22 00:20:47 +02:00
🎨 Improve export PDF footer font
This commit is contained in:
parent
3eaa043889
commit
345857ae79
4 changed files with 47 additions and 27 deletions
|
@ -7,7 +7,7 @@ require (
|
|||
github.com/88250/css v0.1.2
|
||||
github.com/88250/gulu v1.2.3-0.20230321113152-38c4a3d73c37
|
||||
github.com/88250/lute v1.7.6-0.20230330013439-8760e9c073db
|
||||
github.com/88250/pdfcpu v0.3.14-0.20230224021324-e51076eb6390
|
||||
github.com/88250/pdfcpu v0.3.14-0.20230401044135-c7369a99720c
|
||||
github.com/88250/vitess-sqlparser v0.0.0-20210205111146-56a2ded2aba1
|
||||
github.com/ClarkThan/ahocorasick v0.0.0-20230220142845-f237b6348b3e
|
||||
github.com/ConradIrwin/font v0.0.0-20210318200717-ce8d41cc0732
|
||||
|
@ -65,6 +65,9 @@ require (
|
|||
dmitri.shuralyov.com/font/woff2 v0.0.0-20180220214647-957792cbbdab // indirect
|
||||
github.com/Masterminds/goutils v1.1.1 // indirect
|
||||
github.com/Masterminds/semver/v3 v3.2.0 // indirect
|
||||
github.com/adrg/strutil v0.2.2 // indirect
|
||||
github.com/adrg/sysfont v0.1.2 // indirect
|
||||
github.com/adrg/xdg v0.3.0 // indirect
|
||||
github.com/alecthomas/chroma v0.10.0 // indirect
|
||||
github.com/andybalholm/cascadia v1.3.1 // indirect
|
||||
github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef // indirect
|
||||
|
|
|
@ -12,6 +12,8 @@ github.com/88250/lute v1.7.6-0.20230330013439-8760e9c073db h1:VS58OzTzimPqjiw6WQ
|
|||
github.com/88250/lute v1.7.6-0.20230330013439-8760e9c073db/go.mod h1:+wUqx/1kdFDbWtxn9LYJlaCOAeol2pjSO6w+WJTVQsg=
|
||||
github.com/88250/pdfcpu v0.3.14-0.20230224021324-e51076eb6390 h1:q2AR33VoQ87WYtvZ4pEvwj5gZkv22HK/yMlPWwF1oyc=
|
||||
github.com/88250/pdfcpu v0.3.14-0.20230224021324-e51076eb6390/go.mod h1:S5YT38L/GCjVjmB4PB84PymA1qfopjEhfhTNQilLpv4=
|
||||
github.com/88250/pdfcpu v0.3.14-0.20230401044135-c7369a99720c h1:Dl/8S9iLyPMTElnWIBxmjaLiWrkI5P4a21ivwAn5pU0=
|
||||
github.com/88250/pdfcpu v0.3.14-0.20230401044135-c7369a99720c/go.mod h1:S5YT38L/GCjVjmB4PB84PymA1qfopjEhfhTNQilLpv4=
|
||||
github.com/88250/vitess-sqlparser v0.0.0-20210205111146-56a2ded2aba1 h1:48T899JQDwyyRu9yXHePYlPdHtpJfrJEUGBMH3SMBWY=
|
||||
github.com/88250/vitess-sqlparser v0.0.0-20210205111146-56a2ded2aba1/go.mod h1:U3pckKQIgxxkmZjV5yXQjHdGxQK0o/vEZeZ6cQsxfHw=
|
||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
|
@ -29,6 +31,12 @@ github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAc
|
|||
github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
|
||||
github.com/Xuanwo/go-locale v1.1.0 h1:51gUxhxl66oXAjI9uPGb2O0qwPECpriKQb2hl35mQkg=
|
||||
github.com/Xuanwo/go-locale v1.1.0/go.mod h1:UKrHoZB3FPIk9wIG2/tVSobnHgNnceGSH3Y8DY5cASs=
|
||||
github.com/adrg/strutil v0.2.2 h1:XSd9+o2xaOon2oRum0JymNT+f0nfLiAiDzGOcjcIsMI=
|
||||
github.com/adrg/strutil v0.2.2/go.mod h1:EF2fjOFlGTepljfI+FzgTG13oXthR7ZAil9/aginnNQ=
|
||||
github.com/adrg/sysfont v0.1.2 h1:MSU3KREM4RhsQ+7QgH7wPEPTgAgBIz0Hw6Nd4u7QgjE=
|
||||
github.com/adrg/sysfont v0.1.2/go.mod h1:6d3l7/BSjX9VaeXWJt9fcrftFaD/t7l11xgSywCPZGk=
|
||||
github.com/adrg/xdg v0.3.0 h1:BO+k4wFj0IoTolBF1Apn8oZrX3LQrEbBA8+/9vyW9J4=
|
||||
github.com/adrg/xdg v0.3.0/go.mod h1:7I2hH/IT30IsupOpKZ5ue7/qNi3CoKzD6tL3HwpaRMQ=
|
||||
github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek=
|
||||
github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
|
||||
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
|
||||
|
|
|
@ -40,7 +40,6 @@ 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"
|
||||
|
@ -995,20 +994,7 @@ func processPDFFooter(pdfCtx *pdfcpu.Context) {
|
|||
}
|
||||
footer := buf.String()
|
||||
|
||||
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]
|
||||
}
|
||||
fontName := util.InstallPDFFonts()
|
||||
|
||||
pos := "bc"
|
||||
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)
|
||||
|
|
|
@ -22,7 +22,10 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/88250/gulu"
|
||||
"github.com/88250/pdfcpu/pkg/api"
|
||||
"github.com/88250/pdfcpu/pkg/font"
|
||||
"github.com/ConradIrwin/font/sfnt"
|
||||
"github.com/adrg/sysfont"
|
||||
"github.com/flopp/go-findfont"
|
||||
"github.com/siyuan-note/logging"
|
||||
ttc "golang.org/x/image/font/sfnt"
|
||||
|
@ -31,23 +34,43 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
preferredFonts = []string{"Microsoft YaHei", "SimSun", "微软雅黑", "宋体", "仿宋", "Helvetica Neue", "Luxi Sans", "DejaVu Sans", "sans-serif", "Arial"}
|
||||
preferredPDFFonts = []string{"Microsoft YaHei", "SimSun", "微软雅黑", "宋体", "仿宋", "Helvetica Neue", "Luxi Sans", "DejaVu Sans", "sans-serif", "Arial"}
|
||||
preferredPDFWatermarkFonts = []string{"MicrosoftYaHei", "SimSun", "LucidaSans", "Corbel", "Helvetica", "Arial", "sans-serif"}
|
||||
)
|
||||
|
||||
func GetPreferredFontFilePath(currentLanguage string) *Font {
|
||||
fonts := loadFonts(currentLanguage)
|
||||
|
||||
for _, font := range fonts {
|
||||
logging.LogInfof("font: %+v", font)
|
||||
func InstallPDFFonts() string {
|
||||
names := font.UserFontNames()
|
||||
if 0 < len(names) {
|
||||
return getPreferredPDFWatermarkFont(names)
|
||||
}
|
||||
|
||||
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
|
||||
finder := sysfont.NewFinder(&sysfont.FinderOpts{Extensions: []string{".ttf", ".ttc"}})
|
||||
var fontPaths []string
|
||||
for _, preferredFont := range preferredPDFFonts {
|
||||
f := finder.Match(preferredFont)
|
||||
if nil != f {
|
||||
fontPaths = append(fontPaths, f.Filename)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
if err := api.InstallFonts(fontPaths); nil != err {
|
||||
logging.LogErrorf("install font failed: %s", err)
|
||||
}
|
||||
|
||||
names = font.UserFontNames()
|
||||
logging.LogInfof("pdf fonts [%s]", strings.Join(names, ", "))
|
||||
return getPreferredPDFWatermarkFont(names)
|
||||
}
|
||||
|
||||
func getPreferredPDFWatermarkFont(userFontNames []string) string {
|
||||
for _, preferredFont := range preferredPDFWatermarkFonts {
|
||||
for _, userFont := range userFontNames {
|
||||
if preferredFont == userFont {
|
||||
return preferredFont
|
||||
}
|
||||
}
|
||||
}
|
||||
return "Helvetica"
|
||||
}
|
||||
|
||||
func GetSysFonts(currentLanguage string) (ret []string) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue