Files
tdesktop/Telegram/SourceFiles/chat_helpers/stickers_emoji_image_loader.cpp
allhaileris afb81b8278
Some checks failed
Docker. / Ubuntu (push) Has been cancelled
User-agent updater. / User-agent (push) Failing after 15s
Lock Threads / lock (push) Failing after 10s
Waiting for answer. / waiting-for-answer (push) Failing after 22s
Close stale issues and PRs / stale (push) Successful in 13s
Needs user action. / needs-user-action (push) Failing after 8s
Can't reproduce. / cant-reproduce (push) Failing after 8s
init
2026-02-16 15:50:16 +03:00

100 lines
2.4 KiB
C++

/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "chat_helpers/stickers_emoji_image_loader.h"
#include "styles/style_chat.h"
#include <QtCore/QtMath>
namespace Stickers {
EmojiImageLoader::EmojiImageLoader(crl::weak_on_queue<EmojiImageLoader> weak)
: _weak(std::move(weak)) {
}
void EmojiImageLoader::init(
std::shared_ptr<UniversalImages> images,
bool largeEnabled) {
Expects(images != nullptr);
_images = std::move(images);
if (largeEnabled) {
_images->ensureLoaded();
}
}
QImage EmojiImageLoader::prepare(EmojiPtr emoji) const {
const auto loaded = _images->ensureLoaded();
const auto factor = style::DevicePixelRatio();
const auto side = st::largeEmojiSize + 2 * st::largeEmojiOutline;
auto tinted = QImage(
QSize(st::largeEmojiSize, st::largeEmojiSize) * factor,
QImage::Format_ARGB32_Premultiplied);
tinted.fill(Qt::white);
if (loaded) {
QPainter p(&tinted);
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
_images->draw(
p,
emoji,
st::largeEmojiSize * factor,
0,
0);
}
auto result = QImage(
QSize(side, side) * factor,
QImage::Format_ARGB32_Premultiplied);
result.fill(Qt::transparent);
if (loaded) {
QPainter p(&result);
const auto delta = st::largeEmojiOutline * factor;
const auto planar = std::array<QPoint, 4>{ {
{ 0, -1 },
{ -1, 0 },
{ 1, 0 },
{ 0, 1 },
} };
for (const auto &shift : planar) {
for (auto i = 0; i != delta; ++i) {
p.drawImage(QPoint(delta, delta) + shift * (i + 1), tinted);
}
}
const auto diagonal = std::array<QPoint, 4>{ {
{ -1, -1 },
{ 1, -1 },
{ -1, 1 },
{ 1, 1 },
} };
const auto corrected = int(base::SafeRound(delta / M_SQRT2));
for (const auto &shift : diagonal) {
for (auto i = 0; i != corrected; ++i) {
p.drawImage(QPoint(delta, delta) + shift * (i + 1), tinted);
}
}
_images->draw(
p,
emoji,
st::largeEmojiSize * factor,
delta,
delta);
}
return result;
}
void EmojiImageLoader::switchTo(std::shared_ptr<UniversalImages> images) {
_images = std::move(images);
}
auto EmojiImageLoader::releaseImages() -> std::shared_ptr<UniversalImages> {
return std::exchange(
_images,
std::make_shared<UniversalImages>(_images->id()));
}
} // namespace Stickers