Feat/Added label maker custom font (#1038)

* Add label maker font config

* Add document for label maker font config

* Add test for custom font

* Fix custom font setup documentation

- Fallback font is gofont which don't support CJK characters

* Fix golangci-lint error

* Update custom-font-setup.md

* Fix typo
This commit is contained in:
rienkim
2025-10-09 03:49:22 +09:00
committed by GitHub
parent 76154263e0
commit ef52009f57
8 changed files with 431 additions and 2 deletions

View File

@@ -27,6 +27,13 @@ import (
"golang.org/x/image/font/gofont/gomedium"
)
type FontType int
const (
FontTypeRegular FontType = iota
FontTypeBold
)
type GenerateParameters struct {
Width int
Height int
@@ -140,6 +147,48 @@ func wrapText(text string, face font.Face, maxWidth int, maxHeight int, lineHeig
return wrappedLines, ""
}
func loadFont(cfg *config.Config, fontType FontType) (*truetype.Font, error) {
var fontPath *string
var fallbackData []byte
switch fontType {
case FontTypeRegular:
if cfg != nil && cfg.LabelMaker.RegularFontPath != nil {
fontPath = cfg.LabelMaker.RegularFontPath
}
fallbackData = gomedium.TTF
case FontTypeBold:
if cfg != nil && cfg.LabelMaker.BoldFontPath != nil {
fontPath = cfg.LabelMaker.BoldFontPath
}
fallbackData = gobold.TTF
default:
return nil, fmt.Errorf("unknown font type: %d", fontType)
}
if fontPath != nil && *fontPath != "" {
data, err := os.ReadFile(*fontPath)
if err != nil {
log.Printf("Failed to load font from %s: %v, using fallback font", *fontPath, err)
} else {
font, err := truetype.Parse(data)
if err != nil {
log.Printf("Failed to parse font from %s: %v, using fallback font", *fontPath, err)
} else {
log.Printf("Successfully loaded font from %s", *fontPath)
return font, nil
}
}
}
font, err := truetype.Parse(fallbackData)
if err != nil {
return nil, err
}
return font, nil
}
func GenerateLabel(w io.Writer, params *GenerateParameters, cfg *config.Config) error {
if err := params.Validate(); err != nil {
return err
@@ -165,12 +214,12 @@ func GenerateLabel(w io.Writer, params *GenerateParameters, cfg *config.Config)
qr.DisableBorder = true
qrImage := qr.Image(params.QrSize)
regularFont, err := truetype.Parse(gomedium.TTF)
regularFont, err := loadFont(cfg, FontTypeRegular)
if err != nil {
return err
}
boldFont, err := truetype.Parse(gobold.TTF)
boldFont, err := loadFont(cfg, FontTypeBold)
if err != nil {
return err
}