-
-
Notifications
You must be signed in to change notification settings - Fork 172
/
math.go
95 lines (76 loc) · 3.09 KB
/
math.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
package internal
import (
"github.com/johnfercher/maroto/pkg/props"
"github.com/jung-kurt/gofpdf"
)
// Math is the abstraction which deals with useful calc
type Math interface {
GetRectCenterColProperties(imageWidth float64, imageHeight float64, colWidth float64, colHeight float64, xColOffset float64, percent float64) (x float64, y float64, w float64, h float64)
GetRectNonCenterColProperties(imageWidth float64, imageHeight float64, colWidth float64, colHeight float64, xColOffset float64, prop props.Rect) (x float64, y float64, w float64, h float64)
GetCenterCorrection(outerSize, innerSize float64) float64
}
type math struct {
pdf gofpdf.Pdf
}
// NewMath create a Math
func NewMath(pdf gofpdf.Pdf) *math {
return &math{
pdf,
}
}
// GetRectCenterColProperties define Width, Height, X Offset and Y Offset
// to and rectangle (QrCode, Barcode, Image) be centralized inside a cell
func (s *math) GetRectCenterColProperties(imageWidth float64, imageHeight float64, colWidth float64, colHeight float64, xColOffset float64, percent float64) (x float64, y float64, w float64, h float64) {
percent = percent / 100.0
left, top, _, _ := s.pdf.GetMargins()
imageProportion := imageHeight / imageWidth
celProportion := colHeight / colWidth
if imageProportion > celProportion {
newImageWidth := colHeight / imageProportion * percent
newImageHeight := newImageWidth * imageProportion
widthCorrection := s.GetCenterCorrection(colWidth, newImageWidth)
heightCorrection := s.GetCenterCorrection(colHeight, newImageHeight)
x = xColOffset + left + widthCorrection
y = top + heightCorrection
w = newImageWidth
h = newImageHeight
} else {
newImageWidth := colWidth * percent
newImageHeight := newImageWidth * imageProportion
widthCorrection := s.GetCenterCorrection(colWidth, newImageWidth)
heightCorrection := s.GetCenterCorrection(colHeight, newImageHeight)
x = xColOffset + left + widthCorrection
y = top + heightCorrection
w = newImageWidth
h = newImageHeight
}
return
}
// GetRectNonCenterColProperties define Width, Height to and rectangle (QrCode, Barcode, Image) inside a cell
func (s *math) GetRectNonCenterColProperties(imageWidth float64, imageHeight float64, colWidth float64, colHeight float64, xColOffset float64, prop props.Rect) (x float64, y float64, w float64, h float64) {
percent := prop.Percent / 100.0
left, top, _, _ := s.pdf.GetMargins()
imageProportion := imageHeight / imageWidth
celProportion := colHeight / colWidth
if imageProportion > celProportion {
newImageWidth := colHeight / imageProportion * percent
newImageHeight := newImageWidth * imageProportion
x = xColOffset + left + prop.Left
y = top
w = newImageWidth
h = newImageHeight
} else {
newImageWidth := colWidth * percent
newImageHeight := newImageWidth * imageProportion
x = xColOffset + left + prop.Left
y = top
w = newImageWidth
h = newImageHeight
}
return
}
// GetCenterCorrection return the correction of space in X or Y to
// centralize a line in relation with another line
func (s *math) GetCenterCorrection(outerSize, innerSize float64) float64 {
return (outerSize - innerSize) / 2.0
}