forked from tiechui1994/gopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
span.go
330 lines (267 loc) · 7.2 KB
/
span.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package gopdf
import (
"math"
"strings"
"github.com/tiechui1994/gopdf/core"
"github.com/tiechui1994/gopdf/util"
)
// 不会进行自动分页, 可以用于页眉, 页脚的内容.
type Span struct {
pdf *core.Report
font core.Font
width, height float64
lineHeight float64
lineSpace float64
fontColor string
margin core.Scope
border core.Scope
contents []string
horizontalCentered bool
verticalCentered bool
rightAlign bool
}
func NewSpan(lineHeight, lineSpce float64, pdf *core.Report) *Span {
x, _ := pdf.GetXY()
endX, _ := pdf.GetPageEndXY()
if endX-x <= 0 {
panic("please modify current X")
}
f := &Span{
pdf: pdf,
width: endX - x,
height: lineHeight,
lineHeight: lineHeight,
lineSpace: lineSpce,
}
return f
}
func NewSpanWithWidth(width float64, lineHeight, lineSpce float64, pdf *core.Report) *Span {
currX, _ := pdf.GetXY()
endX, _ := pdf.GetPageEndXY()
if endX-currX <= 0 {
panic("please modify current X")
}
if endX-currX <= width {
width = endX - currX
}
f := &Span{
pdf: pdf,
width: width,
height: lineHeight,
lineHeight: lineHeight,
lineSpace: lineSpce,
}
return f
}
func (span *Span) Copy(content string) *Span {
f := &Span{
pdf: span.pdf,
width: span.width,
lineHeight: span.lineHeight,
lineSpace: span.lineSpace,
fontColor: span.fontColor,
}
f.SetBorder(span.border)
f.SetFont(span.font)
f.SetContent(content)
return f
}
func (span *Span) SetHeight(height float64) *Span {
span.height = height
return span
}
func (span *Span) SetMarign(margin core.Scope) *Span {
margin.ReplaceMarign()
config := span.pdf.GetConfig()
_, height := config.GetWidthAndHeight()
x1, _ := config.GetStart()
x2, _ := config.GetEnd()
x, y := span.pdf.GetXY()
// X
if x+margin.Left > x2 || x+margin.Left < x1 {
return span
}
// Y
if y+margin.Top > height || y+margin.Top < 0 {
return span
}
// width
if x+margin.Left+span.border.Left+span.width+span.border.Right > x2 {
width := x2 - (x + margin.Left + span.border.Left + span.border.Right)
if width <= 0 {
return span
}
span.width = width
}
span.margin = margin
return span
}
func (span *Span) SetBorder(border core.Scope) *Span {
border.ReplaceBorder()
config := span.pdf.GetConfig()
x2, _ := config.GetEnd()
x, y := span.pdf.GetXY()
_, height := config.GetWidthAndHeight()
if y+span.margin.Top+border.Top+border.Bottom > height {
return span
}
// width
if x+span.margin.Left+border.Left+span.width+border.Right > x2 {
width := x2 - (x + span.margin.Left + border.Left + border.Right)
if width <= 0 {
return span
}
span.width = width
}
span.border = border
return span
}
func (span *Span) GetHeight() (height float64) {
return span.height
}
func (span *Span) GetWidth() (width float64) {
return span.width
}
func (span *Span) HorizontalCentered() *Span {
span.horizontalCentered = true
span.rightAlign = false
return span
}
func (span *Span) VerticalCentered() *Span {
span.verticalCentered = true
return span
}
func (span *Span) RightAlign() *Span {
span.rightAlign = true
span.horizontalCentered = false
return span
}
func (span *Span) SetFontColor(color string) *Span {
util.CheckColor(color)
span.fontColor = color
return span
}
func (span *Span) SetFont(font core.Font) *Span {
span.font = font
// 注册, 启动
span.pdf.Font(font.Family, font.Size, font.Style)
span.pdf.SetFontWithStyle(font.Family, font.Style, font.Size)
return span
}
func (span *Span) SetFontWithColor(font core.Font, color string) *Span {
span.SetFont(font)
span.SetFontColor(color)
return span
}
func (span *Span) SetContent(content string) *Span {
convertStr := strings.Replace(content, "\t", " ", -1)
var (
blocks = strings.Split(convertStr, "\n") // 分行
contentWidth = span.width
)
// 必须检查字体
if util.IsEmpty(span.font) {
panic("there no avliable font")
}
// 必须先进行注册, 才能设置
span.pdf.Font(span.font.Family, span.font.Size, span.font.Style)
span.pdf.SetFontWithStyle(span.font.Family, span.font.Style, span.font.Size)
if len(blocks) == 1 {
if span.pdf.MeasureTextWidth(convertStr) < contentWidth {
span.contents = []string{convertStr}
height := span.border.Top + span.border.Bottom + span.lineHeight
span.height = math.Max(height, span.height)
return span
}
}
for i := range blocks {
// 单独的一行
if span.pdf.MeasureTextWidth(convertStr) < contentWidth {
span.contents = append(span.contents, blocks[i])
continue
}
var (
line []rune
)
// 单独的一行需要拆分
for _, r := range []rune(blocks[i]) {
line = append(line, r)
lineLength := span.pdf.MeasureTextWidth(string(line))
if lineLength >= contentWidth {
if lineLength-contentWidth > 2 {
span.contents = append(span.contents, string(line[0:len(line)-1]))
line = line[len(line)-1:]
} else {
span.contents = append(span.contents, string(line))
line = []rune{}
}
}
}
// 剩余单独成行
if len(line) > 0 {
span.contents = append(span.contents, string(line))
}
}
// 重新计算 span 的高度
length := float64(len(span.contents))
height := span.border.Top + span.border.Bottom + span.lineHeight*length + span.lineSpace*(length-1)
span.height = math.Max(height, span.height)
return span
}
func (span *Span) GenerateAtomicCell() error {
var (
sx, sy = span.pdf.GetXY()
x, y float64
border core.Scope
)
if util.IsEmpty(span.font) {
panic("no font")
}
span.pdf.Font(span.font.Family, span.font.Size, span.font.Style)
span.pdf.SetFontWithStyle(span.font.Family, span.font.Style, span.font.Size)
// 垂直居中
if span.verticalCentered {
length := float64(len(span.contents))
height := (length-1)*span.lineSpace + length*span.lineHeight + span.border.Top + span.border.Bottom
if height < span.height {
top := (span.height - height) / 2
span.border = core.NewScope(border.Left, top, border.Right, 0)
}
}
border = span.border
if !util.IsEmpty(span.fontColor) {
span.pdf.TextColor(util.GetColorRGB(span.fontColor))
}
for i := 0; i < len(span.contents); i++ {
// 水平居中, 只是对当前的行设置新的 Border
if span.horizontalCentered {
width := span.pdf.MeasureTextWidth(span.contents[i])
if width < span.width {
left := (border.Left + border.Right + span.width - width) / 2
span.border = core.NewScope(left, border.Top, 0, left)
}
}
// 水平居右, 只是对当前的行设置新的 Border
if span.rightAlign {
width := span.pdf.MeasureTextWidth(span.contents[i])
if width < span.width {
left := span.width - width + span.border.Right + span.border.Left
span.border = core.NewScope(left, border.Top, 0, 0)
}
}
x, y = span.getContentPosition(sx, sy, i)
span.pdf.Cell(x, y, span.contents[i])
}
if !util.IsEmpty(span.fontColor) {
span.pdf.TextDefaultColor()
}
x, _ = span.pdf.GetPageStartXY()
span.pdf.SetXY(x, y+span.lineHeight+span.margin.Bottom) // 定格最终的位置
return nil
}
func (span *Span) getContentPosition(sx, sy float64, index int) (x, y float64) {
x = sx + span.margin.Left + span.border.Left
y = sy + span.margin.Top + span.border.Top
y += float64(index) * (span.lineHeight + span.lineSpace)
return x, y
}