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