×
Namespaces

Variants
Actions

Reading all fonts from the device

From Nokia Developer Wiki
Jump to: navigation, search

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
Article
Created: symbianyucca (20 Mar 2007)
Last edited: hamishwillee (11 Jan 2012)

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;
};
This page was last modified on 11 January 2012, at 09:58.
43 page views in the last 30 days.
×