Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
parser.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2023 AUTHORS
4 *
5 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
6 */
7#ifndef SEEN_COLORS_PARSING_H
8#define SEEN_COLORS_PARSING_H
9
10#include <map>
11#include <memory>
12#include <string>
13#include <vector>
14
15#include "spaces/enum.h"
16
17namespace Inkscape::Colors {
18
19class Parser
20{
21public:
22 Parser(std::string prefix, Space::Type type)
23 : _prefix(std::move(prefix))
24 , _type(type)
25 {}
26 virtual ~Parser() = default;
27
28 Space::Type getType() const { return _type; }
29 std::string const getPrefix() const { return _prefix; }
30 static std::string getCssPrefix(std::istringstream &ss);
31 static bool css_number(std::istringstream &ss, double &value, std::string &unit, bool &end, char const sep = 0x0);
32 static bool append_css_value(std::istringstream &ss, std::vector<double> &output, bool &end, char const sep = 0x0,
33 double scale = 1.0);
34
35 // TODO message: return reason for parsing failure. Otherwise we cannot tell the user why we reject certain colors.
36 virtual bool parse(std::istringstream &ss, std::vector<double> &output) const { return false; };
37 virtual bool parse(std::istringstream &ss, std::vector<double> &output, bool &more) const
38 {
39 return parse(ss, output);
40 };
41 virtual std::string parseColor(std::istringstream &ss, std::vector<double> &output, bool &more) const;
42
43private:
44 std::string _prefix;
46};
47
48class LegacyParser : public Parser
49{
50public:
51 LegacyParser(std::string const &prefix, Space::Type type, bool alpha)
52 : Parser(alpha ? prefix + "a" : prefix, type)
53 , _alpha(alpha)
54 {}
55
56protected:
57 bool _alpha = false;
58};
59
60class HueParser : public LegacyParser
61{
62public:
63 HueParser(std::string const &prefix, Space::Type type, bool alpha)
64 : LegacyParser(prefix, type, alpha)
65 {}
66 bool parse(std::istringstream &ss, std::vector<double> &output) const override;
67};
68
69class HexParser : public Parser
70{
71public:
73 : Parser("#", Space::Type::RGB)
74 {}
75 bool parse(std::istringstream &input, std::vector<double> &output, bool &more) const override;
76};
77
78class CssParser : public Parser
79{
80public:
81 CssParser(std::string prefix, Space::Type type, unsigned int channels)
82 : Parser(prefix, type)
83 , _channels(channels)
84 {}
85
86private:
87 bool parse(std::istringstream &ss, std::vector<double> &output) const override;
88
89 unsigned int _channels;
90};
91
93{
94private:
95 Parsers(Parsers const &) = delete;
96 void operator=(Parsers const &) = delete;
97
98public:
99 Parsers();
100 ~Parsers() = default;
101
102 static Parsers &get()
103 {
104 static Parsers instance;
105 return instance;
106 }
107
108 bool parse(std::string const &input, Space::Type &type, std::string &cms, std::vector<double> &values,
109 std::vector<double> &fallback) const;
110
111protected:
112 friend class Manager;
113
114 void addParser(Parser *parser);
115
116private:
117 bool _parse(std::istringstream &ss, Space::Type &type, std::string &cms, std::vector<double> &values,
118 std::vector<double> &fallback) const;
119
120 std::map<std::string, std::vector<std::shared_ptr<Parser>>> _parsers;
121};
122
123} // namespace Inkscape::Colors
124
125#endif // SEEN_COLORS_PARSING_H
bool parse(std::istringstream &ss, std::vector< double > &output) const override
Prase the given string stream as a CSS Color Module Level 4/5 string.
Definition parser.cpp:194
unsigned int _channels
Definition parser.h:89
CssParser(std::string prefix, Space::Type type, unsigned int channels)
Definition parser.h:81
bool parse(std::istringstream &input, std::vector< double > &output, bool &more) const override
Parse either a hex code or an rgb() css string.
Definition parser.cpp:162
bool parse(std::istringstream &ss, std::vector< double > &output) const override
Definition parser.cpp:151
HueParser(std::string const &prefix, Space::Type type, bool alpha)
Definition parser.h:63
LegacyParser(std::string const &prefix, Space::Type type, bool alpha)
Definition parser.h:51
virtual bool parse(std::istringstream &ss, std::vector< double > &output, bool &more) const
Definition parser.h:37
Space::Type _type
Definition parser.h:45
Parser(std::string prefix, Space::Type type)
Definition parser.h:22
Space::Type getType() const
Definition parser.h:28
std::string _prefix
Definition parser.h:44
static bool css_number(std::istringstream &ss, double &value, std::string &unit, bool &end, char const sep=0x0)
Parse a CSS color number after the function name.
Definition parser.cpp:244
virtual ~Parser()=default
static bool append_css_value(std::istringstream &ss, std::vector< double > &output, bool &end, char const sep=0x0, double scale=1.0)
Parse a CSS color number and format it according to it's unit.
Definition parser.cpp:324
static std::string getCssPrefix(std::istringstream &ss)
Parse CSS color numbers after the function name.
Definition parser.cpp:213
std::string const getPrefix() const
Definition parser.h:29
virtual std::string parseColor(std::istringstream &ss, std::vector< double > &output, bool &more) const
Parse this specific color format into output values.
Definition parser.cpp:143
virtual bool parse(std::istringstream &ss, std::vector< double > &output) const
Definition parser.h:36
Parsers(Parsers const &)=delete
bool parse(std::string const &input, Space::Type &type, std::string &cms, std::vector< double > &values, std::vector< double > &fallback) const
Turn a string into a color data, used in Color object creation.
Definition parser.cpp:68
std::map< std::string, std::vector< std::shared_ptr< Parser > > > _parsers
Definition parser.h:120
static Parsers & get()
Definition parser.h:102
void addParser(Parser *parser)
Add a prser to the list of parser objects used when parsing color strings.
Definition parser.cpp:127
void operator=(Parsers const &)=delete
bool _parse(std::istringstream &ss, Space::Type &type, std::string &cms, std::vector< double > &values, std::vector< double > &fallback) const
Internal recursive parser that scans through a string stream.
Definition parser.cpp:86
Geom::Point end
A set of useful color modifying functions which do not fit as generic methods on the color class itse...
Definition profile.cpp:24
STL namespace.