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