-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaceholder.go
178 lines (154 loc) · 4.2 KB
/
placeholder.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2017 The apiholdit Authors. All rights reserved.
package apiholdit
import (
"bytes"
"errors"
"image"
"image/color"
"image/draw"
"image/gif"
"image/jpeg"
"image/png"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
)
// PlaceHolder ...
type PlaceHolder struct {
Width int
Height int
Text string
MarginRatio float64
BackgroundColor *color.RGBA
ForegroundColor *color.RGBA
Canvas *image.RGBA
}
// NewPlaceHolder ...
func NewPlaceHolder() *PlaceHolder {
placeholder := PlaceHolder{
Width: DefaultWidth,
Height: DefaultHeight,
Text: DefaultText,
MarginRatio: DefaultMarginRatio,
BackgroundColor: DefaultBackgroundColor,
ForegroundColor: DefaultForegroundColor,
}
return &placeholder
}
// SetWidth sets the width of the placeholder and regenerates the canvas with
// the new size.
func (p *PlaceHolder) SetWidth(width int) error {
if width < 0 {
return errors.New("width must be >= 0")
}
p.Width = width
rectangle := image.Rect(0, 0, p.Width, p.Height)
p.Canvas = image.NewRGBA(rectangle)
return nil
}
// SetHeight sets the height of the placeholder and regenerates the canvas with
// the new size.
func (p *PlaceHolder) SetHeight(height int) error {
if height < 0 {
return errors.New("height must be >= 0")
}
p.Height = height
rectangle := image.Rect(0, 0, p.Width, p.Height)
p.Canvas = image.NewRGBA(rectangle)
return nil
}
// SetText ...
func (p *PlaceHolder) SetText(text string) error {
p.Text = text
return nil
}
// SetBackgroundColor ...
func (p *PlaceHolder) SetBackgroundColor(bgcolor string) error {
col, err := getColor(bgcolor)
if err != nil {
return err
}
p.BackgroundColor = col
return nil
}
// SetForegroundColor ...
func (p *PlaceHolder) SetForegroundColor(fgcolor string) error {
col, err := getColor(fgcolor)
if err != nil {
return err
}
p.ForegroundColor = col
return nil
}
// Render ...
func (p *PlaceHolder) Render() error {
rectangle := image.Rect(0, 0, p.Width, p.Height)
p.Canvas = image.NewRGBA(rectangle)
// Render background
err := renderBackground(p.Canvas, p.BackgroundColor)
if err != nil {
return err
}
// Get font to be used
fontTTF, err := getFont(DefaultFontPath)
if err != nil {
return err
}
// Render text
err = renderText(p.Canvas, fontTTF, p.Width, p.Height, p.MarginRatio, p.Text, p.ForegroundColor)
return err
}
// EncodePNG ...
func (p *PlaceHolder) EncodePNG() (*bytes.Buffer, error) {
buffer := new(bytes.Buffer)
err := png.Encode(buffer, p.Canvas)
if err != nil {
return buffer, nil
}
return buffer, err
}
// EncodeGIF ...
func (p *PlaceHolder) EncodeGIF(options *gif.Options) (*bytes.Buffer, error) {
buffer := new(bytes.Buffer)
err := gif.Encode(buffer, p.Canvas, options)
if err != nil {
return buffer, nil
}
return buffer, err
}
// EncodeJPEG ...
func (p *PlaceHolder) EncodeJPEG(options *jpeg.Options) (*bytes.Buffer, error) {
buffer := new(bytes.Buffer)
err := jpeg.Encode(buffer, p.Canvas, options)
if err != nil {
return buffer, nil
}
return buffer, err
}
// renderBackground ...
func renderBackground(canvas draw.Image, bgcolor color.Color) error {
rectangle := canvas.Bounds()
color := &image.Uniform{bgcolor}
draw.Draw(canvas, rectangle, color, image.ZP, draw.Src)
return nil
}
// renderText ...
func renderText(canvas draw.Image, fontTTF *truetype.Font, width int, height int, marginratio float64, text string, fgcolor color.Color) error {
rectangle := canvas.Bounds()
context := freetype.NewContext()
context.SetDPI(DefaultDPI)
context.SetFont(fontTTF)
context.SetSrc(image.NewUniform(color.RGBA{0, 0, 0, 0}))
context.SetDst(canvas)
context.SetClip(rectangle)
context.SetHinting(font.HintingNone)
// calculate final font size and coordinates to draw
finalFontSize, finalWidth, finalHeight := getFontFinalSize(text, context, width, height, marginratio)
context.SetFontSize(finalFontSize)
// draw the text
context.SetSrc(image.NewUniform(fgcolor))
xCenter := (float64(width) / 2.0) - (float64(finalWidth) / 2.0)
yCenter := (float64(height) / 2.0) + (float64(finalHeight) / 2.0)
_, err := context.DrawString(text, freetype.Pt(int(xCenter), int(yCenter)))
return err
}