setupapi: Fix resolving target path when given a section. Default to the system direc...
[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     return TRUE;
69 }
70
71 /*****************************************************
72  *      GdiplusStartup [GDIPLUS.@]
73  */
74 Status WINAPI GdiplusStartup(ULONG_PTR *token, const struct GdiplusStartupInput *input,
75                              struct GdiplusStartupOutput *output)
76 {
77     if(!token || !input)
78         return InvalidParameter;
79
80     TRACE("%p %p %p\n", token, input, output);
81     TRACE("GdiplusStartupInput %d %p %d %d\n", input->GdiplusVersion,
82           input->DebugEventCallback, input->SuppressBackgroundThread,
83           input->SuppressExternalCodecs);
84
85     if(input->GdiplusVersion != 1)
86         return UnsupportedGdiplusVersion;
87
88     if(input->SuppressBackgroundThread){
89         if(!output)
90             return InvalidParameter;
91
92         output->NotificationHook = NotificationHook;
93         output->NotificationUnhook = NotificationUnhook;
94     }
95
96     *token = 0xdeadbeef;
97
98     /* FIXME: DebugEventCallback ignored */
99
100     return Ok;
101 }
102
103 GpStatus WINAPI GdiplusNotificationHook(ULONG_PTR *token)
104 {
105     FIXME("%p\n", token);
106     return NotificationHook(token);
107 }
108
109 void WINAPI GdiplusNotificationUnhook(ULONG_PTR token)
110 {
111     FIXME("%ld\n", token);
112     NotificationUnhook(token);
113 }
114
115 /*****************************************************
116  *      GdiplusShutdown [GDIPLUS.@]
117  */
118 void WINAPI GdiplusShutdown(ULONG_PTR token)
119 {
120     /* FIXME: no object tracking */
121 }
122
123 /*****************************************************
124  *      GdipAlloc [GDIPLUS.@]
125  */
126 void* WINGDIPAPI GdipAlloc(SIZE_T size)
127 {
128     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
129 }
130
131 /*****************************************************
132  *      GdipFree [GDIPLUS.@]
133  */
134 void WINGDIPAPI GdipFree(void* ptr)
135 {
136     HeapFree(GetProcessHeap(), 0, ptr);
137 }
138
139 /* Calculates the bezier points needed to fill in the arc portion starting at
140  * angle start and ending at end.  These two angles should be no more than 90
141  * degrees from each other.  x1, y1, x2, y2 describes the bounding box (upper
142  * left and width and height).  Angles must be in radians. write_first indicates
143  * that the first bezier point should be written out (usually this is false).
144  * pt is the array of GpPointFs that gets written to.
145  **/
146 static void add_arc_part(GpPointF * pt, REAL x1, REAL y1, REAL x2, REAL y2,
147     REAL start, REAL end, BOOL write_first)
148 {
149     REAL center_x, center_y, rad_x, rad_y, cos_start, cos_end,
150         sin_start, sin_end, a, half;
151     INT i;
152
153     rad_x = x2 / 2.0;
154     rad_y = y2 / 2.0;
155     center_x = x1 + rad_x;
156     center_y = y1 + rad_y;
157
158     cos_start = cos(start);
159     cos_end = cos(end);
160     sin_start = sin(start);
161     sin_end = sin(end);
162
163     half = (end - start) / 2.0;
164     a = 4.0 / 3.0 * (1 - cos(half)) / sin(half);
165
166     if(write_first){
167         pt[0].X = cos_start;
168         pt[0].Y = sin_start;
169     }
170     pt[1].X = cos_start - a * sin_start;
171     pt[1].Y = sin_start + a * cos_start;
172
173     pt[3].X = cos_end;
174     pt[3].Y = sin_end;
175     pt[2].X = cos_end + a * sin_end;
176     pt[2].Y = sin_end - a * cos_end;
177
178     /* expand the points back from the unit circle to the ellipse */
179     for(i = (write_first ? 0 : 1); i < 4; i ++){
180         pt[i].X = pt[i].X * rad_x + center_x;
181         pt[i].Y = pt[i].Y * rad_y + center_y;
182     }
183 }
184
185 /* We plot the curve as if it is on a circle then stretch the points.  This
186  * adjusts the angles so that when we stretch the points they will end in the
187  * right place. This is only complicated because atan and atan2 do not behave
188  * conveniently. */
189 static void unstretch_angle(REAL * angle, REAL rad_x, REAL rad_y)
190 {
191     REAL stretched;
192     INT revs_off;
193
194     *angle = deg2rad(*angle);
195
196     if(fabs(cos(*angle)) < 0.00001 || fabs(sin(*angle)) < 0.00001)
197         return;
198
199     stretched = gdiplus_atan2(sin(*angle) / fabs(rad_y), cos(*angle) / fabs(rad_x));
200     revs_off = roundr(*angle / (2.0 * M_PI)) - roundr(stretched / (2.0 * M_PI));
201     stretched += ((REAL)revs_off) * M_PI * 2.0;
202     *angle = stretched;
203 }
204
205 /* Stores the bezier points that correspond to the arc in points.  If points is
206  * null, just return the number of points needed to represent the arc. */
207 INT arc2polybezier(GpPointF * points, REAL x1, REAL y1, REAL x2, REAL y2,
208     REAL startAngle, REAL sweepAngle)
209 {
210     INT i, count;
211     REAL end_angle, start_angle, endAngle;
212
213     endAngle = startAngle + sweepAngle;
214     unstretch_angle(&startAngle, x2 / 2.0, y2 / 2.0);
215     unstretch_angle(&endAngle, x2 / 2.0, y2 / 2.0);
216
217     count = ceilf(fabs(endAngle - startAngle) / M_PI_2) * 3 + 1;
218     /* don't make more than a full circle */
219     count = min(MAX_ARC_PTS, count);
220
221     if(count == 1)
222         return 0;
223     if(!points)
224         return count;
225
226     /* start_angle and end_angle are the iterative variables */
227     start_angle = startAngle;
228
229     for(i = 0; i < count - 1; i += 3){
230         /* check if we've overshot the end angle */
231         if( sweepAngle > 0.0 )
232             end_angle = min(start_angle + M_PI_2, endAngle);
233         else
234             end_angle = max(start_angle - M_PI_2, endAngle);
235
236         add_arc_part(&points[i], x1, y1, x2, y2, start_angle, end_angle, i == 0);
237
238         start_angle += M_PI_2 * (sweepAngle < 0.0 ? -1.0 : 1.0);
239     }
240
241     return count;
242 }
243
244 COLORREF ARGB2COLORREF(ARGB color)
245 {
246     /*
247     Packing of these color structures:
248     COLORREF:   00bbggrr
249     ARGB:       aarrggbb
250     FIXME:doesn't handle alpha channel
251     */
252     return ((color & 0x0000ff) << 16) +
253            (color & 0x00ff00) +
254            ((color & 0xff0000) >> 16);
255 }
256
257 /* Like atan2, but puts angle in correct quadrant if dx is 0. */
258 REAL gdiplus_atan2(REAL dy, REAL dx)
259 {
260     if((dx == 0.0) && (dy != 0.0))
261         return dy > 0.0 ? M_PI_2 : -M_PI_2;
262
263     return atan2(dy, dx);
264 }
265
266 GpStatus hresult_to_status(HRESULT res)
267 {
268     switch(res){
269         case S_OK:
270             return Ok;
271         case E_OUTOFMEMORY:
272             return OutOfMemory;
273         case E_INVALIDARG:
274             return InvalidParameter;
275         default:
276             return GenericError;
277     }
278 }
279
280 /* converts a given unit to its value in pixels */
281 REAL convert_unit(HDC hdc, GpUnit unit)
282 {
283     switch(unit)
284     {
285         case UnitInch:
286             return (REAL) GetDeviceCaps(hdc, LOGPIXELSX);
287         case UnitPoint:
288             return ((REAL)GetDeviceCaps(hdc, LOGPIXELSX)) / 72.0;
289         case UnitDocument:
290             return ((REAL)GetDeviceCaps(hdc, LOGPIXELSX)) / 300.0;
291         case UnitMillimeter:
292             return ((REAL)GetDeviceCaps(hdc, LOGPIXELSX)) / 25.4;
293         case UnitWorld:
294             ERR("cannot convert UnitWorld\n");
295             return 0.0;
296         case UnitPixel:
297         case UnitDisplay:
298         default:
299             return 1.0;
300     }
301 }
302
303 /* Calculates Bezier points from cardinal spline points. */
304 void calc_curve_bezier(CONST GpPointF *pts, REAL tension, REAL *x1,
305     REAL *y1, REAL *x2, REAL *y2)
306 {
307     REAL xdiff, ydiff;
308
309     /* calculate tangent */
310     xdiff = pts[2].X - pts[0].X;
311     ydiff = pts[2].Y - pts[0].Y;
312
313     /* apply tangent to get control points */
314     *x1 = pts[1].X - tension * xdiff;
315     *y1 = pts[1].Y - tension * ydiff;
316     *x2 = pts[1].X + tension * xdiff;
317     *y2 = pts[1].Y + tension * ydiff;
318 }
319
320 /* Calculates Bezier points from cardinal spline endpoints. */
321 void calc_curve_bezier_endp(REAL xend, REAL yend, REAL xadj, REAL yadj,
322     REAL tension, REAL *x, REAL *y)
323 {
324     /* tangent at endpoints is the line from the endpoint to the adjacent point */
325     *x = roundr(tension * (xadj - xend) + xend);
326     *y = roundr(tension * (yadj - yend) + yend);
327 }
328
329 /* make sure path has enough space for len more points */
330 BOOL lengthen_path(GpPath *path, INT len)
331 {
332     /* initial allocation */
333     if(path->datalen == 0){
334         path->datalen = len * 2;
335
336         path->pathdata.Points = GdipAlloc(path->datalen * sizeof(PointF));
337         if(!path->pathdata.Points)   return FALSE;
338
339         path->pathdata.Types = GdipAlloc(path->datalen);
340         if(!path->pathdata.Types){
341             GdipFree(path->pathdata.Points);
342             return FALSE;
343         }
344     }
345     /* reallocation, double size of arrays */
346     else if(path->datalen - path->pathdata.Count < len){
347         while(path->datalen - path->pathdata.Count < len)
348             path->datalen *= 2;
349
350         path->pathdata.Points = HeapReAlloc(GetProcessHeap(), 0,
351             path->pathdata.Points, path->datalen * sizeof(PointF));
352         if(!path->pathdata.Points)  return FALSE;
353
354         path->pathdata.Types = HeapReAlloc(GetProcessHeap(), 0,
355             path->pathdata.Types, path->datalen);
356         if(!path->pathdata.Types)   return FALSE;
357     }
358
359     return TRUE;
360 }
361
362 /* recursive deletion of GpRegion nodes */
363 inline void delete_element(region_element* element)
364 {
365     switch(element->type)
366     {
367         case RegionDataRect:
368             break;
369         case RegionDataPath:
370             GdipDeletePath(element->elementdata.pathdata.path);
371             break;
372         case RegionDataEmptyRect:
373         case RegionDataInfiniteRect:
374             break;
375         default:
376             delete_element(element->elementdata.combine.left);
377             delete_element(element->elementdata.combine.right);
378             GdipFree(element->elementdata.combine.left);
379             GdipFree(element->elementdata.combine.right);
380             break;
381     }
382 }