graphicstest/graphicstestharness/src/graphicsfontutils.cpp
branchRCL_3
changeset 163 bbf46f59e123
equal deleted inserted replaced
150:57c618273d5c 163:bbf46f59e123
       
     1 // Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Contains utility classes/functions which are
       
    15 // shared across multiple tests.
       
    16 
       
    17 #include <gdi.h>
       
    18 #include <fbs.h>
       
    19 #include <fntstore.h>
       
    20 #include <ecom/ecom.h>
       
    21 #include <ecom/implementationproxy.h>
       
    22 #include <graphics/openfontconstants.h>
       
    23 #include <graphics/openfontrasterizer.h>
       
    24 #include <test/graphicsfontutils.h>
       
    25 
       
    26 // The width of 1000 pixels, in twips
       
    27 const TInt KDefaultWidthInTwips = 11860;
       
    28 const TInt KDefaultHeightInTwips = 11860;
       
    29 
       
    30 /* 
       
    31 Used for cleanup of RImplInfoArray implementationArray 
       
    32 in CCharCodeConverter::LoadOpenFontLibraries().
       
    33 */
       
    34 void ResetAndDestroyRImplInfoPtrArray(TAny* aPtr)
       
    35     {
       
    36     RImplInfoPtrArray* array = reinterpret_cast <RImplInfoPtrArray*> (aPtr);
       
    37     array->ResetAndDestroy();
       
    38     }
       
    39 
       
    40 
       
    41 CCharCodeConverter::CCharCodeConverter()
       
    42     {
       
    43     }
       
    44 
       
    45 EXPORT_C CCharCodeConverter::~CCharCodeConverter()
       
    46     {
       
    47     if (iFont)
       
    48         {
       
    49         iFontStore->ReleaseFont(iFont);  
       
    50         }
       
    51     delete iFontStore;
       
    52     REComSession::FinalClose();
       
    53     }
       
    54 
       
    55 EXPORT_C CCharCodeConverter* CCharCodeConverter::NewL()
       
    56     {
       
    57     CCharCodeConverter* self = new(ELeave) CCharCodeConverter();
       
    58     CleanupStack::PushL(self);
       
    59     self->ConstructL();
       
    60     CleanupStack::Pop(1); // self;
       
    61     return self;
       
    62     }
       
    63 
       
    64 EXPORT_C CCharCodeConverter* CCharCodeConverter::NewLC()
       
    65     {
       
    66     CCharCodeConverter* self = new(ELeave) CCharCodeConverter();
       
    67     CleanupStack::PushL(self);
       
    68     self->ConstructL();
       
    69     return self;
       
    70     }
       
    71 
       
    72 void CCharCodeConverter::ConstructL()
       
    73     {
       
    74     // Setup fontstore...
       
    75     iFontStore = CFontStore::NewL(&User::Heap());
       
    76     iFontStore->iKPixelWidthInTwips = KDefaultWidthInTwips;
       
    77     iFontStore->iKPixelHeightInTwips = KDefaultHeightInTwips;
       
    78     //load all ecom implemented rasterizer dlls. installs the rasterizer.
       
    79     LoadOpenFontLibraries(iFontStore);
       
    80     //add any required font files
       
    81     iFontStore->LoadFontsAtStartupL();  
       
    82     }
       
    83 
       
    84 void CCharCodeConverter::LoadOpenFontLibraries(CFontStore* aFontStore)
       
    85      {
       
    86      RImplInfoPtrArray implementationArray;
       
    87      TCleanupItem cleanup(ResetAndDestroyRImplInfoPtrArray, &implementationArray);
       
    88      CleanupStack::PushL(cleanup);
       
    89      TInt error;
       
    90      TInt ecomerror;
       
    91      TInt ecomnotready;
       
    92      TUid uid = {KUidOpenFontRasterizerPlunginInterface};
       
    93  
       
    94      // Making sure that no race situation arises
       
    95      // If ECom is not ready, give it another chance and try again. if it still doesn't work 
       
    96      // after the third try, then it just carries on quietly and fails... 
       
    97      for (ecomnotready = 0; ecomnotready < 3; ecomnotready++)
       
    98          {
       
    99          TRAP(ecomerror,REComSession::ListImplementationsL(uid, implementationArray));
       
   100          if (ecomerror == KErrNone)
       
   101              {
       
   102              break;
       
   103              }
       
   104          else
       
   105              {
       
   106              ecomerror = KErrNone;
       
   107              User::After(0);
       
   108              }
       
   109          }
       
   110  
       
   111      const TInt availCount = implementationArray.Count();
       
   112      for (TInt count = 0; count < availCount; ++count)
       
   113          {
       
   114          const CImplementationInformation* info = implementationArray[count];
       
   115          TUid rasterizerUid = info->ImplementationUid();
       
   116          // Create a rasterizer
       
   117          COpenFontRasterizer* rasterizer = 0;
       
   118          TRAP(error,rasterizer = COpenFontRasterizer::NewL(rasterizerUid));
       
   119          if (!error)
       
   120              {
       
   121              // Install it in the font store.
       
   122              TRAP(error,aFontStore->InstallRasterizerL(rasterizer));
       
   123              if (error)
       
   124                  {
       
   125                  delete rasterizer;
       
   126                  }
       
   127              }
       
   128          }
       
   129      CleanupStack::PopAndDestroy(&implementationArray);  
       
   130      }
       
   131 
       
   132 /**
       
   133 Tells the converter which font object is to be used when 
       
   134 converting character codes to glyph codes.
       
   135 @param aFont The font to use for conversion of codes
       
   136  */
       
   137 EXPORT_C void CCharCodeConverter::UseFontL(CFbsFont* aFont)
       
   138     {
       
   139     User::LeaveIfNull(iFontStore);
       
   140     if (iFont)
       
   141         {
       
   142         iFontStore->ReleaseFont(iFont);
       
   143         iFont = NULL;
       
   144         iGlyphIndexExt = NULL;
       
   145         }
       
   146     // Setup font interface...
       
   147     TFontSpec testFontSpec(aFont->FontSpecInTwips().iTypeface.Name(), aFont->HeightInPixels()); 
       
   148     CFont* cfont = 0;
       
   149     iFontStore->GetNearestFontToDesignHeightInPixels(*&cfont, testFontSpec);    
       
   150     iFont = (CBitmapFont*)cfont;
       
   151     User::LeaveIfNull(iFont);
       
   152     TAny* ext = 0;
       
   153     // This is the interface used to get the glyph code index from the rasterizer..
       
   154     iFont->OpenFont()->ExtendedInterface(KUidOpenFontShapingExtension, ext);
       
   155     iGlyphIndexExt = reinterpret_cast<MOpenFontShapingExtension*>(ext);
       
   156     User::LeaveIfNull(ext);
       
   157     }
       
   158 
       
   159 /**
       
   160 Returns the glyph code for the given character code, for the 
       
   161 font that this object was constructed with. No shaping of
       
   162 the characters is performed by this function.
       
   163 @param aCharCode The character code to request the glyph code for.
       
   164 @return The glyph code
       
   165  */
       
   166 EXPORT_C TInt CCharCodeConverter::GlyphCodeL(TInt aCharCode) const
       
   167     {
       
   168     // If a leave occurs, it's because UseFontL() has
       
   169     // not been successfully called.
       
   170     User::LeaveIfNull(iGlyphIndexExt);
       
   171     TInt glyphCode = iGlyphIndexExt->GlyphIndex(aCharCode);
       
   172     return glyphCode;
       
   173     }
       
   174 
       
   175