[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / spannable / spans / font-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/font-span-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali-toolkit/devel-api/text/spans/font-span.h>
23 #include <memory.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/text/color-run.h>
27 #include <dali-toolkit/internal/text/font-description-run.h>
28 #include <dali-toolkit/internal/text/markup-tags-and-attributes.h>
29
30 namespace Dali
31 {
32 namespace Toolkit
33 {
34 namespace Text
35 {
36 namespace Internal
37 {
38 namespace
39 {
40 const float PIXEL_FORMAT_64_FACTOR = 64.f; ///< 64.f is used to convert from point size to 26.6 pixel format.
41 }
42
43 struct FontSpan::Impl
44 {
45   std::string mFamilyName; ///< The font's family name.
46   Dali::TextAbstraction::FontWeight::Type mWeight;     ///< The font's weight.
47   Dali::TextAbstraction::FontWidth::Type  mWidth;      ///< The font's width.
48   Dali::TextAbstraction::FontSlant::Type  mSlant;      ///< The font's slant.
49   float       mSize;       ///< The font's size.
50
51   bool mFamilyNameDefined : 1; ///< Whether the font's family is defined.
52   bool mWeightDefined : 1;     ///< Whether the font's weight is defined.
53   bool mWidthDefined : 1;      ///< Whether the font's width is defined.
54   bool mSlantDefined : 1;      ///< Whether the font's slant is defined.
55   bool mSizeDefined : 1;       ///< Whether the font's size is defined.
56 };
57
58 FontSpan::FontSpan()
59 : BaseSpan(Dali::Toolkit::Text::SpanType::Value::FONT)
60 {
61   mImpl = std::make_unique<Impl>();
62 }
63
64 FontSpan ::~FontSpan()
65 {
66 }
67
68 Dali::Toolkit::Text::FontSpan FontSpan::New(const std::string&                             familyName,
69                                             const float&                                   sizeInPoints,
70                                             const Dali::TextAbstraction::FontWeight::Type& weight,
71                                             const Dali::TextAbstraction::FontWidth::Type&  width,
72                                             const Dali::TextAbstraction::FontSlant::Type&  slant)
73 {
74   FontSpanPtr object = new FontSpan();
75   object->SetFamilyName(familyName);
76   object->SetSize(sizeInPoints);
77   object->SetWeight(weight);
78   object->SetWidth(width);
79   object->SetSlant(slant);
80
81   Dali::Toolkit::Text::FontSpan handle = Dali::Toolkit::Text::FontSpan(object.Get());
82
83   return handle;
84 }
85
86 //Methods
87 const std::string FontSpan::GetFamilyName() const
88 {
89   return mImpl->mFamilyName;
90 }
91
92 bool FontSpan::IsFamilyNameDefined() const
93 {
94   return mImpl->mFamilyNameDefined;
95 }
96
97 void FontSpan::SetFamilyName(const std::string& familyName)
98 {
99   mImpl->mFamilyName        = familyName;
100   mImpl->mFamilyNameDefined = true;
101 }
102
103 Dali::TextAbstraction::FontWeight::Type FontSpan::GetWeight() const
104 {
105   return mImpl->mWeight;
106 }
107
108 bool FontSpan::IsWeightDefined() const
109 {
110   return mImpl->mWeightDefined;
111 }
112
113 void FontSpan::SetWeight(const Dali::TextAbstraction::FontWeight::Type& weight)
114 {
115   mImpl->mWeight        = weight;
116   mImpl->mWeightDefined = true;
117 }
118
119 Dali::TextAbstraction::FontWidth::Type FontSpan::GetWidth() const
120 {
121   return mImpl->mWidth;
122 }
123
124 bool FontSpan::IsWidthDefined() const
125 {
126   return mImpl->mWidthDefined;
127 }
128
129 void FontSpan::SetWidth(const Dali::TextAbstraction::FontWidth::Type& width)
130 {
131   mImpl->mWidth        = width;
132   mImpl->mWidthDefined = true;
133 }
134
135 Dali::TextAbstraction::FontSlant::Type FontSpan::GetSlant() const
136 {
137   return mImpl->mSlant;
138 }
139
140 bool FontSpan::IsSlantDefined() const
141 {
142   return mImpl->mSlantDefined;
143 }
144
145 void FontSpan::SetSlant(const Dali::TextAbstraction::FontSlant::Type& slant)
146 {
147   mImpl->mSlant        = slant;
148   mImpl->mSlantDefined = true;
149 }
150
151 float FontSpan::GetSize() const
152 {
153   return mImpl->mSize;
154 }
155
156 bool FontSpan::IsSizeDefined() const
157 {
158   return mImpl->mSizeDefined;
159 }
160
161 void FontSpan::SetSize(const float& size)
162 {
163   mImpl->mSize        = size;
164   mImpl->mSizeDefined = true;
165 }
166
167 void FontSpan::CreateStyleCharacterRun(IntrusivePtr<LogicalModel>& logicalModel, const Dali::Toolkit::Text::Range& range) const
168 {
169   FontDescriptionRun fontRun;
170   fontRun.characterRun.characterIndex     = range.GetStartIndex();
171   fontRun.characterRun.numberOfCharacters = range.GetNumberOfIndices();
172
173   InitializeFontRun(fontRun);
174   ProcessFontFamily(fontRun);
175   ProcessFontSize(fontRun);
176   ProcessFontWeight(fontRun);
177   ProcessFontWidth(fontRun);
178   ProcessFontSlant(fontRun);
179
180   logicalModel->mFontDescriptionRuns.PushBack(fontRun);
181 }
182
183 void FontSpan::InitializeFontRun(FontDescriptionRun& fontRun) const
184 {
185   fontRun.familyName   = NULL;
186   fontRun.familyLength = 0u;
187   fontRun.weight       = TextAbstraction::FontWeight::NORMAL;
188   fontRun.width        = TextAbstraction::FontWidth::NORMAL;
189   fontRun.slant        = TextAbstraction::FontSlant::NORMAL;
190   fontRun.size         = 0u;
191
192   fontRun.familyDefined = false;
193   fontRun.weightDefined = false;
194   fontRun.widthDefined  = false;
195   fontRun.slantDefined  = false;
196   fontRun.sizeDefined   = false;
197 }
198
199 void FontSpan::ProcessFontFamily(FontDescriptionRun& fontRun) const
200 {
201   if(IsFamilyNameDefined())
202   {
203     const std::string& familyName = GetFamilyName();
204     fontRun.familyDefined         = true;
205     fontRun.familyLength          = familyName.length();
206     fontRun.familyName            = new char[fontRun.familyLength];
207     memcpy(fontRun.familyName, familyName.c_str(), fontRun.familyLength);
208     // The memory is freed when the font run is removed from the logical model.
209   }
210 }
211
212 void FontSpan::ProcessFontSize(FontDescriptionRun& fontRun) const
213 {
214   if(IsSizeDefined())
215   {
216     // 64.f is used to convert from point size to 26.6 pixel format.
217     fontRun.size        = static_cast<PointSize26Dot6>(GetSize() * PIXEL_FORMAT_64_FACTOR);
218     fontRun.sizeDefined = true;
219   }
220 }
221
222 void FontSpan::ProcessFontWeight(FontDescriptionRun& fontRun) const
223 {
224   if(IsWeightDefined())
225   {
226     fontRun.weight        = GetWeight();
227     fontRun.weightDefined = true;
228   }
229 }
230
231 void FontSpan::ProcessFontWidth(FontDescriptionRun& fontRun) const
232 {
233   if(IsWidthDefined())
234   {
235     fontRun.width        = GetWidth();
236     fontRun.widthDefined = true;
237   }
238 }
239
240 void FontSpan::ProcessFontSlant(FontDescriptionRun& fontRun) const
241 {
242   if(IsSlantDefined())
243   {
244     fontRun.slant        = GetSlant();
245     fontRun.slantDefined = true;
246   }
247 }
248
249 } // namespace Internal
250
251 } // namespace Text
252
253 } // namespace Toolkit
254
255 } // namespace Dali