Fork for IVI: mesa fixing
[profile/ivi/uifw.git] / src / graphics / text / FGrp_TextTextUtility.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_TextTextUtility.cpp
20  * @brief       This is the implementation file for TextUtility class.
21  */
22
23 #include <new>
24 #include <FBaseSysLog.h>
25 #include "../FGrp_Canvas.h"
26 #include "FGrp_FontImpl.h"
27 #include "FGrp_TextCommon.h"
28 #include "FGrp_TextTextSimple.h"
29 #include "FGrp_TextTextUtility.h"
30
31 using namespace Tizen::Base::Utility;
32 using namespace Tizen::Base;
33
34 namespace Tizen { namespace Graphics
35 {
36
37 namespace _Text
38 {
39 TextElementType
40 TextUtility::GetObjectTypeFromValueType(TextComponentInfoValueType type)
41 {
42         if (type < MAX_TEXTOBJECT_VALUE)
43         {
44                 return TEXT_ELEMENT_TYPE_TEXT;
45         }
46         else if (type < MAX_IMAGEOBJECT_VALUE)
47         {
48                 return TEXT_ELEMENT_TYPE_IMAGE;
49         }
50
51         return TEXT_ELEMENT_TYPE_MAX;
52 }
53
54 bool
55 TextUtility::CanMerge(TextElement* pCurrentTextElement, TextElement* pNextTextElement)
56 {
57         SysTryReturn(NID_GRP
58                         , pCurrentTextElement && pCurrentTextElement->GetType() == TEXT_ELEMENT_TYPE_TEXT
59                         , false, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
60
61         SysTryReturn(NID_GRP
62                         , pNextTextElement && pNextTextElement->GetType() == TEXT_ELEMENT_TYPE_TEXT
63                         , false, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
64
65         TextSimple* pCurrentSimpleText = dynamic_cast < TextSimple* >(pCurrentTextElement);
66         TextSimple* pNextSimpleText = dynamic_cast < TextSimple* >(pNextTextElement);
67         if (pCurrentSimpleText == null || pNextSimpleText == null)
68         {
69                 return false;
70         }
71
72         if (pCurrentSimpleText->GetTextSource() != pNextSimpleText->GetTextSource())
73         {
74                 return false;
75         }
76
77         if (!IsSameFontAttribute(pCurrentSimpleText->GetFont(), pNextSimpleText->GetFont()))
78         {
79                 return false;
80         }
81
82         if (pCurrentSimpleText->GetForegroundColor() != pNextSimpleText->GetForegroundColor())
83         {
84                 return false;
85         }
86
87         if (pCurrentSimpleText->GetBackgroundColor() != pNextSimpleText->GetBackgroundColor())
88         {
89                 return false;
90         }
91
92         if (pCurrentSimpleText->GetBitmap() != null || pNextSimpleText->GetBitmap() != null)
93         {
94                 return false;
95         }
96
97         return true;
98 }
99
100 bool
101 TextUtility::IsSameFontAttribute(const Font* pFontSrc, const Font* pFontTarget)
102 {
103         if (pFontSrc->GetSize() != pFontTarget->GetSize())
104         {
105                 return false;
106         }
107
108         if (pFontSrc->IsBold() != pFontTarget->IsBold())
109         {
110                 return false;
111         }
112
113         if (pFontSrc->IsItalic() != pFontTarget->IsItalic())
114         {
115                 return false;
116         }
117
118         if (pFontSrc->IsPlain() != pFontTarget->IsPlain())
119         {
120                 return false;
121         }
122
123         if (pFontSrc->IsStrikeOut() != pFontTarget->IsStrikeOut())
124         {
125                 return false;
126         }
127
128         if (pFontSrc->IsUnderlined() != pFontTarget->IsUnderlined())
129         {
130                 return false;
131         }
132
133         return true;
134 }
135
136 int
137 TextUtility::GetFontMaxHeight(Font* pFont)
138 {
139         if (!pFont)
140         {
141                 return 0;
142         }
143
144         _Font* pNativeFont = _Font::GetInstance(*_FontImpl::GetInstance(*pFont));
145         SysTryReturn(NID_GRP
146                         , pNativeFont
147                         , -1, E_SYSTEM, "[E_SYSTEM] Fail to get native font instance.");
148
149         return pNativeFont->GetLeading();
150 }
151
152 int
153 TextUtility::GetFontMaxHeight(_Font* pFont)
154 {
155         if (!pFont)
156         {
157                 return 0;
158         }
159
160         return pFont->GetLeading();
161 }
162
163 int
164 TextUtility::GetTextLength(const wchar_t* pText)
165 {
166         int count = 0;
167
168         while (*(pText + count) != 0)
169         {
170                 count++;
171         }
172
173         return count;
174 }
175
176 int
177 TextUtility::GetCharCountInWidth(_Font* pFont, const wchar_t* pText, int textLength, int maxWidth, bool outline, int& width, int& height)
178 {
179         if (!pFont)
180         {
181                 return 0;
182         }
183
184         result r = E_SUCCESS;
185         int count = 0;
186         Dimension lineDimension;
187         Tizen::Base::String text(pText);
188
189         r = pFont->GetTextExtent(maxWidth, text, 0, textLength, outline, L"", count, lineDimension);
190         SysTryCatch(NID_GRP
191                         , r == E_SUCCESS
192                         , , r, "[%s] Propagating.", GetErrorMessage(r));
193
194         width = lineDimension.width;
195         height = pFont->GetLeading();
196
197         SetLastResult(E_SUCCESS);
198
199         return count;
200
201 CATCH:
202         width = 0;
203         height = 0;
204         return 0;
205 }
206
207 result
208 TextUtility::CopyText(wchar_t* pDstText, const wchar_t* pSrcText, int textLength)
209 {
210         SysTryReturn(NID_GRP
211                         , pDstText && pSrcText
212                         , E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
213
214         int srcTextLength = 0;
215         wchar_t ch = 0;
216
217         srcTextLength = GetTextLength(pSrcText);
218         if (srcTextLength < textLength)
219         {
220                 textLength = srcTextLength;
221         }
222
223         if (textLength == 0)
224         {
225                 *pDstText = 0;
226                 return E_SUCCESS;
227         }
228
229         do
230         {
231                 ch = *pSrcText++;
232                 *pDstText++ = ch;
233                 if (--textLength == 0)
234                 {
235                         if (ch != 0)
236                         {
237                                 *pDstText++ = 0;
238                         }
239                         return E_SUCCESS;
240                 }
241         }
242         while (ch != 0);
243
244         do
245         {
246                 *pDstText++ = 0;
247         }
248         while (0 < --textLength);
249
250         return E_SUCCESS;
251 }
252
253 result
254 TextUtility::CopyText(wchar_t* pDstText, const wchar_t* pSrcText)
255 {
256         SysTryReturn(NID_GRP
257                 , pDstText && pSrcText
258                 , E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
259
260         int i = 0;
261         wchar_t ch = 0;
262
263         do
264         {
265                 ch = pSrcText[i];
266                 pDstText[i] = ch;
267                 i++;
268         }
269         while (ch != 0);
270
271         return E_SUCCESS;
272 }
273
274 result
275 TextUtility::DrawText(_Canvas& canvas, int x, int y, const wchar_t* pText, int textLength)
276 {
277         result r = E_SUCCESS;
278         Point point(x, y);
279
280         Tizen::Base::String text(pText);
281
282         if (pText[textLength - 1] == TEXT_LINE_FEED || pText[textLength - 1] == TEXT_CARRIAGE_RETURN)
283         {
284                 textLength--;
285         }
286
287         r = canvas.DrawText(point, text, 0, textLength);
288         SysTryReturn(NID_GRP
289                 , r == E_SUCCESS
290                 , r, r, "[%s] Propagating.", GetErrorMessage(r));
291
292         return E_SUCCESS;
293 }
294
295 result
296 TextUtility::DrawOutlineText(_Canvas& canvas, int x, int y, const wchar_t* pText, int textLength, Color outlineColor)
297 {
298         result r = E_SUCCESS;
299         Point point(x, y);
300
301         Tizen::Base::String text(pText);
302
303         if (pText[textLength - 1] == TEXT_LINE_FEED || pText[textLength - 1] == TEXT_CARRIAGE_RETURN)
304         {
305                 textLength--;
306         }
307
308         r = canvas.DrawText(point, text, 0, textLength, outlineColor);
309         SysTryReturn(NID_GRP
310                 , r == E_SUCCESS
311                 , r, r, "[%s] Propagating.", GetErrorMessage(r));
312
313         return E_SUCCESS;
314 }
315
316 result
317 TextUtility::GetTextExtent(_Font* pFont, const wchar_t* pText, int textLength, bool outline, int& width, int& height)
318 {
319         SysTryReturn(NID_GRP
320                 , pFont && pText
321                 , E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
322
323         result r = E_SUCCESS;
324         Dimension lineDimension;
325         Tizen::Base::String text(pText);
326
327         r = pFont->GetTextExtent(text, textLength, lineDimension);
328         SysTryCatch(NID_GRP
329                         , r == E_SUCCESS
330                         , , r, "[%s] Propagating.", GetErrorMessage(r));
331
332         width = lineDimension.width;
333         height = pFont->GetLeading();
334
335         return E_SUCCESS;
336
337 CATCH:
338         width = 0;
339         height = 0;
340         return r;
341 }
342
343 result
344 TextUtility::GetTextExtent(Font* pFont, const wchar_t* pText, int textLength, bool outline, int& width, int& height)
345 {
346         SysTryReturn(NID_GRP
347                 , pFont && pText
348                 , E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
349
350         result r = E_SUCCESS;
351         int count = 0;
352         Dimension lineDimension;
353         Tizen::Base::String text(pText);
354
355         _Font* pNativeFont = _Font::GetInstance(*_FontImpl::GetInstance(*pFont));
356         SysTryReturn(NID_GRP
357                         , pNativeFont
358                         , -1, E_SYSTEM, "[E_SYSTEM] Fail to get native font instance.");
359
360         r = pNativeFont->GetTextExtent((Integer::VALUE_MAX >> 1) - 1, text, 0, textLength, outline, L"", count, lineDimension);
361         SysTryCatch(NID_GRP
362                         , r == E_SUCCESS
363                         , , r, "[%s] Propagating.", GetErrorMessage(r));
364
365         width = lineDimension.width;
366         height = pNativeFont->GetLeading();
367
368         return E_SUCCESS;
369
370 CATCH:
371         width = 0;
372         height = 0;
373         return r;
374 }
375
376 }}} // Tizen::Graphics::_Text
377