summaryrefslogtreecommitdiffstats
path: root/src/svg/qsvgdebug.cpp
blob: d04477d9bb9e104e8b955864e54e98ac6d3b8ad8 (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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qsvgvisitor_p.h"
#include <QDebug>

QT_BEGIN_NAMESPACE

static const char *nodeTypeStrings[] = {
    "DOC",
    "G",
    "DEFS",
    "SWITCH",
    "ANIMATION",
    "ARC",
    "CIRCLE",
    "ELLIPSE",
    "IMAGE",
    "LINE",
    "PATH",
    "POLYGON",
    "POLYLINE",
    "RECT",
    "TEXT",
    "TEXTAREA",
    "TSPAN",
    "USE",
    "VIDEO"
};

// TODO: something like this is needed in several places. Make a common version.
static const char *typeName(const QSvgNode *node)
{
    constexpr int typeNameCount = sizeof(nodeTypeStrings) / sizeof(const char *);
    if (node->type() < typeNameCount)
        return nodeTypeStrings[node->type()];
    return "UNKNOWN";
}

class SvgDebugVisitor : public QSvgVisitor
{
public:
    SvgDebugVisitor(QDebug &stream) : debug(stream) {}
    void write(const QSvgTinyDocument *doc);

protected:
    void visitNode(const QSvgNode *) override;
    bool visitStructureNodeStart(const QSvgStructureNode *node) override;
    void visitStructureNodeEnd(const QSvgStructureNode *) override;
    void visitAnimateNode(const QSvgAnimateNode *node) override;
    void visitEllipseNode(const QSvgEllipse *node) override;
    void visitImageNode(const QSvgImage *node) override;
    void visitLineNode(const QSvgLine *node) override;
    void visitPathNode(const QSvgPath *node) override;
    void visitPolygonNode(const QSvgPolygon *node) override;
    void visitPolylineNode(const QSvgPolyline *node) override;
    void visitRectNode(const QSvgRect *node) override;
    void visitTextNode(const QSvgText *node) override;
    void visitUseNode(const QSvgUse *node) override;
    void visitVideoNode(const QSvgVideo *node) override;

private:
    const char *indent() { m_indent.fill(' ', m_indentLevel * 2);  return m_indent.constData();}
    void handleBaseNode(const QSvgNode *node);
    QDebug &debug;
    int m_indentLevel = 0;
    QByteArray m_indent;
    int nodeCounter = 0;
};

void SvgDebugVisitor::handleBaseNode(const QSvgNode *node)
{
    debug << indent() << typeName(node) << "node, ID:" << node->nodeId();
    nodeCounter++;
}

void SvgDebugVisitor::visitNode(const QSvgNode *node)
{
    handleBaseNode(node);
    debug << Qt::endl;
}

bool SvgDebugVisitor::visitStructureNodeStart(const QSvgStructureNode *node)
{
    debug << indent() << "START node" << node->nodeId() << "type" << typeName(node) << node->type() << Qt::endl;
    m_indentLevel++;
    return true;
}

void SvgDebugVisitor::visitStructureNodeEnd(const QSvgStructureNode *node)
{
    m_indentLevel--;
    debug << indent() << "END node" << node->nodeId() << Qt::endl;
}

void SvgDebugVisitor::visitAnimateNode(const QSvgAnimateNode *node)
{
    handleBaseNode(node);
    debug << Qt::endl;
}

void SvgDebugVisitor::visitEllipseNode(const QSvgEllipse *node)
{
    handleBaseNode(node);
    debug << "rect:" << node->rect() << Qt::endl;
}

void SvgDebugVisitor::visitImageNode(const QSvgImage *node)
{
    handleBaseNode(node);
    debug << "image:" << node->image() << Qt::endl;
}

void SvgDebugVisitor::visitLineNode(const QSvgLine *node)
{
    handleBaseNode(node);
    debug << "line:" << node->line() << Qt::endl;
}

void SvgDebugVisitor::visitPathNode(const QSvgPath *node)
{
    handleBaseNode(node);
    debug << "path:" << node->path().elementCount() << "elements." << Qt::endl;
}

void SvgDebugVisitor::visitPolygonNode(const QSvgPolygon *node)
{
    handleBaseNode(node);
    debug << "polygon:" << node->polygon().size() << "elements." << Qt::endl;
}

void SvgDebugVisitor::visitPolylineNode(const QSvgPolyline *node)
{
    handleBaseNode(node);
    debug << "polygon:" << node->polygon().size() << "elements." << Qt::endl;
}

void SvgDebugVisitor::visitRectNode(const QSvgRect *node)
{
    handleBaseNode(node);
    debug << "rect:" << node->rect() << "radius:" << node->radius() << Qt::endl;
}

void SvgDebugVisitor::visitTextNode(const QSvgText *node)
{
    handleBaseNode(node);
    QString text;
    for (const auto *tspan : node->tspans()) {
        if (!tspan)
            text += QStringLiteral("\\n");
        else
            text += tspan->text();
    }
    debug << "text:" << text << Qt::endl;
}

void SvgDebugVisitor::visitUseNode(const QSvgUse *node)
{
    handleBaseNode(node);
    debug << "link ID:" << node->linkId() << Qt::endl;
}

void SvgDebugVisitor::visitVideoNode(const QSvgVideo *node)
{
    handleBaseNode(node);
    debug << Qt::endl;
}

void SvgDebugVisitor::write(const QSvgTinyDocument *doc)
{
    debug << "SVG" << doc->size() << "viewBox" << doc->viewBox() << Qt::endl;
    traverse(doc);

    debug << "END SVG" << nodeCounter << "nodes";
}

QDebug operator<<(QDebug debug, const QSvgTinyDocument &doc)
{
    SvgDebugVisitor visitor(debug);
    visitor.write(&doc);

    return debug;
}

QT_END_NAMESPACE