| 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" |
|---|
| 30 | #include <string.h> |
|---|
| 31 | #include <stdio.h> |
|---|
| 32 | #include <stdlib.h> |
|---|
| 33 | #include "system/system_config.h" |
|---|
| 34 | |
|---|
| 35 | /************************************************************************ |
|---|
| 36 | ** |
|---|
| 37 | ** TextStream |
|---|
| 38 | ** |
|---|
| 39 | */ |
|---|
| 40 | |
|---|
| 41 | static char* wstrchr(const char* s, unsigned int chr) { |
|---|
| 42 | int ws, wc; |
|---|
| 43 | while(*s != 0) { |
|---|
| 44 | if (*s < 0 && s[1] != 0) { |
|---|
| 45 | wc = int((unsigned char)(s[0]))*0x100 + int((unsigned char)(s[1])); |
|---|
| 46 | ws = 2; |
|---|
| 47 | } else { |
|---|
| 48 | wc = (unsigned char)(s[0]); |
|---|
| 49 | ws = 1; |
|---|
| 50 | } |
|---|
| 51 | if (wc == chr) return (char*)s; |
|---|
| 52 | s += ws; |
|---|
| 53 | } |
|---|
| 54 | return NULL; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | TextStream TextStream::ParseMoji(const char* str, int def_r, int def_g, int def_b, int def_size) { |
|---|
| 58 | TextStream ts; |
|---|
| 59 | ts.kanji_type = TextStream::sjis; |
|---|
| 60 | ts.SetColor(def_r, def_g, def_b); |
|---|
| 61 | char* copy_str = new char[strlen(str)+1]; |
|---|
| 62 | char* next_str; |
|---|
| 63 | char* retptr; |
|---|
| 64 | int var; |
|---|
| 65 | |
|---|
| 66 | while( (next_str = wstrchr(str, '#')) != NULL) { |
|---|
| 67 | int len = next_str - str; |
|---|
| 68 | strncpy(copy_str, str, len); |
|---|
| 69 | copy_str[len] = 0; |
|---|
| 70 | ts.Add(copy_str); |
|---|
| 71 | str = next_str + 1; |
|---|
| 72 | |
|---|
| 73 | switch(str[0]) { |
|---|
| 74 | case '#': // separator |
|---|
| 75 | str += 1; |
|---|
| 76 | break; |
|---|
| 77 | case 'D': case 'd': // return |
|---|
| 78 | ts.AddReturn(); |
|---|
| 79 | str += 1; |
|---|
| 80 | break; |
|---|
| 81 | case 'C': case 'c': // color |
|---|
| 82 | str += 1; |
|---|
| 83 | var = strtol(str, &next_str,10); |
|---|
| 84 | if (var == 0 && str == next_str) { // no parameter |
|---|
| 85 | ts.SetColor(def_r, def_g, def_b); |
|---|
| 86 | } else { |
|---|
| 87 | int r,g,b; char key[1024]; |
|---|
| 88 | sprintf(key, "#COLOR_TABLE.%03d", var); |
|---|
| 89 | if (AyuSysConfig::GetInstance()->GetParam(key, 3, &r, &g, &b)) { // color not found |
|---|
| 90 | r = g = b = 0; |
|---|
| 91 | } |
|---|
| 92 | ts.SetColor(r, g, b); |
|---|
| 93 | str = next_str; |
|---|
| 94 | } |
|---|
| 95 | break; |
|---|
| 96 | case 'S': case 's': // size |
|---|
| 97 | str += 1; |
|---|
| 98 | var = strtol(str, &next_str, 10); |
|---|
| 99 | if (var == 0 && str == next_str) { // no parameter |
|---|
| 100 | ts.SetSize(1); |
|---|
| 101 | } else { |
|---|
| 102 | if (def_size == 0) def_size = 20; |
|---|
| 103 | if (var <= 0) var = 1; |
|---|
| 104 | ts.SetSize(double(var)/def_size); |
|---|
| 105 | } |
|---|
| 106 | break; |
|---|
| 107 | case 'X': case 'x': // xpos : not supported |
|---|
| 108 | case 'Y': case 'y': // ypos : not supported |
|---|
| 109 | default: |
|---|
| 110 | ts.Add("#"); |
|---|
| 111 | break; |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | ts.Add(str); |
|---|
| 115 | delete[] copy_str; |
|---|
| 116 | return ts; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | TextStream::TextStream(void) { |
|---|
| 120 | kanji_type = euc; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 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 | } |
|---|
| 129 | |
|---|
| 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 | } |
|---|
| 138 | |
|---|
| 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 | } |
|---|
| 169 | |
|---|
| 170 | void TextStream::Clear(void) { |
|---|
| 171 | container.clear(); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 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 | } |
|---|
| 191 | |
|---|
| 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 | } |
|---|
| 198 | |
|---|
| 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) { |
|---|
| 226 | // è¡é ã®ååïŒ |
|---|
| 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()) { |
|---|
| 233 | // ååãèŠã€ãã£ãã®ã§åé€ |
|---|
| 234 | if (name) { // ä¿åå
ããããªãä¿åãã |
|---|
| 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; |
|---|
| 250 | name = 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 { |
|---|
| 344 | code = codeconv_euc_to_sjis(code); // save file ã®æŒ¢åã³ãŒãã¯SJIS |
|---|
| 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 | } |
|---|