/
font.cpp
58 lines (53 loc) · 1.78 KB
/
font.cpp
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
#include "pxt.h"
using namespace pxt;
#ifndef PXT_CREATE_BUFFER
#define PXT_CREATE_BUFFER(data, len) ManagedBuffer(data, len).leakData()
#endif
namespace scrollbit {
//%
int getFontDataByte(int index) {
if(index < 0 || index >= 475){
return 0;
}
#if MICROBIT_CODAL
auto font = codal::BitmapFont::getSystemFont();
#else
auto font = MicroBitFont::getSystemFont();
#endif
return (char)*(font.characters + index);
}
//%
int getCharWidth(int charCode) {
if(charCode < MICROBIT_FONT_ASCII_START || charCode > MICROBIT_FONT_ASCII_END){
return 5;
}
#if MICROBIT_CODAL
auto font = codal::BitmapFont::getSystemFont();
#else
auto font = MicroBitFont::getSystemFont();
#endif
int offset = (charCode - MICROBIT_FONT_ASCII_START) * 5;
uint8_t width = (uint8_t)*(font.characters + offset)
| (uint8_t)*(font.characters + offset + 1)
| (uint8_t)*(font.characters + offset + 2)
| (uint8_t)*(font.characters + offset + 3)
| (uint8_t)*(font.characters + offset + 4);
if (width & 1) { return 5; }
if (width & 2) { return 4; }
if (width & 4) { return 3; }
return 2;
}
//%
Buffer getFontData(int charCode) {
if(charCode < MICROBIT_FONT_ASCII_START || charCode > MICROBIT_FONT_ASCII_END){
return PXT_CREATE_BUFFER(NULL, 5);
}
#if MICROBIT_CODAL
auto font = codal::BitmapFont::getSystemFont();
#else
auto font = MicroBitFont::getSystemFont();
#endif
int offset = (charCode - MICROBIT_FONT_ASCII_START) * 5;
return PXT_CREATE_BUFFER((uint8_t *)(font.characters + offset), 5);
}
}