Files @ r27709:13454adf22b6
Branch filter:

Location: cpp/openttd-patchpack/source/src/fontcache.cpp - annotation

Patric Stout
Change: [CI] rework preview flow and use Cloudflare Pages to publish (#11116)
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r6179:c0508e7aefec
r5584:545d748cc681
r5584:545d748cc681
r20421:689b79ec796e
r6937:bea726150bf3
r20543:89cc7ba70ae8
r26400:bc4531010a6d
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r8264:d493cb51fe8a
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r20179:3d62e5438cc2
r20448:799c881fb514
r20448:799c881fb514
r11485:42dece12eaa2
r26399:db15b808f86d
r24759:60b551df3641
r18422:ab780a7de508
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20448:799c881fb514
r20448:799c881fb514
r20448:799c881fb514
r20423:f73b67868582
r23607:36c15679007d
r20423:f73b67868582
r20543:89cc7ba70ae8
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r20423:f73b67868582
r22667:39940ab1c009
r20423:f73b67868582
r20543:89cc7ba70ae8
r20423:f73b67868582
r20423:f73b67868582
r24759:60b551df3641
r24759:60b551df3641
r24759:60b551df3641
r24759:60b551df3641
r24759:60b551df3641
r27595:00fc0f274855
r27595:00fc0f274855
r27595:00fc0f274855
r27595:00fc0f274855
r27595:00fc0f274855
r27595:00fc0f274855
r27595:00fc0f274855
r27595:00fc0f274855
r27595:00fc0f274855
r27595:00fc0f274855
r27595:00fc0f274855
r27595:00fc0f274855
r27595:00fc0f274855
r27595:00fc0f274855
r27595:00fc0f274855
r20427:30afb67f2d28
r20427:30afb67f2d28
r20427:30afb67f2d28
r20427:30afb67f2d28
r20427:30afb67f2d28
r20427:30afb67f2d28
r20427:30afb67f2d28
r20427:30afb67f2d28
r20427:30afb67f2d28
r20427:30afb67f2d28
r20427:30afb67f2d28
r20427:30afb67f2d28
r27382:d2aa3dbe2d4a
r20423:f73b67868582
r27382:d2aa3dbe2d4a
r27382:d2aa3dbe2d4a
r27382:d2aa3dbe2d4a
r27382:d2aa3dbe2d4a
r27382:d2aa3dbe2d4a
r27382:d2aa3dbe2d4a
r23735:275a836d8f5d
r23735:275a836d8f5d
r26400:bc4531010a6d
r23735:275a836d8f5d
r23735:275a836d8f5d
r24931:be251d09ce0e
r23735:275a836d8f5d
r26673:f6a0bb2e0bfa
r23735:275a836d8f5d
r23735:275a836d8f5d
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26675:fc2319aeaced
r26832:a6bd9039c80b
r26832:a6bd9039c80b
r26832:a6bd9039c80b
r26832:a6bd9039c80b
r26832:a6bd9039c80b
r26832:a6bd9039c80b
r26832:a6bd9039c80b
r26832:a6bd9039c80b
r26832:a6bd9039c80b
r20430:6112801da287
r26399:db15b808f86d
r20430:6112801da287
r20430:6112801da287
r26399:db15b808f86d
r20430:6112801da287
r27382:d2aa3dbe2d4a
r27382:d2aa3dbe2d4a
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r23736:e007e33ed409
r23736:e007e33ed409
r24787:198b505805b2
r24787:198b505805b2
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r26399:db15b808f86d
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r20430:6112801da287
r26400:bc4531010a6d
r20430:6112801da287
r20430:6112801da287
r24761:9595588c0322
r24931:be251d09ce0e
r24931:be251d09ce0e
r24931:be251d09ce0e
r24931:be251d09ce0e
r24931:be251d09ce0e
r24931:be251d09ce0e
r24931:be251d09ce0e
r24931:be251d09ce0e
r24931:be251d09ce0e
r24931:be251d09ce0e
r24931:be251d09ce0e
r24931:be251d09ce0e
r24931:be251d09ce0e
r24787:198b505805b2
r24761:9595588c0322
r27541:98de37403e2e
r24787:198b505805b2
/*
 * This file is part of OpenTTD.
 * OpenTTD 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, version 2.
 * OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 */

/** @file fontcache.cpp Cache for characters from fonts. */

#include "stdafx.h"
#include "fontcache.h"
#include "fontdetection.h"
#include "blitter/factory.hpp"
#include "gfx_layout.h"
#include "fontcache/spritefontcache.h"
#include "openttd.h"
#include "settings_func.h"
#include "strings_func.h"
#include "viewport_func.h"
#include "window_func.h"

#include "safeguards.h"

/** Default heights for the different sizes of fonts. */
static const int _default_font_height[FS_END]   = {10, 6, 18, 10};
static const int _default_font_ascender[FS_END] = { 8, 5, 15,  8};

FontCacheSettings _fcsettings;

/**
 * Create a new font cache.
 * @param fs The size of the font.
 */
FontCache::FontCache(FontSize fs) : parent(FontCache::Get(fs)), fs(fs), height(_default_font_height[fs]),
		ascender(_default_font_ascender[fs]), descender(_default_font_ascender[fs] - _default_font_height[fs]),
		units_per_em(1)
{
	assert(this->parent == nullptr || this->fs == this->parent->fs);
	FontCache::caches[this->fs] = this;
	Layouter::ResetFontCache(this->fs);
}

/** Clean everything up. */
FontCache::~FontCache()
{
	assert(this->fs == this->parent->fs);
	FontCache::caches[this->fs] = this->parent;
	Layouter::ResetFontCache(this->fs);
}

int FontCache::GetDefaultFontHeight(FontSize fs)
{
	return _default_font_height[fs];
}

/**
 * Get the font name of a given font size.
 * @param fs The font size to look up.
 * @return The font name.
 */
std::string FontCache::GetName(FontSize fs)
{
	FontCache *fc = FontCache::Get(fs);
	if (fc != nullptr) {
		return fc->GetFontName();
	} else {
		return "[NULL]";
	}
}


/**
 * Get height of a character for a given font size.
 * @param size Font size to get height of
 * @return     Height of characters in the given font (pixels)
 */
int GetCharacterHeight(FontSize size)
{
	return FontCache::Get(size)->GetHeight();
}


/* static */ FontCache *FontCache::caches[FS_END];

/* static */ void FontCache::InitializeFontCaches()
{
	for (FontSize fs = FS_BEGIN; fs != FS_END; fs++) {
		if (FontCache::caches[fs] == nullptr) new SpriteFontCache(fs); /* FontCache inserts itself into to the cache. */
	}
}

/* Check if a glyph should be rendered with anti-aliasing. */
bool GetFontAAState(FontSize size, bool check_blitter)
{
	/* AA is only supported for 32 bpp */
	if (check_blitter && BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 32) return false;

	return GetFontCacheSubSetting(size)->aa;
}

void SetFont(FontSize fontsize, const std::string& font, uint size, bool aa)
{
	FontCacheSubSetting *setting = GetFontCacheSubSetting(fontsize);
	bool changed = false;

	if (setting->font != font) {
		setting->font = font;
		changed = true;
	}

	if (setting->size != size) {
		setting->size = size;
		changed = true;
	}

	if (setting->aa != aa) {
		setting->aa = aa;
		changed = true;
	}

	if (!changed) return;

	if (fontsize != FS_MONO) {
		/* Try to reload only the modified font. */
		FontCacheSettings backup = _fcsettings;
		for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
			if (fs == fontsize) continue;
			FontCache *fc = FontCache::Get(fs);
			GetFontCacheSubSetting(fs)->font = fc->HasParent() ? fc->GetFontName() : "";
		}
		CheckForMissingGlyphs();
		_fcsettings = backup;
	} else {
		InitFontCache(true);
	}

	LoadStringWidthTable();
	UpdateAllVirtCoords();
	ReInitAllWindows(true);

	if (_save_config) SaveToConfig();
}

