graphics.c 121 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (C) 2007 Google (Evan Stade)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdarg.h>
#include <math.h>
21
#include <limits.h>
22 23 24 25 26

#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "wingdi.h"
27
#include "wine/unicode.h"
28 29 30 31 32 33 34

#define COBJMACROS
#include "objbase.h"
#include "ocidl.h"
#include "olectl.h"
#include "ole2.h"

35 36 37
#include "winreg.h"
#include "shlwapi.h"

38 39 40
#include "gdiplus.h"
#include "gdiplus_private.h"
#include "wine/debug.h"
41
#include "wine/list.h"
42

43 44 45 46
WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);

/* looks-right constants */
#define ANCHOR_WIDTH (2.0)
47
#define MAX_ITERS (50)
48

49 50 51 52 53 54 55 56 57 58 59 60
/* Converts angle (in degrees) to x/y coordinates */
static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
{
    REAL radAngle, hypotenuse;

    radAngle = deg2rad(angle);
    hypotenuse = 50.0; /* arbitrary */

    *x = x_0 + cos(radAngle) * hypotenuse;
    *y = y_0 + sin(radAngle) * hypotenuse;
}

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/* Converts from gdiplus path point type to gdi path point type. */
static BYTE convert_path_point_type(BYTE type)
{
    BYTE ret;

    switch(type & PathPointTypePathTypeMask){
        case PathPointTypeBezier:
            ret = PT_BEZIERTO;
            break;
        case PathPointTypeLine:
            ret = PT_LINETO;
            break;
        case PathPointTypeStart:
            ret = PT_MOVETO;
            break;
        default:
            ERR("Bad point type\n");
            return 0;
    }

    if(type & PathPointTypeCloseSubpath)
        ret |= PT_CLOSEFIGURE;

    return ret;
}

87 88 89 90
static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
{
    HPEN gdipen;
    REAL width;
91
    INT save_state = SaveDC(graphics->hdc), i, numdashes;
92
    GpPointF pt[2];
93
    DWORD dash_array[MAX_DASHLEN];
94 95 96

    EndPath(graphics->hdc);

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    if(pen->unit == UnitPixel){
        width = pen->width;
    }
    else{
        /* Get an estimate for the amount the pen width is affected by the world
         * transform. (This is similar to what some of the wine drivers do.) */
        pt[0].X = 0.0;
        pt[0].Y = 0.0;
        pt[1].X = 1.0;
        pt[1].Y = 1.0;
        GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
        width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
                     (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);

        width *= pen->width * convert_unit(graphics->hdc,
                              pen->unit == UnitWorld ? graphics->unit : pen->unit);
    }
114

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    if(pen->dash == DashStyleCustom){
        numdashes = min(pen->numdashes, MAX_DASHLEN);

        TRACE("dashes are: ");
        for(i = 0; i < numdashes; i++){
            dash_array[i] = roundr(width * pen->dashes[i]);
            TRACE("%d, ", dash_array[i]);
        }
        TRACE("\n and the pen style is %x\n", pen->style);

        gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb,
                              numdashes, dash_array);
    }
    else
        gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb, 0, NULL);

131 132 133 134 135 136 137 138 139 140 141
    SelectObject(graphics->hdc, gdipen);

    return save_state;
}

static void restore_dc(GpGraphics *graphics, INT state)
{
    DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
    RestoreDC(graphics->hdc, state);
}

142 143 144 145 146 147 148 149 150 151 152
/* This helper applies all the changes that the points listed in ptf need in
 * order to be drawn on the device context.  In the end, this should include at
 * least:
 *  -scaling by page unit
 *  -applying world transformation
 *  -converting from float to int
 * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
 * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
 * gdi to draw, and these functions would irreparably mess with line widths.
 */
static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
153
    GpPointF *ptf, INT count)
154 155
{
    REAL unitscale;
156
    GpMatrix *matrix;
157 158
    int i;

159
    unitscale = convert_unit(graphics->hdc, graphics->unit);
160

161 162 163 164
    /* apply page scale */
    if(graphics->unit != UnitDisplay)
        unitscale *= graphics->scale;

165 166 167
    GdipCloneMatrix(graphics->worldtrans, &matrix);
    GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
    GdipTransformMatrixPoints(matrix, ptf, count);
Evan Stade's avatar
Evan Stade committed
168
    GdipDeleteMatrix(matrix);
169

170
    for(i = 0; i < count; i++){
171 172
        pti[i].x = roundr(ptf[i].X);
        pti[i].y = roundr(ptf[i].Y);
173 174 175
    }
}

176
static ARGB blend_colors(ARGB start, ARGB end, REAL position)
177 178 179 180
{
    ARGB result=0;
    ARGB i;
    for (i=0xff; i<=0xff0000; i = i << 8)
181
        result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
182 183 184
    return result;
}

185 186 187 188
static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
{
    REAL blendfac;

189 190 191 192 193 194 195 196 197 198 199 200 201
    /* clamp to between 0.0 and 1.0, using the wrap mode */
    if (brush->wrap == WrapModeTile)
    {
        position = fmodf(position, 1.0f);
        if (position < 0.0f) position += 1.0f;
    }
    else /* WrapModeFlip* */
    {
        position = fmodf(position, 2.0f);
        if (position < 0.0f) position += 2.0f;
        if (position > 1.0f) position = 2.0f - position;
    }

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    if (brush->blendcount == 1)
        blendfac = position;
    else
    {
        int i=1;
        REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
        REAL range;

        /* locate the blend positions surrounding this position */
        while (position > brush->blendpos[i])
            i++;

        /* interpolate between the blend positions */
        left_blendpos = brush->blendpos[i-1];
        left_blendfac = brush->blendfac[i-1];
        right_blendpos = brush->blendpos[i];
        right_blendfac = brush->blendfac[i];
        range = right_blendpos - left_blendpos;
        blendfac = (left_blendfac * (right_blendpos - position) +
                    right_blendfac * (position - left_blendpos)) / range;
    }
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

    if (brush->pblendcount == 0)
        return blend_colors(brush->startcolor, brush->endcolor, blendfac);
    else
    {
        int i=1;
        ARGB left_blendcolor, right_blendcolor;
        REAL left_blendpos, right_blendpos;

        /* locate the blend colors surrounding this position */
        while (blendfac > brush->pblendpos[i])
            i++;

        /* interpolate between the blend colors */
        left_blendpos = brush->pblendpos[i-1];
        left_blendcolor = brush->pblendcolor[i-1];
        right_blendpos = brush->pblendpos[i];
        right_blendcolor = brush->pblendcolor[i];
        blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
        return blend_colors(left_blendcolor, right_blendcolor, blendfac);
    }
244 245
}

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
{
    switch (brush->bt)
    {
    case BrushTypeLinearGradient:
    {
        GpLineGradient *line = (GpLineGradient*)brush;
        RECT rc;

        SelectClipPath(graphics->hdc, RGN_AND);
        if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
        {
            GpPointF endpointsf[2];
            POINT endpointsi[2];
            POINT poly[4];

            SelectObject(graphics->hdc, GetStockObject(NULL_PEN));

            endpointsf[0] = line->startpoint;
            endpointsf[1] = line->endpoint;
            transform_and_round_points(graphics, endpointsi, endpointsf, 2);

            if (abs(endpointsi[0].x-endpointsi[1].x) > abs(endpointsi[0].y-endpointsi[1].y))
            {
                /* vertical-ish gradient */
                int startx, endx; /* x co-ordinates of endpoints shifted to intersect the top of the visible rectangle */
272
                int startbottomx; /* x co-ordinate of start point shifted to intersect the bottom of the visible rectangle */
273 274 275
                int width;
                COLORREF col;
                HBRUSH hbrush, hprevbrush;
276 277 278
                int leftx, rightx; /* x co-ordinates where the leftmost and rightmost gradient lines hit the top of the visible rectangle */
                int x;
                int tilt; /* horizontal distance covered by a gradient line */
279 280 281 282 283

                startx = roundr((rc.top - endpointsf[0].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[0].X);
                endx = roundr((rc.top - endpointsf[1].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[1].X);
                width = endx - startx;
                startbottomx = roundr((rc.bottom - endpointsf[0].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[0].X);
284
                tilt = startx - startbottomx;
285

286 287 288 289 290 291 292 293 294 295
                if (startx >= startbottomx)
                {
                    leftx = rc.left;
                    rightx = rc.right + tilt;
                }
                else
                {
                    leftx = rc.left + tilt;
                    rightx = rc.right;
                }
296 297 298 299 300 301

                poly[0].y = rc.bottom;
                poly[1].y = rc.top;
                poly[2].y = rc.top;
                poly[3].y = rc.bottom;

302
                for (x=leftx; x<=rightx; x++)
303
                {
304
                    ARGB argb = blend_line_gradient(line, (x-startx)/(REAL)width);
305 306 307
                    col = ARGB2COLORREF(argb);
                    hbrush = CreateSolidBrush(col);
                    hprevbrush = SelectObject(graphics->hdc, hbrush);
308 309
                    poly[0].x = x - tilt - 1;
                    poly[1].x = x - 1;
310 311
                    poly[2].x = x;
                    poly[3].x = x - tilt;
312 313 314 315 316 317 318 319 320
                    Polygon(graphics->hdc, poly, 4);
                    SelectObject(graphics->hdc, hprevbrush);
                    DeleteObject(hbrush);
                }
            }
            else if (endpointsi[0].y != endpointsi[1].y)
            {
                /* horizontal-ish gradient */
                int starty, endy; /* y co-ordinates of endpoints shifted to intersect the left of the visible rectangle */
321
                int startrighty; /* y co-ordinate of start point shifted to intersect the right of the visible rectangle */
322 323 324
                int height;
                COLORREF col;
                HBRUSH hbrush, hprevbrush;
325 326 327
                int topy, bottomy; /* y co-ordinates where the topmost and bottommost gradient lines hit the left of the visible rectangle */
                int y;
                int tilt; /* vertical distance covered by a gradient line */
328 329 330 331 332

                starty = roundr((rc.left - endpointsf[0].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[0].Y);
                endy = roundr((rc.left - endpointsf[1].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[1].Y);
                height = endy - starty;
                startrighty = roundr((rc.right - endpointsf[0].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[0].Y);
333
                tilt = starty - startrighty;
334

335 336 337 338 339 340 341 342 343 344
                if (starty >= startrighty)
                {
                    topy = rc.top;
                    bottomy = rc.bottom + tilt;
                }
                else
                {
                    topy = rc.top + tilt;
                    bottomy = rc.bottom;
                }
345 346 347 348 349 350

                poly[0].x = rc.right;
                poly[1].x = rc.left;
                poly[2].x = rc.left;
                poly[3].x = rc.right;

351
                for (y=topy; y<=bottomy; y++)
352
                {
353
                    ARGB argb = blend_line_gradient(line, (y-starty)/(REAL)height);
354 355 356
                    col = ARGB2COLORREF(argb);
                    hbrush = CreateSolidBrush(col);
                    hprevbrush = SelectObject(graphics->hdc, hbrush);
357 358
                    poly[0].y = y - tilt - 1;
                    poly[1].y = y - 1;
359 360
                    poly[2].y = y;
                    poly[3].y = y - tilt;
361 362 363 364 365 366 367 368 369
                    Polygon(graphics->hdc, poly, 4);
                    SelectObject(graphics->hdc, hprevbrush);
                    DeleteObject(hbrush);
                }
            }
            /* else startpoint == endpoint */
        }
        break;
    }
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    case BrushTypeSolidColor:
    {
        GpSolidFill *fill = (GpSolidFill*)brush;
        if (fill->bmp)
        {
            RECT rc;
            /* partially transparent fill */

            SelectClipPath(graphics->hdc, RGN_AND);
            if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
            {
                HDC hdc = CreateCompatibleDC(NULL);
                HBITMAP oldbmp;
                BLENDFUNCTION bf;

                if (!hdc) break;

                oldbmp = SelectObject(hdc, fill->bmp);

                bf.BlendOp = AC_SRC_OVER;
                bf.BlendFlags = 0;
                bf.SourceConstantAlpha = 255;
                bf.AlphaFormat = AC_SRC_ALPHA;

                GdiAlphaBlend(graphics->hdc, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, hdc, 0, 0, 1, 1, bf);

                SelectObject(hdc, oldbmp);
                DeleteDC(hdc);
            }

            break;
        }
        /* else fall through */
    }
404 405 406 407 408 409 410
    default:
        SelectObject(graphics->hdc, brush->gdibrush);
        FillPath(graphics->hdc);
        break;
    }
}

411
/* GdipDrawPie/GdipFillPie helper function */
412 413
static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
    REAL height, REAL startAngle, REAL sweepAngle)
414
{
415 416
    GpPointF ptf[4];
    POINT pti[4];
417

418 419 420 421 422 423 424
    ptf[0].X = x;
    ptf[0].Y = y;
    ptf[1].X = x + width;
    ptf[1].Y = y + height;

    deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
    deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
425

426
    transform_and_round_points(graphics, pti, ptf, 4);
427

428 429
    Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
        pti[2].y, pti[3].x, pti[3].y);
430 431
}

432
/* Draws the linecap the specified color and size on the hdc.  The linecap is in
433 434
 * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
 * should not be called on an hdc that has a path you care about. */
435
static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
436
    const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
437
{
438
    HGDIOBJ oldbrush = NULL, oldpen = NULL;
439
    GpMatrix *matrix = NULL;
440 441
    HBRUSH brush = NULL;
    HPEN pen = NULL;
442
    PointF ptf[4], *custptf = NULL;
443 444
    POINT pt[4], *custpt = NULL;
    BYTE *tp = NULL;
445
    REAL theta, dsmall, dbig, dx, dy = 0.0;
446 447
    INT i, count;
    LOGBRUSH lb;
448
    BOOL customstroke;
449

450
    if((x1 == x2) && (y1 == y2))
451 452
        return;

453 454
    theta = gdiplus_atan2(y2 - y1, x2 - x1);

455 456 457 458 459 460 461 462 463 464 465 466
    customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
    if(!customstroke){
        brush = CreateSolidBrush(color);
        lb.lbStyle = BS_SOLID;
        lb.lbColor = color;
        lb.lbHatch = 0;
        pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
                           PS_JOIN_MITER, 1, &lb, 0,
                           NULL);
        oldbrush = SelectObject(graphics->hdc, brush);
        oldpen = SelectObject(graphics->hdc, pen);
    }
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483

    switch(cap){
        case LineCapFlat:
            break;
        case LineCapSquare:
        case LineCapSquareAnchor:
        case LineCapDiamondAnchor:
            size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
            if(cap == LineCapDiamondAnchor){
                dsmall = cos(theta + M_PI_2) * size;
                dbig = sin(theta + M_PI_2) * size;
            }
            else{
                dsmall = cos(theta + M_PI_4) * size;
                dbig = sin(theta + M_PI_4) * size;
            }

484 485
            ptf[0].X = x2 - dsmall;
            ptf[1].X = x2 + dbig;
486

487 488
            ptf[0].Y = y2 - dbig;
            ptf[3].Y = y2 + dsmall;
489

490 491
            ptf[1].Y = y2 - dsmall;
            ptf[2].Y = y2 + dbig;
492

493 494
            ptf[3].X = x2 - dbig;
            ptf[2].X = x2 + dsmall;
495

496 497
            transform_and_round_points(graphics, pt, ptf, 4);
            Polygon(graphics->hdc, pt, 4);
498 499 500 501 502

            break;
        case LineCapArrowAnchor:
            size = size * 4.0 / sqrt(3.0);

503 504
            dx = cos(M_PI / 6.0 + theta) * size;
            dy = sin(M_PI / 6.0 + theta) * size;
505

506 507
            ptf[0].X = x2 - dx;
            ptf[0].Y = y2 - dy;
508

509 510
            dx = cos(- M_PI / 6.0 + theta) * size;
            dy = sin(- M_PI / 6.0 + theta) * size;
511

512 513
            ptf[1].X = x2 - dx;
            ptf[1].Y = y2 - dy;
514

515 516
            ptf[2].X = x2;
            ptf[2].Y = y2;
517

518 519
            transform_and_round_points(graphics, pt, ptf, 3);
            Polygon(graphics->hdc, pt, 3);
520 521 522 523 524

            break;
        case LineCapRoundAnchor:
            dx = dy = ANCHOR_WIDTH * size / 2.0;

525 526 527 528 529 530 531
            ptf[0].X = x2 - dx;
            ptf[0].Y = y2 - dy;
            ptf[1].X = x2 + dx;
            ptf[1].Y = y2 + dy;

            transform_and_round_points(graphics, pt, ptf, 2);
            Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
532 533 534 535 536 537 538

            break;
        case LineCapTriangle:
            size = size / 2.0;
            dx = cos(M_PI_2 + theta) * size;
            dy = sin(M_PI_2 + theta) * size;

539 540 541 542
            ptf[0].X = x2 - dx;
            ptf[0].Y = y2 - dy;
            ptf[1].X = x2 + dx;
            ptf[1].Y = y2 + dy;
543

544 545
            dx = cos(theta) * size;
            dy = sin(theta) * size;
546

547 548
            ptf[2].X = x2 + dx;
            ptf[2].Y = y2 + dy;
549

550 551
            transform_and_round_points(graphics, pt, ptf, 3);
            Polygon(graphics->hdc, pt, 3);
552 553 554

            break;
        case LineCapRound:
555 556 557 558 559 560 561
            dx = dy = size / 2.0;

            ptf[0].X = x2 - dx;
            ptf[0].Y = y2 - dy;
            ptf[1].X = x2 + dx;
            ptf[1].Y = y2 + dy;

562 563
            dx = -cos(M_PI_2 + theta) * size;
            dy = -sin(M_PI_2 + theta) * size;
564

565 566 567 568
            ptf[2].X = x2 - dx;
            ptf[2].Y = y2 - dy;
            ptf[3].X = x2 + dx;
            ptf[3].Y = y2 + dy;
569

570 571 572
            transform_and_round_points(graphics, pt, ptf, 4);
            Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
                pt[2].y, pt[3].x, pt[3].y);
573 574 575

            break;
        case LineCapCustom:
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
            if(!custom)
                break;

            count = custom->pathdata.Count;
            custptf = GdipAlloc(count * sizeof(PointF));
            custpt = GdipAlloc(count * sizeof(POINT));
            tp = GdipAlloc(count);

            if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
                goto custend;

            memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));

            GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
            GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
                             MatrixOrderAppend);
            GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
            GdipTransformMatrixPoints(matrix, custptf, count);

595 596 597
            transform_and_round_points(graphics, custpt, custptf, count);

            for(i = 0; i < count; i++)
598 599
                tp[i] = convert_path_point_type(custom->pathdata.Types[i]);

600
            if(custom->fill){
601 602 603 604
                BeginPath(graphics->hdc);
                PolyDraw(graphics->hdc, custpt, tp, count);
                EndPath(graphics->hdc);
                StrokeAndFillPath(graphics->hdc);
605 606
            }
            else
607
                PolyDraw(graphics->hdc, custpt, tp, count);
608 609 610 611 612 613 614

custend:
            GdipFree(custptf);
            GdipFree(custpt);
            GdipFree(tp);
            GdipDeleteMatrix(matrix);
            break;
615 616 617 618
        default:
            break;
    }

