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 const unsigned int MAX_NUM_OF_ATTRIBUTES = 13u; ///< 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' and 's-height' attrubutes.
89 const unsigned int DEFAULT_VECTOR_SIZE = 16u; ///< Default size of run vectors.
91 #if defined(DEBUG_ENABLED)
92 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_MARKUP_PROCESSOR");
95 typedef VectorBase::SizeType RunIndex;
98 * @brief Struct used to retrieve the style runs from the mark-up string.
100 template<typename StyleStackType>
103 Vector<StyleStackType> stack; ///< Use a vector as a style stack.
104 unsigned int topIndex; ///< Points the top of the stack.
110 stack.Resize(DEFAULT_VECTOR_SIZE);
113 void Push(StyleStackType item)
115 // Check if there is space inside the style stack.
116 const VectorBase::SizeType size = stack.Count();
119 // Resize the style stack.
120 stack.Resize(2u * size);
123 // Set the item in the top of the stack.
124 *(stack.Begin() + topIndex) = item;
126 // Reposition the pointer to the top of the stack.
132 // Pop the top of the stack.
134 return *(stack.Begin() + topIndex);
139 * @brief Struct used to retrieve spans from the mark-up string.
143 RunIndex colorRunIndex;
144 RunIndex fontRunIndex;
145 RunIndex underlinedCharacterRunIndex;
146 RunIndex backgroundColorRunIndex;
147 RunIndex strikethroughCharacterRunIndex;
151 bool isUnderlinedCharacterDefined;
152 bool isBackgroundColorDefined;
153 bool isStrikethroughDefined;
157 * @brief Initializes a font run description to its defaults.
159 * @param[in,out] fontRun The font description run to initialize.
161 void Initialize(FontDescriptionRun& fontRun)
163 fontRun.characterRun.characterIndex = 0u;
164 fontRun.characterRun.numberOfCharacters = 0u;
165 fontRun.familyName = NULL;
166 fontRun.familyLength = 0u;
167 fontRun.weight = TextAbstraction::FontWeight::NORMAL;
168 fontRun.width = TextAbstraction::FontWidth::NORMAL;
169 fontRun.slant = TextAbstraction::FontSlant::NORMAL;
171 fontRun.familyDefined = false;
172 fontRun.weightDefined = false;
173 fontRun.widthDefined = false;
174 fontRun.slantDefined = false;
175 fontRun.sizeDefined = false;
179 * @brief Initializes a color run description to its defaults.
181 * @param[in,out] colorRun The font description run to initialize.
183 void Initialize(ColorRun& colorRun)
185 colorRun.characterRun.characterIndex = 0u;
186 colorRun.characterRun.numberOfCharacters = 0u;
190 * @brief Initializes a underlined character run to its defaults.
192 * @param[in,out] underlinedCharacterRun The underelined character run to initialize.
194 void Initialize(UnderlinedCharacterRun& underlinedCharacterRun)
196 underlinedCharacterRun.characterRun.characterIndex = 0u;
197 underlinedCharacterRun.characterRun.numberOfCharacters = 0u;
201 * @brief Initializes a span to its defaults.
203 * @param[in,out] span The span to be initialized.
205 void Initialize(Span& span)
207 span.colorRunIndex = 0u;
208 span.isColorDefined = false;
209 span.fontRunIndex = 0u;
210 span.isFontDefined = false;
211 span.underlinedCharacterRunIndex = 0u;
212 span.isUnderlinedCharacterDefined = false;
213 span.backgroundColorRunIndex = 0u;
214 span.isBackgroundColorDefined = false;
217 span.strikethroughCharacterRunIndex = 0u;
218 span.isStrikethroughDefined = false;
222 * @brief Initializes a strikethrough character run to its defaults.
224 * @param[in,out] strikethroughCharacterRun The strikethrough character run to initialize.
226 void Initialize(StrikethroughCharacterRun& strikethroughCharacterRun)
228 strikethroughCharacterRun.characterRun.characterIndex = 0u;
229 strikethroughCharacterRun.characterRun.numberOfCharacters = 0u;
230 strikethroughCharacterRun.properties.colorDefined = false;
234 * @brief Initializes a bounded-paragraph character run to its defaults.
236 * @param[in,out] boundedParagraphRun The bounded paragraphRun run to initialize.
238 void Initialize(BoundedParagraphRun& boundedParagraphRun)
240 boundedParagraphRun.characterRun.characterIndex = 0u;
241 boundedParagraphRun.characterRun.numberOfCharacters = 0u;
245 * @brief Initializes a character-spacing run to its defaults.
247 * @param[in,out] characterSpacingCharacterRun The character-spacing run to initialize.
249 void Initialize(CharacterSpacingCharacterRun& characterSpacingCharacterRun)
251 characterSpacingCharacterRun.characterRun.characterIndex = 0u;
252 characterSpacingCharacterRun.characterRun.numberOfCharacters = 0u;
253 characterSpacingCharacterRun.value = 0.0f;
257 * @brief Splits the tag string into the tag name and its attributes.
259 * The attributes are stored in a vector in the tag.
261 * @param[in,out] tag The tag.
263 void ParseAttributes(Tag& tag)
265 if(tag.buffer == NULL)
270 tag.attributes.Resize(MAX_NUM_OF_ATTRIBUTES);
272 // Find first the tag name.
273 bool isQuotationOpen = false;
275 const char* tagBuffer = tag.buffer;
276 const char* const tagEndBuffer = tagBuffer + tag.length;
278 for(; tagBuffer < tagEndBuffer; ++tagBuffer)
280 const char character = *tagBuffer;
281 if(WHITE_SPACE < character)
287 // Stops counting the length of the tag when a white space is found.
288 // @note a white space is the WHITE_SPACE character and anything below as 'tab', 'return' or 'control characters'.
292 SkipWhiteSpace(tagBuffer, tagEndBuffer);
294 // Find the attributes.
295 unsigned int attributeIndex = 0u;
296 const char* nameBuffer = NULL;
297 const char* valueBuffer = NULL;
298 Length nameLength = 0u;
299 Length valueLength = 0u;
301 bool addToNameValue = true;
302 Length numberOfWhiteSpace = 0u;
303 for(; tagBuffer < tagEndBuffer; ++tagBuffer)
305 const char character = *tagBuffer;
306 if((WHITE_SPACE >= character) && !isQuotationOpen)
308 if(NULL != valueBuffer)
310 // Remove white spaces at the end of the value.
311 valueLength -= numberOfWhiteSpace;
314 if((NULL != nameBuffer) && (NULL != valueBuffer))
316 // Every time a white space is found, a new attribute is created and stored in the attributes vector.
317 Attribute& attribute = *(tag.attributes.Begin() + attributeIndex);
320 attribute.nameBuffer = nameBuffer;
321 attribute.valueBuffer = valueBuffer;
322 attribute.nameLength = nameLength;
323 attribute.valueLength = valueLength;
330 addToNameValue = true; // next read characters will be added to the name.
333 else if(EQUAL == character) // '='
335 addToNameValue = false; // next read characters will be added to the value.
336 SkipWhiteSpace(tagBuffer, tagEndBuffer);
338 else if(QUOTATION_MARK == character) // '\''
340 // Do not add quotation marks to neither name nor value.
341 isQuotationOpen = !isQuotationOpen;
346 SkipWhiteSpace(tagBuffer, tagEndBuffer);
352 // Adds characters to the name or the value.
355 if(NULL == nameBuffer)
357 nameBuffer = tagBuffer;
365 if(WHITE_SPACE >= character)
367 ++numberOfWhiteSpace;
371 numberOfWhiteSpace = 0u;
374 if(NULL == valueBuffer)
376 valueBuffer = tagBuffer;
383 if(NULL != valueBuffer)
385 // Remove white spaces at the end of the value.
386 valueLength -= numberOfWhiteSpace;
389 if((NULL != nameBuffer) && (NULL != valueBuffer))
391 // Checks if the last attribute needs to be added.
392 Attribute& attribute = *(tag.attributes.Begin() + attributeIndex);
395 attribute.nameBuffer = nameBuffer;
396 attribute.valueBuffer = valueBuffer;
397 attribute.nameLength = nameLength;
398 attribute.valueLength = valueLength;
401 // Resize the vector of attributes.
402 tag.attributes.Resize(attributeIndex);
406 * @brief It parses a tag and its attributes if the given iterator @e it is pointing at a tag beginning.
408 * @param[in,out] markupStringBuffer The mark-up string buffer. It's a const iterator pointing the current character.
409 * @param[in] markupStringEndBuffer Pointer to one character after the end of the mark-up string buffer.
410 * @param[out] tag The tag with its attributes.
412 * @return @e true if the iterator @e it is pointing a mark-up tag. Otherwise @e false.
414 bool IsTag(const char*& markupStringBuffer,
415 const char* const markupStringEndBuffer,
419 bool isQuotationOpen = false;
420 bool attributesFound = false;
421 tag.isEndTag = false;
422 bool isPreviousLessThan = false;
423 bool isPreviousSlash = false;
425 const char character = *markupStringBuffer;
426 if(LESS_THAN == character) // '<'
430 isPreviousLessThan = true;
432 // if the iterator is pointing to a '<' character, then check if it's a mark-up tag is needed.
433 ++markupStringBuffer;
434 if(markupStringBuffer < markupStringEndBuffer)
436 SkipWhiteSpace(markupStringBuffer, markupStringEndBuffer);
438 for(; (!isTag) && (markupStringBuffer < markupStringEndBuffer); ++markupStringBuffer)
440 const char character = *markupStringBuffer;
442 if(!isQuotationOpen && (SLASH == character)) // '/'
444 if(isPreviousLessThan)
450 // if the tag has a '/' it may be an end tag.
451 isPreviousSlash = true;
454 isPreviousLessThan = false;
455 if((markupStringBuffer + 1u < markupStringEndBuffer) && (WHITE_SPACE >= *(markupStringBuffer + 1u)))
457 ++markupStringBuffer;
458 SkipWhiteSpace(markupStringBuffer, markupStringEndBuffer);
459 --markupStringBuffer;
462 else if(GREATER_THAN == character) // '>'
470 isPreviousSlash = false;
471 isPreviousLessThan = false;
473 else if(QUOTATION_MARK == character)
475 isQuotationOpen = !isQuotationOpen;
478 isPreviousSlash = false;
479 isPreviousLessThan = false;
481 else if(WHITE_SPACE >= character) // ' '
483 // If the tag contains white spaces then it may have attributes.
486 attributesFound = true;
492 if(NULL == tag.buffer)
494 tag.buffer = markupStringBuffer;
497 // If it's not any of the 'special' characters then just add it to the tag string.
500 isPreviousSlash = false;
501 isPreviousLessThan = false;
506 // If the tag string has white spaces, then parse the attributes is needed.
509 ParseAttributes(tag);
517 * @brief Returns length of XHTML entity by parsing the text. It also determines if it is XHTML entity or not.
519 * @param[in] markupStringBuffer The mark-up string buffer. It's a const iterator pointing the current character.
520 * @param[in] markupStringEndBuffer Pointing to end of mark-up string buffer.
522 * @return Length of markupText in case of XHTML entity otherwise return 0.
524 unsigned int GetXHTMLEntityLength(const char*& markupStringBuffer,
525 const char* const markupStringEndBuffer)
527 char character = *markupStringBuffer;
528 if(AMPERSAND == character) // '&'
530 // if the iterator is pointing to a '&' character, then check for ';' to find end to XHTML entity.
531 ++markupStringBuffer;
532 if(markupStringBuffer < markupStringEndBuffer)
534 unsigned int len = 1u;
535 for(; markupStringBuffer < markupStringEndBuffer; ++markupStringBuffer)
537 character = *markupStringBuffer;
539 if(SEMI_COLON == character) // ';'
541 // found end of XHTML entity
542 ++markupStringBuffer;
545 else if((AMPERSAND == character) || (BACK_SLASH == character) || (LESS_THAN == character))
556 * @brief It parses a XHTML string which has hex/decimal entity and fill its corresponging utf-8 string.
558 * @param[in] markupText The mark-up text buffer.
559 * @param[out] utf-8 text Corresponding to markup Text
561 * @return true if string is successfully parsed otherwise false
563 bool XHTMLNumericEntityToUtf8(const char* markupText, char* utf8)
567 if(NULL != markupText)
571 // check if hex or decimal entity
572 if((CHAR_ARRAY_END != *markupText) && (HEX_CODE == *markupText))
579 unsigned long l = strtoul(markupText, &end, (isHex ? 16 : 10)); // l contains UTF-32 code in case of correct XHTML entity
581 // check for valid XHTML numeric entities (between '#' or "#x" and ';')
582 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'
584 /* characters XML 1.1 permits */
585 if(((XHTML_DECIMAL_ENTITY_RANGE[0] < l) && (l <= XHTML_DECIMAL_ENTITY_RANGE[1])) ||
586 ((XHTML_DECIMAL_ENTITY_RANGE[2] <= l) && (l <= XHTML_DECIMAL_ENTITY_RANGE[3])) ||
587 ((XHTML_DECIMAL_ENTITY_RANGE[4] <= l) && (l <= XHTML_DECIMAL_ENTITY_RANGE[5])))
589 // Convert UTF32 code to UTF8
590 Utf32ToUtf8(reinterpret_cast<const uint32_t* const>(&l), 1, reinterpret_cast<uint8_t*>(utf8));
599 * @brief Processes a particular tag for the required run (color-run, font-run or underlined-character-run).
601 * @tparam RunType Whether ColorRun , FontDescriptionRun or UnderlinedCharacterRun
603 * @param[in/out] runsContainer The container containing all the runs
604 * @param[in/out] styleStack The style stack
605 * @param[in] tag The tag we are currently processing
606 * @param[in] characterIndex The current character index
607 * @param[in/out] runIndex The run index
608 * @param[in/out] tagReference The tagReference we should increment/decrement
609 * @param[in] parameterSettingFunction This function will be called to set run specific parameters
611 template<typename RunType>
612 void ProcessTagForRun(
613 Vector<RunType>& runsContainer,
614 StyleStack<RunIndex>& styleStack,
616 const CharacterIndex characterIndex,
619 std::function<void(const Tag&, RunType&)> parameterSettingFunction)
627 // Fill the run with the parameters.
628 run.characterRun.characterIndex = characterIndex;
629 parameterSettingFunction(tag, run);
631 // Push the run in the logical model.
632 runsContainer.PushBack(run);
634 // Push the index of the run into the stack.
635 styleStack.Push(runIndex);
637 // Point the next free run.
640 // Increase reference
647 // Pop the top of the stack and set the number of characters of the run.
648 RunType& run = *(runsContainer.Begin() + styleStack.Pop());
649 run.characterRun.numberOfCharacters = characterIndex - run.characterRun.characterIndex;
656 * @brief Processes the item tag
658 * @param[in/out] markupProcessData The markup process data
659 * @param[in] tag The current tag
660 * @param[in/out] characterIndex The current character index
663 MarkupProcessData& markupProcessData,
665 CharacterIndex& characterIndex)
669 // Create an embedded item instance.
671 item.characterIndex = characterIndex;
672 ProcessEmbeddedItem(tag, item);
674 markupProcessData.items.PushBack(item);
676 // Insert white space character that will be replaced by the item.
677 markupProcessData.markupProcessedText.append(1u, WHITE_SPACE);
683 * @brief Processes the paragraph-tag
685 * @param[in/out] markupProcessData The markup process data
686 * @param[in] tag The current tag
687 * @param[in] isEndBuffer Whether the end of buffer
688 * @param[in/out] characterIndex The current character index
690 void ProcessParagraphTag(
691 MarkupProcessData& markupProcessData,
694 CharacterIndex& characterIndex)
696 if((characterIndex > 0 &&
697 markupProcessData.markupProcessedText[characterIndex - 1u] != NEW_LINE) &&
698 (!(tag.isEndTag && isEndBuffer)))
700 // Insert new-line character at the start and end of paragraph.
701 markupProcessData.markupProcessedText.append(1u, NEW_LINE);
707 * @brief Processes the anchor tag
709 * @param[in/out] markupProcessData The markup process data
710 * @param[in] tag The current tag
711 * @param[in/out] characterIndex The current character index
713 void ProcessAnchorTag(
714 MarkupProcessData& markupProcessData,
716 CharacterIndex& characterIndex)
720 // Create an anchor instance.
722 anchor.startIndex = characterIndex;
723 anchor.endIndex = 0u;
724 ProcessAnchor(tag, anchor);
725 markupProcessData.anchors.PushBack(anchor);
730 unsigned int count = markupProcessData.anchors.Count();
733 markupProcessData.anchors[count - 1].endIndex = characterIndex;
739 * @brief Processes span tag for the color-run & font-run.
741 * @param[in] spanTag The tag we are currently processing
742 * @param[inout] spanStack The spans stack
743 * @param[inout] colorRuns The container containing all the color runs
744 * @param[inout] fontRuns The container containing all the font description runs
745 * @param[inout] underlinedCharacterRuns The container containing all the underlined character runs
746 * @param[inout] strikethroughCharacterRuns The container containing all the strikethroughed character runs
747 * @param[inout] colorRunIndex The color run index
748 * @param[inout] fontRunIndex The font run index
749 * @param[inout] underlinedCharacterRunIndex The underlined character run index
750 * @param[inout] strikethroughCharacterRunIndex The strikethroughed character run index
751 * @param[in] characterIndex The current character index
752 * @param[in] tagReference The tagReference we should increment/decrement
754 void ProcessSpanForRun(
756 StyleStack<Span>& spanStack,
757 Vector<ColorRun>& colorRuns,
758 Vector<FontDescriptionRun>& fontRuns,
759 Vector<UnderlinedCharacterRun>& underlinedCharacterRuns,
760 Vector<ColorRun>& backgroundColorRuns,
761 Vector<StrikethroughCharacterRun>& strikethroughCharacterRuns,
762 RunIndex& colorRunIndex,
763 RunIndex& fontRunIndex,
764 RunIndex& underlinedCharacterRunIndex,
765 RunIndex& backgroundColorRunIndex,
766 RunIndex& strikethroughCharacterRunIndex,
767 const CharacterIndex characterIndex,
770 if(!spanTag.isEndTag)
774 Initialize(colorRun);
776 FontDescriptionRun fontRun;
779 UnderlinedCharacterRun underlinedCharacterRun;
780 Initialize(underlinedCharacterRun);
782 ColorRun backgroundColorRun;
783 Initialize(backgroundColorRun);
785 StrikethroughCharacterRun strikethroughCharacterRun;
786 Initialize(strikethroughCharacterRun);
791 // Fill the run with the parameters.
792 colorRun.characterRun.characterIndex = characterIndex;
793 fontRun.characterRun.characterIndex = characterIndex;
794 underlinedCharacterRun.characterRun.characterIndex = characterIndex;
795 backgroundColorRun.characterRun.characterIndex = characterIndex;
796 strikethroughCharacterRun.characterRun.characterIndex = characterIndex;
798 span.colorRunIndex = colorRunIndex;
799 span.fontRunIndex = fontRunIndex;
800 span.underlinedCharacterRunIndex = underlinedCharacterRunIndex;
801 span.backgroundColorRunIndex = backgroundColorRunIndex;
802 span.strikethroughCharacterRunIndex = strikethroughCharacterRunIndex;
804 ProcessSpanTag(spanTag,
807 underlinedCharacterRun,
809 strikethroughCharacterRun,
812 span.isUnderlinedCharacterDefined,
813 span.isBackgroundColorDefined,
814 span.isStrikethroughDefined);
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 // Increase reference
862 // Pop the top of the stack and set the number of characters of the run.
863 Span span = spanStack.Pop();
865 if(span.isColorDefined)
867 ColorRun& colorRun = *(colorRuns.Begin() + span.colorRunIndex);
868 colorRun.characterRun.numberOfCharacters = characterIndex - colorRun.characterRun.characterIndex;
871 if(span.isFontDefined)
873 FontDescriptionRun& fontRun = *(fontRuns.Begin() + span.fontRunIndex);
874 fontRun.characterRun.numberOfCharacters = characterIndex - fontRun.characterRun.characterIndex;
877 if(span.isUnderlinedCharacterDefined)
879 UnderlinedCharacterRun& underlinedCharacterRun = *(underlinedCharacterRuns.Begin() + span.underlinedCharacterRunIndex);
880 underlinedCharacterRun.characterRun.numberOfCharacters = characterIndex - underlinedCharacterRun.characterRun.characterIndex;
883 if(span.isBackgroundColorDefined)
885 ColorRun& backgroundColorRun = *(backgroundColorRuns.Begin() + span.backgroundColorRunIndex);
886 backgroundColorRun.characterRun.numberOfCharacters = characterIndex - backgroundColorRun.characterRun.characterIndex;
889 if(span.isStrikethroughDefined)
891 StrikethroughCharacterRun& strikethroughCharacterRun = *(strikethroughCharacterRuns.Begin() + span.strikethroughCharacterRunIndex);
892 strikethroughCharacterRun.characterRun.numberOfCharacters = characterIndex - strikethroughCharacterRun.characterRun.characterIndex;
901 * @brief Resizes the model's vectors
903 * @param[inout] markupProcessData The markup process data
904 * @param[in] fontRunIndex The font run index
905 * @param[in] colorRunIndex The color run index
906 * @param[in] underlinedCharacterRunIndex The underlined character run index
907 * @param[in] strikethroughCharacterRunIndex The strikethroughed character run index
908 * @param[in] backgroundRunIndex The background run index
909 * @param[in] boundedParagraphRunIndex The bounded paragraph run index
910 * @param[in] characterSpacingCharacterRunIndex The character-spacing character run index
913 void ResizeModelVectors(MarkupProcessData& markupProcessData,
914 const RunIndex fontRunIndex,
915 const RunIndex colorRunIndex,
916 const RunIndex underlinedCharacterRunIndex,
917 const RunIndex strikethroughCharacterRunIndex,
918 const RunIndex backgroundRunIndex,
919 const RunIndex boundedParagraphRunIndex,
920 const RunIndex characterSpacingCharacterRunIndex)
922 markupProcessData.fontRuns.Resize(fontRunIndex);
923 markupProcessData.colorRuns.Resize(colorRunIndex);
924 markupProcessData.underlinedCharacterRuns.Resize(underlinedCharacterRunIndex);
925 markupProcessData.strikethroughCharacterRuns.Resize(strikethroughCharacterRunIndex);
926 markupProcessData.backgroundColorRuns.Resize(backgroundRunIndex);
927 markupProcessData.boundedParagraphRuns.Resize(boundedParagraphRunIndex);
928 markupProcessData.characterSpacingCharacterRuns.Resize(characterSpacingCharacterRunIndex);
931 for(unsigned int i = 0; i < colorRunIndex; ++i)
933 ColorRun& run = markupProcessData.colorRuns[i];
934 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);
940 * @brief Processes the markup string buffer
942 * @param[in/out] markupProcessData The markup process data
943 * @param[in/out] markupStringBuffer The markup string buffer pointer
944 * @param[in] markupStringEndBuffer The markup string end buffer pointer
945 * @param[in/out] characterIndex The current character index
947 void ProcessMarkupStringBuffer(
948 MarkupProcessData& markupProcessData,
949 const char*& markupStringBuffer,
950 const char* const markupStringEndBuffer,
951 CharacterIndex& characterIndex)
953 unsigned char character = *markupStringBuffer;
954 const char* markupBuffer = markupStringBuffer;
955 unsigned char count = GetUtf8Length(character);
958 if((BACK_SLASH == character) && (markupStringBuffer + 1u < markupStringEndBuffer))
960 // Adding < , > or & special character.
961 const unsigned char nextCharacter = *(markupStringBuffer + 1u);
962 if((LESS_THAN == nextCharacter) || (GREATER_THAN == nextCharacter) || (AMPERSAND == nextCharacter))
964 character = nextCharacter;
965 ++markupStringBuffer;
967 count = GetUtf8Length(character);
968 markupBuffer = markupStringBuffer;
971 else // checking if contains XHTML entity or not
973 const unsigned int len = GetXHTMLEntityLength(markupStringBuffer, markupStringEndBuffer);
975 // Parse markupStringTxt if it contains XHTML Entity between '&' and ';'
978 char* entityCode = NULL;
982 // Checking if XHTML Numeric Entity
983 if(HASH == *(markupBuffer + 1u))
985 entityCode = &utf8[0];
986 // markupBuffer is currently pointing to '&'. By adding 2u to markupBuffer it will point to numeric string by skipping "&#'
987 result = XHTMLNumericEntityToUtf8((markupBuffer + 2u), entityCode);
989 else // Checking if XHTML Named Entity
991 entityCode = const_cast<char*>(NamedEntityToUtf8(markupBuffer, len));
992 result = (entityCode != NULL);
996 markupBuffer = entityCode; //utf8 text assigned to markupBuffer
997 character = markupBuffer[0];
1001 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Not valid XHTML entity : (%.*s) \n", len, markupBuffer);
1002 markupBuffer = NULL;
1005 else // in case string conatins Start of XHTML Entity('&') but not its end character(';')
1007 if(character == AMPERSAND)
1009 markupBuffer = NULL;
1010 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Not Well formed XHTML content \n");
1015 if(markupBuffer != NULL)
1017 const unsigned char numberOfBytes = GetUtf8Length(character);
1018 markupProcessData.markupProcessedText.push_back(character);
1020 for(unsigned char i = 1u; i < numberOfBytes; ++i)
1023 markupProcessData.markupProcessedText.push_back(*markupBuffer);
1027 markupStringBuffer += count;
1033 void ProcessMarkupString(const std::string& markupString, MarkupProcessData& markupProcessData)
1035 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "markupString: %s\n", markupString.c_str());
1037 // Reserve space for the plain text.
1038 const Length markupStringSize = markupString.size();
1039 markupProcessData.markupProcessedText.reserve(markupStringSize);
1041 // Stores a struct with the index to the first character of the run, the type of run and its parameters.
1042 StyleStack<RunIndex> styleStack;
1044 // Stores a struct with the index to the first character of the color run & color font for the span.
1045 StyleStack<Span> spanStack;
1047 // Points the next free position in the vector of runs.
1048 RunIndex colorRunIndex = 0u;
1049 RunIndex fontRunIndex = 0u;
1050 RunIndex underlinedCharacterRunIndex = 0u;
1051 RunIndex backgroundRunIndex = 0u;
1052 RunIndex strikethroughCharacterRunIndex = 0u;
1053 RunIndex boundedParagraphRunIndex = 0u;
1054 RunIndex characterSpacingCharacterRunIndex = 0u;
1056 // check tag reference
1057 int colorTagReference = 0u;
1058 int fontTagReference = 0u;
1059 int iTagReference = 0u;
1060 int bTagReference = 0u;
1061 int uTagReference = 0u;
1062 int backgroundTagReference = 0u;
1063 int spanTagReference = 0u;
1064 int sTagReference = 0u;
1065 int pTagReference = 0u;
1066 int characterSpacingTagReference = 0u;
1068 // Give an initial default value to the model's vectors.
1069 markupProcessData.colorRuns.Reserve(DEFAULT_VECTOR_SIZE);
1070 markupProcessData.fontRuns.Reserve(DEFAULT_VECTOR_SIZE);
1071 markupProcessData.underlinedCharacterRuns.Reserve(DEFAULT_VECTOR_SIZE);
1072 markupProcessData.backgroundColorRuns.Reserve(DEFAULT_VECTOR_SIZE);
1073 markupProcessData.strikethroughCharacterRuns.Reserve(DEFAULT_VECTOR_SIZE);
1074 markupProcessData.characterSpacingCharacterRuns.Reserve(DEFAULT_VECTOR_SIZE);
1076 // Get the mark-up string buffer.
1077 const char* markupStringBuffer = markupString.c_str();
1078 const char* const markupStringEndBuffer = markupStringBuffer + markupStringSize;
1081 CharacterIndex characterIndex = 0u;
1082 for(; markupStringBuffer < markupStringEndBuffer;)
1084 tag.attributes.Clear();
1085 if(IsTag(markupStringBuffer,
1086 markupStringEndBuffer,
1089 if(TokenComparison(XHTML_COLOR_TAG, tag.buffer, tag.length))
1091 ProcessTagForRun<ColorRun>(
1092 markupProcessData.colorRuns, styleStack, tag, characterIndex, colorRunIndex, colorTagReference, [](const Tag& tag, ColorRun& run) { ProcessColorTag(tag, run); });
1093 } // <color></color>
1094 else if(TokenComparison(XHTML_I_TAG, tag.buffer, tag.length))
1096 ProcessTagForRun<FontDescriptionRun>(
1097 markupProcessData.fontRuns, styleStack, tag, characterIndex, fontRunIndex, iTagReference, [](const Tag&, FontDescriptionRun& fontRun) {
1098 fontRun.slant = TextAbstraction::FontSlant::ITALIC;
1099 fontRun.slantDefined = true;
1102 else if(TokenComparison(XHTML_U_TAG, tag.buffer, tag.length))
1104 ProcessTagForRun<UnderlinedCharacterRun>(
1105 markupProcessData.underlinedCharacterRuns, styleStack, tag, characterIndex, underlinedCharacterRunIndex, uTagReference, [](const Tag& tag, UnderlinedCharacterRun& run) { ProcessUnderlineTag(tag, run); });
1107 else if(TokenComparison(XHTML_B_TAG, tag.buffer, tag.length))
1109 ProcessTagForRun<FontDescriptionRun>(
1110 markupProcessData.fontRuns, styleStack, tag, characterIndex, fontRunIndex, bTagReference, [](const Tag&, FontDescriptionRun& fontRun) {
1111 fontRun.weight = TextAbstraction::FontWeight::BOLD;
1112 fontRun.weightDefined = true;
1115 else if(TokenComparison(XHTML_FONT_TAG, tag.buffer, tag.length))
1117 ProcessTagForRun<FontDescriptionRun>(
1118 markupProcessData.fontRuns, styleStack, tag, characterIndex, fontRunIndex, fontTagReference, [](const Tag& tag, FontDescriptionRun& fontRun) { ProcessFontTag(tag, fontRun); });
1120 else if(TokenComparison(XHTML_ANCHOR_TAG, tag.buffer, tag.length))
1123 ProcessAnchorTag(markupProcessData, tag, characterIndex);
1125 ProcessTagForRun<ColorRun>(
1126 markupProcessData.colorRuns, styleStack, tag, characterIndex, colorRunIndex, colorTagReference, [](const Tag& tag, ColorRun& run) {
1127 run.color = Color::BLUE;
1128 ProcessColorTag(tag, run);
1131 ProcessTagForRun<UnderlinedCharacterRun>(
1132 markupProcessData.underlinedCharacterRuns, styleStack, tag, characterIndex, underlinedCharacterRunIndex, uTagReference, [](const Tag& tag, UnderlinedCharacterRun& run) {
1133 run.properties.color = Color::BLUE;
1134 run.properties.colorDefined = true;
1135 ProcessUnderlineTag(tag, run);
1137 } // <a href=https://www.tizen.org>tizen</a>
1138 else if(TokenComparison(XHTML_SHADOW_TAG, tag.buffer, tag.length))
1140 // TODO: If !tag.isEndTag, then create a new shadow run.
1141 // else Pop the top of the stack and set the number of characters of the run.
1142 } // <shadow></shadow>
1143 else if(TokenComparison(XHTML_GLOW_TAG, tag.buffer, tag.length))
1145 // TODO: If !tag.isEndTag, then create a new glow run.
1146 // else Pop the top of the stack and set the number of characters of the run.
1148 else if(TokenComparison(XHTML_OUTLINE_TAG, tag.buffer, tag.length))
1150 // TODO: If !tag.isEndTag, then create a new outline run.
1151 // else Pop the top of the stack and set the number of characters of the run.
1152 } // <outline></outline>
1153 else if(TokenComparison(XHTML_ITEM_TAG, tag.buffer, tag.length))
1155 ProcessItemTag(markupProcessData, tag, characterIndex);
1157 else if(TokenComparison(XHTML_BACKGROUND_TAG, tag.buffer, tag.length))
1159 ProcessTagForRun<ColorRun>(
1160 markupProcessData.backgroundColorRuns, styleStack, tag, characterIndex, backgroundRunIndex, backgroundTagReference, [](const Tag& tag, ColorRun& run) { ProcessBackground(tag, run); });
1162 else if(TokenComparison(XHTML_SPAN_TAG, tag.buffer, tag.length))
1164 ProcessSpanForRun(tag,
1166 markupProcessData.colorRuns,
1167 markupProcessData.fontRuns,
1168 markupProcessData.underlinedCharacterRuns,
1169 markupProcessData.backgroundColorRuns,
1170 markupProcessData.strikethroughCharacterRuns,
1173 underlinedCharacterRunIndex,
1175 strikethroughCharacterRunIndex,
1179 else if(TokenComparison(XHTML_STRIKETHROUGH_TAG, tag.buffer, tag.length))
1181 ProcessTagForRun<StrikethroughCharacterRun>(
1182 markupProcessData.strikethroughCharacterRuns, styleStack, tag, characterIndex, strikethroughCharacterRunIndex, sTagReference, [](const Tag& tag, StrikethroughCharacterRun& run) { ProcessStrikethroughTag(tag, run); });
1184 else if(TokenComparison(XHTML_PARAGRAPH_TAG, tag.buffer, tag.length))
1186 ProcessParagraphTag(markupProcessData, tag, (markupStringBuffer == markupStringEndBuffer), characterIndex);
1187 ProcessTagForRun<BoundedParagraphRun>(
1188 markupProcessData.boundedParagraphRuns, styleStack, tag, characterIndex, boundedParagraphRunIndex, pTagReference, [](const Tag& tag, BoundedParagraphRun& run) { ProcessAttributesOfParagraphTag(tag, run); });
1190 else if(TokenComparison(XHTML_CHARACTER_SPACING_TAG, tag.buffer, tag.length))
1192 ProcessTagForRun<CharacterSpacingCharacterRun>(
1193 markupProcessData.characterSpacingCharacterRuns, styleStack, tag, characterIndex, characterSpacingCharacterRunIndex, characterSpacingTagReference, [](const Tag& tag, CharacterSpacingCharacterRun& run) { ProcessCharacterSpacingTag(tag, run); });
1194 } // <char-spacing></char-spacing>
1195 } // end if( IsTag() )
1196 else if(markupStringBuffer < markupStringEndBuffer)
1198 ProcessMarkupStringBuffer(markupProcessData, markupStringBuffer, markupStringEndBuffer, characterIndex);
1202 // Resize the model's vectors.
1203 ResizeModelVectors(markupProcessData, fontRunIndex, colorRunIndex, underlinedCharacterRunIndex, strikethroughCharacterRunIndex, backgroundRunIndex, boundedParagraphRunIndex, characterSpacingCharacterRunIndex);
1205 // Handle the nested tags
1206 OverrideNestedUnderlinedCharacterRuns(markupProcessData.underlinedCharacterRuns);
1207 OverrideNestedStrikethroughCharacterRuns(markupProcessData.strikethroughCharacterRuns);
1212 } // namespace Toolkit