/*-----------------------------------------------------------------------------
[名称]	LogFont.cpp
[概要]	フォント情報クラスの実装
[備考]	なし
[履歴]	2002-12-12	NC		コメント付加(moralog)
-----------------------------------------------------------------------------*/


//-----------------------------------------------------------------------------
// インクルード
//-----------------------------------------------------------------------------
#include "stdafx.h"
#include "LogFont.h"
#include "Common.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


/*-----------------------------------------------------------------------------
[名称]	コンストラクタ
[機能]	インスタンスの生成。
[戻値]	なし
[備考]	なし
-----------------------------------------------------------------------------*/
CLogFont::CLogFont()
:	m_iPointSize( 12 )
,	m_strFontName( "ＭＳ Ｐゴシック" )
,	m_bBold( FALSE )
,	m_bItalic( FALSE )
,	m_bUnderLine( FALSE )
,	m_bStrikeOut( FALSE )
,	m_nCharSet( SHIFTJIS_CHARSET )
,	m_bFontCreate( FALSE )
{
}


/*-----------------------------------------------------------------------------
[名称]	デストラクタ
[機能]	インスタンスの破棄。
[戻値]	なし
[備考]	なし
-----------------------------------------------------------------------------*/
CLogFont::~CLogFont()
{
	if ( m_bFontCreate )
	{
		m_fonText.DeleteObject( );
	}
}


/*-----------------------------------------------------------------------------
[名称]	LOGFONT構造体取得
[機能]	設定された情報からLOGFONT構造体を作成し、ポインタを戻す。
[戻値]	LOGFONT* LOGFONT構造体 [-] (-)
[備考]	戻されるポインタは一時的なもの。
-----------------------------------------------------------------------------*/
LOGFONT*	CLogFont::GetLogFont(
	CDC*	pDC		// I: 対象DC [-] (-) :-
)
{
	memset( &m_tLogFont, 0x00, sizeof(LOGFONT) );

	m_tLogFont.lfHeight = -MulDiv(m_iPointSize, pDC->GetDeviceCaps(LOGPIXELSY), 72);

	memcpy( m_tLogFont.lfFaceName, (LPCTSTR)m_strFontName, m_strFontName.GetLength()+1  );

	if ( m_bBold )
	{
		m_tLogFont.lfWeight = FW_BOLD;
	}
	else
	{
		m_tLogFont.lfWeight = FW_REGULAR;
	}

	if ( m_bItalic )
	{
		m_tLogFont.lfItalic = TRUE;
	}
	else
	{
		m_tLogFont.lfItalic = FALSE;
	}

	if ( m_bUnderLine )
	{
		m_tLogFont.lfUnderline = TRUE;
	}
	else
	{
		m_tLogFont.lfUnderline = FALSE;
	}

	if ( m_bStrikeOut )
	{
		m_tLogFont.lfStrikeOut = TRUE;
	}
	else
	{
		m_tLogFont.lfStrikeOut = FALSE;
	}

	m_tLogFont.lfCharSet = m_nCharSet;

	return( &m_tLogFont );
}


/*-----------------------------------------------------------------------------
[名称]	CFont取得
[機能]	設定された情報からCFontを作成し、ポインタを戻す。
[戻値]	CFont* CFontポインタ [-] (-)
[備考]	戻されるポインタは一時的なもの。
-----------------------------------------------------------------------------*/
CFont*	CLogFont::GetFont(
	CDC*	pDC		// I: 対象DC [-] (-) :-
)
{
	if ( m_bFontCreate )
	{
		m_fonText.DeleteObject( );
	}
	m_fonText.CreateFontIndirect( GetLogFont( pDC ) );
	m_bFontCreate = TRUE;

	return( &m_fonText );
}


