5 ================================================================================
6 Initialization of UTF-8 support and new cvars.
7 ================================================================================
9 // for compatibility this defaults to 0
10 cvar_t utf8_enable = {CVAR_SAVE, "utf8_enable", "0", "Enable UTF-8 support. For compatibility, this is disabled by default in most games."};
14 Cvar_RegisterVariable(&utf8_enable);
18 ================================================================================
19 UTF-8 encoding and decoding functions follow.
20 ================================================================================
23 /** Analyze the next character and return various information if requested.
24 * @param _s An utf-8 string.
25 * @param _start Filled with the start byte-offset of the next valid character
26 * @param _len Fileed with the length of the next valid character
27 * @param _ch Filled with the unicode value of the next character
28 * @param _maxlen Maximum number of bytes to read from _s
29 * @return Whether or not another valid character is in the string
31 #define U8_ANALYZE_INFINITY 7
32 static qboolean u8_analyze(const char *_s, size_t *_start, size_t *_len, Uchar *_ch, size_t _maxlen)
34 const unsigned char *s = (const unsigned char*)_s;
43 // <0xC2 is always an overlong encoding, they're invalid, thus skipped
44 while (i < _maxlen && s[i] && s[i] >= 0x80 && s[i] < 0xC2) {
45 //fprintf(stderr, "skipping\n");
49 //fprintf(stderr, "checking\n");
50 // If we hit the end, well, we're out and invalid
51 if(i >= _maxlen || !s[i]) {
52 if (_start) *_start = i;
57 //fprintf(stderr, "checking ascii\n");
61 if (_start) *_start = i;
63 if (_ch) *_ch = (Uchar)s[i];
64 //fprintf(stderr, "valid ascii\n");
67 //fprintf(stderr, "checking length\n");
69 // Figure out the next char's length
72 // count the 1 bits, they're the # of bytes
73 for (bt = 0x40; bt && (bc & bt); bt >>= 1, ++bits);
76 //fprintf(stderr, "superlong\n");
80 if(i + bits > _maxlen) {
82 if (_start) *_start = i;
89 // turn bt into a mask and give ch a starting value
92 // check the byte sequence for invalid bytes
93 for (j = 1; j < bits; ++j)
95 // valid bit value: 10xx xxxx
96 //if (s[i+j] < 0x80 || s[i+j] >= 0xC0)
97 if ( (s[i+j] & 0xC0) != 0x80 )
99 //fprintf(stderr, "sequence of %i f'd at %i by %x\n", bits, j, (unsigned int)s[i+j]);
100 // this byte sequence is invalid, skip it
102 // find a character after it
105 // at the same time, decode the character
106 ch = (ch << 6) | (s[i+j] & 0x3F);
109 // Now check the decoded byte for an overlong encoding
110 if ( (bits >= 2 && ch < 0x80) ||
111 (bits >= 3 && ch < 0x800) ||
112 (bits >= 4 && ch < 0x10000) ||
113 ch >= 0x10FFFF // RFC 3629
117 //fprintf(stderr, "overlong: %i bytes for %x\n", bits, ch);
127 //fprintf(stderr, "valid utf8\n");
131 /** Get the number of characters in an UTF-8 string.
132 * @param _s An utf-8 encoded null-terminated string.
133 * @return The number of unicode characters in the string.
135 size_t u8_strlen(const char *_s)
139 const unsigned char *s = (const unsigned char*)_s;
141 if (!utf8_enable.integer)
146 // ascii char, skip u8_analyze
154 // invalid, skip u8_analyze
161 if (!u8_analyze((const char*)s, &st, &ln, NULL, U8_ANALYZE_INFINITY))
163 // valid character, skip after it
170 /** Get the number of characters in a part of an UTF-8 string.
171 * @param _s An utf-8 encoded null-terminated string.
172 * @param n The maximum number of bytes.
173 * @return The number of unicode characters in the string.
175 size_t u8_strnlen(const char *_s, size_t n)
179 const unsigned char *s = (const unsigned char*)_s;
181 if (!utf8_enable.integer)
184 return (len < n) ? len : n;
189 // ascii char, skip u8_analyze
198 // invalid, skip u8_analyze
206 if (!u8_analyze((const char*)s, &st, &ln, NULL, n))
208 // valid character, see if it's still inside the range specified by n:
218 /** Get the number of bytes used in a string to represent an amount of characters.
219 * @param _s An utf-8 encoded null-terminated string.
220 * @param n The number of characters we want to know the byte-size for.
221 * @return The number of bytes used to represent n characters.
223 size_t u8_bytelen(const char *_s, size_t n)
227 const unsigned char *s = (const unsigned char*)_s;
229 if (!utf8_enable.integer) {
231 return (len < n) ? len : n;
236 // ascii char, skip u8_analyze
245 // invalid, skip u8_analyze
253 if (!u8_analyze((const char*)s, &st, &ln, NULL, U8_ANALYZE_INFINITY))
262 /** Get the byte-index for a character-index.
263 * @param _s An utf-8 encoded string.
264 * @param i The character-index for which you want the byte offset.
265 * @param len If not null, character's length will be stored in there.
266 * @return The byte-index at which the character begins, or -1 if the string is too short.
268 int u8_byteofs(const char *_s, size_t i, size_t *len)
272 const unsigned char *s = (const unsigned char*)_s;
274 if (!utf8_enable.integer)
290 if (!u8_analyze((const char*)s + ofs, &st, &ln, NULL, U8_ANALYZE_INFINITY))
299 /** Get the char-index for a byte-index.
300 * @param _s An utf-8 encoded string.
301 * @param i The byte offset for which you want the character index.
302 * @param len If not null, the offset within the character is stored here.
303 * @return The character-index, or -1 if the string is too short.
305 int u8_charidx(const char *_s, size_t i, size_t *len)
311 const unsigned char *s = (const unsigned char*)_s;
313 if (!utf8_enable.integer)
319 while (ofs < i && s[ofs])
321 // ascii character, skip u8_analyze
330 // invalid, skip u8_analyze
337 if (!u8_analyze((const char*)s+ofs, &st, &ln, NULL, U8_ANALYZE_INFINITY))
339 // see if next char is after the bytemark
349 // see if bytemark is within the char
361 /** Get the byte offset of the previous byte.
363 * prevchar_pos = u8_byteofs(text, u8_charidx(text, thischar_pos, NULL) - 1, NULL)
364 * @param _s An utf-8 encoded string.
365 * @param i The current byte offset.
366 * @return The byte offset of the previous character
368 size_t u8_prevbyte(const char *_s, size_t i)
371 const unsigned char *s = (const unsigned char*)_s;
375 if (!utf8_enable.integer)
382 while (ofs < i && s[ofs])
384 // ascii character, skip u8_analyze
391 // invalid, skip u8_analyze
398 if (!u8_analyze((const char*)s+ofs, &st, &ln, NULL, U8_ANALYZE_INFINITY))
402 if (ofs + st + ln >= i)
411 static int char_usefont[256] = {
412 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // specials
413 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // specials
414 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // shift+digit line
415 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // digits
416 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // caps
417 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // caps
418 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // small
419 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // small
420 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // specials
421 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // faces
422 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
423 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
424 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
425 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
426 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
427 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
431 /** Fetch a character from an utf-8 encoded string.
432 * @param _s The start of an utf-8 encoded multi-byte character.
433 * @param _end Will point to after the first multi-byte character.
434 * @return The 32-bit integer representation of the first multi-byte character or 0 for invalid characters.
436 Uchar u8_getchar(const char *_s, const char **_end)
441 if (!utf8_enable.integer)
445 /* Careful: if we disable utf8 but not freetype, we wish to see freetype chars
446 * for normal letters. So use E000+x for special chars, but leave the freetype stuff for the
449 if (!char_usefont[(unsigned int)*(const unsigned char*)_s])
450 return 0xE000 + (Uchar)*(const unsigned char*)_s;
451 return (Uchar)*(const unsigned char*)_s;
454 if (!u8_analyze(_s, &st, &ln, &ch, U8_ANALYZE_INFINITY))
457 *_end = _s + st + ln;
461 /** Fetch a character from an utf-8 encoded string.
462 * @param _s The start of an utf-8 encoded multi-byte character.
463 * @param _end Will point to after the first multi-byte character.
464 * @return The 32-bit integer representation of the first multi-byte character or 0 for invalid characters.
466 Uchar u8_getnchar(const char *_s, const char **_end, size_t _maxlen)
471 if (!utf8_enable.integer)
475 /* Careful: if we disable utf8 but not freetype, we wish to see freetype chars
476 * for normal letters. So use E000+x for special chars, but leave the freetype stuff for the
479 if (!char_usefont[(unsigned int)*(const unsigned char*)_s])
480 return 0xE000 + (Uchar)*(const unsigned char*)_s;
481 return (Uchar)*(const unsigned char*)_s;
484 if (!u8_analyze(_s, &st, &ln, &ch, _maxlen))
487 *_end = _s + st + ln;
491 /** Encode a wide-character into utf-8.
492 * @param w The wide character to encode.
493 * @param to The target buffer the utf-8 encoded string is stored to.
494 * @param maxlen The maximum number of bytes that fit into the target buffer.
495 * @return Number of bytes written to the buffer not including the terminating null.
496 * Less or equal to 0 if the buffer is too small.
498 int u8_fromchar(Uchar w, char *to, size_t maxlen)
506 if (w >= 0xE000 && !utf8_enable.integer)
509 if (w < 0x80 || !utf8_enable.integer)
517 // for a little speedup
526 to[1] = 0x80 | (w & 0x3F); w >>= 6;
538 to[2] = 0x80 | (w & 0x3F); w >>= 6;
539 to[1] = 0x80 | (w & 0x3F); w >>= 6;
553 to[3] = 0x80 | (w & 0x3F); w >>= 6;
554 to[2] = 0x80 | (w & 0x3F); w >>= 6;
555 to[1] = 0x80 | (w & 0x3F); w >>= 6;
562 /** uses u8_fromchar on a static buffer
563 * @param ch The unicode character to convert to encode
564 * @param l The number of bytes without the terminating null.
565 * @return A statically allocated buffer containing the character's utf8 representation, or NULL if it fails.
567 char *u8_encodech(Uchar ch, size_t *l)
571 len = u8_fromchar(ch, buf, sizeof(buf));
580 /** Convert a utf-8 multibyte string to a wide character string.
581 * @param wcs The target wide-character buffer.
582 * @param mb The utf-8 encoded multibyte string to convert.
583 * @param maxlen The maximum number of wide-characters that fit into the target buffer.
584 * @return The number of characters written to the target buffer.
586 size_t u8_mbstowcs(Uchar *wcs, const char *mb, size_t maxlen)
592 for (i = 0; *mb && i < maxlen-1; ++i)
594 ch = u8_getchar(mb, &mb);
603 /** Convert a wide-character string to a utf-8 multibyte string.
604 * @param mb The target buffer the utf-8 string is written to.
605 * @param wcs The wide-character string to convert.
606 * @param maxlen The number bytes that fit into the multibyte target buffer.
607 * @return The number of bytes written, not including the terminating \0
609 size_t u8_wcstombs(char *mb, const Uchar *wcs, size_t maxlen)
612 const char *start = mb;
615 for (i = 0; wcs[i] && i < maxlen-1; ++i)
619 if ( (len = u8_fromchar(wcs[i], mb, maxlen - i)) < 0)
623 mb += u8_fromchar(wcs[i], mb, maxlen - i);
631 UTF-8 aware COM_StringLengthNoColors
633 calculates the visible width of a color coded string.
635 *valid is filled with TRUE if the string is a valid colored string (that is, if
636 it does not end with an unfinished color code). If it gets filled with FALSE, a
637 fix would be adding a STRING_COLOR_TAG at the end of the string.
639 valid can be set to NULL if the caller doesn't care.
641 For size_s, specify the maximum number of characters from s to use, or 0 to use
642 all characters until the zero terminator.
646 COM_StringLengthNoColors(const char *s, size_t size_s, qboolean *valid);
648 u8_COM_StringLengthNoColors(const char *_s, size_t size_s, qboolean *valid)
650 const unsigned char *s = (const unsigned char*)_s;
651 const unsigned char *end;
654 if (!utf8_enable.integer)
655 return COM_StringLengthNoColors(_s, size_s, valid);
657 end = size_s ? (s + size_s) : NULL;
661 switch((s == end) ? 0 : *s)
667 case STRING_COLOR_TAG:
669 switch((s == end) ? 0 : *s)
671 case STRING_COLOR_RGB_TAG_CHAR:
672 if (s+1 != end && isxdigit(s[1]) &&
673 s+2 != end && isxdigit(s[2]) &&
674 s+3 != end && isxdigit(s[3]) )
679 ++len; // STRING_COLOR_TAG
680 ++len; // STRING_COLOR_RGB_TAG_CHAR
682 case 0: // ends with unfinished color code!
687 case STRING_COLOR_TAG: // escaped ^
690 case '0': case '1': case '2': case '3': case '4':
691 case '5': case '6': case '7': case '8': case '9': // color code
693 default: // not a color code
694 ++len; // STRING_COLOR_TAG
695 ++len; // the character
704 // start of a wide character
707 for (++s; *s >= 0x80 && *s <= 0xC0; ++s);
710 // part of a wide character, we ignore that one
718 /** Pads a utf-8 string
719 * @param out The target buffer the utf-8 string is written to.
720 * @param outsize The size of the target buffer, including the final NUL
721 * @param in The input utf-8 buffer
722 * @param leftalign Left align the output string (by default right alignment is done)
723 * @param minwidth The minimum output width
724 * @param maxwidth The maximum output width
725 * @return The number of bytes written, not including the terminating \0
727 size_t u8_strpad(char *out, size_t outsize, const char *in, qboolean leftalign, size_t minwidth, size_t maxwidth)
729 if(!utf8_enable.integer)
731 return dpsnprintf(out, outsize, "%*.*s", leftalign ? -(int) minwidth : (int) minwidth, (int) maxwidth, in);
735 size_t l = u8_bytelen(in, maxwidth);
736 size_t actual_width = u8_strnlen(in, l);
737 int pad = (actual_width >= minwidth) ? 0 : (minwidth - actual_width);
739 int lpad = leftalign ? 0 : pad;
740 int rpad = leftalign ? pad : 0;
741 return dpsnprintf(out, outsize, "%*s%.*s%*s", lpad, "", prec, in, rpad, "");