forked from signintech/pdft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent_text.go
109 lines (96 loc) · 1.71 KB
/
content_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
package pdft
import (
"bytes"
gopdf "github.com/juaismar/pdft/minigopdf"
)
//ContentText text in pdf
type ContentText struct {
text string
fontName string
fontStyle int
fontSize int
pageNum int
x float64
y float64
pdfFontData *PDFFontData
w float64
h float64
align int
lineWidth float64
pdfProtection *gopdf.PDFProtection
}
func (c *ContentText) setProtection(p *gopdf.PDFProtection) {
c.pdfProtection = p
}
func (c *ContentText) protection() *gopdf.PDFProtection {
return c.pdfProtection
}
func (c *ContentText) toSteram() (*bytes.Buffer, error) {
var border = 0
if c.lineWidth > 0 {
border = Left | Right | Top | Bottom
}
var rgb gopdf.Rgb
rgb.SetR(1)
rgb.SetG(1)
rgb.SetB(1)
var cc gopdf.CacheContent
cc.Setup(
&gopdf.Rect{
W: c.w,
H: c.h,
},
rgb,
1.0,
c.pdfFontData.fontIndex(),
c.fontSize,
c.fontStyle,
0,
c.x,
c.y,
&c.pdfFontData.font,
pageHeight(),
gopdf.ContentTypeText,
gopdf.CellOption{
Align: c.align,
Border: border,
},
c.lineWidth,
)
cc.WriteTextToContent(c.text)
buff, err := cc.ToStream(c.protection())
if err != nil {
return nil, err
}
buff.Write([]byte("\r\n"))
return buff, nil
}
func (c *ContentText) page() int {
return c.pageNum
}
func (c *ContentText) measureTextWidth() (float64, error) {
var cc gopdf.CacheContent
cc.Setup(
&gopdf.Rect{
W: c.w,
H: c.h,
},
gopdf.Rgb{},
1.0,
c.pdfFontData.fontIndex(),
c.fontSize,
c.fontStyle,
0,
0,
0,
&c.pdfFontData.font,
pageHeight(),
gopdf.ContentTypeText,
gopdf.CellOption{
Align: c.align,
Border: 0,
},
c.lineWidth,
)
return cc.MeasureTextWidth(c.text)
}