style_property_parser.h
1/*
2** ClanLib SDK
3** Copyright (c) 1997-2020 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** Mark Page
28*/
29
30#pragma once
31
32#include <memory>
33#include <string>
34#include <vector>
35#include "../../Display/2D/color.h"
36#include "style_get_value.h"
37#include "style_set_value.h"
38#include "style_set_image.h"
39#include "style_token.h"
40#include "style_tokenizer.h"
41#include "property_hash.h"
42
43namespace clan
44{
45 enum class StyleDimension;
46 class StyleGetValue;
47 class StyleSetValue;
48 class ImageSource;
49 class StyleToken;
50
53 {
54 public:
56
58 virtual void set_value(PropertyHash hash, const StyleSetValue &value) = 0;
59
61 virtual void set_value_array(PropertyHash hash, const std::vector<StyleSetValue> &value_array) = 0;
62 };
63
66 {
67 public:
69 std::vector<StyleToken> tokens;
70
72 bool important_flag = false;
73 };
74
77 {
78 public:
80 StylePropertyParser(const std::vector<std::string> &property_names);
82
84 virtual void parse(StylePropertySetter *setter, const std::string &name, StyleParser &parser) = 0;
85
86 protected:
87 static StyleToken next_token(size_t &pos, const std::vector<StyleToken> &tokens, bool skip_whitespace = true);
88 static bool is_length(const StyleToken &token);
89 static bool is_angle(const StyleToken &token);
90 static bool is_time(const StyleToken &token);
91 static bool is_frequency(const StyleToken &token);
92 static bool is_resolution(const StyleToken &token);
93 static bool parse_length(const StyleToken &token, StyleSetValue &out_length);
94 static bool parse_angle(const StyleToken &token, StyleSetValue &out_angle);
95 static bool parse_time(const StyleToken &token, StyleSetValue &out_time);
96 static bool parse_frequency(const StyleToken &token, StyleSetValue &out_frequency);
97 static bool parse_resolution(const StyleToken &token, StyleSetValue &out_resolution);
98 static bool parse_integer(const std::string &value, int &out_int);
99 static bool parse_gradient(const std::vector<StyleToken> &tokens, size_t &in_out_pos, StyleGradient &out_gradient);
100 static bool parse_color(const std::vector<StyleToken> &tokens, size_t &in_out_pos, Colorf &out_color);
101 static bool parse_position(const std::vector<StyleToken> &tokens, size_t &in_out_pos, StyleSetValue &out_position_x, StyleSetValue &out_position_y);
102 static bool equals(const std::string &s1, const std::string &s2);
103 static void debug_parse_error(const std::string &name, const std::vector<StyleToken> &tokens);
104
105 private:
106 };
107
110 {
111 public:
112 StylePropertyDefault(const std::string &name, const StyleGetValue &value, bool inherit);
113 };
114
117 {
118 public:
121
124
126 static void parse(StylePropertySetter *setter, const std::string &styles);
127 };
128}
Floating point color description class (for float).
Definition: color.h:799
Definition: property_hash.h:35
Style value returned by style classes.
Definition: style_get_value.h:40
Linear or radial gradient.
Definition: style_set_image.h:49
Parser interface used during property parsing.
Definition: style_property_parser.h:66
std::vector< StyleToken > tokens
Tokenized style string relevant for parsing a property.
Definition: style_property_parser.h:69
bool important_flag
True if the !important flag was specified in the style string.
Definition: style_property_parser.h:72
Sets the default value for a property.
Definition: style_property_parser.h:110
StylePropertyDefault(const std::string &name, const StyleGetValue &value, bool inherit)
Style property parser.
Definition: style_property_parser.h:77
static bool is_length(const StyleToken &token)
virtual void parse(StylePropertySetter *setter, const std::string &name, StyleParser &parser)=0
Parse a property and set property values if successful.
static bool parse_angle(const StyleToken &token, StyleSetValue &out_angle)
static bool parse_frequency(const StyleToken &token, StyleSetValue &out_frequency)
StylePropertyParser(const std::vector< std::string > &property_names)
Constructs a property parser for the specified property names.
static void debug_parse_error(const std::string &name, const std::vector< StyleToken > &tokens)
static StyleToken next_token(size_t &pos, const std::vector< StyleToken > &tokens, bool skip_whitespace=true)
static bool parse_resolution(const StyleToken &token, StyleSetValue &out_resolution)
static bool is_time(const StyleToken &token)
static bool parse_position(const std::vector< StyleToken > &tokens, size_t &in_out_pos, StyleSetValue &out_position_x, StyleSetValue &out_position_y)
static bool is_angle(const StyleToken &token)
static bool equals(const std::string &s1, const std::string &s2)
static bool is_resolution(const StyleToken &token)
virtual ~StylePropertyParser()
Definition: style_property_parser.h:81
static bool parse_color(const std::vector< StyleToken > &tokens, size_t &in_out_pos, Colorf &out_color)
static bool is_frequency(const StyleToken &token)
static bool parse_length(const StyleToken &token, StyleSetValue &out_length)
static bool parse_time(const StyleToken &token, StyleSetValue &out_time)
static bool parse_integer(const std::string &value, int &out_int)
static bool parse_gradient(const std::vector< StyleToken > &tokens, size_t &in_out_pos, StyleGradient &out_gradient)
Interface used by style parsers to store values in a style property set.
Definition: style_property_parser.h:53
virtual ~StylePropertySetter()
Definition: style_property_parser.h:55
virtual void set_value(PropertyHash hash, const StyleSetValue &value)=0
Set the value for the specified property name.
virtual void set_value_array(PropertyHash hash, const std::vector< StyleSetValue > &value_array)=0
Set a value array for the specified property name.
Style property interface used to parse or query properties by name.
Definition: style_property_parser.h:117
static const StyleGetValue & default_value(PropertyHash hash)
Gets the default value for a given property.
static void parse(StylePropertySetter *setter, const std::string &styles)
Parses a string of styles and sets the values.
static bool is_inherited(PropertyHash hash)
Indicates if this an inherited property or not.
Style value variable.
Definition: style_set_value.h:40
Style token.
Definition: style_token.h:69
Definition: clanapp.h:36
StyleDimension
Unit of a style value.
Definition: style_dimension.h:35
@ hash
Syntax error.