-
Notifications
You must be signed in to change notification settings - Fork 17
/
pdf_ttfont.go
333 lines (304 loc) · 8.62 KB
/
pdf_ttfont.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
// -----------------------------------------------------------------------------
// github.com/balacode/one-file-pdf one-file-pdf/[pdf_ttfont.go]
// (c) [email protected] License: MIT
// -----------------------------------------------------------------------------
// THIS FILE IS A WORK IN PROGRESS
// This file contains a TTF font parser and PDF font-related functionality.
// It augments PDF in pdf_core.go to support Unicode and font embedding,
// but is not required for basic PDF functionality.
// # Module Initialization
// init()
//
// # pdfFontHandler Interface (f *pdfTTFont)
// readFont(owner *PDF, font interface{}) bool
// textWidthPt(s string) float64
// writeText(s string)
// writeFontObjects(font *pdfFont)
package pdf
import (
"bytes"
"fmt"
"io"
"os"
"reflect"
)
// pdfTTFont __
type pdfTTFont struct {
Name string
Data []byte
Err error
HEAD struct { // font header table: general info about the font
UnitsPerEm uint16
XMin int16
YMin int16
XMax int16
YMax int16
}
HHEA struct { // horizontal header: layout of horizontally-written fonts
HMetricCount uint16
Ascent int16
Descent int16
}
MAXP struct { // maximum profile table: specifies memory requirements
NumGlyphs uint16
}
HMTX struct { // horizontal metrics table: width of each glyph
Widths []uint16
}
CMAP struct { // maps character codes to glyph indices
Chars map[int]uint16
}
NAME struct { // font names table
PostScriptName string
}
OS2 struct { // OS/2 compatibility table
Version uint16
STypoAscender int16
STypoDescender int16
STypoLineGap int16
}
POST struct { // glyph name and PostScript font table
ItalicAngle int16
}
LOCA []uint32 // glyph data location table
pdf *PDF
} // pdfTTFont
// -----------------------------------------------------------------------------
// # Module Initialization
// init __
func init() {
// TODO: uncomment this line when font handler is implemented
// pdfNewFontHandler = func() pdfFontHandler { return &pdfTTFont{} }
} // init
// -----------------------------------------------------------------------------
// # pdfFontHandler Interface (f *pdfTTFont)
// readFont loads a font from a file name, slice of bytes, or io.Reader
func (f *pdfTTFont) readFont(owner *PDF, font interface{}) bool {
f.Err = nil
f.pdf = owner
var (
src string
rd io.Reader
)
switch arg := font.(type) {
case string:
{
src = arg
data, err := os.ReadFile(arg)
if err != nil {
f.pdf.putError(0xE5445B, "Failed reading font file", src)
return false
}
rd = bytes.NewReader(data)
}
case []byte:
{
src = fmt.Sprintf("[]byte len(%d)", len(arg))
rd = bytes.NewReader(arg)
}
case io.Reader:
{
src = "io.Reader"
rd = arg
}
default:
f.pdf.putError(0xECEB7B, "Invalid type in arg",
reflect.TypeOf(font).String())
return false
}
_ = rd // TODO: remove when reader is used
return f.Err == nil
} // readFont
// textWidthPt returns the width of text 's' in points
func (f *pdfTTFont) textWidthPt(s string) float64 {
f.Err = nil
var ret float64
for _, r := range s {
_ = r
glyph, found := 0, false // TODO: find out glyph entry
if !found {
// TODO: 0xE0074A: error
continue
}
w := float64(f.HMTX.Widths[glyph])
if f.HEAD.UnitsPerEm != 1000 {
w = w * 1000.0 / float64(f.HEAD.UnitsPerEm)
}
ret += w / 1000.0 * f.pdf.fontSizePt
}
return ret
} // textWidthPt
// writeText encodes text in the string 's'
func (f *pdfTTFont) writeText(s string) {
f.Err = nil
f.pdf.write("BT ", f.pdf.page.x, " ", f.pdf.page.y, " Td ")
//
// TODO: add each rune of s, to determine glyphs to embed
//
// write hex encoded text to PDF
f.pdf.write("[<")
for _, r := range s {
_ = r
glyph, found := 0, false // TODO: find out glyph entry
if !found {
// TODO: 0xE1DC96: error
return
}
f.pdf.write(fmt.Sprintf("%04X", glyph))
}
f.pdf.write(">] TJ ET\n")
} // writeText
// writeFontObjects writes the PDF objects that define the embedded font
func (f *pdfTTFont) writeFontObjects(font *pdfFont) {
f.Err = nil
// TODO: write font-related objects here (call writer methods)
if f.Err != nil {
f.pdf.putError(0xED2CDF, f.Err.Error(), "")
}
} // writeFontObjects
// -----------------------------------------------------------------------------
// # TTF Parsing Methods (f *pdfTTFont)
// readTTF __
func (f *pdfTTFont) readTTF(reader io.Reader) {
if f.Err != nil {
return
}
var err error
f.Data, err = io.ReadAll(reader)
if err != nil {
f.Err = err
return
}
rd := bytes.NewReader(f.Data)
ver := f.read(rd, 4)
if !bytes.Equal(ver, []byte{0, 1, 0, 0}) {
// TODO: 0xE0E9AE: error
return
}
for _, fn := range []func(*bytes.Reader){
f.readHEAD, f.readHHEA, f.readMAXP, f.readHMTX, f.readCMAP,
f.readNAME, f.readOS2, f.readPOST, f.readLOCA} {
if f.Err != nil {
/// 0xE2257E: log error
break
}
fn(rd)
}
} // readTTF
// readHEAD __
func (f *pdfTTFont) readHEAD(rd *bytes.Reader) {
if f.Err != nil {
return
}
// TODO: implement
} // readHEAD
// readHHEA __
func (f *pdfTTFont) readHHEA(rd *bytes.Reader) {
if f.Err != nil {
return
}
// TODO: implement
} // readHHEA
// readMAXP __
func (f *pdfTTFont) readMAXP(rd *bytes.Reader) {
if f.Err != nil {
return
}
// TODO: implement
} // readMAXP
// readHMTX __
func (f *pdfTTFont) readHMTX(rd *bytes.Reader) {
if f.Err != nil {
return
}
// TODO: implement
} // readHMTX
// readCMAP __
func (f *pdfTTFont) readCMAP(rd *bytes.Reader) {
if f.Err != nil {
return
}
// TODO: implement
} // readCMAP
// readNAME __
func (f *pdfTTFont) readNAME(rd *bytes.Reader) {
if f.Err != nil {
return
}
// TODO: implement
} // readNAME
// readOS2 __
func (f *pdfTTFont) readOS2(rd *bytes.Reader) {
if f.Err != nil {
return
}
// TODO: implement
} // readOS2
// readPOST __
func (f *pdfTTFont) readPOST(rd *bytes.Reader) {
if f.Err != nil {
return
}
// TODO: implement
} // readPOST
// readLOCA __
func (f *pdfTTFont) readLOCA(rd *bytes.Reader) {
if f.Err != nil {
return
}
// TODO: implement
} // readLOCA
// read __
func (f *pdfTTFont) read(rd *bytes.Reader, size int, useData ...bool) []byte {
if f.Err != nil {
return nil
}
if len(useData) > 0 && useData[0] == false {
_, err := rd.Seek(int64(size), 1)
if err != nil {
f.Err = err
}
return nil
}
ret := make([]byte, size)
n, err := rd.Read(ret)
if err != nil {
f.Err = err
return nil
}
if n != size {
/// 0xE9B50D: error "END OF FILE DURING READING"
return nil
}
return ret
} // read
// readI16 __
func (f *pdfTTFont) readI16(rd *bytes.Reader) int16 {
ar := f.read(rd, 2)
if f.Err != nil {
return 0
}
ret := int(uint16(ar[0])<<8 | uint16(ar[1]))
if ret >= 32768 {
ret -= 65536
}
return int16(ret)
} // readI16
// readUI16 __
func (f *pdfTTFont) readUI16(rd *bytes.Reader) uint16 {
ar := f.read(rd, 2)
if f.Err != nil {
return 0
}
return uint16(ar[0])<<8 | uint16(ar[1])
} // readUI16
// readUI32 __
func (f *pdfTTFont) readUI32(rd *bytes.Reader) uint32 {
ar := f.read(rd, 4)
if f.Err != nil {
return 0
}
return uint32(ar[0])<<24 | uint32(ar[1])<<16 |
uint32(ar[2])<<8 | uint32(ar[3])
} // readUI32
// end