1 #pragma once
2 
3 #include <mbgl/text/glyph_range.hpp>
4 #include <mbgl/util/font_stack.hpp>
5 #include <mbgl/util/rect.hpp>
6 #include <mbgl/util/traits.hpp>
7 #include <mbgl/util/optional.hpp>
8 #include <mbgl/util/immutable.hpp>
9 #include <mbgl/util/image.hpp>
10 #include <mbgl/util/util.hpp>
11 
12 #include <cstdint>
13 #include <vector>
14 #include <string>
15 #include <map>
16 #include <set>
17 
18 namespace mbgl {
19 
20 using GlyphID = char16_t;
21 using GlyphIDs = std::set<GlyphID>;
22 
23 // Note: this only works for the BMP
24 GlyphRange getGlyphRange(GlyphID glyph);
25 
26 struct GlyphMetrics {
27     uint32_t width = 0;
28     uint32_t height = 0;
29     int32_t left = 0;
30     int32_t top = 0;
31     uint32_t advance = 0;
32 };
33 
operator ==(const GlyphMetrics & lhs,const GlyphMetrics & rhs)34 inline bool operator==(const GlyphMetrics& lhs, const GlyphMetrics& rhs) {
35     return lhs.width == rhs.width &&
36         lhs.height == rhs.height &&
37         lhs.left == rhs.left &&
38         lhs.top == rhs.top &&
39         lhs.advance == rhs.advance;
40 }
41 
42 class Glyph {
43 public:
44     // We're using this value throughout the Mapbox GL ecosystem. If this is different, the glyphs
45     // also need to be reencoded.
46     static constexpr const uint8_t borderSize = 3;
47 
48     GlyphID id = 0;
49 
50     // A signed distance field of the glyph with a border (see above).
51     AlphaImage bitmap;
52 
53     // Glyph metrics
54     GlyphMetrics metrics;
55 };
56 
57 using Glyphs = std::map<GlyphID, optional<Immutable<Glyph>>>;
58 using GlyphMap = std::map<FontStack, Glyphs>;
59 
60 class PositionedGlyph {
61 public:
PositionedGlyph(GlyphID glyph_,float x_,float y_,bool vertical_)62     explicit PositionedGlyph(GlyphID glyph_, float x_, float y_, bool vertical_)
63         : glyph(glyph_), x(x_), y(y_), vertical(vertical_) {}
64 
65     GlyphID glyph = 0;
66     float x = 0;
67     float y = 0;
68     bool vertical = false;
69 };
70 
71 enum class WritingModeType : uint8_t;
72 
73 class Shaping {
74     public:
75     explicit Shaping() = default;
Shaping(float x,float y,WritingModeType writingMode_)76     explicit Shaping(float x, float y, WritingModeType writingMode_)
77         : top(y), bottom(y), left(x), right(x), writingMode(writingMode_) {}
78     std::vector<PositionedGlyph> positionedGlyphs;
79     float top = 0;
80     float bottom = 0;
81     float left = 0;
82     float right = 0;
83     WritingModeType writingMode;
84 
operator bool() const85     explicit operator bool() const { return !positionedGlyphs.empty(); }
86 };
87 
88 enum class WritingModeType : uint8_t {
89     None = 0,
90     Horizontal = 1 << 0,
91     Vertical = 1 << 1,
92 };
93 
operator |(WritingModeType a,WritingModeType b)94 MBGL_CONSTEXPR WritingModeType operator|(WritingModeType a, WritingModeType b) {
95     return WritingModeType(mbgl::underlying_type(a) | mbgl::underlying_type(b));
96 }
97 
operator |=(WritingModeType & a,WritingModeType b)98 MBGL_CONSTEXPR WritingModeType& operator|=(WritingModeType& a, WritingModeType b) {
99     return (a = a | b);
100 }
101 
operator &(WritingModeType lhs,WritingModeType rhs)102 MBGL_CONSTEXPR bool operator&(WritingModeType lhs, WritingModeType rhs) {
103     return mbgl::underlying_type(lhs) & mbgl::underlying_type(rhs);
104 }
105 
operator &=(WritingModeType & lhs,WritingModeType rhs)106 MBGL_CONSTEXPR WritingModeType& operator&=(WritingModeType& lhs, WritingModeType rhs) {
107     return (lhs = WritingModeType(mbgl::underlying_type(lhs) & mbgl::underlying_type(rhs)));
108 }
109 
operator ~(WritingModeType value)110 MBGL_CONSTEXPR WritingModeType operator~(WritingModeType value) {
111     return WritingModeType(~mbgl::underlying_type(value));
112 }
113 
114 using GlyphDependencies = std::map<FontStack,GlyphIDs>;
115 using GlyphRangeDependencies = std::map<FontStack,GlyphRangeSet>;
116 
117 } // end namespace mbgl
118