Krita Source Code Documentation
Loading...
Searching...
No Matches
SvgLoadingContext Class Reference

Contains data used for loading svg. More...

#include <SvgLoadingContext.h>

+ Inheritance diagram for SvgLoadingContext:

Public Types

typedef std::function< QByteArray(const QString &) FileFetcherFunc)
 

Public Member Functions

QString absoluteFilePath (const QString &href)
 Constructs an absolute file path from the given href and current xml base directory.
 
void addDefinition (const QDomElement &element)
 Adds a definition for later use.
 
void addStyleSheet (const QDomElement &styleSheet)
 Adds a css style sheet.
 
SvgGraphicsContextcurrentGC () const
 Returns the current graphics context.
 
QDomElement definition (const QString &id) const
 Returns the definition with the specified id.
 
QByteArray fetchExternalFile (const QString &url)
 
bool hasDefinition (const QString &id) const
 Checks if a definition with the specified id exists.
 
bool isRootContext () const
 
QStringList matchingCssStyles (const QDomElement &element) const
 Returns list of css styles matching to the specified element.
 
int nextZIndex ()
 Returns the next z-index.
 
void parseProfile (const QDomElement &element)
 parses 'color-profile' tag and saves it in the context
 
void popGraphicsContext ()
 Pops the current graphics context from the stack.
 
 Private ()
 
QHash< QString, const KoColorProfile * > profiles ()
 Return the profiles in the context.
 
SvgGraphicsContextpushGraphicsContext (const QDomElement &element=QDomElement(), bool inherit=true)
 Pushes a new graphics context to the stack.
 
void registerShape (const QString &id, KoShape *shape)
 Registers a shape so it can be referenced later.
 
QString relativeFilePath (const QString &href)
 
KoSvgTextProperties resolvedProperties () const
 These are the text properties, completely resolved, ensuring that everything is inherited and the sizes are converted to pt.
 
void setFileFetcher (FileFetcherFunc func)
 
void setInitialXmlBaseDir (const QString &baseDir)
 Sets the initial xml base dir, i.e. the directory the svg file is read from.
 
KoShapeshapeById (const QString &id)
 Returns shape with specified id.
 
SvgStyleParserstyleParser ()
 Returns a style parser to parse styles.
 
 SvgLoadingContext (KoDocumentResourceManager *documentResourceManager)
 
QString xmlBaseDir () const
 Returns the current xml base dir.
 
 ~Private ()
 
 ~SvgLoadingContext ()
 

Public Attributes

SvgCssHelper cssStyles
 
QHash< QString, QDomElement > definitions
 
KoDocumentResourceManagerdocumentResourceManager
 
FileFetcherFunc fileFetcher
 
QStack< SvgGraphicsContext * > gcStack
 
QString initialXmlBaseDir
 
QHash< QString, KoShape * > loadedShapes
 
QHash< QString, const KoColorProfile * > profiles
 
SvgStyleParserstyleParser
 
int zIndex
 

Private Attributes

QScopedPointer< Privated
 
- Private Attributes inherited from Private
KisCanvas2canvas
 
int displayedFrame
 
int intendedFrame
 

Additional Inherited Members

- Private Member Functions inherited from Private
 Private (KisCanvas2 *c)
 

Detailed Description

Contains data used for loading svg.

Definition at line 27 of file SvgLoadingContext.cpp.

Member Typedef Documentation

◆ FileFetcherFunc

typedef std::function<QByteArray(const QString&) SvgLoadingContext::FileFetcherFunc)

Definition at line 89 of file SvgLoadingContext.h.

Constructor & Destructor Documentation

◆ ~Private()

SvgLoadingContext::~Private ( )
inline

Definition at line 38 of file SvgLoadingContext.cpp.

