summaryrefslogtreecommitdiff
path: root/src/mbgl/text/glyph_pbf.cpp
blob: c14f52de7a3ad285580bba0df78a2c4e4bf5d566 (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
#include <mbgl/text/glyph_pbf.hpp>

#include <mbgl/storage/file_source.hpp>
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/text/font_stack.hpp>
#include <mbgl/text/glyph_store.hpp>
#include <mbgl/util/exception.hpp>
#include <mbgl/util/pbf.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/thread_context.hpp>
#include <mbgl/util/token.hpp>
#include <mbgl/util/url.hpp>

#include <sstream>

namespace {

void parseGlyphPBF(mbgl::FontStack& stack, const std::string& data) {
    mbgl::pbf glyphs_pbf(reinterpret_cast<const uint8_t *>(data.data()), data.size());

    while (glyphs_pbf.next()) {
        if (glyphs_pbf.tag == 1) { // stacks
            mbgl::pbf fontstack_pbf = glyphs_pbf.message();
            while (fontstack_pbf.next()) {
                if (fontstack_pbf.tag == 3) { // glyphs
                    mbgl::pbf glyph_pbf = fontstack_pbf.message();

                    mbgl::SDFGlyph glyph;

                    while (glyph_pbf.next()) {
                        if (glyph_pbf.tag == 1) { // id
                            glyph.id = glyph_pbf.varint();
                        } else if (glyph_pbf.tag == 2) { // bitmap
                            glyph.bitmap = glyph_pbf.string();
                        } else if (glyph_pbf.tag == 3) { // width
                            glyph.metrics.width = glyph_pbf.varint();
                        } else if (glyph_pbf.tag == 4) { // height
                            glyph.metrics.height = glyph_pbf.varint();
                        } else if (glyph_pbf.tag == 5) { // left
                            glyph.metrics.left = glyph_pbf.svarint();
                        } else if (glyph_pbf.tag == 6) { // top
                            glyph.metrics.top = glyph_pbf.svarint();
                        } else if (glyph_pbf.tag == 7) { // advance
                            glyph.metrics.advance = glyph_pbf.varint();
                        } else {
                            glyph_pbf.skip();
                        }
                    }

                    stack.insert(glyph.id, glyph);
                } else {
                    fontstack_pbf.skip();
                }
            }
        } else {
            glyphs_pbf.skip();
        }
    }
}

} // namespace

namespace mbgl {

GlyphPBF::GlyphPBF(GlyphStore* store,
                   const std::string& fontStack,
                   const GlyphRange& glyphRange)
    : parsed(false) {
    // Load the glyph set URL
    std::string url = util::replaceTokens(store->getURL(), [&](const std::string &name) -> std::string {
        if (name == "fontstack") return util::percentEncode(fontStack);
        if (name == "range") return util::toString(glyphRange.first) + "-" + util::toString(glyphRange.second);
        return "";
    });

    auto requestCallback = [this, store, fontStack, url](Response res) {
        if (res.stale) {
            // Only handle fresh responses.
            return;
        }
        req = nullptr;

        if (res.error) {
            std::stringstream message;
            message <<  "Failed to load [" << url << "]: " << res.error->message;
            emitGlyphPBFLoadingFailed(message.str());
        } else {
            data = res.data;
            parse(store, fontStack, url);
        }
    };

    FileSource* fs = util::ThreadContext::getFileSource();
    req = fs->request({ Resource::Kind::Glyphs, url }, requestCallback);
}

GlyphPBF::~GlyphPBF() = default;

void GlyphPBF::parse(GlyphStore* store, const std::string& fontStack, const std::string& url) {
    assert(data);
    if (data->empty()) {
        // If there is no data, this means we either haven't
        // received any data.
        return;
    }

    try {
        parseGlyphPBF(**store->getFontStack(fontStack), *data);
    } catch (const std::exception& ex) {
        std::stringstream message;
        message <<  "Failed to parse [" << url << "]: " << ex.what();
        emitGlyphPBFLoadingFailed(message.str());
        return;
    }

    parsed = true;

    emitGlyphPBFLoaded();
}

void GlyphPBF::setObserver(Observer* observer_) {
    observer = observer_;
}

void GlyphPBF::emitGlyphPBFLoaded() {
    if (observer) {
        observer->onGlyphPBFLoaded();
    }
}

void GlyphPBF::emitGlyphPBFLoadingFailed(const std::string& message) {
    if (!observer) {
        return;
    }

    auto error = std::make_exception_ptr(util::GlyphRangeLoadingException(message));
    observer->onGlyphPBFLoadingFailed(error);
}

} // namespace mbgl