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
26 #include "wine/unicode.h"
38 #include "gdiplus_private.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
43 /* looks-right constants */
44 #define TENSION_CONST (0.3)
45 #define ANCHOR_WIDTH (2.0)
46 #define MAX_ITERS (50)
48 /* Converts angle (in degrees) to x/y coordinates */
49 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
51 REAL radAngle, hypotenuse;
53 radAngle = deg2rad(angle);
54 hypotenuse = 50.0; /* arbitrary */
56 *x = x_0 + cos(radAngle) * hypotenuse;
57 *y = y_0 + sin(radAngle) * hypotenuse;
60 /* Converts from gdiplus path point type to gdi path point type. */
61 static BYTE convert_path_point_type(BYTE type)
65 switch(type & PathPointTypePathTypeMask){
66 case PathPointTypeBezier:
69 case PathPointTypeLine:
72 case PathPointTypeStart:
76 ERR("Bad point type\n");
80 if(type & PathPointTypeCloseSubpath)
81 ret |= PT_CLOSEFIGURE;
86 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
90 INT save_state = SaveDC(graphics->hdc), i, numdashes;
92 DWORD dash_array[MAX_DASHLEN];
94 EndPath(graphics->hdc);
96 if(pen->unit == UnitPixel){
100 /* Get an estimate for the amount the pen width is affected by the world
101 * transform. (This is similar to what some of the wine drivers do.) */
106 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
107 width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
108 (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
110 width *= pen->width * convert_unit(graphics->hdc,
111 pen->unit == UnitWorld ? graphics->unit : pen->unit);
114 if(pen->dash == DashStyleCustom){
115 numdashes = min(pen->numdashes, MAX_DASHLEN);
117 TRACE("dashes are: ");
118 for(i = 0; i < numdashes; i++){
119 dash_array[i] = roundr(width * pen->dashes[i]);
120 TRACE("%d, ", dash_array[i]);
122 TRACE("\n and the pen style is %x\n", pen->style);
124 gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb,
125 numdashes, dash_array);
128 gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb, 0, NULL);
130 SelectObject(graphics->hdc, gdipen);
135 static void restore_dc(GpGraphics *graphics, INT state)
137 DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
138 RestoreDC(graphics->hdc, state);
141 /* This helper applies all the changes that the points listed in ptf need in
142 * order to be drawn on the device context. In the end, this should include at
144 * -scaling by page unit
145 * -applying world transformation
146 * -converting from float to int
147 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
148 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
149 * gdi to draw, and these functions would irreparably mess with line widths.
151 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
152 GpPointF *ptf, INT count)
158 unitscale = convert_unit(graphics->hdc, graphics->unit);
160 /* apply page scale */
161 if(graphics->unit != UnitDisplay)
162 unitscale *= graphics->scale;
164 GdipCloneMatrix(graphics->worldtrans, &matrix);
165 GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
166 GdipTransformMatrixPoints(matrix, ptf, count);
167 GdipDeleteMatrix(matrix);
169 for(i = 0; i < count; i++){
170 pti[i].x = roundr(ptf[i].X);
171 pti[i].y = roundr(ptf[i].Y);
175 /* GdipDrawPie/GdipFillPie helper function */
176 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
177 REAL height, REAL startAngle, REAL sweepAngle)
184 ptf[1].X = x + width;
185 ptf[1].Y = y + height;
187 deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
188 deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
190 transform_and_round_points(graphics, pti, ptf, 4);
192 Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
193 pti[2].y, pti[3].x, pti[3].y);
196 /* GdipDrawCurve helper function.
197 * Calculates Bezier points from cardinal spline points. */
198 static void calc_curve_bezier(CONST GpPointF *pts, REAL tension, REAL *x1,
199 REAL *y1, REAL *x2, REAL *y2)
203 /* calculate tangent */
204 xdiff = pts[2].X - pts[0].X;
205 ydiff = pts[2].Y - pts[0].Y;
207 /* apply tangent to get control points */
208 *x1 = pts[1].X - tension * xdiff;
209 *y1 = pts[1].Y - tension * ydiff;
210 *x2 = pts[1].X + tension * xdiff;
211 *y2 = pts[1].Y + tension * ydiff;
214 /* GdipDrawCurve helper function.
215 * Calculates Bezier points from cardinal spline endpoints. */
216 static void calc_curve_bezier_endp(REAL xend, REAL yend, REAL xadj, REAL yadj,
217 REAL tension, REAL *x, REAL *y)
219 /* tangent at endpoints is the line from the endpoint to the adjacent point */
220 *x = roundr(tension * (xadj - xend) + xend);
221 *y = roundr(tension * (yadj - yend) + yend);
224 /* Draws the linecap the specified color and size on the hdc. The linecap is in
225 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
226 * should not be called on an hdc that has a path you care about. */
227 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
228 const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
230 HGDIOBJ oldbrush = NULL, oldpen = NULL;
231 GpMatrix *matrix = NULL;
234 PointF ptf[4], *custptf = NULL;
235 POINT pt[4], *custpt = NULL;
237 REAL theta, dsmall, dbig, dx, dy = 0.0;
242 if((x1 == x2) && (y1 == y2))
245 theta = gdiplus_atan2(y2 - y1, x2 - x1);
247 customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
249 brush = CreateSolidBrush(color);
250 lb.lbStyle = BS_SOLID;
253 pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
254 PS_JOIN_MITER, 1, &lb, 0,
256 oldbrush = SelectObject(graphics->hdc, brush);
257 oldpen = SelectObject(graphics->hdc, pen);
264 case LineCapSquareAnchor:
265 case LineCapDiamondAnchor:
266 size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
267 if(cap == LineCapDiamondAnchor){
268 dsmall = cos(theta + M_PI_2) * size;
269 dbig = sin(theta + M_PI_2) * size;
272 dsmall = cos(theta + M_PI_4) * size;
273 dbig = sin(theta + M_PI_4) * size;
276 ptf[0].X = x2 - dsmall;
277 ptf[1].X = x2 + dbig;
279 ptf[0].Y = y2 - dbig;
280 ptf[3].Y = y2 + dsmall;
282 ptf[1].Y = y2 - dsmall;
283 ptf[2].Y = y2 + dbig;
285 ptf[3].X = x2 - dbig;
286 ptf[2].X = x2 + dsmall;
288 transform_and_round_points(graphics, pt, ptf, 4);
289 Polygon(graphics->hdc, pt, 4);
292 case LineCapArrowAnchor:
293 size = size * 4.0 / sqrt(3.0);
295 dx = cos(M_PI / 6.0 + theta) * size;
296 dy = sin(M_PI / 6.0 + theta) * size;
301 dx = cos(- M_PI / 6.0 + theta) * size;
302 dy = sin(- M_PI / 6.0 + theta) * size;
310 transform_and_round_points(graphics, pt, ptf, 3);
311 Polygon(graphics->hdc, pt, 3);
314 case LineCapRoundAnchor:
315 dx = dy = ANCHOR_WIDTH * size / 2.0;
322 transform_and_round_points(graphics, pt, ptf, 2);
323 Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
326 case LineCapTriangle:
328 dx = cos(M_PI_2 + theta) * size;
329 dy = sin(M_PI_2 + theta) * size;
336 dx = cos(theta) * size;
337 dy = sin(theta) * size;
342 transform_and_round_points(graphics, pt, ptf, 3);
343 Polygon(graphics->hdc, pt, 3);
347 dx = dy = size / 2.0;
354 dx = -cos(M_PI_2 + theta) * size;
355 dy = -sin(M_PI_2 + theta) * size;
362 transform_and_round_points(graphics, pt, ptf, 4);
363 Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
364 pt[2].y, pt[3].x, pt[3].y);
371 count = custom->pathdata.Count;
372 custptf = GdipAlloc(count * sizeof(PointF));
373 custpt = GdipAlloc(count * sizeof(POINT));
374 tp = GdipAlloc(count);
376 if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
379 memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
381 GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
382 GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
384 GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
385 GdipTransformMatrixPoints(matrix, custptf, count);
387 transform_and_round_points(graphics, custpt, custptf, count);
389 for(i = 0; i < count; i++)
390 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
393 BeginPath(graphics->hdc);
394 PolyDraw(graphics->hdc, custpt, tp, count);
395 EndPath(graphics->hdc);
396 StrokeAndFillPath(graphics->hdc);
399 PolyDraw(graphics->hdc, custpt, tp, count);
405 GdipDeleteMatrix(matrix);
412 SelectObject(graphics->hdc, oldbrush);
413 SelectObject(graphics->hdc, oldpen);
419 /* Shortens the line by the given percent by changing x2, y2.
420 * If percent is > 1.0 then the line will change direction.
421 * If percent is negative it can lengthen the line. */
422 static void shorten_line_percent(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL percent)
424 REAL dist, theta, dx, dy;
426 if((y1 == *y2) && (x1 == *x2))
429 dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
430 theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
431 dx = cos(theta) * dist;
432 dy = sin(theta) * dist;
438 /* Shortens the line by the given amount by changing x2, y2.
439 * If the amount is greater than the distance, the line will become length 0.
440 * If the amount is negative, it can lengthen the line. */
441 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
443 REAL dx, dy, percent;
447 if(dx == 0 && dy == 0)
450 percent = amt / sqrt(dx * dx + dy * dy);
457 shorten_line_percent(x1, y1, x2, y2, percent);
460 /* Draws lines between the given points, and if caps is true then draws an endcap
461 * at the end of the last line. */
462 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
463 GDIPCONST GpPointF * pt, INT count, BOOL caps)
466 GpPointF *ptcopy = NULL;
467 GpStatus status = GenericError;
472 pti = GdipAlloc(count * sizeof(POINT));
473 ptcopy = GdipAlloc(count * sizeof(GpPointF));
476 status = OutOfMemory;
480 memcpy(ptcopy, pt, count * sizeof(GpPointF));
483 if(pen->endcap == LineCapArrowAnchor)
484 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
485 &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
486 else if((pen->endcap == LineCapCustom) && pen->customend)
487 shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
488 &ptcopy[count-1].X, &ptcopy[count-1].Y,
489 pen->customend->inset * pen->width);
491 if(pen->startcap == LineCapArrowAnchor)
492 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
493 &ptcopy[0].X, &ptcopy[0].Y, pen->width);
494 else if((pen->startcap == LineCapCustom) && pen->customstart)
495 shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
496 &ptcopy[0].X, &ptcopy[0].Y,
497 pen->customstart->inset * pen->width);
499 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
500 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
501 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
502 pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);\
505 transform_and_round_points(graphics, pti, ptcopy, count);
507 Polyline(graphics->hdc, pti, count);
516 /* Conducts a linear search to find the bezier points that will back off
517 * the endpoint of the curve by a distance of amt. Linear search works
518 * better than binary in this case because there are multiple solutions,
519 * and binary searches often find a bad one. I don't think this is what
520 * Windows does but short of rendering the bezier without GDI's help it's
521 * the best we can do. If rev then work from the start of the passed points
522 * instead of the end. */
523 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
526 REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
527 INT i, first = 0, second = 1, third = 2, fourth = 3;
536 origx = pt[fourth].X;
537 origy = pt[fourth].Y;
538 memcpy(origpt, pt, sizeof(GpPointF) * 4);
540 for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
541 /* reset bezier points to original values */
542 memcpy(pt, origpt, sizeof(GpPointF) * 4);
543 /* Perform magic on bezier points. Order is important here.*/
544 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
545 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
546 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
547 shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
548 shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
549 shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
551 dx = pt[fourth].X - origx;
552 dy = pt[fourth].Y - origy;
554 diff = sqrt(dx * dx + dy * dy);
555 percent += 0.0005 * amt;
559 /* Draws bezier curves between given points, and if caps is true then draws an
560 * endcap at the end of the last line. */
561 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
562 GDIPCONST GpPointF * pt, INT count, BOOL caps)
566 GpStatus status = GenericError;
571 pti = GdipAlloc(count * sizeof(POINT));
572 ptcopy = GdipAlloc(count * sizeof(GpPointF));
575 status = OutOfMemory;
579 memcpy(ptcopy, pt, count * sizeof(GpPointF));
582 if(pen->endcap == LineCapArrowAnchor)
583 shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
584 else if((pen->endcap == LineCapCustom) && pen->customend)
585 shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
588 if(pen->startcap == LineCapArrowAnchor)
589 shorten_bezier_amt(ptcopy, pen->width, TRUE);
590 else if((pen->startcap == LineCapCustom) && pen->customstart)
591 shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
593 /* the direction of the line cap is parallel to the direction at the
594 * end of the bezier (which, if it has been shortened, is not the same
595 * as the direction from pt[count-2] to pt[count-1]) */
596 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
597 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
598 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
599 pt[count - 1].X, pt[count - 1].Y);
601 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
602 pt[0].X - (ptcopy[0].X - ptcopy[1].X),
603 pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
606 transform_and_round_points(graphics, pti, ptcopy, count);
608 PolyBezier(graphics->hdc, pti, count);
619 /* Draws a combination of bezier curves and lines between points. */
620 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
621 GDIPCONST BYTE * types, INT count, BOOL caps)
623 POINT *pti = GdipAlloc(count * sizeof(POINT));
624 BYTE *tp = GdipAlloc(count);
625 GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
627 GpStatus status = GenericError;
633 if(!pti || !tp || !ptcopy){
634 status = OutOfMemory;
638 for(i = 1; i < count; i++){
639 if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
640 if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
641 || !(types[i + 1] & PathPointTypeBezier)){
642 ERR("Bad bezier points\n");
649 memcpy(ptcopy, pt, count * sizeof(GpPointF));
651 /* If we are drawing caps, go through the points and adjust them accordingly,
652 * and draw the caps. */
654 switch(types[count - 1] & PathPointTypePathTypeMask){
655 case PathPointTypeBezier:
656 if(pen->endcap == LineCapArrowAnchor)
657 shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
658 else if((pen->endcap == LineCapCustom) && pen->customend)
659 shorten_bezier_amt(&ptcopy[count - 4],
660 pen->width * pen->customend->inset, FALSE);
662 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
663 pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
664 pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
665 pt[count - 1].X, pt[count - 1].Y);
668 case PathPointTypeLine:
669 if(pen->endcap == LineCapArrowAnchor)
670 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
671 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
673 else if((pen->endcap == LineCapCustom) && pen->customend)
674 shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
675 &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
676 pen->customend->inset * pen->width);
678 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
679 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
684 ERR("Bad path last point\n");
688 /* Find start of points */
689 for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
690 == PathPointTypeStart); j++);
692 switch(types[j] & PathPointTypePathTypeMask){
693 case PathPointTypeBezier:
694 if(pen->startcap == LineCapArrowAnchor)
695 shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
696 else if((pen->startcap == LineCapCustom) && pen->customstart)
697 shorten_bezier_amt(&ptcopy[j - 1],
698 pen->width * pen->customstart->inset, TRUE);
700 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
701 pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
702 pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
703 pt[j - 1].X, pt[j - 1].Y);
706 case PathPointTypeLine:
707 if(pen->startcap == LineCapArrowAnchor)
708 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
709 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
711 else if((pen->startcap == LineCapCustom) && pen->customstart)
712 shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
713 &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
714 pen->customstart->inset * pen->width);
716 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
717 pt[j].X, pt[j].Y, pt[j - 1].X,
722 ERR("Bad path points\n");
727 transform_and_round_points(graphics, pti, ptcopy, count);
729 for(i = 0; i < count; i++){
730 tp[i] = convert_path_point_type(types[i]);
733 PolyDraw(graphics->hdc, pti, tp, count);
745 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
753 return InvalidParameter;
755 *graphics = GdipAlloc(sizeof(GpGraphics));
756 if(!*graphics) return OutOfMemory;
758 if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
763 (*graphics)->hdc = hdc;
764 (*graphics)->hwnd = NULL;
765 (*graphics)->smoothing = SmoothingModeDefault;
766 (*graphics)->compqual = CompositingQualityDefault;
767 (*graphics)->interpolation = InterpolationModeDefault;
768 (*graphics)->pixeloffset = PixelOffsetModeDefault;
769 (*graphics)->compmode = CompositingModeSourceOver;
770 (*graphics)->unit = UnitDisplay;
771 (*graphics)->scale = 1.0;
776 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
780 if((ret = GdipCreateFromHDC(GetDC(hwnd), graphics)) != Ok)
783 (*graphics)->hwnd = hwnd;
788 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
789 GpMetafile **metafile)
793 if(!hemf || !metafile)
794 return InvalidParameter;
797 FIXME("not implemented\n");
799 return NotImplemented;
802 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
803 GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
805 IStream *stream = NULL;
809 GpStatus retval = GenericError;
811 if(!hwmf || !metafile || !placeable)
812 return InvalidParameter;
815 read = GetMetaFileBitsEx(hwmf, 0, NULL);
818 copy = GdipAlloc(read);
819 GetMetaFileBitsEx(hwmf, read, copy);
821 hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
824 read = GetEnhMetaFileBits(hemf, 0, NULL);
825 copy = GdipAlloc(read);
826 GetEnhMetaFileBits(hemf, read, copy);
827 DeleteEnhMetaFile(hemf);
829 if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
830 ERR("could not make stream\n");
835 *metafile = GdipAlloc(sizeof(GpMetafile));
837 retval = OutOfMemory;
841 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
842 (LPVOID*) &((*metafile)->image.picture)) != S_OK)
846 (*metafile)->image.type = ImageTypeMetafile;
847 (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
848 (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Right) / ((REAL) placeable->Inch);
849 (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
850 - placeable->BoundingBox.Left)) / ((REAL) placeable->Inch);
851 (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
852 - placeable->BoundingBox.Top)) / ((REAL) placeable->Inch);
853 (*metafile)->unit = UnitInch;
856 DeleteMetaFile(hwmf);
862 IStream_Release(stream);
866 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
867 UINT access, IStream **stream)
872 if(!stream || !filename)
873 return InvalidParameter;
875 if(access & GENERIC_WRITE)
876 dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
877 else if(access & GENERIC_READ)
878 dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
880 return InvalidParameter;
882 ret = SHCreateStreamOnFileW(filename, dwMode, stream);
884 return hresult_to_status(ret);
887 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
889 if(!graphics) return InvalidParameter;
891 ReleaseDC(graphics->hwnd, graphics->hdc);
893 GdipDeleteMatrix(graphics->worldtrans);
894 HeapFree(GetProcessHeap(), 0, graphics);
899 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
900 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
902 INT save_state, num_pts;
903 GpPointF points[MAX_ARC_PTS];
906 if(!graphics || !pen)
907 return InvalidParameter;
909 num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
911 save_state = prepare_dc(graphics, pen);
913 retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
915 restore_dc(graphics, save_state);
920 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
921 REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
927 if(!graphics || !pen)
928 return InvalidParameter;
939 save_state = prepare_dc(graphics, pen);
941 retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
943 restore_dc(graphics, save_state);
948 /* Approximates cardinal spline with Bezier curves. */
949 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
950 GDIPCONST GpPointF *points, INT count, REAL tension)
952 /* PolyBezier expects count*3-2 points. */
953 INT i, len_pt = count*3-2, save_state;
958 if(!graphics || !pen)
959 return InvalidParameter;
961 pt = GdipAlloc(len_pt * sizeof(GpPointF));
962 tension = tension * TENSION_CONST;
964 calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
967 pt[0].X = points[0].X;
968 pt[0].Y = points[0].Y;
972 for(i = 0; i < count-2; i++){
973 calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
977 pt[3*i+3].X = points[i+1].X;
978 pt[3*i+3].Y = points[i+1].Y;
983 calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
984 points[count-2].X, points[count-2].Y, tension, &x1, &y1);
988 pt[len_pt-1].X = points[count-1].X;
989 pt[len_pt-1].Y = points[count-1].Y;
991 save_state = prepare_dc(graphics, pen);
993 retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
996 restore_dc(graphics, save_state);
1001 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
1004 UINT width, height, srcw, srch;
1006 if(!graphics || !image)
1007 return InvalidParameter;
1009 GdipGetImageWidth(image, &width);
1010 GdipGetImageHeight(image, &height);
1012 srcw = width * (((REAL) INCH_HIMETRIC) /
1013 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX)));
1014 srch = height * (((REAL) INCH_HIMETRIC) /
1015 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY)));
1017 if(image->type != ImageTypeMetafile){
1022 IPicture_Render(image->picture, graphics->hdc, x, y, width, height,
1023 0, 0, srcw, srch, NULL);
1028 /* FIXME: partially implemented (only works for rectangular parallelograms) */
1029 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1030 GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
1031 REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
1032 DrawImageAbort callback, VOID * callbackData)
1038 TRACE("%p %p %p %d %f %f %f %f %d %p %p %p\n", graphics, image, points, count,
1039 srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1042 if(!graphics || !image || !points || !imageAttributes || count != 3)
1043 return InvalidParameter;
1045 if(srcUnit == UnitInch)
1046 dx = dy = (REAL) INCH_HIMETRIC;
1047 else if(srcUnit == UnitPixel){
1048 dx = ((REAL) INCH_HIMETRIC) /
1049 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
1050 dy = ((REAL) INCH_HIMETRIC) /
1051 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
1054 return NotImplemented;
1056 memcpy(ptf, points, 3 * sizeof(GpPointF));
1057 transform_and_round_points(graphics, pti, ptf, 3);
1059 /* IPicture renders bitmaps with the y-axis reversed
1060 * FIXME: flipping for unknown image type might not be correct. */
1061 if(image->type != ImageTypeMetafile){
1064 pti[0].y = pti[2].y;
1068 if(IPicture_Render(image->picture, graphics->hdc,
1069 pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
1070 srcx * dx, srcy * dy,
1071 srcwidth * dx, srcheight * dy,
1074 callback(callbackData);
1075 return GenericError;
1081 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
1082 REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
1083 REAL srcwidth, REAL srcheight, GpUnit srcUnit,
1084 GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
1085 VOID * callbackData)
1091 points[1].X = dstx + dstwidth;
1094 points[2].Y = dsty + dstheight;
1096 return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1097 srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
1100 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
1101 REAL y1, REAL x2, REAL y2)
1107 if(!pen || !graphics)
1108 return InvalidParameter;
1115 save_state = prepare_dc(graphics, pen);
1117 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1119 restore_dc(graphics, save_state);
1124 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
1125 INT y1, INT x2, INT y2)
1131 if(!pen || !graphics)
1132 return InvalidParameter;
1139 save_state = prepare_dc(graphics, pen);
1141 retval = draw_polyline(graphics, pen, pt, 2, TRUE);
1143 restore_dc(graphics, save_state);
1148 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
1149 GpPointF *points, INT count)
1154 if(!pen || !graphics || (count < 2))
1155 return InvalidParameter;
1157 save_state = prepare_dc(graphics, pen);
1159 retval = draw_polyline(graphics, pen, points, count, TRUE);
1161 restore_dc(graphics, save_state);
1166 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
1171 if(!pen || !graphics)
1172 return InvalidParameter;
1174 save_state = prepare_dc(graphics, pen);
1176 retval = draw_poly(graphics, pen, path->pathdata.Points,
1177 path->pathdata.Types, path->pathdata.Count, TRUE);
1179 restore_dc(graphics, save_state);
1184 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
1185 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1189 if(!graphics || !pen)
1190 return InvalidParameter;
1192 save_state = prepare_dc(graphics, pen);
1193 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1195 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1197 restore_dc(graphics, save_state);
1202 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
1203 INT y, INT width, INT height)
1209 if(!pen || !graphics)
1210 return InvalidParameter;
1214 ptf[1].X = x + width;
1216 ptf[2].X = x + width;
1217 ptf[2].Y = y + height;
1219 ptf[3].Y = y + height;
1221 save_state = prepare_dc(graphics, pen);
1222 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1224 transform_and_round_points(graphics, pti, ptf, 4);
1225 Polygon(graphics->hdc, pti, 4);
1227 restore_dc(graphics, save_state);
1232 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
1233 GpRectF* rects, INT count)
1239 if(!graphics || !pen || !rects || count < 1)
1240 return InvalidParameter;
1242 ptf = GdipAlloc(4 * count * sizeof(GpPointF));
1243 pti = GdipAlloc(4 * count * sizeof(POINT));
1251 for(i = 0; i < count; i++){
1252 ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
1253 ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
1254 ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
1255 ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
1258 save_state = prepare_dc(graphics, pen);
1259 SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1261 transform_and_round_points(graphics, pti, ptf, 4 * count);
1263 for(i = 0; i < count; i++)
1264 Polygon(graphics->hdc, &pti[4 * i], 4);
1266 restore_dc(graphics, save_state);
1274 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
1275 INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
1276 GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
1281 TEXTMETRICW textmet;
1282 GpPointF pt[2], rectcpy[4];
1285 REAL angle, ang_cos, ang_sin, rel_width, rel_height;
1286 INT sum = 0, height = 0, fit, fitcpy, save_state, i, j, lret, nwidth;
1289 if(!graphics || !string || !font || !brush || !rect)
1290 return InvalidParameter;
1292 if((brush->bt != BrushTypeSolidColor)){
1293 FIXME("not implemented for given parameters\n");
1294 return NotImplemented;
1298 TRACE("may be ignoring some format flags: attr %x\n", format->attr);
1300 if(length == -1) length = lstrlenW(string);
1302 stringdup = GdipAlloc(length * sizeof(WCHAR));
1303 if(!stringdup) return OutOfMemory;
1305 save_state = SaveDC(graphics->hdc);
1306 SetBkMode(graphics->hdc, TRANSPARENT);
1307 SetTextColor(graphics->hdc, brush->lb.lbColor);
1309 rectcpy[3].X = rectcpy[0].X = rect->X;
1310 rectcpy[1].Y = rectcpy[0].Y = rect->Y;
1311 rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
1312 rectcpy[3].Y = rectcpy[2].Y = rect->Y + rect->Height;
1313 transform_and_round_points(graphics, corners, rectcpy, 4);
1314 rel_width = sqrt((corners[1].x - corners[0].x) * (corners[1].x - corners[0].x) +
1315 (corners[1].y - corners[0].y) * (corners[1].y - corners[0].y))
1317 nwidth = roundr(rel_width * rect->Width);
1318 rel_height = sqrt((corners[2].x - corners[1].x) * (corners[2].x - corners[1].x) +
1319 (corners[2].y - corners[1].y) * (corners[2].y - corners[1].y))
1322 rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
1323 SelectClipRgn(graphics->hdc, rgn);
1325 /* Use gdi to find the font, then perform transformations on it (height,
1327 SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
1328 GetTextMetricsW(graphics->hdc, &textmet);
1329 memcpy(&lfw, &font->lfw, sizeof(LOGFONTW));
1331 lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
1332 lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
1338 GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
1339 angle = gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
1340 ang_cos = cos(angle);
1341 ang_sin = sin(angle);
1342 lfw.lfEscapement = lfw.lfOrientation = -roundr((angle / M_PI) * 1800.0);
1344 gdifont = CreateFontIndirectW(&lfw);
1345 DeleteObject(SelectObject(graphics->hdc, CreateFontIndirectW(&lfw)));
1347 for(i = 0, j = 0; i < length; i++){
1348 if(!isprintW(string[i]) && (string[i] != '\n'))
1351 stringdup[j] = string[i];
1358 while(sum < length){
1359 GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
1360 nwidth, &fit, NULL, &size);
1364 TabbedTextOutW(graphics->hdc,
1365 corners[0].x + roundr(ang_sin * (REAL) height),
1366 corners[0].y + roundr(ang_cos * (REAL) height),
1367 stringdup + sum, 1, 0, 0, 0);
1371 for(lret = 0; lret < fit; lret++)
1372 if(*(stringdup + sum + lret) == '\n')
1375 /* Line break code (may look strange, but it imitates windows). */
1377 fit = lret; /* this is not an off-by-one error */
1378 else if(fit < (length - sum)){
1379 if(*(stringdup + sum + fit) == ' ')
1380 while(*(stringdup + sum + fit) == ' ')
1383 while(*(stringdup + sum + fit - 1) != ' '){
1386 if(*(stringdup + sum + fit) == '\t')
1396 TabbedTextOutW(graphics->hdc,
1397 corners[0].x - roundr(ang_sin * (REAL) height),
1398 corners[0].y + roundr(ang_cos * (REAL) height),
1399 stringdup + sum, min(length - sum, fit), 0, 0, 0);
1401 sum += fit + (lret < fitcpy ? 1 : 0);
1404 if(height > roundr(rect->Height * rel_height))
1407 /* Stop if this was a linewrap (but not if it was a linebreak). */
1408 if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
1413 DeleteObject(gdifont);
1415 RestoreDC(graphics->hdc, save_state);
1420 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
1425 if(!brush || !graphics || !path)
1426 return InvalidParameter;
1428 save_state = SaveDC(graphics->hdc);
1429 EndPath(graphics->hdc);
1430 SelectObject(graphics->hdc, brush->gdibrush);
1431 SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
1434 BeginPath(graphics->hdc);
1435 retval = draw_poly(graphics, NULL, path->pathdata.Points,
1436 path->pathdata.Types, path->pathdata.Count, FALSE);
1441 EndPath(graphics->hdc);
1442 FillPath(graphics->hdc);
1447 RestoreDC(graphics->hdc, save_state);
1452 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
1453 REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1457 if(!graphics || !brush)
1458 return InvalidParameter;
1460 save_state = SaveDC(graphics->hdc);
1461 EndPath(graphics->hdc);
1462 SelectObject(graphics->hdc, brush->gdibrush);
1463 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1465 draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
1467 RestoreDC(graphics->hdc, save_state);
1472 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
1473 GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
1476 GpPointF *ptf = NULL;
1478 GpStatus retval = Ok;
1480 if(!graphics || !brush || !points || !count)
1481 return InvalidParameter;
1483 ptf = GdipAlloc(count * sizeof(GpPointF));
1484 pti = GdipAlloc(count * sizeof(POINT));
1486 retval = OutOfMemory;
1490 memcpy(ptf, points, count * sizeof(GpPointF));
1492 save_state = SaveDC(graphics->hdc);
1493 EndPath(graphics->hdc);
1494 SelectObject(graphics->hdc, brush->gdibrush);
1495 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1496 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1499 transform_and_round_points(graphics, pti, ptf, count);
1500 Polygon(graphics->hdc, pti, count);
1502 RestoreDC(graphics->hdc, save_state);
1511 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
1512 GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
1515 GpPointF *ptf = NULL;
1517 GpStatus retval = Ok;
1519 if(!graphics || !brush || !points || !count)
1520 return InvalidParameter;
1522 ptf = GdipAlloc(count * sizeof(GpPointF));
1523 pti = GdipAlloc(count * sizeof(POINT));
1525 retval = OutOfMemory;
1529 for(i = 0; i < count; i ++){
1530 ptf[i].X = (REAL) points[i].X;
1531 ptf[i].Y = (REAL) points[i].Y;
1534 save_state = SaveDC(graphics->hdc);
1535 EndPath(graphics->hdc);
1536 SelectObject(graphics->hdc, brush->gdibrush);
1537 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1538 SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
1541 transform_and_round_points(graphics, pti, ptf, count);
1542 Polygon(graphics->hdc, pti, count);
1544 RestoreDC(graphics->hdc, save_state);
1553 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
1554 REAL x, REAL y, REAL width, REAL height)
1560 if(!graphics || !brush)
1561 return InvalidParameter;
1565 ptf[1].X = x + width;
1567 ptf[2].X = x + width;
1568 ptf[2].Y = y + height;
1570 ptf[3].Y = y + height;
1572 save_state = SaveDC(graphics->hdc);
1573 EndPath(graphics->hdc);
1574 SelectObject(graphics->hdc, brush->gdibrush);
1575 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1577 transform_and_round_points(graphics, pti, ptf, 4);
1579 Polygon(graphics->hdc, pti, 4);
1581 RestoreDC(graphics->hdc, save_state);
1586 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
1587 INT x, INT y, INT width, INT height)
1593 if(!graphics || !brush)
1594 return InvalidParameter;
1598 ptf[1].X = x + width;
1600 ptf[2].X = x + width;
1601 ptf[2].Y = y + height;
1603 ptf[3].Y = y + height;
1605 save_state = SaveDC(graphics->hdc);
1606 EndPath(graphics->hdc);
1607 SelectObject(graphics->hdc, brush->gdibrush);
1608 SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
1610 transform_and_round_points(graphics, pti, ptf, 4);
1612 Polygon(graphics->hdc, pti, 4);
1614 RestoreDC(graphics->hdc, save_state);
1619 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
1620 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
1621 CompositingMode *mode)
1623 if(!graphics || !mode)
1624 return InvalidParameter;
1626 *mode = graphics->compmode;
1631 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
1632 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
1633 CompositingQuality *quality)
1635 if(!graphics || !quality)
1636 return InvalidParameter;
1638 *quality = graphics->compqual;
1643 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
1644 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
1645 InterpolationMode *mode)
1647 if(!graphics || !mode)
1648 return InvalidParameter;
1650 *mode = graphics->interpolation;
1655 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
1657 if(!graphics || !scale)
1658 return InvalidParameter;
1660 *scale = graphics->scale;
1665 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
1667 if(!graphics || !unit)
1668 return InvalidParameter;
1670 *unit = graphics->unit;
1675 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
1676 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1679 if(!graphics || !mode)
1680 return InvalidParameter;
1682 *mode = graphics->pixeloffset;
1687 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
1688 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
1690 if(!graphics || !mode)
1691 return InvalidParameter;
1693 *mode = graphics->smoothing;
1698 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
1699 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
1700 TextRenderingHint *hint)
1702 if(!graphics || !hint)
1703 return InvalidParameter;
1705 *hint = graphics->texthint;
1710 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
1712 if(!graphics || !matrix)
1713 return InvalidParameter;
1715 memcpy(matrix, graphics->worldtrans, sizeof(GpMatrix));
1719 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
1720 GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
1721 GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
1722 INT *codepointsfitted, INT *linesfilled)
1726 INT sum = 0, height = 0, fit, fitcpy, max_width = 0, i, j, lret, nwidth;
1729 if(!graphics || !string || !font || !rect)
1730 return InvalidParameter;
1732 if(format || codepointsfitted || linesfilled){
1733 FIXME("not implemented for given parameters\n");
1735 TRACE("format attr is %d\n", format->attr);
1736 return NotImplemented;
1739 if(length == -1) length = lstrlenW(string);
1741 stringdup = GdipAlloc(length * sizeof(WCHAR));
1742 if(!stringdup) return OutOfMemory;
1744 oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
1745 nwidth = roundr(rect->Width);
1747 for(i = 0, j = 0; i < length; i++){
1748 if(!isprintW(string[i]) && (string[i] != '\n'))
1751 stringdup[j] = string[i];
1758 while(sum < length){
1759 GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
1760 nwidth, &fit, NULL, &size);
1766 for(lret = 0; lret < fit; lret++)
1767 if(*(stringdup + sum + lret) == '\n')
1770 /* Line break code (may look strange, but it imitates windows). */
1772 fit = lret; /* this is not an off-by-one error */
1773 else if(fit < (length - sum)){
1774 if(*(stringdup + sum + fit) == ' ')
1775 while(*(stringdup + sum + fit) == ' ')
1778 while(*(stringdup + sum + fit - 1) != ' '){
1781 if(*(stringdup + sum + fit) == '\t')
1791 GetTextExtentExPointW(graphics->hdc, stringdup + sum, fit,
1792 nwidth, &j, NULL, &size);
1794 sum += fit + (lret < fitcpy ? 1 : 0);
1796 max_width = max(max_width, size.cx);
1798 if(height > roundr(rect->Height))
1802 bounds->X = rect->X;
1803 bounds->Y = rect->Y;
1804 bounds->Width = (REAL)max_width;
1805 bounds->Height = min((REAL)height, rect->Height);
1807 DeleteObject(SelectObject(graphics->hdc, oldfont));
1812 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
1817 return InvalidParameter;
1820 FIXME("graphics state not implemented\n");
1822 return NotImplemented;
1825 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
1826 GpMatrixOrder order)
1829 return InvalidParameter;
1831 return GdipRotateMatrix(graphics->worldtrans, angle, order);
1834 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
1838 if(!graphics || !state)
1839 return InvalidParameter;
1842 FIXME("graphics state not implemented\n");
1844 return NotImplemented;
1847 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
1848 REAL sy, GpMatrixOrder order)
1851 return InvalidParameter;
1853 return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
1856 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
1857 CompositingMode mode)
1860 return InvalidParameter;
1862 graphics->compmode = mode;
1867 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
1868 CompositingQuality quality)
1871 return InvalidParameter;
1873 graphics->compqual = quality;
1878 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
1879 InterpolationMode mode)
1882 return InvalidParameter;
1884 graphics->interpolation = mode;
1889 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
1891 if(!graphics || (scale <= 0.0))
1892 return InvalidParameter;
1894 graphics->scale = scale;
1899 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
1901 if(!graphics || (unit == UnitWorld))
1902 return InvalidParameter;
1904 graphics->unit = unit;
1909 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
1913 return InvalidParameter;
1915 graphics->pixeloffset = mode;
1920 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
1923 return InvalidParameter;
1925 graphics->smoothing = mode;
1930 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
1931 TextRenderingHint hint)
1934 return InvalidParameter;
1936 graphics->texthint = hint;
1941 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
1943 if(!graphics || !matrix)
1944 return InvalidParameter;
1946 GdipDeleteMatrix(graphics->worldtrans);
1947 return GdipCloneMatrix(matrix, &graphics->worldtrans);
1950 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
1951 REAL dy, GpMatrixOrder order)
1954 return InvalidParameter;
1956 return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);