port some of the text shaping changes
This commit is contained in:
@@ -195,7 +195,7 @@ void SymbolBucket::addFeatures(const GeometryTileLayer& layer,
|
||||
/* translate */ vec2<float>(layout.text.offset[0], layout.text.offset[1]));
|
||||
|
||||
// Add the glyphs we need for this label to the glyph atlas.
|
||||
if (shaping.size()) {
|
||||
if (shaping.positionedGlyphs.size()) {
|
||||
glyphAtlas.addGlyphs(tileUID, feature.label, layout.text.font, fontStack, face);
|
||||
}
|
||||
}
|
||||
@@ -211,7 +211,7 @@ void SymbolBucket::addFeatures(const GeometryTileLayer& layer,
|
||||
}
|
||||
|
||||
// if either shaping or icon position is present, add the feature
|
||||
if (shaping.size() || image) {
|
||||
if (shaping.positionedGlyphs.size() || image) {
|
||||
for (const std::vector<Coordinate> &line : feature.geometry) {
|
||||
if (line.size()) {
|
||||
addFeature(line, shaping, face, image);
|
||||
@@ -240,7 +240,7 @@ void SymbolBucket::addFeature(const std::vector<Coordinate> &line, const Shaping
|
||||
const float fontScale = layout.text.max_size / glyphSize;
|
||||
const float textBoxScale = collision.tilePixelRatio * fontScale;
|
||||
const float iconBoxScale = collision.tilePixelRatio * layout.icon.max_size;
|
||||
const bool iconWithoutText = layout.text.optional || !shaping.size();
|
||||
const bool iconWithoutText = layout.text.optional || !shaping.positionedGlyphs.size();
|
||||
const bool textWithoutIcon = layout.icon.optional || !image;
|
||||
const bool avoidEdges = layout.avoid_edges && layout.placement != PlacementType::Line;
|
||||
|
||||
@@ -249,10 +249,10 @@ void SymbolBucket::addFeature(const std::vector<Coordinate> &line, const Shaping
|
||||
if (layout.placement == PlacementType::Line) {
|
||||
float resampleOffset = 0;
|
||||
|
||||
if (shaping.size()) {
|
||||
if (shaping.positionedGlyphs.size()) {
|
||||
float minX = std::numeric_limits<float>::infinity();
|
||||
float maxX = -std::numeric_limits<float>::infinity();
|
||||
for (const auto &glyph : shaping) {
|
||||
for (const auto &glyph : shaping.positionedGlyphs) {
|
||||
minX = std::min(minX, glyph.x);
|
||||
maxX = std::max(maxX, glyph.x);
|
||||
}
|
||||
@@ -287,7 +287,7 @@ void SymbolBucket::addFeature(const std::vector<Coordinate> &line, const Shaping
|
||||
|
||||
if (avoidEdges && !inside) continue;
|
||||
|
||||
if (shaping.size()) {
|
||||
if (shaping.positionedGlyphs.size()) {
|
||||
glyphPlacement = Placement::getGlyphs(anchor, origin, shaping, face, textBoxScale,
|
||||
horizontalText, line, layout);
|
||||
glyphScale =
|
||||
|
||||
@@ -54,7 +54,18 @@ public:
|
||||
float y = 0;
|
||||
};
|
||||
|
||||
typedef std::vector<PositionedGlyph> Shaping;
|
||||
class Shaping {
|
||||
public:
|
||||
inline explicit Shaping() : top(0), bottom(0), left(0), right(0) {}
|
||||
inline explicit Shaping(float x, float y)
|
||||
: top(-y), bottom(y), left(-x), right(x) {}
|
||||
std::vector<PositionedGlyph> positionedGlyphs;
|
||||
int32_t top;
|
||||
int32_t bottom;
|
||||
int32_t left;
|
||||
int32_t right;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -40,21 +40,24 @@ const Shaping FontStack::getShaping(const std::u32string &string, const float ma
|
||||
const float spacing, const vec2<float> &translate) const {
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
|
||||
Shaping shaping;
|
||||
Shaping shaping(translate.x, translate.y);
|
||||
|
||||
// the y offset *should* be part of the font metadata
|
||||
const int32_t yOffset = -17;
|
||||
|
||||
int32_t x = std::round(translate.x * 24); // one em
|
||||
const int32_t y = std::round(translate.y * 24); // one em
|
||||
const int32_t y = std::round(translate.y * 24) + yOffset; // one em
|
||||
|
||||
// Loop through all characters of this label and shape.
|
||||
for (uint32_t chr : string) {
|
||||
shaping.emplace_back(chr, x, y);
|
||||
shaping.positionedGlyphs.emplace_back(chr, x, y);
|
||||
auto metric = metrics.find(chr);
|
||||
if (metric != metrics.end()) {
|
||||
x += metric->second.advance + spacing;
|
||||
}
|
||||
}
|
||||
|
||||
if (!shaping.size())
|
||||
if (!shaping.positionedGlyphs.size())
|
||||
return shaping;
|
||||
|
||||
lineWrap(shaping, lineHeight, maxWidth, horizontalAlign, verticalAlign, justify);
|
||||
@@ -68,22 +71,22 @@ void align(Shaping &shaping, const float justify, const float horizontalAlign,
|
||||
const float shiftX = (justify - horizontalAlign) * maxLineLength;
|
||||
const float shiftY = (-verticalAlign * (line + 1) + 0.5) * lineHeight;
|
||||
|
||||
for (PositionedGlyph &glyph : shaping) {
|
||||
for (PositionedGlyph &glyph : shaping.positionedGlyphs) {
|
||||
glyph.x += shiftX;
|
||||
glyph.y += shiftY;
|
||||
}
|
||||
}
|
||||
|
||||
void justifyLine(Shaping &shaping, const std::map<uint32_t, GlyphMetrics> &metrics, uint32_t start,
|
||||
void justifyLine(std::vector<PositionedGlyph> &positionedGlyphs, const std::map<uint32_t, GlyphMetrics> &metrics, uint32_t start,
|
||||
uint32_t end, float justify) {
|
||||
PositionedGlyph &glyph = shaping[end];
|
||||
PositionedGlyph &glyph = positionedGlyphs[end];
|
||||
auto metric = metrics.find(glyph.glyph);
|
||||
if (metric != metrics.end()) {
|
||||
const uint32_t lastAdvance = metric->second.advance;
|
||||
const float lineIndent = float(glyph.x + lastAdvance) * justify;
|
||||
|
||||
for (uint32_t j = start; j <= end; j++) {
|
||||
shaping[j].x -= lineIndent;
|
||||
positionedGlyphs[j].x -= lineIndent;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -99,25 +102,27 @@ void FontStack::lineWrap(Shaping &shaping, const float lineHeight, const float m
|
||||
|
||||
uint32_t maxLineLength = 0;
|
||||
|
||||
std::vector<PositionedGlyph> &positionedGlyphs = shaping.positionedGlyphs;
|
||||
|
||||
if (maxWidth) {
|
||||
for (uint32_t i = 0; i < shaping.size(); i++) {
|
||||
PositionedGlyph &shape = shaping[i];
|
||||
for (uint32_t i = 0; i < positionedGlyphs.size(); i++) {
|
||||
PositionedGlyph &shape = positionedGlyphs[i];
|
||||
|
||||
shape.x -= lengthBeforeCurrentLine;
|
||||
shape.y += lineHeight * line;
|
||||
|
||||
if (shape.x > maxWidth && lastSafeBreak > 0) {
|
||||
|
||||
uint32_t lineLength = shaping[lastSafeBreak + 1].x;
|
||||
uint32_t lineLength = positionedGlyphs[lastSafeBreak + 1].x;
|
||||
maxLineLength = util::max(lineLength, maxLineLength);
|
||||
|
||||
for (uint32_t k = lastSafeBreak + 1; k <= i; k++) {
|
||||
shaping[k].y += lineHeight;
|
||||
shaping[k].x -= lineLength;
|
||||
positionedGlyphs[k].y += lineHeight;
|
||||
positionedGlyphs[k].x -= lineLength;
|
||||
}
|
||||
|
||||
if (justify) {
|
||||
justifyLine(shaping, metrics, lineStartIndex, lastSafeBreak - 1, justify);
|
||||
justifyLine(positionedGlyphs, metrics, lineStartIndex, lastSafeBreak - 1, justify);
|
||||
}
|
||||
|
||||
lineStartIndex = lastSafeBreak + 1;
|
||||
@@ -132,10 +137,22 @@ void FontStack::lineWrap(Shaping &shaping, const float lineHeight, const float m
|
||||
}
|
||||
}
|
||||
|
||||
if (!maxLineLength) maxLineLength = shaping.back().x;
|
||||
const PositionedGlyph& lastPositionedGlyph = positionedGlyphs.back();
|
||||
const auto lastGlyphMetric = metrics.find(lastPositionedGlyph.glyph);
|
||||
assert(lastGlyphMetric != metrics.end());
|
||||
const uint32_t lastLineLength = lastPositionedGlyph.x + lastGlyphMetric->second.advance;
|
||||
maxLineLength = std::max(maxLineLength, lastLineLength);
|
||||
|
||||
justifyLine(shaping, metrics, lineStartIndex, uint32_t(shaping.size()) - 1, justify);
|
||||
const uint32_t height = (line + 1) * lineHeight;
|
||||
|
||||
justifyLine(positionedGlyphs, metrics, lineStartIndex, uint32_t(positionedGlyphs.size()) - 1, justify);
|
||||
align(shaping, justify, horizontalAlign, verticalAlign, maxLineLength, lineHeight, line);
|
||||
|
||||
// Calculate the bounding box
|
||||
shaping.top += -verticalAlign * height;
|
||||
shaping.bottom = shaping.top + height;
|
||||
shaping.left += -horizontalAlign * maxLineLength;
|
||||
shaping.right = shaping.left + maxLineLength;
|
||||
}
|
||||
|
||||
GlyphPBF::GlyphPBF(const std::string &glyphURL,
|
||||
|
||||
@@ -211,7 +211,7 @@ Placement Placement::getGlyphs(Anchor &anchor, const vec2<float> &origin, const
|
||||
|
||||
const uint32_t buffer = 3;
|
||||
|
||||
for (const PositionedGlyph &shape : shaping) {
|
||||
for (const PositionedGlyph &shape : shaping.positionedGlyphs) {
|
||||
auto face_it = face.find(shape.glyph);
|
||||
if (face_it == face.end())
|
||||
continue;
|
||||
|
||||
Reference in New Issue
Block a user