//-----------------------------------------------------------------------------
///
/// \brief  Monochrom simple font for graphical displays
///
///         Currently for "Newhaven" displays
///
/// \date   20151226
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//-----------------------------------------------------------------------------


//---Includes------------------------------------------------------------------


//---General--------------------------


//---Own------------------------------

#include <biwak/font.h>


//---Implementation------------------------------------------------------------

CFont::CFont()
{
   m_pFont = nullptr;
}


int CFont::textWidth(const char *text)
{
   int pos=0;
   int xSize=0;

   const TMatrix *character;

   while ( text[pos] )
   {
      character=findCharacter(text[pos]);
      xSize+=m_pFont->spacing;
      if(character)
      {
         xSize += character->xSize;
      }
      else
         xSize += m_pFont->baseXSize;
      pos++;
   }

   if(xSize)
      xSize--;

   return(xSize);
}


void CFont::setFont(const TFont &font)
{
   m_pFont=&font;
}


/// \brief  Finds the matrix for a character in our font
///
/// \return Pointer to matrix or NULL if not found
const TMatrix *CFont::findCharacter(const char ch)
{
   const TMatrix *matrix=nullptr;

   for(int i1=0; i1<m_pFont->letters; i1++)
   {
      if(m_pFont->matrix[i1].letter==ch)
      {
         matrix=&m_pFont->matrix[i1];
         return(matrix);
         break;
      }
   };
   return(matrix);
}


//---fin-----------------------------------------------------------------------
