// 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";


//______________________________________________________________________________
static float CalibrateFont()
{
    // 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 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;
}

//______________________________________________________________________________
static inline float FontMagicFactor(float size)
{
   // Adjust the font size to match that for Postscipt format
   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
}

//______________________________________________________________________________
void  TQtPadFont::SetTextFont(Font_t fontnumber)
{
   //*-*-*-*-*-*-*-*-*-*-*-*-*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

   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)
{
   //*-*-*-*-*-*-*-*-*-*-*-*-*Set current text size*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
   //*-*                      =====================
   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)
 {
    // Set the text pixel size
    SetTextSize(static_cast<float>(npixels));
 }
//______________________________________________________________________________
const char *TQtPadFont::RomanFontName()
{
   // Get the system name for the "Roman" font
   return fgRomanFontName;
}

//______________________________________________________________________________
const char *TQtPadFont::ArialFontName()
{
   // Get the system name for the "Arial" font
   return fgArialFontName;
}

//______________________________________________________________________________
const char *TQtPadFont::CourierFontName()
{
   // Get the system name for the "Courier" font
   return fgCourierFontName;
}
//______________________________________________________________________________
const char *TQtPadFont::SymbolFontFamily()
{
   // Get the system name for the "Symbol" font
   return fgSymbolFontFamily;
}
//______________________________________________________________________________
void TQtPadFont::SetSymbolFontFamily(const char *symbolFnName)
{
   // Set the system name for the "Symbol" font
   fgSymbolFontFamily = symbolFnName;  // we need the TString here !!!
}

