/
boxes.go
1230 lines (1093 loc) · 29.1 KB
/
boxes.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2020 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 (
"fmt"
"strconv"
"strings"
"github.com/pdfcpu/pdfcpu/pkg/types"
"github.com/pkg/errors"
)
// Box is a rectangular region in user space
// expressed either explicitly via Rect
// or implicitly via margins applied to the containing parent box.
// Media box serves as parent box for crop box.
// Crop box serves as parent box for trim, bleed and art box.
type Box struct {
Rect *Rectangle // Rectangle in user space.
Inherited bool // Media box and Crop box may be inherited.
RefBox string // Use position of another box,
// Margins to parent box in points.
// Relative to parent box if 0 < x < 0.5
MLeft, MRight float64
MTop, MBot float64
// Relative position within parent box
Dim *Dim // dimensions
Pos anchor // position anchor within parent box, one of tl,tc,tr,l,c,r,bl,bc,br.
Dx, Dy int // anchor offset
}
// PageBoundaries represent the defined PDF page boundaries.
type PageBoundaries struct {
Media, Crop, Trim, Bleed, Art *Box
}
// SelectAll selects all page boundaries.
func (pb *PageBoundaries) SelectAll() {
b := &Box{}
pb.Media, pb.Crop, pb.Trim, pb.Bleed, pb.Art = b, b, b, b, b
}
func (pb PageBoundaries) String() string {
ss := []string{}
if pb.Media != nil {
ss = append(ss, "mediaBox")
}
if pb.Crop != nil {
ss = append(ss, "cropBox")
}
if pb.Trim != nil {
ss = append(ss, "trimBox")
}
if pb.Bleed != nil {
ss = append(ss, "bleedBox")
}
if pb.Art != nil {
ss = append(ss, "artBox")
}
return strings.Join(ss, ", ")
}
// MediaBox returns the effective mediabox for pb.
func (pb PageBoundaries) MediaBox() *Rectangle {
if pb.Media == nil {
return nil
}
return pb.Media.Rect
}
// CropBox returns the effective cropbox for pb.
func (pb PageBoundaries) CropBox() *Rectangle {
if pb.Crop == nil || pb.Crop.Rect == nil {
return pb.MediaBox()
}
return pb.Crop.Rect
}
// TrimBox returns the effective trimbox for pb.
func (pb PageBoundaries) TrimBox() *Rectangle {
if pb.Trim == nil || pb.Trim.Rect == nil {
return pb.CropBox()
}
return pb.Trim.Rect
}
// BleedBox returns the effective bleedbox for pb.
func (pb PageBoundaries) BleedBox() *Rectangle {
if pb.Bleed == nil || pb.Bleed.Rect == nil {
return pb.CropBox()
}
return pb.Bleed.Rect
}
// ArtBox returns the effective artbox for pb.
func (pb PageBoundaries) ArtBox() *Rectangle {
if pb.Art == nil || pb.Art.Rect == nil {
return pb.CropBox()
}
return pb.Art.Rect
}
// ResolveBox resolves s and tries to assign an empty page boundary.
func (pb *PageBoundaries) ResolveBox(s string) error {
for _, k := range []string{"media", "crop", "trim", "bleed", "art"} {
b := &Box{}
if strings.HasPrefix(k, s) {
switch k {
case "media":
pb.Media = b
case "crop":
pb.Crop = b
case "trim":
pb.Trim = b
case "bleed":
pb.Bleed = b
case "art":
pb.Art = b
}
return nil
}
}
return errors.Errorf("pdfcpu: invalid box prefix: %s", s)
}
// ParseBoxList parses a list of box types.
func ParseBoxList(s string) (*PageBoundaries, error) {
// A comma separated, unsorted list of values:
//
// m(edia), c(rop), t(rim), b(leed), a(rt)
s = strings.TrimSpace(s)
if len(s) == 0 {
return nil, nil
}
pb := &PageBoundaries{}
for _, s := range strings.Split(s, ",") {
if err := pb.ResolveBox(strings.TrimSpace(s)); err != nil {
return nil, err
}
}
return pb, nil
}
func resolveBoxType(s string) (string, error) {
for _, k := range []string{"media", "crop", "trim", "bleed", "art"} {
if strings.HasPrefix(k, s) {
return k, nil
}
}
return "", errors.Errorf("pdfcpu: invalid box type: %s", s)
}
func processBox(b **Box, boxID, paramValueStr string, unit DisplayUnit) error {
var err error
if *b != nil {
return errors.Errorf("pdfcpu: duplicate box definition: %s", boxID)
}
// process box assignment
boxVal, err := resolveBoxType(paramValueStr)
if err == nil {
if boxVal == boxID {
return errors.Errorf("pdfcpu: invalid box self assigment: %s", boxID)
}
*b = &Box{RefBox: boxVal}
return nil
}
// process box definition
*b, err = ParseBox(paramValueStr, unit)
return err
}
// ParsePageBoundaries parses a list of box definitions and assignments.
func ParsePageBoundaries(s string, unit DisplayUnit) (*PageBoundaries, error) {
// A sequence of box definitions/assignments:
//
// m(edia): {box}
// c(rop): {box}
// a(rt): {box} | b(leed) | c(rop) | m(edia) | t(rim)
// b(leed): {box} | a(rt) | c(rop) | m(edia) | t(rim)
// t(rim): {box} | a(rt) | b(leed) | c(rop) | m(edia)
s = strings.TrimSpace(s)
if len(s) == 0 {
return nil, errors.New("pdfcpu: missing page boundaries in the form of box definitions/assignments")
}
pb := &PageBoundaries{}
for _, s := range strings.Split(s, ",") {
s1 := strings.Split(s, ":")
if len(s1) != 2 {
return nil, errors.New("pdfcpu: invalid box assignment")
}
paramPrefix := strings.TrimSpace(s1[0])
paramValueStr := strings.TrimSpace(s1[1])
boxKey, err := resolveBoxType(paramPrefix)
if err != nil {
return nil, errors.New("pdfcpu: invalid box type")
}
// process box definition
switch boxKey {
case "media":
if pb.Media != nil {
return nil, errors.New("pdfcpu: duplicate box definition: media")
}
// process media box definition
pb.Media, err = ParseBox(paramValueStr, unit)
case "crop":
if pb.Crop != nil {
return nil, errors.New("pdfcpu: duplicate box definition: crop")
}
// process crop box definition
pb.Crop, err = ParseBox(paramValueStr, unit)
case "trim":
err = processBox(&pb.Trim, "trim", paramValueStr, unit)
case "bleed":
err = processBox(&pb.Bleed, "bleed", paramValueStr, unit)
case "art":
err = processBox(&pb.Art, "art", paramValueStr, unit)
}
if err != nil {
return nil, err
}
}
return pb, nil
}
func parseBoxByRectangle(s string, u DisplayUnit) (*Box, error) {
ss := strings.Fields(s)
if len(ss) != 4 {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
f, err := strconv.ParseFloat(ss[0], 64)
if err != nil {
return nil, err
}
xmin := toUserSpace(f, u)
f, err = strconv.ParseFloat(ss[1], 64)
if err != nil {
return nil, err
}
ymin := toUserSpace(f, u)
f, err = strconv.ParseFloat(ss[2], 64)
if err != nil {
return nil, err
}
xmax := toUserSpace(f, u)
f, err = strconv.ParseFloat(ss[3], 64)
if err != nil {
return nil, err
}
ymax := toUserSpace(f, u)
if xmax < xmin {
xmin, xmax = xmax, xmin
}
if ymax < ymin {
ymin, ymax = ymax, ymin
}
return &Box{Rect: Rect(xmin, ymin, xmax, ymax)}, nil
}
func parseBoxPercentage(s string) (float64, error) {
pct, err := strconv.ParseFloat(s, 64)
if err != nil {
return 0, err
}
if pct <= -50 || pct >= 50 {
return 0, errors.Errorf("pdfcpu: invalid margin percentage: %s must be < 50%%", s)
}
return pct / 100, nil
}
func parseBoxBySingleMarginVal(s, s1 string, abs bool, u DisplayUnit) (*Box, error) {
if s1[len(s1)-1] == '%' {
// margin percentage
// 10.5%
// % has higher precedence than abs/rel.
s1 = s1[:len(s1)-1]
if len(s1) == 0 {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
m, err := parseBoxPercentage(s1)
if err != nil {
return nil, err
}
return &Box{MLeft: m, MRight: m, MTop: m, MBot: m}, nil
}
m, err := strconv.ParseFloat(s1, 64)
if err != nil {
return nil, err
}
if !abs {
// 0.25 rel (=25%)
if m <= 0 || m >= .5 {
return nil, errors.Errorf("pdfcpu: invalid relative box margin: %f must be positive < 0.5", m)
}
return &Box{MLeft: m, MRight: m, MTop: m, MBot: m}, nil
}
// 10
// 10 abs
// .5
// .5 abs
m = toUserSpace(m, u)
return &Box{MLeft: m, MRight: m, MTop: m, MBot: m}, nil
}
func parseBoxBy2Percentages(s, s1, s2 string) (*Box, error) {
// 10% 40%
// Parse vert margin.
s1 = s1[:len(s1)-1]
if len(s1) == 0 {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
vm, err := parseBoxPercentage(s1)
if err != nil {
return nil, err
}
if s2[len(s2)-1] != '%' {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
// Parse hor margin.
s2 = s2[:len(s2)-1]
if len(s2) == 0 {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
hm, err := parseBoxPercentage(s2)
if err != nil {
return nil, err
}
return &Box{MLeft: hm, MRight: hm, MTop: vm, MBot: vm}, nil
}
func parseBoxBy2MarginVals(s, s1, s2 string, abs bool, u DisplayUnit) (*Box, error) {
if s1[len(s1)-1] == '%' {
return parseBoxBy2Percentages(s, s1, s2)
}
// 10 5
// 10 5 abs
// .1 .5
// .1 .5 abs
// .1 .4 rel
vm, err := strconv.ParseFloat(s1, 64)
if err != nil {
return nil, err
}
if !abs {
// eg 0.25 rel (=25%)
if vm <= 0 || vm >= .5 {
return nil, errors.Errorf("pdfcpu: invalid relative vertical box margin: %f must be positive < 0.5", vm)
}
}
hm, err := strconv.ParseFloat(s2, 64)
if err != nil {
return nil, err
}
if !abs {
// eg 0.25 rel (=25%)
if hm <= 0 || hm >= .5 {
return nil, errors.Errorf("pdfcpu: invalid relative horizontal box margin: %f must be positive < 0.5", hm)
}
}
if abs {
vm = toUserSpace(vm, u)
hm = toUserSpace(hm, u)
}
return &Box{MLeft: hm, MRight: hm, MTop: vm, MBot: vm}, nil
}
func parseBoxBy3Percentages(s, s1, s2, s3 string) (*Box, error) {
// 10% 15.5% 10%
// Parse top margin.
s1 = s1[:len(s1)-1]
if len(s1) == 0 {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
pct, err := strconv.ParseFloat(s1, 64)
if err != nil {
return nil, err
}
tm := pct / 100
if s2[len(s2)-1] != '%' {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
// Parse hor margin.
s2 = s2[:len(s2)-1]
if len(s2) == 0 {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
hm, err := parseBoxPercentage(s2)
if err != nil {
return nil, err
}
if s3[len(s3)-1] != '%' {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
// Parse bottom margin.
s3 = s3[:len(s3)-1]
if len(s3) == 0 {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
pct, err = strconv.ParseFloat(s3, 64)
if err != nil {
return nil, err
}
bm := pct / 100
if tm+bm >= 1 {
return nil, errors.Errorf("pdfcpu: vertical margin overflow: %s", s)
}
return &Box{MLeft: hm, MRight: hm, MTop: tm, MBot: bm}, nil
}
func parseBoxBy3MarginVals(s, s1, s2, s3 string, abs bool, u DisplayUnit) (*Box, error) {
if s1[len(s1)-1] == '%' {
return parseBoxBy3Percentages(s, s1, s2, s3)
}
// 10 5 15 ... absolute, top:10 left,right:5 bottom:15
// 10 5 15 abs ... absolute, top:10 left,right:5 bottom:15
// .1 .155 .1 ... absolute, top:.1 left,right:.155 bottom:.1
// .1 .155 .1 abs ... absolute, top:.1 left,right:.155 bottom:.1
// .1 .155 .1 rel ... relative, top:.1 left,right:.155 bottom:.1
tm, err := strconv.ParseFloat(s1, 64)
if err != nil {
return nil, err
}
hm, err := strconv.ParseFloat(s2, 64)
if err != nil {
return nil, err
}
if !abs {
// eg 0.25 rel (=25%)
if hm <= 0 || hm >= .5 {
return nil, errors.Errorf("pdfcpu: invalid relative horizontal box margin: %f must be positive < 0.5", hm)
}
}
bm, err := strconv.ParseFloat(s3, 64)
if err != nil {
return nil, err
}
if !abs && (tm+bm >= 1) {
return nil, errors.Errorf("pdfcpu: vertical margin overflow: %s", s)
}
if abs {
tm = toUserSpace(tm, u)
hm = toUserSpace(hm, u)
bm = toUserSpace(bm, u)
}
return &Box{MLeft: hm, MRight: hm, MTop: tm, MBot: bm}, nil
}
func parseBoxBy4Percentages(s, s1, s2, s3, s4 string) (*Box, error) {
// 10% 15% 15% 10%
// Parse top margin.
s1 = s1[:len(s1)-1]
if len(s1) == 0 {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
pct, err := strconv.ParseFloat(s1, 64)
if err != nil {
return nil, err
}
tm := pct / 100
// Parse right margin.
if s2[len(s2)-1] != '%' {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
s2 = s2[:len(s2)-1]
if len(s2) == 0 {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
pct, err = strconv.ParseFloat(s1, 64)
if err != nil {
return nil, err
}
rm := pct / 100
// Parse bottom margin.
if s3[len(s3)-1] != '%' {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
s3 = s3[:len(s3)-1]
if len(s3) == 0 {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
pct, err = strconv.ParseFloat(s3, 64)
if err != nil {
return nil, err
}
bm := pct / 100
// Parse left margin.
if s4[len(s4)-1] != '%' {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
s4 = s4[:len(s4)-1]
if len(s4) == 0 {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
pct, err = strconv.ParseFloat(s3, 64)
if err != nil {
return nil, err
}
lm := pct / 100
if tm+bm >= 1 {
return nil, errors.Errorf("pdfcpu: vertical margin overflow: %s", s)
}
if rm+lm >= 1 {
return nil, errors.Errorf("pdfcpu: horizontal margin overflow: %s", s)
}
return &Box{MLeft: lm, MRight: rm, MTop: tm, MBot: bm}, nil
}
func parseBoxBy4MarginVals(s, s1, s2, s3, s4 string, abs bool, u DisplayUnit) (*Box, error) {
if s1[len(s1)-1] == '%' {
return parseBoxBy4Percentages(s, s1, s2, s3, s4)
}
// 0.4 0.4 20 20 ... absolute, top:.4 right:.4 bottom:20 left:20
// 0.4 0.4 .1 .1 ... absolute, top:.4 right:.4 bottom:.1 left:.1
// 0.4 0.4 .1 .1 abs ... absolute, top:.4 right:.4 bottom:.1 left:.1
// 0.4 0.4 .1 .1 rel ... relative, top:.4 right:.4 bottom:.1 left:.1
// Parse top margin.
tm, err := strconv.ParseFloat(s1, 64)
if err != nil {
return nil, err
}
// Parse right margin.
rm, err := strconv.ParseFloat(s2, 64)
if err != nil {
return nil, err
}
// Parse bottom margin.
bm, err := strconv.ParseFloat(s3, 64)
if err != nil {
return nil, err
}
// Parse left margin.
lm, err := strconv.ParseFloat(s4, 64)
if err != nil {
return nil, err
}
if !abs {
if tm+bm >= 1 {
return nil, errors.Errorf("pdfcpu: vertical margin overflow: %s", s)
}
if lm+rm >= 1 {
return nil, errors.Errorf("pdfcpu: horizontal margin overflow: %s", s)
}
}
if abs {
tm = toUserSpace(tm, u)
rm = toUserSpace(rm, u)
bm = toUserSpace(bm, u)
lm = toUserSpace(lm, u)
}
return &Box{MLeft: lm, MRight: rm, MTop: tm, MBot: bm}, nil
}
func parseBoxOffset(s string, b *Box, u DisplayUnit) error {
d := strings.Split(s, " ")
if len(d) != 2 {
return errors.Errorf("pdfcpu: illegal position offset string: need 2 numeric values, %s\n", s)
}
f, err := strconv.ParseFloat(d[0], 64)
if err != nil {
return err
}
b.Dx = int(toUserSpace(f, u))
f, err = strconv.ParseFloat(d[1], 64)
if err != nil {
return err
}
b.Dy = int(toUserSpace(f, u))
return nil
}
func parseBoxDimByPercentage(s, s1, s2 string, b *Box) error {
// 10% 40%
// Parse width.
s1 = s1[:len(s1)-1]
if len(s1) == 0 {
return errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
pct, err := strconv.ParseFloat(s1, 64)
if err != nil {
return err
}
if pct <= 0 || pct >= 100 {
return errors.Errorf("pdfcpu: invalid percentage: %s", s)
}
w := pct / 100
if s2[len(s2)-1] != '%' {
return errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
// Parse height.
s2 = s2[:len(s2)-1]
if len(s2) == 0 {
return errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
pct, err = strconv.ParseFloat(s2, 64)
if err != nil {
return err
}
if pct <= 0 || pct >= 100 {
return errors.Errorf("pdfcpu: invalid percentage: %s", s)
}
h := pct / 100
b.Dim = &Dim{w, h}
return nil
}
func parseBoxDimWidthAndHeight(s1, s2 string, abs bool) (float64, float64, error) {
var (
w, h float64
err error
)
w, err = strconv.ParseFloat(s1, 64)
if err != nil {
return w, h, err
}
if !abs {
// eg 0.25 rel (=25%)
if w <= 0 || w >= 1 {
return w, h, errors.Errorf("pdfcpu: invalid relative box width: %f must be positive < 1", w)
}
}
h, err = strconv.ParseFloat(s2, 64)
if err != nil {
return w, h, err
}
if !abs {
// eg 0.25 rel (=25%)
if h <= 0 || h >= 1 {
return w, h, errors.Errorf("pdfcpu: invalid relative box height: %f must be positive < 1", h)
}
}
return w, h, nil
}
func parseBoxDim(s string, b *Box, u DisplayUnit) error {
ss := strings.Fields(s)
if len(ss) != 2 && len(ss) != 3 {
return errors.Errorf("pdfcpu: illegal dimension string: need 2 positive numeric values, %s\n", s)
}
abs := true
if len(ss) == 3 {
s1 := ss[2]
if s1 != "rel" && s1 != "abs" {
return errors.New("pdfcpu: illegal dimension string")
}
abs = s1 == "abs"
}
s1, s2 := ss[0], ss[1]
if s1[len(s1)-1] == '%' {
return parseBoxDimByPercentage(s, s1, s2, b)
}
w, h, err := parseBoxDimWidthAndHeight(s1, s2, abs)
if err != nil {
return err
}
if abs {
w = toUserSpace(w, u)
h = toUserSpace(h, u)
}
b.Dim = &Dim{w, h}
return nil
}
func parseBoxByPosWithinParent(s string, ss []string, u DisplayUnit) (*Box, error) {
b := &Box{Pos: Center}
for _, s := range ss {
ss1 := strings.Split(s, ":")
if len(ss1) != 2 {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
paramPrefix := strings.TrimSpace(ss1[0])
paramValueStr := strings.TrimSpace(ss1[1])
switch paramPrefix {
case "dim":
if err := parseBoxDim(paramValueStr, b, u); err != nil {
return nil, err
}
case "pos":
a, err := parsePositionAnchor(paramValueStr)
if err != nil {
return nil, err
}
b.Pos = a
case "off":
if err := parseBoxOffset(paramValueStr, b, u); err != nil {
return nil, err
}
default:
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
}
if b.Dim == nil {
return nil, errors.New("pdfcpu: missing box definition attr dim")
}
return b, nil
}
func parseBoxByMarginVals(ss []string, s string, abs bool, u DisplayUnit) (*Box, error) {
switch len(ss) {
case 1:
return parseBoxBySingleMarginVal(s, ss[0], abs, u)
case 2:
return parseBoxBy2MarginVals(s, ss[0], ss[1], abs, u)
case 3:
return parseBoxBy3MarginVals(s, ss[0], ss[1], ss[2], abs, u)
case 4:
return parseBoxBy4MarginVals(s, ss[0], ss[1], ss[2], ss[3], abs, u)
case 5:
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
return nil, nil
}
// ParseBox parses a box definition.
func ParseBox(s string, u DisplayUnit) (*Box, error) {
// A rectangular region in userspace expressed in terms of
// a rectangle or margins relative to its parent box.
// Media box serves as parent/default for crop box.
// Crop box serves as parent/default for trim, bleed and art box:
// [0 10 200 150] ... rectangle
// 0.5 0.5 20 20 ... absolute, top:.5 right:.5 bottom:20 left:20
// 0.5 0.5 .1 .1 abs ... absolute, top:.5 right:.5 bottom:.1 left:.1
// 0.5 0.5 .1 .1 rel ... relative, top:.5 right:.5 bottom:20 left:20
// 10 ... absolute, top,right,bottom,left:10
// 10 5 ... absolute, top,bottom:10 left,right:5
// 10 5 15 ... absolute, top:10 left,right:5 bottom:15
// 5% <50% ... relative, top,right,bottom,left:5% of parent box width/height
// .1 .5 ... absolute, top,bottom:.1 left,right:.5
// .1 .3 rel ... relative, top,bottom:.1=10% left,right:.3=30%
// -10 ... absolute, top,right,bottom,left enlarging the parent box as well
// dim:30 30 ... 30 x 30 display units, anchored at center of parent box
// dim:30 30 abs ... 30 x 30 display units, anchored at center of parent box
// dim:.3 .3 rel ... 0.3 x 0.3 relative width/height of parent box, anchored at center of parent box
// dim:30% 30% ... 0.3 x 0.3 relative width/height of parent box, anchored at center of parent box
// pos:tl, dim:30 30 ... 0.3 x 0.3 relative width/height of parent box, anchored at top left corner of parent box
// pos:bl, off: 5 5, dim:30 30 ...30 x 30 display units with offset 5/5, anchored at bottom left corner of parent box
// pos:bl, off: -5 -5, dim:.3 .3 rel ...0.3 x 0.3 relative width/height and anchored at bottom left corner of parent box
s = strings.TrimSpace(s)
if len(s) == 0 {
return nil, nil
}
if s[0] == '[' && s[len(s)-1] == ']' {
// Rectangle in PDF Array notation.
return parseBoxByRectangle(s[1:len(s)-1], u)
}
// Via relative position within parent box.
ss := strings.Split(s, ",")
if len(ss) > 3 {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
if len(ss) > 1 || strings.HasPrefix(ss[0], "dim") {
return parseBoxByPosWithinParent(s, ss, u)
}
// Via margins relative to parent box.
ss = strings.Fields(s)
if len(ss) > 5 {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
if len(ss) == 1 && (ss[0] == "abs" || ss[0] == "rel") {
return nil, errors.Errorf("pdfcpu: invalid box definition: %s", s)
}
abs := true
l := len(ss) - 1
s1 := ss[l]
if s1 == "rel" || s1 == "abs" {
abs = s1 == "abs"
ss = ss[:l]
}
return parseBoxByMarginVals(ss, s, abs, u)
}
// ListPageBoundaries lists page boundaries specified in wantPB for selected pages.
func (ctx *Context) ListPageBoundaries(selectedPages IntSet, wantPB *PageBoundaries) ([]string, error) {
unit := ctx.unit()
// TODO ctx.PageBoundaries(selectedPages)
pbs, err := ctx.PageBoundaries()
if err != nil {
return nil, err
}
ss := []string{}
for i, pb := range pbs {
if _, found := selectedPages[i+1]; !found {
continue
}
ss = append(ss, fmt.Sprintf("Page %d:", i+1))
if wantPB.Media != nil {
s := ""
if pb.Media.Inherited {
s = "(inherited)"
}
ss = append(ss, fmt.Sprintf(" MediaBox (%s) %v %s", unit, pb.MediaBox().Format(ctx.Unit), s))
}
if wantPB.Crop != nil {
s := ""
if pb.Crop == nil {
s = "(default)"
} else if pb.Crop.Inherited {
s = "(inherited)"
}
ss = append(ss, fmt.Sprintf(" CropBox (%s) %v %s", unit, pb.CropBox().Format(ctx.Unit), s))
}
if wantPB.Trim != nil {
s := ""
if pb.Trim == nil {
s = "(default)"
}
ss = append(ss, fmt.Sprintf(" TrimBox (%s) %v %s", unit, pb.TrimBox().Format(ctx.Unit), s))
}
if wantPB.Bleed != nil {
s := ""
if pb.Bleed == nil {
s = "(default)"
}
ss = append(ss, fmt.Sprintf(" BleedBox (%s) %v %s", unit, pb.BleedBox().Format(ctx.Unit), s))
}
if wantPB.Art != nil {
s := ""
if pb.Art == nil {
s = "(default)"
}
ss = append(ss, fmt.Sprintf(" ArtBox (%s) %v %s", unit, pb.ArtBox().Format(ctx.Unit), s))
}
ss = append(ss, "")
}
return ss, nil
}
// RemovePageBoundaries removes page boundaries specified by pb for selected pages.
// The media box is mandatory (inherited or not) and can't be removed.
// A removed crop box defaults to the media box.
// Removed trim/bleed/art boxes default to the crop box.
func (ctx *Context) RemovePageBoundaries(selectedPages IntSet, pb *PageBoundaries) error {
for k, v := range selectedPages {
if !v {
continue
}
d, inhPAttrs, err := ctx.PageDict(k, false)
if err != nil {
return err
}
if pb.Crop != nil {
if oldVal := d.Delete("CropBox"); oldVal == nil {
d.Insert("CropBox", inhPAttrs.mediaBox.Array())
}
}
if pb.Trim != nil {
d.Delete("TrimBox")
}
if pb.Bleed != nil {
d.Delete("BleedBox")
}
if pb.Art != nil {
d.Delete("ArtBox")
}
}
return nil
}
func boxLowerLeftCorner(r *Rectangle, w, h float64, a anchor) types.Point {
var p types.Point
switch a {
case TopLeft:
p.X = r.LL.X
p.Y = r.UR.Y - h
case TopCenter:
p.X = r.UR.X - r.Width()/2 - w/2
p.Y = r.UR.Y - h
case TopRight:
p.X = r.UR.X - w
p.Y = r.UR.Y - h
case Left:
p.X = r.LL.X
p.Y = r.UR.Y - r.Height()/2 - h/2
case Center:
p.X = r.UR.X - r.Width()/2 - w/2
p.Y = r.UR.Y - r.Height()/2 - h/2
case Right:
p.X = r.UR.X - w
p.Y = r.UR.Y - r.Height()/2 - h/2
case BottomLeft:
p.X = r.LL.X
p.Y = r.LL.Y
case BottomCenter:
p.X = r.UR.X - r.Width()/2 - w/2
p.Y = r.LL.Y
case BottomRight:
p.X = r.UR.X - w
p.Y = r.LL.Y
}
return p
}
func boxByDim(boxName string, b *Box, d Dict, parent *Rectangle) *Rectangle {
w := b.Dim.Width
if w < 1 {
w *= parent.Width()
}
h := b.Dim.Height
if h < 1 {
h *= parent.Height()
}
ll := boxLowerLeftCorner(parent, w, h, b.Pos)
r := RectForWidthAndHeight(ll.X+float64(b.Dx), ll.Y+float64(b.Dy), w, h)
d.Update(boxName, r.Array())
return r
}
func applyBox(boxName string, b *Box, d Dict, parent *Rectangle) *Rectangle {
if b.Rect != nil {
d.Update(boxName, b.Rect.Array())
return b.Rect
}