From 52d9330114a0e005a0a4575675d2c9fbd1e05b82 Mon Sep 17 00:00:00 2001 From: tomangelo2 Date: Sun, 15 Oct 2017 23:31:06 +0200 Subject: [PATCH] Replaced multiple methods with one --- src/common/font_config.cpp | 172 ++++++++++++++++++----------------- src/common/font_config.h | 31 +------ src/graphics/engine/text.cpp | 16 ++-- 3 files changed, 99 insertions(+), 120 deletions(-) diff --git a/src/common/font_config.cpp b/src/common/font_config.cpp index e4dc51b1..11d55833 100644 --- a/src/common/font_config.cpp +++ b/src/common/font_config.cpp @@ -27,6 +27,8 @@ #include "common/system/system.h" +#include "graphics/engine/text.h" + #include #include #include @@ -84,92 +86,92 @@ bool CFontConfigFile::Init() return true; } -std::string CFontConfigFile::GetCommonFont() +std::string CFontConfigFile::GetFont(Gfx::FontType type) { - try + switch(type) { - std::string path = std::string("/fonts/") + m_propertyTree.get("FontCommon"); - return path; + case Gfx::FONT_COMMON: + { + try + { + std::string path = std::string("/fonts/") + m_propertyTree.get("FontCommon"); + return path; + } + catch (std::exception & e) + { + GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s. Default font will be used instead.\n", e.what()); + return "/fonts/dvu_sans.ttf"; + } + } + case Gfx::FONT_COMMON_BOLD: + { + try + { + std::string path = std::string("/fonts/") + m_propertyTree.get("FontCommonBold"); + return path; + } + catch (std::exception & e) + { + GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s. Default font will be used instead.\n", e.what()); + return "/fonts/dvu_sans_bold.ttf"; + } + } + case Gfx::FONT_COMMON_ITALIC: + { + try + { + std::string path = std::string("/fonts/") + m_propertyTree.get("FontCommonItalic"); + return path; + } + catch (std::exception & e) + { + GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s. Default font will be used instead.\n", e.what()); + return "/fonts/dvu_sans_italic.ttf"; + } + } + case Gfx::FONT_STUDIO: + { + try + { + std::string path = std::string("/fonts/") + m_propertyTree.get("FontStudio"); + return path; + } + catch (std::exception & e) + { + GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s. Default font will be used instead.\n", e.what()); + return "/fonts/dvu_sans_mono.ttf"; + } + } + case Gfx::FONT_STUDIO_BOLD: + { + try + { + std::string path = std::string("/fonts/") + m_propertyTree.get("FontStudioBold"); + return path; + } + catch (std::exception & e) + { + GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s. Default font will be used instead.\n", e.what()); + return "/fonts/dvu_sans_mono_bold.ttf"; + } + } + case Gfx::FONT_SATCOM: + { + try + { + std::string path = std::string("/fonts/") + m_propertyTree.get("FontSatCom"); + return path; + } + catch (std::exception & e) + { + GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s. Default font will be used instead.\n", e.what()); + return "/fonts/dvu_sans.ttf"; + } + } + default: + { + GetLogger()->Debug("Incorrect font type: %i.\n", type); + return nullptr; + } } - catch (std::exception & e) - { - GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s. Default font will be used instead.\n", e.what()); - return "/fonts/dvu_sans.ttf"; - } - return ""; -} - -std::string CFontConfigFile::GetCommonBoldFont() -{ - try - { - std::string path = std::string("/fonts/") + m_propertyTree.get("FontCommonBold"); - return path; - } - catch (std::exception & e) - { - GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s. Default font will be used instead.\n", e.what()); - return "/fonts/dvu_sans_bold.ttf"; - } - return ""; -} - -std::string CFontConfigFile::GetCommonItalicFont() -{ - try - { - std::string path = std::string("/fonts/") + m_propertyTree.get("FontCommonItalic"); - return path; - } - catch (std::exception & e) - { - GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s. Default font will be used instead.\n", e.what()); - return "/fonts/dvu_sans_italic.ttf"; - } - return ""; -} - -std::string CFontConfigFile::GetStudioFont() -{ - try - { - std::string path = std::string("/fonts/") + m_propertyTree.get("FontStudio"); - return path; - } - catch (std::exception & e) - { - GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s. Default font will be used instead.\n", e.what()); - return "/fonts/dvu_sans_mono.ttf"; - } - return ""; -} - -std::string CFontConfigFile::GetStudioBoldFont() -{ - try - { - std::string path = std::string("/fonts/") + m_propertyTree.get("FontStudioBold"); - return path; - } - catch (std::exception & e) - { - GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s. Default font will be used instead.\n", e.what()); - return "/fonts/dvu_sans_mono_bold.ttf"; - } - return ""; -} - -std::string CFontConfigFile::GetSatComFont() -{ - try - { - std::string path = std::string("/fonts/") + m_propertyTree.get("FontSatCom"); - return path; - } - catch (std::exception & e) - { - GetLogger()->Log(m_loaded ? LOG_INFO : LOG_TRACE, "Error on parsing config file: %s. Default font will be used instead.\n", e.what()); - return "/fonts/dvu_sans.ttf"; - } - return ""; } diff --git a/src/common/font_config.h b/src/common/font_config.h index cbe0863e..cbe2d3d9 100644 --- a/src/common/font_config.h +++ b/src/common/font_config.h @@ -26,6 +26,8 @@ #include "common/singleton.h" +#include "graphics/engine/text.h" + #include #include @@ -48,35 +50,10 @@ public: */ bool Init(); - /** Reads common font from file + /** Reads given font from file * \return return path to font file */ - std::string GetCommonFont(); - - /** Reads common bold font from file - * \return return path to font file - */ - std::string GetCommonBoldFont(); - - /** Reads common italic font from file - * \return return path to font file - */ - std::string GetCommonItalicFont(); - - /** Reads studio font from file - * \return return path to font file - */ - std::string GetStudioFont(); - - /** Reads studio bold font from file - * \return returns path to font file - */ - std::string GetStudioBoldFont(); - - /** Reads satcom font from file - * \return returns path to font file - */ - std::string GetSatComFont(); + std::string GetFont(Gfx::FontType type); private: boost::property_tree::ptree m_propertyTree; diff --git a/src/graphics/engine/text.cpp b/src/graphics/engine/text.cpp index 6ecf9284..016b1d22 100644 --- a/src/graphics/engine/text.cpp +++ b/src/graphics/engine/text.cpp @@ -127,16 +127,16 @@ bool CText::Create() m_error = std::string("TTF_Init error: ") + std::string(TTF_GetError()); return false; } - - m_fonts[FONT_COMMON] = MakeUnique(GetFontConfigFile().GetCommonFont()); - m_fonts[FONT_COMMON_BOLD] = MakeUnique(GetFontConfigFile().GetCommonBoldFont()); - m_fonts[FONT_COMMON_ITALIC] = MakeUnique(GetFontConfigFile().GetCommonItalicFont()); - m_fonts[FONT_SATCOM] = MakeUnique(GetFontConfigFile().GetSatComFont()); - - m_fonts[FONT_STUDIO] = MakeUnique(GetFontConfigFile().GetStudioFont()); - m_fonts[FONT_STUDIO_BOLD] = MakeUnique(GetFontConfigFile().GetStudioBoldFont()); + m_fonts[FONT_COMMON] = MakeUnique(GetFontConfigFile().GetFont(FONT_COMMON)); + m_fonts[FONT_COMMON_BOLD] = MakeUnique(GetFontConfigFile().GetFont(FONT_COMMON_BOLD)); + m_fonts[FONT_COMMON_ITALIC] = MakeUnique(GetFontConfigFile().GetFont(FONT_COMMON_ITALIC)); + + m_fonts[FONT_SATCOM] = MakeUnique(GetFontConfigFile().GetFont(FONT_SATCOM)); + m_fonts[FONT_STUDIO] = MakeUnique(GetFontConfigFile().GetFont(FONT_STUDIO)); + m_fonts[FONT_STUDIO_BOLD] = MakeUnique(GetFontConfigFile().GetFont(FONT_STUDIO_BOLD)); + for (auto it = m_fonts.begin(); it != m_fonts.end(); ++it) { FontType type = (*it).first;