Added fonts configurability by a separate file

1008-fix
tomangelo2 2017-10-09 00:36:31 +02:00
parent 0de347d430
commit 250c934b9e
5 changed files with 446 additions and 5 deletions

View File

@ -106,6 +106,8 @@ set(BASE_SOURCES
common/error.h common/error.h
common/event.cpp common/event.cpp
common/event.h common/event.h
common/font_file.h
common/font_file.cpp
common/global.h common/global.h
common/image.cpp common/image.cpp
common/image.h common/image.h

View File

@ -44,6 +44,7 @@ class CSoundInterface;
class CInput; class CInput;
class CPathManager; class CPathManager;
class CConfigFile; class CConfigFile;
class CFontConfigFile;
class CSystemUtils; class CSystemUtils;
struct SystemTimeStamp; struct SystemTimeStamp;

285
src/common/font_file.cpp Normal file
View File

@ -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 <memory>
#include <utility>
#include <cstring>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/regex.hpp>
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<std::istream> stream;
bool good;
if (m_useCurrentDirectory)
{
auto inputStream = MakeUnique<std::ifstream>("./fonts.ini");
good = inputStream->good();
stream = std::move(inputStream);
}
else
{
auto inputStream = MakeUnique<CInputStream>("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<std::ostream> stream;
bool good;
if (m_useCurrentDirectory)
{
auto outputStream = MakeUnique<std::ofstream>("./fonts.ini");
good = outputStream->good();
stream = std::move(outputStream);
}
else
{
auto outputStream = MakeUnique<COutputStream>("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<std::string>("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<std::string>("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<std::string>("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<std::string>("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<std::string>("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;
}

147
src/common/font_file.h Normal file
View File

@ -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 <boost/property_tree/ptree.hpp>
#include <string>
/**
* \class CFontConfigFile
*
* \brief Class for loading config file
*
*/
class CFontConfigFile : public CSingleton<CFontConfigFile>
{
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();
}

View File

@ -22,6 +22,7 @@
#include "app/app.h" #include "app/app.h"
#include "common/font_file.h"
#include "common/image.h" #include "common/image.h"
#include "common/logger.h" #include "common/logger.h"
#include "common/stringutils.h" #include "common/stringutils.h"
@ -116,18 +117,23 @@ CText::~CText()
bool CText::Create() 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) if (TTF_Init() != 0)
{ {
m_error = std::string("TTF_Init error: ") + std::string(TTF_GetError()); m_error = std::string("TTF_Init error: ") + std::string(TTF_GetError());
return false; return false;
} }
m_fonts[FONT_COLOBOT] = MakeUnique<MultisizeFont>("fonts/dvu_sans.ttf"); m_fonts[FONT_COLOBOT] = MakeUnique<MultisizeFont>(GetFontConfigFile().GetCommonFont());
m_fonts[FONT_COLOBOT_BOLD] = MakeUnique<MultisizeFont>("fonts/dvu_sans_bold.ttf"); m_fonts[FONT_COLOBOT_BOLD] = MakeUnique<MultisizeFont>(GetFontConfigFile().GetCommonBoldFont());
m_fonts[FONT_COLOBOT_ITALIC] = MakeUnique<MultisizeFont>("fonts/dvu_sans_italic.ttf"); m_fonts[FONT_COLOBOT_ITALIC] = MakeUnique<MultisizeFont>(GetFontConfigFile().GetCommonItalicFont());
m_fonts[FONT_COURIER] = MakeUnique<MultisizeFont>("fonts/dvu_sans_mono.ttf"); m_fonts[FONT_COURIER] = MakeUnique<MultisizeFont>(GetFontConfigFile().GetStudioFont());
m_fonts[FONT_COURIER_BOLD] = MakeUnique<MultisizeFont>("fonts/dvu_sans_mono_bold.ttf"); m_fonts[FONT_COURIER_BOLD] = MakeUnique<MultisizeFont>(GetFontConfigFile().GetStudioBoldFont());
for (auto it = m_fonts.begin(); it != m_fonts.end(); ++it) for (auto it = m_fonts.begin(); it != m_fonts.end(); ++it)
{ {