refactor font loading

This commit is contained in:
Gabriel Augendre 2024-11-26 18:26:45 +01:00
parent c6c4e6ef38
commit 25e083ba71
5 changed files with 84 additions and 51 deletions

View file

@ -1,4 +1,4 @@
package main package fonts
import ( import (
"fmt" "fmt"
@ -6,13 +6,13 @@ import (
"github.com/llgcode/draw2d" "github.com/llgcode/draw2d"
) )
type MyFontCache map[string]*truetype.Font type Cache map[string]*truetype.Font
func (fc MyFontCache) Store(fd draw2d.FontData, font *truetype.Font) { func (fc Cache) Store(fd draw2d.FontData, font *truetype.Font) {
fc[fd.Name] = font fc[fd.Name] = font
} }
func (fc MyFontCache) Load(fd draw2d.FontData) (*truetype.Font, error) { func (fc Cache) Load(fd draw2d.FontData) (*truetype.Font, error) {
font, stored := fc[fd.Name] font, stored := fc[fd.Name]
if !stored { if !stored {
return nil, fmt.Errorf("font %s is not stored in font cache", fd.Name) return nil, fmt.Errorf("font %s is not stored in font cache", fd.Name)

View file

@ -1,20 +1,71 @@
package fonts package fonts
import _ "embed" import (
_ "embed"
//go:embed ttf/OpenSans-Bold.ttf "fmt"
var Bold []byte "github.com/golang/freetype/truetype"
"github.com/llgcode/draw2d"
)
//go:embed ttf/OpenSans-Regular.ttf //go:embed ttf/OpenSans-Regular.ttf
var Regular []byte var RegularTTF []byte
//go:embed ttf/OpenSans-SemiBold.ttf
var SemiBoldTTF []byte
//go:embed ttf/OpenSans-Bold.ttf
var BoldTTF []byte
//go:embed ttf/OpenSans-Italic.ttf //go:embed ttf/OpenSans-Italic.ttf
var Italic []byte var ItalicTTF []byte
//go:embed ttf/Phosphor.ttf //go:embed ttf/Phosphor.ttf
var Icons []byte var IconsTTF []byte
const ( const (
IconXOffset = 38 IconXOffset = 38
IconYOffset = 2 IconYOffset = 2
) )
const (
Regular = "regular"
SemiBold = "semibold"
Bold = "bold"
Italic = "italic"
Icons = "icons"
)
func NewCache() (Cache, error) {
cache := Cache{}
fonts := []struct {
ttf []byte
name string
}{
{RegularTTF, Regular},
{SemiBoldTTF, SemiBold},
{BoldTTF, Bold},
{ItalicTTF, Italic},
{IconsTTF, Icons},
}
for _, font := range fonts {
err := loadFont(cache, font.ttf, font.name)
if err != nil {
return cache, fmt.Errorf("loading font %q: %w", font.name, err)
}
}
return cache, nil
}
func loadFont(fontCache Cache, ttf []byte, name string) error {
font, err := truetype.Parse(ttf)
if err != nil {
return fmt.Errorf("parsing font %v: %w", name, err)
}
fontCache.Store(draw2d.FontData{Name: name}, font)
return nil
}

Binary file not shown.

35
img.go
View file

@ -149,8 +149,7 @@ func getImg(ctx context.Context, nowFunc func() time.Time, transportsClient *tra
} }
func drawMsg(gc *draw2dimg.GraphicContext, quote string) { func drawMsg(gc *draw2dimg.GraphicContext, quote string) {
text(gc, quote, 15, leftX, 450, fontItalic) text(gc, quote, 15, leftX, 450, fonts.Italic)
} }
func drawWeather(ctx context.Context, gc *draw2dimg.GraphicContext, wthr *weather.Prevision) { func drawWeather(ctx context.Context, gc *draw2dimg.GraphicContext, wthr *weather.Prevision) {
@ -172,15 +171,15 @@ func drawWeather(ctx context.Context, gc *draw2dimg.GraphicContext, wthr *weathe
slog.ErrorContext(ctx, "Failed to draw weather icon", "err", err) slog.ErrorContext(ctx, "Failed to draw weather icon", "err", err)
} }
text(gc, formatTemp(wthr.Current.Temp), 23, leftX, 125, fontRegular) text(gc, formatTemp(wthr.Current.Temp), 23, leftX, 125, fonts.Regular)
const xAlign = 120 const xAlign = 120
const fontSize = 18 const fontSize = 18
text(gc, "journée", fontSize, xAlign, 35, fontBold) text(gc, "journée", fontSize, xAlign, 35, fonts.Bold)
text(gc, "max "+formatTemp(daily.Temp.Max), fontSize, xAlign, 65, fontRegular) text(gc, "max "+formatTemp(daily.Temp.Max), fontSize, xAlign, 65, fonts.Regular)
text(gc, fmt.Sprintf("pluie %v%%", int(math.Round(daily.Pop*100))), fontSize, xAlign, 95, fontRegular) text(gc, fmt.Sprintf("pluie %v%%", int(math.Round(daily.Pop*100))), fontSize, xAlign, 95, fonts.Regular)
text(gc, dailyWeather.Description, fontSize, xAlign, 125, fontRegular) text(gc, dailyWeather.Description, fontSize, xAlign, 125, fonts.Regular)
} }
func drawWeatherIcon(gc *draw2dimg.GraphicContext, dailyWeather weather.Weather) error { func drawWeatherIcon(gc *draw2dimg.GraphicContext, dailyWeather weather.Weather) error {
@ -209,21 +208,21 @@ func drawVelov(gc *draw2dimg.GraphicContext, station *transports.Station, yOffse
return return
} }
text(gc, station.Name, 23, rightX, yOffset, fontBold) text(gc, station.Name, 23, rightX, yOffset, fonts.Bold)
yOffset += 30 yOffset += 30
text(gc, "\uE0D6", 22, rightX, yOffset+fonts.IconYOffset, fontIcons) // bike icon text(gc, "\uE0D6", 22, rightX, yOffset+fonts.IconYOffset, fonts.Icons) // bike icon
text(gc, strconv.Itoa(station.BikesAvailable), 22, rightX+fonts.IconXOffset, yOffset, fontRegular) text(gc, strconv.Itoa(station.BikesAvailable), 22, rightX+fonts.IconXOffset, yOffset, fonts.Regular)
nextCol := rightX + 100.0 nextCol := rightX + 100.0
text(gc, "\uEC08", 22, nextCol, yOffset+fonts.IconYOffset, fontIcons) // parking icon text(gc, "\uEC08", 22, nextCol, yOffset+fonts.IconYOffset, fonts.Icons) // parking icon
text(gc, strconv.Itoa(station.DocksAvailable), 22, nextCol+fonts.IconXOffset, yOffset, fontRegular) text(gc, strconv.Itoa(station.DocksAvailable), 22, nextCol+fonts.IconXOffset, yOffset, fonts.Regular)
} }
func drawDate(gc *draw2dimg.GraphicContext, now time.Time) { func drawDate(gc *draw2dimg.GraphicContext, now time.Time) {
text(gc, now.Format("15:04"), 110, leftX, 300, fontBold) text(gc, now.Format("15:04"), 110, leftX, 300, fonts.Bold)
text(gc, getDate(now), 30, leftX, 345, fontRegular) text(gc, getDate(now), 30, leftX, 345, fonts.Regular)
} }
func drawFete(gc *draw2dimg.GraphicContext, fetes *fete.Fete) { func drawFete(gc *draw2dimg.GraphicContext, fetes *fete.Fete) {
@ -231,7 +230,7 @@ func drawFete(gc *draw2dimg.GraphicContext, fetes *fete.Fete) {
return return
} }
text(gc, fmt.Sprintf("On fête les %s", fetes.Name), 18, leftX, 380, fontRegular) text(gc, fmt.Sprintf("On fête les %s", fetes.Name), 18, leftX, 380, fonts.Regular)
} }
func drawTCL(gc *draw2dimg.GraphicContext, passages *transports.Passages, yoffset float64) { func drawTCL(gc *draw2dimg.GraphicContext, passages *transports.Passages, yoffset float64) {
@ -241,11 +240,11 @@ func drawTCL(gc *draw2dimg.GraphicContext, passages *transports.Passages, yoffse
for i, passage := range passages.Passages { for i, passage := range passages.Passages {
x := float64(rightX + i*120) x := float64(rightX + i*120)
text(gc, "\uE106", 23, x, yoffset+fonts.IconYOffset, fontIcons) text(gc, "\uE106", 23, x, yoffset+fonts.IconYOffset, fonts.Icons)
text(gc, passage.Ligne, 23, x+fonts.IconXOffset, yoffset, fontBold) text(gc, passage.Ligne, 23, x+fonts.IconXOffset, yoffset, fonts.Bold)
for j, delay := range passage.Delays { for j, delay := range passage.Delays {
y := yoffset + float64(j+1)*35 y := yoffset + float64(j+1)*35
text(gc, delay, 22, x, y, fontRegular) text(gc, delay, 22, x, y, fonts.Regular)
if j >= 2 { // limit number of delays displayed if j >= 2 { // limit number of delays displayed
break break
} }

27
main.go
View file

@ -7,7 +7,6 @@ import (
"github.com/Crocmagnon/display-epaper/home_assistant" "github.com/Crocmagnon/display-epaper/home_assistant"
"github.com/Crocmagnon/display-epaper/transports" "github.com/Crocmagnon/display-epaper/transports"
"github.com/Crocmagnon/display-epaper/weather" "github.com/Crocmagnon/display-epaper/weather"
"github.com/golang/freetype/truetype"
"github.com/llgcode/draw2d" "github.com/llgcode/draw2d"
_ "golang.org/x/image/bmp" _ "golang.org/x/image/bmp"
"log/slog" "log/slog"
@ -15,23 +14,16 @@ import (
"time" "time"
) )
const (
fontRegular = "regular"
fontBold = "bold"
fontItalic = "italic"
fontIcons = "icons"
)
func main() { func main() {
ctx := context.Background() ctx := context.Background()
slog.InfoContext(ctx, "starting...") slog.InfoContext(ctx, "starting...")
fontCache := MyFontCache{}
loadFont(ctx, fontCache, fonts.Regular, fontRegular) fontCache, err := fonts.NewCache()
loadFont(ctx, fontCache, fonts.Bold, fontBold) if err != nil {
loadFont(ctx, fontCache, fonts.Italic, fontItalic) slog.ErrorContext(ctx, "could not create font cache", "error", err.Error())
loadFont(ctx, fontCache, fonts.Icons, fontIcons) os.Exit(1)
}
draw2d.SetFontCache(fontCache) draw2d.SetFontCache(fontCache)
@ -85,12 +77,3 @@ func main() {
slog.InfoContext(ctx, "done") slog.InfoContext(ctx, "done")
} }
func loadFont(ctx context.Context, fontCache MyFontCache, ttf []byte, name string) {
font, err := truetype.Parse(ttf)
if err != nil {
slog.ErrorContext(ctx, "error loading font", "err", err)
os.Exit(1)
}
fontCache.Store(draw2d.FontData{Name: name}, font)
}