summaryrefslogtreecommitdiff
path: root/include/llmr/style/style_parser.hpp
blob: 3c2a12a2a30a320d16e9f21254e49e20dc884fb9 (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
79
80
81
82
83
84
85
86
87
88
#ifndef LLMR_STYLE_STYLE_PARSER
#define LLMR_STYLE_STYLE_PARSER

#include <rapidjson/document.h>
#include <llmr/style/style.hpp>
#include <llmr/map/source.hpp>
#include <llmr/style/filter_expression.hpp>
#include <llmr/style/class_properties.hpp>
#include <llmr/style/rasterize_properties.hpp>
#include <llmr/style/style_bucket.hpp>

#include <unordered_map>
#include <forward_list>


namespace llmr {

enum class ClassID : uint32_t;

class StyleLayer;
class StyleLayerGroup;

class StyleParser {
public:
    using JSVal = const rapidjson::Value&;

    void parse(JSVal document);

    std::shared_ptr<StyleLayerGroup> getLayers() {
        return root;
    }

private:
    void parseConstants(JSVal value);
    JSVal replaceConstant(JSVal value);

    void parseSources(JSVal value);

    std::unique_ptr<StyleLayerGroup> createLayers(JSVal value);
    std::shared_ptr<StyleLayer> createLayer(JSVal value);
    void parseLayers();
    void parseLayer(std::pair<JSVal, std::shared_ptr<StyleLayer>> &pair);
    void parseStyles(JSVal value, std::map<ClassID, ClassProperties> &styles);
    void parseStyle(JSVal, ClassProperties &properties);
    std::unique_ptr<const RasterizeProperties> parseRasterize(JSVal value);
    void parseReference(JSVal value, std::shared_ptr<StyleLayer> &layer);
    void parseBucket(JSVal value, std::shared_ptr<StyleLayer> &layer);
    void parseRender(JSVal value, std::shared_ptr<StyleLayer> &layer);

    // Parses optional properties into a render bucket.
    template<typename T>
    bool parseRenderProperty(JSVal value, T &target, const char *name);
    template <typename T, typename Parser>
    bool parseRenderProperty(JSVal value, T &target, const char *name, Parser &parser);

    // Parses optional properties into style class properties.
    template <typename T>
    bool parseStyleProperty(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value);
    template <typename T>
    bool parseStyleProperty(const char *property_name, const std::vector<PropertyKey> &keys, ClassProperties &klass, JSVal value);


    FilterExpression parseFilter(JSVal, FilterExpression::Operator op);
    FilterExpression parseFilter(JSVal);
    Value parseValue(JSVal value);
    std::forward_list<Value> parseValues(JSVal values);

private:
    std::unordered_map<std::string, const rapidjson::Value *> constants;

    std::unordered_map<std::string, const std::shared_ptr<Source>> sources;

    // This stores the root layer.
    std::shared_ptr<StyleLayerGroup> root;

    // This maps ids to Layer objects, with all items being at the root level.
    std::unordered_map<std::string, std::pair<JSVal, std::shared_ptr<StyleLayer>>> layers;

    // Layer IDs that we have encountered previously while parsing this stylesheet.
    std::set<std::string> existing_layer_ids;

    // Store a stack of layers we're parsing right now. This is to prevent reference cycles.
    std::forward_list<StyleLayer *> stack;
};

}

#endif