ddraw: Set dwMaxVertexCount to 2048.
[wine.git] / dlls / gdiplus / gdiplus.c
blob32e287b040958fd97d202d62fc17964930fe1b16
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 const REAL mm_per_inch = 25.4;
39 static const REAL inch_per_mm = 1.0 / 25.4;
40 static const REAL point_per_inch = 72.0;
41 static const REAL inch_per_point = 1.0 / 72.0;
43 static Status WINAPI NotificationHook(ULONG_PTR *token)
45 TRACE("%p\n", token);
46 if(!token)
47 return InvalidParameter;
49 return Ok;
52 static void WINAPI NotificationUnhook(ULONG_PTR token)
54 TRACE("%Id\n", token);
57 /*****************************************************
58 * DllMain
60 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
62 TRACE("(%p, %ld, %p)\n", hinst, reason, reserved);
64 switch(reason)
66 case DLL_PROCESS_ATTACH:
67 DisableThreadLibraryCalls( hinst );
68 init_generic_string_formats();
69 break;
71 case DLL_PROCESS_DETACH:
72 if (reserved) break;
73 free_installed_fonts();
74 free_generic_string_formats();
75 break;
77 return TRUE;
80 /*****************************************************
81 * GdiplusStartup [GDIPLUS.@]
83 Status WINAPI GdiplusStartup(ULONG_PTR *token, const struct GdiplusStartupInput *input,
84 struct GdiplusStartupOutput *output)
86 if(!token || !input)
87 return InvalidParameter;
89 TRACE("%p %p %p\n", token, input, output);
90 TRACE("GdiplusStartupInput %d %p %d %d\n", input->GdiplusVersion,
91 input->DebugEventCallback, input->SuppressBackgroundThread,
92 input->SuppressExternalCodecs);
94 if(input->GdiplusVersion < 1 || input->GdiplusVersion > 2)
95 return UnsupportedGdiplusVersion;
97 if(input->SuppressBackgroundThread){
98 if(!output)
99 return InvalidParameter;
101 output->NotificationHook = NotificationHook;
102 output->NotificationUnhook = NotificationUnhook;
105 *token = 0xdeadbeef;
107 /* FIXME: DebugEventCallback ignored */
109 return Ok;
112 GpStatus WINAPI GdiplusNotificationHook(ULONG_PTR *token)
114 FIXME("%p\n", token);
115 return NotificationHook(token);
118 void WINAPI GdiplusNotificationUnhook(ULONG_PTR token)
120 FIXME("%Id\n", token);
121 NotificationUnhook(token);
124 /*****************************************************
125 * GdiplusShutdown [GDIPLUS.@]
127 ULONG WINAPI GdiplusShutdown_wrapper(ULONG_PTR token)
129 /* Notice the slightly different prototype from the official
130 * signature which forces us to use the _wrapper suffix.
133 /* FIXME: no object tracking */
135 /* "bricksntiles" expects a return value of 0, which native
136 * coincidentally gives.
138 return 0;
141 /*****************************************************
142 * GdipAlloc [GDIPLUS.@]
144 void* WINGDIPAPI GdipAlloc(SIZE_T size)
146 return calloc(1, size);
149 /*****************************************************
150 * GdipFree [GDIPLUS.@]
152 void WINGDIPAPI GdipFree(void* ptr)
154 free(ptr);
157 /* Calculates the bezier points needed to fill in the arc portion starting at
158 * angle start and ending at end. These two angles should be no more than 90
159 * degrees from each other. x1, y1, x2, y2 describes the bounding box (upper
160 * left and width and height). Angles must be in radians. write_first indicates
161 * that the first bezier point should be written out (usually this is false).
162 * pt is the array of GpPointFs that gets written to.
164 static void add_arc_part(GpPointF * pt, REAL x1, REAL y1, REAL x2, REAL y2,
165 REAL start, REAL end, BOOL write_first)
167 REAL center_x, center_y, rad_x, rad_y, cos_start, cos_end,
168 sin_start, sin_end, a, half;
169 INT i;
171 rad_x = x2 / 2.0;
172 rad_y = y2 / 2.0;
173 center_x = x1 + rad_x;
174 center_y = y1 + rad_y;
176 cos_start = cos(start);
177 cos_end = cos(end);
178 sin_start = sin(start);
179 sin_end = sin(end);
181 half = (end - start) / 2.0;
182 a = 4.0 / 3.0 * (1 - cos(half)) / sin(half);
184 if(write_first){
185 pt[0].X = cos_start;
186 pt[0].Y = sin_start;
188 pt[1].X = cos_start - a * sin_start;
189 pt[1].Y = sin_start + a * cos_start;
191 pt[3].X = cos_end;
192 pt[3].Y = sin_end;
193 pt[2].X = cos_end + a * sin_end;
194 pt[2].Y = sin_end - a * cos_end;
196 /* expand the points back from the unit circle to the ellipse */
197 for(i = (write_first ? 0 : 1); i < 4; i ++){
198 pt[i].X = pt[i].X * rad_x + center_x;
199 pt[i].Y = pt[i].Y * rad_y + center_y;
203 /* We plot the curve as if it is on a circle then stretch the points. This
204 * adjusts the angles so that when we stretch the points they will end in the
205 * right place. This is only complicated because atan and atan2 do not behave
206 * conveniently. */
207 static REAL unstretch_angle(REAL angle, REAL dia_x, REAL dia_y)
209 REAL stretched;
210 INT revs_off;
212 if(fabs(cos(angle)) < 0.00001 || fabs(sin(angle)) < 0.00001)
213 return angle;
215 stretched = gdiplus_atan2(sin(angle) / fabs(dia_y), cos(angle) / fabs(dia_x));
216 revs_off = gdip_round(angle / (2.0 * M_PI)) - gdip_round(stretched / (2.0 * M_PI));
217 stretched += ((REAL)revs_off) * M_PI * 2.0;
218 return stretched;
221 /* Stores the bezier points that correspond to the arc in points. If points is
222 * null, just return the number of points needed to represent the arc. */
223 INT arc2polybezier(GpPointF * points, REAL left, REAL top, REAL width, REAL height,
224 REAL start_angle, REAL sweep_angle)
226 INT i;
227 REAL partial_end_angle, end_angle;
229 end_angle = deg2rad(start_angle + sweep_angle);
230 start_angle = deg2rad(start_angle);
232 if (width != height)
234 start_angle = unstretch_angle(start_angle, width, height);
235 end_angle = unstretch_angle(end_angle, width, height);
238 for(i = 0; i < MAX_ARC_PTS - 1; i += 3){
239 /* check if we've overshot the end angle */
240 if( sweep_angle > 0.0 )
242 if (start_angle >= end_angle) break;
243 partial_end_angle = min(start_angle + M_PI_2, end_angle);
245 else
247 if (start_angle <= end_angle) break;
248 partial_end_angle = max(start_angle - M_PI_2, end_angle);
251 if (points)
252 add_arc_part(&points[i], left, top, width, height, start_angle, partial_end_angle, i == 0);
254 start_angle = partial_end_angle;
257 if (i == 0) return 0;
258 else return i+1;
261 COLORREF ARGB2COLORREF(ARGB color)
264 Packing of these color structures:
265 COLORREF: 00bbggrr
266 ARGB: aarrggbb
267 FIXME:doesn't handle alpha channel
269 return ((color & 0x0000ff) << 16) +
270 (color & 0x00ff00) +
271 ((color & 0xff0000) >> 16);
274 HBITMAP ARGB2BMP(ARGB color)
276 BITMAPINFO bi;
277 HBITMAP result;
278 RGBQUAD *bits;
279 int alpha;
281 if ((color & 0xff000000) == 0xff000000) return 0;
283 bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
284 bi.bmiHeader.biWidth = 1;
285 bi.bmiHeader.biHeight = 1;
286 bi.bmiHeader.biPlanes = 1;
287 bi.bmiHeader.biBitCount = 32;
288 bi.bmiHeader.biCompression = BI_RGB;
289 bi.bmiHeader.biSizeImage = 0;
290 bi.bmiHeader.biXPelsPerMeter = 0;
291 bi.bmiHeader.biYPelsPerMeter = 0;
292 bi.bmiHeader.biClrUsed = 0;
293 bi.bmiHeader.biClrImportant = 0;
295 result = CreateDIBSection(0, &bi, DIB_RGB_COLORS, (void*)&bits, NULL, 0);
297 bits[0].rgbReserved = alpha = (color>>24)&0xff;
298 bits[0].rgbRed = ((color>>16)&0xff)*alpha/255;
299 bits[0].rgbGreen = ((color>>8)&0xff)*alpha/255;
300 bits[0].rgbBlue = (color&0xff)*alpha/255;
302 return result;
305 /* Like atan2, but puts angle in correct quadrant if dx is 0. */
306 REAL gdiplus_atan2(REAL dy, REAL dx)
308 if((dx == 0.0) && (dy != 0.0))
309 return dy > 0.0 ? M_PI_2 : -M_PI_2;
311 return atan2(dy, dx);
314 GpStatus hresult_to_status(HRESULT res)
316 switch(res){
317 case S_OK:
318 return Ok;
319 case E_OUTOFMEMORY:
320 return OutOfMemory;
321 case E_INVALIDARG:
322 return InvalidParameter;
323 default:
324 return GenericError;
328 /* converts a given unit to its value in pixels */
329 REAL units_to_pixels(REAL units, GpUnit unit, REAL dpi, BOOL printer_display)
331 switch (unit)
333 case UnitPixel:
334 case UnitWorld:
335 return units;
336 case UnitDisplay:
337 if (printer_display)
338 return units * dpi * 0.01f;
339 else
340 return units;
341 case UnitPoint:
342 return units * dpi * inch_per_point;
343 case UnitInch:
344 return units * dpi;
345 case UnitDocument:
346 return units * dpi * (1.0f / 300.0f); /* Per MSDN */
347 case UnitMillimeter:
348 return units * dpi * inch_per_mm;
349 default:
350 FIXME("Unhandled unit type: %d\n", unit);
351 return 0;
355 /* converts value in pixels to a given unit */
356 REAL pixels_to_units(REAL pixels, GpUnit unit, REAL dpi, BOOL printer_display)
358 switch (unit)
360 case UnitPixel:
361 case UnitWorld:
362 return pixels;
363 case UnitDisplay:
364 if (printer_display)
365 return pixels * 100.0 / dpi;
366 else
367 return pixels;
368 case UnitPoint:
369 return pixels * point_per_inch / dpi;
370 case UnitInch:
371 return pixels / dpi;
372 case UnitDocument:
373 return pixels * 300.0 / dpi;
374 case UnitMillimeter:
375 return pixels * mm_per_inch / dpi;
376 default:
377 FIXME("Unhandled unit type: %d\n", unit);
378 return 0;
382 REAL units_scale(GpUnit from, GpUnit to, REAL dpi, BOOL printer_display)
384 REAL pixels = units_to_pixels(1.0, from, dpi, printer_display);
385 return pixels_to_units(pixels, to, dpi, printer_display);
388 /* Calculates Bezier points from cardinal spline points. */
389 void calc_curve_bezier(const GpPointF *pts, REAL tension, REAL *x1,
390 REAL *y1, REAL *x2, REAL *y2)
392 REAL xdiff, ydiff;
394 /* calculate tangent */
395 xdiff = pts[2].X - pts[0].X;
396 ydiff = pts[2].Y - pts[0].Y;
398 /* apply tangent to get control points */
399 *x1 = pts[1].X - tension * xdiff;
400 *y1 = pts[1].Y - tension * ydiff;
401 *x2 = pts[1].X + tension * xdiff;
402 *y2 = pts[1].Y + tension * ydiff;
405 /* Calculates Bezier points from cardinal spline endpoints. */
406 void calc_curve_bezier_endp(REAL xend, REAL yend, REAL xadj, REAL yadj,
407 REAL tension, REAL *x, REAL *y)
409 /* tangent at endpoints is the line from the endpoint to the adjacent point */
410 *x = gdip_round(tension * (xadj - xend) + xend);
411 *y = gdip_round(tension * (yadj - yend) + yend);
414 /* make sure path has enough space for len more points */
415 BOOL lengthen_path(GpPath *path, INT len)
417 /* initial allocation */
418 if(path->datalen == 0){
419 path->datalen = len * 2;
421 path->pathdata.Points = calloc(path->datalen, sizeof(PointF));
422 if(!path->pathdata.Points) return FALSE;
424 path->pathdata.Types = calloc(1, path->datalen);
425 if(!path->pathdata.Types){
426 free(path->pathdata.Points);
427 return FALSE;
430 /* reallocation, double size of arrays */
431 else if(path->datalen - path->pathdata.Count < len){
432 while(path->datalen - path->pathdata.Count < len)
433 path->datalen *= 2;
435 path->pathdata.Points = realloc(path->pathdata.Points, path->datalen * sizeof(PointF));
436 if(!path->pathdata.Points) return FALSE;
438 path->pathdata.Types = realloc(path->pathdata.Types, path->datalen);
439 if(!path->pathdata.Types) return FALSE;
442 return TRUE;
445 void convert_32bppARGB_to_32bppPARGB(UINT width, UINT height,
446 BYTE *dst_bits, INT dst_stride, const BYTE *src_bits, INT src_stride)
448 INT x, y;
449 for (y=0; y<height; y++)
451 const BYTE *src=src_bits+y*src_stride;
452 BYTE *dst=dst_bits+y*dst_stride;
453 for (x=0; x<width; x++)
455 BYTE alpha=src[3];
456 *dst++ = (*src++ * alpha + 127) / 255;
457 *dst++ = (*src++ * alpha + 127) / 255;
458 *dst++ = (*src++ * alpha + 127) / 255;
459 *dst++ = *src++;
464 /* recursive deletion of GpRegion nodes */
465 void delete_element(region_element* element)
467 switch(element->type)
469 case RegionDataRect:
470 break;
471 case RegionDataPath:
472 GdipDeletePath(element->elementdata.path);
473 break;
474 case RegionDataEmptyRect:
475 case RegionDataInfiniteRect:
476 break;
477 default:
478 delete_element(element->elementdata.combine.left);
479 delete_element(element->elementdata.combine.right);
480 free(element->elementdata.combine.left);
481 free(element->elementdata.combine.right);
482 break;
486 const char *debugstr_rectf(const RectF* rc)
488 if (!rc) return "(null)";
489 return wine_dbg_sprintf("(%0.2f,%0.2f,%0.2f,%0.2f)", rc->X, rc->Y, rc->Width, rc->Height);
492 const char *debugstr_pointf(const PointF* pt)
494 if (!pt) return "(null)";
495 return wine_dbg_sprintf("(%0.2f,%0.2f)", pt->X, pt->Y);
498 const char *debugstr_matrix(const GpMatrix *matrix)
500 if (!matrix) return "(null)";
501 return wine_dbg_sprintf("%p(%0.2f,%0.2f,%0.2f,%0.2f,%0.2f,%0.2f)",matrix, matrix->matrix[0], matrix->matrix[1],
502 matrix->matrix[2], matrix->matrix[3], matrix->matrix[4], matrix->matrix[5]);