Support color in text
This commit is contained in:
@@ -28,43 +28,58 @@ namespace FontWriter {
|
||||
}
|
||||
}
|
||||
|
||||
void write(int16_t x, int16_t y, const char* text) {
|
||||
Position write(Position pos, const char* text) {
|
||||
static const auto parse_esc = [](const char* text, const GPU::Color24& color) -> pair<const char*, GPU::Color24> {
|
||||
static const auto char_to_color = [](char number) constexpr -> uint8_t {
|
||||
return (1 << (number - '0')) - 1;
|
||||
};
|
||||
if(text[0] != '[' || text[2] != ';' || text[4] != ';' || text[6] != 'm') {
|
||||
return {text, color};
|
||||
}
|
||||
|
||||
return {text + 7, GPU::Color24(char_to_color(text[1]), char_to_color(text[3]), char_to_color(text[5]))};
|
||||
};
|
||||
const auto* cur_text_end = &TextBuffer[GPU::Display::current_id][TextBufferSize];
|
||||
const auto org_x = x;
|
||||
const auto org_x = pos.x;
|
||||
auto font_color = GPU::Color24::Grey();
|
||||
|
||||
while(TextPtr < cur_text_end) {
|
||||
const char cur_char = *text;
|
||||
text++;
|
||||
|
||||
(*TextPtr)->position = {x, y};
|
||||
(*TextPtr)->position = pos;
|
||||
switch(cur_char) {
|
||||
case '\0':
|
||||
return;
|
||||
goto end;
|
||||
|
||||
case '\n':
|
||||
y += 16;
|
||||
x = org_x;
|
||||
pos.y += 16;
|
||||
pos.x = org_x;
|
||||
continue;
|
||||
|
||||
case '\x1b':
|
||||
tie(text, font_color) = parse_esc(text, font_color);
|
||||
continue;
|
||||
|
||||
case 'i':
|
||||
case '!':
|
||||
x += 12;
|
||||
pos.x += 12;
|
||||
break;
|
||||
|
||||
default:
|
||||
x += 16;
|
||||
}
|
||||
|
||||
if(cur_char == '\0') {
|
||||
break;
|
||||
pos.x += 16;
|
||||
}
|
||||
|
||||
const uint8_t char_id = cur_char - '!';
|
||||
(*TextPtr)->page = {static_cast<uint8_t>((char_id & 0xF) << 4), static_cast<uint8_t>((char_id >> 4) << 4)};
|
||||
(*TextPtr)->color = GPU::Color24::Grey();
|
||||
(*TextPtr)->color = font_color;
|
||||
|
||||
TextLink = &TextLink->concat(*TextPtr);
|
||||
TextPtr++;
|
||||
}
|
||||
|
||||
end:
|
||||
return pos;
|
||||
}
|
||||
|
||||
void render() {
|
||||
|
||||
Reference in New Issue
Block a user