push 7f8c39dca3a5819e8ef115eebf7abed537de3a22
[wine/hacks.git] / dlls / gdiplus / graphics.c
blobc274b2e00fb0045d6dd94efec36cdf5b813a4d97
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 /* FIXME: no icm handling */
801 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
803 return GdipCreateFromHWND(hwnd, graphics);
806 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
807 GpMetafile **metafile)
809 static int calls;
811 if(!hemf || !metafile)
812 return InvalidParameter;
814 if(!(calls++))
815 FIXME("not implemented\n");
817 return NotImplemented;
820 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
821 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
823 IStream *stream = NULL;
824 UINT read;
825 BYTE* copy;
826 HENHMETAFILE hemf;
827 GpStatus retval = GenericError;
829 if(!hwmf || !metafile || !placeable)
830 return InvalidParameter;
832 *metafile = NULL;
833 read = GetMetaFileBitsEx(hwmf, 0, NULL);
834 if(!read)
835 return GenericError;
836 copy = GdipAlloc(read);
837 GetMetaFileBitsEx(hwmf, read, copy);
839 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
840 GdipFree(copy);
842 read = GetEnhMetaFileBits(hemf, 0, NULL);
843 copy = GdipAlloc(read);
844 GetEnhMetaFileBits(hemf, read, copy);
845 DeleteEnhMetaFile(hemf);
847 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
848 ERR("could not make stream\n");
849 GdipFree(copy);
850 goto err;
853 *metafile = GdipAlloc(sizeof(GpMetafile));
854 if(!*metafile){
855 retval = OutOfMemory;
856 goto err;
859 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
860 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
861 goto err;
864 (*metafile)->image.type = ImageTypeMetafile;
865 (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
866 (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Right) / ((REAL) placeable->Inch);
867 (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
868 - placeable->BoundingBox.Left)) / ((REAL) placeable->Inch);
869 (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
870 - placeable->BoundingBox.Top)) / ((REAL) placeable->Inch);
871 (*metafile)->unit = UnitInch;
873 if(delete)
874 DeleteMetaFile(hwmf);
876 return Ok;
878 err:
879 GdipFree(*metafile);
880 IStream_Release(stream);
881 return retval;
884 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
885 UINT access, IStream **stream)
887 DWORD dwMode;
888 HRESULT ret;
890 if(!stream || !filename)
891 return InvalidParameter;
893 if(access & GENERIC_WRITE)
894 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
895 else if(access & GENERIC_READ)
896 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
897 else
898 return InvalidParameter;
900 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
902 return hresult_to_status(ret);
905 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
907 if(!graphics) return InvalidParameter;
908 if(graphics->hwnd)
909 ReleaseDC(graphics->hwnd, graphics->hdc);
911 GdipDeleteMatrix(graphics->worldtrans);
912 HeapFree(GetProcessHeap(), 0, graphics);
914 return Ok;
917 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
918 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
920 INT save_state, num_pts;
921 GpPointF points[MAX_ARC_PTS];
922 GpStatus retval;
924 if(!graphics || !pen || width <= 0 || height <= 0)
925 return InvalidParameter;
927 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
929 save_state = prepare_dc(graphics, pen);
931 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
933 restore_dc(graphics, save_state);
935 return retval;
938 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
939 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
941 return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
944 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
945 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
947 INT save_state;
948 GpPointF pt[4];
949 GpStatus retval;
951 if(!graphics || !pen)
952 return InvalidParameter;
954 pt[0].X = x1;
955 pt[0].Y = y1;
956 pt[1].X = x2;
957 pt[1].Y = y2;
958 pt[2].X = x3;
959 pt[2].Y = y3;
960 pt[3].X = x4;
961 pt[3].Y = y4;
963 save_state = prepare_dc(graphics, pen);
965 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
967 restore_dc(graphics, save_state);
969 return retval;
972 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
973 INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
975 INT save_state;
976 GpPointF pt[4];
977 GpStatus retval;
979 if(!graphics || !pen)
980 return InvalidParameter;
982 pt[0].X = x1;
983 pt[0].Y = y1;
984 pt[1].X = x2;
985 pt[1].Y = y2;
986 pt[2].X = x3;
987 pt[2].Y = y3;
988 pt[3].X = x4;
989 pt[3].Y = y4;
991 save_state = prepare_dc(graphics, pen);
993 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
995 restore_dc(graphics, save_state);
997 return retval;
1000 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
1001 GDIPCONST GpPointF *points, INT count)
1003 return GdipDrawCurve2(graphics,pen,points,count,1.0);
1006 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
1007 GDIPCONST GpPoint *points, INT count)
1009 GpPointF *pointsF;
1010 GpStatus ret;
1011 INT i;
1013 if(!points || count <= 0)
1014 return InvalidParameter;
1016 pointsF = GdipAlloc(sizeof(GpPointF)*count);
1017 if(!pointsF)
1018 return OutOfMemory;
1020 for(i = 0; i < count; i++){
1021 pointsF[i].X = (REAL)points[i].X;
1022 pointsF[i].Y = (REAL)points[i].Y;
1025 ret = GdipDrawCurve(graphics,pen,pointsF,count);
1026 GdipFree(pointsF);
1028 return ret;
1031 /* Approximates cardinal spline with Bezier curves. */
1032 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
1033 GDIPCONST GpPointF *points, INT count, REAL tension)
1035 /* PolyBezier expects count*3-2 points. */
1036 INT i, len_pt = count*3-2, save_state;
1037 GpPointF *pt;
1038 REAL x1, x2, y1, y2;
1039 GpStatus retval;
1041 if(!graphics || !pen)
1042 return InvalidParameter;
1044 pt = GdipAlloc(len_pt * sizeof(GpPointF));
1045 tension = tension * TENSION_CONST;
1047 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
1048 tension, &x1, &y1);
1050 pt[0].X = points[0].X;
1051 pt[0].Y = points[0].Y;
1052 pt[1].X = x1;
1053 pt[1].Y = y1;
1055 for(i = 0; i < count-2; i++){
1056 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
1058 pt[3*i+2].X = x1;
1059 pt[3*i+2].Y = y1;
1060 pt[3*i+3].X = points[i+1].X;
1061 pt[3*i+3].Y = points[i+1].Y;
1062 pt[3*i+4].X = x2;
1063 pt[3*i+4].Y = y2;
1066 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
1067 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
1069 pt[len_pt-2].X = x1;
1070 pt[len_pt-2].Y = y1;
1071 pt[len_pt-1].X = points[count-1].X;
1072 pt[len_pt-1].Y = points[count-1].Y;
1074 save_state = prepare_dc(graphics, pen);
1076 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
1078 GdipFree(pt);
1079 restore_dc(graphics, save_state);
1081 return retval;
1084 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
1085 GDIPCONST GpPoint *points, INT count, REAL tension)
1087 GpPointF *pointsF;
1088 GpStatus ret;
1089 INT i;
1091 if(!points || count <= 0)
1092 return InvalidParameter;
1094 pointsF = GdipAlloc(sizeof(GpPointF)*count);
1095 if(!pointsF)
1096 return OutOfMemory;
1098 for(i = 0; i < count; i++){
1099 pointsF[i].X = (REAL)points[i].X;
1100 pointsF[i].Y = (REAL)points[i].Y;
1103 ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
1104 GdipFree(pointsF);
1106 return ret;
1109 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
1110 REAL y, REAL width, REAL height)
1112 INT save_state;
1113 GpPointF ptf[2];
1114 POINT pti[2];
1116 if(!graphics || !pen)
1117 return InvalidParameter;
1119 ptf[0].X = x;
1120 ptf[0].Y = y;
1121 ptf[1].X = x + width;
1122 ptf[1].Y = y + height;
1124 save_state = prepare_dc(graphics, pen);
1125 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1127 transform_and_round_points(graphics, pti, ptf, 2);
1129 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
1131 restore_dc(graphics, save_state);
1133 return Ok;
1136 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
1137 INT y, INT width, INT height)
1139 return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1143 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
1145 /* IPicture::Render uses LONG coords */
1146 return GdipDrawImageI(graphics,image,roundr(x),roundr(y));
1149 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
1150 INT y)
1152 UINT width, height, srcw, srch;
1154 if(!graphics || !image)
1155 return InvalidParameter;
1157 GdipGetImageWidth(image, &width);
1158 GdipGetImageHeight(image, &height);
1160 srcw = width * (((REAL) INCH_HIMETRIC) /
1161 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX)));
1162 srch = height * (((REAL) INCH_HIMETRIC) /
1163 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY)));
1165 if(image->type != ImageTypeMetafile){
1166 y += height;
1167 height *= -1;
1170 IPicture_Render(image->picture, graphics->hdc, x, y, width, height,
1171 0, 0, srcw, srch, NULL);
1173 return Ok;
1176 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1177 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1178 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1179 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1180 DrawImageAbort callback, VOID * callbackData)
1182 GpPointF ptf[3];
1183 POINT pti[3];
1184 REAL dx, dy;
1186 TRACE("%p %p %p %d %f %f %f %f %d %p %p %p\n", graphics, image, points, count,
1187 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1188 callbackData);
1190 if(!graphics || !image || !points || count != 3)
1191 return InvalidParameter;
1193 if(srcUnit == UnitInch)
1194 dx = dy = (REAL) INCH_HIMETRIC;
1195 else if(srcUnit == UnitPixel){
1196 dx = ((REAL) INCH_HIMETRIC) /
1197 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1198 dy = ((REAL) INCH_HIMETRIC) /
1199 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1201 else
1202 return NotImplemented;
1204 memcpy(ptf, points, 3 * sizeof(GpPointF));
1205 transform_and_round_points(graphics, pti, ptf, 3);
1207 /* IPicture renders bitmaps with the y-axis reversed
1208 * FIXME: flipping for unknown image type might not be correct. */
1209 if(image->type != ImageTypeMetafile){
1210 INT temp;
1211 temp = pti[0].y;
1212 pti[0].y = pti[2].y;
1213 pti[2].y = temp;
1216 if(IPicture_Render(image->picture, graphics->hdc,
1217 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1218 srcx * dx, srcy * dy,
1219 srcwidth * dx, srcheight * dy,
1220 NULL) != S_OK){
1221 if(callback)
1222 callback(callbackData);
1223 return GenericError;
1226 return Ok;
1229 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
1230 GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
1231 INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1232 DrawImageAbort callback, VOID * callbackData)
1234 GpPointF pointsF[3];
1235 INT i;
1237 if(!points || count!=3)
1238 return InvalidParameter;
1240 for(i = 0; i < count; i++){
1241 pointsF[i].X = (REAL)points[i].X;
1242 pointsF[i].Y = (REAL)points[i].Y;
1245 return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
1246 (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
1247 callback, callbackData);
1250 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
1251 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
1252 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
1253 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
1254 VOID * callbackData)
1256 GpPointF points[3];
1258 points[0].X = dstx;
1259 points[0].Y = dsty;
1260 points[1].X = dstx + dstwidth;
1261 points[1].Y = dsty;
1262 points[2].X = dstx;
1263 points[2].Y = dsty + dstheight;
1265 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1266 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1269 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
1270 INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
1271 INT srcwidth, INT srcheight, GpUnit srcUnit,
1272 GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
1273 VOID * callbackData)
1275 GpPointF points[3];
1277 points[0].X = dstx;
1278 points[0].Y = dsty;
1279 points[1].X = dstx + dstwidth;
1280 points[1].Y = dsty;
1281 points[2].X = dstx;
1282 points[2].Y = dsty + dstheight;
1284 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1285 srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
1288 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
1289 REAL x, REAL y, REAL width, REAL height)
1291 RectF bounds;
1292 GpUnit unit;
1293 GpStatus ret;
1295 if(!graphics || !image)
1296 return InvalidParameter;
1298 ret = GdipGetImageBounds(image, &bounds, &unit);
1299 if(ret != Ok)
1300 return ret;
1302 return GdipDrawImageRectRect(graphics, image, x, y, width, height,
1303 bounds.X, bounds.Y, bounds.Width, bounds.Height,
1304 unit, NULL, NULL, NULL);
1307 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
1308 INT x, INT y, INT width, INT height)
1310 return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
1313 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
1314 REAL y1, REAL x2, REAL y2)
1316 INT save_state;
1317 GpPointF pt[2];
1318 GpStatus retval;
1320 if(!pen || !graphics)
1321 return InvalidParameter;
1323 pt[0].X = x1;
1324 pt[0].Y = y1;
1325 pt[1].X = x2;
1326 pt[1].Y = y2;
1328 save_state = prepare_dc(graphics, pen);
1330 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1332 restore_dc(graphics, save_state);
1334 return retval;
1337 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
1338 INT y1, INT x2, INT y2)
1340 INT save_state;
1341 GpPointF pt[2];
1342 GpStatus retval;
1344 if(!pen || !graphics)
1345 return InvalidParameter;
1347 pt[0].X = (REAL)x1;
1348 pt[0].Y = (REAL)y1;
1349 pt[1].X = (REAL)x2;
1350 pt[1].Y = (REAL)y2;
1352 save_state = prepare_dc(graphics, pen);
1354 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1356 restore_dc(graphics, save_state);
1358 return retval;
1361 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
1362 GpPointF *points, INT count)
1364 INT save_state;
1365 GpStatus retval;
1367 if(!pen || !graphics || (count < 2))
1368 return InvalidParameter;
1370 save_state = prepare_dc(graphics, pen);
1372 retval = draw_polyline(graphics, pen, points, count, TRUE);
1374 restore_dc(graphics, save_state);
1376 return retval;
1379 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
1380 GpPoint *points, INT count)
1382 INT save_state;
1383 GpStatus retval;
1384 GpPointF *ptf = NULL;
1385 int i;
1387 if(!pen || !graphics || (count < 2))
1388 return InvalidParameter;
1390 ptf = GdipAlloc(count * sizeof(GpPointF));
1391 if(!ptf) return OutOfMemory;
1393 for(i = 0; i < count; i ++){
1394 ptf[i].X = (REAL) points[i].X;
1395 ptf[i].Y = (REAL) points[i].Y;
1398 save_state = prepare_dc(graphics, pen);
1400 retval = draw_polyline(graphics, pen, ptf, count, TRUE);
1402 restore_dc(graphics, save_state);
1404 GdipFree(ptf);
1405 return retval;
1408 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
1410 INT save_state;
1411 GpStatus retval;
1413 if(!pen || !graphics)
1414 return InvalidParameter;
1416 save_state = prepare_dc(graphics, pen);
1418 retval = draw_poly(graphics, pen, path->pathdata.Points,
1419 path->pathdata.Types, path->pathdata.Count, TRUE);
1421 restore_dc(graphics, save_state);
1423 return retval;
1426 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
1427 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1429 INT save_state;
1431 if(!graphics || !pen)
1432 return InvalidParameter;
1434 save_state = prepare_dc(graphics, pen);
1435 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1437 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1439 restore_dc(graphics, save_state);
1441 return Ok;
1444 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
1445 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1447 return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1450 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
1451 REAL y, REAL width, REAL height)
1453 INT save_state;
1454 GpPointF ptf[4];
1455 POINT pti[4];
1457 if(!pen || !graphics)
1458 return InvalidParameter;
1460 ptf[0].X = x;
1461 ptf[0].Y = y;
1462 ptf[1].X = x + width;
1463 ptf[1].Y = y;
1464 ptf[2].X = x + width;
1465 ptf[2].Y = y + height;
1466 ptf[3].X = x;
1467 ptf[3].Y = y + height;
1469 save_state = prepare_dc(graphics, pen);
1470 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1472 transform_and_round_points(graphics, pti, ptf, 4);
1473 Polygon(graphics->hdc, pti, 4);
1475 restore_dc(graphics, save_state);
1477 return Ok;
1480 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
1481 INT y, INT width, INT height)
1483 return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1486 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
1487 GDIPCONST GpRectF* rects, INT count)
1489 GpPointF *ptf;
1490 POINT *pti;
1491 INT save_state, i;
1493 if(!graphics || !pen || !rects || count < 1)
1494 return InvalidParameter;
1496 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
1497 pti = GdipAlloc(4 * count * sizeof(POINT));
1499 if(!ptf || !pti){
1500 GdipFree(ptf);
1501 GdipFree(pti);
1502 return OutOfMemory;
1505 for(i = 0; i < count; i++){
1506 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
1507 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
1508 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
1509 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
1512 save_state = prepare_dc(graphics, pen);
1513 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1515 transform_and_round_points(graphics, pti, ptf, 4 * count);
1517 for(i = 0; i < count; i++)
1518 Polygon(graphics->hdc, &pti[4 * i], 4);
1520 restore_dc(graphics, save_state);
1522 GdipFree(ptf);
1523 GdipFree(pti);
1525 return Ok;
1528 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
1529 GDIPCONST GpRect* rects, INT count)
1531 GpRectF *rectsF;
1532 GpStatus ret;
1533 INT i;
1535 if(!rects || count<=0)
1536 return InvalidParameter;
1538 rectsF = GdipAlloc(sizeof(GpRectF) * count);
1539 if(!rectsF)
1540 return OutOfMemory;
1542 for(i = 0;i < count;i++){
1543 rectsF[i].X = (REAL)rects[i].X;
1544 rectsF[i].Y = (REAL)rects[i].Y;
1545 rectsF[i].Width = (REAL)rects[i].Width;
1546 rectsF[i].Height = (REAL)rects[i].Height;
1549 ret = GdipDrawRectangles(graphics, pen, rectsF, count);
1550 GdipFree(rectsF);
1552 return ret;
1555 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
1556 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
1557 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
1559 HRGN rgn = NULL;
1560 HFONT gdifont;
1561 LOGFONTW lfw;
1562 TEXTMETRICW textmet;
1563 GpPointF pt[2], rectcpy[4];
1564 POINT corners[4];
1565 WCHAR* stringdup;
1566 REAL angle, ang_cos, ang_sin, rel_width, rel_height;
1567 INT sum = 0, height = 0, fit, fitcpy, save_state, i, j, lret, nwidth,
1568 nheight;
1569 SIZE size;
1570 RECT drawcoord;
1572 if(!graphics || !string || !font || !brush || !rect)
1573 return InvalidParameter;
1575 if((brush->bt != BrushTypeSolidColor)){
1576 FIXME("not implemented for given parameters\n");
1577 return NotImplemented;
1580 if(format)
1581 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
1583 if(length == -1) length = lstrlenW(string);
1585 stringdup = GdipAlloc(length * sizeof(WCHAR));
1586 if(!stringdup) return OutOfMemory;
1588 save_state = SaveDC(graphics->hdc);
1589 SetBkMode(graphics->hdc, TRANSPARENT);
1590 SetTextColor(graphics->hdc, brush->lb.lbColor);
1592 rectcpy[3].X = rectcpy[0].X = rect->X;
1593 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
1594 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
1595 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
1596 transform_and_round_points(graphics, corners, rectcpy, 4);
1598 if(roundr(rect->Width) == 0 && roundr(rect->Height) == 0){
1599 rel_width = rel_height = 1.0;
1600 nwidth = nheight = INT_MAX;
1602 else{
1603 rel_width = sqrt((corners[1].x - corners[0].x) * (corners[1].x - corners[0].x) +
1604 (corners[1].y - corners[0].y) * (corners[1].y - corners[0].y))
1605 / rect->Width;
1606 rel_height = sqrt((corners[2].x - corners[1].x) * (corners[2].x - corners[1].x) +
1607 (corners[2].y - corners[1].y) * (corners[2].y - corners[1].y))
1608 / rect->Height;
1610 nwidth = roundr(rel_width * rect->Width);
1611 nheight = roundr(rel_height * rect->Height);
1612 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
1613 SelectClipRgn(graphics->hdc, rgn);
1616 /* Use gdi to find the font, then perform transformations on it (height,
1617 * width, angle). */
1618 SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
1619 GetTextMetricsW(graphics->hdc, &textmet);
1620 lfw = font->lfw;
1622 lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
1623 lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
1625 pt[0].X = 0.0;
1626 pt[0].Y = 0.0;
1627 pt[1].X = 1.0;
1628 pt[1].Y = 0.0;
1629 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
1630 angle = gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
1631 ang_cos = cos(angle);
1632 ang_sin = sin(angle);
1633 lfw.lfEscapement = lfw.lfOrientation = -roundr((angle / M_PI) * 1800.0);
1635 gdifont = CreateFontIndirectW(&lfw);
1636 DeleteObject(SelectObject(graphics->hdc, CreateFontIndirectW(&lfw)));
1638 for(i = 0, j = 0; i < length; i++){
1639 if(!isprintW(string[i]) && (string[i] != '\n'))
1640 continue;
1642 stringdup[j] = string[i];
1643 j++;
1646 stringdup[j] = 0;
1647 length = j;
1649 while(sum < length){
1650 drawcoord.left = corners[0].x + roundr(ang_sin * (REAL) height);
1651 drawcoord.top = corners[0].y + roundr(ang_cos * (REAL) height);
1653 GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
1654 nwidth, &fit, NULL, &size);
1655 fitcpy = fit;
1657 if(fit == 0){
1658 DrawTextW(graphics->hdc, stringdup + sum, 1, &drawcoord, DT_NOCLIP |
1659 DT_EXPANDTABS);
1660 break;
1663 for(lret = 0; lret < fit; lret++)
1664 if(*(stringdup + sum + lret) == '\n')
1665 break;
1667 /* Line break code (may look strange, but it imitates windows). */
1668 if(lret < fit)
1669 fit = lret; /* this is not an off-by-one error */
1670 else if(fit < (length - sum)){
1671 if(*(stringdup + sum + fit) == ' ')
1672 while(*(stringdup + sum + fit) == ' ')
1673 fit++;
1674 else
1675 while(*(stringdup + sum + fit - 1) != ' '){
1676 fit--;
1678 if(*(stringdup + sum + fit) == '\t')
1679 break;
1681 if(fit == 0){
1682 fit = fitcpy;
1683 break;
1687 DrawTextW(graphics->hdc, stringdup + sum, min(length - sum, fit),
1688 &drawcoord, DT_NOCLIP | DT_EXPANDTABS);
1690 sum += fit + (lret < fitcpy ? 1 : 0);
1691 height += size.cy;
1693 if(height > nheight)
1694 break;
1696 /* Stop if this was a linewrap (but not if it was a linebreak). */
1697 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
1698 break;
1701 GdipFree(stringdup);
1702 DeleteObject(rgn);
1703 DeleteObject(gdifont);
1705 RestoreDC(graphics->hdc, save_state);
1707 return Ok;
1710 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
1711 REAL y, REAL width, REAL height)
1713 INT save_state;
1714 GpPointF ptf[2];
1715 POINT pti[2];
1717 if(!graphics || !brush)
1718 return InvalidParameter;
1720 ptf[0].X = x;
1721 ptf[0].Y = y;
1722 ptf[1].X = x + width;
1723 ptf[1].Y = y + height;
1725 save_state = SaveDC(graphics->hdc);
1726 EndPath(graphics->hdc);
1727 SelectObject(graphics->hdc, brush->gdibrush);
1728 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1730 transform_and_round_points(graphics, pti, ptf, 2);
1732 Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
1734 RestoreDC(graphics->hdc, save_state);
1736 return Ok;
1739 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
1740 INT y, INT width, INT height)
1742 return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1745 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
1747 INT save_state;
1748 GpStatus retval;
1750 if(!brush || !graphics || !path)
1751 return InvalidParameter;
1753 save_state = SaveDC(graphics->hdc);
1754 EndPath(graphics->hdc);
1755 SelectObject(graphics->hdc, brush->gdibrush);
1756 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
1757 : WINDING));
1759 BeginPath(graphics->hdc);
1760 retval = draw_poly(graphics, NULL, path->pathdata.Points,
1761 path->pathdata.Types, path->pathdata.Count, FALSE);
1763 if(retval != Ok)
1764 goto end;
1766 EndPath(graphics->hdc);
1767 FillPath(graphics->hdc);
1769 retval = Ok;
1771 end:
1772 RestoreDC(graphics->hdc, save_state);
1774 return retval;
1777 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
1778 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1780 INT save_state;
1782 if(!graphics || !brush)
1783 return InvalidParameter;
1785 save_state = SaveDC(graphics->hdc);
1786 EndPath(graphics->hdc);
1787 SelectObject(graphics->hdc, brush->gdibrush);
1788 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1790 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1792 RestoreDC(graphics->hdc, save_state);
1794 return Ok;
1797 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
1798 INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1800 return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1803 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
1804 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
1806 INT save_state;
1807 GpPointF *ptf = NULL;
1808 POINT *pti = NULL;
1809 GpStatus retval = Ok;
1811 if(!graphics || !brush || !points || !count)
1812 return InvalidParameter;
1814 ptf = GdipAlloc(count * sizeof(GpPointF));
1815 pti = GdipAlloc(count * sizeof(POINT));
1816 if(!ptf || !pti){
1817 retval = OutOfMemory;
1818 goto end;
1821 memcpy(ptf, points, count * sizeof(GpPointF));
1823 save_state = SaveDC(graphics->hdc);
1824 EndPath(graphics->hdc);
1825 SelectObject(graphics->hdc, brush->gdibrush);
1826 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1827 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1828 : WINDING));
1830 transform_and_round_points(graphics, pti, ptf, count);
1831 Polygon(graphics->hdc, pti, count);
1833 RestoreDC(graphics->hdc, save_state);
1835 end:
1836 GdipFree(ptf);
1837 GdipFree(pti);
1839 return retval;
1842 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
1843 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
1845 INT save_state, i;
1846 GpPointF *ptf = NULL;
1847 POINT *pti = NULL;
1848 GpStatus retval = Ok;
1850 if(!graphics || !brush || !points || !count)
1851 return InvalidParameter;
1853 ptf = GdipAlloc(count * sizeof(GpPointF));
1854 pti = GdipAlloc(count * sizeof(POINT));
1855 if(!ptf || !pti){
1856 retval = OutOfMemory;
1857 goto end;
1860 for(i = 0; i < count; i ++){
1861 ptf[i].X = (REAL) points[i].X;
1862 ptf[i].Y = (REAL) points[i].Y;
1865 save_state = SaveDC(graphics->hdc);
1866 EndPath(graphics->hdc);
1867 SelectObject(graphics->hdc, brush->gdibrush);
1868 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1869 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1870 : WINDING));
1872 transform_and_round_points(graphics, pti, ptf, count);
1873 Polygon(graphics->hdc, pti, count);
1875 RestoreDC(graphics->hdc, save_state);
1877 end:
1878 GdipFree(ptf);
1879 GdipFree(pti);
1881 return retval;
1884 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
1885 REAL x, REAL y, REAL width, REAL height)
1887 INT save_state;
1888 GpPointF ptf[4];
1889 POINT pti[4];
1891 if(!graphics || !brush)
1892 return InvalidParameter;
1894 ptf[0].X = x;
1895 ptf[0].Y = y;
1896 ptf[1].X = x + width;
1897 ptf[1].Y = y;
1898 ptf[2].X = x + width;
1899 ptf[2].Y = y + height;
1900 ptf[3].X = x;
1901 ptf[3].Y = y + height;
1903 save_state = SaveDC(graphics->hdc);
1904 EndPath(graphics->hdc);
1905 SelectObject(graphics->hdc, brush->gdibrush);
1906 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1908 transform_and_round_points(graphics, pti, ptf, 4);
1910 Polygon(graphics->hdc, pti, 4);
1912 RestoreDC(graphics->hdc, save_state);
1914 return Ok;
1917 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
1918 INT x, INT y, INT width, INT height)
1920 INT save_state;
1921 GpPointF ptf[4];
1922 POINT pti[4];
1924 if(!graphics || !brush)
1925 return InvalidParameter;
1927 ptf[0].X = x;
1928 ptf[0].Y = y;
1929 ptf[1].X = x + width;
1930 ptf[1].Y = y;
1931 ptf[2].X = x + width;
1932 ptf[2].Y = y + height;
1933 ptf[3].X = x;
1934 ptf[3].Y = y + height;
1936 save_state = SaveDC(graphics->hdc);
1937 EndPath(graphics->hdc);
1938 SelectObject(graphics->hdc, brush->gdibrush);
1939 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1941 transform_and_round_points(graphics, pti, ptf, 4);
1943 Polygon(graphics->hdc, pti, 4);
1945 RestoreDC(graphics->hdc, save_state);
1947 return Ok;
1950 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
1951 INT count)
1953 GpStatus ret;
1954 INT i;
1956 if(!rects)
1957 return InvalidParameter;
1959 for(i = 0; i < count; i++){
1960 ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
1961 if(ret != Ok) return ret;
1964 return Ok;
1967 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
1968 INT count)
1970 GpRectF *rectsF;
1971 GpStatus ret;
1972 INT i;
1974 if(!rects || count <= 0)
1975 return InvalidParameter;
1977 rectsF = GdipAlloc(sizeof(GpRectF)*count);
1978 if(!rectsF)
1979 return OutOfMemory;
1981 for(i = 0; i < count; i++){
1982 rectsF[i].X = (REAL)rects[i].X;
1983 rectsF[i].Y = (REAL)rects[i].Y;
1984 rectsF[i].X = (REAL)rects[i].Width;
1985 rectsF[i].Height = (REAL)rects[i].Height;
1988 ret = GdipFillRectangles(graphics,brush,rectsF,count);
1989 GdipFree(rectsF);
1991 return ret;
1994 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
1996 static int calls;
1998 if(!graphics)
1999 return InvalidParameter;
2001 if(!(calls++))
2002 FIXME("not implemented\n");
2004 return NotImplemented;
2007 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
2008 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
2009 CompositingMode *mode)
2011 if(!graphics || !mode)
2012 return InvalidParameter;
2014 *mode = graphics->compmode;
2016 return Ok;
2019 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
2020 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
2021 CompositingQuality *quality)
2023 if(!graphics || !quality)
2024 return InvalidParameter;
2026 *quality = graphics->compqual;
2028 return Ok;
2031 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
2032 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
2033 InterpolationMode *mode)
2035 if(!graphics || !mode)
2036 return InvalidParameter;
2038 *mode = graphics->interpolation;
2040 return Ok;
2043 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
2045 if(!graphics || !scale)
2046 return InvalidParameter;
2048 *scale = graphics->scale;
2050 return Ok;
2053 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
2055 if(!graphics || !unit)
2056 return InvalidParameter;
2058 *unit = graphics->unit;
2060 return Ok;
2063 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
2064 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
2065 *mode)
2067 if(!graphics || !mode)
2068 return InvalidParameter;
2070 *mode = graphics->pixeloffset;
2072 return Ok;
2075 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
2076 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
2078 if(!graphics || !mode)
2079 return InvalidParameter;
2081 *mode = graphics->smoothing;
2083 return Ok;
2086 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
2087 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
2088 TextRenderingHint *hint)
2090 if(!graphics || !hint)
2091 return InvalidParameter;
2093 *hint = graphics->texthint;
2095 return Ok;
2098 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
2100 if(!graphics || !matrix)
2101 return InvalidParameter;
2103 *matrix = *graphics->worldtrans;
2104 return Ok;
2107 /* Find the smallest rectangle that bounds the text when it is printed in rect
2108 * according to the format options listed in format. If rect has 0 width and
2109 * height, then just find the smallest rectangle that bounds the text when it's
2110 * printed at location (rect->X, rect-Y). */
2111 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
2112 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
2113 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
2114 INT *codepointsfitted, INT *linesfilled)
2116 HFONT oldfont;
2117 WCHAR* stringdup;
2118 INT sum = 0, height = 0, fit, fitcpy, max_width = 0, i, j, lret, nwidth,
2119 nheight;
2120 SIZE size;
2122 if(!graphics || !string || !font || !rect)
2123 return InvalidParameter;
2125 if(codepointsfitted || linesfilled){
2126 FIXME("not implemented for given parameters\n");
2127 return NotImplemented;
2130 if(format)
2131 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
2133 if(length == -1) length = lstrlenW(string);
2135 stringdup = GdipAlloc(length * sizeof(WCHAR));
2136 if(!stringdup) return OutOfMemory;
2138 oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
2139 nwidth = roundr(rect->Width);
2140 nheight = roundr(rect->Height);
2142 if((nwidth == 0) && (nheight == 0))
2143 nwidth = nheight = INT_MAX;
2145 for(i = 0, j = 0; i < length; i++){
2146 if(!isprintW(string[i]) && (string[i] != '\n'))
2147 continue;
2149 stringdup[j] = string[i];
2150 j++;
2153 stringdup[j] = 0;
2154 length = j;
2156 while(sum < length){
2157 GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
2158 nwidth, &fit, NULL, &size);
2159 fitcpy = fit;
2161 if(fit == 0)
2162 break;
2164 for(lret = 0; lret < fit; lret++)
2165 if(*(stringdup + sum + lret) == '\n')
2166 break;
2168 /* Line break code (may look strange, but it imitates windows). */
2169 if(lret < fit)
2170 fit = lret; /* this is not an off-by-one error */
2171 else if(fit < (length - sum)){
2172 if(*(stringdup + sum + fit) == ' ')
2173 while(*(stringdup + sum + fit) == ' ')
2174 fit++;
2175 else
2176 while(*(stringdup + sum + fit - 1) != ' '){
2177 fit--;
2179 if(*(stringdup + sum + fit) == '\t')
2180 break;
2182 if(fit == 0){
2183 fit = fitcpy;
2184 break;
2189 GetTextExtentExPointW(graphics->hdc, stringdup + sum, fit,
2190 nwidth, &j, NULL, &size);
2192 sum += fit + (lret < fitcpy ? 1 : 0);
2193 height += size.cy;
2194 max_width = max(max_width, size.cx);
2196 if(height > nheight)
2197 break;
2199 /* Stop if this was a linewrap (but not if it was a linebreak). */
2200 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
2201 break;
2204 bounds->X = rect->X;
2205 bounds->Y = rect->Y;
2206 bounds->Width = (REAL)max_width;
2207 bounds->Height = (REAL) min(height, nheight);
2209 GdipFree(stringdup);
2210 DeleteObject(SelectObject(graphics->hdc, oldfont));
2212 return Ok;
2215 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
2217 static int calls;
2219 if(!graphics)
2220 return InvalidParameter;
2222 if(!(calls++))
2223 FIXME("graphics state not implemented\n");
2225 return NotImplemented;
2228 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
2229 GpMatrixOrder order)
2231 if(!graphics)
2232 return InvalidParameter;
2234 return GdipRotateMatrix(graphics->worldtrans, angle, order);
2237 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
2239 static int calls;
2241 if(!graphics || !state)
2242 return InvalidParameter;
2244 if(!(calls++))
2245 FIXME("graphics state not implemented\n");
2247 return NotImplemented;
2250 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
2251 REAL sy, GpMatrixOrder order)
2253 if(!graphics)
2254 return InvalidParameter;
2256 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
2259 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
2260 CompositingMode mode)
2262 if(!graphics)
2263 return InvalidParameter;
2265 graphics->compmode = mode;
2267 return Ok;
2270 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
2271 CompositingQuality quality)
2273 if(!graphics)
2274 return InvalidParameter;
2276 graphics->compqual = quality;
2278 return Ok;
2281 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
2282 InterpolationMode mode)
2284 if(!graphics)
2285 return InvalidParameter;
2287 graphics->interpolation = mode;
2289 return Ok;
2292 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
2294 if(!graphics || (scale <= 0.0))
2295 return InvalidParameter;
2297 graphics->scale = scale;
2299 return Ok;
2302 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
2304 if(!graphics || (unit == UnitWorld))
2305 return InvalidParameter;
2307 graphics->unit = unit;
2309 return Ok;
2312 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
2313 mode)
2315 if(!graphics)
2316 return InvalidParameter;
2318 graphics->pixeloffset = mode;
2320 return Ok;
2323 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
2325 if(!graphics)
2326 return InvalidParameter;
2328 graphics->smoothing = mode;
2330 return Ok;
2333 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
2334 TextRenderingHint hint)
2336 if(!graphics)
2337 return InvalidParameter;
2339 graphics->texthint = hint;
2341 return Ok;
2344 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
2346 if(!graphics || !matrix)
2347 return InvalidParameter;
2349 GdipDeleteMatrix(graphics->worldtrans);
2350 return GdipCloneMatrix(matrix, &graphics->worldtrans);
2353 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
2354 REAL dy, GpMatrixOrder order)
2356 if(!graphics)
2357 return InvalidParameter;
2359 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
2362 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
2363 INT width, INT height,
2364 CombineMode combineMode)
2366 static int calls;
2368 if(!(calls++))
2369 FIXME("not implemented\n");
2371 return NotImplemented;
2374 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
2375 CombineMode combineMode)
2377 static int calls;
2379 if(!(calls++))
2380 FIXME("not implemented\n");
2382 return NotImplemented;
2385 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpGraphics *graphics,
2386 UINT limitDpi)
2388 static int calls;
2390 if(!(calls++))
2391 FIXME("not implemented\n");
2393 return NotImplemented;
2396 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
2397 INT count)
2399 INT save_state;
2400 POINT *pti;
2402 if(!graphics || !pen || count<=0)
2403 return InvalidParameter;
2405 pti = GdipAlloc(sizeof(POINT) * count);
2407 save_state = prepare_dc(graphics, pen);
2408 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2410 transform_and_round_points(graphics, pti, (GpPointF*)points, count);
2411 Polygon(graphics->hdc, pti, count);
2413 restore_dc(graphics, save_state);
2414 GdipFree(pti);
2416 return Ok;
2419 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
2420 INT count)
2422 GpStatus ret;
2423 GpPointF *ptf;
2424 INT i;
2426 if(count<=0) return InvalidParameter;
2427 ptf = GdipAlloc(sizeof(GpPointF) * count);
2429 for(i = 0;i < count; i++){
2430 ptf[i].X = (REAL)points[i].X;
2431 ptf[i].Y = (REAL)points[i].Y;
2434 ret = GdipDrawPolygon(graphics,pen,ptf,count);
2435 GdipFree(ptf);
2437 return ret;
2440 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
2442 if(!graphics || !dpi)
2443 return InvalidParameter;
2445 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
2447 return Ok;
2450 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
2452 if(!graphics || !dpi)
2453 return InvalidParameter;
2455 *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
2457 return Ok;
2460 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
2461 GpMatrixOrder order)
2463 GpMatrix m;
2464 GpStatus ret;
2466 if(!graphics || !matrix)
2467 return InvalidParameter;
2469 m = *(graphics->worldtrans);
2471 ret = GdipMultiplyMatrix(&m, (GpMatrix*)matrix, order);
2472 if(ret == Ok)
2473 *(graphics->worldtrans) = m;
2475 return ret;
2478 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
2480 FIXME("(%p, %p): stub\n", graphics, hdc);
2482 *hdc = NULL;
2483 return NotImplemented;
2486 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
2488 FIXME("(%p, %p): stub\n", graphics, hdc);
2490 return NotImplemented;
2493 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
2495 FIXME("(%p, %p): stub\n", graphics, region);
2497 return NotImplemented;