phonebookui/Phonebook2/Presentation/src/Pbk2PresentationUtils.cpp
branchRCL_3
changeset 63 f4a778e096c2
child 64 c1e8ba0c2b16
child 74 6b5524b4f673
equal deleted inserted replaced
62:5b6f26637ad3 63:f4a778e096c2
       
     1 /*
       
     2 * Copyright (c) 2002-2007 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 FILES
       
    20 #include "Pbk2PresentationUtils.h"
       
    21 
       
    22 // From system
       
    23 #include <featmgr.h>
       
    24 #include <fbs.h>
       
    25 
       
    26 /// Namespace for local PresentationUtils definitions
       
    27 namespace __RVCT_UNS_Pbk2PresentationUtils {
       
    28 
       
    29 // LOCAL CONSTANTS
       
    30 enum TPanicCode
       
    31     {
       
    32     EPanicPreCond_CopyAndReplaceChars = 1
       
    33     };
       
    34 
       
    35 // default presentation resource file
       
    36 _LIT( KPbk2PresentationResFile, "Pbk2Presentation.rsc" );
       
    37 // Chinese presentation resource file 
       
    38 _LIT( KPbk2PresentationResFileChinese, "Pbk2PresentationChinese.rsc" );
       
    39 
       
    40 // ================= LOCAL FUNCTIONS =======================
       
    41 #ifdef _DEBUG
       
    42 void Panic(TPanicCode aReason)
       
    43     {
       
    44     _LIT(KPanicText, "Pbk2PresentationUtils");
       
    45     User::Panic(KPanicText, aReason);
       
    46     }
       
    47 #endif // _DEBUG
       
    48 
       
    49 // Zero Width characters
       
    50 const TUint KZWSCharacter = 0x200B;
       
    51 const TUint KZWNJCharacter = 0x200C;
       
    52 const TUint KZWJCharacter = 0x200D;
       
    53 
       
    54 // ================= LOCAL FUNCTIONS =======================
       
    55 
       
    56 /** 
       
    57  * Check if the given char is a zero width character: 
       
    58  * Zero Width Space, Zero Width Non-Joiner
       
    59  * or Zero Width Joiner character
       
    60  * @param aChar Given character
       
    61  * @return ETrue if aChar is zero width character.
       
    62  *         EFalse if aChar is not specified zero width character.
       
    63  */
       
    64 inline TBool ZWSCharacter( const TChar aChar )
       
    65     {
       
    66     const TChar zwsChar( KZWSCharacter );
       
    67     const TChar zwnjChar( KZWNJCharacter );
       
    68     const TChar zwjChar( KZWJCharacter );
       
    69     return ( aChar == zwsChar ) || ( aChar == zwnjChar ) ||
       
    70            ( aChar == zwjChar );
       
    71     }
       
    72       
       
    73 } // __RVCT_UNS_Pbk2PresentationUtils
       
    74 
       
    75 using namespace __RVCT_UNS_Pbk2PresentationUtils;
       
    76 
       
    77 
       
    78 // ================= MEMBER FUNCTIONS =======================
       
    79 
       
    80 // --------------------------------------------------------------------------
       
    81 // Pbk2PresentationUtils::TrimAllAppend
       
    82 // --------------------------------------------------------------------------
       
    83 //
       
    84 TInt Pbk2PresentationUtils::TrimAllAppend(const TDesC& aText, TDes& aDest)
       
    85     {
       
    86     const TInt oldDestLength = aDest.Length();
       
    87 
       
    88     // 1. Find first non space character
       
    89     const TInt textLength = aText.Length();
       
    90     TInt firstNonSpaceChar = 0;
       
    91     while(firstNonSpaceChar < textLength
       
    92         && TChar(aText[firstNonSpaceChar]).IsSpace())
       
    93         {
       
    94         ++firstNonSpaceChar;
       
    95         }
       
    96 
       
    97     // 2. Append the source text to destination,
       
    98     // not including the leading spaces
       
    99     aDest.Append(aText.Mid(firstNonSpaceChar));
       
   100     // 3. Trim the new part of the destination to remove
       
   101     // trailing and middle spaces
       
   102     TPtr ptr = aDest.MidTPtr(oldDestLength);
       
   103     ptr.TrimAll();
       
   104     aDest.SetLength(oldDestLength);
       
   105     aDest.Append(ptr);
       
   106 
       
   107     return aDest.Length() - oldDestLength;
       
   108     }
       
   109 
       
   110 // --------------------------------------------------------------------------
       
   111 // Pbk2PresentationUtils::TrimRightAppend
       
   112 // --------------------------------------------------------------------------
       
   113 //
       
   114 EXPORT_C TInt Pbk2PresentationUtils::TrimRightAppend(
       
   115         const TDesC& aText, TDes& aDest)
       
   116     {
       
   117     // Create modifiable descriptor pointer that points to the non-used space
       
   118     // at the end of the destination buffer
       
   119     TPtr temp(
       
   120             (TUint16*) aDest.MidTPtr( aDest.Length() ).Ptr(),
       
   121             aDest.MaxLength() - aDest.Length() );
       
   122     temp.Append( aText );
       
   123     temp.TrimRight();
       
   124     // Update the destination descriptor length to match the actual contents
       
   125     aDest.SetLength( aDest.Length() + temp.Length() );
       
   126     return temp.Length();
       
   127     }
       
   128     
       
   129 // --------------------------------------------------------------------------
       
   130 // Pbk2PresentationUtils::AppendAndReplaceChars
       
   131 // --------------------------------------------------------------------------
       
   132 //
       
   133 EXPORT_C TInt Pbk2PresentationUtils::AppendAndReplaceChars
       
   134         (TDes& aDest, const TDesC& aSrc,
       
   135         const TDesC& aCharsToReplace, const TDesC& aReplaceChars)
       
   136     {
       
   137     // Check that aDest has enough capacity
       
   138     __ASSERT_DEBUG((aDest.MaxLength()-aDest.Length()) >= aSrc.Length(),
       
   139         Panic(EPanicPreCond_CopyAndReplaceChars));
       
   140     // Check that a replacament is provided for all characters
       
   141     __ASSERT_DEBUG(aCharsToReplace.Length() == aReplaceChars.Length(),
       
   142         Panic(EPanicPreCond_CopyAndReplaceChars));
       
   143 
       
   144     TInt count, i;
       
   145     const TInt sourceLenght = aSrc.Length();
       
   146     for (count=0, i=0; i < sourceLenght; ++i)
       
   147         {
       
   148         TText ch = aSrc[i];
       
   149         const TInt replacePos = aCharsToReplace.Locate(aSrc[i]);
       
   150         if (replacePos != KErrNotFound)
       
   151             {
       
   152             ++count;
       
   153             ch = aReplaceChars[replacePos];
       
   154             }
       
   155         aDest.Append(ch);
       
   156         }
       
   157 
       
   158     return count;
       
   159     }
       
   160     
       
   161 // --------------------------------------------------------------------------
       
   162 // Pbk2PresentationUtils::ReplaceNonGraphicCharacters
       
   163 // --------------------------------------------------------------------------
       
   164 //
       
   165 EXPORT_C void Pbk2PresentationUtils::ReplaceNonGraphicCharacters
       
   166         (TDes& aText, TText aChar)
       
   167     {
       
   168     const TInt len = aText.Length();
       
   169     for ( TInt i=0; i < len; ++i )
       
   170         {
       
   171         if ( !TChar(aText[i]).IsGraph() &&
       
   172              !ZWSCharacter( aText[i] ) )
       
   173             {
       
   174             // If non-graphic char is specified in ZWSCharacter,
       
   175             // it will not be replaced. Otherwise replace non-graphic
       
   176             // character with aChar.
       
   177                 aText[i] = aChar;
       
   178             }
       
   179         }
       
   180     }
       
   181 
       
   182 // --------------------------------------------------------------------------
       
   183 // Pbk2PresentationUtils::AppendWithNewlineTranslationL
       
   184 // --------------------------------------------------------------------------
       
   185 //
       
   186 EXPORT_C void Pbk2PresentationUtils::AppendWithNewlineTranslationL
       
   187         ( TDes& aBuffer, const TDesC& aText )
       
   188     {
       
   189     const TUint16 KCarriageReturn = 0x000d;
       
   190     const TUint16 KLineFeed = 0x000a;
       
   191     const TUint16 KParagraphSeparator = 0x2029;
       
   192     
       
   193     const TInt length( aText.Length() );
       
   194     const TInt max( aBuffer.MaxLength() );
       
   195     
       
   196     for ( register TInt i = 0;
       
   197           (i < length) && (aBuffer.Length() < max); ++i )
       
   198         {
       
   199         register TUint16 ch = aText[i];
       
   200         if ( ch == KCarriageReturn )
       
   201             {
       
   202             ch = KParagraphSeparator;
       
   203             // Skip linefeeds that follow carriage return
       
   204             if ( (i < length-1) && (aText[i+1] == KLineFeed) )
       
   205                 {
       
   206                 ++i;
       
   207                 }
       
   208             }
       
   209         else if ( ch == KLineFeed )
       
   210             {
       
   211             ch = KParagraphSeparator;
       
   212             }
       
   213         aBuffer.Append( ch );
       
   214         }
       
   215     }
       
   216     
       
   217 // --------------------------------------------------------------------------
       
   218 // Pbk2PresentationUtils::PresentationResourceFile
       
   219 // --------------------------------------------------------------------------
       
   220 //
       
   221 EXPORT_C const TDesC& Pbk2PresentationUtils::PresentationResourceFile()
       
   222     {
       
   223     if (FeatureManager::FeatureSupported(KFeatureIdChinese))
       
   224         {
       
   225         return KPbk2PresentationResFileChinese;
       
   226         }
       
   227     return KPbk2PresentationResFile;
       
   228     }    
       
   229 
       
   230 
       
   231 // --------------------------------------------------------------------------
       
   232 // Pbk2PresentationImageUtils::CropImageL
       
   233 // --------------------------------------------------------------------------
       
   234 //
       
   235 EXPORT_C void Pbk2PresentationImageUtils::CropImageL( 
       
   236         CFbsBitmap& aBitmap, 
       
   237         TCroppingMode aCroppingMode, 
       
   238         const TSize& aTargetSize  )
       
   239     {
       
   240     const TSize sourceSize( aBitmap.SizeInPixels() );
       
   241     // crop the image only if the width is bigger than height
       
   242     TBool landscape( ETrue );
       
   243     if( sourceSize.iHeight >= sourceSize.iWidth )
       
   244         {
       
   245         // portrait image
       
   246         landscape = EFalse;
       
   247         }
       
   248     // take the shorter side
       
   249     const TInt sideSizeH( 
       
   250             landscape ? sourceSize.iHeight : sourceSize.iWidth );
       
   251     const TInt targetSizeW( 
       
   252             landscape ? aTargetSize.iWidth : aTargetSize.iHeight );
       
   253     TInt sideSizeW( sideSizeH );   
       
   254     if( EOptimizedCropping == aCroppingMode &&
       
   255         sideSizeW < targetSizeW )
       
   256         {
       
   257         sideSizeW = targetSizeW;
       
   258         if( sideSizeW >= 
       
   259                 ( landscape ? sourceSize.iWidth : sourceSize.iHeight ) )
       
   260             {
       
   261             return; // no cropping
       
   262             }
       
   263         }
       
   264     
       
   265     // set target size
       
   266     const TSize targetSize( landscape? 
       
   267             TSize(sideSizeW, sideSizeH ): TSize( sideSizeH, sideSizeW ) );
       
   268 
       
   269     // crop from both sides
       
   270     const TPoint targetPoint( ( sourceSize.iWidth - targetSize.iWidth ) / 2,
       
   271                               ( sourceSize.iHeight - targetSize.iHeight ) / 2 );
       
   272     const TRect targetRect( targetPoint, targetSize );
       
   273     
       
   274     // create new bitmap
       
   275     CFbsBitmap* target = new( ELeave ) CFbsBitmap;
       
   276     CleanupStack::PushL( target );
       
   277     const TDisplayMode displayMode( aBitmap.DisplayMode() );
       
   278     User::LeaveIfError( target->Create( targetSize, displayMode ) );
       
   279     
       
   280     // get scanline
       
   281     HBufC8* scanLine = HBufC8::NewLC( aBitmap.ScanLineLength
       
   282         ( targetSize.iWidth, displayMode ) );
       
   283     TPtr8 scanLinePtr = scanLine->Des();
       
   284     
       
   285     TPoint startPoint( targetRect.iTl.iX, targetRect.iTl.iY );
       
   286     TInt targetY = 0;
       
   287     for (; startPoint.iY < targetRect.iBr.iY; ++startPoint.iY )
       
   288         {
       
   289         aBitmap.GetScanLine( 
       
   290                 scanLinePtr, startPoint, targetSize.iWidth, displayMode );
       
   291         target->SetScanLine( scanLinePtr, targetY++ );
       
   292         }
       
   293 
       
   294     aBitmap.Reset();
       
   295     User::LeaveIfError( aBitmap.Duplicate( target->Handle() ) );
       
   296     CleanupStack::PopAndDestroy( 2, target ); // scanLine, target    
       
   297     }    
       
   298 
       
   299 // End of File