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