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.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-anchor.h>
29 #include <dali-toolkit/internal/text/markup-processor-background.h>
30 #include <dali-toolkit/internal/text/markup-processor-character-spacing.h>
31 #include <dali-toolkit/internal/text/markup-processor-color.h>
32 #include <dali-toolkit/internal/text/markup-processor-embedded-item.h>
33 #include <dali-toolkit/internal/text/markup-processor-font.h>
34 #include <dali-toolkit/internal/text/markup-processor-helper-functions.h>
35 #include <dali-toolkit/internal/text/markup-processor-paragraph.h>
36 #include <dali-toolkit/internal/text/markup-processor-span.h>
37 #include <dali-toolkit/internal/text/markup-processor-strikethrough.h>
38 #include <dali-toolkit/internal/text/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 Initializes a font run description to its defaults.
144 * @param[in,out] fontRun The font description run to initialize.
146 void Initialize(FontDescriptionRun& fontRun)
148 fontRun.characterRun.characterIndex = 0u;
149 fontRun.characterRun.numberOfCharacters = 0u;
150 fontRun.familyName = NULL;
151 fontRun.familyLength = 0u;
152 fontRun.weight = TextAbstraction::FontWeight::NORMAL;
153 fontRun.width = TextAbstraction::FontWidth::NORMAL;
154 fontRun.slant = TextAbstraction::FontSlant::NORMAL;
156 fontRun.familyDefined = false;
157 fontRun.weightDefined = false;
158 fontRun.widthDefined = false;
159 fontRun.slantDefined = false;
160 fontRun.sizeDefined = false;
164 * @brief Initializes a color run description to its defaults.
166 * @param[in,out] colorRun The font description run to initialize.
168 void Initialize(ColorRun& colorRun)
170 colorRun.characterRun.characterIndex = 0u;
171 colorRun.characterRun.numberOfCharacters = 0u;
175 * @brief Initializes a underlined character run to its defaults.
177 * @param[in,out] underlinedCharacterRun The underelined character run to initialize.
179 void Initialize(UnderlinedCharacterRun& underlinedCharacterRun)
181 underlinedCharacterRun.characterRun.characterIndex = 0u;
182 underlinedCharacterRun.characterRun.numberOfCharacters = 0u;
186 * @brief Initializes a span to its defaults.
188 * @param[in,out] span The span to be initialized.
190 void Initialize(Span& span)
192 span.colorRunIndex = 0u;
193 span.isColorDefined = false;
195 span.fontRunIndex = 0u;
196 span.isFontDefined = false;
198 span.underlinedCharacterRunIndex = 0u;
199 span.isUnderlinedCharacterDefined = false;
200 span.backgroundColorRunIndex = 0u;
201 span.isBackgroundColorDefined = false;
204 span.strikethroughCharacterRunIndex = 0u;
205 span.isStrikethroughDefined = false;
208 span.characterSpacingCharacterRunIndex = 0u;
209 span.isCharacterSpacingDefined = false;
213 * @brief Initializes a strikethrough character run to its defaults.
215 * @param[in,out] strikethroughCharacterRun The strikethrough character run to initialize.
217 void Initialize(StrikethroughCharacterRun& strikethroughCharacterRun)
219 strikethroughCharacterRun.characterRun.characterIndex = 0u;
220 strikethroughCharacterRun.characterRun.numberOfCharacters = 0u;
221 strikethroughCharacterRun.properties.colorDefined = false;
225 * @brief Initializes a bounded-paragraph character run to its defaults.
227 * @param[in,out] boundedParagraphRun The bounded paragraphRun run to initialize.
229 void Initialize(BoundedParagraphRun& boundedParagraphRun)
231 boundedParagraphRun.characterRun.characterIndex = 0u;
232 boundedParagraphRun.characterRun.numberOfCharacters = 0u;
236 * @brief Initializes a character-spacing run to its defaults.
238 * @param[in,out] characterSpacingCharacterRun The character-spacing run to initialize.
240 void Initialize(CharacterSpacingCharacterRun& characterSpacingCharacterRun)
242 characterSpacingCharacterRun.characterRun.characterIndex = 0u;
243 characterSpacingCharacterRun.characterRun.numberOfCharacters = 0u;
244 characterSpacingCharacterRun.value = 0.0f;
248 * @brief Splits the tag string into the tag name and its attributes.
250 * The attributes are stored in a vector in the tag.
252 * @param[in,out] tag The tag.
254 void ParseAttributes(Tag& tag)
256 if(tag.buffer == NULL)
261 tag.attributes.Resize(MAX_NUM_OF_ATTRIBUTES);
263 // Find first the tag name.
264 bool isQuotationOpen = false;
266 const char* tagBuffer = tag.buffer;
267 const char* const tagEndBuffer = tagBuffer + tag.length;
269 for(; tagBuffer < tagEndBuffer; ++tagBuffer)
271 const char character = *tagBuffer;
272 if(WHITE_SPACE < character)
278 // Stops counting the length of the tag when a white space is found.
279 // @note a white space is the WHITE_SPACE character and anything below as 'tab', 'return' or 'control characters'.
283 SkipWhiteSpace(tagBuffer, tagEndBuffer);
285 // Find the attributes.
286 unsigned int attributeIndex = 0u;
287 const char* nameBuffer = NULL;
288 const char* valueBuffer = NULL;
289 Length nameLength = 0u;
290 Length valueLength = 0u;
292 bool addToNameValue = true;
293 Length numberOfWhiteSpace = 0u;
294 for(; tagBuffer < tagEndBuffer; ++tagBuffer)
296 const char character = *tagBuffer;
297 if((WHITE_SPACE >= character) && !isQuotationOpen)
299 if(NULL != valueBuffer)
301 // Remove white spaces at the end of the value.
302 valueLength -= numberOfWhiteSpace;
305 if((NULL != nameBuffer) && (NULL != valueBuffer))
307 // Every time a white space is found, a new attribute is created and stored in the attributes vector.
308 Attribute& attribute = *(tag.attributes.Begin() + attributeIndex);
311 attribute.nameBuffer = nameBuffer;
312 attribute.valueBuffer = valueBuffer;
313 attribute.nameLength = nameLength;
314 attribute.valueLength = valueLength;
321 addToNameValue = true; // next read characters will be added to the name.
324 else if(EQUAL == character) // '='
326 addToNameValue = false; // next read characters will be added to the value.
327 SkipWhiteSpace(tagBuffer, tagEndBuffer);
329 else if(QUOTATION_MARK == character) // '\''
331 // Do not add quotation marks to neither name nor value.
332 isQuotationOpen = !isQuotationOpen;
337 SkipWhiteSpace(tagBuffer, tagEndBuffer);
343 // Adds characters to the name or the value.
346 if(NULL == nameBuffer)
348 nameBuffer = tagBuffer;
356 if(WHITE_SPACE >= character)
358 ++numberOfWhiteSpace;
362 numberOfWhiteSpace = 0u;
365 if(NULL == valueBuffer)
367 valueBuffer = tagBuffer;
374 if(NULL != valueBuffer)
376 // Remove white spaces at the end of the value.
377 valueLength -= numberOfWhiteSpace;
380 if((NULL != nameBuffer) && (NULL != valueBuffer))
382 // Checks if the last attribute needs to be added.
383 Attribute& attribute = *(tag.attributes.Begin() + attributeIndex);
386 attribute.nameBuffer = nameBuffer;
387 attribute.valueBuffer = valueBuffer;
388 attribute.nameLength = nameLength;
389 attribute.valueLength = valueLength;
392 // Resize the vector of attributes.
393 tag.attributes.Resize(attributeIndex);
397 * @brief It parses a tag and its attributes if the given iterator @e it is pointing at a tag beginning.
399 * @param[in,out] markupStringBuffer The mark-up string buffer. It's a const iterator pointing the current character.
400 * @param[in] markupStringEndBuffer Pointer to one character after the end of the mark-up string buffer.
401 * @param[out] tag The tag with its attributes.
403 * @return @e true if the iterator @e it is pointing a mark-up tag. Otherwise @e false.
405 bool IsTag(const char*& markupStringBuffer,
406 const char* const markupStringEndBuffer,
410 bool isQuotationOpen = false;
411 bool attributesFound = false;
412 tag.isEndTag = false;
413 bool isPreviousLessThan = false;
414 bool isPreviousSlash = false;
416 const char character = *markupStringBuffer;
417 if(LESS_THAN == character) // '<'
421 isPreviousLessThan = true;
423 // if the iterator is pointing to a '<' character, then check if it's a mark-up tag is needed.
424 ++markupStringBuffer;
425 if(markupStringBuffer < markupStringEndBuffer)
427 SkipWhiteSpace(markupStringBuffer, markupStringEndBuffer);
429 for(; (!isTag) && (markupStringBuffer < markupStringEndBuffer); ++markupStringBuffer)
431 const char character = *markupStringBuffer;
433 if(!isQuotationOpen && (SLASH == character)) // '/'
435 if(isPreviousLessThan)
441 // if the tag has a '/' it may be an end tag.
442 isPreviousSlash = true;
445 isPreviousLessThan = false;
446 if((markupStringBuffer + 1u < markupStringEndBuffer) && (WHITE_SPACE >= *(markupStringBuffer + 1u)))
448 ++markupStringBuffer;
449 SkipWhiteSpace(markupStringBuffer, markupStringEndBuffer);
450 --markupStringBuffer;
453 else if(GREATER_THAN == character) // '>'
461 isPreviousSlash = false;
462 isPreviousLessThan = false;
464 else if(QUOTATION_MARK == character)
466 isQuotationOpen = !isQuotationOpen;
469 isPreviousSlash = false;
470 isPreviousLessThan = false;
472 else if(WHITE_SPACE >= character) // ' '
474 // If the tag contains white spaces then it may have attributes.
477 attributesFound = true;
483 if(NULL == tag.buffer)
485 tag.buffer = markupStringBuffer;
488 // If it's not any of the 'special' characters then just add it to the tag string.
491 isPreviousSlash = false;
492 isPreviousLessThan = false;
497 // If the tag string has white spaces, then parse the attributes is needed.
500 ParseAttributes(tag);
508 * @brief Returns length of XHTML entity by parsing the text. It also determines if it is XHTML entity or not.
510 * @param[in] markupStringBuffer The mark-up string buffer. It's a const iterator pointing the current character.
511 * @param[in] markupStringEndBuffer Pointing to end of mark-up string buffer.
513 * @return Length of markupText in case of XHTML entity otherwise return 0.
515 unsigned int GetXHTMLEntityLength(const char*& markupStringBuffer,
516 const char* const markupStringEndBuffer)
518 char character = *markupStringBuffer;
519 if(AMPERSAND == character) // '&'
521 // if the iterator is pointing to a '&' character, then check for ';' to find end to XHTML entity.
522 ++markupStringBuffer;
523 if(markupStringBuffer < markupStringEndBuffer)
525 unsigned int len = 1u;
526 for(; markupStringBuffer < markupStringEndBuffer; ++markupStringBuffer)
528 character = *markupStringBuffer;
530 if(SEMI_COLON == character) // ';'
532 // found end of XHTML entity
533 ++markupStringBuffer;
536 else if((AMPERSAND == character) || (BACK_SLASH == character) || (LESS_THAN == character))
547 * @brief It parses a XHTML string which has hex/decimal entity and fill its corresponging utf-8 string.
549 * @param[in] markupText The mark-up text buffer.
550 * @param[out] utf-8 text Corresponding to markup Text
552 * @return true if string is successfully parsed otherwise false
554 bool XHTMLNumericEntityToUtf8(const char* markupText, char* utf8)
558 if(NULL != markupText)
562 // check if hex or decimal entity
563 if((CHAR_ARRAY_END != *markupText) && (HEX_CODE == *markupText))
570 unsigned long l = strtoul(markupText, &end, (isHex ? 16 : 10)); // l contains UTF-32 code in case of correct XHTML entity
572 // check for valid XHTML numeric entities (between '#' or "#x" and ';')
573 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'
575 /* characters XML 1.1 permits */
576 if(((XHTML_DECIMAL_ENTITY_RANGE[0] < l) && (l <= XHTML_DECIMAL_ENTITY_RANGE[1])) ||
577 ((XHTML_DECIMAL_ENTITY_RANGE[2] <= l) && (l <= XHTML_DECIMAL_ENTITY_RANGE[3])) ||
578 ((XHTML_DECIMAL_ENTITY_RANGE[4] <= l) && (l <= XHTML_DECIMAL_ENTITY_RANGE[5])))
580 // Convert UTF32 code to UTF8
581 Utf32ToUtf8(reinterpret_cast<const uint32_t* const>(&l), 1, reinterpret_cast<uint8_t*>(utf8));
590 * @brief Processes a particular tag for the required run (color-run, font-run or underlined-character-run).
592 * @tparam RunType Whether ColorRun , FontDescriptionRun or UnderlinedCharacterRun
594 * @param[in/out] runsContainer The container containing all the runs
595 * @param[in/out] styleStack The style stack
596 * @param[in] tag The tag we are currently processing
597 * @param[in] characterIndex The current character index
598 * @param[in/out] runIndex The run index
599 * @param[in/out] tagReference The tagReference we should increment/decrement
600 * @param[in] parameterSettingFunction This function will be called to set run specific parameters
602 template<typename RunType>
603 void ProcessTagForRun(
604 Vector<RunType>& runsContainer,
605 StyleStack<RunIndex>& styleStack,
607 const CharacterIndex characterIndex,
610 std::function<void(const Tag&, RunType&)> parameterSettingFunction)
618 // Fill the run with the parameters.
619 run.characterRun.characterIndex = characterIndex;
620 parameterSettingFunction(tag, run);
622 // Push the run in the logical model.
623 runsContainer.PushBack(run);
625 // Push the index of the run into the stack.
626 styleStack.Push(runIndex);
628 // Point the next free run.
631 // Increase reference
638 // Pop the top of the stack and set the number of characters of the run.
639 RunType& run = *(runsContainer.Begin() + styleStack.Pop());
640 run.characterRun.numberOfCharacters = characterIndex - run.characterRun.characterIndex;
647 * @brief Processes the item tag
649 * @param[in/out] markupProcessData The markup process data
650 * @param[in] tag The current tag
651 * @param[in/out] characterIndex The current character index
654 MarkupProcessData& markupProcessData,
656 CharacterIndex& characterIndex)
660 // Create an embedded item instance.
662 item.characterIndex = characterIndex;
663 ProcessEmbeddedItem(tag, item);
665 markupProcessData.items.PushBack(item);
667 // Insert white space character that will be replaced by the item.
668 markupProcessData.markupProcessedText.append(1u, WHITE_SPACE);
674 * @brief Processes the paragraph-tag
676 * @param[in/out] markupProcessData The markup process data
677 * @param[in] tag The current tag
678 * @param[in] isEndBuffer Whether the end of buffer
679 * @param[in/out] characterIndex The current character index
681 void ProcessParagraphTag(
682 MarkupProcessData& markupProcessData,
685 CharacterIndex& characterIndex)
687 if((characterIndex > 0 &&
688 markupProcessData.markupProcessedText[characterIndex - 1u] != NEW_LINE) &&
689 (!(tag.isEndTag && isEndBuffer)))
691 // Insert new-line character at the start and end of paragraph.
692 markupProcessData.markupProcessedText.append(1u, NEW_LINE);
698 * @brief Processes the anchor tag
700 * @param[in/out] markupProcessData The markup process data
701 * @param[in] tag The current tag
702 * @param[in/out] characterIndex The current character index
704 void ProcessAnchorTag(
705 MarkupProcessData& markupProcessData,
707 CharacterIndex& characterIndex)
711 // Create an anchor instance.
713 anchor.startIndex = characterIndex;
714 anchor.endIndex = 0u;
715 ProcessAnchor(tag, anchor);
716 markupProcessData.anchors.PushBack(anchor);
721 unsigned int count = markupProcessData.anchors.Count();
724 markupProcessData.anchors[count - 1].endIndex = characterIndex;
730 * @brief Processes span tag for the color-run & font-run.
732 * @param[in] spanTag The tag we are currently processing
733 * @param[inout] spanStack The spans stack
734 * @param[inout] colorRuns The container containing all the color runs
735 * @param[inout] fontRuns The container containing all the font description runs
736 * @param[inout] underlinedCharacterRuns The container containing all the underlined character runs
737 * @param[inout] strikethroughCharacterRuns The container containing all the strikethroughed character runs
738 * @param[inout] colorRunIndex The color run index
739 * @param[inout] fontRunIndex The font run index
740 * @param[inout] underlinedCharacterRunIndex The underlined character run index
741 * @param[inout] strikethroughCharacterRunIndex The strikethroughed character run index
742 * @param[in] characterIndex The current character index
743 * @param[in] tagReference The tagReference we should increment/decrement
745 void ProcessSpanForRun(
747 StyleStack<Span>& spanStack,
748 Vector<ColorRun>& colorRuns,
749 Vector<FontDescriptionRun>& fontRuns,
750 Vector<UnderlinedCharacterRun>& underlinedCharacterRuns,
751 Vector<ColorRun>& backgroundColorRuns,
752 Vector<StrikethroughCharacterRun>& strikethroughCharacterRuns,
753 Vector<CharacterSpacingCharacterRun>& characterSpacingCharacterRuns,
754 RunIndex& colorRunIndex,
755 RunIndex& fontRunIndex,
756 RunIndex& underlinedCharacterRunIndex,
757 RunIndex& backgroundColorRunIndex,
758 RunIndex& strikethroughCharacterRunIndex,
759 RunIndex& characterSpacingCharacterRunIndex,
760 const CharacterIndex characterIndex,
763 if(!spanTag.isEndTag)
767 Initialize(colorRun);
769 FontDescriptionRun fontRun;
772 UnderlinedCharacterRun underlinedCharacterRun;
773 Initialize(underlinedCharacterRun);
775 ColorRun backgroundColorRun;
776 Initialize(backgroundColorRun);
778 StrikethroughCharacterRun strikethroughCharacterRun;
779 Initialize(strikethroughCharacterRun);
781 CharacterSpacingCharacterRun characterSpacingCharacterRun;
782 Initialize(characterSpacingCharacterRun);
787 // Fill the run with the parameters.
788 colorRun.characterRun.characterIndex = characterIndex;
789 fontRun.characterRun.characterIndex = characterIndex;
790 underlinedCharacterRun.characterRun.characterIndex = characterIndex;
791 backgroundColorRun.characterRun.characterIndex = characterIndex;
792 strikethroughCharacterRun.characterRun.characterIndex = characterIndex;
793 characterSpacingCharacterRun.characterRun.characterIndex = characterIndex;
795 span.colorRunIndex = colorRunIndex;
796 span.fontRunIndex = fontRunIndex;
797 span.underlinedCharacterRunIndex = underlinedCharacterRunIndex;
798 span.backgroundColorRunIndex = backgroundColorRunIndex;
799 span.strikethroughCharacterRunIndex = strikethroughCharacterRunIndex;
800 span.characterSpacingCharacterRunIndex = characterSpacingCharacterRunIndex;
802 ProcessSpanTag(spanTag,
805 underlinedCharacterRun,
807 strikethroughCharacterRun,
808 characterSpacingCharacterRun,
811 span.isUnderlinedCharacterDefined,
812 span.isBackgroundColorDefined,
813 span.isStrikethroughDefined,
814 span.isCharacterSpacingDefined);
816 // Push the span into the stack.
817 spanStack.Push(span);
819 // Point the next free run.
820 if(span.isColorDefined)
822 // Push the run in the logical model.
823 colorRuns.PushBack(colorRun);
827 if(span.isFontDefined)
829 // Push the run in the logical model.
830 fontRuns.PushBack(fontRun);
834 if(span.isUnderlinedCharacterDefined)
836 // Push the run in the logical model.
837 underlinedCharacterRuns.PushBack(underlinedCharacterRun);
838 ++underlinedCharacterRunIndex;
841 if(span.isBackgroundColorDefined)
843 // Push the run in the logical model.
844 backgroundColorRuns.PushBack(backgroundColorRun);
845 ++backgroundColorRunIndex;
848 if(span.isStrikethroughDefined)
850 // Push the run in the logical model.
851 strikethroughCharacterRuns.PushBack(strikethroughCharacterRun);
852 ++strikethroughCharacterRunIndex;
855 if(span.isCharacterSpacingDefined)
857 // Push the run in the logical model.
858 characterSpacingCharacterRuns.PushBack(characterSpacingCharacterRun);
859 ++characterSpacingCharacterRunIndex;
862 // Increase reference
869 // Pop the top of the stack and set the number of characters of the run.
870 Span span = spanStack.Pop();
872 if(span.isColorDefined)
874 ColorRun& colorRun = *(colorRuns.Begin() + span.colorRunIndex);
875 colorRun.characterRun.numberOfCharacters = characterIndex - colorRun.characterRun.characterIndex;
878 if(span.isFontDefined)
880 FontDescriptionRun& fontRun = *(fontRuns.Begin() + span.fontRunIndex);
881 fontRun.characterRun.numberOfCharacters = characterIndex - fontRun.characterRun.characterIndex;
884 if(span.isUnderlinedCharacterDefined)
886 UnderlinedCharacterRun& underlinedCharacterRun = *(underlinedCharacterRuns.Begin() + span.underlinedCharacterRunIndex);
887 underlinedCharacterRun.characterRun.numberOfCharacters = characterIndex - underlinedCharacterRun.characterRun.characterIndex;
890 if(span.isBackgroundColorDefined)
892 ColorRun& backgroundColorRun = *(backgroundColorRuns.Begin() + span.backgroundColorRunIndex);
893 backgroundColorRun.characterRun.numberOfCharacters = characterIndex - backgroundColorRun.characterRun.characterIndex;
896 if(span.isStrikethroughDefined)
898 StrikethroughCharacterRun& strikethroughCharacterRun = *(strikethroughCharacterRuns.Begin() + span.strikethroughCharacterRunIndex);
899 strikethroughCharacterRun.characterRun.numberOfCharacters = characterIndex - strikethroughCharacterRun.characterRun.characterIndex;
902 if(span.isCharacterSpacingDefined)
904 CharacterSpacingCharacterRun& characterSpacingCharacterRun = *(characterSpacingCharacterRuns.Begin() + span.characterSpacingCharacterRunIndex);
905 characterSpacingCharacterRun.characterRun.numberOfCharacters = characterIndex - characterSpacingCharacterRun.characterRun.characterIndex;
914 * @brief Resizes the model's vectors
916 * @param[inout] markupProcessData The markup process data
917 * @param[in] fontRunIndex The font run index
918 * @param[in] colorRunIndex The color run index
919 * @param[in] underlinedCharacterRunIndex The underlined character run index
920 * @param[in] strikethroughCharacterRunIndex The strikethroughed character run index
921 * @param[in] backgroundRunIndex The background run index
922 * @param[in] boundedParagraphRunIndex The bounded paragraph run index
923 * @param[in] characterSpacingCharacterRunIndex The character-spacing character run index
926 void ResizeModelVectors(MarkupProcessData& markupProcessData,
927 const RunIndex fontRunIndex,
928 const RunIndex colorRunIndex,
929 const RunIndex underlinedCharacterRunIndex,
930 const RunIndex strikethroughCharacterRunIndex,
931 const RunIndex backgroundRunIndex,
932 const RunIndex boundedParagraphRunIndex,
933 const RunIndex characterSpacingCharacterRunIndex)
935 markupProcessData.fontRuns.Resize(fontRunIndex);
936 markupProcessData.colorRuns.Resize(colorRunIndex);
937 markupProcessData.underlinedCharacterRuns.Resize(underlinedCharacterRunIndex);
938 markupProcessData.strikethroughCharacterRuns.Resize(strikethroughCharacterRunIndex);
939 markupProcessData.backgroundColorRuns.Resize(backgroundRunIndex);
940 markupProcessData.boundedParagraphRuns.Resize(boundedParagraphRunIndex);
941 markupProcessData.characterSpacingCharacterRuns.Resize(characterSpacingCharacterRunIndex);
944 for(uint32_t i = 0; gLogFilter->IsEnabledFor(Debug::Verbose) && i < colorRunIndex; ++i)
946 ColorRun& run = markupProcessData.colorRuns[i];
947 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);
953 * @brief Processes the markup string buffer
955 * @param[in/out] markupProcessData The markup process data
956 * @param[in/out] markupStringBuffer The markup string buffer pointer
957 * @param[in] markupStringEndBuffer The markup string end buffer pointer
958 * @param[in/out] characterIndex The current character index
960 void ProcessMarkupStringBuffer(
961 MarkupProcessData& markupProcessData,
962 const char*& markupStringBuffer,
963 const char* const markupStringEndBuffer,
964 CharacterIndex& characterIndex)
966 unsigned char character = *markupStringBuffer;
967 const char* markupBuffer = markupStringBuffer;
968 unsigned char count = GetUtf8Length(character);
971 if((BACK_SLASH == character) && (markupStringBuffer + 1u < markupStringEndBuffer))
973 // Adding < , > or & special character.
974 const unsigned char nextCharacter = *(markupStringBuffer + 1u);
975 if((LESS_THAN == nextCharacter) || (GREATER_THAN == nextCharacter) || (AMPERSAND == nextCharacter))
977 character = nextCharacter;
978 ++markupStringBuffer;
980 count = GetUtf8Length(character);
981 markupBuffer = markupStringBuffer;
984 else // checking if contains XHTML entity or not
986 const unsigned int len = GetXHTMLEntityLength(markupStringBuffer, markupStringEndBuffer);
988 // Parse markupStringTxt if it contains XHTML Entity between '&' and ';'
991 char* entityCode = NULL;
995 // Checking if XHTML Numeric Entity
996 if(HASH == *(markupBuffer + 1u))
998 entityCode = &utf8[0];
999 // markupBuffer is currently pointing to '&'. By adding 2u to markupBuffer it will point to numeric string by skipping "&#'
1000 result = XHTMLNumericEntityToUtf8((markupBuffer + 2u), entityCode);
1002 else // Checking if XHTML Named Entity
1004 entityCode = const_cast<char*>(NamedEntityToUtf8(markupBuffer, len));
1005 result = (entityCode != NULL);
1009 markupBuffer = entityCode; //utf8 text assigned to markupBuffer
1010 character = markupBuffer[0];
1014 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Not valid XHTML entity : (%.*s) \n", len, markupBuffer);
1015 markupBuffer = NULL;
1018 else // in case string conatins Start of XHTML Entity('&') but not its end character(';')
1020 if(character == AMPERSAND)
1022 markupBuffer = NULL;
1023 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Not Well formed XHTML content \n");
1028 if(markupBuffer != NULL)
1030 const unsigned char numberOfBytes = GetUtf8Length(character);
1031 markupProcessData.markupProcessedText.push_back(character);
1033 for(unsigned char i = 1u; i < numberOfBytes; ++i)
1036 markupProcessData.markupProcessedText.push_back(*markupBuffer);
1040 markupStringBuffer += count;
1046 void ProcessMarkupString(const std::string& markupString, MarkupProcessData& markupProcessData)
1048 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "markupString: %s\n", markupString.c_str());
1050 // Reserve space for the plain text.
1051 const Length markupStringSize = markupString.size();
1052 markupProcessData.markupProcessedText.reserve(markupStringSize);
1054 // Stores a struct with the index to the first character of the run, the type of run and its parameters.
1055 StyleStack<RunIndex> styleStack;
1057 // Stores a struct with the index to the first character of the color run & color font for the span.
1058 StyleStack<Span> spanStack;
1060 // Points the next free position in the vector of runs.
1061 RunIndex colorRunIndex = 0u;
1062 RunIndex fontRunIndex = 0u;
1063 RunIndex underlinedCharacterRunIndex = 0u;
1064 RunIndex backgroundRunIndex = 0u;
1065 RunIndex strikethroughCharacterRunIndex = 0u;
1066 RunIndex boundedParagraphRunIndex = 0u;
1067 RunIndex characterSpacingCharacterRunIndex = 0u;
1069 // check tag reference
1070 int colorTagReference = 0u;
1071 int fontTagReference = 0u;
1072 int iTagReference = 0u;
1073 int bTagReference = 0u;
1074 int uTagReference = 0u;
1075 int backgroundTagReference = 0u;
1076 int spanTagReference = 0u;
1077 int sTagReference = 0u;
1078 int pTagReference = 0u;
1079 int characterSpacingTagReference = 0u;
1081 // Give an initial default value to the model's vectors.
1082 markupProcessData.colorRuns.Reserve(DEFAULT_VECTOR_SIZE);
1083 markupProcessData.fontRuns.Reserve(DEFAULT_VECTOR_SIZE);
1084 markupProcessData.underlinedCharacterRuns.Reserve(DEFAULT_VECTOR_SIZE);
1085 markupProcessData.backgroundColorRuns.Reserve(DEFAULT_VECTOR_SIZE);
1086 markupProcessData.strikethroughCharacterRuns.Reserve(DEFAULT_VECTOR_SIZE);
1087 markupProcessData.characterSpacingCharacterRuns.Reserve(DEFAULT_VECTOR_SIZE);
1089 // Get the mark-up string buffer.
1090 const char* markupStringBuffer = markupString.c_str();
1091 const char* const markupStringEndBuffer = markupStringBuffer + markupStringSize;
1094 CharacterIndex characterIndex = 0u;
1095 for(; markupStringBuffer < markupStringEndBuffer;)
1097 tag.attributes.Clear();
1098 if(IsTag(markupStringBuffer,
1099 markupStringEndBuffer,
1102 if(TokenComparison(MARKUP::TAG::COLOR, tag.buffer, tag.length))
1104 ProcessTagForRun<ColorRun>(
1105 markupProcessData.colorRuns, styleStack, tag, characterIndex, colorRunIndex, colorTagReference, [](const Tag& tag, ColorRun& run) { ProcessColorTag(tag, run); });
1106 } // <color></color>
1107 else if(TokenComparison(MARKUP::TAG::ITALIC, tag.buffer, tag.length))
1109 ProcessTagForRun<FontDescriptionRun>(
1110 markupProcessData.fontRuns, styleStack, tag, characterIndex, fontRunIndex, iTagReference, [](const Tag&, FontDescriptionRun& fontRun) {
1111 fontRun.slant = TextAbstraction::FontSlant::ITALIC;
1112 fontRun.slantDefined = true;
1115 else if(TokenComparison(MARKUP::TAG::UNDERLINE, tag.buffer, tag.length))
1117 ProcessTagForRun<UnderlinedCharacterRun>(
1118 markupProcessData.underlinedCharacterRuns, styleStack, tag, characterIndex, underlinedCharacterRunIndex, uTagReference, [](const Tag& tag, UnderlinedCharacterRun& run) { ProcessUnderlineTag(tag, run); });
1120 else if(TokenComparison(MARKUP::TAG::BOLD, tag.buffer, tag.length))
1122 ProcessTagForRun<FontDescriptionRun>(
1123 markupProcessData.fontRuns, styleStack, tag, characterIndex, fontRunIndex, bTagReference, [](const Tag&, FontDescriptionRun& fontRun) {
1124 fontRun.weight = TextAbstraction::FontWeight::BOLD;
1125 fontRun.weightDefined = true;
1128 else if(TokenComparison(MARKUP::TAG::FONT, tag.buffer, tag.length))
1130 ProcessTagForRun<FontDescriptionRun>(
1131 markupProcessData.fontRuns, styleStack, tag, characterIndex, fontRunIndex, fontTagReference, [](const Tag& tag, FontDescriptionRun& fontRun) { ProcessFontTag(tag, fontRun); });
1133 else if(TokenComparison(MARKUP::TAG::ANCHOR, tag.buffer, tag.length))
1136 ProcessAnchorTag(markupProcessData, tag, characterIndex);
1138 ProcessTagForRun<ColorRun>(
1139 markupProcessData.colorRuns, styleStack, tag, characterIndex, colorRunIndex, colorTagReference, [](const Tag& tag, ColorRun& run) {
1140 run.color = Color::BLUE;
1141 ProcessColorTag(tag, run);
1144 ProcessTagForRun<UnderlinedCharacterRun>(
1145 markupProcessData.underlinedCharacterRuns, styleStack, tag, characterIndex, underlinedCharacterRunIndex, uTagReference, [](const Tag& tag, UnderlinedCharacterRun& run) {
1146 run.properties.color = Color::BLUE;
1147 run.properties.colorDefined = true;
1148 ProcessUnderlineTag(tag, run);
1150 } // <a href=https://www.tizen.org>tizen</a>
1151 else if(TokenComparison(MARKUP::TAG::SHADOW, tag.buffer, tag.length))
1153 // TODO: If !tag.isEndTag, then create a new shadow run.
1154 // else Pop the top of the stack and set the number of characters of the run.
1155 } // <shadow></shadow>
1156 else if(TokenComparison(MARKUP::TAG::GLOW, tag.buffer, tag.length))
1158 // TODO: If !tag.isEndTag, then create a new glow run.
1159 // else Pop the top of the stack and set the number of characters of the run.
1161 else if(TokenComparison(MARKUP::TAG::OUTLINE, tag.buffer, tag.length))
1163 // TODO: If !tag.isEndTag, then create a new outline run.
1164 // else Pop the top of the stack and set the number of characters of the run.
1165 } // <outline></outline>
1166 else if(TokenComparison(MARKUP::TAG::EMBEDDED_ITEM, tag.buffer, tag.length))
1168 ProcessItemTag(markupProcessData, tag, characterIndex);
1170 else if(TokenComparison(MARKUP::TAG::BACKGROUND, tag.buffer, tag.length))
1172 ProcessTagForRun<ColorRun>(
1173 markupProcessData.backgroundColorRuns, styleStack, tag, characterIndex, backgroundRunIndex, backgroundTagReference, [](const Tag& tag, ColorRun& run) { ProcessBackground(tag, run); });
1175 else if(TokenComparison(MARKUP::TAG::SPAN, tag.buffer, tag.length))
1177 ProcessSpanForRun(tag,
1179 markupProcessData.colorRuns,
1180 markupProcessData.fontRuns,
1181 markupProcessData.underlinedCharacterRuns,
1182 markupProcessData.backgroundColorRuns,
1183 markupProcessData.strikethroughCharacterRuns,
1184 markupProcessData.characterSpacingCharacterRuns,
1187 underlinedCharacterRunIndex,
1189 strikethroughCharacterRunIndex,
1190 characterSpacingCharacterRunIndex,
1194 else if(TokenComparison(MARKUP::TAG::STRIKETHROUGH, tag.buffer, tag.length))
1196 ProcessTagForRun<StrikethroughCharacterRun>(
1197 markupProcessData.strikethroughCharacterRuns, styleStack, tag, characterIndex, strikethroughCharacterRunIndex, sTagReference, [](const Tag& tag, StrikethroughCharacterRun& run) { ProcessStrikethroughTag(tag, run); });
1199 else if(TokenComparison(MARKUP::TAG::PARAGRAPH, tag.buffer, tag.length))
1201 ProcessParagraphTag(markupProcessData, tag, (markupStringBuffer == markupStringEndBuffer), characterIndex);
1202 ProcessTagForRun<BoundedParagraphRun>(
1203 markupProcessData.boundedParagraphRuns, styleStack, tag, characterIndex, boundedParagraphRunIndex, pTagReference, [](const Tag& tag, BoundedParagraphRun& run) { ProcessAttributesOfParagraphTag(tag, run); });
1205 else if(TokenComparison(MARKUP::TAG::CHARACTER_SPACING, tag.buffer, tag.length))
1207 ProcessTagForRun<CharacterSpacingCharacterRun>(
1208 markupProcessData.characterSpacingCharacterRuns, styleStack, tag, characterIndex, characterSpacingCharacterRunIndex, characterSpacingTagReference, [](const Tag& tag, CharacterSpacingCharacterRun& run) { ProcessCharacterSpacingTag(tag, run); });
1209 } // <char-spacing></char-spacing>
1210 } // end if( IsTag() )
1211 else if(markupStringBuffer < markupStringEndBuffer)
1213 ProcessMarkupStringBuffer(markupProcessData, markupStringBuffer, markupStringEndBuffer, characterIndex);
1217 // Resize the model's vectors.
1218 ResizeModelVectors(markupProcessData, fontRunIndex, colorRunIndex, underlinedCharacterRunIndex, strikethroughCharacterRunIndex, backgroundRunIndex, boundedParagraphRunIndex, characterSpacingCharacterRunIndex);
1220 // Handle the nested tags
1221 OverrideNestedUnderlinedCharacterRuns(markupProcessData.underlinedCharacterRuns);
1222 OverrideNestedStrikethroughCharacterRuns(markupProcessData.strikethroughCharacterRuns);
1227 } // namespace Toolkit