Update contrib.
1 // Copyright (c) 1998-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 #include "gdistructs.h"
20 #include "gdiconsts.h"
23 // Global panic function
25 _LIT(KGdiPanicCategory,"GDI");
27 void Panic(TGdiPanic aError)
29 User::Panic(KGdiPanicCategory,aError);
32 _LIT(KGDIPanicDesc1, "Gdi internal Panic %S, in file %S @ line %i");
33 _LIT(KGDIPanicDesc2, "Assert condition = \"%S\"");
34 _LIT(KGDIPanicDesc3, "Gdi internal %S, in file %S @ line %i");
36 void PanicWithCondAndInfo(TGdiPanic aError, const TDesC& aCondition, const TDesC& aFileName, const TDesC& aPanicName, TInt aLine)
39 buf.Format(KGDIPanicDesc1, &aPanicName, &aFileName, aLine);
42 buf.Format(KGDIPanicDesc2, &aCondition);
47 void PanicLogWithInfo(const TDesC& aCommand, const TDesC& aCondition, const TDesC& aFileName, TInt aLine)
50 buf.Format(KGDIPanicDesc3, &aCommand, &aFileName, aLine);
53 buf.Format(KGDIPanicDesc2, &aCondition);
63 EXPORT_C MGraphicsDeviceMap::MGraphicsDeviceMap()
64 /** Default constructor. */
68 EXPORT_C MGraphicsDeviceMap::~MGraphicsDeviceMap()
73 EXPORT_C TPoint MGraphicsDeviceMap::TwipsToPixels(const TPoint& aTwipPoint) const
74 /** Converts a point in twips to a point in pixels.
76 @param aTwipPoint A point on the graphics device in twips.
77 @return A point on the graphics device in pixels. */
79 return TPoint(HorizontalTwipsToPixels(aTwipPoint.iX),VerticalTwipsToPixels(aTwipPoint.iY));
83 EXPORT_C TRect MGraphicsDeviceMap::TwipsToPixels(const TRect& aTwipRect) const
84 /** Converts a rectangle in twips to a rectangle in pixels.
86 @param aTwipRect A rectangle on the graphics device in twips
87 @return A rectangle on the graphics device in pixels. */
89 return TRect(TwipsToPixels(aTwipRect.iTl),TwipsToPixels(aTwipRect.iBr));
93 EXPORT_C TPoint MGraphicsDeviceMap::PixelsToTwips(const TPoint& aPixelPoint) const
94 /** Converts a point in pixels to a point in twips.
96 @param aPixelPoint A point on the graphics device in pixels.
97 @return A point on the graphics device in twips. */
99 return TPoint(HorizontalPixelsToTwips(aPixelPoint.iX),VerticalPixelsToTwips(aPixelPoint.iY));
103 EXPORT_C TRect MGraphicsDeviceMap::PixelsToTwips(const TRect& aPixelRect) const
104 /** Converts a rectangle in pixels to a rectangle in twips.
106 @param aPixelRect A rectangle on the graphics device in pixels.
107 @return A rectangle on the graphics device in twips. */
109 return TRect(PixelsToTwips(aPixelRect.iTl),PixelsToTwips(aPixelRect.iBr));
116 EXPORT_C TInt CGraphicsContext::JustificationInPixels(TInt aExcessPixels,TInt aTotalUnits,TInt aFirstUnit,TInt aNumUnits)
117 /** Gets the amount of space in pixels by which to adjust letter or word spacing,
118 given the total number of words and spaces, a start space, and the number
119 of units to be adjusted.
121 The first two arguments are the number of pixels (character groups) and the
122 number of units (word spaces) over which justification is to occur. The third
123 argument specifies the current character group or word space, while the final
124 argument specifies the number of units that are to be adjusted.
126 A panic occurs if aExcessPixels is 0, aTotalUnits is not greater than 0, or
127 aFirstUnit is not less than aTotalUnits.
129 @param aExcessPixels The number of pixels by which the width of the text is
130 to be changed. It may be positive, in which case the text is stretched, or
131 negative, in which case it is shrunk.
132 @param aTotalUnits The number of word spaces over which the change in width
133 is to be distributed.
134 @param aFirstUnit The current unit — the character group or word space we
136 @param aNumUnits The number of units that are to be adjusted — starting
138 @return The number of pixels to be added to the width of the current unit.
139 @see SetWordJustification()
140 @see SetCharJustification() */
142 if(aExcessPixels==0 || aTotalUnits<=0 || aFirstUnit>=aTotalUnits)
144 TInt noExtra=Abs(aExcessPixels%aTotalUnits);
145 TInt extraPixel=aExcessPixels/Abs(aExcessPixels);
146 GDI_ASSERT_DEBUG_GENERAL( aFirstUnit>=0 , User::Panic(KGdiPanicCategory,KErrArgument) ) ;
147 GDI_ASSERT_DEBUG_GENERAL( aNumUnits>=0 , User::Panic(KGdiPanicCategory,KErrArgument) ) ;
148 if(aFirstUnit+aNumUnits>aTotalUnits)
149 aNumUnits=aTotalUnits-aFirstUnit;
150 TInt clip=aNumUnits*(aExcessPixels/aTotalUnits);
151 if(aFirstUnit>=noExtra)
153 if(aFirstUnit+aNumUnits>noExtra)
154 aNumUnits=noExtra-aFirstUnit;
155 return(clip+aNumUnits*extraPixel);
159 EXPORT_C TInt CGraphicsContext::JustificationInPixels(TInt& aExcessPixels,TInt& aTotalUnits)
160 /** Gets the amount of space in pixels by which to adjust the current letter or
161 word spacing, and also retrieves the number of excess pixels and word spaces
162 remaining after the adjustment is performed.
164 The arguments are the number of remaining pixels (character groups) and units
165 (word spaces) over which justification is to occur. The function can be called
166 repetitively until the number of units is zero, and hence justification is
167 complete. A panic occurs if the number of units is less than one or the amount
170 @param aExcessPixels The number of pixels by which the width of the text is
171 to be changed. It may be positive, in which case the text is stretched, or
172 negative, in which case it is shrunk. On return, this is equal to its old
173 value minus the return value.
174 @param aTotalUnits The number of word spaces over which the change in width
175 is to be distributed. On return, this is reduced by one.
176 @return The number of pixels to be added to the width of the current unit.
177 @see SetWordJustification()
178 @see SetCharJustification() */
180 GDI_ASSERT_DEBUG_GENERAL(aExcessPixels!=0,User::Panic(KGdiPanicCategory,KErrArgument));
181 GDI_ASSERT_DEBUG_GENERAL(aTotalUnits>0,User::Panic(KGdiPanicCategory,KErrArgument));
182 TInt justification=aExcessPixels/aTotalUnits;
183 if(aExcessPixels%aTotalUnits)
191 aExcessPixels-=justification;
192 return(justification);
196 EXPORT_C TInt CGraphicsContext::DrawTextExtended(const TDesC& aText,const TPoint& aPosition,
197 const TDrawTextExtendedParam& aParam)
198 /** Draws text, optionally changing its direction (right-to-left / left-to-right).
200 Apart from reordering the text, the function is the same as the two parameter
201 variant of DrawText(), described above.
203 @param aText The text string to be drawn, optionally changing its direction
204 (right-to-left / left-to-right).
205 @param aPosition A point specifying the position of the left end of the text.
206 @param aParam Indicates whether the text should be drawn from right-to-left
207 (for scripts like Arabic and Hebrew) or left-to-right.
208 @return KErrNoMemory indicates there was an OOM error when reordering the text;
209 KErrNone if the reordering was successful. */
211 // Reorder the text bidirectionally.
212 TText* reordered_text = NULL;
213 int error = TBidirectionalState::ReorderText(aText.Ptr(),aText.Length(),aParam.iParRightToLeft,reordered_text);
214 TPtrC p(reordered_text,aText.Length());
215 DrawText(p,aPosition,aParam);
216 if (reordered_text != aText.Ptr())
217 delete [] reordered_text;
222 EXPORT_C void CGraphicsContext::DrawText(const TDesC& aText,const TPoint& aPosition,const TDrawTextParam& /*aParam*/)
223 /** Draws the specified text at the given position using the parameters supplied.
225 @param aText The text to be drawn.
226 @param aPosition The position to draw the text at.
227 @param aParam Parameters to use for text drawing. */
229 DrawText(aText,aPosition);
233 Can be used to find out the top and bottom of an underline for the active font.
234 This allows correct calculation of the area required in which to draw text with underline.
236 @param TInt& aTop The top of the underline position
237 @param TInt& aBottom The bottom of the underline position
238 @return TInt KErrNone if successful, else a standard system wide error value.
240 EXPORT_C TInt CGraphicsContext::GetUnderlineMetrics(TInt& aTop,TInt& aBottom)
243 TTwoTInt outputPackage;
244 TTwoTInt* outputPtr = &outputPackage;
245 TInt err = APIExtension(KGetUnderlineMetrics, (TAny*&) outputPtr, NULL);
246 aTop = outputPackage.iTop;
247 aBottom = outputPackage.iBottom;
251 EXPORT_C TInt CGraphicsContext::SetShadowColor(const TRgb& aShadowColor)
253 TRgb shadowColor = aShadowColor;
255 return APIExtension(KSetShadowColor, (TAny*&)dummy, (TAny*)&shadowColor);
258 EXPORT_C TInt CGraphicsContext::GetShadowColor(TRgb& aShadowColor)
260 TRgb* shadowColor = &aShadowColor;
261 return APIExtension(KGetShadowColor, (TAny*&)shadowColor, NULL);
264 EXPORT_C TBool CGraphicsContext::IsFbsBitGc() const
266 TBool isFbsBitGc=EFalse;
267 TBool* isFbsBitGcPtr=&isFbsBitGc;
269 //Have a non const this since want the published API to be const
270 CGraphicsContext *nonConstThis = const_cast<CGraphicsContext*>(this);
272 //The API extension function is non-const, and this is const function
273 TInt err= nonConstThis->APIExtension(KUidIsFbsBitmapGc, (TAny*&)isFbsBitGcPtr, NULL);
275 //on error, return EFalse
276 return (!err ? isFbsBitGc : EFalse);
279 EXPORT_C void CGraphicsContext::DrawText(const TDesC& aText,const TTextParameters* iParam,const TPoint& aPosition)
283 TDrawTextInContextInternal context;
284 TDrawTextInContextInternal* contextPtr = &context;
285 contextPtr->iText.Set(aText);
286 contextPtr->iPosition.SetXY(0,0);
287 contextPtr->iPosition += aPosition;
288 contextPtr->iParam.iStart = iParam->iStart;
289 contextPtr->iParam.iEnd = iParam->iEnd;
290 if (KErrNotSupported == APIExtension(KDrawTextInContextUid, (TAny*&)dummy, (TAny*)contextPtr))
292 DrawText(aText,aPosition);
296 EXPORT_C void CGraphicsContext::DrawText(const TDesC& aText,const TTextParameters* iParam,const TRect& aBox,TInt aBaselineOffset,TTextAlign aHrz,TInt aMargin)
300 TDrawTextInContextInternal context;
301 TDrawTextInContextInternal* contextPtr = &context;
302 contextPtr->iText.Set(aText);
303 contextPtr->iBox.SetRect(aBox.iTl, aBox.iBr);
304 contextPtr->iBaselineOffset = aBaselineOffset;
305 contextPtr->iAlign = aHrz;
306 contextPtr->iMargin = aMargin;
307 contextPtr->iParam.iStart = iParam->iStart;
308 contextPtr->iParam.iEnd = iParam->iEnd;
309 if (KErrNotSupported == APIExtension(KDrawBoxTextInContextUid, (TAny*&)dummy, (TAny*)contextPtr))
311 DrawText(aText,aBox,aBaselineOffset,aHrz,aMargin);
315 EXPORT_C void CGraphicsContext::DrawText(const TDesC& aText,const TTextParameters* iParam,const TPoint& aPosition,const TDrawTextParam& /*aParam*/)
319 TDrawTextInContextInternal context;
320 TDrawTextInContextInternal* contextPtr = &context;
321 contextPtr->iText.Set(aText);
322 contextPtr->iPosition.SetXY(0,0);
323 contextPtr->iPosition += aPosition;
324 contextPtr->iParam.iStart = iParam->iStart;
325 contextPtr->iParam.iEnd = iParam->iEnd;
326 if (KErrNotSupported == APIExtension(KDrawTextInContextUid, (TAny*&)dummy, (TAny*)contextPtr))
328 DrawText(aText,aPosition);
332 EXPORT_C void CGraphicsContext::DrawTextVertical(const TDesC& aText,const TTextParameters* iParam,const TPoint& aPos,TBool aUp)
336 TDrawTextInContextInternal context;
337 TDrawTextInContextInternal* contextPtr = &context;
338 contextPtr->iText.Set(aText);
339 contextPtr->iPosition.SetXY(0,0);
340 contextPtr->iPosition += aPos;
341 contextPtr->iUp = aUp;
342 contextPtr->iParam.iStart = iParam->iStart;
343 contextPtr->iParam.iEnd = iParam->iEnd;
344 if (KErrNotSupported == APIExtension(KDrawTextInContextVerticalUid, (TAny*&)dummy, (TAny*)contextPtr))
346 DrawTextVertical(aText,aPos,aUp);
350 EXPORT_C void CGraphicsContext::DrawTextVertical(const TDesC& aText,const TTextParameters* iParam,const TRect& aBox,TInt aBaselineOffset,TBool aUp,TTextAlign aVert,TInt aMargin)
354 TDrawTextInContextInternal context;
355 TDrawTextInContextInternal* contextPtr = &context;
356 contextPtr->iText.Set(aText);
357 contextPtr->iBox.SetRect(aBox.iTl, aBox.iBr);
358 contextPtr->iBaselineOffset = aBaselineOffset;
359 contextPtr->iAlign = aVert;
360 contextPtr->iMargin = aMargin;
361 contextPtr->iUp = aUp;
362 contextPtr->iParam.iStart = iParam->iStart;
363 contextPtr->iParam.iEnd = iParam->iEnd;
364 if (KErrNotSupported == APIExtension(KDrawBoxTextInContextVerticalUid, (TAny*&)dummy, (TAny*)contextPtr))
366 DrawTextVertical(aText,aBox,aBaselineOffset,aUp,aVert,aMargin);
370 EXPORT_C TInt CGraphicsContext::DrawTextExtended(const TDesC& aText,const TTextParameters* aTextParam,const TPoint& aPosition,
371 const TDrawTextExtendedParam& aParam)
372 /** Draws text, optionally changing its direction (right-to-left / left-to-right).
374 Apart from reordering the text, the function is the same as the two parameter
375 variant of DrawText(), described above.
377 @param aText The text string to be drawn, optionally changing its direction
378 (right-to-left / left-to-right).
379 @param aPosition A point specifying the position of the left end of the text.
380 @param aParam Indicates whether the text should be drawn from right-to-left
381 (for scripts like Arabic and Hebrew) or left-to-right.
382 @return KErrNoMemory indicates there was an OOM error when reordering the text;
383 KErrNone if the reordering was successful. */
385 // Reorder the text bidirectionally.
386 TText* reordered_text = NULL;
387 int error = TBidirectionalState::ReorderText(aText.Ptr(),aText.Length(),aParam.iParRightToLeft,reordered_text);
388 TPtrC p(reordered_text,aText.Length());
389 DrawText(p,aTextParam,aPosition,aParam);
390 if (reordered_text != aText.Ptr())
391 delete [] reordered_text;
394 EXPORT_C void CGraphicsContext::Reserved()
395 /**Reserved function for future use. */
400 An API extension for CGraphics context replacing a reserved virtual method.
401 Effectively allows multiple methods to use just one ordinal number.
403 @param TUid aUid A unique identifier for the internal method required
404 @param TAny*& aOutput The output parameter
405 @param TAny* aInput The input argument. Notably not const.
406 @return KErrNone If a successful derived function is found, if the
407 default is used then KErrNotSupported is returned.
409 EXPORT_C TInt CGraphicsContext::APIExtension(TUid /*aUid*/, TAny*& /*aOutput*/, TAny* /*aInput*/)
411 return KErrNotSupported;
414 //Default implementation of reserved virtual
415 EXPORT_C void CGraphicsContext::Reserved_CGraphicsContext_2()
419 EXPORT_C TInt CBitmapContext::APIExtension(TUid aUid, TAny*& aOutput, TAny* aInput)
421 return CGraphicsContext::APIExtension(aUid, aOutput, aInput);
424 //Default implementation of reserved virtual
425 EXPORT_C void CBitmapContext::Reserved_CGraphicsContext_2()
427 CGraphicsContext::Reserved_CGraphicsContext_2();
430 //Default implementation of reserved virtual
431 EXPORT_C void CBitmapContext::Reserved_CBitmapContext_1()
435 //Default implementation of reserved virtual
436 EXPORT_C void CBitmapContext::Reserved_CBitmapContext_2()
440 //Default implementation of reserved virtual
441 EXPORT_C void CBitmapContext::Reserved_CBitmapContext_3()