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 <QX11Info> | ||||
27 | #include <QDebug> | ||||
28 | | ||||
29 | #include <X11/Xft/Xft.h> | ||||
30 | | ||||
31 | #ifdef HAVE_FONTCONFIG | ||||
32 | | ||||
33 | | ||||
34 | static int qtToFcWeight(int weight) | ||||
35 | { | ||||
36 | switch(weight) | ||||
37 | { | ||||
38 | case 0: | ||||
39 | return FC_WEIGHT_THIN; | ||||
40 | case QFont::Light>>1: | ||||
41 | return FC_WEIGHT_EXTRALIGHT; | ||||
42 | case QFont::Light: | ||||
43 | return FC_WEIGHT_LIGHT; | ||||
44 | default: | ||||
45 | case QFont::Normal: | ||||
46 | return FC_WEIGHT_REGULAR; | ||||
47 | case (QFont::Normal+QFont::DemiBold)>>1: | ||||
48 | #ifdef KFI_HAVE_MEDIUM_WEIGHT | ||||
49 | return FC_WEIGHT_MEDIUM; | ||||
50 | #endif | ||||
51 | case QFont::DemiBold: | ||||
52 | return FC_WEIGHT_DEMIBOLD; | ||||
53 | case QFont::Bold: | ||||
54 | return FC_WEIGHT_BOLD; | ||||
55 | case (QFont::Bold+QFont::Black)>>1: | ||||
56 | return FC_WEIGHT_EXTRABOLD; | ||||
57 | case QFont::Black: | ||||
58 | return FC_WEIGHT_BLACK; | ||||
59 | } | ||||
60 | } | ||||
61 | | ||||
62 | #ifndef KFI_FC_NO_WIDTHS | ||||
63 | static int qtToFcWidth(int weight) | ||||
64 | { | ||||
65 | switch(weight) | ||||
66 | { | ||||
67 | case QFont::UltraCondensed: | ||||
68 | return KFI_FC_WIDTH_ULTRACONDENSED; | ||||
69 | case QFont::ExtraCondensed: | ||||
70 | return KFI_FC_WIDTH_EXTRACONDENSED; | ||||
71 | case QFont::Condensed: | ||||
72 | return KFI_FC_WIDTH_CONDENSED; | ||||
73 | case QFont::SemiCondensed: | ||||
74 | return KFI_FC_WIDTH_SEMICONDENSED; | ||||
75 | default: | ||||
76 | case QFont::Unstretched: | ||||
77 | return KFI_FC_WIDTH_NORMAL; | ||||
78 | case QFont::SemiExpanded: | ||||
79 | return KFI_FC_WIDTH_SEMIEXPANDED; | ||||
80 | case QFont::Expanded: | ||||
81 | return KFI_FC_WIDTH_EXPANDED; | ||||
82 | case QFont::ExtraExpanded: | ||||
83 | return KFI_FC_WIDTH_EXTRAEXPANDED; | ||||
84 | case QFont::UltraExpanded: | ||||
85 | return KFI_FC_WIDTH_ULTRAEXPANDED; | ||||
86 | } | ||||
87 | } | ||||
88 | #endif | ||||
89 | | ||||
90 | static bool qtToFcSlant(int slant) | ||||
91 | { | ||||
92 | switch(slant) | ||||
93 | { | ||||
94 | default: | ||||
95 | case QFont::StyleNormal: | ||||
96 | return FC_SLANT_ROMAN; | ||||
97 | case QFont::StyleItalic: | ||||
98 | return FC_SLANT_ITALIC; | ||||
99 | case QFont::StyleOblique: | ||||
100 | return FC_SLANT_OBLIQUE; | ||||
101 | } | ||||
102 | } | ||||
103 | | ||||
104 | static quint32 qtToFcStyle(const QFont &font) | ||||
105 | { | ||||
106 | return KFI::FC::createStyleVal( | ||||
107 | qtToFcWeight(font.weight()), | ||||
108 | qtToFcWidth(font.stretch()), | ||||
109 | qtToFcSlant(font.style()) | ||||
110 | ); | ||||
111 | } | ||||
112 | | ||||
113 | static bool hasStr(XftFont *font, QString &str) | ||||
114 | { | ||||
115 | unsigned int slen=str.length(), | ||||
116 | ch; | ||||
117 | | ||||
118 | for(ch=0; ch<slen; ++ch) | ||||
119 | if(!FcCharSetHasChar(font->charset, str[ch].unicode())) | ||||
120 | return false; | ||||
121 | return true; | ||||
122 | } | ||||
123 | | ||||
124 | | ||||
125 | PreviewRenderEngine::PreviewRenderEngine(bool init) | ||||
126 | : CFcEngine(init) | ||||
127 | { | ||||
128 | if(init) | ||||
129 | FcInitReinitialize(); | ||||
130 | } | ||||
131 | | ||||
132 | PreviewRenderEngine::~PreviewRenderEngine() | ||||
133 | { | ||||
134 | } | ||||
135 | | ||||
136 | QImage PreviewRenderEngine::drawAutoSize(const QFont &font, const QColor &txt, const QColor &bgnd, const QString &text) | ||||
137 | { | ||||
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 | const QString& name = font.family(); | ||||
139 | const quint32 style = qtToFcStyle(font); | ||||
140 | int faceNo = 3; | ||||
141 | | ||||
142 | int fSize((int)(((font.pointSizeF()*QX11Info::appDpiY())/72.0)+0.5)); | ||||
143 | | ||||
144 | return draw(name, style, faceNo, txt, bgnd, fSize, text); | ||||
145 | } | ||||
146 | | ||||
147 | #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.