Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TText.cxx
Go to the documentation of this file.
1// @(#)root/graf:$Id$
2// Author: Nicolas 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 "TText.h"
13
14#include "TROOT.h"
15#include "TBuffer.h"
16#include "TVirtualPad.h"
17#include "TVirtualPadPainter.h"
18#include "TCanvasImp.h"
19#include "TMath.h"
20#include "TAttMarker.h"
21#include "TPoint.h"
22
23#include <cwchar>
24#include <cstdlib>
25#include <iostream>
26
27
28
29/** \class TText
30\ingroup BasicGraphics
31
32Base class for several text objects.
33
34See TAttText for a list of text attributes or fonts,
35and also for a discussion on text speed and font quality.
36
37By default, the text is drawn in the pad coordinates system.
38One can draw in NDC coordinates [0,1] if the function SetNDC
39is called for a TText object.
40
41Example:
42Begin_Macro(source)
43{
44 TText *t = new TText(.5,.5,"Hello World !");
45 t->SetTextAlign(22);
46 t->SetTextColor(kRed+2);
47 t->SetTextFont(43);
48 t->SetTextSize(40);
49 t->SetTextAngle(45);
50 t->Draw();
51}
52End_Macro
53*/
54
55////////////////////////////////////////////////////////////////////////////////
56/// Text normal constructor.
57
58TText::TText(Double_t x, Double_t y, const char *text) : TNamed("",text), TAttText(), fWcsTitle(nullptr)
59{
60 fX = x;
61 fY = y;
62}
63
64////////////////////////////////////////////////////////////////////////////////
65/// Text normal constructor.
66
68{
69 fX = x;
70 fY = y;
71 fWcsTitle = new std::wstring(text);
72 SetName("");
74}
75
76////////////////////////////////////////////////////////////////////////////////
77/// Text default destructor.
78
80{
81 if (fWcsTitle) delete reinterpret_cast<std::wstring *>(fWcsTitle);
82}
83
84////////////////////////////////////////////////////////////////////////////////
85/// Copy constructor.
86
87TText::TText(const TText &text) : TNamed(text), TAttText(text), TAttBBox2D(text), fWcsTitle(nullptr)
88{
89 text.TText::Copy(*this);
90}
91
92////////////////////////////////////////////////////////////////////////////////
93/// Assignment operator.
94
96{
97 src.TText::Copy(*this);
98 return *this;
99}
100
101////////////////////////////////////////////////////////////////////////////////
102/// Copy this text to text.
103
104void TText::Copy(TObject &obj) const
105{
106 ((TText&)obj).fX = fX;
107 ((TText&)obj).fY = fY;
108 TNamed::Copy(obj);
109 TAttText::Copy(((TText&)obj));
110 if (((TText&)obj).fWcsTitle) {
111 if (fWcsTitle) {
112 *reinterpret_cast<std::wstring*>(((TText&)obj).fWcsTitle) = *reinterpret_cast<const std::wstring*>(fWcsTitle);
113 } else {
114 delete reinterpret_cast<std::wstring*>(((TText&)obj).fWcsTitle);
115 ((TText&)obj).fWcsTitle = nullptr;
116 }
117 } else {
118 if (fWcsTitle) {
119 ((TText&)(obj)).fWcsTitle = new std::wstring(*reinterpret_cast<const std::wstring*>(fWcsTitle));
120 }
121 }
122}
123
124////////////////////////////////////////////////////////////////////////////////
125/// Returns the text as UNICODE.
126
127const void *TText::GetWcsTitle(void) const
128{
129 if (fWcsTitle) {
130 return reinterpret_cast<std::wstring *>(fWcsTitle)->c_str();
131 } else {
132 return nullptr;
133 }
134}
135
136////////////////////////////////////////////////////////////////////////////////
137/// Compute distance from point px,py to a string.
138/// The rectangle surrounding this string is evaluated.
139/// If the point (px,py) is in the rectangle, the distance is set to zero.
140
142{
143 if (!gPad) return 9999;
144 Int_t ptx, pty;
145
146 if (TestBit(kTextNDC)) {
147 ptx = gPad->UtoAbsPixel(fX);
148 pty = gPad->VtoAbsPixel(fY);
149 } else {
150 ptx = gPad->XtoAbsPixel(gPad->XtoPad(fX));
151 pty = gPad->YtoAbsPixel(gPad->YtoPad(fY));
152 }
153
154 // Get the text control box
155 Int_t cBoxX[5], cBoxY[5];
156 GetControlBox(ptx, pty, -fTextAngle, cBoxX, cBoxY);
157 cBoxY[4] = cBoxY[0];
158 cBoxX[4] = cBoxX[0];
159
160 // Check if the point (px,py) is inside the text control box
161 if (TMath::IsInside(px, py, 5, cBoxX, cBoxY)){
162 return 0;
163 } else {
164 return 9999;
165 }
166}
167
168////////////////////////////////////////////////////////////////////////////////
169/// Draw this text with new coordinates.
170
172{
173 TText *newtext = new TText(x, y, text);
174 TAttText::Copy(*newtext);
175 newtext->SetBit(kCanDelete);
176 if (TestBit(kTextNDC)) newtext->SetNDC();
177 newtext->AppendPad();
178 return newtext;
179}
180
181////////////////////////////////////////////////////////////////////////////////
182/// Draw this text with new coordinates.
183
185{
186 TText *newtext = new TText(x, y, text);
187 TAttText::Copy(*newtext);
188 newtext->SetBit(kCanDelete);
189 if (TestBit(kTextNDC)) newtext->SetNDC();
190 newtext->AppendPad();
191 return newtext;
192}
193
194////////////////////////////////////////////////////////////////////////////////
195/// Draw this text with new coordinates in NDC.
196
198{
199 TText *newtext = DrawText(x, y, text);
200 newtext->SetNDC();
201 return newtext;
202}
203
204////////////////////////////////////////////////////////////////////////////////
205/// Draw this text with new coordinates in NDC.
206
208{
209 TText *newtext = DrawText(x, y, text);
210 newtext->SetNDC();
211 return newtext;
212}
213
215public:
216 Int_t px1 = 0, py1 = 0, pxold = 0, pyold = 0, height = 0, width = 0;
219
221 {
222 if (ndc) {
223 px1 = parent.UtoAbsPixel(x);
224 py1 = parent.VtoAbsPixel(y);
225 } else {
226 px1 = parent.XtoAbsPixel(parent.XtoPad(x));
227 py1 = parent.YtoAbsPixel(parent.YtoPad(y));
228 }
229 theta = angle;
230 }
231
232 void SelectPart(TVirtualPad &parent, Int_t part, Int_t halign, Int_t valign)
233 {
234 switch (part) {
235 case 0:
236 if (halign == 3) {
237 turn = kTRUE;
238 right = kTRUE;
239 parent.SetCursor(kRotate);
240 } else {
241 resize = kTRUE;
242 height = valign;
243 width = halign;
244 parent.SetCursor(kArrowVer);
245 }
246 break;
247 case 1: parent.SetCursor(kMove); break;
248 case 2:
249 if (halign == 3) {
250 resize = kTRUE;
251 height = valign;
252 width = halign;
253 parent.SetCursor(kArrowVer);
254 } else {
255 turn = kTRUE;
256 right = kFALSE;
257 parent.SetCursor(kRotate);
258 }
259 }
260 }
261
262 void ProcessMove(TVirtualPad &parent, TText *txt, Int_t px, Int_t py)
263 {
264 if (turn) {
265 auto norm = TMath::Sqrt(1.*(py-py1)*(py-py1)+ 1.*(px-px1)*(px-px1));
266 if (norm > 0) {
267 theta = TMath::ACos((px - px1) / norm) / TMath::Pi() * 180;
268 auto dtheta = TMath::ASin((py1 - py) / norm);
269 if (dtheta < 0)
270 theta = -theta;
271 if (right)
272 theta += 180;
273 if (theta < 0)
274 theta += 360;
275 else if (theta >= 360)
276 theta -= 360;
277 }
278 } else if (resize) {
279 Int_t valign = txt->GetTextAlign() % 10;
280 Int_t h = txt->GetTextSizePixels(parent) / 2;
281 Int_t w = h*strlen(txt->GetTitle()); // approximate width
282 auto co = TMath::Cos(txt->GetTextAngle()/180.*TMath::Pi());
283 auto si = TMath::Sin(txt->GetTextAngle()/180.*TMath::Pi());
284 Int_t ax = 0, ay = 0, bx, by;
285 if (width == 1) {
286 switch (valign) {
287 case 1:
288 ax = px1;
289 ay = py1;
290 break;
291 case 2:
292 ax = px1 + Int_t(si * h / 2);
293 ay = py1 + Int_t(co * h / 2);
294 break;
295 case 3:
296 ax = px1 + Int_t(si * h * 3 / 2);
297 ay = py1 + Int_t(co * h * 3 / 2);
298 break;
299 }
300 } else if (width == 2) {
301 switch (valign) {
302 case 1:
303 ax = px1 - Int_t(co * w / 2);
304 ay = py1 + Int_t(si * w / 2);
305 break;
306 case 2:
307 ax = px1 - Int_t(co * w / 2 + si * h / 2);
308 ay = py1 + Int_t(si * w / 2 + co * h / 2);
309 break;
310 case 3:
311 ax = px1 - Int_t(co * w / 2 + si * h * 3 / 2);
312 ay = py1 + Int_t(si * w / 2 + co * h * 3 / 2);
313 break;
314 }
315 } else if (width == 3) {
316 switch (valign) {
317 case 1:
318 ax = px1 - Int_t(co * w);
319 ay = py1 + Int_t(si * w);
320 break;
321 case 2:
322 ax = px1 - Int_t(co * w + si * h / 2);
323 ay = py1 + Int_t(si * w + co * h / 2);
324 break;
325 case 3:
326 ax = px1 - Int_t(co * w + si * h * 3 / 2);
327 ay = py1 + Int_t(si * w + co * h * 3 / 2);
328 break;
329 }
330 }
331 if (height == 3) {
332 bx = ax - Int_t(si*h);
333 by = ay - Int_t(co*h);
334 } else {
335 bx = ax;
336 by = ay;
337 }
338 Int_t cx = bx+Int_t(co*w);
339 Int_t cy = by-Int_t(si*w);
340 Double_t lambda = Double_t(((px-bx)*(cx-bx)+(py-by)*(cy-by)))/Double_t(((cx-bx)*(cx-bx)+(cy-by)*(cy-by)));
341 Double_t x2 = Double_t(px) - lambda*Double_t(cx-bx)-Double_t(bx);
342 Double_t y2 = Double_t(py) - lambda*Double_t(cy-by)-Double_t(by);
343 Int_t Size = Int_t(TMath::Sqrt(x2*x2+y2*y2)*2);
344 if (Size < 4) Size = 4;
345
346 txt->SetTextSizePixels(Size);
347 } else {
348 px1 += px - pxold; pxold = px;
349 py1 += py - pyold; pyold = py;
350 }
351 }
352
353 void PaintControlBox(TVirtualPad &parent, Int_t *cBoxX, Int_t *cBoxY, Bool_t show_corner)
354 {
355 Double_t xx[5], yy[5];
356 for(Int_t p = 0; p < 4; ++p) {
357 xx[p] = parent.AbsPixeltoX(cBoxX[p]);
358 yy[p] = parent.AbsPixeltoY(cBoxY[p]);
359 }
360 xx[4] = xx[0];
361 yy[4] = yy[0];
362 parent.PaintPolyLine(5, xx, yy, "itextbox");
363
364 if (show_corner) {
365 Double_t mX = parent.AbsPixeltoX(px1);
366 Double_t mY = parent.AbsPixeltoY(py1);
367 parent.PaintPolyMarker(1, &mX, &mY, "iftextcorner");
368 }
369 }
370
371};
372
373////////////////////////////////////////////////////////////////////////////////
374/// Execute action corresponding to one event.
375///
376/// This member function must be implemented to realize the action
377/// corresponding to the mouse click on the object in the window
378
380{
381 if (!gPad || !gPad->IsEditable())
382 return;
383
384 auto &parent = *gPad;
385
386 Short_t halign = GetTextAlign() / 10;
387 Short_t valign = GetTextAlign() % 10;
388 Bool_t opaque = parent.OpaqueMoving();
389
390 auto inter = parent.GetInteractive<TTextInteractive>(this);
391
392 switch (event) {
393
394 case kArrowKeyPress:
395 case kButton1Down:
396 case kMouseMotion: {
397 inter = parent.MakeInteractive<TTextInteractive>(this, parent, TestBit(kTextNDC), GetX(), GetY(), GetTextAngle());
398 inter->pxold = px;
399 inter->pyold = py;
400 auto co = TMath::Cos(GetTextAngle()/180.*TMath::Pi());
401 auto si = TMath::Sin(GetTextAngle()/180.*TMath::Pi());
402 Int_t cBoxX[4], cBoxY[4];
403 GetControlBox(inter->px1, inter->py1, -inter->theta, cBoxX, cBoxY);
404 auto div = ((cBoxX[3]-cBoxX[0])*co-(cBoxY[3]-cBoxY[0])*si);
405 Int_t part = TMath::Abs(div) < 1e-8 ? 0 : (Int_t)(3 * ((px - cBoxX[0]) * co - (py - cBoxY[0]) * si) / div);
406 inter->SelectPart(parent, part, halign, valign);
407 break;
408 }
409
410 case kArrowKeyRelease:
411 case kButton1Motion:
412 if (!inter)
413 return;
414
415 inter->ProcessMove(parent, this, px, py);
416
417 if (opaque) {
418 SetX(GetXCoord(inter->px1, TestBit(kTextNDC), kTRUE));
419 SetY(GetYCoord(inter->py1, TestBit(kTextNDC), kTRUE));
420 SetTextAngle(inter->theta);
421 if (inter->resize)
422 parent.ShowGuidelines(this, event, 't', false);
423 if (!inter->resize && !inter->turn)
424 parent.ShowGuidelines(this, event, 'i', true);
425 parent.Modified();
426 } else {
427 Int_t cBoxX[4], cBoxY[4];
428 GetControlBox(inter->px1, inter->py1, -inter->theta, cBoxX, cBoxY);
429 inter->PaintControlBox(parent, cBoxX, cBoxY, (halign != 2) || (valign != 2));
430 }
431 parent.UpdateAsync();
432 break;
433
434 case kButton1Up:
435 if (opaque) {
436 parent.ShowGuidelines(this, event);
437 } else {
438 SetX(GetXCoord(inter->px1, TestBit(kTextNDC), kTRUE));
439 SetY(GetYCoord(inter->py1, TestBit(kTextNDC), kTRUE));
440 SetTextAngle(inter->theta);
441 }
442 parent.Modified(kTRUE);
443 parent.FreeInteractive(this);
444 break;
445
446 case kButton1Locate:
447 ExecuteEvent(kButton1Down, px, py);
448
449 while (1) {
450 px = py = 0;
451 event = parent.GetCanvasImp()->RequestLocator(px, py);
452
454
455 if (event != -1) { // button is released
456 ExecuteEvent(kButton1Up, px, py);
457 return;
458 }
459 }
460 }
461}
462
463////////////////////////////////////////////////////////////////////////////////
464/// Return the text control box. The text position coordinates is (x,y) and
465/// the text angle is theta. The control box coordinates are returned in cBoxX
466/// and cBoxY.
467
469 Int_t cBoxX[4], Int_t cBoxY[4])
470{
471 Short_t halign = fTextAlign/10; // horizontal alignment
472 Short_t valign = fTextAlign - 10*halign; // vertical alignment
473 UInt_t cBoxW = 0, cBoxH = 0; // control box width and heigh
474 UInt_t Dx = 0, Dy = 0; // delta along x and y to align the box
475
476 if (gPad) {
478 auto pp = gPad->GetPainter();
479 if (pp)
480 pp->GetTextExtent(GetTextFont(), tsize, cBoxW, cBoxH, GetTitle());
481 }
482
483 // compute the translations (Dx, Dy) required by the alignments
484 switch (halign) {
485 case 1 : Dx = 0 ; break;
486 case 2 : Dx = cBoxW/2; break;
487 case 3 : Dx = cBoxW ; break;
488 }
489 switch (valign) {
490 case 1 : Dy = 0 ; break;
491 case 2 : Dy = cBoxH/2; break;
492 case 3 : Dy = cBoxH ; break;
493 }
494
495 // compute the control box coordinates before rotation
496 cBoxX[0] = x-Dx;
497 cBoxY[0] = y+Dy;
498 cBoxX[1] = x-Dx;
499 cBoxY[1] = y-cBoxH+Dy;
500 cBoxX[2] = x+cBoxW-Dx;
501 cBoxY[2] = y-cBoxH+Dy;
502 cBoxX[3] = x+cBoxW-Dx;
503 cBoxY[3] = y+Dy;
504
505 // rotate the control box if needed
506 if (theta) {
507 Double_t cosTheta = TMath::Cos(theta*0.017453293);
508 Double_t sinTheta = TMath::Sin(theta*0.017453293);
509 for (int i=0; i<4 ; i++) {
510 Int_t hcBoxX = cBoxX[i];
511 Int_t hcBoxY = cBoxY[i];
512 cBoxX[i] = (Int_t)((hcBoxX-x)*cosTheta-(hcBoxY-y)*sinTheta+x);
513 cBoxY[i] = (Int_t)((hcBoxX-x)*sinTheta+(hcBoxY-y)*cosTheta+y);
514 }
515 }
516}
517
518////////////////////////////////////////////////////////////////////////////////
519/// Return text size in pixels. By default the size returned does not take
520/// into account the text angle (angle = kFALSE). If angle is set to kTRUE
521/// w and h take the angle into account.
522
524{
525 const char *text = GetTitle();
526 if (!text[0]) {
527 w = h = 0;
528 return;
529 }
530
531 if (!gPad)
532 return;
533 if (angle) {
534 Int_t cBoxX[4], cBoxY[4];
535 Int_t ptx, pty;
536 if (TestBit(kTextNDC)) {
537 ptx = gPad->UtoAbsPixel(fX);
538 pty = gPad->VtoAbsPixel(fY);
539 } else {
540 ptx = gPad->XtoAbsPixel(gPad->XtoPad(fX));
541 pty = gPad->YtoAbsPixel(gPad->YtoPad(fY));
542 }
543 GetControlBox(ptx, pty, fTextAngle, cBoxX, cBoxY);
544 Int_t x1 = cBoxX[0];
545 Int_t x2 = cBoxX[0];
546 Int_t y1 = cBoxY[0];
547 Int_t y2 = cBoxY[0];
548 for (Int_t i=1; i<4; i++) {
549 if (cBoxX[i] < x1) x1 = cBoxX[i];
550 if (cBoxX[i] > x2) x2 = cBoxX[i];
551 if (cBoxY[i] < y1) y1 = cBoxY[i];
552 if (cBoxY[i] > y2) y2 = cBoxY[i];
553 }
554 w = x2 - x1;
555 h = y2 - y1;
556 } else {
558 auto pp = gPad->GetPainter();
559 if (pp)
560 pp->GetTextExtent(GetTextFont(), tsize, w, h, GetTitle());
561 }
562}
563
564////////////////////////////////////////////////////////////////////////////////
565/// Return text ascent and descent for string text
566/// - in a return total text ascent
567/// - in d return text descent
568
569void TText::GetTextAscentDescent(UInt_t &a, UInt_t &d, const char *text) const
570{
571 if (!gPad)
572 return;
573
575
576 auto pp = gPad->GetPainter();
577 if (pp)
578 pp->GetTextAscentDescent(GetTextFont(), tsize, a, d, text);
579}
580
581
582////////////////////////////////////////////////////////////////////////////////
583/// Return text ascent and descent for string text
584/// - in a return total text ascent
585/// - in d return text descent
586
587void TText::GetTextAscentDescent(UInt_t &a, UInt_t &d, const wchar_t *text) const
588{
589 if (!gPad)
590 return;
591
593
594 auto pp = gPad->GetPainter();
595 if (pp)
596 pp->GetTextAscentDescent(GetTextFont(), tsize, a, d, text);
597}
598
599////////////////////////////////////////////////////////////////////////////////
600/// Return text extent for string text
601/// - in w return total text width
602/// - in h return text height
603
604void TText::GetTextExtent(UInt_t &w, UInt_t &h, const char *text) const
605{
606 if (!gPad)
607 return;
608
610
611 auto pp = gPad->GetPainter();
612 if (pp)
613 pp->GetTextExtent(GetTextFont(), tsize, w, h, text);
614}
615
616////////////////////////////////////////////////////////////////////////////////
617/// Return text advance for string text
618/// if kern is true (default) kerning is taken into account. If it is false
619/// the kerning is not taken into account.
620
621void TText::GetTextAdvance(UInt_t &a, const char *text, const Bool_t kern) const
622{
623 if (!gPad)
624 return;
625
627
628 auto pp = gPad->GetPainter();
629 if (pp)
630 a = pp->GetTextAdvance(GetTextFont(), tsize, text, kern);
631}
632
633////////////////////////////////////////////////////////////////////////////////
634/// Return text extent for string text
635/// - in w return total text width
636/// - in h return text height
637
638void TText::GetTextExtent(UInt_t &w, UInt_t &h, const wchar_t *text) const
639{
640 if (!gPad)
641 return;
642
644
645 auto pp = gPad->GetPainter();
646 if (pp)
647 pp->GetTextExtent(GetTextFont(), tsize, w, h, text);
648}
649
650////////////////////////////////////////////////////////////////////////////////
651/// List this text with its attributes.
652
653void TText::ls(Option_t *) const
654{
656 printf("Text X=%f Y=%f Text=%s\n",fX,fY,GetTitle());
657}
658
659////////////////////////////////////////////////////////////////////////////////
660/// Paint this text with its current attributes.
661
663{
664 if (!gPad) return;
665 TAttText::Modify(); //Change text attributes only if necessary
666 if (TestBit(kTextNDC)) gPad->PaintTextNDC(fX,fY,GetTitle());
667 else gPad->PaintText(gPad->XtoPad(fX),gPad->YtoPad(fY),GetTitle());
668}
669
670////////////////////////////////////////////////////////////////////////////////
671/// Paint the text control box. (x,y) are the coordinates where the control
672/// box should be painted and theta is the angle of the box.
673
675{
676 Int_t cBoxX[4], cBoxY[4];
677 Short_t halign = fTextAlign/10; // horizontal alignment
678 Short_t valign = fTextAlign - 10*halign; // vertical alignment
679
680 GetControlBox(x, y, theta, cBoxX, cBoxY);
681
682 auto pp = gPad->GetPainter();
683 if (!pp)
684 return;
685
686 // Draw the text control box outline
687 pp->SetAttLine({(Style_t)1, 1, 1});
688 for (int p1 = 0; p1 < 4; ++p1) {
689 int p2 = (p1 + 1) % 4;
690 pp->DrawLine(gPad->AbsPixeltoX(cBoxX[p1]),
691 gPad->AbsPixeltoY(cBoxY[p1]),
692 gPad->AbsPixeltoX(cBoxX[p2]),
693 gPad->AbsPixeltoY(cBoxY[p2]));
694 }
695
696 // Draw a symbol at the text starting point
697 TPoint p;
698 Int_t ix = 0, iy = 0;
699 switch (halign) {
700 case 1 :
701 switch (valign) {
702 case 1 : ix = 0 ; iy = 0 ; break;
703 case 2 : ix = 0 ; iy = 1 ; break;
704 case 3 : ix = 1 ; iy = 1 ; break;
705 }
706 break;
707 case 2 :
708 switch (valign) {
709 case 1 : ix = 0 ; iy = 3 ; break;
710 case 2 : ix = 0 ; iy = 2 ; break;
711 case 3 : ix = 1 ; iy = 2 ; break;
712 }
713 break;
714 case 3 :
715 switch (valign) {
716 case 1 : ix = 3 ; iy = 3 ; break;
717 case 2 : ix = 2 ; iy = 3 ; break;
718 case 3 : ix = 2 ; iy = 2 ; break;
719 }
720 break;
721 }
722 Double_t mX = gPad->AbsPixeltoX((cBoxX[ix]+cBoxX[iy])/2);
723 Double_t mY = gPad->AbsPixeltoY((cBoxY[ix]+cBoxY[iy])/2);
724 pp->SetAttMarker({(Color_t)1, 24, 0.7});
725 pp->DrawPolyMarker(1, &mX, &mY);
726}
727
728////////////////////////////////////////////////////////////////////////////////
729/// Draw this text with new coordinates.
730
732{
733 TAttText::Modify(); //Change text attributes only if necessary
734 if (gPad) gPad->PaintText(x,y,text);
735}
736
737////////////////////////////////////////////////////////////////////////////////
738/// Draw this text with new coordinates.
739
740void TText::PaintText(Double_t x, Double_t y, const wchar_t *text)
741{
742 TAttText::Modify(); //Change text attributes only if necessary
743 if (gPad) gPad->PaintText(x,y,text);
744}
745
746////////////////////////////////////////////////////////////////////////////////
747/// Draw this text with new coordinates in NDC.
748
750{
751 TAttText::Modify(); //Change text attributes only if necessary
752 if (gPad) gPad->PaintTextNDC(u,v,text);
753}
754
755////////////////////////////////////////////////////////////////////////////////
756/// Draw this text with new coordinates in NDC.
757
759{
760 TAttText::Modify(); //Change text attributes only if necessary
761 if (gPad) gPad->PaintTextNDC(u,v,text);
762}
763
764////////////////////////////////////////////////////////////////////////////////
765/// Dump this text with its attributes.
766
768{
769 printf("Text X=%f Y=%f Text=%s Font=%d Size=%f",fX,fY,GetTitle(),GetTextFont(),GetTextSize());
770 if (GetTextColor() != 1 ) printf(" Color=%d",GetTextColor());
771 if (GetTextAlign() != 10) printf(" Align=%d",GetTextAlign());
772 if (GetTextAngle() != 0 ) printf(" Angle=%f",GetTextAngle());
773 printf("\n");
774}
775
776////////////////////////////////////////////////////////////////////////////////
777/// Save primitive as a C++ statement(s) on output stream out
778
779void TText::SavePrimitive(std::ostream &out, Option_t *option)
780{
782 out, Class(), "text",
783 TString::Format("%g, %g, \"%s\"", fX, fY, TString(GetTitle()).ReplaceSpecialCppChars().Data()), kFALSE);
784
785 SaveTextAttributes(out, "text", 11, 0, 1, 62, 0.05);
786
787 if (TestBit(kTextNDC))
788 out << " text->SetNDC();\n";
789
790 SavePrimitiveDraw(out, "text", option);
791}
792
793////////////////////////////////////////////////////////////////////////////////
794/// Set NDC mode on if isNDC = kTRUE, off otherwise
795
797{
798 SetBit(kTextNDC, isNDC);
799}
800
801////////////////////////////////////////////////////////////////////////////////
802/// Change (i.e. set) the title of the TNamed.
803
804void TText::SetMbTitle(const wchar_t *title)
805{
806 char *mb_title = new char[MB_CUR_MAX * wcslen(title) + 1]();
807 char *p = mb_title;
808 size_t length = wcslen(title);
809 for (size_t i = 0; i < length; i++) {
810 const int n = wctomb(p, title[i]);
811 if (n >= 0) p += n;
812 }
813 fTitle = mb_title;
814 delete [] mb_title;
815 if (gPad && TestBit(kMustCleanup)) gPad->Modified();
816}
817
818////////////////////////////////////////////////////////////////////////////////
819/// Stream an object of class TText.
820
822{
823 if (R__b.IsReading()) {
824 UInt_t R__s, R__c;
825 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
826 if (R__v > 1) {
827 R__b.ReadClassBuffer(TText::Class(), this, R__v, R__s, R__c);
828 return;
829 }
830 //====process old versions before automatic schema evolution
831 TNamed::Streamer(R__b);
832 TAttText::Streamer(R__b);
833 Float_t x,y;
834 R__b >> x; fX = x;
835 R__b >> y; fY = y;
836 //====end of old versions
837
838 } else {
839 R__b.WriteClassBuffer(TText::Class(),this);
840 }
841}
842////////////////////////////////////////////////////////////////////////////////
843/// Return the "bounding Box" of the Box
844
846{
847 Rectangle_t bbox{0, 0, 0, 0};
848 if (gPad) {
849 UInt_t w, h;
850 Int_t Dx = 0, Dy = 0;
851 GetBoundingBox(w, h, false);
852
853 Short_t halign = fTextAlign / 10;
854 Short_t valign = fTextAlign - 10 * halign;
855
856 switch (halign) {
857 case 1: Dx = 0; break;
858 case 2: Dx = w / 2; break;
859 case 3: Dx = w; break;
860 }
861
862 switch (valign) {
863 case 1: Dy = h; break;
864 case 2: Dy = h / 2; break;
865 case 3: Dy = 0; break;
866 }
867 if (TestBit(kTextNDC)) {
868 bbox.fX = gPad->UtoPixel(GetX()) - Dx;
869 bbox.fY = gPad->VtoPixel(GetY()) - Dy;
870 } else {
871 bbox.fX = gPad->XtoPixel(gPad->XtoPad(GetX())) - Dx;
872 bbox.fY = gPad->YtoPixel(gPad->YtoPad(GetY())) - Dy;
873 }
874 bbox.fWidth = w;
875 bbox.fHeight = h;
876 }
877 return bbox;
878}
879
880////////////////////////////////////////////////////////////////////////////////
881/// Return the point given by Alignment as 'center'
882
884{
885 TPoint p(0, 0);
886 if (gPad) {
887 if (TestBit(kTextNDC)) {
888 p.SetX(gPad->UtoPixel(GetX()));
889 p.SetY(gPad->VtoPixel(GetY()));
890 } else {
891 p.SetX(gPad->XtoPixel(gPad->XtoPad(GetX())));
892 p.SetY(gPad->YtoPixel(gPad->YtoPad(GetY())));
893 }
894 }
895 return p;
896}
897
898////////////////////////////////////////////////////////////////////////////////
899/// Set X coordinate of the point given by Alignment as 'center'
900
905
906////////////////////////////////////////////////////////////////////////////////
907/// Set Y coordinate of the point given by Alignment as 'center'
908
@ kMouseMotion
Definition Buttons.h:23
@ kArrowKeyRelease
Definition Buttons.h:21
@ kButton1Motion
Definition Buttons.h:20
@ kButton1Up
Definition Buttons.h:19
@ kArrowKeyPress
Definition Buttons.h:21
@ kButton1Down
Definition Buttons.h:17
@ kButton1Locate
Definition Buttons.h:22
@ kArrowVer
Definition GuiTypes.h:375
@ kMove
Definition GuiTypes.h:375
@ kRotate
Definition GuiTypes.h:375
#define d(i)
Definition RSha256.hxx:102
#define a(i)
Definition RSha256.hxx:99
#define h(i)
Definition RSha256.hxx:106
#define e(i)
Definition RSha256.hxx:103
cudaEvent_t event
short Style_t
Style number (short)
Definition RtypesCore.h:97
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:60
short Color_t
Color number (short)
Definition RtypesCore.h:100
short Version_t
Class version identifier (short)
Definition RtypesCore.h:80
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:72
short Short_t
Signed Short integer 2 bytes (short)
Definition RtypesCore.h:54
constexpr Bool_t kFALSE
Definition RtypesCore.h:109
double Double_t
Double 8 bytes.
Definition RtypesCore.h:74
constexpr Bool_t kTRUE
Definition RtypesCore.h:108
const char Option_t
Option string (const char)
Definition RtypesCore.h:81
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h length
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
Option_t Option_t TPoint TPoint angle
Option_t Option_t TPoint TPoint const char y2
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t src
Option_t Option_t TPoint TPoint DrawText
Option_t Option_t TPoint TPoint const char text
Option_t Option_t TPoint TPoint const char y1
#define gPad
Abstract base class for elements drawn in the editor.
Definition TAttBBox2D.h:19
Double_t GetYCoord(const Int_t y, Bool_t is_ndc=kFALSE, Bool_t is_absolute=kFALSE)
Can return ndc or normal values Also one can specify to use absolute coordinates for input parameter ...
Double_t GetXCoord(const Int_t x, Bool_t is_ndc=kFALSE, Bool_t is_absolute=kFALSE)
Return user X coordinate for pixel X value Can return ndc or normal values Also one can specify to us...
Text Attributes class.
Definition TAttText.h:21
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
Float_t fTextAngle
Text angle.
Definition TAttText.h:24
virtual Color_t GetTextColor() const
Return the text color.
Definition TAttText.h:37
virtual void Streamer(TBuffer &)
virtual void SetTextAngle(Float_t tangle=0)
Set the text angle.
Definition TAttText.h:49
virtual Float_t GetTextAngle() const
Return the text angle.
Definition TAttText.h:36
virtual void SetTextSizePixels(Int_t npixels)
Set the text size in pixel.
Definition TAttText.cxx:441
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
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 Float_t GetTextSizePixels(TVirtualPad &pad) const
Return the text size in pixels for specified pad.
Definition TAttText.cxx:350
Buffer base class used for serializing objects.
Definition TBuffer.h:43
virtual Version_t ReadVersion(UInt_t *start=nullptr, UInt_t *bcnt=nullptr, const TClass *cl=nullptr)=0
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=nullptr)=0
Bool_t IsReading() const
Definition TBuffer.h:86
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
void Copy(TObject &named) const override
Copy this to obj.
Definition TNamed.cxx:93
void Streamer(TBuffer &) override
Stream an object of class TObject.
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
TString fTitle
Definition TNamed.h:33
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:149
Mother of all ROOT objects.
Definition TObject.h:42
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:204
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition TObject.cxx:203
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:886
static void SavePrimitiveDraw(std::ostream &out, const char *variable_name, Option_t *option=nullptr)
Save invocation of primitive Draw() method Skipped if option contains "nodraw" string.
Definition TObject.cxx:844
static void SavePrimitiveConstructor(std::ostream &out, TClass *cl, const char *variable_name, const char *constructor_agrs="", Bool_t empty_line=kTRUE)
Save object constructor in the output stream "out".
Definition TObject.cxx:776
@ kCanDelete
if object in a list can be deleted
Definition TObject.h:71
@ kMustCleanup
if object destructor must call RecursiveRemove()
Definition TObject.h:73
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition TROOT.cxx:3067
Basic string class.
Definition TString.h:137
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2460
void SelectPart(TVirtualPad &parent, Int_t part, Int_t halign, Int_t valign)
Definition TText.cxx:232
void PaintControlBox(TVirtualPad &parent, Int_t *cBoxX, Int_t *cBoxY, Bool_t show_corner)
Definition TText.cxx:353
void ProcessMove(TVirtualPad &parent, TText *txt, Int_t px, Int_t py)
Definition TText.cxx:262
Double_t theta
Definition TText.cxx:218
TTextInteractive(TVirtualPad &parent, Bool_t ndc, Double_t x, Double_t y, Double_t angle)
Definition TText.cxx:220
Base class for several text objects.
Definition TText.h:22
@ kTextNDC
The text position is in the NDC space.
Definition TText.h:32
Double_t fY
Y position of text (left,center,etc..)
Definition TText.h:26
const void * GetWcsTitle(void) const
Returns the text as UNICODE.
Definition TText.cxx:127
Rectangle_t GetBBox() override
Return the "bounding Box" of the Box.
Definition TText.cxx:845
void Copy(TObject &text) const override
Copy this text to text.
Definition TText.cxx:104
TText & operator=(const TText &src)
Assignment operator.
Definition TText.cxx:95
virtual void PaintControlBox(Int_t x, Int_t y, Double_t theta) R__DEPRECATED(6
Paint the text control box.
Definition TText.cxx:674
Double_t GetX() const
Definition TText.h:53
virtual void GetBoundingBox(UInt_t &w, UInt_t &h, Bool_t angle=kFALSE)
Return text size in pixels.
Definition TText.cxx:523
virtual void SetY(Double_t y)
Definition TText.h:77
virtual TText * DrawText(Double_t x, Double_t y, const char *text)
Draw this text with new coordinates.
Definition TText.cxx:171
void * fWcsTitle
!Used by TMathText
Definition TText.h:27
Double_t fX
X position of text (left,center,etc..)
Definition TText.h:25
~TText() override
Text default destructor.
Definition TText.cxx:79
virtual void PaintTextNDC(Double_t u, Double_t v, const char *text)
Draw this text with new coordinates in NDC.
Definition TText.cxx:749
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Definition TText.cxx:779
void ls(Option_t *option="") const override
List this text with its attributes.
Definition TText.cxx:653
virtual void PaintText(Double_t x, Double_t y, const char *text)
Draw this text with new coordinates.
Definition TText.cxx:731
TText()
Definition TText.h:35
virtual void GetTextExtent(UInt_t &w, UInt_t &h, const char *text) const
Return text extent for string text.
Definition TText.cxx:604
virtual void GetTextAscentDescent(UInt_t &a, UInt_t &d, const char *text) const
Return text ascent and descent for string text.
Definition TText.cxx:569
Int_t DistancetoPrimitive(Int_t px, Int_t py) override
Compute distance from point px,py to a string.
Definition TText.cxx:141
TPoint GetBBoxCenter() override
Return the point given by Alignment as 'center'.
Definition TText.cxx:883
virtual void SetX(Double_t x)
Definition TText.h:76
virtual void GetTextAdvance(UInt_t &a, const char *text, const Bool_t kern=kTRUE) const
Return text advance for string text if kern is true (default) kerning is taken into account.
Definition TText.cxx:621
void Paint(Option_t *option="") override
Paint this text with its current attributes.
Definition TText.cxx:662
void SetBBoxCenterY(const Int_t y) override
Set Y coordinate of the point given by Alignment as 'center'.
Definition TText.cxx:909
virtual void SetNDC(Bool_t isNDC=kTRUE)
Set NDC mode on if isNDC = kTRUE, off otherwise.
Definition TText.cxx:796
void Print(Option_t *option="") const override
Dump this text with its attributes.
Definition TText.cxx:767
virtual void GetControlBox(Int_t x, Int_t y, Double_t theta, Int_t cBoxX[4], Int_t cBoxY[4])
Return the text control box.
Definition TText.cxx:468
void SetBBoxCenterX(const Int_t x) override
Set X coordinate of the point given by Alignment as 'center'.
Definition TText.cxx:901
Double_t GetY() const
Definition TText.h:61
void Streamer(TBuffer &) override
Stream an object of class TText.
Definition TText.cxx:821
virtual TText * DrawTextNDC(Double_t x, Double_t y, const char *text)
Draw this text with new coordinates in NDC.
Definition TText.cxx:197
static TClass * Class()
virtual void SetMbTitle(const wchar_t *title=L"")
Change (i.e. set) the title of the TNamed.
Definition TText.cxx:804
void ExecuteEvent(Int_t event, Int_t px, Int_t py) override
Execute action corresponding to one event.
Definition TText.cxx:379
Helper class to store interactive parameters for individual objects Should be used via MakeInteractiv...
Definition TVirtualPad.h:72
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:53
virtual Int_t YtoAbsPixel(Double_t y) const =0
virtual void PaintPolyMarker(Int_t n, Float_t *x, Float_t *y, Option_t *option="")=0
virtual Double_t AbsPixeltoX(Double_t px)=0
virtual Int_t XtoAbsPixel(Double_t x) const =0
virtual Int_t UtoAbsPixel(Double_t u) const =0
virtual Double_t YtoPad(Double_t y) const =0
virtual void PaintPolyLine(Int_t n, Float_t *x, Float_t *y, Option_t *option="")=0
virtual Double_t XtoPad(Double_t x) const =0
virtual Int_t VtoAbsPixel(Double_t v) const =0
virtual Double_t AbsPixeltoY(Double_t py)=0
virtual void SetCursor(ECursor cursor)=0
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
Double_t ACos(Double_t)
Returns the principal value of the arc cosine of x, expressed in radians.
Definition TMath.h:645
Bool_t IsInside(T xp, T yp, Int_t np, T *x, T *y)
Function which returns kTRUE if point xp,yp lies inside the polygon defined by the np points in array...
Definition TMath.h:1326
Double_t ASin(Double_t)
Returns the principal value of the arc sine of x, expressed in radians.
Definition TMath.h:637
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:675
Double_t Cos(Double_t)
Returns the cosine of an angle of x radians.
Definition TMath.h:607
constexpr Double_t Pi()
Definition TMath.h:40
Double_t Sin(Double_t)
Returns the sine of an angle of x radians.
Definition TMath.h:601
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:122
Rectangle structure (maps to the X11 XRectangle structure)
Definition GuiTypes.h:362