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: 6d558fec0af001b0404c4329d2e5e2b87bde7b49 (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
#include "text_shape.hpp"
#include "text_layout.hpp"
#include "fribidi.hpp"

#include "../drape/glsl_types.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/string_utils.hpp"

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

namespace df
{

namespace
{

float const TEXT_EXPAND_FACTOR = 1.3f;

glsl::vec2 GetShift(dp::Anchor anchor, float width, float height)
{
  switch(anchor)
  {
  case dp::Center:      return glsl::vec2(-width / 2.0f, height / 2.0f);
  case dp::Left:        return glsl::vec2(0.0f, height / 2.0f);
  case dp::Right:       return glsl::vec2(-width, height / 2.0f);
  case dp::Top:         return glsl::vec2(-width / 2.0f, height);
  case dp::Bottom:      return glsl::vec2(-width / 2.0f, 0);
  case dp::LeftTop:     return glsl::vec2(0.0f, height);
  case dp::RightTop:    return glsl::vec2(-width, height);
  case dp::LeftBottom:  return glsl::vec2(0.0f, 0.0f);
  case dp::RightBottom: return glsl::vec2(-width, 0.0f);
  default:              return glsl::vec2(0.0f, 0.0f);
  }
}

void BatchText(dp::RefPointer<dp::Batcher> batcher, int32_t textureSet,
               vector<glsl::Quad4> const & positions,
               vector<glsl::Quad4> const & texCoord,
               vector<glsl::Quad4> const & color,
               vector<glsl::Quad1> const & index,
               size_t glyphCount,
               dp::OverlayHandle * handle)
{
  ASSERT(glyphCount <= positions.size(), ());
  ASSERT(positions.size() == texCoord.size(), ());
  ASSERT(positions.size() == color.size(), ());
  ASSERT(positions.size() == index.size(), ());

  dp::GLState state(gpu::FONT_PROGRAM, dp::GLState::OverlayLayer);
  state.SetTextureSet(textureSet);
  state.SetBlending(dp::Blending(true));

  dp::AttributeProvider provider(4, 4 * glyphCount);
  {
    dp::BindingInfo position(1);
    dp::BindingDecl & decl = position.GetBindingDecl(0);
    decl.m_attributeName = "a_position";
    decl.m_componentCount = 4;
    decl.m_componentType = gl_const::GLFloatType;
    decl.m_offset = 0;
    decl.m_stride = 0;
    provider.InitStream(0, position, dp::MakeStackRefPointer((void*)&positions[0]));
  }
  {
    dp::BindingInfo texcoord(1);
    dp::BindingDecl & decl = texcoord.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, texcoord, dp::MakeStackRefPointer((void*)&texCoord[0]));
  }
  {
    dp::BindingInfo base_color(1);
    dp::BindingDecl & decl = base_color.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, base_color, dp::MakeStackRefPointer((void*)&color[0]));
  }
  {
    dp::BindingInfo outline_color(1);
    dp::BindingDecl & decl = outline_color.GetBindingDecl(0);
    decl.m_attributeName = "a_index";
    decl.m_componentCount = 1;
    decl.m_componentType = gl_const::GLFloatType;
    decl.m_offset = 0;
    decl.m_stride = 0;
    provider.InitStream(3, outline_color, dp::MakeStackRefPointer((void*)&index[0]));
  }

  batcher->InsertListOfStrip(state, dp::MakeStackRefPointer(&provider), MovePointer(handle), 4);
}

///Old code
void SplitText(strings::UniString const & visText,
              buffer_vector<strings::UniString, 3> & res,
              char const * delims,
              bool splitAllFound)
{
  if (!splitAllFound)
  {
    size_t count = visText.size();
    if (count > 15)
    {
      // split on two parts
      typedef strings::UniString::const_iterator TIter;
      TIter const iMiddle = visText.begin() + count/2;

      size_t const delimsSize = strlen(delims);

      // find next delimeter after middle [m, e)
      TIter iNext = find_first_of(iMiddle,
                                  visText.end(),
                                  delims, delims + delimsSize);

      // find last delimeter before middle [b, m)
      TIter iPrev = find_first_of(reverse_iterator<TIter>(iMiddle),
                                  reverse_iterator<TIter>(visText.begin()),
                                  delims, delims + delimsSize).base();
      // don't do split like this:
      //     xxxx
      // xxxxxxxxxxxx
      if (4 * distance(visText.begin(), iPrev) <= count)
        iPrev = visText.end();
      else
        --iPrev;

      // get closest delimiter to the middle
      if (iNext == visText.end() ||
          (iPrev != visText.end() && distance(iPrev, iMiddle) < distance(iMiddle, iNext)))
      {
        iNext = iPrev;
      }

      // split string on 2 parts
      if (iNext != visText.end())
      {
        ASSERT_NOT_EQUAL(iNext, visText.begin(), ());
        res.push_back(strings::UniString(visText.begin(), iNext));

        if (++iNext != visText.end())
          res.push_back(strings::UniString(iNext, visText.end()));

        return;
      }
    }

    res.push_back(visText);
  }
  else
  {
    // split string using according to all delimiters
    typedef strings::SimpleDelimiter TDelim;
    for (strings::TokenizeIterator<TDelim> iter(visText, TDelim(delims)); iter; ++iter)
      res.push_back(iter.GetUniString());
  }
}

pair<bool, bool> GetBidiTexts(
    strings::UniString const & visText, strings::UniString const & auxVisText,
    strings::UniString & primaryText, strings::UniString & secondaryText)
{
  primaryText = fribidi::log2vis(visText);
  secondaryText = fribidi::log2vis(auxVisText);
  return make_pair(visText != primaryText, auxVisText != secondaryText);
}

void GetLayouts(vector<TextLayout> & layouts, strings::UniString const & text, bool isBidi, FontDecl const & fontDecl, dp::RefPointer<dp::TextureSetHolder> textures)
{
  if (!isBidi)
  {
    buffer_vector<strings::UniString, 3> res;
    SplitText(text, res, " \n\t", false);
    for (int i = 0; i < res.size(); ++i)
    {
      TextLayout tl = TextLayout(res[res.size() - i - 1], fontDecl, textures);
      if (tl.GetGlyphCount() == 0)
        continue;
      layouts.push_back(tl);
    }
  }
  else
  {
    TextLayout tl = TextLayout(text, fontDecl, textures);
    if (tl.GetGlyphCount() == 0)
      return;
    layouts.push_back(tl);
  }
}
}

