mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-25 20:48:52 +03:00
Merge pull request #858 from tastytea/inline-img-aa
Request inline images in the right size and anti-alias them
This commit is contained in:
commit
bca7309e5f
2 changed files with 51 additions and 12 deletions
|
@ -27,6 +27,7 @@ MxcImageProvider::requestImageResponse(const QString &id, const QSize &requested
|
||||||
auto id_ = id;
|
auto id_ = id;
|
||||||
bool crop = true;
|
bool crop = true;
|
||||||
double radius = 0;
|
double radius = 0;
|
||||||
|
auto size = requestedSize;
|
||||||
|
|
||||||
auto queryStart = id.lastIndexOf('?');
|
auto queryStart = id.lastIndexOf('?');
|
||||||
if (queryStart != -1) {
|
if (queryStart != -1) {
|
||||||
|
@ -39,11 +40,14 @@ MxcImageProvider::requestImageResponse(const QString &id, const QSize &requested
|
||||||
crop = false;
|
crop = false;
|
||||||
} else if (b.startsWith("radius=")) {
|
} else if (b.startsWith("radius=")) {
|
||||||
radius = b.mid(7).toDouble();
|
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
|
void
|
||||||
|
@ -120,7 +124,12 @@ MxcImageProvider::download(const QString &id,
|
||||||
if (fileInfo.exists()) {
|
if (fileInfo.exists()) {
|
||||||
QImage image = utils::readImageFromFile(fileInfo.absoluteFilePath());
|
QImage image = utils::readImageFromFile(fileInfo.absoluteFilePath());
|
||||||
if (!image.isNull()) {
|
if (!image.isNull()) {
|
||||||
image = image.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
if (requestedSize.width() <= 0) {
|
||||||
|
image = image.scaledToHeight(requestedSize.height(), Qt::SmoothTransformation);
|
||||||
|
} else {
|
||||||
|
image =
|
||||||
|
image.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
||||||
|
}
|
||||||
|
|
||||||
if (radius != 0) {
|
if (radius != 0) {
|
||||||
image = clipRadius(std::move(image), radius);
|
image = clipRadius(std::move(image), radius);
|
||||||
|
@ -151,8 +160,13 @@ MxcImageProvider::download(const QString &id,
|
||||||
auto data = QByteArray(res.data(), (int)res.size());
|
auto data = QByteArray(res.data(), (int)res.size());
|
||||||
QImage image = utils::readImage(data);
|
QImage image = utils::readImage(data);
|
||||||
if (!image.isNull()) {
|
if (!image.isNull()) {
|
||||||
image =
|
if (requestedSize.width() <= 0) {
|
||||||
image.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
image =
|
||||||
|
image.scaledToHeight(requestedSize.height(), Qt::SmoothTransformation);
|
||||||
|
} else {
|
||||||
|
image =
|
||||||
|
image.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
||||||
|
}
|
||||||
|
|
||||||
if (radius != 0) {
|
if (radius != 0) {
|
||||||
image = clipRadius(std::move(image), radius);
|
image = clipRadius(std::move(image), radius);
|
||||||
|
|
|
@ -561,14 +561,39 @@ TimelineModel::data(const mtx::events::collections::TimelineEvents &event, int r
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(Nico): Don't parse html with a regex
|
// TODO(Nico): Don't parse html with a regex
|
||||||
const static QRegularExpression matchImgUri("(<img [^>]*)src=\"mxc://([^\"]*)\"([^>]*>)");
|
const static QRegularExpression matchIsImg("<img [^>]+>");
|
||||||
formattedBody_.replace(matchImgUri, "\\1 src=\"image://mxcImage/\\2\"\\3");
|
auto itIsImg = matchIsImg.globalMatch(formattedBody_);
|
||||||
// Same regex but for single quotes around the src
|
while (itIsImg.hasNext()) {
|
||||||
const static QRegularExpression matchImgUri2("(<img [^>]*)src=\'mxc://([^\']*)\'([^>]*>)");
|
// The current <img> tag.
|
||||||
formattedBody_.replace(matchImgUri2, "\\1 src=\"image://mxcImage/\\2\"\\3");
|
const QString curImg = itIsImg.next().captured(0);
|
||||||
const static QRegularExpression matchEmoticonHeight(
|
// The replacement for the current <img>.
|
||||||
"(<img data-mx-emoticon [^>]*)height=\"([^\"]*)\"([^>]*>)");
|
auto imgReplacement = curImg;
|
||||||
formattedBody_.replace(matchEmoticonHeight, QString("\\1 height=\"%1\"\\3").arg(ascent));
|
|
||||||
|
// Construct image parameters later used by MxcImageProvider.
|
||||||
|
QString imgParams;
|
||||||
|
if (curImg.contains("height")) {
|
||||||
|
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 <img>.
|
||||||
|
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(
|
return QVariant(
|
||||||
utils::replaceEmoji(utils::linkifyMessage(utils::escapeBlacklistedHtml(formattedBody_))));
|
utils::replaceEmoji(utils::linkifyMessage(utils::escapeBlacklistedHtml(formattedBody_))));
|
||||||
|
|
Loading…
Reference in a new issue