00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "TQtPadFont.h"
00022 #include "TSystem.h"
00023 #include "TMath.h"
00024 #include <QFontMetrics>
00025 #include <QDebug>
00026
00027 TString TQtPadFont::fgRomanFontName = "Times New Roman";
00028 TString TQtPadFont::fgArialFontName = "Arial";
00029 TString TQtPadFont::fgCourierFontName = "Courier New";
00030 TString TQtPadFont::fgSymbolFontFamily= "Symbol";
00031
00032
00033
00034 static float CalibrateFont()
00035 {
00036
00037
00038 static float fontCalibFactor = -1;
00039 if (fontCalibFactor < 0 ) {
00040
00041 const char * envFactor = gSystem->Getenv("ROOTFONTFACTOR");
00042 bool ok=false;
00043 if (envFactor && envFactor[0])
00044 fontCalibFactor= QString(envFactor).toFloat(&ok);
00045 if (!ok) {
00046 TQtPadFont pattern;
00047 pattern.SetTextFont(62);
00048
00049 int w,h;
00050 QFontMetrics metrics(pattern);
00051 w = metrics.width("This is a PX distribution");
00052 h = metrics.height();
00053
00054
00055
00056
00057
00058
00059
00060 qDebug() << " Font metric w = " << w <<" h = "<< h
00061 << "points=" << pattern.pointSize()
00062 << "pixels=" << pattern.pixelSize()
00063 << pattern;
00064
00065 const Float_t kScale = 0.95;
00066 float f;
00067 switch (h) {
00068 case 12: f = 1.10; break;
00069 case 14: f = 0.915; break;
00070 case 16: f = 0.965; break;
00071 case 18: f = 0.92; break;
00072 case 20: f = 0.99; break;
00073 case 21: f = 0.90; break;
00074 default: f = kScale; break;
00075 }
00076 fontCalibFactor = f;
00077 }
00078 }
00079 return fontCalibFactor;
00080 }
00081
00082
00083 static inline float FontMagicFactor(float size)
00084 {
00085
00086 static float calibration =0;
00087 if (calibration == 0) calibration = CalibrateFont();
00088 return TMath::Max(calibration*size,Float_t(1.0));
00089 }
00090
00091
00092 TQtPadFont::TQtPadFont(): TAttText()
00093 {fTextFont = -1;fTextSize = -1; }
00094
00095
00096 void TQtPadFont::SetTextFont(const char *fontname, int italic_, int bold_)
00097 {
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 this->setWeight((long) bold_*10);
00108 this->setItalic((Bool_t)italic_);
00109 this->setFamily(fontname);
00110
00111 if (!strcmp(fontname,RomanFontName())) {
00112 this->setStyleHint(QFont::Serif);
00113 } else if (!strcmp(fontname,ArialFontName())) {
00114 this->setStyleHint(QFont::SansSerif);
00115 } else if (!strcmp(fontname,CourierFontName())){
00116 this->setStyleHint(QFont::TypeWriter);
00117 }
00118 this->setStyleStrategy(QFont::PreferDevice);
00119 #if 0
00120 qDebug() << "TQtPadFont::SetTextFont:" << fontname
00121 << this->lastResortFamily ()
00122 << this->lastResortFont ()
00123 << this->substitute (fontname)
00124 << "ROOT font number=" << fTextFont;
00125 #endif
00126
00127 #if 0
00128 qDebug() << "TGQt::SetTextFont font:" << fontname
00129 << " bold=" << bold_
00130 << " italic="<< italic_;
00131 #endif
00132 }
00133
00134
00135 void TQtPadFont::SetTextFont(Font_t fontnumber)
00136 {
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 if ( (fTextFont == fontnumber) || (fontnumber <0) ) return;
00158 TAttText::SetTextFont(fontnumber);
00159
00160 int it, bld;
00161 const char *fontName;
00162
00163 switch(fTextFont/10) {
00164
00165 case 1:
00166 it = 1;
00167 bld = 5;
00168 fontName = RomanFontName();
00169 break;
00170 case 2:
00171 it = 0;
00172 bld = 8;
00173 fontName = RomanFontName();
00174 break;
00175 case 3:
00176 it = 1;
00177 bld = 8;
00178 fontName = RomanFontName();
00179 break;
00180 case 4:
00181 it = 0;
00182 bld = 5;
00183 fontName = ArialFontName();
00184 break;
00185 case 5:
00186 it = 1;
00187 bld = 5;
00188 fontName = ArialFontName();
00189 break;
00190 case 6:
00191 it = 0;
00192 bld = 8;
00193 fontName = ArialFontName();
00194 break;
00195 case 7:
00196 it = 1;
00197 bld = 8;
00198 fontName = ArialFontName();
00199 break;
00200 case 8:
00201 it = 0;
00202 bld = 5;
00203 fontName = CourierFontName();
00204 break;
00205 case 9:
00206 it = 1;
00207 bld = 5;
00208 fontName = CourierFontName();
00209 break;
00210 case 10:
00211 it = 0;
00212 bld = 8;
00213 fontName = CourierFontName();
00214 break;
00215 case 11:
00216 it = 1;
00217 bld = 8;
00218 fontName = CourierFontName();
00219 break;
00220 case 12:
00221 it = 0;
00222 bld = 5;
00223 fontName = SymbolFontFamily();
00224 break;
00225 case 13:
00226 it = 0;
00227 bld = 5;
00228 fontName = RomanFontName();
00229 break;
00230 case 14:
00231 it = 0;
00232 bld = 5;
00233 fontName = "Wingdings";
00234 break;
00235 default:
00236 it = 0;
00237 bld = 5;
00238 fontName = RomanFontName();
00239 break;
00240
00241 }
00242 SetTextFont(fontName, it, bld);
00243 }
00244
00245
00246 void TQtPadFont::SetTextSize(Float_t textsize)
00247 {
00248
00249
00250 if ( fTextSize != textsize ) {
00251 TAttText::SetTextSize(textsize);
00252 if (fTextSize > 0) {
00253 Int_t tsize =(Int_t)( textsize+0.5);
00254 this->setPixelSize(static_cast<int>(FontMagicFactor(tsize)));
00255 }
00256 }
00257 }
00258
00259 void TQtPadFont::SetTextSizePixels(Int_t npixels)
00260 {
00261
00262 SetTextSize(static_cast<float>(npixels));
00263 }
00264
00265 const char *TQtPadFont::RomanFontName()
00266 {
00267
00268 return fgRomanFontName;
00269 }
00270
00271
00272 const char *TQtPadFont::ArialFontName()
00273 {
00274
00275 return fgArialFontName;
00276 }
00277
00278
00279 const char *TQtPadFont::CourierFontName()
00280 {
00281
00282 return fgCourierFontName;
00283 }
00284
00285 const char *TQtPadFont::SymbolFontFamily()
00286 {
00287
00288 return fgSymbolFontFamily;
00289 }
00290
00291 void TQtPadFont::SetSymbolFontFamily(const char *symbolFnName)
00292 {
00293
00294 fgSymbolFontFamily = symbolFnName;
00295 }
00296
00297
00298 void TQtPadFont::SetTextMagnify(Float_t mgn)
00299 {
00300
00301
00302
00303
00304
00305
00306 Int_t tsize = (Int_t)(fTextSize+0.5);
00307 if (TMath::Abs(mgn-1) >0.05) {
00308 int pxSize = int(mgn*FontMagicFactor(tsize));
00309 if(pxSize<=0) pxSize=1;
00310 this->setPixelSize(pxSize);
00311 }
00312 }