-
Notifications
You must be signed in to change notification settings - Fork 0
/
bdfread.cpp
264 lines (242 loc) · 5.67 KB
/
bdfread.cpp
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
//
// copyright wave.suzuki.z@gmail.com
// since 2021
//
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
namespace
{
///
bool
inrange(uint32_t code)
{
if (code <= 0x7f)
return true; // ascii
if (code >= 0x2000 && code <= 0x206f)
return true; // 記号
if (code >= 0x25a0 && code <= 0x29ff)
return true; // 記号
if (code >= 0x3000 && code <= 0x303f)
return true; // 句読点
if (code >= 0x3040 && code <= 0x309f)
return true; // 平仮名
if (code >= 0x30a0 && code <= 0x30ff)
return true; // カタカナ
if (code >= 0x4e00 && code <= 0x9ffc)
return true; // 漢字
if (code >= 0xfff0 && code <= 0xffef)
return true; // 全角英数字
return false;
}
///
std::string
convert(uint32_t c)
{
char buff[5]{};
int len = 1;
// 0000 0000-0000 007F | 0xxxxxxx
// 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
// 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
// 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
if (c <= 0x7f)
{
buff[0] = c;
}
else if (c <= 0x7ff)
{
buff[0] = 0xc0 | (c >> 6);
buff[1] = 0x80 | (c & 0x3f);
len = 2;
}
else if (c <= 0xffff)
{
buff[0] = 0xe0 | (c >> 12);
buff[1] = 0x80 | ((c >> 6) & 0x3f);
buff[2] = 0x80 | ((c & 0x3f));
len = 3;
}
return buff;
}
/// 文字列nを文字sで区切って配列にして返す
/// 適当に切り貼りするので長い文字列には使用しない(遅い)
std::vector<std::string>
split(const std::string& n, const char s = ' ')
{
std::vector<std::string> l;
size_t pos = 0;
do
{
auto p = n.find(s, pos);
if (p == std::string::npos)
break;
l.emplace_back(n.substr(pos, p - pos));
pos = p + 1;
} while (true);
return l;
}
/// 文字列nがsで始まるか調べて、その通りなら残りの文字列をargに入れて返す
bool
cmp(const std::string& n, const char* s, std::string& arg)
{
int l = std::strlen(s);
if (n.length() < l)
{
arg = "";
return false;
}
for (int i = 0; i < l; i++)
{
if (n[i] != s[i])
{
arg = "";
return false;
}
}
arg = n.substr(l);
return true;
};
/// 一行切り抜き
size_t
cut(const std::string& n, size_t pos, std::string& ret)
{
auto p = n.find('\n', pos);
ret = p != std::string::npos ? n.substr(pos, p - pos) : "";
return p;
}
} // namespace
int
main(int argc, char** argv)
{
if (argc < 2)
{
std::cerr << argv[0] << ": need bdf-file" << std::endl;
return -1;
}
std::filesystem::path fname{argv[1]};
if (fname.extension() != ".bdf")
{
std::cerr << argv[0] << ": not bdf" << std::endl;
return -1;
}
std::ifstream infile{fname};
infile.seekg(0, std::ios_base::end);
int filesize = infile.tellg();
infile.seekg(0, std::ios_base::beg);
std::cout << "filesize: " << filesize << std::endl;
std::string buffer;
buffer.resize(filesize);
infile.read(buffer.data(), buffer.size());
//
size_t fontSize = 16;
size_t count = 0;
std::string encode = "ascii";
// プロパティを読む
size_t pos = 0;
do
{
std::string line, arg;
if (cut(buffer, pos, line) == std::string::npos)
{
std::cerr << "invalid format!" << std::endl;
return -1;
}
pos += line.length() + 1;
if (cmp(line, "PIXEL_SIZE ", arg))
fontSize = std::stoi(arg);
else if (cmp(line, "CHARSET_REGISTRY ", arg))
encode = arg;
else if (cmp(line, "ENDPROPERTIES", arg))
break;
} while (true);
{
// 文字数取得
std::string line, arg;
if (cut(buffer, pos, line) != std::string::npos)
{
if (cmp(line, "CHARS ", arg))
count = std::stoi(arg);
}
pos += line.length() + 1;
}
// info
std::cout << "文字コード: " << encode << std::endl;
std::cout << "フォントサイズ: " << fontSize << std::endl;
std::cout << "収録文字数: " << count << std::endl;
struct font
{
uint32_t code;
std::string utf8;
std::vector<uint32_t> bitmap;
bool operator==(const std::string& str) const { return utf8 == str; }
};
std::vector<font> fontList;
fontList.reserve(count);
font current;
current.bitmap.resize(fontSize);
//
int cnt = 0;
int onBitmap = -1;
while (true)
{
std::string line, arg;
if (cut(buffer, pos, line) == std::string::npos)
break;
if (cmp(line, "STARTCHAR ", arg))
{
cnt++;
}
else if (cmp(line, "ENCODING ", arg))
{
current.code = std::stoi(arg);
current.utf8 = convert(current.code);
}
else if (cmp(line, "BITMAP", arg))
{
onBitmap = 0;
}
else if (cmp(line, "ENDCHAR", arg))
{
onBitmap = -1;
if (inrange(current.code))
fontList.push_back(current);
}
else if (onBitmap >= 0)
{
current.bitmap[onBitmap++] = std::stoi(line, nullptr, 16);
}
pos += line.length() + 1;
}
std::cout << "find: " << fontList.size() << std::endl;
// 検索
if (argc >= 3)
{
std::string n = argv[2];
auto it = std::find(fontList.begin(), fontList.end(), n);
if (it != fontList.end())
{
auto& font = *it;
std::cout << "found: " << n << std::endl;
std::cout << " code: " << font.code << std::endl;
std::cout << " index: " << it - fontList.begin() << std::endl;
for (int y = 0; y < fontSize; y++)
{
uint32_t l = font.bitmap[y];
char xl[33]{};
for (int x = 0; x < 32; x++)
{
xl[x] = l & (1 << (31 - x)) ? '#' : ' ';
}
std::cout << xl << std::endl;
}
}
else
{
std::cout << "not fount: " << n << std::endl;
}
}
return 0;
}