summaryrefslogtreecommitdiffstats
path: root/tests/auto/gui/painting/qpdfwriter/tst_qpdfwriter.cpp
blob: 8c98a8ad9833bed7065d0f86241692a60e06cda7 (plain)
1
2
3
4
5
6
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
87
88
89
90
91
92
93
94
95
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
123
124
125
126
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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
249
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
279
280
281
282
283
284
285
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtTest/QtTest>
#include <QtGlobal>
#include <QtAlgorithms>
#include <QtGui/QAbstractTextDocumentLayout>
#include <QtGui/QPageLayout>
#include <QtGui/QPdfWriter>
#include <QtGui/QTextCursor>
#include <QtGui/QTextDocument>

class tst_QPdfWriter : public QObject
{
    Q_OBJECT

private slots:
    void basics();
    void testPageMetrics_data();
    void testPageMetrics();
    void qtbug59443();
};

void tst_QPdfWriter::basics()
{
    QTemporaryFile file;
    QVERIFY2(file.open(), qPrintable(file.errorString()));
    QPdfWriter writer(file.fileName());

    QCOMPARE(writer.title(), QString());
    writer.setTitle(QString("Test Title"));
    QCOMPARE(writer.title(), QString("Test Title"));

    QCOMPARE(writer.creator(), QString());
    writer.setCreator(QString("Test Creator"));
    QCOMPARE(writer.creator(), QString("Test Creator"));

    QCOMPARE(writer.resolution(), 1200);
    writer.setResolution(600);
    QCOMPARE(writer.resolution(), 600);

    QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::A4);
    QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::A4);
    QCOMPARE(writer.pageLayout().pageSize().size(QPageSize::Millimeter), QSizeF(210, 297));

    writer.setPageSize(QPageSize(QPageSize::A5));
    QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::A5);
    QCOMPARE(writer.pageLayout().pageSize().size(QPageSize::Millimeter), QSizeF(148, 210));

    writer.setPageSize(QPageSize(QPageSize::A3));
    QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::A3);
    QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::A3);
    QCOMPARE(writer.pageLayout().pageSize().size(QPageSize::Millimeter), QSizeF(297, 420));

    writer.setPageSize(QPageSize(QSize(210, 297), QPageSize::Millimeter));
    QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::A4);
    QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::A4);
    QCOMPARE(writer.pageLayout().pageSize().size(QPageSize::Millimeter), QSizeF(210, 297));

    QCOMPARE(writer.pageLayout().orientation(), QPageLayout::Portrait);
    writer.setPageOrientation(QPageLayout::Landscape);
    QCOMPARE(writer.pageLayout().orientation(), QPageLayout::Landscape);
    QCOMPARE(writer.pageLayout().pageSize().size(QPageSize::Millimeter), QSizeF(210, 297));

    QCOMPARE(writer.pageLayout().margins(), QMarginsF(10, 10, 10, 10));
    QCOMPARE(writer.pageLayout().units(), QPageLayout::Point);
    QCOMPARE(writer.pageLayout().margins(QPageLayout::Millimeter).left(), 3.53);  // mm
    QCOMPARE(writer.pageLayout().margins(QPageLayout::Millimeter).right(), 3.53);
    QCOMPARE(writer.pageLayout().margins(QPageLayout::Millimeter).top(), 3.53);
    QCOMPARE(writer.pageLayout().margins(QPageLayout::Millimeter).bottom(), 3.53);
    writer.setPageMargins(QMarginsF(20, 20, 20, 20), QPageLayout::Millimeter);
    QCOMPARE(writer.pageLayout().margins(), QMarginsF(20, 20, 20, 20));
    QCOMPARE(writer.pageLayout().units(), QPageLayout::Millimeter);
    QCOMPARE(writer.pageLayout().margins().left(), 20.0);
    QCOMPARE(writer.pageLayout().margins().right(), 20.0);
    QCOMPARE(writer.pageLayout().margins().top(), 20.0);
    QCOMPARE(writer.pageLayout().margins().bottom(), 20.0);
    const QMarginsF margins = {50, 50, 50, 50};
    writer.setPageMargins(margins, QPageLayout::Millimeter);
    QCOMPARE(writer.pageLayout().margins(), margins);
    QCOMPARE(writer.pageLayout().units(), QPageLayout::Millimeter);
    QCOMPARE(writer.pageLayout().margins().left(), 50.0);
    QCOMPARE(writer.pageLayout().margins().right(), 50.0);
    QCOMPARE(writer.pageLayout().margins().top(), 50.0);
    QCOMPARE(writer.pageLayout().margins().bottom(), 50.0);

    QCOMPARE(writer.pageLayout().fullRect(QPageLayout::Millimeter), QRectF(0, 0, 297, 210));
    QCOMPARE(writer.pageLayout().paintRect(QPageLayout::Millimeter), QRectF(50, 50, 197, 110));

    QByteArray metadata (
            "<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?>\n"
            "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\">\n"
            "  <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\"\n"
            "    <rdf:Description xmlns:dc=\"http://purl.org/dc/elements/1.1/\" rdf:about=\"\">\n"
            "      <dc:title>\n"
            "        <rdf:Alt>\n"
            "          <rdf:li xml:lang=\"x-default\">TITLE</rdf:li>\n"
            "        </rdf:Alt>\n"
            "      </dc:title>\n"
            "    </rdf:Description>\n"
            "    <rdf:Description xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\" rdf:about="" xmp:CreatorTool=\"OUR_OWN_XMP\" xmp:CreateDate=\"2019-12-16T00:00:00+01:00\" xmp:ModifyDate=\"2019-12-16T00:00:00+01:00\"/>\n"
            "    <rdf:Description xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\" rdf:about="" pdf:Producer=\"MetaType Info Producer\"/>\n"
            "    <rdf:Description xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\" rdf:about=\"THI IS ALL ABOUT\" pdfaid:part=\"1\" pdfaid:conformance=\"B\"/>\n"
            "  </rdf:RDF>\n"
            "</x:xmpmeta>\n"
            "<?xpacket end='w'?>\n"
    );

    QCOMPARE(writer.documentXmpMetadata(), QByteArray());
    writer.setDocumentXmpMetadata(metadata);
    QCOMPARE(writer.documentXmpMetadata(), metadata);
}

