diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e0857506..1cf50289 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -106,6 +106,8 @@ set(BASE_SOURCES common/error.h common/event.cpp common/event.h + common/font_file.h + common/font_file.cpp common/global.h common/image.cpp common/image.h diff --git a/src/app/app.h b/src/app/app.h index 20721e24..3835b418 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -44,6 +44,7 @@ class CSoundInterface; class CInput; class CPathManager; class CConfigFile; +class CFontConfigFile; class CSystemUtils; struct SystemTimeStamp; diff --git a/src/common/font_file.cpp b/src/common/font_file.cpp new file mode 100644 index 00000000..0b234d02 --- /dev/null +++ b/src/common/font_file.cpp @@ -0,0 +1,285 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2016, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + +#include "common/font_file.h" + +#include "common/logger.h" +#include "common/make_unique.h" + +#include "common/resources/inputstream.h" +#include "common/resources/outputstream.h" + +#include "common/system/system.h" + +#include +#include +#include +#include +#include + +namespace bp = boost::property_tree; + +CFontConfigFile::CFontConfigFile() + : m_needsSave(false) + , m_useCurrentDirectory(false) + , m_loaded(false) +{ +} + +CFontConfigFile::~CFontConfigFile() +{ + if (m_needsSave) + { + GetLogger()->Warn("Font config file was not properly saved! Saving now...\n"); + Save(); + } +} + +bool CFontConfigFile::Init() +{ + try + { + std::unique_ptr stream; + bool good; + if (m_useCurrentDirectory) + { + auto inputStream = MakeUnique("./fonts.ini"); + good = inputStream->good(); + stream = std::move(inputStream); + } + else + { + auto inputStream = MakeUnique("fonts.ini"); + good = inputStream->is_open(); + stream = std::move(inputStream); + } + + if (good) + { + bp::ini_parser::read_ini(*stream, m_propertyTree); + m_loaded = true; + } + else + { + return false; + } + } + catch (std::exception & e) + { + GetLogger()->Error("Error on parsing config file: %s\n", e.what()); + return false; + } + return true; +} + +bool CFontConfigFile::Save() +{ + if (m_needsSave) + { + try + { + std::unique_ptr stream; + bool good; + if (m_useCurrentDirectory) + { + auto outputStream = MakeUnique("./fonts.ini"); + good = outputStream->good(); + stream = std::move(outputStream); + } + else + { + auto outputStream = MakeUnique("fonts.ini"); + good = outputStream->is_open(); + stream = std::move(outputStream); + } + + if (good) + { + bp::ini_parser::write_ini(*stream, m_propertyTree); + m_needsSave = false; + } + else + { + GetLogger()->Error("Error on storing fonts config file: failed to open file\n"); + return false; + } + } + catch (std::exception & e) + { + GetLogger()->Error("Error on storing fonts config file: %s\n", e.what()); + return false; + } + } + return true; +} + +std::string CFontConfigFile::GetCommonFont() +{ + try + { + std::string path = std::string("/fonts/") + m_propertyTree.get("FONT_COMMON"); + 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()); + SetCommonFont("dvu_sans.ttf"); + return "/fonts/dvu_sans.ttf"; + } + return ""; +} + +bool CFontConfigFile::SetCommonFont(std::string filename) +{ + try + { + m_propertyTree.put("FONT_COMMON", filename); + m_needsSave = true; + } + catch (std::exception & e) + { + GetLogger()->Error("Error on editing config file: %s\n", e.what()); + return false; + } + return true; +} + +std::string CFontConfigFile::GetCommonBoldFont() +{ + try + { + std::string path = std::string("/fonts/") + m_propertyTree.get("FONT_COMMON_BOLD"); + 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()); + SetCommonBoldFont("dvu_sans_bold.ttf"); + return "/fonts/dvu_sans_bold.ttf"; + } + return ""; +} + +bool CFontConfigFile::SetCommonBoldFont(std::string filename) +{ + try + { + m_propertyTree.put("FONT_COMMON_BOLD", filename); + m_needsSave = true; + } + catch (std::exception & e) + { + GetLogger()->Error("Error on editing config file: %s\n", e.what()); + return false; + } + return true; +} + +std::string CFontConfigFile::GetCommonItalicFont() +{ + try + { + std::string path = std::string("/fonts/") + m_propertyTree.get("FONT_COMMON_ITALIC"); + 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()); + SetCommonItalicFont("dvu_sans_italic.ttf"); + return "/fonts/dvu_sans_italic.ttf"; + } + return ""; +} + +bool CFontConfigFile::SetCommonItalicFont(std::string filename) +{ + try + { + m_propertyTree.put("FONT_COMMON_ITALIC", filename); + m_needsSave = true; + } + catch (std::exception & e) + { + GetLogger()->Error("Error on editing config file: %s\n", e.what()); + return false; + } + return true; +} + +std::string CFontConfigFile::GetStudioFont() +{ + try + { + std::string path = std::string("/fonts/") + m_propertyTree.get("FONT_STUDIO"); + 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()); + SetStudioFont("dvu_sans_mono.ttf"); + return "/fonts/dvu_sans_mono.ttf"; + } + return ""; +} + +bool CFontConfigFile::SetStudioFont(std::string filename) +{ + try + { + m_propertyTree.put("FONT_STUDIO", filename); + m_needsSave = true; + } + catch (std::exception & e) + { + GetLogger()->Error("Error on editing config file: %s\n", e.what()); + return false; + } + return true; +} + +std::string CFontConfigFile::GetStudioBoldFont() +{ + try + { + std::string path = std::string("/fonts/") + m_propertyTree.get("FONT_STUDIO_BOLD"); + 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()); + SetStudioBoldFont("dvu_sans_mono_bold.ttf"); + return "/fonts/dvu_sans_mono_bold.ttf"; + } + return ""; +} + +bool CFontConfigFile::SetStudioBoldFont(std::string filename) +{ + try + { + m_propertyTree.put("FONT_STUDIO_BOLD", filename); + m_needsSave = true; + } + catch (std::exception & e) + { + GetLogger()->Error("Error on editing config file: %s\n", e.what()); + return false; + } + return true; +} diff --git a/src/common/font_file.h b/src/common/font_file.h new file mode 100644 index 00000000..c5d2869d --- /dev/null +++ b/src/common/font_file.h @@ -0,0 +1,147 @@ +/* + * This file is part of the Colobot: Gold Edition source code + * Copyright (C) 2001-2016, Daniel Roux, EPSITEC SA & TerranovaTeam + * http://epsitec.ch; http://colobot.info; http://github.com/colobot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://gnu.org/licenses + */ + + /** + * \file common/font_file.h + * \brief Class for loading fonts from /data/fonts/fonts.ini + */ + +#pragma once + +#include "common/singleton.h" + +#include + +#include + +/** +* \class CFontConfigFile +* +* \brief Class for loading config file +* +*/ + +class CFontConfigFile : public CSingleton +{ +public: + CFontConfigFile(); + virtual ~CFontConfigFile(); + + /** Loads fonts.ini + * \return return true on success + */ + bool Init(); + + /** Saves fonts.ini + * \return return true on success + */ + bool Save(); + + /** Reads common font from file + * \return return path to font file + */ + + std::string GetCommonFont(); + + /** Writes common font to file + * \return return true on success + */ + + bool SetCommonFont(std::string filename); + + /** Reads common bold font from file + * \return return path to font file + */ + + std::string GetCommonBoldFont(); + + /** Writes common bold font to file + * \return return true on success + */ + + bool SetCommonBoldFont(std::string filename); + + /** Reads common italic font from file + * \return return path to font file + */ + + std::string GetCommonItalicFont(); + + /** Writes common italic font to file + * \return return true on success + */ + + bool SetCommonItalicFont(std::string filename); + + /** Reads studio font from file + * \return return path to font file + */ + + std::string GetStudioFont(); + + /** Writes studio font to file + * \return return true on success + */ + + bool SetStudioFont(std::string filename); + + /** Reads studio bold font from file + * \return returns path to font file + */ + + std::string GetStudioBoldFont(); + + /** Writes studio bold font to file + * \return return true on success + */ + + bool SetStudioBoldFont(std::string filename); + +private: + boost::property_tree::ptree m_propertyTree; + bool m_needsSave; + bool m_useCurrentDirectory; + bool m_loaded; + + /*std::string m_colobotFont; + std::string m_colobotFontb; + std::string m_colobotFonti; + + std::string m_courierFont; + std::string m_courierFontb;*/ +}; + +/** + * \enum Fonts + * \brief enum of types of fonts used in game + *//* +enum Fonts +{ + FONT_COLOBOT = 0, + FONT_COLOBOT_BOLD = 1, + FONT_COLOBOT_ITALIC = 2, + FONT_COURIER = 3, + FONT_COURIER_BOLD = 4 +};*/ + +//! Global function to get config file instance +inline CFontConfigFile & GetFontConfigFile() +{ + return CFontConfigFile::GetInstance(); +} \ No newline at end of file diff --git a/src/graphics/engine/text.cpp b/src/graphics/engine/text.cpp index 53bd7f6a..aa7008d1 100644 --- a/src/graphics/engine/text.cpp +++ b/src/graphics/engine/text.cpp @@ -22,6 +22,7 @@ #include "app/app.h" +#include "common/font_file.h" #include "common/image.h" #include "common/logger.h" #include "common/stringutils.h" @@ -116,18 +117,23 @@ CText::~CText() bool CText::Create() { + CFontConfigFile fontconfig; + if (!GetFontConfigFile().Init()) + { + GetLogger()->Warn("Error on parsing fonts config file: failed to open file\n"); + } if (TTF_Init() != 0) { m_error = std::string("TTF_Init error: ") + std::string(TTF_GetError()); return false; } - m_fonts[FONT_COLOBOT] = MakeUnique("fonts/dvu_sans.ttf"); - m_fonts[FONT_COLOBOT_BOLD] = MakeUnique("fonts/dvu_sans_bold.ttf"); - m_fonts[FONT_COLOBOT_ITALIC] = MakeUnique("fonts/dvu_sans_italic.ttf"); + m_fonts[FONT_COLOBOT] = MakeUnique(GetFontConfigFile().GetCommonFont()); + m_fonts[FONT_COLOBOT_BOLD] = MakeUnique(GetFontConfigFile().GetCommonBoldFont()); + m_fonts[FONT_COLOBOT_ITALIC] = MakeUnique(GetFontConfigFile().GetCommonItalicFont()); - m_fonts[FONT_COURIER] = MakeUnique("fonts/dvu_sans_mono.ttf"); - m_fonts[FONT_COURIER_BOLD] = MakeUnique("fonts/dvu_sans_mono_bold.ttf"); + m_fonts[FONT_COURIER] = MakeUnique(GetFontConfigFile().GetStudioFont()); + m_fonts[FONT_COURIER_BOLD] = MakeUnique(GetFontConfigFile().GetStudioBoldFont()); for (auto it = m_fonts.begin(); it != m_fonts.end(); ++it) {