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

glyph_layout.cpp « graphics - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e8012433973268b43eb133339311c22c46da6be0 (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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#include "glyph_layout.hpp"
#include "font_desc.hpp"
#include "resource.hpp"

#include "../base/logging.hpp"
#include "../base/math.hpp"
#include "../std/sstream.hpp"

#include "../geometry/angles.hpp"
#include "../geometry/any_rect2d.hpp"
#include "../base/thread.hpp"

namespace graphics
{
  double GlyphLayout::getKerning(GlyphLayoutElem const & prevElem,
                                 GlyphMetrics const & prevMetrics,
                                 GlyphLayoutElem const & curElem,
                                 GlyphMetrics const & curMetrics)
  {
    double res = 0;
    /// check, whether we should offset this symbol slightly
    /// not to overlap the previous symbol

    m2::AnyRectD prevSymRectAA(
          prevElem.m_pt.Move(prevMetrics.m_height,
                            -prevElem.m_angle.cos(),
                             prevElem.m_angle.sin()), //< moving by angle = prevElem.m_angle - math::pi / 2
          prevElem.m_angle,
          m2::RectD(prevMetrics.m_xOffset,
                    prevMetrics.m_yOffset,
                    prevMetrics.m_xOffset + prevMetrics.m_width,
                    prevMetrics.m_yOffset + prevMetrics.m_height));

    m2::AnyRectD curSymRectAA(
          curElem.m_pt.Move(curMetrics.m_height,
                           -curElem.m_angle.cos(),
                            curElem.m_angle.sin()), //< moving by angle = curElem.m_angle - math::pi / 2
          curElem.m_angle,
          m2::RectD(curMetrics.m_xOffset,
                    curMetrics.m_yOffset,
                    curMetrics.m_xOffset + curMetrics.m_width,
                    curMetrics.m_yOffset + curMetrics.m_height)
          );

    if (prevElem.m_angle.val() == curElem.m_angle.val())
      return res;

    //m2::RectD prevLocalRect = prevSymRectAA.GetLocalRect();
    m2::PointD pts[4];
    prevSymRectAA.GetGlobalPoints(pts);
    curSymRectAA.ConvertTo(pts, 4);

    m2::RectD prevRectInCurSym(pts[0].x, pts[0].y, pts[0].x, pts[0].y);
    prevRectInCurSym.Add(pts[1]);
    prevRectInCurSym.Add(pts[2]);
    prevRectInCurSym.Add(pts[3]);

    m2::RectD const & curSymRect = curSymRectAA.GetLocalRect();

    if (curSymRect.minX() < prevRectInCurSym.maxX())
      res = prevRectInCurSym.maxX() - curSymRect.minX();

    return res;
  }

  GlyphLayout::GlyphLayout()
  {}

  GlyphLayout::GlyphLayout(GlyphCache * glyphCache,
                           FontDesc const & fontDesc,
                           m2::PointD const & pt,
                           strings::UniString const & visText,
                           graphics::EPosition pos)
    : m_firstVisible(0),
      m_lastVisible(visText.size()),
      m_fontDesc(fontDesc),
      m_pivot(pt),
      m_offset(0, 0),
      m_textLength(0),
      m_textOffset(0)
  {
    initStraigthText(glyphCache, fontDesc, pt, visText, pos, numeric_limits<unsigned>::max());
  }

  GlyphLayout::GlyphLayout(GlyphCache * glyphCache,
                          FontDesc const & fontDesc,
                          m2::PointD const & pt,
                          strings::UniString const & visText,
                          graphics::EPosition pos,
                          unsigned maxWidth)
    : m_firstVisible(0),
      m_lastVisible(visText.size()),
      m_fontDesc(fontDesc),
      m_pivot(pt),
      m_offset(0, 0),
      m_textLength(0),
      m_textOffset(0)
  {
    initStraigthText(glyphCache, fontDesc, pt, visText, pos, maxWidth);
  }

  GlyphLayout::GlyphLayout(GlyphLayout const & src,
                           math::Matrix<double, 3, 3> const & m)
    : m_firstVisible(0),
      m_lastVisible(0),
      m_path(src.m_path, m),
      m_visText(src.m_visText),
      m_fontDesc(src.m_fontDesc),
      m_metrics(src.m_metrics),
      m_pivot(0, 0),
      m_offset(0, 0),
      m_textLength(src.m_textLength)
  {
    if (!m_fontDesc.IsValid())
      return;

    m_boundRects.push_back(m2::AnyRectD(m2::RectD(0, 0, 0, 0)));

    m_textOffset = (m2::PointD(0, src.m_textOffset) * m).Length(m2::PointD(0, 0) * m);

    // if isReverse flag is changed, recalculate m_textOffset
    if (src.m_path.isReverse() != m_path.isReverse())
      m_textOffset = m_path.fullLength() - m_textOffset - m_textLength;

    recalcAlongPath();
  }

  GlyphLayout::GlyphLayout(GlyphCache * glyphCache,
                           FontDesc const & fontDesc,
                           m2::PointD const * pts,
                           size_t ptsCount,
                           strings::UniString const & visText,
                           double fullLength,
                           double pathOffset,
                           double textOffset)
    : m_firstVisible(0),
      m_lastVisible(0),
      m_path(pts, ptsCount, fullLength, pathOffset),
      m_visText(visText),
      m_fontDesc(fontDesc),
      m_pivot(0, 0),
      m_offset(0, 0),
      m_textLength(0),
      m_textOffset(textOffset)
  {
    if (!m_fontDesc.IsValid())
      return;

    m_boundRects.push_back(m2::AnyRectD(m2::RectD(0, 0, 0, 0)));

    size_t const cnt = m_visText.size();
    m_metrics.resize(cnt);

    for (size_t i = 0; i < cnt; ++i)
    {
      GlyphKey key(visText[i],
                   m_fontDesc.m_size,
                   false, // calculating glyph positions using the unmasked glyphs.
                   graphics::Color(0, 0, 0, 0));
      m_metrics[i] = glyphCache->getGlyphMetrics(key);
      m_textLength += m_metrics[i].m_xAdvance;
    }

    // if was reversed - recalculate m_textOffset
    if (m_path.isReverse())
      m_textOffset = m_path.fullLength() - m_textOffset - m_textLength;

    recalcAlongPath();
  }

  void GlyphLayout::addGlyph(GlyphCache * glyphCache,
                             GlyphKey const & key,
                             bool isFirst,
                             strings::UniChar symbol,
                             m2::RectD & boundRect,
                             m2::PointD & curPt)
  {
    GlyphMetrics const m = glyphCache->getGlyphMetrics(key);

    m_textLength += m.m_xAdvance;

    if (isFirst)
    {
      boundRect = m2::RectD(m.m_xOffset,
                           -m.m_yOffset,
                            m.m_xOffset,
                           -m.m_yOffset);

    }
    else
      boundRect.Add(m2::PointD(m.m_xOffset + curPt.x, -m.m_yOffset + curPt.y));

    boundRect.Add(m2::PointD(m.m_xOffset + m.m_width,
                           -(m.m_yOffset + m.m_height)) + curPt);

    GlyphLayoutElem elem;
    elem.m_sym = symbol;
    elem.m_angle = 0;
    elem.m_pt = curPt;
    m_entries.push_back(elem);
    m_metrics.push_back(m);

    curPt += m2::PointD(m.m_xAdvance, m.m_yAdvance);
  }

  void GlyphLayout::initStraigthText(GlyphCache * glyphCache,
                                     FontDesc const & fontDesc,
                                     m2::PointD const & pt,
                                     strings::UniString const & visText,
                                     graphics::EPosition pos,
                                     unsigned maxWidth)
  {
    if (!m_fontDesc.IsValid())
      return;

    size_t const cnt = visText.size();
    ASSERT_GREATER(cnt, 0, ());

    m_entries.reserve(cnt + 2);
    m_metrics.reserve(cnt + 2);

    m2::RectD boundRect;
    m2::PointD curPt(0, 0);

    bool isFirst = true;

    strings::UniChar dotSymbol = '.';
    GlyphKey dotKey(dotSymbol,
                    fontDesc.m_size,
                    false,
                    fontDesc.m_color);
    maxWidth -= glyphCache->getGlyphMetrics(dotKey).m_xAdvance;

    for (size_t i = 0; i < visText.size(); ++i)
    {
      if (m_textLength >= maxWidth)
      {
        addGlyph(glyphCache, dotKey, isFirst, dotSymbol, boundRect, curPt);
        addGlyph(glyphCache, dotKey, isFirst, dotSymbol, boundRect, curPt);
        addGlyph(glyphCache, dotKey, isFirst, dotSymbol, boundRect, curPt);
        m_lastVisible = i + 3;
        break;
      }

      GlyphKey glyphKey(visText[i],
                        fontDesc.m_size,
                        false, //< calculating glyph positions using unmasked glyphs.
                        fontDesc.m_color);

      addGlyph(glyphCache, glyphKey, isFirst, visText[i], boundRect, curPt);
      isFirst = false;
    }

    boundRect.Inflate(2, 2);

    double halfSizeX = boundRect.SizeX() / 2.0;
    double halfSizeY = boundRect.SizeY() / 2.0;

    m2::PointD ptOffs(-halfSizeX - boundRect.minX(),
                      -halfSizeY - boundRect.minY());

    // adjusting according to position
    if (pos & EPosLeft)
      ptOffs += m2::PointD(-halfSizeX, 0);

    if (pos & EPosRight)
      ptOffs += m2::PointD(halfSizeX, 0);

    if (pos & EPosAbove)
      ptOffs += m2::PointD(0, -halfSizeY);

    if (pos & EPosUnder)
      ptOffs += m2::PointD(0, halfSizeY);

    for (unsigned i = 0; i < m_entries.size(); ++i)
      m_entries[i].m_pt += ptOffs;

    boundRect.Offset(ptOffs);

    m_boundRects.push_back(m2::AnyRectD(boundRect));
  }

  void GlyphLayout::recalcAlongPath()
  {
    size_t const count = m_visText.size();
    if (count == 0 || m_path.fullLength() < m_textLength)
      return;

    m_entries.resize(count);
    for (size_t i = 0; i < count; ++i)
      m_entries[i].m_sym = m_visText[i];

    PathPoint arrPathStart = m_path.front();

    // Offset of the text from path's start.
    // In normal behaviour it should be always > 0,
    // but now we do scale tiles for the fixed layout.
    double offset = m_textOffset - m_path.pathOffset();
    if (offset < 0.0)
    {
      /// @todo Try to fix this heuristic.
      if (offset > -3.0)
        offset = 0.0;
      else
        return;
    }

    // find first visible glyph
    size_t symPos = 0;
    //while (offset < 0 &&  symPos < count)
    //  offset += m_metrics[symPos++].m_xAdvance;

    PathPoint glyphStartPt = m_path.offsetPoint(arrPathStart, offset);

    /// @todo Calculate better pivot (invariant point when scaling and rotating text).
    m_pivot = glyphStartPt.m_pt;

    m_firstVisible = symPos;

    GlyphLayoutElem prevElem; // previous glyph, to compute kerning from
    GlyphMetrics prevMetrics;
    bool hasPrevElem = false;

    // calculate base line offset
    double const blOffset = 2 - m_fontDesc.m_size / 2;

    for (; symPos < count; ++symPos)
    {
      if (glyphStartPt.m_i == -1)
      {
        //LOG(LWARNING, ("Can't find glyph", symPos, count));
        return;
      }

      GlyphMetrics const & metrics = m_metrics[symPos];
      GlyphLayoutElem & entry = m_entries[symPos];

      // full advance, including possible kerning.
      double fullGlyphAdvance = metrics.m_xAdvance;

      if (metrics.m_width != 0)
      {
        double fullKern = 0;
        bool pathIsTooBended = false;

        int i = 0;
        for (; i < 100; ++i)
        {
          PivotPoint pivotPt = m_path.findPivotPoint(glyphStartPt, metrics, fullKern);

          if (pivotPt.m_pp.m_i == -1)
          {
            //LOG(LWARNING, ("Path text pivot error for glyph", symPos, count));
            return;
          }

          entry.m_angle = pivotPt.m_angle;
          double const centerOffset = metrics.m_xOffset + metrics.m_width / 2.0;
          entry.m_pt = pivotPt.m_pp.m_pt.Move(-centerOffset,
                                              entry.m_angle.sin(),
                                              entry.m_angle.cos());

          entry.m_pt = entry.m_pt.Move(blOffset,
                                       -entry.m_angle.cos(),
                                       entry.m_angle.sin());

          // check if angle between letters is too big
          if (hasPrevElem && (ang::GetShortestDistance(prevElem.m_angle.m_val, entry.m_angle.m_val) > 0.8))
          {
            pathIsTooBended = true;
            break;
          }

          // check whether we should "kern"
          double kern = 0.0;
          if (hasPrevElem)
          {
            kern = getKerning(prevElem, prevMetrics, entry, metrics);
            if (kern < 0.5)
              kern = 0.0;

            fullGlyphAdvance += kern;
            fullKern += kern;
          }
          if (kern == 0.0)
            break;
        }

        if (i == 100)
          LOG(LINFO, ("100 iteration on computing kerning exceeded. possibly infinite loop occured"));

        if (pathIsTooBended)
          break;

        // kerning should be computed for baseline centered glyph
        prevElem = entry;
        prevMetrics = metrics;
        hasPrevElem = true;

        // align to baseline
        //entry.m_pt = entry.m_pt.Move(blOffset - kernOffset, entry.m_angle - math::pi / 2);
      }
      else
      {
        if (symPos == m_firstVisible)
        {
          m_firstVisible = symPos + 1;
        }
        else
        {
          m_entries[symPos].m_angle = ang::AngleD();
          m_entries[symPos].m_pt = glyphStartPt.m_pt;
        }
      }

      glyphStartPt = m_path.offsetPoint(glyphStartPt, fullGlyphAdvance);

      m_lastVisible = symPos + 1;
    }

    // storing glyph coordinates relative to pivot point.
    for (unsigned i = m_firstVisible; i < m_lastVisible; ++i)
      m_entries[i].m_pt -= m_pivot;

    computeBoundRects();
  }

  void GlyphLayout::computeBoundRects()
  {
    map<double, m2::AnyRectD> rects;

    for (unsigned i = m_firstVisible; i < m_lastVisible; ++i)
    {
      if (m_metrics[i].m_width != 0)
      {
        ang::AngleD const & a = m_entries[i].m_angle;

        map<double, m2::AnyRectD>::iterator it = rects.find(a.val());

        m2::AnyRectD symRectAA(
              m_entries[i].m_pt.Move(m_metrics[i].m_height + m_metrics[i].m_yOffset, -a.cos(), a.sin()), //< moving by angle = m_entries[i].m_angle - math::pi / 2
              a,
              m2::RectD(m_metrics[i].m_xOffset,
                        0,
                        m_metrics[i].m_xOffset + m_metrics[i].m_width,
                        m_metrics[i].m_height
                        ));

        if (it == rects.end())
          rects[a.val()] = symRectAA;
        else
          rects[a.val()].Add(symRectAA);
      }
    }

    m_boundRects.clear();

    for (map<double, m2::AnyRectD>::const_iterator it = rects.begin(); it != rects.end(); ++it)
    {
      m2::AnyRectD r(it->second);
      m2::PointD zero = r.GlobalZero();

      double dx = zero.x - floor(zero.x);
      double dy = zero.y - floor(zero.y);

      r.Offset(m2::PointD(-dx, -dy));

      r.Offset(m_pivot);

      m_boundRects.push_back(r);
    }
  }

  size_t GlyphLayout::firstVisible() const
  {
    return m_firstVisible;
  }

  size_t GlyphLayout::lastVisible() const
  {
    return m_lastVisible;
  }

  buffer_vector<GlyphLayoutElem, 32> const & GlyphLayout::entries() const
  {
    return m_entries;
  }

  buffer_vector<m2::AnyRectD, 16> const & GlyphLayout::boundRects() const
  {
    return m_boundRects;
  }

  m2::PointD const & GlyphLayout::pivot() const
  {
    return m_pivot;
  }

  void GlyphLayout::setPivot(m2::PointD const & pivot)
  {
    for (unsigned i = 0; i < m_boundRects.size(); ++i)
      m_boundRects[i].Offset(pivot - m_pivot);

    m_pivot = pivot;
  }

  m2::PointD const & GlyphLayout::offset() const
  {
    return m_offset;
  }

  void GlyphLayout::setOffset(m2::PointD const & offset)
  {
    for (unsigned i = 0; i < m_boundRects.size(); ++i)
      m_boundRects[i].Offset(offset - m_offset);

    m_offset = offset;
  }

  graphics::FontDesc const & GlyphLayout::fontDesc() const
  {
    return m_fontDesc;
  }

  int GlyphLayout::baseLineOffset()
  {
    int result = 0;
    for (size_t i = 0; i < m_metrics.size(); ++i)
      result = min(m_metrics[i].m_yOffset, result);

    return -result;
  }
}