Repository navigation
Expand file tree
/
Copy pathreference_renderer_ut.cpp
More file actions
285 lines (246 loc) · 9.35 KB
/
Copy pathreference_renderer_ut.cpp
File metadata and controls
285 lines (246 loc) · 9.35 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Copyright (C) 2026 Shitty team
* MIT licensed
* See the file LICENSE.MIT for the full license.
*/
#include "reference_renderer.h"
#include "cell_extra_store.h"
#include "composer.h"
#include "font_pack.h"
#include "options.h"
#include "vterm.h"
#include <std/mem/obj_pool.h>
#include <std/tst/ut.h>
using namespace stl;
namespace {
struct FakeFontpack final: public Fontpack {
u16 getPx() const override;
u16 getPy() const override;
bool hasBold() const override;
bool hasItalic() const override;
bool hasBoldItalic() const override;
bool hasDoubleWidth() const override;
FontGlyph glyph(const u32* codepoints, size_t count, FontStyle style, bool doubleWidth) override;
u8 bitmap[128]{};
size_t bitmapLength = 0;
bool colorGlyph = false;
bool doubleWidthAvailable = true;
u32 received[8]{};
size_t receivedCount = 0;
FontStyle receivedStyle = FontStyle::Regular;
bool receivedDoubleWidth = false;
};
void configure(Composer& composer, FakeFontpack& fonts, u16 columns, u16 rows, u16 glyphWidth, u16 glyphHeight) {
composer.fonts = &fonts;
composer.setGlyphSize(glyphWidth, glyphHeight);
CellExtraStore::create(composer, (size_t)(columns)*rows);
composer.resize((u16)(columns * glyphWidth + 2 * opts.border), (u16)(rows * glyphHeight + 2 * opts.border));
}
Color pixel(const ReferenceImage& image, u16 x, u16 y) {
const size_t index = 3 * ((size_t)(y)*image.width + x);
return {
image.pixels[index],
image.pixels[index + 1],
image.pixels[index + 2],
};
}
Color cellPixel(const ReferenceImage& image, u16 x, u16 y) {
return pixel(image, (u16)(opts.border + x), (u16)(opts.border + y));
}
TerminalCell coloredCell(Color foreground, Color background) {
TerminalCell cell{};
cell.setForeground(CellColor::direct(foreground));
cell.setBackground(CellColor::direct(background));
return cell;
}
}
u16 FakeFontpack::getPx() const {
return 0;
}
u16 FakeFontpack::getPy() const {
return 0;
}
bool FakeFontpack::hasBold() const {
return true;
}
bool FakeFontpack::hasItalic() const {
return true;
}
bool FakeFontpack::hasBoldItalic() const {
return true;
}
bool FakeFontpack::hasDoubleWidth() const {
return doubleWidthAvailable;
}
FontGlyph FakeFontpack::glyph(const u32* codepoints, size_t count, FontStyle style, bool doubleWidth) {
receivedCount = count < 8 ? count : 8;
for (size_t index = 0; index < receivedCount; ++index) {
received[index] = codepoints[index];
}
receivedStyle = style;
receivedDoubleWidth = doubleWidth;
return {
.data = bitmap,
.len = bitmapLength,
.color = colorGlyph,
};
}
STD_TEST_SUITE(ReferenceRenderer) {
STD_TEST(RejectsMismatchedCellCount) {
auto pool = ObjPool::fromMemory();
Composer composer(pool.mutPtr());
FakeFontpack fonts;
configure(composer, fonts, 1, 1, 1, 1);
ReferenceRenderer* renderer = ReferenceRenderer::create(composer);
TerminalColors colors;
TerminalUpdate update;
update.colors = &colors;
const ReferenceImage image = renderer->render(update);
STD_INSIST(image.pixels == nullptr);
STD_INSIST(image.length == 0);
}
STD_TEST(BlendsMonochromeCoverage) {
auto pool = ObjPool::fromMemory();
Composer composer(pool.mutPtr());
FakeFontpack fonts;
fonts.bitmap[0] = 0;
fonts.bitmap[1] = 128;
fonts.bitmap[2] = 255;
fonts.bitmap[3] = 64;
fonts.bitmapLength = 4;
configure(composer, fonts, 1, 1, 2, 2);
ReferenceRenderer* renderer = ReferenceRenderer::create(composer);
TerminalColors colors;
TerminalCell cell = coloredCell({255, 0, 0}, {0, 0, 255});
TerminalCellSpan span{0, 1, &cell};
TerminalUpdate update;
update.spans = &span;
update.spanCount = 1;
update.colors = &colors;
const ReferenceImage image = renderer->render(update);
STD_INSIST((cellPixel(image, 0, 0) == Color{0, 0, 255}));
STD_INSIST((cellPixel(image, 1, 0) == Color{128, 0, 127}));
STD_INSIST((cellPixel(image, 0, 1) == Color{255, 0, 0}));
STD_INSIST((cellPixel(image, 1, 1) == Color{64, 0, 191}));
}
STD_TEST(InverseAndScreenReverseCancelEachOther) {
auto pool = ObjPool::fromMemory();
Composer composer(pool.mutPtr());
FakeFontpack fonts;
fonts.bitmap[0] = 255;
fonts.bitmapLength = 1;
configure(composer, fonts, 1, 1, 1, 1);
ReferenceRenderer* renderer = ReferenceRenderer::create(composer);
TerminalColors colors;
TerminalCell cell = coloredCell({255, 0, 0}, {0, 0, 255});
cell.inverse = true;
TerminalCellSpan span{0, 1, &cell};
TerminalUpdate update;
update.spans = &span;
update.spanCount = 1;
update.colors = &colors;
ReferenceImage image = renderer->render(update);
STD_INSIST((cellPixel(image, 0, 0) == Color{0, 0, 255}));
update.screenReverse = true;
image = renderer->render(update);
STD_INSIST((cellPixel(image, 0, 0) == Color{255, 0, 0}));
}
STD_TEST(AppliesSparseUpdatesToRetainedCells) {
auto pool = ObjPool::fromMemory();
Composer composer(pool.mutPtr());
FakeFontpack fonts;
configure(composer, fonts, 2, 1, 1, 1);
ReferenceRenderer* renderer = ReferenceRenderer::create(composer);
TerminalColors colors;
TerminalCell initial[2]{};
initial[0].setBackground(CellColor::direct({10, 20, 30}));
initial[1].setBackground(CellColor::direct({40, 50, 60}));
TerminalCellSpan span{0, 2, initial};
TerminalUpdate update;
update.spans = &span;
update.spanCount = 1;
update.colors = &colors;
ReferenceImage image = renderer->render(update);
STD_INSIST((cellPixel(image, 0, 0) == Color{10, 20, 30}));
STD_INSIST((cellPixel(image, 1, 0) == Color{40, 50, 60}));
TerminalCell changed = initial[1];
changed.setBackground(CellColor::direct({70, 80, 90}));
span = {1, 1, &changed};
image = renderer->render(update);
STD_INSIST((cellPixel(image, 0, 0) == Color{10, 20, 30}));
STD_INSIST((cellPixel(image, 1, 0) == Color{70, 80, 90}));
}
STD_TEST(AppliesSelectionColors) {
auto pool = ObjPool::fromMemory();
Composer composer(pool.mutPtr());
FakeFontpack fonts;
fonts.bitmap[0] = 255;
fonts.bitmap[1] = 0;
fonts.bitmapLength = 2;
configure(composer, fonts, 1, 1, 2, 1);
ReferenceRenderer* renderer = ReferenceRenderer::create(composer);
TerminalColors colors;
TerminalCell cell = coloredCell({255, 0, 0}, {0, 0, 255});
TerminalCellSpan span{0, 1, &cell};
TerminalUpdate update;
update.spans = &span;
update.spanCount = 1;
update.colors = &colors;
update.snappedSelection = Rect(0, 0);
update.selectionColorMask = 3;
update.selectionForeground = {1, 2, 3};
update.selectionBackground = {4, 5, 6};
const ReferenceImage image = renderer->render(update);
STD_INSIST((cellPixel(image, 0, 0) == Color{1, 2, 3}));
STD_INSIST((cellPixel(image, 1, 0) == Color{4, 5, 6}));
}
STD_TEST(CompositesPremultipliedColorGlyph) {
auto pool = ObjPool::fromMemory();
Composer composer(pool.mutPtr());
FakeFontpack fonts;
fonts.bitmap[0] = 100;
fonts.bitmap[1] = 50;
fonts.bitmap[2] = 0;
fonts.bitmap[3] = 128;
fonts.bitmapLength = 4;
fonts.colorGlyph = true;
configure(composer, fonts, 1, 1, 1, 1);
ReferenceRenderer* renderer = ReferenceRenderer::create(composer);
TerminalColors colors;
TerminalCell cell{};
cell.setBackground(CellColor::direct({0, 0, 100}));
TerminalCellSpan span{0, 1, &cell};
TerminalUpdate update;
update.spans = &span;
update.spanCount = 1;
update.colors = &colors;
const ReferenceImage image = renderer->render(update);
STD_INSIST((cellPixel(image, 0, 0) == Color{100, 50, 49}));
}
STD_TEST(PassesStoredGraphemeAndStyleToFontpack) {
auto pool = ObjPool::fromMemory();
Composer composer(pool.mutPtr());
FakeFontpack fonts;
fonts.bitmap[0] = 0;
fonts.bitmapLength = 1;
configure(composer, fonts, 1, 1, 1, 1);
ReferenceRenderer* renderer = ReferenceRenderer::create(composer);
TerminalColors colors;
const u32 codepoints[] = {'a', 0x0301};
TerminalCell stored{};
stored.bold = true;
stored.italic = true;
composer.cellExtras->setGrapheme(stored, codepoints, 2);
TerminalCellSpan span{0, 1, &stored};
TerminalUpdate update;
update.spans = &span;
update.spanCount = 1;
update.colors = &colors;
renderer->render(update);
STD_INSIST(fonts.receivedCount == 2);
STD_INSIST(fonts.received[0] == codepoints[0]);
STD_INSIST(fonts.received[1] == codepoints[1]);
STD_INSIST(fonts.receivedStyle == FontStyle::BoldItalic);
STD_INSIST(!fonts.receivedDoubleWidth);
}
}