Skip to content

Commit

Permalink
Refactor|AbstractFont|Resources|Client: Relocated more functionality …
Browse files Browse the repository at this point in the history
…out of AbstractFont
  • Loading branch information
danij-deng committed Nov 13, 2013
1 parent 5fdd9d1 commit d9922a8
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 58 deletions.
25 changes: 8 additions & 17 deletions doomsday/client/include/resource/abstractfont.h
Expand Up @@ -43,41 +43,32 @@
class AbstractFont
{
public:
/// @ref fontFlags.
int _flags;

/// Unique identifier of the primary binding in the owning collection.
fontid_t _primaryBind;

/// Font metrics.
int _leading;
int _ascent;
int _descent;

/// Do fonts have margins? Is this a pixel border in the composited character
/// map texture (perhaps per-glyph)?
de::Vector2ui _margin;
/// @ref fontFlags.
int _flags;

AbstractFont(fontid_t bindId = 0);
virtual ~AbstractFont() {}

DENG2_AS_IS_METHODS()

fontid_t primaryBind() const;
void setPrimaryBind(fontid_t bindId);

/// @return @ref fontFlags
int flags() const;

int ascent();
int descent();
int lineSpacing();
virtual int ascent();
virtual int descent();
virtual int lineSpacing();

virtual void glInit();
virtual void glDeinit();

virtual de::Rectanglei const &glyphPosCoords(uchar ch) = 0;
virtual de::Rectanglei const &glyphTexCoords(uchar ch) = 0;

fontid_t primaryBind() const;
void setPrimaryBind(fontid_t bindId);
};

#endif // CLIENT_RESOURCE_ABSTRACTFONT_H
6 changes: 5 additions & 1 deletion doomsday/client/include/resource/bitmapfont.h
Expand Up @@ -38,12 +38,16 @@ class BitmapFont : public AbstractFont

static BitmapFont *fromFile(fontid_t bindId, de::String resourcePath);

void rebuildFromFile(de::String resourcePath);
void setFilePath(de::String resourcePath);

/// @return GL-texture name.
GLuint textureGLName() const;
de::Vector2i const &textureDimensions() const;
de::Vector2ui const &textureMargin() const;

int ascent();
int descent();
int lineSpacing();

void glInit();
void glDeinit();
Expand Down
4 changes: 4 additions & 0 deletions doomsday/client/include/resource/compositebitmapfont.h
Expand Up @@ -62,6 +62,10 @@ class CompositeBitmapFont : public AbstractFont
*/
void rebuildFromDef(ded_compositefont_t *def);

int ascent();
int descent();
int lineSpacing();

void glInit();
void glDeinit();

Expand Down
23 changes: 12 additions & 11 deletions doomsday/client/src/render/rend_font.cpp
Expand Up @@ -776,6 +776,18 @@ static void drawChar(uchar ch, float x, float y, AbstractFont *font,
/// @todo We should not need to re-bind this texture here.
GL_BindTextureUnmanaged(bmapFont->textureGLName(), gl::ClampToEdge,
gl::ClampToEdge, filterUI? gl::Linear : gl::Nearest);

Vector2ui const &texMargin = bmapFont->textureMargin();
if(texMargin.x)
{
geometry.topLeft.x -= texMargin.x;
geometry.setWidth(geometry.width() + texMargin.x * 2);
}
if(texMargin.y)
{
geometry.topLeft.y -= texMargin.y;
geometry.setHeight(geometry.height() + texMargin.y * 2);
}
}
else if(CompositeBitmapFont *compFont = font->maybeAs<CompositeBitmapFont>())
{
Expand All @@ -786,17 +798,6 @@ static void drawChar(uchar ch, float x, float y, AbstractFont *font,
}
}

if(font->_margin.x)
{
geometry.topLeft.x -= font->_margin.x;
geometry.setWidth(geometry.width() + font->_margin.x * 2);
}
if(font->_margin.y)
{
geometry.topLeft.y -= font->_margin.y;
geometry.setHeight(geometry.height() + font->_margin.y * 2);
}

