-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpcf.c
More file actions
375 lines (351 loc) · 9.87 KB
/
pcf.c
File metadata and controls
375 lines (351 loc) · 9.87 KB
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
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "pcf.h"
#define PCF_PROPERTIES (1<<0)
#define PCF_ACCELERATORS (1<<1)
#define PCF_METRICS (1<<2)
#define PCF_BITMAPS (1<<3)
#define PCF_INK_METRICS (1<<4)
#define PCF_BDF_ENCODINGS (1<<5)
#define PCF_SWIDTHS (1<<6)
#define PCF_GLYPH_NAMES (1<<7)
#define PCF_BDF_ACCELERATORS (1<<8)
#define PCF_DEFAULT_FORMAT 0x00000000
#define PCF_INKBOUNDS 0x00000200
#define PCF_ACCEL_W_INKBOUNDS 0x00000100
#define PCF_COMPRESSED_METRICS 0x00000100
#define PCF_BYTE_MASK (1<<2)
#define DEBUG 0
#define bread32_0(f, e) bread32(u32_0, f, e)
#define bread32_1(f, e) bread32(u32_1, f, e)
#define bread16_0(f, e) bread16(u16_0, f, e)
#define bread16_1(f, e) bread16(u16_1, f, e)
static uint8 bread[4];
uint32 u32_0(uint8 * buf) {
uint32 result = 0;
for (int i = 3; i >= 0; i--) {
result <<= 8;
result |= buf[i];
}
return result;
}
uint32 u32_1(uint8 * buf) {
uint32 result = 0;
for (int i = 0; i < 4; i++) {
result <<= 8;
result |= buf[i];
}
return result;
}
uint16 u16_0(uint8 * buf) {
uint16 result = 0;
for (int i = 1; i >= 0; i--) {
result <<= 8;
result |= buf[i];
}
return result;
}
uint16 u16_1(uint8 * buf) {
uint16 result = 0;
for (int i = 0; i < 2; i++) {
result <<= 8;
result |= buf[i];
}
return result;
}
uint32 bread32(uint32 (*r)(), FILE * f, error * err) {
if (fread(bread, 1, 4, f) < 0) {
*err = "fread error";
}
return r(bread);
}
uint32 bread16(uint16 (*r)(), FILE * f, error * err) {
if (fread(bread, 1, 2, f) < 0) {
*err = "fread error";
}
return r(bread);
}
/**
* 读取unicode与字模偏移表。
*/
void pcf_encoding_load(encoding_table * t, FILE * f, error * err) {
#if (DEBUG)
printf("%s offset: %d\n", __FUNCTION__, t->table->offset);
#endif
if (fseek(f, t->table->offset, SEEK_SET) < 0) { *err = "fseek error"; return; }
t->format = bread32_0(f, err);
#if (DEBUG)
printf("%s format: %04X %04X\n", __FUNCTION__, t->format, t->table->format);
#endif
if (*err) { return; }
t->minCharOrByte2 = bread16_1(f, err);
if (*err) { return; }
t->maxCharOrByte2 = bread16_1(f, err);
if (*err) { return; }
t->minByte1 = bread16_1(f, err);
if (*err) { return; }
t->maxByte1 = bread16_1(f, err);
if (*err) { return; }
t->defChar = bread16_1(f, err);
if (*err) { return; }
int32 size = t-> index_count = (t->maxCharOrByte2 - t->minCharOrByte2 + 1) * (t->maxByte1 - t->minByte1 + 1);
int16 * indexes = t->indexes = malloc(sizeof(int16) * size);
for (int i = 0; i < size; i++) {
indexes[i] = bread16_1(f, err);
if (*err) { return; }
}
if ((t->maxCharOrByte2 - t->minCharOrByte2) == 0xff
&& (t->maxByte1 - t->minByte1) == 0xff)
t->direct = 1;
else
t->direct = 0;
#if (DEBUG)
printf("encoding table: %d %d %d %d %d\n", t->minCharOrByte2, t->maxCharOrByte2, t->minByte1, t->maxByte1, size);
#endif
}
/**
* 查找unicode字符对应的字模偏移。
*/
uint32 pcf_encoding_find(encoding_table * t, uint32 r, error * err) {
if (t->direct) {
#if (DEBUG)
printf("%s direct\n", __FUNCTION__);
#endif
return t->indexes[r];
}
uint8 b1 = 0xff & r, b2 = (r >> 8) & 0xff;
uint32 off = 0;
if (b2 == 0) {
off = b1 - t->minCharOrByte2;
} else {
off = (b2 - (uint32)t->minByte1) * (t->maxCharOrByte2 - t->minCharOrByte2 + 1)
+ (b1 - t->minCharOrByte2);
}
#if (DEBUG)
printf("encoding_lookup0: %x %x %x %x\n", r, off, b1, b2);
printf("encoding_lookup1: %x %x %x %x\n", t->minCharOrByte2, t->maxCharOrByte2, t->minByte1, t->maxByte1);
printf("encoding_lookup3: %x %x\n", off, t->indexes[off]);
#endif
return t->indexes[off];
}
/**
* 读取字形偏移表。
*/
void pcf_metric_load(metric_table * t, FILE * f, error * err) {
#if (DEBUG)
printf("%s offset: %d\n", __FUNCTION__, t->table->offset);
#endif
if (fseek(f, t->table->offset, SEEK_SET) < 0) { *err = "fseek error"; return; }
t->format = bread32_0(f, err);
#if (DEBUG)
printf("%s format: %04X %04X\n", __FUNCTION__, t->format, t->table->format);
#endif
if (*err) { return; }
if (t->table->format & PCF_COMPRESSED_METRICS) {
t->count = (t->table->format & PCF_BYTE_MASK)
? bread16_1(f, err) : bread16_0(f, err);
#if (DEBUG)
printf("%s compressed count: %d\n", __FUNCTION__, t->count);
#endif
} else {
t->count = (t->table->format & PCF_BYTE_MASK)
? bread32_1(f, err) : bread32_0(f, err);
#if (DEBUG)
printf("%s uncompressed count: %d\n", __FUNCTION__, t->count);
#endif
}
if (*err) { return; }
}
/**
* 读取字形。
*/
void pcf_metric_find(metric_table * t, FILE * f, uint32 i, metric_entry * me, error * err) {
#if (DEBUG)
printf("%s i: %d, count: %d\n", __FUNCTION__, i, t->count);
#endif
if (i > t->count) { *err = "readMetricEntry: out of range"; return; }
if (t->table->format & PCF_COMPRESSED_METRICS) {
if (fseek(f, (uint64)t->table->offset + 6 + (uint64)i * 5, 0) < 0) { *err = "fseek error"; return; }
uint8 b[5];
fread(b, sizeof(uint8), sizeof(b), f);
me->left_sided_bearing = b[0];
me->left_sided_bearing -= 0x80;
me->right_side_bearing = b[1];
me->right_side_bearing -= 0x80;
me->character_width = b[2];
me->character_width -= 0x80;
me->character_ascent = b[3];
me->character_ascent -= 0x80;
me->character_descent = b[4];
me->character_descent -= 0x80;
} else {
if (fseek(f, (uint64)t->table->offset + 8 + (uint64)i * 12, 0) < 0) { *err = "fseek error"; return; }
uint16 b[6];
fread(b, sizeof(uint16), sizeof(b), f);
me->left_sided_bearing = b[0];
me->right_side_bearing = b[1];
me->character_width = b[2];
me->character_ascent = b[3];
me->character_descent = b[4];
me->character_attributes = b[5];
}
}
/**
* 读取字模偏移表。
*/
void pcf_bitmap_load(bitmap_table * t, FILE * f, error * err) {
#if (DEBUG)
printf("%s offset: %d\n", __FUNCTION__, t->table->offset);
#endif
if (fseek(f, t->table->offset, 0) < 0) { *err = "fseek error"; return; }
t->format = bread32_0(f, err);
#if (DEBUG)
printf("%s format: %04X %04X\n", __FUNCTION__, t->format, t->table->format);
#endif
if (*err) { return; }
int32 count = t->count = bread32_1(f, err);
if (*err) { return; }
int32 * offsets = t->offsets = malloc(sizeof(int32) * count);
for (int i = 0; i < count; i++) {
offsets[i] = bread32_1(f, err);
if (*err) { return; }
}
int32 * bs = t->bitmapSizes;
for (int i = 0; i < 4; i++) {
bs[i] = bread32_1(f, err);
if (*err) { return; }
}
#if (DEBUG)
printf("bitmapSizes: %d %d %d %d, %x\n", bs[0], bs[1], bs[2], bs[3], t->format);
#endif
}
/**
* 读取字模。
*/
void pcf_bitmap_find(bitmap_table * t, FILE * f, uint32 i, bitmap * b, error * err) {
if (i + 1 > t->count) { *err = "readData: out of range"; return; }
uint64 off = (uint64)t->table->offset + (uint64)(8 + 4 * t->count + 16);
off += (uint64)t->offsets[i];
uint32 size = t->offsets[i + 1] - t->offsets[i];
if (size < 0) { *err = "readData: invalid offsets"; return; }
if (fseek(f, off, 0) < 0) { *err = "fseek error"; return; }
uint8 * data = b->data = malloc(size);
b->length = size;
fread(data, size, 1, f);
}
/**
* 释放字模占用内存。
*/
void pcf_bitmap_free(bitmap * b) {
#if (DEBUG)
printf("%s: %p\n", __FUNCTION__, b);
#endif
free(b->data);
}
/**
* 打开字体文件并读取其目录。
*/
void pcf_open(pf * pf, char * path, error * err) {
if (pf->init) { return; } // 已经初始化
FILE * file = fopen(path, "r");
if (!file) { *err = "file not found"; return; }
pf->f = file;
file_header fh;
fread(&fh, sizeof(fh), 1, file);
#if (DEBUG)
printf("%02X%02X%02X%02X\n", fh.header[0], fh.header[1], fh.header[2], fh.header[3]);
printf("tableCount: %d\n", fh.table_count);
#endif
toc_entry * toc_metrics, * toc_bitmaps, * toc_encoding;
for (int i = 0; i < fh.table_count; i++) {
toc_entry * toc = malloc(sizeof(toc_entry)); // minor memory leak
fread(toc, sizeof(toc_entry), 1, file);
// printf("toc->type:%d\n", toc->type);
switch (toc->type) {
case PCF_METRICS:
toc_metrics = toc;
case PCF_BITMAPS:
toc_bitmaps = toc;
case PCF_BDF_ENCODINGS:
toc_encoding = toc;
}
}
if (!toc_metrics || !toc_bitmaps) {
*err = "metrics or bitmap toc not found";
printf("%s\n", *err);
return;
}
if (!toc_encoding) {
*err = "encoding toc not found";
printf("%s\n", *err);
return;
}
metric_table * mt = pf->metric = malloc(sizeof(metric_table));
mt->table = toc_metrics;
pcf_metric_load(mt, file, err);
if (*err) { return; }
#if (DEBUG)
printf("metrics: %d\n", mt->count);
#endif
bitmap_table * bt = pf->bitmap = malloc(sizeof(bitmap_table));
bt->table = toc_bitmaps;
pcf_bitmap_load(bt, file, err);
if (*err) { return; }
#if (DEBUG)
printf("bitmaps: %d\n", bt->count);
#endif
encoding_table * et = pf->encoding = malloc(sizeof(encoding_table));
et->table = toc_encoding;
pcf_encoding_load(et, file, err);
if (*err) { return; }
pf->init = 1;
}
/**
* 释放字体占用内存。TODO
*/
void pcf_free(pf * pf) {
}
/**
* 寻找unicode字符对应的字模和字形信息。
*/
void pcf_lookup(pf * pf, uint32 r, bitmap * b, metric_entry * me, error * err) {
uint32 i = pcf_encoding_find(pf->encoding, r, err);
if (*err) { return; }
pcf_metric_find(pf->metric, pf->f, i, me, err);
if (*err) { return; }
pcf_bitmap_find(pf->bitmap, pf->f, i, b, err);
if (*err) { return; }
// *stride = 4;
}
/**
* 寻找字模并在命令行显示。
*/
void pcf_dump_ascii(pf * pf, char * path, uint32 r, error * err) {
pcf_open(pf, path, err);
if (*err) { return; }
bitmap b;
metric_entry me;
// int32 stride;
pcf_lookup(pf, r, &b, &me, err);
if (*err) { return; }
#if (DEBUG)
printf("lookup: %p %d\n", b.data, b.length);
// printf("lookup: MetricEntry %p\n", me);
#endif
for (int i = 0, stride = 4; i < b.length; i += stride) {
for (int j = 0; j < stride; j++) {
char bits = b.data[i + j];
for (int k = 7; k >= 0; k--) {
if (j * 8 + 7 - k > me.character_width) { continue; }
if (bits & (1<<k)) {
printf("\x1b[47m ");
} else {
printf("\x1b[0m ");
}
}
}
printf("\n");
}
pcf_bitmap_free(&b);
}