-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleglyf.go
358 lines (311 loc) · 9.65 KB
/
simpleglyf.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
// 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 opentype
import (
"fmt"
"math"
"slices"
"seehuhn.de/go/postscript/funit"
"seehuhn.de/go/sfnt"
sfntcmap "seehuhn.de/go/sfnt/cmap"
"seehuhn.de/go/sfnt/glyf"
"seehuhn.de/go/sfnt/glyph"
"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/subset"
"seehuhn.de/go/pdf/font/truetype"
"seehuhn.de/go/pdf/font/widths"
)
type embeddedGlyfSimple struct {
w pdf.Putter
pdf.Res
*font.Geometry
sfnt *sfnt.Font
layouter *sfnt.Layouter
*encoding.SimpleEncoder
closed bool
}
// Layout implements the [font.Layouter] interface.
func (f *embeddedGlyfSimple) Layout(seq *font.GlyphSeq, ptSize float64, s string) *font.GlyphSeq {
if seq == nil {
seq = &font.GlyphSeq{}
}
buf := f.layouter.Layout(s)
seq.Seq = slices.Grow(seq.Seq, len(buf))
for _, g := range buf {
xOffset := float64(g.XOffset) * ptSize * f.sfnt.FontMatrix[0]
if len(seq.Seq) == 0 {
seq.Skip += xOffset
} else {
seq.Seq[len(seq.Seq)-1].Advance += xOffset
}
seq.Seq = append(seq.Seq, font.Glyph{
GID: g.GID,
Advance: float64(g.Advance) * ptSize * f.sfnt.FontMatrix[0],
Rise: float64(g.YOffset) * ptSize * f.sfnt.FontMatrix[3],
Text: g.Text,
})
}
return seq
}
func (f *embeddedGlyfSimple) ForeachWidth(s pdf.String, yield func(width float64, is_space bool)) {
for _, c := range s {
gid := f.Encoding[c]
width := float64(f.sfnt.GlyphWidth(gid)) / float64(f.sfnt.UnitsPerEm)
yield(width, c == ' ')
}
}
func (f *embeddedGlyfSimple) CodeAndWidth(s pdf.String, gid glyph.ID, rr []rune) (pdf.String, float64, bool) {
width := float64(f.sfnt.GlyphWidth(gid)) / float64(f.sfnt.UnitsPerEm)
c := f.GIDToCode(gid, rr)
return append(s, c), width, c == ' '
}
func (f *embeddedGlyfSimple) Close() error {
if f.closed {
return nil
}
f.closed = true
if f.SimpleEncoder.Overflow() {
return fmt.Errorf("too many distinct glyphs used in font %q (%s)",
f.DefName, f.sfnt.PostScriptName())
}
encoding := f.SimpleEncoder.Encoding
origOTF := f.sfnt.Clone()
origOTF.CMapTable = nil
origOTF.Gdef = nil
origOTF.Gsub = nil
origOTF.Gpos = nil
// subset the font
subsetGID := f.SimpleEncoder.Subset()
subsetTag := subset.Tag(subsetGID, origOTF.NumGlyphs())
subsetOTF, err := origOTF.Subset(subsetGID)
if err != nil {
return fmt.Errorf("font subset: %w", err)
}
subsetGid := make(map[glyph.ID]glyph.ID)
for gNew, gOld := range subsetGID {
subsetGid[gOld] = glyph.ID(gNew)
}
subsetEncoding := make([]glyph.ID, 256)
for i, gid := range encoding {
subsetEncoding[i] = subsetGid[gid]
}
m := f.SimpleEncoder.ToUnicode()
toUnicode := cmap.NewToUnicode(charcode.Simple, m)
// TODO(voss): check whether a ToUnicode CMap is actually needed
info := FontDictGlyfSimple{
Font: subsetOTF,
SubsetTag: subsetTag,
Encoding: subsetEncoding,
ToUnicode: toUnicode,
}
return info.Embed(f.w, f.Data.(pdf.Reference))
}
// FontDictGlyfSimple is the information needed to embed a simple OpenType/glyf font.
type FontDictGlyfSimple struct {
// Font is the font to embed (already subsetted, if needed).
Font *sfnt.Font
// SubsetTag should be a unique tag for the font subset,
// or the empty string if this is the full font.
SubsetTag string
// Encoding is the encoding vector used by the client (a slice of length 256).
// Together with the font's built-in encoding, this is used to determine
// the `Encoding` entry of the PDF font dictionary.
Encoding []glyph.ID
ForceBold bool
IsAllCap bool
IsSmallCap bool
// ToUnicode (optional) is a map from character codes to unicode strings.
ToUnicode *cmap.ToUnicode
}
// Embed adds the font to a PDF file.
//
// This implements the [font.Dict] interface.
func (info *FontDictGlyfSimple) Embed(w pdf.Putter, fontDictRef pdf.Reference) error {
err := pdf.CheckVersion(w, "simple OpenType/glyf fonts", pdf.V1_6)
if err != nil {
return err
}
otf := info.Font.Clone()
if !otf.IsGlyf() {
return fmt.Errorf("not an OpenType/glyf font")
}
fontName := otf.PostScriptName()
if info.SubsetTag != "" {
fontName = info.SubsetTag + "+" + fontName
}
unitsPerEm := otf.UnitsPerEm
ww := make([]float64, 256)
q := 1000 / float64(unitsPerEm)
for i := range ww {
ww[i] = float64(otf.GlyphWidth(info.Encoding[i])) * q
}
widthsInfo := widths.EncodeSimple(ww)
// Mark the font as "symbolic", and use a (1, 0) "cmap" subtable to map
// character codes to glyphs.
//
// TODO(voss): also try the two allowed encodings for "non-symbolic" fonts.
//
// TODO(voss): revisit this, once
// https://github.com/pdf-association/pdf-issues/issues/316 is resolved.
isSymbolic := true
subtable := sfntcmap.Format4{}
for i, gid := range info.Encoding {
if gid == 0 {
continue
}
subtable[uint16(i)] = gid
}
otf.CMapTable = sfntcmap.Table{
{PlatformID: 1, EncodingID: 0}: subtable.Encode(0),
}
bbox := otf.BBox()
fontBBox := &pdf.Rectangle{
LLx: bbox.LLx.AsFloat(q),
LLy: bbox.LLy.AsFloat(q),
URx: bbox.URx.AsFloat(q),
URy: bbox.URy.AsFloat(q),
}
widthsRef := w.Alloc()
fontDescriptorRef := w.Alloc()
fontFileRef := w.Alloc()
// See section 9.6.2.1 of PDF 32000-1:2008.
fontDict := pdf.Dict{
"Type": pdf.Name("Font"),
"Subtype": pdf.Name("TrueType"),
"BaseFont": pdf.Name(fontName),
"FirstChar": widthsInfo.FirstChar,
"LastChar": widthsInfo.LastChar,
"Widths": widthsRef,
"FontDescriptor": fontDescriptorRef,
}
var toUnicodeRef pdf.Reference
if info.ToUnicode != nil {
toUnicodeRef = w.Alloc()
fontDict["ToUnicode"] = toUnicodeRef
}
fd := &font.Descriptor{
FontName: fontName,
IsFixedPitch: otf.IsFixedPitch(),
IsSerif: otf.IsSerif,
IsSymbolic: isSymbolic,
IsScript: otf.IsScript,
IsItalic: otf.IsItalic,
IsAllCap: info.IsAllCap,
IsSmallCap: info.IsSmallCap,
ForceBold: info.ForceBold,
FontBBox: fontBBox,
ItalicAngle: otf.ItalicAngle,
Ascent: otf.Ascent.AsFloat(q),
Descent: otf.Descent.AsFloat(q),
CapHeight: otf.CapHeight.AsFloat(q),
MissingWidth: widthsInfo.MissingWidth,
}
fontDescriptor := fd.AsDict()
fontDescriptor["FontFile3"] = fontFileRef
compressedRefs := []pdf.Reference{fontDictRef, fontDescriptorRef, widthsRef}
compressedObjects := []pdf.Object{fontDict, fontDescriptor, widthsInfo.Widths}
err = w.WriteCompressed(compressedRefs, compressedObjects...)
if err != nil {
return pdf.Wrap(err, "simple OpenType/glyf font dicts")
}
// See section 9.9 of PDF 32000-1:2008 for details.
length1 := pdf.NewPlaceholder(w, 10)
fontFileDict := pdf.Dict{
"Length1": length1, // TODO(voss): needed?
"Subtype": pdf.Name("OpenType"),
}
fontFileStream, err := w.OpenStream(fontFileRef, fontFileDict, pdf.FilterCompress{})
if err != nil {
return err
}
n, err := otf.WriteTrueTypePDF(fontFileStream)
if err != nil {
return fmt.Errorf("embedding TrueType font %q: %w", fontName, err)
}
err = length1.Set(pdf.Integer(n))
if err != nil {
return err
}
err = fontFileStream.Close()
if err != nil {
return err
}
if toUnicodeRef != 0 {
err = info.ToUnicode.Embed(w, toUnicodeRef)
if err != nil {
return err
}
}
return nil
}
// ExtractGlyfSimple extracts information about a simple OpenType font.
func ExtractGlyfSimple(r pdf.Getter, dicts *font.Dicts) (*FontDictGlyfSimple, error) {
if err := dicts.Type.MustBe(font.OpenTypeGlyfSimple); err != nil {
return nil, err
}
res := &FontDictGlyfSimple{}
baseFont, _ := pdf.GetName(r, dicts.FontDict["BaseFont"])
if m := subset.TagRegexp.FindStringSubmatch(string(baseFont)); m != nil {
res.SubsetTag = m[1]
}
if dicts.FontProgram != nil {
stm, err := pdf.DecodeStream(r, dicts.FontProgram, 0)
if err != nil {
return nil, pdf.Wrap(err, "uncompressing OpenType/glyf font stream")
}
otf, err := sfnt.Read(stm)
if err != nil {
return nil, pdf.Wrap(err, "decoding OpenType/glyf font")
}
_, ok := otf.Outlines.(*glyf.Outlines)
if !ok {
return nil, fmt.Errorf("expected glyf outlines, got %T", otf.Outlines)
}
if otf.FamilyName == "" {
otf.FamilyName = dicts.FontDescriptor.FontFamily
}
if otf.Width == 0 {
otf.Width = dicts.FontDescriptor.FontStretch
}
if otf.Weight == 0 {
otf.Weight = dicts.FontDescriptor.FontWeight
}
q := 1000 / float64(otf.UnitsPerEm)
if otf.CapHeight == 0 {
capHeight := dicts.FontDescriptor.CapHeight
otf.CapHeight = funit.Int16(math.Round(float64(capHeight) / q))
}
if otf.XHeight == 0 {
xHeight := dicts.FontDescriptor.XHeight
otf.XHeight = funit.Int16(math.Round(float64(xHeight) / q))
}
res.Font = otf
}
if res.Font != nil {
res.Encoding = truetype.ExtractEncoding(r, dicts.FontDict["Encoding"], res.Font)
}
res.IsAllCap = dicts.FontDescriptor.IsAllCap
res.IsSmallCap = dicts.FontDescriptor.IsSmallCap
res.ForceBold = dicts.FontDescriptor.ForceBold
if info, _ := cmap.ExtractToUnicode(r, dicts.FontDict["ToUnicode"], charcode.Simple); info != nil {
res.ToUnicode = info
}
return res, nil
}