forked from Techbinator/go-table-image
/
tableimage.go
76 lines (63 loc) · 1.33 KB
/
tableimage.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
package tableimage
import (
"image"
)
//FileType the image format png or jpg
type FileType string
//TD a table data container
type TD struct {
Text string
Color string
}
//TR the table row
type TR struct {
BorderColor string
Tds []TD
}
type tableImage struct {
width int
height int
th TR
trs []TR
backgroundColor string
fileType FileType
filePath string
img *image.RGBA
}
const (
rowSpace = 26
tablePadding = 20
letterPerPx = 10
separatorPadding = 10
wrapWordsLen = 20
columnSpace = wrapWordsLen * letterPerPx
PNG FileType = "png"
JPEG FileType = "jpg"
)
//Init initialise the table image receiver
func Init(backgroundColor string, fileType FileType, filePath string) tableImage {
ti := tableImage{
backgroundColor: backgroundColor,
fileType: fileType,
filePath: filePath,
}
ti.setRgba()
return ti
}
//AddTH adds the table header
func (ti *tableImage) AddTH(th TR) {
ti.th = th
}
//AddTRs add the table rows
func (ti *tableImage) AddTRs(trs []TR) {
ti.trs = trs
}
//Save saves the table
func (ti *tableImage) Save() {
ti.calculateHeight()
ti.calculateWidth()
ti.setRgba()
ti.drawTH()
ti.drawTR()
ti.saveFile()
}