Skip to content

Commit

Permalink
Refactor|ClientApp: Moved font related script bindings to BaseGuiApp
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Mar 3, 2014
1 parent 41f8a96 commit cdce853
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 63 deletions.
64 changes: 2 additions & 62 deletions doomsday/client/src/clientapp.cpp
Expand Up @@ -91,61 +91,6 @@ static Value *Function_App_GamePlugin(Context &, Function::ArgumentValues const
return new TextValue(name);
}

static Value *Function_App_LoadFont(Context &, Function::ArgumentValues const &args)
{
LOG_AS("ClientApp");
try
{
// Try to load the specific font.
Block data(App::fileSystem().root().locate<File const>(args.at(0)->asText()));
int id;
id = QFontDatabase::addApplicationFontFromData(data);
if(id < 0)
{
LOG_RES_WARNING("Failed to load font:");
}
else
{
LOG_RES_VERBOSE("Loaded font: %s") << args.at(0)->asText();
//qDebug() << args.at(0)->asText();
//qDebug() << "Families:" << QFontDatabase::applicationFontFamilies(id);
}
}
catch(Error const &er)
{
LOG_RES_WARNING("Failed to load font:\n") << er.asText();
}
return 0;
}

static Value *Function_App_AddFontMapping(Context &, Function::ArgumentValues const &args)
{
// arg 0: family name
// arg 1: dictionary with [Text style, Number weight] => Text fontname

// styles: regular, italic
// weight: 0-99 (25=light, 50=normal, 75=bold)

NativeFont::StyleMapping mapping;

DictionaryValue const &dict = args.at(1)->as<DictionaryValue>();
DENG2_FOR_EACH_CONST(DictionaryValue::Elements, i, dict.elements())
{
NativeFont::Spec spec;
ArrayValue const &key = i->first.value->as<ArrayValue>();
if(key.at(0).asText() == "italic")
{
spec.style = NativeFont::Italic;
}
spec.weight = roundi(key.at(1).asNumber());
mapping.insert(spec, i->second->asText());
}

NativeFont::defineMapping(args.at(0)->asText(), mapping);

return 0;
}

static Value *Function_App_Quit(Context &, Function::ArgumentValues const &)
{
Sys_Quit();
Expand Down Expand Up @@ -319,9 +264,6 @@ ClientApp::ClientApp(int &argc, char **argv)
{
novideo = false;

// Override the system locale (affects number/time formatting).
QLocale::setDefault(QLocale("en_US.UTF-8"));

// Use the host system's proxy configuration.
QNetworkProxyFactory::setUseSystemConfiguration(true);

Expand All @@ -335,10 +277,8 @@ ClientApp::ClientApp(int &argc, char **argv)
setGame(d->games.nullGame());

d->binder.init(scriptSystem().nativeModule("App"))
<< DENG2_FUNC_NOARG (App_GamePlugin, "gamePlugin")
<< DENG2_FUNC (App_AddFontMapping, "addFontMapping", "family" << "mappings")
<< DENG2_FUNC (App_LoadFont, "loadFont", "fileName")
<< DENG2_FUNC_NOARG (App_Quit, "quit");
<< DENG2_FUNC_NOARG (App_GamePlugin, "gamePlugin")
<< DENG2_FUNC_NOARG (App_Quit, "quit");
}

void ClientApp::initialize()
Expand Down
71 changes: 70 additions & 1 deletion doomsday/libappfw/src/baseguiapp.cpp
Expand Up @@ -19,18 +19,87 @@
#include "de/BaseGuiApp"
#include "de/VRConfig"

#include <de/Function>
#include <de/ArrayValue>
#include <de/DictionaryValue>
#include <de/NativeFont>
#include <QFontDatabase>

namespace de {

static Value *Function_App_LoadFont(Context &, Function::ArgumentValues const &args)
{
LOG_AS("ClientApp");
try
{
// Try to load the specific font.
Block data(App::fileSystem().root().locate<File const>(args.at(0)->asText()));
int id;
id = QFontDatabase::addApplicationFontFromData(data);
if(id < 0)
{
LOG_RES_WARNING("Failed to load font:");
}
else
{
LOG_RES_VERBOSE("Loaded font: %s") << args.at(0)->asText();
//qDebug() << args.at(0)->asText();
//qDebug() << "Families:" << QFontDatabase::applicationFontFamilies(id);
}
}
catch(Error const &er)
{
LOG_RES_WARNING("Failed to load font:\n") << er.asText();
}
return 0;
}

static Value *Function_App_AddFontMapping(Context &, Function::ArgumentValues const &args)
{
// arg 0: family name
// arg 1: dictionary with [Text style, Number weight] => Text fontname

// styles: regular, italic
// weight: 0-99 (25=light, 50=normal, 75=bold)

NativeFont::StyleMapping mapping;

DictionaryValue const &dict = args.at(1)->as<DictionaryValue>();
DENG2_FOR_EACH_CONST(DictionaryValue::Elements, i, dict.elements())
{
NativeFont::Spec spec;
ArrayValue const &key = i->first.value->as<ArrayValue>();
if(key.at(0).asText() == "italic")
{
spec.style = NativeFont::Italic;
}
spec.weight = roundi(key.at(1).asNumber());
mapping.insert(spec, i->second->asText());
}

NativeFont::defineMapping(args.at(0)->asText(), mapping);

return 0;
}

DENG2_PIMPL_NOREF(BaseGuiApp)
{
Binder binder;
QScopedPointer<PersistentState> uiState;
GLShaderBank shaders;
VRConfig vr;
};

BaseGuiApp::BaseGuiApp(int &argc, char **argv)
: GuiApp(argc, argv), d(new Instance)
{}
{
// Override the system locale (affects number/time formatting).
QLocale::setDefault(QLocale("en_US.UTF-8"));

d->binder.init(scriptSystem().nativeModule("App"))
<< DENG2_FUNC (App_AddFontMapping, "addFontMapping", "family" << "mappings")
<< DENG2_FUNC (App_LoadFont, "loadFont", "fileName");
}

void BaseGuiApp::initSubsystems(SubsystemInitFlags flags)
{
Expand Down

0 comments on commit cdce853

Please sign in to comment.