ROOT logo
// @(#)root/base:$Id: TAttText.cxx 34680 2010-07-30 16:03:49Z couet $
// Author: Rene Brun   12/12/94

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "Riostream.h"
#include "Strlen.h"
#include "TROOT.h"
#include "TAttText.h"
#include "TVirtualPad.h"
#include "TStyle.h"
#include "TVirtualX.h"
#include "TError.h"
#include "TVirtualPadEditor.h"
#include "TColor.h"

ClassImp(TAttText)


//______________________________________________________________________________
/* Begin_Html
<center><h2>Text Attributes class</h2></center>

This class is used (in general by secondary inheritance)
by many other classes (graphics, histograms). It holds all the text attributes.

<h3>Text attributes</h3>
Text attributes are:
<ul>
<li><a href="#T1">Text Alignment.</a></li>
<li><a href="#T2">Text Angle.</a></li>
<li><a href="#T3">Text Color.</a></li>
<li><a href="#T4">Text Size.</a></li>
<li><a href="#T5">Text Font and Precision.</a></li>
<ul>
   <li><a href="#T51">Font quality and speed.</a></li>
   <li><a href="#T52">How to use True Type Fonts.</a></li>
   <li><a href="#T53">List of the currently supported fonts.</a></li>
</ul>
</ul>

<a name="T1"></a><h3>Text Alignment</h3>
The text alignment is an integer number (<tt>align</tt>) allowing to control
the horizontal and vertical position of the text string with respect
to the text position.
The text alignment of any class inheriting from <tt>TAttText</tt> can
be changed using the method <tt>SetTextAlign</tt> and retrieved using the
method <tt>GetTextAlign</tt>.
<pre>
   align = 10*HorizontalAlign + VerticalAlign
</pre>
For horizontal alignment the following convention applies:
<pre>
   1=left adjusted, 2=centered, 3=right adjusted
</pre>
For vertical alignment the following convention applies:
<pre>
   1=bottom adjusted, 2=centered, 3=top adjusted
</pre>
For example:
<pre>
   align = 11 = left adjusted and bottom adjusted
   align = 32 = right adjusted and vertically centered
</pre>
End_Html
Begin_Macro(source)
textalign.C
End_Macro

Begin_Html
<a name="T2"></a><h3>Text Angle</h3>
Text angle in degrees.
The text angle of any class inheriting from <tt>TAttText</tt> can
be changed using the method <tt>SetTextAngle</tt> and retrieved using the
method <tt>GetTextAngle</tt>.
The following picture shows the text angle:
End_Html
Begin_Macro(source)
textangle.C
End_Macro

Begin_Html
<a name="T3"></a><h3>Text Color</h3>
The text color is a color index (integer) pointing in the ROOT
color table.
The text color of any class inheriting from <tt>TAttText</tt> can
be changed using the method <tt>SetTextColor</tt> and retrieved using the
method <tt>GetTextColor</tt>.
The following table shows the first 50 default colors.
End_Html
Begin_Macro(source)
{
   TCanvas *c = new TCanvas("c","Text colors",0,0,500,200);
   c.DrawColorTable();
   return c;
}
End_Macro

Begin_Html
<a name="T4"></a><h3>Text Size</h3>
If the text precision (see next paragraph) is smaller than 3, the text
size (<tt>textsize</tt>) is a fraction of the current pad size. Therefore the
same <tt>textsize</tt> value can generate text outputs with different absolute
sizes in two different pads.
The text size in pixels (<tt>charheight</tt>) is computed the following way:
<p>
<pre>
   pad_width  = gPad->XtoPixel(gPad->GetX2());
   pad_height = gPad->YtoPixel(gPad->GetY1());
   if (pad_width < pad_height)  charheight = textsize*pad_width;
   else                         charheight = textsize*pad_height;
</pre>
<p>
If the text precision is equal to 3, the text size doesn't depend on the pad's
dimensions. A given <tt>textsize</tt> value always generates the same absolute
size. The text size (<tt>charheight</tt>) is given in pixels:
<pre>
   charheight = textsize;
</pre>
<p>
Note that to scale fonts to the same size as the old True Type package a
scale factor of <tt>0.93376068</tt> is apply to the text size before drawing.
<p>
The text size of any class inheriting from <tt>TAttText</tt> can
be changed using the method <tt>SetTextSize</tt> and retrieved using the
method <tt>GetTextSize</tt>.


<a name="T5"></a><h3>Text Font and Precision</h3>
The text font code is combination of the font number and the precision.
<pre>
   Text font code = 10*fontnumber + precision
</pre>
Font numbers must be between 1 and 14.
<p>
The precision can be:

<br><tt>precision = 0</tt> fast hardware fonts (steps in the size)
<br><tt>precision = 1</tt> scalable and rotatable hardware fonts (see below)
<br><tt>precision = 2</tt> scalable and rotatable hardware fonts
<br><tt>precision = 3</tt> scalable and rotatable hardware fonts. Text size
                           is given in pixels.
<p>
The text font and precision of any class inheriting from <tt>TAttText</tt> can
be changed using the method <tt>SetTextFont</tt> and retrieved using the
method <tt>GetTextFont</tt>.

<a name="T51"></a><h4>Font quality and speed</h4>
When precision 0 is used, only the original non-scaled system fonts are
used. The fonts have a minimum (4) and maximum (37) size in pixels. These
fonts are fast and are of good quality. Their size varies with large steps
and they cannot be rotated.
Precision 1 and 2 fonts have a different behaviour depending if the
True Type Fonts (TTF) are used or not. If TTF are used, you always get very good
quality scalable and rotatable fonts. However TTF are slow.

<a name="T52"></a><h4>How to use True Type Fonts</h4>
One can activate the TTF by adding (or activating) the following line
in the <tt>.rootrc</tt> file:
<pre>
   Unix.*.Root.UseTTFonts:     true
</pre>
It is possible to check the TTF are in use in a Root session
with the command:
<pre>
   gEnv->Print();
</pre>
If the TTF are in use the following line will appear at the beginning of the
printout given by this command:
<pre>
   Unix.*.Root.UseTTFonts:   true                           [Global]
</pre>


<a name="T53"></a><h4>List of the currently supported fonts</h4>
<pre>
   Font number         X11 Names             Win32/TTF Names
       1 :       times-medium-i-normal      "Times New Roman"
       2 :       times-bold-r-normal        "Times New Roman"
       3 :       times-bold-i-normal        "Times New Roman"
       4 :       helvetica-medium-r-normal  "Arial"
       5 :       helvetica-medium-o-normal  "Arial"
       6 :       helvetica-bold-r-normal    "Arial"
       7 :       helvetica-bold-o-normal    "Arial"
       8 :       courier-medium-r-normal    "Courier New"
       9 :       courier-medium-o-normal    "Courier New"
      10 :       courier-bold-r-normal      "Courier New"
      11 :       courier-bold-o-normal      "Courier New"
      12 :       symbol-medium-r-normal     "Symbol"
      13 :       times-medium-r-normal      "Times New Roman"
      14 :                                  "Wingdings"
      15 :       Symbol italic (derived from Symbol)
</pre>
<br>
 The following picture shows how each font looks. The number on the left
 is the "text font code". In this picture precision 2 was selected.
End_Html
Begin_Macro(source)
fonts.C
End_Macro */


