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

path_text_shape.cpp « drape_frontend - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5c7c9b977e599526668ac0b7a378ad67e2ab059c (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
#include "path_text_shape.hpp"
#include "text_layout.hpp"
#include "visual_params.hpp"
#include "intrusive_vector.hpp"

#include "../drape/shader_def.hpp"
#include "../drape/attribute_provider.hpp"
#include "../drape/glstate.hpp"
#include "../drape/batcher.hpp"

#include "../base/math.hpp"
#include "../base/logging.hpp"
#include "../base/stl_add.hpp"
#include "../base/string_utils.hpp"
#include "../base/timer.hpp"
#include "../base/matrix.hpp"

#include "../geometry/transformations.hpp"

#include "../std/algorithm.hpp"
#include "../std/vector.hpp"

using m2::Spline;

namespace
{
  class PathTextHandle : public dp::OverlayHandle
  {
  public:
    PathTextHandle(m2::SharedSpline const & spl,
                   df::SharedTextLayout const & layout,
                   float const mercatorOffset,
                   float const depth)
      : OverlayHandle(FeatureID(), dp::Center, depth)
      , m_spline(spl)
      , m_layout(layout)
    {
      m_centerPointIter = m_spline.CreateIterator();
      m_centerPointIter.Advance(mercatorOffset);
      m_normals.resize(4 * m_layout->GetGlyphCount());
    }

    void Update(ScreenBase const & screen)
    {
      if (m_layout->CacheDynamicGeometry(m_centerPointIter, screen, m_normals))
      {
        SetIsValid(true);
        return;
      }

      SetIsValid(false);
    }

    m2::RectD GetPixelRect(ScreenBase const & screen) const
    {
      ASSERT(IsValid(), ());

      m2::PointD pixelPivot(screen.GtoP(m_centerPointIter.m_pos));
      m2::RectD result;
      for (gpu::TextDynamicVertex const & v : m_normals)
        result.Add(pixelPivot + glsl::ToPoint(v.m_normal));

      return result;
    }

    void GetPixelShape(ScreenBase const & screen, Rects & rects) const
    {
      ASSERT(IsValid(), ());

      m2::PointD pixelPivot(screen.GtoP(m_centerPointIter.m_pos));
      for (size_t quadIndex = 0; quadIndex < m_normals.size(); quadIndex += 4)
      {
        m2::RectF r;
        r.Add(pixelPivot + glsl::ToPoint(m_normals[quadIndex].m_normal));
        r.Add(pixelPivot + glsl::ToPoint(m_normals[quadIndex + 1].m_normal));
        r.Add(pixelPivot + glsl::ToPoint(m_normals[quadIndex + 2].m_normal));
        r.Add(pixelPivot + glsl::ToPoint(m_normals[quadIndex + 3].m_normal));
        rects.push_back(r);
      }
    }

    void GetAttributeMutation(dp::RefPointer<dp::AttributeBufferMutator> mutator, ScreenBase const & screen) const
    {
      ASSERT(IsValid(), ());

      TOffsetNode const & node = GetOffsetNode(gpu::TextDynamicVertex::GetDynamicStreamID());
      ASSERT(node.first.GetElementSize() == sizeof(gpu::TextDynamicVertex), ());
      ASSERT(node.second.m_count == m_normals.size(), ());

      uint32_t byteCount = m_normals.size() * sizeof(gpu::TextDynamicVertex);
      void * buffer = mutator->AllocateMutationBuffer(byteCount);
      memcpy(buffer, m_normals.data(), byteCount);
      dp::MutateNode mutateNode;
      mutateNode.m_region = node.second;
      mutateNode.m_data = dp::MakeStackRefPointer(buffer);
      mutator->AddMutation(node.first, mutateNode);
    }

  private:
    m2::SharedSpline m_spline;
    m2::Spline::iterator m_centerPointIter;
    gpu::TTextDynamicVertexBuffer m_normals;

    df::SharedTextLayout m_layout;
  };
}

namespace df
{

PathTextShape::PathTextShape(m2::SharedSpline const & spline,
                             PathTextViewParams const & params)
  : m_spline(spline)
  , m_params(params)
{
}

void PathTextShape::Draw(dp::RefPointer<dp::Batcher> batcher, dp::RefPointer<dp::TextureManager> textures) const
{
  PathTextLayout * layout = new PathTextLayout(strings::MakeUniString(m_params.m_text),
                                               m_params.m_textFont.m_size,
                                               textures);

  uint32_t glyphCount = layout->GetGlyphCount();
  if (glyphCount == 0)
    return;

  //we leave a little space on either side of the text that would
  //remove the comparison for equality of spline portions
  float const TextBorder = 4.0f;
  float const textLength = TextBorder + layout->GetPixelLength();
  float const textHalfLength = textLength / 2.0f;
  float const pathGlbLength = m_spline->GetLength();

  // on next readable scale m_scaleGtoP will be twice
  if (textLength > pathGlbLength * 2 * m_params.m_baseGtoPScale)
    return;

  float const pathLength = m_params.m_baseGtoPScale * m_spline->GetLength();

  /// copied from old code
  /// @todo Choose best constant for minimal space.
  float const etalonEmpty = max(200 * df::VisualParams::Instance().GetVisualScale(), (double)textLength);
  float const minPeriodSize = etalonEmpty + textLength;
  float const twoTextAndEmpty = minPeriodSize + textLength;

  buffer_vector<float, 32> offsets;

  float const scalePtoG = 1.0f / m_params.m_baseGtoPScale;

  if (pathLength < twoTextAndEmpty)
  {
    // if we can't place 2 text and empty part on path
    // we place only one text on center of path
    offsets.push_back(pathGlbLength / 2.0f);

  }
  else if (pathLength < twoTextAndEmpty + minPeriodSize)
  {
    // if we can't place 3 text and 2 empty path
    // we place 2 text with empty space beetwen
    // and some offset from path end
    float const endOffset = (pathLength - (2 * textLength + etalonEmpty)) / 2;

    // division on m_scaleGtoP give as global coord frame (Mercator)
    offsets.push_back((endOffset + textHalfLength) * scalePtoG);
    offsets.push_back((pathLength - (textHalfLength + endOffset)) * scalePtoG);
  }
  else
  {
    // here we place 2 text on the ends of path
    // then we place as much as possible text on center path uniformly
    offsets.push_back(textHalfLength * scalePtoG);
    offsets.push_back((pathLength - textHalfLength) * scalePtoG);
    float const emptySpace = pathLength - 2 * textLength;
    uint32_t textCount = static_cast<uint32_t>(ceil(emptySpace / minPeriodSize));
    float const offset = (emptySpace - textCount * textLength) / (textCount + 1);
    for (size_t i = 0; i < textCount; ++i)
      offsets.push_back((textHalfLength + (textLength + offset) * (i + 1)) * scalePtoG);
  }

  dp::TextureManager::ColorRegion color;
  dp::TextureManager::ColorRegion outline;
  textures->GetColorRegion(m_params.m_textFont.m_color, color);
  textures->GetColorRegion(m_params.m_textFont.m_outlineColor, outline);

  dp::GLState state(gpu::TEXT_PROGRAM, dp::GLState::OverlayLayer);
  state.SetBlending(dp::Blending(true));
  state.SetColorTexture(color.GetTexture());
  state.SetMaskTexture(layout->GetMaskTexture());

  ASSERT(!offsets.empty(), ());
  gpu::TTextStaticVertexBuffer staticBuffer;
  gpu::TTextDynamicVertexBuffer dynBuffer;
  SharedTextLayout layoutPtr(layout);
  for (float offset : offsets)
  {
    staticBuffer.clear();
    dynBuffer.clear();

    Spline::iterator iter = m_spline.CreateIterator();
    iter.Advance(offset);
    layout->CacheStaticGeometry(glsl::vec3(glsl::ToVec2(iter.m_pos), m_params.m_depth),
                                color, outline, staticBuffer);

    dynBuffer.resize(staticBuffer.size(), gpu::TextDynamicVertex(glsl::vec2(0.0, 0.0)));

    dp::AttributeProvider provider(2, staticBuffer.size());
    provider.InitStream(0, gpu::TextStaticVertex::GetBindingInfo(), dp::MakeStackRefPointer<void>(staticBuffer.data()));
    provider.InitStream(1, gpu::TextDynamicVertex::GetBindingInfo(), dp::MakeStackRefPointer<void>(dynBuffer.data()));

    dp::OverlayHandle * handle = new PathTextHandle(m_spline, layoutPtr, offset, m_params.m_depth);
    batcher->InsertListOfStrip(state, dp::MakeStackRefPointer(&provider), dp::MovePointer(handle), 4);
  }
}

}