Skip to content

Commit

Permalink
Refactor|Fonts|Client: Began cleanup of (composite) bitmap fonts
Browse files Browse the repository at this point in the history
Obviously the ultimate goal here is a representation which can be
integrated with/on-top-of de::Font in libgui. I won't be taking it
quite that far at this stage, however.
  • Loading branch information
danij-deng committed Nov 10, 2013
1 parent 1ac294c commit c3ff0e8
Show file tree
Hide file tree
Showing 8 changed files with 357 additions and 538 deletions.
81 changes: 46 additions & 35 deletions doomsday/client/include/resource/bitmapfont.h
Expand Up @@ -33,10 +33,9 @@ typedef struct {
Point2Raw coords[4];
} bitmapfont_char_t;

typedef struct bitmapfont_s {
/// Base font.
font_t _base;

class bitmapfont_t : public font_t
{
public:
/// Absolute file path to the archived version of this font (if any).
ddstring_t _filePath;

Expand All @@ -48,12 +47,17 @@ typedef struct bitmapfont_s {

/// Character map.
bitmapfont_char_t _chars[MAX_CHARS];
} bitmapfont_t;

font_t *BitmapFont_New(fontid_t bindId);
void BitmapFont_Delete(font_t *font);
bitmapfont_t(fontid_t bindId);
~bitmapfont_t();

void glInit();
void glDeinit();

void BitmapFont_Prepare(font_t *font);
RectRaw const *charGeometry(unsigned char ch);
int charWidth(unsigned char ch);
int charHeight(unsigned char ch);
};

void BitmapFont_SetFilePath(font_t *font, char const *filePath);

Expand All @@ -63,50 +67,57 @@ Size2Raw const *BitmapFont_TextureSize(font_t const *font);
int BitmapFont_TextureHeight(font_t const *font);
int BitmapFont_TextureWidth(font_t const *font);

void BitmapFont_DeleteGLTexture(font_t *font);

RectRaw const *BitmapFont_CharGeometry(font_t *font, unsigned char chr);
int BitmapFont_CharWidth(font_t *font, unsigned char ch);
int BitmapFont_CharHeight(font_t *font, unsigned char ch);

void BitmapFont_CharCoords(font_t *font, unsigned char ch, Point2Raw coords[4]);

// Data for a character.
typedef struct {
RectRaw geometry;
patchid_t patch;
de::Texture::Variant *tex;
uint8_t border;
} bitmapcompositefont_char_t;

typedef struct bitmapcompositefont_s {
/// Base font.
font_t _base;
class bitmapcompositefont_t : public font_t
{
public:
// Data for a character.
struct bitmapcompositefont_char_t
{
RectRaw geometry;
patchid_t patch;
de::Texture::Variant *tex;
uint8_t border;
};

/// Definition used to construct this else @c NULL if not applicable.
struct ded_compositefont_s *_def;

/// Character map.
bitmapcompositefont_char_t _chars[MAX_CHARS];
} bitmapcompositefont_t;

font_t *BitmapCompositeFont_New(fontid_t bindId);
void BitmapCompositeFont_Delete(font_t *font);
public:
bitmapcompositefont_t(fontid_t bindId);
~bitmapcompositefont_t();

static bitmapcompositefont_t *fromDef(fontid_t bindId, ded_compositefont_t *def);

struct ded_compositefont_s *definition() const;
void setDefinition(struct ded_compositefont_s *def);

void BitmapCompositeFont_Prepare(font_t *font);
/**
* Update the font according to the supplied definition. To be called after
* an engine update/reset.
*
* @param def Definition to update using.
*
* @todo Should observe engine reset.
*/
void rebuildFromDef(ded_compositefont_t *def);

RectRaw const *BitmapCompositeFont_CharGeometry(font_t *font, unsigned char chr);
int BitmapCompositeFont_CharWidth(font_t *font, unsigned char ch);
int BitmapCompositeFont_CharHeight(font_t *font, unsigned char ch);
void glInit();
void glDeinit();

struct ded_compositefont_s *BitmapCompositeFont_Definition(font_t const *font);
void BitmapCompositeFont_SetDefinition(font_t *font, struct ded_compositefont_s *def);
RectRaw const *charGeometry(unsigned char ch);
int charWidth(unsigned char ch);
int charHeight(unsigned char ch);
};

patchid_t BitmapCompositeFont_CharPatch(font_t *font, unsigned char ch);
void BitmapCompositeFont_CharSetPatch(font_t *font, unsigned char ch, char const *encodedPatchName);

de::Texture::Variant *BitmapCompositeFont_CharTexture(font_t *font, unsigned char ch);
void BitmapCompositeFont_ReleaseTextures(font_t *font);

uint8_t BitmapCompositeFont_CharBorder(font_t *font, unsigned char chr);

Expand Down
80 changes: 28 additions & 52 deletions doomsday/client/include/resource/font.h
Expand Up @@ -17,16 +17,12 @@
* http://www.gnu.org/licenses</small>
*/

#ifndef LIBDENG_FONT_H
#define LIBDENG_FONT_H
#ifndef CLIENT_RESOURCE_FONT_H
#define CLIENT_RESOURCE_FONT_H

#include "dd_types.h"
#include "def_main.h"

#ifdef __cplusplus
extern "C" {
#endif

// Font types.
typedef enum {
FT_FIRST = 0,
Expand All @@ -51,11 +47,14 @@ typedef enum {
* @ingroup refresh
*/
#define MAX_CHARS 256 // Normal 256 ANSI characters.
typedef struct font_s {

class font_t
{
public:
fonttype_t _type;

/// @c true= Font requires a full update.
boolean _isDirty;
bool _isDirty;

/// @ref fontFlags.
int _flags;
Expand All @@ -73,60 +72,37 @@ typedef struct font_s {
/// dj: Do fonts have margins? Is this a pixel border in the composited
/// character map texture (perhaps per-glyph)?
int _marginWidth, _marginHeight;
} font_t;

font_t *Font_New(fonttype_t type, fontid_t bindId);
void Font_Delete(font_t *font);

void Font_Init(font_t *font, fonttype_t type, fontid_t bindId);

font_t *Font_FromDef(fontid_t bindId, ded_compositefont_t *def);

font_t *Font_FromFile(fontid_t bindId, char const *resourcePath);
font_t(fonttype_t type = FT_FIRST, fontid_t bindId = 0);
virtual ~font_t() {}

/**
* Update the Font according to the supplied definition.
* To be called after an engine update/reset.
*
* @param font Font to be updated.
* @param def font definition to update using.
*/
void Font_RebuildFromDef(font_t *font, ded_compositefont_t *def);
DENG2_AS_IS_METHODS()

void Font_RebuildFromFile(font_t *font, char const *resourcePath);
fonttype_t type() const;

void Font_Release(font_t *font);
fontid_t primaryBind() const;

boolean Font_IsPrepared(font_t *font);
void setPrimaryBind(fontid_t bindId);

void Font_Prepare(font_t *font);
/// @return @ref fontFlags
int flags() const;

fonttype_t Font_Type(font_t const *font);
int ascent();
int descent();
int lineSpacing();

fontid_t Font_PrimaryBind(font_t const *font);
virtual void glInit();
virtual void glDeinit();

void Font_SetPrimaryBind(font_t *font, fontid_t bindId);
virtual RectRaw const *charGeometry(unsigned char ch) = 0;
virtual int charWidth(unsigned char ch) = 0;
virtual int charHeight(unsigned char ch) = 0;

/// @return @ref fontFlags
int Font_Flags(font_t const *font);
void charSize(Size2Raw *size, unsigned char ch);
};

int Font_Ascent(font_t *font);

int Font_Descent(font_t *font);

int Font_Leading(font_t *font);

/**
* Query the visible dimensions of a character in this font.
*/
void Font_CharSize(font_t *font, Size2Raw *size, unsigned char ch);

int Font_CharHeight(font_t *font, unsigned char ch);

int Font_CharWidth(font_t *font, unsigned char ch);
font_t *Font_FromFile(fontid_t bindId, char const *resourcePath);

#ifdef __cplusplus
} // extern "C"
#endif
void Font_RebuildFromFile(font_t *font, char const *resourcePath);

#endif /* LIBDENG_FONT_H */
#endif // CLIENT_RESOURCE_FONT_H
24 changes: 9 additions & 15 deletions doomsday/client/include/resource/fonts.h
Expand Up @@ -23,13 +23,12 @@

#include "dd_share.h" // fontschemeid_t
#include "def_data.h"
#include "resource/font.h"
#include "uri.hh"

/// Special value used to signify an invalid font id.
#define NOFONTID 0

struct font_s;

namespace de {

/**
Expand Down Expand Up @@ -77,7 +76,7 @@ class Fonts
* Returns the symbolic name associated with the unique @a schemeId; otherwise
* a zero-length string.
*/
ddstring_t const *schemeName(fontschemeid_t schemeId);
String const &schemeName(fontschemeid_t schemeId);

/**
* Returns the total number of fonts in the collection.
Expand Down Expand Up @@ -115,12 +114,12 @@ class Fonts
/**
* Returns the unique identifier of the primary name for @a font else @c NOFONTID.
*/
fontid_t id(struct font_s *font);
fontid_t id(font_t *font);

/**
* Returns the Font associated with unique identifier @a fontId else @c NULL.
*/
struct font_s *toFont(fontid_t fontId);
font_t *toFont(fontid_t fontId);

/**
* Returns the Font associated with the scheme-unique identifier @a index else @c NOFONTID.
Expand All @@ -137,20 +136,15 @@ class Fonts
*/
fontschemeid_t scheme(fontid_t fontId);

/**
* Returns the symbolic name/path-to this font as a string.
*/
AutoStr *composePath(fontid_t fontId);

/**
* Composes the URI to this font. Must be destroyed with Uri_Delete().
*/
Uri *composeUri(fontid_t fontid);
Uri composeUri(fontid_t fontid);

/**
* Compose the unique URN to this font. Must be destroyed with Uri_Delete().
*/
Uri *composeUrn(fontid_t fontId);
Uri composeUrn(fontid_t fontId);

/**
* Search the Fonts collection for a font associated with @a uri.
Expand All @@ -175,9 +169,9 @@ class Fonts
*/
fontid_t declare(Uri const &uri, int uniqueId);

struct font_s *createFontFromFile(Uri const &uri, char const *resourcePath);
font_t *createFontFromFile(Uri const &uri, char const *resourcePath);

struct font_s *createFontFromDef(ded_compositefont_t *def);
font_t *createFontFromDef(ded_compositefont_t *def);

/**
* Iterate over defined Fonts in the collection making a callback for each
Expand All @@ -191,7 +185,7 @@ class Fonts
*
* @return @c 0 iff iteration completed wholly.
*/
int iterate(fontschemeid_t schemeId, int (*callback)(struct font_s *font, void *context),
int iterate(fontschemeid_t schemeId, int (*callback)(font_t *font, void *context),
void *context = 0);

/**
Expand Down
4 changes: 2 additions & 2 deletions doomsday/client/src/con_main.cpp
Expand Up @@ -2797,9 +2797,9 @@ D_CMD(Font)
fontid_t newFont = App_Fonts().resolveUri(uri, true/*quiet please*/);
if(newFont)
{
QScopedPointer<de::Uri> uri(App_Fonts().composeUri(newFont));
de::Uri uri = App_Fonts().composeUri(newFont);
Con_SetFont(newFont);
if(!uri->scheme().compareWithoutCase("Game"))
if(!uri.scheme().compareWithoutCase("Game"))
{
Con_SetFontScale(1.5f, 2);
Con_SetFontLeading(1.25f);
Expand Down
6 changes: 2 additions & 4 deletions doomsday/client/src/render/r_main.cpp
Expand Up @@ -192,8 +192,7 @@ static fontid_t loadSystemFont(char const *name)
DENG2_ASSERT(name != 0 && name[0]);

// Compose the resource name.
de::Uri uri("System:", RC_NULL);
uri.setPath(name);
de::Uri uri = de::Uri("System:", RC_NULL).setPath(name);

// Compose the resource data path.
ddstring_t resourcePath; Str_InitStd(&resourcePath);
Expand Down Expand Up @@ -222,8 +221,7 @@ static void loadFontIfNeeded(char const *uri, fontid_t *fid)
*fid = NOFONTID;
if(uri && uri[0])
{
*fid = App_Fonts().resolveUri(de::Uri(uri, RC_NULL),
!(verbose >= 1)/*log warnings if verbose*/);
*fid = App_Fonts().resolveUri(de::Uri(uri, RC_NULL), !(verbose >= 1)/*log warnings if verbose*/);
}

if(*fid == NOFONTID)
Expand Down

0 comments on commit c3ff0e8

Please sign in to comment.