Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support go module #4

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
40 changes: 40 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Reference https://github.com/github/gitignore/blob/master/Go.gitignore
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
vendor/

# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# OS General
Thumbs.db
.DS_Store

# project
*.cert
*.key
*.log
bin/

# Develop tools
.vscode/
.idea/
*.swp

# project directories and files
logs/
coverage.txt
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
Binary file removed example/Alice.png
Binary file not shown.
Binary file removed example/Bob.png
Binary file not shown.
Binary file removed example/Carol.png
Binary file not shown.
Binary file removed example/Dave.png
Binary file not shown.
Binary file removed example/Eve.png
Binary file not shown.
Binary file removed example/Frank.png
Binary file not shown.
Binary file removed example/Gloria.png
Binary file not shown.
Binary file removed example/Henry.png
Binary file not shown.
Binary file removed example/Isabella.png
Binary file not shown.
Binary file removed example/James.png
Binary file not shown.
Binary file added example/firstletter/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/firstletter/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/firstletter/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/firstletter/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/firstletter/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/firstletter/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/firstletter/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/firstletter/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/firstletter/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/firstletter/James.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions example/example.go → example/firstletter/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"unicode/utf8"

"github.com/disintegration/letteravatar"
"github.com/UnderTreeTech/letteravatar"
)

var names = []string{
Expand All @@ -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
Binary file added example/firstletter/Жозефина.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/firstletter/Ярослав.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/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
Binary file added example/multiletter/Eve.png
Binary file added example/multiletter/Frank.png
Binary file added example/multiletter/Gloria.png
Binary file added example/multiletter/Henry.png
Binary file added example/multiletter/Isabella.png
Binary file added example/multiletter/James.png
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/UnderTreeTech/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
Binary file added example/multiletter/Ярослав.png
Binary file added example/multiletter/中国文化.png
Binary file removed example/Жозефина.png
Diff not rendered.
Binary file removed example/Ярослав.png
Diff not rendered.
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/UnderTreeTech/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=