39 {
40 if (! gcStack.isEmpty() && !gcStack.top()->isResolutionFrame) {
41 // Resolution frame is usually the first and is not removed.
42 warnFlake << "the context stack is not empty (current count" << gcStack.size() << ", expected 0)";
43 }
44 qDeleteAll(gcStack);
45 gcStack.clear();
46 delete styleParser;
47 }
#define warnFlake
Definition FlakeDebug.h:16
QStack< SvgGraphicsContext * > gcStack
SvgStyleParser * styleParser

References warnFlake.

◆ SvgLoadingContext()

SvgLoadingContext::SvgLoadingContext ( KoDocumentResourceManager * documentResourceManager)
explicit

Definition at line 60 of file SvgLoadingContext.cpp.

61 : d(new Private())
62{
63 d->documentResourceManager = documentResourceManager;
64 d->styleParser = new SvgStyleParser(*this);
65 Q_ASSERT(d->documentResourceManager);
66}
KoDocumentResourceManager * documentResourceManager
QScopedPointer< Private > d

References d, and documentResourceManager.

◆ ~SvgLoadingContext()

SvgLoadingContext::~SvgLoadingContext ( )

Definition at line 68 of file SvgLoadingContext.cpp.

69{
70}

Member Function Documentation

◆ absoluteFilePath()

QString SvgLoadingContext::absoluteFilePath ( const QString & href)

Constructs an absolute file path from the given href and current xml base directory.

Definition at line 136 of file SvgLoadingContext.cpp.

137{
138 QFileInfo info(href);
139 if (! info.isRelative())
140 return href;
141
143 if (!gc)
144 return d->initialXmlBaseDir;
145
146 QString baseDir = d->initialXmlBaseDir;
147 if (! gc->xmlBaseDir.isEmpty())
148 baseDir = absoluteFilePath(gc->xmlBaseDir);
149
150 QFileInfo pathInfo(QFileInfo(baseDir).filePath());
151
152 QString relFile = href;
153 while (relFile.startsWith(QLatin1String("../"))) {
154 relFile.remove(0, 3);
155 pathInfo.setFile(pathInfo.dir(), QString());
156 }
157
158 QString absFile = pathInfo.absolutePath() + '/' + relFile;
159
160 return absFile;
161}
QString xmlBaseDir
the current base directory (used for loading external content)
QString absoluteFilePath(const QString &href)
Constructs an absolute file path from the given href and current xml base directory.
SvgGraphicsContext * currentGC() const
Returns the current graphics context.

References absoluteFilePath(), currentGC(), d, and SvgGraphicsContext::xmlBaseDir.

◆ addDefinition()

void SvgLoadingContext::addDefinition ( const QDomElement & element)

Adds a definition for later use.

Definition at line 200 of file SvgLoadingContext.cpp.

201{
202 const QString id = element.attribute("id");
203 if (id.isEmpty() || d->definitions.contains(id))
204 return;
205 d->definitions.insert(id, element);
206}

References d.

◆ addStyleSheet()

void SvgLoadingContext::addStyleSheet ( const QDomElement & styleSheet)

Adds a css style sheet.

Definition at line 218 of file SvgLoadingContext.cpp.

219{
220 d->cssStyles.parseStylesheet(styleSheet);
221}

References d.

◆ currentGC()

SvgGraphicsContext * SvgLoadingContext::currentGC ( ) const

Returns the current graphics context.

Definition at line 72 of file SvgLoadingContext.cpp.

73{
74 if (d->gcStack.isEmpty())
75 return 0;
76
77 return d->gcStack.top();
78}

References d.

◆ definition()

QDomElement SvgLoadingContext::definition ( const QString & id) const

Returns the definition with the specified id.

Definition at line 208 of file SvgLoadingContext.cpp.

209{
210 return d->definitions.value(id);
211}

References d.

◆ fetchExternalFile()

QByteArray SvgLoadingContext::fetchExternalFile ( const QString & url)

Definition at line 308 of file SvgLoadingContext.cpp.

309{
310 return d->fileFetcher ? d->fileFetcher(url) : QByteArray();
311}

References d.

◆ hasDefinition()

