X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Ftext%2Fmarkup-processor.cpp;h=b656aa46f6349b5a321c52a0ff5e32ceb381bf53;hp=ea7f869588a7ee8d73203f56308dd91bfb12764d;hb=cc7137614f4136f571ccecc22ad1b72c01f16156;hpb=38217811ad89debdb195c12f6c059f265fe6d5cd diff --git a/dali-toolkit/internal/text/markup-processor.cpp b/dali-toolkit/internal/text/markup-processor.cpp index ea7f869..b656aa4 100644 --- a/dali-toolkit/internal/text/markup-processor.cpp +++ b/dali-toolkit/internal/text/markup-processor.cpp @@ -26,10 +26,12 @@ // INTERNAL INCLUDES #include #include +#include #include #include #include #include +#include #include namespace Dali @@ -53,6 +55,8 @@ const std::string XHTML_GLOW_TAG("glow"); const std::string XHTML_OUTLINE_TAG("outline"); const std::string XHTML_ITEM_TAG("item"); const std::string XHTML_ANCHOR_TAG("a"); +const std::string XHTML_BACKGROUND_TAG("background"); +const std::string XHTML_SPAN_TAG("span"); const char LESS_THAN = '<'; const char GREATER_THAN = '>'; @@ -80,15 +84,16 @@ const unsigned int DEFAULT_VECTOR_SIZE = 16u; ///< Default size of run vectors Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_MARKUP_PROCESSOR"); #endif +typedef VectorBase::SizeType RunIndex; + /** * @brief Struct used to retrieve the style runs from the mark-up string. */ +template struct StyleStack { - typedef VectorBase::SizeType RunIndex; - - Vector stack; ///< Use a vector as a style stack. Stores the indices pointing where the run is stored inside the logical model. - unsigned int topIndex; ///< Points the top of the stack. + Vector stack; ///< Use a vector as a style stack. + unsigned int topIndex; ///< Points the top of the stack. StyleStack() : stack(), @@ -97,7 +102,7 @@ struct StyleStack stack.Resize(DEFAULT_VECTOR_SIZE); } - void Push(RunIndex index) + void Push(StyleStackType item) { // Check if there is space inside the style stack. const VectorBase::SizeType size = stack.Count(); @@ -107,14 +112,14 @@ struct StyleStack stack.Resize(2u * size); } - // Set the run index in the top of the stack. - *(stack.Begin() + topIndex) = index; + // Set the item in the top of the stack. + *(stack.Begin() + topIndex) = item; // Reposition the pointer to the top of the stack. ++topIndex; } - RunIndex Pop() + StyleStackType Pop() { // Pop the top of the stack. --topIndex; @@ -123,6 +128,17 @@ struct StyleStack }; /** + * @brief Struct used to retrieve spans from the mark-up string. + */ +struct Span +{ + RunIndex colorRunIndex; + RunIndex fontRunIndex; + bool isColorDefined; + bool isFontDefined; +}; + +/** * @brief Initializes a font run description to its defaults. * * @param[in,out] fontRun The font description run to initialize. @@ -167,6 +183,19 @@ void Initialize(UnderlinedCharacterRun& underlinedCharacterRun) } /** + * @brief Initializes a span to its defaults. + * + * @param[in,out] span The span to be initialized. + */ +void Initialize(Span& span) +{ + span.colorRunIndex = 0u; + span.isColorDefined = false; + span.fontRunIndex = 0u; + span.isFontDefined = false; +} + +/** * @brief Splits the tag string into the tag name and its attributes. * * The attributes are stored in a vector in the tag. @@ -524,10 +553,10 @@ bool XHTMLNumericEntityToUtf8(const char* markupText, char* utf8) template void ProcessTagForRun( Vector& runsContainer, - StyleStack& styleStack, + StyleStack& styleStack, const Tag& tag, const CharacterIndex characterIndex, - StyleStack::RunIndex& runIndex, + RunIndex& runIndex, int& tagReference, std::function parameterSettingFunction) { @@ -625,18 +654,108 @@ void ProcessAnchorTag( } /** + * @brief Processes span tag for the color-run & font-run. + * + * @param[in] spanTag The tag we are currently processing + * @param[in/out] spanStack The spans stack + * @param[int/out] colorRuns The container containing all the color runs + * @param[int/out] fontRuns The container containing all the font description runs + * @param[in/out] colorRunIndex The color run index + * @param[in/out] fontRunIndex The font run index + * @param[in] characterIndex The current character index + * @param[in] tagReference The tagReference we should increment/decrement + */ +void ProcessSpanForRun( + const Tag& spanTag, + StyleStack& spanStack, + Vector& colorRuns, + Vector& fontRuns, + RunIndex& colorRunIndex, + RunIndex& fontRunIndex, + const CharacterIndex characterIndex, + int& tagReference) +{ + if(!spanTag.isEndTag) + { + // Create a new run. + ColorRun colorRun; + Initialize(colorRun); + + FontDescriptionRun fontRun; + Initialize(fontRun); + + Span span; + Initialize(span); + + // Fill the run with the parameters. + colorRun.characterRun.characterIndex = characterIndex; + fontRun.characterRun.characterIndex = characterIndex; + + span.colorRunIndex = colorRunIndex; + span.fontRunIndex = fontRunIndex; + + ProcessSpanTag(spanTag, colorRun, fontRun, span.isColorDefined, span.isFontDefined); + + // Push the span into the stack. + spanStack.Push(span); + + // Point the next free run. + if(span.isColorDefined) + { + // Push the run in the logical model. + colorRuns.PushBack(colorRun); + ++colorRunIndex; + } + + if(span.isFontDefined) + { + // Push the run in the logical model. + fontRuns.PushBack(fontRun); + ++fontRunIndex; + } + + // Increase reference + ++tagReference; + } + else + { + if(tagReference > 0) + { + // Pop the top of the stack and set the number of characters of the run. + Span span = spanStack.Pop(); + + if(span.isColorDefined) + { + ColorRun& colorRun = *(colorRuns.Begin() + span.colorRunIndex); + colorRun.characterRun.numberOfCharacters = characterIndex - colorRun.characterRun.characterIndex; + } + + if(span.isFontDefined) + { + FontDescriptionRun& fontRun = *(fontRuns.Begin() + span.fontRunIndex); + fontRun.characterRun.numberOfCharacters = characterIndex - fontRun.characterRun.characterIndex; + } + + --tagReference; + } + } +} + +/** * @brief Resizes the model's vectors * * @param[in/out] markupProcessData The markup process data * @param[in] fontRunIndex The font run index * @param[in] colorRunIndex The color run index * @param[in] underlinedCharacterRunIndex The underlined character run index + * @param[in] backgroundRunIndex The background run index */ -void ResizeModelVectors(MarkupProcessData& markupProcessData, const StyleStack::RunIndex fontRunIndex, const StyleStack::RunIndex colorRunIndex, const StyleStack::RunIndex underlinedCharacterRunIndex) +void ResizeModelVectors(MarkupProcessData& markupProcessData, const RunIndex fontRunIndex, const RunIndex colorRunIndex, const RunIndex underlinedCharacterRunIndex, const RunIndex backgroundRunIndex) { markupProcessData.fontRuns.Resize(fontRunIndex); markupProcessData.colorRuns.Resize(colorRunIndex); markupProcessData.underlinedCharacterRuns.Resize(underlinedCharacterRunIndex); + markupProcessData.backgroundColorRuns.Resize(backgroundRunIndex); #ifdef DEBUG_ENABLED for(unsigned int i = 0; i < colorRunIndex; ++i) @@ -750,24 +869,31 @@ void ProcessMarkupString(const std::string& markupString, MarkupProcessData& mar markupProcessData.markupProcessedText.reserve(markupStringSize); // Stores a struct with the index to the first character of the run, the type of run and its parameters. - StyleStack styleStack; + StyleStack styleStack; + + // Stores a struct with the index to the first character of the color run & color font for the span. + StyleStack spanStack; // Points the next free position in the vector of runs. - StyleStack::RunIndex colorRunIndex = 0u; - StyleStack::RunIndex fontRunIndex = 0u; - StyleStack::RunIndex underlinedCharacterRunIndex = 0u; + RunIndex colorRunIndex = 0u; + RunIndex fontRunIndex = 0u; + RunIndex underlinedCharacterRunIndex = 0u; + RunIndex backgroundRunIndex = 0u; // check tag reference - int colorTagReference = 0u; - int fontTagReference = 0u; - int iTagReference = 0u; - int bTagReference = 0u; - int uTagReference = 0u; + int colorTagReference = 0u; + int fontTagReference = 0u; + int iTagReference = 0u; + int bTagReference = 0u; + int uTagReference = 0u; + int backgroundTagReference = 0u; + int spanTagReference = 0u; // Give an initial default value to the model's vectors. markupProcessData.colorRuns.Reserve(DEFAULT_VECTOR_SIZE); markupProcessData.fontRuns.Reserve(DEFAULT_VECTOR_SIZE); markupProcessData.underlinedCharacterRuns.Reserve(DEFAULT_VECTOR_SIZE); + markupProcessData.backgroundColorRuns.Reserve(DEFAULT_VECTOR_SIZE); // Get the mark-up string buffer. const char* markupStringBuffer = markupString.c_str(); @@ -798,8 +924,8 @@ void ProcessMarkupString(const std::string& markupString, MarkupProcessData& mar else if(TokenComparison(XHTML_U_TAG, tag.buffer, tag.length)) { ProcessTagForRun( - markupProcessData.underlinedCharacterRuns, styleStack, tag, characterIndex, underlinedCharacterRunIndex, uTagReference, [](const Tag& tag, UnderlinedCharacterRun& run) { }); - } // + markupProcessData.underlinedCharacterRuns, styleStack, tag, characterIndex, underlinedCharacterRunIndex, uTagReference, [](const Tag& tag, UnderlinedCharacterRun& run) {}); + } // else if(TokenComparison(XHTML_B_TAG, tag.buffer, tag.length)) { ProcessTagForRun( @@ -844,6 +970,15 @@ void ProcessMarkupString(const std::string& markupString, MarkupProcessData& mar { ProcessItemTag(markupProcessData, tag, characterIndex); } + else if(TokenComparison(XHTML_BACKGROUND_TAG, tag.buffer, tag.length)) + { + ProcessTagForRun( + markupProcessData.backgroundColorRuns, styleStack, tag, characterIndex, backgroundRunIndex, backgroundTagReference, [](const Tag& tag, ColorRun& run) { ProcessBackground(tag, run); }); + } + else if(TokenComparison(XHTML_SPAN_TAG, tag.buffer, tag.length)) + { + ProcessSpanForRun(tag, spanStack, markupProcessData.colorRuns, markupProcessData.fontRuns, colorRunIndex, fontRunIndex, characterIndex, spanTagReference); + } } // end if( IsTag() ) else if(markupStringBuffer < markupStringEndBuffer) { @@ -852,7 +987,7 @@ void ProcessMarkupString(const std::string& markupString, MarkupProcessData& mar } // Resize the model's vectors. - ResizeModelVectors(markupProcessData, fontRunIndex, colorRunIndex, underlinedCharacterRunIndex); + ResizeModelVectors(markupProcessData, fontRunIndex, colorRunIndex, underlinedCharacterRunIndex, backgroundRunIndex); } } // namespace Text