alaCarte Maps
Renderer for OpenStreetMap tiles
mapcss_grammar.cpp
Go to the documentation of this file.
1 
23 #include <boost/spirit/include/qi.hpp>
24 #include <boost/spirit/include/phoenix_core.hpp>
25 #include <boost/spirit/include/phoenix_operator.hpp>
26 #include <boost/spirit/include/phoenix_fusion.hpp>
27 #include <boost/spirit/include/phoenix_object.hpp>
28 #include <boost/spirit/include/phoenix_stl.hpp>
29 #include <boost/spirit/include/phoenix_bind.hpp>
30 #include <boost/spirit/include/phoenix.hpp>
31 
32 #include "server/eval/eval.hpp"
33 #include "server/eval/stnode.hpp"
34 
38 
39 #include "server/stylesheet.hpp"
40 #include "server/style.hpp"
41 
42 
43 
44 template<typename Type>
46  : public AttributeCreator
47 {
48 public:
49  typedef shared_ptr<eval::Eval<Type> > StyleTemplate::*attr_member;
50 
52  : member(member)
53  {
54  assert(member);
55  }
56 
57  void addAttribute(const shared_ptr<StyleTemplate>& styleTemplate, const string& specifier, const shared_ptr<ParserLogger>& logger, const ParseInfo& info)
58  {
59  assert(styleTemplate);
60  (styleTemplate.get()->*member) = boost::make_shared< eval::Eval<Type> >(specifier, logger, info);
61  }
62 
63 private:
65 };
66 
67 template<typename Type>
68 shared_ptr< SpecificAttributeCreator<Type> > CreateAttribute(shared_ptr<eval::Eval<Type> > StyleTemplate::*attr)
69 {
70  return boost::make_shared< SpecificAttributeCreator<Type> >(attr);
71 }
72 
74 {
75  this->add
92 
93  ("text-halo-color", CreateAttribute(&StyleTemplate::text_halo_color))
94  ("text-halo-radius", CreateAttribute(&StyleTemplate::text_halo_radius))
95 
100 
101  ("shield-color", CreateAttribute(&StyleTemplate::shield_color))
102  ("shield-opacity", CreateAttribute(&StyleTemplate::shield_opacity))
103  ("shield-frame-color", CreateAttribute(&StyleTemplate::shield_frame_color))
104  ("shield-frame-width", CreateAttribute(&StyleTemplate::shield_frame_width))
105  ("shield-casing-color", CreateAttribute(&StyleTemplate::shield_casing_color))
106  ("shield-casing-width", CreateAttribute(&StyleTemplate::shield_casing_width))
108  ("shield-image", CreateAttribute(&StyleTemplate::shield_image))
109  ("shield-shape", CreateAttribute(&StyleTemplate::shield_shape))
110 
113  ("casing-linecap", CreateAttribute(&StyleTemplate::casing_linecap))
114  ("casing-linejoin", CreateAttribute(&StyleTemplate::casing_linejoin))
115 
117  ("casing-dashes", CreateAttribute(&StyleTemplate::casing_dashes))
118  ;
119 }
120 
121 
122 
123 
124 
125 
126 
127 
134 template<typename T>
135 void AppendVector(std::vector<T>& target, const std::vector<T>& append)
136 {
137  target.insert(target.end(), append.begin(), append.end());
138 }
139 
146  : MapCSSGrammar::base_type(rule_stylesheet, "MapCSS-MainGrammar")
147 {
148  using namespace qi::labels;
149  using qi::uint_;
150  auto& _1 = qi::_1;
151 
152  rule_tag %= +qi::char_("a-zA-Z_0-9:-");
153  rule_value %= +qi::char_("a-zA-Z_0-9./-") | ('\"' >> +(qi::char_ - '\"') >> '\"') | ('\'' >> +(qi::char_ - '\'') >> '\'');
154 
155 
156  /******************************* Attributes *************************************/
157 
158  attribute_name = +qi::char_("a-zA-Z_0-9-");
159  rule_specifier = ('\"' > +(qi::char_ - '\"') > '\"' ) | +(qi::char_ - ';' - '}');
160  rule_styleset = qi::eps[
161  _val = phx::construct<StylePtr>(phx::new_<StyleTemplate>())
162  ]
163  >> -(
164  (
165  attributeType_[_a = _1]
166  > ':' > pip::pinfo[_b = _1]
167  > rule_specifier[phx::bind(&MapCssParser::addAttributeToTemplate, &parser, _val, _a, _1, _b)]
168  ) | (
170  > ':' > rule_specifier
171  )
172  ) % ';'
173  >> -qi::lit(';');
174 
175  /******************************* Selectors *********************************/
176 
177  //rule_condition = (rule_tag > BinaryTypes > rule_string) | (UnaryTypes > rule_tag) | rule_tag;
178 
179  rule_zoom = "|z" > qi::eps[_a = ALAC_ZOOM_BOTTOM, _b = ALAC_ZOOM_TOP]
180  > ((-uint_[_a = _1] >> '-' >> -uint_[_b = _1])
181  | (uint_[_a = _b = _1]))
182  >> qi::eps[_val = phx::construct<Zoom>(_a,_b)];
183 
184 
185  rule_binary_condition %= rule_tag >> binaryOperator_ >> rule_value;// >> binaryOperator_ >> chs::string;
187 
188  //rule_class = (lit(':') | lit('.')) > +chs::alpha;
189  rule_subselector = selectorObject_[phx::at_c<0>(_val) = _1]
190  >> -rule_zoom[phx::at_c<1>(_val) = _1]
191  >> *(
192  '['
194  [
195  phx::push_back(phx::at_c<2>(_val), _1)
196  ]
197  > ']'
198  )
199  //>> -( (lit('!') > rule_class) | rule_class)
200  ;
201 
202  rule_selector = (
203  +rule_subselector[phx::push_back(_a, _1)]
204  )
205  [_val = phx::bind(&MapCssParser::createSelectorChain, &parser, _a)];
206 
207 
208  /******************************* Stylesheet *********************************/
209  rule_cssrule = (rule_selector[phx::push_back(_val, _1)] % ',')
210  > '{'
211  > rule_styleset[phx::bind(&MapCssParser::applyStyleToRules, &parser, _val, _1)]
212  > '}';
213 
214  rule_stylesheet = (
215  *(
217  [
218  phx::bind(&AppendVector<RulePtr>, _a, _1)
219  ]
220  | (
221  qi::lit("canvas")
222  > '{'
223  > -rule_styleset
224  [
225  _b = _1
226  ]
227  > '}'
228  )
229  )
230  )
231  [
232  qi::_val = phx::construct<StylesheetPtr>(phx::new_<Stylesheet>(phx::ref(parser.geodata), _a, _b))
233  ];
234 }
shared_ptr< eval::Eval< float > > icon_opacity
shared_ptr< eval::Eval< Style::LineJoin > > linejoin
shared_ptr< eval::Eval< float > > shield_opacity
shared_ptr< eval::Eval< Color > > color
qi::rule< ItType, StylePtr(), qi::locals< shared_ptr< AttributeCreator >, ParseInfo >, Skipper > rule_styleset
Rule to parse a styleset of a rule.
shared_ptr< eval::Eval< MaybeCachedString > > text
void AppendVector(std::vector< T > &target, const std::vector< T > &append)
Appends a vector to another.
qi::rule< ItType, Zoom(), qi::locals< int, int >, Skipper > rule_zoom
Rule to parse a zoom range.
shared_ptr< eval::Eval< Style::FontWeight > > font_weight
A StyleTemplate is the "raw" / "unevaluated" form of a Style specified by a single MapCSS rule...
shared_ptr< eval::Eval< Type > > StyleTemplate::* attr_member
The grammar for the mapcss format.
qi::rule< ItType, std::vector< RulePtr >), Skipper > rule_cssrule
Rule to parse one rule. Returns a list of rules, because selectors can be splited.
shared_ptr< eval::Eval< float > > casing_width
shared_ptr< Geodata > geodata
The geodata used to inject them in the rules.
shared_ptr< eval::Eval< float > > text_halo_radius
This file is part of alaCarte.
shared_ptr< eval::Eval< Style::LineCap > > linecap
shared_ptr< eval::Eval< float > > font_size
shared_ptr< eval::Eval< float > > shield_frame_width
AttributeTypes attributeType_
The symbol parser for style attributes.
void addAttribute(const shared_ptr< StyleTemplate > &styleTemplate, const string &specifier, const shared_ptr< ParserLogger > &logger, const ParseInfo &info)
shared_ptr< eval::Eval< Style::LineJoin > > casing_linejoin
shared_ptr< eval::Eval< float > > shield_casing_width
qi::rule< ItType, string(), Skipper > rule_specifier
Rule to parse the specifier.
shared_ptr< SpecificAttributeCreator< Type > > CreateAttribute(shared_ptr< eval::Eval< Type > > StyleTemplate::*attr)
shared_ptr< eval::Eval< Color > > shield_frame_color
qi::rule< ItType, string()> rule_tag
Rule to parse a tag.
shared_ptr< eval::Eval< std::vector< double > > > dashes
shared_ptr< eval::Eval< Color > > text_color
SpecificAttributeCreator(attr_member member)
shared_ptr< eval::Eval< Color > > casing_color
ObjectTypes selectorObject_
The symbol parser for objects.
qi::rule< ItType, RulePtr(), qi::locals< std::vector< SelectorItem > >, Skipper > rule_selector
Rule to parse a selector.
RulePtr createSelectorChain(const std::vector< SelectorItem > &items)
Creates a chain of selectors from given subselectors.
shared_ptr< eval::Eval< float > > width
shared_ptr< eval::Eval< MaybeCachedString > > shield_text
qi::rule< ItType, string()> rule_value
Rule to parse a value.
shared_ptr< eval::Eval< float > > icon_height
shared_ptr< eval::Eval< MaybeCachedString > > fill_image
void addAttributeToTemplate(StylePtr &style, const shared_ptr< AttributeCreator > &attrType, const string &specifier, const ParseInfo &info)
Converts a given string into a text position.
qi::rule< ItType, SelectorItem(), Skipper > rule_subselector
Rule to parse one part of the selector.
shared_ptr< eval::Eval< MaybeCachedString > > image
#define ALAC_ZOOM_BOTTOM
Definition: settings.hpp:132
shared_ptr< eval::Eval< int > > z_index
UnaryTypes unaryOperator_
The symbol parser for unary operators.
Represents a value in a style template.
Definition: eval.hpp:53
void applyStyleToRules(std::vector< RulePtr > &rules, const StylePtr &style)
Sets a given style as the style of given rules.
shared_ptr< eval::Eval< MaybeCachedString > > font_family
BinaryTypes binaryOperator_
The symbol parser for binary operators.
shared_ptr< eval::Eval< Style::ShieldShape > > shield_shape
shared_ptr< eval::Eval< Color > > shield_casing_color
shared_ptr< eval::Eval< Style::FontStyle > > font_style
shared_ptr< eval::Eval< MaybeCachedString > > icon_image
#define ALAC_ZOOM_TOP
Definition: settings.hpp:133
qi::rule< ItType, string(), Skipper > attribute_name
Rule to parse generic (unknown) attribute name.
shared_ptr< eval::Eval< Color > > text_halo_color
shared_ptr< eval::Eval< float > > icon_width
shared_ptr< eval::Eval< float > > text_offset
qi::rule< ItType, BinaryCondition()> rule_binary_condition
Rule to parse a binary condition.
shared_ptr< eval::Eval< MaybeCachedString > > shield_image
shared_ptr< eval::Eval< Color > > shield_color
shared_ptr< eval::Eval< Color > > fill_color
shared_ptr< eval::Eval< Style::TextPosition > > text_position
MapCSSGrammar(MapCssParser &parser)
Creates all rules for the mapcss grammar.
void warnUnsupportedAttribute(const string &attribute) const
Emits a warning for unknown mapcss attribute.
shared_ptr< eval::Eval< std::vector< double > > > casing_dashes
qi::rule< ItType, StylesheetPtr(), qi::locals< std::vector< RulePtr >, StylePtr >, Skipper > rule_stylesheet
Main rule returning a stylesheet.
qi::rule< ItType, UnaryCondition()> rule_unary_condition
Rule to parse a unary condition.
shared_ptr< eval::Eval< Style::LineCap > > casing_linecap