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
|
import { describe, it } from "node:test";
import assert from "node:assert";
import { createCanvas } from "canvas";
import { parse } from "wmap-parser";
import { renderToCanvas, renderToImage } from "../src/index.js";
import { StageType } from "../src/stage_type.js";
import fs from "fs";
const wmapSource = fs.readFileSync("./test/example.wmap", "utf-8");
describe("wmap-renderer-canvas", () => {
describe("renderToCanvas", () => {
it("should render a map to canvas", async () => {
const map = parse(wmapSource);
const canvas = createCanvas(1384, 1084);
await renderToCanvas(map, canvas);
assert.strictEqual(canvas.width, 1384);
assert.strictEqual(canvas.height, 1084);
});
it("should throw error without map", async () => {
const canvas = createCanvas(1384, 1084);
await assert.rejects(
async () => await renderToCanvas(null, canvas),
{ message: "Map object is required" },
);
});
it("should throw error without canvas", async () => {
const map = parse(wmapSource);
await assert.rejects(async () => await renderToCanvas(map, null), {
message: "Canvas is required",
});
});
it("should render without background when option is set", async () => {
const map = parse(wmapSource);
const canvas = createCanvas(1384, 1084);
await renderToCanvas(map, canvas, StageType.ACTIVITIES, {
showBackground: false,
});
assert.strictEqual(canvas.width, 1384);
assert.strictEqual(canvas.height, 1084);
});
});
describe("renderToImage", () => {
it("should render a map to PNG stream", async () => {
const map = parse(wmapSource);
const stream = await renderToImage(map, "png");
assert.ok(stream);
const out = fs.createWriteStream("test-output.png");
stream.pipe(out);
await new Promise((resolve, reject) => {
out.on("finish", resolve);
out.on("error", reject);
});
});
it("should render a map to JPEG stream", async () => {
const map = parse(wmapSource);
const stream = await renderToImage(map, "jpeg");
assert.ok(stream);
const out = fs.createWriteStream("test-output.jpeg");
stream.pipe(out);
await new Promise((resolve, reject) => {
out.on("finish", resolve);
out.on("error", reject);
});
});
it("should render a map to PDF stream", async () => {
const map = parse(wmapSource);
const stream = await renderToImage(map, "pdf");
assert.ok(stream);
const out = fs.createWriteStream("test-output.pdf");
stream.pipe(out);
await new Promise((resolve, reject) => {
out.on("finish", resolve);
out.on("error", reject);
});
});
it("should render without background when option is set", async () => {
const map = parse(wmapSource);
const stream = await renderToImage(
map,
"png",
StageType.ACTIVITIES,
{
showBackground: false,
},
);
assert.ok(stream);
const out = fs.createWriteStream("test-output-no-bg.png");
stream.pipe(out);
await new Promise((resolve, reject) => {
out.on("finish", resolve);
out.on("error", reject);
});
});
it("should throw error for invalid image type", async () => {
const map = parse(wmapSource);
await assert.rejects(
async () => await renderToImage(map, "invalid"),
{
message:
"Invalid image type. Must be one of: png, jpeg, pdf",
},
);
});
it("should throw error without map", async () => {
await assert.rejects(async () => await renderToImage(null, "png"), {
message: "Map object is required",
});
});
});
});
|