Update contrib.
1 // Copyright (c) 2002-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
19 @internalComponent Internal Symbian test code
23 #include "TGraphicsContext.h"
27 // Utility functions to show contents of test data using test.Printf
29 extern void PrintTestData (const TDesC& aTitle , const TDesC16& aDataBuffer);
31 extern void PrintTestData(const TDesC& aTitle, const TText16* aDataBuffer, const TInt aSize);
36 _LIT(KTestFontName, "Non Functional Test Font");
39 CTestPalette2::~CTestPalette2()
41 iArray = 0; //avoid attempting to deallocate iArray in ~CPalette
47 // CTestGraphicsDevice
50 CTestGraphicsDevice* CTestGraphicsDevice::NewL(TSize aSizeInPixels)
52 CTestGraphicsDevice* theDevice = new (ELeave) CTestGraphicsDevice(aSizeInPixels);
53 CleanupStack::PushL(theDevice);
54 theDevice->iPalette = new (ELeave) CTestPalette2;
55 theDevice->iPalette->SetEntry(0, KRgbBlack);
56 theDevice->iPalette->SetEntry(1, KRgbWhite);
57 theDevice->iPalette->SetEntry(2, KRgbMagenta);
58 theDevice->iPalette->SetEntry(3, KRgbCyan);
60 CleanupStack::Pop(theDevice);
64 CTestGraphicsDevice::CTestGraphicsDevice(TSize aSizeInPixels)
65 : iSize(aSizeInPixels),
66 iHorizontalTwipsToPixels(40),
67 iVerticalTwipsToPixels(40)
70 CTestGraphicsDevice::~CTestGraphicsDevice()
75 void CTestGraphicsDevice::SetHorizontalTwipsToPixels(TInt aTwipsToPixels)
77 iHorizontalTwipsToPixels = aTwipsToPixels;
80 void CTestGraphicsDevice::SetVerticalTwipsToPixels(TInt aTwipsToPixels)
82 iVerticalTwipsToPixels = aTwipsToPixels;
85 TDisplayMode CTestGraphicsDevice::DisplayMode() const
90 TSize CTestGraphicsDevice::SizeInPixels() const
95 TSize CTestGraphicsDevice::SizeInTwips() const
97 return TSize(iSize.iWidth * iHorizontalTwipsToPixels,
98 iSize.iHeight * iVerticalTwipsToPixels);
101 TInt CTestGraphicsDevice::CreateContext(CGraphicsContext*& aGC)
103 CGraphicsContext* r = new CTestGraphicsContext(this);
110 TInt CTestGraphicsDevice::NumTypefaces() const
115 void CTestGraphicsDevice::TypefaceSupport(TTypefaceSupport& aTypefaceSupport, TInt aTypefaceIndex) const
117 // The only font we have at the moment is 10 pixels * 12 pixels for every character
118 __ASSERT_ALWAYS(aTypefaceIndex == 0,
119 CTestGraphicsContext::Panic(CTestGraphicsContext::ETypefaceIndexOutOfRange));
120 aTypefaceSupport.iIsScalable = EFalse;
121 aTypefaceSupport.iMaxHeightInTwips = iVerticalTwipsToPixels * 12;
122 aTypefaceSupport.iMinHeightInTwips = iVerticalTwipsToPixels * 10;
123 aTypefaceSupport.iNumHeights = 1;
124 aTypefaceSupport.iTypeface.iName = KTestFontName;
125 aTypefaceSupport.iTypeface.SetIsProportional(ETrue); // a bit of a lie
126 aTypefaceSupport.iTypeface.SetIsSerif(EFalse);
127 aTypefaceSupport.iTypeface.SetIsSymbol(EFalse);
130 TInt CTestGraphicsDevice::FontHeightInTwips(TInt aTypefaceIndex, TInt aHeightIndex) const
132 // The only font we have at the moment is 10 pixels * 12 pixels for every character
133 __ASSERT_ALWAYS(aTypefaceIndex == 0,
134 CTestGraphicsContext::Panic(CTestGraphicsContext::ETypefaceIndexOutOfRange));
135 return iVerticalTwipsToPixels * FontHeightInPixels(aTypefaceIndex, aHeightIndex);
138 void CTestGraphicsDevice::PaletteAttributes(TBool& aModifiable, TInt& aNumEntries) const
144 void CTestGraphicsDevice::SetPalette(CPalette* aPalette)
146 for (TInt i = 0; i != CTestPalette2::KNumEntries; ++i)
148 TRgb col = aPalette->GetEntry(i);
149 iPalette -> SetEntry(i, col);
153 TInt CTestGraphicsDevice::GetPalette(CPalette*& aPalette) const
155 aPalette = const_cast<CTestPalette2*>(iPalette);
159 void CTestGraphicsDevice::GetPixel(TRgb& aColor, const TPoint&) const
164 void CTestGraphicsDevice::GetScanLine(TDes8&, const TPoint&, TInt, TDisplayMode) const
166 __ASSERT_DEBUG(0, CTestGraphicsContext::Panic(CTestGraphicsContext::EUnimplemented));
169 TInt CTestGraphicsDevice::AddFile(const TDesC&, TInt&)
171 return KErrNotSupported;
174 void CTestGraphicsDevice::RemoveFile(TInt)
178 TInt CTestGraphicsDevice::GetNearestFontInPixels(CFont*& aFont, const TFontSpec& aFontSpec)
180 return GetNearestFontToDesignHeightInPixels(aFont, aFontSpec);
183 TInt CTestGraphicsDevice::GetNearestFontToDesignHeightInPixels(CFont*& aFont, const TFontSpec& /*aFontSpec*/)
185 CTestFont* font = new CTestFont();
192 TInt CTestGraphicsDevice::GetNearestFontToMaxHeightInPixels(CFont*& aFont, const TFontSpec& /*aFontSpec*/, TInt /*aMaxHeight*/)
194 CTestFont* font = new CTestFont();
201 TInt CTestGraphicsDevice::FontHeightInPixels(TInt, TInt) const
206 TInt CTestGraphicsDevice::HorizontalTwipsToPixels(TInt aTwips) const
208 return aTwips / iHorizontalTwipsToPixels;
211 TInt CTestGraphicsDevice::VerticalTwipsToPixels(TInt aTwips) const
213 return aTwips / iVerticalTwipsToPixels;
216 TInt CTestGraphicsDevice::HorizontalPixelsToTwips(TInt aPixels) const
218 return aPixels * iHorizontalTwipsToPixels;
221 TInt CTestGraphicsDevice::VerticalPixelsToTwips(TInt aPixels) const
223 return aPixels * iVerticalTwipsToPixels;
226 TInt CTestGraphicsDevice::GetNearestFontInTwips(CFont*& aFont, const TFontSpec& aFontSpec)
228 return GetNearestFontToDesignHeightInTwips(aFont, aFontSpec);
231 TInt CTestGraphicsDevice::GetNearestFontToDesignHeightInTwips(CFont*& aFont, const TFontSpec& aFontSpec)
233 TFontSpec fontSpec = aFontSpec;
234 fontSpec.iHeight = VerticalTwipsToPixels(fontSpec.iHeight);
235 return GetNearestFontToDesignHeightInPixels(aFont, fontSpec);
238 TInt CTestGraphicsDevice::GetNearestFontToMaxHeightInTwips(CFont*& aFont, const TFontSpec& aFontSpec, TInt aMaxHeight)
240 TFontSpec fontSpec = aFontSpec;
241 fontSpec.iHeight = VerticalTwipsToPixels(fontSpec.iHeight);
242 return GetNearestFontToMaxHeightInPixels(aFont, fontSpec, VerticalTwipsToPixels(aMaxHeight));
245 void CTestGraphicsDevice::ReleaseFont(CFont* aFont)
247 __ASSERT_ALWAYS(aFont->TypeUid() == TUid::Uid(12345),
248 CTestGraphicsContext::Panic(CTestGraphicsContext::EUnknownFont));
249 delete static_cast<CTestFont*>(aFont);
254 // CTestGraphicsContext
257 void CTestGraphicsContext::Panic(TInt aReason)
259 User::Panic(_L("CTestGC"), aReason);
262 CTestGraphicsContext::CTestGraphicsContext(CTestGraphicsDevice* aGd)
263 : iGd(aGd), iDrawMode(EDrawModePEN), iPenSize(1,1)
267 CTestGraphicsContext::~CTestGraphicsContext()
271 void CTestGraphicsContext::AddRectToDrawnArea(const TRect& aRect)
273 if (iDrawnArea.IsEmpty())
276 iDrawnArea.BoundingRect(aRect);
277 // only one condition at the moment
278 if (iDrawMode == EDrawModeXOR)
280 if (iAreaDrawnWithCondition.IsEmpty())
281 iAreaDrawnWithCondition = aRect;
283 iAreaDrawnWithCondition.BoundingRect(aRect);
287 void CTestGraphicsContext::AddPointToDrawnArea(const TPoint& aPoint)
289 AddRectToDrawnArea(TRect(aPoint, iPenSize));
292 CGraphicsDevice* CTestGraphicsContext::Device() const
297 void CTestGraphicsContext::SetOrigin(const TPoint& aPos)
302 void CTestGraphicsContext::SetDrawMode(TDrawMode aDrawingMode)
304 iDrawMode = aDrawingMode;
307 void CTestGraphicsContext::SetClippingRect(const TRect& /*aRect*/)
311 void CTestGraphicsContext::CancelClippingRect()
315 void CTestGraphicsContext::Reset()
317 iDrawMode = EDrawModePEN;
320 iPenSize.iHeight = 1;
323 void CTestGraphicsContext::UseFont(const CFont* aFont)
328 void CTestGraphicsContext::DiscardFont()
333 void CTestGraphicsContext::SetUnderlineStyle(TFontUnderline /*UnderlineStyle*/)
337 void CTestGraphicsContext::SetStrikethroughStyle(TFontStrikethrough /*aStrikethroughStyle*/)
341 void CTestGraphicsContext::SetWordJustification(TInt /*aExcessWidth*/,TInt /*aNumGaps*/)
345 void CTestGraphicsContext::SetCharJustification(TInt /*aExcessWidth*/,TInt /*aNumChars*/)
349 void CTestGraphicsContext::SetPenColor(const TRgb& aColor)
352 iGd->GetPalette(palette);
353 iPenColorIndex = palette->NearestIndex(aColor);
356 void CTestGraphicsContext::SetPenStyle(TPenStyle /*aPenStyle*/)
360 void CTestGraphicsContext::SetPenSize(const TSize& aSize)
365 void CTestGraphicsContext::SetBrushColor(const TRgb& /*aColor*/)
369 void CTestGraphicsContext::SetBrushStyle(TBrushStyle /*aBrushStyle*/)
373 void CTestGraphicsContext::SetBrushOrigin(const TPoint& /*aOrigin*/)
377 void CTestGraphicsContext::UseBrushPattern(const CFbsBitmap* /*aBitmap*/)
381 void CTestGraphicsContext::DiscardBrushPattern()
385 void CTestGraphicsContext::MoveTo(const TPoint& aPoint)
387 iCurrentPos = iOrigin + aPoint;
390 void CTestGraphicsContext::MoveBy(const TPoint& aVector)
392 iCurrentPos += aVector;
395 void CTestGraphicsContext::Plot(const TPoint& aPoint)
397 iCurrentPos = iOrigin + aPoint;
398 AddPointToDrawnArea(iCurrentPos);
401 void CTestGraphicsContext::DrawArc(const TRect& aRect,const TPoint& /*aStart*/,const TPoint& aEnd)
405 AddRectToDrawnArea(r);
406 iCurrentPos = iOrigin + aEnd;
409 void CTestGraphicsContext::DrawLine(const TPoint& aPoint1,const TPoint& aPoint2)
411 AddPointToDrawnArea(iOrigin + aPoint1);
412 iCurrentPos = iOrigin + aPoint2;
413 AddPointToDrawnArea(iCurrentPos);
416 void CTestGraphicsContext::DrawLineTo(const TPoint& aPoint)
418 AddPointToDrawnArea(iCurrentPos);
419 iCurrentPos = iOrigin + aPoint;
420 AddPointToDrawnArea(iCurrentPos);
423 void CTestGraphicsContext::DrawLineBy(const TPoint& aVector)
425 AddPointToDrawnArea(iCurrentPos);
426 iCurrentPos += aVector;
427 AddPointToDrawnArea(iCurrentPos);
430 void CTestGraphicsContext::DrawPolyLine(const CArrayFix<TPoint>* aPointList)
432 TInt num = aPointList->Count();
435 iCurrentPos = iOrigin + (*aPointList)[num - 1];
436 AddPointToDrawnArea(iCurrentPos);
440 void CTestGraphicsContext::DrawPolyLine(const TPoint* aPointList,TInt aNumPoints)
444 iCurrentPos = iOrigin + aPointList[aNumPoints - 1];
445 AddPointToDrawnArea(iCurrentPos);
449 void CTestGraphicsContext::DrawPie(const TRect& aRect,
450 const TPoint& /*aStart*/, const TPoint& aEnd)
454 AddRectToDrawnArea(r);
455 iCurrentPos = iOrigin + aEnd;
458 void CTestGraphicsContext::DrawEllipse(const TRect& aRect)
462 AddRectToDrawnArea(r);
465 void CTestGraphicsContext::DrawRect(const TRect& aRect)
469 AddRectToDrawnArea(r);
472 void CTestGraphicsContext::DrawRoundRect(const TRect& aRect,const TSize& aCornerSize)
477 AddRectToDrawnArea(r);
480 TInt CTestGraphicsContext::DrawPolygon(const CArrayFix<TPoint>* aPointList,TFillRule /*aFillRule*/)
482 TInt num = aPointList->Count();
485 iCurrentPos = iOrigin + (*aPointList)[num - 1];
486 AddPointToDrawnArea(iCurrentPos);
491 TInt CTestGraphicsContext::DrawPolygon(const TPoint* aPointList,TInt aNumPoints,TFillRule /*aFillRule*/)
495 iCurrentPos = iOrigin + aPointList[aNumPoints - 1];
496 AddPointToDrawnArea(iCurrentPos);
501 void CTestGraphicsContext::DrawBitmap(const TPoint& /*aTopLeft*/,const CFbsBitmap* /*aSource*/)
505 void CTestGraphicsContext::DrawBitmap(const TRect& /*aDestRect*/,const CFbsBitmap* /*aSource*/)
509 void CTestGraphicsContext::DrawBitmap(const TRect& /*aDestRect*/,const CFbsBitmap* /*aSource*/,const TRect& /*aSourceRect*/)
513 void CTestGraphicsContext::DrawBitmapMasked(const TRect& /*aDestRect*/,const CFbsBitmap* /*aBitmap*/,const TRect& /*aSourceRect*/,const CFbsBitmap* /*aMaskBitmap*/,TBool /*aInvertMask*/)
516 void CTestGraphicsContext::DrawBitmapMasked(const TRect& /*aDestRect*/,const CWsBitmap* /*aBitmap*/,const TRect& /*aSourceRect*/,const CWsBitmap* /*aMaskBitmap*/,TBool /*aInvertMask*/)
520 void CTestGraphicsContext::MapColors(const TRect &/*aRect*/,const TRgb */*aColors*/,TInt /*aNumPairs*/,TBool /*aMapForwards*/)
523 TInt CTestGraphicsContext::SetClippingRegion(const TRegion &/*aRegion*/)
528 void CTestGraphicsContext::CancelClippingRegion()
531 void CTestGraphicsContext::DrawTextVertical(const TDesC& /*aText*/,const TPoint& /*aPos*/,TBool /*aUp*/)
534 void CTestGraphicsContext::DrawTextVertical(const TDesC& /*aText*/,const TRect& /*aBox*/,TInt /*aBaselineOffset*/,TBool /*aUp*/,TTextAlign /*aVert*/,TInt /*aMargin*/)
539 * DrawText - merely add line "drawn" to collection
544 void CTestGraphicsContext::DrawText(const TDesC& aText,const TPoint& aPosition)
547 #ifdef PRINT_DRAWTEXT_LINES
549 _LIT(KDrawTextFormat, "DrawText called for position %d,%d:");
550 _LIT(KDrawTextTitle, "Text being drawn");
551 test.Printf(KDrawTextFormat, aPosition.iX, aPosition.iY);
552 PrintTestData(KDrawTextTitle, aText);
554 #endif /* PRINT_DRAWTEXT_LINES */
556 TTestGCDisplayLine thisLine;
557 thisLine.Set(aPosition, aText);
558 iLineArray.Append(thisLine);
562 void CTestGraphicsContext::DrawText(const TDesC& aText,const TRect& aBox,TInt aBaselineOffset,
563 TTextAlign /*aAlignment*/, TInt aLeftMargin)
565 TPoint pos(aBox.iBr.iX + aLeftMargin, aBox.iTl.iY + aBaselineOffset);
567 DrawText(aText, pos);
570 void CTestGraphicsContext::DrawText(const TDesC& aText, const TPoint& aPosition,
571 const TDrawTextParam&)
573 DrawText(aText, aPosition);
581 TUid CTestFont::DoTypeUid() const
583 return TUid::Uid(12345);
586 TInt CTestFont::DoHeightInPixels() const
591 TInt CTestFont::DoAscentInPixels() const
596 TInt CTestFont::DoCharWidthInPixels(TChar aChar) const
616 TInt CTestFont::DoTextWidthInPixels(const TDesC& aText) const
619 const TText* p = aText.Ptr();
620 const TText* end = p + aText.Length();
622 total += CharWidthInPixels(*p);
626 TInt CTestFont::DoBaselineOffsetInPixels() const
631 TInt CTestFont::DoTextCount(const TDesC& aText,TInt aWidthInPixels) const
634 return DoTextCount(aText, aWidthInPixels, excess);
637 TInt CTestFont::DoTextCount(const TDesC& aText,TInt aWidthInPixels,TInt& aExcessWidthInPixels) const
639 aExcessWidthInPixels = aWidthInPixels;
640 const TText* start = aText.Ptr();
641 const TText* end = start + aText.Length();
642 const TText* p = start;
645 && (width = CharWidthInPixels(*p)) <= aExcessWidthInPixels)
647 aExcessWidthInPixels -= width;
653 TInt CTestFont::DoMaxCharWidthInPixels() const
658 TInt CTestFont::DoMaxNormalCharWidthInPixels() const
663 TFontSpec CTestFont::DoFontSpecInTwips() const
665 return TFontSpec(KTestFontName, 12);
668 CFont::TCharacterDataAvailability
669 CTestFont::DoGetCharacterData(TUint aCode, TOpenFontCharMetrics& aMetrics,
670 const TUint8*& aBitmap, TSize& aBitmapSize) const
676 // ESC character should cause this fault; no character data available.
677 return CFont::ENoCharacterData;
679 // We want 'W' to have side-bearings
680 CFont::DoGetCharacterData(aCode, aMetrics, aBitmap, aBitmapSize);
681 width = aMetrics.Width();
682 aMetrics.SetHorizBearingX(-1);
683 aMetrics.SetWidth(width + 2);
684 return CFont::ECharacterWidthOnly ;
686 // Arabic Waw-- has massive left side-bearing
687 CFont::DoGetCharacterData(aCode, aMetrics, aBitmap, aBitmapSize);
688 width = aMetrics.Width();
689 aMetrics.SetHorizBearingX(-5);
690 aMetrics.SetWidth(width + 5);
691 return CFont::ECharacterWidthOnly ;
693 // We want 'X' to have a left side-bearing only
694 CFont::DoGetCharacterData(aCode, aMetrics, aBitmap, aBitmapSize);
695 aMetrics.SetHorizBearingX(-1);
696 return CFont::ECharacterWidthOnly ;
698 // We want Hebrew Tav to have a +ve left side-bearing
699 CFont::DoGetCharacterData(aCode, aMetrics, aBitmap, aBitmapSize);
700 aMetrics.SetHorizBearingX(1);
701 return CFont::ECharacterWidthOnly ;
703 // We want Hiragana Ka to have a top end-bearing
704 CFont::DoGetCharacterData(aCode, aMetrics, aBitmap, aBitmapSize);
705 aMetrics.SetVertBearingY(-1);
706 return CFont::EAllCharacterData ;
708 CFont::DoGetCharacterData(aCode, aMetrics, aBitmap, aBitmapSize);
709 aMetrics.SetWidth(0);
710 aMetrics.SetHeight(0);
711 aMetrics.SetVertAdvance(0);
712 return CFont::ECharacterWidthOnly ;
714 return CFont::DoGetCharacterData(aCode, aMetrics, aBitmap, aBitmapSize);