push 9eb9af089d68d39110a91889d3a673043db63c4b
[wine/hacks.git] / dlls / gdiplus / gdiplus.c
blob79b9ca0d159c9ba462e700dc879937dca1b1e379
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
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.
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.
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
19 #include <stdarg.h>
20 #include <math.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winerror.h"
25 #include "wine/debug.h"
26 #include "wingdi.h"
28 #include "objbase.h"
30 #include "winreg.h"
31 #include "shlwapi.h"
33 #include "gdiplus.h"
34 #include "gdiplus_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
38 static Status WINAPI NotificationHook(ULONG_PTR *token)
40 TRACE("%p\n", token);
41 if(!token)
42 return InvalidParameter;
44 return Ok;
47 static void WINAPI NotificationUnhook(ULONG_PTR token)
49 TRACE("%ld\n", token);
52 /*****************************************************
53 * DllMain
55 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
57 TRACE("(%p, %d, %p)\n", hinst, reason, reserved);
59 switch(reason)
61 case DLL_WINE_PREATTACH:
62 return FALSE; /* prefer native version */
64 case DLL_PROCESS_ATTACH:
65 DisableThreadLibraryCalls( hinst );
66 break;
68 return TRUE;
71 /*****************************************************
72 * GdiplusStartup [GDIPLUS.@]
74 Status WINAPI GdiplusStartup(ULONG_PTR *token, const struct GdiplusStartupInput *input,
75 struct GdiplusStartupOutput *output)
77 if(!token || !input)
78 return InvalidParameter;
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);
85 if(input->GdiplusVersion != 1)
86 return UnsupportedGdiplusVersion;
88 if(input->SuppressBackgroundThread){
89 if(!output)
90 return InvalidParameter;
92 output->NotificationHook = NotificationHook;
93 output->NotificationUnhook = NotificationUnhook;
96 *token = 0xdeadbeef;
98 /* FIXME: DebugEventCallback ignored */
100 return Ok;
103 /*****************************************************
104 * GdiplusShutdown [GDIPLUS.@]
106 void WINAPI GdiplusShutdown(ULONG_PTR token)
108 /* FIXME: no object tracking */
111 /*****************************************************
112 * GdipAlloc [GDIPLUS.@]
114 void* WINGDIPAPI GdipAlloc(SIZE_T size)
116 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
119 /*****************************************************
120 * GdipFree [GDIPLUS.@]
122 void WINGDIPAPI GdipFree(void* ptr)
124 HeapFree(GetProcessHeap(), 0, ptr);
127 /* Calculates the bezier points needed to fill in the arc portion starting at
128 * angle start and ending at end. These two angles should be no more than 90
129 * degrees from each other. x1, y1, x2, y2 describes the bounding box (upper
130 * left and width and height). Angles must be in radians. write_first indicates
131 * that the first bezier point should be written out (usually this is false).
132 * pt is the array of GpPointFs that gets written to.
134 static void add_arc_part(GpPointF * pt, REAL x1, REAL y1, REAL x2, REAL y2,
135 REAL start, REAL end, BOOL write_first)
137 REAL center_x, center_y, rad_x, rad_y, cos_start, cos_end,
138 sin_start, sin_end, a, half;
139 INT i;
141 rad_x = x2 / 2.0;
142 rad_y = y2 / 2.0;
143 center_x = x1 + rad_x;
144 center_y = y1 + rad_y;
146 cos_start = cos(start);
147 cos_end = cos(end);
148 sin_start = sin(start);
149 sin_end = sin(end);
151 half = (end - start) / 2.0;
152 a = 4.0 / 3.0 * (1 - cos(half)) / sin(half);
154 if(write_first){
155 pt[0].X = cos_start;
156 pt[0].Y = sin_start;
158 pt[1].X = cos_start - a * sin_start;
159 pt[1].Y = sin_start + a * cos_start;
161 pt[3].X = cos_end;
162 pt[3].Y = sin_end;
163 pt[2].X = cos_end + a * sin_end;
164 pt[2].Y = sin_end - a * cos_end;
166 /* expand the points back from the unit circle to the ellipse */
167 for(i = (write_first ? 0 : 1); i < 4; i ++){
168 pt[i].X = pt[i].X * rad_x + center_x;
169 pt[i].Y = pt[i].Y * rad_y + center_y;
173 /* We plot the curve as if it is on a circle then stretch the points. This
174 * adjusts the angles so that when we stretch the points they will end in the
175 * right place. This is only complicated because atan and atan2 do not behave
176 * conveniently. */
177 static void unstretch_angle(REAL * angle, REAL rad_x, REAL rad_y)
179 REAL stretched;
180 INT revs_off;
182 *angle = deg2rad(*angle);
184 if(fabs(cos(*angle)) < 0.00001 || fabs(sin(*angle)) < 0.00001)
185 return;
187 stretched = gdiplus_atan2(sin(*angle) / fabs(rad_y), cos(*angle) / fabs(rad_x));
188 revs_off = roundr(*angle / (2.0 * M_PI)) - roundr(stretched / (2.0 * M_PI));
189 stretched += ((REAL)revs_off) * M_PI * 2.0;
190 *angle = stretched;
193 /* Stores the bezier points that correspond to the arc in points. If points is
194 * null, just return the number of points needed to represent the arc. */
195 INT arc2polybezier(GpPointF * points, REAL x1, REAL y1, REAL x2, REAL y2,
196 REAL startAngle, REAL sweepAngle)
198 INT i, count;
199 REAL end_angle, start_angle, endAngle;
201 endAngle = startAngle + sweepAngle;
202 unstretch_angle(&startAngle, x2 / 2.0, y2 / 2.0);
203 unstretch_angle(&endAngle, x2 / 2.0, y2 / 2.0);
205 count = ceilf(fabs(endAngle - startAngle) / M_PI_2) * 3 + 1;
206 /* don't make more than a full circle */
207 count = min(MAX_ARC_PTS, count);
209 if(count == 1)
210 return 0;
211 if(!points)
212 return count;
214 /* start_angle and end_angle are the iterative variables */
215 start_angle = startAngle;
217 for(i = 0; i < count - 1; i += 3){
218 /* check if we've overshot the end angle */
219 if( sweepAngle > 0.0 )
220 end_angle = min(start_angle + M_PI_2, endAngle);
221 else
222 end_angle = max(start_angle - M_PI_2, endAngle);
224 add_arc_part(&points[i], x1, y1, x2, y2, start_angle, end_angle, i == 0);
226 start_angle += M_PI_2 * (sweepAngle < 0.0 ? -1.0 : 1.0);
229 return count;
232 COLORREF ARGB2COLORREF(ARGB color)
235 Packing of these color structures:
236 COLORREF: 00bbggrr
237 ARGB: aarrggbb
238 FIXME:doesn't handle alpha channel
240 return ((color & 0x0000ff) << 16) +
241 (color & 0x00ff00) +
242 ((color & 0xff0000) >> 16);
245 /* Like atan2, but puts angle in correct quadrant if dx is 0. */
246 REAL gdiplus_atan2(REAL dy, REAL dx)
248 if((dx == 0.0) && (dy != 0.0))
249 return dy > 0.0 ? M_PI_2 : -M_PI_2;
251 return atan2(dy, dx);
254 GpStatus hresult_to_status(HRESULT res)
256 switch(res){
257 case S_OK:
258 return Ok;
259 case E_OUTOFMEMORY:
260 return OutOfMemory;
261 case E_INVALIDARG:
262 return InvalidParameter;
263 default:
264 return GenericError;
268 /* converts a given unit to its value in pixels */
269 REAL convert_unit(HDC hdc, GpUnit unit)
271 switch(unit)
273 case UnitInch:
274 return (REAL) GetDeviceCaps(hdc, LOGPIXELSX);
275 case UnitPoint:
276 return ((REAL)GetDeviceCaps(hdc, LOGPIXELSX)) / 72.0;
277 case UnitDocument:
278 return ((REAL)GetDeviceCaps(hdc, LOGPIXELSX)) / 300.0;
279 case UnitMillimeter:
280 return ((REAL)GetDeviceCaps(hdc, LOGPIXELSX)) / 25.4;
281 case UnitWorld:
282 ERR("cannot convert UnitWorld\n");
283 return 0.0;
284 case UnitPixel:
285 case UnitDisplay:
286 default:
287 return 1.0;
291 /* Calculates Bezier points from cardinal spline points. */
292 void calc_curve_bezier(CONST GpPointF *pts, REAL tension, REAL *x1,
293 REAL *y1, REAL *x2, REAL *y2)
295 REAL xdiff, ydiff;
297 /* calculate tangent */
298 xdiff = pts[2].X - pts[0].X;
299 ydiff = pts[2].Y - pts[0].Y;
301 /* apply tangent to get control points */
302 *x1 = pts[1].X - tension * xdiff;
303 *y1 = pts[1].Y - tension * ydiff;
304 *x2 = pts[1].X + tension * xdiff;
305 *y2 = pts[1].Y + tension * ydiff;
308 /* Calculates Bezier points from cardinal spline endpoints. */
309 void calc_curve_bezier_endp(REAL xend, REAL yend, REAL xadj, REAL yadj,
310 REAL tension, REAL *x, REAL *y)
312 /* tangent at endpoints is the line from the endpoint to the adjacent point */
313 *x = roundr(tension * (xadj - xend) + xend);
314 *y = roundr(tension * (yadj - yend) + yend);
317 /* make sure path has enough space for len more points */
318 BOOL lengthen_path(GpPath *path, INT len)
320 /* initial allocation */
321 if(path->datalen == 0){
322 path->datalen = len * 2;
324 path->pathdata.Points = GdipAlloc(path->datalen * sizeof(PointF));
325 if(!path->pathdata.Points) return FALSE;
327 path->pathdata.Types = GdipAlloc(path->datalen);
328 if(!path->pathdata.Types){
329 GdipFree(path->pathdata.Points);
330 return FALSE;
333 /* reallocation, double size of arrays */
334 else if(path->datalen - path->pathdata.Count < len){
335 while(path->datalen - path->pathdata.Count < len)
336 path->datalen *= 2;
338 path->pathdata.Points = HeapReAlloc(GetProcessHeap(), 0,
339 path->pathdata.Points, path->datalen * sizeof(PointF));
340 if(!path->pathdata.Points) return FALSE;
342 path->pathdata.Types = HeapReAlloc(GetProcessHeap(), 0,
343 path->pathdata.Types, path->datalen);
344 if(!path->pathdata.Types) return FALSE;
347 return TRUE;
350 /* recursive deletion of GpRegion nodes */
351 inline void delete_element(region_element* element)
353 switch(element->type)
355 case RegionDataRect:
356 break;
357 case RegionDataPath:
358 GdipDeletePath(element->elementdata.pathdata.path);
359 break;
360 case RegionDataEmptyRect:
361 case RegionDataInfiniteRect:
362 break;
363 default:
364 delete_element(element->elementdata.combine.left);
365 delete_element(element->elementdata.combine.right);
366 GdipFree(element->elementdata.combine.left);
367 GdipFree(element->elementdata.combine.right);
368 break;