Skip to content

Commit

Permalink
repo: update static read from bindata
Browse files Browse the repository at this point in the history
  • Loading branch information
laojianzi committed Nov 23, 2020
1 parent 9a98263 commit 6cacc4d
Show file tree
Hide file tree
Showing 4 changed files with 315 additions and 7 deletions.
10 changes: 5 additions & 5 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
// DefaultConfig config on mdavatar default
// can be used directly
var DefaultConfig = &Config{
avatarTextHandle: DefaultAvatarTextHandle,
avatarSize: 256,
letterFont: "static/Roboto-Light.ttf",
asianFont: "static/NotoSansSC-Regular.otf",
colors: DefaultColors,
avatarTextHandle: DefaultAvatarTextHandle,
avatarSize: 256,
colors: DefaultColors,
letterFontContent: MustAsset("static/Roboto-Light.ttf"),
asianFontContent: MustAsset("static/NotoSansSC-Regular.otf"),
}

// DefaultColors colors on mdavatar default
Expand Down
39 changes: 37 additions & 2 deletions mdavatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ type Config struct {
avatarSize int
padding int
letterFont string
letterFontContent []byte
asianFont string
asianFontContent []byte
enableAsianFontChar bool
colors []color.RGBA
background *image.RGBA
Expand Down Expand Up @@ -95,14 +97,16 @@ func (config *Config) drawText() (*image.RGBA, error) {
var (
fgColor image.Image
fontSize = 128.0
err error
)
fgColor = image.White
fontBytes, err := ioutil.ReadFile("static/NotoSansSC-Regular.otf")

fontContent, err := config.fontContent()
if err != nil {
return nil, err
}

fontFace, err := opentype.Parse(fontBytes)
fontFace, err := opentype.Parse(fontContent)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -132,3 +136,34 @@ func (config *Config) drawText() (*image.RGBA, error) {
fontDrawer.DrawString(config.avatarText)
return config.background, err
}

func (config *Config) fontContent() ([]byte, error) {
var content []byte
var err error

if config.enableAsianFontChar {
if config.asianFont != "" {
content, err = ioutil.ReadFile(config.asianFont)
if err != nil {
return nil, err
}

config.asianFontContent = content
}

content = []byte(config.asianFontContent)
} else {
if config.letterFont != "" {
content, err = ioutil.ReadFile(config.letterFont)
if err != nil {
return nil, err
}

config.letterFontContent = content
}

content = []byte(config.asianFontContent)
}

return content, nil
}
3 changes: 3 additions & 0 deletions static.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package mdavatar

//go:generate go-bindata -nomemcopy -pkg=mdavatar -ignore="\\.DS_Store|README.md|TRANSLATORS" -debug=false -o=static_gen.go static/...
270 changes: 270 additions & 0 deletions static_gen.go

Large diffs are not rendered by default.

0 comments on commit 6cacc4d

Please sign in to comment.