/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CExcelFont.h  -  description
                             -------------------
    begin                : Mon Jan 19 2004
    copyright            : (C) 2004 by Alexey S.Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

/***************************************************************************
   This library is free software; you can redistribute it and/or modify it 
   under the terms of the GNU Library General Public License as published by
   the Free Software Foundation; either version 2 of the License, or (at 
   your option) any later version.

   This library is distributed in the hope that it will be useful, but 
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
   General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; if not, write to the Free Software Foundation, 
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

   Please report all bugs and problems to "alexeyp@gmail.com"
 ***************************************************************************/

#ifndef __CEXCELFONT_H__
#define __CEXCELFONT_H__

#include <sptk3/excel/CExcelRecord.h>
#include <sptk3/excel/CExcelRecordsBuffer.h>

#include <sptk3/CStrings.h>
#include <map>

#define EFM_ATTR_BOLD      1
#define EFM_ATTR_ITALIC    2
#define EFM_ATTR_UNDERLINE 4
#define EFM_ATTR_STRIKEOUT 8

/// @addtogroup excel Excel Classes
/// @{

/// Excel FONT record
class CExcelFont : public CExcelRecord {
   friend class CExcelFonts;
protected:  
   std::string   m_fontMetrics;  ///< A string that uniquely identifies the font - font metrics
   unsigned  m_fontIndex;    ///< Logical index of the font
protected:  
   /// Protected constructor
   /// @param fontIndex unsigned, logical index of the font
   /// @param fontName const char *, font name
   /// @param fontHeight unsigned, font height in points
   /// @param fontAttr unsigned, font attr (EFM_ATTR_ITALIC,EFM_ATTR_BOLD,EFM_ATTR_UNDERLINE,EFM_ATTR_STRIKEOUT)
   /// @param charSet unsigned, character set as defined in Windows API LOGFONT structure
   CExcelFont(unsigned fontIndex,const char *fontName,unsigned fontHeight,unsigned fontAttr=0,unsigned charSet=0);

   /// Protected constructor
   /// @param fontIndex unsigned, logical index of the font
   /// @param fontDataSize unsigned, the size of the FONT record data
   /// @param fontData const char *, FONT record data in internal Excel format
   CExcelFont(unsigned fontIndex,unsigned fontDataSize,const char *fontData);

public:
   std::string  name() const;     ///< Returns font name
   unsigned height() const;   ///< Returns font height
   unsigned attr() const;     ///< Returns font attributes

   /// Sets font name
   /// @param fname std::string, font name i.e. "Arial"
   void name(std::string fname);

   /// Sets font height
   /// @param fheight unsigned, font height in points
   void height(unsigned fheight);

   /// Sets font attributes
   /// @param fattr unsigned, font attr (EFM_ATTR_ITALIC,EFM_ATTR_STRIKEOUT)
   void attr(unsigned fattr);

   /// Returns a string uniquely identifying font - font metrics
   std::string fontMetrics() const { return m_fontMetrics; }

   /// Returns logical index of the font
   unsigned index() const { return m_fontIndex; }    

   /// Computes the unique string defining this font
   /// @param fontName const char *, font name
   /// @param fontHeight unsigned, font height
   /// @param fontAttr unsigned, font attributes
   /// @param charSet unsigned, the character set
   static std::string computeFontMetrics(const char *fontName,unsigned fontHeight,unsigned fontAttr,unsigned charSet);
};

/// The list of Excel file fonts
class CExcelFonts {
   typedef std::map<std::string,CExcelFont *> CFontNamesMap;
   typedef std::vector<CExcelFont *> CFontList;
   CFontList                  m_fontList;
   CFontNamesMap					m_fontMap;
public:
   /// Default constructor
   CExcelFonts() {}

   /// Creates the minimal set of required fonts
   void init();

   /// Encodes fonts into internal Excel format
   void saveRecords(CExcelRecordsBuffer& records);

   /// Adds a font to the font list, or finds similar existing font
   /// @param fontName const char *, font name
   /// @param fontHeight unsigned, font height in points
   /// @param fontAttr unsigned, font attr (EFM_ATTR_ITALIC,EFM_ATTR_BOLD,EFM_ATTR_UNDERLINE,EFM_ATTR_STRIKEOUT)
   /// @param charSet unsigned, character set as defined in Windows API LOGFONT structure
   /// @returns logical index of the font
   int add(const char *fontName,unsigned fontHeight,unsigned fontAttr=0,unsigned charSet=0);

   /// Adds a font to the font list, even if similar font exists. In the last case the metrics field
   /// is appended with the logical index of the font. That allows to read the Excel files with several
   /// identical FONT records defined
   /// @param fontDataSize unsigned, the size of the FONT record data
   /// @param fontData const char *, FONT record data in internal Excel format
   /// @returns logical index of the new font
   int add(unsigned fontDataSize,const char *fontData);

   /// Clears the list of the existing fonts
   void clear();

   /// Returns the number of fonts in the list
   unsigned size() const { return m_fontList.size(); }

   /// Returns the font by the logical index. 
   /// Index 4 is reserved and shouldn't be used
   CExcelFont& operator [] (unsigned i) { return *m_fontList[i]; }

   /// Returns the font by the logical index (const version). 
   /// Index 4 is reserved and shouldn't be used
   const CExcelFont& operator [] (unsigned i) const { return *m_fontList[i]; }
};

/// @}

#endif
