-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
writer.go
1177 lines (1086 loc) · 31 KB
/
writer.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
package pdf
import (
"bytes"
"compress/zlib"
"encoding/ascii85"
"encoding/binary"
"fmt"
"image"
"io"
"math"
"reflect"
"sort"
"strings"
"time"
"github.com/tdewolff/canvas"
canvasText "github.com/tdewolff/canvas/text"
canvasFont "github.com/tdewolff/font"
)
// TODO: Invalid graphics transparency, Group has a transparency S entry or the S entry is null
// TODO: Invalid Color space, The operator "g" can't be used without Color Profile
type pdfWriter struct {
w io.Writer
err error
pos int
objOffsets []int
pages []pdfRef
page *pdfPageWriter
fontSubset map[*canvas.Font]*canvas.FontSubsetter
fontsH map[*canvas.Font]pdfRef
fontsV map[*canvas.Font]pdfRef
compress bool
subset bool
title string
subject string
keywords string
author string
creator string
}
func newPDFWriter(writer io.Writer) *pdfWriter {
w := &pdfWriter{
w: writer,
objOffsets: []int{0, 0, 0}, // catalog, metadata, page tree
fontSubset: map[*canvas.Font]*canvas.FontSubsetter{},
fontsH: map[*canvas.Font]pdfRef{},
fontsV: map[*canvas.Font]pdfRef{},
compress: true,
subset: true,
}
w.write("%%PDF-1.7\n%%Ŧǟċơ\n")
return w
}
// SetCompression enable the compression of the streams.
func (w *pdfWriter) SetCompression(compress bool) {
w.compress = compress
}
// SeFontSubsetting enables the subsetting of embedded fonts.
func (w *pdfWriter) SetFontSubsetting(subset bool) {
w.subset = subset
}
// SetTitle sets the document's title.
func (w *pdfWriter) SetTitle(title string) {
w.title = title
}
// SetSubject sets the document's subject.
func (w *pdfWriter) SetSubject(subject string) {
w.subject = subject
}
// SetKeywords sets the document's keywords.
func (w *pdfWriter) SetKeywords(keywords string) {
w.keywords = keywords
}
// SetAuthor sets the document's author.
func (w *pdfWriter) SetAuthor(author string) {
w.author = author
}
// SetCreator sets the document's creator.
func (w *pdfWriter) SetCreator(creator string) {
w.creator = creator
}
func (w *pdfWriter) writeBytes(b []byte) {
if w.err != nil {
return
}
n, err := w.w.Write(b)
w.pos += n
w.err = err
}
func (w *pdfWriter) write(s string, v ...interface{}) {
if w.err != nil {
return
}
n, err := fmt.Fprintf(w.w, s, v...)
w.pos += n
w.err = err
}
type pdfRef int
type pdfName string
type pdfArray []interface{}
type pdfDict map[pdfName]interface{}
type pdfFilter string
type pdfStream struct {
dict pdfDict
stream []byte
}
const (
pdfFilterASCII85 pdfFilter = "ASCII85Decode"
pdfFilterFlate pdfFilter = "FlateDecode"
)
func (w *pdfWriter) writeVal(i interface{}) {
switch v := i.(type) {
case bool:
if v {
w.write("true")
} else {
w.write("false")
}
case int:
w.write("%d", v)
case float64:
w.write("%v", dec(v))
case string:
v = strings.Replace(v, `\`, `\\`, -1)
v = strings.Replace(v, `(`, `\(`, -1)
v = strings.Replace(v, `)`, `\)`, -1)
w.write("(%v)", v)
case pdfRef:
w.write("%v 0 R", v)
case pdfName, pdfFilter:
w.write("/%v", v)
case pdfArray:
w.write("[")
for j, val := range v {
if j != 0 {
w.write(" ")
}
w.writeVal(val)
}
w.write("]")
case pdfDict:
w.write("<< ")
if val, ok := v["Type"]; ok {
w.write("/Type ")
w.writeVal(val)
w.write(" ")
}
if val, ok := v["Subtype"]; ok {
w.write("/Subtype ")
w.writeVal(val)
w.write(" ")
}
keys := []string{}
for key := range v {
if key != "Type" && key != "Subtype" {
keys = append(keys, string(key))
}
}
sort.Strings(keys)
for _, key := range keys {
w.writeVal(pdfName(key))
w.write(" ")
w.writeVal(v[pdfName(key)])
w.write(" ")
}
w.write(">>")
case pdfStream:
if v.dict == nil {
v.dict = pdfDict{}
}
filters := []pdfFilter{}
if filter, ok := v.dict["Filter"].(pdfFilter); ok {
filters = append(filters, filter)
} else if filterArray, ok := v.dict["Filter"].(pdfArray); ok {
for i := len(filterArray) - 1; i >= 0; i-- {
if filter, ok := filterArray[i].(pdfFilter); ok {
filters = append(filters, filter)
}
}
}
b := v.stream
for _, filter := range filters {
var b2 bytes.Buffer
switch filter {
case pdfFilterASCII85:
w := ascii85.NewEncoder(&b2)
w.Write(b)
w.Close()
fmt.Fprintf(&b2, "~>")
case pdfFilterFlate:
w := zlib.NewWriter(&b2)
w.Write(b)
w.Close()
}
b = b2.Bytes()
}
v.dict["Length"] = len(b)
w.writeVal(v.dict)
w.write(" stream\n")
w.writeBytes(b)
w.write("\nendstream")
default:
panic(fmt.Sprintf("unknown PDF type %T", i))
}
}
func (w *pdfWriter) writeObject(val interface{}) pdfRef {
w.objOffsets = append(w.objOffsets, w.pos)
w.write("%v 0 obj\n", len(w.objOffsets))
w.writeVal(val)
w.write("\nendobj\n")
return pdfRef(len(w.objOffsets))
}
func (w *pdfWriter) getFont(font *canvas.Font, vertical bool) pdfRef {
fonts := w.fontsH
if vertical {
fonts = w.fontsV
}
if ref, ok := fonts[font]; ok {
return ref
}
w.objOffsets = append(w.objOffsets, 0)
ref := pdfRef(len(w.objOffsets))
fonts[font] = ref
w.fontSubset[font] = canvas.NewFontSubsetter()
return ref
}
func (w *pdfWriter) writeFont(ref pdfRef, font *canvas.Font, vertical bool) {
// subset the font, we only write the used characters to the PDF CMap object to reduce its
// length. At the end of the function we add a CID to GID mapping to correctly select the
// right glyphID.
sfnt := font.SFNT
glyphIDs := w.fontSubset[font].List() // also when not subsetting, to minimize cmap table
if w.subset {
if sfnt.IsCFF && sfnt.CFF != nil {
sfnt.CFF.SetGlyphNames(nil)
}
sfntSubset, err := sfnt.Subset(glyphIDs, canvasFont.SubsetOptions{Tables: canvasFont.KeepPDFTables})
if err == nil {
// TODO: report error?
sfnt = sfntSubset
}
}
fontProgram := sfnt.Write()
// calculate the character widths for the W array and shorten it
f := 1000.0 / float64(font.SFNT.Head.UnitsPerEm)
widths := make([]int, len(glyphIDs)+1)
for subsetGlyphID, glyphID := range glyphIDs {
widths[subsetGlyphID] = int(f*float64(font.SFNT.GlyphAdvance(glyphID)) + 0.5)
}
DW := widths[0]
W := pdfArray{}
i, j := 1, 1
for k, width := range widths {
if k != 0 && width != widths[j] {
if 4 < k-j { // at about 5 equal widths, it would be shorter using the other notation format
if i < j {
arr := pdfArray{}
for _, w := range widths[i:j] {
arr = append(arr, w)
}
W = append(W, i, arr)
}
if widths[j] != DW {
W = append(W, j, k-1, widths[j])
}
i = k
}
j = k
}
}
if i < len(widths) {
arr := pdfArray{}
for _, w := range widths[i:] {
arr = append(arr, w)
}
W = append(W, i, arr)
}
// create ToUnicode CMap
var bfRange, bfChar strings.Builder
var bfRangeCount, bfCharCount int
startGlyphID := uint16(0)
startUnicode := uint32('\uFFFD')
length := uint16(1)
for subsetGlyphID, glyphID := range glyphIDs[1:] {
unicode := uint32(font.SFNT.Cmap.ToUnicode(glyphID))
if 0x010000 <= unicode && unicode <= 0x10FFFF {
// UTF-16 surrogates
unicode -= 0x10000
unicode = (0xD800+(unicode>>10)&0x3FF)<<16 + 0xDC00 + unicode&0x3FF
}
if uint16(subsetGlyphID+1) == startGlyphID+length && unicode == startUnicode+uint32(length) {
length++
} else {
if 1 < length {
fmt.Fprintf(&bfRange, "<%04X> <%04X> <%04X>\n", startGlyphID, startGlyphID+length-1, startUnicode)
} else {
fmt.Fprintf(&bfChar, "<%04X> <%04X>\n", startGlyphID, startUnicode)
}
startGlyphID = uint16(subsetGlyphID + 1)
startUnicode = unicode
length = 1
}
}
if 1 < length {
fmt.Fprintf(&bfRange, "<%04X> <%04X> <%04X>\n", startGlyphID, startGlyphID+length-1, startUnicode)
} else {
fmt.Fprintf(&bfChar, "<%04X> <%04X>\n", startGlyphID, startUnicode)
}
toUnicode := fmt.Sprintf(`/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo
<< /Registry (Adobe)
/Ordering (UCS)
/Supplement 0
>> def
/CMapName /Adobe-Identity-UCS def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
%d beginbfrange
%sendbfrange
%d beginbfchar
%sendbfchar
endcmap
CMapName currentdict /CMap defineresource pop
end
end`, bfRangeCount, bfRange.String(), bfCharCount, bfChar.String())
toUnicodeStream := pdfStream{
dict: pdfDict{},
stream: []byte(toUnicode),
}
if w.compress {
toUnicodeStream.dict["Filter"] = pdfFilterFlate
}
toUnicodeRef := w.writeObject(toUnicodeStream)
// write font program
fontfileRef := w.writeObject(pdfStream{
dict: pdfDict{
"Subtype": pdfName("OpenType"),
"Filter": pdfFilterFlate,
},
stream: fontProgram,
})
// get name and CID subtype
name := font.Name()
if records := font.SFNT.Name.Get(canvasFont.NamePostScript); 0 < len(records) {
name = records[0].String()
}
baseFont := strings.ReplaceAll(name, " ", "")
if w.subset {
baseFont = "SUBSET+" + baseFont // TODO: give unique subset name
}
encoding := "Identity-H"
if vertical {
encoding = "Identity-V"
}
cidSubtype := ""
if font.SFNT.IsTrueType {
cidSubtype = "CIDFontType2"
} else if font.SFNT.IsCFF {
cidSubtype = "CIDFontType0"
}
// in order to support more than 256 characters, we need to use a CIDFont dictionary which must be inside a Type0 font. Character codes in the stream are glyph IDs, however for subsetted fonts they are the _old_ glyph IDs, which is why we need the CIDToGIDMap
dict := pdfDict{
"Type": pdfName("Font"),
"Subtype": pdfName("Type0"),
"BaseFont": pdfName(baseFont),
"Encoding": pdfName(encoding), // map character codes in the stream to CID with identity encoding, we additionally map CID to GID in the descendant font when subsetting, otherwise that is also identity
"ToUnicode": toUnicodeRef,
"DescendantFonts": pdfArray{pdfDict{
"Type": pdfName("Font"),
"Subtype": pdfName(cidSubtype),
"BaseFont": pdfName(baseFont),
"DW": DW,
"W": W,
"CIDToGIDMap": pdfName("Identity"),
"CIDSystemInfo": pdfDict{
"Registry": "Adobe",
"Ordering": "Identity",
"Supplement": 0,
},
"FontDescriptor": pdfDict{
"Type": pdfName("FontDescriptor"),
"FontName": pdfName(baseFont),
"Flags": 4, // Symbolic
"FontBBox": pdfArray{
int(f * float64(font.SFNT.Head.XMin)),
int(f * float64(font.SFNT.Head.YMin)),
int(f * float64(font.SFNT.Head.XMax)),
int(f * float64(font.SFNT.Head.YMax)),
},
"ItalicAngle": float64(font.SFNT.Post.ItalicAngle),
"Ascent": int(f * float64(font.SFNT.Hhea.Ascender)),
"Descent": -int(f * float64(font.SFNT.Hhea.Descender)),
"CapHeight": int(f * float64(font.SFNT.OS2.SCapHeight)),
"StemV": 80, // taken from Inkscape, should be calculated somehow, maybe use: 10+220*(usWeightClass-50)/900
"FontFile3": fontfileRef,
},
}},
}
if !w.subset {
cidToGIDMap := make([]byte, 2*len(glyphIDs))
for subsetGlyphID, glyphID := range glyphIDs {
j := int(subsetGlyphID) * 2
cidToGIDMap[j+0] = byte((glyphID & 0xFF00) >> 8)
cidToGIDMap[j+1] = byte(glyphID & 0x00FF)
}
cidToGIDMapStream := pdfStream{
dict: pdfDict{},
stream: cidToGIDMap,
}
if w.compress {
cidToGIDMapStream.dict["Filter"] = pdfFilterFlate
}
cidToGIDMapRef := w.writeObject(cidToGIDMapStream)
dict["DescendantFonts"].(pdfArray)[0].(pdfDict)["CIDToGIDMap"] = cidToGIDMapRef
}
w.objOffsets[ref-1] = w.pos
w.write("%v 0 obj\n", ref)
w.writeVal(dict)
w.write("\nendobj\n")
}
func (w *pdfWriter) writeFonts(fontMap map[*canvas.Font]pdfRef, vertical bool) {
// sort fonts by ref to make PDF deterministic
refs := make([]pdfRef, 0, len(fontMap))
refMap := make(map[pdfRef]*canvas.Font, len(fontMap))
for font, ref := range fontMap {
refs = append(refs, ref)
refMap[ref] = font
}
sort.Slice(refs, func(i, j int) bool {
return refs[i] < refs[j]
})
for _, ref := range refs {
w.writeFont(ref, refMap[ref], vertical)
}
}
// Close finished the document.
func (w *pdfWriter) Close() error {
// TODO: support cross reference table streams and compressed objects for all dicts
if w.page != nil {
w.pages = append(w.pages, w.page.writePage(pdfRef(3)))
}
kids := pdfArray{}
for _, page := range w.pages {
kids = append(kids, page)
}
// write fonts
w.writeFonts(w.fontsH, false)
w.writeFonts(w.fontsV, false)
// document catalog
w.objOffsets[0] = w.pos
w.write("%v 0 obj\n", 1)
w.writeVal(pdfDict{
"Type": pdfName("Catalog"),
"Pages": pdfRef(3),
// TODO: add metadata?
})
w.write("\nendobj\n")
// metadata
info := pdfDict{
"Producer": "tdewolff/canvas",
"CreationDate": time.Now().Format("D:20060102150405Z0700"),
}
if w.title != "" {
info["Title"] = w.title
}
if w.subject != "" {
info["Subject"] = w.subject
}
if w.keywords != "" {
info["Keywords"] = w.keywords
}
if w.author != "" {
info["Author"] = w.author
}
if w.creator != "" {
info["Creator"] = w.creator
}
w.objOffsets[1] = w.pos
w.write("%v 0 obj\n", 2)
w.writeVal(info)
w.write("\nendobj\n")
// page tree
w.objOffsets[2] = w.pos
w.write("%v 0 obj\n", 3)
w.writeVal(pdfDict{
"Type": pdfName("Pages"),
"Kids": pdfArray(kids),
"Count": len(kids),
})
w.write("\nendobj\n")
xrefOffset := w.pos
w.write("xref\n0 %d\n0000000000 65535 f \n", len(w.objOffsets)+1)
for _, objOffset := range w.objOffsets {
w.write("%010d 00000 n \n", objOffset)
}
w.write("trailer\n")
w.writeVal(pdfDict{
"Root": pdfRef(1),
"Size": len(w.objOffsets) + 1,
"Info": pdfRef(2),
// TODO: write document ID
})
w.write("\nstartxref\n%v\n%%%%EOF\n", xrefOffset)
return w.err
}
type pdfPageWriter struct {
*bytes.Buffer
pdf *pdfWriter
width, height float64
resources pdfDict
annots pdfArray
graphicsStates map[float64]pdfName
alpha float64
fill canvas.Paint
stroke canvas.Paint
lineWidth float64
lineCap int
lineJoin int
miterLimit float64
dashes []float64
font *canvas.Font
fontSize float64
fontDirection canvasText.Direction
inTextObject bool
textPosition canvas.Matrix
textCharSpace float64
textRenderMode int
}
// NewPage starts a new page.
func (w *pdfWriter) NewPage(width, height float64) *pdfPageWriter {
if w.page != nil {
w.pages = append(w.pages, w.page.writePage(pdfRef(3)))
}
// for defaults see https://help.adobe.com/pdfl_sdk/15/PDFL_SDK_HTMLHelp/PDFL_SDK_HTMLHelp/API_References/PDFL_API_Reference/PDFEdit_Layer/General.html#_t_PDEGraphicState
w.page = &pdfPageWriter{
Buffer: &bytes.Buffer{},
pdf: w,
width: width,
height: height,
resources: pdfDict{},
graphicsStates: map[float64]pdfName{},
alpha: 1.0,
fill: canvas.Paint{Color: canvas.Black},
stroke: canvas.Paint{Color: canvas.Black},
lineWidth: 1.0,
lineCap: 0,
lineJoin: 0,
miterLimit: 10.0,
dashes: []float64{0.0}, // dashArray and dashPhase
font: nil,
fontSize: 0.0,
fontDirection: canvasText.LeftToRight,
inTextObject: false,
textPosition: canvas.Identity,
textCharSpace: 0.0,
textRenderMode: 0,
}
m := canvas.Identity.Scale(ptPerMm, ptPerMm)
fmt.Fprintf(w.page, " %v %v %v %v %v %v cm", dec(m[0][0]), dec(m[1][0]), dec(m[0][1]), dec(m[1][1]), dec(m[0][2]), dec(m[1][2]))
return w.page
}
func (w *pdfPageWriter) writePage(parent pdfRef) pdfRef {
b := w.Bytes()
if 0 < len(b) && b[0] == ' ' {
b = b[1:]
}
stream := pdfStream{
dict: pdfDict{},
stream: b,
}
if w.pdf.compress {
stream.dict["Filter"] = pdfFilterFlate
}
contents := w.pdf.writeObject(stream)
page := pdfDict{
"Type": pdfName("Page"),
"Parent": parent,
"MediaBox": pdfArray{0.0, 0.0, w.width * ptPerMm, w.height * ptPerMm},
"Resources": w.resources,
"Group": pdfDict{
"Type": pdfName("Group"),
"S": pdfName("Transparency"),
"I": true,
"CS": pdfName("DeviceRGB"),
},
"Contents": contents,
}
if 0 < len(w.annots) {
page["Annots"] = w.annots
}
return w.pdf.writeObject(page)
}
// AddAnnotation adds an annotation.
func (w *pdfPageWriter) AddURIAction(uri string, rect canvas.Rect) {
annot := pdfDict{
"Type": pdfName("Annot"),
"Subtype": pdfName("Link"),
"Border": pdfArray{0, 0, 0},
"Rect": pdfArray{rect.X * ptPerMm, rect.Y * ptPerMm, (rect.X + rect.W) * ptPerMm, (rect.Y + rect.H) * ptPerMm},
"Contents": uri,
"A": pdfDict{
"S": pdfName("URI"),
"URI": uri,
},
}
w.annots = append(w.annots, annot)
}
// SetAlpha sets the transparency value.
func (w *pdfPageWriter) SetAlpha(alpha float64) {
if alpha != w.alpha {
gs := w.getOpacityGS(alpha)
fmt.Fprintf(w, " /%v gs", gs)
w.alpha = alpha
}
}
// SetFill sets the filling paint.
func (w *pdfPageWriter) SetFill(fill canvas.Paint) {
if fill.Equal(w.fill) {
return
}
if fill.IsPattern() {
// TODO
} else if fill.IsGradient() {
// TODO: should we unset cs?
fmt.Fprintf(w, " /Pattern cs /%v scn", w.getPattern(fill.Gradient))
} else {
a := float64(fill.Color.A) / 255.0
if fill.Color.R == fill.Color.G && fill.Color.R == fill.Color.B {
fmt.Fprintf(w, " %v g", dec(float64(fill.Color.R)/255.0/a))
} else {
fmt.Fprintf(w, " %v %v %v rg", dec(float64(fill.Color.R)/255.0/a), dec(float64(fill.Color.G)/255.0/a), dec(float64(fill.Color.B)/255.0/a))
}
w.SetAlpha(a)
}
w.fill = fill
}
// SetStroke sets the stroking paint.
func (w *pdfPageWriter) SetStroke(stroke canvas.Paint) {
if stroke.Equal(w.stroke) {
return
}
if stroke.IsPattern() {
// TODO
} else if stroke.IsGradient() {
// TODO: should we unset CS?
fmt.Fprintf(w, " /Pattern CS /%v SCN", w.getPattern(stroke.Gradient))
} else {
a := float64(stroke.Color.A) / 255.0
if stroke.Color.R == stroke.Color.G && stroke.Color.R == stroke.Color.B {
fmt.Fprintf(w, " %v G", dec(float64(stroke.Color.R)/255.0/a))
} else {
fmt.Fprintf(w, " %v %v %v RG", dec(float64(stroke.Color.R)/255.0/a), dec(float64(stroke.Color.G)/255.0/a), dec(float64(stroke.Color.B)/255.0/a))
}
w.SetAlpha(a)
}
w.stroke = stroke
}
// SetLineWidth sets the stroke width.
func (w *pdfPageWriter) SetLineWidth(lineWidth float64) {
if lineWidth != w.lineWidth {
fmt.Fprintf(w, " %v w", dec(lineWidth))
w.lineWidth = lineWidth
}
}
// SetLineCap sets the stroke cap type.
func (w *pdfPageWriter) SetLineCap(capper canvas.Capper) {
var lineCap int
if _, ok := capper.(canvas.ButtCapper); ok {
lineCap = 0
} else if _, ok := capper.(canvas.RoundCapper); ok {
lineCap = 1
} else if _, ok := capper.(canvas.SquareCapper); ok {
lineCap = 2
} else {
panic("PDF: line cap not support")
}
if lineCap != w.lineCap {
fmt.Fprintf(w, " %d J", lineCap)
w.lineCap = lineCap
}
}
// SetLineJoin sets the stroke join type.
func (w *pdfPageWriter) SetLineJoin(joiner canvas.Joiner) {
var lineJoin int
var miterLimit float64
if _, ok := joiner.(canvas.BevelJoiner); ok {
lineJoin = 2
} else if _, ok := joiner.(canvas.RoundJoiner); ok {
lineJoin = 1
} else if miter, ok := joiner.(canvas.MiterJoiner); ok {
lineJoin = 0
if math.IsNaN(miter.Limit) {
panic("PDF: line join not support")
} else {
miterLimit = miter.Limit
}
} else {
panic("PDF: line join not support")
}
if lineJoin != w.lineJoin {
fmt.Fprintf(w, " %d j", lineJoin)
w.lineJoin = lineJoin
}
if lineJoin == 0 && miterLimit != w.miterLimit {
fmt.Fprintf(w, " %v M", dec(miterLimit))
w.miterLimit = miterLimit
}
}
// SetDashes sets the dash phase and array.
func (w *pdfPageWriter) SetDashes(dashPhase float64, dashArray []float64) {
if len(dashArray)%2 == 1 {
dashArray = append(dashArray, dashArray...)
}
// PDF can't handle negative dash phases
if dashPhase < 0.0 {
totalLength := 0.0
for _, dash := range dashArray {
totalLength += dash
}
for dashPhase < 0.0 {
dashPhase += totalLength
}
}
dashes := append(dashArray, dashPhase)
if !float64sEqual(dashes, w.dashes) {
if len(dashes) == 1 {
fmt.Fprintf(w, " [] 0 d")
dashes[0] = 0.0
} else {
fmt.Fprintf(w, " [%v", dec(dashes[0]))
for _, dash := range dashes[1 : len(dashes)-1] {
fmt.Fprintf(w, " %v", dec(dash))
}
fmt.Fprintf(w, "] %v d", dec(dashes[len(dashes)-1]))
}
w.dashes = dashes
}
}
// SetFont sets the font.
func (w *pdfPageWriter) SetFont(font *canvas.Font, size float64, direction canvasText.Direction) {
if !w.inTextObject {
panic("must be in text object")
}
if font != w.font || w.fontSize != size || w.fontDirection != direction {
w.font = font
w.fontSize = size
w.fontDirection = direction
vertical := direction == canvasText.TopToBottom || direction == canvasText.BottomToTop
ref := w.pdf.getFont(font, vertical)
if _, ok := w.resources["Font"]; !ok {
w.resources["Font"] = pdfDict{}
} else {
for name, fontRef := range w.resources["Font"].(pdfDict) {
if ref == fontRef {
fmt.Fprintf(w, " /%v %v Tf", name, dec(size))
return
}
}
}
name := pdfName(fmt.Sprintf("F%d", len(w.resources["Font"].(pdfDict))))
w.resources["Font"].(pdfDict)[name] = ref
fmt.Fprintf(w, " /%v %v Tf", name, dec(size))
}
}
// SetTextPosition sets the text position.
func (w *pdfPageWriter) SetTextPosition(m canvas.Matrix) {
if !w.inTextObject {
panic("must be in text object")
}
if m.Equals(w.textPosition) {
return
}
if canvas.Equal(m[0][0], w.textPosition[0][0]) && canvas.Equal(m[0][1], w.textPosition[0][1]) && canvas.Equal(m[1][0], w.textPosition[1][0]) && canvas.Equal(m[1][1], w.textPosition[1][1]) {
d := w.textPosition.Inv().Dot(canvas.Point{m[0][2], m[1][2]})
fmt.Fprintf(w, " %v %v Td", dec(d.X), dec(d.Y))
} else {
fmt.Fprintf(w, " %v %v %v %v %v %v Tm", dec(m[0][0]), dec(m[1][0]), dec(m[0][1]), dec(m[1][1]), dec(m[0][2]), dec(m[1][2]))
}
w.textPosition = m
}
// SetTextRenderMode sets the text rendering mode.
func (w *pdfPageWriter) SetTextRenderMode(mode int) {
if !w.inTextObject {
panic("must be in text object")
}
if w.textRenderMode != mode {
fmt.Fprintf(w, " %d Tr", mode)
w.textRenderMode = mode
}
}
// SetTextCharSpace sets the text character spacing.
func (w *pdfPageWriter) SetTextCharSpace(space float64) {
if !w.inTextObject {
panic("must be in text object")
}
if !canvas.Equal(w.textCharSpace, space) {
fmt.Fprintf(w, " %v Tc", dec(space))
w.textCharSpace = space
}
}
// StartTextObject starts a text object.
func (w *pdfPageWriter) StartTextObject() {
if w.inTextObject {
panic("already in text object")
}
fmt.Fprintf(w, " BT")
w.textPosition = canvas.Identity
w.inTextObject = true
}
// EndTextObject ends a text object.
func (w *pdfPageWriter) EndTextObject() {
if !w.inTextObject {
panic("must be in text object")
}
fmt.Fprintf(w, " ET")
w.inTextObject = false
}
// WriteText writes text using a writing mode and a list of strings and inter-character distance modifiers (ints or float64s).
func (w *pdfPageWriter) WriteText(mode canvas.WritingMode, TJ ...interface{}) {
if !w.inTextObject {
panic("must be in text object")
}
if len(TJ) == 0 || w.font == nil {
return
}
first := true
write := func(glyphs []canvasText.Glyph) {
if first {
fmt.Fprintf(w, "(")
first = false
} else {
fmt.Fprintf(w, " (")
}
subset := w.pdf.fontSubset[w.font]
for _, glyph := range glyphs {
glyphID := subset.Get(glyph.ID)
for _, c := range []uint8{uint8((glyphID & 0xff00) >> 8), uint8(glyphID & 0x00ff)} {
if c == '\n' {
binary.Write(w, binary.BigEndian, uint8('\\'))
binary.Write(w, binary.BigEndian, uint8('n'))
} else if c == '\r' {
binary.Write(w, binary.BigEndian, uint8('\\'))
binary.Write(w, binary.BigEndian, uint8('r'))
} else if c == '\\' || c == '(' || c == ')' {
binary.Write(w, binary.BigEndian, uint8('\\'))
binary.Write(w, binary.BigEndian, c)
} else {
binary.Write(w, binary.BigEndian, c)
}
}
}
fmt.Fprintf(w, ")")
}
writeString := func(s string) {
rs := []rune(s)
glyphs := make([]canvasText.Glyph, len(rs))
for i, r := range rs {
glyphs[i].ID = w.font.SFNT.GlyphIndex(r)
}
write(glyphs)
}
position := w.textPosition
if glyphs, ok := TJ[0].([]canvasText.Glyph); ok && 0 < len(glyphs) && mode != canvas.HorizontalTB && !glyphs[0].Vertical {
glyphRotation, glyphOffset := glyphs[0].Rotation(), glyphs[0].YOffset-int32(glyphs[0].SFNT.Head.UnitsPerEm/2)
if glyphRotation != canvasText.NoRotation || glyphOffset != 0 {
w.SetTextPosition(position.Rotate(float64(glyphRotation)).Translate(0.0, glyphs[0].Size/float64(glyphs[0].SFNT.Head.UnitsPerEm)*mmPerPt*float64(glyphOffset)))
}
}
f := 1000.0 / float64(w.font.SFNT.Head.UnitsPerEm)
fmt.Fprintf(w, "[")
for _, tj := range TJ {
switch val := tj.(type) {
case []canvasText.Glyph:
i := 0
for j, glyph := range val {
if mode == canvas.HorizontalTB || !glyph.Vertical {
origXAdvance := int32(w.font.SFNT.GlyphAdvance(glyph.ID))
if glyph.XAdvance != origXAdvance {
write(val[i : j+1])
fmt.Fprintf(w, " %d", -int(f*float64(glyph.XAdvance-origXAdvance)+0.5))
i = j + 1
}
} else {
origYAdvance := -int32(w.font.SFNT.GlyphVerticalAdvance(glyph.ID))
if glyph.YAdvance != origYAdvance {
write(val[i : j+1])
fmt.Fprintf(w, " %d", -int(f*float64(glyph.YAdvance-origYAdvance)+0.5))
i = j + 1
}
}
}
write(val[i:])
case string:
i := 0
if mode == canvas.HorizontalTB {
var rPrev rune
for j, r := range val {
if i < j {
kern := w.font.SFNT.Kerning(w.font.SFNT.GlyphIndex(rPrev), w.font.SFNT.GlyphIndex(r))
if kern != 0 {
writeString(val[i:j])
fmt.Fprintf(w, " %d", -int(f*float64(kern)+0.5))
i = j
}
}
rPrev = r
}
}
writeString(val[i:])
case float64:
fmt.Fprintf(w, " %d", -int(val*1000.0/w.fontSize+0.5))
case int:
fmt.Fprintf(w, " %d", -int(float64(val)*1000.0/w.fontSize+0.5))
}
}
fmt.Fprintf(w, "]TJ")
}
// DrawImage embeds and draws an image.
func (w *pdfPageWriter) DrawImage(img image.Image, enc canvas.ImageEncoding, m canvas.Matrix) {
size := img.Bounds().Size()