| 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 Qt SVG module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 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 Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qsvgnode_p.h" |
| 41 | #include "qsvgtinydocument_p.h" |
| 42 | |
| 43 | #include "qdebug.h" |
| 44 | #include "qstack.h" |
| 45 | |
| 46 | QT_BEGIN_NAMESPACE |
| 47 | |
| 48 | QSvgNode::QSvgNode(QSvgNode *parent) |
| 49 | : m_parent(parent), |
| 50 | m_visible(true), |
| 51 | m_displayMode(BlockMode) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | QSvgNode::~QSvgNode() |
| 56 | { |
| 57 | |
| 58 | } |
| 59 | |
| 60 | bool QSvgNode::isDescendantOf(const QSvgNode *parent) const |
| 61 | { |
| 62 | const QSvgNode *n = this; |
| 63 | while (n) { |
| 64 | if (n == parent) |
| 65 | return true; |
| 66 | n = n->m_parent; |
| 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | void QSvgNode::appendStyleProperty(QSvgStyleProperty *prop, const QString &id) |
| 72 | { |
| 73 | //qDebug()<<"appending "<<prop->type()<< " ("<< id <<") "<<"to "<<this<<this->type(); |
| 74 | QSvgTinyDocument *doc; |
| 75 | switch (prop->type()) { |
| 76 | case QSvgStyleProperty::QUALITY: |
| 77 | m_style.quality = static_cast<QSvgQualityStyle*>(prop); |
| 78 | break; |
| 79 | case QSvgStyleProperty::FILL: |
| 80 | m_style.fill = static_cast<QSvgFillStyle*>(prop); |
| 81 | break; |
| 82 | case QSvgStyleProperty::VIEWPORT_FILL: |
| 83 | m_style.viewportFill = static_cast<QSvgViewportFillStyle*>(prop); |
| 84 | break; |
| 85 | case QSvgStyleProperty::FONT: |
| 86 | m_style.font = static_cast<QSvgFontStyle*>(prop); |
| 87 | break; |
| 88 | case QSvgStyleProperty::STROKE: |
| 89 | m_style.stroke = static_cast<QSvgStrokeStyle*>(prop); |
| 90 | break; |
| 91 | case QSvgStyleProperty::SOLID_COLOR: |
| 92 | m_style.solidColor = static_cast<QSvgSolidColorStyle*>(prop); |
| 93 | doc = document(); |
| 94 | if (doc && !id.isEmpty()) |
| 95 | doc->addNamedStyle(id, style: m_style.solidColor); |
| 96 | break; |
| 97 | case QSvgStyleProperty::GRADIENT: |
| 98 | m_style.gradient = static_cast<QSvgGradientStyle*>(prop); |
| 99 | doc = document(); |
| 100 | if (doc && !id.isEmpty()) |
| 101 | doc->addNamedStyle(id, style: m_style.gradient); |
| 102 | break; |
| 103 | case QSvgStyleProperty::TRANSFORM: |
| 104 | m_style.transform = static_cast<QSvgTransformStyle*>(prop); |
| 105 | break; |
| 106 | case QSvgStyleProperty::ANIMATE_COLOR: |
| 107 | m_style.animateColor = static_cast<QSvgAnimateColor*>(prop); |
| 108 | break; |
| 109 | case QSvgStyleProperty::ANIMATE_TRANSFORM: |
| 110 | m_style.animateTransforms.append( |
| 111 | t: static_cast<QSvgAnimateTransform*>(prop)); |
| 112 | break; |
| 113 | case QSvgStyleProperty::OPACITY: |
| 114 | m_style.opacity = static_cast<QSvgOpacityStyle*>(prop); |
| 115 | break; |
| 116 | case QSvgStyleProperty::COMP_OP: |
| 117 | m_style.compop = static_cast<QSvgCompOpStyle*>(prop); |
| 118 | break; |
| 119 | default: |
| 120 | qDebug(msg: "QSvgNode: Trying to append unknown property!" ); |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void QSvgNode::(QPainter *p, QSvgExtraStates &states) const |
| 126 | { |
| 127 | m_style.apply(p, node: this, states); |
| 128 | } |
| 129 | |
| 130 | void QSvgNode::(QPainter *p, QSvgExtraStates &states) const |
| 131 | { |
| 132 | m_style.revert(p, states); |
| 133 | } |
| 134 | |
| 135 | QSvgStyleProperty * QSvgNode::styleProperty(QSvgStyleProperty::Type type) const |
| 136 | { |
| 137 | const QSvgNode *node = this; |
| 138 | while (node) { |
| 139 | switch (type) { |
| 140 | case QSvgStyleProperty::QUALITY: |
| 141 | if (node->m_style.quality) |
| 142 | return node->m_style.quality; |
| 143 | break; |
| 144 | case QSvgStyleProperty::FILL: |
| 145 | if (node->m_style.fill) |
| 146 | return node->m_style.fill; |
| 147 | break; |
| 148 | case QSvgStyleProperty::VIEWPORT_FILL: |
| 149 | if (m_style.viewportFill) |
| 150 | return node->m_style.viewportFill; |
| 151 | break; |
| 152 | case QSvgStyleProperty::FONT: |
| 153 | if (node->m_style.font) |
| 154 | return node->m_style.font; |
| 155 | break; |
| 156 | case QSvgStyleProperty::STROKE: |
| 157 | if (node->m_style.stroke) |
| 158 | return node->m_style.stroke; |
| 159 | break; |
| 160 | case QSvgStyleProperty::SOLID_COLOR: |
| 161 | if (node->m_style.solidColor) |
| 162 | return node->m_style.solidColor; |
| 163 | break; |
| 164 | case QSvgStyleProperty::GRADIENT: |
| 165 | if (node->m_style.gradient) |
| 166 | return node->m_style.gradient; |
| 167 | break; |
| 168 | case QSvgStyleProperty::TRANSFORM: |
| 169 | if (node->m_style.transform) |
| 170 | return node->m_style.transform; |
| 171 | break; |
| 172 | case QSvgStyleProperty::ANIMATE_COLOR: |
| 173 | if (node->m_style.animateColor) |
| 174 | return node->m_style.animateColor; |
| 175 | break; |
| 176 | case QSvgStyleProperty::ANIMATE_TRANSFORM: |
| 177 | if (!node->m_style.animateTransforms.isEmpty()) |
| 178 | return node->m_style.animateTransforms.first(); |
| 179 | break; |
| 180 | case QSvgStyleProperty::OPACITY: |
| 181 | if (node->m_style.opacity) |
| 182 | return node->m_style.opacity; |
| 183 | break; |
| 184 | case QSvgStyleProperty::COMP_OP: |
| 185 | if (node->m_style.compop) |
| 186 | return node->m_style.compop; |
| 187 | break; |
| 188 | default: |
| 189 | break; |
| 190 | } |
| 191 | node = node->parent(); |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | QSvgFillStyleProperty * QSvgNode::styleProperty(const QString &id) const |
| 198 | { |
| 199 | QString rid = id; |
| 200 | if (rid.startsWith(c: QLatin1Char('#'))) |
| 201 | rid.remove(i: 0, len: 1); |
| 202 | QSvgTinyDocument *doc = document(); |
| 203 | return doc ? doc->namedStyle(id: rid) : 0; |
| 204 | } |
| 205 | |
| 206 | QRectF QSvgNode::(QPainter *, QSvgExtraStates &) const |
| 207 | { |
| 208 | return QRectF(0, 0, 0, 0); |
| 209 | } |
| 210 | |
| 211 | QRectF QSvgNode::transformedBounds() const |
| 212 | { |
| 213 | if (!m_cachedBounds.isEmpty()) |
| 214 | return m_cachedBounds; |
| 215 | |
| 216 | QImage dummy(1, 1, QImage::Format_RGB32); |
| 217 | QPainter p(&dummy); |
| 218 | QSvgExtraStates states; |
| 219 | |
| 220 | QPen pen(Qt::NoBrush, 1, Qt::SolidLine, Qt::FlatCap, Qt::SvgMiterJoin); |
| 221 | pen.setMiterLimit(4); |
| 222 | p.setPen(pen); |
| 223 | |
| 224 | QStack<QSvgNode*> parentApplyStack; |
| 225 | QSvgNode *parent = m_parent; |
| 226 | while (parent) { |
| 227 | parentApplyStack.push(t: parent); |
| 228 | parent = parent->parent(); |
| 229 | } |
| 230 | |
| 231 | for (int i = parentApplyStack.size() - 1; i >= 0; --i) |
| 232 | parentApplyStack[i]->applyStyle(p: &p, states); |
| 233 | |
| 234 | p.setWorldTransform(matrix: QTransform()); |
| 235 | |
| 236 | m_cachedBounds = transformedBounds(p: &p, states); |
| 237 | return m_cachedBounds; |
| 238 | } |
| 239 | |
| 240 | QSvgTinyDocument * QSvgNode::document() const |
| 241 | { |
| 242 | QSvgTinyDocument *doc = 0; |
| 243 | QSvgNode *node = const_cast<QSvgNode*>(this); |
| 244 | while (node && node->type() != QSvgNode::DOC) { |
| 245 | node = node->parent(); |
| 246 | } |
| 247 | doc = static_cast<QSvgTinyDocument*>(node); |
| 248 | |
| 249 | return doc; |
| 250 | } |
| 251 | |
| 252 | void QSvgNode::setRequiredFeatures(const QStringList &lst) |
| 253 | { |
| 254 | m_requiredFeatures = lst; |
| 255 | } |
| 256 | |
| 257 | const QStringList & QSvgNode::requiredFeatures() const |
| 258 | { |
| 259 | return m_requiredFeatures; |
| 260 | } |
| 261 | |
| 262 | void QSvgNode::setRequiredExtensions(const QStringList &lst) |
| 263 | { |
| 264 | m_requiredExtensions = lst; |
| 265 | } |
| 266 | |
| 267 | const QStringList & QSvgNode::requiredExtensions() const |
| 268 | { |
| 269 | return m_requiredExtensions; |
| 270 | } |
| 271 | |
| 272 | void QSvgNode::setRequiredLanguages(const QStringList &lst) |
| 273 | { |
| 274 | m_requiredLanguages = lst; |
| 275 | } |
| 276 | |
| 277 | const QStringList & QSvgNode::requiredLanguages() const |
| 278 | { |
| 279 | return m_requiredLanguages; |
| 280 | } |
| 281 | |
| 282 | void QSvgNode::setRequiredFormats(const QStringList &lst) |
| 283 | { |
| 284 | m_requiredFormats = lst; |
| 285 | } |
| 286 | |
| 287 | const QStringList & QSvgNode::requiredFormats() const |
| 288 | { |
| 289 | return m_requiredFormats; |
| 290 | } |
| 291 | |
| 292 | void QSvgNode::setRequiredFonts(const QStringList &lst) |
| 293 | { |
| 294 | m_requiredFonts = lst; |
| 295 | } |
| 296 | |
| 297 | const QStringList & QSvgNode::requiredFonts() const |
| 298 | { |
| 299 | return m_requiredFonts; |
| 300 | } |
| 301 | |
| 302 | void QSvgNode::setVisible(bool visible) |
| 303 | { |
| 304 | //propagate visibility change of true to the parent |
| 305 | //not propagating false is just a small performance |
| 306 | //degradation since we'll iterate over children without |
| 307 | //drawing any of them |
| 308 | if (m_parent && visible && !m_parent->isVisible()) |
| 309 | m_parent->setVisible(true); |
| 310 | |
| 311 | m_visible = visible; |
| 312 | } |
| 313 | |
| 314 | QRectF QSvgNode::(QPainter *p, QSvgExtraStates &states) const |
| 315 | { |
| 316 | applyStyle(p, states); |
| 317 | QRectF rect = bounds(p, states); |
| 318 | revertStyle(p, states); |
| 319 | return rect; |
| 320 | } |
| 321 | |
| 322 | void QSvgNode::setNodeId(const QString &i) |
| 323 | { |
| 324 | m_id = i; |
| 325 | } |
| 326 | |
| 327 | void QSvgNode::setXmlClass(const QString &str) |
| 328 | { |
| 329 | m_class = str; |
| 330 | } |
| 331 | |
| 332 | void QSvgNode::setDisplayMode(DisplayMode mode) |
| 333 | { |
| 334 | m_displayMode = mode; |
| 335 | } |
| 336 | |
| 337 | QSvgNode::DisplayMode QSvgNode::displayMode() const |
| 338 | { |
| 339 | return m_displayMode; |
| 340 | } |
| 341 | |
| 342 | qreal QSvgNode::strokeWidth(QPainter *p) |
| 343 | { |
| 344 | const QPen &pen = p->pen(); |
| 345 | if (pen.style() == Qt::NoPen || pen.brush().style() == Qt::NoBrush || pen.isCosmetic()) |
| 346 | return 0; |
| 347 | return pen.widthF(); |
| 348 | } |
| 349 | |
| 350 | QT_END_NAMESPACE |
| 351 | |