#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";
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();
qDebug() << " Font metric w = " << w <<" h = "<< h
<< "points=" << pattern.pointSize()
<< "pixels=" << pattern.pixelSize()
<< pattern;
const Float_t kScale = 0.95;
float f;
switch (h) {
case 12: f = 1.10; break;
case 14: f = 0.915; break;
case 16: f = 0.965; break;
case 18: f = 0.92; break;
case 20: f = 0.99; break;
case 21: f = 0.90; break;
default: f = kScale; break;
}
fontCalibFactor = f;
}
}
return fontCalibFactor;
}
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_)
{
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
}
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);
}
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)));
}
}
}
void TQtPadFont::SetTextSizePixels(Int_t npixels)
{
SetTextSize(static_cast<float>(npixels));
}
const char *TQtPadFont::RomanFontName()
{
return fgRomanFontName;
}
const char *TQtPadFont::ArialFontName()
{
return fgArialFontName;
}
const char *TQtPadFont::CourierFontName()
{
return fgCourierFontName;
}
const char *TQtPadFont::SymbolFontFamily()
{
return fgSymbolFontFamily;
}
void TQtPadFont::SetSymbolFontFamily(const char *symbolFnName)
{
fgSymbolFontFamily = symbolFnName;
}
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);
}
}