src/sn-svg.c: Add the -e (embed) option.
[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 <unistd.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 static int next_vertex_number = 0;
39
40 static _Bool embedded = 0;
41
42 static void exit_usage (void) /* {{{ */
43 {
44   printf ("Usage: sn-svg [options] [file]\n"
45       "\n"
46       "Valid options are:\n"
47       "  -e    Create output suitable for embedding into other XML files.\n" 
48       "\n");
49   exit (EXIT_FAILURE);
50 } /* }}} void exit_usage */
51
52 static int read_options (int argc, char **argv) /* {{{ */
53 {
54   int option;
55
56   while ((option = getopt (argc, argv, "eh?")) != -1)
57   {
58     switch (option)
59     {
60       case 'e':
61         embedded = 1;
62         break;
63
64       case 'h':
65       case '?':
66       default:
67         exit_usage ();
68     }
69   }
70
71   return (0);
72 } /* }}} int read_options */
73
74 static int tex_show_stage (sn_stage_t *s) /* {{{ */
75 {
76   int lines[s->comparators_num];
77   int right[s->comparators_num];
78   int lines_used = 0;
79   int i;
80
81   printf ("  <!-- stage %i -->\n", SN_STAGE_DEPTH (s));
82
83   for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
84   {
85     lines[i] = -1;
86     right[i] = -1;
87   }
88
89   for (i = 0; i < SN_STAGE_COMP_NUM (s); i++)
90   {
91     int j;
92     sn_comparator_t *c = SN_STAGE_COMP_GET (s, i);
93
94     int min_num;
95     int max_num;
96
97     min_num = next_vertex_number;
98     next_vertex_number++;
99
100     max_num = next_vertex_number;
101     next_vertex_number++;
102
103     for (j = 0; j < lines_used; j++)
104       if (SN_COMP_LEFT (c) > right[j])
105         break;
106
107     lines[i] = j;
108     right[j] = SN_COMP_RIGHT (c);
109     if (j >= lines_used)
110       lines_used = j + 1;
111
112     if (1)
113     {
114       double x1 = x_offset + (j * INNER_SPACING);
115       double x2 = x1;
116       int y1 = Y_OFFSET + (SN_COMP_MIN (c) * Y_SPACING);
117       int y2 = Y_OFFSET + (SN_COMP_MAX (c) * Y_SPACING);
118
119       printf ("  <svg:line x1=\"%g\" y1=\"%i\" x2=\"%g\" y2=\"%i\" "
120           "stroke=\"black\" stroke-width=\"1\" />\n"
121           "  <svg:circle cx=\"%g\" cy=\"%i\" r=\"%g\" fill=\"black\" />\n"
122           "  <svg:circle cx=\"%g\" cy=\"%i\" r=\"%g\" fill=\"black\" />\n",
123           x1, y1, x2, y2,
124           x1, y1, RADIUS,
125           x2, y2, RADIUS);
126     }
127   }
128
129   x_offset = x_offset + ((lines_used - 1) * INNER_SPACING) + OUTER_SPACING;
130
131   printf ("\n");
132
133   return (0);
134 } /* }}} int tex_show_stage */
135
136 int main (int argc, char **argv) /* {{{ */
137 {
138   sn_network_t *n;
139   FILE *fh = NULL;
140   int i;
141
142   read_options (argc, argv);
143
144   if ((argc - optind) == 0)
145     fh = stdin;
146   else if ((argc - optind) == 1)
147     fh = fopen (argv[optind], "r");
148
149   if (fh == NULL)
150     exit_usage ();
151
152   n = sn_network_read (fh);
153
154   if (n == NULL)
155   {
156     printf ("n == NULL!\n");
157     return (1);
158   }
159
160   if (embedded)
161     printf ("<svg:svg version=\"1.1\">\n");
162   else
163     printf ("<?xml version=\"1.0\" standalone=\"no\"?>\n"
164         "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" "
165         "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"
166         "<svg:svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n");
167
168   printf ("<!-- Output generated with sn-svg from %s -->\n", PACKAGE_STRING);
169
170   for (i = 0; i < SN_NETWORK_STAGE_NUM (n); i++)
171      tex_show_stage (SN_NETWORK_STAGE_GET (n, i));
172
173   printf ("  <!-- horizontal lines -->\n");
174   for (i = 0; i < SN_NETWORK_INPUT_NUM (n); i++)
175     printf ("  <svg:line x1=\"%g\" y1=\"%i\" x2=\"%g\" y2=\"%i\" "
176         "stroke=\"black\" stroke-width=\"1\" />\n",
177         0.0, Y_OFFSET + (i * Y_SPACING),
178         x_offset, Y_OFFSET + (i * Y_SPACING));
179
180   printf ("</svg:svg>\n");
181
182   return (0);
183 } /* }}} int main */
184
185 /* vim: set shiftwidth=2 softtabstop=2 fdm=marker : */