All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CanvasConfigurer.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/maptools/CanvasConfigurer.cpp
22 
23  \brief A Symbology Enconding visitor that configures a given canvas based on symbolizers elements.
24 */
25 
26 // TerraLib
27 #include "../common/StringUtils.h"
28 #include "../common/STLUtils.h"
29 #include "../fe/Literal.h"
30 #include "../se/ExternalGraphic.h"
31 #include "../se/Fill.h"
32 #include "../se/Font.h"
33 #include "../se/Graphic.h"
34 #include "../se/GraphicStroke.h"
35 #include "../se/Halo.h"
36 #include "../se/LineSymbolizer.h"
37 #include "../se/PointSymbolizer.h"
38 #include "../se/PolygonSymbolizer.h"
39 #include "../se/Stroke.h"
40 #include "../se/SvgParameter.h"
41 #include "../se/Symbolizer.h"
42 #include "../se/TextSymbolizer.h"
43 #include "../xlink/SimpleLink.h"
44 #include "Canvas.h"
45 #include "CanvasConfigurer.h"
47 #include "MarkRendererManager.h"
48 #include "Utils.h"
49 
50 // STL
51 #include <cassert>
52 #include <cstdlib>
53 #include <fstream>
54 #include <vector>
55 
56 std::map<std::string, te::map::LineCapStyle> te::map::CanvasConfigurer::sm_lineCapMap;
57 std::map<std::string, te::map::LineJoinStyle> te::map::CanvasConfigurer::sm_lineJoinMap;
58 
60  : m_canvas(canvas)
61 {
62  // LineCapMap
66 
67  // LineJoinMap
71 }
72 
74 {}
75 
77 {
78  symbolizer->accept(*this);
79 }
80 
82 {
83  // no need
84 }
85 
87 {
88 // no need
89 }
90 
92 {
93 // no need
94 }
95 
97 {
98 // no need
99 }
100 
102 {
103  // Default
104  configDefaultPolygon();
105 
106  // Configuring the polygon stroke...
107  const te::se::Stroke* stroke = visited.getStroke();
108  if(stroke)
109  config(stroke, false);
110  else
111  m_canvas->setPolygonContourColor(te::color::RGBAColor(0, 0, 0, TE_TRANSPARENT)); // no stroke
112 
113  // Configuring the polygon fill...
114  const te::se::Fill* fill = visited.getFill();
115  if(fill)
116  config(fill);
117  else
118  m_canvas->setPolygonFillColor(te::color::RGBAColor(0, 0, 0, TE_TRANSPARENT)); // no fill
119 }
120 
122 {
123  // Configuring the line stroke...
124  const te::se::Stroke* stroke = visited.getStroke();
125  if(!stroke)
126  {
127  m_canvas->setLineColor(te::color::RGBAColor(0, 0, 0, TE_TRANSPARENT)); // no stroke
128  return;
129  }
130 
131  // Default
132  configDefaultLine();
133 
134  config(stroke);
135 }
136 
138 {
139  // Default
140  configDefaultPoint();
141 
142  const te::se::Graphic* graphic = visited.getGraphic();
143  if(graphic)
144  config(graphic, te::map::CanvasConfigurer::Point);
145 }
146 
148 {
149  // Default
150  configDefaultText();
151 
152  // Color
153  const te::se::Fill* fill = visited.getFill();
155  te::map::GetColor(fill, color);
156  m_canvas->setTextColor(color);
157 
158  const te::se::Font* font = visited.getFont();
159  if(font)
160  {
161  // Family
162  const te::se::SvgParameter* family = font->getFamily();
163  if(family)
164  m_canvas->setFontFamily(te::map::GetString(family));
165 
166  // Size
167  const te::se::SvgParameter* size = font->getSize();
168  if(size)
169  m_canvas->setTextPointSize(te::map::GetDouble(size));
170 
171  // Style - {normal, italic, and oblique}
172  const te::se::SvgParameter* style = font->getStyle();
173  if(style)
174  {
175  std::string value = te::map::GetString(style);
176  //TODO: m_canvas->setTextStyle(need a map <std::string, te::FontStyle>);
177  }
178 
179  // Weight - {normal, and bold}
180  const te::se::SvgParameter* weight = font->getWeight();
181  if(weight)
182  {
183  std::string value = te::map::GetString(weight);
184  //TODO: m_canvas->setTextWeight(need a map <std::string, te::WeightStyle>);
185  }
186  }
187 
188  // Halo
189  const te::se::Halo* halo = visited.getHalo();
190  if(halo)
191  {
192  m_canvas->setTextContourEnabled(true);
193 
194  // Halo Color
195  const te::se::Fill* haloFill = halo->getFill();
197  te::map::GetColor(haloFill, haloColor);
198  m_canvas->setTextContourColor(haloColor);
199 
200  // Halo Radius
201  int width = TE_SE_DEFAULT_HALO_RADIUS;
202  const te::se::ParameterValue* radius = halo->getRadius();
203  if(radius)
204  width = te::map::GetInt(radius);
205  m_canvas->setTextContourWidth(width);
206  }
207 
208  // Property that will be labeled
209  //const te::se::ParameterValue* label = visited.getLabel();
210 }
211 
213 {
214 // TODO
215 }
216 
217 void te::map::CanvasConfigurer::config(const te::se::Stroke* stroke, const bool& fromLineSymbolizer)
218 {
219  // Graphic Fill
220  const te::se::Graphic* graphicFill = stroke->getGraphicFill();
221  if(graphicFill)
222  fromLineSymbolizer ? config(graphicFill, te::map::CanvasConfigurer::Line) : config(graphicFill, te::map::CanvasConfigurer::Contour);
223  else
224  {
225  // Color
227  te::map::GetColor(stroke, color);
228  fromLineSymbolizer ? m_canvas->setLineColor(color) : m_canvas->setPolygonContourColor(color);
229  }
230 
231  // Width
232  const te::se::SvgParameter* width = stroke->getWidth();
233  if(width)
234  fromLineSymbolizer ? m_canvas->setLineWidth(te::map::GetInt(width)) : m_canvas->setPolygonContourWidth(te::map::GetInt(width));
235 
236  // LineCap
237  const te::se::SvgParameter* linecap = stroke->getLineCap();
238  if(linecap)
239  {
240  std::map<std::string, te::map::LineCapStyle>::iterator it = sm_lineCapMap.find(te::map::GetString(linecap));
241  if(it != sm_lineCapMap.end())
242  fromLineSymbolizer ? m_canvas->setLineCapStyle(it->second) : m_canvas->setPolygonContourCapStyle(it->second);
243  }
244 
245  // LineJoin
246  const te::se::SvgParameter* linejoin = stroke->getLineJoin();
247  if(linejoin)
248  {
249  std::map<std::string, te::map::LineJoinStyle>::iterator it = sm_lineJoinMap.find(te::map::GetString(linejoin));
250  if(it != sm_lineJoinMap.end())
251  fromLineSymbolizer ? m_canvas->setLineJoinStyle(it->second) : m_canvas->setPolygonContourJoinStyle(it->second);
252  }
253 
254  // Dasharray
255  const te::se::SvgParameter* dasharray = stroke->getDashArray();
256  if(dasharray)
257  {
258  std::string value = te::map::GetString(dasharray);
259  std::vector<double> pattern;
260  te::map::GetDashStyle(value, pattern);
261  fromLineSymbolizer ? m_canvas->setLineDashStyle(pattern) : m_canvas->setPolygonContourDashStyle(pattern);
262  }
263 
264  /* TODO: 1) stroke-dashoffset;
265  2) Should be verified the GraphicStroke. */
266 }
267 
269 {
270  // Graphic Fill
271  const te::se::Graphic* graphicFill = fill->getGraphicFill();
272  if(graphicFill)
273  {
274  config(graphicFill, te::map::CanvasConfigurer::Fill);
275  return;
276  }
277 
278  // Color
280  te::map::GetColor(fill, color);
281  m_canvas->setPolygonFillColor(color);
282 }
283 
285 {
286  // Gets the graphic size (it defines the height)
287  const te::se::ParameterValue* size = graphic->getSize();
288  std::size_t height = TE_SE_DEFAULT_GRAPHIC_SIZE;
289  if(size)
290  height = static_cast<std::size_t>(te::map::GetInt(size));
291 
292  // Gets the graphic rotation
293  const te::se::ParameterValue* rotation = graphic->getRotation();
294  double angle = 0.0;
295  if(rotation)
296  angle = te::map::GetDouble(rotation);
297 
298  // Gets the graphic opacity
299  int alpha = TE_OPAQUE;
300  const te::se::ParameterValue* opacity = graphic->getOpacity();
301  if(opacity)
302  alpha = (int)(te::map::GetDouble(opacity) * TE_OPAQUE);
303 
304  std::size_t width = height;
305 
306  /* TODO: Here we have a set of marks and external graphics. Need review!
307  Some questions:
308  :: Create a Graphic2ImagePatternFactory? The factory will create an image pattern that represents the composition of marks and external graphics.
309  :: Keep a reference to the dataset and make the draw here?
310  For while consider Mark or ExtrenalGraphic and use the first! */
311 
312  // The image pattern that will be used to configure the canvas
313  te::color::RGBAColor** rgba = 0;
314 
315  // Generates the image pattern
316  //const std::vector<te::se::Mark*> marks = graphic->getMarks();
317  //if(!marks.empty())
318  // rgba = te::map::MarkRendererManager::getInstance().render(marks[0], height);
319  //else
320  //{
321  // const std::vector<te::se::ExternalGraphic*> exgs = graphic->getExternalGraphics();
322  // if(!exgs.empty())
323  // rgba = te::map::ExternalGraphicRendererManager::getInstance().render(exgs[0], height, width);
324  //}
325 
326  const std::vector<te::se::ExternalGraphic*> exgs = graphic->getExternalGraphics();
327 
328  if(!exgs.empty())
329  rgba = te::map::ExternalGraphicRendererManager::getInstance().render(exgs[0], height, width);
330  else
331  {
332  const std::vector<te::se::Mark*> marks = graphic->getMarks();
333  if(!marks.empty())
334  rgba = te::map::MarkRendererManager::getInstance().render(marks[0], height);
335  }
336 
337  // Let's config the canvas!
338  switch(configStyle)
339  {
341  m_canvas->setPolygonFillColor(te::color::RGBAColor(0, 0, 0, TE_TRANSPARENT)); // It shouldn't be necessary! (Uba, Feb 2013)
342  m_canvas->setPolygonFillPattern(rgba, width, height);
343  m_canvas->setPolygonPatternRotation(angle);
344  m_canvas->setPolygonPatternOpacity(alpha);
345  break;
346 
348  m_canvas->setPolygonContourColor(te::color::RGBAColor(0, 0, 0, TE_TRANSPARENT)); // It shouldn't be necessary! (Uba, Feb 2013)
349  m_canvas->setPolygonContourPattern(rgba, width, height);
350  m_canvas->setPolygonContourPatternRotation(angle);
351  m_canvas->setPolygonContourPatternOpacity(alpha);
352  break;
353 
355  m_canvas->setLineColor(te::color::RGBAColor(0, 0, 0, TE_TRANSPARENT)); // It shouldn't be necessary! (Uba, Feb 2013)
356  m_canvas->setLinePattern(rgba, width, height);
357  m_canvas->setLinePatternRotation(angle);
358  m_canvas->setLinePatternOpacity(alpha);
359  break;
360 
362  m_canvas->setPointColor(te::color::RGBAColor(0, 0, 0, TE_TRANSPARENT)); // It shouldn't be necessary! (Uba, Feb 2013)
363  m_canvas->setPointPattern(rgba, width, height);
364  m_canvas->setPointPatternRotation(angle);
365  m_canvas->setPointPatternOpacity(alpha);
366  break;
367  }
368 
369  te::common::Free(rgba, height);
370 }
371 
373 {
374  m_canvas->setPolygonContourColor(te::color::RGBAColor(TE_SE_DEFAULT_STROKE_BASIC_COLOR, TE_OPAQUE));
375  m_canvas->setPolygonContourWidth(TE_SE_DEFAULT_STROKE_BASIC_WIDTH);
376 
377  m_canvas->setPolygonContourDashStyle(te::map::SolidLine);
378 
379  m_canvas->setPolygonContourCapStyle(te::map::ButtCap);
380  m_canvas->setPolygonContourJoinStyle(te::map::MiterJoin);
381 
382  m_canvas->setPolygonFillColor(te::color::RGBAColor(TE_SE_DEFAULT_FILL_BASIC_COLOR, TE_OPAQUE));
383 }
384 
386 {
388  m_canvas->setLineWidth(TE_SE_DEFAULT_STROKE_BASIC_WIDTH);
389 
390  m_canvas->setLineDashStyle(te::map::SolidLine);
391 
392  m_canvas->setLineCapStyle(te::map::ButtCap);
393  m_canvas->setLineJoinStyle(te::map::MiterJoin);
394 }
395 
397 {
398  m_canvas->setPointColor(te::color::RGBAColor(0, 0, 0, TE_OPAQUE));
399 }
400 
402 {
403  m_canvas->setTextColor(te::color::RGBAColor(TE_SE_DEFAULT_TEXT_COLOR, TE_OPAQUE));
404  m_canvas->setTextPointSize(TE_SE_DEFAULT_FONT_SIZE);
405 }
A LineSymbolizer is used to style a stroke along a linear geometry type, such as a string of line seg...
The RasterSymbolizer describes how to render raster/matrix-coverage data (e.g., satellite photos...
const SvgParameter * getLineJoin() const
Definition: Stroke.cpp:138
#define TE_SE_DEFAULT_HALO_RADIUS
It specifies the default halo radius (in pixels).
Definition: Config.h:72
#define TE_SE_DEFAULT_GRAPHIC_SIZE
It specifies the default graphic size.
Definition: Config.h:100
TEMAPEXPORT void GetColor(const te::se::Stroke *stroke, te::color::RGBAColor &color)
Gets the RGBA color from Stroke element.
Definition: Utils.cpp:83
const Fill * getFill() const
A Fill specifies the pattern for filling an area geometry.
Definition: Fill.h:59
TEMAPEXPORT int GetInt(const te::se::ParameterValue *param)
Gets the parameter value as integer.
Definition: Utils.cpp:122
#define TE_SE_ROUND_CAP
It specifies the value &quot;round&quot; for stroke-linecap parameter.
Definition: Config.h:128
#define TE_SE_ROUND_JOIN
It specifies the value &quot;round&quot; for stroke-linejoin parameter.
Definition: Config.h:149
const SvgParameter * getWidth() const
Definition: Stroke.cpp:133
The FeatureTypeStyle defines the styling that is to be applied to a dataset that can be viewed as a f...
void configDefaultPoint()
Configs the canvas with default values for point styles.
void visit(const te::se::Style &visited)
void configDefaultPolygon()
Configs the canvas with default values for polygon styles.
te::color::RGBAColor ** render(const te::se::Mark *mark, std::size_t size)
It generates the image pattern from the given Symbology Enconding Mark element.
const SvgParameter * getLineCap() const
Definition: Stroke.cpp:143
te::color::RGBAColor ** render(const te::se::ExternalGraphic *eg, std::size_t height, std::size_t &width)
It generates the image pattern from the given Symbology Enconding External Graphic element...
Utility functions for MapTools module.
A canvas is an abstraction of a drawing area.
Definition: Canvas.h:91
#define TE_SE_BUTT_CAP
It specifies the value &quot;butt&quot; for stroke-linecap parameter.
Definition: Config.h:121
#define TE_SE_DEFAULT_TEXT_COLOR
It specifies the default color used to show texts.
Definition: Config.h:51
void configDefaultText()
Configs the canvas with default values for text styles.
A Symbolizer describes how a feature is to appear on a map.
Definition: Symbolizer.h:80
A PolygonSymbolizer is used to draw a polygon (or other area-type geometries), including filling its ...
void config(const te::se::Symbolizer *symbolizer)
It configs the canvas based on given symbolizer.
A Stroke specifies the appearance of a linear geometry.
Definition: Stroke.h:67
const Graphic * getGraphicFill() const
Gets the GraphicFill element associate to this Fill.
Definition: Fill.cpp:52
TEMAPEXPORT double GetDouble(const te::se::ParameterValue *param)
Gets the parameter value as double.
Definition: Utils.cpp:127
The CoverageStyle defines the styling that is to be applied to a subset of Coverage data...
Definition: CoverageStyle.h:45
#define TE_SE_DEFAULT_STROKE_BASIC_WIDTH
It specifies the default width used by stroke basic (solid colors).
Definition: Config.h:93
static std::map< std::string, te::map::LineCapStyle > sm_lineCapMap
A map that associates stroke-linecap value to the correct LineCapStyle.
const SvgParameter * getSize() const
Definition: Font.cpp:100
const SvgParameter * getDashArray() const
Definition: Stroke.cpp:148
#define TE_TRANSPARENT
For an RGBA color this is the value of the alpha-channel for totally transparent. ...
Definition: Config.h:46
const SvgParameter * getWeight() const
Definition: Font.cpp:95
static std::map< std::string, te::map::LineJoinStyle > sm_lineJoinMap
A map that associates stroke-linejoin value to the correct LineJoinStyle.
TEMAPEXPORT void GetDashStyle(const std::string &dasharray, std::vector< double > &style)
Converts a dasharray pattern coded by a string to a vector of double.
Definition: Utils.cpp:227
ConfigStyle
Internal enum that indicates which style that should be configured.
const Stroke * getStroke() const
const ParameterValue * getRadius() const
Definition: Halo.cpp:49
const Font * getFont() const
const Stroke * getStroke() const
Gets the Stroke associates with the PolygonSymbolizer.
A PointSymbolizer specifies the rendering of a graphic Symbolizer at a point.
#define TE_SE_SQUARE_CAP
It specifies the value &quot;square&quot; for stroke-linecap parameter.
Definition: Config.h:135
const Halo * getHalo() const
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
A Font specifies the text font to use in a text symbolizer.
Definition: Font.h:63
const Graphic * getGraphic() const
A TextSymbolizer is used to render text labels according to various graphical parameters.
TEMAPEXPORT std::string GetString(const te::se::ParameterValue *param)
Gets the parameter value as string.
Definition: Utils.cpp:132
A Symbology Enconding visitor that configures a given canvas based on symbolizers elements...
const Fill * getFill() const
Definition: Halo.cpp:60
#define TE_SE_DEFAULT_FONT_SIZE
It specifies the default font size.
Definition: Config.h:58
#define TE_SE_DEFAULT_STROKE_BASIC_COLOR
It specifies the default color used by stroke basic (solid colors).
Definition: Config.h:86
#define TE_SE_BEVEL_JOIN
It specifies the value &quot;bevel&quot; for stroke-linejoin parameter.
Definition: Config.h:156
#define TE_SE_MITRE_JOIN
It specifies the value &quot;mitre&quot; for stroke-linejoin parameter.
Definition: Config.h:142
#define TE_SE_DEFAULT_FILL_BASIC_COLOR
It specifies the default color used by basic fill (solid colors).
Definition: Config.h:79
A Halo is a type of Fill that is applied to the backgrounds of font glyphs.
Definition: Halo.h:64
#define TE_SE_DEFAULT_HALO_COLOR
It specifies the default color used to show text halo.
Definition: Config.h:65
This is a singleton for managing all mark renderers instances available in the system.
const ParameterValue * getOpacity() const
Definition: Graphic.cpp:109
The Style defines the styling that is to be applied to a geographic dataset (vector geometries or cov...
Definition: Style.h:65
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
Definition: Config.h:39
void Free(std::vector< T * > *v)
This function can be applied to a pointer to a vector of pointers.
Definition: STLUtils.h:131
const SvgParameter * getStyle() const
Definition: Font.cpp:90
CanvasConfigurer(Canvas *canvas)
Constructor.
const ParameterValue * getSize() const
Definition: Graphic.cpp:120
const Fill * getFill() const
Gets the Fill associates with the PolygonSymbolizer.
virtual ReturnType accept(VisitorType &guest) const =0
It call the visit method from the guest object.
const Graphic * getGraphicFill() const
Gets the GraphicFill element associate to this Stroke.
Definition: Stroke.cpp:63
const ParameterValue * getRotation() const
Definition: Graphic.cpp:131
const std::vector< ExternalGraphic * > getExternalGraphics() const
Definition: Graphic.cpp:76
static ExternalGraphicRendererManager & getInstance()
It returns a reference to the singleton instance.
const SvgParameter * getFamily() const
Definition: Font.cpp:85
A Graphic is a graphic symbol with an inherent shape, color(s), and possibly size.
Definition: Graphic.h:66
A SvgParameter refers to an SVG/CSS graphical-formatting parameter.
Definition: SvgParameter.h:48
The &quot;ParameterValueType&quot; uses WFS-Filter expressions to give values for SE graphic parameters...
void configDefaultLine()
Configs the canvas with default values for line styles.
const std::vector< Mark * > getMarks() const
Definition: Graphic.cpp:98
This is a singleton for managing all external graphic renderers instances available in the system...