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

stylist.cpp « drape_frontend - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 54df0b0f8acb273ca4b69e9c488b18fe42062852 (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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include "drape_frontend/stylist.hpp"

#include "indexer/feature.hpp"
#include "indexer/feature_visibility.hpp"
#include "indexer/ftypes_matcher.hpp"
#include "indexer/drawing_rules.hpp"
#include "indexer/drules_include.hpp"
#include "indexer/scales.hpp"

#include "std/limits.hpp"

namespace df
{

namespace
{

enum Type
{
  Line      =               1,
  Area      = Line       << 1,
  Symbol    = Area       << 1,
  Caption   = Symbol     << 1,
  Circle    = Caption    << 1,
  PathText  = Circle     << 1,
  Waymarker = PathText   << 1,
  Shield    = Waymarker  << 1,
  CountOfType = PathText + 1
};

inline drule::rule_type_t Convert(Type t)
{
  switch (t)
  {
  case Line     : return drule::line;
  case Area     : return drule::area;
  case Symbol   : return drule::symbol;
  case Caption  : return drule::caption;
  case Circle   : return drule::circle;
  case PathText : return drule::pathtext;
  case Waymarker: return drule::waymarker;
  case Shield   : return drule::shield;
  default:
    return drule::count_of_rules;
  }
}

double constexpr kMinPriority = numeric_limits<double>::lowest();

// ==================================== //

inline bool IsTypeOf(drule::Key const & key, int flags)
{
  int currentFlag = Line;
  while (currentFlag < CountOfType)
  {
    Type type = Type(flags & currentFlag);
    if (type != 0 && key.m_type == Convert(type))
      return true;

    currentFlag <<= 1;
  }

  return false;
}

bool IsMiddleTunnel(int const layer, double const depth)
{
  return layer != feature::LAYER_EMPTY && depth < 19000;
}

void FilterRulesByRuntimeSelector(FeatureType const & f, int zoomLevel, drule::KeysT & keys)
{
  keys.erase_if([&f, zoomLevel](drule::Key const & key)->bool
  {
    drule::BaseRule const * const rule = drule::rules().Find(key);
    ASSERT(rule != nullptr, ());
    return !rule->TestFeature(f, zoomLevel);
  });
}

class KeyFunctor
{
public:
  KeyFunctor(FeatureType const & f,
             feature::EGeomType type,
             int const zoomLevel,
             int const keyCount,
             CaptionDescription & descr)
    : m_pointStyleFound(false)
    , m_lineStyleFound(false)
    , m_iconFound(false)
    , m_captionWithoutOffsetFound(false)
    , m_auxCaptionFound(false)
    , m_mainTextType(drule::text_type_name)
    , m_descrInit(false)
    , m_f(f)
    , m_geomType(type)
    , m_zoomLevel(zoomLevel)
    , m_descr(descr)
  {
    m_rules.reserve(keyCount);
    Init();
  }

  void ProcessKey(drule::Key const & key)
  {
    double depth = key.m_priority;
    if (IsMiddleTunnel(m_depthLayer, depth) &&
        IsTypeOf(key, Line | Area | Waymarker))
    {
      double const layerPart = m_depthLayer * drule::layer_base_priority;
      double const depthPart = fmod(depth, drule::layer_base_priority);
      depth = layerPart + depthPart;
    }

    if (IsTypeOf(key, Caption | Symbol | Circle | PathText))
    {
      depth += m_priorityModifier;
      if (m_geomType == feature::GEOM_POINT) ++depth;
    }
    else if (IsTypeOf(key, Area))
      depth -= m_priorityModifier;

    drule::BaseRule const * const dRule = drule::rules().Find(key);
    m_rules.push_back(make_pair(dRule, depth));

    if (dRule->GetCaption(0) != nullptr)
    {
      InitCaptionDescription();
      m_mainTextType = dRule->GetCaptionTextType(0);
    }

    bool const isNonEmptyCaption = IsTypeOf(key, Caption) && IsNameExists();
    m_pointStyleFound |= (IsTypeOf(key, Symbol | Circle) || isNonEmptyCaption);
    m_lineStyleFound  |= IsTypeOf(key, Line);
    m_auxCaptionFound |= (dRule->GetCaption(1) != nullptr);
  }

  bool m_pointStyleFound;
  bool m_lineStyleFound;
  bool m_iconFound;
  bool m_captionWithoutOffsetFound;
  bool m_auxCaptionFound;
  buffer_vector<Stylist::TRuleWrapper, 8> m_rules;
  drule::text_type_t m_mainTextType;
  bool m_descrInit;

private:
  void Init()
  {
    m_depthLayer = m_f.GetLayer();
    if (m_depthLayer == feature::LAYER_TRANSPARENT_TUNNEL)
      m_depthLayer = feature::LAYER_EMPTY;

    if (m_geomType == feature::GEOM_POINT)
      m_priorityModifier = (double)m_f.GetPopulation() / 7E9;
    else
    {
      m2::RectD const r = m_f.GetLimitRect(m_zoomLevel);
      m_priorityModifier = min(1.0, r.SizeX() * r.SizeY() * 10000.0);
    }
  }

  void InitCaptionDescription()
  {
    if (!m_descrInit)
    {
      m_descr.Init(m_f, m_zoomLevel);
      m_descrInit = true;
    }
  }

  inline bool IsNameExists() const
  {
    ASSERT(m_descrInit, ());
    return m_descr.IsNameExists();
  }

private:
  FeatureType const & m_f;
  feature::EGeomType m_geomType;
  int const m_zoomLevel;
  double m_priorityModifier;
  int m_depthLayer;
  CaptionDescription & m_descr;
};

const uint8_t CoastlineFlag  = 1;
const uint8_t AreaStyleFlag  = 1 << 1;
const uint8_t LineStyleFlag  = 1 << 2;
const uint8_t PointStyleFlag = 1 << 3;

} // namespace

// ==================================== //

void CaptionDescription::Init(FeatureType const & f,
                              int const zoomLevel)
{
  f.GetPreferredNames(m_mainText, m_auxText);

  m_roadNumber = f.GetRoadNumber();
  m_houseNumber = f.GetHouseNumber();

  SwapCaptions(zoomLevel);
  DiscardLongCaption(zoomLevel);
}

void CaptionDescription::FormatCaptions(FeatureType const & f,
                                        feature::EGeomType type,
                                        drule::text_type_t mainTextType,
                                        bool auxCaptionExists)
{
  if (!auxCaptionExists && !m_auxText.empty() && type != feature::GEOM_LINE)
  {
    m_mainText.swap(m_auxText);
    m_auxText.clear();
  }

  if (mainTextType == drule::text_type_housenumber)
  {
    m_mainText.swap(m_houseNumber);
    m_houseNumber.clear();
  }
  else if (mainTextType == drule::text_type_name)
  {
    if (!m_houseNumber.empty())
    {
      if (m_mainText.empty() || m_houseNumber.find(m_mainText) != string::npos)
        m_houseNumber.swap(m_mainText);
    }
  }
}

string const & CaptionDescription::GetMainText() const
{
  return m_mainText;
}

string const & CaptionDescription::GetAuxText() const
{
  return m_auxText;
}

string const & CaptionDescription::GetRoadNumber() const
{
  return m_roadNumber;
}

string CaptionDescription::GetPathName() const
{
  // Always concat names for linear features because we process only one draw rule now.
  if (m_mainText.empty())
    return m_mainText;
  else
    return m_mainText + "   " + m_auxText;
}

bool CaptionDescription::IsNameExists() const
{
  return !m_mainText.empty() || !m_houseNumber.empty();
}

void CaptionDescription::SwapCaptions(int const zoomLevel)
{
  if (zoomLevel <= scales::GetUpperWorldScale() && !m_auxText.empty())
  {
    m_mainText.swap(m_auxText);
    m_auxText.clear();
  }
}

void CaptionDescription::DiscardLongCaption(int const zoomLevel)
{
  if (zoomLevel < 5 && m_mainText.size() > 50)
    m_mainText.clear();
}

// ==================================== //

Stylist::Stylist()
  : m_state(0)
{
}

bool Stylist::IsCoastLine() const
{
  return (m_state & CoastlineFlag) != 0;
}

bool Stylist::AreaStyleExists() const
{
  return (m_state & AreaStyleFlag) != 0;
}

bool Stylist::LineStyleExists() const
{
  return (m_state & LineStyleFlag) != 0;
}

bool Stylist::PointStyleExists() const
{
  return (m_state & PointStyleFlag) != 0;
}

CaptionDescription const & Stylist::GetCaptionDescription() const
{
  return m_captionDescriptor;
}

void Stylist::ForEachRule(Stylist::TRuleCallback const & fn) const
{
  typedef rules_t::const_iterator const_iter;
  for (const_iter it = m_rules.begin(); it != m_rules.end(); ++it)
    fn(*it);
}

bool Stylist::IsEmpty() const
{
  return m_rules.empty();
}

void Stylist::RaiseCoastlineFlag()
{
  m_state |= CoastlineFlag;
}

void Stylist::RaiseAreaStyleFlag()
{
  m_state |= AreaStyleFlag;
}

void Stylist::RaiseLineStyleFlag()
{
  m_state |= LineStyleFlag;
}

void Stylist::RaisePointStyleFlag()
{
  m_state |= PointStyleFlag;
}

CaptionDescription & Stylist::GetCaptionDescriptionImpl()
{
  return m_captionDescriptor;
}

bool InitStylist(FeatureType const & f, int const zoomLevel, bool buildings3d, Stylist & s)
{
  feature::TypesHolder const types(f);

  if (!buildings3d && ftypes::IsBuildingPartChecker::Instance()(types) &&
      !ftypes::IsBuildingChecker::Instance()(types))
    return false;

  drule::KeysT keys;
  pair<int, bool> const geomType = feature::GetDrawRule(types, zoomLevel, keys);

  FilterRulesByRuntimeSelector(f, zoomLevel, keys);

  if (keys.empty())
    return false;

  drule::MakeUnique(keys);

  if (geomType.second)
    s.RaiseCoastlineFlag();

  feature::EGeomType mainGeomType = feature::EGeomType(geomType.first);

  switch (mainGeomType)
  {
  case feature::GEOM_POINT:
    s.RaisePointStyleFlag();
    break;
  case feature::GEOM_LINE :
    s.RaiseLineStyleFlag();
    break;
  case feature::GEOM_AREA :
    s.RaiseAreaStyleFlag();
    break;
  default:
    ASSERT(false, ());
    return false;
  }

  CaptionDescription & descr = s.GetCaptionDescriptionImpl();

  KeyFunctor keyFunctor(f, mainGeomType, zoomLevel, keys.size(), descr);
  for (auto const & key : keys)
    keyFunctor.ProcessKey(key);

  if (keyFunctor.m_pointStyleFound)
    s.RaisePointStyleFlag();
  if (keyFunctor.m_lineStyleFound)
    s.RaiseLineStyleFlag();

  s.m_rules.swap(keyFunctor.m_rules);

  if (keyFunctor.m_descrInit)
    descr.FormatCaptions(f, mainGeomType, keyFunctor.m_mainTextType, keyFunctor.m_auxCaptionFound);

  return true;
}

double GetFeaturePriority(FeatureType const & f, int const zoomLevel)
{
  drule::KeysT keys;
  pair<int, bool> const geomType = feature::GetDrawRule(f, zoomLevel, keys);

  FilterRulesByRuntimeSelector(f, zoomLevel, keys);

  feature::EGeomType const mainGeomType = feature::EGeomType(geomType.first);

  CaptionDescription descr;

  KeyFunctor keyFunctor(f, mainGeomType, zoomLevel, keys.size(), descr);
  for (auto const & key : keys)
    keyFunctor.ProcessKey(key);

  double maxPriority = kMinPriority;
  for (auto const & rule : keyFunctor.m_rules)
  {
    if (rule.second > maxPriority)
      maxPriority = rule.second;
  }

  return maxPriority;
}

} // namespace df