Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
fonts_ttf.cxx
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_webcanv
3/// \notebook -js
4/// Usage of TTF fonts in web canvas.
5///
6/// One can load TTF font file and specify it for usage in the web canvas
7/// Produced drawing also can be saved in PDF files.
8///
9/// Functionality available only in web-based graphics
10///
11/// \macro_image (tcanvas_js)
12/// \macro_code
13///
14/// \author Sergey Linev
15
16void fonts_ttf()
17{
18 TString fontdir = TROOT::GetDataDir() + "/fonts/";
19
20 auto fontid = TWebCanvas::AddFont("UserBold", fontdir + "trebuc.ttf");
21 auto fontid_bold = TWebCanvas::AddFont("UserBold", fontdir + "trebucbd.ttf");
22 auto fontid_bold_italic = TWebCanvas::AddFont("UserBoldItalic", fontdir + "trebucbi.ttf");
23 auto fontid_italic = TWebCanvas::AddFont("UserItalic", fontdir + "trebucit.ttf");
24
25 if ((fontid < 0) || (fontid_bold < 0) || (fontid_bold_italic < 0) || (fontid_italic < 0))
26 ::Error("fonts_ttf.cxx", "fail to load ttf fonts from %s", fontdir.Data());
27
28 auto c1 = new TCanvas("c1", "c1", 1000, 600);
29
30 if (!gROOT->IsBatch() && !c1->IsWeb())
31 ::Warning("fonts_ttf.cxx", "macro will not work without enabling web-based canvas");
32
33 auto l1 = new TLatex(0.5, 0.8, "Custom font from trebuc.ttf");
34 l1->SetTextFont(fontid);
35 l1->SetTextAlign(22);
36 l1->SetTextSize(0.1);
37 c1->Add(l1);
38
39 auto l2 = new TLatex(0.5, 0.6, "Custom bold font from trebucbd.ttf");
40 l2->SetTextFont(fontid_bold);
41 l2->SetTextAlign(22);
42 l2->SetTextSize(0.1);
43 c1->Add(l2);
44
45 auto l3 = new TLatex(0.5, 0.4, "Custom bold italic font from trebucbi.ttf");
46 l3->SetTextFont(fontid_bold_italic);
47 l3->SetTextAlign(22);
48 l3->SetTextSize(0.1);
49 c1->Add(l3);
50
51 auto l4 = new TLatex(0.5, 0.2, "Custom italic font from trebucit.ttf");
52 l4->SetTextFont(fontid_italic);
53 l4->SetTextAlign(22);
54 l4->SetTextSize(0.1);
55 c1->Add(l4);
56}
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:229
#define gROOT
Definition TROOT.h:406
The Canvas class.
Definition TCanvas.h:23
To draw Mathematical Formula.
Definition TLatex.h:18
static const TString & GetDataDir()
Get the data directory in the installation. Static utility function.
Definition TROOT.cxx:3065
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
static Font_t AddFont(const char *name, const char *ttffile, Int_t precision=2)
Add font to static list of fonts supported by the canvas Name specifies name of the font,...
return c1
Definition legend1.C:41