Skip to content

Commit a2bea54

Browse files
authored
Implement #url in TLatex (#21369)
1 parent a0fb339 commit a2bea54

18 files changed

Lines changed: 138 additions & 15 deletions

File tree

core/base/inc/TVirtualPS.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class TVirtualPS : public TNamed, public TAttLine, public TAttFill, public TAttM
6262
virtual void Open(const char *filename, Int_t type=-111) = 0;
6363
virtual void Text(Double_t x, Double_t y, const char *string) = 0;
6464
virtual void Text(Double_t x, Double_t y, const wchar_t *string) = 0;
65+
virtual void TextUrl(Double_t x, Double_t y, const char *string, const char *url) = 0;
6566
virtual void SetColor(Float_t r, Float_t g, Float_t b) = 0;
6667

6768
virtual void PrintFast(Int_t nch, const char *string="");

core/base/inc/TVirtualPad.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ class TVirtualPad : public TObject, public TAttLine, public TAttFill,
195195
virtual void PaintText(Double_t x, Double_t y, const wchar_t *text) = 0;
196196
virtual void PaintTextNDC(Double_t u, Double_t v, const char *text) = 0;
197197
virtual void PaintTextNDC(Double_t u, Double_t v, const wchar_t *text) = 0;
198+
virtual void PaintTextUrl(Double_t x, Double_t y, const char *text, const char *url) = 0;
198199
virtual Double_t PixeltoX(Double_t px) = 0;
199200
virtual Double_t PixeltoY(Double_t py) = 0;
200201
virtual void PixeltoXY(Double_t xpixel, Double_t ypixel, Double_t &x, Double_t &y) = 0;

graf2d/gpad/inc/TPad.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ friend class TWebCanvas;
307307
void PaintText(Double_t x, Double_t y, const wchar_t *text) override;
308308
void PaintTextNDC(Double_t u, Double_t v, const char *text) override;
309309
void PaintTextNDC(Double_t u, Double_t v, const wchar_t *text) override;
310+
void PaintTextUrl(Double_t x, Double_t y, const char *text, const char *url) override;
310311
virtual TPad *Pick(Int_t px, Int_t py, TObjLink *&pickobj);
311312
Double_t PixeltoX(Double_t px) override;
312313
Double_t PixeltoY(Double_t py) override;

graf2d/gpad/src/TPad.cxx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4777,6 +4777,20 @@ void TPad::PaintText(Double_t x, Double_t y, const wchar_t *text)
47774777
if (gVirtualPS) gVirtualPS->Text(x, y, text);
47784778
}
47794779

4780+
////////////////////////////////////////////////////////////////////////////////
4781+
/// Paint text with URL in CurrentPad World coordinates.
4782+
4783+
void TPad::PaintTextUrl(Double_t x, Double_t y, const char *text, const char *url)
4784+
{
4785+
Modified();
4786+
4787+
if (!gPad->IsBatch() && GetPainter())
4788+
GetPainter()->DrawText(x, y, text, TVirtualPadPainter::kClear);
4789+
4790+
if (gVirtualPS) gVirtualPS->TextUrl(x, y, text, url);
4791+
}
4792+
4793+
47804794
////////////////////////////////////////////////////////////////////////////////
47814795
/// Paint text in CurrentPad NDC coordinates.
47824796

graf2d/graf/src/TLatex.cxx

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ to the Latex's one. It provides several functionalities:
4444
- [Italic and Boldface](\ref L12)
4545
- [Examples](\ref L13)
4646
- [Interface to TMathText](\ref L14)
47+
- [URL links](\ref L15)
4748
4849
When the font precision (see `TAttText`) is low (0 or 1), TLatex is
4950
painted as a normal TText, the control characters are not interpreted.
@@ -393,6 +394,21 @@ TeX syntax and uses "\\" as control instead of "#". If a piece of text containin
393394
"\\" is given to `TLatex` then `TMathText` is automatically invoked.
394395
Therefore, as histograms' titles, axis titles, labels etc ... are drawn using
395396
`TLatex`, the `TMathText` syntax can be used for them also.
397+
398+
\anchor L15
399+
## URL links
400+
JSROOT and standard SVG output support the syntax '#url[link]{label}'.
401+
This can be combined with other LaTeX commands, such as color or font settings.
402+
Begin_Macro(source)
403+
{
404+
auto cl = new TCanvas("cl", "Use of #url in TLatex", 1200, 800);
405+
auto latex = new TLatex(0.5, 0.5, "Link on #color[4]{#url[https://root.cern]{root.cern}} web site");
406+
latex->SetTextSize(0.1);
407+
latex->SetTextAlign(22);
408+
latex->Draw();
409+
cl->Print("cl.svg");
410+
}
411+
End_Macro
396412
*/
397413

