From 9057c4980dc12515cd8d6f33d9253e61592272fe Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Fri, 30 May 2014 01:32:15 -0400 Subject: line wrapping --- bin/style.js | 4 ++-- include/llmr/text/glyph.hpp | 4 ++-- src/text/glyph_store.cpp | 51 +++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/bin/style.js b/bin/style.js index 931bde0112..38ffd7c327 100644 --- a/bin/style.js +++ b/bin/style.js @@ -765,8 +765,8 @@ module.exports = { "path": "horizontal", "font": "Open Sans Semibold, Arial Unicode MS Bold", "fontSize": 18, - "text-max-width": 2, - "text-line-height": 1.4, + "maxWidth": 2, + "lineHeight": 1.4, "feature_type": "point", "type": "text" }, diff --git a/include/llmr/text/glyph.hpp b/include/llmr/text/glyph.hpp index e6138e712e..c13088c416 100644 --- a/include/llmr/text/glyph.hpp +++ b/include/llmr/text/glyph.hpp @@ -53,8 +53,8 @@ public: uint32_t face = 0; uint32_t glyph = 0; - uint32_t x = 0; - uint32_t y = 0; + int32_t x = 0; + int32_t y = 0; }; typedef std::vector Shaping; 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 &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; } -- cgit v1.2.1