619 620 621 622 623 624
    if(!customstroke){
        SelectObject(graphics->hdc, oldbrush);
        SelectObject(graphics->hdc, oldpen);
        DeleteObject(brush);
        DeleteObject(pen);
    }
625 626 627
}

/* Shortens the line by the given percent by changing x2, y2.
628 629
 * If percent is > 1.0 then the line will change direction.
 * If percent is negative it can lengthen the line. */
630 631 632 633 634 635 636
static void shorten_line_percent(REAL x1, REAL  y1, REAL *x2, REAL *y2, REAL percent)
{
    REAL dist, theta, dx, dy;

    if((y1 == *y2) && (x1 == *x2))
        return;

637
    dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
638
    theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
639 640 641
    dx = cos(theta) * dist;
    dy = sin(theta) * dist;

642 643
    *x2 = *x2 + dx;
    *y2 = *y2 + dy;
644 645 646
}

/* Shortens the line by the given amount by changing x2, y2.
647 648
 * If the amount is greater than the distance, the line will become length 0.
 * If the amount is negative, it can lengthen the line. */
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
{
    REAL dx, dy, percent;

    dx = *x2 - x1;
    dy = *y2 - y1;
    if(dx == 0 && dy == 0)
        return;

    percent = amt / sqrt(dx * dx + dy * dy);
    if(percent >= 1.0){
        *x2 = x1;
        *y2 = y1;
        return;
    }

    shorten_line_percent(x1, y1, x2, y2, percent);
}

/* Draws lines between the given points, and if caps is true then draws an endcap
669
 * at the end of the last line. */
670 671
static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
    GDIPCONST GpPointF * pt, INT count, BOOL caps)
672
{
673 674
    POINT *pti = NULL;
    GpPointF *ptcopy = NULL;
675 676 677 678 679 680
    GpStatus status = GenericError;

    if(!count)
        return Ok;

    pti = GdipAlloc(count * sizeof(POINT));
681
    ptcopy = GdipAlloc(count * sizeof(GpPointF));
682

683
    if(!pti || !ptcopy){
684 685 686
        status = OutOfMemory;
        goto end;
    }
687

688
    memcpy(ptcopy, pt, count * sizeof(GpPointF));
689

690
    if(caps){
691
        if(pen->endcap == LineCapArrowAnchor)
692 693
            shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
                             &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
694
        else if((pen->endcap == LineCapCustom) && pen->customend)
695 696 697 698 699 700 701 702 703 704
            shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
                             &ptcopy[count-1].X, &ptcopy[count-1].Y,
                             pen->customend->inset * pen->width);

        if(pen->startcap == LineCapArrowAnchor)
            shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
                             &ptcopy[0].X, &ptcopy[0].Y, pen->width);
        else if((pen->startcap == LineCapCustom) && pen->customstart)
            shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
                             &ptcopy[0].X, &ptcopy[0].Y,
705
                             pen->customstart->inset * pen->width);
706

707
        draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
708
                 pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
709
        draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
710
                         pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
711
    }
712 713

    transform_and_round_points(graphics, pti, ptcopy, count);
714

715 716
    if(Polyline(graphics->hdc, pti, count))
        status = Ok;
717 718

end:
719
    GdipFree(pti);
720
    GdipFree(ptcopy);
721 722

    return status;
723 724
}

725 726 727 728 729
/* Conducts a linear search to find the bezier points that will back off
 * the endpoint of the curve by a distance of amt. Linear search works
 * better than binary in this case because there are multiple solutions,
 * and binary searches often find a bad one. I don't think this is what
 * Windows does but short of rendering the bezier without GDI's help it's
730 731 732
 * the best we can do. If rev then work from the start of the passed points
 * instead of the end. */
static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
733 734
{
    GpPointF origpt[4];
735 736 737 738 739 740 741 742 743
    REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
    INT i, first = 0, second = 1, third = 2, fourth = 3;

    if(rev){
        first = 3;
        second = 2;
        third = 1;
        fourth = 0;
    }
744

745 746
    origx = pt[fourth].X;
    origy = pt[fourth].Y;
747 748 749 750 751 752
    memcpy(origpt, pt, sizeof(GpPointF) * 4);

    for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
        /* reset bezier points to original values */
        memcpy(pt, origpt, sizeof(GpPointF) * 4);
        /* Perform magic on bezier points. Order is important here.*/
753 754 755 756 757 758
        shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
        shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
        shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
        shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
        shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
        shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
759

760 761
        dx = pt[fourth].X - origx;
        dy = pt[fourth].Y - origy;
762 763 764 765 766 767 768

        diff = sqrt(dx * dx + dy * dy);
        percent += 0.0005 * amt;
    }
}

/* Draws bezier curves between given points, and if caps is true then draws an
769
 * endcap at the end of the last line. */
770 771
static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
    GDIPCONST GpPointF * pt, INT count, BOOL caps)
772
{
773
    POINT *pti;
774
    GpPointF *ptcopy;
775 776 777 778 779 780
    GpStatus status = GenericError;

    if(!count)
        return Ok;

    pti = GdipAlloc(count * sizeof(POINT));
781
    ptcopy = GdipAlloc(count * sizeof(GpPointF));
782

783
    if(!pti || !ptcopy){
784 785 786
        status = OutOfMemory;
        goto end;
    }
787

788
    memcpy(ptcopy, pt, count * sizeof(GpPointF));
789

790
    if(caps){
791
        if(pen->endcap == LineCapArrowAnchor)
792
            shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
793 794 795
        else if((pen->endcap == LineCapCustom) && pen->customend)
            shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
                               FALSE);
796

797 798
        if(pen->startcap == LineCapArrowAnchor)
            shorten_bezier_amt(ptcopy, pen->width, TRUE);
799
        else if((pen->startcap == LineCapCustom) && pen->customstart)
800
            shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
801

802 803 804
        /* the direction of the line cap is parallel to the direction at the
         * end of the bezier (which, if it has been shortened, is not the same
         * as the direction from pt[count-2] to pt[count-1]) */
805
        draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
806 807
            pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
            pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
808
            pt[count - 1].X, pt[count - 1].Y);
809

810
        draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
811 812
            pt[0].X - (ptcopy[0].X - ptcopy[1].X),
            pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
813
    }
814 815

    transform_and_round_points(graphics, pti, ptcopy, count);
816

817
    PolyBezier(graphics->hdc, pti, count);
818 819 820 821

    status = Ok;

end:
822
    GdipFree(pti);
823
    GdipFree(ptcopy);
824 825

    return status;
826 827
}

828
/* Draws a combination of bezier curves and lines between points. */
829
static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
830 831
    GDIPCONST BYTE * types, INT count, BOOL caps)
{
832
    POINT *pti = GdipAlloc(count * sizeof(POINT));
833
    BYTE *tp = GdipAlloc(count);
834 835
    GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
    INT i, j;
836 837 838 839 840 841
    GpStatus status = GenericError;

    if(!count){
        status = Ok;
        goto end;
    }
842
    if(!pti || !tp || !ptcopy){
843 844 845 846
        status = OutOfMemory;
        goto end;
    }

847
    for(i = 1; i < count; i++){
848 849 850 851 852 853 854 855 856 857
        if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
            if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
                || !(types[i + 1] & PathPointTypeBezier)){
                ERR("Bad bezier points\n");
                goto end;
            }
            i += 2;
        }
    }

858 859
    memcpy(ptcopy, pt, count * sizeof(GpPointF));

860 861 862 863 864 865
    /* If we are drawing caps, go through the points and adjust them accordingly,
     * and draw the caps. */
    if(caps){
        switch(types[count - 1] & PathPointTypePathTypeMask){
            case PathPointTypeBezier:
                if(pen->endcap == LineCapArrowAnchor)
866
                    shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
867 868 869
                else if((pen->endcap == LineCapCustom) && pen->customend)
                    shorten_bezier_amt(&ptcopy[count - 4],
                                       pen->width * pen->customend->inset, FALSE);
870

871
                draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
872 873
                    pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
                    pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
874 875 876 877 878
                    pt[count - 1].X, pt[count - 1].Y);

                break;
            case PathPointTypeLine:
                if(pen->endcap == LineCapArrowAnchor)
879 880
                    shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
                                     &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
881
                                     pen->width);
882
                else if((pen->endcap == LineCapCustom) && pen->customend)
883 884
                    shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
                                     &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
885
                                     pen->customend->inset * pen->width);
886

887
                draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
888 889
                         pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
                         pt[count - 1].Y);
890 891 892 893 894

                break;
            default:
                ERR("Bad path last point\n");
                goto end;
895
        }
896 897 898 899 900 901 902 903 904

        /* Find start of points */
        for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
            == PathPointTypeStart); j++);

        switch(types[j] & PathPointTypePathTypeMask){
            case PathPointTypeBezier:
                if(pen->startcap == LineCapArrowAnchor)
                    shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
905 906
                else if((pen->startcap == LineCapCustom) && pen->customstart)
                    shorten_bezier_amt(&ptcopy[j - 1],
907
                                       pen->width * pen->customstart->inset, TRUE);
908

909
                draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
                    pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
                    pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
                    pt[j - 1].X, pt[j - 1].Y);

                break;
            case PathPointTypeLine:
                if(pen->startcap == LineCapArrowAnchor)
                    shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
                                     &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
                                     pen->width);
                else if((pen->startcap == LineCapCustom) && pen->customstart)
                    shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
                                     &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
                                     pen->customstart->inset * pen->width);

Evan Stade's avatar
Evan Stade committed
925
                draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
926 927 928 929 930 931 932 933
                         pt[j].X, pt[j].Y, pt[j - 1].X,
                         pt[j - 1].Y);

                break;
            default:
                ERR("Bad path points\n");
                goto end;
        }
934
    }
935 936

    transform_and_round_points(graphics, pti, ptcopy, count);
937 938 939 940 941

    for(i = 0; i < count; i++){
        tp[i] = convert_path_point_type(types[i]);
    }

942
    PolyDraw(graphics->hdc, pti, tp, count);
943 944 945 946 947

    status = Ok;

end:
    GdipFree(pti);
948
    GdipFree(ptcopy);
