summaryrefslogtreecommitdiff
path: root/include/mbgl/style/expression/formatted.hpp
blob: 09edad240fa44ecd14abba786535e66f6b3e810d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#pragma once

#include <mbgl/style/conversion.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/util/font_stack.hpp>
#include <mbgl/util/optional.hpp>

#include <vector>
#include <string>

namespace mbgl {
namespace style {
namespace expression {

extern const char* const kFormattedSectionFontScale;
extern const char* const kFormattedSectionTextFont;
extern const char* const kFormattedSectionTextColor;

struct FormattedSection {
    FormattedSection(std::string text_,
                     optional<double> fontScale_,
                     optional<FontStack> fontStack_,
                     optional<Color> textColor_)
        : text(std::move(text_))
        , fontScale(std::move(fontScale_))
        , fontStack(std::move(fontStack_))
        , textColor(std::move(textColor_))
    {}

    std::string text;
    optional<double> fontScale;
    optional<FontStack> fontStack;
    optional<Color> textColor;
};

class Formatted {
public:
    Formatted() = default;

    Formatted(const char* plainU8String) {
        sections.emplace_back(std::string(plainU8String), nullopt, nullopt, nullopt);
    }
    
    Formatted(std::vector<FormattedSection> sections_)
        : sections(std::move(sections_))
    {}
    
    bool operator==(const Formatted& ) const;
    
    std::string toString() const;
    mbgl::Value toObject() const;

    bool empty() const {
        return sections.empty() || sections.at(0).text.empty();
    }
     
    std::vector<FormattedSection> sections;
};
            
} // namespace expression
    
namespace conversion {

template <>
struct Converter<expression::Formatted> {
public:
    optional<expression::Formatted> operator()(const Convertible& value, Error& error) const;
};

template <>
struct ValueFactory<expression::Formatted> {
    static Value make(const expression::Formatted& formatted) { return formatted.toObject(); }
};

} // namespace conversion
    
} // namespace style
} // namespace mbgl