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 "TVirtualPadPainter.h"
18#include "TVirtualPadEditor.h"
19#include "TMathBase.h"
20#include "TStyle.h"
21#include "TError.h"
22#include "TColor.h"
23
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`) computed in the following way:
152
153~~~ {.cpp}
154 UInt_t pad_width = gPad->GetPadWidth();
155 UInt_t pad_height = gPad->GetPadHeight();
156 Float_t charheight = textsize*TMath::Min(pad_width, pad_height);
157~~~
158
159This value can be obtained using GetTextSizePixels() method:
160
161~~~ {.cpp}
162 TText txt;
163 auto charheight = text.GetTextSizePixels(*gPad);
164~~~
165
166If the text precision is equal to 3, the text size doesn't depend on the pad's
167dimensions. A given `textsize` value always generates the same absolute
168size. The text size (`charheight`) is given in pixels:
169
170~~~ {.cpp}
171 charheight = textsize;
172~~~
173
174Note that to scale fonts to the same size as the old True Type package a
175scale factor of `0.93376068` is apply to the text size before drawing.
176
177The text size of any class inheriting from `TAttText` can
178be changed using the method `SetTextSize` and retrieved using the
179method `GetTextSize`.
180
181\anchor ATTTEXT5
182## Text Font and Precision
183
184The text font code is combination of the font number and the precision.
185~~~ {.cpp}
186 Text font code = 10*fontnumber + precision
187~~~
188Font numbers must be between 1 and 14.
189
190The precision can be:
191
192 - `precision = 0` fast hardware fonts (steps in the size)
193 - `precision = 1` scalable and rotatable hardware fonts (see below)
194 - `precision = 2` scalable and rotatable hardware fonts
195 - `precision = 3` scalable and rotatable hardware fonts. Text size
196 is given in pixels.
197
198The text font and precision of any class inheriting from `TAttText` can
199be changed using the method `SetTextFont` and retrieved using the
200method `GetTextFont`.
201
202\anchor ATTTEXT51
203### Font quality and speed
204
205When precision 0 is used, only the original non-scaled X11 system fonts are
206used. The fonts have a minimum (4) and maximum (37) size in pixels. These
207fonts are fast and are of good quality. Their size varies with large steps
208and they cannot be rotated.
209Precision 1 and 2 fonts have a different behaviour depending if the
210True Type Fonts (TTF) are used or not. If TTF are used, you always get very good
211quality scalable and rotatable fonts.
212These days TTF fonts are rendered fast enough and can be used in all cases.
213
214\anchor ATTTEXT52
215### How to use True Type Fonts
216
217TTF fonts are used by default. They can be deactivated via the following line
218in the `.rootrc` file:
219
220~~~ {.cpp}
221 Unix.*.Root.UseTTFonts: false
222~~~
223
224\anchor ATTTEXT53
225### List of the currently supported fonts
226
227~~~ {.cpp}
228 Font number TTF Names PostScript/PDF Names
229 1 : "Free Serif Italic" "Times-Italic"
230 2 : "Free Serif Bold" "Times-Bold"
231 3 : "Free Serif Bold Italic" "Times-BoldItalic"
232 4 : "Tex Gyre Regular" "Helvetica"
233 5 : "Tex Gyre Italic" "Helvetica-Oblique"
234 6 : "Tex Gyre Bold" "Helvetica-Bold"
235 7 : "Tex Gyre Bold Italic" "Helvetica-BoldOblique"
236 8 : "Free Mono" "Courier"
237 9 : "Free Mono Oblique" "Courier-Oblique"
238 10 : "Free Mono Bold" "Courier-Bold"
239 11 : "Free Mono Bold Oblique" "Courier-BoldOblique"
240 12 : "Symbol" "Symbol"
241 13 : "Free Serif" "Times-Roman"
242 14 : "Wingdings" "ZapfDingbats"
243~~~
244
245The PostScript and PDF backends use the original PostScript-defined 13 fonts' styles
246forming four type families (Courier, Helvetica, Times, Symbol) as listed in the
247"Core Font Set" section of [this page](https://en.wikipedia.org/wiki/PostScript_fonts).
248These fonts are always available and do not need to be loaded in the PS or PDF files
249allowing to keep the files' sizes small.
250
251On screen, text is rendered using free TTF fonts similar to the PDF ones. The corresponding
252font files are coming with the ROOT distribution in `$ROOTSYS/fonts/Free*`.
253
254Begin_Macro
255fonts.C
256End_Macro
257*/
258
259////////////////////////////////////////////////////////////////////////////////
260/// AttText default constructor.
261///
262/// Default text attributes are taken from the current style.
275}
276
277////////////////////////////////////////////////////////////////////////////////
278/// AttText normal constructor.
279///
280/// Text attributes are taken from the argument list.
283{
284 fTextAlign = align;
286 fTextColor = color;
287 fTextFont = font;
289}
290
291////////////////////////////////////////////////////////////////////////////////
292/// AttText destructor.
296}
297
298////////////////////////////////////////////////////////////////////////////////
299/// Copy this text attributes to a new TAttText.
302{
303 atttext.fTextAlign = fTextAlign;
304 atttext.fTextAngle = fTextAngle;
305 atttext.fTextColor = fTextColor;
306 atttext.fTextFont = fTextFont;
307 atttext.fTextSize = fTextSize;
308}
309
310////////////////////////////////////////////////////////////////////////////////
311/// Return the text size in percent of the pad size.
312///
313/// If the font precision is greater than 2, the text size returned is the size in pixel
314/// converted into percent of the pad size, otherwise the size returned is the same as the
315/// size given as input parameter.
318{
319 if ((GetTextFont() % 2 < 3) || !gPad)
320 return size;
321
322 auto size0 = fTextSize;
323 fTextSize = size;
326 return size;
327}
328
329////////////////////////////////////////////////////////////////////////////////
330/// Return the text size in relative units
331///
332/// If the font precision grater then 2 use pad dimensions to get value
335{
337 if (GetTextFont() % 10 > 2) {
338 UInt_t padw = pad.GetPadWidth();
339 UInt_t padh = pad.GetPadHeight();
341 }
342 return rsize;
343}
344
345////////////////////////////////////////////////////////////////////////////////
346/// Return the text size in pixels for the specified pad
347///
348/// If the font precision less than 3 size defined as percent of pad size and
349/// scaled to minimal pad size
352{
354 if (GetTextFont() % 10 <= 2) {
355 UInt_t padw = pad.GetPadWidth();
356 UInt_t padh = pad.GetPadHeight();
358 }
359 return tsize;
360}
361
362////////////////////////////////////////////////////////////////////////////////
363/// Change current text attributes if necessary.
365void TAttText::Modify()
366{
367 if (gPad)
368 ModifyOn(*gPad);
369}
370
371////////////////////////////////////////////////////////////////////////////////
372/// Change current text attributes if necessary on specified pad.
375{
376 auto pp = pad.GetPainter();
377 if (!pp)
378 return;
379
380 pp->OnPad(&pad);
381
382 pp->SetAttText(*this);
383}
384
385////////////////////////////////////////////////////////////////////////////////
386/// Reset this text attributes to default values.
389{
390 fTextAlign = 11;
391 fTextAngle = 0;
392 fTextColor = 1;
393 fTextFont = 62;
394 fTextSize = 0.05;
395}
396
397////////////////////////////////////////////////////////////////////////////////
398/// Save text attributes as C++ statement(s) on output stream out.
400void TAttText::SaveTextAttributes(std::ostream &out, const char *name, Int_t alidef,
403{
404 if (fTextAlign != alidef)
405 out << " " << name << "->SetTextAlign(" << fTextAlign << ");\n";
406 if (fTextColor != coldef)
407 out << " " << name << "->SetTextColor(" << TColor::SavePrimitiveColor(fTextColor) << ");\n";
408 if (fTextFont != fondef)
409 out << " " << name << "->SetTextFont(" << fTextFont << ");\n";
410 if (fTextSize != sizdef)
411 out << " " << name << "->SetTextSize(" << fTextSize << ");\n";
412 if (fTextAngle != angdef)
413 out << " " << name << "->SetTextAngle(" << fTextAngle << ");\n";
414}
415
416////////////////////////////////////////////////////////////////////////////////
417/// Invoke the DialogCanvas Text attributes.
423}
424
425////////////////////////////////////////////////////////////////////////////////
426/// Set a transparent text color.
427/// \param tcolor defines the text color
428/// \param talpha defines the percentage of opacity from 0. (fully transparent) to 1. (fully opaque).
429/// \note talpha is ignored (treated as 1) if the TCanvas has no GL support activated.
434}
435
436////////////////////////////////////////////////////////////////////////////////
437/// Set the text size in pixels.
438///
439/// If the font precision is greater than 2, the text size is set to npixels,
440/// otherwise the text size is computed as a percent of the pad size.
443{
444 if (fTextFont%10 > 2) {
446 } else {
447 TVirtualPad *pad = gROOT->GetSelectedPad();
448 if (!pad) return;
449 Float_t dy = pad->AbsPixeltoY(0) - pad->AbsPixeltoY(npixels);
450 fTextSize = dy/(pad->GetY2() - pad->GetY1());
451 }
452}
457}
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
short Style_t
Style number (short)
Definition RtypesCore.h:96
short Color_t
Color number (short)
Definition RtypesCore.h:99
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint angle
char name[80]
Definition TGX11.cxx:148
#define gROOT
Definition TROOT.h:426
R__EXTERN TStyle * gStyle
Definition TStyle.h:442
#define gPad
Text Attributes class.
Definition TAttText.h:21
virtual void ModifyOn(TVirtualPad &pad)
Change current text attributes if necessary on specified pad.
Definition TAttText.cxx:373
virtual Float_t GetTextSize() const
Return the text size.
Definition TAttText.h:39
virtual void Modify()
Change current text attributes if necessary.
Definition TAttText.cxx:364
virtual Short_t GetTextAlign() const
Return the text alignment.
Definition TAttText.h:35
virtual Font_t GetTextFont() const
Return the text font.
Definition TAttText.h:38
Color_t fTextColor
Text color.
Definition TAttText.h:27
Float_t fTextAngle
Text angle.
Definition TAttText.h:24
TAttText()
AttText default constructor.
Definition TAttText.cxx:263
virtual void SetTextColorAlpha(Color_t tcolor, Float_t talpha)
Set a transparent text color.
Definition TAttText.cxx:430
virtual Color_t GetTextColor() const
Return the text color.
Definition TAttText.h:37
virtual Float_t GetTextSizeRelative(TVirtualPad &pad) const
Return the text size in relative units.
Definition TAttText.cxx:333
virtual ~TAttText()
AttText destructor.
Definition TAttText.cxx:293
virtual Float_t GetTextAngle() const
Return the text angle.
Definition TAttText.h:36
virtual void SetTextAttributes()
Invoke the DialogCanvas Text attributes.
Definition TAttText.cxx:418
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition TAttText.h:50
virtual void SetTextSizePixels(Int_t npixels)
Set the text size in pixel.
Definition TAttText.cxx:441
Font_t fTextFont
Text font.
Definition TAttText.h:28
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:399
virtual Float_t GetTextSizePercent(Float_t size)
Return the text in percent of the pad size.
Definition TAttText.cxx:316
Short_t fTextAlign
Text alignment.
Definition TAttText.h:26
void Copy(TAttText &atttext) const
Copy this text attributes to a new TAttText.
Definition TAttText.cxx:300
virtual void ResetAttText(Option_t *toption="")
Reset this text attributes to default values.
Definition TAttText.cxx:387
virtual Float_t GetTextSizePixels(TVirtualPad &pad) const
Return the text size in pixels for specified pad.
Definition TAttText.cxx:350
Float_t fTextSize
Text size.
Definition TAttText.h:25
static TString SavePrimitiveColor(Int_t ci)
Convert color in C++ statement which can be used in SetColor directives Produced statement either inc...
Definition TColor.cxx:2556
static Int_t GetColorTransparent(Int_t color, Float_t a)
Static function: Returns the transparent color number corresponding to n.
Definition TColor.cxx:2182
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
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition TMathBase.h:249
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:197