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