os/graphics/graphicsdeviceinterface/bitgdi/tbit/TFontSelect.CPP
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
/**
sl@0
    17
 @file
sl@0
    18
 @test
sl@0
    19
 @internalComponent - Internal Symbian test code 
sl@0
    20
*/
sl@0
    21
sl@0
    22
#include <e32hal.h>
sl@0
    23
#include <e32std.h>
sl@0
    24
#include <e32test.h>
sl@0
    25
#include <bitstd.h>
sl@0
    26
#include <bitdev.h>
sl@0
    27
#include <fntstore.h>
sl@0
    28
#include <fbs.h>
sl@0
    29
#include <hal.h>
sl@0
    30
#include <graphics/fbsdefs.h>
sl@0
    31
sl@0
    32
#include "tfontselect.h"
sl@0
    33
sl@0
    34
sl@0
    35
//If you want to do font tests with scaled device - change KScalingFactorX, KScalingFactorY, KScalingOriginPt.
sl@0
    36
const TInt KScalingFactorX = 1;
sl@0
    37
const TInt KScalingFactorY = 1;
sl@0
    38
const TPoint KScalingOriginPt(0, 0);
sl@0
    39
sl@0
    40
//Non valid font --> This font comes from System Test test data. Not a proper font, so it's best to skip it.
sl@0
    41
_LIT(KWrongFontFileName, "fnt");
sl@0
    42
sl@0
    43
sl@0
    44
CTFontSelect::CTFontSelect(CTestStep* aStep) :
sl@0
    45
	CTGraphicsBase(aStep),
sl@0
    46
	iFont1(NULL),
sl@0
    47
	iFont2(NULL),
sl@0
    48
	iFont3(NULL),
sl@0
    49
	iGc(NULL),
sl@0
    50
	iDevice(NULL)	
sl@0
    51
	{
sl@0
    52
	}
sl@0
    53
sl@0
    54
CTFontSelect::~CTFontSelect()
sl@0
    55
	{
sl@0
    56
	delete iGc;
sl@0
    57
	delete iDevice;
sl@0
    58
	}
sl@0
    59
sl@0
    60
void CTFontSelect::ConstructL()
sl@0
    61
	{
sl@0
    62
	TRAPD(err,iDevice = CFbsScreenDevice::NewL(KNullDesC,EColor256));
sl@0
    63
	if (err == KErrNotSupported)
sl@0
    64
		TRAP(err,iDevice = CFbsScreenDevice::NewL(KNullDesC,EColor16MU));
sl@0
    65
	if (err == KErrNotSupported)
sl@0
    66
		TRAP(err,iDevice = CFbsScreenDevice::NewL(KNullDesC,EColor4K));
sl@0
    67
	if (err == KErrNotSupported)
sl@0
    68
		TRAP(err,iDevice = CFbsScreenDevice::NewL(KNullDesC,EColor16M));
sl@0
    69
	if (err == KErrNotSupported)
sl@0
    70
		TRAP(err,iDevice = CFbsScreenDevice::NewL(KNullDesC,EColor64K));
sl@0
    71
	if (err == KErrNotSupported)
sl@0
    72
		TRAP(err,iDevice = CFbsScreenDevice::NewL(KNullDesC,EGray256));
sl@0
    73
	if (err == KErrNotSupported)
sl@0
    74
		TRAP(err,iDevice = CFbsScreenDevice::NewL(KNullDesC,EGray16));
sl@0
    75
	if (err == KErrNotSupported)
sl@0
    76
		TRAP(err,iDevice = CFbsScreenDevice::NewL(KNullDesC,EGray4));
sl@0
    77
	if (err == KErrNotSupported)
sl@0
    78
		TRAP(err,iDevice = CFbsScreenDevice::NewL(KNullDesC,EGray2));
sl@0
    79
	if (err == KErrNotSupported)
sl@0
    80
		TRAP(err,iDevice = CFbsScreenDevice::NewL(KNullDesC,EColor16));
sl@0
    81
	if (err == KErrNotSupported)
sl@0
    82
		TRAP(err,iDevice = CFbsScreenDevice::NewL(KNullDesC,EColor16MA));
sl@0
    83
	if (err == KErrNotSupported)
sl@0
    84
		TRAP(err,iDevice = CFbsScreenDevice::NewL(KNullDesC,EColor16MAP));
sl@0
    85
	if (err!=KErrNone)
sl@0
    86
		{
sl@0
    87
		_LIT(KLog,"Failed to create screen device, last returned error %d");
sl@0
    88
		INFO_PRINTF2(KLog,err);
sl@0
    89
		}
sl@0
    90
sl@0
    91
	User::LeaveIfError(err);
sl@0
    92
	iDevice->ChangeScreenDevice(NULL);
sl@0
    93
	iDevice->SetAutoUpdate(ETrue);
sl@0
    94
	iDevice->CreateContext(iGc);
sl@0
    95
	iDevice->SetScalingFactor(KScalingOriginPt, KScalingFactorX, KScalingFactorY, 1, 1);
sl@0
    96
	iGc->Activate(iDevice);
sl@0
    97
sl@0
    98
	}