bool SvgLoadingContext::hasDefinition ( const QString & id) const

Checks if a definition with the specified id exists.

Definition at line 213 of file SvgLoadingContext.cpp.

214{
215 return d->definitions.contains(id);
216}

References d.

◆ isRootContext()

bool SvgLoadingContext::isRootContext ( ) const

Definition at line 297 of file SvgLoadingContext.cpp.

298{
299 KIS_ASSERT(!d->gcStack.isEmpty());
300 return d->gcStack.size() == 1;
301}
#define KIS_ASSERT(cond)
Definition kis_assert.h:33

References d, and KIS_ASSERT.

◆ matchingCssStyles()

QStringList SvgLoadingContext::matchingCssStyles ( const QDomElement & element) const

Returns list of css styles matching to the specified element.

Definition at line 223 of file SvgLoadingContext.cpp.

224{
225 return d->cssStyles.matchStyles(element);
226}

References d.

◆ nextZIndex()

int SvgLoadingContext::nextZIndex ( )

Returns the next z-index.

Definition at line 184 of file SvgLoadingContext.cpp.

185{
186 return d->zIndex++;
187}

References d.

◆ parseProfile()

void SvgLoadingContext::parseProfile ( const QDomElement & element)

parses 'color-profile' tag and saves it in the context

Definition at line 233 of file SvgLoadingContext.cpp.

234{
235 const QString href = element.attribute("xlink:href");
236 const QByteArray uniqueId = QByteArray::fromHex(element.attribute("local").toLatin1());
237 const QString name = element.attribute("name");
238
239 if (element.attribute("rendering-intent", "auto") != "auto") {
240 // WARNING: Krita does *not* treat rendering intents attributes of the profile!
241 warnFlake << "WARNING: we do *not* treat rendering intents attributes of the profile!";
242 }
243
244 if (d->profiles.contains(name)) {
245 debugFlake << "Profile already in the map!" << ppVar(name);
246 return;
247 }
248
249 const KoColorProfile *profile =
251
252 if (!profile && d->fileFetcher) {
254 KIS_ASSERT(engine);
255 if (engine) {
256 const QString fileName = relativeFilePath(href);
257 const QByteArray profileData = d->fileFetcher(fileName);
258 if (!profileData.isEmpty()) {
259 profile = engine->addProfile(profileData);
260
261 if (profile->uniqueId() != uniqueId) {
262 warnFlake << "WARNING: ProfileID of the attached profile doesn't match the one mentioned in SVG element";
263 warnFlake << " " << ppVar(profile->uniqueId().toHex());
264 warnFlake << " " << ppVar(uniqueId.toHex());
265 }
266 } else {
267 warnFlake << "WARNING: couldn't fetch the ICCprofile file!" << fileName;
268 }
269 }
270 }
271
272 if (profile) {
273 d->profiles.insert(name, profile);
274 } else {
275 warnFlake << "WARNING: couldn't load SVG profile" << ppVar(name) << ppVar(href) << ppVar(uniqueId);
276 }
277}
#define debugFlake
Definition FlakeDebug.h:15
static KoColorSpaceEngineRegistry * instance()
T get(const QString &id) const
QString relativeFilePath(const QString &href)
#define ppVar(var)
Definition kis_debug.h:155
const char * name(StandardAction id)
virtual QByteArray uniqueId() const =0
virtual const KoColorProfile * addProfile(const QString &filename)=0
static KoColorSpaceRegistry * instance()
const KoColorProfile * profileByUniqueId(const QByteArray &id) const

References KoColorSpaceEngine::addProfile(), d, debugFlake, KoGenericRegistry< T >::get(), KoColorSpaceEngineRegistry::instance(), KoColorSpaceRegistry::instance(), KIS_ASSERT, ppVar, KoColorSpaceRegistry::profileByUniqueId(), relativeFilePath(), KoColorProfile::uniqueId(), and warnFlake.

◆ popGraphicsContext()

void SvgLoadingContext::popGraphicsContext ( )

