[PATCH 2/3] svgtiny: add alpha support and parse fill-opacity / stroke-opacity

Peter Korsgaard jacmet at sunsite.dk
Sat Sep 18 20:52:50 BST 2010


Signed-off-by: Peter Korsgaard <jacmet at sunsite.dk>
---
 include/svgtiny.h      |   11 +++++++----
 src/svgtiny.c          |   35 +++++++++++++++++++++++++++++++++++
 src/svgtiny_internal.h |    2 ++
 3 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/include/svgtiny.h b/include/svgtiny.h
index 9d685e9..e54ca98 100644
--- a/include/svgtiny.h
+++ b/include/svgtiny.h
@@ -10,18 +10,21 @@
 
 #include <libxml/parser.h>
 
-typedef int svgtiny_colour;
-#define svgtiny_TRANSPARENT 0x1000000
+typedef unsigned int svgtiny_colour;
+#define svgtiny_TRANSPARENT 0
 #ifdef __riscos__
-#define svgtiny_RGB(r, g, b) ((b) << 16 | (g) << 8 | (r))
+#define svgtiny_RGB(r, g, b) (0xff000000 | (b) << 16 | (g) << 8 | (r))
 #define svgtiny_RED(c) ((c) & 0xff)
 #define svgtiny_GREEN(c) (((c) >> 8) & 0xff)
 #define svgtiny_BLUE(c) (((c) >> 16) & 0xff)
+#define svgtiny_ALPHA(c) (((c) >> 24) & 0xff)
+
 #else
-#define svgtiny_RGB(r, g, b) ((r) << 16 | (g) << 8 | (b))
+#define svgtiny_RGB(r, g, b) (0xff000000 | (r) << 16 | (g) << 8 | (b))
 #define svgtiny_RED(c) (((c) >> 16) & 0xff)
 #define svgtiny_GREEN(c) (((c) >> 8) & 0xff)
 #define svgtiny_BLUE(c) ((c) & 0xff)
+#define svgtiny_ALPHA(c) (((c) >> 24) & 0xff)
 #endif
 
 struct svgtiny_shape {
diff --git a/src/svgtiny.c b/src/svgtiny.c
index 7a07b61..f8b214f 100644
--- a/src/svgtiny.c
+++ b/src/svgtiny.c
@@ -887,11 +887,15 @@ void svgtiny_parse_paint_attributes(const xmlNode *node,
 		const char *content = (const char *) attr->children->content;
 		if (strcmp(name, "fill") == 0)
 			svgtiny_parse_color(content, &state->fill, state);
+		else if (strcmp(name, "fill-opacity") == 0)
+			svgtiny_parse_opacity(content, &state->fill, state);
 		else if (strcmp(name, "stroke") == 0)
 			svgtiny_parse_color(content, &state->stroke, state);
 		else if (strcmp(name, "stroke-width") == 0)
 			state->stroke_width = svgtiny_parse_length(content,
 					state->viewport_width, *state);
+		else if (strcmp(name, "stroke-opacity") == 0)
+			svgtiny_parse_opacity(content, &state->stroke, state);
 		else if (strcmp(name, "style") == 0) {
 			const char *style = (const char *)
 					attr->children->content;
@@ -905,6 +909,14 @@ void svgtiny_parse_paint_attributes(const xmlNode *node,
 				svgtiny_parse_color(value, &state->fill, state);
 				free(value);
 			}
+			if ((s = strstr(style, "fill-opacity:"))) {
+				s += 13;
+				while (*s == ' ')
+					s++;
+				value = strndup(s, strcspn(s, "; "));
+				svgtiny_parse_opacity(value, &state->fill, state);
+				free(value);
+			}
 			if ((s = strstr(style, "stroke:"))) {
 				s += 7;
 				while (*s == ' ')
@@ -913,6 +925,14 @@ void svgtiny_parse_paint_attributes(const xmlNode *node,
 				svgtiny_parse_color(value, &state->stroke, state);
 				free(value);
 			}
+			if ((s = strstr(style, "stroke-opacity:"))) {
+				s += 15;
+				while (*s == ' ')
+					s++;
+				value = strndup(s, strcspn(s, "; "));
+				svgtiny_parse_opacity(value, &state->stroke, state);
+				free(value);
+			}
 			if ((s = strstr(style, "stroke-width:"))) {
 				s += 13;
 				while (*s == ' ')
@@ -989,6 +1009,21 @@ void svgtiny_parse_color(const char *s, svgtiny_colour *c,
 }
 
 
+void svgtiny_parse_opacity(const char *s, svgtiny_colour *c,
+		struct svgtiny_parse_state *state)
+{
+	float af;
+	unsigned int a;
+
+	UNUSED(state);
+
+	if (sscanf(s, "%f", &af) == 1) {
+		a = af * 255;
+		*c = (*c & 0x00ffffff) | (a << 24);
+	}
+}
+
+
 /**
  * Parse font attributes, if present.
  */
diff --git a/src/svgtiny_internal.h b/src/svgtiny_internal.h
index 3ea6e7b..92bd744 100644
--- a/src/svgtiny_internal.h
+++ b/src/svgtiny_internal.h
@@ -58,6 +58,8 @@ float svgtiny_parse_length(const char *s, int viewport_size,
 		const struct svgtiny_parse_state state);
 void svgtiny_parse_color(const char *s, svgtiny_colour *c,
 		struct svgtiny_parse_state *state);
+void svgtiny_parse_opacity(const char *s, svgtiny_colour *c,
+		struct svgtiny_parse_state *state);
 void svgtiny_parse_transform(char *s, float *ma, float *mb,
 		float *mc, float *md, float *me, float *mf);
 struct svgtiny_shape *svgtiny_add_shape(struct svgtiny_parse_state *state);
-- 
1.7.1




More information about the netsurf-dev mailing list