2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/text/markup-processor.h>
22 #include <climits> // for ULONG_MAX
23 #include <dali/integration-api/debug.h>
26 #include <dali-toolkit/internal/text/character-set-conversion.h>
27 #include <dali-toolkit/internal/text/markup-processor-color.h>
28 #include <dali-toolkit/internal/text/markup-processor-font.h>
29 #include <dali-toolkit/internal/text/markup-processor-helper-functions.h>
30 #include <dali-toolkit/internal/text/xhtml-entities.h>
45 // HTML-ISH tag and attribute constants.
46 // Note they must be lower case in order to make the comparison to work
47 // as the parser converts all the read tags to lower case.
48 const std::string XHTML_COLOR_TAG("color");
49 const std::string XHTML_FONT_TAG("font");
50 const std::string XHTML_B_TAG("b");
51 const std::string XHTML_I_TAG("i");
52 const std::string XHTML_U_TAG("u");
53 const std::string XHTML_SHADOW_TAG("shadow");
54 const std::string XHTML_GLOW_TAG("glow");
55 const std::string XHTML_OUTLINE_TAG("outline");
57 const char LESS_THAN = '<';
58 const char GREATER_THAN = '>';
59 const char EQUAL = '=';
60 const char QUOTATION_MARK = '\'';
61 const char SLASH = '/';
62 const char BACK_SLASH = '\\';
63 const char AMPERSAND = '&';
64 const char HASH = '#';
65 const char SEMI_COLON = ';';
66 const char CHAR_ARRAY_END = '\0';
67 const char HEX_CODE = 'x';
69 const char WHITE_SPACE = 0x20; // ASCII value of the white space.
71 // Range 1 0x0u < XHTML_DECIMAL_ENTITY_RANGE <= 0xD7FFu
72 // Range 2 0xE000u < XHTML_DECIMAL_ENTITY_RANGE <= 0xFFFDu
73 // Range 3 0x10000u < XHTML_DECIMAL_ENTITY_RANGE <= 0x10FFFFu
74 const unsigned long XHTML_DECIMAL_ENTITY_RANGE[] = { 0x0u, 0xD7FFu, 0xE000u, 0xFFFDu, 0x10000u, 0x10FFFFu };
76 const unsigned int MAX_NUM_OF_ATTRIBUTES = 5u; ///< The font tag has the 'family', 'size' 'weight', 'width' and 'slant' attrubutes.
77 const unsigned int DEFAULT_VECTOR_SIZE = 16u; ///< Default size of run vectors.
79 #if defined(DEBUG_ENABLED)
80 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_MARKUP_PROCESSOR");
84 * @brief Struct used to retrieve the style runs from the mark-up string.
88 typedef VectorBase::SizeType RunIndex;
90 Vector<RunIndex> stack; ///< Use a vector as a style stack. Stores the indices pointing where the run is stored inside the logical model.
91 unsigned int topIndex; ///< Points the top of the stack.
97 stack.Resize( DEFAULT_VECTOR_SIZE );
100 void Push( RunIndex index )
102 // Check if there is space inside the style stack.
103 const VectorBase::SizeType size = stack.Count();
104 if( topIndex >= size )
106 // Resize the style stack.
107 stack.Resize( 2u * size );
110 // Set the run index in the top of the stack.
111 *( stack.Begin() + topIndex ) = index;
113 // Reposition the pointer to the top of the stack.
119 // Pop the top of the stack.
121 return *( stack.Begin() + topIndex );
126 * @brief Initializes a font run description to its defaults.
128 * @param[in,out] fontRun The font description run to initialize.
130 void Initialize( FontDescriptionRun& fontRun )
132 fontRun.characterRun.characterIndex = 0u;
133 fontRun.characterRun.numberOfCharacters = 0u;
134 fontRun.familyName = NULL;
135 fontRun.familyLength = 0u;
136 fontRun.weight = TextAbstraction::FontWeight::NORMAL;
137 fontRun.width = TextAbstraction::FontWidth::NORMAL;
138 fontRun.slant = TextAbstraction::FontSlant::NORMAL;
140 fontRun.familyDefined = false;
141 fontRun.weightDefined = false;
142 fontRun.widthDefined = false;
143 fontRun.slantDefined = false;
144 fontRun.sizeDefined = false;
148 * @brief Splits the tag string into the tag name and its attributes.
150 * The attributes are stored in a vector in the tag.
152 * @param[in,out] tag The tag.
154 void ParseAttributes( Tag& tag )
156 tag.attributes.Resize( MAX_NUM_OF_ATTRIBUTES );
158 // Find first the tag name.
159 bool isQuotationOpen = false;
161 const char* tagBuffer = tag.buffer;
162 const char* const tagEndBuffer = tagBuffer + tag.length;
164 for( ; tagBuffer < tagEndBuffer; ++tagBuffer )
166 const char character = *tagBuffer;
167 if( WHITE_SPACE < character )
173 // Stops counting the length of the tag when a white space is found.
174 // @note a white space is the WHITE_SPACE character and anything below as 'tab', 'return' or 'control characters'.
178 SkipWhiteSpace( tagBuffer, tagEndBuffer );
180 // Find the attributes.
181 unsigned int attributeIndex = 0u;
182 const char* nameBuffer = NULL;
183 const char* valueBuffer = NULL;
184 Length nameLength = 0u;
185 Length valueLength = 0u;
187 bool addToNameValue = true;
188 Length numberOfWhiteSpace = 0u;
189 for( ; tagBuffer < tagEndBuffer; ++tagBuffer )
191 const char character = *tagBuffer;
192 if( ( WHITE_SPACE >= character ) && !isQuotationOpen )
194 if( NULL != valueBuffer )
196 // Remove white spaces at the end of the value.
197 valueLength -= numberOfWhiteSpace;
200 if( ( NULL != nameBuffer ) && ( NULL != valueBuffer ) )
202 // Every time a white space is found, a new attribute is created and stored in the attributes vector.
203 Attribute& attribute = *( tag.attributes.Begin() + attributeIndex );
206 attribute.nameBuffer = nameBuffer;
207 attribute.valueBuffer = valueBuffer;
208 attribute.nameLength = nameLength;
209 attribute.valueLength = valueLength;
216 addToNameValue = true; // next read characters will be added to the name.
219 else if( EQUAL == character ) // '='
221 addToNameValue = false; // next read characters will be added to the value.
222 SkipWhiteSpace( tagBuffer, tagEndBuffer );
224 else if( QUOTATION_MARK == character ) // '\''
226 // Do not add quotation marks to neither name nor value.
227 isQuotationOpen = !isQuotationOpen;
229 if( isQuotationOpen )
232 SkipWhiteSpace( tagBuffer, tagEndBuffer );
238 // Adds characters to the name or the value.
241 if( NULL == nameBuffer )
243 nameBuffer = tagBuffer;
249 if( isQuotationOpen )
251 if( WHITE_SPACE >= character )
253 ++numberOfWhiteSpace;
257 numberOfWhiteSpace = 0u;
260 if( NULL == valueBuffer )
262 valueBuffer = tagBuffer;
269 if( NULL != valueBuffer )
271 // Remove white spaces at the end of the value.
272 valueLength -= numberOfWhiteSpace;
275 if( ( NULL != nameBuffer ) && ( NULL != valueBuffer ) )
277 // Checks if the last attribute needs to be added.
278 Attribute& attribute = *( tag.attributes.Begin() + attributeIndex );
281 attribute.nameBuffer = nameBuffer;
282 attribute.valueBuffer = valueBuffer;
283 attribute.nameLength = nameLength;
284 attribute.valueLength = valueLength;
287 // Resize the vector of attributes.
288 tag.attributes.Resize( attributeIndex );
292 * @brief It parses a tag and its attributes if the given iterator @e it is pointing at a tag beginning.
294 * @param[in,out] markupStringBuffer The mark-up string buffer. It's a const iterator pointing the current character.
295 * @param[in] markupStringEndBuffer Pointer to one character after the end of the mark-up string buffer.
296 * @param[out] tag The tag with its attributes.
298 * @return @e true if the iterator @e it is pointing a mark-up tag. Otherwise @e false.
300 bool IsTag( const char*& markupStringBuffer,
301 const char* const markupStringEndBuffer,
305 bool isQuotationOpen = false;
306 bool attributesFound = false;
307 tag.isEndTag = false;
309 const char character = *markupStringBuffer;
310 if( LESS_THAN == character ) // '<'
315 // if the iterator is pointing to a '<' character, then check if it's a mark-up tag is needed.
316 ++markupStringBuffer;
317 if( markupStringBuffer < markupStringEndBuffer )
319 SkipWhiteSpace( markupStringBuffer, markupStringEndBuffer );
321 for( ; ( !isTag ) && ( markupStringBuffer < markupStringEndBuffer ); ++markupStringBuffer )
323 const char character = *markupStringBuffer;
325 if( SLASH == character ) // '/'
327 // if the tag has a '/' then it's an end or empty tag.
330 if( ( markupStringBuffer + 1u < markupStringEndBuffer ) && ( WHITE_SPACE >= *( markupStringBuffer + 1u ) ) && ( !isQuotationOpen ) )
332 ++markupStringBuffer;
333 SkipWhiteSpace( markupStringBuffer, markupStringEndBuffer );
334 --markupStringBuffer;
337 else if( GREATER_THAN == character ) // '>'
341 else if( QUOTATION_MARK == character )
343 isQuotationOpen = !isQuotationOpen;
346 else if( WHITE_SPACE >= character ) // ' '
348 // If the tag contains white spaces then it may have attributes.
349 if( !isQuotationOpen )
351 attributesFound = true;
357 if( NULL == tag.buffer )
359 tag.buffer = markupStringBuffer;
362 // If it's not any of the 'special' characters then just add it to the tag string.
368 // If the tag string has white spaces, then parse the attributes is needed.
369 if( attributesFound )
371 ParseAttributes( tag );
379 * @brief Returns length of XHTML entity by parsing the text. It also determines if it is XHTML entity or not.
381 * @param[in] markupStringBuffer The mark-up string buffer. It's a const iterator pointing the current character.
382 * @param[in] markupStringEndBuffer Pointing to end of mark-up string buffer.
384 * @return Length of markupText in case of XHTML entity otherwise return 0.
386 unsigned int GetXHTMLEntityLength( const char*& markupStringBuffer,
387 const char* const markupStringEndBuffer )
389 char character = *markupStringBuffer;
390 if( AMPERSAND == character ) // '&'
392 // if the iterator is pointing to a '&' character, then check for ';' to find end to XHTML entity.
393 ++markupStringBuffer;
394 if( markupStringBuffer < markupStringEndBuffer )
396 unsigned int len = 1u;
397 for( ; markupStringBuffer < markupStringEndBuffer ; ++markupStringBuffer )
399 character = *markupStringBuffer;
401 if( SEMI_COLON == character ) // ';'
403 // found end of XHTML entity
404 ++markupStringBuffer;
407 else if( ( AMPERSAND == character ) || ( BACK_SLASH == character ) || ( LESS_THAN == character ))
418 * @brief It parses a XHTML string which has hex/decimal entity and fill its corresponging utf-8 string.
420 * @param[in] markupText The mark-up text buffer.
421 * @param[out] utf-8 text Corresponding to markup Text
423 * @return true if string is successfully parsed otherwise false
425 bool XHTMLNumericEntityToUtf8 ( const char* markupText, char* utf8 )
429 if( NULL != markupText )
433 // check if hex or decimal entity
434 if( ( CHAR_ARRAY_END != *markupText ) && ( HEX_CODE == *markupText ) )
441 unsigned long l = strtoul( markupText, &end, ( isHex ? 16 : 10 ) ); // l contains UTF-32 code in case of correct XHTML entity
443 // check for valid XHTML numeric entities (between '#' or "#x" and ';')
444 if( ( l > 0 ) && ( l < ULONG_MAX ) && ( *end == SEMI_COLON ) ) // in case wrong XHTML entity is set eg. "abcdefs;" in that case *end will be 'a'
446 /* characters XML 1.1 permits */
447 if( ( ( XHTML_DECIMAL_ENTITY_RANGE[0] < l ) && ( l <= XHTML_DECIMAL_ENTITY_RANGE[1] ) ) ||
448 ( ( XHTML_DECIMAL_ENTITY_RANGE[2] <= l ) && ( l <= XHTML_DECIMAL_ENTITY_RANGE[3] ) ) ||
449 ( ( XHTML_DECIMAL_ENTITY_RANGE[4] <= l ) && ( l <= XHTML_DECIMAL_ENTITY_RANGE[5] ) ) )
451 // Convert UTF32 code to UTF8
452 Utf32ToUtf8( reinterpret_cast<const uint32_t* const>( &l ), 1, reinterpret_cast<uint8_t*>( utf8 ) );
462 void ProcessMarkupString( const std::string& markupString, MarkupProcessData& markupProcessData )
464 // Reserve space for the plain text.
465 const Length markupStringSize = markupString.size();
466 markupProcessData.markupProcessedText.reserve( markupStringSize );
468 // Stores a struct with the index to the first character of the run, the type of run and its parameters.
469 StyleStack styleStack;
471 // Points the next free position in the vector of runs.
472 StyleStack::RunIndex colorRunIndex = 0u;
473 StyleStack::RunIndex fontRunIndex = 0u;
475 // Give an initial default value to the model's vectors.
476 markupProcessData.colorRuns.Reserve( DEFAULT_VECTOR_SIZE );
477 markupProcessData.fontRuns.Reserve( DEFAULT_VECTOR_SIZE );
479 // Get the mark-up string buffer.
480 const char* markupStringBuffer = markupString.c_str();
481 const char* const markupStringEndBuffer = markupStringBuffer + markupStringSize;
484 CharacterIndex characterIndex = 0u;
485 for( ; markupStringBuffer < markupStringEndBuffer; )
487 if( IsTag( markupStringBuffer,
488 markupStringEndBuffer,
491 if( TokenComparison( XHTML_COLOR_TAG, tag.buffer, tag.length ) )
495 // Create a new color run.
497 colorRun.characterRun.numberOfCharacters = 0u;
499 // Set the start character index.
500 colorRun.characterRun.characterIndex = characterIndex;
502 // Fill the run with the attributes.
503 ProcessColorTag( tag, colorRun );
505 // Push the color run in the logical model.
506 markupProcessData.colorRuns.PushBack( colorRun );
508 // Push the index of the run into the stack.
509 styleStack.Push( colorRunIndex );
511 // Point the next color run.
516 // Pop the top of the stack and set the number of characters of the run.
517 ColorRun& colorRun = *( markupProcessData.colorRuns.Begin() + styleStack.Pop() );
518 colorRun.characterRun.numberOfCharacters = characterIndex - colorRun.characterRun.characterIndex;
521 else if( TokenComparison( XHTML_I_TAG, tag.buffer, tag.length ) )
525 // Create a new font run.
526 FontDescriptionRun fontRun;
527 Initialize( fontRun );
529 // Fill the run with the parameters.
530 fontRun.characterRun.characterIndex = characterIndex;
531 fontRun.slant = TextAbstraction::FontSlant::ITALIC;
532 fontRun.slantDefined = true;
534 // Push the font run in the logical model.
535 markupProcessData.fontRuns.PushBack( fontRun );
537 // Push the index of the run into the stack.
538 styleStack.Push( fontRunIndex );
540 // Point the next free font run.
545 // Pop the top of the stack and set the number of characters of the run.
546 FontDescriptionRun& fontRun = *( markupProcessData.fontRuns.Begin() + styleStack.Pop() );
547 fontRun.characterRun.numberOfCharacters = characterIndex - fontRun.characterRun.characterIndex;
550 else if( TokenComparison( XHTML_U_TAG, tag.buffer, tag.length ) )
554 // Create a new underline run.
558 // Pop the top of the stack and set the number of characters of the run.
561 else if( TokenComparison( XHTML_B_TAG, tag.buffer, tag.length ) )
565 // Create a new font run.
566 FontDescriptionRun fontRun;
567 Initialize( fontRun );
569 // Fill the run with the parameters.
570 fontRun.characterRun.characterIndex = characterIndex;
571 fontRun.weight = TextAbstraction::FontWeight::BOLD;
572 fontRun.weightDefined = true;
574 // Push the font run in the logical model.
575 markupProcessData.fontRuns.PushBack( fontRun );
577 // Push the index of the run into the stack.
578 styleStack.Push( fontRunIndex );
580 // Point the next free font run.
585 // Pop the top of the stack and set the number of characters of the run.
586 FontDescriptionRun& fontRun = *( markupProcessData.fontRuns.Begin() + styleStack.Pop() );
587 fontRun.characterRun.numberOfCharacters = characterIndex - fontRun.characterRun.characterIndex;
590 else if( TokenComparison( XHTML_FONT_TAG, tag.buffer, tag.length ) )
594 // Create a new font run.
595 FontDescriptionRun fontRun;
596 Initialize( fontRun );
598 // Fill the run with the parameters.
599 fontRun.characterRun.characterIndex = characterIndex;
601 ProcessFontTag( tag, fontRun );
603 // Push the font run in the logical model.
604 markupProcessData.fontRuns.PushBack( fontRun );
606 // Push the index of the run into the stack.
607 styleStack.Push( fontRunIndex );
609 // Point the next free font run.
614 // Pop the top of the stack and set the number of characters of the run.
615 FontDescriptionRun& fontRun = *( markupProcessData.fontRuns.Begin() + styleStack.Pop() );
616 fontRun.characterRun.numberOfCharacters = characterIndex - fontRun.characterRun.characterIndex;
619 else if( TokenComparison( XHTML_SHADOW_TAG, tag.buffer, tag.length ) )
623 // Create a new shadow run.
627 // Pop the top of the stack and set the number of characters of the run.
629 } // <shadow></shadow>
630 else if( TokenComparison( XHTML_GLOW_TAG, tag.buffer, tag.length ) )
634 // Create a new glow run.
638 // Pop the top of the stack and set the number of characters of the run.
641 else if( TokenComparison( XHTML_OUTLINE_TAG, tag.buffer, tag.length ) )
645 // Create a new outline run.
649 // Pop the top of the stack and set the number of characters of the run.
651 } // <outline></outline>
652 } // end if( IsTag() )
653 else if( markupStringBuffer < markupStringEndBuffer )
655 unsigned char character = *markupStringBuffer;
656 const char* markupBuffer = markupStringBuffer;
657 unsigned char count = GetUtf8Length( character );
660 if( ( BACK_SLASH == character ) && ( markupStringBuffer + 1u < markupStringEndBuffer ) )
662 // Adding < , > or & special character.
663 const unsigned char nextCharacter = *( markupStringBuffer + 1u );
664 if( ( LESS_THAN == nextCharacter ) || ( GREATER_THAN == nextCharacter ) || ( AMPERSAND == nextCharacter ) )
666 character = nextCharacter;
667 ++markupStringBuffer;
669 count = GetUtf8Length( character );
670 markupBuffer = markupStringBuffer;
673 else // checking if conatins XHTML entity or not
675 const unsigned int len = GetXHTMLEntityLength( markupStringBuffer, markupStringEndBuffer);
677 // Parse markupStringTxt if it contains XHTML Entity between '&' and ';'
680 char* entityCode = NULL;
684 // Checking if XHTML Numeric Entity
685 if( HASH == *( markupBuffer + 1u ) )
687 entityCode = &utf8[0];
688 // markupBuffer is currently pointing to '&'. By adding 2u to markupBuffer it will point to numeric string by skipping "&#'
689 result = XHTMLNumericEntityToUtf8( ( markupBuffer + 2u ), entityCode );
691 else // Checking if XHTML Named Entity
693 entityCode = const_cast<char*> ( NamedEntityToUtf8( markupBuffer, len ) );
694 result = ( entityCode != NULL );
698 markupBuffer = entityCode; //utf8 text assigned to markupBuffer
699 character = markupBuffer[0];
703 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Not valid XHTML entity : (%.*s) \n", len, markupBuffer );
707 else // in case string conatins Start of XHTML Entity('&') but not its end character(';')
709 if( character == AMPERSAND )
712 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Not Well formed XHTML content \n" );
717 if( markupBuffer != NULL )
719 const unsigned char numberOfBytes = GetUtf8Length( character );
720 markupProcessData.markupProcessedText.push_back( character );
722 for( unsigned char i = 1u; i < numberOfBytes; ++i )
725 markupProcessData.markupProcessedText.push_back( *markupBuffer );
729 markupStringBuffer += count;
734 // Resize the model's vectors.
735 if( 0u == fontRunIndex )
737 markupProcessData.fontRuns.Clear();
741 markupProcessData.fontRuns.Resize( fontRunIndex );
744 if( 0u == colorRunIndex )
746 markupProcessData.colorRuns.Clear();
750 markupProcessData.colorRuns.Resize( colorRunIndex );
756 } // namespace Toolkit