qwmf.cpp 38.9 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1 2
/* Windows Meta File Loader/Painter Class Implementation
 *
3 4
 * SPDX-FileCopyrightText: 1998 Stefan Taferner
 * SPDX-FileCopyrightText: 2002 thierry lorthiois
Laurent Montel's avatar
Laurent Montel committed
5
 *
6
 * SPDX-License-Identifier: GPL-2.0-or-later
Laurent Montel's avatar
Laurent Montel committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 */

#include <config-ktnef.h> // WORDS_BIGENDIAN

#include <math.h>
#include <assert.h>

#include <QImage>
#include <QPainter>
#include <QPainterPath>
#include <QDataStream>
#include <QBuffer>
#include <QFile>
#include <QPolygon>

#include "ktnef_debug.h"

bool qwmfDebug = false;

#include "qwmf.h"
#include "wmfstruct.h"
#include "metafuncs.h"

#define QWMF_DEBUG  0

class WmfCmd
{
public:
    ~WmfCmd()
    {
        delete next;
    }
Laurent Montel's avatar
Laurent Montel committed
39

Laurent Montel's avatar
Laurent Montel committed
40 41
    WmfCmd *next;
    unsigned short funcIndex;
Laurent Montel's avatar
Laurent Montel committed
42
    long numParm;
Laurent Montel's avatar
Laurent Montel committed
43 44 45 46 47 48
    short *parm;
};

class WinObjHandle
{
public:
Laurent Montel's avatar
Laurent Montel committed
49 50 51 52
    virtual ~WinObjHandle()
    {
    }

Laurent Montel's avatar
Laurent Montel committed
53 54 55
    virtual void apply(QPainter &p) = 0;
};

Laurent Montel's avatar
Laurent Montel committed
56
class WinObjBrushHandle : public WinObjHandle
Laurent Montel's avatar
Laurent Montel committed
57 58
{
public:
59
    void apply(QPainter &p) override;
Laurent Montel's avatar
Laurent Montel committed
60
    QBrush brush;
Laurent Montel's avatar
Laurent Montel committed
61 62 63
    virtual ~WinObjBrushHandle()
    {
    }
Laurent Montel's avatar
Laurent Montel committed
64 65
};

Laurent Montel's avatar
Laurent Montel committed
66
class WinObjPenHandle : public WinObjHandle
Laurent Montel's avatar
Laurent Montel committed
67 68
{
public:
69
    void apply(QPainter &p) override;
Laurent Montel's avatar
Laurent Montel committed
70
    QPen pen;
Laurent Montel's avatar
Laurent Montel committed
71 72 73
    virtual ~WinObjPenHandle()
    {
    }
Laurent Montel's avatar
Laurent Montel committed
74 75
};

Laurent Montel's avatar
Laurent Montel committed
76
class WinObjPatternBrushHandle : public WinObjHandle
Laurent Montel's avatar
Laurent Montel committed
77 78
{
public:
79
    void apply(QPainter &p) override;
Laurent Montel's avatar
Laurent Montel committed
80 81
    QBrush brush;
    QImage image;
Laurent Montel's avatar
Laurent Montel committed
82 83 84
    virtual ~WinObjPatternBrushHandle()
    {
    }
Laurent Montel's avatar
Laurent Montel committed
85 86
};

Laurent Montel's avatar
Laurent Montel committed
87
class WinObjFontHandle : public WinObjHandle
Laurent Montel's avatar
Laurent Montel committed
88 89
{
public:
90
    void apply(QPainter &p) override;
Laurent Montel's avatar
Laurent Montel committed
91 92
    QFont font;
    int rotation;
Laurent Montel's avatar
Laurent Montel committed
93 94 95
    virtual ~WinObjFontHandle()
    {
    }
Laurent Montel's avatar
Laurent Montel committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
};

void WinObjBrushHandle::apply(QPainter &p)
{
    p.setBrush(brush);
}

void WinObjPenHandle::apply(QPainter &p)
{
    p.setPen(pen);
}

void WinObjPatternBrushHandle::apply(QPainter &p)
{
    p.setBrush(brush);
}

void WinObjFontHandle::apply(QPainter &p)
{
    p.setFont(font);
}

#define MAX_OBJHANDLE 64

//-----------------------------------------------------------------------------
QWinMetaFile::QWinMetaFile()
{
Laurent Montel's avatar
Laurent Montel committed
123
    mValid = false;
Laurent Montel's avatar
Laurent Montel committed
124 125
    mFirstCmd = nullptr;
    mObjHandleTab = nullptr;
Laurent Montel's avatar
Laurent Montel committed
126
    mDpi = 1000;
Laurent Montel's avatar
Laurent Montel committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
}

//-----------------------------------------------------------------------------
QWinMetaFile::~QWinMetaFile()
{
    delete mFirstCmd;
    if (mObjHandleTab) {
        delete[] mObjHandleTab;
    }
}

//-----------------------------------------------------------------------------
bool QWinMetaFile::load(const QString &filename)
{
    QFile file(filename);

    if (!file.exists()) {
        qCDebug(KTNEFAPPS_LOG) << "File" << QFile::encodeName(filename) << " does not exist";
        return false;
    }

    if (!file.open(QIODevice::ReadOnly)) {
        qCDebug(KTNEFAPPS_LOG) << "Cannot open file" << QFile::encodeName(filename);
        return false;
    }

    QByteArray ba = file.readAll();
    file.close();

    QBuffer buffer(&ba);
    buffer.open(QIODevice::ReadOnly);
    return load(buffer);
}

