2 * libsortnetwork - src/sn-svg.c
3 * Copyright (C) 2008-2010 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <ff at octo.it>
29 #include "sn_network.h"
31 #define INNER_SPACING 15.0
32 #define OUTER_SPACING 40.0
38 static double x_offset = OUTER_SPACING;
39 static _Bool embedded = 0;
41 static void exit_usage (void) /* {{{ */
43 printf ("Usage: sn-svg [options] [file]\n"
45 "Valid options are:\n"
46 " -e Create output suitable for embedding into other XML files.\n"
49 } /* }}} void exit_usage */
51 static int read_options (int argc, char **argv) /* {{{ */
55 while ((option = getopt (argc, argv, "eh?")) != -1)
71 } /* }}} int read_options */
73 static double determine_stage_width (sn_stage_t *s)
75 int lines[s->comparators_num];
76 int right[s->comparators_num];
80 if (SN_STAGE_COMP_NUM (s) == 0)
83 for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
89 for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
92 sn_comparator_t *c = SN_STAGE_COMP_GET (s, i);
94 for (j = 0; j < lines_used; j++)
95 if (SN_COMP_LEFT (c) > right[j])
99 right[j] = SN_COMP_RIGHT (c);
103 assert (lines_used >= 1);
105 return (((double) (lines_used - 1)) * INNER_SPACING);
108 static double determine_network_width (sn_network_t *n) /* {{{ */
113 /* Spacing between stages and at the beginning and end of the network */
114 width = (SN_NETWORK_STAGE_NUM (n) + 1) * OUTER_SPACING;
116 /* Spacing required within a stage */
117 for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
118 width += determine_stage_width (SN_NETWORK_STAGE_GET (n, i));
121 } /* }}} double determine_network_width */
123 static int sn_svg_show_stage (sn_stage_t *s) /* {{{ */
125 int lines[s->comparators_num];
126 int right[s->comparators_num];
130 printf (" <!-- stage %i -->\n", SN_STAGE_DEPTH (s));
132 for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
138 for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
141 sn_comparator_t *c = SN_STAGE_COMP_GET (s, i);
143 for (j = 0; j < lines_used; j++)
144 if (SN_COMP_LEFT (c) > right[j])
148 right[j] = SN_COMP_RIGHT (c);
154 double x1 = x_offset + (j * INNER_SPACING);
156 int y1 = Y_OFFSET + (SN_COMP_MIN (c) * Y_SPACING);
157 int y2 = Y_OFFSET + (SN_COMP_MAX (c) * Y_SPACING);
159 printf (" <svg:line x1=\"%g\" y1=\"%i\" x2=\"%g\" y2=\"%i\" "
160 "stroke=\"black\" stroke-width=\"1\" />\n"
161 " <svg:circle cx=\"%g\" cy=\"%i\" r=\"%g\" fill=\"black\" />\n"
162 " <svg:circle cx=\"%g\" cy=\"%i\" r=\"%g\" fill=\"black\" />\n",
169 x_offset = x_offset + ((lines_used - 1) * INNER_SPACING) + OUTER_SPACING;
174 } /* }}} int sn_svg_show_stage */
176 int main (int argc, char **argv) /* {{{ */
186 read_options (argc, argv);
188 if ((argc - optind) == 0)
190 else if ((argc - optind) == 1)
191 fh = fopen (argv[optind], "r");
196 n = sn_network_read (fh);
200 printf ("n == NULL!\n");
204 svg_height = (2 * Y_OFFSET) + ((SN_NETWORK_INPUT_NUM (n) - 1) * Y_SPACING);
205 svg_width = determine_network_width (n);
208 printf ("<svg:svg version=\"1.1\" viewBox=\"0 0 %g %g\">\n",
209 svg_width, svg_height);
211 printf ("<?xml version=\"1.0\" standalone=\"no\"?>\n"
212 "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" "
213 "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"
214 "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" "
215 "width=\"%gpt\" height=\"%gpt\" viewBox=\"0 0 %g %g\">\n",
216 svg_width, svg_height, svg_width, svg_height);
218 printf ("<!-- Output generated with sn-svg from %s -->\n", PACKAGE_STRING);
220 for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
221 sn_svg_show_stage (SN_NETWORK_STAGE_GET (n, i));
223 printf (" <!-- horizontal lines -->\n");
224 for (i = 0; i < SN_NETWORK_INPUT_NUM (n); i++)
225 printf (" <svg:line x1=\"%g\" y1=\"%i\" x2=\"%g\" y2=\"%i\" "
226 "stroke=\"black\" stroke-width=\"1\" />\n",
227 0.0, Y_OFFSET + (i * Y_SPACING),
228 x_offset, Y_OFFSET + (i * Y_SPACING));
230 printf ("</svg:svg>\n");
235 /* vim: set shiftwidth=2 softtabstop=2 fdm=marker : */