949 950 951 952 953
    GdipFree(tp);

    return status;
}

954 955 956 957 958 959 960 961 962 963 964
GpStatus trace_path(GpGraphics *graphics, GpPath *path)
{
    GpStatus result;

    BeginPath(graphics->hdc);
    result = draw_poly(graphics, NULL, path->pathdata.Points,
                       path->pathdata.Types, path->pathdata.Count, FALSE);
    EndPath(graphics->hdc);
    return result;
}

965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
typedef struct _GraphicsContainerItem {
    struct list entry;
    GraphicsContainer contid;

    SmoothingMode smoothing;
    CompositingQuality compqual;
    InterpolationMode interpolation;
    CompositingMode compmode;
    TextRenderingHint texthint;
    REAL scale;
    GpUnit unit;
    PixelOffsetMode pixeloffset;
    UINT textcontrast;
    GpMatrix* worldtrans;
    GpRegion* clip;
} GraphicsContainerItem;

static GpStatus init_container(GraphicsContainerItem** container,
        GDIPCONST GpGraphics* graphics){
    GpStatus sts;

    *container = GdipAlloc(sizeof(GraphicsContainerItem));
    if(!(*container))
        return OutOfMemory;

    (*container)->contid = graphics->contid + 1;

    (*container)->smoothing = graphics->smoothing;
    (*container)->compqual = graphics->compqual;
    (*container)->interpolation = graphics->interpolation;
    (*container)->compmode = graphics->compmode;
    (*container)->texthint = graphics->texthint;
    (*container)->scale = graphics->scale;
    (*container)->unit = graphics->unit;
    (*container)->textcontrast = graphics->textcontrast;
    (*container)->pixeloffset = graphics->pixeloffset;

    sts = GdipCloneMatrix(graphics->worldtrans, &(*container)->worldtrans);
    if(sts != Ok){
        GdipFree(*container);
        *container = NULL;
        return sts;
    }

    sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
    if(sts != Ok){
        GdipDeleteMatrix((*container)->worldtrans);
        GdipFree(*container);
        *container = NULL;
        return sts;
    }

    return Ok;
}

static void delete_container(GraphicsContainerItem* container){
    GdipDeleteMatrix(container->worldtrans);
    GdipDeleteRegion(container->clip);
    GdipFree(container);
}

static GpStatus restore_container(GpGraphics* graphics,
        GDIPCONST GraphicsContainerItem* container){
    GpStatus sts;
    GpMatrix *newTrans;
    GpRegion *newClip;

    sts = GdipCloneMatrix(container->worldtrans, &newTrans);
    if(sts != Ok)
        return sts;

    sts = GdipCloneRegion(container->clip, &newClip);
    if(sts != Ok){
        GdipDeleteMatrix(newTrans);
        return sts;
    }

    GdipDeleteMatrix(graphics->worldtrans);
    graphics->worldtrans = newTrans;

    GdipDeleteRegion(graphics->clip);
    graphics->clip = newClip;

    graphics->contid = container->contid - 1;

    graphics->smoothing = container->smoothing;
    graphics->compqual = container->compqual;
    graphics->interpolation = container->interpolation;
    graphics->compmode = container->compmode;
    graphics->texthint = container->texthint;
    graphics->scale = container->scale;
    graphics->unit = container->unit;
    graphics->textcontrast = container->textcontrast;
    graphics->pixeloffset = container->pixeloffset;

    return Ok;
}

1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
{
    RECT wnd_rect;

    if(graphics->hwnd) {
        if(!GetClientRect(graphics->hwnd, &wnd_rect))
            return GenericError;

        rect->X = wnd_rect.left;
        rect->Y = wnd_rect.top;
        rect->Width = wnd_rect.right - wnd_rect.left;
        rect->Height = wnd_rect.bottom - wnd_rect.top;
    }else{
        rect->X = 0;
        rect->Y = 0;
        rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
        rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
    }

    return Ok;
}

1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
/* on success, rgn will contain the region of the graphics object which
 * is visible after clipping has been applied */
static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
{
    GpStatus stat;
    GpRectF rectf;
    GpRegion* tmp;

    if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
        return stat;

    if((stat = GdipCreateRegion(&tmp)) != Ok)
        return stat;

    if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
        goto end;

    if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
        goto end;

    stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);

end:
    GdipDeleteRegion(tmp);
    return stat;
}

1112
GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
1113
{
1114 1115
    TRACE("(%p, %p)\n", hdc, graphics);

1116 1117 1118 1119
    return GdipCreateFromHDC2(hdc, NULL, graphics);
}

GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
1120
{
1121 1122
    GpStatus retval;

1123 1124
    TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);

1125
    if(hDevice != NULL) {
1126
        FIXME("Don't know how to handle parameter hDevice\n");
1127 1128 1129
        return NotImplemented;
    }

1130 1131 1132 1133 1134 1135
    if(hdc == NULL)
        return OutOfMemory;

    if(graphics == NULL)
        return InvalidParameter;

1136 1137 1138
    *graphics = GdipAlloc(sizeof(GpGraphics));
    if(!*graphics)  return OutOfMemory;

1139 1140 1141 1142 1143
    if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
        GdipFree(*graphics);
        return retval;
    }

1144 1145 1146 1147 1148 1149
    if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
        GdipFree((*graphics)->worldtrans);
        GdipFree(*graphics);
        return retval;
    }

1150
    (*graphics)->hdc = hdc;
1151
    (*graphics)->hwnd = WindowFromDC(hdc);
1152
    (*graphics)->owndc = FALSE;
1153
    (*graphics)->smoothing = SmoothingModeDefault;
1154
    (*graphics)->compqual = CompositingQualityDefault;
1155
    (*graphics)->interpolation = InterpolationModeDefault;
1156
    (*graphics)->pixeloffset = PixelOffsetModeDefault;
1157
    (*graphics)->compmode = CompositingModeSourceOver;
1158
    (*graphics)->unit = UnitDisplay;
1159
    (*graphics)->scale = 1.0;
1160
    (*graphics)->busy = FALSE;
1161
    (*graphics)->textcontrast = 4;
1162 1163
    list_init(&(*graphics)->containers);
    (*graphics)->contid = 0;
1164

1165 1166
    TRACE("<-- %p\n", *graphics);

1167 1168 1169 1170 1171 1172
    return Ok;
}

GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
{
    GpStatus ret;
1173
    HDC hdc;
1174

1175 1176
    TRACE("(%p, %p)\n", hwnd, graphics);

1177 1178 1179 1180 1181
    hdc = GetDC(hwnd);

    if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
    {
        ReleaseDC(hwnd, hdc);
1182
        return ret;
1183
    }
1184 1185

    (*graphics)->hwnd = hwnd;
1186
    (*graphics)->owndc = TRUE;
1187 1188 1189 1190

    return Ok;
}

1191 1192 1193
/* FIXME: no icm handling */
GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
{
1194 1195
    TRACE("(%p, %p)\n", hwnd, graphics);

1196 1197 1198
    return GdipCreateFromHWND(hwnd, graphics);
}

1199 1200 1201 1202 1203
GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
    GpMetafile **metafile)
{
    static int calls;

1204 1205
    TRACE("(%p,%i,%p)\n", hemf, delete, metafile);

1206 1207 1208 1209 1210 1211 1212 1213 1214
    if(!hemf || !metafile)
        return InvalidParameter;

    if(!(calls++))
        FIXME("not implemented\n");

    return NotImplemented;
}

1215 1216 1217
GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
    GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
{
1218
    IStream *stream = NULL;
1219 1220 1221
    UINT read;
    BYTE* copy;
    HENHMETAFILE hemf;
1222
    GpStatus retval = Ok;
1223

1224 1225
    TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);

1226 1227
    if(!hwmf || !metafile || !placeable)
        return InvalidParameter;
1228 1229

    *metafile = NULL;
1230 1231 1232 1233 1234 1235
    read = GetMetaFileBitsEx(hwmf, 0, NULL);
    if(!read)
        return GenericError;
    copy = GdipAlloc(read);
    GetMetaFileBitsEx(hwmf, read, copy);

1236
    hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
1237 1238 1239 1240 1241 1242 1243 1244 1245
    GdipFree(copy);

    read = GetEnhMetaFileBits(hemf, 0, NULL);
    copy = GdipAlloc(read);
    GetEnhMetaFileBits(hemf, read, copy);
    DeleteEnhMetaFile(hemf);

    if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
        ERR("could not make stream\n");
1246
        GdipFree(copy);
1247
        retval = GenericError;
1248
        goto err;
1249 1250 1251
    }

    *metafile = GdipAlloc(sizeof(GpMetafile));
1252 1253
    if(!*metafile){
        retval = OutOfMemory;
1254
        goto err;
1255
    }
1256 1257

    if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1258
        (LPVOID*) &((*metafile)->image.picture)) != S_OK)
1259 1260
    {
        retval = GenericError;
1261
        goto err;
1262
    }
1263

1264 1265

    (*metafile)->image.type = ImageTypeMetafile;
1266
    memcpy(&(*metafile)->image.format, &ImageFormatWMF, sizeof(GUID));
1267 1268 1269 1270
    (*metafile)->image.palette_flags = 0;
    (*metafile)->image.palette_count = 0;
    (*metafile)->image.palette_size = 0;
    (*metafile)->image.palette_entries = NULL;
1271 1272
    (*metafile)->image.xres = (REAL)placeable->Inch;
    (*metafile)->image.yres = (REAL)placeable->Inch;
1273
    (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
1274
    (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Top) / ((REAL) placeable->Inch);
1275 1276 1277 1278 1279 1280
    (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
                    - placeable->BoundingBox.Left)) / ((REAL) placeable->Inch);
    (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
                   - placeable->BoundingBox.Top)) / ((REAL) placeable->Inch);
    (*metafile)->unit = UnitInch;

1281 1282 1283
    if(delete)
        DeleteMetaFile(hwmf);

1284 1285
    TRACE("<-- %p\n", *metafile);

1286
err:
1287 1288
    if (retval != Ok)
        GdipFree(*metafile);
1289 1290
    IStream_Release(stream);
    return retval;
1291 1292
}

1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
    GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
{
    HMETAFILE hmf = GetMetaFileW(file);

    TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);

    if(!hmf) return InvalidParameter;

    return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
}

1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
    GpMetafile **metafile)
{
    FIXME("(%p, %p): stub\n", file, metafile);
    return NotImplemented;
}

GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
    GpMetafile **metafile)
{
    FIXME("(%p, %p): stub\n", stream, metafile);
    return NotImplemented;
}

1319 1320 1321 1322 1323 1324
GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
    UINT access, IStream **stream)
{
    DWORD dwMode;
    HRESULT ret;

1325 1326
    TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);

1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
    if(!stream || !filename)
        return InvalidParameter;

    if(access & GENERIC_WRITE)
        dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
    else if(access & GENERIC_READ)
        dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
    else
        return InvalidParameter;

    ret = SHCreateStreamOnFileW(filename, dwMode, stream);

    return hresult_to_status(ret);
}

1342 1343
GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
{
1344
    GraphicsContainerItem *cont, *next;
1345 1346
    TRACE("(%p)\n", graphics);

1347
    if(!graphics) return InvalidParameter;
1348 1349
    if(graphics->busy) return ObjectBusy;

1350
    if(graphics->owndc)
1351 1352
        ReleaseDC(graphics->hwnd, graphics->hdc);

1353 1354 1355 1356 1357
    LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
        list_remove(&cont->entry);
        delete_container(cont);
    }

1358
    GdipDeleteRegion(graphics->clip);
1359
    GdipDeleteMatrix(graphics->worldtrans);
1360
    GdipFree(graphics);
1361 1362 1363

    return Ok;
}
1364

1365 1366 1367
GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
    REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
{
1368 1369
    INT save_state, num_pts;
    GpPointF points[MAX_ARC_PTS];
1370
    GpStatus retval;
1371

1372 1373 1374
    TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
          width, height, startAngle, sweepAngle);

1375
    if(!graphics || !pen || width <= 0 || height <= 0)
1376 1377
        return InvalidParameter;

1378 1379 1380
    if(graphics->busy)
        return ObjectBusy;

1381
    num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
1382

1383
    save_state = prepare_dc(graphics, pen);
1384

1385
    retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
1386

1387
    restore_dc(graphics, save_state);
1388

1389
    return retval;
1390 1391
}

1392 1393 1394
GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
    INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
{
1395 1396 1397
    TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
          width, height, startAngle, sweepAngle);

1398
    return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1399 1400
}

1401 1402 1403
GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
    REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
{
1404 1405
    INT save_state;
    GpPointF pt[4];
1406
    GpStatus retval;
1407

1408 1409 1410
    TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
          x2, y2, x3, y3, x4, y4);

1411 1412 1413
    if(!graphics || !pen)
        return InvalidParameter;

1414 1415 1416
    if(graphics->busy)
        return ObjectBusy;

1417 1418 1419 1420 1421 1422 1423 1424
    pt[0].X = x1;
    pt[0].Y = y1;
    pt[1].X = x2;
    pt[1].Y = y2;
    pt[2].X = x3;
    pt[2].Y = y3;
    pt[3].X = x4;
    pt[3].Y = y4;
1425

1426
    save_state = prepare_dc(graphics, pen);
1427

1428
    retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441

    restore_dc(graphics, save_state);

    return retval;
}

GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
    INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
{
    INT save_state;
    GpPointF pt[4];
    GpStatus retval;

1442 1443 1444
    TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
          x2, y2, x3, y3, x4, y4);

1445 1446 1447
    if(!graphics || !pen)
        return InvalidParameter;

1448 1449 1450
    if(graphics->busy)
        return ObjectBusy;

1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
    pt[0].X = x1;
    pt[0].Y = y1;
    pt[1].X = x2;
    pt[1].Y = y2;
    pt[2].X = x3;
    pt[2].Y = y3;
    pt[3].X = x4;
    pt[3].Y = y4;

    save_state = prepare_dc(graphics, pen);

    retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1463

1464
    restore_dc(graphics, save_state);
1465

1466
    return retval;
1467 1468
}

1469 1470 1471 1472 1473 1474
GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
    GDIPCONST GpPointF *points, INT count)
{
    INT i;
    GpStatus ret;

1475 1476
    TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);

1477 1478 1479
    if(!graphics || !pen || !points || (count <= 0))
        return InvalidParameter;

1480 1481 1482
    if(graphics->busy)
        return ObjectBusy;

1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
    for(i = 0; i < floor(count / 4); i++){
        ret = GdipDrawBezier(graphics, pen,
                             points[4*i].X, points[4*i].Y,
                             points[4*i + 1].X, points[4*i + 1].Y,
                             points[4*i + 2].X, points[4*i + 2].Y,
                             points[4*i + 3].X, points[4*i + 3].Y);
        if(ret != Ok)
            return ret;
    }

    return Ok;
}

GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
    GDIPCONST GpPoint *points, INT count)
{
    GpPointF *pts;
    GpStatus ret;
    INT i;

1503 1504
    TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);

1505 1506 1507
    if(!graphics || !pen || !points || (count <= 0))
        return InvalidParameter;

1508 1509 1510
    if(graphics->busy)
        return ObjectBusy;

1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526
    pts = GdipAlloc(sizeof(GpPointF) * count);
    if(!pts)
        return OutOfMemory;

    for(i = 0; i < count; i++){
        pts[i].X = (REAL)points[i].X;
        pts[i].Y = (REAL)points[i].Y;
    }

    ret = GdipDrawBeziers(graphics,pen,pts,count);

    GdipFree(pts);

    return ret;
}

1527 1528 1529
GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
    GDIPCONST GpPointF *points, INT count)
{
1530 1531
    TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);

1532 1533 1534 1535 1536 1537
    return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
}

GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
    GDIPCONST GpPoint *points, INT count)
{
1538 1539
    TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);

1540 1541 1542
    return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
}

1543 1544 1545
GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
    GDIPCONST GpPointF *points, INT count, REAL tension)
{
1546
    GpPath *path;
1547 1548
    GpStatus stat;

1549 1550
    TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);

1551 1552 1553
    if(!graphics || !pen || !points || count <= 0)
        return InvalidParameter;

1554 1555 1556
    if(graphics->busy)
        return ObjectBusy;

1557 1558
    if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
        return stat;
1559

1560 1561 1562 1563 1564
    stat = GdipAddPathClosedCurve2(path, points, count, tension);
    if(stat != Ok){
        GdipDeletePath(path);
        return stat;
    }
1565

1566
    stat = GdipDrawPath(graphics, pen, path);
1567

1568
    GdipDeletePath(path);
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579

    return stat;
}

GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
    GDIPCONST GpPoint *points, INT count, REAL tension)
{
    GpPointF *ptf;
    GpStatus stat;
    INT i;

1580 1581
    TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);

1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
    if(!points || count <= 0)
        return InvalidParameter;

    ptf = GdipAlloc(sizeof(GpPointF)*count);
    if(!ptf)
        return OutOfMemory;

    for(i = 0; i < count; i++){
        ptf[i].X = (REAL)points[i].X;
        ptf[i].Y = (REAL)points[i].Y;
    }

    stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);

    GdipFree(ptf);

    return stat;
}

1601 1602 1603
GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
    GDIPCONST GpPointF *points, INT count)
{
1604 1605
    TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);

1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
    return GdipDrawCurve2(graphics,pen,points,count,1.0);
}

GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
    GDIPCONST GpPoint *points, INT count)
{
    GpPointF *pointsF;
    GpStatus ret;
    INT i;

1616 1617
    TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);

1618
    if(!points)
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
        return InvalidParameter;

    pointsF = GdipAlloc(sizeof(GpPointF)*count);
    if(!pointsF)
        return OutOfMemory;

    for(i = 0; i < count; i++){
        pointsF[i].X = (REAL)points[i].X;
        pointsF[i].Y = (REAL)points[i].Y;
    }

    ret = GdipDrawCurve(graphics,pen,pointsF,count);
    GdipFree(pointsF);

    return ret;
}

1636 1637 1638 1639 1640
/* Approximates cardinal spline with Bezier curves. */
GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
    GDIPCONST GpPointF *points, INT count, REAL tension)
{
    /* PolyBezier expects count*3-2 points. */
1641 1642
    INT i, len_pt = count*3-2, save_state;
    GpPointF *pt;
1643
    REAL x1, x2, y1, y2;
1644
    GpStatus retval;
1645

1646 1647
    TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);

1648 1649 1650
    if(!graphics || !pen)
        return InvalidParameter;

1651 1652 1653
    if(graphics->busy)
        return ObjectBusy;

1654 1655 1656
    if(count < 2)
        return InvalidParameter;

1657
    pt = GdipAlloc(len_pt * sizeof(GpPointF));
1658 1659 1660
    if(!pt)
        return OutOfMemory;

1661 1662 1663 1664 1665
    tension = tension * TENSION_CONST;

    calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
        tension, &x1, &y1);

1666 1667 1668 1669
    pt[0].X = points[0].X;
    pt[0].Y = points[0].Y;
    pt[1].X = x1;
    pt[1].Y = y1;
1670 1671 1672 1673

    for(i = 0; i < count-2; i++){
        calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);

1674 1675 1676 1677 1678 1679
        pt[3*i+2].X = x1;
        pt[3*i+2].Y = y1;
        pt[3*i+3].X = points[i+1].X;
        pt[3*i+3].Y = points[i+1].Y;
        pt[3*i+4].X = x2;
        pt[3*i+4].Y = y2;
1680 1681 1682 1683 1684
    }

    calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
        points[count-2].X, points[count-2].Y, tension, &x1, &y1);

1685 1686 1687 1688
    pt[len_pt-2].X = x1;
    pt[len_pt-2].Y = y1;
    pt[len_pt-1].X = points[count-1].X;
    pt[len_pt-1].Y = points[count-1].Y;
1689

1690
    save_state = prepare_dc(graphics, pen);
1691

1692
    retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
1693

1694
    GdipFree(pt);
1695
    restore_dc(graphics, save_state);
1696

1697
    return retval;
1698 1699
}

1700 1701 1702 1703 1704 1705 1706
GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
    GDIPCONST GpPoint *points, INT count, REAL tension)
{
    GpPointF *pointsF;
    GpStatus ret;
    INT i;

1707 1708
    TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);

1709
    if(!points)
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
        return InvalidParameter;

    pointsF = GdipAlloc(sizeof(GpPointF)*count);
    if(!pointsF)
        return OutOfMemory;

    for(i = 0; i < count; i++){
        pointsF[i].X = (REAL)points[i].X;
        pointsF[i].Y = (REAL)points[i].Y;
    }

    ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
    GdipFree(pointsF);

    return ret;
}

1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
    GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
    REAL tension)
{
    TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);

    if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
        return InvalidParameter;
    }

    return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
}

GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
    GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
    REAL tension)
{
    TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);

    if(count < 0){
        return OutOfMemory;
    }

    if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
        return InvalidParameter;
    }

    return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
}

1757 1758 1759 1760 1761 1762 1763
GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
    REAL y, REAL width, REAL height)
{
    INT save_state;
    GpPointF ptf[2];
    POINT pti[2];

1764 1765
    TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);

1766 1767 1768
    if(!graphics || !pen)
        return InvalidParameter;

1769 1770 1771
    if(graphics->busy)
        return ObjectBusy;

1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
    ptf[0].X = x;
    ptf[0].Y = y;
    ptf[1].X = x + width;
    ptf[1].Y = y + height;

    save_state = prepare_dc(graphics, pen);
    SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));

    transform_and_round_points(graphics, pti, ptf, 2);

    Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);

    restore_dc(graphics, save_state);

    return Ok;
}

GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
    INT y, INT width, INT height)
{
1792 1793
    TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);

1794 1795 1796 1797
    return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
}


1798 1799
GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
{
1800 1801
    UINT width, height;
    GpPointF points[3];
1802

1803
    TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
1804

1805 1806 1807 1808 1809 1810
    if(!graphics || !image)
        return InvalidParameter;

    GdipGetImageWidth(image, &width);
    GdipGetImageHeight(image, &height);

1811
    /* FIXME: we should use the graphics and image dpi, somehow */
1812

1813 1814 1815 1816
    points[0].X = points[2].X = x;
    points[0].Y = points[1].Y = y;
    points[1].X = x + width;
    points[2].Y = y + height;
1817

1818 1819 1820
    return GdipDrawImagePointsRect(graphics, image, points, 3, 0, 0, width, height,
        UnitPixel, NULL, NULL, NULL);
}
1821

1822 1823 1824 1825 1826 1827
GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
    INT y)
{
    TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);

    return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
1828 1829
}

1830 1831 1832 1833
GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
    REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
    GpUnit srcUnit)
{
1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
    GpPointF points[3];
    TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);

    points[0].X = points[2].X = x;
    points[0].Y = points[1].Y = y;

    /* FIXME: convert image coordinates to Graphics coordinates? */
    points[1].X = x + srcwidth;
    points[2].Y = y + srcheight;

    return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
        srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
1846 1847 1848 1849 1850 1851
}

GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
    INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
    GpUnit srcUnit)
{
1852
    return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
1853 1854
}

1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
    GDIPCONST GpPointF *dstpoints, INT count)
{
    FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
    return NotImplemented;
}

GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
    GDIPCONST GpPoint *dstpoints, INT count)
{
    FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
    return NotImplemented;
}

1869
/* FIXME: partially implemented (only works for rectangular parallelograms) */
1870
GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
1871 1872 1873
     GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
     REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
     DrawImageAbort callback, VOID * callbackData)
1874
{
1875 1876
    GpPointF ptf[3];
    POINT pti[3];
1877
    REAL dx, dy;
1878

1879 1880
    TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
          count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
1881
          callbackData);
1882

1883
    if(!graphics || !image || !points || count != 3)
1884
         return InvalidParameter;
1885

1886 1887 1888
    TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
        debugstr_pointf(&points[2]));

1889 1890 1891
    memcpy(ptf, points, 3 * sizeof(GpPointF));
    transform_and_round_points(graphics, pti, ptf, 3);

1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913
    if (image->picture)
    {
        if(srcUnit == UnitInch)
            dx = dy = (REAL) INCH_HIMETRIC;
        else if(srcUnit == UnitPixel){
            dx = ((REAL) INCH_HIMETRIC) /
                 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
            dy = ((REAL) INCH_HIMETRIC) /
                 ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
        }
        else
            return NotImplemented;

        if(IPicture_Render(image->picture, graphics->hdc,
            pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
            srcx * dx, srcy * dy,
            srcwidth * dx, srcheight * dy,
            NULL) != S_OK){
            if(callback)
                callback(callbackData);
            return GenericError;
        }
1914
    }
1915 1916 1917 1918
    else if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
    {
        HDC hdc;
        GpBitmap* bitmap = (GpBitmap*)image;
1919 1920
        int temp_hdc=0, temp_bitmap=0;
        HBITMAP hbitmap, old_hbm=NULL;
1921 1922 1923 1924 1925 1926 1927 1928

        if (srcUnit == UnitInch)
            dx = dy = 96.0; /* FIXME: use the image resolution */
        else if (srcUnit == UnitPixel)
            dx = dy = 1.0;
        else
            return NotImplemented;

1929
        if (bitmap->format == PixelFormat32bppARGB)
1930
        {
1931 1932 1933 1934
            BITMAPINFOHEADER bih;
            BYTE *temp_bits;

            /* we need a bitmap with premultiplied alpha */
1935
            hdc = CreateCompatibleDC(0);
1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
            temp_hdc = 1;
            temp_bitmap = 1;

            bih.biSize = sizeof(BITMAPINFOHEADER);
            bih.biWidth = bitmap->width;
            bih.biHeight = -bitmap->height;
            bih.biPlanes = 1;
            bih.biBitCount = 32;
            bih.biCompression = BI_RGB;
            bih.biSizeImage = 0;
            bih.biXPelsPerMeter = 0;
            bih.biYPelsPerMeter = 0;
            bih.biClrUsed = 0;
            bih.biClrImportant = 0;

            hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
                (void**)&temp_bits, NULL, 0);

            convert_32bppARGB_to_32bppPARGB(bitmap->width, bitmap->height,
                temp_bits, bitmap->width*4, bitmap->bits, bitmap->stride);
        }
        else
        {
            hbitmap = bitmap->hbitmap;
            hdc = bitmap->hdc;
            temp_hdc = (hdc == 0);
        }

        if (temp_hdc)
        {
            if (!hdc) hdc = CreateCompatibleDC(0);
            old_hbm = SelectObject(hdc, hbitmap);
1968
        }
1969

1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
        if (bitmap->format == PixelFormat32bppARGB || bitmap->format == PixelFormat32bppPARGB)
        {
            BLENDFUNCTION bf;

            bf.BlendOp = AC_SRC_OVER;
            bf.BlendFlags = 0;
            bf.SourceConstantAlpha = 255;
            bf.AlphaFormat = AC_SRC_ALPHA;

            GdiAlphaBlend(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
                hdc, srcx*dx, srcy*dy, srcwidth*dx, srcheight*dy, bf);
        }
        else
        {
            StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
                hdc, srcx*dx, srcy*dy, srcwidth*dx, srcheight*dy, SRCCOPY);
        }
1987

1988
        if (temp_hdc)
1989 1990 1991 1992
        {
            SelectObject(hdc, old_hbm);
            DeleteDC(hdc);
        }
1993 1994 1995

        if (temp_bitmap)
            DeleteObject(hbitmap);
1996 1997 1998 1999 2000
    }
    else
    {
        ERR("GpImage with no IPicture or HBITMAP?!\n");
        return NotImplemented;
2001 2002 2003
    }

    return Ok;
2004 2005
}

2006 2007 2008 2009 2010 2011 2012 2013
GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
     GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
     INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
     DrawImageAbort callback, VOID * callbackData)
{
    GpPointF pointsF[3];
    INT i;

2014 2015 2016 2017
    TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
          srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
          callbackData);

2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
    if(!points || count!=3)
        return InvalidParameter;

    for(i = 0; i < count; i++){
        pointsF[i].X = (REAL)points[i].X;
        pointsF[i].Y = (REAL)points[i].Y;
    }

    return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
                                   (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
                                   callback, callbackData);
}

2031 2032 2033 2034 2035 2036 2037 2038
GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
    REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
    REAL srcwidth, REAL srcheight, GpUnit srcUnit,
    GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
    VOID * callbackData)
{
    GpPointF points[3];

2039
    TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
2040 2041 2042
          graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
          srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);

2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053
    points[0].X = dstx;
    points[0].Y = dsty;
    points[1].X = dstx + dstwidth;
    points[1].Y = dsty;
    points[2].X = dstx;
    points[2].Y = dsty + dstheight;

    return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
               srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
}

2054 2055 2056 2057 2058 2059
GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
	INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
	INT srcwidth, INT srcheight, GpUnit srcUnit,
	GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
	VOID * callbackData)
{
2060 2061
    GpPointF points[3];

2062
    TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
2063 2064
          graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
          srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076

    points[0].X = dstx;
    points[0].Y = dsty;
    points[1].X = dstx + dstwidth;
    points[1].Y = dsty;
    points[2].X = dstx;
    points[2].Y = dsty + dstheight;

    return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
               srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
}