//-----------------------------------------------------------------------------
bool QWinMetaFile::load(QBuffer &buffer)
{
    QDataStream st;
    WmfEnhMetaHeader eheader;
    WmfMetaHeader header;
    WmfPlaceableHeader pheader;
    int filePos, idx, i;
    WmfCmd *cmd, *last;
    DWORD rdSize;
    WORD rdFunc;

    mTextAlign = 0;
    mRotation = 0;
    mTextColor = Qt::black;
    if (mFirstCmd) {
        delete mFirstCmd;
    }
Laurent Montel's avatar
Laurent Montel committed
179
    mFirstCmd = nullptr;
Laurent Montel's avatar
Laurent Montel committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

    st.setDevice(&buffer);
    st.setByteOrder(QDataStream::LittleEndian);   // Great, I love Qt !

    //----- Read placeable metafile header
    st >> pheader.key;
    mIsPlaceable = (pheader.key == (DWORD)APMHEADER_KEY);
    if (mIsPlaceable) {
        st >> pheader.hmf;
        st >> pheader.bbox.left;
        st >> pheader.bbox.top;
        st >> pheader.bbox.right;
        st >> pheader.bbox.bottom;
        st >> pheader.inch;
        st >> pheader.reserved;
        st >> pheader.checksum;
Laurent Montel's avatar
Laurent Montel committed
196
        const WORD checksum = calcCheckSum(&pheader);
Laurent Montel's avatar
Laurent Montel committed
197 198 199 200 201 202 203 204 205 206 207
        if (pheader.checksum != checksum) {
            mIsPlaceable = false;
        }

        mDpi = pheader.inch;
        mBBox.setLeft(pheader.bbox.left);
        mBBox.setTop(pheader.bbox.top);
        mBBox.setRight(pheader.bbox.right);
        mBBox.setBottom(pheader.bbox.bottom);
        mHeaderBoundingBox = mBBox;
        if (QWMF_DEBUG) {
208
            qCDebug(KTNEFAPPS_LOG) << "\n-------------------------------------------------";
Laurent Montel's avatar
Laurent Montel committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
            qCDebug(KTNEFAPPS_LOG) << "WMF Placeable Header (" << static_cast<int>(sizeof(pheader)) << "):";
            qCDebug(KTNEFAPPS_LOG) << "  bbox=(" << mBBox.left() << ";" << mBBox.top() << ";" << mBBox.width()
                                   << "; " << mBBox.height() << ")";
            qCDebug(KTNEFAPPS_LOG) << "  inch=" << pheader.inch;
            qCDebug(KTNEFAPPS_LOG) << "  checksum=" << pheader.checksum << "("
                                   << (pheader.checksum == checksum ? "ok" : "wrong") << " )";
        }
    } else {
        buffer.reset();
    }

    //----- Read as enhanced metafile header
    filePos = buffer.pos();
    st >> eheader.iType;
    st >> eheader.nSize;
    st >> eheader.rclBounds.left;
    st >> eheader.rclBounds.top;
    st >> eheader.rclBounds.right;
    st >> eheader.rclBounds.bottom;
    st >> eheader.rclFrame.left;
    st >> eheader.rclFrame.top;
    st >> eheader.rclFrame.right;
    st >> eheader.rclFrame.bottom;
    st >> eheader.dSignature;
    mIsEnhanced = (eheader.dSignature == ENHMETA_SIGNATURE);
    if (mIsEnhanced) { // is it really enhanced ?
        st >> eheader.nVersion;
        st >> eheader.nBytes;
        st >> eheader.nRecords;
        st >> eheader.nHandles;
        st >> eheader.sReserved;
        st >> eheader.nDescription;
        st >> eheader.offDescription;
        st >> eheader.nPalEntries;
        st >> eheader.szlDevice.width;
        st >> eheader.szlDevice.height;
        st >> eheader.szlMillimeters.width;
        st >> eheader.szlMillimeters.height;

        if (QWMF_DEBUG) {
249
            qCDebug(KTNEFAPPS_LOG) << "\n-------------------------------------------------";
Laurent Montel's avatar
Laurent Montel committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
            qCDebug(KTNEFAPPS_LOG) << "WMF Extended Header:";
            qCDebug(KTNEFAPPS_LOG) << "  iType=" << eheader.iType;
            qCDebug(KTNEFAPPS_LOG) << "  nSize=" << eheader.nSize;
            qCDebug(KTNEFAPPS_LOG) << "  rclBounds=(" << eheader.rclBounds.left << ";" << eheader.rclBounds.top << ";"
                                   << eheader.rclBounds.right << "; " << eheader.rclBounds.bottom << ")";
            qCDebug(KTNEFAPPS_LOG) << "  rclFrame=(" << eheader.rclFrame.left << ";" << eheader.rclFrame.top << ";"
                                   << eheader.rclFrame.right << "; " << eheader.rclFrame.bottom << ")";
            qCDebug(KTNEFAPPS_LOG) << "  nBytes=" << eheader.nBytes;
            qCDebug(KTNEFAPPS_LOG) << "\nNOT YET IMPLEMENTED, SORRY.";
        }
    } else { // no, not enhanced
        //----- Read as standard metafile header
        buffer.seek(filePos);
        st >> header.mtType;
        st >> header.mtHeaderSize;
        st >> header.mtVersion;
        st >> header.mtSize;
        st >> header.mtNoObjects;
        st >> header.mtMaxRecord;
        st >> header.mtNoParameters;
        if (QWMF_DEBUG) {
            qCDebug(KTNEFAPPS_LOG) << "WMF Header:" << "mtSize=" << header.mtSize;
        }
    }

    //----- Test header validity
    mValid = ((header.mtHeaderSize == 9) && (header.mtNoParameters == 0)) || mIsEnhanced || mIsPlaceable;
    if (mValid) {
        //----- Read Metafile Records
Laurent Montel's avatar
Laurent Montel committed
279
        last = nullptr;
Laurent Montel's avatar
Laurent Montel committed
280 281 282 283 284 285 286 287
        rdFunc = -1;
        while (!st.atEnd() && (rdFunc != 0)) {
            st >> rdSize;
            st >> rdFunc;
            idx = findFunc(rdFunc);
            rdSize -= 3;

            cmd = new WmfCmd;
Laurent Montel's avatar
Laurent Montel committed
288
            cmd->next = nullptr;
Laurent Montel's avatar
Laurent Montel committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
            if (last) {
                last->next = cmd;
            } else {
                mFirstCmd = cmd;
            }

            cmd->funcIndex = idx;
            cmd->numParm = rdSize;
            cmd->parm = new WORD[ rdSize ];
            last = cmd;

            for (i = 0; i < rdSize && !st.atEnd(); ++i) {
                st >> cmd->parm[ i ];
            }

            if (rdFunc == 0x020B) {           // SETWINDOWORG: dimensions
                mBBox.setLeft(cmd->parm[ 1 ]);
                mBBox.setTop(cmd->parm[ 0 ]);
            }
            if (rdFunc == 0x020C) {           // SETWINDOWEXT: dimensions
                mBBox.setWidth(cmd->parm[ 1 ]);
                mBBox.setHeight(cmd->parm[ 0 ]);
            }

            if (i < rdSize) {
                qCDebug(KTNEFAPPS_LOG) << "WMF : file truncated !";
                return false;
            }
        }
        //----- Test records validities
        mValid = (rdFunc == 0) && (mBBox.width() != 0) && (mBBox.height() != 0);
        if (!mValid) {
            qCDebug(KTNEFAPPS_LOG) << "WMF : incorrect file format !";
        }
    } else {
        qCDebug(KTNEFAPPS_LOG) << "WMF Header : incorrect header !";
    }

    buffer.close();
    return mValid;
}

