Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
REveText.cxx
Go to the documentation of this file.
1// @(#)root/eve7:$Id$
2// Author: Waad Alshehri, 2023
3
4/*************************************************************************
5 * Copyright (C) 1995-2019, 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
13#include <ROOT/REveText.hxx>
15
16#include <nlohmann/json.hpp>
17
18using namespace ROOT::Experimental;
19
20////////////////////////////////////////////////////////////////////////////////
21/// Constructor.
22
23REveText::REveText(const Text_t* n, const Text_t* t) :
24 REveShape(n, t)
25{
26 // MainColor set to FillColor in Shape.
27 fPickable = true;
28 fLineWidth = 0.05; // override, in text-size units
29}
30
31////////////////////////////////////////////////////////////////////////////////
32/// Fill core part of JSON representation.
33
34Int_t REveText::WriteCoreJson(nlohmann::json &j, Int_t rnr_offset)
35{
36 Int_t ret = REveShape::WriteCoreJson(j, rnr_offset);
37
38 j["fText"] = fText;
39 j["fFont"] = fFont;
40 j["fPosX"] = fPosition.fX;
41 j["fPosY"] = fPosition.fY;
42 j["fPosZ"] = fPosition.fZ;
43 j["fFontSize"] = fFontSize;
44 j["fFontHinting"] = fFontHinting;
45 j["fExtraBorder"] = fExtraBorder;
46 j["fMode"] = fMode;
47 j["fTextColor"] = fTextColor;
48
49 return ret;
50}
51
52////////////////////////////////////////////////////////////////////////////////
53/// Crates 3D point array for rendering.
54
56{
57 fRenderData = std::make_unique<REveRenderData>("makeZText");
59 // TODO write fPosition and fFontSize here ...
60 fRenderData->PushV(0.f, 0.f, 0.f); // write floats so the data is not empty
61}
62
63////////////////////////////////////////////////////////////////////////////////
64/// Compute bounding-box of the data.
65
67{
68 //BBoxInit();
69}
70
71
72#include "ROOT/REveManager.hxx"
73#include "TSystem.h"
74#include "TROOT.h"
75#include "TEnv.h"
76
77std::string REveText::sSdfFontDir;
78
79////////////////////////////////////////////////////////////////////////////////
80/// Set location where SDF fonts and their metrics data are stored or are to be
81/// created via the AssertSdfFont() static function.
82/// If require_write_access is true (default), write permission in the directory
83// dir is required.
84/// REveManager needs to be created before calling this function.
85/// Static function.
86
87bool REveText::SetSdfFontDir(std::string_view dir, bool require_write_access)
88{
89 static const char* tpfx = "REveText::SetSdfFontDir";
90
91 if (gEve == nullptr) {
92 ::Error(tpfx, "REveManager needs to be initialized before font setup can begin.");
93 return false;
94 }
95
96 std::string sanitized_dir(dir);
97 if (sanitized_dir.back() != '/')
98 sanitized_dir += '/';
99 if (gSystem->AccessPathName(sanitized_dir.data())) {
100 if (gSystem->mkdir(sanitized_dir.data(), true)) {
101 ::Error(tpfx, "Directory does not exist and mkdir failed for '%s", dir.data());
102 return false;
103 }
104 }
105 auto dir_perms = require_write_access ? kWritePermission : kReadPermission;
106 if (gSystem->AccessPathName(sanitized_dir.data(), dir_perms) == false) {
107 sSdfFontDir = sanitized_dir;
108 gEve->AddLocation("sdf-fonts/", sSdfFontDir.data());
109 return true;
110 } else {
111 return false;
112 }
113}
114
115////////////////////////////////////////////////////////////////////////////////
116/// Set default SDF font directory based on write permissions in $ROOTSYS and
117/// in the current working directory.
118/// Alternative fallback to /tmp or user's home directory is not attempted.
119
121{
122 static const char* tpfx = "REveText::SetDefaultSdfFontDir";
123
124 static bool s_font_init_failed = false;
125
126 if (s_font_init_failed) {
127 return false;
128 }
129
130 std::string dir( gEnv->GetValue("WebGui.RootUi5Path", gSystem->ExpandPathName("${ROOTSYS}/ui5")) );
131 s_font_init_failed = true;
132 if (SetSdfFontDir(dir + "/eve7/sdf-fonts/")) {
133 ::Info(tpfx, "Using install-wide SDF font dir $ROOTSYS/ui5/eve7/sdf-fonts");
134 } else if (SetSdfFontDir("./sdf-fonts/")) {
135 ::Info(tpfx, "Using SDF font dir sdf_fonts/ in current directory");
136 } else {
137 ::Error(tpfx, "Error setting up default SDF font dir. "
138 "Please set it manually through REveText::SetSdfFontDir(<dir-name>)");
139 return false;
140 }
141 s_font_init_failed = false;
142
143 return true;
144}
145
146////////////////////////////////////////////////////////////////////////////////
147/// Check if font exists, otherwise try to create it.
148/// If SDF font dir is not yet set, an attempt will be made to set it to
149/// one of the default locations, in $ROOTSYS or in the current working directory.
150/// Returns true if font files are present, false otherwise.
151/// Static function.
152
153bool REveText::AssertSdfFont(std::string_view font_name, std::string_view ttf_font)
154{
155 static const char* tpfx = "REveText::AssertSdfFont";
156
157 if (sSdfFontDir.empty() && ! SetDefaultSdfFontDir()) {
158 return false;
159 }
160
161 std::string base = sSdfFontDir + font_name.data();
162 std::string png = base + ".png";
163 std::string js = base + ".js.gz";
164
165 if (gSystem->AccessPathName(png.data()) || gSystem->AccessPathName(js.data())) {
166 if (gSystem->AccessPathName(ttf_font.data())) {
167 ::Warning(tpfx, "Source TTF font '%s' not found.", ttf_font.data());
168 return false;
169 }
170 // Invoke through interpreter to avoid REve dependece on RGL.
171 char command[8192];
172 int cl = snprintf(command, 8192, "TGLSdfFontMaker::MakeFont(\"%s\", \"%s\");",
173 ttf_font.data(), base.data());
174 if (cl < 0) {
175 ::Warning(tpfx, "Error generating interpreter command for TGLSdfFontMaker::MakeFont(), ret=%d.", cl);
176 return false;
177 }
178#ifdef WIN32
179 while (--cl >= 0) if (command[cl] == '\\') command[cl] = '/';
180#endif
181 gROOT->ProcessLine(command);
182 if (gSystem->AccessPathName(png.data()) || gSystem->AccessPathName(js.data())) {
183 ::Warning(tpfx, "Creation of font '%s' failed.", font_name.data());
184 return false;
185 }
186 }
187 return true;
188}
char Text_t
Definition RtypesCore.h:62
R__EXTERN TEnv * gEnv
Definition TEnv.h:170
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
Definition TError.cxx:218
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
@ kReadPermission
Definition TSystem.h:45
@ kWritePermission
Definition TSystem.h:44
R__EXTERN TSystem * gSystem
Definition TSystem.h:555
#define snprintf
Definition civetweb.c:1540
std::unique_ptr< REveRenderData > fRenderData
Externally assigned and controlled user data.
virtual void BuildRenderData()
Write transformation Matrix to render data.
void AddLocation(const std::string &name, const std::string &path)
Register new directory to THttpServer.
Int_t WriteCoreJson(nlohmann::json &j, Int_t rnr_offset) override
Fill core part of JSON representation.
Definition REveShape.cxx:51
static bool AssertSdfFont(std::string_view font_name, std::string_view ttf_font)
Check if font exists, otherwise try to create it.
Definition REveText.cxx:153
static std::string sSdfFontDir
Definition REveText.hxx:42
REveText(const REveText &)=delete
Int_t WriteCoreJson(nlohmann::json &j, Int_t rnr_offset) override
Fill core part of JSON representation.
Definition REveText.cxx:34
void ComputeBBox() override
Compute bounding-box of the data.
Definition REveText.cxx:66
static bool SetDefaultSdfFontDir()
Set default SDF font directory based on write permissions in $ROOTSYS and in the current working dire...
Definition REveText.cxx:120
static bool SetSdfFontDir(std::string_view dir, bool require_write_access=true)
Set location where SDF fonts and their metrics data are stored or are to be created via the AssertSdf...
Definition REveText.cxx:87
void BuildRenderData() override
Crates 3D point array for rendering.
Definition REveText.cxx:55
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition TEnv.cxx:491
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition TSystem.cxx:1274
virtual int mkdir(const char *name, Bool_t recursive=kFALSE)
Make a file system directory.
Definition TSystem.cxx:906
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition TSystem.cxx:1296
const Int_t n
Definition legend1.C:16
R__EXTERN REveManager * gEve