// EIKFNTUT.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#include <badesca.h>
#include <gdi.h>
#include <eikfntut.h>
#include <eikftflg.hrh>

#if defined(_UNICODE)
#include <eikenv.h>
#include <barsread.h>
#include <eikpriv.rsg>
#endif

enum { EMinFontHeight=4 };

EXPORT_C void EikFontUtils::GetAvailableFontsL(CGraphicsDevice& aDevice,CDesCArray& aFontNameList,TInt aFonts)
	{ // static
	aFontNameList.Reset();
	const TInt numTypefaces=aDevice.NumTypefaces();
	TTypefaceSupport typefaceInfo;
	for (TInt ii=0;ii<numTypefaces;ii++)
		{
		aDevice.TypefaceSupport(typefaceInfo,ii);
		if (typefaceInfo.iTypeface.IsProportional())
			{
			if (aFonts==EEikMonospaceFontsOnly)
				continue;
			}
		else if (aFonts==EEikNoMonospaceFonts || aFonts==EEikNoMonospaceOrSymbolFonts)
			continue;
		if (typefaceInfo.iTypeface.IsSymbol())
			{
			if (aFonts==EEikNoSymbolFonts || aFonts==EEikNoMonospaceOrSymbolFonts || aFonts==EEikMonospaceFontsOnly)
				continue;
			}
		else if (aFonts==EEikSymbolFontsOnly)
			continue;
		aFontNameList.AppendL(typefaceInfo.iTypeface.iName);
		}
#if defined(_UNICODE)
	TBuf<KMaxTypefaceNameLength> typeface;
	CEikonEnv* eikEnv=CEikonEnv::Static();
	eikEnv->ReadResource(typeface,R_EIK_DEFAULT_CHAR_FORMAT_TYPEFACE);
	aFontNameList.AppendL(typeface);
#endif
	}

EXPORT_C TInt EikFontUtils::TypefaceAttributes(CGraphicsDevice& aDevice,const TDesC& aTypefaceName)
	{
	const TInt numTypefaces=aDevice.NumTypefaces();
	TInt fontIndex;
	for (fontIndex=0;fontIndex<numTypefaces;fontIndex++)
		{
    	TTypefaceSupport typefaceInfo;
		aDevice.TypefaceSupport(typefaceInfo,fontIndex);
		if (typefaceInfo.iTypeface.iName==aTypefaceName)
			return(typefaceInfo.iTypeface.Attributes());
		}	
	return(0);
	}

EXPORT_C TInt EikFontUtils::GetAvailableHeightsInTwipsL(CGraphicsDevice& aDevice,const TDesC& aTypefaceName,CArrayFix<TInt>& aHeightList)
	{ // static
	aHeightList.Reset();
	const TInt numTypefaces=aDevice.NumTypefaces();
	TInt fontIndex;
	for (fontIndex=0;fontIndex<numTypefaces;fontIndex++)
		{
    	TTypefaceSupport typefaceInfo;
		aDevice.TypefaceSupport(typefaceInfo,fontIndex);
		if (typefaceInfo.iTypeface.iName==aTypefaceName)
			break;
		}
#if defined(_UNICODE)
	TBuf<KMaxTypefaceNameLength> typeface;
	CEikonEnv* eikEnv=CEikonEnv::Static();
	eikEnv->ReadResource(typeface,R_EIK_DEFAULT_CHAR_FORMAT_TYPEFACE);

	if (aTypefaceName.CompareF(typeface)==0)
		{
		TResourceReader reader;
		eikEnv->CreateResourceReaderLC(reader,R_EIK_DEFAULT_CHAR_FORMAT_HEIGHT);
		TInt height=reader.ReadInt16();
		CleanupStack::PopAndDestroy(); // reader
		aHeightList.AppendL(height);
		return KErrNone;
		}
#endif
	if (fontIndex>=numTypefaces)
		return KErrNotSupported;
	TTypefaceSupport typefaceInfo;
	aDevice.TypefaceSupport(typefaceInfo,fontIndex);
	const TInt numHeights=typefaceInfo.iNumHeights;
	for (TInt ii=0;ii<numHeights;ii++)
		{
		const TInt height=aDevice.FontHeightInTwips(fontIndex,ii);
		if (PointsFromTwips(height)>=EMinFontHeight)
			aHeightList.AppendL(height);
		}
	return KErrNone;
	}

EXPORT_C TInt EikFontUtils::GetAvailableHeightsInTwipsAndPointsL(CGraphicsDevice& aDevice,const TDesC& aTypefaceName,CArrayFix<TInt>& aTwipsList,CDesCArray& aPointsList)
	{ // static
	aTwipsList.Reset();
	aPointsList.Reset();
	TInt err=GetAvailableHeightsInTwipsL(aDevice,aTypefaceName,aTwipsList);
	if (err==KErrNotSupported)
		return err;
	const TInt count=aTwipsList.Count();
	for (TInt ii=0;ii<count;ii++)
		{
		const TInt points=PointsFromTwips(aTwipsList[ii]);
		if (points<EMinFontHeight)
			continue;
		TBuf<8> num;
		num.Num(points);
		aPointsList.AppendL(num);
		}
	return KErrNone;
	}

EXPORT_C TInt EikFontUtils::PointsFromTwips(TInt aTwips)
	{ // static
	//one point=20 twips
	return (aTwips+10)/20;
	}

EXPORT_C TInt EikFontUtils::TwipsFromPoints(TInt aPoints)
	{ // static
	//one point=20 twips
	return (aPoints*20);
	}

EXPORT_C TInt EikFontUtils::TwipsFromPoints(const TDesC& aPoints)
	{ // static
	TInt digits=aPoints.Length();
	TInt num=aPoints[--digits];
	TInt count=0;
	while (digits)
		num+=aPoints[--digits]*(++count*10);
	return TwipsFromPoints(num);
	}

EXPORT_C TInt EikFontUtils::IndexOfNearestHeight(CArrayFix<TInt>& aTwipsList,TInt aHeight)
	{ // static
	TInt pos=0;
	const TInt count=aTwipsList.Count();
	for (TInt ii=0; ii<count; ii++)
		{
		if (aTwipsList[ii]>aHeight)
			break;
		pos=ii;
		}
	return pos;
	}
