Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Addition for Font  (Read 4954 times)

0 Members and 1 Guest are viewing this topic.

tgm

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Addition for Font
« on: April 23, 2008, 08:39:21 pm »
A friend of mine+Me are currently building a GUI for SFML.
Doing editfields with mouse support is quite hard without access to the fonts character dimensions. So i did add a member to Font, that will return a Vector containing the size of a given character with a given style and a given scale. Since I got no SVN access I gonna post the patch in here..
(hoping it will be used in the SVN version)

Font.hpp:
Code: [Select]

#include <SFML/System/Vector2.hpp>
//..
//.. Rest of the code here
//..



////////////////////////////////////////////////////////////

    /// gets the size of a given character if renderet with this font

    ///

    /// \param c : The character, whose size should be returned

    /// \param s : The style of the character (default: Regular)

    /// \param scale : Scale of the string (default: 1)

    ///

    /// \return the Vector2f containing the size of the character

    ///

    ////////////////////////////////////////////////////////////
Vector2f  GetCharSize(wchar_t c, int s=0, float scale=1);
Vector2f  GetCharSize(char c, int s=0, float scale=1);


Font.cpp:
Code: [Select]

////////////////////////////////////////////////////////////

/// Gets the Vector2f containing the size of a single Character

////////////////////////////////////////////////////////////
Vector2f  Font::GetCharSize(wchar_t c, int s, float scale)
{
float Factor    = scale / myCharSize;
Vector2f  retValue(0,0);

std::map<wchar_t, Font::Character>::const_iterator It = myCharacters.find(c);

if (It == myCharacters.end())

{

return (retValue);

}

const Character& CurChar = It->second;

const IntRect& Rect = CurChar.Rect;

float Advance = CurChar.Advance * Factor;
// use Advance as size.y of the character

retValue.x += Advance;


// get size.x for  this character

float CharHeight = (myCharSize + Rect.Bottom) * Factor;

retValue.y = CharHeight;

if (s & 1)

    {

        retValue.x  += 1 * Factor;

        retValue.y += 1 * Factor;

    }



    // Add a slight width if we're using the italic style

    if (s & 2)

    {

        retValue.x += 0.208f * scale;

    }



    // Add a slight height if we're using the underlined style

    if (s & 4)

    {

        if (retValue.y < scale + 4 * Factor)

            retValue.y += 4 * Factor;

    }
    return retValue;
}


////////////////////////////////////////////////////////////

/// Gets the Vector2f containing the size of a single Character

////////////////////////////////////////////////////////////
Vector2f  Font::GetCharSize(char c, int s, float scale)
{
return GetCharSize((wchar_t)(c),s,scale);
}


int s is the style of the string, int scale, the scale of the String and c a single character  :lol:
greetz TGM

*edit* one small error in there..
Code: [Select]
Vector2f  Font::GetCharSize(char c, int s, float scale)
{
return GetCharSize((char)(c),s,scale);
}

has to be
Code: [Select]

Vector2f  Font::GetCharSize(char c, int s, float scale)
{
return GetCharSize((wchar_t)(c),s,scale);
}
[/code]

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
Addition for Font
« Reply #1 on: April 23, 2008, 09:48:52 pm »
Thanks a lot man, this is really gonna be useful later in my currrent project. I hope it will be added to the SVN.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Addition for Font
« Reply #2 on: April 24, 2008, 03:25:50 am »
I agree, this is an important feature especially for GUI and text input.

I'll try to integrate this for the next release, thank you a lot :)
Laurent Gomila - SFML developer

tgm

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Addition for Font
« Reply #3 on: April 25, 2008, 10:31:39 am »
sry.. update..