Logo ROOT   6.18/05
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
29Text Attributes class.
30
31This class is used (in general by secondary inheritance)
32by many other classes (graphics, histograms). It holds all the text attributes.
33
34## Text attributes
35Text 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
48The text alignment is an integer number (`align`) allowing to control
49the horizontal and vertical position of the text string with respect
50to the text position.
51The text alignment of any class inheriting from `TAttText` can
52be changed using the method `SetTextAlign` and retrieved using the
53method `GetTextAlign`.
54
55~~~ {.cpp}
56 align = 10*HorizontalAlign + VerticalAlign
57~~~
58
59For horizontal alignment the following convention applies:
60
61~~~ {.cpp}
62 1=left adjusted, 2=centered, 3=right adjusted
63~~~
64
65For vertical alignment the following convention applies:
66
67~~~ {.cpp}
68 1=bottom adjusted, 2=centered, 3=top adjusted
69~~~
70
71For example:
72
73~~~ {.cpp}
74 align = 11 = left adjusted and bottom adjusted
75 align = 32 = right adjusted and vertically centered
76~~~
77
78Begin_Macro(source)
79textalign.C
80End_Macro
81
82Mnemonic constants are available:
83
84~~~ {.cpp}
85kHAlignLeft = 10, kHAlignCenter = 20, kHAlignRight = 30,
86kVAlignBottom = 1, kVAlignCenter = 2, kVAlignTop = 3
87~~~
88
89They allow to write:
90
91~~~ {.cpp}
92object->SetTextAlign(kHAlignLeft+kVAlignTop);
93~~~
94
95## <a name="T2"></a> Text Angle
96
97Text angle in degrees.
98The text angle of any class inheriting from `TAttText` can
99be changed using the method `SetTextAngle` and retrieved using the
100method `GetTextAngle`.
101The following picture shows the text angle:
102
103Begin_Macro(source)
104textangle.C
105End_Macro
106
107## <a name="T3"></a> Text Color
108
109The text color is a color index (integer) pointing in the ROOT
110color table.
111The text color of any class inheriting from `TAttText` can
112be changed using the method `SetTextColor` and retrieved using the
113method `GetTextColor`.
114The following table shows the first 50 default colors.
115
116Begin_Macro
117{
118 TCanvas *c = new TCanvas("c","Fill Area colors",0,0,500,200);
119 c->DrawColorTable();
120 return c;
121}
122End_Macro
123
124### Color transparency
125`SetTextColorAlpha()`, allows to set a transparent color.
126In the following example the text color of the text `text`
127is set to blue with a transparency of 35%. The color `kBlue`
128itself remains fully opaque.
129
130~~~ {.cpp}
131text->SetTextColorAlpha(kBlue, 0.35);
132~~~
133
134The transparency is available on all platforms when the flag `OpenGL.CanvasPreferGL` is set to `1`
135in `$ROOTSYS/etc/system.rootrc`, or on Mac with the Cocoa backend. On the file output
136it is visible with PDF, PNG, Gif, JPEG, SVG, TeX ... but not PostScript.
137
138## <a name="T4"></a> Text Size
139
140If the text precision (see next paragraph) is smaller than 3, the text
141size (`textsize`) is a fraction of the current pad size. Therefore the
142same `textsize` value can generate text outputs with different absolute
143sizes in two different pads.
144The 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
153If the text precision is equal to 3, the text size doesn't depend on the pad's
154dimensions. A given `textsize` value always generates the same absolute
155size. The text size (`charheight`) is given in pixels:
156
157~~~ {.cpp}
158 charheight = textsize;
159~~~
160
161Note that to scale fonts to the same size as the old True Type package a
162scale factor of `0.93376068` is apply to the text size before drawing.
163
164The text size of any class inheriting from `TAttText` can
165be changed using the method `SetTextSize` and retrieved using the
166method `GetTextSize`.
167
168## <a name="T5"></a> Text Font and Precision
169
170The text font code is combination of the font number and the precision.
171~~~ {.cpp}
172 Text font code = 10*fontnumber + precision
173~~~
174Font numbers must be between 1 and 14.
175
176The 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
184The text font and precision of any class inheriting from `TAttText` can
185be changed using the method `SetTextFont` and retrieved using the
186method `GetTextFont`.
187
188### <a name="T51"></a> Font quality and speed
189
190When precision 0 is used, only the original non-scaled system fonts are
191used. The fonts have a minimum (4) and maximum (37) size in pixels. These
192fonts are fast and are of good quality. Their size varies with large steps
193and they cannot be rotated.
194Precision 1 and 2 fonts have a different behaviour depending if the
195True Type Fonts (TTF) are used or not. If TTF are used, you always get very good
196quality scalable and rotatable fonts. However TTF are slow.
197
198### <a name="T52"></a> How to use True Type Fonts
199
200One can activate the TTF by adding (or activating) the following line
201in the `.rootrc` file:
202
203~~~ {.cpp}
204 Unix.*.Root.UseTTFonts: true
205~~~
206
207It is possible to check the TTF are in use in a Root session
208with the command:
209
210~~~ {.cpp}
211 gEnv->Print();
212~~~
213
214If the TTF are in use the following line will appear at the beginning of the
215printout 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
242The following picture shows how each font looks. The number on the left
243is the "text font code". In this picture precision 2 was selected.
244
245Begin_Macro
246fonts.C
247End_Macro
248*/
249
250////////////////////////////////////////////////////////////////////////////////
251/// AttText default constructor.
252///
253/// Default text attributes are taken from the current style.
256{
257 if (!gStyle) {
258 ResetAttText();
259 return;
260 }
266}
267
268////////////////////////////////////////////////////////////////////////////////
269/// AttText normal constructor.
270///
271/// Text attributes are taken from the argument list.
273TAttText::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.
286{
287}
288
289////////////////////////////////////////////////////////////////////////////////
290/// Copy this text attributes to a new TAttText.
292void 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.
304void 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 }
328}
329
330////////////////////////////////////////////////////////////////////////////////
331/// Reset this text attributes to default values.
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.
345void 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) {
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.
374{
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).
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.
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}
int Int_t
Definition: RtypesCore.h:41
short Color_t
Definition: RtypesCore.h:79
short Style_t
Definition: RtypesCore.h:76
float Float_t
Definition: RtypesCore.h:53
const char Option_t
Definition: RtypesCore.h:62
#define ClassImp(name)
Definition: Rtypes.h:365
char name[80]
Definition: TGX11.cxx:109
#define gROOT
Definition: TROOT.h:414
R__EXTERN TStyle * gStyle
Definition: TStyle.h:406
#define gPad
Definition: TVirtualPad.h:286
#define gVirtualX
Definition: TVirtualX.h:345
Text Attributes class.
Definition: TAttText.h:18
virtual Float_t GetTextSize() const
Return the text size.
Definition: TAttText.h:36
virtual void Modify()
Change current text attributes if necessary.
Definition: TAttText.cxx:303
virtual Short_t GetTextAlign() const
Return the text alignment.
Definition: TAttText.h:32
virtual Font_t GetTextFont() const
Return the text font.
Definition: TAttText.h:35
Color_t fTextColor
Text color.
Definition: TAttText.h:24
Float_t fTextAngle
Text angle.
Definition: TAttText.h:21
TAttText()
AttText default constructor.
Definition: TAttText.cxx:254
virtual void SetTextColorAlpha(Color_t tcolor, Float_t talpha)
Set a transparent marker color.
Definition: TAttText.cxx:382
virtual Color_t GetTextColor() const
Return the text color.
Definition: TAttText.h:34
virtual ~TAttText()
AttText destructor.
Definition: TAttText.cxx:284
virtual Float_t GetTextAngle() const
Return the text angle.
Definition: TAttText.h:33
virtual void SetTextAttributes()
Invoke the DialogCanvas Text attributes.
Definition: TAttText.cxx:372
virtual void SetTextSizePixels(Int_t npixels)
Set the text size in pixels.
Definition: TAttText.cxx:393
Font_t fTextFont
Text font.
Definition: TAttText.h:25
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
Short_t fTextAlign
Text alignment.
Definition: TAttText.h:23
void Copy(TAttText &atttext) const
Copy this text attributes to a new TAttText.
Definition: TAttText.cxx:291
virtual void ResetAttText(Option_t *toption="")
Reset this text attributes to default values.
Definition: TAttText.cxx:332
Float_t fTextSize
Text size.
Definition: TAttText.h:22
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:2108
static Int_t GetColorTransparent(Int_t color, Float_t a)
Static function: Returns the transparent color number corresponding to n.
Definition: TColor.cxx:1966
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.
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition: TVirtualPad.h:50
virtual Double_t GetY1() const =0
virtual Double_t AbsPixeltoY(Int_t py)=0
virtual Double_t GetY2() const =0