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/xhtml-entities.h>
49 // HTML-ISH tag and attribute constants.
50 // Note they must be lower case in order to make the comparison to work
51 // as the parser converts all the read tags to lower case.
52 const std::string XHTML_COLOR_TAG("color");
53 const std::string XHTML_FONT_TAG("font");
54 const std::string XHTML_B_TAG("b");
55 const std::string XHTML_I_TAG("i");
56 const std::string XHTML_U_TAG("u");
57 const std::string XHTML_SHADOW_TAG("shadow");
58 const std::string XHTML_GLOW_TAG("glow");
59 const std::string XHTML_OUTLINE_TAG("outline");
60 const std::string XHTML_ITEM_TAG("item");
61 const std::string XHTML_ANCHOR_TAG("a");
62 const std::string XHTML_BACKGROUND_TAG("background");
63 const std::string XHTML_SPAN_TAG("span");
64 const std::string XHTML_STRIKETHROUGH_TAG("s");
65 const std::string XHTML_PARAGRAPH_TAG("p");
66 const std::string XHTML_CHARACTER_SPACING_TAG("char-spacing");
68 const char LESS_THAN = '<';
69 const char GREATER_THAN = '>';
70 const char EQUAL = '=';
71 const char QUOTATION_MARK = '\'';
72 const char SLASH = '/';
73 const char BACK_SLASH = '\\';
74 const char AMPERSAND = '&';
75 const char HASH = '#';
76 const char SEMI_COLON = ';';
77 const char CHAR_ARRAY_END = '\0';
78 const char HEX_CODE = 'x';
80 const char WHITE_SPACE = 0x20; // ASCII value of the white space.
81 const char NEW_LINE = 0x0A; // ASCII value of the newline.
83 // Range 1 0x0u < XHTML_DECIMAL_ENTITY_RANGE <= 0xD7FFu
84 // Range 2 0xE000u < XHTML_DECIMAL_ENTITY_RANGE <= 0xFFFDu
85 // Range 3 0x10000u < XHTML_DECIMAL_ENTITY_RANGE <= 0x10FFFFu
86 const unsigned long XHTML_DECIMAL_ENTITY_RANGE[] = {0x0u, 0xD7FFu, 0xE000u, 0xFFFDu, 0x10000u, 0x10FFFFu};
88 // 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.
89 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.
90 const unsigned int DEFAULT_VECTOR_SIZE = 16u; ///< Default size of run vectors.
92 #if defined(DEBUG_ENABLED)
93 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_MARKUP_PROCESSOR");
96 typedef VectorBase::SizeType RunIndex;
99 * @brief Struct used to retrieve the style runs from the mark-up string.
101 template<typename StyleStackType>
104 Vector<StyleStackType> stack; ///< Use a vector as a style stack.
105 unsigned int topIndex; ///< Points the top of the stack.
111 stack.Resize(DEFAULT_VECTOR_SIZE);
114 void Push(StyleStackType item)
116 // Check if there is space inside the style stack.
117 const VectorBase::SizeType size = stack.Count();
120 // Resize the style stack.
121 stack.Resize(2u * size);
124 // Set the item in the top of the stack.
125 *(stack.Begin() + topIndex) = item;
127 // Reposition the pointer to the top of the stack.
133 // Pop the top of the stack.
135 return *(stack.Begin() + topIndex);
140 * @brief Struct used to retrieve spans from the mark-up string.
144 RunIndex colorRunIndex;
145 RunIndex fontRunIndex;
146 RunIndex underlinedCharacterRunIndex;
147 RunIndex backgroundColorRunIndex;
148 RunIndex strikethroughCharacterRunIndex;
149 RunIndex characterSpacingCharacterRunIndex;
153 bool isUnderlinedCharacterDefined;
154 bool isBackgroundColorDefined;
155 bool isStrikethroughDefined;
156 bool isCharacterSpacingDefined;
160 * @brief Initializes a font run description to its defaults.
162 * @param[in,out] fontRun The font description run to initialize.
164 void Initialize(FontDescriptionRun& fontRun)
166 fontRun.characterRun.characterIndex = 0u;
167 fontRun.characterRun.numberOfCharacters = 0u;
168 fontRun.familyName = NULL;
169 fontRun.familyLength = 0u;
170 fontRun.weight = TextAbstraction::FontWeight::NORMAL;
171 fontRun.width = TextAbstraction::FontWidth::NORMAL;
172 fontRun.slant = TextAbstraction::FontSlant::NORMAL;
174 fontRun.familyDefined = false;
175 fontRun.weightDefined = false;
176 fontRun.widthDefined = false;
177 fontRun.slantDefined = false;
178 fontRun.sizeDefined = false;
182 * @brief Initializes a color run description to its defaults.
184 * @param[in,out] colorRun The font description run to initialize.
186 void Initialize(ColorRun& colorRun)
188 colorRun.characterRun.characterIndex = 0u;
189 colorRun.characterRun.numberOfCharacters = 0u;
193 * @brief Initializes a underlined character run to its defaults.
195 * @param[in,out] underlinedCharacterRun The underelined character run to initialize.
197 void Initialize(UnderlinedCharacterRun& underlinedCharacterRun)
199 underlinedCharacterRun.characterRun.characterIndex = 0u;
200 underlinedCharacterRun.characterRun.numberOfCharacters = 0u;
204 * @brief Initializes a span to its defaults.
206 * @param[in,out] span The span to be initialized.
208 void Initialize(Span& span)
210 span.colorRunIndex = 0u;
211 span.isColorDefined = false;
213 span.fontRunIndex = 0u;
214 span.isFontDefined = false;
216 span.underlinedCharacterRunIndex = 0u;
217 span.isUnderlinedCharacterDefined = false;
218 span.backgroundColorRunIndex = 0u;
219 span.isBackgroundColorDefined = false;
222 span.strikethroughCharacterRunIndex = 0u;
223 span.isStrikethroughDefined = false;
226 span.characterSpacingCharacterRunIndex = 0u;
227 span.isCharacterSpacingDefined = false;
231 * @brief Initializes a strikethrough character run to its defaults.
233 * @param[in,out] strikethroughCharacterRun The strikethrough character run to initialize.
235 void Initialize(StrikethroughCharacterRun& strikethroughCharacterRun)
237 strikethroughCharacterRun.characterRun.characterIndex = 0u;
238 strikethroughCharacterRun.characterRun.numberOfCharacters = 0u;
239 strikethroughCharacterRun.properties.colorDefined = false;
243 * @brief Initializes a bounded-paragraph character run to its defaults.
245 * @param[in,out] boundedParagraphRun The bounded paragraphRun run to initialize.
247 void Initialize(BoundedParagraphRun& boundedParagraphRun)
249 boundedParagraphRun.characterRun.characterIndex = 0u;
250 boundedParagraphRun.characterRun.numberOfCharacters = 0u;
254 * @brief Initializes a character-spacing run to its defaults.
256 * @param[in,out] characterSpacingCharacterRun The character-spacing run to initialize.
258 void Initialize(CharacterSpacingCharacterRun& characterSpacingCharacterRun)
260 characterSpacingCharacterRun.characterRun.characterIndex = 0u;
261 characterSpacingCharacterRun.characterRun.numberOfCharacters = 0u;
262 characterSpacingCharacterRun.value = 0.0f;
266 * @brief Splits the tag string into the tag name and its attributes.
268 * The attributes are stored in a vector in the tag.
270 * @param[in,out] tag The tag.
272 void ParseAttributes(Tag& tag)
274 if(tag.buffer == NULL)
279 tag.attributes.Resize(MAX_NUM_OF_ATTRIBUTES);
281 // Find first the tag name.
282 bool isQuotationOpen = false;
284 const char* tagBuffer = tag.buffer;
285 const char* const tagEndBuffer = tagBuffer + tag.length;
287 for(; tagBuffer < tagEndBuffer; ++tagBuffer)
289 const char character = *tagBuffer;
290 if(WHITE_SPACE < character)
296 // Stops counting the length of the tag when a white space is found.
297 // @note a white space is the WHITE_SPACE character and anything below as 'tab', 'return' or 'control characters'.
301 SkipWhiteSpace(tagBuffer, tagEndBuffer);
303 // Find the attributes.
304 unsigned int attributeIndex = 0u;
305 const char* nameBuffer = NULL;
306 const char* valueBuffer = NULL;
307 Length nameLength = 0u;
308 Length valueLength = 0u;
310 bool addToNameValue = true;
311 Length numberOfWhiteSpace = 0u;
312 for(; tagBuffer < tagEndBuffer; ++tagBuffer)
314 const char character = *tagBuffer;
315 if((WHITE_SPACE >= character) && !isQuotationOpen)
317 if(NULL != valueBuffer)
319 // Remove white spaces at the end of the value.
320 valueLength -= numberOfWhiteSpace;
323 if((NULL != nameBuffer) && (NULL != valueBuffer))
325 // Every time a white space is found, a new attribute is created and stored in the attributes vector.
326 Attribute& attribute = *(tag.attributes.Begin() + attributeIndex);
329 attribute.nameBuffer = nameBuffer;
330 attribute.valueBuffer = valueBuffer;
331 attribute.nameLength = nameLength;
332 attribute.valueLength = valueLength;
339 addToNameValue = true; // next read characters will be added to the name.
342 else if(EQUAL == character) // '='
344 addToNameValue = false; // next read characters will be added to the value.
345 SkipWhiteSpace(tagBuffer, tagEndBuffer);
347 else if(QUOTATION_MARK == character) // '\''
349 // Do not add quotation marks to neither name nor value.
350 isQuotationOpen = !isQuotationOpen;
355 SkipWhiteSpace(tagBuffer, tagEndBuffer);
361 // Adds characters to the name or the value.
364 if(NULL == nameBuffer)
366 nameBuffer = tagBuffer;
374 if(WHITE_SPACE >= character)
376 ++numberOfWhiteSpace;
380 numberOfWhiteSpace = 0u;
383 if(NULL == valueBuffer)
385 valueBuffer = tagBuffer;
392 if(NULL != valueBuffer)
394 // Remove white spaces at the end of the value.
395 valueLength -= numberOfWhiteSpace;
398 if((NULL != nameBuffer) && (NULL != valueBuffer))
400 // Checks if the last attribute needs to be added.
401 Attribute& attribute = *(tag.attributes.Begin() + attributeIndex);
404 attribute.nameBuffer = nameBuffer;
405 attribute.valueBuffer = valueBuffer;
406 attribute.nameLength = nameLength;
407 attribute.valueLength = valueLength;
410 // Resize the vector of attributes.
411 tag.attributes.Resize(attributeIndex);
415 * @brief It parses a tag and its attributes if the given iterator @e it is pointing at a tag beginning.
417 * @param[in,out] markupStringBuffer The mark-up string buffer. It's a const iterator pointing the current character.
418 * @param[in] markupStringEndBuffer Pointer to one character after the end of the mark-up string buffer.
419 * @param[out] tag The tag with its attributes.
421 * @return @e true if the iterator @e it is pointing a mark-up tag. Otherwise @e false.
423 bool IsTag(const char*& markupStringBuffer,
424 const char* const markupStringEndBuffer,
428 bool isQuotationOpen = false;
429 bool attributesFound = false;
430 tag.isEndTag = false;
431 bool isPreviousLessThan = false;
432 bool isPreviousSlash = false;
434 const char character = *markupStringBuffer;
435 if(LESS_THAN == character) // '<'
439 isPreviousLessThan = true;
441 // if the iterator is pointing to a '<' character, then check if it's a mark-up tag is needed.
442 ++markupStringBuffer;
443 if(markupStringBuffer < markupStringEndBuffer)
445 SkipWhiteSpace(markupStringBuffer, markupStringEndBuffer);
447 for(; (!isTag) && (markupStringBuffer < markupStringEndBuffer); ++markupStringBuffer)
449 const char character = *markupStringBuffer;
451 if(!isQuotationOpen && (SLASH == character)) // '/'
453 if(isPreviousLessThan)
459 // if the tag has a '/' it may be an end tag.
460 isPreviousSlash = true;
463 isPreviousLessThan = false;
464 if((markupStringBuffer + 1u < markupStringEndBuffer) && (WHITE_SPACE >= *(markupStringBuffer + 1u)))
466 ++markupStringBuffer;
467 SkipWhiteSpace(markupStringBuffer, markupStringEndBuffer);
468 --markupStringBuffer;
471 else if(GREATER_THAN == character) // '>'
479 isPreviousSlash = false;
480 isPreviousLessThan = false;
482 else if(QUOTATION_MARK == character)
484 isQuotationOpen = !isQuotationOpen;
487 isPreviousSlash = false;
488 isPreviousLessThan = false;
490 else if(WHITE_SPACE >= character) // ' '
492 // If the tag contains white spaces then it may have attributes.
495 attributesFound = true;
501 if(NULL == tag.buffer)
503 tag.buffer = markupStringBuffer;
506 // If it's not any of the 'special' characters then just add it to the tag string.
509 isPreviousSlash = false;
510 isPreviousLessThan = false;
515 // If the tag string has white spaces, then parse the attributes is needed.
518 ParseAttributes(tag);
526 * @brief Returns length of XHTML entity by parsing the text. It also determines if it is XHTML entity or not.
528 * @param[in] markupStringBuffer The mark-up string buffer. It's a const iterator pointing the current character.
529 * @param[in] markupStringEndBuffer Pointing to end of mark-up string buffer.
531 * @return Length of markupText in case of XHTML entity otherwise return 0.
533 unsigned int GetXHTMLEntityLength(const char*& markupStringBuffer,
534 const char* const markupStringEndBuffer)
536 char character = *markupStringBuffer;
537 if(AMPERSAND == character) // '&'
539 // if the iterator is pointing to a '&' character, then check for ';' to find end to XHTML entity.
540 ++markupStringBuffer;
541 if(markupStringBuffer < markupStringEndBuffer)
543 unsigned int len = 1u;
544 for(; markupStringBuffer < markupStringEndBuffer; ++markupStringBuffer)
546 character = *markupStringBuffer;
548 if(SEMI_COLON == character) // ';'
550 // found end of XHTML entity
551 ++markupStringBuffer;
554 else if((AMPERSAND == character) || (BACK_SLASH == character) || (LESS_THAN == character))
565 * @brief It parses a XHTML string which has hex/decimal entity and fill its corresponging utf-8 string.
567 * @param[in] markupText The mark-up text buffer.
568 * @param[out] utf-8 text Corresponding to markup Text
570 * @return true if string is successfully parsed otherwise false
572 bool XHTMLNumericEntityToUtf8(const char* markupText, char* utf8)
576 if(NULL != markupText)
580 // check if hex or decimal entity
581 if((CHAR_ARRAY_END != *markupText) && (HEX_CODE == *markupText))
588 unsigned long l = strtoul(markupText, &end, (isHex ? 16 : 10)); // l contains UTF-32 code in case of correct XHTML entity
590 // check for valid XHTML numeric entities (between '#' or "#x" and ';')
591 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'
593 /* characters XML 1.1 permits */
594 if(((XHTML_DECIMAL_ENTITY_RANGE[0] < l) && (l <= XHTML_DECIMAL_ENTITY_RANGE[1])) ||
595 ((XHTML_DECIMAL_ENTITY_RANGE[2] <= l) && (l <= XHTML_DECIMAL_ENTITY_RANGE[3])) ||
596 ((XHTML_DECIMAL_ENTITY_RANGE[4] <= l) && (l <= XHTML_DECIMAL_ENTITY_RANGE[5])))
598 // Convert UTF32 code to UTF8
599 Utf32ToUtf8(reinterpret_cast<const uint32_t* const>(&l), 1, reinterpret_cast<uint8_t*>(utf8));
608 * @brief Processes a particular tag for the required run (color-run, font-run or underlined-character-run).
610 * @tparam RunType Whether ColorRun , FontDescriptionRun or UnderlinedCharacterRun
612 * @param[in/out] runsContainer The container containing all the runs
613 * @param[in/out] styleStack The style stack
614 * @param[in] tag The tag we are currently processing
615 * @param[in] characterIndex The current character index
616 * @param[in/out] runIndex The run index
617 * @param[in/out] tagReference The tagReference we should increment/decrement
618 * @param[in] parameterSettingFunction This function will be called to set run specific parameters
620 template<typename RunType>
621 void ProcessTagForRun(
622 Vector<RunType>& runsContainer,
623 StyleStack<RunIndex>& styleStack,
625 const CharacterIndex characterIndex,
628 std::function<void(const Tag&, RunType&)> parameterSettingFunction)
636 // Fill the run with the parameters.
637 run.characterRun.characterIndex = characterIndex;
638 parameterSettingFunction(tag, run);
640 // Push the run in the logical model.
641 runsContainer.PushBack(run);
643 // Push the index of the run into the stack.
644 styleStack.Push(runIndex);
646 // Point the next free run.
649 // Increase reference
656 // Pop the top of the stack and set the number of characters of the run.
657 RunType& run = *(runsContainer.Begin() + styleStack.Pop());
658 run.characterRun.numberOfCharacters = characterIndex - run.characterRun.characterIndex;
665 * @brief Processes the item tag
667 * @param[in/out] markupProcessData The markup process data
668 * @param[in] tag The current tag
669 * @param[in/out] characterIndex The current character index
672 MarkupProcessData& markupProcessData,
674 CharacterIndex& characterIndex)
678 // Create an embedded item instance.
680 item.characterIndex = characterIndex;
681 ProcessEmbeddedItem(tag, item);
683 markupProcessData.items.PushBack(item);
685 // Insert white space character that will be replaced by the item.
686 markupProcessData.markupProcessedText.append(1u, WHITE_SPACE);
692 * @brief Processes the paragraph-tag
694 * @param[in/out] markupProcessData The markup process data
695 * @param[in] tag The current tag
696 * @param[in] isEndBuffer Whether the end of buffer
697 * @param[in/out] characterIndex The current character index
699 void ProcessParagraphTag(
700 MarkupProcessData& markupProcessData,
703 CharacterIndex& characterIndex)
705 if((characterIndex > 0 &&
706 markupProcessData.markupProcessedText[characterIndex - 1u] != NEW_LINE) &&
707 (!(tag.isEndTag && isEndBuffer)))
709 // Insert new-line character at the start and end of paragraph.
710 markupProcessData.markupProcessedText.append(1u, NEW_LINE);
716 * @brief Processes the anchor tag
718 * @param[in/out] markupProcessData The markup process data
719 * @param[in] tag The current tag
720 * @param[in/out] characterIndex The current character index
722 void ProcessAnchorTag(
723 MarkupProcessData& markupProcessData,
725 CharacterIndex& characterIndex)
729 // Create an anchor instance.
731 anchor.startIndex = characterIndex;
732 anchor.endIndex = 0u;
733 ProcessAnchor(tag, anchor);
734 markupProcessData.anchors.PushBack(anchor);
739 unsigned int count = markupProcessData.anchors.Count();
742 markupProcessData.anchors[count - 1].endIndex = characterIndex;
748 * @brief Processes span tag for the color-run & font-run.
750 * @param[in] spanTag The tag we are currently processing
751 * @param[inout] spanStack The spans stack
752 * @param[inout] colorRuns The container containing all the color runs
753 * @param[inout] fontRuns The container containing all the font description runs
754 * @param[inout] underlinedCharacterRuns The container containing all the underlined character runs
755 * @param[inout] strikethroughCharacterRuns The container containing all the strikethroughed character runs
756 * @param[inout] colorRunIndex The color run index
757 * @param[inout] fontRunIndex The font run index
758 * @param[inout] underlinedCharacterRunIndex The underlined character run index
759 * @param[inout] strikethroughCharacterRunIndex The strikethroughed character run index
760 * @param[in] characterIndex The current character index
761 * @param[in] tagReference The tagReference we should increment/decrement
763 void ProcessSpanForRun(
765 StyleStack<Span>& spanStack,
766 Vector<ColorRun>& colorRuns,
767 Vector<FontDescriptionRun>& fontRuns,
768 Vector<UnderlinedCharacterRun>& underlinedCharacterRuns,
769 Vector<ColorRun>& backgroundColorRuns,
770 Vector<StrikethroughCharacterRun>& strikethroughCharacterRuns,
771 Vector<CharacterSpacingCharacterRun>& characterSpacingCharacterRuns,
772 RunIndex& colorRunIndex,
773 RunIndex& fontRunIndex,
774 RunIndex& underlinedCharacterRunIndex,
775 RunIndex& backgroundColorRunIndex,
776 RunIndex& strikethroughCharacterRunIndex,
777 RunIndex& characterSpacingCharacterRunIndex,
778 const CharacterIndex characterIndex,
781 if(!spanTag.isEndTag)
785 Initialize(colorRun);
787 FontDescriptionRun fontRun;
790 UnderlinedCharacterRun underlinedCharacterRun;
791 Initialize(underlinedCharacterRun);
793 ColorRun backgroundColorRun;
794 Initialize(backgroundColorRun);
796 StrikethroughCharacterRun strikethroughCharacterRun;
797 Initialize(strikethroughCharacterRun);
799 CharacterSpacingCharacterRun characterSpacingCharacterRun;
800 Initialize(characterSpacingCharacterRun);
805 // Fill the run with the parameters.
806 colorRun.characterRun.characterIndex = characterIndex;
807 fontRun.characterRun.characterIndex = characterIndex;
808 underlinedCharacterRun.characterRun.characterIndex = characterIndex;
809 backgroundColorRun.characterRun.characterIndex = characterIndex;
810 strikethroughCharacterRun.characterRun.characterIndex = characterIndex;
811 characterSpacingCharacterRun.characterRun.characterIndex = characterIndex;
813 span.colorRunIndex = colorRunIndex;
814 span.fontRunIndex = fontRunIndex;
815 span.underlinedCharacterRunIndex = underlinedCharacterRunIndex;
816 span.backgroundColorRunIndex = backgroundColorRunIndex;
817 span.strikethroughCharacterRunIndex = strikethroughCharacterRunIndex;
818 span.characterSpacingCharacterRunIndex = characterSpacingCharacterRunIndex;
820 ProcessSpanTag(spanTag,
823 underlinedCharacterRun,
825 strikethroughCharacterRun,
826 characterSpacingCharacterRun,
829 span.isUnderlinedCharacterDefined,
830 span.isBackgroundColorDefined,
831 span.isStrikethroughDefined,
832 span.isCharacterSpacingDefined);
834 // Push the span into the stack.
835 spanStack.Push(span);
837 // Point the next free run.
838 if(span.isColorDefined)
840 // Push the run in the logical model.
841 colorRuns.PushBack(colorRun);
845 if(span.isFontDefined)
847 // Push the run in the logical model.
848 fontRuns.PushBack(fontRun);
852 if(span.isUnderlinedCharacterDefined)
854 // Push the run in the logical model.
855 underlinedCharacterRuns.PushBack(underlinedCharacterRun);
856 ++underlinedCharacterRunIndex;
859 if(span.isBackgroundColorDefined)
861 // Push the run in the logical model.
862 backgroundColorRuns.PushBack(backgroundColorRun);
863 ++backgroundColorRunIndex;
866 if(span.isStrikethroughDefined)
868 // Push the run in the logical model.
869 strikethroughCharacterRuns.PushBack(strikethroughCharacterRun);
870 ++strikethroughCharacterRunIndex;
873 if(span.isCharacterSpacingDefined)
875 // Push the run in the logical model.
876 characterSpacingCharacterRuns.PushBack(characterSpacingCharacterRun);
877 ++characterSpacingCharacterRunIndex;
880 // Increase reference
887 // Pop the top of the stack and set the number of characters of the run.
888 Span span = spanStack.Pop();
890 if(span.isColorDefined)
892 ColorRun& colorRun = *(colorRuns.Begin() + span.colorRunIndex);
893 colorRun.characterRun.numberOfCharacters = characterIndex - colorRun.characterRun.characterIndex;
896 if(span.isFontDefined)
898 FontDescriptionRun& fontRun = *(fontRuns.Begin() + span.fontRunIndex);
899 fontRun.characterRun.numberOfCharacters = characterIndex - fontRun.characterRun.characterIndex;
902 if(span.isUnderlinedCharacterDefined)
904 UnderlinedCharacterRun& underlinedCharacterRun = *(underlinedCharacterRuns.Begin() + span.underlinedCharacterRunIndex);
905 underlinedCharacterRun.characterRun.numberOfCharacters = characterIndex - underlinedCharacterRun.characterRun.characterIndex;
908 if(span.isBackgroundColorDefined)
910 ColorRun& backgroundColorRun = *(backgroundColorRuns.Begin() + span.backgroundColorRunIndex);
911 backgroundColorRun.characterRun.numberOfCharacters = characterIndex - backgroundColorRun.characterRun.characterIndex;
914 if(span.isStrikethroughDefined)
916 StrikethroughCharacterRun& strikethroughCharacterRun = *(strikethroughCharacterRuns.Begin() + span.strikethroughCharacterRunIndex);
917 strikethroughCharacterRun.characterRun.numberOfCharacters = characterIndex - strikethroughCharacterRun.characterRun.characterIndex;
920 if(span.isCharacterSpacingDefined)
922 CharacterSpacingCharacterRun& characterSpacingCharacterRun = *(characterSpacingCharacterRuns.Begin() + span.characterSpacingCharacterRunIndex);
923 characterSpacingCharacterRun.characterRun.numberOfCharacters = characterIndex - characterSpacingCharacterRun.characterRun.characterIndex;
932 * @brief Resizes the model's vectors
934 * @param[inout] markupProcessData The markup process data
935 * @param[in] fontRunIndex The font run index
936 * @param[in] colorRunIndex The color run index
937 * @param[in] underlinedCharacterRunIndex The underlined character run index
938 * @param[in] strikethroughCharacterRunIndex The strikethroughed character run index
939 * @param[in] backgroundRunIndex The background run index
940 * @param[in] boundedParagraphRunIndex The bounded paragraph run index
941 * @param[in] characterSpacingCharacterRunIndex The character-spacing character run index
944 void ResizeModelVectors(MarkupProcessData& markupProcessData,
945 const RunIndex fontRunIndex,
946 const RunIndex colorRunIndex,
947 const RunIndex underlinedCharacterRunIndex,
948 const RunIndex strikethroughCharacterRunIndex,
949 const RunIndex backgroundRunIndex,
950 const RunIndex boundedParagraphRunIndex,
951 const RunIndex characterSpacingCharacterRunIndex)
953 markupProcessData.fontRuns.Resize(fontRunIndex);
954 markupProcessData.colorRuns.Resize(colorRunIndex);
955 markupProcessData.underlinedCharacterRuns.Resize(underlinedCharacterRunIndex);
956 markupProcessData.strikethroughCharacterRuns.Resize(strikethroughCharacterRunIndex);
957 markupProcessData.backgroundColorRuns.Resize(backgroundRunIndex);
958 markupProcessData.boundedParagraphRuns.Resize(boundedParagraphRunIndex);
959 markupProcessData.characterSpacingCharacterRuns.Resize(characterSpacingCharacterRunIndex);
962 for(unsigned int i = 0; i < colorRunIndex; ++i)
964 ColorRun& run = markupProcessData.colorRuns[i];
965 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);
971 * @brief Processes the markup string buffer
973 * @param[in/out] markupProcessData The markup process data
974 * @param[in/out] markupStringBuffer The markup string buffer pointer
975 * @param[in] markupStringEndBuffer The markup string end buffer pointer
976 * @param[in/out] characterIndex The current character index
978 void ProcessMarkupStringBuffer(
979 MarkupProcessData& markupProcessData,
980 const char*& markupStringBuffer,
981 const char* const markupStringEndBuffer,
982 CharacterIndex& characterIndex)
984 unsigned char character = *markupStringBuffer;
985 const char* markupBuffer = markupStringBuffer;
986 unsigned char count = GetUtf8Length(character);
989 if((BACK_SLASH == character) && (markupStringBuffer + 1u < markupStringEndBuffer))
991 // Adding < , > or & special character.
992 const unsigned char nextCharacter = *(markupStringBuffer + 1u);
993 if((LESS_THAN == nextCharacter) || (GREATER_THAN == nextCharacter) || (AMPERSAND == nextCharacter))
995 character = nextCharacter;
996 ++markupStringBuffer;
998 count = GetUtf8Length(character);
999 markupBuffer = markupStringBuffer;
1002 else // checking if contains XHTML entity or not
1004 const unsigned int len = GetXHTMLEntityLength(markupStringBuffer, markupStringEndBuffer);
1006 // Parse markupStringTxt if it contains XHTML Entity between '&' and ';'
1009 char* entityCode = NULL;
1010 bool result = false;
1013 // Checking if XHTML Numeric Entity
1014 if(HASH == *(markupBuffer + 1u))
1016 entityCode = &utf8[0];
1017 // markupBuffer is currently pointing to '&'. By adding 2u to markupBuffer it will point to numeric string by skipping "&#'
1018 result = XHTMLNumericEntityToUtf8((markupBuffer + 2u), entityCode);
1020 else // Checking if XHTML Named Entity
1022 entityCode = const_cast<char*>(NamedEntityToUtf8(markupBuffer, len));
1023 result = (entityCode != NULL);
1027 markupBuffer = entityCode; //utf8 text assigned to markupBuffer
1028 character = markupBuffer[0];
1032 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Not valid XHTML entity : (%.*s) \n", len, markupBuffer);
1033 markupBuffer = NULL;
1036 else // in case string conatins Start of XHTML Entity('&') but not its end character(';')
1038 if(character == AMPERSAND)
1040 markupBuffer = NULL;
1041 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Not Well formed XHTML content \n");
1046 if(markupBuffer != NULL)
1048 const unsigned char numberOfBytes = GetUtf8Length(character);
1049 markupProcessData.markupProcessedText.push_back(character);
1051 for(unsigned char i = 1u; i < numberOfBytes; ++i)
1054 markupProcessData.markupProcessedText.push_back(*markupBuffer);
1058 markupStringBuffer += count;
1064 void ProcessMarkupString(const std::string& markupString, MarkupProcessData& markupProcessData)
1066 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "markupString: %s\n", markupString.c_str());
1068 // Reserve space for the plain text.
1069 const Length markupStringSize = markupString.size();
1070 markupProcessData.markupProcessedText.reserve(markupStringSize);
1072 // Stores a struct with the index to the first character of the run, the type of run and its parameters.
1073 StyleStack<RunIndex> styleStack;
1075 // Stores a struct with the index to the first character of the color run & color font for the span.
1076 StyleStack<Span> spanStack;
1078 // Points the next free position in the vector of runs.
1079 RunIndex colorRunIndex = 0u;
1080 RunIndex fontRunIndex = 0u;
1081 RunIndex underlinedCharacterRunIndex = 0u;
1082 RunIndex backgroundRunIndex = 0u;
1083 RunIndex strikethroughCharacterRunIndex = 0u;
1084 RunIndex boundedParagraphRunIndex = 0u;
1085 RunIndex characterSpacingCharacterRunIndex = 0u;
1087 // check tag reference
1088 int colorTagReference = 0u;
1089 int fontTagReference = 0u;
1090 int iTagReference = 0u;
1091 int bTagReference = 0u;
1092 int uTagReference = 0u;
1093 int backgroundTagReference = 0u;
1094 int spanTagReference = 0u;
1095 int sTagReference = 0u;
1096 int pTagReference = 0u;
1097 int characterSpacingTagReference = 0u;
1099 // Give an initial default value to the model's vectors.
1100 markupProcessData.colorRuns.Reserve(DEFAULT_VECTOR_SIZE);
1101 markupProcessData.fontRuns.Reserve(DEFAULT_VECTOR_SIZE);
1102 markupProcessData.underlinedCharacterRuns.Reserve(DEFAULT_VECTOR_SIZE);
1103 markupProcessData.backgroundColorRuns.Reserve(DEFAULT_VECTOR_SIZE);
1104 markupProcessData.strikethroughCharacterRuns.Reserve(DEFAULT_VECTOR_SIZE);
1105 markupProcessData.characterSpacingCharacterRuns.Reserve(DEFAULT_VECTOR_SIZE);
1107 // Get the mark-up string buffer.
1108 const char* markupStringBuffer = markupString.c_str();
1109 const char* const markupStringEndBuffer = markupStringBuffer + markupStringSize;
1112 CharacterIndex characterIndex = 0u;
1113 for(; markupStringBuffer < markupStringEndBuffer;)
1115 tag.attributes.Clear();
1116 if(IsTag(markupStringBuffer,
1117 markupStringEndBuffer,
1120 if(TokenComparison(XHTML_COLOR_TAG, tag.buffer, tag.length))
1122 ProcessTagForRun<ColorRun>(
1123 markupProcessData.colorRuns, styleStack, tag, characterIndex, colorRunIndex, colorTagReference, [](const Tag& tag, ColorRun& run) { ProcessColorTag(tag, run); });
1124 } // <color></color>
1125 else if(TokenComparison(XHTML_I_TAG, tag.buffer, tag.length))
1127 ProcessTagForRun<FontDescriptionRun>(
1128 markupProcessData.fontRuns, styleStack, tag, characterIndex, fontRunIndex, iTagReference, [](const Tag&, FontDescriptionRun& fontRun) {
1129 fontRun.slant = TextAbstraction::FontSlant::ITALIC;
1130 fontRun.slantDefined = true;
1133 else if(TokenComparison(XHTML_U_TAG, tag.buffer, tag.length))
1135 ProcessTagForRun<UnderlinedCharacterRun>(
1136 markupProcessData.underlinedCharacterRuns, styleStack, tag, characterIndex, underlinedCharacterRunIndex, uTagReference, [](const Tag& tag, UnderlinedCharacterRun& run) { ProcessUnderlineTag(tag, run); });
1138 else if(TokenComparison(XHTML_B_TAG, tag.buffer, tag.length))
1140 ProcessTagForRun<FontDescriptionRun>(
1141 markupProcessData.fontRuns, styleStack, tag, characterIndex, fontRunIndex, bTagReference, [](const Tag&, FontDescriptionRun& fontRun) {
1142 fontRun.weight = TextAbstraction::FontWeight::BOLD;
1143 fontRun.weightDefined = true;
1146 else if(TokenComparison(XHTML_FONT_TAG, tag.buffer, tag.length))
1148 ProcessTagForRun<FontDescriptionRun>(
1149 markupProcessData.fontRuns, styleStack, tag, characterIndex, fontRunIndex, fontTagReference, [](const Tag& tag, FontDescriptionRun& fontRun) { ProcessFontTag(tag, fontRun); });
1151 else if(TokenComparison(XHTML_ANCHOR_TAG, tag.buffer, tag.length))
1154 ProcessAnchorTag(markupProcessData, tag, characterIndex);
1156 ProcessTagForRun<ColorRun>(
1157 markupProcessData.colorRuns, styleStack, tag, characterIndex, colorRunIndex, colorTagReference, [](const Tag& tag, ColorRun& run) {
1158 run.color = Color::BLUE;
1159 ProcessColorTag(tag, run);
1162 ProcessTagForRun<UnderlinedCharacterRun>(
1163 markupProcessData.underlinedCharacterRuns, styleStack, tag, characterIndex, underlinedCharacterRunIndex, uTagReference, [](const Tag& tag, UnderlinedCharacterRun& run) {
1164 run.properties.color = Color::BLUE;
1165 run.properties.colorDefined = true;
1166 ProcessUnderlineTag(tag, run);
1168 } // <a href=https://www.tizen.org>tizen</a>
1169 else if(TokenComparison(XHTML_SHADOW_TAG, tag.buffer, tag.length))
1171 // TODO: If !tag.isEndTag, then create a new shadow run.
1172 // else Pop the top of the stack and set the number of characters of the run.
1173 } // <shadow></shadow>
1174 else if(TokenComparison(XHTML_GLOW_TAG, tag.buffer, tag.length))
1176 // TODO: If !tag.isEndTag, then create a new glow run.
1177 // else Pop the top of the stack and set the number of characters of the run.
1179 else if(TokenComparison(XHTML_OUTLINE_TAG, tag.buffer, tag.length))
1181 // TODO: If !tag.isEndTag, then create a new outline run.
1182 // else Pop the top of the stack and set the number of characters of the run.
1183 } // <outline></outline>
1184 else if(TokenComparison(XHTML_ITEM_TAG, tag.buffer, tag.length))
1186 ProcessItemTag(markupProcessData, tag, characterIndex);
1188 else if(TokenComparison(XHTML_BACKGROUND_TAG, tag.buffer, tag.length))
1190 ProcessTagForRun<ColorRun>(
1191 markupProcessData.backgroundColorRuns, styleStack, tag, characterIndex, backgroundRunIndex, backgroundTagReference, [](const Tag& tag, ColorRun& run) { ProcessBackground(tag, run); });
1193 else if(TokenComparison(XHTML_SPAN_TAG, tag.buffer, tag.length))
1195 ProcessSpanForRun(tag,
1197 markupProcessData.colorRuns,
1198 markupProcessData.fontRuns,
1199 markupProcessData.underlinedCharacterRuns,
1200 markupProcessData.backgroundColorRuns,
1201 markupProcessData.strikethroughCharacterRuns,
1202 markupProcessData.characterSpacingCharacterRuns,
1205 underlinedCharacterRunIndex,
1207 strikethroughCharacterRunIndex,
1208 characterSpacingCharacterRunIndex,
1212 else if(TokenComparison(XHTML_STRIKETHROUGH_TAG, tag.buffer, tag.length))
1214 ProcessTagForRun<StrikethroughCharacterRun>(
1215 markupProcessData.strikethroughCharacterRuns, styleStack, tag, characterIndex, strikethroughCharacterRunIndex, sTagReference, [](const Tag& tag, StrikethroughCharacterRun& run) { ProcessStrikethroughTag(tag, run); });
1217 else if(TokenComparison(XHTML_PARAGRAPH_TAG, tag.buffer, tag.length))
1219 ProcessParagraphTag(markupProcessData, tag, (markupStringBuffer == markupStringEndBuffer), characterIndex);
1220 ProcessTagForRun<BoundedParagraphRun>(
1221 markupProcessData.boundedParagraphRuns, styleStack, tag, characterIndex, boundedParagraphRunIndex, pTagReference, [](const Tag& tag, BoundedParagraphRun& run) { ProcessAttributesOfParagraphTag(tag, run); });
1223 else if(TokenComparison(XHTML_CHARACTER_SPACING_TAG, tag.buffer, tag.length))
1225 ProcessTagForRun<CharacterSpacingCharacterRun>(
1226 markupProcessData.characterSpacingCharacterRuns, styleStack, tag, characterIndex, characterSpacingCharacterRunIndex, characterSpacingTagReference, [](const Tag& tag, CharacterSpacingCharacterRun& run) { ProcessCharacterSpacingTag(tag, run); });
1227 } // <char-spacing></char-spacing>
1228 } // end if( IsTag() )
1229 else if(markupStringBuffer < markupStringEndBuffer)
1231 ProcessMarkupStringBuffer(markupProcessData, markupStringBuffer, markupStringEndBuffer, characterIndex);
1235 // Resize the model's vectors.
1236 ResizeModelVectors(markupProcessData, fontRunIndex, colorRunIndex, underlinedCharacterRunIndex, strikethroughCharacterRunIndex, backgroundRunIndex, boundedParagraphRunIndex, characterSpacingCharacterRunIndex);
1238 // Handle the nested tags
1239 OverrideNestedUnderlinedCharacterRuns(markupProcessData.underlinedCharacterRuns);
1240 OverrideNestedStrikethroughCharacterRuns(markupProcessData.strikethroughCharacterRuns);
1245 } // namespace Toolkit