/
fillers.go
115 lines (101 loc) · 2.5 KB
/
fillers.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
package receipt
import (
"image"
)
type (
PaddingStruct interface {
getPaddingFnc() func(DrawStruct) DrawStruct
drawContent() DrawStruct
}
fixedFiller struct {
x int
y int
}
padding struct {
paddingRight int
paddingTop int
paddingLeft int
paddingBottom int
content DrawStruct
}
empty struct{}
)
func Fixed(x, y Measure) DrawStruct {
return fixedFiller{x: x.toPixel(), y: y.toPixel()}
}
func (f fixedFiller) WriteTo(canvas Canvas, rect image.Rectangle) image.Point {
return image.Point{
X: rect.Min.X + f.x,
Y: rect.Min.Y + f.y,
}
}
func FixedY(y Measure) DrawStruct {
return Fixed(pixels(0), y)
}
// PaddingLeftRight adds padding on all sides to the drawing object
func Padding4(pad Measure, d DrawStruct) DrawStruct {
return padding{
paddingRight: pad.toPixel(),
paddingTop: pad.toPixel(),
paddingLeft: pad.toPixel(),
paddingBottom: pad.toPixel(),
content: d,
}
}
// PaddingLeftRight allows to adjust the padding on each side of the object border
func Padding(l, t, r, b Measure, d DrawStruct) DrawStruct {
return padding{
paddingRight: r.toPixel(),
paddingTop: t.toPixel(),
paddingLeft: l.toPixel(),
paddingBottom: b.toPixel(),
content: d,
}
}
// PaddingLeftRight adds left and right padding to the drawing object
func PaddingLeftRight(pad Measure, d DrawStruct) DrawStruct {
return padding{
paddingRight: pad.toPixel(),
paddingTop: 0,
paddingLeft: pad.toPixel(),
paddingBottom: 0,
content: d,
}
}
// PaddingLeftRight adds left and right padding to the drawing object
func PaddingTopBottom(pad Measure, d DrawStruct) DrawStruct {
return padding{
paddingRight: 0,
paddingTop: pad.toPixel(),
paddingLeft: 0,
paddingBottom: pad.toPixel(),
content: d,
}
}
func (p padding) getPaddingFnc() func(DrawStruct) DrawStruct {
return func(content DrawStruct) DrawStruct {
return Padding(
Pixels(p.paddingLeft),
Pixels(p.paddingTop),
Pixels(p.paddingRight),
Pixels(p.paddingBottom),
content,
)
}
}
func (p padding) drawContent() DrawStruct {
return p.content
}
func (p padding) WriteTo(canvas Canvas, rect image.Rectangle) image.Point {
result := p.content.WriteTo(canvas, padRect4(rect, p.paddingLeft, p.paddingTop, p.paddingRight, p.paddingBottom))
return image.Point{X: rect.Min.X, Y: result.Y + p.paddingBottom}
}
func Empty() DrawStruct {
return empty{}
}
func (e empty) WriteTo(canvas Canvas, rect image.Rectangle) image.Point {
return image.Point{
X: rect.Min.X,
Y: rect.Min.Y,
}
}