Skip to content

Commit

Permalink
libgui|FontBank: Add fonts based on definitions, load them when needed
Browse files Browse the repository at this point in the history
Also added various convenience methods for accessing data in records
and variables.
  • Loading branch information
skyjake committed May 13, 2013
1 parent 0c781e6 commit 76d2f91
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 32 deletions.
7 changes: 4 additions & 3 deletions doomsday/libdeng2/include/de/data/bank.h
Expand Up @@ -326,12 +326,13 @@ class DENG2_PUBLIC Bank
virtual IData *loadFromSource(ISource &source) = 0;

/**
* Construct a new concrete instance of the data item.
* Called before deserialization.
* Construct a new concrete instance of the data item. Called before
* deserialization. Default implementation just returns NULL (seriliazation
* not supported).
*
* @return IData instance. Ownership given to caller.
*/
virtual IData *newData() = 0;
virtual IData *newData();

private:
DENG2_PRIVATE(d)
Expand Down
16 changes: 16 additions & 0 deletions doomsday/libdeng2/include/de/data/variable.h
Expand Up @@ -29,6 +29,7 @@
namespace de {

class Value;
class Record;

/**
* Stores a value and name identifier. Variables are typically stored in a Record.
Expand Down Expand Up @@ -172,6 +173,21 @@ class DENG2_PUBLIC Variable : public ISerializable
return *v;
}

/**
* Returns the Record that the variable references. If the variable does
* not have a RecordValue, an exception is thrown.
*
* @return Referenced Record.
*/
Record const &valueAsRecord() const;

operator Record const & () const;

// Automatic conversion to native primitive types.
operator String () const;
operator QString () const;
operator ddouble () const;

/**
* Returns the value of the variable.
*/
Expand Down
3 changes: 3 additions & 0 deletions doomsday/libdeng2/include/de/libdeng2.h
Expand Up @@ -306,6 +306,9 @@ class PrivateAutoPtr
InstType *get() const {
return ptr;
}
operator InstType *() const {
return ptr;
}
InstType *release() {
InstType *p = ptr;
ptr = 0;
Expand Down
2 changes: 2 additions & 0 deletions doomsday/libdeng2/include/de/scriptsys/scriptedinfo.h
Expand Up @@ -150,6 +150,8 @@ class DENG2_PUBLIC ScriptedInfo

Record const &names() const;

Variable const &operator [] (String const &path) const;

/**
* Finds all the blocks of a given type in the processed namespace.
* The block type has been stored as a member called __type__ in each
Expand Down
5 changes: 5 additions & 0 deletions doomsday/libdeng2/src/data/bank.cpp
Expand Up @@ -898,4 +898,9 @@ void Bank::purge()
*/
}

Bank::IData *Bank::newData()
{
return 0;
}

} // namespace de
26 changes: 26 additions & 0 deletions doomsday/libdeng2/src/data/variable.cpp
Expand Up @@ -26,6 +26,7 @@
#include "de/DictionaryValue"
#include "de/BlockValue"
#include "de/TimeValue"
#include "de/RecordValue"
#include "de/Reader"
#include "de/Writer"
#include "de/Log"
Expand Down Expand Up @@ -95,6 +96,31 @@ Value &Variable::value()
return *_value;
}

Record const &Variable::valueAsRecord() const
{
return value<RecordValue>().dereference();
}

Variable::operator Record const & () const
{
return valueAsRecord();
}

Variable::operator String () const
{
return value().asText();
}

Variable::operator QString () const
{
return value().asText();
}

Variable::operator ddouble () const
{
return value().asNumber();
}

Variable::Flags Variable::mode() const
{
return _mode;
Expand Down
5 changes: 5 additions & 0 deletions doomsday/libdeng2/src/scriptsys/scriptedinfo.cpp
Expand Up @@ -410,6 +410,11 @@ Record const &ScriptedInfo::names() const
return d->process.globals();
}

Variable const &ScriptedInfo::operator [] (String const &path) const
{
return names()[path];
}

