forked from pdfcpu/pdfcpu
/
form.go
446 lines (392 loc) · 9.22 KB
/
form.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
Copyright 2021 The pdfcpu Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pdfcpu
import (
"strings"
pdffont "github.com/Equilaw/pdfcpu/pkg/font"
"github.com/pkg/errors"
)
func ParseHorAlignment(s string) (HAlignment, error) {
var a HAlignment
switch strings.ToLower(s) {
case "l", "left":
a = AlignLeft
case "r", "right":
a = AlignRight
case "c", "center":
a = AlignCenter
default:
return a, errors.Errorf("pdfcpu: unknown textfield alignment (left, center, right): %s", s)
}
return a, nil
}
// RelPosition represents the relative position of a text field's label.
type RelPosition int
// These are the options for relative label positions.
const (
RelPosLeft RelPosition = iota
RelPosRight
RelPosTop
RelPosBottom
)
func ParseRelPosition(s string) (RelPosition, error) {
var p RelPosition
switch strings.ToLower(s) {
case "l", "left":
p = RelPosLeft
case "r", "right":
p = RelPosRight
case "t", "top":
p = RelPosTop
case "b", "bottom":
p = RelPosBottom
default:
return p, errors.Errorf("pdfcpu: unknown textfield alignment (left, right, top, bottom): %s", s)
}
return p, nil
}
// Refactor because of orientation in nup.go
type Orientation int
const (
Horizontal Orientation = iota
Vertical
)
func ParseRadioButtonOrientation(s string) (Orientation, error) {
var o Orientation
switch strings.ToLower(s) {
case "h", "hor", "horizontal":
o = Horizontal
case "v", "vert", "vertical":
o = Vertical
default:
return o, errors.Errorf("pdfcpu: unknown radiobutton orientation (hor, vert): %s", s)
}
return o, nil
}
func AnchorPosition(a Anchor, r *Rectangle, w, h float64) (x float64, y float64) {
switch a {
case TopLeft:
x, y = 0, r.Height()-h
case TopCenter:
x, y = r.Width()/2-w/2, r.Height()-h
case TopRight:
x, y = r.Width()-w, r.Height()-h
case Left:
x, y = 0, r.Height()/2-h/2
case Center:
x, y = r.Width()/2-w/2, r.Height()/2-h/2
case Right:
x, y = r.Width()-w, r.Height()/2-h/2
case BottomLeft:
x, y = 0, 0
case BottomCenter:
x, y = r.Width()/2-w/2, 0
case BottomRight:
x, y = r.Width()-w, 0
}
return
}
// NormalizeCoord transfers P(x,y) from pdfcpu user space into PDF user space,
// which uses a coordinate system with origin in the lower left corner of r.
//
// pdfcpu user space coordinate systems have the origin in one of four corners of r:
//
// LowerLeft corner (default = PDF user space)
// x extends to the right,
// y extends upward
// LowerRight corner:
// x extends to the left,
// y extends upward
// UpperLeft corner:
// x extends to the right,
// y extends downward
// UpperRight corner:
// x extends to the left,
// y extends downward
func NormalizeCoord(x, y float64, r *Rectangle, origin Corner, absolute bool) (float64, float64) {
switch origin {
case UpperLeft:
if y >= 0 {
y = r.Height() - y
if y < 0 {
y = 0
}
}
case LowerRight:
if x >= 0 {
x = r.Width() - x
if x < 0 {
x = 0
}
}
case UpperRight:
if x >= 0 {
x = r.Width() - x
if x < 0 {
x = 0
}
}
if y >= 0 {
y = r.Height() - y
if y < 0 {
y = 0
}
}
}
if absolute {
if x >= 0 {
x += r.LL.X
}
if y >= 0 {
y += r.LL.Y
}
}
return x, y
}
// Normalize offset transfers x and y into offsets in the PDF user space.
func NormalizeOffset(x, y float64, origin Corner) (float64, float64) {
switch origin {
case UpperLeft:
y = -y
case LowerRight:
x = -x
case UpperRight:
x = -x
y = -y
}
return x, y
}
func addPageResources(
xRefTable *XRefTable,
pageDict Dict,
p Page,
fonts map[string]IndirectRef,
formFontIDs map[string]string) error {
fontRes := Dict{}
for fontName, font := range p.Fm {
if font.Res.IndRef != nil {
fonts[fontName] = *font.Res.IndRef
}
indRef, ok := fonts[fontName]
if !ok {
_, ok := formFontIDs[fontName]
ir, err := EnsureFontDict(xRefTable, fontName, !ok, nil)
if err != nil {
return err
}
indRef = *ir
fonts[fontName] = indRef
}
fontRes[font.Res.ID] = indRef
}
imgRes := Dict{}
for _, img := range p.Im {
imgRes[img.Res.ID] = *img.Res.IndRef
}
if len(fontRes) > 0 || len(imgRes) > 0 {
resDict := Dict{}
if len(fontRes) > 0 {
resDict["Font"] = fontRes
}
if len(imgRes) > 0 {
resDict["XObject"] = imgRes
}
pageDict["Resources"] = resDict
}
return nil
}
func CreatePage(
xRefTable *XRefTable,
parentPageIndRef IndirectRef,
p Page,
fonts map[string]IndirectRef,
fields *Array,
formFontIDs map[string]string) (*IndirectRef, Dict, error) {
pageDict := Dict(
map[string]Object{
"Type": Name("Page"),
"Parent": parentPageIndRef,
"MediaBox": p.MediaBox.Array(),
"CropBox": p.CropBox.Array(),
},
)
err := addPageResources(xRefTable, pageDict, p, fonts, formFontIDs)
if err != nil {
return nil, nil, err
}
ir, err := xRefTable.streamDictIndRef(p.Buf.Bytes())
if err != nil {
return nil, pageDict, err
}
pageDict.Insert("Contents", *ir)
pageDictIndRef, err := xRefTable.IndRefForNewObject(pageDict)
if err != nil {
return nil, nil, err
}
if len(p.AnnotIndRefs) > 0 || len(p.Annots) > 0 || len(p.LinkAnnots) > 0 {
a := p.AnnotIndRefs
for _, ann := range p.Annots {
ann["P"] = *pageDictIndRef
if _, ok := ann["Parent"]; ok {
continue
}
ir, err := xRefTable.IndRefForNewObject(ann)
if err != nil {
return nil, nil, err
}
a = append(a, *ir)
*fields = append(*fields, *ir)
}
for _, ar := range p.LinkAnnots {
d := ar.RenderDict(*pageDictIndRef)
ir, err := xRefTable.IndRefForNewObject(d)
if err != nil {
return nil, nil, err
}
a = append(a, *ir)
}
pageDict["Annots"] = a
}
return pageDictIndRef, pageDict, err
}
func updateUserfont(xRefTable *XRefTable, fontName string, font FontResource) error {
ttf, ok := pdffont.UserFontMetrics[fontName]
if !ok {
return errors.Errorf("pdfcpu: userfont %s not available", fontName)
}
usedGIDs, err := usedGIDsFromCMapIndRef(xRefTable, *font.ToUnicode)
if err != nil {
return err
}
for _, gid := range usedGIDs {
ttf.UsedGIDs[gid] = true
}
if _, err = toUnicodeCMap(xRefTable, ttf, true, font.ToUnicode); err != nil {
return err
}
if _, err = ttfSubFontFile(xRefTable, ttf, fontName, font.FontFile); err != nil {
return err
}
if _, err = CIDWidths(xRefTable, ttf, true, font.W); err != nil {
return err
}
if _, err := CIDSet(xRefTable, ttf, font.CIDSet); err != nil {
return err
}
return nil
}
func addPageResourcesForExistingPage(
xRefTable *XRefTable,
d, res Dict,
p Page,
fonts map[string]IndirectRef,
formFontIDs map[string]string) error {
if len(p.Fm) > 0 {
fontRes, ok := res["Font"].(Dict)
if !ok {
fontRes = Dict{}
}
for fontName, font := range p.Fm {
indRef, ok := fonts[fontName]
if ok {
fontRes.Insert(font.Res.ID, indRef)
continue
}
if font.Res.IndRef == nil {
// Create corefont or userfont.
_, ok := formFontIDs[fontName]
indRef, err := EnsureFontDict(xRefTable, fontName, !ok, nil)
if err != nil {
return err
}
fonts[fontName] = *indRef
fontRes.Insert(font.Res.ID, *indRef)
continue
}
if pdffont.IsCoreFont(fontName) || font.FontFile == nil {
// Reuse font.
fonts[fontName] = *font.Res.IndRef
fontRes.Insert(font.Res.ID, *font.Res.IndRef)
continue
}
if err := updateUserfont(xRefTable, fontName, font); err != nil {
return err
}
fonts[fontName] = *font.Res.IndRef
fontRes.Insert(font.Res.ID, *font.Res.IndRef)
}
res["Font"] = fontRes
}
if len(p.Im) > 0 {
imgRes, ok := res["XObject"].(Dict)
if !ok {
imgRes = Dict{}
}
for _, img := range p.Im {
imgRes.Insert(img.Res.ID, *img.Res.IndRef)
}
res["XObject"] = imgRes
}
if len(p.Fm) > 0 || len(p.Im) > 0 {
d["Resources"] = res
}
return nil
}
func ModifyPageContent(
xRefTable *XRefTable,
dIndRef IndirectRef,
d, res Dict,
p Page,
fonts map[string]IndirectRef,
fields *Array,
formFontIDs map[string]string) error {
// TODO Account for existing page rotation.
err := addPageResourcesForExistingPage(xRefTable, d, res, p, fonts, formFontIDs)
if err != nil {
return err
}
err = xRefTable.AppendContent(d, p.Buf.Bytes())
if err != nil {
return err
}
if len(p.AnnotIndRefs) > 0 || len(p.Annots) > 0 || len(p.LinkAnnots) > 0 {
a, err := xRefTable.DereferenceArray(d["Annots"])
if err != nil {
return err
}
a = append(a, p.AnnotIndRefs...)
for _, ann := range p.Annots {
ann["P"] = dIndRef
if _, ok := ann["Parent"]; ok {
// takes effect in xreftable entry?
continue
}
ir, err := xRefTable.IndRefForNewObject(ann)
if err != nil {
return err
}
a = append(a, *ir)
*fields = append(*fields, *ir)
}
for _, ar := range p.LinkAnnots {
d := ar.RenderDict(dIndRef)
ir, err := xRefTable.IndRefForNewObject(d)
if err != nil {
return err
}
a = append(a, *ir)
}
d["Annots"] = a
}
return nil
}