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
25 #include "wine/debug.h"
27 #include "gdiplus_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
31 /*****************************************************
34 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
36 TRACE("(%p, %d, %p)\n", hinst, reason, reserved);
40 case DLL_WINE_PREATTACH:
41 return FALSE; /* prefer native version */
43 case DLL_PROCESS_ATTACH:
44 DisableThreadLibraryCalls( hinst );
50 /*****************************************************
51 * GdiplusStartup [GDIPLUS.@]
53 Status WINAPI GdiplusStartup(ULONG_PTR *token, const struct GdiplusStartupInput *input,
54 struct GdiplusStartupOutput *output)
57 return InvalidParameter;
59 if(input->GdiplusVersion != 1) {
60 return UnsupportedGdiplusVersion;
61 } else if ((input->DebugEventCallback) ||
62 (input->SuppressBackgroundThread) || (input->SuppressExternalCodecs)){
63 FIXME("Unimplemented for non-default GdiplusStartupInput\n");
64 return NotImplemented;
66 FIXME("Unimplemented for non-null GdiplusStartupOutput\n");
67 return NotImplemented;
73 /*****************************************************
74 * GdiplusShutdown [GDIPLUS.@]
76 void WINAPI GdiplusShutdown(ULONG_PTR token)
78 /* FIXME: no object tracking */
81 /*****************************************************
82 * GdipAlloc [GDIPLUS.@]
84 void* WINGDIPAPI GdipAlloc(SIZE_T size)
86 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
89 /*****************************************************
90 * GdipFree [GDIPLUS.@]
92 void WINGDIPAPI GdipFree(void* ptr)
94 HeapFree(GetProcessHeap(), 0, ptr);
97 /* Calculates the bezier points needed to fill in the arc portion starting at
98 * angle start and ending at end. These two angles should be no more than 90
99 * degrees from each other. x1, y1, x2, y2 describes the bounding box (upper
100 * left and width and height). Angles must be in radians. write_first indicates
101 * that the first bezier point should be written out (usually this is false).
102 * pt is the array of GpPointFs that gets written to.
104 static void add_arc_part(GpPointF * pt, REAL x1, REAL y1, REAL x2, REAL y2,
105 REAL start, REAL end, BOOL write_first)
107 REAL center_x, center_y, rad_x, rad_y, cos_start, cos_end,
108 sin_start, sin_end, a, half;
113 center_x = x1 + rad_x;
114 center_y = y1 + rad_y;
116 cos_start = cos(start);
118 sin_start = sin(start);
121 half = (end - start) / 2.0;
122 a = 4.0 / 3.0 * (1 - cos(half)) / sin(half);
128 pt[1].X = cos_start - a * sin_start;
129 pt[1].Y = sin_start + a * cos_start;
133 pt[2].X = cos_end + a * sin_end;
134 pt[2].Y = sin_end - a * cos_end;
136 /* expand the points back from the unit circle to the ellipse */
137 for(i = (write_first ? 0 : 1); i < 4; i ++){
138 pt[i].X = pt[i].X * rad_x + center_x;
139 pt[i].Y = pt[i].Y * rad_y + center_y;
143 /* We plot the curve as if it is on a circle then stretch the points. This
144 * adjusts the angles so that when we stretch the points they will end in the
145 * right place. This is only complicated because atan and atan2 do not behave
147 static void unstretch_angle(REAL * angle, REAL rad_x, REAL rad_y)
152 *angle = deg2rad(*angle);
154 if(cos(*angle) == 0 || sin(*angle) == 0)
157 stretched = gdiplus_atan2(sin(*angle) / rad_y, cos(*angle) / rad_x);
158 revs_off = roundr(*angle / (2.0 * M_PI)) - roundr(stretched / (2.0 * M_PI));
159 stretched += ((REAL)revs_off) * M_PI * 2.0;
163 /* Stores the bezier points that correspond to the arc in points. If points is
164 * null, just return the number of points needed to represent the arc. */
165 INT arc2polybezier(GpPointF * points, REAL x1, REAL y1, REAL x2, REAL y2,
166 REAL startAngle, REAL sweepAngle)
169 REAL end_angle, start_angle, endAngle;
171 endAngle = startAngle + sweepAngle;
172 unstretch_angle(&startAngle, x2 / 2.0, y2 / 2.0);
173 unstretch_angle(&endAngle, x2 / 2.0, y2 / 2.0);
175 count = ceilf(fabs(endAngle - startAngle) / M_PI_2) * 3 + 1;
176 /* don't make more than a full circle */
177 count = min(MAX_ARC_PTS, count);
184 /* start_angle and end_angle are the iterative variables */
185 start_angle = startAngle;
187 for(i = 0; i < count - 1; i += 3){
188 /* check if we've overshot the end angle */
189 if( sweepAngle > 0.0 )
190 end_angle = min(start_angle + M_PI_2, endAngle);
192 end_angle = max(start_angle - M_PI_2, endAngle);
194 add_arc_part(&points[i], x1, y1, x2, y2, start_angle, end_angle, i == 0);
196 start_angle += M_PI_2 * (sweepAngle < 0.0 ? -1.0 : 1.0);
202 COLORREF ARGB2COLORREF(ARGB color)
205 Packing of these color structures:
208 FIXME:doesn't handle alpha channel
211 ((color & 0x0000ff) << 16) +
213 ((color & 0xff0000) >> 16);
216 /* Like atan2, but puts angle in correct quadrant if dx is 0. */
217 FLOAT gdiplus_atan2(FLOAT dy, FLOAT dx)
219 if((dx == 0.0) && (dy != 0.0))
220 return dy > 0.0 ? M_PI_2 : -M_PI_2;
222 return atan2(dy, dx);