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
|
#include "drape_frontend/path_text_shape.hpp"
#include "drape_frontend/text_handle.hpp"
#include "drape_frontend/text_layout.hpp"
#include "drape_frontend/intrusive_vector.hpp"
#include "drape/attribute_provider.hpp"
#include "drape/batcher.hpp"
#include "drape/glstate.hpp"
#include "drape/shader_def.hpp"
#include "drape/overlay_handle.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 df::TextHandle
{
public:
PathTextHandle(dp::OverlayID const & id, m2::SharedSpline const & spl,
df::SharedTextLayout const & layout,
float mercatorOffset, float depth, uint32_t textIndex,
uint64_t priority, uint64_t priorityFollowingMode, int fixedHeight,
ref_ptr<dp::TextureManager> textureManager,
bool isBillboard)
: TextHandle(id, layout->GetText(), dp::Center, priority, fixedHeight,
textureManager, isBillboard)
, m_spline(spl)
, m_layout(layout)
, m_textIndex(textIndex)
, m_globalOffset(mercatorOffset)
, m_depth(depth)
, m_priorityFollowingMode(priorityFollowingMode)
{
m2::Spline::iterator centerPointIter = m_spline.CreateIterator();
centerPointIter.Advance(m_globalOffset);
m_globalPivot = centerPointIter.m_pos;
m_buffer.resize(4 * m_layout->GetGlyphCount());
}
bool Update(ScreenBase const & screen) override
{
if (!df::TextHandle::Update(screen))
return false;
vector<m2::PointD> const & globalPoints = m_spline->GetPath();
m2::Spline pixelSpline(m_spline->GetSize());
m2::Spline::iterator centerPointIter;
if (screen.isPerspective())
{
// In perspective mode we draw the first label only.
if (m_textIndex != 0)
return false;
float pixelOffset = 0.0f;
bool foundOffset = false;
for (auto pos : globalPoints)
{
pos = screen.GtoP(pos);
if (!screen.PixelRect().IsPointInside(pos))
{
if ((foundOffset = CalculatePerspectiveOffsets(pixelSpline, pixelOffset)))
break;
pixelSpline = m2::Spline(m_spline->GetSize());
continue;
}
pixelSpline.AddPoint(screen.PtoP3d(pos));
}
// We aren't able to place the only label anywhere.
if (!foundOffset && !CalculatePerspectiveOffsets(pixelSpline, pixelOffset))
return false;
centerPointIter.Attach(pixelSpline);
centerPointIter.Advance(pixelOffset);
m_globalPivot = screen.PtoG(screen.P3dtoP(centerPointIter.m_pos));
}
else
{
for (auto const & pos : globalPoints)
pixelSpline.AddPoint(screen.GtoP(pos));
centerPointIter.Attach(pixelSpline);
centerPointIter.Advance(m_globalOffset / screen.GetScale());
m_globalPivot = screen.PtoG(centerPointIter.m_pos);
}
if (m_buffer.empty())
m_buffer.resize(4 * m_layout->GetGlyphCount());
return m_layout->CacheDynamicGeometry(centerPointIter, m_depth, m_globalPivot, m_buffer);
}
m2::RectD GetPixelRect(ScreenBase const & screen, bool perspective) const override
{
m2::PointD const pixelPivot(screen.GtoP(m_globalPivot));
if (perspective)
{
if (IsBillboard())
{
m2::RectD r = GetPixelRect(screen, false);
m2::PointD pixelPivotPerspective = screen.PtoP3d(pixelPivot);
r.Offset(-pixelPivot);
r.Offset(pixelPivotPerspective);
return r;
}
return GetPixelRectPerspective(screen);
}
m2::RectD result;
for (gpu::TextDynamicVertex const & v : m_buffer)
result.Add(pixelPivot + glsl::ToPoint(v.m_normal));
return result;
}
void GetPixelShape(ScreenBase const & screen, bool perspective, Rects & rects) const override
{
m2::PointD const pixelPivot(screen.GtoP(m_globalPivot));
for (size_t quadIndex = 0; quadIndex < m_buffer.size(); quadIndex += 4)
{
m2::RectF r;
r.Add(pixelPivot + glsl::ToPoint(m_buffer[quadIndex].m_normal));
r.Add(pixelPivot + glsl::ToPoint(m_buffer[quadIndex + 1].m_normal));
r.Add(pixelPivot + glsl::ToPoint(m_buffer[quadIndex + 2].m_normal));
r.Add(pixelPivot + glsl::ToPoint(m_buffer[quadIndex + 3].m_normal));
if (perspective)
{
if (IsBillboard())
{
m2::PointD const pxPivotPerspective = screen.PtoP3d(pixelPivot);
r.Offset(-pixelPivot);
r.Offset(pxPivotPerspective);
}
else
{
r = m2::RectF(GetPerspectiveRect(m2::RectD(r), screen));
}
}
bool const needAddRect = perspective ? !screen.IsReverseProjection3d(r.Center()) : true;
if (needAddRect)
rects.emplace_back(move(r));
}
}
void GetAttributeMutation(ref_ptr<dp::AttributeBufferMutator> mutator) const override
{
// for visible text paths we always update normals
SetForceUpdateNormals(IsVisible());
TextHandle::GetAttributeMutation(mutator);
}
uint64_t GetPriorityMask() const override
{
return dp::kPriorityMaskManual | dp::kPriorityMaskRank;
}
bool Enable3dExtention() const override
{
// Do not extend overlays for path texts.
return false;
}
uint64_t GetPriorityInFollowingMode() const override
{
return m_priorityFollowingMode;
}
bool HasLinearFeatureShape() const override
{
return true;
}
private:
bool CalculatePerspectiveOffsets(const m2::Spline & pixelSpline, float & pixelOffset) const
{
if (pixelSpline.GetSize() < 2)
return false;
float offset = 0.0f;
if (!df::PathTextLayout::CalculatePerspectivePosition(pixelSpline.GetLength(),
m_layout->GetPixelLength(), offset))
return false;
pixelOffset = offset;
return true;
}
private:
m2::SharedSpline m_spline;
df::SharedTextLayout m_layout;
uint32_t const m_textIndex;
m2::PointD m_globalPivot;
float const m_globalOffset;
float const m_depth;
uint64_t const m_priorityFollowingMode;
};
}
namespace df
{
PathTextShape::PathTextShape(m2::SharedSpline const & spline, PathTextViewParams const & params,
TileKey const & tileKey, uint32_t baseTextIndex)
: m_spline(spline)
, m_params(params)
, m_tileCoords(tileKey.GetTileCoords())
, m_baseTextIndex(baseTextIndex)
{}
uint64_t PathTextShape::GetOverlayPriority(uint32_t textIndex, bool followingMode) const
{
// Overlay priority for path text shapes considers length of the text and index of text.
// Greater text length has more priority, because smaller texts have more chances to be shown along the road.
// [6 bytes - standard overlay priority][1 byte - length][1 byte - path text index].
static uint64_t constexpr kMask = ~static_cast<uint64_t>(0xFFFF);
uint64_t priority = dp::kPriorityMaskAll;
if (!followingMode)
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_text.size()) << 8);
priority |= static_cast<uint8_t>(textIndex);
return priority;
}
void PathTextShape::DrawPathTextPlain(ref_ptr<dp::TextureManager> textures,
ref_ptr<dp::Batcher> batcher,
unique_ptr<PathTextLayout> && layout,
vector<float> const & offsets) const
{
dp::TextureManager::ColorRegion color;
textures->GetColorRegion(m_params.m_textFont.m_color, color);
dp::GLState state(layout->GetFixedHeight() > 0 ? gpu::TEXT_FIXED_PROGRAM : gpu::TEXT_PROGRAM, dp::GLState::OverlayLayer);
state.SetProgram3dIndex(layout->GetFixedHeight() > 0 ? gpu::TEXT_FIXED_BILLBOARD_PROGRAM : gpu::TEXT_BILLBOARD_PROGRAM);
state.SetColorTexture(color.GetTexture());
state.SetMaskTexture(layout->GetMaskTexture());
ASSERT(!offsets.empty(), ());
gpu::TTextStaticVertexBuffer staticBuffer;
gpu::TTextDynamicVertexBuffer dynBuffer;
SharedTextLayout layoutPtr(layout.release());
for (uint32_t textIndex = 0; textIndex < static_cast<uint32_t>(offsets.size()); ++textIndex)
{
float offset = offsets[textIndex];
staticBuffer.clear();
dynBuffer.clear();
layoutPtr->CacheStaticGeometry(color, staticBuffer);
dynBuffer.resize(staticBuffer.size());
dp::AttributeProvider provider(2, static_cast<uint32_t>(staticBuffer.size()));
provider.InitStream(0, gpu::TextStaticVertex::GetBindingInfo(), make_ref(staticBuffer.data()));
provider.InitStream(1, gpu::TextDynamicVertex::GetBindingInfo(), make_ref(dynBuffer.data()));
dp::OverlayID const overlayId = dp::OverlayID(m_params.m_featureID, m_tileCoords, m_baseTextIndex + textIndex);
drape_ptr<dp::OverlayHandle> handle = make_unique_dp<PathTextHandle>(overlayId, m_spline,
layoutPtr, offset, m_params.m_depth, textIndex,
GetOverlayPriority(textIndex, false /* followingMode */),
GetOverlayPriority(textIndex, true /* followingMode */),
layoutPtr->GetFixedHeight(),
textures, true);
batcher->InsertListOfStrip(state, make_ref(&provider), move(handle), 4);
}
}
void PathTextShape::DrawPathTextOutlined(ref_ptr<dp::TextureManager> textures,
ref_ptr<dp::Batcher> batcher,
unique_ptr<PathTextLayout> && layout,
vector<float> const & offsets) const
{
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_OUTLINED_PROGRAM, dp::GLState::OverlayLayer);
state.SetProgram3dIndex(gpu::TEXT_OUTLINED_BILLBOARD_PROGRAM);
state.SetColorTexture(color.GetTexture());
state.SetMaskTexture(layout->GetMaskTexture());
ASSERT(!offsets.empty(), ());
gpu::TTextOutlinedStaticVertexBuffer staticBuffer;
gpu::TTextDynamicVertexBuffer dynBuffer;
SharedTextLayout layoutPtr(layout.release());
for (uint32_t textIndex = 0; textIndex < static_cast<uint32_t>(offsets.size()); ++textIndex)
{
float offset = offsets[textIndex];
staticBuffer.clear();
dynBuffer.clear();
layoutPtr->CacheStaticGeometry(color, outline, staticBuffer);
dynBuffer.resize(staticBuffer.size());
dp::AttributeProvider provider(2, static_cast<uint32_t>(staticBuffer.size()));
provider.InitStream(0, gpu::TextOutlinedStaticVertex::GetBindingInfo(), make_ref(staticBuffer.data()));
provider.InitStream(1, gpu::TextDynamicVertex::GetBindingInfo(), make_ref(dynBuffer.data()));
dp::OverlayID const overlayId = dp::OverlayID(m_params.m_featureID, m_tileCoords, m_baseTextIndex + textIndex);
drape_ptr<dp::OverlayHandle> handle = make_unique_dp<PathTextHandle>(overlayId, m_spline, layoutPtr, offset,
m_params.m_depth, textIndex,
GetOverlayPriority(textIndex, false /* followingMode */),
GetOverlayPriority(textIndex, true /* followingMode */),
layoutPtr->GetFixedHeight(),
textures, true);
batcher->InsertListOfStrip(state, make_ref(&provider), move(handle), 4);
}
}
void PathTextShape::Draw(ref_ptr<dp::Batcher> batcher, ref_ptr<dp::TextureManager> textures) const
{
auto layout = make_unique<PathTextLayout>(m_params.m_tileCenter, strings::MakeUniString(m_params.m_text),
m_params.m_textFont.m_size, m_params.m_textFont.m_isSdf, textures);
uint32_t glyphCount = layout->GetGlyphCount();
if (glyphCount == 0)
return;
vector<float> offsets;
PathTextLayout::CalculatePositions(offsets, m_spline->GetLength(), m_params.m_baseGtoPScale,
layout->GetPixelLength());
if (offsets.empty())
return;
if (m_params.m_textFont.m_outlineColor == dp::Color::Transparent())
DrawPathTextPlain(textures, batcher, move(layout), offsets);
else
DrawPathTextOutlined(textures, batcher, move(layout), offsets);
}
}
|