style_property_parser.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28
29 #pragma once
30
31 #include <memory>
32 #include <string>
33 #include <vector>
34 #include "../../Display/2D/color.h"
35 #include "style_get_value.h"
36 #include "style_set_value.h"
37 #include "style_set_image.h"
38 #include "style_token.h"
39 #include "style_tokenizer.h"
40
41 namespace clan
42 {
43 enum class StyleDimension;
44 class StyleGetValue;
45 class StyleSetValue;
46 class ImageSource;
47 class StyleToken;
48
50 class StylePropertySetter
51 {
52 public:
53 virtual ~StylePropertySetter() { }
54
56 virtual void set_value(const std::string &name, const StyleSetValue &value) = 0;
57
59 virtual void set_value_array(const std::string &name, const std::vector<StyleSetValue> &value_array) = 0;
60 };
61
63 class StyleParser
64 {
65 public:
67 std::vector<StyleToken> tokens;
68
70 bool important_flag = false;
71 };
72
74 class StylePropertyParser
75 {
76 public:
78 StylePropertyParser(const std::vector<std::string> &property_names);
79 virtual ~StylePropertyParser() { }
80
82 virtual void parse(StylePropertySetter *setter, const std::string &name, StyleParser &parser) = 0;
83
84 protected:
85 static StyleToken next_token(size_t &pos, const std::vector<StyleToken> &tokens, bool skip_whitespace = true);
86 static bool is_length(const StyleToken &token);
87 static bool is_angle(const StyleToken &token);
88 static bool is_time(const StyleToken &token);
89 static bool is_frequency(const StyleToken &token);
90 static bool is_resolution(const StyleToken &token);
91 static bool parse_length(const StyleToken &token, StyleSetValue &out_length);
92 static bool parse_angle(const StyleToken &token, StyleSetValue &out_angle);
93 static bool parse_time(const StyleToken &token, StyleSetValue &out_time);
94 static bool parse_frequency(const StyleToken &token, StyleSetValue &out_frequency);
95 static bool parse_resolution(const StyleToken &token, StyleSetValue &out_resolution);
96 static bool parse_integer(const std::string &value, int &out_int);
97 static bool parse_gradient(const std::vector<StyleToken> &tokens, size_t &in_out_pos, StyleGradient &out_gradient);
98 static bool parse_color(const std::vector<StyleToken> &tokens, size_t &in_out_pos, Colorf &out_color);
99 static bool parse_position(const std::vector<StyleToken> &tokens, size_t &in_out_pos, StyleSetValue &out_position_x, StyleSetValue &out_position_y);
100 static bool equals(const std::string &s1, const std::string &s2);
101 static void debug_parse_error(const std::string &name, const std::vector<StyleToken> &tokens);
102
103 private:
104 struct ColorType
105 {
106 const std::string::value_type *name;
107 int color;
108 };
109 static ColorType colors[];
110 };
111
113 class StylePropertyDefault
114 {
115 public:
116 StylePropertyDefault(const std::string &name, const StyleGetValue &value);
117 };
118
120 class StyleProperty
121 {
122 public:
124 static const StyleGetValue &default_value(const char *name);
125 static const StyleGetValue &default_value(const std::string &name);
126
128 static bool is_inherited(const char *name);
129 static bool is_inherited(const std::string &name);
130
132 static void parse(StylePropertySetter *setter, const std::string &styles);
133 };
134 }
clan::StylePropertyParser::parse_color
static bool parse_color(const std::vector< StyleToken > &tokens, size_t &in_out_pos, Colorf &out_color)
clan::StylePropertySetter::set_value
virtual void set_value(const std::string &name, const StyleSetValue &value)=0
Set the value for the specified property name.
clan
Definition: clanapp.h:35
clan::Colorf
Floating point color description class (for float).
Definition: color.h:660
clan::StylePropertyParser::parse_position
static bool parse_position(const std::vector< StyleToken > &tokens, size_t &in_out_pos, StyleSetValue &out_position_x, StyleSetValue &out_position_y)
clan::StylePropertyParser::is_resolution
static bool is_resolution(const StyleToken &token)
clan::StyleGradient
Linear or radial gradient.
Definition: style_set_image.h:48
clan::StyleParser::tokens
std::vector< StyleToken > tokens
Tokenized style string relevant for parsing a property.
Definition: style_property_parser.h:67
clan::StylePropertyParser::equals
static bool equals(const std::string &s1, const std::string &s2)
clan::StyleProperty::default_value
static const StyleGetValue & default_value(const char *name)
Gets the default value for a given property.
clan::StylePropertyParser::parse_frequency
static bool parse_frequency(const StyleToken &token, StyleSetValue &out_frequency)
clan::StylePropertyParser::parse_resolution
static bool parse_resolution(const StyleToken &token, StyleSetValue &out_resolution)
clan::StyleProperty::is_inherited
static bool is_inherited(const char *name)
Indicates if this an inherited property or not.
clan::StylePropertyParser::parse_length
static bool parse_length(const StyleToken &token, StyleSetValue &out_length)
clan::StylePropertyParser::is_time
static bool is_time(const StyleToken &token)
clan::StyleProperty
Style property interface used to parse or query properties by name.
Definition: style_property_parser.h:120
clan::StyleTokenType::string
@ keyword
clan::StylePropertySetter
Interface used by style parsers to store values in a style property set.
Definition: style_property_parser.h:50
clan::StyleProperty::parse
static void parse(StylePropertySetter *setter, const std::string &styles)
Parses a string of styles and sets the values.
clan::StylePropertyParser::parse_angle
static bool parse_angle(const StyleToken &token, StyleSetValue &out_angle)
clan::StylePropertyDefault
Sets the default value for a property.
Definition: style_property_parser.h:113
clan::StyleDimension
StyleDimension
Unit of a style value.
Definition: style_dimension.h:34
clan::StyleToken
Style token.
Definition: style_token.h:68
clan::StylePropertyParser::is_length
static bool is_length(const StyleToken &token)
clan::StyleGetValue
Style value returned by style classes.
Definition: style_get_value.h:39
clan::StylePropertyParser::parse
virtual void parse(StylePropertySetter *setter, const std::string &name, StyleParser &parser)=0
Parse a property and set property values if successful.
clan::StylePropertyParser
Style property parser.
Definition: style_property_parser.h:74
clan::StylePropertyParser::~StylePropertyParser
virtual ~StylePropertyParser()
Definition: style_property_parser.h:79
clan::StylePropertyDefault::StylePropertyDefault
StylePropertyDefault(const std::string &name, const StyleGetValue &value)
clan::StylePropertyParser::parse_time
static bool parse_time(const StyleToken &token, StyleSetValue &out_time)
clan::StylePropertyParser::parse_gradient
static bool parse_gradient(const std::vector< StyleToken > &tokens, size_t &in_out_pos, StyleGradient &out_gradient)
clan::StylePropertySetter::set_value_array
virtual void set_value_array(const std::string &name, const std::vector< StyleSetValue > &value_array)=0
Set a value array for the specified property name.
clan::StylePropertyParser::is_frequency
static bool is_frequency(const StyleToken &token)
clan::StylePropertyParser::debug_parse_error
static void debug_parse_error(const std::string &name, const std::vector< StyleToken > &tokens)
clan::StylePropertyParser::is_angle
static bool is_angle(const StyleToken &token)
clan::StylePropertySetter::~StylePropertySetter
virtual ~StylePropertySetter()
Definition: style_property_parser.h:53
clan::StyleSetValue
Style value variable.
Definition: style_set_value.h:39
clan::StyleValueType::color
value is an url
clan::StylePropertyParser::StylePropertyParser
StylePropertyParser(const std::vector< std::string > &property_names)
Constructs a property parser for the specified property names.
clan::StylePropertyParser::parse_integer
static bool parse_integer(const std::string &value, int &out_int)
clan::StyleParser::important_flag
bool important_flag
True if the !important flag was specified in the style string.
Definition: style_property_parser.h:70
clan::StyleParser
Parser interface used during property parsing.
Definition: style_property_parser.h:63
clan::StylePropertyParser::next_token
static StyleToken next_token(size_t &pos, const std::vector< StyleToken > &tokens, bool skip_whitespace=true)