aboutsummaryrefslogtreecommitdiff
path: root/project/dclib/dclib-utf8.h
blob: 86556c5aacbc541f44e19acc76d310bfdb4c1592 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

/***************************************************************************
 *                                                                         *
 *                     _____     ____                                      *
 *                    |  __ \   / __ \   _     _ _____                     *
 *                    | |  \ \ / /  \_\ | |   | |  _  \                    *
 *                    | |   \ \| |      | |   | | |_| |                    *
 *                    | |   | || |      | |   | |  ___/                    *
 *                    | |   / /| |   __ | |   | |  _  \                    *
 *                    | |__/ / \ \__/ / | |___| | |_| |                    *
 *                    |_____/   \____/  |_____|_|_____/                    *
 *                                                                         *
 *                       Wiimms source code library                        *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *        Copyright (c) 2012-2022 by Dirk Clemens <wiimm@wiimm.de>         *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *   This library is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This library is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt       *
 *                                                                         *
 ***************************************************************************/

#ifndef DCLIB_UTF8_H
#define DCLIB_UTF8_H 1
#ifndef WIN_DCLIB

#include "dclib-basics.h"

///////////////////////////////////////////////////////////////////////////////
/////   this software is taken from dcLib2 and now publiced under GPL2.   /////
///////////////////////////////////////////////////////////////////////////////
// [[dcUnicodeConsts]]

typedef enum dcUnicodeConsts
{
    DCLIB_UNICODE_MAX_UTF8_1	= 0x7f,
    DCLIB_UNICODE_MAX_UTF8_2	= 0x7ff,
    DCLIB_UNICODE_MAX_UTF8_3	= 0xffff,
    DCLIB_UNICODE_MAX_UTF8_4	= 0x1fffff,

    DCLIB_UNICODE_CODE_MASK	= 0x1fffff,

} dcUnicodeConsts;

///////////////////////////////////////////////////////////////////////////////
// [[dcUTF8Mode]]

typedef enum dcUTF8Mode
{
    DC_UTF8_ILLEGAL		= 0x0000, // Illegale UTF8 Zeichen-Kombination

    DC_UTF8_1CHAR		= 0x0001, // Das Zeichen ist ein Einzelzeichen

    DC_UTF8_2CHAR		= 0x0002, // Beginn einer 2-Zeichen Sequenz
    DC_UTF8_CONT_22		= 0x0004, // ein Fortsetzungszeichen an Pos 2 einer 2-er Sequenz

    DC_UTF8_3CHAR		= 0x0008, // Beginn einer 3-Zeichen Sequenz
    DC_UTF8_CONT_23		= 0x0010, // ein Fortsetzungszeichen an Pos 2 einer 3-er Sequenz
    DC_UTF8_CONT_33		= 0x0020, // ein Fortsetzungszeichen an Pos 3 einer 3-er Sequenz

    DC_UTF8_4CHAR		= 0x0040, // Beginn einer 4-Zeichen Sequenz
    DC_UTF8_CONT_24		= 0x0080, // ein Fortsetzungszeichen an Pos 2 einer 4-er Sequenz
    DC_UTF8_CONT_34		= 0x0100, // ein Fortsetzungszeichen an Pos 3 einer 4-er Sequenz
    DC_UTF8_CONT_44		= 0x0200, // ein Fortsetzungszeichen an Pos 4 einer 4-er Sequenz

    DC_UTF8_CONT_ANY		= 0x0400, // ein Fortsetzungszeichen an beliebger Stelle

    DC_UTF8_1CHAR_POSSIBLE	= 0x0800, // als Einzelzeichen darstellbar
    DC_UTF8_2CHAR_POSSIBLE	= 0x1000, // als 2-er Sequenz darstellbar
    DC_UTF8_3CHAR_POSSIBLE	= 0x2000, // als 3-er Sequenz darstellbar
    DC_UTF8_4CHAR_POSSIBLE	= 0x4000, // als 4-er Sequenz darstellbar

} dcUTF8Mode;

///////////////////////////////////////////////////////////////////////////////

extern const unsigned short TableUTF8Mode[0x100];
static inline dcUTF8Mode CheckUTF8Mode ( unsigned char ch )
	{ return (dcUTF8Mode)TableUTF8Mode[ch]; }

int	GetUTF8CharLength ( u32 code );
char *	NextUTF8Char  ( ccp str );
char *	NextUTF8CharE ( ccp str, ccp end );
char *	PrevUTF8Char  ( ccp str );
char *  PrevUTF8CharB ( ccp str, ccp begin );
char *	SkipUTF8Char  ( ccp str, int skip );
char *	SkipUTF8CharE ( ccp str, ccp end, int skip );

u32	GetUTF8Char       ( ccp str );
u32	ScanUTF8Char      ( ccp * str );
u32	ScanUTF8CharE     ( ccp * str, ccp end );
u32	ScanUTF8CharInc   ( ccp * str );
u32	ScanUTF8CharIncE  ( ccp * str, ccp end );
u32	GetUTF8AnsiChar   ( ccp str );
u32	ScanUTF8AnsiChar  ( ccp * str );
u32	ScanUTF8AnsiCharE ( ccp * str, ccp end );
int	ScanUTF8Length	  ( ccp str );
int	ScanUTF8LengthE	  ( ccp str, ccp end );
int	CalcUTF8PrintFW   ( ccp str, ccp end, uint wanted_fw );

char *	PrintUTF8Char ( char * buf, u32 code );
char *	PrintUTF8CharToCircBuf ( u32 code );

exmem_t AlignUTF8 ( exmem_dest_t *dest, ccp str, int str_len, int fw, int prec );
ccp	AlignUTF8ToCircBuf ( ccp str, int fw, int prec );

///////////////////////////////////////////////////////////////////////////////
// special variants, that ignore known escape sequences

char *	NextEUTF8Char    ( ccp str );
char *	NextEUTF8CharE   ( ccp str, ccp end );
char *	SkipEUTF8Char    ( ccp str, int skip );
char *	SkipEUTF8CharE   ( ccp str, ccp end, int skip );
int	ScanEUTF8Length  ( ccp str );
int	ScanEUTF8LengthE ( ccp str, ccp end );

exmem_t AlignEUTF8 ( exmem_dest_t *dest, ccp str, int str_len, int fw, int prec );
ccp	AlignEUTF8ToCircBuf ( ccp str, int fw, int prec );

///////////////////////////////////////////////////////////////////////////////

static inline int strlen8 ( ccp str )
	{ return ScanUTF8Length(str); }

static inline int strlen8e ( ccp str, ccp end )
	{ return ScanUTF8LengthE(str,end); }

static inline int strlene8 ( ccp str )
	{ return ScanEUTF8Length(str); }

static inline int strlene8e ( ccp str, ccp end )
	{ return ScanEUTF8LengthE(str,end); }

///////////////////////////////////////////////////////////////////////////////

typedef struct dcUnicodeTripel
{
	u32 code1;
	u32 code2;
	u32 code3;

} dcUnicodeTripel;

extern const dcUnicodeTripel TableUnicodeDecomp[];
const dcUnicodeTripel * DecomposeUnicode ( u32 code );

//
///////////////////////////////////////////////////////////////////////////////
///////////////				END			///////////////
///////////////////////////////////////////////////////////////////////////////

#endif // WIN_DCLIB
#endif // DCLIB_UTF8_H