//-----------------------------------------------------------------------------
bool QWinMetaFile::paint(QPaintDevice *aTarget, bool absolute)
{
    int idx, i;
    WmfCmd *cmd;

    if (!mValid) {
        return false;
    }

Laurent Montel's avatar
Laurent Montel committed
341
    assert(aTarget != nullptr);
Laurent Montel's avatar
Laurent Montel committed
342 343 344 345 346 347 348
    if (mPainter.isActive()) {
        return false;
    }

    if (mObjHandleTab) {
        delete[] mObjHandleTab;
    }
Laurent Montel's avatar
Laurent Montel committed
349
    mObjHandleTab = new WinObjHandle * [ MAX_OBJHANDLE ];
Yuri Chornoivan's avatar
Yuri Chornoivan committed
350
    for (i = MAX_OBJHANDLE - 1; i >= 0; --i) {
Laurent Montel's avatar
Laurent Montel committed
351
        mObjHandleTab[ i ] = nullptr;
Laurent Montel's avatar
Laurent Montel committed
352 353
    }

354
    mPainter.resetTransform();
Laurent Montel's avatar
Laurent Montel committed
355 356 357 358
    mWinding = false;
    mAbsoluteCoord = absolute;

    mPainter.begin(aTarget);
Laurent Montel's avatar
Laurent Montel committed
359
    if (QWMF_DEBUG) {
Laurent Montel's avatar
Laurent Montel committed
360 361 362 363 364 365 366 367 368 369 370 371 372
        qCDebug(KTNEFAPPS_LOG) << "Bounding box :" << mBBox.left()
                               << " " << mBBox.top() << " " << mBBox.right() << " " << mBBox.bottom();
    }

    if (mAbsoluteCoord) {
        mPainter.setWindow(mBBox.top(), mBBox.left(), mBBox.width(), mBBox.height());
    }
    mInternalWorldMatrix.reset();

    for (cmd = mFirstCmd; cmd; cmd = cmd->next) {
        idx = cmd->funcIndex;
        (this->*metaFuncTab[ idx ].method)(cmd->numParm, cmd->parm);

Laurent Montel's avatar
Laurent Montel committed
373
        if (QWMF_DEBUG) {
Laurent Montel's avatar
Laurent Montel committed
374
            QString str, param;
Laurent Montel's avatar
Laurent Montel committed
375
            if (metaFuncTab[ idx ].name == nullptr) {
Laurent Montel's avatar
Laurent Montel committed
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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
                str += QLatin1String("UNKNOWN ");
            }
            if (metaFuncTab[ idx ].method == &QWinMetaFile::noop) {
                str += QLatin1String("UNIMPLEMENTED ");
            }
            str += QLatin1String(metaFuncTab[ idx ].name);
            str += QLatin1String(" : ");

            for (i = 0; i < cmd->numParm; ++i) {
                param.setNum(cmd->parm[ i ]);
                str += param;
                str += QLatin1Char(' ');
            }
            qCDebug(KTNEFAPPS_LOG) << str;
        }
    }
    /*
        // TODO: cleanup this code when QPicture::setBoundingBox() is possible in KOClipart (QT31)
        // because actually QPicture::boundingBox() != mBBox()
        mWindowsCoord += 1;
        if ( mWindowsCoord == 2 )  {
            qCDebug(KTNEFAPPS_LOG) <<"DRAW ANGLES";
            mPainter.setPen( Qt::white );
            mPainter.drawPoint( mBBox.left(), mBBox.top()  );
            mPainter.drawPoint( mBBox.right(), mBBox.bottom() );
        }
    */
    mPainter.end();
    return true;
}

//----------------s-------------------------------------------------------------
// Metafile painter methods
//-----------------------------------------------------------------------------
void QWinMetaFile::setWindowOrg(long, short *parm)
{
    if (mAbsoluteCoord) {
        QRect r = mPainter.window();
        mPainter.setWindow(parm[ 1 ], parm[ 0 ], r.width(), r.height());
    } else {
        double dx = mInternalWorldMatrix.dx();
        double dy = mInternalWorldMatrix.dy();

        mInternalWorldMatrix.translate(-dx, -dy);
        mInternalWorldMatrix.translate(-parm[ 1 ], -parm[ 0 ]);
        mPainter.translate(-dx, -dy);
        mPainter.translate(-parm[ 1 ], -parm[ 0 ]);
    }
}

//-----------------------------------------------------------------------------
void QWinMetaFile::setWindowExt(long, short *parm)
{
    // negative value allowed for width and height : QABS() forbidden
    if (mAbsoluteCoord) {
        QRect r = mPainter.window();
        mPainter.setWindow(r.left(), r.top(), parm[ 1 ], parm[ 0 ]);
    } else {
        if ((parm[ 0 ] != 0) && (parm[ 1 ] != 0)) {
            QRect r = mPainter.window();
            double dx = mInternalWorldMatrix.dx();
            double dy = mInternalWorldMatrix.dy();
            double sx = mInternalWorldMatrix.m11();
            double sy = mInternalWorldMatrix.m22();

            mInternalWorldMatrix.translate(-dx, -dy);
            mInternalWorldMatrix.scale(1 / sx, 1 / sy);
            mPainter.translate(-dx, -dy);
            mPainter.scale(1 / sx, 1 / sy);

            sx = (double)r.width() / (double)parm[ 1 ];
            sy = (double)r.height() / (double)parm[ 0 ];

            mInternalWorldMatrix.scale(sx, sy);
            mInternalWorldMatrix.translate(dx, dy);
            mPainter.scale(sx, sy);
            mPainter.translate(dx, dy);
        }
    }
}

//-----------------------------------------------------------------------------
// Drawing
//-----------------------------------------------------------------------------
void QWinMetaFile::lineTo(long, short *parm)
{
    mPainter.drawLine(mLastPos, QPoint(parm[1], parm[0]));
}

//-----------------------------------------------------------------------------
void QWinMetaFile::moveTo(long, short *parm)
{
    mLastPos = QPoint(parm[ 1 ], parm[ 0 ]);
}

//-----------------------------------------------------------------------------
void QWinMetaFile::ellipse(long, short *parm)
{
    mPainter.drawEllipse(parm[ 3 ], parm[ 2 ], parm[ 1 ] - parm[ 3 ], parm[ 0 ] - parm[ 2 ]);
}

//-----------------------------------------------------------------------------
void QWinMetaFile::polygon(long, short *parm)
{
    QPolygon *pa;              // causing a memleck ???

    pa = pointArray(parm[ 0 ], &parm[ 1 ]);
    if (mWinding) {
        mPainter.drawPolygon(*pa, Qt::WindingFill);
    } else {
        mPainter.drawPolygon(*pa, Qt::OddEvenFill);
    }

    delete pa;
}

