[dali_1.1.28] Merge branch 'devel/master'
[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       if( UPDATE_ACTUAL_SIZE & operations )
1331       {
1332         mImpl->mVisualModel->SetLayoutSize( Size::ZERO );
1333       }
1334
1335       // Nothing else to do if there is no glyphs.
1336       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout no glyphs, view updated true\n" );
1337       return true;
1338     }
1339
1340     const Vector<LineBreakInfo>& lineBreakInfo = mImpl->mLogicalModel->mLineBreakInfo;
1341     const Vector<WordBreakInfo>& wordBreakInfo = mImpl->mLogicalModel->mWordBreakInfo;
1342     const Vector<CharacterDirection>& characterDirection = mImpl->mLogicalModel->mCharacterDirections;
1343     const Vector<GlyphInfo>& glyphs = mImpl->mVisualModel->mGlyphs;
1344     const Vector<CharacterIndex>& glyphsToCharactersMap = mImpl->mVisualModel->mGlyphsToCharacters;
1345     const Vector<Length>& charactersPerGlyph = mImpl->mVisualModel->mCharactersPerGlyph;
1346     const Character* const textBuffer = mImpl->mLogicalModel->mText.Begin();
1347     const Vector<GlyphIndex>& charactersToGlyph = mImpl->mVisualModel->mCharactersToGlyph;
1348     const Vector<Length>& glyphsPerCharacter = mImpl->mVisualModel->mGlyphsPerCharacter;
1349
1350     // Set the layout parameters.
1351     LayoutParameters layoutParameters( size,
1352                                        textBuffer,
1353                                        lineBreakInfo.Begin(),
1354                                        wordBreakInfo.Begin(),
1355                                        ( 0u != characterDirection.Count() ) ? characterDirection.Begin() : NULL,
1356                                        glyphs.Begin(),
1357                                        glyphsToCharactersMap.Begin(),
1358                                        charactersPerGlyph.Begin(),
1359                                        charactersToGlyph.Begin(),
1360                                        glyphsPerCharacter.Begin(),
1361                                        numberOfGlyphs );
1362
1363     // The laid-out lines.
1364     // It's not possible to know in how many lines the text is going to be laid-out,
1365     // but it can be resized at least with the number of 'paragraphs' to avoid
1366     // some re-allocations.
1367     Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1368
1369     // Delete any previous laid out lines before setting the new ones.
1370     lines.Clear();
1371
1372     // The capacity of the bidirectional paragraph info is the number of paragraphs.
1373     lines.Reserve( mImpl->mLogicalModel->mBidirectionalParagraphInfo.Capacity() );
1374
1375     // Resize the vector of positions to have the same size than the vector of glyphs.
1376     Vector<Vector2>& glyphPositions = mImpl->mVisualModel->mGlyphPositions;
1377     glyphPositions.Resize( numberOfGlyphs );
1378
1379     // Whether the last character is a new paragraph character.
1380     layoutParameters.isLastNewParagraph = TextAbstraction::IsNewParagraph( *( textBuffer + ( mImpl->mLogicalModel->mText.Count() - 1u ) ) );
1381
1382     // The initial glyph and the number of glyphs to layout.
1383     layoutParameters.startGlyphIndex = 0u;
1384     layoutParameters.numberOfGlyphs = numberOfGlyphs;
1385
1386     // Update the visual model.
1387     viewUpdated = mImpl->mLayoutEngine.LayoutText( layoutParameters,
1388                                                    glyphPositions,
1389                                                    lines,
1390                                                    layoutSize );
1391
1392     if( viewUpdated )
1393     {
1394       // Reorder the lines
1395       if( REORDER & operations )
1396       {
1397         Vector<BidirectionalParagraphInfoRun>& bidirectionalInfo = mImpl->mLogicalModel->mBidirectionalParagraphInfo;
1398         Vector<BidirectionalLineInfoRun>& bidirectionalLineInfo = mImpl->mLogicalModel->mBidirectionalLineInfo;
1399
1400         // Check first if there are paragraphs with bidirectional info.
1401         if( 0u != bidirectionalInfo.Count() )
1402         {
1403           // Get the lines
1404           const Length numberOfLines = mImpl->mVisualModel->mLines.Count();
1405           const CharacterIndex startIndex = 0u;
1406           const Length requestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1407
1408           // Reorder the lines.
1409           bidirectionalLineInfo.Reserve( numberOfLines ); // Reserve because is not known yet how many lines have right to left characters.
1410           ReorderLines( bidirectionalInfo,
1411                         startIndex,
1412                         requestedNumberOfCharacters,
1413                         lines,
1414                         bidirectionalLineInfo );
1415
1416           // Set the bidirectional info per line into the layout parameters.
1417           layoutParameters.lineBidirectionalInfoRunsBuffer = bidirectionalLineInfo.Begin();
1418           layoutParameters.numberOfBidirectionalInfoRuns = bidirectionalLineInfo.Count();
1419
1420           // Set the bidirectional info into the model.
1421           mImpl->mLogicalModel->SetVisualToLogicalMap( layoutParameters.lineBidirectionalInfoRunsBuffer,
1422                                                        layoutParameters.numberOfBidirectionalInfoRuns,
1423                                                        startIndex,
1424                                                        requestedNumberOfCharacters );
1425
1426           // Re-layout the text. Reorder those lines with right to left characters.
1427           mImpl->mLayoutEngine.ReLayoutRightToLeftLines( layoutParameters,
1428                                                          startIndex,
1429                                                          requestedNumberOfCharacters,
1430                                                          glyphPositions );
1431
1432           // Free the allocated memory used to store the conversion table in the bidirectional line info run.
1433           for( Vector<BidirectionalLineInfoRun>::Iterator it = bidirectionalLineInfo.Begin(),
1434                  endIt = bidirectionalLineInfo.End();
1435                it != endIt;
1436                ++it )
1437           {
1438             BidirectionalLineInfoRun& bidiLineInfo = *it;
1439
1440             free( bidiLineInfo.visualToLogicalMap );
1441             bidiLineInfo.visualToLogicalMap = NULL;
1442           }
1443
1444           bidirectionalLineInfo.Clear();
1445         }
1446       } // REORDER
1447
1448       // Sets the actual size.
1449       if( UPDATE_ACTUAL_SIZE & operations )
1450       {
1451         mImpl->mVisualModel->SetLayoutSize( layoutSize );
1452       }
1453     } // view updated
1454   }
1455   else
1456   {
1457     layoutSize = mImpl->mVisualModel->GetLayoutSize();
1458   }
1459
1460   if( ALIGN & operations )
1461   {
1462     // The laid-out lines.
1463     Vector<LineRun>& lines = mImpl->mVisualModel->mLines;
1464
1465     const CharacterIndex startIndex = 0u;
1466     const Length requestedNumberOfCharacters = mImpl->mLogicalModel->mText.Count();
1467
1468     mImpl->mLayoutEngine.Align( size,
1469                                 startIndex,
1470                                 requestedNumberOfCharacters,
1471                                 lines );
1472
1473     viewUpdated = true;
1474   }
1475
1476   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "<--Controller::DoRelayout, view updated %s\n", ( viewUpdated ? "true" : "false" ) );
1477   return viewUpdated;
1478 }
1479
1480 void Controller::SetMultiLineEnabled( bool enable )
1481 {
1482   const LayoutEngine::Layout layout = enable ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
1483
1484   if( layout != mImpl->mLayoutEngine.GetLayout() )
1485   {
1486     // Set the layout type.
1487     mImpl->mLayoutEngine.SetLayout( layout );
1488
1489     // Set the flags to redo the layout operations
1490     const OperationsMask layoutOperations =  static_cast<OperationsMask>( LAYOUT             |
1491                                                                           UPDATE_ACTUAL_SIZE |
1492                                                                           ALIGN              |
1493                                                                           REORDER );
1494
1495     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | layoutOperations );
1496
1497     mImpl->RequestRelayout();
1498   }
1499 }
1500
1501 bool Controller::IsMultiLineEnabled() const
1502 {
1503   return LayoutEngine::MULTI_LINE_BOX == mImpl->mLayoutEngine.GetLayout();
1504 }
1505
1506 void Controller::SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment )
1507 {
1508   if( alignment != mImpl->mLayoutEngine.GetHorizontalAlignment() )
1509   {
1510     // Set the alignment.
1511     mImpl->mLayoutEngine.SetHorizontalAlignment( alignment );
1512
1513     // Set the flag to redo the alignment operation.
1514     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1515
1516     mImpl->RequestRelayout();
1517   }
1518 }
1519
1520 LayoutEngine::HorizontalAlignment Controller::GetHorizontalAlignment() const
1521 {
1522   return mImpl->mLayoutEngine.GetHorizontalAlignment();
1523 }
1524
1525 void Controller::SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment )
1526 {
1527   if( alignment != mImpl->mLayoutEngine.GetVerticalAlignment() )
1528   {
1529     // Set the alignment.
1530     mImpl->mLayoutEngine.SetVerticalAlignment( alignment );
1531
1532     mImpl->mOperationsPending = static_cast<OperationsMask>( mImpl->mOperationsPending | ALIGN );
1533
1534     mImpl->RequestRelayout();
1535   }
1536 }
1537
1538 LayoutEngine::VerticalAlignment Controller::GetVerticalAlignment() const
1539 {
1540   return mImpl->mLayoutEngine.GetVerticalAlignment();
1541 }
1542
1543 void Controller::CalculateTextAlignment( const Size& controlSize )
1544 {
1545   Size layoutSize = mImpl->mVisualModel->GetLayoutSize();
1546
1547   if( fabsf( layoutSize.height ) < Math::MACHINE_EPSILON_1000 )
1548   {
1549     // Get the line height of the default font.
1550     layoutSize.height = mImpl->GetDefaultFontLineHeight();
1551   }
1552
1553   if( LayoutEngine::SINGLE_LINE_BOX == mImpl->mLayoutEngine.GetLayout() )
1554   {
1555     // Get the direction of the first character.
1556     const CharacterDirection firstParagraphDirection = mImpl->mLogicalModel->GetCharacterDirection( 0u );
1557
1558     // If the first paragraph is right to left swap ALIGN_BEGIN and ALIGN_END;
1559     LayoutEngine::HorizontalAlignment horizontalAlignment = mImpl->mLayoutEngine.GetHorizontalAlignment();
1560     if( firstParagraphDirection )
1561     {
1562       switch( horizontalAlignment )
1563       {
1564         case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1565         {
1566           horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_END;
1567           break;
1568         }
1569         case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1570         {
1571           // Nothing to do.
1572           break;
1573         }
1574         case LayoutEngine::HORIZONTAL_ALIGN_END:
1575         {
1576           horizontalAlignment = LayoutEngine::HORIZONTAL_ALIGN_BEGIN;
1577           break;
1578         }
1579       }
1580     }
1581
1582     switch( horizontalAlignment )
1583     {
1584       case LayoutEngine::HORIZONTAL_ALIGN_BEGIN:
1585       {
1586         mImpl->mAlignmentOffset.x = 0.f;
1587         break;
1588       }
1589       case LayoutEngine::HORIZONTAL_ALIGN_CENTER:
1590       {
1591         mImpl->mAlignmentOffset.x = floorf( 0.5f * ( controlSize.width - layoutSize.width ) ); // try to avoid pixel alignment.
1592         break;
1593       }
1594       case LayoutEngine::HORIZONTAL_ALIGN_END:
1595       {
1596         mImpl->mAlignmentOffset.x = controlSize.width - layoutSize.width;
1597         break;
1598       }
1599     }
1600   }
1601
1602   const LayoutEngine::VerticalAlignment verticalAlignment = mImpl->mLayoutEngine.GetVerticalAlignment();
1603   switch( verticalAlignment )
1604   {
1605     case LayoutEngine::VERTICAL_ALIGN_TOP:
1606     {
1607       mImpl->mAlignmentOffset.y = 0.f;
1608       break;
1609     }
1610     case LayoutEngine::VERTICAL_ALIGN_CENTER:
1611     {
1612       mImpl->mAlignmentOffset.y = floorf( 0.5f * ( controlSize.height - layoutSize.height ) ); // try to avoid pixel alignment.
1613       break;
1614     }
1615     case LayoutEngine::VERTICAL_ALIGN_BOTTOM:
1616     {
1617       mImpl->mAlignmentOffset.y = controlSize.height - layoutSize.height;
1618       break;
1619     }
1620   }
1621 }
1622
1623 LayoutEngine& Controller::GetLayoutEngine()
1624 {
1625   return mImpl->mLayoutEngine;
1626 }
1627
1628 View& Controller::GetView()
1629 {
1630   return mImpl->mView;
1631 }
1632
1633 void Controller::KeyboardFocusGainEvent()
1634 {
1635   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusGainEvent" );
1636
1637   if( NULL != mImpl->mEventData )
1638   {
1639     if( ( EventData::INACTIVE == mImpl->mEventData->mState ) ||
1640         ( EventData::INTERRUPTED == mImpl->mEventData->mState ) )
1641     {
1642       mImpl->ChangeState( EventData::EDITING );
1643       mImpl->mEventData->mUpdateCursorPosition = true; //If editing started without tap event, cursor update must be triggered.
1644     }
1645
1646     if( mImpl->IsShowingPlaceholderText() )
1647     {
1648       // Show alternative placeholder-text when editing
1649       ShowPlaceholderText();
1650     }
1651
1652     mImpl->RequestRelayout();
1653   }
1654 }
1655
1656 void Controller::KeyboardFocusLostEvent()
1657 {
1658   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyboardFocusLostEvent" );
1659
1660   if( NULL != mImpl->mEventData )
1661   {
1662     if( EventData::INTERRUPTED != mImpl->mEventData->mState )
1663     {
1664       mImpl->ChangeState( EventData::INACTIVE );
1665
1666       if( !mImpl->IsShowingRealText() )
1667       {
1668         // Revert to regular placeholder-text when not editing
1669         ShowPlaceholderText();
1670       }
1671     }
1672   }
1673   mImpl->RequestRelayout();
1674 }
1675
1676 bool Controller::KeyEvent( const Dali::KeyEvent& keyEvent )
1677 {
1678   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected KeyEvent" );
1679
1680   bool textChanged( false );
1681
1682   if( ( NULL != mImpl->mEventData ) &&
1683       ( keyEvent.state == KeyEvent::Down ) )
1684   {
1685     int keyCode = keyEvent.keyCode;
1686     const std::string& keyString = keyEvent.keyPressed;
1687
1688     // Pre-process to separate modifying events from non-modifying input events.
1689     if( Dali::DALI_KEY_ESCAPE == keyCode )
1690     {
1691       // Escape key is a special case which causes focus loss
1692       KeyboardFocusLostEvent();
1693     }
1694     else if( ( Dali::DALI_KEY_CURSOR_LEFT  == keyCode ) ||
1695              ( Dali::DALI_KEY_CURSOR_RIGHT == keyCode ) ||
1696              ( Dali::DALI_KEY_CURSOR_UP    == keyCode ) ||
1697              ( Dali::DALI_KEY_CURSOR_DOWN  == keyCode ) )
1698     {
1699       Event event( Event::CURSOR_KEY_EVENT );
1700       event.p1.mInt = keyCode;
1701       mImpl->mEventData->mEventQueue.push_back( event );
1702     }
1703     else if( Dali::DALI_KEY_BACKSPACE == keyCode )
1704     {
1705       textChanged = BackspaceKeyEvent();
1706     }
1707     else if( IsKey( keyEvent,  Dali::DALI_KEY_POWER ) )
1708     {
1709       mImpl->ChangeState( EventData::INTERRUPTED ); // State is not INACTIVE as expect to return to edit mode.
1710       // Avoids calling the InsertText() method which can delete selected text
1711     }
1712     else if( IsKey( keyEvent, Dali::DALI_KEY_MENU ) ||
1713              IsKey( keyEvent, Dali::DALI_KEY_HOME ) )
1714     {
1715       mImpl->ChangeState( EventData::INACTIVE );
1716       // Menu/Home key behaviour does not allow edit mode to resume like Power key
1717       // Avoids calling the InsertText() method which can delete selected text
1718     }
1719     else if( Dali::DALI_KEY_SHIFT_LEFT == keyCode )
1720     {
1721       // DALI_KEY_SHIFT_LEFT is the key code for the Left Shift. It's sent (by the imf?) when the predictive text is enabled
1722       // and a character is typed after the type of a upper case latin character.
1723
1724       // Do nothing.
1725     }
1726     else
1727     {
1728       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p keyString %s\n", this, keyString.c_str() );
1729
1730       // IMF manager is no longer handling key-events
1731       mImpl->ClearPreEditFlag();
1732
1733       InsertText( keyString, COMMIT );
1734       textChanged = true;
1735     }
1736
1737     if( ( mImpl->mEventData->mState != EventData::INTERRUPTED ) &&
1738         ( mImpl->mEventData->mState != EventData::INACTIVE ) )
1739     {
1740       mImpl->ChangeState( EventData::EDITING );
1741     }
1742
1743     mImpl->RequestRelayout();
1744   }
1745
1746   if( textChanged )
1747   {
1748     // Do this last since it provides callbacks into application code
1749     mImpl->mControlInterface.TextChanged();
1750   }
1751
1752   return true;
1753 }
1754
1755 void Controller::InsertText( const std::string& text, Controller::InsertType type )
1756 {
1757   bool removedPrevious( false );
1758   bool maxLengthReached( false );
1759
1760   DALI_ASSERT_DEBUG( NULL != mImpl->mEventData && "Unexpected InsertText" )
1761
1762   if( NULL == mImpl->mEventData )
1763   {
1764     return;
1765   }
1766
1767   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n",
1768                  this, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"),
1769                  mImpl->mEventData->mPrimaryCursorPosition, mImpl->mEventData->mPreEditFlag, mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1770
1771   // TODO: At the moment the underline runs are only for pre-edit.
1772   mImpl->mVisualModel->mUnderlineRuns.Clear();
1773
1774   Vector<Character> utf32Characters;
1775   Length characterCount( 0u );
1776
1777   // Remove the previous IMF pre-edit (predicitive text)
1778   if( mImpl->mEventData->mPreEditFlag &&
1779       ( 0 != mImpl->mEventData->mPreEditLength ) )
1780   {
1781     CharacterIndex offset = mImpl->mEventData->mPrimaryCursorPosition - mImpl->mEventData->mPreEditStartPosition;
1782
1783     removedPrevious = RemoveText( -static_cast<int>(offset), mImpl->mEventData->mPreEditLength );
1784
1785     mImpl->mEventData->mPrimaryCursorPosition = mImpl->mEventData->mPreEditStartPosition;
1786     mImpl->mEventData->mPreEditLength = 0;
1787   }
1788   else
1789   {
1790     // Remove the previous Selection
1791     removedPrevious = RemoveSelectedText();
1792   }
1793
1794   if( !text.empty() )
1795   {
1796     //  Convert text into UTF-32
1797     utf32Characters.Resize( text.size() );
1798
1799     // This is a bit horrible but std::string returns a (signed) char*
1800     const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text.c_str() );
1801
1802     // Transform a text array encoded in utf8 into an array encoded in utf32.
1803     // It returns the actual number of characters.
1804     characterCount = Utf8ToUtf32( utf8, text.size(), utf32Characters.Begin() );
1805     utf32Characters.Resize( characterCount );
1806
1807     DALI_ASSERT_DEBUG( text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length" );
1808     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count() );
1809   }
1810
1811   if( 0u != utf32Characters.Count() ) // Check if Utf8ToUtf32 conversion succeeded
1812   {
1813     // The placeholder text is no longer needed
1814     if( mImpl->IsShowingPlaceholderText() )
1815     {
1816       ResetText();
1817     }
1818
1819     mImpl->ChangeState( EventData::EDITING );
1820
1821     // Handle the IMF (predicitive text) state changes
1822     if( COMMIT == type )
1823     {
1824       // IMF manager is no longer handling key-events
1825       mImpl->ClearPreEditFlag();
1826     }
1827     else // PRE_EDIT
1828     {
1829       if( !mImpl->mEventData->mPreEditFlag )
1830       {
1831         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Entered PreEdit state" );
1832
1833         // Record the start of the pre-edit text
1834         mImpl->mEventData->mPreEditStartPosition = mImpl->mEventData->mPrimaryCursorPosition;
1835       }
1836
1837       mImpl->mEventData->mPreEditLength = utf32Characters.Count();
1838       mImpl->mEventData->mPreEditFlag = true;
1839
1840       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", mImpl->mEventData->mPreEditStartPosition, mImpl->mEventData->mPreEditLength );
1841     }
1842
1843     const Length numberOfCharactersInModel = mImpl->mLogicalModel->mText.Count();
1844
1845     // Restrict new text to fit within Maximum characters setting
1846     Length maxSizeOfNewText = std::min( ( mImpl->mMaximumNumberOfCharacters - numberOfCharactersInModel ), characterCount );
1847     maxLengthReached = ( characterCount > maxSizeOfNewText );
1848
1849     // The cursor position.
1850     CharacterIndex& cursorIndex = mImpl->mEventData->mPrimaryCursorPosition;
1851
1852     // Update the text's style.
1853
1854     // Updates the text style runs by adding characters.
1855     mImpl->mLogicalModel->UpdateTextStyleRuns( cursorIndex, maxSizeOfNewText );
1856
1857     // Get the character index from the cursor index.
1858     const CharacterIndex styleIndex = ( cursorIndex > 0u ) ? cursorIndex - 1u : 0u;
1859
1860     // Retrieve the text's style for the given index.
1861     InputStyle style;
1862     mImpl->mLogicalModel->RetrieveStyle( styleIndex, style );
1863
1864     // Whether to add a new text color run.
1865     const bool addColorRun = style.textColor != mImpl->mEventData->mInputStyle.textColor;
1866
1867     // Whether to add a new font run.
1868     const bool addFontNameRun = style.familyName != mImpl->mEventData->mInputStyle.familyName;
1869     const bool addFontWeightRun = style.weight != mImpl->mEventData->mInputStyle.weight;
1870     const bool addFontWidthRun = style.width != mImpl->mEventData->mInputStyle.width;
1871     const bool addFontSlantRun = style.slant != mImpl->mEventData->mInputStyle.slant;
1872     const bool addFontSizeRun = style.size != mImpl->mEventData->mInputStyle.size;
1873
1874     // Add style runs.
1875     if( addColorRun )
1876     {
1877       const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mColorRuns.Count();
1878       mImpl->mLogicalModel->mColorRuns.Resize( numberOfRuns + 1u );
1879
1880       ColorRun& colorRun = *( mImpl->mLogicalModel->mColorRuns.Begin() + numberOfRuns );
1881       colorRun.color = mImpl->mEventData->mInputStyle.textColor;
1882       colorRun.characterRun.characterIndex = cursorIndex;
1883       colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
1884     }
1885
1886     if( addFontNameRun   ||
1887         addFontWeightRun ||
1888         addFontWidthRun  ||
1889         addFontSlantRun  ||
1890         addFontSizeRun )
1891     {
1892       const VectorBase::SizeType numberOfRuns = mImpl->mLogicalModel->mFontDescriptionRuns.Count();
1893       mImpl->mLogicalModel->mFontDescriptionRuns.Resize( numberOfRuns + 1u );
1894
1895       FontDescriptionRun& fontDescriptionRun = *( mImpl->mLogicalModel->mFontDescriptionRuns.Begin() + numberOfRuns );
1896
1897       if( addFontNameRun )
1898       {
1899         fontDescriptionRun.familyLength = mImpl->mEventData->mInputStyle.familyName.size();
1900         fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
1901         memcpy( fontDescriptionRun.familyName, mImpl->mEventData->mInputStyle.familyName.c_str(), fontDescriptionRun.familyLength );
1902         fontDescriptionRun.familyDefined = true;
1903
1904         // The memory allocated for the font family name is freed when the font description is removed from the logical model.
1905       }
1906
1907       if( addFontWeightRun )
1908       {
1909         fontDescriptionRun.weight = mImpl->mEventData->mInputStyle.weight;
1910         fontDescriptionRun.weightDefined = true;
1911       }
1912
1913       if( addFontWidthRun )
1914       {
1915         fontDescriptionRun.width = mImpl->mEventData->mInputStyle.width;
1916         fontDescriptionRun.widthDefined = true;
1917       }
1918
1919       if( addFontSlantRun )
1920       {
1921         fontDescriptionRun.slant = mImpl->mEventData->mInputStyle.slant;
1922         fontDescriptionRun.slantDefined = true;
1923       }
1924
1925       if( addFontSizeRun )
1926       {
1927         fontDescriptionRun.size = static_cast<PointSize26Dot6>( mImpl->mEventData->mInputStyle.size * 64.f );
1928         fontDescriptionRun.sizeDefined = true;
1929       }
1930
1931       fontDescriptionRun.characterRun.characterIndex = cursorIndex;
1932       fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
1933     }
1934
1935     // Insert at current cursor position.
1936     Vector<Character>& modifyText = mImpl->mLogicalModel->mText;
1937
1938     if( cursorIndex < numberOfCharactersInModel )
1939     {
1940       modifyText.Insert( modifyText.Begin() + cursorIndex, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1941     }
1942     else
1943     {
1944       modifyText.Insert( modifyText.End(), utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText );
1945     }
1946
1947     cursorIndex += maxSizeOfNewText;
1948
1949     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, mImpl->mLogicalModel->mText.Count(), mImpl->mEventData->mPrimaryCursorPosition );
1950   }
1951
1952   if( ( 0u == mImpl->mLogicalModel->mText.Count() ) &&
1953       mImpl->IsPlaceholderAvailable() )
1954   {
1955     // Show place-holder if empty after removing the pre-edit text
1956     ShowPlaceholderText();
1957     mImpl->mEventData->mUpdateCursorPosition = true;
1958     mImpl->ClearPreEditFlag();
1959   }
1960   else if( removedPrevious ||
1961            ( 0 != utf32Characters.Count() ) )
1962   {
1963     // Queue an inserted event
1964     mImpl->QueueModifyEvent( ModifyEvent::TEXT_INSERTED );
1965   }
1966
1967   if( maxLengthReached )
1968   {
1969     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", mImpl->mLogicalModel->mText.Count() );
1970
1971     mImpl->ResetImfManager();
1972
1973     // Do this last since it provides callbacks into application code
1974     mImpl->mControlInterface.MaxLengthReached();
1975   }
1976 }
1977
1978 bool Controller::RemoveSelectedText()
1979 {
1980   bool textRemoved( false );
1981
1982   if( EventData::SELECTING == mImpl->mEventData->mState )
1983   {
1984     std::string removedString;
1985     mImpl->RetrieveSelection( removedString, true );
1986
1987     if( !removedString.empty() )
1988     {
1989       textRemoved = true;
1990       mImpl->ChangeState( EventData::EDITING );
1991     }
1992   }
1993
1994   return textRemoved;
1995 }
1996
1997 void Controller::TapEvent( unsigned int tapCount, float x, float y )
1998 {
1999   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected TapEvent" );
2000
2001   if( NULL != mImpl->mEventData )
2002   {
2003     DALI_LOG_INFO( gLogFilter, Debug::Concise, "TapEvent state:%d \n", mImpl->mEventData->mState );
2004
2005     if( 1u == tapCount )
2006     {
2007       // This is to avoid unnecessary relayouts when tapping an empty text-field
2008       bool relayoutNeeded( false );
2009
2010       if( ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) ||
2011           ( EventData::EDITING_WITH_PASTE_POPUP == mImpl->mEventData->mState ) )
2012       {
2013         mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE);  // If Popup shown hide it here so can be shown again if required.
2014       }
2015
2016       if( mImpl->IsShowingRealText() && ( EventData::INACTIVE != mImpl->mEventData->mState ) )
2017       {
2018         // Already in an active state so show a popup
2019         if( !mImpl->IsClipboardEmpty() )
2020         {
2021           // Shows Paste popup but could show full popup with Selection options. ( EDITING_WITH_POPUP )
2022           mImpl->ChangeState( EventData::EDITING_WITH_PASTE_POPUP );
2023         }
2024         else
2025         {
2026           mImpl->ChangeState( EventData::EDITING_WITH_GRAB_HANDLE );
2027         }
2028         relayoutNeeded = true;
2029       }
2030       else
2031       {
2032         if( mImpl->IsShowingPlaceholderText() && !mImpl->IsFocusedPlaceholderAvailable() )
2033         {
2034           // Hide placeholder text
2035           ResetText();
2036         }
2037
2038         if( EventData::INACTIVE == mImpl->mEventData->mState )
2039         {
2040           mImpl->ChangeState( EventData::EDITING );
2041         }
2042         else if( !mImpl->IsClipboardEmpty() )
2043         {
2044           mImpl->ChangeState( EventData::EDITING_WITH_POPUP );
2045         }
2046         relayoutNeeded = true;
2047       }
2048
2049       // Handles & cursors must be repositioned after Relayout() i.e. after the Model has been updated
2050       if( relayoutNeeded )
2051       {
2052         Event event( Event::TAP_EVENT );
2053         event.p1.mUint = tapCount;
2054         event.p2.mFloat = x;
2055         event.p3.mFloat = y;
2056         mImpl->mEventData->mEventQueue.push_back( event );
2057
2058         mImpl->RequestRelayout();
2059       }
2060     }
2061     else if( 2u == tapCount )
2062     {
2063       if( mImpl->mEventData->mSelectionEnabled &&
2064           mImpl->IsShowingRealText() )
2065       {
2066         SelectEvent( x, y, false );
2067       }
2068     }
2069   }
2070
2071   // Reset keyboard as tap event has occurred.
2072   mImpl->ResetImfManager();
2073 }
2074
2075 void Controller::PanEvent( Gesture::State state, const Vector2& displacement )
2076         // Show cursor and grabhandle on first tap, this matches the behaviour of tapping when already editing
2077 {
2078   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected PanEvent" );
2079
2080   if( NULL != mImpl->mEventData )
2081   {
2082     Event event( Event::PAN_EVENT );
2083     event.p1.mInt = state;
2084     event.p2.mFloat = displacement.x;
2085     event.p3.mFloat = displacement.y;
2086     mImpl->mEventData->mEventQueue.push_back( event );
2087
2088     mImpl->RequestRelayout();
2089   }
2090 }
2091
2092 void Controller::LongPressEvent( Gesture::State state, float x, float y  )
2093 {
2094   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected LongPressEvent" );
2095
2096   if( ( state == Gesture::Started ) &&
2097       ( NULL != mImpl->mEventData ) )
2098   {
2099     if( !mImpl->IsShowingRealText() )
2100     {
2101       Event event( Event::LONG_PRESS_EVENT );
2102       event.p1.mInt = state;
2103       mImpl->mEventData->mEventQueue.push_back( event );
2104       mImpl->RequestRelayout();
2105     }
2106     else
2107     {
2108       // The 1st long-press on inactive text-field is treated as tap
2109       if( EventData::INACTIVE == mImpl->mEventData->mState )
2110       {
2111         mImpl->ChangeState( EventData::EDITING );
2112
2113         Event event( Event::TAP_EVENT );
2114         event.p1.mUint = 1;
2115         event.p2.mFloat = x;
2116         event.p3.mFloat = y;
2117         mImpl->mEventData->mEventQueue.push_back( event );
2118
2119         mImpl->RequestRelayout();
2120       }
2121       else
2122       {
2123         // Reset the imf manger to commit the pre-edit before selecting the text.
2124         mImpl->ResetImfManager();
2125
2126         SelectEvent( x, y, false );
2127       }
2128     }
2129   }
2130 }
2131
2132 void Controller::SelectEvent( float x, float y, bool selectAll )
2133 {
2134   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::SelectEvent\n" );
2135
2136   if( NULL != mImpl->mEventData )
2137   {
2138     mImpl->ChangeState( EventData::SELECTING );
2139
2140     if( selectAll )
2141     {
2142       Event event( Event::SELECT_ALL );
2143       mImpl->mEventData->mEventQueue.push_back( event );
2144     }
2145     else
2146     {
2147       Event event( Event::SELECT );
2148       event.p2.mFloat = x;
2149       event.p3.mFloat = y;
2150       mImpl->mEventData->mEventQueue.push_back( event );
2151     }
2152
2153     mImpl->RequestRelayout();
2154   }
2155 }
2156
2157 void Controller::GetTargetSize( Vector2& targetSize )
2158 {
2159   targetSize = mImpl->mVisualModel->mControlSize;
2160 }
2161
2162 void Controller::AddDecoration( Actor& actor, bool needsClipping )
2163 {
2164   mImpl->mControlInterface.AddDecoration( actor, needsClipping );
2165 }
2166
2167 void Controller::DecorationEvent( HandleType handleType, HandleState state, float x, float y )
2168 {
2169   DALI_ASSERT_DEBUG( mImpl->mEventData && "Unexpected DecorationEvent" );
2170
2171   if( NULL != mImpl->mEventData )
2172   {
2173     switch( handleType )
2174     {
2175       case GRAB_HANDLE:
2176       {
2177         Event event( Event::GRAB_HANDLE_EVENT );
2178         event.p1.mUint  = state;
2179         event.p2.mFloat = x;
2180         event.p3.mFloat = y;
2181
2182         mImpl->mEventData->mEventQueue.push_back( event );
2183         break;
2184       }
2185       case LEFT_SELECTION_HANDLE:
2186       {
2187         Event event( Event::LEFT_SELECTION_HANDLE_EVENT );
2188         event.p1.mUint  = state;
2189         event.p2.mFloat = x;
2190         event.p3.mFloat = y;
2191
2192         mImpl->mEventData->mEventQueue.push_back( event );
2193         break;
2194       }
2195       case RIGHT_SELECTION_HANDLE:
2196       {
2197         Event event( Event::RIGHT_SELECTION_HANDLE_EVENT );
2198         event.p1.mUint  = state;
2199         event.p2.mFloat = x;
2200         event.p3.mFloat = y;
2201
2202         mImpl->mEventData->mEventQueue.push_back( event );
2203         break;
2204       }
2205       case LEFT_SELECTION_HANDLE_MARKER:
2206       case RIGHT_SELECTION_HANDLE_MARKER:
2207       {
2208         // Markers do not move the handles.
2209         break;
2210       }
2211       case HANDLE_TYPE_COUNT:
2212       {
2213         DALI_ASSERT_DEBUG( !"Controller::HandleEvent. Unexpected handle type" );
2214       }
2215     }
2216
2217     mImpl->RequestRelayout();
2218   }
2219 }
2220
2221 void Controller::PasteText( const std::string& stringToPaste )
2222 {
2223   InsertText( stringToPaste, Text::Controller::COMMIT );
2224   mImpl->ChangeState( EventData::EDITING );
2225   mImpl->RequestRelayout();
2226
2227   // Do this last since it provides callbacks into application code
2228   mImpl->mControlInterface.TextChanged();
2229 }
2230
2231 void Controller::PasteClipboardItemEvent()
2232 {
2233   // Retrieve the clipboard contents first
2234   ClipboardEventNotifier notifier( ClipboardEventNotifier::Get() );
2235   std::string stringToPaste( notifier.GetContent() );
2236
2237   // Commit the current pre-edit text; the contents of the clipboard should be appended
2238   mImpl->ResetImfManager();
2239
2240   // Paste
2241   PasteText( stringToPaste );
2242 }
2243
2244 void Controller::TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button )
2245 {
2246   if( NULL == mImpl->mEventData )
2247   {
2248     return;
2249   }
2250
2251   switch( button )
2252   {
2253     case Toolkit::TextSelectionPopup::CUT:
2254     {
2255       mImpl->SendSelectionToClipboard( true ); // Synchronous call to modify text
2256       mImpl->mOperationsPending = ALL_OPERATIONS;
2257
2258       // This is to reset the virtual keyboard to Upper-case
2259       if( 0u == mImpl->mLogicalModel->mText.Count() )
2260       {
2261         NotifyImfManager();
2262       }
2263
2264       if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2265           !mImpl->IsPlaceholderAvailable() )
2266       {
2267         mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2268       }
2269       else
2270       {
2271         ShowPlaceholderText();
2272         mImpl->mEventData->mUpdateCursorPosition = true;
2273       }
2274       mImpl->RequestRelayout();
2275       mImpl->mControlInterface.TextChanged();
2276       break;
2277     }
2278     case Toolkit::TextSelectionPopup::COPY:
2279     {
2280       mImpl->SendSelectionToClipboard( false ); // Text not modified
2281       mImpl->RequestRelayout(); // Handles, Selection Highlight, Popup
2282       break;
2283     }
2284     case Toolkit::TextSelectionPopup::PASTE:
2285     {
2286       std::string stringToPaste("");
2287       mImpl->GetTextFromClipboard( 0, stringToPaste ); // Paste latest item from system clipboard
2288       PasteText( stringToPaste );
2289       break;
2290     }
2291     case Toolkit::TextSelectionPopup::SELECT:
2292     {
2293       const Vector2& currentCursorPosition = mImpl->mEventData->mDecorator->GetPosition( PRIMARY_CURSOR );
2294
2295       if( mImpl->mEventData->mSelectionEnabled )
2296       {
2297         // Creates a SELECT event.
2298         SelectEvent( currentCursorPosition.x, currentCursorPosition.y, false );
2299       }
2300       break;
2301     }
2302     case Toolkit::TextSelectionPopup::SELECT_ALL:
2303     {
2304       // Creates a SELECT_ALL event
2305       SelectEvent( 0.f, 0.f, true );
2306       break;
2307     }
2308     case Toolkit::TextSelectionPopup::CLIPBOARD:
2309     {
2310       mImpl->ShowClipboard();
2311       break;
2312     }
2313     case Toolkit::TextSelectionPopup::NONE:
2314     {
2315       // Nothing to do.
2316       break;
2317     }
2318   }
2319 }
2320
2321 ImfManager::ImfCallbackData Controller::OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent )
2322 {
2323   bool update = false;
2324   bool requestRelayout = false;
2325
2326   std::string text;
2327   unsigned int cursorPosition = 0u;
2328
2329   switch( imfEvent.eventName )
2330   {
2331     case ImfManager::COMMIT:
2332     {
2333       InsertText( imfEvent.predictiveString, Text::Controller::COMMIT );
2334       update = true;
2335       requestRelayout = true;
2336       break;
2337     }
2338     case ImfManager::PREEDIT:
2339     {
2340       InsertText( imfEvent.predictiveString, Text::Controller::PRE_EDIT );
2341       update = true;
2342       requestRelayout = true;
2343       break;
2344     }
2345     case ImfManager::DELETESURROUNDING:
2346     {
2347       update = RemoveText( imfEvent.cursorOffset, imfEvent.numberOfChars );
2348
2349       if( update )
2350       {
2351         if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2352             !mImpl->IsPlaceholderAvailable() )
2353         {
2354           mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2355         }
2356         else
2357         {
2358           ShowPlaceholderText();
2359           mImpl->mEventData->mUpdateCursorPosition = true;
2360         }
2361       }
2362       requestRelayout = true;
2363       break;
2364     }
2365     case ImfManager::GETSURROUNDING:
2366     {
2367       GetText( text );
2368       cursorPosition = GetLogicalCursorPosition();
2369
2370       imfManager.SetSurroundingText( text );
2371       imfManager.SetCursorPosition( cursorPosition );
2372       break;
2373     }
2374     case ImfManager::VOID:
2375     {
2376       // do nothing
2377       break;
2378     }
2379   } // end switch
2380
2381   if( ImfManager::GETSURROUNDING != imfEvent.eventName )
2382   {
2383     GetText( text );
2384     cursorPosition = GetLogicalCursorPosition();
2385   }
2386
2387   if( requestRelayout )
2388   {
2389     mImpl->mOperationsPending = ALL_OPERATIONS;
2390     mImpl->RequestRelayout();
2391
2392     // Do this last since it provides callbacks into application code
2393     mImpl->mControlInterface.TextChanged();
2394   }
2395
2396   ImfManager::ImfCallbackData callbackData( update, cursorPosition, text, false );
2397
2398   return callbackData;
2399 }
2400
2401 Controller::~Controller()
2402 {
2403   delete mImpl;
2404 }
2405
2406 bool Controller::BackspaceKeyEvent()
2407 {
2408   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE\n", this );
2409
2410   bool removed = false;
2411
2412   if( NULL == mImpl->mEventData )
2413   {
2414     return removed;
2415   }
2416
2417   // IMF manager is no longer handling key-events
2418   mImpl->ClearPreEditFlag();
2419
2420   if( EventData::SELECTING == mImpl->mEventData->mState )
2421   {
2422     removed = RemoveSelectedText();
2423   }
2424   else if( mImpl->mEventData->mPrimaryCursorPosition > 0 )
2425   {
2426     // Remove the character before the current cursor position
2427     removed = RemoveText( -1, 1 );
2428   }
2429
2430   if( removed )
2431   {
2432     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Controller::KeyEvent %p DALI_KEY_BACKSPACE RemovedText\n", this );
2433     // Notifiy the IMF manager after text changed
2434     // Automatic  Upper-case and restarting prediction on an existing word require this.
2435     NotifyImfManager();
2436
2437     if( ( 0u != mImpl->mLogicalModel->mText.Count() ) ||
2438         !mImpl->IsPlaceholderAvailable() )
2439     {
2440       mImpl->QueueModifyEvent( ModifyEvent::TEXT_DELETED );
2441     }
2442     else
2443     {
2444       ShowPlaceholderText();
2445       mImpl->mEventData->mUpdateCursorPosition = true;
2446     }
2447   }
2448
2449   return removed;
2450 }
2451
2452 void Controller::NotifyImfManager()
2453 {
2454   if( NULL != mImpl->mEventData )
2455   {
2456     if( mImpl->mEventData->mImfManager )
2457     {
2458       // Notifying IMF of a cursor change triggers a surrounding text request so updating it now.
2459       std::string text;
2460       GetText( text );
2461       mImpl->mEventData->mImfManager.SetSurroundingText( text );
2462
2463       mImpl->mEventData->mImfManager.SetCursorPosition( GetLogicalCursorPosition() );
2464       mImpl->mEventData->mImfManager.NotifyCursorPosition();
2465     }
2466   }
2467 }
2468
2469 void Controller::ShowPlaceholderText()
2470 {
2471   if( mImpl->IsPlaceholderAvailable() )
2472   {
2473     DALI_ASSERT_DEBUG( mImpl->mEventData && "No placeholder text available" );
2474
2475     if( NULL == mImpl->mEventData )
2476     {
2477       return;
2478     }
2479
2480     mImpl->mEventData->mIsShowingPlaceholderText = true;
2481
2482     // Disable handles when showing place-holder text
2483     mImpl->mEventData->mDecorator->SetHandleActive( GRAB_HANDLE, false );
2484     mImpl->mEventData->mDecorator->SetHandleActive( LEFT_SELECTION_HANDLE, false );
2485     mImpl->mEventData->mDecorator->SetHandleActive( RIGHT_SELECTION_HANDLE, false );
2486
2487     const char* text( NULL );
2488     size_t size( 0 );
2489
2490     // TODO - Switch placeholder text styles when changing state
2491     if( ( EventData::INACTIVE != mImpl->mEventData->mState ) &&
2492         ( 0u != mImpl->mEventData->mPlaceholderTextActive.c_str() ) )
2493     {
2494       text = mImpl->mEventData->mPlaceholderTextActive.c_str();
2495       size = mImpl->mEventData->mPlaceholderTextActive.size();
2496     }
2497     else
2498     {
2499       text = mImpl->mEventData->mPlaceholderTextInactive.c_str();
2500       size = mImpl->mEventData->mPlaceholderTextInactive.size();
2501     }
2502
2503     // Reset model for showing placeholder.
2504     mImpl->mLogicalModel->mText.Clear();
2505     ClearModelData();
2506     mImpl->mVisualModel->SetTextColor( mImpl->mEventData->mPlaceholderTextColor );
2507
2508     // Convert text into UTF-32
2509     Vector<Character>& utf32Characters = mImpl->mLogicalModel->mText;
2510     utf32Characters.Resize( size );
2511
2512     // This is a bit horrible but std::string returns a (signed) char*
2513     const uint8_t* utf8 = reinterpret_cast<const uint8_t*>( text );
2514
2515     // Transform a text array encoded in utf8 into an array encoded in utf32.
2516     // It returns the actual number of characters.
2517     Length characterCount = Utf8ToUtf32( utf8, size, utf32Characters.Begin() );
2518     utf32Characters.Resize( characterCount );
2519
2520     // Reset the cursor position
2521     mImpl->mEventData->mPrimaryCursorPosition = 0;
2522
2523     // The natural size needs to be re-calculated.
2524     mImpl->mRecalculateNaturalSize = true;
2525
2526     // Apply modifications to the model
2527     mImpl->mOperationsPending = ALL_OPERATIONS;
2528
2529     // Update the rest of the model during size negotiation
2530     mImpl->QueueModifyEvent( ModifyEvent::TEXT_REPLACED );
2531   }
2532 }
2533
2534 void Controller::ClearModelData()
2535 {
2536   // n.b. This does not Clear the mText from mLogicalModel
2537   mImpl->mLogicalModel->mScriptRuns.Clear();
2538   mImpl->mLogicalModel->mFontRuns.Clear();
2539   mImpl->mLogicalModel->mLineBreakInfo.Clear();
2540   mImpl->mLogicalModel->mWordBreakInfo.Clear();
2541   mImpl->mLogicalModel->mBidirectionalParagraphInfo.Clear();
2542   mImpl->mLogicalModel->mCharacterDirections.Clear();
2543   mImpl->mLogicalModel->mBidirectionalLineInfo.Clear();
2544   mImpl->mLogicalModel->mLogicalToVisualMap.Clear();
2545   mImpl->mLogicalModel->mVisualToLogicalMap.Clear();
2546   mImpl->mVisualModel->mGlyphs.Clear();
2547   mImpl->mVisualModel->mGlyphsToCharacters.Clear();
2548   mImpl->mVisualModel->mCharactersToGlyph.Clear();
2549   mImpl->mVisualModel->mCharactersPerGlyph.Clear();
2550   mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
2551   mImpl->mVisualModel->mGlyphPositions.Clear();
2552   mImpl->mVisualModel->mLines.Clear();
2553   mImpl->mVisualModel->mColorRuns.Clear();
2554   mImpl->mVisualModel->ClearCaches();
2555 }
2556
2557 void Controller::ClearFontData()
2558 {
2559   mImpl->mFontDefaults->mFontId = 0u; // Remove old font ID
2560   mImpl->mLogicalModel->mFontRuns.Clear();
2561   mImpl->mVisualModel->mGlyphs.Clear();
2562   mImpl->mVisualModel->mGlyphsToCharacters.Clear();
2563   mImpl->mVisualModel->mCharactersToGlyph.Clear();
2564   mImpl->mVisualModel->mCharactersPerGlyph.Clear();
2565   mImpl->mVisualModel->mGlyphsPerCharacter.Clear();
2566   mImpl->mVisualModel->mGlyphPositions.Clear();
2567   mImpl->mVisualModel->mLines.Clear();
2568   mImpl->mVisualModel->ClearCaches();
2569 }
2570
2571 void Controller::ClearStyleData()
2572 {
2573   mImpl->mLogicalModel->mColorRuns.Clear();
2574   mImpl->mLogicalModel->ClearFontDescriptionRuns();
2575 }
2576
2577 Controller::Controller( ControlInterface& controlInterface )
2578 : mImpl( NULL )
2579 {
2580   mImpl = new Controller::Impl( controlInterface );
2581 }
2582
2583 } // namespace Text
2584
2585 } // namespace Toolkit
2586
2587 } // namespace Dali