svg_mapper.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2009-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2015, 2016.
  4. // Modifications copyright (c) 2015-2016, Oracle and/or its affiliates.
  5. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  8. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  9. // Use, modification and distribution is subject to the Boost Software License,
  10. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #ifndef BOOST_GEOMETRY_IO_SVG_MAPPER_HPP
  13. #define BOOST_GEOMETRY_IO_SVG_MAPPER_HPP
  14. #include <cstdio>
  15. #include <vector>
  16. #include <boost/config.hpp>
  17. #include <boost/mpl/assert.hpp>
  18. #include <boost/noncopyable.hpp>
  19. #include <boost/scoped_ptr.hpp>
  20. #include <boost/type_traits/is_same.hpp>
  21. #include <boost/type_traits/remove_const.hpp>
  22. #include <boost/algorithm/string/split.hpp>
  23. #include <boost/algorithm/string/classification.hpp>
  24. #include <boost/geometry/core/tags.hpp>
  25. #include <boost/geometry/core/tag_cast.hpp>
  26. #include <boost/geometry/algorithms/envelope.hpp>
  27. #include <boost/geometry/algorithms/expand.hpp>
  28. #include <boost/geometry/algorithms/is_empty.hpp>
  29. #include <boost/geometry/algorithms/transform.hpp>
  30. #include <boost/geometry/strategies/transform/map_transformer.hpp>
  31. #include <boost/geometry/views/segment_view.hpp>
  32. #include <boost/geometry/io/svg/write.hpp>
  33. // Helper geometries (all points are transformed to svg-points)
  34. #include <boost/geometry/geometries/geometries.hpp>
  35. namespace boost { namespace geometry
  36. {
  37. #ifndef DOXYGEN_NO_DISPATCH
  38. namespace dispatch
  39. {
  40. template <typename GeometryTag, typename Geometry, typename SvgPoint>
  41. struct svg_map
  42. {
  43. BOOST_MPL_ASSERT_MSG
  44. (
  45. false, NOT_OR_NOT_YET_IMPLEMENTED_FOR_THIS_GEOMETRY_TYPE
  46. , (Geometry)
  47. );
  48. };
  49. template <typename Point, typename SvgPoint>
  50. struct svg_map<point_tag, Point, SvgPoint>
  51. {
  52. template <typename TransformStrategy>
  53. static inline void apply(std::ostream& stream,
  54. std::string const& style, double size,
  55. Point const& point, TransformStrategy const& strategy)
  56. {
  57. SvgPoint ipoint;
  58. geometry::transform(point, ipoint, strategy);
  59. stream << geometry::svg(ipoint, style, size) << std::endl;
  60. }
  61. };
  62. template <typename BoxSeg1, typename BoxSeg2, typename SvgPoint>
  63. struct svg_map_box_seg
  64. {
  65. template <typename TransformStrategy>
  66. static inline void apply(std::ostream& stream,
  67. std::string const& style, double size,
  68. BoxSeg1 const& box_seg, TransformStrategy const& strategy)
  69. {
  70. BoxSeg2 ibox_seg;
  71. // Fix bug in gcc compiler warning for possible uninitialization
  72. #if defined(BOOST_GCC)
  73. geometry::assign_zero(ibox_seg);
  74. #endif
  75. geometry::transform(box_seg, ibox_seg, strategy);
  76. stream << geometry::svg(ibox_seg, style, size) << std::endl;
  77. }
  78. };
  79. template <typename Box, typename SvgPoint>
  80. struct svg_map<box_tag, Box, SvgPoint>
  81. : svg_map_box_seg<Box, model::box<SvgPoint>, SvgPoint>
  82. {};
  83. template <typename Segment, typename SvgPoint>
  84. struct svg_map<segment_tag, Segment, SvgPoint>
  85. : svg_map_box_seg<Segment, model::segment<SvgPoint>, SvgPoint>
  86. {};
  87. template <typename Range1, typename Range2, typename SvgPoint>
  88. struct svg_map_range
  89. {
  90. template <typename TransformStrategy>
  91. static inline void apply(std::ostream& stream,
  92. std::string const& style, double size,
  93. Range1 const& range, TransformStrategy const& strategy)
  94. {
  95. Range2 irange;
  96. geometry::transform(range, irange, strategy);
  97. stream << geometry::svg(irange, style, size) << std::endl;
  98. }
  99. };
  100. template <typename Ring, typename SvgPoint>
  101. struct svg_map<ring_tag, Ring, SvgPoint>
  102. : svg_map_range<Ring, model::ring<SvgPoint>, SvgPoint>
  103. {};
  104. template <typename Linestring, typename SvgPoint>
  105. struct svg_map<linestring_tag, Linestring, SvgPoint>
  106. : svg_map_range<Linestring, model::linestring<SvgPoint>, SvgPoint>
  107. {};
  108. template <typename Polygon, typename SvgPoint>
  109. struct svg_map<polygon_tag, Polygon, SvgPoint>
  110. {
  111. template <typename TransformStrategy>
  112. static inline void apply(std::ostream& stream,
  113. std::string const& style, double size,
  114. Polygon const& polygon, TransformStrategy const& strategy)
  115. {
  116. model::polygon<SvgPoint> ipoly;
  117. geometry::transform(polygon, ipoly, strategy);
  118. stream << geometry::svg(ipoly, style, size) << std::endl;
  119. }
  120. };
  121. template <typename Multi, typename SvgPoint>
  122. struct svg_map<multi_tag, Multi, SvgPoint>
  123. {
  124. typedef typename single_tag_of
  125. <
  126. typename geometry::tag<Multi>::type
  127. >::type stag;
  128. template <typename TransformStrategy>
  129. static inline void apply(std::ostream& stream,
  130. std::string const& style, double size,
  131. Multi const& multi, TransformStrategy const& strategy)
  132. {
  133. for (typename boost::range_iterator<Multi const>::type it
  134. = boost::begin(multi);
  135. it != boost::end(multi);
  136. ++it)
  137. {
  138. svg_map
  139. <
  140. stag,
  141. typename boost::range_value<Multi>::type,
  142. SvgPoint
  143. >::apply(stream, style, size, *it, strategy);
  144. }
  145. }
  146. };
  147. template <typename SvgPoint, typename Geometry>
  148. struct devarianted_svg_map
  149. {
  150. template <typename TransformStrategy>
  151. static inline void apply(std::ostream& stream,
  152. std::string const& style,
  153. double size,
  154. Geometry const& geometry,
  155. TransformStrategy const& strategy)
  156. {
  157. svg_map
  158. <
  159. typename tag_cast
  160. <
  161. typename tag<Geometry>::type,
  162. multi_tag
  163. >::type,
  164. typename boost::remove_const<Geometry>::type,
  165. SvgPoint
  166. >::apply(stream, style, size, geometry, strategy);
  167. }
  168. };
  169. template <typename SvgPoint, BOOST_VARIANT_ENUM_PARAMS(typename T)>
  170. struct devarianted_svg_map<SvgPoint, variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  171. {
  172. template <typename TransformStrategy>
  173. struct visitor: static_visitor<void>
  174. {
  175. std::ostream& m_os;
  176. std::string const& m_style;
  177. double m_size;
  178. TransformStrategy const& m_strategy;
  179. visitor(std::ostream& os,
  180. std::string const& style,
  181. double size,
  182. TransformStrategy const& strategy)
  183. : m_os(os)
  184. , m_style(style)
  185. , m_size(size)
  186. , m_strategy(strategy)
  187. {}
  188. template <typename Geometry>
  189. inline void operator()(Geometry const& geometry) const
  190. {
  191. devarianted_svg_map<SvgPoint, Geometry>::apply(m_os, m_style, m_size, geometry, m_strategy);
  192. }
  193. };
  194. template <typename TransformStrategy>
  195. static inline void apply(std::ostream& stream,
  196. std::string const& style,
  197. double size,
  198. variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
  199. TransformStrategy const& strategy)
  200. {
  201. boost::apply_visitor(visitor<TransformStrategy>(stream, style, size, strategy), geometry);
  202. }
  203. };
  204. } // namespace dispatch
  205. #endif
  206. template <typename SvgPoint, typename Geometry, typename TransformStrategy>
  207. inline void svg_map(std::ostream& stream,
  208. std::string const& style, double size,
  209. Geometry const& geometry, TransformStrategy const& strategy)
  210. {
  211. dispatch::devarianted_svg_map<SvgPoint, Geometry>::apply(stream,
  212. style, size, geometry, strategy);
  213. }
  214. /*!
  215. \brief Helper class to create SVG maps
  216. \tparam Point Point type, for input geometries.
  217. \tparam SameScale Boolean flag indicating if horizontal and vertical scale should
  218. be the same. The default value is true
  219. \tparam SvgCoordinateType Coordinate type of SVG points. SVG is capable to
  220. use floating point coordinates. Therefore the default value is double
  221. \ingroup svg
  222. \qbk{[include reference/io/svg.qbk]}
  223. */
  224. template
  225. <
  226. typename Point,
  227. bool SameScale = true,
  228. typename SvgCoordinateType = double
  229. >
  230. class svg_mapper : boost::noncopyable
  231. {
  232. typedef model::point<SvgCoordinateType, 2, cs::cartesian> svg_point_type;
  233. typedef typename geometry::select_most_precise
  234. <
  235. typename coordinate_type<Point>::type,
  236. double
  237. >::type calculation_type;
  238. typedef strategy::transform::map_transformer
  239. <
  240. calculation_type,
  241. geometry::dimension<Point>::type::value,
  242. geometry::dimension<Point>::type::value,
  243. true,
  244. SameScale
  245. > transformer_type;
  246. model::box<Point> m_bounding_box;
  247. boost::scoped_ptr<transformer_type> m_matrix;
  248. std::ostream& m_stream;
  249. SvgCoordinateType m_width, m_height;
  250. std::string m_width_height; // for <svg> tag only, defaults to 2x 100%
  251. void init_matrix()
  252. {
  253. if (! m_matrix)
  254. {
  255. m_matrix.reset(new transformer_type(m_bounding_box,
  256. m_width, m_height));
  257. m_stream << "<?xml version=\"1.0\" standalone=\"no\"?>"
  258. << std::endl
  259. << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\""
  260. << std::endl
  261. << "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">"
  262. << std::endl
  263. << "<svg " << m_width_height << " version=\"1.1\""
  264. << std::endl
  265. << "xmlns=\"http://www.w3.org/2000/svg\""
  266. << std::endl
  267. << "xmlns:xlink=\"http://www.w3.org/1999/xlink\""
  268. << ">"
  269. << std::endl;
  270. }
  271. }
  272. public :
  273. /*!
  274. \brief Constructor, initializing the SVG map. Opens and initializes the SVG.
  275. Should be called explicitly.
  276. \param stream Output stream, should be a stream already open
  277. \param width Width of the SVG map (in SVG pixels)
  278. \param height Height of the SVG map (in SVG pixels)
  279. \param width_height Optional information to increase width and/or height
  280. */
  281. svg_mapper(std::ostream& stream
  282. , SvgCoordinateType width
  283. , SvgCoordinateType height
  284. , std::string const& width_height = "width=\"100%\" height=\"100%\"")
  285. : m_stream(stream)
  286. , m_width(width)
  287. , m_height(height)
  288. , m_width_height(width_height)
  289. {
  290. assign_inverse(m_bounding_box);
  291. }
  292. /*!
  293. \brief Destructor, called automatically. Closes the SVG by streaming <\/svg>
  294. */
  295. virtual ~svg_mapper()
  296. {
  297. m_stream << "</svg>" << std::endl;
  298. }
  299. /*!
  300. \brief Adds a geometry to the transformation matrix. After doing this,
  301. the specified geometry can be mapped fully into the SVG map
  302. \tparam Geometry \tparam_geometry
  303. \param geometry \param_geometry
  304. */
  305. template <typename Geometry>
  306. void add(Geometry const& geometry)
  307. {
  308. if (! geometry::is_empty(geometry))
  309. {
  310. expand(m_bounding_box,
  311. return_envelope
  312. <
  313. model::box<Point>
  314. >(geometry));
  315. }
  316. }
  317. /*!
  318. \brief Maps a geometry into the SVG map using the specified style
  319. \tparam Geometry \tparam_geometry
  320. \param geometry \param_geometry
  321. \param style String containing verbatim SVG style information
  322. \param size Optional size (used for SVG points) in SVG pixels. For linestrings,
  323. specify linewidth in the SVG style information
  324. */
  325. template <typename Geometry>
  326. void map(Geometry const& geometry, std::string const& style,
  327. double size = -1.0)
  328. {
  329. init_matrix();
  330. svg_map<svg_point_type>(m_stream, style, size, geometry, *m_matrix);
  331. }
  332. /*!
  333. \brief Adds a text to the SVG map
  334. \tparam TextPoint \tparam_point
  335. \param point Location of the text (in map units)
  336. \param s The text itself
  337. \param style String containing verbatim SVG style information, of the text
  338. \param offset_x Offset in SVG pixels, defaults to 0
  339. \param offset_y Offset in SVG pixels, defaults to 0
  340. \param lineheight Line height in SVG pixels, in case the text contains \n
  341. */
  342. template <typename TextPoint>
  343. void text(TextPoint const& point, std::string const& s,
  344. std::string const& style,
  345. double offset_x = 0.0, double offset_y = 0.0,
  346. double lineheight = 10.0)
  347. {
  348. init_matrix();
  349. svg_point_type map_point;
  350. transform(point, map_point, *m_matrix);
  351. m_stream
  352. << "<text style=\"" << style << "\""
  353. << " x=\"" << get<0>(map_point) + offset_x << "\""
  354. << " y=\"" << get<1>(map_point) + offset_y << "\""
  355. << ">";
  356. if (s.find("\n") == std::string::npos)
  357. {
  358. m_stream << s;
  359. }
  360. else
  361. {
  362. // Multi-line modus
  363. std::vector<std::string> splitted;
  364. boost::split(splitted, s, boost::is_any_of("\n"));
  365. for (std::vector<std::string>::const_iterator it
  366. = splitted.begin();
  367. it != splitted.end();
  368. ++it, offset_y += lineheight)
  369. {
  370. m_stream
  371. << "<tspan x=\"" << get<0>(map_point) + offset_x
  372. << "\""
  373. << " y=\"" << get<1>(map_point) + offset_y
  374. << "\""
  375. << ">" << *it << "</tspan>";
  376. }
  377. }
  378. m_stream << "</text>" << std::endl;
  379. }
  380. };
  381. }} // namespace boost::geometry
  382. #endif // BOOST_GEOMETRY_IO_SVG_MAPPER_HPP