//-----------------------------------------------------------------------------
void QWinMetaFile::polyPolygon(long, short *parm)
{
    QRegion region;
Laurent Montel's avatar
Laurent Montel committed
496
    int i, j, startPolygon;
Laurent Montel's avatar
Laurent Montel committed
497 498 499 500 501 502 503 504

    mPainter.save();

    // define clipping region
    QRect win = bbox();
    startPolygon = 1 + parm[ 0 ];
    for (i = 0; i < parm[ 0 ]; ++i) {
        QPolygon pa1(parm[ 1 + i ]);
Yuri Chornoivan's avatar
Yuri Chornoivan committed
505
        for (j = 0; j < parm[ 1 + i ]; ++j) {
Laurent Montel's avatar
Laurent Montel committed
506 507 508 509
            pa1.setPoint(j, parm[ startPolygon ], parm[ startPolygon + 1 ]);
            startPolygon += 2;
        }
        QRegion r(pa1);
Laurent Montel's avatar
Laurent Montel committed
510
        region = region.xored(r);
Laurent Montel's avatar
Laurent Montel committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
    }
    mPainter.setClipRegion(region);

    // fill polygons
    mPainter.fillRect(win.left(), win.top(), win.width(), win.height(), mPainter.brush());

    // draw polygon's border if necessary
    if (mPainter.pen().style() != Qt::NoPen) {
        mPainter.setClipping(false);
        mPainter.setBrush(Qt::NoBrush);

        QPolygon *pa;
        int idxPolygon = 1 + parm[ 0 ];
        for (i = 0; i < parm[ 0 ]; ++i) {
            pa = pointArray(parm[ 1 + i ], &parm[ idxPolygon ]);
            mPainter.drawPolygon(*pa);
            idxPolygon += parm[ 1 + i ] * 2;
        }
    }

    mPainter.restore();
}

//-----------------------------------------------------------------------------
void QWinMetaFile::polyline(long, short *parm)
{
    QPolygon *pa;

    pa = pointArray(parm[ 0 ], &parm[ 1 ]);
    mPainter.drawPolyline(*pa);
}

//-----------------------------------------------------------------------------
void QWinMetaFile::rectangle(long, short *parm)
{
    mPainter.drawRect(parm[ 3 ], parm[ 2 ], parm[ 1 ] - parm[ 3 ], parm[ 0 ] - parm[ 2 ]);
}

//-----------------------------------------------------------------------------
void QWinMetaFile::roundRect(long, short *parm)
{
    int xRnd = 0, yRnd = 0;

    // convert (xRound, yRound) in percentage
    if ((parm[ 3 ] - parm[ 5 ]) != 0) {
        xRnd = (parm[ 1 ] * 100) / (parm[ 3 ] - parm[ 5 ]);
    }
    if ((parm[ 2 ] - parm[ 4 ]) != 0) {
        yRnd = (parm[ 0 ] * 100) / (parm[ 2 ] - parm[ 4 ]);
    }

562 563
    mPainter.drawRoundedRect(parm[ 5 ], parm[ 4 ], parm[ 3 ] - parm[ 5 ], parm[ 2 ] - parm[ 4 ],
                             xRnd, yRnd, Qt::RelativeSize);
Laurent Montel's avatar
Laurent Montel committed
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
}

//-----------------------------------------------------------------------------
void QWinMetaFile::arc(long, short *parm)
{
    int xCenter, yCenter, angleStart, aLength;

    xCenter = parm[ 7 ] + ((parm[ 5 ] - parm[ 7 ]) / 2);
    yCenter = parm[ 6 ] + ((parm[ 4 ] - parm[ 6 ]) / 2);

    xyToAngle(parm[ 3 ] - xCenter, yCenter - parm[ 2 ], parm[ 1 ] - xCenter, yCenter - parm[ 0 ], angleStart, aLength);

    mPainter.drawArc(parm[ 7 ], parm[ 6 ], parm[ 5 ] - parm[ 7 ], parm[ 4 ] - parm[ 6 ], angleStart, aLength);
}

//-----------------------------------------------------------------------------
void QWinMetaFile::chord(long, short *parm)
{
    int xCenter, yCenter, angleStart, aLength;

    xCenter = parm[ 7 ] + ((parm[ 5 ] - parm[ 7 ]) / 2);
    yCenter = parm[ 6 ] + ((parm[ 4 ] - parm[ 6 ]) / 2);

    xyToAngle(parm[ 3 ] - xCenter, yCenter - parm[ 2 ], parm[ 1 ] - xCenter, yCenter - parm[ 0 ], angleStart, aLength);

    mPainter.drawChord(parm[ 7 ], parm[ 6 ], parm[ 5 ] - parm[ 7 ], parm[ 4 ] - parm[ 6 ], angleStart, aLength);
}

//-----------------------------------------------------------------------------
void QWinMetaFile::pie(long, short *parm)
{
    int xCenter, yCenter, angleStart, aLength;

    xCenter = parm[ 7 ] + ((parm[ 5 ] - parm[ 7 ]) / 2);
    yCenter = parm[ 6 ] + ((parm[ 4 ] - parm[ 6 ]) / 2);

    xyToAngle(parm[ 3 ] - xCenter, yCenter - parm[ 2 ], parm[ 1 ] - xCenter, yCenter - parm[ 0 ], angleStart, aLength);

    mPainter.drawPie(parm[ 7 ], parm[ 6 ], parm[ 5 ] - parm[ 7 ], parm[ 4 ] - parm[ 6 ], angleStart, aLength);
}

//-----------------------------------------------------------------------------
void QWinMetaFile::setPolyFillMode(long, short *parm)
{
    mWinding = parm[ 0 ];
}

//-----------------------------------------------------------------------------
void QWinMetaFile::setBkColor(long, short *parm)
{
    mPainter.setBackground(QBrush(color(parm)));
}

//-----------------------------------------------------------------------------
void QWinMetaFile::setBkMode(long, short *parm)
{
    if (parm[ 0 ] == 1) {
        mPainter.setBackgroundMode(Qt::TransparentMode);
    } else {
        mPainter.setBackgroundMode(Qt::OpaqueMode);
    }
}

//-----------------------------------------------------------------------------
void QWinMetaFile::setPixel(long, short *parm)
{
    QPen pen = mPainter.pen();
    mPainter.setPen(color(parm));
    mPainter.drawPoint(parm[ 3 ], parm[ 2 ]);
    mPainter.setPen(pen);
}

//-----------------------------------------------------------------------------
void QWinMetaFile::setRop(long, short *parm)
{
    mPainter.setCompositionMode(winToQtComposition(parm[ 0 ]));
}

//-----------------------------------------------------------------------------
void QWinMetaFile::saveDC(long, short *)
{
    mPainter.save();
}

//-----------------------------------------------------------------------------
void QWinMetaFile::restoreDC(long, short *parm)
{
    for (int i = 0; i > parm[ 0 ]; i--) {
        mPainter.restore();
    }
}

//-----------------------------------------------------------------------------
void QWinMetaFile::intersectClipRect(long, short *parm)
{
    /*  TODO: better implementation : need QT 3.0.2
        QRegion region = mPainter.clipRegion();
        if ( region.isEmpty() )
            region = bbox();
    */
    QRegion region(bbox());

    QRegion newRegion(parm[ 3 ], parm[ 2 ], parm[ 1 ] - parm[ 3 ], parm[ 0 ] - parm[ 2 ]);
Laurent Montel's avatar
Laurent Montel committed
667
    region = region.intersected(newRegion);
Laurent Montel's avatar
Laurent Montel committed
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682

    mPainter.setClipRegion(region);
}

//-----------------------------------------------------------------------------
void QWinMetaFile::excludeClipRect(long, short *parm)
{
    /*  TODO: better implementation : need QT 3.0.2
        QRegion region = mPainter.clipRegion();
        if ( region.isEmpty() )
            region = bbox();
    */
    QRegion region(bbox());

    QRegion newRegion(parm[ 3 ], parm[ 2 ], parm[ 1 ] - parm[ 3 ], parm[ 0 ] - parm[ 2 ]);
Laurent Montel's avatar
Laurent Montel committed
683
    region = region.subtracted(newRegion);
Laurent Montel's avatar
Laurent Montel committed
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726

    mPainter.setClipRegion(region);
}

//-----------------------------------------------------------------------------
// Text
//-----------------------------------------------------------------------------
void QWinMetaFile::setTextColor(long, short *parm)
{
    mTextColor = color(parm);
}

//-----------------------------------------------------------------------------
void QWinMetaFile::setTextAlign(long, short *parm)
{
    mTextAlign = parm[ 0 ];
}

//-----------------------------------------------------------------------------
void QWinMetaFile::textOut(long num, short *parm)
{
    short *copyParm = new short[ num + 1 ];

    // re-order parameters
    int idxOffset = (parm[ 0 ] / 2) + 1 + (parm[ 0 ] & 1);
    copyParm[ 0 ] = parm[ idxOffset ];
    copyParm[ 1 ] = parm[ idxOffset + 1 ];
    copyParm[ 2 ] = parm[ 0 ];
    copyParm[ 3 ] = 0;
    memcpy(&copyParm[ 4 ], &parm[ 1 ], parm[ 0 ]);

    extTextOut(num + 1, copyParm);
    delete [] copyParm;
}

//-----------------------------------------------------------------------------
void QWinMetaFile::extTextOut(long num, short *parm)
{
    char *ptStr;
    int x, y, width, height;
    int idxOffset;

    if (parm[ 3 ] != 0) {       // ETO_CLIPPED flag add 4 parameters
Laurent Montel's avatar
Laurent Montel committed
727
        ptStr = (char *)&parm[ 8 ];
Laurent Montel's avatar
Laurent Montel committed
728
    } else {
Laurent Montel's avatar
Laurent Montel committed
729
        ptStr = (char *)&parm[ 4 ];
Laurent Montel's avatar
Laurent Montel committed
730 731 732 733 734
    }

    QByteArray text(ptStr, parm[ 2 ] + 1);

    QFontMetrics fm(mPainter.font());
Laurent Montel's avatar
Laurent Montel committed
735
    width = fm.boundingRect(QLatin1String(text)).width() + fm.descent();    // because fm.width(text) isn't rigth with Italic text
Laurent Montel's avatar
Laurent Montel committed
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
    height = fm.height();

    mPainter.save();

    if (mTextAlign & 0x01) {         // (left, top) position = current logical position
        x = mLastPos.x();
        y = mLastPos.y();
    } else {                          // (left, top) position = parameters
        x = parm[ 1 ];
        y = parm[ 0 ];
    }

    if (mRotation) {
        mPainter.translate(parm[ 1 ], parm[ 0 ]);
        mPainter.rotate(mRotation);
        mPainter.translate(-parm[ 1 ], -parm[ 0 ]);
    }

    // alignment
    if (mTextAlign & 0x06) {
        x -= (width / 2);
    }
    if (mTextAlign & 0x08) {
        y -= (height - fm.descent());
    }

    mPainter.setPen(mTextColor);
    idxOffset = (parm[ 2 ] / 2) + 4 + (parm[ 2 ] & 1);
    if ((parm[ 2 ] > 1) && (num >= (idxOffset + parm[ 2 ])) && (parm[ 3 ] == 0)) {
        // offset for each char
        int left = x;
        mPainter.drawText(left, y, width, height, Qt::AlignLeft | Qt::AlignTop, QLatin1String(text.mid(0, 1)));
        for (int i = 1; i < parm[ 2 ]; ++i) {
            left += parm[ idxOffset + i - 1 ];
            mPainter.drawText(left, y, width, height, Qt::AlignLeft | Qt::AlignTop, QLatin1String(text.mid(i, 1)));
        }
    } else {
        mPainter.drawText(x, y, width, height, Qt::AlignLeft | Qt::AlignTop, QLatin1String(text));
    }

    mPainter.restore();
}

//-----------------------------------------------------------------------------
// Bitmap
//-----------------------------------------------------------------------------
void QWinMetaFile::dibBitBlt(long num, short *parm)
{
    if (num > 9) {        // DIB image
        QImage bmpSrc;

        if (dibToBmp(bmpSrc, (char *)&parm[ 8 ], (num - 8) * 2)) {
            long raster = toDWord(parm);

            mPainter.setCompositionMode(winToQtComposition(raster));

            // wmf file allow negative width or height
            mPainter.save();
            if (parm[ 5 ] < 0) {    // width < 0 => horizontal flip
795 796
                QTransform m(-1.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F);
                mPainter.setWorldTransform(m, true);
Laurent Montel's avatar
Laurent Montel committed
797 798
            }
            if (parm[ 4 ] < 0) {    // height < 0 => vertical flip
799 800
                QTransform m(1.0F, 0.0F, 0.0F, -1.0F, 0.0F, 0.0F);
                mPainter.setWorldTransform(m, true);
Laurent Montel's avatar
Laurent Montel committed
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
            }
            mPainter.drawImage(parm[ 7 ], parm[ 6 ], bmpSrc, parm[ 3 ], parm[ 2 ], parm[ 5 ], parm[ 4 ]);
            mPainter.restore();
        }
    } else {
        qCDebug(KTNEFAPPS_LOG) << "QWinMetaFile::dibBitBlt without image: not implemented";
    }
}

//-----------------------------------------------------------------------------
void QWinMetaFile::dibStretchBlt(long num, short *parm)
{
    QImage bmpSrc;

    if (dibToBmp(bmpSrc, (char *)&parm[ 10 ], (num - 10) * 2)) {
        long raster = toDWord(parm);

        mPainter.setCompositionMode(winToQtComposition(raster));

        // wmf file allow negative width or height
        mPainter.save();
        if (parm[ 7 ] < 0) {    // width < 0 => horizontal flip
823 824
            QTransform m(-1.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F);
            mPainter.setWorldTransform(m, true);
Laurent Montel's avatar
Laurent Montel committed
825 826
        }
        if (parm[ 6 ] < 0) {    // height < 0 => vertical flip
827 828
            QTransform m(1.0F, 0.0F, 0.0F, -1.0F, 0.0F, 0.0F);
            mPainter.setWorldTransform(m, true);
Laurent Montel's avatar
Laurent Montel committed
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
        }
        bmpSrc = bmpSrc.copy(parm[ 5 ], parm[ 4 ], parm[ 3 ], parm[ 2 ]);
        // TODO: scale the bitmap ( QImage::scale(parm[ 7 ], parm[ 6 ]) is actually too slow )

        mPainter.drawImage(parm[ 9 ], parm[ 8 ], bmpSrc);
        mPainter.restore();
    }
}

//-----------------------------------------------------------------------------
void QWinMetaFile::stretchDib(long num, short *parm)
{
    QImage bmpSrc;

    if (dibToBmp(bmpSrc, (char *)&parm[ 11 ], (num - 11) * 2)) {
        long raster = toDWord(parm);

        mPainter.setCompositionMode(winToQtComposition(raster));

        // wmf file allow negative width or height
        mPainter.save();
        if (parm[ 8 ] < 0) {    // width < 0 => horizontal flip
851 852
            QTransform m(-1.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F);
            mPainter.setWorldTransform(m, true);
Laurent Montel's avatar
Laurent Montel committed
853 854
        }
        if (parm[ 7 ] < 0) {    // height < 0 => vertical flip
855 856
            QTransform m(1.0F, 0.0F, 0.0F, -1.0F, 0.0F, 0.0F);
            mPainter.setWorldTransform(m, true);
Laurent Montel's avatar
Laurent Montel committed
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
        }
        bmpSrc = bmpSrc.copy(parm[ 6 ], parm[ 5 ], parm[ 4 ], parm[ 3 ]);
        // TODO: scale the bitmap ( QImage::scale(parm[ 8 ], parm[ 7 ]) is actually too slow )

        mPainter.drawImage(parm[ 10 ], parm[ 9 ], bmpSrc);
        mPainter.restore();
    }
}

//-----------------------------------------------------------------------------
void QWinMetaFile::dibCreatePatternBrush(long num, short *parm)
{
    WinObjPatternBrushHandle *handle = new WinObjPatternBrushHandle;
    addHandle(handle);
    QImage bmpSrc;

    if (dibToBmp(bmpSrc, (char *)&parm[ 2 ], (num - 2) * 2)) {
        handle->image = bmpSrc;
        handle->brush.setTextureImage(handle->image);
    }
}

//-----------------------------------------------------------------------------
// Object handle
//-----------------------------------------------------------------------------
void QWinMetaFile::selectObject(long, short *parm)
{
    int idx = parm[ 0 ];
    if (idx >= 0 && idx < MAX_OBJHANDLE && mObjHandleTab[ idx ]) {
        mObjHandleTab[ idx ]->apply(mPainter);
    }
}

//-----------------------------------------------------------------------------
void QWinMetaFile::deleteObject(long, short *parm)
{
    deleteHandle(parm[ 0 ]);
}

//-----------------------------------------------------------------------------
void QWinMetaFile::createEmptyObject(long, short *)
{
    // allocation of an empty object (to keep object counting in sync)
    WinObjPenHandle *handle = new WinObjPenHandle;
    addHandle(handle);
    qCDebug(KTNEFAPPS_LOG) << "QWinMetaFile: unimplemented createObject";
}

//-----------------------------------------------------------------------------
void QWinMetaFile::createBrushIndirect(long, short *parm)
{
    static Qt::BrushStyle hatchedStyleTab[] = {
        Qt::HorPattern,
        Qt::FDiagPattern,
        Qt::BDiagPattern,
        Qt::CrossPattern,
        Qt::DiagCrossPattern
    };
Laurent Montel's avatar
Laurent Montel committed
915 916 917 918 919 920 921 922 923 924 925
    static Qt::BrushStyle styleTab[] = {
        Qt::SolidPattern,
        Qt::NoBrush,
        Qt::FDiagPattern,                                    /* hatched */
        Qt::Dense4Pattern,                                   /* should be custom bitmap pattern */
        Qt::HorPattern,                                      /* should be BS_INDEXED (?) */
        Qt::VerPattern,                                      /* should be device-independent bitmap */
        Qt::Dense6Pattern,                                   /* should be device-independent packed-bitmap */
        Qt::Dense2Pattern,                                   /* should be BS_PATTERN8x8 */
        Qt::Dense3Pattern                                    /* should be device-independent BS_DIBPATTERN8x8 */
    };
Laurent Montel's avatar
Laurent Montel committed
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
    Qt::BrushStyle style;
    short arg;
    WinObjBrushHandle *handle = new WinObjBrushHandle;
    addHandle(handle);

    arg = parm[ 0 ];
    if (arg == 2) {
        arg = parm[ 3 ];
        if (arg >= 0 && arg < 5) {
            style = hatchedStyleTab[ arg ];
        } else {
            qCDebug(KTNEFAPPS_LOG) << "QWinMetaFile::createBrushIndirect: invalid hatched brush" << arg;
            style = Qt::SolidPattern;
        }
    } else if (arg >= 0 && arg < 9) {
        style = styleTab[ arg ];
    } else {
        qCDebug(KTNEFAPPS_LOG) << "QWinMetaFile::createBrushIndirect: invalid brush" << arg;
        style = Qt::SolidPattern;
    }
    handle->brush.setStyle(style);
    handle->brush.setColor(color(parm + 1));
}

//-----------------------------------------------------------------------------
void QWinMetaFile::createPenIndirect(long, short *parm)
{
Laurent Montel's avatar
Laurent Montel committed
953 954 955 956
    static Qt::PenStyle styleTab[] = {
        Qt::SolidLine, Qt::DashLine, Qt::DotLine, Qt::DashDotLine, Qt::DashDotDotLine,
        Qt::NoPen, Qt::SolidLine
    };
Laurent Montel's avatar
Laurent Montel committed
957 958 959 960 961 962 963 964 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
    Qt::PenStyle style;
    WinObjPenHandle *handle = new WinObjPenHandle;
    addHandle(handle);

    if (parm[ 0 ] >= 0 && parm[ 0 ] < 6) {
        style = styleTab[ parm[ 0 ] ];
    } else {
        qCDebug(KTNEFAPPS_LOG) << "QWinMetaFile::createPenIndirect: invalid pen" << parm[ 0 ];
        style = Qt::SolidLine;
    }

    handle->pen.setStyle(style);
    handle->pen.setColor(color(parm + 3));
    handle->pen.setCapStyle(Qt::RoundCap);

    //int width = 0;
    // TODO : width of pen proportional to device context width
    // DOESN'T WORK
    /*
        QRect devRec;
        devRec = mPainter.transformed( mBBox );
        width = ( parm[ 0 ] * devRec.width() ) / mBBox.width() ;
        qCDebug(KTNEFAPPS_LOG) <<"CreatePenIndirect:";
        qCDebug(KTNEFAPPS_LOG) <<"   log coord. :" << mBBox.width() <<"" << mBBox.height();
        qCDebug(KTNEFAPPS_LOG) <<"   log. pen :" << parm[ 1 ] <<"" << parm[ 2 ];
        qCDebug(KTNEFAPPS_LOG) <<"   dev. pen :" << width;
        handle->pen.setWidth( width );
    */
}

//-----------------------------------------------------------------------------
void QWinMetaFile::createFontIndirect(long, short *parm)
{
    WinObjFontHandle *handle = new WinObjFontHandle;
    addHandle(handle);

    QString family(QLatin1String((const char *)&parm[ 9 ]));

    mRotation = -parm[ 2 ]  / 10;               // text rotation (in 1/10 degree)
    // TODO: memorisation of rotation in object Font
    handle->font.setFamily(family);
    handle->font.setFixedPitch(((parm[ 8 ] & 0x01) == 0));
    // TODO: investigation why some test case need -2. (size of font in logical point)
    handle->font.setPointSize(qAbs(parm[ 0 ]) - 2);
    handle->font.setWeight((parm[ 4 ] >> 3));
    handle->font.setItalic((parm[ 5 ] & 0x01));
    handle->font.setUnderline((parm[ 5 ] & 0x100));
}

//-----------------------------------------------------------------------------
// Misc
//-----------------------------------------------------------------------------
void QWinMetaFile::noop(long, short *)
{
}

void QWinMetaFile::end(long, short *)
{
    // end of file :
//    qCDebug(KTNEFAPPS_LOG) <<"END bbox=(" << mBBox.left() <<";" << mBBox.top() <<";" << mBBox.width() <<";" << mBBox.height() <<")";
}

//-----------------------------------------------------------------------------
unsigned short QWinMetaFile::calcCheckSum(WmfPlaceableHeader *apmfh)
{
Laurent Montel's avatar
Laurent Montel committed
1022 1023
    WORD *lpWord;
    WORD wResult, i;
Laurent Montel's avatar
Laurent Montel committed
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038

    // Start with the first word
    wResult = *(lpWord = (WORD *)(apmfh));
    // XOR in each of the other 9 words
    for (i = 1; i <= 9; ++i) {
        wResult ^= lpWord[ i ];
    }
    return wResult;
}

//-----------------------------------------------------------------------------
int QWinMetaFile::findFunc(unsigned short aFunc) const
{
    int i;

Laurent Montel's avatar
Laurent Montel committed
1039
    for (i = 0; metaFuncTab[ i ].name; ++i) {
Laurent Montel's avatar
Laurent Montel committed
1040 1041 1042
        if (metaFuncTab[ i ].func == aFunc) {
            return i;
        }
Laurent Montel's avatar
Laurent Montel committed
1043
    }
Laurent Montel's avatar
Laurent Montel committed
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067

    // here : unknown function
    return i;
}

//-----------------------------------------------------------------------------
QPolygon *QWinMetaFile::pointArray(short num, short *parm)
{
    int i;

    mPoints.resize(num);

    for (i = 0; i < num; ++i, parm += 2) {
        mPoints.setPoint(i, parm[ 0 ], parm[ 1 ]);
    }

    return &mPoints;
}

//-----------------------------------------------------------------------------
unsigned int QWinMetaFile::toDWord(short *parm)
{
    unsigned int l;

Laurent Montel's avatar
Laurent Montel committed
1068
#if !defined(WORDS_BIGENDIAN)
Laurent Montel's avatar
Laurent Montel committed
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
    l = *(unsigned int *)(parm);
#else
    char *bytes;
    char swap[ 4 ];
    bytes = (char *)parm;
    swap[ 0 ] = bytes[ 2 ];
    swap[ 1 ] = bytes[ 3 ];
    swap[ 2 ] = bytes[ 0 ];
    swap[ 3 ] = bytes[ 1 ];
    l = *(unsigned int *)(swap);
#endif

    return l;
}

//-----------------------------------------------------------------------------
QColor QWinMetaFile::color(short *parm)
{
    unsigned int colorRef;
    int red, green, blue;

    colorRef = toDWord(parm) & 0xffffff;
Laurent Montel's avatar
Laurent Montel committed
1091 1092 1093
    red = colorRef & 255;
    green = (colorRef >> 8) & 255;
    blue = (colorRef >> 16) & 255;
Laurent Montel's avatar
Laurent Montel committed
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117

    return QColor(red, green, blue);
}

//-----------------------------------------------------------------------------
void QWinMetaFile::xyToAngle(int xStart, int yStart, int xEnd, int yEnd, int &angleStart, int &angleLength)
{
    float aStart, aLength;

    aStart = atan2((double)yStart, (double)xStart);
    aLength = atan2((double)yEnd, (double)xEnd) - aStart;

    angleStart = (int)(aStart * 2880 / 3.14166);
    angleLength = (int)(aLength * 2880 / 3.14166);
    if (angleLength < 0) {
        angleLength = 5760 + angleLength;
    }
}

