From 415d0772e9043f67f6201af352b3bc3d3535c3ab Mon Sep 17 00:00:00 2001 From: ssabah Date: Wed, 9 Feb 2022 17:19:37 +0200 Subject: [PATCH] Refactor some parts in Markup processor Simplify some functions and reduce duplicated code Adding some needed common functions Change-Id: I41104772649633e5bfe864d8b23730f1a4ff26a6 --- dali-toolkit/internal/file.list | 1 + ...markup-processor-attribute-helper-functions.cpp | 49 ++++++++++++ .../markup-processor-attribute-helper-functions.h | 88 ++++++++++++++++++++++ .../internal/text/markup-processor-font.cpp | 21 ++---- .../text/markup-processor-helper-functions.cpp | 2 + .../text/markup-processor-helper-functions.h | 9 +++ 6 files changed, 154 insertions(+), 16 deletions(-) create mode 100644 dali-toolkit/internal/text/markup-processor-attribute-helper-functions.cpp create mode 100644 dali-toolkit/internal/text/markup-processor-attribute-helper-functions.h diff --git a/dali-toolkit/internal/file.list b/dali-toolkit/internal/file.list index e57fa31..8c84b35 100644 --- a/dali-toolkit/internal/file.list +++ b/dali-toolkit/internal/file.list @@ -150,6 +150,7 @@ SET( toolkit_src_files ${toolkit_src_dir}/text/markup-processor-span.cpp ${toolkit_src_dir}/text/markup-processor-strikethrough.cpp ${toolkit_src_dir}/text/markup-processor-helper-functions.cpp + ${toolkit_src_dir}/text/markup-processor-attribute-helper-functions.cpp ${toolkit_src_dir}/text/multi-language-support.cpp ${toolkit_src_dir}/text/hidden-text.cpp ${toolkit_src_dir}/text/input-filter.cpp diff --git a/dali-toolkit/internal/text/markup-processor-attribute-helper-functions.cpp b/dali-toolkit/internal/text/markup-processor-attribute-helper-functions.cpp new file mode 100644 index 0000000..da00996 --- /dev/null +++ b/dali-toolkit/internal/text/markup-processor-attribute-helper-functions.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// FILE HEADER +#include + +// INTERNAL INCLUDES +#include + +// EXTERNAL INCLUDES +#include + +namespace Dali +{ +namespace Toolkit +{ +namespace Text +{ +void CopyAttributeValueFromBuffer(const Attribute& attribute, const Length maxLengthAttributeValue, char* value) +{ + const Length length = attribute.valueLength > maxLengthAttributeValue ? maxLengthAttributeValue : attribute.valueLength; + memcpy(value, attribute.valueBuffer, length); + value[length] = 0; +} + +float ProcessFloatAttribute(const Attribute& attribute) +{ + return StringToFloat(attribute.valueBuffer); +} + +} // namespace Text + +} // namespace Toolkit + +} // namespace Dali diff --git a/dali-toolkit/internal/text/markup-processor-attribute-helper-functions.h b/dali-toolkit/internal/text/markup-processor-attribute-helper-functions.h new file mode 100644 index 0000000..52771b5 --- /dev/null +++ b/dali-toolkit/internal/text/markup-processor-attribute-helper-functions.h @@ -0,0 +1,88 @@ +#ifndef DALI_TOOLKIT_TEXT_MARKUP_PROCESSOR_ATTRIBUTE_HELPER_FUNCTIONS_H +#define DALI_TOOLKIT_TEXT_MARKUP_PROCESSOR_ATTRIBUTE_HELPER_FUNCTIONS_H + +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// INTERNAL INCLUDES +#include + +// EXTERNAL INCLUDES +#include + +namespace Dali +{ +namespace Toolkit +{ +namespace Text +{ +struct Attribute; + +/** + * @brief Copy the value from attribute buffer to value. + * + * @param[in] attribute the value of attribute. + * @param[in] maxLengthAttributeValue the maximum length of any of the possible value for attribute + * @param[out] value the value container. + * + */ +void CopyAttributeValueFromBuffer(const Attribute& attribute, const Length maxLengthAttributeValue, char* value); + +/** + * @brief Process the float attribute value from buffer. + * + * @param[in] attribute the float attribute. + * + * @return The float value. + */ +float ProcessFloatAttribute(const Attribute& attribute); + +/** + * @brief Process the Enumeration attribute value from buffer. + * + * @param[in] attribute the Enumeration attribute. + * @param[in] maxLengthAttributeValue the maximum length of any of the possible value for attribute + * @param[in] funcStringToEnum the function converts string value to enum value + * @param[out] enumValue the enum value + * + * @return True if the enum value was processed successfully + * + */ +template +bool ProcessEnumerationAttribute(const Attribute& attribute, + const Length maxLengthAttributeValue, + std::function funcStringToEnum, + T& enumValue) +{ + char* value = new char[maxLengthAttributeValue + 1u]; + + CopyAttributeValueFromBuffer(attribute, maxLengthAttributeValue, value); + + enumValue = funcStringToEnum(value); // @TODO: the functions that process Enum value should be refactored to return bool from Scripting::GetEnumeration + + delete[] value; + + return true; +} + +} // namespace Text + +} // namespace Toolkit + +} // namespace Dali + +#endif // DALI_TOOLKIT_TEXT_MARKUP_PROCESSOR_ATTRIBUTE_HELPER_FUNCTIONS_H diff --git a/dali-toolkit/internal/text/markup-processor-font.cpp b/dali-toolkit/internal/text/markup-processor-font.cpp index df8b6ec..0c469b8 100644 --- a/dali-toolkit/internal/text/markup-processor-font.cpp +++ b/dali-toolkit/internal/text/markup-processor-font.cpp @@ -24,6 +24,7 @@ // INTERNAL INCLUDES #include +#include #include #include @@ -68,35 +69,23 @@ void ProcessFontFamily(const Attribute& attribute, FontDescriptionRun& fontRun) void ProcessFontSize(const Attribute& attribute, FontDescriptionRun& fontRun) { // 64.f is used to convert from point size to 26.6 pixel format. - fontRun.size = static_cast(StringToFloat(attribute.valueBuffer) * PIXEL_FORMAT_64_FACTOR); + fontRun.size = static_cast(ProcessFloatAttribute(attribute) * PIXEL_FORMAT_64_FACTOR); fontRun.sizeDefined = true; } void ProcessFontWeight(const Attribute& attribute, FontDescriptionRun& fontRun) { - char value[MAX_FONT_ATTRIBUTE_SIZE + 1u]; - processFontAttributeValue(value, attribute); - - fontRun.weight = StringToWeight(value); - fontRun.weightDefined = true; + fontRun.weightDefined = ProcessEnumerationAttribute(attribute, MAX_FONT_ATTRIBUTE_SIZE, &StringToWeight, fontRun.weight); } void ProcessFontWidth(const Attribute& attribute, FontDescriptionRun& fontRun) { - char value[MAX_FONT_ATTRIBUTE_SIZE + 1u]; - processFontAttributeValue(value, attribute); - - fontRun.width = StringToWidth(value); - fontRun.widthDefined = true; + fontRun.widthDefined = ProcessEnumerationAttribute(attribute, MAX_FONT_ATTRIBUTE_SIZE, &StringToWidth, fontRun.width); } void ProcessFontSlant(const Attribute& attribute, FontDescriptionRun& fontRun) { - char value[MAX_FONT_ATTRIBUTE_SIZE + 1u]; - processFontAttributeValue(value, attribute); - - fontRun.slant = StringToSlant(value); - fontRun.slantDefined = true; + fontRun.slantDefined = ProcessEnumerationAttribute(attribute, MAX_FONT_ATTRIBUTE_SIZE, &StringToSlant, fontRun.slant); } void ProcessFontTag(const Tag& tag, FontDescriptionRun& fontRun) diff --git a/dali-toolkit/internal/text/markup-processor-helper-functions.cpp b/dali-toolkit/internal/text/markup-processor-helper-functions.cpp index ce8bcee..e4cb1c4 100644 --- a/dali-toolkit/internal/text/markup-processor-helper-functions.cpp +++ b/dali-toolkit/internal/text/markup-processor-helper-functions.cpp @@ -38,6 +38,8 @@ const char FIRST_UPPER_CASE = 0x41; // ASCII value of the one after the first up const char LAST_UPPER_CASE = 0x5b; // ASCII value of the one after the last upper case character (Z). const char TO_LOWER_CASE = 32; // Value to add to a upper case character to transform it into a lower case. +const unsigned int MAX_FLOAT_ATTRIBUTE_SIZE = 17u; ///< The maximum length of any of the possible float values. +99999.999999999f (sign, five digits, dot, nine digits, f) + const char WEB_COLOR_TOKEN('#'); const char* const HEX_COLOR_TOKEN("0x"); const char* const ALPHA_ONE("FF"); diff --git a/dali-toolkit/internal/text/markup-processor-helper-functions.h b/dali-toolkit/internal/text/markup-processor-helper-functions.h index d7a6596..a28c1cb 100644 --- a/dali-toolkit/internal/text/markup-processor-helper-functions.h +++ b/dali-toolkit/internal/text/markup-processor-helper-functions.h @@ -193,6 +193,15 @@ void Vector2ToString(const Vector2& value, std::string& vector2Str); */ void UnderlineTypeStringToTypeValue(const char* const typeStr, Length length, Text::Underline::Type& retType); +/** + * @brief Converts a string into a float value. + * + * @param[in] floatStr A float packed inside a string. + * + * @return The float value. + */ +float StringToFloat(const char* const floatStr); + } // namespace Text } // namespace Toolkit -- 2.7.4