mirror of
https://github.com/ZoiteChat/zoitechat.git
synced 2026-10-12 16:47:14 +00:00
feat(xtext): render \x04 hex color codes in chat text
This commit is contained in:
@@ -197,3 +197,17 @@ test('Emoji Data Tests', emoji_data_tests,
|
||||
protocol: 'tap',
|
||||
timeout: 120,
|
||||
)
|
||||
|
||||
hexcolor_tests = executable('hexcolor_tests',
|
||||
[
|
||||
'tests/test-hexcolor.c',
|
||||
'util.c',
|
||||
],
|
||||
include_directories: [config_h_include, include_directories('.')],
|
||||
dependencies: [libgio_dep] + (libssl_dep.found() ? [libssl_dep] : []),
|
||||
)
|
||||
|
||||
test('Hex Color Tests', hexcolor_tests,
|
||||
protocol: 'tap',
|
||||
timeout: 120,
|
||||
)
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
/* ZoiteChat
|
||||
*
|
||||
* This program 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.
|
||||
*
|
||||
* This program 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 this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#include "../util.h"
|
||||
|
||||
static void
|
||||
test_parse_foreground (void)
|
||||
{
|
||||
guint32 fg = 0, bg = 0xDEAD;
|
||||
int has_bg = TRUE;
|
||||
|
||||
g_assert_cmpint (hexcolor_parse ("FF8800text", 10, &fg, &bg, &has_bg), ==, 6);
|
||||
g_assert_cmphex (fg, ==, 0xFF8800);
|
||||
g_assert_false (has_bg);
|
||||
g_assert_cmphex (bg, ==, 0xDEAD);
|
||||
}
|
||||
|
||||
static void
|
||||
test_parse_foreground_and_background (void)
|
||||
{
|
||||
guint32 fg = 0, bg = 0;
|
||||
int has_bg = FALSE;
|
||||
|
||||
g_assert_cmpint (hexcolor_parse ("112233,a0b0c0x", 14, &fg, &bg, &has_bg), ==, 13);
|
||||
g_assert_cmphex (fg, ==, 0x112233);
|
||||
g_assert_cmphex (bg, ==, 0xA0B0C0);
|
||||
g_assert_true (has_bg);
|
||||
}
|
||||
|
||||
static void
|
||||
test_parse_bad_background_keeps_foreground (void)
|
||||
{
|
||||
guint32 fg = 0;
|
||||
int has_bg = TRUE;
|
||||
|
||||
/* ",12" is plain text after a valid foreground, not a background */
|
||||
g_assert_cmpint (hexcolor_parse ("112233,12 hi", 12, &fg, NULL, &has_bg), ==, 6);
|
||||
g_assert_cmphex (fg, ==, 0x112233);
|
||||
g_assert_false (has_bg);
|
||||
}
|
||||
|
||||
static void
|
||||
test_parse_bare (void)
|
||||
{
|
||||
/* a bare \004 resets colors, like a bare \003 */
|
||||
g_assert_cmpint (hexcolor_parse (" text", 5, NULL, NULL, NULL), ==, 0);
|
||||
g_assert_cmpint (hexcolor_parse ("12345G", 6, NULL, NULL, NULL), ==, 0);
|
||||
g_assert_cmpint (hexcolor_parse ("", 0, NULL, NULL, NULL), ==, 0);
|
||||
}
|
||||
|
||||
static void
|
||||
test_parse_respects_length (void)
|
||||
{
|
||||
g_assert_cmpint (hexcolor_parse ("FF8800", 5, NULL, NULL, NULL), ==, 0);
|
||||
g_assert_cmpint (hexcolor_parse ("FF8800,112233", 12, NULL, NULL, NULL), ==, 6);
|
||||
}
|
||||
|
||||
static void
|
||||
assert_stripped (const char *in, int flags, const char *expected)
|
||||
{
|
||||
char out[128];
|
||||
|
||||
strip_color2 (in, -1, out, flags);
|
||||
g_assert_cmpstr (out, ==, expected);
|
||||
}
|
||||
|
||||
static void
|
||||
test_strip (void)
|
||||
{
|
||||
assert_stripped ("\004FF8800alice\017: hi", STRIP_ALL, "alice: hi");
|
||||
assert_stripped ("\004112233,445566bg", STRIP_ALL, "bg");
|
||||
assert_stripped ("a\004 b", STRIP_ALL, "a b");
|
||||
assert_stripped ("\004FF8800\00312x", STRIP_ALL, "x");
|
||||
/* a trailing code with too few digits leaves the rest as text */
|
||||
assert_stripped ("x\004FF88", STRIP_ALL, "xFF88");
|
||||
}
|
||||
|
||||
static void
|
||||
test_strip_keeps_hex_without_strip_color (void)
|
||||
{
|
||||
assert_stripped ("\004FF8800red\002", STRIP_ATTRIB, "\004FF8800red");
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
|
||||
g_test_add_func ("/hexcolor/parse/foreground", test_parse_foreground);
|
||||
g_test_add_func ("/hexcolor/parse/foreground-and-background", test_parse_foreground_and_background);
|
||||
g_test_add_func ("/hexcolor/parse/bad-background", test_parse_bad_background_keeps_foreground);
|
||||
g_test_add_func ("/hexcolor/parse/bare", test_parse_bare);
|
||||
g_test_add_func ("/hexcolor/parse/length", test_parse_respects_length);
|
||||
g_test_add_func ("/hexcolor/strip/all", test_strip);
|
||||
g_test_add_func ("/hexcolor/strip/attrib-only", test_strip_keeps_hex_without_strip_color);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
||||
@@ -301,6 +301,42 @@ strip_color (const char *text, int len, int flags)
|
||||
return new_str;
|
||||
}
|
||||
|
||||
static int
|
||||
hexcolor_component (const char *s, int avail, guint32 *rgb)
|
||||
{
|
||||
int i;
|
||||
guint32 value = 0;
|
||||
|
||||
if (avail < 6)
|
||||
return FALSE;
|
||||
for (i = 0; i < 6; i++)
|
||||
{
|
||||
if (!g_ascii_isxdigit (s[i]))
|
||||
return FALSE;
|
||||
value = (value << 4) | g_ascii_xdigit_value (s[i]);
|
||||
}
|
||||
if (rgb)
|
||||
*rgb = value;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// Parses the parameters of a \004 (hex color) code: RRGGBB[,RRGGBB].
|
||||
int
|
||||
hexcolor_parse (const char *s, int avail, guint32 *fg, guint32 *bg, int *has_bg)
|
||||
{
|
||||
if (has_bg)
|
||||
*has_bg = FALSE;
|
||||
if (!hexcolor_component (s, avail, fg))
|
||||
return 0;
|
||||
if (avail >= 7 && s[6] == ',' && hexcolor_component (s + 7, avail - 7, bg))
|
||||
{
|
||||
if (has_bg)
|
||||
*has_bg = TRUE;
|
||||
return 13;
|
||||
}
|
||||
return 6;
|
||||
}
|
||||
|
||||
/* CL: strip_color2 strips src and writes the output at dst; pass the same pointer
|
||||
in both arguments to strip in place. */
|
||||
int
|
||||
@@ -330,6 +366,14 @@ strip_color2 (const char *src, int len, char *dst, int flags)
|
||||
if (!(flags & STRIP_COLOR)) goto pass_char;
|
||||
rcol = 2;
|
||||
break;
|
||||
case '\004': /*ATTR_HEXCOLOR: */
|
||||
if (!(flags & STRIP_COLOR)) goto pass_char;
|
||||
{
|
||||
int skip = hexcolor_parse (src + 1, len, NULL, NULL, NULL);
|
||||
src += skip;
|
||||
len -= skip;
|
||||
}
|
||||
break;
|
||||
case HIDDEN_CHAR: /* CL: invisible text (for event formats only) */ /* this takes care of the topic */
|
||||
if (!(flags & STRIP_HIDDEN)) goto pass_char;
|
||||
break;
|
||||
|
||||
@@ -59,6 +59,7 @@ void util_exec (const char *cmd);
|
||||
#define STRIP_ALL 7
|
||||
gchar *strip_color (const char *text, int len, int flags);
|
||||
int strip_color2 (const char *src, int len, char *dst, int flags);
|
||||
int hexcolor_parse (const char *s, int avail, guint32 *fg, guint32 *bg, int *has_bg);
|
||||
int strip_hidden_attribute (char *src, char *dst);
|
||||
char *errorstring (int err);
|
||||
int waitline (int sok, char *buf, int bufsize, int);
|
||||
|
||||
+65
-2
@@ -775,16 +775,35 @@ backend_draw_text_emph (GtkXText *xtext, gboolean dofill, int x, int y,
|
||||
cairo_destroy (cr);
|
||||
}
|
||||
|
||||
static XTextColor
|
||||
xtext_color_for_index (GtkXText *xtext, int index)
|
||||
{
|
||||
if (index == XTEXT_HEX_FG)
|
||||
return xtext->hex_fg;
|
||||
if (index == XTEXT_HEX_BG)
|
||||
return xtext->hex_bg;
|
||||
return xtext->palette[index];
|
||||
}
|
||||
|
||||
static void
|
||||
xtext_set_fg (GtkXText *xtext, int index)
|
||||
{
|
||||
xtext->fgc = xtext->palette[index];
|
||||
xtext->fgc = xtext_color_for_index (xtext, index);
|
||||
}
|
||||
|
||||
static void
|
||||
xtext_set_bg (GtkXText *xtext, int index)
|
||||
{
|
||||
xtext->bgc = xtext->palette[index];
|
||||
xtext->bgc = xtext_color_for_index (xtext, index);
|
||||
}
|
||||
|
||||
static void
|
||||
xtext_rgb_to_color (guint32 rgb, XTextColor *color)
|
||||
{
|
||||
color->red = ((rgb >> 16) & 0xFF) / 255.0;
|
||||
color->green = ((rgb >> 8) & 0xFF) / 255.0;
|
||||
color->blue = (rgb & 0xFF) / 255.0;
|
||||
color->alpha = 1.0;
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -2999,6 +3018,14 @@ gtk_xtext_strip_color (unsigned char *text, int len, unsigned char *outbuf,
|
||||
xtext_do_chunk (&c);
|
||||
rcol = 2;
|
||||
break;
|
||||
case ATTR_HEXCOLOR:
|
||||
xtext_do_chunk (&c);
|
||||
{
|
||||
int skip = hexcolor_parse ((char *)text + 1, len - 1, NULL, NULL, NULL);
|
||||
text += skip;
|
||||
len -= skip;
|
||||
}
|
||||
break;
|
||||
case ATTR_BEEP:
|
||||
case ATTR_RESET:
|
||||
case ATTR_REVERSE:
|
||||
@@ -3545,6 +3572,35 @@ gtk_xtext_render_str (GtkXText * xtext, int y, textentry * ent,
|
||||
pstr += j + 1;
|
||||
j = 0;
|
||||
break;
|
||||
case ATTR_HEXCOLOR:
|
||||
RENDER_FLUSH;
|
||||
{
|
||||
guint32 fg = 0, bg = 0;
|
||||
int has_bg;
|
||||
int skip = hexcolor_parse ((char *)str + i + 1, len - i - 1, &fg, &bg, &has_bg);
|
||||
|
||||
if (skip == 0)
|
||||
gtk_xtext_reset (xtext, mark, FALSE);
|
||||
else
|
||||
{
|
||||
xtext_rgb_to_color (fg, &xtext->hex_fg);
|
||||
xtext->col_fore = XTEXT_HEX_FG;
|
||||
if (!mark)
|
||||
xtext_set_fg (xtext, XTEXT_HEX_FG);
|
||||
if (has_bg)
|
||||
{
|
||||
xtext_rgb_to_color (bg, &xtext->hex_bg);
|
||||
xtext->col_back = XTEXT_HEX_BG;
|
||||
xtext->backcolor = TRUE;
|
||||
if (!mark)
|
||||
xtext_set_bg (xtext, XTEXT_HEX_BG);
|
||||
}
|
||||
}
|
||||
pstr += j + 1 + skip;
|
||||
j = 0;
|
||||
i += skip;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
tmp = charlen (str + i);
|
||||
/* invalid utf8 safe guard */
|
||||
@@ -3762,6 +3818,13 @@ find_next_wrap (GtkXText * xtext, textentry * ent, unsigned char *str,
|
||||
limit_offset++;
|
||||
str++;
|
||||
break;
|
||||
case ATTR_HEXCOLOR:
|
||||
{
|
||||
int skip = hexcolor_parse ((char *)str + 1, (ent->str + ent->str_len) - str - 1, NULL, NULL, NULL);
|
||||
limit_offset += 1 + skip;
|
||||
str += 1 + skip;
|
||||
}
|
||||
break;
|
||||
case ATTR_HIDDEN:
|
||||
if (xtext->ignore_hidden)
|
||||
goto def;
|
||||
|
||||
@@ -38,6 +38,7 @@ GType gtk_xtext_get_type (void);
|
||||
|
||||
#define ATTR_BOLD '\002'
|
||||
#define ATTR_COLOR '\003'
|
||||
#define ATTR_HEXCOLOR '\004'
|
||||
#define ATTR_BLINK '\006'
|
||||
#define ATTR_BEEP '\007'
|
||||
#define ATTR_HIDDEN '\010'
|
||||
@@ -57,6 +58,8 @@ GType gtk_xtext_get_type (void);
|
||||
#define XTEXT_BG 102
|
||||
#define XTEXT_MARKER 103 /* for marker line */
|
||||
#define XTEXT_MAX_COLOR 98
|
||||
#define XTEXT_HEX_FG 200 /* not palette slots: colors from \004RRGGBB */
|
||||
#define XTEXT_HEX_BG 201
|
||||
typedef struct textentry textentry;
|
||||
|
||||
/*
|
||||
@@ -181,6 +184,8 @@ struct _GtkXText
|
||||
|
||||
int col_fore;
|
||||
int col_back;
|
||||
XTextColor hex_fg;
|
||||
XTextColor hex_bg;
|
||||
|
||||
int depth; /* gdk window depth */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user