-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
writeTextToCanvasSpec.js
131 lines (105 loc) · 3.69 KB
/
writeTextToCanvasSpec.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
import { Color, writeTextToCanvas } from "../../index.js";
describe("Core/writeTextToCanvas", function () {
it("returns undefined when text is blank", function () {
const canvas = writeTextToCanvas("");
expect(canvas).toBeUndefined();
});
it("throws when text is undefined", function () {
expect(function () {
writeTextToCanvas();
}).toThrowDeveloperError();
});
it("sizes the canvas to fit the text", function () {
const canvas1 = writeTextToCanvas("m");
const canvas2 = writeTextToCanvas("mm");
expect(canvas1.width).not.toEqual(canvas2.width);
expect(canvas1.height).toEqual(canvas2.height);
});
it("allows the text to be either stroked or filled", function () {
const row = 20;
// exact pixel checks are problematic due to browser differences in how text is drawn
// so walk pixels left-to-right and check the number of times that the pixel value changes
// color, ignoring alpha.
function getColorChangeCount(canvas) {
let colorChangeCount = 0;
const context = canvas.getContext("2d");
let pixel = context.getImageData(0, row, 1, 1).data;
let lastRed = pixel[0];
let lastGreen = pixel[1];
let lastBlue = pixel[2];
for (let column = 0; column < canvas.width; ++column) {
pixel = context.getImageData(column, row, 1, 1).data;
let red = pixel[0];
let green = pixel[1];
let blue = pixel[2];
// round up pixels that have been subpixel anti-aliased
if (red > 0 && red !== 255) {
red = 255;
}
if (green > 0 && green !== 255) {
green = 255;
}
if (blue > 0 && blue !== 255) {
blue = 255;
}
if (red !== lastRed || green !== lastGreen || blue !== lastBlue) {
++colorChangeCount;
}
lastRed = red;
lastGreen = green;
lastBlue = blue;
}
return colorChangeCount;
}
const canvas1 = writeTextToCanvas("I", {
font: '90px "Open Sans"',
fill: true,
fillColor: Color.RED,
stroke: false,
});
// canvas1 is filled, completely by the I on the left
// and then has empty space on the right, so there
// should only be one "edge": fill -> outside
let count = getColorChangeCount(canvas1);
expect(count === 1 || count === 2).toEqual(true);
const canvas2 = writeTextToCanvas("I", {
font: '90px "Open Sans"',
fill: false,
stroke: true,
strokeColor: Color.BLUE,
});
// canvas2 is stroked, so there should be three "edges": outline -> inside -> outline -> outside
count = getColorChangeCount(canvas2);
expect(count === 3 || count === 4).toEqual(true);
});
it("background color defaults to transparent", function () {
const canvas = writeTextToCanvas("a", {
font: '90px "Open Sans"',
});
const context = canvas.getContext("2d");
const pixel = context.getImageData(0, 0, 1, 1).data;
expect(pixel).toEqual([0, 0, 0, 0]);
});
it("background can be set", function () {
const canvas = writeTextToCanvas("a", {
font: '90px "Open Sans"',
backgroundColor: Color.RED,
});
const context = canvas.getContext("2d");
const pixel = context.getImageData(0, 0, 1, 1).data;
expect(pixel).toEqual([255, 0, 0, 255]);
});
it("border can be set", function () {
const canvas1 = writeTextToCanvas("a", {
font: '90px "Open Sans"',
backgroundColor: Color.RED,
});
const canvas2 = writeTextToCanvas("a", {
font: '90px "Open Sans"',
backgroundColor: Color.RED,
padding: 2,
});
expect(canvas2.width).toEqual(canvas1.width + 4);
expect(canvas2.height).toEqual(canvas1.height + 4);
});
});