From 2c217c6a25c8d4af305678078af1b31312745375 Mon Sep 17 00:00:00 2001 From: Chris Loer Date: Thu, 23 Feb 2017 16:47:48 -0800 Subject: Determine widths of glyph clusters for use in line breaking algorithm. --- src/mbgl/text/glyph_set.cpp | 57 ++++++++++++++++++++++----------------- src/mbgl/text/glyph_set.hpp | 10 +++---- src/mbgl/text/harfbuzz_shaper.cpp | 44 ++++++++++++++++++++++++++++-- src/mbgl/text/harfbuzz_shaper.hpp | 3 ++- 4 files changed, 80 insertions(+), 34 deletions(-) diff --git a/src/mbgl/text/glyph_set.cpp b/src/mbgl/text/glyph_set.cpp index da6654b704..5430980964 100644 --- a/src/mbgl/text/glyph_set.cpp +++ b/src/mbgl/text/glyph_set.cpp @@ -9,6 +9,7 @@ #include #include +#include namespace mbgl { @@ -35,6 +36,8 @@ void GlyphSet::insert(uint32_t id, SDFGlyph&& glyph) { const std::map& GlyphSet::getSDFs() const { return sdfs; } + + const Shaping GlyphSet::getShaping(const std::u16string& logicalInput, const float maxWidth, @@ -50,10 +53,12 @@ const Shaping GlyphSet::getShaping(const std::u16string& logicalInput, // The string stored in shaping.text is used for finding duplicates, but may end up quite // different from the glyphs that get shown Shaping shaping(translate.x * 24, translate.y * 24, logicalInput); + + std::vector> clusterWidths = localFont ? harfbuzz::getClusterWidths(localFont, logicalInput) : getClusterWidths(logicalInput, spacing); std::vector reorderedLines = bidi.processText(logicalInput, - determineLineBreaks(logicalInput, spacing, maxWidth)); + determineLineBreaks(clusterWidths, maxWidth)); shapeLines(shaping, reorderedLines, spacing, lineHeight, horizontalAlign, verticalAlign, justify, translate, localFont); @@ -102,16 +107,10 @@ void justifyLine(std::vector& positionedGlyphs, } } -float GlyphSet::determineAverageLineWidth(const std::u16string& logicalInput, - const float spacing, - float maxWidth) const { +float GlyphSet::determineAverageLineWidth(const std::vector>& clusters, float maxWidth) const { float totalWidth = 0; - - for (char16_t chr : logicalInput) { - auto it = sdfs.find(chr); - if (it != sdfs.end()) { - totalWidth += it->second.metrics.advance + spacing; - } + for (auto cluster : clusters) { + totalWidth += cluster.second; } int32_t targetLineCount = std::fmax(1, std::ceil(totalWidth / maxWidth)); @@ -196,43 +195,51 @@ std::set leastBadBreaks(const PotentialBreak& lastLineBreak) { return leastBadBreaks; } +std::vector> GlyphSet::getClusterWidths(const std::u16string& logicalInput, const float spacing) const { + std::vector> clusterWidths; + for (std::size_t i = 0; i < logicalInput.size(); i++) { + const char16_t codePoint = logicalInput[i]; + auto it = sdfs.find(codePoint); + if (it != sdfs.end() && !boost::algorithm::is_any_of(u" \t\n\v\f\r")(codePoint)) { + clusterWidths.emplace_back(codePoint, it->second.metrics.advance + spacing); + } else { + clusterWidths.emplace_back(codePoint, 0); + } + } + return clusterWidths; +} // We determine line breaks based on shaped text in logical order. Working in visual order would be // more intuitive, but we can't do that because the visual order may be changed by line breaks! -std::set GlyphSet::determineLineBreaks(const std::u16string& logicalInput, - const float spacing, - float maxWidth) const { +std::set GlyphSet::determineLineBreaks(const std::vector>& clusters, float maxWidth) const { if (!maxWidth) { return {}; } - if (logicalInput.empty()) { + if (clusters.empty()) { return {}; } - const float targetWidth = determineAverageLineWidth(logicalInput, spacing, maxWidth); + const float targetWidth = determineAverageLineWidth(clusters, maxWidth); std::list potentialBreaks; float currentX = 0; - for (std::size_t i = 0; i < logicalInput.size(); i++) { - const char16_t codePoint = logicalInput[i]; - auto it = sdfs.find(codePoint); - if (it != sdfs.end() && !boost::algorithm::is_any_of(u" \t\n\v\f\r")(codePoint)) { - currentX += it->second.metrics.advance + spacing; - } + for (std::size_t i = 0; i < clusters.size(); i++) { + const char16_t codePoint = clusters[i].first; + currentX += clusters[i].second; // Ideographic characters, spaces, and word-breaking punctuation that often appear without // surrounding spaces. - if ((i < logicalInput.size() - 1) && + if ((i < clusters.size() - 1) && (util::i18n::allowsWordBreaking(codePoint) || util::i18n::allowsIdeographicBreaking(codePoint))) { potentialBreaks.push_back(evaluateBreak(i+1, currentX, targetWidth, potentialBreaks, - calculatePenalty(codePoint, logicalInput[i+1]), + calculatePenalty(codePoint, clusters[i+1].first), false)); } } - return leastBadBreaks(evaluateBreak(logicalInput.size(), currentX, targetWidth, potentialBreaks, 0, true)); + return leastBadBreaks(evaluateBreak(clusters.size(), currentX, targetWidth, potentialBreaks, 0, true)); } void GlyphSet::shapeLines(Shaping& shaping, @@ -265,7 +272,7 @@ void GlyphSet::shapeLines(Shaping& shaping, std::size_t lineStartIndex = shaping.positionedGlyphs.size(); if (localFont) { - harfbuzz::applyShaping(localFont, util::utf16_to_utf8::convert(line), shaping.positionedGlyphs, x, y); + harfbuzz::applyShaping(localFont, line, shaping.positionedGlyphs, x, y); } else { for (char16_t chr : line) { auto it = sdfs.find(chr); diff --git a/src/mbgl/text/glyph_set.hpp b/src/mbgl/text/glyph_set.hpp index 6354f5803c..5df639396a 100644 --- a/src/mbgl/text/glyph_set.hpp +++ b/src/mbgl/text/glyph_set.hpp @@ -24,12 +24,10 @@ public: hb_font_t* localFont) const; private: - float determineAverageLineWidth(const std::u16string& logicalInput, - const float spacing, - float maxWidth) const; - std::set determineLineBreaks(const std::u16string& logicalInput, - const float spacing, - float maxWidth) const; + std::vector> getClusterWidths(const std::u16string& logicalInput, const float spacing) const; + + float determineAverageLineWidth(const std::vector>& clusters, float maxWidth) const; + std::set determineLineBreaks(const std::vector>& clusters, float maxWidth) const; void shapeLines(Shaping& shaping, const std::vector& lines, diff --git a/src/mbgl/text/harfbuzz_shaper.cpp b/src/mbgl/text/harfbuzz_shaper.cpp index 6c945c46c2..d234548dea 100644 --- a/src/mbgl/text/harfbuzz_shaper.cpp +++ b/src/mbgl/text/harfbuzz_shaper.cpp @@ -3,6 +3,9 @@ #include #include +#include +#include + // freetype2 extern "C" { @@ -50,11 +53,48 @@ RequiredGlyphsForFont getGlyphIDs(const LocalFonts& localFonts, const std::strin return RequiredGlyphsForFont(localFonts.back().id, RequiredGlyphs()); // All tofu for this label } -void applyShaping(hb_font_t* font, const std::string& u8text, std::vector& positionedGlyphs, float& current_x, float& current_y) { +// TODO: This can share a shaping call with getGlyphIDs, although the timing is awkward because the non-Harfbuzz pathway can't do shaping until later when the glyphs have already been downloaded + +std::vector> getClusterWidths(hb_font_t* font, const std::u16string& u16text) { + hb_buffer_t *hb_buffer; + hb_buffer = hb_buffer_create (); + + hb_buffer_add_utf16(hb_buffer, reinterpret_cast(u16text.c_str()), -1, 0, -1); + hb_buffer_guess_segment_properties(hb_buffer); + hb_buffer_set_direction(hb_buffer, HB_DIRECTION_LTR); // We set the direction LTR because by the time we get here we've already reversed RTL text with BiDi + + hb_shape(font, hb_buffer, NULL, 0); + + /* Get glyph information and positions out of the buffer. */ + unsigned int len = hb_buffer_get_length(hb_buffer); + hb_glyph_info_t *info = hb_buffer_get_glyph_infos(hb_buffer, NULL); + hb_glyph_position_t *pos = hb_buffer_get_glyph_positions(hb_buffer, NULL); + + std::vector> clusterWidths; + + // TODO: Re-write to collect all data in a single pass through info/pos... + for (size_t i = 0; i < u16text.size(); i++) { + double accumulatedWidth = 0; + for (unsigned int j = 0; j < len; j++) { + if (info[j].cluster == i) { + accumulatedWidth += pos[j].x_advance / 64.; + } + } + // NOTE: Some codepoints won't have any glyphs associated with them -- we just make those into zero-width clusters. + clusterWidths.emplace_back(u16text[i],accumulatedWidth); + } + + hb_buffer_destroy(hb_buffer); + + return clusterWidths; +} + +void applyShaping(hb_font_t* font, const std::u16string& u16text, std::vector& positionedGlyphs, float& current_x, float& current_y) { + hb_buffer_t *hb_buffer; hb_buffer = hb_buffer_create (); - hb_buffer_add_utf8(hb_buffer, u8text.c_str(), -1, 0, -1); + hb_buffer_add_utf16(hb_buffer, reinterpret_cast(u16text.c_str()), -1, 0, -1); hb_buffer_guess_segment_properties(hb_buffer); hb_buffer_set_direction(hb_buffer, HB_DIRECTION_LTR); // We set the direction LTR because by the time we get here we've already reversed RTL text with BiDi diff --git a/src/mbgl/text/harfbuzz_shaper.hpp b/src/mbgl/text/harfbuzz_shaper.hpp index 31e10adf65..75130e7306 100644 --- a/src/mbgl/text/harfbuzz_shaper.hpp +++ b/src/mbgl/text/harfbuzz_shaper.hpp @@ -13,7 +13,8 @@ namespace harfbuzz { RequiredGlyphsForFont getGlyphIDs(const LocalFonts& fonts, const std::string& u8label); -void applyShaping(hb_font_t* font, const std::string& u8text, std::vector& positionedGlyphs, float& current_x, float& current_y); +std::vector> getClusterWidths(hb_font_t* font, const std::u16string& u16text); +void applyShaping(hb_font_t* font, const std::u16string& u16text, std::vector& positionedGlyphs, float& current_x, float& current_y); } } -- cgit v1.2.1