// Test the old page metrics methods, see also QPrinter tests for the same.
void tst_QPdfWriter::testPageMetrics_data()
{
    QTest::addColumn<QPageSize::PageSizeId>("pageSizeId");
    QTest::addColumn<qreal>("widthMMf");
    QTest::addColumn<qreal>("heightMMf");
    QTest::addColumn<bool>("setMargins");
    QTest::addColumn<qreal>("leftMMf");
    QTest::addColumn<qreal>("rightMMf");
    QTest::addColumn<qreal>("topMMf");
    QTest::addColumn<qreal>("bottomMMf");

    QTest::newRow("A4")         << QPageSize::A4 << 210.0 << 297.0 << false
                                << 3.53 <<  3.53 << 3.53 <<  3.53;
    QTest::newRow("A4 Margins") << QPageSize::A4 << 210.0 << 297.0 << true
                                << 20.0 << 30.0  << 40.0  << 50.0;

    QTest::newRow("Portrait")          << QPageSize::Custom << 345.0 << 678.0 << false
                                       << 3.53 << 3.53 << 3.53 <<  3.53;
    QTest::newRow("Portrait Margins")  << QPageSize::Custom << 345.0 << 678.0 << true
                                       << 20.0 << 30.0 << 40.0 << 50.0;
    QTest::newRow("Landscape")         << QPageSize::Custom << 678.0 << 345.0 << false
                                       << 3.53 << 3.53 << 3.53 << 3.53;
    QTest::newRow("Landscape Margins") << QPageSize::Custom << 678.0 << 345.0 << true
                                       << 20.0 << 30.0 << 40.0 << 50.0;
}

