forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TQtPadFont.cxx
320 lines (282 loc) · 9.82 KB
/
TQtPadFont.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Author: Valeri Fine 21/01/2002
/****************************************************************************
** $Id$
**
** Copyright (C) 2002 by Valeri Fine. Brookhaven National Laboratory.
** All rights reserved.
**
**
*****************************************************************************/
/////////////////////////////////////////////////////////////////////////////////
//
// TQtPadFont class is Qt QFont class with TAttText ROOT class interface
//
/////////////////////////////////////////////////////////////////////////////////
#include "TQtPadFont.h"
#include "TSystem.h"
#include "TMath.h"
#include <QFontMetrics>
#include <QDebug>
TString TQtPadFont::fgRomanFontName = "Times New Roman";
TString TQtPadFont::fgArialFontName = "Arial";
TString TQtPadFont::fgCourierFontName = "Courier New";
TString TQtPadFont::fgSymbolFontFamily= "Symbol";
////////////////////////////////////////////////////////////////////////////////
/// Use the ROOT font with ID=1 to calibrate the current font on fly;
/// Environment variable ROOTFONTFACTOR allows to set the factor manually
static float CalibrateFont()
{
static float fontCalibFactor = -1;
if (fontCalibFactor < 0 ) {
const char * envFactor = gSystem->Getenv("ROOTFONTFACTOR");
bool ok=false;
if (envFactor && envFactor[0])
fontCalibFactor= QString(envFactor).toFloat(&ok);
if (!ok) {
TQtPadFont pattern;
pattern.SetTextFont(62);
int w,h;
QFontMetrics metrics(pattern);
w = metrics.width("This is a PX distribution");
h = metrics.height();
// X11 returns h = 12
// XFT returns h = 14
// WIN32 TTF returns h = 16
// Nimbus Roman returns h = 18
// Qt4 XFT h = 21
qDebug() << " Font metric w = " << w <<" h = "<< h
<< "points=" << pattern.pointSize()
<< "pixels=" << pattern.pixelSize()
<< pattern;
// graf2d/graf/src/TTF.cxx:const Float_t kScale = 0.93376068
const Float_t kScale = 0.95; //0.93376068;
float f;
switch (h) {
case 12: f = 1.10; break;// it was f = 1.13 :-(;
case 14: f = 0.915; break;// it was f = 0.94 :-(;
case 16: f = 0.965; break;// to be tested yet
case 18: f = 0.92; break;// to be tested yet
case 20: f = 0.99; break;// to be tested yet
case 21: f = 0.90; break;// to be tested yet
default: f = kScale; break;
}
fontCalibFactor = f;
}
}
return fontCalibFactor;
}
////////////////////////////////////////////////////////////////////////////////
/// Adjust the font size to match that for Postscipt format
static inline float FontMagicFactor(float size)
{
static float calibration =0;
if (calibration == 0) calibration = CalibrateFont();
return TMath::Max(calibration*size,Float_t(1.0));
}
////////////////////////////////////////////////////////////////////////////////
TQtPadFont::TQtPadFont(): TAttText()
{fTextFont = -1;fTextSize = -1; }
////////////////////////////////////////////////////////////////////////////////
void TQtPadFont::SetTextFont(const char *fontname, int italic_, int bold_)
{
//*-* mode : Option message
//*-* italic : Italic attribut of the TTF font
//*-* bold : Weight attribute of the TTF font
//*-* fontname : the name of True Type Font (TTF) to draw text.
//*-*
//*-* Set text font to specified name. This function returns 0 if
//*-* the specified font is found, 1 if not.
this->setWeight((long) bold_*10);
this->setItalic((Bool_t)italic_);
this->setFamily(fontname);
if (!strcmp(fontname,RomanFontName())) {
this->setStyleHint(QFont::Serif);
} else if (!strcmp(fontname,ArialFontName())) {
this->setStyleHint(QFont::SansSerif);
} else if (!strcmp(fontname,CourierFontName())){
this->setStyleHint(QFont::TypeWriter);
}
this->setStyleStrategy(QFont::PreferDevice);
#if 0
qDebug() << "TQtPadFont::SetTextFont:" << fontname
<< this->lastResortFamily ()
<< this->lastResortFont ()
<< this->substitute (fontname)
<< "ROOT font number=" << fTextFont;
#endif
#if 0
qDebug() << "TGQt::SetTextFont font:" << fontname
<< " bold=" << bold_
<< " italic="<< italic_;
#endif
}
////////////////////////////////////////////////////////////////////////////////
///*-*-*-*-*-*-*-*-*-*-*-*-*Set current text font number*-*-*-*-*-*-*-*-*-*-*-*
///*-* ===========================
///*-* List of the currently supported fonts (screen and PostScript)
///*-* =============================================================
///*-* Font ID X11 Win32 TTF lfItalic lfWeight x 10
///*-* 1 : times-medium-i-normal "Times New Roman" 1 5
///*-* 2 : times-bold-r-normal "Times New Roman" 0 8
///*-* 3 : times-bold-i-normal "Times New Roman" 1 8
///*-* 4 : helvetica-medium-r-normal "Arial" 0 5
///*-* 5 : helvetica-medium-o-normal "Arial" 1 5
///*-* 6 : helvetica-bold-r-normal "Arial" 0 8
///*-* 7 : helvetica-bold-o-normal "Arial" 1 8
///*-* 8 : courier-medium-r-normal "Courier New" 0 5
///*-* 9 : courier-medium-o-normal "Courier New" 1 5
///*-* 10 : courier-bold-r-normal "Courier New" 0 8
///*-* 11 : courier-bold-o-normal "Courier New" 1 8
///*-* 12 : symbol-medium-r-normal "Symbol" 0 6
///*-* 13 : times-medium-r-normal "Times New Roman" 0 5
///*-* 14 : "Wingdings" 0 5
void TQtPadFont::SetTextFont(Font_t fontnumber)
{
if ( (fTextFont == fontnumber) || (fontnumber <0) ) return;
TAttText::SetTextFont(fontnumber);
int it, bld;
const char *fontName;
switch(fTextFont/10) {
case 1:
it = 1;
bld = 5;
fontName = RomanFontName();
break;
case 2:
it = 0;
bld = 8;
fontName = RomanFontName();
break;
case 3:
it = 1;
bld = 8;
fontName = RomanFontName();
break;
case 4:
it = 0;
bld = 5;
fontName = ArialFontName();
break;
case 5:
it = 1;
bld = 5;
fontName = ArialFontName();
break;
case 6:
it = 0;
bld = 8;
fontName = ArialFontName();
break;
case 7:
it = 1;
bld = 8;
fontName = ArialFontName();
break;
case 8:
it = 0;
bld = 5;
fontName = CourierFontName();
break;
case 9:
it = 1;
bld = 5;
fontName = CourierFontName();
break;
case 10:
it = 0;
bld = 8;
fontName = CourierFontName();
break;
case 11:
it = 1;
bld = 8;
fontName = CourierFontName();
break;
case 12:
it = 0;
bld = 5;
fontName = SymbolFontFamily();
break;
case 13:
it = 0;
bld = 5;
fontName = RomanFontName();
break;
case 14:
it = 0;
bld = 5;
fontName = "Wingdings";
break;
default:
it = 0;
bld = 5;
fontName = RomanFontName();
break;
}
SetTextFont(fontName, it, bld);
}
////////////////////////////////////////////////////////////////////////////////
///*-*-*-*-*-*-*-*-*-*-*-*-*Set current text size*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
///*-* =====================
void TQtPadFont::SetTextSize(Float_t textsize)
{
if ( fTextSize != textsize ) {
TAttText::SetTextSize(textsize);
if (fTextSize > 0) {
Int_t tsize =(Int_t)( textsize+0.5);
this->setPixelSize(static_cast<int>(FontMagicFactor(tsize)));
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// Set the text pixel size
void TQtPadFont::SetTextSizePixels(Int_t npixels)
{
SetTextSize(static_cast<float>(npixels));
}
////////////////////////////////////////////////////////////////////////////////
/// Get the system name for the "Roman" font
const char *TQtPadFont::RomanFontName()
{
return fgRomanFontName;
}
////////////////////////////////////////////////////////////////////////////////
/// Get the system name for the "Arial" font
const char *TQtPadFont::ArialFontName()
{
return fgArialFontName;
}
////////////////////////////////////////////////////////////////////////////////
/// Get the system name for the "Courier" font
const char *TQtPadFont::CourierFontName()
{
return fgCourierFontName;
}
////////////////////////////////////////////////////////////////////////////////
/// Get the system name for the "Symbol" font
const char *TQtPadFont::SymbolFontFamily()
{
return fgSymbolFontFamily;
}
////////////////////////////////////////////////////////////////////////////////
/// Set the system name for the "Symbol" font
void TQtPadFont::SetSymbolFontFamily(const char *symbolFnName)
{
fgSymbolFontFamily = symbolFnName; // we need the TString here !!!
}
////////////////////////////////////////////////////////////////////////////////
///
/// Scale the font accroding the inout mgn magnification factor
/// mgn : magnification factor
/// -------
/// see: TVirtualX::DrawText(int x, int y, float angle, float mgn, const char *text, TVirtualX::ETextMode /*mode*/)
///
void TQtPadFont::SetTextMagnify(Float_t mgn)
{
Int_t tsize = (Int_t)(fTextSize+0.5);
if (TMath::Abs(mgn-1) >0.05) {
int pxSize = int(mgn*FontMagicFactor(tsize));
if(pxSize<=0) pxSize=1;
this->setPixelSize(pxSize);
}
}