1 #pragma once
2 
3 #include <mbgl/text/glyph.hpp>
4 #include <mbgl/text/glyph_manager_observer.hpp>
5 #include <mbgl/text/glyph_range.hpp>
6 #include <mbgl/text/local_glyph_rasterizer.hpp>
7 #include <mbgl/util/noncopyable.hpp>
8 #include <mbgl/util/font_stack.hpp>
9 #include <mbgl/util/immutable.hpp>
10 
11 #include <string>
12 #include <unordered_map>
13 
14 namespace mbgl {
15 
16 class FileSource;
17 class AsyncRequest;
18 class Response;
19 
20 class GlyphRequestor {
21 public:
22     virtual ~GlyphRequestor() = default;
23     virtual void onGlyphsAvailable(GlyphMap) = 0;
24 };
25 
26 class GlyphManager : public util::noncopyable {
27 public:
28     GlyphManager(FileSource&, std::unique_ptr<LocalGlyphRasterizer> = std::make_unique<LocalGlyphRasterizer>(optional<std::string>()));
29     ~GlyphManager();
30 
31     // Workers send a `getGlyphs` message to the main thread once they have determined
32     // their `GlyphDependencies`. If all glyphs are already locally available, GlyphManager
33     // will provide them to the requestor immediately. Otherwise, it makes a request on the
34     // FileSource is made for each range neeed, and notifies the observer when all are
35     // complete.
36     void getGlyphs(GlyphRequestor&, GlyphDependencies);
37     void removeRequestor(GlyphRequestor&);
38 
setURL(const std::string & url)39     void setURL(const std::string& url) {
40         glyphURL = url;
41     }
42 
43     void setObserver(GlyphManagerObserver*);
44 
45 private:
46     Glyph generateLocalSDF(const FontStack& fontStack, GlyphID glyphID);
47 
48     FileSource& fileSource;
49     std::string glyphURL;
50 
51     struct GlyphRequest {
52         bool parsed = false;
53         std::unique_ptr<AsyncRequest> req;
54         std::unordered_map<GlyphRequestor*, std::shared_ptr<GlyphDependencies>> requestors;
55     };
56 
57     struct Entry {
58         std::map<GlyphRange, GlyphRequest> ranges;
59         std::map<GlyphID, Immutable<Glyph>> glyphs;
60     };
61 
62     std::unordered_map<FontStack, Entry, FontStackHash> entries;
63 
64     void requestRange(GlyphRequest&, const FontStack&, const GlyphRange&);
65     void processResponse(const Response&, const FontStack&, const GlyphRange&);
66     void notify(GlyphRequestor&, const GlyphDependencies&);
67 
68     GlyphManagerObserver* observer = nullptr;
69 
70     std::unique_ptr<LocalGlyphRasterizer> localGlyphRasterizer;
71 };
72 
73 } // namespace mbgl
74