Pops the current graphics context from the stack.

Definition at line 120 of file SvgLoadingContext.cpp.

121{
122 delete(d->gcStack.pop());
123}

References d.

◆ Private()

SvgLoadingContext::Private ( )
inline

Definition at line 30 of file SvgLoadingContext.cpp.

31 : zIndex(0)
33 , styleParser(0)
34 {
35
36 }

◆ profiles()

QHash< QString, const KoColorProfile * > SvgLoadingContext::profiles ( )

Return the profiles in the context.

◆ pushGraphicsContext()

SvgGraphicsContext * SvgLoadingContext::pushGraphicsContext ( const QDomElement & element = QDomElement(),
bool inherit = true )

Pushes a new graphics context to the stack.

Definition at line 82 of file SvgLoadingContext.cpp.

83{
85 // copy data from current context
86 if (! d->gcStack.isEmpty() && inherit) {
87 gc = new SvgGraphicsContext(*d->gcStack.top());
88 } else {
89 gc = new SvgGraphicsContext();
90 }
91
93
94 gc->filterId.clear(); // filters are not inherited
95 gc->clipPathId.clear(); // clip paths are not inherited
96 gc->clipMaskId.clear(); // clip masks are not inherited
97 gc->display = true; // display is not inherited
98 gc->opacity = 1.0; // opacity is not inherited
99 gc->paintOrder = QString(); //paint order is inherited by default
100
101 if (!element.isNull()) {
102 if (element.hasAttribute("transform")) {
103 SvgTransformParser p(element.attribute("transform"));
104 if (p.isValid()) {
105 QTransform mat = p.transform();
106 gc->matrix = mat * gc->matrix;
107 }
108 }
109 if (element.hasAttribute("xml:base"))
110 gc->xmlBaseDir = element.attribute("xml:base");
111 if (element.hasAttribute("xml:space"))
112 gc->preserveWhitespace = element.attribute("xml:space") == "preserve";
113 }
114
115 d->gcStack.push(gc);
116
117 return gc;
118}
const Params2D p
QString clipPathId
the current clip path id
bool preserveWhitespace
preserve whitespace in element text
KoSvgTextProperties textProperties
Stores textProperties.
bool display
controls display of shape
QTransform matrix
the current transformation matrix
QString filterId
the current filter id
QString paintOrder
String list indicating paint order;.
QString clipMaskId
the current clip mask id
qreal opacity
the shapes opacity

References SvgGraphicsContext::clipMaskId, SvgGraphicsContext::clipPathId, d, SvgGraphicsContext::display, SvgGraphicsContext::filterId, SvgGraphicsContext::matrix, SvgGraphicsContext::opacity, p, SvgGraphicsContext::paintOrder, SvgGraphicsContext::preserveWhitespace, SvgGraphicsContext::textProperties, and SvgGraphicsContext::xmlBaseDir.

◆ registerShape()

void SvgLoadingContext::registerShape ( const QString & id,
KoShape * shape )

Registers a shape so it can be referenced later.

Definition at line 189 of file SvgLoadingContext.cpp.

190{
191 if (!id.isEmpty())
192 d->loadedShapes.insert(id, shape);
193}

References d.

◆ relativeFilePath()

QString SvgLoadingContext::relativeFilePath ( const QString & href)

Definition at line 163 of file SvgLoadingContext.cpp.

164{
165 const SvgGraphicsContext *gc = currentGC();
166 if (!gc) return href;
167
168 QString result = href;
169
170 QFileInfo info(href);
171 if (info.isRelative())
172 return href;
173
174
175 if (!gc->xmlBaseDir.isEmpty()) {
176 result = QDir(gc->xmlBaseDir).relativeFilePath(href);
177 } else if (!d->initialXmlBaseDir.isEmpty()) {
178 result = QDir(d->initialXmlBaseDir).relativeFilePath(href);
179 }
180
181 return QDir::cleanPath(result);
182}

