summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnsis Brammanis <ansis.brammanis@gmail.com>2014-05-30 01:32:15 -0400
committerAnsis Brammanis <ansis.brammanis@gmail.com>2014-05-30 01:32:15 -0400
commit9057c4980dc12515cd8d6f33d9253e61592272fe (patch)
tree5973560ff73b39f1d01cc456cd2bc69c4482a0bf
parent63729a500944a47c034713a531b7add337df6688 (diff)
downloadqtlocation-mapboxgl-9057c4980dc12515cd8d6f33d9253e61592272fe.tar.gz
line wrapping
-rw-r--r--bin/style.js4
-rw-r--r--include/llmr/text/glyph.hpp4
-rw-r--r--src/text/glyph_store.cpp51
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<GlyphPlacement> 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<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;
}