]> git.xonotic.org Git - xonotic/darkplaces.git/blob - utf8lib.h
f54547764981b299f3419b028540d16853461746
[xonotic/darkplaces.git] / utf8lib.h
1 /*
2  * UTF-8 utility functions for DarkPlaces
3  */
4 #ifndef UTF8LIB_H__
5 #define UTF8LIB_H__
6
7 #include <stddef.h>
8 #include "qtypes.h"
9
10 // types for unicode strings
11 // let them be 32 bit for now
12 // normally, whcar_t is 16 or 32 bit, 16 on linux I think, 32 on haiku and maybe windows
13
14 // Uchar, a wide character
15 typedef int32_t Uchar;
16
17 // Initialize UTF8, this registers cvars which allows for UTF8 to be disabled
18 // completely.
19 // When UTF8 is disabled, every u8_ function will work exactly as you'd expect
20 // a non-utf8 version to work: u8_strlen() will wrap to strlen()
21 // u8_byteofs() and u8_charidx() will simply return whatever is passed as index parameter
22 // u8_getchar() will will just return the next byte, u8_fromchar will write one byte, ...
23 extern struct cvar_s utf8_enable;
24 void   u8_Init(void);
25
26 size_t u8_strlen(const char*);
27 size_t u8_strnlen(const char*, size_t);
28 int    u8_byteofs(const char*, size_t, size_t*);
29 int    u8_charidx(const char*, size_t, size_t*);
30 size_t u8_bytelen(const char*, size_t);
31 size_t u8_prevbyte(const char*, size_t);
32 Uchar  u8_getchar_utf8_enabled(const char*, const char**);
33 Uchar  u8_getnchar_utf8_enabled(const char*, const char**, size_t);
34 int    u8_fromchar(Uchar, char*, size_t);
35 size_t u8_mbstowcs(Uchar *, const char *, size_t);
36 size_t u8_wcstombs(char*, const Uchar*, size_t);
37 size_t u8_COM_StringLengthNoColors(const char *s, size_t size_s, qbool *valid);
38
39 // returns a static buffer, use this for inlining
40 char  *u8_encodech(Uchar ch, size_t*, char*buf16);
41
42 size_t u8_strpad(char *out, size_t outsize, const char *in, qbool leftalign, size_t minwidth, size_t maxwidth);
43 size_t u8_strpad_colorcodes(char *out, size_t outsize, const char *in, qbool leftalign, size_t minwidth, size_t maxwidth);
44
45 /* Careful: if we disable utf8 but not freetype, we wish to see freetype chars
46  * for normal letters. So use E000+x for special chars, but leave the freetype stuff for the
47  * rest:
48  */
49 extern Uchar u8_quake2utf8map[256];
50 // these defines get a bit tricky, as c and e may be aliased to the same variable
51 #define u8_getchar(c,e) (utf8_enable.integer ? u8_getchar_utf8_enabled(c,e) : (u8_quake2utf8map[(unsigned char)(*(e) = (c) + 1)[-1]]))
52 #define u8_getchar_noendptr(c) (utf8_enable.integer ? u8_getchar_utf8_enabled(c,NULL) : (u8_quake2utf8map[(unsigned char)*(c)]))
53 #define u8_getchar_check(c,e) ((e) ? u8_getchar((c),(e)) : u8_getchar_noendptr((c)))
54 #define u8_getnchar(c,e,n) (utf8_enable.integer ? u8_getnchar_utf8_enabled(c,e,n) : ((n) <= 0 ? ((*(e) = c), 0) : (u8_quake2utf8map[(unsigned char)(*(e) = (c) + 1)[-1]])))
55 #define u8_getnchar_noendptr(c,n) (utf8_enable.integer ? u8_getnchar_utf8_enabled(c,NULL,n) : ((n) <= 0 ? 0  : (u8_quake2utf8map[(unsigned char)*(c)])))
56 #define u8_getnchar_check(c,e,n) ((e) ? u8_getchar((c),(e),(n)) : u8_getchar_noendptr((c),(n)))
57
58 Uchar u8_toupper(Uchar ch);
59 Uchar u8_tolower(Uchar ch);
60
61 #endif // UTF8LIB_H__