diff options
author | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2019-07-17 23:00:46 (EDT) |
---|---|---|
committer | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2019-07-17 23:01:39 (EDT) |
commit | aaebf87cf60ce3c040eacf2403ff078b41e37331 (patch) | |
tree | df3439538a1496902a6de933aad803bd725cf660 | |
parent | 35cfcd0fd74ebce66cd7bf12309b600d37c34623 (diff) |
sgr(): New function (4-bit colors only)
-rw-r--r-- | src/local.mk | 1 | ||||
-rw-r--r-- | src/sgr.c | 85 | ||||
-rw-r--r-- | src/sgr.h | 30 | ||||
-rw-r--r-- | src/text.c | 7 |
4 files changed, 123 insertions, 0 deletions
diff --git a/src/local.mk b/src/local.mk index 43f5c4a..24d8af2 100644 --- a/src/local.mk +++ b/src/local.mk @@ -2,4 +2,5 @@ fbcon2png_SOURCES += \ %reldir%/font.c \ %reldir%/image.c \ %reldir%/main.c \ + %reldir%/sgr.c \ %reldir%/text.c diff --git a/src/sgr.c b/src/sgr.c new file mode 100644 index 0000000..baec803 --- /dev/null +++ b/src/sgr.c @@ -0,0 +1,85 @@ +/* + * Select Graphic Rendition color parameters function + * + * Copyright (C) 2019 Patrick McDermott + * + * This file is part of fbcon2png. + * + * fbcon2png is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * fbcon2png is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with fbcon2png. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "sgr.h" + +#include <stdlib.h> +#include <string.h> + +#include <png.h> + +static png_color _4bit[] = { + { 0, 0, 0}, /* Black */ + {170, 0, 0}, /* Red */ + { 0, 170, 0}, /* Green */ + {170, 85, 0}, /* Yellow */ + { 0, 0, 170}, /* Blue */ + {170, 0, 170}, /* Magenta */ + { 0, 170, 170}, /* Cyan */ + {170, 170, 170}, /* White */ + { 85, 85, 85}, /* Bright Black */ + {255, 85, 85}, /* Bright Red */ + { 85, 255, 85}, /* Bright Green */ + {255, 255, 85}, /* Bright Yellow */ + { 85, 85, 255}, /* Bright Blue */ + {255, 85, 255}, /* Bright Magenta */ + { 85, 255, 255}, /* Bright Cyan */ + {255, 255, 255}, /* Bright White */ +}; + +/* TODO: Add 8-bit and 24-bit modes. */ + +void +sgr(char *param, png_colorp fg, png_colorp bg) +{ + int end; + char *p; + int i; + + end = 0; + for (p = param; param; ++param) { + if (*param == ';' || *param == ':' || *param == '\0') { + if (*param == '\0') { + end = 1; + } + *param = '\0'; + i = atoi(p); + if (i >= 30 && i <= 37) { /* Set foreground color */ + memcpy(fg, &_4bit[i - 30], sizeof(*fg)); + } + if (i >= 40 && i <= 47) { /* Set background color */ + memcpy(bg, &_4bit[i - 40], sizeof(*bg)); + } + if (i >= 90 && i <= 97) { /* Set foreground color */ + memcpy(fg, &_4bit[i - 90 + 8], sizeof(*fg)); + } + if (i >= 100 && i <= 107) { /* Set background color */ + memcpy(bg, &_4bit[i - 100 + 8], sizeof(*bg)); + } + if (end) { + break; + } + p = param + 1; + } else if (*param < '0' || *param > '9') { + break; + } + } +} diff --git a/src/sgr.h b/src/sgr.h new file mode 100644 index 0000000..3b2362e --- /dev/null +++ b/src/sgr.h @@ -0,0 +1,30 @@ +/* + * Select Graphic Rendition color parameters function + * + * Copyright (C) 2019 Patrick McDermott + * + * This file is part of fbcon2png. + * + * fbcon2png is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * fbcon2png is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with fbcon2png. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef SRG_H_ +#define SGR_H_ + +#include <png.h> + +void +sgr(char *param, png_colorp fg, png_colorp bg); + +#endif /* SGR_H_ */ @@ -27,6 +27,7 @@ #include <png.h> #include "font.h" +#include "sgr.h" #define MAX(a, b) (((a) > (b)) ? (a) : (b)) @@ -102,6 +103,7 @@ text_render(struct text *text, struct font *font, png_bytepp rows) char *string; size_t row; size_t col; + char *parameter; png_color fg = {255, 255, 255}; png_color bg = { 0, 0, 0}; @@ -117,9 +119,14 @@ text_render(struct text *text, struct font *font, png_bytepp rows) while (string && *string) { if (*string == 0x1B && *(string + 1) == '[') { /* CSI */ string += 2; + parameter = string; for (; *string >= 0x30 && *string <= 0x3F; ++string); for (; *string >= 0x20 && *string <= 0x2F; ++string); if (*string >= 0x40 && *string <= 0x7E) { + if (*string == 'm') { + *string = '\0'; + sgr(parameter, &fg, &bg); + } ++string; } } else if (*string == '\n') { |