sl@0: // Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // Contains utility classes/functions which are sl@0: // shared across multiple tests. sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: // The width of 1000 pixels, in twips sl@0: const TInt KDefaultWidthInTwips = 11860; sl@0: const TInt KDefaultHeightInTwips = 11860; sl@0: sl@0: /* sl@0: Used for cleanup of RImplInfoArray implementationArray sl@0: in CCharCodeConverter::LoadOpenFontLibraries(). sl@0: */ sl@0: void ResetAndDestroyRImplInfoPtrArray(TAny* aPtr) sl@0: { sl@0: RImplInfoPtrArray* array = reinterpret_cast (aPtr); sl@0: array->ResetAndDestroy(); sl@0: } sl@0: sl@0: sl@0: CCharCodeConverter::CCharCodeConverter() sl@0: { sl@0: } sl@0: sl@0: EXPORT_C CCharCodeConverter::~CCharCodeConverter() sl@0: { sl@0: if (iFont) sl@0: { sl@0: iFontStore->ReleaseFont(iFont); sl@0: } sl@0: delete iFontStore; sl@0: REComSession::FinalClose(); sl@0: } sl@0: sl@0: EXPORT_C CCharCodeConverter* CCharCodeConverter::NewL() sl@0: { sl@0: CCharCodeConverter* self = new(ELeave) CCharCodeConverter(); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(); sl@0: CleanupStack::Pop(1); // self; sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CCharCodeConverter* CCharCodeConverter::NewLC() sl@0: { sl@0: CCharCodeConverter* self = new(ELeave) CCharCodeConverter(); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(); sl@0: return self; sl@0: } sl@0: sl@0: void CCharCodeConverter::ConstructL() sl@0: { sl@0: // Setup fontstore... sl@0: iFontStore = CFontStore::NewL(&User::Heap()); sl@0: iFontStore->iKPixelWidthInTwips = KDefaultWidthInTwips; sl@0: iFontStore->iKPixelHeightInTwips = KDefaultHeightInTwips; sl@0: //load all ecom implemented rasterizer dlls. installs the rasterizer. sl@0: LoadOpenFontLibraries(iFontStore); sl@0: //add any required font files sl@0: iFontStore->LoadFontsAtStartupL(); sl@0: } sl@0: sl@0: void CCharCodeConverter::LoadOpenFontLibraries(CFontStore* aFontStore) sl@0: { sl@0: RImplInfoPtrArray implementationArray; sl@0: TCleanupItem cleanup(ResetAndDestroyRImplInfoPtrArray, &implementationArray); sl@0: CleanupStack::PushL(cleanup); sl@0: TInt error; sl@0: TInt ecomerror; sl@0: TInt ecomnotready; sl@0: TUid uid = {KUidOpenFontRasterizerPlunginInterface}; sl@0: sl@0: // Making sure that no race situation arises sl@0: // If ECom is not ready, give it another chance and try again. if it still doesn't work sl@0: // after the third try, then it just carries on quietly and fails... sl@0: for (ecomnotready = 0; ecomnotready < 3; ecomnotready++) sl@0: { sl@0: TRAP(ecomerror,REComSession::ListImplementationsL(uid, implementationArray)); sl@0: if (ecomerror == KErrNone) sl@0: { sl@0: break; sl@0: } sl@0: else sl@0: { sl@0: ecomerror = KErrNone; sl@0: User::After(0); sl@0: } sl@0: } sl@0: sl@0: const TInt availCount = implementationArray.Count(); sl@0: for (TInt count = 0; count < availCount; ++count) sl@0: { sl@0: const CImplementationInformation* info = implementationArray[count]; sl@0: TUid rasterizerUid = info->ImplementationUid(); sl@0: // Create a rasterizer sl@0: COpenFontRasterizer* rasterizer = 0; sl@0: TRAP(error,rasterizer = COpenFontRasterizer::NewL(rasterizerUid)); sl@0: if (!error) sl@0: { sl@0: // Install it in the font store. sl@0: TRAP(error,aFontStore->InstallRasterizerL(rasterizer)); sl@0: if (error) sl@0: { sl@0: delete rasterizer; sl@0: } sl@0: } sl@0: } sl@0: CleanupStack::PopAndDestroy(&implementationArray); sl@0: } sl@0: sl@0: /** sl@0: Tells the converter which font object is to be used when sl@0: converting character codes to glyph codes. sl@0: @param aFont The font to use for conversion of codes sl@0: */ sl@0: EXPORT_C void CCharCodeConverter::UseFontL(CFbsFont* aFont) sl@0: { sl@0: User::LeaveIfNull(iFontStore); sl@0: if (iFont) sl@0: { sl@0: iFontStore->ReleaseFont(iFont); sl@0: iFont = NULL; sl@0: iGlyphIndexExt = NULL; sl@0: } sl@0: // Setup font interface... sl@0: TFontSpec testFontSpec(aFont->FontSpecInTwips().iTypeface.Name(), aFont->HeightInPixels()); sl@0: CFont* cfont = 0; sl@0: iFontStore->GetNearestFontToDesignHeightInPixels(*&cfont, testFontSpec); sl@0: iFont = (CBitmapFont*)cfont; sl@0: User::LeaveIfNull(iFont); sl@0: TAny* ext = 0; sl@0: // This is the interface used to get the glyph code index from the rasterizer.. sl@0: iFont->OpenFont()->ExtendedInterface(KUidOpenFontShapingExtension, ext); sl@0: iGlyphIndexExt = reinterpret_cast(ext); sl@0: User::LeaveIfNull(ext); sl@0: } sl@0: sl@0: /** sl@0: Returns the glyph code for the given character code, for the sl@0: font that this object was constructed with. No shaping of sl@0: the characters is performed by this function. sl@0: @param aCharCode The character code to request the glyph code for. sl@0: @return The glyph code sl@0: */ sl@0: EXPORT_C TInt CCharCodeConverter::GlyphCodeL(TInt aCharCode) const sl@0: { sl@0: // If a leave occurs, it's because UseFontL() has sl@0: // not been successfully called. sl@0: User::LeaveIfNull(iGlyphIndexExt); sl@0: TInt glyphCode = iGlyphIndexExt->GlyphIndex(aCharCode); sl@0: return glyphCode; sl@0: } sl@0: sl@0: