-
Notifications
You must be signed in to change notification settings - Fork 0
/
jdvReader.c
109 lines (98 loc) · 3.01 KB
/
jdvReader.c
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
//
// Created by david on 2021/5/20.
//
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "fontObject.h"
#include "jdvReader.h"
#define SET1 128
#define SET_RULE 132
#define BOP 139
#define EOP 140
#define XXX1 239
#define FONT_DEF1 243
#define PRE 247
int numPage;
int paperWidth = 595;
int paperHeight = 842;
FILE* inFile;
struct FontTable {
int size;
Font* font;
} fontTable[64];
inline static int readJdvInt(int size, FILE* f)
{
int result = 0;
for (int i=0; i<size; ++i)
result = (result << 8) + fgetc(f);
return result;
}
/**
* 第一次扫描。用于记录页数和所有字体命令。
* @param fileName 文件名
*/
void parse1(const char* fileName)
{
int32_t pointer;
int tmp;
char buffer[512];
inFile = fopen(fileName, "rb");
if (!inFile)
{
fputs("找不到指定的文件。", stderr);
exit(1);
}
// 寻找文件尾
fseek(inFile, -5, SEEK_END); // 跳过固定的4字节
while (fgetc(inFile) == 223)
fseek(inFile, -2, SEEK_CUR); // 向前一字节
// 新读的这个字节肯定是identification byte
fseek(inFile, -5, SEEK_CUR);
pointer = readJdvInt(4, inFile); // postamble的第一字节
fseek(inFile, pointer + 1, SEEK_SET);
numPage = 0;
while ((pointer = readJdvInt(4, inFile)) != -1)
{
++numPage;
fseek(inFile, pointer + 41, SEEK_SET);
}
printf("%d", numPage);
// 从头开始寻找各类font_def命令
fseek(inFile, 0x0El, SEEK_SET);
tmp = fgetc(inFile); // comment长度
fseek(inFile, tmp, SEEK_CUR); // 第一个BOP位置
while ((tmp = fgetc(inFile)) != -1)
{
if (tmp < SET1){} // 1~127号,输出字符
else if (tmp < SET_RULE) // 128~131号,127以后的字符
fseek(inFile, tmp - 127, SEEK_CUR);
else if (tmp == BOP) // 139号命令表示BOP
fseek(inFile, 44l, SEEK_CUR);
else if (tmp >= XXX1 && tmp < FONT_DEF1) // 239~242;注释、special
{
tmp = readJdvInt(tmp - XXX1 + 1, inFile);
fseek(inFile, tmp, SEEK_CUR);
}
else if (tmp >= FONT_DEF1 && tmp < PRE) // 字体定义
{
tmp = readJdvInt(tmp - FONT_DEF1 + 1, inFile);
struct FontTable* p = fontTable + tmp; // 指向相应的序号
fseek(inFile, 4l, SEEK_CUR); // 不再用checksum
p->size = readJdvInt(4, inFile);
fseek(inFile, 4l, SEEK_CUR); // 不再用TFM中的size
tmp = fgetc(inFile);
tmp += fgetc(inFile); // 整个目录的长度
fread(buffer, 1, tmp, inFile);
buffer[tmp] = 0; // 字符串结尾
if (*buffer == ':') // 表示有TTC中的字体序号
{
char* pos = strchr(buffer, ':');
*pos = 0;
p->font = fontFromFile(pos+1, atoi(buffer + 1));
}
else p->font = fontFromFile(buffer, 0);
}
}
}