Reading all fonts from the device
CFontsContainer illustrates how to read and display all fonts currently usable at the target device (also works in the emulator). All necessary fonts information can be accessed through CWsScreenDevice, for you can get pointer using CEikonEnv as shown in the ConstructL() function. In this example the key event handler function (OfferKeyEventL() ) is only used to change the current font index, all font selection and drawing is handled in normal draw function.
Article Metadata
Fonts_Container.cpp
#include <coemain.h>
#include <aknutils.h>
CFontsContainer::CFontsContainer()
{
}
CFontsContainer::~CFontsContainer()
{
}
void CFontsContainer::ConstructL(const TRect& aRect)
{
CreateWindowL();
iNumTypefaces = CEikonEnv::Static()->ScreenDevice()->NumTypefaces();
SetRect(aRect);
ActivateL();
}
TKeyResponse CFontsContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{
TKeyResponse Ret = EKeyWasNotConsumed;
switch (aKeyEvent.iCode)
{
case EKeyRightArrow:
case EKeyUpArrow:
{
iCurrent++;
if(iCurrent >= iNumTypefaces)
{
iCurrent = 0;
}
}
DrawNow();
break;
case EKeyDownArrow:
case EKeyLeftArrow:
{
iCurrent--;
if(iCurrent < 0 )
{
iCurrent = (iNumTypefaces - 1);
}
}
DrawNow();
break;
default:
break;
}
return Ret;
}
void CFontsContainer::Draw(const TRect& aRect) const
{
CWindowGc &gc = SystemGc();
TRect rect = Rect();
CGraphicsDevice *dev = CEikonEnv::Static()->ScreenDevice();
gc.Clear();
TBuf<128> buf;
TTypefaceSupport tfs;
dev->TypefaceSupport(tfs, iCurrent);
TFontSpec spec(tfs.iTypeface.iName, 100);
CFont *font(NULL);
dev->GetNearestFontInTwips(font, spec);
buf.Num(iCurrent);
buf.Append(_L("/"));
buf.AppendNum(iNumTypefaces);
buf.Append(_L(" : "));
buf.Append(tfs.iTypeface.iName);
gc.UseFont(font);
TInt hhhh = font->HeightInPixels();
TRect DrwRect(5,5,Rect().Width(),(5 + hhhh));
gc.DrawText(buf,DrwRect,font->AscentInPixels(), CGraphicsContext::ELeft, 0);
DrwRect.iTl.iY = DrwRect.iBr.iY + 5;
DrwRect.iBr.iY = DrwRect.iTl.iY + hhhh;
gc.DrawText(_L("abcdefghIJKLMNOPQRSTU"),DrwRect,font->AscentInPixels(), CGraphicsContext::ELeft, 0);
DrwRect.iTl.iY = DrwRect.iBr.iY + 5;
DrwRect.iBr.iY = DrwRect.iTl.iY + hhhh;
gc.DrawText(_L("1234567890"),DrwRect,font->AscentInPixels(), CGraphicsContext::ELeft, 0);
gc.DiscardFont();
dev->ReleaseFont(font);
}
Fonts_Container.h
#include <coecntrl.h>
class CFontsContainer : public CCoeControl
{
public:
void ConstructL(const TRect& aRect);
CFontsContainer();
~CFontsContainer();
public: // from CCoeControl
void Draw(const TRect& aRect) const;
TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType);
private:
TInt iNumTypefaces,iCurrent;
};


06 Sep
2009
This article demonstrates on how to retrieve all the available fonts and draws them on to the screen. The draw function clearly illustrates how to use a font, access font specification and draw it on to the screen after getting the font height so that the gap between two lines is maintained. Gives a good idea on how to uses fonts for beginners.