author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Wed, 15 Sep 2010 11:56:55 +0300 | |
branch | RCL_3 |
changeset 74 | 6b5524b4f673 |
parent 63 | f4a778e096c2 |
child 85 | 38bb213f60ba |
permissions | -rw-r--r-- |
63 | 1 |
/* |
2 |
* Copyright (c) 2002-2007 Nokia Corporation and/or its subsidiary(-ies). |
|
3 |
* All rights reserved. |
|
4 |
* This component and the accompanying materials are made available |
|
5 |
* under the terms of "Eclipse Public License v1.0" |
|
6 |
* which accompanies this distribution, and is available |
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 |
* |
|
9 |
* Initial Contributors: |
|
10 |
* Nokia Corporation - initial contribution. |
|
11 |
* |
|
12 |
* Contributors: |
|
13 |
* |
|
14 |
* Description: |
|
15 |
* |
|
16 |
*/ |
|
17 |
||
18 |
||
19 |
// INCLUDE FILES |
|
20 |
#include "Pbk2PresentationUtils.h" |
|
21 |
||
22 |
// From system |
|
23 |
#include <featmgr.h> |
|
24 |
#include <fbs.h> |
|
25 |
||
26 |
/// Namespace for local PresentationUtils definitions |
|
27 |
namespace __RVCT_UNS_Pbk2PresentationUtils { |
|
28 |
||
29 |
// LOCAL CONSTANTS |
|
30 |
enum TPanicCode |
|
31 |
{ |
|
32 |
EPanicPreCond_CopyAndReplaceChars = 1 |
|
33 |
}; |
|
34 |
||
35 |
// default presentation resource file |
|
36 |
_LIT( KPbk2PresentationResFile, "Pbk2Presentation.rsc" ); |
|
37 |
// Chinese presentation resource file |
|
38 |
_LIT( KPbk2PresentationResFileChinese, "Pbk2PresentationChinese.rsc" ); |
|
74
6b5524b4f673
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
63
diff
changeset
|
39 |
// UnSync Chinese presentation resource file |
6b5524b4f673
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
63
diff
changeset
|
40 |
_LIT( KPbk2PresentationResFileChineseUnSync, "Pbk2PresentationChineseUnSync.rsc" ); |
63 | 41 |
|
42 |
// ================= LOCAL FUNCTIONS ======================= |
|
43 |
#ifdef _DEBUG |
|
44 |
void Panic(TPanicCode aReason) |
|
45 |
{ |
|
46 |
_LIT(KPanicText, "Pbk2PresentationUtils"); |
|
47 |
User::Panic(KPanicText, aReason); |
|
48 |
} |
|
49 |
#endif // _DEBUG |
|
50 |
||
51 |
// Zero Width characters |
|
52 |
const TUint KZWSCharacter = 0x200B; |
|
53 |
const TUint KZWNJCharacter = 0x200C; |
|
54 |
const TUint KZWJCharacter = 0x200D; |
|
55 |
||
56 |
// ================= LOCAL FUNCTIONS ======================= |
|
57 |
||
58 |
/** |
|
59 |
* Check if the given char is a zero width character: |
|
60 |
* Zero Width Space, Zero Width Non-Joiner |
|
61 |
* or Zero Width Joiner character |
|
62 |
* @param aChar Given character |
|
63 |
* @return ETrue if aChar is zero width character. |
|
64 |
* EFalse if aChar is not specified zero width character. |
|
65 |
*/ |
|
66 |
inline TBool ZWSCharacter( const TChar aChar ) |
|
67 |
{ |
|
68 |
const TChar zwsChar( KZWSCharacter ); |
|
69 |
const TChar zwnjChar( KZWNJCharacter ); |
|
70 |
const TChar zwjChar( KZWJCharacter ); |
|
71 |
return ( aChar == zwsChar ) || ( aChar == zwnjChar ) || |
|
72 |
( aChar == zwjChar ); |
|
73 |
} |
|
74 |
||
75 |
} // __RVCT_UNS_Pbk2PresentationUtils |
|
76 |
||
77 |
using namespace __RVCT_UNS_Pbk2PresentationUtils; |
|
78 |
||
79 |
||
80 |
// ================= MEMBER FUNCTIONS ======================= |
|
81 |
||
82 |
// -------------------------------------------------------------------------- |
|
83 |
// Pbk2PresentationUtils::TrimAllAppend |
|
84 |
// -------------------------------------------------------------------------- |
|
85 |
// |
|
86 |
TInt Pbk2PresentationUtils::TrimAllAppend(const TDesC& aText, TDes& aDest) |
|
87 |
{ |
|
88 |
const TInt oldDestLength = aDest.Length(); |
|
89 |
||
90 |
// 1. Find first non space character |
|
91 |
const TInt textLength = aText.Length(); |
|
92 |
TInt firstNonSpaceChar = 0; |
|
93 |
while(firstNonSpaceChar < textLength |
|
94 |
&& TChar(aText[firstNonSpaceChar]).IsSpace()) |
|
95 |
{ |
|
96 |
++firstNonSpaceChar; |
|
97 |
} |
|
98 |
||
99 |
// 2. Append the source text to destination, |
|
100 |
// not including the leading spaces |
|
101 |
aDest.Append(aText.Mid(firstNonSpaceChar)); |
|
102 |
// 3. Trim the new part of the destination to remove |
|
103 |
// trailing and middle spaces |
|
104 |
TPtr ptr = aDest.MidTPtr(oldDestLength); |
|
105 |
ptr.TrimAll(); |
|
106 |
aDest.SetLength(oldDestLength); |
|
107 |
aDest.Append(ptr); |
|
108 |
||
109 |
return aDest.Length() - oldDestLength; |
|
110 |
} |
|
111 |
||
112 |
// -------------------------------------------------------------------------- |
|
113 |
// Pbk2PresentationUtils::TrimRightAppend |
|
114 |
// -------------------------------------------------------------------------- |
|
115 |
// |
|
116 |
EXPORT_C TInt Pbk2PresentationUtils::TrimRightAppend( |
|
117 |
const TDesC& aText, TDes& aDest) |
|
118 |
{ |
|
119 |
// Create modifiable descriptor pointer that points to the non-used space |
|
120 |
// at the end of the destination buffer |
|
121 |
TPtr temp( |
|
122 |
(TUint16*) aDest.MidTPtr( aDest.Length() ).Ptr(), |
|
123 |
aDest.MaxLength() - aDest.Length() ); |
|
124 |
temp.Append( aText ); |
|
125 |
temp.TrimRight(); |
|
126 |
// Update the destination descriptor length to match the actual contents |
|
127 |
aDest.SetLength( aDest.Length() + temp.Length() ); |
|
128 |
return temp.Length(); |
|
129 |
} |
|
130 |
||
131 |
// -------------------------------------------------------------------------- |
|
132 |
// Pbk2PresentationUtils::AppendAndReplaceChars |
|
133 |
// -------------------------------------------------------------------------- |
|
134 |
// |
|
135 |
EXPORT_C TInt Pbk2PresentationUtils::AppendAndReplaceChars |
|
136 |
(TDes& aDest, const TDesC& aSrc, |
|
137 |
const TDesC& aCharsToReplace, const TDesC& aReplaceChars) |
|
138 |
{ |
|
139 |
// Check that aDest has enough capacity |
|
140 |
__ASSERT_DEBUG((aDest.MaxLength()-aDest.Length()) >= aSrc.Length(), |
|
141 |
Panic(EPanicPreCond_CopyAndReplaceChars)); |
|
142 |
// Check that a replacament is provided for all characters |
|
143 |
__ASSERT_DEBUG(aCharsToReplace.Length() == aReplaceChars.Length(), |
|
144 |
Panic(EPanicPreCond_CopyAndReplaceChars)); |
|
145 |
||
146 |
TInt count, i; |
|
147 |
const TInt sourceLenght = aSrc.Length(); |
|
148 |
for (count=0, i=0; i < sourceLenght; ++i) |
|
149 |
{ |
|
150 |
TText ch = aSrc[i]; |
|
151 |
const TInt replacePos = aCharsToReplace.Locate(aSrc[i]); |
|
152 |
if (replacePos != KErrNotFound) |
|
153 |
{ |
|
154 |
++count; |
|
155 |
ch = aReplaceChars[replacePos]; |
|
156 |
} |
|
157 |
aDest.Append(ch); |
|
158 |
} |
|
159 |
||
160 |
return count; |
|
161 |
} |
|
162 |
||
163 |
// -------------------------------------------------------------------------- |
|
164 |
// Pbk2PresentationUtils::ReplaceNonGraphicCharacters |
|
165 |
// -------------------------------------------------------------------------- |
|
166 |
// |
|
167 |
EXPORT_C void Pbk2PresentationUtils::ReplaceNonGraphicCharacters |
|
168 |
(TDes& aText, TText aChar) |
|
169 |
{ |
|
170 |
const TInt len = aText.Length(); |
|
171 |
for ( TInt i=0; i < len; ++i ) |
|
172 |
{ |
|
173 |
if ( !TChar(aText[i]).IsGraph() && |
|
174 |
!ZWSCharacter( aText[i] ) ) |
|
175 |
{ |
|
176 |
// If non-graphic char is specified in ZWSCharacter, |
|
177 |
// it will not be replaced. Otherwise replace non-graphic |
|
178 |
// character with aChar. |
|
179 |
aText[i] = aChar; |
|
180 |
} |
|
181 |
} |
|
182 |
} |
|
183 |
||
184 |
// -------------------------------------------------------------------------- |
|
185 |
// Pbk2PresentationUtils::AppendWithNewlineTranslationL |
|
186 |
// -------------------------------------------------------------------------- |
|
187 |
// |
|
188 |
EXPORT_C void Pbk2PresentationUtils::AppendWithNewlineTranslationL |
|
189 |
( TDes& aBuffer, const TDesC& aText ) |
|
190 |
{ |
|
191 |
const TUint16 KCarriageReturn = 0x000d; |
|
192 |
const TUint16 KLineFeed = 0x000a; |
|
193 |
const TUint16 KParagraphSeparator = 0x2029; |
|
194 |
||
195 |
const TInt length( aText.Length() ); |
|
196 |
const TInt max( aBuffer.MaxLength() ); |
|
197 |
||
198 |
for ( register TInt i = 0; |
|
199 |
(i < length) && (aBuffer.Length() < max); ++i ) |
|
200 |
{ |
|
201 |
register TUint16 ch = aText[i]; |
|
202 |
if ( ch == KCarriageReturn ) |
|
203 |
{ |
|
204 |
ch = KParagraphSeparator; |
|
205 |
// Skip linefeeds that follow carriage return |
|
206 |
if ( (i < length-1) && (aText[i+1] == KLineFeed) ) |
|
207 |
{ |
|
208 |
++i; |
|
209 |
} |
|
210 |
} |
|
211 |
else if ( ch == KLineFeed ) |
|
212 |
{ |
|
213 |
ch = KParagraphSeparator; |
|
214 |
} |
|
215 |
aBuffer.Append( ch ); |
|
216 |
} |
|
217 |
} |
|
218 |
||
219 |
// -------------------------------------------------------------------------- |
|
220 |
// Pbk2PresentationUtils::PresentationResourceFile |
|
221 |
// -------------------------------------------------------------------------- |
|
222 |
// |
|
223 |
EXPORT_C const TDesC& Pbk2PresentationUtils::PresentationResourceFile() |
|
224 |
{ |
|
225 |
if (FeatureManager::FeatureSupported(KFeatureIdChinese)) |
|
226 |
{ |
|
74
6b5524b4f673
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
63
diff
changeset
|
227 |
if ( FeatureManager::FeatureSupported(KFeatureIdFfTdUnSyncabPbfieldsInd) ) |
6b5524b4f673
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
63
diff
changeset
|
228 |
{ |
6b5524b4f673
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
63
diff
changeset
|
229 |
// The resource which can't be synchronized with a server. |
6b5524b4f673
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
63
diff
changeset
|
230 |
return KPbk2PresentationResFileChineseUnSync; |
6b5524b4f673
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
63
diff
changeset
|
231 |
} |
6b5524b4f673
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
63
diff
changeset
|
232 |
else |
6b5524b4f673
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
63
diff
changeset
|
233 |
{ |
6b5524b4f673
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
63
diff
changeset
|
234 |
return KPbk2PresentationResFileChinese; |
6b5524b4f673
Revision: 201035
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
63
diff
changeset
|
235 |
} |
63 | 236 |
} |
237 |
return KPbk2PresentationResFile; |
|
238 |
} |
|
239 |
||
240 |
||
241 |
// -------------------------------------------------------------------------- |
|
242 |
// Pbk2PresentationImageUtils::CropImageL |
|
243 |
// -------------------------------------------------------------------------- |
|
244 |
// |
|
245 |
EXPORT_C void Pbk2PresentationImageUtils::CropImageL( |
|
246 |
CFbsBitmap& aBitmap, |
|
247 |
TCroppingMode aCroppingMode, |
|
248 |
const TSize& aTargetSize ) |
|
249 |
{ |
|
250 |
const TSize sourceSize( aBitmap.SizeInPixels() ); |
|
251 |
// crop the image only if the width is bigger than height |
|
252 |
TBool landscape( ETrue ); |
|
253 |
if( sourceSize.iHeight >= sourceSize.iWidth ) |
|
254 |
{ |
|
255 |
// portrait image |
|
256 |
landscape = EFalse; |
|
257 |
} |
|
258 |
// take the shorter side |
|
259 |
const TInt sideSizeH( |
|
260 |
landscape ? sourceSize.iHeight : sourceSize.iWidth ); |
|
261 |
const TInt targetSizeW( |
|
262 |
landscape ? aTargetSize.iWidth : aTargetSize.iHeight ); |
|
263 |
TInt sideSizeW( sideSizeH ); |
|
264 |
if( EOptimizedCropping == aCroppingMode && |
|
265 |
sideSizeW < targetSizeW ) |
|
266 |
{ |
|
267 |
sideSizeW = targetSizeW; |
|
268 |
if( sideSizeW >= |
|
269 |
( landscape ? sourceSize.iWidth : sourceSize.iHeight ) ) |
|
270 |
{ |
|
271 |
return; // no cropping |
|
272 |
} |
|
273 |
} |
|
274 |
||
275 |
// set target size |
|
276 |
const TSize targetSize( landscape? |
|
277 |
TSize(sideSizeW, sideSizeH ): TSize( sideSizeH, sideSizeW ) ); |
|
278 |
||
279 |
// crop from both sides |
|
280 |
const TPoint targetPoint( ( sourceSize.iWidth - targetSize.iWidth ) / 2, |
|
281 |
( sourceSize.iHeight - targetSize.iHeight ) / 2 ); |
|
282 |
const TRect targetRect( targetPoint, targetSize ); |
|
283 |
||
284 |
// create new bitmap |
|
285 |
CFbsBitmap* target = new( ELeave ) CFbsBitmap; |
|
286 |
CleanupStack::PushL( target ); |
|
287 |
const TDisplayMode displayMode( aBitmap.DisplayMode() ); |
|
288 |
User::LeaveIfError( target->Create( targetSize, displayMode ) ); |
|
289 |
||
290 |
// get scanline |
|
291 |
HBufC8* scanLine = HBufC8::NewLC( aBitmap.ScanLineLength |
|
292 |
( targetSize.iWidth, displayMode ) ); |
|
293 |
TPtr8 scanLinePtr = scanLine->Des(); |
|
294 |
||
295 |
TPoint startPoint( targetRect.iTl.iX, targetRect.iTl.iY ); |
|
296 |
TInt targetY = 0; |
|
297 |
for (; startPoint.iY < targetRect.iBr.iY; ++startPoint.iY ) |
|
298 |
{ |
|
299 |
aBitmap.GetScanLine( |
|
300 |
scanLinePtr, startPoint, targetSize.iWidth, displayMode ); |
|
301 |
target->SetScanLine( scanLinePtr, targetY++ ); |
|
302 |
} |
|
303 |
||
304 |
aBitmap.Reset(); |
|
305 |
User::LeaveIfError( aBitmap.Duplicate( target->Handle() ) ); |
|
306 |
CleanupStack::PopAndDestroy( 2, target ); // scanLine, target |
|
307 |
} |
|
308 |
||
309 |
// End of File |