2 #include <msp/strings/format.h>
3 #include <msp/strings/utils.h>
4 #include "libr2c2/catalogue.h"
5 #include "libr2c2/track.h"
6 #include "libr2c2/tracktype.h"
7 #include "svgexporter.h"
13 SvgExporter::SvgExporter(const Layout &l):
18 void SvgExporter::save(const string &fn)
20 gauge = layout.get_catalogue().get_gauge();
22 xmlpp::Document *doc = new xmlpp::Document;
23 root = doc->create_root_node("svg", "http://www.w3.org/2000/svg");
25 xmlpp::Element *style = root->add_child("style");
26 style->set_attribute("type", "text/css");
27 styles.push_back(".rail { fill: none; stroke: #000000; }");
28 styles.push_back(".border { fill: none; stroke: #808080; }");
29 styles.push_back(format(".artnr { text-anchor: middle; font-family: sans; font-size: %.3f; }", gauge*750));
31 defs = root->add_child("defs");
35 const set<Track *> &tracks = layout.get_all<Track>();
36 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
40 unsigned n_endpoints = (*i)->get_type().get_endpoints().size();
41 for(unsigned j=0; j<n_endpoints; ++j)
43 Vector pos = (*i)->get_snap_node(j).position;
44 if(i==tracks.begin() && j==0)
48 minp.x = min(minp.x, pos.x);
49 minp.y = min(minp.y, pos.y);
50 maxp.x = max(maxp.x, pos.x);
51 maxp.y = max(maxp.y, pos.y);
56 style->set_child_text(join(styles.begin(), styles.end(), "\n"));
58 root->set_attribute("viewBox", format("%.3f %.3f %.3f %.3f",
59 minp.x*1000-gauge*3, -maxp.y*1000-gauge*3, (maxp.x-minp.x)*1000+gauge*6, (maxp.y-minp.y)*1000+gauge*6));
61 doc->write_to_file_formatted(fn);
64 string SvgExporter::create_appearance(const TrackAppearance &appearance)
66 string key = format("a%p", &appearance);
68 if(!appearances_created.count(&appearance))
70 float rail_width = appearance.get_rail_profile().get_width();
72 styles.push_back(format(".%s .rail { stroke-width: %.3f }", key, rail_width*1000));
73 styles.push_back(format(".%s .border { stroke-width: %.3f }", key, rail_width*500));
75 appearances_created.insert(&appearance);
81 string SvgExporter::create_point_data(const Vector &p)
83 return format("%.3f %.3f", p.x*1000, -p.y*1000);
86 string SvgExporter::create_line_data(const Vector &s, const Vector &e)
88 return format("M %s L %s", create_point_data(s), create_point_data(e));
91 string SvgExporter::create_curve_data(const Vector &s, const Vector &st, const Vector &et, const Vector &e)
93 return format("M %s C %s %s %s", create_point_data(s),
94 create_point_data(s+st), create_point_data(e-et), create_point_data(e));
97 string SvgExporter::create_track_type(const TrackType &type)
99 const ArticleNumber &art_nr = type.get_article_number();
100 string key = format("t%s", art_nr.str());
102 if(!tracks_created.count(&type))
104 const TrackAppearance &appearance = type.get_appearance();
105 string appearance_key = create_appearance(appearance);
107 xmlpp::Element *group = defs->add_child("g");
108 group->set_attribute("id", key);
109 group->set_attribute("class", appearance_key);
111 float rail_width = appearance.get_rail_profile().get_width();
113 const vector<TrackType::Endpoint> &endpoints = type.get_endpoints();
114 for(vector<TrackType::Endpoint>::const_iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
116 xmlpp::Element *path = group->add_child("path");
117 path->set_attribute("class", "border");
119 Vector delta = rotated_vector(Vector(0, gauge, 0), i->dir);
121 path->set_attribute("d", create_line_data(i->pos+delta, i->pos-delta));
124 const vector<TrackPart> &parts = type.get_parts();
125 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
127 xmlpp::Element *path = group->add_child("path");
128 path->set_attribute("class", "rail");
132 OrientedPoint start = i->get_point(0);
133 OrientedPoint end = i->get_point(i->get_length());
136 Vector delta1 = rotated_vector(Vector(0, (gauge+rail_width)*0.5, 0), start.rotation);
137 Vector delta2 = rotated_vector(Vector(0, (gauge+rail_width)*0.5, 0), end.rotation);
138 // Largely an educated guess, but seems to be accurate enough
139 float clen = i->get_length()/2.9;
140 Vector ctrl1 = rotated_vector(Vector(clen, 0, 0), start.rotation);
141 Vector ctrl2 = rotated_vector(Vector(clen, 0, 0), end.rotation);
143 data = create_curve_data(start.position+delta1, ctrl1, ctrl2, end.position+delta2);
145 data += create_curve_data(start.position-delta1, ctrl1, ctrl2, end.position-delta2);
149 Vector delta = rotated_vector(Vector(0, (gauge+rail_width)*0.5, 0), start.rotation);
151 data = create_line_data(start.position+delta, end.position+delta);
153 data += create_line_data(start.position-delta ,end.position-delta);
156 path->set_attribute("d", data);
159 OrientedPoint label_pt = parts.front().get_point(parts.front().get_length()/2);
161 label_pt.rotation.wrap_with_base(-Angle::quarter_turn());
162 if(label_pt.rotation>Angle::quarter_turn())
163 label_pt.rotation -= Angle::half_turn();
165 label_pt.position += rotated_vector(Vector(0, -gauge*0.25, 0), label_pt.rotation);
167 xmlpp::Element *text = group->add_child("text");
168 text->set_attribute("class", "artnr");
169 text->set_attribute("transform", format("translate(%s) rotate(%.3f)",
170 create_point_data(label_pt.position), -label_pt.rotation.degrees()));
171 text->set_child_text(type.get_article_number().str());
173 tracks_created.insert(&type);
179 void SvgExporter::save_track(const Track &track)
181 string type_key = create_track_type(track.get_type());
182 xmlpp::Element *use = root->add_child("use");
184 const Vector &pos = track.get_position();
185 const Angle &rot = track.get_rotation();
186 string transform = format("translate(%s) rotate(%.3f)", create_point_data(pos), -rot.degrees());
187 use->set_attribute("transform", transform);
189 use->set_attribute("xlink:href", "#"+type_key);