All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] stkutil: convert text attributes to html
@ 2010-06-14 15:19 Kristen Carlson Accardi
  2010-06-14 23:10 ` andrzej zaborowski
  0 siblings, 1 reply; 4+ messages in thread
From: Kristen Carlson Accardi @ 2010-06-14 15:19 UTC (permalink / raw)
  To: ofono

[-- Attachment #1: Type: text/plain, Size: 7265 bytes --]

---
 src/stkutil.c |  203 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/stkutil.h |   21 ++++++
 2 files changed, 224 insertions(+), 0 deletions(-)

diff --git a/src/stkutil.c b/src/stkutil.c
index 8ac1dba..dbcb6ac 100644
--- a/src/stkutil.c
+++ b/src/stkutil.c
@@ -4307,3 +4307,206 @@ const unsigned char *stk_pdu_from_envelope(const struct stk_envelope *envelope,
 
 	return pdu;
 }
+
+static GString *mapColourToHtml(GString *string, guint8 colour)
+{
+	GString *result = string;
+	int fg = colour & 0x0f;
+	int bg = (colour >> 4) & 0x0f;
+	static const char *html_colors[] = {
+		"#000000", /* Black */
+		"#808080", /* Dark Grey */
+		"#C11B17", /* Dark Red */
+		"#FBB117", /* Dark Yellow */
+		"#347235", /* Dark Green */
+		"#307D7E", /* Dark Cyan */
+		"#0000A0", /* Dark Blue */
+		"#C031C7", /* Dark Magenta */
+		"#C0C0C0", /* Grey */
+		"#FFFFFF", /* White */
+		"#FF0000", /* Bright Red */
+		"#FFFF00", /* Bright Yellow */
+		"#00FF00", /* Bright Green */
+		"#00FFFF", /* Bright Cyan */
+		"#0000FF", /* Bright Blue */
+		"#FF00FF", /* Bright Magenta */
+	};
+
+	if (colour == 0)
+		return string;
+
+	if (fg) {
+		result = g_string_append(result, "color: ");
+		result = g_string_append(result, html_colors[fg]);
+		result = g_string_append_c(result, ';');
+	}
+	if (bg) {
+		result = g_string_append(result, "background-color: ");
+		result = g_string_append(result, html_colors[bg]);
+		result = g_string_append_c(result, ';');
+	}
+
+
+	return result;
+}
+
+static GString *endFormat(GString *string)
+{
+	return g_string_append(string, "</p>");
+}
+
+static GString *mapFormatToHtml(GString *string, guint8 code, guint8 colour)
+{
+	GString *result = string;
+	guint8 align = code & STK_TEXT_FORMAT_ALIGN_MASK;
+	guint8 font = code & STK_TEXT_FORMAT_FONT_MASK;
+	guint8 style = code & STK_TEXT_FORMAT_STYLE_MASK;
+
+	/* formatting applies to a block of test */
+	result = g_string_append(result, "<p style=\"");
+
+	if (align == STK_TEXT_FORMAT_RIGHT_ALIGN)
+		result = g_string_append(result, "text-align: right;");
+	else if (align == STK_TEXT_FORMAT_CENTER_ALIGN)
+		result = g_string_append(result, "text-align: center;");
+	else if (align == STK_TEXT_FORMAT_LEFT_ALIGN)
+		result = g_string_append(result, "text-align: left;");
+
+	if (font == STK_TEXT_FORMAT_FONT_SIZE_LARGE)
+		result = g_string_append(result, "font-size: big;");
+	else if (font == STK_TEXT_FORMAT_FONT_SIZE_SMALL)
+		result = g_string_append(result, "font-size: small;");
+
+	if (style == STK_TEXT_FORMAT_STYLE_BOLD)
+		result = g_string_append(result, "font-weight: bold;");
+	else if (style == STK_TEXT_FORMAT_STYLE_ITALIC)
+		result = g_string_append(result, "font-style: italic;");
+	else if (style == STK_TEXT_FORMAT_STYLE_UNDERLINED)
+		result = g_string_append(result, "text-decoration: underline;");
+	else if (style == STK_TEXT_FORMAT_STYLE_STRIKETHROUGH)
+		result = g_string_append(result,
+					"text-decoration: line-through;");
+
+	/* add any color */
+	result = mapColourToHtml(result, colour);
+
+	result = g_string_append(result, "\">");
+
+	return result;
+}
+
+static gboolean isSpecialChar(char c)
+{
+	return (c == '\n' || c == '\r' || c == '<' || c == '>' || c == '&');
+}
+
+static gboolean mapCharToHtml(char *s, int pos, int len,
+			GString **string)
+{
+	gboolean rval = FALSE;
+	GString *result = *string;
+	unsigned char c = s[pos];
+
+	switch (c) {
+		case '\n':
+			result = g_string_append(result, "<br>");
+			break;
+		case '\r':
+			result = g_string_append(result, "<br>");
+			if ((pos + 1 < len) && (s[pos + 1] == '\n'))
+				rval = TRUE;
+			break;
+		case '<':
+			result = g_string_append(result, "&lt;");
+			break;
+		case '>':
+			result = g_string_append(result, "&gt;");
+			break;
+		case '&':
+			result = g_string_append(result, "&amp;");
+			break;
+	}
+	*string = result;
+	return rval;
+}
+
+static GString *copyText(GString *string, char *text,
+				int *start_pos, int start, int end)
+{
+	GString *result = string;
+	int pos = *start_pos;
+
+	while (pos < start && pos < end) {
+		if (isSpecialChar(text[pos])) {
+			if (mapCharToHtml(text, pos, end, &result) == TRUE)
+				pos++;
+		}
+		else
+			result = g_string_append_c(result, text[pos]);
+		pos++;
+	}
+	*start_pos = pos;
+	return result;
+}
+
+static int extractFormat(const unsigned char *attrs, int index, int attrs_len,
+				int text_len, guint8 *start,
+				guint8 *end, guint8 *code, guint8 *colour)
+{
+	int i = index;
+
+	/* If there are no more attributes, initialize to default values */
+	if (i >= attrs_len) {
+		*start = text_len;
+		*end = text_len;
+		*code = 0;
+		*colour = 0;
+		return i;
+	}
+
+	*start = attrs[i++];
+	*end = attrs[i++];
+	*code = attrs[i++];
+
+	if (i < attrs_len)
+		*colour = attrs[i++];
+	else
+		*colour = 0;
+
+	if (*end == 0)
+		*end = text_len;
+
+	return i;
+}
+
+char *textToHtml(char *text, int text_len,
+				const unsigned char *attrs, int attrs_len)
+{
+	GString *result = g_string_new(NULL);
+	guint8 start, end, code, colour;
+	int pos = 0;
+	int i = 0;
+
+	while (pos < text_len) {
+		/* check for an attribute */
+		i = extractFormat(attrs, i, attrs_len, text_len, &start,
+					&end, &code, &colour);
+
+		/* insert any non-formatted text */
+		result = copyText(result, text, &pos, start, text_len);
+		if (pos >= text_len)
+			break;
+
+		/* start formatting */
+		result = mapFormatToHtml(result, code, colour);
+
+		/* insert formatted text */
+		result = copyText(result, text, &pos, end, text_len);
+
+		/* end formatting */
+		result = endFormat(result);
+	}
+
+	/* return characters from result. Caller must free char data */
+	return g_string_free(result, FALSE);
+}
diff --git a/src/stkutil.h b/src/stkutil.h
index 2da787d..2c78382 100644
--- a/src/stkutil.h
+++ b/src/stkutil.h
@@ -447,6 +447,25 @@ enum stk_broadcast_network_technology {
 	STK_BROADCAST_NETWORK_T_DMB = 0x03
 };
 
+#define STK_TEXT_FORMAT_ALIGN_MASK 0x03
+#define STK_TEXT_FORMAT_FONT_MASK 0x0C
+#define STK_TEXT_FORMAT_STYLE_MASK 0xF0
+
+/* Defined in ETSI 123 40 9.2.3.24.10.1.1 */
+enum stk_text_format_code {
+	STK_TEXT_FORMAT_LEFT_ALIGN = 0x00,
+	STK_TEXT_FORMAT_CENTER_ALIGN = 0x01,
+	STK_TEXT_FORMAT_RIGHT_ALIGN = 0x02,
+	STK_TEXT_FORMAT_NO_ALIGN = 0x03,
+	STK_TEXT_FORMAT_FONT_SIZE_LARGE = 0x04,
+	STK_TEXT_FORMAT_FONT_SIZE_SMALL = 0x08,
+	STK_TEXT_FORMAT_FONT_SIZE_RESERVED = 0x0c,
+	STK_TEXT_FORMAT_STYLE_BOLD = 0x10,
+	STK_TEXT_FORMAT_STYLE_ITALIC = 0x20,
+	STK_TEXT_FORMAT_STYLE_UNDERLINED = 0x40,
+	STK_TEXT_FORMAT_STYLE_STRIKETHROUGH = 0x80,
+};
+
 /* For data object that only has a byte array with undetermined length */
 struct stk_common_byte_array {
 	unsigned char *array;
@@ -1213,3 +1232,5 @@ const unsigned char *stk_pdu_from_response(const struct stk_response *response,
 						unsigned int *out_length);
 const unsigned char *stk_pdu_from_envelope(const struct stk_envelope *envelope,
 						unsigned int *out_length);
+char *textToHtml(char *text, int text_len,
+				const unsigned char *attrs, int attrs_len);
-- 
1.6.6.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-06-16  6:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-06-14 15:19 [PATCH] stkutil: convert text attributes to html Kristen Carlson Accardi
2010-06-14 23:10 ` andrzej zaborowski
2010-06-15 23:32   ` Kristen Carlson Accardi
2010-06-16  6:27     ` Marcel Holtmann

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.