2 * Copyright (c) 2022 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/markup-processor.h>
22 #include <dali/integration-api/debug.h>
23 #include <climits> // for ULONG_MAX
27 #include <dali-toolkit/internal/text/character-set-conversion.h>
28 #include <dali-toolkit/internal/text/markup-processor/markup-processor-anchor.h>
29 #include <dali-toolkit/internal/text/markup-processor/markup-processor-background.h>
30 #include <dali-toolkit/internal/text/markup-processor/markup-processor-character-spacing.h>
31 #include <dali-toolkit/internal/text/markup-processor/markup-processor-color.h>
32 #include <dali-toolkit/internal/text/markup-processor/markup-processor-embedded-item.h>
33 #include <dali-toolkit/internal/text/markup-processor/markup-processor-font.h>
34 #include <dali-toolkit/internal/text/markup-processor/markup-processor-helper-functions.h>
35 #include <dali-toolkit/internal/text/markup-processor/markup-processor-paragraph.h>
36 #include <dali-toolkit/internal/text/markup-processor/markup-processor-span.h>
37 #include <dali-toolkit/internal/text/markup-processor/markup-processor-strikethrough.h>
38 #include <dali-toolkit/internal/text/markup-processor/markup-processor-underline.h>
39 #include <dali-toolkit/internal/text/markup-tags-and-attributes.h>
40 #include <dali-toolkit/internal/text/xhtml-entities.h>
50 const char LESS_THAN = '<';
51 const char GREATER_THAN = '>';
52 const char EQUAL = '=';
53 const char QUOTATION_MARK = '\'';
54 const char SLASH = '/';
55 const char BACK_SLASH = '\\';
56 const char AMPERSAND = '&';
57 const char HASH = '#';
58 const char SEMI_COLON = ';';
59 const char CHAR_ARRAY_END = '\0';
60 const char HEX_CODE = 'x';
62 const char WHITE_SPACE = 0x20; // ASCII value of the white space.
63 const char NEW_LINE = 0x0A; // ASCII value of the newline.
65 // Range 1 0x0u < XHTML_DECIMAL_ENTITY_RANGE <= 0xD7FFu
66 // Range 2 0xE000u < XHTML_DECIMAL_ENTITY_RANGE <= 0xFFFDu
67 // Range 3 0x10000u < XHTML_DECIMAL_ENTITY_RANGE <= 0x10FFFFu
68 const unsigned long XHTML_DECIMAL_ENTITY_RANGE[] = {0x0u, 0xD7FFu, 0xE000u, 0xFFFDu, 0x10000u, 0x10FFFFu};
70 // The MAX_NUM_OF_ATTRIBUTES is the number of attributes in span tag "markup-processor-span.cpp". Because it contains the maximum number of attributes in all tags.
71 const unsigned int MAX_NUM_OF_ATTRIBUTES = 14u; ///< The span tag has the 'font-family', 'font-size' 'font-weight', 'font-width', 'font-slant','text-color', 'u-color', 'u-height','u-type','u-dash-gap', 'u-dash-width', 's-color', 's-height' and 'char-space-value' attrubutes.
72 const unsigned int DEFAULT_VECTOR_SIZE = 16u; ///< Default size of run vectors.
74 #if defined(DEBUG_ENABLED)
75 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_MARKUP_PROCESSOR");
78 typedef VectorBase::SizeType RunIndex;
81 * @brief Struct used to retrieve the style runs from the mark-up string.
83 template<typename StyleStackType>
86 Vector<StyleStackType> stack; ///< Use a vector as a style stack.
87 unsigned int topIndex; ///< Points the top of the stack.
93 stack.Resize(DEFAULT_VECTOR_SIZE);
96 void Push(StyleStackType item)
98 // Check if there is space inside the style stack.
99 const VectorBase::SizeType size = stack.Count();
102 // Resize the style stack.
103 stack.Resize(2u * size);
106 // Set the item in the top of the stack.
107 *(stack.Begin() + topIndex) = item;
109 // Reposition the pointer to the top of the stack.
115 // Pop the top of the stack.
117 return *(stack.Begin() + topIndex);
122 * @brief Struct used to retrieve spans from the mark-up string.
126 RunIndex colorRunIndex;
127 RunIndex fontRunIndex;
128 RunIndex underlinedCharacterRunIndex;
129 RunIndex backgroundColorRunIndex;
130 RunIndex strikethroughCharacterRunIndex;
131 RunIndex characterSpacingCharacterRunIndex;
135 bool isUnderlinedCharacterDefined;
136 bool isBackgroundColorDefined;
137 bool isStrikethroughDefined;
138 bool isCharacterSpacingDefined;
142 * @brief Struct used to retrieve anchors from the mark-up string.
144 struct AnchorForStack
146 RunIndex colorRunIndex;
147 RunIndex underlinedCharacterRunIndex;
151 * @brief Initializes a font run description to its defaults.
153 * @param[in,out] fontRun The font description run to initialize.
155 void Initialize(FontDescriptionRun& fontRun)
157 fontRun.characterRun.characterIndex = 0u;
158 fontRun.characterRun.numberOfCharacters = 0u;
159 fontRun.familyName = NULL;
160 fontRun.familyLength = 0u;
161 fontRun.weight = TextAbstraction::FontWeight::NORMAL;
162 fontRun.width = TextAbstraction::FontWidth::NORMAL;
163 fontRun.slant = TextAbstraction::FontSlant::NORMAL;
165 fontRun.familyDefined = false;
166 fontRun.weightDefined = false;
167 fontRun.widthDefined = false;
168 fontRun.slantDefined = false;
169 fontRun.sizeDefined = false;
173 * @brief Initializes a color run description to its defaults.
175 * @param[in,out] colorRun The font description run to initialize.
177 void Initialize(ColorRun& colorRun)
179 colorRun.characterRun.characterIndex = 0u;
180 colorRun.characterRun.numberOfCharacters = 0u;
184 * @brief Initializes a underlined character run to its defaults.
186 * @param[in,out] underlinedCharacterRun The underelined character run to initialize.
188 void Initialize(UnderlinedCharacterRun& underlinedCharacterRun)
190 underlinedCharacterRun.characterRun.characterIndex = 0u;
191 underlinedCharacterRun.characterRun.numberOfCharacters = 0u;
195 * @brief Initializes a span to its defaults.
197 * @param[in,out] span The span to be initialized.
199 void Initialize(Span& span)
201 span.colorRunIndex = 0u;
202 span.isColorDefined = false;
204 span.fontRunIndex = 0u;
205 span.isFontDefined = false;
207 span.underlinedCharacterRunIndex = 0u;
208 span.isUnderlinedCharacterDefined = false;
209 span.backgroundColorRunIndex = 0u;
210 span.isBackgroundColorDefined = false;
213 span.strikethroughCharacterRunIndex = 0u;
214 span.isStrikethroughDefined = false;
217 span.characterSpacingCharacterRunIndex = 0u;
218 span.isCharacterSpacingDefined = false;
222 * @brief Initializes a anchor to its defaults.
224 * @param[in,out] anchor The anchor to be initialized.
226 void Initialize(AnchorForStack& anchor)
228 anchor.colorRunIndex = 0u;
229 anchor.underlinedCharacterRunIndex = 0u;
233 * @brief Initializes a strikethrough character run to its defaults.
235 * @param[in,out] strikethroughCharacterRun The strikethrough character run to initialize.
237 void Initialize(StrikethroughCharacterRun& strikethroughCharacterRun)
239 strikethroughCharacterRun.characterRun.characterIndex = 0u;
240 strikethroughCharacterRun.characterRun.numberOfCharacters = 0u;
241 strikethroughCharacterRun.properties.colorDefined = false;
245 * @brief Initializes a bounded-paragraph character run to its defaults.
247 * @param[in,out] boundedParagraphRun The bounded paragraphRun run to initialize.
249 void Initialize(BoundedParagraphRun& boundedParagraphRun)
251 boundedParagraphRun.characterRun.characterIndex = 0u;
252 boundedParagraphRun.characterRun.numberOfCharacters = 0u;
256 * @brief Initializes a character-spacing run to its defaults.
258 * @param[in,out] characterSpacingCharacterRun The character-spacing run to initialize.
260 void Initialize(CharacterSpacingCharacterRun& characterSpacingCharacterRun)
262 characterSpacingCharacterRun.characterRun.characterIndex = 0u;
263 characterSpacingCharacterRun.characterRun.numberOfCharacters = 0u;
264 characterSpacingCharacterRun.value = 0.0f;
268 * @brief Splits the tag string into the tag name and its attributes.
270 * The attributes are stored in a vector in the tag.
272 * @param[in,out] tag The tag.
274 void ParseAttributes(Tag& tag)
276 if(tag.buffer == NULL)
281 tag.attributes.Resize(MAX_NUM_OF_ATTRIBUTES);
283 // Find first the tag name.
284 bool isQuotationOpen = false;
286 const char* tagBuffer = tag.buffer;
287 const char* const tagEndBuffer = tagBuffer + tag.length;
289 for(; tagBuffer < tagEndBuffer; ++tagBuffer)
291 const char character = *tagBuffer;
292 if(WHITE_SPACE < character)
298 // Stops counting the length of the tag when a white space is found.
299 // @note a white space is the WHITE_SPACE character and anything below as 'tab', 'return' or 'control characters'.
303 SkipWhiteSpace(tagBuffer, tagEndBuffer);
305 // Find the attributes.
306 unsigned int attributeIndex = 0u;
307 const char* nameBuffer = NULL;
308 const char* valueBuffer = NULL;
309 Length nameLength = 0u;
310 Length valueLength = 0u;
312 bool addToNameValue = true;
313 Length numberOfWhiteSpace = 0u;
314 for(; tagBuffer < tagEndBuffer; ++tagBuffer)
316 const char character = *tagBuffer;
317 if((WHITE_SPACE >= character) && !isQuotationOpen)
319 if(NULL != valueBuffer)
321 // Remove white spaces at the end of the value.
322 valueLength -= numberOfWhiteSpace;
325 if((NULL != nameBuffer) && (NULL != valueBuffer))
327 // Every time a white space is found, a new attribute is created and stored in the attributes vector.
328 Attribute& attribute = *(tag.attributes.Begin() + attributeIndex);
331 attribute.nameBuffer = nameBuffer;
332 attribute.valueBuffer = valueBuffer;
333 attribute.nameLength = nameLength;
334 attribute.valueLength = valueLength;
341 addToNameValue = true; // next read characters will be added to the name.
344 else if(EQUAL == character) // '='
352 addToNameValue = false; // next read characters will be added to the value.
353 SkipWhiteSpace(tagBuffer, tagEndBuffer);
356 else if(QUOTATION_MARK == character) // '\''
358 // Do not add quotation marks to neither name nor value.
359 isQuotationOpen = !isQuotationOpen;
364 SkipWhiteSpace(tagBuffer, tagEndBuffer);
370 // Adds characters to the name or the value.
373 if(NULL == nameBuffer)
375 nameBuffer = tagBuffer;
383 if(WHITE_SPACE >= character)
385 ++numberOfWhiteSpace;
389 numberOfWhiteSpace = 0u;
392 if(NULL == valueBuffer)
394 valueBuffer = tagBuffer;
401 if(NULL != valueBuffer)
403 // Remove white spaces at the end of the value.
404 valueLength -= numberOfWhiteSpace;
407 if((NULL != nameBuffer) && (NULL != valueBuffer))
409 // Checks if the last attribute needs to be added.
410 Attribute& attribute = *(tag.attributes.Begin() + attributeIndex);
413 attribute.nameBuffer = nameBuffer;
414 attribute.valueBuffer = valueBuffer;
415 attribute.nameLength = nameLength;
416 attribute.valueLength = valueLength;
419 // Resize the vector of attributes.
420 tag.attributes.Resize(attributeIndex);
424 * @brief It parses a tag and its attributes if the given iterator @e it is pointing at a tag beginning.
426 * @param[in,out] markupStringBuffer The mark-up string buffer. It's a const iterator pointing the current character.
427 * @param[in] markupStringEndBuffer Pointer to one character after the end of the mark-up string buffer.
428 * @param[out] tag The tag with its attributes.
430 * @return @e true if the iterator @e it is pointing a mark-up tag. Otherwise @e false.
432 bool IsTag(const char*& markupStringBuffer,
433 const char* const markupStringEndBuffer,
437 bool isQuotationOpen = false;
438 bool attributesFound = false;
439 tag.isEndTag = false;
440 bool isPreviousLessThan = false;
441 bool isPreviousSlash = false;
443 const char character = *markupStringBuffer;
444 if(LESS_THAN == character) // '<'
448 isPreviousLessThan = true;
450 // if the iterator is pointing to a '<' character, then check if it's a mark-up tag is needed.
451 ++markupStringBuffer;
452 if(markupStringBuffer < markupStringEndBuffer)
454 SkipWhiteSpace(markupStringBuffer, markupStringEndBuffer);
456 for(; (!isTag) && (markupStringBuffer < markupStringEndBuffer); ++markupStringBuffer)
458 const char character = *markupStringBuffer;
460 if(!isQuotationOpen && (SLASH == character)) // '/'
462 if(isPreviousLessThan)
468 // if the tag has a '/' it may be an end tag.
469 isPreviousSlash = true;
472 isPreviousLessThan = false;
473 if((markupStringBuffer + 1u < markupStringEndBuffer) && (WHITE_SPACE >= *(markupStringBuffer + 1u)))
475 ++markupStringBuffer;
476 SkipWhiteSpace(markupStringBuffer, markupStringEndBuffer);
477 --markupStringBuffer;
480 else if(GREATER_THAN == character) // '>'
488 isPreviousSlash = false;
489 isPreviousLessThan = false;
491 else if(QUOTATION_MARK == character)
493 isQuotationOpen = !isQuotationOpen;
496 isPreviousSlash = false;
497 isPreviousLessThan = false;
499 else if(WHITE_SPACE >= character) // ' '
501 // If the tag contains white spaces then it may have attributes.
504 attributesFound = true;
510 if(NULL == tag.buffer)
512 tag.buffer = markupStringBuffer;
515 // If it's not any of the 'special' characters then just add it to the tag string.
518 isPreviousSlash = false;
519 isPreviousLessThan = false;
524 // If the tag string has white spaces, then parse the attributes is needed.
527 ParseAttributes(tag);
535 * @brief Returns length of XHTML entity by parsing the text. It also determines if it is XHTML entity or not.
537 * @param[in] markupStringBuffer The mark-up string buffer. It's a const iterator pointing the current character.
538 * @param[in] markupStringEndBuffer Pointing to end of mark-up string buffer.
540 * @return Length of markupText in case of XHTML entity otherwise return 0.
542 unsigned int GetXHTMLEntityLength(const char*& markupStringBuffer,
543 const char* const markupStringEndBuffer)
545 char character = *markupStringBuffer;
546 if(AMPERSAND == character) // '&'
548 // if the iterator is pointing to a '&' character, then check for ';' to find end to XHTML entity.
549 ++markupStringBuffer;
550 if(markupStringBuffer < markupStringEndBuffer)
552 unsigned int len = 1u;
553 for(; markupStringBuffer < markupStringEndBuffer; ++markupStringBuffer)
555 character = *markupStringBuffer;
557 if(SEMI_COLON == character) // ';'
559 // found end of XHTML entity
560 ++markupStringBuffer;
563 else if((AMPERSAND == character) || (BACK_SLASH == character) || (LESS_THAN == character))
574 * @brief It parses a XHTML string which has hex/decimal entity and fill its corresponging utf-8 string.
576 * @param[in] markupText The mark-up text buffer.
577 * @param[out] utf-8 text Corresponding to markup Text
579 * @return true if string is successfully parsed otherwise false
581 bool XHTMLNumericEntityToUtf8(const char* markupText, char* utf8)
585 if(NULL != markupText)
589 // check if hex or decimal entity
590 if((CHAR_ARRAY_END != *markupText) && (HEX_CODE == *markupText))
597 unsigned long l = strtoul(markupText, &end, (isHex ? 16 : 10)); // l contains UTF-32 code in case of correct XHTML entity
599 // check for valid XHTML numeric entities (between '#' or "#x" and ';')
600 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'
602 /* characters XML 1.1 permits */
603 if(((XHTML_DECIMAL_ENTITY_RANGE[0] < l) && (l <= XHTML_DECIMAL_ENTITY_RANGE[1])) ||
604 ((XHTML_DECIMAL_ENTITY_RANGE[2] <= l) && (l <= XHTML_DECIMAL_ENTITY_RANGE[3])) ||
605 ((XHTML_DECIMAL_ENTITY_RANGE[4] <= l) && (l <= XHTML_DECIMAL_ENTITY_RANGE[5])))
607 // Convert UTF32 code to UTF8
608 Utf32ToUtf8(reinterpret_cast<const uint32_t* const>(&l), 1, reinterpret_cast<uint8_t*>(utf8));
617 * @brief Processes a particular tag for the required run (color-run, font-run or underlined-character-run).
619 * @tparam RunType Whether ColorRun , FontDescriptionRun or UnderlinedCharacterRun
621 * @param[in/out] runsContainer The container containing all the runs
622 * @param[in/out] styleStack The style stack
623 * @param[in] tag The tag we are currently processing
624 * @param[in] characterIndex The current character index
625 * @param[in/out] runIndex The run index
626 * @param[in/out] tagReference The tagReference we should increment/decrement
627 * @param[in] parameterSettingFunction This function will be called to set run specific parameters
629 template<typename RunType>
630 void ProcessTagForRun(
631 Vector<RunType>& runsContainer,
632 StyleStack<RunIndex>& styleStack,
634 const CharacterIndex characterIndex,
637 std::function<void(const Tag&, RunType&)> parameterSettingFunction)
645 // Fill the run with the parameters.
646 run.characterRun.characterIndex = characterIndex;
647 parameterSettingFunction(tag, run);
649 // Push the run in the logical model.
650 runsContainer.PushBack(run);
652 // Push the index of the run into the stack.
653 styleStack.Push(runIndex);
655 // Point the next free run.
658 // Increase reference
665 // Pop the top of the stack and set the number of characters of the run.
666 RunType& run = *(runsContainer.Begin() + styleStack.Pop());
667 run.characterRun.numberOfCharacters = characterIndex - run.characterRun.characterIndex;
674 * @brief Processes the item tag
676 * @param[in/out] markupProcessData The markup process data
677 * @param[in] tag The current tag
678 * @param[in/out] characterIndex The current character index
681 MarkupProcessData& markupProcessData,
683 CharacterIndex& characterIndex)
687 // Create an embedded item instance.
689 item.characterIndex = characterIndex;
690 ProcessEmbeddedItem(tag, item);
692 markupProcessData.items.PushBack(item);
694 // Insert white space character that will be replaced by the item.
695 markupProcessData.markupProcessedText.append(1u, WHITE_SPACE);
701 * @brief Processes the paragraph-tag
703 * @param[in/out] markupProcessData The markup process data
704 * @param[in] tag The current tag
705 * @param[in] isEndBuffer Whether the end of buffer
706 * @param[in/out] characterIndex The current character index
708 void ProcessParagraphTag(
709 MarkupProcessData& markupProcessData,
712 CharacterIndex& characterIndex)
714 if((characterIndex > 0 &&
715 markupProcessData.markupProcessedText[characterIndex - 1u] != NEW_LINE) &&
716 (!(tag.isEndTag && isEndBuffer)))
718 // Insert new-line character at the start and end of paragraph.
719 markupProcessData.markupProcessedText.append(1u, NEW_LINE);
725 * @brief Processes span tag for the color-run & font-run.
727 * @param[in] spanTag The tag we are currently processing
728 * @param[inout] spanStack The spans stack
729 * @param[inout] colorRuns The container containing all the color runs
730 * @param[inout] fontRuns The container containing all the font description runs
731 * @param[inout] underlinedCharacterRuns The container containing all the underlined character runs
732 * @param[inout] strikethroughCharacterRuns The container containing all the strikethroughed character runs
733 * @param[inout] colorRunIndex The color run index
734 * @param[inout] fontRunIndex The font run index
735 * @param[inout] underlinedCharacterRunIndex The underlined character run index
736 * @param[inout] strikethroughCharacterRunIndex The strikethroughed character run index
737 * @param[in] characterIndex The current character index
738 * @param[in] tagReference The tagReference we should increment/decrement
740 void ProcessSpanForRun(
742 StyleStack<Span>& spanStack,
743 Vector<ColorRun>& colorRuns,
744 Vector<FontDescriptionRun>& fontRuns,
745 Vector<UnderlinedCharacterRun>& underlinedCharacterRuns,
746 Vector<ColorRun>& backgroundColorRuns,
747 Vector<StrikethroughCharacterRun>& strikethroughCharacterRuns,
748 Vector<CharacterSpacingCharacterRun>& characterSpacingCharacterRuns,
749 RunIndex& colorRunIndex,
750 RunIndex& fontRunIndex,
751 RunIndex& underlinedCharacterRunIndex,
752 RunIndex& backgroundColorRunIndex,
753 RunIndex& strikethroughCharacterRunIndex,
754 RunIndex& characterSpacingCharacterRunIndex,
755 const CharacterIndex characterIndex,
758 if(!spanTag.isEndTag)
762 Initialize(colorRun);
764 FontDescriptionRun fontRun;
767 UnderlinedCharacterRun underlinedCharacterRun;
768 Initialize(underlinedCharacterRun);
770 ColorRun backgroundColorRun;
771 Initialize(backgroundColorRun);
773 StrikethroughCharacterRun strikethroughCharacterRun;
774 Initialize(strikethroughCharacterRun);
776 CharacterSpacingCharacterRun characterSpacingCharacterRun;
777 Initialize(characterSpacingCharacterRun);
782 // Fill the run with the parameters.
783 colorRun.characterRun.characterIndex = characterIndex;
784 fontRun.characterRun.characterIndex = characterIndex;
785 underlinedCharacterRun.characterRun.characterIndex = characterIndex;
786 backgroundColorRun.characterRun.characterIndex = characterIndex;
787 strikethroughCharacterRun.characterRun.characterIndex = characterIndex;
788 characterSpacingCharacterRun.characterRun.characterIndex = characterIndex;
790 span.colorRunIndex = colorRunIndex;
791 span.fontRunIndex = fontRunIndex;
792 span.underlinedCharacterRunIndex = underlinedCharacterRunIndex;
793 span.backgroundColorRunIndex = backgroundColorRunIndex;
794 span.strikethroughCharacterRunIndex = strikethroughCharacterRunIndex;
795 span.characterSpacingCharacterRunIndex = characterSpacingCharacterRunIndex;
797 ProcessSpanTag(spanTag,
800 underlinedCharacterRun,
802 strikethroughCharacterRun,
803 characterSpacingCharacterRun,
806 span.isUnderlinedCharacterDefined,
807 span.isBackgroundColorDefined,
808 span.isStrikethroughDefined,
809 span.isCharacterSpacingDefined);
811 // Push the span into the stack.
812 spanStack.Push(span);
814 // Point the next free run.
815 if(span.isColorDefined)
817 // Push the run in the logical model.
818 colorRuns.PushBack(colorRun);
822 if(span.isFontDefined)
824 // Push the run in the logical model.
825 fontRuns.PushBack(fontRun);
829 if(span.isUnderlinedCharacterDefined)
831 // Push the run in the logical model.
832 underlinedCharacterRuns.PushBack(underlinedCharacterRun);
833 ++underlinedCharacterRunIndex;
836 if(span.isBackgroundColorDefined)
838 // Push the run in the logical model.
839 backgroundColorRuns.PushBack(backgroundColorRun);
840 ++backgroundColorRunIndex;
843 if(span.isStrikethroughDefined)
845 // Push the run in the logical model.
846 strikethroughCharacterRuns.PushBack(strikethroughCharacterRun);
847 ++strikethroughCharacterRunIndex;
850 if(span.isCharacterSpacingDefined)
852 // Push the run in the logical model.
853 characterSpacingCharacterRuns.PushBack(characterSpacingCharacterRun);
854 ++characterSpacingCharacterRunIndex;
857 // Increase reference
864 // Pop the top of the stack and set the number of characters of the run.
865 Span span = spanStack.Pop();
867 if(span.isColorDefined)
869 ColorRun& colorRun = *(colorRuns.Begin() + span.colorRunIndex);
870 colorRun.characterRun.numberOfCharacters = characterIndex - colorRun.characterRun.characterIndex;
873 if(span.isFontDefined)
875 FontDescriptionRun& fontRun = *(fontRuns.Begin() + span.fontRunIndex);
876 fontRun.characterRun.numberOfCharacters = characterIndex - fontRun.characterRun.characterIndex;
879 if(span.isUnderlinedCharacterDefined)
881 UnderlinedCharacterRun& underlinedCharacterRun = *(underlinedCharacterRuns.Begin() + span.underlinedCharacterRunIndex);
882 underlinedCharacterRun.characterRun.numberOfCharacters = characterIndex - underlinedCharacterRun.characterRun.characterIndex;
885 if(span.isBackgroundColorDefined)
887 ColorRun& backgroundColorRun = *(backgroundColorRuns.Begin() + span.backgroundColorRunIndex);
888 backgroundColorRun.characterRun.numberOfCharacters = characterIndex - backgroundColorRun.characterRun.characterIndex;
891 if(span.isStrikethroughDefined)
893 StrikethroughCharacterRun& strikethroughCharacterRun = *(strikethroughCharacterRuns.Begin() + span.strikethroughCharacterRunIndex);
894 strikethroughCharacterRun.characterRun.numberOfCharacters = characterIndex - strikethroughCharacterRun.characterRun.characterIndex;
897 if(span.isCharacterSpacingDefined)
899 CharacterSpacingCharacterRun& characterSpacingCharacterRun = *(characterSpacingCharacterRuns.Begin() + span.characterSpacingCharacterRunIndex);
900 characterSpacingCharacterRun.characterRun.numberOfCharacters = characterIndex - characterSpacingCharacterRun.characterRun.characterIndex;
909 * @brief Processes anchor tag for the color-run & underline-run.
911 * @param[in,out] markupProcessData The markup process data
912 * @param[in] markupPropertyData The markup property data
913 * @param[in] tag The tag we are currently processing
914 * @param[in,out] anchorStack The anchors stack
915 * @param[in,out] colorRuns The container containing all the color runs
916 * @param[in,out] underlinedCharacterRuns The container containing all the underlined character runs
917 * @param[in,out] colorRunIndex The color run index
918 * @param[in,out] underlinedCharacterRunIndex The underlined character run index
919 * @param[in] characterIndex The current character index
920 * @param[in] tagReference The tagReference we should increment/decrement
922 void ProcessAnchorForRun(
923 MarkupProcessData& markupProcessData,
924 MarkupPropertyData& markupPropertyData,
926 StyleStack<AnchorForStack>& anchorStack,
927 Vector<ColorRun>& colorRuns,
928 Vector<UnderlinedCharacterRun>& underlinedCharacterRuns,
929 RunIndex& colorRunIndex,
930 RunIndex& underlinedCharacterRunIndex,
931 const CharacterIndex characterIndex,
936 // Create an anchor instance.
938 anchor.href = nullptr;
939 anchor.startIndex = characterIndex;
940 anchor.endIndex = 0u;
941 anchor.colorRunIndex = colorRunIndex;
942 anchor.underlinedCharacterRunIndex = underlinedCharacterRunIndex;
943 anchor.markupClickedColor = markupPropertyData.anchorClickedColor;
947 Initialize(colorRun);
949 UnderlinedCharacterRun underlinedCharacterRun;
950 Initialize(underlinedCharacterRun);
952 AnchorForStack anchorForStack;
953 Initialize(anchorForStack);
955 // Fill the run with the parameters.
956 colorRun.characterRun.characterIndex = characterIndex;
957 underlinedCharacterRun.characterRun.characterIndex = characterIndex;
959 anchorForStack.colorRunIndex = colorRunIndex;
960 anchorForStack.underlinedCharacterRunIndex = underlinedCharacterRunIndex;
962 // Init default color
963 colorRun.color = markupPropertyData.anchorColor;
964 underlinedCharacterRun.properties.color = markupPropertyData.anchorColor;
965 underlinedCharacterRun.properties.colorDefined = true;
967 ProcessAnchorTag(tag, anchor, colorRun, underlinedCharacterRun);
969 markupProcessData.anchors.PushBack(anchor);
971 // Push the anchor into the stack.
972 anchorStack.Push(anchorForStack);
974 // Push the run in the logical model.
975 colorRuns.PushBack(colorRun);
978 // Push the run in the logical model.
979 underlinedCharacterRuns.PushBack(underlinedCharacterRun);
980 ++underlinedCharacterRunIndex;
982 // Increase reference
990 unsigned int count = markupProcessData.anchors.Count();
993 markupProcessData.anchors[count - 1].endIndex = characterIndex;
996 // Pop the top of the stack and set the number of characters of the run.
997 AnchorForStack anchorForStack = anchorStack.Pop();
999 ColorRun& colorRun = *(colorRuns.Begin() + anchorForStack.colorRunIndex);
1000 colorRun.characterRun.numberOfCharacters = characterIndex - colorRun.characterRun.characterIndex;
1002 UnderlinedCharacterRun& underlinedCharacterRun = *(underlinedCharacterRuns.Begin() + anchorForStack.underlinedCharacterRunIndex);
1003 underlinedCharacterRun.characterRun.numberOfCharacters = characterIndex - underlinedCharacterRun.characterRun.characterIndex;
1011 * @brief Resizes the model's vectors
1013 * @param[inout] markupProcessData The markup process data
1014 * @param[in] fontRunIndex The font run index
1015 * @param[in] colorRunIndex The color run index
1016 * @param[in] underlinedCharacterRunIndex The underlined character run index
1017 * @param[in] strikethroughCharacterRunIndex The strikethroughed character run index
1018 * @param[in] backgroundRunIndex The background run index
1019 * @param[in] boundedParagraphRunIndex The bounded paragraph run index
1020 * @param[in] characterSpacingCharacterRunIndex The character-spacing character run index
1023 void ResizeModelVectors(MarkupProcessData& markupProcessData,
1024 const RunIndex fontRunIndex,
1025 const RunIndex colorRunIndex,
1026 const RunIndex underlinedCharacterRunIndex,
1027 const RunIndex strikethroughCharacterRunIndex,
1028 const RunIndex backgroundRunIndex,
1029 const RunIndex boundedParagraphRunIndex,
1030 const RunIndex characterSpacingCharacterRunIndex)
1032 markupProcessData.fontRuns.Resize(fontRunIndex);
1033 markupProcessData.colorRuns.Resize(colorRunIndex);
1034 markupProcessData.underlinedCharacterRuns.Resize(underlinedCharacterRunIndex);
1035 markupProcessData.strikethroughCharacterRuns.Resize(strikethroughCharacterRunIndex);
1036 markupProcessData.backgroundColorRuns.Resize(backgroundRunIndex);
1037 markupProcessData.boundedParagraphRuns.Resize(boundedParagraphRunIndex);
1038 markupProcessData.characterSpacingCharacterRuns.Resize(characterSpacingCharacterRunIndex);
1040 #ifdef DEBUG_ENABLED
1041 if(gLogFilter->IsEnabledFor(Debug::Verbose))
1043 for(uint32_t i = 0; i < colorRunIndex; ++i)
1045 ColorRun& run = markupProcessData.colorRuns[i];
1046 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);
1053 * @brief Processes the markup string buffer
1055 * @param[in/out] markupProcessData The markup process data
1056 * @param[in/out] markupStringBuffer The markup string buffer pointer
1057 * @param[in] markupStringEndBuffer The markup string end buffer pointer
1058 * @param[in/out] characterIndex The current character index
1060 void ProcessMarkupStringBuffer(
1061 MarkupProcessData& markupProcessData,
1062 const char*& markupStringBuffer,
1063 const char* const markupStringEndBuffer,
1064 CharacterIndex& characterIndex)
1066 unsigned char character = *markupStringBuffer;
1067 const char* markupBuffer = markupStringBuffer;
1068 unsigned char count = GetUtf8Length(character);
1071 if((BACK_SLASH == character) && (markupStringBuffer + 1u < markupStringEndBuffer))
1073 // Adding < , > or & special character.
1074 const unsigned char nextCharacter = *(markupStringBuffer + 1u);
1075 if((LESS_THAN == nextCharacter) || (GREATER_THAN == nextCharacter) || (AMPERSAND == nextCharacter))
1077 character = nextCharacter;
1078 ++markupStringBuffer;
1080 count = GetUtf8Length(character);
1081 markupBuffer = markupStringBuffer;
1084 else // checking if contains XHTML entity or not
1086 const unsigned int len = GetXHTMLEntityLength(markupStringBuffer, markupStringEndBuffer);
1088 // Parse markupStringTxt if it contains XHTML Entity between '&' and ';'
1091 char* entityCode = NULL;
1092 bool result = false;
1095 // Checking if XHTML Numeric Entity
1096 if(HASH == *(markupBuffer + 1u))
1098 entityCode = &utf8[0];
1099 // markupBuffer is currently pointing to '&'. By adding 2u to markupBuffer it will point to numeric string by skipping "&#'
1100 result = XHTMLNumericEntityToUtf8((markupBuffer + 2u), entityCode);
1102 else // Checking if XHTML Named Entity
1104 entityCode = const_cast<char*>(NamedEntityToUtf8(markupBuffer, len));
1105 result = (entityCode != NULL);
1109 markupBuffer = entityCode; //utf8 text assigned to markupBuffer
1110 character = markupBuffer[0];
1114 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Not valid XHTML entity : (%.*s) \n", len, markupBuffer);
1115 markupBuffer = NULL;
1118 else // in case string conatins Start of XHTML Entity('&') but not its end character(';')
1120 if(character == AMPERSAND)
1122 markupBuffer = NULL;
1123 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Not Well formed XHTML content \n");
1128 if(markupBuffer != NULL)
1130 const unsigned char numberOfBytes = GetUtf8Length(character);
1131 markupProcessData.markupProcessedText.push_back(character);
1133 for(unsigned char i = 1u; i < numberOfBytes; ++i)
1136 markupProcessData.markupProcessedText.push_back(*markupBuffer);
1140 markupStringBuffer += count;
1146 void ProcessMarkupString(const std::string& markupString, MarkupPropertyData& markupPropertyData, MarkupProcessData& markupProcessData)
1148 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "markupString: %s\n", markupString.c_str());
1150 // Reserve space for the plain text.
1151 const Length markupStringSize = markupString.size();
1152 markupProcessData.markupProcessedText.reserve(markupStringSize);
1154 // Stores a struct with the index to the first character of the run, the type of run and its parameters.
1155 StyleStack<RunIndex> styleStack;
1157 // Stores a struct with the index to the first character of the color run & color font for the span.
1158 StyleStack<Span> spanStack;
1160 StyleStack<AnchorForStack> anchorStack;
1162 // Points the next free position in the vector of runs.
1163 RunIndex colorRunIndex = 0u;
1164 RunIndex fontRunIndex = 0u;
1165 RunIndex underlinedCharacterRunIndex = 0u;
1166 RunIndex backgroundRunIndex = 0u;
1167 RunIndex strikethroughCharacterRunIndex = 0u;
1168 RunIndex boundedParagraphRunIndex = 0u;
1169 RunIndex characterSpacingCharacterRunIndex = 0u;
1171 // check tag reference
1172 int colorTagReference = 0u;
1173 int fontTagReference = 0u;
1174 int iTagReference = 0u;
1175 int bTagReference = 0u;
1176 int uTagReference = 0u;
1177 int backgroundTagReference = 0u;
1178 int spanTagReference = 0u;
1179 int sTagReference = 0u;
1180 int pTagReference = 0u;
1181 int characterSpacingTagReference = 0u;
1182 int aTagReference = 0u;
1184 // Give an initial default value to the model's vectors.
1185 markupProcessData.colorRuns.Reserve(DEFAULT_VECTOR_SIZE);
1186 markupProcessData.fontRuns.Reserve(DEFAULT_VECTOR_SIZE);
1187 markupProcessData.underlinedCharacterRuns.Reserve(DEFAULT_VECTOR_SIZE);
1188 markupProcessData.backgroundColorRuns.Reserve(DEFAULT_VECTOR_SIZE);
1189 markupProcessData.strikethroughCharacterRuns.Reserve(DEFAULT_VECTOR_SIZE);
1190 markupProcessData.characterSpacingCharacterRuns.Reserve(DEFAULT_VECTOR_SIZE);
1192 // Get the mark-up string buffer.
1193 const char* markupStringBuffer = markupString.c_str();
1194 const char* const markupStringEndBuffer = markupStringBuffer + markupStringSize;
1197 CharacterIndex characterIndex = 0u;
1198 for(; markupStringBuffer < markupStringEndBuffer;)
1200 tag.attributes.Clear();
1201 if(IsTag(markupStringBuffer,
1202 markupStringEndBuffer,
1205 if(TokenComparison(MARKUP::TAG::COLOR, tag.buffer, tag.length))
1207 ProcessTagForRun<ColorRun>(
1208 markupProcessData.colorRuns, styleStack, tag, characterIndex, colorRunIndex, colorTagReference, [](const Tag& tag, ColorRun& run) { ProcessColorTag(tag, run); });
1209 } // <color></color>
1210 else if(TokenComparison(MARKUP::TAG::ITALIC, tag.buffer, tag.length))
1212 ProcessTagForRun<FontDescriptionRun>(
1213 markupProcessData.fontRuns, styleStack, tag, characterIndex, fontRunIndex, iTagReference, [](const Tag&, FontDescriptionRun& fontRun) {
1214 fontRun.slant = TextAbstraction::FontSlant::ITALIC;
1215 fontRun.slantDefined = true;
1218 else if(TokenComparison(MARKUP::TAG::UNDERLINE, tag.buffer, tag.length))
1220 ProcessTagForRun<UnderlinedCharacterRun>(
1221 markupProcessData.underlinedCharacterRuns, styleStack, tag, characterIndex, underlinedCharacterRunIndex, uTagReference, [](const Tag& tag, UnderlinedCharacterRun& run) { ProcessUnderlineTag(tag, run); });
1223 else if(TokenComparison(MARKUP::TAG::BOLD, tag.buffer, tag.length))
1225 ProcessTagForRun<FontDescriptionRun>(
1226 markupProcessData.fontRuns, styleStack, tag, characterIndex, fontRunIndex, bTagReference, [](const Tag&, FontDescriptionRun& fontRun) {
1227 fontRun.weight = TextAbstraction::FontWeight::BOLD;
1228 fontRun.weightDefined = true;
1231 else if(TokenComparison(MARKUP::TAG::FONT, tag.buffer, tag.length))
1233 ProcessTagForRun<FontDescriptionRun>(
1234 markupProcessData.fontRuns, styleStack, tag, characterIndex, fontRunIndex, fontTagReference, [](const Tag& tag, FontDescriptionRun& fontRun) { ProcessFontTag(tag, fontRun); });
1236 else if(TokenComparison(MARKUP::TAG::ANCHOR, tag.buffer, tag.length))
1238 ProcessAnchorForRun(markupProcessData,
1242 markupProcessData.colorRuns,
1243 markupProcessData.underlinedCharacterRuns,
1245 underlinedCharacterRunIndex,
1248 } // <a href=https://www.tizen.org>tizen</a>
1249 else if(TokenComparison(MARKUP::TAG::SHADOW, tag.buffer, tag.length))
1251 // TODO: If !tag.isEndTag, then create a new shadow run.
1252 // else Pop the top of the stack and set the number of characters of the run.
1253 } // <shadow></shadow>
1254 else if(TokenComparison(MARKUP::TAG::GLOW, tag.buffer, tag.length))
1256 // TODO: If !tag.isEndTag, then create a new glow run.
1257 // else Pop the top of the stack and set the number of characters of the run.
1259 else if(TokenComparison(MARKUP::TAG::OUTLINE, tag.buffer, tag.length))
1261 // TODO: If !tag.isEndTag, then create a new outline run.
1262 // else Pop the top of the stack and set the number of characters of the run.
1263 } // <outline></outline>
1264 else if(TokenComparison(MARKUP::TAG::EMBEDDED_ITEM, tag.buffer, tag.length))
1266 ProcessItemTag(markupProcessData, tag, characterIndex);
1268 else if(TokenComparison(MARKUP::TAG::BACKGROUND, tag.buffer, tag.length))
1270 ProcessTagForRun<ColorRun>(
1271 markupProcessData.backgroundColorRuns, styleStack, tag, characterIndex, backgroundRunIndex, backgroundTagReference, [](const Tag& tag, ColorRun& run) { ProcessBackground(tag, run); });
1273 else if(TokenComparison(MARKUP::TAG::SPAN, tag.buffer, tag.length))
1275 ProcessSpanForRun(tag,
1277 markupProcessData.colorRuns,
1278 markupProcessData.fontRuns,
1279 markupProcessData.underlinedCharacterRuns,
1280 markupProcessData.backgroundColorRuns,
1281 markupProcessData.strikethroughCharacterRuns,
1282 markupProcessData.characterSpacingCharacterRuns,
1285 underlinedCharacterRunIndex,
1287 strikethroughCharacterRunIndex,
1288 characterSpacingCharacterRunIndex,
1292 else if(TokenComparison(MARKUP::TAG::STRIKETHROUGH, tag.buffer, tag.length))
1294 ProcessTagForRun<StrikethroughCharacterRun>(
1295 markupProcessData.strikethroughCharacterRuns, styleStack, tag, characterIndex, strikethroughCharacterRunIndex, sTagReference, [](const Tag& tag, StrikethroughCharacterRun& run) { ProcessStrikethroughTag(tag, run); });
1297 else if(TokenComparison(MARKUP::TAG::PARAGRAPH, tag.buffer, tag.length))
1299 ProcessParagraphTag(markupProcessData, tag, (markupStringBuffer == markupStringEndBuffer), characterIndex);
1300 ProcessTagForRun<BoundedParagraphRun>(
1301 markupProcessData.boundedParagraphRuns, styleStack, tag, characterIndex, boundedParagraphRunIndex, pTagReference, [](const Tag& tag, BoundedParagraphRun& run) { ProcessAttributesOfParagraphTag(tag, run); });
1303 else if(TokenComparison(MARKUP::TAG::CHARACTER_SPACING, tag.buffer, tag.length))
1305 ProcessTagForRun<CharacterSpacingCharacterRun>(
1306 markupProcessData.characterSpacingCharacterRuns, styleStack, tag, characterIndex, characterSpacingCharacterRunIndex, characterSpacingTagReference, [](const Tag& tag, CharacterSpacingCharacterRun& run) { ProcessCharacterSpacingTag(tag, run); });
1307 } // <char-spacing></char-spacing>
1308 } // end if( IsTag() )
1309 else if(markupStringBuffer < markupStringEndBuffer)
1311 ProcessMarkupStringBuffer(markupProcessData, markupStringBuffer, markupStringEndBuffer, characterIndex);
1315 // Resize the model's vectors.
1316 ResizeModelVectors(markupProcessData, fontRunIndex, colorRunIndex, underlinedCharacterRunIndex, strikethroughCharacterRunIndex, backgroundRunIndex, boundedParagraphRunIndex, characterSpacingCharacterRunIndex);
1318 // Handle the nested tags
1319 OverrideNestedUnderlinedCharacterRuns(markupProcessData.underlinedCharacterRuns);
1320 OverrideNestedStrikethroughCharacterRuns(markupProcessData.strikethroughCharacterRuns);
1325 } // namespace Toolkit