/*-----------------------------------------------------------------------------
[名称]	ファイル書き出し
[機能]	設定ファイルへの書き出し。
[戻値]	BOOL 処理結果 [-] (TRUE:成功、FALSE:失敗)
[備考]	Ini形式。
-----------------------------------------------------------------------------*/
BOOL	CLogFont::Write(
	CString&		strFilename,
	const char*		strSection
)
{
	BOOL	bSuccess = TRUE;
	bSuccess &= WriteProfile( strFilename, strSection, "iPointSize",	LongToString( m_iPointSize ) );
	bSuccess &= WriteProfile( strFilename, strSection, "strFontName",	QuoteString( m_strFontName ) );
	bSuccess &= WriteProfile( strFilename, strSection, "strFontStyle",	QuoteString( m_strFontStyle ) );
	bSuccess &= WriteProfile( strFilename, strSection, "bBold",			BoolToString( m_bBold ) );
	bSuccess &= WriteProfile( strFilename, strSection, "bItalic",		BoolToString( m_bItalic ) );
	bSuccess &= WriteProfile( strFilename, strSection, "bUnderLine",	BoolToString( m_bUnderLine ) );
	bSuccess &= WriteProfile( strFilename, strSection, "bStrikeOut",	BoolToString( m_bStrikeOut ) );
	bSuccess &= WriteProfile( strFilename, strSection, "nCharSet",		LongToString( m_nCharSet ) );

	return( bSuccess );
}


/*-----------------------------------------------------------------------------
[名称]	ファイル読み込み
[機能]	設定ファイルからの読み込み。
[戻値]	BOOL 処理結果 [-] (TRUE:成功、FALSE:失敗)
[備考]	Ini形式。
-----------------------------------------------------------------------------*/
BOOL	CLogFont::Read(
	CString&		strFilename,
	const char*		strSection
)
{
	BOOL		bSuccess;
	CString		strValue;

	bSuccess = ReadProfile( strFilename, strSection, "iPointSize", &strValue );
	if ( bSuccess ){ m_iPointSize = StringToLong( strValue ); }

	bSuccess = ReadProfile( strFilename, strSection, "strFontName", &strValue );
	if ( bSuccess ){ m_strFontName = DeQuoteString( strValue ); }

	bSuccess = ReadProfile( strFilename, strSection, "strFontStyle", &strValue );
	if ( bSuccess ){ m_strFontStyle = DeQuoteString( strValue ); }

	bSuccess = ReadProfile( strFilename, strSection, "bBold", &strValue );
	if ( bSuccess ){ m_bBold = StringToBool( strValue ); }

	bSuccess = ReadProfile( strFilename, strSection, "bItalic", &strValue );
	if ( bSuccess ){ m_bItalic = StringToBool( strValue ); }

	bSuccess = ReadProfile( strFilename, strSection, "bUnderLine", &strValue );
	if ( bSuccess ){ m_bUnderLine = StringToBool( strValue ); }

	bSuccess = ReadProfile( strFilename, strSection, "bStrikeOut", &strValue );
	if ( bSuccess ){ m_bStrikeOut = StringToBool( strValue ); }

	bSuccess = ReadProfile( strFilename, strSection, "nCharSet", &strValue );
	if ( bSuccess ){ m_nCharSet = (char)StringToLong( strValue ); }

	return( TRUE );
}


/*-----------------------------------------------------------------------------
[名称]	値複製
[機能]	属性値のコピー。
[戻値]	BOOL 処理結果 [-] (TRUE:成功、FALSE:失敗)
[備考]	なし
-----------------------------------------------------------------------------*/
BOOL CLogFont::Copy(
	const CLogFont&	fontSrc
)
{
	m_iPointSize = fontSrc.m_iPointSize;
	m_strFontName =	fontSrc.m_strFontName;
	m_strFontStyle = fontSrc.m_strFontStyle;
	m_bBold = fontSrc.m_bBold;
	m_bItalic =	fontSrc.m_bItalic;
	m_bUnderLine = fontSrc.m_bUnderLine;
	m_bStrikeOut = fontSrc.m_bStrikeOut;
	m_nCharSet = fontSrc.m_nCharSet;

	return( TRUE );
}