diff options
author | Ansis Brammanis <ansis.brammanis@gmail.com> | 2014-05-30 01:32:15 -0400 |
---|---|---|
committer | Ansis Brammanis <ansis.brammanis@gmail.com> | 2014-05-30 01:32:15 -0400 |
commit | 9057c4980dc12515cd8d6f33d9253e61592272fe (patch) | |
tree | 5973560ff73b39f1d01cc456cd2bc69c4482a0bf /src/text | |
parent | 63729a500944a47c034713a531b7add337df6688 (diff) | |
download | qtlocation-mapboxgl-9057c4980dc12515cd8d6f33d9253e61592272fe.tar.gz |
line wrapping
Diffstat (limited to 'src/text')
-rw-r--r-- | src/text/glyph_store.cpp | 51 |
1 files changed, 47 insertions, 4 deletions
diff --git a/src/text/glyph_store.cpp b/src/text/glyph_store.cpp index a148ac9285..6293aee23b 100644 --- a/src/text/glyph_store.cpp +++ b/src/text/glyph_store.cpp @@ -47,11 +47,54 @@ const Shaping FontStack::getShaping(const std::u32string &string, const float &m return shaped; } +void alignHorizontally(Shaping &shaping, const std::map<uint32_t, GlyphMetrics> &metrics, + const uint32_t &start, const uint32_t &end, const float &alignment) { + + uint32_t lastAdvance = metrics.find(shaping[end].glyph)->second.advance; + int32_t lineIndent = (shaping[end].x + lastAdvance) * alignment; + + for (uint32_t j = start; j <= end; j++) { + shaping[j].x -= lineIndent; + } + } + Shaping FontStack::lineWrap(Shaping shaped, const float &lineHeight, const float &maxWidth, const float &alignment) const { - std::cerr << "\nlineHeight: " << lineHeight << - "\nmaxWidth: " << maxWidth << - "\nalignment: " << alignment << - "\n"; + uint32_t lastSafeBreak = 0; + uint32_t lengthBeforeCurrentLine = 0; + uint32_t lineStartIndex = 0; + uint32_t line = 0; + + for (uint32_t i = 0; i < shaped.size(); i++) { + GlyphPlacement &shape = shaped[i]; + + shape.x -= lengthBeforeCurrentLine; + shape.y += lineHeight * line; + + if (shape.x > maxWidth && lastSafeBreak != 0) { + + uint32_t lineLength = shaped[lastSafeBreak + 1].x; + + for (uint32_t k = lastSafeBreak + 1; k <= i; k++) { + shaped[k].y += lineHeight; + shaped[k].x -= lineLength; + } + + if (alignment) { + alignHorizontally(shaped, metrics, lineStartIndex, lastSafeBreak - 1, alignment); + } + + lineStartIndex = lastSafeBreak + 1; + lastSafeBreak = 0; + lengthBeforeCurrentLine += lineLength; + line++; + } + if (shape.glyph == 32) { + lastSafeBreak = i; + } + } + + alignHorizontally(shaped, metrics, lineStartIndex, shaped.size() - 1, alignment); + return shaped; } |