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 <ft2build.h>
18#include FT_FREETYPE_H
19#include FT_GLYPH_H
20#include "TTF.h"
21#include "TVirtualX.h"
22#include "TMath.h"
23#include "TPoint.h"
24
25#include <cwchar>
26#include <cstdlib>
27#include <iostream>
28
29
30
31/** \class TText
32\ingroup BasicGraphics
33
34Base class for several text objects.
35
36See TAttText for a list of text attributes or fonts,
37and also for a discussion on text speed and font quality.
38
39By default, the text is drawn in the pad coordinates system.
40One can draw in NDC coordinates [0,1] if the function SetNDC
41is called for a TText object.
42
43Example:
44Begin_Macro(source)
45{
46 TText *t = new TText(.5,.5,"Hello World !");
47 t->SetTextAlign(22);
48 t->SetTextColor(kRed+2);
49 t->SetTextFont(43);
50 t->SetTextSize(40);
51 t->SetTextAngle(45);
52 t->Draw();
53}
54End_Macro
55*/
56
57////////////////////////////////////////////////////////////////////////////////
58/// Text normal constructor.
59
60TText::TText(Double_t x, Double_t y, const char *text) : TNamed("",text), TAttText(), fWcsTitle(nullptr)
61{
62 fX = x;
63 fY = y;
64}
65
66////////////////////////////////////////////////////////////////////////////////
67/// Text normal constructor.
68
70{
71 fX = x;
72 fY = y;
73 fWcsTitle = new std::wstring(text);
74 SetName("");
76}
77
78////////////////////////////////////////////////////////////////////////////////
79/// Text default destructor.
80
82{
83 if (fWcsTitle) delete reinterpret_cast<std::wstring *>(fWcsTitle);
84}
85
86////////////////////////////////////////////////////////////////////////////////
87/// Copy constructor.
88
89TText::TText(const TText &text) : TNamed(text), TAttText(text), TAttBBox2D(text), fWcsTitle(nullptr)
90{
91 text.TText::Copy(*this);
92}
93
94////////////////////////////////////////////////////////////////////////////////
95/// Assignment operator.
96
98{
99 src.TText::Copy(*this);
100 return *this;
101}
102
103////////////////////////////////////////////////////////////////////////////////
104/// Copy this text to text.
105
106void TText::Copy(TObject &obj) const
107{
108 ((TText&)obj).fX = fX;
109 ((TText&)obj).fY = fY;
110 TNamed::Copy(obj);
111 TAttText::Copy(((TText&)obj));
112 if (((TText&)obj).fWcsTitle) {
113 if (fWcsTitle) {
114 *reinterpret_cast<std::wstring*>(((TText&)obj).fWcsTitle) = *reinterpret_cast<const std::wstring*>(fWcsTitle);
115 } else {
116 delete reinterpret_cast<std::wstring*>(((TText&)obj).fWcsTitle);
117 ((TText&)obj).fWcsTitle = nullptr;
118 }
119 } else {
120 if (fWcsTitle) {
121 ((TText&)(obj)).fWcsTitle = new std::wstring(*reinterpret_cast<const std::wstring*>(fWcsTitle));
122 }
123 }
124}
125
126////////////////////////////////////////////////////////////////////////////////
127/// Returns the text as UNICODE.
128
129const void *TText::GetWcsTitle(void) const
130{
131 if (fWcsTitle) {
132 return reinterpret_cast<std::wstring *>(fWcsTitle)->c_str();
133 } else {
134 return nullptr;
135 }
136}
137
138////////////////////////////////////////////////////////////////////////////////
139/// Compute distance from point px,py to a string.
140/// The rectangle surrounding this string is evaluated.
141/// If the point (px,py) is in the rectangle, the distance is set to zero.
142
144{
145 if (!gPad) return 9999;
146 Int_t ptx, pty;
147
148 TAttText::Modify(); // change text attributes only if necessary
149
150 if (TestBit(kTextNDC)) {
151 ptx = gPad->UtoPixel(fX);
152 pty = gPad->VtoPixel(fY);
153 } else {
154 ptx = gPad->XtoAbsPixel(gPad->XtoPad(fX));
155 pty = gPad->YtoAbsPixel(gPad->YtoPad(fY));
156 }
157
158 // Get the text control box
159 Int_t cBoxX[5], cBoxY[5];
161 cBoxY[4] = cBoxY[0];
162 cBoxX[4] = cBoxX[0];
163
164 // Check if the point (px,py) is inside the text control box
165 if (TMath::IsInside(px, py, 5, cBoxX, cBoxY)){
166 return 0;
167 } else {
168 return 9999;
169 }
170}
171
172////////////////////////////////////////////////////////////////////////////////
173/// Draw this text with new coordinates.
174
176{
177 TText *newtext = new TText(x, y, text);
179 newtext->SetBit(kCanDelete);
180 if (TestBit(kTextNDC)) newtext->SetNDC();
181 newtext->AppendPad();
182 return newtext;
183}
184
185////////////////////////////////////////////////////////////////////////////////
186/// Draw this text with new coordinates.
187
189{
190 TText *newtext = new TText(x, y, text);
192 newtext->SetBit(kCanDelete);
193 if (TestBit(kTextNDC)) newtext->SetNDC();
194 newtext->AppendPad();
195 return newtext;
196}
197
198////////////////////////////////////////////////////////////////////////////////
199/// Draw this text with new coordinates in NDC.
200
202{
204 newtext->SetNDC();
205 return newtext;
206}
207
208////////////////////////////////////////////////////////////////////////////////
209/// Draw this text with new coordinates in NDC.
210
212{
214 newtext->SetNDC();
215 return newtext;
216}
217
218////////////////////////////////////////////////////////////////////////////////
219/// Execute action corresponding to one event.
220///
221/// This member function must be implemented to realize the action
222/// corresponding to the mouse click on the object in the window
223
225{
226 if (!gPad) return;
227
228 static Int_t px1, py1, pxold, pyold, Size, height, width;
229 static Bool_t resize,turn;
230 Int_t dx, dy;
231 const char *text = GetTitle();
232 Int_t len = strlen(text);
233 Double_t sizetowin = gPad->GetAbsHNDC()*Double_t(gPad->GetWh());
235 Int_t h = Int_t(fh/2);
236 Int_t w = h*len;
240 static Bool_t right, ndcsav;
241 static Double_t theta;
242 Int_t ax, ay, bx, by, cx, cy;
243 ax = ay = 0;
244 Double_t lambda, x2,y2;
246 Int_t cBoxX[4], cBoxY[4], part;
247 Double_t div = 0;
248 Bool_t opaque = gPad->OpaqueMoving();
249
250 if (!gPad->IsEditable()) return;
251 switch (event) {
252
253 case kArrowKeyPress:
254 case kButton1Down:
256 // No break !!!
257
258 case kMouseMotion:
259 if (TestBit(kTextNDC)) {
260 px1 = gPad->UtoPixel(fX);
261 py1 = gPad->VtoPixel(fY);
262 } else {
263 px1 = gPad->XtoAbsPixel(gPad->XtoPad(fX));
264 py1 = gPad->YtoAbsPixel(gPad->YtoPad(fY));
265 }
266 theta = fTextAngle;
267 Size = 0;
268 pxold = px;
269 pyold = py;
270 co = TMath::Cos(fTextAngle*0.017453293);
271 si = TMath::Sin(fTextAngle*0.017453293);
272 resize = kFALSE;
273 turn = kFALSE;
274 GetControlBox(px1, py1, -theta, cBoxX, cBoxY);
275 div = ((cBoxX[3]-cBoxX[0])*co-(cBoxY[3]-cBoxY[0])*si);
276 if (TMath::Abs(div) > 1e-8) part = (Int_t)(3*((px-cBoxX[0])*co-(py-cBoxY[0])*si)/ div);
277 else part = 0;
278 switch (part) {
279 case 0:
280 if (halign == 3) {
281 turn = kTRUE;
282 right = kTRUE;
283 gPad->SetCursor(kRotate);
284 } else {
285 resize = kTRUE;
286 height = valign;
287 width = halign;
288 gPad->SetCursor(kArrowVer);
289 }
290 break;
291 case 1:
292 gPad->SetCursor(kMove);
293 break;
294 case 2:
295 if (halign == 3) {
296 resize = kTRUE;
297 height = valign;
298 width = halign;
299 gPad->SetCursor(kArrowVer);
300 } else {
301 turn = kTRUE;
302 right = kFALSE;
303 gPad->SetCursor(kRotate);
304 }
305 }
306 break;
307
308 case kArrowKeyRelease:
309 case kButton1Motion:
310 if (!opaque) PaintControlBox(px1, py1, -theta);
311 if (turn) {
312 norm = TMath::Sqrt(Double_t((py-py1)*(py-py1)+(px-px1)*(px-px1)));
313 if (norm>0) {
314 theta = TMath::ACos((px-px1)/norm);
315 dtheta= TMath::ASin((py1-py)/norm);
316 if (dtheta<0) theta = -theta;
317 theta = theta/TMath::Pi()*180;
318 if (theta<0) theta += 360;
319 if (right) {theta = theta+180; if (theta>=360) theta -= 360;}
320 }
321 } else if (resize) {
322
323 co = TMath::Cos(fTextAngle*0.017453293);
324 si = TMath::Sin(fTextAngle*0.017453293);
325 if (width == 1) {
326 switch (valign) {
327 case 1 : ax = px1; ay = py1; break;
328 case 2 : ax = px1+Int_t(si*h/2); ay = py1+Int_t(co*h/2); break;
329 case 3 : ax = px1+Int_t(si*h*3/2); ay = py1+Int_t(co*h*3/2); break;
330 }
331 }
332 if (width == 2) {
333 switch (valign) {
334 case 1 : ax = px1-Int_t(co*w/2); ay = py1+Int_t(si*w/2); break;
335 case 2 : ax = px1-Int_t(co*w/2+si*h/2); ay = py1+Int_t(si*w/2+co*h/2); break;
336 case 3 : ax = px1-Int_t(co*w/2+si*h*3/2); ay = py1+Int_t(si*w/2+co*h*3/2); break;
337 }
338 }
339 if (width == 3) {
340 switch (valign) {
341 case 1 : ax = px1-Int_t(co*w); ay = py1+Int_t(si*w); break;
342 case 2 : ax = px1-Int_t(co*w+si*h/2); ay = py1+Int_t(si*w+co*h/2); break;
343 case 3 : ax = px1-Int_t(co*w+si*h*3/2); ay = py1+Int_t(si*w+co*h*3/2); break;
344 }
345 }
346 if (height == 3) {bx = ax-Int_t(si*h); by = ay-Int_t(co*h);}
347 else {bx = ax; by = ay;}
348 cx = bx+Int_t(co*w); cy = by-Int_t(si*w);
349 lambda = Double_t(((px-bx)*(cx-bx)+(py-by)*(cy-by)))/Double_t(((cx-bx)*(cx-bx)+(cy-by)*(cy-by)));
350 x2 = Double_t(px) - lambda*Double_t(cx-bx)-Double_t(bx);
351 y2 = Double_t(py) - lambda*Double_t(cy-by)-Double_t(by);
352 Size = Int_t(TMath::Sqrt(x2*x2+y2*y2)*2);
353 if (Size<4) Size = 4;
354
357 } else {
358 dx = px - pxold; px1 += dx;
359 dy = py - pyold; py1 += dy;
360 }
361 if (opaque) {
362 if (ndcsav) this->SetNDC(kFALSE);
363 this->SetX(gPad->PadtoX(gPad->AbsPixeltoX(px1)));
364 this->SetY(gPad->PadtoY(gPad->AbsPixeltoY(py1)));
365 if (resize) gPad->ShowGuidelines(this, event, 't', false);
366 if ((!resize)&&(!turn)) gPad->ShowGuidelines(this, event, 'i', true);
367 gPad->ShowGuidelines(this, event, !resize&!turn);
368 this->SetTextAngle(theta);
369 gPad->Modified(kTRUE);
370 gPad->Update();
371 }
372 if (!opaque) PaintControlBox(px1, py1, -theta);
373 pxold = px; pyold = py;
374 break;
375
376 case kButton1Up:
377 if (opaque) {
378 if (ndcsav && !this->TestBit(kTextNDC)) {
379 this->SetX((fX - gPad->GetX1())/(gPad->GetX2()-gPad->GetX1()));
380 this->SetY((fY - gPad->GetY1())/(gPad->GetY2()-gPad->GetY1()));
381 this->SetNDC();
382 }
383 gPad->ShowGuidelines(this, event, !resize&!turn);
384 } else {
385 if (TestBit(kTextNDC)) {
386 dpx = gPad->GetX2() - gPad->GetX1();
387 dpy = gPad->GetY2() - gPad->GetY1();
388 xp1 = gPad->GetX1();
389 yp1 = gPad->GetY1();
390 fX = (gPad->AbsPixeltoX(px1)-xp1)/dpx;
391 fY = (gPad->AbsPixeltoY(py1)-yp1)/dpy;
392 } else {
393 fX = gPad->PadtoX(gPad->AbsPixeltoX(px1));
394 fY = gPad->PadtoY(gPad->AbsPixeltoY(py1));
395 }
396 fTextAngle = theta;
397 }
398 gPad->Modified(kTRUE);
399 break;
400
401 case kButton1Locate:
402 ExecuteEvent(kButton1Down, px, py);
403
404 while (1) {
405 px = py = 0;
406 event = gVirtualX->RequestLocator(1, 1, px, py);
407
409
410 if (event != -1) { // button is released
411 ExecuteEvent(kButton1Up, px, py);
412 return;
413 }
414 }
415 }
416}
417
418////////////////////////////////////////////////////////////////////////////////
419/// Return the text control box. The text position coordinates is (x,y) and
420/// the text angle is theta. The control box coordinates are returned in cBoxX
421/// and cBoxY.
422
424 Int_t cBoxX[4], Int_t cBoxY[4])
425{
426 Short_t halign = fTextAlign/10; // horizontal alignment
427 Short_t valign = fTextAlign - 10*halign; // vertical alignment
428 UInt_t cBoxW, cBoxH; // control box width and heigh
429 UInt_t Dx = 0, Dy = 0; // delta along x and y to align the box
430
432
433 // compute the translations (Dx, Dy) required by the alignments
434 switch (halign) {
435 case 1 : Dx = 0 ; break;
436 case 2 : Dx = cBoxW/2; break;
437 case 3 : Dx = cBoxW ; break;
438 }
439 switch (valign) {
440 case 1 : Dy = 0 ; break;
441 case 2 : Dy = cBoxH/2; break;
442 case 3 : Dy = cBoxH ; break;
443 }
444
445 // compute the control box coordinates before rotation
446 cBoxX[0] = x-Dx;
447 cBoxY[0] = y+Dy;
448 cBoxX[1] = x-Dx;
449 cBoxY[1] = y-cBoxH+Dy;
450 cBoxX[2] = x+cBoxW-Dx;
451 cBoxY[2] = y-cBoxH+Dy;
452 cBoxX[3] = x+cBoxW-Dx;
453 cBoxY[3] = y+Dy;
454
455 // rotate the control box if needed
456 if (theta) {
457 Double_t cosTheta = TMath::Cos(theta*0.017453293);
458 Double_t sinTheta = TMath::Sin(theta*0.017453293);
459 for (int i=0; i<4 ; i++) {
460 Int_t hcBoxX = cBoxX[i];
461 Int_t hcBoxY = cBoxY[i];
464 }
465 }
466}
467
468////////////////////////////////////////////////////////////////////////////////
469/// Return text size in pixels. By default the size returned does not take
470/// into account the text angle (angle = kFALSE). If angle is set to kTRUE
471/// w and h take the angle into account.
472
474{
475 const char *text = GetTitle();
476 if (!text[0]) {
477 w = h = 0;
478 return;
479 }
480
481 if (!gPad) return;
482 if (angle) {
483 Int_t cBoxX[4], cBoxY[4];
484 Int_t ptx, pty;
485 if (TestBit(kTextNDC)) {
486 ptx = gPad->UtoPixel(fX);
487 pty = gPad->VtoPixel(fY);
488 } else {
489 ptx = gPad->XtoAbsPixel(gPad->XtoPad(fX));
490 pty = gPad->YtoAbsPixel(gPad->YtoPad(fY));
491 }
493 Int_t x1 = cBoxX[0];
494 Int_t x2 = cBoxX[0];
495 Int_t y1 = cBoxY[0];
496 Int_t y2 = cBoxY[0];
497 for (Int_t i=1; i<4; i++) {
498 if (cBoxX[i] < x1) x1 = cBoxX[i];
499 if (cBoxX[i] > x2) x2 = cBoxX[i];
500 if (cBoxY[i] < y1) y1 = cBoxY[i];
501 if (cBoxY[i] > y2) y2 = cBoxY[i];
502 }
503 w = x2-x1;
504 h = y2-y1;
505 } else {
506 if ((gVirtualX->HasTTFonts() && TTF::IsInitialized()) || gPad->IsBatch()) {
507 TTF::GetTextExtent(w, h, (char*)GetTitle());
508 } else {
509 const Font_t oldFont = gVirtualX->GetTextFont();
510 if (gVirtualX->InheritsFrom("TGCocoa"))
511 gVirtualX->SetTextFont(fTextFont);
512 gVirtualX->GetTextExtent(w, h, (char*)GetTitle());
513 if (gVirtualX->InheritsFrom("TGCocoa"))
514 gVirtualX->SetTextFont(oldFont);
515 }
516 }
517}
518
519////////////////////////////////////////////////////////////////////////////////
520/// Return text ascent and descent for string text
521/// - in a return total text ascent
522/// - in d return text descent
523
524void TText::GetTextAscentDescent(UInt_t &a, UInt_t &d, const char *text) const
525{
526 if (!gPad) return;
527 Double_t wh = (Double_t)gPad->XtoPixel(gPad->GetX2());
528 Double_t hh = (Double_t)gPad->YtoPixel(gPad->GetY1());
530 if (wh < hh) tsize = fTextSize*wh;
531 else tsize = fTextSize*hh;
532
533 if (gVirtualX->HasTTFonts() || gPad->IsBatch()) {
536 a = TTF::GetBox().yMax;
538 } else {
539 const Font_t oldFont = gVirtualX->GetTextFont();
540 if (gVirtualX->InheritsFrom("TGCocoa"))
541 gVirtualX->SetTextFont(fTextFont);
542 gVirtualX->SetTextSize(tsize);
543 a = gVirtualX->GetFontAscent(text);
544 if (!a) {
545 UInt_t w;
546 gVirtualX->GetTextExtent(w, a, (char*)text);
547 }
548 d = gVirtualX->GetFontDescent(text);
549 if (gVirtualX->InheritsFrom("TGCocoa"))
550 gVirtualX->SetTextFont(oldFont);
551 }
552}
553
554
555////////////////////////////////////////////////////////////////////////////////
556/// Return text ascent and descent for string text
557/// - in a return total text ascent
558/// - in d return text descent
559
560void TText::GetTextAscentDescent(UInt_t &a, UInt_t &d, const wchar_t *text) const
561{
562 if (!gPad) return;
563 Double_t wh = (Double_t)gPad->XtoPixel(gPad->GetX2());
564 Double_t hh = (Double_t)gPad->YtoPixel(gPad->GetY1());
566 if (wh < hh) tsize = fTextSize*wh;
567 else tsize = fTextSize*hh;
568
569 if (gVirtualX->HasTTFonts() || gPad->IsBatch() || gVirtualX->InheritsFrom("TGCocoa")) {
572 a = TTF::GetBox().yMax;
574 } else {
575 gVirtualX->SetTextSize(tsize);
576 a = gVirtualX->GetFontAscent();
577 if (!a) {
578 UInt_t w;
579 gVirtualX->GetTextExtent(w, a, (wchar_t*)text);
580 }
581 d = gVirtualX->GetFontDescent();
582 }
583}
584
585////////////////////////////////////////////////////////////////////////////////
586/// Return text extent for string text
587/// - in w return total text width
588/// - in h return text height
589
590void TText::GetTextExtent(UInt_t &w, UInt_t &h, const char *text) const
591{
592 if (!gPad) return;
593 Double_t wh = (Double_t)gPad->XtoPixel(gPad->GetX2());
594 Double_t hh = (Double_t)gPad->YtoPixel(gPad->GetY1());
596 if (wh < hh) tsize = fTextSize*wh;
597 else tsize = fTextSize*hh;
598
599 if (gVirtualX->HasTTFonts() || gPad->IsBatch()) {
602 TTF::GetTextExtent(w, h, (char*)text);
603 } else {
604 const Font_t oldFont = gVirtualX->GetTextFont();
605 if (gVirtualX->InheritsFrom("TGCocoa"))
606 gVirtualX->SetTextFont(fTextFont);
607 gVirtualX->SetTextSize(tsize);
608 gVirtualX->GetTextExtent(w, h, (char*)text);
609 if (gVirtualX->InheritsFrom("TGCocoa"))
610 gVirtualX->SetTextFont(oldFont);
611 }
612}
613
614////////////////////////////////////////////////////////////////////////////////
615/// Return text advance for string text
616/// if kern is true (default) kerning is taken into account. If it is false
617/// the kerning is not taken into account.
618
619void TText::GetTextAdvance(UInt_t &a, const char *text, const Bool_t kern) const
620{
621 if (!gPad) return;
622 Double_t wh = (Double_t)gPad->XtoPixel(gPad->GetX2());
623 Double_t hh = (Double_t)gPad->YtoPixel(gPad->GetY1());
625 if (wh < hh) tsize = fTextSize*wh;
626 else tsize = fTextSize*hh;
627
628 if (gVirtualX->HasTTFonts() || gPad->IsBatch()) {
633 TTF::GetTextAdvance(a, (char*)text);
635 } else {
636 UInt_t h;
637 const Font_t oldFont = gVirtualX->GetTextFont();
638 //how do I know what to calculate without a font???
639 if (gVirtualX->InheritsFrom("TGCocoa"))
640 gVirtualX->SetTextFont(fTextFont);
641
642 gVirtualX->SetTextSize(tsize);
643 gVirtualX->GetTextExtent(a, h, (char*)text);
644
645 if (gVirtualX->InheritsFrom("TGCocoa"))
646 gVirtualX->SetTextFont(oldFont);
647 }
648}
649
650////////////////////////////////////////////////////////////////////////////////
651/// Return text extent for string text
652/// - in w return total text width
653/// - in h return text height
654
655void TText::GetTextExtent(UInt_t &w, UInt_t &h, const wchar_t *text) const
656{
657 if (!gPad) return;
658 Double_t wh = (Double_t)gPad->XtoPixel(gPad->GetX2());
659 Double_t hh = (Double_t)gPad->YtoPixel(gPad->GetY1());
661 if (wh < hh) tsize = fTextSize*wh;
662 else tsize = fTextSize*hh;
663
664 if (gVirtualX->HasTTFonts() || gPad->IsBatch() || gVirtualX->InheritsFrom("TGCocoa")) {
667 TTF::GetTextExtent(w, h, (wchar_t*)text);
668 } else {
669 gVirtualX->SetTextSize(tsize);
670 gVirtualX->GetTextExtent(w, h, (wchar_t*)text);
671 }
672}
673
674////////////////////////////////////////////////////////////////////////////////
675/// List this text with its attributes.
676
677void TText::ls(Option_t *) const
678{
680 printf("Text X=%f Y=%f Text=%s\n",fX,fY,GetTitle());
681}
682
683////////////////////////////////////////////////////////////////////////////////
684/// Paint this text with its current attributes.
685
687{
688 if (!gPad) return;
689 TAttText::Modify(); //Change text attributes only if necessary
690 if (TestBit(kTextNDC)) gPad->PaintTextNDC(fX,fY,GetTitle());
691 else gPad->PaintText(gPad->XtoPad(fX),gPad->YtoPad(fY),GetTitle());
692}
693
694////////////////////////////////////////////////////////////////////////////////
695/// Paint the text control box. (x,y) are the coordinates where the control
696/// box should be painted and theta is the angle of the box.
697
699{
700 Int_t cBoxX[4], cBoxY[4];
701 Short_t halign = fTextAlign/10; // horizontal alignment
702 Short_t valign = fTextAlign - 10*halign; // vertical alignment
703
704 GetControlBox(x, y, theta, cBoxX, cBoxY);
705 // Draw the text control box outline
706 gVirtualX->SetLineStyle((Style_t)1);
707 gVirtualX->SetLineWidth(1);
708 gVirtualX->SetLineColor(1);
709 gVirtualX->DrawLine(cBoxX[0], cBoxY[0], cBoxX[1], cBoxY[1]);
710 gVirtualX->DrawLine(cBoxX[1], cBoxY[1], cBoxX[2], cBoxY[2]);
711 gVirtualX->DrawLine(cBoxX[2], cBoxY[2], cBoxX[3], cBoxY[3]);
712 gVirtualX->DrawLine(cBoxX[3], cBoxY[3], cBoxX[0], cBoxY[0]);
713
714 // Draw a symbol at the text starting point
715 TPoint p;
716 Int_t ix = 0, iy = 0;
717 switch (halign) {
718 case 1 :
719 switch (valign) {
720 case 1 : ix = 0 ; iy = 0 ; break;
721 case 2 : ix = 0 ; iy = 1 ; break;
722 case 3 : ix = 1 ; iy = 1 ; break;
723 }
724 break;
725 case 2 :
726 switch (valign) {
727 case 1 : ix = 0 ; iy = 3 ; break;
728 case 2 : ix = 0 ; iy = 2 ; break;
729 case 3 : ix = 1 ; iy = 2 ; break;
730 }
731 break;
732 case 3 :
733 switch (valign) {
734 case 1 : ix = 3 ; iy = 3 ; break;
735 case 2 : ix = 2 ; iy = 3 ; break;
736 case 3 : ix = 2 ; iy = 2 ; break;
737 }
738 break;
739 }
740 p.fX = (cBoxX[ix]+cBoxX[iy])/2;
741 p.fY = (cBoxY[ix]+cBoxY[iy])/2;
742 gVirtualX->SetMarkerColor(1);
743 gVirtualX->SetMarkerStyle(24);
744 gVirtualX->SetMarkerSize(0.7);
745 gVirtualX->DrawPolyMarker(1, &p);
746}
747
748////////////////////////////////////////////////////////////////////////////////
749/// Draw this text with new coordinates.
750
752{
753 TAttText::Modify(); //Change text attributes only if necessary
754 if (gPad) gPad->PaintText(x,y,text);
755}
756
757////////////////////////////////////////////////////////////////////////////////
758/// Draw this text with new coordinates.
759
760void TText::PaintText(Double_t x, Double_t y, const wchar_t *text)
761{
762 TAttText::Modify(); //Change text attributes only if necessary
763 if (gPad) gPad->PaintText(x,y,text);
764}
765
766////////////////////////////////////////////////////////////////////////////////
767/// Draw this text with new coordinates in NDC.
768
770{
771 TAttText::Modify(); //Change text attributes only if necessary
772 if (gPad) gPad->PaintTextNDC(u,v,text);
773}
774
775////////////////////////////////////////////////////////////////////////////////
776/// Draw this text with new coordinates in NDC.
777
779{
780 TAttText::Modify(); //Change text attributes only if necessary
781 if (gPad) gPad->PaintTextNDC(u,v,text);
782}
783
784////////////////////////////////////////////////////////////////////////////////
785/// Dump this text with its attributes.
786
788{
789 printf("Text X=%f Y=%f Text=%s Font=%d Size=%f",fX,fY,GetTitle(),GetTextFont(),GetTextSize());
790 if (GetTextColor() != 1 ) printf(" Color=%d",GetTextColor());
791 if (GetTextAlign() != 10) printf(" Align=%d",GetTextAlign());
792 if (GetTextAngle() != 0 ) printf(" Angle=%f",GetTextAngle());
793 printf("\n");
794}
795
796////////////////////////////////////////////////////////////////////////////////
797/// Save primitive as a C++ statement(s) on output stream out
798
799void TText::SavePrimitive(std::ostream &out, Option_t *option)
800{
802 out, Class(), "text",
803 TString::Format("%g, %g, \"%s\"", fX, fY, TString(GetTitle()).ReplaceSpecialCppChars().Data()), kFALSE);
804
805 SaveTextAttributes(out, "text", 11, 0, 1, 62, 0.05);
806
807 if (TestBit(kTextNDC))
808 out << " text->SetNDC();\n";
809
810 SavePrimitiveDraw(out, "text", option);
811}
812
813////////////////////////////////////////////////////////////////////////////////
814/// Set NDC mode on if isNDC = kTRUE, off otherwise
815
820
821////////////////////////////////////////////////////////////////////////////////
822/// Change (i.e. set) the title of the TNamed.
823
824void TText::SetMbTitle(const wchar_t *title)
825{
826 char *mb_title = new char[MB_CUR_MAX * wcslen(title) + 1]();
827 char *p = mb_title;
828 size_t length = wcslen(title);
829 for (size_t i = 0; i < length; i++) {
830 const int n = wctomb(p, title[i]);
831 if (n >= 0) p += n;
832 }
834 delete [] mb_title;
835 if (gPad && TestBit(kMustCleanup)) gPad->Modified();
836}
837
838////////////////////////////////////////////////////////////////////////////////
839/// Stream an object of class TText.
840
842{
843 if (R__b.IsReading()) {
845 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
846 if (R__v > 1) {
847 R__b.ReadClassBuffer(TText::Class(), this, R__v, R__s, R__c);
848 return;
849 }
850 //====process old versions before automatic schema evolution
853 Float_t x,y;
854 R__b >> x; fX = x;
855 R__b >> y; fY = y;
856 //====end of old versions
857
858 } else {
859 R__b.WriteClassBuffer(TText::Class(),this);
860 }
861}
862////////////////////////////////////////////////////////////////////////////////
863/// Return the "bounding Box" of the Box
864
866{
867 Rectangle_t BBox{0, 0, 0, 0};
868 if (gPad) {
869 UInt_t w, h;
870 Int_t Dx = 0, Dy = 0;
871 GetBoundingBox(w, h, false);
872
875
876 switch (halign) {
877 case 1: Dx = 0; break;
878 case 2: Dx = w / 2; break;
879 case 3: Dx = w; break;
880 }
881
882 switch (valign) {
883 case 1: Dy = h; break;
884 case 2: Dy = h / 2; break;
885 case 3: Dy = 0; break;
886 }
887
888 BBox.fX = gPad->XtoPixel(fX) - Dx;
889 BBox.fY = gPad->YtoPixel(fY) - Dy;
890 BBox.fWidth = w;
891 BBox.fHeight = h;
892 }
893 return BBox;
894}
895
896////////////////////////////////////////////////////////////////////////////////
897/// Return the point given by Alignment as 'center'
898
900{
901 TPoint p(0, 0);
902 if (gPad) {
903 p.SetX(gPad->XtoPixel(fX));
904 p.SetY(gPad->YtoPixel(fY));
905 }
906 return p;
907}
908
909////////////////////////////////////////////////////////////////////////////////
910/// Set the point given by Alignment as 'center'
911
913{
914 if (!gPad) return;
915 this->SetX(gPad->PixeltoX(p.GetX()));
916 this->SetY(gPad->PixeltoY(p.GetY()-gPad->VtoPixel(0)));
917}
918
919////////////////////////////////////////////////////////////////////////////////
920/// Set X coordinate of the point given by Alignment as 'center'
921
923{
924 if (!gPad) return;
925 this->SetX(gPad->PixeltoX(x));
926}
927
928////////////////////////////////////////////////////////////////////////////////
929/// Set Y coordinate of the point given by Alignment as 'center'
930
932{
933 if (!gPad) return;
934 this->SetY(gPad->PixeltoY(y - gPad->VtoPixel(0)));
935}
936
937////////////////////////////////////////////////////////////////////////////////
938/// Set left hand side of BoundingBox to a value
939/// (resize in x direction on left)
940
941void TText::SetBBoxX1(const Int_t /*x*/)
942{
943 //NOT IMPLEMENTED
944}
945
946////////////////////////////////////////////////////////////////////////////////
947/// Set right hand side of BoundingBox to a value
948/// (resize in x direction on right)
949
950void TText::SetBBoxX2(const Int_t /*x*/)
951{
952 //NOT IMPLEMENTED
953}
954
955////////////////////////////////////////////////////////////////////////////////
956/// Set top of BoundingBox to a value (resize in y direction on top)
957
958void TText::SetBBoxY1(const Int_t /*y*/)
959{
960 //NOT IMPLEMENTED
961}
962
963////////////////////////////////////////////////////////////////////////////////
964/// Set bottom of BoundingBox to a value
965/// (resize in y direction on bottom)
966
967void TText::SetBBoxY2(const Int_t /*y*/)
968{
969 //NOT IMPLEMENTED
970}
@ 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:374
@ kMove
Definition GuiTypes.h:374
@ kRotate
Definition GuiTypes.h:374
#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
short Style_t
Style number (short)
Definition RtypesCore.h:96
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
short Version_t
Class version identifier (short)
Definition RtypesCore.h:79
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
short Font_t
Font number (short)
Definition RtypesCore.h:95
short Short_t
Signed Short integer 2 bytes (short)
Definition RtypesCore.h:53
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
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.
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t option
Option_t Option_t SetTextSize
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 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 Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
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 width
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 GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t height
Option_t Option_t TPoint TPoint const char text
Option_t Option_t TPoint TPoint const char y1
#define gPad
#define gVirtualX
Definition TVirtualX.h:337
Abstract base class for elements drawn in the editor.
Definition TAttBBox2D.h:19
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
Float_t fTextAngle
Text angle.
Definition TAttText.h:23
virtual Color_t GetTextColor() const
Return the text color.
Definition TAttText.h:36
virtual void Streamer(TBuffer &)
virtual void SetTextAngle(Float_t tangle=0)
Set the text angle.
Definition TAttText.h:45
virtual Float_t GetTextAngle() const
Return the text angle.
Definition TAttText.h:35
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
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
Float_t fTextSize
Text size.
Definition TAttText.h:24
Buffer base class used for serializing objects.
Definition TBuffer.h:43
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:41
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:202
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:864
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:822
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:771
@ kCanDelete
if object in a list can be deleted
Definition TObject.h:68
@ kMustCleanup
if object destructor must call RecursiveRemove()
Definition TObject.h:70
SCoord_t GetY() const
Definition TPoint.h:47
SCoord_t GetX() const
Definition TPoint.h:46
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition TROOT.cxx:2898
Basic string class.
Definition TString.h:138
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:2384
static void SetKerning(Bool_t state)
Set kerning flag.
Definition TTF.cxx:339
static Bool_t IsInitialized()
Definition TTF.cxx:614
static void GetTextAdvance(UInt_t &a, char *text)
Get advance (a) when text is horizontal.
Definition TTF.cxx:169
static Bool_t GetKerning()
Definition TTF.cxx:600
static void SetTextFont(Font_t fontnumber)
Set specified font.
Definition TTF.cxx:495
static void GetTextExtent(UInt_t &w, UInt_t &h, char *text)
Get width (w) and height (h) when text is horizontal.
Definition TTF.cxx:153
static const FT_BBox & GetBox()
Definition TTF.cxx:656
static void SetTextSize(Float_t textsize)
Set current text size.
Definition TTF.cxx:566
Base class for several text objects.
Definition TText.h:22
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:129
void SetBBoxCenter(const TPoint &p) override
Set the point given by Alignment as 'center'.
Definition TText.cxx:912
Rectangle_t GetBBox() override
Return the "bounding Box" of the Box.
Definition TText.cxx:865
void Copy(TObject &text) const override
Copy this text to text.
Definition TText.cxx:106
TText & operator=(const TText &src)
Assignment operator.
Definition TText.cxx:97
@ kTextNDC
The text position is in the NDC space.
Definition TText.h:32
virtual void GetBoundingBox(UInt_t &w, UInt_t &h, Bool_t angle=kFALSE)
Return text size in pixels.
Definition TText.cxx:473
virtual void SetY(Double_t y)
Definition TText.h:77
virtual void PaintControlBox(Int_t x, Int_t y, Double_t theta)
Paint the text control box.
Definition TText.cxx:698
virtual TText * DrawText(Double_t x, Double_t y, const char *text)
Draw this text with new coordinates.
Definition TText.cxx:175
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:81
virtual void PaintTextNDC(Double_t u, Double_t v, const char *text)
Draw this text with new coordinates in NDC.
Definition TText.cxx:769
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Definition TText.cxx:799
void ls(Option_t *option="") const override
List this text with its attributes.
Definition TText.cxx:677
virtual void PaintText(Double_t x, Double_t y, const char *text)
Draw this text with new coordinates.
Definition TText.cxx:751
TText()
Definition TText.h:35
void SetBBoxY2(const Int_t) override
Set bottom of BoundingBox to a value (resize in y direction on bottom)
Definition TText.cxx:967
virtual void GetTextExtent(UInt_t &w, UInt_t &h, const char *text) const
Return text extent for string text.
Definition TText.cxx:590
virtual void GetTextAscentDescent(UInt_t &a, UInt_t &d, const char *text) const
Return text ascent and descent for string text.
Definition TText.cxx:524
Int_t DistancetoPrimitive(Int_t px, Int_t py) override
Compute distance from point px,py to a string.
Definition TText.cxx:143
TPoint GetBBoxCenter() override
Return the point given by Alignment as 'center'.
Definition TText.cxx:899
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:619
void Paint(Option_t *option="") override
Paint this text with its current attributes.
Definition TText.cxx:686
void SetBBoxCenterY(const Int_t y) override
Set Y coordinate of the point given by Alignment as 'center'.
Definition TText.cxx:931
virtual void SetNDC(Bool_t isNDC=kTRUE)
Set NDC mode on if isNDC = kTRUE, off otherwise.
Definition TText.cxx:816
void Print(Option_t *option="") const override
Dump this text with its attributes.
Definition TText.cxx:787
void SetBBoxY1(const Int_t) override
Set top of BoundingBox to a value (resize in y direction on top)
Definition TText.cxx:958
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:423
void SetBBoxX2(const Int_t) override
Set right hand side of BoundingBox to a value (resize in x direction on right)
Definition TText.cxx:950
void SetBBoxCenterX(const Int_t x) override
Set X coordinate of the point given by Alignment as 'center'.
Definition TText.cxx:922
void SetBBoxX1(const Int_t) override
Set left hand side of BoundingBox to a value (resize in x direction on left)
Definition TText.cxx:941
void Streamer(TBuffer &) override
Stream an object of class TText.
Definition TText.cxx:841
virtual TText * DrawTextNDC(Double_t x, Double_t y, const char *text)
Draw this text with new coordinates in NDC.
Definition TText.cxx:201
static TClass * Class()
virtual void SetMbTitle(const wchar_t *title=L"")
Change (i.e. set) the title of the TNamed.
Definition TText.cxx:824
void ExecuteEvent(Int_t event, Int_t px, Int_t py) override
Execute action corresponding to one event.
Definition TText.cxx:224
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:643
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:1320
Double_t ASin(Double_t)
Returns the principal value of the arc sine of x, expressed in radians.
Definition TMath.h:635
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:673
Double_t Cos(Double_t)
Returns the cosine of an angle of x radians.
Definition TMath.h:605
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:599
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:124
Rectangle structure (maps to the X11 XRectangle structure)
Definition GuiTypes.h:361