398414
////////////////////////////////////////////////////////////////////////////////
@@ -647,6 +663,7 @@ TLatex::TLatexFormSize TLatex::Analyse(Double_t x, Double_t y, const TextSpec_t
647663
Int_t opSquareCurly = -1; // Position of first ]{
648664
Int_t opCloseCurly = -2; // Position of first }
649665
Int_t opColor = -1; // Position of first #color
666+
Int_t opUrl = -1; // Position of first #url
650667
Int_t opFont = -1; // Position of first #font
651668
Int_t opScale = -1; // Position of first #scale
652669
Int_t opGreek = -1; // Position of a Greek letter
@@ -863,6 +880,11 @@ TLatex::TLatexFormSize TLatex::Analyse(Double_t x, Double_t y, const TextSpec_t
863880
if (i>0 && opCloseCurly==-2) opCloseCurly=i-1;
864881
continue;
865882
}
883+
if (strncmp(buf,"url[",4)==0 || strncmp(buf,"url{",4)==0) {
884+
opUrl=i; opFound = kTRUE;
885+
if (i>0 && opCloseCurly==-2) opCloseCurly=i-1;
886+
continue ;
887+
}
866888
}
867889
if (length>i+3) {
868890
Char_t buf[4];
@@ -1738,6 +1760,25 @@ TLatex::TLatexFormSize TLatex::Analyse(Double_t x, Double_t y, const TextSpec_t
17381760
Analyse(x,y,newSpec,text+opSquareCurly+1,length-opSquareCurly-1);
17391761
}
17401762
}
1763+
else if (opUrl>-1) { // \url found
1764+
if (opSquareCurly==-1) {
1765+
// url is not specified
1766+
fError = "Missing url. Syntax is #url[http://...]{ ... }";
1767+
delete[] text;
1768+
return TLatexFormSize(0,0,0);
1769+
}
1770+
TextSpec_t newSpec = spec;
1771+
Char_t *url = new Char_t[opSquareCurly-opUrl-4];
1772+
strncpy(url,text+opUrl+5,opSquareCurly-opUrl-5);
1773+
fName = url;
1774+
delete[] url;
1775+
if (!fShow) {
1776+
result = Anal1(newSpec,text+opSquareCurly+1,length-opSquareCurly-1);
1777+
} else {
1778+
Analyse(x,y,newSpec,text+opSquareCurly+1,length-opSquareCurly-1);
1779+
}
1780+
fName = "";
1781+
}
17411782
else if (opFont>-1) { // \font found
17421783
if (opSquareCurly==-1) {
17431784
// font number is not specified
@@ -1927,7 +1968,8 @@ TLatex::TLatexFormSize TLatex::Analyse(Double_t x, Double_t y, const TextSpec_t
19271968
// paint the Latex sub-expression per sub-expression
19281969
Double_t xx, yy;
19291970
Rotate(gPad, spec.fAngle, x, y, xx, yy);
1930-
gPad->PaintText(xx, yy, text);
1971+
if (fName.Length() > 0) gPad->PaintTextUrl(xx, yy, text, fName.Data());
1972+
else gPad->PaintText(xx, yy, text);
19311973
} else {
19321974
GetTextExtent(w,h,text);
19331975
Double_t width = w;
@@ -2310,19 +2352,19 @@ Int_t TLatex::PaintLatex1(Double_t x, Double_t y, Double_t angle, Double_t size,
23102352

23112353
Int_t TLatex::CheckLatexSyntax(TString &text)
23122354
{
2313-
const Char_t *kWord1[] = {"{}^{","{}_{","^{","_{","#scale{","#color{","#font{","#sqrt{","#[]{","#{}{","#||{",
2355+
const Char_t *kWord1[] = {"{}^{","{}_{","^{","_{","#scale{","#color{","#url{","#font{","#sqrt{","#[]{","#{}{","#||{",
23142356
"#bar{","#vec{","#dot{","#hat{","#ddot{","#acute{","#grave{","#check{","#tilde{","#slash{","#bf{","#it{","#mbox{",
23152357
"\\scale{","\\color{","\\font{","\\sqrt{","\\[]{","\\{}{","\\||{","#(){","\\(){",
23162358
"\\bar{","\\vec{","\\dot{","\\hat{","\\ddot{","\\acute{","\\grave{","\\check{","\\bf{","\\it{","\\mbox{"}; // check for }
2317-
const Char_t *kWord2[] = {"#scale[","#color[","#font[","#sqrt[","#kern[","#lower[","\\scale[","\\color[","\\font[","\\sqrt[","\\kern[","\\lower["}; // check for ]{ + }
2359+
const Char_t *kWord2[] = {"#scale[","#color[","#url[","#font[","#sqrt[","#kern[","#lower[","\\scale[","\\color[","\\font[","\\sqrt[","\\kern[","\\lower["}; // check for ]{ + }
23182360
const Char_t *kWord3[] = {"#frac{","\\frac{","#splitline{","\\splitline{"}; // check for }{ then }
23192361
const Char_t *kLeft1[] = {"#left[","\\left[","#left{","\\left{","#left|","\\left|","#left(","\\left("};
23202362
const Char_t *kLeft2[] = {"#[]{","#[]{","#{}{","#{}{","#||{","#||{","#(){","#(){"};
23212363
const Char_t *kRight[] = {"#right]","\\right]","#right}","\\right}","#right|","\\right|","#right)","\\right)"};
2322-
const Int_t lkWord1[] = {4,4,2,2,7,7,6,6,4,4,4,5,5,5,5,6,7,7,7,7,7,4,4,6,7,7,6,6,4,4,4,4,4,5,5,5,5,6,7,7,7,4,4,6};
2323-
const Int_t lkWord2[] = {7,7,6,6,6,7,7,7,6,6,6,7} ;
2364+
const Int_t lkWord1[] = {4,4,2,2,7,7,5,6,6,4,4,4,5,5,5,5,6,7,7,7,7,7,4,4,6,7,7,6,6,4,4,4,4,4,5,5,5,5,6,7,7,7,4,4,6};
2365+
const Int_t lkWord2[] = {7,7,5,6,6,6,7,7,7,6,6,6,7} ;
23242366
const Int_t lkWord3[] = {6,6,11,11} ;
2325-
Int_t nkWord1 = 44, nkWord2 = 12, nkWord3 = 4;
2367+
Int_t nkWord1 = 45, nkWord2 = 13, nkWord3 = 4;
23262368
Int_t i,k ;
23272369
Int_t nLeft1 , nRight , nOfLeft, nOfRight;
23282370
Int_t lLeft1 = 6 ;

graf2d/postscript/inc/TImageDump.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class TImageDump : public TVirtualPS {
4949
void Open(const char *filename, Int_t type = -111) override;
5050
void Text(Double_t x, Double_t y, const char *string) override;
5151
void Text(Double_t x, Double_t y, const wchar_t *string) override;
52+
void TextUrl(Double_t x, Double_t y, const char *string, const char *url) override;
5253
void SetColor(Float_t r, Float_t g, Float_t b) override;
5354
void *GetStream() const override { return (void*)fImage; }
5455
void SetType(Int_t type = -111) override { fType = type; }

graf2d/postscript/inc/TPDF.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class TPDF : public TVirtualPS {
103103
void SetTextColor(Color_t cindex=1) override;
104104
void Text(Double_t x, Double_t y, const char *string) override;
105105
void Text(Double_t, Double_t, const wchar_t *) override;
106+
void TextUrl(Double_t x, Double_t y, const char *string, const char *url) override;
106107
void TextNDC(Double_t u, Double_t v, const char *string);
107108
void TextNDC(Double_t, Double_t, const wchar_t *);
108109
void WriteCompressedBuffer();

graf2d/postscript/inc/TPostScript.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ class TPostScript : public TVirtualPS {
129129
void SetColor(Int_t color = 1);
130130
void SetColor(Float_t r, Float_t g, Float_t b) override;
131131
void Text(Double_t x, Double_t y, const char *string) override;
132+
void TextUrl(Double_t x, Double_t y, const char *string, const char *url) override;
132133
void Text(Double_t x, Double_t y, const wchar_t *string) override;
133134
void TextNDC(Double_t u, Double_t v, const char *string);
134135
void TextNDC(Double_t u, Double_t v, const wchar_t *string);

graf2d/postscript/inc/TSVG.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class TSVG : public TVirtualPS {
7777
void SetTextColor(Color_t cindex=1) override;
7878
void Text(Double_t x, Double_t y, const char *string) override;
7979
void Text(Double_t, Double_t, const wchar_t *) override {}
80+
void TextUrl(Double_t x, Double_t y, const char *string, const char *url) override;
8081
void TextNDC(Double_t u, Double_t v, const char *string);
8182
void TextNDC(Double_t, Double_t, const wchar_t *) {}
8283
void WriteReal(Float_t r, Bool_t space=kTRUE) override;

graf2d/postscript/inc/TTeXDump.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class TTeXDump : public TVirtualPS {
7070
void SetTextColor(Color_t cindex=1) override;
7171
void Text(Double_t x, Double_t y, const char *string) override;
7272
void Text(Double_t, Double_t, const wchar_t *) override {}
73+
void TextUrl(Double_t x, Double_t y, const char *string, const char *url) override;
7374
void TextNDC(Double_t u, Double_t v, const char *string);
7475
void TextNDC(Double_t, Double_t, const wchar_t *) {}
7576
Float_t UtoTeX(Double_t u);

0 commit comments

Comments
 (0)