-
Notifications
You must be signed in to change notification settings - Fork 17
/
text2pic.go
100 lines (88 loc) · 2.11 KB
/
text2pic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package text2pic
import (
"fmt"
"github.com/golang/freetype/truetype"
"golang.org/x/image/math/fixed"
"image"
"image/color"
"image/draw"
"image/jpeg"
"image/png"
"io"
)
type Color image.Image
var (
ColorRed = image.NewUniform(color.RGBA{0xFF, 0x00, 0x00, 0xff})
ColorGreen = image.NewUniform(color.RGBA{0x00, 0xFF, 0x00, 0xff})
ColorBlue = image.NewUniform(color.RGBA{0x00, 0x00, 0xFF, 0xff})
ColorWhite = image.White
ColorBlack = image.Black
TypePng = 1
TypeJpeg = 2
)
type Padding struct {
Left int
Right int
Bottom int
Top int
LineSpace int
}
type Configure struct {
Width int
BgColor Color
}
func NewTextPicture(conf Configure) *TextPicture {
pic := new(TextPicture)
pic.conf = conf
return pic
}
type TextPicture struct {
text string
conf Configure
lines []line
}
func (this *TextPicture) AddTextLine(text string, fontSize float64, font *truetype.Font, color Color, padding Padding) {
textline := new(textLine)
textline.font = font
textline.fontsize = fontSize
textline.text = text
textline.color = color
textline.padding = padding
this.lines = append(this.lines, textline)
}
func (this *TextPicture) AddPictureLine(reader io.Reader, padding Padding) {
picline := new(pictureLine)
picline.reader = reader
picline.padding = padding
this.lines = append(this.lines, picline)
}
func (this *TextPicture) Draw(writer io.Writer, filetype int) error {
var err error
// Initialize the context.
height := 0
width := this.conf.Width
rgba := image.NewRGBA(image.Rect(0, 0, width, height))
for _, v := range this.lines {
height += v.getHeight(width, rgba)
}
rgba = image.NewRGBA(image.Rect(0, 0, width, height))
draw.Draw(rgba, rgba.Bounds(), this.conf.BgColor, image.ZP, draw.Src)
pt := fixed.Point26_6{X: fixed.Int26_6(0), Y: fixed.Int26_6(0)}
for _, v := range this.lines {
if e := v.draw(this.conf.Width, &pt, rgba); e != nil {
fmt.Println("draw error :", e)
}
}
if filetype == TypePng {
err = png.Encode(writer, rgba)
if err != nil {
return err
}
} else {
err = jpeg.Encode(writer, rgba, nil)
if err != nil {
return err
}
}
return nil
}