pen.c 16.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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 "windef.h"
#include "winbase.h"
#include "wingdi.h"
24 25 26

#include "objbase.h"

27 28 29 30 31 32
#include "gdiplus.h"
#include "gdiplus_private.h"
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);

33 34 35 36 37 38 39 40 41 42 43 44 45 46
static DWORD gdip_to_gdi_dash(GpDashStyle dash)
{
    switch(dash){
        case DashStyleSolid:
            return PS_SOLID;
        case DashStyleDash:
            return PS_DASH;
        case DashStyleDot:
            return PS_DOT;
        case DashStyleDashDot:
            return PS_DASHDOT;
        case DashStyleDashDotDot:
            return PS_DASHDOTDOT;
        case DashStyleCustom:
47
            return PS_USERSTYLE;
48 49 50 51 52 53
        default:
            ERR("Not a member of GpDashStyle enumeration\n");
            return 0;
    }
}

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
static DWORD gdip_to_gdi_join(GpLineJoin join)
{
    switch(join){
        case LineJoinRound:
            return PS_JOIN_ROUND;
        case LineJoinBevel:
            return PS_JOIN_BEVEL;
        case LineJoinMiter:
        case LineJoinMiterClipped:
            return PS_JOIN_MITER;
        default:
            ERR("Not a member of GpLineJoin enumeration\n");
            return 0;
    }
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
static GpPenType bt_to_pt(GpBrushType bt)
{
    switch(bt){
        case BrushTypeSolidColor:
            return PenTypeSolidColor;
        case BrushTypeHatchFill:
            return PenTypeHatchFill;
        case BrushTypeTextureFill:
            return PenTypeTextureFill;
        case BrushTypePathGradient:
            return PenTypePathGradient;
        case BrushTypeLinearGradient:
            return PenTypeLinearGradient;
        default:
            return PenTypeUnknown;
    }
}

88 89
GpStatus WINGDIPAPI GdipClonePen(GpPen *pen, GpPen **clonepen)
{
90 91
    TRACE("(%p, %p)\n", pen, clonepen);

92 93 94 95 96 97
    if(!pen || !clonepen)
        return InvalidParameter;

    *clonepen = GdipAlloc(sizeof(GpPen));
    if(!*clonepen)  return OutOfMemory;

98
    **clonepen = *pen;
99

100 101 102
    GdipCloneCustomLineCap(pen->customstart, &(*clonepen)->customstart);
    GdipCloneCustomLineCap(pen->customend, &(*clonepen)->customend);
    GdipCloneBrush(pen->brush, &(*clonepen)->brush);
103

104 105
    TRACE("<-- %p\n", *clonepen);

106 107 108
    return Ok;
}

109
GpStatus WINGDIPAPI GdipCreatePen1(ARGB color, REAL width, GpUnit unit,
110
    GpPen **pen)
111 112
{
    GpBrush *brush;
113 114
    GpStatus status;

115 116
    TRACE("(%x, %.2f, %d, %p)\n", color, width, unit, pen);

117
    GdipCreateSolidFill(color, (GpSolidFill **)(&brush));
118 119 120
    status = GdipCreatePen2(brush, width, unit, pen);
    GdipDeleteBrush(brush);
    return status;
121 122 123 124
}

GpStatus WINGDIPAPI GdipCreatePen2(GpBrush *brush, REAL width, GpUnit unit,
    GpPen **pen)
125 126
{
    GpPen *gp_pen;
127
    GpBrush *clone_brush;
128

129
    TRACE("(%p, %.2f, %d, %p)\n", brush, width, unit, pen);
130

131
    if(!pen || !brush)
132 133
        return InvalidParameter;

134
    gp_pen = GdipAlloc(sizeof(GpPen));
135
    if(!gp_pen)    return OutOfMemory;
136 137 138 139

    gp_pen->style = GP_DEFAULT_PENSTYLE;
    gp_pen->width = width;
    gp_pen->unit = unit;
140
    gp_pen->endcap = LineCapFlat;
141 142
    gp_pen->join = LineJoinMiter;
    gp_pen->miterlimit = 10.0;
143
    gp_pen->dash = DashStyleSolid;
144
    gp_pen->offset = 0.0;
145 146
    gp_pen->customstart = NULL;
    gp_pen->customend = NULL;
147

148
    if(!((gp_pen->unit == UnitWorld) || (gp_pen->unit == UnitPixel))) {
149
        FIXME("UnitWorld, UnitPixel only supported units\n");
150
        GdipFree(gp_pen);
151 152 153
        return NotImplemented;
    }

154 155 156
    GdipCloneBrush(brush, &clone_brush);
    gp_pen->brush = clone_brush;

157 158
    *pen = gp_pen;

159 160
    TRACE("<-- %p\n", *pen);

161 162 163 164 165
    return Ok;
}

GpStatus WINGDIPAPI GdipDeletePen(GpPen *pen)
{
166 167
    TRACE("(%p)\n", pen);

168
    if(!pen)    return InvalidParameter;
169

170
    GdipDeleteBrush(pen->brush);
171 172
    GdipDeleteCustomLineCap(pen->customstart);
    GdipDeleteCustomLineCap(pen->customend);
173
    GdipFree(pen->dashes);
174 175 176 177
    GdipFree(pen);

    return Ok;
}
178

179 180
GpStatus WINGDIPAPI GdipGetPenBrushFill(GpPen *pen, GpBrush **brush)
{
181 182
    TRACE("(%p, %p)\n", pen, brush);

183 184 185 186 187 188
    if(!pen || !brush)
        return InvalidParameter;

    return GdipCloneBrush(pen->brush, brush);
}

189 190
GpStatus WINGDIPAPI GdipGetPenColor(GpPen *pen, ARGB *argb)
{
191 192
    TRACE("(%p, %p)\n", pen, argb);

193 194 195
    if(!pen || !argb)
        return InvalidParameter;

196 197 198 199
    if(pen->brush->bt != BrushTypeSolidColor)
        return NotImplemented;

    return GdipGetSolidFillColor(((GpSolidFill*)pen->brush), argb);
200 201
}

202 203
GpStatus WINGDIPAPI GdipGetPenCustomEndCap(GpPen *pen, GpCustomLineCap** customCap)
{
204 205
    TRACE("(%p, %p)\n", pen, customCap);

206 207 208 209 210 211 212 213 214 215 216 217 218
    if(!pen || !customCap)
        return InvalidParameter;

    if(!pen->customend){
        *customCap = NULL;
        return Ok;
    }

    return GdipCloneCustomLineCap(pen->customend, customCap);
}

GpStatus WINGDIPAPI GdipGetPenCustomStartCap(GpPen *pen, GpCustomLineCap** customCap)
{
219 220
    TRACE("(%p, %p)\n", pen, customCap);

221 222 223 224 225 226 227 228 229 230 231
    if(!pen || !customCap)
        return InvalidParameter;

    if(!pen->customstart){
        *customCap = NULL;
        return Ok;
    }

    return GdipCloneCustomLineCap(pen->customstart, customCap);
}

232 233
GpStatus WINGDIPAPI GdipGetPenDashArray(GpPen *pen, REAL *dash, INT count)
{
234 235
    TRACE("(%p, %p, %d)\n", pen, dash, count);

236 237 238 239 240 241 242 243 244 245 246 247
    if(!pen || !dash || count > pen->numdashes)
        return InvalidParameter;

    /* note: if you pass a negative value for count, it crashes native gdiplus. */
    if(count < 0)
        return GenericError;

    memcpy(dash, pen->dashes, count * sizeof(REAL));

    return Ok;
}

248 249
GpStatus WINGDIPAPI GdipGetPenDashCap197819(GpPen *pen, GpDashCap *dashCap)
{
250 251
    TRACE("(%p, %p)\n", pen, dashCap);

252 253 254 255 256 257 258 259
    if(!pen || !dashCap)
        return InvalidParameter;

    *dashCap = pen->dashcap;

    return Ok;
}

260 261
GpStatus WINGDIPAPI GdipGetPenDashCount(GpPen *pen, INT *count)
{
262 263
    TRACE("(%p, %p)\n", pen, count);

264 265 266 267 268 269 270 271
    if(!pen || !count)
        return InvalidParameter;

    *count = pen->numdashes;

    return Ok;
}

272 273
GpStatus WINGDIPAPI GdipGetPenDashOffset(GpPen *pen, REAL *offset)
{
274 275
    TRACE("(%p, %p)\n", pen, offset);

276 277 278 279 280 281 282 283
    if(!pen || !offset)
        return InvalidParameter;

    *offset = pen->offset;

    return Ok;
}

284 285
GpStatus WINGDIPAPI GdipGetPenDashStyle(GpPen *pen, GpDashStyle *dash)
{
286 287
    TRACE("(%p, %p)\n", pen, dash);

288 289 290 291 292 293 294 295
    if(!pen || !dash)
        return InvalidParameter;

    *dash = pen->dash;

    return Ok;
}

296 297
GpStatus WINGDIPAPI GdipGetPenEndCap(GpPen *pen, GpLineCap *endCap)
{
298 299
    TRACE("(%p, %p)\n", pen, endCap);

300 301 302 303 304 305 306 307
    if(!pen || !endCap)
        return InvalidParameter;

    *endCap = pen->endcap;

    return Ok;
}

308 309 310 311 312 313 314 315 316 317 318 319
GpStatus WINGDIPAPI GdipGetPenFillType(GpPen *pen, GpPenType* type)
{
    TRACE("(%p, %p)\n", pen, type);

    if(!pen || !type)
        return InvalidParameter;

    *type = bt_to_pt(pen->brush->bt);

    return Ok;
}

320 321
GpStatus WINGDIPAPI GdipGetPenLineJoin(GpPen *pen, GpLineJoin *lineJoin)
{
322 323
    TRACE("(%p, %p)\n", pen, lineJoin);

324 325 326 327 328 329 330 331
    if(!pen || !lineJoin)
        return InvalidParameter;

    *lineJoin = pen->join;

    return Ok;
}

332 333
GpStatus WINGDIPAPI GdipGetPenMode(GpPen *pen, GpPenAlignment *mode)
{
334 335
    TRACE("(%p, %p)\n", pen, mode);

336 337 338 339 340 341 342 343
    if(!pen || !mode)
        return InvalidParameter;

    *mode = pen->align;

    return Ok;
}

344 345
GpStatus WINGDIPAPI GdipGetPenMiterLimit(GpPen *pen, REAL *miterLimit)
{
346 347
    TRACE("(%p, %p)\n", pen, miterLimit);

348 349 350 351 352 353 354 355
    if(!pen || !miterLimit)
        return InvalidParameter;

    *miterLimit = pen->miterlimit;

    return Ok;
}

356 357
GpStatus WINGDIPAPI GdipGetPenStartCap(GpPen *pen, GpLineCap *startCap)
{
358 359
    TRACE("(%p, %p)\n", pen, startCap);

360 361 362 363 364 365 366 367
    if(!pen || !startCap)
        return InvalidParameter;

    *startCap = pen->startcap;

    return Ok;
}

368 369
GpStatus WINGDIPAPI GdipGetPenUnit(GpPen *pen, GpUnit *unit)
{
370 371
    TRACE("(%p, %p)\n", pen, unit);

372 373 374 375 376 377 378 379
    if(!pen || !unit)
        return InvalidParameter;

    *unit = pen->unit;

    return Ok;
}

380 381
GpStatus WINGDIPAPI GdipGetPenWidth(GpPen *pen, REAL *width)
{
382 383
    TRACE("(%p, %p)\n", pen, width);

384 385 386 387 388 389 390 391
    if(!pen || !width)
        return InvalidParameter;

    *width = pen->width;

    return Ok;
}

392 393 394 395
GpStatus WINGDIPAPI GdipResetPenTransform(GpPen *pen)
{
    static int calls;

396 397
    TRACE("(%p)\n", pen);

398 399 400 401 402 403 404 405 406
    if(!pen)
        return InvalidParameter;

    if(!(calls++))
        FIXME("(%p) stub\n", pen);

    return NotImplemented;
}

407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
GpStatus WINGDIPAPI GdipSetPenTransform(GpPen *pen, GpMatrix *matrix)
{
    static int calls;

    TRACE("(%p,%p)\n", pen, matrix);

    if(!pen || !matrix)
        return InvalidParameter;

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

    return NotImplemented;
}

422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
GpStatus WINGDIPAPI GdipGetPenTransform(GpPen *pen, GpMatrix *matrix)
{
    static int calls;

    TRACE("(%p,%p)\n", pen, matrix);

    if(!pen || !matrix)
        return InvalidParameter;

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

    return NotImplemented;
}

437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
GpStatus WINGDIPAPI GdipTranslatePenTransform(GpPen *pen, REAL dx, REAL dy, GpMatrixOrder order)
{
    static int calls;

    TRACE("(%p,%0.2f,%0.2f,%u)\n", pen, dx, dy, order);

    if(!pen)
        return InvalidParameter;

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

    return NotImplemented;
}

452 453 454 455
GpStatus WINGDIPAPI GdipScalePenTransform(GpPen *pen, REAL sx, REAL sy, GpMatrixOrder order)
{
    static int calls;

456 457
    TRACE("(%p,%0.2f,%0.2f,%u)\n", pen, sx, sy, order);

458 459 460 461 462 463 464 465 466
    if(!pen)
        return InvalidParameter;

    if(!(calls++))
        FIXME("(%p, %.2f, %.2f, %d) stub\n", pen, sx, sy, order);

    return NotImplemented;
}

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
GpStatus WINGDIPAPI GdipRotatePenTransform(GpPen *pen, REAL angle, GpMatrixOrder order)
{
    static int calls;

    TRACE("(%p,%0.2f,%u)\n", pen, angle, order);

    if(!pen)
        return InvalidParameter;

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

    return NotImplemented;
}

482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
GpStatus WINGDIPAPI GdipMultiplyPenTransform(GpPen *pen, GDIPCONST GpMatrix *matrix,
    GpMatrixOrder order)
{
    static int calls;

    TRACE("(%p,%p,%u)\n", pen, matrix, order);

    if(!pen)
        return InvalidParameter;

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

    return NotImplemented;
}

498 499
GpStatus WINGDIPAPI GdipSetPenBrushFill(GpPen *pen, GpBrush *brush)
{
500 501
    TRACE("(%p, %p)\n", pen, brush);

502 503 504 505
    if(!pen || !brush)
        return InvalidParameter;

    GdipDeleteBrush(pen->brush);
506
    return GdipCloneBrush(brush, &pen->brush);
507 508
}

509 510
GpStatus WINGDIPAPI GdipSetPenColor(GpPen *pen, ARGB argb)
{
511 512
    TRACE("(%p, %x)\n", pen, argb);

513 514 515 516 517 518 519 520 521
    if(!pen)
        return InvalidParameter;

    if(pen->brush->bt != BrushTypeSolidColor)
        return NotImplemented;

    return GdipSetSolidFillColor(((GpSolidFill*)pen->brush), argb);
}

522 523 524 525 526 527 528 529 530 531
GpStatus WINGDIPAPI GdipGetPenCompoundCount(GpPen *pen, INT *count)
{
    FIXME("(%p, %p): stub\n", pen, count);

    if (!pen || !count)
        return InvalidParameter;

    return NotImplemented;
}

532 533 534
GpStatus WINGDIPAPI GdipSetPenCompoundArray(GpPen *pen, GDIPCONST REAL *dash,
    INT count)
{
535
    FIXME("(%p, %p, %i): stub\n", pen, dash, count);
536 537 538 539 540 541 542

    if (!pen || !dash || count < 2 || count%2 == 1)
        return InvalidParameter;

    return NotImplemented;
}

543 544 545 546 547
GpStatus WINGDIPAPI GdipSetPenCustomEndCap(GpPen *pen, GpCustomLineCap* customCap)
{
    GpCustomLineCap * cap;
    GpStatus ret;

548 549
    TRACE("(%p, %p)\n", pen, customCap);

550 551
    /* native crashes on pen == NULL, customCap != NULL */
    if(!customCap) return InvalidParameter;
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566

    if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
        GdipDeleteCustomLineCap(pen->customend);
        pen->endcap = LineCapCustom;
        pen->customend = cap;
    }

    return ret;
}

