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

#include <mapbox/shelf-pack.hpp>

namespace mbgl {

static constexpr uint32_t padding = 1;

GlyphAtlas makeGlyphAtlas(const Glyphs& glyphs) {
    GlyphAtlas result;

    mapbox::ShelfPack::ShelfPackOptions options;
    options.autoResize = true;
    mapbox::ShelfPack pack(0, 0, options);

    std::vector<mapbox::Bin> bins;
    bins.reserve(glyphs.size());

    for (const auto& entry : glyphs) {
        if (entry.second && (*entry.second)->bitmap.valid()) {
            const Glyph& glyph = **entry.second;
            bins.emplace_back(glyph.id,
                              glyph.bitmap.size.width + 2 * padding,
                              glyph.bitmap.size.height + 2 * padding);
        }
    }

    mapbox::ShelfPack::PackOptions packOptions;
    packOptions.inPlace = true;
    pack.pack(bins, packOptions);

    result.image = AlphaImage({
        static_cast<uint32_t>(pack.width()),
        static_cast<uint32_t>(pack.height())
    });

    for (const auto& bin : bins) {
        const Glyph& glyph = **glyphs.at(bin.id);

        AlphaImage::copy(glyph.bitmap,
                         result.image,
                         { 0, 0 },
                         {
                            bin.x + padding,
                            bin.y + padding
                         },
                         glyph.bitmap.size);

        result.positions.emplace(glyph.id,
                                 GlyphPosition {
                                    Rect<uint16_t> {
                                        static_cast<uint16_t>(bin.x),
                                        static_cast<uint16_t>(bin.y),
                                        static_cast<uint16_t>(bin.w),
                                        static_cast<uint16_t>(bin.h)
                                    },
                                    glyph.metrics
                                 });
    }

    return result;
}

} // namespace mbgl