sl@0
    99
sl@0
   100
/**
sl@0
   101
  @SYMTestCaseID GRAPHICS-BITGDI-0083
sl@0
   102
 
sl@0
   103
  @SYMDEF             
sl@0
   104
sl@0
   105
  @SYMTestCaseDesc Tests font selection
sl@0
   106
   
sl@0
   107
  @SYMTestPriority High
sl@0
   108
sl@0
   109
  @SYMTestStatus Implemented
sl@0
   110
sl@0
   111
  @SYMTestActions For different typeface and heights tests font selection to specific sizes
sl@0
   112
 
sl@0
   113
  @SYMTestExpectedResults Test should perform graphics operations succesfully. 
sl@0
   114
*/
sl@0
   115
void CTFontSelect::DoFontSelectTestsL()
sl@0
   116
	{
sl@0
   117
	TFontSpec fs;
sl@0
   118
	TInt typefaces = iDevice->NumTypefaces();
sl@0
   119
	TTypefaceSupport info;
sl@0
   120
	_LIT(KIgnoreFont1, "Test2");
sl@0
   121
	_LIT(KIgnoreFont2, "LinkedFont3TT"); //LinkedFont3/4TT both cause test failure on Freetype
sl@0
   122
	_LIT(KIgnoreFont3, "LinkedFont4TT"); //They are test files with incorrect metrics
sl@0
   123
sl@0
   124
	// Maximum height of font allowed by iType rasteriser
sl@0
   125
	const TInt KMaxITypeHeightInPixels = 256;
sl@0
   126
	_LIT(KErrorMessageWithFontName, "Font: %S, pixel height requested: %d, pixel height of font: %d");
sl@0
   127
	_LIT(KErrorMessage, "Pixel height requested: %d, pixel height of font: %d");
sl@0
   128
sl@0
   129
	for (TInt count = 0; count < typefaces; count++)
sl@0
   130
		{
sl@0
   131
		iDevice->TypefaceSupport( info, count );
sl@0
   132
sl@0
   133
		for (TInt index = 0; index < info.iNumHeights; index++)
sl@0
   134
			{
sl@0
   135
			TInt height = iDevice->FontHeightInPixels( count, index );
sl@0
   136
			if (height > KMaxITypeHeightInPixels)
sl@0
   137
				{
sl@0
   138
				continue;
sl@0
   139
				}
sl@0
   140
			fs.iTypeface = info.iTypeface;
sl@0
   141
			fs.iHeight = height;
sl@0
   142
sl@0
   143
			INFO_PRINTF1(_L("Test GetNearestFontInPixels"));
sl@0
   144
			User::LeaveIfError( iDevice->GetNearestFontInPixels( (CFont*&)iFont1 ,fs ) );
sl@0
   145
			TInt heightInPixels = iFont1->HeightInPixels();
sl@0
   146
			if ( heightInPixels == height )
sl@0
   147
				{
sl@0
   148
				TEST( ETrue );
sl@0
   149
				}
sl@0
   150
			else
sl@0
   151
				{
sl@0
   152
				TOpenFontFaceAttrib attrib;
sl@0
   153
				if( iFont1->GetFaceAttrib( attrib ) )
sl@0
   154
					{
sl@0
   155
					INFO_PRINTF4(KErrorMessageWithFontName, &attrib.FullName(), height, heightInPixels);
sl@0
   156
					}
sl@0
   157
				else
sl@0
   158
					{
sl@0
   159
					INFO_PRINTF3(KErrorMessage, height, heightInPixels);
sl@0
   160
					}
sl@0
   161
				TEST( EFalse );
sl@0
   162
				}
sl@0
   163
sl@0
   164
			INFO_PRINTF1(_L("Test GetNearestFontToDesignHeightInPixels"));
sl@0
   165
			User::LeaveIfError( iDevice->GetNearestFontToDesignHeightInPixels( (CFont*&)iFont2, fs ) );
sl@0
   166
			heightInPixels = iFont2->HeightInPixels();
sl@0
   167
			if ( heightInPixels == height )
sl@0
   168
				{
sl@0
   169
				TEST( ETrue );
sl@0
   170
				}
sl@0
   171
			else
sl@0
   172
				{
sl@0
   173
				TOpenFontFaceAttrib attrib;
sl@0
   174
				if( iFont2->GetFaceAttrib( attrib ) )
sl@0
   175
					{
sl@0
   176
					INFO_PRINTF4(KErrorMessageWithFontName, &attrib.FullName(), height, heightInPixels);
sl@0
   177
					}
sl@0
   178
				else
sl@0
   179
					{
sl@0
   180
					INFO_PRINTF3(KErrorMessage, height, heightInPixels);
sl@0
   181
					}
sl@0
   182
				TEST( EFalse );
sl@0
   183
				}
sl@0
   184
			iDevice->ReleaseFont( iFont2 );
sl@0
   185
			iDevice->ReleaseFont( iFont1 );
sl@0
   186
			}
sl@0
   187
			
sl@0
   188
		INFO_PRINTF1(_L("Test GetNearestFontToMaxHeightInPixels"));
sl@0
   189
		for (TInt maxHeight = 20; maxHeight <= 50; maxHeight += 10)
sl@0
   190
			{
sl@0
   191
			fs.iTypeface = info.iTypeface;
sl@0
   192
			fs.iHeight = maxHeight;
sl@0
   193
			User::LeaveIfError( iDevice->GetNearestFontToMaxHeightInPixels( (CFont*&)iFont3, fs, maxHeight ) );
sl@0
   194
sl@0
   195
			TBufC<64> fontName = ((CFont*&)iFont3)->FontSpecInTwips().iTypeface.iName;
sl@0
   196
			INFO_PRINTF2(_L("Font name: '%S'."), &fontName);
sl@0
   197
			if (fontName == KWrongFontFileName)
sl@0
   198
				{
sl@0
   199
				INFO_PRINTF2(_L(" ** '%S' is a wrong font file. Skip checks for this font **"), &fontName);
sl@0
   200
				break;
sl@0
   201
				}
sl@0
   202
sl@0
   203
			if (!fontName.Compare(KIgnoreFont1))
sl@0
   204
				{
sl@0
   205
				INFO_PRINTF1(_L(" ** 'Test2' font ignored. This files metrics are incorrect."));
sl@0
   206
				continue;
sl@0
   207
				}
sl@0
   208
			if (!fontName.Compare(KIgnoreFont2))
sl@0
   209
				{
sl@0
   210
				INFO_PRINTF1(_L(" ** 'LinkedFont3TT' font ignored. This files metrics are incorrect."));
sl@0
   211
				continue;
sl@0
   212
				}
sl@0
   213
			if (!fontName.Compare(KIgnoreFont3))
sl@0
   214
				{
sl@0
   215
				INFO_PRINTF1(_L(" ** 'LinkedFont4TT' font ignored. This files metrics are incorrect."));
sl@0
   216
				continue;
sl@0
   217
				}
sl@0
   218
			
sl@0
   219
			if (info.iIsScalable)
sl@0
   220
				{ // got a scalable (TrueType) font - can't guarantee will fit for bitmap font
sl@0
   221
				// N.B. If this ever fails then try replacing maxHeight with maxHeight + 1 in the test
sl@0
   222
				// as it could be down to the hinting rounding error that can very occasionally occur
sl@0
   223
				// We can't make this test reliably with bitmap fonts
sl@0
   224
				INFO_PRINTF1(_L("Font is scalable"));
sl@0
   225
				TEST(iFont3->FontMaxHeight() <= maxHeight+1);
sl@0
   226
				}
sl@0
   227
sl@0
   228
			INFO_PRINTF1(_L("Test font level metrics"));
sl@0
   229
			INFO_PRINTF3(_L("Requested max height %d, returned max height %d"), maxHeight, iFont3->FontMaxHeight());
sl@0
   230
			INFO_PRINTF4(_L("MaxAscent %d, MaxDescent %d, HeightInPixels %d"), iFont3->FontMaxAscent(), iFont3->FontMaxDescent(), iFont3->HeightInPixels());
sl@0
   231
			INFO_PRINTF3(_L("CapAscent %d, StdDescent %d"), iFont3->FontCapitalAscent(), iFont3->FontStandardDescent());
sl@0
   232
			TEST(iFont3->FontMaxHeight() >= ( iFont3->FontMaxAscent() + iFont3->FontMaxDescent() ));
sl@0
   233
			TEST(iFont3->FontMaxAscent() >= iFont3->FontCapitalAscent());
sl@0
   234
			TInt expMaxDescent=iFont3->FontStandardDescent();
sl@0
   235
			if (iDevice->SizeInPixels().iHeight>400)
sl@0
   236
				--expMaxDescent;		//To get test to pass on X86 and NaviEngine
sl@0
   237
			TEST(iFont3->FontMaxDescent() >= expMaxDescent);
sl@0
   238
sl@0
   239
			iDevice->ReleaseFont(iFont3);
sl@0
   240
			}
sl@0
   241
		}
sl@0
   242
	}
