summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Loer <chris.loer@gmail.com>2017-02-23 16:47:48 -0800
committerChris Loer <chris.loer@gmail.com>2017-02-23 16:47:48 -0800
commit2c217c6a25c8d4af305678078af1b31312745375 (patch)
treee637775774fa468f5204ab1092c2c9c36087b5c6
parentf66d98095e6ab682f06460e3aac0268ee6db341b (diff)
downloadqtlocation-mapboxgl-upstream/cloer_harfbuzz.tar.gz
Determine widths of glyph clusters for use in line breaking algorithm.upstream/cloer_harfbuzz
-rw-r--r--src/mbgl/text/glyph_set.cpp57
-rw-r--r--src/mbgl/text/glyph_set.hpp10
-rw-r--r--src/mbgl/text/harfbuzz_shaper.cpp44
-rw-r--r--src/mbgl/text/harfbuzz_shaper.hpp3
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 <algorithm>
#include <cassert>
+#include <numeric>
namespace mbgl {
@@ -35,6 +36,8 @@ void GlyphSet::insert(uint32_t id, SDFGlyph&& glyph) {
const std::map<uint32_t, SDFGlyph>& 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<std::pair<char16_t,double>> clusterWidths = localFont ? harfbuzz::getClusterWidths(localFont, logicalInput) : getClusterWidths(logicalInput, spacing);
std::vector<std::u16string> 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<PositionedGlyph>& positionedGlyphs,
}
}
-float GlyphSet::determineAverageLineWidth(const std::u16string& logicalInput,
- const float spacing,
- float maxWidth) const {
+float GlyphSet::determineAverageLineWidth(const std::vector<std::pair<char16_t,double>>& 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<std::size_t> leastBadBreaks(const PotentialBreak& lastLineBreak) {
return leastBadBreaks;
}
+std::vector<std::pair<char16_t,double>> GlyphSet::getClusterWidths(const std::u16string& logicalInput, const float spacing) const {
+ std::vector<std::pair<char16_t,double>> 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<std::size_t> GlyphSet::determineLineBreaks(const std::u16string& logicalInput,
- const float spacing,
- float maxWidth) const {
+std::set<std::size_t> GlyphSet::determineLineBreaks(const std::vector<std::pair<char16_t,double>>& 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<PotentialBreak> 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<std::size_t> determineLineBreaks(const std::u16string& logicalInput,
- const float spacing,
- float maxWidth) const;
+ std::vector<std::pair<char16_t,double>> getClusterWidths(const std::u16string& logicalInput, const float spacing) const;
+
+ float determineAverageLineWidth(const std::vector<std::pair<char16_t,double>>& clusters, float maxWidth) const;
+ std::set<std::size_t> determineLineBreaks(const std::vector<std::pair<char16_t,double>>& clusters, float maxWidth) const;
void shapeLines(Shaping& shaping,
const std::vector<std::u16string>& 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 <harfbuzz/hb.h>
#include <harfbuzz/hb-ft.h>
+#include <mutex>
+#include <iostream>
+
// 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<PositionedGlyph>& 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<std::pair<char16_t,double>> 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<const uint16_t*>(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<std::pair<char16_t,double>> 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<PositionedGlyph>& 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<const uint16_t*>(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<PositionedGlyph>& positionedGlyphs, float& current_x, float& current_y);
+std::vector<std::pair<char16_t,double>> getClusterWidths(hb_font_t* font, const std::u16string& u16text);
+void applyShaping(hb_font_t* font, const std::u16string& u16text, std::vector<PositionedGlyph>& positionedGlyphs, float& current_x, float& current_y);
}
}