Mercurial > otakunoraifu
annotate font/text_stream.cc @ 68:043d5db57474
Fix index_series implementation (still incomplete)
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Wed, 23 Feb 2011 16:19:11 +0100 |
parents | 4416cfac86ae |
children |
rev | line source |
---|---|
0 | 1 /* |
2 * Copyright (c) 2004 Kazunori "jagarl" Ueno | |
3 * All rights reserved. | |
4 * | |
5 * Redistribution and use in source and binary forms, with or without | |
6 * modification, are permitted provided that the following conditions | |
7 * are met: | |
8 * 1. Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * 2. Redistributions in binary form must reproduce the above copyright | |
11 * notice, this list of conditions and the following disclaimer in the | |
12 * documentation and/or other materials provided with the distribution. | |
13 * 3. The name of the author may not be used to endorse or promote products | |
14 * derived from this software without specific prior written permission. | |
15 * | |
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
26 */ | |
27 | |
28 #include "text.h" | |
29 #include "codeconv.h" | |
52 | 30 #include <string.h> |
31 #include <stdio.h> | |
53
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
32 #include <stdlib.h> |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
33 #include "system/system_config.h" |
0 | 34 |
35 /************************************************************************ | |
36 ** | |
37 ** TextStream | |
38 ** | |
39 */ | |
40 | |
53
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
41 static char* wstrchr(const char* s, unsigned int chr) { |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
42 int ws, wc; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
43 while(*s != 0) { |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
44 if (*s < 0 && s[1] != 0) { |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
45 wc = int((unsigned char)(s[0]))*0x100 + int((unsigned char)(s[1])); |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
46 ws = 2; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
47 } else { |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
48 wc = (unsigned char)(s[0]); |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
49 ws = 1; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
50 } |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
51 if (wc == chr) return (char*)s; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
52 s += ws; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
53 } |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
54 return NULL; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
55 } |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
56 |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
57 TextStream TextStream::ParseMoji(const char* str, int def_r, int def_g, int def_b, int def_size) { |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
58 TextStream ts; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
59 ts.kanji_type = TextStream::sjis; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
60 ts.SetColor(def_r, def_g, def_b); |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
61 char* copy_str = new char[strlen(str)+1]; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
62 char* next_str; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
63 char* retptr; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
64 int var; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
65 |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
66 while( (next_str = wstrchr(str, '#')) != NULL) { |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
67 int len = next_str - str; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
68 strncpy(copy_str, str, len); |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
69 copy_str[len] = 0; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
70 ts.Add(copy_str); |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
71 str = next_str + 1; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
72 |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
73 switch(str[0]) { |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
74 case '#': // separator |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
75 str += 1; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
76 break; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
77 case 'D': case 'd': // return |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
78 ts.AddReturn(); |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
79 str += 1; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
80 break; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
81 case 'C': case 'c': // color |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
82 str += 1; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
83 var = strtol(str, &next_str,10); |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
84 if (var == 0 && str == next_str) { // no parameter |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
85 ts.SetColor(def_r, def_g, def_b); |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
86 } else { |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
87 int r,g,b; char key[1024]; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
88 sprintf(key, "#COLOR_TABLE.%03d", var); |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
89 if (AyuSysConfig::GetInstance()->GetParam(key, 3, &r, &g, &b)) { // color not found |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
90 r = g = b = 0; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
91 } |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
92 ts.SetColor(r, g, b); |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
93 str = next_str; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
94 } |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
95 break; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
96 case 'S': case 's': // size |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
97 str += 1; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
98 var = strtol(str, &next_str, 10); |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
99 if (var == 0 && str == next_str) { // no parameter |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
100 ts.SetSize(1); |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
101 } else { |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
102 if (def_size == 0) def_size = 20; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
103 if (var <= 0) var = 1; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
104 ts.SetSize(double(var)/def_size); |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
105 } |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
106 break; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
107 case 'X': case 'x': // xpos : not supported |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
108 case 'Y': case 'y': // ypos : not supported |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
109 default: |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
110 ts.Add("#"); |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
111 break; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
112 } |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
113 } |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
114 ts.Add(str); |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
115 delete[] copy_str; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
116 return ts; |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
117 } |
ddbcbd000206
* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons
thib
parents:
52
diff
changeset
|
118 |
0 | 119 TextStream::TextStream(void) { |
120 kanji_type = euc; | |
121 } | |
52 | 122 |
0 | 123 void TextStream::SetSize(double scale) { |
124 TextElem elem; | |
125 elem.type = TextElem::size; | |
126 elem.impl.Size.scale = scale; | |
127 container.push_back(elem); | |
128 } | |
52 | 129 |
0 | 130 void TextStream::SetColor(unsigned char r, unsigned char g, unsigned char b) { |
131 TextElem elem; | |
132 elem.type = TextElem::color; | |
133 elem.impl.Color.r = r; | |
134 elem.impl.Color.g = g; | |
135 elem.impl.Color.b = b; | |
136 container.push_back(elem); | |
137 } | |
52 | 138 |
0 | 139 void TextStream::InsertColor(int begin_pos, int end_pos, unsigned char r, unsigned char g, unsigned char b) { |
140 TextElem elem; | |
141 if (begin_pos < 0) begin_pos = 0; | |
142 if (begin_pos > container.size()) begin_pos = container.size(); | |
143 if (end_pos < 0) end_pos = 0; | |
144 if (end_pos > container.size()) end_pos = container.size(); | |
145 if (begin_pos >= end_pos) return; | |
146 | |
147 elem.type = TextElem::color; | |
148 elem.impl.Color.r = 255; | |
149 elem.impl.Color.g = 255; | |
150 elem.impl.Color.b = 255; | |
151 container.insert(container.begin()+end_pos, elem); | |
152 elem.impl.Color.r = r; | |
153 elem.impl.Color.g = g; | |
154 elem.impl.Color.b = b; | |
155 container.insert(container.begin()+begin_pos, elem); | |
156 Iterator it = container.begin()+begin_pos+1; | |
157 Iterator end = container.begin()+end_pos+1; | |
158 for (; it != end; it++) { | |
159 if (it->type == TextElem::color) { | |
160 TextElem& elem = *it; | |
161 if (elem.impl.Color.r == 255 && elem.impl.Color.g == 255 && elem.impl.Color.b == 255) { | |
162 elem.impl.Color.r = r; | |
163 elem.impl.Color.g = g; | |
164 elem.impl.Color.b = b; | |
165 } | |
166 } | |
167 } | |
168 } | |
52 | 169 |
0 | 170 void TextStream::Clear(void) { |
171 container.clear(); | |
172 } | |
52 | 173 |
0 | 174 void TextStream::Add(const char* str) { |
175 TextElem elem; | |
176 for (; *str; str++) { | |
177 if (*str >= 0x20) { | |
178 elem.type = TextElem::glyph; | |
179 elem.impl.Glyph.code = *str; | |
180 } else if (*str < 0 && str[1] != 0) { | |
181 elem.type = TextElem::glyph; | |
182 elem.impl.Glyph.code = ((int(*(unsigned char*)str))<<8) | int(*(unsigned char*)(str+1)); | |
183 if (kanji_type == sjis) elem.impl.Glyph.code = codeconv_sjis_to_euc(elem.impl.Glyph.code); | |
184 str++; | |
185 } else { | |
186 continue; | |
187 } | |
188 container.push_back(elem); | |
189 } | |
190 } | |
52 | 191 |
0 | 192 void TextStream::AddReturn(void) { |
193 TextElem elem; | |
194 elem.type = TextElem::escape; | |
195 elem.impl.Escape.type = TextElem::ret; | |
196 container.push_back(elem); | |
197 } | |
52 | 198 |
0 | 199 void TextStream::AddName(const char* str) { |
200 TextElem elem; | |
201 elem.type = TextElem::escape; | |
202 elem.impl.Escape.type = TextElem::name_start; | |
203 container.push_back(elem); | |
204 Add(str); | |
205 elem.impl.Escape.type = TextElem::name_end; | |
206 container.push_back(elem); | |
207 } | |
208 | |
209 void TextStream::AddRuby(const char* str, const char* ruby) { | |
210 TextElem elem; | |
211 elem.type = TextElem::escape; | |
212 elem.impl.Escape.type = TextElem::ruby_start; | |
213 container.push_back(elem); | |
214 Add(str); | |
215 elem.impl.Escape.type = TextElem::ruby_startruby; | |
216 container.push_back(elem); | |
217 Add(ruby); | |
218 elem.impl.Escape.type = TextElem::ruby_end; | |
219 container.push_back(elem); | |
220 } | |
221 | |
222 void TextStream::RemoveName(char* name, int namelen) { | |
223 Iterator it; | |
224 for (it = container.begin(); it != container.end(); it++) { | |
225 if (it->type == TextElem::escape && it->impl.Escape.type == TextElem::name_start) { | |
65
4416cfac86ae
Convert EUC-JP files to UTF8
Thibaut Girka <thib@sitedethib.com>
parents:
53
diff
changeset
|
226 // 行頭の名前? |
0 | 227 int pt = it - container.begin(); |
228 Iterator name_start = it; | |
229 for (; it != container.end(); it++) { | |
230 if (it->type == TextElem::escape && it->impl.Escape.type == TextElem::name_end) break; | |
231 } | |
232 if (it != container.end()) { | |
65
4416cfac86ae
Convert EUC-JP files to UTF8
Thibaut Girka <thib@sitedethib.com>
parents:
53
diff
changeset
|
233 // 名前が見つかったので削除 |
4416cfac86ae
Convert EUC-JP files to UTF8
Thibaut Girka <thib@sitedethib.com>
parents:
53
diff
changeset
|
234 if (name) { // 保存先があるなら保存する |
0 | 235 Iterator name_end = it; |
236 int pos = 0; | |
237 namelen--; | |
238 for (it=name_start; it != name_end; it++) { | |
239 if (it->type == TextElem::glyph) { | |
240 unsigned int code = it->impl.Glyph.code; | |
241 if (code < 0x100) { | |
242 if (pos < namelen) name[pos++] = code; | |
243 } else { | |
244 if (pos < namelen) name[pos++] = code>>8; | |
245 if (pos < namelen) name[pos++] = code; | |
246 } | |
247 } | |
248 } | |
249 name[pos] = 0; | |
65
4416cfac86ae
Convert EUC-JP files to UTF8
Thibaut Girka <thib@sitedethib.com>
parents:
53
diff
changeset
|
250 name = 0; // 最初に出た名前のみ保存する |
0 | 251 } |
252 it++; | |
253 container.erase(name_start, it); | |
254 it = container.begin() + pt; | |
255 } | |
256 } | |
257 for (;it != container.end(); it++) { | |
258 if (it->type == TextElem::escape && it->impl.Escape.type == TextElem::ret) break; | |
259 } | |
260 if (it == container.end()) break; | |
261 } | |
262 } | |
263 | |
264 /************************************************************************ | |
265 ** | |
266 ** TextStream::Save | |
267 ** | |
268 */ | |
269 | |
270 /* escape sequence : '=XX' ; x < 0x30 || 'x>0x39 && x<0x40' (symbols) */ | |
271 /* !"#$%&'()*+,-./:;<=>? */ | |
272 | |
273 void TextStream::Load(const std::string& from_s) { | |
274 /* kigou : !"#$%&'()*+,-./:;<=>? */ | |
275 const char* s = from_s.c_str(); | |
276 container.clear(); | |
277 if (*s == '=' && *s == 'e') {kanji_type = euc; s+=2;} | |
278 | |
279 TextElem e; | |
280 while(s && *s) { | |
281 e.type = TextElem::glyph; | |
282 if (*s == '=') { | |
283 if (s[1] >= 'A' && s[1] <= 'Q' && s[2] >= 'A' && s[2] <= 'Q') { // symbols | |
284 e.type = TextElem::glyph; | |
285 e.impl.Glyph.code = (s[1]-'A')*16 + (s[2]-'A'); | |
286 s += 3; | |
287 } else if (s[1] == 'x') { | |
288 e.type = TextElem::escape; | |
289 e.impl.Escape.type = TextElem::EscapeType(s[2]-'A'); | |
290 s += 3; | |
291 } else if (s[1] == 'c') { | |
292 int c; | |
293 e.type = TextElem::color; | |
294 sscanf(s+2, "%x", &c); | |
295 e.impl.Color.r = (c>>16)&0xff; | |
296 e.impl.Color.g = (c>>8)&0xff; | |
297 e.impl.Color.b = (c)&0xff; | |
298 s = strchr(s, '.'); | |
299 if (s) s++; | |
300 } else if (s[1] == 's') { | |
301 e.impl.Size.scale = TextElem::size; | |
302 sscanf(s+2, "%f", &e.impl.Size.scale); | |
303 s = strchr(s, '.'); | |
304 if (s) s++; | |
305 } else { | |
306 fprintf(stderr,"TextStream::Load(): Cannot convert text-stream from Serialized-data\n"); | |
307 s++; | |
308 } | |
309 } else { | |
310 if (*s < 0) { // kanji-code | |
311 if (s[1] == 0) break; | |
312 if (s[1] >= 0 && s[1] < 0x40) break; // not EUC nor SJIS | |
313 e.type = TextElem::glyph; | |
314 e.impl.Glyph.code = codeconv_sjis_to_euc(int(*(unsigned char*)(s))*0x100 + int(*(unsigned char*)(s+1))); | |
315 s += 2; | |
316 } else { // ascii-code | |
317 if (s[0] < 0x30) break; // must be escaped | |
318 if (s[0] > 0x39 && s[0] < 0x40) break; // must be escaped | |
319 e.type = TextElem::glyph; | |
320 e.impl.Glyph.code = s[0]; | |
321 s++; | |
322 } | |
323 } | |
324 container.push_back(e); | |
325 } | |
326 } | |
327 | |
328 std::string TextStream::Save(void) { | |
329 /* kigou : !"#$%&'()*+,-./:;<=>? */ | |
330 Iterator it; | |
331 std::string ret_s; | |
332 char buf_orig[1024]; | |
333 char* buf = buf_orig; | |
334 for (it=container.begin(); it != container.end(); it++) { | |
335 TextElem& e = *it; | |
336 switch(e.type) { | |
337 case TextElem::glyph: { | |
338 int code = e.impl.Glyph.code; | |
339 if (code < 0x30 || (code > 0x39 && code < 0x40)) { | |
340 *buf++ = '='; | |
341 *buf++ = (code/0x10) + 'A'; | |
342 *buf++ = (code%0x10) + 'A'; | |
343 } else { | |
65
4416cfac86ae
Convert EUC-JP files to UTF8
Thibaut Girka <thib@sitedethib.com>
parents:
53
diff
changeset
|
344 code = codeconv_euc_to_sjis(code); // save file の漢字コードはSJIS |
0 | 345 *buf++ = code/256; |
346 *buf++ = code%256; | |
347 } | |
348 break; | |
349 } | |
350 case TextElem::escape: | |
351 sprintf(buf, "=x%c", e.impl.Escape.type+'A'); | |
352 buf += 3; | |
353 break; | |
354 case TextElem::color: | |
355 sprintf(buf, "=c%x.",int(e.impl.Color.r)*256*256+int(e.impl.Color.g)*256+e.impl.Color.b); | |
356 buf += strlen(buf); | |
357 break; | |
358 case TextElem::size: | |
359 sprintf(buf, "=s%f.", e.impl.Size.scale); | |
360 buf += strlen(buf); | |
361 break; | |
362 } | |
363 if (buf-buf_orig > 1000) { | |
364 *buf = 0; | |
365 ret_s += buf_orig; | |
366 buf = buf_orig; | |
367 } | |
368 } | |
369 *buf = 0; | |
370 ret_s += buf_orig; | |
371 buf = buf_orig; | |
372 return ret_s; | |
373 } | |
374 | |
375 /************************************************************************ | |
376 ** | |
377 ** TextGlyphStream | |
378 ** | |
379 */ | |
380 | |
381 void TextGlyphStream::SetColor(int r, int g, int b) { | |
382 iterator it; | |
383 for (it=begin(); it != end(); it++) { | |
384 // if (it->r == 255 && it->g == 255 && it->b == 255) { | |
385 it->r = r; | |
386 it->g = g; | |
387 it->b = b; | |
388 // } | |
389 } | |
390 } | |
391 void TextGlyphStream::SetReverse(bool is_rev) { | |
392 iterator it; | |
393 for (it=begin(); it != end(); it++) { | |
394 it->is_rev = is_rev; | |
395 } | |
396 } |