fontservices/fontstore/tfs/FNTBODY_OLD.CPP
changeset 0 1fb32624e06b
equal deleted inserted replaced
-1:000000000000 0:1fb32624e06b
       
     1 /*
       
     2 * Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <s32file.h>
       
    20 #include "FNTSTORE.H"
       
    21 #include "FNTBODY_OLD.H"
       
    22 #include <graphics/openfontconstants.h>
       
    23 
       
    24 TBitmapCodeSectionOld::TBitmapCodeSectionOld(RHeap* aHeap)
       
    25  :	TCodeSection(),
       
    26 	iHeap(aHeap),
       
    27 	iOffsetsId(),
       
    28 	iCharacterOffsetsListOffset(0),
       
    29 	iBitmapId(),
       
    30 	iBitmapOffset(0)
       
    31 	{
       
    32 	}
       
    33 
       
    34 void TBitmapCodeSectionOld::InternalizeL(RReadStream &aStream)
       
    35 	{
       
    36 	iStart = aStream.ReadUint16L();
       
    37 	iEnd = aStream.ReadUint16L();
       
    38 	aStream >> iOffsetsId;
       
    39 	aStream >> iBitmapId; 
       
    40 	}
       
    41 
       
    42 void TBitmapCodeSectionOld::RestoreComponentsL(const CStreamStore& aStreamStore)
       
    43 	{
       
    44 	if (iCharacterOffsetsListOffset == 0)
       
    45 		{
       
    46 		RStoreReadStream stream;
       
    47 		stream.OpenLC(aStreamStore, iOffsetsId);
       
    48 		InternalizeOffsetsL(stream);
       
    49 		CleanupStack::PopAndDestroy();
       
    50 		}
       
    51 	if (iBitmapOffset == 0)
       
    52 		{
       
    53 		RStoreReadStream stream;
       
    54 		stream.OpenLC(aStreamStore, iBitmapId);
       
    55 		InternalizeBitmapL(stream);
       
    56 		CleanupStack::PopAndDestroy();
       
    57 		}
       
    58 	}
       
    59 
       
    60 void TBitmapCodeSectionOld::FixUpComponents(TInt aFileAddress)
       
    61 	{
       
    62 	TBitmapFontCharacterOffset* characterOffsetsList = (TBitmapFontCharacterOffset*) (aFileAddress + sizeof(TInt) + iOffsetsId.Value());
       
    63 	iCharacterOffsetsListOffset = TInt(characterOffsetsList);
       
    64 	iOffsetsId = KNullStreamId;
       
    65 	TUint8* bitmap = (TUint8*) (aFileAddress + sizeof(TInt) + iBitmapId.Value());
       
    66 	iBitmapOffset = TInt(bitmap);
       
    67 	}
       
    68 
       
    69 void TBitmapCodeSectionOld::DeleteComponents()
       
    70 	{
       
    71 	if (iCharacterOffsetsListOffset)
       
    72 		{
       
    73 		iHeap->Free(CharacterOffsetsList());
       
    74 		iCharacterOffsetsListOffset = 0;
       
    75 		}
       
    76 	if (iBitmapOffset)
       
    77 		{
       
    78 		iHeap->Free(Bitmap());
       
    79 		iBitmapOffset = 0;
       
    80 		}
       
    81 	}
       
    82 
       
    83 void TBitmapCodeSectionOld::operator delete(TAny *aThis)
       
    84 	{
       
    85 	if (((TBitmapCodeSectionOld *)aThis)->iHeap)
       
    86 		{
       
    87 		((TBitmapCodeSectionOld *)aThis)->iHeap->Free(aThis);
       
    88 		}
       
    89 	}
       
    90 
       
    91 void TBitmapCodeSectionOld::InternalizeOffsetsL(RReadStream &aStream)
       
    92 	{
       
    93 	TInt size = aStream.ReadInt32L();
       
    94 	TBitmapFontCharacterOffset* characterOffsetsList = (TBitmapFontCharacterOffset*)iHeap->AllocL(sizeof(TBitmapFontCharacterOffset) * size);
       
    95 	iCharacterOffsetsListOffset = TInt(characterOffsetsList) - TInt(this);
       
    96 	TBitmapFontCharacterOffset* pEnd = characterOffsetsList + size;
       
    97 	for (TBitmapFontCharacterOffset* p = characterOffsetsList; p < pEnd; p++)
       
    98 		{
       
    99 		p->InternalizeL(aStream);
       
   100 		}
       
   101 	}
       
   102 
       
   103 void TBitmapCodeSectionOld::InternalizeBitmapL(RReadStream &aStream)
       
   104 	{
       
   105 	TInt size = aStream.ReadInt32L();
       
   106 	TUint8* bitmap = (TUint8*)iHeap->AllocL(size);
       
   107 	iBitmapOffset = TInt(bitmap) - TInt(this);
       
   108 	aStream.ReadL(bitmap, size);
       
   109 	}
       
   110 
       
   111 TBitmapFontCharacterOffset* TBitmapCodeSectionOld::CharacterOffsetsList() const
       
   112 	{
       
   113 	if (iOffsetsId == KNullStreamId)
       
   114 		{
       
   115 		return (TBitmapFontCharacterOffset*)iCharacterOffsetsListOffset;
       
   116 		}
       
   117 	TInt charOffsList = TInt(this) + iCharacterOffsetsListOffset;
       
   118 	return (TBitmapFontCharacterOffset*)charOffsList;
       
   119 	}
       
   120 
       
   121 TUint8* TBitmapCodeSectionOld::Bitmap() const
       
   122 	{
       
   123 	if (iOffsetsId == KNullStreamId)
       
   124 		{
       
   125 		return (TUint8*)iBitmapOffset;
       
   126 		}
       
   127 	TInt bitmap = TInt(this)+iBitmapOffset;
       
   128 	return (TUint8*)bitmap;
       
   129 	}
       
   130 
       
   131 CFontBitmapOld::CFontBitmapOld(RHeap* aHeap, CFontStoreFile* aFontStoreFile)
       
   132  :	iHeap(aHeap),
       
   133 	iFontStoreFileOffset(0),
       
   134 	iUid(KNullUid),
       
   135 	iPosture(0),
       
   136 	iStrokeWeight(0),
       
   137 	iIsProportional(0),
       
   138 	iIsInRAM(!aFontStoreFile->iFileAddress),
       
   139 	iUsageCount(1),
       
   140 	iCellHeightInPixels(0),
       
   141 	iAscentInPixels(0),
       
   142 	iMaxCharWidthInPixels(0),
       
   143 	iMaxNormalCharWidthInPixels(0),
       
   144 	iBitmapEncoding(0),
       
   145 	iNumCodeSections(0),
       
   146 	iCodeSectionListOffset(0),
       
   147 	iCharacterMetricsTable(aHeap)
       
   148 	{
       
   149 	iFontStoreFileOffset = TInt(aFontStoreFile) - TInt(this);
       
   150 	}
       
   151 
       
   152 void CFontBitmapOld::InternalizeL(RReadStream &aStream)
       
   153 	{
       
   154 	aStream >> iUid;
       
   155 	iPosture = aStream.ReadInt8L();
       
   156 	iStrokeWeight = aStream.ReadInt8L();
       
   157 	iIsProportional = aStream.ReadInt8L();
       
   158 	iCellHeightInPixels = aStream.ReadInt8L();
       
   159 	iAscentInPixels = aStream.ReadInt8L();
       
   160 	iMaxCharWidthInPixels = aStream.ReadInt8L();
       
   161 	iMaxNormalCharWidthInPixels = aStream.ReadInt8L();
       
   162 	iBitmapEncoding = aStream.ReadInt32L();
       
   163 	iCharacterMetricsTable.InternalizeL(aStream);
       
   164 	const TBool fixup = FontStoreFile()->iFileAddress;
       
   165 	if (fixup)
       
   166 		{
       
   167 		iCharacterMetricsTable.FixUp(FontStoreFile()->iFileAddress);
       
   168 		}
       
   169 	iNumCodeSections = aStream.ReadInt32L();
       
   170 	TBitmapCodeSectionOld* codesectionlist = (TBitmapCodeSectionOld*)User::LeaveIfNull(iHeap->AllocL(iNumCodeSections * sizeof(TBitmapCodeSectionOld)));
       
   171 	iCodeSectionListOffset = TInt(codesectionlist) - TInt(this);
       
   172 	for (TInt i = 0; i < iNumCodeSections; i++)
       
   173 		{
       
   174 		new(codesectionlist + i) TBitmapCodeSectionOld(iHeap);
       
   175 		codesectionlist[i].InternalizeL(aStream);
       
   176 		if (fixup)
       
   177 			codesectionlist[i].FixUpComponents(FontStoreFile()->iFileAddress);
       
   178 		}
       
   179 	}
       
   180 
       
   181 void CFontBitmapOld::UseL()
       
   182 	{
       
   183 	iUsageCount++;
       
   184 	if (iUsageCount == 2)
       
   185 		RestoreComponentsL();
       
   186 	}
       
   187 
       
   188 void CFontBitmapOld::Release()
       
   189 	{
       
   190 	iUsageCount--;
       
   191 	if (!iUsageCount)
       
   192 		{
       
   193 		delete this;
       
   194 		}
       
   195 	}
       
   196 
       
   197 /*
       
   198 Get the metrics for a given character.
       
   199 Return aBytes as null if the character aCode doesn't exist in the font.
       
   200 */
       
   201 TBitmapFontCharacterMetrics CFontBitmapOld::CharacterMetrics(TInt aCode, const TUint8*& aBytes) const
       
   202 	{
       
   203  	const TBitmapCodeSectionOld* matchSection = NULL;
       
   204 	const TBitmapCodeSectionOld* const lastSection = CodeSectionList() + iNumCodeSections - 1;
       
   205 
       
   206 	TBitmapFontCharacterOffset offset;
       
   207 	aBytes = NULL;
       
   208 
       
   209 	TBitmapFontCharacterMetrics metrics;
       
   210 	const TBitmapCodeSectionOld* startSearchBand = CodeSectionList();
       
   211 	TInt numCodeSectionsRemaining = iNumCodeSections;
       
   212 	while (numCodeSectionsRemaining >= 1)
       
   213 		{
       
   214 		TInt halfNumCodeSectionsRemaining = numCodeSectionsRemaining/2;
       
   215 		const TBitmapCodeSectionOld* centralSearchBand = startSearchBand+halfNumCodeSectionsRemaining;
       
   216 		if ((aCode >= centralSearchBand->iStart) && (aCode <= centralSearchBand->iEnd))
       
   217 			{
       
   218 			matchSection = centralSearchBand;
       
   219 			break;
       
   220 			}
       
   221 		else if ((aCode < centralSearchBand->iStart) || (centralSearchBand == lastSection))
       
   222 			numCodeSectionsRemaining = halfNumCodeSectionsRemaining;
       
   223 		else
       
   224 			{
       
   225 			startSearchBand = centralSearchBand + 1;
       
   226 			numCodeSectionsRemaining -= halfNumCodeSectionsRemaining + 1;
       
   227 			}
       
   228 		}
       
   229 
       
   230 	if (matchSection)
       
   231 		{
       
   232 		offset =* ((matchSection->CharacterOffsetsList()) + (aCode-matchSection->iStart));
       
   233 
       
   234 		// Fill characters within code section.
       
   235 		// Recursive call ensures that a valid metric is always returned.
       
   236 		if (offset.iBitmapOffset == KFillCharacterOffset)
       
   237 			{
       
   238 			return CharacterMetrics(KReplacementCharacter, aBytes);
       
   239 			}
       
   240 		
       
   241 		aBytes = matchSection->Bitmap() + offset.iBitmapOffset;
       
   242 		
       
   243 		// retrieve metric index from encoded 1 or 2 bytes
       
   244 		TInt index = 0;
       
   245 		TUint8 byte1 = (TUint8)*aBytes;
       
   246 		const TInt switchMask = 0x1;
       
   247 		const TBool oneByteIndex =! (byte1 & switchMask);
       
   248 		byte1 = TUint8(byte1 >> 1);
       
   249 		if (oneByteIndex)
       
   250 			{
       
   251 			index = byte1;
       
   252 			aBytes += 1;
       
   253 			}
       
   254 		else
       
   255 			{
       
   256 			const TUint8 byte2 = (TUint8)(*(aBytes + 1));
       
   257 			index = byte1 + (byte2 * 128);
       
   258 			aBytes += 2;
       
   259 			}
       
   260 		// Copy metric from table
       
   261 		metrics =* iCharacterMetricsTable.Metric(index);
       
   262 		}
       
   263 	return metrics;
       
   264 	}
       
   265 
       
   266 void CFontBitmapOld::operator delete(TAny *aThis)
       
   267 	{
       
   268 	if (((CFontBitmapOld *)aThis)->iHeap)
       
   269 		{
       
   270 		((CFontBitmapOld *)aThis)->iHeap->Free(aThis);
       
   271 		}
       
   272 	}
       
   273 
       
   274 void CFontBitmapOld::SetPosture(TFontPosture aPosture)
       
   275 	{
       
   276 	iPosture = (TInt8)aPosture;
       
   277 	}
       
   278 
       
   279 TFontPosture CFontBitmapOld::Posture()
       
   280 	{
       
   281 	return (TFontPosture)iPosture;	 // iPosture is always smaller than TFontPosture
       
   282 	}
       
   283 
       
   284 void CFontBitmapOld::SetStrokeWeight(TFontStrokeWeight aStrokeWeight)
       
   285 	{
       
   286 	iStrokeWeight = (TInt8)aStrokeWeight;
       
   287 	}
       
   288 
       
   289 TFontStrokeWeight CFontBitmapOld::StrokeWeight()
       
   290 	{
       
   291 	return (TFontStrokeWeight)iStrokeWeight;
       
   292 	}
       
   293 
       
   294 void CFontBitmapOld::SetIsProportional(TBool aIsProportional)
       
   295 	{
       
   296 	iIsProportional = (TInt8)aIsProportional;
       
   297 	}
       
   298 
       
   299 TBool CFontBitmapOld::IsProportional()
       
   300 	{
       
   301 	return iIsProportional;
       
   302 	}
       
   303 
       
   304 CFontStoreFile* CFontBitmapOld::FontStoreFile() const
       
   305 	{
       
   306 	TInt fsf = TInt(this) + iFontStoreFileOffset;
       
   307 	return (CFontStoreFile*)fsf;
       
   308 	}
       
   309 
       
   310 CFontBitmapOld::~CFontBitmapOld()
       
   311 	{
       
   312 	DeleteComponents();
       
   313 	iHeap->Free(CodeSectionList());
       
   314 	iCodeSectionListOffset = 0;
       
   315 	}
       
   316 
       
   317 void CFontBitmapOld::RestoreComponentsL()
       
   318 	{
       
   319 	if (iIsInRAM)
       
   320 		{
       
   321 		CStreamStore& store =* FontStoreFile()->iFileStore;
       
   322 		for (TInt i = 0; i < iNumCodeSections; i++)
       
   323 			{
       
   324 			CodeSectionList()[i].RestoreComponentsL(store);
       
   325 			}
       
   326 		iCharacterMetricsTable.RestoreL(store);
       
   327 		}
       
   328 	}
       
   329 
       
   330 void CFontBitmapOld::DeleteComponents()
       
   331 	{
       
   332 	if (iIsInRAM)
       
   333 		{
       
   334 		for (TInt i = 0; i < iNumCodeSections; i++)
       
   335 			{
       
   336 			CodeSectionList()[i].DeleteComponents();
       
   337 			}
       
   338 		iCharacterMetricsTable.Delete();
       
   339 		}
       
   340 	}
       
   341 
       
   342 TBitmapCodeSectionOld* CFontBitmapOld::CodeSectionList() const
       
   343 	{
       
   344 	TInt bcs = TInt(this) + iCodeSectionListOffset;
       
   345 	return (TBitmapCodeSectionOld*)bcs;
       
   346 	}
       
   347