-
Notifications
You must be signed in to change notification settings - Fork 0
/
font.go
584 lines (513 loc) · 14.5 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
// seehuhn.de/go/pdf - a library for reading and writing PDF files
// Copyright (C) 2023 Jochen Voss <voss@seehuhn.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package type3
import (
"errors"
"fmt"
"io"
"math"
"sort"
"golang.org/x/exp/maps"
"seehuhn.de/go/pdf"
"seehuhn.de/go/pdf/font"
"seehuhn.de/go/pdf/font/charcode"
"seehuhn.de/go/pdf/font/cmap"
"seehuhn.de/go/pdf/font/encoding"
"seehuhn.de/go/pdf/font/pdfenc"
"seehuhn.de/go/postscript/funit"
"seehuhn.de/go/postscript/type1/names"
"seehuhn.de/go/sfnt/glyph"
)
// Font is a PDF Type 3 font.
type Font struct {
Glyphs map[string]*Glyph
FontMatrix [6]float64
Ascent funit.Int16
Descent funit.Int16
BaseLineSkip funit.Int16
ItalicAngle float64
IsFixedPitch bool
IsSerif bool
IsScript bool
IsAllCap bool
IsSmallCap bool
ForceBold bool
Resources *pdf.Resources
NumOpen int // -1 if the font is embedded, otherwise the number of glyphs not yet closed
}
// Glyph is a glyph in a type 3 font.
type Glyph struct {
WidthX funit.Int16
BBox funit.Rect16
Data []byte
}
// New creates a new type 3 font.
// Initally the font does not contain any glyphs.
// Use [Font.AddGlyph] to add glyphs to the font.
// Once the font is embedded the first time, no more glyphs can be added.
func New(unitsPerEm uint16) *Font {
m := [6]float64{
1 / float64(unitsPerEm), 0,
0, 1 / float64(unitsPerEm),
0, 0,
}
res := &Font{
FontMatrix: m,
Glyphs: map[string]*Glyph{},
Resources: &pdf.Resources{},
}
return res
}
// glyphList returns a list of all glyph names in the font. The list starts
// with the empty string (to avoid allocating GID 0), followed by the glyph
// names in alphabetical order.
func (f *Font) glyphList() []string {
glyphNames := maps.Keys(f.Glyphs)
glyphNames = append(glyphNames, "")
sort.Strings(glyphNames)
return glyphNames
}
// Embed implements the [font.Font] interface.
func (f *Font) Embed(w pdf.Putter, opt *font.Options) (font.Layouter, error) {
if f.NumOpen > 0 {
return nil, fmt.Errorf("font: %d glyphs not closed", f.NumOpen)
}
f.NumOpen = -1
if opt == nil {
opt = &font.Options{}
}
if opt.Composite {
return nil, fmt.Errorf("Type 3 fonts do not support composite embedding")
}
glyphNames := f.glyphList()
cmap := map[rune]glyph.ID{}
for gid, name := range glyphNames {
if rr := names.ToUnicode(string(name), false); len(rr) == 1 {
cmap[rr[0]] = glyph.ID(gid)
}
}
res := &embedded{
Font: f,
GlyphNames: glyphNames,
w: w,
Res: pdf.Res{
Data: w.Alloc(),
DefName: opt.ResName,
},
CMap: cmap,
SimpleEncoder: encoding.NewSimpleEncoder(),
}
w.AutoClose(res)
return res, nil
}
type embedded struct {
*Font
GlyphNames []string
w pdf.Putter
pdf.Res
CMap map[rune]glyph.ID
*encoding.SimpleEncoder
closed bool
}
// GetGeometry implements the [font.Layouter] interface.
func (f *embedded) GetGeometry() *font.Geometry {
glyphExtents := make([]pdf.Rectangle, len(f.GlyphNames))
widths := make([]float64, len(f.GlyphNames))
for i, name := range f.GlyphNames {
if i == 0 {
continue
}
glyphExtents[i] = glyphBoxtoPDF(f.Glyphs[name].BBox, f.Font.FontMatrix[:])
widths[i] = float64(f.Glyphs[name].WidthX) * f.Font.FontMatrix[0]
}
res := &font.Geometry{
Ascent: float64(f.Ascent) * f.Font.FontMatrix[3],
Descent: float64(f.Descent) * f.Font.FontMatrix[3],
BaseLineDistance: float64(f.BaseLineSkip) * f.Font.FontMatrix[3],
GlyphExtents: glyphExtents,
Widths: widths,
}
return res
}
func glyphBoxtoPDF(b funit.Rect16, M []float64) pdf.Rectangle {
bPDF := pdf.Rectangle{
LLx: math.Inf(+1),
LLy: math.Inf(+1),
URx: math.Inf(-1),
URy: math.Inf(-1),
}
corners := []struct{ x, y funit.Int16 }{
{b.LLx, b.LLy},
{b.LLx, b.URy},
{b.URx, b.LLy},
{b.URx, b.URy},
}
for _, c := range corners {
xf := float64(c.x)
yf := float64(c.y)
x, y := M[0]*xf+M[2]*yf+M[4], M[1]*xf+M[3]*yf+M[5]
bPDF.LLx = min(bPDF.LLx, x)
bPDF.LLy = min(bPDF.LLy, y)
bPDF.URx = max(bPDF.URx, x)
bPDF.URy = max(bPDF.URy, y)
}
return bPDF
}
// Layout implements the [font.Layouter] interface.
func (f *embedded) Layout(seq *font.GlyphSeq, ptSize float64, s string) *font.GlyphSeq {
if seq == nil {
seq = &font.GlyphSeq{}
}
q := f.Font.FontMatrix[0] * ptSize
for _, r := range []rune(s) {
gid, ok := f.CMap[r]
if !ok {
continue
}
seq.Seq = append(seq.Seq, font.Glyph{
GID: gid,
Text: []rune{r},
Advance: float64(f.Glyphs[f.GlyphNames[gid]].WidthX) * q,
})
}
return seq
}
func (f *embedded) ForeachWidth(s pdf.String, yield func(width float64, is_space bool)) {
for _, c := range s {
gid := f.Encoding[c]
name := f.GlyphNames[gid]
width := float64(f.Glyphs[name].WidthX) * f.Font.FontMatrix[0]
yield(width, c == ' ')
}
}
func (f *embedded) CodeAndWidth(s pdf.String, gid glyph.ID, rr []rune) (pdf.String, float64, bool) {
name := f.GlyphNames[gid]
width := float64(f.Glyphs[name].WidthX) * f.Font.FontMatrix[0]
c := f.GIDToCode(gid, rr)
return append(s, c), width, c == ' '
}
func (f *embedded) Close() error {
if f.closed {
return nil
}
f.closed = true
if f.SimpleEncoder.Overflow() {
return fmt.Errorf("too many distinct glyphs used in Type 3 font %q",
f.DefName)
}
encodingGid := f.Encoding
encoding := make([]string, 256)
subset := make(map[string]*Glyph)
for i, gid := range encodingGid {
// Gid 0 maps to the empty glyph name, which is not in the charProcs map.
if glyph := f.Glyphs[f.GlyphNames[gid]]; glyph != nil {
name := f.GlyphNames[gid]
encoding[i] = name
subset[name] = glyph
}
}
var descriptor *font.Descriptor
if pdf.IsTagged(f.w) {
descriptor = &font.Descriptor{
IsFixedPitch: f.IsFixedPitch,
IsSerif: f.IsSerif,
IsScript: f.IsScript,
IsItalic: f.ItalicAngle != 0,
IsAllCap: f.IsAllCap,
IsSmallCap: f.IsSmallCap,
ForceBold: f.ForceBold,
ItalicAngle: f.ItalicAngle,
StemV: -1,
}
if pdf.GetVersion(f.w) == pdf.V1_0 {
// required by PDF 2.0 specification errata, if the font dictionary has a Name entry
// https://pdf-issues.pdfa.org/32000-2-2020/clause09.html#H9.8.1
descriptor.FontName = string(f.DefName)
}
}
// var toUnicode *tounicode.Info
// TODO(voss): construct a toUnicode map, when needed
info := &EmbedInfo{
FontMatrix: f.Font.FontMatrix,
Glyphs: subset,
Resources: f.Resources,
Encoding: encoding,
// ToUnicode: toUnicode,
ResName: f.DefName,
ItalicAngle: f.ItalicAngle,
IsFixedPitch: f.IsFixedPitch,
IsSerif: f.IsSerif,
IsScript: f.IsScript,
ForceBold: f.ForceBold,
IsAllCap: f.IsAllCap,
IsSmallCap: f.IsSmallCap,
}
return info.Embed(f.w, f.Data.(pdf.Reference))
}
// EmbedInfo contains the information needed to embed a type 3 font into a PDF document.
type EmbedInfo struct {
Glyphs map[string]*Glyph
FontMatrix [6]float64
// Encoding (a slice of length 256) is the encoding vector used by the client.
// This is used to construct the `Encoding` entry of the PDF font dictionary.
Encoding []string
// ResName is the resource name for the font.
// This is only used for PDF version 1.0.
ResName pdf.Name
// Resources is the resource dictionary for the font.
Resources *pdf.Resources
ItalicAngle float64
IsFixedPitch bool
IsSerif bool
IsScript bool
ForceBold bool
IsAllCap bool
IsSmallCap bool
// ToUnicode (optional) is a map from character codes to unicode strings.
ToUnicode *cmap.ToUnicode
}
// Embed implements the [font.Dict] interface.
func (info *EmbedInfo) Embed(w pdf.Putter, fontDictRef pdf.Reference) error {
if len(info.FontMatrix) != 6 {
return errors.New("invalid font matrix")
}
fontMatrix := make(pdf.Array, len(info.FontMatrix))
for i, x := range info.FontMatrix {
fontMatrix[i] = pdf.Number(x)
}
charProcs := make(pdf.Dict, len(info.Glyphs))
for name := range info.Glyphs {
charProcs[pdf.Name(name)] = w.Alloc()
}
differences := pdf.Array{}
prev := 256
for code, name := range info.Encoding {
name := pdf.Name(name)
if _, exists := charProcs[name]; !exists {
continue
}
if code != prev+1 {
differences = append(differences, pdf.Integer(code))
}
differences = append(differences, name)
prev = code
}
encoding := pdf.Dict{
"Differences": differences,
}
ww := make([]funit.Int16, 256)
for i := range ww {
name := info.Encoding[i]
g := info.Glyphs[name]
if g != nil {
ww[i] = g.WidthX
}
}
var firstChar pdf.Integer
lastChar := pdf.Integer(255)
for lastChar > 0 && ww[lastChar] == 0 {
lastChar--
}
for firstChar < lastChar && ww[firstChar] == 0 {
firstChar++
}
widths := make(pdf.Array, lastChar-firstChar+1)
for i := range widths {
// TODO(voss): These widths shall be interpreted in glyph space as
// specified by FontMatrix (unlike the widths of a Type 1 font, which
// are in thousandths of a unit of text space). If FontMatrix
// specifies a rotation, only the horizontal component of the
// transformed width shall be used.
widths[i] = pdf.Integer(ww[int(firstChar)+i])
}
// See section 9.6.4 of PDF 32000-1:2008.
fontDict := pdf.Dict{
"Type": pdf.Name("Font"),
"Subtype": pdf.Name("Type3"),
"FontBBox": &pdf.Rectangle{}, // [0 0 0 0] is always valid for Type 3 fonts
"FontMatrix": fontMatrix,
"CharProcs": charProcs,
"Encoding": encoding,
"FirstChar": firstChar,
"LastChar": lastChar,
"Widths": widths,
}
if w.GetMeta().Version == pdf.V1_0 {
fontDict["Name"] = info.ResName
}
if !info.Resources.IsEmpty() {
resources := pdf.AsDict(info.Resources)
fontDict["Resources"] = resources
}
var toUnicodeRef pdf.Reference
if info.ToUnicode != nil {
toUnicodeRef = w.Alloc()
fontDict["ToUnicode"] = toUnicodeRef
}
compressedRefs := []pdf.Reference{fontDictRef}
compressedObjects := []pdf.Object{fontDict}
if pdf.IsTagged(w) {
isSymbolic := true
for name := range charProcs {
if !pdfenc.IsStandardLatin[string(name)] {
isSymbolic = false
break
}
}
fd := &font.Descriptor{
IsFixedPitch: info.IsFixedPitch,
IsSerif: info.IsSerif,
IsSymbolic: isSymbolic,
IsScript: info.IsScript,
IsItalic: info.ItalicAngle != 0,
IsAllCap: info.IsAllCap,
IsSmallCap: info.IsSmallCap,
ForceBold: info.ForceBold,
ItalicAngle: info.ItalicAngle,
StemV: -1,
}
if name, ok := fontDict["Name"].(pdf.Name); ok {
fd.FontName = string(name)
}
fdRef := w.Alloc()
fontDict["FontDescriptor"] = fdRef
fontDescriptor := fd.AsDict()
compressedObjects = append(compressedObjects, fontDescriptor)
compressedRefs = append(compressedRefs, fdRef)
}
err := w.WriteCompressed(compressedRefs, compressedObjects...)
if err != nil {
return pdf.Wrap(err, "Type 3 font dicts")
}
for name, g := range info.Glyphs {
gRef := charProcs[pdf.Name(name)].(pdf.Reference)
stm, err := w.OpenStream(gRef, nil, pdf.FilterCompress{})
if err != nil {
return nil
}
_, err = stm.Write(g.Data)
if err != nil {
return nil
}
err = stm.Close()
if err != nil {
return nil
}
}
if toUnicodeRef != 0 {
err = info.ToUnicode.Embed(w, toUnicodeRef)
if err != nil {
return err
}
}
return nil
}
// Extract extracts information about a type 3 font from a PDF file.
func Extract(r pdf.Getter, dicts *font.Dicts) (*EmbedInfo, error) {
if err := dicts.Type.MustBe(font.Type3); err != nil {
return nil, err
}
res := &EmbedInfo{}
charProcs, err := pdf.GetDict(r, dicts.FontDict["CharProcs"])
if err != nil {
return nil, pdf.Wrap(err, "CharProcs")
}
glyphs := make(map[string]*Glyph, len(charProcs))
for name, ref := range charProcs {
stm, err := pdf.GetStream(r, ref)
if stm == nil {
err = errors.New("stream not found")
}
if err != nil {
return nil, pdf.Wrap(err, fmt.Sprintf("CharProcs[%s]", name))
}
decoded, err := pdf.DecodeStream(r, stm, 0)
if err != nil {
return nil, pdf.Wrap(err, fmt.Sprintf("decoding CharProcs[%s]", name))
}
data, err := io.ReadAll(decoded)
if err != nil {
return nil, pdf.Wrap(err, fmt.Sprintf("reading CharProcs[%s]", name))
}
g := &Glyph{Data: data}
setGlyphGeometry(g, data)
glyphs[string(name)] = g
}
res.Glyphs = glyphs
fontMatrix, err := pdf.GetArray(r, dicts.FontDict["FontMatrix"])
if err != nil {
return nil, pdf.Wrap(err, "FontMatrix")
}
if len(fontMatrix) != 6 {
return nil, errors.New("invalid font matrix")
}
for i, x := range fontMatrix {
xi, err := pdf.GetNumber(r, x)
if err != nil {
return nil, pdf.Wrap(err, fmt.Sprintf("FontMatrix[%d]", i))
}
res.FontMatrix[i] = float64(xi)
}
encoding, err := pdf.GetDict(r, dicts.FontDict["Encoding"])
if err != nil {
return nil, pdf.Wrap(err, "Encoding")
}
differences, err := pdf.GetArray(r, encoding["Differences"])
if err != nil {
return nil, pdf.Wrap(err, "Encoding.Differences")
}
res.Encoding = make([]string, 256)
code := 0
for _, obj := range differences {
obj, err = pdf.Resolve(r, obj)
if err != nil {
return nil, err
}
switch obj := obj.(type) {
case pdf.Integer:
code = int(obj)
case pdf.Name:
name := string(obj)
if _, exists := glyphs[name]; exists && code < 256 {
res.Encoding[code] = name
}
code++
}
}
if info, _ := cmap.ExtractToUnicode(r, dicts.FontDict["ToUnicode"], charcode.Simple); info != nil {
res.ToUnicode = info
}
res.ResName, _ = pdf.GetName(r, dicts.FontDict["Name"])
resources, err := pdf.GetDict(r, dicts.FontDict["Resources"])
if err != nil {
return nil, pdf.Wrap(err, "Resources")
}
res.Resources = &pdf.Resources{}
err = pdf.DecodeDict(r, res.Resources, resources)
if err != nil {
return nil, pdf.Wrap(err, "decoding Resources")
}
if dicts.FontDescriptor != nil {
res.ItalicAngle = dicts.FontDescriptor.ItalicAngle
res.IsFixedPitch = dicts.FontDescriptor.IsFixedPitch
res.IsSerif = dicts.FontDescriptor.IsSerif
res.IsScript = dicts.FontDescriptor.IsScript
res.IsAllCap = dicts.FontDescriptor.IsAllCap
res.IsSmallCap = dicts.FontDescriptor.IsSmallCap
res.ForceBold = dicts.FontDescriptor.ForceBold
}
return res, nil
}