forked from signintech/gopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_obj.go
278 lines (238 loc) · 8.66 KB
/
content_obj.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
package gopdf
import (
"bytes"
"fmt"
"log"
"strconv"
"strings"
)
type ContentObj struct { //impl IObj
buffer bytes.Buffer
stream bytes.Buffer
//text bytes.Buffer
getRoot func() *GoPdf
}
func (c *ContentObj) init(funcGetRoot func() *GoPdf) {
c.getRoot = funcGetRoot
}
func (c *ContentObj) build() error {
streamlen := c.stream.Len()
c.buffer.WriteString("<<\n")
c.buffer.WriteString("/Length " + strconv.Itoa(streamlen) + "\n")
c.buffer.WriteString(">>\n")
c.buffer.WriteString("stream\n")
c.buffer.Write(c.stream.Bytes())
c.buffer.WriteString("endstream\n")
return nil
}
func (c *ContentObj) getType() string {
return "Content"
}
func (c *ContentObj) getObjBuff() *bytes.Buffer {
return &(c.buffer)
}
func (c *ContentObj) AppendStreamSubsetFont(rectangle *Rect, text string) {
r := c.getRoot().Curr.textColor().r
g := c.getRoot().Curr.textColor().g
b := c.getRoot().Curr.textColor().b
grayFill := c.getRoot().Curr.grayFill
sumWidth := uint64(0)
var buff bytes.Buffer
for _, r := range text {
index, err := c.getRoot().Curr.Font_ISubset.CharIndex(r)
if err != nil {
log.Fatalf("err:%s", err.Error())
}
buff.WriteString(fmt.Sprintf("%04X", index))
width, err := c.getRoot().Curr.Font_ISubset.CharWidth(r)
if err != nil {
log.Fatalf("err:%s", err.Error())
}
sumWidth += width
}
fontSize := c.getRoot().Curr.Font_Size
x := fmt.Sprintf("%0.2f", c.getRoot().Curr.X)
y := fmt.Sprintf("%0.2f", c.getRoot().config.PageSize.H-c.getRoot().Curr.Y-(float64(fontSize)*0.7))
c.stream.WriteString("BT\n")
c.stream.WriteString(x + " " + y + " TD\n")
c.stream.WriteString("/F" + strconv.Itoa(c.getRoot().Curr.Font_FontCount+1) + " " + strconv.Itoa(fontSize) + " Tf\n")
if r+g+b != 0 {
rFloat := float64(r) * 0.00392156862745
gFloat := float64(g) * 0.00392156862745
bFloat := float64(b) * 0.00392156862745
rgb := fmt.Sprintf("%0.2f %0.2f %0.2f rg\n", rFloat, gFloat, bFloat)
c.stream.WriteString(rgb)
} else {
c.AppendStreamSetGrayFill(grayFill)
}
c.stream.WriteString("<" + buff.String() + "> Tj\n")
c.stream.WriteString("ET\n")
if rectangle == nil {
fontSize := c.getRoot().Curr.Font_Size
c.getRoot().Curr.X += float64(sumWidth) * (float64(fontSize) / 1000.0)
} else {
c.getRoot().Curr.X += rectangle.W
}
}
func (c *ContentObj) AppendStream(rectangle *Rect, text string) {
fontSize := c.getRoot().Curr.Font_Size
r := c.getRoot().Curr.textColor().r
g := c.getRoot().Curr.textColor().g
b := c.getRoot().Curr.textColor().b
grayFill := c.getRoot().Curr.grayFill
x := fmt.Sprintf("%0.2f", c.getRoot().Curr.X)
y := fmt.Sprintf("%0.2f", c.getRoot().config.PageSize.H-c.getRoot().Curr.Y-(float64(fontSize)*0.7))
c.stream.WriteString("BT\n")
c.stream.WriteString(x + " " + y + " TD\n")
c.stream.WriteString("/F" + strconv.Itoa(c.getRoot().Curr.Font_FontCount+1) + " " + strconv.Itoa(fontSize) + " Tf\n")
if r+g+b != 0 {
rFloat := float64(r) * 0.00392156862745
gFloat := float64(g) * 0.00392156862745
bFloat := float64(b) * 0.00392156862745
rgb := fmt.Sprintf("%0.2f %0.2f %0.2f rg\n", rFloat, gFloat, bFloat)
c.stream.WriteString(rgb)
} else {
c.AppendStreamSetGrayFill(grayFill)
}
c.stream.WriteString("(" + text + ") Tj\n")
c.stream.WriteString("ET\n")
if rectangle == nil {
c.getRoot().Curr.X += StrHelperGetStringWidth(text, fontSize, c.getRoot().Curr.Font_IFont)
} else {
c.getRoot().Curr.X += rectangle.W
}
}
func (c *ContentObj) AppendStreamLine(x1 float64, y1 float64, x2 float64, y2 float64) {
h := c.getRoot().config.PageSize.H
c.stream.WriteString(fmt.Sprintf("%0.2f %0.2f m %0.2f %0.2f l s\n", x1, h-y1, x2, h-y2))
}
//AppendStreamRectangle : draw rectangle from lower-left corner (x, y) with specif width/height
func (c *ContentObj) AppendStreamRectangle(x float64, y float64, wdth float64, hght float64) {
h := c.getRoot().config.PageSize.H
c.stream.WriteString(fmt.Sprintf("%0.2f %0.2f %0.2f %0.2f re b\n", x, h-y, wdth, hght))
}
func (c *ContentObj) AppendStreamOval(x1 float64, y1 float64, x2 float64, y2 float64) {
h := c.getRoot().config.PageSize.H
cp := 0.55228 // Magnification of the control point
v1 := [2]float64{x1 + (x2-x1)/2, h - y2} // Vertex of the lower
v2 := [2]float64{x2, h - (y1 + (y2-y1)/2)} // .. Right
v3 := [2]float64{x1 + (x2-x1)/2, h - y1} // .. Upper
v4 := [2]float64{x1, h - (y1 + (y2-y1)/2)} // .. Left
c.stream.WriteString(fmt.Sprintf("%0.2f %0.2f m\n", v1[0], v1[1]))
c.stream.WriteString(fmt.Sprintf(
"%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c\n",
v1[0]+(x2-x1)/2*cp, v1[1], v2[0], v2[1]-(y2-y1)/2*cp, v2[0], v2[1],
))
c.stream.WriteString(fmt.Sprintf(
"%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c\n",
v2[0], v2[1]+(y2-y1)/2*cp, v3[0]+(x2-x1)/2*cp, v3[1], v3[0], v3[1],
))
c.stream.WriteString(fmt.Sprintf(
"%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c\n",
v3[0]-(x2-x1)/2*cp, v3[1], v4[0], v4[1]+(y2-y1)/2*cp, v4[0], v4[1],
))
c.stream.WriteString(fmt.Sprintf(
"%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c b\n",
v4[0], v4[1]-(y2-y1)/2*cp, v1[0]-(x2-x1)/2*cp, v1[1], v1[0], v1[1],
))
}
//AppendStreamCurve draw curve
// - x0, y0: Start point
// - x1, y1: Control point 1
// - x2, y2: Control point 2
// - x3, y3: End point
// - style: Style of rectangule (draw and/or fill: D, F, DF, FD)
func (c *ContentObj) AppendStreamCurve(x0 float64, y0 float64, x1 float64, y1 float64, x2 float64, y2 float64, x3 float64, y3 float64, style string) {
h := c.getRoot().config.PageSize.H
//cp := 0.55228
c.stream.WriteString(fmt.Sprintf("%0.2f %0.2f m\n", x0, h-y0))
c.stream.WriteString(fmt.Sprintf(
"%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f c",
x1, h-y1, x2, h-y2, x3, h-y3,
))
style = strings.TrimSpace(style)
op := "S"
if style == "F" {
op = "f"
} else if style == "FD" || style == "DF" {
op = "B"
}
c.stream.WriteString(fmt.Sprintf(" %s\n", op))
}
func (c *ContentObj) AppendUnderline(startX float64, y float64, endX float64, endY float64, text string) {
h := c.getRoot().config.PageSize.H
ut := int(0)
if c.getRoot().Curr.Font_IFont != nil {
ut = c.getRoot().Curr.Font_IFont.GetUt()
} else if c.getRoot().Curr.Font_ISubset != nil {
ut = int(c.getRoot().Curr.Font_ISubset.GetUt())
} else {
log.Fatal("error AppendUnderline not found font")
}
textH := ContentObj_CalTextHeight(c.getRoot().Curr.Font_Size)
arg3 := float64(h) - float64(y) - textH - textH*0.07
arg4 := (float64(ut) / 1000.00) * float64(c.getRoot().Curr.Font_Size)
c.stream.WriteString(fmt.Sprintf("%0.2f %0.2f %0.2f -%0.2f re f\n", startX, arg3, endX-startX, arg4))
}
//AppendStreamSetLineWidth : set line width
func (c *ContentObj) AppendStreamSetLineWidth(w float64) {
c.stream.WriteString(fmt.Sprintf("%.2f w\n", w))
}
//AppendStreamSetLineType : Set linetype [solid, dashed, dotted]
func (c *ContentObj) AppendStreamSetLineType(t string) {
switch t {
case "dashed":
c.stream.WriteString(fmt.Sprint("[5] 2 d\n"))
case "dotted":
c.stream.WriteString(fmt.Sprint("[2 3] 11 d\n"))
default:
c.stream.WriteString(fmt.Sprint("[] 0 d\n"))
}
}
//AppendStreamSetGrayFill set the grayscale fills
func (c *ContentObj) AppendStreamSetGrayFill(w float64) {
w = fixRange10(w)
c.stream.WriteString(fmt.Sprintf("%.2f g\n", w))
}
//AppendStreamSetGrayStroke set the grayscale stroke
func (c *ContentObj) AppendStreamSetGrayStroke(w float64) {
w = fixRange10(w)
c.stream.WriteString(fmt.Sprintf("%.2f G\n", w))
}
//AppendStreamSetColorStroke set the color stroke
func (c *ContentObj) AppendStreamSetColorStroke(r uint8, g uint8, b uint8) {
//w = fixRange10(w)
rFloat := float64(r) * 0.00392156862745
gFloat := float64(g) * 0.00392156862745
bFloat := float64(b) * 0.00392156862745
c.stream.WriteString(fmt.Sprintf("%.2f %.2f %.2f RG\n", rFloat, gFloat, bFloat))
}
//AppendStreamSetColorFill set the color fill
func (c *ContentObj) AppendStreamSetColorFill(r uint8, g uint8, b uint8) {
rFloat := float64(r) * 0.00392156862745
gFloat := float64(g) * 0.00392156862745
bFloat := float64(b) * 0.00392156862745
c.stream.WriteString(fmt.Sprintf("%.2f %.2f %.2f rg\n", rFloat, gFloat, bFloat))
}
//AppendStreamImage append image
func (c *ContentObj) AppendStreamImage(index int, x float64, y float64, rect *Rect) {
//fmt.Printf("index = %d",index)
h := c.getRoot().config.PageSize.H
c.stream.WriteString(fmt.Sprintf("q %0.2f 0 0 %0.2f %0.2f %0.2f cm /I%d Do Q\n", rect.W, rect.H, x, h-(y+rect.H), index+1))
}
//ContentObj_CalTextHeight calculate height of text
func ContentObj_CalTextHeight(fontsize int) float64 {
return (float64(fontsize) * 0.7)
}
// When setting colour and grayscales the value has to be between 0.00 and 1.00
// This function takes a float64 and returns 0.0 if it is less than 0.0 and 1.0 if it
// is more than 1.0
func fixRange10(val float64) float64 {
if val < 0.0 {
return 0.0
}
if val > 1.0 {
return 1.0
}
return val
}