Tizen 2.1 base
[framework/osp/uifw.git] / src / graphics / text / FGrp_TextTextImage.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_TextTextImage.cpp
20  * @brief       This is the implementation file for TextImage class.
21  */
22
23 #include <new>
24 #include <FGrpBitmap.h>
25 #include <FBaseSysLog.h>
26 #include "FGrp_BitmapImpl.h"
27 #include "FGrp_CanvasImpl.h"
28 #include "FGrp_TextCommon.h"
29 #include "FGrp_TextTextElement.h"
30 #include "FGrp_TextTextImage.h"
31 #include "../FGrp_BitmapUtil.h"
32 #include "../FGrp_Canvas.h"
33
34 namespace Tizen { namespace Graphics
35 {
36
37 namespace _Text
38 {
39
40 TextImage::TextImage(Bitmap& bitmap, TextElementSourceType sourceType, Rectangle* pRect, TextObjectAlignment align)
41         : TextElement()
42 {
43         result r = E_SUCCESS;
44         _type = TEXT_ELEMENT_TYPE_IMAGE;
45         _sourceType = sourceType;
46         _length = 1;
47         _hasFocus = false;
48         _isAlternateLookEnabled = false;
49         __align = align;
50         __pBitmap = null;
51
52         if (sourceType == TEXT_ELEMENT_SOURCE_TYPE_INTERNAL)
53         {
54                 __pBitmap = new (std::nothrow) Bitmap();
55                 SysTryCatch(NID_GRP
56                         , __pBitmap
57                         , , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
58
59                 r = __pBitmap->Construct(bitmap, Rectangle(0, 0, bitmap.GetWidth(), bitmap.GetHeight()));
60                 SysTryCatch(NID_GRP
61                         , r == E_SUCCESS
62                         , , r, "[%s] Propagating.", GetErrorMessage(r));
63         }
64         else
65         {
66                 __pBitmap = &bitmap;
67         }
68
69         if (pRect == null)
70         {
71                 __rect.x = 0;
72                 __rect.y = 0;
73                 __rect.width = __pBitmap->GetWidth();
74                 __rect.height = __pBitmap->GetHeight();
75         }
76         else
77         {
78                 __rect = *pRect;
79         }
80         return;
81
82 CATCH:
83         if (__pBitmap)
84         {
85                 delete __pBitmap;
86                 __pBitmap = null;
87         }
88 }
89
90 TextImage::~TextImage(void)
91 {
92         if (_sourceType == TEXT_ELEMENT_SOURCE_TYPE_INTERNAL)
93         {
94                 if (__pBitmap)
95                 {
96                         delete __pBitmap;
97                         __pBitmap = null;
98                 }
99         }
100 }
101
102 int
103 TextImage::ForwardAnalyze(int startTextIndex, int textLength, int maxWidth, TextObjectWrapType wrap, int& actualLength,
104                                                    int& width, int& height)
105 {
106         SysTryReturn(NID_GRP
107                         , 0 <= startTextIndex && startTextIndex < _length
108                         , -1, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
109
110         SysTryReturn(NID_GRP
111                         , textLength <= _length
112                         , -1, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
113
114         actualLength = 0;
115         width = 0;
116         height = 0;
117
118         if (textLength == 0)
119         {
120                 return TEXT_RETBY_NORMAL;
121         }
122
123         if (maxWidth < __rect.width)
124         {
125                 return TEXT_RETBY_OVERWIDTH;
126         }
127         else
128         {
129                 actualLength = 1;
130                 width = __rect.width;
131                 height = __rect.height;
132         }
133
134         return TEXT_RETBY_NORMAL;
135 }
136
137 result
138 TextImage::GetRegion(int textIndex, int textLength, int& width, int& height) const
139 {
140         SysTryReturn(NID_GRP
141                         , 0 <= textIndex && textIndex < _length
142                         , E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
143
144         SysTryReturn(NID_GRP
145                         , textLength <= _length
146                         , E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
147
148         if (textLength == 0)
149         {
150                 width = 0;
151                 height = 0;
152                 return E_SUCCESS;
153         }
154
155         width = __rect.width;
156         height = __rect.height;
157
158         return E_SUCCESS;
159 }
160
161 result
162 TextImage::GetBlockRegion(int textIndex, int textLength, int& width, int& height) const
163 {
164         return GetRegion(textIndex, textLength, width, height);
165 }
166
167 int
168 TextImage::GetHeight(void) const
169 {
170         return __rect.height;
171 }
172
173 result
174 TextImage::Draw(_CanvasImpl& canvasImpl, Rectangle& displayRect, int startTextIndex, int textLength, const TextObjectAlignment align, const TextObjectActionType action)
175 {
176         SysTryReturn(NID_GRP
177                 , __pBitmap
178                 , E_INVALID_STATE, E_INVALID_STATE, "[E_INVALID_STATE] This instance is not constructed yet.");
179
180         result r = E_SUCCESS;
181         if (textLength == 0)
182         {
183                 return E_SUCCESS;
184         }
185
186         _Canvas* pCanvas = _Canvas::GetInstance(canvasImpl);
187         SysTryReturn(NID_GRP
188                         , pCanvas
189                         , E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Fail to get native canvas instance.");
190
191
192         Rectangle bitmapRect = displayRect;
193
194         if (bitmapRect.width <= 0)
195         {
196                 bitmapRect.width = __rect.width;
197         }
198         if (bitmapRect.height <= 0)
199         {
200                 bitmapRect.height = __rect.height;
201         }
202
203         if (bitmapRect.width > __rect.width)
204         {
205                 bitmapRect.width = __rect.width;
206         }
207         if (bitmapRect.height > __rect.height)
208         {
209                 bitmapRect.height = __rect.height;
210         }
211
212         switch (__align & TEXT_ALIGNMASK_VERT)
213         {
214         case TEXT_OBJECT_ALIGNMENT_TOP:
215                 // fall through
216         default:
217                 break;
218
219         case TEXT_OBJECT_ALIGNMENT_MIDDLE:
220                 bitmapRect.y = (bitmapRect.height - __pBitmap->GetHeight()) / 2;
221                 break;
222
223         case TEXT_OBJECT_ALIGNMENT_BOTTOM:
224                 bitmapRect.y = (bitmapRect.height - __pBitmap->GetHeight());
225                 break;
226         }
227
228         switch (__align & TEXT_ALIGNMASK_HORIZ)
229         {
230         case TEXT_OBJECT_ALIGNMENT_LEFT:
231                 // fall through
232         default:
233                 break;
234
235         case TEXT_OBJECT_ALIGNMENT_CENTER:
236                 bitmapRect.x = (bitmapRect.width - __pBitmap->GetWidth()) / 2;
237                 break;
238
239         case TEXT_OBJECT_ALIGNMENT_RIGHT:
240                 bitmapRect.x = (bitmapRect.width - __pBitmap->GetWidth());
241                 break;
242         }
243
244         r = pCanvas->DrawBitmap(bitmapRect, *GetBitmapEx(*_BitmapImpl::GetInstance(*__pBitmap)));
245         SysTryReturn(NID_GRP
246                 , r == E_SUCCESS
247                 , r, r, "[%s] Propagating.", GetErrorMessage(r));
248
249         if (_hasFocus)
250         {
251                 r = pCanvas->DrawRectangle(_Util::Convert<Rectangle, _Util::Rectangle<double> >(bitmapRect));
252                 SysTryReturn(NID_GRP
253                         , r == E_SUCCESS
254                         , r, r, "[%s] Propagating.", GetErrorMessage(r));
255         }
256
257         return E_SUCCESS;
258 }
259
260 TextElement*
261 TextImage::CloneN(TextComponentInfoValueType type, unsigned int value)
262 {
263         SysTryReturn(NID_GRP, __pBitmap, null, E_SYSTEM, "[E_SYSTEM] Failed to clone image.");
264
265         TextImage* pImageText = null;
266
267         pImageText = new (std::nothrow) TextImage(*__pBitmap, TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL, &__rect, __align);
268         SysTryReturn(NID_GRP
269                 , pImageText
270                 , null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
271
272         TextElement::CopyMembers(pImageText);
273
274         pImageText->SetValue(type, value);
275
276         return pImageText;
277 }
278
279 TextElement*
280 TextImage::CopyN(int startTextIndex, int textLength)
281 {
282         SysTryReturn(NID_GRP, __pBitmap, null, E_SYSTEM, "[E_SYSTEM] Failed to copy image.");
283
284         if (startTextIndex != 0)
285         {
286                 return null;
287         }
288
289         if (textLength != 1)
290         {
291                 textLength = 1;
292         }
293
294         TextImage* pImageText = null;
295
296         pImageText = new (std::nothrow) TextImage(*__pBitmap, _sourceType, &__rect, __align);
297         SysTryReturn(NID_GRP
298                 , pImageText
299                 , null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
300
301         return pImageText;
302 }
303
304 unsigned int
305 TextImage::GetValue(TextComponentInfoValueType type) const
306 {
307         switch (type)
308         {
309         case SET_IMAGE_ALIGN:
310                 // fall through
311                 return (unsigned int) __align;
312
313         case SET_IMAGE_RECT:
314                 // fall through
315                 return (unsigned int) &__rect;
316
317         case SET_ALTERNATE_LOOK:
318                 // fall through
319                 return (unsigned int) _isAlternateLookEnabled;
320
321         case SET_TEXT_OFFSET:
322                 break;
323
324         case SET_ALTERNATIVE_FGCOLOR:
325                 break;
326
327         default:
328                 break;
329         }
330
331         return 0;
332 }
333
334 result
335 TextImage::SetValue(TextComponentInfoValueType type, unsigned int value)
336 {
337         switch (type)
338         {
339         case SET_IMAGE_ALIGN:
340                 __align = (TextObjectAlignment) value;
341                 break;
342
343         case SET_IMAGE_RECT:
344                 break;
345
346         case SET_TEXT_OFFSET:
347                 break;
348
349         case SET_ALTERNATE_LOOK:
350                 _isAlternateLookEnabled = (bool) value;
351                 break;
352
353         case SET_ALTERNATIVE_FGCOLOR:
354                 break;
355
356         default:
357                 break;
358         }
359
360         return E_SUCCESS;
361 }
362
363 result
364 TextImage::SetBounds(const Rectangle& rect)
365 {
366         __rect = rect;
367         __rect.x = 0;
368         __rect.y = 0;
369
370         return E_SUCCESS;
371 }
372
373 result
374 TextImage::SetAlignment(TextObjectAlignment alignment)
375 {
376         SysTryReturn(NID_GRP
377                 , TEXT_OBJECT_ALIGNMENT_LEFT <= alignment && alignment <= TEXT_OBJECT_ALIGNMENT_BOTTOM
378                 , E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
379
380         __align = alignment;
381
382         return E_SUCCESS;
383 }
384
385 Rectangle
386 TextImage::GetBounds(void) const
387 {
388         return __rect;
389 }
390
391 TextObjectAlignment
392 TextImage::GetAlignment(void) const
393 {
394         return __align;
395 }
396
397 const Bitmap*
398 TextImage::GetBitmap(void) const
399 {
400         return __pBitmap;
401 }
402
403 void
404 TextImage::SetTextLength(int length)
405 {
406         return;
407 }
408
409 int
410 TextImage::GetBaseline(void) const
411 {
412         return 0;
413 }
414
415 }}} // Tizen::Graphics::_Text