svgtopt/SVG/SVGImpl/src/SVGFontHashMap.cpp
changeset 0 d46562c3d99d
equal deleted inserted replaced
-1:000000000000 0:d46562c3d99d
       
     1 /*
       
     2 * Copyright (c) 2003 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:  SVG Implementation source file
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include "SVGFontHashMap.h"
       
    20 #include "SVGDocumentImpl.h"
       
    21 
       
    22 //SVG Font storage 
       
    23 
       
    24 // ==========================================================================
       
    25 // Need method description
       
    26 // ==========================================================================
       
    27 CSvgFontHashMap* CSvgFontHashMap::NewL()
       
    28     {
       
    29     CSvgFontHashMap*   self    = new ( ELeave ) CSvgFontHashMap;
       
    30     CleanupStack::PushL( self );
       
    31     self->ConstructL();
       
    32     CleanupStack::Pop();
       
    33 
       
    34     return self;
       
    35     }
       
    36 
       
    37 // ==========================================================================
       
    38 // Need method description
       
    39 // ==========================================================================
       
    40 CSvgFontHashMap* CSvgFontHashMap::NewLC()
       
    41     {
       
    42     CSvgFontHashMap*   self    = new ( ELeave ) CSvgFontHashMap;
       
    43     CleanupStack::PushL( self );
       
    44     self->ConstructL();
       
    45 
       
    46     return self;
       
    47     }
       
    48 
       
    49 // ==========================================================================
       
    50 // Need method description
       
    51 // ==========================================================================
       
    52 void CSvgFontHashMap::ConstructL()
       
    53     {
       
    54     }
       
    55 
       
    56 // ==========================================================================
       
    57 // Need method description
       
    58 // ==========================================================================
       
    59 CSvgFontHashMap::CSvgFontHashMap()
       
    60 
       
    61     {
       
    62     }
       
    63 
       
    64 // ==========================================================================
       
    65 // Need method description
       
    66 // ==========================================================================
       
    67 CSvgFontHashMap::~CSvgFontHashMap()
       
    68     {	
       
    69     	iSvgFontMapItems.ResetAndDestroy();
       
    70     	iSvgFontMapItems.Close();
       
    71     	
       
    72     	iFontDocumentPointers.ResetAndDestroy();
       
    73     	iFontDocumentPointers.Close();
       
    74     }
       
    75 
       
    76 // ==========================================================================
       
    77 // CSvgFontHashMap::AddFontL( CSvgFontElementImpl* aFontPtr,
       
    78 //                                 const TDesC& aFontFamilyName )
       
    79 // return ETrue if Font added
       
    80 // return EFalse if Font was already in map
       
    81 // ==========================================================================
       
    82 TBool CSvgFontHashMap::AddFontL( CSvgFontElementImpl* aFontPtr,
       
    83                                  const TDesC& aFontFamilyName )
       
    84 {
       
    85 	CSvgFontMapItem* lFontMapItem = GetFontMapItem( aFontFamilyName );
       
    86     	
       
    87     	if (lFontMapItem)
       
    88     	{
       
    89     		//already a reference to this font in the font map
       
    90     		AddReferenceToFontL( aFontFamilyName);
       
    91     		return EFalse;
       
    92     	}
       
    93     	else
       
    94     	{
       
    95     		//didnt find that font in the font map...add it
       
    96 			CSvgFontMapItem* lNewSvgFontMapItem = CSvgFontMapItem::NewL( );
       
    97 		
       
    98 			lNewSvgFontMapItem->SetFontPtr( aFontPtr );
       
    99        		lNewSvgFontMapItem->CopyFontFamilyName( aFontFamilyName );
       
   100        		lNewSvgFontMapItem->IncrementReferenceCount();
       
   101        		
       
   102 			iSvgFontMapItems.Append(lNewSvgFontMapItem);
       
   103 			
       
   104 			return ETrue;
       
   105 		}
       
   106 }
       
   107 
       
   108 // --------------------------------------------------------------------------
       
   109 // void CSvgFontHashMap::AddReferenceToFontL( CFbsBitmap* aFont,
       
   110 // return ETrue if reference successfully added
       
   111 // return EFalse if already in map so just incremented reference
       
   112 // ---------------------------------------------------------------------------
       
   113 TBool CSvgFontHashMap::AddReferenceToFontL( const TDesC& aFontFamilyName )
       
   114     {	
       
   115     	CSvgFontMapItem* lFontMapItem = GetFontMapItem( aFontFamilyName );
       
   116     	
       
   117     	if (lFontMapItem)
       
   118     	{
       
   119     		//already a reference to this Font in the Font map
       
   120     		lFontMapItem->IncrementReferenceCount();
       
   121     		return ETrue;
       
   122     	}
       
   123     	else
       
   124     	{
       
   125     		return EFalse;
       
   126 		}
       
   127         
       
   128     }
       
   129 
       
   130 // --------------------------------------------------------------------------
       
   131 // TBool CSvgFontHashMap::HasFontName( const TDesC& aFontName
       
   132 // ---------------------------------------------------------------------------
       
   133 TBool CSvgFontHashMap::HasFontFamilyName( const TDesC& aFontFamilyName )
       
   134     {
       
   135         TBool lHaveFont = EFalse;
       
   136 		
       
   137 		TInt svgFontMapItemsCnt = iSvgFontMapItems.Count();
       
   138         for (TInt i=0; i < svgFontMapItemsCnt; i++)
       
   139         {
       
   140             if ( aFontFamilyName == ((CSvgFontMapItem*)iSvgFontMapItems[i])->GetFontFamilyName() )
       
   141             {
       
   142                 lHaveFont = ETrue;
       
   143             }
       
   144         }
       
   145 
       
   146         return lHaveFont;
       
   147 
       
   148     }
       
   149 
       
   150 // --------------------------------------------------------------------------
       
   151 // CSvgFontMapItem* CSvgFontHashMap::GetFontMapItem( const TDesC& aFontName
       
   152 // ---------------------------------------------------------------------------
       
   153 CSvgFontMapItem* CSvgFontHashMap::GetFontMapItem( const TDesC& aFontFamilyName )
       
   154 {
       
   155 	TInt svgFontMapItemsCnt = iSvgFontMapItems.Count();
       
   156 	for ( TInt i = 0; i < svgFontMapItemsCnt; i++ )
       
   157             {
       
   158             CSvgFontMapItem* lFontMap = (CSvgFontMapItem*)iSvgFontMapItems[i];
       
   159 
       
   160 			TInt result = aFontFamilyName.Compare( lFontMap->GetFontFamilyName() );
       
   161 			
       
   162             if ( result == 0 )
       
   163                 {
       
   164 					return lFontMap;
       
   165 				}
       
   166 			}
       
   167 	
       
   168 	return NULL;
       
   169 }
       
   170 
       
   171 // --------------------------------------------------------------------------
       
   172 // TBool CSvgDocumentImpl::RemoveReferenceFromFontL( const TDesC& aFontName )
       
   173 // returns True if there aren't any left
       
   174 // returns False if there are references to that Font left
       
   175 // ---------------------------------------------------------------------------
       
   176 TBool CSvgFontHashMap::RemoveReferenceFromFontL( const TDesC& aFontFamilyName )
       
   177     {
       
   178 
       
   179 		CSvgFontMapItem* lFontMapItem = GetFontMapItem( aFontFamilyName );
       
   180 		
       
   181 		if (lFontMapItem != NULL)
       
   182 		{
       
   183 			//we have this lFontMapElement
       
   184 			TInt aReferenceCount = lFontMapItem->DecrementReferenceCount();
       
   185 			
       
   186 			if (aReferenceCount <= 0)
       
   187 			{
       
   188 				//no more references to this Font remove it from the map
       
   189 				iSvgFontMapItems.Remove(iSvgFontMapItems.Find(lFontMapItem));
       
   190 				delete lFontMapItem;
       
   191 				return ETrue;
       
   192 			}
       
   193 		}
       
   194 		
       
   195 		return EFalse;
       
   196     }
       
   197 
       
   198 // --------------------------------------------------------------------------
       
   199 // START OF EXTERNAL SVG FONT DOCUMENT METHODS
       
   200 // --------------------------------------------------------------------------
       
   201 // TBool CSvgFontHashMap::AddFontDocument(CSvgDocumentImpl* newFontDocument, const TDesC& aFontFamily)
       
   202 // ---------------------------------------------------------------------------
       
   203 TBool CSvgFontHashMap::AddFontDocument(CSvgDocumentImpl* aFontDocument, const TDesC& aFontFamily)
       
   204 {
       
   205     if (aFontDocument == NULL)
       
   206     {
       
   207         //no document passed in
       
   208         return EFalse;
       
   209     }
       
   210 
       
   211     CSvgElementImpl* myFontElement = (CSvgElementImpl*)GetFontPtr(aFontFamily);
       
   212 
       
   213     if (myFontElement == NULL)
       
   214     {
       
   215         //no font in of this name in map so we can't add its document handle
       
   216         return EFalse;
       
   217     }
       
   218 
       
   219     if ( !HasFontDocument(aFontDocument) )
       
   220     {
       
   221             //then add it to the list of font documents
       
   222             iFontDocumentPointers.Append(aFontDocument);
       
   223 
       
   224             //and add it to the current document's font map
       
   225             TInt errorcode = KErrNone;
       
   226             TRAP(errorcode, AddFontL((CSvgFontElementImpl*)myFontElement, aFontFamily));
       
   227 
       
   228             return ETrue;
       
   229     }
       
   230 
       
   231     return EFalse;
       
   232 }
       
   233 
       
   234 // --------------------------------------------------------------------------
       
   235 // void CSvgFontHashMap::DeleteFontDocument(const TDesC& aFontFamily)
       
   236 // ---------------------------------------------------------------------------
       
   237 void CSvgFontHashMap::DeleteFontDocument(const TDesC& aFontFamily)
       
   238 {
       
   239     CSvgElementImpl* myFontElement = NULL;
       
   240 
       
   241 	TInt fontDocPointersCnt = iFontDocumentPointers.Count();
       
   242     for (TInt i=0; i < fontDocPointersCnt; i++)
       
   243     {
       
   244         CSvgDocumentImpl* myDoc = (CSvgDocumentImpl*)iFontDocumentPointers[i];
       
   245 
       
   246         myFontElement = (CSvgElementImpl*)GetFontPtr(aFontFamily);
       
   247 
       
   248         if (myFontElement != NULL)
       
   249         {
       
   250             iFontDocumentPointers.Remove(i);
       
   251             TInt error = KErrNone;
       
   252             TRAP(error,RemoveReferenceFromFontL(aFontFamily));
       
   253             if (myDoc)
       
   254             {
       
   255                 delete myDoc;
       
   256                 myDoc = NULL;
       
   257             }
       
   258         }
       
   259     }
       
   260 }
       
   261 
       
   262 // --------------------------------------------------------------------------
       
   263 // TBool CSvgFontHashMap::HasFontDocument(CSvgDocumentImpl* myDoc)
       
   264 // ---------------------------------------------------------------------------
       
   265 TBool CSvgFontHashMap::HasFontDocument(CSvgDocumentImpl* myDoc)
       
   266 {
       
   267     //check to see if we have this document pointer already
       
   268     TBool hasFontPointer = EFalse;
       
   269 
       
   270 	TInt fontDocPointersCnt = iFontDocumentPointers.Count();
       
   271     for (TInt i=0; i < fontDocPointersCnt; i++)
       
   272     {
       
   273         if ((CSvgDocumentImpl*)iFontDocumentPointers[i] == myDoc)
       
   274         {
       
   275             hasFontPointer = ETrue;
       
   276         }
       
   277     }
       
   278 
       
   279     return hasFontPointer;
       
   280 }
       
   281 
       
   282 // --------------------------------------------------------------------------
       
   283 //END OF EXTERNAL SVG FONT DOCUMENT
       
   284 // --------------------------------------------------------------------------
       
   285 
       
   286 // --------------------------------------------------------------------------
       
   287 // void CSvgFontHashMap::PrintFontHashMap( )
       
   288 // ---------------------------------------------------------------------------
       
   289 void CSvgFontHashMap::PrintFontHashMap()
       
   290 {
       
   291 	RDebug::Printf("----Font Hash Map----\n");
       
   292 	TInt svgFontMapItemsCnt = iSvgFontMapItems.Count();
       
   293 	for (TInt i=0; i< svgFontMapItemsCnt; i++)
       
   294 	{
       
   295 		CSvgFontMapItem* lFontMapItem = iSvgFontMapItems[i];
       
   296 		
       
   297 		if(lFontMapItem)
       
   298 		{
       
   299 			lFontMapItem->Print();	
       
   300 		}	
       
   301 	}
       
   302 }
       
   303 
       
   304 // --------------------------------------------------------------------------
       
   305 // CFbsBitmap* CSvgDocumentImpl::GetFontPtr( const TDesC& aFontName )
       
   306 // ---------------------------------------------------------------------------
       
   307 CSvgFontElementImpl* CSvgFontHashMap::GetFontPtr( const TDesC& aFontFamilyName )
       
   308     {
       
   309     
       
   310     CSvgFontMapItem* lFontMapItem = GetFontMapItem( aFontFamilyName );
       
   311     
       
   312     if (lFontMapItem)
       
   313     	{
       
   314     	return (lFontMapItem->GetFontPtr());
       
   315     	}
       
   316     
       
   317     	return NULL;
       
   318  
       
   319     }
       
   320 
       
   321 // ==========================================================================
       
   322 // Need method description
       
   323 // ==========================================================================
       
   324 CSvgFontMapItem* CSvgFontMapItem::NewL()
       
   325     {
       
   326     CSvgFontMapItem*   self    = new ( ELeave ) CSvgFontMapItem;
       
   327     CleanupStack::PushL( self );
       
   328     self->ConstructL();
       
   329     CleanupStack::Pop();
       
   330 
       
   331     return self;
       
   332     }
       
   333 
       
   334 // ==========================================================================
       
   335 // Need method description
       
   336 // ==========================================================================
       
   337 CSvgFontMapItem* CSvgFontMapItem::NewLC()
       
   338     {
       
   339     CSvgFontMapItem*   self    = new ( ELeave ) CSvgFontMapItem;
       
   340     CleanupStack::PushL( self );
       
   341     self->ConstructL();
       
   342 
       
   343     return self;
       
   344     }
       
   345 
       
   346 // ==========================================================================
       
   347 // Need method description
       
   348 // ==========================================================================
       
   349 void CSvgFontMapItem::ConstructL()
       
   350     {
       
   351     }
       
   352 
       
   353 // ==========================================================================
       
   354 // Need method description
       
   355 // ==========================================================================
       
   356 CSvgFontMapItem::CSvgFontMapItem()
       
   357     {
       
   358     }
       
   359 
       
   360 // ==========================================================================
       
   361 // Need method description
       
   362 // ==========================================================================
       
   363 CSvgFontMapItem::~CSvgFontMapItem()
       
   364     {
       
   365     	if (iFontFamilyName)
       
   366     	{
       
   367     		delete iFontFamilyName;
       
   368     		iFontFamilyName = NULL;
       
   369     	}
       
   370     	/*if (iFontPtr)
       
   371     	{
       
   372     		delete iFontPtr;
       
   373     	}
       
   374     	if (iFontDocumentPtr)
       
   375     	{
       
   376     		delete iFontDocumentPtr;
       
   377     	}*/
       
   378     }
       
   379 
       
   380 // ---------------------------------------------------------------------------
       
   381 //
       
   382 // ---------------------------------------------------------------------------
       
   383 CSvgFontElementImpl* CSvgFontMapItem::GetFontPtr()
       
   384     {
       
   385     return iFontPtr;
       
   386     }
       
   387 
       
   388 
       
   389 // ---------------------------------------------------------------------------
       
   390 //
       
   391 // ---------------------------------------------------------------------------
       
   392 const TDesC& CSvgFontMapItem::GetFontFamilyName()
       
   393     {
       
   394     return ( const TDesC & ) *iFontFamilyName;
       
   395     }
       
   396 
       
   397 // ---------------------------------------------------------------------------
       
   398 //
       
   399 // ---------------------------------------------------------------------------
       
   400 void CSvgFontMapItem::SetFontPtr( CSvgFontElementImpl* aFontPtr )
       
   401     {
       
   402     iFontPtr = aFontPtr;
       
   403     }
       
   404 
       
   405 
       
   406 // ---------------------------------------------------------------------------
       
   407 //
       
   408 // ---------------------------------------------------------------------------
       
   409 void CSvgFontMapItem::CopyFontFamilyName( const TDesC& aFontFamilyName )
       
   410     {
       
   411     if (iFontFamilyName)
       
   412     {
       
   413     	delete iFontFamilyName;
       
   414     	iFontFamilyName = NULL;
       
   415     }
       
   416     
       
   417     iFontFamilyName = aFontFamilyName.Alloc();
       
   418     }
       
   419 
       
   420 //-----------------------------------------------------------------------
       
   421 //
       
   422 //-----------------------------------------------------------------------
       
   423 TInt CSvgFontMapItem::IncrementReferenceCount( )
       
   424 	{
       
   425 	iFontReferenceCount++;
       
   426 	return iFontReferenceCount;
       
   427 	}
       
   428 
       
   429 //-----------------------------------------------------------------------
       
   430 //
       
   431 //-----------------------------------------------------------------------
       
   432 TInt CSvgFontMapItem::DecrementReferenceCount()
       
   433 	{
       
   434 	iFontReferenceCount--;
       
   435 	return iFontReferenceCount;
       
   436 	}
       
   437 
       
   438 //-----------------------------------------------------------------------
       
   439 //
       
   440 //-----------------------------------------------------------------------	
       
   441 void CSvgFontMapItem::Print()
       
   442 {
       
   443 	RDebug::RawPrint(iFontFamilyName->Des());
       
   444 	RDebug::Printf("ref_count = %d fontptr = %d fontdocptr = %d \n", iFontReferenceCount, iFontPtr, iFontDocumentPtr);	
       
   445 }