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

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

#include "../std/numeric.hpp"
#include "../std/algorithm.hpp"
#include "../std/bind.hpp"

using glsl_types::vec4;
using glsl_types::Quad4;

namespace
{
  void FillColor(vector<Quad4> & data, dp::Color const & color)
  {
    Quad4 c;
    c.v[0] = c.v[1] = c.v[2] = c.v[3] = vec4(dp::ColorF(color));
    fill(data.begin(), data.end(), c);
  }
}

namespace df
{

class StraightTextHandle : public dp::OverlayHandle
{
public:
  StraightTextHandle(FeatureID const & id, m2::PointD const & pivot,
                     m2::PointD const & pxSize, m2::PointD const & offset,
                     double priority)
    : OverlayHandle(id, dp::LeftBottom, priority)
    , m_pivot(pivot)
    , m_offset(offset)
    , m_size(pxSize)
  {
  }

  m2::RectD GetPixelRect(ScreenBase const & screen) const
  {
    m2::PointD const pivot = screen.GtoP(m_pivot) + m_offset;
    return m2::RectD(pivot, pivot + m_size);
  }

private:
  m2::PointD m_pivot;
  m2::PointD m_offset;
  m2::PointD m_size;
};

namespace
{

#ifdef DEBUG
  void ValidateTextureSet(buffer_vector<dp::TextureSetHolder::GlyphRegion, 32> const & metrics)
  {
    if (metrics.size() < 2)
      return;

    ASSERT(metrics[0].IsValid(), ());
    uint32_t textureSet = metrics[0].GetTextureNode().m_textureSet;
    for (size_t i = 1; i < metrics.size(); ++i)
    {
      ASSERT(metrics[i].IsValid(), ());
      ASSERT_EQUAL(metrics[i].GetTextureNode().m_textureSet, textureSet, ());
    }
  }
#endif