2077 2078 2079 2080 2081 2082 2083
GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
    REAL x, REAL y, REAL width, REAL height)
{
    RectF bounds;
    GpUnit unit;
    GpStatus ret;

2084 2085
    TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);

2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
    if(!graphics || !image)
        return InvalidParameter;

    ret = GdipGetImageBounds(image, &bounds, &unit);
    if(ret != Ok)
        return ret;

    return GdipDrawImageRectRect(graphics, image, x, y, width, height,
                                 bounds.X, bounds.Y, bounds.Width, bounds.Height,
                                 unit, NULL, NULL, NULL);
}

GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
    INT x, INT y, INT width, INT height)
{
2101 2102
    TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);

2103 2104 2105
    return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
}

2106 2107 2108 2109 2110 2111 2112
GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
    REAL y1, REAL x2, REAL y2)
{
    INT save_state;
    GpPointF pt[2];
    GpStatus retval;

2113 2114
    TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);

2115 2116 2117
    if(!pen || !graphics)
        return InvalidParameter;

2118 2119 2120
    if(graphics->busy)
        return ObjectBusy;

2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134
    pt[0].X = x1;
    pt[0].Y = y1;
    pt[1].X = x2;
    pt[1].Y = y2;

    save_state = prepare_dc(graphics, pen);

    retval = draw_polyline(graphics, pen, pt, 2, TRUE);

    restore_dc(graphics, save_state);

    return retval;
}

2135 2136 2137
GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
    INT y1, INT x2, INT y2)
{
2138
    INT save_state;
2139
    GpPointF pt[2];
2140
    GpStatus retval;
2141

2142 2143
    TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);

2144 2145 2146
    if(!pen || !graphics)
        return InvalidParameter;

2147 2148 2149
    if(graphics->busy)
        return ObjectBusy;

2150 2151 2152 2153 2154
    pt[0].X = (REAL)x1;
    pt[0].Y = (REAL)y1;
    pt[1].X = (REAL)x2;
    pt[1].Y = (REAL)y2;

2155
    save_state = prepare_dc(graphics, pen);
2156

2157
    retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2158

2159
    restore_dc(graphics, save_state);
2160

2161
    return retval;
2162
}
2163

2164 2165 2166
GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
    GpPointF *points, INT count)
{
2167
    INT save_state;
2168
    GpStatus retval;
2169

2170 2171
    TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);

2172 2173 2174
    if(!pen || !graphics || (count < 2))
        return InvalidParameter;

2175 2176 2177
    if(graphics->busy)
        return ObjectBusy;

2178
    save_state = prepare_dc(graphics, pen);
2179

2180
    retval = draw_polyline(graphics, pen, points, count, TRUE);
2181

2182
    restore_dc(graphics, save_state);
2183

2184
    return retval;
2185 2186
}

2187 2188 2189 2190 2191 2192 2193 2194
GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
    GpPoint *points, INT count)
{
    INT save_state;
    GpStatus retval;
    GpPointF *ptf = NULL;
    int i;

2195 2196
    TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);

2197 2198 2199
    if(!pen || !graphics || (count < 2))
        return InvalidParameter;

2200 2201 2202
    if(graphics->busy)
        return ObjectBusy;

2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220
    ptf = GdipAlloc(count * sizeof(GpPointF));
    if(!ptf) return OutOfMemory;

    for(i = 0; i < count; i ++){
        ptf[i].X = (REAL) points[i].X;
        ptf[i].Y = (REAL) points[i].Y;
    }

    save_state = prepare_dc(graphics, pen);

    retval = draw_polyline(graphics, pen, ptf, count, TRUE);

    restore_dc(graphics, save_state);

    GdipFree(ptf);
    return retval;
}

2221 2222
GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
{
2223
    INT save_state;
2224 2225
    GpStatus retval;

2226 2227
    TRACE("(%p, %p, %p)\n", graphics, pen, path);

2228 2229 2230
    if(!pen || !graphics)
        return InvalidParameter;

2231 2232 2233
    if(graphics->busy)
        return ObjectBusy;

2234
    save_state = prepare_dc(graphics, pen);
2235

2236
    retval = draw_poly(graphics, pen, path->pathdata.Points,
2237
                       path->pathdata.Types, path->pathdata.Count, TRUE);
2238

2239
    restore_dc(graphics, save_state);
2240 2241 2242 2243

    return retval;
}

2244 2245 2246
GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
    REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
{
2247 2248
    INT save_state;

2249 2250 2251
    TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
            width, height, startAngle, sweepAngle);

2252
    if(!graphics || !pen)
2253 2254
        return InvalidParameter;

2255 2256 2257
    if(graphics->busy)
        return ObjectBusy;

2258 2259 2260 2261 2262 2263 2264 2265
    save_state = prepare_dc(graphics, pen);
    SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));

    draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);

    restore_dc(graphics, save_state);

    return Ok;
2266 2267
}

2268 2269 2270
GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
    INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
{
2271 2272 2273
    TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
            width, height, startAngle, sweepAngle);

2274 2275 2276
    return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
}

2277 2278
GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
    REAL y, REAL width, REAL height)
2279
{
2280
    INT save_state;
2281 2282
    GpPointF ptf[4];
    POINT pti[4];
2283

2284 2285
    TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);

2286 2287 2288
    if(!pen || !graphics)
        return InvalidParameter;

2289 2290 2291
    if(graphics->busy)
        return ObjectBusy;

2292 2293 2294 2295 2296 2297 2298 2299 2300
    ptf[0].X = x;
    ptf[0].Y = y;
    ptf[1].X = x + width;
    ptf[1].Y = y;
    ptf[2].X = x + width;
    ptf[2].Y = y + height;
    ptf[3].X = x;
    ptf[3].Y = y + height;

2301
    save_state = prepare_dc(graphics, pen);
2302
    SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2303

2304 2305
    transform_and_round_points(graphics, pti, ptf, 4);
    Polygon(graphics->hdc, pti, 4);
2306

2307
    restore_dc(graphics, save_state);
2308 2309 2310

    return Ok;
}
2311

2312 2313 2314
GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
    INT y, INT width, INT height)
{
2315 2316
    TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);

2317 2318 2319
    return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
}

2320
GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
2321
    GDIPCONST GpRectF* rects, INT count)
2322 2323 2324 2325 2326
{
    GpPointF *ptf;
    POINT *pti;
    INT save_state, i;

2327 2328
    TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);

2329 2330 2331
    if(!graphics || !pen || !rects || count < 1)
        return InvalidParameter;

2332 2333 2334
    if(graphics->busy)
        return ObjectBusy;

2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366
    ptf = GdipAlloc(4 * count * sizeof(GpPointF));
    pti = GdipAlloc(4 * count * sizeof(POINT));

    if(!ptf || !pti){
        GdipFree(ptf);
        GdipFree(pti);
        return OutOfMemory;
    }

    for(i = 0; i < count; i++){
        ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
        ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
        ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
        ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
    }

    save_state = prepare_dc(graphics, pen);
    SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));

    transform_and_round_points(graphics, pti, ptf, 4 * count);

    for(i = 0; i < count; i++)
        Polygon(graphics->hdc, &pti[4 * i], 4);

    restore_dc(graphics, save_state);

    GdipFree(ptf);
    GdipFree(pti);

    return Ok;
}

2367 2368 2369 2370 2371 2372 2373
GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
    GDIPCONST GpRect* rects, INT count)
{
    GpRectF *rectsF;
    GpStatus ret;
    INT i;

2374 2375
    TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);

2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395
    if(!rects || count<=0)
        return InvalidParameter;

    rectsF = GdipAlloc(sizeof(GpRectF) * count);
    if(!rectsF)
        return OutOfMemory;

    for(i = 0;i < count;i++){
        rectsF[i].X      = (REAL)rects[i].X;
        rectsF[i].Y      = (REAL)rects[i].Y;
        rectsF[i].Width  = (REAL)rects[i].Width;
        rectsF[i].Height = (REAL)rects[i].Height;
    }

    ret = GdipDrawRectangles(graphics, pen, rectsF, count);
    GdipFree(rectsF);

    return ret;
}

2396 2397 2398 2399
GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
    INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
    GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
{
2400
    HRGN rgn = NULL;
2401 2402 2403
    HFONT gdifont;
    LOGFONTW lfw;
    TEXTMETRICW textmet;
2404
    GpPointF pt[3], rectcpy[4];
2405 2406 2407
    POINT corners[4];
    WCHAR* stringdup;
    REAL angle, ang_cos, ang_sin, rel_width, rel_height;
2408
    INT sum = 0, height = 0, offsety = 0, fit, fitcpy, save_state, i, j, lret, nwidth,
2409
        nheight, lineend;
2410
    SIZE size;
2411 2412
    POINT drawbase;
    UINT drawflags;
2413
    RECT drawcoord;
2414

2415 2416 2417
    TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
        length, font, debugstr_rectf(rect), format, brush);

2418 2419 2420
    if(!graphics || !string || !font || !brush || !rect)
        return InvalidParameter;

2421
    if((brush->bt != BrushTypeSolidColor)){
2422 2423 2424 2425
        FIXME("not implemented for given parameters\n");
        return NotImplemented;
    }

2426
    if(format){
2427 2428
        TRACE("may be ignoring some format flags: attr %x\n", format->attr);

2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441
        /* Should be no need to explicitly test for StringAlignmentNear as
         * that is default behavior if no alignment is passed. */
        if(format->vertalign != StringAlignmentNear){
            RectF bounds;
            GdipMeasureString(graphics, string, length, font, rect, format, &bounds, 0, 0);

            if(format->vertalign == StringAlignmentCenter)
                offsety = (rect->Height - bounds.Height) / 2;
            else if(format->vertalign == StringAlignmentFar)
                offsety = (rect->Height - bounds.Height);
        }
    }

2442 2443
    if(length == -1) length = lstrlenW(string);

2444 2445 2446 2447 2448 2449 2450
    stringdup = GdipAlloc(length * sizeof(WCHAR));
    if(!stringdup) return OutOfMemory;

    save_state = SaveDC(graphics->hdc);
    SetBkMode(graphics->hdc, TRANSPARENT);
    SetTextColor(graphics->hdc, brush->lb.lbColor);

2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465
    pt[0].X = 0.0;
    pt[0].Y = 0.0;
    pt[1].X = 1.0;
    pt[1].Y = 0.0;
    pt[2].X = 0.0;
    pt[2].Y = 1.0;
    GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
    angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
    ang_cos = cos(angle);
    ang_sin = sin(angle);
    rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
                     (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
    rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
                      (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));

2466
    rectcpy[3].X = rectcpy[0].X = rect->X;
2467
    rectcpy[1].Y = rectcpy[0].Y = rect->Y + offsety;
2468
    rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
2469
    rectcpy[3].Y = rectcpy[2].Y = rect->Y + offsety + rect->Height;
2470 2471
    transform_and_round_points(graphics, corners, rectcpy, 4);

2472 2473 2474 2475 2476 2477 2478 2479
    if (roundr(rect->Width) == 0)
        nwidth = INT_MAX;
    else
        nwidth = roundr(rel_width * rect->Width);

    if (roundr(rect->Height) == 0)
        nheight = INT_MAX;
    else
2480
        nheight = roundr(rel_height * rect->Height);
2481 2482 2483 2484

    if (roundr(rect->Width) != 0 && roundr(rect->Height) != 0)
    {
        /* FIXME: If only the width or only the height is 0, we should probably still clip */
2485 2486 2487
        rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
        SelectClipRgn(graphics->hdc, rgn);
    }
2488 2489 2490 2491 2492

    /* Use gdi to find the font, then perform transformations on it (height,
     * width, angle). */
    SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
    GetTextMetricsW(graphics->hdc, &textmet);
2493
    lfw = font->lfw;
2494 2495 2496 2497

    lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
    lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);

2498
    lfw.lfEscapement = lfw.lfOrientation = roundr((angle / M_PI) * 1800.0);
2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512

    gdifont = CreateFontIndirectW(&lfw);
    DeleteObject(SelectObject(graphics->hdc, CreateFontIndirectW(&lfw)));

    for(i = 0, j = 0; i < length; i++){
        if(!isprintW(string[i]) && (string[i] != '\n'))
            continue;

        stringdup[j] = string[i];
        j++;
    }

    length = j;

2513
    if (!format || format->align == StringAlignmentNear)
2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531
    {
        drawbase.x = corners[0].x;
        drawbase.y = corners[0].y;
        drawflags = DT_NOCLIP | DT_EXPANDTABS;
    }
    else if (format->align == StringAlignmentCenter)
    {
        drawbase.x = (corners[0].x + corners[1].x)/2;
        drawbase.y = (corners[0].y + corners[1].y)/2;
        drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_CENTER;
    }
    else /* (format->align == StringAlignmentFar) */
    {
        drawbase.x = corners[1].x;
        drawbase.y = corners[1].y;
        drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_RIGHT;
    }

2532
    while(sum < length){
2533 2534
        drawcoord.left = drawcoord.right = drawbase.x + roundr(ang_sin * (REAL) height);
        drawcoord.top = drawcoord.bottom = drawbase.y + roundr(ang_cos * (REAL) height);
2535

2536 2537
        GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
                              nwidth, &fit, NULL, &size);
2538 2539 2540
        fitcpy = fit;

        if(fit == 0){
2541
            DrawTextW(graphics->hdc, stringdup + sum, 1, &drawcoord, drawflags);
2542 2543 2544 2545 2546 2547 2548 2549 2550
            break;
        }

        for(lret = 0; lret < fit; lret++)
            if(*(stringdup + sum + lret) == '\n')
                break;

        /* Line break code (may look strange, but it imitates windows). */
        if(lret < fit)
2551
            lineend = fit = lret;    /* this is not an off-by-one error */
2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566
        else if(fit < (length - sum)){
            if(*(stringdup + sum + fit) == ' ')
                while(*(stringdup + sum + fit) == ' ')
                    fit++;
            else
                while(*(stringdup + sum + fit - 1) != ' '){
                    fit--;

                    if(*(stringdup + sum + fit) == '\t')
                        break;

                    if(fit == 0){
                        fit = fitcpy;
                        break;
                    }
2567
                }
2568 2569 2570 2571
            lineend = fit;
            while(*(stringdup + sum + lineend - 1) == ' ' ||
                  *(stringdup + sum + lineend - 1) == '\t')
                lineend--;
2572
        }
2573 2574 2575
        else
            lineend = fit;
        DrawTextW(graphics->hdc, stringdup + sum, min(length - sum, lineend),
2576
                  &drawcoord, drawflags);
2577 2578 2579 2580

        sum += fit + (lret < fitcpy ? 1 : 0);
        height += size.cy;

2581
        if(height > nheight)
2582
            break;
2583 2584 2585 2586

        /* Stop if this was a linewrap (but not if it was a linebreak). */
        if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
            break;
