summaryrefslogtreecommitdiff
path: root/src/mbgl/text/glyph_store.hpp
blob: 77452a3057097e4f8fe271dfa5bf590136222f86 (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
#ifndef MBGL_TEXT_GLYPH_STORE
#define MBGL_TEXT_GLYPH_STORE

#include <mbgl/text/glyph.hpp>
#include <mbgl/util/vec.hpp>
#include <mbgl/util/ptr.hpp>
#include <mbgl/util/exclusive.hpp>

#include <cstdint>
#include <vector>
#include <future>
#include <map>
#include <set>
#include <string>
#include <unordered_map>

typedef struct uv_loop_s uv_loop_t;

namespace uv {

class async;

}

namespace mbgl {

class FileSource;
class Environment;
class Request;

class SDFGlyph {
public:
    uint32_t id = 0;

    // A signed distance field of the glyph with a border of 3 pixels.
    std::string bitmap;

    // Glyph metrics
    GlyphMetrics metrics;
};

class FontStack {
public:
    void insert(uint32_t id, const SDFGlyph &glyph);
    const std::map<uint32_t, GlyphMetrics> &getMetrics() const;
    const std::map<uint32_t, SDFGlyph> &getSDFs() const;
    const Shaping getShaping(const std::u32string &string, float maxWidth, float lineHeight,
                             float horizontalAlign, float verticalAlign, float justify,
                             float spacing, const vec2<float> &translate) const;
    void lineWrap(Shaping &shaping, float lineHeight, float maxWidth, float horizontalAlign,
                  float verticalAlign, float justify) const;

private:
    std::map<uint32_t, std::string> bitmaps;
    std::map<uint32_t, GlyphMetrics> metrics;
    std::map<uint32_t, SDFGlyph> sdfs;
};

class GlyphPBF {
public:
    using GlyphLoadedCallback = std::function<void(GlyphPBF*)>;
    using GlyphLoadingFailedCallback = std::function<void(const std::string&)>;

    GlyphPBF(const std::string &glyphURL,
             const std::string &fontStack,
             GlyphRange glyphRange,
             Environment &env,
             const GlyphLoadedCallback& successCallback,
             const GlyphLoadingFailedCallback& failureCallback);
    ~GlyphPBF();

    void parse(FontStack &stack);
    bool isParsed() const;

private:
    GlyphPBF(const GlyphPBF &) = delete;
    GlyphPBF(GlyphPBF &&) = delete;
    GlyphPBF &operator=(const GlyphPBF &) = delete;
    GlyphPBF &operator=(GlyphPBF &&) = delete;

    std::string data;
    std::atomic_bool parsed;

    Environment& env;
    Request* req = nullptr;
};

// Manages Glyphrange PBF loading.
class GlyphStore {
public:
    class Observer {
    public:
        virtual ~Observer() = default;

        virtual void onGlyphRangeLoaded() = 0;
        virtual void onGlyphRangeLoadingFailed(std::exception_ptr error) = 0;
    };

    GlyphStore(uv_loop_t* loop, Environment &);
    ~GlyphStore();

    // Asynchronously request for GlyphRanges and when it gets loaded, notifies the
    // observer subscribed to this object. Successive requests for the same range are
    // going to be discarded. Returns true if a request was made or false if all the
    // GlyphRanges are already available, and thus, no request is performed.
    bool requestGlyphRangesIfNeeded(const std::string &fontStack, const std::set<GlyphRange> &glyphRanges);

    util::exclusive<FontStack> getFontStack(const std::string &fontStack);

    void setURL(const std::string &url);

    void setObserver(Observer* observer);

private:
    void emitGlyphRangeLoaded();
    void emitGlyphRangeLoadingFailed();

    util::exclusive<FontStack> createFontStack(const std::string &fontStack);

    std::string glyphURL;
    Environment &env;

    std::unordered_map<std::string, std::map<GlyphRange, std::unique_ptr<GlyphPBF>>> ranges;
    std::mutex rangesMutex;

    std::unordered_map<std::string, std::unique_ptr<FontStack>> stacks;
    std::mutex stacksMutex;

    std::string errorMessage;
    std::mutex errorMessageMutex;

    std::unique_ptr<uv::async> asyncEmitGlyphRangeLoaded;
    std::unique_ptr<uv::async> asyncEmitGlyphRangeLoadedingFailed;

    Observer* observer;
};

}

#endif