QPageSize - Add new QPageSize class

New QPageSize class to encapsulate paper sizes and names to ensure all
sizes and conversions are consistent and match the Postscript standard
sizes.

Subsequent changes will use this class in the paged paint devices,
paint engines, print engines, and print plugins to replace multiple
inconsistent local implementations.

[ChangeLog][QtGui][QPageSize] Added new QPageSize class to implement
Adobe Postscript PPD standard page sizes. This class supports the
standard page sizes, names and keys from the PPD standard, and provides
convenient size and rect conversion methods.

Change-Id: Ie2c8be0c3df0d29ac5da4cd9877ad41d0982633c
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
This commit is contained in:
John Layt 2013-12-10 20:23:07 +01:00 committed by The Qt Project
parent ad0163d088
commit 87d802465b
9 changed files with 2459 additions and 0 deletions

View File

@ -19,6 +19,7 @@ HEADERS += \
painting/qoutlinemapper_p.h \
painting/qpagedpaintdevice.h \
painting/qpagedpaintdevice_p.h \
painting/qpagesize.h \
painting/qpaintdevice.h \
painting/qpaintengine.h \
painting/qpaintengine_p.h \
@ -66,6 +67,7 @@ SOURCES += \
painting/qmemrotate.cpp \
painting/qoutlinemapper.cpp \
painting/qpagedpaintdevice.cpp \
painting/qpagesize.cpp \
painting/qpaintdevice.cpp \
painting/qpaintengine.cpp \
painting/qpaintengineex.cpp \

View File

