forked from signintech/gopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subset_font_obj.go
352 lines (302 loc) · 9.3 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
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
package gopdf
import (
"errors"
"fmt"
"io"
"github.com/signintech/gopdf/fontmaker/core"
)
// ErrCharNotFound char not found
var ErrCharNotFound = errors.New("char not found")
// ErrGlyphNotFound font file not contain glyph
var ErrGlyphNotFound = errors.New("glyph 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
funcGetRoot func() *GoPdf
}
func (s *SubsetFontObj) init(funcGetRoot func() *GoPdf) {
s.CharacterToGlyphIndex = NewMapOfCharacterToGlyphIndex() //make(map[rune]uint)
s.funcKernOverride = nil
s.funcGetRoot = funcGetRoot
}
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) {
if option.OnGlyphNotFoundSubstitute == nil {
option.OnGlyphNotFoundSubstitute = DefaultOnGlyphNotFoundSubstitute
}
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
}
// SetTTFData set ttf
func (s *SubsetFontObj) SetTTFData(data []byte) error {
useKerning := s.ttfFontOption.UseKerning
s.ttfp.SetUseKerning(useKerning)
err := s.ttfp.ParseFontData(data)
if err != nil {
return err
}
return nil
}
// AddChars add char to map CharacterToGlyphIndex
func (s *SubsetFontObj) AddChars(txt string) (string, error) {
var buff []rune
for _, runeValue := range txt {
if s.CharacterToGlyphIndex.KeyExists(runeValue) {
buff = append(buff, runeValue)
continue
}
glyphIndex, err := s.CharCodeToGlyphIndex(runeValue)
if err == ErrGlyphNotFound {
//never return error on this, just call function OnGlyphNotFound
if s.ttfFontOption.OnGlyphNotFound != nil {
s.ttfFontOption.OnGlyphNotFound(runeValue)
}
//start: try to find rune for replace
alreadyExists, runeValueReplace, glyphIndexReplace := s.replaceGlyphThatNotFound(runeValue)
if !alreadyExists {
s.CharacterToGlyphIndex.Set(runeValueReplace, glyphIndexReplace) // [runeValue] = glyphIndex
}
//end: try to find rune for replace
buff = append(buff, runeValueReplace)
continue
} else if err != nil {
return "", err
}
s.CharacterToGlyphIndex.Set(runeValue, glyphIndex) // [runeValue] = glyphIndex
buff = append(buff, runeValue)
}
return string(buff), 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 == ErrGlyphNotFound {
//never return error on this, just call function OnGlyphNotFound
if s.ttfFontOption.OnGlyphNotFound != nil {
s.ttfFontOption.OnGlyphNotFound(runeValue)
}
//start: try to find rune for replace
runeValueReplace, glyphIndexReplace, ok := s.replaceGlyphThatNotFound(runeValue)
if ok {
s.CharacterToGlyphIndex.Set(runeValueReplace, glyphIndexReplace) // [runeValue] = glyphIndex
}
//end: try to find rune for replace
continue
} else if err != nil {
return err
}
s.CharacterToGlyphIndex.Set(runeValue, glyphIndex) // [runeValue] = glyphIndex
}
return nil
}
*/
// replaceGlyphThatNotFound find glyph to replaced
// it returns
// - true if rune already add to CharacterToGlyphIndex
// - rune for replace
// - rune for replace is found or not
// - glyph index for replace
func (s *SubsetFontObj) replaceGlyphThatNotFound(runeNotFound rune) (bool, rune, uint) {
if s.ttfFontOption.OnGlyphNotFoundSubstitute != nil {
runeForReplace := s.ttfFontOption.OnGlyphNotFoundSubstitute(runeNotFound)
if s.CharacterToGlyphIndex.KeyExists(runeForReplace) {
return true, runeForReplace, 0
}
glyphIndexForReplace, err := s.CharCodeToGlyphIndex(runeForReplace)
if err != nil {
return false, runeForReplace, 0
}
return false, runeForReplace, glyphIndexForReplace
}
return false, runeNotFound, 0
}
// CharIndex index of char in glyph table
func (s *SubsetFontObj) CharIndex(r rune) (uint, error) {
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) {
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), ErrGlyphNotFound
}
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, ErrGlyphNotFound
}
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 gets 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
}
gIndex, err := s.charCodeToGlyphIndexFormat12(r)
if err != nil {
return 0, err
}
return gIndex, nil
}
// GlyphIndexToPdfWidth gets width 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 gets TTFParser.
func (s *SubsetFontObj) GetTTFParser() *core.TTFParser {
return &s.ttfp
}
// GetUnderlineThickness underlineThickness.
func (s *SubsetFontObj) GetUnderlineThickness() int {
return s.ttfp.UnderlineThickness()
}
func (s *SubsetFontObj) GetUnderlineThicknessPx(fontSize float64) float64 {
return (float64(s.ttfp.UnderlineThickness()) / float64(s.ttfp.UnitsPerEm())) * fontSize
}
// GetUnderlinePosition underline postion.
func (s *SubsetFontObj) GetUnderlinePosition() int {
return s.ttfp.UnderlinePosition()
}
func (s *SubsetFontObj) GetUnderlinePositionPx(fontSize float64) float64 {
return (float64(s.ttfp.UnderlinePosition()) / float64(s.ttfp.UnitsPerEm())) * fontSize
}
func (s *SubsetFontObj) GetAscender() int {
return s.ttfp.Ascender()
}
func (s *SubsetFontObj) GetAscenderPx(fontSize float64) float64 {
return (float64(s.ttfp.Ascender()) / float64(s.ttfp.UnitsPerEm())) * fontSize
}
func (s *SubsetFontObj) GetDescender() int {
return s.ttfp.Descender()
}
func (s *SubsetFontObj) GetDescenderPx(fontSize float64) float64 {
return (float64(s.ttfp.Descender()) / float64(s.ttfp.UnitsPerEm())) * fontSize
}