/
builtin.go
232 lines (199 loc) · 5.36 KB
/
builtin.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
// seehuhn.de/go/pdf - a library for reading and writing PDF files
// Copyright (C) 2021 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 builtin implements support for the 14 built-in PDF fonts.
package builtin
import (
"errors"
"fmt"
"seehuhn.de/go/pdf"
"seehuhn.de/go/pdf/font"
"seehuhn.de/go/pdf/font/cmap"
"seehuhn.de/go/sfnt/afm"
"seehuhn.de/go/sfnt/glyph"
"seehuhn.de/go/sfnt/type1/names"
)
// Embed loads one of the 14 builtin fonts and embeds it into a PDF file.
// The valid font names are given in [FontNames].
func Embed(w pdf.Putter, fontName string, resName pdf.Name) (font.Embedded, error) {
font, err := Font(fontName)
if err != nil {
return nil, err
}
return font.Embed(w, resName)
}
// EmbedAfm loads a simple Type 1 font described by `afm` and embeds it
// into a PDF file.
func EmbedAfm(w pdf.Putter, afm *afm.Info, resName pdf.Name) (font.Embedded, error) {
font, err := FontAfm(afm)
if err != nil {
return nil, err
}
return font.Embed(w, resName)
}
// Font returns a Font structure representing one of the 14 builtin fonts.
// The valid font names are given in [FontNames].
func Font(fontName string) (font.Font, error) {
afm, err := Afm(fontName)
if err != nil {
return nil, err
}
return FontAfm(afm)
}
// FontAfm returns a Font structure representing a simple Type 1 font,
// described by `afm`.
func FontAfm(afm *afm.Info) (font.Font, error) {
if len(afm.Widths) == 0 {
return nil, errors.New("no glyphs in font")
}
g := &font.Geometry{
UnitsPerEm: 1000,
GlyphExtents: afm.GlyphExtents,
Widths: afm.Widths,
Ascent: afm.Ascent,
Descent: afm.Descent,
BaseLineSkip: 1200, // TODO(voss): is this ok?
// TODO(voss): UnderlinePosition
// TODO(voss): UnderlineThickness
}
isDingbats := afm.FontName == "ZapfDingbats"
cmap := make(map[rune]int)
for gid, name := range afm.GlyphName {
rr := names.ToUnicode(name, isDingbats)
if len(rr) != 1 {
continue
}
r := rr[0]
if j, exists := cmap[r]; exists && afm.GlyphName[j] < name {
// In case two names map to the same rune, use the
// one with the lexicographically earlier name.
continue
}
cmap[r] = gid
}
res := &builtin{
afm: afm,
g: g,
cmap: cmap,
}
return res, nil
}
type builtin struct {
afm *afm.Info
g *font.Geometry
cmap map[rune]int
}
func (f *builtin) GetGeometry() *font.Geometry {
return f.g
}
func (f *builtin) Layout(s string, ptSize float64) glyph.Seq {
rr := []rune(s)
gg := make(glyph.Seq, len(rr))
for i, r := range rr {
gid := f.cmap[r]
gg[i].Gid = glyph.ID(gid)
gg[i].Text = []rune{r}
}
// TODO(voss): merge this into the loop above
if len(gg) >= 2 {
var res glyph.Seq
last := gg[0]
for _, g := range gg[1:] {
lig, ok := f.afm.Ligatures[glyph.Pair{Left: last.Gid, Right: g.Gid}]
if ok {
last.Gid = lig
last.Text = append(last.Text, g.Text...)
} else {
res = append(res, last)
last = g
}
}
gg = append(res, last)
}
for i := range gg {
gg[i].Advance = f.afm.Widths[gg[i].Gid]
}
if len(gg) >= 2 {
for i := 0; i < len(gg)-1; i++ {
kern := f.afm.Kern[glyph.Pair{Left: gg[i].Gid, Right: gg[i+1].Gid}]
gg[i].Advance += kern
}
}
return gg
}
func (f *builtin) Embed(w pdf.Putter, resName pdf.Name) (font.Embedded, error) {
res := &embedded{
builtin: f,
w: w,
ref: w.Alloc(),
resName: resName,
enc: cmap.NewSimpleEncoder(),
}
w.AutoClose(res)
return res, nil
}
type embedded struct {
*builtin
w pdf.Putter
ref pdf.Reference
resName pdf.Name
enc cmap.SimpleEncoder
closed bool
}
func (e *embedded) AppendEncoded(s pdf.String, gid glyph.ID, rr []rune) pdf.String {
return append(s, e.enc.Encode(gid, rr))
}
func (e *embedded) Reference() pdf.Reference {
return e.ref
}
func (e *embedded) ResourceName() pdf.Name {
return e.resName
}
func (e *embedded) Close() error {
if e.closed {
return nil
}
e.closed = true
if e.enc.Overflow() {
return fmt.Errorf("too many distinct glyphs used in font %q (%s)",
e.resName, e.afm.FontName)
}
e.enc = cmap.NewFrozenSimpleEncoder(e.enc)
// See section 9.6.2.1 of PDF 32000-1:2008.
Font := pdf.Dict{
"Type": pdf.Name("Font"),
"Subtype": pdf.Name("Type1"),
"BaseFont": pdf.Name(e.afm.FontName),
}
builtinEncoding := make([]glyph.ID, 256)
glyphID := make(map[string]glyph.ID)
for gid, name := range e.afm.GlyphName {
glyphID[name] = glyph.ID(gid)
}
for code, name := range e.afm.Encoding {
builtinEncoding[code] = glyphID[name]
}
isDingbats := e.afm.FontName == "ZapfDingbats"
enc := font.DescribeEncoding(e.enc.Encoding(), builtinEncoding,
e.afm.GlyphName, isDingbats)
if enc != nil {
Font["Encoding"] = enc
}
if e.w.GetMeta().Version == pdf.V1_0 {
Font["Name"] = e.resName
}
err := e.w.Put(e.ref, Font)
return err
}