2587 2588
    }

2589
    GdipFree(stringdup);
2590 2591 2592 2593 2594 2595 2596 2597
    DeleteObject(rgn);
    DeleteObject(gdifont);

    RestoreDC(graphics->hdc, save_state);

    return Ok;
}

2598 2599 2600 2601 2602 2603
GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
    GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
{
    GpPath *path;
    GpStatus stat;

2604 2605 2606
    TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
            count, tension, fill);

2607 2608 2609
    if(!graphics || !brush || !points)
        return InvalidParameter;

2610 2611 2612
    if(graphics->busy)
        return ObjectBusy;

2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640
    stat = GdipCreatePath(fill, &path);
    if(stat != Ok)
        return stat;

    stat = GdipAddPathClosedCurve2(path, points, count, tension);
    if(stat != Ok){
        GdipDeletePath(path);
        return stat;
    }

    stat = GdipFillPath(graphics, brush, path);
    if(stat != Ok){
        GdipDeletePath(path);
        return stat;
    }

    GdipDeletePath(path);

    return Ok;
}

GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
    GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
{
    GpPointF *ptf;
    GpStatus stat;
    INT i;

2641 2642 2643
    TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
            count, tension, fill);

2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662
    if(!points || count <= 0)
        return InvalidParameter;

    ptf = GdipAlloc(sizeof(GpPointF)*count);
    if(!ptf)
        return OutOfMemory;

    for(i = 0;i < count;i++){
        ptf[i].X = (REAL)points[i].X;
        ptf[i].Y = (REAL)points[i].Y;
    }

    stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);

    GdipFree(ptf);

    return stat;
}

2663 2664 2665 2666 2667 2668 2669
GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
    REAL y, REAL width, REAL height)
{
    INT save_state;
    GpPointF ptf[2];
    POINT pti[2];

2670 2671
    TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);

2672 2673 2674
    if(!graphics || !brush)
        return InvalidParameter;

2675 2676 2677
    if(graphics->busy)
        return ObjectBusy;

2678 2679 2680 2681 2682 2683 2684 2685 2686 2687
    ptf[0].X = x;
    ptf[0].Y = y;
    ptf[1].X = x + width;
    ptf[1].Y = y + height;

    save_state = SaveDC(graphics->hdc);
    EndPath(graphics->hdc);

    transform_and_round_points(graphics, pti, ptf, 2);

2688
    BeginPath(graphics->hdc);
2689
    Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2690 2691 2692
    EndPath(graphics->hdc);

    brush_fill_path(graphics, brush);
2693 2694 2695 2696 2697 2698 2699 2700 2701

    RestoreDC(graphics->hdc, save_state);

    return Ok;
}

GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
    INT y, INT width, INT height)
{
2702 2703
    TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);

2704 2705 2706
    return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
}

2707 2708 2709 2710 2711
GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
{
    INT save_state;
    GpStatus retval;

2712 2713
    TRACE("(%p, %p, %p)\n", graphics, brush, path);

2714 2715 2716
    if(!brush || !graphics || !path)
        return InvalidParameter;

2717 2718 2719
    if(graphics->busy)
        return ObjectBusy;

2720 2721 2722 2723 2724 2725
    save_state = SaveDC(graphics->hdc);
    EndPath(graphics->hdc);
    SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
                                                                    : WINDING));

    BeginPath(graphics->hdc);
2726
    retval = draw_poly(graphics, NULL, path->pathdata.Points,
2727 2728 2729 2730 2731 2732
                       path->pathdata.Types, path->pathdata.Count, FALSE);

    if(retval != Ok)
        goto end;

    EndPath(graphics->hdc);
2733
    brush_fill_path(graphics, brush);
2734 2735 2736 2737 2738 2739 2740 2741 2742

    retval = Ok;

end:
    RestoreDC(graphics->hdc, save_state);

    return retval;
}

2743 2744 2745
GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
    REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
{
2746 2747
    INT save_state;

2748 2749 2750
    TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
            graphics, brush, x, y, width, height, startAngle, sweepAngle);

2751
    if(!graphics || !brush)
2752 2753
        return InvalidParameter;

2754 2755 2756
    if(graphics->busy)
        return ObjectBusy;

2757 2758 2759
    save_state = SaveDC(graphics->hdc);
    EndPath(graphics->hdc);

2760
    BeginPath(graphics->hdc);
2761
    draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2762 2763 2764
    EndPath(graphics->hdc);

    brush_fill_path(graphics, brush);
2765 2766 2767 2768

    RestoreDC(graphics->hdc, save_state);

    return Ok;
2769
}
2770

2771 2772 2773
GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
    INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
{
2774 2775 2776
    TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
            graphics, brush, x, y, width, height, startAngle, sweepAngle);

2777 2778 2779
    return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
}

2780 2781 2782 2783 2784 2785 2786 2787
GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
    GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
{
    INT save_state;
    GpPointF *ptf = NULL;
    POINT *pti = NULL;
    GpStatus retval = Ok;

2788 2789
    TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);

2790 2791 2792
    if(!graphics || !brush || !points || !count)
        return InvalidParameter;

2793 2794 2795
    if(graphics->busy)
        return ObjectBusy;

2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810
    ptf = GdipAlloc(count * sizeof(GpPointF));
    pti = GdipAlloc(count * sizeof(POINT));
    if(!ptf || !pti){
        retval = OutOfMemory;
        goto end;
    }

    memcpy(ptf, points, count * sizeof(GpPointF));

    save_state = SaveDC(graphics->hdc);
    EndPath(graphics->hdc);
    SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
                                                                  : WINDING));

    transform_and_round_points(graphics, pti, ptf, count);
2811 2812

    BeginPath(graphics->hdc);
2813
    Polygon(graphics->hdc, pti, count);
2814 2815 2816
    EndPath(graphics->hdc);

    brush_fill_path(graphics, brush);
2817 2818 2819 2820 2821 2822 2823 2824 2825 2826

    RestoreDC(graphics->hdc, save_state);

end:
    GdipFree(ptf);
    GdipFree(pti);

    return retval;
}

2827 2828 2829
GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
    GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
{
2830 2831 2832 2833
    INT save_state, i;
    GpPointF *ptf = NULL;
    POINT *pti = NULL;
    GpStatus retval = Ok;
2834

2835 2836
    TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);

2837 2838 2839
    if(!graphics || !brush || !points || !count)
        return InvalidParameter;

2840 2841 2842
    if(graphics->busy)
        return ObjectBusy;

2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854
    ptf = GdipAlloc(count * sizeof(GpPointF));
    pti = GdipAlloc(count * sizeof(POINT));
    if(!ptf || !pti){
        retval = OutOfMemory;
        goto end;
    }

    for(i = 0; i < count; i ++){
        ptf[i].X = (REAL) points[i].X;
        ptf[i].Y = (REAL) points[i].Y;
    }

2855 2856 2857 2858
    save_state = SaveDC(graphics->hdc);
    EndPath(graphics->hdc);
    SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
                                                                  : WINDING));
2859 2860

    transform_and_round_points(graphics, pti, ptf, count);
2861 2862

    BeginPath(graphics->hdc);
2863
    Polygon(graphics->hdc, pti, count);
2864 2865 2866
    EndPath(graphics->hdc);

    brush_fill_path(graphics, brush);
2867 2868

    RestoreDC(graphics->hdc, save_state);
2869 2870 2871 2872 2873 2874

end:
    GdipFree(ptf);
    GdipFree(pti);

    return retval;
2875 2876
}

2877 2878 2879
GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
    GDIPCONST GpPointF *points, INT count)
{
2880 2881
    TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);

2882 2883 2884 2885 2886 2887
    return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
}

GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
    GDIPCONST GpPoint *points, INT count)
{
2888 2889
    TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);

2890 2891 2892
    return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
}

2893 2894 2895 2896 2897 2898 2899
GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
    REAL x, REAL y, REAL width, REAL height)
{
    INT save_state;
    GpPointF ptf[4];
    POINT pti[4];

2900 2901
    TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);

2902 2903 2904
    if(!graphics || !brush)
        return InvalidParameter;

2905 2906 2907
    if(graphics->busy)
        return ObjectBusy;

2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921
    ptf[0].X = x;
    ptf[0].Y = y;
    ptf[1].X = x + width;
    ptf[1].Y = y;
    ptf[2].X = x + width;
    ptf[2].Y = y + height;
    ptf[3].X = x;
    ptf[3].Y = y + height;

    save_state = SaveDC(graphics->hdc);
    EndPath(graphics->hdc);

    transform_and_round_points(graphics, pti, ptf, 4);

2922
    BeginPath(graphics->hdc);
2923
    Polygon(graphics->hdc, pti, 4);
2924 2925 2926
    EndPath(graphics->hdc);

    brush_fill_path(graphics, brush);
2927 2928 2929 2930 2931 2932

    RestoreDC(graphics->hdc, save_state);

    return Ok;
}

2933 2934 2935 2936 2937 2938 2939
GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
    INT x, INT y, INT width, INT height)
{
    INT save_state;
    GpPointF ptf[4];
    POINT pti[4];

2940 2941
    TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);

2942 2943 2944
    if(!graphics || !brush)
        return InvalidParameter;

2945 2946 2947
    if(graphics->busy)
        return ObjectBusy;

2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961
    ptf[0].X = x;
    ptf[0].Y = y;
    ptf[1].X = x + width;
    ptf[1].Y = y;
    ptf[2].X = x + width;
    ptf[2].Y = y + height;
    ptf[3].X = x;
    ptf[3].Y = y + height;

    save_state = SaveDC(graphics->hdc);
    EndPath(graphics->hdc);

    transform_and_round_points(graphics, pti, ptf, 4);

2962
    BeginPath(graphics->hdc);
2963
    Polygon(graphics->hdc, pti, 4);
2964 2965 2966
    EndPath(graphics->hdc);

    brush_fill_path(graphics, brush);
2967 2968 2969 2970 2971 2972

    RestoreDC(graphics->hdc, save_state);

    return Ok;
}

2973 2974 2975 2976 2977 2978
GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
    INT count)
{
    GpStatus ret;
    INT i;

2979 2980
    TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);

2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998
    if(!rects)
        return InvalidParameter;

    for(i = 0; i < count; i++){
        ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
        if(ret != Ok)   return ret;
    }

    return Ok;
}

GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
    INT count)
{
    GpRectF *rectsF;
    GpStatus ret;
    INT i;

2999 3000
    TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);

3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020
    if(!rects || count <= 0)
        return InvalidParameter;

    rectsF = GdipAlloc(sizeof(GpRectF)*count);
    if(!rectsF)
        return OutOfMemory;

    for(i = 0; i < count; i++){
        rectsF[i].X      = (REAL)rects[i].X;
        rectsF[i].Y      = (REAL)rects[i].Y;
        rectsF[i].X      = (REAL)rects[i].Width;
        rectsF[i].Height = (REAL)rects[i].Height;
    }

    ret = GdipFillRectangles(graphics,brush,rectsF,count);
    GdipFree(rectsF);

    return ret;
}

3021 3022 3023
/*****************************************************************************
 * GdipFillRegion [GDIPLUS.@]
 */
3024 3025 3026
GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
        GpRegion* region)
{
3027 3028 3029
    INT save_state;
    GpStatus status;
    HRGN hrgn;
3030
    RECT rc;
3031 3032 3033

    TRACE("(%p, %p, %p)\n", graphics, brush, region);

3034 3035 3036
    if (!(graphics && brush && region))
        return InvalidParameter;

3037 3038 3039
    if(graphics->busy)
        return ObjectBusy;

3040 3041 3042
    status = GdipGetRegionHRgn(region, graphics, &hrgn);
    if(status != Ok)
        return status;
3043

3044 3045 3046
    save_state = SaveDC(graphics->hdc);
    EndPath(graphics->hdc);

3047 3048 3049 3050 3051 3052 3053 3054 3055 3056
    ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);

    if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
    {
        BeginPath(graphics->hdc);
        Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
        EndPath(graphics->hdc);

        brush_fill_path(graphics, brush);
    }
3057 3058 3059 3060 3061 3062

    RestoreDC(graphics->hdc, save_state);

    DeleteObject(hrgn);

    return Ok;
3063 3064
}

3065 3066 3067 3068
GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
{
    static int calls;

3069 3070
    TRACE("(%p,%u)\n", graphics, intention);

3071 3072 3073
    if(!graphics)
        return InvalidParameter;

3074 3075 3076
    if(graphics->busy)
        return ObjectBusy;

3077 3078 3079 3080 3081 3082
    if(!(calls++))
        FIXME("not implemented\n");

    return NotImplemented;
}

3083 3084 3085 3086 3087 3088 3089 3090 3091 3092
/*****************************************************************************
 * GdipGetClipBounds [GDIPLUS.@]
 */
GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
{
    TRACE("(%p, %p)\n", graphics, rect);

    if(!graphics)
        return InvalidParameter;

3093 3094 3095
    if(graphics->busy)
        return ObjectBusy;

3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108
    return GdipGetRegionBounds(graphics->clip, graphics, rect);
}

/*****************************************************************************
 * GdipGetClipBoundsI [GDIPLUS.@]
 */
GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
{
    TRACE("(%p, %p)\n", graphics, rect);

    if(!graphics)
        return InvalidParameter;

3109 3110 3111
    if(graphics->busy)
        return ObjectBusy;

3112 3113 3114
    return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
}

3115 3116 3117 3118
/* FIXME: Compositing mode is not used anywhere except the getter/setter. */
GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
    CompositingMode *mode)
{
3119 3120
    TRACE("(%p, %p)\n", graphics, mode);

3121 3122 3123
    if(!graphics || !mode)
        return InvalidParameter;

3124 3125 3126
    if(graphics->busy)
        return ObjectBusy;

3127 3128 3129 3130 3131
    *mode = graphics->compmode;

    return Ok;
}

3132 3133 3134 3135
/* FIXME: Compositing quality is not used anywhere except the getter/setter. */
GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
    CompositingQuality *quality)
{
3136 3137
    TRACE("(%p, %p)\n", graphics, quality);

3138 3139 3140
    if(!graphics || !quality)
        return InvalidParameter;

3141 3142 3143
    if(graphics->busy)
        return ObjectBusy;

3144 3145 3146 3147 3148
    *quality = graphics->compqual;

    return Ok;
}

