SVG.cpp
Go to the documentation of this file.
1 // Copyright (c) 2013-2014 by Wayne C. Gramlich. All rights reserved.
2 
3 #include <assert.h>
4 
5 #include "Bounding_Box.hpp"
6 #include "String.hpp"
7 #include "SVG.hpp"
8 
9 #include <algorithm>
10 
22 
23 void SVG::cartesian_scale(double x_width, double y_height,
24  BoundingBox *bounding_box) {
25  // Grab the minimum/maximum values from *bounding_box*.
26  double maximum_x = bounding_box->max_x();
27  double minimum_x = bounding_box->min_x();
28  double maximum_y = bounding_box->max_y();
29  double minimum_y = bounding_box->min_y();
30 
31  // Compute the span in x and y:
32  double x_span = maximum_x - minimum_x;
33  double y_span = maximum_y - minimum_y;
34  //File__format(stderr, "X/Y span=(%.2f, %.2f)\n", x_span, y_span);
35 
36  // COmpute the axis independent scale in X and Y:
37  double x_scale = x_width / x_span;
38  double y_scale = y_height / y_span;
39  //File__format(stderr, "X/Y scale=(%.6f, %.6f)\n", x_scale, y_scale);
40 
41  // Take the smaller of the two so that the X and Y scales are equal:
42  x_scale = std::min(x_scale, y_scale);
43  y_scale = x_scale;
44  //File__format(stderr, "X/Y scale=(%.6f, %.6f)\n", x_scale, y_scale);
45 
46  // Compute X/Y spans adjusted by the aspect ratio:
47  double scaled_x_span = x_span;
48  double scaled_y_span = y_span;
49  //File__format(stderr,
50  // "Scaled X/Y span=(%.2f, %.2f)\n", scaled_x_span, scaled_y_span);
51 
52  // Now compute the X and Y offsets:
53  double x_offset = (minimum_x + maximum_x) / 2.0 - scaled_x_span / 2.0;
54  // Swap Y axis direction by adding *scaled_y_span* instead of subtracting:
55  double y_offset = (minimum_y + maximum_y) / 2.0 + scaled_y_span / 2.0;
56  //File__format(stderr, "X/Y offset=(%.2f, %.2f)\n", x_offset, y_offset);
57 
58  // Load up *svg*:
59  width = x_width * x_scale;
60  height = y_height * y_scale;
61  this->x_scale = x_scale;
62  // Swap Y axis direction by negating *y_scale*:
63  this->y_scale = -y_scale;
64  this->x_offset = -x_offset;
65  this->y_offset = -y_offset;
66 }
67 
68 
73 
75  // Close *svg*:
76  File__format(stream, "</svg>\n");
78 }
79 
90 
91 void SVG::line(double x1, double y1, double x2, double y2,
92  String_Const stroke) {
93 
94  // Output "<line ... />" to *svg_stream*:
96  "<line x1=\"%f%s\" y1=\"%f%s\"",
97  (x1 + x_offset) * x_scale, units, (y1 + y_offset) * y_scale, units);
99  " x2=\"%f%s\" y2=\"%f%s\"",
100  (x2 + x_offset) * x_scale, units, (y2 + y_offset) * y_scale, units);
102  " style=\"stroke:%s\"/>\n", stroke);
103 }
104 
116 
118  double width, double height, double x_scale, double y_scale,
120  // Verify that units are OK:
121  assert (String__equal(units, "cm") ||
122  String__equal(units, "mm") ||
123  String__equal(units, "in"));
124 
125  // Get *svg_stream* opened:
126  char file_name[100];
127  (void)sprintf(file_name, "%s.svg", base_name);
128  stream = File__open(file_name, "w");
129  if (stream == (File)0) {
130  File__format(stderr, "Unable to open %s.svg\n", base_name);
131  } else {
132  // Allocate and load up *svg*:
133  this->height = height;
134  this->width = width;
135  this->units = units;
136  this->x_scale = x_scale;
137  this->y_scale = y_scale;
138  this->x_offset = 0.0;
139  this->y_offset = 0.0;
140 
141  // Ouput the header for *svg*:
143  "<?xml version=\"1.0\" standalone=\"no\"?>\n\n");
145  "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n");
147  " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n\n");
149  "<svg width=\"%f%s\" height=\"%f%s\"\n",
150  width * x_scale, units, height * y_scale, units);
152  " version=\"1.1\"\n");
154  " xmlns=\"http://www.w3.org/2000/svg\">\n\n");
155  }
156 }
157 
170 
171 void SVG::rectangle(double x, double y, double width, double height,
172  String_Const stroke_color, String_Const fill_color) {
173  // Output "<rect ... />" to *svg_stream*:
174  double x_final = (x + x_offset) * x_scale;
175  double y_final = (y + y_offset) * y_scale;
177  "<rect x=\"%f%s\" y=\"%f%s\"", x_final, units, y_final, units);
179  " width=\"%f%s\" height=\"%f%s\"",
180  width * x_scale, units, height * y_scale, units);
182  " style=\"stroke:%s; fill:%s\"/>\n", stroke_color, fill_color);
183 }
184 
195 
196 void SVG::text(String_Const message, double x, double y,
197  String_Const font_family, unsigned int font_size) {
198 
200  "<text x=\"%f%s\" y=\"%f%s\"",
201  (x + x_offset) * x_scale, units, (y + y_offset) * y_scale, units);
203  " style=\"font-family:%s; font-size:%d\">", font_family, font_size);
204  File__format(stream, "%s</text>\n", message);
205 }
206 
207 void SVG::setOffsets(double x, double y) {
208  x_offset = x;
209  y_offset = y;
210 }
double width
Width of SVG image.
Definition: SVG.hpp:24
SVG(String_Const base_name, double width, double height, double x_scale, double y_scale, String_Const units)
Open an SVG object:
Definition: SVG.cpp:117
double y_scale
Amount to scale Y by.
Definition: SVG.hpp:39
double max_y()
double y_offset
Amount to offset Y by.
Definition: SVG.hpp:36
void File__close(File file)
Closes file.
Definition: File.cpp:50
~SVG()
Close SVG object.
Definition: SVG.cpp:74
void cartesian_scale(double x_width, double y_height, BoundingBox *bounding_box)
Turns svg into a cartesian graphing canvas bounded by bounding_box.
Definition: SVG.cpp:23
double x_scale
Amount to scale X by.
Definition: SVG.hpp:33
FILE * File
FILE is a file I/O object.
Definition: File.hpp:12
double max_x()
void line(double x1, double y1, double x2, double y2, String_Const stroke)
Draw a line from (x1, y1) to (x2, y2) using stroke.
Definition: SVG.cpp:91
double min_x()
void File__format(File file, String_Const format,...)
will write format out to file with all patterns that start with "%" replaced by formatted versions of...
Definition: File.cpp:107
File stream
The output stream.
Definition: SVG.hpp:18
void text(String_Const message, double x, double y, String_Const font_family, unsigned int font_size)
draw message at (x, y).
Definition: SVG.cpp:196
double x_offset
Amount to offset X by.
Definition: SVG.hpp:30
void setOffsets(double x_offset, double y_offset)
Definition: SVG.cpp:207
String_Const units
Units to use for SVG values.
Definition: SVG.hpp:27
double height
Hight of SVG image.
Definition: SVG.hpp:21
File File__open(String_Const file_name, String_Const flags)
will open file_name using flags to specify read/write options.
Definition: File.cpp:243
double min_y()
const char * String_Const
Definition: String.hpp:9
bool String__equal(String_Const string1, String_Const string2)
Returns true if string1 equals string2.
Definition: String.cpp:31
void rectangle(double x, double y, double width, double height, String_Const stroke_color, String_Const fill_color)
Draw a width by height rectangle at (x, y).
Definition: SVG.cpp:171


fiducial_lib
Author(s): Wayne Gramlich
autogenerated on Thu Dec 28 2017 04:06:53