os/textandloc/fontservices/fontstore/src/FNTBODY.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/textandloc/fontservices/fontstore/src/FNTBODY.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,594 @@
     1.4 +/*
     1.5 +* Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include <s32file.h>
    1.23 +#include <fntstore.h>
    1.24 +#include "FNTBODY.H"
    1.25 +#include <graphics/openfontconstants.h>
    1.26 +
    1.27 +#include "OstTraceDefinitions.h"
    1.28 +#ifdef OST_TRACE_COMPILER_IN_USE
    1.29 +#include "FNTBODYTraces.h"
    1.30 +#endif
    1.31 +
    1.32 +
    1.33 +CFontStoreFile::CFontStoreFile()
    1.34 + :	iCollectionUid(KNullUid),
    1.35 +	iUsageCount(1),
    1.36 +	iFileStore(NULL),
    1.37 +	iFileAddress(0),
    1.38 +	iDataStreamId(KNullStreamId)
    1.39 +	{
    1.40 +	}
    1.41 +
    1.42 +void CFontStoreFile::ConstructL(const TParse& aParse,RFs& aFs)
    1.43 +	{
    1.44 +	TInt drive;
    1.45 +	User::LeaveIfError(RFs::CharToDrive(aParse.Drive()[0], drive));
    1.46 +	TDriveInfo driveinfo;
    1.47 +	User::LeaveIfError(aFs.Drive(driveinfo, drive));
    1.48 +	RFile file;
    1.49 +
    1.50 +	// store the filename
    1.51 +	iFullName = aParse.FullName().AllocL();
    1.52 +	User::LeaveIfError(file.Open(aFs, *iFullName, EFileStream | EFileRead | EFileShareReadersOnly));
    1.53 +
    1.54 +	// check to see if the fonts are stored on ROM.  Note that NAND != rom so font files in NAND devices
    1.55 +	// must be handled as if they were in RAM.  A NULL pointer returned by IsFileInRom means its RAM
    1.56 +
    1.57 +	if (aFs.IsFileInRom(*iFullName)!=NULL)
    1.58 +		{
    1.59 +		// fonts are stored on a XIP (execute in place) device
    1.60 +		TInt ret = file.Seek(ESeekAddress, iFileAddress);
    1.61 +
    1.62 +		if (ret != KErrNone)
    1.63 +			{
    1.64 +			file.Close();
    1.65 +			User::Leave(ret);
    1.66 +			}
    1.67 +		}
    1.68 +
    1.69 +	// convert RFile into a CDirectFileStore
    1.70 +	CDirectFileStore* fileStore = CDirectFileStore::FromLC(file);
    1.71 +
    1.72 +	if (fileStore->Type()[1] != TUid::Uid(KFontStoreFileUidVal))
    1.73 +		User::Leave(KErrNotSupported);
    1.74 +	TStreamId headerid = fileStore->Root();
    1.75 +	RStoreReadStream stream;
    1.76 +	stream.OpenLC(*fileStore, headerid);
    1.77 +	TInt fnttranversion = stream.ReadInt32L();
    1.78 +	// This works for version 42 (with new metrics) and for earlier versions 
    1.79 +	// which will synthesize the required metrics. It may have to be changed 
    1.80 +	// if the version number is incremented again.
    1.81 +	if (fnttranversion < (KFnttranVersion - 1)  && fnttranversion != KFnttran7650Version) 
    1.82 +		User::Leave(KErrNotSupported);
    1.83 +	iFontVersion = fnttranversion;
    1.84 +	stream >> iCollectionUid;
    1.85 +	iKPixelAspectRatio = stream.ReadInt32L();
    1.86 +	stream >> iDataStreamId;
    1.87 +	CleanupStack::PopAndDestroy(&stream);	// close root stream
    1.88 +	// ensure font data stream can be opened
    1.89 +	stream.OpenLC(*fileStore, iDataStreamId);
    1.90 +	CleanupStack::PopAndDestroy(&stream);	// close font stream
    1.91 +	// transfer ownership of fileStore
    1.92 +	CleanupStack::Pop(fileStore);
    1.93 +	iFileStore = fileStore;
    1.94 +	}
    1.95 +
    1.96 +CFontStoreFile* CFontStoreFile::NewL(const TParse& aParse, RFs& aFs)
    1.97 +	{
    1.98 +	CFontStoreFile* fontstorefile = new(ELeave) CFontStoreFile;
    1.99 +	CleanupStack::PushL(fontstorefile);
   1.100 +	fontstorefile->ConstructL(aParse, aFs);
   1.101 +	CleanupStack::Pop();
   1.102 +	return fontstorefile;
   1.103 +	}
   1.104 +
   1.105 +CFontStoreFile::~CFontStoreFile()
   1.106 +	{
   1.107 +	delete iFullName;
   1.108 +	iFullName = NULL;
   1.109 +	delete iFileStore;
   1.110 +	iFileStore = NULL;
   1.111 +	}
   1.112 +
   1.113 +TBitmapCodeSection::TBitmapCodeSection()
   1.114 + :	TCodeSection(),
   1.115 +	iCharacterData(),
   1.116 +	iBitmapData()
   1.117 +	{
   1.118 +	}
   1.119 +
   1.120 +//This method is called  from CFontBitmap::InternalizeL.
   1.121 +//We have to read stream IDs from the stream, not offsets.
   1.122 +//Obviously the method is called once per life time of 
   1.123 +//CFontBitmap instance.
   1.124 +void TBitmapCodeSection::InternalizeL(RReadStream &aStream)
   1.125 +	{
   1.126 +	iStart = aStream.ReadUint16L();
   1.127 +	iEnd = aStream.ReadUint16L();
   1.128 +	aStream >> iCharacterData.iOffsetsId;
   1.129 +	aStream >> iBitmapData.iBitmapId; 
   1.130 +	}
   1.131 +
   1.132 +//This method is called  from CFontBitmap::RestoreComponentsL - 
   1.133 +//if the CFontBitmap instance is in RAM and CFontBitmap::iComponentsRestored is EFalse.
   1.134 +//We use here stream IDs, not offsets.
   1.135 +//If the memory allocation for the offsets doesn't fail - aAllocMemCounter is incremented
   1.136 +//After calling of TBitmapCodeSection::InternalizeOffsetsL character metrics streamID is no more valid - 
   1.137 +//we have valid character metrics offset into RAM memory.
   1.138 +void TBitmapCodeSection::InternalizeOffsetsL(const CStreamStore& aStreamStore, RHeap* aHeap, TInt& aAllocMemCounter)
   1.139 +	{
   1.140 +	RStoreReadStream stream;
   1.141 +	stream.OpenLC(aStreamStore, iCharacterData.iOffsetsId);
   1.142 +
   1.143 +	TInt size = stream.ReadInt32L();
   1.144 +	TBitmapFontCharacterOffset* characterOffsetsList = (TBitmapFontCharacterOffset*)aHeap->AllocL(sizeof(TBitmapFontCharacterOffset) * size);
   1.145 +	aAllocMemCounter++;
   1.146 +	iCharacterData.iCharacterOffsetsListOffset = TInt(characterOffsetsList) - TInt(this);
   1.147 +	TBitmapFontCharacterOffset* pEnd = characterOffsetsList + size;
   1.148 +	for (TBitmapFontCharacterOffset* p = characterOffsetsList; p < pEnd; p++)
   1.149 +		{
   1.150 +		p->InternalizeL(stream);
   1.151 +		}
   1.152 +
   1.153 +	CleanupStack::PopAndDestroy();
   1.154 +	}
   1.155 +
   1.156 +//This method is called  from CFontBitmap::RestoreComponentsL - 
   1.157 +//if the CFontBitmap instance is in RAM and CFontBitmap::iComponentsRestored is EFalse.
   1.158 +//We use here stream IDs, not offsets.
   1.159 +//If the memory allocation for the bitmap doesn't fail - aAllocMemCounter is incremented
   1.160 +//After calling of TBitmapCodeSection::InternalizeBitmapL bitmap streamID is no more valid - 
   1.161 +//we have valid bitmap offset into RAM memory.
   1.162 +void TBitmapCodeSection::InternalizeBitmapL(const CStreamStore& aStreamStore, RHeap* aHeap, TInt& aAllocMemCounter)
   1.163 +	{
   1.164 +	RStoreReadStream stream;
   1.165 +	stream.OpenLC(aStreamStore, iBitmapData.iBitmapId);
   1.166 +
   1.167 +	TInt size = stream.ReadInt32L();
   1.168 +	TUint8* bitmap = (TUint8*)aHeap->AllocL(size);
   1.169 +	aAllocMemCounter++;
   1.170 +	iBitmapData.iBitmapOffset = TInt(bitmap) - TInt(this);
   1.171 +	stream.ReadL(bitmap, size);
   1.172 +
   1.173 +	CleanupStack::PopAndDestroy();
   1.174 +	}
   1.175 +
   1.176 +//This method is called from CFontBitmap::InternalizeL if the
   1.177 +//CFontBitmap instance is in ROM.
   1.178 +//We use here stream IDs to calculate offsets.
   1.179 +//After calling of TBitmapCodeSection::FixUpComponents streamIDs are no more valid - 
   1.180 +//we have valid offsets into ROM memory.
   1.181 +//Obviously the method is called once per life time of 
   1.182 +//CFontBitmap instance.
   1.183 +void TBitmapCodeSection::FixUpComponents(TInt aFileAddress)
   1.184 +	{
   1.185 +	TBitmapFontCharacterOffset* characterOffsetsList = (TBitmapFontCharacterOffset*) (aFileAddress + sizeof(TInt) + iCharacterData.iOffsetsId);
   1.186 +	iCharacterData.iCharacterOffsetsListOffset = TInt(characterOffsetsList);
   1.187 +	TUint8* bitmap = (TUint8*) (aFileAddress + sizeof(TInt) + iBitmapData.iBitmapId);
   1.188 +	iBitmapData.iBitmapOffset = TInt(bitmap);
   1.189 +	}
   1.190 +
   1.191 +//This method is caled from CFontBitmap::DeleteComponents(),
   1.192 +//only if the CFontBitmap instance is in RAM.
   1.193 +void TBitmapCodeSection::DeleteOffsets(RHeap* aHeap)
   1.194 +	{
   1.195 +	TBitmapFontCharacterOffset*  charactersOffsetsList = CharacterOffsetsList(ETrue);
   1.196 +	if(TUint32(this) != TUint32(charactersOffsetsList))
   1.197 +		{
   1.198 +		aHeap->Free(charactersOffsetsList);
   1.199 +		}
   1.200 +	}
   1.201 +
   1.202 +//This method is caled from CFontBitmap::DeleteComponents(),
   1.203 +//only if the CFontBitmap instance is in RAM.
   1.204 +void TBitmapCodeSection::DeleteBitmap(RHeap* aHeap)
   1.205 +	{
   1.206 +	TUint8* bitmap = Bitmap(ETrue);
   1.207 +	if(TUint32(this) != TUint32(bitmap))
   1.208 +		{
   1.209 +		aHeap->Free(bitmap);
   1.210 +		}
   1.211 +	}
   1.212 +
   1.213 +TBitmapFontCharacterOffset* TBitmapCodeSection::CharacterOffsetsList(TBool aIsInRAM) const
   1.214 +	{
   1.215 +	return reinterpret_cast <TBitmapFontCharacterOffset*> 
   1.216 +		(iCharacterData.iCharacterOffsetsListOffset + (aIsInRAM ? TInt(this) : 0));
   1.217 +	}
   1.218 +
   1.219 +TUint8* TBitmapCodeSection::Bitmap(TBool aIsInRAM) const
   1.220 +	{
   1.221 +	return reinterpret_cast <TUint8*> 
   1.222 +		(iBitmapData.iBitmapOffset + (aIsInRAM ? TInt(this) : 0));
   1.223 +	}
   1.224 +
   1.225 +TCharacterMetricsTable::TCharacterMetricsTable(RHeap* aHeap)
   1.226 + :	iHeap(aHeap),
   1.227 +	iMetricsStartId(KNullStreamId),
   1.228 +	iCharacterMetricsStartPtr(0),
   1.229 +	iNumberOfMetrics(0),
   1.230 +	iMetricsOnHeap(EFalse)
   1.231 +	{}
   1.232 +
   1.233 +void TCharacterMetricsTable::InternalizeL(RReadStream& aStream)
   1.234 +	{
   1.235 +	iMetricsStartId = aStream.ReadInt32L();
   1.236 +	iNumberOfMetrics = aStream.ReadInt32L();
   1.237 +	}
   1.238 +
   1.239 +void TCharacterMetricsTable::InternalizeMetricsL(RReadStream& aStream)
   1.240 +	{
   1.241 +	aStream.ReadInt32L(); // size
   1.242 +	TBitmapFontCharacterMetrics* charactermetricslist = static_cast<TBitmapFontCharacterMetrics*>(iHeap->AllocL(sizeof(TBitmapFontCharacterMetrics) * iNumberOfMetrics));
   1.243 +	iMetricsOnHeap = ETrue;
   1.244 +	// Offset from this to location on the heap ('cos the file is not in ROM)
   1.245 +	iCharacterMetricsStartPtr = reinterpret_cast<TInt>(charactermetricslist) - reinterpret_cast<TInt>(this);
   1.246 +	TBitmapFontCharacterMetrics* pEnd = charactermetricslist + iNumberOfMetrics;
   1.247 +	for (TBitmapFontCharacterMetrics* p = charactermetricslist; p < pEnd; p++)
   1.248 +		{
   1.249 +		p->InternalizeL(aStream);
   1.250 +		}
   1.251 +	}
   1.252 +
   1.253 +void TCharacterMetricsTable::RestoreL(const CStreamStore& aStreamStore)
   1.254 +	{
   1.255 +	if (iCharacterMetricsStartPtr == 0)
   1.256 +		{	// We haven't already read it in from RAM file
   1.257 +		RStoreReadStream stream;
   1.258 +		stream.OpenLC(aStreamStore, iMetricsStartId);
   1.259 +		InternalizeMetricsL(stream);
   1.260 +		CleanupStack::PopAndDestroy();
   1.261 +		}
   1.262 +	}
   1.263 +
   1.264 +void TCharacterMetricsTable::FixUp(TInt aFileAddress)
   1.265 +	{
   1.266 +	TBitmapFontCharacterMetrics* charactermetricslist = reinterpret_cast<TBitmapFontCharacterMetrics*>(aFileAddress + sizeof(TInt) + iMetricsStartId.Value());
   1.267 +	iCharacterMetricsStartPtr = TInt(charactermetricslist);	// Ptr to location in a ROM file
   1.268 +	iMetricsStartId = KNullStreamId;
   1.269 +	iMetricsOnHeap = EFalse;
   1.270 +	}
   1.271 +
   1.272 +void TCharacterMetricsTable::Delete()
   1.273 +	{	// This routine is only called if the font file is in RAM, not ROM, and therefore the metrics have been stashed on the heap
   1.274 +    if (iMetricsOnHeap && iCharacterMetricsStartPtr)
   1.275 +        {
   1.276 +        iHeap->Free(reinterpret_cast<TAny*>(MetricsFromOffset(0)));
   1.277 +        iCharacterMetricsStartPtr = 0;
   1.278 +        iMetricsOnHeap = EFalse;
   1.279 +        }
   1.280 +	}
   1.281 +
   1.282 +const TBitmapFontCharacterMetrics* TCharacterMetricsTable::Metric(TInt aIndex) const
   1.283 +	{
   1.284 +	if ((aIndex < 0) || (aIndex > iNumberOfMetrics))
   1.285 +	    {
   1.286 +	    OstTraceExt2( TRACE_FATAL, TCHARACTERMETRICSTABLE_METRIC, "TCharacterMetricsTable::Metric, aIndex=%d, iNumberOfMetrics=%d, Panic(EFntMetricsIndexOutOfBounds)", aIndex, iNumberOfMetrics);
   1.287 +	    __ASSERT_DEBUG(0, Panic(EFntMetricsIndexOutOfBounds));
   1.288 +	    }
   1.289 +    // Sometimes the start ptr is to a metrics heap item and sometimes it points into a ROM file
   1.290 +    if (iMetricsOnHeap)
   1.291 +        {
   1.292 +        // Start ptr is to metrics heap item
   1.293 +        return MetricsFromOffset(aIndex);
   1.294 +        }
   1.295 +    else
   1.296 +        {
   1.297 +        // Start ptr is to a file in ROM
   1.298 +        return reinterpret_cast<const TBitmapFontCharacterMetrics*> (iCharacterMetricsStartPtr + (aIndex * sizeof(TBitmapFontCharacterMetrics)));
   1.299 +        }
   1.300 +	}
   1.301 +
   1.302 +TInt TCharacterMetricsTable::NumberOfMetrics() const
   1.303 +	{
   1.304 +	return iNumberOfMetrics;
   1.305 +	}
   1.306 +
   1.307 +TBitmapFontCharacterMetrics* TCharacterMetricsTable::MetricsFromOffset(TInt aIndex) const
   1.308 +    {
   1.309 +    __ASSERT_DEBUG(iMetricsOnHeap,Panic(EFntMetricsNotOnHeap));
   1.310 +    return reinterpret_cast<TBitmapFontCharacterMetrics*>(reinterpret_cast<TInt>(this) + iCharacterMetricsStartPtr+ (aIndex * sizeof(TBitmapFontCharacterMetrics)));
   1.311 +    }
   1.312 +
   1.313 +CFontBitmap::CFontBitmap(RHeap* aHeap, CFontStoreFile* aFontStoreFile)
   1.314 + :	iHeap(aHeap),
   1.315 +	iFontStoreFileOffset(0),
   1.316 +	iUid(KNullUid),
   1.317 +	iPosture(0),
   1.318 +	iStrokeWeight(0),
   1.319 +	iIsProportional(0),
   1.320 +	iIsInRAM(!aFontStoreFile->iFileAddress),
   1.321 +	iUsageCount(1),
   1.322 +	iCellHeightInPixels(0),
   1.323 +	iAscentInPixels(0),
   1.324 +	iMaxCharWidthInPixels(0),
   1.325 +	iMaxNormalCharWidthInPixels(0),
   1.326 +	iBitmapEncoding(0),
   1.327 +	iNumCodeSections(0),
   1.328 +	iCodeSectionListOffset(0),
   1.329 +	iCharacterMetricsTable(aHeap),
   1.330 +	iComponentsRestored(EFalse),
   1.331 +	iAllocMemCounter_Offsets(0),
   1.332 +	iAllocMemCounter_Bitmaps(0),
   1.333 +	iFontCapitalAscent(0),
   1.334 +	iFontMaxAscent(0),
   1.335 +	iFontStandardDescent(0),
   1.336 +	iFontMaxDescent(0),
   1.337 +	iFontLineGap(0)
   1.338 +	{
   1.339 +	iFontStoreFileOffset = TInt(aFontStoreFile) - TInt(this);
   1.340 +	}
   1.341 +
   1.342 +void CFontBitmap::InternalizeL(RReadStream &aStream, TInt aFontVersion)
   1.343 +	{
   1.344 +	aStream >> iUid;
   1.345 +	iPosture = aStream.ReadInt8L();
   1.346 +	iStrokeWeight = aStream.ReadInt8L();
   1.347 +	iIsProportional = aStream.ReadInt8L();
   1.348 +	iCellHeightInPixels = aStream.ReadInt8L();
   1.349 +	iAscentInPixels = aStream.ReadInt8L();
   1.350 +	iMaxCharWidthInPixels = aStream.ReadInt8L();
   1.351 +	iMaxNormalCharWidthInPixels = aStream.ReadInt8L();
   1.352 +	if ( aFontVersion  >= KFnttranVersion )
   1.353 +		{ // read the new metrics in
   1.354 +		iFontCapitalAscent = aStream.ReadInt8L();
   1.355 +		iFontMaxAscent = aStream.ReadInt8L();
   1.356 +		iFontStandardDescent = aStream.ReadInt8L();
   1.357 +		iFontMaxDescent = aStream.ReadInt8L();
   1.358 +		iFontLineGap = aStream.ReadInt8L();
   1.359 +		}
   1.360 +	else // synthesize the extra metrics (data compatibility with third party bitmap fonts for old phones)
   1.361 +		{
   1.362 +		iFontMaxAscent = iFontCapitalAscent = iAscentInPixels;
   1.363 +		iFontMaxDescent = iFontStandardDescent = iCellHeightInPixels - iAscentInPixels;
   1.364 +		iFontLineGap = ( ( iCellHeightInPixels * 12 ) + 5) / 10;  // 1.2 times design height
   1.365 +		}	
   1.366 +	iBitmapEncoding = aStream.ReadInt32L();
   1.367 +	iCharacterMetricsTable.InternalizeL(aStream);
   1.368 +	const TBool fixup = FontStoreFile()->iFileAddress;
   1.369 +	if (fixup)
   1.370 +		{
   1.371 +		iCharacterMetricsTable.FixUp(FontStoreFile()->iFileAddress);
   1.372 +		}
   1.373 +	iNumCodeSections = aStream.ReadInt32L();
   1.374 +	TBitmapCodeSection* codesectionlist = (TBitmapCodeSection*)User::LeaveIfNull(iHeap->AllocL(iNumCodeSections * sizeof(TBitmapCodeSection)));
   1.375 +	iCodeSectionListOffset = TInt(codesectionlist) - TInt(this);
   1.376 +	for (TInt i = 0; i < iNumCodeSections; i++)
   1.377 +		{
   1.378 +		new(codesectionlist + i) TBitmapCodeSection;
   1.379 +		codesectionlist[i].InternalizeL(aStream);
   1.380 +		if (fixup)
   1.381 +			codesectionlist[i].FixUpComponents(FontStoreFile()->iFileAddress);
   1.382 +		}
   1.383 +	}
   1.384 +
   1.385 +void CFontBitmap::UseL()
   1.386 +	{
   1.387 +	// Note object is created with a Usage Count of 1.
   1.388 +	// So incrementing to 2 normally indicates the first external reference.
   1.389 +	iUsageCount++;
   1.390 +	if (iUsageCount == 2)
   1.391 +		{
   1.392 +		RestoreComponentsL();
   1.393 +		}
   1.394 +	}
   1.395 +
   1.396 +void CFontBitmap::Release()
   1.397 +	{
   1.398 +	iUsageCount--;
   1.399 +	if (!iUsageCount)
   1.400 +		{ // object and all its users have closed
   1.401 +		delete this;
   1.402 +		}
   1.403 +	}
   1.404 +
   1.405 +/*
   1.406 +Get the metrics for a given character.
   1.407 +Return aBytes as null if the character aCode doesn't exist in the font.
   1.408 +*/
   1.409 +TBitmapFontCharacterMetrics CFontBitmap::CharacterMetrics(TInt aCode, const TUint8*& aBytes) const
   1.410 +	{
   1.411 + 	const TBitmapCodeSection* matchSection = NULL;
   1.412 +	const TBitmapCodeSection* const lastSection = CodeSectionList() + iNumCodeSections - 1;
   1.413 +
   1.414 +	TBitmapFontCharacterOffset offset;
   1.415 +	aBytes = NULL;
   1.416 +
   1.417 +	TBitmapFontCharacterMetrics metrics;
   1.418 +	const TBitmapCodeSection* startSearchBand = CodeSectionList();
   1.419 +	TInt numCodeSectionsRemaining = iNumCodeSections;
   1.420 +	while (numCodeSectionsRemaining >= 1)
   1.421 +		{
   1.422 +		TInt halfNumCodeSectionsRemaining = numCodeSectionsRemaining/2;
   1.423 +		const TBitmapCodeSection* centralSearchBand = startSearchBand+halfNumCodeSectionsRemaining;
   1.424 +		if ((aCode >= centralSearchBand->iStart) && (aCode <= centralSearchBand->iEnd))
   1.425 +			{
   1.426 +			matchSection = centralSearchBand;
   1.427 +			break;
   1.428 +			}
   1.429 +		else if ((aCode < centralSearchBand->iStart) || (centralSearchBand == lastSection))
   1.430 +			numCodeSectionsRemaining = halfNumCodeSectionsRemaining;
   1.431 +		else
   1.432 +			{
   1.433 +			startSearchBand = centralSearchBand + 1;
   1.434 +			numCodeSectionsRemaining -= halfNumCodeSectionsRemaining + 1;
   1.435 +			}
   1.436 +		}
   1.437 +
   1.438 +	if (matchSection)
   1.439 +		{
   1.440 +		offset =* ((matchSection->CharacterOffsetsList(iIsInRAM)) + (aCode-matchSection->iStart));
   1.441 +
   1.442 +		// Fill characters within code section.
   1.443 +		// Recursive call ensures that a valid metric is always returned.
   1.444 +		if (offset.iBitmapOffset == KFillCharacterOffset)
   1.445 +			{
   1.446 +			return CharacterMetrics(KReplacementCharacter, aBytes);
   1.447 +			}
   1.448 +		
   1.449 +		aBytes = matchSection->Bitmap(iIsInRAM) + offset.iBitmapOffset;
   1.450 +		
   1.451 +		// retrieve metric index from encoded 1 or 2 bytes
   1.452 +		TInt index = 0;
   1.453 +		TUint8 byte1 = (TUint8)*aBytes;
   1.454 +		const TInt switchMask = 0x1;
   1.455 +		const TBool oneByteIndex =! (byte1 & switchMask);
   1.456 +		byte1 = TUint8(byte1 >> 1);
   1.457 +		if (oneByteIndex)
   1.458 +			{
   1.459 +			index = byte1;
   1.460 +			aBytes += 1;
   1.461 +			}
   1.462 +		else
   1.463 +			{
   1.464 +			const TUint8 byte2 = (TUint8)(*(aBytes + 1));
   1.465 +			index = byte1 + (byte2 * 128);
   1.466 +			aBytes += 2;
   1.467 +			}
   1.468 +		// Copy metric from table
   1.469 +		metrics =* iCharacterMetricsTable.Metric(index);
   1.470 +		}
   1.471 +	return metrics;
   1.472 +	}
   1.473 +
   1.474 +void CFontBitmap::operator delete(TAny *aThis)
   1.475 +	{
   1.476 +	if (((CFontBitmap *)aThis)->iHeap)
   1.477 +		{
   1.478 +		((CFontBitmap *)aThis)->iHeap->Free(aThis);
   1.479 +		}
   1.480 +	}
   1.481 +
   1.482 +void CFontBitmap::SetPosture(TFontPosture aPosture)
   1.483 +	{
   1.484 +	iPosture = (TInt8)aPosture;
   1.485 +	}
   1.486 +
   1.487 +TFontPosture CFontBitmap::Posture() const
   1.488 +	{
   1.489 +	return (TFontPosture)iPosture;	 // iPosture is always smaller than TFontPosture
   1.490 +	}
   1.491 +
   1.492 +void CFontBitmap::SetStrokeWeight(TFontStrokeWeight aStrokeWeight)
   1.493 +	{
   1.494 +	iStrokeWeight = (TInt8)aStrokeWeight;
   1.495 +	}
   1.496 +
   1.497 +TFontStrokeWeight CFontBitmap::StrokeWeight() const
   1.498 +	{
   1.499 +	return (TFontStrokeWeight)iStrokeWeight;
   1.500 +	}
   1.501 +
   1.502 +void CFontBitmap::SetIsProportional(TBool aIsProportional)
   1.503 +	{
   1.504 +	iIsProportional = (TInt8)aIsProportional;
   1.505 +	}
   1.506 +
   1.507 +TBool CFontBitmap::IsProportional() const
   1.508 +	{
   1.509 +	return iIsProportional;
   1.510 +	}
   1.511 +
   1.512 +CFontStoreFile* CFontBitmap::FontStoreFile() const
   1.513 +	{
   1.514 +	TInt fsf = TInt(this) + iFontStoreFileOffset;
   1.515 +	return (CFontStoreFile*)fsf;
   1.516 +	}
   1.517 +
   1.518 +CFontBitmap::~CFontBitmap()
   1.519 +	{
   1.520 +	DeleteComponents();
   1.521 +	TBitmapCodeSection* codeSectionList = CodeSectionList();
   1.522 +	if(TUint32(this) != TUint32(codeSectionList))
   1.523 +		{
   1.524 +		iHeap->Free(codeSectionList);
   1.525 +		}
   1.526 +	iCodeSectionListOffset = 0;
   1.527 +	}
   1.528 +
   1.529 +//We have to count how many offsets and bitmaps are allocated successfully because if
   1.530 +//some of codesection's Internalize..L fails we have to deallocate the right amount of 
   1.531 +//data.
   1.532 +void CFontBitmap::RestoreComponentsL()
   1.533 +	{
   1.534 +	if (iIsInRAM)
   1.535 +		{
   1.536 +		if(!iComponentsRestored)
   1.537 +			{
   1.538 +			iAllocMemCounter_Offsets = 0;
   1.539 +			iAllocMemCounter_Bitmaps = 0;
   1.540 +			CStreamStore& store =* FontStoreFile()->iFileStore;
   1.541 +			for (TInt i = 0; i < iNumCodeSections; i++)
   1.542 +				{
   1.543 +				CodeSectionList()[i].InternalizeOffsetsL(store, iHeap, iAllocMemCounter_Offsets);
   1.544 +				CodeSectionList()[i].InternalizeBitmapL(store, iHeap, iAllocMemCounter_Bitmaps);
   1.545 +				}
   1.546 +			iCharacterMetricsTable.RestoreL(store);
   1.547 +			}
   1.548 +		iComponentsRestored = ETrue;
   1.549 +		}
   1.550 +	}
   1.551 +
   1.552 +void CFontBitmap::DeleteComponents()
   1.553 +	{
   1.554 +	if (iIsInRAM)
   1.555 +		{
   1.556 +		TInt i;
   1.557 +		for (i = 0; i < iAllocMemCounter_Offsets; i++)
   1.558 +			{
   1.559 +			CodeSectionList()[i].DeleteOffsets(iHeap);
   1.560 +			}
   1.561 +		for (i = 0; i < iAllocMemCounter_Bitmaps; i++)
   1.562 +			{
   1.563 +			CodeSectionList()[i].DeleteBitmap(iHeap);
   1.564 +			}
   1.565 +		iCharacterMetricsTable.Delete();
   1.566 +		}
   1.567 +	iAllocMemCounter_Offsets = 0;
   1.568 +	iAllocMemCounter_Bitmaps = 0;
   1.569 +	iComponentsRestored = EFalse;
   1.570 +	}
   1.571 +
   1.572 +TBitmapCodeSection* CFontBitmap::CodeSectionList() const
   1.573 +	{
   1.574 +	TInt bcs = TInt(this) + iCodeSectionListOffset;
   1.575 +	return (TBitmapCodeSection*)bcs;
   1.576 +	}
   1.577 +
   1.578 +TTypefaceFontBitmap::TTypefaceFontBitmap()
   1.579 + :	iTypeface(NULL),
   1.580 +	iFontBitmap(NULL),
   1.581 +	iHeightFactor(1),
   1.582 +	iWidthFactor(1)
   1.583 +	{
   1.584 +	}
   1.585 +
   1.586 +TTypefaceFontBitmap::TTypefaceFontBitmap(TTypeface* aTypeface,CFontBitmap* aFontBitmap)
   1.587 + :	iTypeface(aTypeface),
   1.588 +	iFontBitmap(aFontBitmap),
   1.589 +	iHeightFactor(1),
   1.590 +	iWidthFactor(1)
   1.591 +	{
   1.592 +	}
   1.593 +
   1.594 +TInt TTypefaceFontBitmap::HeightInPixels() const
   1.595 +	{
   1.596 +	return iFontBitmap->iCellHeightInPixels * iHeightFactor;
   1.597 +	}