]> git.xonotic.org Git - xonotic/darkplaces.git/blob - utf8lib.h
Make stepping up while jumping reliable
[xonotic/darkplaces.git] / utf8lib.h
1 /*
2 Copyright (C) 2009-2020 DarkPlaces contributors
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 /*
22  * UTF-8 utility functions for DarkPlaces
23  */
24 #ifndef UTF8LIB_H__
25 #define UTF8LIB_H__
26
27 #include <stddef.h>
28 #include "qtypes.h"
29
30 // types for unicode strings
31 // let them be 32 bit for now
32 // normally, whcar_t is 16 or 32 bit, 16 on linux I think, 32 on haiku and maybe windows
33
34 // Uchar, a wide character
35 typedef int32_t Uchar;
36
37 // Initialize UTF8, this registers cvars which allows for UTF8 to be disabled
38 // completely.
39 // When UTF8 is disabled, every u8_ function will work exactly as you'd expect
40 // a non-utf8 version to work: u8_strlen() will wrap to strlen()
41 // u8_byteofs() and u8_charidx() will simply return whatever is passed as index parameter
42 // u8_getchar() will will just return the next byte, u8_fromchar will write one byte, ...
43 extern struct cvar_s utf8_enable;
44 void   u8_Init(void);
45
46 size_t u8_strlen(const char*);
47 size_t u8_strnlen(const char*, size_t);
48 int    u8_byteofs(const char*, size_t, size_t*);
49 int    u8_charidx(const char*, size_t, size_t*);
50 size_t u8_bytelen(const char*, size_t);
51 size_t u8_prevbyte(const char*, size_t);
52 Uchar  u8_getchar_utf8_enabled(const char*, const char**);
53 Uchar  u8_getnchar_utf8_enabled(const char*, const char**, size_t);
54 int    u8_fromchar(Uchar, char*, size_t);
55 size_t u8_mbstowcs(Uchar *, const char *, size_t);
56 size_t u8_wcstombs(char*, const Uchar*, size_t);
57 size_t u8_COM_StringLengthNoColors(const char *s, size_t size_s, qbool *valid);
58
59 // returns a static buffer, use this for inlining
60 char  *u8_encodech(Uchar ch, size_t*, char*buf16);
61
62 size_t u8_strpad(char *out, size_t outsize, const char *in, qbool leftalign, size_t minwidth, size_t maxwidth);
63 size_t u8_strpad_colorcodes(char *out, size_t outsize, const char *in, qbool leftalign, size_t minwidth, size_t maxwidth);
64
65 /* Careful: if we disable utf8 but not freetype, we wish to see freetype chars
66  * for normal letters. So use E000+x for special chars, but leave the freetype stuff for the
67  * rest:
68  */
69 extern Uchar u8_quake2utf8map[256];
70 // these defines get a bit tricky, as c and e may be aliased to the same variable
71 #define u8_getchar(c,e) (utf8_enable.integer ? u8_getchar_utf8_enabled(c,e) : (u8_quake2utf8map[(unsigned char)(*(e) = (c) + 1)[-1]]))
72 #define u8_getchar_noendptr(c) (utf8_enable.integer ? u8_getchar_utf8_enabled(c,NULL) : (u8_quake2utf8map[(unsigned char)*(c)]))
73 #define u8_getchar_check(c,e) ((e) ? u8_getchar((c),(e)) : u8_getchar_noendptr((c)))
74 #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]])))
75 #define u8_getnchar_noendptr(c,n) (utf8_enable.integer ? u8_getnchar_utf8_enabled(c,NULL,n) : ((n) <= 0 ? 0  : (u8_quake2utf8map[(unsigned char)*(c)])))
76 #define u8_getnchar_check(c,e,n) ((e) ? u8_getchar((c),(e),(n)) : u8_getchar_noendptr((c),(n)))
77
78 Uchar u8_toupper(Uchar ch);
79 Uchar u8_tolower(Uchar ch);
80
81 #endif // UTF8LIB_H__