forked from unidoc/unidoc
/
font.go
613 lines (497 loc) · 15.1 KB
/
font.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
/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.md', which is part of this source code package.
*/
package model
import (
"errors"
"io/ioutil"
"github.com/unidoc/unidoc/common"
"github.com/unidoc/unidoc/pdf/core"
"github.com/unidoc/unidoc/pdf/model/fonts"
"github.com/unidoc/unidoc/pdf/model/textencoding"
)
// The PdfFont structure represents an underlying font structure which can be of type:
// - Type0
// - Type1
// - TrueType
// etc.
type PdfFont struct {
context interface{} // The underlying font: Type0, Type1, Truetype, etc..
}
// Set the encoding for the underlying font.
func (font PdfFont) SetEncoder(encoder textencoding.TextEncoder) {
switch t := font.context.(type) {
case *pdfFontTrueType:
t.SetEncoder(encoder)
}
}
func (font PdfFont) GetGlyphCharMetrics(glyph string) (fonts.CharMetrics, bool) {
switch t := font.context.(type) {
case *pdfFontTrueType:
return t.GetGlyphCharMetrics(glyph)
}
return fonts.CharMetrics{}, false
}
func newPdfFontFromPdfObject(obj core.PdfObject) (*PdfFont, error) {
font := &PdfFont{}
dictObj := obj
if ind, is := obj.(*core.PdfIndirectObject); is {
dictObj = ind.PdfObject
}
d, ok := dictObj.(*core.PdfObjectDictionary)
if !ok {
common.Log.Debug("Font not given by a dictionary (%T)", obj)
return nil, errors.New("Type check error")
}
if obj := d.Get("Type"); obj != nil {
oname, is := obj.(*core.PdfObjectName)
if !is || string(*oname) != "Font" {
common.Log.Debug("Incompatibility ERROR: Type (Required) defined but not Font name")
return nil, errors.New("Range check error")
}
} else {
common.Log.Debug("Incompatibility ERROR: Type (Required) missing")
return nil, errors.New("Required attribute missing")
}
obj = d.Get("Subtype")
if obj == nil {
common.Log.Debug("Incompatibility ERROR: Subtype (Required) missing")
return nil, errors.New("Required attribute missing")
}
subtype, ok := core.TraceToDirectObject(obj).(*core.PdfObjectName)
if !ok {
common.Log.Debug("Incompatibility ERROR: subtype not a name (%T) ", obj)
return nil, errors.New("Type check error")
}
switch subtype.String() {
case "TrueType":
truefont, err := newPdfFontTrueTypeFromPdfObject(obj)
if err != nil {
common.Log.Debug("Error loading truetype font: %v", truefont)
return nil, err
}
font.context = truefont
default:
common.Log.Debug("Unsupported font type: %s", subtype.String())
return nil, errors.New("Unsupported font type")
}
return font, nil
}
func (font PdfFont) ToPdfObject() core.PdfObject {
switch f := font.context.(type) {
case *pdfFontTrueType:
return f.ToPdfObject()
}
// If not supported, return null..
common.Log.Debug("Unsupported font (%T) - returning null object", font.context)
return core.MakeNull()
}
type pdfFontTrueType struct {
Encoder textencoding.TextEncoder
firstChar int
lastChar int
charWidths []float64
// Subtype shall be TrueType.
// Encoding is subject to limitations that are described in 9.6.6, "Character Encoding".
// BaseFont is derived differently.
BaseFont core.PdfObject
FirstChar core.PdfObject
LastChar core.PdfObject
Widths core.PdfObject
FontDescriptor *PdfFontDescriptor
Encoding core.PdfObject
ToUnicode core.PdfObject
container *core.PdfIndirectObject
}
func (font pdfFontTrueType) SetEncoder(encoder textencoding.TextEncoder) {
font.Encoder = encoder
}
func (font pdfFontTrueType) GetGlyphCharMetrics(glyph string) (fonts.CharMetrics, bool) {
metrics := fonts.CharMetrics{}
code, found := font.Encoder.GlyphToCharcode(glyph)
if !found {
return metrics, false
}
if int(code) < font.firstChar {
common.Log.Debug("Code lower than firstchar (%d < %d)", code, font.firstChar)
return metrics, false
}
if int(code) > font.lastChar {
common.Log.Debug("Code higher than lastchar (%d < %d)", code, font.lastChar)
return metrics, false
}
index := int(code) - font.firstChar
if index >= len(font.charWidths) {
common.Log.Debug("Code outside of widths range")
return metrics, false
}
width := font.charWidths[index]
metrics.Wx = width
return metrics, true
}
func newPdfFontTrueTypeFromPdfObject(obj core.PdfObject) (*pdfFontTrueType, error) {
font := &pdfFontTrueType{}
if ind, is := obj.(*core.PdfIndirectObject); is {
font.container = ind
obj = ind.PdfObject
}
d, ok := obj.(*core.PdfObjectDictionary)
if !ok {
common.Log.Debug("Font object invalid, not a dictionary (%T)", obj)
return nil, errors.New("Type check error")
}
if obj := d.Get("Type"); obj != nil {
oname, is := obj.(*core.PdfObjectName)
if !is || oname.String() != "Font" {
common.Log.Debug("Incompatibility: Type defined but not Font")
}
}
if obj := d.Get("Subtype"); obj != nil {
oname, is := obj.(*core.PdfObjectName)
if !is || oname.String() != "TrueType" {
common.Log.Debug("Incompatibility: Loading TrueType font but Subtype != TrueType")
}
}
font.BaseFont = d.Get("BaseFont")
if obj := d.Get("FirstChar"); obj != nil {
font.FirstChar = obj
intVal, ok := core.TraceToDirectObject(obj).(*core.PdfObjectInteger)
if !ok {
common.Log.Debug("Invalid FirstChar type (%T)", obj)
return nil, errors.New("Type check error")
}
font.firstChar = int(*intVal)
} else {
common.Log.Debug("ERROR: FirstChar attribute missing")
return nil, errors.New("Required attribute missing")
}
if obj := d.Get("LastChar"); obj != nil {
font.LastChar = obj
intVal, ok := core.TraceToDirectObject(obj).(*core.PdfObjectInteger)
if !ok {
common.Log.Debug("Invalid LastChar type (%T)", obj)
return nil, errors.New("Type check error")
}
font.lastChar = int(*intVal)
} else {
common.Log.Debug("ERROR: FirstChar attribute missing")
return nil, errors.New("Required attribute missing")
}
font.charWidths = []float64{}
if obj := d.Get("Widths"); obj != nil {
font.Widths = obj
arr, ok := core.TraceToDirectObject(obj).(*core.PdfObjectArray)
if !ok {
common.Log.Debug("Widths attribute != array (%T)", arr)
return nil, errors.New("Type check error")
}
widths, err := arr.ToFloat64Array()
if err != nil {
common.Log.Debug("Error converting widths to array")
return nil, err
}
if len(widths) != (font.lastChar - font.firstChar + 1) {
common.Log.Debug("Invalid widths length != %d (%d)", font.lastChar-font.firstChar+1, len(widths))
return nil, errors.New("Range check error")
}
font.charWidths = widths
} else {
common.Log.Debug("Widths missing from font")
return nil, errors.New("Required attribute missing")
}
if obj := d.Get("FontDescriptor"); obj != nil {
descriptor, err := newPdfFontDescriptorFromPdfObject(obj)
if err != nil {
common.Log.Debug("Error loading font descriptor: %v", err)
return nil, err
}
font.FontDescriptor = descriptor
}
font.Encoding = d.Get("Encoding")
font.ToUnicode = d.Get("ToUnicode")
return font, nil
}
func (this *pdfFontTrueType) ToPdfObject() core.PdfObject {
if this.container == nil {
this.container = &core.PdfIndirectObject{}
}
d := core.MakeDict()
this.container.PdfObject = d
d.Set("Type", core.MakeName("Font"))
d.Set("Subtype", core.MakeName("TrueType"))
if this.BaseFont != nil {
d.Set("BaseFont", this.BaseFont)
}
if this.FirstChar != nil {
d.Set("FirstChar", this.FirstChar)
}
if this.LastChar != nil {
d.Set("LastChar", this.LastChar)
}
if this.Widths != nil {
d.Set("Widths", this.Widths)
}
if this.FontDescriptor != nil {
d.Set("FontDescriptor", this.FontDescriptor.ToPdfObject())
}
if this.Encoding != nil {
d.Set("Encoding", this.Encoding)
}
if this.ToUnicode != nil {
d.Set("ToUnicode", this.ToUnicode)
}
return this.container
}
func NewPdfFontFromTTFFile(filePath string) (*PdfFont, error) {
ttf, err := fonts.TtfParse(filePath)
if err != nil {
common.Log.Debug("Error loading ttf font: %v", err)
return nil, err
}
truefont := &pdfFontTrueType{}
truefont.Encoder = textencoding.NewWinAnsiTextEncoder()
truefont.firstChar = 32
truefont.lastChar = 255
truefont.BaseFont = core.MakeName(ttf.PostScriptName)
truefont.FirstChar = core.MakeInteger(32)
truefont.LastChar = core.MakeInteger(255)
k := 1000.0 / float64(ttf.UnitsPerEm)
if len(ttf.Widths) <= 0 {
return nil, errors.New("Missing required attribute (Widths)")
}
missingWidth := k * float64(ttf.Widths[0])
vals := []float64{}
for charcode := 32; charcode <= 255; charcode++ {
runeVal, found := truefont.Encoder.CharcodeToRune(byte(charcode))
if !found {
common.Log.Debug("Rune not found (charcode: %d)", charcode)
vals = append(vals, missingWidth)
continue
}
pos, ok := ttf.Chars[uint16(runeVal)]
if !ok {
common.Log.Debug("Rune not in TTF Chars")
vals = append(vals, missingWidth)
continue
}
w := k * float64(ttf.Widths[pos])
vals = append(vals, w)
}
truefont.Widths = &core.PdfIndirectObject{PdfObject: core.MakeArrayFromFloats(vals)}
if len(vals) < (255 - 32 + 1) {
common.Log.Debug("Invalid length of widths, %d < %d", len(vals), 255-32+1)
return nil, errors.New("Range check error")
}
truefont.charWidths = vals[:255-32+1]
// Default.
// XXX/FIXME TODO: Only use the encoder object.
truefont.Encoding = core.MakeName("WinAnsiEncoding")
descriptor := &PdfFontDescriptor{}
descriptor.Ascent = core.MakeFloat(k * float64(ttf.TypoAscender))
descriptor.Descent = core.MakeFloat(k * float64(ttf.TypoDescender))
descriptor.CapHeight = core.MakeFloat(k * float64(ttf.CapHeight))
descriptor.FontBBox = core.MakeArrayFromFloats([]float64{k * float64(ttf.Xmin), k * float64(ttf.Ymin), k * float64(ttf.Xmax), k * float64(ttf.Ymax)})
descriptor.ItalicAngle = core.MakeFloat(float64(ttf.ItalicAngle))
descriptor.MissingWidth = core.MakeFloat(k * float64(ttf.Widths[0]))
ttfBytes, err := ioutil.ReadFile(filePath)
if err != nil {
common.Log.Debug("Unable to read file contents: %v", err)
return nil, err
}
// XXX/TODO: Encode the file...
stream, err := core.MakeStream(ttfBytes, core.NewFlateEncoder())
if err != nil {
common.Log.Debug("Unable to make stream: %v", err)
return nil, err
}
stream.PdfObjectDictionary.Set("Length1", core.MakeInteger(int64(len(ttfBytes))))
descriptor.FontFile2 = stream
if ttf.Bold {
descriptor.StemV = core.MakeInteger(120)
} else {
descriptor.StemV = core.MakeInteger(70)
}
// Flags.
flags := 1 << 5
if ttf.IsFixedPitch {
flags |= 1
}
if ttf.ItalicAngle != 0 {
flags |= 1 << 6
}
descriptor.Flags = core.MakeInteger(int64(flags))
// Build Font.
truefont.FontDescriptor = descriptor
font := &PdfFont{}
font.context = truefont
return font, nil
}
// Font descriptors specifies metrics and other attributes of a font.
type PdfFontDescriptor struct {
FontName core.PdfObject
FontFamily core.PdfObject
FontStretch core.PdfObject
FontWeight core.PdfObject
Flags core.PdfObject
FontBBox core.PdfObject
ItalicAngle core.PdfObject
Ascent core.PdfObject
Descent core.PdfObject
Leading core.PdfObject
CapHeight core.PdfObject
XHeight core.PdfObject
StemV core.PdfObject
StemH core.PdfObject
AvgWidth core.PdfObject
MaxWidth core.PdfObject
MissingWidth core.PdfObject
FontFile core.PdfObject
FontFile2 core.PdfObject
FontFile3 core.PdfObject
CharSet core.PdfObject
// Additional entries for CIDFonts
Style core.PdfObject
Lang core.PdfObject
FD core.PdfObject
CIDSet core.PdfObject
// Container.
container *core.PdfIndirectObject
}
// Load the font descriptor from a PdfObject. Can either be a *PdfIndirectObject or
// a *PdfObjectDictionary.
func newPdfFontDescriptorFromPdfObject(obj core.PdfObject) (*PdfFontDescriptor, error) {
descriptor := &PdfFontDescriptor{}
if ind, is := obj.(*core.PdfIndirectObject); is {
descriptor.container = ind
obj = ind.PdfObject
}
d, ok := obj.(*core.PdfObjectDictionary)
if !ok {
common.Log.Debug("FontDescriptor not given by a dictionary (%T)", obj)
return nil, errors.New("Type check error")
}
if obj := d.Get("Type"); obj != nil {
oname, is := obj.(*core.PdfObjectName)
if !is || string(*oname) != "FontDescriptor" {
common.Log.Debug("Incompatibility: Font descriptor Type invalid (%T)", obj)
}
} else {
common.Log.Debug("Incompatibility: Type (Required) missing")
}
if obj := d.Get("FontName"); obj != nil {
descriptor.FontName = obj
} else {
common.Log.Debug("Incompatibility: FontName (Required) missing")
}
descriptor.FontFamily = d.Get("FontFamily")
descriptor.FontStretch = d.Get("FontStretch")
descriptor.FontWeight = d.Get("FontWeight")
descriptor.Flags = d.Get("Flags")
descriptor.FontBBox = d.Get("FontBBox")
descriptor.ItalicAngle = d.Get("ItalicAngle")
descriptor.Ascent = d.Get("Ascent")
descriptor.Descent = d.Get("Descent")
descriptor.Leading = d.Get("Leading")
descriptor.CapHeight = d.Get("CapHeight")
descriptor.XHeight = d.Get("XHeight")
descriptor.StemV = d.Get("StemV")
descriptor.StemH = d.Get("StemH")
descriptor.AvgWidth = d.Get("AvgWidth")
descriptor.MaxWidth = d.Get("MaxWidth")
descriptor.MissingWidth = d.Get("MissingWidth")
descriptor.FontFile = d.Get("FontFile")
descriptor.FontFile2 = d.Get("FontFile2")
descriptor.FontFile3 = d.Get("FontFile3")
descriptor.CharSet = d.Get("CharSet")
descriptor.Style = d.Get("Style")
descriptor.Lang = d.Get("Lang")
descriptor.FD = d.Get("FD")
descriptor.CIDSet = d.Get("CIDSet")
return descriptor, nil
}
// Convert to a PDF dictionary inside an indirect object.
func (this *PdfFontDescriptor) ToPdfObject() core.PdfObject {
d := core.MakeDict()
if this.container == nil {
this.container = &core.PdfIndirectObject{}
}
this.container.PdfObject = d
d.Set("Type", core.MakeName("FontDescriptor"))
if this.FontName != nil {
d.Set("FontName", this.FontName)
}
if this.FontFamily != nil {
d.Set("FontFamily", this.FontFamily)
}
if this.FontStretch != nil {
d.Set("FontStretch", this.FontStretch)
}
if this.FontWeight != nil {
d.Set("FontWeight", this.FontWeight)
}
if this.Flags != nil {
d.Set("Flags", this.Flags)
}
if this.FontBBox != nil {
d.Set("FontBBox", this.FontBBox)
}
if this.ItalicAngle != nil {
d.Set("ItalicAngle", this.ItalicAngle)
}
if this.Ascent != nil {
d.Set("Ascent", this.Ascent)
}
if this.Descent != nil {
d.Set("Descent", this.Descent)
}
if this.Leading != nil {
d.Set("Leading", this.Leading)
}
if this.CapHeight != nil {
d.Set("CapHeight", this.CapHeight)
}
if this.XHeight != nil {
d.Set("XHeight", this.XHeight)
}
if this.StemV != nil {
d.Set("StemV", this.StemV)
}
if this.StemH != nil {
d.Set("StemH", this.StemH)
}
if this.AvgWidth != nil {
d.Set("AvgWidth", this.AvgWidth)
}
if this.MaxWidth != nil {
d.Set("MaxWidth", this.MaxWidth)
}
if this.MissingWidth != nil {
d.Set("MissingWidth", this.MissingWidth)
}
if this.FontFile != nil {
d.Set("FontFile", this.FontFile)
}
if this.FontFile2 != nil {
d.Set("FontFile2", this.FontFile2)
}
if this.FontFile3 != nil {
d.Set("FontFile3", this.FontFile3)
}
if this.CharSet != nil {
d.Set("CharSet", this.CharSet)
}
if this.Style != nil {
d.Set("FontName", this.FontName)
}
if this.Lang != nil {
d.Set("Lang", this.Lang)
}
if this.FD != nil {
d.Set("FD", this.FD)
}
if this.CIDSet != nil {
d.Set("CIDSet", this.CIDSet)
}
return this.container
}