1 /* output-svg.h - output in SVG format
2 
3    Copyright (C) 1999, 2000, 2001 Bernhard Herzog
4 
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public License
7    as published by the Free Software Foundation; either version 2.1 of
8    the License, or (at your option) any later version.
9 
10    This library is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18    USA. */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif /* Def: HAVE_CONFIG_H */
23 
24 #include "spline.h"
25 #include "color.h"
26 #include "output-svg.h"
27 
28 static void
out_splines(FILE * file,spline_list_array_type shape,int height)29 out_splines (FILE * file, spline_list_array_type shape, int height)
30 {
31   unsigned this_list;
32   spline_list_type list;
33   color_type last_color = {0,0,0};
34 
35   for (this_list = 0; this_list < SPLINE_LIST_ARRAY_LENGTH (shape);
36     this_list++)
37     {
38       unsigned this_spline;
39 	  spline_type first;
40 
41       list = SPLINE_LIST_ARRAY_ELT (shape, this_list);
42       first = SPLINE_LIST_ELT (list, 0);
43 
44       if (this_list == 0 || !COLOR_EQUAL(list.color, last_color))
45 		{
46           if (this_list > 0)
47             {
48               if (!(shape.centerline || list.open)) fputs("z", file);
49               fputs("\"/>\n", file);
50             }
51           fprintf(file, "<path style=\"%s:#%02x%02x%02x; %s:none;\" d=\"",
52 		    (shape.centerline || list.open) ? "stroke" : "fill",
53 		    list.color.r, list.color.g, list.color.b,
54 		    (shape.centerline || list.open) ? "fill" : "stroke");
55         }
56       fprintf(file, "M%g %g",
57         START_POINT(first).x, height - START_POINT(first).y);
58       for (this_spline = 0; this_spline < SPLINE_LIST_LENGTH (list);
59 	     this_spline++)
60         {
61           spline_type s = SPLINE_LIST_ELT (list, this_spline);
62 
63 
64           if (SPLINE_DEGREE(s) == LINEARTYPE)
65             {
66               fprintf(file, "L%g %g",
67               END_POINT(s).x, height - END_POINT(s).y);
68             }
69           else
70             {
71               fprintf(file, "C%g %g %g %g %g %g",
72                 CONTROL1(s).x, height - CONTROL1(s).y,
73                 CONTROL2(s).x, height - CONTROL2(s).y,
74                 END_POINT(s).x, height - END_POINT(s).y);
75             }
76           last_color = list.color;
77         }
78     }
79   if (!(shape.centerline || list.open)) fputs("z", file);
80   if (SPLINE_LIST_ARRAY_LENGTH(shape) > 0)
81     fputs("\"/>\n", file);
82 }
83 
84 
output_svg_writer(FILE * file,at_string name,int llx,int lly,int urx,int ury,at_output_opts_type * opts,spline_list_array_type shape,at_msg_func msg_func,at_address msg_data)85 int output_svg_writer(FILE* file, at_string name,
86 		      int llx, int lly, int urx, int ury,
87 		      at_output_opts_type * opts,
88 		      spline_list_array_type shape,
89 		      at_msg_func msg_func,
90 		      at_address msg_data)
91 {
92     int width = urx - llx;
93     int height = ury - lly;
94     fputs("<?xml version=\"1.0\" standalone=\"yes\"?>\n", file);
95     fprintf(file, "<svg width=\"%d\" height=\"%d\">\n", width, height);
96 
97     out_splines(file, shape, height);
98     fputs("</svg>\n", file);
99 
100     return 0;
101 }
102