-#include "quakedef.h"
+#include "darkplaces.h"
#include "utf8lib.h"
+#include "draw.h"
/*
================================================================================
================================================================================
*/
// for compatibility this defaults to 0
-cvar_t utf8_enable = {CVAR_SAVE, "utf8_enable", "0", "Enable UTF-8 support. For compatibility, this is disabled by default in most games."};
+cvar_t utf8_enable = {CF_CLIENT | CF_SERVER | CF_ARCHIVE, "utf8_enable", "0", "Enable UTF-8 support. For compatibility, this is disabled by default in most games."};
void u8_Init(void)
{
* @return Whether or not another valid character is in the string
*/
#define U8_ANALYZE_INFINITY 7
-static qboolean u8_analyze(const char *_s, size_t *_start, size_t *_len, Uchar *_ch, size_t _maxlen)
+static qbool u8_analyze(const char *_s, size_t *_start, size_t *_len, Uchar *_ch, size_t _maxlen)
{
const unsigned char *s = (const unsigned char*)_s;
size_t i, j;
return len;
}
+static int colorcode_skipwidth(const unsigned char *s)
+{
+ if(*s == STRING_COLOR_TAG)
+ {
+ if(s[1] <= '9' && s[1] >= '0') // ^[0-9] found
+ return 2;
+ else if(s[1] == STRING_COLOR_RGB_TAG_CHAR && isxdigit(s[2]) && isxdigit(s[3]) && isxdigit(s[4]))
+ return 5;
+ else if(s[1] == STRING_COLOR_TAG)
+ return 1; // special case, do NOT call colorcode_skipwidth for next char
+ }
+ return 0;
+}
+
/** Get the number of characters in a part of an UTF-8 string.
* @param _s An utf-8 encoded null-terminated string.
* @param n The maximum number of bytes.
return len;
}
+static size_t u8_strnlen_colorcodes(const char *_s, size_t n)
+{
+ size_t st, ln;
+ size_t len = 0;
+ const unsigned char *s = (const unsigned char*)_s;
+
+ while (*s && n)
+ {
+ int w = colorcode_skipwidth(s);
+ n -= w;
+ s += w;
+ if(w > 1) // == 1 means single caret
+ continue;
+
+ // ascii char, skip u8_analyze
+ if (*s < 0x80 || !utf8_enable.integer)
+ {
+ ++len;
+ ++s;
+ --n;
+ continue;
+ }
+
+ // invalid, skip u8_analyze
+ if (*s < 0xC2)
+ {
+ ++s;
+ --n;
+ continue;
+ }
+
+ if (!u8_analyze((const char*)s, &st, &ln, NULL, n))
+ break;
+ // valid character, see if it's still inside the range specified by n:
+ if (n < st + ln)
+ return len;
+ ++len;
+ n -= st + ln;
+ s += st + ln;
+ }
+ return len;
+}
+
/** Get the number of bytes used in a string to represent an amount of characters.
* @param _s An utf-8 encoded null-terminated string.
* @param n The number of characters we want to know the byte-size for.
return len;
}
+static size_t u8_bytelen_colorcodes(const char *_s, size_t n)
+{
+ size_t st, ln;
+ size_t len = 0;
+ const unsigned char *s = (const unsigned char*)_s;
+
+ while (*s && n)
+ {
+ int w = colorcode_skipwidth(s);
+ len += w;
+ s += w;
+ if(w > 1) // == 1 means single caret
+ continue;
+
+ // ascii char, skip u8_analyze
+ if (*s < 0x80 || !utf8_enable.integer)
+ {
+ ++len;
+ ++s;
+ --n;
+ continue;
+ }
+
+ // invalid, skip u8_analyze
+ if (*s < 0xC2)
+ {
+ ++s;
+ ++len;
+ continue;
+ }
+
+ if (!u8_analyze((const char*)s, &st, &ln, NULL, U8_ANALYZE_INFINITY))
+ break;
+ --n;
+ s += st + ln;
+ len += st + ln;
+ }
+ return len;
+}
+
/** Get the byte-index for a character-index.
* @param _s An utf-8 encoded string.
* @param i The character-index for which you want the byte offset.
}
if (len) *len = 1;
- return i;
+ return (int)i;
}
st = ln = 0;
} while(i-- > 0);
if (len)
*len = ln;
- return ofs;
+ return (int)ofs;
}
/** Get the char-index for a byte-index.
if (!utf8_enable.integer)
{
if (len) *len = 0;
- return i;
+ return (int)i;
}
while (ofs < i && s[ofs])
============
*/
size_t
-COM_StringLengthNoColors(const char *s, size_t size_s, qboolean *valid);
+COM_StringLengthNoColors(const char *s, size_t size_s, qbool *valid);
size_t
-u8_COM_StringLengthNoColors(const char *_s, size_t size_s, qboolean *valid)
+u8_COM_StringLengthNoColors(const char *_s, size_t size_s, qbool *valid)
{
const unsigned char *s = (const unsigned char*)_s;
const unsigned char *end;
{
case 0:
if(valid)
- *valid = TRUE;
+ *valid = true;
return len;
case STRING_COLOR_TAG:
++s;
case 0: // ends with unfinished color code!
++len;
if(valid)
- *valid = FALSE;
+ *valid = false;
return len;
case STRING_COLOR_TAG: // escaped ^
++len;
{
// we CAN end up here, if an invalid char is between this one and the end of the string
if(valid)
- *valid = TRUE;
+ *valid = true;
return len;
}
{
// string length exceeded by new character
if(valid)
- *valid = TRUE;
+ *valid = true;
return len;
}
* @param maxwidth The maximum output width
* @return The number of bytes written, not including the terminating \0
*/
-size_t u8_strpad(char *out, size_t outsize, const char *in, qboolean leftalign, size_t minwidth, size_t maxwidth)
+size_t u8_strpad(char *out, size_t outsize, const char *in, qbool leftalign, size_t minwidth, size_t maxwidth)
{
if(!utf8_enable.integer)
{
{
size_t l = u8_bytelen(in, maxwidth);
size_t actual_width = u8_strnlen(in, l);
- int pad = (actual_width >= minwidth) ? 0 : (minwidth - actual_width);
- int prec = l;
+ int pad = (int)((actual_width >= minwidth) ? 0 : (minwidth - actual_width));
+ int prec = (int)l;
int lpad = leftalign ? 0 : pad;
int rpad = leftalign ? pad : 0;
return dpsnprintf(out, outsize, "%*s%.*s%*s", lpad, "", prec, in, rpad, "");
}
}
+size_t u8_strpad_colorcodes(char *out, size_t outsize, const char *in, qbool leftalign, size_t minwidth, size_t maxwidth)
+{
+ size_t l = u8_bytelen_colorcodes(in, maxwidth);
+ size_t actual_width = u8_strnlen_colorcodes(in, l);
+ int pad = (int)((actual_width >= minwidth) ? 0 : (minwidth - actual_width));
+ int prec = (int)l;
+ int lpad = leftalign ? 0 : pad;
+ int rpad = leftalign ? pad : 0;
+ return dpsnprintf(out, outsize, "%*s%.*s%*s", lpad, "", prec, in, rpad, "");
+}
+
/*
The two following functions (u8_toupper, u8_tolower) are derived from