File: output-svg.c

package info (click to toggle)
autotrace 0.31.1-15
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,500 kB
  • ctags: 1,201
  • sloc: ansic: 12,595; sh: 8,311; makefile: 203
file content (101 lines) | stat: -rw-r--r-- 3,238 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* output-svg.h - output in SVG format

   Copyright (C) 1999, 2000, 2001 Bernhard Herzog

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public License
   as published by the Free Software Foundation; either version 2.1 of
   the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   USA. */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* Def: HAVE_CONFIG_H */

#include "spline.h"
#include "color.h"
#include "output-svg.h"

static void
out_splines (FILE * file, spline_list_array_type shape, int height)
{
  unsigned this_list;
  spline_list_type list;
  color_type last_color = {0,0,0};

  for (this_list = 0; this_list < SPLINE_LIST_ARRAY_LENGTH (shape);
    this_list++)
    {
      unsigned this_spline;
	  spline_type first;

      list = SPLINE_LIST_ARRAY_ELT (shape, this_list);
      first = SPLINE_LIST_ELT (list, 0);

      if (this_list == 0 || !COLOR_EQUAL(list.color, last_color))
		{
          if (this_list > 0)
            {
              if (!(shape.centerline || list.open)) fputs("z", file);
              fputs("\"/>\n", file);
            }
          fprintf(file, "<path style=\"%s:#%02x%02x%02x; %s:none;\" d=\"",
		    (shape.centerline || list.open) ? "stroke" : "fill",
		    list.color.r, list.color.g, list.color.b,
		    (shape.centerline || list.open) ? "fill" : "stroke");
        }
      fprintf(file, "M%g %g",
        START_POINT(first).x, height - START_POINT(first).y);
      for (this_spline = 0; this_spline < SPLINE_LIST_LENGTH (list);
	     this_spline++)
        {
          spline_type s = SPLINE_LIST_ELT (list, this_spline);


          if (SPLINE_DEGREE(s) == LINEARTYPE)
            {
              fprintf(file, "L%g %g",
              END_POINT(s).x, height - END_POINT(s).y);
            }
          else
            {
              fprintf(file, "C%g %g %g %g %g %g",
                CONTROL1(s).x, height - CONTROL1(s).y,
                CONTROL2(s).x, height - CONTROL2(s).y,
                END_POINT(s).x, height - END_POINT(s).y);
            }
          last_color = list.color;
        }
    }
  if (!(shape.centerline || list.open)) fputs("z", file);
  if (SPLINE_LIST_ARRAY_LENGTH(shape) > 0)
    fputs("\"/>\n", file);
}


int 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)
{
    int width = urx - llx;
    int height = ury - lly;
    fputs("<?xml version=\"1.0\" standalone=\"yes\"?>\n", file);
    fprintf(file, "<svg width=\"%d\" height=\"%d\">\n", width, height);

    out_splines(file, shape, height);
    fputs("</svg>\n", file);
    
    return 0;
}