Sign in to follow this  
Metal Typhoon

Centering 2d text ?

Recommended Posts

i've done the same for centering 3d font a in nehe's 14th lesson but now i would like to have the same but with bitmaped fonts . Now what i would usually what's behind the code is.. (ScreenWidth / 2) - AllStringAddedTextWidth ... how do i find the width being used by CreateFont when i set the mWidth parameters as 0 ? thank you in advance [Edited by - Metal Typhoon on June 30, 2005 11:16:11 PM]

Share this post


Link to post
Share on other sites
You ask how to find width from font that is created by some function... well, we don't know how this function looks like or where did you take it from, so we can't say anything for sure. How to count width, depends on your code, how it's designed, so one could use sth trivial like function GetTextWidth() { return width; }, other would need to write sth like this:


// ------------------------------------------------- start TextWidth

SC :: ulint SC :: Renderer :: TextWidth(const char * _text, const char * _fontName, usint _size, ulint _style,
eTextRenderMode _renderMode)
{
SC_ASSERT(_text)
SC_ASSERT(_fontName)
SC_ASSERT(_size > 0)

TGlyphsContainer * glyphs = getFontMgr.GetGlyphsContainer(_fontName, _size);

if (!glyphs)
{
logError2("Renderer", "Couldn't find font to count text width.")
return 0;
}

// -------------------------------------------------

std :: vector < std :: string> vLines; // these are the individual lines of text
std :: string text(_text);

int n = 0;

// Break the string into its lines, remember that it's different from std C++ language: end of line
// is determined by "/n" not "\n" existance!
while( n != -1 )
{
// Get until either '/n' or '\0'
std :: string strSub;
n = text.find( "/n", 0 ); // Find the next "/n"
strSub = text.substr( 0, n);
if( n != -1 )
text = text.substr( n+2, std::string::npos );

vLines.push_back(strSub);
}

ulint maxRenderedTextWidth = 0;

for (std :: vector<std :: string> :: iterator itor = vLines.begin(); itor != vLines.end(); ++itor)
{
const char * txtPtr = (*itor).c_str();
ulint renderedTextWidth = 0;

// sum width of all letters from _text
while (*txtPtr)
{
GlyphTexture * glyph = glyphs->GetGlyphTexture(*txtPtr, _style, _renderMode);
renderedTextWidth += glyph->advance;
++txtPtr;
}

maxRenderedTextWidth = Max(maxRenderedTextWidth, renderedTextWidth);
}

return maxRenderedTextWidth;
}

// ------------------------------------------------- end TextWidth




In short: post some code! :-)


Btw, easy way for generic text centering, is to write sth like this:



if (_horizontal == tp_Centered) // if we need to center text...
xPos -= (renderedText.x)/2; // just move its x position to left


Share this post


Link to post
Share on other sites
The thing is that i also dont know.. because i use CreateFont from the Win API so that'swhy i was wondering if someone knew whre to get the width of the certain font at certain height.

Share this post


Link to post
Share on other sites
Also, be aware that unless it's a monospaced font, each letter will have a different width - so what text you want to display depends on how it's centred - regardless of how many actual letters there are.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this