TAttText.cxx

Go to the documentation of this file.
00001 // @(#)root/base:$Id: TAttText.cxx 34680 2010-07-30 16:03:49Z couet $
00002 // Author: Rene Brun   12/12/94
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #include "Riostream.h"
00013 #include "Strlen.h"
00014 #include "TROOT.h"
00015 #include "TAttText.h"
00016 #include "TVirtualPad.h"
00017 #include "TStyle.h"
00018 #include "TVirtualX.h"
00019 #include "TError.h"
00020 #include "TVirtualPadEditor.h"
00021 #include "TColor.h"
00022 
00023 ClassImp(TAttText)
00024 
00025 
00026 //______________________________________________________________________________
00027 /* Begin_Html
00028 <center><h2>Text Attributes class</h2></center>
00029 
00030 This class is used (in general by secondary inheritance)
00031 by many other classes (graphics, histograms). It holds all the text attributes.
00032 
00033 <h3>Text attributes</h3>
00034 Text attributes are:
00035 <ul>
00036 <li><a href="#T1">Text Alignment.</a></li>
00037 <li><a href="#T2">Text Angle.</a></li>
00038 <li><a href="#T3">Text Color.</a></li>
00039 <li><a href="#T4">Text Size.</a></li>
00040 <li><a href="#T5">Text Font and Precision.</a></li>
00041 <ul>
00042    <li><a href="#T51">Font quality and speed.</a></li>
00043    <li><a href="#T52">How to use True Type Fonts.</a></li>
00044    <li><a href="#T53">List of the currently supported fonts.</a></li>
00045 </ul>
00046 </ul>
00047 
00048 <a name="T1"></a><h3>Text Alignment</h3>
00049 The text alignment is an integer number (<tt>align</tt>) allowing to control
00050 the horizontal and vertical position of the text string with respect
00051 to the text position.
00052 The text alignment of any class inheriting from <tt>TAttText</tt> can
00053 be changed using the method <tt>SetTextAlign</tt> and retrieved using the
00054 method <tt>GetTextAlign</tt>.
00055 <pre>
00056    align = 10*HorizontalAlign + VerticalAlign
00057 </pre>
00058 For horizontal alignment the following convention applies:
00059 <pre>
00060    1=left adjusted, 2=centered, 3=right adjusted
00061 </pre>
00062 For vertical alignment the following convention applies:
00063 <pre>
00064    1=bottom adjusted, 2=centered, 3=top adjusted
00065 </pre>
00066 For example:
00067 <pre>
00068    align = 11 = left adjusted and bottom adjusted
00069    align = 32 = right adjusted and vertically centered
00070 </pre>
00071 End_Html
00072 Begin_Macro(source)
00073 textalign.C
00074 End_Macro
00075 
00076 Begin_Html
00077 <a name="T2"></a><h3>Text Angle</h3>
00078 Text angle in degrees.
00079 The text angle of any class inheriting from <tt>TAttText</tt> can
00080 be changed using the method <tt>SetTextAngle</tt> and retrieved using the
00081 method <tt>GetTextAngle</tt>.
00082 The following picture shows the text angle:
00083 End_Html
00084 Begin_Macro(source)
00085 textangle.C
00086 End_Macro
00087 
00088 Begin_Html
00089 <a name="T3"></a><h3>Text Color</h3>
00090 The text color is a color index (integer) pointing in the ROOT
00091 color table.
00092 The text color of any class inheriting from <tt>TAttText</tt> can
00093 be changed using the method <tt>SetTextColor</tt> and retrieved using the
00094 method <tt>GetTextColor</tt>.
00095 The following table shows the first 50 default colors.
00096 End_Html
00097 Begin_Macro(source)
00098 {
00099    TCanvas *c = new TCanvas("c","Text colors",0,0,500,200);
00100    c.DrawColorTable();
00101    return c;
00102 }
00103 End_Macro
00104 
00105 Begin_Html
00106 <a name="T4"></a><h3>Text Size</h3>
00107 If the text precision (see next paragraph) is smaller than 3, the text
00108 size (<tt>textsize</tt>) is a fraction of the current pad size. Therefore the
00109 same <tt>textsize</tt> value can generate text outputs with different absolute
00110 sizes in two different pads.
00111 The text size in pixels (<tt>charheight</tt>) is computed the following way:
00112 <p>
00113 <pre>
00114    pad_width  = gPad->XtoPixel(gPad->GetX2());
00115    pad_height = gPad->YtoPixel(gPad->GetY1());
00116    if (pad_width < pad_height)  charheight = textsize*pad_width;
00117    else                         charheight = textsize*pad_height;
00118 </pre>
00119 <p>
00120 If the text precision is equal to 3, the text size doesn't depend on the pad's
00121 dimensions. A given <tt>textsize</tt> value always generates the same absolute
00122 size. The text size (<tt>charheight</tt>) is given in pixels:
00123 <pre>
00124    charheight = textsize;
00125 </pre>
00126 <p>
00127 Note that to scale fonts to the same size as the old True Type package a
00128 scale factor of <tt>0.93376068</tt> is apply to the text size before drawing.
00129 <p>
00130 The text size of any class inheriting from <tt>TAttText</tt> can
00131 be changed using the method <tt>SetTextSize</tt> and retrieved using the
00132 method <tt>GetTextSize</tt>.
00133 
00134 
00135 <a name="T5"></a><h3>Text Font and Precision</h3>
00136 The text font code is combination of the font number and the precision.
00137 <pre>
00138    Text font code = 10*fontnumber + precision
00139 </pre>
00140 Font numbers must be between 1 and 14.
00141 <p>
00142 The precision can be:
00143 
00144 <br><tt>precision = 0</tt> fast hardware fonts (steps in the size)
00145 <br><tt>precision = 1</tt> scalable and rotatable hardware fonts (see below)
00146 <br><tt>precision = 2</tt> scalable and rotatable hardware fonts
00147 <br><tt>precision = 3</tt> scalable and rotatable hardware fonts. Text size
00148                            is given in pixels.
00149 <p>
00150 The text font and precision of any class inheriting from <tt>TAttText</tt> can
00151 be changed using the method <tt>SetTextFont</tt> and retrieved using the
00152 method <tt>GetTextFont</tt>.
00153 
00154 <a name="T51"></a><h4>Font quality and speed</h4>
00155 When precision 0 is used, only the original non-scaled system fonts are
00156 used. The fonts have a minimum (4) and maximum (37) size in pixels. These
00157 fonts are fast and are of good quality. Their size varies with large steps
00158 and they cannot be rotated.
00159 Precision 1 and 2 fonts have a different behaviour depending if the
00160 True Type Fonts (TTF) are used or not. If TTF are used, you always get very good
00161 quality scalable and rotatable fonts. However TTF are slow.
00162 
00163 <a name="T52"></a><h4>How to use True Type Fonts</h4>
00164 One can activate the TTF by adding (or activating) the following line
00165 in the <tt>.rootrc</tt> file:
00166 <pre>
00167    Unix.*.Root.UseTTFonts:     true
00168 </pre>
00169 It is possible to check the TTF are in use in a Root session
00170 with the command:
00171 <pre>
00172    gEnv->Print();
00173 </pre>
00174 If the TTF are in use the following line will appear at the beginning of the
00175 printout given by this command:
00176 <pre>
00177    Unix.*.Root.UseTTFonts:   true                           [Global]
00178 </pre>
00179 
00180 
00181 <a name="T53"></a><h4>List of the currently supported fonts</h4>
00182 <pre>
00183    Font number         X11 Names             Win32/TTF Names
00184        1 :       times-medium-i-normal      "Times New Roman"
00185        2 :       times-bold-r-normal        "Times New Roman"
00186        3 :       times-bold-i-normal        "Times New Roman"
00187        4 :       helvetica-medium-r-normal  "Arial"
00188        5 :       helvetica-medium-o-normal  "Arial"
00189        6 :       helvetica-bold-r-normal    "Arial"
00190        7 :       helvetica-bold-o-normal    "Arial"
00191        8 :       courier-medium-r-normal    "Courier New"
00192        9 :       courier-medium-o-normal    "Courier New"
00193       10 :       courier-bold-r-normal      "Courier New"
00194       11 :       courier-bold-o-normal      "Courier New"
00195       12 :       symbol-medium-r-normal     "Symbol"
00196       13 :       times-medium-r-normal      "Times New Roman"
00197       14 :                                  "Wingdings"
00198       15 :       Symbol italic (derived from Symbol)
00199 </pre>
00200 <br>
00201  The following picture shows how each font looks. The number on the left
00202  is the "text font code". In this picture precision 2 was selected.
00203 End_Html
00204 Begin_Macro(source)
00205 fonts.C
00206 End_Macro */
00207 
00208 
00209 //______________________________________________________________________________
00210 TAttText::TAttText()
00211 {
00212    // AttText default constructor.
00213    //
00214    // Default text attributes are taken from the current style.
00215 
00216    if (!gStyle) {
00217       ResetAttText();
00218       return;
00219    }
00220    fTextAlign = gStyle->GetTextAlign();
00221    fTextAngle = gStyle->GetTextAngle();
00222    fTextColor = gStyle->GetTextColor();
00223    fTextFont  = gStyle->GetTextFont();
00224    fTextSize  = gStyle->GetTextSize();
00225 }
00226 
00227 
00228 //______________________________________________________________________________
00229 TAttText::TAttText(Int_t align, Float_t angle, Color_t color, Style_t font, Float_t tsize)
00230 {
00231    // AttText normal constructor.
00232    //
00233    // Text attributes are taken from the argument list.
00234 
00235    fTextAlign = align;
00236    fTextAngle = angle;
00237    fTextColor = color;
00238    fTextFont  = font;
00239    fTextSize  = tsize;
00240 }
00241 
00242 
00243 //______________________________________________________________________________
00244 TAttText::~TAttText()
00245 {
00246    // AttText destructor.
00247 }
00248 
00249 
00250 //______________________________________________________________________________
00251 void TAttText::Copy(TAttText &atttext) const
00252 {
00253    // Copy this text attributes to a new TAttText.
00254 
00255    atttext.fTextAlign  = fTextAlign;
00256    atttext.fTextAngle  = fTextAngle;
00257    atttext.fTextColor  = fTextColor;
00258    atttext.fTextFont   = fTextFont;
00259    atttext.fTextSize   = fTextSize;
00260 }
00261 
00262 
00263 //______________________________________________________________________________
00264 void TAttText::Modify()
00265 {
00266    // Change current text attributes if necessary.
00267 
00268    if (!gPad) return;
00269 
00270    // Do we need to change font?
00271    if (!gPad->IsBatch()) {
00272       gVirtualX->SetTextAngle(fTextAngle);
00273       Float_t wh = (Float_t)gPad->XtoPixel(gPad->GetX2());
00274       Float_t hh = (Float_t)gPad->YtoPixel(gPad->GetY1());
00275       Float_t tsize;
00276       if (wh < hh)  tsize = fTextSize*wh;
00277       else          tsize = fTextSize*hh;
00278       if (fTextFont%10 > 2) tsize = fTextSize;
00279 
00280       if (gVirtualX->GetTextFont() != fTextFont) {
00281             gVirtualX->SetTextFont(fTextFont);
00282             gVirtualX->SetTextSize(tsize);
00283       }
00284       if (gVirtualX->GetTextSize() != tsize)
00285             gVirtualX->SetTextSize(tsize);
00286       gVirtualX->SetTextAlign(fTextAlign);
00287       gVirtualX->SetTextColor(fTextColor);
00288    }
00289    gPad->SetAttTextPS(fTextAlign,fTextAngle,fTextColor,fTextFont,fTextSize);
00290 }
00291 
00292 
00293 //______________________________________________________________________________
00294 void TAttText::ResetAttText(Option_t *)
00295 {
00296    // Reset this text attributes to default values.
00297 
00298    fTextAlign  = 11;
00299    fTextAngle  = 0;
00300    fTextColor  = 1;
00301    fTextFont   = 62;
00302    fTextSize   = 0.05;
00303 }
00304 
00305 
00306 //______________________________________________________________________________
00307 void TAttText::SaveTextAttributes(ostream &out, const char *name, Int_t alidef,
00308                                   Float_t angdef, Int_t coldef, Int_t fondef,
00309                                   Float_t sizdef)
00310 {
00311    // Save text attributes as C++ statement(s) on output stream out.
00312 
00313    if (fTextAlign != alidef) {
00314       out<<"   "<<name<<"->SetTextAlign("<<fTextAlign<<");"<<endl;
00315    }
00316    if (fTextColor != coldef) {
00317       if (fTextColor > 228) {
00318          TColor::SaveColor(out, fTextColor);
00319          out<<"   "<<name<<"->SetTextColor(ci);" << endl;
00320       } else
00321          out<<"   "<<name<<"->SetTextColor("<<fTextColor<<");"<<endl;
00322    }
00323    if (fTextFont != fondef) {
00324       out<<"   "<<name<<"->SetTextFont("<<fTextFont<<");"<<endl;
00325    }
00326    if (fTextSize != sizdef) {
00327       out<<"   "<<name<<"->SetTextSize("<<fTextSize<<");"<<endl;
00328    }
00329    if (fTextAngle != angdef) {
00330       out<<"   "<<name<<"->SetTextAngle("<<fTextAngle<<");"<<endl;
00331    }
00332 }
00333 
00334 
00335 //______________________________________________________________________________
00336 void TAttText::SetTextAttributes()
00337 {
00338    // Invoke the DialogCanvas Text attributes.
00339 
00340    TVirtualPadEditor::UpdateTextAttributes(fTextAlign,fTextAngle,fTextColor,
00341                                            fTextFont,fTextSize);
00342 }
00343 
00344 
00345 //______________________________________________________________________________
00346 void TAttText::SetTextSizePixels(Int_t npixels)
00347 {
00348    // Set the text size in pixels.
00349    // If the font precision is greater than 2, the text size is set to npixels,
00350    // otherwise the text size is computed as a percent of the pad size.
00351 
00352    if (fTextFont%10 > 2) {
00353       fTextSize = Float_t(npixels);
00354    } else {
00355       TVirtualPad *pad = gROOT->GetSelectedPad();
00356       if (!pad) return;
00357       Float_t dy = pad->AbsPixeltoY(0) - pad->AbsPixeltoY(npixels);
00358       fTextSize = dy/(pad->GetY2() - pad->GetY1());
00359    }
00360 }

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