fix: fallback to username if chars in name are not supported for avatars

This commit is contained in:
Elias Schneider
2026-10-11 00:54:58 +02:00
parent 3355b656b5
commit e2cacac193
6 changed files with 95 additions and 75 deletions
@@ -7,6 +7,8 @@ import (
"image"
"image/color"
"io"
"strings"
"sync"
"github.com/boxes-ltd/imaging"
"github.com/disintegration/imageorient"
@@ -15,6 +17,7 @@ import (
"golang.org/x/image/math/fixed"
"golang.org/x/image/webp"
"github.com/pocket-id/pocket-id/backend/internal/utils"
"github.com/pocket-id/pocket-id/backend/resources"
)
@@ -84,21 +87,58 @@ func centerSquare(img image.Image) image.Image {
return sub.SubImage(image.Rect(x, y, x+side, y+side))
}
var loadInitialsFont = sync.OnceValues(func() (*opentype.Font, error) {
fontBytes, err := resources.FS.ReadFile("fonts/Gloock.ttf")
if err != nil {
return nil, fmt.Errorf("failed to read font file: %w", err)
}
fontFace, err := opentype.Parse(fontBytes)
if err != nil {
return nil, fmt.Errorf("failed to parse font: %w", err)
}
return fontFace, nil
})
// Initials returns the initials shown on the default profile picture
func Initials(firstName, lastName, username string) string {
// The font only covers Latin, so names it can't render fall back to the username, which is always ASCII
initials := strings.ToUpper(utils.GetFirstCharacter(firstName) + utils.GetFirstCharacter(lastName))
if initials != "" && canRenderInitials(initials) {
return initials
}
return strings.ToUpper(utils.GetFirstCharacter(username))
}
// canRenderInitials reports whether the initials font has a glyph for every character of the initials
// The font only covers Latin, so other scripts would otherwise be drawn as empty boxes
func canRenderInitials(initials string) bool {
fontFace, err := loadInitialsFont()
if err != nil {
return false
}
for _, r := range initials {
idx, err := fontFace.GlyphIndex(nil, r)
if err != nil || idx == 0 {
return false
}
}
return true
}
// CreateDefaultProfilePicture creates a profile picture with the initials
func CreateDefaultProfilePicture(initials string) (*bytes.Buffer, error) {
// Create a blank image with a white background
img := imaging.New(profilePictureSize, profilePictureSize, color.RGBA{R: 255, G: 255, B: 255, A: 255})
// Load the font
fontBytes, err := resources.FS.ReadFile("fonts/Gloock.ttf")
fontFace, err := loadInitialsFont()
if err != nil {
return nil, fmt.Errorf("failed to read font file: %w", err)
}
// Parse the font
fontFace, err := opentype.Parse(fontBytes)
if err != nil {
return nil, fmt.Errorf("failed to parse font: %w", err)
return nil, err
}
// Create a font.Face with a specific size
@@ -0,0 +1,41 @@
package profilepicture
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestInitials(t *testing.T) {
tests := []struct {
name string
firstName string
lastName string
username string
expected string
}{
{name: "first and last name", firstName: "John", lastName: "Doe", username: "username", expected: "JD"},
{name: "first name only", firstName: "John", username: "username", expected: "J"},
{name: "last name only", lastName: "Doe", username: "username", expected: "D"},
{name: "accented Latin name", firstName: "émile", lastName: "Öz", username: "username", expected: "ÉÖ"},
{name: "name the font can't render falls back to username", firstName: "민준", lastName: "김", username: "kim", expected: "K"},
{name: "partially unrenderable name falls back to username", firstName: "John", lastName: "Иванов", username: "ivan", expected: "I"},
{name: "ASCII username", username: "username", expected: "U"},
{name: "single-character username", username: "a", expected: "A"},
{name: "empty username", expected: ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, Initials(tt.firstName, tt.lastName, tt.username))
})
}
}
func TestCanRenderInitials(t *testing.T) {
assert.True(t, canRenderInitials("ES"))
assert.True(t, canRenderInitials("ÉÖ"))
assert.True(t, canRenderInitials("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
assert.False(t, canRenderInitials("한글"))
assert.False(t, canRenderInitials("EЖ"))
}