push 86212bdcde7bcfde2e129f3537a0607fbbd70bde
[wine/hacks.git] / dlls / gdiplus / graphics.c
blob65e715196a8cdc0edaaf1d936be49619e7f09073
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 Polyline(graphics->hdc, pti, count);
510 end:
511 GdipFree(pti);
512 GdipFree(ptcopy);
514 return status;
517 /* Conducts a linear search to find the bezier points that will back off
518 * the endpoint of the curve by a distance of amt. Linear search works
519 * better than binary in this case because there are multiple solutions,
520 * and binary searches often find a bad one. I don't think this is what
521 * Windows does but short of rendering the bezier without GDI's help it's
522 * the best we can do. If rev then work from the start of the passed points
523 * instead of the end. */
524 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
526 GpPointF origpt[4];
527 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
528 INT i, first = 0, second = 1, third = 2, fourth = 3;
530 if(rev){
531 first = 3;
532 second = 2;
533 third = 1;
534 fourth = 0;
537 origx = pt[fourth].X;
538 origy = pt[fourth].Y;
539 memcpy(origpt, pt, sizeof(GpPointF) * 4);
541 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
542 /* reset bezier points to original values */
543 memcpy(pt, origpt, sizeof(GpPointF) * 4);
544 /* Perform magic on bezier points. Order is important here.*/
545 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
546 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
547 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
548 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
549 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
550 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
552 dx = pt[fourth].X - origx;
553 dy = pt[fourth].Y - origy;
555 diff = sqrt(dx * dx + dy * dy);
556 percent += 0.0005 * amt;
560 /* Draws bezier curves between given points, and if caps is true then draws an
561 * endcap at the end of the last line. */
562 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
563 GDIPCONST GpPointF * pt, INT count, BOOL caps)
565 POINT *pti;
566 GpPointF *ptcopy;
567 GpStatus status = GenericError;
569 if(!count)
570 return Ok;
572 pti = GdipAlloc(count * sizeof(POINT));
573 ptcopy = GdipAlloc(count * sizeof(GpPointF));
575 if(!pti || !ptcopy){
576 status = OutOfMemory;
577 goto end;
580 memcpy(ptcopy, pt, count * sizeof(GpPointF));
582 if(caps){
583 if(pen->endcap == LineCapArrowAnchor)
584 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
585 else if((pen->endcap == LineCapCustom) && pen->customend)
586 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
587 FALSE);
589 if(pen->startcap == LineCapArrowAnchor)
590 shorten_bezier_amt(ptcopy, pen->width, TRUE);
591 else if((pen->startcap == LineCapCustom) && pen->customstart)
592 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
594 /* the direction of the line cap is parallel to the direction at the
595 * end of the bezier (which, if it has been shortened, is not the same
596 * as the direction from pt[count-2] to pt[count-1]) */
597 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
598 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
599 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
600 pt[count - 1].X, pt[count - 1].Y);
602 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
603 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
604 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
607 transform_and_round_points(graphics, pti, ptcopy, count);
609 PolyBezier(graphics->hdc, pti, count);
611 status = Ok;
613 end:
614 GdipFree(pti);
615 GdipFree(ptcopy);
617 return status;
620 /* Draws a combination of bezier curves and lines between points. */
621 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
622 GDIPCONST BYTE * types, INT count, BOOL caps)
624 POINT *pti = GdipAlloc(count * sizeof(POINT));
625 BYTE *tp = GdipAlloc(count);
626 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
627 INT i, j;
628 GpStatus status = GenericError;
630 if(!count){
631 status = Ok;
632 goto end;
634 if(!pti || !tp || !ptcopy){
635 status = OutOfMemory;
636 goto end;
639 for(i = 1; i < count; i++){
640 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
641 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
642 || !(types[i + 1] & PathPointTypeBezier)){
643 ERR("Bad bezier points\n");
644 goto end;
646 i += 2;
650 memcpy(ptcopy, pt, count * sizeof(GpPointF));
652 /* If we are drawing caps, go through the points and adjust them accordingly,
653 * and draw the caps. */
654 if(caps){
655 switch(types[count - 1] & PathPointTypePathTypeMask){
656 case PathPointTypeBezier:
657 if(pen->endcap == LineCapArrowAnchor)
658 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
659 else if((pen->endcap == LineCapCustom) && pen->customend)
660 shorten_bezier_amt(&ptcopy[count - 4],
661 pen->width * pen->customend->inset, FALSE);
663 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
664 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
665 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
666 pt[count - 1].X, pt[count - 1].Y);
668 break;
669 case PathPointTypeLine:
670 if(pen->endcap == LineCapArrowAnchor)
671 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
672 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
673 pen->width);
674 else if((pen->endcap == LineCapCustom) && pen->customend)
675 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
676 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
677 pen->customend->inset * pen->width);
679 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
680 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
681 pt[count - 1].Y);
683 break;
684 default:
685 ERR("Bad path last point\n");
686 goto end;
689 /* Find start of points */
690 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
691 == PathPointTypeStart); j++);
693 switch(types[j] & PathPointTypePathTypeMask){
694 case PathPointTypeBezier:
695 if(pen->startcap == LineCapArrowAnchor)
696 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
697 else if((pen->startcap == LineCapCustom) && pen->customstart)
698 shorten_bezier_amt(&ptcopy[j - 1],
699 pen->width * pen->customstart->inset, TRUE);
701 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
702 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
703 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
704 pt[j - 1].X, pt[j - 1].Y);
706 break;
707 case PathPointTypeLine:
708 if(pen->startcap == LineCapArrowAnchor)
709 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
710 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
711 pen->width);
712 else if((pen->startcap == LineCapCustom) && pen->customstart)
713 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
714 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
715 pen->customstart->inset * pen->width);
717 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
718 pt[j].X, pt[j].Y, pt[j - 1].X,
719 pt[j - 1].Y);
721 break;
722 default:
723 ERR("Bad path points\n");
724 goto end;
728 transform_and_round_points(graphics, pti, ptcopy, count);
730 for(i = 0; i < count; i++){
731 tp[i] = convert_path_point_type(types[i]);
734 PolyDraw(graphics->hdc, pti, tp, count);
736 status = Ok;
738 end:
739 GdipFree(pti);
740 GdipFree(ptcopy);
741 GdipFree(tp);
743 return status;
746 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
748 return GdipCreateFromHDC2(hdc, NULL, graphics);
751 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
753 GpStatus retval;
755 if(hDevice != NULL) {
756 FIXME("Don't know how to hadle parameter hDevice\n");
757 return NotImplemented;
760 if(hdc == NULL)
761 return OutOfMemory;
763 if(graphics == NULL)
764 return InvalidParameter;
766 *graphics = GdipAlloc(sizeof(GpGraphics));
767 if(!*graphics) return OutOfMemory;
769 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
770 GdipFree(*graphics);
771 return retval;
774 (*graphics)->hdc = hdc;
775 (*graphics)->hwnd = NULL;
776 (*graphics)->smoothing = SmoothingModeDefault;
777 (*graphics)->compqual = CompositingQualityDefault;
778 (*graphics)->interpolation = InterpolationModeDefault;
779 (*graphics)->pixeloffset = PixelOffsetModeDefault;
780 (*graphics)->compmode = CompositingModeSourceOver;
781 (*graphics)->unit = UnitDisplay;
782 (*graphics)->scale = 1.0;
784 return Ok;
787 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
789 GpStatus ret;
791 if((ret = GdipCreateFromHDC(GetDC(hwnd), graphics)) != Ok)
792 return ret;
794 (*graphics)->hwnd = hwnd;
796 return Ok;
799 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
800 GpMetafile **metafile)
802 static int calls;
804 if(!hemf || !metafile)
805 return InvalidParameter;
807 if(!(calls++))
808 FIXME("not implemented\n");
810 return NotImplemented;
813 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
814 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
816 IStream *stream = NULL;
817 UINT read;
818 BYTE* copy;
819 HENHMETAFILE hemf;
820 GpStatus retval = GenericError;
822 if(!hwmf || !metafile || !placeable)
823 return InvalidParameter;
825 *metafile = NULL;
826 read = GetMetaFileBitsEx(hwmf, 0, NULL);
827 if(!read)
828 return GenericError;
829 copy = GdipAlloc(read);
830 GetMetaFileBitsEx(hwmf, read, copy);
832 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
833 GdipFree(copy);
835 read = GetEnhMetaFileBits(hemf, 0, NULL);
836 copy = GdipAlloc(read);
837 GetEnhMetaFileBits(hemf, read, copy);
838 DeleteEnhMetaFile(hemf);
840 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
841 ERR("could not make stream\n");
842 GdipFree(copy);
843 goto err;
846 *metafile = GdipAlloc(sizeof(GpMetafile));
847 if(!*metafile){
848 retval = OutOfMemory;
849 goto err;
852 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
853 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
854 goto err;
857 (*metafile)->image.type = ImageTypeMetafile;
858 (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
859 (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Right) / ((REAL) placeable->Inch);
860 (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
861 - placeable->BoundingBox.Left)) / ((REAL) placeable->Inch);
862 (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
863 - placeable->BoundingBox.Top)) / ((REAL) placeable->Inch);
864 (*metafile)->unit = UnitInch;
866 if(delete)
867 DeleteMetaFile(hwmf);
869 return Ok;
871 err:
872 GdipFree(*metafile);
873 IStream_Release(stream);
874 return retval;
877 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
878 UINT access, IStream **stream)
880 DWORD dwMode;
881 HRESULT ret;
883 if(!stream || !filename)
884 return InvalidParameter;
886 if(access & GENERIC_WRITE)
887 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
888 else if(access & GENERIC_READ)
889 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
890 else
891 return InvalidParameter;
893 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
895 return hresult_to_status(ret);
898 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
900 if(!graphics) return InvalidParameter;
901 if(graphics->hwnd)
902 ReleaseDC(graphics->hwnd, graphics->hdc);
904 GdipDeleteMatrix(graphics->worldtrans);
905 HeapFree(GetProcessHeap(), 0, graphics);
907 return Ok;
910 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
911 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
913 INT save_state, num_pts;
914 GpPointF points[MAX_ARC_PTS];
915 GpStatus retval;
917 if(!graphics || !pen)
918 return InvalidParameter;
920 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
922 save_state = prepare_dc(graphics, pen);
924 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
926 restore_dc(graphics, save_state);
928 return retval;
931 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
932 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
934 INT save_state;
935 GpPointF pt[4];
936 GpStatus retval;
938 if(!graphics || !pen)
939 return InvalidParameter;
941 pt[0].X = x1;
942 pt[0].Y = y1;
943 pt[1].X = x2;
944 pt[1].Y = y2;
945 pt[2].X = x3;
946 pt[2].Y = y3;
947 pt[3].X = x4;
948 pt[3].Y = y4;
950 save_state = prepare_dc(graphics, pen);
952 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
954 restore_dc(graphics, save_state);
956 return retval;
959 /* Approximates cardinal spline with Bezier curves. */
960 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
961 GDIPCONST GpPointF *points, INT count, REAL tension)
963 /* PolyBezier expects count*3-2 points. */
964 INT i, len_pt = count*3-2, save_state;
965 GpPointF *pt;
966 REAL x1, x2, y1, y2;
967 GpStatus retval;
969 if(!graphics || !pen)
970 return InvalidParameter;
972 pt = GdipAlloc(len_pt * sizeof(GpPointF));
973 tension = tension * TENSION_CONST;
975 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
976 tension, &x1, &y1);
978 pt[0].X = points[0].X;
979 pt[0].Y = points[0].Y;
980 pt[1].X = x1;
981 pt[1].Y = y1;
983 for(i = 0; i < count-2; i++){
984 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
986 pt[3*i+2].X = x1;
987 pt[3*i+2].Y = y1;
988 pt[3*i+3].X = points[i+1].X;
989 pt[3*i+3].Y = points[i+1].Y;
990 pt[3*i+4].X = x2;
991 pt[3*i+4].Y = y2;
994 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
995 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
997 pt[len_pt-2].X = x1;
998 pt[len_pt-2].Y = y1;
999 pt[len_pt-1].X = points[count-1].X;
1000 pt[len_pt-1].Y = points[count-1].Y;
1002 save_state = prepare_dc(graphics, pen);
1004 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
1006 GdipFree(pt);
1007 restore_dc(graphics, save_state);
1009 return retval;
1012 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
1013 INT y)
1015 UINT width, height, srcw, srch;
1017 if(!graphics || !image)
1018 return InvalidParameter;
1020 GdipGetImageWidth(image, &width);
1021 GdipGetImageHeight(image, &height);
1023 srcw = width * (((REAL) INCH_HIMETRIC) /
1024 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX)));
1025 srch = height * (((REAL) INCH_HIMETRIC) /
1026 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY)));
1028 if(image->type != ImageTypeMetafile){
1029 y += height;
1030 height *= -1;
1033 IPicture_Render(image->picture, graphics->hdc, x, y, width, height,
1034 0, 0, srcw, srch, NULL);
1036 return Ok;
1039 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1040 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1041 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1042 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1043 DrawImageAbort callback, VOID * callbackData)
1045 GpPointF ptf[3];
1046 POINT pti[3];
1047 REAL dx, dy;
1049 TRACE("%p %p %p %d %f %f %f %f %d %p %p %p\n", graphics, image, points, count,
1050 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1051 callbackData);
1053 if(!graphics || !image || !points || !imageAttributes || count != 3)
1054 return InvalidParameter;
1056 if(srcUnit == UnitInch)
1057 dx = dy = (REAL) INCH_HIMETRIC;
1058 else if(srcUnit == UnitPixel){
1059 dx = ((REAL) INCH_HIMETRIC) /
1060 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1061 dy = ((REAL) INCH_HIMETRIC) /
1062 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1064 else
1065 return NotImplemented;
1067 memcpy(ptf, points, 3 * sizeof(GpPointF));
1068 transform_and_round_points(graphics, pti, ptf, 3);
1070 /* IPicture renders bitmaps with the y-axis reversed
1071 * FIXME: flipping for unknown image type might not be correct. */
1072 if(image->type != ImageTypeMetafile){
1073 INT temp;
1074 temp = pti[0].y;
1075 pti[0].y = pti[2].y;
1076 pti[2].y = temp;
1079 if(IPicture_Render(image->picture, graphics->hdc,
1080 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1081 srcx * dx, srcy * dy,
1082 srcwidth * dx, srcheight * dy,
1083 NULL) != S_OK){
1084 if(callback)
1085 callback(callbackData);
1086 return GenericError;
1089 return Ok;
1092 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
1093 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
1094 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
1095 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
1096 VOID * callbackData)
1098 GpPointF points[3];
1100 points[0].X = dstx;
1101 points[0].Y = dsty;
1102 points[1].X = dstx + dstwidth;
1103 points[1].Y = dsty;
1104 points[2].X = dstx;
1105 points[2].Y = dsty + dstheight;
1107 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1108 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1111 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
1112 REAL y1, REAL x2, REAL y2)
1114 INT save_state;
1115 GpPointF pt[2];
1116 GpStatus retval;
1118 if(!pen || !graphics)
1119 return InvalidParameter;
1121 pt[0].X = x1;
1122 pt[0].Y = y1;
1123 pt[1].X = x2;
1124 pt[1].Y = y2;
1126 save_state = prepare_dc(graphics, pen);
1128 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1130 restore_dc(graphics, save_state);
1132 return retval;
1135 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
1136 INT y1, INT x2, INT y2)
1138 INT save_state;
1139 GpPointF pt[2];
1140 GpStatus retval;
1142 if(!pen || !graphics)
1143 return InvalidParameter;
1145 pt[0].X = (REAL)x1;
1146 pt[0].Y = (REAL)y1;
1147 pt[1].X = (REAL)x2;
1148 pt[1].Y = (REAL)y2;
1150 save_state = prepare_dc(graphics, pen);
1152 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1154 restore_dc(graphics, save_state);
1156 return retval;
1159 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
1160 GpPointF *points, INT count)
1162 INT save_state;
1163 GpStatus retval;
1165 if(!pen || !graphics || (count < 2))
1166 return InvalidParameter;
1168 save_state = prepare_dc(graphics, pen);
1170 retval = draw_polyline(graphics, pen, points, count, TRUE);
1172 restore_dc(graphics, save_state);
1174 return retval;
1177 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
1179 INT save_state;
1180 GpStatus retval;
1182 if(!pen || !graphics)
1183 return InvalidParameter;
1185 save_state = prepare_dc(graphics, pen);
1187 retval = draw_poly(graphics, pen, path->pathdata.Points,
1188 path->pathdata.Types, path->pathdata.Count, TRUE);
1190 restore_dc(graphics, save_state);
1192 return retval;
1195 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
1196 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1198 INT save_state;
1200 if(!graphics || !pen)
1201 return InvalidParameter;
1203 save_state = prepare_dc(graphics, pen);
1204 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1206 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1208 restore_dc(graphics, save_state);
1210 return Ok;
1213 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
1214 INT y, INT width, INT height)
1216 INT save_state;
1217 GpPointF ptf[4];
1218 POINT pti[4];
1220 if(!pen || !graphics)
1221 return InvalidParameter;
1223 ptf[0].X = x;
1224 ptf[0].Y = y;
1225 ptf[1].X = x + width;
1226 ptf[1].Y = y;
1227 ptf[2].X = x + width;
1228 ptf[2].Y = y + height;
1229 ptf[3].X = x;
1230 ptf[3].Y = y + height;
1232 save_state = prepare_dc(graphics, pen);
1233 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1235 transform_and_round_points(graphics, pti, ptf, 4);
1236 Polygon(graphics->hdc, pti, 4);
1238 restore_dc(graphics, save_state);
1240 return Ok;
1243 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
1244 GDIPCONST GpRectF* rects, INT count)
1246 GpPointF *ptf;
1247 POINT *pti;
1248 INT save_state, i;
1250 if(!graphics || !pen || !rects || count < 1)
1251 return InvalidParameter;
1253 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
1254 pti = GdipAlloc(4 * count * sizeof(POINT));
1256 if(!ptf || !pti){
1257 GdipFree(ptf);
1258 GdipFree(pti);
1259 return OutOfMemory;
1262 for(i = 0; i < count; i++){
1263 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
1264 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
1265 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
1266 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
1269 save_state = prepare_dc(graphics, pen);
1270 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1272 transform_and_round_points(graphics, pti, ptf, 4 * count);
1274 for(i = 0; i < count; i++)
1275 Polygon(graphics->hdc, &pti[4 * i], 4);
1277 restore_dc(graphics, save_state);
1279 GdipFree(ptf);
1280 GdipFree(pti);
1282 return Ok;
1285 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
1286 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
1287 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
1289 HRGN rgn = NULL;
1290 HFONT gdifont;
1291 LOGFONTW lfw;
1292 TEXTMETRICW textmet;
1293 GpPointF pt[2], rectcpy[4];
1294 POINT corners[4];
1295 WCHAR* stringdup;
1296 REAL angle, ang_cos, ang_sin, rel_width, rel_height;
1297 INT sum = 0, height = 0, fit, fitcpy, save_state, i, j, lret, nwidth,
1298 nheight;
1299 SIZE size;
1300 RECT drawcoord;
1302 if(!graphics || !string || !font || !brush || !rect)
1303 return InvalidParameter;
1305 if((brush->bt != BrushTypeSolidColor)){
1306 FIXME("not implemented for given parameters\n");
1307 return NotImplemented;
1310 if(format)
1311 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
1313 if(length == -1) length = lstrlenW(string);
1315 stringdup = GdipAlloc(length * sizeof(WCHAR));
1316 if(!stringdup) return OutOfMemory;
1318 save_state = SaveDC(graphics->hdc);
1319 SetBkMode(graphics->hdc, TRANSPARENT);
1320 SetTextColor(graphics->hdc, brush->lb.lbColor);
1322 rectcpy[3].X = rectcpy[0].X = rect->X;
1323 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
1324 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
1325 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
1326 transform_and_round_points(graphics, corners, rectcpy, 4);
1328 if(roundr(rect->Width) == 0 && roundr(rect->Height) == 0){
1329 rel_width = rel_height = 1.0;
1330 nwidth = nheight = INT_MAX;
1332 else{
1333 rel_width = sqrt((corners[1].x - corners[0].x) * (corners[1].x - corners[0].x) +
1334 (corners[1].y - corners[0].y) * (corners[1].y - corners[0].y))
1335 / rect->Width;
1336 rel_height = sqrt((corners[2].x - corners[1].x) * (corners[2].x - corners[1].x) +
1337 (corners[2].y - corners[1].y) * (corners[2].y - corners[1].y))
1338 / rect->Height;
1340 nwidth = roundr(rel_width * rect->Width);
1341 nheight = roundr(rel_height * rect->Height);
1342 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
1343 SelectClipRgn(graphics->hdc, rgn);
1346 /* Use gdi to find the font, then perform transformations on it (height,
1347 * width, angle). */
1348 SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
1349 GetTextMetricsW(graphics->hdc, &textmet);
1350 memcpy(&lfw, &font->lfw, sizeof(LOGFONTW));
1352 lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
1353 lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
1355 pt[0].X = 0.0;
1356 pt[0].Y = 0.0;
1357 pt[1].X = 1.0;
1358 pt[1].Y = 0.0;
1359 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
1360 angle = gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
1361 ang_cos = cos(angle);
1362 ang_sin = sin(angle);
1363 lfw.lfEscapement = lfw.lfOrientation = -roundr((angle / M_PI) * 1800.0);
1365 gdifont = CreateFontIndirectW(&lfw);
1366 DeleteObject(SelectObject(graphics->hdc, CreateFontIndirectW(&lfw)));
1368 for(i = 0, j = 0; i < length; i++){
1369 if(!isprintW(string[i]) && (string[i] != '\n'))
1370 continue;
1372 stringdup[j] = string[i];
1373 j++;
1376 stringdup[j] = 0;
1377 length = j;
1379 while(sum < length){
1380 drawcoord.left = corners[0].x + roundr(ang_sin * (REAL) height);
1381 drawcoord.top = corners[0].y + roundr(ang_cos * (REAL) height);
1383 GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
1384 nwidth, &fit, NULL, &size);
1385 fitcpy = fit;
1387 if(fit == 0){
1388 DrawTextW(graphics->hdc, stringdup + sum, 1, &drawcoord, DT_NOCLIP |
1389 DT_EXPANDTABS);
1390 break;
1393 for(lret = 0; lret < fit; lret++)
1394 if(*(stringdup + sum + lret) == '\n')
1395 break;
1397 /* Line break code (may look strange, but it imitates windows). */
1398 if(lret < fit)
1399 fit = lret; /* this is not an off-by-one error */
1400 else if(fit < (length - sum)){
1401 if(*(stringdup + sum + fit) == ' ')
1402 while(*(stringdup + sum + fit) == ' ')
1403 fit++;
1404 else
1405 while(*(stringdup + sum + fit - 1) != ' '){
1406 fit--;
1408 if(*(stringdup + sum + fit) == '\t')
1409 break;
1411 if(fit == 0){
1412 fit = fitcpy;
1413 break;
1417 DrawTextW(graphics->hdc, stringdup + sum, min(length - sum, fit),
1418 &drawcoord, DT_NOCLIP | DT_EXPANDTABS);
1420 sum += fit + (lret < fitcpy ? 1 : 0);
1421 height += size.cy;
1423 if(height > nheight)
1424 break;
1426 /* Stop if this was a linewrap (but not if it was a linebreak). */
1427 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
1428 break;
1431 GdipFree(stringdup);
1432 DeleteObject(rgn);
1433 DeleteObject(gdifont);
1435 RestoreDC(graphics->hdc, save_state);
1437 return Ok;
1440 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
1442 INT save_state;
1443 GpStatus retval;
1445 if(!brush || !graphics || !path)
1446 return InvalidParameter;
1448 save_state = SaveDC(graphics->hdc);
1449 EndPath(graphics->hdc);
1450 SelectObject(graphics->hdc, brush->gdibrush);
1451 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
1452 : WINDING));
1454 BeginPath(graphics->hdc);
1455 retval = draw_poly(graphics, NULL, path->pathdata.Points,
1456 path->pathdata.Types, path->pathdata.Count, FALSE);
1458 if(retval != Ok)
1459 goto end;
1461 EndPath(graphics->hdc);
1462 FillPath(graphics->hdc);
1464 retval = Ok;
1466 end:
1467 RestoreDC(graphics->hdc, save_state);
1469 return retval;
1472 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
1473 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1475 INT save_state;
1477 if(!graphics || !brush)
1478 return InvalidParameter;
1480 save_state = SaveDC(graphics->hdc);
1481 EndPath(graphics->hdc);
1482 SelectObject(graphics->hdc, brush->gdibrush);
1483 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1485 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1487 RestoreDC(graphics->hdc, save_state);
1489 return Ok;
1492 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
1493 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
1495 INT save_state;
1496 GpPointF *ptf = NULL;
1497 POINT *pti = NULL;
1498 GpStatus retval = Ok;
1500 if(!graphics || !brush || !points || !count)
1501 return InvalidParameter;
1503 ptf = GdipAlloc(count * sizeof(GpPointF));
1504 pti = GdipAlloc(count * sizeof(POINT));
1505 if(!ptf || !pti){
1506 retval = OutOfMemory;
1507 goto end;
1510 memcpy(ptf, points, count * sizeof(GpPointF));
1512 save_state = SaveDC(graphics->hdc);
1513 EndPath(graphics->hdc);
1514 SelectObject(graphics->hdc, brush->gdibrush);
1515 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1516 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1517 : WINDING));
1519 transform_and_round_points(graphics, pti, ptf, count);
1520 Polygon(graphics->hdc, pti, count);
1522 RestoreDC(graphics->hdc, save_state);
1524 end:
1525 GdipFree(ptf);
1526 GdipFree(pti);
1528 return retval;
1531 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
1532 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
1534 INT save_state, i;
1535 GpPointF *ptf = NULL;
1536 POINT *pti = NULL;
1537 GpStatus retval = Ok;
1539 if(!graphics || !brush || !points || !count)
1540 return InvalidParameter;
1542 ptf = GdipAlloc(count * sizeof(GpPointF));
1543 pti = GdipAlloc(count * sizeof(POINT));
1544 if(!ptf || !pti){
1545 retval = OutOfMemory;
1546 goto end;
1549 for(i = 0; i < count; i ++){
1550 ptf[i].X = (REAL) points[i].X;
1551 ptf[i].Y = (REAL) points[i].Y;
1554 save_state = SaveDC(graphics->hdc);
1555 EndPath(graphics->hdc);
1556 SelectObject(graphics->hdc, brush->gdibrush);
1557 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1558 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1559 : WINDING));
1561 transform_and_round_points(graphics, pti, ptf, count);
1562 Polygon(graphics->hdc, pti, count);
1564 RestoreDC(graphics->hdc, save_state);
1566 end:
1567 GdipFree(ptf);
1568 GdipFree(pti);
1570 return retval;
1573 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
1574 REAL x, REAL y, REAL width, REAL height)
1576 INT save_state;
1577 GpPointF ptf[4];
1578 POINT pti[4];
1580 if(!graphics || !brush)
1581 return InvalidParameter;
1583 ptf[0].X = x;
1584 ptf[0].Y = y;
1585 ptf[1].X = x + width;
1586 ptf[1].Y = y;
1587 ptf[2].X = x + width;
1588 ptf[2].Y = y + height;
1589 ptf[3].X = x;
1590 ptf[3].Y = y + height;
1592 save_state = SaveDC(graphics->hdc);
1593 EndPath(graphics->hdc);
1594 SelectObject(graphics->hdc, brush->gdibrush);
1595 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1597 transform_and_round_points(graphics, pti, ptf, 4);
1599 Polygon(graphics->hdc, pti, 4);
1601 RestoreDC(graphics->hdc, save_state);
1603 return Ok;
1606 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
1607 INT x, INT y, INT width, INT height)
1609 INT save_state;
1610 GpPointF ptf[4];
1611 POINT pti[4];
1613 if(!graphics || !brush)
1614 return InvalidParameter;
1616 ptf[0].X = x;
1617 ptf[0].Y = y;
1618 ptf[1].X = x + width;
1619 ptf[1].Y = y;
1620 ptf[2].X = x + width;
1621 ptf[2].Y = y + height;
1622 ptf[3].X = x;
1623 ptf[3].Y = y + height;
1625 save_state = SaveDC(graphics->hdc);
1626 EndPath(graphics->hdc);
1627 SelectObject(graphics->hdc, brush->gdibrush);
1628 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1630 transform_and_round_points(graphics, pti, ptf, 4);
1632 Polygon(graphics->hdc, pti, 4);
1634 RestoreDC(graphics->hdc, save_state);
1636 return Ok;
1639 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
1640 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
1641 CompositingMode *mode)
1643 if(!graphics || !mode)
1644 return InvalidParameter;
1646 *mode = graphics->compmode;
1648 return Ok;
1651 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
1652 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
1653 CompositingQuality *quality)
1655 if(!graphics || !quality)
1656 return InvalidParameter;
1658 *quality = graphics->compqual;
1660 return Ok;
1663 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
1664 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
1665 InterpolationMode *mode)
1667 if(!graphics || !mode)
1668 return InvalidParameter;
1670 *mode = graphics->interpolation;
1672 return Ok;
1675 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
1677 if(!graphics || !scale)
1678 return InvalidParameter;
1680 *scale = graphics->scale;
1682 return Ok;
1685 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
1687 if(!graphics || !unit)
1688 return InvalidParameter;
1690 *unit = graphics->unit;
1692 return Ok;
1695 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
1696 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1697 *mode)
1699 if(!graphics || !mode)
1700 return InvalidParameter;
1702 *mode = graphics->pixeloffset;
1704 return Ok;
1707 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
1708 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
1710 if(!graphics || !mode)
1711 return InvalidParameter;
1713 *mode = graphics->smoothing;
1715 return Ok;
1718 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
1719 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
1720 TextRenderingHint *hint)
1722 if(!graphics || !hint)
1723 return InvalidParameter;
1725 *hint = graphics->texthint;
1727 return Ok;
1730 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
1732 if(!graphics || !matrix)
1733 return InvalidParameter;
1735 memcpy(matrix, graphics->worldtrans, sizeof(GpMatrix));
1736 return Ok;
1739 /* Find the smallest rectangle that bounds the text when it is printed in rect
1740 * according to the format options listed in format. If rect has 0 width and
1741 * height, then just find the smallest rectangle that bounds the text when it's
1742 * printed at location (rect->X, rect-Y). */
1743 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
1744 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
1745 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
1746 INT *codepointsfitted, INT *linesfilled)
1748 HFONT oldfont;
1749 WCHAR* stringdup;
1750 INT sum = 0, height = 0, fit, fitcpy, max_width = 0, i, j, lret, nwidth,
1751 nheight;
1752 SIZE size;
1754 if(!graphics || !string || !font || !rect)
1755 return InvalidParameter;
1757 if(codepointsfitted || linesfilled){
1758 FIXME("not implemented for given parameters\n");
1759 return NotImplemented;
1762 if(format)
1763 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
1765 if(length == -1) length = lstrlenW(string);
1767 stringdup = GdipAlloc(length * sizeof(WCHAR));
1768 if(!stringdup) return OutOfMemory;
1770 oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
1771 nwidth = roundr(rect->Width);
1772 nheight = roundr(rect->Height);
1774 if((nwidth == 0) && (nheight == 0))
1775 nwidth = nheight = INT_MAX;
1777 for(i = 0, j = 0; i < length; i++){
1778 if(!isprintW(string[i]) && (string[i] != '\n'))
1779 continue;
1781 stringdup[j] = string[i];
1782 j++;
1785 stringdup[j] = 0;
1786 length = j;
1788 while(sum < length){
1789 GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
1790 nwidth, &fit, NULL, &size);
1791 fitcpy = fit;
1793 if(fit == 0)
1794 break;
1796 for(lret = 0; lret < fit; lret++)
1797 if(*(stringdup + sum + lret) == '\n')
1798 break;
1800 /* Line break code (may look strange, but it imitates windows). */
1801 if(lret < fit)
1802 fit = lret; /* this is not an off-by-one error */
1803 else if(fit < (length - sum)){
1804 if(*(stringdup + sum + fit) == ' ')
1805 while(*(stringdup + sum + fit) == ' ')
1806 fit++;
1807 else
1808 while(*(stringdup + sum + fit - 1) != ' '){
1809 fit--;
1811 if(*(stringdup + sum + fit) == '\t')
1812 break;
1814 if(fit == 0){
1815 fit = fitcpy;
1816 break;
1821 GetTextExtentExPointW(graphics->hdc, stringdup + sum, fit,
1822 nwidth, &j, NULL, &size);
1824 sum += fit + (lret < fitcpy ? 1 : 0);
1825 height += size.cy;
1826 max_width = max(max_width, size.cx);
1828 if(height > nheight)
1829 break;
1831 /* Stop if this was a linewrap (but not if it was a linebreak). */
1832 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
1833 break;
1836 bounds->X = rect->X;
1837 bounds->Y = rect->Y;
1838 bounds->Width = (REAL)max_width;
1839 bounds->Height = (REAL) min(height, nheight);
1841 GdipFree(stringdup);
1842 DeleteObject(SelectObject(graphics->hdc, oldfont));
1844 return Ok;
1847 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
1849 static int calls;
1851 if(!graphics)
1852 return InvalidParameter;
1854 if(!(calls++))
1855 FIXME("graphics state not implemented\n");
1857 return NotImplemented;
1860 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
1861 GpMatrixOrder order)
1863 if(!graphics)
1864 return InvalidParameter;
1866 return GdipRotateMatrix(graphics->worldtrans, angle, order);
1869 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
1871 static int calls;
1873 if(!graphics || !state)
1874 return InvalidParameter;
1876 if(!(calls++))
1877 FIXME("graphics state not implemented\n");
1879 return NotImplemented;
1882 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
1883 REAL sy, GpMatrixOrder order)
1885 if(!graphics)
1886 return InvalidParameter;
1888 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
1891 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
1892 CompositingMode mode)
1894 if(!graphics)
1895 return InvalidParameter;
1897 graphics->compmode = mode;
1899 return Ok;
1902 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
1903 CompositingQuality quality)
1905 if(!graphics)
1906 return InvalidParameter;
1908 graphics->compqual = quality;
1910 return Ok;
1913 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
1914 InterpolationMode mode)
1916 if(!graphics)
1917 return InvalidParameter;
1919 graphics->interpolation = mode;
1921 return Ok;
1924 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
1926 if(!graphics || (scale <= 0.0))
1927 return InvalidParameter;
1929 graphics->scale = scale;
1931 return Ok;
1934 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
1936 if(!graphics || (unit == UnitWorld))
1937 return InvalidParameter;
1939 graphics->unit = unit;
1941 return Ok;
1944 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1945 mode)
1947 if(!graphics)
1948 return InvalidParameter;
1950 graphics->pixeloffset = mode;
1952 return Ok;
1955 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
1957 if(!graphics)
1958 return InvalidParameter;
1960 graphics->smoothing = mode;
1962 return Ok;
1965 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
1966 TextRenderingHint hint)
1968 if(!graphics)
1969 return InvalidParameter;
1971 graphics->texthint = hint;
1973 return Ok;
1976 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
1978 if(!graphics || !matrix)
1979 return InvalidParameter;
1981 GdipDeleteMatrix(graphics->worldtrans);
1982 return GdipCloneMatrix(matrix, &graphics->worldtrans);
1985 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
1986 REAL dy, GpMatrixOrder order)
1988 if(!graphics)
1989 return InvalidParameter;
1991 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);