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 if(pen->unit == UnitPixel){
99 /* Get an estimate for the amount the pen width is affected by the world
100 * transform. (This is similar to what some of the wine drivers do.) */
105 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
106 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
107 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
109 width *= pen->width * convert_unit(graphics->hdc,
110 pen->unit == UnitWorld ? graphics->unit : pen->unit);
113 if(pen->dash == DashStyleCustom){
114 numdashes = min(pen->numdashes, MAX_DASHLEN);
116 TRACE("dashes are: ");
117 for(i = 0; i < numdashes; i++){
118 dash_array[i] = roundr(width * pen->dashes[i]);
119 TRACE("%d, ", dash_array[i]);
121 TRACE("\n and the pen style is %x\n", pen->style);
123 gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb,
124 numdashes, dash_array);
127 gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb, 0, NULL);
129 SelectObject(graphics->hdc, gdipen);
134 static void restore_dc(GpGraphics *graphics, INT state)
136 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
137 RestoreDC(graphics->hdc, state);
140 /* This helper applies all the changes that the points listed in ptf need in
141 * order to be drawn on the device context. In the end, this should include at
143 * -scaling by page unit
144 * -applying world transformation
145 * -converting from float to int
146 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
147 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
148 * gdi to draw, and these functions would irreparably mess with line widths.
150 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
151 GpPointF *ptf, INT count)
157 unitscale = convert_unit(graphics->hdc, graphics->unit);
159 /* apply page scale */
160 if(graphics->unit != UnitDisplay)
161 unitscale *= graphics->scale;
163 GdipCloneMatrix(graphics->worldtrans, &matrix);
164 GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
165 GdipTransformMatrixPoints(matrix, ptf, count);
166 GdipDeleteMatrix(matrix);
168 for(i = 0; i < count; i++){
169 pti[i].x = roundr(ptf[i].X);
170 pti[i].y = roundr(ptf[i].Y);
174 /* GdipDrawPie/GdipFillPie helper function */
175 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
176 REAL height, REAL startAngle, REAL sweepAngle)
183 ptf[1].X = x + width;
184 ptf[1].Y = y + height;
186 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
187 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
189 transform_and_round_points(graphics, pti, ptf, 4);
191 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
192 pti[2].y, pti[3].x, pti[3].y);
195 /* GdipDrawCurve helper function.
196 * Calculates Bezier points from cardinal spline points. */
197 static void calc_curve_bezier(CONST GpPointF *pts, REAL tension, REAL *x1,
198 REAL *y1, REAL *x2, REAL *y2)
202 /* calculate tangent */
203 xdiff = pts[2].X - pts[0].X;
204 ydiff = pts[2].Y - pts[0].Y;
206 /* apply tangent to get control points */
207 *x1 = pts[1].X - tension * xdiff;
208 *y1 = pts[1].Y - tension * ydiff;
209 *x2 = pts[1].X + tension * xdiff;
210 *y2 = pts[1].Y + tension * ydiff;
213 /* GdipDrawCurve helper function.
214 * Calculates Bezier points from cardinal spline endpoints. */
215 static void calc_curve_bezier_endp(REAL xend, REAL yend, REAL xadj, REAL yadj,
216 REAL tension, REAL *x, REAL *y)
218 /* tangent at endpoints is the line from the endpoint to the adjacent point */
219 *x = roundr(tension * (xadj - xend) + xend);
220 *y = roundr(tension * (yadj - yend) + yend);
223 /* Draws the linecap the specified color and size on the hdc. The linecap is in
224 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
225 * should not be called on an hdc that has a path you care about. */
226 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
227 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
229 HGDIOBJ oldbrush = NULL, oldpen = NULL;
230 GpMatrix *matrix = NULL;
233 PointF ptf[4], *custptf = NULL;
234 POINT pt[4], *custpt = NULL;
236 REAL theta, dsmall, dbig, dx, dy = 0.0;
241 if((x1 == x2) && (y1 == y2))
244 theta = gdiplus_atan2(y2 - y1, x2 - x1);
246 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
248 brush = CreateSolidBrush(color);
249 lb.lbStyle = BS_SOLID;
252 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
253 PS_JOIN_MITER, 1, &lb, 0,
255 oldbrush = SelectObject(graphics->hdc, brush);
256 oldpen = SelectObject(graphics->hdc, pen);
263 case LineCapSquareAnchor:
264 case LineCapDiamondAnchor:
265 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
266 if(cap == LineCapDiamondAnchor){
267 dsmall = cos(theta + M_PI_2) * size;
268 dbig = sin(theta + M_PI_2) * size;
271 dsmall = cos(theta + M_PI_4) * size;
272 dbig = sin(theta + M_PI_4) * size;
275 ptf[0].X = x2 - dsmall;
276 ptf[1].X = x2 + dbig;
278 ptf[0].Y = y2 - dbig;
279 ptf[3].Y = y2 + dsmall;
281 ptf[1].Y = y2 - dsmall;
282 ptf[2].Y = y2 + dbig;
284 ptf[3].X = x2 - dbig;
285 ptf[2].X = x2 + dsmall;
287 transform_and_round_points(graphics, pt, ptf, 4);
288 Polygon(graphics->hdc, pt, 4);
291 case LineCapArrowAnchor:
292 size = size * 4.0 / sqrt(3.0);
294 dx = cos(M_PI / 6.0 + theta) * size;
295 dy = sin(M_PI / 6.0 + theta) * size;
300 dx = cos(- M_PI / 6.0 + theta) * size;
301 dy = sin(- M_PI / 6.0 + theta) * size;
309 transform_and_round_points(graphics, pt, ptf, 3);
310 Polygon(graphics->hdc, pt, 3);
313 case LineCapRoundAnchor:
314 dx = dy = ANCHOR_WIDTH * size / 2.0;
321 transform_and_round_points(graphics, pt, ptf, 2);
322 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
325 case LineCapTriangle:
327 dx = cos(M_PI_2 + theta) * size;
328 dy = sin(M_PI_2 + theta) * size;
335 dx = cos(theta) * size;
336 dy = sin(theta) * size;
341 transform_and_round_points(graphics, pt, ptf, 3);
342 Polygon(graphics->hdc, pt, 3);
346 dx = dy = size / 2.0;
353 dx = -cos(M_PI_2 + theta) * size;
354 dy = -sin(M_PI_2 + theta) * size;
361 transform_and_round_points(graphics, pt, ptf, 4);
362 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
363 pt[2].y, pt[3].x, pt[3].y);
370 count = custom->pathdata.Count;
371 custptf = GdipAlloc(count * sizeof(PointF));
372 custpt = GdipAlloc(count * sizeof(POINT));
373 tp = GdipAlloc(count);
375 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
378 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
380 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
381 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
383 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
384 GdipTransformMatrixPoints(matrix, custptf, count);
386 transform_and_round_points(graphics, custpt, custptf, count);
388 for(i = 0; i < count; i++)
389 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
392 BeginPath(graphics->hdc);
393 PolyDraw(graphics->hdc, custpt, tp, count);
394 EndPath(graphics->hdc);
395 StrokeAndFillPath(graphics->hdc);
398 PolyDraw(graphics->hdc, custpt, tp, count);
404 GdipDeleteMatrix(matrix);
411 SelectObject(graphics->hdc, oldbrush);
412 SelectObject(graphics->hdc, oldpen);
418 /* Shortens the line by the given percent by changing x2, y2.
419 * If percent is > 1.0 then the line will change direction.
420 * If percent is negative it can lengthen the line. */
421 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
423 REAL dist, theta, dx, dy;
425 if((y1 == *y2) && (x1 == *x2))
428 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
429 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
430 dx = cos(theta) * dist;
431 dy = sin(theta) * dist;
437 /* Shortens the line by the given amount by changing x2, y2.
438 * If the amount is greater than the distance, the line will become length 0.
439 * If the amount is negative, it can lengthen the line. */
440 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
442 REAL dx, dy, percent;
446 if(dx == 0 && dy == 0)
449 percent = amt / sqrt(dx * dx + dy * dy);
456 shorten_line_percent(x1, y1, x2, y2, percent);
459 /* Draws lines between the given points, and if caps is true then draws an endcap
460 * at the end of the last line. */
461 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
462 GDIPCONST GpPointF * pt, INT count, BOOL caps)
465 GpPointF *ptcopy = NULL;
466 GpStatus status = GenericError;
471 pti = GdipAlloc(count * sizeof(POINT));
472 ptcopy = GdipAlloc(count * sizeof(GpPointF));
475 status = OutOfMemory;
479 memcpy(ptcopy, pt, count * sizeof(GpPointF));
482 if(pen->endcap == LineCapArrowAnchor)
483 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
484 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
485 else if((pen->endcap == LineCapCustom) && pen->customend)
486 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
487 &ptcopy[count-1].X, &ptcopy[count-1].Y,
488 pen->customend->inset * pen->width);
490 if(pen->startcap == LineCapArrowAnchor)
491 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
492 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
493 else if((pen->startcap == LineCapCustom) && pen->customstart)
494 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
495 &ptcopy[0].X, &ptcopy[0].Y,
496 pen->customstart->inset * pen->width);
498 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
499 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
500 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
501 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);\
504 transform_and_round_points(graphics, pti, ptcopy, count);
506 Polyline(graphics->hdc, pti, count);
515 /* Conducts a linear search to find the bezier points that will back off
516 * the endpoint of the curve by a distance of amt. Linear search works
517 * better than binary in this case because there are multiple solutions,
518 * and binary searches often find a bad one. I don't think this is what
519 * Windows does but short of rendering the bezier without GDI's help it's
520 * the best we can do. If rev then work from the start of the passed points
521 * instead of the end. */
522 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
525 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
526 INT i, first = 0, second = 1, third = 2, fourth = 3;
535 origx = pt[fourth].X;
536 origy = pt[fourth].Y;
537 memcpy(origpt, pt, sizeof(GpPointF) * 4);
539 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
540 /* reset bezier points to original values */
541 memcpy(pt, origpt, sizeof(GpPointF) * 4);
542 /* Perform magic on bezier points. Order is important here.*/
543 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
544 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
545 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
546 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].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);
550 dx = pt[fourth].X - origx;
551 dy = pt[fourth].Y - origy;
553 diff = sqrt(dx * dx + dy * dy);
554 percent += 0.0005 * amt;
558 /* Draws bezier curves between given points, and if caps is true then draws an
559 * endcap at the end of the last line. */
560 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
561 GDIPCONST GpPointF * pt, INT count, BOOL caps)
565 GpStatus status = GenericError;
570 pti = GdipAlloc(count * sizeof(POINT));
571 ptcopy = GdipAlloc(count * sizeof(GpPointF));
574 status = OutOfMemory;
578 memcpy(ptcopy, pt, count * sizeof(GpPointF));
581 if(pen->endcap == LineCapArrowAnchor)
582 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
583 else if((pen->endcap == LineCapCustom) && pen->customend)
584 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
587 if(pen->startcap == LineCapArrowAnchor)
588 shorten_bezier_amt(ptcopy, pen->width, TRUE);
589 else if((pen->startcap == LineCapCustom) && pen->customstart)
590 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
592 /* the direction of the line cap is parallel to the direction at the
593 * end of the bezier (which, if it has been shortened, is not the same
594 * as the direction from pt[count-2] to pt[count-1]) */
595 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
596 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
597 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
598 pt[count - 1].X, pt[count - 1].Y);
600 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
601 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
602 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
605 transform_and_round_points(graphics, pti, ptcopy, count);
607 PolyBezier(graphics->hdc, pti, count);
618 /* Draws a combination of bezier curves and lines between points. */
619 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
620 GDIPCONST BYTE * types, INT count, BOOL caps)
622 POINT *pti = GdipAlloc(count * sizeof(POINT));
623 BYTE *tp = GdipAlloc(count);
624 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
626 GpStatus status = GenericError;
632 if(!pti || !tp || !ptcopy){
633 status = OutOfMemory;
637 for(i = 1; i < count; i++){
638 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
639 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
640 || !(types[i + 1] & PathPointTypeBezier)){
641 ERR("Bad bezier points\n");
648 memcpy(ptcopy, pt, count * sizeof(GpPointF));
650 /* If we are drawing caps, go through the points and adjust them accordingly,
651 * and draw the caps. */
653 switch(types[count - 1] & PathPointTypePathTypeMask){
654 case PathPointTypeBezier:
655 if(pen->endcap == LineCapArrowAnchor)
656 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
657 else if((pen->endcap == LineCapCustom) && pen->customend)
658 shorten_bezier_amt(&ptcopy[count - 4],
659 pen->width * pen->customend->inset, FALSE);
661 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
662 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
663 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
664 pt[count - 1].X, pt[count - 1].Y);
667 case PathPointTypeLine:
668 if(pen->endcap == LineCapArrowAnchor)
669 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
670 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
672 else if((pen->endcap == LineCapCustom) && pen->customend)
673 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
674 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
675 pen->customend->inset * pen->width);
677 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
678 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
683 ERR("Bad path last point\n");
687 /* Find start of points */
688 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
689 == PathPointTypeStart); j++);
691 switch(types[j] & PathPointTypePathTypeMask){
692 case PathPointTypeBezier:
693 if(pen->startcap == LineCapArrowAnchor)
694 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
695 else if((pen->startcap == LineCapCustom) && pen->customstart)
696 shorten_bezier_amt(&ptcopy[j - 1],
697 pen->width * pen->customstart->inset, TRUE);
699 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
700 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
701 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
702 pt[j - 1].X, pt[j - 1].Y);
705 case PathPointTypeLine:
706 if(pen->startcap == LineCapArrowAnchor)
707 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
708 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
710 else if((pen->startcap == LineCapCustom) && pen->customstart)
711 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
712 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
713 pen->customstart->inset * pen->width);
715 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
716 pt[j].X, pt[j].Y, pt[j - 1].X,
721 ERR("Bad path points\n");
726 transform_and_round_points(graphics, pti, ptcopy, count);
728 for(i = 0; i < count; i++){
729 tp[i] = convert_path_point_type(types[i]);
732 PolyDraw(graphics->hdc, pti, tp, count);
744 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
752 return InvalidParameter;
754 *graphics = GdipAlloc(sizeof(GpGraphics));
755 if(!*graphics) return OutOfMemory;
757 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
762 (*graphics)->hdc = hdc;
763 (*graphics)->hwnd = NULL;
764 (*graphics)->smoothing = SmoothingModeDefault;
765 (*graphics)->compqual = CompositingQualityDefault;
766 (*graphics)->interpolation = InterpolationModeDefault;
767 (*graphics)->pixeloffset = PixelOffsetModeDefault;
768 (*graphics)->compmode = CompositingModeSourceOver;
769 (*graphics)->unit = UnitDisplay;
770 (*graphics)->scale = 1.0;
775 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
779 if((ret = GdipCreateFromHDC(GetDC(hwnd), graphics)) != Ok)
782 (*graphics)->hwnd = hwnd;
787 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
788 GpMetafile **metafile)
792 if(!hemf || !metafile)
793 return InvalidParameter;
796 FIXME("not implemented\n");
798 return NotImplemented;
801 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
802 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
804 IStream *stream = NULL;
808 GpStatus retval = GenericError;
810 if(!hwmf || !metafile || !placeable)
811 return InvalidParameter;
814 read = GetMetaFileBitsEx(hwmf, 0, NULL);
817 copy = GdipAlloc(read);
818 GetMetaFileBitsEx(hwmf, read, copy);
820 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
823 read = GetEnhMetaFileBits(hemf, 0, NULL);
824 copy = GdipAlloc(read);
825 GetEnhMetaFileBits(hemf, read, copy);
826 DeleteEnhMetaFile(hemf);
828 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
829 ERR("could not make stream\n");
834 *metafile = GdipAlloc(sizeof(GpMetafile));
836 retval = OutOfMemory;
840 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
841 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
845 (*metafile)->image.type = ImageTypeMetafile;
846 (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
847 (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Right) / ((REAL) placeable->Inch);
848 (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
849 - placeable->BoundingBox.Left)) / ((REAL) placeable->Inch);
850 (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
851 - placeable->BoundingBox.Top)) / ((REAL) placeable->Inch);
852 (*metafile)->unit = UnitInch;
855 DeleteMetaFile(hwmf);
861 IStream_Release(stream);
865 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
866 UINT access, IStream **stream)
871 if(!stream || !filename)
872 return InvalidParameter;
874 if(access & GENERIC_WRITE)
875 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
876 else if(access & GENERIC_READ)
877 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
879 return InvalidParameter;
881 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
883 return hresult_to_status(ret);
886 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
888 if(!graphics) return InvalidParameter;
890 ReleaseDC(graphics->hwnd, graphics->hdc);
892 GdipDeleteMatrix(graphics->worldtrans);
893 HeapFree(GetProcessHeap(), 0, graphics);
898 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
899 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
901 INT save_state, num_pts;
902 GpPointF points[MAX_ARC_PTS];
905 if(!graphics || !pen)
906 return InvalidParameter;
908 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
910 save_state = prepare_dc(graphics, pen);
912 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
914 restore_dc(graphics, save_state);
919 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
920 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
926 if(!graphics || !pen)
927 return InvalidParameter;
938 save_state = prepare_dc(graphics, pen);
940 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
942 restore_dc(graphics, save_state);
947 /* Approximates cardinal spline with Bezier curves. */
948 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
949 GDIPCONST GpPointF *points, INT count, REAL tension)
951 /* PolyBezier expects count*3-2 points. */
952 INT i, len_pt = count*3-2, save_state;
957 if(!graphics || !pen)
958 return InvalidParameter;
960 pt = GdipAlloc(len_pt * sizeof(GpPointF));
961 tension = tension * TENSION_CONST;
963 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
966 pt[0].X = points[0].X;
967 pt[0].Y = points[0].Y;
971 for(i = 0; i < count-2; i++){
972 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
976 pt[3*i+3].X = points[i+1].X;
977 pt[3*i+3].Y = points[i+1].Y;
982 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
983 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
987 pt[len_pt-1].X = points[count-1].X;
988 pt[len_pt-1].Y = points[count-1].Y;
990 save_state = prepare_dc(graphics, pen);
992 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
995 restore_dc(graphics, save_state);
1000 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
1003 UINT width, height, srcw, srch;
1005 if(!graphics || !image)
1006 return InvalidParameter;
1008 GdipGetImageWidth(image, &width);
1009 GdipGetImageHeight(image, &height);
1011 srcw = width * (((REAL) INCH_HIMETRIC) /
1012 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX)));
1013 srch = height * (((REAL) INCH_HIMETRIC) /
1014 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY)));
1016 if(image->type != ImageTypeMetafile){
1021 IPicture_Render(image->picture, graphics->hdc, x, y, width, height,
1022 0, 0, srcw, srch, NULL);
1027 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1028 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1029 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1030 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1031 DrawImageAbort callback, VOID * callbackData)
1037 TRACE("%p %p %p %d %f %f %f %f %d %p %p %p\n", graphics, image, points, count,
1038 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1041 if(!graphics || !image || !points || !imageAttributes || count != 3)
1042 return InvalidParameter;
1044 if(srcUnit == UnitInch)
1045 dx = dy = (REAL) INCH_HIMETRIC;
1046 else if(srcUnit == UnitPixel){
1047 dx = ((REAL) INCH_HIMETRIC) /
1048 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1049 dy = ((REAL) INCH_HIMETRIC) /
1050 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1053 return NotImplemented;
1055 memcpy(ptf, points, 3 * sizeof(GpPointF));
1056 transform_and_round_points(graphics, pti, ptf, 3);
1058 /* IPicture renders bitmaps with the y-axis reversed
1059 * FIXME: flipping for unknown image type might not be correct. */
1060 if(image->type != ImageTypeMetafile){
1063 pti[0].y = pti[2].y;
1067 if(IPicture_Render(image->picture, graphics->hdc,
1068 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1069 srcx * dx, srcy * dy,
1070 srcwidth * dx, srcheight * dy,
1073 callback(callbackData);
1074 return GenericError;
1080 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
1081 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
1082 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
1083 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
1084 VOID * callbackData)
1090 points[1].X = dstx + dstwidth;
1093 points[2].Y = dsty + dstheight;
1095 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1096 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1099 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
1100 REAL y1, REAL x2, REAL y2)
1106 if(!pen || !graphics)
1107 return InvalidParameter;
1114 save_state = prepare_dc(graphics, pen);
1116 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1118 restore_dc(graphics, save_state);
1123 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
1124 INT y1, INT x2, INT y2)
1130 if(!pen || !graphics)
1131 return InvalidParameter;
1138 save_state = prepare_dc(graphics, pen);
1140 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1142 restore_dc(graphics, save_state);
1147 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
1148 GpPointF *points, INT count)
1153 if(!pen || !graphics || (count < 2))
1154 return InvalidParameter;
1156 save_state = prepare_dc(graphics, pen);
1158 retval = draw_polyline(graphics, pen, points, count, TRUE);
1160 restore_dc(graphics, save_state);
1165 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
1170 if(!pen || !graphics)
1171 return InvalidParameter;
1173 save_state = prepare_dc(graphics, pen);
1175 retval = draw_poly(graphics, pen, path->pathdata.Points,
1176 path->pathdata.Types, path->pathdata.Count, TRUE);
1178 restore_dc(graphics, save_state);
1183 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
1184 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1188 if(!graphics || !pen)
1189 return InvalidParameter;
1191 save_state = prepare_dc(graphics, pen);
1192 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1194 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1196 restore_dc(graphics, save_state);
1201 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
1202 INT y, INT width, INT height)
1208 if(!pen || !graphics)
1209 return InvalidParameter;
1213 ptf[1].X = x + width;
1215 ptf[2].X = x + width;
1216 ptf[2].Y = y + height;
1218 ptf[3].Y = y + height;
1220 save_state = prepare_dc(graphics, pen);
1221 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1223 transform_and_round_points(graphics, pti, ptf, 4);
1224 Polygon(graphics->hdc, pti, 4);
1226 restore_dc(graphics, save_state);
1231 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
1236 if(!brush || !graphics || !path)
1237 return InvalidParameter;
1239 save_state = SaveDC(graphics->hdc);
1240 EndPath(graphics->hdc);
1241 SelectObject(graphics->hdc, brush->gdibrush);
1242 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
1245 BeginPath(graphics->hdc);
1246 retval = draw_poly(graphics, NULL, path->pathdata.Points,
1247 path->pathdata.Types, path->pathdata.Count, FALSE);
1252 EndPath(graphics->hdc);
1253 FillPath(graphics->hdc);
1258 RestoreDC(graphics->hdc, save_state);
1263 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
1264 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1268 if(!graphics || !brush)
1269 return InvalidParameter;
1271 save_state = SaveDC(graphics->hdc);
1272 EndPath(graphics->hdc);
1273 SelectObject(graphics->hdc, brush->gdibrush);
1274 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1276 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1278 RestoreDC(graphics->hdc, save_state);
1283 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
1284 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
1287 GpPointF *ptf = NULL;
1289 GpStatus retval = Ok;
1291 if(!graphics || !brush || !points || !count)
1292 return InvalidParameter;
1294 ptf = GdipAlloc(count * sizeof(GpPointF));
1295 pti = GdipAlloc(count * sizeof(POINT));
1297 retval = OutOfMemory;
1301 memcpy(ptf, points, count * sizeof(GpPointF));
1303 save_state = SaveDC(graphics->hdc);
1304 EndPath(graphics->hdc);
1305 SelectObject(graphics->hdc, brush->gdibrush);
1306 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1307 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1310 transform_and_round_points(graphics, pti, ptf, count);
1311 Polygon(graphics->hdc, pti, count);
1313 RestoreDC(graphics->hdc, save_state);
1322 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
1323 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
1326 GpPointF *ptf = NULL;
1328 GpStatus retval = Ok;
1330 if(!graphics || !brush || !points || !count)
1331 return InvalidParameter;
1333 ptf = GdipAlloc(count * sizeof(GpPointF));
1334 pti = GdipAlloc(count * sizeof(POINT));
1336 retval = OutOfMemory;
1340 for(i = 0; i < count; i ++){
1341 ptf[i].X = (REAL) points[i].X;
1342 ptf[i].Y = (REAL) points[i].Y;
1345 save_state = SaveDC(graphics->hdc);
1346 EndPath(graphics->hdc);
1347 SelectObject(graphics->hdc, brush->gdibrush);
1348 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1349 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1352 transform_and_round_points(graphics, pti, ptf, count);
1353 Polygon(graphics->hdc, pti, count);
1355 RestoreDC(graphics->hdc, save_state);
1364 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
1365 REAL x, REAL y, REAL width, REAL height)
1371 if(!graphics || !brush)
1372 return InvalidParameter;
1376 ptf[1].X = x + width;
1378 ptf[2].X = x + width;
1379 ptf[2].Y = y + height;
1381 ptf[3].Y = y + height;
1383 save_state = SaveDC(graphics->hdc);
1384 EndPath(graphics->hdc);
1385 SelectObject(graphics->hdc, brush->gdibrush);
1386 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1388 transform_and_round_points(graphics, pti, ptf, 4);
1390 Polygon(graphics->hdc, pti, 4);
1392 RestoreDC(graphics->hdc, save_state);
1397 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
1398 INT x, INT y, INT width, INT height)
1404 if(!graphics || !brush)
1405 return InvalidParameter;
1409 ptf[1].X = x + width;
1411 ptf[2].X = x + width;
1412 ptf[2].Y = y + height;
1414 ptf[3].Y = y + height;
1416 save_state = SaveDC(graphics->hdc);
1417 EndPath(graphics->hdc);
1418 SelectObject(graphics->hdc, brush->gdibrush);
1419 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1421 transform_and_round_points(graphics, pti, ptf, 4);
1423 Polygon(graphics->hdc, pti, 4);
1425 RestoreDC(graphics->hdc, save_state);
1430 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
1431 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
1432 CompositingMode *mode)
1434 if(!graphics || !mode)
1435 return InvalidParameter;
1437 *mode = graphics->compmode;
1442 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
1443 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
1444 CompositingQuality *quality)
1446 if(!graphics || !quality)
1447 return InvalidParameter;
1449 *quality = graphics->compqual;
1454 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
1455 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
1456 InterpolationMode *mode)
1458 if(!graphics || !mode)
1459 return InvalidParameter;
1461 *mode = graphics->interpolation;
1466 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
1468 if(!graphics || !scale)
1469 return InvalidParameter;
1471 *scale = graphics->scale;
1476 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
1478 if(!graphics || !unit)
1479 return InvalidParameter;
1481 *unit = graphics->unit;
1486 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
1487 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1490 if(!graphics || !mode)
1491 return InvalidParameter;
1493 *mode = graphics->pixeloffset;
1498 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
1499 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
1501 if(!graphics || !mode)
1502 return InvalidParameter;
1504 *mode = graphics->smoothing;
1509 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
1511 if(!graphics || !matrix)
1512 return InvalidParameter;
1514 memcpy(matrix, graphics->worldtrans, sizeof(GpMatrix));
1518 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
1523 return InvalidParameter;
1526 FIXME("graphics state not implemented\n");
1528 return NotImplemented;
1531 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
1532 GpMatrixOrder order)
1535 return InvalidParameter;
1537 return GdipRotateMatrix(graphics->worldtrans, angle, order);
1540 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
1544 if(!graphics || !state)
1545 return InvalidParameter;
1548 FIXME("graphics state not implemented\n");
1550 return NotImplemented;
1553 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
1554 CompositingMode mode)
1557 return InvalidParameter;
1559 graphics->compmode = mode;
1564 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
1565 CompositingQuality quality)
1568 return InvalidParameter;
1570 graphics->compqual = quality;
1575 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
1576 InterpolationMode mode)
1579 return InvalidParameter;
1581 graphics->interpolation = mode;
1586 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
1588 if(!graphics || (scale <= 0.0))
1589 return InvalidParameter;
1591 graphics->scale = scale;
1596 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
1598 if(!graphics || (unit == UnitWorld))
1599 return InvalidParameter;
1601 graphics->unit = unit;
1606 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1610 return InvalidParameter;
1612 graphics->pixeloffset = mode;
1617 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
1620 return InvalidParameter;
1622 graphics->smoothing = mode;
1627 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
1629 if(!graphics || !matrix)
1630 return InvalidParameter;
1632 GdipDeleteMatrix(graphics->worldtrans);
1633 return GdipCloneMatrix(matrix, &graphics->worldtrans);
1636 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
1637 REAL dy, GpMatrixOrder order)
1640 return InvalidParameter;
1642 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);