aboutsummaryrefslogtreecommitdiff
Commit message (Expand)AuthorAge
* Fix detector lookup in ATC railsorwell962017-12-18
* Do not spam the server chat with messages from /at_sync_ndb and trains going ...orwell962017-12-18
* Rewrite rail connection system...orwell962017-12-18
* Use preferred rail orientation algorithm also for double connectionsorwell962017-12-18
* Move train_load() function into advtrains_train_trackorwell962017-12-18
* Try to fix occasional crash when placing wagonsorwell962017-12-17
* Improve textures and models of industrial train and add new more powerful ind...mbb2017-12-12
* Make sure an old_velocity is always passedorwell962017-12-06
* Implement sound api and some soundsorwell962017-12-06
* Remove zip file and makefileorwell962017-11-29
* Change name of update_animation functionorwell962017-11-29
* remove superfluous parameterorwell962017-11-27
* Do not modify rails that should not be modifiedorwell962017-11-24
* Merge branch 'master' of https://github.com/orwell96/advtrainsorwell962017-11-24
|\
| * Preserve the player's looking directionorwell962017-11-23
* | Correct yaw to preferred rail direction calculationorwell962017-11-24
|/
* Add modifiable wagon extentsorwell962017-11-23
* Fix multiple track types not working simultaneouslyorwell962017-11-22
* do not register wagons in the advtrains namespace automaticallyorwell962017-11-22
* Make trackplacer align rails by any tracks, not just by tracks with the same ...orwell962017-11-22
* Do not punch signsorwell962017-11-14
* Fix occasional crash in discouple on_punchorwell962017-11-14
* Fix entity damageorwell962017-11-02
* Change name of the node database group in order to clone node database code i...orwell962017-10-31
* Punch non-player objects when they get overridden by a train.orwell962017-10-25
* Fix subway train placerorwell962017-10-25
* Some workaround fixes for Linuxworks serverorwell962017-10-25
* Add missing documentation for set_lineorwell962017-10-25
* Update readme.txtorwell962017-10-25
* Implement multi-occupation in detector.on_node table to finally fix collisionsorwell962017-10-25
* Replace many math.floor(x+0.5) calls (or math.floor calls that should be thos...orwell962017-10-25
* Fix coupling and collisions in certain casesorwell962017-10-25
* Moved default train track to separate mod, for integration with advcarts.Gabriel Pérez-Cerezo2017-10-25
* Fix last commitorwell962017-10-23
* Fix continous object_property modificationorwell962017-10-23
* Set wagon line numberGabriel Pérez-Cerezo2017-10-23
* Prefer saved nodedb node before node loaded from maporwell962017-10-11
* Merge remote-tracking branch 'lemon-melon-repo/master'orwell962017-10-11
|\
| * Add speed as number in km/h to train hudlemon-melon2017-07-15
| * Add m/s to km/h helper functionlemon-melon2017-07-15
* | Do not crash when train_pos is nil while checking wagon entity loadingorwell962017-10-11
* | Fix error cascade due to inexistent drb_dump when debugging is disabledorwell962017-10-11
* | Remove zip release files, move mod to root, exclude assets from Makefile (#92)rubenwardy2017-09-20
* | Merge PR from mbb - Improve models and texturesMBB2017-08-24
* | Repack for gpcf's PRorwell962017-08-15
* | Added command to reroute trainsGabriel Pérez-Cerezo2017-08-15
* | Fixed #86Gabriel Pérez-Cerezo2017-08-15
|/
* Add command to throw all players out of trains, fixes #78orwell962017-06-12
* Update Zip - Release 1.8.3orwell962017-06-08
* Fix broken door animations caused by continuous set_object_properties() messa...orwell962017-06-08
f='#n430'>430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
/*
Minetest
Copyright (C) 2010-2014 sapier <sapier at gmx dot net>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "fontengine.h"
#include "client/renderingengine.h"
#include "config.h"
#include "porting.h"
#include "filesys.h"

#if USE_FREETYPE
#include "gettext.h"
#include "irrlicht_changes/CGUITTFont.h"
#endif

/** maximum size distance for getting a "similar" font size */
#define MAX_FONT_SIZE_OFFSET 10

/** reference to access font engine, has to be initialized by main */
FontEngine* g_fontengine = NULL;

/** callback to be used on change of font size setting */
static void font_setting_changed(const std::string &name, void *userdata)
{
	g_fontengine->readSettings();
}

/******************************************************************************/
FontEngine::FontEngine(Settings* main_settings, gui::IGUIEnvironment* env) :
	m_settings(main_settings),
	m_env(env)
{

	for (u32 &i : m_default_size) {
		i = (FontMode) FONT_SIZE_UNSPECIFIED;
	}

	assert(m_settings != NULL); // pre-condition
	assert(m_env != NULL); // pre-condition
	assert(m_env->getSkin() != NULL); // pre-condition

	m_currentMode = FM_Simple;

#if USE_FREETYPE
	if (g_settings->getBool("freetype")) {
		m_default_size[FM_Standard] = m_settings->getU16("font_size");
		m_default_size[FM_Fallback] = m_settings->getU16("fallback_font_size");
		m_default_size[FM_Mono]     = m_settings->getU16("mono_font_size");

		if (is_yes(gettext("needs_fallback_font"))) {
			m_currentMode = FM_Fallback;
		}
		else {
			m_currentMode = FM_Standard;
		}
	}

	// having freetype but not using it is quite a strange case so we need to do
	// special handling for it
	if (m_currentMode == FM_Simple) {
		std::stringstream fontsize;
		fontsize << DEFAULT_FONT_SIZE;
		m_settings->setDefault("font_size", fontsize.str());
		m_settings->setDefault("mono_font_size", fontsize.str());
	}
#endif

	m_default_size[FM_Simple]       = m_settings->getU16("font_size");
	m_default_size[FM_SimpleMono]   = m_settings->getU16("mono_font_size");

	updateSkin();

	if (m_currentMode == FM_Standard) {
		m_settings->registerChangedCallback("font_size", font_setting_changed, NULL);
		m_settings->registerChangedCallback("font_path", font_setting_changed, NULL);
		m_settings->registerChangedCallback("font_shadow", font_setting_changed, NULL);
		m_settings->registerChangedCallback("font_shadow_alpha", font_setting_changed, NULL);
	}
	else if (m_currentMode == FM_Fallback) {
		m_settings->registerChangedCallback("fallback_font_size", font_setting_changed, NULL);
		m_settings->registerChangedCallback("fallback_font_path", font_setting_changed, NULL);
		m_settings->registerChangedCallback("fallback_font_shadow", font_setting_changed, NULL);
		m_settings->registerChangedCallback("fallback_font_shadow_alpha", font_setting_changed, NULL);
	}

	m_settings->registerChangedCallback("mono_font_path", font_setting_changed, NULL);
	m_settings->registerChangedCallback("mono_font_size", font_setting_changed, NULL);
	m_settings->registerChangedCallback("screen_dpi", font_setting_changed, NULL);
	m_settings->registerChangedCallback("gui_scaling", font_setting_changed, NULL);
}

/******************************************************************************/
FontEngine::~FontEngine()
{
	cleanCache();
}

/******************************************************************************/
void FontEngine::cleanCache()
{
	for (auto &font_cache_it : m_font_cache) {

		for (auto &font_it : font_cache_it) {
			font_it.second->drop();
			font_it.second = NULL;
		}
		font_cache_it.clear();
	}
}

/******************************************************************************/
irr::gui::IGUIFont* FontEngine::getFont(unsigned int font_size, FontMode mode)
{
	if (mode == FM_Unspecified) {
		mode = m_currentMode;
	}
	else if ((mode == FM_Mono) && (m_currentMode == FM_Simple)) {
		mode = FM_SimpleMono;
	}

	if (font_size == FONT_SIZE_UNSPECIFIED) {
		font_size = m_default_size[mode];
	}

	if ((font_size == m_lastSize) && (mode == m_lastMode)) {
		return m_lastFont;
	}

	if (m_font_cache[mode].find(font_size) == m_font_cache[mode].end()) {
		initFont(font_size, mode);
	}

	if (m_font_cache[mode].find(font_size) == m_font_cache[mode].end()) {
		return NULL;
	}

	m_lastSize = font_size;
	m_lastMode = mode;
	m_lastFont = m_font_cache[mode][font_size];

	return m_font_cache[mode][font_size];
}

/******************************************************************************/
unsigned int FontEngine::getTextHeight(unsigned int font_size, FontMode mode)
{
	irr::gui::IGUIFont* font = getFont(font_size, mode);

	// use current skin font as fallback
	if (font == NULL) {
		font = m_env->getSkin()->getFont();
	}
	FATAL_ERROR_IF(font == NULL, "Could not get skin font");

	return font->getDimension(L"Some unimportant example String").Height;
}

/******************************************************************************/
unsigned int FontEngine::getTextWidth(const std::wstring& text,
		unsigned int font_size, FontMode mode)
{
	irr::gui::IGUIFont* font = getFont(font_size, mode);

	// use current skin font as fallback
	if (font == NULL) {
		font = m_env->getSkin()->getFont();
	}
	FATAL_ERROR_IF(font == NULL, "Could not get font");

	return font->getDimension(text.c_str()).Width;
}


/** get line height for a specific font (including empty room between lines) */
unsigned int FontEngine::getLineHeight(unsigned int font_size, FontMode mode)
{
	irr::gui::IGUIFont* font = getFont(font_size, mode);

	// use current skin font as fallback
	if (font == NULL) {
		font = m_env->getSkin()->getFont();
	}
	FATAL_ERROR_IF(font == NULL, "Could not get font");

	return font->getDimension(L"Some unimportant example String").Height
			+ font->getKerningHeight();
}

/******************************************************************************/
unsigned int FontEngine::getDefaultFontSize()
{
	return m_default_size[m_currentMode];
}

/******************************************************************************/
void FontEngine::readSettings()
{
#if USE_FREETYPE
	if (g_settings->getBool("freetype")) {
		m_default_size[FM_Standard] = m_settings->getU16("font_size");
		m_default_size[FM_Fallback] = m_settings->getU16("fallback_font_size");
		m_default_size[FM_Mono]     = m_settings->getU16("mono_font_size");

		if (is_yes(gettext("needs_fallback_font"))) {
			m_currentMode = FM_Fallback;
		}
		else {
			m_currentMode = FM_Standard;
		}
	}
#endif
	m_default_size[FM_Simple]       = m_settings->getU16("font_size");
	m_default_size[FM_SimpleMono]   = m_settings->getU16("mono_font_size");

	cleanCache();
	updateFontCache();
	updateSkin();
}