From 7086e23bdd73143529882e221b8f0963c8a7e82c Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 27 Dec 2021 00:17:27 +0100 Subject: [PATCH 1/3] Request inline images in the right size and anti-alias them - If an inline image has specified a height, add parameters to the image:// URI. - Add scaled to the parameters, the images would be cropped otherwise. - Extract the height from image:// URI and use it for requestSize. - Use scaledToHeight instead of scaled. --- src/MxcImageProvider.cpp | 16 ++++++++++++---- src/timeline/TimelineModel.cpp | 32 +++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/MxcImageProvider.cpp b/src/MxcImageProvider.cpp index 4834c2fc..97cbfff3 100644 --- a/src/MxcImageProvider.cpp +++ b/src/MxcImageProvider.cpp @@ -27,6 +27,7 @@ MxcImageProvider::requestImageResponse(const QString &id, const QSize &requested auto id_ = id; bool crop = true; double radius = 0; + auto size = requestedSize; auto queryStart = id.lastIndexOf('?'); if (queryStart != -1) { @@ -39,11 +40,14 @@ MxcImageProvider::requestImageResponse(const QString &id, const QSize &requested crop = false; } else if (b.startsWith("radius=")) { radius = b.mid(7).toDouble(); + } else if (b.startsWith("height=")) { + size.setHeight(b.mid(7).toInt()); + size.setWidth(0); } } } - return new MxcImageResponse(id_, crop, radius, requestedSize); + return new MxcImageResponse(id_, crop, radius, size); } void @@ -120,7 +124,9 @@ MxcImageProvider::download(const QString &id, if (fileInfo.exists()) { QImage image = utils::readImageFromFile(fileInfo.absoluteFilePath()); if (!image.isNull()) { - image = image.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation); + if (requestedSize != image.size()) { + image = image.scaledToHeight(requestedSize.height(), Qt::SmoothTransformation); + } if (radius != 0) { image = clipRadius(std::move(image), radius); @@ -151,8 +157,10 @@ MxcImageProvider::download(const QString &id, auto data = QByteArray(res.data(), (int)res.size()); QImage image = utils::readImage(data); if (!image.isNull()) { - image = - image.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation); + if (requestedSize != image.size()) { + image = + image.scaledToHeight(requestedSize.height(), Qt::SmoothTransformation); + } if (radius != 0) { image = clipRadius(std::move(image), radius); diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index b9941dfa..9696dffb 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -561,15 +561,37 @@ TimelineModel::data(const mtx::events::collections::TimelineEvents &event, int r } // TODO(Nico): Don't parse html with a regex - const static QRegularExpression matchImgUri("(]*)src=\"mxc://([^\"]*)\"([^>]*>)"); - formattedBody_.replace(matchImgUri, "\\1 src=\"image://mxcImage/\\2\"\\3"); - // Same regex but for single quotes around the src - const static QRegularExpression matchImgUri2("(]*)src=\'mxc://([^\']*)\'([^>]*>)"); - formattedBody_.replace(matchImgUri2, "\\1 src=\"image://mxcImage/\\2\"\\3"); const static QRegularExpression matchEmoticonHeight( "(]*)height=\"([^\"]*)\"([^>]*>)"); formattedBody_.replace(matchEmoticonHeight, QString("\\1 height=\"%1\"\\3").arg(ascent)); + const static QRegularExpression matchIsImg("]+>"); + auto itIsImg = matchIsImg.globalMatch(formattedBody_); + while (itIsImg.hasNext()) { + const QString curImg = itIsImg.next().captured(0); + + // Construct image parameters later used by MxcImageProvider. + QString imgParams; + if (curImg.contains("height")) { + const static QRegularExpression matchImgHeight("height=[\"\']?(\\d+)[\"\']?"); + const auto height = matchImgHeight.match(curImg).captured(1).toInt(); + imgParams = QString("?scale&height=%1").arg(height); + } + + // Replace src in current . + auto imgReplacement = curImg; + const static QRegularExpression matchImgUri("src=\"mxc://([^\"]*)\""); + imgReplacement.replace(matchImgUri, + QString("src=\"image://mxcImage/\\1%1\"").arg(imgParams)); + // Same regex but for single quotes around the src + const static QRegularExpression matchImgUri2("src=\'mxc://([^\']*)\'"); + imgReplacement.replace(matchImgUri2, + QString("src=\'image://mxcImage/\\1%1\'").arg(imgParams)); + + // Replace in formattedBody_ with our new . + formattedBody_.replace(curImg, imgReplacement); + } + return QVariant( utils::replaceEmoji(utils::linkifyMessage(utils::escapeBlacklistedHtml(formattedBody_)))); } From 5ac11954027a3209bc5820db8219a76de7b798a5 Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 27 Dec 2021 22:59:51 +0100 Subject: [PATCH 2/3] MxcImageProvider: Only use scaledToHeight if width <= 0. --- src/MxcImageProvider.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/MxcImageProvider.cpp b/src/MxcImageProvider.cpp index 97cbfff3..74b4bedc 100644 --- a/src/MxcImageProvider.cpp +++ b/src/MxcImageProvider.cpp @@ -124,8 +124,11 @@ MxcImageProvider::download(const QString &id, if (fileInfo.exists()) { QImage image = utils::readImageFromFile(fileInfo.absoluteFilePath()); if (!image.isNull()) { - if (requestedSize != image.size()) { + if (requestedSize.width() <= 0) { image = image.scaledToHeight(requestedSize.height(), Qt::SmoothTransformation); + } else { + image = + image.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation); } if (radius != 0) { @@ -157,9 +160,12 @@ MxcImageProvider::download(const QString &id, auto data = QByteArray(res.data(), (int)res.size()); QImage image = utils::readImage(data); if (!image.isNull()) { - if (requestedSize != image.size()) { + if (requestedSize.width() <= 0) { image = image.scaledToHeight(requestedSize.height(), Qt::SmoothTransformation); + } else { + image = + image.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation); } if (radius != 0) { From 850d139e3d9e9d2ee96ea44666d676af28df4a8f Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 27 Dec 2021 23:16:26 +0100 Subject: [PATCH 3/3] Make custom emoticons twice as high as the font. --- src/timeline/TimelineModel.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index 9696dffb..0bec9505 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -561,25 +561,28 @@ TimelineModel::data(const mtx::events::collections::TimelineEvents &event, int r } // TODO(Nico): Don't parse html with a regex - const static QRegularExpression matchEmoticonHeight( - "(]*)height=\"([^\"]*)\"([^>]*>)"); - formattedBody_.replace(matchEmoticonHeight, QString("\\1 height=\"%1\"\\3").arg(ascent)); - const static QRegularExpression matchIsImg("]+>"); auto itIsImg = matchIsImg.globalMatch(formattedBody_); while (itIsImg.hasNext()) { + // The current tag. const QString curImg = itIsImg.next().captured(0); + // The replacement for the current . + auto imgReplacement = curImg; // Construct image parameters later used by MxcImageProvider. QString imgParams; if (curImg.contains("height")) { - const static QRegularExpression matchImgHeight("height=[\"\']?(\\d+)[\"\']?"); - const auto height = matchImgHeight.match(curImg).captured(1).toInt(); + const static QRegularExpression matchImgHeight("height=([\"\']?)(\\d+)([\"\']?)"); + // Make emoticons twice as high as the font. + if (curImg.contains("data-mx-emoticon")) { + imgReplacement = + imgReplacement.replace(matchImgHeight, "height=\\1%1\\3").arg(ascent * 2); + } + const auto height = matchImgHeight.match(imgReplacement).captured(2).toInt(); imgParams = QString("?scale&height=%1").arg(height); } // Replace src in current . - auto imgReplacement = curImg; const static QRegularExpression matchImgUri("src=\"mxc://([^\"]*)\""); imgReplacement.replace(matchImgUri, QString("src=\"image://mxcImage/\\1%1\"").arg(imgParams));