crypt32: Add and use HCRYPTPROV_LEGACY and HCRYPTPROV_OR_NCRYPT_KEY_HANDLE.
[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 "gdiplus.h"
31 #include "gdiplus_private.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
34
35 /*****************************************************
36  *      DllMain
37  */
38 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
39 {
40     TRACE("(%p, %d, %p)\n", hinst, reason, reserved);
41
42     switch(reason)
43     {
44     case DLL_WINE_PREATTACH:
45         return FALSE;  /* prefer native version */
46
47     case DLL_PROCESS_ATTACH:
48         DisableThreadLibraryCalls( hinst );
49         break;
50     }
51     return TRUE;
52 }
53
54 /*****************************************************
55  *      GdiplusStartup [GDIPLUS.@]
56  */
57 Status WINAPI GdiplusStartup(ULONG_PTR *token, const struct GdiplusStartupInput *input,
58                              struct GdiplusStartupOutput *output)
59 {
60     if(!token)
61         return InvalidParameter;
62
63     if(input->GdiplusVersion != 1) {
64         return UnsupportedGdiplusVersion;
65     } else if ((input->DebugEventCallback) ||
66         (input->SuppressBackgroundThread) || (input->SuppressExternalCodecs)){
67         FIXME("Unimplemented for non-default GdiplusStartupInput\n");
68         return NotImplemented;
69     } else if(output) {
70         FIXME("Unimplemented for non-null GdiplusStartupOutput\n");
71         return NotImplemented;
72     }
73
74     return Ok;
75 }
76
77 /*****************************************************
78  *      GdiplusShutdown [GDIPLUS.@]
79  */
80 void WINAPI GdiplusShutdown(ULONG_PTR token)
81 {
82     /* FIXME: no object tracking */
83 }
84
85 /*****************************************************
86  *      GdipAlloc [GDIPLUS.@]
87  */
88 void* WINGDIPAPI GdipAlloc(SIZE_T size)
89 {
90     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
91 }
92
93 /*****************************************************
94  *      GdipFree [GDIPLUS.@]
95  */
96 void WINGDIPAPI GdipFree(void* ptr)
97 {
98     HeapFree(GetProcessHeap(), 0, ptr);
99 }
100
101 /* Calculates the bezier points needed to fill in the arc portion starting at
102  * angle start and ending at end.  These two angles should be no more than 90
103  * degrees from each other.  x1, y1, x2, y2 describes the bounding box (upper
104  * left and width and height).  Angles must be in radians. write_first indicates
105  * that the first bezier point should be written out (usually this is false).
106  * pt is the array of GpPointFs that gets written to.
107  **/
108 static void add_arc_part(GpPointF * pt, REAL x1, REAL y1, REAL x2, REAL y2,
109     REAL start, REAL end, BOOL write_first)
110 {
111     REAL center_x, center_y, rad_x, rad_y, cos_start, cos_end,
112         sin_start, sin_end, a, half;
113     INT i;
114
115     rad_x = x2 / 2.0;
116     rad_y = y2 / 2.0;
117     center_x = x1 + rad_x;
118     center_y = y1 + rad_y;
119
120     cos_start = cos(start);
121     cos_end = cos(end);
122     sin_start = sin(start);
123     sin_end = sin(end);
124
125     half = (end - start) / 2.0;
126     a = 4.0 / 3.0 * (1 - cos(half)) / sin(half);
127
128     if(write_first){
129         pt[0].X = cos_start;
130         pt[0].Y = sin_start;
131     }
132     pt[1].X = cos_start - a * sin_start;
133     pt[1].Y = sin_start + a * cos_start;
134
135     pt[3].X = cos_end;
136     pt[3].Y = sin_end;
137     pt[2].X = cos_end + a * sin_end;
138     pt[2].Y = sin_end - a * cos_end;
139
140     /* expand the points back from the unit circle to the ellipse */
141     for(i = (write_first ? 0 : 1); i < 4; i ++){
142         pt[i].X = pt[i].X * rad_x + center_x;
143         pt[i].Y = pt[i].Y * rad_y + center_y;
144     }
145 }
146
147 /* We plot the curve as if it is on a circle then stretch the points.  This
148  * adjusts the angles so that when we stretch the points they will end in the
149  * right place. This is only complicated because atan and atan2 do not behave
150  * conveniently. */
151 static void unstretch_angle(REAL * angle, REAL rad_x, REAL rad_y)
152 {
153     REAL stretched;
154     INT revs_off;
155
156     *angle = deg2rad(*angle);
157
158     if(fabs(cos(*angle)) < 0.00001 || fabs(sin(*angle)) < 0.00001)
159         return;
160
161     stretched = gdiplus_atan2(sin(*angle) / fabs(rad_y), cos(*angle) / fabs(rad_x));
162     revs_off = roundr(*angle / (2.0 * M_PI)) - roundr(stretched / (2.0 * M_PI));
163     stretched += ((REAL)revs_off) * M_PI * 2.0;
164     *angle = stretched;
165 }
166
167 /* Stores the bezier points that correspond to the arc in points.  If points is
168  * null, just return the number of points needed to represent the arc. */
169 INT arc2polybezier(GpPointF * points, REAL x1, REAL y1, REAL x2, REAL y2,
170     REAL startAngle, REAL sweepAngle)
171 {
172     INT i, count;
173     REAL end_angle, start_angle, endAngle;
174
175     endAngle = startAngle + sweepAngle;
176     unstretch_angle(&startAngle, x2 / 2.0, y2 / 2.0);
177     unstretch_angle(&endAngle, x2 / 2.0, y2 / 2.0);
178
179     count = ceilf(fabs(endAngle - startAngle) / M_PI_2) * 3 + 1;
180     /* don't make more than a full circle */
181     count = min(MAX_ARC_PTS, count);
182
183     if(count == 1)
184         return 0;
185     if(!points)
186         return count;
187
188     /* start_angle and end_angle are the iterative variables */
189     start_angle = startAngle;
190
191     for(i = 0; i < count - 1; i += 3){
192         /* check if we've overshot the end angle */
193         if( sweepAngle > 0.0 )
194             end_angle = min(start_angle + M_PI_2, endAngle);
195         else
196             end_angle = max(start_angle - M_PI_2, endAngle);
197
198         add_arc_part(&points[i], x1, y1, x2, y2, start_angle, end_angle, i == 0);
199
200         start_angle += M_PI_2 * (sweepAngle < 0.0 ? -1.0 : 1.0);
201     }
202
203     return count;
204 }
205
206 COLORREF ARGB2COLORREF(ARGB color)
207 {
208     /*
209     Packing of these color structures:
210     COLORREF:   00bbggrr
211     ARGB:       aarrggbb
212     FIXME:doesn't handle alpha channel
213     */
214     return (COLORREF)
215         ((color & 0x0000ff) << 16) +
216          (color & 0x00ff00) +
217         ((color & 0xff0000) >> 16);
218 }
219
220 /* Like atan2, but puts angle in correct quadrant if dx is 0. */
221 REAL gdiplus_atan2(REAL dy, REAL dx)
222 {
223     if((dx == 0.0) && (dy != 0.0))
224         return dy > 0.0 ? M_PI_2 : -M_PI_2;
225
226     return atan2(dy, dx);
227 }