Tizen 2.1 base
[framework/osp/uifw.git] / src / graphics / FGrpFont.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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  * @file                FGrpFont.cpp
20  * @brief               This is the implementation file for Font class.
21  *
22  * This header file contains implementation of Font class.
23  *
24  */
25
26 #include <FApp_AppInfo.h>
27 #include <FGrpFont.h>
28 #include <FBaseString.h>
29
30 #include <FBaseSysLog.h>
31
32 #include "FGrp_FontImpl.h"
33 #include "FGrp_ResUtil.h"
34
35 #define CHECK_NOT_CONSTRUCTED \
36         SysTryReturnResult(NID_GRP, this->__pImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Fails to allocate memory.") \
37         SysAssertf(!this->__pImpl->IsConstructed(), \
38                 "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class.");
39
40 #define CHECK_CONSTRUCTED \
41         SysAssertf((this->__pImpl != null) && this->__pImpl->IsConstructed(), \
42                 "Not yet constructed! Construct() should be called before use.");
43
44 #define CHECK_CONSTRUCTED_EX(_result) \
45         SysAssertf((this->__pImpl != null) && this->__pImpl->IsConstructed(), \
46                 "Not yet constructed! Construct() should be called before use.");
47
48 #define CHECK_CONSTRUCTED_VOID \
49         SysAssertf((this->__pImpl != null) && this->__pImpl->IsConstructed(), \
50                 "Not yet constructed! Construct() should be called before use.");
51
52
53 using namespace Tizen::Base;
54 using namespace Tizen::Graphics;
55
56
57 namespace Tizen { namespace Graphics
58 {
59
60 Font::Font()
61 {
62         __pImpl = new (std::nothrow) _FontImpl;
63 }
64
65 Font::Font(const Font& rhs)
66 {
67         __pImpl = rhs.__pImpl->CloneN();
68 }
69
70 Font::~Font(void)
71 {
72         delete __pImpl;
73         __pImpl = null;
74 }
75
76 result
77 Font::Construct(int style, int vc_size)
78 {
79         CHECK_NOT_CONSTRUCTED;
80
81         // check input param.
82         SysTryReturnResult(NID_GRP, vc_size > 0, E_INVALID_ARG, "Font size should be greater than 0");
83         SysTryReturnResult(NID_GRP, FONT_STYLE_MIN < style && style <= (FONT_STYLE_PLAIN | FONT_STYLE_BOLD | FONT_STYLE_ITALIC), E_INVALID_ARG,
84                 "Style(%d) is invalid", style);
85
86         result r = __pImpl->Construct(style, vc_size);
87
88         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
89
90         return E_SUCCESS;
91 }
92
93 result
94 Font::Construct(const Tizen::Base::String& fontNameOrPath, int style, int vc_size)
95 {
96         CHECK_NOT_CONSTRUCTED;
97
98         // check input param.
99         SysTryReturnResult(NID_GRP, vc_size > 0, E_INVALID_ARG, "Font size should be greater than 0");
100         SysTryReturnResult(NID_GRP, FONT_STYLE_MIN < style && style <= (FONT_STYLE_PLAIN | FONT_STYLE_BOLD | FONT_STYLE_ITALIC), E_INVALID_ARG,
101                 "Style(%d) is invalid.\n", style);
102         SysTryReturnResult(NID_GRP, fontNameOrPath.GetLength() > 0, E_FILE_NOT_FOUND, "The specified font could not be found or accessed");
103         SysTryReturnResult(NID_GRP, !fontNameOrPath.EndsWith(L"/") && !fontNameOrPath.EndsWith(L"\\"), E_FILE_NOT_FOUND,
104                 "The specified font could not be found or accessed.\n");
105
106         result r = __pImpl->Construct(fontNameOrPath, style, vc_size);
107
108         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
109
110         return E_SUCCESS;
111 }
112
113 result
114 Font::Construct(const Tizen::Base::ByteBuffer& fontData, int style, int vc_size)
115 {
116         CHECK_NOT_CONSTRUCTED;
117
118         // check input param.
119         SysTryReturnResult(NID_GRP, fontData.GetLimit() > 0, E_INVALID_ARG, "Buffer size should be greater than 0");
120         SysTryReturnResult(NID_GRP, vc_size > 0, E_INVALID_ARG, "FontEx size should be greater than 0");
121         SysTryReturnResult(NID_GRP, style > FONT_STYLE_MIN && style <= (FONT_STYLE_PLAIN | FONT_STYLE_BOLD | FONT_STYLE_ITALIC), E_INVALID_ARG,
122                 "Style(%d) is invalid", style);
123
124         result r = __pImpl->Construct(fontData, style, vc_size);
125
126         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
127
128         return E_SUCCESS;
129 }
130
131 int
132 Font::GetMaxHeight(void) const
133 {
134         CHECK_CONSTRUCTED_EX(-1);
135
136         return __pImpl->GetMaxHeight();
137 }
138
139 int
140 Font::GetMaxWidth(void) const
141 {
142         CHECK_CONSTRUCTED_EX(-1);
143
144         return __pImpl->GetMaxWidth();
145 }
146
147 int
148 Font::GetAscender(void) const
149 {
150         CHECK_CONSTRUCTED_EX(-1);
151
152         return __pImpl->GetAscender();
153 }
154
155 int
156 Font::GetDescender(void) const
157 {
158         CHECK_CONSTRUCTED_EX(-1);
159
160         return __pImpl->GetDescender();
161 }
162
163 result
164 Font::GetLeftBear(wchar_t character, int& vc_leftBear) const
165 {
166         CHECK_CONSTRUCTED;
167
168         return __pImpl->GetLeftBear(character, vc_leftBear);
169 }
170
171 result
172 Font::GetRightBear(wchar_t character, int& vc_rightBear) const
173 {
174         CHECK_CONSTRUCTED;
175
176         return __pImpl->GetRightBear(character, vc_rightBear);
177 }
178
179 result
180 Font::GetTextExtent(const Tizen::Base::String& text, int length, Dimension& vc_dim) const
181 {
182         CHECK_CONSTRUCTED;
183
184         SysTryReturnResult(NID_GRP, length >= 0 && length <= text.GetLength(), E_OUT_OF_RANGE,
185                 "The length(%d) of the given text is out of range", length);
186
187         if (length == 0 || text.GetLength() == 0)
188         {
189                 return E_SUCCESS;
190         }
191
192         result r = __pImpl->GetTextExtent(text, length, vc_dim);
193
194         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
195
196         if (!Tizen::App::_AppInfo::IsOspCompat())
197         {
198                 vc_dim.height = GetMaxHeight();
199         }
200
201         return E_SUCCESS;
202 }
203
204 bool
205 Font::IsBold(void) const
206 {
207         CHECK_CONSTRUCTED_EX(false);
208
209         return __pImpl->IsBold();
210 }
211
212 bool
213 Font::IsItalic(void) const
214 {
215         CHECK_CONSTRUCTED_EX(false);
216
217         return __pImpl->IsItalic();
218 }
219
220 bool
221 Font::IsPlain(void) const
222 {
223         CHECK_CONSTRUCTED_EX(false);
224
225         return __pImpl->IsPlain();
226 }
227
228 bool
229 Font::IsStrikeOut(void) const
230 {
231         CHECK_CONSTRUCTED_EX(false);
232
233         return __pImpl->IsStrikeOut();
234 }
235
236 bool
237 Font::IsUnderlined(void) const
238 {
239         CHECK_CONSTRUCTED_EX(false);
240
241         return __pImpl->IsUnderlined();
242 }
243
244 int
245 Font::GetSize(void) const
246 {
247         CHECK_CONSTRUCTED_EX(-1);
248
249         return __pImpl->GetSize();
250 }
251
252 void
253 Font::SetStrikeOut(bool strikeout)
254 {
255         CHECK_CONSTRUCTED_VOID;
256
257         return __pImpl->SetStrikeOut(strikeout);
258 }
259
260 void
261 Font::SetUnderline(bool underline)
262 {
263         CHECK_CONSTRUCTED_VOID;
264
265         return __pImpl->SetUnderline(underline);
266 }
267
268 void
269 Font::SetCharSpace(int vc_space)
270 {
271         CHECK_CONSTRUCTED_VOID;
272
273         return __pImpl->SetCharSpace(vc_space);
274 }
275
276 int
277 Font::GetCharSpace(void) const
278 {
279         CHECK_CONSTRUCTED_EX(0);
280
281         return __pImpl->GetCharSpace();
282 }
283
284 Tizen::Base::String
285 Font::GetFaceName(void) const
286 {
287         CHECK_CONSTRUCTED_EX("");
288
289         return __pImpl->GetFaceName();
290 }
291
292 Tizen::Base::Collection::IList*
293 Font::GetSystemFontListN(void)
294 {
295         return _FontImpl::GetSystemFontListN();
296 }
297
298 Tizen::Base::String
299 Font::GetFaceName(const Tizen::Base::String& filePath)
300 {
301         return _FontImpl::GetFaceName(filePath);
302 }
303
304 }} // Tizen::Graphics