comparison font/text_stream.cc @ 53:ddbcbd000206

* MuSys, AyuSysConfig, FileSearcher (former FILESEARCHER) and KeyHolder (former KEYHOLDER) are now singletons * ParseMoji moved to TextStream * Some cleaning (0 -> NULL when needed, removal of useless returns, ...)
author thib
date Sun, 19 Apr 2009 11:44:05 +0000
parents 15a18fbe6f21
children 4416cfac86ae
comparison
equal deleted inserted replaced
52:15a18fbe6f21 53:ddbcbd000206
27 27
28 #include "text.h" 28 #include "text.h"
29 #include "codeconv.h" 29 #include "codeconv.h"
30 #include <string.h> 30 #include <string.h>
31 #include <stdio.h> 31 #include <stdio.h>
32 #include <stdlib.h>
33 #include "system/system_config.h"
32 34
33 /************************************************************************ 35 /************************************************************************
34 ** 36 **
35 ** TextStream 37 ** TextStream
36 ** 38 **
37 */ 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 }
38 118
39 TextStream::TextStream(void) { 119 TextStream::TextStream(void) {
40 kanji_type = euc; 120 kanji_type = euc;
41 } 121 }
42 122
93 173
94 void TextStream::Add(const char* str) { 174 void TextStream::Add(const char* str) {
95 TextElem elem; 175 TextElem elem;
96 for (; *str; str++) { 176 for (; *str; str++) {
97 if (*str >= 0x20) { 177 if (*str >= 0x20) {
98 if (*str == '#') { // Commands
99 str++;
100 char command = (*str)|32;
101 str++;
102 const char *tmp = str;
103 while (isdigit(*str)) {
104 str++;
105 }
106 char *args = new char[str+1-tmp];
107 strncpy(args, tmp, str-tmp);
108 args[str-tmp] = 0;
109 // Different commands:
110 switch(command) {
111 case 'c':
112 /*TODO: int r, g, b; char key[17];
113 sprintf(key, "#COLOR_TABLE.%03d", args);
114 if (config.GetParam(key, 3, &r, &g, &b)) { // color not found
115 r = g = b = 0;
116 }
117 elem.type = TextElem::color;
118 elem.impl.Color.r = r;
119 elem.impl.Color.g = g;
120 elem.impl.Color.b = b;*/
121 break;
122 }
123 delete[] args;
124 }
125 elem.type = TextElem::glyph; 178 elem.type = TextElem::glyph;
126 elem.impl.Glyph.code = *str; 179 elem.impl.Glyph.code = *str;
127 } else if (*str < 0 && str[1] != 0) { 180 } else if (*str < 0 && str[1] != 0) {
128 elem.type = TextElem::glyph; 181 elem.type = TextElem::glyph;
129 elem.impl.Glyph.code = ((int(*(unsigned char*)str))<<8) | int(*(unsigned char*)(str+1)); 182 elem.impl.Glyph.code = ((int(*(unsigned char*)str))<<8) | int(*(unsigned char*)(str+1));