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: f9832f0df353eb8ee6a76e47e0eae4863017392f (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
#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 "../drape/texture_set_holder.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;
using glsl_types::vec2;

namespace
{
  struct AccumulateRect
  {
    ScreenBase const & m_screen;
    m2::RectD m_pixelRect;
    AccumulateRect(ScreenBase const & screen)
      : m_screen(screen)
    {
    }

    void operator()(m2::PointF const & pt)
    {
      m_pixelRect.Add(m_screen.GtoP(pt));
    }

  };

  class PathTextHandle : public dp::OverlayHandle
  {
  public:
    static const uint8_t PathGlyphPositionID = 1;

    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_splineOffset(mercatorOffset)
    {
    }

    void Update(ScreenBase const & screen)
    {
      m_scalePtoG = screen.GetScale();
      GetBegEnd(m_begin, m_end);

      SetIsValid(!m_begin.BeginAgain() && !m_end.BeginAgain());
      if (!IsValid())
        return;

      if (screen.GtoP(m_end.m_pos).x < screen.GtoP(m_begin.m_pos).x)
        m_isForward = false;
      else
        m_isForward = true;
    }

    m2::RectD GetPixelRect(ScreenBase const & screen) const
    {
      ASSERT(IsValid(), ());
      ASSERT(!m_begin.BeginAgain(), ());
      ASSERT(!m_end.BeginAgain(), ());

      AccumulateRect f(screen);
      m_spline->ForEachNode(m_begin, m_end, f);
      float const pixelHeight = m_layout->GetPixelHeight();
      f.m_pixelRect.Inflate(2 * pixelHeight, 2 * pixelHeight);
      return f.m_pixelRect;
    }


    void GetPixelShape(ScreenBase const & screen, Rects & rects) const
    {
      rects = m_bboxes;
    }

    void GetAttributeMutation(dp::RefPointer<dp::AttributeBufferMutator> mutator, ScreenBase const & screen) const
    {
      ASSERT(IsValid(), ());
      uint32_t byteCount = 4 * m_layout->GetGlyphCount() * sizeof(glsl_types::vec2);
      void * buffer = mutator->AllocateMutationBuffer(byteCount);
      df::IntrusiveVector<glsl_types::vec2> positions(buffer, byteCount);
      // m_splineOffset set offset to Center of text.
      // By this we calc offset for start of text in mercator
      m_layout->LayoutPathText(m_begin, m_scalePtoG, positions, m_isForward, m_bboxes, screen);

      TOffsetNode const & node = GetOffsetNode(PathGlyphPositionID);
      dp::MutateNode mutateNode;
      mutateNode.m_region = node.second;
      mutateNode.m_data = dp::MakeStackRefPointer<void>(buffer);
      mutator->AddMutation(node.first, mutateNode);
    }

  private:
    void GetBegEnd(Spline::iterator & beg, Spline::iterator & end) const
    {
      beg = m_spline.CreateIterator();
      end = m_spline.CreateIterator();
      float const textLength = m_layout->GetPixelLength() * m_scalePtoG;
      float const step = max(0.0f, m_splineOffset - textLength / 2.0f);
      if (step > 0.0f)
        beg.Step(step);
      end.Step(step + textLength);
    }

  private:
    m2::SharedSpline m_spline;
    m2::Spline::iterator m_begin;
    m2::Spline::iterator m_end;
    mutable Rects m_bboxes;

    df::SharedTextLayout m_layout;
    float m_scalePtoG;
    float m_splineOffset;
    bool m_isForward;
  };

  void BatchPathText(m2::SharedSpline const & spline,
                      buffer_vector<float, 32> const & offsets,
                      float depth,
                      dp::RefPointer<dp::Batcher> batcher,
                      df::SharedTextLayout const & layout,
                      vector<glsl_types::vec2> & positions,
                      vector<glsl_types::Quad4> & texCoord,
                      vector<glsl_types::Quad4> & fontColor,
                      vector<glsl_types::Quad4> & outlineColor)
  {
    ASSERT(!offsets.empty(), ());
    layout->InitPathText(depth, texCoord, fontColor, outlineColor);

    dp::GLState state(gpu::PATH_FONT_PROGRAM, dp::GLState::OverlayLayer);
    state.SetTextureSet(layout->GetTextureSet());
    state.SetBlending(dp::Blending(true));

    for (size_t i = 0; i < offsets.size(); ++i)
    {
      dp::AttributeProvider provider(4, 4 * layout->GetGlyphCount());
      {
        dp::BindingInfo positionBind(1, PathTextHandle::PathGlyphPositionID);
        dp::BindingDecl & decl = positionBind.GetBindingDecl(0);
        decl.m_attributeName = "a_position";
        decl.m_componentCount = 2;
        decl.m_componentType = gl_const::GLFloatType;
        decl.m_offset = 0;
        decl.m_stride = 0;
        provider.InitStream(0, positionBind, dp::MakeStackRefPointer(&positions[0]));
      }
      {
        dp::BindingInfo texCoordBind(1);
        dp::BindingDecl & decl = texCoordBind.GetBindingDecl(0);
        decl.m_attributeName = "a_texcoord";
        decl.m_componentCount = 4;
        decl.m_componentType = gl_const::GLFloatType;
        decl.m_offset = 0;
        decl.m_stride = 0;
        provider.InitStream(1, texCoordBind, dp::MakeStackRefPointer(&texCoord[0]));
      }
      {
        dp::BindingInfo fontColorBind(1);
        dp::BindingDecl & decl = fontColorBind.GetBindingDecl(0);
        decl.m_attributeName = "a_color";
        decl.m_componentCount = 4;
        decl.m_componentType = gl_const::GLFloatType;
        decl.m_offset = 0;
        decl.m_stride = 0;
        provider.InitStream(2, fontColorBind, dp::MakeStackRefPointer(&fontColor[0]));
      }
      {
        dp::BindingInfo outlineColorBind(1);
        dp::BindingDecl & decl = outlineColorBind.GetBindingDecl(0);
        decl.m_attributeName = "a_outline_color";
        decl.m_componentCount = 4;
        decl.m_componentType = gl_const::GLFloatType;
        decl.m_offset = 0;
        decl.m_stride = 0;
        provider.InitStream(3, outlineColorBind, dp::MakeStackRefPointer(&outlineColor[0]));
      }

      dp::OverlayHandle * handle = new PathTextHandle(spline, layout, offsets[i], depth);
      batcher->InsertListOfStrip(state, dp::MakeStackRefPointer(&provider), dp::MovePointer(handle), 4);
    }
  }
}

namespace df
{

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

void PathTextShape::Draw(dp::RefPointer<dp::Batcher> batcher, dp::RefPointer<dp::TextureSetHolder> textures) const
{
  SharedTextLayout layout(new TextLayout(strings::MakeUniString(m_params.m_text),
                                         m_params.m_textFont,
                                         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_scaleGtoP)
    return;

  float const pathLength = m_scaleGtoP * 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;

  vector<glsl_types::vec2>  positions(glyphCount, vec2(0.0, 0.0));
  vector<glsl_types::Quad4> texCoords(glyphCount);
  vector<glsl_types::Quad4> fontColor(glyphCount);
  vector<glsl_types::Quad4> outlineColor(glyphCount);
  buffer_vector<float, 32> offsets;

  float const scalePtoG = 1.0f / m_scaleGtoP;

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

  BatchPathText(m_spline, offsets, m_params.m_depth, batcher, layout,
                positions, texCoords, fontColor, outlineColor);
}

}