//______________________________________________________________________________
TAttText::TAttText()
{
   // AttText default constructor.
   //
   // Default text attributes are taken from the current style.

   if (!gStyle) {
      ResetAttText();
      return;
   }
   fTextAlign = gStyle->GetTextAlign();
   fTextAngle = gStyle->GetTextAngle();
   fTextColor = gStyle->GetTextColor();
   fTextFont  = gStyle->GetTextFont();
   fTextSize  = gStyle->GetTextSize();
}


//______________________________________________________________________________
TAttText::TAttText(Int_t align, Float_t angle, Color_t color, Style_t font, Float_t tsize)
{
   // AttText normal constructor.
   //
   // Text attributes are taken from the argument list.

   fTextAlign = align;
   fTextAngle = angle;
   fTextColor = color;
   fTextFont  = font;
   fTextSize  = tsize;
}


//______________________________________________________________________________
TAttText::~TAttText()
{
   // AttText destructor.
}


//______________________________________________________________________________
void TAttText::Copy(TAttText &atttext) const
{
   // Copy this text attributes to a new TAttText.

   atttext.fTextAlign  = fTextAlign;
   atttext.fTextAngle  = fTextAngle;
   atttext.fTextColor  = fTextColor;
   atttext.fTextFont   = fTextFont;
   atttext.fTextSize   = fTextSize;
}


