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

#include <mapbox/shelf-pack.hpp>

namespace mbgl {

static constexpr uint32_t padding = 1;

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

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

    for (const auto& glyphMapEntry : glyphs) {
        const FontStack& fontStack = glyphMapEntry.first;
        GlyphPositionMap& positions = result.positions[fontStack];

        for (const auto& entry : glyphMapEntry.second) {
            if (entry.second && (*entry.second)->bitmap.valid()) {
                const Glyph& glyph = **entry.second;

                const mapbox::Bin& bin = *pack.packOne(-1,
                    glyph.bitmap.size.width + 2 * padding,
                    glyph.bitmap.size.height + 2 * padding);

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

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

                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
                                  });
            }
        }
    }

//    pack.shrink();
//    result.image.resize({
//        static_cast<uint32_t>(pack.width()),
//        static_cast<uint32_t>(pack.height())
//    });

    return result;
}

} // namespace mbgl