gdiplus: Implemented GdipDrawImage.
[wine/hacks.git] / dlls / gdiplus / graphics.c
blob4559390e2a144d67deccb183b42778e75272c9a7
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>
21 #include <limits.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/unicode.h"
29 #define COBJMACROS
30 #include "objbase.h"
31 #include "ocidl.h"
32 #include "olectl.h"
33 #include "ole2.h"
35 #include "winreg.h"
36 #include "shlwapi.h"
38 #include "gdiplus.h"
39 #include "gdiplus_private.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
44 /* looks-right constants */
45 #define TENSION_CONST (0.3)
46 #define ANCHOR_WIDTH (2.0)
47 #define MAX_ITERS (50)
49 /* Converts angle (in degrees) to x/y coordinates */
50 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
52 REAL radAngle, hypotenuse;
54 radAngle = deg2rad(angle);
55 hypotenuse = 50.0; /* arbitrary */
57 *x = x_0 + cos(radAngle) * hypotenuse;
58 *y = y_0 + sin(radAngle) * hypotenuse;
61 /* Converts from gdiplus path point type to gdi path point type. */
62 static BYTE convert_path_point_type(BYTE type)
64 BYTE ret;
66 switch(type & PathPointTypePathTypeMask){
67 case PathPointTypeBezier:
68 ret = PT_BEZIERTO;
69 break;
70 case PathPointTypeLine:
71 ret = PT_LINETO;
72 break;
73 case PathPointTypeStart:
74 ret = PT_MOVETO;
75 break;
76 default:
77 ERR("Bad point type\n");
78 return 0;
81 if(type & PathPointTypeCloseSubpath)
82 ret |= PT_CLOSEFIGURE;
84 return ret;
87 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
89 HPEN gdipen;
90 REAL width;
91 INT save_state = SaveDC(graphics->hdc), i, numdashes;
92 GpPointF pt[2];
93 DWORD dash_array[MAX_DASHLEN];
95 EndPath(graphics->hdc);
97 if(pen->unit == UnitPixel){
98 width = pen->width;
100 else{
101 /* Get an estimate for the amount the pen width is affected by the world
102 * transform. (This is similar to what some of the wine drivers do.) */
103 pt[0].X = 0.0;
104 pt[0].Y = 0.0;
105 pt[1].X = 1.0;
106 pt[1].Y = 1.0;
107 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
108 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
109 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
111 width *= pen->width * convert_unit(graphics->hdc,
112 pen->unit == UnitWorld ? graphics->unit : pen->unit);
115 if(pen->dash == DashStyleCustom){
116 numdashes = min(pen->numdashes, MAX_DASHLEN);
118 TRACE("dashes are: ");
119 for(i = 0; i < numdashes; i++){
120 dash_array[i] = roundr(width * pen->dashes[i]);
121 TRACE("%d, ", dash_array[i]);
123 TRACE("\n and the pen style is %x\n", pen->style);
125 gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb,
126 numdashes, dash_array);
128 else
129 gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb, 0, NULL);
131 SelectObject(graphics->hdc, gdipen);
133 return save_state;
136 static void restore_dc(GpGraphics *graphics, INT state)
138 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
139 RestoreDC(graphics->hdc, state);
142 /* This helper applies all the changes that the points listed in ptf need in
143 * order to be drawn on the device context. In the end, this should include at
144 * least:
145 * -scaling by page unit
146 * -applying world transformation
147 * -converting from float to int
148 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
149 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
150 * gdi to draw, and these functions would irreparably mess with line widths.
152 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
153 GpPointF *ptf, INT count)
155 REAL unitscale;
156 GpMatrix *matrix;
157 int i;
159 unitscale = convert_unit(graphics->hdc, graphics->unit);
161 /* apply page scale */
162 if(graphics->unit != UnitDisplay)
163 unitscale *= graphics->scale;
165 GdipCloneMatrix(graphics->worldtrans, &matrix);
166 GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
167 GdipTransformMatrixPoints(matrix, ptf, count);
168 GdipDeleteMatrix(matrix);
170 for(i = 0; i < count; i++){
171 pti[i].x = roundr(ptf[i].X);
172 pti[i].y = roundr(ptf[i].Y);
176 /* GdipDrawPie/GdipFillPie helper function */
177 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
178 REAL height, REAL startAngle, REAL sweepAngle)
180 GpPointF ptf[4];
181 POINT pti[4];
183 ptf[0].X = x;
184 ptf[0].Y = y;
185 ptf[1].X = x + width;
186 ptf[1].Y = y + height;
188 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
189 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
191 transform_and_round_points(graphics, pti, ptf, 4);
193 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
194 pti[2].y, pti[3].x, pti[3].y);
197 /* GdipDrawCurve helper function.
198 * Calculates Bezier points from cardinal spline points. */
199 static void calc_curve_bezier(CONST GpPointF *pts, REAL tension, REAL *x1,
200 REAL *y1, REAL *x2, REAL *y2)
202 REAL xdiff, ydiff;
204 /* calculate tangent */
205 xdiff = pts[2].X - pts[0].X;
206 ydiff = pts[2].Y - pts[0].Y;
208 /* apply tangent to get control points */
209 *x1 = pts[1].X - tension * xdiff;
210 *y1 = pts[1].Y - tension * ydiff;
211 *x2 = pts[1].X + tension * xdiff;
212 *y2 = pts[1].Y + tension * ydiff;
215 /* GdipDrawCurve helper function.
216 * Calculates Bezier points from cardinal spline endpoints. */
217 static void calc_curve_bezier_endp(REAL xend, REAL yend, REAL xadj, REAL yadj,
218 REAL tension, REAL *x, REAL *y)
220 /* tangent at endpoints is the line from the endpoint to the adjacent point */
221 *x = roundr(tension * (xadj - xend) + xend);
222 *y = roundr(tension * (yadj - yend) + yend);
225 /* Draws the linecap the specified color and size on the hdc. The linecap is in
226 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
227 * should not be called on an hdc that has a path you care about. */
228 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
229 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
231 HGDIOBJ oldbrush = NULL, oldpen = NULL;
232 GpMatrix *matrix = NULL;
233 HBRUSH brush = NULL;
234 HPEN pen = NULL;
235 PointF ptf[4], *custptf = NULL;
236 POINT pt[4], *custpt = NULL;
237 BYTE *tp = NULL;
238 REAL theta, dsmall, dbig, dx, dy = 0.0;
239 INT i, count;
240 LOGBRUSH lb;
241 BOOL customstroke;
243 if((x1 == x2) && (y1 == y2))
244 return;
246 theta = gdiplus_atan2(y2 - y1, x2 - x1);
248 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
249 if(!customstroke){
250 brush = CreateSolidBrush(color);
251 lb.lbStyle = BS_SOLID;
252 lb.lbColor = color;
253 lb.lbHatch = 0;
254 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
255 PS_JOIN_MITER, 1, &lb, 0,
256 NULL);
257 oldbrush = SelectObject(graphics->hdc, brush);
258 oldpen = SelectObject(graphics->hdc, pen);
261 switch(cap){
262 case LineCapFlat:
263 break;
264 case LineCapSquare:
265 case LineCapSquareAnchor:
266 case LineCapDiamondAnchor:
267 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
268 if(cap == LineCapDiamondAnchor){
269 dsmall = cos(theta + M_PI_2) * size;
270 dbig = sin(theta + M_PI_2) * size;
272 else{
273 dsmall = cos(theta + M_PI_4) * size;
274 dbig = sin(theta + M_PI_4) * size;
277 ptf[0].X = x2 - dsmall;
278 ptf[1].X = x2 + dbig;
280 ptf[0].Y = y2 - dbig;
281 ptf[3].Y = y2 + dsmall;
283 ptf[1].Y = y2 - dsmall;
284 ptf[2].Y = y2 + dbig;
286 ptf[3].X = x2 - dbig;
287 ptf[2].X = x2 + dsmall;
289 transform_and_round_points(graphics, pt, ptf, 4);
290 Polygon(graphics->hdc, pt, 4);
292 break;
293 case LineCapArrowAnchor:
294 size = size * 4.0 / sqrt(3.0);
296 dx = cos(M_PI / 6.0 + theta) * size;
297 dy = sin(M_PI / 6.0 + theta) * size;
299 ptf[0].X = x2 - dx;
300 ptf[0].Y = y2 - dy;
302 dx = cos(- M_PI / 6.0 + theta) * size;
303 dy = sin(- M_PI / 6.0 + theta) * size;
305 ptf[1].X = x2 - dx;
306 ptf[1].Y = y2 - dy;
308 ptf[2].X = x2;
309 ptf[2].Y = y2;
311 transform_and_round_points(graphics, pt, ptf, 3);
312 Polygon(graphics->hdc, pt, 3);
314 break;
315 case LineCapRoundAnchor:
316 dx = dy = ANCHOR_WIDTH * size / 2.0;
318 ptf[0].X = x2 - dx;
319 ptf[0].Y = y2 - dy;
320 ptf[1].X = x2 + dx;
321 ptf[1].Y = y2 + dy;
323 transform_and_round_points(graphics, pt, ptf, 2);
324 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
326 break;
327 case LineCapTriangle:
328 size = size / 2.0;
329 dx = cos(M_PI_2 + theta) * size;
330 dy = sin(M_PI_2 + theta) * size;
332 ptf[0].X = x2 - dx;
333 ptf[0].Y = y2 - dy;
334 ptf[1].X = x2 + dx;
335 ptf[1].Y = y2 + dy;
337 dx = cos(theta) * size;
338 dy = sin(theta) * size;
340 ptf[2].X = x2 + dx;
341 ptf[2].Y = y2 + dy;
343 transform_and_round_points(graphics, pt, ptf, 3);
344 Polygon(graphics->hdc, pt, 3);
346 break;
347 case LineCapRound:
348 dx = dy = size / 2.0;
350 ptf[0].X = x2 - dx;
351 ptf[0].Y = y2 - dy;
352 ptf[1].X = x2 + dx;
353 ptf[1].Y = y2 + dy;
355 dx = -cos(M_PI_2 + theta) * size;
356 dy = -sin(M_PI_2 + theta) * size;
358 ptf[2].X = x2 - dx;
359 ptf[2].Y = y2 - dy;
360 ptf[3].X = x2 + dx;
361 ptf[3].Y = y2 + dy;
363 transform_and_round_points(graphics, pt, ptf, 4);
364 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
365 pt[2].y, pt[3].x, pt[3].y);
367 break;
368 case LineCapCustom:
369 if(!custom)
370 break;
372 count = custom->pathdata.Count;
373 custptf = GdipAlloc(count * sizeof(PointF));
374 custpt = GdipAlloc(count * sizeof(POINT));
375 tp = GdipAlloc(count);
377 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
378 goto custend;
380 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
382 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
383 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
384 MatrixOrderAppend);
385 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
386 GdipTransformMatrixPoints(matrix, custptf, count);
388 transform_and_round_points(graphics, custpt, custptf, count);
390 for(i = 0; i < count; i++)
391 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
393 if(custom->fill){
394 BeginPath(graphics->hdc);
395 PolyDraw(graphics->hdc, custpt, tp, count);
396 EndPath(graphics->hdc);
397 StrokeAndFillPath(graphics->hdc);
399 else
400 PolyDraw(graphics->hdc, custpt, tp, count);
402 custend:
403 GdipFree(custptf);
404 GdipFree(custpt);
405 GdipFree(tp);
406 GdipDeleteMatrix(matrix);
407 break;
408 default:
409 break;
412 if(!customstroke){
413 SelectObject(graphics->hdc, oldbrush);
414 SelectObject(graphics->hdc, oldpen);
415 DeleteObject(brush);
416 DeleteObject(pen);
420 /* Shortens the line by the given percent by changing x2, y2.
421 * If percent is > 1.0 then the line will change direction.
422 * If percent is negative it can lengthen the line. */
423 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
425 REAL dist, theta, dx, dy;
427 if((y1 == *y2) && (x1 == *x2))
428 return;
430 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
431 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
432 dx = cos(theta) * dist;
433 dy = sin(theta) * dist;
435 *x2 = *x2 + dx;
436 *y2 = *y2 + dy;
439 /* Shortens the line by the given amount by changing x2, y2.
440 * If the amount is greater than the distance, the line will become length 0.
441 * If the amount is negative, it can lengthen the line. */
442 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
444 REAL dx, dy, percent;
446 dx = *x2 - x1;
447 dy = *y2 - y1;
448 if(dx == 0 && dy == 0)
449 return;
451 percent = amt / sqrt(dx * dx + dy * dy);
452 if(percent >= 1.0){
453 *x2 = x1;
454 *y2 = y1;
455 return;
458 shorten_line_percent(x1, y1, x2, y2, percent);
461 /* Draws lines between the given points, and if caps is true then draws an endcap
462 * at the end of the last line. */
463 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
464 GDIPCONST GpPointF * pt, INT count, BOOL caps)
466 POINT *pti = NULL;
467 GpPointF *ptcopy = NULL;
468 GpStatus status = GenericError;
470 if(!count)
471 return Ok;
473 pti = GdipAlloc(count * sizeof(POINT));
474 ptcopy = GdipAlloc(count * sizeof(GpPointF));
476 if(!pti || !ptcopy){
477 status = OutOfMemory;
478 goto end;
481 memcpy(ptcopy, pt, count * sizeof(GpPointF));
483 if(caps){
484 if(pen->endcap == LineCapArrowAnchor)
485 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
486 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
487 else if((pen->endcap == LineCapCustom) && pen->customend)
488 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
489 &ptcopy[count-1].X, &ptcopy[count-1].Y,
490 pen->customend->inset * pen->width);
492 if(pen->startcap == LineCapArrowAnchor)
493 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
494 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
495 else if((pen->startcap == LineCapCustom) && pen->customstart)
496 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
497 &ptcopy[0].X, &ptcopy[0].Y,
498 pen->customstart->inset * pen->width);
500 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
501 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
502 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
503 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
506 transform_and_round_points(graphics, pti, ptcopy, count);
508 if(Polyline(graphics->hdc, pti, count))
509 status = Ok;
511 end:
512 GdipFree(pti);
513 GdipFree(ptcopy);
515 return status;
518 /* Conducts a linear search to find the bezier points that will back off
519 * the endpoint of the curve by a distance of amt. Linear search works
520 * better than binary in this case because there are multiple solutions,
521 * and binary searches often find a bad one. I don't think this is what
522 * Windows does but short of rendering the bezier without GDI's help it's
523 * the best we can do. If rev then work from the start of the passed points
524 * instead of the end. */
525 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
527 GpPointF origpt[4];
528 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
529 INT i, first = 0, second = 1, third = 2, fourth = 3;
531 if(rev){
532 first = 3;
533 second = 2;
534 third = 1;
535 fourth = 0;
538 origx = pt[fourth].X;
539 origy = pt[fourth].Y;
540 memcpy(origpt, pt, sizeof(GpPointF) * 4);
542 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
543 /* reset bezier points to original values */
544 memcpy(pt, origpt, sizeof(GpPointF) * 4);
545 /* Perform magic on bezier points. Order is important here.*/
546 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
547 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
548 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
549 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
550 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
551 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
553 dx = pt[fourth].X - origx;
554 dy = pt[fourth].Y - origy;
556 diff = sqrt(dx * dx + dy * dy);
557 percent += 0.0005 * amt;
561 /* Draws bezier curves between given points, and if caps is true then draws an
562 * endcap at the end of the last line. */
563 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
564 GDIPCONST GpPointF * pt, INT count, BOOL caps)
566 POINT *pti;
567 GpPointF *ptcopy;
568 GpStatus status = GenericError;
570 if(!count)
571 return Ok;
573 pti = GdipAlloc(count * sizeof(POINT));
574 ptcopy = GdipAlloc(count * sizeof(GpPointF));
576 if(!pti || !ptcopy){
577 status = OutOfMemory;
578 goto end;
581 memcpy(ptcopy, pt, count * sizeof(GpPointF));
583 if(caps){
584 if(pen->endcap == LineCapArrowAnchor)
585 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
586 else if((pen->endcap == LineCapCustom) && pen->customend)
587 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
588 FALSE);
590 if(pen->startcap == LineCapArrowAnchor)
591 shorten_bezier_amt(ptcopy, pen->width, TRUE);
592 else if((pen->startcap == LineCapCustom) && pen->customstart)
593 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
595 /* the direction of the line cap is parallel to the direction at the
596 * end of the bezier (which, if it has been shortened, is not the same
597 * as the direction from pt[count-2] to pt[count-1]) */
598 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
599 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
600 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
601 pt[count - 1].X, pt[count - 1].Y);
603 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
604 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
605 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
608 transform_and_round_points(graphics, pti, ptcopy, count);
610 PolyBezier(graphics->hdc, pti, count);
612 status = Ok;
614 end:
615 GdipFree(pti);
616 GdipFree(ptcopy);
618 return status;
621 /* Draws a combination of bezier curves and lines between points. */
622 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
623 GDIPCONST BYTE * types, INT count, BOOL caps)
625 POINT *pti = GdipAlloc(count * sizeof(POINT));
626 BYTE *tp = GdipAlloc(count);
627 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
628 INT i, j;
629 GpStatus status = GenericError;
631 if(!count){
632 status = Ok;
633 goto end;
635 if(!pti || !tp || !ptcopy){
636 status = OutOfMemory;
637 goto end;
640 for(i = 1; i < count; i++){
641 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
642 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
643 || !(types[i + 1] & PathPointTypeBezier)){
644 ERR("Bad bezier points\n");
645 goto end;
647 i += 2;
651 memcpy(ptcopy, pt, count * sizeof(GpPointF));
653 /* If we are drawing caps, go through the points and adjust them accordingly,
654 * and draw the caps. */
655 if(caps){
656 switch(types[count - 1] & PathPointTypePathTypeMask){
657 case PathPointTypeBezier:
658 if(pen->endcap == LineCapArrowAnchor)
659 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
660 else if((pen->endcap == LineCapCustom) && pen->customend)
661 shorten_bezier_amt(&ptcopy[count - 4],
662 pen->width * pen->customend->inset, FALSE);
664 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
665 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
666 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
667 pt[count - 1].X, pt[count - 1].Y);
669 break;
670 case PathPointTypeLine:
671 if(pen->endcap == LineCapArrowAnchor)
672 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
673 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
674 pen->width);
675 else if((pen->endcap == LineCapCustom) && pen->customend)
676 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
677 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
678 pen->customend->inset * pen->width);
680 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
681 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
682 pt[count - 1].Y);
684 break;
685 default:
686 ERR("Bad path last point\n");
687 goto end;
690 /* Find start of points */
691 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
692 == PathPointTypeStart); j++);
694 switch(types[j] & PathPointTypePathTypeMask){
695 case PathPointTypeBezier:
696 if(pen->startcap == LineCapArrowAnchor)
697 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
698 else if((pen->startcap == LineCapCustom) && pen->customstart)
699 shorten_bezier_amt(&ptcopy[j - 1],
700 pen->width * pen->customstart->inset, TRUE);
702 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
703 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
704 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
705 pt[j - 1].X, pt[j - 1].Y);
707 break;
708 case PathPointTypeLine:
709 if(pen->startcap == LineCapArrowAnchor)
710 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
711 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
712 pen->width);
713 else if((pen->startcap == LineCapCustom) && pen->customstart)
714 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
715 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
716 pen->customstart->inset * pen->width);
718 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
719 pt[j].X, pt[j].Y, pt[j - 1].X,
720 pt[j - 1].Y);
722 break;
723 default:
724 ERR("Bad path points\n");
725 goto end;
729 transform_and_round_points(graphics, pti, ptcopy, count);
731 for(i = 0; i < count; i++){
732 tp[i] = convert_path_point_type(types[i]);
735 PolyDraw(graphics->hdc, pti, tp, count);
737 status = Ok;
739 end:
740 GdipFree(pti);
741 GdipFree(ptcopy);
742 GdipFree(tp);
744 return status;
747 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
749 return GdipCreateFromHDC2(hdc, NULL, graphics);
752 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
754 GpStatus retval;
756 if(hDevice != NULL) {
757 FIXME("Don't know how to hadle parameter hDevice\n");
758 return NotImplemented;
761 if(hdc == NULL)
762 return OutOfMemory;
764 if(graphics == NULL)
765 return InvalidParameter;
767 *graphics = GdipAlloc(sizeof(GpGraphics));
768 if(!*graphics) return OutOfMemory;
770 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
771 GdipFree(*graphics);
772 return retval;
775 (*graphics)->hdc = hdc;
776 (*graphics)->hwnd = NULL;
777 (*graphics)->smoothing = SmoothingModeDefault;
778 (*graphics)->compqual = CompositingQualityDefault;
779 (*graphics)->interpolation = InterpolationModeDefault;
780 (*graphics)->pixeloffset = PixelOffsetModeDefault;
781 (*graphics)->compmode = CompositingModeSourceOver;
782 (*graphics)->unit = UnitDisplay;
783 (*graphics)->scale = 1.0;
785 return Ok;
788 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
790 GpStatus ret;
792 if((ret = GdipCreateFromHDC(GetDC(hwnd), graphics)) != Ok)
793 return ret;
795 (*graphics)->hwnd = hwnd;
797 return Ok;
800 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
801 GpMetafile **metafile)
803 static int calls;
805 if(!hemf || !metafile)
806 return InvalidParameter;
808 if(!(calls++))
809 FIXME("not implemented\n");
811 return NotImplemented;
814 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
815 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
817 IStream *stream = NULL;
818 UINT read;
819 BYTE* copy;
820 HENHMETAFILE hemf;
821 GpStatus retval = GenericError;
823 if(!hwmf || !metafile || !placeable)
824 return InvalidParameter;
826 *metafile = NULL;
827 read = GetMetaFileBitsEx(hwmf, 0, NULL);
828 if(!read)
829 return GenericError;
830 copy = GdipAlloc(read);
831 GetMetaFileBitsEx(hwmf, read, copy);
833 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
834 GdipFree(copy);
836 read = GetEnhMetaFileBits(hemf, 0, NULL);
837 copy = GdipAlloc(read);
838 GetEnhMetaFileBits(hemf, read, copy);
839 DeleteEnhMetaFile(hemf);
841 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
842 ERR("could not make stream\n");
843 GdipFree(copy);
844 goto err;
847 *metafile = GdipAlloc(sizeof(GpMetafile));
848 if(!*metafile){
849 retval = OutOfMemory;
850 goto err;
853 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
854 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
855 goto err;
858 (*metafile)->image.type = ImageTypeMetafile;
859 (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
860 (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Right) / ((REAL) placeable->Inch);
861 (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
862 - placeable->BoundingBox.Left)) / ((REAL) placeable->Inch);
863 (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
864 - placeable->BoundingBox.Top)) / ((REAL) placeable->Inch);
865 (*metafile)->unit = UnitInch;
867 if(delete)
868 DeleteMetaFile(hwmf);
870 return Ok;
872 err:
873 GdipFree(*metafile);
874 IStream_Release(stream);
875 return retval;
878 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
879 UINT access, IStream **stream)
881 DWORD dwMode;
882 HRESULT ret;
884 if(!stream || !filename)
885 return InvalidParameter;
887 if(access & GENERIC_WRITE)
888 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
889 else if(access & GENERIC_READ)
890 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
891 else
892 return InvalidParameter;
894 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
896 return hresult_to_status(ret);
899 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
901 if(!graphics) return InvalidParameter;
902 if(graphics->hwnd)
903 ReleaseDC(graphics->hwnd, graphics->hdc);
905 GdipDeleteMatrix(graphics->worldtrans);
906 HeapFree(GetProcessHeap(), 0, graphics);
908 return Ok;
911 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
912 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
914 INT save_state, num_pts;
915 GpPointF points[MAX_ARC_PTS];
916 GpStatus retval;
918 if(!graphics || !pen || width <= 0 || height <= 0)
919 return InvalidParameter;
921 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
923 save_state = prepare_dc(graphics, pen);
925 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
927 restore_dc(graphics, save_state);
929 return retval;
932 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
933 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
935 INT save_state, num_pts;
936 GpPointF points[MAX_ARC_PTS];
937 GpStatus retval;
939 if(!graphics || !pen || width <= 0 || height <= 0)
940 return InvalidParameter;
942 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
944 save_state = prepare_dc(graphics, pen);
946 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
948 restore_dc(graphics, save_state);
950 return retval;
953 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
954 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
956 INT save_state;
957 GpPointF pt[4];
958 GpStatus retval;
960 if(!graphics || !pen)
961 return InvalidParameter;
963 pt[0].X = x1;
964 pt[0].Y = y1;
965 pt[1].X = x2;
966 pt[1].Y = y2;
967 pt[2].X = x3;
968 pt[2].Y = y3;
969 pt[3].X = x4;
970 pt[3].Y = y4;
972 save_state = prepare_dc(graphics, pen);
974 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
976 restore_dc(graphics, save_state);
978 return retval;
981 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
982 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
984 INT save_state;
985 GpPointF pt[4];
986 GpStatus retval;
988 if(!graphics || !pen)
989 return InvalidParameter;
991 pt[0].X = x1;
992 pt[0].Y = y1;
993 pt[1].X = x2;
994 pt[1].Y = y2;
995 pt[2].X = x3;
996 pt[2].Y = y3;
997 pt[3].X = x4;
998 pt[3].Y = y4;
1000 save_state = prepare_dc(graphics, pen);
1002 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1004 restore_dc(graphics, save_state);
1006 return retval;
1009 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
1010 GDIPCONST GpPointF *points, INT count)
1012 return GdipDrawCurve2(graphics,pen,points,count,1.0);
1015 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
1016 GDIPCONST GpPoint *points, INT count)
1018 GpPointF *pointsF;
1019 GpStatus ret;
1020 INT i;
1022 if(!points || count <= 0)
1023 return InvalidParameter;
1025 pointsF = GdipAlloc(sizeof(GpPointF)*count);
1026 if(!pointsF)
1027 return OutOfMemory;
1029 for(i = 0; i < count; i++){
1030 pointsF[i].X = (REAL)points[i].X;
1031 pointsF[i].Y = (REAL)points[i].Y;
1034 ret = GdipDrawCurve(graphics,pen,pointsF,count);
1035 GdipFree(pointsF);
1037 return ret;
1040 /* Approximates cardinal spline with Bezier curves. */
1041 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
1042 GDIPCONST GpPointF *points, INT count, REAL tension)
1044 /* PolyBezier expects count*3-2 points. */
1045 INT i, len_pt = count*3-2, save_state;
1046 GpPointF *pt;
1047 REAL x1, x2, y1, y2;
1048 GpStatus retval;
1050 if(!graphics || !pen)
1051 return InvalidParameter;
1053 pt = GdipAlloc(len_pt * sizeof(GpPointF));
1054 tension = tension * TENSION_CONST;
1056 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
1057 tension, &x1, &y1);
1059 pt[0].X = points[0].X;
1060 pt[0].Y = points[0].Y;
1061 pt[1].X = x1;
1062 pt[1].Y = y1;
1064 for(i = 0; i < count-2; i++){
1065 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
1067 pt[3*i+2].X = x1;
1068 pt[3*i+2].Y = y1;
1069 pt[3*i+3].X = points[i+1].X;
1070 pt[3*i+3].Y = points[i+1].Y;
1071 pt[3*i+4].X = x2;
1072 pt[3*i+4].Y = y2;
1075 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
1076 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
1078 pt[len_pt-2].X = x1;
1079 pt[len_pt-2].Y = y1;
1080 pt[len_pt-1].X = points[count-1].X;
1081 pt[len_pt-1].Y = points[count-1].Y;
1083 save_state = prepare_dc(graphics, pen);
1085 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
1087 GdipFree(pt);
1088 restore_dc(graphics, save_state);
1090 return retval;
1093 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
1094 GDIPCONST GpPoint *points, INT count, REAL tension)
1096 GpPointF *pointsF;
1097 GpStatus ret;
1098 INT i;
1100 if(!points || count <= 0)
1101 return InvalidParameter;
1103 pointsF = GdipAlloc(sizeof(GpPointF)*count);
1104 if(!pointsF)
1105 return OutOfMemory;
1107 for(i = 0; i < count; i++){
1108 pointsF[i].X = (REAL)points[i].X;
1109 pointsF[i].Y = (REAL)points[i].Y;
1112 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
1113 GdipFree(pointsF);
1115 return ret;
1118 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
1120 /* IPicture::Render uses LONG coords */
1121 return GdipDrawImageI(graphics,image,roundr(x),roundr(y));
1124 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
1125 INT y)
1127 UINT width, height, srcw, srch;
1129 if(!graphics || !image)
1130 return InvalidParameter;
1132 GdipGetImageWidth(image, &width);
1133 GdipGetImageHeight(image, &height);
1135 srcw = width * (((REAL) INCH_HIMETRIC) /
1136 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX)));
1137 srch = height * (((REAL) INCH_HIMETRIC) /
1138 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY)));
1140 if(image->type != ImageTypeMetafile){
1141 y += height;
1142 height *= -1;
1145 IPicture_Render(image->picture, graphics->hdc, x, y, width, height,
1146 0, 0, srcw, srch, NULL);
1148 return Ok;
1151 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1152 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1153 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1154 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1155 DrawImageAbort callback, VOID * callbackData)
1157 GpPointF ptf[3];
1158 POINT pti[3];
1159 REAL dx, dy;
1161 TRACE("%p %p %p %d %f %f %f %f %d %p %p %p\n", graphics, image, points, count,
1162 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1163 callbackData);
1165 if(!graphics || !image || !points || count != 3)
1166 return InvalidParameter;
1168 if(srcUnit == UnitInch)
1169 dx = dy = (REAL) INCH_HIMETRIC;
1170 else if(srcUnit == UnitPixel){
1171 dx = ((REAL) INCH_HIMETRIC) /
1172 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1173 dy = ((REAL) INCH_HIMETRIC) /
1174 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1176 else
1177 return NotImplemented;
1179 memcpy(ptf, points, 3 * sizeof(GpPointF));
1180 transform_and_round_points(graphics, pti, ptf, 3);
1182 /* IPicture renders bitmaps with the y-axis reversed
1183 * FIXME: flipping for unknown image type might not be correct. */
1184 if(image->type != ImageTypeMetafile){
1185 INT temp;
1186 temp = pti[0].y;
1187 pti[0].y = pti[2].y;
1188 pti[2].y = temp;
1191 if(IPicture_Render(image->picture, graphics->hdc,
1192 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1193 srcx * dx, srcy * dy,
1194 srcwidth * dx, srcheight * dy,
1195 NULL) != S_OK){
1196 if(callback)
1197 callback(callbackData);
1198 return GenericError;
1201 return Ok;
1204 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
1205 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
1206 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
1207 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
1208 VOID * callbackData)
1210 GpPointF points[3];
1212 points[0].X = dstx;
1213 points[0].Y = dsty;
1214 points[1].X = dstx + dstwidth;
1215 points[1].Y = dsty;
1216 points[2].X = dstx;
1217 points[2].Y = dsty + dstheight;
1219 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1220 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1223 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
1224 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
1225 INT srcwidth, INT srcheight, GpUnit srcUnit,
1226 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
1227 VOID * callbackData)
1229 GpPointF points[3];
1231 points[0].X = dstx;
1232 points[0].Y = dsty;
1233 points[1].X = dstx + dstwidth;
1234 points[1].Y = dsty;
1235 points[2].X = dstx;
1236 points[2].Y = dsty + dstheight;
1238 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1239 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
1242 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
1243 REAL x, REAL y, REAL width, REAL height)
1245 RectF bounds;
1246 GpUnit unit;
1247 GpStatus ret;
1249 if(!graphics || !image)
1250 return InvalidParameter;
1252 ret = GdipGetImageBounds(image, &bounds, &unit);
1253 if(ret != Ok)
1254 return ret;
1256 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
1257 bounds.X, bounds.Y, bounds.Width, bounds.Height,
1258 unit, NULL, NULL, NULL);
1261 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
1262 INT x, INT y, INT width, INT height)
1264 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
1267 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
1268 REAL y1, REAL x2, REAL y2)
1270 INT save_state;
1271 GpPointF pt[2];
1272 GpStatus retval;
1274 if(!pen || !graphics)
1275 return InvalidParameter;
1277 pt[0].X = x1;
1278 pt[0].Y = y1;
1279 pt[1].X = x2;
1280 pt[1].Y = y2;
1282 save_state = prepare_dc(graphics, pen);
1284 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1286 restore_dc(graphics, save_state);
1288 return retval;
1291 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
1292 INT y1, INT x2, INT y2)
1294 INT save_state;
1295 GpPointF pt[2];
1296 GpStatus retval;
1298 if(!pen || !graphics)
1299 return InvalidParameter;
1301 pt[0].X = (REAL)x1;
1302 pt[0].Y = (REAL)y1;
1303 pt[1].X = (REAL)x2;
1304 pt[1].Y = (REAL)y2;
1306 save_state = prepare_dc(graphics, pen);
1308 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1310 restore_dc(graphics, save_state);
1312 return retval;
1315 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
1316 GpPointF *points, INT count)
1318 INT save_state;
1319 GpStatus retval;
1321 if(!pen || !graphics || (count < 2))
1322 return InvalidParameter;
1324 save_state = prepare_dc(graphics, pen);
1326 retval = draw_polyline(graphics, pen, points, count, TRUE);
1328 restore_dc(graphics, save_state);
1330 return retval;
1333 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
1334 GpPoint *points, INT count)
1336 INT save_state;
1337 GpStatus retval;
1338 GpPointF *ptf = NULL;
1339 int i;
1341 if(!pen || !graphics || (count < 2))
1342 return InvalidParameter;
1344 ptf = GdipAlloc(count * sizeof(GpPointF));
1345 if(!ptf) return OutOfMemory;
1347 for(i = 0; i < count; i ++){
1348 ptf[i].X = (REAL) points[i].X;
1349 ptf[i].Y = (REAL) points[i].Y;
1352 save_state = prepare_dc(graphics, pen);
1354 retval = draw_polyline(graphics, pen, ptf, count, TRUE);
1356 restore_dc(graphics, save_state);
1358 GdipFree(ptf);
1359 return retval;
1362 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
1364 INT save_state;
1365 GpStatus retval;
1367 if(!pen || !graphics)
1368 return InvalidParameter;
1370 save_state = prepare_dc(graphics, pen);
1372 retval = draw_poly(graphics, pen, path->pathdata.Points,
1373 path->pathdata.Types, path->pathdata.Count, TRUE);
1375 restore_dc(graphics, save_state);
1377 return retval;
1380 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
1381 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1383 INT save_state;
1385 if(!graphics || !pen)
1386 return InvalidParameter;
1388 save_state = prepare_dc(graphics, pen);
1389 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1391 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1393 restore_dc(graphics, save_state);
1395 return Ok;
1398 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
1399 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1401 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1404 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
1405 REAL y, REAL width, REAL height)
1407 INT save_state;
1408 GpPointF ptf[4];
1409 POINT pti[4];
1411 if(!pen || !graphics)
1412 return InvalidParameter;
1414 ptf[0].X = x;
1415 ptf[0].Y = y;
1416 ptf[1].X = x + width;
1417 ptf[1].Y = y;
1418 ptf[2].X = x + width;
1419 ptf[2].Y = y + height;
1420 ptf[3].X = x;
1421 ptf[3].Y = y + height;
1423 save_state = prepare_dc(graphics, pen);
1424 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1426 transform_and_round_points(graphics, pti, ptf, 4);
1427 Polygon(graphics->hdc, pti, 4);
1429 restore_dc(graphics, save_state);
1431 return Ok;
1434 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
1435 INT y, INT width, INT height)
1437 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1440 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
1441 GDIPCONST GpRectF* rects, INT count)
1443 GpPointF *ptf;
1444 POINT *pti;
1445 INT save_state, i;
1447 if(!graphics || !pen || !rects || count < 1)
1448 return InvalidParameter;
1450 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
1451 pti = GdipAlloc(4 * count * sizeof(POINT));
1453 if(!ptf || !pti){
1454 GdipFree(ptf);
1455 GdipFree(pti);
1456 return OutOfMemory;
1459 for(i = 0; i < count; i++){
1460 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
1461 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
1462 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
1463 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
1466 save_state = prepare_dc(graphics, pen);
1467 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1469 transform_and_round_points(graphics, pti, ptf, 4 * count);
1471 for(i = 0; i < count; i++)
1472 Polygon(graphics->hdc, &pti[4 * i], 4);
1474 restore_dc(graphics, save_state);
1476 GdipFree(ptf);
1477 GdipFree(pti);
1479 return Ok;
1482 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
1483 GDIPCONST GpRect* rects, INT count)
1485 GpRectF *rectsF;
1486 GpStatus ret;
1487 INT i;
1489 if(!rects || count<=0)
1490 return InvalidParameter;
1492 rectsF = GdipAlloc(sizeof(GpRectF) * count);
1493 if(!rectsF)
1494 return OutOfMemory;
1496 for(i = 0;i < count;i++){
1497 rectsF[i].X = (REAL)rects[i].X;
1498 rectsF[i].Y = (REAL)rects[i].Y;
1499 rectsF[i].Width = (REAL)rects[i].Width;
1500 rectsF[i].Height = (REAL)rects[i].Height;
1503 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
1504 GdipFree(rectsF);
1506 return ret;
1509 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
1510 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
1511 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
1513 HRGN rgn = NULL;
1514 HFONT gdifont;
1515 LOGFONTW lfw;
1516 TEXTMETRICW textmet;
1517 GpPointF pt[2], rectcpy[4];
1518 POINT corners[4];
1519 WCHAR* stringdup;
1520 REAL angle, ang_cos, ang_sin, rel_width, rel_height;
1521 INT sum = 0, height = 0, fit, fitcpy, save_state, i, j, lret, nwidth,
1522 nheight;
1523 SIZE size;
1524 RECT drawcoord;
1526 if(!graphics || !string || !font || !brush || !rect)
1527 return InvalidParameter;
1529 if((brush->bt != BrushTypeSolidColor)){
1530 FIXME("not implemented for given parameters\n");
1531 return NotImplemented;
1534 if(format)
1535 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
1537 if(length == -1) length = lstrlenW(string);
1539 stringdup = GdipAlloc(length * sizeof(WCHAR));
1540 if(!stringdup) return OutOfMemory;
1542 save_state = SaveDC(graphics->hdc);
1543 SetBkMode(graphics->hdc, TRANSPARENT);
1544 SetTextColor(graphics->hdc, brush->lb.lbColor);
1546 rectcpy[3].X = rectcpy[0].X = rect->X;
1547 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
1548 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
1549 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
1550 transform_and_round_points(graphics, corners, rectcpy, 4);
1552 if(roundr(rect->Width) == 0 && roundr(rect->Height) == 0){
1553 rel_width = rel_height = 1.0;
1554 nwidth = nheight = INT_MAX;
1556 else{
1557 rel_width = sqrt((corners[1].x - corners[0].x) * (corners[1].x - corners[0].x) +
1558 (corners[1].y - corners[0].y) * (corners[1].y - corners[0].y))
1559 / rect->Width;
1560 rel_height = sqrt((corners[2].x - corners[1].x) * (corners[2].x - corners[1].x) +
1561 (corners[2].y - corners[1].y) * (corners[2].y - corners[1].y))
1562 / rect->Height;
1564 nwidth = roundr(rel_width * rect->Width);
1565 nheight = roundr(rel_height * rect->Height);
1566 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
1567 SelectClipRgn(graphics->hdc, rgn);
1570 /* Use gdi to find the font, then perform transformations on it (height,
1571 * width, angle). */
1572 SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
1573 GetTextMetricsW(graphics->hdc, &textmet);
1574 lfw = font->lfw;
1576 lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
1577 lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
1579 pt[0].X = 0.0;
1580 pt[0].Y = 0.0;
1581 pt[1].X = 1.0;
1582 pt[1].Y = 0.0;
1583 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
1584 angle = gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
1585 ang_cos = cos(angle);
1586 ang_sin = sin(angle);
1587 lfw.lfEscapement = lfw.lfOrientation = -roundr((angle / M_PI) * 1800.0);
1589 gdifont = CreateFontIndirectW(&lfw);
1590 DeleteObject(SelectObject(graphics->hdc, CreateFontIndirectW(&lfw)));
1592 for(i = 0, j = 0; i < length; i++){
1593 if(!isprintW(string[i]) && (string[i] != '\n'))
1594 continue;
1596 stringdup[j] = string[i];
1597 j++;
1600 stringdup[j] = 0;
1601 length = j;
1603 while(sum < length){
1604 drawcoord.left = corners[0].x + roundr(ang_sin * (REAL) height);
1605 drawcoord.top = corners[0].y + roundr(ang_cos * (REAL) height);
1607 GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
1608 nwidth, &fit, NULL, &size);
1609 fitcpy = fit;
1611 if(fit == 0){
1612 DrawTextW(graphics->hdc, stringdup + sum, 1, &drawcoord, DT_NOCLIP |
1613 DT_EXPANDTABS);
1614 break;
1617 for(lret = 0; lret < fit; lret++)
1618 if(*(stringdup + sum + lret) == '\n')
1619 break;
1621 /* Line break code (may look strange, but it imitates windows). */
1622 if(lret < fit)
1623 fit = lret; /* this is not an off-by-one error */
1624 else if(fit < (length - sum)){
1625 if(*(stringdup + sum + fit) == ' ')
1626 while(*(stringdup + sum + fit) == ' ')
1627 fit++;
1628 else
1629 while(*(stringdup + sum + fit - 1) != ' '){
1630 fit--;
1632 if(*(stringdup + sum + fit) == '\t')
1633 break;
1635 if(fit == 0){
1636 fit = fitcpy;
1637 break;
1641 DrawTextW(graphics->hdc, stringdup + sum, min(length - sum, fit),
1642 &drawcoord, DT_NOCLIP | DT_EXPANDTABS);
1644 sum += fit + (lret < fitcpy ? 1 : 0);
1645 height += size.cy;
1647 if(height > nheight)
1648 break;
1650 /* Stop if this was a linewrap (but not if it was a linebreak). */
1651 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
1652 break;
1655 GdipFree(stringdup);
1656 DeleteObject(rgn);
1657 DeleteObject(gdifont);
1659 RestoreDC(graphics->hdc, save_state);
1661 return Ok;
1664 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
1666 INT save_state;
1667 GpStatus retval;
1669 if(!brush || !graphics || !path)
1670 return InvalidParameter;
1672 save_state = SaveDC(graphics->hdc);
1673 EndPath(graphics->hdc);
1674 SelectObject(graphics->hdc, brush->gdibrush);
1675 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
1676 : WINDING));
1678 BeginPath(graphics->hdc);
1679 retval = draw_poly(graphics, NULL, path->pathdata.Points,
1680 path->pathdata.Types, path->pathdata.Count, FALSE);
1682 if(retval != Ok)
1683 goto end;
1685 EndPath(graphics->hdc);
1686 FillPath(graphics->hdc);
1688 retval = Ok;
1690 end:
1691 RestoreDC(graphics->hdc, save_state);
1693 return retval;
1696 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
1697 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1699 INT save_state;
1701 if(!graphics || !brush)
1702 return InvalidParameter;
1704 save_state = SaveDC(graphics->hdc);
1705 EndPath(graphics->hdc);
1706 SelectObject(graphics->hdc, brush->gdibrush);
1707 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1709 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1711 RestoreDC(graphics->hdc, save_state);
1713 return Ok;
1716 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
1717 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1719 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1722 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
1723 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
1725 INT save_state;
1726 GpPointF *ptf = NULL;
1727 POINT *pti = NULL;
1728 GpStatus retval = Ok;
1730 if(!graphics || !brush || !points || !count)
1731 return InvalidParameter;
1733 ptf = GdipAlloc(count * sizeof(GpPointF));
1734 pti = GdipAlloc(count * sizeof(POINT));
1735 if(!ptf || !pti){
1736 retval = OutOfMemory;
1737 goto end;
1740 memcpy(ptf, points, count * sizeof(GpPointF));
1742 save_state = SaveDC(graphics->hdc);
1743 EndPath(graphics->hdc);
1744 SelectObject(graphics->hdc, brush->gdibrush);
1745 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1746 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1747 : WINDING));
1749 transform_and_round_points(graphics, pti, ptf, count);
1750 Polygon(graphics->hdc, pti, count);
1752 RestoreDC(graphics->hdc, save_state);
1754 end:
1755 GdipFree(ptf);
1756 GdipFree(pti);
1758 return retval;
1761 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
1762 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
1764 INT save_state, i;
1765 GpPointF *ptf = NULL;
1766 POINT *pti = NULL;
1767 GpStatus retval = Ok;
1769 if(!graphics || !brush || !points || !count)
1770 return InvalidParameter;
1772 ptf = GdipAlloc(count * sizeof(GpPointF));
1773 pti = GdipAlloc(count * sizeof(POINT));
1774 if(!ptf || !pti){
1775 retval = OutOfMemory;
1776 goto end;
1779 for(i = 0; i < count; i ++){
1780 ptf[i].X = (REAL) points[i].X;
1781 ptf[i].Y = (REAL) points[i].Y;
1784 save_state = SaveDC(graphics->hdc);
1785 EndPath(graphics->hdc);
1786 SelectObject(graphics->hdc, brush->gdibrush);
1787 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1788 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1789 : WINDING));
1791 transform_and_round_points(graphics, pti, ptf, count);
1792 Polygon(graphics->hdc, pti, count);
1794 RestoreDC(graphics->hdc, save_state);
1796 end:
1797 GdipFree(ptf);
1798 GdipFree(pti);
1800 return retval;
1803 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
1804 REAL x, REAL y, REAL width, REAL height)
1806 INT save_state;
1807 GpPointF ptf[4];
1808 POINT pti[4];
1810 if(!graphics || !brush)
1811 return InvalidParameter;
1813 ptf[0].X = x;
1814 ptf[0].Y = y;
1815 ptf[1].X = x + width;
1816 ptf[1].Y = y;
1817 ptf[2].X = x + width;
1818 ptf[2].Y = y + height;
1819 ptf[3].X = x;
1820 ptf[3].Y = y + height;
1822 save_state = SaveDC(graphics->hdc);
1823 EndPath(graphics->hdc);
1824 SelectObject(graphics->hdc, brush->gdibrush);
1825 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1827 transform_and_round_points(graphics, pti, ptf, 4);
1829 Polygon(graphics->hdc, pti, 4);
1831 RestoreDC(graphics->hdc, save_state);
1833 return Ok;
1836 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
1837 INT x, INT y, INT width, INT height)
1839 INT save_state;
1840 GpPointF ptf[4];
1841 POINT pti[4];
1843 if(!graphics || !brush)
1844 return InvalidParameter;
1846 ptf[0].X = x;
1847 ptf[0].Y = y;
1848 ptf[1].X = x + width;
1849 ptf[1].Y = y;
1850 ptf[2].X = x + width;
1851 ptf[2].Y = y + height;
1852 ptf[3].X = x;
1853 ptf[3].Y = y + height;
1855 save_state = SaveDC(graphics->hdc);
1856 EndPath(graphics->hdc);
1857 SelectObject(graphics->hdc, brush->gdibrush);
1858 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1860 transform_and_round_points(graphics, pti, ptf, 4);
1862 Polygon(graphics->hdc, pti, 4);
1864 RestoreDC(graphics->hdc, save_state);
1866 return Ok;
1869 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
1870 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
1871 CompositingMode *mode)
1873 if(!graphics || !mode)
1874 return InvalidParameter;
1876 *mode = graphics->compmode;
1878 return Ok;
1881 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
1882 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
1883 CompositingQuality *quality)
1885 if(!graphics || !quality)
1886 return InvalidParameter;
1888 *quality = graphics->compqual;
1890 return Ok;
1893 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
1894 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
1895 InterpolationMode *mode)
1897 if(!graphics || !mode)
1898 return InvalidParameter;
1900 *mode = graphics->interpolation;
1902 return Ok;
1905 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
1907 if(!graphics || !scale)
1908 return InvalidParameter;
1910 *scale = graphics->scale;
1912 return Ok;
1915 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
1917 if(!graphics || !unit)
1918 return InvalidParameter;
1920 *unit = graphics->unit;
1922 return Ok;
1925 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
1926 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1927 *mode)
1929 if(!graphics || !mode)
1930 return InvalidParameter;
1932 *mode = graphics->pixeloffset;
1934 return Ok;
1937 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
1938 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
1940 if(!graphics || !mode)
1941 return InvalidParameter;
1943 *mode = graphics->smoothing;
1945 return Ok;
1948 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
1949 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
1950 TextRenderingHint *hint)
1952 if(!graphics || !hint)
1953 return InvalidParameter;
1955 *hint = graphics->texthint;
1957 return Ok;
1960 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
1962 if(!graphics || !matrix)
1963 return InvalidParameter;
1965 *matrix = *graphics->worldtrans;
1966 return Ok;
1969 /* Find the smallest rectangle that bounds the text when it is printed in rect
1970 * according to the format options listed in format. If rect has 0 width and
1971 * height, then just find the smallest rectangle that bounds the text when it's
1972 * printed at location (rect->X, rect-Y). */
1973 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
1974 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
1975 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
1976 INT *codepointsfitted, INT *linesfilled)
1978 HFONT oldfont;
1979 WCHAR* stringdup;
1980 INT sum = 0, height = 0, fit, fitcpy, max_width = 0, i, j, lret, nwidth,
1981 nheight;
1982 SIZE size;
1984 if(!graphics || !string || !font || !rect)
1985 return InvalidParameter;
1987 if(codepointsfitted || linesfilled){
1988 FIXME("not implemented for given parameters\n");
1989 return NotImplemented;
1992 if(format)
1993 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
1995 if(length == -1) length = lstrlenW(string);
1997 stringdup = GdipAlloc(length * sizeof(WCHAR));
1998 if(!stringdup) return OutOfMemory;
2000 oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
2001 nwidth = roundr(rect->Width);
2002 nheight = roundr(rect->Height);
2004 if((nwidth == 0) && (nheight == 0))
2005 nwidth = nheight = INT_MAX;
2007 for(i = 0, j = 0; i < length; i++){
2008 if(!isprintW(string[i]) && (string[i] != '\n'))
2009 continue;
2011 stringdup[j] = string[i];
2012 j++;
2015 stringdup[j] = 0;
2016 length = j;
2018 while(sum < length){
2019 GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
2020 nwidth, &fit, NULL, &size);
2021 fitcpy = fit;
2023 if(fit == 0)
2024 break;
2026 for(lret = 0; lret < fit; lret++)
2027 if(*(stringdup + sum + lret) == '\n')
2028 break;
2030 /* Line break code (may look strange, but it imitates windows). */
2031 if(lret < fit)
2032 fit = lret; /* this is not an off-by-one error */
2033 else if(fit < (length - sum)){
2034 if(*(stringdup + sum + fit) == ' ')
2035 while(*(stringdup + sum + fit) == ' ')
2036 fit++;
2037 else
2038 while(*(stringdup + sum + fit - 1) != ' '){
2039 fit--;
2041 if(*(stringdup + sum + fit) == '\t')
2042 break;
2044 if(fit == 0){
2045 fit = fitcpy;
2046 break;
2051 GetTextExtentExPointW(graphics->hdc, stringdup + sum, fit,
2052 nwidth, &j, NULL, &size);
2054 sum += fit + (lret < fitcpy ? 1 : 0);
2055 height += size.cy;
2056 max_width = max(max_width, size.cx);
2058 if(height > nheight)
2059 break;
2061 /* Stop if this was a linewrap (but not if it was a linebreak). */
2062 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
2063 break;
2066 bounds->X = rect->X;
2067 bounds->Y = rect->Y;
2068 bounds->Width = (REAL)max_width;
2069 bounds->Height = (REAL) min(height, nheight);
2071 GdipFree(stringdup);
2072 DeleteObject(SelectObject(graphics->hdc, oldfont));
2074 return Ok;
2077 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
2079 static int calls;
2081 if(!graphics)
2082 return InvalidParameter;
2084 if(!(calls++))
2085 FIXME("graphics state not implemented\n");
2087 return NotImplemented;
2090 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
2091 GpMatrixOrder order)
2093 if(!graphics)
2094 return InvalidParameter;
2096 return GdipRotateMatrix(graphics->worldtrans, angle, order);
2099 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
2101 static int calls;
2103 if(!graphics || !state)
2104 return InvalidParameter;
2106 if(!(calls++))
2107 FIXME("graphics state not implemented\n");
2109 return NotImplemented;
2112 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
2113 REAL sy, GpMatrixOrder order)
2115 if(!graphics)
2116 return InvalidParameter;
2118 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
2121 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
2122 CompositingMode mode)
2124 if(!graphics)
2125 return InvalidParameter;
2127 graphics->compmode = mode;
2129 return Ok;
2132 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
2133 CompositingQuality quality)
2135 if(!graphics)
2136 return InvalidParameter;
2138 graphics->compqual = quality;
2140 return Ok;
2143 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
2144 InterpolationMode mode)
2146 if(!graphics)
2147 return InvalidParameter;
2149 graphics->interpolation = mode;
2151 return Ok;
2154 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
2156 if(!graphics || (scale <= 0.0))
2157 return InvalidParameter;
2159 graphics->scale = scale;
2161 return Ok;
2164 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
2166 if(!graphics || (unit == UnitWorld))
2167 return InvalidParameter;
2169 graphics->unit = unit;
2171 return Ok;
2174 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
2175 mode)
2177 if(!graphics)
2178 return InvalidParameter;
2180 graphics->pixeloffset = mode;
2182 return Ok;
2185 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
2187 if(!graphics)
2188 return InvalidParameter;
2190 graphics->smoothing = mode;
2192 return Ok;
2195 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
2196 TextRenderingHint hint)
2198 if(!graphics)
2199 return InvalidParameter;
2201 graphics->texthint = hint;
2203 return Ok;
2206 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
2208 if(!graphics || !matrix)
2209 return InvalidParameter;
2211 GdipDeleteMatrix(graphics->worldtrans);
2212 return GdipCloneMatrix(matrix, &graphics->worldtrans);
2215 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
2216 REAL dy, GpMatrixOrder order)
2218 if(!graphics)
2219 return InvalidParameter;
2221 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
2224 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
2225 INT width, INT height,
2226 CombineMode combineMode)
2228 static int calls;
2230 if(!(calls++))
2231 FIXME("not implemented\n");
2233 return NotImplemented;
2236 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
2237 CombineMode combineMode)
2239 static int calls;
2241 if(!(calls++))
2242 FIXME("not implemented\n");
2244 return NotImplemented;
2247 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpGraphics *graphics,
2248 UINT limitDpi)
2250 static int calls;
2252 if(!(calls++))
2253 FIXME("not implemented\n");
2255 return NotImplemented;
2258 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
2259 INT count)
2261 INT save_state;
2262 POINT *pti;
2264 if(!graphics || !pen || count<=0)
2265 return InvalidParameter;
2267 pti = GdipAlloc(sizeof(POINT) * count);
2269 save_state = prepare_dc(graphics, pen);
2270 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2272 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
2273 Polygon(graphics->hdc, pti, count);
2275 restore_dc(graphics, save_state);
2276 GdipFree(pti);
2278 return Ok;
2281 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
2282 INT count)
2284 GpStatus ret;
2285 GpPointF *ptf;
2286 INT i;
2288 if(count<=0) return InvalidParameter;
2289 ptf = GdipAlloc(sizeof(GpPointF) * count);
2291 for(i = 0;i < count; i++){
2292 ptf[i].X = (REAL)points[i].X;
2293 ptf[i].Y = (REAL)points[i].Y;
2296 ret = GdipDrawPolygon(graphics,pen,ptf,count);
2297 GdipFree(ptf);
2299 return ret;