[dia] [transform] SVG matrix-to-string and transform-to-matrix



commit 7073f7f4eb5aa767bfd91dd6ed0c5e59cc86df16
Author: Hans Breuer <hans breuer org>
Date:   Sun Sep 12 14:34:34 2010 +0200

    [transform] SVG matrix-to-string and transform-to-matrix
    
    Helper functions for string<->matrix transform, not only
    useful for plain SVG.

 lib/dia_svg.c  |   82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/dia_svg.h  |    4 ++-
 lib/libdia.def |    2 +
 3 files changed, 87 insertions(+), 1 deletions(-)
---
diff --git a/lib/dia_svg.c b/lib/dia_svg.c
index 96c95d5..fd50e57 100644
--- a/lib/dia_svg.c
+++ b/lib/dia_svg.c
@@ -890,3 +890,85 @@ MORETOPARSE:
   }
   return points;
 }
+
+DiaMatrix *
+dia_svg_parse_transform(const gchar *trans, real scale)
+{
+  DiaMatrix *m = g_new0 (DiaMatrix, 1);
+  gchar *p = strchr (trans, '(');
+  gchar **list = g_strsplit (p+1, ",", -1);
+  int i = 0;
+
+  if (strncmp (trans, "matrix", 6) == 0) {
+    if (list[i])
+      m->xx = g_ascii_strtod (list[i], NULL), ++i;
+    if (list[i])
+      m->yx = g_ascii_strtod (list[i], NULL), ++i;
+    if (list[i])
+      m->xy = g_ascii_strtod (list[i], NULL), ++i;
+    if (list[i])
+      m->yy = g_ascii_strtod (list[i], NULL), ++i;
+    if (list[i])
+      m->x0 = g_ascii_strtod (list[i], NULL), ++i;
+    if (list[i])
+      m->y0 = g_ascii_strtod (list[i], NULL), ++i;
+  } else if (strncmp (trans, "translate", 9) == 0) {
+    m->xx = m->yy = 1.0;
+    if (list[i])
+      m->x0 = g_ascii_strtod (list[i], NULL), ++i;
+    if (list[i])
+      m->y0 = g_ascii_strtod (list[i], NULL), ++i;
+  } else if (strncmp (trans, "scale", 5) == 0) {
+    if (list[i])
+      m->xx = g_ascii_strtod (list[i], NULL), ++i;
+    if (list[i])
+      m->yy = g_ascii_strtod (list[i], NULL), ++i;
+    else
+      m->yy = m->xx;
+  } else if (strncmp (trans, "rotate", 6) == 0) {
+    real angle;
+    
+    if (list[i])
+      angle = g_ascii_strtod (list[i], NULL);
+    m->xx =  cos(G_PI*angle/180);
+    m->xy =  sin(G_PI*angle/180);
+    m->yx = -sin(G_PI*angle/180);
+    m->yy =  cos(G_PI*angle/180);
+  } else {
+    g_warning ("%s?", trans);
+    g_free (m);
+    m = NULL;
+  }
+  if (scale > 0 && m) {
+    m->x0 /= scale;
+    m->y0 /= scale;
+  }
+  g_strfreev(list);
+  return m;
+}
+
+gchar *
+dia_svg_from_matrix(const DiaMatrix *matrix, real scale)
+{
+  /*  transform="matrix(1,0,0,1,0,0)" */
+  gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
+  GString *sm = g_string_new ("matrix(");
+  gchar *s;
+
+  g_ascii_formatd (buf, sizeof(buf), "%g", matrix->xx);
+  g_string_append (sm, buf); g_string_append (sm, ",");
+  g_ascii_formatd (buf, sizeof(buf), "%g", matrix->yx);
+  g_string_append (sm, buf); g_string_append (sm, ",");
+  g_ascii_formatd (buf, sizeof(buf), "%g", matrix->xy);
+  g_string_append (sm, buf); g_string_append (sm, ",");
+  g_ascii_formatd (buf, sizeof(buf), "%g", matrix->yy);
+  g_string_append (sm, buf); g_string_append (sm, ",");
+  g_ascii_formatd (buf, sizeof(buf), "%g", matrix->x0 * scale);
+  g_string_append (sm, buf); g_string_append (sm, ",");
+  g_ascii_formatd (buf, sizeof(buf), "%g", matrix->y0 * scale);
+  g_string_append (sm, buf); g_string_append (sm, ")");
+  
+  s = sm->str;
+  g_string_free (sm, FALSE);
+  return s;
+}
diff --git a/lib/dia_svg.h b/lib/dia_svg.h
index dd127dd..71193a2 100644
--- a/lib/dia_svg.h
+++ b/lib/dia_svg.h
@@ -59,6 +59,8 @@ void dia_svg_style_init (DiaSvgStyle *gs, DiaSvgStyle *parent_style);
 void dia_svg_style_copy (DiaSvgStyle *dest, DiaSvgStyle *src);
 void dia_svg_parse_style(xmlNodePtr node, DiaSvgStyle *s, real user_scale);
 /* parse the svg sub format for pathes int an array of BezPoint */
-GArray* dia_svg_parse_path(const gchar *path_str, gchar **unparsed, gboolean *closed);
+GArray *dia_svg_parse_path(const gchar *path_str, gchar **unparsed, gboolean *closed);
+DiaMatrix *dia_svg_parse_transform(const gchar *trans, real scale);
+gchar *dia_svg_from_matrix(const DiaMatrix *matrix, real scale);
 
 #endif /* DIA_SVG_H */
diff --git a/lib/libdia.def b/lib/libdia.def
index 2f49d61..43ac06d 100644
--- a/lib/libdia.def
+++ b/lib/libdia.def
@@ -341,6 +341,8 @@ EXPORTS
  dia_svg_style_copy
  dia_svg_parse_style
  dia_svg_parse_path
+ dia_svg_parse_transform
+ dia_svg_from_matrix
 
  dia_svg_renderer_get_type
 



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