From 24479c83934bf2719ca53fc1db9997c20734653d Mon Sep 17 00:00:00 2001 From: Bassel Al Sheikh Ali Date: Sun, 29 Dec 2024 15:11:49 +0300 Subject: [PATCH] Add Arabic Support to Avatar Generator --- app/handlers/images.go | 9 ++++++++- app/pkg/letteravatar/letteravatar.go | 14 +++----------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/app/handlers/images.go b/app/handlers/images.go index fc04f8769..443b3aa41 100644 --- a/app/handlers/images.go +++ b/app/handlers/images.go @@ -34,25 +34,32 @@ func LetterAvatar() web.HandlerFunc { // URL decode the name decodedName, err := url.QueryUnescape(name) if err != nil { + log.Error(c, err) return c.Failure(err) } size, err := c.QueryParamAsInt("size") if err != nil { + log.Error(c, err) return c.BadRequest(web.Map{}) } size = between(size, 50, 200) - img, err := letteravatar.Draw(size, letteravatar.Extract(decodedName), &letteravatar.Options{ + extractedLetter := letteravatar.Extract(decodedName) + log.Debugf(c, "Generating letter avatar for name '%s', extracted letter: '%s'", decodedName, extractedLetter) + + img, err := letteravatar.Draw(size, extractedLetter, &letteravatar.Options{ PaletteKey: fmt.Sprintf("%s:%s", id, decodedName), }) if err != nil { + log.Error(c, err) return c.Failure(err) } buf := new(bytes.Buffer) err = png.Encode(buf, img) if err != nil { + log.Error(c, err) return c.Failure(err) } diff --git a/app/pkg/letteravatar/letteravatar.go b/app/pkg/letteravatar/letteravatar.go index 12ae15717..360a6eb3a 100644 --- a/app/pkg/letteravatar/letteravatar.go +++ b/app/pkg/letteravatar/letteravatar.go @@ -1,7 +1,6 @@ package letteravatar import ( - "embed" "image" "image/color" "image/draw" @@ -12,11 +11,9 @@ import ( "github.com/golang/freetype" "github.com/golang/freetype/truetype" "golang.org/x/image/font" + "golang.org/x/image/font/gofont/goregular" ) -//go:embed fonts -var fonts embed.FS - // Options contains the options for letter avatar generation type Options struct { PaletteKey string @@ -53,13 +50,8 @@ func Draw(size int, text string, opts *Options) (image.Image, error) { // Draw the background draw.Draw(img, img.Bounds(), &image.Uniform{bg}, image.Point{}, draw.Src) - // Load the font - fontBytes, err := fonts.ReadFile("fonts/IBMPlexSansArabic-Regular.ttf") - if err != nil { - return nil, err - } - - f, err := truetype.Parse(fontBytes) + // Use the built-in Go font for now + f, err := truetype.Parse(goregular.TTF) if err != nil { return nil, err }