GpStatus WINGDIPAPI GdipSetPenCustomStartCap(GpPen *pen, GpCustomLineCap* customCap)
{
    GpCustomLineCap * cap;
    GpStatus ret;

567 568
    TRACE("(%p, %p)\n", pen, customCap);

569 570
    /* native crashes on pen == NULL, customCap != NULL */
    if(!customCap) return InvalidParameter;
571 572 573 574 575 576 577 578 579 580

    if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
        GdipDeleteCustomLineCap(pen->customstart);
        pen->startcap = LineCapCustom;
        pen->customstart = cap;
    }

    return ret;
}

581 582 583
GpStatus WINGDIPAPI GdipSetPenDashArray(GpPen *pen, GDIPCONST REAL *dash,
    INT count)
{
584 585 586
    INT i;
    REAL sum = 0;

587 588
    TRACE("(%p, %p, %d)\n", pen, dash, count);

589 590 591
    if(!pen || !dash)
        return InvalidParameter;

592 593 594
    if(count <= 0)
        return OutOfMemory;

595 596 597 598 599 600 601 602 603
    for(i = 0; i < count; i++){
        sum += dash[i];
        if(dash[i] < 0.0)
            return InvalidParameter;
    }

    if(sum == 0.0 && count)
        return InvalidParameter;

604 605 606 607 608 609 610 611 612 613
    GdipFree(pen->dashes);
    pen->dashes = NULL;

    if(count > 0)
        pen->dashes = GdipAlloc(count * sizeof(REAL));
    if(!pen->dashes){
        pen->numdashes = 0;
        return OutOfMemory;
    }

614
    GdipSetPenDashStyle(pen, DashStyleCustom);
615 616 617 618 619 620
    memcpy(pen->dashes, dash, count * sizeof(REAL));
    pen->numdashes = count;

    return Ok;
}

