summary refs log tree commit diff
path: root/src/timeline
diff options
context:
space:
mode:
authortastytea <tastytea@tastytea.de>2021-12-27 00:17:27 +0100
committertastytea <tastytea@tastytea.de>2021-12-27 22:47:51 +0100
commit7086e23bdd73143529882e221b8f0963c8a7e82c (patch)
treea8521a79b8859b8edc50c8e138ff896070f8ca55 /src/timeline
parentUse body in alt text of custom emoji (diff)
downloadnheko-7086e23bdd73143529882e221b8f0963c8a7e82c.tar.xz
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.
Diffstat (limited to 'src/timeline')
-rw-r--r--src/timeline/TimelineModel.cpp32
1 files changed, 27 insertions, 5 deletions
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("(<img [^>]*)src=\"mxc://([^\"]*)\"([^>]*>)");
-        formattedBody_.replace(matchImgUri, "\\1 src=\"image://mxcImage/\\2\"\\3");
-        // Same regex but for single quotes around the src
-        const static QRegularExpression matchImgUri2("(<img [^>]*)src=\'mxc://([^\']*)\'([^>]*>)");
-        formattedBody_.replace(matchImgUri2, "\\1 src=\"image://mxcImage/\\2\"\\3");
         const static QRegularExpression matchEmoticonHeight(
           "(<img data-mx-emoticon [^>]*)height=\"([^\"]*)\"([^>]*>)");
         formattedBody_.replace(matchEmoticonHeight, QString("\\1 height=\"%1\"\\3").arg(ascent));
 
+        const static QRegularExpression matchIsImg("<img [^>]+>");
+        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 <img>.
+            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 <img> in formattedBody_ with our new <img>.
+            formattedBody_.replace(curImg, imgReplacement);
+        }
+
         return QVariant(
           utils::replaceEmoji(utils::linkifyMessage(utils::escapeBlacklistedHtml(formattedBody_))));
     }