os/graphics/windowing/windowserver/test/tman/TTEXT.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1995-2009 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 // Print some text, to be checked by eye
    15 // 
    16 //
    17 
    18 #include <e32std.h>
    19 #include <e32svr.h>
    20 #include "W32STD.H"
    21 #include "../tlib/testbase.h"
    22 #include "TMAN.H"
    23 
    24 class TTextTest;
    25 
    26 enum {EDrawModeFonts,EDrawModeCharJust,EDrawModeWordJust};
    27 
    28 _LIT(KTestFontTypefaceName,"DejaVu Sans Condensed");
    29 
    30 class CTextWindow : public CTWin
    31 	{
    32 public:
    33 	CTextWindow(TTextTest *aTest);
    34 	void SetUpLD(TPoint pos,TSize size,CTWinBase *parent, CWindowGc &aGc);
    35 	void Draw();
    36 	void WinKeyL(const TKeyEvent &aKey,const TTime &aTime);
    37 	void ResetPrintLine();
    38 	void Print(const CFont *aFont, const TDesC &aText);
    39 	void PrintLine(const CFont *aFont, const TDesC &aText);
    40 	void PrintDivider();
    41 	void PrintStylesL(const TDesC &aText, TFontSpec &aFontSpec, const TFontStyle &aFontStyle);
    42 	void DrawCharJustified(const TDesC &aText);
    43 	void DrawWordJustified(const TDesC &aText);
    44 	TBool NextPage();
    45 	void SetDrawMode(TInt aMode);
    46 private:
    47 	TInt iDrawMode;
    48 	TTextTest *iTest;
    49 	CFbsFont *iTmpFont;
    50 	TInt iNumTypeFaces;
    51 	TInt iTypeFaceIndex;
    52 	TInt iXStartPos;
    53 	TInt iYpos;
    54 	TInt iXpos;
    55 	};
    56 
    57 class TTextTest : public CTestBase
    58 	{
    59 public:
    60 	TTextTest();
    61 	~TTextTest();
    62 	TestState DoTestL();
    63 	void ConstructL();
    64 private:
    65 	CTextWindow *iWin;
    66 	TSize iWinSize;
    67 	TInt iState;
    68 	};
    69 
    70 GLDEF_C CTestBase *CreateTextTest()
    71 	{
    72 	return(new(ELeave) TTextTest());
    73 	}
    74 
    75 CTextWindow::CTextWindow(TTextTest *aTest) : CTWin(), iDrawMode(EDrawModeWordJust), iTest(aTest)
    76 	{}
    77 
    78 void CTextWindow::SetUpLD(TPoint pos,TSize size,CTWinBase *parent, CWindowGc &aGc)
    79 	{
    80 	ConstructExtLD(*parent,pos,size);
    81 	Activate();
    82 	AssignGC(aGc);
    83 	iNumTypeFaces=Client()->iScreen->NumTypefaces();
    84 	}
    85 
    86 void CTextWindow::ResetPrintLine()
    87 	{
    88 	iXpos=iXStartPos=5;
    89 	iYpos=2;
    90 	}
    91 
    92 void CTextWindow::PrintDivider()
    93 	{
    94 	iGc->DrawLine(TPoint(0,iYpos+5),TPoint(Size().iWidth,iYpos+5));
    95 	iYpos+=10;
    96 	}
    97 
    98 void CTextWindow::Print(const CFont *aFont, const TDesC &aText)
    99 	{
   100 	iGc->DrawText(aText, TPoint(iXpos, iYpos+aFont->AscentInPixels()));
   101 	iXpos+=aFont->TextWidthInPixels(aText);
   102 	}
   103 
   104 void CTextWindow::PrintLine(const CFont *aFont, const TDesC &aText)
   105 	{
   106 	iGc->DrawText(aText, TPoint(iXpos, iYpos+aFont->AscentInPixels()));
   107 	iXpos=iXStartPos;
   108 	iYpos+=aFont->HeightInPixels()+2;
   109 	}
   110 
   111 TBool CTextWindow::NextPage()
   112 	{
   113 	if (iTypeFaceIndex==(iNumTypeFaces-1))
   114 		return(ETrue);
   115 	++iTypeFaceIndex;
   116 	return(EFalse);
   117 	}
   118 
   119 void CTextWindow::PrintStylesL(const TDesC &aText, TFontSpec &aFontSpec, const TFontStyle &aFontStyle)
   120 	{
   121 	aFontSpec.iFontStyle=aFontStyle;
   122 	User::LeaveIfError(Client()->iScreen->GetNearestFontToDesignHeightInTwips((CFont *&)iTmpFont, aFontSpec));
   123 	iGc->UseFont(iTmpFont);
   124 	Print(iTmpFont,aText);
   125 	iGc->SetUnderlineStyle(EUnderlineOn);
   126 	Print(iTmpFont,_L("Underline, "));
   127 	iGc->SetStrikethroughStyle(EStrikethroughOn);
   128 	Print(iTmpFont,_L("Strikethrough/underline, "));
   129 	iGc->SetUnderlineStyle(EUnderlineOff);
   130 	PrintLine(iTmpFont,_L("Strikethrough"));
   131 	iGc->SetStrikethroughStyle(EStrikethroughOff);
   132 	Client()->iScreen->ReleaseFont(iTmpFont);
   133 	iTmpFont=NULL;
   134 	}
   135 
   136 void CTextWindow::DrawCharJustified(const TDesC &aText)
   137 	{
   138 	iGc->SetCharJustification(Size().iWidth-10-iTmpFont->TextWidthInPixels(aText),aText.Length()-1);
   139 	PrintLine(iTmpFont, aText);
   140 	}
   141 
   142 void CTextWindow::DrawWordJustified(const TDesC &aText)
   143 	{
   144 	TInt count=0;
   145 	for(TInt index=0;index<aText.Length();index++)
   146 		if (aText[index]==' ')
   147 			count++;
   148 	iGc->SetWordJustification(Size().iWidth-10-iTmpFont->TextWidthInPixels(aText),count);
   149 	PrintLine(iTmpFont, aText);
   150 	}
   151 
   152 void CTextWindow::Draw()
   153 //This function is virtual and so cannot have an 'L' at the end of it's name
   154 	{
   155 	iGc->Clear();
   156 	ResetPrintLine();
   157 	switch(iDrawMode)
   158 		{
   159 	case EDrawModeWordJust:
   160 		User::LeaveIfError(Client()->iScreen->GetNearestFontToDesignHeightInTwips((CFont *&)iTmpFont, TFontSpec(KTestFontTypefaceName,200)));
   161 		iGc->UseFont(iTmpFont);
   162 		DrawWordJustified(_L("Hello World"));
   163 		DrawWordJustified(_L("One Two Three Four Five Six Seven"));
   164 		DrawWordJustified(_L("AA    B        CC D"));
   165 		DrawWordJustified(_L("ONEWORD"));
   166 		iGc->DiscardFont();
   167 		Client()->iScreen->ReleaseFont(iTmpFont);
   168 		iTmpFont=NULL;
   169 		break;
   170 	case EDrawModeCharJust:
   171 		User::LeaveIfError(Client()->iScreen->GetNearestFontToDesignHeightInTwips((CFont *&)iTmpFont, TFontSpec(KTestFontTypefaceName,200)));
   172 		iGc->UseFont(iTmpFont);
   173 		DrawCharJustified(_L("Hello World"));
   174 		DrawCharJustified(_L("One Two Three Four Five Six Seven"));
   175 		DrawCharJustified(_L("AA    B        CC D"));
   176 		DrawCharJustified(_L("ONEWORD"));
   177 		iGc->DiscardFont();
   178 		Client()->iScreen->ReleaseFont(iTmpFont);
   179 		iTmpFont=NULL;
   180 		break;
   181 	case EDrawModeFonts:
   182 		{
   183 		TTypefaceSupport typefaceSupport;
   184 		Client()->iScreen->TypefaceSupport(typefaceSupport,iTypeFaceIndex);
   185 		TBuf<0x40> title;
   186 		TBuf16<KMaxTypefaceNameLength> tmpBuf;
   187 		tmpBuf.Copy(typefaceSupport.iTypeface.iName);
   188 		title.Append(tmpBuf);
   189 		title.AppendFormat(TRefByValue<const TDesC>(_L(", Heights (Min=%d, Max=%d, Num=%d)")),typefaceSupport.iMinHeightInTwips,typefaceSupport.iMaxHeightInTwips,typefaceSupport.iNumHeights);
   190 		PrintLine(iFont,title);
   191 		PrintDivider();
   192 		for (TInt tfHeight=0;tfHeight<typefaceSupport.iNumHeights;tfHeight++)
   193 			{
   194 			TFontSpec fspec(typefaceSupport.iTypeface.iName,Client()->iScreen->FontHeightInTwips(iTypeFaceIndex,tfHeight));
   195 			PrintStylesL(_L("Normal, "), fspec, TFontStyle());
   196 			PrintStylesL(_L("Bold, "), fspec, TFontStyle(EPostureUpright,EStrokeWeightBold,EPrintPosNormal));
   197 			PrintStylesL(_L("Italic, "), fspec, TFontStyle(EPostureItalic,EStrokeWeightNormal,EPrintPosNormal));
   198 			PrintStylesL(_L("Bold/italic, "), fspec, TFontStyle(EPostureItalic,EStrokeWeightBold,EPrintPosNormal));
   199 			if (iYpos>Size().iHeight)
   200 				break;
   201 			}
   202 		}
   203 		break;
   204 		}
   205 	}
   206 
   207 void CTextWindow::WinKeyL(const TKeyEvent &aKey,const TTime &)
   208 	{
   209 	if (iDrawMode!=EDrawModeFonts || (aKey.iCode==EKeyEscape || NextPage()))
   210 		CActiveScheduler::Stop();
   211 	else
   212 		iWin.Invalidate();
   213 	}
   214 
   215 void CTextWindow::SetDrawMode(TInt aDrawMode)
   216 	{
   217 	iDrawMode=aDrawMode;
   218 	iWin.Invalidate();
   219 	}
   220 
   221 TTextTest::TTextTest() : CTestBase(_L("Text"))
   222 	{}
   223 
   224 TTextTest::~TTextTest()
   225 	{
   226 	CTWin::Delete(iWin);
   227 	}
   228 
   229 void TTextTest::ConstructL()
   230 	{
   231 	CTextWindow *win=new(ELeave) CTextWindow(this);
   232 	win->SetUpLD(TPoint(0,0),Client()->iScreen->SizeInPixels(),Client()->iGroup,*Client()->iGc);
   233 	iWin=win;
   234 	Client()->iGroup->SetCurrentWindow(iWin);
   235 	Client()->iGroup->GroupWin()->SetOrdinalPosition(0);
   236 	}
   237 
   238 TestState TTextTest::DoTestL()
   239 	{
   240 	switch(iState)
   241 		{
   242 		case 0:
   243 			LogSubTest(_L("SetWordJustification"),1);
   244 			iWin->SetDrawMode(EDrawModeWordJust);
   245  			CActiveScheduler::Start();
   246 			iState++;
   247 			break;
   248 		case 1:
   249 			LogSubTest(_L("SetCharJustification"),2);
   250 			iWin->SetDrawMode(EDrawModeCharJust);
   251 			CActiveScheduler::Start();
   252 			iState++;
   253 			break;
   254 		case 2:
   255 			LogSubTest(_L("Text 1"),3);
   256 			iWin->SetDrawMode(EDrawModeFonts);
   257 			CActiveScheduler::Start();
   258 			iState++;
   259 			break;
   260 		default:
   261 			return(EFinished);
   262 		}
   263 	return(ENext);
   264 	}
   265