write_svg.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2009-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  4. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #ifndef BOOST_GEOMETRY_IO_SVG_WRITE_SVG_HPP
  9. #define BOOST_GEOMETRY_IO_SVG_WRITE_SVG_HPP
  10. #include <ostream>
  11. #include <string>
  12. #include <boost/config.hpp>
  13. #include <boost/mpl/assert.hpp>
  14. #include <boost/range.hpp>
  15. #include <boost/typeof/typeof.hpp>
  16. #include <boost/geometry/core/exterior_ring.hpp>
  17. #include <boost/geometry/core/interior_rings.hpp>
  18. #include <boost/geometry/core/ring_type.hpp>
  19. #include <boost/geometry/geometries/concepts/check.hpp>
  20. namespace boost { namespace geometry
  21. {
  22. #ifndef DOXYGEN_NO_DETAIL
  23. namespace detail { namespace svg
  24. {
  25. template <typename Point>
  26. struct svg_point
  27. {
  28. template <typename Char, typename Traits>
  29. static inline void apply(std::basic_ostream<Char, Traits>& os,
  30. Point const& p, std::string const& style, int size)
  31. {
  32. os << "<circle cx=\"" << geometry::get<0>(p)
  33. << "\" cy=\"" << geometry::get<1>(p)
  34. << "\" r=\"" << (size < 0 ? 5 : size)
  35. << "\" style=\"" << style << "\"/>";
  36. }
  37. };
  38. template <typename Box>
  39. struct svg_box
  40. {
  41. template <typename Char, typename Traits>
  42. static inline void apply(std::basic_ostream<Char, Traits>& os,
  43. Box const& box, std::string const& style, int )
  44. {
  45. // Prevent invisible boxes, making them >=1, using "max"
  46. BOOST_USING_STD_MAX();
  47. typedef typename coordinate_type<Box>::type ct;
  48. ct x = geometry::get<geometry::min_corner, 0>(box);
  49. ct y = geometry::get<geometry::min_corner, 1>(box);
  50. ct width = max BOOST_PREVENT_MACRO_SUBSTITUTION(1,
  51. geometry::get<geometry::max_corner, 0>(box) - x);
  52. ct height = max BOOST_PREVENT_MACRO_SUBSTITUTION (1,
  53. geometry::get<geometry::max_corner, 1>(box) - y);
  54. os << "<rect x=\"" << x << "\" y=\"" << y
  55. << "\" width=\"" << width << "\" height=\"" << height
  56. << "\" style=\"" << style << "\"/>";
  57. }
  58. };
  59. /*!
  60. \brief Stream ranges as SVG
  61. \note policy is used to select type (polyline/polygon)
  62. */
  63. template <typename Range, typename Policy>
  64. struct svg_range
  65. {
  66. template <typename Char, typename Traits>
  67. static inline void apply(std::basic_ostream<Char, Traits>& os,
  68. Range const& range, std::string const& style, int )
  69. {
  70. typedef typename boost::range_iterator<Range const>::type iterator;
  71. bool first = true;
  72. os << "<" << Policy::prefix() << " points=\"";
  73. for (iterator it = boost::begin(range);
  74. it != boost::end(range);
  75. ++it, first = false)
  76. {
  77. os << (first ? "" : " " )
  78. << geometry::get<0>(*it)
  79. << ","
  80. << geometry::get<1>(*it);
  81. }
  82. os << "\" style=\"" << style << Policy::style() << "\"/>";
  83. }
  84. };
  85. template <typename Polygon>
  86. struct svg_poly
  87. {
  88. template <typename Char, typename Traits>
  89. static inline void apply(std::basic_ostream<Char, Traits>& os,
  90. Polygon const& polygon, std::string const& style, int )
  91. {
  92. typedef typename geometry::ring_type<Polygon>::type ring_type;
  93. typedef typename boost::range_iterator<ring_type const>::type iterator_type;
  94. bool first = true;
  95. os << "<g fill-rule=\"evenodd\"><path d=\"";
  96. ring_type const& ring = geometry::exterior_ring(polygon);
  97. for (iterator_type it = boost::begin(ring);
  98. it != boost::end(ring);
  99. ++it, first = false)
  100. {
  101. os << (first ? "M" : " L") << " "
  102. << geometry::get<0>(*it)
  103. << ","
  104. << geometry::get<1>(*it);
  105. }
  106. // Inner rings:
  107. {
  108. typename interior_return_type<Polygon const>::type rings
  109. = interior_rings(polygon);
  110. for (BOOST_AUTO_TPL(rit, boost::begin(rings));
  111. rit != boost::end(rings); ++rit)
  112. {
  113. first = true;
  114. for (BOOST_AUTO_TPL(it, boost::begin(*rit)); it != boost::end(*rit);
  115. ++it, first = false)
  116. {
  117. os << (first ? "M" : " L") << " "
  118. << geometry::get<0>(*it)
  119. << ","
  120. << geometry::get<1>(*it);
  121. }
  122. }
  123. }
  124. os << " z \" style=\"" << style << "\"/></g>";
  125. }
  126. };
  127. struct prefix_linestring
  128. {
  129. static inline const char* prefix() { return "polyline"; }
  130. static inline const char* style() { return ";fill:none"; }
  131. };
  132. struct prefix_ring
  133. {
  134. static inline const char* prefix() { return "polygon"; }
  135. static inline const char* style() { return ""; }
  136. };
  137. }} // namespace detail::svg
  138. #endif // DOXYGEN_NO_DETAIL
  139. #ifndef DOXYGEN_NO_DISPATCH
  140. namespace dispatch
  141. {
  142. /*!
  143. \brief Dispatching base struct for SVG streaming, specialized below per geometry type
  144. \details Specializations should implement a static method "stream" to stream a geometry
  145. The static method should have the signature:
  146. template <typename Char, typename Traits>
  147. static inline void apply(std::basic_ostream<Char, Traits>& os, G const& geometry)
  148. */
  149. template <typename GeometryTag, typename Geometry>
  150. struct svg
  151. {
  152. BOOST_MPL_ASSERT_MSG
  153. (
  154. false, NOT_OR_NOT_YET_IMPLEMENTED_FOR_THIS_GEOMETRY_TYPE
  155. , (Geometry)
  156. );
  157. };
  158. template <typename Point>
  159. struct svg<point_tag, Point> : detail::svg::svg_point<Point> {};
  160. template <typename Box>
  161. struct svg<box_tag, Box> : detail::svg::svg_box<Box> {};
  162. template <typename Linestring>
  163. struct svg<linestring_tag, Linestring>
  164. : detail::svg::svg_range<Linestring, detail::svg::prefix_linestring> {};
  165. template <typename Ring>
  166. struct svg<ring_tag, Ring>
  167. : detail::svg::svg_range<Ring, detail::svg::prefix_ring> {};
  168. template <typename Polygon>
  169. struct svg<polygon_tag, Polygon>
  170. : detail::svg::svg_poly<Polygon> {};
  171. } // namespace dispatch
  172. #endif // DOXYGEN_NO_DISPATCH
  173. /*!
  174. \brief Generic geometry template manipulator class, takes corresponding output class from traits class
  175. \ingroup svg
  176. \details Stream manipulator, streams geometry classes as SVG (Scalable Vector Graphics)
  177. */
  178. template <typename G>
  179. class svg_manipulator
  180. {
  181. public:
  182. inline svg_manipulator(G const& g, std::string const& style, int size)
  183. : m_geometry(g)
  184. , m_style(style)
  185. , m_size(size)
  186. {}
  187. template <typename Char, typename Traits>
  188. inline friend std::basic_ostream<Char, Traits>& operator<<(
  189. std::basic_ostream<Char, Traits>& os, svg_manipulator const& m)
  190. {
  191. dispatch::svg
  192. <
  193. typename tag<G>::type, G
  194. >::apply(os, m.m_geometry, m.m_style, m.m_size);
  195. os.flush();
  196. return os;
  197. }
  198. private:
  199. G const& m_geometry;
  200. std::string const& m_style;
  201. int m_size;
  202. };
  203. /*!
  204. \brief Manipulator to stream geometries as SVG
  205. \tparam Geometry \tparam_geometry
  206. \param geometry \param_geometry
  207. \param style String containing verbatim SVG style information
  208. \param size Optional size (used for SVG points) in SVG pixels. For linestrings,
  209. specify linewidth in the SVG style information
  210. \ingroup svg
  211. */
  212. template <typename Geometry>
  213. inline svg_manipulator<Geometry> svg(Geometry const& geometry, std::string const& style, int size = -1)
  214. {
  215. concept::check<Geometry const>();
  216. return svg_manipulator<Geometry>(geometry, style, size);
  217. }
  218. }} // namespace boost::geometry
  219. #endif // BOOST_GEOMETRY_IO_SVG_WRITE_SVG_HPP