add verticalAlign for text

Default is center. Other options are "top" and "bottom"
This commit is contained in:
Ansis Brammanis
2014-05-30 03:11:57 -04:00
parent 9057c4980d
commit 41c310c762
6 changed files with 41 additions and 13 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -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.

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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