summaryrefslogtreecommitdiff
path: root/src/mbgl/text/glyph_manager.hpp
blob: 00df079462a6ab8f924ec93a0f6491938f08a449 (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
#pragma once

#include <mbgl/text/glyph.hpp>
#include <mbgl/text/glyph_manager_observer.hpp>
#include <mbgl/text/glyph_range.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/font_stack.hpp>
#include <mbgl/util/immutable.hpp>

#include <string>
#include <unordered_map>

namespace mbgl {

class FileSource;
class AsyncRequest;
class Response;

class GlyphRequestor {
public:
    virtual ~GlyphRequestor() = default;
    virtual void onGlyphsAvailable(GlyphMap) = 0;
};

class GlyphManager : public util::noncopyable {
public:
    GlyphManager(FileSource&);
    ~GlyphManager();

    // Workers send a `getGlyphs` message to the main thread once they have determined
    // their `GlyphDependencies`. If all glyphs are already locally available, GlyphManager
    // will provide them to the requestor immediately. Otherwise, it makes a request on the
    // FileSource is made for each range neeed, and notifies the observer when all are
    // complete.
    void getGlyphs(GlyphRequestor&, GlyphDependencies);
    void removeRequestor(GlyphRequestor&);

    void setURL(const std::string& url) {
        glyphURL = url;
    }

    void setObserver(GlyphManagerObserver*);

private:
    FileSource& fileSource;
    std::string glyphURL;

    struct GlyphRequest {
        bool parsed = false;
        std::unique_ptr<AsyncRequest> req;
        std::unordered_map<GlyphRequestor*, std::shared_ptr<GlyphDependencies>> requestors;
    };

    struct Entry {
        std::map<GlyphRange, GlyphRequest> ranges;
        std::map<GlyphID, Immutable<Glyph>> glyphs;
    };

    std::unordered_map<FontStack, Entry, FontStackHash> entries;

    GlyphRequest& requestRange(Entry&, const FontStack&, const GlyphRange&);
    void processResponse(const Response&, const FontStack&, const GlyphRange&);
    void notify(GlyphRequestor&, const GlyphDependencies&);

    GlyphManagerObserver* observer = nullptr;
};

} // namespace mbgl