diff options
Diffstat (limited to 'src/gui/text/qtextimagehandler.cpp')
-rw-r--r-- | src/gui/text/qtextimagehandler.cpp | 40 |
1 files changed, 31 insertions, 9 deletions
diff --git a/src/gui/text/qtextimagehandler.cpp b/src/gui/text/qtextimagehandler.cpp index 70e8961467..920e6c689c 100644 --- a/src/gui/text/qtextimagehandler.cpp +++ b/src/gui/text/qtextimagehandler.cpp @@ -12,6 +12,7 @@ #include <private/qtextengine_p.h> #include <qpalette.h> #include <qthread.h> +#include <limits> QT_BEGIN_NAMESPACE @@ -23,12 +24,14 @@ static inline QString findAtNxFileOrResource(const QString &baseFileName, { // qt_findAtNxFile expects a file name that can be tested with QFile::exists. // so if the format.name() is a file:/ or qrc:/ URL, then we need to strip away the schema. - QString localFile = baseFileName; - if (localFile.startsWith("file:/"_L1)) - localFile = localFile.sliced(6); - else if (localFile.startsWith("qrc:/"_L1)) - localFile = localFile.sliced(3); - + QString localFile; + const QUrl url(baseFileName); + if (url.isLocalFile()) + localFile = url.toLocalFile(); + else if (baseFileName.startsWith("qrc:/"_L1)) + localFile = baseFileName.sliced(3); + else + localFile = baseFileName; extern QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRatio, qreal *sourceDevicePixelRatio); return qt_findAtNxFile(localFile, targetDevicePixelRatio, sourceDevicePixelRatio); @@ -70,21 +73,40 @@ template<typename T> static QSize getSize(QTextDocument *doc, const QTextImageFormat &format) { const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth); - const int width = qRound(format.width()); + int width = qRound(format.width()); const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight); const int height = qRound(format.height()); + const bool hasMaxWidth = format.hasProperty(QTextFormat::ImageMaxWidth); + const auto maxWidth = format.maximumWidth(); + + int effectiveMaxWidth = std::numeric_limits<int>::max(); + if (hasMaxWidth) { + if (maxWidth.type() == QTextLength::PercentageLength) + effectiveMaxWidth = (doc->pageSize().width() - 2 * doc->documentMargin()) * maxWidth.value(100) / 100; + else + effectiveMaxWidth = maxWidth.rawValue(); + + width = qMin(effectiveMaxWidth, width); + } + T source; QSize size(width, height); if (!hasWidth || !hasHeight) { source = getAs<T>(doc, format); - const QSizeF sourceSize = source.deviceIndependentSize(); + QSizeF sourceSize = source.deviceIndependentSize(); + + if (sourceSize.width() > effectiveMaxWidth) { + // image is bigger than effectiveMaxWidth, scale it down + sourceSize.setHeight(effectiveMaxWidth * (sourceSize.height() / qreal(sourceSize.width()))); + sourceSize.setWidth(effectiveMaxWidth); + } if (!hasWidth) { if (!hasHeight) size.setWidth(sourceSize.width()); else - size.setWidth(qRound(height * (sourceSize.width() / qreal(sourceSize.height())))); + size.setWidth(qMin(effectiveMaxWidth, qRound(height * (sourceSize.width() / qreal(sourceSize.height()))))); } if (!hasHeight) { if (!hasWidth) |