References currentGC(), d, and SvgGraphicsContext::xmlBaseDir.

◆ resolvedProperties()

KoSvgTextProperties SvgLoadingContext::resolvedProperties ( ) const

These are the text properties, completely resolved, ensuring that everything is inherited and the sizes are converted to pt.

Definition at line 284 of file SvgLoadingContext.cpp.

285{
287 for (auto it = d->gcStack.begin(); it != d->gcStack.end(); it++) {
288 SvgGraphicsContext *gc = *it;
291 props2.inheritFrom(props, true);
292 props = props2;
293 }
294 return props;
295}
void inheritFrom(const KoSvgTextProperties &parentProperties, bool resolve=false)

References d, KoSvgTextProperties::inheritFrom(), KoSvgTextProperties::resetNonInheritableToDefault(), and SvgGraphicsContext::textProperties.

◆ setFileFetcher()

void SvgLoadingContext::setFileFetcher ( SvgLoadingContext::FileFetcherFunc func)

Definition at line 303 of file SvgLoadingContext.cpp.

304{
305 d->fileFetcher = func;
306}

References d.

◆ setInitialXmlBaseDir()

void SvgLoadingContext::setInitialXmlBaseDir ( const QString & baseDir)

Sets the initial xml base dir, i.e. the directory the svg file is read from.

Definition at line 125 of file SvgLoadingContext.cpp.

126{
127 d->initialXmlBaseDir = baseDir;
128}

References d.

◆ shapeById()

KoShape * SvgLoadingContext::shapeById ( const QString & id)

Returns shape with specified id.

Definition at line 195 of file SvgLoadingContext.cpp.

196{
197 return d->loadedShapes.value(id);
198}

References d.

◆ styleParser()

SvgStyleParser & SvgLoadingContext::styleParser ( )

Returns a style parser to parse styles.

◆ xmlBaseDir()

QString SvgLoadingContext::xmlBaseDir ( ) const

Returns the current xml base dir.

Definition at line 130 of file SvgLoadingContext.cpp.

131{
133 return (gc && !gc->xmlBaseDir.isEmpty()) ? gc->xmlBaseDir : d->initialXmlBaseDir;
134}

References currentGC(), d, and SvgGraphicsContext::xmlBaseDir.

Member Data Documentation

◆ cssStyles

SvgCssHelper SvgLoadingContext::cssStyles

Definition at line 55 of file SvgLoadingContext.cpp.

◆ d

QScopedPointer<Private> SvgLoadingContext::d
private

Definition at line 96 of file SvgLoadingContext.h.

◆ definitions

QHash<QString, QDomElement> SvgLoadingContext::definitions

Definition at line 53 of file SvgLoadingContext.cpp.

◆ documentResourceManager

KoDocumentResourceManager* SvgLoadingContext::documentResourceManager

Definition at line 51 of file SvgLoadingContext.cpp.

◆ fileFetcher

FileFetcherFunc SvgLoadingContext::fileFetcher

Definition at line 57 of file SvgLoadingContext.cpp.

◆ gcStack

QStack<SvgGraphicsContext*> SvgLoadingContext::gcStack

Definition at line 48 of file SvgLoadingContext.cpp.

◆ initialXmlBaseDir

QString SvgLoadingContext::initialXmlBaseDir

Definition at line 49 of file SvgLoadingContext.cpp.

◆ loadedShapes

QHash<QString, KoShape*> SvgLoadingContext::loadedShapes

Definition at line 52 of file SvgLoadingContext.cpp.

◆ profiles

QHash< QString, const KoColorProfile * > SvgLoadingContext::profiles

Definition at line 54 of file SvgLoadingContext.cpp.

◆ styleParser

SvgStyleParser & SvgLoadingContext::styleParser

Definition at line 56 of file SvgLoadingContext.cpp.

◆ zIndex

int SvgLoadingContext::zIndex

Definition at line 50 of file SvgLoadingContext.cpp.


The documentation for this class was generated from the following files: