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"
|
1
|
30 #include<string.h>
|
0
|
31 #include<stdio.h>
|
|
32
|
|
33 /************************************************************************
|
|
34 **
|
|
35 ** TextStream
|
|
36 **
|
|
37 */
|
|
38
|
|
39 TextStream::TextStream(void) {
|
|
40 kanji_type = euc;
|
|
41 }
|
|
42 void TextStream::SetSize(double scale) {
|
|
43 TextElem elem;
|
|
44 elem.type = TextElem::size;
|
|
45 elem.impl.Size.scale = scale;
|
|
46 container.push_back(elem);
|
|
47 }
|
|
48 void TextStream::SetColor(unsigned char r, unsigned char g, unsigned char b) {
|
|
49 TextElem elem;
|
|
50 elem.type = TextElem::color;
|
|
51 elem.impl.Color.r = r;
|
|
52 elem.impl.Color.g = g;
|
|
53 elem.impl.Color.b = b;
|
|
54 container.push_back(elem);
|
|
55 }
|
|
56 void TextStream::InsertColor(int begin_pos, int end_pos, unsigned char r, unsigned char g, unsigned char b) {
|
|
57 TextElem elem;
|
|
58 if (begin_pos < 0) begin_pos = 0;
|
|
59 if (begin_pos > container.size()) begin_pos = container.size();
|
|
60 if (end_pos < 0) end_pos = 0;
|
|
61 if (end_pos > container.size()) end_pos = container.size();
|
|
62 if (begin_pos >= end_pos) return;
|
|
63
|
|
64 elem.type = TextElem::color;
|
|
65 elem.impl.Color.r = 255;
|
|
66 elem.impl.Color.g = 255;
|
|
67 elem.impl.Color.b = 255;
|
|
68 container.insert(container.begin()+end_pos, elem);
|
|
69 elem.impl.Color.r = r;
|
|
70 elem.impl.Color.g = g;
|
|
71 elem.impl.Color.b = b;
|
|
72 container.insert(container.begin()+begin_pos, elem);
|
|
73 Iterator it = container.begin()+begin_pos+1;
|
|
74 Iterator end = container.begin()+end_pos+1;
|
|
75 for (; it != end; it++) {
|
|
76 if (it->type == TextElem::color) {
|
|
77 TextElem& elem = *it;
|
|
78 if (elem.impl.Color.r == 255 && elem.impl.Color.g == 255 && elem.impl.Color.b == 255) {
|
|
79 elem.impl.Color.r = r;
|
|
80 elem.impl.Color.g = g;
|
|
81 elem.impl.Color.b = b;
|
|
82 }
|
|
83 }
|
|
84 }
|
|
85 }
|
|
86 void TextStream::Clear(void) {
|
|
87 container.clear();
|
|
88 }
|
|
89 void TextStream::Add(const char* str) {
|
|
90 TextElem elem;
|
|
91 for (; *str; str++) {
|
|
92 if (*str >= 0x20) {
|
|
93 elem.type = TextElem::glyph;
|
|
94 elem.impl.Glyph.code = *str;
|
|
95 } else if (*str < 0 && str[1] != 0) {
|
|
96 elem.type = TextElem::glyph;
|
|
97 elem.impl.Glyph.code = ((int(*(unsigned char*)str))<<8) | int(*(unsigned char*)(str+1));
|
|
98 if (kanji_type == sjis) elem.impl.Glyph.code = codeconv_sjis_to_euc(elem.impl.Glyph.code);
|
|
99 str++;
|
|
100 } else {
|
|
101 continue;
|
|
102 }
|
|
103 container.push_back(elem);
|
|
104 }
|
|
105 }
|
|
106 void TextStream::AddReturn(void) {
|
|
107 TextElem elem;
|
|
108 elem.type = TextElem::escape;
|
|
109 elem.impl.Escape.type = TextElem::ret;
|
|
110 container.push_back(elem);
|
|
111 }
|
|
112 void TextStream::AddName(const char* str) {
|
|
113 TextElem elem;
|
|
114 elem.type = TextElem::escape;
|
|
115 elem.impl.Escape.type = TextElem::name_start;
|
|
116 container.push_back(elem);
|
|
117 Add(str);
|
|
118 elem.impl.Escape.type = TextElem::name_end;
|
|
119 container.push_back(elem);
|
|
120 }
|
|
121
|
|
122 void TextStream::AddRuby(const char* str, const char* ruby) {
|
|
123 TextElem elem;
|
|
124 elem.type = TextElem::escape;
|
|
125 elem.impl.Escape.type = TextElem::ruby_start;
|
|
126 container.push_back(elem);
|
|
127 Add(str);
|
|
128 elem.impl.Escape.type = TextElem::ruby_startruby;
|
|
129 container.push_back(elem);
|
|
130 Add(ruby);
|
|
131 elem.impl.Escape.type = TextElem::ruby_end;
|
|
132 container.push_back(elem);
|
|
133 }
|
|
134
|
|
135 void TextStream::RemoveName(char* name, int namelen) {
|
|
136 Iterator it;
|
|
137 for (it = container.begin(); it != container.end(); it++) {
|
|
138 if (it->type == TextElem::escape && it->impl.Escape.type == TextElem::name_start) {
|
|
139 // 行頭の名前?
|
|
140 int pt = it - container.begin();
|
|
141 Iterator name_start = it;
|
|
142 for (; it != container.end(); it++) {
|
|
143 if (it->type == TextElem::escape && it->impl.Escape.type == TextElem::name_end) break;
|
|
144 }
|
|
145 if (it != container.end()) {
|
|
146 // 名前が見つかったので削除
|
|
147 if (name) { // 保存先があるなら保存する
|
|
148 Iterator name_end = it;
|
|
149 int pos = 0;
|
|
150 namelen--;
|
|
151 for (it=name_start; it != name_end; it++) {
|
|
152 if (it->type == TextElem::glyph) {
|
|
153 unsigned int code = it->impl.Glyph.code;
|
|
154 if (code < 0x100) {
|
|
155 if (pos < namelen) name[pos++] = code;
|
|
156 } else {
|
|
157 if (pos < namelen) name[pos++] = code>>8;
|
|
158 if (pos < namelen) name[pos++] = code;
|
|
159 }
|
|
160 }
|
|
161 }
|
|
162 name[pos] = 0;
|
|
163 name = 0; // 最初に出た名前のみ保存する
|
|
164 }
|
|
165 it++;
|
|
166 container.erase(name_start, it);
|
|
167 it = container.begin() + pt;
|
|
168 }
|
|
169 }
|
|
170 for (;it != container.end(); it++) {
|
|
171 if (it->type == TextElem::escape && it->impl.Escape.type == TextElem::ret) break;
|
|
172 }
|
|
173 if (it == container.end()) break;
|
|
174 }
|
|
175 }
|
|
176
|
|
177 /************************************************************************
|
|
178 **
|
|
179 ** TextStream::Save
|
|
180 **
|
|
181 */
|
|
182
|
|
183 /* escape sequence : '=XX' ; x < 0x30 || 'x>0x39 && x<0x40' (symbols) */
|
|
184 /* !"#$%&'()*+,-./:;<=>? */
|
|
185
|
|
186 void TextStream::Load(const std::string& from_s) {
|
|
187 /* kigou : !"#$%&'()*+,-./:;<=>? */
|
|
188 const char* s = from_s.c_str();
|
|
189 container.clear();
|
|
190 if (*s == '=' && *s == 'e') {kanji_type = euc; s+=2;}
|
|
191
|
|
192 TextElem e;
|
|
193 while(s && *s) {
|
|
194 e.type = TextElem::glyph;
|
|
195 if (*s == '=') {
|
|
196 if (s[1] >= 'A' && s[1] <= 'Q' && s[2] >= 'A' && s[2] <= 'Q') { // symbols
|
|
197 e.type = TextElem::glyph;
|
|
198 e.impl.Glyph.code = (s[1]-'A')*16 + (s[2]-'A');
|
|
199 s += 3;
|
|
200 } else if (s[1] == 'x') {
|
|
201 e.type = TextElem::escape;
|
|
202 e.impl.Escape.type = TextElem::EscapeType(s[2]-'A');
|
|
203 s += 3;
|
|
204 } else if (s[1] == 'c') {
|
|
205 int c;
|
|
206 e.type = TextElem::color;
|
|
207 sscanf(s+2, "%x", &c);
|
|
208 e.impl.Color.r = (c>>16)&0xff;
|
|
209 e.impl.Color.g = (c>>8)&0xff;
|
|
210 e.impl.Color.b = (c)&0xff;
|
|
211 s = strchr(s, '.');
|
|
212 if (s) s++;
|
|
213 } else if (s[1] == 's') {
|
|
214 e.impl.Size.scale = TextElem::size;
|
|
215 sscanf(s+2, "%f", &e.impl.Size.scale);
|
|
216 s = strchr(s, '.');
|
|
217 if (s) s++;
|
|
218 } else {
|
|
219 fprintf(stderr,"TextStream::Load(): Cannot convert text-stream from Serialized-data\n");
|
|
220 s++;
|
|
221 }
|
|
222 } else {
|
|
223 if (*s < 0) { // kanji-code
|
|
224 if (s[1] == 0) break;
|
|
225 if (s[1] >= 0 && s[1] < 0x40) break; // not EUC nor SJIS
|
|
226 e.type = TextElem::glyph;
|
|
227 e.impl.Glyph.code = codeconv_sjis_to_euc(int(*(unsigned char*)(s))*0x100 + int(*(unsigned char*)(s+1)));
|
|
228 s += 2;
|
|
229 } else { // ascii-code
|
|
230 if (s[0] < 0x30) break; // must be escaped
|
|
231 if (s[0] > 0x39 && s[0] < 0x40) break; // must be escaped
|
|
232 e.type = TextElem::glyph;
|
|
233 e.impl.Glyph.code = s[0];
|
|
234 s++;
|
|
235 }
|
|
236 }
|
|
237 container.push_back(e);
|
|
238 }
|
|
239 }
|
|
240
|
|
241 std::string TextStream::Save(void) {
|
|
242 /* kigou : !"#$%&'()*+,-./:;<=>? */
|
|
243 Iterator it;
|
|
244 std::string ret_s;
|
|
245 char buf_orig[1024];
|
|
246 char* buf = buf_orig;
|
|
247 for (it=container.begin(); it != container.end(); it++) {
|
|
248 TextElem& e = *it;
|
|
249 switch(e.type) {
|
|
250 case TextElem::glyph: {
|
|
251 int code = e.impl.Glyph.code;
|
|
252 if (code < 0x30 || (code > 0x39 && code < 0x40)) {
|
|
253 *buf++ = '=';
|
|
254 *buf++ = (code/0x10) + 'A';
|
|
255 *buf++ = (code%0x10) + 'A';
|
|
256 } else {
|
|
257 code = codeconv_euc_to_sjis(code); // save file の漢字コードはSJIS
|
|
258 *buf++ = code/256;
|
|
259 *buf++ = code%256;
|
|
260 }
|
|
261 break;
|
|
262 }
|
|
263 case TextElem::escape:
|
|
264 sprintf(buf, "=x%c", e.impl.Escape.type+'A');
|
|
265 buf += 3;
|
|
266 break;
|
|
267 case TextElem::color:
|
|
268 sprintf(buf, "=c%x.",int(e.impl.Color.r)*256*256+int(e.impl.Color.g)*256+e.impl.Color.b);
|
|
269 buf += strlen(buf);
|
|
270 break;
|
|
271 case TextElem::size:
|
|
272 sprintf(buf, "=s%f.", e.impl.Size.scale);
|
|
273 buf += strlen(buf);
|
|
274 break;
|
|
275 }
|
|
276 if (buf-buf_orig > 1000) {
|
|
277 *buf = 0;
|
|
278 ret_s += buf_orig;
|
|
279 buf = buf_orig;
|
|
280 }
|
|
281 }
|
|
282 *buf = 0;
|
|
283 ret_s += buf_orig;
|
|
284 buf = buf_orig;
|
|
285 return ret_s;
|
|
286 }
|
|
287
|
|
288 /************************************************************************
|
|
289 **
|
|
290 ** TextGlyphStream
|
|
291 **
|
|
292 */
|
|
293
|
|
294 void TextGlyphStream::SetColor(int r, int g, int b) {
|
|
295 iterator it;
|
|
296 for (it=begin(); it != end(); it++) {
|
|
297 // if (it->r == 255 && it->g == 255 && it->b == 255) {
|
|
298 it->r = r;
|
|
299 it->g = g;
|
|
300 it->b = b;
|
|
301 // }
|
|
302 }
|
|
303 }
|
|
304 void TextGlyphStream::SetReverse(bool is_rev) {
|
|
305 iterator it;
|
|
306 for (it=begin(); it != end(); it++) {
|
|
307 it->is_rev = is_rev;
|
|
308 }
|
|
309 }
|