//-----------------------------------------------------------------------------
void QWinMetaFile::addHandle(WinObjHandle *handle)
{
    int idx;

Laurent Montel's avatar
Laurent Montel committed
1118
    for (idx = 0; idx < MAX_OBJHANDLE; idx++) {
Laurent Montel's avatar
Laurent Montel committed
1119
        if (mObjHandleTab[ idx ] == nullptr) {
Laurent Montel's avatar
Laurent Montel committed
1120 1121
            break;
        }
Laurent Montel's avatar
Laurent Montel committed
1122
    }
Laurent Montel's avatar
Laurent Montel committed
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135

    if (idx < MAX_OBJHANDLE) {
        mObjHandleTab[ idx ] = handle;
    } else {
        qCDebug(KTNEFAPPS_LOG) << "QWinMetaFile error: handle table full !";
    }
}

//-----------------------------------------------------------------------------
void QWinMetaFile::deleteHandle(int idx)
{
    if (idx >= 0 && idx < MAX_OBJHANDLE && mObjHandleTab[ idx ]) {
        delete mObjHandleTab[ idx ];
Laurent Montel's avatar
Laurent Montel committed
1136
        mObjHandleTab[ idx ] = nullptr;
Laurent Montel's avatar
Laurent Montel committed
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
    }
}

//-----------------------------------------------------------------------------
QPainter::CompositionMode QWinMetaFile::winToQtComposition(short parm) const
{
    static const QPainter::CompositionMode opTab[] = {
        // ### untested (conversion from Qt::RasterOp)
        QPainter::CompositionMode_Source, // Qt::CopyROP
        QPainter::CompositionMode_Clear, // Qt::ClearROP
        QPainter::CompositionMode_SourceOut, // Qt::NandROP
        QPainter::CompositionMode_SourceOut, // Qt::NotAndROP
        QPainter::CompositionMode_DestinationOut, // Qt::NotCopyROP
        QPainter::CompositionMode_DestinationOut, // Qt::AndNotROP
        QPainter::CompositionMode_DestinationOut, // Qt::NotROP
        QPainter::CompositionMode_Xor, // Qt::XorROP
        QPainter::CompositionMode_Source, // Qt::NorROP
        QPainter::CompositionMode_SourceIn, // Qt::AndROP
        QPainter::CompositionMode_SourceIn, // Qt::NotXorROP
        QPainter::CompositionMode_Destination, // Qt::NopROP
        QPainter::CompositionMode_Destination, // Qt::NotOrROP
        QPainter::CompositionMode_Source, // Qt::CopyROP
        QPainter::CompositionMode_Source, // Qt::OrNotROP
        QPainter::CompositionMode_SourceOver, // Qt::OrROP
        QPainter::CompositionMode_Source // Qt::SetROP
    };

    if (parm > 0 && parm <= 16) {
        return opTab[ parm ];
    } else {
        return QPainter::CompositionMode_Source;
    }
}

//-----------------------------------------------------------------------------
Laurent Montel's avatar
Laurent Montel committed
1172
QPainter::CompositionMode QWinMetaFile::winToQtComposition(long parm) const
Laurent Montel's avatar
Laurent Montel committed
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
{
    /* TODO: Ternary raster operations
    0x00C000CA  dest = (source AND pattern)
    0x00F00021  dest = pattern
    0x00FB0A09  dest = DPSnoo
    0x005A0049  dest = pattern XOR dest   */
    static const struct OpTab {
        long winRasterOp;
        QPainter::CompositionMode qtRasterOp;
    } opTab[] = {
        // ### untested (conversion from Qt::RasterOp)
        { 0x00CC0020, QPainter::CompositionMode_Source }, // CopyROP
        { 0x00EE0086, QPainter::CompositionMode_SourceOver }, // OrROP
        { 0x008800C6, QPainter::CompositionMode_SourceIn }, // AndROP
        { 0x00660046, QPainter::CompositionMode_Xor }, // XorROP
        { 0x00440328, QPainter::CompositionMode_DestinationOut }, // AndNotROP
        { 0x00330008, QPainter::CompositionMode_DestinationOut }, // NotCopyROP
        { 0x001100A6, QPainter::CompositionMode_SourceOut }, // NandROP
        { 0x00C000CA, QPainter::CompositionMode_Source }, // CopyROP
        { 0x00BB0226, QPainter::CompositionMode_Destination }, // NotOrROP
        { 0x00F00021, QPainter::CompositionMode_Source }, // CopyROP
        { 0x00FB0A09, QPainter::CompositionMode_Source }, // CopyROP
        { 0x005A0049, QPainter::CompositionMode_Source }, // CopyROP
        { 0x00550009, QPainter::CompositionMode_DestinationOut }, // NotROP
        { 0x00000042, QPainter::CompositionMode_Clear }, // ClearROP
        { 0x00FF0062, QPainter::CompositionMode_Source } // SetROP
    };

    int i;
Laurent Montel's avatar
Laurent Montel committed
1202
    for (i = 0; i < 15; ++i) {
Laurent Montel's avatar
Laurent Montel committed
1203 1204 1205
        if (opTab[ i ].winRasterOp == parm) {
            break;
        }
Laurent Montel's avatar
Laurent Montel committed
1206
    }
Laurent Montel's avatar
Laurent Montel committed
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232

    if (i < 15) {
        return opTab[ i ].qtRasterOp;
    } else {
        return QPainter::CompositionMode_Source;
    }
}

//-----------------------------------------------------------------------------
bool QWinMetaFile::dibToBmp(QImage &bmp, const char *dib, long size)
{
    typedef struct _BMPFILEHEADER {
        WORD bmType;
        DWORD bmSize;
        WORD bmReserved1;
        WORD bmReserved2;
        DWORD bmOffBits;
    }  BMPFILEHEADER;

    int sizeBmp = size + 14;
    QByteArray pattern;       // BMP header and DIB data
    pattern.fill(0, sizeBmp);    //resize and fill
    pattern.insert(14, QByteArray::fromRawData(dib, size));

    // add BMP header
    BMPFILEHEADER *bmpHeader;
1233
    bmpHeader = (BMPFILEHEADER *)(pattern.constData());
Laurent Montel's avatar
Laurent Montel committed
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
    bmpHeader->bmType = 0x4D42;
    bmpHeader->bmSize = sizeBmp;

    if (!bmp.loadFromData((const uchar *)bmpHeader, pattern.size(), "BMP")) {
        qCDebug(KTNEFAPPS_LOG) << "QWinMetaFile::dibToBmp: invalid bitmap";
        return false;
    } else {
//        if ( bmp.save("/home/software/kde-cvs/qt/examples/wmf/test.bmp", "BMP") )
//        if ( bmp.load( "/home/software/kde-cvs/qt/examples/wmf/test.bmp", "BMP" ) )
//            fprintf(stderr, "Bitmap ok \n");
        return true;
    }
}