//______________________________________________________________________________
void TAttText::Modify()
{
   // Change current text attributes if necessary.

   if (!gPad) return;

   // Do we need to change font?
   if (!gPad->IsBatch()) {
      gVirtualX->SetTextAngle(fTextAngle);
      Float_t wh = (Float_t)gPad->XtoPixel(gPad->GetX2());
      Float_t hh = (Float_t)gPad->YtoPixel(gPad->GetY1());
      Float_t tsize;
      if (wh < hh)  tsize = fTextSize*wh;
      else          tsize = fTextSize*hh;
      if (fTextFont%10 > 2) tsize = fTextSize;

      if (gVirtualX->GetTextFont() != fTextFont) {
            gVirtualX->SetTextFont(fTextFont);
            gVirtualX->SetTextSize(tsize);
      }
      if (gVirtualX->GetTextSize() != tsize)
            gVirtualX->SetTextSize(tsize);
      gVirtualX->SetTextAlign(fTextAlign);
      gVirtualX->SetTextColor(fTextColor);
   }
   gPad->SetAttTextPS(fTextAlign,fTextAngle,fTextColor,fTextFont,fTextSize);
}


//______________________________________________________________________________
void TAttText::ResetAttText(Option_t *)
{
   // Reset this text attributes to default values.

   fTextAlign  = 11;
   fTextAngle  = 0;
   fTextColor  = 1;
   fTextFont   = 62;
   fTextSize   = 0.05;
}


//______________________________________________________________________________
void TAttText::SaveTextAttributes(ostream &out, const char *name, Int_t alidef,
                                  Float_t angdef, Int_t coldef, Int_t fondef,
                                  Float_t sizdef)
{
   // Save text attributes as C++ statement(s) on output stream out.

   if (fTextAlign != alidef) {
      out<<"   "<<name<<"->SetTextAlign("<<fTextAlign<<");"<<endl;
   }
   if (fTextColor != coldef) {
      if (fTextColor > 228) {
         TColor::SaveColor(out, fTextColor);
         out<<"   "<<name<<"->SetTextColor(ci);" << endl;
      } else
         out<<"   "<<name<<"->SetTextColor("<<fTextColor<<");"<<endl;
   }
   if (fTextFont != fondef) {
      out<<"   "<<name<<"->SetTextFont("<<fTextFont<<");"<<endl;
   }
   if (fTextSize != sizdef) {
      out<<"   "<<name<<"->SetTextSize("<<fTextSize<<");"<<endl;
   }
   if (fTextAngle != angdef) {
      out<<"   "<<name<<"->SetTextAngle("<<fTextAngle<<");"<<endl;
   }
}


//______________________________________________________________________________
void TAttText::SetTextAttributes()
{
   // Invoke the DialogCanvas Text attributes.

   TVirtualPadEditor::UpdateTextAttributes(fTextAlign,fTextAngle,fTextColor,
                                           fTextFont,fTextSize);
}


//______________________________________________________________________________
void TAttText::SetTextSizePixels(Int_t npixels)
{
   // Set the text size in pixels.
   // If the font precision is greater than 2, the text size is set to npixels,
   // otherwise the text size is computed as a percent of the pad size.

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