| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QSVGTINYDOCUMENT_P_H |
| 5 | #define QSVGTINYDOCUMENT_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include "qsvgstructure_p.h" |
| 19 | #include "qtsvgglobal_p.h" |
| 20 | |
| 21 | #include "QtCore/qrect.h" |
| 22 | #include "QtCore/qlist.h" |
| 23 | #include "QtCore/qhash.h" |
| 24 | #include "QtCore/qdatetime.h" |
| 25 | #include "QtCore/qxmlstream.h" |
| 26 | #include "qsvgstyle_p.h" |
| 27 | #include "qsvgfont_p.h" |
| 28 | |
| 29 | QT_BEGIN_NAMESPACE |
| 30 | |
| 31 | class QPainter; |
| 32 | class QByteArray; |
| 33 | class QSvgFont; |
| 34 | class QTransform; |
| 35 | |
| 36 | class Q_SVG_EXPORT QSvgTinyDocument : public QSvgStructureNode |
| 37 | { |
| 38 | public: |
| 39 | static QSvgTinyDocument *load(const QString &file, QtSvg::Options options = {}); |
| 40 | static QSvgTinyDocument *load(const QByteArray &contents, QtSvg::Options options = {}); |
| 41 | static QSvgTinyDocument *load(QXmlStreamReader *contents, QtSvg::Options options = {}); |
| 42 | static bool isLikelySvg(QIODevice *device, bool *isCompressed = nullptr); |
| 43 | public: |
| 44 | QSvgTinyDocument(QtSvg::Options options); |
| 45 | ~QSvgTinyDocument(); |
| 46 | Type type() const override; |
| 47 | |
| 48 | inline QSize size() const; |
| 49 | void setWidth(int len, bool percent); |
| 50 | void setHeight(int len, bool percent); |
| 51 | inline int width() const; |
| 52 | inline int height() const; |
| 53 | inline bool widthPercent() const; |
| 54 | inline bool heightPercent() const; |
| 55 | |
| 56 | inline bool preserveAspectRatio() const; |
| 57 | void setPreserveAspectRatio(bool on); |
| 58 | |
| 59 | inline QRectF viewBox() const; |
| 60 | void setViewBox(const QRectF &rect); |
| 61 | |
| 62 | QtSvg::Options options() const; |
| 63 | |
| 64 | void drawCommand(QPainter *, QSvgExtraStates &) override; |
| 65 | |
| 66 | void draw(QPainter *p); |
| 67 | void draw(QPainter *p, const QRectF &bounds); |
| 68 | void draw(QPainter *p, const QString &id, |
| 69 | const QRectF &bounds=QRectF()); |
| 70 | |
| 71 | QTransform transformForElement(const QString &id) const; |
| 72 | QRectF boundsOnElement(const QString &id) const; |
| 73 | bool elementExists(const QString &id) const; |
| 74 | |
| 75 | void addSvgFont(QSvgFont *); |
| 76 | QSvgFont *svgFont(const QString &family) const; |
| 77 | void addNamedNode(const QString &id, QSvgNode *node); |
| 78 | QSvgNode *namedNode(const QString &id) const; |
| 79 | void addNamedStyle(const QString &id, QSvgPaintStyleProperty *style); |
| 80 | QSvgPaintStyleProperty *namedStyle(const QString &id) const; |
| 81 | |
| 82 | void restartAnimation(); |
| 83 | inline int currentElapsed() const; |
| 84 | bool animated() const; |
| 85 | void setAnimated(bool a); |
| 86 | inline int animationDuration() const; |
| 87 | int currentFrame() const; |
| 88 | void setCurrentFrame(int); |
| 89 | void setFramesPerSecond(int num); |
| 90 | |
| 91 | private: |
| 92 | void mapSourceToTarget(QPainter *p, const QRectF &targetRect, const QRectF &sourceRect = QRectF()); |
| 93 | private: |
| 94 | QSize m_size; |
| 95 | bool m_widthPercent; |
| 96 | bool m_heightPercent; |
| 97 | |
| 98 | mutable bool m_implicitViewBox = true; |
| 99 | mutable QRectF m_viewBox; |
| 100 | bool m_preserveAspectRatio = false; |
| 101 | |
| 102 | QHash<QString, QSvgRefCounter<QSvgFont> > m_fonts; |
| 103 | QHash<QString, QSvgNode *> m_namedNodes; |
| 104 | QHash<QString, QSvgRefCounter<QSvgPaintStyleProperty> > m_namedStyles; |
| 105 | |
| 106 | qint64 m_time; |
| 107 | bool m_animated; |
| 108 | int m_animationDuration; |
| 109 | int m_fps; |
| 110 | |
| 111 | QSvgExtraStates m_states; |
| 112 | |
| 113 | const QtSvg::Options m_options; |
| 114 | }; |
| 115 | |
| 116 | Q_SVG_EXPORT QDebug operator<<(QDebug debug, const QSvgTinyDocument &doc); |
| 117 | |
| 118 | inline QSize QSvgTinyDocument::size() const |
| 119 | { |
| 120 | if (m_size.isEmpty()) |
| 121 | return viewBox().size().toSize(); |
| 122 | if (m_widthPercent || m_heightPercent) { |
| 123 | const int width = m_widthPercent ? qRound(d: 0.01 * m_size.width() * viewBox().size().width()) : m_size.width(); |
| 124 | const int height = m_heightPercent ? qRound(d: 0.01 * m_size.height() * viewBox().size().height()) : m_size.height(); |
| 125 | return QSize(width, height); |
| 126 | } |
| 127 | return m_size; |
| 128 | } |
| 129 | |
| 130 | inline int QSvgTinyDocument::width() const |
| 131 | { |
| 132 | return size().width(); |
| 133 | } |
| 134 | |
| 135 | inline int QSvgTinyDocument::height() const |
| 136 | { |
| 137 | return size().height(); |
| 138 | } |
| 139 | |
| 140 | inline bool QSvgTinyDocument::widthPercent() const |
| 141 | { |
| 142 | return m_widthPercent; |
| 143 | } |
| 144 | |
| 145 | inline bool QSvgTinyDocument::heightPercent() const |
| 146 | { |
| 147 | return m_heightPercent; |
| 148 | } |
| 149 | |
| 150 | inline QRectF QSvgTinyDocument::viewBox() const |
| 151 | { |
| 152 | if (m_viewBox.isNull()) { |
| 153 | m_viewBox = bounds(); |
| 154 | m_implicitViewBox = true; |
| 155 | } |
| 156 | |
| 157 | return m_viewBox; |
| 158 | } |
| 159 | |
| 160 | inline bool QSvgTinyDocument::preserveAspectRatio() const |
| 161 | { |
| 162 | return m_preserveAspectRatio; |
| 163 | } |
| 164 | |
| 165 | inline int QSvgTinyDocument::currentElapsed() const |
| 166 | { |
| 167 | return QDateTime::currentMSecsSinceEpoch() - m_time; |
| 168 | } |
| 169 | |
| 170 | inline int QSvgTinyDocument::animationDuration() const |
| 171 | { |
| 172 | return m_animationDuration; |
| 173 | } |
| 174 | |
| 175 | QT_END_NAMESPACE |
| 176 | |
| 177 | #endif // QSVGTINYDOCUMENT_P_H |
| 178 | |