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");
50 //fprintf(stderr, "checking\n");
52 // If we hit the end, well, we're out and invalid
55 //fprintf(stderr, "checking ascii\n");
60 if (_start) *_start = i;
62 if (_ch) *_ch = (Uchar)s[i];
63 //fprintf(stderr, "valid ascii\n");
66 //fprintf(stderr, "checking length\n");
68 // Figure out the next char's length
71 // count the 1 bits, they're the # of bytes
72 for (bt = 0x40; bt && (bc & bt); bt >>= 1, ++bits);
75 //fprintf(stderr, "superlong\n");
79 if(i + bits > _maxlen)
81 // turn bt into a mask and give ch a starting value
84 // check the byte sequence for invalid bytes
85 for (j = 1; j < bits; ++j)
87 // valid bit value: 10xx xxxx
88 //if (s[i+j] < 0x80 || s[i+j] >= 0xC0)
89 if ( (s[i+j] & 0xC0) != 0x80 )
91 //fprintf(stderr, "sequence of %i f'd at %i by %x\n", bits, j, (unsigned int)s[i+j]);
92 // this byte sequence is invalid, skip it
94 // find a character after it
97 // at the same time, decode the character
98 ch = (ch << 6) | (s[i+j] & 0x3F);
101 // Now check the decoded byte for an overlong encoding
102 if ( (bits >= 2 && ch < 0x80) ||
103 (bits >= 3 && ch < 0x800) ||
104 (bits >= 4 && ch < 0x10000) ||
105 ch >= 0x10FFFF // RFC 3629
109 //fprintf(stderr, "overlong: %i bytes for %x\n", bits, ch);
119 //fprintf(stderr, "valid utf8\n");
123 /** Get the number of characters in an UTF-8 string.
124 * @param _s An utf-8 encoded null-terminated string.
125 * @return The number of unicode characters in the string.
127 size_t u8_strlen(const char *_s)
131 const unsigned char *s = (const unsigned char*)_s;
133 if (!utf8_enable.integer)
138 // ascii char, skip u8_analyze
146 // invalid, skip u8_analyze
153 if (!u8_analyze((const char*)s, &st, &ln, NULL, U8_ANALYZE_INFINITY))
155 // valid character, skip after it
162 /** Get the number of characters in a part of an UTF-8 string.
163 * @param _s An utf-8 encoded null-terminated string.
164 * @param n The maximum number of bytes.
165 * @return The number of unicode characters in the string.
167 size_t u8_strnlen(const char *_s, size_t n)
171 const unsigned char *s = (const unsigned char*)_s;
173 if (!utf8_enable.integer)
176 return (len < n) ? len : n;
181 // ascii char, skip u8_analyze
190 // invalid, skip u8_analyze
198 if (!u8_analyze((const char*)s, &st, &ln, NULL, n))
200 // valid character, see if it's still inside the range specified by n:
210 /** Get the number of bytes used in a string to represent an amount of characters.
211 * @param _s An utf-8 encoded null-terminated string.
212 * @param n The number of characters we want to know the byte-size for.
213 * @return The number of bytes used to represent n characters.
215 size_t u8_bytelen(const char *_s, size_t n)
219 const unsigned char *s = (const unsigned char*)_s;
221 if (!utf8_enable.integer)
226 // ascii char, skip u8_analyze
235 // invalid, skip u8_analyze
243 if (!u8_analyze((const char*)s, &st, &ln, NULL, U8_ANALYZE_INFINITY))
252 /** Get the byte-index for a character-index.
253 * @param _s An utf-8 encoded string.
254 * @param i The character-index for which you want the byte offset.
255 * @param len If not null, character's length will be stored in there.
256 * @return The byte-index at which the character begins, or -1 if the string is too short.
258 int u8_byteofs(const char *_s, size_t i, size_t *len)
262 const unsigned char *s = (const unsigned char*)_s;
264 if (!utf8_enable.integer)
274 if (!u8_analyze((const char*)s + ofs, &st, &ln, NULL, U8_ANALYZE_INFINITY))
283 /** Get the char-index for a byte-index.
284 * @param _s An utf-8 encoded string.
285 * @param i The byte offset for which you want the character index.
286 * @param len If not null, the offset within the character is stored here.
287 * @return The character-index, or -1 if the string is too short.
289 int u8_charidx(const char *_s, size_t i, size_t *len)
295 const unsigned char *s = (const unsigned char*)_s;
297 if (!utf8_enable.integer)
303 while (ofs < i && s[ofs])
305 // ascii character, skip u8_analyze
314 // invalid, skip u8_analyze
321 if (!u8_analyze((const char*)s+ofs, &st, &ln, NULL, U8_ANALYZE_INFINITY))
323 // see if next char is after the bytemark
333 // see if bytemark is within the char
345 /** Get the byte offset of the previous byte.
347 * prevchar_pos = u8_byteofs(text, u8_charidx(text, thischar_pos, NULL) - 1, NULL)
348 * @param _s An utf-8 encoded string.
349 * @param i The current byte offset.
350 * @return The byte offset of the previous character
352 size_t u8_prevbyte(const char *_s, size_t i)
355 const unsigned char *s = (const unsigned char*)_s;
359 if (!utf8_enable.integer)
366 while (ofs < i && s[ofs])
368 // ascii character, skip u8_analyze
375 // invalid, skip u8_analyze
382 if (!u8_analyze((const char*)s+ofs, &st, &ln, NULL, U8_ANALYZE_INFINITY))
386 if (ofs + st + ln >= i)
395 static int char_usefont[256] = {
396 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // specials
397 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // specials
398 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // shift+digit line
399 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // digits
400 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // caps
401 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // caps
402 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // small
403 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // small
404 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // specials
405 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // faces
406 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
407 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
408 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
409 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
410 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
411 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
415 /** Fetch a character from an utf-8 encoded string.
416 * @param _s The start of an utf-8 encoded multi-byte character.
417 * @param _end Will point to after the first multi-byte character.
418 * @return The 32-bit integer representation of the first multi-byte character or 0 for invalid characters.
420 Uchar u8_getchar(const char *_s, const char **_end)
425 if (!utf8_enable.integer)
429 /* Careful: if we disable utf8 but not freetype, we wish to see freetype chars
430 * for normal letters. So use E000+x for special chars, but leave the freetype stuff for the
433 if (!char_usefont[(unsigned int)*(const unsigned char*)_s])
434 return 0xE000 + (Uchar)*(const unsigned char*)_s;
435 return (Uchar)*(const unsigned char*)_s;
438 if (!u8_analyze(_s, &st, &ln, &ch, U8_ANALYZE_INFINITY))
441 *_end = _s + st + ln;
445 /** Fetch a character from an utf-8 encoded string.
446 * @param _s The start of an utf-8 encoded multi-byte character.
447 * @param _end Will point to after the first multi-byte character.
448 * @return The 32-bit integer representation of the first multi-byte character or 0 for invalid characters.
450 Uchar u8_getnchar(const char *_s, const char **_end, size_t _maxlen)
455 if (!utf8_enable.integer)
459 /* Careful: if we disable utf8 but not freetype, we wish to see freetype chars
460 * for normal letters. So use E000+x for special chars, but leave the freetype stuff for the
463 if (!char_usefont[(unsigned int)*(const unsigned char*)_s])
464 return 0xE000 + (Uchar)*(const unsigned char*)_s;
465 return (Uchar)*(const unsigned char*)_s;
468 if (!u8_analyze(_s, &st, &ln, &ch, _maxlen))
471 *_end = _s + st + ln;
475 /** Encode a wide-character into utf-8.
476 * @param w The wide character to encode.
477 * @param to The target buffer the utf-8 encoded string is stored to.
478 * @param maxlen The maximum number of bytes that fit into the target buffer.
479 * @return Number of bytes written to the buffer not including the terminating null.
480 * Less or equal to 0 if the buffer is too small.
482 int u8_fromchar(Uchar w, char *to, size_t maxlen)
490 if (w >= 0xE000 && !utf8_enable.integer)
493 if (w < 0x80 || !utf8_enable.integer)
501 // for a little speedup
510 to[1] = 0x80 | (w & 0x3F); w >>= 6;
522 to[2] = 0x80 | (w & 0x3F); w >>= 6;
523 to[1] = 0x80 | (w & 0x3F); w >>= 6;
537 to[3] = 0x80 | (w & 0x3F); w >>= 6;
538 to[2] = 0x80 | (w & 0x3F); w >>= 6;
539 to[1] = 0x80 | (w & 0x3F); w >>= 6;
546 /** uses u8_fromchar on a static buffer
547 * @param ch The unicode character to convert to encode
548 * @param l The number of bytes without the terminating null.
549 * @return A statically allocated buffer containing the character's utf8 representation, or NULL if it fails.
551 char *u8_encodech(Uchar ch, size_t *l)
555 len = u8_fromchar(ch, buf, sizeof(buf));
564 /** Convert a utf-8 multibyte string to a wide character string.
565 * @param wcs The target wide-character buffer.
566 * @param mb The utf-8 encoded multibyte string to convert.
567 * @param maxlen The maximum number of wide-characters that fit into the target buffer.
568 * @return The number of characters written to the target buffer.
570 size_t u8_mbstowcs(Uchar *wcs, const char *mb, size_t maxlen)
576 for (i = 0; *mb && i < maxlen-1; ++i)
578 ch = u8_getchar(mb, &mb);
587 /** Convert a wide-character string to a utf-8 multibyte string.
588 * @param mb The target buffer the utf-8 string is written to.
589 * @param wcs The wide-character string to convert.
590 * @param maxlen The number bytes that fit into the multibyte target buffer.
591 * @return The number of bytes written, not including the terminating \0
593 size_t u8_wcstombs(char *mb, const Uchar *wcs, size_t maxlen)
596 const char *start = mb;
599 for (i = 0; wcs[i] && i < maxlen-1; ++i)
602 if ( (len = u8_fromchar(wcs[i], mb, maxlen - i)) < 0)
612 UTF-8 aware COM_StringLengthNoColors
614 calculates the visible width of a color coded string.
616 *valid is filled with TRUE if the string is a valid colored string (that is, if
617 it does not end with an unfinished color code). If it gets filled with FALSE, a
618 fix would be adding a STRING_COLOR_TAG at the end of the string.
620 valid can be set to NULL if the caller doesn't care.
622 For size_s, specify the maximum number of characters from s to use, or 0 to use
623 all characters until the zero terminator.
627 COM_StringLengthNoColors(const char *s, size_t size_s, qboolean *valid);
629 u8_COM_StringLengthNoColors(const char *_s, size_t size_s, qboolean *valid)
631 const unsigned char *s = (const unsigned char*)_s;
632 const unsigned char *end;
635 if (!utf8_enable.integer)
636 return COM_StringLengthNoColors(_s, size_s, valid);
638 end = size_s ? (s + size_s) : NULL;
642 switch((s == end) ? 0 : *s)
648 case STRING_COLOR_TAG:
650 switch((s == end) ? 0 : *s)
652 case STRING_COLOR_RGB_TAG_CHAR:
653 if (s+1 != end && isxdigit(s[1]) &&
654 s+2 != end && isxdigit(s[2]) &&
655 s+3 != end && isxdigit(s[3]) )
660 ++len; // STRING_COLOR_TAG
661 ++len; // STRING_COLOR_RGB_TAG_CHAR
663 case 0: // ends with unfinished color code!
668 case STRING_COLOR_TAG: // escaped ^
671 case '0': case '1': case '2': case '3': case '4':
672 case '5': case '6': case '7': case '8': case '9': // color code
674 default: // not a color code
675 ++len; // STRING_COLOR_TAG
676 ++len; // the character
685 // start of a wide character
688 for (++s; *s >= 0x80 && *s <= 0xC0; ++s);
691 // part of a wide character, we ignore that one
699 /** Pads a utf-8 string
700 * @param out The target buffer the utf-8 string is written to.
701 * @param outsize The size of the target buffer, including the final NUL
702 * @param in The input utf-8 buffer
703 * @param leftalign Left align the output string (by default right alignment is done)
704 * @param minwidth The minimum output width
705 * @param maxwidth The maximum output width
706 * @return The number of bytes written, not including the terminating \0
708 size_t u8_strpad(char *out, size_t outsize, const char *in, qboolean leftalign, size_t minwidth, size_t maxwidth)
710 if(!utf8_enable.integer)
712 return dpsnprintf(out, outsize, "%*.*s", leftalign ? -(int) minwidth : (int) minwidth, (int) maxwidth, in);
716 size_t l = u8_bytelen(in, maxwidth);
717 size_t actual_width = u8_strnlen(in, l);
718 int pad = (actual_width >= minwidth) ? 0 : (minwidth - actual_width);
720 int lpad = leftalign ? 0 : pad;
721 int rpad = leftalign ? pad : 0;
722 return dpsnprintf(out, outsize, "%*s%.*s%*s", lpad, "", prec, in, rpad, "");