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>
24 #ifndef _ISOC99_SOURCE
25 # define _ISOC99_SOURCE
27 #ifndef _POSIX_C_SOURCE
28 # define _POSIX_C_SOURCE 200112L
36 #include "sn_network.h"
38 #define INNER_SPACING 15.0
39 #define OUTER_SPACING 40.0
45 static double x_offset = OUTER_SPACING;
46 static _Bool embedded = 0;
48 static void exit_usage (void) /* {{{ */
50 printf ("Usage: sn-svg [options] [file]\n"
52 "Valid options are:\n"
53 " -e Create output suitable for embedding into other XML files.\n"
56 } /* }}} void exit_usage */
58 static int read_options (int argc, char **argv) /* {{{ */
62 while ((option = getopt (argc, argv, "eh?")) != -1)
78 } /* }}} int read_options */
80 static double determine_stage_width (sn_stage_t *s) /* {{{ */
82 int lines[s->comparators_num];
83 int right[s->comparators_num];
87 if (SN_STAGE_COMP_NUM (s) == 0)
90 for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
96 for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
99 sn_comparator_t *c = SN_STAGE_COMP_GET (s, i);
101 for (j = 0; j < lines_used; j++)
102 if (SN_COMP_LEFT (c) > right[j])
106 right[j] = SN_COMP_RIGHT (c);
110 assert (lines_used >= 1);
112 return (((double) (lines_used - 1)) * INNER_SPACING);
113 } /* }}} double determine_stage_width */
115 static double determine_network_width (sn_network_t *n) /* {{{ */
120 /* Spacing between stages and at the beginning and end of the network */
121 width = (SN_NETWORK_STAGE_NUM (n) + 1) * OUTER_SPACING;
123 /* Spacing required within a stage */
124 for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
125 width += determine_stage_width (SN_NETWORK_STAGE_GET (n, i));
128 } /* }}} double determine_network_width */
130 static int sn_svg_show_stage (sn_stage_t *s) /* {{{ */
132 int lines[s->comparators_num];
133 int right[s->comparators_num];
137 printf (" <!-- stage %i -->\n", SN_STAGE_DEPTH (s));
139 for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
145 for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
148 sn_comparator_t *c = SN_STAGE_COMP_GET (s, i);
150 for (j = 0; j < lines_used; j++)
151 if (SN_COMP_LEFT (c) > right[j])
155 right[j] = SN_COMP_RIGHT (c);
161 double x1 = x_offset + (j * INNER_SPACING);
163 int y1 = Y_OFFSET + (SN_COMP_MIN (c) * Y_SPACING);
164 int y2 = Y_OFFSET + (SN_COMP_MAX (c) * Y_SPACING);
166 printf (" <svg:line x1=\"%g\" y1=\"%i\" x2=\"%g\" y2=\"%i\" "
167 "stroke=\"black\" stroke-width=\"1\" />\n"
168 " <svg:circle cx=\"%g\" cy=\"%i\" r=\"%g\" fill=\"black\" />\n"
169 " <svg:circle cx=\"%g\" cy=\"%i\" r=\"%g\" fill=\"black\" />\n",
176 x_offset = x_offset + ((lines_used - 1) * INNER_SPACING) + OUTER_SPACING;
181 } /* }}} int sn_svg_show_stage */
183 int main (int argc, char **argv) /* {{{ */
193 read_options (argc, argv);
195 if ((argc - optind) == 0)
197 else if ((argc - optind) == 1)
198 fh = fopen (argv[optind], "r");
203 n = sn_network_read (fh);
207 printf ("n == NULL!\n");
211 svg_height = (2 * Y_OFFSET) + ((SN_NETWORK_INPUT_NUM (n) - 1) * Y_SPACING);
212 svg_width = determine_network_width (n);
215 printf ("<svg:svg version=\"1.1\" viewBox=\"0 0 %g %g\">\n",
216 svg_width, svg_height);
218 printf ("<?xml version=\"1.0\" standalone=\"no\"?>\n"
219 "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" "
220 "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"
221 "<svg:svg xmlns:svg=\"http://www.w3.org/2000/svg\" version=\"1.1\" "
222 "width=\"%g\" height=\"%g\" viewBox=\"0 0 %g %g\">\n",
223 svg_width, svg_height, svg_width, svg_height);
225 printf ("<!-- Output generated with sn-svg from %s -->\n", PACKAGE_STRING);
227 for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
228 sn_svg_show_stage (SN_NETWORK_STAGE_GET (n, i));
230 printf (" <!-- horizontal lines -->\n");
231 for (i = 0; i < SN_NETWORK_INPUT_NUM (n); i++)
232 printf (" <svg:line x1=\"%g\" y1=\"%i\" x2=\"%g\" y2=\"%i\" "
233 "stroke=\"black\" stroke-width=\"1\" />\n",
234 0.0, Y_OFFSET + (i * Y_SPACING),
235 x_offset, Y_OFFSET + (i * Y_SPACING));
237 printf ("</svg:svg>\n");
242 /* vim: set shiftwidth=2 softtabstop=2 fdm=marker : */