Back to index

nux  3.0.0
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Private Attributes | Friends
nux::FontTexture Class Reference

#include <FontTexture.h>

Inheritance diagram for nux::FontTexture:
Inheritance graph
[legend]
Collaboration diagram for nux::FontTexture:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 NUX_DECLARE_OBJECT_TYPE (FontTexture, Object)
 FontTexture (const char *FontFile, NUX_FILE_LINE_PROTO)
 FontTexture (INT width, INT height, BYTE *Texture)
 ~FontTexture ()
int GetLineHeight () const
int GetCharWidth (const char &c) const
int GetStringWidth (const NString &str) const
int GetCharStringWidth (const char *str) const
int GetStringWidth (const NString &str, int num_char_to_compute) const
int GetCharStringWidth (const char *str, int num_char_to_compute) const
int GetFontHeight ()
bool CursorPosToX (const NString &Str, int icp, bool fTrailing, int *pX)
bool XToCursorPosition (const NString &Str, int iX, unsigned int FirstVisibleCharIndex, int *piCh, int *piTrailing)
bool BMFontParseFNT (std::istream &Stream)
const CharsetGetFontInfo () const
 NUX_DECLARE_OBJECT_TYPE (BaseObject, Trackable)
bool Reference ()
 Increase reference count.
bool UnReference ()
 Decrease reference count.
virtual bool SinkReference ()
 Mark the object as owned.
virtual bool Dispose ()
 Destroy and object that has a floating reference.
int GetReferenceCount () const
 Get the reference count of this object.
std::string GetAllocationLoation () const
 NUX_DECLARE_ROOT_OBJECT_TYPE (Trackable)
bool OwnsTheReference ()
 Test if object reference is owned.
bool IsHeapAllocated ()
 Test if object was allocated dynamically.
bool IsDynamic () const
 Test if object was allocated dynamically.
virtual int GetObjectSize ()
 Return the size of allocated for this object.
bool SetProperty (std::string const &name, const char *value)
 If the property was not able to be set with the value, the method returns false.
template<typename T >
bool SetProperty (std::string const &name, T const &value)
template<typename T >
GetProperty (std::string const &name, T *foo=0)
void AddProperty (std::string const &name, PropertyBase *property)

Static Public Member Functions

static std::new_handler set_new_handler (std::new_handler handler)
static void * operator new (size_t size)
static void * operator new (size_t size, void *ptr)
static void operator delete (void *ptr)

Public Attributes

std::vector< BaseTexture * > TextureArray
sigc::signal< void, Object * > object_destroyed
 Signal emitted immediately before the object is destroyed.

Protected Member Functions

void SetOwnedReference (bool b)

Protected Attributes

int _heap_allocated

Private Attributes

INT _RefCount
INT _textureBMF
std::vector< unsigned int > m_gl_texture_id
Charset m_Charset

Friends

class FontRenderer

Detailed Description

Definition at line 176 of file FontTexture.h.


Constructor & Destructor Documentation

nux::FontTexture::FontTexture ( const char *  FontFile,
NUX_FILE_LINE_PROTO   
)
nux::FontTexture::FontTexture ( INT  width,
INT  height,
BYTE Texture 
)

Definition at line 53 of file FontTexture.cpp.

  {
    std::vector<BaseTexture*>::iterator it;
    for (it = TextureArray.begin(); it != TextureArray.end(); it++)
    {
      (*it)->UnReference();
    }
    TextureArray.clear();
  }

Member Function Documentation

void nux::Introspectable::AddProperty ( std::string const &  name,
PropertyBase property 
) [inline, inherited]

Definition at line 254 of file Property-inl.h.

{
  // check to see if it exists and if it does barf horribly as we can't
  // have two properties with the same name;
  properties_[name] = property;
}

Here is the caller graph for this function:

bool nux::FontTexture::BMFontParseFNT ( std::istream &  Stream)

