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

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

#include "drape/utils/vertex_decl.hpp"
#include "drape/shader_def.hpp"
#include "drape/attribute_provider.hpp"
#include "drape/batcher.hpp"
#include "drape/glstate.hpp"
#include "drape/overlay_handle.hpp"
#include "drape/texture_manager.hpp"

#include "base/string_utils.hpp"

#include "std/vector.hpp"

namespace df
{

namespace
{

class StraightTextHandle : public TextHandle
{
  using TBase = TextHandle;

public:
  StraightTextHandle(FeatureID const & id, strings::UniString const & text,
                     dp::Anchor anchor, glsl::vec2 const & pivot,
                     glsl::vec2 const & pxSize, glsl::vec2 const & offset,
                     uint64_t priority, ref_ptr<dp::TextureManager> textureManager,
                     bool isOptional, bool affectedByZoomPriority,
                     gpu::TTextDynamicVertexBuffer && normals, bool isBillboard = false)
    : TextHandle(id, text, anchor, priority, textureManager, move(normals), isBillboard)
    , m_pivot(glsl::ToPoint(pivot))
    , m_offset(glsl::ToPoint(offset))
    , m_size(glsl::ToPoint(pxSize))
    , m_isOptional(isOptional)
    , m_affectedByZoomPriority(affectedByZoomPriority)
  {}

  m2::PointD GetPivot(ScreenBase const & screen, bool perspective) const override
  {
    m2::PointD pivot = TBase::GetPivot(screen, false);
    if (perspective)
      pivot = screen.PtoP3d(pivot - m_offset, -m_pivotZ) + m_offset;
    return pivot;
  }

  m2::RectD GetPixelRect(ScreenBase const & screen, bool perspective) const override
  {
    if (perspective)
    {
      if (IsBillboard())
      {
        m2::PointD const pxPivot = screen.GtoP(m_pivot);
        m2::PointD const pxPivotPerspective = screen.PtoP3d(pxPivot, -m_pivotZ);

        m2::RectD pxRectPerspective = GetPixelRect(screen, false);
        pxRectPerspective.Offset(-pxPivot);
        pxRectPerspective.Offset(pxPivotPerspective);

        return pxRectPerspective;
      }
      return GetPixelRectPerspective(screen);
    }

    m2::PointD pivot = screen.GtoP(m_pivot) + m_offset;
    double x = pivot.x;
    double y = pivot.y;
    if (m_anchor & dp::Left)
      x += m_size.x;
    else if (m_anchor & dp::Right)
      x -= m_size.x;
    else
    {
      float halfWidth = m_size.x / 2.0f;
      x += halfWidth;
      pivot.x -= halfWidth;
    }

    if (m_anchor & dp::Top)
      y += m_size.y;
    else if (m_anchor & dp::Bottom)
      y -= m_size.y;
    else
    {
      float halfHeight = m_size.y / 2.0f;
      y += halfHeight;
      pivot.y -= halfHeight;
    }

    return m2::RectD(min(x, pivot.x), min(y, pivot.y),
                     max(x, pivot.x), max(y, pivot.y));
  }

  void GetPixelShape(ScreenBase const & screen, bool perspective, Rects & rects) const override
  {
    rects.emplace_back(GetPixelRect(screen, perspective));
  }

  uint64_t GetPriorityMask() const override
  {
    if (!m_affectedByZoomPriority)
      return dp::kPriorityMaskManual | dp::kPriorityMaskRank;

    return dp::kPriorityMaskAll;
  }

