From aaebf87cf60ce3c040eacf2403ff078b41e37331 Mon Sep 17 00:00:00 2001 From: Patrick McDermott Date: Wed, 17 Jul 2019 23:00:46 -0400 Subject: sgr(): New function (4-bit colors only) --- 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 . + */ + +#include "sgr.h" + +#include +#include + +#include + +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 . + */ + +#ifndef SRG_H_ +#define SGR_H_ + +#include + +void +sgr(char *param, png_colorp fg, png_colorp bg); + +#endif /* SGR_H_ */ diff --git a/src/text.c b/src/text.c index cb777e4..1d888d8 100644 --- a/src/text.c +++ b/src/text.c @@ -27,6 +27,7 @@ #include #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') { -- cgit v0.9.1