[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / spannable / span-ranges-container-impl.cpp
1
2 /*
3  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 // CLASS HEADER
20 #include <dali-toolkit/internal/text/spannable/span-ranges-container-impl.h>
21
22 // EXTERNAL INCLUDES
23 #include <dali/devel-api/common/map-wrapper.h>
24 #include <utility>
25
26 namespace Dali
27 {
28 namespace Toolkit
29 {
30 namespace Text
31 {
32 namespace Internal
33 {
34 struct SpanRangesContainer::Impl
35 {
36   using SpanId = uint32_t;
37
38   // Key : BaseSpan, Value : <Range, Added order>
39   using SpanRangeContainer = std::map<Dali::Toolkit::Text::BaseSpan, std::pair<Dali::Toolkit::Text::Range, SpanId>>;
40
41   SpanRangeContainer mSpanWithRanges{}; ///< The list of style-span
42   SpanId             mSpanId{0};        ///< The global id for each added Span. It will be used when we determine order of span.
43 };
44
45 SpanRangesContainer::SpanRangesContainer()
46 {
47   mImpl = std::make_unique<Impl>();
48 }
49
50 SpanRangesContainer::~SpanRangesContainer()
51 {
52 }
53
54 void SpanRangesContainer::AddSpan(const Dali::Toolkit::Text::BaseSpan& span, const Dali::Toolkit::Text::Range& range)
55 {
56   mImpl->mSpanWithRanges.insert(std::make_pair(span, std::make_pair(range, mImpl->mSpanId++)));
57 }
58
59 void SpanRangesContainer::RemoveSpan(const Dali::Toolkit::Text::BaseSpan& span)
60 {
61   mImpl->mSpanWithRanges.erase(span);
62 }
63
64 bool SpanRangesContainer::Contains(const Dali::Toolkit::Text::BaseSpan& span) const
65 {
66   const auto it = mImpl->mSpanWithRanges.find(span);
67
68   return it != mImpl->mSpanWithRanges.end();
69 }
70
71 void SpanRangesContainer::GetSpans(std::vector<Dali::Toolkit::Text::BaseSpan>& listOfSpans) const
72 {
73   std::map<Impl::SpanId, Dali::Toolkit::Text::BaseSpan> reorderedListOfSpans;
74
75   listOfSpans.reserve(listOfSpans.size() + mImpl->mSpanWithRanges.size());
76
77   // At first, sort the list of spans ordered by added time.
78   for(auto iter = mImpl->mSpanWithRanges.begin(), iterEnd = mImpl->mSpanWithRanges.end(); iter != iterEnd; ++iter)
79   {
80     reorderedListOfSpans.insert(std::make_pair(iter->second.second, iter->first));
81   }
82
83   // Retrieve result.
84   for(auto iter = reorderedListOfSpans.begin(), iterEnd = reorderedListOfSpans.end(); iter != iterEnd; ++iter)
85   {
86     listOfSpans.push_back(iter->second);
87   }
88 }
89
90 void SpanRangesContainer::GetSpansAndRanges(std::vector<Dali::Toolkit::Text::BaseSpan>& spans, std::vector<Dali::Toolkit::Text::Range>& ranges) const
91 {
92   std::map<Impl::SpanId, std::pair<Dali::Toolkit::Text::BaseSpan, Dali::Toolkit::Text::Range>> reorderedListOfSpansWithRanges;
93
94   spans.reserve(spans.size() + mImpl->mSpanWithRanges.size());
95   ranges.reserve(ranges.size() + mImpl->mSpanWithRanges.size());
96
97   // At first, sort the list of spans ordered by added time.
98   for(auto iter = mImpl->mSpanWithRanges.begin(), iterEnd = mImpl->mSpanWithRanges.end(); iter != iterEnd; ++iter)
99   {
100     reorderedListOfSpansWithRanges.insert(std::make_pair(iter->second.second, std::make_pair(iter->first, iter->second.first)));
101   }
102
103   // Retrieve result.
104   for(auto iter = reorderedListOfSpansWithRanges.begin(), iterEnd = reorderedListOfSpansWithRanges.end(); iter != iterEnd; ++iter)
105   {
106     spans.push_back(iter->second.first);
107     ranges.push_back(iter->second.second);
108   }
109 }
110 } // namespace Internal
111
112 } // namespace Text
113
114 } // namespace Toolkit
115
116 } // namespace Dali