wined3d: Remove FLOAT16 vertex attribute conversion support.
[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_PROCESS_ATTACH:
62         DisableThreadLibraryCalls( hinst );
63         break;
64
65     case DLL_PROCESS_DETACH:
66         free_installed_fonts();
67         break;
68     }
69     return TRUE;
70 }
71
72 /*****************************************************
73  *      GdiplusStartup [GDIPLUS.@]
74  */
75 Status WINAPI GdiplusStartup(ULONG_PTR *token, const struct GdiplusStartupInput *input,
76                              struct GdiplusStartupOutput *output)
77 {
78     if(!token || !input)
79         return InvalidParameter;
80
81     TRACE("%p %p %p\n", token, input, output);
82     TRACE("GdiplusStartupInput %d %p %d %d\n", input->GdiplusVersion,
83           input->DebugEventCallback, input->SuppressBackgroundThread,
84           input->SuppressExternalCodecs);
85
86     if(input->GdiplusVersion < 1 || input->GdiplusVersion > 2)
87         return UnsupportedGdiplusVersion;
88
89     if(input->SuppressBackgroundThread){
90         if(!output)
91             return InvalidParameter;
92
93         output->NotificationHook = NotificationHook;
94         output->NotificationUnhook = NotificationUnhook;
95     }
96
97     *token = 0xdeadbeef;
98
99     /* FIXME: DebugEventCallback ignored */
100
101     return Ok;
102 }
103
104 GpStatus WINAPI GdiplusNotificationHook(ULONG_PTR *token)
105 {
106     FIXME("%p\n", token);
107     return NotificationHook(token);
108 }
109
110 void WINAPI GdiplusNotificationUnhook(ULONG_PTR token)
111 {
112     FIXME("%ld\n", token);
113     NotificationUnhook(token);
114 }
115
116 /*****************************************************
117  *      GdiplusShutdown [GDIPLUS.@]
118  */
119 ULONG WINAPI GdiplusShutdown_wrapper(ULONG_PTR token)
120 {
121     /* Notice the slightly different prototype from the official
122      * signature which forces us to use the the _wrapper suffix.
123      */
124
125     /* FIXME: no object tracking */
126
127     /* "bricksntiles" expects a return value of 0, which native
128      * coincidentally gives.
129      */
130     return 0;
131 }
132
133 /*****************************************************
134  *      GdipAlloc [GDIPLUS.@]
135  */
136 void* WINGDIPAPI GdipAlloc(SIZE_T size)
137 {
138     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
139 }
140
141 /*****************************************************
142  *      GdipFree [GDIPLUS.@]
143  */
144 void WINGDIPAPI GdipFree(void* ptr)
145 {
146     HeapFree(GetProcessHeap(), 0, ptr);
147 }
148
149 /* Calculates the bezier points needed to fill in the arc portion starting at
150  * angle start and ending at end.  These two angles should be no more than 90
151  * degrees from each other.  x1, y1, x2, y2 describes the bounding box (upper
152  * left and width and height).  Angles must be in radians. write_first indicates
153  * that the first bezier point should be written out (usually this is false).
154  * pt is the array of GpPointFs that gets written to.
155  **/
156 static void add_arc_part(GpPointF * pt, REAL x1, REAL y1, REAL x2, REAL y2,
157     REAL start, REAL end, BOOL write_first)
158 {
159     REAL center_x, center_y, rad_x, rad_y, cos_start, cos_end,
160         sin_start, sin_end, a, half;
161     INT i;
162
163     rad_x = x2 / 2.0;
164     rad_y = y2 / 2.0;
165     center_x = x1 + rad_x;
166     center_y = y1 + rad_y;
167
168     cos_start = cos(start);
169     cos_end = cos(end);
170     sin_start = sin(start);
171     sin_end = sin(end);
172
173     half = (end - start) / 2.0;
174     a = 4.0 / 3.0 * (1 - cos(half)) / sin(half);
175
176     if(write_first){
177         pt[0].X = cos_start;
178         pt[0].Y = sin_start;
179     }
180     pt[1].X = cos_start - a * sin_start;
181     pt[1].Y = sin_start + a * cos_start;
182
183     pt[3].X = cos_end;
184     pt[3].Y = sin_end;
185     pt[2].X = cos_end + a * sin_end;
186     pt[2].Y = sin_end - a * cos_end;
187
188     /* expand the points back from the unit circle to the ellipse */
189     for(i = (write_first ? 0 : 1); i < 4; i ++){
190         pt[i].X = pt[i].X * rad_x + center_x;
191         pt[i].Y = pt[i].Y * rad_y + center_y;
192     }
193 }
194
195 /* We plot the curve as if it is on a circle then stretch the points.  This
196  * adjusts the angles so that when we stretch the points they will end in the
197  * right place. This is only complicated because atan and atan2 do not behave
198  * conveniently. */
199 static void unstretch_angle(REAL * angle, REAL rad_x, REAL rad_y)
200 {
201     REAL stretched;
202     INT revs_off;
203
204     *angle = deg2rad(*angle);
205
206     if(fabs(cos(*angle)) < 0.00001 || fabs(sin(*angle)) < 0.00001)
207         return;
208
209     stretched = gdiplus_atan2(sin(*angle) / fabs(rad_y), cos(*angle) / fabs(rad_x));
210     revs_off = roundr(*angle / (2.0 * M_PI)) - roundr(stretched / (2.0 * M_PI));
211     stretched += ((REAL)revs_off) * M_PI * 2.0;
212     *angle = stretched;
213 }
214
215 /* Stores the bezier points that correspond to the arc in points.  If points is
216  * null, just return the number of points needed to represent the arc. */
217 INT arc2polybezier(GpPointF * points, REAL x1, REAL y1, REAL x2, REAL y2,
218     REAL startAngle, REAL sweepAngle)
219 {
220     INT i;
221     REAL end_angle, start_angle, endAngle;
222
223     endAngle = startAngle + sweepAngle;
224     unstretch_angle(&startAngle, x2 / 2.0, y2 / 2.0);
225     unstretch_angle(&endAngle, x2 / 2.0, y2 / 2.0);
226
227     /* start_angle and end_angle are the iterative variables */
228     start_angle = startAngle;
229
230     for(i = 0; i < MAX_ARC_PTS - 1; i += 3){
231         /* check if we've overshot the end angle */
232         if( sweepAngle > 0.0 )
233         {
234             if (start_angle >= endAngle) break;
235             end_angle = min(start_angle + M_PI_2, endAngle);
236         }
237         else
238         {
239             if (start_angle <= endAngle) break;
240             end_angle = max(start_angle - M_PI_2, endAngle);
241         }
242
243         if (points)
244             add_arc_part(&points[i], x1, y1, x2, y2, start_angle, end_angle, i == 0);
245
246         start_angle += M_PI_2 * (sweepAngle < 0.0 ? -1.0 : 1.0);
247     }
248
249     if (i == 0) return 0;
250     else return i+1;
251 }
252
253 COLORREF ARGB2COLORREF(ARGB color)
254 {
255     /*
256     Packing of these color structures:
257     COLORREF:   00bbggrr
258     ARGB:       aarrggbb
259     FIXME:doesn't handle alpha channel
260     */
261     return ((color & 0x0000ff) << 16) +
262            (color & 0x00ff00) +
263            ((color & 0xff0000) >> 16);
264 }
265
266 HBITMAP ARGB2BMP(ARGB color)
267 {
268     HDC hdc;
269     BITMAPINFO bi;
270     HBITMAP result;
271     RGBQUAD *bits;
272     int alpha;
273
274     if ((color & 0xff000000) == 0xff000000) return 0;
275
276     hdc = CreateCompatibleDC(NULL);
277
278     bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
279     bi.bmiHeader.biWidth = 1;
280     bi.bmiHeader.biHeight = 1;
281     bi.bmiHeader.biPlanes = 1;
282     bi.bmiHeader.biBitCount = 32;
283     bi.bmiHeader.biCompression = BI_RGB;
284     bi.bmiHeader.biSizeImage = 0;
285     bi.bmiHeader.biXPelsPerMeter = 0;
286     bi.bmiHeader.biYPelsPerMeter = 0;
287     bi.bmiHeader.biClrUsed = 0;
288     bi.bmiHeader.biClrImportant = 0;
289
290     result = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, (void*)&bits, NULL, 0);
291
292     bits[0].rgbReserved = alpha = (color>>24)&0xff;
293     bits[0].rgbRed = ((color>>16)&0xff)*alpha/255;
294     bits[0].rgbGreen = ((color>>8)&0xff)*alpha/255;
295     bits[0].rgbBlue = (color&0xff)*alpha/255;
296
297     DeleteDC(hdc);
298
299     return result;
300 }
301
302 /* Like atan2, but puts angle in correct quadrant if dx is 0. */
303 REAL gdiplus_atan2(REAL dy, REAL dx)
304 {
305     if((dx == 0.0) && (dy != 0.0))
306         return dy > 0.0 ? M_PI_2 : -M_PI_2;
307
308     return atan2(dy, dx);
309 }
310
311 GpStatus hresult_to_status(HRESULT res)
312 {
313     switch(res){
314         case S_OK:
315             return Ok;
316         case E_OUTOFMEMORY:
317             return OutOfMemory;
318         case E_INVALIDARG:
319             return InvalidParameter;
320         default:
321             return GenericError;
322     }
323 }
324
325 /* converts a given unit to its value in pixels */
326 REAL convert_unit(REAL logpixels, GpUnit unit)
327 {
328     switch(unit)
329     {
330         case UnitInch:
331             return logpixels;
332         case UnitPoint:
333             return logpixels / 72.0;
334         case UnitDocument:
335             return logpixels / 300.0;
336         case UnitMillimeter:
337             return logpixels / 25.4;
338         case UnitWorld:
339             ERR("cannot convert UnitWorld\n");
340             return 0.0;
341         case UnitPixel:
342         case UnitDisplay:
343         default:
344             return 1.0;
345     }
346 }
347
348 /* Calculates Bezier points from cardinal spline points. */
349 void calc_curve_bezier(CONST GpPointF *pts, REAL tension, REAL *x1,
350     REAL *y1, REAL *x2, REAL *y2)
351 {
352     REAL xdiff, ydiff;
353
354     /* calculate tangent */
355     xdiff = pts[2].X - pts[0].X;
356     ydiff = pts[2].Y - pts[0].Y;
357
358     /* apply tangent to get control points */
359     *x1 = pts[1].X - tension * xdiff;
360     *y1 = pts[1].Y - tension * ydiff;
361     *x2 = pts[1].X + tension * xdiff;
362     *y2 = pts[1].Y + tension * ydiff;
363 }
364
365 /* Calculates Bezier points from cardinal spline endpoints. */
366 void calc_curve_bezier_endp(REAL xend, REAL yend, REAL xadj, REAL yadj,
367     REAL tension, REAL *x, REAL *y)
368 {
369     /* tangent at endpoints is the line from the endpoint to the adjacent point */
370     *x = roundr(tension * (xadj - xend) + xend);
371     *y = roundr(tension * (yadj - yend) + yend);
372 }
373
374 /* make sure path has enough space for len more points */
375 BOOL lengthen_path(GpPath *path, INT len)
376 {
377     /* initial allocation */
378     if(path->datalen == 0){
379         path->datalen = len * 2;
380
381         path->pathdata.Points = GdipAlloc(path->datalen * sizeof(PointF));
382         if(!path->pathdata.Points)   return FALSE;
383
384         path->pathdata.Types = GdipAlloc(path->datalen);
385         if(!path->pathdata.Types){
386             GdipFree(path->pathdata.Points);
387             return FALSE;
388         }
389     }
390     /* reallocation, double size of arrays */
391     else if(path->datalen - path->pathdata.Count < len){
392         while(path->datalen - path->pathdata.Count < len)
393             path->datalen *= 2;
394
395         path->pathdata.Points = HeapReAlloc(GetProcessHeap(), 0,
396             path->pathdata.Points, path->datalen * sizeof(PointF));
397         if(!path->pathdata.Points)  return FALSE;
398
399         path->pathdata.Types = HeapReAlloc(GetProcessHeap(), 0,
400             path->pathdata.Types, path->datalen);
401         if(!path->pathdata.Types)   return FALSE;
402     }
403
404     return TRUE;
405 }
406
407 void convert_32bppARGB_to_32bppPARGB(UINT width, UINT height,
408     BYTE *dst_bits, INT dst_stride, const BYTE *src_bits, INT src_stride)
409 {
410     UINT x, y;
411     for (y=0; y<height; y++)
412     {
413         const BYTE *src=src_bits+y*src_stride;
414         BYTE *dst=dst_bits+y*dst_stride;
415         for (x=0; x<width; x++)
416         {
417             BYTE alpha=src[3];
418             *dst++ = *src++ * alpha / 255;
419             *dst++ = *src++ * alpha / 255;
420             *dst++ = *src++ * alpha / 255;
421             *dst++ = *src++;
422         }
423     }
424 }
425
426 /* recursive deletion of GpRegion nodes */
427 void delete_element(region_element* element)
428 {
429     switch(element->type)
430     {
431         case RegionDataRect:
432             break;
433         case RegionDataPath:
434             GdipDeletePath(element->elementdata.pathdata.path);
435             break;
436         case RegionDataEmptyRect:
437         case RegionDataInfiniteRect:
438             break;
439         default:
440             delete_element(element->elementdata.combine.left);
441             delete_element(element->elementdata.combine.right);
442             GdipFree(element->elementdata.combine.left);
443             GdipFree(element->elementdata.combine.right);
444             break;
445     }
446 }
447
448 const char *debugstr_rectf(CONST RectF* rc)
449 {
450     if (!rc) return "(null)";
451     return wine_dbg_sprintf("(%0.2f,%0.2f,%0.2f,%0.2f)", rc->X, rc->Y, rc->Width, rc->Height);
452 }
453
454 const char *debugstr_pointf(CONST PointF* pt)
455 {
456     if (!pt) return "(null)";
457     return wine_dbg_sprintf("(%0.2f,%0.2f)", pt->X, pt->Y);
458 }