3149 3150 3151 3152
/* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
    InterpolationMode *mode)
{
3153 3154
    TRACE("(%p, %p)\n", graphics, mode);

3155 3156 3157
    if(!graphics || !mode)
        return InvalidParameter;

3158 3159 3160
    if(graphics->busy)
        return ObjectBusy;

3161 3162 3163 3164 3165
    *mode = graphics->interpolation;

    return Ok;
}

3166 3167
GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
{
3168 3169
    FIXME("(%p, %p): stub\n", graphics, argb);

3170 3171 3172 3173 3174 3175 3176 3177 3178
    if(!graphics || !argb)
        return InvalidParameter;

    if(graphics->busy)
        return ObjectBusy;

    return NotImplemented;
}

3179 3180
GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
{
3181 3182
    TRACE("(%p, %p)\n", graphics, scale);

3183 3184 3185
    if(!graphics || !scale)
        return InvalidParameter;

3186 3187 3188
    if(graphics->busy)
        return ObjectBusy;

3189 3190 3191 3192 3193
    *scale = graphics->scale;

    return Ok;
}

3194 3195
GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
{
3196 3197
    TRACE("(%p, %p)\n", graphics, unit);

3198 3199 3200
    if(!graphics || !unit)
        return InvalidParameter;

3201 3202 3203
    if(graphics->busy)
        return ObjectBusy;

3204 3205 3206 3207 3208
    *unit = graphics->unit;

    return Ok;
}

3209 3210 3211 3212
/* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
    *mode)
{
3213 3214
    TRACE("(%p, %p)\n", graphics, mode);

3215 3216 3217
    if(!graphics || !mode)
        return InvalidParameter;

3218 3219 3220
    if(graphics->busy)
        return ObjectBusy;

3221 3222 3223 3224 3225
    *mode = graphics->pixeloffset;

    return Ok;
}

3226 3227 3228
/* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
{
3229 3230
    TRACE("(%p, %p)\n", graphics, mode);

3231 3232 3233
    if(!graphics || !mode)
        return InvalidParameter;

3234 3235 3236
    if(graphics->busy)
        return ObjectBusy;

3237 3238 3239 3240 3241
    *mode = graphics->smoothing;

    return Ok;
}

3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253
GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
{
    TRACE("(%p, %p)\n", graphics, contrast);

    if(!graphics || !contrast)
        return InvalidParameter;

    *contrast = graphics->textcontrast;

    return Ok;
}

3254 3255 3256 3257
/* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
    TextRenderingHint *hint)
{
3258 3259
    TRACE("(%p, %p)\n", graphics, hint);

3260 3261 3262
    if(!graphics || !hint)
        return InvalidParameter;

3263 3264 3265
    if(graphics->busy)
        return ObjectBusy;

3266 3267 3268 3269 3270
    *hint = graphics->texthint;

    return Ok;
}

3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287
GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
{
    GpRegion *clip_rgn;
    GpStatus stat;

    TRACE("(%p, %p)\n", graphics, rect);

    if(!graphics || !rect)
        return InvalidParameter;

    if(graphics->busy)
        return ObjectBusy;

    /* intersect window and graphics clipping regions */
    if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
        return stat;

3288
    if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320
        goto cleanup;

    /* get bounds of the region */
    stat = GdipGetRegionBounds(clip_rgn, graphics, rect);

cleanup:
    GdipDeleteRegion(clip_rgn);

    return stat;
}

GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
{
    GpRectF rectf;
    GpStatus stat;

    TRACE("(%p, %p)\n", graphics, rect);

    if(!graphics || !rect)
        return InvalidParameter;

    if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
    {
        rect->X = roundr(rectf.X);
        rect->Y = roundr(rectf.Y);
        rect->Width  = roundr(rectf.Width);
        rect->Height = roundr(rectf.Height);
    }

    return stat;
}

3321 3322
GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
{
3323 3324
    TRACE("(%p, %p)\n", graphics, matrix);

3325 3326 3327
    if(!graphics || !matrix)
        return InvalidParameter;

3328 3329 3330
    if(graphics->busy)
        return ObjectBusy;

3331
    *matrix = *graphics->worldtrans;
3332
    return Ok;
3333 3334
}

3335 3336 3337 3338
GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
{
    GpSolidFill *brush;
    GpStatus stat;
3339
    GpRectF wnd_rect;
3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351

    TRACE("(%p, %x)\n", graphics, color);

    if(!graphics)
        return InvalidParameter;

    if(graphics->busy)
        return ObjectBusy;

    if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
        return stat;

3352 3353 3354
    if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
        GdipDeleteBrush((GpBrush*)brush);
        return stat;
3355
    }
3356 3357 3358

    GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
                                                 wnd_rect.Width, wnd_rect.Height);
3359 3360 3361 3362 3363 3364

    GdipDeleteBrush((GpBrush*)brush);

    return Ok;
}

3365 3366
GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
{
3367 3368
    TRACE("(%p, %p)\n", graphics, res);

3369 3370 3371 3372 3373 3374
    if(!graphics || !res)
        return InvalidParameter;

    return GdipIsEmptyRegion(graphics->clip, graphics, res);
}

3375 3376
GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
{
3377 3378 3379 3380 3381
    GpStatus stat;
    GpRegion* rgn;
    GpPointF pt;

    TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
3382 3383 3384 3385 3386 3387 3388

    if(!graphics || !result)
        return InvalidParameter;

    if(graphics->busy)
        return ObjectBusy;

3389 3390 3391 3392 3393
    pt.X = x;
    pt.Y = y;
    if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
                   CoordinateSpaceWorld, &pt, 1)) != Ok)
        return stat;
3394

3395 3396
    if((stat = GdipCreateRegion(&rgn)) != Ok)
        return stat;
3397

3398 3399
    if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
        goto cleanup;
3400

3401
    stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
3402

3403 3404 3405 3406 3407 3408 3409 3410
cleanup:
    GdipDeleteRegion(rgn);
    return stat;
}

GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
{
    return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
3411 3412
}

3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456
GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
{
    GpStatus stat;
    GpRegion* rgn;
    GpPointF pts[2];

    TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);

    if(!graphics || !result)
        return InvalidParameter;

    if(graphics->busy)
        return ObjectBusy;

    pts[0].X = x;
    pts[0].Y = y;
    pts[1].X = x + width;
    pts[1].Y = y + height;

    if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
                    CoordinateSpaceWorld, pts, 2)) != Ok)
        return stat;

    pts[1].X -= pts[0].X;
    pts[1].Y -= pts[0].Y;

    if((stat = GdipCreateRegion(&rgn)) != Ok)
        return stat;

    if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
        goto cleanup;

    stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);

cleanup:
    GdipDeleteRegion(rgn);
    return stat;
}

GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
{
    return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
}

3457 3458 3459 3460 3461 3462 3463 3464
GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
        GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
        GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
        INT regionCount, GpRegion** regions)
{
    FIXME("stub: %p %s %d %p %p %p %d %p\n", graphics, debugstr_w(string),
            length, font, layoutRect, stringFormat, regionCount, regions);

3465 3466 3467
    if (!(graphics && string && font && layoutRect && stringFormat && regions))
        return InvalidParameter;

3468 3469 3470
    return NotImplemented;
}

3471 3472 3473 3474
/* Find the smallest rectangle that bounds the text when it is printed in rect
 * according to the format options listed in format. If rect has 0 width and
 * height, then just find the smallest rectangle that bounds the text when it's
 * printed at location (rect->X, rect-Y). */
3475 3476 3477 3478 3479 3480 3481
GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
    GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
    GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
    INT *codepointsfitted, INT *linesfilled)
{
    HFONT oldfont;
    WCHAR* stringdup;
3482
    INT sum = 0, height = 0, fit, fitcpy, max_width = 0, i, j, lret, nwidth,
3483
        nheight, lineend;
3484 3485
    SIZE size;

3486 3487 3488 3489
    TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
        debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
        bounds, codepointsfitted, linesfilled);

3490 3491 3492
    if(!graphics || !string || !font || !rect)
        return InvalidParameter;

3493 3494
    if(linesfilled) *linesfilled = 0;
    if(codepointsfitted) *codepointsfitted = 0;
3495

3496 3497 3498
    if(format)
        TRACE("may be ignoring some format flags: attr %x\n", format->attr);

3499 3500
    if(length == -1) length = lstrlenW(string);

3501
    stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
3502 3503 3504 3505
    if(!stringdup) return OutOfMemory;

    oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
    nwidth = roundr(rect->Width);
3506 3507 3508 3509
    nheight = roundr(rect->Height);

    if((nwidth == 0) && (nheight == 0))
        nwidth = nheight = INT_MAX;
3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535

    for(i = 0, j = 0; i < length; i++){
        if(!isprintW(string[i]) && (string[i] != '\n'))
            continue;

        stringdup[j] = string[i];
        j++;
    }

    stringdup[j] = 0;
    length = j;

    while(sum < length){
        GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
                              nwidth, &fit, NULL, &size);
        fitcpy = fit;

        if(fit == 0)
            break;

        for(lret = 0; lret < fit; lret++)
            if(*(stringdup + sum + lret) == '\n')
                break;

        /* Line break code (may look strange, but it imitates windows). */
        if(lret < fit)
3536
            lineend = fit = lret;    /* this is not an off-by-one error */
3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552
        else if(fit < (length - sum)){
            if(*(stringdup + sum + fit) == ' ')
                while(*(stringdup + sum + fit) == ' ')
                    fit++;
            else
                while(*(stringdup + sum + fit - 1) != ' '){
                    fit--;

                    if(*(stringdup + sum + fit) == '\t')
                        break;

                    if(fit == 0){
                        fit = fitcpy;
                        break;
                    }
                }
3553 3554 3555 3556
            lineend = fit;
            while(*(stringdup + sum + lineend - 1) == ' ' ||
                  *(stringdup + sum + lineend - 1) == '\t')
                lineend--;
3557
        }
3558 3559
        else
            lineend = fit;
3560

3561
        GetTextExtentExPointW(graphics->hdc, stringdup + sum, lineend,
3562 3563 3564
                              nwidth, &j, NULL, &size);

        sum += fit + (lret < fitcpy ? 1 : 0);
3565 3566
        if(codepointsfitted) *codepointsfitted = sum;

3567
        height += size.cy;
3568
        if(linesfilled) *linesfilled += size.cy;
3569 3570
        max_width = max(max_width, size.cx);

3571
        if(height > nheight)
3572
            break;
3573 3574 3575 3576

        /* Stop if this was a linewrap (but not if it was a linebreak). */
        if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
            break;
3577 3578 3579 3580 3581
    }

    bounds->X = rect->X;
    bounds->Y = rect->Y;
    bounds->Width = (REAL)max_width;
3582
    bounds->Height = (REAL) min(height, nheight);
3583

3584
    GdipFree(stringdup);
3585 3586 3587
    DeleteObject(SelectObject(graphics->hdc, oldfont));

    return Ok;
3588 3589
}

3590 3591
GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
{
3592 3593
    TRACE("(%p)\n", graphics);

3594 3595 3596 3597 3598 3599 3600 3601 3602
    if(!graphics)
        return InvalidParameter;

    if(graphics->busy)
        return ObjectBusy;

    return GdipSetInfinite(graphics->clip);
}

3603 3604
GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
{
3605 3606
    TRACE("(%p)\n", graphics);

3607 3608 3609
    if(!graphics)
        return InvalidParameter;

3610 3611 3612
    if(graphics->busy)
        return ObjectBusy;

3613 3614 3615 3616 3617 3618 3619 3620 3621 3622
    graphics->worldtrans->matrix[0] = 1.0;
    graphics->worldtrans->matrix[1] = 0.0;
    graphics->worldtrans->matrix[2] = 0.0;
    graphics->worldtrans->matrix[3] = 1.0;
    graphics->worldtrans->matrix[4] = 0.0;
    graphics->worldtrans->matrix[5] = 0.0;

    return Ok;
}

3623 3624
GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
{
3625
    return GdipEndContainer(graphics, state);
3626 3627
}

3628 3629 3630
GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
    GpMatrixOrder order)
{
3631 3632
    TRACE("(%p, %.2f, %d)\n", graphics, angle, order);

3633 3634 3635
    if(!graphics)
        return InvalidParameter;

3636 3637 3638
    if(graphics->busy)
        return ObjectBusy;

3639 3640 3641
    return GdipRotateMatrix(graphics->worldtrans, angle, order);
}

3642 3643
GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
{
3644
    return GdipBeginContainer2(graphics, state);
3645 3646
}

3647 3648
GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
        GraphicsContainer *state)
3649
{
3650 3651 3652 3653
    GraphicsContainerItem *container;
    GpStatus sts;

    TRACE("(%p, %p)\n", graphics, state);
3654 3655 3656 3657

    if(!graphics || !state)
        return InvalidParameter;

3658 3659 3660 3661 3662 3663 3664
    sts = init_container(&container, graphics);
    if(sts != Ok)
        return sts;

    list_add_head(&graphics->containers, &container->entry);
    *state = graphics->contid = container->contid;

3665 3666 3667
    return Ok;
}

3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679
GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
{
    FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
    return NotImplemented;
}

GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
{
    FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
    return NotImplemented;
}

3680 3681 3682 3683 3684 3685
GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
{
    FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
    return NotImplemented;
}

3686
GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
3687
{
3688 3689
    GpStatus sts;
    GraphicsContainerItem *container, *container2;
3690

3691 3692 3693
    TRACE("(%p, %x)\n", graphics, state);

    if(!graphics)
3694 3695
        return InvalidParameter;

3696 3697 3698 3699 3700 3701 3702 3703 3704
    LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
        if(container->contid == state)
            break;
    }

    /* did not find a matching container */
    if(&container->entry == &graphics->containers)
        return Ok;

3705 3706 3707 3708
    sts = restore_container(graphics, container);
    if(sts != Ok)
        return sts;

3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719
    /* remove all of the containers on top of the found container */
    LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
        if(container->contid == state)
            break;
        list_remove(&container->entry);
        delete_container(container);
    }

    list_remove(&container->entry);
    delete_container(container);

3720
    return Ok;
3721 3722
}

3723 3724 3725
GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
    REAL sy, GpMatrixOrder order)
{
3726 3727
    TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);

3728 3729 3730
    if(!graphics)
        return InvalidParameter;

3731 3732 3733
    if(graphics->busy)
        return ObjectBusy;

3734 3735 3736
    return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
}

3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747
GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
    CombineMode mode)
{
    TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);

    if(!graphics || !srcgraphics)
        return InvalidParameter;

    return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
}

3748 3749 3750
GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
    CompositingMode mode)
{
3751 3752
    TRACE("(%p, %d)\n", graphics, mode);

3753 3754 3755
    if(!graphics)
        return InvalidParameter;

3756 3757 3758
    if(graphics->busy)
        return ObjectBusy;

3759 3760 3761 3762 3763
    graphics->compmode = mode;

    return Ok;
}

3764 3765 3766
GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
    CompositingQuality quality)
{
3767 3768
    TRACE("(%p, %d)\n", graphics, quality);

3769 3770 3771
    if(!graphics)
        return InvalidParameter;

3772 3773 3774
    if(graphics->busy)
        return ObjectBusy;

3775 3776 3777 3778 3779
    graphics->compqual = quality;

    return Ok;
}