621 622
GpStatus WINGDIPAPI GdipSetPenDashCap197819(GpPen *pen, GpDashCap dashCap)
{
623 624
    TRACE("(%p, %d)\n", pen, dashCap);

625 626 627 628 629 630 631 632
    if(!pen)
        return InvalidParameter;

    pen->dashcap = dashCap;

    return Ok;
}

633 634 635
/* FIXME: dash offset not used */
GpStatus WINGDIPAPI GdipSetPenDashOffset(GpPen *pen, REAL offset)
{
636 637
    TRACE("(%p, %.2f)\n", pen, offset);

638 639 640 641 642 643 644 645
    if(!pen)
        return InvalidParameter;

    pen->offset = offset;

    return Ok;
}

646 647
GpStatus WINGDIPAPI GdipSetPenDashStyle(GpPen *pen, GpDashStyle dash)
{
648 649
    TRACE("(%p, %d)\n", pen, dash);

650 651 652
    if(!pen)
        return InvalidParameter;

653 654 655 656 657 658
    if(dash != DashStyleCustom){
        GdipFree(pen->dashes);
        pen->dashes = NULL;
        pen->numdashes = 0;
    }

659 660 661 662 663 664 665 666
    pen->dash = dash;
    pen->style &= ~(PS_ALTERNATE | PS_SOLID | PS_DASH | PS_DOT | PS_DASHDOT |
                    PS_DASHDOTDOT | PS_NULL | PS_USERSTYLE | PS_INSIDEFRAME);
    pen->style |= gdip_to_gdi_dash(dash);

    return Ok;
}

