From 7b29c5b2576d687fb8491291f1f28b09020100de Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Mon, 30 Mar 2015 18:52:25 -0700 Subject: [PATCH] port some of the text shaping changes --- src/mbgl/renderer/symbol_bucket.cpp | 12 +++---- src/mbgl/text/glyph.hpp | 13 +++++++- src/mbgl/text/glyph_store.cpp | 49 +++++++++++++++++++---------- src/mbgl/text/placement.cpp | 2 +- 4 files changed, 52 insertions(+), 24 deletions(-) diff --git a/src/mbgl/renderer/symbol_bucket.cpp b/src/mbgl/renderer/symbol_bucket.cpp index 46e8436a3..c50ac15df 100644 --- a/src/mbgl/renderer/symbol_bucket.cpp +++ b/src/mbgl/renderer/symbol_bucket.cpp @@ -195,7 +195,7 @@ void SymbolBucket::addFeatures(const GeometryTileLayer& layer, /* translate */ vec2(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 &line : feature.geometry) { if (line.size()) { addFeature(line, shaping, face, image); @@ -240,7 +240,7 @@ void SymbolBucket::addFeature(const std::vector &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 &line, const Shaping if (layout.placement == PlacementType::Line) { float resampleOffset = 0; - if (shaping.size()) { + if (shaping.positionedGlyphs.size()) { float minX = std::numeric_limits::infinity(); float maxX = -std::numeric_limits::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 &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 = diff --git a/src/mbgl/text/glyph.hpp b/src/mbgl/text/glyph.hpp index 4fbb75fc1..eee7572af 100644 --- a/src/mbgl/text/glyph.hpp +++ b/src/mbgl/text/glyph.hpp @@ -54,7 +54,18 @@ public: float y = 0; }; -typedef std::vector 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 positionedGlyphs; + int32_t top; + int32_t bottom; + int32_t left; + int32_t right; +}; + } #endif diff --git a/src/mbgl/text/glyph_store.cpp b/src/mbgl/text/glyph_store.cpp index ab1776c04..06a211351 100644 --- a/src/mbgl/text/glyph_store.cpp +++ b/src/mbgl/text/glyph_store.cpp @@ -40,21 +40,24 @@ const Shaping FontStack::getShaping(const std::u32string &string, const float ma const float spacing, const vec2 &translate) const { std::lock_guard 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 &metrics, uint32_t start, +void justifyLine(std::vector &positionedGlyphs, const std::map &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 &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, diff --git a/src/mbgl/text/placement.cpp b/src/mbgl/text/placement.cpp index 9d9e82c75..ff25204f2 100644 --- a/src/mbgl/text/placement.cpp +++ b/src/mbgl/text/placement.cpp @@ -211,7 +211,7 @@ Placement Placement::getGlyphs(Anchor &anchor, const vec2 &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;