msctf: Use FAILED instead of !SUCCEDED.
[wine] / dlls / gdiplus / gdiplus.c
1 /*
2  * Copyright (C) 2007 Google (Evan Stade)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdarg.h>
20 #include <math.h>
21
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winerror.h"
25 #include "wine/debug.h"
26 #include "wingdi.h"
27
28 #include "objbase.h"
29
30 #include "winreg.h"
31 #include "shlwapi.h"
32
33 #include "gdiplus.h"
34 #include "gdiplus_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
37
38 static Status WINAPI NotificationHook(ULONG_PTR *token)
39 {
40     TRACE("%p\n", token);
41     if(!token)
42         return InvalidParameter;
43
44     return Ok;
45 }
46
47 static void WINAPI NotificationUnhook(ULONG_PTR token)
48 {
49     TRACE("%ld\n", token);
50 }
51
52 /*****************************************************
53  *      DllMain
54  */
55 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
56 {
57     TRACE("(%p, %d, %p)\n", hinst, reason, reserved);
58
59     switch(reason)
60     {
61     case DLL_WINE_PREATTACH:
62         return FALSE;  /* prefer native version */
63
64     case DLL_PROCESS_ATTACH:
65         DisableThreadLibraryCalls( hinst );
66         break;
67
68     case DLL_PROCESS_DETACH:
69         free_installed_fonts();
70         break;
71     }
72     return TRUE;
73 }
74
75 /*****************************************************
76  *      GdiplusStartup [GDIPLUS.@]
77  */
78 Status WINAPI GdiplusStartup(ULONG_PTR *token, const struct GdiplusStartupInput *input,
79                              struct GdiplusStartupOutput *output)
80 {
81     if(!token || !input)
82         return InvalidParameter;
83
84     TRACE("%p %p %p\n", token, input, output);
85     TRACE("GdiplusStartupInput %d %p %d %d\n", input->GdiplusVersion,
86           input->DebugEventCallback, input->SuppressBackgroundThread,
87           input->SuppressExternalCodecs);
88
89     if(input->GdiplusVersion != 1)
90         return UnsupportedGdiplusVersion;
91
92     if(input->SuppressBackgroundThread){
93         if(!output)
94             return InvalidParameter;
95
96         output->NotificationHook = NotificationHook;
97         output->NotificationUnhook = NotificationUnhook;
98     }
99
100     *token = 0xdeadbeef;
101
102     /* FIXME: DebugEventCallback ignored */
103
104     return Ok;
105 }
106
107 GpStatus WINAPI GdiplusNotificationHook(ULONG_PTR *token)
108 {
109     FIXME("%p\n", token);
110     return NotificationHook(token);
111 }
112
113 void WINAPI GdiplusNotificationUnhook(ULONG_PTR token)
114 {
115     FIXME("%ld\n", token);
116     NotificationUnhook(token);
117 }
118
119 /*****************************************************
120  *      GdiplusShutdown [GDIPLUS.@]
121  */
122 void WINAPI GdiplusShutdown(ULONG_PTR token)
123 {
124     /* FIXME: no object tracking */
125 }
126
127 /*****************************************************
128  *      GdipAlloc [GDIPLUS.@]
129  */
130 void* WINGDIPAPI GdipAlloc(SIZE_T size)
131 {
132     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
133 }
134
135 /*****************************************************
136  *      GdipFree [GDIPLUS.@]
137  */
138 void WINGDIPAPI GdipFree(void* ptr)
139 {
140     HeapFree(GetProcessHeap(), 0, ptr);
141 }
142
143 /* Calculates the bezier points needed to fill in the arc portion starting at
144  * angle start and ending at end.  These two angles should be no more than 90
145  * degrees from each other.  x1, y1, x2, y2 describes the bounding box (upper
146  * left and width and height).  Angles must be in radians. write_first indicates
147  * that the first bezier point should be written out (usually this is false).
148  * pt is the array of GpPointFs that gets written to.
149  **/
150 static void add_arc_part(GpPointF * pt, REAL x1, REAL y1, REAL x2, REAL y2,
151     REAL start, REAL end, BOOL write_first)
152 {
153     REAL center_x, center_y, rad_x, rad_y, cos_start, cos_end,
154         sin_start, sin_end, a, half;
155     INT i;
156
157     rad_x = x2 / 2.0;
158     rad_y = y2 / 2.0;
159     center_x = x1 + rad_x;
160     center_y = y1 + rad_y;
161
162     cos_start = cos(start);
163     cos_end = cos(end);
164     sin_start = sin(start);
165     sin_end = sin(end);
166
167     half = (end - start) / 2.0;
168     a = 4.0 / 3.0 * (1 - cos(half)) / sin(half);
169
170     if(write_first){
171         pt[0].X = cos_start;
172         pt[0].Y = sin_start;
173     }
174     pt[1].X = cos_start - a * sin_start;
175     pt[1].Y = sin_start + a * cos_start;
176
177     pt[3].X = cos_end;
178     pt[3].Y = sin_end;
179     pt[2].X = cos_end + a * sin_end;
180     pt[2].Y = sin_end - a * cos_end;
181
182     /* expand the points back from the unit circle to the ellipse */
183     for(i = (write_first ? 0 : 1); i < 4; i ++){
184         pt[i].X = pt[i].X * rad_x + center_x;
185         pt[i].Y = pt[i].Y * rad_y + center_y;
186     }
187 }
188
189 /* We plot the curve as if it is on a circle then stretch the points.  This
190  * adjusts the angles so that when we stretch the points they will end in the
191  * right place. This is only complicated because atan and atan2 do not behave
192  * conveniently. */
193 static void unstretch_angle(REAL * angle, REAL rad_x, REAL rad_y)
194 {
195     REAL stretched;
196     INT revs_off;
197
198     *angle = deg2rad(*angle);
199
200     if(fabs(cos(*angle)) < 0.00001 || fabs(sin(*angle)) < 0.00001)
201         return;
202
203     stretched = gdiplus_atan2(sin(*angle) / fabs(rad_y), cos(*angle) / fabs(rad_x));
204     revs_off = roundr(*angle / (2.0 * M_PI)) - roundr(stretched / (2.0 * M_PI));
205     stretched += ((REAL)revs_off) * M_PI * 2.0;
206     *angle = stretched;
207 }
208
209 /* Stores the bezier points that correspond to the arc in points.  If points is
210  * null, just return the number of points needed to represent the arc. */
211 INT arc2polybezier(GpPointF * points, REAL x1, REAL y1, REAL x2, REAL y2,
212     REAL startAngle, REAL sweepAngle)
213 {
214     INT i, count;
215     REAL end_angle, start_angle, endAngle;
216
217     endAngle = startAngle + sweepAngle;
218     unstretch_angle(&startAngle, x2 / 2.0, y2 / 2.0);
219     unstretch_angle(&endAngle, x2 / 2.0, y2 / 2.0);
220
221     count = ceilf(fabs(endAngle - startAngle) / M_PI_2) * 3 + 1;
222     /* don't make more than a full circle */
223     count = min(MAX_ARC_PTS, count);
224
225     if(count == 1)
226         return 0;
227     if(!points)
228         return count;
229
230     /* start_angle and end_angle are the iterative variables */
231     start_angle = startAngle;
232
233     for(i = 0; i < count - 1; i += 3){
234         /* check if we've overshot the end angle */
235         if( sweepAngle > 0.0 )
236             end_angle = min(start_angle + M_PI_2, endAngle);
237         else
238             end_angle = max(start_angle - M_PI_2, endAngle);
239
240         add_arc_part(&points[i], x1, y1, x2, y2, start_angle, end_angle, i == 0);
241
242         start_angle += M_PI_2 * (sweepAngle < 0.0 ? -1.0 : 1.0);
243     }
244
245     return count;
246 }
247
248 COLORREF ARGB2COLORREF(ARGB color)
249 {
250     /*
251     Packing of these color structures:
252     COLORREF:   00bbggrr
253     ARGB:       aarrggbb
254     FIXME:doesn't handle alpha channel
255     */
256     return ((color & 0x0000ff) << 16) +
257            (color & 0x00ff00) +
258            ((color & 0xff0000) >> 16);
259 }
260
261 /* Like atan2, but puts angle in correct quadrant if dx is 0. */
262 REAL gdiplus_atan2(REAL dy, REAL dx)
263 {
264     if((dx == 0.0) && (dy != 0.0))
265         return dy > 0.0 ? M_PI_2 : -M_PI_2;
266
267     return atan2(dy, dx);
268 }
269
270 GpStatus hresult_to_status(HRESULT res)
271 {
272     switch(res){
273         case S_OK:
274             return Ok;
275         case E_OUTOFMEMORY:
276             return OutOfMemory;
277         case E_INVALIDARG:
278             return InvalidParameter;
279         default:
280             return GenericError;
281     }
282 }
283
284 /* converts a given unit to its value in pixels */
285 REAL convert_unit(HDC hdc, GpUnit unit)
286 {
287     switch(unit)
288     {
289         case UnitInch:
290             return (REAL) GetDeviceCaps(hdc, LOGPIXELSX);
291         case UnitPoint:
292             return ((REAL)GetDeviceCaps(hdc, LOGPIXELSX)) / 72.0;
293         case UnitDocument:
294             return ((REAL)GetDeviceCaps(hdc, LOGPIXELSX)) / 300.0;
295         case UnitMillimeter:
296             return ((REAL)GetDeviceCaps(hdc, LOGPIXELSX)) / 25.4;
297         case UnitWorld:
298             ERR("cannot convert UnitWorld\n");
299             return 0.0;
300         case UnitPixel:
301         case UnitDisplay:
302         default:
303             return 1.0;
304     }
305 }
306
307 /* Calculates Bezier points from cardinal spline points. */
308 void calc_curve_bezier(CONST GpPointF *pts, REAL tension, REAL *x1,
309     REAL *y1, REAL *x2, REAL *y2)
310 {
311     REAL xdiff, ydiff;
312
313     /* calculate tangent */
314     xdiff = pts[2].X - pts[0].X;
315     ydiff = pts[2].Y - pts[0].Y;
316
317     /* apply tangent to get control points */
318     *x1 = pts[1].X - tension * xdiff;
319     *y1 = pts[1].Y - tension * ydiff;
320     *x2 = pts[1].X + tension * xdiff;
321     *y2 = pts[1].Y + tension * ydiff;
322 }
323
324 /* Calculates Bezier points from cardinal spline endpoints. */
325 void calc_curve_bezier_endp(REAL xend, REAL yend, REAL xadj, REAL yadj,
326     REAL tension, REAL *x, REAL *y)
327 {
328     /* tangent at endpoints is the line from the endpoint to the adjacent point */
329     *x = roundr(tension * (xadj - xend) + xend);
330     *y = roundr(tension * (yadj - yend) + yend);
331 }
332
333 /* make sure path has enough space for len more points */
334 BOOL lengthen_path(GpPath *path, INT len)
335 {
336     /* initial allocation */
337     if(path->datalen == 0){
338         path->datalen = len * 2;
339
340         path->pathdata.Points = GdipAlloc(path->datalen * sizeof(PointF));
341         if(!path->pathdata.Points)   return FALSE;
342
343         path->pathdata.Types = GdipAlloc(path->datalen);
344         if(!path->pathdata.Types){
345             GdipFree(path->pathdata.Points);
346             return FALSE;
347         }
348     }
349     /* reallocation, double size of arrays */
350     else if(path->datalen - path->pathdata.Count < len){
351         while(path->datalen - path->pathdata.Count < len)
352             path->datalen *= 2;
353
354         path->pathdata.Points = HeapReAlloc(GetProcessHeap(), 0,
355             path->pathdata.Points, path->datalen * sizeof(PointF));
356         if(!path->pathdata.Points)  return FALSE;
357
358         path->pathdata.Types = HeapReAlloc(GetProcessHeap(), 0,
359             path->pathdata.Types, path->datalen);
360         if(!path->pathdata.Types)   return FALSE;
361     }
362
363     return TRUE;
364 }
365
366 /* recursive deletion of GpRegion nodes */
367 inline void delete_element(region_element* element)
368 {
369     switch(element->type)
370     {
371         case RegionDataRect:
372             break;
373         case RegionDataPath:
374             GdipDeletePath(element->elementdata.pathdata.path);
375             break;
376         case RegionDataEmptyRect:
377         case RegionDataInfiniteRect:
378             break;
379         default:
380             delete_element(element->elementdata.combine.left);
381             delete_element(element->elementdata.combine.right);
382             GdipFree(element->elementdata.combine.left);
383             GdipFree(element->elementdata.combine.right);
384             break;
385     }
386 }
387
388 const char *debugstr_rectf(CONST RectF* rc)
389 {
390     if (!rc) return "(null)";
391     return wine_dbg_sprintf("(%0.2f,%0.2f,%0.2f,%0.2f)", rc->X, rc->Y, rc->Width, rc->Height);
392 }