TextShape::TextShape(m2::PointF const & basePoint, TextViewParams const & params)
  : m_basePoint(basePoint),
    m_params(params)
{
}

void TextShape::Draw(dp::RefPointer<dp::Batcher> batcher, dp::RefPointer<dp::TextureSetHolder> textures) const
{
  ASSERT(!m_params.m_primaryText.empty(), ());
  vector<TextLayout> layouts;
  strings::UniString primaryText, secondaryText;
  pair<bool, bool> isBidi = GetBidiTexts(strings::MakeUniString(m_params.m_primaryText), strings::MakeUniString(m_params.m_secondaryText), primaryText, secondaryText);
  if (m_params.m_secondaryText.empty())
  {
    GetLayouts(layouts, primaryText, isBidi.first, m_params.m_primaryTextFont, textures);
    if (layouts.size() == 0)
      return;
    DrawMultipleLines(batcher, layouts, -1, textures);
  }
  else
  {
    GetLayouts(layouts, secondaryText, isBidi.second, m_params.m_secondaryTextFont, textures);
    int size = layouts.size() - 1;
    GetLayouts(layouts, primaryText, isBidi.first, m_params.m_primaryTextFont, textures);
    if (layouts.size() == 0)
      return;
    DrawMultipleLines(batcher, layouts, size, textures);
  }
}

void TextShape::DrawMultipleLines(dp::RefPointer<dp::Batcher> batcher, vector<TextLayout> & layouts,
                                  int delim, dp::RefPointer<dp::TextureSetHolder> textures) const
{
  size_t const count = layouts.size();
  vector<float> lengths(count);
  float maxLength = 0.0f;
  float textHeight = 0.0f;
  uint32_t maxCount = 0;
  uint32_t symCount = 0;
  vector<uint32_t> heights(count);
  for (size_t i = 0; i < count; ++i)
  {
    lengths[i] = layouts[i].GetPixelLength();
    heights[i] = layouts[i].GetPixelHeight();
    textHeight += heights[i];
    maxLength = max(maxLength, lengths[i]);
    symCount += layouts[i].GetGlyphCount();
    maxCount = max(maxCount, layouts[i].GetGlyphCount());
  }

  glsl::vec2 const anchorOffset = GetShift(m_params.m_anchor, maxLength, textHeight * TEXT_EXPAND_FACTOR);

  vector<glsl::Quad4> positions(symCount);
  vector<glsl::Quad4> texCoord(symCount);
  vector<glsl::Quad4> fontColor(symCount);
  vector<glsl::Quad1> indexes(symCount);

  float dy = (1.0f - TEXT_EXPAND_FACTOR) * heights[0];
  vector<glsl::vec2> pixelOffset(count);
  uint32_t delSymCount = 0;
  uint32_t lastIndex = 0;
  vector<TextLayout>::iterator it1;
  vector<glsl::vec2>::iterator it2;
  glsl::vec2 pivot(m_basePoint.x, m_basePoint.y);
  for (size_t i = 0; i < count; ++i)
  {
    float const dx = (maxLength - lengths[i]) / 2.0f;
    pixelOffset[i] = glsl::vec2(dx, dy) + anchorOffset + glsl::vec2(m_params.m_primaryOffset.x,
                                                                    m_params.m_primaryOffset.y);
    dy -= heights[i] * TEXT_EXPAND_FACTOR;
    delSymCount += layouts[i].GetGlyphCount();
    if (i == delim)
    {
      it2 = pixelOffset.begin();
      it1 = layouts.begin();
      dp::OverlayHandle * handle = LayoutText(m_params.m_featureID, pivot,
                                              it1, it2, m_params.m_depth,
                                              positions, texCoord, fontColor,
                                              indexes, textures, i + 1);

      BatchText(batcher, layouts[0].GetTextureSet(),
                positions, texCoord,
                fontColor, indexes,
                delSymCount, handle);

      delSymCount = 0;
      lastIndex = i + 1;
    }
  }
  it2 = pixelOffset.begin() + lastIndex;
  it1 = layouts.begin() + lastIndex;
  dp::OverlayHandle * handle = LayoutText(m_params.m_featureID, pivot,
                                          it1, it2, m_params.m_depth,
                                          positions, texCoord, fontColor, indexes,
                                          textures, count - lastIndex);

  BatchText(batcher, layouts[0].GetTextureSet(),
            positions, texCoord,
            fontColor, indexes,
            delSymCount, handle);
}

} //end of df namespace