/***************************************************************************** * * This file is part of Mapnik (c++ mapping toolkit) * * Copyright (C) 2006 Artem Pavlenko * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * *****************************************************************************/ //$Id$ // mapnik #include #include #include #include #include #include #include #include #include #include // agg #define AGG_RENDERING_BUFFER row_ptr_cache #include "agg_rendering_buffer.h" #include "agg_pixfmt_rgba.h" #include "agg_rasterizer_scanline_aa.h" #include "agg_basics.h" #include "agg_scanline_p.h" #include "agg_scanline_u.h" #include "agg_renderer_scanline.h" #include "agg_path_storage.h" #include "agg_span_allocator.h" #include "agg_span_pattern_rgba.h" #include "agg_image_accessors.h" #include "agg_conv_stroke.h" #include "agg_conv_dash.h" #include "agg_conv_contour.h" #include "agg_conv_clip_polyline.h" #include "agg_vcgen_stroke.h" #include "agg_conv_adaptor_vcgen.h" #include "agg_conv_smooth_poly1.h" #include "agg_conv_marker.h" #include "agg_vcgen_markers_term.h" #include "agg_renderer_outline_aa.h" #include "agg_rasterizer_outline_aa.h" #include "agg_rasterizer_outline.h" #include "agg_renderer_outline_image.h" #include "agg_span_allocator.h" #include "agg_span_pattern_rgba.h" #include "agg_renderer_scanline.h" #include "agg_pattern_filters_rgba.h" #include "agg_renderer_outline_image.h" #include "agg_vpgen_clip_polyline.h" #include "agg_arrowhead.h" // boost #include #include // stl #ifdef MAPNIK_DEBUG #include #endif #include namespace mapnik { class pattern_source : private boost::noncopyable { public: pattern_source(image_data_32 const& pattern) : pattern_(pattern) {} unsigned int width() const { return pattern_.width(); } unsigned int height() const { return pattern_.height(); } agg::rgba8 pixel(int x, int y) const { unsigned c = pattern_(x,y); return agg::rgba8(c & 0xff, (c >> 8) & 0xff, (c >> 16) & 0xff, (c >> 24) & 0xff); } private: image_data_32 const& pattern_; }; template agg_renderer::agg_renderer(Map const& m, T & pixmap, double scale_factor, unsigned offset_x, unsigned offset_y) : feature_style_processor(m), pixmap_(pixmap), width_(pixmap_.width()), height_(pixmap_.height()), scale_factor_(scale_factor), t_(m.getWidth(),m.getHeight(),m.getCurrentExtent(),offset_x,offset_y), font_engine_(), font_manager_(font_engine_), detector_(box2d(-m.buffer_size(), -m.buffer_size(), m.getWidth() + m.buffer_size() ,m.getHeight() + m.buffer_size())), ras_ptr(new rasterizer) { boost::optional bg = m.background(); if (bg) pixmap_.set_background(*bg); #ifdef MAPNIK_DEBUG std::clog << "scale=" << m.scale() << "\n"; #endif } template agg_renderer::~agg_renderer() {} template void agg_renderer::start_map_processing(Map const& map) { #ifdef MAPNIK_DEBUG std::clog << "start map processing bbox=" << map.getCurrentExtent() << "\n"; #endif ras_ptr->clip_box(0,0,width_,height_); } template void agg_renderer::end_map_processing(Map const& ) { #ifdef MAPNIK_DEBUG std::clog << "end map processing\n"; #endif } template void agg_renderer::start_layer_processing(layer const& lay) { #ifdef MAPNIK_DEBUG std::clog << "start layer processing : " << lay.name() << "\n"; std::clog << "datasource = " << lay.datasource().get() << "\n"; #endif if (lay.clear_label_cache()) { detector_.clear(); } } template void agg_renderer::end_layer_processing(layer const&) { #ifdef MAPNIK_DEBUG std::clog << "end layer processing\n"; #endif } template void agg_renderer::process(polygon_symbolizer const& sym, Feature const& feature, proj_transform const& prj_trans) { typedef coord_transform2 path_type; typedef agg::renderer_base ren_base; typedef agg::renderer_scanline_aa_solid renderer; color const& fill_ = sym.get_fill(); agg::scanline_u8 sl; agg::rendering_buffer buf(pixmap_.raw_data(),width_,height_, width_ * 4); agg::pixfmt_rgba32_plain pixf(buf); ren_base renb(pixf); unsigned r=fill_.red(); unsigned g=fill_.green(); unsigned b=fill_.blue(); unsigned a=fill_.alpha(); renderer ren(renb); ras_ptr->reset(); ras_ptr->gamma(agg::gamma_linear(0.0, sym.get_gamma())); for (unsigned i=0;i 2) { path_type path(t_,geom,prj_trans); ras_ptr->add_path(path); } } ren.color(agg::rgba8(r, g, b, int(a * sym.get_opacity()))); agg::render_scanlines(*ras_ptr, sl, ren); } typedef boost::tuple segment_t; bool y_order(segment_t const& first,segment_t const& second) { double miny0 = std::min(first.get<1>(),first.get<3>()); double miny1 = std::min(second.get<1>(),second.get<3>()); return miny0 > miny1; } template void agg_renderer::process(building_symbolizer const& sym, Feature const& feature, proj_transform const& prj_trans) { typedef coord_transform2 path_type; typedef coord_transform3 path_type_roof; typedef agg::renderer_base ren_base; typedef agg::renderer_scanline_aa_solid renderer; agg::rendering_buffer buf(pixmap_.raw_data(),width_,height_, width_ * 4); agg::pixfmt_rgba32_plain pixf(buf); ren_base renb(pixf); color const& fill_ = sym.get_fill(); unsigned r=fill_.red(); unsigned g=fill_.green(); unsigned b=fill_.blue(); unsigned a=fill_.alpha(); renderer ren(renb); agg::scanline_u8 sl; ras_ptr->reset(); ras_ptr->gamma(agg::gamma_linear()); double height = 0.7071 * sym.height(); // height in meters for (unsigned i=0;i 2) { boost::scoped_ptr frame(new line_string_impl); boost::scoped_ptr roof(new polygon_impl); std::deque face_segments; double x0(0); double y0(0); unsigned cm = geom.vertex(&x0,&y0); for (unsigned j=1;jmove_to(x,y); } else if (cm == SEG_LINETO) { frame->line_to(x,y); face_segments.push_back(segment_t(x0,y0,x,y)); } x0 = x; y0 = y; } std::sort(face_segments.begin(),face_segments.end(), y_order); std::deque::const_iterator itr=face_segments.begin(); for (;itr!=face_segments.end();++itr) { boost::scoped_ptr faces(new polygon_impl); faces->move_to(itr->get<0>(),itr->get<1>()); faces->line_to(itr->get<2>(),itr->get<3>()); faces->line_to(itr->get<2>(),itr->get<3>() + height); faces->line_to(itr->get<0>(),itr->get<1>() + height); path_type faces_path (t_,*faces,prj_trans); ras_ptr->add_path(faces_path); ren.color(agg::rgba8(int(r*0.8), int(g*0.8), int(b*0.8), int(a * sym.get_opacity()))); agg::render_scanlines(*ras_ptr, sl, ren); ras_ptr->reset(); frame->move_to(itr->get<0>(),itr->get<1>()); frame->line_to(itr->get<0>(),itr->get<1>()+height); } geom.rewind(0); for (unsigned j=0;jmove_to(x,y+height); roof->move_to(x,y+height); } else if (cm == SEG_LINETO) { frame->line_to(x,y+height); roof->line_to(x,y+height); } } path_type path(t_,*frame,prj_trans); agg::conv_stroke stroke(path); ras_ptr->add_path(stroke); ren.color(agg::rgba8(r * 0.8, g * 0.8 , b * 0.8, int(255 * sym.get_opacity()))); agg::render_scanlines(*ras_ptr, sl, ren); ras_ptr->reset(); path_type roof_path (t_,*roof,prj_trans); ras_ptr->add_path(roof_path); ren.color(agg::rgba8(r, g, b, int(a * sym.get_opacity()))); agg::render_scanlines(*ras_ptr, sl, ren); } } } template void agg_renderer::process(line_symbolizer const& sym, Feature const& feature, proj_transform const& prj_trans) { typedef agg::renderer_base ren_base; typedef coord_transform2 path_type; typedef agg::renderer_outline_aa renderer_oaa; typedef agg::rasterizer_outline_aa rasterizer_outline_aa; typedef agg::renderer_scanline_aa_solid renderer; agg::rendering_buffer buf(pixmap_.raw_data(),width_,height_, width_ * 4); agg::pixfmt_rgba32_plain pixf(buf); ren_base renb(pixf); stroke const& stroke_ = sym.get_stroke(); color const& col = stroke_.get_color(); unsigned r=col.red(); unsigned g=col.green(); unsigned b=col.blue(); unsigned a=col.alpha(); renderer ren(renb); ras_ptr->reset(); ras_ptr->gamma(agg::gamma_linear()); agg::scanline_p8 sl; for (unsigned i=0;i 1) { path_type path(t_,geom,prj_trans); if (stroke_.has_dash()) { agg::conv_dash dash(path); dash_array const& d = stroke_.get_dash_array(); dash_array::const_iterator itr = d.begin(); dash_array::const_iterator end = d.end(); for (;itr != end;++itr) { dash.add_dash(itr->first * scale_factor_, itr->second * scale_factor_); } agg::conv_stroke > stroke(dash); line_join_e join=stroke_.get_line_join(); if ( join == MITER_JOIN) stroke.generator().line_join(agg::miter_join); else if( join == MITER_REVERT_JOIN) stroke.generator().line_join(agg::miter_join); else if( join == ROUND_JOIN) stroke.generator().line_join(agg::round_join); else stroke.generator().line_join(agg::bevel_join); line_cap_e cap=stroke_.get_line_cap(); if (cap == BUTT_CAP) stroke.generator().line_cap(agg::butt_cap); else if (cap == SQUARE_CAP) stroke.generator().line_cap(agg::square_cap); else stroke.generator().line_cap(agg::round_cap); stroke.generator().miter_limit(4.0); stroke.generator().width(stroke_.get_width() * scale_factor_); ras_ptr->add_path(stroke); } else { agg::conv_stroke stroke(path); line_join_e join=stroke_.get_line_join(); if ( join == MITER_JOIN) stroke.generator().line_join(agg::miter_join); else if( join == MITER_REVERT_JOIN) stroke.generator().line_join(agg::miter_join); else if( join == ROUND_JOIN) stroke.generator().line_join(agg::round_join); else stroke.generator().line_join(agg::bevel_join); line_cap_e cap=stroke_.get_line_cap(); if (cap == BUTT_CAP) stroke.generator().line_cap(agg::butt_cap); else if (cap == SQUARE_CAP) stroke.generator().line_cap(agg::square_cap); else stroke.generator().line_cap(agg::round_cap); stroke.generator().miter_limit(4.0); stroke.generator().width(stroke_.get_width() * scale_factor_); ras_ptr->add_path(stroke); } } } ren.color(agg::rgba8(r, g, b, int(a*stroke_.get_opacity()))); agg::render_scanlines(*ras_ptr, sl, ren); } template void agg_renderer::process(line_pattern_symbolizer const& sym, Feature const& feature, proj_transform const& prj_trans) { typedef coord_transform2 path_type; typedef agg::line_image_pattern pattern_type; typedef agg::renderer_base renderer_base; typedef agg::renderer_outline_image renderer_type; typedef agg::rasterizer_outline_aa rasterizer_type; agg::rendering_buffer buf(pixmap_.raw_data(),width_,height_, width_ * 4); agg::pixfmt_rgba32_plain pixf(buf); std::string filename = path_processor_type::evaluate( *sym.get_filename(), feature); boost::optional pat = image_cache::instance()->find(filename,true); if (!pat) return; renderer_base ren_base(pixf); agg::pattern_filter_bilinear_rgba8 filter; pattern_source source(*(*pat)); pattern_type pattern (filter,source); renderer_type ren(ren_base, pattern); ren.clip_box(0,0,width_,height_); rasterizer_type ras(ren); for (unsigned i=0;i 1) { path_type path(t_,geom,prj_trans); ras.add_path(path); } } } template void agg_renderer::process(polygon_pattern_symbolizer const& sym, Feature const& feature, proj_transform const& prj_trans) { typedef coord_transform2 path_type; typedef agg::renderer_base ren_base; typedef agg::wrap_mode_repeat wrap_x_type; typedef agg::wrap_mode_repeat wrap_y_type; typedef agg::pixfmt_alpha_blend_rgba, agg::pixel32_type> rendering_buffer; typedef agg::image_accessor_wrap img_source_type; typedef agg::span_pattern_rgba span_gen_type; typedef agg::renderer_scanline_aa, span_gen_type> renderer_type; agg::rendering_buffer buf(pixmap_.raw_data(),width_,height_, width_ * 4); agg::pixfmt_rgba32_plain pixf(buf); ren_base renb(pixf); agg::scanline_u8 sl; ras_ptr->reset(); ras_ptr->gamma(agg::gamma_linear()); std::string filename = path_processor_type::evaluate( *sym.get_filename(), feature); boost::optional pat = image_cache::instance()->find(filename,true); if (!pat) return; unsigned w=(*pat)->width(); unsigned h=(*pat)->height(); agg::row_accessor pattern_rbuf((agg::int8u*)(*pat)->getBytes(),w,h,w*4); agg::span_allocator sa; agg::pixfmt_alpha_blend_rgba, agg::pixel32_type> pixf_pattern(pattern_rbuf); img_source_type img_src(pixf_pattern); double x0=0,y0=0; unsigned num_geometries = feature.num_geometries(); if (num_geometries>0) { path_type path(t_,feature.get_geometry(0),prj_trans); path.vertex(&x0,&y0); } unsigned offset_x = unsigned(width_-x0); unsigned offset_y = unsigned(height_-y0); span_gen_type sg(img_src, offset_x, offset_y); renderer_type rp(renb,sa, sg); for (unsigned i=0;i 2) { path_type path(t_,geom,prj_trans); ras_ptr->add_path(path); } } agg::render_scanlines(*ras_ptr, sl, rp); } template void agg_renderer::process(raster_symbolizer const& sym, Feature const& feature, proj_transform const& prj_trans) { raster_ptr const& raster=feature.get_raster(); if (raster) { // If there's a colorizer defined, use it to color the raster in-place raster_colorizer_ptr colorizer = sym.get_colorizer(); if (colorizer) colorizer->colorize(raster); box2d ext=t_.forward(raster->ext_); int start_x = rint(ext.minx()); int start_y = rint(ext.miny()); int raster_width = rint(ext.width()); int raster_height = rint(ext.height()); int end_x = start_x + raster_width; int end_y = start_y + raster_height; double err_offs_x = (ext.minx()-start_x + ext.maxx()-end_x)/2; double err_offs_y = (ext.miny()-start_y + ext.maxy()-end_y)/2; if ( raster_width > 0 && raster_height > 0) { image_data_32 target(raster_width,raster_height); if (sym.get_scaling() == "fast") { scale_image(target,raster->data_); } else if (sym.get_scaling() == "bilinear"){ scale_image_bilinear(target,raster->data_, err_offs_x, err_offs_y); } else if (sym.get_scaling() == "bilinear8"){ scale_image_bilinear8(target,raster->data_, err_offs_x, err_offs_y); } else { scale_image(target,raster->data_); } if (sym.get_mode() == "normal"){ if (sym.get_opacity() == 1.0) { pixmap_.set_rectangle(start_x,start_y,target); } else { pixmap_.set_rectangle_alpha2(target,start_x,start_y, sym.get_opacity()); } } else if (sym.get_mode() == "grain_merge"){ pixmap_.template merge_rectangle (target,start_x,start_y, sym.get_opacity()); } else if (sym.get_mode() == "grain_merge2"){ pixmap_.template merge_rectangle (target,start_x,start_y, sym.get_opacity()); } else if (sym.get_mode() == "multiply"){ pixmap_.template merge_rectangle (target,start_x,start_y, sym.get_opacity()); } else if (sym.get_mode() == "multiply2"){ pixmap_.template merge_rectangle (target,start_x,start_y, sym.get_opacity()); } else if (sym.get_mode() == "divide"){ pixmap_.template merge_rectangle (target,start_x,start_y, sym.get_opacity()); } else if (sym.get_mode() == "divide2"){ pixmap_.template merge_rectangle (target,start_x,start_y, sym.get_opacity()); } else if (sym.get_mode() == "screen"){ pixmap_.template merge_rectangle (target,start_x,start_y, sym.get_opacity()); } else if (sym.get_mode() == "hard_light"){ pixmap_.template merge_rectangle (target,start_x,start_y, sym.get_opacity()); } else { if (sym.get_opacity() == 1.0){ pixmap_.set_rectangle(start_x,start_y,target); } else { pixmap_.set_rectangle_alpha2(target,start_x,start_y, sym.get_opacity()); } } // TODO: other modes? (add,diff,sub,...) } } } template void agg_renderer::process(text_symbolizer const& sym, Feature const& feature, proj_transform const& prj_trans) { typedef coord_transform2 path_type; expression_ptr name_expr = sym.get_name(); if (!name_expr) return; value_type result = boost::apply_visitor(evaluate(feature),*name_expr); UnicodeString text = result.to_unicode(); if ( sym.get_text_convert() == TOUPPER) { text = text.toUpper(); } else if ( sym.get_text_convert() == TOLOWER) { text = text.toLower(); } if ( text.length() > 0 ) { color const& fill = sym.get_fill(); face_set_ptr faces; if (sym.get_fontset().size() > 0) { faces = font_manager_.get_face_set(sym.get_fontset()); } else { faces = font_manager_.get_face_set(sym.get_face_name()); } if (faces->size() > 0) { text_renderer ren(pixmap_, faces); ren.set_pixel_size(sym.get_text_size() * scale_factor_); ren.set_fill(fill); ren.set_halo_fill(sym.get_halo_fill()); ren.set_halo_radius(sym.get_halo_radius()); ren.set_opacity(sym.get_opacity()); placement_finder finder(detector_); string_info info(text); faces->get_string_info(info); unsigned num_geom = feature.num_geometries(); for (unsigned i=0;i 0) // don't bother with empty geometries { placement text_placement(info,sym); text_placement.avoid_edges = sym.get_avoid_edges(); if (sym.get_label_placement() == POINT_PLACEMENT) { double label_x, label_y, z=0.0; geom.label_position(&label_x, &label_y); prj_trans.backward(label_x,label_y, z); t_.forward(&label_x,&label_y); double angle = 0.0; expression_ptr angle_expr = sym.get_orientation(); if (angle_expr) { // apply rotation value_type result = boost::apply_visitor(evaluate(feature),*angle_expr); angle = result.to_double(); } finder.find_point_placement(text_placement,label_x,label_y, angle, sym.get_vertical_alignment(),sym.get_line_spacing(), sym.get_character_spacing(),sym.get_horizontal_alignment(),sym.get_justify_alignment()); finder.update_detector(text_placement); } else if ( geom.num_points() > 1 && sym.get_label_placement() == LINE_PLACEMENT) { path_type path(t_,geom,prj_trans); finder.find_line_placements(text_placement,path); } for (unsigned int ii = 0; ii < text_placement.placements.size(); ++ii) { double x = text_placement.placements[ii].starting_x; double y = text_placement.placements[ii].starting_y; box2d dim = ren.prepare_glyphs(&text_placement.placements[ii]); ren.render(x,y); } } } } else { throw config_error("Unable to find specified font face '" + sym.get_face_name() + "'"); } } } template void agg_renderer::process(glyph_symbolizer const& sym, Feature const& feature, proj_transform const& prj_trans) { face_set_ptr faces = font_manager_.get_face_set(sym.get_face_name()); if (faces->size() > 0) { // Get x and y from geometry and translate to pixmap coords. double x, y, z=0.0; feature.get_geometry(0).label_position(&x, &y); prj_trans.backward(x,y,z); t_.forward(&x, &y); text_renderer ren(pixmap_, faces); // set fill and halo colors color fill = sym.eval_color(feature); ren.set_fill(fill); if (fill != color("transparent")) { ren.set_halo_fill(sym.get_halo_fill()); ren.set_halo_radius(sym.get_halo_radius()); } // set font size unsigned size = sym.eval_size(feature); ren.set_pixel_size(size * scale_factor_); faces->set_pixel_sizes(size * scale_factor_); // Get and render text path // text_path_ptr path = sym.get_text_path(faces, feature); // apply displacement position pos = sym.get_displacement(); double dx = boost::get<0>(pos); double dy = boost::get<1>(pos); path->starting_x = x = x+dx; path->starting_y = y = y+dy; // Prepare glyphs to set internal state and calculate the marker's // final box so we can check for a valid placement box2d dim = ren.prepare_glyphs(path.get()); double bsize = (dim.width()>dim.height()?dim.width():dim.height())/2; box2d ext( floor(x-bsize), floor(y-bsize), ceil(x+bsize), ceil(y+bsize) ); if ((sym.get_allow_overlap() || detector_.has_placement(ext)) && (!sym.get_avoid_edges() || detector_.extent().contains(ext))) { // Placement is valid, render glyph and update detector. ren.render(x, y); detector_.insert(ext); } } else { throw config_error( "Unable to find specified font face in GlyphSymbolizer" ); } } template class agg_renderer; }