/
tablelist.go
executable file
·144 lines (114 loc) · 3.52 KB
/
tablelist.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
package internal
import (
"github.com/rongfengliang/maroto/pkg/color"
"github.com/rongfengliang/maroto/pkg/consts"
"github.com/rongfengliang/maroto/pkg/props"
)
// MarotoGridPart is the abstraction to deal with the gris system inside the table list
type MarotoGridPart interface {
// Grid System
Row(height float64, closure func())
Col(width uint, closure func())
ColSpace(width uint)
// Helpers
SetBackgroundColor(color color.Color)
GetCurrentOffset() float64
GetPageSize() (width float64, height float64)
GetPageMargins() (left float64, top float64, right float64, bottom float64)
// Outside Col/Row Components
Line(spaceHeight float64)
// Inside Col/Row Components
Text(text string, prop ...props.Text)
}
// TableList is the abstraction to create a table with header and contents
type TableList interface {
Create(header []string, contents [][]string, prop ...props.TableList)
BindGrid(part MarotoGridPart)
}
type tableList struct {
pdf MarotoGridPart
text Text
font Font
}
// NewTableList create a TableList
func NewTableList(text Text, font Font) *tableList {
return &tableList{
text: text,
font: font,
}
}
// BindGrid bind the grid system to TableList
func (s *tableList) BindGrid(pdf MarotoGridPart) {
s.pdf = pdf
}
// Create create a header section with a list of strings and
// create many rows with contents
func (s *tableList) Create(header []string, contents [][]string, prop ...props.TableList) {
if len(header) == 0 {
return
}
if len(contents) == 0 {
return
}
tableProp := props.TableList{}
if len(prop) > 0 {
tableProp = prop[0]
}
tableProp.MakeValid(header, contents)
headerHeight := s.calcLinesHeight(header, tableProp.HeaderProp, tableProp.Align)
// Draw header
s.pdf.Row(headerHeight+1, func() {
for i, h := range header {
hs := h
s.pdf.Col(tableProp.HeaderProp.GridSizes[i], func() {
reason := hs
s.pdf.Text(reason, tableProp.HeaderProp.ToTextProp(tableProp.Align, 0, false, 0.0))
})
}
})
// Define space between header and contents
s.pdf.Row(tableProp.HeaderContentSpace, func() {
s.pdf.ColSpace(0)
})
// Draw contents
for index, content := range contents {
contentHeight := s.calcLinesHeight(content, tableProp.ContentProp, tableProp.Align)
if tableProp.AlternatedBackground != nil && index%2 == 0 {
s.pdf.SetBackgroundColor(*tableProp.AlternatedBackground)
}
s.pdf.Row(contentHeight+1, func() {
for i, c := range content {
cs := c
s.pdf.Col(tableProp.ContentProp.GridSizes[i], func() {
s.pdf.Text(cs, tableProp.ContentProp.ToTextProp(tableProp.Align, 0, false, 0.0))
})
}
})
if tableProp.AlternatedBackground != nil && index%2 == 0 {
s.pdf.SetBackgroundColor(color.NewWhite())
}
if tableProp.Line {
s.pdf.Line(1.0)
}
}
}
func (s *tableList) calcLinesHeight(textList []string, contentProp props.TableListContent, align consts.Align) float64 {
maxLines := 1.0
left, _, right, _ := s.pdf.GetPageMargins()
width, _ := s.pdf.GetPageSize()
usefulWidth := float64(width - left - right)
textProp := contentProp.ToTextProp(align, 0, false, 0.0)
for i, text := range textList {
gridSize := float64(contentProp.GridSizes[i])
percentSize := gridSize / consts.MaxGridSum
colWidth := usefulWidth * percentSize
qtdLines := float64(s.text.GetLinesQuantity(text, textProp, colWidth))
if qtdLines > maxLines {
maxLines = qtdLines
}
}
_, _, fontSize := s.font.GetFont()
// Font size corrected by the scale factor from "mm" inside gofpdf f.k
fontHeight := fontSize / s.font.GetScaleFactor()
return fontHeight * maxLines
}