forked from balacode/one-file-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf_test.go
4337 lines (4276 loc) · 124 KB
/
pdf_test.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
// -----------------------------------------------------------------------------
// (c) [email protected] License: MIT
// :v: 2020-06-20 10:07:23 20DE3D one-file-pdf/utest/[pdf_test.go]
// -----------------------------------------------------------------------------
package pdf
// # Public Tests:
// Test_NewPDF_
// Test_PDF_Clean_
// Test_PDF_Color_
// Test_PDF_Compression_
// Test_PDF_CurrentPage_
// Test_PDF_DocAuthor_
// Test_PDF_DocCreator_
// Test_PDF_DocKeywords_
// Test_PDF_DocSubject_
// Test_PDF_DocTitle_
// Test_PDF_DrawBox_
// Test_PDF_DrawCircle_
// Test_PDF_DrawImage_
// Test_PDF_DrawTextAt_
// Test_PDF_DrawTextInBox_
// Test_PDF_DrawText_
// Test_PDF_DrawUnitGrid_
// Test_PDF_Errors_
// Test_PDF_FillBox_
// Test_PDF_FillCircle_
// Test_PDF_FontName_
// Test_PDF_FontSize_
// Test_PDF_HorizontalScaling_
// Test_PDF_LineWidth_
// Test_PDF_PageCount_
// Test_PDF_PageHeight_
// Test_PDF_PageWidth_
// Test_PDF_PullError_
// Test_PDF_Reset_
// Test_PDF_SetFont_
// Test_PDF_SetXY_
// Test_PDF_ToColor_1_
// Test_PDF_ToColor_2_
// Test_PDF_ToPoints_
// Test_PDF_ToUnits_
// Test_PDF_Units_
// Test_PDF_X_
// Test_PDF_Y_
//
// # Internal Tests
// Test_getPapreSize_
//
// # Helper Functions
// callerList() []string
// failIfHasErrors(t *testing.T, errors func() []error)
// floatStr(val float64) string
// formatLines(s string, formatStreams bool) []string
// getStack() string
// mismatch(t *testing.T, tag string, want, got interface{})
// pdfCompare(t *testing.T, got []byte, want string)
// pdfFormatStreams(s string) string
// permuteStrings(parts ...[]string) (ret []string)
// tCaller() string
// tEqual(t *testing.T, got interface{}, want interface{}) bool
// This file contains unit tests for internal methods/functions.
//
// To generate a test coverage report use:
// go test -coverprofile cover.out
// go tool cover -html=cover.out
import (
"bytes"
"fmt"
"image/color"
"os"
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"
"testing"
"time"
)
// to run all tests
// Test_PDF_Clean_ is the unit test for PDF.Clean()
func Test_PDF_Clean_(t *testing.T) {
//
// calling Clean() multiple times on a non-initialized PDF:
// (you should not do this normally, use NewPDF() to create a PDF)
// - should not panic
// - length of Errors() should be zero
// - Errors() should be []error{}, not nil
//
func() {
var doc PDF // uninitialized PDF
doc.Clean().Clean().Clean()
//
// got want
tEqual(t, len(doc.Errors()), 0)
tEqual(t, doc.Errors(), []error{})
}()
// same as above for a PDF properly initialized with NewPDF()
// (also, call Clean() without chaining)
func() {
doc := NewPDF("A4")
doc.Clean()
doc.Clean()
doc.Clean()
// got want
tEqual(t, len(doc.Errors()), 0)
tEqual(t, doc.Errors(), []error{})
}()
// create a new PDF with an unknown page size, then call Clean()
// first, Errors should have 1 error
// after Clean(), Errors should be zero-length
func() {
doc := NewPDF("Parchment")
// got want
tEqual(t, len(doc.Errors()), 1)
doc.Clean()
// got want
tEqual(t, len(doc.Errors()), 0)
tEqual(t, doc.Errors(), []error{})
}()
} // Test_PDF_Clean_
// Test_PDF_Color_ tests PDF.Color() and SetColor()
func Test_PDF_Color_(t *testing.T) {
//
// (ob *PDF) Color() color.RGBA
//
func() {
var doc PDF // uninitialized PDF
tEqual(t, doc.Color(), color.RGBA{A: 255})
}()
func() {
doc := NewPDF("A4") // initialized PDF
tEqual(t, doc.Color(), color.RGBA{A: 255})
}()
//
// (ob *PDF) SetColor(nameOrHTMLColor string) *PDF
//
// test various named colors and codes
for _, test := range []struct {
input string
want color.RGBA
}{
// test HTML color codes
{"#000000", color.RGBA{R: 0, G: 0, B: 0, A: 255}},
{"#010203", color.RGBA{R: 1, G: 2, B: 3, A: 255}},
{"#FFFFFF", color.RGBA{R: 255, G: 255, B: 255, A: 255}},
//
// test all named colors
{"ALICE BLUE", color.RGBA{R: 240, G: 248, B: 255, A: 255}},
{"ANTIQUE WHITE", color.RGBA{R: 250, G: 235, B: 215, A: 255}},
{"AQUA", color.RGBA{R: 000, G: 255, B: 255, A: 255}},
{"AQUAMARINE", color.RGBA{R: 127, G: 255, B: 212, A: 255}},
{"AZURE", color.RGBA{R: 240, G: 255, B: 255, A: 255}},
{"BEIGE", color.RGBA{R: 245, G: 245, B: 220, A: 255}},
{"BISQUE", color.RGBA{R: 255, G: 228, B: 196, A: 255}},
{"BLACK", color.RGBA{R: 000, G: 000, B: 000, A: 255}},
{"BLANCHED ALMOND", color.RGBA{R: 255, G: 235, B: 205, A: 255}},
{"BLUE", color.RGBA{R: 000, G: 000, B: 255, A: 255}},
{"BLUE VIOLET", color.RGBA{R: 138, G: 43, B: 226, A: 255}},
{"BROWN", color.RGBA{R: 165, G: 42, B: 42, A: 255}},
{"BURLYWOOD", color.RGBA{R: 222, G: 184, B: 135, A: 255}},
{"CADET BLUE", color.RGBA{R: 95, G: 158, B: 160, A: 255}},
{"CHARTREUSE", color.RGBA{R: 127, G: 255, B: 000, A: 255}},
{"CHOCOLATE", color.RGBA{R: 210, G: 105, B: 30, A: 255}},
{"CORAL", color.RGBA{R: 255, G: 127, B: 80, A: 255}},
{"CORNFLOWER BLUE", color.RGBA{R: 100, G: 149, B: 237, A: 255}},
{"CORNSILK", color.RGBA{R: 255, G: 248, B: 220, A: 255}},
{"CRIMSON", color.RGBA{R: 220, G: 20, B: 60, A: 255}},
{"CYAN", color.RGBA{R: 000, G: 255, B: 255, A: 255}},
{"DARK BLUE", color.RGBA{R: 000, G: 000, B: 139, A: 255}},
{"DARK CYAN", color.RGBA{R: 000, G: 139, B: 139, A: 255}},
{"DARK GOLDEN ROD", color.RGBA{R: 184, G: 134, B: 11, A: 255}},
{"DARK GRAY", color.RGBA{R: 169, G: 169, B: 169, A: 255}},
{"DARK GREEN", color.RGBA{R: 000, G: 100, B: 000, A: 255}},
{"DARK KHAKI", color.RGBA{R: 189, G: 183, B: 107, A: 255}},
{"DARK MAGENTA", color.RGBA{R: 139, G: 000, B: 139, A: 255}},
{"DARK OLIVE GREEN", color.RGBA{R: 85, G: 107, B: 47, A: 255}},
{"DARK ORANGE", color.RGBA{R: 255, G: 140, B: 000, A: 255}},
{"DARK ORCHID", color.RGBA{R: 153, G: 50, B: 204, A: 255}},
{"DARK RED", color.RGBA{R: 139, G: 000, B: 000, A: 255}},
{"DARK SALMON", color.RGBA{R: 233, G: 150, B: 122, A: 255}},
{"DARK SEA GREEN", color.RGBA{R: 143, G: 188, B: 143, A: 255}},
{"DARK SLATE BLUE", color.RGBA{R: 72, G: 61, B: 139, A: 255}},
{"DARK SLATE GRAY", color.RGBA{R: 47, G: 79, B: 79, A: 255}},
{"DARK TURQUOISE", color.RGBA{R: 000, G: 206, B: 209, A: 255}},
{"DARK VIOLET", color.RGBA{R: 148, G: 000, B: 211, A: 255}},
{"DEEP PINK", color.RGBA{R: 255, G: 20, B: 147, A: 255}},
{"DEEP SKY BLUE", color.RGBA{R: 000, G: 191, B: 255, A: 255}},
{"DIM GRAY", color.RGBA{R: 105, G: 105, B: 105, A: 255}},
{"DODGER BLUE", color.RGBA{R: 30, G: 144, B: 255, A: 255}},
{"FIRE BRICK", color.RGBA{R: 178, G: 34, B: 34, A: 255}},
{"FLORAL WHITE", color.RGBA{R: 255, G: 250, B: 240, A: 255}},
{"FOREST GREEN", color.RGBA{R: 34, G: 139, B: 34, A: 255}},
{"FUCHSIA", color.RGBA{R: 255, G: 000, B: 255, A: 255}},
{"GAINSBORO", color.RGBA{R: 220, G: 220, B: 220, A: 255}},
{"GHOST WHITE", color.RGBA{R: 248, G: 248, B: 255, A: 255}},
{"GOLD", color.RGBA{R: 255, G: 215, B: 000, A: 255}},
{"GOLDEN ROD", color.RGBA{R: 218, G: 165, B: 32, A: 255}},
{"GRAY", color.RGBA{R: 190, G: 190, B: 190, A: 255}},
{"GREEN", color.RGBA{R: 000, G: 255, B: 000, A: 255}},
{"GREEN YELLOW", color.RGBA{R: 173, G: 255, B: 47, A: 255}},
{"HONEY DEW", color.RGBA{R: 240, G: 255, B: 240, A: 255}},
{"HOT PINK", color.RGBA{R: 255, G: 105, B: 180, A: 255}},
{"INDIAN RED", color.RGBA{R: 205, G: 92, B: 92, A: 255}},
{"INDIGO", color.RGBA{R: 75, G: 000, B: 130, A: 255}},
{"IVORY", color.RGBA{R: 255, G: 255, B: 240, A: 255}},
{"KHAKI", color.RGBA{R: 240, G: 230, B: 140, A: 255}},
{"LAVENDER", color.RGBA{R: 230, G: 230, B: 250, A: 255}},
{"LAVENDER BLUSH", color.RGBA{R: 255, G: 240, B: 245, A: 255}},
{"LAWN GREEN", color.RGBA{R: 124, G: 252, B: 000, A: 255}},
{"LEMON CHIFFON", color.RGBA{R: 255, G: 250, B: 205, A: 255}},
{"LIGHT BLUE", color.RGBA{R: 173, G: 216, B: 230, A: 255}},
{"LIGHT CORAL", color.RGBA{R: 240, G: 128, B: 128, A: 255}},
{"LIGHT CYAN", color.RGBA{R: 224, G: 255, B: 255, A: 255}},
{"LIGHT GOLDENROD YELLOW", color.RGBA{R: 250, G: 250, B: 210, A: 255}},
{"LIGHT GRAY", color.RGBA{R: 211, G: 211, B: 211, A: 255}},
{"LIGHT GREEN", color.RGBA{R: 144, G: 238, B: 144, A: 255}},
{"LIGHT PINK", color.RGBA{R: 255, G: 182, B: 193, A: 255}},
{"LIGHT SALMON", color.RGBA{R: 255, G: 160, B: 122, A: 255}},
{"LIGHT SEA GREEN", color.RGBA{R: 32, G: 178, B: 170, A: 255}},
{"LIGHT SKY BLUE", color.RGBA{R: 135, G: 206, B: 250, A: 255}},
{"LIGHT SLATE GRAY", color.RGBA{R: 119, G: 136, B: 153, A: 255}},
{"LIGHT STEEL BLUE", color.RGBA{R: 176, G: 196, B: 222, A: 255}},
{"LIGHT YELLOW", color.RGBA{R: 255, G: 255, B: 224, A: 255}},
{"LIME", color.RGBA{R: 000, G: 255, B: 000, A: 255}},
{"LIME GREEN", color.RGBA{R: 50, G: 205, B: 50, A: 255}},
{"LINEN", color.RGBA{R: 250, G: 240, B: 230, A: 255}},
{"MAGENTA", color.RGBA{R: 255, G: 000, B: 255, A: 255}},
{"MAROON", color.RGBA{R: 176, G: 48, B: 96, A: 255}},
{"MEDIUM AQUAMARINE", color.RGBA{R: 102, G: 205, B: 170, A: 255}},
{"MEDIUM BLUE", color.RGBA{R: 000, G: 000, B: 205, A: 255}},
{"MEDIUM ORCHID", color.RGBA{R: 186, G: 85, B: 211, A: 255}},
{"MEDIUM PURPLE", color.RGBA{R: 147, G: 112, B: 219, A: 255}},
{"MEDIUM SEA GREEN", color.RGBA{R: 60, G: 179, B: 113, A: 255}},
{"MEDIUM SLATE BLUE", color.RGBA{R: 123, G: 104, B: 238, A: 255}},
{"MEDIUM SPRING GREEN", color.RGBA{R: 000, G: 250, B: 154, A: 255}},
{"MEDIUM TURQUOISE", color.RGBA{R: 72, G: 209, B: 204, A: 255}},
{"MEDIUM VIOLET RED", color.RGBA{R: 199, G: 21, B: 133, A: 255}},
{"MIDNIGHT BLUE", color.RGBA{R: 25, G: 25, B: 112, A: 255}},
{"MINT CREAM", color.RGBA{R: 245, G: 255, B: 250, A: 255}},
{"MISTY ROSE", color.RGBA{R: 255, G: 228, B: 225, A: 255}},
{"MOCCASIN", color.RGBA{R: 255, G: 228, B: 181, A: 255}},
{"NAVAJO WHITE", color.RGBA{R: 255, G: 222, B: 173, A: 255}},
{"NAVY", color.RGBA{R: 000, G: 000, B: 128, A: 255}},
{"OLD LACE", color.RGBA{R: 253, G: 245, B: 230, A: 255}},
{"OLIVE", color.RGBA{R: 128, G: 128, B: 000, A: 255}},
{"OLIVE DRAB", color.RGBA{R: 107, G: 142, B: 35, A: 255}},
{"ORANGE", color.RGBA{R: 255, G: 165, B: 000, A: 255}},
{"ORANGE RED", color.RGBA{R: 255, G: 69, B: 000, A: 255}},
{"ORCHID", color.RGBA{R: 218, G: 112, B: 214, A: 255}},
{"PALE GOLDEN ROD", color.RGBA{R: 238, G: 232, B: 170, A: 255}},
{"PALE GREEN", color.RGBA{R: 152, G: 251, B: 152, A: 255}},
{"PALE TURQUOISE", color.RGBA{R: 175, G: 238, B: 238, A: 255}},
{"PALE VIOLET RED", color.RGBA{R: 219, G: 112, B: 147, A: 255}},
{"PAPAYA WHIP", color.RGBA{R: 255, G: 239, B: 213, A: 255}},
{"PEACH PUFF", color.RGBA{R: 255, G: 218, B: 185, A: 255}},
{"PERU", color.RGBA{R: 205, G: 133, B: 63, A: 255}},
{"PINK", color.RGBA{R: 255, G: 192, B: 203, A: 255}},
{"PLUM", color.RGBA{R: 221, G: 160, B: 221, A: 255}},
{"POWDER BLUE", color.RGBA{R: 176, G: 224, B: 230, A: 255}},
{"PURPLE", color.RGBA{R: 160, G: 32, B: 240, A: 255}},
{"REBECCA PURPLE", color.RGBA{R: 102, G: 51, B: 153, A: 255}},
{"RED", color.RGBA{R: 255, G: 000, B: 000, A: 255}},
{"ROSY BROWN", color.RGBA{R: 188, G: 143, B: 143, A: 255}},
{"ROYAL BLUE", color.RGBA{R: 65, G: 105, B: 225, A: 255}},
{"SADDLE BROWN", color.RGBA{R: 139, G: 69, B: 19, A: 255}},
{"SALMON", color.RGBA{R: 250, G: 128, B: 114, A: 255}},
{"SANDY BROWN", color.RGBA{R: 244, G: 164, B: 96, A: 255}},
{"SEA GREEN", color.RGBA{R: 46, G: 139, B: 87, A: 255}},
{"SEASHELL", color.RGBA{R: 255, G: 245, B: 238, A: 255}},
{"SIENNA", color.RGBA{R: 160, G: 82, B: 45, A: 255}},
{"SILVER", color.RGBA{R: 192, G: 192, B: 192, A: 255}},
{"SKY BLUE", color.RGBA{R: 135, G: 206, B: 235, A: 255}},
{"SLATE BLUE", color.RGBA{R: 106, G: 90, B: 205, A: 255}},
{"SLATE GRAY", color.RGBA{R: 112, G: 128, B: 144, A: 255}},
{"SNOW", color.RGBA{R: 255, G: 250, B: 250, A: 255}},
{"SPRING GREEN", color.RGBA{R: 000, G: 255, B: 127, A: 255}},
{"STEEL BLUE", color.RGBA{R: 70, G: 130, B: 180, A: 255}},
{"TAN", color.RGBA{R: 210, G: 180, B: 140, A: 255}},
{"TEAL", color.RGBA{R: 000, G: 128, B: 128, A: 255}},
{"THISTLE", color.RGBA{R: 216, G: 191, B: 216, A: 255}},
{"TOMATO", color.RGBA{R: 255, G: 99, B: 71, A: 255}},
{"TURQUOISE", color.RGBA{R: 64, G: 224, B: 208, A: 255}},
{"VIOLET", color.RGBA{R: 238, G: 130, B: 238, A: 255}},
{"WEB GRAY", color.RGBA{R: 128, G: 128, B: 128, A: 255}},
{"WEB GREEN", color.RGBA{R: 000, G: 128, B: 000, A: 255}},
{"WEB MAROON", color.RGBA{R: 127, G: 000, B: 000, A: 255}},
{"WEB PURPLE", color.RGBA{R: 127, G: 000, B: 127, A: 255}},
{"WHEAT", color.RGBA{R: 245, G: 222, B: 179, A: 255}},
{"WHITE", color.RGBA{R: 255, G: 255, B: 255, A: 255}},
{"WHITE SMOKE", color.RGBA{R: 245, G: 245, B: 245, A: 255}},
{"YELLOW", color.RGBA{R: 255, G: 255, B: 000, A: 255}},
{"YELLOW GREEN", color.RGBA{R: 154, G: 205, B: 50, A: 255}},
} {
for pass := 0; pass < 3; pass++ {
s := test.input
switch pass {
case 0:
// do nothing
case 1:
s = strings.ToLower(s)
case 2:
s = strings.Replace(s, " ", "", -1)
case 3:
s = strings.ToLower(strings.Replace(s, " ", "", -1))
case 4:
s = strings.Replace(s, " ", "-", -1)
case 5:
s = strings.ToLower(strings.Replace(s, " ", "-", -1))
case 6:
s = strings.Replace(s, " ", "_", -1)
case 7:
s = strings.ToLower(strings.Replace(s, " ", "_", -1))
}
doc := NewPDF("A4")
doc2 := doc.SetColor(s)
if doc2 != &doc {
t.Errorf(
`Address of pointer returned by SetColor("%s") is wrong`,
test.input)
}
if doc.Color() != test.want {
t.Errorf(
`After SetColor("%s"), Color() returned %v instead of %v`,
test.input, doc.Color(), test.want)
}
}
}
// test color names with trimming and case insensitivity
for _, name := range permuteStrings(
[]string{"", " ", " "},
[]string{"red", "Red", "RED"},
[]string{"", " ", " "},
) {
doc := NewPDF("A4")
doc2 := doc.SetColor(name)
tEqual(t, doc.Color(), color.RGBA{R: 255, A: 255})
tEqual(t, &doc, doc2)
}
// try setting a blank color name
func() {
var doc PDF // uninitialized PDF
tEqual(t, len(doc.Errors()), 0)
doc.SetColor("")
tEqual(t, len(doc.Errors()), 1)
//
if len(doc.Errors()) == 1 {
tEqual(t,
doc.Errors()[0],
fmt.Errorf(`Unknown color name "" @SetColor`))
}
tEqual(t, doc.Color(), color.RGBA{A: 255})
}()
// try setting an unknown color name
func() {
var doc PDF // uninitialized PDF
tEqual(t, len(doc.Errors()), 0)
doc.SetColor("TheColourOutOfSpace")
tEqual(t, len(doc.Errors()), 1)
//
if len(doc.Errors()) == 1 {
tEqual(t,
doc.Errors()[0],
fmt.Errorf(
`Unknown color name "TheColourOutOfSpace" @SetColor`))
}
tEqual(t, doc.Color(), color.RGBA{A: 255})
}()
//
// SetColorRGB(red, green, blue int) *PDF
//
func() {
// red
a := NewPDF("A4")
b := a.SetColorRGB(128, 0, 0)
tEqual(t, a.Color(), color.RGBA{R: 128, A: 255})
tEqual(t, &a, b)
}()
func() {
// green
a := NewPDF("A4")
b := a.SetColorRGB(0, 128, 0)
tEqual(t, a.Color(), color.RGBA{G: 128, A: 255})
tEqual(t, &a, b)
}()
func() {
// blue
a := NewPDF("A4")
b := a.SetColorRGB(0, 0, 128)
tEqual(t, a.Color(), color.RGBA{B: 128, A: 255})
tEqual(t, &a, b)
}()
} // Test_PDF_Color_
// Test_PDF_Compression_ tests PDF.Compression() and SetCompression()
func Test_PDF_Compression_(t *testing.T) {
draw := func(doc *PDF) {
doc.SetUnits("cm").
SetXY(1, 1).
SetFont("Helvetica", 10).
DrawText("Hello World! Hello World!")
}
func() {
var doc PDF // uninitialized PDF
tEqual(t, doc.Compression(), true)
failIfHasErrors(t, doc.Errors)
}()
func() {
doc := NewPDF("A4") // initialized PDF
tEqual(t, doc.Compression(), true)
failIfHasErrors(t, doc.Errors)
}()
// generate a simple PDF with compression turned on
func() {
const want = `
%PDF-1.4
1 0 obj <</Type/Catalog/Pages 2 0 R>>
endobj
2 0 obj <</Type/Pages/Count 1/MediaBox[0 0 595 841]/Kids[3 0 R]>>
endobj
3 0 obj <</Type/Page/Parent 2 0 R/Contents 4 0 R
/Resources <</Font <</FNT1 5 0 R>> >> >>
endobj
4 0 obj <</Filter/FlateDecode/Length 81>> stream
0A 78 9C 72 0A 51 D0 77 F3 0B 31 54 30 34 50 08
49 53 70 0D E1 52 30 D0 33 30 30 40 21 8B D2 B9
30 05 83 DC B9 9C 42 14 8C 2C 14 2C 0C 8D 15 42
52 14 34 3C 52 73 72 F2 15 C2 F3 8B 72 52 14 15
90 39 9A 0A 21 59 20 93 01 01 00 00 FF FF F6 FE
19 77 0A
endstream
endobj
5 0 obj <</Type/Font/Subtype/Type1/Name/FNT1
/BaseFont/Helvetica
/Encoding/StandardEncoding>>
endobj
xref
0 6
0000000000 65535 f
0000000010 00000 n
0000000056 00000 n
0000000130 00000 n
0000000228 00000 n
0000000378 00000 n
trailer
<</Size 6/Root 1 0 R>>
startxref
479
%%EOF
`
doc := NewPDF("A4") // initialized PDF
doc.SetCompression(true)
draw(&doc)
failIfHasErrors(t, doc.Errors)
pdfCompare(t, doc.Bytes(), want)
}()
// generate a simple PDF with compression turned off
func() {
want := `
%PDF-1.4
1 0 obj <</Type/Catalog/Pages 2 0 R>>
endobj
2 0 obj <</Type/Pages/Count 1/MediaBox[0 0 595 841]/Kids[3 0 R]>>
endobj
3 0 obj <</Type/Page/Parent 2 0 R/Contents 4 0 R
/Resources <</Font <</FNT1 5 0 R>> >> >>
endobj
4 0 obj <</Length 108>> stream
BT /FNT1 10 Tf ET
0.000 0.000 0.000 rg
0.000 0.000 0.000 RG
BT 28 813 Td (Hello World! Hello World!) Tj ET
endstream
endobj
5 0 obj <</Type/Font/Subtype/Type1/Name/FNT1
/BaseFont/Helvetica
/Encoding/StandardEncoding>>
endobj
xref
0 6
0000000000 65535 f
0000000010 00000 n
0000000056 00000 n
0000000130 00000 n
0000000228 00000 n
0000000387 00000 n
trailer
<</Size 6/Root 1 0 R>>
startxref
488
%%EOF
`
doc := NewPDF("A4") // initialized PDF
doc.SetCompression(false)
draw(&doc)
failIfHasErrors(t, doc.Errors)
pdfCompare(t, doc.Bytes(), want)
}()
} // Test_PDF_Compression_
// Test_PDF_CurrentPage_ tests PDF.CurrentPage()
func Test_PDF_CurrentPage_(t *testing.T) {
func() {
var doc PDF // uninitialized PDF
//
// before calling AddPage(), returns 1
tEqual(t, doc.CurrentPage(), 1)
//
// since AddPage() is called without any drawing method,
// the page is added implicitly: therefore still on page 1
doc.AddPage()
tEqual(t, doc.CurrentPage(), 1)
//
// the next call to AddPage(), returns 2, and so on
doc.AddPage()
tEqual(t, doc.CurrentPage(), 2)
//
doc.AddPage()
tEqual(t, doc.CurrentPage(), 3)
//
doc.AddPage()
tEqual(t, doc.CurrentPage(), 4)
}()
func() {
doc := NewPDF("LETTER")
//
// before calling AddPage(), returns 1
tEqual(t, doc.CurrentPage(), 1)
//
// since AddPage() is called without any drawing method,
// the page is added implicitly: therefore still on page 1
doc.AddPage()
tEqual(t, doc.CurrentPage(), 1)
//
// the next call to AddPage(), returns 2, and so on
doc.AddPage()
tEqual(t, doc.CurrentPage(), 2)
//
doc.AddPage()
tEqual(t, doc.CurrentPage(), 3)
//
doc.AddPage()
tEqual(t, doc.CurrentPage(), 4)
}()
} // Test_PDF_CurrentPage_
// Test_PDF_DocAuthor_ is the unit test for
func Test_PDF_DocAuthor_(t *testing.T) {
//
// (ob *PDF) DocAuthor() string
//
func() {
var doc PDF // uninitialized PDF
tEqual(t, doc.DocAuthor(), "")
}()
func() {
doc := NewPDF("A4") // initialized PDF
tEqual(t, doc.DocAuthor(), "")
}()
//
// (ob *PDF) SetDocAuthor(s string) *PDF
//
func() {
var doc PDF // uninitialized PDF
tEqual(t, doc.SetDocAuthor("Abcdefg").DocAuthor(), "Abcdefg")
}()
func() {
doc := NewPDF("A4") // initialized PDF
tEqual(t, doc.SetDocAuthor("Abcdefg").DocAuthor(), "Abcdefg")
}()
//
// Test PDF generation
//
func() {
doc := NewPDF("A4") // initialized PDF
doc.SetCompression(false).SetDocAuthor("'Author' metadata entry")
const want = `
%PDF-1.4
1 0 obj <</Type/Catalog/Pages 2 0 R>>
endobj
2 0 obj <</Type/Pages/Count 1/MediaBox[0 0 595 841]/Kids[3 0 R]>>
endobj
3 0 obj <</Type/Page/Parent 2 0 R/Contents 4 0 R>>
endobj
4 0 obj <</Length 0>> stream
endstream
endobj
5 0 obj <</Type/Info/Author ('Author' metadata entry)>>
endobj
xref
0 6
0000000000 65535 f
0000000010 00000 n
0000000056 00000 n
0000000130 00000 n
0000000189 00000 n
0000000238 00000 n
trailer
<</Size 6/Root 1 0 R/Info 5 0 R>>
startxref
302
%%EOF
`
pdfCompare(t, doc.Bytes(), want)
}()
} // Test_PDF_DocAuthor_
// Test_PDF_DocCreator_ is the unit test for
func Test_PDF_DocCreator_(t *testing.T) {
//
// (ob *PDF) DocCreator() string
//
func() {
var doc PDF // uninitialized PDF
tEqual(t, doc.DocCreator(), "")
}()
func() {
doc := NewPDF("A4") // initialized PDF
tEqual(t, doc.DocCreator(), "")
}()
//
// (ob *PDF) SetDocCreator(s string) *PDF
//
func() {
var doc PDF // uninitialized PDF
tEqual(t, doc.SetDocCreator("Abcdefg").DocCreator(), "Abcdefg")
}()
func() {
doc := NewPDF("A4") // initialized PDF
tEqual(t, doc.SetDocCreator("Abcdefg").DocCreator(), "Abcdefg")
}()
//
// Test PDF generation
//
func() {
doc := NewPDF("A4") // initialized PDF
doc.SetCompression(false).SetDocCreator("'Creator' metadata entry")
const want = `
%PDF-1.4
1 0 obj <</Type/Catalog/Pages 2 0 R>>
endobj
2 0 obj <</Type/Pages/Count 1/MediaBox[0 0 595 841]/Kids[3 0 R]>>
endobj
3 0 obj <</Type/Page/Parent 2 0 R/Contents 4 0 R>>
endobj
4 0 obj <</Length 0>> stream
endstream
endobj
5 0 obj <</Type/Info/Creator ('Creator' metadata entry)>>
endobj
xref
0 6
0000000000 65535 f
0000000010 00000 n
0000000056 00000 n
0000000130 00000 n
0000000189 00000 n
0000000238 00000 n
trailer
<</Size 6/Root 1 0 R/Info 5 0 R>>
startxref
304
%%EOF
`
pdfCompare(t, doc.Bytes(), want)
}()
} // Test_PDF_DocCreator_
// Test_PDF_DocKeywords_ is the unit test for
func Test_PDF_DocKeywords_(t *testing.T) {
//
// (ob *PDF) DocKeywords() string
//
func() {
var doc PDF // uninitialized PDF
tEqual(t, doc.DocKeywords(), "")
}()
func() {
doc := NewPDF("A4") // initialized PDF
tEqual(t, doc.DocKeywords(), "")
}()
//
// (ob *PDF) SetDocKeywords(s string) *PDF
//
func() {
var doc PDF // uninitialized PDF
tEqual(t, doc.SetDocKeywords("Abcdefg").DocKeywords(), "Abcdefg")
}()
func() {
doc := NewPDF("A4") // initialized PDF
tEqual(t, doc.SetDocKeywords("Abcdefg").DocKeywords(), "Abcdefg")
}()
//
// Test PDF generation
//
func() {
doc := NewPDF("A4") // initialized PDF
doc.SetCompression(false).SetDocKeywords("'Keywords' metadata entry")
const want = `
%PDF-1.4
1 0 obj <</Type/Catalog/Pages 2 0 R>>
endobj
2 0 obj <</Type/Pages/Count 1/MediaBox[0 0 595 841]/Kids[3 0 R]>>
endobj
3 0 obj <</Type/Page/Parent 2 0 R/Contents 4 0 R>>
endobj
4 0 obj <</Length 0>> stream
endstream
endobj
5 0 obj <</Type/Info/Keywords ('Keywords' metadata entry)>>
endobj
xref
0 6
0000000000 65535 f
0000000010 00000 n
0000000056 00000 n
0000000130 00000 n
0000000189 00000 n
0000000238 00000 n
trailer
<</Size 6/Root 1 0 R/Info 5 0 R>>
startxref
306
%%EOF
`
pdfCompare(t, doc.Bytes(), want)
}()
} // Test_PDF_DocKeywords_
// Test_PDF_DocSubject_ is the unit test for
func Test_PDF_DocSubject_(t *testing.T) {
//
// (ob *PDF) DocSubject() string
//
func() {
var doc PDF // uninitialized PDF
tEqual(t, doc.DocSubject(), "")
}()
func() {
doc := NewPDF("A4") // initialized PDF
tEqual(t, doc.DocSubject(), "")
}()
//
// (ob *PDF) SetDocSubject(s string) *PDF
//
func() {
var doc PDF // uninitialized PDF
tEqual(t, doc.SetDocSubject("Abcdefg").DocSubject(), "Abcdefg")
}()
func() {
doc := NewPDF("A4") // initialized PDF
tEqual(t, doc.SetDocSubject("Abcdefg").DocSubject(), "Abcdefg")
}()
//
// Test PDF generation
//
func() {
doc := NewPDF("A4") // initialized PDF
doc.SetCompression(false).SetDocSubject("'Subject' metadata entry")
const want = `
%PDF-1.4
1 0 obj <</Type/Catalog/Pages 2 0 R>>
endobj
2 0 obj <</Type/Pages/Count 1/MediaBox[0 0 595 841]/Kids[3 0 R]>>
endobj
3 0 obj <</Type/Page/Parent 2 0 R/Contents 4 0 R>>
endobj
4 0 obj <</Length 0>> stream
endstream
endobj
5 0 obj <</Type/Info/Subject ('Subject' metadata entry)>>
endobj
xref
0 6
0000000000 65535 f
0000000010 00000 n
0000000056 00000 n
0000000130 00000 n
0000000189 00000 n
0000000238 00000 n
trailer
<</Size 6/Root 1 0 R/Info 5 0 R>>
startxref
304
%%EOF
`
pdfCompare(t, doc.Bytes(), want)
}()
} // Test_PDF_DocSubject_
// Test_PDF_DocTitle_ is the unit test for
func Test_PDF_DocTitle_(t *testing.T) {
//
// (ob *PDF) DocTitle() string
//
func() {
var doc PDF // uninitialized PDF
tEqual(t, doc.DocTitle(), "")
}()
func() {
doc := NewPDF("A4") // initialized PDF
tEqual(t, doc.DocTitle(), "")
}()
//
// (ob *PDF) SetDocTitle(s string) *PDF
//
func() {
var doc PDF // uninitialized PDF
tEqual(t, doc.SetDocTitle("Abcdefg").DocTitle(), "Abcdefg")
}()
func() {
doc := NewPDF("A4") // initialized PDF
tEqual(t, doc.SetDocTitle("Abcdefg").DocTitle(), "Abcdefg")
}()
//
// Test PDF generation
//
func() {
doc := NewPDF("A4")
doc.SetCompression(false).SetDocTitle("'Title' metadata entry")
const want = `
%PDF-1.4
1 0 obj <</Type/Catalog/Pages 2 0 R>>
endobj
2 0 obj <</Type/Pages/Count 1/MediaBox[0 0 595 841]/Kids[3 0 R]>>
endobj
3 0 obj <</Type/Page/Parent 2 0 R/Contents 4 0 R>>
endobj
4 0 obj <</Length 0>> stream
endstream
endobj
5 0 obj <</Type/Info/Title ('Title' metadata entry)>>
endobj
xref
0 6
0000000000 65535 f
0000000010 00000 n
0000000056 00000 n
0000000130 00000 n
0000000189 00000 n
0000000238 00000 n
trailer
<</Size 6/Root 1 0 R/Info 5 0 R>>
startxref
300
%%EOF
`
pdfCompare(t, doc.Bytes(), want)
}()
} // Test_PDF_DocTitle_
// Test_PDF_DrawBox_ is the unit test for
// PDF.DrawBox(x, y, width, height float64, fill ...bool) *PDF
//
// Runs the test by drawing three rectangles and one filled rectangle
func Test_PDF_DrawBox_(t *testing.T) {
var (
doc = NewPDF("18cm x 18cm")
x = 1.0
y = 1.0
)
{
doc.SetCompression(false).
SetUnits("cm").
SetLineWidth(5).
SetColor("Black").DrawBox(x, y, 1, 1, true). // fill
SetColor("Red").DrawBox(x, y, 4, 4).
SetColor("DarkGreen").DrawBox(x, y, 9, 9).
SetColor("Blue").DrawBox(x, y, 16, 16)
}
const want = `
%PDF-1.4
1 0 obj <</Type/Catalog/Pages 2 0 R>>
endobj
2 0 obj <</Type/Pages/Count 1/MediaBox[0 0 510 510]/Kids[3 0 R]>>
endobj
3 0 obj <</Type/Page/Parent 2 0 R/Contents 4 0 R>>
endobj
4 0 obj <</Length 255>> stream
0.000 0.000 0.000 rg
0.000 0.000 0.000 RG
5.000 w
28.346 453.543 28.346 28.346 re b
1.000 0.000 0.000 RG
28.346 368.504 113.386 113.386 re S
0.000 0.392 0.000 RG
28.346 226.772 255.118 255.118 re S
0.000 0.000 1.000 RG
28.346 28.346 453.543 453.543 re S
endstream
endobj
xref
0 5
0000000000 65535 f
0000000010 00000 n
0000000056 00000 n
0000000130 00000 n
0000000189 00000 n
trailer
<</Size 5/Root 1 0 R>>
startxref
495
%%EOF
`
pdfCompare(t, doc.Bytes(), want)
} // Test_PDF_DrawBox_
// Test_PDF_DrawCircle_ is the unit test for
// PDF.DrawCircle(x, y, radius float64, fill ...bool) *PDF
//
// Runs the test by drawing three concentric
// circles and one small filled circle
func Test_PDF_DrawCircle_(t *testing.T) {
var (
doc = NewPDF("20cm x 20cm")
x, y = 10.0, 10.0 // center of page
)
{
doc.SetCompression(false).
SetUnits("cm").
SetLineWidth(5).
SetColor("Black").DrawCircle(x, y, 0.5, true). // fill
SetColor("Red").DrawCircle(x, y, 2).
SetColor("DarkGreen").DrawCircle(x, y, 4.5).
SetColor("Blue").DrawCircle(x, y, 8.5)
}
const want = `
%PDF-1.4
1 0 obj <</Type/Catalog/Pages 2 0 R>>
endobj
2 0 obj <</Type/Pages/Count 1/MediaBox[0 0 566 566]/Kids[3 0 R]>>
endobj
3 0 obj <</Type/Page/Parent 2 0 R/Contents 4 0 R>>
endobj
4 0 obj <</Length 1003>> stream
0.000 0.000 0.000 rg
0.000 0.000 0.000 RG
5.000 w
269.291 283.465 m
269.291 291.292 275.637 297.638 283.465 297.638 c
291.292 297.638 297.638 291.292 297.638 283.465 c
297.638 275.637 291.292 269.291 283.465 269.291 c
275.637 269.291 269.291 275.637 269.291 283.465 c
b
1.000 0.000 0.000 RG
226.772 283.465 m
226.772 314.775 252.154 340.157 283.465 340.157 c
314.775 340.157 340.157 314.775 340.157 283.465 c
340.157 252.154 314.775 226.772 283.465 226.772 c
252.154 226.772 226.772 252.154 226.772 283.465 c
S
0.000 0.392 0.000 RG
155.906 283.465 m
155.906 353.913 213.016 411.024 283.465 411.024 c
353.913 411.024 411.024 353.913 411.024 283.465 c
411.024 213.016 353.913 155.906 283.465 155.906 c
213.016 155.906 155.906 213.016 155.906 283.465 c
S
0.000 0.000 1.000 RG
42.520 283.465 m
42.520 416.535 150.394 524.409 283.465 524.409 c
416.535 524.409 524.409 416.535 524.409 283.465 c
524.409 150.394 416.535 42.520 283.465 42.520 c
150.394 42.520 42.520 150.394 42.520 283.465 c
S
endstream
endobj
xref
0 5
0000000000 65535 f
0000000010 00000 n
0000000056 00000 n
0000000130 00000 n
0000000189 00000 n
trailer
<</Size 5/Root 1 0 R>>
startxref
1244
%%EOF
`
pdfCompare(t, doc.Bytes(), want)
} // Test_PDF_DrawCircle_
// Test_PDF_DrawImage_ is the unit test for
// PDF.DrawImage(x, y, height float64, fileNameOrBytes interface{},
// backColor ...string) *PDF
//
// Runs the test by drawing rgbw64.png:
// a small 64 x 64 PNG split into pure red, green, blue
// and transparent gradient squares
func Test_PDF_DrawImage_(t *testing.T) {
var (
x = 5.0