annotate font/text_stream.cc @ 52:15a18fbe6f21

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