Fix redundant glTexEnv calls
[neverball] / share / text.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include <string.h>
16 #include "text.h"
17
18 /*---------------------------------------------------------------------------*/
19
20 int text_add_char(Uint32 unicode, char *string, int maxbytes)
21 {
22     size_t pos = strlen(string);
23     int l;
24
25     if      (unicode < 0x80)    l = 1;
26     else if (unicode < 0x0800)  l = 2;
27     else if (unicode < 0x10000) l = 3;
28     else                        l = 4;
29
30     if (pos + l >= maxbytes)
31         return 0;
32
33     if (unicode < 0x80)
34         string[pos++] = (char) unicode;
35     else if (unicode < 0x0800)
36     {
37         string[pos++] = (char) ((unicode >> 6) | 0xC0);
38         string[pos++] = (char) ((unicode & 0x3F) | 0x80);
39     }
40     else if (unicode < 0x10000)
41     {
42         string[pos++] = (char) ((unicode >> 12) | 0xE0);
43         string[pos++] = (char) (((unicode >> 6) & 0x3F) | 0x80);
44         string[pos++] = (char) ((unicode & 0x3F) | 0x80);
45     }
46     else
47     {
48         string[pos++] = (char) ((unicode >> 18) | 0xF0);
49         string[pos++] = (char) (((unicode >> 12) & 0x3F) | 0x80);
50         string[pos++] = (char) (((unicode >> 6) & 0x3F) | 0x80);
51         string[pos++] = (char) ((unicode & 0x3F) | 0x80);
52     }
53
54     string[pos++] = 0;
55
56     return l;
57 }
58
59 int text_del_char(char *string)
60 {
61     int pos = (int) strlen(string) - 1;
62
63     while (pos >= 0 && ((string[pos] & 0xC0) == 0x80))
64         string[pos--] = 0;
65
66     if (pos >= 0)
67     {
68         string[pos] = 0;
69         return 1;
70     }
71
72     return 0;
73 }
74
75 int text_length(const char *string)
76 {
77     int result = 0;
78
79     while (*string != '\0')
80         if ((*string++ & 0xC0) != 0x80)
81             result++;
82
83     return result;
84 }
85
86 /*---------------------------------------------------------------------------*/