Tizen 2.1 base
[framework/osp/uifw.git] / src / graphics / util / FGrp_Util.h
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_Util.h
20  * @brief       This is the header file for internal _Util namespace.
21  *
22  */
23
24 #ifndef _FGRP_INTERNAL_UTIL_H_
25 #define _FGRP_INTERNAL_UTIL_H_
26
27 #include <new>
28
29 #include <FGrpPoint.h>
30 #include <FGrpFloatPoint.h>
31
32 #include <FGrpDimension.h>
33 #include <FGrpFloatDimension.h>
34
35 #include <FGrpRectangle.h>
36 #include <FGrpFloatRectangle.h>
37
38 #include <FGrpBufferInfo.h>
39 #include <FGrpBitmap.h>
40 #include <FGrpCanvas.h>
41 #include <FGrpFont.h>
42 #include <FGrpEnrichedText.h>
43
44 #include "FGrp_BitmapImpl.h"
45 #include "FGrp_CanvasImpl.h"
46 #include "FGrp_FontImpl.h"
47 #include "FGrp_EnrichedTextImpl.h"
48
49 #include "FGrp_UtilTemplate.h"
50 #include "FGrp_UtilType.h"
51
52
53 namespace Tizen { namespace Graphics
54 {
55 class Point;
56
57 class _Bitmap;
58 class _Canvas;
59
60 namespace _Util
61 {
62
63 result Validate(const ::Tizen::Graphics::Rectangle& rtSrc, const ::Tizen::Graphics::Rectangle& rtDest);
64 result Validate(const ::Tizen::Graphics::Point& ptSrc, const ::Tizen::Graphics::Rectangle& rtDest);
65
66 template <typename T>
67 result ValidateT(const Rectangle<T>& rtSrc, const Rectangle<T>& rtDest)
68 {
69         // check 1. is width/height less or equal than 0?
70         if (rtSrc.w <= 0 || rtSrc.h <= 0 || rtDest.w <= 0 || rtDest.h <= 0)
71         {
72                 SysTryReturnResult(NID_GRP, 0, E_OUT_OF_RANGE, "The argument is out of range. (src(w:%d,h:%d), dst(w:%d,h:%d)).",
73                                                   rtSrc.w, rtSrc.h, rtDest.w, rtDest.h);
74         }
75
76         // check 2.     is src exiting outside of dest entirely?
77         if (rtSrc.x > rtDest.x + rtDest.w - 1 || rtSrc.x + rtSrc.w - 1 < rtDest.x)
78         {
79                 SysTryReturnResult(NID_GRP, 0, E_OUT_OF_RANGE,
80                                                   "The argument is out of range. (src(x:%d,y:%d,w:%d,h:%d), dst(x:%d,y:%d,w:%d,h:%d)).",
81                                                   rtSrc.x, rtSrc.y, rtSrc.w, rtSrc.h, rtDest.x, rtDest.y, rtDest.w, rtDest.h);
82         }
83
84         if (rtSrc.y > rtDest.y + rtDest.h - 1 || rtSrc.y + rtSrc.h - 1 < rtDest.y)
85         {
86                 SysTryReturnResult(NID_GRP, 0, E_OUT_OF_RANGE,
87                                                   "The argument is out of range. (src(x:%d,y:%d,w:%d,h:%d), dst(x:%d,y:%d,w:%d,h:%d)).",
88                                                   rtSrc.x, rtSrc.y, rtSrc.w, rtSrc.h, rtDest.x, rtDest.y, rtDest.w, rtDest.h);
89         }
90
91         return E_SUCCESS;
92 }
93
94 template <typename T>
95 result ValidateT(const Point<T>& ptSrc, const Rectangle<T>& rtDest)
96 {
97         SysTryReturnResult(NID_GRP, rtDest.x <= ptSrc.x &&
98                                           ptSrc.x < rtDest.x + rtDest.w &&
99                                           rtDest.y <= ptSrc.y &&
100                                           ptSrc.y < rtDest.y + rtDest.h,
101                                           E_OUT_OF_RANGE, "The argument is out of range. (src(x:%d,y:%d), dst(x:%d,y:%d,w:%d,h:%d)).\n",
102                                           ptSrc.x, ptSrc.y, rtDest.x, rtDest.y, rtDest.w, rtDest.h);
103
104         return E_SUCCESS;
105 }
106
107 bool IntersectRect(Rectangle<int>& outRect, const Rectangle<int>& srcRect1, const Rectangle<int>& srcRect2);
108 bool IntersectRect(Rectangle<float>& outRect, const Rectangle<float>& srcRect1, const Rectangle<float>& srcRect2);
109
110 ////////////////////////////////////////////////////////////////////////////
111 // LockManager class
112
113 class LockManager
114 {
115 public:
116         LockManager(const class _Bitmap& bitmap);
117         LockManager(const class _Canvas& canvas);
118         LockManager(const class _BitmapImpl& bitmapImpl);
119         LockManager(const class _CanvasImpl& canvasImpl);
120         ~LockManager();
121
122         bool IsValid(void) const;
123         result GetResult(void) const;
124         const BufferInfo& GetBufferInfo(void) const;
125
126 private:
127         result __result;
128
129         _Bitmap* __pBitmap;
130         _Canvas* __pCanvas;
131         _BitmapImpl* __pBitmapImpl;
132         _CanvasImpl* __pCanvasImpl;
133         BufferInfo __bufferInfo;
134 }; // LockManager
135
136 ////////////////////////////////////////////////////////////////////////////
137 // ScalarHolder structure
138
139 struct ScalarHolder
140 {
141         enum
142         {
143                 AS_INTEGER,
144                 AS_DOUBLE,
145         } applied;
146
147         int asInteger;
148         double asDouble;
149
150         ScalarHolder(int initValue)
151                 : applied(AS_INTEGER)
152                 , asInteger(initValue)
153                 , asDouble(double(initValue))
154         {
155         }
156
157         ScalarHolder(double initValue)
158                 : applied(AS_DOUBLE)
159                 , asInteger(int(initValue + 0.5))
160                 , asDouble(initValue)
161         {
162         }
163
164         int operator=(int value)
165         {
166                 applied = AS_INTEGER;
167                 asInteger = value;
168                 asDouble = double(value);
169
170                 return value;
171         }
172
173         double operator=(double value)
174         {
175                 applied = AS_DOUBLE;
176                 asInteger = int(value + 0.5);
177                 asDouble = value;
178
179                 return value;
180         }
181 }; // ScalarHolder
182
183 ////////////////////////////////////////////////////////////////////////////
184 // Utility function
185
186 result GetPatchList(AccumList <Pair <Rectangle<int>, Rectangle<int> > >& outList, const ::Tizen::Graphics::Rectangle& rect, const _Bitmap& bitmap);
187
188 ////////////////////////////////////////////////////////////////////////////
189 // Validity check
190
191 template<class T>
192 inline bool
193 CheckValidity(T* pInstance)
194 {
195         return (pInstance != null);
196 }
197
198 template<>
199 inline bool
200 CheckValidity(const Bitmap* pInstance)
201 {
202         return (pInstance && _BitmapImpl::GetInstance(*pInstance));
203 }
204
205 template<>
206 inline bool
207 CheckValidity(Bitmap* pInstance)
208 {
209         return (pInstance && _BitmapImpl::GetInstance(*pInstance));
210 }
211
212 template<>
213 inline bool
214 CheckValidity(const Canvas* pInstance)
215 {
216         return (pInstance && _CanvasImpl::GetInstance(*pInstance));
217 }
218
219 template<>
220 inline bool
221 CheckValidity(Canvas* pInstance)
222 {
223         return (pInstance && _CanvasImpl::GetInstance(*pInstance));
224 }
225
226 template<>
227 inline bool
228 CheckValidity(const Font* pInstance)
229 {
230         return (pInstance && _FontImpl::GetInstance(*pInstance));
231 }
232
233 template<>
234 inline bool
235 CheckValidity(Font* pInstance)
236 {
237         return (pInstance && _FontImpl::GetInstance(*pInstance));
238 }
239
240 template<>
241 inline bool
242 CheckValidity(const EnrichedText* pInstance)
243 {
244         return (pInstance && _EnrichedTextImpl::GetInstance(*pInstance));
245 }
246
247 template<>
248 inline bool
249 CheckValidity(EnrichedText* pInstance)
250 {
251         return (pInstance && _EnrichedTextImpl::GetInstance(*pInstance));
252 }
253
254 ////////////////////////////////////////////////////////////////////////////
255 // Convertor
256
257 template <typename T1, typename T2>
258 inline T2 Convert(const T1&);
259
260 template <>
261 inline _Util::Point<int> Convert<Tizen::Graphics::Point, _Util::Point<int> >(const Tizen::Graphics::Point& pos)
262 {
263         _Util::Point<int> temp =
264         {
265                 pos.x,
266                 pos.y
267         };
268
269         return temp;
270 }
271
272 template <>
273 inline _Util::Point<double> Convert<Tizen::Graphics::Point, _Util::Point<double> >(const Tizen::Graphics::Point& pos)
274 {
275         _Util::Point<double> temp =
276         {
277                 static_cast<double>(pos.x),
278                 static_cast<double>(pos.y)
279         };
280
281         return temp;
282 }
283
284 template <>
285 inline _Util::Point<double> Convert<Tizen::Graphics::FloatPoint, _Util::Point<double> >(const Tizen::Graphics::FloatPoint& pos)
286 {
287         _Util::Point<double> temp = { pos.x, pos.y };
288
289         return temp;
290 }
291
292 template <>
293 inline _Util::Dimension<double> Convert<Tizen::Graphics::Dimension, _Util::Dimension<double> >(const Tizen::Graphics::Dimension& dim)
294 {
295         _Util::Dimension<double> temp =
296         {
297                 static_cast<double>(dim.width),
298                 static_cast<double>(dim.height)
299         };
300
301         return temp;
302 }
303
304 template <>
305 inline _Util::Dimension<double> Convert<Tizen::Graphics::FloatDimension, _Util::Dimension<double> >(const Tizen::Graphics::FloatDimension& dim)
306 {
307         _Util::Dimension<double> temp = { dim.width, dim.height };
308
309         return temp;
310 }
311
312 template <>
313 inline _Util::Rectangle<double> Convert<Tizen::Graphics::Rectangle, _Util::Rectangle<double> >(const Tizen::Graphics::Rectangle& rect)
314 {
315         _Util::Rectangle<double> temp =
316         {
317                 static_cast<double>(rect.x),
318                 static_cast<double>(rect.y),
319                 static_cast<double>(rect.width),
320                 static_cast<double>(rect.height)
321         };
322
323         return temp;
324 }
325
326 template <>
327 inline _Util::Rectangle<double> Convert<Tizen::Graphics::FloatRectangle, _Util::Rectangle<double> >(const Tizen::Graphics::FloatRectangle& rect)
328 {
329         _Util::Rectangle<double> temp = { rect.x, rect.y, rect.width, rect.height };
330
331         return temp;
332 }
333
334 } // Tizen::Graphics::_Util
335
336 }} // Tizen::Graphics
337
338 #endif // _FGRP_INTERNAL_UTIL_H_