Commit e560ea67 authored by Fushan Wen's avatar Fushan Wen 💬
Browse files

kcms/fonts: Fix font hinting preview

In `combineImages`, the width and the height were calculated
incorrectly. This makes the preview image correctly obey the device
pixel ratio of the primary screen, so the preview image can be clear
and sharp.

BUG: 413673
FIXED-IN: 5.25.1


(cherry picked from commit b96d56ae)
parent a5fea985
Pipeline #192957 passed with stage
in 8 minutes and 1 second
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
SPDX-License-Identifier: LGPL-2.0-or-later SPDX-License-Identifier: LGPL-2.0-or-later
*/ */
#include <cmath>
#include <QApplication> #include <QApplication>
#include <QPainter> #include <QPainter>
#include <QPalette> #include <QPalette>
...@@ -14,36 +16,51 @@ ...@@ -14,36 +16,51 @@
#include "previewimageprovider.h" #include "previewimageprovider.h"
#include "previewrenderengine.h" #include "previewrenderengine.h"
QImage combineImages(const QList<QImage> &images, const QColor &bgnd, int spacing = 0) /**
* @brief Combines a list of images vertically into one image
*
* Combines a list of images vertically with the given spacing
* between adjacent rows of images.
* The width of the combined image is the width of the longest
* image in the list, and the height is the sum of the heights
* of all images in the list.
* The device pixel ratio of the combined image is the same
* as that of the first image in the list.
*
* @param images the list of images to be combined
* @param bgnd the background color of the combined image
* @param _spacing the spacing between adjacent rows of images
* @return the combined image
*/
QImage combineImages(const QList<QImage> &images, const QColor &bgnd, int _spacing = 0)
{ {
if (images.empty()) {
return QImage();
}
int width = 0; int width = 0;
int height = 0; int height = 0;
QImage::Format format = QImage::Format_Invalid; const double devicePixelRatio = images.at(0).devicePixelRatio();
int devicePixelRatio = 1; const int spacing = std::lround(_spacing * devicePixelRatio);
for (const auto &image : images) { for (const auto &image : images) {
if (width < image.width()) { if (width < image.width()) {
width = image.width(); width = image.width();
} }
height += image.height() + spacing; height += image.height();
format = image.format();
devicePixelRatio = image.devicePixelRatio();
} }
height -= spacing; height += spacing * images.count();
// To correctly align the image pixels on a high dpi display, // To correctly align the image pixels on a high dpi display,
// the image dimensions need to be a multiple of devicePixelRatio // the image dimensions need to be a multiple of devicePixelRatio
width = (width + devicePixelRatio - 1) / devicePixelRatio * devicePixelRatio; QImage combinedImage(width * devicePixelRatio, height * devicePixelRatio, images.at(0).format());
height = (height + devicePixelRatio - 1) / devicePixelRatio * devicePixelRatio;
QImage combinedImage(width, height, format);
combinedImage.setDevicePixelRatio(devicePixelRatio); combinedImage.setDevicePixelRatio(devicePixelRatio);
combinedImage.fill(bgnd); combinedImage.fill(bgnd);
int offset = 0; int offset = spacing; // Top margin
QPainter p(&combinedImage); QPainter p(&combinedImage);
for (const auto &image : images) { for (const auto &image : images) {
p.drawImage(0, offset, image); p.drawImage(0, offset, image);
offset += (image.height() + spacing) / devicePixelRatio; offset += image.height() + spacing;
} }
return combinedImage; return combinedImage;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment