-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
142 lines (125 loc) · 2.79 KB
/
table.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
package pdfrenderer
import (
"github.com/signintech/gopdf"
)
type Table struct {
size Size
position Vec2
rows [][]Text
}
func (t *Table) Position(position Vec2) {
t.position = position
}
func (t *Table) GetPosition() Vec2 {
return t.position
}
func (t *Table) GetSize() Size {
return t.size
}
func (t *Table) Rows(rows [][]string) *Table {
for _, r := range rows {
t.AddRow(r)
}
return t
}
func (t *Table) AddRow(row []string) *Table {
r := make([]Text, len(row))
for idx, h := range row {
r[idx] = Text{
text: h,
fontSize: 12,
color: BLACK,
borderColor: &BLACK,
paddingLeft: 2.5,
}
}
t.rows = append(t.rows, r)
return t
}
func (t *Table) Measure(pdf *gopdf.GoPdf) Size {
rowsCount := len(t.rows)
if rowsCount == 0 {
return Size{}
}
header := t.rows[0]
cellWidth := t.size.Width / float64(len(header))
for _, row := range t.rows {
cellHeight := 0.0
for idx := range row {
cell := &row[idx]
size := cell.Measure(pdf)
if size.Height > cellHeight {
cellHeight = size.Height
}
cell.size.Width = cellWidth
}
cellHeight += 5
t.size.Height += cellHeight
for idx := range row {
cell := &row[idx]
cell.size.Height = cellHeight
cell.position.X = t.position.X + (cellWidth * float64(idx))
cell.position.Y = t.position.Y + t.size.Height
}
}
return t.size
}
func (t *Table) Draw(pdf *gopdf.GoPdf) {
pdf.SetFillColor(BLUE.R, BLUE.G, BLUE.B)
pdf.SetFontSize(12)
for _, row := range t.rows {
for _, cell := range row {
cell.Draw(pdf)
}
}
// cellWidth := t.size.Width / float64(len(header))
// for idx, header := range header {
// // width, err := pdf.MeasureTextWidth(header)
// height, err := pdf.MeasureCellHeightByText(header)
// if err != nil {
// panic(err)
// }
// x := t.position.X + (cellWidth * float64(idx))
// y := t.position.Y + height - 2.5
// pdf.SetXY(x+2.5, y)
// pdf.SetFillColor(BLACK.R, BLACK.G, BLACK.B)
// pdf.Rectangle(
// x,
// y-height,
// x+cellWidth,
// y+2.5,
// "D", 0, 0)
// err = pdf.Text(header)
// if err != nil {
// panic(err)
// }
// }
// for _, row := range t.rows {
// for idx, cell := range row {
// // width, err := pdf.MeasureTextWidth(header)
// height, err := pdf.MeasureCellHeightByText(cell)
// if err != nil {
// panic(err)
// }
// x := t.position.X + (cellWidth * float64(idx))
// y := t.position.Y + height - 2.5
// pdf.SetXY(x+2.5, y)
// pdf.SetFillColor(BLACK.R, BLACK.G, BLACK.B)
// pdf.Rectangle(
// x,
// y-height,
// x+cellWidth,
// y+2.5,
// "D", 0, 0)
// err = pdf.Text(cell)
// if err != nil {
// panic(err)
// }
// }
// }
// pdf.SetXY(t.position.X, t.position.Y+t.size.Height-2.5)
// err := pdf.Text("Hello World")
// if err != nil {
// panic(err)
// }
}