Definition at line 139 of file FontTexture.cpp.

  {
    std::string Line;
    int KerningIndex = 0;

    while ( !Stream.eof())
    {
      std::getline( Stream, Line );

      unsigned int line_size = (unsigned int) Line.length();
      char *tc = new char[line_size+1];
      const char *stream = tc;
      Memcpy(tc, Line.c_str(), line_size + 1);
      tc[line_size] = 0;

      if ( ParseCommand(&stream, "common") /*Read == "common"*/)
      {
        Parse_bool(tc, "Bold=",        m_Charset.bold);
        Parse_bool(tc, "Italic=",      m_Charset.italic);
        Parse_u16(tc, "base=",        m_Charset.Base);
        Parse_u16(tc, "scaleW=",      m_Charset.Width);
        Parse_u16(tc, "scaleH=",      m_Charset.Height);
        Parse_u16(tc, "NumPages=",    m_Charset.Pages);
        Parse_u16(tc, "FontHeight=",  m_Charset.FontHeight);
        Parse_u16(tc, "Ascent=",      m_Charset.Ascent);
        Parse_u16(tc, "Descent=",     m_Charset.Descent);
        Parse_int(tc, "AvgCharWidth=",     m_Charset.AvgCharWidth);
        Parse_int(tc, "MaxCharWidth=",     m_Charset.MaxCharWidth);
        Parse_int(tc, "InternalLeading=",     m_Charset.InternalLeading);
        Parse_int(tc, "ExternalLeading=",     m_Charset.ExternalLeading);
        // Constant for now... Should be read from the font file
        m_Charset.NumChar = 256;
      }
      else if (ParseCommand(&stream, "char"))
      {

        unsigned short CharID = 0;

        Parse_u16(tc, "id=", CharID);
        Parse_u16(tc, "x=", m_Charset.Chars[CharID].x);
        Parse_u16(tc, "y=", m_Charset.Chars[CharID].y);
        Parse_u16(tc, "width=", m_Charset.Chars[CharID].Width);
        Parse_u16(tc, "height=", m_Charset.Chars[CharID].Height);
        Parse_s16(tc, "xoffset=", m_Charset.Chars[CharID].XOffset);
        Parse_s16(tc, "yoffset=", m_Charset.Chars[CharID].YOffset);
        Parse_s16(tc, "xadvance=", m_Charset.Chars[CharID].XAdvance);
        Parse_s16(tc, "abcA=", m_Charset.Chars[CharID].abcA);
        Parse_s16(tc, "abcB=", m_Charset.Chars[CharID].abcB);
        Parse_s16(tc, "abcC=", m_Charset.Chars[CharID].abcC);
        Parse_u16(tc, "page=", m_Charset.Chars[CharID].page);
      }
      else if ( ParseCommand(&stream, "Kerning"))
      {
        Parse_u16(tc, "count=", m_Charset.NumKerningPairs);

        if (m_Charset.NumKerningPairs > 0)
          m_Charset.Kerning = new KerningPair[m_Charset.NumKerningPairs];
      }
      else if ( ParseCommand(&stream, "KerningPair"))
      {
        if (KerningIndex < m_Charset.NumKerningPairs)
        {
          Parse_u16(tc, "first=", m_Charset.Kerning[KerningIndex].first);
          Parse_u16(tc, "second=", m_Charset.Kerning[KerningIndex].second);
          Parse_s16(tc, "amount=", m_Charset.Kerning[KerningIndex].amount);
          KerningIndex++;
        }
      }
      else if ( ParseCommand(&stream, "Texture"))
      {
        char texture[256];

        if (ParseLine(&stream, texture, 256))
        {
//                 FilePath FontPath;
//                 FontPath.AddSearchPath(""); // for case where fully qualified path is given
//                 FontPath.AddSearchPath(".");
//                 FontPath.AddSearchPath("../Fonts");

#ifdef UNICODE
          NString font_texture_file = GNuxGraphicsResources.FindResourceLocation(texture);
#else
          NString font_texture_file = GNuxGraphicsResources.FindResourceLocation(texture);
#endif

#ifdef NUX_OPENGLES_20
          Texture2D *Texture = new Texture2D(NUX_TRACKER_LOCATION);
#else
          TextureRectangle *Texture = new TextureRectangle(NUX_TRACKER_LOCATION);
#endif

          NBitmapData* bitmap_data = LoadImageFile(font_texture_file.GetTCharPtr());

          if (bitmap_data)
            Texture->Update(bitmap_data, false);

          delete bitmap_data;
          TextureArray.push_back(Texture);
        }
      }

      delete [] tc;
    }

    return true;
  }

Here is the call graph for this function:

bool nux::FontTexture::CursorPosToX ( const NString Str,
int  icp,
bool  fTrailing,
int *  pX 
)

Definition at line 261 of file FontTexture.cpp.

  {
    if (icp > (int) Str.Size())
      return false;

    if (fTrailing)
      // get pX at the right of the character at position icp
      *pX = GetStringWidth(Str, icp + 1);
    else
      // get pX at the left of the character at position icp
      *pX = GetStringWidth(Str, icp);

    return true;
  }

Here is the call graph for this function:

bool nux::Object::Dispose ( ) [virtual, inherited]

Destroy and object that has a floating reference.

Returns:
True is the object has been destroyed

Reimplemented from nux::Trackable.

Definition at line 311 of file Object.cpp.

  {
    // The intent of the Dispose call is to destroy objects with a float
    // reference (reference count is equal to 1 and the '_owns_the_reference'
    // flag is set to false). In Nux, only widgets object can have a floating
    // reference.  And widgets are only visible if added to the widget tree.
    // When an object with a floating reference is added to the widget tree,
    // it becomes "owned'. It looses it floating reference status but it still
    // has a reference count number of 1.  In practice, since widgets must be
    // added to the widget tree, there should never be a need to call Dispose
    // (except in a few cases).

    // Dispose() was designed to only destroy objects with floating
    // references, while UnReference() destroys objects that are "owned".
    // That is now relaxed. Dispose() calls UnReference().
    return UnReference();
  }

Here is the call graph for this function:

Here is the caller graph for this function:

std::string nux::Object::GetAllocationLoation ( ) const [inherited]

Definition at line 348 of file Object.cpp.

{
  std::ostringstream sout;
  sout << allocation_file_name_ << ":" << allocation_line_number_;
  return sout.str();
}

Here is the caller graph for this function:

int nux::FontTexture::GetCharStringWidth ( const char *  str) const

Definition at line 86 of file FontTexture.cpp.

  {
    if ((str == 0) || (NString(str) == NString("")))
      return 0;

    unsigned int total = 0;

    for (int i = 0; ; ++i)
    {
      if (str[i] == 0)
        return total;

      total += GetCharWidth(str[i]);
    }

    return total;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

int nux::FontTexture::GetCharStringWidth ( const char *  str,
int  num_char_to_compute 
) const

Definition at line 109 of file FontTexture.cpp.

  {
    if ((str == 0) || (NString(str) == NString("")))
      return 0;

    int num_chars = num_char_to_compute;

    if (num_chars <= 0)
    {
      return 0;
    }

    int total = 0;

    for (int i = 0; i < num_chars; ++i)
    {
      if (str[i] == 0)
        return total;

      total += GetCharWidth(str[i]);
    }

    return total;
  }

Here is the call graph for this function:

int nux::FontTexture::GetCharWidth ( const char &  c) const

Definition at line 63 of file FontTexture.cpp.

  {
    int ascii = c & 0xff;
    nuxAssert(ascii < m_Charset.NumChar);

    if (ascii >= m_Charset.NumChar)
      return 0;

    // XAdvance = abcA + abcB + abcC
    return m_Charset.Chars[ascii].XAdvance;
  }

Here is the caller graph for this function:

Definition at line 134 of file FontTexture.cpp.

  {
    return m_Charset.FontHeight;
  }

Here is the caller graph for this function:

Definition at line 359 of file FontTexture.cpp.

  {
    return m_Charset;
  }
int nux::FontTexture::GetLineHeight ( ) const [inline]

Definition at line 186 of file FontTexture.h.

    {
      return m_Charset.FontHeight;
    }

Here is the caller graph for this function:

int nux::Trackable::GetObjectSize ( ) [virtual, inherited]

Return the size of allocated for this object.

Returns:
The size allocated for this object.

Definition at line 217 of file Object.cpp.

  {
    return _size_of_this_object;
  }
template<typename T >
T nux::Introspectable::GetProperty ( std::string const &  name,
T *  foo = 0 
) [inherited]

Definition at line 285 of file Property-inl.h.

{
  PropertyContainer::iterator i = properties_.find(name);
  if (i == properties_.end())
    return T();

  std::string s = i->second->GetSerializedValue();
  std::pair<T, bool> result = type::PropertyTrait<T>::from_string(s);
  // If this is called with a template type that the property does not
  // support nice conversion to, you'll get no error, but will get
  // a default constructed T.  We could use an exception here.
  return result.first;
}
int nux::Object::GetReferenceCount ( ) const [inherited]

Get the reference count of this object.

Returns:
The reference count of this object.

Definition at line 343 of file Object.cpp.

  {
    return reference_count_->GetValue();
  }

Here is the call graph for this function:

Here is the caller graph for this function:

int nux::FontTexture::GetStringWidth ( const NString str) const

Definition at line 75 of file FontTexture.cpp.

  {
//     unsigned int total = 0;
//     for (unsigned int i = 0; i != (unsigned int)str.size(); ++i)
//     {
//         total += GetCharWidth(str[i]);
//     }
//     return total;
    return GetCharStringWidth(str.GetTCharPtr());
  }

Here is the call graph for this function:

Here is the caller graph for this function:

int nux::FontTexture::GetStringWidth ( const NString str,
int  num_char_to_compute 
) const

Definition at line 104 of file FontTexture.cpp.

  {
    return GetCharStringWidth(str.GetTCharPtr(), num_char_to_compute);
  }

Here is the call graph for this function:

bool nux::Trackable::IsDynamic ( ) const [inherited]

Test if object was allocated dynamically.

Definition at line 201 of file Object.cpp.

  {
    // Get pointer to beginning of the memory occupied by this.
    const void *ptr = dynamic_cast<const void *> (this);

    // Search for ptr in allocation_list
#if defined(NUX_OS_WINDOWS) && !defined(NUX_VISUAL_STUDIO_2010)
    std::list<void*>::iterator i = std::find(GObjectStats._allocation_list.begin(),
      GObjectStats._allocation_list.end(), ptr);
#else
    auto i = std::find(GObjectStats._allocation_list.begin(),
                       GObjectStats._allocation_list.end(), ptr);
#endif
    return i != GObjectStats._allocation_list.end();
  }

Here is the caller graph for this function:

bool nux::Trackable::IsHeapAllocated ( ) [inherited]

Test if object was allocated dynamically.

Definition at line 191 of file Object.cpp.

  {
    if (_heap_allocated == -1)
    {
      _heap_allocated = (IsDynamic () ? 1 : 0);
    }

    return (_heap_allocated == 1 ? true : false);
  }

Here is the call graph for this function:

Here is the caller graph for this function:

nux::Object::NUX_DECLARE_OBJECT_TYPE ( BaseObject  ,
Trackable   
) [inherited]
void nux::Trackable::operator delete ( void *  ptr) [static, inherited]

Definition at line 178 of file Object.cpp.

  {
    ObjectStats::AllocationList::iterator i = std::find (GObjectStats._allocation_list.begin(), GObjectStats._allocation_list.end(), ptr);

    if (i != GObjectStats._allocation_list.end() )
    {
      GObjectStats._total_allocated_size -= NUX_STATIC_CAST (Trackable *, ptr)->_size_of_this_object;
      --GObjectStats._number_of_objects;
      GObjectStats._allocation_list.erase (i);
      ::operator delete (ptr);
    }
  }
void * nux::Trackable::operator new ( size_t  size) [static, inherited]

Definition at line 140 of file Object.cpp.

  {
    // Set the new_handler for this call
    std::new_handler global_handler  = std::set_new_handler (_new_current_handler);

    // If allocation fails _new_current_handler is called, if specified,
    // otherwise the global new_handler is called.
    void *ptr;

    try
    {
      ptr = ::operator new (size);

      GObjectStats._allocation_list.push_front (ptr);
      NUX_STATIC_CAST (Trackable *, ptr)->_size_of_this_object = size;
      GObjectStats._total_allocated_size += size;
      ++GObjectStats._number_of_objects;
    }
    catch (std::bad_alloc &)
    {
      std::set_new_handler (global_handler);
      throw;
    }

    //  Reset gloabal new_handler
    std::set_new_handler (global_handler);
    return ptr;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

void * nux::Trackable::operator new ( size_t  size,
void *  ptr 
) [static, inherited]

Definition at line 171 of file Object.cpp.

  {
    return ::operator new (size, ptr);
  }

Here is the call graph for this function:

bool nux::Trackable::OwnsTheReference ( ) [inherited]

Test if object reference is owned.

Definition at line 117 of file Object.cpp.

  {
    return _owns_the_reference;
  }

Here is the caller graph for this function:

bool nux::Object::Reference ( ) [virtual, inherited]

Increase reference count.

Returns:
True if the object has successfully referenced.

Reimplemented from nux::Trackable.

Definition at line 254 of file Object.cpp.

  {
    if (!IsHeapAllocated())
    {
      LOG_WARN(logger) << "Trying to reference an object that was not heap allocated."
                       << "\nObject allocated at: " << GetAllocationLoation() << "\n";
      return false;
    }

    if (!OwnsTheReference())
    {
      SetOwnedReference(true);
      // The ref count remains at 1. Exit the method.
      return true;
    }

    reference_count_->Increment();
    return true;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

std::new_handler nux::Trackable::set_new_handler ( std::new_handler  handler) [static, inherited]

Definition at line 133 of file Object.cpp.

  {
    std::new_handler old_handler = _new_current_handler;
    _new_current_handler = handler;
    return old_handler;
  }

Here is the caller graph for this function:

void nux::Trackable::SetOwnedReference ( bool  b) [protected, inherited]

Definition at line 122 of file Object.cpp.

  {
    if (_owns_the_reference == true)
    {
      LOG_DEBUG(logger) << "Do not change the ownership if is already set to true!" << "\n";
      return;
    }

    _owns_the_reference = b;
  }

Here is the caller graph for this function:

bool nux::Introspectable::SetProperty ( std::string const &  name,
const char *  value 
) [inline, inherited]

If the property was not able to be set with the value, the method returns false.

Definition at line 262 of file Property-inl.h.

{
  PropertyContainer::iterator i = properties_.find(name);
  if (i == properties_.end())
    return false;
  else
    return i->second->SetValue(value);
}
template<typename T >
bool nux::Introspectable::SetProperty ( std::string const &  name,
T const &  value 
) [inherited]

Definition at line 273 of file Property-inl.h.

{
  PropertyContainer::iterator i = properties_.find(name);
  if (i == properties_.end())
    return false;
  else
  {
    return i->second->SetValue(type::PropertyTrait<T>::to_string(value));
  }
}
bool nux::Object::SinkReference ( ) [virtual, inherited]

Mark the object as owned.

If this object is not owned, calling SinkReference() as the same effect as calling Reference().

Returns:
True if the object was not owned previously

Reimplemented from nux::Trackable.

Definition at line 306 of file Object.cpp.

  {
    return Reference();
  }

Here is the call graph for this function:

Here is the caller graph for this function:

bool nux::Object::UnReference ( ) [virtual, inherited]

Decrease reference count.

Returns:
True if the object reference count has reached 0 and the object has been destroyed.

Reimplemented from nux::Trackable.

Definition at line 274 of file Object.cpp.

  {
    if (!IsHeapAllocated())
    {
      LOG_WARN(logger) << "Trying to un-reference an object that was not heap allocated."
                       << "\nObject allocated at: " << GetAllocationLoation() << "\n";
      return false;
    }

    if (objectptr_count_->GetValue() == reference_count_->GetValue())
    {
      // There are ObjectPtr's hosting this object. Release all of them to
      // destroy this object.  This prevent from calling UnReference () many
      // times and destroying the object when there are ObjectPtr's hosting
      // it.  This method should not be called directly in that case.
      LOG_WARN(logger) << "There are ObjectPtr hosting this object. "
                       << "Release all of them to destroy this object. "
                       << "\nObject allocated at: " << GetAllocationLoation() << "\n";
      return false;
    }

    reference_count_->Decrement();

    if (reference_count_->GetValue() == 0)
    {
      Destroy();
      return true;
    }

    return false;
  }

Here is the call graph for this function:

bool nux::FontTexture::XToCursorPosition ( const NString Str,
int  iX,
unsigned int  FirstVisibleCharIndex,
int *  piCh,
int *  piTrailing 
)

Definition at line 296 of file FontTexture.cpp.

  {
    unsigned int num_chars;
    num_chars = (unsigned int) Str.Size();
    nuxAssert(FirstVisibleCharIndex < num_chars);

    *piCh = 0;
    *piTrailing = 0;

    unsigned int total = 0;

    if (iX == 0)
    {
      *piCh = 0;
      *piTrailing = 0;
      return true;
    }


    unsigned int X = iX;

    for (unsigned int i = 0; i < FirstVisibleCharIndex; ++i)
    {
      X += GetCharWidth(Str[i]);
    }

    for (unsigned int i = 0; i < num_chars; ++i)
    {
      unsigned int s = GetCharWidth(Str[i]);

      if (i >= FirstVisibleCharIndex)
      {
        if (total == X)
        {
          *piCh = i;
          *piTrailing = 0;
          return true;
        }
        else if (total + s / 2 > X)
        {
          *piCh = i;
          *piTrailing = 0;
          return true;
        }

        else if (total + GetCharWidth(Str[i+1]) / 2 > X)
        {
          *piCh = i + 1;
          *piTrailing = 0;
          return true;
        }
      }

      total += s;
    }

    return false;
  }

Here is the call graph for this function:


Friends And Related Function Documentation

friend class FontRenderer [friend]

Definition at line 254 of file FontTexture.h.


Member Data Documentation

int nux::Trackable::_heap_allocated [protected, inherited]

Definition at line 158 of file Object.h.

Definition at line 249 of file FontTexture.h.

Definition at line 250 of file FontTexture.h.

Definition at line 252 of file FontTexture.h.

std::vector<unsigned int> nux::FontTexture::m_gl_texture_id [private]

Definition at line 251 of file FontTexture.h.

sigc::signal<void, Object *> nux::Object::object_destroyed [inherited]

Signal emitted immediately before the object is destroyed.

Definition at line 214 of file Object.h.

Definition at line 246 of file FontTexture.h.


The documentation for this class was generated from the following files: