QGIS API Documentation 4.5.0-Master (3b5d115045b)
Loading...
Searching...
No Matches
qgssvgcache.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssvgcache.h
3 ------------------------------
4 begin : 2011
5 copyright : (C) 2011 by Marco Hugentobler
6 email : marco dot hugentobler at sourcepole dot ch
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgssvgcache.h"
19
20#include "qgis.h"
22#include "qgslogger.h"
23#include "qgsmessagelog.h"
26#include "qgspainting.h"
27#include "qgssymbollayerutils.h"
28
29#include <QApplication>
30#include <QCoreApplication>
31#include <QCursor>
32#include <QDomDocument>
33#include <QDomElement>
34#include <QFile>
35#include <QFileInfo>
36#include <QImage>
37#include <QNetworkReply>
38#include <QNetworkRequest>
39#include <QPainter>
40#include <QPicture>
41#include <QRegularExpression>
42#include <QString>
43#include <QSvgRenderer>
44
45#include "moc_qgssvgcache.cpp"
46
47using namespace Qt::StringLiterals;
48
50
51//
52// QgsSvgCacheEntry
53//
54
55QgsSvgCacheEntry::QgsSvgCacheEntry(
56 const QString &path, double size, double strokeWidth, double widthScaleFactor, const QColor &fill, const QColor &stroke, double fixedAspectRatio, const QMap<QString, QString> &parameters
57)
59 , size( size )
60 , strokeWidth( strokeWidth )
61 , widthScaleFactor( widthScaleFactor )
62 , fixedAspectRatio( fixedAspectRatio )
63 , fill( fill )
64 , stroke( stroke )
65 , parameters( parameters )
66{}
67
68bool QgsSvgCacheEntry::isEqual( const QgsAbstractContentCacheEntry *other ) const
69{
70 const QgsSvgCacheEntry *otherSvg = dynamic_cast< const QgsSvgCacheEntry * >( other );
71 // cheapest checks first!
72 if ( !otherSvg
73 || !qgsDoubleNear( otherSvg->fixedAspectRatio, fixedAspectRatio )
74 || !qgsDoubleNear( otherSvg->size, size )
75 || !qgsDoubleNear( otherSvg->strokeWidth, strokeWidth )
76 || !qgsDoubleNear( otherSvg->widthScaleFactor, widthScaleFactor )
77 || otherSvg->fill != fill
78 || otherSvg->stroke != stroke
79 || otherSvg->path != path
80 || otherSvg->parameters != parameters )
81 return false;
82
83 return true;
84}
85
86int QgsSvgCacheEntry::dataSize() const
87{
88 int size = svgContent.size();
89 if ( picture )
90 {
91 size += picture->size();
92 }
93 if ( image )
94 {
95 size += ( image->width() * image->height() * 32 );
96 }
97 return size;
98}
99
100void QgsSvgCacheEntry::dump() const
101{
102 QgsDebugMsgLevel( u"path: %1, size %2, width scale factor %3"_s.arg( path ).arg( size ).arg( widthScaleFactor ), 4 );
103}
105
106
107//
108// QgsSvgCache
109//
110
111QgsSvgCache::QgsSvgCache( QObject *parent )
112 : QgsAbstractContentCache< QgsSvgCacheEntry >( parent, QObject::tr( "SVG" ) )
113{
114 mMissingSvg = u"<svg width='10' height='10'><text x='5' y='10' font-size='10' text-anchor='middle'>?</text></svg>"_s.toLatin1();
115
116 const QString downloadingSvgPath = QgsApplication::defaultThemePath() + u"downloading_svg.svg"_s;
117 if ( QFile::exists( downloadingSvgPath ) )
118 {
119 QFile file( downloadingSvgPath );
120 if ( file.open( QIODevice::ReadOnly ) )
121 {
122 mFetchingSvg = file.readAll();
123 }
124 }
125
126 if ( mFetchingSvg.isEmpty() )
127 {
128 mFetchingSvg = u"<svg width='10' height='10'><text x='5' y='10' font-size='10' text-anchor='middle'>?</text></svg>"_s.toLatin1();
129 }
130
132}
133
135 const QString &file,
136 double size,
137 const QColor &fill,
138 const QColor &stroke,
139 double strokeWidth,
140 double widthScaleFactor,
141 bool &fitsInCache,
142 double fixedAspectRatio,
143 bool blocking,
144 const QMap<QString, QString> &parameters
145)
146{
147 const QMutexLocker locker( &mMutex );
148
149 fitsInCache = true;
150 QgsSvgCacheEntry *currentEntry = cacheEntry( file, size, fill, stroke, strokeWidth, widthScaleFactor, fixedAspectRatio, parameters, blocking );
151
152 QImage result;
153
154 //if current entry image is 0: cache image for entry
155 // checks to see if image will fit into cache
156 //update stats for memory usage
157 if ( !currentEntry->image )
158 {
159 const QSvgRenderer r( currentEntry->svgContent );
160 double hwRatio = 1.0;
161 if ( r.viewBoxF().width() > 0 )
162 {
163 if ( currentEntry->fixedAspectRatio > 0 )
164 {
165 hwRatio = currentEntry->fixedAspectRatio;
166 }
167 else
168 {
169 hwRatio = r.viewBoxF().height() / r.viewBoxF().width();
170 }
171 }
172 long cachedDataSize = 0;
173 cachedDataSize += currentEntry->svgContent.size();
174 cachedDataSize += static_cast< int >( currentEntry->size * currentEntry->size * hwRatio * 32 );
175 if ( cachedDataSize > mMaxCacheSize / 2 )
176 {
177 fitsInCache = false;
178 currentEntry->image.reset();
179
180 // instead cache picture
181 if ( !currentEntry->picture )
182 {
183 cachePicture( currentEntry, false );
184 }
185
186 // ...and render cached picture to result image
187 result = imageFromCachedPicture( *currentEntry );
188 }
189 else
190 {
191 cacheImage( currentEntry );
192 result = *( currentEntry->image );
193 }
195 }
196 else
197 {
198 result = *( currentEntry->image );
199 }
200
201 return result;
202}
203
205 const QString &path,
206 double size,
207 const QColor &fill,
208 const QColor &stroke,
209 double strokeWidth,
210 double widthScaleFactor,
211 bool forceVectorOutput,
212 double fixedAspectRatio,
213 bool blocking,
214 const QMap<QString, QString> &parameters
215)
216{
217 const QMutexLocker locker( &mMutex );
218
219 QgsSvgCacheEntry *currentEntry = cacheEntry( path, size, fill, stroke, strokeWidth, widthScaleFactor, fixedAspectRatio, parameters, blocking );
220
221 //if current entry picture is 0: cache picture for entry
222 //update stats for memory usage
223 if ( !currentEntry->picture )
224 {
225 cachePicture( currentEntry, forceVectorOutput );
227 }
228
229 return QgsPainting::deepCopyQPicture( *( currentEntry->picture.get() ) );
230}
231
233 const QString &path,
234 double size,
235 const QColor &fill,
236 const QColor &stroke,
237 double strokeWidth,
238 double widthScaleFactor,
239 double fixedAspectRatio,
240 bool blocking,
241 const QMap<QString, QString> &parameters,
242 bool *isMissingImage
243)
244{
245 const QMutexLocker locker( &mMutex );
246
247 QgsSvgCacheEntry *currentEntry = cacheEntry( path, size, fill, stroke, strokeWidth, widthScaleFactor, fixedAspectRatio, parameters, blocking, isMissingImage );
248
249 return currentEntry->svgContent;
250}
251
253 const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth, double widthScaleFactor, double fixedAspectRatio, bool blocking, const QMap<QString, QString> &parameters
254)
255{
256 const QMutexLocker locker( &mMutex );
257
258 QgsSvgCacheEntry *currentEntry = cacheEntry( path, size, fill, stroke, strokeWidth, widthScaleFactor, fixedAspectRatio, parameters, blocking );
259 return currentEntry->viewboxSize;
260}
261
263 const QString &path, bool &hasFillParam, QColor &defaultFillColor, bool &hasStrokeParam, QColor &defaultStrokeColor, bool &hasStrokeWidthParam, double &defaultStrokeWidth, bool blocking
264) const
265{
266 bool hasDefaultFillColor = false;
267 bool hasFillOpacityParam = false;
268 bool hasDefaultFillOpacity = false;
269 double defaultFillOpacity = 1.0;
270 bool hasDefaultStrokeColor = false;
271 bool hasDefaultStrokeWidth = false;
272 bool hasStrokeOpacityParam = false;
273 bool hasDefaultStrokeOpacity = false;
274 double defaultStrokeOpacity = 1.0;
275
277 path,
278 hasFillParam,
279 hasDefaultFillColor,
280 defaultFillColor,
281 hasFillOpacityParam,
282 hasDefaultFillOpacity,
283 defaultFillOpacity,
284 hasStrokeParam,
285 hasDefaultStrokeColor,
286 defaultStrokeColor,
287 hasStrokeWidthParam,
288 hasDefaultStrokeWidth,
289 defaultStrokeWidth,
290 hasStrokeOpacityParam,
291 hasDefaultStrokeOpacity,
292 defaultStrokeOpacity,
293 blocking
294 );
295}
296
298 const QString &path,
299 bool &hasFillParam,
300 bool &hasDefaultFillParam,
301 QColor &defaultFillColor,
302 bool &hasFillOpacityParam,
303 bool &hasDefaultFillOpacity,
304 double &defaultFillOpacity,
305 bool &hasStrokeParam,
306 bool &hasDefaultStrokeColor,
307 QColor &defaultStrokeColor,
308 bool &hasStrokeWidthParam,
309 bool &hasDefaultStrokeWidth,
310 double &defaultStrokeWidth,
311 bool &hasStrokeOpacityParam,
312 bool &hasDefaultStrokeOpacity,
313 double &defaultStrokeOpacity,
314 bool blocking
315) const
316{
317 hasFillParam = false;
318 hasFillOpacityParam = false;
319 hasStrokeParam = false;
320 hasStrokeWidthParam = false;
321 hasStrokeOpacityParam = false;
322 defaultFillColor = QColor( Qt::white );
323 defaultFillOpacity = 1.0;
324 defaultStrokeColor = QColor( Qt::black );
325 defaultStrokeWidth = 0.2;
326 defaultStrokeOpacity = 1.0;
327
328 hasDefaultFillParam = false;
329 hasDefaultFillOpacity = false;
330 hasDefaultStrokeColor = false;
331 hasDefaultStrokeWidth = false;
332 hasDefaultStrokeOpacity = false;
333
334 QDomDocument svgDoc;
335 if ( !svgDoc.setContent( getContent( path, mMissingSvg, mFetchingSvg, blocking ) ) )
336 {
337 return;
338 }
339
340 const QDomElement docElem = svgDoc.documentElement();
341 containsElemParams(
342 docElem,
343 hasFillParam,
344 hasDefaultFillParam,
345 defaultFillColor,
346 hasFillOpacityParam,
347 hasDefaultFillOpacity,
348 defaultFillOpacity,
349 hasStrokeParam,
350 hasDefaultStrokeColor,
351 defaultStrokeColor,
352 hasStrokeWidthParam,
353 hasDefaultStrokeWidth,
354 defaultStrokeWidth,
355 hasStrokeOpacityParam,
356 hasDefaultStrokeOpacity,
357 defaultStrokeOpacity
358 );
359}
360
361void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry *entry, bool blocking )
362{
363 if ( !entry )
364 {
365 return;
366 }
367
368 const QByteArray content = getContent( entry->path, mMissingSvg, mFetchingSvg, blocking );
369 entry->isMissingImage = content == mMissingSvg;
370 QDomDocument svgDoc;
371 if ( !svgDoc.setContent( content ) )
372 {
373 return;
374 }
375
376 //replace fill color, stroke color, stroke with in all nodes
377 QDomElement docElem = svgDoc.documentElement();
378
379 QSizeF viewboxSize;
380 const double sizeScaleFactor = calcSizeScaleFactor( entry, docElem, viewboxSize );
381 entry->viewboxSize = viewboxSize;
382 replaceElemParams( docElem, entry->fill, entry->stroke, entry->strokeWidth * sizeScaleFactor, entry->parameters );
383
384 entry->svgContent = svgDoc.toByteArray( 0 );
385
386
387 // toByteArray screws up tspans inside text by adding new lines before and after each span... this should help, at the
388 // risk of potentially breaking some svgs where the newline is desired
389 entry->svgContent.replace( "\n<tspan", "<tspan" );
390 entry->svgContent.replace( "</tspan>\n", "</tspan>" );
391
392 mTotalSize += entry->svgContent.size();
393}
394
395double QgsSvgCache::calcSizeScaleFactor( QgsSvgCacheEntry *entry, const QDomElement &docElem, QSizeF &viewboxSize ) const
396{
397 QString viewBox;
398
399 //bad size
400 if ( !entry || qgsDoubleNear( entry->size, 0.0 ) )
401 return 1.0;
402
403 //find svg viewbox attribute
404 //first check if docElem is svg element
405 if ( docElem.tagName() == "svg"_L1 && docElem.hasAttribute( u"viewBox"_s ) )
406 {
407 viewBox = docElem.attribute( u"viewBox"_s, QString() );
408 }
409 else if ( docElem.tagName() == "svg"_L1 && docElem.hasAttribute( u"viewbox"_s ) )
410 {
411 viewBox = docElem.attribute( u"viewbox"_s, QString() );
412 }
413 else
414 {
415 const QDomElement svgElem = docElem.firstChildElement( u"svg"_s );
416 if ( !svgElem.isNull() )
417 {
418 if ( svgElem.hasAttribute( u"viewBox"_s ) )
419 viewBox = svgElem.attribute( u"viewBox"_s, QString() );
420 else if ( svgElem.hasAttribute( u"viewbox"_s ) )
421 viewBox = svgElem.attribute( u"viewbox"_s, QString() );
422 }
423 }
424
425 //could not find valid viewbox attribute
426 if ( viewBox.isEmpty() )
427 {
428 // trying looking for width/height and use them as a fallback
429 if ( docElem.tagName() == "svg"_L1 && docElem.hasAttribute( u"width"_s ) )
430 {
431 const QString widthString = docElem.attribute( u"width"_s );
432 const thread_local QRegularExpression measureRegEx( u"([\\d\\.]+).*?$"_s );
433 const QRegularExpressionMatch widthMatch = measureRegEx.match( widthString );
434 if ( widthMatch.hasMatch() )
435 {
436 const double width = widthMatch.captured( 1 ).toDouble();
437 const QString heightString = docElem.attribute( u"height"_s );
438
439 const QRegularExpressionMatch heightMatch = measureRegEx.match( heightString );
440 if ( heightMatch.hasMatch() )
441 {
442 const double height = heightMatch.captured( 1 ).toDouble();
443 viewboxSize = QSizeF( width, height );
444 return width / entry->size;
445 }
446 }
447 }
448
449 return 1.0;
450 }
451
452
453 //width should be 3rd element in a 4 part space delimited string
454 const QStringList parts = viewBox.split( ' ' );
455 if ( parts.count() != 4 )
456 return 1.0;
457
458 bool heightOk = false;
459 const double height = parts.at( 3 ).toDouble( &heightOk );
460
461 bool widthOk = false;
462 const double width = parts.at( 2 ).toDouble( &widthOk );
463 if ( widthOk )
464 {
465 if ( heightOk )
466 viewboxSize = QSizeF( width, height );
467 return width / entry->size;
468 }
469
470 return 1.0;
471}
472
473
474QByteArray QgsSvgCache::getImageData( const QString &path, bool blocking ) const
475{
476 return getContent( path, mMissingSvg, mFetchingSvg, blocking );
477};
478
479bool QgsSvgCache::checkReply( QNetworkReply *reply, const QString &path ) const
480{
481 // we accept both real SVG mime types AND plain text types - because some sites
482 // (notably github) serve up svgs as raw text
483 const QString contentType = reply->header( QNetworkRequest::ContentTypeHeader ).toString();
484 if ( !contentType.startsWith( "image/svg+xml"_L1, Qt::CaseInsensitive ) && !contentType.startsWith( "text/plain"_L1, Qt::CaseInsensitive ) )
485 {
486 QgsMessageLog::logMessage( tr( "Unexpected MIME type %1 received for %2" ).arg( contentType, path ), tr( "SVG" ) );
487 return false;
488 }
489 return true;
490}
491
492void QgsSvgCache::cacheImage( QgsSvgCacheEntry *entry )
493{
494 if ( !entry )
495 {
496 return;
497 }
498
499 entry->image.reset();
500
501 QSizeF viewBoxSize;
502 QSizeF scaledSize;
503 const QSize imageSize = sizeForImage( *entry, viewBoxSize, scaledSize );
504
505 // cast double image sizes to int for QImage
506 auto image = std::make_unique< QImage >( imageSize, QImage::Format_ARGB32_Premultiplied );
507 image->fill( 0 ); // transparent background
508
509 const bool isFixedAR = entry->fixedAspectRatio > 0;
510
511 QPainter p( image.get() );
512 QSvgRenderer r( entry->svgContent );
513 if ( qgsDoubleNear( viewBoxSize.width(), viewBoxSize.height() ) )
514 {
515 r.render( &p );
516 }
517 else
518 {
519 QSizeF s( viewBoxSize );
520 s.scale( scaledSize.width(), scaledSize.height(), isFixedAR ? Qt::IgnoreAspectRatio : Qt::KeepAspectRatio );
521 const QRectF rect( ( imageSize.width() - s.width() ) / 2, ( imageSize.height() - s.height() ) / 2, s.width(), s.height() );
522 r.render( &p, rect );
523 }
524
525 mTotalSize += ( image->width() * image->height() * 32 );
526 entry->image = std::move( image );
527}
528
529void QgsSvgCache::cachePicture( QgsSvgCacheEntry *entry, bool forceVectorOutput )
530{
531 Q_UNUSED( forceVectorOutput )
532 if ( !entry )
533 {
534 return;
535 }
536
537 entry->picture.reset();
538
539 const bool isFixedAR = entry->fixedAspectRatio > 0;
540
541 //correct QPictures dpi correction
542 auto picture = std::make_unique< QPicture >();
543 QRectF rect;
544 QSvgRenderer r( entry->svgContent );
545 double hwRatio = 1.0;
546 if ( r.viewBoxF().width() > 0 )
547 {
548 if ( isFixedAR )
549 {
550 hwRatio = entry->fixedAspectRatio;
551 }
552 else
553 {
554 hwRatio = r.viewBoxF().height() / r.viewBoxF().width();
555 }
556 }
557
558 const double wSize = entry->size;
559 const double hSize = wSize * hwRatio;
560
561 QSizeF s( r.viewBoxF().size() );
562 s.scale( wSize, hSize, isFixedAR ? Qt::IgnoreAspectRatio : Qt::KeepAspectRatio );
563 rect = QRectF( -s.width() / 2.0, -s.height() / 2.0, s.width(), s.height() );
564
565 QPainter p( picture.get() );
566 r.render( &p, rect );
567 entry->picture = std::move( picture );
568 mTotalSize += entry->picture->size();
569}
570
571QgsSvgCacheEntry *QgsSvgCache::cacheEntry(
572 const QString &path,
573 double size,
574 const QColor &fill,
575 const QColor &stroke,
576 double strokeWidth,
577 double widthScaleFactor,
578 double fixedAspectRatio,
579 const QMap<QString, QString> &parameters,
580 bool blocking,
581 bool *isMissingImage
582)
583{
584 QgsSvgCacheEntry *currentEntry = findExistingEntry( new QgsSvgCacheEntry( path, size, strokeWidth, widthScaleFactor, fill, stroke, fixedAspectRatio, parameters ) );
585
586 if ( currentEntry->svgContent.isEmpty() )
587 {
588 replaceParamsAndCacheSvg( currentEntry, blocking );
589 }
590
591 if ( isMissingImage )
592 *isMissingImage = currentEntry->isMissingImage;
593
594 return currentEntry;
595}
596
597
598void QgsSvgCache::replaceElemParams( QDomElement &elem, const QColor &fill, const QColor &stroke, double strokeWidth, const QMap<QString, QString> &parameters )
599{
600 if ( elem.isNull() )
601 {
602 return;
603 }
604
605 //go through attributes
606 const QDomNamedNodeMap attributes = elem.attributes();
607 const int nAttributes = attributes.count();
608 for ( int i = 0; i < nAttributes; ++i )
609 {
610 const QDomAttr attribute = attributes.item( i ).toAttr();
611 //e.g. style="fill:param(fill);param(stroke)"
612 if ( attribute.name().compare( "style"_L1, Qt::CaseInsensitive ) == 0 )
613 {
614 //entries separated by ';'
615 QString newAttributeString;
616
617 const QStringList entryList = attribute.value().split( ';' );
618 QStringList::const_iterator entryIt = entryList.constBegin();
619 for ( ; entryIt != entryList.constEnd(); ++entryIt )
620 {
621 const QStringList keyValueSplit = entryIt->split( ':' );
622 if ( keyValueSplit.size() < 2 )
623 {
624 continue;
625 }
626 const QString key = keyValueSplit.at( 0 );
627 QString value = keyValueSplit.at( 1 );
628 QString newValue = value;
629 value = value.trimmed().toLower();
630
631 if ( value.startsWith( "param(fill)"_L1 ) )
632 {
633 newValue = fill.name();
634 }
635 else if ( value.startsWith( "param(fill-opacity)"_L1 ) )
636 {
637 newValue = QString::number( fill.alphaF() );
638 }
639 else if ( value.startsWith( "param(outline)"_L1 ) )
640 {
641 newValue = stroke.name();
642 }
643 else if ( value.startsWith( "param(outline-opacity)"_L1 ) )
644 {
645 newValue = QString::number( stroke.alphaF() );
646 }
647 else if ( value.startsWith( "param(outline-width)"_L1 ) )
648 {
649 newValue = QString::number( strokeWidth );
650 }
651
652 if ( entryIt != entryList.constBegin() )
653 {
654 newAttributeString.append( ';' );
655 }
656 newAttributeString.append( key + ':' + newValue );
657 }
658 elem.setAttribute( attribute.name(), newAttributeString );
659 }
660 else
661 {
662 const QString value = attribute.value().trimmed().toLower();
663 if ( value.startsWith( "param(fill)"_L1 ) )
664 {
665 elem.setAttribute( attribute.name(), fill.name() );
666 }
667 else if ( value.startsWith( "param(fill-opacity)"_L1 ) )
668 {
669 elem.setAttribute( attribute.name(), fill.alphaF() );
670 }
671 else if ( value.startsWith( "param(outline)"_L1 ) )
672 {
673 elem.setAttribute( attribute.name(), stroke.name() );
674 }
675 else if ( value.startsWith( "param(outline-opacity)"_L1 ) )
676 {
677 elem.setAttribute( attribute.name(), stroke.alphaF() );
678 }
679 else if ( value.startsWith( "param(outline-width)"_L1 ) )
680 {
681 elem.setAttribute( attribute.name(), QString::number( strokeWidth ) );
682 }
683 else
684 {
685 QMap<QString, QString>::const_iterator paramIt = parameters.constBegin();
686 for ( ; paramIt != parameters.constEnd(); ++paramIt )
687 {
688 if ( value.startsWith( QString( "param(%1)"_L1 ).arg( paramIt.key() ) ) )
689 {
690 elem.setAttribute( attribute.name(), paramIt.value() );
691 break;
692 }
693 }
694 }
695 }
696 }
697
698 QDomNode child = elem.firstChild();
699 if ( child.isText() && child.nodeValue().startsWith( "param(" ) )
700 {
701 QMap<QString, QString>::const_iterator paramIt = parameters.constBegin();
702 for ( ; paramIt != parameters.constEnd(); ++paramIt )
703 {
704 if ( child.toText().data().startsWith( QString( "param(%1)"_L1 ).arg( paramIt.key() ) ) )
705 {
706 child.setNodeValue( paramIt.value() );
707 break;
708 }
709 }
710 }
711
712 const QDomNodeList childList = elem.childNodes();
713 const int nChildren = childList.count();
714 for ( int i = 0; i < nChildren; ++i )
715 {
716 QDomElement childElem = childList.at( i ).toElement();
717 replaceElemParams( childElem, fill, stroke, strokeWidth, parameters );
718 }
719}
720
721void QgsSvgCache::containsElemParams(
722 const QDomElement &elem,
723 bool &hasFillParam,
724 bool &hasDefaultFill,
725 QColor &defaultFill,
726 bool &hasFillOpacityParam,
727 bool &hasDefaultFillOpacity,
728 double &defaultFillOpacity,
729 bool &hasStrokeParam,
730 bool &hasDefaultStroke,
731 QColor &defaultStroke,
732 bool &hasStrokeWidthParam,
733 bool &hasDefaultStrokeWidth,
734 double &defaultStrokeWidth,
735 bool &hasStrokeOpacityParam,
736 bool &hasDefaultStrokeOpacity,
737 double &defaultStrokeOpacity
738) const
739{
740 if ( elem.isNull() )
741 {
742 return;
743 }
744
745 //we already have all the information, no need to go deeper
746 if ( hasFillParam && hasStrokeParam && hasStrokeWidthParam && hasFillOpacityParam && hasStrokeOpacityParam )
747 {
748 return;
749 }
750
751 //check this elements attribute
752 const QDomNamedNodeMap attributes = elem.attributes();
753 const int nAttributes = attributes.count();
754
755 QStringList valueSplit;
756 for ( int i = 0; i < nAttributes; ++i )
757 {
758 const QDomAttr attribute = attributes.item( i ).toAttr();
759 if ( attribute.name().compare( "style"_L1, Qt::CaseInsensitive ) == 0 )
760 {
761 //entries separated by ';'
762 const QStringList entryList = attribute.value().split( ';' );
763 QStringList::const_iterator entryIt = entryList.constBegin();
764 for ( ; entryIt != entryList.constEnd(); ++entryIt )
765 {
766 const QStringList keyValueSplit = entryIt->split( ':' );
767 if ( keyValueSplit.size() < 2 )
768 {
769 continue;
770 }
771 const QString value = keyValueSplit.at( 1 );
772 valueSplit = value.split( ' ' );
773 if ( !hasFillParam && value.startsWith( "param(fill)"_L1 ) )
774 {
775 hasFillParam = true;
776 if ( valueSplit.size() > 1 )
777 {
778 defaultFill = QColor( valueSplit.at( 1 ) );
779 hasDefaultFill = true;
780 }
781 }
782 else if ( !hasFillOpacityParam && value.startsWith( "param(fill-opacity)"_L1 ) )
783 {
784 hasFillOpacityParam = true;
785 if ( valueSplit.size() > 1 )
786 {
787 bool ok;
788 const double opacity = valueSplit.at( 1 ).toDouble( &ok );
789 if ( ok )
790 {
791 defaultFillOpacity = opacity;
792 hasDefaultFillOpacity = true;
793 }
794 }
795 }
796 else if ( !hasStrokeParam && value.startsWith( "param(outline)"_L1 ) )
797 {
798 hasStrokeParam = true;
799 if ( valueSplit.size() > 1 )
800 {
801 defaultStroke = QColor( valueSplit.at( 1 ) );
802 hasDefaultStroke = true;
803 }
804 }
805 else if ( !hasStrokeWidthParam && value.startsWith( "param(outline-width)"_L1 ) )
806 {
807 hasStrokeWidthParam = true;
808 if ( valueSplit.size() > 1 )
809 {
810 defaultStrokeWidth = valueSplit.at( 1 ).toDouble();
811 hasDefaultStrokeWidth = true;
812 }
813 }
814 else if ( !hasStrokeOpacityParam && value.startsWith( "param(outline-opacity)"_L1 ) )
815 {
816 hasStrokeOpacityParam = true;
817 if ( valueSplit.size() > 1 )
818 {
819 bool ok;
820 const double opacity = valueSplit.at( 1 ).toDouble( &ok );
821 if ( ok )
822 {
823 defaultStrokeOpacity = opacity;
824 hasDefaultStrokeOpacity = true;
825 }
826 }
827 }
828 }
829 }
830 else
831 {
832 const QString value = attribute.value();
833 valueSplit = value.split( ' ' );
834 if ( !hasFillParam && value.startsWith( "param(fill)"_L1 ) )
835 {
836 hasFillParam = true;
837 if ( valueSplit.size() > 1 )
838 {
839 defaultFill = QColor( valueSplit.at( 1 ) );
840 hasDefaultFill = true;
841 }
842 }
843 else if ( !hasFillOpacityParam && value.startsWith( "param(fill-opacity)"_L1 ) )
844 {
845 hasFillOpacityParam = true;
846 if ( valueSplit.size() > 1 )
847 {
848 bool ok;
849 const double opacity = valueSplit.at( 1 ).toDouble( &ok );
850 if ( ok )
851 {
852 defaultFillOpacity = opacity;
853 hasDefaultFillOpacity = true;
854 }
855 }
856 }
857 else if ( !hasStrokeParam && value.startsWith( "param(outline)"_L1 ) )
858 {
859 hasStrokeParam = true;
860 if ( valueSplit.size() > 1 )
861 {
862 defaultStroke = QColor( valueSplit.at( 1 ) );
863 hasDefaultStroke = true;
864 }
865 }
866 else if ( !hasStrokeWidthParam && value.startsWith( "param(outline-width)"_L1 ) )
867 {
868 hasStrokeWidthParam = true;
869 if ( valueSplit.size() > 1 )
870 {
871 defaultStrokeWidth = valueSplit.at( 1 ).toDouble();
872 hasDefaultStrokeWidth = true;
873 }
874 }
875 else if ( !hasStrokeOpacityParam && value.startsWith( "param(outline-opacity)"_L1 ) )
876 {
877 hasStrokeOpacityParam = true;
878 if ( valueSplit.size() > 1 )
879 {
880 bool ok;
881 const double opacity = valueSplit.at( 1 ).toDouble( &ok );
882 if ( ok )
883 {
884 defaultStrokeOpacity = opacity;
885 hasDefaultStrokeOpacity = true;
886 }
887 }
888 }
889 }
890 }
891
892 //pass it further to child items
893 const QDomNodeList childList = elem.childNodes();
894 const int nChildren = childList.count();
895 for ( int i = 0; i < nChildren; ++i )
896 {
897 const QDomElement childElem = childList.at( i ).toElement();
898 containsElemParams(
899 childElem,
900 hasFillParam,
901 hasDefaultFill,
902 defaultFill,
903 hasFillOpacityParam,
904 hasDefaultFillOpacity,
905 defaultFillOpacity,
906 hasStrokeParam,
907 hasDefaultStroke,
908 defaultStroke,
909 hasStrokeWidthParam,
910 hasDefaultStrokeWidth,
911 defaultStrokeWidth,
912 hasStrokeOpacityParam,
913 hasDefaultStrokeOpacity,
914 defaultStrokeOpacity
915 );
916 }
917}
918
919QSize QgsSvgCache::sizeForImage( const QgsSvgCacheEntry &entry, QSizeF &viewBoxSize, QSizeF &scaledSize ) const
920{
921 const bool isFixedAR = entry.fixedAspectRatio > 0;
922
923 const QSvgRenderer r( entry.svgContent );
924 double hwRatio = 1.0;
925 viewBoxSize = r.viewBoxF().size();
926 if ( viewBoxSize.width() > 0 )
927 {
928 if ( isFixedAR )
929 {
930 hwRatio = entry.fixedAspectRatio;
931 }
932 else
933 {
934 hwRatio = viewBoxSize.height() / viewBoxSize.width();
935 }
936 }
937
938 // cast double image sizes to int for QImage
939 scaledSize.setWidth( entry.size );
940 int wImgSize = static_cast< int >( scaledSize.width() );
941 if ( wImgSize < 1 )
942 {
943 wImgSize = 1;
944 }
945 scaledSize.setHeight( scaledSize.width() * hwRatio );
946 int hImgSize = static_cast< int >( scaledSize.height() );
947 if ( hImgSize < 1 )
948 {
949 hImgSize = 1;
950 }
951 return QSize( wImgSize, hImgSize );
952}
953
954QImage QgsSvgCache::imageFromCachedPicture( const QgsSvgCacheEntry &entry ) const
955{
956 QSizeF viewBoxSize;
957 QSizeF scaledSize;
958 QImage image( sizeForImage( entry, viewBoxSize, scaledSize ), QImage::Format_ARGB32_Premultiplied );
959 image.fill( 0 ); // transparent background
960
961 QPainter p( &image );
962 p.drawPicture( QPoint( 0, 0 ), *entry.picture );
963 return image;
964}
965
966template class QgsAbstractContentCache<QgsSvgCacheEntry>; // clazy:exclude=missing-qobject-macro
void remoteContentFetched(const QString &url)
Emitted when the cache has finished retrieving content from a remote url.
Base class for entries in a QgsAbstractContentCache.
Abstract base class for file content caches, such as SVG or raster image caches.
QByteArray getContent(const QString &path, const QByteArray &missingContent, const QByteArray &fetchingContent, bool blocking=false) const
QgsSvgCacheEntry * findExistingEntry(QgsSvgCacheEntry *entryTemplate)
QgsAbstractContentCache(QObject *parent=nullptr, const QString &typeString=QString(), long maxCacheSize=20000000, int fileModifiedCheckTimeout=30000)
static QString defaultThemePath()
Returns the path to the default theme directory.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
Adds a message to the log instance (and creates it if necessary).
static QPicture deepCopyQPicture(const QPicture &picture)
Deep copies a QPicture, making a copy which is safe to use in a different thread.
QSizeF svgViewboxSize(const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth, double widthScaleFactor, double fixedAspectRatio=0, bool blocking=false, const QMap< QString, QString > &parameters=QMap< QString, QString >())
Calculates the viewbox size of a (possibly cached) SVG file.
QByteArray getImageData(const QString &path, bool blocking=false) const
Gets the SVG content corresponding to the given path.
QPicture svgAsPicture(const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth, double widthScaleFactor, bool forceVectorOutput=false, double fixedAspectRatio=0, bool blocking=false, const QMap< QString, QString > &parameters=QMap< QString, QString >())
Returns an SVG drawing as a QPicture.
QgsSvgCache(QObject *parent=nullptr)
Constructor for QgsSvgCache.
bool checkReply(QNetworkReply *reply, const QString &path) const override
Runs additional checks on a network reply to ensure that the reply content is consistent with that re...
void containsParams(const QString &path, bool &hasFillParam, QColor &defaultFillColor, bool &hasStrokeParam, QColor &defaultStrokeColor, bool &hasStrokeWidthParam, double &defaultStrokeWidth, bool blocking=false) const
Tests if an SVG file contains parameters for fill, stroke color, stroke width.
void remoteSvgFetched(const QString &url)
Emitted when the cache has finished retrieving an SVG file from a remote url.
QImage svgAsImage(const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth, double widthScaleFactor, bool &fitsInCache, double fixedAspectRatio=0, bool blocking=false, const QMap< QString, QString > &parameters=QMap< QString, QString >())
Returns an SVG drawing as a QImage.
QByteArray svgContent(const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth, double widthScaleFactor, double fixedAspectRatio=0, bool blocking=false, const QMap< QString, QString > &parameters=QMap< QString, QString >(), bool *isMissingImage=nullptr)
Gets the SVG content corresponding to the given path.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:7826
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:80