forked from cho45/bdfcanvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
executable file
·153 lines (136 loc) · 2.91 KB
/
test.js
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
#!node
var fs = require('fs');
var sys = require('sys');
MockCanvasContext2d = function () { this.init.apply(this, arguments) };
MockCanvasContext2d.prototype = {
init : function (width, height) {
this.width = width;
this.height = height;
var bitmap = new Array(height);
for (var y = 0; y < height; y++) {
bitmap[y] = new Array(width);
for (var x = 0; x < width; x++) {
bitmap[y][x] = 0;
}
}
this.bitmap = bitmap;
this.fillStyle = 1;
},
fillRect : function (sx, sy, w, h) {
var bitmap = this.bitmap;
var ex = sx + w, ey = sy + h;
for (var y = sy; y < ey; y++) {
for (var x = sx; x < ex; x++) {
try {
bitmap[y][x] = this.fillStyle;
} catch (e) {}
}
}
},
clearRect : function (sx, sy, w, h) {
var bitmap = this.bitmap;
var ex = sx + w, ey = sy + h;
for (var y = sy; y < ey; y++) {
for (var x = sx; x < ex; x++) {
try {
bitmap[y][x] = 0;
} catch (e) {}
}
}
},
dumpBitMap : function (sx, sy, w, h) {
var ret = [];
var bitmap = this.bitmap;
var ex = sx + w, ey = sy + h;
for (var y = sy; y < ey; y++) {
var line = "";
for (var x = sx; x < ex; x++) {
try {
line += bitmap[y][x] ? '#' : '.';
} catch (e) {
line += ' ';
}
}
ret.push(line);
}
return ret;
}
};
function sameBitmap (got, expected) {
got = got.join("\n");
expected = expected.join("\n");
if (got == expected) {
console.log("ok");
} else {
console.log("ng");
console.log("got:");
console.log(got.replace(/^/mg, '\t '));
console.log("expected:");
console.log(expected.replace(/^/mg, '\t '));
}
}
var data = fs.readFileSync('bdffont.js', 'ascii');
var bdf = fs.readFileSync('mplus_f10r.bdf', 'ascii');
eval(data);
var ctx = new MockCanvasContext2d(20, 20);
var font = new BDFFont(bdf);
font.drawChar(ctx, 'A'.charCodeAt(0), 1, 10);
sameBitmap(
ctx.dumpBitMap(0, 4, 7, 7),
[
"...#...",
"...#...",
"..#.#..",
"..#.#..",
".#####.",
".#...#.",
".#...#.",
]
);
ctx.clearRect(0, 0, 20, 20);
font.drawText(ctx, 'for', 1, 10);
sameBitmap(
ctx.dumpBitMap(0, 4, 19, 7),
[
"....##.............",
"...#...............",
"...#....###..#.##..",
".#####.#...#.##..#.",
"...#...#...#.#.....",
"...#...#...#.#.....",
"...#....###..#....."
]
);
font.drawEdgeText(ctx, 'for', 1, 10);
sameBitmap(
ctx.dumpBitMap(0, 3, 19, 9),
[
"...####............",
"..#####............",
"..################.",
"###################",
"###################",
"#########.#########",
"..###.#########....",
"..###.#########....",
"..###..########...."
]
);
ctx.fillStyle = 1;
font.drawEdgeText(ctx, 'for', 1, 10);
ctx.fillStyle = 0;
font.drawText(ctx, 'for', 1, 10);
sameBitmap(
ctx.dumpBitMap(0, 3, 19, 9),
[
"...####............",
"..##..#............",
"..#.##############.",
"###.####...##.#..##",
"#.....#.###.#..##.#",
"###.###.#.#.#.#####",
"..#.#.#.###.#.#....",
"..#.#.##...##.#....",
"..###..########...."
]
);