Vector2i coords[4] = { font->glyphTexCoords(ch).topLeft,
font->glyphTexCoords(ch).topRight(),
font->glyphTexCoords(ch).bottomRight,
Expand Down
13 changes: 3 additions & 10 deletions doomsday/client/src/resource/abstractfont.cpp
Expand Up @@ -25,9 +25,6 @@ using namespace de;
AbstractFont::AbstractFont(fontid_t bindId)
: _flags(0)
, _primaryBind(bindId)
, _leading(0)
, _ascent(0)
, _descent(0)
{}

void AbstractFont::glInit()
Expand All @@ -53,19 +50,15 @@ int AbstractFont::flags() const

int AbstractFont::ascent()
{
glInit();
return _ascent;
return 0;
}

int AbstractFont::descent()
{
glInit();
return _descent;
return 0;
}

int AbstractFont::lineSpacing()
{
glInit();
return _leading;
return 0;
}

57 changes: 42 additions & 15 deletions doomsday/client/src/resource/bitmapfont.cpp
Expand Up @@ -52,18 +52,27 @@ struct Glyph

DENG2_PIMPL(BitmapFont)
{
String filePath; ///< The "archived" version of this font (if any).
GLuint texGLName; ///< GL-texture name.
Vector2i texDimensions; ///< Texture dimensions in pixels.
String filePath; ///< The "archived" version of this font (if any).
GLuint texGLName; ///< GL-texture name.
de::Vector2ui texMargin; ///< Margin in pixels.
Vector2i texDimensions; ///< Texture dimensions in pixels.
bool needGLInit;

/// Font metrics.
int leading;
int ascent;
int descent;

Glyph glyphs[MAX_CHARS];
Glyph missingGlyph;

Instance(Public *i)
: Base(i)
, texGLName(0)
, needGLInit(true)
, leading(0)
, ascent(0)
, descent(0)
{}

~Instance()
Expand All @@ -83,7 +92,7 @@ DENG2_PIMPL(BitmapFont)

self._flags |= FF_COLORIZE;
self._flags &= ~FF_SHADOWED;
self._margin = Vector2ui(0, 0);
texMargin = Vector2ui(0, 0);

// Load in the data.
texDimensions.x = inShort(file);
Expand All @@ -99,7 +108,7 @@ DENG2_PIMPL(BitmapFont)
ushort w = inByte(file);
ushort h = inByte(file);

ch->posCoords = Rectanglei::fromSize(Vector2i(0, 0), Vector2ui(w, h) - self._margin * 2);
ch->posCoords = Rectanglei::fromSize(Vector2i(0, 0), Vector2ui(w, h) - texMargin * 2);
ch->texCoords = Rectanglei::fromSize(Vector2i(x, y), Vector2ui(w, h));

avgSize += ch->posCoords.size();
Expand Down Expand Up @@ -152,12 +161,12 @@ DENG2_PIMPL(BitmapFont)
texDimensions.x = inShort(file);
texDimensions.y = M_CeilPow2(inShort(file));
int glyphCount = inShort(file);
self._margin.x = self._margin.y = inShort(file);
texMargin.x = texMargin.y = inShort(file);

self._leading = inShort(file);
leading = inShort(file);
/*glyphHeight =*/ inShort(file); // Unused.
self._ascent = inShort(file);
self._descent = inShort(file);
ascent = inShort(file);
descent = inShort(file);

Vector2ui avgSize;
for(int i = 0; i < glyphCount; ++i)
Expand All @@ -169,7 +178,7 @@ DENG2_PIMPL(BitmapFont)
ushort h = inShort(file);
Glyph *ch = &glyphs[code];

ch->posCoords = Rectanglei::fromSize(Vector2i(0, 0), self._margin * 2 - Vector2ui(w, h));
ch->posCoords = Rectanglei::fromSize(Vector2i(0, 0), texMargin * 2 - Vector2ui(w, h));
ch->texCoords = Rectanglei::fromSize(Vector2i(x, y), Vector2ui(w, h));

avgSize += ch->posCoords.size();
Expand Down Expand Up @@ -213,11 +222,6 @@ BitmapFont::BitmapFont(fontid_t bindId) : AbstractFont(), d(new Instance(this))
setPrimaryBind(bindId);
}

void BitmapFont::rebuildFromFile(String resourcePath)
{
setFilePath(resourcePath);
}

BitmapFont *BitmapFont::fromFile(fontid_t bindId, String resourcePath) // static
{
BitmapFont *font = new BitmapFont(bindId);
Expand All @@ -230,6 +234,24 @@ BitmapFont *BitmapFont::fromFile(fontid_t bindId, String resourcePath) // static
return font;
}

int BitmapFont::ascent()
{
glInit();
return d->ascent;
}

int BitmapFont::descent()
{
glInit();
return d->descent;
}

int BitmapFont::lineSpacing()
{
glInit();
return d->leading;
}

Rectanglei const &BitmapFont::glyphPosCoords(uchar ch)
{
glInit();
Expand Down Expand Up @@ -320,3 +342,8 @@ Vector2i const &BitmapFont::textureDimensions() const
{
return d->texDimensions;
}

Vector2ui const &BitmapFont::textureMargin() const
{
return d->texMargin;
}
29 changes: 27 additions & 2 deletions doomsday/client/src/resource/compositebitmapfont.cpp
Expand Up @@ -33,13 +33,21 @@ DENG2_PIMPL(CompositeBitmapFont)
ded_compositefont_t *def; /// Definition on which "this" font is derived (if any).
bool needGLInit;

/// Font metrics.
int leading;
int ascent;
int descent;

Glyph glyphs[MAX_CHARS];
Glyph missingGlyph;

Instance(Public *i)
: Base(i)
, def(0)
, needGLInit(true)
, leading(0)
, ascent(0)
, descent(0)
{
zap(glyphs);
zap(missingGlyph);
Expand All @@ -65,6 +73,24 @@ CompositeBitmapFont::CompositeBitmapFont(fontid_t bindId)
setPrimaryBind(bindId);
}

int CompositeBitmapFont::ascent()
{
glInit();
return d->ascent;
}

int CompositeBitmapFont::descent()
{
glInit();
return d->descent;
}

int CompositeBitmapFont::lineSpacing()
{
glInit();
return d->leading;
}

Rectanglei const &CompositeBitmapFont::glyphPosCoords(uchar ch)
{
glInit();
Expand Down Expand Up @@ -142,8 +168,7 @@ void CompositeBitmapFont::glInit()
}

ch->geometry = Rectanglei::fromSize(Vector2i(info.geometry.origin.xy),
Vector2ui(info.geometry.size.width, info.geometry.size.height))
.expanded(_margin.toVector2i());
Vector2ui(info.geometry.size.width, info.geometry.size.height));

avgSize += ch->geometry.size();
++foundGlyphs;
Expand Down
4 changes: 2 additions & 2 deletions doomsday/client/src/resource/fonts.cpp
Expand Up @@ -508,7 +508,7 @@ DENG2_PIMPL(Fonts)
LOG_DEBUG("A Font with uri \"%s\" already exists, returning existing.")
<< self.composeUri(id);
#endif
bmapFont->rebuildFromFile(resourcePath);
bmapFont->setFilePath(resourcePath);
}
return record->font;
}
Expand Down Expand Up @@ -1106,7 +1106,7 @@ AbstractFont *Fonts::createFontFromFile(Uri const &uri, char const *resourcePath
{
if(BitmapFont *bmapFont = font->maybeAs<BitmapFont>())
{
bmapFont->rebuildFromFile(resourcePath);
bmapFont->setFilePath(resourcePath);
}
}
else
Expand Down

0 comments on commit d9922a8

Please sign in to comment.