3 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <dali-toolkit/internal/text/spannable/span-ranges-container-impl.h>
23 #include <dali/devel-api/common/map-wrapper.h>
34 struct SpanRangesContainer::Impl
36 using SpanId = uint32_t;
38 // Key : BaseSpan, Value : <Range, Added order>
39 using SpanRangeContainer = std::map<Dali::Toolkit::Text::BaseSpan, std::pair<Dali::Toolkit::Text::Range, SpanId>>;
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.
45 SpanRangesContainer::SpanRangesContainer()
47 mImpl = std::make_unique<Impl>();
50 SpanRangesContainer::~SpanRangesContainer()
54 void SpanRangesContainer::AddSpan(const Dali::Toolkit::Text::BaseSpan& span, const Dali::Toolkit::Text::Range& range)
56 mImpl->mSpanWithRanges.insert(std::make_pair(span, std::make_pair(range, mImpl->mSpanId++)));
59 void SpanRangesContainer::RemoveSpan(const Dali::Toolkit::Text::BaseSpan& span)
61 mImpl->mSpanWithRanges.erase(span);
64 bool SpanRangesContainer::Contains(const Dali::Toolkit::Text::BaseSpan& span) const
66 const auto it = mImpl->mSpanWithRanges.find(span);
68 return it != mImpl->mSpanWithRanges.end();
71 void SpanRangesContainer::GetSpans(std::vector<Dali::Toolkit::Text::BaseSpan>& listOfSpans) const
73 std::map<Impl::SpanId, Dali::Toolkit::Text::BaseSpan> reorderedListOfSpans;
75 listOfSpans.reserve(listOfSpans.size() + mImpl->mSpanWithRanges.size());
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)
80 reorderedListOfSpans.insert(std::make_pair(iter->second.second, iter->first));
84 for(auto iter = reorderedListOfSpans.begin(), iterEnd = reorderedListOfSpans.end(); iter != iterEnd; ++iter)
86 listOfSpans.push_back(iter->second);
90 void SpanRangesContainer::GetSpansAndRanges(std::vector<Dali::Toolkit::Text::BaseSpan>& spans, std::vector<Dali::Toolkit::Text::Range>& ranges) const
92 std::map<Impl::SpanId, std::pair<Dali::Toolkit::Text::BaseSpan, Dali::Toolkit::Text::Range>> reorderedListOfSpansWithRanges;
94 spans.reserve(spans.size() + mImpl->mSpanWithRanges.size());
95 ranges.reserve(ranges.size() + mImpl->mSpanWithRanges.size());
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)
100 reorderedListOfSpansWithRanges.insert(std::make_pair(iter->second.second, std::make_pair(iter->first, iter->second.first)));
104 for(auto iter = reorderedListOfSpansWithRanges.begin(), iterEnd = reorderedListOfSpansWithRanges.end(); iter != iterEnd; ++iter)
106 spans.push_back(iter->second.first);
107 ranges.push_back(iter->second.second);
110 } // namespace Internal
114 } // namespace Toolkit