667 668
GpStatus WINGDIPAPI GdipSetPenEndCap(GpPen *pen, GpLineCap cap)
{
669 670
    TRACE("(%p, %d)\n", pen, cap);

671 672
    if(!pen)    return InvalidParameter;

673 674 675
    /* The old custom cap gets deleted even if the new style is LineCapCustom. */
    GdipDeleteCustomLineCap(pen->customend);
    pen->customend = NULL;
676 677 678 679
    pen->endcap = cap;

    return Ok;
}
680

681 682 683 684
/* FIXME: startcap, dashcap not used. */
GpStatus WINGDIPAPI GdipSetPenLineCap197819(GpPen *pen, GpLineCap start,
    GpLineCap end, GpDashCap dash)
{
685 686
    TRACE("%p, %d, %d, %d)\n", pen, start, end, dash);

687 688 689
    if(!pen)
        return InvalidParameter;

690 691 692 693 694
    GdipDeleteCustomLineCap(pen->customend);
    GdipDeleteCustomLineCap(pen->customstart);
    pen->customend = NULL;
    pen->customstart = NULL;

695 696 697 698 699 700 701
    pen->startcap = start;
    pen->endcap = end;
    pen->dashcap = dash;

    return Ok;
}

702 703 704 705
/* FIXME: Miter line joins behave a bit differently than they do in windows.
 * Both kinds of miter joins clip if the angle is less than 11 degrees. */
