Refactor some parts in Markup processor 87/271187/3
authorssabah <s.sabah@samsung.com>
Wed, 9 Feb 2022 15:19:37 +0000 (17:19 +0200)
committershrouq Sabah <s.sabah@samsung.com>
Mon, 21 Feb 2022 16:05:48 +0000 (16:05 +0000)
Simplify some functions and reduce duplicated code
Adding some needed common functions

Change-Id: I41104772649633e5bfe864d8b23730f1a4ff26a6

dali-toolkit/internal/file.list
dali-toolkit/internal/text/markup-processor-attribute-helper-functions.cpp [new file with mode: 0644]
dali-toolkit/internal/text/markup-processor-attribute-helper-functions.h [new file with mode: 0644]
dali-toolkit/internal/text/markup-processor-font.cpp
dali-toolkit/internal/text/markup-processor-helper-functions.cpp
dali-toolkit/internal/text/markup-processor-helper-functions.h

index e57fa31..8c84b35 100644 (file)
@@ -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 (file)
index 0000000..da00996
--- /dev/null
@@ -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 <dali-toolkit/internal/text/markup-processor-attribute-helper-functions.h>
+
+// INTERNAL INCLUDES
+#include <dali-toolkit/internal/text/markup-processor-helper-functions.h>
+
+// EXTERNAL INCLUDES
+#include <memory.h>
+
+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 (file)
index 0000000..52771b5
--- /dev/null
@@ -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 <dali-toolkit/internal/text/text-definitions.h>
+
+// EXTERNAL INCLUDES
+#include <functional>
+
+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<typename T>
+bool ProcessEnumerationAttribute(const Attribute&                    attribute,
+                                 const Length                        maxLengthAttributeValue,
+                                 std::function<T(const char* const)> 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
index df8b6ec..0c469b8 100644 (file)
@@ -24,6 +24,7 @@
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/internal/text/font-description-run.h>
+#include <dali-toolkit/internal/text/markup-processor-attribute-helper-functions.h>
 #include <dali-toolkit/internal/text/markup-processor-helper-functions.h>
 #include <dali-toolkit/internal/text/text-font-style.h>
 
@@ -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<PointSize26Dot6>(StringToFloat(attribute.valueBuffer) * PIXEL_FORMAT_64_FACTOR);
+  fontRun.size        = static_cast<PointSize26Dot6>(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<FontWeight>(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<FontWidth>(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<FontSlant>(attribute, MAX_FONT_ATTRIBUTE_SIZE, &StringToSlant, fontRun.slant);
 }
 
 void ProcessFontTag(const Tag& tag, FontDescriptionRun& fontRun)
index ce8bcee..e4cb1c4 100644 (file)
@@ -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");
index d7a6596..a28c1cb 100644 (file)
@@ -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