  bool IsBound() const override
  {
    return !m_isOptional;
  }

private:
  m2::PointF m_pivot;
  m2::PointF m_offset;
  m2::PointF m_size;
  bool m_isOptional;
  bool m_affectedByZoomPriority;
};

} // namespace

TextShape::TextShape(m2::PointF const & basePoint, TextViewParams const & params, bool hasPOI,
                     size_t textIndex, bool affectedByZoomPriority, int displacementMode,
                     uint16_t specialModePriority)
  : m_basePoint(basePoint)
  , m_params(params)
  , m_hasPOI(hasPOI)
  , m_affectedByZoomPriority(affectedByZoomPriority)
  , m_textIndex(textIndex)
  , m_displacementMode(displacementMode)
  , m_specialModePriority(specialModePriority)
{}

void TextShape::Draw(ref_ptr<dp::Batcher> batcher, ref_ptr<dp::TextureManager> textures) const
{
  ASSERT(!m_params.m_primaryText.empty(), ());
  StraightTextLayout primaryLayout(strings::MakeUniString(m_params.m_primaryText),
                                   m_params.m_primaryTextFont.m_size, textures, m_params.m_anchor);
  glsl::vec2 primaryOffset = glsl::ToVec2(m_params.m_primaryOffset);

  if (!m_params.m_secondaryText.empty())
  {
    StraightTextLayout secondaryLayout(strings::MakeUniString(m_params.m_secondaryText),
                                       m_params.m_secondaryTextFont.m_size, textures, m_params.m_anchor);

    glsl::vec2 secondaryOffset = primaryOffset;

    if (m_params.m_anchor & dp::Top)
      secondaryOffset += glsl::vec2(0.0, primaryLayout.GetPixelSize().y);
    else if (m_params.m_anchor & dp::Bottom)
      primaryOffset -= glsl::vec2(0.0, secondaryLayout.GetPixelSize().y);
    else
    {
      primaryOffset -= glsl::vec2(0.0, primaryLayout.GetPixelSize().y / 2.0f);
      secondaryOffset += glsl::vec2(0.0, secondaryLayout.GetPixelSize().y / 2.0f);
    }

    if (secondaryLayout.GetGlyphCount() > 0)
      DrawSubString(secondaryLayout, m_params.m_secondaryTextFont, secondaryOffset, batcher,
                    textures, false /* isPrimary */, m_params.m_secondaryOptional);
  }

  if (primaryLayout.GetGlyphCount() > 0)
    DrawSubString(primaryLayout, m_params.m_primaryTextFont, primaryOffset, batcher,
                  textures, true /* isPrimary */, m_params.m_primaryOptional);
}

void TextShape::DrawSubString(StraightTextLayout const & layout, dp::FontDecl const & font,
                              glm::vec2 const & baseOffset, ref_ptr<dp::Batcher> batcher,
                              ref_ptr<dp::TextureManager> textures,
                              bool isPrimary, bool isOptional) const
{
  dp::Color outlineColor = isPrimary ? m_params.m_primaryTextFont.m_outlineColor
                                     : m_params.m_secondaryTextFont.m_outlineColor;

  if (outlineColor == dp::Color::Transparent())
  {
    DrawSubStringPlain(layout, font, baseOffset,batcher, textures, isPrimary, isOptional);
  }
  else
  {
    DrawSubStringOutlined(layout, font, baseOffset, batcher, textures, isPrimary, isOptional);
  }
}

void TextShape::DrawSubStringPlain(StraightTextLayout const & layout, dp::FontDecl const & font,
                                   glm::vec2 const & baseOffset, ref_ptr<dp::Batcher> batcher,
                                   ref_ptr<dp::TextureManager> textures, bool isPrimary, bool isOptional) const
{
  gpu::TTextStaticVertexBuffer staticBuffer;
  gpu::TTextDynamicVertexBuffer dynamicBuffer;

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

  glsl::vec2 const pt = glsl::ToVec2(ConvertToLocal(m_basePoint, m_params.m_tileCenter, kShapeCoordScalar));
  layout.Cache(glsl::vec4(pt, m_params.m_depth, -m_params.m_posZ),
               baseOffset, color, staticBuffer, dynamicBuffer);

  dp::GLState state(gpu::TEXT_PROGRAM, dp::GLState::OverlayLayer);
  state.SetProgram3dIndex(gpu::TEXT_BILLBOARD_PROGRAM);
  ASSERT(color.GetTexture() == outline.GetTexture(), ());
  state.SetColorTexture(color.GetTexture());
  state.SetMaskTexture(layout.GetMaskTexture());

  gpu::TTextDynamicVertexBuffer initialDynBuffer(dynamicBuffer.size());

  m2::PointU const & pixelSize = layout.GetPixelSize();

  drape_ptr<dp::OverlayHandle> handle = make_unique_dp<StraightTextHandle>(m_params.m_featureID,
                                                                           layout.GetText(),
                                                                           m_params.m_anchor,
                                                                           glsl::ToVec2(m_basePoint),
                                                                           glsl::vec2(pixelSize.x, pixelSize.y),
                                                                           baseOffset,
                                                                           GetOverlayPriority(),
                                                                           textures,
                                                                           isOptional,
                                                                           m_affectedByZoomPriority,
                                                                           move(dynamicBuffer),
                                                                           true);
  handle->SetDisplacementMode(m_displacementMode);
  handle->SetPivotZ(m_params.m_posZ);
  handle->SetOverlayRank(m_hasPOI ? (isPrimary ? dp::OverlayRank1 : dp::OverlayRank2) : dp::OverlayRank0);
  handle->SetExtendingSize(m_params.m_extendingSize);

  dp::AttributeProvider provider(2, staticBuffer.size());
  provider.InitStream(0, gpu::TextStaticVertex::GetBindingInfo(), make_ref(staticBuffer.data()));
  provider.InitStream(1, gpu::TextDynamicVertex::GetBindingInfo(), make_ref(initialDynBuffer.data()));
  batcher->InsertListOfStrip(state, make_ref(&provider), move(handle), 4);
}

void TextShape::DrawSubStringOutlined(StraightTextLayout const & layout, dp::FontDecl const & font,
                                      glm::vec2 const & baseOffset, ref_ptr<dp::Batcher> batcher,
                                      ref_ptr<dp::TextureManager> textures, bool isPrimary, bool isOptional) const
{
  gpu::TTextOutlinedStaticVertexBuffer staticBuffer;
  gpu::TTextDynamicVertexBuffer dynamicBuffer;

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

  glsl::vec2 const pt = glsl::ToVec2(ConvertToLocal(m_basePoint, m_params.m_tileCenter, kShapeCoordScalar));
  layout.Cache(glsl::vec4(pt, m_params.m_depth, -m_params.m_posZ),
               baseOffset, color, outline, staticBuffer, dynamicBuffer);

  dp::GLState state(gpu::TEXT_OUTLINED_PROGRAM, dp::GLState::OverlayLayer);
  state.SetProgram3dIndex(gpu::TEXT_OUTLINED_BILLBOARD_PROGRAM);
  ASSERT(color.GetTexture() == outline.GetTexture(), ());
  state.SetColorTexture(color.GetTexture());
  state.SetMaskTexture(layout.GetMaskTexture());

  gpu::TTextDynamicVertexBuffer initialDynBuffer(dynamicBuffer.size());

  m2::PointU const & pixelSize = layout.GetPixelSize();

  drape_ptr<dp::OverlayHandle> handle = make_unique_dp<StraightTextHandle>(m_params.m_featureID,
                                                                           layout.GetText(),
                                                                           m_params.m_anchor,
                                                                           glsl::ToVec2(m_basePoint),
                                                                           glsl::vec2(pixelSize.x, pixelSize.y),
                                                                           baseOffset,
                                                                           GetOverlayPriority(),
                                                                           textures,
                                                                           isOptional,
                                                                           m_affectedByZoomPriority,
                                                                           move(dynamicBuffer),
                                                                           true);
  handle->SetDisplacementMode(m_displacementMode);
  handle->SetPivotZ(m_params.m_posZ);
  handle->SetOverlayRank(m_hasPOI ? (isPrimary ? dp::OverlayRank1 : dp::OverlayRank2) : dp::OverlayRank0);
  handle->SetExtendingSize(m_params.m_extendingSize);

  dp::AttributeProvider provider(2, staticBuffer.size());
  provider.InitStream(0, gpu::TextOutlinedStaticVertex::GetBindingInfo(), make_ref(staticBuffer.data()));
  provider.InitStream(1, gpu::TextDynamicVertex::GetBindingInfo(), make_ref(initialDynBuffer.data()));
  batcher->InsertListOfStrip(state, make_ref(&provider), move(handle), 4);
}

uint64_t TextShape::GetOverlayPriority() const
{
  // Set up maximum priority for shapes which created by user in the editor and in case of disabling
  // displacement.
  if (m_params.m_createdByEditor || m_disableDisplacing)
    return dp::kPriorityMaskAll;

  // Special displacement mode.
  if ((m_displacementMode & dp::displacement::kDefaultMode) == 0)
    return dp::CalculateSpecialModePriority(m_specialModePriority);

  // Set up minimal priority for shapes which belong to areas
  if (m_params.m_hasArea)
    return 0;

  // Overlay priority for text shapes considers length of the primary text
  // (the more text length, the more priority) and index of text.
  // [6 bytes - standard overlay priority][1 byte - length][1 byte - text index].
  static uint64_t constexpr kMask = ~static_cast<uint64_t>(0xFFFF);
  uint64_t priority = dp::CalculateOverlayPriority(m_params.m_minVisibleScale, m_params.m_rank, m_params.m_depth);
  priority &= kMask;
  priority |= (static_cast<uint8_t>(m_params.m_primaryText.size()) << 8);
  priority |= static_cast<uint8_t>(m_textIndex);

  return priority;
}

} //end of df namespace