1 #include <mbgl/text/glyph_atlas.hpp>
2 
3 #include <mapbox/shelf-pack.hpp>
4 
5 namespace mbgl {
6 
7 static constexpr uint32_t padding = 1;
8 
makeGlyphAtlas(const GlyphMap & glyphs)9 GlyphAtlas makeGlyphAtlas(const GlyphMap& glyphs) {
10     GlyphAtlas result;
11 
12     mapbox::ShelfPack::ShelfPackOptions options;
13     options.autoResize = true;
14     mapbox::ShelfPack pack(0, 0, options);
15 
16     for (const auto& glyphMapEntry : glyphs) {
17         const FontStack& fontStack = glyphMapEntry.first;
18         GlyphPositionMap& positions = result.positions[fontStack];
19 
20         for (const auto& entry : glyphMapEntry.second) {
21             if (entry.second && (*entry.second)->bitmap.valid()) {
22                 const Glyph& glyph = **entry.second;
23 
24                 const mapbox::Bin& bin = *pack.packOne(-1,
25                     glyph.bitmap.size.width + 2 * padding,
26                     glyph.bitmap.size.height + 2 * padding);
27 
28                 result.image.resize({
29                     static_cast<uint32_t>(pack.width()),
30                     static_cast<uint32_t>(pack.height())
31                 });
32 
33                 AlphaImage::copy(glyph.bitmap,
34                                  result.image,
35                                  { 0, 0 },
36                                  {
37                                     bin.x + padding,
38                                     bin.y + padding
39                                  },
40                                  glyph.bitmap.size);
41 
42                 positions.emplace(glyph.id,
43                                   GlyphPosition {
44                                      Rect<uint16_t> {
45                                          static_cast<uint16_t>(bin.x),
46                                          static_cast<uint16_t>(bin.y),
47                                          static_cast<uint16_t>(bin.w),
48                                          static_cast<uint16_t>(bin.h)
49                                      },
50                                      glyph.metrics
51                                   });
52             }
53         }
54     }
55 
56     pack.shrink();
57     result.image.resize({
58         static_cast<uint32_t>(pack.width()),
59         static_cast<uint32_t>(pack.height())
60     });
61 
62     return result;
63 }
64 
65 } // namespace mbgl
66