Skip to content

Commit

Permalink
Refactor|ResourceSystem|Fonts|Client: Cleanup of Font resource manage…
Browse files Browse the repository at this point in the history
…ment continues
  • Loading branch information
danij-deng committed Nov 10, 2013
1 parent 1c2a92c commit 1ac294c
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 394 deletions.
50 changes: 41 additions & 9 deletions doomsday/client/include/resource/font.h
Expand Up @@ -21,6 +21,7 @@
#define LIBDENG_FONT_H

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

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -74,24 +75,55 @@ typedef struct font_s {
int _marginWidth, _marginHeight;
} font_t;

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

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

fontid_t Font_PrimaryBind(const font_t* font);
font_t *Font_FromDef(fontid_t bindId, ded_compositefont_t *def);

void Font_SetPrimaryBind(font_t* font, fontid_t bindId);
font_t *Font_FromFile(fontid_t bindId, char const *resourcePath);

/**
* 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);

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

void Font_Release(font_t *font);

boolean Font_IsPrepared(font_t *font);

void Font_Prepare(font_t *font);

fonttype_t Font_Type(font_t const *font);

fontid_t Font_PrimaryBind(font_t const *font);

void Font_SetPrimaryBind(font_t *font, fontid_t bindId);

/// @return @ref fontFlags
int Font_Flags(const font_t* font);
int Font_Flags(font_t const *font);

int Font_Ascent(font_t *font);

int Font_Ascent(font_t* font);
int Font_Descent(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_Leading(font_t* font);
int Font_CharHeight(font_t *font, unsigned char ch);

boolean Font_IsPrepared(font_t* font);
int Font_CharWidth(font_t *font, unsigned char ch);

#ifdef __cplusplus
} // extern "C"
Expand Down
131 changes: 57 additions & 74 deletions doomsday/client/include/resource/fonts.h
Expand Up @@ -41,11 +41,11 @@ namespace de {
* @em System fonts are loaded at startup and remain in memory all the time. After
* clearing they must be manually reloaded.
*
* "Clearing" a font means any names bound to it are deleted (on client side, any
* GL textures acquired for it are 'released' at this time). The Font instance
* record used to represent it is also deleted.
* "Clearing" a font means any names bound to it are deleted and any GL textures
* acquired for it are 'released' at this time). The Font instance record used
* to represent it is also deleted.
*
* "Releasing" a font on client side will release any GL textures acquired for it.
* "Releasing" a font will release any GL textures acquired for it.
*
* Thus there are two general states for a font:
*
Expand All @@ -66,30 +66,42 @@ class Fonts
void clearDefinitionLinks();

/**
* Try to interpret a font scheme identifier from @a str.
* If found to match a known scheme name, return the associated identifier.
* If the reference @a str is not valid (i.e., NULL or a zero-length string)
* then the special identifier @c FS_ANY is returned.
* Otherwise @c FS_INVALID.
* Try to interpret a font scheme identifier from @a str. If found to match
* a known scheme name, return the associated identifier. If the reference
* @a str is not valid (i.e., NULL or a zero-length string) then the special
* identifier @c FS_ANY is returned. Otherwise @c FS_INVALID.
*/
fontschemeid_t parseScheme(char const *str);

/// @return Name associated with the identified @a schemeId else a zero-length string.
/**
* Returns the symbolic name associated with the unique @a schemeId; otherwise
* a zero-length string.
*/
ddstring_t const *schemeName(fontschemeid_t schemeId);

/// @return Total number of unique Fonts in the collection.
/**
* Returns the total number of fonts in the collection.
*/
uint size();

/// @return Number of unique Fonts in the identified @a schemeId.
/**
* Returns the number of fonts in the identified @a schemeId.
*/
uint count(fontschemeid_t schemeId);

/// Clear all fonts in all schemes (and release any acquired GL-textures).
/**
* Clear all fonts in all schemes (and release any acquired GL-textures).
*/
void clear();

/// Clear all fonts flagged 'runtime' (and release any acquired GL-textures).
/**
* Clear all fonts flagged 'runtime' (and release any acquired GL-textures).
*/
void clearRuntime();

/// Clear all fonts flagged 'system' (and release any acquired GL-textures).
/**
* Clear all fonts flagged 'system' (and release any acquired GL-textures).
*/
void clearSystem();

/**
Expand All @@ -100,35 +112,52 @@ class Fonts
*/
void clearScheme(fontschemeid_t schemeId);

