summaryrefslogtreecommitdiff
path: root/src/mbgl/renderer/symbol_bucket.hpp
blob: 8581a23574a9909f48aa350e9a66c90699bdd4ac (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#ifndef MBGL_RENDERER_SYMBOLBUCKET
#define MBGL_RENDERER_SYMBOLBUCKET

#include <mbgl/renderer/bucket.hpp>
#include <mbgl/tile/geometry_tile.hpp>
#include <mbgl/map/mode.hpp>
#include <mbgl/geometry/vao.hpp>
#include <mbgl/geometry/elements_buffer.hpp>
#include <mbgl/geometry/text_buffer.hpp>
#include <mbgl/geometry/icon_buffer.hpp>
#include <mbgl/geometry/collision_box_buffer.hpp>
#include <mbgl/text/glyph.hpp>
#include <mbgl/text/collision_feature.hpp>
#include <mbgl/text/shaping.hpp>
#include <mbgl/text/quads.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/layer/symbol_layer.hpp>

#include <memory>
#include <map>
#include <set>
#include <vector>

namespace mbgl {

class SDFShader;
class IconShader;
class CollisionBoxShader;
class DotShader;
class CollisionTile;
class SpriteAtlas;
class SpriteStore;
class GlyphAtlas;
class GlyphStore;
class IndexedSubfeature;

class SymbolFeature {
public:
    GeometryCollection geometry;
    std::u32string label;
    std::string sprite;
    std::size_t index;
};

struct Anchor;

class SymbolInstance {
    public:
        explicit SymbolInstance(Anchor& anchor, const GeometryCoordinates& line,
                const Shaping& shapedText, const PositionedIcon& shapedIcon,
                const SymbolLayoutProperties& layout, const bool inside, const uint32_t index,
                const float textBoxScale, const float textPadding, const float textAlongLine,
                const float iconBoxScale, const float iconPadding, const float iconAlongLine,
                const GlyphPositions& face, const IndexedSubfeature& indexedfeature);
        Point<float> point;
        uint32_t index;
        bool hasText;
        bool hasIcon;
        SymbolQuads glyphQuads;
        SymbolQuads iconQuads;
        CollisionFeature textCollisionFeature;
        CollisionFeature iconCollisionFeature;
};

class SymbolBucket : public Bucket {
    typedef ElementGroup<1> TextElementGroup;
    typedef ElementGroup<2> IconElementGroup;
    typedef ElementGroup<1> CollisionBoxElementGroup;

public:
    SymbolBucket(uint32_t overscaling, float zoom, const MapMode, const std::string& bucketName_, const std::string& sourceLayerName_);
    ~SymbolBucket() override;

    void upload(gl::GLObjectStore&) override;
    void render(Painter&, const StyleLayer&, const UnwrappedTileID&, const mat4&) override;
    bool hasData() const override;
    bool hasTextData() const;
    bool hasIconData() const;
    bool hasCollisionBoxData() const;
    bool needsClipping() const override;

    void addFeatures(uintptr_t tileUID,
                     SpriteAtlas&,
                     GlyphAtlas&,
                     GlyphStore&);

    void drawGlyphs(SDFShader&, gl::GLObjectStore&);
    void drawIcons(SDFShader&, gl::GLObjectStore&);
    void drawIcons(IconShader&, gl::GLObjectStore&);
    void drawCollisionBoxes(CollisionBoxShader&, gl::GLObjectStore&);

    void parseFeatures(const GeometryTileLayer&, const Filter&);
    bool needsDependencies(GlyphStore&, SpriteStore&);
    void placeFeatures(CollisionTile&) override;

private:
    void addFeature(const GeometryCollection &lines,
            const Shaping &shapedText, const PositionedIcon &shapedIcon,
            const GlyphPositions &face,
            const size_t index);
    bool anchorIsTooClose(const std::u32string &text, const float repeatDistance, Anchor &anchor);
    std::map<std::u32string, std::vector<Anchor>> compareText;
    
    void addToDebugBuffers(CollisionTile &collisionTile);

    void swapRenderData() override;

    // Adds placed items to the buffer.
    template <typename Buffer, typename GroupType>
    void addSymbols(Buffer &buffer, const SymbolQuads &symbols, float scale,
            const bool keepUpright, const bool alongLine, const float placementAngle);

public:
    SymbolLayoutProperties layout;

    float iconMaxSize = 1.0f;
    float textMaxSize = 16.0f;

    bool sdfIcons = false;
    bool iconsNeedLinear = false;

private:

    const float overscaling;
    const float zoom;
    const uint32_t tileSize;
    const float tilePixelRatio;
    const MapMode mode;
    const std::string bucketName;
    const std::string sourceLayerName;

    std::set<GlyphRange> ranges;
    std::vector<SymbolInstance> symbolInstances;
    std::vector<SymbolFeature> features;

    struct SymbolRenderData {
        struct TextBuffer {
            TextVertexBuffer vertices;
            TriangleElementsBuffer triangles;
            std::vector<std::unique_ptr<TextElementGroup>> groups;
        } text;

        struct IconBuffer {
            IconVertexBuffer vertices;
            TriangleElementsBuffer triangles;
            std::vector<std::unique_ptr<IconElementGroup>> groups;
        } icon;

        struct CollisionBoxBuffer {
            CollisionBoxVertexBuffer vertices;
            std::vector<std::unique_ptr<CollisionBoxElementGroup>> groups;
        } collisionBox;
    };

    std::unique_ptr<SymbolRenderData> renderData;
    std::unique_ptr<SymbolRenderData> renderDataInProgress;
};

} // namespace mbgl

#endif