#ifdef WITH_FREETYPE
extern void LoadFreeTypeFont(FontSize fs);
extern void UninitFreeType();
#elif defined(_WIN32)
extern void LoadWin32Font(FontSize fs);
#elif defined(WITH_COCOA)
extern void LoadCoreTextFont(FontSize fs);
#endif

/**
 * (Re)initialize the font cache related things, i.e. load the non-sprite fonts.
 * @param monospace Whether to initialise the monospace or regular fonts.
 */
void InitFontCache(bool monospace)
{
	FontCache::InitializeFontCaches();

	for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
		if (monospace != (fs == FS_MONO)) continue;

		FontCache *fc = FontCache::Get(fs);
		if (fc->HasParent()) delete fc;

#ifdef WITH_FREETYPE
		LoadFreeTypeFont(fs);
#elif defined(_WIN32)
		LoadWin32Font(fs);
#elif defined(WITH_COCOA)
		LoadCoreTextFont(fs);
#endif
	}
}

/**
 * Free everything allocated w.r.t. fonts.
 */
void UninitFontCache()
{
	for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
		FontCache *fc = FontCache::Get(fs);
		if (fc->HasParent()) delete fc;
	}

#ifdef WITH_FREETYPE
	UninitFreeType();
#endif /* WITH_FREETYPE */
}

/**
 * Should any of the active fonts be anti-aliased?
 * @return True if any of the loaded fonts want anti-aliased drawing.
 */
bool HasAntialiasedFonts()
{
	for (FontSize fs = FS_BEGIN; fs < FS_END; fs++) {
		if (!FontCache::Get(fs)->IsBuiltInFont() && GetFontAAState(fs, false)) return true;
	}

	return false;
}

#if !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA)

bool SetFallbackFont(FontCacheSettings *settings, const std::string &language_isocode, int winlangid, MissingGlyphSearcher *callback) { return false; }
#endif /* !defined(_WIN32) && !defined(__APPLE__) && !defined(WITH_FONTCONFIG) && !defined(WITH_COCOA) */