License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / public-api / markup-processor / markup-processor.cpp
1 /*
2  * Copyright (c) 2014 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 // HEADER INCLUDE
19 #include <dali-toolkit/public-api/markup-processor/markup-processor.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/text/text.h>
23
24
25 // EXTERNAL INCLUDES
26 #include <stack>
27 #include <sstream>
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace MarkupProcessor
36 {
37
38 namespace
39 {
40 const std::string WEB_COLOR_TOKEN( "#" );
41 const std::string HEX_COLOR_TOKEN( "0x" );
42 const std::string ALPHA_ONE( "FF" );
43
44 const std::string BLACK_COLOR( "black" );
45 const std::string WHITE_COLOR( "white" );
46 const std::string RED_COLOR( "red" );
47 const std::string GREEN_COLOR( "green" );
48 const std::string BLUE_COLOR( "blue" );
49 const std::string YELLOW_COLOR( "yellow" );
50 const std::string MAGENTA_COLOR( "magenta" );
51 const std::string CYAN_COLOR( "cyan" );
52 const std::string TRANSPARENT_COLOR( "transparent" );
53
54 const std::string XHTML_B_TAG("b");
55 const std::string XHTML_I_TAG("i");
56 const std::string XHTML_U_TAG("u");
57 const std::string XHTML_BR_TAG("br");
58 const std::string XHTML_FONT_TAG("font");
59 const std::string XHTML_SHADOW_TAG("shadow");
60 const std::string XHTML_GLOW_TAG("glow");
61 const std::string XHTML_OUTLINE_TAG("outline");
62 const std::string XHTML_SMOOTH_EDGE_TAG("smooth");
63 const std::string XHTML_SIZE_PROPERTY("size");
64 const std::string XHTML_COLOR_PROPERTY("color");
65 const std::string XHTML_FACE_PROPERTY("face");
66 const std::string XHTML_STYLE_PROPERTY("style");
67 const std::string XHTML_PARAM_PROPERTY("param");
68 const std::string XHTML_PARAM_X_PROPERTY("paramx");
69 const std::string XHTML_PARAM_Y_PROPERTY("paramy");
70
71 const char LESS_THAN( '<' );
72 const char GREATER_THAN( '>' );
73 const char EQUAL( '=' );
74 const char QUOTATION_MARK( '\'');
75 const char LINE_SEPARATOR_CR( 0x0D ); // Carriage return character  CR
76 const char LINE_SEPARATOR_LF( 0x0A ); // New line character         LF
77 const char SLASH( '/' );
78 const char BACK_SLASH( '\\' );
79
80 const std::string LINE_SEPARATOR_LF_STRING(1 , LINE_SEPARATOR_LF); ///< a string with 1 line separator character
81 /**
82  * Stores a property pair: name, value.
83  */
84 struct Property
85 {
86   Property( const std::string& n, const std::string& v )
87   : name( n ),
88     value( v )
89   {}
90
91   std::string name;
92   std::string value;
93 };
94
95
96 /**
97  * Compare if two strings are equal.
98  *
99  * The comparison is case insensitive.
100  *
101  * @param[in] string1 First of the two strings to be compared.
102  * @param[in] string2 Second of the strings to be compared.
103  *
104  * @return \e true if both strings are equal (case insensitive).
105  */
106 bool CaseInsensitiveComparison( const std::string& string1, const std::string& string2 )
107 {
108   const std::size_t stringSize = string1.size();
109   if( stringSize != string2.size() )
110   {
111     // Early return. Strings have different sizes.
112     return false;
113   }
114
115   bool equal = true;
116   for( std::size_t index = 0; equal && ( index < stringSize ); ++index )
117   {
118     if( std::toupper( *( string1.begin() + index ) ) != std::toupper( *( string2.begin() + index ) ) )
119     {
120       equal = false;
121     }
122   }
123
124   return equal;
125 }
126
127 /**
128  * Converts a string into a float value.
129  * @param[in] floatStr A float packed inside a string.
130  * @return The float value.
131  */
132 float StringToFloat( const std::string& floatStr )
133 {
134   float ret = 0.f;
135
136   std::istringstream( floatStr ) >> ret;
137
138   return ret;
139 }
140
141 /**
142  * Converts a float into a string.
143  * @param[in] value. A float.
144  * @return The float packed into a string.
145  */
146 std::string FloatToString( float value )
147 {
148   std::ostringstream stream;
149   stream << value;
150
151   return stream.str();
152 }
153
154 /**
155  * Converts a string into an hexadecimal unsigned int.
156  * @param[in] uintStr An hexadecimal unsigned int packed inside a string.
157  * @return The hexadecimal value.
158  */
159 unsigned int StringToHex( const std::string& uintStr )
160 {
161   unsigned int ret = 0;
162
163   std::string str( uintStr );
164   if( uintStr.size() <= 8 )
165   {
166     str.insert( 2, std::string( "ff" ) );
167   }
168
169   std::istringstream( str ) >> std::hex >> ret;
170
171   return ret;
172 }
173
174 /**
175  * Converts an hexadecimal value into a string.
176  * @param[in] value. An hexadecimal value.
177  * @return The hexadecimal value packed inside a string.
178  */
179 std::string HexToString( unsigned int value )
180 {
181   std::ostringstream stream;
182   stream << std::hex << value;
183   return stream.str();
184 }
185
186 /**
187  * Converts an ARGB color packed in 4 byte unsigned int into a Vector4 color used in Dali.
188  * @param[in] color An ARGB color packed in an unsigned int.
189  * @param[out] retColor A Vector4 with the converted color.
190  */
191 void UintColorToVector4( unsigned int color, Vector4& retColor )
192 {
193   retColor.a = static_cast<float>( ( color & 0xFF000000 ) >> 24 ) / 255.f;
194   retColor.r = static_cast<float>( ( color & 0x00FF0000 ) >> 16 ) / 255.f;
195   retColor.g = static_cast<float>( ( color & 0x0000FF00 ) >> 8 ) / 255.f;
196   retColor.b = static_cast<float>( color & 0x000000FF ) / 255.f;
197 }
198
199 /**
200  * Converts an ARGB Vector4 color into a 4 byte packed inside an unsigned int.
201  * @param[in] color A Vector4 with an ARGB color.
202  * @return An unsigned int with the converted color.
203  */
204 unsigned int Vector4ColorToUint( const Vector4& color )
205 {
206   unsigned int retColor = 0;
207
208   retColor = static_cast<unsigned int>( color.a * 255.f ) << 24;
209   retColor += static_cast<unsigned int>( color.r * 255.f ) << 16;
210   retColor += static_cast<unsigned int>( color.g * 255.f ) << 8;
211   retColor += static_cast<unsigned int>( color.b * 255.f );
212
213   return retColor;
214 }
215
216 /**
217  * Converts a color packed inside a string into an ARGB Vector4 color.
218  * The string color could be in hexadecimal ( 0xFF0000FF ), webcolor ( #0000FF or #00F ) or some constant values:
219  * black, white, red, green, blue, yellow, magenta, cyan, transparent.
220  * @param[in] colorStr A color packed inside a string..
221  * @param[out] retColor A color packed inside a Vector4.
222  */
223 void ColorStringToVector4( const std::string& colorStr, Vector4& retColor )
224 {
225   std::string subStr1 = colorStr.substr( 0, 1 );
226   std::string subStr2 = colorStr.substr( 0, 2 );
227
228   if( WEB_COLOR_TOKEN == subStr1 )
229   {
230     std::string webColor( colorStr.begin() + 1, colorStr.end() );
231     if( 3 == webColor.size() )                      // 3 component web color #F00 (red)
232     {
233       webColor.insert( 2, &( webColor[2] ), 1 );
234       webColor.insert( 1, &( webColor[1] ), 1 );
235       webColor.insert( 0, &( webColor[0] ), 1 );
236       webColor.insert( 0, ALPHA_ONE );
237     }
238     else if( 6 == webColor.size() )                 // 6 component web color #FF0000 (red)
239     {
240       webColor.insert( 0, ALPHA_ONE );
241     }
242     webColor.insert( 0, HEX_COLOR_TOKEN );
243
244     UintColorToVector4( StringToHex( webColor ), retColor );
245   }
246   else if( CaseInsensitiveComparison( HEX_COLOR_TOKEN, subStr2 ) )
247   {
248     UintColorToVector4( StringToHex( colorStr ), retColor );
249   }
250   else if( CaseInsensitiveComparison( BLACK_COLOR, colorStr ) )
251   {
252     retColor = Color::BLACK;
253   }
254   else if( CaseInsensitiveComparison( WHITE_COLOR, colorStr ) )
255   {
256     retColor = Color::WHITE;
257   }
258   else if( CaseInsensitiveComparison( RED_COLOR, colorStr ) )
259   {
260     retColor = Color::RED;
261   }
262   else if( CaseInsensitiveComparison( GREEN_COLOR, colorStr ) )
263   {
264     retColor = Color::GREEN;
265   }
266   else if( CaseInsensitiveComparison( BLUE_COLOR, colorStr ) )
267   {
268     retColor = Color::BLUE;
269   }
270   else if( CaseInsensitiveComparison( YELLOW_COLOR, colorStr ) )
271   {
272     retColor = Color::YELLOW;
273   }
274   else if( CaseInsensitiveComparison( MAGENTA_COLOR, colorStr ) )
275   {
276     retColor = Color::MAGENTA;
277   }
278   else if( CaseInsensitiveComparison( CYAN_COLOR, colorStr ) )
279   {
280     retColor = Color::CYAN;
281   }
282   else if( CaseInsensitiveComparison( TRANSPARENT_COLOR, colorStr ) )
283   {
284     retColor = Color::TRANSPARENT;
285   }
286 }
287
288 /**
289  * Converts a color packed inside a Vector4 into a string.
290  * The string color could be in hexadecimal ( 0xFF0000FF ), webcolor ( #0000FF or #00F ) or some constant values:
291  * black, white, red, green, blue, yellow, magenta, cyan, transparent.
292  * @param[in] color A color packed inside a Vector4
293  * @return The color packed inside a string.
294  */
295 std::string Vector4ToColorString( const Vector4& color )
296 {
297   std::string colorStr;
298
299   if( Color::BLACK == color )
300   {
301     colorStr = BLACK_COLOR;
302   }
303   else if( Color::WHITE == color )
304   {
305     colorStr = WHITE_COLOR;
306   }
307   else if( Color::RED == color )
308   {
309     colorStr = RED_COLOR;
310   }
311   else if( Color::GREEN == color )
312   {
313     colorStr = GREEN_COLOR;
314   }
315   else if( Color::BLUE == color )
316   {
317     colorStr = BLUE_COLOR;
318   }
319   else if( Color::YELLOW == color )
320   {
321     colorStr = YELLOW_COLOR;
322   }
323   else if( Color::MAGENTA == color )
324   {
325     colorStr = MAGENTA_COLOR;
326   }
327   else if( Color::CYAN == color )
328   {
329     colorStr = CYAN_COLOR;
330   }
331   else if( Color::TRANSPARENT == color )
332   {
333     colorStr = TRANSPARENT_COLOR;
334   }
335   else
336   {
337     colorStr = HEX_COLOR_TOKEN + HexToString( Vector4ColorToUint( color ) );
338   }
339
340   return colorStr;
341 }
342
343 /**
344  * Skips any unnecessary white space.
345  * @param[in,out] it Iterator pointing to the current character of the markup string which is being parsed.
346  * @param[in] endIt Iterator pointing to the end of the markup string.
347  */
348 void SkipWhiteSpace( std::string::const_iterator& it, const std::string::const_iterator& endIt )
349 {
350   bool found = false;
351   for( ; ( !found ) && ( it != endIt ); ++it )
352   {
353     if( !isspace( *it ) )
354     {
355       found = true;
356       --it;
357     }
358   }
359 }
360
361 /**
362  * Adds a line separator 'LF' character to the given styled text array.
363  * @param[in] style The current style.
364  * @param[in,out] styledTextArray The given styled text array.
365  */
366 void AddNewLineChar( const TextStyle& style, StyledTextArray& styledTextArray )
367 {
368   const Text text( LINE_SEPARATOR_LF_STRING );
369   styledTextArray.push_back( StyledText( text, style ) );
370 }
371
372 /**
373  * Adds text to the given styled text array.
374  * It splits the text in characters.
375  * @param[in] textToBeStored The text to be stored.
376  * @param[in] styleToBeStored The current style.
377  * @param[in,out] styledTextArray The given styled text array.
378  */
379 void AddText( const std::string& textToBeStored, const TextStyle& styleToBeStored, StyledTextArray& styledTextArray )
380 {
381   const Text text( textToBeStored );
382   for( size_t i = 0, length = text.GetLength(); i < length; ++i )
383   {
384     styledTextArray.push_back( StyledText( Text( text[i] ), styleToBeStored ) );
385   }
386 }
387
388 /**
389  * Splits the tag string into the tag name and its properties.
390  * @param[in] tag The tag string with its pairs property, value.
391  * @param[out] tagName The name of the tag.
392  * @param[out] properties Vector of properties.
393  */
394 void ParseProperties( const std::string& tag, std::string& tagName, std::vector<Property>& properties )
395 {
396   // Find first the tag name.
397   bool found = false;
398   bool isQuotationOpen = false;
399   std::string::const_iterator it, endIt;
400   for( it = tag.begin(), endIt = tag.end(); ( !found ) && ( it != endIt ); ++it )
401   {
402     const char character( *it );
403     if( !isspace( character ) )
404     {
405       tagName += character;
406     }
407     else
408     {
409       found = true;
410     }
411   }
412   SkipWhiteSpace( it, endIt );
413
414   // Find the properties.
415   std::string name;
416   std::string value;
417   bool addToNameValue = true;
418   for( ; it != endIt; ++it )
419   {
420     const char character( *it );
421     if( isspace( character ) && !isQuotationOpen )
422     {
423       if( !name.empty() && !value.empty() )
424       {
425         // Every time a white space is found, a new property is created and stored in the properties vector.
426         properties.push_back( Property( name, value ) );
427         name .clear();
428         value.clear();
429         addToNameValue = true; // next read characters will be added to the name.
430       }
431     }
432     else if( EQUAL == character ) // '='
433     {
434       addToNameValue = false; // next read characters will be added to the value.
435       SkipWhiteSpace( it, endIt );
436     }
437     else if( QUOTATION_MARK == character ) // '\''
438     {
439       // Do not add quotation marks to neither name nor value.
440       isQuotationOpen = !isQuotationOpen;
441     }
442     else
443     {
444       // Adds characters to the name or the value.
445       if( addToNameValue )
446       {
447         name += character;
448       }
449       else
450       {
451         value += character;
452       }
453     }
454   }
455   if( !name.empty() && !value.empty() )
456   {
457     // Checks if the last property needs to be added.
458     properties.push_back( Property( name, value ) );
459   }
460 }
461
462 /**
463  * It parses a tag and its properties if the given iterator \e it is pointing at a tag beginning.
464  * @param[in,out] it Iterator pointing to the current character of the markup string which is being parsed.
465  * @param[in] endIt Iterator pointing to the end of the markup string.
466  * @param[out] tag Name of the tag.
467  * @param[out] isEndTag Whether the tag is and end tag i.e </tag_name> or not.
468  * @param[out] properties The vector with tag properties.
469  * @return \e true if the iterator \e it is pointing a markup tag. Otherwise \e false.
470  */
471 bool IsTag( std::string::const_iterator& it, const std::string::const_iterator& endIt, std::string& tag, bool& isEndTag, std::vector<Property>& properties )
472 {
473   bool isTag = false;
474   bool isQuotationOpen = false;
475   bool propertiesFound = false;
476   std::string tagString;
477
478   const char character( *it );
479   if( LESS_THAN == character ) // '<'
480   {
481     // if the iterator is pointing to a '<' character, then check if it's a markup tag is needed.
482     ++it;
483     if( it != endIt )
484     {
485       SkipWhiteSpace( it, endIt );
486
487       for( ; ( !isTag ) && ( it != endIt ); ++it )
488       {
489         const char character( *it );
490
491         if( SLASH == character ) // '/'
492         {
493           // if the tag has a '/' then it's an end or empty tag.
494           isEndTag = true;
495
496           if( ( it + 1 != endIt ) && ( isspace( *( it + 1 ) ) )  && ( !isQuotationOpen ) )
497           {
498             ++it;
499             SkipWhiteSpace( it, endIt );
500             --it;
501           }
502         }
503         else if( GREATER_THAN == character ) // '>'
504         {
505           isTag = true;
506         }
507         else if(QUOTATION_MARK == character)
508         {
509           isQuotationOpen = !isQuotationOpen;
510           tagString += character;
511         }
512         else if( isspace( character ) ) // ' '
513         {
514           // If the tag contains white spaces then it may have properties.
515           if ( !isQuotationOpen )
516           {
517             propertiesFound = true;
518           }
519           tagString += character;
520         }
521         else
522         {
523           // If it's not any of the 'special' characters then just add it to the tag string.
524           tagString += character;
525         }
526       }
527       --it;
528     }
529   }
530
531   // If the tag string has white spaces, then parse the properties is needed.
532   if( propertiesFound )
533   {
534     ParseProperties( tagString, tag, properties );
535   }
536   else
537   {
538     tag = tagString;
539   }
540
541   return isTag;
542 }
543
544 } // namespace
545
546 void GetStyledTextArray( const std::string& markupString, StyledTextArray& styledTextArray, bool scanForMarkup )
547 {
548   styledTextArray.clear();
549
550   if ( !scanForMarkup )
551   {
552     styledTextArray.push_back( StyledText( Text( markupString ), TextStyle() ) );
553     return;
554   }
555
556   TextStyle defaultStyle;
557   std::stack<TextStyle> styleStack;
558
559   styleStack.push( defaultStyle );
560   TextStyle currentStyle = styleStack.top();
561   std::string textToBeStored;
562   TextStyle styleToBeStored( currentStyle );
563   for( std::string::const_iterator it = markupString.begin(), endIt = markupString.end(); it != endIt; ++it )
564   {
565     std::string tag;
566     bool isEndTag = false;
567     std::vector<Property> tagProperties;
568     if( IsTag( it, endIt, tag, isEndTag, tagProperties ) )
569     {
570       if( CaseInsensitiveComparison( XHTML_I_TAG, tag ) )
571       {
572         if( !isEndTag )
573         {
574           TextStyle newStyle( currentStyle );
575           newStyle.SetItalics( true );
576           styleStack.push( newStyle );
577           currentStyle = styleStack.top();
578         }
579         else
580         {
581           styleStack.pop();
582           currentStyle = styleStack.top();
583         }
584       } // <i></i>
585       else if( CaseInsensitiveComparison( XHTML_U_TAG, tag ) )
586       {
587         if( !isEndTag )
588         {
589           TextStyle newStyle( currentStyle );
590           newStyle.SetUnderline( true );
591           styleStack.push( newStyle );
592           currentStyle = styleStack.top();
593         }
594         else
595         {
596           styleStack.pop();
597           currentStyle = styleStack.top();
598         }
599       } // <u></u>
600       else if( CaseInsensitiveComparison( XHTML_B_TAG, tag ) )
601       {
602         if( !isEndTag )
603         {
604           TextStyle newStyle( currentStyle );
605           newStyle.SetWeight( TextStyle::BOLD );
606           styleStack.push( newStyle );
607           currentStyle = styleStack.top();
608         }
609         else
610         {
611           styleStack.pop();
612           currentStyle = styleStack.top();
613         }
614       } // <b></b>
615       else if( CaseInsensitiveComparison( XHTML_BR_TAG, tag ) )
616       {
617         if( isEndTag )
618         {
619           AddText( textToBeStored, styleToBeStored, styledTextArray );
620           AddNewLineChar( currentStyle, styledTextArray );
621           textToBeStored.clear();
622         }
623       } // <br/>
624       else if( CaseInsensitiveComparison( XHTML_FONT_TAG, tag ) )
625       {
626         if( !isEndTag )
627         {
628           TextStyle newStyle( currentStyle );
629           for( std::vector<Property>::const_iterator it = tagProperties.begin(), endIt = tagProperties.end(); it != endIt; ++it )
630           {
631             const Property& property( *it );
632             if( CaseInsensitiveComparison( XHTML_FACE_PROPERTY, property.name ) )
633             {
634               newStyle.SetFontName( property.value );
635             }
636             else if( CaseInsensitiveComparison( XHTML_STYLE_PROPERTY, property.name ) )
637             {
638               newStyle.SetFontStyle( property.value );
639             }
640             else if( CaseInsensitiveComparison( XHTML_COLOR_PROPERTY, property.name ) )
641             {
642               Vector4 color;
643               ColorStringToVector4( property.value, color );
644               newStyle.SetTextColor( color );
645             }
646             else if( CaseInsensitiveComparison( XHTML_SIZE_PROPERTY, property.name ) )
647             {
648               newStyle.SetFontPointSize( PointSize( StringToFloat( property.value ) ) );
649             }
650           }
651           styleStack.push( newStyle );
652           currentStyle = styleStack.top();
653         }
654         else
655         {
656           styleStack.pop();
657           currentStyle = styleStack.top();
658         }
659       } // <font></font>
660       else if( CaseInsensitiveComparison( XHTML_SHADOW_TAG, tag ) )
661       {
662         if( !isEndTag )
663         {
664           TextStyle newStyle( currentStyle );
665           Vector4 color( TextStyle::DEFAULT_SHADOW_COLOR );
666           Vector2 offset( TextStyle::DEFAULT_SHADOW_OFFSET );
667           for( std::vector<Property>::const_iterator it = tagProperties.begin(), endIt = tagProperties.end(); it != endIt; ++it )
668           {
669             const Property& property( *it );
670             if( CaseInsensitiveComparison( XHTML_PARAM_X_PROPERTY, property.name ) )
671             {
672               offset.x = StringToFloat( property.value );
673             }
674             else if( CaseInsensitiveComparison( XHTML_PARAM_Y_PROPERTY, property.name ) )
675             {
676               offset.y = StringToFloat( property.value );
677             }
678             else if( CaseInsensitiveComparison( XHTML_COLOR_PROPERTY, property.name ) )
679             {
680               ColorStringToVector4( property.value, color );
681             }
682           }
683           newStyle.SetShadow( true, color, offset );
684           styleStack.push( newStyle );
685           currentStyle = styleStack.top();
686         }
687         else
688         {
689           styleStack.pop();
690           currentStyle = styleStack.top();
691         }
692       } // <shadow></shadow>
693       else if( CaseInsensitiveComparison( XHTML_GLOW_TAG, tag ) )
694       {
695         if( !isEndTag )
696         {
697           TextStyle newStyle( currentStyle );
698           Vector4 color( TextStyle::DEFAULT_GLOW_COLOR );
699           float intensity = TextStyle::DEFAULT_GLOW_INTENSITY;
700           for( std::vector<Property>::const_iterator it = tagProperties.begin(), endIt = tagProperties.end(); it != endIt; ++it )
701           {
702             const Property& property( *it );
703             if( CaseInsensitiveComparison( XHTML_PARAM_PROPERTY, property.name ) )
704             {
705               intensity = StringToFloat( property.value );
706             }
707             else if( CaseInsensitiveComparison( XHTML_COLOR_PROPERTY, property.name ) )
708             {
709               ColorStringToVector4( property.value, color );
710             }
711           }
712           newStyle.SetGlow( true, color, intensity );
713           styleStack.push( newStyle );
714           currentStyle = styleStack.top();
715         }
716         else
717         {
718           styleStack.pop();
719           currentStyle = styleStack.top();
720         }
721       } // <glow></glow>
722       else if( CaseInsensitiveComparison( XHTML_OUTLINE_TAG, tag ) )
723       {
724         if( !isEndTag )
725         {
726           TextStyle newStyle( currentStyle );
727           Vector4 color( TextStyle::DEFAULT_OUTLINE_COLOR );
728           Vector2 thickness( TextStyle::DEFAULT_OUTLINE_THICKNESS );
729           for( std::vector<Property>::const_iterator it = tagProperties.begin(), endIt = tagProperties.end(); it != endIt; ++it )
730           {
731             const Property& property( *it );
732             if( CaseInsensitiveComparison( XHTML_PARAM_X_PROPERTY, property.name ) )
733             {
734               thickness.x = StringToFloat( property.value );
735             }
736             else if( CaseInsensitiveComparison( XHTML_PARAM_Y_PROPERTY, property.name ) )
737             {
738               thickness.y = StringToFloat( property.value );
739             }
740             else if( CaseInsensitiveComparison( XHTML_COLOR_PROPERTY, property.name ) )
741             {
742               ColorStringToVector4( property.value, color );
743             }
744           }
745           newStyle.SetOutline( true, color, thickness );
746           styleStack.push( newStyle );
747           currentStyle = styleStack.top();
748         }
749         else
750         {
751           styleStack.pop();
752           currentStyle = styleStack.top();
753         }
754       } // <outline></outline>
755       else if( CaseInsensitiveComparison( XHTML_SMOOTH_EDGE_TAG, tag ) )
756       {
757         if( !isEndTag )
758         {
759           TextStyle newStyle( currentStyle );
760           for( std::vector<Property>::const_iterator it = tagProperties.begin(), endIt = tagProperties.end(); it != endIt; ++it )
761           {
762             const Property& property( *it );
763             if( CaseInsensitiveComparison( XHTML_PARAM_PROPERTY, property.name ) )
764             {
765               newStyle.SetSmoothEdge( StringToFloat( property.value ) );
766             }
767           }
768           styleStack.push( newStyle );
769           currentStyle = styleStack.top();
770         }
771         else
772         {
773           styleStack.pop();
774           currentStyle = styleStack.top();
775         }
776       } // <smooth></smooth>
777     } // end if( IsTag() )
778     else
779     {
780       char character( *it );
781
782       // Adding < or > special character.
783       if( ( BACK_SLASH == character ) && ( it + 1 != endIt ) )
784       {
785         const char nextChar( *( it + 1 ) );
786         if( ( LESS_THAN == nextChar ) || ( GREATER_THAN == nextChar ) )
787         {
788           character = nextChar;
789           ++it;
790         }
791       }
792       else if( ( LINE_SEPARATOR_CR == character ) && ( it + 1 != endIt ) )
793       {
794         if( LINE_SEPARATOR_LF == *( it + 1 ) )
795         {
796           character = LINE_SEPARATOR_LF;
797           ++it;
798         }
799       }
800
801       if( styleToBeStored != currentStyle )
802       {
803         if( !textToBeStored.empty() )
804         {
805           AddText( textToBeStored, styleToBeStored, styledTextArray );
806           textToBeStored.clear();
807         }
808         styleToBeStored = currentStyle;
809       }
810       textToBeStored.insert( textToBeStored.end(), character );
811     }
812   }
813   if( !textToBeStored.empty() )
814   {
815     AddText( textToBeStored, styleToBeStored, styledTextArray );
816     textToBeStored.clear();
817   }
818 }
819
820 void GetPlainString( const StyledTextArray& styledTextArray, std::string& plainString )
821 {
822   // First step is put all simultaneous characters with same style together.
823   for( StyledTextArray::const_iterator it = styledTextArray.begin(), endIt = styledTextArray.end(); it != endIt; ++it )
824   {
825     const StyledText& styledText( *it );
826     plainString += styledText.mText.GetText();
827   }
828 }
829
830 void GetMarkupString( const StyledTextArray& styledTextArray, std::string& markupString )
831 {
832   const std::string WHITE_SPACE( " " );
833
834   TextStyle previousStyle;
835   StyledText newStyledText;
836   StyledTextArray compressedStyledTextArray;
837
838   markupString.clear();
839
840   // First step is put all simultaneous characters with same style together.
841   for( StyledTextArray::const_iterator it = styledTextArray.begin(), endIt = styledTextArray.end(); it != endIt; ++it )
842   {
843     const StyledText& styledText( *it );
844
845     if( previousStyle != styledText.mStyle )
846     {
847       if( !newStyledText.mText.IsEmpty() )
848       {
849         compressedStyledTextArray.push_back( newStyledText );
850       }
851       newStyledText = StyledText();
852       newStyledText.mStyle = styledText.mStyle;
853     }
854
855     if( !styledText.mText.IsEmpty() )
856     {
857       const char character = styledText.mText.GetText()[0];
858       if( ( character == LESS_THAN ) || ( character == GREATER_THAN ) )
859       {
860         newStyledText.mText.Append( Text( std::string( &BACK_SLASH, 1 ) ) );
861       }
862     }
863
864     newStyledText.mText.Append( styledText.mText );
865
866     previousStyle = newStyledText.mStyle;
867   }
868
869   //Add the last characters.
870   if( !newStyledText.mText.IsEmpty() )
871   {
872     compressedStyledTextArray.push_back( newStyledText );
873   }
874
875   // Write markup string.
876   const std::string lineSeparatorStr( &LINE_SEPARATOR_LF );
877   const Text lineSeparator( lineSeparatorStr );
878
879   const TextStyle defaultStyle;
880   for( StyledTextArray::const_iterator it = compressedStyledTextArray.begin(), endIt = compressedStyledTextArray.end(); it != endIt; ++it )
881   {
882     const StyledText& styledText( *it );
883
884     bool isItalics = styledText.mStyle.GetItalics();
885     bool isBold = defaultStyle.GetWeight() != styledText.mStyle.GetWeight();
886     bool isUnderline = styledText.mStyle.GetUnderline();
887     bool hasFontFace = defaultStyle.GetFontName() != styledText.mStyle.GetFontName();
888     bool hasFontStyle = defaultStyle.GetFontStyle() != styledText.mStyle.GetFontStyle();
889     bool hasFontSize = fabsf( defaultStyle.GetFontPointSize() - styledText.mStyle.GetFontPointSize() ) > GetRangedEpsilon( defaultStyle.GetFontPointSize(), styledText.mStyle.GetFontPointSize() );
890     bool hasFontColor = defaultStyle.GetTextColor() != styledText.mStyle.GetTextColor();
891
892     bool hasSmooth = fabsf( defaultStyle.GetSmoothEdge() - styledText.mStyle.GetSmoothEdge() ) > GetRangedEpsilon( defaultStyle.GetSmoothEdge(), styledText.mStyle.GetSmoothEdge() );
893     bool hasShadowColor = defaultStyle.GetShadowColor() != styledText.mStyle.GetShadowColor();
894     bool hasShadowParams = defaultStyle.GetShadowOffset() != styledText.mStyle.GetShadowOffset();
895     bool hasGlowColor = defaultStyle.GetGlowColor() != styledText.mStyle.GetGlowColor();
896     bool hasGlowParams = fabsf( defaultStyle.GetGlowIntensity() - styledText.mStyle.GetGlowIntensity() ) > GetRangedEpsilon( defaultStyle.GetGlowIntensity(), styledText.mStyle.GetGlowIntensity() );
897     bool hasOutlineColor = defaultStyle.GetOutlineColor() != styledText.mStyle.GetOutlineColor();
898     bool hasOutlineParams = defaultStyle.GetOutlineThickness() != styledText.mStyle.GetOutlineThickness();
899
900     // Write font info.
901     if( hasFontFace || hasFontStyle || hasFontSize || hasFontColor )
902     {
903       markupString += LESS_THAN + XHTML_FONT_TAG;
904
905       if( hasFontFace )
906       {
907         markupString += WHITE_SPACE + XHTML_FACE_PROPERTY + EQUAL + QUOTATION_MARK + styledText.mStyle.GetFontName() + QUOTATION_MARK; // face=''
908       }
909
910       if( hasFontStyle )
911       {
912         markupString += WHITE_SPACE + XHTML_STYLE_PROPERTY + EQUAL + QUOTATION_MARK + styledText.mStyle.GetFontStyle() + QUOTATION_MARK; // style=''
913       }
914
915       if( hasFontSize )
916       {
917         markupString += WHITE_SPACE + XHTML_SIZE_PROPERTY + EQUAL + QUOTATION_MARK + FloatToString( styledText.mStyle.GetFontPointSize() ) + QUOTATION_MARK; // size=''
918       }
919
920       if( hasFontColor )
921       {
922         markupString += WHITE_SPACE + XHTML_COLOR_PROPERTY + EQUAL + QUOTATION_MARK + Vector4ToColorString( styledText.mStyle.GetTextColor() ) + QUOTATION_MARK; // color=''
923       }
924
925       markupString += GREATER_THAN;
926     } // <font>
927
928     // Write italics.
929     if( isItalics )
930     {
931       markupString += LESS_THAN + XHTML_I_TAG + GREATER_THAN;
932     } // <i>
933
934     // Write bold.
935     if( isBold )
936     {
937       markupString += LESS_THAN + XHTML_B_TAG + GREATER_THAN;
938     } // <b>
939
940     // Write underline.
941     if( isUnderline )
942     {
943       markupString += LESS_THAN + XHTML_U_TAG + GREATER_THAN;
944     } // <u>
945
946     // Write smooth.
947     if( hasSmooth )
948     {
949       markupString += LESS_THAN + XHTML_SMOOTH_EDGE_TAG + WHITE_SPACE + XHTML_PARAM_PROPERTY + EQUAL + QUOTATION_MARK + FloatToString( styledText.mStyle.GetSmoothEdge() ) + QUOTATION_MARK + GREATER_THAN;
950     }
951
952     // Write shadow.
953     if( styledText.mStyle.GetShadow() )
954     {
955       markupString += LESS_THAN + XHTML_SHADOW_TAG;
956
957       if( hasShadowColor )
958       {
959         markupString += WHITE_SPACE + XHTML_COLOR_PROPERTY + EQUAL + QUOTATION_MARK + Vector4ToColorString( styledText.mStyle.GetShadowColor() ) + QUOTATION_MARK;
960       }
961
962       if( hasShadowParams )
963       {
964         markupString += WHITE_SPACE + XHTML_PARAM_X_PROPERTY + EQUAL + QUOTATION_MARK + FloatToString( styledText.mStyle.GetShadowOffset().x ) + QUOTATION_MARK;
965         markupString += WHITE_SPACE + XHTML_PARAM_Y_PROPERTY + EQUAL + QUOTATION_MARK + FloatToString( styledText.mStyle.GetShadowOffset().y ) + QUOTATION_MARK;
966       }
967
968       markupString += GREATER_THAN;
969     }
970
971     // Write glow.
972     if( styledText.mStyle.GetGlow() )
973     {
974       markupString += LESS_THAN + XHTML_GLOW_TAG;
975
976       if( hasGlowColor )
977       {
978         markupString += WHITE_SPACE + XHTML_COLOR_PROPERTY + EQUAL + QUOTATION_MARK + Vector4ToColorString( styledText.mStyle.GetGlowColor() ) + QUOTATION_MARK; // color=''
979       }
980
981       if( hasGlowParams )
982       {
983         markupString += WHITE_SPACE + XHTML_PARAM_PROPERTY + EQUAL + QUOTATION_MARK + FloatToString( styledText.mStyle.GetGlowIntensity() ) + QUOTATION_MARK; // param=''
984       }
985
986       markupString += GREATER_THAN;
987     } // <glow>
988
989     // Write outline.
990     if( styledText.mStyle.GetOutline() )
991     {
992       markupString += LESS_THAN + XHTML_OUTLINE_TAG;
993
994       if( hasOutlineColor )
995       {
996         markupString += WHITE_SPACE + XHTML_COLOR_PROPERTY + EQUAL + QUOTATION_MARK + Vector4ToColorString( styledText.mStyle.GetOutlineColor() ) + QUOTATION_MARK; // color = ''
997       }
998
999       if( hasOutlineParams )
1000       {
1001         markupString += WHITE_SPACE + XHTML_PARAM_X_PROPERTY + EQUAL + QUOTATION_MARK + FloatToString( styledText.mStyle.GetOutlineThickness().x ) + QUOTATION_MARK; // paramx=''
1002         markupString += WHITE_SPACE + XHTML_PARAM_Y_PROPERTY + EQUAL + QUOTATION_MARK + FloatToString( styledText.mStyle.GetOutlineThickness().y ) + QUOTATION_MARK; // paramy=''
1003       }
1004
1005       markupString += GREATER_THAN;
1006     } // <outline>
1007
1008     // Write text.
1009     if( styledText.mText[0] == lineSeparator[0] )
1010     {
1011       markupString += LESS_THAN + XHTML_BR_TAG + WHITE_SPACE + SLASH + GREATER_THAN; // <br />
1012     }
1013     else
1014     {
1015       markupString += styledText.mText.GetText();
1016     }
1017
1018     // Write outline close tag.
1019     if( styledText.mStyle.GetOutline() )
1020     {
1021       markupString += LESS_THAN + ( SLASH + XHTML_OUTLINE_TAG + GREATER_THAN ); // </outline>
1022     }
1023
1024     // Write glow close tag.
1025     if( styledText.mStyle.GetGlow() )
1026     {
1027       markupString += LESS_THAN + ( SLASH + XHTML_GLOW_TAG + GREATER_THAN ); // </glow>
1028     }
1029
1030     // Write shadow close tag.
1031     if( styledText.mStyle.GetShadow() )
1032     {
1033       markupString += LESS_THAN + ( SLASH + XHTML_SHADOW_TAG + GREATER_THAN ); // </shadow>
1034     }
1035
1036     // Write smooth close tag.
1037     if( hasSmooth )
1038     {
1039       markupString += LESS_THAN + ( SLASH + XHTML_SMOOTH_EDGE_TAG + GREATER_THAN ); // </smooth>
1040     }
1041
1042     // Write underline close tag.
1043     if( isUnderline )
1044     {
1045       markupString += LESS_THAN + ( SLASH + XHTML_U_TAG + GREATER_THAN ); // </u>
1046     }
1047
1048     // Write bold close tag.
1049     if( isBold )
1050     {
1051       markupString += LESS_THAN + ( SLASH + XHTML_B_TAG + GREATER_THAN ); // </b>
1052     }
1053
1054     // Write italics close tag.
1055     if( isItalics )
1056     {
1057       markupString += LESS_THAN + ( SLASH + XHTML_I_TAG + GREATER_THAN ); // </i>
1058     }
1059
1060     // Write font close tag.
1061     if( hasFontFace || hasFontStyle || hasFontSize || hasFontColor )
1062     {
1063       markupString += LESS_THAN + ( SLASH + XHTML_FONT_TAG + GREATER_THAN ); // </font>
1064     }
1065   }
1066 }
1067
1068 void SetTextStyle( StyledTextArray& styledTextArray, const TextStyle& style, const TextStyle::Mask mask )
1069 {
1070   if( !styledTextArray.empty() )
1071   {
1072     const size_t size = styledTextArray.size() - 1;
1073     SetTextStyleToRange( styledTextArray, style, mask, 0, size );
1074   }
1075 }
1076
1077 void SetTextStyle( const Text& text, StyledTextArray& styledTextArray, const TextStyle& style, const TextStyle::Mask mask )
1078 {
1079   if( !text.IsEmpty() )
1080   {
1081     const size_t size = text.GetLength();
1082
1083     for( size_t i = 0; i < size; ++i )
1084     {
1085       StyledText styledText;
1086       styledText.mText = Text( text[i] );
1087       styledText.mStyle = style;
1088
1089       styledTextArray.push_back( styledText );
1090     }
1091
1092     SetTextStyleToRange( styledTextArray, style, mask, 0, size - 1 );
1093   }
1094 }
1095
1096 void SetTextStyleToRange( StyledTextArray& styledTextArray, const TextStyle& style, const TextStyle::Mask mask, const std::size_t begin, const std::size_t end )
1097 {
1098   const size_t size = styledTextArray.size();
1099   DALI_ASSERT_ALWAYS( begin < size );
1100   DALI_ASSERT_ALWAYS( end < size );
1101
1102   for( StyledTextArray::iterator it = styledTextArray.begin() + std::min(begin, end), endIt = styledTextArray.begin() + std::max(begin, end) + 1; it != endIt; ++it )
1103   {
1104     StyledText& styledText( *it );
1105
1106     styledText.mStyle.Copy( style, mask );
1107   } // for loop
1108 }
1109
1110 } // namespace MarkupProcessor
1111
1112 } // namespace Toolkit
1113
1114 } // namespace Dali