summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnsis Brammanis <ansis.brammanis@gmail.com>2014-05-30 03:11:57 -0400
committerAnsis Brammanis <ansis.brammanis@gmail.com>2014-05-30 03:11:57 -0400
commit41c310c76238d3a2823c79592b553a6ff3205c47 (patch)
tree8e017924c9ac1bc7d1d56d00c19366b2e398abe8
parent9057c4980dc12515cd8d6f33d9253e61592272fe (diff)
downloadqtlocation-mapboxgl-41c310c76238d3a2823c79592b553a6ff3205c47.tar.gz
add verticalAlign for text
Default is center. Other options are "top" and "bottom"
-rw-r--r--include/llmr/style/bucket_description.hpp7
-rw-r--r--include/llmr/text/glyph_store.hpp6
-rw-r--r--src/map/tile_parser.cpp4
-rw-r--r--src/style/style_parser.cpp6
-rw-r--r--src/text/glyph_store.cpp29
-rw-r--r--src/text/placement.cpp2
6 files changed, 41 insertions, 13 deletions
diff --git a/include/llmr/style/bucket_description.hpp b/include/llmr/style/bucket_description.hpp
index 004932aa49..b708ef6866 100644
--- a/include/llmr/style/bucket_description.hpp
+++ b/include/llmr/style/bucket_description.hpp
@@ -74,6 +74,12 @@ inline float alignmentType(const std::string& alignment) {
else return 0.5;
};
+inline float verticalAlignmentType(const std::string& alignment) {
+ if (alignment == "bottom") return 1.0f;
+ else if (alignment == "top") return 0.0f;
+ else return 0.5;
+};
+
class BucketGeometryDescription {
public:
CapType cap = CapType::None;
@@ -86,6 +92,7 @@ public:
float round_limit = 1.0f;
TextPathType path = TextPathType::Horizontal;
float alignment = 0.5;
+ float vertical_alignment = 0.5;
float line_height = 1.2 * 24;
float max_width = 15.0f * 24;
float padding = 2.0f;
diff --git a/include/llmr/text/glyph_store.hpp b/include/llmr/text/glyph_store.hpp
index 74472194b0..f8b14683d7 100644
--- a/include/llmr/text/glyph_store.hpp
+++ b/include/llmr/text/glyph_store.hpp
@@ -33,11 +33,13 @@ public:
const Shaping getShaping(const std::u32string &string,
const float &maxWidth,
const float &lineHeight,
- const float &alignment) const;
+ const float &alignment,
+ const float &verticalAlignment) const;
Shaping lineWrap(Shaping shaped,
const float &lineHeight,
const float &maxWidth,
- const float &alignment) const;
+ const float &alignment,
+ const float &verticalAlignment) const;
private:
std::map<uint32_t, std::string> bitmaps;
diff --git a/src/map/tile_parser.cpp b/src/map/tile_parser.cpp
index 4313003189..f2b3ac05cb 100644
--- a/src/map/tile_parser.cpp
+++ b/src/map/tile_parser.cpp
@@ -225,7 +225,9 @@ std::unique_ptr<Bucket> TileParser::createTextBucket(const VectorTileLayer& laye
const std::u32string string = ucs4conv.convert(toString(it_prop->second));
// Shape labels.
- const Shaping shaped = fontStack.getShaping(string, bucket_desc.geometry.max_width, bucket_desc.geometry.line_height, bucket_desc.geometry.alignment);
+ const Shaping shaped = fontStack.getShaping(string, bucket_desc.geometry.max_width,
+ bucket_desc.geometry.line_height, bucket_desc.geometry.alignment,
+ bucket_desc.geometry.vertical_alignment);
shaping.emplace(toString(it_prop->second), shaped);
// Place labels.
diff --git a/src/style/style_parser.cpp b/src/style/style_parser.cpp
index ba189f76f2..535e63916e 100644
--- a/src/style/style_parser.cpp
+++ b/src/style/style_parser.cpp
@@ -108,6 +108,12 @@ BucketDescription StyleParser::parseBucket(JSVal value) {
} else {
throw Style::exception("alignment must be a string");
}
+ } else if (name == "verticalAlignment") {
+ if (value.IsString()) {
+ bucket.geometry.vertical_alignment = verticalAlignmentType({ value.GetString(), value.GetStringLength() });
+ } else {
+ throw Style::exception("alignment must be a string");
+ }
} else if (name == "lineHeight") {
if (value.IsNumber()) {
bucket.geometry.line_height = value.GetDouble() * 24;
diff --git a/src/text/glyph_store.cpp b/src/text/glyph_store.cpp
index 6293aee23b..ac647eb842 100644
--- a/src/text/glyph_store.cpp
+++ b/src/text/glyph_store.cpp
@@ -29,7 +29,8 @@ const std::map<uint32_t, SDFGlyph> &FontStack::getSDFs() const {
return sdfs;
}
-const Shaping FontStack::getShaping(const std::u32string &string, const float &maxWidth, const float &lineHeight, const float &alignment) const {
+const Shaping FontStack::getShaping(const std::u32string &string, const float &maxWidth,
+ const float &lineHeight, const float &alignment, const float &verticalAlignment) const {
std::lock_guard<std::mutex> lock(mtx);
uint32_t i = 0;
uint32_t x = 0;
@@ -42,23 +43,32 @@ const Shaping FontStack::getShaping(const std::u32string &string, const float &m
x += metrics.find(chr)->second.advance;
}
- shaped = lineWrap(shaped, lineHeight, maxWidth, alignment);
+ shaped = lineWrap(shaped, lineHeight, maxWidth, alignment, verticalAlignment);
return shaped;
}
+void alignVertically(Shaping &shaping, const uint32_t &lines, const float &lineHeight, const float &verticalAlign) {
+ const float dy = -(lineHeight * (lines - 1) + 24) * verticalAlign - 5;
+ for (GlyphPlacement &shape : shaping) {
+ shape.y += dy;
+ }
+}
+
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;
+ 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;
+ }
+}
- 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 float &verticalAlignment) const {
-Shaping FontStack::lineWrap(Shaping shaped, const float &lineHeight, const float &maxWidth, const float &alignment) const {
uint32_t lastSafeBreak = 0;
uint32_t lengthBeforeCurrentLine = 0;
uint32_t lineStartIndex = 0;
@@ -94,6 +104,7 @@ Shaping FontStack::lineWrap(Shaping shaped, const float &lineHeight, const float
}
alignHorizontally(shaped, metrics, lineStartIndex, shaped.size() - 1, alignment);
+ alignVertically(shaped, line + 1, lineHeight, verticalAlignment);
return shaped;
}
diff --git a/src/text/placement.cpp b/src/text/placement.cpp
index ed71f881e2..edefa6075d 100644
--- a/src/text/placement.cpp
+++ b/src/text/placement.cpp
@@ -261,7 +261,7 @@ void Placement::addFeature(TextBucket& bucket,
Anchors anchors;
// TODO: figure out correct ascender height.
- vec2<float> origin{0, -17};
+ vec2<float> origin{0, 0};
if (line.size() == 1) {
// Point labels