diff options
Diffstat (limited to 'src/mbgl/text/harfbuzz_shaper.cpp')
-rw-r--r-- | src/mbgl/text/harfbuzz_shaper.cpp | 44 |
1 files changed, 42 insertions, 2 deletions
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 |