alaCarte Maps
Renderer for OpenStreetMap tiles
node_renderer.cpp
Go to the documentation of this file.
1 
22 /*
23  * =====================================================================================
24  *
25  * Filename: node_renderer.cpp
26  *
27  * Description: Wrapper for rendering operations of a node.
28  *
29  * =====================================================================================
30  */
31 
32 #include <boost/unordered_map.hpp>
33 #include <boost/math/constants/constants.hpp>
34 
35 #include "general/geodata.hpp"
36 #include "general/node.hpp"
37 
38 #include "server/style.hpp"
39 
42 #include "node_renderer.hpp"
43 
44 NodeRenderer::NodeRenderer(const shared_ptr<Geodata>& data,
45  NodeId nid,
46  const Style* s,
47  const cairo_matrix_t* transform)
48  : ObjectRenderer(data, s, transform)
49  , node(data->getNode(nid))
50 {
51  const FixedPoint& coord = node->getLocation();
52  location.x = coord.x;
53  location.y = coord.y;
54  cairo_matrix_transform_point(transform, &location.x, &location.y);
56 }
57 
58 void NodeRenderer::casing(cairo_t* cr)
59 {
60  // nothing to render
61  if (s->casing_width <= 0.0)
62  return;
63 
64  cairo_save(cr);
65 
66  cairo_arc(cr, location.x, location.y, s->width/2.0 + s->casing_width, 0, 2*boost::math::constants::pi<double>());
67 
68  cairo_set_source_rgba(cr, COLOR2RGBA(s->casing_color));
69 
70  cairo_fill(cr);
71  cairo_restore(cr);
72 }
73 
74 void NodeRenderer::stroke(cairo_t* cr)
75 {
76  // nothing to stroke
77  if (s->width <= 0.0)
78  return;
79 
80  cairo_save(cr);
81 
82  cairo_arc(cr, location.x, location.y, s->width/2.0, 0, 2*boost::math::constants::pi<double>());
83 
84  cairo_set_source_rgba(cr, COLOR2RGBA(s->color));
85 
86  cairo_fill(cr);
87  cairo_restore(cr);
88 }
89 
90 void NodeRenderer::label(cairo_t* cr,
91  std::list<shared_ptr<Label> >& labels,
92  AssetCache& cache)
93 {
94  // nothing to print
95  if (s->text.str().size() == 0 || s->font_size <= 0)
96  return;
97 
98  cairo_save(cr);
99 
100  cairo_set_font_size(cr, s->font_size);
101 
102  cairo_select_font_face(cr,
103  s->font_family.c_str(),
104  s->font_style == Style::STYLE_ITALIC ? CAIRO_FONT_SLANT_ITALIC : CAIRO_FONT_SLANT_NORMAL,
105  s->font_weight == Style::WEIGHT_BOLD ? CAIRO_FONT_WEIGHT_BOLD : CAIRO_FONT_WEIGHT_NORMAL
106  );
107 
108  cairo_text_extents_t textSize;
109  cairo_text_extents(cr, s->text.c_str(), &textSize);
110 
111  addLabel(labels, location + FloatPoint(0.0, s->text_offset), &textSize);
112 
113  cairo_restore(cr);
114 }
115 
116 void NodeRenderer::shield(cairo_t* cr,
117  std::list<shared_ptr<Shield> >& shields,
118  AssetCache& cache)
119 {
120  // nothing to print
121  if (s->shield_text.str().size() == 0 || s->font_size <= 0)
122  return;
123 
124  cairo_save(cr);
125 
126  cairo_set_font_size(cr, s->font_size);
127 
128  cairo_select_font_face(cr,
129  s->font_family.c_str(),
130  s->font_style == Style::STYLE_ITALIC ? CAIRO_FONT_SLANT_ITALIC : CAIRO_FONT_SLANT_NORMAL,
131  s->font_weight == Style::WEIGHT_BOLD ? CAIRO_FONT_WEIGHT_BOLD : CAIRO_FONT_WEIGHT_NORMAL
132  );
133 
134  cairo_text_extents_t textSize;
135  cairo_text_extents(cr, s->shield_text.c_str(), &textSize);
136 
137  addShield(shields, location, &textSize);
138 
139  cairo_restore(cr);
140 }
141 
142 void NodeRenderer::icon(cairo_t* cr, AssetCache& cache)
143 {
144  // path to icon not set
145  if (s->icon_image.str().size() == 0 || s->icon_width == 0.0 || s->icon_height == 0.0)
146  return;
147 
148  cairo_save(cr);
149 
150  cairo_surface_t* image = cache.getImage(s->icon_image.str());
151  double surface_width = cairo_image_surface_get_width(image);
152  double surface_height = cairo_image_surface_get_height(image);
153  double width = s->icon_width < 0 ? surface_width : s->icon_width;
154  double height = s->icon_height < 0 ? surface_height : s->icon_height;
155  double x0 = floor(location.x - width/2.0);
156  double y0 = floor(location.y - height/2.0);
157  cairo_translate(cr, x0, y0);
158  cairo_scale(cr, width / surface_width,
159  height / surface_height);
160  cairo_set_source_surface(cr, image, 0, 0);
161 
162  if (s->icon_opacity < 1.0)
163  cairo_paint_with_alpha(cr, s->icon_opacity);
164  else
165  cairo_paint(cr);
166 
167  cairo_restore(cr);
168 }
169 
MaybeCachedString shield_text
Definition: style.hpp:120
basic_rect< double > FloatRect
Definition: rect.hpp:170
void icon(cairo_t *cr, AssetCache &cache)
float casing_width
Definition: style.hpp:85
void stroke(cairo_t *cr)
float text_offset
Definition: style.hpp:91
const string & str() const
Returns the internal string.
void label(cairo_t *cr, std::list< shared_ptr< Label > > &labels, AssetCache &cache)
#define COLOR2RGBA(_X)
NodeRenderer(const shared_ptr< Geodata > &data, NodeId nid, const Style *s, const cairo_matrix_t *transform)
This file is part of alaCarte.
MaybeCachedString font_family
Definition: style.hpp:93
void casing(cairo_t *cr)
float icon_width
Definition: style.hpp:107
FontWeight font_weight
Definition: style.hpp:94
FloatPoint location
A Style stores the MapCSS properties for a single Node or Way, or a Relation of type multipolygon...
Definition: style.hpp:35
float icon_opacity
Definition: style.hpp:109
TESTABLE const FixedPoint & getLocation() const
Definition: node.cpp:43
FloatRect bounds
is set by addWayPath for ways or in transformLocation for nodes
Color color
Definition: style.hpp:78
float icon_height
Definition: style.hpp:108
float font_size
Definition: style.hpp:92
MaybeCachedString icon_image
Definition: style.hpp:106
basic_vector2< double > FloatPoint
Definition: point.hpp:145
void addLabel(std::list< shared_ptr< Label > > &labels, const FloatPoint &p, const cairo_text_extents_t *textSize) const
used by node and way renderer to place a shield.
cairo_surface_t * getImage(string path)
float width
Definition: style.hpp:84
const char * c_str() const
Returns the internal c string.
void addShield(std::list< shared_ptr< Shield > > &shields, const FloatPoint &p, const cairo_text_extents_t *textSize) const
used by node and way renderer to place a shield.
FontStyle font_style
Definition: style.hpp:95
void shield(cairo_t *cr, std::list< shared_ptr< Shield > > &shields, AssetCache &cache)
Color casing_color
Definition: style.hpp:86
MaybeCachedString text
Definition: style.hpp:88
const Style * s