Changeset View
Changeset View
Standalone View
Standalone View
kcms/fonts/previewrenderengine.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (c) 2018 Julian Wolff <wolff@julianwolff.de> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Library General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2 of the License, or (at your option) any later version. | ||||
8 | | ||||
9 | This library is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
12 | Library General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Library General Public License | ||||
15 | along with this library; see the file COPYING.LIB. If not, write to | ||||
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
17 | Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | | ||||
21 | | ||||
22 | #include "kxftconfig.h" | ||||
23 | #include "previewrenderengine.h" | ||||
24 | #include "Fc.h" | ||||
25 | | ||||
26 | #include <QApplication> | ||||
27 | #include <QDesktopWidget> | ||||
28 | #include <QX11Info> | ||||
29 | | ||||
30 | #include <X11/Xft/Xft.h> | ||||
31 | | ||||
32 | #ifdef HAVE_FONTCONFIG | ||||
33 | | ||||
34 | | ||||
35 | static int qtToFcWeight(int weight) | ||||
36 | { | ||||
37 | switch(weight) | ||||
38 | { | ||||
39 | case 0: | ||||
40 | return FC_WEIGHT_THIN; | ||||
41 | case QFont::Light>>1: | ||||
42 | return FC_WEIGHT_EXTRALIGHT; | ||||
43 | case QFont::Light: | ||||
44 | return FC_WEIGHT_LIGHT; | ||||
45 | default: | ||||
46 | case QFont::Normal: | ||||
47 | return FC_WEIGHT_REGULAR; | ||||
48 | case (QFont::Normal+QFont::DemiBold)>>1: | ||||
49 | #ifdef KFI_HAVE_MEDIUM_WEIGHT | ||||
50 | return FC_WEIGHT_MEDIUM; | ||||
51 | #endif | ||||
52 | case QFont::DemiBold: | ||||
53 | return FC_WEIGHT_DEMIBOLD; | ||||
54 | case QFont::Bold: | ||||
55 | return FC_WEIGHT_BOLD; | ||||
56 | case (QFont::Bold+QFont::Black)>>1: | ||||
57 | return FC_WEIGHT_EXTRABOLD; | ||||
58 | case QFont::Black: | ||||
59 | return FC_WEIGHT_BLACK; | ||||
60 | } | ||||
61 | } | ||||
62 | | ||||
63 | #ifndef KFI_FC_NO_WIDTHS | ||||
64 | static int qtToFcWidth(int weight) | ||||
65 | { | ||||
66 | switch(weight) | ||||
67 | { | ||||
68 | case QFont::UltraCondensed: | ||||
69 | return KFI_FC_WIDTH_ULTRACONDENSED; | ||||
70 | case QFont::ExtraCondensed: | ||||
71 | return KFI_FC_WIDTH_EXTRACONDENSED; | ||||
72 | case QFont::Condensed: | ||||
73 | return KFI_FC_WIDTH_CONDENSED; | ||||
74 | case QFont::SemiCondensed: | ||||
75 | return KFI_FC_WIDTH_SEMICONDENSED; | ||||
76 | default: | ||||
77 | case QFont::Unstretched: | ||||
78 | return KFI_FC_WIDTH_NORMAL; | ||||
79 | case QFont::SemiExpanded: | ||||
80 | return KFI_FC_WIDTH_SEMIEXPANDED; | ||||
81 | case QFont::Expanded: | ||||
82 | return KFI_FC_WIDTH_EXPANDED; | ||||
83 | case QFont::ExtraExpanded: | ||||
84 | return KFI_FC_WIDTH_EXTRAEXPANDED; | ||||
85 | case QFont::UltraExpanded: | ||||
86 | return KFI_FC_WIDTH_ULTRAEXPANDED; | ||||
87 | } | ||||
88 | } | ||||
89 | #endif | ||||
90 | | ||||
91 | static bool qtToFcSlant(int slant) | ||||
92 | { | ||||
93 | switch(slant) | ||||
94 | { | ||||
95 | default: | ||||
96 | case QFont::StyleNormal: | ||||
97 | return FC_SLANT_ROMAN; | ||||
98 | case QFont::StyleItalic: | ||||
99 | return FC_SLANT_ITALIC; | ||||
100 | case QFont::StyleOblique: | ||||
101 | return FC_SLANT_OBLIQUE; | ||||
102 | } | ||||
103 | } | ||||
104 | | ||||
105 | static quint32 qtToFcStyle(const QFont &font) | ||||
106 | { | ||||
107 | return KFI::FC::createStyleVal( | ||||
108 | qtToFcWeight(font.weight()), | ||||
109 | qtToFcWidth(font.stretch()), | ||||
110 | qtToFcSlant(font.style()) | ||||
111 | ); | ||||
112 | } | ||||
113 | | ||||
114 | PreviewRenderEngine::PreviewRenderEngine(bool init) | ||||
115 | : CFcEngine(init) | ||||
116 | { | ||||
117 | if(init) | ||||
118 | FcInitReinitialize(); | ||||
119 | } | ||||
120 | | ||||
121 | PreviewRenderEngine::~PreviewRenderEngine() | ||||
122 | { | ||||
123 | } | ||||
124 | | ||||
125 | QImage PreviewRenderEngine::drawAutoSize(const QFont &font, const QColor &txt, const QColor &bgnd, const QString &text) | ||||
126 | { | ||||
127 | const QString& name = font.family(); | ||||
128 | const quint32 style = qtToFcStyle(font); | ||||
129 | int faceNo = 0; | ||||
130 | | ||||
131 | double ratio = QApplication::desktop()->screen()->devicePixelRatio(); | ||||
132 | double dpi = QX11Info::appDpiY(); | ||||
133 | | ||||
134 | int fSize((int)(((font.pointSizeF()*dpi*ratio)/72.0)+0.5)); | ||||
135 | | ||||
136 | QImage image(draw(name, style, faceNo, txt, bgnd, fSize, text)); | ||||
137 | image.setDevicePixelRatio(ratio); | ||||
davidedmundson: change to
QImage image(draw(name, style,...));
image.setDevicePixelRatio(ratio);
return image… | |||||
progwolff: Thanks, this would be much cleaner. Sadly it doesn't seem to work.
I tried this before. Also… | |||||
It's a bug in Qt: https://bugreports.qt.io/browse/QTBUG-38127 Workaround: set the sourceSize to something. progwolff: It's a bug in Qt: https://bugreports.qt.io/browse/QTBUG-38127
Workaround: set the sourceSize… | |||||
mart: can you put the workaround with a giant comment that points to the qt bug? | |||||
138 | return image; | ||||
139 | } | ||||
140 | | ||||
141 | #endif // HAVE_FONTCONFIG |
change to
QImage image(draw(name, style,...));
image.setDevicePixelRatio(ratio);
return image;
This keeps the metadata of the image scaling with the image, and then the Image item knows automatically what the logical of this image is.