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