From eec39ff823d6f18ab2d53af945dd1de6d4fca563 Mon Sep 17 00:00:00 2001 From: "r.kuznetsov" Date: Wed, 23 Dec 2015 14:10:58 +0300 Subject: [PATCH 1/2] Fixed crash on font loading on startup --- drape/glyph_manager.cpp | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/drape/glyph_manager.cpp b/drape/glyph_manager.cpp index eb0b973ba9..12533c6be6 100644 --- a/drape/glyph_manager.cpp +++ b/drape/glyph_manager.cpp @@ -126,6 +126,7 @@ class Font public: Font(uint32_t sdfScale, ReaderPtr fontReader, FT_Library lib) : m_fontReader(fontReader) + , m_fontFace(nullptr) , m_sdfScale(sdfScale) { m_stream.base = 0; @@ -152,10 +153,18 @@ public: FREETYPE_CHECK(FT_Open_Face(lib, &args, 0, &m_fontFace)); } + bool IsValid() const + { + return m_fontFace != nullptr; + } + void DestroyFont() { - FREETYPE_CHECK(FT_Done_Face(m_fontFace)); - m_fontFace = nullptr; + if (m_fontFace != nullptr) + { + FREETYPE_CHECK(FT_Done_Face(m_fontFace)); + m_fontFace = nullptr; + } } bool HasGlyph(strings::UniChar unicodePoint) const @@ -396,12 +405,22 @@ GlyphManager::GlyphManager(GlyphManager::Params const & params) vector charCodes; try { - m_impl->m_fonts.emplace_back(make_unique(params.m_sdfScale, GetPlatform().GetReader(fontName), m_impl->m_library)); - m_impl->m_fonts.back()->GetCharcodes(charCodes); + auto fontPtr = make_unique(params.m_sdfScale, GetPlatform().GetReader(fontName), m_impl->m_library); + if (fontPtr->IsValid()) + { + fontPtr->GetCharcodes(charCodes); + m_impl->m_fonts.push_back(move(fontPtr)); + } + else + { + // A font is not valid, skip it. + continue; + } } catch(RootException const & e) { LOG(LWARNING, ("Error read font file : ", e.what())); + continue; } typedef size_t TBlockIndex; From bf44de358b7de92ae99ddfa80de8f8488c7d8827 Mon Sep 17 00:00:00 2001 From: "r.kuznetsov" Date: Wed, 23 Dec 2015 14:38:12 +0300 Subject: [PATCH 2/2] Review fixes --- drape/glyph_manager.cpp | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/drape/glyph_manager.cpp b/drape/glyph_manager.cpp index 12533c6be6..cf0c59d468 100644 --- a/drape/glyph_manager.cpp +++ b/drape/glyph_manager.cpp @@ -124,6 +124,8 @@ void ParseFontList(string const & fontListFile, ToDo toDo) class Font { public: + DECLARE_EXCEPTION(InvalidFontException, RootException); + Font(uint32_t sdfScale, ReaderPtr fontReader, FT_Library lib) : m_fontReader(fontReader) , m_fontFace(nullptr) @@ -150,12 +152,18 @@ public: args.num_params = 0; args.params = 0; - FREETYPE_CHECK(FT_Open_Face(lib, &args, 0, &m_fontFace)); + FT_Error const err = FT_Open_Face(lib, &args, 0, &m_fontFace); +#ifdef DEBUG + if (err) + LOG(LWARNING, ("Freetype:", g_FT_Errors[err].m_code, g_FT_Errors[err].m_message)); +#endif + if (err || !IsValid()) + MYTHROW(InvalidFontException, ()); } bool IsValid() const { - return m_fontFace != nullptr; + return m_fontFace != nullptr && m_fontFace->num_glyphs > 0; } void DestroyFont() @@ -405,17 +413,8 @@ GlyphManager::GlyphManager(GlyphManager::Params const & params) vector charCodes; try { - auto fontPtr = make_unique(params.m_sdfScale, GetPlatform().GetReader(fontName), m_impl->m_library); - if (fontPtr->IsValid()) - { - fontPtr->GetCharcodes(charCodes); - m_impl->m_fonts.push_back(move(fontPtr)); - } - else - { - // A font is not valid, skip it. - continue; - } + m_impl->m_fonts.emplace_back(make_unique(params.m_sdfScale, GetPlatform().GetReader(fontName), m_impl->m_library)); + m_impl->m_fonts.back()->GetCharcodes(charCodes); } catch(RootException const & e) {