File indexing completed on 2024-05-19 05:38:06

0001 /*
0002     SPDX-FileCopyrightText: 2018 Julian Wolff <wolff@julianwolff.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "previewrenderengine.h"
0008 #include "Fc.h"
0009 
0010 #include <QApplication>
0011 #include <QScreen>
0012 #include <private/qtx11extras_p.h>
0013 
0014 #include <X11/Xft/Xft.h>
0015 
0016 #ifdef HAVE_FONTCONFIG
0017 
0018 static int qtToFcWeight(int weight)
0019 {
0020     switch (weight) {
0021     case 0:
0022         return FC_WEIGHT_THIN;
0023     case QFont::Light >> 1:
0024         return FC_WEIGHT_EXTRALIGHT;
0025     case QFont::Light:
0026         return FC_WEIGHT_LIGHT;
0027     default:
0028     case QFont::Normal:
0029         return FC_WEIGHT_REGULAR;
0030     case (QFont::Normal + QFont::DemiBold) >> 1:
0031 #ifdef KFI_HAVE_MEDIUM_WEIGHT
0032         return FC_WEIGHT_MEDIUM;
0033 #endif
0034     case QFont::DemiBold:
0035         return FC_WEIGHT_DEMIBOLD;
0036     case QFont::Bold:
0037         return FC_WEIGHT_BOLD;
0038     case (QFont::Bold + QFont::Black) >> 1:
0039         return FC_WEIGHT_EXTRABOLD;
0040     case QFont::Black:
0041         return FC_WEIGHT_BLACK;
0042     }
0043 }
0044 
0045 #ifndef KFI_FC_NO_WIDTHS
0046 static int qtToFcWidth(int weight)
0047 {
0048     switch (weight) {
0049     case QFont::UltraCondensed:
0050         return KFI_FC_WIDTH_ULTRACONDENSED;
0051     case QFont::ExtraCondensed:
0052         return KFI_FC_WIDTH_EXTRACONDENSED;
0053     case QFont::Condensed:
0054         return KFI_FC_WIDTH_CONDENSED;
0055     case QFont::SemiCondensed:
0056         return KFI_FC_WIDTH_SEMICONDENSED;
0057     default:
0058     case QFont::Unstretched:
0059         return KFI_FC_WIDTH_NORMAL;
0060     case QFont::SemiExpanded:
0061         return KFI_FC_WIDTH_SEMIEXPANDED;
0062     case QFont::Expanded:
0063         return KFI_FC_WIDTH_EXPANDED;
0064     case QFont::ExtraExpanded:
0065         return KFI_FC_WIDTH_EXTRAEXPANDED;
0066     case QFont::UltraExpanded:
0067         return KFI_FC_WIDTH_ULTRAEXPANDED;
0068     }
0069 }
0070 #endif
0071 
0072 static bool qtToFcSlant(int slant)
0073 {
0074     switch (slant) {
0075     default:
0076     case QFont::StyleNormal:
0077         return FC_SLANT_ROMAN;
0078     case QFont::StyleItalic:
0079         return FC_SLANT_ITALIC;
0080     case QFont::StyleOblique:
0081         return FC_SLANT_OBLIQUE;
0082     }
0083 }
0084 
0085 static quint32 qtToFcStyle(const QFont &font)
0086 {
0087     return KFI::FC::createStyleVal(qtToFcWeight(font.weight()), qtToFcWidth(font.stretch()), qtToFcSlant(font.style()));
0088 }
0089 
0090 PreviewRenderEngine::PreviewRenderEngine(bool init)
0091     : CFcEngine(init)
0092 {
0093     if (init)
0094         FcInitReinitialize();
0095 }
0096 
0097 PreviewRenderEngine::~PreviewRenderEngine()
0098 {
0099 }
0100 
0101 QImage PreviewRenderEngine::drawAutoSize(const QFont &font, const QColor &txt, const QColor &bgnd, const QString &text)
0102 {
0103     const QString &name = font.family();
0104     const quint32 style = qtToFcStyle(font);
0105     int faceNo = 0;
0106 
0107     double ratio = QGuiApplication::primaryScreen()->devicePixelRatio();
0108     double dpi = QX11Info::appDpiY();
0109 
0110     int fSize((int)(((font.pointSizeF() * dpi * ratio) / 72.0) + 0.5));
0111 
0112     QImage image(draw(name, style, faceNo, txt, bgnd, fSize, text));
0113     image.setDevicePixelRatio(ratio);
0114     return image;
0115 }
0116 
0117 #endif // HAVE_FONTCONFIG