src/sn-tex.c: Make it possible to specify the absolute width of the graphic.
[sort-networks.git] / src / sn-svg.c
1 /**
2  * libsortnetwork - src/sn-svg.c
3  * Copyright (C) 2008-2010  Florian octo Forster
4  *
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.
8  *
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.
13  *
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
17  *
18  * Authors:
19  *   Florian octo Forster <ff at octo.it>
20  **/
21
22 #include "config.h"
23
24 #ifndef _ISOC99_SOURCE
25 # define _ISOC99_SOURCE
26 #endif
27 #ifndef _POSIX_C_SOURCE
28 # define _POSIX_C_SOURCE 200112L
29 #endif
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <assert.h>
35
36 #include "sn_network.h"
37
38 #define INNER_SPACING 15.0
39 #define OUTER_SPACING 40.0
40 #define RADIUS 4.0
41
42 #define Y_OFFSET 5
43 #define Y_SPACING 40
44
45 static double x_offset = OUTER_SPACING;
46 static _Bool embedded = 0;
47
48 static void exit_usage (void) /* {{{ */
49 {
50   printf ("Usage: sn-svg [options] [file]\n"
51       "\n"
52       "Valid options are:\n"
53       "  -e    Create output suitable for embedding into other XML files.\n" 
54       "\n");
55   exit (EXIT_FAILURE);
56 } /* }}} void exit_usage */
57
58 static int read_options (int argc, char **argv) /* {{{ */
59 {
60   int option;
61
62   while ((option = getopt (argc, argv, "eh?")) != -1)
63   {
64     switch (option)
65     {
66       case 'e':
67         embedded = 1;
68         break;
69
70       case 'h':
71       case '?':
72       default:
73         exit_usage ();
74     }
75   }
76
77   return (0);
78 } /* }}} int read_options */
79
80 static double determine_stage_width (sn_stage_t *s) /* {{{ */
81 {
82   int lines[s->comparators_num];
83   int right[s->comparators_num];
84   int lines_used = 0;
85   int i;
86
87   if (SN_STAGE_COMP_NUM (s) == 0)
88     return (0.0);
89
90   for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
91   {
92     lines[i] = -1;
93     right[i] = -1;
94   }
95
96   for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
97   {
98     int j;
99     sn_comparator_t *c = SN_STAGE_COMP_GET (s, i);
100
101     for (j = 0; j < lines_used; j++)
102       if (SN_COMP_LEFT (c) > right[j])
103         break;
104
105     lines[i] = j;
106     right[j] = SN_COMP_RIGHT (c);
107     if (j >= lines_used)
108       lines_used = j + 1;
109   }
110   assert (lines_used >= 1);
111
112   return (((double) (lines_used - 1)) * INNER_SPACING);
113 } /* }}} double determine_stage_width */
114
115 static double determine_network_width (sn_network_t *n) /* {{{ */
116 {
117   double width;
118   int i;
119
120   /* Spacing between stages and at the beginning and end of the network */
121   width = (SN_NETWORK_STAGE_NUM (n) + 1) * OUTER_SPACING;
122
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));
126
127   return (width);
128 } /* }}} double determine_network_width */
129
130 static int sn_svg_show_stage (sn_stage_t *s) /* {{{ */
131 {
132   int lines[s->comparators_num];
133   int right[s->comparators_num];
134   int lines_used = 0;
135   int i;
136
137   printf ("  <!-- stage %i -->\n", SN_STAGE_DEPTH (s));
138
139   for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
140   {
141     lines[i] = -1;
142     right[i] = -1;
143   }
144
145   for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
146   {
147     int j;
148     sn_comparator_t *c = SN_STAGE_COMP_GET (s, i);
149
150     for (j = 0; j < lines_used; j++)
151       if (SN_COMP_LEFT (c) > right[j])
152         break;
153
154     lines[i] = j;
155     right[j] = SN_COMP_RIGHT (c);
156     if (j >= lines_used)
157       lines_used = j + 1;
158
159     if (1)
160     {
161       double x1 = x_offset + (j * INNER_SPACING);
162       double x2 = x1;
163       int y1 = Y_OFFSET + (SN_COMP_MIN (c) * Y_SPACING);
164       int y2 = Y_OFFSET + (SN_COMP_MAX (c) * Y_SPACING);
165
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",
170           x1, y1, x2, y2,
171           x1, y1, RADIUS,
172           x2, y2, RADIUS);
173     }
174   }
175
176   x_offset = x_offset + ((lines_used - 1) * INNER_SPACING) + OUTER_SPACING;
177
178   printf ("\n");
179
180   return (0);
181 } /* }}} int sn_svg_show_stage */
182
183 int main (int argc, char **argv) /* {{{ */
184 {
185   sn_network_t *n;
186   FILE *fh = NULL;
187
188   double svg_height;
189   double svg_width;
190
191   int i;
192
193   read_options (argc, argv);
194
195   if ((argc - optind) == 0)
196     fh = stdin;
197   else if ((argc - optind) == 1)
198     fh = fopen (argv[optind], "r");
199
200   if (fh == NULL)
201     exit_usage ();
202
203   n = sn_network_read (fh);
204
205   if (n == NULL)
206   {
207     printf ("n == NULL!\n");
208     return (1);
209   }
210
211   svg_height = (2 * Y_OFFSET) + ((SN_NETWORK_INPUT_NUM (n) - 1) * Y_SPACING);
212   svg_width = determine_network_width (n);
213
214   if (embedded)
215     printf ("<svg:svg version=\"1.1\" viewBox=\"0 0 %g %g\">\n",
216         svg_width, svg_height);
217   else
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);
224
225   printf ("<!-- Output generated with sn-svg from %s -->\n", PACKAGE_STRING);
226
227   for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
228      sn_svg_show_stage (SN_NETWORK_STAGE_GET (n, i));
229
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));
236
237   printf ("</svg:svg>\n");
238
239   return (0);
240 } /* }}} int main */
241
242 /* vim: set shiftwidth=2 softtabstop=2 fdm=marker : */