sl@0
   243
sl@0
   244
void CTFontSelect::RunTestCaseL(TInt aCurTestCase)
sl@0
   245
	{
sl@0
   246
	((CTFontSelectStep*)iStep)->SetTestStepID(KUnknownSYMTestCaseIDName);
sl@0
   247
	switch(aCurTestCase)
sl@0
   248
		{
sl@0
   249
		case 1:
sl@0
   250
			{
sl@0
   251
			((CTFontSelectStep*)iStep)->SetTestStepID(_L("GRAPHICS-BITGDI-0083"));
sl@0
   252
			INFO_PRINTF1(_L("DoFontSelectTest"));
sl@0
   253
			TRAPD(ret, DoFontSelectTestsL());
sl@0
   254
			TEST(ret == KErrNone);
sl@0
   255
			break;
sl@0
   256
			}
sl@0
   257
		case 2:
sl@0
   258
			{
sl@0
   259
			((CTFontSelectStep*)iStep)->SetTestStepID(KNotATestSYMTestCaseIDName);
sl@0
   260
			((CTFontSelectStep*)iStep)->CloseTMSGraphicsStep();
sl@0
   261
			TestComplete();		
sl@0
   262
			break;
sl@0
   263
			}
sl@0
   264
		}
sl@0
   265
	((CTFontSelectStep*)iStep)->RecordTestResultL();
sl@0
   266
	}
sl@0
   267
sl@0
   268
sl@0
   269
//--------------
sl@0
   270
__CONSTRUCT_STEP__(FontSelect)
sl@0
   271
sl@0
   272
void CTFontSelectStep::TestSetupL()
sl@0
   273
	{
sl@0
   274
	FbsStartup();
sl@0
   275
	User::LeaveIfError(RFbsSession::Connect());
sl@0
   276
	}
sl@0
   277
	
sl@0
   278
void CTFontSelectStep::TestClose()
sl@0
   279
	{
sl@0
   280
	RFbsSession::Disconnect();
sl@0
   281
	}