Spannable: Add BackgroundSpan
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / spannable / spans / background-color-span-impl.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/text/spannable/spans/background-color-span-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali-toolkit/internal/text/color-run.h>
23 #include <dali-toolkit/internal/text/markup-tags-and-attributes.h>
24
25 namespace Dali
26 {
27 namespace Toolkit
28 {
29 namespace Text
30 {
31 namespace Internal
32 {
33 struct BackgroundColorSpan::Impl
34 {
35   Vector4 mBackgroundColor;            ///< The color of the characters.
36   bool    mBackgroundColorDefined : 1; ///< Whether the background color is defined.
37 };
38
39 BackgroundColorSpan::BackgroundColorSpan()
40 : BaseSpan()
41 {
42   mImpl = std::make_unique<Impl>();
43 }
44
45 BackgroundColorSpan ::~BackgroundColorSpan()
46 {
47 }
48
49 Dali::Toolkit::Text::BackgroundColorSpan BackgroundColorSpan::New(const Vector4 &color)
50 {
51   BackgroundColorSpanPtr object = new BackgroundColorSpan();
52   object->SetBackgroundColor(color);
53   Dali::Toolkit::Text::BackgroundColorSpan handle = Dali::Toolkit::Text::BackgroundColorSpan(object.Get());
54
55   return handle;
56 }
57
58 //Methods
59 const Vector4 BackgroundColorSpan::GetBackgroundColor() const
60 {
61   return mImpl->mBackgroundColor;
62 }
63
64 bool BackgroundColorSpan::IsBackgroundColorDefined() const
65 {
66   return mImpl->mBackgroundColorDefined;
67 }
68
69 void BackgroundColorSpan::SetBackgroundColor(const Vector4 &color)
70 {
71   mImpl->mBackgroundColor        = color;
72   mImpl->mBackgroundColorDefined = true;
73 }
74
75 void BackgroundColorSpan::CreateStyleCharacterRun(IntrusivePtr<LogicalModel>& logicalModel, const Dali::Toolkit::Text::Range& range) const
76 {
77   ColorRun backgroundColorRun;
78   backgroundColorRun.characterRun.characterIndex     = range.GetStartIndex();
79   backgroundColorRun.characterRun.numberOfCharacters = range.GetNumberOfIndices();
80   backgroundColorRun.color                           = mImpl->mBackgroundColor;
81   logicalModel->mBackgroundColorRuns.PushBack(backgroundColorRun);
82 }
83
84 } // namespace Internal
85
86 } // namespace Text
87
88 } // namespace Toolkit
89
90 } // namespace Dali