|
1 /* |
|
2 * Copyright (c) 2004-2005 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: Implementation of TtsCommon |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include "srsfbldvariant.hrh" |
|
21 #include "ttscommonbody.h" |
|
22 #include "nssttscommon.h" |
|
23 |
|
24 // ============================ MEMBER FUNCTIONS =============================== |
|
25 |
|
26 // ----------------------------------------------------------------------------- |
|
27 // CTtsParsedTextBody::CTtsParsedTextBody |
|
28 // C++ default constructor can NOT contain any code, that |
|
29 // might leave. |
|
30 // ----------------------------------------------------------------------------- |
|
31 // |
|
32 CTtsParsedTextBody::CTtsParsedTextBody() : |
|
33 iText( NULL ), |
|
34 iPhonemeSequence( NULL ), |
|
35 iPhonemeNotation( NULL ) |
|
36 { |
|
37 // nothing |
|
38 } |
|
39 |
|
40 // ----------------------------------------------------------------------------- |
|
41 // CTtsParsedTextBody::ConstructL |
|
42 // Symbian 2nd phase constructor can leave. |
|
43 // ----------------------------------------------------------------------------- |
|
44 // |
|
45 void CTtsParsedTextBody::ConstructL( |
|
46 const TDesC& aText, |
|
47 const TDesC8& aPhonemeSequence, |
|
48 const TDesC& aPhonemeNotation ) |
|
49 { |
|
50 |
|
51 if ( aText != KNullDesC ) |
|
52 { |
|
53 // copy text |
|
54 iText = HBufC::NewL( aText.Length() ); |
|
55 TPtr tempPtr( iText->Des() ); |
|
56 tempPtr = aText; |
|
57 } |
|
58 |
|
59 if ( aPhonemeSequence != KNullDesC8 ) |
|
60 { |
|
61 // copy phoneme sequence |
|
62 iPhonemeSequence = HBufC8::NewL( aPhonemeSequence.Length() ); |
|
63 TPtr8 tempPtr8( iPhonemeSequence->Des() ); |
|
64 tempPtr8 = aPhonemeSequence; |
|
65 } |
|
66 |
|
67 if ( aPhonemeNotation != KNullDesC ) |
|
68 { |
|
69 // copy phoneme notation |
|
70 iPhonemeNotation = HBufC::NewL( aPhonemeNotation.Length() ); |
|
71 TPtr tempPtr( iPhonemeNotation->Des() ); |
|
72 tempPtr = aPhonemeNotation; |
|
73 } |
|
74 } |
|
75 |
|
76 // ----------------------------------------------------------------------------- |
|
77 // CTtsParsedTextBody::NewL |
|
78 // Two-phased constructor. |
|
79 // ----------------------------------------------------------------------------- |
|
80 // |
|
81 CTtsParsedTextBody* CTtsParsedTextBody::NewL( |
|
82 const TDesC& aText, |
|
83 const TDesC8& aPhonemeSequence, |
|
84 const TDesC& aPhonemeNotation ) |
|
85 { |
|
86 CTtsParsedTextBody* self = new( ELeave ) CTtsParsedTextBody; |
|
87 |
|
88 CleanupStack::PushL( self ); |
|
89 self->ConstructL( aText, |
|
90 aPhonemeSequence, |
|
91 aPhonemeNotation ); |
|
92 CleanupStack::Pop(); |
|
93 |
|
94 return self; |
|
95 } |
|
96 |
|
97 |
|
98 // Destructor |
|
99 CTtsParsedTextBody::~CTtsParsedTextBody() |
|
100 { |
|
101 delete iText; |
|
102 delete iPhonemeSequence; |
|
103 delete iPhonemeNotation; |
|
104 iSegmentArray.Reset(); |
|
105 } |
|
106 |
|
107 |
|
108 // ----------------------------------------------------------------------------- |
|
109 // CTtsParsedTextBody::AddSegmentL |
|
110 // Add new segment to array |
|
111 // (other items were commented in a header). |
|
112 // ----------------------------------------------------------------------------- |
|
113 // |
|
114 void CTtsParsedTextBody::AddSegmentL( |
|
115 const TTtsSegment& aSegment, |
|
116 TInt aIndex ) |
|
117 { |
|
118 if ( aIndex < 0 ) |
|
119 { |
|
120 User::Leave( KErrArgument ); // The index is negative |
|
121 } |
|
122 else if ( aIndex >= iSegmentArray.Count() ) |
|
123 { |
|
124 // added to end |
|
125 User::LeaveIfError( iSegmentArray.Append( aSegment ) ); |
|
126 } |
|
127 else |
|
128 { |
|
129 // added into middle |
|
130 User::LeaveIfError( iSegmentArray.Insert( aSegment, aIndex ) ); |
|
131 } |
|
132 } |
|
133 |
|
134 // ----------------------------------------------------------------------------- |
|
135 // CTtsParsedTextBody::DeleteSegment |
|
136 // Delete segment |
|
137 // (other items were commented in a header). |
|
138 // ----------------------------------------------------------------------------- |
|
139 // |
|
140 void CTtsParsedTextBody::DeleteSegmentL( |
|
141 TInt aIndex ) |
|
142 { |
|
143 if ( aIndex < 0 || aIndex >= iSegmentArray.Count() ) |
|
144 { |
|
145 User::Leave( KErrArgument ); // The index is out of bounds. |
|
146 } |
|
147 |
|
148 iSegmentArray.Remove( aIndex ); |
|
149 } |
|
150 |
|
151 // ----------------------------------------------------------------------------- |
|
152 // CTtsParsedTextBody::IsValid |
|
153 // Checks that segments do not overlap or pointer outside. |
|
154 // (other items were commented in a header). |
|
155 // ----------------------------------------------------------------------------- |
|
156 // |
|
157 TInt CTtsParsedTextBody::IsValid() const |
|
158 { |
|
159 TInt index( 0 ); |
|
160 |
|
161 if ( iPhonemeSequence == NULL && |
|
162 iText == NULL && |
|
163 iSegmentArray.Count() > 0 ) |
|
164 { |
|
165 // no phoneme sequence and no text but segments still exist |
|
166 return EFalse; |
|
167 } |
|
168 |
|
169 // check phoneme sequence part |
|
170 if ( iPhonemeSequence ) |
|
171 { |
|
172 const TUint8* beginPhSeqPtr = iPhonemeSequence->Ptr(); |
|
173 const TUint8* endPhSeqPtr = iPhonemeSequence->Ptr() |
|
174 + iPhonemeSequence->Length() - 1; |
|
175 |
|
176 for ( index = 0; index < iSegmentArray.Count(); index++ ) |
|
177 { |
|
178 // check that pointers do not point outside |
|
179 const TTtsSegment& segment = iSegmentArray[index]; |
|
180 const TDesC8& phSeq = segment.PhonemeSequencePtr(); |
|
181 if ( phSeq != KNullDesC8 ) |
|
182 { |
|
183 const TUint8* phSeqPtr = phSeq.Ptr(); |
|
184 if ( phSeqPtr < beginPhSeqPtr || |
|
185 phSeqPtr > endPhSeqPtr || |
|
186 phSeqPtr + phSeq.Length() - 1 > endPhSeqPtr ) |
|
187 { |
|
188 return EFalse; |
|
189 } |
|
190 |
|
191 |
|
192 // check order and overlapping |
|
193 if ( index > 0 ) |
|
194 { |
|
195 const TDesC8& prevPhSeq |
|
196 = iSegmentArray[index - 1].PhonemeSequencePtr(); |
|
197 if ( prevPhSeq == KNullDesC8 ) |
|
198 { |
|
199 // all pointer must point to either null or valid data |
|
200 return EFalse; |
|
201 } |
|
202 |
|
203 const TUint8* prevEndPhSeqPtr |
|
204 = prevPhSeq.Ptr() + prevPhSeq.Length() - 1; |
|
205 if ( prevEndPhSeqPtr >= phSeqPtr ) |
|
206 { |
|
207 return EFalse; |
|
208 } |
|
209 } |
|
210 } |
|
211 } |
|
212 } |
|
213 |
|
214 // check phoneme sequence part |
|
215 if ( iText ) |
|
216 { |
|
217 const TUint16* beginTextPtr = iText->Ptr(); |
|
218 const TUint16* endTextPtr = iText->Ptr() + iText->Length() - 1; |
|
219 |
|
220 for ( index = 0; index < iSegmentArray.Count(); index++ ) |
|
221 { |
|
222 // check that pointers do not point outside |
|
223 const TTtsSegment& segment = iSegmentArray[index]; |
|
224 const TDesC& text = segment.TextPtr(); |
|
225 if ( text != KNullDesC ) |
|
226 { |
|
227 const TUint16* textPtr = text.Ptr(); |
|
228 if ( textPtr < beginTextPtr || |
|
229 textPtr > endTextPtr || |
|
230 textPtr + text.Length() - 1 > endTextPtr ) |
|
231 { |
|
232 return EFalse; |
|
233 } |
|
234 |
|
235 // check order and overlapping |
|
236 if ( index > 0 ) |
|
237 { |
|
238 const TDesC& prevText = iSegmentArray[index - 1].TextPtr(); |
|
239 if ( prevText == KNullDesC ) |
|
240 { |
|
241 // all pointer must point to either null or valid data |
|
242 return EFalse; |
|
243 } |
|
244 const TUint16* prevEndTextPtr |
|
245 = prevText.Ptr() + prevText.Length() - 1; |
|
246 if ( prevEndTextPtr >= textPtr ) |
|
247 { |
|
248 return EFalse; |
|
249 } |
|
250 } |
|
251 } |
|
252 } |
|
253 } |
|
254 |
|
255 // all checks passed |
|
256 return ETrue; |
|
257 } |
|
258 |
|
259 // ----------------------------------------------------------------------------- |
|
260 // CTtsParsedTextBody::NumberOfSegments |
|
261 // Return number of segments |
|
262 // (other items were commented in a header). |
|
263 // ----------------------------------------------------------------------------- |
|
264 // |
|
265 TInt CTtsParsedTextBody::NumberOfSegments() const |
|
266 { |
|
267 return iSegmentArray.Count(); |
|
268 } |
|
269 |
|
270 // ----------------------------------------------------------------------------- |
|
271 // CTtsParsedTextBody::PhonemeNotation |
|
272 // Returns phoneme notation |
|
273 // (other items were commented in a header). |
|
274 // ----------------------------------------------------------------------------- |
|
275 // |
|
276 const TDesC& CTtsParsedTextBody::PhonemeNotation() const |
|
277 { |
|
278 if ( iPhonemeNotation ) |
|
279 { |
|
280 // Phoneme sequence has been set |
|
281 return *iPhonemeNotation; |
|
282 } |
|
283 return KNullDesC; |
|
284 } |
|
285 |
|
286 // ----------------------------------------------------------------------------- |
|
287 // CTtsParsedTextBody::PhonemeSequence |
|
288 // Returns phoneme sequence |
|
289 // (other items were commented in a header). |
|
290 // ----------------------------------------------------------------------------- |
|
291 // |
|
292 const TDesC8& CTtsParsedTextBody::PhonemeSequence() const |
|
293 { |
|
294 if ( iPhonemeSequence ) |
|
295 { |
|
296 // Phoneme sequence has been set |
|
297 return *iPhonemeSequence; |
|
298 } |
|
299 return KNullDesC8; |
|
300 } |
|
301 |
|
302 // ----------------------------------------------------------------------------- |
|
303 // CTtsParsedTextBody::SegmentL |
|
304 // Returns segment of given index |
|
305 // (other items were commented in a header). |
|
306 // ----------------------------------------------------------------------------- |
|
307 // |
|
308 const TTtsSegment& CTtsParsedTextBody::SegmentL( |
|
309 TInt aIndex ) const |
|
310 { |
|
311 if ( aIndex < 0 || aIndex >= iSegmentArray.Count() ) |
|
312 { |
|
313 User::Leave( KErrArgument ); |
|
314 } |
|
315 return iSegmentArray[aIndex]; |
|
316 } |
|
317 |
|
318 // ----------------------------------------------------------------------------- |
|
319 // CTtsParsedTextBody::SetPhonemeNotationL |
|
320 // Sets phoneme notation |
|
321 // (other items were commented in a header). |
|
322 // ----------------------------------------------------------------------------- |
|
323 // |
|
324 void CTtsParsedTextBody::SetPhonemeNotationL( |
|
325 const TDesC& aPhonemeNotation ) |
|
326 { |
|
327 if ( !iPhonemeNotation ) |
|
328 { |
|
329 // copy phoneme notation |
|
330 iPhonemeNotation = HBufC::NewL( aPhonemeNotation.Length() ); |
|
331 TPtr tempPtr( iPhonemeNotation->Des() ); |
|
332 tempPtr = aPhonemeNotation; |
|
333 } |
|
334 else |
|
335 { |
|
336 TPtr bufferPtr( iPhonemeNotation->Des() ); |
|
337 if ( aPhonemeNotation.Length() > bufferPtr.MaxLength() ) |
|
338 { |
|
339 // new buffer does not fit into old one |
|
340 iPhonemeNotation->ReAllocL( aPhonemeNotation.Length() ); |
|
341 bufferPtr.Set( iPhonemeNotation->Des() ); |
|
342 } |
|
343 bufferPtr = aPhonemeNotation; |
|
344 } |
|
345 } |
|
346 |
|
347 // ----------------------------------------------------------------------------- |
|
348 // CTtsParsedTextBody::SetPhonemeSequenceL |
|
349 // Sets phoneme sequence |
|
350 // (other items were commented in a header). |
|
351 // ----------------------------------------------------------------------------- |
|
352 // |
|
353 void CTtsParsedTextBody::SetPhonemeSequenceL( |
|
354 const TDesC8& aPhonemeSequence ) |
|
355 { |
|
356 |
|
357 if ( !iPhonemeSequence ) |
|
358 { |
|
359 // create new phoneme sequence |
|
360 iPhonemeSequence = HBufC8::NewL( aPhonemeSequence.Length() ); |
|
361 TPtr8 tempPtr8( iPhonemeSequence->Des() ); |
|
362 tempPtr8 = aPhonemeSequence; |
|
363 } |
|
364 else |
|
365 { |
|
366 TPtr8 bufferPtr( iPhonemeSequence->Des() ); |
|
367 if ( aPhonemeSequence.Length() > bufferPtr.MaxLength() ) |
|
368 { |
|
369 // new buffer does not fit into old one |
|
370 iPhonemeSequence = iPhonemeSequence->ReAllocL( aPhonemeSequence.Length() ); |
|
371 bufferPtr.Set( iPhonemeSequence->Des() ); |
|
372 } |
|
373 bufferPtr = aPhonemeSequence; |
|
374 } |
|
375 |
|
376 } |
|
377 |
|
378 // ----------------------------------------------------------------------------- |
|
379 // CTtsParsedTextBody::SetTextL |
|
380 // Sets text |
|
381 // (other items were commented in a header). |
|
382 // ----------------------------------------------------------------------------- |
|
383 // |
|
384 void CTtsParsedTextBody::SetTextL( |
|
385 const TDesC& aText ) |
|
386 { |
|
387 if ( !iText ) |
|
388 { |
|
389 // create new text |
|
390 iText = HBufC::NewL( aText.Length() ); |
|
391 TPtr tempPtr( iText->Des() ); |
|
392 tempPtr = aText; |
|
393 } |
|
394 else |
|
395 { |
|
396 TPtr bufferPtr( iText->Des() ); |
|
397 if ( aText.Length() > bufferPtr.MaxLength() ) |
|
398 { |
|
399 // new buffer does not fit into old one |
|
400 iText = iText->ReAllocL( aText.Length() ); |
|
401 bufferPtr.Set( iText->Des() ); |
|
402 } |
|
403 bufferPtr = aText; |
|
404 } |
|
405 } |
|
406 |
|
407 |
|
408 // ----------------------------------------------------------------------------- |
|
409 // CTtsParsedTextBody::Text |
|
410 // Returns text |
|
411 // (other items were commented in a header). |
|
412 // ----------------------------------------------------------------------------- |
|
413 // |
|
414 const TDesC& CTtsParsedTextBody::Text() const |
|
415 { |
|
416 if ( iText ) |
|
417 { |
|
418 // text has been set |
|
419 return *iText; |
|
420 } |
|
421 return KNullDesC; |
|
422 } |
|
423 |
|
424 // End of File |