GpStatus WINGDIPAPI GdipSetPenLineJoin(GpPen *pen, GpLineJoin join)
{
706 707
    TRACE("(%p, %d)\n", pen, join);

708 709 710 711 712 713 714 715
    if(!pen)    return InvalidParameter;

    pen->join = join;
    pen->style &= ~(PS_JOIN_ROUND | PS_JOIN_BEVEL | PS_JOIN_MITER);
    pen->style |= gdip_to_gdi_join(join);

    return Ok;
}
716 717 718

GpStatus WINGDIPAPI GdipSetPenMiterLimit(GpPen *pen, REAL limit)
{
719 720
    TRACE("(%p, %.2f)\n", pen, limit);

721 722 723 724 725 726 727
    if(!pen)
        return InvalidParameter;

    pen->miterlimit = limit;

    return Ok;
}
728 729 730

GpStatus WINGDIPAPI GdipSetPenStartCap(GpPen *pen, GpLineCap cap)
{
731 732
    TRACE("(%p, %d)\n", pen, cap);

733 734 735 736 737 738 739 740
    if(!pen)    return InvalidParameter;

    GdipDeleteCustomLineCap(pen->customstart);
    pen->customstart = NULL;
    pen->startcap = cap;

    return Ok;
}
741 742 743

GpStatus WINGDIPAPI GdipSetPenWidth(GpPen *pen, REAL width)
{
744 745
    TRACE("(%p, %.2f)\n", pen, width);

746 747 748 749 750 751
    if(!pen)    return InvalidParameter;

    pen->width = width;

    return Ok;
}
752

753
GpStatus WINGDIPAPI GdipSetPenMode(GpPen *pen, GpPenAlignment mode)
754
{
755 756
    TRACE("(%p, %d)\n", pen, mode);

757 758
    if(!pen)    return InvalidParameter;

759
    pen->align = mode;
760 761 762

    return Ok;
}