diff 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
line wrap: on
line diff
--- a/font/text_stream.cc
+++ b/font/text_stream.cc
@@ -29,6 +29,8 @@
 #include "codeconv.h"
 #include <string.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include "system/system_config.h"
 
 /************************************************************************
 **
@@ -36,6 +38,84 @@
 **
 */
 
+static char* wstrchr(const char* s, unsigned int chr) {
+	int ws, wc;
+	while(*s != 0) {
+		if (*s < 0 && s[1] != 0) {
+			wc = int((unsigned char)(s[0]))*0x100 + int((unsigned char)(s[1]));
+			ws = 2;
+		} else {
+			wc = (unsigned char)(s[0]);
+			ws = 1;
+		}
+		if (wc == chr) return (char*)s;
+		s += ws;
+	}
+	return NULL;
+}
+
+TextStream TextStream::ParseMoji(const char* str, int def_r, int def_g, int def_b, int def_size) {
+	TextStream ts;
+	ts.kanji_type = TextStream::sjis;
+	ts.SetColor(def_r, def_g, def_b);
+	char* copy_str = new char[strlen(str)+1];
+	char* next_str;
+	char* retptr;
+	int var;
+
+	while( (next_str = wstrchr(str, '#')) != NULL) {
+		int len = next_str - str;
+		strncpy(copy_str, str, len);
+		copy_str[len] = 0;
+		ts.Add(copy_str);
+		str = next_str + 1;
+
+		switch(str[0]) {
+		case '#': // separator
+			str += 1;
+			break;
+		case 'D': case 'd': // return
+			ts.AddReturn();
+			str += 1;
+			break;
+		case 'C': case 'c': // color
+			str += 1;
+			var = strtol(str, &next_str,10);
+			if (var == 0 && str == next_str) { // no parameter
+				ts.SetColor(def_r, def_g, def_b);
+			} else {
+				int r,g,b; char key[1024];
+				sprintf(key, "#COLOR_TABLE.%03d", var);
+				if (AyuSysConfig::GetInstance()->GetParam(key, 3, &r, &g, &b)) { // color not found
+					r = g = b = 0;
+				}
+				ts.SetColor(r, g, b);
+				str = next_str;
+			}
+			break;
+		case 'S': case 's': // size
+			str += 1;
+			var = strtol(str, &next_str, 10);
+			if (var == 0 && str == next_str) { // no parameter
+				ts.SetSize(1);
+			} else {
+				if (def_size == 0) def_size = 20;
+				if (var <= 0) var = 1;
+				ts.SetSize(double(var)/def_size);
+			}
+			break;
+		case 'X': case 'x': // xpos : not supported
+		case 'Y': case 'y': // ypos : not supported
+		default:
+			ts.Add("#");
+			break;
+		}
+	}
+	ts.Add(str);
+	delete[] copy_str;
+	return ts;
+}
+
 TextStream::TextStream(void) {
 	kanji_type = euc;
 }
@@ -95,33 +175,6 @@ void TextStream::Add(const char* str) {
 	TextElem elem;
 	for (; *str; str++) {
 		if (*str >= 0x20) {
-			if (*str == '#') { // Commands
-				str++;
-				char command = (*str)|32;
-				str++;
-				const char *tmp = str;
-				while (isdigit(*str)) {
-					str++;
-				}
-				char *args = new char[str+1-tmp];
-				strncpy(args, tmp, str-tmp);
-				args[str-tmp] = 0;
-				// Different commands:
-				switch(command) {
-					case 'c':
-						/*TODO: int r, g, b; char key[17];
-						sprintf(key, "#COLOR_TABLE.%03d", args);
-						if (config.GetParam(key, 3, &r, &g, &b)) { // color not found
-							r = g = b = 0;
-						}
-						elem.type = TextElem::color;
-						elem.impl.Color.r = r;
-						elem.impl.Color.g = g;
-						elem.impl.Color.b = b;*/
-						break;
-				}
-				delete[] args;
-			}
 			elem.type = TextElem::glyph;
 			elem.impl.Glyph.code = *str;
 		} else if (*str < 0 && str[1] != 0) {