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"
34 #include "gdiplus_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
38 static Status WINAPI NotificationHook(ULONG_PTR *token)
42 return InvalidParameter;
47 static void WINAPI NotificationUnhook(ULONG_PTR token)
49 TRACE("%ld\n", token);
52 /*****************************************************
55 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
57 TRACE("(%p, %d, %p)\n", hinst, reason, reserved);
61 case DLL_WINE_PREATTACH:
62 return FALSE; /* prefer native version */
64 case DLL_PROCESS_ATTACH:
65 DisableThreadLibraryCalls( hinst );
71 /*****************************************************
72 * GdiplusStartup [GDIPLUS.@]
74 Status WINAPI GdiplusStartup(ULONG_PTR *token, const struct GdiplusStartupInput *input,
75 struct GdiplusStartupOutput *output)
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){
90 return InvalidParameter;
92 output->NotificationHook = NotificationHook;
93 output->NotificationUnhook = NotificationUnhook;
96 /* FIXME: DebugEventCallback ignored */
101 /*****************************************************
102 * GdiplusShutdown [GDIPLUS.@]
104 void WINAPI GdiplusShutdown(ULONG_PTR token)
106 /* FIXME: no object tracking */
109 /*****************************************************
110 * GdipAlloc [GDIPLUS.@]
112 void* WINGDIPAPI GdipAlloc(SIZE_T size)
114 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
117 /*****************************************************
118 * GdipFree [GDIPLUS.@]
120 void WINGDIPAPI GdipFree(void* ptr)
122 HeapFree(GetProcessHeap(), 0, ptr);
125 /* Calculates the bezier points needed to fill in the arc portion starting at
126 * angle start and ending at end. These two angles should be no more than 90
127 * degrees from each other. x1, y1, x2, y2 describes the bounding box (upper
128 * left and width and height). Angles must be in radians. write_first indicates
129 * that the first bezier point should be written out (usually this is false).
130 * pt is the array of GpPointFs that gets written to.
132 static void add_arc_part(GpPointF * pt, REAL x1, REAL y1, REAL x2, REAL y2,
133 REAL start, REAL end, BOOL write_first)
135 REAL center_x, center_y, rad_x, rad_y, cos_start, cos_end,
136 sin_start, sin_end, a, half;
141 center_x = x1 + rad_x;
142 center_y = y1 + rad_y;
144 cos_start = cos(start);
146 sin_start = sin(start);
149 half = (end - start) / 2.0;
150 a = 4.0 / 3.0 * (1 - cos(half)) / sin(half);
156 pt[1].X = cos_start - a * sin_start;
157 pt[1].Y = sin_start + a * cos_start;
161 pt[2].X = cos_end + a * sin_end;
162 pt[2].Y = sin_end - a * cos_end;
164 /* expand the points back from the unit circle to the ellipse */
165 for(i = (write_first ? 0 : 1); i < 4; i ++){
166 pt[i].X = pt[i].X * rad_x + center_x;
167 pt[i].Y = pt[i].Y * rad_y + center_y;
171 /* We plot the curve as if it is on a circle then stretch the points. This
172 * adjusts the angles so that when we stretch the points they will end in the
173 * right place. This is only complicated because atan and atan2 do not behave
175 static void unstretch_angle(REAL * angle, REAL rad_x, REAL rad_y)
180 *angle = deg2rad(*angle);
182 if(fabs(cos(*angle)) < 0.00001 || fabs(sin(*angle)) < 0.00001)
185 stretched = gdiplus_atan2(sin(*angle) / fabs(rad_y), cos(*angle) / fabs(rad_x));
186 revs_off = roundr(*angle / (2.0 * M_PI)) - roundr(stretched / (2.0 * M_PI));
187 stretched += ((REAL)revs_off) * M_PI * 2.0;
191 /* Stores the bezier points that correspond to the arc in points. If points is
192 * null, just return the number of points needed to represent the arc. */
193 INT arc2polybezier(GpPointF * points, REAL x1, REAL y1, REAL x2, REAL y2,
194 REAL startAngle, REAL sweepAngle)
197 REAL end_angle, start_angle, endAngle;
199 endAngle = startAngle + sweepAngle;
200 unstretch_angle(&startAngle, x2 / 2.0, y2 / 2.0);
201 unstretch_angle(&endAngle, x2 / 2.0, y2 / 2.0);
203 count = ceilf(fabs(endAngle - startAngle) / M_PI_2) * 3 + 1;
204 /* don't make more than a full circle */
205 count = min(MAX_ARC_PTS, count);
212 /* start_angle and end_angle are the iterative variables */
213 start_angle = startAngle;
215 for(i = 0; i < count - 1; i += 3){
216 /* check if we've overshot the end angle */
217 if( sweepAngle > 0.0 )
218 end_angle = min(start_angle + M_PI_2, endAngle);
220 end_angle = max(start_angle - M_PI_2, endAngle);
222 add_arc_part(&points[i], x1, y1, x2, y2, start_angle, end_angle, i == 0);
224 start_angle += M_PI_2 * (sweepAngle < 0.0 ? -1.0 : 1.0);
230 COLORREF ARGB2COLORREF(ARGB color)
233 Packing of these color structures:
236 FIXME:doesn't handle alpha channel
238 return ((color & 0x0000ff) << 16) +
240 ((color & 0xff0000) >> 16);
243 /* Like atan2, but puts angle in correct quadrant if dx is 0. */
244 REAL gdiplus_atan2(REAL dy, REAL dx)
246 if((dx == 0.0) && (dy != 0.0))
247 return dy > 0.0 ? M_PI_2 : -M_PI_2;
249 return atan2(dy, dx);
252 GpStatus hresult_to_status(HRESULT res)
260 return InvalidParameter;
266 /* converts a given unit to its value in pixels */
267 REAL convert_unit(HDC hdc, GpUnit unit)
272 return (REAL) GetDeviceCaps(hdc, LOGPIXELSX);
274 return ((REAL)GetDeviceCaps(hdc, LOGPIXELSX)) / 72.0;
276 return ((REAL)GetDeviceCaps(hdc, LOGPIXELSX)) / 300.0;
278 return ((REAL)GetDeviceCaps(hdc, LOGPIXELSX)) / 25.4;
280 ERR("cannot convert UnitWorld\n");
289 /* Calculates Bezier points from cardinal spline points. */
290 void calc_curve_bezier(CONST GpPointF *pts, REAL tension, REAL *x1,
291 REAL *y1, REAL *x2, REAL *y2)
295 /* calculate tangent */
296 xdiff = pts[2].X - pts[0].X;
297 ydiff = pts[2].Y - pts[0].Y;
299 /* apply tangent to get control points */
300 *x1 = pts[1].X - tension * xdiff;
301 *y1 = pts[1].Y - tension * ydiff;
302 *x2 = pts[1].X + tension * xdiff;
303 *y2 = pts[1].Y + tension * ydiff;
306 /* Calculates Bezier points from cardinal spline endpoints. */
307 void calc_curve_bezier_endp(REAL xend, REAL yend, REAL xadj, REAL yadj,
308 REAL tension, REAL *x, REAL *y)
310 /* tangent at endpoints is the line from the endpoint to the adjacent point */
311 *x = roundr(tension * (xadj - xend) + xend);
312 *y = roundr(tension * (yadj - yend) + yend);