-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.go
226 lines (194 loc) · 5.73 KB
/
text.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package pdflexgo
import (
"github.com/jung-kurt/gofpdf"
"github.com/kjk/flex"
)
var defaultBackgroundColor = rgba{0, 0, 0, 0}
type TextElement struct {
abstractElement
lineHeight float64
lineHeightAssigned bool
firstRequestMade bool
content string
size float64
fontStyle FontStyle
fontFamily string
color rgba
}
func Text() *TextElement {
text := &TextElement{
size: -1,
}
text.abstractElement.initialize()
text.color = DefaultFontColor
return text
}
func (text *TextElement) Content(content string) *TextElement {
text.content = content
return text
}
func (text *TextElement) Size(size float64) *TextElement {
text.size = size
return text
}
func (text *TextElement) Color(red int, green int, blue int, alpha ...float64) *TextElement {
text.color = getRgba(red, green, blue, alpha...)
return text
}
func (text *TextElement) Thin() *TextElement {
text.fontStyle = FontStyleThin
return text
}
func (text *TextElement) ExtraLight() *TextElement {
text.fontStyle = FontStyleExtraLight
return text
}
func (text *TextElement) Light() *TextElement {
text.fontStyle = FontStyleLight
return text
}
func (text *TextElement) Regular() *TextElement {
text.fontStyle = FontStyleRegular
return text
}
func (text *TextElement) Medium() *TextElement {
text.fontStyle = FontStyleMedium
return text
}
func (text *TextElement) SemiBold() *TextElement {
text.fontStyle = FontStyleSemiBold
return text
}
func (text *TextElement) Bold() *TextElement {
text.fontStyle = FontStyleBold
return text
}
func (text *TextElement) ExtraBold() *TextElement {
text.fontStyle = FontStyleExtraBold
return text
}
func (text *TextElement) Black() *TextElement {
text.fontStyle = FontStyleBlack
return text
}
func (text *TextElement) ThinItalic() *TextElement {
text.fontStyle = FontStyleThinItalic
return text
}
func (text *TextElement) ExtraLightItalic() *TextElement {
text.fontStyle = FontStyleExtraLightItalic
return text
}
func (text *TextElement) LightItalic() *TextElement {
text.fontStyle = FontStyleLightItalic
return text
}
func (text *TextElement) RegularItalic() *TextElement {
text.fontStyle = FontStyleRegularItalic
return text
}
func (text *TextElement) MediumItalic() *TextElement {
text.fontStyle = FontStyleMediumItalic
return text
}
func (text *TextElement) SemiBoldItalic() *TextElement {
text.fontStyle = FontStyleSemiBoldItalic
return text
}
func (text *TextElement) BoldItalic() *TextElement {
text.fontStyle = FontStyleBoldItalic
return text
}
func (text *TextElement) ExtraBoldItalic() *TextElement {
text.fontStyle = FontStyleExtraBoldItalic
return text
}
func (text *TextElement) BlackItalic() *TextElement {
text.fontStyle = FontStyleBlackItalic
return text
}
func (text *TextElement) LineHeight(height float64) *TextElement {
text.lineHeight = height
text.lineHeightAssigned = true
return text
}
func (text *TextElement) FontFamily(family string) *TextElement {
text.fontFamily = family
return text
}
func (elem *TextElement) markRequiredAsDirty() {
elem.flexNode.MarkDirty()
}
func (text *TextElement) preRender(defaultProps *defaultProps, fpdf *gofpdf.Fpdf) {
if text.fontFamily == "" {
text.fontFamily = defaultProps.fontFamily
}
if text.fontStyle == "" {
text.fontStyle = defaultProps.fontStyle
}
if text.size == -1 {
text.size = defaultProps.fontSize
}
if equalColor(text.color, DefaultFontColor) {
text.color = defaultProps.fontColor
}
var measureFunc = func(node *flex.Node, width float32, widthMode flex.MeasureMode, height float32, heightMode flex.MeasureMode) flex.Size {
setFont(fpdf, text.fontFamily, text.fontStyle, text.size)
_, fontSize := fpdf.GetFontSize()
if !text.lineHeightAssigned {
text.lineHeight = fontSize
}
if !text.firstRequestMade {
width = float32(fpdf.GetStringWidth(text.content))
height = float32(text.lineHeight)
text.firstRequestMade = true
} else {
fpdf.SetXY(0, 0)
pageWidth, _ := fpdf.GetPageSize()
marginRight := pageWidth - (float64(text.flexNode.LayoutGetWidth() +
text.flexNode.LayoutGetPadding(flex.EdgeLeft) +
text.flexNode.LayoutGetPadding(flex.EdgeRight) +
text.flexNode.LayoutGetBorder(flex.EdgeLeft) +
text.flexNode.LayoutGetBorder(flex.EdgeRight)))
if marginRight < 0 {
marginRight = 0
}
fpdf.SetMargins(0, 0, marginRight)
fpdf.SetCellMargin(0)
fpdf.Write(text.lineHeight, text.content)
height = float32(fpdf.GetY() + text.lineHeight)
width = text.flexNode.LayoutGetWidth() - (text.flexNode.LayoutGetPadding(flex.EdgeLeft) +
text.flexNode.LayoutGetPadding(flex.EdgeRight) +
text.flexNode.LayoutGetBorder(flex.EdgeLeft) +
text.flexNode.LayoutGetBorder(flex.EdgeRight))
}
return flex.Size{Width: width, Height: float32(height)}
}
text.getFlexNode().SetMeasureFunc(measureFunc)
}
func (text *TextElement) render(pdf *Pdf) {
text.renderElement(pdf)
fpdf := pdf.fpdf
setFont(fpdf, text.fontFamily, text.fontStyle, text.size)
fpdf.SetXY(
float64(text.x()),
float64(text.y()))
pageWidth, _ := fpdf.GetPageSize()
marginRight := pageWidth - float64(text.x()+text.flexNode.LayoutGetWidth()+text.flexNode.LayoutGetPadding(flex.EdgeLeft)+text.flexNode.LayoutGetPadding(flex.EdgeRight))
if marginRight < 0 {
marginRight = 0
}
fpdf.SetCellMargin(0)
y := float64(text.y() + text.flexNode.LayoutGetPadding(flex.EdgeTop) + text.flexNode.LayoutGetBorder(flex.EdgeTop))
x := float64(text.x() + text.flexNode.LayoutGetPadding(flex.EdgeLeft) + text.flexNode.LayoutGetBorder(flex.EdgeLeft))
fpdf.SetXY(x, y)
fpdf.SetMargins(x, y, marginRight)
fpdf.SetTextColor(text.color.red, text.color.green, text.color.blue)
fpdf.SetAlpha(text.color.alpha, "")
lineHeight := text.lineHeight
if !text.lineHeightAssigned {
lineHeight = text.size
}
fpdf.Write(lineHeight, text.content)
fpdf.SetAlpha(1, "")
}