src/sn-svg.c: Print the SVG's height and width and a viewBox.
[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 #include <stdlib.h>
25 #include <stdio.h>
26 #include <assert.h>
27
28 #include "sn_network.h"
29
30 #define INNER_SPACING 15.0
31 #define OUTER_SPACING 40.0
32 #define RADIUS 4.0
33
34 #define Y_OFFSET 5
35 #define Y_SPACING 40
36
37 static double x_offset = OUTER_SPACING;
38
39 static double determine_stage_width (sn_stage_t *s)
40 {
41   int lines[s->comparators_num];
42   int right[s->comparators_num];
43   int lines_used = 0;
44   int i;
45
46   if (SN_STAGE_COMP_NUM (s) == 0)
47     return (0.0);
48
49   for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
50   {
51     lines[i] = -1;
52     right[i] = -1;
53   }
54
55   for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
56   {
57     int j;
58     sn_comparator_t *c = SN_STAGE_COMP_GET (s, i);
59
60     for (j = 0; j < lines_used; j++)
61       if (SN_COMP_LEFT (c) > right[j])
62         break;
63
64     lines[i] = j;
65     right[j] = SN_COMP_RIGHT (c);
66     if (j >= lines_used)
67       lines_used = j + 1;
68   }
69   assert (lines_used >= 1);
70
71   return (((double) (lines_used - 1)) * INNER_SPACING);
72 }
73
74 static double determine_network_width (sn_network_t *n) /* {{{ */
75 {
76   double width;
77   int i;
78
79   /* Spacing between stages and at the beginning and end of the network */
80   width = (SN_NETWORK_STAGE_NUM (n) + 1) * OUTER_SPACING;
81
82   /* Spacing required within a stage */
83   for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
84     width += determine_stage_width (SN_NETWORK_STAGE_GET (n, i));
85
86   return (width);
87 } /* }}} double determine_network_width */
88
89 static int sn_svg_show_stage (sn_stage_t *s)
90 {
91   int lines[s->comparators_num];
92   int right[s->comparators_num];
93   int lines_used = 0;
94   int i;
95
96   printf ("  <!-- stage %i -->\n", SN_STAGE_DEPTH (s));
97
98   for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
99   {
100     lines[i] = -1;
101     right[i] = -1;
102   }
103
104   for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
105   {
106     int j;
107     sn_comparator_t *c = SN_STAGE_COMP_GET (s, i);
108
109     for (j = 0; j < lines_used; j++)
110       if (SN_COMP_LEFT (c) > right[j])
111         break;
112
113     lines[i] = j;
114     right[j] = SN_COMP_RIGHT (c);
115     if (j >= lines_used)
116       lines_used = j + 1;
117
118     if (1)
119     {
120       double x1 = x_offset + (j * INNER_SPACING);
121       double x2 = x1;
122       int y1 = Y_OFFSET + (SN_COMP_MIN (c) * Y_SPACING);
123       int y2 = Y_OFFSET + (SN_COMP_MAX (c) * Y_SPACING);
124
125       printf ("  <line x1=\"%g\" y1=\"%i\" x2=\"%g\" y2=\"%i\" "
126           "stroke=\"black\" stroke-width=\"1\" />\n"
127           "  <circle cx=\"%g\" cy=\"%i\" r=\"%g\" fill=\"black\" />\n"
128           "  <circle cx=\"%g\" cy=\"%i\" r=\"%g\" fill=\"black\" />\n",
129           x1, y1, x2, y2,
130           x1, y1, RADIUS,
131           x2, y2, RADIUS);
132     }
133   }
134
135   x_offset = x_offset + ((lines_used - 1) * INNER_SPACING) + OUTER_SPACING;
136
137   printf ("\n");
138
139   return (0);
140 } /* int sn_svg_show_stage */
141
142 int main (int argc, char **argv)
143 {
144   sn_network_t *n;
145   FILE *fh = NULL;
146
147   double svg_height;
148   double svg_width;
149
150   int i;
151
152   if (argc == 1)
153     fh = stdin;
154   else if (argc == 2)
155     fh = fopen (argv[1], "r");
156
157   if (fh == NULL)
158   {
159     printf ("fh == NULL!\n");
160     return (1);
161   }
162
163   n = sn_network_read (fh);
164
165   if (n == NULL)
166   {
167     printf ("n == NULL!\n");
168     return (1);
169   }
170
171   svg_height = (2 * Y_OFFSET) + ((SN_NETWORK_INPUT_NUM (n) - 1) * Y_SPACING);
172   svg_width = determine_network_width (n);
173
174   printf ("<?xml version=\"1.0\" standalone=\"no\"?>\n"
175       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" "
176       "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"
177       "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" "
178       "width=\"%gpt\" height=\"%gpt\" viewBox=\"0 0 %g %g\">\n",
179       svg_width, svg_height, svg_width, svg_height);
180
181   printf ("<!-- Output generated with sn-svg from %s -->\n", PACKAGE_STRING);
182
183   for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
184      sn_svg_show_stage (SN_NETWORK_STAGE_GET (n, i));
185
186   printf ("  <!-- horizontal lines -->\n");
187   for (i = 0; i < SN_NETWORK_INPUT_NUM (n); i++)
188     printf ("  <line x1=\"%g\" y1=\"%i\" x2=\"%g\" y2=\"%i\" "
189         "stroke=\"black\" stroke-width=\"1\" />\n",
190         0.0, Y_OFFSET + (i * Y_SPACING),
191         x_offset, Y_OFFSET + (i * Y_SPACING));
192
193   printf ("</svg>\n");
194
195   return (0);
196 } /* int main */
197
198 /* vim: set shiftwidth=2 softtabstop=2 : */