2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/text/spannable/spans/font-span-impl.h>
22 #include <dali-toolkit/devel-api/text/spans/font-span.h>
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>
40 const float PIXEL_FORMAT_64_FACTOR = 64.f; ///< 64.f is used to convert from point size to 26.6 pixel format.
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.
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.
59 : BaseSpan(Dali::Toolkit::Text::SpanType::Value::FONT)
61 mImpl = std::make_unique<Impl>();
64 FontSpan ::~FontSpan()
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)
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);
81 Dali::Toolkit::Text::FontSpan handle = Dali::Toolkit::Text::FontSpan(object.Get());
87 const std::string FontSpan::GetFamilyName() const
89 return mImpl->mFamilyName;
92 bool FontSpan::IsFamilyNameDefined() const
94 return mImpl->mFamilyNameDefined;
97 void FontSpan::SetFamilyName(const std::string& familyName)
99 mImpl->mFamilyName = familyName;
100 mImpl->mFamilyNameDefined = true;
103 Dali::TextAbstraction::FontWeight::Type FontSpan::GetWeight() const
105 return mImpl->mWeight;
108 bool FontSpan::IsWeightDefined() const
110 return mImpl->mWeightDefined;
113 void FontSpan::SetWeight(const Dali::TextAbstraction::FontWeight::Type& weight)
115 mImpl->mWeight = weight;
116 mImpl->mWeightDefined = true;
119 Dali::TextAbstraction::FontWidth::Type FontSpan::GetWidth() const
121 return mImpl->mWidth;
124 bool FontSpan::IsWidthDefined() const
126 return mImpl->mWidthDefined;
129 void FontSpan::SetWidth(const Dali::TextAbstraction::FontWidth::Type& width)
131 mImpl->mWidth = width;
132 mImpl->mWidthDefined = true;
135 Dali::TextAbstraction::FontSlant::Type FontSpan::GetSlant() const
137 return mImpl->mSlant;
140 bool FontSpan::IsSlantDefined() const
142 return mImpl->mSlantDefined;
145 void FontSpan::SetSlant(const Dali::TextAbstraction::FontSlant::Type& slant)
147 mImpl->mSlant = slant;
148 mImpl->mSlantDefined = true;
151 float FontSpan::GetSize() const
156 bool FontSpan::IsSizeDefined() const
158 return mImpl->mSizeDefined;
161 void FontSpan::SetSize(const float& size)
164 mImpl->mSizeDefined = true;
167 void FontSpan::CreateStyleCharacterRun(IntrusivePtr<LogicalModel>& logicalModel, const Dali::Toolkit::Text::Range& range) const
169 FontDescriptionRun fontRun;
170 fontRun.characterRun.characterIndex = range.GetStartIndex();
171 fontRun.characterRun.numberOfCharacters = range.GetNumberOfIndices();
173 InitializeFontRun(fontRun);
174 ProcessFontFamily(fontRun);
175 ProcessFontSize(fontRun);
176 ProcessFontWeight(fontRun);
177 ProcessFontWidth(fontRun);
178 ProcessFontSlant(fontRun);
180 logicalModel->mFontDescriptionRuns.PushBack(fontRun);
183 void FontSpan::InitializeFontRun(FontDescriptionRun& fontRun) const
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;
192 fontRun.familyDefined = false;
193 fontRun.weightDefined = false;
194 fontRun.widthDefined = false;
195 fontRun.slantDefined = false;
196 fontRun.sizeDefined = false;
199 void FontSpan::ProcessFontFamily(FontDescriptionRun& fontRun) const
201 if(IsFamilyNameDefined())
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.
212 void FontSpan::ProcessFontSize(FontDescriptionRun& fontRun) const
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;
222 void FontSpan::ProcessFontWeight(FontDescriptionRun& fontRun) const
224 if(IsWeightDefined())
226 fontRun.weight = GetWeight();
227 fontRun.weightDefined = true;
231 void FontSpan::ProcessFontWidth(FontDescriptionRun& fontRun) const
235 fontRun.width = GetWidth();
236 fontRun.widthDefined = true;
240 void FontSpan::ProcessFontSlant(FontDescriptionRun& fontRun) const
244 fontRun.slant = GetSlant();
245 fontRun.slantDefined = true;
249 } // namespace Internal
253 } // namespace Toolkit