/// @return Unique identifier of the primary name for @a font else @c NOFONTID.
/**
* Returns the unique identifier of the primary name for @a font else @c NOFONTID.
*/
fontid_t id(struct font_s *font);

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

/// @return Font associated with the scheme-unique identifier @a index else @c NOFONTID.
/**
* Returns the Font associated with the scheme-unique identifier @a index else @c NOFONTID.
*/
fontid_t fontForUniqueId(fontschemeid_t schemeId, int uniqueId);

/// @return Scheme-unique identfier associated with the identified @a fontId.
/**
* Returns the scheme-unique identfier associated with the identified @a fontId.
*/
int uniqueId(fontid_t fontId);

/// @return Unique identifier of the scheme this name is in.
/**
* Returns the unique identifier of the scheme this name is in.
*/
fontschemeid_t scheme(fontid_t fontId);

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

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

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

/**
* Search the Fonts collection for a font associated with @a uri.
*
* @param uri Either a path or URN to the font.
* @param quiet @c true: suppress the console message that is printed if the Uri cannot be found.
* @param quiet @c true: suppress the console message that is printed if the
* Uri cannot be found.
*
* @return Unique identifier of the found texture else @c NOFONTID.
*/
Expand Down Expand Up @@ -180,10 +209,6 @@ class Fonts
int iterateDeclared(fontschemeid_t schemeId, int (*callback)(fontid_t textureId, void *context),
void *context = 0);

inline void releaseAllTextures() { releaseTexturesByScheme(FS_ANY); }

void releaseTexturesByScheme(fontschemeid_t schemeId);

/**
* To be called during engine/gl-subsystem reset to release all resources
* acquired from the GL subsystem (v-buffers, d-lists, textures, etc...)
Expand All @@ -195,59 +220,17 @@ class Fonts
void releaseRuntimeTextures();
void releaseSystemTextures();

/// @return List of collected font names.
ddstring_t **collectNames(int *count);
void releaseTexturesByScheme(fontschemeid_t schemeId);

public: /// @todo should be private:
/// Load an external font from a local file.
struct font_s *createFromFile(fontid_t id, char const *filePath);
inline void releaseAllTextures() { releaseTexturesByScheme(FS_ANY); }

/// Create a bitmap composite font from @a def.
struct font_s *createFromDef(fontid_t id, ded_compositefont_t *def);
/// @return List of collected font names.
ddstring_t **collectNames(int *count);

private:
DENG2_PRIVATE(d)
};

} // namespace de

#ifdef __cplusplus
extern "C" {
#endif

/**
* 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(struct font_s *font, ded_compositefont_t *def);

void Font_RebuildFromFile(struct font_s *font, char const *resourcePath);

/// Same as Fonts_ResolveUri except @a uri is a C-string.
fontid_t Fonts_ResolveUriCString2(char const *uri, boolean quiet);
fontid_t Fonts_ResolveUriCString(char const *uri); /*quiet=!(verbose >= 1)*/

/*
* Here follows miscellaneous routines currently awaiting refactoring into the
* revised resource and texture management APIs.
*/

int Fonts_Ascent(struct font_s *font);
int Fonts_Descent(struct font_s *font);
int Fonts_Leading(struct font_s *font);

/**
* Query the visible dimensions of a character in this font.
*/
void Fonts_CharSize(struct font_s *font, Size2Raw *size, unsigned char ch);
int Fonts_CharHeight(struct font_s *font, unsigned char ch);
int Fonts_CharWidth(struct font_s *font, unsigned char ch);

#ifdef __cplusplus
} // extern "C"
#endif

#endif // DENG_RESOURCE_FONTS_H
8 changes: 7 additions & 1 deletion doomsday/client/src/render/r_main.cpp
Expand Up @@ -219,7 +219,13 @@ static fontid_t loadSystemFont(char const *name)

