Tizen 2.1 base
[framework/osp/uifw.git] / src / graphics / FGrp_FontImpl.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        FGrp_FontImpl.cpp
20  * @brief       This is the implementation file for _FontImpl class.
21  *
22  */
23
24 #include <new>
25 #include <memory>
26
27 #include <FGrpFont.h>
28
29 #include <FBaseSysLog.h>
30
31 #include "FGrp_FontImpl.h"
32 #include "FGrp_Font.h"
33 #include "FGrp_ResUtil.h"
34
35
36 #define IF_NOT_INITIALIZED(code) if (this->_pNativeFont == null || this->_pCoordHolder == null) \
37         { \
38                 code; \
39         }
40 #define IF_NOT_CONSTRUCTED(code) if (this->_pNativeFont == null || (!this->_pNativeFont->IsConstructed())) \
41         { \
42                 code; \
43         }
44 #define IsSucceeded(X) (!IsFailed(X))
45 #define CONVERT_INTEGER_TO_26_6_FIXED_POINT(value) (value * 64)
46
47
48 namespace Tizen { namespace Graphics
49 {
50
51 struct _FontCoordHolder
52 {
53         _ResUtil::CoordHolder <int> size;
54         _ResUtil::CoordHolder <int> charSpace;
55
56         _FontCoordHolder()
57         {
58                 Reset(-1);
59         }
60
61         void Reset(int _size)
62         {
63                 size = _size;
64                 charSpace = 0;
65         }
66 };
67
68
69 _FontImpl::_FontImpl()
70         : _pCoordHolder(new (std::nothrow) _FontCoordHolder)
71         , _pNativeFont(new (std::nothrow) _Font)
72 {
73         if (_pNativeFont == null || _pCoordHolder == null)
74         {
75                 delete _pNativeFont;
76                 delete _pCoordHolder;
77
78                 _pNativeFont = null;
79                 _pCoordHolder = null;
80         }
81 }
82
83 _FontImpl::_FontImpl(const _FontImpl& obj)
84         : _pCoordHolder(new (std::nothrow) _FontCoordHolder)
85         , _pNativeFont(0)
86 {
87         if (_pCoordHolder)
88         {
89                 *_pCoordHolder = *obj._pCoordHolder;
90         }
91
92         _pNativeFont = obj._pNativeFont->CloneN();
93
94         if (_pNativeFont == null || _pCoordHolder == null)
95         {
96                 delete _pNativeFont;
97                 delete _pCoordHolder;
98
99                 _pNativeFont = null;
100                 _pCoordHolder = null;
101         }
102 }
103
104 _FontImpl::~_FontImpl()
105 {
106         if (_pNativeFont)
107         {
108                 delete _pNativeFont;
109                 _pNativeFont = 0;
110         }
111
112         if (_pCoordHolder)
113         {
114                 delete _pCoordHolder;
115                 _pCoordHolder = 0;
116         }
117 }
118
119 result
120 _FontImpl::Construct(int style, int vcSize)
121 {
122         IF_NOT_INITIALIZED(return E_OUT_OF_MEMORY);
123
124         // convert VC -> PC
125         int pcSize = _ResUtil::ConvertToPhyCoordHeight(CONVERT_INTEGER_TO_26_6_FIXED_POINT(vcSize));
126         if (vcSize > 0 && pcSize <= CONVERT_INTEGER_TO_26_6_FIXED_POINT(1))
127         {
128                 pcSize = CONVERT_INTEGER_TO_26_6_FIXED_POINT(1);
129         }
130
131         result r = _pNativeFont->Construct(style, pcSize);
132         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Failed to construct _Font", GetErrorMessage(r));
133
134         _pCoordHolder->Reset(vcSize);
135
136         return E_SUCCESS;
137 }
138
139 result
140 _FontImpl::Construct(const Tizen::Base::String& fontName, int style, int vcSize, bool isPathEnabled)
141 {
142         IF_NOT_INITIALIZED(return E_OUT_OF_MEMORY);
143
144         // convert VC -> PC
145         int pcSize = _ResUtil::ConvertToPhyCoordHeight(CONVERT_INTEGER_TO_26_6_FIXED_POINT(vcSize));
146         if (vcSize > 0 && pcSize <= CONVERT_INTEGER_TO_26_6_FIXED_POINT(1))
147         {
148                 pcSize = CONVERT_INTEGER_TO_26_6_FIXED_POINT(1);
149         }
150
151         result r = _pNativeFont->Construct(fontName, style, pcSize, isPathEnabled);
152         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Failed to construct _Font", GetErrorMessage(r));
153
154         _pCoordHolder->Reset(vcSize);
155
156         return E_SUCCESS;
157 }
158
159 result
160 _FontImpl::Construct(const Tizen::Base::ByteBuffer& fontData, int style, int vcSize)
161 {
162         IF_NOT_INITIALIZED(return E_OUT_OF_MEMORY);
163
164         // convert VC -> PC
165         int pcSize = _ResUtil::ConvertToPhyCoordHeight(CONVERT_INTEGER_TO_26_6_FIXED_POINT(vcSize));
166         if (vcSize > 0 && pcSize <= CONVERT_INTEGER_TO_26_6_FIXED_POINT(1))
167         {
168                 pcSize = CONVERT_INTEGER_TO_26_6_FIXED_POINT(1);
169         }
170
171         result r = _pNativeFont->Construct(fontData, style, pcSize);
172         SysTryReturn(NID_GRP, r == E_SUCCESS, r, r, "[%s] Failed to construct _Font", GetErrorMessage(r));
173
174         _pCoordHolder->Reset(vcSize);
175
176         return E_SUCCESS;
177 }
178
179 bool
180 _FontImpl::IsConstructed(void) const
181 {
182         IF_NOT_CONSTRUCTED(return false);
183
184         return true;
185 }
186
187 int
188 _FontImpl::GetMaxHeight(void) const
189 {
190         IF_NOT_CONSTRUCTED(return -1);
191
192         int pcMaxHeight = _pNativeFont->GetMaxHeight();
193         int vcMaxHeight = _ResUtil::ConvertToVirCoordY(pcMaxHeight);
194
195         return vcMaxHeight;
196 }
197
198 int
199 _FontImpl::GetMaxWidth(void) const
200 {
201         IF_NOT_CONSTRUCTED(return -1);
202
203         int pcMaxWidth = _pNativeFont->GetMaxWidth();
204         int vcMaxWidth = _ResUtil::ConvertToVirCoordX(pcMaxWidth);
205
206         return vcMaxWidth;
207 }
208
209 int
210 _FontImpl::GetAscender(void) const
211 {
212         IF_NOT_CONSTRUCTED(return -1);
213
214         int pcAscender = _pNativeFont->GetAscender();
215         int vcAscender = _ResUtil::ConvertToVirCoordY(pcAscender);
216
217         return vcAscender;
218 }
219
220 int
221 _FontImpl::GetDescender(void) const
222 {
223         IF_NOT_CONSTRUCTED(return -1);
224
225         int pcDescender = _pNativeFont->GetDescender();
226         int vcDescender = _ResUtil::ConvertToVirCoordY(pcDescender);
227         if (vcDescender < 0)
228         {
229                 vcDescender *= -1;
230         }
231
232         return vcDescender;
233 }
234
235 result
236 _FontImpl::GetLeftBear(wchar_t character, int& vcLeftBear) const
237 {
238         IF_NOT_CONSTRUCTED(return E_OPERATION_FAILED);
239
240         int pcLeftBear = 0;
241         result r = _pNativeFont->GetLeftBear(character, pcLeftBear);
242
243         // set outparam
244         vcLeftBear = _ResUtil::ConvertToVirCoordX(pcLeftBear);
245
246         return r;
247 }
248
249 result
250 _FontImpl::GetRightBear(wchar_t character, int& vcRightBear) const
251 {
252         IF_NOT_CONSTRUCTED(return E_OPERATION_FAILED);
253
254         int pcRightBear = 0;
255         result r = _pNativeFont->GetRightBear(character, pcRightBear);
256
257         // set outparam
258         vcRightBear = _ResUtil::ConvertToVirCoordX(pcRightBear);
259
260         return r;
261 }
262
263 result
264 _FontImpl::GetTextExtent(const Tizen::Base::String& text, int length, Dimension& vcDim) const
265 {
266         IF_NOT_CONSTRUCTED(return E_OPERATION_FAILED);
267
268         if (_ResUtil::NeedToConvertCoord())
269         {
270                 Dimension pcDim;
271                 result r = _pNativeFont->GetTextExtent(text, length, pcDim);
272
273                 if (IsSucceeded(r))
274                 {
275                         vcDim = _ResUtil::ConvertToVirCoord(pcDim);
276                 }
277
278                 return r;
279         }
280         else
281         {
282                 return _pNativeFont->GetTextExtent(text, length, vcDim);
283         }
284 }
285
286 bool
287 _FontImpl::IsBold(void) const
288 {
289         IF_NOT_CONSTRUCTED(return false);
290
291         return _pNativeFont->IsBold();
292 }
293
294 bool
295 _FontImpl::IsItalic(void) const
296 {
297         IF_NOT_CONSTRUCTED(return false);
298
299         return _pNativeFont->IsItalic();
300 }
301
302 bool
303 _FontImpl::IsPlain(void) const
304 {
305         IF_NOT_CONSTRUCTED(return false);
306
307         return _pNativeFont->IsPlain();
308 }
309
310 bool
311 _FontImpl::IsStrikeOut(void) const
312 {
313         IF_NOT_CONSTRUCTED(return false);
314
315         return _pNativeFont->IsStrikeOut();
316 }
317
318 bool
319 _FontImpl::IsUnderlined(void) const
320 {
321         IF_NOT_CONSTRUCTED(return false);
322
323         return _pNativeFont->IsUnderlined();
324 }
325
326 int
327 _FontImpl::GetSize(void) const
328 {
329         IF_NOT_CONSTRUCTED(return -1);
330
331         if (_ResUtil::NeedToConvertCoord())
332         {
333                 return _pCoordHolder->size.required;
334         }
335         else
336         {
337                 return _pNativeFont->GetSize();
338         }
339 }
340
341 void
342 _FontImpl::SetStrikeOut(bool strikeOut)
343 {
344         IF_NOT_CONSTRUCTED(return);
345
346         return _pNativeFont->SetStrikeOut(strikeOut);
347 }
348
349 void
350 _FontImpl::SetUnderline(bool underline)
351 {
352         IF_NOT_CONSTRUCTED(return);
353
354         return _pNativeFont->SetUnderline(underline);
355 }
356
357 void
358 _FontImpl::SetCharSpace(int vcSpace)
359 {
360         IF_NOT_CONSTRUCTED(return);
361
362         // save it
363         _pCoordHolder->charSpace = vcSpace;
364
365         // convert VC -> PC
366         int pcSpace = _ResUtil::ConvertToPhyCoordWidth(vcSpace);
367
368         return _pNativeFont->SetCharSpace(pcSpace);
369 }
370
371 int
372 _FontImpl::GetCharSpace(void) const
373 {
374         IF_NOT_CONSTRUCTED(return 0);
375
376         if (_ResUtil::NeedToConvertCoord())
377         {
378                 return _pCoordHolder->charSpace.required;
379         }
380         else
381         {
382                 return _pNativeFont->GetCharSpace();
383         }
384 }
385
386 Tizen::Base::String
387 _FontImpl::GetFaceName(void) const
388 {
389         IF_NOT_CONSTRUCTED(return "");
390
391         return _pNativeFont->GetFaceName();
392 }
393
394 Tizen::Base::Collection::IList*
395 _FontImpl::GetSystemFontListN(void)
396 {
397         return _Font::GetSystemFontListN();
398 }
399
400 Tizen::Base::String
401 _FontImpl::GetFaceName(const Tizen::Base::String& filePath)
402 {
403         return _Font::GetFaceName(filePath);
404 }
405
406 _FontImpl*
407 _FontImpl::CloneN(void)
408 {
409         IF_NOT_CONSTRUCTED(return null);
410
411         std::auto_ptr<_FontImpl> pFontImpl(new (std::nothrow) _FontImpl(*this));
412
413         SysTryReturn(NID_GRP, pFontImpl.get(), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocated memory");
414         SysTryReturn(NID_GRP, pFontImpl->_pNativeFont, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] FontEx::CloneN() has been failed");
415
416         return pFontImpl.release();
417 }
418
419 Font*
420 _FontImpl::CloneN(const Font& font)
421 {
422         // check the validation of input param
423         SysTryReturn(NID_GRP, font.__pImpl && font.__pImpl->IsConstructed(), null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input param given");
424
425         // make sure if the copy construction is well done
426         std::auto_ptr<Font> pFont(new (std::nothrow) Font(font));
427
428         SysTryReturn(NID_GRP, pFont.get() && pFont->__pImpl && pFont->__pImpl->IsConstructed(), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory");
429
430         return pFont.release();
431 }
432
433 result
434 _FontImpl::GetTextExtent(int vcWidth, const Tizen::Base::String& text, int startIndex, int length, bool outline, int& count, Dimension& vcDim) const
435 {
436         IF_NOT_CONSTRUCTED(return E_OPERATION_FAILED);
437
438         SysTryReturnResult(NID_GRP, startIndex >= 0 && length >= 0 && startIndex < text.GetLength(), E_OUT_OF_RANGE,
439                 "The length(%d) & startIndex(%d) of the given text is out of range", length, startIndex);
440
441         if (_ResUtil::NeedToConvertCoord())
442         {
443                 int pcWidth = _ResUtil::ToPhyCoordW(vcWidth);
444                 Dimension pcDim;
445                 result r = _pNativeFont->GetTextExtent(pcWidth, text, startIndex, length, outline, count, pcDim);
446
447                 if (IsSucceeded(r))
448                 {
449                         vcDim = _ResUtil::ConvertToVirCoord(pcDim);
450                 }
451
452                 return r;
453         }
454         else
455         {
456                 return _pNativeFont->GetTextExtent(vcWidth, text, startIndex, length, outline, count, vcDim);
457         }
458 }
459
460 result
461 _FontImpl::GetTextExtent(int vcWidth, const Tizen::Base::String& text, int startIndex, int length, bool outline, const Tizen::Base::String& delimiter, int& count, Dimension& vcDim) const
462 {
463         IF_NOT_CONSTRUCTED(return E_OPERATION_FAILED);
464
465         SysTryReturnResult(NID_GRP, startIndex >= 0 && length >= 0 && startIndex < text.GetLength(), E_OUT_OF_RANGE,
466                 "The length(%d) & startIndex(%d) of the given text is out of range", length, startIndex);
467
468         if (_ResUtil::NeedToConvertCoord())
469         {
470                 int pcWidth = _ResUtil::ToPhyCoordW(vcWidth);
471                 Dimension pcDim;
472                 result r = _pNativeFont->GetTextExtent(pcWidth, text, startIndex, length, outline, delimiter, count, pcDim);
473
474                 if (IsSucceeded(r))
475                 {
476                         vcDim = _ResUtil::ConvertToVirCoord(pcDim);
477                 }
478
479                 return r;
480         }
481         else
482         {
483                 return _pNativeFont->GetTextExtent(vcWidth, text, startIndex, length, outline, delimiter, count, vcDim);
484         }
485 }
486
487 result
488 _FontImpl::SetSize(int vcSize)
489 {
490         IF_NOT_CONSTRUCTED(return E_OPERATION_FAILED);
491         SysTryReturnResult(NID_GRP, vcSize > 0, E_INVALID_ARG, "font size should be greater than 0");
492
493         _pCoordHolder->Reset(vcSize);
494
495         // convert VC -> PC
496         int pcSize = _ResUtil::ConvertToPhyCoordHeight(CONVERT_INTEGER_TO_26_6_FIXED_POINT(vcSize));
497         if (vcSize > 0 && pcSize <= CONVERT_INTEGER_TO_26_6_FIXED_POINT(1))
498         {
499                 pcSize = CONVERT_INTEGER_TO_26_6_FIXED_POINT(1);
500                 _pCoordHolder->size.phyCoord = pcSize;
501                 _pCoordHolder->size.virCoord = vcSize;
502         }
503
504         result r = _pNativeFont->SetSize(pcSize);
505         SysTryReturnResult(NID_GRP, r == E_SUCCESS, E_SYSTEM, "Failed to set the size of _Font");
506
507         return E_SUCCESS;
508 }
509
510 result
511 _FontImpl::SetStyle(int style)
512 {
513         IF_NOT_CONSTRUCTED(return E_OPERATION_FAILED);
514         SysTryReturnResult(NID_GRP, style > FONT_STYLE_MIN && style <= (FONT_STYLE_PLAIN | FONT_STYLE_BOLD | FONT_STYLE_ITALIC), E_INVALID_ARG,
515                 "Style(%d) is invalid", style);
516
517         result r = _pNativeFont->SetStyle(style);
518         SysTryReturnResult(NID_GRP, r == E_SUCCESS, E_SYSTEM, "Failed to set the style of _Font");
519
520         return E_SUCCESS;
521 }
522
523 int
524 _FontImpl::GetStyle(void) const
525 {
526         IF_NOT_CONSTRUCTED(return FONT_STYLE_PLAIN);
527
528         return _pNativeFont->GetStyle();
529 }
530
531 int
532 _FontImpl::GetLeading(void) const
533 {
534         IF_NOT_CONSTRUCTED(return -1);
535
536         int pcLeading = _pNativeFont->GetLeading();
537         int vcLeading = _ResUtil::ConvertToVirCoordY(pcLeading);
538
539         return vcLeading;
540 }
541
542 _FontImpl*
543 _FontImpl::GetInstance(Font& font)
544 {
545         return (&font != null) ? font.__pImpl : null;
546 }
547
548 const _FontImpl*
549 _FontImpl::GetInstance(const Font& font)
550 {
551         return (&font != null) ? font.__pImpl : null;
552 }
553
554 bool
555 _FontImpl::UpdateDefaultFont(const Tizen::Base::String& key)
556 {
557         return _Font::UpdateDefaultFont(key);
558 }
559
560 }} // Tizen::Graphics