-
Notifications
You must be signed in to change notification settings - Fork 43
/
pdf_font_data.go
128 lines (102 loc) · 2.62 KB
/
pdf_font_data.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
package pdft
import (
"bytes"
"io"
gopdf "github.com/signintech/pdft/minigopdf"
)
// PDFFontData font data
type PDFFontData struct {
fontname string
fontindex int
//gopdf obj
font gopdf.SubsetFontObj
cid gopdf.CIDFontObj
unicodeMap gopdf.UnicodeMap
fontDesc gopdf.SubfontDescriptorObj
dictionary gopdf.PdfDictionaryObj
//IDs
startID int
fontID, cidID, unicodeMapID, fontDescID, dictionaryID int
//stream
fontStream *bytes.Buffer
cidStream *bytes.Buffer
unicodeMapStream *bytes.Buffer
fontDescStream *bytes.Buffer
dictionaryStream *bytes.Buffer
}
func (p *PDFFontData) init() {
p.font.CharacterToGlyphIndex = gopdf.NewMapOfCharacterToGlyphIndex() //make(map[rune]uint)
}
func (p *PDFFontData) setFontName(name string) {
p.font.SetFamily(name)
p.fontname = name
}
func (p *PDFFontData) setFontIndex(fontindex int) {
p.fontindex = fontindex
}
func (p *PDFFontData) fontIndex() int {
return p.fontindex
}
func (p *PDFFontData) fontName() string {
return p.fontname
}
func (p *PDFFontData) setTTFPath(path string) error {
return p.font.SetTTFByPath(path)
}
func (p *PDFFontData) setTTFReader(reader io.Reader) error {
return p.font.SetTTFByReader(reader)
}
func (p *PDFFontData) addChars(text string) error {
return p.font.AddChars(text)
}
func (p *PDFFontData) build() (int, error) {
p.fontID = p.startID + 1
p.cidID = p.startID + 2
p.unicodeMapID = p.startID + 3
p.fontDescID = p.startID + 4
p.dictionaryID = p.startID + 5
newMaxID := p.dictionaryID
//font
p.font.SetIndexObjCIDFont(p.cidID - 1)
p.font.SetIndexObjUnicodeMap(p.unicodeMapID - 1)
//cid
p.cid.SetPtrToSubsetFontObj(&p.font)
p.cid.SetIndexObjSubfontDescriptor(p.fontDescID - 1)
//unicode
p.unicodeMap.SetPtrToSubsetFontObj(&p.font)
//font descriptor
p.fontDesc.SetPtrToSubsetFontObj(&p.font)
p.fontDesc.SetIndexObjPdfDictionary(p.dictionaryID - 1)
//dictionary
p.dictionary.SetPtrToSubsetFontObj(&p.font)
var err error
err = p.font.Build(p.fontID)
if err != nil {
return 0, err
}
err = p.cid.Build(p.cidID)
if err != nil {
return 0, err
}
err = p.unicodeMap.Build(p.unicodeMapID)
if err != nil {
return 0, err
}
err = p.fontDesc.Build(p.fontDescID)
if err != nil {
return 0, err
}
err = p.dictionary.Build(p.dictionaryID)
if err != nil {
return 0, err
}
p.fontStream = p.font.GetObjBuff()
p.cidStream = p.cid.GetObjBuff()
p.unicodeMapStream = p.unicodeMap.GetObjBuff()
p.fontDescStream = p.fontDesc.GetObjBuff()
p.dictionaryStream = p.dictionary.GetObjBuff()
return newMaxID, nil
}
func (p *PDFFontData) setStartID(id int) {
p.startID = id
}