fix incorrect calculaion of natural size in text
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / hidden-text.cpp
1 /*
2  * Copyright (c) 2021 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/hidden-text.h>
20
21 // INTERNAL INCLUDES
22 #include <dali-toolkit/public-api/controls/text-controls/text-editor.h>
23
24 using namespace Dali::Toolkit;
25
26 const int      DEFAULT_SHOW_DURATION = 1000;
27
28 namespace Dali
29 {
30 namespace Toolkit
31 {
32 namespace Text
33 {
34 const char* const PROPERTY_MODE                 = "mode";
35 const char* const PROPERTY_SUBSTITUTE_CHARACTER = "substituteCharacter";
36 const char* const PROPERTY_SUBSTITUTE_COUNT     = "substituteCount";
37 const char* const PROPERTY_SHOW_DURATION        = "showDuration";
38
39 HiddenText::HiddenText(Observer* observer)
40 : mObserver(observer),
41   mHideMode(static_cast<int>(Toolkit::HiddenInput::Mode::HIDE_NONE)),
42   mSubstituteText(STAR),
43   mDisplayDuration(DEFAULT_SHOW_DURATION),
44   mSubstituteCount(0),
45   mPreviousTextCount(0)
46 {
47   mTimer = Timer::New(mDisplayDuration);
48   mTimer.TickSignal().Connect(this, &HiddenText::OnTick);
49 }
50
51 void HiddenText::SetProperties(const Property::Map& map)
52 {
53   const Property::Map::SizeType count = map.Count();
54
55   for(Property::Map::SizeType position = 0; position < count; ++position)
56   {
57     KeyValuePair     keyValue = map.GetKeyValue(position);
58     Property::Key&   key      = keyValue.first;
59     Property::Value& value    = keyValue.second;
60
61     if(key == Toolkit::HiddenInput::Property::MODE || key == PROPERTY_MODE)
62     {
63       value.Get(mHideMode);
64     }
65     else if(key == Toolkit::HiddenInput::Property::SUBSTITUTE_CHARACTER || key == PROPERTY_SUBSTITUTE_CHARACTER)
66     {
67       value.Get(mSubstituteText);
68     }
69     else if(key == Toolkit::HiddenInput::Property::SUBSTITUTE_COUNT || key == PROPERTY_SUBSTITUTE_COUNT)
70     {
71       value.Get(mSubstituteCount);
72     }
73     else if(key == Toolkit::HiddenInput::Property::SHOW_LAST_CHARACTER_DURATION || key == PROPERTY_SHOW_DURATION)
74     {
75       value.Get(mDisplayDuration);
76     }
77   }
78 }
79
80 void HiddenText::GetProperties(Property::Map& map)
81 {
82   map[Toolkit::HiddenInput::Property::MODE]                         = mHideMode;
83   map[Toolkit::HiddenInput::Property::SUBSTITUTE_CHARACTER]         = mSubstituteText;
84   map[Toolkit::HiddenInput::Property::SUBSTITUTE_COUNT]             = mSubstituteCount;
85   map[Toolkit::HiddenInput::Property::SHOW_LAST_CHARACTER_DURATION] = mDisplayDuration;
86 }
87
88 void HiddenText::Substitute(const Vector<Character>& source, Vector<Character>& destination)
89 {
90   const Length characterCount = source.Count();
91
92   destination.Resize(characterCount);
93
94   uint32_t* begin     = destination.Begin();
95   uint32_t* end       = begin + characterCount;
96   uint32_t* hideStart = NULL;
97   uint32_t* hideEnd   = NULL;
98   uint32_t* sourcePos = source.Begin();
99
100   switch(mHideMode)
101   {
102     case Toolkit::HiddenInput::Mode::HIDE_NONE:
103     {
104       hideStart = NULL;
105       hideEnd   = NULL;
106       break;
107     }
108     case Toolkit::HiddenInput::Mode::HIDE_ALL:
109     {
110       hideStart = begin;
111       hideEnd   = end;
112       break;
113     }
114     case Toolkit::HiddenInput::Mode::HIDE_COUNT:
115     {
116       hideStart = begin;
117       hideEnd   = begin + mSubstituteCount;
118       break;
119     }
120     case Toolkit::HiddenInput::Mode::SHOW_COUNT:
121     {
122       hideStart = begin + mSubstituteCount;
123       hideEnd   = end;
124       break;
125     }
126     case Toolkit::HiddenInput::Mode::SHOW_LAST_CHARACTER:
127     {
128       if(mPreviousTextCount < characterCount)
129       {
130         hideStart = begin;
131         hideEnd   = end - 1;
132         if(mDisplayDuration > 0)
133         {
134           mTimer.SetInterval(mDisplayDuration);
135           mTimer.Start();
136         }
137         else
138         {
139           OnTick();
140         }
141       }
142       else
143       {
144         hideStart = begin;
145         hideEnd   = end;
146       }
147       break;
148     }
149   }
150   for(; begin < end; ++begin)
151   {
152     if(begin >= hideStart && begin < hideEnd)
153     {
154       *begin = static_cast<uint32_t>(mSubstituteText);
155       sourcePos++;
156     }
157     else
158     {
159       *begin = *sourcePos++;
160     }
161   }
162   mPreviousTextCount = characterCount;
163 }
164
165 bool HiddenText::OnTick()
166 {
167   if(mObserver != NULL)
168   {
169     mObserver->DisplayTimeExpired();
170   }
171
172   return false;
173 }
174
175 } // namespace Text
176
177 } // namespace Toolkit
178
179 } // namespace Dali