void tst_QPdfWriter::testPageMetrics()
{
    QFETCH(QPageSize::PageSizeId, pageSizeId);
    QFETCH(qreal, widthMMf);
    QFETCH(qreal, heightMMf);
    QFETCH(bool, setMargins);
    QFETCH(qreal, leftMMf);
    QFETCH(qreal, rightMMf);
    QFETCH(qreal, topMMf);
    QFETCH(qreal, bottomMMf);

    QSizeF sizeMMf = QSizeF(widthMMf, heightMMf);

    QTemporaryFile file;
    QVERIFY2(file.open(), qPrintable(file.errorString()));
    QPdfWriter writer(file.fileName());
    QCOMPARE(writer.pageLayout().orientation(), QPageLayout::Portrait);

    if (setMargins) {
        // Setup the given margins
        writer.setPageMargins({leftMMf, topMMf, rightMMf, bottomMMf}, QPageLayout::Millimeter);
        QCOMPARE(writer.pageLayout().margins().left(), leftMMf);
        QCOMPARE(writer.pageLayout().margins().right(), rightMMf);
        QCOMPARE(writer.pageLayout().margins().top(), topMMf);
        QCOMPARE(writer.pageLayout().margins().bottom(), bottomMMf);
    }

    // Set the given size, in Portrait mode
    const QPageSize pageSize = pageSizeId == QPageSize::Custom
        ? QPageSize(sizeMMf, QPageSize::Millimeter) : QPageSize(pageSizeId);
    writer.setPageSize(pageSize);
    QCOMPARE(writer.pageLayout().pageSize().id(), pageSizeId);
    QCOMPARE(int(writer.pageLayout().pageSize().id()), int(pageSizeId));

    QCOMPARE(writer.pageLayout().orientation(), QPageLayout::Portrait);
    QCOMPARE(writer.pageLayout().margins(QPageLayout::Millimeter).left(), leftMMf);
    QCOMPARE(writer.pageLayout().margins(QPageLayout::Millimeter).right(), rightMMf);
    QCOMPARE(writer.pageLayout().margins(QPageLayout::Millimeter).top(), topMMf);
    QCOMPARE(writer.pageLayout().margins(QPageLayout::Millimeter).bottom(), bottomMMf);

    // QPagedPaintDevice::pageLayout().pageSize().size(QPageSize::Millimeter) always returns Portrait
    QCOMPARE(writer.pageLayout().pageSize().size(QPageSize::Millimeter), sizeMMf);

    // QPagedPaintDevice::widthMM() and heightMM() are paint metrics and always return set orientation
    QCOMPARE(writer.widthMM(), qRound(widthMMf - leftMMf - rightMMf));
    QCOMPARE(writer.heightMM(), qRound(heightMMf - topMMf - bottomMMf));

    // Now switch to Landscape mode, size should be unchanged, but rect and metrics should change
    writer.setPageOrientation(QPageLayout::Landscape);
    QCOMPARE(writer.pageLayout().pageSize().id(), pageSizeId);
    QCOMPARE(int(writer.pageLayout().pageSize().id()), int(pageSizeId));
    QCOMPARE(writer.pageLayout().orientation(), QPageLayout::Landscape);
    QCOMPARE(writer.pageLayout().margins(QPageLayout::Millimeter).left(), leftMMf);
    QCOMPARE(writer.pageLayout().margins(QPageLayout::Millimeter).right(), rightMMf);
    QCOMPARE(writer.pageLayout().margins(QPageLayout::Millimeter).top(), topMMf);
    QCOMPARE(writer.pageLayout().margins(QPageLayout::Millimeter).bottom(), bottomMMf);

    // QPagedPaintDevice::pageLayout().pageSize().size(QPageSize::Millimeter) always returns Portrait
    QCOMPARE(writer.pageLayout().pageSize().size(QPageSize::Millimeter), sizeMMf);

    // QPagedPaintDevice::widthMM() and heightMM() are paint metrics and always return set orientation
    QCOMPARE(writer.widthMM(), qRound(heightMMf - leftMMf - rightMMf));
    QCOMPARE(writer.heightMM(), qRound(widthMMf - topMMf - bottomMMf));

    // QPdfWriter::fullRect() always returns set orientation
    QCOMPARE(writer.pageLayout().fullRect(QPageLayout::Millimeter), QRectF(0, 0, heightMMf, widthMMf));

    // QPdfWriter::paintRect() always returns set orientation
    QCOMPARE(writer.pageLayout().paintRect(QPageLayout::Millimeter), QRectF(leftMMf, topMMf, heightMMf - leftMMf - rightMMf, widthMMf - topMMf - bottomMMf));


    // Now while in Landscape mode, set the size again, results should be the same
    writer.setPageSize(pageSize);
    QCOMPARE(writer.pageLayout().pageSize().id(), pageSizeId);
    QCOMPARE(int(writer.pageLayout().pageSize().id()), int(pageSizeId));
    QCOMPARE(writer.pageLayout().orientation(), QPageLayout::Landscape);
    QCOMPARE(writer.pageLayout().margins(QPageLayout::Millimeter).left(), leftMMf);
    QCOMPARE(writer.pageLayout().margins(QPageLayout::Millimeter).right(), rightMMf);
    QCOMPARE(writer.pageLayout().margins(QPageLayout::Millimeter).top(), topMMf);
    QCOMPARE(writer.pageLayout().margins(QPageLayout::Millimeter).bottom(), bottomMMf);

    // QPagedPaintDevice::pageLayout().pageSize().size(QPageSize::Millimeter) always returns Portrait
    QCOMPARE(writer.pageLayout().pageSize().size(QPageSize::Millimeter), sizeMMf);

    // QPagedPaintDevice::widthMM() and heightMM() are paint metrics and always return set orientation
    QCOMPARE(writer.widthMM(), qRound(heightMMf - leftMMf - rightMMf));
    QCOMPARE(writer.heightMM(), qRound(widthMMf - topMMf - bottomMMf));

    // QPdfWriter::fullRect() always returns set orientation
    QCOMPARE(writer.pageLayout().fullRect(QPageLayout::Millimeter), QRectF(0, 0, heightMMf, widthMMf));

    // QPdfWriter::paintRect() always returns set orientation
    QCOMPARE(writer.pageLayout().paintRect(QPageLayout::Millimeter), QRectF(leftMMf, topMMf, heightMMf - leftMMf - rightMMf, widthMMf - topMMf - bottomMMf));
}

void tst_QPdfWriter::qtbug59443()
{
    // Do not crash or assert
    QTemporaryFile file;
    QVERIFY2(file.open(), qPrintable(file.errorString()));
    QPdfWriter writer(file.fileName());
    writer.setPageSize(QPageSize(QPageSize::A4));
    QTextDocument doc;
    doc.documentLayout()->setPaintDevice(&writer);

    doc.setUndoRedoEnabled(false);
    QTextCursor cursor(&doc);
    QFont font = doc.defaultFont();
    font.setFamily("Calibri");
    font.setPointSize(8);
    doc.setDefaultFont(font);

    cursor.insertText(QString::fromStdWString(L"기초하며, 베어링제조업체와 타\n"));
    doc.print(&writer);

}

QTEST_MAIN(tst_QPdfWriter)

#include "tst_qpdfwriter.moc"