03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

fontSpec));<br />

The key function here is GetNearestFontInTwips(), a member function of<br />

MGraphicsDeviceMap. You pass a TFontSpec to this function, and you get back a<br />

pointer to a device-dependent font (a CFont*).<br />

The mapping from TFontSpec to CFont* is ultimately handled by a graphics device<br />

(though the font specification may be zoomed by a zoom factor). Once you have a CFont*,<br />

you can only use it on the device that allocated it – more precisely, you can only use it for<br />

drawing through a graphics context to the device that allocated it.<br />

This function usually finds a match, but in the unlikely case that it doesn't, it returns an error<br />

code. I propagate any error by calling User::LeaveIfError()<br />

Notice the need to release the font after use; when you no longer need a CFont*, you must<br />

ask the device to release it.<br />

If you forget to release a font, the effect will be the same as a memory leak: your program<br />

will get panicked on exit from emulator debug builds.<br />

There is no error if ReleaseFont() is called when the font has not been allocated yet, the<br />

function just returns. Therefore, it is safe to release the font at the start of the function. The<br />

last font to be allocated before the application is closed is released in the hello control class<br />

destructor.<br />

The code iFont=NULL exists because of the possibility of the function leaving before the<br />

font is reallocated. In this case, the destructor would attempt to release the font again if<br />

iFont wasn't set to NULL, causing the application to crash.<br />

The font specification uses the TFontSpec class, defined by the GDI, in gdi.h. Here's its<br />

declaration:<br />

class TFontSpec<br />

{<br />

public:<br />

IMPORT_C TFontSpec();<br />

IMPORT_C TFontSpec(const TDesC& aTypefaceName, TInt aHeight);<br />

IMPORT_C TBool operator==(const TFontSpec& aFontSpec) const;<br />

IMPORT_C void InternalizeL(RReadStream& aStream);<br />

IMPORT_C void ExternalizeL(RWriteStream& aStream) const;<br />

public:<br />

TTypeface iTypeface;<br />

TInt iHeight;<br />

TFontStyle iFontStyle;<br />

};<br />

A font specification consists of a typeface, a height, and a font style. The TTypeface class<br />

is also defined by the GDI. It has several attributes, of which the most important is the name.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!