Skip to content

Commit

Permalink
feat:支持otf
Browse files Browse the repository at this point in the history
  • Loading branch information
yeyudekuangxiang committed Nov 5, 2022
1 parent 7f69720 commit 17f1dd2
Show file tree
Hide file tree
Showing 11 changed files with 354 additions and 99 deletions.
1 change: 1 addition & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func main() {
//文字自动分行
fillText := imagedraw.NewText("夜雨寄北(李商隐)君问归期未有期,巴山夜雨涨秋池。何当共剪西窗烛,却话巴山夜雨时。")
fillText.SetFontSize(60)
fillText.SetFont(imagedraw.SiYuanHeiYiBold())
fillText.SetLineHeight(90)
fillText.SetTextAlign("left")
fillText.SetArea(1300, 0, 500, 1000)
Expand Down
12 changes: 8 additions & 4 deletions fill.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import (
)

type FillItem interface {
draw(img draw.Image) draw.Image
draw(img draw.Image) (draw.Image, error)
}

func fill(dst draw.Image, items ...FillItem) draw.Image {
func fill(dst draw.Image, items ...FillItem) (draw.Image, error) {
var err error
for _, item := range items {
dst = item.draw(dst)
dst, err = item.draw(dst)
if err != nil {
return nil, err
}
}
return dst
return dst, nil
}
21 changes: 21 additions & 0 deletions fonts/font.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package fonts

import "encoding/base64"

func SiYuanHeiTiTTF() []byte {
dbuf := make([]byte, base64.StdEncoding.DecodedLen(len(siyuanheiti)))
_, err := base64.StdEncoding.Decode(dbuf, []byte(siyuanheiti))
if err != nil {
panic(err)
}
return dbuf
}

func SiYuanHeiTiOTFBold() []byte {
dbuf := make([]byte, base64.StdEncoding.DecodedLen(len(siyuanheitibold)))
_, err := base64.StdEncoding.Decode(dbuf, []byte(siyuanheitibold))
if err != nil {
panic(err)
}
return dbuf
}
3 changes: 3 additions & 0 deletions fonts/syht.go

Large diffs are not rendered by default.

Binary file added fonts/syht.ttf
Binary file not shown.
3 changes: 3 additions & 0 deletions fonts/syhtbold.go

Large diffs are not rendered by default.

Binary file added fonts/syhtbold.otf
Binary file not shown.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/yeyudekuangxiang/imagedraw
go 1.14

require (
github.com/fogleman/gg v1.3.0 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d
)
5 changes: 1 addition & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
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-20210622092929-e6eecd499c2c h1:FRR4fGZm/CMwZka5baQ4z8c8StbxJOMjS/45e0BAxK0=
golang.org/x/image v0.0.0-20210622092929-e6eecd499c2c/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d h1:RNPAfi2nHY7C2srAV8A49jpsYr0ADedCk1wq6fTMTvs=
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
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=
14 changes: 9 additions & 5 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,10 +670,10 @@ func (i *Image) SetOp(op draw.Op) *Image {
}

//实现FillItem接口
func (i *Image) draw(dst draw.Image) draw.Image {
func (i *Image) draw(dst draw.Image) (draw.Image, error) {
//resizeImage := resize(i.img, i.area.Max.X-i.area.Min.X, i.area.Max.Y-i.area.Min.Y, BilinearInterpolation)
draw.Draw(dst, i.area, i.img, image.Pt(0, 0), i.op)
return dst
return dst, nil
}

//截取圆形并且返回一个新的对象 (x,y)原点坐标 r圆半径长度
Expand All @@ -695,9 +695,13 @@ func (i Image) Resize(w, h int, resizeType ...ResizeType) *Image {
}

//将其他元素填充进本图片
func (i *Image) Fill(item ...FillItem) *Image {
i.img = fill(i.img, item...)
return i
func (i *Image) Fill(item ...FillItem) (*Image, error) {
img, err := fill(i.img, item...)
if err != nil {
return nil, err
}
i.img = img
return i, nil
}

//将图片保存在本地
Expand Down
Loading

0 comments on commit 17f1dd2

Please sign in to comment.