-
-
Notifications
You must be signed in to change notification settings - Fork 172
/
line.go
64 lines (54 loc) · 1.53 KB
/
line.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
package internal
import (
"github.com/johnfercher/maroto/internal/fpdf"
"github.com/johnfercher/maroto/pkg/color"
"github.com/johnfercher/maroto/pkg/consts"
"github.com/johnfercher/maroto/pkg/props"
)
// Line is the abstraction which deals with lines.
type Line interface {
Draw(cell Cell, lineProp props.Line)
}
type line struct {
pdf fpdf.Fpdf
defaultLineColor color.Color
}
// NewLine create a Line Helper.
func NewLine(pdf fpdf.Fpdf) *line {
return &line{
pdf: pdf,
defaultLineColor: color.NewBlack(),
}
}
func (s *line) Draw(cell Cell, lineProp props.Line) {
s.pdf.SetDrawColor(lineProp.Color.Red, lineProp.Color.Green, lineProp.Color.Blue)
s.pdf.SetLineWidth(lineProp.Width)
s.drawStylizedLine(cell, lineProp)
s.pdf.SetDrawColor(s.defaultLineColor.Red, s.defaultLineColor.Green, s.defaultLineColor.Blue)
s.pdf.SetLineWidth(consts.DefaultLineWidth)
}
func (s *line) drawStylizedLine(cell Cell, prop props.Line) {
if prop.Style == consts.Solid {
s.pdf.Line(cell.X, cell.Y, cell.Width, cell.Height)
return
}
if prop.Style == consts.Dashed {
s.drawDashedLine(cell)
return
}
s.drawDottedLine(cell, prop.Width)
}
func (s *line) drawDashedLine(cell Cell) {
xStep := 5.0
halfDivisor := 2.0
xHalfStep := xStep / halfDivisor
for x := cell.X; x < cell.Width; x += xStep {
s.pdf.Line(x, cell.Y, x+xHalfStep, cell.Height)
}
}
func (s *line) drawDottedLine(cell Cell, width float64) {
xStep := 3.0
for x := cell.X; x < cell.Width; x += xStep {
s.pdf.Line(x, cell.Y, x+width, cell.Height)
}
}