From fea3aa573ad49003068f94374d766300fc35247f Mon Sep 17 00:00:00 2001 From: Mike Morris Date: Tue, 12 Apr 2016 18:33:39 -0700 Subject: everything is wrong but harfbuzz output is being consumed --- src/mbgl/text/font.cpp | 42 +++++++++++++++++++++++++++--------------- src/mbgl/text/font.hpp | 6 +++++- src/mbgl/text/font_stack.cpp | 9 +++++++-- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/mbgl/text/font.cpp b/src/mbgl/text/font.cpp index 3ee3b5f33a..34cfda4501 100644 --- a/src/mbgl/text/font.cpp +++ b/src/mbgl/text/font.cpp @@ -5,7 +5,6 @@ #include #include -#include #include namespace mbgl { @@ -48,35 +47,38 @@ void Font::load() { hb_blob_destroy(blob); font_ = hb_font_create(face); -#if 1 // TODO: Font size - unsigned int upem = hb_face_get_upem(face); - hb_font_set_scale(font_, upem, upem); -#endif + upem_ = hb_face_get_upem(face); + hb_font_set_scale(font_, upem_, upem_); + hb_face_destroy(face); hb_ft_font_set_funcs(font_); } -void Font::shape(const std::u32string &text) -{ - if (!font_) return; +Shaping Font::shape(const std::u32string &text, const float spacing, const vec2 &translate) { + Shaping shaping(translate.x * 24, translate.y * 24, text); + + // TODO: the y offset *should* be part of the font metadata + // const int32_t yOffset = -17; + + if (!font_) return shaping; + /* std::wstring_convert, char32_t> cv; std::cout << cv.to_bytes(text) << std::endl; + */ hb_buffer_reset(buffer_); - hb_buffer_add_utf32(buffer_, reinterpret_cast(text.c_str()), text.length(), 0, text.length()); hb_buffer_set_direction(buffer_, HB_DIRECTION_RTL); + // hb_buffer_set_direction(buffer, hb_direction_from_string (direction, -1)); + hb_buffer_set_script(buffer_, HB_SCRIPT_ARABIC); - hb_buffer_set_language(buffer_, hb_language_from_string("ar", -1)); + // hb_buffer_set_script(buffer, hb_script_from_string (script, -1)); -#if 0 - hb_buffer_set_direction(buffer, hb_direction_from_string (direction, -1)); - hb_buffer_set_script(buffer, hb_script_from_string (script, -1)); - hb_buffer_set_language(buffer, hb_language_from_string (language, -1)); -#endif + hb_buffer_set_language(buffer_, hb_language_from_string("ar", -1)); + // hb_buffer_set_language(buffer, hb_language_from_string (language, -1)); hb_shape(font_, buffer_, 0 /*features*/, 0 /*num_features*/); @@ -84,7 +86,14 @@ void Font::shape(const std::u32string &text) hb_glyph_info_t *hb_glyph = hb_buffer_get_glyph_infos(buffer_, NULL); hb_glyph_position_t *hb_position = hb_buffer_get_glyph_positions(buffer_, NULL); + // Loop through all characters of this label and shape. for (int i = 0; i < num_glyphs; i++) { + uint32_t codepoint = hb_glyph[i].codepoint; + shaping.positionedGlyphs.emplace_back(codepoint, + hb_position[i].x_advance + spacing, + hb_position[i].y_advance); + + /* std::cout << "glyph codepoint:" << hb_glyph[i].codepoint << " cluster: " << hb_glyph[i].cluster << " mask: " << hb_glyph[i].mask << @@ -93,8 +102,11 @@ void Font::shape(const std::u32string &text) // " x_offset: "<< hb_position[i].x_offset << // " y_offset: "<< hb_position[i].y_offset << "\n"; // std::cout << "glyph:" << hb_glyph->codepoint << "\n"; + */ } + return shaping; + /* auto hb_buffer_deleter = [](hb_buffer_t * buffer) { hb_buffer_destroy(buffer);}; const std::unique_ptr buffer(hb_buffer_create(),hb_buffer_deleter); diff --git a/src/mbgl/text/font.hpp b/src/mbgl/text/font.hpp index afb3248888..9d34b18df1 100644 --- a/src/mbgl/text/font.hpp +++ b/src/mbgl/text/font.hpp @@ -1,6 +1,9 @@ #ifndef MBGL_TEXT_FONT #define MBGL_TEXT_FONT +#include +#include + #include #include @@ -12,7 +15,7 @@ public: Font(const std::string &filename); ~Font(); - void shape(const std::u32string &text); + Shaping shape(const std::u32string &text, const float spacing, const vec2 &translate); private: void load(); @@ -20,6 +23,7 @@ private: std::string filename_; hb_font_t *font_; hb_buffer_t *buffer_; + unsigned int upem_; }; } // end namespace mbgl diff --git a/src/mbgl/text/font_stack.cpp b/src/mbgl/text/font_stack.cpp index d49e128f51..4c27a3dc5f 100644 --- a/src/mbgl/text/font_stack.cpp +++ b/src/mbgl/text/font_stack.cpp @@ -35,26 +35,31 @@ const Shaping FontStack::getShaping(const std::u32string &string, const float ma const float lineHeight, const float horizontalAlign, const float verticalAlign, const float justify, const float spacing, const vec2 &translate) const { + /* Shaping shaping(translate.x * 24, translate.y * 24, string); - // the y offset *should* be part of the font metadata + // TODO: the y offset *should* be part of the font metadata const int32_t yOffset = -17; float x = 0; const float y = yOffset; + */ // Create new Harfbuzz font object Font font("/Library/Fonts/Arial Unicode.ttf"); - font.shape(string); + Shaping shaping = font.shape(string, spacing, translate); + /* // Loop through all characters of this label and shape. for (uint32_t chr : string) { auto it = sdfs.find(chr); if (it != sdfs.end()) { + // TODO: change this instead of assuming they stay in order shaping.positionedGlyphs.emplace_back(chr, x, y); x += it->second.metrics.advance + spacing; } } + */ if (shaping.positionedGlyphs.empty()) return shaping; -- cgit v1.2.1