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
|
#define _CRT_SECURE_NO_WARNINGS
#include "Font.h"
string readfile(string filename);
using namespace std;
string Font::font_dir = "../containers/test/fonts/"; // ctest is run from litehtml/build
Font::size_name Font::installed_fonts[] =
{
{ 12, "terminus-ascii-bold-12px.yaff" },
{ 14, "terminus-ascii-bold-14px.yaff" },
{ 16, "terminus-ascii-bold-16px.yaff" },
{ 18, "terminus-ascii-bold-18px.yaff" },
{ 20, "terminus-ascii-bold-20px.yaff" },
{ 22, "terminus-ascii-bold-22px.yaff" },
{ 24, "terminus-ascii-bold-24px.yaff" },
{ 28, "terminus-ascii-bold-28px.yaff" },
{ 32, "terminus-ascii-bold-32px.yaff" },
{ 0 }
};
Font::Font(int size)
{
// find most suitable font
int min_diff = 1000;
int n = 0;
for (int i = 0; installed_fonts[i].size; i++)
{
int diff = abs(installed_fonts[i].size - size);
if (diff < min_diff)
{
min_diff = diff;
n = i;
}
}
load(font_dir + installed_fonts[n].name);
}
Bitmap Font::get_glyph(int ch, web_color color)
{
if (ch < 0 || ch >= 128 || glyphs[ch].width == 0)
{
Bitmap bmp(width, height, web_color::transparent);
bmp.draw_rect(1, 1, width - 2, height - 2, color);
return bmp;
}
else if (color != web_color::black)
{
Bitmap bmp = glyphs[ch];
bmp.replace_color(web_color::black, color);
return bmp;
}
else
{
return glyphs[ch];
}
}
// load .yaff font file in an ad hoc manner (can't parse arbitrary yaff files)
void Font::load(string filename)
{
string text = readfile(filename);
string_vector lines;
split_string(text, lines, "\n");
int i;
// parse header
for (i = 0; i < lines.size(); i++)
{
string line = lines[i];
trim(line);
if (line == "" || line[0] == '#') continue; // skip empty lines and comments
auto sep = line.find(':');
if (sep == -1) return; // line without ':' - error
auto key = line.substr(0, sep); trim(key);
auto val = line.substr(sep + 1); trim(val);
if (val == "") break; // end of header
if (key == "cell-size") sscanf(val.c_str(), "%d %d", &width, &height);
else if (key == "ascent") ascent = atoi(val.c_str());
else if (key == "descent") descent = atoi(val.c_str());
}
// parse glyphs
// only u+NNNN: label is recognized, all others are skipped
auto parse_key = [&]() {
int ch = -1;
for (; i < lines.size(); i++)
{
string line = lines[i];
trim(line);
if (line == "") continue;
if (line.find(':') == -1) break; // start of glyph data
if (line.substr(0, 2) == "u+")
sscanf(line.c_str(), "u+%X:", &ch);
}
return ch;
};
auto parse_glyph = [&](int ch) {
Bitmap& glyph = glyphs[ch] = Bitmap(width, height, web_color::transparent);
for (int y = 0; i < lines.size() && y < height; i++, y++)
{
string line = lines[i];
trim(line);
for (int x = 0; x < min((int)line.size(), width); x++)
{
if (line[x] == '@')
glyph.set_pixel(x, y, web_color::black);
}
}
};
while (i < lines.size())
{
int ch = parse_key();
if (ch < 0 || ch >= 128) break;
parse_glyph(ch);
}
x_height = glyphs['x'].find_picture(web_color::transparent).height;
}
|