forked from signintech/gopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_content_text.go
368 lines (317 loc) · 9.57 KB
/
cache_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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package gopdf
import (
"errors"
"fmt"
"io"
)
//ContentTypeCell cell
const ContentTypeCell = 0
//ContentTypeText text
const ContentTypeText = 1
type cacheContentText struct {
//---setup---
rectangle *Rect
textColor Rgb
grayFill float64
fontCountIndex int //Curr.Font_FontCount+1
fontSize int
fontStyle int
setXCount int //จำนวนครั้งที่ใช้ setX
x, y float64
fontSubset *SubsetFontObj
pageheight float64
contentType int
cellOpt CellOption
lineWidth float64
text string
//---result---
cellWidthPdfUnit, textWidthPdfUnit float64
cellHeightPdfUnit float64
}
func (c *cacheContentText) isSame(cache cacheContentText) bool {
if c.rectangle != nil {
//if rectangle != nil we assumes this is not same content
return false
}
if c.textColor.equal(cache.textColor) &&
c.grayFill == cache.grayFill &&
c.fontCountIndex == cache.fontCountIndex &&
c.fontSize == cache.fontSize &&
c.fontStyle == cache.fontStyle &&
c.setXCount == cache.setXCount &&
c.y == cache.y {
return true
}
return false
}
func (c *cacheContentText) setPageHeight(pageheight float64) {
c.pageheight = pageheight
}
func (c *cacheContentText) pageHeight() float64 {
return c.pageheight //841.89
}
func convertTypoUnit(val float64, unitsPerEm uint, fontSize float64) float64 {
val = val * 1000.00 / float64(unitsPerEm)
return val * fontSize / 1000.0
}
func (c *cacheContentText) calTypoAscender() float64 {
return convertTypoUnit(float64(c.fontSubset.ttfp.TypoAscender()), c.fontSubset.ttfp.UnitsPerEm(), float64(c.fontSize))
}
func (c *cacheContentText) calTypoDescender() float64 {
return convertTypoUnit(float64(c.fontSubset.ttfp.TypoDescender()), c.fontSubset.ttfp.UnitsPerEm(), float64(c.fontSize))
}
func (c *cacheContentText) calY() (float64, error) {
pageHeight := c.pageHeight()
if c.contentType == ContentTypeText {
return pageHeight - c.y, nil
} else if c.contentType == ContentTypeCell {
y := float64(0.0)
if c.cellOpt.Align&Bottom == Bottom {
y = pageHeight - c.y - c.cellHeightPdfUnit - c.calTypoDescender()
} else if c.cellOpt.Align&Middle == Middle {
y = pageHeight - c.y - c.cellHeightPdfUnit*0.5 - (c.calTypoDescender()+c.calTypoAscender())*0.5
} else {
//top
y = pageHeight - c.y - c.calTypoAscender()
}
return y, nil
}
return 0.0, errors.New("contentType not found")
}
func (c *cacheContentText) calX() (float64, error) {
if c.contentType == ContentTypeText {
return c.x, nil
} else if c.contentType == ContentTypeCell {
x := float64(0.0)
if c.cellOpt.Align&Right == Right {
x = c.x + c.cellWidthPdfUnit - c.textWidthPdfUnit
} else if c.cellOpt.Align&Center == Center {
x = c.x + c.cellWidthPdfUnit*0.5 - c.textWidthPdfUnit*0.5
} else {
x = c.x
}
return x, nil
}
return 0.0, errors.New("contentType not found")
}
func (c *cacheContentText) write(w io.Writer, protection *PDFProtection) error {
r := c.textColor.r
g := c.textColor.g
b := c.textColor.b
x, err := c.calX()
if err != nil {
return err
}
y, err := c.calY()
if err != nil {
return err
}
io.WriteString(w, "BT\n")
fmt.Fprintf(w, "%0.2f %0.2f TD\n", x, y)
fmt.Fprintf(w, "/F%d %d Tf\n", c.fontCountIndex, c.fontSize)
if !(r == 0 && g == 0 && b == 0) {
rFloat := float64(r) * 0.00392156862745
gFloat := float64(g) * 0.00392156862745
bFloat := float64(b) * 0.00392156862745
fmt.Fprintf(w, "%0.2f %0.2f %0.2f rg\n", rFloat, gFloat, bFloat)
} else {
//c.AppendStreamSetGrayFill(grayFill)
}
io.WriteString(w, "[<")
unitsPerEm := int(c.fontSubset.ttfp.UnitsPerEm())
var leftRune rune
var leftRuneIndex uint
for i, r := range c.text {
glyphindex, err := c.fontSubset.CharIndex(r)
if err != nil {
return err
}
pairvalPdfUnit := 0
if i > 0 && c.fontSubset.ttfFontOption.UseKerning { //kerning
pairval := kern(c.fontSubset, leftRune, r, leftRuneIndex, glyphindex)
pairvalPdfUnit = convertTTFUnit2PDFUnit(int(pairval), unitsPerEm)
if pairvalPdfUnit != 0 {
fmt.Fprintf(w, ">%d<", (-1)*pairvalPdfUnit)
}
}
fmt.Fprintf(w, "%04X", glyphindex)
leftRune = r
leftRuneIndex = glyphindex
}
io.WriteString(w, ">] TJ\n")
io.WriteString(w, "ET\n")
if c.fontStyle&Underline == Underline {
err := c.underline(w, c.x, c.y, c.x+c.cellWidthPdfUnit, c.y)
if err != nil {
return err
}
}
c.drawBorder(w)
return nil
}
func (c *cacheContentText) drawBorder(w io.Writer) error {
//stream.WriteString(fmt.Sprintf("%.2f w\n", 0.1))
lineOffset := c.lineWidth * 0.5
if c.cellOpt.Border&Top == Top {
startX := c.x - lineOffset
startY := c.pageHeight() - c.y
endX := c.x + c.cellWidthPdfUnit + lineOffset
endY := startY
_, err := fmt.Fprintf(w, "%0.2f %0.2f m %0.2f %0.2f l s\n", startX, startY, endX, endY)
if err != nil {
return err
}
}
if c.cellOpt.Border&Left == Left {
startX := c.x
startY := c.pageHeight() - c.y
endX := c.x
endY := startY - c.cellHeightPdfUnit
_, err := fmt.Fprintf(w, "%0.2f %0.2f m %0.2f %0.2f l s\n", startX, startY, endX, endY)
if err != nil {
return err
}
}
if c.cellOpt.Border&Right == Right {
startX := c.x + c.cellWidthPdfUnit
startY := c.pageHeight() - c.y
endX := c.x + c.cellWidthPdfUnit
endY := startY - c.cellHeightPdfUnit
_, err := fmt.Fprintf(w, "%0.2f %0.2f m %0.2f %0.2f l s\n", startX, startY, endX, endY)
if err != nil {
return err
}
}
if c.cellOpt.Border&Bottom == Bottom {
startX := c.x - lineOffset
startY := c.pageHeight() - c.y - c.cellHeightPdfUnit
endX := c.x + c.cellWidthPdfUnit + lineOffset
endY := startY
_, err := fmt.Fprintf(w, "%0.2f %0.2f m %0.2f %0.2f l s\n", startX, startY, endX, endY)
if err != nil {
return err
}
}
return nil
}
func (c *cacheContentText) underline(w io.Writer, startX float64, startY float64, endX float64, endY float64) error {
if c.fontSubset == nil {
return errors.New("error AppendUnderline not found font")
}
unitsPerEm := float64(c.fontSubset.ttfp.UnitsPerEm())
h := c.pageHeight()
ut := float64(c.fontSubset.GetUt())
up := float64(c.fontSubset.GetUp())
textH := ContentObj_CalTextHeight(c.fontSize)
arg3 := float64(h) - (float64(startY) - ((up / unitsPerEm) * float64(c.fontSize))) - textH
arg4 := (ut / unitsPerEm) * float64(c.fontSize)
fmt.Fprintf(w, "%0.2f %0.2f %0.2f -%0.2f re f\n", startX, arg3, endX-startX, arg4)
//fmt.Printf("arg3=%f arg4=%f\n", arg3, arg4)
return nil
}
func (c *cacheContentText) createContent() (float64, float64, error) {
cellWidthPdfUnit, cellHeightPdfUnit, textWidthPdfUnit, err := createContent(c.fontSubset, c.text, c.fontSize, c.rectangle)
if err != nil {
return 0, 0, err
}
c.cellWidthPdfUnit = cellWidthPdfUnit
c.cellHeightPdfUnit = cellHeightPdfUnit
c.textWidthPdfUnit = textWidthPdfUnit
return cellWidthPdfUnit, cellHeightPdfUnit, nil
}
func createContent(f *SubsetFontObj, text string, fontSize int, rectangle *Rect) (float64, float64, float64, error) {
unitsPerEm := int(f.ttfp.UnitsPerEm())
var leftRune rune
var leftRuneIndex uint
sumWidth := int(0)
//fmt.Printf("unitsPerEm = %d", unitsPerEm)
for i, r := range text {
glyphindex, err := f.CharIndex(r)
if err != nil {
return 0, 0, 0, err
}
pairvalPdfUnit := 0
if i > 0 && f.ttfFontOption.UseKerning { //kerning
pairval := kern(f, leftRune, r, leftRuneIndex, glyphindex)
pairvalPdfUnit = convertTTFUnit2PDFUnit(int(pairval), unitsPerEm)
}
width, err := f.CharWidth(r)
if err != nil {
return 0, 0, 0, err
}
sumWidth += int(width) + int(pairvalPdfUnit)
leftRune = r
leftRuneIndex = glyphindex
}
cellWidthPdfUnit := float64(0)
cellHeightPdfUnit := float64(0)
if rectangle == nil {
cellWidthPdfUnit = float64(sumWidth) * (float64(fontSize) / 1000.0)
typoAscender := convertTypoUnit(float64(f.ttfp.TypoAscender()), f.ttfp.UnitsPerEm(), float64(fontSize))
typoDescender := convertTypoUnit(float64(f.ttfp.TypoDescender()), f.ttfp.UnitsPerEm(), float64(fontSize))
cellHeightPdfUnit = typoAscender - typoDescender
} else {
cellWidthPdfUnit = rectangle.W
cellHeightPdfUnit = rectangle.H
}
textWidthPdfUnit := float64(sumWidth) * (float64(fontSize) / 1000.0)
return cellWidthPdfUnit, cellHeightPdfUnit, textWidthPdfUnit, nil
}
func kern(f *SubsetFontObj, leftRune rune, rightRune rune, leftIndex uint, rightIndex uint) int16 {
pairVal := int16(0)
if haveKerning, kval := f.KernValueByLeft(leftIndex); haveKerning {
if ok, v := kval.ValueByRight(rightIndex); ok {
pairVal = v
}
}
if f.funcKernOverride != nil {
pairVal = f.funcKernOverride(
leftRune,
rightRune,
leftIndex,
rightIndex,
pairVal,
)
}
return pairVal
}
//CacheContent Export cacheContent
type CacheContent struct {
cacheContentText
}
//Setup setup all infomation for cacheContent
func (c *CacheContent) Setup(rectangle *Rect,
textColor Rgb,
grayFill float64,
fontCountIndex int, //Curr.Font_FontCount+1
fontSize int,
fontStyle int,
setXCount int, //จำนวนครั้งที่ใช้ setX
x, y float64,
fontSubset *SubsetFontObj,
pageheight float64,
contentType int,
cellOpt CellOption,
lineWidth float64,
) {
c.cacheContentText = cacheContentText{
fontSubset: fontSubset,
rectangle: rectangle,
textColor: textColor,
grayFill: grayFill,
fontCountIndex: fontCountIndex,
fontSize: fontSize,
fontStyle: fontStyle,
setXCount: setXCount,
x: x,
y: y,
pageheight: pageheight,
contentType: ContentTypeCell,
cellOpt: cellOpt,
lineWidth: lineWidth,
}
}
//WriteTextToContent write text to content
func (c *CacheContent) WriteTextToContent(text string) {
c.cacheContentText.text += text
}