  float const BASE_HEIGHT = 28.0f;
}

TextLayout::TextLayout(strings::UniString const & string,
                       df::FontDecl const & font,
                       dp::RefPointer<dp::TextureSetHolder> textures)
  : m_font(font)
  , m_textSizeRatio(font.m_size / BASE_HEIGHT)
{
  ASSERT(!string.empty(), ());
  m_metrics.reserve(string.size());
  for_each(string.begin(), string.end(), bind(&TextLayout::InitMetric, this, _1, textures));
#ifdef DEBUG
  ValidateTextureSet(m_metrics);
#endif
}

dp::OverlayHandle * TextLayout::LayoutText(const FeatureID & featureID,
                                           m2::PointF const & pivot,
                                           m2::PointF const & pixelOffset,
                                           float depth,
                                           vector<Quad4> & positions,
                                           vector<Quad4> & texCoord,
                                           vector<Quad4> & fontColor,
                                           vector<Quad4> & outlineColor) const
{
  STATIC_ASSERT(sizeof(vec4) == 4 * sizeof(float));
  STATIC_ASSERT(sizeof(Quad4) == 4 * sizeof(vec4));

  size_t glyphCount = GetGlyphCount();
  ASSERT(glyphCount <= positions.size(), ());
  ASSERT(glyphCount <= texCoord.size(), ());
  ASSERT(glyphCount <= fontColor.size(), ());
  ASSERT(glyphCount <= outlineColor.size(), ());

  FillColor(fontColor, m_font.m_color);
  FillColor(outlineColor, m_font.m_outlineColor);

  float glyphOffset = 0.0;
  for (size_t i = 0; i < glyphCount; ++i)
  {
    GlyphRegion const & region = m_metrics[i];
    ASSERT(region.IsValid(), ());
    GetTextureQuad(region, depth, texCoord[i]);

    float xOffset, yOffset, advance;
    region.GetMetrics(xOffset, yOffset, advance);

    xOffset *= m_textSizeRatio;
    yOffset *= m_textSizeRatio;
    advance *= m_textSizeRatio;

    m2::PointU size;
    region.GetPixelSize(size);
    double const h = size.y * m_textSizeRatio;
    double const w = size.x * m_textSizeRatio;

    Quad4 & position = positions[i];
    position.v[0] = vec4(pivot, m2::PointF(glyphOffset + xOffset, yOffset) + pixelOffset);
    position.v[1] = vec4(pivot, m2::PointF(glyphOffset + xOffset, yOffset + h) + pixelOffset);
    position.v[2] = vec4(pivot, m2::PointF(glyphOffset + w + xOffset, yOffset) + pixelOffset);
    position.v[3] = vec4(pivot, m2::PointF(glyphOffset + w + xOffset, yOffset + h) + pixelOffset);
    glyphOffset += advance;
  }

  return new StraightTextHandle(featureID, pivot, m2::PointD(glyphOffset, m_font.m_size),
                                pixelOffset, depth);
}

void TextLayout::InitPathText(float depth,
                              vector<glsl_types::Quad4> & texCoord,
                              vector<glsl_types::Quad4> & fontColor,
                              vector<glsl_types::Quad4> & outlineColor) const
{
  STATIC_ASSERT(sizeof(vec4) == 4 * sizeof(float));
  STATIC_ASSERT(sizeof(Quad4) == 4 * sizeof(vec4));

  size_t glyphCount = GetGlyphCount();
  ASSERT(glyphCount <= texCoord.size(), ());
  ASSERT(glyphCount <= fontColor.size(), ());
  ASSERT(glyphCount <= outlineColor.size(), ());

  FillColor(fontColor, m_font.m_color);
  FillColor(outlineColor, m_font.m_outlineColor);

  for (size_t i = 0; i < glyphCount; ++i)
    GetTextureQuad(m_metrics[i], depth, texCoord[i]);
}

void TextLayout::LayoutPathText(m2::Spline::iterator const & iterator,
                                float const scalePtoG,
                                IntrusiveVector<glsl_types::vec2> & positions,
                                bool isForwardDirection) const
{
  if (!isForwardDirection)
    positions.SetFillDirection(df::Backward);

  m2::Spline::iterator itr = iterator;

  uint32_t glyphCount = GetGlyphCount();
  int32_t startIndex = isForwardDirection ? 0 : glyphCount - 1;
  int32_t endIndex = isForwardDirection ? glyphCount : -1;
  int32_t incSign = isForwardDirection ? 1 : -1;

  for (int32_t i = startIndex; i != endIndex; i += incSign)
  {
    float xOffset, yOffset, advance;
    float halfWidth, halfHeight;
    GetMetrics(i, xOffset, yOffset, advance, halfWidth, halfHeight);
    advance *= scalePtoG;

    ASSERT_NOT_EQUAL(advance, 0.0, ());
    m2::PointF const pos = itr.m_pos;
    itr.Step(advance);
    ASSERT(!itr.BeginAgain(), ());

    m2::PointF dir = itr.m_avrDir.Normalize();
    m2::PointF norm(-dir.y, dir.x);
    m2::PointF norm2 = norm;
    dir *= halfWidth * scalePtoG;
    norm *= halfHeight * scalePtoG;

    float const halfFontSize = m_textSizeRatio * scalePtoG / 2.0f;
    m2::PointF const dirComponent = dir * xOffset / halfWidth;
    m2::PointF const normalComponent = -norm * incSign * yOffset / halfHeight;
    m2::PointF const fontSizeComponent = norm2 * incSign * halfFontSize;
    m2::PointF const pivot = dirComponent + normalComponent + pos - fontSizeComponent;

    positions.PushBack(glsl_types::vec2(pivot - dir + norm));
    positions.PushBack(glsl_types::vec2(pivot - dir - norm));
    positions.PushBack(glsl_types::vec2(pivot + dir + norm));
    positions.PushBack(glsl_types::vec2(pivot + dir - norm));
  }
}

uint32_t TextLayout::GetGlyphCount() const
{
  return m_metrics.size();
}

uint32_t TextLayout::GetTextureSet() const
{
  return m_metrics[0].GetTextureNode().m_textureSet;
}

float TextLayout::GetPixelLength() const
{
  return m_textSizeRatio * accumulate(m_metrics.begin(), m_metrics.end(), 0.0,
                                      bind(&TextLayout::AccumulateAdvance, this, _1, _2));
}

float TextLayout::GetPixelHeight() const
{
  return m_font.m_size;
}

void TextLayout::GetTextureQuad(GlyphRegion const & region,
                                float depth,
                                Quad4 & quad) const
{
  ASSERT(region.IsValid(), ());

  m2::RectF const & rect = region.GetTexRect();
  uint8_t needOutline = m_font.m_needOutline ? 1 : 0;
  float textureOffset = static_cast<float>((region.GetTextureNode().m_textureOffset << 1) + needOutline);
  quad.v[0] = vec4(rect.minX(), rect.minY(), textureOffset, depth);
  quad.v[1] = vec4(rect.minX(), rect.maxY(), textureOffset, depth);
  quad.v[2] = vec4(rect.maxX(), rect.minY(), textureOffset, depth);
  quad.v[3] = vec4(rect.maxX(), rect.maxY(), textureOffset, depth);
}

float TextLayout::AccumulateAdvance(double const & currentValue, GlyphRegion const & reg1) const
{
  ASSERT(reg1.IsValid(), ());

  return currentValue + reg1.GetAdvance();
}

void TextLayout::InitMetric(strings::UniChar const & unicodePoint,
                            dp::RefPointer<dp::TextureSetHolder> textures)
{
  GlyphRegion region;
  if (textures->GetGlyphRegion(unicodePoint, region))
    m_metrics.push_back(region);
}

void TextLayout::GetMetrics(int32_t const index, float & xOffset, float & yOffset, float & advance,
                            float & halfWidth, float & halfHeight) const
{
  GlyphRegion const & region = m_metrics[index];
  m2::PointU size;
  region.GetPixelSize(size);
  region.GetMetrics(xOffset, yOffset, advance);

  halfWidth = m_textSizeRatio * size.x / 2.0f;
  halfHeight = m_textSizeRatio * size.y / 2.0f;

  xOffset = xOffset * m_textSizeRatio + halfWidth;
  yOffset = yOffset * m_textSizeRatio + halfHeight;
  advance *= m_textSizeRatio;
}

///////////////////////////////////////////////////////////////
SharedTextLayout::SharedTextLayout(TextLayout * layout)
  : m_layout(layout)
{
}

bool SharedTextLayout::IsNull() const
{
  return m_layout == NULL;
}

void SharedTextLayout::Reset(TextLayout * layout)
{
  m_layout.reset(layout);
}

TextLayout * SharedTextLayout::operator->()
{
  return m_layout.get();
}

TextLayout const * SharedTextLayout::operator->() const
{
  return m_layout.get();
}



}