a2fec9a2d78e596b848d6bf3954b3091b9f20465
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-controller.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/text/text-controller.h>
20
21 // EXTERNAL INCLUDES
22 #include <limits>
23 #include <memory.h>
24 #include <dali/public-api/adaptor-framework/key.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/devel-api/adaptor-framework/clipboard-event-notifier.h>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/internal/text/bidirectional-support.h>
30 #include <dali-toolkit/internal/text/character-set-conversion.h>
31 #include <dali-toolkit/internal/text/layouts/layout-parameters.h>
32 #include <dali-toolkit/internal/text/markup-processor.h>
33 #include <dali-toolkit/internal/text/text-controller-impl.h>
34
35 namespace
36 {
37
38 #if defined(DEBUG_ENABLED)
39   Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
40 #endif
41
42 const float MAX_FLOAT = std::numeric_limits<float>::max();
43 const unsigned int POINTS_PER_INCH = 72;
44
45 const std::string EMPTY_STRING("");
46
47 float ConvertToEven( float value )
48 {
49   int intValue(static_cast<int>( value ));
50   return static_cast<float>(intValue % 2 == 0) ? intValue : (intValue + 1);
51 }
52
53 } // namespace
54
55 namespace Dali
56 {
57
58 namespace Toolkit
59 {
60
61 namespace Text
62 {
63
64 /**
65  * @brief Adds a new font description run for the selected text.
66  *
67  * The new font parameters are added after the call to this method.
68  *
69  * @param[in] eventData The event data pointer.
70  * @param[in] logicalModel The logical model where to add the new font description run.
71  */
72 FontDescriptionRun& UpdateSelectionFontStyleRun( EventData* eventData,
73                                                  LogicalModelPtr logicalModel )
74 {
75   const bool handlesCrossed = eventData->mLeftSelectionPosition > eventData->mRightSelectionPosition;
76
77   // Get start and end position of selection
78   const CharacterIndex startOfSelectedText = handlesCrossed ? eventData->mRightSelectionPosition : eventData->mLeftSelectionPosition;
79   const Length lengthOfSelectedText = ( handlesCrossed ? eventData->mLeftSelectionPosition : eventData->mRightSelectionPosition ) - startOfSelectedText;
80
81   // Add the font run.
82   const VectorBase::SizeType numberOfRuns = logicalModel->mFontDescriptionRuns.Count();
83   logicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
84
85   FontDescriptionRun& fontDescriptionRun = *( logicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
86
87   fontDescriptionRun.characterRun.characterIndex = startOfSelectedText;
88   fontDescriptionRun.characterRun.numberOfCharacters = lengthOfSelectedText;
89
90   // Recalculate the selection highlight as the metrics may have changed.
91   eventData->mUpdateLeftSelectionPosition = true;
92   eventData->mUpdateRightSelectionPosition = true;
93
94   return fontDescriptionRun;
95 }
96
97 ControllerPtr Controller::New( ControlInterface& controlInterface )
98 {
99   return ControllerPtr( new Controller( controlInterface ) );
100 }
101
102 void Controller::EnableTextInput( DecoratorPtr decorator )
103 {
104   if( NULL == mImpl->mEventData )
105   {
106     mImpl->mEventData = new EventData( decorator );
107   }
108 }
109
110 void Controller::SetMarkupProcessorEnabled( bool enable )
111 {
112   mImpl->mMarkupProcessorEnabled = enable;
113 }
114
115 bool Controller::IsMarkupProcessorEnabled() const
116 {
117   return mImpl->mMarkupProcessorEnabled;
118 }
119
120 void Controller::SetText( const std::string& text )
121 {
122   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText\n" );
123
124   // Reset keyboard as text changed
125   mImpl->ResetImfManager();
126
127   // Remove the previously set text and style.
128   ResetText();
129
130   // Remove the style.
131   ClearStyleData();
132
133   CharacterIndex lastCursorIndex = 0u;
134
135   if( NULL != mImpl->mEventData )
136   {
137     // If popup shown then hide it by switching to Editing state
138     if( ( EventData::SELECTING == mImpl->mEventData->mState )          ||
139         ( EventData::EDITING_WITH_POPUP == mImpl->mEventData->mState ) ||
140         ( EventData::EDITING_WITH_GRAB_HANDLE == mImpl->mEventData->mState ) ||
141         ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
142     {
143       mImpl->ChangeState( EventData::EDITING );
144     }
145   }
146
147   if( !text.empty() )
148   {
149     MarkupProcessData markupProcessData( mImpl->mLogicalModel->mColorRuns,
150                                          mImpl->mLogicalModel->mFontDescriptionRuns );
151
152     Length textSize = 0u;
153     const uint8_t* utf8 = NULL;
154     if( mImpl->mMarkupProcessorEnabled )
155     {
156       ProcessMarkupString( text, markupProcessData );
157       textSize = markupProcessData.markupProcessedText.size();
158
159       // This is a bit horrible but std::string returns a (signed) char*
160       utf8 = reinterpret_cast<const uint8_t*>( markupProcessData.markupProcessedText.c_str() );
161     }
162     else
163     {
164       textSize = text.size();
165
166       // This is a bit horrible but std::string returns a (signed) char*
167       utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
168     }
169
170     //  Convert text into UTF-32
171     Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
172     utf32Characters.Resize( textSize );
173
174     // Transform a text array encoded in utf8 into an array encoded in utf32.
175     // It returns the actual number of characters.
176     Length characterCount = Utf8ToUtf32( utf8, textSize, utf32Characters.Begin() );
177     utf32Characters.Resize( characterCount );
178
179     DALI_ASSERT_DEBUG( textSize >= characterCount && "Invalid UTF32 conversion length" );
180     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", this, textSize, mImpl->mLogicalModel->mText.Count() );
181
182     // To reset the cursor position
183     lastCursorIndex = characterCount;
184
185     // Update the rest of the model during size negotiation
186     mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
187
188     // The natural size needs to be re-calculated.
189     mImpl->mRecalculateNaturalSize = true;
190
191     // Apply modifications to the model
192     mImpl->mOperationsPending = ALL_OPERATIONS;
193   }
194   else
195   {
196     ShowPlaceholderText();
197   }
198
199   // Resets the cursor position.
200   ResetCursorPosition( lastCursorIndex );
201
202   // Scrolls the text to make the cursor visible.
203   ResetScrollPosition();
204
205   mImpl->RequestRelayout();
206
207   if( NULL != mImpl->mEventData )
208   {
209     // Cancel previously queued events
210     mImpl->mEventData->mEventQueue.clear();
211   }
212
213   // Notify IMF as text changed
214   NotifyImfManager();
215
216   // Do this last since it provides callbacks into application code
217   mImpl->mControlInterface.TextChanged();
218 }
219
220 void Controller::GetText( std::string& text ) const
221 {
222   if( !mImpl->IsShowingPlaceholderText() )
223   {
224     Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
225
226     if( 0u != utf32Characters.Count() )
227     {
228       Utf32ToUtf8( &utf32Characters[0], utf32Characters.Count(), text );
229     }
230   }
231   else
232   {
233     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::GetText %p empty (but showing placeholder)\n", this );
234   }
235 }
236
237 unsigned int Controller::GetLogicalCursorPosition() const
238 {
239   if( NULL != mImpl->mEventData )
240   {
241     return mImpl->mEventData->mPrimaryCursorPosition;
242   }
243
244   return 0u;
245 }
246
247 void Controller::SetPlaceholderText( PlaceholderType type, const std::string& text )
248 {
249   if( NULL != mImpl->mEventData )
250   {
251     if( PLACEHOLDER_TYPE_INACTIVE == type )
252     {
253       mImpl->mEventData->mPlaceholderTextInactive = text;
254     }
255     else
256     {
257       mImpl->mEventData->mPlaceholderTextActive = text;
258     }
259
260     // Update placeholder if there is no text
261     if( mImpl->IsShowingPlaceholderText() ||
262         ( 0u == mImpl->mLogicalModel->mText.Count() ) )
263     {
264       ShowPlaceholderText();
265     }
266   }
267 }
268
269 void Controller::GetPlaceholderText( PlaceholderType type, std::string& text ) const
270 {
271   if( NULL != mImpl->mEventData )
272   {
273     if( PLACEHOLDER_TYPE_INACTIVE == type )
274     {
275       text = mImpl->mEventData->mPlaceholderTextInactive;
276     }
277     else
278     {
279       text = mImpl->mEventData->mPlaceholderTextActive;
280     }
281   }
282 }
283
284 void Controller::SetMaximumNumberOfCharacters( Length maxCharacters )
285 {
286   mImpl->mMaximumNumberOfCharacters = maxCharacters;
287 }
288
289 int Controller::GetMaximumNumberOfCharacters()
290 {
291   return mImpl->mMaximumNumberOfCharacters;
292 }
293
294 void Controller::SetDefaultFontFamily( const std::string& defaultFontFamily )
295 {
296   if( NULL == mImpl->mFontDefaults )
297   {
298     mImpl->mFontDefaults = new FontDefaults();
299   }
300
301   mImpl->mFontDefaults->mFontDescription.family = defaultFontFamily;
302   DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultFontFamily %s\n", defaultFontFamily.c_str());
303   mImpl->mFontDefaults->familyDefined = true;
304
305   // Clear the font-specific data
306   ClearFontData();
307
308   mImpl->mOperationsPending = ALL_OPERATIONS;
309   mImpl->mRecalculateNaturalSize = true;
310
311   mImpl->RequestRelayout();
312 }
313
314 const std::string& Controller::GetDefaultFontFamily() const
315 {
316   if( NULL != mImpl->mFontDefaults )
317   {
318     return mImpl->mFontDefaults->mFontDescription.family;
319   }
320
321   return EMPTY_STRING;
322 }
323
324 void Controller::SetDefaultFontStyle( const std::string& style )
325 {
326   if( NULL == mImpl->mFontDefaults )
327   {
328     mImpl->mFontDefaults = new FontDefaults();
329   }
330
331   mImpl->mFontDefaults->mFontStyle = style;
332 }
333
334 const std::string& Controller::GetDefaultFontStyle() const
335 {
336   if( NULL != mImpl->mFontDefaults )
337   {
338     return mImpl->mFontDefaults->mFontStyle;
339   }
340
341   return EMPTY_STRING;
342 }
343
344 void Controller::SetDefaultFontWeight( FontWeight weight )
345 {
346   if( NULL == mImpl->mFontDefaults )
347   {
348     mImpl->mFontDefaults = new FontDefaults();
349   }
350
351   mImpl->mFontDefaults->mFontDescription.weight = weight;
352   mImpl->mFontDefaults->weightDefined = true;
353
354   // Clear the font-specific data
355   ClearFontData();
356
357   mImpl->mOperationsPending = ALL_OPERATIONS;
358   mImpl->mRecalculateNaturalSize = true;
359
360   mImpl->RequestRelayout();
361 }
362
363 FontWeight Controller::GetDefaultFontWeight() const
364 {
365   if( NULL != mImpl->mFontDefaults )
366   {
367     return mImpl->mFontDefaults->mFontDescription.weight;
368   }
369
370   return TextAbstraction::FontWeight::NORMAL;
371 }
372
373 void Controller::SetDefaultFontWidth( FontWidth width )
374 {
375   if( NULL == mImpl->mFontDefaults )
376   {
377     mImpl->mFontDefaults = new FontDefaults();
378   }
379
380   mImpl->mFontDefaults->mFontDescription.width = width;
381   mImpl->mFontDefaults->widthDefined = true;
382
383   // Clear the font-specific data
384   ClearFontData();
385
386   mImpl->mOperationsPending = ALL_OPERATIONS;
387   mImpl->mRecalculateNaturalSize = true;
388
389   mImpl->RequestRelayout();
390 }
391
392 FontWidth Controller::GetDefaultFontWidth() const
393 {
394   if( NULL != mImpl->mFontDefaults )
395   {
396     return mImpl->mFontDefaults->mFontDescription.width;
397   }
398
399   return TextAbstraction::FontWidth::NORMAL;
400 }
401
402 void Controller::SetDefaultFontSlant( FontSlant slant )
403 {
404   if( NULL == mImpl->mFontDefaults )
405   {
406     mImpl->mFontDefaults = new FontDefaults();
407   }
408
409   mImpl->mFontDefaults->mFontDescription.slant = slant;
410   mImpl->mFontDefaults->slantDefined = true;
411
412   // Clear the font-specific data
413   ClearFontData();
414
415   mImpl->mOperationsPending = ALL_OPERATIONS;
416   mImpl->mRecalculateNaturalSize = true;
417
418   mImpl->RequestRelayout();
419 }
420
421 FontSlant Controller::GetDefaultFontSlant() const
422 {
423   if( NULL != mImpl->mFontDefaults )
424   {
425     return mImpl->mFontDefaults->mFontDescription.slant;
426   }
427
428   return TextAbstraction::FontSlant::NORMAL;
429 }
430
431 void Controller::SetDefaultPointSize( float pointSize )
432 {
433   if( NULL == mImpl->mFontDefaults )
434   {
435     mImpl->mFontDefaults = new FontDefaults();
436   }
437
438   mImpl->mFontDefaults->mDefaultPointSize = pointSize;
439   mImpl->mFontDefaults->sizeDefined = true;
440
441   unsigned int horizontalDpi( 0u );
442   unsigned int verticalDpi( 0u );
443   mImpl->mFontClient.GetDpi( horizontalDpi, verticalDpi );
444
445   // Adjust the metrics if the fixed-size font should be down-scaled
446   int maxEmojiSize( pointSize/POINTS_PER_INCH * verticalDpi );
447   DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetDefaultPointSize %p setting MaxEmojiSize %d\n", this, maxEmojiSize );
448   mImpl->mMetrics->SetMaxEmojiSize( maxEmojiSize );
449
450   // Clear the font-specific data
451   ClearFontData();
452
453   mImpl->mOperationsPending = ALL_OPERATIONS;
454   mImpl->mRecalculateNaturalSize = true;
455
456   mImpl->RequestRelayout();
457 }
458
459 float Controller::GetDefaultPointSize() const
460 {
461   if( NULL != mImpl->mFontDefaults )
462   {
463     return mImpl->mFontDefaults->mDefaultPointSize;
464   }
465
466   return 0.0f;
467 }
468
469 void Controller::UpdateAfterFontChange( std::string& newDefaultFont )
470 {
471   DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange");
472
473   if( !mImpl->mFontDefaults->familyDefined ) // If user defined font then should not update when system font changes
474   {
475     DALI_LOG_INFO( gLogFilter, Debug::Concise, "Controller::UpdateAfterFontChange newDefaultFont(%s)\n", newDefaultFont.c_str() );
476     ClearFontData();
477     mImpl->mFontDefaults->mFontDescription.family = newDefaultFont;
478     mImpl->UpdateModel( ALL_OPERATIONS );
479     mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
480     mImpl->mRecalculateNaturalSize = true;
481     mImpl->RequestRelayout();
482   }
483 }
484
485 void Controller::SetTextColor( const Vector4& textColor )
486 {
487   mImpl->mTextColor = textColor;
488
489   if( !mImpl->IsShowingPlaceholderText() )
490   {
491     mImpl->mVisualModel->SetTextColor( textColor );
492
493     mImpl->RequestRelayout();
494   }
495 }
496
497 const Vector4& Controller::GetTextColor() const
498 {
499   return mImpl->mTextColor;
500 }
501
502 bool Controller::RemoveText( int cursorOffset, int numberOfChars )
503 {
504   bool removed = false;
505
506   if( NULL == mImpl->mEventData )
507   {
508     return removed;
509   }
510
511   DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfChars %d\n",
512                  this, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition, cursorOffset, numberOfChars );
513
514   if( !mImpl->IsShowingPlaceholderText() )
515   {
516     // Delete at current cursor position
517     Vector<Character>& currentText = mImpl->mLogicalModel->mText;
518     CharacterIndex& oldCursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
519
520     CharacterIndex cursorIndex = oldCursorIndex;
521
522     // Validate the cursor position & number of characters
523     if( static_cast< CharacterIndex >( std::abs( cursorOffset ) ) <= cursorIndex )
524     {
525       cursorIndex = oldCursorIndex + cursorOffset;
526     }
527
528     if( ( cursorIndex + numberOfChars ) > currentText.Count() )
529     {
530       numberOfChars = currentText.Count() - cursorIndex;
531     }
532
533     if( ( cursorIndex + numberOfChars ) <= currentText.Count() )
534     {
535       // Update the input style and remove the text's style before removing the text.
536
537       // Set first the default input style.
538       mImpl->RetrieveDefaultInputStyle( mImpl->mEventData->mInputStyle );
539
540       // Update the input style.
541       mImpl->mLogicalModel->RetrieveStyle( cursorIndex, mImpl->mEventData->mInputStyle );
542
543       // Remove the text's style before removing the text.
544       mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, -numberOfChars );
545
546       // Remove the characters.
547       Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
548       Vector<Character>::Iterator last  = first + numberOfChars;
549
550       currentText.Erase( first, last );
551
552       // Cursor position retreat
553       oldCursorIndex = cursorIndex;
554
555       DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", this, numberOfChars );
556       removed = true;
557     }
558   }
559
560   return removed;
561 }
562
563 void Controller::SetPlaceholderTextColor( const Vector4& textColor )
564 {
565   if( NULL != mImpl->mEventData )
566   {
567     mImpl->mEventData->mPlaceholderTextColor = textColor;
568   }
569
570   if( mImpl->IsShowingPlaceholderText() )
571   {
572     mImpl->mVisualModel->SetTextColor( textColor );
573     mImpl->RequestRelayout();
574   }
575 }
576
577 const Vector4& Controller::GetPlaceholderTextColor() const
578 {
579   if( NULL != mImpl->mEventData )
580   {
581     return mImpl->mEventData->mPlaceholderTextColor;
582   }
583
584   return Color::BLACK;
585 }
586
587 void Controller::SetShadowOffset( const Vector2& shadowOffset )
588 {
589   mImpl->mVisualModel->SetShadowOffset( shadowOffset );
590
591   mImpl->RequestRelayout();
592 }
593
594 const Vector2& Controller::GetShadowOffset() const
595 {
596   return mImpl->mVisualModel->GetShadowOffset();
597 }
598
599 void Controller::SetShadowColor( const Vector4& shadowColor )
600 {
601   mImpl->mVisualModel->SetShadowColor( shadowColor );
602
603   mImpl->RequestRelayout();
604 }
605
606 const Vector4& Controller::GetShadowColor() const
607 {
608   return mImpl->mVisualModel->GetShadowColor();
609 }
610
611 void Controller::SetUnderlineColor( const Vector4& color )
612 {
613   mImpl->mVisualModel->SetUnderlineColor( color );
614
615   mImpl->RequestRelayout();
616 }
617
618 const Vector4& Controller::GetUnderlineColor() const
619 {
620   return mImpl->mVisualModel->GetUnderlineColor();
621 }
622
623 void Controller::SetUnderlineEnabled( bool enabled )
624 {
625   mImpl->mVisualModel->SetUnderlineEnabled( enabled );
626
627   mImpl->RequestRelayout();
628 }
629
630 bool Controller::IsUnderlineEnabled() const
631 {
632   return mImpl->mVisualModel->IsUnderlineEnabled();
633 }
634
635 void Controller::SetUnderlineHeight( float height )
636 {
637   mImpl->mVisualModel->SetUnderlineHeight( height );
638
639   mImpl->RequestRelayout();
640 }
641
642 float Controller::GetUnderlineHeight() const
643 {
644   return mImpl->mVisualModel->GetUnderlineHeight();
645 }
646
647 void Controller::SetInputColor( const Vector4& color )
648 {
649   if( NULL != mImpl->mEventData )
650   {
651     mImpl->mEventData->mInputStyle.textColor = color;
652
653     if( EventData::SELECTING == mImpl->mEventData->mState )
654     {
655       const bool handlesCrossed = mImpl->mEventData->mLeftSelectionPosition > mImpl->mEventData->mRightSelectionPosition;
656
657       // Get start and end position of selection
658       const CharacterIndex startOfSelectedText = handlesCrossed ? mImpl->mEventData->mRightSelectionPosition : mImpl->mEventData->mLeftSelectionPosition;
659       const Length lengthOfSelectedText = ( handlesCrossed ? mImpl->mEventData->mLeftSelectionPosition : mImpl->mEventData->mRightSelectionPosition ) - startOfSelectedText;
660
661       // Add the color run.
662       const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
663       mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
664
665       ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
666       colorRun.color = color;
667       colorRun.characterRun.characterIndex = startOfSelectedText;
668       colorRun.characterRun.numberOfCharacters = lengthOfSelectedText;
669
670       // Request to relayout.
671       mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | COLOR );
672       mImpl->RequestRelayout();
673     }
674   }
675 }
676
677 const Vector4& Controller::GetInputColor() const
678 {
679   if( NULL != mImpl->mEventData )
680   {
681     return mImpl->mEventData->mInputStyle.textColor;
682   }
683
684   // Return the default text's color if there is no EventData.
685   return mImpl->mTextColor;
686
687 }
688
689 void Controller::SetInputFontFamily( const std::string& fontFamily )
690 {
691   if( NULL != mImpl->mEventData )
692   {
693     mImpl->mEventData->mInputStyle.familyName = fontFamily;
694     mImpl->mEventData->mInputStyle.familyDefined = true;
695
696     if( EventData::SELECTING == mImpl->mEventData->mState )
697     {
698       FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
699                                                                             mImpl->mLogicalModel );
700
701       fontDescriptionRun.familyLength = fontFamily.size();
702       fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
703       memcpy( fontDescriptionRun.familyName, fontFamily.c_str(), fontDescriptionRun.familyLength );
704       fontDescriptionRun.familyDefined = true;
705
706       // The memory allocated for the font family name is freed when the font description is removed from the logical model.
707
708       // Request to relayout.
709       mImpl->mOperationsPending = ALL_OPERATIONS;
710       mImpl->mRecalculateNaturalSize = true;
711       mImpl->RequestRelayout();
712
713       // As the font changes, recalculate the handle positions is needed.
714       mImpl->mEventData->mUpdateLeftSelectionPosition = true;
715       mImpl->mEventData->mUpdateRightSelectionPosition = true;
716       mImpl->mEventData->mScrollAfterUpdatePosition = true;
717     }
718   }
719 }
720
721 const std::string& Controller::GetInputFontFamily() const
722 {
723   if( NULL != mImpl->mEventData )
724   {
725     return mImpl->mEventData->mInputStyle.familyName;
726   }
727
728   // Return the default font's family if there is no EventData.
729   return GetDefaultFontFamily();
730 }
731
732 void Controller::SetInputFontStyle( const std::string& fontStyle )
733 {
734   if( NULL != mImpl->mEventData )
735   {
736     mImpl->mEventData->mInputStyle.fontStyle = fontStyle;
737   }
738 }
739
740 const std::string& Controller::GetInputFontStyle() const
741 {
742   if( NULL != mImpl->mEventData )
743   {
744     return mImpl->mEventData->mInputStyle.fontStyle;
745   }
746
747   // Return the default font's style if there is no EventData.
748   return GetDefaultFontStyle();
749 }
750
751 void Controller::SetInputFontWeight( FontWeight weight )
752 {
753   if( NULL != mImpl->mEventData )
754   {
755     mImpl->mEventData->mInputStyle.weight = weight;
756     mImpl->mEventData->mInputStyle.weightDefined = true;
757
758     if( EventData::SELECTING == mImpl->mEventData->mState )
759     {
760       FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
761                                                                             mImpl->mLogicalModel );
762
763       fontDescriptionRun.weight = weight;
764       fontDescriptionRun.weightDefined = true;
765
766       // Request to relayout.
767       mImpl->mOperationsPending = ALL_OPERATIONS;
768       mImpl->mRecalculateNaturalSize = true;
769       mImpl->RequestRelayout();
770
771       // As the font might change, recalculate the handle positions is needed.
772       mImpl->mEventData->mUpdateLeftSelectionPosition = true;
773       mImpl->mEventData->mUpdateRightSelectionPosition = true;
774       mImpl->mEventData->mScrollAfterUpdatePosition = true;
775     }
776   }
777 }
778
779 FontWeight Controller::GetInputFontWeight() const
780 {
781   if( NULL != mImpl->mEventData )
782   {
783     return mImpl->mEventData->mInputStyle.weight;
784   }
785
786   return GetDefaultFontWeight();
787 }
788
789 void Controller::SetInputFontWidth( FontWidth width )
790 {
791   if( NULL != mImpl->mEventData )
792   {
793     mImpl->mEventData->mInputStyle.width = width;
794     mImpl->mEventData->mInputStyle.widthDefined = true;
795
796     if( EventData::SELECTING == mImpl->mEventData->mState )
797     {
798       FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
799                                                                             mImpl->mLogicalModel );
800
801       fontDescriptionRun.width = width;
802       fontDescriptionRun.widthDefined = true;
803
804       // Request to relayout.
805       mImpl->mOperationsPending = ALL_OPERATIONS;
806       mImpl->mRecalculateNaturalSize = true;
807       mImpl->RequestRelayout();
808
809       // As the font might change, recalculate the handle positions is needed.
810       mImpl->mEventData->mUpdateLeftSelectionPosition = true;
811       mImpl->mEventData->mUpdateRightSelectionPosition = true;
812       mImpl->mEventData->mScrollAfterUpdatePosition = true;
813     }
814   }
815 }
816
817 FontWidth Controller::GetInputFontWidth() const
818 {
819   if( NULL != mImpl->mEventData )
820   {
821     return mImpl->mEventData->mInputStyle.width;
822   }
823
824   return GetDefaultFontWidth();
825 }
826
827 void Controller::SetInputFontSlant( FontSlant slant )
828 {
829   if( NULL != mImpl->mEventData )
830   {
831     mImpl->mEventData->mInputStyle.slant = slant;
832     mImpl->mEventData->mInputStyle.slantDefined = true;
833
834     if( EventData::SELECTING == mImpl->mEventData->mState )
835     {
836       FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
837                                                                             mImpl->mLogicalModel );
838
839       fontDescriptionRun.slant = slant;
840       fontDescriptionRun.slantDefined = true;
841
842       // Request to relayout.
843       mImpl->mOperationsPending = ALL_OPERATIONS;
844       mImpl->mRecalculateNaturalSize = true;
845       mImpl->RequestRelayout();
846
847       // As the font might change, recalculate the handle positions is needed.
848       mImpl->mEventData->mUpdateLeftSelectionPosition = true;
849       mImpl->mEventData->mUpdateRightSelectionPosition = true;
850       mImpl->mEventData->mScrollAfterUpdatePosition = true;
851     }
852   }
853 }
854
855 FontSlant Controller::GetInputFontSlant() const
856 {
857   if( NULL != mImpl->mEventData )
858   {
859     return mImpl->mEventData->mInputStyle.slant;
860   }
861
862   return GetDefaultFontSlant();
863 }
864
865 void Controller::SetInputFontPointSize( float size )
866 {
867   if( NULL != mImpl->mEventData )
868   {
869     mImpl->mEventData->mInputStyle.size = size;
870
871     if( EventData::SELECTING == mImpl->mEventData->mState )
872     {
873       FontDescriptionRun& fontDescriptionRun = UpdateSelectionFontStyleRun( mImpl->mEventData,
874                                                                             mImpl->mLogicalModel );
875
876       fontDescriptionRun.size = static_cast<PointSize26Dot6>( size * 64.f );
877       fontDescriptionRun.sizeDefined = true;
878
879       // Request to relayout.
880       mImpl->mOperationsPending = ALL_OPERATIONS;
881       mImpl->mRecalculateNaturalSize = true;
882       mImpl->RequestRelayout();
883
884       // As the font might change, recalculate the handle positions is needed.
885       mImpl->mEventData->mUpdateLeftSelectionPosition = true;
886       mImpl->mEventData->mUpdateRightSelectionPosition = true;
887       mImpl->mEventData->mScrollAfterUpdatePosition = true;
888     }
889   }
890 }
891
892 float Controller::GetInputFontPointSize() const
893 {
894   if( NULL != mImpl->mEventData )
895   {
896     return mImpl->mEventData->mInputStyle.size;
897   }
898
899   // Return the default font's point size if there is no EventData.
900   return GetDefaultPointSize();
901 }
902
903 void Controller::SetEnableCursorBlink( bool enable )
904 {
905   DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "TextInput disabled" );
906
907   if( NULL != mImpl->mEventData )
908   {
909     mImpl->mEventData->mCursorBlinkEnabled = enable;
910
911     if( !enable &&
912         mImpl->mEventData->mDecorator )
913     {
914       mImpl->mEventData->mDecorator->StopCursorBlink();
915     }
916   }
917 }
918
919 bool Controller::GetEnableCursorBlink() const
920 {
921   if( NULL != mImpl->mEventData )
922   {
923     return mImpl->mEventData->mCursorBlinkEnabled;
924   }
925
926   return false;
927 }
928
929 const Vector2& Controller::GetScrollPosition() const
930 {
931   if( NULL != mImpl->mEventData )
932   {
933     return mImpl->mEventData->mScrollPosition;
934   }
935
936   return Vector2::ZERO;
937 }
938
939 const Vector2& Controller::GetAlignmentOffset() const
940 {
941   return mImpl->mAlignmentOffset;
942 }
943
944 Vector3 Controller::GetNaturalSize()
945 {
946   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetNaturalSize\n" );
947   Vector3 naturalSize;
948
949   // Make sure the model is up-to-date before layouting
950   ProcessModifyEvents();
951
952   if( mImpl->mRecalculateNaturalSize )
953   {
954     // Operations that can be done only once until the text changes.
955     const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32  |
956                                                                            GET_SCRIPTS       |
957                                                                            VALIDATE_FONTS    |
958                                                                            GET_LINE_BREAKS   |
959                                                                            GET_WORD_BREAKS   |
960                                                                            BIDI_INFO         |
961                                                                            SHAPE_TEXT        |
962                                                                            GET_GLYPH_METRICS );
963     // Make sure the model is up-to-date before layouting
964     mImpl->UpdateModel( onlyOnceOperations );
965
966     // Operations that need to be done if the size changes.
967     const OperationsMask sizeOperations =  static_cast<OperationsMask>( LAYOUT |
968                                                                         ALIGN  |
969                                                                         REORDER );
970
971     DoRelayout( Size( MAX_FLOAT, MAX_FLOAT ),
972                 static_cast<OperationsMask>( onlyOnceOperations |
973                                              sizeOperations ),
974                 naturalSize.GetVectorXY() );
975
976     // Do not do again the only once operations.
977     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
978
979     // Do the size related operations again.
980     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
981
982     // Stores the natural size to avoid recalculate it again
983     // unless the text/style changes.
984     mImpl->mVisualModel->SetNaturalSize( naturalSize.GetVectorXY() );
985
986     mImpl->mRecalculateNaturalSize = false;
987
988     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize calculated %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
989   }
990   else
991   {
992     naturalSize = mImpl->mVisualModel->GetNaturalSize();
993
994     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetNaturalSize cached %f,%f,%f\n", naturalSize.x, naturalSize.y, naturalSize.z );
995   }
996
997   naturalSize.x = ConvertToEven( naturalSize.x );
998   naturalSize.y = ConvertToEven( naturalSize.y );
999
1000   return naturalSize;
1001 }
1002
1003 float Controller::GetHeightForWidth( float width )
1004 {
1005   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::GetHeightForWidth %p width %f\n", this, width );
1006   // Make sure the model is up-to-date before layouting
1007   ProcessModifyEvents();
1008
1009   Size layoutSize;
1010   if( fabsf( width - mImpl->mVisualModel->mControlSize.width ) > Math::MACHINE_EPSILON_1000 )
1011   {
1012     // Operations that can be done only once until the text changes.
1013     const OperationsMask onlyOnceOperations = static_cast<OperationsMask>( CONVERT_TO_UTF32  |
1014                                                                            GET_SCRIPTS       |
1015                                                                            VALIDATE_FONTS    |
1016                                                                            GET_LINE_BREAKS   |
1017                                                                            GET_WORD_BREAKS   |
1018                                                                            BIDI_INFO         |
1019                                                                            SHAPE_TEXT        |
1020                                                                            GET_GLYPH_METRICS );
1021     // Make sure the model is up-to-date before layouting
1022     mImpl->UpdateModel( onlyOnceOperations );
1023
1024     // Operations that need to be done if the size changes.
1025     const OperationsMask sizeOperations =  static_cast<OperationsMask>( LAYOUT |
1026                                                                         ALIGN  |
1027                                                                         REORDER );
1028
1029     DoRelayout( Size( width, MAX_FLOAT ),
1030                 static_cast<OperationsMask>( onlyOnceOperations |
1031                                              sizeOperations ),
1032                 layoutSize );
1033
1034     // Do not do again the only once operations.
1035     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending & ~onlyOnceOperations );
1036
1037     // Do the size related operations again.
1038     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | sizeOperations );
1039     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth calculated %f\n", layoutSize.height );
1040   }
1041   else
1042   {
1043     layoutSize = mImpl->mVisualModel->GetLayoutSize();
1044     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::GetHeightForWidth cached %f\n", layoutSize.height );
1045   }
1046
1047   return layoutSize.height;
1048 }
1049
1050 bool Controller::Relayout( const Size& size )
1051 {
1052   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::Relayout %p size %f,%f\n", this, size.width, size.height );
1053
1054   if( ( size.width < Math::MACHINE_EPSILON_1000 ) || ( size.height < Math::MACHINE_EPSILON_1000 ) )
1055   {
1056     bool glyphsRemoved( false );
1057     if( 0u != mImpl->mVisualModel->mGlyphPositions.Count() )
1058     {
1059       mImpl->mVisualModel->mGlyphPositions.Clear();
1060       glyphsRemoved = true;
1061     }
1062     // Not worth to relayout if width or height is equal to zero.
1063     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout (skipped)\n" );
1064     return glyphsRemoved;
1065   }
1066
1067   // Whether a new size has been set.
1068   const bool newSize = ( size != mImpl->mVisualModel->mControlSize );
1069
1070   if( newSize )
1071   {
1072     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "new size (previous size %f,%f)\n", mImpl->mVisualModel->mControlSize.width, mImpl->mVisualModel->mControlSize.height );
1073
1074     // Layout operations that need to be done if the size changes.
1075     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1076                                                              LAYOUT                    |
1077                                                              ALIGN                     |
1078                                                              UPDATE_ACTUAL_SIZE        |
1079                                                              REORDER );
1080
1081     mImpl->mVisualModel->mControlSize = size;
1082   }
1083
1084   // Whether there are modify events.
1085   const bool isModifyEventsEmpty = 0u == mImpl->mModifyEvents.Count();
1086
1087   // Make sure the model is up-to-date before layouting.
1088   ProcessModifyEvents();
1089   mImpl->UpdateModel( mImpl->mOperationsPending );
1090
1091   // Style operations that need to be done if the text is modified.
1092   if( !isModifyEventsEmpty )
1093   {
1094     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending |
1095                                                              COLOR );
1096   }
1097
1098   // Apply the style runs if text is modified.
1099   bool updated = mImpl->UpdateModelStyle( mImpl->mOperationsPending );
1100
1101   // Layout the text.
1102   Size layoutSize;
1103   updated = DoRelayout( mImpl->mVisualModel->mControlSize,
1104                         mImpl->mOperationsPending,
1105                         layoutSize ) || updated;
1106
1107   // Do not re-do any operation until something changes.
1108   mImpl->mOperationsPending = NO_OPERATION;
1109
1110   // Whether the text control is editable
1111   const bool isEditable = NULL != mImpl->mEventData;
1112
1113   // Keep the current offset and alignment as it will be used to update the decorator's positions (if the size changes).
1114   Vector2 offset;
1115   if( newSize && isEditable )
1116   {
1117     offset = mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition;
1118   }
1119
1120   // After doing the text layout, the alignment offset to place the actor in the desired position can be calculated.
1121   CalculateTextAlignment( size );
1122
1123   if( isEditable )
1124   {
1125     if( newSize )
1126     {
1127       // If there is a new size, the scroll position needs to be clamped.
1128       mImpl->ClampHorizontalScroll( layoutSize );
1129
1130       // Update the decorator's positions is needed if there is a new size.
1131       mImpl->mEventData->mDecorator->UpdatePositions( mImpl->mAlignmentOffset + mImpl->mEventData->mScrollPosition - offset );
1132     }
1133
1134     // Move the cursor, grab handle etc.
1135     updated = mImpl->ProcessInputEvents() || updated;
1136   }
1137
1138   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::Relayout\n" );
1139   return updated;
1140 }
1141
1142 void Controller::ProcessModifyEvents()
1143 {
1144   Vector<ModifyEvent>& events = mImpl->mModifyEvents;
1145
1146   if( 0u == events.Count() )
1147   {
1148     // Nothing to do.
1149     return;
1150   }
1151
1152   for( Vector<ModifyEvent>::ConstIterator it = events.Begin(),
1153          endIt = events.End();
1154        it != endIt;
1155        ++it )
1156   {
1157     const ModifyEvent& event = *it;
1158
1159     if( ModifyEvent::TEXT_REPLACED == event.type )
1160     {
1161       // A (single) replace event should come first, otherwise we wasted time processing NOOP events
1162       DALI_ASSERT_DEBUG( it == events.Begin() && "Unexpected TEXT_REPLACED event" );
1163
1164       TextReplacedEvent();
1165     }
1166     else if( ModifyEvent::TEXT_INSERTED == event.type )
1167     {
1168       TextInsertedEvent();
1169     }
1170     else if( ModifyEvent::TEXT_DELETED == event.type )
1171     {
1172       // Placeholder-text cannot be deleted
1173       if( !mImpl->IsShowingPlaceholderText() )
1174       {
1175         TextDeletedEvent();
1176       }
1177     }
1178   }
1179
1180   if( NULL != mImpl->mEventData )
1181   {
1182     // When the text is being modified, delay cursor blinking
1183     mImpl->mEventData->mDecorator->DelayCursorBlink();
1184   }
1185
1186   // Discard temporary text
1187   events.Clear();
1188 }
1189
1190 void Controller::ResetText()
1191 {
1192   // Reset buffers.
1193   mImpl->mLogicalModel->mText.Clear();
1194   ClearModelData();
1195
1196   // We have cleared everything including the placeholder-text
1197   mImpl->PlaceholderCleared();
1198
1199   // The natural size needs to be re-calculated.
1200   mImpl->mRecalculateNaturalSize = true;
1201
1202   // Apply modifications to the model
1203   mImpl->mOperationsPending = ALL_OPERATIONS;
1204 }
1205
1206 void Controller::ResetCursorPosition( CharacterIndex cursorIndex )
1207 {
1208   // Reset the cursor position
1209   if( NULL != mImpl->mEventData )
1210   {
1211     mImpl->mEventData->mPrimaryCursorPosition = cursorIndex;
1212
1213     // Update the cursor if it's in editing mode.
1214     if( EventData::IsEditingState( mImpl->mEventData->mState )  )
1215     {
1216       mImpl->mEventData->mUpdateCursorPosition = true;
1217     }
1218   }
1219 }
1220
1221 void Controller::ResetScrollPosition()
1222 {
1223   if( NULL != mImpl->mEventData )
1224   {
1225     // Reset the scroll position.
1226     mImpl->mEventData->mScrollPosition = Vector2::ZERO;
1227     mImpl->mEventData->mScrollAfterUpdatePosition = true;
1228   }
1229 }
1230
1231 void Controller::TextReplacedEvent()
1232 {
1233   // Reset buffers.
1234   ClearModelData();
1235
1236   // The natural size needs to be re-calculated.
1237   mImpl->mRecalculateNaturalSize = true;
1238
1239   // Apply modifications to the model
1240   mImpl->mOperationsPending = ALL_OPERATIONS;
1241   mImpl->UpdateModel( ALL_OPERATIONS );
1242   mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT             |
1243                                                            ALIGN              |
1244                                                            UPDATE_ACTUAL_SIZE |
1245                                                            REORDER );
1246 }
1247
1248 void Controller::TextInsertedEvent()
1249 {
1250   DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextInsertedEvent" );
1251
1252   if( NULL == mImpl->mEventData )
1253   {
1254     return;
1255   }
1256
1257   // TODO - Optimize this
1258   ClearModelData();
1259
1260   // The natural size needs to be re-calculated.
1261   mImpl->mRecalculateNaturalSize = true;
1262
1263   // Apply modifications to the model; TODO - Optimize this
1264   mImpl->mOperationsPending = ALL_OPERATIONS;
1265   mImpl->UpdateModel( ALL_OPERATIONS );
1266   mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT             |
1267                                                            ALIGN              |
1268                                                            UPDATE_ACTUAL_SIZE |
1269                                                            REORDER );
1270
1271   // Queue a cursor reposition event; this must wait until after DoRelayout()
1272   if( EventData::IsEditingState( mImpl->mEventData->mState ) )
1273   {
1274     mImpl->mEventData->mUpdateCursorPosition = true;
1275     mImpl->mEventData->mScrollAfterUpdatePosition = true;
1276   }
1277 }
1278
1279 void Controller::TextDeletedEvent()
1280 {
1281   DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected TextDeletedEvent" );
1282
1283   if( NULL == mImpl->mEventData )
1284   {
1285     return;
1286   }
1287
1288   // TODO - Optimize this
1289   ClearModelData();
1290
1291   // The natural size needs to be re-calculated.
1292   mImpl->mRecalculateNaturalSize = true;
1293
1294   // Apply modifications to the model; TODO - Optimize this
1295   mImpl->mOperationsPending = ALL_OPERATIONS;
1296   mImpl->UpdateModel( ALL_OPERATIONS );
1297   mImpl->mOperationsPending = static_cast<OperationsMask>( LAYOUT             |
1298                                                            ALIGN              |
1299                                                            UPDATE_ACTUAL_SIZE |
1300                                                            REORDER );
1301
1302   // Queue a cursor reposition event; this must wait until after DoRelayout()
1303   mImpl->mEventData->mUpdateCursorPosition = true;
1304   if( 0u != mImpl->mLogicalModel->mText.Count() )
1305   {
1306     mImpl->mEventData->mScrollAfterDelete = true;
1307   }
1308 }
1309
1310 bool Controller::DoRelayout( const Size& size,
1311                              OperationsMask operationsRequired,
1312                              Size& layoutSize )
1313 {
1314   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "-->Controller::DoRelayout %p size %f,%f\n", this, size.width, size.height );
1315   bool viewUpdated( false );
1316
1317   // Calculate the operations to be done.
1318   const OperationsMask operations = static_cast<OperationsMask>( mImpl->mOperationsPending & operationsRequired );
1319
1320   if( LAYOUT & operations )
1321   {
1322     // Some vectors with data needed to layout and reorder may be void
1323     // after the first time the text has been laid out.
1324     // Fill the vectors again.
1325
1326     const Length numberOfGlyphs = mImpl->mVisualModel->mGlyphs.Count();
1327
1328     if( 0u == numberOfGlyphs )
1329     {
1330       // Nothing else to do if there is no glyphs.
1331       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
1332       return true;
1333     }
1334
1335     const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
1336     const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
1337     const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
1338     const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
1339     const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
1340     const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
1341     const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
1342     const Vector<GlyphIndex>& charactersToGlyph = mImpl->mVisualModel->mCharactersToGlyph;
1343     const Vector<Length>& glyphsPerCharacter = mImpl->mVisualModel->mGlyphsPerCharacter;
1344
1345     // Set the layout parameters.
1346     LayoutParameters layoutParameters( size,
1347                                        textBuffer,
1348                                        lineBreakInfo.Begin(),
1349                                        wordBreakInfo.Begin(),
1350                                        ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
1351                                        glyphs.Begin(),
1352                                        glyphsToCharactersMap.Begin(),
1353                                        charactersPerGlyph.Begin(),
1354                                        charactersToGlyph.Begin(),
1355                                        glyphsPerCharacter.Begin(),
1356                                        numberOfGlyphs );
1357
1358     // The laid-out lines.
1359     // It's not possible to know in how many lines the text is going to be laid-out,
1360     // but it can be resized at least with the number of 'paragraphs' to avoid
1361     // some re-allocations.
1362     Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1363
1364     // Delete any previous laid out lines before setting the new ones.
1365     lines.Clear();
1366
1367     // The capacity of the bidirectional paragraph info is the number of paragraphs.
1368     lines.Reserve( mImpl->mLogicalModel->mBidirectionalParagraphInfo.Capacity() );
1369
1370     // Resize the vector of positions to have the same size than the vector of glyphs.
1371     Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
1372     glyphPositions.Resize( numberOfGlyphs );
1373
1374     // Whether the last character is a new paragraph character.
1375     layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
1376
1377     // The initial glyph and the number of glyphs to layout.
1378     layoutParameters.startGlyphIndex = 0u;
1379     layoutParameters.numberOfGlyphs = numberOfGlyphs;
1380
1381     // Update the visual model.
1382     viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1383                                                    glyphPositions,
1384                                                    lines,
1385                                                    layoutSize );
1386
1387     if( viewUpdated )
1388     {
1389       // Reorder the lines
1390       if( REORDER & operations )
1391       {
1392         Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1393         Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = mImpl->mLogicalModel->mBidirectionalLineInfo;
1394
1395         // Check first if there are paragraphs with bidirectional info.
1396         if( 0u != bidirectionalInfo.Count() )
1397         {
1398           // Get the lines
1399           const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1400           const CharacterIndex startIndex = 0u;
1401           const Length requestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1402
1403           // Reorder the lines.
1404           bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1405           ReorderLines( bidirectionalInfo,
1406                         startIndex,
1407                         requestedNumberOfCharacters,
1408                         lines,
1409                         bidirectionalLineInfo );
1410
1411           // Set the bidirectional info per line into the layout parameters.
1412           layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
1413           layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
1414
1415           // Set the bidirectional info into the model.
1416           mImpl->mLogicalModel->SetVisualToLogicalMap( layoutParameters.lineBidirectionalInfoRunsBuffer,
1417                                                        layoutParameters.numberOfBidirectionalInfoRuns,
1418                                                        startIndex,
1419                                                        requestedNumberOfCharacters );
1420
1421           // Re-layout the text. Reorder those lines with right to left characters.
1422           mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1423                                                          startIndex,
1424                                                          requestedNumberOfCharacters,
1425                                                          glyphPositions );
1426
1427           // Free the allocated memory used to store the conversion table in the bidirectional line info run.
1428           for( Vector<BidirectionalLineInfoRun>::Iterator it = bidirectionalLineInfo.Begin(),
1429                  endIt = bidirectionalLineInfo.End();
1430                it != endIt;
1431                ++it )
1432           {
1433             BidirectionalLineInfoRun& bidiLineInfo = *it;
1434
1435             free( bidiLineInfo.visualToLogicalMap );
1436             bidiLineInfo.visualToLogicalMap = NULL;
1437           }
1438
1439           bidirectionalLineInfo.Clear();
1440         }
1441       } // REORDER
1442
1443       // Sets the actual size.
1444       if( UPDATE_ACTUAL_SIZE & operations )
1445       {
1446         mImpl->mVisualModel->SetLayoutSize( layoutSize );
1447       }
1448     } // view updated
1449   }
1450   else
1451   {
1452     layoutSize = mImpl->mVisualModel->GetLayoutSize();
1453   }
1454
1455   if( ALIGN & operations )
1456   {
1457     // The laid-out lines.
1458     Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1459
1460     const CharacterIndex startIndex = 0u;
1461     const Length requestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1462
1463     mImpl->mLayoutEngine.Align( size,
1464                                 startIndex,
1465                                 requestedNumberOfCharacters,
1466                                 lines );
1467
1468     viewUpdated = true;
1469   }
1470
1471   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1472   return viewUpdated;
1473 }
1474
1475 void Controller::SetMultiLineEnabled( bool enable )
1476 {
1477   const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1478
1479   if( layout != mImpl->mLayoutEngine.GetLayout() )
1480   {
1481     // Set the layout type.
1482     mImpl->mLayoutEngine.SetLayout( layout );
1483
1484     // Set the flags to redo the layout operations
1485     const OperationsMask layoutOperations =  static_cast<OperationsMask>( LAYOUT             |
1486                                                                           UPDATE_ACTUAL_SIZE |
1487                                                                           ALIGN              |
1488                                                                           REORDER );
1489
1490     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1491
1492     mImpl->RequestRelayout();
1493   }
1494 }
1495
1496 bool Controller::IsMultiLineEnabled() const
1497 {
1498   return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1499 }
1500
1501 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1502 {
1503   if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1504   {
1505     // Set the alignment.
1506     mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1507
1508     // Set the flag to redo the alignment operation.
1509     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1510
1511     mImpl->RequestRelayout();
1512   }
1513 }
1514
1515 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1516 {
1517   return mImpl->mLayoutEngine.GetHorizontalAlignment();
1518 }
1519
1520 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1521 {
1522   if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1523   {
1524     // Set the alignment.
1525     mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1526
1527     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1528
1529     mImpl->RequestRelayout();
1530   }
1531 }
1532
1533 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1534 {
1535   return mImpl->mLayoutEngine.GetVerticalAlignment();
1536 }
1537
1538 void Controller::CalculateTextAlignment( const Size& controlSize )
1539 {
1540   Size layoutSize = mImpl->mVisualModel->GetLayoutSize();
1541
1542   if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
1543   {
1544     // Get the line height of the default font.
1545     layoutSize.height = mImpl->GetDefaultFontLineHeight();
1546   }
1547
1548   if( LayoutEngine::SINGLE_LINE_BOX == mImpl->mLayoutEngine.GetLayout() )
1549   {
1550     // Get the direction of the first character.
1551     const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
1552
1553     // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
1554     LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
1555     if( firstParagraphDirection )
1556     {
1557       switch( horizontalAlignment )
1558       {
1559         case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1560         {
1561           horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
1562           break;
1563         }
1564         case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1565         {
1566           // Nothing to do.
1567           break;
1568         }
1569         case LayoutEngine::HORIZONTAL_ALIGN_END:
1570         {
1571           horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
1572           break;
1573         }
1574       }
1575     }
1576
1577     switch( horizontalAlignment )
1578     {
1579       case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1580       {
1581         mImpl->mAlignmentOffset.x = 0.f;
1582         break;
1583       }
1584       case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1585       {
1586         mImpl->mAlignmentOffset.x = floorf( 0.5f * ( controlSize.width - layoutSize.width ) ); // try to avoid pixel alignment.
1587         break;
1588       }
1589       case LayoutEngine::HORIZONTAL_ALIGN_END:
1590       {
1591         mImpl->mAlignmentOffset.x = controlSize.width - layoutSize.width;
1592         break;
1593       }
1594     }
1595   }
1596
1597   const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1598   switch( verticalAlignment )
1599   {
1600     case LayoutEngine::VERTICAL_ALIGN_TOP:
1601     {
1602       mImpl->mAlignmentOffset.y = 0.f;
1603       break;
1604     }
1605     case LayoutEngine::VERTICAL_ALIGN_CENTER:
1606     {
1607       mImpl->mAlignmentOffset.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
1608       break;
1609     }
1610     case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1611     {
1612       mImpl->mAlignmentOffset.y = controlSize.height - layoutSize.height;
1613       break;
1614     }
1615   }
1616 }
1617
1618 LayoutEngine& Controller::GetLayoutEngine()
1619 {
1620   return mImpl->mLayoutEngine;
1621 }
1622
1623 View& Controller::GetView()
1624 {
1625   return mImpl->mView;
1626 }
1627
1628 void Controller::KeyboardFocusGainEvent()
1629 {
1630   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1631
1632   if( NULL != mImpl->mEventData )
1633   {
1634     if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1635         ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1636     {
1637       mImpl->ChangeState( EventData::EDITING );
1638       mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1639     }
1640
1641     if( mImpl->IsShowingPlaceholderText() )
1642     {
1643       // Show alternative placeholder-text when editing
1644       ShowPlaceholderText();
1645     }
1646
1647     mImpl->RequestRelayout();
1648   }
1649 }
1650
1651 void Controller::KeyboardFocusLostEvent()
1652 {
1653   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1654
1655   if( NULL != mImpl->mEventData )
1656   {
1657     if( EventData::INTERRUPTED != mImpl->mEventData->mState )
1658     {
1659       mImpl->ChangeState( EventData::INACTIVE );
1660
1661       if( !mImpl->IsShowingRealText() )
1662       {
1663         // Revert to regular placeholder-text when not editing
1664         ShowPlaceholderText();
1665       }
1666     }
1667   }
1668   mImpl->RequestRelayout();
1669 }
1670
1671 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1672 {
1673   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1674
1675   bool textChanged( false );
1676
1677   if( ( NULL != mImpl->mEventData ) &&
1678       ( keyEvent.state == KeyEvent::Down ) )
1679   {
1680     int keyCode = keyEvent.keyCode;
1681     const std::string& keyString = keyEvent.keyPressed;
1682
1683     // Pre-process to separate modifying events from non-modifying input events.
1684     if( Dali::DALI_KEY_ESCAPE == keyCode )
1685     {
1686       // Escape key is a special case which causes focus loss
1687       KeyboardFocusLostEvent();
1688     }
1689     else if( ( Dali::DALI_KEY_CURSOR_LEFT  == keyCode ) ||
1690              ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
1691              ( Dali::DALI_KEY_CURSOR_UP    == keyCode ) ||
1692              ( Dali::DALI_KEY_CURSOR_DOWN  == keyCode ) )
1693     {
1694       Event event( Event::CURSOR_KEY_EVENT );
1695       event.p1.mInt = keyCode;
1696       mImpl->mEventData->mEventQueue.push_back( event );
1697     }
1698     else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1699     {
1700       textChanged = BackspaceKeyEvent();
1701     }
1702     else if( IsKey( keyEvent,  Dali::DALI_KEY_POWER ) )
1703     {
1704       mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1705       // Avoids calling the InsertText() method which can delete selected text
1706     }
1707     else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1708              IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1709     {
1710       mImpl->ChangeState( EventData::INACTIVE );
1711       // Menu/Home key behaviour does not allow edit mode to resume like Power key
1712       // Avoids calling the InsertText() method which can delete selected text
1713     }
1714     else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1715     {
1716       // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1717       // and a character is typed after the type of a upper case latin character.
1718
1719       // Do nothing.
1720     }
1721     else
1722     {
1723       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1724
1725       // IMF manager is no longer handling key-events
1726       mImpl->ClearPreEditFlag();
1727
1728       InsertText( keyString, COMMIT );
1729       textChanged = true;
1730     }
1731
1732     if( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1733         ( mImpl->mEventData->mState != EventData::INACTIVE ) )
1734     {
1735       mImpl->ChangeState( EventData::EDITING );
1736     }
1737
1738     mImpl->RequestRelayout();
1739   }
1740
1741   if( textChanged )
1742   {
1743     // Do this last since it provides callbacks into application code
1744     mImpl->mControlInterface.TextChanged();
1745   }
1746
1747   return true;
1748 }
1749
1750 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1751 {
1752   bool removedPrevious( false );
1753   bool maxLengthReached( false );
1754
1755   DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1756
1757   if( NULL == mImpl->mEventData )
1758   {
1759     return;
1760   }
1761
1762   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1763                  this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1764                  mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1765
1766   // TODO: At the moment the underline runs are only for pre-edit.
1767   mImpl->mVisualModel->mUnderlineRuns.Clear();
1768
1769   Vector<Character> utf32Characters;
1770   Length characterCount( 0u );
1771
1772   // Remove the previous IMF pre-edit (predicitive text)
1773   if( mImpl->mEventData->mPreEditFlag &&
1774       ( 0 != mImpl->mEventData->mPreEditLength ) )
1775   {
1776     CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1777
1778     removedPrevious = RemoveText( -static_cast<int>(offset), mImpl->mEventData->mPreEditLength );
1779
1780     mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1781     mImpl->mEventData->mPreEditLength = 0;
1782   }
1783   else
1784   {
1785     // Remove the previous Selection
1786     removedPrevious = RemoveSelectedText();
1787   }
1788
1789   if( !text.empty() )
1790   {
1791     //  Convert text into UTF-32
1792     utf32Characters.Resize( text.size() );
1793
1794     // This is a bit horrible but std::string returns a (signed) char*
1795     const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1796
1797     // Transform a text array encoded in utf8 into an array encoded in utf32.
1798     // It returns the actual number of characters.
1799     characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1800     utf32Characters.Resize( characterCount );
1801
1802     DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1803     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1804   }
1805
1806   if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1807   {
1808     // The placeholder text is no longer needed
1809     if( mImpl->IsShowingPlaceholderText() )
1810     {
1811       ResetText();
1812     }
1813
1814     mImpl->ChangeState( EventData::EDITING );
1815
1816     // Handle the IMF (predicitive text) state changes
1817     if( COMMIT == type )
1818     {
1819       // IMF manager is no longer handling key-events
1820       mImpl->ClearPreEditFlag();
1821     }
1822     else // PRE_EDIT
1823     {
1824       if( !mImpl->mEventData->mPreEditFlag )
1825       {
1826         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1827
1828         // Record the start of the pre-edit text
1829         mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1830       }
1831
1832       mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1833       mImpl->mEventData->mPreEditFlag = true;
1834
1835       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1836     }
1837
1838     const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
1839
1840     // Restrict new text to fit within Maximum characters setting
1841     Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1842     maxLengthReached = ( characterCount > maxSizeOfNewText );
1843
1844     // The cursor position.
1845     CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1846
1847     // Update the text's style.
1848
1849     // Updates the text style runs by adding characters.
1850     mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
1851
1852     // Get the character index from the cursor index.
1853     const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
1854
1855     // Retrieve the text's style for the given index.
1856     InputStyle style;
1857     mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
1858
1859     // Whether to add a new text color run.
1860     const bool addColorRun = style.textColor != mImpl->mEventData->mInputStyle.textColor;
1861
1862     // Whether to add a new font run.
1863     const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
1864     const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
1865     const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
1866     const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
1867     const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
1868
1869     // Add style runs.
1870     if( addColorRun )
1871     {
1872       const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
1873       mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
1874
1875       ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
1876       colorRun.color = mImpl->mEventData->mInputStyle.textColor;
1877       colorRun.characterRun.characterIndex = cursorIndex;
1878       colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
1879     }
1880
1881     if( addFontNameRun   ||
1882         addFontWeightRun ||
1883         addFontWidthRun  ||
1884         addFontSlantRun  ||
1885         addFontSizeRun )
1886     {
1887       const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
1888       mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
1889
1890       FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
1891
1892       if( addFontNameRun )
1893       {
1894         fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
1895         fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
1896         memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
1897         fontDescriptionRun.familyDefined = true;
1898
1899         // The memory allocated for the font family name is freed when the font description is removed from the logical model.
1900       }
1901
1902       if( addFontWeightRun )
1903       {
1904         fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
1905         fontDescriptionRun.weightDefined = true;
1906       }
1907
1908       if( addFontWidthRun )
1909       {
1910         fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
1911         fontDescriptionRun.widthDefined = true;
1912       }
1913
1914       if( addFontSlantRun )
1915       {
1916         fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
1917         fontDescriptionRun.slantDefined = true;
1918       }
1919
1920       if( addFontSizeRun )
1921       {
1922         fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
1923         fontDescriptionRun.sizeDefined = true;
1924       }
1925
1926       fontDescriptionRun.characterRun.characterIndex = cursorIndex;
1927       fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
1928     }
1929
1930     // Insert at current cursor position.
1931     Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1932
1933     if( cursorIndex < numberOfCharactersInModel )
1934     {
1935       modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1936     }
1937     else
1938     {
1939       modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1940     }
1941
1942     cursorIndex += maxSizeOfNewText;
1943
1944     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1945   }
1946
1947   if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
1948       mImpl->IsPlaceholderAvailable() )
1949   {
1950     // Show place-holder if empty after removing the pre-edit text
1951     ShowPlaceholderText();
1952     mImpl->mEventData->mUpdateCursorPosition = true;
1953     mImpl->ClearPreEditFlag();
1954   }
1955   else if( removedPrevious ||
1956            ( 0 != utf32Characters.Count() ) )
1957   {
1958     // Queue an inserted event
1959     mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
1960   }
1961
1962   if( maxLengthReached )
1963   {
1964     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
1965
1966     mImpl->ResetImfManager();
1967
1968     // Do this last since it provides callbacks into application code
1969     mImpl->mControlInterface.MaxLengthReached();
1970   }
1971 }
1972
1973 bool Controller::RemoveSelectedText()
1974 {
1975   bool textRemoved( false );
1976
1977   if( EventData::SELECTING == mImpl->mEventData->mState )
1978   {
1979     std::string removedString;
1980     mImpl->RetrieveSelection( removedString, true );
1981
1982     if( !removedString.empty() )
1983     {
1984       textRemoved = true;
1985       mImpl->ChangeState( EventData::EDITING );
1986     }
1987   }
1988
1989   return textRemoved;
1990 }
1991
1992 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1993 {
1994   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
1995
1996   if( NULL != mImpl->mEventData )
1997   {
1998     DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
1999
2000     if( 1u == tapCount )
2001     {
2002       // This is to avoid unnecessary relayouts when tapping an empty text-field
2003       bool relayoutNeeded( false );
2004
2005       if( ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) ||
2006           ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
2007       {
2008         mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE);  // If Popup shown hide it here so can be shown again if required.
2009       }
2010
2011       if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != mImpl->mEventData->mState ) )
2012       {
2013         // Already in an active state so show a popup
2014         if( !mImpl->IsClipboardEmpty() )
2015         {
2016           // Shows Paste popup but could show full popup with Selection options. ( EDITING_WITH_POPUP )
2017           mImpl->ChangeState( EventData::EDITING_WITH_PASTE_POPUP );
2018         }
2019         else
2020         {
2021           mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
2022         }
2023         relayoutNeeded = true;
2024       }
2025       else
2026       {
2027         if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
2028         {
2029           // Hide placeholder text
2030           ResetText();
2031         }
2032
2033         if( EventData::INACTIVE == mImpl->mEventData->mState )
2034         {
2035           mImpl->ChangeState( EventData::EDITING );
2036         }
2037         else if( !mImpl->IsClipboardEmpty() )
2038         {
2039           mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
2040         }
2041         relayoutNeeded = true;
2042       }
2043
2044       // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
2045       if( relayoutNeeded )
2046       {
2047         Event event( Event::TAP_EVENT );
2048         event.p1.mUint = tapCount;
2049         event.p2.mFloat = x;
2050         event.p3.mFloat = y;
2051         mImpl->mEventData->mEventQueue.push_back( event );
2052
2053         mImpl->RequestRelayout();
2054       }
2055     }
2056     else if( 2u == tapCount )
2057     {
2058       if( mImpl->mEventData->mSelectionEnabled &&
2059           mImpl->IsShowingRealText() )
2060       {
2061         SelectEvent( x, y, false );
2062       }
2063     }
2064   }
2065
2066   // Reset keyboard as tap event has occurred.
2067   mImpl->ResetImfManager();
2068 }
2069
2070 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
2071         // Show cursor and grabhandle on first tap, this matches the behaviour of tapping when already editing
2072 {
2073   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
2074
2075   if( NULL != mImpl->mEventData )
2076   {
2077     Event event( Event::PAN_EVENT );
2078     event.p1.mInt = state;
2079     event.p2.mFloat = displacement.x;
2080     event.p3.mFloat = displacement.y;
2081     mImpl->mEventData->mEventQueue.push_back( event );
2082
2083     mImpl->RequestRelayout();
2084   }
2085 }
2086
2087 void Controller::LongPressEvent( Gesture::State state, float x, float y  )
2088 {
2089   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
2090
2091   if( ( state == Gesture::Started ) &&
2092       ( NULL != mImpl->mEventData ) )
2093   {
2094     if( !mImpl->IsShowingRealText() )
2095     {
2096       Event event( Event::LONG_PRESS_EVENT );
2097       event.p1.mInt = state;
2098       mImpl->mEventData->mEventQueue.push_back( event );
2099       mImpl->RequestRelayout();
2100     }
2101     else
2102     {
2103       // The 1st long-press on inactive text-field is treated as tap
2104       if( EventData::INACTIVE == mImpl->mEventData->mState )
2105       {
2106         mImpl->ChangeState( EventData::EDITING );
2107
2108         Event event( Event::TAP_EVENT );
2109         event.p1.mUint = 1;
2110         event.p2.mFloat = x;
2111         event.p3.mFloat = y;
2112         mImpl->mEventData->mEventQueue.push_back( event );
2113
2114         mImpl->RequestRelayout();
2115       }
2116       else
2117       {
2118         // Reset the imf manger to commit the pre-edit before selecting the text.
2119         mImpl->ResetImfManager();
2120
2121         SelectEvent( x, y, false );
2122       }
2123     }
2124   }
2125 }
2126
2127 void Controller::SelectEvent( float x, float y, bool selectAll )
2128 {
2129   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2130
2131   if( NULL != mImpl->mEventData )
2132   {
2133     mImpl->ChangeState( EventData::SELECTING );
2134
2135     if( selectAll )
2136     {
2137       Event event( Event::SELECT_ALL );
2138       mImpl->mEventData->mEventQueue.push_back( event );
2139     }
2140     else
2141     {
2142       Event event( Event::SELECT );
2143       event.p2.mFloat = x;
2144       event.p3.mFloat = y;
2145       mImpl->mEventData->mEventQueue.push_back( event );
2146     }
2147
2148     mImpl->RequestRelayout();
2149   }
2150 }
2151
2152 void Controller::GetTargetSize( Vector2& targetSize )
2153 {
2154   targetSize = mImpl->mVisualModel->mControlSize;
2155 }
2156
2157 void Controller::AddDecoration( Actor& actor, bool needsClipping )
2158 {
2159   mImpl->mControlInterface.AddDecoration( actor, needsClipping );
2160 }
2161
2162 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
2163 {
2164   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
2165
2166   if( NULL != mImpl->mEventData )
2167   {
2168     switch( handleType )
2169     {
2170       case GRAB_HANDLE:
2171       {
2172         Event event( Event::GRAB_HANDLE_EVENT );
2173         event.p1.mUint  = state;
2174         event.p2.mFloat = x;
2175         event.p3.mFloat = y;
2176
2177         mImpl->mEventData->mEventQueue.push_back( event );
2178         break;
2179       }
2180       case LEFT_SELECTION_HANDLE:
2181       {
2182         Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2183         event.p1.mUint  = state;
2184         event.p2.mFloat = x;
2185         event.p3.mFloat = y;
2186
2187         mImpl->mEventData->mEventQueue.push_back( event );
2188         break;
2189       }
2190       case RIGHT_SELECTION_HANDLE:
2191       {
2192         Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2193         event.p1.mUint  = state;
2194         event.p2.mFloat = x;
2195         event.p3.mFloat = y;
2196
2197         mImpl->mEventData->mEventQueue.push_back( event );
2198         break;
2199       }
2200       case LEFT_SELECTION_HANDLE_MARKER:
2201       case RIGHT_SELECTION_HANDLE_MARKER:
2202       {
2203         // Markers do not move the handles.
2204         break;
2205       }
2206       case HANDLE_TYPE_COUNT:
2207       {
2208         DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2209       }
2210     }
2211
2212     mImpl->RequestRelayout();
2213   }
2214 }
2215
2216 void Controller::PasteText( const std::string& stringToPaste )
2217 {
2218   InsertText( stringToPaste, Text::Controller::COMMIT );
2219   mImpl->ChangeState( EventData::EDITING );
2220   mImpl->RequestRelayout();
2221
2222   // Do this last since it provides callbacks into application code
2223   mImpl->mControlInterface.TextChanged();
2224 }
2225
2226 void Controller::PasteClipboardItemEvent()
2227 {
2228   // Retrieve the clipboard contents first
2229   ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
2230   std::string stringToPaste( notifier.GetContent() );
2231
2232   // Commit the current pre-edit text; the contents of the clipboard should be appended
2233   mImpl->ResetImfManager();
2234
2235   // Paste
2236   PasteText( stringToPaste );
2237 }
2238
2239 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2240 {
2241   if( NULL == mImpl->mEventData )
2242   {
2243     return;
2244   }
2245
2246   switch( button )
2247   {
2248     case Toolkit::TextSelectionPopup::CUT:
2249     {
2250       mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2251       mImpl->mOperationsPending = ALL_OPERATIONS;
2252
2253       // This is to reset the virtual keyboard to Upper-case
2254       if( 0u == mImpl->mLogicalModel->mText.Count() )
2255       {
2256         NotifyImfManager();
2257       }
2258
2259       if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2260           !mImpl->IsPlaceholderAvailable() )
2261       {
2262         mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2263       }
2264       else
2265       {
2266         ShowPlaceholderText();
2267         mImpl->mEventData->mUpdateCursorPosition = true;
2268       }
2269       mImpl->RequestRelayout();
2270       mImpl->mControlInterface.TextChanged();
2271       break;
2272     }
2273     case Toolkit::TextSelectionPopup::COPY:
2274     {
2275       mImpl->SendSelectionToClipboard( false ); // Text not modified
2276       mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
2277       break;
2278     }
2279     case Toolkit::TextSelectionPopup::PASTE:
2280     {
2281       std::string stringToPaste("");
2282       mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
2283       PasteText( stringToPaste );
2284       break;
2285     }
2286     case Toolkit::TextSelectionPopup::SELECT:
2287     {
2288       const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2289
2290       if( mImpl->mEventData->mSelectionEnabled )
2291       {
2292         // Creates a SELECT event.
2293         SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2294       }
2295       break;
2296     }
2297     case Toolkit::TextSelectionPopup::SELECT_ALL:
2298     {
2299       // Creates a SELECT_ALL event
2300       SelectEvent( 0.f, 0.f, true );
2301       break;
2302     }
2303     case Toolkit::TextSelectionPopup::CLIPBOARD:
2304     {
2305       mImpl->ShowClipboard();
2306       break;
2307     }
2308     case Toolkit::TextSelectionPopup::NONE:
2309     {
2310       // Nothing to do.
2311       break;
2312     }
2313   }
2314 }
2315
2316 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
2317 {
2318   bool update = false;
2319   bool requestRelayout = false;
2320
2321   std::string text;
2322   unsigned int cursorPosition = 0u;
2323
2324   switch( imfEvent.eventName )
2325   {
2326     case ImfManager::COMMIT:
2327     {
2328       InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
2329       update = true;
2330       requestRelayout = true;
2331       break;
2332     }
2333     case ImfManager::PREEDIT:
2334     {
2335       InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
2336       update = true;
2337       requestRelayout = true;
2338       break;
2339     }
2340     case ImfManager::DELETESURROUNDING:
2341     {
2342       update = RemoveText( imfEvent.cursorOffset, imfEvent.numberOfChars );
2343
2344       if( update )
2345       {
2346         if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2347             !mImpl->IsPlaceholderAvailable() )
2348         {
2349           mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2350         }
2351         else
2352         {
2353           ShowPlaceholderText();
2354           mImpl->mEventData->mUpdateCursorPosition = true;
2355         }
2356       }
2357       requestRelayout = true;
2358       break;
2359     }
2360     case ImfManager::GETSURROUNDING:
2361     {
2362       GetText( text );
2363       cursorPosition = GetLogicalCursorPosition();
2364
2365       imfManager.SetSurroundingText( text );
2366       imfManager.SetCursorPosition( cursorPosition );
2367       break;
2368     }
2369     case ImfManager::VOID:
2370     {
2371       // do nothing
2372       break;
2373     }
2374   } // end switch
2375
2376   if( ImfManager::GETSURROUNDING != imfEvent.eventName )
2377   {
2378     GetText( text );
2379     cursorPosition = GetLogicalCursorPosition();
2380   }
2381
2382   if( requestRelayout )
2383   {
2384     mImpl->mOperationsPending = ALL_OPERATIONS;
2385     mImpl->RequestRelayout();
2386
2387     // Do this last since it provides callbacks into application code
2388     mImpl->mControlInterface.TextChanged();
2389   }
2390
2391   ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
2392
2393   return callbackData;
2394 }
2395
2396 Controller::~Controller()
2397 {
2398   delete mImpl;
2399 }
2400
2401 bool Controller::BackspaceKeyEvent()
2402 {
2403   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2404
2405   bool removed = false;
2406
2407   if( NULL == mImpl->mEventData )
2408   {
2409     return removed;
2410   }
2411
2412   // IMF manager is no longer handling key-events
2413   mImpl->ClearPreEditFlag();
2414
2415   if( EventData::SELECTING == mImpl->mEventData->mState )
2416   {
2417     removed = RemoveSelectedText();
2418   }
2419   else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2420   {
2421     // Remove the character before the current cursor position
2422     removed = RemoveText( -1, 1 );
2423   }
2424
2425   if( removed )
2426   {
2427     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE RemovedText\n", this );
2428     // Notifiy the IMF manager after text changed
2429     // Automatic  Upper-case and restarting prediction on an existing word require this.
2430     NotifyImfManager();
2431
2432     if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2433         !mImpl->IsPlaceholderAvailable() )
2434     {
2435       mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2436     }
2437     else
2438     {
2439       ShowPlaceholderText();
2440       mImpl->mEventData->mUpdateCursorPosition = true;
2441     }
2442   }
2443
2444   return removed;
2445 }
2446
2447 void Controller::NotifyImfManager()
2448 {
2449   if( NULL != mImpl->mEventData )
2450   {
2451     if( mImpl->mEventData->mImfManager )
2452     {
2453       // Notifying IMF of a cursor change triggers a surrounding text request so updating it now.
2454       std::string text;
2455       GetText( text );
2456       mImpl->mEventData->mImfManager.SetSurroundingText( text );
2457
2458       mImpl->mEventData->mImfManager.SetCursorPosition( GetLogicalCursorPosition() );
2459       mImpl->mEventData->mImfManager.NotifyCursorPosition();
2460     }
2461   }
2462 }
2463
2464 void Controller::ShowPlaceholderText()
2465 {
2466   if( mImpl->IsPlaceholderAvailable() )
2467   {
2468     DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2469
2470     if( NULL == mImpl->mEventData )
2471     {
2472       return;
2473     }
2474
2475     mImpl->mEventData->mIsShowingPlaceholderText = true;
2476
2477     // Disable handles when showing place-holder text
2478     mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2479     mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2480     mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2481
2482     const char* text( NULL );
2483     size_t size( 0 );
2484
2485     // TODO - Switch placeholder text styles when changing state
2486     if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2487         ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2488     {
2489       text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2490       size = mImpl->mEventData->mPlaceholderTextActive.size();
2491     }
2492     else
2493     {
2494       text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2495       size = mImpl->mEventData->mPlaceholderTextInactive.size();
2496     }
2497
2498     // Reset model for showing placeholder.
2499     mImpl->mLogicalModel->mText.Clear();
2500     ClearModelData();
2501     mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2502
2503     // Convert text into UTF-32
2504     Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2505     utf32Characters.Resize( size );
2506
2507     // This is a bit horrible but std::string returns a (signed) char*
2508     const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2509
2510     // Transform a text array encoded in utf8 into an array encoded in utf32.
2511     // It returns the actual number of characters.
2512     Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2513     utf32Characters.Resize( characterCount );
2514
2515     // Reset the cursor position
2516     mImpl->mEventData->mPrimaryCursorPosition = 0;
2517
2518     // The natural size needs to be re-calculated.
2519     mImpl->mRecalculateNaturalSize = true;
2520
2521     // Apply modifications to the model
2522     mImpl->mOperationsPending = ALL_OPERATIONS;
2523
2524     // Update the rest of the model during size negotiation
2525     mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2526   }
2527 }
2528
2529 void Controller::ClearModelData()
2530 {
2531   // n.b. This does not Clear the mText from mLogicalModel
2532   mImpl->mLogicalModel->mScriptRuns.Clear();
2533   mImpl->mLogicalModel->mFontRuns.Clear();
2534   mImpl->mLogicalModel->mLineBreakInfo.Clear();
2535   mImpl->mLogicalModel->mWordBreakInfo.Clear();
2536   mImpl->mLogicalModel->mBidirectionalParagraphInfo.Clear();
2537   mImpl->mLogicalModel->mCharacterDirections.Clear();
2538   mImpl->mLogicalModel->mBidirectionalLineInfo.Clear();
2539   mImpl->mLogicalModel->mLogicalToVisualMap.Clear();
2540   mImpl->mLogicalModel->mVisualToLogicalMap.Clear();
2541   mImpl->mVisualModel->mGlyphs.Clear();
2542   mImpl->mVisualModel->mGlyphsToCharacters.Clear();
2543   mImpl->mVisualModel->mCharactersToGlyph.Clear();
2544   mImpl->mVisualModel->mCharactersPerGlyph.Clear();
2545   mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
2546   mImpl->mVisualModel->mGlyphPositions.Clear();
2547   mImpl->mVisualModel->mLines.Clear();
2548   mImpl->mVisualModel->mColorRuns.Clear();
2549   mImpl->mVisualModel->ClearCaches();
2550 }
2551
2552 void Controller::ClearFontData()
2553 {
2554   mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2555   mImpl->mLogicalModel->mFontRuns.Clear();
2556   mImpl->mVisualModel->mGlyphs.Clear();
2557   mImpl->mVisualModel->mGlyphsToCharacters.Clear();
2558   mImpl->mVisualModel->mCharactersToGlyph.Clear();
2559   mImpl->mVisualModel->mCharactersPerGlyph.Clear();
2560   mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
2561   mImpl->mVisualModel->mGlyphPositions.Clear();
2562   mImpl->mVisualModel->mLines.Clear();
2563   mImpl->mVisualModel->ClearCaches();
2564 }
2565
2566 void Controller::ClearStyleData()
2567 {
2568   mImpl->mLogicalModel->mColorRuns.Clear();
2569   mImpl->mLogicalModel->ClearFontDescriptionRuns();
2570 }
2571
2572 Controller::Controller( ControlInterface& controlInterface )
2573 : mImpl( NULL )
2574 {
2575   mImpl = new Controller::Impl( controlInterface );
2576 }
2577
2578 } // namespace Text
2579
2580 } // namespace Toolkit
2581
2582 } // namespace Dali