Skip to content

Commit

Permalink
demote fontconfig generation to a commandline param
Browse files Browse the repository at this point in the history
apparently "a few seconds" can be up to a minute on
some font-rich systems which is too long for comfort
  • Loading branch information
rt committed Feb 23, 2018
1 parent 517f7b4 commit 02191c9
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 34 deletions.
94 changes: 61 additions & 33 deletions rts/Rendering/Fonts/CFontTexture.cpp
Expand Up @@ -91,70 +91,72 @@ static spring::recursive_mutex m;




#ifndef HEADLESS
class FtLibraryHandler
{
class FtLibraryHandler {
public:
FtLibraryHandler() {
const FT_Error error = FT_Init_FreeType(&lib);

if (error)
throw std::runtime_error(std::string("FT_Init_FreeType failed:") + GetFTError(error));

#ifdef USE_FONTCONFIG
#ifdef USE_FONTCONFIG
if (!FcInit())
throw std::runtime_error("FontConfig failed");

// Windows users most likely don't have a fontconfig configuration file
// so manually add windows fonts dir and engine fonts dir to fontconfig
// so it can use them as fallback.
#ifdef WIN32
{
ScopedOnceTimer timer("FtLibraryHandler::FcConfigBuildFonts");
char osFontsDir[32 * 1024];

ExpandEnvironmentStrings("%WINDIR%\\fonts", osFontsDir, sizeof(osFontsDir)); // expands %HOME% etc.
LOG_L(L_INFO, "[%s] creating fontconfig for directory %s\n", __func__, osFontsDir);

FcConfigAppFontAddDir(nullptr, reinterpret_cast<FcChar8*>(osFontsDir));
FcConfigAppFontAddDir(nullptr, reinterpret_cast<const FcChar8*>("fonts"));
FcConfigBuildFonts(nullptr);
}
#endif

#endif
};
}

~FtLibraryHandler() {
FT_Done_FreeType(lib);
#ifdef USE_FONTCONFIG
#ifdef USE_FONTCONFIG
FcFini();
#endif
}

#ifdef USE_FONTCONFIG
bool CheckFontConfig() const { return (FcConfigUptoDate(nullptr)); }
bool BuildFontConfig(const char* osFontsDir) const {
// Windows users most likely don't have a fontconfig configuration file
// so manually add windows fonts dir and engine fonts dir to fontconfig
// so it can use them as fallback.
FcConfigAppFontAddDir(nullptr, reinterpret_cast<const FcChar8*>(osFontsDir));
FcConfigAppFontAddDir(nullptr, reinterpret_cast<const FcChar8*>("fonts"));
FcConfigBuildFonts(nullptr);
return true;
}
#else
bool CheckFontConfig() const { return false; }
bool BuildFontConfig(const char*) const { return false; }
#endif
};

static FT_Library& GetLibrary() {
// singleton
#ifndef WIN32
#ifndef WIN32
std::call_once(flag, []() {
singleton.reset(new FtLibraryHandler());
});
#else

#else

std::lock_guard<spring::recursive_mutex> lk(m);
if (flag) {
singleton.reset(new FtLibraryHandler());
flag = false;
}
#endif
#endif

return singleton->lib;
};

private:
FT_Library lib;
#ifndef WIN32

#ifndef WIN32
static std::once_flag flag;
#else
#else
static bool flag;
#endif
#endif

static std::unique_ptr<FtLibraryHandler> singleton;
};

Expand All @@ -170,6 +172,7 @@ std::unique_ptr<FtLibraryHandler> FtLibraryHandler::singleton = nullptr;




/*******************************************************************************/
/*******************************************************************************/
/*******************************************************************************/
Expand Down Expand Up @@ -309,8 +312,11 @@ static std::shared_ptr<FontFace> GetFontForCharacters(const std::vector<char32_t
std::unique_ptr<FcFontSet, decltype(del)> fs_(fs, del);
FcPatternDestroy(pattern);
FcCharSetDestroy(cset);
if (!fs) return nullptr;
if (res != FcResultMatch) return nullptr;

if (fs == nullptr)
return nullptr;
if (res != FcResultMatch)
return nullptr;

// iterate returned font list
for (int i = 0; i < fs->nfont; ++i) {
Expand Down Expand Up @@ -433,6 +439,28 @@ void CFontTexture::Update() {
}


bool CFontTexture::GenFontConfig() {
#ifndef HEADLESS
#ifdef WIN32
// called only from SpringApp::ParseCmdLine, regular singleton does not exist
FtLibraryHandler ftLibHandler;
char osFontsDir[32 * 1024];

ExpandEnvironmentStrings("%WINDIR%\\fonts", osFontsDir, sizeof(osFontsDir)); // expands %HOME% etc.

if (ftLibHandler.CheckFontConfig()) {
printf("[%s] fontconfig for directory %s up to date\n", __func__, osFontsDir);
return true;
}

printf("[%s] creating fontconfig for directory %s\n", __func__, osFontsDir);
return (ftLibHandler.BuildFontConfig(osFontsDir));
#endif
#endif
return true;
}


const GlyphInfo& CFontTexture::GetGlyph(char32_t ch)
{
#ifndef HEADLESS
Expand Down
1 change: 1 addition & 0 deletions rts/Rendering/Fonts/CFontTexture.h
Expand Up @@ -76,6 +76,7 @@ class CFontTexture
{
public:
static void Update();
static bool GenFontConfig();

public:
CFontTexture(const std::string& fontfile, int size, int outlinesize, float outlineweight);
Expand Down
1 change: 1 addition & 0 deletions rts/Rendering/Fonts/glFont.cpp
Expand Up @@ -3,6 +3,7 @@

#include "glFont.h"
#include "FontLogSection.h"

#include <stdarg.h>
#include <stdexcept>

Expand Down
8 changes: 7 additions & 1 deletion rts/System/SpringApp.cpp
Expand Up @@ -105,6 +105,7 @@ CONFIG(std::string, SplashScreenDir).defaultValue(".");


DEFINE_bool_EX (sync_version, "sync-version", false, "Display program sync version (for online gaming)");
DEFINE_bool_EX (gen_fontconfig, "gen-fontconfig", false, "Generate font-configuration database");
DEFINE_bool (fullscreen, false, "Run in fullscreen mode");
DEFINE_bool (window, false, "Run in windowed mode");
DEFINE_bool (hidden, false, "Start in background (minimised, no taskbar entry)");
Expand Down Expand Up @@ -475,13 +476,18 @@ void SpringApp::ParseCmdLine(int argc, char* argv[])
inputFile = argv[1];

#ifndef WIN32
if (!FLAGS_nocolor && (getenv("SPRING_NOCOLOR") == NULL)) {
if (!FLAGS_nocolor && (getenv("SPRING_NOCOLOR") == nullptr)) {
// don't colorize, if our output is piped to a diff tool or file
if (isatty(fileno(stdout)))
log_console_colorizedOutput(true);
}
#endif

if (FLAGS_gen_fontconfig) {
CFontTexture::GenFontConfig();
exit(EXIT_SUCCESS);
}

if (FLAGS_sync_version) {
// Note, the missing "Spring " is intentionally to make it compatible with `spring-dedicated --sync-version`
std::cout << SpringVersion::GetSync() << std::endl;
Expand Down

0 comments on commit 02191c9

Please sign in to comment.