dia r4187 - in trunk: . plug-ins/svg



Author: hans
Date: Sun Jan 11 15:29:06 2009
New Revision: 4187
URL: http://svn.gnome.org/viewvc/dia?rev=4187&view=rev

Log:
2009-01-11  Hans Breuer  <hans breuer org>

	* plug-ins/svg/svg-import.c(read_text_svg) : the text style was not
	properly intialize leading e.g. to random text colors.
	Also it now supports multi-line text elements "<tspan/>". Both can
	be booked under bug #108502 - but that bug will never get fixed;)

	* plug-ins/render_svg.c(draw_text) : implementation supporting 
	multi-line text via <tspan/>



Modified:
   trunk/ChangeLog
   trunk/plug-ins/svg/render_svg.c
   trunk/plug-ins/svg/svg-import.c

Modified: trunk/plug-ins/svg/render_svg.c
==============================================================================
--- trunk/plug-ins/svg/render_svg.c	(original)
+++ trunk/plug-ins/svg/render_svg.c	Sun Jan 11 15:29:06 2009
@@ -90,6 +90,7 @@
 			       Color *colour);
 static void draw_text_line    (DiaRenderer *self, TextLine *text_line,
 	                       Point *pos, Alignment alignment, Color *colour);
+static void draw_text         (DiaRenderer *self, Text *text);
 
 static void svg_renderer_class_init (SvgRendererClass *klass);
 
@@ -175,6 +176,7 @@
   renderer_class->draw_rounded_rect = draw_rounded_rect;
   renderer_class->fill_rounded_rect = fill_rounded_rect;
   renderer_class->draw_string  = draw_string;
+  renderer_class->draw_text  = draw_text;
   renderer_class->draw_text_line  = draw_text_line;
 }
 
@@ -378,6 +380,7 @@
    */
   real font_size = dia_font_get_size (font) * (font_height / dia_font_get_height (font));
   /* ... but at least Inkscape and Firefox would produce the wrong font-size */
+  const gchar *family = dia_font_get_family(font);
 
   saved_width = renderer->linewidth;
   renderer->linewidth = 0.001;
@@ -389,24 +392,24 @@
    */
   switch (alignment) {
   case ALIGN_LEFT:
-    style = g_strconcat(style, "; text-anchor:start", NULL);
+    style = g_strconcat(style, ";text-anchor:start", NULL);
     break;
   case ALIGN_CENTER:
-    style = g_strconcat(style, "; text-anchor:middle", NULL);
+    style = g_strconcat(style, ";text-anchor:middle", NULL);
     break;
   case ALIGN_RIGHT:
-    style = g_strconcat(style, "; text-anchor:end", NULL);
+    style = g_strconcat(style, ";text-anchor:end", NULL);
     break;
   }
