Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 \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","Fill Area colors",0,0,500,200);
119  c->DrawColorTable();
120  return c;
121 }
122 End_Macro
123 
124 ### Color transparency
125 `SetTextColorAlpha()`, allows to set a transparent color.
126 In the following example the text color of the text `text`
127 is set to blue with a transparency of 35%. The color `kBlue`
128 itself remains fully opaque.
129 
130 ~~~ {.cpp}
131 text->SetTextColorAlpha(kBlue, 0.35);
132 ~~~
133 
134 The transparency is available on all platforms when the flag `OpenGL.CanvasPreferGL` is set to `1`
135 in `$ROOTSYS/etc/system.rootrc`, or on Mac with the Cocoa backend. On the file output
136 it is visible with PDF, PNG, Gif, JPEG, SVG, TeX ... but not PostScript.
137 
138 ## <a name="T4"></a> Text Size
139 
140 If the text precision (see next paragraph) is smaller than 3, the text
141 size (`textsize`) is a fraction of the current pad size. Therefore the
142 same `textsize` value can generate text outputs with different absolute
143 sizes in two different pads.
144 The text size in pixels (`charheight`) is computed the following way:
145 
146 ~~~ {.cpp}
147  pad_width = gPad->XtoPixel(gPad->GetX2());
148  pad_height = gPad->YtoPixel(gPad->GetY1());
149  if (pad_width < pad_height) charheight = textsize*pad_width;
150  else charheight = textsize*pad_height;
151 ~~~
152 
153 If the text precision is equal to 3, the text size doesn't depend on the pad's
154 dimensions. A given `textsize` value always generates the same absolute
155 size. The text size (`charheight`) is given in pixels:
156 
157 ~~~ {.cpp}
158  charheight = textsize;
159 ~~~
160 
161 Note that to scale fonts to the same size as the old True Type package a
162 scale factor of `0.93376068` is apply to the text size before drawing.
163 
164 The text size of any class inheriting from `TAttText` can
165 be changed using the method `SetTextSize` and retrieved using the
166 method `GetTextSize`.
167 
168 ## <a name="T5"></a> Text Font and Precision
169 
170 The text font code is combination of the font number and the precision.
171 ~~~ {.cpp}
172  Text font code = 10*fontnumber + precision
173 ~~~
174 Font numbers must be between 1 and 14.
175 
176 The precision can be:
177 
178  - `precision = 0` fast hardware fonts (steps in the size)
179  - `precision = 1` scalable and rotatable hardware fonts (see below)
180  - `precision = 2` scalable and rotatable hardware fonts
181  - `precision = 3` scalable and rotatable hardware fonts. Text size
182  is given in pixels.
183 
184 The text font and precision of any class inheriting from `TAttText` can
185 be changed using the method `SetTextFont` and retrieved using the
186 method `GetTextFont`.
187 
188 ### <a name="T51"></a> Font quality and speed
189 
190 When precision 0 is used, only the original non-scaled system fonts are
191 used. The fonts have a minimum (4) and maximum (37) size in pixels. These
192 fonts are fast and are of good quality. Their size varies with large steps
193 and they cannot be rotated.
194 Precision 1 and 2 fonts have a different behaviour depending if the
195 True Type Fonts (TTF) are used or not. If TTF are used, you always get very good
196 quality scalable and rotatable fonts. However TTF are slow.
197 
198 ### <a name="T52"></a> How to use True Type Fonts
199 
200 One can activate the TTF by adding (or activating) the following line
201 in the `.rootrc` file:
202 
203 ~~~ {.cpp}
204  Unix.*.Root.UseTTFonts: true
205 ~~~
206 
207 It is possible to check the TTF are in use in a Root session
208 with the command:
209 
210 ~~~ {.cpp}
211  gEnv->Print();
212 ~~~
213 
214 If the TTF are in use the following line will appear at the beginning of the
215 printout given by this command:
216 
217 ~~~ {.cpp}
218  Unix.*.Root.UseTTFonts: true [Global]
219 ~~~
220 
221 ### <a name="T53"></a> List of the currently supported fonts
222 
223 ~~~ {.cpp}
224  Font number X11 Names Win32/TTF Names
225  1 : times-medium-i-normal "Times New Roman"
226  2 : times-bold-r-normal "Times New Roman"
227  3 : times-bold-i-normal "Times New Roman"
228  4 : helvetica-medium-r-normal "Arial"
229  5 : helvetica-medium-o-normal "Arial"
230  6 : helvetica-bold-r-normal "Arial"
231  7 : helvetica-bold-o-normal "Arial"
232  8 : courier-medium-r-normal "Courier New"
233  9 : courier-medium-o-normal "Courier New"
234  10 : courier-bold-r-normal "Courier New"
235  11 : courier-bold-o-normal "Courier New"
236  12 : symbol-medium-r-normal "Symbol"
237  13 : times-medium-r-normal "Times New Roman"
238  14 : "Wingdings"
239  15 : Symbol italic (derived from Symbol)
240 ~~~
241 
242 The following picture shows how each font looks. The number on the left
243 is the "text font code". In this picture precision 2 was selected.
244 
245 Begin_Macro
246 fonts.C
247 End_Macro
248 */
249 
250 ////////////////////////////////////////////////////////////////////////////////
251 /// AttText default constructor.
252 ///
253 /// Default text attributes are taken from the current style.
254 
255 TAttText::TAttText()
256 {
257  if (!gStyle) {
258  ResetAttText();
259  return;
260  }
261  fTextAlign = gStyle->GetTextAlign();
262  fTextAngle = gStyle->GetTextAngle();
263  fTextColor = gStyle->GetTextColor();
264  fTextFont = gStyle->GetTextFont();
265  fTextSize = gStyle->GetTextSize();
266 }
267 
268 ////////////////////////////////////////////////////////////////////////////////
269 /// AttText normal constructor.
270 ///
271 /// Text attributes are taken from the argument list.
272 
273 TAttText::TAttText(Int_t align, Float_t angle, Color_t color, Style_t font, Float_t tsize)
274 {
275  fTextAlign = align;
276  fTextAngle = angle;
277  fTextColor = color;
278  fTextFont = font;
279  fTextSize = tsize;
280 }
281 
282 ////////////////////////////////////////////////////////////////////////////////
283 /// AttText destructor.
284 
285 TAttText::~TAttText()
286 {
287 }
288 
289 ////////////////////////////////////////////////////////////////////////////////
290 /// Copy this text attributes to a new TAttText.
291 
292 void TAttText::Copy(TAttText &atttext) const
293 {
294  atttext.fTextAlign = fTextAlign;
295  atttext.fTextAngle = fTextAngle;
296  atttext.fTextColor = fTextColor;
297  atttext.fTextFont = fTextFont;
298  atttext.fTextSize = fTextSize;
299 }
300 
301 ////////////////////////////////////////////////////////////////////////////////
302 /// Change current text attributes if necessary.
303 
304 void TAttText::Modify()
305 {
306  if (!gPad) return;
307 
308  // Do we need to change font?
309  if (!gPad->IsBatch()) {
310  gVirtualX->SetTextAngle(fTextAngle);
311  Float_t wh = (Float_t)gPad->XtoPixel(gPad->GetX2());
312  Float_t hh = (Float_t)gPad->YtoPixel(gPad->GetY1());
313  Float_t tsize;
314  if (wh < hh) tsize = fTextSize*wh;
315  else tsize = fTextSize*hh;
316  if (fTextFont%10 > 2) tsize = fTextSize;
317 
318  if (gVirtualX->GetTextFont() != fTextFont) {
319  gVirtualX->SetTextFont(fTextFont);
320  gVirtualX->SetTextSize(tsize);
321  }
322  if (gVirtualX->GetTextSize() != tsize)
323  gVirtualX->SetTextSize(tsize);
324  gVirtualX->SetTextAlign(fTextAlign);
325  gVirtualX->SetTextColor(fTextColor);
326  }
327  gPad->SetAttTextPS(fTextAlign,fTextAngle,fTextColor,fTextFont,fTextSize);
328 }
329 
330 ////////////////////////////////////////////////////////////////////////////////
331 /// Reset this text attributes to default values.
332 
333 void TAttText::ResetAttText(Option_t *)
334 {
335  fTextAlign = 11;
336  fTextAngle = 0;
337  fTextColor = 1;
338  fTextFont = 62;
339  fTextSize = 0.05;
340 }
341 
342 ////////////////////////////////////////////////////////////////////////////////
343 /// Save text attributes as C++ statement(s) on output stream out.
344 
345 void TAttText::SaveTextAttributes(std::ostream &out, const char *name, Int_t alidef,
346  Float_t angdef, Int_t coldef, Int_t fondef,
347  Float_t sizdef)
348 {
349  if (fTextAlign != alidef) {
350  out<<" "<<name<<"->SetTextAlign("<<fTextAlign<<");"<<std::endl;
351  }
352  if (fTextColor != coldef) {
353  if (fTextColor > 228) {
354  TColor::SaveColor(out, fTextColor);
355  out<<" "<<name<<"->SetTextColor(ci);" << std::endl;
356  } else
357  out<<" "<<name<<"->SetTextColor("<<fTextColor<<");"<<std::endl;
358  }
359  if (fTextFont != fondef) {
360  out<<" "<<name<<"->SetTextFont("<<fTextFont<<");"<<std::endl;
361  }
362  if (fTextSize != sizdef) {
363  out<<" "<<name<<"->SetTextSize("<<fTextSize<<");"<<std::endl;
364  }
365  if (fTextAngle != angdef) {
366  out<<" "<<name<<"->SetTextAngle("<<fTextAngle<<");"<<std::endl;
367  }
368 }
369 
370 ////////////////////////////////////////////////////////////////////////////////
371 /// Invoke the DialogCanvas Text attributes.
372 
373 void TAttText::SetTextAttributes()
374 {
375  TVirtualPadEditor::UpdateTextAttributes(fTextAlign,fTextAngle,fTextColor,
376  fTextFont,fTextSize);
377 }
378 
379 ////////////////////////////////////////////////////////////////////////////////
380 /// Set a transparent marker color. talpha defines the percentage of
381 /// the color opacity from 0. (fully transparent) to 1. (fully opaque).
382 
383 void TAttText::SetTextColorAlpha(Color_t tcolor, Float_t talpha)
384 {
385  fTextColor = TColor::GetColorTransparent(tcolor, talpha);
386 }
387 
388 ////////////////////////////////////////////////////////////////////////////////
389 /// Set the text size in pixels.
390 ///
391 /// If the font precision is greater than 2, the text size is set to npixels,
392 /// otherwise the text size is computed as a percent of the pad size.
393 
394 void TAttText::SetTextSizePixels(Int_t npixels)
395 {
396  if (fTextFont%10 > 2) {
397  fTextSize = Float_t(npixels);
398  } else {
399  TVirtualPad *pad = gROOT->GetSelectedPad();
400  if (!pad) return;
401  Float_t dy = pad->AbsPixeltoY(0) - pad->AbsPixeltoY(npixels);
402  fTextSize = dy/(pad->GetY2() - pad->GetY1());
403  }
404 }