int GUIFontManager::getCharacterWidth(char Char, int fontIndex)
{
  GUIFont *currentFont = getFont(fontIndex);
  currentFont          = currentFont ? currentFont : getDefaultFont();
  if(currentFont)
    return  currentFont->getFontObject()->getCharHorizontalGlyphs()[Char];  

  return 0;
}
int GUIFontManager::getCharacterWidth(char Char, GUIFont *font)
{
  GUIFont *currentFont = getDefaultFont();
  if(!font)
  {
    if(currentFont)
      return  currentFont->getFontObject()->getCharHorizontalGlyphs()[Char];  
  }
  else
    return font->getFontObject()->getCharHorizontalGlyphs()[Char];
  return 0;
}
void GUIText::computeDimensions()
{
  if (needUpdating())
  {
    GUIFontManager::setCurrentFont(fontIndex);
    GUIFont *currentFont = GUIFontManager::getCurrentFont();
    
    if (!currentFont && !(currentFont = GUIFontManager::getDefaultFont()))
      return;
      
    if (currentFont == GUIFontManager::getDefaultFont())
      fontIndex = GUIFontManager::findFontIndex(currentFont);
      
    if (text.getLength())
    {
      size = currentFont->getFontObject()->getStringDimensions(text);
      size.x = int(float(size.x)*scales.x);
    }
    size.y = int(float(currentFont->getFontObject()->getHeight())*scales.y);
    forceUpdate(false);
  }
}
void  GUIText::print(int x, int y, int startIndex, int endIndex)
{
  if (!text.getLength())
    return;
    
  endIndex   = (endIndex  < 0) ? int(text.getLength()) : endIndex;
  startIndex = clamp(startIndex, 0, endIndex);
  
  GUIFontManager::setCurrentFont(fontIndex);
  GUIFont *currentFont = GUIFontManager::getCurrentFont();
  computeDimensions();
  
  if (!currentFont && !(currentFont = GUIFontManager::getDefaultFont()))
    return;
    
  currentFont->getFontObject()->printSubString(float(x), float(y), scales.x, scales.y,
      color.x, color.y, color.z,
      startIndex, endIndex,
      text);
}
void    GUITextBox::setupBlinker(int x)
{
  if (!focused || !active)
    return;
    
  GUIFont *font           = GUIFontManager::getFont(label.getFontIndex());
  const    String &string = label.getString();
  const    int    *spaces = NULL;
  blinkerPosition         = getWindowBounds().x + padding.x;
  x -= 1;
  
  if (font)
  {
    spaces = font->getFontObject()->getCharHorizontalGlyphs();
    
    for (size_t i = 0; i < string.getLength(); i++)
      if (blinkerPosition < x)
        blinkerPosition += spaces[int(string.getBytes()[i])];
  }
  
  blinkerOn = true;
}
void GUITextBox::setupText(int type, char Char)
{
  GUIFont *font           = GUIFontManager::getFont(label.getFontIndex());
  const    int    *spaces = font ? font->getFontObject()->getCharHorizontalGlyphs() : NULL,
                            length = label.getString().getLength();
  String   temp;
  int      start  = windowBounds.x + padding.x,
                    index  = 0;
                    
  if (!spaces)
    return;
    
  for (int t = 0; t < length; t++)
  {
    if (blinkerPosition > start)
    {
      index++;
      start += spaces[label.getCharString()[t]];
    }
  }
  
  if (type == INSERT_CHAR)
  {
    if (index != length && length)
    {
      String leftSide;
      leftSide.set(label.getCharString(), index);
      leftSide += Char;
      temp.set(label.getCharString() + index, length - index);
      label.setString(leftSide + temp);
    }
    else
    {
      temp  = label.getString();
      temp += Char;
      label.setString(temp);
    }
    blinkerPosition  = blinkerPosition + GUIFontManager::getCharacterWidth(Char, font);
  }
  
  if (type == BACKSPACE_DELETE && (blinkerPosition != windowBounds.x + padding.x))
  {
    if (index != length)
    {
      String leftSide;
      setupBlinker(blinkerPosition - GUIFontManager::getCharacterWidth(label.getCharString()[index -1],
                   label.getFontIndex()));
                   
      leftSide.set(label.getCharString(), index - 1);
      temp.set(label.getCharString() + index, length - index);
      label.setString(leftSide + temp);
      return;
    }
    
    setupBlinker(blinkerPosition - GUIFontManager::getCharacterWidth(label.getCharString()[length -1],
                 font));
                 
    temp.set(label.getCharString(), length - 1);
    if (temp.getLength())
      label.setString(temp);
    else
    {
      label.clear();
      blinkerPosition = windowBounds.x + padding.x;
    }
  }
  
  if (type == SIMPLE_DELETE && length)
  {
    if ((blinkerPosition == windowBounds.x + padding.x) && (length == 1))
    {
      label.clear();
      return;
    }
    
    if (index < length)
    {
      String leftSide;
      leftSide.set(label.getCharString(), index);
      temp.set(label.getCharString() + index + 1, length - index - 1);
      label.setString(leftSide + temp);
    }
  }
  if (type == PARSE_VISIBLE)
    textEndIndex = font->getFontObject()->getMaxFittingLength(label.getString(), getWidth());
}