//______________________________________________________________________________
void   TQtPadFont::SetTextMagnify(Float_t  mgn)
{
   //
   // 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*/)
   //
    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);
    }
}
 TQtPadFont.cxx:1
 TQtPadFont.cxx:2
 TQtPadFont.cxx:3
 TQtPadFont.cxx:4
 TQtPadFont.cxx:5
 TQtPadFont.cxx:6
 TQtPadFont.cxx:7
 TQtPadFont.cxx:8
 TQtPadFont.cxx:9
 TQtPadFont.cxx:10
 TQtPadFont.cxx:11
 TQtPadFont.cxx:12
 TQtPadFont.cxx:13
 TQtPadFont.cxx:14
 TQtPadFont.cxx:15
 TQtPadFont.cxx:16
 TQtPadFont.cxx:17
 TQtPadFont.cxx:18
 TQtPadFont.cxx:19
 TQtPadFont.cxx:20
 TQtPadFont.cxx:21
 TQtPadFont.cxx:22
 TQtPadFont.cxx:23
 TQtPadFont.cxx:24
 TQtPadFont.cxx:25
 TQtPadFont.cxx:26
 TQtPadFont.cxx:27
 TQtPadFont.cxx:28
 TQtPadFont.cxx:29
 TQtPadFont.cxx:30
 TQtPadFont.cxx:31
 TQtPadFont.cxx:32
 TQtPadFont.cxx:33
 TQtPadFont.cxx:34
 TQtPadFont.cxx:35
 TQtPadFont.cxx:36
 TQtPadFont.cxx:37
 TQtPadFont.cxx:38
 TQtPadFont.cxx:39
 TQtPadFont.cxx:40
 TQtPadFont.cxx:41
 TQtPadFont.cxx:42
 TQtPadFont.cxx:43
 TQtPadFont.cxx:44
 TQtPadFont.cxx:45
 TQtPadFont.cxx:46
 TQtPadFont.cxx:47
 TQtPadFont.cxx:48
 TQtPadFont.cxx:49
 TQtPadFont.cxx:50
 TQtPadFont.cxx:51
 TQtPadFont.cxx:52
 TQtPadFont.cxx:53
 TQtPadFont.cxx:54
 TQtPadFont.cxx:55
 TQtPadFont.cxx:56
 TQtPadFont.cxx:57
 TQtPadFont.cxx:58
 TQtPadFont.cxx:59
 TQtPadFont.cxx:60
 TQtPadFont.cxx:61
 TQtPadFont.cxx:62
 TQtPadFont.cxx:63
 TQtPadFont.cxx:64
 TQtPadFont.cxx:65
 TQtPadFont.cxx:66
 TQtPadFont.cxx:67
 TQtPadFont.cxx:68
 TQtPadFont.cxx:69
 TQtPadFont.cxx:70
 TQtPadFont.cxx:71
 TQtPadFont.cxx:72
 TQtPadFont.cxx:73
 TQtPadFont.cxx:74
 TQtPadFont.cxx:75
 TQtPadFont.cxx:76
 TQtPadFont.cxx:77
 TQtPadFont.cxx:78
 TQtPadFont.cxx:79
 TQtPadFont.cxx:80
 TQtPadFont.cxx:81
 TQtPadFont.cxx:82
 TQtPadFont.cxx:83
 TQtPadFont.cxx:84
 TQtPadFont.cxx:85
 TQtPadFont.cxx:86
 TQtPadFont.cxx:87
 TQtPadFont.cxx:88
 TQtPadFont.cxx:89
 TQtPadFont.cxx:90
 TQtPadFont.cxx:91
 TQtPadFont.cxx:92
 TQtPadFont.cxx:93
 TQtPadFont.cxx:94
 TQtPadFont.cxx:95
 TQtPadFont.cxx:96
 TQtPadFont.cxx:97
 TQtPadFont.cxx:98
 TQtPadFont.cxx:99
 TQtPadFont.cxx:100
 TQtPadFont.cxx:101
 TQtPadFont.cxx:102
 TQtPadFont.cxx:103
 TQtPadFont.cxx:104
 TQtPadFont.cxx:105
 TQtPadFont.cxx:106
 TQtPadFont.cxx:107
 TQtPadFont.cxx:108
 TQtPadFont.cxx:109
 TQtPadFont.cxx:110
 TQtPadFont.cxx:111
 TQtPadFont.cxx:112
 TQtPadFont.cxx:113
 TQtPadFont.cxx:114
 TQtPadFont.cxx:115
 TQtPadFont.cxx:116
 TQtPadFont.cxx:117
 TQtPadFont.cxx:118
 TQtPadFont.cxx:119
 TQtPadFont.cxx:120
 TQtPadFont.cxx:121
 TQtPadFont.cxx:122
 TQtPadFont.cxx:123
 TQtPadFont.cxx:124
 TQtPadFont.cxx:125
 TQtPadFont.cxx:126
 TQtPadFont.cxx:127
 TQtPadFont.cxx:128
 TQtPadFont.cxx:129
 TQtPadFont.cxx:130
 TQtPadFont.cxx:131
 TQtPadFont.cxx:132
 TQtPadFont.cxx:133
 TQtPadFont.cxx:134
 TQtPadFont.cxx:135
 TQtPadFont.cxx:136
 TQtPadFont.cxx:137
 TQtPadFont.cxx:138
 TQtPadFont.cxx:139
 TQtPadFont.cxx:140
 TQtPadFont.cxx:141
 TQtPadFont.cxx:142
 TQtPadFont.cxx:143
 TQtPadFont.cxx:144
 TQtPadFont.cxx:145
 TQtPadFont.cxx:146
 TQtPadFont.cxx:147
 TQtPadFont.cxx:148
 TQtPadFont.cxx:149
 TQtPadFont.cxx:150
 TQtPadFont.cxx:151
 TQtPadFont.cxx:152
 TQtPadFont.cxx:153
 TQtPadFont.cxx:154
 TQtPadFont.cxx:155
 TQtPadFont.cxx:156
 TQtPadFont.cxx:157
 TQtPadFont.cxx:158
 TQtPadFont.cxx:159
 TQtPadFont.cxx:160
 TQtPadFont.cxx:161
 TQtPadFont.cxx:162
 TQtPadFont.cxx:163
 TQtPadFont.cxx:164
 TQtPadFont.cxx:165
 TQtPadFont.cxx:166
 TQtPadFont.cxx:167
 TQtPadFont.cxx:168
 TQtPadFont.cxx:169
 TQtPadFont.cxx:170
 TQtPadFont.cxx:171
 TQtPadFont.cxx:172
 TQtPadFont.cxx:173
 TQtPadFont.cxx:174
 TQtPadFont.cxx:175
 TQtPadFont.cxx:176
 TQtPadFont.cxx:177
 TQtPadFont.cxx:178
 TQtPadFont.cxx:179
 TQtPadFont.cxx:180
 TQtPadFont.cxx:181
 TQtPadFont.cxx:182
 TQtPadFont.cxx:183
 TQtPadFont.cxx:184
 TQtPadFont.cxx:185
 TQtPadFont.cxx:186
 TQtPadFont.cxx:187
 TQtPadFont.cxx:188
 TQtPadFont.cxx:189
 TQtPadFont.cxx:190
 TQtPadFont.cxx:191
 TQtPadFont.cxx:192
 TQtPadFont.cxx:193
 TQtPadFont.cxx:194
 TQtPadFont.cxx:195
 TQtPadFont.cxx:196
 TQtPadFont.cxx:197
 TQtPadFont.cxx:198
 TQtPadFont.cxx:199
 TQtPadFont.cxx:200
 TQtPadFont.cxx:201
 TQtPadFont.cxx:202
 TQtPadFont.cxx:203
 TQtPadFont.cxx:204
 TQtPadFont.cxx:205
 TQtPadFont.cxx:206
 TQtPadFont.cxx:207
 TQtPadFont.cxx:208
 TQtPadFont.cxx:209
 TQtPadFont.cxx:210
 TQtPadFont.cxx:211
 TQtPadFont.cxx:212
 TQtPadFont.cxx:213
 TQtPadFont.cxx:214
 TQtPadFont.cxx:215
 TQtPadFont.cxx:216
 TQtPadFont.cxx:217
 TQtPadFont.cxx:218
 TQtPadFont.cxx:219
 TQtPadFont.cxx:220
 TQtPadFont.cxx:221
 TQtPadFont.cxx:222
 TQtPadFont.cxx:223
 TQtPadFont.cxx:224
 TQtPadFont.cxx:225
 TQtPadFont.cxx:226
 TQtPadFont.cxx:227
 TQtPadFont.cxx:228
 TQtPadFont.cxx:229
 TQtPadFont.cxx:230
 TQtPadFont.cxx:231
 TQtPadFont.cxx:232
 TQtPadFont.cxx:233
 TQtPadFont.cxx:234
 TQtPadFont.cxx:235
 TQtPadFont.cxx:236
 TQtPadFont.cxx:237
 TQtPadFont.cxx:238
 TQtPadFont.cxx:239
 TQtPadFont.cxx:240
 TQtPadFont.cxx:241
 TQtPadFont.cxx:242
 TQtPadFont.cxx:243
 TQtPadFont.cxx:244
 TQtPadFont.cxx:245
 TQtPadFont.cxx:246
 TQtPadFont.cxx:247
 TQtPadFont.cxx:248
 TQtPadFont.cxx:249
 TQtPadFont.cxx:250
 TQtPadFont.cxx:251
 TQtPadFont.cxx:252
 TQtPadFont.cxx:253
 TQtPadFont.cxx:254
 TQtPadFont.cxx:255
 TQtPadFont.cxx:256
 TQtPadFont.cxx:257
 TQtPadFont.cxx:258
 TQtPadFont.cxx:259
 TQtPadFont.cxx:260
 TQtPadFont.cxx:261
 TQtPadFont.cxx:262
 TQtPadFont.cxx:263
 TQtPadFont.cxx:264
 TQtPadFont.cxx:265
 TQtPadFont.cxx:266
 TQtPadFont.cxx:267
 TQtPadFont.cxx:268
 TQtPadFont.cxx:269
 TQtPadFont.cxx:270
 TQtPadFont.cxx:271
 TQtPadFont.cxx:272
 TQtPadFont.cxx:273
 TQtPadFont.cxx:274
 TQtPadFont.cxx:275
 TQtPadFont.cxx:276
 TQtPadFont.cxx:277
 TQtPadFont.cxx:278
 TQtPadFont.cxx:279
 TQtPadFont.cxx:280
 TQtPadFont.cxx:281
 TQtPadFont.cxx:282
 TQtPadFont.cxx:283
 TQtPadFont.cxx:284
 TQtPadFont.cxx:285
 TQtPadFont.cxx:286
 TQtPadFont.cxx:287
 TQtPadFont.cxx:288
 TQtPadFont.cxx:289
 TQtPadFont.cxx:290
 TQtPadFont.cxx:291
 TQtPadFont.cxx:292
 TQtPadFont.cxx:293
 TQtPadFont.cxx:294
 TQtPadFont.cxx:295
 TQtPadFont.cxx:296
 TQtPadFont.cxx:297
 TQtPadFont.cxx:298
 TQtPadFont.cxx:299
 TQtPadFont.cxx:300
 TQtPadFont.cxx:301
 TQtPadFont.cxx:302
 TQtPadFont.cxx:303
 TQtPadFont.cxx:304
 TQtPadFont.cxx:305
 TQtPadFont.cxx:306
 TQtPadFont.cxx:307
 TQtPadFont.cxx:308
 TQtPadFont.cxx:309