Welcome to mirror list, hosted at ThFree Co, Russian Federation.

symbols_texture.cpp « drape - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6197255d658cf26081b6090a148ff1df156dc25f (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
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
#include "drape/symbols_texture.hpp"
#include "drape/utils/stb_image.h"

#include "platform/platform.hpp"

#include "coding/reader.hpp"
#include "coding/parse_xml.hpp"

#include "base/string_utils.hpp"

namespace dp
{

class SymbolsTexture::DefinitionLoader
{
public:
  DefinitionLoader(SymbolsTexture::TSymDefinition & definition)
    : m_def(definition)
    , 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(), ());
      m_def.insert(make_pair(m_name, SymbolsTexture::SymbolInfo(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, ());
        m_rect.setMinX(v / (float)m_width);
      }
      else if (attribute == "minY")
      {
        ASSERT(m_height != 0, ());
        m_rect.setMinY(v / (float)m_height);
      }
      else if (attribute == "maxX")
      {
        ASSERT(m_width != 0, ());
        m_rect.setMaxX(v / (float)m_width);
      }
      else if (attribute == "maxY")
      {
        ASSERT(m_height != 0, ());
        m_rect.setMaxY(v / (float)m_height);
      }
      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:
  SymbolsTexture::TSymDefinition & m_def;
  uint32_t m_width;
  uint32_t m_height;

  string m_name;
  m2::RectF m_rect;

};

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

void SymbolsTexture::Load(string const & skinPathName)
{
  vector<unsigned char> rawData;
  uint32_t width, height;

  try
  {
    DefinitionLoader loader(m_definition);

    {
      ReaderPtr<Reader> reader = GetPlatform().GetReader(skinPathName + ".sdf");
      ReaderSource<ReaderPtr<Reader> > source(reader);
      if (!ParseXML(source, loader))
      {
        LOG(LERROR, ("Error parsing skin"));
        Fail();
        return;
      }

      width = loader.GetWidth();
      height = loader.GetHeight();
    }

    {
      ReaderPtr<Reader> reader = GetPlatform().GetReader(skinPathName + ".png");
      size_t const size = reader.Size();
      rawData.resize(size);
      reader.Read(0, &rawData[0], size);
    }
  }
  catch (RootException & e)
  {
    LOG(LERROR, (e.what()));
    Fail();
    return;
  }

  int w, h, bpp;
  unsigned char * data = stbi_png_load_from_memory(&rawData[0], rawData.size(), &w, &h, &bpp, 0);

  if (width == w && height == h)
    Create(width, height, RGBA8, MakeStackRefPointer<void>(data));
  else
    Fail();

  stbi_image_free(data);
}

RefPointer<Texture::ResourceInfo> SymbolsTexture::FindResource(Texture::Key const & key) const
{
  if (key.GetType() != Texture::Symbol)
    return RefPointer<ResourceInfo>();

  string const & symbolName = static_cast<SymbolKey const &>(key).GetSymbolName();

  TSymDefinition::iterator it = m_definition.find(symbolName);
  ASSERT(it != m_definition.end(), ());
  return MakeStackRefPointer<ResourceInfo>(&it->second);
}

void SymbolsTexture::Fail()
{
  m_definition.clear();
  int32_t alfaTexture = 0;
  Create(1, 1, RGBA8, MakeStackRefPointer(&alfaTexture));
}

} // namespace dp