ScriptedInfo::Paths ScriptedInfo::allBlocksOfType(String const &blockType) const
{
Paths found;
Expand Down
23 changes: 14 additions & 9 deletions doomsday/libgui/include/de/gui/fontbank.h
Expand Up @@ -21,6 +21,7 @@

#include <de/Bank>
#include <de/File>
#include "../Font"
#include "libgui.h"

namespace de {
Expand All @@ -34,26 +35,30 @@ class LIBGUI_PUBLIC FontBank : public Bank
FontBank();

/**
* Constructs a bank of fonts based on information in Info source.
* Creates a number of fonts based on information in an Info document.
*
* @param source Info source containing font definitions.
*/
FontBank(String const &source);
void readInfo(String const &source);

/**
* Constructs a bank of fonts based on information in an Info file.
* Creates a number of fonts based on information in an Info document.
*
* @param file File with Info source.
* @param source File with Info source containing font definitions.
*/
FontBank(File const &file);

void readInfo(String const &source);

void readInfo(File const &file);

/**
* Finds a specific font.
*
* @param path Identifier of the font.
*
* @return Font instance.
*/
Font const &font(Path const &path) const;

protected:
virtual IData *loadFromSource(ISource &source);
virtual IData *newData();

private:
DENG2_PRIVATE(d)
Expand Down
89 changes: 69 additions & 20 deletions doomsday/libgui/src/fontbank.cpp
Expand Up @@ -17,13 +17,74 @@
*/

#include "de/FontBank"
#include "de/Font"
#include <de/ScriptedInfo>
#include <de/Block>
#include <de/Time>

namespace de {

DENG2_PIMPL(FontBank)
{
struct FontSource : public ISource
{
Instance *d;
String id;

FontSource(Instance *inst, String const &fontId) : d(inst), id(fontId)
{}

Time modifiedAt() const
{
return d->modTime;
}

Font *load() const
{
Record const &def = d->info[id];

// Font family.
QFont font(def["family"]);

// Size.
String size = def["size"];
if(size.endsWith("px"))
{
font.setPixelSize(size.toInt(0, 10, String::AllowSuffix));
}
else
{
font.setPointSize(size.toInt(0, 10, String::AllowSuffix));
}

// Weight.
String const weight = def["weight"];
font.setWeight(weight == "light"? QFont::Light :
weight == "bold"? QFont::Bold :
QFont::Normal);

// Style.
String const style = def["style"];
font.setStyle(style == "italic"? QFont::StyleItalic : QFont::StyleNormal);

return new Font(font);
}
};

struct FontData : public IData
{
Font *font; // owned

FontData(Font *f = 0) : font(f) {}
~FontData() { delete font; }

duint sizeInMemory() const
{
return 0; // we don't count
}
};

Time modTime;
ScriptedInfo info;

Instance(Public *i) : Base(i)
Expand All @@ -34,30 +95,17 @@ FontBank::FontBank()
: Bank(DisableHotStorage), d(new Instance(this))
{}

FontBank::FontBank(String const &source)
: Bank(DisableHotStorage), d(new Instance(this))
{
readInfo(source);
}

FontBank::FontBank(File const &file)
: Bank(DisableHotStorage), d(new Instance(this))
{
readInfo(String::fromUtf8(Block(file)));
}

void FontBank::readInfo(String const &source)
{
LOG_AS("FontBank");
try
{
d->modTime = Time();
d->info.parse(source);

ScriptedInfo::Paths fonts = d->info.allBlocksOfType("font");
LOG_DEBUG("Found %i fonts:") << fonts.size();
foreach(String fn, fonts)
foreach(String fn, d->info.allBlocksOfType("font"))
{
LOG_DEBUG(" %s") << fn;
add(fn, new Instance::FontSource(d, fn));
}
}
catch(Error const &er)
Expand All @@ -69,16 +117,17 @@ void FontBank::readInfo(String const &source)
void FontBank::readInfo(File const &file)
{
readInfo(String::fromUtf8(Block(file)));
d->modTime = file.status().modifiedAt;
}

Bank::IData *FontBank::loadFromSource(ISource &)
Font const &FontBank::font(Path const &path) const
{
return 0;
return *static_cast<Instance::FontData &>(data(path)).font;
}

Bank::IData *FontBank::newData()
Bank::IData *FontBank::loadFromSource(ISource &source)
{
return 0;
return new Instance::FontData(static_cast<Instance::FontSource &>(source).load());
}

} // namespace de

0 comments on commit 76d2f91

Please sign in to comment.