File indexing completed on 2024-06-09 04:20:48

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2011 Jan Hambrecht <jaham@gmx.net>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #ifndef SVGLOADINGCONTEXT_H
0008 #define SVGLOADINGCONTEXT_H
0009 
0010 #include <functional>
0011 #include <QStringList>
0012 #include <QDomDocument>
0013 #include <QScopedPointer>
0014 
0015 #include "kritaflake_export.h"
0016 
0017 class SvgGraphicsContext;
0018 class SvgStyleParser;
0019 class KoDocumentResourceManager;
0020 class KoShape;
0021 class KoColorProfile;
0022 
0023 /// Contains data used for loading svg
0024 class KRITAFLAKE_EXPORT SvgLoadingContext
0025 {
0026 public:
0027     explicit SvgLoadingContext(KoDocumentResourceManager *documentResourceManager);
0028     ~SvgLoadingContext();
0029 
0030     /// Returns the current graphics context
0031     SvgGraphicsContext *currentGC() const;
0032 
0033     /// Pushes a new graphics context to the stack
0034     SvgGraphicsContext *pushGraphicsContext(const QDomElement &element = QDomElement(), bool inherit = true);
0035 
0036     /// Pops the current graphics context from the stack
0037     void popGraphicsContext();
0038 
0039     /// Sets the initial xml base dir, i.e. the directory the svg file is read from
0040     void setInitialXmlBaseDir(const QString &baseDir);
0041 
0042     /// Returns the current xml base dir
0043     QString xmlBaseDir() const;
0044 
0045     /// Constructs an absolute file path from the given href and current xml base directory
0046     QString absoluteFilePath(const QString &href);
0047 
0048     QString relativeFilePath(const QString &href);
0049 
0050     /// Returns the next z-index
0051     int nextZIndex();
0052 
0053     /// Registers a shape so it can be referenced later
0054     void registerShape(const QString &id, KoShape *shape);
0055 
0056     /// Returns shape with specified id
0057     KoShape* shapeById(const QString &id);
0058 
0059     /// Adds a definition for later use
0060     void addDefinition(const QDomElement &element);
0061 
0062     /// Returns the definition with the specified id
0063     QDomElement definition(const QString &id) const;
0064 
0065     /// Checks if a definition with the specified id exists
0066     bool hasDefinition(const QString &id) const;
0067 
0068     /// Adds a css style sheet
0069     void addStyleSheet(const QDomElement &styleSheet);
0070 
0071     /// Returns list of css styles matching to the specified element
0072     QStringList matchingCssStyles(const QDomElement &element) const;
0073 
0074     /// Returns a style parser to parse styles
0075     SvgStyleParser &styleParser();
0076 
0077     /// parses 'color-profile' tag and saves it in the context
0078     void parseProfile(const QDomElement &element);
0079 
0080     /// Return the profiles in the context.
0081     QHash<QString, const KoColorProfile*> profiles();
0082 
0083     bool isRootContext() const;
0084 
0085     typedef std::function<QByteArray(const QString&)> FileFetcherFunc;
0086     void setFileFetcher(FileFetcherFunc func);
0087 
0088     QByteArray fetchExternalFile(const QString &url);
0089 
0090 private:
0091     class Private;
0092     QScopedPointer<Private> d;
0093 };
0094 
0095 #endif // SVGLOADINGCONTEXT_H