3780 3781 3782
GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
    InterpolationMode mode)
{
3783 3784
    TRACE("(%p, %d)\n", graphics, mode);

3785 3786 3787
    if(!graphics)
        return InvalidParameter;

3788 3789 3790
    if(graphics->busy)
        return ObjectBusy;

3791 3792 3793 3794 3795
    graphics->interpolation = mode;

    return Ok;
}

3796 3797
GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
{
3798 3799
    TRACE("(%p, %.2f)\n", graphics, scale);

3800 3801 3802
    if(!graphics || (scale <= 0.0))
        return InvalidParameter;

3803 3804 3805
    if(graphics->busy)
        return ObjectBusy;

3806 3807 3808 3809 3810
    graphics->scale = scale;

    return Ok;
}

3811 3812
GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
{
3813 3814
    TRACE("(%p, %d)\n", graphics, unit);

3815 3816 3817 3818 3819 3820 3821
    if(!graphics)
        return InvalidParameter;

    if(graphics->busy)
        return ObjectBusy;

    if(unit == UnitWorld)
3822 3823 3824 3825 3826 3827 3828
        return InvalidParameter;

    graphics->unit = unit;

    return Ok;
}

3829 3830 3831
GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
    mode)
{
3832 3833
    TRACE("(%p, %d)\n", graphics, mode);

3834 3835 3836
    if(!graphics)
        return InvalidParameter;

3837 3838 3839
    if(graphics->busy)
        return ObjectBusy;

3840 3841 3842 3843 3844
    graphics->pixeloffset = mode;

    return Ok;
}

3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856
GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
{
    static int calls;

    TRACE("(%p,%i,%i)\n", graphics, x, y);

    if (!(calls++))
        FIXME("not implemented\n");

    return NotImplemented;
}

3857 3858
GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
{
3859 3860
    TRACE("(%p, %d)\n", graphics, mode);

3861 3862 3863
    if(!graphics)
        return InvalidParameter;

3864 3865 3866
    if(graphics->busy)
        return ObjectBusy;

3867 3868 3869 3870
    graphics->smoothing = mode;

    return Ok;
}
3871

3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883
GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
{
    TRACE("(%p, %d)\n", graphics, contrast);

    if(!graphics)
        return InvalidParameter;

    graphics->textcontrast = contrast;

    return Ok;
}

3884 3885 3886
GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
    TextRenderingHint hint)
{
3887 3888
    TRACE("(%p, %d)\n", graphics, hint);

3889 3890 3891
    if(!graphics)
        return InvalidParameter;

3892 3893 3894
    if(graphics->busy)
        return ObjectBusy;

3895 3896 3897 3898 3899
    graphics->texthint = hint;

    return Ok;
}

3900 3901
GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
{
3902 3903
    TRACE("(%p, %p)\n", graphics, matrix);

3904 3905 3906
    if(!graphics || !matrix)
        return InvalidParameter;

3907 3908 3909
    if(graphics->busy)
        return ObjectBusy;

3910 3911 3912
    GdipDeleteMatrix(graphics->worldtrans);
    return GdipCloneMatrix(matrix, &graphics->worldtrans);
}
3913 3914 3915 3916

GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
    REAL dy, GpMatrixOrder order)
{
3917 3918
    TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);

3919 3920 3921
    if(!graphics)
        return InvalidParameter;

3922 3923 3924
    if(graphics->busy)
        return ObjectBusy;

3925 3926
    return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
}
3927

3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950
/*****************************************************************************
 * GdipSetClipHrgn [GDIPLUS.@]
 */
GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
{
    GpRegion *region;
    GpStatus status;

    TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);

    if(!graphics)
        return InvalidParameter;

    status = GdipCreateRegionHrgn(hrgn, &region);
    if(status != Ok)
        return status;

    status = GdipSetClipRegion(graphics, region, mode);

    GdipDeleteRegion(region);
    return status;
}

3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963
GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
{
    TRACE("(%p, %p, %d)\n", graphics, path, mode);

    if(!graphics)
        return InvalidParameter;

    if(graphics->busy)
        return ObjectBusy;

    return GdipCombineRegionPath(graphics->clip, path, mode);
}

3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985
GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
                                    REAL width, REAL height,
                                    CombineMode mode)
{
    GpRectF rect;

    TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);

    if(!graphics)
        return InvalidParameter;

    if(graphics->busy)
        return ObjectBusy;

    rect.X = x;
    rect.Y = y;
    rect.Width  = width;
    rect.Height = height;

    return GdipCombineRegionRect(graphics->clip, &rect, mode);
}

3986 3987
GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
                                     INT width, INT height,
3988
                                     CombineMode mode)
3989
{
3990
    TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
3991

3992 3993 3994 3995 3996 3997
    if(!graphics)
        return InvalidParameter;

    if(graphics->busy)
        return ObjectBusy;

3998
    return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
3999
}
4000 4001

GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
4002
                                      CombineMode mode)
4003
{
4004
    TRACE("(%p, %p, %d)\n", graphics, region, mode);
4005

4006 4007
    if(!graphics || !region)
        return InvalidParameter;
4008

4009 4010 4011 4012
    if(graphics->busy)
        return ObjectBusy;

    return GdipCombineRegionRegion(graphics->clip, region, mode);
4013
}
4014

4015
GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
4016 4017 4018 4019
    UINT limitDpi)
{
    static int calls;

4020 4021
    TRACE("(%p,%u)\n", metafile, limitDpi);

4022 4023 4024 4025 4026
    if(!(calls++))
        FIXME("not implemented\n");

    return NotImplemented;
}
4027 4028 4029 4030 4031 4032 4033

GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
    INT count)
{
    INT save_state;
    POINT *pti;

4034 4035
    TRACE("(%p, %p, %d)\n", graphics, points, count);

4036 4037 4038
    if(!graphics || !pen || count<=0)
        return InvalidParameter;

4039 4040 4041
    if(graphics->busy)
        return ObjectBusy;

4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062
    pti = GdipAlloc(sizeof(POINT) * count);

    save_state = prepare_dc(graphics, pen);
    SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));

    transform_and_round_points(graphics, pti, (GpPointF*)points, count);
    Polygon(graphics->hdc, pti, count);

    restore_dc(graphics, save_state);
    GdipFree(pti);

    return Ok;
}

GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
    INT count)
{
    GpStatus ret;
    GpPointF *ptf;
    INT i;

4063 4064
    TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);

4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077
    if(count<=0)    return InvalidParameter;
    ptf = GdipAlloc(sizeof(GpPointF) * count);

    for(i = 0;i < count; i++){
        ptf[i].X = (REAL)points[i].X;
        ptf[i].Y = (REAL)points[i].Y;
    }

    ret = GdipDrawPolygon(graphics,pen,ptf,count);
    GdipFree(ptf);

    return ret;
}
4078 4079 4080

GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
{
4081 4082
    TRACE("(%p, %p)\n", graphics, dpi);

4083 4084 4085
    if(!graphics || !dpi)
        return InvalidParameter;

4086 4087 4088
    if(graphics->busy)
        return ObjectBusy;

4089 4090 4091 4092 4093 4094 4095
    *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);

    return Ok;
}

GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
{
4096 4097
    TRACE("(%p, %p)\n", graphics, dpi);

4098 4099 4100
    if(!graphics || !dpi)
        return InvalidParameter;

4101 4102 4103
    if(graphics->busy)
        return ObjectBusy;

4104 4105 4106 4107
    *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);

    return Ok;
}
4108 4109 4110 4111 4112 4113 4114

GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
    GpMatrixOrder order)
{
    GpMatrix m;
    GpStatus ret;

4115 4116
    TRACE("(%p, %p, %d)\n", graphics, matrix, order);

4117 4118 4119
    if(!graphics || !matrix)
        return InvalidParameter;

4120 4121 4122
    if(graphics->busy)
        return ObjectBusy;

4123 4124
    m = *(graphics->worldtrans);

4125
    ret = GdipMultiplyMatrix(&m, matrix, order);
4126 4127 4128 4129 4130
    if(ret == Ok)
        *(graphics->worldtrans) = m;

    return ret;
}
4131 4132 4133

GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
{
4134 4135
    TRACE("(%p, %p)\n", graphics, hdc);

4136 4137 4138
    if(!graphics || !hdc)
        return InvalidParameter;

4139 4140 4141 4142 4143 4144 4145
    if(graphics->busy)
        return ObjectBusy;

    *hdc = graphics->hdc;
    graphics->busy = TRUE;

    return Ok;
4146 4147 4148 4149
}

GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
{
4150 4151
    TRACE("(%p, %p)\n", graphics, hdc);

4152 4153 4154
    if(!graphics)
        return InvalidParameter;

4155
    if(graphics->hdc != hdc || !(graphics->busy))
4156 4157
        return InvalidParameter;

4158 4159 4160
    graphics->busy = FALSE;

    return Ok;
4161
}
4162 4163 4164

GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
{
4165 4166 4167 4168 4169
    GpRegion *clip;
    GpStatus status;

    TRACE("(%p, %p)\n", graphics, region);

4170 4171
    if(!graphics || !region)
        return InvalidParameter;
4172

4173 4174 4175
    if(graphics->busy)
        return ObjectBusy;

4176 4177 4178 4179 4180 4181
    if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
        return status;

    /* free everything except root node and header */
    delete_element(&region->node);
    memcpy(region, clip, sizeof(GpRegion));
Huw Davies's avatar
Huw Davies committed
4182
    GdipFree(clip);
4183 4184

    return Ok;
4185
}
4186 4187 4188 4189

GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
                                        GpCoordinateSpace src_space, GpPointF *points, INT count)
{
4190 4191 4192 4193
    GpMatrix *matrix;
    GpStatus stat;
    REAL unitscale;

4194 4195 4196 4197 4198 4199
    if(!graphics || !points || count <= 0)
        return InvalidParameter;

    if(graphics->busy)
        return ObjectBusy;

4200
    TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
4201

4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254
    if (src_space == dst_space) return Ok;

    stat = GdipCreateMatrix(&matrix);
    if (stat == Ok)
    {
        unitscale = convert_unit(graphics->hdc, graphics->unit);

        if(graphics->unit != UnitDisplay)
            unitscale *= graphics->scale;

        /* transform from src_space to CoordinateSpacePage */
        switch (src_space)
        {
        case CoordinateSpaceWorld:
            GdipMultiplyMatrix(matrix, graphics->worldtrans, MatrixOrderAppend);
            break;
        case CoordinateSpacePage:
            break;
        case CoordinateSpaceDevice:
            GdipScaleMatrix(matrix, 1.0/unitscale, 1.0/unitscale, MatrixOrderAppend);
            break;
        }

        /* transform from CoordinateSpacePage to dst_space */
        switch (dst_space)
        {
        case CoordinateSpaceWorld:
            {
                GpMatrix *inverted_transform;
                stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
                if (stat == Ok)
                {
                    stat = GdipInvertMatrix(inverted_transform);
                    if (stat == Ok)
                        GdipMultiplyMatrix(matrix, inverted_transform, MatrixOrderAppend);
                    GdipDeleteMatrix(inverted_transform);
                }
                break;
            }
        case CoordinateSpacePage:
            break;
        case CoordinateSpaceDevice:
            GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
            break;
        }

        if (stat == Ok)
            stat = GdipTransformMatrixPoints(matrix, points, count);

        GdipDeleteMatrix(matrix);
    }

    return stat;
4255 4256 4257 4258 4259
}

GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
                                         GpCoordinateSpace src_space, GpPoint *points, INT count)
{
4260 4261 4262
    GpPointF *pointsF;
    GpStatus ret;
    INT i;
4263

4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287
    TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);

    if(count <= 0)
        return InvalidParameter;

    pointsF = GdipAlloc(sizeof(GpPointF) * count);
    if(!pointsF)
        return OutOfMemory;

    for(i = 0; i < count; i++){
        pointsF[i].X = (REAL)points[i].X;
        pointsF[i].Y = (REAL)points[i].Y;
    }

    ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);

    if(ret == Ok)
        for(i = 0; i < count; i++){
            points[i].X = roundr(pointsF[i].X);
            points[i].Y = roundr(pointsF[i].Y);
        }
    GdipFree(pointsF);

    return ret;
4288
}
4289 4290 4291 4292 4293 4294 4295

HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
{
    FIXME("\n");

    return NULL;
}
4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306

/*****************************************************************************
 * GdipTranslateClip [GDIPLUS.@]
 */
GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
{
    TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);

    if(!graphics)
        return InvalidParameter;

4307 4308 4309
    if(graphics->busy)
        return ObjectBusy;

4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322
    return GdipTranslateRegion(graphics->clip, dx, dy);
}

/*****************************************************************************
 * GdipTranslateClipI [GDIPLUS.@]
 */
GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
{
    TRACE("(%p, %d, %d)\n", graphics, dx, dy);

    if(!graphics)
        return InvalidParameter;

4323 4324 4325
    if(graphics->busy)
        return ObjectBusy;

4326 4327
    return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
}
4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348


/*****************************************************************************
 * GdipMeasureDriverString [GDIPLUS.@]
 */
GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
                                            GDIPCONST GpFont *font, GDIPCONST PointF *positions,
                                            INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
{
    FIXME("(%p %p %d %p %p %d %p %p): stub\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
    return NotImplemented;
}

/*****************************************************************************
 * GdipDrawDriverString [GDIPLUS.@]
 */
GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
                                         GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
                                         GDIPCONST PointF *positions, INT flags,
                                         GDIPCONST GpMatrix *matrix )
{
4349
    FIXME("(%p %p %d %p %p %p %d %p): stub\n", graphics, text, length, font, brush, positions, flags, matrix);
4350 4351
    return NotImplemented;
}
4352

4353 4354 4355 4356 4357 4358 4359 4360 4361
/*****************************************************************************
 * GdipRecordMetafileI [GDIPLUS.@]
 */
GpStatus WINGDIPAPI GdipRecordMetafileI(HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
                                        MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
{
    FIXME("(%p %d %p %d %p %p): stub\n", hdc, type, frameRect, frameUnit, desc, metafile);
    return NotImplemented;
}
4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384

/*****************************************************************************
 * GdipIsVisibleClipEmpty [GDIPLUS.@]
 */
GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
{
    GpStatus stat;
    GpRegion* rgn;

    TRACE("(%p, %p)\n", graphics, res);

    if((stat = GdipCreateRegion(&rgn)) != Ok)
        return stat;

    if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
        goto cleanup;

    stat = GdipIsEmptyRegion(rgn, graphics, res);

cleanup:
    GdipDeleteRegion(rgn);
    return stat;
}