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