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
37 #include "gdiplus_private.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
42 /* looks-right constants */
43 #define TENSION_CONST (0.3)
44 #define ANCHOR_WIDTH (2.0)
45 #define MAX_ITERS (50)
47 /* Converts angle (in degrees) to x/y coordinates */
48 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
50 REAL radAngle, hypotenuse;
52 radAngle = deg2rad(angle);
53 hypotenuse = 50.0; /* arbitrary */
55 *x = x_0 + cos(radAngle) * hypotenuse;
56 *y = y_0 + sin(radAngle) * hypotenuse;
59 /* Converts from gdiplus path point type to gdi path point type. */
60 static BYTE convert_path_point_type(BYTE type)
64 switch(type & PathPointTypePathTypeMask){
65 case PathPointTypeBezier:
68 case PathPointTypeLine:
71 case PathPointTypeStart:
75 ERR("Bad point type\n");
79 if(type & PathPointTypeCloseSubpath)
80 ret |= PT_CLOSEFIGURE;
85 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
89 INT save_state = SaveDC(graphics->hdc), i, numdashes;
91 DWORD dash_array[MAX_DASHLEN];
93 EndPath(graphics->hdc);
95 /* Get an estimate for the amount the pen width is affected by the world
96 * transform. (This is similar to what some of the wine drivers do.) */
101 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
102 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
103 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
105 width *= pen->width * convert_unit(graphics->hdc,
106 pen->unit == UnitWorld ? graphics->unit : pen->unit);
108 if(pen->dash == DashStyleCustom){
109 numdashes = min(pen->numdashes, MAX_DASHLEN);
111 TRACE("dashes are: ");
112 for(i = 0; i < numdashes; i++){
113 dash_array[i] = roundr(width * pen->dashes[i]);
114 TRACE("%d, ", dash_array[i]);
116 TRACE("\n and the pen style is %x\n", pen->style);
118 gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb,
119 numdashes, dash_array);
122 gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb, 0, NULL);
124 SelectObject(graphics->hdc, gdipen);
129 static void restore_dc(GpGraphics *graphics, INT state)
131 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
132 RestoreDC(graphics->hdc, state);
135 /* This helper applies all the changes that the points listed in ptf need in
136 * order to be drawn on the device context. In the end, this should include at
138 * -scaling by page unit
139 * -applying world transformation
140 * -converting from float to int
141 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
142 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
143 * gdi to draw, and these functions would irreparably mess with line widths.
145 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
146 GpPointF *ptf, INT count)
152 unitscale = convert_unit(graphics->hdc, graphics->unit);
154 /* apply page scale */
155 if(graphics->unit != UnitDisplay)
156 unitscale *= graphics->scale;
158 GdipCloneMatrix(graphics->worldtrans, &matrix);
159 GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
160 GdipTransformMatrixPoints(matrix, ptf, count);
161 GdipDeleteMatrix(matrix);
163 for(i = 0; i < count; i++){
164 pti[i].x = roundr(ptf[i].X);
165 pti[i].y = roundr(ptf[i].Y);
169 /* GdipDrawPie/GdipFillPie helper function */
170 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
171 REAL height, REAL startAngle, REAL sweepAngle)
178 ptf[1].X = x + width;
179 ptf[1].Y = y + height;
181 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
182 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
184 transform_and_round_points(graphics, pti, ptf, 4);
186 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
187 pti[2].y, pti[3].x, pti[3].y);
190 /* GdipDrawCurve helper function.
191 * Calculates Bezier points from cardinal spline points. */
192 static void calc_curve_bezier(CONST GpPointF *pts, REAL tension, REAL *x1,
193 REAL *y1, REAL *x2, REAL *y2)
197 /* calculate tangent */
198 xdiff = pts[2].X - pts[0].X;
199 ydiff = pts[2].Y - pts[0].Y;
201 /* apply tangent to get control points */
202 *x1 = pts[1].X - tension * xdiff;
203 *y1 = pts[1].Y - tension * ydiff;
204 *x2 = pts[1].X + tension * xdiff;
205 *y2 = pts[1].Y + tension * ydiff;
208 /* GdipDrawCurve helper function.
209 * Calculates Bezier points from cardinal spline endpoints. */
210 static void calc_curve_bezier_endp(REAL xend, REAL yend, REAL xadj, REAL yadj,
211 REAL tension, REAL *x, REAL *y)
213 /* tangent at endpoints is the line from the endpoint to the adjacent point */
214 *x = roundr(tension * (xadj - xend) + xend);
215 *y = roundr(tension * (yadj - yend) + yend);
218 /* Draws the linecap the specified color and size on the hdc. The linecap is in
219 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
220 * should not be called on an hdc that has a path you care about. */
221 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
222 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
224 HGDIOBJ oldbrush = NULL, oldpen = NULL;
225 GpMatrix *matrix = NULL;
228 PointF ptf[4], *custptf = NULL;
229 POINT pt[4], *custpt = NULL;
231 REAL theta, dsmall, dbig, dx, dy = 0.0;
236 if((x1 == x2) && (y1 == y2))
239 theta = gdiplus_atan2(y2 - y1, x2 - x1);
241 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
243 brush = CreateSolidBrush(color);
244 lb.lbStyle = BS_SOLID;
247 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
248 PS_JOIN_MITER, 1, &lb, 0,
250 oldbrush = SelectObject(graphics->hdc, brush);
251 oldpen = SelectObject(graphics->hdc, pen);
258 case LineCapSquareAnchor:
259 case LineCapDiamondAnchor:
260 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
261 if(cap == LineCapDiamondAnchor){
262 dsmall = cos(theta + M_PI_2) * size;
263 dbig = sin(theta + M_PI_2) * size;
266 dsmall = cos(theta + M_PI_4) * size;
267 dbig = sin(theta + M_PI_4) * size;
270 ptf[0].X = x2 - dsmall;
271 ptf[1].X = x2 + dbig;
273 ptf[0].Y = y2 - dbig;
274 ptf[3].Y = y2 + dsmall;
276 ptf[1].Y = y2 - dsmall;
277 ptf[2].Y = y2 + dbig;
279 ptf[3].X = x2 - dbig;
280 ptf[2].X = x2 + dsmall;
282 transform_and_round_points(graphics, pt, ptf, 4);
283 Polygon(graphics->hdc, pt, 4);
286 case LineCapArrowAnchor:
287 size = size * 4.0 / sqrt(3.0);
289 dx = cos(M_PI / 6.0 + theta) * size;
290 dy = sin(M_PI / 6.0 + theta) * size;
295 dx = cos(- M_PI / 6.0 + theta) * size;
296 dy = sin(- M_PI / 6.0 + theta) * size;
304 transform_and_round_points(graphics, pt, ptf, 3);
305 Polygon(graphics->hdc, pt, 3);
308 case LineCapRoundAnchor:
309 dx = dy = ANCHOR_WIDTH * size / 2.0;
316 transform_and_round_points(graphics, pt, ptf, 2);
317 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
320 case LineCapTriangle:
322 dx = cos(M_PI_2 + theta) * size;
323 dy = sin(M_PI_2 + theta) * size;
330 dx = cos(theta) * size;
331 dy = sin(theta) * size;
336 transform_and_round_points(graphics, pt, ptf, 3);
337 Polygon(graphics->hdc, pt, 3);
341 dx = dy = size / 2.0;
348 dx = -cos(M_PI_2 + theta) * size;
349 dy = -sin(M_PI_2 + theta) * size;
356 transform_and_round_points(graphics, pt, ptf, 4);
357 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
358 pt[2].y, pt[3].x, pt[3].y);
365 count = custom->pathdata.Count;
366 custptf = GdipAlloc(count * sizeof(PointF));
367 custpt = GdipAlloc(count * sizeof(POINT));
368 tp = GdipAlloc(count);
370 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
373 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
375 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
376 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
378 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
379 GdipTransformMatrixPoints(matrix, custptf, count);
381 transform_and_round_points(graphics, custpt, custptf, count);
383 for(i = 0; i < count; i++)
384 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
387 BeginPath(graphics->hdc);
388 PolyDraw(graphics->hdc, custpt, tp, count);
389 EndPath(graphics->hdc);
390 StrokeAndFillPath(graphics->hdc);
393 PolyDraw(graphics->hdc, custpt, tp, count);
399 GdipDeleteMatrix(matrix);
406 SelectObject(graphics->hdc, oldbrush);
407 SelectObject(graphics->hdc, oldpen);
413 /* Shortens the line by the given percent by changing x2, y2.
414 * If percent is > 1.0 then the line will change direction.
415 * If percent is negative it can lengthen the line. */
416 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
418 REAL dist, theta, dx, dy;
420 if((y1 == *y2) && (x1 == *x2))
423 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
424 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
425 dx = cos(theta) * dist;
426 dy = sin(theta) * dist;
432 /* Shortens the line by the given amount by changing x2, y2.
433 * If the amount is greater than the distance, the line will become length 0.
434 * If the amount is negative, it can lengthen the line. */
435 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
437 REAL dx, dy, percent;
441 if(dx == 0 && dy == 0)
444 percent = amt / sqrt(dx * dx + dy * dy);
451 shorten_line_percent(x1, y1, x2, y2, percent);
454 /* Draws lines between the given points, and if caps is true then draws an endcap
455 * at the end of the last line. */
456 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
457 GDIPCONST GpPointF * pt, INT count, BOOL caps)
460 GpPointF *ptcopy = NULL;
461 GpStatus status = GenericError;
466 pti = GdipAlloc(count * sizeof(POINT));
467 ptcopy = GdipAlloc(count * sizeof(GpPointF));
470 status = OutOfMemory;
474 memcpy(ptcopy, pt, count * sizeof(GpPointF));
477 if(pen->endcap == LineCapArrowAnchor)
478 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
479 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
480 else if((pen->endcap == LineCapCustom) && pen->customend)
481 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
482 &ptcopy[count-1].X, &ptcopy[count-1].Y,
483 pen->customend->inset * pen->width);
485 if(pen->startcap == LineCapArrowAnchor)
486 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
487 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
488 else if((pen->startcap == LineCapCustom) && pen->customstart)
489 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
490 &ptcopy[0].X, &ptcopy[0].Y,
491 pen->customstart->inset * pen->width);
493 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
494 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
495 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
496 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);\
499 transform_and_round_points(graphics, pti, ptcopy, count);
501 Polyline(graphics->hdc, pti, count);
510 /* Conducts a linear search to find the bezier points that will back off
511 * the endpoint of the curve by a distance of amt. Linear search works
512 * better than binary in this case because there are multiple solutions,
513 * and binary searches often find a bad one. I don't think this is what
514 * Windows does but short of rendering the bezier without GDI's help it's
515 * the best we can do. If rev then work from the start of the passed points
516 * instead of the end. */
517 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
520 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
521 INT i, first = 0, second = 1, third = 2, fourth = 3;
530 origx = pt[fourth].X;
531 origy = pt[fourth].Y;
532 memcpy(origpt, pt, sizeof(GpPointF) * 4);
534 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
535 /* reset bezier points to original values */
536 memcpy(pt, origpt, sizeof(GpPointF) * 4);
537 /* Perform magic on bezier points. Order is important here.*/
538 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
539 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
540 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
541 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
542 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
543 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
545 dx = pt[fourth].X - origx;
546 dy = pt[fourth].Y - origy;
548 diff = sqrt(dx * dx + dy * dy);
549 percent += 0.0005 * amt;
553 /* Draws bezier curves between given points, and if caps is true then draws an
554 * endcap at the end of the last line. */
555 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
556 GDIPCONST GpPointF * pt, INT count, BOOL caps)
560 GpStatus status = GenericError;
565 pti = GdipAlloc(count * sizeof(POINT));
566 ptcopy = GdipAlloc(count * sizeof(GpPointF));
569 status = OutOfMemory;
573 memcpy(ptcopy, pt, count * sizeof(GpPointF));
576 if(pen->endcap == LineCapArrowAnchor)
577 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
578 else if((pen->endcap == LineCapCustom) && pen->customend)
579 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
582 if(pen->startcap == LineCapArrowAnchor)
583 shorten_bezier_amt(ptcopy, pen->width, TRUE);
584 else if((pen->startcap == LineCapCustom) && pen->customstart)
585 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
587 /* the direction of the line cap is parallel to the direction at the
588 * end of the bezier (which, if it has been shortened, is not the same
589 * as the direction from pt[count-2] to pt[count-1]) */
590 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
591 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
592 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
593 pt[count - 1].X, pt[count - 1].Y);
595 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
596 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
597 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
600 transform_and_round_points(graphics, pti, ptcopy, count);
602 PolyBezier(graphics->hdc, pti, count);
613 /* Draws a combination of bezier curves and lines between points. */
614 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
615 GDIPCONST BYTE * types, INT count, BOOL caps)
617 POINT *pti = GdipAlloc(count * sizeof(POINT));
618 BYTE *tp = GdipAlloc(count);
619 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
621 GpStatus status = GenericError;
627 if(!pti || !tp || !ptcopy){
628 status = OutOfMemory;
632 for(i = 1; i < count; i++){
633 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
634 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
635 || !(types[i + 1] & PathPointTypeBezier)){
636 ERR("Bad bezier points\n");
643 memcpy(ptcopy, pt, count * sizeof(GpPointF));
645 /* If we are drawing caps, go through the points and adjust them accordingly,
646 * and draw the caps. */
648 switch(types[count - 1] & PathPointTypePathTypeMask){
649 case PathPointTypeBezier:
650 if(pen->endcap == LineCapArrowAnchor)
651 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
652 else if((pen->endcap == LineCapCustom) && pen->customend)
653 shorten_bezier_amt(&ptcopy[count - 4],
654 pen->width * pen->customend->inset, FALSE);
656 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
657 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
658 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
659 pt[count - 1].X, pt[count - 1].Y);
662 case PathPointTypeLine:
663 if(pen->endcap == LineCapArrowAnchor)
664 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
665 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
667 else if((pen->endcap == LineCapCustom) && pen->customend)
668 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
669 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
670 pen->customend->inset * pen->width);
672 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
673 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
678 ERR("Bad path last point\n");
682 /* Find start of points */
683 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
684 == PathPointTypeStart); j++);
686 switch(types[j] & PathPointTypePathTypeMask){
687 case PathPointTypeBezier:
688 if(pen->startcap == LineCapArrowAnchor)
689 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
690 else if((pen->startcap == LineCapCustom) && pen->customstart)
691 shorten_bezier_amt(&ptcopy[j - 1],
692 pen->width * pen->customstart->inset, TRUE);
694 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
695 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
696 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
697 pt[j - 1].X, pt[j - 1].Y);
700 case PathPointTypeLine:
701 if(pen->startcap == LineCapArrowAnchor)
702 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
703 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
705 else if((pen->startcap == LineCapCustom) && pen->customstart)
706 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
707 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
708 pen->customstart->inset * pen->width);
710 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
711 pt[j].X, pt[j].Y, pt[j - 1].X,
716 ERR("Bad path points\n");
721 transform_and_round_points(graphics, pti, ptcopy, count);
723 for(i = 0; i < count; i++){
724 tp[i] = convert_path_point_type(types[i]);
727 PolyDraw(graphics->hdc, pti, tp, count);
739 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
747 return InvalidParameter;
749 *graphics = GdipAlloc(sizeof(GpGraphics));
750 if(!*graphics) return OutOfMemory;
752 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
757 (*graphics)->hdc = hdc;
758 (*graphics)->hwnd = NULL;
759 (*graphics)->smoothing = SmoothingModeDefault;
760 (*graphics)->compqual = CompositingQualityDefault;
761 (*graphics)->interpolation = InterpolationModeDefault;
762 (*graphics)->pixeloffset = PixelOffsetModeDefault;
763 (*graphics)->unit = UnitDisplay;
764 (*graphics)->scale = 1.0;
769 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
773 if((ret = GdipCreateFromHDC(GetDC(hwnd), graphics)) != Ok)
776 (*graphics)->hwnd = hwnd;
781 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
782 GpMetafile **metafile)
786 if(!hemf || !metafile)
787 return InvalidParameter;
790 FIXME("not implemented\n");
792 return NotImplemented;
795 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
796 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
798 IStream *stream = NULL;
802 GpStatus retval = GenericError;
804 if(!hwmf || !metafile || !placeable)
805 return InvalidParameter;
808 read = GetMetaFileBitsEx(hwmf, 0, NULL);
811 copy = GdipAlloc(read);
812 GetMetaFileBitsEx(hwmf, read, copy);
814 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
817 read = GetEnhMetaFileBits(hemf, 0, NULL);
818 copy = GdipAlloc(read);
819 GetEnhMetaFileBits(hemf, read, copy);
820 DeleteEnhMetaFile(hemf);
822 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
823 ERR("could not make stream\n");
828 *metafile = GdipAlloc(sizeof(GpMetafile));
830 retval = OutOfMemory;
834 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
835 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
839 (*metafile)->image.type = ImageTypeMetafile;
840 (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
841 (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Right) / ((REAL) placeable->Inch);
842 (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
843 - placeable->BoundingBox.Left)) / ((REAL) placeable->Inch);
844 (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
845 - placeable->BoundingBox.Top)) / ((REAL) placeable->Inch);
846 (*metafile)->unit = UnitInch;
849 DeleteMetaFile(hwmf);
855 IStream_Release(stream);
859 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
860 UINT access, IStream **stream)
865 if(!stream || !filename)
866 return InvalidParameter;
868 if(access & GENERIC_WRITE)
869 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
870 else if(access & GENERIC_READ)
871 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
873 return InvalidParameter;
875 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
877 return hresult_to_status(ret);
880 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
882 if(!graphics) return InvalidParameter;
884 ReleaseDC(graphics->hwnd, graphics->hdc);
886 GdipDeleteMatrix(graphics->worldtrans);
887 HeapFree(GetProcessHeap(), 0, graphics);
892 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
893 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
895 INT save_state, num_pts;
896 GpPointF points[MAX_ARC_PTS];
899 if(!graphics || !pen)
900 return InvalidParameter;
902 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
904 save_state = prepare_dc(graphics, pen);
906 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
908 restore_dc(graphics, save_state);
913 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
914 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
920 if(!graphics || !pen)
921 return InvalidParameter;
932 save_state = prepare_dc(graphics, pen);
934 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
936 restore_dc(graphics, save_state);
941 /* Approximates cardinal spline with Bezier curves. */
942 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
943 GDIPCONST GpPointF *points, INT count, REAL tension)
945 /* PolyBezier expects count*3-2 points. */
946 INT i, len_pt = count*3-2, save_state;
951 if(!graphics || !pen)
952 return InvalidParameter;
954 pt = GdipAlloc(len_pt * sizeof(GpPointF));
955 tension = tension * TENSION_CONST;
957 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
960 pt[0].X = points[0].X;
961 pt[0].Y = points[0].Y;
965 for(i = 0; i < count-2; i++){
966 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
970 pt[3*i+3].X = points[i+1].X;
971 pt[3*i+3].Y = points[i+1].Y;
976 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
977 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
981 pt[len_pt-1].X = points[count-1].X;
982 pt[len_pt-1].Y = points[count-1].Y;
984 save_state = prepare_dc(graphics, pen);
986 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
989 restore_dc(graphics, save_state);
994 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
997 UINT width, height, srcw, srch;
999 if(!graphics || !image)
1000 return InvalidParameter;
1002 GdipGetImageWidth(image, &width);
1003 GdipGetImageHeight(image, &height);
1005 srcw = width * (((REAL) INCH_HIMETRIC) /
1006 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX)));
1007 srch = height * (((REAL) INCH_HIMETRIC) /
1008 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY)));
1010 if(image->type != ImageTypeMetafile){
1015 IPicture_Render(image->picture, graphics->hdc, x, y, width, height,
1016 0, 0, srcw, srch, NULL);
1021 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1022 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1023 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1024 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1025 DrawImageAbort callback, VOID * callbackData)
1031 TRACE("%p %p %p %d %f %f %f %f %d %p %p %p\n", graphics, image, points, count,
1032 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1035 if(!graphics || !image || !points || !imageAttributes || count != 3)
1036 return InvalidParameter;
1038 if(srcUnit == UnitInch)
1039 dx = dy = (REAL) INCH_HIMETRIC;
1040 else if(srcUnit == UnitPixel){
1041 dx = ((REAL) INCH_HIMETRIC) /
1042 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1043 dy = ((REAL) INCH_HIMETRIC) /
1044 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1047 return NotImplemented;
1049 memcpy(ptf, points, 3 * sizeof(GpPointF));
1050 transform_and_round_points(graphics, pti, ptf, 3);
1052 /* IPicture renders bitmaps with the y-axis reversed
1053 * FIXME: flipping for unknown image type might not be correct. */
1054 if(image->type != ImageTypeMetafile){
1057 pti[0].y = pti[2].y;
1061 if(IPicture_Render(image->picture, graphics->hdc,
1062 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1063 srcx * dx, srcy * dy,
1064 srcwidth * dx, srcheight * dy,
1067 callback(callbackData);
1068 return GenericError;
1074 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
1075 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
1076 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
1077 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
1078 VOID * callbackData)
1084 points[1].X = dstx + dstwidth;
1087 points[2].Y = dsty + dstheight;
1089 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1090 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1093 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
1094 REAL y1, REAL x2, REAL y2)
1100 if(!pen || !graphics)
1101 return InvalidParameter;
1108 save_state = prepare_dc(graphics, pen);
1110 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1112 restore_dc(graphics, save_state);
1117 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
1118 INT y1, INT x2, INT y2)
1124 if(!pen || !graphics)
1125 return InvalidParameter;
1132 save_state = prepare_dc(graphics, pen);
1134 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1136 restore_dc(graphics, save_state);
1141 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
1142 GpPointF *points, INT count)
1147 if(!pen || !graphics || (count < 2))
1148 return InvalidParameter;
1150 save_state = prepare_dc(graphics, pen);
1152 retval = draw_polyline(graphics, pen, points, count, TRUE);
1154 restore_dc(graphics, save_state);
1159 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
1164 if(!pen || !graphics)
1165 return InvalidParameter;
1167 save_state = prepare_dc(graphics, pen);
1169 retval = draw_poly(graphics, pen, path->pathdata.Points,
1170 path->pathdata.Types, path->pathdata.Count, TRUE);
1172 restore_dc(graphics, save_state);
1177 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
1178 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1182 if(!graphics || !pen)
1183 return InvalidParameter;
1185 save_state = prepare_dc(graphics, pen);
1186 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1188 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1190 restore_dc(graphics, save_state);
1195 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
1196 INT y, INT width, INT height)
1202 if(!pen || !graphics)
1203 return InvalidParameter;
1207 ptf[1].X = x + width;
1209 ptf[2].X = x + width;
1210 ptf[2].Y = y + height;
1212 ptf[3].Y = y + height;
1214 save_state = prepare_dc(graphics, pen);
1215 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1217 transform_and_round_points(graphics, pti, ptf, 4);
1218 Polygon(graphics->hdc, pti, 4);
1220 restore_dc(graphics, save_state);
1225 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
1230 if(!brush || !graphics || !path)
1231 return InvalidParameter;
1233 save_state = SaveDC(graphics->hdc);
1234 EndPath(graphics->hdc);
1235 SelectObject(graphics->hdc, brush->gdibrush);
1236 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
1239 BeginPath(graphics->hdc);
1240 retval = draw_poly(graphics, NULL, path->pathdata.Points,
1241 path->pathdata.Types, path->pathdata.Count, FALSE);
1246 EndPath(graphics->hdc);
1247 FillPath(graphics->hdc);
1252 RestoreDC(graphics->hdc, save_state);
1257 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
1258 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1262 if(!graphics || !brush)
1263 return InvalidParameter;
1265 save_state = SaveDC(graphics->hdc);
1266 EndPath(graphics->hdc);
1267 SelectObject(graphics->hdc, brush->gdibrush);
1268 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1270 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1272 RestoreDC(graphics->hdc, save_state);
1277 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
1278 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
1281 GpPointF *ptf = NULL;
1283 GpStatus retval = Ok;
1285 if(!graphics || !brush || !points || !count)
1286 return InvalidParameter;
1288 ptf = GdipAlloc(count * sizeof(GpPointF));
1289 pti = GdipAlloc(count * sizeof(POINT));
1291 retval = OutOfMemory;
1295 memcpy(ptf, points, count * sizeof(GpPointF));
1297 save_state = SaveDC(graphics->hdc);
1298 EndPath(graphics->hdc);
1299 SelectObject(graphics->hdc, brush->gdibrush);
1300 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1301 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1304 transform_and_round_points(graphics, pti, ptf, count);
1305 Polygon(graphics->hdc, pti, count);
1307 RestoreDC(graphics->hdc, save_state);
1316 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
1317 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
1320 GpPointF *ptf = NULL;
1322 GpStatus retval = Ok;
1324 if(!graphics || !brush || !points || !count)
1325 return InvalidParameter;
1327 ptf = GdipAlloc(count * sizeof(GpPointF));
1328 pti = GdipAlloc(count * sizeof(POINT));
1330 retval = OutOfMemory;
1334 for(i = 0; i < count; i ++){
1335 ptf[i].X = (REAL) points[i].X;
1336 ptf[i].Y = (REAL) points[i].Y;
1339 save_state = SaveDC(graphics->hdc);
1340 EndPath(graphics->hdc);
1341 SelectObject(graphics->hdc, brush->gdibrush);
1342 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1343 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1346 transform_and_round_points(graphics, pti, ptf, count);
1347 Polygon(graphics->hdc, pti, count);
1349 RestoreDC(graphics->hdc, save_state);
1358 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
1359 REAL x, REAL y, REAL width, REAL height)
1365 if(!graphics || !brush)
1366 return InvalidParameter;
1370 ptf[1].X = x + width;
1372 ptf[2].X = x + width;
1373 ptf[2].Y = y + height;
1375 ptf[3].Y = y + height;
1377 save_state = SaveDC(graphics->hdc);
1378 EndPath(graphics->hdc);
1379 SelectObject(graphics->hdc, brush->gdibrush);
1380 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1382 transform_and_round_points(graphics, pti, ptf, 4);
1384 Polygon(graphics->hdc, pti, 4);
1386 RestoreDC(graphics->hdc, save_state);
1391 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
1392 INT x, INT y, INT width, INT height)
1398 if(!graphics || !brush)
1399 return InvalidParameter;
1403 ptf[1].X = x + width;
1405 ptf[2].X = x + width;
1406 ptf[2].Y = y + height;
1408 ptf[3].Y = y + height;
1410 save_state = SaveDC(graphics->hdc);
1411 EndPath(graphics->hdc);
1412 SelectObject(graphics->hdc, brush->gdibrush);
1413 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1415 transform_and_round_points(graphics, pti, ptf, 4);
1417 Polygon(graphics->hdc, pti, 4);
1419 RestoreDC(graphics->hdc, save_state);
1424 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
1425 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
1426 CompositingQuality *quality)
1428 if(!graphics || !quality)
1429 return InvalidParameter;
1431 *quality = graphics->compqual;
1436 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
1437 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
1438 InterpolationMode *mode)
1440 if(!graphics || !mode)
1441 return InvalidParameter;
1443 *mode = graphics->interpolation;
1448 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
1450 if(!graphics || !scale)
1451 return InvalidParameter;
1453 *scale = graphics->scale;
1458 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
1460 if(!graphics || !unit)
1461 return InvalidParameter;
1463 *unit = graphics->unit;
1468 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
1469 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1472 if(!graphics || !mode)
1473 return InvalidParameter;
1475 *mode = graphics->pixeloffset;
1480 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
1481 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
1483 if(!graphics || !mode)
1484 return InvalidParameter;
1486 *mode = graphics->smoothing;
1491 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
1493 if(!graphics || !matrix)
1494 return InvalidParameter;
1496 memcpy(matrix, graphics->worldtrans, sizeof(GpMatrix));
1500 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
1505 return InvalidParameter;
1508 FIXME("graphics state not implemented\n");
1510 return NotImplemented;
1513 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
1517 if(!graphics || !state)
1518 return InvalidParameter;
1521 FIXME("graphics state not implemented\n");
1523 return NotImplemented;
1526 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
1527 CompositingQuality quality)
1530 return InvalidParameter;
1532 graphics->compqual = quality;
1537 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
1538 InterpolationMode mode)
1541 return InvalidParameter;
1543 graphics->interpolation = mode;
1548 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
1550 if(!graphics || (scale <= 0.0))
1551 return InvalidParameter;
1553 graphics->scale = scale;
1558 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
1560 if(!graphics || (unit == UnitWorld))
1561 return InvalidParameter;
1563 graphics->unit = unit;
1568 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1572 return InvalidParameter;
1574 graphics->pixeloffset = mode;
1579 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
1582 return InvalidParameter;
1584 graphics->smoothing = mode;
1589 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
1591 if(!graphics || !matrix)
1592 return InvalidParameter;
1594 GdipDeleteMatrix(graphics->worldtrans);
1595 return GdipCloneMatrix(matrix, &graphics->worldtrans);