static void loadFontIfNeeded(char const *uri, fontid_t *fid)
{
*fid = Fonts_ResolveUriCString(uri);
*fid = NOFONTID;
if(uri && uri[0])
{
*fid = App_Fonts().resolveUri(de::Uri(uri, RC_NULL),
!(verbose >= 1)/*log warnings if verbose*/);
}

if(*fid == NOFONTID)
{
*fid = loadSystemFont(uri);
Expand Down
35 changes: 20 additions & 15 deletions doomsday/client/src/render/rend_font.cpp
Expand Up @@ -124,18 +124,18 @@ static void errorIfNotInited(const char* callerName)

static int topToAscent(font_t* font)
{
int lineHeight = Fonts_Leading(font);
int lineHeight = Font_Leading(font);
if(lineHeight == 0)
return 0;
return lineHeight - Fonts_Ascent(font);
return lineHeight - Font_Ascent(font);
}

static int lineHeight(font_t* font, unsigned char ch)
{
int ascent = Fonts_Ascent(font);
int ascent = Font_Ascent(font);
if(ascent != 0)
return ascent;
return Fonts_CharHeight(font, ch);
return Font_CharHeight(font, ch);
}

static __inline fr_state_attributes_t* currentAttribs(void)
Expand Down Expand Up @@ -429,15 +429,15 @@ void FR_SetCaseScale(boolean value)
void FR_CharSize(Size2Raw* size, unsigned char ch)
{
errorIfNotInited("FR_CharSize");
Fonts_CharSize(App_Fonts().toFont(fr.fontNum), size, ch);
Font_CharSize(App_Fonts().toFont(fr.fontNum), size, ch);
}

#undef FR_CharWidth
int FR_CharWidth(unsigned char ch)
{
errorIfNotInited("FR_CharWidth");
if(fr.fontNum != 0)
return Fonts_CharWidth(App_Fonts().toFont(fr.fontNum), ch);
return Font_CharWidth(App_Fonts().toFont(fr.fontNum), ch);
return 0;
}

Expand All @@ -446,7 +446,7 @@ int FR_CharHeight(unsigned char ch)
{
errorIfNotInited("FR_CharHeight");
if(fr.fontNum != 0)
return Fonts_CharHeight(App_Fonts().toFont(fr.fontNum), ch);
return Font_CharHeight(App_Fonts().toFont(fr.fontNum), ch);
return 0;
}

Expand All @@ -455,11 +455,11 @@ int FR_SingleLineHeight(const char* text)
errorIfNotInited("FR_SingleLineHeight");
if(fr.fontNum == 0 || !text)
return 0;
{ int ascent = Fonts_Ascent(App_Fonts().toFont(fr.fontNum));
{ int ascent = Font_Ascent(App_Fonts().toFont(fr.fontNum));
if(ascent != 0)
return ascent;
}
return Fonts_CharHeight(App_Fonts().toFont(fr.fontNum), (unsigned char)text[0]);
return Font_CharHeight(App_Fonts().toFont(fr.fontNum), (unsigned char)text[0]);
}

int FR_GlyphTopToAscent(const char* text)
Expand All @@ -468,10 +468,10 @@ int FR_GlyphTopToAscent(const char* text)
errorIfNotInited("FR_GlyphTopToAscent");
if(fr.fontNum == 0 || !text)
return 0;
lineHeight = Fonts_Leading(App_Fonts().toFont(fr.fontNum));
lineHeight = Font_Leading(App_Fonts().toFont(fr.fontNum));
if(lineHeight == 0)
return 0;
return lineHeight - Fonts_Ascent(App_Fonts().toFont(fr.fontNum));
return lineHeight - Font_Ascent(App_Fonts().toFont(fr.fontNum));
}

static int textFragmentWidth(const char* fragment)
Expand Down Expand Up @@ -757,9 +757,9 @@ static void drawChar(unsigned char ch, int posX, int posY, font_t* font,
RectRaw geometry;

if(alignFlags & ALIGN_RIGHT)
x -= Fonts_CharWidth(font, ch);
x -= Font_CharWidth(font, ch);
else if(!(alignFlags & ALIGN_LEFT))
x -= Fonts_CharWidth(font, ch) / 2;
x -= Font_CharWidth(font, ch) / 2;

if(alignFlags & ALIGN_BOTTOM)
y -= topToAscent(font) + lineHeight(font, ch);
Expand Down Expand Up @@ -1563,8 +1563,13 @@ void FR_Init(void)
typeInTime = 0;
}

// fonts.cpp
DENG_EXTERN_C fontid_t Fonts_ResolveUri(uri_s const *uri); /*quiet=!(verbose >= 1)*/
#undef Fonts_ResolveUri
DENG_EXTERN_C fontid_t Fonts_ResolveUri(uri_s const *uri)
{
if(!uri) return NOFONTID;
return App_Fonts().resolveUri(*reinterpret_cast<de::Uri const *>(uri),
!(verbose >= 1)/*log warnings if verbose*/);
}

DENG_DECLARE_API(FR) =
{
Expand Down

0 comments on commit 1ac294c

Please sign in to comment.