-  tmp = g_strdup_printf("%s; font-size: %s", style,
+  tmp = g_strdup_printf("%s;font-size:%s", style,
 			dia_svg_dtostr(d_buf, font_size) );
   g_free (style);
   style = tmp;
 
   if (font) {
-     tmp = g_strdup_printf("%s; font-family: %s; font-style: %s; "
-                           "font-weight: %s",style,
-                           dia_font_get_family(font),
+     tmp = g_strdup_printf("%s;font-family:%s;font-style:%s;"
+                           "font-weight:%s",style,
+                           strcmp(family, "sans") == 0 ? "sanserif" : family,
                            dia_font_get_slant_string(font),
                            dia_font_get_weight_string(font));
      g_free(style);
@@ -462,6 +465,39 @@
   dia_svg_dtostr(d_buf, text_line_get_width(text_line));
   xmlSetProp(node, (const xmlChar*)"textLength", (xmlChar *) d_buf);
 }
+
+static void
+draw_text (DiaRenderer *self, Text *text)
+{
+  DiaSvgRenderer *renderer = DIA_SVG_RENDERER (self);
+  Point pos = text->position;
+  int i;
+  xmlNodePtr node_text, node_tspan;
+  gchar d_buf[G_ASCII_DTOSTR_BUF_SIZE];
+
+  node_text = xmlNewChild(renderer->root, renderer->svg_name_space, (const xmlChar *)"text", NULL);
+  /* text 'global' properties  */
+  node_set_text_style(node_text, renderer, text->font, text->height, text->alignment, &text->color);
+  dia_svg_dtostr(d_buf, pos.x);
+  xmlSetProp(node_text, (const xmlChar *)"x", (xmlChar *) d_buf);
+  dia_svg_dtostr(d_buf, pos.y);
+  xmlSetProp(node_text, (const xmlChar *)"y", (xmlChar *) d_buf);
+  
+  pos = text->position;
+  for (i=0;i<text->numlines;i++) {
+    TextLine *text_line = text->lines[i];
+
+    node_tspan = xmlNewChild(node_text, renderer->svg_name_space, (const xmlChar *)"tspan",
+                             text_line_get_string(text_line));
+    dia_svg_dtostr(d_buf, pos.x);
+    xmlSetProp(node_tspan, (const xmlChar *)"x", (xmlChar *) d_buf);
+    dia_svg_dtostr(d_buf, pos.y);
+    xmlSetProp(node_tspan, (const xmlChar *)"y", (xmlChar *) d_buf);
+    
+    pos.y += text->height;
+  }
+}
+
 static void
 export_svg(DiagramData *data, const gchar *filename, 
            const gchar *diafilename, void* user_data)

Modified: trunk/plug-ins/svg/svg-import.c
==============================================================================
--- trunk/plug-ins/svg/svg-import.c	(original)
+++ trunk/plug-ins/svg/svg-import.c	Sun Jan 11 15:29:06 2009
@@ -48,6 +48,7 @@
 #include "create.h"
 #include "group.h"
 #include "font.h"
+#include "attributes.h"
 
 gboolean import_svg(const gchar *filename, DiagramData *dia, void* user_data);
 static GList *read_ellipse_svg(xmlNodePtr node, DiaSvgStyle *parent_style, GList *list);
@@ -185,7 +186,7 @@
       if(gs->stroke != (-1)) {
         cprop->color_data = get_colour(gs->stroke);
       } else {
-	if(gs->fill == (-1)) {
+	if(gs->fill == DIA_SVG_COLOUR_NONE) {
 	  cprop->color_data = get_colour(0x000000);
 	} else {
 	  cprop->color_data = get_colour(gs->fill);
@@ -195,7 +196,7 @@
       rprop->real_data = gs->line_width;
   
       lsprop = g_ptr_array_index(props,2);
-      lsprop->style = gs->linestyle;
+      lsprop->style = gs->linestyle != DIA_SVG_LINESTYLE_DEFAULT ? gs->linestyle : LINESTYLE_SOLID;
       lsprop->dash = gs->dashlength;
 
       cprop = g_ptr_array_index(props,3);
@@ -226,7 +227,7 @@
     gchar *str, *pathdata, *unparsed = NULL;
     GArray *bezpoints = NULL;
     gboolean closed = FALSE;
-    guint i;
+    gint i;
     
     pathdata = str = (char *) xmlGetProp(node, (const xmlChar *)"d");
     do {
@@ -287,13 +288,12 @@
     Point point;
     GPtrArray *props;
     TextProperty *prop;
-    xmlChar *str;
+    xmlChar *str = NULL;
+    gchar *multiline = NULL;
     DiaSvgStyle *gs;
 
     gs = g_new(DiaSvgStyle, 1);
-    gs->font = NULL;
-    gs->font_height = 1.0;
-    gs->alignment = ALIGN_LEFT;
+    dia_svg_style_init (gs, parent_style);
 
     point.x = 0;
     point.y = 0;
@@ -310,8 +310,28 @@
       xmlFree(str);
     }
 
-    str = xmlNodeGetContent(node);
-    if(str) {
+    if (node->children && xmlStrcmp (node->children->name, "tspan") == 0) {
+      xmlNode *tspan = node->children;
+      GString *paragraph = g_string_sized_new(512);
+
+      do {
+        xmlChar *line = xmlNodeGetContent(tspan);
+	if (line) {
+	  g_string_append(paragraph, line);
+	  if (tspan->next && xmlStrcmp (tspan->next->name, "tspan") == 0)
+	    g_string_append(paragraph, "\n");
+	  xmlFree(line);
+	}
+	tspan = tspan->next;
+      } while (tspan);
+
+      multiline = paragraph->str;
+      g_string_free (paragraph, FALSE);
+      str = NULL;
+    } else {
+      str = xmlNodeGetContent(node);
+    }
+    if(str || multiline) {
       new_obj = otype->ops->create(&point, otype->default_user_data,
 				 &h1, &h2);
       list = g_list_append (list, new_obj);
@@ -325,14 +345,29 @@
       }
       prop = g_ptr_array_index(props, 0);
       g_free(prop->text_data);
-      prop->text_data = g_strdup((char *) str);
+      prop->text_data = str ? g_strdup((char *) str) : multiline;
       xmlFree(str);
       prop->attr.alignment = gs->alignment;
       prop->attr.position.x = point.x;
       prop->attr.position.y = point.y;
-      prop->attr.font = gs->font;
+      /* FIXME: looks like a leak but without this an imported svg is 
+       * crashing on release */
+      prop->attr.font = dia_font_ref (gs->font);
       prop->attr.height = gs->font_height;
-      prop->attr.color = get_colour (gs->fill);
+      /* when operating with default values foreground and background are intentionally swapped
+       * to avoid getting white text by default */
+      switch (gs->fill) {
+      case DIA_SVG_COLOUR_TEXT :
+      case DIA_SVG_COLOUR_FOREGROUND :
+        prop->attr.color = attributes_get_background();
+	break;
+      case DIA_SVG_COLOUR_BACKGROUND :
+        prop->attr.color = attributes_get_foreground();
+	break;
+      default :
+        prop->attr.color = get_colour (gs->fill);
+	break;
+      }
       new_obj->ops->set_props(new_obj, props);
       prop_list_free(props);
     }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]