summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/modules/evas/vg_savers/svg/evas_vg_save_svg.c142
1 files changed, 132 insertions, 10 deletions
diff --git a/src/modules/evas/vg_savers/svg/evas_vg_save_svg.c b/src/modules/evas/vg_savers/svg/evas_vg_save_svg.c
index 8c11fb3d69..c44b0d536f 100644
--- a/src/modules/evas/vg_savers/svg/evas_vg_save_svg.c
+++ b/src/modules/evas/vg_savers/svg/evas_vg_save_svg.c
@@ -5,26 +5,148 @@ static int _evas_vg_saver_svg_log_dom = -1;
5#ifdef ERR 5#ifdef ERR
6# undef ERR 6# undef ERR
7#endif 7#endif
8#define ERR(...) EINA_LOG_DOM_ERR(_evas_vg_saver_eet_log_dom, __VA_ARGS__) 8#define ERR(...) EINA_LOG_DOM_ERR(_evas_vg_saver_svg_log_dom, __VA_ARGS__)
9 9
10#ifdef INF 10#ifdef INF
11# undef INF 11# undef INF
12#endif 12#endif
13#define INF(...) EINA_LOG_DOM_INFO(_evas_vg_saver_eet_log_dom, __VA_ARGS__) 13#define INF(...) EINA_LOG_DOM_INFO(_evas_vg_saver_svg_log_dom, __VA_ARGS__)
14
15static void
16printf_style(Svg_Style_Property *style, Eina_Strbuf *buf)
17{
18 if ((style->fill.paint.r) || (style->fill.paint.g) || (style->fill.paint.b))
19 eina_strbuf_append_printf(buf, " fill=\"#%02X%02X%02X\" ", style->fill.paint.r, style->fill.paint.g, style->fill.paint.b);
20 if (style->fill.fill_rule == EFL_GFX_FILL_RULE_ODD_EVEN)
21 eina_strbuf_append_printf(buf, " fill-rule=\"evenodd\" ");
22 if (style->fill.opacity != 255)
23 eina_strbuf_append_printf(buf, " fill-opacity=\"%f\"", style->fill.opacity / 255.0);
24 if ((style->stroke.paint.r) || (style->stroke.paint.g) || (style->stroke.paint.b))
25 eina_strbuf_append_printf(buf, " stroke=\"#%02X%02X%02X\" ", style->stroke.paint.r, style->stroke.paint.g, style->stroke.paint.b);
26 if (style->stroke.width)
27 eina_strbuf_append_printf(buf, " stroke-width=\"%f\" ", style->stroke.width);
28 if (style->stroke.cap == EFL_GFX_CAP_ROUND)
29 eina_strbuf_append_printf(buf, " stroke-linecap=\"round\" ");
30 else if (style->stroke.cap == EFL_GFX_CAP_SQUARE)
31 eina_strbuf_append_printf(buf, " stroke-linecap=\"square\" ");
32}
33
34static void
35_svg_node_printf(Svg_Node *parent, Eina_Strbuf *buf)
36{
37 int i = 0;
38 double *points;
39 double last_x = 0, last_y = 0;
40 Efl_Gfx_Path_Command *commands;
41 Eina_List *l;
42 Svg_Node *data;
43
44 switch (parent->type)
45 {
46 case SVG_NODE_DOC:
47 eina_strbuf_append_printf(buf, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
48 eina_strbuf_append_printf(buf, "<svg viewBox=\"%f %f %f %f\" xmlns=\"http://www.w3.org/2000/svg\""
49 " xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
50 parent->node.doc.vx, parent->node.doc.vy,
51 parent->node.doc.vw, parent->node.doc.vh);
52 EINA_LIST_FOREACH(parent->child, l, data)
53 {
54 _svg_node_printf(data, buf);
55 }
56 eina_strbuf_append_printf(buf, "</svg>\n");
57 break;
58 case SVG_NODE_G:
59 eina_strbuf_append_printf(buf, "<g");
60 if (parent->transform)
61 eina_strbuf_append_printf(buf,
62 " transform=\"matrix(%f %f %f %f %f %f)\"",
63 parent->transform->xx,
64 parent->transform->yx,
65 parent->transform->xy,
66 parent->transform->yy,
67 parent->transform->xz,
68 parent->transform->yz);
69 eina_strbuf_append_printf(buf, ">\n");
70 EINA_LIST_FOREACH(parent->child, l, data)
71 {
72 _svg_node_printf(data, buf);
73 }
74 eina_strbuf_append_printf(buf, "</g>\n");
75 break;
76 case SVG_NODE_CUSTOME_COMMAND:
77 points = parent->node.command.points;
78 commands = parent->node.command.commands;
79 eina_strbuf_append_printf(buf, "<path d=\"");
80 for (i = 0; i < parent->node.command.commands_count; i++)
81 {
82 switch (commands[i])
83 {
84 case EFL_GFX_PATH_COMMAND_TYPE_END:
85 eina_strbuf_append_printf(buf, "\"");
86 if (parent->transform)
87 eina_strbuf_append_printf(buf, " transform=\"matrix(%f %f %f %f %f %f)\"",
88 parent->transform->xx,
89 parent->transform->yx,
90 parent->transform->xy,
91 parent->transform->yy,
92 parent->transform->xz,
93 parent->transform->yz);
94 printf_style(parent->style, buf);
95 eina_strbuf_append_printf(buf, "/>\n");
96 break;
97 case EFL_GFX_PATH_COMMAND_TYPE_MOVE_TO:
98 eina_strbuf_append_printf(buf, "m%f,%f ", points[0] - last_x, points[1] - last_y);
99 last_x = points[0];
100 last_y = points[1];
101 points += 2;
102 break;
103 case EFL_GFX_PATH_COMMAND_TYPE_LINE_TO:
104 eina_strbuf_append_printf(buf, "L%f,%f ", points[0], points[1]);
105 last_x = points[0];
106 last_y = points[1];
107 points += 2;
108 break;
109 case EFL_GFX_PATH_COMMAND_TYPE_CUBIC_TO:
110 eina_strbuf_append_printf(buf, "c%f,%f %f,%f %f,%f ",
111 points[0] - last_x, points[1] - last_y,
112 points[2] - last_x, points[3] - last_y,
113 points[4] - last_x, points[5] - last_y);
114 last_x = points[4];
115 last_y = points[5];
116 points += 6;
117 break;
118 case EFL_GFX_PATH_COMMAND_TYPE_CLOSE:
119 eina_strbuf_append_printf(buf, " z ");
120 break;
121 case EFL_GFX_PATH_COMMAND_TYPE_LAST:
122 default:
123 break;
124 }
125 }
126 break;
127 default:
128 break;
129 }
130}
14 131
15int 132int
16evas_vg_save_file_svg(Vg_File_Data *evg_data, const char *file, const char *key EINA_UNUSED, int compress EINA_UNUSED) 133evas_vg_save_file_svg(Vg_File_Data *evg_data, const char *file, const char *key EINA_UNUSED, int compress EINA_UNUSED)
17{ 134{
18 Svg_Node *root EINA_UNUSED; 135 Eina_Strbuf *buf = NULL;
19 Eet_File *ef; 136 Svg_Node *root;
20 137 FILE *f = fopen(file, "w+");
21 ef = eet_open(file, EET_FILE_MODE_WRITE); 138 if (!f)
22 if (!ef) 139 {
23 return EVAS_LOAD_ERROR_GENERIC; 140 ERR("Cannot open file '%s' for SVG save", file);
141 return EFL_IMAGE_LOAD_ERROR_GENERIC;
142 }
24 143
25 root = vg_common_create_svg_node(evg_data); 144 root = vg_common_create_svg_node(evg_data);
26 //TODO: parse root into SVG source code and save into file as text file 145 buf = eina_strbuf_new();
27 eet_close(ef); 146 _svg_node_printf(root, buf);
147 fprintf(f, "%s\n", eina_strbuf_string_get(buf));
148 fclose(f);
149 eina_strbuf_free(buf);
28 150
29 return EVAS_LOAD_ERROR_NONE; 151 return EVAS_LOAD_ERROR_NONE;
30} 152}