// Boost.Geometry (aka GGL, Generic Geometry Library) // Copyright (c) 2009-2015 Barend Gehrels, Amsterdam, the Netherlands. // This file was modified by Oracle on 2015-2021. // Modifications copyright (c) 2015-2020, Oracle and/or its affiliates. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands. // Use, modification and distribution is subject to the Boost Software License, // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_GEOMETRY_IO_SVG_MAPPER_HPP #define BOOST_GEOMETRY_IO_SVG_MAPPER_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // Helper geometries (all points are transformed to svg-points) #include namespace boost { namespace geometry { #ifndef DOXYGEN_NO_DISPATCH namespace dispatch { template struct svg_map { BOOST_GEOMETRY_STATIC_ASSERT_FALSE( "Not or not yet implemented for this Geometry type.", GeometryTag, Geometry); }; template struct svg_map { template static inline void apply(std::ostream& stream, std::string const& style, double size, Point const& point, TransformStrategy const& strategy) { SvgPoint ipoint; geometry::transform(point, ipoint, strategy); stream << geometry::svg(ipoint, style, size) << std::endl; } }; template struct svg_map_box_seg { template static inline void apply(std::ostream& stream, std::string const& style, double size, BoxSeg1 const& box_seg, TransformStrategy const& strategy) { BoxSeg2 ibox_seg; // Fix bug in gcc compiler warning for possible uninitialization #if defined(BOOST_GCC) geometry::assign_zero(ibox_seg); #endif geometry::transform(box_seg, ibox_seg, strategy); stream << geometry::svg(ibox_seg, style, size) << std::endl; } }; template struct svg_map : svg_map_box_seg, SvgPoint> {}; template struct svg_map : svg_map_box_seg, SvgPoint> {}; template struct svg_map_range { template static inline void apply(std::ostream& stream, std::string const& style, double size, Range1 const& range, TransformStrategy const& strategy) { Range2 irange; geometry::transform(range, irange, strategy); stream << geometry::svg(irange, style, size) << std::endl; } }; template struct svg_map : svg_map_range, SvgPoint> {}; template struct svg_map : svg_map_range, SvgPoint> {}; template struct svg_map { template static inline void apply(std::ostream& stream, std::string const& style, double size, Polygon const& polygon, TransformStrategy const& strategy) { model::polygon ipoly; geometry::transform(polygon, ipoly, strategy); stream << geometry::svg(ipoly, style, size) << std::endl; } }; template struct svg_map { typedef typename single_tag_of < typename geometry::tag::type >::type stag; template static inline void apply(std::ostream& stream, std::string const& style, double size, Multi const& multi, TransformStrategy const& strategy) { for (typename boost::range_iterator::type it = boost::begin(multi); it != boost::end(multi); ++it) { svg_map < stag, typename boost::range_value::type, SvgPoint >::apply(stream, style, size, *it, strategy); } } }; template struct devarianted_svg_map { template static inline void apply(std::ostream& stream, std::string const& style, double size, Geometry const& geometry, TransformStrategy const& strategy) { svg_map < typename tag_cast < typename tag::type, multi_tag >::type, typename std::remove_const::type, SvgPoint >::apply(stream, style, size, geometry, strategy); } }; template struct devarianted_svg_map > { template struct visitor: static_visitor { std::ostream& m_os; std::string const& m_style; double m_size; TransformStrategy const& m_strategy; visitor(std::ostream& os, std::string const& style, double size, TransformStrategy const& strategy) : m_os(os) , m_style(style) , m_size(size) , m_strategy(strategy) {} template inline void operator()(Geometry const& geometry) const { devarianted_svg_map::apply(m_os, m_style, m_size, geometry, m_strategy); } }; template static inline void apply(std::ostream& stream, std::string const& style, double size, variant const& geometry, TransformStrategy const& strategy) { boost::apply_visitor(visitor(stream, style, size, strategy), geometry); } }; } // namespace dispatch #endif template inline void svg_map(std::ostream& stream, std::string const& style, double size, Geometry const& geometry, TransformStrategy const& strategy) { dispatch::devarianted_svg_map::apply(stream, style, size, geometry, strategy); } /*! \brief Helper class to create SVG maps \tparam Point Point type, for input geometries. \tparam SameScale Boolean flag indicating if horizontal and vertical scale should be the same. The default value is true \tparam SvgCoordinateType Coordinate type of SVG points. SVG is capable to use floating point coordinates. Therefore the default value is double \ingroup svg \qbk{[include reference/io/svg.qbk]} */ template < typename Point, bool SameScale = true, typename SvgCoordinateType = double > class svg_mapper : boost::noncopyable { typedef model::point svg_point_type; typedef typename geometry::select_most_precise < typename coordinate_type::type, double >::type calculation_type; typedef strategy::transform::map_transformer < calculation_type, geometry::dimension::type::value, geometry::dimension::type::value, true, SameScale > transformer_type; model::box m_bounding_box; boost::scoped_ptr m_matrix; std::ostream& m_stream; SvgCoordinateType const m_width; SvgCoordinateType const m_height; calculation_type const m_scale{1.0}; void scale_bounding_box() { if (m_scale != 1.0 && m_scale > 0) { // Zoom out (scale < 1) or zoom in (scale > 1). // The default is 0.95, giving a small margin around geometries. auto& b = m_bounding_box; auto const w = geometry::get<1, 0>(b) - geometry::get<0, 0>(b); auto const h = geometry::get<1, 1>(b) - geometry::get<0, 1>(b); auto const& m = (std::max)(w, h) * (1.0 - m_scale); geometry::set<0, 0>(b, geometry::get<0, 0>(b) - m); geometry::set<0, 1>(b, geometry::get<0, 1>(b) - m); geometry::set<1, 0>(b, geometry::get<1, 0>(b) + m); geometry::set<1, 1>(b, geometry::get<1, 1>(b) + m); } } void init_matrix() { if (! m_matrix) { scale_bounding_box(); m_matrix.reset(new transformer_type(m_bounding_box, m_width, m_height)); } } void write_header(std::string const& width_height) { m_stream << "" << std::endl << "" << std::endl << "" << std::endl; } public : /*! \brief Constructor, initializing the SVG map. Opens and initializes the SVG. Should be called explicitly. \param stream Output stream, should be a stream already open \param width Width of the SVG map (in SVG pixels) \param height Height of the SVG map (in SVG pixels) \param scale Scale factor of the automatically determined bounding box. If the factor is less than 1.0, there will be a margin around the geometries. A factor of 0.95 is often a convenient margin. If the factor is more than 1.0, the SVG map is zoomed and not all geometries will be visible. \param width_height Optional information to increase width and/or height */ svg_mapper(std::ostream& stream , SvgCoordinateType width , SvgCoordinateType height , calculation_type scale , std::string const& width_height = "width=\"100%\" height=\"100%\"") : m_stream(stream) , m_width(width) , m_height(height) , m_scale(scale) { assign_inverse(m_bounding_box); write_header(width_height); } /*! \brief Constructor, initializing the SVG map. Opens and initializes the SVG. Should be called explicitly. \param stream Output stream, should be a stream already open \param width Width of the SVG map (in SVG pixels) \param height Height of the SVG map (in SVG pixels) \param width_height Optional information to increase width and/or height */ svg_mapper(std::ostream& stream , SvgCoordinateType width , SvgCoordinateType height , std::string const& width_height = "width=\"100%\" height=\"100%\"") : m_stream(stream) , m_width(width) , m_height(height) { assign_inverse(m_bounding_box); write_header(width_height); } /*! \brief Destructor, called automatically. Closes the SVG by streaming <\/svg> */ virtual ~svg_mapper() { m_stream << "" << std::endl; } /*! \brief Adds a geometry to the transformation matrix. After doing this, the specified geometry can be mapped fully into the SVG map \tparam Geometry \tparam_geometry \param geometry \param_geometry */ template void add(Geometry const& geometry) { if (! geometry::is_empty(geometry)) { expand(m_bounding_box, return_envelope < model::box >(geometry)); } } /*! \brief Maps a geometry into the SVG map using the specified style \tparam Geometry \tparam_geometry \param geometry \param_geometry \param style String containing verbatim SVG style information \param size Optional size (used for SVG points) in SVG pixels. For linestrings, specify linewidth in the SVG style information */ template void map(Geometry const& geometry, std::string const& style, double size = -1.0) { init_matrix(); svg_map(m_stream, style, size, geometry, *m_matrix); } /*! \brief Adds a text to the SVG map \tparam TextPoint \tparam_point \param point Location of the text (in map units) \param s The text itself \param style String containing verbatim SVG style information, of the text \param offset_x Offset in SVG pixels, defaults to 0 \param offset_y Offset in SVG pixels, defaults to 0 \param lineheight Line height in SVG pixels, in case the text contains \n */ template void text(TextPoint const& point, std::string const& s, std::string const& style, double offset_x = 0.0, double offset_y = 0.0, double lineheight = 10.0) { init_matrix(); svg_point_type map_point; transform(point, map_point, *m_matrix); m_stream << "(map_point) + offset_x << "\"" << " y=\"" << get<1>(map_point) + offset_y << "\"" << ">"; if (s.find("\n") == std::string::npos) { m_stream << s; } else { // Multi-line modus std::vector splitted; boost::split(splitted, s, boost::is_any_of("\n")); for (std::vector::const_iterator it = splitted.begin(); it != splitted.end(); ++it, offset_y += lineheight) { m_stream << "(map_point) + offset_x << "\"" << " y=\"" << get<1>(map_point) + offset_y << "\"" << ">" << *it << ""; } } m_stream << "" << std::endl; } }; }} // namespace boost::geometry #endif // BOOST_GEOMETRY_IO_SVG_MAPPER_HPP