pictographs/AknPictograph/src/AknPictographFactory.cpp
changeset 0 05e9090e2422
equal deleted inserted replaced
-1:000000000000 0:05e9090e2422
       
     1 /*
       
     2 * Copyright (c) 2002 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:  Pictograph drawer
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "AknPictographFactory.h"
       
    22 #include "AknPictographConstants.h"
       
    23 #include "AknPictographDefinitions.h"
       
    24 #include "AknStaticPictographDefKey.h"
       
    25 #include "AknAnimatedPictographDefKey.h"
       
    26 
       
    27 #include <fbs.h>
       
    28 #include <gdi.h>
       
    29 #include <bitdev.h>
       
    30 
       
    31 
       
    32 
       
    33 
       
    34 // CONSTANTS
       
    35 
       
    36 // ============================ MEMBER FUNCTIONS ===============================
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // AknPictographFactory::SupportedPictographCodesL
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 HBufC* AknPictographFactory::SupportedPictographCodesL()
       
    43     {
       
    44     // static pictographs
       
    45     const TAknStaticPictographDefinition* staticTable = 
       
    46         TheStaticPictographDefinitions;
       
    47 
       
    48     // animated pictographs
       
    49     const TAknAnimatedPictographDefinition* animatedTable = 
       
    50         TheAnimatedPictographDefinitions;
       
    51 
       
    52     TInt numPictographs = KAknPictographStaticPictographsCount +
       
    53                           KAknPictographAnimatedPictographsCount;
       
    54 
       
    55     RArray<TUint> array( numPictographs );
       
    56     CleanupClosePushL( array );
       
    57 
       
    58     // append static pictograph codes
       
    59     for ( TInt i = 0 ; i < KAknPictographStaticPictographsCount ; i++ )
       
    60         {
       
    61         User::LeaveIfError( array.Append( staticTable[i].iCode ) );
       
    62         }
       
    63 
       
    64     // append animated pictograph codes
       
    65     for ( TInt j = 0 ; j < KAknPictographAnimatedPictographsCount ; j++ )
       
    66         {
       
    67         User::LeaveIfError( array.Append( animatedTable[j].iCode ) );
       
    68         }
       
    69 
       
    70     // sort
       
    71     array.Sort();
       
    72 
       
    73     // copy to descriptor
       
    74     HBufC* buf = HBufC::NewMaxL( numPictographs );
       
    75     TText* codes = const_cast<TText*>( buf->Ptr() );
       
    76 
       
    77     for ( TInt k = 0 ; k < numPictographs ; k++ )
       
    78         {
       
    79         codes[k] = static_cast<TText>( array[k] );
       
    80         }
       
    81 
       
    82     CleanupStack::PopAndDestroy(); // array
       
    83     return buf;
       
    84     }
       
    85 
       
    86 // -----------------------------------------------------------------------------
       
    87 // AknPictographFactory::StaticPictographData
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 TInt AknPictographFactory::StaticPictographData( 
       
    91     TText aCode, TAknPictographHeight aHeight, TAknPictographData& aData )
       
    92     {
       
    93     // Search static pictograph table.
       
    94     TAknStaticPictographDefKey staticDefKey;
       
    95     staticDefKey.SetPtr( &aCode );
       
    96     TInt pos;
       
    97 
       
    98     TInt ret = User::BinarySearch( KAknPictographStaticPictographsCount,
       
    99                                    staticDefKey,
       
   100                                    pos );
       
   101 
       
   102     const TAknStaticPictographDefinition* def = NULL;
       
   103 
       
   104     if ( ret == KErrNone )
       
   105         {
       
   106         def = &( TheStaticPictographDefinitions[pos] );
       
   107 
       
   108         TUint index = 0;
       
   109         ret = GetArrayIndexForHeight(aHeight,index);
       
   110 
       
   111         if ( ret == KErrNone )
       
   112             {
       
   113             aData.iOffset = def->iMetrics[index].iX;
       
   114             aData.iSize.iWidth = def->iMetrics[index].iWidth;
       
   115             aData.iSize.iHeight = def->iMetrics[index].iHeight;
       
   116             aData.iAnimated = EFalse;
       
   117             }
       
   118         }
       
   119 
       
   120     return ret;
       
   121     }
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 // AknPictographFactory::AnimatedPictographData
       
   125 // -----------------------------------------------------------------------------
       
   126 //
       
   127 TInt AknPictographFactory::AnimatedPictographData(
       
   128     TText aCode,
       
   129     TAknPictographHeight aHeight,
       
   130     TUint aCounter,
       
   131     TAknPictographData& aData )
       
   132     {
       
   133     // Search animated pictograph table.
       
   134     TAknAnimatedPictographDefKey animatedDefKey;
       
   135     animatedDefKey.SetPtr( &aCode );
       
   136     TInt pos;
       
   137 
       
   138     TInt ret = User::BinarySearch( KAknPictographAnimatedPictographsCount,
       
   139                                    animatedDefKey,
       
   140                                    pos );
       
   141 
       
   142     const TAknAnimatedPictographDefinition* def = NULL;
       
   143 
       
   144     if ( ret == KErrNone )
       
   145         {
       
   146         def = &( TheAnimatedPictographDefinitions[pos] );
       
   147 
       
   148 
       
   149         TUint index = 0;
       
   150         ret = GetArrayIndexForHeight(aHeight,index);
       
   151 
       
   152         if ( ret == KErrNone )
       
   153             {
       
   154             TInt frame = aCounter % def->iFramesDefinitions[index].iFrames;
       
   155 
       
   156             const TAknPictographMetrics* metrics = def->iFramesDefinitions[index].iMetrics;
       
   157             aData.iOffset = metrics[frame].iX;
       
   158             aData.iSize.iWidth = metrics[frame].iWidth;
       
   159             aData.iSize.iHeight = metrics[frame].iHeight;
       
   160             aData.iAnimated = ETrue;
       
   161             }
       
   162         }
       
   163 
       
   164     return ret;
       
   165     }
       
   166 
       
   167 // -----------------------------------------------------------------------------
       
   168 // AknPictographFactory::StaticPictographTable
       
   169 // -----------------------------------------------------------------------------
       
   170 //
       
   171 const TAknStaticPictographDefinition* 
       
   172     AknPictographFactory::StaticPictographTable()
       
   173     {
       
   174     return TheStaticPictographDefinitions;
       
   175     }
       
   176 
       
   177 // -----------------------------------------------------------------------------
       
   178 // AknPictographFactory::AnimatedPictographTable
       
   179 // -----------------------------------------------------------------------------
       
   180 //
       
   181 const TAknAnimatedPictographDefinition* 
       
   182     AknPictographFactory::AnimatedPictographTable()
       
   183     {
       
   184     return TheAnimatedPictographDefinitions;
       
   185     }
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // AknPictographFactory::LoadBitmapsL
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 void AknPictographFactory::LoadBitmapsL(
       
   192             RPointerArray<CFbsBitmap>*& aBitmaps,
       
   193             RPointerArray<CFbsBitmap>*& aMasks
       
   194             )
       
   195     {
       
   196     // Allocates array object and sets proper granularity values
       
   197     aBitmaps = new(ELeave) RPointerArray<CFbsBitmap>(KAknPictographSupportedHeightsCount);
       
   198     aMasks = new(ELeave) RPointerArray<CFbsBitmap>(KAknPictographSupportedHeightsCount);
       
   199 
       
   200     TFileName filename;
       
   201     CFbsBitmap* bitmap = NULL;
       
   202 
       
   203     // Loads supported bitamps and masks
       
   204     for (TInt i = 0; i < KAknPictographSupportedHeightsCount; i++)
       
   205         {
       
   206         // Load bitmap and append to aBitmaps
       
   207         filename = KBitmapFileNameBase;
       
   208         filename.AppendNum( TAknPictographSupportedHeightsNumbers[i] );
       
   209         filename.Append( KExtension );
       
   210         bitmap = new( ELeave ) CFbsBitmap;
       
   211         CleanupStack::PushL( bitmap );
       
   212         User::LeaveIfError( aBitmaps->Append(bitmap) );
       
   213         CleanupStack::Pop( bitmap );
       
   214         User::LeaveIfError( bitmap->Load( filename ) );
       
   215 
       
   216         // Load bitmap mask and append to aBitmaps
       
   217         filename = KMaskFileNameBase;
       
   218         filename.AppendNum( TAknPictographSupportedHeightsNumbers[i] );
       
   219         filename.Append( KExtension );
       
   220         bitmap = new( ELeave ) CFbsBitmap;
       
   221         CleanupStack::PushL( bitmap );
       
   222         User::LeaveIfError( aMasks->Append(bitmap) );
       
   223         CleanupStack::Pop( bitmap );
       
   224         User::LeaveIfError( bitmap->Load( filename ) );
       
   225         }
       
   226     }
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 // AknPictographFactory::CreateWhiteBitmapsL
       
   230 // -----------------------------------------------------------------------------
       
   231 //
       
   232 void AknPictographFactory::CreateWhiteBitmapsL(
       
   233             CFbsBitmap*& aBitmap,
       
   234             CFbsBitmap*& aMask,
       
   235             CFbsBitmapDevice*& aMaskBitmapDevice,
       
   236             CFbsBitGc*& aMaskGc
       
   237             )
       
   238     {
       
   239     __ASSERT_DEBUG( KAknPictographSupportedHeightsCount > 0, User::Invariant() );
       
   240     // Note: TAknPictographSupportedHeightsNumbers should contain height numbers
       
   241     // ascending order
       
   242     // The size estimate below is derived from the heights of the biggest supported pictograph
       
   243     const TInt sideLenght = TAknPictographSupportedHeightsNumbers[KAknPictographSupportedHeightsCount-1]*2;
       
   244     const TSize whiteBitmapSize(sideLenght,sideLenght);
       
   245 
       
   246     // create white bitmap
       
   247     CFbsBitmap* bitmap = new (ELeave) CFbsBitmap();
       
   248     CleanupStack::PushL(bitmap);
       
   249     User::LeaveIfError( bitmap->Create( whiteBitmapSize, EGray2 ) );
       
   250     CFbsBitmapDevice* bitmapDev = CFbsBitmapDevice::NewL( bitmap );
       
   251     CleanupStack::PushL( bitmapDev );
       
   252     CFbsBitGc* bitmapGc = NULL;
       
   253     User::LeaveIfError( bitmapDev->CreateContext( bitmapGc ) );
       
   254     CleanupStack::PushL( bitmapGc );
       
   255 
       
   256     // fill the whole bitmap with white color
       
   257     bitmapGc->SetBrushColor(KRgbWhite);
       
   258     bitmapGc->SetBrushStyle(CGraphicsContext::ESolidBrush);
       
   259     bitmapGc->Clear();
       
   260     CleanupStack::PopAndDestroy(2); // bitmapDev, bitmapGc
       
   261 
       
   262     // create the mask (to be filled later)
       
   263     CFbsBitmap* mask = new (ELeave) CFbsBitmap();
       
   264     CleanupStack::PushL(mask);
       
   265     User::LeaveIfError( mask->Create( whiteBitmapSize, EGray2 ) );
       
   266     CFbsBitmapDevice* maskDev = CFbsBitmapDevice::NewL( mask );
       
   267     CleanupStack::PushL( maskDev );
       
   268     CFbsBitGc* maskGc = NULL;
       
   269     User::LeaveIfError( maskDev->CreateContext( maskGc ) );
       
   270 
       
   271     // fill the whole are of mask with white color
       
   272     maskGc->SetBrushColor(KRgbWhite);
       
   273     maskGc->SetBrushStyle(CGraphicsContext::ESolidBrush);
       
   274     maskGc->Clear();
       
   275 
       
   276     CleanupStack::Pop(3); // bitmap, mask, maskDev
       
   277     aBitmap = bitmap;
       
   278     aMask = mask;
       
   279     aMaskBitmapDevice = maskDev;
       
   280     aMaskGc = maskGc;
       
   281     }
       
   282 
       
   283 // -----------------------------------------------------------------------------
       
   284 // AknPictographFactory::GetArrayIndexForHeight
       
   285 // -----------------------------------------------------------------------------
       
   286 //
       
   287 TInt AknPictographFactory::GetArrayIndexForHeight(
       
   288             TAknPictographHeight aHeight,
       
   289             TUint& aIndex
       
   290             )
       
   291     {
       
   292     TInt ret = KErrNotSupported;
       
   293 
       
   294     for (TInt i = 0; i < KAknPictographSupportedHeightsCount; i++)
       
   295         {
       
   296         if (TAknPictographSupportedHeights[i] == aHeight)
       
   297             {
       
   298             aIndex = i;
       
   299             ret = KErrNone;
       
   300             break;
       
   301             }
       
   302         }
       
   303     return ret;
       
   304     }
       
   305 
       
   306 // -----------------------------------------------------------------------------
       
   307 // AknPictographFactory::SelectPictographHeightForFont
       
   308 // -----------------------------------------------------------------------------
       
   309 //
       
   310 TInt AknPictographFactory::SelectPictographHeightForFont(
       
   311             const CFont& aFont,
       
   312             TAknPictographHeight& aHeight
       
   313             )
       
   314     {
       
   315     TInt ret = KErrNotSupported;
       
   316     TInt fontHeight = aFont.HeightInPixels();
       
   317     TInt i;
       
   318 
       
   319     // Search for the first height that is bigger
       
   320     // Note: TAknPictographSupportedHeightsNumbers should contain height numbers
       
   321     // ascending order
       
   322     for (i = 0; i < KAknPictographSupportedHeightsCount; i++)
       
   323         {
       
   324         if ((TUint)fontHeight < TAknPictographSupportedHeightsNumbers[i])
       
   325             {
       
   326             break;
       
   327             }
       
   328         }
       
   329 
       
   330     // The previous height should be the best match
       
   331     // Use the biggest supported pictograph height in case the font height is
       
   332     // bigger
       
   333     TInt index = i-1;
       
   334     if (index >= 0)
       
   335         {
       
   336         aHeight = TAknPictographSupportedHeights[index];
       
   337         ret = KErrNone;
       
   338         }
       
   339 
       
   340     return ret;
       
   341     }
       
   342 
       
   343 
       
   344 
       
   345 //  End of File