TQtPadFont.cxx

Go to the documentation of this file.
00001 // Author: Valeri Fine   21/01/2002
00002 /****************************************************************************
00003 ** $Id: TQtPadFont.cxx 35889 2010-09-30 09:04:47Z brun $
00004 **
00005 ** Copyright (C) 2002 by Valeri Fine. Brookhaven National Laboratory.
00006 **                                    All rights reserved.
00007 **
00008 ** This file may be distributed under the terms of the Q Public License
00009 ** as defined by Trolltech AS of Norway and appearing in the file
00010 ** LICENSE.QPL included in the packaging of this file.
00011 **
00012 *****************************************************************************/
00013 
00014 
00015 /////////////////////////////////////////////////////////////////////////////////
00016 //
00017 // TQtPadFont class is Qt QFont class with TAttText ROOT class interface
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     // Use the ROOT font with ID=1 to calibrate the current font on fly;
00037     // Environment variable ROOTFONTFACTOR allows to set the factor manually
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 //  X11 returns          h = 12
00055 //  XFT returns          h = 14
00056 //  WIN32 TTF returns    h = 16
00057 //  Nimbus Roman returns h = 18
00058 //  Qt4 XFT              h = 21
00059 
00060           qDebug() << " Font metric w = " << w <<" h = "<< h
00061                    << "points=" << pattern.pointSize()
00062                    << "pixels=" << pattern.pixelSize()
00063                    << pattern;
00064 // graf2d/graf/src/TTF.cxx:const Float_t kScale = 0.93376068
00065           const Float_t kScale = 0.95; //0.93376068;
00066           float f;
00067           switch (h) {
00068              case 12: f = 1.10;  break;// it was  f = 1.13 :-(;
00069              case 14: f = 0.915; break;// it was f = 0.94  :-(;
00070              case 16: f = 0.965; break;// to be tested yet
00071              case 18: f = 0.92;  break;// to be tested yet
00072              case 20: f = 0.99;  break;// to be tested yet
00073              case 21: f = 0.90;  break;// to be tested yet
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    // Adjust the font size to match that for Postscipt format
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    //*-*    mode              : Option message
00100    //*-*    italic   : Italic attribut of the TTF font
00101    //*-*    bold     : Weight attribute of the TTF font
00102    //*-*    fontname : the name of True Type Font (TTF) to draw text.
00103    //*-*
00104    //*-*    Set text font to specified name. This function returns 0 if
00105    //*-*    the specified font is found, 1 if not.
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    //*-*-*-*-*-*-*-*-*-*-*-*-*Set current text font number*-*-*-*-*-*-*-*-*-*-*-*
00138    //*-*                      ===========================
00139    //*-*  List of the currently supported fonts (screen and PostScript)
00140    //*-*  =============================================================
00141    //*-*   Font ID       X11                       Win32 TTF       lfItalic  lfWeight x 10
00142    //*-*        1 : times-medium-i-normal      "Times New Roman"      1           5
00143    //*-*        2 : times-bold-r-normal        "Times New Roman"      0           8
00144    //*-*        3 : times-bold-i-normal        "Times New Roman"      1           8
00145    //*-*        4 : helvetica-medium-r-normal  "Arial"                0           5
00146    //*-*        5 : helvetica-medium-o-normal  "Arial"                1           5
00147    //*-*        6 : helvetica-bold-r-normal    "Arial"                0           8
00148    //*-*        7 : helvetica-bold-o-normal    "Arial"                1           8
00149    //*-*        8 : courier-medium-r-normal    "Courier New"          0           5
00150    //*-*        9 : courier-medium-o-normal    "Courier New"          1           5
00151    //*-*       10 : courier-bold-r-normal      "Courier New"          0           8
00152    //*-*       11 : courier-bold-o-normal      "Courier New"          1           8
00153    //*-*       12 : symbol-medium-r-normal     "Symbol"               0           6
00154    //*-*       13 : times-medium-r-normal      "Times New Roman"      0           5
00155    //*-*       14 :                            "Wingdings"            0           5
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    //*-*-*-*-*-*-*-*-*-*-*-*-*Set current text size*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
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     // Set the text pixel size
00262     SetTextSize(static_cast<float>(npixels));
00263  }
00264 //______________________________________________________________________________
00265 const char *TQtPadFont::RomanFontName() 
00266 { 
00267    // Get the system name for the "Roman" font
00268    return fgRomanFontName;
00269 }
00270 
00271 //______________________________________________________________________________
00272 const char *TQtPadFont::ArialFontName() 
00273 {
00274    // Get the system name for the "Arial" font
00275    return fgArialFontName;
00276 }
00277 
00278 //______________________________________________________________________________
00279 const char *TQtPadFont::CourierFontName()
00280 { 
00281    // Get the system name for the "Courier" font
00282    return fgCourierFontName;
00283 }
00284 //______________________________________________________________________________
00285 const char *TQtPadFont::SymbolFontFamily() 
00286 { 
00287    // Get the system name for the "Symbol" font
00288    return fgSymbolFontFamily;
00289 }
00290 //______________________________________________________________________________
00291 void TQtPadFont::SetSymbolFontFamily(const char *symbolFnName)
00292 { 
00293    // Set the system name for the "Symbol" font
00294    fgSymbolFontFamily = symbolFnName;  // we need the TString here !!!
00295 }
00296 
00297 //______________________________________________________________________________
00298 void   TQtPadFont::SetTextMagnify(Float_t  mgn)
00299 {
00300    //
00301    // Scale the font accroding the inout mgn magnification factor
00302    // mgn        : magnification factor
00303    // -------
00304    // see: TVirtualX::DrawText(int x, int y, float angle, float mgn, const char *text, TVirtualX::ETextMode /*mode*/)
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 }

Generated on Tue Jul 5 14:14:36 2011 for ROOT_528-00b_version by  doxygen 1.5.1