-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathFrameBuffer.cpp
More file actions
360 lines (328 loc) · 10.1 KB
/
Copy pathFrameBuffer.cpp
File metadata and controls
360 lines (328 loc) · 10.1 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <Arduino.h>
#include <stdlib.h>
#include "FrameBuffer.h"
#include "Utf8Iterator.h"
FrameBuffer::FrameBuffer(uint32_t nativeWidth, uint32_t nativeHeight)
{
_nativeWidth = nativeWidth;
_nativeHeight = nativeHeight;
_length = nativeWidth * nativeHeight / 4;
setRotation(ROTATION_0);
setAlpha(NO_ALPHA);
data = new uint8_t[_length];
clear();
}
FrameBuffer::~FrameBuffer()
{
delete[] data;
}
void FrameBuffer::clear(Color color)
{
memset(data, (color << 6) | (color << 4) | (color << 2) | color, _length);
}
void FrameBuffer::test()
{
memset(
data,
(BLACK << 6) | (BLACK << 4) | (BLACK << 2) | BLACK,
_length / 4
);
memset(
&data[_length / 4],
(DGREY << 6) | (DGREY << 4) | (DGREY << 2) | DGREY,
_length / 4
);
memset(
&data[_length / 2],
(LGREY << 6) | (LGREY << 4) | (LGREY << 2) | LGREY,
_length / 4
);
memset(
&data[_length * 3 / 4],
(WHITE << 6) | (WHITE << 4) | (WHITE << 2) | WHITE,
_length / 4
);
}
size_t FrameBuffer::getPixelIndex(int32_t x, int32_t y) const
{
if (x < 0 || x >= _width || y < 0 || y >= _height) {
return SIZE_MAX;
}
switch (_rotation) {
case ROTATION_90:
std::swap(x, y);
x = _nativeWidth - 1 - x;
break;
case ROTATION_180:
x = _nativeWidth - 1 - x;
y = _nativeHeight - 1 - y;
break;
case ROTATION_270:
std::swap(x, y);
y = _nativeHeight - 1 - y;
break;
default:
break;
}
return _nativeWidth * y + x;
}
uint8_t FrameBuffer::getPx(int32_t x, int32_t y) const
{
const size_t i = getPixelIndex(x, y);
if (i != SIZE_MAX) {
return (data[i / 4] >> ((3 - i % 4) * 2)) & 0b11;
} else {
return 0;
}
}
void FrameBuffer::setPx(int32_t x, int32_t y, Color color)
{
const size_t i = getPixelIndex(x, y);
if (i != SIZE_MAX) {
uint8_t *fb = &data[i / 4];
const uint8_t shift = (3 - i % 4) * 2;
*fb &= ~(0b11 << shift);
*fb |= (color & 0b11) << shift;
}
}
void FrameBuffer::drawImage(const Image &image, int32_t x, int32_t y, Align align)
{
ImageReader reader = ImageReader(image);
adjustAlignment(&x, &y, image.width, image.height, align);
Color color;
uint32_t y_dst;
for (uint32_t y_src = 0; y_src < image.height; ++y_src) {
y_dst = y + y_src;
for (uint32_t x_src = 0; x_src < image.width; ++x_src) {
color = static_cast<Color>(reader.next());
if (color != _alpha) {
setPx(x + x_src, y_dst, color);
}
}
}
}
uint32_t FrameBuffer::measureText(String str, const Font &font, int32_t tracking)
{
if (str.length() == 0) {
return 0;
}
uint32_t length = 0;
Utf8Iterator it = Utf8Iterator(str);
uint16_t cp;
while ((cp = it.next())) {
if (Utf8Iterator::isSpaceCodePoint(cp)) {
length += font.spaceWidth + tracking;
} else {
const FontGlyph glyph = font.getGlyph(cp);
length += glyph.width + glyph.left + tracking;
}
}
return length - tracking;
}
std::vector<String> FrameBuffer::wordWrap(String str, const Font &font, uint32_t maxLineLength, int32_t tracking)
{
std::vector<String> lines;
if (str.length() == 0) {
return lines;
}
unsigned int lineStart = 0, safeLineEnd = 0;
uint32_t length = 0, safeLength = 0;
Utf8Iterator it = Utf8Iterator(str);
uint16_t cp;
while ((cp = it.next())) {
if (Utf8Iterator::isNewlineCodePoint(cp)) {
if (maxLineLength > 0 && length > maxLineLength + tracking) {
// Wrap at the last word too
lines.push_back(str.substring(lineStart, safeLineEnd - 1));
lineStart = safeLineEnd;
}
// Wrap here
lines.push_back(str.substring(lineStart, it.getCurrentPosition() - 1));
lineStart = safeLineEnd = it.getCurrentPosition();
length = safeLength = 0;
} else if (Utf8Iterator::isSpaceCodePoint(cp)) {
if (maxLineLength > 0 && length > maxLineLength + tracking) {
if (safeLineEnd == lineStart) {
// Line cannot be word wrapped, so wrap at current position
lines.push_back(str.substring(lineStart, it.getCurrentPosition() - 1));
lineStart = it.getCurrentPosition();
length = 0;
} else {
// Wrap at last word
lines.push_back(str.substring(lineStart, safeLineEnd - 1));
lineStart = safeLineEnd;
length -= safeLength;
}
} else {
length += font.spaceWidth + tracking;
}
safeLineEnd = it.getCurrentPosition();
safeLength = length;
} else {
const FontGlyph glyph = font.getGlyph(cp);
length += glyph.width + glyph.left + tracking;
}
}
if (lineStart < str.length()) {
lines.push_back(str.substring(lineStart));
}
return lines;
}
void FrameBuffer::drawText(String str, const Font &font, int32_t x, int32_t y, Align align, int32_t tracking)
{
if (align != TOP_LEFT) {
uint32_t width;
// Measurement isn't needed and width isn't used by adjustAligment if horizontal alignment is left
if (!(align & _ALIGN_LEFT)) {
width = measureText(str, font);
}
adjustAlignment(&x, &y, width, font.ascent + font.descent, align);
}
Utf8Iterator it = Utf8Iterator(str);
uint16_t cp;
while ((cp = it.next())) {
if (Utf8Iterator::isSpaceCodePoint(cp)) {
x += font.spaceWidth + tracking;
} else {
const FontGlyph glyph = font.getGlyph(cp);
x += glyph.left;
drawImage(glyph, x, y + glyph.top);
x += glyph.width + tracking;
}
}
}
void FrameBuffer::drawMultilineText(
String str,
const Font &font,
int32_t x,
int32_t y,
uint32_t maxLineLength,
Align align,
int32_t tracking,
int32_t leading
) {
// This implementation is simple because it assumes justification equals the horizontal alignment,
// and that's all I needed it to do.
leading += font.ascent + font.descent;
std::vector<String> lines = wordWrap(str, font, maxLineLength, tracking);
if (!(align & _ALIGN_TOP)) {
adjustAlignment(&x, &y, 0, leading * lines.size(), align);
}
// Use top alignment for each line since that axis has already been adjusted for the whole block
align = (Align)(align & ~_ALIGN_BOTTOM & ~_ALIGN_VCENTER | _ALIGN_TOP);
for (String str : lines) {
drawText(str, font, x, y, align, tracking);
y += leading;
}
}
void FrameBuffer::drawQrCode(qrcodegen::QrCode qrcode, int32_t x, int32_t y, int32_t scale, Align align)
{
const int32_t size = qrcode.getSize() * scale;
adjustAlignment(&x, &y, size, size, align);
Color color;
int32_t y2;
for(int y1 = 0; y1 < size; ++y1) {
y2 = y1 / scale;
for (int x1 = 0; x1 < size; ++x1) {
setPx(x + x1, y + y1, qrcode.getModule(x1 / scale, y2) ? BLACK : WHITE);
}
}
}
void FrameBuffer::drawHLine(int32_t x, int32_t y, int32_t length, uint32_t thickness, Color color, Align align)
{
if (length < 0) {
x += length;
length = -length;
}
adjustAlignment(&x, &y, length, thickness, align);
const int32_t ymax = y + thickness;
int32_t i, y1;
for (i = 0; i < length; ++i) {
for (y1 = y; y1 < ymax; ++y1) {
setPx(x + i, y1, color);
}
}
}
void FrameBuffer::drawVLine(int32_t x, int32_t y, int32_t length, uint32_t thickness, Color color, Align align)
{
if (length < 0) {
y += length;
length = -length;
}
adjustAlignment(&x, &y, thickness, length, align);
const int32_t xmax = x + thickness;
int32_t i, x1;
for (i = 0; i < length; ++i) {
for (x1 = x; x1 < xmax; ++x1) {
setPx(x1, y + i, color);
}
}
}
void FrameBuffer::fillRect(int32_t x, int32_t y, int32_t width, int32_t height, Color color, Align align)
{
if (width < 0) {
x += width;
width = -width;
}
if (height < 0) {
y += height;
height = -height;
}
adjustAlignment(&x, &y, width, height, align);
int32_t x2 = x + width, y2 = y + height;
for (int xi = x; xi < x2; ++xi) {
for (int yi = y; yi < y2; ++yi) {
setPx(xi, yi, color);
}
}
}
void FrameBuffer::strokeRect(int32_t x, int32_t y, int32_t width, int32_t height, uint32_t strokeWidth, Color color, bool strokeOutside, Align align)
{
if (width < 0) {
x += width;
width = -width;
}
if (height < 0) {
y += height;
height = -height;
}
adjustAlignment(&x, &y, width, height, align);
if (strokeOutside) {
x -= strokeWidth;
y -= strokeWidth;
width += strokeWidth * 2;
height += strokeWidth * 2;
}
drawHLine(x, y, width, strokeWidth, color, TOP_LEFT);
drawVLine(x, y, height, strokeWidth, color, TOP_LEFT);
drawHLine(x, y + height, width, strokeWidth, color, BOTTOM_LEFT);
drawVLine(x + width, y, height, strokeWidth, color, TOP_RIGHT);
}
void FrameBuffer::adjustAlignment(int32_t *x, int32_t *y, int32_t width, int32_t height, Align align)
{
if (align & _ALIGN_HCENTER) {
*x -= width / 2;
} else if (align & _ALIGN_RIGHT) {
*x -= width;
}
if (align & _ALIGN_VCENTER) {
*y -= height / 2;
} else if (align & _ALIGN_BOTTOM) {
*y -= height;
}
}
void FrameBuffer::setRotation(Rotation rotation)
{
_rotation = rotation;
if (rotation == ROTATION_0 || rotation == ROTATION_180) {
_width = _nativeWidth;
_height = _nativeHeight;
} else {
_width = _nativeHeight;
_height = _nativeWidth;
}
}
void FrameBuffer::setAlpha(uint8_t alpha)
{
_alpha = alpha;
}