@ -60,6 +60,7 @@ public:
virtual bool newPage() = 0;
// ### Qt6 Remove in favor of QPage::PageSize
enum PageSize { A4, B5, Letter, Legal, Executive,
A0, A1, A2, A3, A5, A6, A7, A8, A9, B0, B1,
B10, B2, B3, B4, B6, B7, B8, B9, C5E, Comm10E,

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,310 @@
/****************************************************************************
**
** Copyright (C) 2014 John Layt <jlayt@kde.org>
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QPAGESIZE_H
#define QPAGESIZE_H
#include <QtCore/qsharedpointer.h>
QT_BEGIN_NAMESPACE
#if defined(B0)
#undef B0 // Terminal hang-up. We assume that you do not want that.
#endif
class QPageSizePrivate;
class QString;
class QSize;
class QSizeF;
class Q_GUI_EXPORT QPageSize
{
public:
// ### Qt6 Re-order and remove duplicates
// NOTE: Must keep in sync with QPagedPrintEngine and QPrinter
enum PageSizeId {
// Existing Qt sizes
A4,
B5,
Letter,
Legal,
Executive,
A0,
A1,
A2,
A3,
A5,
A6,
A7,
A8,
A9,
B0,
B1,
B10,
B2,
B3,
B4,
B6,
B7,
B8,
B9,
C5E,
Comm10E,
DLE,
Folio,
Ledger,
Tabloid,
Custom,
// New values derived from PPD standard
A10,
A3Extra,
A4Extra,
A4Plus,
A4Small,
A5Extra,
B5Extra,
JisB0,
JisB1,
JisB2,
JisB3,
JisB4,
JisB5,
JisB6,
JisB7,
JisB8,
JisB9,
JisB10,
// AnsiA = Letter,
// AnsiB = Ledger,
AnsiC,
AnsiD,
AnsiE,
LegalExtra,
LetterExtra,
LetterPlus,
LetterSmall,
TabloidExtra,
ArchA,
ArchB,
ArchC,
ArchD,
ArchE,
Imperial7x9,
Imperial8x10,
Imperial9x11,
Imperial9x12,
Imperial10x11,
Imperial10x13,
Imperial10x14,
Imperial12x11,
Imperial15x11,
ExecutiveStandard,
Note,
Quarto,
Statement,
SuperA,
SuperB,
Postcard,
DoublePostcard,
Prc16K,
Prc32K,
Prc32KBig,
FanFoldUS,
FanFoldGerman,
FanFoldGermanLegal,
EnvelopeB4,
EnvelopeB5,
EnvelopeB6,
EnvelopeC0,
EnvelopeC1,
EnvelopeC2,
EnvelopeC3,
EnvelopeC4,
// EnvelopeC5 = C5E,
EnvelopeC6,
EnvelopeC65,
EnvelopeC7,
// EnvelopeDL = DLE,
Envelope9,
// Envelope10 = Comm10E,
Envelope11,
Envelope12,
Envelope14,
EnvelopeMonarch,
EnvelopePersonal,
EnvelopeChou3,
EnvelopeChou4,
EnvelopeInvite,
EnvelopeItalian,
EnvelopeKaku2,
EnvelopeKaku3,
EnvelopePrc1,
EnvelopePrc2,
EnvelopePrc3,
EnvelopePrc4,
EnvelopePrc5,
EnvelopePrc6,
EnvelopePrc7,
EnvelopePrc8,
EnvelopePrc9,
EnvelopePrc10,
EnvelopeYou4,
// Last item, with commonly used synynoms from QPagedPrintEngine / QPrinter
LastPageSize = EnvelopeYou4,
NPageSize = LastPageSize,
NPaperSize = LastPageSize,
// Convenience overloads for naming consistency
AnsiA = Letter,
AnsiB = Ledger,
EnvelopeC5 = C5E,
EnvelopeDL = DLE,
Envelope10 = Comm10E
};
// NOTE: Must keep in sync with QPageLayout::Unit and QPrinter::Unit
enum Unit {
Millimeter,
Point,
Inch,
Pica,
Didot,
Cicero
};
enum SizeMatchPolicy {
FuzzyMatch,
FuzzyOrientationMatch,
ExactMatch
};
QPageSize();
explicit QPageSize(QPageSize::PageSizeId pageSizeId);
QPageSize(const QSize &pointSize,
const QString &name = QString(),
QPageSize::SizeMatchPolicy matchPolicy = QPageSize::FuzzyMatch);
QPageSize(const QSizeF &size, QPageSize::Unit units,
const QString &name = QString(),
QPageSize::SizeMatchPolicy matchPolicy = QPageSize::FuzzyMatch);
QPageSize(const QPageSize &other);
~QPageSize();
QPageSize &operator=(const QPageSize &other);
#ifdef Q_COMPILER_RVALUE_REFS
QPageSize &operator=(QPageSize &&other) { swap(other); return *this; }
#endif
void swap(QPageSize &other) { d.swap(other.d); }
bool operator==(const QPageSize &other) const;
bool isEquivalentTo(const QPageSize &other) const;
bool isValid() const;
QString key() const;
QString name() const;
QPageSize::PageSizeId id() const;
int windowsId() const;
QSizeF definitionSize() const;
QPageSize::Unit definitionUnits() const;
QSizeF size(QPageSize::Unit units) const;
QSize sizePoints() const;
QSize sizePixels(int resolution) const;
QRectF rect(QPageSize::Unit units) const;
QRect rectPoints() const;
QRect rectPixels(int resolution) const;
static QString key(QPageSize::PageSizeId pageSizeId);
static QString name(QPageSize::PageSizeId pageSizeId);
static QPageSize::PageSizeId id(const QSize &pointSize,
QPageSize::SizeMatchPolicy matchPolicy = QPageSize::FuzzyMatch);
static QPageSize::PageSizeId id(const QSizeF &size, QPageSize::Unit units,
QPageSize::SizeMatchPolicy matchPolicy = QPageSize::FuzzyMatch);
static QPageSize::PageSizeId id(int windowsId);
static int windowsId(QPageSize::PageSizeId pageSizeId);
static QSizeF definitionSize(QPageSize::PageSizeId pageSizeId);
static QPageSize::Unit definitionUnits(QPageSize::PageSizeId pageSizeId);
static QSizeF size(QPageSize::PageSizeId pageSizeId, QPageSize::Unit units);
static QSize sizePoints(QPageSize::PageSizeId pageSizeId);
static QSize sizePixels(QPageSize::PageSizeId pageSizeId, int resolution);
private:
friend class QPageSizePrivate;
QPageSize(const QString &key, const QSize &pointSize, const QString &name);
QPageSize(int windowsId, const QSize &pointSize, const QString &name);
QPageSize(QPageSizePrivate &dd);
QSharedDataPointer<QPageSizePrivate> d;
};
Q_DECLARE_SHARED(QPageSize)
#ifndef QT_NO_DEBUG_STREAM
Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QPageSize &pageSize);
#endif
QT_END_NAMESPACE
Q_DECLARE_METATYPE(QPageSize)
Q_DECLARE_METATYPE(QPageSize::PageSizeId)
Q_DECLARE_METATYPE(QPageSize::Unit)
#endif // QPAGESIZE_H

View File

@ -75,6 +75,7 @@ public:
enum Orientation { Portrait, Landscape };
// ### Qt6 Remove in favor of QPage::PageSize
#ifndef Q_QDOC
typedef PageSize PaperSize;
#else

View File

@ -5,6 +5,7 @@ SUBDIRS=\
qcolor \
qbrush \
qregion \
qpagesize \
qpainter \
qpathclipper \
qpen \

View File

@ -0,0 +1 @@
tst_qpagesize

View File

@ -0,0 +1,9 @@
CONFIG += testcase
CONFIG += parallel_test
TARGET = tst_qpagesize
SOURCES += tst_qpagesize.cpp
QT += testlib
DEFINES += QT_USE_USING_NAMESPACE
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0

View File

@ -0,0 +1,258 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <QtGlobal>
#include <QtAlgorithms>
#include <QtGui/QPageSize>
#ifdef Q_OS_WIN
#include <QtCore/qt_windows.h>
#endif // Q_OS_WIN
class tst_QPageSize : public QObject
{
Q_OBJECT
private slots:
void basics();
void fuzzy();
void custom();
void statics();
};
void tst_QPageSize::basics()
{
// Invalid
QPageSize invalid = QPageSize(QPageSize::Custom);
QCOMPARE(invalid.isValid(), false);
invalid = QPageSize(QSize());
QCOMPARE(invalid.isValid(), false);
invalid = QPageSize(QSizeF(), QPageSize::Millimeter);
QCOMPARE(invalid.isValid(), false);
// Simple QPageSize::PaperSizeId
QPageSize a4 = QPageSize(QPageSize::A4);
QCOMPARE(a4.isValid(), true);
QCOMPARE(a4.key(), QString("A4"));
QCOMPARE(a4.name(), QString("A4"));
QCOMPARE(a4.id(), QPageSize::A4);
#ifdef Q_OS_WIN
QCOMPARE(a4.windowsId(), DMPAPER_A4);
#else
QCOMPARE(a4.windowsId(), 9); // DMPAPER_A4
#endif
QCOMPARE(a4.definitionSize(), QSizeF(210, 297));
QCOMPARE(a4.definitionUnits(), QPageSize::Millimeter);
QCOMPARE(a4.size(QPageSize::Millimeter), QSizeF(210, 297));
QCOMPARE(a4.size(QPageSize::Inch), QSizeF(8.27, 11.69));
QCOMPARE(a4.size(QPageSize::Pica), QSizeF(49.58, 70.17));
QCOMPARE(a4.sizePoints(), QSize(595, 842));
QCOMPARE(a4.sizePixels(72), QSize(595, 842));
QCOMPARE(a4.rect(QPageSize::Millimeter), QRectF(0, 0, 210, 297));
QCOMPARE(a4.rect(QPageSize::Inch), QRectF(0, 0, 8.27, 11.69));
QCOMPARE(a4.rect(QPageSize::Pica), QRectF(0, 0, 49.58, 70.17));
QCOMPARE(a4.rectPoints(), QRect(0, 0, 595, 842));
QCOMPARE(a4.rectPixels(72), QRect(0, 0, 595, 842));
// Simple QPageSize::PaperSizeId later in list
QPageSize folio = QPageSize(QPageSize::Folio);
QCOMPARE(folio.isValid(), true);
QCOMPARE(folio.key(), QString("Folio"));
QCOMPARE(folio.name(), QString("Folio (8.27 x 13 in)"));
QCOMPARE(folio.id(), QPageSize::Folio);
QCOMPARE(folio.definitionSize(), QSizeF(210, 330));
QCOMPARE(folio.definitionUnits(), QPageSize::Millimeter);
QCOMPARE(folio.size(QPageSize::Millimeter), QSizeF(210, 330));
QCOMPARE(folio.sizePoints(), QSize(595, 935));
QCOMPARE(folio.sizePixels(72), QSize(595, 935));
QCOMPARE(folio.size(QPageSize::Inch), QSizeF(8.27, 13));
// Simple QPageSize::PaperSizeId last in list
QPageSize you4 = QPageSize(QPageSize::EnvelopeYou4);
QCOMPARE(you4.isValid(), true);
QCOMPARE(you4.key(), QString("EnvYou4"));
QCOMPARE(you4.name(), QString("Envelope You 4"));
QCOMPARE(you4.id(), QPageSize::EnvelopeYou4);
#ifdef Q_OS_WIN
QCOMPARE(you4.windowsId(), DMPAPER_JENV_YOU4);
#else
QCOMPARE(you4.windowsId(), 91);
#endif
QCOMPARE(you4.size(QPageSize::Millimeter), QSizeF(105, 235));
QCOMPARE(you4.size(QPageSize::Inch), QSizeF(4.13, 9.25));
QCOMPARE(you4.sizePoints(), QSize(298, 666));
QCOMPARE(you4.sizePixels(72), QSize(298, 666));
// Simple QSize in Points
QPageSize a4b = QPageSize(QSize(595, 842));
QCOMPARE(a4b.isValid(), true);
QCOMPARE(a4b.id(), QPageSize::A4);
QCOMPARE(a4b.sizePoints(), QSize(595, 842));
// Simple QSize in Points later in list, custom name
QPageSize folio2 = QPageSize(QSize(595, 935), QStringLiteral("Folio2"));
QCOMPARE(folio2.isValid(), true);
QCOMPARE(folio2.name(), QString("Folio2"));
QCOMPARE(folio2.id(), QPageSize::Folio);
QCOMPARE(folio2.sizePoints(), QSize(595, 935));
// Comparisons
QCOMPARE((a4 == folio), false);
QCOMPARE((a4.isEquivalentTo(folio)), false);
QCOMPARE((a4 == a4b), true);
QCOMPARE((a4.isEquivalentTo(a4b)), true);
QCOMPARE((folio == folio2), false); // Name different
QCOMPARE((folio.isEquivalentTo(folio2)), true);
// Simple QSize in Millimeters
QPageSize folio3 = QPageSize(QSizeF(210, 330), QPageSize::Millimeter);
QCOMPARE(folio3.isValid(), true);
QCOMPARE(folio3.id(), QPageSize::Folio);
QCOMPARE(folio3.sizePoints(), QSize(595, 935));
}
void tst_QPageSize::fuzzy()
{
// Using FuzzyMatch by default
// Simple QSize within 3 Points
QPageSize a4a = QPageSize(QSize(592, 845));
QCOMPARE(a4a.isValid(), true);
QCOMPARE(a4a.id(), QPageSize::A4);
QCOMPARE(a4a.sizePoints(), QSize(595, 842));
// Simple QSizeF within 1mm
QPageSize a4b = QPageSize(QSizeF(211, 298), QPageSize::Millimeter);
QCOMPARE(a4b.isValid(), true);
QCOMPARE(a4b.id(), QPageSize::A4);
QCOMPARE(a4b.size(QPageSize::Millimeter), QSizeF(210, 297));
QCOMPARE(a4b.sizePoints(), QSize(595, 842));
// Using FuzzyOrientationMatch
// Exact A4 in landscape mode
QPageSize a4l = QPageSize(QSize(842, 595));
QCOMPARE(a4l.isValid(), true);
QCOMPARE(a4l.id(), QPageSize::Custom);
QCOMPARE(a4l.sizePoints(), QSize(842, 595));
a4l = QPageSize(QSize(842, 595), QString(), QPageSize::FuzzyOrientationMatch);
QCOMPARE(a4l.isValid(), true);
QCOMPARE(a4l.id(), QPageSize::A4);
QCOMPARE(a4l.sizePoints(), QSize(595, 842));
// Using ExactMatch
// Simple QSize within 3 Points
QPageSize a4d = QPageSize(QSize(592, 845), QString(), QPageSize::ExactMatch);
QCOMPARE(a4d.isValid(), true);
QCOMPARE(a4d.id(), QPageSize::Custom);
QCOMPARE(a4d.sizePoints(), QSize(592, 845));
// Simple QSizeF within 1mm
QPageSize a4e = QPageSize(QSizeF(211, 298), QPageSize::Millimeter, QString(), QPageSize::ExactMatch);
QCOMPARE(a4e.isValid(), true);
QCOMPARE(a4e.id(), QPageSize::Custom);
QCOMPARE(a4e.size(QPageSize::Millimeter), QSizeF(211, 298));
QCOMPARE(a4e.sizePoints(), QSize(598, 845));
}
void tst_QPageSize::custom()
{
// Simple non-standard Points QSize
QPageSize custom1 = QPageSize(QSize(500, 600));
QCOMPARE(custom1.isValid(), true);
QCOMPARE(custom1.key(), QString("Custom.500x600"));
QCOMPARE(custom1.name(), QString("Custom (500pt x 600pt)"));
QCOMPARE(custom1.id(), QPageSize::Custom);
QCOMPARE(custom1.definitionSize(), QSizeF(500, 600));
QCOMPARE(custom1.definitionUnits(), QPageSize::Point);
QCOMPARE(custom1.size(QPageSize::Millimeter), QSizeF(176.39, 211.67));
QCOMPARE(custom1.size(QPageSize::Pica), QSizeF(41.67, 50));
QCOMPARE(custom1.sizePoints(), QSize(500, 600));
QCOMPARE(custom1.sizePixels(72), QSize(500, 600));
// Simple non-standard MM QSizeF
QPageSize custom2 = QPageSize(QSizeF(500.3, 600.57), QPageSize::Millimeter);
QCOMPARE(custom2.isValid(), true);
QCOMPARE(custom2.key(), QString("Custom.500.3x600.57mm"));
QCOMPARE(custom2.name(), QString("Custom (500.3mm x 600.57mm)"));
QCOMPARE(custom2.id(), QPageSize::Custom);
QCOMPARE(custom2.definitionSize(), QSizeF(500.3, 600.57));
QCOMPARE(custom2.definitionUnits(), QPageSize::Millimeter);
QCOMPARE(custom2.size(QPageSize::Millimeter), QSizeF(500.3, 600.57));
QCOMPARE(custom2.size(QPageSize::Pica), QSizeF(118.18, 141.87));
QCOMPARE(custom2.sizePoints(), QSize(1418, 1702));
QCOMPARE(custom2.sizePixels(72), QSize(1418, 1702));
}
void tst_QPageSize::statics()
{
QCOMPARE(QPageSize::key(QPageSize::EnvelopeYou4), QString("EnvYou4"));
QCOMPARE(QPageSize::name(QPageSize::EnvelopeYou4), QString("Envelope You 4"));
#ifdef Q_OS_WIN
QCOMPARE(QPageSize::windowsId(QPageSize::EnvelopeYou4), DMPAPER_JENV_YOU4);
QCOMPARE(QPageSize::id(DMPAPER_JENV_YOU4), QPageSize::EnvelopeYou4);
QCOMPARE(QPageSize::id(DMPAPER_A4_ROTATED), QPageSize::A4);
#else
QCOMPARE(QPageSize::windowsId(QPageSize::EnvelopeYou4), 91);
QCOMPARE(QPageSize::id(91), QPageSize::EnvelopeYou4);
QCOMPARE(QPageSize::id(77), QPageSize::A4);
#endif
QCOMPARE(QPageSize::id(QSize(298, 666)), QPageSize::EnvelopeYou4);
QCOMPARE(QPageSize::id(QSizeF(105, 235), QPageSize::Millimeter), QPageSize::EnvelopeYou4);
QCOMPARE(QPageSize::definitionSize(QPageSize::Letter), QSizeF(8.5, 11));
QCOMPARE(QPageSize::definitionUnits(QPageSize::Letter), QPageSize::Inch);
QCOMPARE(QPageSize::definitionSize(QPageSize::EnvelopeYou4), QSizeF(105, 235));
QCOMPARE(QPageSize::definitionUnits(QPageSize::EnvelopeYou4), QPageSize::Millimeter);
QCOMPARE(QPageSize::size(QPageSize::EnvelopeYou4, QPageSize::Millimeter), QSizeF(105, 235));
QCOMPARE(QPageSize::sizePoints(QPageSize::EnvelopeYou4), QSize(298, 666));
QCOMPARE(QPageSize::sizePixels(QPageSize::EnvelopeYou4, 72), QSize(298, 666));
}
QTEST_APPLESS_MAIN(tst_QPageSize)
#include "tst_qpagesize.moc"