Logo ROOT  
Reference Guide
TAttText.cxx
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Rene Brun 12/12/94
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include <iostream>
13 #include "Strlen.h"
14 #include "TROOT.h"
15 #include "TAttText.h"
16 #include "TVirtualPad.h"
17 #include "TStyle.h"
18 #include "TVirtualX.h"
19 #include "TError.h"
20 #include "TVirtualPadEditor.h"
21 #include "TColor.h"
22 
24 
25 /** \class TAttText
26 \ingroup Base
27 \ingroup GraphicsAtt
28 
29 Text Attributes class.
30 
31 This class is used (in general by secondary inheritance)
32 by many other classes (graphics, histograms). It holds all the text attributes.
33 
34 ## Text attributes
35 Text attributes are:
36 
37  - [Text Alignment](\ref T1)
38  - [Text Angle](\ref T2)
39  - [Text Color](\ref T3)
40  - [Text Size](\ref T4)
41  - [Text Font and Precision](\ref T5)
42  - [Font quality and speed](\ref T51)
43  - [How to use True Type Fonts](\ref T52)
44  - [List of the currently supported fonts](\ref T53)
45 
46 \anchor T1
47 ## Text Alignment
48 
49 The text alignment is an integer number (`align`) allowing to control
50 the horizontal and vertical position of the text string with respect
51 to the text position.
52 The text alignment of any class inheriting from `TAttText` can
53 be changed using the method `SetTextAlign` and retrieved using the
54 method `GetTextAlign`.
55 
56 ~~~ {.cpp}
57  align = 10*HorizontalAlign + VerticalAlign
58 ~~~
59 
60 For horizontal alignment the following convention applies:
61 
62 ~~~ {.cpp}
63  1=left adjusted, 2=centered, 3=right adjusted
64 ~~~
65 
66 For vertical alignment the following convention applies:
67 
68 ~~~ {.cpp}
69  1=bottom adjusted, 2=centered, 3=top adjusted
70 ~~~
71 
72 For example:
73 
74 ~~~ {.cpp}
75  align = 11 = left adjusted and bottom adjusted
76  align = 32 = right adjusted and vertically centered
77 ~~~
78 
79 Begin_Macro(source)
80 textalign.C
81 End_Macro
82 
83 Mnemonic constants are available:
84 
85 ~~~ {.cpp}
86 kHAlignLeft = 10, kHAlignCenter = 20, kHAlignRight = 30,
87 kVAlignBottom = 1, kVAlignCenter = 2, kVAlignTop = 3
88 ~~~
89 
90 They allow to write:
91 
92 ~~~ {.cpp}
93 object->SetTextAlign(kHAlignLeft+kVAlignTop);
94 ~~~
95 
96 \anchor T2
97 ## Text Angle
98 
99 Text angle in degrees.
100 The text angle of any class inheriting from `TAttText` can
101 be changed using the method `SetTextAngle` and retrieved using the
102 method `GetTextAngle`.
103 The following picture shows the text angle:
104 
105 Begin_Macro(source)
106 textangle.C
107 End_Macro
108 
109 \anchor T3
110 ## Text Color
111 
112 The text color is a color index (integer) pointing in the ROOT
113 color table.
114 The text color of any class inheriting from `TAttText` can
115 be changed using the method `SetTextColor` and retrieved using the
116 method `GetTextColor`.
117 The following table shows the first 50 default colors.
118 
119 Begin_Macro
120 {
121  TCanvas *c = new TCanvas("c","Fill Area colors",0,0,500,200);
122  c->DrawColorTable();
123  return c;
124 }
125 End_Macro
126 
127 ### Color transparency
128 `SetTextColorAlpha()`, allows to set a transparent color.
129 In the following example the text color of the text `text`
130 is set to blue with a transparency of 35%. The color `kBlue`
131 itself remains fully opaque.
132 
133 ~~~ {.cpp}
134 text->SetTextColorAlpha(kBlue, 0.35);
135 ~~~
136 
137 The transparency is available on all platforms when the flag `OpenGL.CanvasPreferGL` is set to `1`
138 in `$ROOTSYS/etc/system.rootrc`, or on Mac with the Cocoa backend. On the file output
139 it is visible with PDF, PNG, Gif, JPEG, SVG, TeX ... but not PostScript.
140 
141 \anchor T4
142 ## Text Size
143 
144 If the text precision (see next paragraph) is smaller than 3, the text
145 size (`textsize`) is a fraction of the current pad size. Therefore the
146 same `textsize` value can generate text outputs with different absolute
147 sizes in two different pads.
148 The text size in pixels (`charheight`) is computed the following way:
149 
150 ~~~ {.cpp}
151  pad_width = gPad->XtoPixel(gPad->GetX2());
152  pad_height = gPad->YtoPixel(gPad->GetY1());
153  if (pad_width < pad_height) charheight = textsize*pad_width;
154  else charheight = textsize*pad_height;
155 ~~~
156 
157 If the text precision is equal to 3, the text size doesn't depend on the pad's
158 dimensions. A given `textsize` value always generates the same absolute
159 size. The text size (`charheight`) is given in pixels:
160 
161 ~~~ {.cpp}
162  charheight = textsize;
163 ~~~
164 
165 Note that to scale fonts to the same size as the old True Type package a
166 scale factor of `0.93376068` is apply to the text size before drawing.
167 
168 The text size of any class inheriting from `TAttText` can
169 be changed using the method `SetTextSize` and retrieved using the
170 method `GetTextSize`.
171 
172 \anchor T5
173 ## Text Font and Precision
174 
175 The text font code is combination of the font number and the precision.
176 ~~~ {.cpp}
177  Text font code = 10*fontnumber + precision
178 ~~~
179 Font numbers must be between 1 and 14.
180 
181 The precision can be:
182 
183  - `precision = 0` fast hardware fonts (steps in the size)
184  - `precision = 1` scalable and rotatable hardware fonts (see below)
185  - `precision = 2` scalable and rotatable hardware fonts
186  - `precision = 3` scalable and rotatable hardware fonts. Text size
187  is given in pixels.
188 
189 The text font and precision of any class inheriting from `TAttText` can
190 be changed using the method `SetTextFont` and retrieved using the
191 method `GetTextFont`.
192 
193 \anchor T51
194 ### Font quality and speed
195 
196 When precision 0 is used, only the original non-scaled system fonts are
197 used. The fonts have a minimum (4) and maximum (37) size in pixels. These
198 fonts are fast and are of good quality. Their size varies with large steps
199 and they cannot be rotated.
200 Precision 1 and 2 fonts have a different behaviour depending if the
201 True Type Fonts (TTF) are used or not. If TTF are used, you always get very good
202 quality scalable and rotatable fonts. However TTF are slow.
203 
204 \anchor T52
205 ### How to use True Type Fonts
206 
207 One can activate the TTF by adding (or activating) the following line
208 in the `.rootrc` file:
209 
210 ~~~ {.cpp}
211  Unix.*.Root.UseTTFonts: true
212 ~~~
213 
214 It is possible to check the TTF are in use in a Root session
215 with the command:
216 
217 ~~~ {.cpp}
218  gEnv->Print();
219 ~~~
220 
221 If the TTF are in use the following line will appear at the beginning of the
222 printout given by this command:
223 
224 ~~~ {.cpp}
225  Unix.*.Root.UseTTFonts: true [Global]
226 ~~~
227 
228 \anchor T53
229 ### List of the currently supported fonts
230 
231 ~~~ {.cpp}
232  Font number X11 Names Win32/TTF Names
233  1 : times-medium-i-normal "Times New Roman"
234  2 : times-bold-r-normal "Times New Roman"
235  3 : times-bold-i-normal "Times New Roman"
236  4 : helvetica-medium-r-normal "Arial"
237  5 : helvetica-medium-o-normal "Arial"
238  6 : helvetica-bold-r-normal "Arial"
239  7 : helvetica-bold-o-normal "Arial"
240  8 : courier-medium-r-normal "Courier New"
241  9 : courier-medium-o-normal "Courier New"
242  10 : courier-bold-r-normal "Courier New"
243  11 : courier-bold-o-normal "Courier New"
244  12 : symbol-medium-r-normal "Symbol"
245  13 : times-medium-r-normal "Times New Roman"
246  14 : "Wingdings"
247  15 : Symbol italic (derived from Symbol)
248 ~~~
249 
250 The following picture shows how each font looks. The number on the left
251 is the "text font code". In this picture precision 2 was selected.
252 
253 Begin_Macro
254 fonts.C
255 End_Macro
256 */
257 
258 ////////////////////////////////////////////////////////////////////////////////
259 /// AttText default constructor.
260 ///
261 /// Default text attributes are taken from the current style.
262 
264 {
265  if (!gStyle) {
266  ResetAttText();
267  return;
268  }
274 }
275 
276 ////////////////////////////////////////////////////////////////////////////////
277 /// AttText normal constructor.
278 ///
279 /// Text attributes are taken from the argument list.
280 
281 TAttText::TAttText(Int_t align, Float_t angle, Color_t color, Style_t font, Float_t tsize)
282 {
283  fTextAlign = align;
284  fTextAngle = angle;
285  fTextColor = color;
286  fTextFont = font;
287  fTextSize = tsize;
288 }
289 
290 ////////////////////////////////////////////////////////////////////////////////
291 /// AttText destructor.
292 
294 {
295 }
296 
297 ////////////////////////////////////////////////////////////////////////////////
298 /// Copy this text attributes to a new TAttText.
299 
300 void TAttText::Copy(TAttText &atttext) const
301 {
302  atttext.fTextAlign = fTextAlign;
303  atttext.fTextAngle = fTextAngle;
304  atttext.fTextColor = fTextColor;
305  atttext.fTextFont = fTextFont;
306  atttext.fTextSize = fTextSize;
307 }
308 
309 ////////////////////////////////////////////////////////////////////////////////
310 /// Change current text attributes if necessary.
311 
312 void TAttText::Modify()
313 {
314  if (!gPad) return;
315 
316  // Do we need to change font?
317  if (!gPad->IsBatch()) {
318  gVirtualX->SetTextAngle(fTextAngle);
319  Float_t wh = (Float_t)gPad->XtoPixel(gPad->GetX2());
320  Float_t hh = (Float_t)gPad->YtoPixel(gPad->GetY1());
321  Float_t tsize;
322  if (wh < hh) tsize = fTextSize*wh;
323  else tsize = fTextSize*hh;
324  if (fTextFont%10 > 2) tsize = fTextSize;
325 
326  if (gVirtualX->GetTextFont() != fTextFont) {
327  gVirtualX->SetTextFont(fTextFont);
328  gVirtualX->SetTextSize(tsize);
329  }
330  if (gVirtualX->GetTextSize() != tsize)
331  gVirtualX->SetTextSize(tsize);
332  gVirtualX->SetTextAlign(fTextAlign);
333  gVirtualX->SetTextColor(fTextColor);
334  }
336 }
337 
338 ////////////////////////////////////////////////////////////////////////////////
339 /// Reset this text attributes to default values.
340 
342 {
343  fTextAlign = 11;
344  fTextAngle = 0;
345  fTextColor = 1;
346  fTextFont = 62;
347  fTextSize = 0.05;
348 }
349 
350 ////////////////////////////////////////////////////////////////////////////////
351 /// Save text attributes as C++ statement(s) on output stream out.
352 
353 void TAttText::SaveTextAttributes(std::ostream &out, const char *name, Int_t alidef,
354  Float_t angdef, Int_t coldef, Int_t fondef,
355  Float_t sizdef)
356 {
357  if (fTextAlign != alidef) {
358  out<<" "<<name<<"->SetTextAlign("<<fTextAlign<<");"<<std::endl;
359  }
360  if (fTextColor != coldef) {
361  if (fTextColor > 228) {
363  out<<" "<<name<<"->SetTextColor(ci);" << std::endl;
364  } else
365  out<<" "<<name<<"->SetTextColor("<<fTextColor<<");"<<std::endl;
366  }
367  if (fTextFont != fondef) {
368  out<<" "<<name<<"->SetTextFont("<<fTextFont<<");"<<std::endl;
369  }
370  if (fTextSize != sizdef) {
371  out<<" "<<name<<"->SetTextSize("<<fTextSize<<");"<<std::endl;
372  }
373  if (fTextAngle != angdef) {
374  out<<" "<<name<<"->SetTextAngle("<<fTextAngle<<");"<<std::endl;
375  }
376 }
377 
378 ////////////////////////////////////////////////////////////////////////////////
379 /// Invoke the DialogCanvas Text attributes.
380 
382 {
385 }
386 
387 ////////////////////////////////////////////////////////////////////////////////
388 /// Set a transparent marker color. talpha defines the percentage of
389 /// the color opacity from 0. (fully transparent) to 1. (fully opaque).
390 
391 void TAttText::SetTextColorAlpha(Color_t tcolor, Float_t talpha)
392 {
393  fTextColor = TColor::GetColorTransparent(tcolor, talpha);
394 }
395 
396 ////////////////////////////////////////////////////////////////////////////////
397 /// Set the text size in pixels.
398 ///
399 /// If the font precision is greater than 2, the text size is set to npixels,
400 /// otherwise the text size is computed as a percent of the pad size.
401 
403 {
404  if (fTextFont%10 > 2) {
405  fTextSize = Float_t(npixels);
406  } else {
407  TVirtualPad *pad = gROOT->GetSelectedPad();
408  if (!pad) return;
409  Float_t dy = pad->AbsPixeltoY(0) - pad->AbsPixeltoY(npixels);
410  fTextSize = dy/(pad->GetY2() - pad->GetY1());
411  }
412 }
TAttText
Text Attributes class.
Definition: TAttText.h:18
Style_t
short Style_t
Definition: RtypesCore.h:89
Option_t
const char Option_t
Definition: RtypesCore.h:66
gVirtualX
#define gVirtualX
Definition: TVirtualX.h:338
TAttText::~TAttText
virtual ~TAttText()
AttText destructor.
Definition: TAttText.cxx:292
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TAttText::GetTextColor
virtual Color_t GetTextColor() const
Return the text color.
Definition: TAttText.h:34
TAttText.h
TAttText::fTextAlign
Short_t fTextAlign
Text alignment.
Definition: TAttText.h:23
TAttText::SaveTextAttributes
virtual void SaveTextAttributes(std::ostream &out, const char *name, Int_t alidef=12, Float_t angdef=0, Int_t coldef=1, Int_t fondef=61, Float_t sizdef=1)
Save text attributes as C++ statement(s) on output stream out.
Definition: TAttText.cxx:352
TAttText::TAttText
TAttText()
AttText default constructor.
Definition: TAttText.cxx:262
Float_t
float Float_t
Definition: RtypesCore.h:57
TStyle.h
TAttText::Copy
void Copy(TAttText &atttext) const
Copy this text attributes to a new TAttText.
Definition: TAttText.cxx:299
Strlen.h
TAttText::fTextSize
Float_t fTextSize
Text size.
Definition: TAttText.h:22
TColor.h
TVirtualX.h
TAttText::GetTextSize
virtual Float_t GetTextSize() const
Return the text size.
Definition: TAttText.h:36
TAttText::fTextAngle
Float_t fTextAngle
Text angle.
Definition: TAttText.h:21
TColor::SaveColor
static void SaveColor(std::ostream &out, Int_t ci)
Save a color with index > 228 as a C++ statement(s) on output stream out.
Definition: TColor.cxx:2127
TVirtualPadEditor::UpdateTextAttributes
static void UpdateTextAttributes(Int_t align, Float_t angle, Int_t col, Int_t font, Float_t tsize)
Update text attributes via the pad editor.
Definition: TVirtualPadEditor.cxx:140
Color_t
short Color_t
Definition: RtypesCore.h:92
TAttText::ResetAttText
virtual void ResetAttText(Option_t *toption="")
Reset this text attributes to default values.
Definition: TAttText.cxx:340
TVirtualPad::GetY2
virtual Double_t GetY2() const =0
TAttText::Modify
virtual void Modify()
Change current text attributes if necessary.
Definition: TAttText.cxx:311
TROOT.h
TAttText::GetTextAngle
virtual Float_t GetTextAngle() const
Return the text angle.
Definition: TAttText.h:33
TAttText::fTextColor
Color_t fTextColor
Text color.
Definition: TAttText.h:24
TVirtualPadEditor.h
gStyle
R__EXTERN TStyle * gStyle
Definition: TStyle.h:412
TAttText::GetTextFont
virtual Font_t GetTextFont() const
Return the text font.
Definition: TAttText.h:35
TVirtualPad::AbsPixeltoY
virtual Double_t AbsPixeltoY(Int_t py)=0
TVirtualPad.h
TAttText::GetTextAlign
virtual Short_t GetTextAlign() const
Return the text alignment.
Definition: TAttText.h:32
TVirtualPad
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition: TVirtualPad.h:51
TAttText::SetTextSizePixels
virtual void SetTextSizePixels(Int_t npixels)
Set the text size in pixels.
Definition: TAttText.cxx:401
TColor::GetColorTransparent
static Int_t GetColorTransparent(Int_t color, Float_t a)
Static function: Returns the transparent color number corresponding to n.
Definition: TColor.cxx:1985
TVirtualPad::GetY1
virtual Double_t GetY1() const =0
TAttText::SetTextColorAlpha
virtual void SetTextColorAlpha(Color_t tcolor, Float_t talpha)
Set a transparent marker color.
Definition: TAttText.cxx:390
name
char name[80]
Definition: TGX11.cxx:110
TAttText::fTextFont
Font_t fTextFont
Text font.
Definition: TAttText.h:25
gPad
#define gPad
Definition: TVirtualPad.h:287
TAttText::SetTextAttributes
virtual void SetTextAttributes()
Invoke the DialogCanvas Text attributes.
Definition: TAttText.cxx:380
gROOT
#define gROOT
Definition: TROOT.h:406
int
TError.h