+/**
+ * @brief Returns length of XHTML entity by parsing the text. It also determines if it is XHTML entity or not.
+ *
+ * @param[in] markupStringBuffer The mark-up string buffer. It's a const iterator pointing the current character.
+ * @param[in] markupStringEndBuffer Pointing to end of mark-up string buffer.
+ *
+ * @return Length of markupText in case of XHTML entity otherwise return 0.
+ */
+unsigned int GetXHTMLEntityLength(const char*& markupStringBuffer,
+ const char* const markupStringEndBuffer)
+{
+ char character = *markupStringBuffer;
+ if(AMPERSAND == character) // '&'
+ {
+ // if the iterator is pointing to a '&' character, then check for ';' to find end to XHTML entity.
+ ++markupStringBuffer;
+ if(markupStringBuffer < markupStringEndBuffer)
+ {
+ unsigned int len = 1u;
+ for(; markupStringBuffer < markupStringEndBuffer; ++markupStringBuffer)
+ {
+ character = *markupStringBuffer;
+ ++len;
+ if(SEMI_COLON == character) // ';'
+ {
+ // found end of XHTML entity
+ ++markupStringBuffer;
+ return len;
+ }
+ else if((AMPERSAND == character) || (BACK_SLASH == character) || (LESS_THAN == character))
+ {
+ return 0;
+ }
+ }
+ }
+ }
+ return 0;
+}
+
+/**
+ * @brief It parses a XHTML string which has hex/decimal entity and fill its corresponging utf-8 string.
+ *
+ * @param[in] markupText The mark-up text buffer.
+ * @param[out] utf-8 text Corresponding to markup Text
+ *
+ * @return true if string is successfully parsed otherwise false
+ */
+bool XHTMLNumericEntityToUtf8(const char* markupText, char* utf8)
+{
+ bool result = false;
+
+ if(NULL != markupText)
+ {
+ bool isHex = false;
+
+ // check if hex or decimal entity
+ if((CHAR_ARRAY_END != *markupText) && (HEX_CODE == *markupText))
+ {
+ isHex = true;
+ ++markupText;
+ }
+
+ char* end = NULL;
+ unsigned long l = strtoul(markupText, &end, (isHex ? 16 : 10)); // l contains UTF-32 code in case of correct XHTML entity
+
+ // check for valid XHTML numeric entities (between '#' or "#x" and ';')
+ 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'
+ {
+ /* characters XML 1.1 permits */
+ if(((XHTML_DECIMAL_ENTITY_RANGE[0] < l) && (l <= XHTML_DECIMAL_ENTITY_RANGE[1])) ||
+ ((XHTML_DECIMAL_ENTITY_RANGE[2] <= l) && (l <= XHTML_DECIMAL_ENTITY_RANGE[3])) ||
+ ((XHTML_DECIMAL_ENTITY_RANGE[4] <= l) && (l <= XHTML_DECIMAL_ENTITY_RANGE[5])))
+ {
+ // Convert UTF32 code to UTF8
+ Utf32ToUtf8(reinterpret_cast<const uint32_t* const>(&l), 1, reinterpret_cast<uint8_t*>(utf8));
+ result = true;
+ }
+ }
+ }
+ return result;
+}
+
+/**
+ * @brief Processes a particular tag for the required run (color-run, font-run or underlined-character-run).
+ *
+ * @tparam RunType Whether ColorRun , FontDescriptionRun or UnderlinedCharacterRun
+ *
+ * @param[in/out] runsContainer The container containing all the runs
+ * @param[in/out] styleStack The style stack
+ * @param[in] tag The tag we are currently processing
+ * @param[in] characterIndex The current character index
+ * @param[in/out] runIndex The run index
+ * @param[in/out] tagReference The tagReference we should increment/decrement
+ * @param[in] parameterSettingFunction This function will be called to set run specific parameters
+ */
+template<typename RunType>
+void ProcessTagForRun(
+ Vector<RunType>& runsContainer,
+ StyleStack<RunIndex>& styleStack,
+ const Tag& tag,
+ const CharacterIndex characterIndex,
+ RunIndex& runIndex,
+ int& tagReference,
+ std::function<void(const Tag&, RunType&)> parameterSettingFunction)
+{
+ if(!tag.isEndTag)
+ {
+ // Create a new run.
+ RunType run;
+ Initialize(run);
+
+ // Fill the run with the parameters.
+ run.characterRun.characterIndex = characterIndex;
+ parameterSettingFunction(tag, run);
+
+ // Push the run in the logical model.
+ runsContainer.PushBack(run);
+
+ // Push the index of the run into the stack.
+ styleStack.Push(runIndex);
+
+ // Point the next free run.
+ ++runIndex;
+
+ // Increase reference
+ ++tagReference;
+ }
+ else
+ {
+ if(tagReference > 0)
+ {
+ // Pop the top of the stack and set the number of characters of the run.
+ RunType& run = *(runsContainer.Begin() + styleStack.Pop());
+ run.characterRun.numberOfCharacters = characterIndex - run.characterRun.characterIndex;
+ --tagReference;
+ }
+ }
+}
+
+/**
+ * @brief Processes the item tag
+ *
+ * @param[in/out] markupProcessData The markup process data
+ * @param[in] tag The current tag
+ * @param[in/out] characterIndex The current character index
+ */
+void ProcessItemTag(
+ MarkupProcessData& markupProcessData,
+ const Tag tag,
+ CharacterIndex& characterIndex)
+{
+ if(tag.isEndTag)
+ {
+ // Create an embedded item instance.
+ EmbeddedItem item;
+ item.characterIndex = characterIndex;
+ ProcessEmbeddedItem(tag, item);
+
+ markupProcessData.items.PushBack(item);
+
+ // Insert white space character that will be replaced by the item.
+ markupProcessData.markupProcessedText.append(1u, WHITE_SPACE);
+ ++characterIndex;
+ }
+}
+
+/**
+ * @brief Processes the paragraph-tag
+ *
+ * @param[in/out] markupProcessData The markup process data
+ * @param[in] tag The current tag
+ * @param[in] isEndBuffer Whether the end of buffer
+ * @param[in/out] characterIndex The current character index
+ */
+void ProcessParagraphTag(
+ MarkupProcessData& markupProcessData,
+ const Tag tag,
+ bool isEndBuffer,
+ CharacterIndex& characterIndex)
+{
+ if((characterIndex > 0 &&
+ markupProcessData.markupProcessedText[characterIndex - 1u] != NEW_LINE) &&
+ (!(tag.isEndTag && isEndBuffer)))
+ {
+ // Insert new-line character at the start and end of paragraph.
+ markupProcessData.markupProcessedText.append(1u, NEW_LINE);
+ ++characterIndex;
+ }
+}
+
+/**
+ * @brief Processes the anchor tag
+ *
+ * @param[in/out] markupProcessData The markup process data
+ * @param[in] tag The current tag
+ * @param[in/out] characterIndex The current character index
+ */
+void ProcessAnchorTag(
+ MarkupProcessData& markupProcessData,
+ const Tag tag,
+ CharacterIndex& characterIndex)
+{
+ if(!tag.isEndTag)
+ {
+ // Create an anchor instance.
+ Anchor anchor;
+ anchor.startIndex = characterIndex;
+ anchor.endIndex = 0u;
+ ProcessAnchor(tag, anchor);
+ markupProcessData.anchors.PushBack(anchor);
+ }
+ else
+ {
+ // Update end index.
+ unsigned int count = markupProcessData.anchors.Count();
+ if(count > 0)
+ {
+ markupProcessData.anchors[count - 1].endIndex = characterIndex;
+ }
+ }
+}
+
+/**
+ * @brief Processes span tag for the color-run & font-run.
+ *
+ * @param[in] spanTag The tag we are currently processing
+ * @param[inout] spanStack The spans stack
+ * @param[inout] colorRuns The container containing all the color runs
+ * @param[inout] fontRuns The container containing all the font description runs
+ * @param[inout] underlinedCharacterRuns The container containing all the underlined character runs
+ * @param[inout] strikethroughCharacterRuns The container containing all the strikethroughed character runs
+ * @param[inout] colorRunIndex The color run index
+ * @param[inout] fontRunIndex The font run index
+ * @param[inout] underlinedCharacterRunIndex The underlined character run index
+ * @param[inout] strikethroughCharacterRunIndex The strikethroughed character run index
+ * @param[in] characterIndex The current character index
+ * @param[in] tagReference The tagReference we should increment/decrement
+ */
+void ProcessSpanForRun(
+ const Tag& spanTag,
+ StyleStack<Span>& spanStack,
+ Vector<ColorRun>& colorRuns,
+ Vector<FontDescriptionRun>& fontRuns,
+ Vector<UnderlinedCharacterRun>& underlinedCharacterRuns,
+ Vector<ColorRun>& backgroundColorRuns,
+ Vector<StrikethroughCharacterRun>& strikethroughCharacterRuns,
+ Vector<CharacterSpacingCharacterRun>& characterSpacingCharacterRuns,
+ RunIndex& colorRunIndex,
+ RunIndex& fontRunIndex,
+ RunIndex& underlinedCharacterRunIndex,
+ RunIndex& backgroundColorRunIndex,
+ RunIndex& strikethroughCharacterRunIndex,
+ RunIndex& characterSpacingCharacterRunIndex,
+ const CharacterIndex characterIndex,
+ int& tagReference)
+{
+ if(!spanTag.isEndTag)
+ {
+ // Create a new run.
+ ColorRun colorRun;
+ Initialize(colorRun);
+
+ FontDescriptionRun fontRun;
+ Initialize(fontRun);
+
+ UnderlinedCharacterRun underlinedCharacterRun;
+ Initialize(underlinedCharacterRun);
+
+ ColorRun backgroundColorRun;
+ Initialize(backgroundColorRun);
+
+ StrikethroughCharacterRun strikethroughCharacterRun;
+ Initialize(strikethroughCharacterRun);
+
+ CharacterSpacingCharacterRun characterSpacingCharacterRun;
+ Initialize(characterSpacingCharacterRun);
+
+ Span span;
+ Initialize(span);
+
+ // Fill the run with the parameters.
+ colorRun.characterRun.characterIndex = characterIndex;
+ fontRun.characterRun.characterIndex = characterIndex;
+ underlinedCharacterRun.characterRun.characterIndex = characterIndex;
+ backgroundColorRun.characterRun.characterIndex = characterIndex;
+ strikethroughCharacterRun.characterRun.characterIndex = characterIndex;
+ characterSpacingCharacterRun.characterRun.characterIndex = characterIndex;
+
+ span.colorRunIndex = colorRunIndex;
+ span.fontRunIndex = fontRunIndex;
+ span.underlinedCharacterRunIndex = underlinedCharacterRunIndex;
+ span.backgroundColorRunIndex = backgroundColorRunIndex;
+ span.strikethroughCharacterRunIndex = strikethroughCharacterRunIndex;
+ span.characterSpacingCharacterRunIndex = characterSpacingCharacterRunIndex;
+
+ ProcessSpanTag(spanTag,
+ colorRun,
+ fontRun,
+ underlinedCharacterRun,
+ backgroundColorRun,
+ strikethroughCharacterRun,
+ characterSpacingCharacterRun,
+ span.isColorDefined,
+ span.isFontDefined,
+ span.isUnderlinedCharacterDefined,
+ span.isBackgroundColorDefined,
+ span.isStrikethroughDefined,
+ span.isCharacterSpacingDefined);
+
+ // Push the span into the stack.
+ spanStack.Push(span);
+
+ // Point the next free run.
+ if(span.isColorDefined)
+ {
+ // Push the run in the logical model.
+ colorRuns.PushBack(colorRun);
+ ++colorRunIndex;
+ }
+
+ if(span.isFontDefined)
+ {
+ // Push the run in the logical model.
+ fontRuns.PushBack(fontRun);
+ ++fontRunIndex;
+ }
+
+ if(span.isUnderlinedCharacterDefined)
+ {
+ // Push the run in the logical model.
+ underlinedCharacterRuns.PushBack(underlinedCharacterRun);
+ ++underlinedCharacterRunIndex;
+ }
+
+ if(span.isBackgroundColorDefined)
+ {
+ // Push the run in the logical model.
+ backgroundColorRuns.PushBack(backgroundColorRun);
+ ++backgroundColorRunIndex;
+ }
+
+ if(span.isStrikethroughDefined)
+ {
+ // Push the run in the logical model.
+ strikethroughCharacterRuns.PushBack(strikethroughCharacterRun);
+ ++strikethroughCharacterRunIndex;
+ }
+
+ if(span.isCharacterSpacingDefined)
+ {
+ // Push the run in the logical model.
+ characterSpacingCharacterRuns.PushBack(characterSpacingCharacterRun);
+ ++characterSpacingCharacterRunIndex;
+ }
+
+ // Increase reference
+ ++tagReference;
+ }
+ else
+ {
+ if(tagReference > 0)
+ {
+ // Pop the top of the stack and set the number of characters of the run.
+ Span span = spanStack.Pop();
+
+ if(span.isColorDefined)
+ {
+ ColorRun& colorRun = *(colorRuns.Begin() + span.colorRunIndex);
+ colorRun.characterRun.numberOfCharacters = characterIndex - colorRun.characterRun.characterIndex;
+ }
+
+ if(span.isFontDefined)
+ {
+ FontDescriptionRun& fontRun = *(fontRuns.Begin() + span.fontRunIndex);
+ fontRun.characterRun.numberOfCharacters = characterIndex - fontRun.characterRun.characterIndex;
+ }
+
+ if(span.isUnderlinedCharacterDefined)
+ {
+ UnderlinedCharacterRun& underlinedCharacterRun = *(underlinedCharacterRuns.Begin() + span.underlinedCharacterRunIndex);
+ underlinedCharacterRun.characterRun.numberOfCharacters = characterIndex - underlinedCharacterRun.characterRun.characterIndex;
+ }
+
+ if(span.isBackgroundColorDefined)
+ {
+ ColorRun& backgroundColorRun = *(backgroundColorRuns.Begin() + span.backgroundColorRunIndex);
+ backgroundColorRun.characterRun.numberOfCharacters = characterIndex - backgroundColorRun.characterRun.characterIndex;
+ }
+
+ if(span.isStrikethroughDefined)
+ {
+ StrikethroughCharacterRun& strikethroughCharacterRun = *(strikethroughCharacterRuns.Begin() + span.strikethroughCharacterRunIndex);
+ strikethroughCharacterRun.characterRun.numberOfCharacters = characterIndex - strikethroughCharacterRun.characterRun.characterIndex;
+ }
+
+ if(span.isCharacterSpacingDefined)
+ {
+ CharacterSpacingCharacterRun& characterSpacingCharacterRun = *(characterSpacingCharacterRuns.Begin() + span.characterSpacingCharacterRunIndex);
+ characterSpacingCharacterRun.characterRun.numberOfCharacters = characterIndex - characterSpacingCharacterRun.characterRun.characterIndex;
+ }
+
+ --tagReference;
+ }
+ }
+}
+
+/**
+ * @brief Resizes the model's vectors
+ *
+ * @param[inout] markupProcessData The markup process data
+ * @param[in] fontRunIndex The font run index
+ * @param[in] colorRunIndex The color run index
+ * @param[in] underlinedCharacterRunIndex The underlined character run index
+ * @param[in] strikethroughCharacterRunIndex The strikethroughed character run index
+ * @param[in] backgroundRunIndex The background run index
+ * @param[in] boundedParagraphRunIndex The bounded paragraph run index
+ * @param[in] characterSpacingCharacterRunIndex The character-spacing character run index
+ *
+ */
+void ResizeModelVectors(MarkupProcessData& markupProcessData,
+ const RunIndex fontRunIndex,
+ const RunIndex colorRunIndex,
+ const RunIndex underlinedCharacterRunIndex,
+ const RunIndex strikethroughCharacterRunIndex,
+ const RunIndex backgroundRunIndex,
+ const RunIndex boundedParagraphRunIndex,
+ const RunIndex characterSpacingCharacterRunIndex)
+{
+ markupProcessData.fontRuns.Resize(fontRunIndex);
+ markupProcessData.colorRuns.Resize(colorRunIndex);
+ markupProcessData.underlinedCharacterRuns.Resize(underlinedCharacterRunIndex);
+ markupProcessData.strikethroughCharacterRuns.Resize(strikethroughCharacterRunIndex);
+ markupProcessData.backgroundColorRuns.Resize(backgroundRunIndex);
+ markupProcessData.boundedParagraphRuns.Resize(boundedParagraphRunIndex);
+ markupProcessData.characterSpacingCharacterRuns.Resize(characterSpacingCharacterRunIndex);
+
+#ifdef DEBUG_ENABLED
+ for(unsigned int i = 0; i < colorRunIndex; ++i)
+ {
+ ColorRun& run = markupProcessData.colorRuns[i];
+ DALI_LOG_INFO(gLogFilter, Debug::Verbose, "run[%d] index: %d, length: %d, color %f,%f,%f,%f\n", i, run.characterRun.characterIndex, run.characterRun.numberOfCharacters, run.color.r, run.color.g, run.color.b, run.color.a);
+ }
+#endif
+}
+
+/**
+ * @brief Processes the markup string buffer
+ *
+ * @param[in/out] markupProcessData The markup process data
+ * @param[in/out] markupStringBuffer The markup string buffer pointer
+ * @param[in] markupStringEndBuffer The markup string end buffer pointer
+ * @param[in/out] characterIndex The current character index
+ */
+void ProcessMarkupStringBuffer(
+ MarkupProcessData& markupProcessData,
+ const char*& markupStringBuffer,
+ const char* const markupStringEndBuffer,
+ CharacterIndex& characterIndex)
+{
+ unsigned char character = *markupStringBuffer;
+ const char* markupBuffer = markupStringBuffer;
+ unsigned char count = GetUtf8Length(character);
+ char utf8[8];
+
+ if((BACK_SLASH == character) && (markupStringBuffer + 1u < markupStringEndBuffer))
+ {
+ // Adding < , > or & special character.
+ const unsigned char nextCharacter = *(markupStringBuffer + 1u);
+ if((LESS_THAN == nextCharacter) || (GREATER_THAN == nextCharacter) || (AMPERSAND == nextCharacter))
+ {
+ character = nextCharacter;
+ ++markupStringBuffer;
+
+ count = GetUtf8Length(character);
+ markupBuffer = markupStringBuffer;
+ }
+ }
+ else // checking if contains XHTML entity or not
+ {
+ const unsigned int len = GetXHTMLEntityLength(markupStringBuffer, markupStringEndBuffer);
+
+ // Parse markupStringTxt if it contains XHTML Entity between '&' and ';'
+ if(len > 0)
+ {
+ char* entityCode = NULL;
+ bool result = false;
+ count = 0;
+
+ // Checking if XHTML Numeric Entity
+ if(HASH == *(markupBuffer + 1u))
+ {
+ entityCode = &utf8[0];
+ // markupBuffer is currently pointing to '&'. By adding 2u to markupBuffer it will point to numeric string by skipping "&#'
+ result = XHTMLNumericEntityToUtf8((markupBuffer + 2u), entityCode);
+ }
+ else // Checking if XHTML Named Entity
+ {
+ entityCode = const_cast<char*>(NamedEntityToUtf8(markupBuffer, len));
+ result = (entityCode != NULL);
+ }
+ if(result)
+ {
+ markupBuffer = entityCode; //utf8 text assigned to markupBuffer
+ character = markupBuffer[0];
+ }
+ else
+ {
+ DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Not valid XHTML entity : (%.*s) \n", len, markupBuffer);
+ markupBuffer = NULL;
+ }
+ }
+ else // in case string conatins Start of XHTML Entity('&') but not its end character(';')
+ {
+ if(character == AMPERSAND)
+ {
+ markupBuffer = NULL;
+ DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Not Well formed XHTML content \n");
+ }
+ }
+ }
+
+ if(markupBuffer != NULL)
+ {
+ const unsigned char numberOfBytes = GetUtf8Length(character);
+ markupProcessData.markupProcessedText.push_back(character);
+
+ for(unsigned char i = 1u; i < numberOfBytes; ++i)
+ {
+ ++markupBuffer;
+ markupProcessData.markupProcessedText.push_back(*markupBuffer);
+ }
+
+ ++characterIndex;
+ markupStringBuffer += count;
+ }
+}
+