First public contribution.
1 // Copyright (c) 2003-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 _LIT(KGdiTZoomFactorPanicCategory,"TZoomFactor");
26 EXPORT_C TZoomFactor::TZoomFactor():
27 iZoomFactor(EZoomOneToOne),
29 /** Constructs a default zoom factor object.
31 Note that a TZoomFactor object cannot be used until a CGraphicsDevice to which
32 it is associated is specified (by SetGraphicsDeviceMap()). Therefore the other
33 constructor is normally used for constructing TZoomFactors. The default constructor
34 function is provided for use in TZoomFactor-derived classes. */
38 EXPORT_C TZoomFactor::~TZoomFactor()
41 Frees resources owned by the object, prior to its destruction. */
45 EXPORT_C TInt TZoomFactor::ZoomFactor() const
46 /** Gets the zoom factor.
48 @return The zoom factor */
54 EXPORT_C void TZoomFactor::SetZoomFactor(TInt aZoomFactor)
55 /** Sets the zoom factor.
57 @param aZoomFactor The desired zoom factor. */
59 iZoomFactor=aZoomFactor;
63 EXPORT_C void TZoomFactor::SetTwipToPixelMapping(const TSize& aSizeInPixels,const TSize& aSizeInTwips)
64 // Ensure that aSizeInTwips fits inside aSizeInPixels when zoomed
65 /** Sets the twips to pixels mapping for the graphics device with which the zoom
68 This setting is used by all the twips to pixels and pixels to twips conversion
71 @param aSizeInPixels The size of the graphics device area in pixels.
72 @param aSizeInTwips The size of the graphics device area in twips. */
74 GDI_ASSERT_ALWAYS_GENERAL(iDevice!=NULL,User::Panic(KGdiTZoomFactorPanicCategory,KErrNotFound));
75 TInt devicepixelwidth=iDevice->HorizontalTwipsToPixels(aSizeInTwips.iWidth);
76 TInt devicepixelheight=iDevice->VerticalTwipsToPixels(aSizeInTwips.iHeight);
77 if(devicepixelwidth==0 || devicepixelheight==0)
82 iZoomFactor = (1000*aSizeInPixels.iWidth+(devicepixelwidth/2))/devicepixelwidth;
83 TInt temp = (1000*aSizeInPixels.iHeight+(devicepixelheight/2))/devicepixelheight;
88 EXPORT_C TInt TZoomFactor::HorizontalTwipsToPixels(TInt aTwipWidth) const
89 /** Converts a horizontal dimension from twips to pixels on the graphics
92 This function implements the pure virtual function defined in
93 MGraphicsDeviceMap::HorizontalTwipsToPixels() */
95 // iZoomFactor expressed in units of a thousandth
96 // delegated conversion is done 8 times multiplied for 3 guard bits
97 // hence result divided by 8
98 GDI_ASSERT_ALWAYS_GENERAL(iDevice!=NULL,User::Panic(KGdiTZoomFactorPanicCategory,KErrNotFound));
99 return (iDevice->HorizontalTwipsToPixels(((aTwipWidth*iZoomFactor)/125))+((aTwipWidth<0)?-4:4))/8;
103 EXPORT_C TInt TZoomFactor::VerticalTwipsToPixels(TInt aTwipHeight) const
104 /** Converts a vertical dimension from twips to pixels on the graphics
107 This function implements the pure virtual function defined in
108 MGraphicsDeviceMap::VerticalTwipsToPixels() */
110 GDI_ASSERT_ALWAYS_GENERAL(iDevice!=NULL,User::Panic(KGdiTZoomFactorPanicCategory,KErrNotFound));
111 return (iDevice->VerticalTwipsToPixels(((aTwipHeight*iZoomFactor)/125))+((aTwipHeight<0)?-4:4))/8;
115 EXPORT_C TInt TZoomFactor::HorizontalPixelsToTwips(TInt aPixelWidth) const
116 /** Converts a horizontal dimension from pixels to twips on the graphics
119 This function implements the pure virtual function defined in
120 MGraphicsDeviceMap::HorizontalPixelsToTwips() */
122 GDI_ASSERT_ALWAYS_GENERAL(iDevice!=NULL,User::Panic(KGdiTZoomFactorPanicCategory,KErrNotFound));
123 TInt temp = iDevice->HorizontalPixelsToTwips(aPixelWidth*8)*1000;
124 temp+=(temp<0)?-(iZoomFactor*4):iZoomFactor*4;
125 TInt eightTimesZoom=iZoomFactor*8;
127 return temp/eightTimesZoom;
131 EXPORT_C TInt TZoomFactor::VerticalPixelsToTwips(TInt aPixelHeight) const
132 /** Converts a vertical dimension from pixels to twips on the graphics
135 This function implements the pure virtual function defined in
136 MGraphicsDeviceMap::VerticalPixelsToTwips() */
138 GDI_ASSERT_ALWAYS_GENERAL(iDevice!=NULL,User::Panic(KGdiTZoomFactorPanicCategory,KErrNotFound));
139 TInt temp = iDevice->VerticalPixelsToTwips(aPixelHeight*8)*1000;
140 temp+=(temp<0)?-(iZoomFactor*4):iZoomFactor*4;
141 TInt eightTimesZoom=iZoomFactor*8;
143 return temp/eightTimesZoom;
150 EXPORT_C TInt TZoomFactor::GetNearestFontInTwips(CFont*& aFont, const TFontSpec& aFontSpec)
152 return GetNearestFontToDesignHeightInTwips(aFont, aFontSpec);
156 Gets the font which is the nearest to the given font specification.
157 Matching to design height gives no guarantees on the actual physical size of the font.
159 @param aFont On return, contains a pointer to the nearest font.
160 @param aFontSpec The specification of the font to be matched.
161 @return KErrNone if successful; a system-wide error code otherwise.
165 EXPORT_C TInt TZoomFactor::GetNearestFontToDesignHeightInTwips(CFont*& aFont, const TFontSpec& aFontSpec)
167 GDI_ASSERT_ALWAYS_GENERAL(iDevice, User::Panic(KGdiTZoomFactorPanicCategory, KErrNotFound));
168 TFontSpec fontSpec = aFontSpec;
169 fontSpec.iHeight = (fontSpec.iHeight * iZoomFactor + EZoomOneToOne / 2) / EZoomOneToOne;
170 return (const_cast<MGraphicsDeviceMap*>(iDevice))->GetNearestFontToDesignHeightInTwips(aFont, fontSpec);
174 Gets the font which is the nearest to the given font specification.
175 Matching to maximum height returns a font that will fit within the height specified.
177 @param aFont On return, contains a pointer to the nearest font.
178 @param aFontSpec The specification of the font to be matched.
179 @param aMaxHeight The maximum height within which the font must fit.
180 This overrides the height specified in aFontSpec. If maximum height
181 is greater than 1024 pixels, the function returns KErrTooBig. And
182 returns KErrArgument if equals to 1 pixel.
183 @return KErrNone if successful; a system-wide error code otherwise.
187 EXPORT_C TInt TZoomFactor::GetNearestFontToMaxHeightInTwips(CFont*& aFont, const TFontSpec& aFontSpec, TInt aMaxHeight)
189 GDI_ASSERT_ALWAYS_GENERAL(iDevice, User::Panic(KGdiTZoomFactorPanicCategory, KErrNotFound));
190 TFontSpec fontSpec = aFontSpec;
191 fontSpec.iHeight = (fontSpec.iHeight * iZoomFactor + EZoomOneToOne / 2) / EZoomOneToOne;
192 aMaxHeight = (aMaxHeight * iZoomFactor + EZoomOneToOne / 2) / EZoomOneToOne;
193 return (const_cast<MGraphicsDeviceMap*>(iDevice))->GetNearestFontToMaxHeightInTwips(aFont, fontSpec, aMaxHeight);
196 /** Releases the specified font.
198 This function implements the pure virtual function defined in
199 MGraphicsDeviceMap::ReleaseFont()
200 @param aFont A pointer to the font to be released. */
201 EXPORT_C void TZoomFactor::ReleaseFont(CFont* aFont)
203 GDI_ASSERT_ALWAYS_GENERAL(iDevice != NULL, User::Panic(KGdiTZoomFactorPanicCategory, KErrNotFound));
204 ((MGraphicsDeviceMap*)iDevice)->ReleaseFont(aFont);