forked from signintech/gopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subset_font_obj.go
256 lines (215 loc) · 5.97 KB
/
subset_font_obj.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
package gopdf
import (
"errors"
"fmt"
"io"
"github.com/signintech/gopdf/fontmaker/core"
)
//ErrCharNotFound char not found
var ErrCharNotFound = errors.New("char not found")
//SubsetFontObj pdf subsetFont object
type SubsetFontObj struct {
ttfp core.TTFParser
Family string
CharacterToGlyphIndex *MapOfCharacterToGlyphIndex
CountOfFont int
indexObjCIDFont int
indexObjUnicodeMap int
ttfFontOption TtfOption
funcKernOverride FuncKernOverride
}
func (s *SubsetFontObj) init(funcGetRoot func() *GoPdf) {
s.CharacterToGlyphIndex = NewMapOfCharacterToGlyphIndex() //make(map[rune]uint)
s.funcKernOverride = nil
}
func (s *SubsetFontObj) write(w io.Writer, objID int) error {
//me.AddChars("จ")
io.WriteString(w, "<<\n")
fmt.Fprintf(w, "/BaseFont /%s\n", CreateEmbeddedFontSubsetName(s.Family))
fmt.Fprintf(w, "/DescendantFonts [%d 0 R]\n", s.indexObjCIDFont+1)
io.WriteString(w, "/Encoding /Identity-H\n")
io.WriteString(w, "/Subtype /Type0\n")
fmt.Fprintf(w, "/ToUnicode %d 0 R\n", s.indexObjUnicodeMap+1)
io.WriteString(w, "/Type /Font\n")
io.WriteString(w, ">>\n")
return nil
}
//SetIndexObjCIDFont set IndexObjCIDFont
func (s *SubsetFontObj) SetIndexObjCIDFont(index int) {
s.indexObjCIDFont = index
}
//SetIndexObjUnicodeMap set IndexObjUnicodeMap
func (s *SubsetFontObj) SetIndexObjUnicodeMap(index int) {
s.indexObjUnicodeMap = index
}
//SetFamily set font family name
func (s *SubsetFontObj) SetFamily(familyname string) {
s.Family = familyname
}
//GetFamily get font family name
func (s *SubsetFontObj) GetFamily() string {
return s.Family
}
//SetTtfFontOption set TtfOption must set before SetTTFByPath
func (s *SubsetFontObj) SetTtfFontOption(option TtfOption) {
s.ttfFontOption = option
}
//GetTtfFontOption get TtfOption must set before SetTTFByPath
func (s *SubsetFontObj) GetTtfFontOption() TtfOption {
return s.ttfFontOption
}
//KernValueByLeft find kern value from kern table by left
func (s *SubsetFontObj) KernValueByLeft(left uint) (bool, *core.KernValue) {
if !s.ttfFontOption.UseKerning {
return false, nil
}
k := s.ttfp.Kern()
if k == nil {
return false, nil
}
if kval, ok := k.Kerning[left]; ok {
return true, &kval
}
return false, nil
}
//SetTTFByPath set ttf
func (s *SubsetFontObj) SetTTFByPath(ttfpath string) error {
useKerning := s.ttfFontOption.UseKerning
s.ttfp.SetUseKerning(useKerning)
err := s.ttfp.Parse(ttfpath)
if err != nil {
return err
}
return nil
}
//SetTTFByReader set ttf
func (s *SubsetFontObj) SetTTFByReader(rd io.Reader) error {
useKerning := s.ttfFontOption.UseKerning
s.ttfp.SetUseKerning(useKerning)
err := s.ttfp.ParseByReader(rd)
if err != nil {
return err
}
return nil
}
//AddChars add char to map CharacterToGlyphIndex
func (s *SubsetFontObj) AddChars(txt string) error {
for _, runeValue := range txt {
if s.CharacterToGlyphIndex.KeyExists(runeValue) {
continue
}
glyphIndex, err := s.CharCodeToGlyphIndex(runeValue)
if err != nil {
return err
}
s.CharacterToGlyphIndex.Set(runeValue, glyphIndex) // [runeValue] = glyphIndex
}
return nil
}
//CharIndex index of char in glyph table
func (s *SubsetFontObj) CharIndex(r rune) (uint, error) {
/*
if index, ok := s.CharacterToGlyphIndex[r]; ok {
return index, nil
}
return 0, ErrCharNotFound
*/
glyIndex, ok := s.CharacterToGlyphIndex.Val(r)
if ok {
return glyIndex, nil
}
return 0, ErrCharNotFound
}
//CharWidth with of char
func (s *SubsetFontObj) CharWidth(r rune) (uint, error) {
/*glyphIndex := s.CharacterToGlyphIndex
if index, ok := glyphIndex[r]; ok {
return s.GlyphIndexToPdfWidth(index), nil
}
return 0, ErrCharNotFound*/
glyIndex, ok := s.CharacterToGlyphIndex.Val(r)
if ok {
return s.GlyphIndexToPdfWidth(glyIndex), nil
}
return 0, ErrCharNotFound
}
func (s *SubsetFontObj) getType() string {
return "SubsetFont"
}
func (s *SubsetFontObj) charCodeToGlyphIndexFormat12(r rune) (uint, error) {
value := uint(r)
gTbs := s.ttfp.GroupingTables()
for _, gTb := range gTbs {
if value >= gTb.StartCharCode && value <= gTb.EndCharCode {
gIndex := (value - gTb.StartCharCode) + gTb.GlyphID
return gIndex, nil
}
}
return uint(0), errors.New("not found glyph")
}
func (s *SubsetFontObj) charCodeToGlyphIndexFormat4(r rune) (uint, error) {
value := uint(r)
seg := uint(0)
segCount := s.ttfp.SegCount
for seg < segCount {
if value <= s.ttfp.EndCount[seg] {
break
}
seg++
}
//fmt.Printf("\ncccc--->%#v\n", me.ttfp.Chars())
if value < s.ttfp.StartCount[seg] {
return 0, nil
}
if s.ttfp.IdRangeOffset[seg] == 0 {
return (value + s.ttfp.IdDelta[seg]) & 0xFFFF, nil
}
//fmt.Printf("IdRangeOffset=%d\n", me.ttfp.IdRangeOffset[seg])
idx := s.ttfp.IdRangeOffset[seg]/2 + (value - s.ttfp.StartCount[seg]) - (segCount - seg)
if s.ttfp.GlyphIdArray[int(idx)] == uint(0) {
return 0, nil
}
return (s.ttfp.GlyphIdArray[int(idx)] + s.ttfp.IdDelta[seg]) & 0xFFFF, nil
}
//CharCodeToGlyphIndex get glyph index from char code
func (s *SubsetFontObj) CharCodeToGlyphIndex(r rune) (uint, error) {
value := uint64(r)
if value <= 0xFFFF {
gIndex, err := s.charCodeToGlyphIndexFormat4(r)
if err != nil {
return 0, err
}
return gIndex, nil
} else {
gIndex, err := s.charCodeToGlyphIndexFormat12(r)
if err != nil {
return 0, err
}
return gIndex, nil
}
}
//GlyphIndexToPdfWidth get with from glyphIndex
func (s *SubsetFontObj) GlyphIndexToPdfWidth(glyphIndex uint) uint {
numberOfHMetrics := s.ttfp.NumberOfHMetrics()
unitsPerEm := s.ttfp.UnitsPerEm()
if glyphIndex >= numberOfHMetrics {
glyphIndex = numberOfHMetrics - 1
}
width := s.ttfp.Widths()[glyphIndex]
if unitsPerEm == 1000 {
return width
}
return width * 1000 / unitsPerEm
}
//GetTTFParser get TTFParser
func (s *SubsetFontObj) GetTTFParser() *core.TTFParser {
return &s.ttfp
}
//GetUt underlineThickness
func (s *SubsetFontObj) GetUt() int {
return s.ttfp.UnderlineThickness()
}
//GetUp underline postion
func (s *SubsetFontObj) GetUp() int {
return s.ttfp.UnderlinePosition()
}