1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtTest/QtTest>
30#include <QtGlobal>
31#include <QtAlgorithms>
32#include <QtGui/QAbstractTextDocumentLayout>
33#include <QtGui/QPageLayout>
34#include <QtGui/QPdfWriter>
35#include <QtGui/QTextCursor>
36#include <QtGui/QTextDocument>
37
38class tst_QPdfWriter : public QObject
39{
40 Q_OBJECT
41
42private slots:
43 void basics();
44 void testPageMetrics_data();
45 void testPageMetrics();
46 void qtbug59443();
47};
48
49void tst_QPdfWriter::basics()
50{
51 QTemporaryFile file;
52 QVERIFY2(file.open(), qPrintable(file.errorString()));
53 QPdfWriter writer(file.fileName());
54
55 QCOMPARE(writer.title(), QString());
56 writer.setTitle(QString("Test Title"));
57 QCOMPARE(writer.title(), QString("Test Title"));
58
59 QCOMPARE(writer.creator(), QString());
60 writer.setCreator(QString("Test Creator"));
61 QCOMPARE(writer.creator(), QString("Test Creator"));
62
63 QCOMPARE(writer.resolution(), 1200);
64 writer.setResolution(600);
65 QCOMPARE(writer.resolution(), 600);
66
67 QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::A4);
68 QCOMPARE(writer.pageSize(), QPdfWriter::A4);
69 QCOMPARE(writer.pageSizeMM(), QSizeF(210, 297));
70
71 writer.setPageSize(QPageSize(QPageSize::A5));
72 QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::A5);
73 QCOMPARE(writer.pageSize(), QPdfWriter::A5);
74 QCOMPARE(writer.pageSizeMM(), QSizeF(148, 210));
75
76 writer.setPageSize(QPageSize(QPageSize::A3));
77 QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::A3);
78 QCOMPARE(writer.pageSize(), QPdfWriter::A3);
79 QCOMPARE(writer.pageSizeMM(), QSizeF(297, 420));
80
81 writer.setPageSize(QPageSize(QSize(210, 297), QPageSize::Millimeter));
82 QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::A4);
83 QCOMPARE(writer.pageSize(), QPdfWriter::A4);
84 QCOMPARE(writer.pageSizeMM(), QSizeF(210, 297));
85
86 QCOMPARE(writer.pageLayout().orientation(), QPageLayout::Portrait);
87 writer.setPageOrientation(QPageLayout::Landscape);
88 QCOMPARE(writer.pageLayout().orientation(), QPageLayout::Landscape);
89 QCOMPARE(writer.pageSizeMM(), QSizeF(210, 297));
90
91 QCOMPARE(writer.pageLayout().margins(), QMarginsF(10, 10, 10, 10));
92 QCOMPARE(writer.pageLayout().units(), QPageLayout::Point);
93 QCOMPARE(writer.margins().left, 3.53); // mm
94 QCOMPARE(writer.margins().right, 3.53);
95 QCOMPARE(writer.margins().top, 3.53);
96 QCOMPARE(writer.margins().bottom, 3.53);
97 writer.setPageMargins(margins: QMarginsF(20, 20, 20, 20), units: QPageLayout::Millimeter);
98 QCOMPARE(writer.pageLayout().margins(), QMarginsF(20, 20, 20, 20));
99 QCOMPARE(writer.pageLayout().units(), QPageLayout::Millimeter);
100 QCOMPARE(writer.margins().left, 20.0);
101 QCOMPARE(writer.margins().right, 20.0);
102 QCOMPARE(writer.margins().top, 20.0);
103 QCOMPARE(writer.margins().bottom, 20.0);
104 const QMarginsF margins = {50, 50, 50, 50};
105 writer.setPageMargins(margins, units: QPageLayout::Millimeter);
106 QCOMPARE(writer.pageLayout().margins(), margins);
107 QCOMPARE(writer.pageLayout().units(), QPageLayout::Millimeter);
108 QCOMPARE(writer.margins().left, 50.0);
109 QCOMPARE(writer.margins().right, 50.0);
110 QCOMPARE(writer.margins().top, 50.0);
111 QCOMPARE(writer.margins().bottom, 50.0);
112
113 QCOMPARE(writer.pageLayout().fullRect(QPageLayout::Millimeter), QRectF(0, 0, 297, 210));
114 QCOMPARE(writer.pageLayout().paintRect(QPageLayout::Millimeter), QRectF(50, 50, 197, 110));
115
116 QByteArray metadata (
117 "<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?>\n"
118 "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\">\n"
119 " <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\"\n"
120 " <rdf:Description xmlns:dc=\"http://purl.org/dc/elements/1.1/\" rdf:about=\"\">\n"
121 " <dc:title>\n"
122 " <rdf:Alt>\n"
123 " <rdf:li xml:lang=\"x-default\">TITLE</rdf:li>\n"
124 " </rdf:Alt>\n"
125 " </dc:title>\n"
126 " </rdf:Description>\n"
127 " <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"
128 " <rdf:Description xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\" rdf:about="" pdf:Producer=\"MetaType Info Producer\"/>\n"
129 " <rdf:Description xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\" rdf:about=\"THI IS ALL ABOUT\" pdfaid:part=\"1\" pdfaid:conformance=\"B\"/>\n"
130 " </rdf:RDF>\n"
131 "</x:xmpmeta>\n"
132 "<?xpacket end='w'?>\n"
133 );
134
135 QCOMPARE(writer.documentXmpMetadata(), QByteArray());
136 writer.setDocumentXmpMetadata(metadata);
137 QCOMPARE(writer.documentXmpMetadata(), metadata);
138}
139
140// Test the old page metrics methods, see also QPrinter tests for the same.
141void tst_QPdfWriter::testPageMetrics_data()
142{
143 QTest::addColumn<QPageSize::PageSizeId>(name: "pageSizeId");
144 QTest::addColumn<qreal>(name: "widthMMf");
145 QTest::addColumn<qreal>(name: "heightMMf");
146 QTest::addColumn<bool>(name: "setMargins");
147 QTest::addColumn<qreal>(name: "leftMMf");
148 QTest::addColumn<qreal>(name: "rightMMf");
149 QTest::addColumn<qreal>(name: "topMMf");
150 QTest::addColumn<qreal>(name: "bottomMMf");
151
152 QTest::newRow(dataTag: "A4") << QPageSize::A4 << 210.0 << 297.0 << false
153 << 3.53 << 3.53 << 3.53 << 3.53;
154 QTest::newRow(dataTag: "A4 Margins") << QPageSize::A4 << 210.0 << 297.0 << true
155 << 20.0 << 30.0 << 40.0 << 50.0;
156
157 QTest::newRow(dataTag: "Portrait") << QPageSize::Custom << 345.0 << 678.0 << false
158 << 3.53 << 3.53 << 3.53 << 3.53;
159 QTest::newRow(dataTag: "Portrait Margins") << QPageSize::Custom << 345.0 << 678.0 << true
160 << 20.0 << 30.0 << 40.0 << 50.0;
161 QTest::newRow(dataTag: "Landscape") << QPageSize::Custom << 678.0 << 345.0 << false
162 << 3.53 << 3.53 << 3.53 << 3.53;
163 QTest::newRow(dataTag: "Landscape Margins") << QPageSize::Custom << 678.0 << 345.0 << true
164 << 20.0 << 30.0 << 40.0 << 50.0;
165}
166
167void tst_QPdfWriter::testPageMetrics()
168{
169 QFETCH(QPageSize::PageSizeId, pageSizeId);
170 QFETCH(qreal, widthMMf);
171 QFETCH(qreal, heightMMf);
172 QFETCH(bool, setMargins);
173 QFETCH(qreal, leftMMf);
174 QFETCH(qreal, rightMMf);
175 QFETCH(qreal, topMMf);
176 QFETCH(qreal, bottomMMf);
177
178 QSizeF sizeMMf = QSizeF(widthMMf, heightMMf);
179
180 QTemporaryFile file;
181 QVERIFY2(file.open(), qPrintable(file.errorString()));
182 QPdfWriter writer(file.fileName());
183 QCOMPARE(writer.pageLayout().orientation(), QPageLayout::Portrait);
184
185 if (setMargins) {
186 // Setup the given margins
187 writer.setPageMargins(margins: {leftMMf, topMMf, rightMMf, bottomMMf}, units: QPageLayout::Millimeter);
188 QCOMPARE(writer.margins().left, leftMMf);
189 QCOMPARE(writer.margins().right, rightMMf);
190 QCOMPARE(writer.margins().top, topMMf);
191 QCOMPARE(writer.margins().bottom, bottomMMf);
192 }
193
194 // Set the given size, in Portrait mode
195 const QPageSize pageSize = pageSizeId == QPageSize::Custom
196 ? QPageSize(sizeMMf, QPageSize::Millimeter) : QPageSize(pageSizeId);
197 writer.setPageSize(pageSize);
198 QCOMPARE(writer.pageLayout().pageSize().id(), pageSizeId);
199 QCOMPARE(int(writer.pageSize()), int(pageSizeId));
200
201 QCOMPARE(writer.pageLayout().orientation(), QPageLayout::Portrait);
202 QCOMPARE(writer.margins().left, leftMMf);
203 QCOMPARE(writer.margins().right, rightMMf);
204 QCOMPARE(writer.margins().top, topMMf);
205 QCOMPARE(writer.margins().bottom, bottomMMf);
206
207 // QPagedPaintDevice::pageSizeMM() always returns Portrait
208 QCOMPARE(writer.pageSizeMM(), sizeMMf);
209
210 // QPagedPaintDevice::widthMM() and heightMM() are paint metrics and always return set orientation
211 QCOMPARE(writer.widthMM(), qRound(widthMMf - leftMMf - rightMMf));
212 QCOMPARE(writer.heightMM(), qRound(heightMMf - topMMf - bottomMMf));
213
214 // Now switch to Landscape mode, size should be unchanged, but rect and metrics should change
215 writer.setPageOrientation(QPageLayout::Landscape);
216 QCOMPARE(writer.pageLayout().pageSize().id(), pageSizeId);
217 QCOMPARE(int(writer.pageSize()), int(pageSizeId));
218 QCOMPARE(writer.pageLayout().orientation(), QPageLayout::Landscape);
219 QCOMPARE(writer.margins().left, leftMMf);
220 QCOMPARE(writer.margins().right, rightMMf);
221 QCOMPARE(writer.margins().top, topMMf);
222 QCOMPARE(writer.margins().bottom, bottomMMf);
223
224 // QPagedPaintDevice::pageSizeMM() always returns Portrait
225 QCOMPARE(writer.pageSizeMM(), sizeMMf);
226
227 // QPagedPaintDevice::widthMM() and heightMM() are paint metrics and always return set orientation
228 QCOMPARE(writer.widthMM(), qRound(heightMMf - leftMMf - rightMMf));
229 QCOMPARE(writer.heightMM(), qRound(widthMMf - topMMf - bottomMMf));
230
231 // QPdfWriter::fullRect() always returns set orientation
232 QCOMPARE(writer.pageLayout().fullRect(QPageLayout::Millimeter), QRectF(0, 0, heightMMf, widthMMf));
233
234 // QPdfWriter::paintRect() always returns set orientation
235 QCOMPARE(writer.pageLayout().paintRect(QPageLayout::Millimeter), QRectF(leftMMf, topMMf, heightMMf - leftMMf - rightMMf, widthMMf - topMMf - bottomMMf));
236
237
238 // Now while in Landscape mode, set the size again, results should be the same
239 writer.setPageSize(pageSize);
240 QCOMPARE(writer.pageLayout().pageSize().id(), pageSizeId);
241 QCOMPARE(int(writer.pageSize()), int(pageSizeId));
242 QCOMPARE(writer.pageLayout().orientation(), QPageLayout::Landscape);
243 QCOMPARE(writer.margins().left, leftMMf);
244 QCOMPARE(writer.margins().right, rightMMf);
245 QCOMPARE(writer.margins().top, topMMf);
246 QCOMPARE(writer.margins().bottom, bottomMMf);
247
248 // QPagedPaintDevice::pageSizeMM() always returns Portrait
249 QCOMPARE(writer.pageSizeMM(), sizeMMf);
250
251 // QPagedPaintDevice::widthMM() and heightMM() are paint metrics and always return set orientation
252 QCOMPARE(writer.widthMM(), qRound(heightMMf - leftMMf - rightMMf));
253 QCOMPARE(writer.heightMM(), qRound(widthMMf - topMMf - bottomMMf));
254
255 // QPdfWriter::fullRect() always returns set orientation
256 QCOMPARE(writer.pageLayout().fullRect(QPageLayout::Millimeter), QRectF(0, 0, heightMMf, widthMMf));
257
258 // QPdfWriter::paintRect() always returns set orientation
259 QCOMPARE(writer.pageLayout().paintRect(QPageLayout::Millimeter), QRectF(leftMMf, topMMf, heightMMf - leftMMf - rightMMf, widthMMf - topMMf - bottomMMf));
260}
261
262void tst_QPdfWriter::qtbug59443()
263{
264 // Do not crash or assert
265 QTemporaryFile file;
266 QVERIFY2(file.open(), qPrintable(file.errorString()));
267 QPdfWriter writer(file.fileName());
268 writer.setPageSize(QPageSize(QPageSize::A4));
269 QTextDocument doc;
270 doc.documentLayout()->setPaintDevice(&writer);
271
272 doc.setUndoRedoEnabled(false);
273 QTextCursor cursor(&doc);
274 QFont font = doc.defaultFont();
275 font.setFamily("Calibri");
276 font.setPointSize(8);
277 doc.setDefaultFont(font);
278
279 cursor.insertText(text: QString::fromStdWString(s: L"기초하며, 베어링제조업체와 타\n"));
280 doc.print(printer: &writer);
281
282}
283
284QTEST_MAIN(tst_QPdfWriter)
285
286#include "tst_qpdfwriter.moc"
287

source code of qtbase/tests/auto/gui/painting/qpdfwriter/tst_qpdfwriter.cpp