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 
60 std::map<std::string, te::se::Font::FontStyleType> te::map::CanvasConfigurer::sm_fontStyleMap;
61 std::map<std::string, te::se::Font::FontWeightType> te::map::CanvasConfigurer::sm_fontWeightMap;
62 
64  : m_canvas(canvas)
65 {
66  // LineCapMap
70 
71  // LineJoinMap
75 
76  // FontStyle
80 
81  // FontWeightType
87 }
88 
90 
92 {
93  symbolizer->accept(*this);
94 }
95 
97 {
98  // no need
99 }
100 
102  const te::se::FeatureTypeStyle& /*visited*/)
103 {
104 // no need
105 }
106 
108 {
109 // no need
110 }
111 
113 {
114 // no need
115 }
116 
118 {
119  // Default
121 
122  // Configuring the polygon stroke...
123  const te::se::Stroke* stroke = visited.getStroke();
124  if(stroke)
125  config(stroke, false);
126  else
128 
129  // Configuring the polygon fill...
130  const te::se::Fill* fill = visited.getFill();
131  if(fill)
132  config(fill);
133  else
135 }
136 
138 {
139  // Configuring the line stroke...
140  const te::se::Stroke* stroke = visited.getStroke();
141  if(!stroke)
142  {
143  m_canvas->setLineColor(te::color::RGBAColor(0, 0, 0, TE_TRANSPARENT)); // no stroke
144  return;
145  }
146 
147  // Default
149 
150  config(stroke);
151 }
152 
154 {
155  // Default
157 
158  const te::se::Graphic* graphic = visited.getGraphic();
159  if(graphic)
161 }
162 
164 {
165  // Default
167 
168  // Color
169  const te::se::Fill* fill = visited.getFill();
171  te::se::GetColor(fill, color);
172  m_canvas->setTextColor(color);
173 
174  const te::se::Font* font = visited.getFont();
175  if(font)
176  {
177  // Family
178  const te::se::SvgParameter* family = font->getFamily();
179  if(family)
181 
182  // Size
183  const te::se::SvgParameter* size = font->getSize();
184  if(size)
186 
187  // Style - {normal, italic, and oblique}
188  const te::se::SvgParameter* style = font->getStyle();
189  if(style)
190  {
191  std::string value = te::se::GetString(style);
193  }
194 
195  // Weight - {light, normal, demibold, bold and black}
196  const te::se::SvgParameter* weight = font->getWeight();
197  if(weight)
198  {
199  std::string value = te::se::GetString(weight);
201  }
202  }
203 
204  // Halo
205  const te::se::Halo* halo = visited.getHalo();
206  if(halo)
207  {
209 
210  // Halo Color
211  const te::se::Fill* haloFill = halo->getFill();
213  te::se::GetColor(haloFill, haloColor);
214  m_canvas->setTextContourColor(haloColor);
215 
216  // Halo Radius
217  int width = TE_SE_DEFAULT_HALO_RADIUS;
218  const te::se::ParameterValue* radius = halo->getRadius();
219  if(radius)
220  width = te::se::GetInt(radius);
222  }
223 
224  // Property that will be labeled
225  //const te::se::ParameterValue* label = visited.getLabel();
226 }
227 
229  const te::se::RasterSymbolizer& /*visited*/)
230 {
231 // TODO
232 }
233 
234 void te::map::CanvasConfigurer::config(const te::se::Stroke* stroke, const bool& fromLineSymbolizer)
235 {
236  // Graphic Fill
237  const te::se::Graphic* graphicFill = stroke->getGraphicFill();
238  if(graphicFill)
239  fromLineSymbolizer ? config(graphicFill, te::map::CanvasConfigurer::Line) : config(graphicFill, te::map::CanvasConfigurer::Contour);
240  else
241  {
242  // Color
244  te::se::GetColor(stroke, color);
245  fromLineSymbolizer ? m_canvas->setLineColor(color) : m_canvas->setPolygonContourColor(color);
246  }
247 
248  // Width
249  const te::se::SvgParameter* width = stroke->getWidth();
250  if(width)
251  fromLineSymbolizer ? m_canvas->setLineWidth(te::se::GetInt(width)) : m_canvas->setPolygonContourWidth(te::se::GetInt(width));
252 
253  // LineCap
254  const te::se::SvgParameter* linecap = stroke->getLineCap();
255  if(linecap)
256  {
257  std::map<std::string, te::map::LineCapStyle>::iterator it = sm_lineCapMap.find(te::se::GetString(linecap));
258  if(it != sm_lineCapMap.end())
259  fromLineSymbolizer ? m_canvas->setLineCapStyle(it->second) : m_canvas->setPolygonContourCapStyle(it->second);
260  }
261 
262  // LineJoin
263  const te::se::SvgParameter* linejoin = stroke->getLineJoin();
264  if(linejoin)
265  {
266  std::map<std::string, te::map::LineJoinStyle>::iterator it = sm_lineJoinMap.find(te::se::GetString(linejoin));
267  if(it != sm_lineJoinMap.end())
268  fromLineSymbolizer ? m_canvas->setLineJoinStyle(it->second) : m_canvas->setPolygonContourJoinStyle(it->second);
269  }
270 
271  // Dasharray
272  const te::se::SvgParameter* dasharray = stroke->getDashArray();
273  if(dasharray)
274  {
275  std::string value = te::se::GetString(dasharray);
276  std::vector<double> pattern;
277  te::map::GetDashStyle(value, pattern);
278  fromLineSymbolizer ? m_canvas->setLineDashStyle(pattern) : m_canvas->setPolygonContourDashStyle(pattern);
279  }
280 
281  /* TODO: 1) stroke-dashoffset;
282  2) Should be verified the GraphicStroke. */
283 }
284 
286 {
287  // Graphic Fill
288  const te::se::Graphic* graphicFill = fill->getGraphicFill();
289  if(graphicFill)
290  {
292  return;
293  }
294 
295  // Color
297  te::se::GetColor(fill, color);
299 }
300 
302 {
303  // Gets the graphic size (it defines the height)
304  const te::se::ParameterValue* size = graphic->getSize();
305  std::size_t height = TE_SE_DEFAULT_GRAPHIC_SIZE;
306  if(size)
307  height = static_cast<std::size_t>(te::se::GetInt(size));
308 
309  // Gets the graphic rotation
310  const te::se::ParameterValue* rotation = graphic->getRotation();
311  double angle = 0.0;
312  if(rotation)
313  angle = te::se::GetDouble(rotation);
314 
315  // Gets the graphic opacity
316  int alpha = TE_OPAQUE;
317  const te::se::ParameterValue* opacity = graphic->getOpacity();
318  if(opacity)
319  alpha = (int)(te::se::GetDouble(opacity) * TE_OPAQUE);
320 
321  std::size_t width = height;
322 
323  /* TODO: Here we have a set of marks and external graphics. Need review!
324  Some questions:
325  :: Create a Graphic2ImagePatternFactory? The factory will create an image pattern that represents the composition of marks and external graphics.
326  :: Keep a reference to the dataset and make the draw here?
327  For while consider Mark or ExtrenalGraphic and use the first! */
328 
329  // The image pattern that will be used to configure the canvas
330  te::color::RGBAColor** rgba = nullptr;
331 
332  // Generates the image pattern
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  //else
337  //{
338  // const std::vector<te::se::ExternalGraphic*> exgs = graphic->getExternalGraphics();
339  // if(!exgs.empty())
340  // rgba = te::map::ExternalGraphicRendererManager::getInstance().render(exgs[0], height, width);
341  //}
342 
343  const std::vector<te::se::ExternalGraphic*> exgs = graphic->getExternalGraphics();
344 
345  if(!exgs.empty())
346  rgba = te::map::ExternalGraphicRendererManager::getInstance().render(exgs[0], height, width);
347  else
348  {
349  const std::vector<te::se::Mark*> marks = graphic->getMarks();
350  if(!marks.empty())
351  rgba = te::map::MarkRendererManager::getInstance().render(marks[0], height);
352  }
353 
354  // Let's config the canvas!
355  switch(configStyle)
356  {
358  m_canvas->setPolygonFillColor(te::color::RGBAColor(0, 0, 0, TE_TRANSPARENT)); // It shouldn't be necessary! (Uba, Feb 2013)
359  m_canvas->setPolygonFillPattern(rgba, static_cast<int>(width), static_cast<int>(height));
362  break;
363 
365  m_canvas->setPolygonContourColor(te::color::RGBAColor(0, 0, 0, TE_TRANSPARENT)); // It shouldn't be necessary! (Uba, Feb 2013)
366  m_canvas->setPolygonContourPattern(rgba, static_cast<int>(width), static_cast<int>(height));
369  break;
370 
372  m_canvas->setLineColor(te::color::RGBAColor(0, 0, 0, TE_TRANSPARENT)); // It shouldn't be necessary! (Uba, Feb 2013)
373  m_canvas->setLinePattern(rgba, static_cast<int>(width), static_cast<int>(height));
376  break;
377 
379  m_canvas->setPointColor(te::color::RGBAColor(0, 0, 0, TE_TRANSPARENT)); // It shouldn't be necessary! (Uba, Feb 2013)
380  m_canvas->setPointPattern(rgba, static_cast<int>(width), static_cast<int>(height));
383  break;
384  }
385 
386  te::common::Free(rgba, height);
387 }
388 
390 {
393 
395 
398 
400 }
401 
403 {
406 
408 
411 }
412 
414 {
416 }
417 
419 {
422 }
const ParameterValue * getRotation() const
Definition: Graphic.cpp:131
A TextSymbolizer is used to render text labels according to various graphical parameters.
static std::map< std::string, te::map::LineJoinStyle > sm_lineJoinMap
A map that associates stroke-linejoin value to the correct LineJoinStyle.
virtual void setLineCapStyle(LineCapStyle style)=0
It sets the line cap style.
const Fill * getFill() const
Gets the Fill associates with the PolygonSymbolizer.
static std::map< std::string, te::se::Font::FontStyleType > sm_fontStyleMap
A map that associates FontStyleType to the correct string value.
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.
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
#define TE_SE_DEFAULT_GRAPHIC_SIZE
It specifies the default graphic size.
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...
virtual void setFontFamily(const std::string &family)=0
It sets the text font family.
A PolygonSymbolizer is used to draw a polygon (or other area-type geometries), including filling its ...
#define TE_SE_DEFAULT_STROKE_BASIC_WIDTH
It specifies the default width used by stroke basic (solid colors).
const std::vector< ExternalGraphic * > getExternalGraphics() const
Definition: Graphic.cpp:76
#define TE_SE_DEFAULT_HALO_RADIUS
It specifies the default halo radius (in pixels).
A Symbolizer describes how a feature is to appear on a map.
Definition: Symbolizer.h:80
const SvgParameter * getLineJoin() const
Definition: Stroke.cpp:138
virtual void setPolygonFillColor(const te::color::RGBAColor &color)=0
It sets the color used to fill the draw of polygon geometries.
The CoverageStyle defines the styling that is to be applied to a subset of Coverage data...
Definition: CoverageStyle.h:45
virtual void setPointPattern(te::color::RGBAColor **pattern, int ncols, int nrows)=0
It sets the point pattern.
virtual void setLineWidth(int w)=0
It sets the line width.
virtual void setPointPatternRotation(const double &angle)=0
It sets the point pattern rotation. Rotation is made ​​from the center of the pattern.
const SvgParameter * getFamily() const
Definition: Font.cpp:88
This is a singleton for managing all mark renderers instances available in the system.
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
Canvas * m_canvas
A pointer to the canvas that will be configured.
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.
virtual ReturnType accept(VisitorType &guest) const =0
It call the visit method from the guest object.
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:98
The "ParameterValueType" uses WFS-Filter expressions to give values for SE graphic parameters...
virtual void setPolygonContourPatternRotation(const double &angle)=0
It sets the polygon contour pattern rotation.
virtual void setPolygonPatternRotation(const double &angle)=0
It sets the polygon pattern rotation.
virtual void setLineJoinStyle(LineJoinStyle style)=0
It sets the line join style.
const SvgParameter * getSize() const
Definition: Font.cpp:103
#define TE_SE_BEVEL_JOIN
It specifies the value "bevel" for stroke-linejoin parameter.
virtual void setLinePattern(te::color::RGBAColor **pattern, int ncols, int nrows)=0
It sets the line pattern.
#define TE_SE_DEFAULT_FONT_SIZE
It specifies the default font size.
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.
virtual void setPolygonContourPattern(te::color::RGBAColor **pattern, int ncols, int nrows)=0
It sets the pen pattern used to draw the boundary of polygon geometries.
virtual void setPolygonFillPattern(te::color::RGBAColor **pattern, int ncols, int nrows)=0
It sets the polygon fill pattern.
static std::map< std::string, te::map::LineCapStyle > sm_lineCapMap
A map that associates stroke-linecap value to the correct LineCapStyle.
static std::map< std::string, te::se::Font::FontWeightType > sm_fontWeightMap
A map that associates FontWeightType to the correct string value.
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.
virtual void setPolygonPatternOpacity(int opacity)=0
It sets the polygon pattern opacity.
The FeatureTypeStyle defines the styling that is to be applied to a dataset that can be viewed as a f...
const Graphic * getGraphicFill() const
Gets the GraphicFill element associate to this Fill.
Definition: Fill.cpp:52
#define TE_SE_BUTT_CAP
It specifies the value "butt" for stroke-linecap parameter.
virtual void setLineDashStyle(LineDashStyle style)=0
It sets the line dash style.
A Font specifies the text font to use in a text symbolizer.
Definition: Font.h:63
virtual void setTextColor(const te::color::RGBAColor &color)=0
It sets the text drawing color.
virtual void setTextWeight(te::se::Font::FontWeightType weight)=0
It sets the text weight.
virtual void setLinePatternOpacity(int opacity)=0
It sets the line pattern opacity.
virtual void setTextContourWidth(int width)=0
It sets the text contour width.
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...
A Fill specifies the pattern for filling an area geometry.
Definition: Fill.h:59
virtual void setLinePatternRotation(const double &angle)=0
It sets the line pattern rotation. Rotation is made ​​from the center of the pattern.
virtual void setPolygonContourPatternOpacity(int opacity)=0
It sets the polygon contour pattern opacity.
#define TE_SE_DEFAULT_TEXT_COLOR
It specifies the default color used to show texts.
const Font * getFont() const
virtual void setPolygonContourWidth(int w)=0
It sets the polygon contour width.
virtual void setTextContourColor(const te::color::RGBAColor &color)=0
It sets the text contour (outline) drawing color.
const Stroke * getStroke() const
#define TE_SE_SQUARE_CAP
It specifies the value "square" for stroke-linecap parameter.
#define TE_SE_ROUND_JOIN
It specifies the value "round" for stroke-linejoin parameter.
#define TE_SE_ROUND_CAP
It specifies the value "round" for stroke-linecap parameter.
A canvas is an abstraction of a drawing area.
virtual void setPointColor(const te::color::RGBAColor &color)=0
It sets the point drawing color.
const std::vector< Mark * > getMarks() const
Definition: Graphic.cpp:98
const SvgParameter * getDashArray() const
Definition: Stroke.cpp:148
virtual void setTextContourEnabled(bool b)=0
It controls the display of the text outline.
A Stroke specifies the appearance of a linear geometry.
Definition: Stroke.h:67
#define TE_SE_MITRE_JOIN
It specifies the value "mitre" for stroke-linejoin parameter.
virtual void setPolygonContourColor(const te::color::RGBAColor &color)=0
It sets the pen color used to draw the boundary of polygon geometries.
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.
#define TE_SE_DEFAULT_FILL_BASIC_COLOR
It specifies the default color used by basic fill (solid colors).
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:93
A LineSymbolizer is used to style a stroke along a linear geometry type, such as a string of line seg...
#define TE_SE_DEFAULT_STROKE_BASIC_COLOR
It specifies the default color used by stroke basic (solid colors).
virtual void setTextStyle(te::se::Font::FontStyleType style)=0
It sets the text style.
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.
virtual void setPolygonContourJoinStyle(LineJoinStyle style)=0
It sets the polygon contour join style.
virtual void setLineColor(const te::color::RGBAColor &color)=0
It sets the pen color used to draw line geometries.
virtual void setPointPatternOpacity(int opacity)=0
It sets the point pattern opacity.
~CanvasConfigurer()
Destructor.
void config(const te::se::Symbolizer *symbolizer)
It configs the canvas based on given symbolizer.
virtual void setPolygonContourDashStyle(LineDashStyle style)=0
It sets the polygon contour dash style.
CanvasConfigurer(Canvas *canvas)
Constructor.
void visit(const te::se::Style &visited)
Utility functions for MapTools module.
#define TE_TRANSPARENT
For an RGBA color this is the value of the alpha-channel for totally transparent. ...
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.
#define TE_SE_DEFAULT_HALO_COLOR
It specifies the default color used to show text halo.
const SvgParameter * getWidth() const
Definition: Stroke.cpp:133
A Symbology Enconding visitor that configures a given canvas based on symbolizers elements...
virtual void setTextPointSize(double size)=0
It sets the text point Size.
const ParameterValue * getOpacity() const
Definition: Graphic.cpp:109
virtual void setPolygonContourCapStyle(LineCapStyle style)=0
It sets the polygon contour cap style.