-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_obj.go
325 lines (294 loc) · 8.33 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
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
package gopdf
import (
"bytes"
"compress/zlib"
"strconv"
"strings"
)
//ContentObj content object
type ContentObj struct { //impl IObj
buffer bytes.Buffer
stream bytes.Buffer
listCache listCacheContent
//text bytes.Buffer
getRoot func() *GoPdf
}
func (c *ContentObj) protection() *PDFProtection {
return c.getRoot().protection()
}
func (c *ContentObj) init(funcGetRoot func() *GoPdf) {
c.getRoot = funcGetRoot
}
func (c *ContentObj) build(objID int) error {
buff, err := c.listCache.toStream(c.protection())
if err != nil {
return err
}
isFlate := (c.getRoot().compressLevel != zlib.NoCompression)
//zipvar buff bytes.Buffer
var cPtr *bytes.Buffer // "Pointer to content"
if isFlate {
var zbuff bytes.Buffer
zwriter, err := zlib.NewWriterLevel(&zbuff,
c.getRoot().compressLevel)
if err != nil {
// should never happen...
return err
}
c.stream.WriteTo(zwriter)
buff.WriteTo(zwriter)
zwriter.Close()
cPtr = &zbuff
} else {
// zlib compressLevel = 0 adds header and thus
// needs /FlateDecode => pass through
buff.WriteTo(&c.stream)
cPtr = &c.stream
}
streamlen := (*cPtr).Len()
c.buffer.WriteString("<<\n")
if isFlate {
c.buffer.WriteString("/Filter/FlateDecode")
}
c.buffer.WriteString("/Length " + strconv.Itoa(streamlen) + "\n")
c.buffer.WriteString(">>\n")
c.buffer.WriteString("stream\n")
if c.protection() != nil {
tmp, err := rc4Cip(c.protection().objectkey(objID), (*cPtr).Bytes())
if err != nil {
return err
}
c.buffer.Write(tmp)
c.buffer.WriteString("\n")
} else {
c.buffer.Write((*cPtr).Bytes())
if isFlate {
c.buffer.WriteString("\n")
}
}
c.buffer.WriteString("endstream\n")
return nil
}
func (c *ContentObj) getType() string {
return "Content"
}
func (c *ContentObj) getObjBuff() *bytes.Buffer {
return &(c.buffer)
}
//AppendStreamText append text
func (c *ContentObj) AppendStreamText(text string) error {
//support only CURRENT_FONT_TYPE_SUBSET
textColor := c.getRoot().curr.textColor()
grayFill := c.getRoot().curr.grayFill
fontCountIndex := c.getRoot().curr.Font_FontCount + 1
fontSize := c.getRoot().curr.Font_Size
fontStyle := c.getRoot().curr.Font_Style
x := c.getRoot().curr.X
y := c.getRoot().curr.Y
setXCount := c.getRoot().curr.setXCount
fontSubset := c.getRoot().curr.Font_ISubset
cache := cacheContentText{
fontSubset: fontSubset,
rectangle: nil,
textColor: textColor,
grayFill: grayFill,
fontCountIndex: fontCountIndex,
fontSize: fontSize,
fontStyle: fontStyle,
setXCount: setXCount,
x: x,
y: y,
pageheight: c.getRoot().curr.pageSize.H,
contentType: ContentTypeText,
lineWidth: c.getRoot().curr.lineWidth,
}
var err error
c.getRoot().curr.X, c.getRoot().curr.Y, err = c.listCache.appendContentText(cache, text)
if err != nil {
return err
}
return nil
}
//AppendStreamSubsetFont add stream of text
func (c *ContentObj) AppendStreamSubsetFont(rectangle *Rect, text string, cellOpt CellOption) error {
textColor := c.getRoot().curr.textColor()
grayFill := c.getRoot().curr.grayFill
fontCountIndex := c.getRoot().curr.Font_FontCount + 1
fontSize := c.getRoot().curr.Font_Size
fontStyle := c.getRoot().curr.Font_Style
x := c.getRoot().curr.X
y := c.getRoot().curr.Y
setXCount := c.getRoot().curr.setXCount
fontSubset := c.getRoot().curr.Font_ISubset
cache := cacheContentText{
fontSubset: fontSubset,
rectangle: rectangle,
textColor: textColor,
grayFill: grayFill,
fontCountIndex: fontCountIndex,
fontSize: fontSize,
fontStyle: fontStyle,
setXCount: setXCount,
x: x,
y: y,
pageheight: c.getRoot().curr.pageSize.H,
contentType: ContentTypeCell,
cellOpt: cellOpt,
lineWidth: c.getRoot().curr.lineWidth,
}
var err error
c.getRoot().curr.X, c.getRoot().curr.Y, err = c.listCache.appendContentText(cache, text)
if err != nil {
return err
}
return nil
}
//AppendStreamLine append line
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))
var cache cacheContentLine
cache.pageHeight = c.getRoot().curr.pageSize.H
cache.x1 = x1
cache.y1 = y1
cache.x2 = x2
cache.y2 = y2
c.listCache.append(&cache)
}
//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, style string) {
var cache cacheContentRectangle
cache.pageHeight = c.getRoot().curr.pageSize.H
cache.x = x
cache.y = y
cache.width = wdth
cache.height = hght
cache.style = style
c.listCache.append(&cache)
}
//AppendStreamOval append oval
func (c *ContentObj) AppendStreamOval(x1 float64, y1 float64, x2 float64, y2 float64) {
var cache cacheContentOval
cache.pageHeight = c.getRoot().curr.pageSize.H
cache.x1 = x1
cache.y1 = y1
cache.x2 = x2
cache.y2 = y2
c.listCache.append(&cache)
}
//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)
// D or empty string: draw. This is the default value.
// F: fill
// DF or FD: draw and fill
func (c *ContentObj) AppendStreamCurve(x0 float64, y0 float64, x1 float64, y1 float64, x2 float64, y2 float64, x3 float64, y3 float64, style string) {
var cache cacheContentCurve
cache.pageHeight = c.getRoot().curr.pageSize.H
cache.x0 = x0
cache.y0 = y0
cache.x1 = x1
cache.y1 = y1
cache.x2 = x2
cache.y2 = y2
cache.x3 = x3
cache.y3 = y3
cache.style = strings.ToUpper(strings.TrimSpace(style))
c.listCache.append(&cache)
}
//AppendStreamSetLineWidth : set line width
func (c *ContentObj) AppendStreamSetLineWidth(w float64) {
var cache cacheContentLineWidth
cache.width = w
c.listCache.append(&cache)
}
//AppendStreamSetLineType : Set linetype [solid, dashed, dotted]
func (c *ContentObj) AppendStreamSetLineType(t string) {
var cache cacheContentLineType
cache.lineType = t
c.listCache.append(&cache)
}
//AppendStreamSetGrayFill set the grayscale fills
func (c *ContentObj) AppendStreamSetGrayFill(w float64) {
w = fixRange10(w)
var cache cacheContentGray
cache.grayType = grayTypeFill
cache.scale = w
c.listCache.append(&cache)
}
//AppendStreamSetGrayStroke set the grayscale stroke
func (c *ContentObj) AppendStreamSetGrayStroke(w float64) {
w = fixRange10(w)
var cache cacheContentGray
cache.grayType = grayTypeStroke
cache.scale = w
c.listCache.append(&cache)
}
//AppendStreamSetColorStroke set the color stroke
func (c *ContentObj) AppendStreamSetColorStroke(r uint8, g uint8, b uint8) {
var cache cacheContentColor
cache.colorType = colorTypeStroke
cache.r = r
cache.g = g
cache.b = b
c.listCache.append(&cache)
}
//AppendStreamSetColorFill set the color fill
func (c *ContentObj) AppendStreamSetColorFill(r uint8, g uint8, b uint8) {
var cache cacheContentColor
cache.colorType = colorTypeFill
cache.r = r
cache.g = g
cache.b = b
c.listCache.append(&cache)
}
//AppendStreamImage append image
func (c *ContentObj) AppendStreamImage(index int, x float64, y float64, rect *Rect) {
//fmt.Printf("index = %d",index)
h := c.getRoot().curr.pageSize.H
var cache cacheContentImage
cache.h = h
cache.x = x
cache.y = y
cache.rect = *rect
cache.index = index
c.listCache.append(&cache)
//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
}
func convertTTFUnit2PDFUnit(n int, upem int) int {
var ret int
if n < 0 {
rest1 := n % upem
storrest := 1000 * rest1
//ledd2 := (storrest != 0 ? rest1 / storrest : 0);
ledd2 := 0
if storrest != 0 {
ledd2 = rest1 / storrest
} else {
ledd2 = 0
}
ret = -((-1000*n)/upem - int(ledd2))
} else {
ret = (n/upem)*1000 + ((n%upem)*1000)/upem
}
return ret
}