-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpcf.go
413 lines (364 loc) · 7.93 KB
/
pcf.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
package pcf
import (
"io"
"fmt"
"log"
"os"
"reflect"
"unsafe"
_ "image"
)
const (
PCF_PROPERTIES = (1 << 0)
PCF_ACCELERATORS = (1 << 1)
PCF_METRICS = (1 << 2)
PCF_BITMAPS = (1 << 3)
PCF_INK_METRICS = (1 << 4)
PCF_BDF_ENCODINGS = (1 << 5)
PCF_SWIDTHS = (1 << 6)
PCF_GLYPH_NAMES = (1 << 7)
PCF_BDF_ACCELERATORS = (1 << 8)
PCF_DEFAULT_FORMAT = 0x00000000
PCF_INKBOUNDS = 0x00000200
PCF_ACCEL_W_INKBOUNDS = 0x00000100
PCF_COMPRESSED_METRICS = 0x00000100
)
type tocEntry struct {
typ uint32
format uint32
size uint32
offset uint32
}
type fileHeader struct {
header [4]byte
tableCount int
}
type MetricEntry struct {
LeftSidedBearing int
RightSidedBearing int
CharWidth int
CharAscent int
CharDescent int
CharAttr int
}
type metricTable struct {
table *tocEntry
format int32
count int
}
func (t *metricTable) read(f io.ReadSeeker) (err error) {
if _, err = f.Seek(int64(t.table.offset), 0); err != nil {
return
}
if err = bread(f, &t.format); err != nil {
return
}
if (t.table.format & PCF_COMPRESSED_METRICS) != 0 {
var count int16
if err = breadSwap(f, &count); err != nil {
return
}
t.count = int(count)
} else {
var count int32
if err = breadSwap(f, &count); err != nil {
return
}
t.count = int(count)
}
return
}
func (t *metricTable) readMetricEntry(r io.ReadSeeker, i int, entry *MetricEntry) (err error) {
if i > t.count {
err = fmt.Errorf("readMetricEntry: out of range (%d of %d)", i, t.count)
return
}
if (t.table.format & PCF_COMPRESSED_METRICS) != 0 {
if _, err = r.Seek(int64(t.table.offset) + 6 + int64(i)*5, 0); err != nil {
return
}
var b [5]byte
if _, err = r.Read(b[:]); err != nil {
return
}
entry.LeftSidedBearing = int(b[0])
entry.LeftSidedBearing -= 0x80
entry.RightSidedBearing = int(b[1])
entry.RightSidedBearing -= 0x80
entry.CharWidth = int(b[2])
entry.CharWidth -= 0x80
entry.CharAscent = int(b[3])
entry.CharAscent -= 0x80
entry.CharDescent = int(b[4])
entry.CharDescent -= 0x80
} else {
if _, err = r.Seek(int64(t.table.offset) + 8 + int64(i)*12, 0); err != nil {
return
}
var b [6]int16
if err = bread(r, b); err != nil {
return
}
entry.LeftSidedBearing = int(b[0])
entry.RightSidedBearing = int(b[1])
entry.CharWidth = int(b[2])
entry.CharAscent = int(b[3])
entry.CharDescent = int(b[4])
entry.CharAttr = int(b[5])
}
return
}
type bitmapTable struct {
table *tocEntry
format int32
count int32
offsets []int32
bitmapSizes [4]int32
}
func (t *bitmapTable) read(r io.ReadSeeker) (err error) {
if _, err = r.Seek(int64(t.table.offset), 0); err != nil {
return
}
if err = bread(r, &t.format); err != nil {
return
}
if err = breadSwap(r, &t.count); err != nil {
return
}
t.offsets = make([]int32, t.count)
if err = breadSwap(r, t.offsets); err != nil {
return
}
if err = breadSwap(r, t.bitmapSizes[:]); err != nil {
return
}
if Debug {
log.Println("bitmap sizes", t.bitmapSizes, t.format&3)
}
return
}
func (t *bitmapTable) readData(r io.ReadSeeker, i int) (b []byte, err error) {
if i+1 > int(t.count) {
err = fmt.Errorf("bitmapReadData: out of range (%d of %d)", i, t.count)
return
}
off := int64(t.table.offset) + int64(8 + 4*len(t.offsets) + 16)
off += int64(t.offsets[i])
size := t.offsets[i+1] - t.offsets[i]
if size < 0 {
err = fmt.Errorf("bitmapReadData: invalid offsets")
return
}
if _, err = r.Seek(off, 0); err != nil {
return
}
b = make([]byte, size)
_, err = r.Read(b)
return
}
type encodingTable struct {
table *tocEntry
format int32
minCharOrByte2 int16
maxCharOrByte2 int16
minByte1 int16
maxByte1 int16
defChar int16
index []int16
}
func (t *encodingTable) read(r io.ReadSeeker) (err error) {
if _, err = r.Seek(int64(t.table.offset), 0); err != nil {
return
}
if err = bread(r, &t.format); err != nil {
return
}
if err = breadSwap(r, &t.minCharOrByte2); err != nil {
return
}
if err = breadSwap(r, &t.maxCharOrByte2); err != nil {
return
}
if err = breadSwap(r, &t.minByte1); err != nil {
return
}
if err = breadSwap(r, &t.maxByte1); err != nil {
return
}
if err = breadSwap(r, &t.defChar); err != nil {
return
}
size := int(t.maxCharOrByte2-t.minCharOrByte2+1) * int(t.maxByte1-t.minByte1+1)
t.index = make([]int16, size)
if err = breadSwap(r, t.index); err != nil {
return
}
if Debug {
log.Println("encoding table:", t.minCharOrByte2, t.maxCharOrByte2, t.minByte1, t.maxByte1, size)
}
return
}
func (t *encodingTable) lookup(i int) (r int, err error) {
b1, b2 := i&0xff, i>>8
off := 0
if b2 == 0 {
off = b1-int(t.minCharOrByte2)
} else {
off = (b2-int(t.minByte1))*int(t.maxCharOrByte2-t.minCharOrByte2+1) +
(b1-int(t.minCharOrByte2))
}
if Debug {
log.Println("lookup", i, off, b1, b2)
}
r = int(t.index[off])
return
}
func _bread(r io.Reader, v interface{}, swap bool) error {
rv := reflect.ValueOf(v)
slice := []byte{}
rslice := (*reflect.SliceHeader)(unsafe.Pointer(&slice))
nelem := 0
switch rv.Type().Kind() {
case reflect.Ptr:
nelem = 1
case reflect.Slice:
nelem = rv.Len()
default:
return fmt.Errorf("_bread: unsupported type")
}
size := int(rv.Type().Elem().Size())
rslice.Data = rv.Pointer()
rslice.Len = size * nelem
rslice.Cap = rslice.Len
n, err := r.Read(slice)
if n != rslice.Len {
return err
}
if swap {
for i := 0; i < nelem; i++ {
start := i*size
for j := 0; j < size/2; j++ {
slice[start+j], slice[start+size-1-j] = slice[start+size-1-j], slice[start+j]
}
}
}
return nil
}
func breadSwap(r io.Reader, v interface{}) error {
return _bread(r, v, true)
}
func bread(r io.Reader, v interface{}) error {
return _bread(r, v, false)
}
type File struct {
encoding *encodingTable
bitmap *bitmapTable
metric *metricTable
f *os.File
}
var Debug bool
func Open(file string) (pf *File, err error) {
var f *os.File
f, err = os.Open(file)
if err != nil {
return
}
var fh fileHeader
if err = bread(f, &fh); err != nil {
return
}
pf = &File{f: f}
var tocMetrics, tocBitmaps *tocEntry
var tocEncoding *tocEntry
if Debug {
log.Println("tableCount:", fh.tableCount)
}
for i := 0; i < fh.tableCount; i++ {
toc := &tocEntry{}
if err = bread(f, toc); err != nil {
return
}
switch toc.typ {
case PCF_METRICS:
tocMetrics = toc
case PCF_BITMAPS:
tocBitmaps = toc
case PCF_BDF_ENCODINGS:
tocEncoding = toc
}
}
if tocMetrics == nil || tocBitmaps == nil {
err = fmt.Errorf("metrics or bitmap toc not found")
log.Println(err)
return
}
if tocEncoding == nil {
err = fmt.Errorf("encoding toc not found")
log.Println(err)
return
}
pf.metric = &metricTable{table: tocMetrics}
if err = pf.metric.read(f); err != nil {
return
}
if Debug {
log.Println("total metric", pf.metric.count)
}
pf.bitmap = &bitmapTable{table: tocBitmaps}
if err = pf.bitmap.read(f); err != nil {
return
}
if Debug {
log.Println("total bitmap glyphs", pf.bitmap.count)
}
pf.encoding = &encodingTable{table: tocEncoding}
if err = pf.encoding.read(f); err != nil {
return
}
return
}
func (pf *File) Lookup(r rune) (b []byte, me MetricEntry, stride int, err error) {
var i int
if i, err = pf.encoding.lookup(int(r)); err != nil {
return
}
if err = pf.metric.readMetricEntry(pf.f, i, &me); err != nil {
return
}
if b, err = pf.bitmap.readData(pf.f, i); err != nil {
return
}
stride = 4
return
}
func (pf *File) DumpAscii(fname string, r rune) {
f, err := os.Create(fname)
if err != nil {
return
}
var b []byte
var me MetricEntry
var stride int
b, me, stride, err = pf.Lookup(r)
if Debug {
log.Println("lookup: len", len(b))
log.Println("lookup: MetricEntry", me)
}
for i := 0; i < len(b); i += stride {
for j := 0; j < stride; j++ {
bits := b[i+j]
for k := 7; k >= 0; k-- {
if j*8+7-k > me.CharWidth {
continue
}
if bits & (1<<byte(k)) != 0 {
f.Write([]byte{'@'})
} else {
f.Write([]byte{'.'})
}
}
}
f.Write([]byte{'\n'})
}
f.Close()
}