|
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 |
|
25 /// Namespace for local PresentationUtils definitions |
|
26 namespace __RVCT_UNS_Pbk2PresentationUtils { |
|
27 |
|
28 // LOCAL CONSTANTS |
|
29 enum TPanicCode |
|
30 { |
|
31 EPanicPreCond_CopyAndReplaceChars = 1 |
|
32 }; |
|
33 |
|
34 // default presentation resource file |
|
35 _LIT( KPbk2PresentationResFile, "Pbk2Presentation.rsc" ); |
|
36 // Chinese presentation resource file |
|
37 _LIT( KPbk2PresentationResFileChinese, "Pbk2PresentationChinese.rsc" ); |
|
38 |
|
39 // ================= LOCAL FUNCTIONS ======================= |
|
40 #ifdef _DEBUG |
|
41 void Panic(TPanicCode aReason) |
|
42 { |
|
43 _LIT(KPanicText, "Pbk2PresentationUtils"); |
|
44 User::Panic(KPanicText, aReason); |
|
45 } |
|
46 #endif // _DEBUG |
|
47 |
|
48 // Zero Width characters |
|
49 const TUint KZWSCharacter = 0x200B; |
|
50 const TUint KZWNJCharacter = 0x200C; |
|
51 const TUint KZWJCharacter = 0x200D; |
|
52 |
|
53 // ================= LOCAL FUNCTIONS ======================= |
|
54 |
|
55 /** |
|
56 * Check if the given char is a zero width character: |
|
57 * Zero Width Space, Zero Width Non-Joiner |
|
58 * or Zero Width Joiner character |
|
59 * @param aChar Given character |
|
60 * @return ETrue if aChar is zero width character. |
|
61 * EFalse if aChar is not specified zero width character. |
|
62 */ |
|
63 inline TBool ZWSCharacter( const TChar aChar ) |
|
64 { |
|
65 const TChar zwsChar( KZWSCharacter ); |
|
66 const TChar zwnjChar( KZWNJCharacter ); |
|
67 const TChar zwjChar( KZWJCharacter ); |
|
68 return ( aChar == zwsChar ) || ( aChar == zwnjChar ) || |
|
69 ( aChar == zwjChar ); |
|
70 } |
|
71 |
|
72 } // __RVCT_UNS_Pbk2PresentationUtils |
|
73 |
|
74 using namespace __RVCT_UNS_Pbk2PresentationUtils; |
|
75 |
|
76 |
|
77 // ================= MEMBER FUNCTIONS ======================= |
|
78 |
|
79 // -------------------------------------------------------------------------- |
|
80 // Pbk2PresentationUtils::TrimAllAppend |
|
81 // -------------------------------------------------------------------------- |
|
82 // |
|
83 TInt Pbk2PresentationUtils::TrimAllAppend(const TDesC& aText, TDes& aDest) |
|
84 { |
|
85 const TInt oldDestLength = aDest.Length(); |
|
86 |
|
87 // 1. Find first non space character |
|
88 const TInt textLength = aText.Length(); |
|
89 TInt firstNonSpaceChar = 0; |
|
90 while(firstNonSpaceChar < textLength |
|
91 && TChar(aText[firstNonSpaceChar]).IsSpace()) |
|
92 { |
|
93 ++firstNonSpaceChar; |
|
94 } |
|
95 |
|
96 // 2. Append the source text to destination, |
|
97 // not including the leading spaces |
|
98 aDest.Append(aText.Mid(firstNonSpaceChar)); |
|
99 // 3. Trim the new part of the destination to remove |
|
100 // trailing and middle spaces |
|
101 TPtr ptr = aDest.MidTPtr(oldDestLength); |
|
102 ptr.TrimAll(); |
|
103 aDest.SetLength(oldDestLength); |
|
104 aDest.Append(ptr); |
|
105 |
|
106 return aDest.Length() - oldDestLength; |
|
107 } |
|
108 |
|
109 // -------------------------------------------------------------------------- |
|
110 // Pbk2PresentationUtils::TrimRightAppend |
|
111 // -------------------------------------------------------------------------- |
|
112 // |
|
113 EXPORT_C TInt Pbk2PresentationUtils::TrimRightAppend( |
|
114 const TDesC& aText, TDes& aDest) |
|
115 { |
|
116 // Create modifiable descriptor pointer that points to the non-used space |
|
117 // at the end of the destination buffer |
|
118 TPtr temp( |
|
119 (TUint16*) aDest.MidTPtr( aDest.Length() ).Ptr(), |
|
120 aDest.MaxLength() - aDest.Length() ); |
|
121 temp.Append( aText ); |
|
122 temp.TrimRight(); |
|
123 // Update the destination descriptor length to match the actual contents |
|
124 aDest.SetLength( aDest.Length() + temp.Length() ); |
|
125 return temp.Length(); |
|
126 } |
|
127 |
|
128 // -------------------------------------------------------------------------- |
|
129 // Pbk2PresentationUtils::AppendAndReplaceChars |
|
130 // -------------------------------------------------------------------------- |
|
131 // |
|
132 EXPORT_C TInt Pbk2PresentationUtils::AppendAndReplaceChars |
|
133 (TDes& aDest, const TDesC& aSrc, |
|
134 const TDesC& aCharsToReplace, const TDesC& aReplaceChars) |
|
135 { |
|
136 // Check that aDest has enough capacity |
|
137 __ASSERT_DEBUG((aDest.MaxLength()-aDest.Length()) >= aSrc.Length(), |
|
138 Panic(EPanicPreCond_CopyAndReplaceChars)); |
|
139 // Check that a replacament is provided for all characters |
|
140 __ASSERT_DEBUG(aCharsToReplace.Length() == aReplaceChars.Length(), |
|
141 Panic(EPanicPreCond_CopyAndReplaceChars)); |
|
142 |
|
143 TInt count, i; |
|
144 const TInt sourceLenght = aSrc.Length(); |
|
145 for (count=0, i=0; i < sourceLenght; ++i) |
|
146 { |
|
147 TText ch = aSrc[i]; |
|
148 const TInt replacePos = aCharsToReplace.Locate(aSrc[i]); |
|
149 if (replacePos != KErrNotFound) |
|
150 { |
|
151 ++count; |
|
152 ch = aReplaceChars[replacePos]; |
|
153 } |
|
154 aDest.Append(ch); |
|
155 } |
|
156 |
|
157 return count; |
|
158 } |
|
159 |
|
160 // -------------------------------------------------------------------------- |
|
161 // Pbk2PresentationUtils::ReplaceNonGraphicCharacters |
|
162 // -------------------------------------------------------------------------- |
|
163 // |
|
164 EXPORT_C void Pbk2PresentationUtils::ReplaceNonGraphicCharacters |
|
165 (TDes& aText, TText aChar) |
|
166 { |
|
167 const TInt len = aText.Length(); |
|
168 for ( TInt i=0; i < len; ++i ) |
|
169 { |
|
170 if ( !TChar(aText[i]).IsGraph() && |
|
171 !ZWSCharacter( aText[i] ) ) |
|
172 { |
|
173 // If non-graphic char is specified in ZWSCharacter, |
|
174 // it will not be replaced. Otherwise replace non-graphic |
|
175 // character with aChar. |
|
176 aText[i] = aChar; |
|
177 } |
|
178 } |
|
179 } |
|
180 |
|
181 // -------------------------------------------------------------------------- |
|
182 // Pbk2PresentationUtils::AppendWithNewlineTranslationL |
|
183 // -------------------------------------------------------------------------- |
|
184 // |
|
185 EXPORT_C void Pbk2PresentationUtils::AppendWithNewlineTranslationL |
|
186 ( TDes& aBuffer, const TDesC& aText ) |
|
187 { |
|
188 const TUint16 KCarriageReturn = 0x000d; |
|
189 const TUint16 KLineFeed = 0x000a; |
|
190 const TUint16 KParagraphSeparator = 0x2029; |
|
191 |
|
192 const TInt length( aText.Length() ); |
|
193 const TInt max( aBuffer.MaxLength() ); |
|
194 |
|
195 for ( register TInt i = 0; |
|
196 (i < length) && (aBuffer.Length() < max); ++i ) |
|
197 { |
|
198 register TUint16 ch = aText[i]; |
|
199 if ( ch == KCarriageReturn ) |
|
200 { |
|
201 ch = KParagraphSeparator; |
|
202 // Skip linefeeds that follow carriage return |
|
203 if ( (i < length-1) && (aText[i+1] == KLineFeed) ) |
|
204 { |
|
205 ++i; |
|
206 } |
|
207 } |
|
208 else if ( ch == KLineFeed ) |
|
209 { |
|
210 ch = KParagraphSeparator; |
|
211 } |
|
212 aBuffer.Append( ch ); |
|
213 } |
|
214 } |
|
215 |
|
216 // -------------------------------------------------------------------------- |
|
217 // Pbk2PresentationUtils::PresentationResourceFile |
|
218 // -------------------------------------------------------------------------- |
|
219 // |
|
220 EXPORT_C const TDesC& Pbk2PresentationUtils::PresentationResourceFile() |
|
221 { |
|
222 if (FeatureManager::FeatureSupported(KFeatureIdChinese)) |
|
223 { |
|
224 return KPbk2PresentationResFileChinese; |
|
225 } |
|
226 return KPbk2PresentationResFile; |
|
227 } |
|
228 |
|
229 |
|
230 // End of File |