Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
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](\ref ATTTEXT1)
38 - [Text Angle](\ref ATTTEXT2)
39 - [Text Color](\ref ATTTEXT3)
40 - [Text Size](\ref ATTTEXT4)
41 - [Text Font and Precision](\ref ATTTEXT5)
42 - [Font quality and speed](\ref ATTTEXT51)
43 - [How to use True Type Fonts](\ref ATTTEXT52)
44 - [List of the currently supported fonts](\ref ATTTEXT53)
45
46\anchor ATTTEXT1
47## Text Alignment
48
49The text alignment is an integer number (`align`) allowing to control
50the horizontal and vertical position of the text string with respect
51to the text position.
52The text alignment of any class inheriting from `TAttText` can
53be changed using the method `SetTextAlign` and retrieved using the
54method `GetTextAlign`.
55
56~~~ {.cpp}
57 align = 10*HorizontalAlign + VerticalAlign
58~~~
59
60For horizontal alignment the following convention applies:
61
62~~~ {.cpp}
63 1=left adjusted, 2=centered, 3=right adjusted
64~~~
65
66For vertical alignment the following convention applies:
67
68~~~ {.cpp}
69 1=bottom adjusted, 2=centered, 3=top adjusted
70~~~
71
72For example:
73
74~~~ {.cpp}
75 align = 11 = left adjusted and bottom adjusted
76 align = 32 = right adjusted and vertically centered
77~~~
78
79Begin_Macro(source)
80textalign.C
81End_Macro
82
83Mnemonic constants are available:
84
85~~~ {.cpp}
86kHAlignLeft = 10, kHAlignCenter = 20, kHAlignRight = 30,
87kVAlignBottom = 1, kVAlignCenter = 2, kVAlignTop = 3
88~~~
89
90They allow to write:
91
92~~~ {.cpp}
93object->SetTextAlign(kHAlignLeft+kVAlignTop);
94~~~
95
96\anchor ATTTEXT2
97## Text Angle
98
99Text angle in degrees.
100The text angle of any class inheriting from `TAttText` can
101be changed using the method `SetTextAngle` and retrieved using the
102method `GetTextAngle`.
103The following picture shows the text angle:
104
105Begin_Macro(source)
106textangle.C
107End_Macro
108
109\anchor ATTTEXT3
110## Text Color
111
112The text color is a color index (integer) pointing in the ROOT
113color table.
114The text color of any class inheriting from `TAttText` can
115be changed using the method `SetTextColor` and retrieved using the
116method `GetTextColor`.
117The following table shows the first 50 default colors.
118
119Begin_Macro
120{
121 TCanvas *c = new TCanvas("c","Fill Area colors",0,0,500,200);
122 c->DrawColorTable();
123 return c;
124}
125End_Macro
126
127### Color transparency
128`SetTextColorAlpha()`, allows to set a transparent color.
129In the following example the text color of the text `text`
130is set to blue with an opacity of 35% (i.e. a transparency of 65%).
131(The color `kBlue` itself is internally stored as fully opaque.)
132
133~~~ {.cpp}
134text->SetTextColorAlpha(kBlue, 0.35);
135~~~
136
137The transparency is available on all platforms when the flag `OpenGL.CanvasPreferGL` is set to `1`
138in `$ROOTSYS/etc/system.rootrc`, or on Mac with the Cocoa backend. On the file output
139it is visible with PDF, PNG, Gif, JPEG, SVG, TeX ... but not PostScript.
140
141Alternatively, you can call at the top of your script `gSytle->SetCanvasPreferGL();`.
142Or if you prefer to activate GL for a single canvas `c`, then use `c->SetSupportGL(true);`.
143
144\anchor ATTTEXT4
145## Text Size
146
147If the text precision (see next paragraph) is smaller than 3, the text
148size (`textsize`) is a fraction of the current pad size. Therefore the
149same `textsize` value can generate text outputs with different absolute
150sizes in two different pads.
151The text size in pixels (`charheight`) is computed the following way:
152
153~~~ {.cpp}
154 pad_width = gPad->XtoPixel(gPad->GetX2());
155 pad_height = gPad->YtoPixel(gPad->GetY1());
156 if (pad_width < pad_height) charheight = textsize*pad_width;
157 else charheight = textsize*pad_height;
158~~~
159
160If the text precision is equal to 3, the text size doesn't depend on the pad's
161dimensions. A given `textsize` value always generates the same absolute
162size. The text size (`charheight`) is given in pixels:
163
164~~~ {.cpp}
165 charheight = textsize;
166~~~
167
168Note that to scale fonts to the same size as the old True Type package a
169scale factor of `0.93376068` is apply to the text size before drawing.
170
171The text size of any class inheriting from `TAttText` can
172be changed using the method `SetTextSize` and retrieved using the
173method `GetTextSize`.
174
175\anchor ATTTEXT5
176## Text Font and Precision
177
178The text font code is combination of the font number and the precision.
179~~~ {.cpp}
180 Text font code = 10*fontnumber + precision
181~~~
182Font numbers must be between 1 and 14.
183
184The precision can be:
185
186 - `precision = 0` fast hardware fonts (steps in the size)
187 - `precision = 1` scalable and rotatable hardware fonts (see below)
188 - `precision = 2` scalable and rotatable hardware fonts
189 - `precision = 3` scalable and rotatable hardware fonts. Text size
190 is given in pixels.
191
192The text font and precision of any class inheriting from `TAttText` can
193be changed using the method `SetTextFont` and retrieved using the
194method `GetTextFont`.
195
196\anchor ATTTEXT51
197### Font quality and speed
198
199When precision 0 is used, only the original non-scaled X11 system fonts are
200used. The fonts have a minimum (4) and maximum (37) size in pixels. These
201fonts are fast and are of good quality. Their size varies with large steps
202and they cannot be rotated.
203Precision 1 and 2 fonts have a different behaviour depending if the
204True Type Fonts (TTF) are used or not. If TTF are used, you always get very good
205quality scalable and rotatable fonts.
206These days TTF fonts are rendered fast enough and can be used in all cases.
207
208\anchor ATTTEXT52
209### How to use True Type Fonts
210
211TTF fonts are used by default. They can be deactivated via the following line
212in the `.rootrc` file:
213
214~~~ {.cpp}
215 Unix.*.Root.UseTTFonts: false
216~~~
217
218\anchor ATTTEXT53
219### List of the currently supported fonts
220
221~~~ {.cpp}
222 Font number TTF Names PostScript/PDF Names
223 1 : "Free Serif Italic" "Times-Italic"
224 2 : "Free Serif Bold" "Times-Bold"
225 3 : "Free Serif Bold Italic" "Times-BoldItalic"
226 4 : "Tex Gyre Regular" "Helvetica"
227 5 : "Tex Gyre Italic" "Helvetica-Oblique"
228 6 : "Tex Gyre Bold" "Helvetica-Bold"
229 7 : "Tex Gyre Bold Italic" "Helvetica-BoldOblique"
230 8 : "Free Mono" "Courier"
231 9 : "Free Mono Oblique" "Courier-Oblique"
232 10 : "Free Mono Bold" "Courier-Bold"
233 11 : "Free Mono Bold Oblique" "Courier-BoldOblique"
234 12 : "Symbol" "Symbol"
235 13 : "Free Serif" "Times-Roman"
236 14 : "Wingdings" "ZapfDingbats"
237~~~
238
239The PostScript and PDF backends use the original PostScript-defined 13 fonts' styles
240forming four type families (Courier, Helvetica, Times, Symbol) as listed in the
241"Core Font Set" section of [this page](https://en.wikipedia.org/wiki/PostScript_fonts).
242These fonts are always available and do not need to be loaded in the PS or PDF files
243allowing to keep the files' sizes small.
244
245On screen, text is rendered using free TTF fonts similar to the PDF ones. The corresponding
246font files are coming with the ROOT distribution in `$ROOTSYS/fonts/Free*`.
247
248Begin_Macro
249fonts.C
250End_Macro
251*/
252
253////////////////////////////////////////////////////////////////////////////////
254/// AttText default constructor.
255///
256/// Default text attributes are taken from the current style.
259{
260 if (!gStyle) {
261 ResetAttText();
262 return;
263 }
269}
270
271////////////////////////////////////////////////////////////////////////////////
272/// AttText normal constructor.
273///
274/// Text attributes are taken from the argument list.
276TAttText::TAttText(Int_t align, Float_t angle, Color_t color, Style_t font, Float_t tsize)
277{
278 fTextAlign = align;
280 fTextColor = color;
281 fTextFont = font;
282 fTextSize = tsize;
283}
284
285////////////////////////////////////////////////////////////////////////////////
286/// AttText destructor.
289{
290}
291
292////////////////////////////////////////////////////////////////////////////////
293/// Copy this text attributes to a new TAttText.
295void TAttText::Copy(TAttText &atttext) const
296{
297 atttext.fTextAlign = fTextAlign;
298 atttext.fTextAngle = fTextAngle;
299 atttext.fTextColor = fTextColor;
300 atttext.fTextFont = fTextFont;
301 atttext.fTextSize = fTextSize;
302}
303
304////////////////////////////////////////////////////////////////////////////////
305/// Return the text in percent of the pad size.
306///
307/// If the font precision is greater than 2, the text size returned is the size in pixel
308/// converted into percent of the pad size, otherwise the size returned is the same as the
309/// size given as input parameter.
312{
313 Float_t rsize = size;
314 if (fTextFont%10 > 2 && gPad) {
315 UInt_t w = TMath::Abs(gPad->XtoAbsPixel(gPad->GetX2()) -
316 gPad->XtoAbsPixel(gPad->GetX1()));
317 UInt_t h = TMath::Abs(gPad->YtoAbsPixel(gPad->GetY2()) -
318 gPad->YtoAbsPixel(gPad->GetY1()));
319 if (w < h)
320 rsize = rsize/w;
321 else
322 rsize = rsize/h;
323 }
324 return rsize;
325}
326
327////////////////////////////////////////////////////////////////////////////////
328/// Change current text attributes if necessary.
330void TAttText::Modify()
331{
332 if (!gPad) return;
333
334 // Do we need to change font?
335 if (!gPad->IsBatch()) {
336 gVirtualX->SetTextAngle(fTextAngle);
337 Float_t tsize;
338 if (fTextFont%10 > 2) {
339 tsize = fTextSize;
340 } else {
341 Float_t wh = (Float_t)gPad->XtoPixel(gPad->GetX2());
342 Float_t hh = (Float_t)gPad->YtoPixel(gPad->GetY1());
343 if (wh < hh) tsize = fTextSize*wh;
344 else tsize = fTextSize*hh;
345 }
346
347 if (gVirtualX->GetTextFont() != fTextFont) {
348 gVirtualX->SetTextFont(fTextFont);
349 gVirtualX->SetTextSize(tsize);
350 } else if (gVirtualX->GetTextSize() != tsize) {
351 gVirtualX->SetTextSize(tsize);
352 }
353 gVirtualX->SetTextAlign(fTextAlign);
354 gVirtualX->SetTextColor(fTextColor);
355 }
357}
358
359////////////////////////////////////////////////////////////////////////////////
360/// Reset this text attributes to default values.
363{
364 fTextAlign = 11;
365 fTextAngle = 0;
366 fTextColor = 1;
367 fTextFont = 62;
368 fTextSize = 0.05;
369}
370
371////////////////////////////////////////////////////////////////////////////////
372/// Save text attributes as C++ statement(s) on output stream out.
374void TAttText::SaveTextAttributes(std::ostream &out, const char *name, Int_t alidef,
375 Float_t angdef, Int_t coldef, Int_t fondef,
376 Float_t sizdef)
377{
378 if (fTextAlign != alidef) {
379 out<<" "<<name<<"->SetTextAlign("<<fTextAlign<<");"<<std::endl;
380 }
381 if (fTextColor != coldef) {
383 out<<" "<<name<<"->SetTextColor(ci);" << std::endl;
384 else
385 out<<" "<<name<<"->SetTextColor("<<fTextColor<<");"<<std::endl;
386 }
387 if (fTextFont != fondef) {
388 out<<" "<<name<<"->SetTextFont("<<fTextFont<<");"<<std::endl;
389 }
390 if (fTextSize != sizdef) {
391 out<<" "<<name<<"->SetTextSize("<<fTextSize<<");"<<std::endl;
392 }
393 if (fTextAngle != angdef) {
394 out<<" "<<name<<"->SetTextAngle("<<fTextAngle<<");"<<std::endl;
395 }
396}
397
398////////////////////////////////////////////////////////////////////////////////
399/// Invoke the DialogCanvas Text attributes.
402{
405}
406
407////////////////////////////////////////////////////////////////////////////////
408/// Set a transparent text color.
409/// \param tcolor defines the text color
410/// \param talpha defines the percentage of opacity from 0. (fully transparent) to 1. (fully opaque).
411/// \note talpha is ignored (treated as 1) if the TCanvas has no GL support activated.
414{
415 fTextColor = TColor::GetColorTransparent(tcolor, talpha);
416}
417
418////////////////////////////////////////////////////////////////////////////////
419/// Set the text size in pixels.
420///
421/// If the font precision is greater than 2, the text size is set to npixels,
422/// otherwise the text size is computed as a percent of the pad size.
425{
426 if (fTextFont%10 > 2) {
427 fTextSize = Float_t(npixels);
428 } else {
430 if (!pad) return;
431 Float_t dy = pad->AbsPixeltoY(0) - pad->AbsPixeltoY(npixels);
432 fTextSize = dy/(pad->GetY2() - pad->GetY1());
433 }
434}
#define h(i)
Definition RSha256.hxx:106
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
short Style_t
Definition RtypesCore.h:82
short Color_t
Definition RtypesCore.h:85
float Float_t
Definition RtypesCore.h:57
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:382
Option_t Option_t TPoint TPoint angle
char name[80]
Definition TGX11.cxx:110
#define gROOT
Definition TROOT.h:406
R__EXTERN TStyle * gStyle
Definition TStyle.h:436
#define gPad
#define gVirtualX
Definition TVirtualX.h:337
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:329
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:257
virtual void SetTextColorAlpha(Color_t tcolor, Float_t talpha)
Set a transparent text color.
Definition TAttText.cxx:412
virtual Color_t GetTextColor() const
Return the text color.
Definition TAttText.h:34
virtual ~TAttText()
AttText destructor.
Definition TAttText.cxx:287
virtual Float_t GetTextAngle() const
Return the text angle.
Definition TAttText.h:33
virtual void SetTextAttributes()
Invoke the DialogCanvas Text attributes.
Definition TAttText.cxx:400
virtual void SetTextSizePixels(Int_t npixels)
Set the text size in pixel.
Definition TAttText.cxx:423
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:373
virtual Float_t GetTextSizePercent(Float_t size)
Return the text in percent of the pad size.
Definition TAttText.cxx:310
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:294
virtual void ResetAttText(Option_t *toption="")
Reset this text attributes to default values.
Definition TAttText.cxx:361
Float_t fTextSize
Text size.
Definition TAttText.h:22
static Bool_t 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:2543
static Int_t GetColorTransparent(Int_t color, Float_t a)
Static function: Returns the transparent color number corresponding to n.
Definition TColor.cxx:2174
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:51
virtual TVirtualPad * GetSelectedPad() const =0
virtual Double_t GetY1() const =0
virtual Double_t AbsPixeltoY(Int_t py)=0
virtual Double_t GetY2() const =0
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:123