Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type Options struct {
Palette []color.Color
LetterColor color.Color

// FontSize is used to set font size
FontSize int

// PaletteKey is used to pick the background color from the Palette.
// Using the same PaletteKey leads to the same background color being picked.
// If PaletteKey is empty (default) the background color is picked randomly.
Expand All @@ -30,7 +33,7 @@ var defaultLetterColor = color.RGBA{0xf0, 0xf0, 0xf0, 0xf0}

// Draw generates a new letter-avatar image of the given size using the given letter
// with the given options. Default parameters are used if a nil *Options is passed.
func Draw(size int, letter rune, options *Options) (image.Image, error) {
func Draw(size int, letter []rune, options *Options) (image.Image, error) {
font := defaultFont
if options != nil && options.Font != nil {
font = options.Font
Expand All @@ -55,14 +58,18 @@ func Draw(size int, letter rune, options *Options) (image.Image, error) {
}
}

return drawAvatar(bgColor, letterColor, font, size, letter)
fontSize := float64(options.FontSize)
if options.FontSize == 0 {
fontSize = float64(size) * 0.6
}

return drawAvatar(bgColor, letterColor, font, size, fontSize, letter)
}

func drawAvatar(bgColor, fgColor color.Color, font *truetype.Font, size int, letter rune) (image.Image, error) {
func drawAvatar(bgColor, fgColor color.Color, font *truetype.Font, size int, fontSize float64, letter []rune) (image.Image, error) {
dst := newRGBA(size, size, bgColor)

fontSize := float64(size) * 0.6
src, err := drawString(bgColor, fgColor, font, fontSize, string(letter))
src, err := drawString(bgColor, fgColor, font, fontSize, letter)
if err != nil {
return nil, err
}
Expand All @@ -73,12 +80,15 @@ func drawAvatar(bgColor, fgColor color.Color, font *truetype.Font, size int, let
return dst, nil
}

func drawString(bgColor, fgColor color.Color, font *truetype.Font, fontSize float64, str string) (image.Image, error) {
func drawString(bgColor, fgColor color.Color, font *truetype.Font, fontSize float64, letter []rune) (image.Image, error) {
c := freetype.NewContext()
c.SetDPI(72)

bb := font.Bounds(c.PointToFixed(fontSize))
w := bb.Max.X.Ceil() - bb.Min.X.Floor()
if len(letter) > 0 {
w = w + int(fontSize)*(len(letter)-1)
}
h := bb.Max.Y.Ceil() - bb.Min.Y.Floor()

dst := newRGBA(w, h, bgColor)
Expand All @@ -90,7 +100,7 @@ func drawString(bgColor, fgColor color.Color, font *truetype.Font, fontSize floa
c.SetFontSize(fontSize)
c.SetFont(font)

p, err := c.DrawString(str, fixed.Point26_6{X: 0, Y: bb.Max.Y})
p, err := c.DrawString(string(letter), fixed.Point26_6{X: 0, Y: bb.Max.Y})
if err != nil {
return nil, err
}
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
2 changes: 1 addition & 1 deletion example/example.go → example/firstletter/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
for _, name := range names {
firstLetter, _ := utf8.DecodeRuneInString(name)

img, err := letteravatar.Draw(75, firstLetter, nil)
img, err := letteravatar.Draw(75, []rune{firstLetter}, &letteravatar.Options{})
if err != nil {
log.Fatal(err)
}
Expand Down
File renamed without changes
Binary file added example/multiletter/Alice.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/multiletter/Bob.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/multiletter/Carol.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/multiletter/Dave.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/multiletter/Eve.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/multiletter/Frank.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/multiletter/Gloria.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/multiletter/Henry.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/multiletter/Isabella.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/multiletter/James.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/multiletter/PingFang-SC-Regular.ttf
Binary file not shown.
69 changes: 69 additions & 0 deletions example/multiletter/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"image/png"
"io/ioutil"
"log"
"os"

"github.com/golang/freetype/truetype"

"github.com/disintegration/letteravatar"
)

var names = []string{
"Alice",
"Bob",
"Carol",
"Dave",
"Eve",
"Frank",
"Gloria",
"Henry",
"Isabella",
"James",
"Жозефина",
"Ярослав",
"中国文化",
}

func main() {
font, err := loadFont("./PingFang-SC-Regular.ttf")
if err != nil {
log.Fatal(err)
}

opt := &letteravatar.Options{
FontSize: 20,
Font: font,
}
for _, name := range names {

img, err := letteravatar.Draw(100, ([]rune(name))[:5], opt)
if err != nil {
log.Fatal(err)
}

file, err := os.Create(name + ".png")
if err != nil {
log.Fatal(err)
}

err = png.Encode(file, img)
if err != nil {
log.Fatal(err)
}
}
}

func loadFont(path string) (*truetype.Font, error) {
fontBytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
f, err := truetype.Parse(fontBytes)
if err != nil {
return nil, err
}
return f, nil
}
Binary file added example/multiletter/Жозефина.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/multiletter/Ярослав.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/multiletter/中国文化.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/disintegration/letteravatar

go 1.14

require (
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
golang.org/x/image v0.0.0-20211028202545-6944b10bf410
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 h1:hTftEOvwiOq2+O8k2D5/Q7COC7k5Qcrgc2TFURJYnvQ=
golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=