diff options
author | Carl Schwan <[email protected]> | 2024-04-10 16:35:30 +0200 |
---|---|---|
committer | Carl Schwan <[email protected]> | 2024-05-27 09:08:04 +0200 |
commit | 8ab9e0104c15684858ca742c349c4e4ccae2e426 (patch) | |
tree | 647e0de3d77015e85416775d2b016d0ca85de8c1 /src/quick/items/qquicktextdocument.cpp | |
parent | 790c9372137795f0fbfe56af4ce141381df6aff0 (diff) |
QQuickTextImageHandler: Use newly introduced max-width
The implementation follows the one from QTextImageHandler added
in ab2f4ef8fad82fd07dac8768b8dcb94b11b271b6 in qtbase.
Change-Id: Ic5816546a0dec1852894b7a7ab5b40b6affa1958
Reviewed-by: Eskil Abrahamsen Blomfeldt <[email protected]>
Diffstat (limited to 'src/quick/items/qquicktextdocument.cpp')
-rw-r--r-- | src/quick/items/qquicktextdocument.cpp | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/src/quick/items/qquicktextdocument.cpp b/src/quick/items/qquicktextdocument.cpp index 7812cf107e..a1fb7adcea 100644 --- a/src/quick/items/qquicktextdocument.cpp +++ b/src/quick/items/qquicktextdocument.cpp @@ -589,10 +589,24 @@ QSizeF QQuickTextImageHandler::intrinsicSize( { if (format.isImageFormat()) { QTextImageFormat imageFormat = format.toImageFormat(); - const int width = qRound(imageFormat.width()); + int width = qRound(imageFormat.width()); const bool hasWidth = imageFormat.hasProperty(QTextFormat::ImageWidth) && width > 0; const int height = qRound(imageFormat.height()); const bool hasHeight = imageFormat.hasProperty(QTextFormat::ImageHeight) && height > 0; + const auto maxWidth = imageFormat.maximumWidth(); + const bool hasMaxWidth = imageFormat.hasProperty(QTextFormat::ImageMaxWidth) && maxWidth.type() != QTextLength::VariableLength; + + int effectiveMaxWidth = 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); + } + QSizeF size(width, height); if (!hasWidth || !hasHeight) { QVariant res = doc->resource(QTextDocument::ImageResource, QUrl(imageFormat.name())); @@ -607,11 +621,17 @@ QSizeF QQuickTextImageHandler::intrinsicSize( return size; } QSize imgSize = image.size(); + if (imgSize.width() > effectiveMaxWidth) { + // image is bigger than effectiveMaxWidth, scale it down + imgSize.setHeight(effectiveMaxWidth * imgSize.height() / (qreal) imgSize.width()); + imgSize.setWidth(effectiveMaxWidth); + } + if (!hasWidth) { if (!hasHeight) size.setWidth(imgSize.width()); else - size.setWidth(qRound(height * (imgSize.width() / (qreal) imgSize.height()))); + size.setWidth(qMin(effectiveMaxWidth, qRound(height * (imgSize.width() / (qreal) imgSize.height())))); } if (!hasHeight) { if (!hasWidth) |