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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
#include "drape/symbols_texture.hpp"
#include "3party/stb_image/stb_image.h"
#include "indexer/map_style_reader.hpp"
#include "platform/platform.hpp"
#include "coding/reader.hpp"
#include "coding/parse_xml.hpp"
#include "base/string_utils.hpp"
#ifdef DEBUG
#include "3party/glm/glm/gtx/bit.hpp"
#endif
namespace dp
{
namespace
{
using TDefinitionInserter = function<void(string const &, m2::RectF const &)>;
using TSymbolsLoadingCompletion = function<void(unsigned char *, uint32_t, uint32_t)>;
using TSymbolsLoadingFailure = function<void(string const &)>;
class DefinitionLoader
{
public:
DefinitionLoader(TDefinitionInserter const & definitionInserter, bool convertToUV)
: m_definitionInserter(definitionInserter)
, m_convertToUV(convertToUV)
, m_width(0)
, m_height(0)
{
}
bool Push(string const & /*element*/) { return true;}
void Pop(string const & element)
{
if (element == "symbol")
{
ASSERT(!m_name.empty(), ());
ASSERT(m_rect.IsValid(), ());
ASSERT(m_definitionInserter != nullptr, ());
m_definitionInserter(m_name, m_rect);
m_name = "";
m_rect.MakeEmpty();
}
}
void AddAttr(string const & attribute, string const & value)
{
if (attribute == "name")
m_name = value;
else
{
int v;
if (!strings::to_int(value, v))
return;
if (attribute == "minX")
{
ASSERT(m_width != 0, ());
float const scalar = m_convertToUV ? static_cast<float>(m_width) : 1.0f;
m_rect.setMinX(v / scalar);
}
else if (attribute == "minY")
{
ASSERT(m_height != 0, ());
float const scalar = m_convertToUV ? static_cast<float>(m_height) : 1.0f;
m_rect.setMinY(v / scalar);
}
else if (attribute == "maxX")
{
ASSERT(m_width != 0, ());
float const scalar = m_convertToUV ? static_cast<float>(m_width) : 1.0f;
m_rect.setMaxX(v / scalar);
}
else if (attribute == "maxY")
{
ASSERT(m_height != 0, ());
float const scalar = m_convertToUV ? static_cast<float>(m_height) : 1.0f;
m_rect.setMaxY(v / scalar);
}
else if (attribute == "height")
{
m_height = v;
}
else if (attribute == "width")
{
m_width = v;
}
}
}
void CharData(string const &) {}
uint32_t GetWidth() const { return m_width; }
uint32_t GetHeight() const { return m_height; }
private:
TDefinitionInserter m_definitionInserter;
bool m_convertToUV;
uint32_t m_width;
uint32_t m_height;
string m_name;
m2::RectF m_rect;
};
void LoadSymbols(std::string const & skinPathName, std::string const & textureName,
bool convertToUV, TDefinitionInserter const & definitionInserter,
TSymbolsLoadingCompletion const & completionHandler,
TSymbolsLoadingFailure const & failureHandler)
{
ASSERT(definitionInserter != nullptr, ());
ASSERT(completionHandler != nullptr, ());
ASSERT(failureHandler != nullptr, ());
vector<unsigned char> rawData;
uint32_t width, height;
try
{
DefinitionLoader loader(definitionInserter, convertToUV);
{
ReaderPtr<Reader> reader = GetStyleReader().GetResourceReader(textureName + ".sdf", skinPathName);
ReaderSource<ReaderPtr<Reader> > source(reader);
if (!ParseXML(source, loader))
{
failureHandler("Error parsing skin");
return;
}
width = loader.GetWidth();
height = loader.GetHeight();
}
{
ReaderPtr<Reader> reader = GetStyleReader().GetResourceReader(textureName + ".png", skinPathName);
size_t const size = static_cast<size_t>(reader.Size());
rawData.resize(size);
reader.Read(0, &rawData[0], size);
}
}
catch (RootException & e)
{
failureHandler(e.what());
return;
}
int w, h, bpp;
unsigned char * data = stbi_load_from_memory(&rawData[0], static_cast<int>(rawData.size()), &w, &h, &bpp, 0);
ASSERT_EQUAL(bpp, 4, ("Incorrect symbols texture format"));
ASSERT(glm::isPowerOfTwo(w), (w));
ASSERT(glm::isPowerOfTwo(h), (h));
if (width == static_cast<uint32_t>(w) && height == static_cast<uint32_t>(h))
{
completionHandler(data, width, height);
}
else
{
failureHandler("Error symbols texture creation");
}
stbi_image_free(data);
}
}
SymbolsTexture::SymbolKey::SymbolKey(string const & symbolName)
: m_symbolName(symbolName)
{
}
Texture::ResourceType SymbolsTexture::SymbolKey::GetType() const
{
return Texture::Symbol;
}
string const & SymbolsTexture::SymbolKey::GetSymbolName() const
{
return m_symbolName;
}
SymbolsTexture::SymbolInfo::SymbolInfo(const m2::RectF & texRect)
: ResourceInfo(texRect)
{
}
Texture::ResourceType SymbolsTexture::SymbolInfo::GetType() const
{
return Symbol;
}
SymbolsTexture::SymbolsTexture(std::string const & skinPathName, std::string const & textureName,
ref_ptr<HWTextureAllocator> allocator)
: m_name(textureName)
{
Load(skinPathName, allocator);
}
void SymbolsTexture::Load(std::string const & skinPathName, ref_ptr<HWTextureAllocator> allocator)
{
auto definitionInserter = [this](string const & name, m2::RectF const & rect)
{
m_definition.insert(std::make_pair(name, SymbolsTexture::SymbolInfo(rect)));
};
auto completionHandler = [this, &allocator](unsigned char * data, uint32_t width, uint32_t height)
{
Texture::Params p;
p.m_allocator = allocator;
p.m_format = dp::RGBA8;
p.m_width = width;
p.m_height = height;
Create(p, make_ref(data));
};
auto failureHandler = [this](string const & reason)
{
LOG(LERROR, (reason));
Fail();
};
LoadSymbols(skinPathName, m_name, true /* convertToUV */, definitionInserter,
completionHandler, failureHandler);
}
void SymbolsTexture::Invalidate(string const & skinPathName, ref_ptr<HWTextureAllocator> allocator)
{
Destroy();
m_definition.clear();
Load(skinPathName, allocator);
}
ref_ptr<Texture::ResourceInfo> SymbolsTexture::FindResource(Texture::Key const & key, bool & newResource)
{
newResource = false;
if (key.GetType() != Texture::Symbol)
return nullptr;
string const & symbolName = static_cast<SymbolKey const &>(key).GetSymbolName();
TSymDefinition::iterator it = m_definition.find(symbolName);
ASSERT(it != m_definition.end(), (symbolName));
return make_ref(&it->second);
}
void SymbolsTexture::Fail()
{
m_definition.clear();
int32_t alfaTexture = 0;
Texture::Params p;
p.m_allocator = GetDefaultAllocator();
p.m_format = dp::RGBA8;
p.m_width = 1;
p.m_height = 1;
Create(p, make_ref(&alfaTexture));
}
bool SymbolsTexture::IsSymbolContained(std::string const & symbolName) const
{
return m_definition.find(symbolName) != m_definition.end();
}
bool SymbolsTexture::DecodeToMemory(std::string const & skinPathName, std::string const & textureName,
vector<uint8_t> & symbolsSkin,
std::map<string, m2::RectU> & symbolsIndex,
uint32_t & skinWidth, uint32_t & skinHeight)
{
auto definitionInserter = [&symbolsIndex](string const & name, m2::RectF const & rect)
{
symbolsIndex.insert(make_pair(name, m2::RectU(rect)));
};
bool result = true;
auto completionHandler = [&result, &symbolsSkin, &skinWidth, &skinHeight](unsigned char * data,
uint32_t width, uint32_t height)
{
size_t size = 4 * width * height;
symbolsSkin.resize(size);
memcpy(symbolsSkin.data(), data, size);
skinWidth = width;
skinHeight = height;
result = true;
};
auto failureHandler = [&result](string const & reason)
{
LOG(LERROR, (reason));
result = false;
};
LoadSymbols(skinPathName, textureName, false /* convertToUV */,
definitionInserter, completionHandler, failureHandler);
return result;
}
} // namespace dp
|