|
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". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include <gdi.h> |
|
17 #include "GDIPANIC.h" |
|
18 |
|
19 _LIT(KGdiTZoomFactorPanicCategory,"TZoomFactor"); |
|
20 |
|
21 // |
|
22 // TZoomFactor |
|
23 // |
|
24 |
|
25 |
|
26 EXPORT_C TZoomFactor::TZoomFactor(): |
|
27 iZoomFactor(EZoomOneToOne), |
|
28 iDevice(NULL) |
|
29 /** Constructs a default zoom factor object. |
|
30 |
|
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. */ |
|
35 {} |
|
36 |
|
37 |
|
38 EXPORT_C TZoomFactor::~TZoomFactor() |
|
39 /** Destructor. |
|
40 |
|
41 Frees resources owned by the object, prior to its destruction. */ |
|
42 {} |
|
43 |
|
44 |
|
45 EXPORT_C TInt TZoomFactor::ZoomFactor() const |
|
46 /** Gets the zoom factor. |
|
47 |
|
48 @return The zoom factor */ |
|
49 { |
|
50 return iZoomFactor; |
|
51 } |
|
52 |
|
53 |
|
54 EXPORT_C void TZoomFactor::SetZoomFactor(TInt aZoomFactor) |
|
55 /** Sets the zoom factor. |
|
56 |
|
57 @param aZoomFactor The desired zoom factor. */ |
|
58 { |
|
59 iZoomFactor=aZoomFactor; |
|
60 } |
|
61 |
|
62 |
|
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 |
|
66 factor is associated. |
|
67 |
|
68 This setting is used by all the twips to pixels and pixels to twips conversion |
|
69 functions. |
|
70 |
|
71 @param aSizeInPixels The size of the graphics device area in pixels. |
|
72 @param aSizeInTwips The size of the graphics device area in twips. */ |
|
73 { |
|
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) |
|
78 { |
|
79 iZoomFactor=0; |
|
80 return; |
|
81 } |
|
82 iZoomFactor = (1000*aSizeInPixels.iWidth+(devicepixelwidth/2))/devicepixelwidth; |
|
83 TInt temp = (1000*aSizeInPixels.iHeight+(devicepixelheight/2))/devicepixelheight; |
|
84 if (temp<iZoomFactor) |
|
85 iZoomFactor = temp; |
|
86 } |
|
87 |
|
88 EXPORT_C TInt TZoomFactor::HorizontalTwipsToPixels(TInt aTwipWidth) const |
|
89 /** Converts a horizontal dimension from twips to pixels on the graphics |
|
90 device. |
|
91 |
|
92 This function implements the pure virtual function defined in |
|
93 MGraphicsDeviceMap::HorizontalTwipsToPixels() */ |
|
94 { |
|
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; |
|
100 } |
|
101 |
|
102 |
|
103 EXPORT_C TInt TZoomFactor::VerticalTwipsToPixels(TInt aTwipHeight) const |
|
104 /** Converts a vertical dimension from twips to pixels on the graphics |
|
105 device. |
|
106 |
|
107 This function implements the pure virtual function defined in |
|
108 MGraphicsDeviceMap::VerticalTwipsToPixels() */ |
|
109 { |
|
110 GDI_ASSERT_ALWAYS_GENERAL(iDevice!=NULL,User::Panic(KGdiTZoomFactorPanicCategory,KErrNotFound)); |
|
111 return (iDevice->VerticalTwipsToPixels(((aTwipHeight*iZoomFactor)/125))+((aTwipHeight<0)?-4:4))/8; |
|
112 } |
|
113 |
|
114 |
|
115 EXPORT_C TInt TZoomFactor::HorizontalPixelsToTwips(TInt aPixelWidth) const |
|
116 /** Converts a horizontal dimension from pixels to twips on the graphics |
|
117 device. |
|
118 |
|
119 This function implements the pure virtual function defined in |
|
120 MGraphicsDeviceMap::HorizontalPixelsToTwips() */ |
|
121 { |
|
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; |
|
126 if(eightTimesZoom) |
|
127 return temp/eightTimesZoom; |
|
128 return(0); |
|
129 } |
|
130 |
|
131 EXPORT_C TInt TZoomFactor::VerticalPixelsToTwips(TInt aPixelHeight) const |
|
132 /** Converts a vertical dimension from pixels to twips on the graphics |
|
133 device. |
|
134 |
|
135 This function implements the pure virtual function defined in |
|
136 MGraphicsDeviceMap::VerticalPixelsToTwips() */ |
|
137 { |
|
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; |
|
142 if(eightTimesZoom) |
|
143 return temp/eightTimesZoom; |
|
144 return(0); |
|
145 } |
|
146 |
|
147 /** |
|
148 @publishedAll |
|
149 */ |
|
150 EXPORT_C TInt TZoomFactor::GetNearestFontInTwips(CFont*& aFont, const TFontSpec& aFontSpec) |
|
151 { |
|
152 return GetNearestFontToDesignHeightInTwips(aFont, aFontSpec); |
|
153 } |
|
154 |
|
155 /** |
|
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. |
|
158 |
|
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. |
|
162 @publishedAll |
|
163 @released |
|
164 */ |
|
165 EXPORT_C TInt TZoomFactor::GetNearestFontToDesignHeightInTwips(CFont*& aFont, const TFontSpec& aFontSpec) |
|
166 { |
|
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); |
|
171 } |
|
172 |
|
173 /** |
|
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. |
|
176 |
|
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. |
|
184 @publishedAll |
|
185 @released |
|
186 */ |
|
187 EXPORT_C TInt TZoomFactor::GetNearestFontToMaxHeightInTwips(CFont*& aFont, const TFontSpec& aFontSpec, TInt aMaxHeight) |
|
188 { |
|
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); |
|
194 } |
|
195 |
|
196 /** Releases the specified font. |
|
197 |
|
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) |
|
202 { |
|
203 GDI_ASSERT_ALWAYS_GENERAL(iDevice != NULL, User::Panic(KGdiTZoomFactorPanicCategory, KErrNotFound)); |
|
204 ((MGraphicsDeviceMap*)iDevice)->ReleaseFont(aFont); |
|
205 } |