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
34////////////////////////////////////////////////////////////////////////////////
35/// Make this text a button: a click that does not become a drag sends `mir`
36/// to `target`, or to this element if null. The effect reaches every client.
37/// A target that is an REveAunt is tracked, and its destruction clears the
38/// action. Any other target is stored by id only.
39
40void REveText::SetClickAction(const std::string &mir, REveElement *target)
41{
42 if (fClickAunt) {
44 fClickAunt = nullptr;
45 }
46
47 fClickMir = mir;
48 fClickTargetId = target ? target->GetElementId() : 0;
49
50 if (target) {
51 if (auto au = dynamic_cast<REveAunt *>(target)) {
52 au->AddNiece(this);
53 fClickAunt = au;
54 }
55 }
57}
58
59////////////////////////////////////////////////////////////////////////////////
60/// Clear the click action when the click target is destroyed, so the button
61/// does not keep an id that may be reused by an unrelated element.
62
64{
65 if (au == fClickAunt) {
66 fClickAunt = nullptr;
67 fClickMir.clear();
70 }
72}
73
74Int_t REveText::WriteCoreJson(nlohmann::json &j, Int_t rnr_offset)
75{
76 Int_t ret = REveShape::WriteCoreJson(j, rnr_offset);
77
78 j["fText"] = fText;
79 j["fFont"] = fFont;
80 j["fPosX"] = fPosition.fX;
81 j["fPosY"] = fPosition.fY;
82 j["fPosZ"] = fPosition.fZ;
83 j["fFontSize"] = fFontSize;
84 j["fFontHinting"] = fFontHinting;
85 j["fFontWeight"] = fFontWeight;
86 j["fExtraBorder"] = fExtraBorder;
87 j["fMode"] = fMode;
88 j["fResizable"] = fResizable;
89 j["fAlignH"] = fAlignH;
90 j["fAlignV"] = fAlignV;
91 j["fClickMir"] = fClickMir;
92 j["fClickTargetId"] = fClickTargetId;
93 j["fTextColor"] = fTextColor;
94
95 return ret;
96}
97
98////////////////////////////////////////////////////////////////////////////////
99/// Crates 3D point array for rendering.
100
102{
103 fRenderData = std::make_unique<REveRenderData>("makeZText");
105 // TODO write fPosition and fFontSize here ...
106 fRenderData->PushV(0.f, 0.f, 0.f); // write floats so the data is not empty
107}
108
109////////////////////////////////////////////////////////////////////////////////
110/// Compute bounding-box of the data.
111
113{
114 //BBoxInit();
115}
116
117
118#include "ROOT/REveManager.hxx"
119#include "TSystem.h"
120#include "TROOT.h"
121#include "TEnv.h"
122
123std::string REveText::sSdfFontDir;
124
125////////////////////////////////////////////////////////////////////////////////
126/// Set location where SDF fonts and their metrics data are stored or are to be
127/// created via the AssertSdfFont() static function.
128/// If require_write_access is true (default), write permission in the directory
129// dir is required.
130/// REveManager needs to be created before calling this function.
131/// Static function.
132
133bool REveText::SetSdfFontDir(const std::string &dir, bool require_write_access)
134{
135 static const char* tpfx = "REveText::SetSdfFontDir";
136
137 if (gEve == nullptr) {
138 ::Error(tpfx, "REveManager needs to be initialized before font setup can begin.");
139 return false;
140 }
141
142 std::string sanitized_dir(dir);
143 if (sanitized_dir.back() != '/')
144 sanitized_dir += '/';
145 if (gSystem->AccessPathName(sanitized_dir.data())) {
146 if (gSystem->mkdir(sanitized_dir.data(), true)) {
147 ::Error(tpfx, "Directory does not exist and mkdir failed for '%s", dir.data());
148 return false;
149 }
150 }
151 auto dir_perms = require_write_access ? kWritePermission : kReadPermission;
152 if (gSystem->AccessPathName(sanitized_dir.data(), dir_perms) == false) {
153 sSdfFontDir = sanitized_dir;
154 gEve->AddLocation("sdf-fonts/", sSdfFontDir.data());
155 return true;
156 } else {
157 return false;
158 }
159}
160
161////////////////////////////////////////////////////////////////////////////////
162/// Set default SDF font directory based on write permissions in the ui5
163/// directory, WebGui.RootUi5Path or else $ROOTSYS/ui5, and in the current
164/// working directory.
165/// Alternative fallback to /tmp or user's home directory is not attempted.
166/// Needs REveManager; called before it exists, it fails and can be retried.
167
169{
170 static const char* tpfx = "REveText::SetDefaultSdfFontDir";
171
172 static bool s_font_init_failed = false;
173
174 if (s_font_init_failed) {
175 return false;
176 }
177
178 // Both directories are registered with the manager. Without one, fail but
179 // do not mark the setup as failed, so a call after Create() can succeed.
180 if (gEve == nullptr) {
181 static bool s_have_warned = false;
182 if (!s_have_warned) {
183 ::Error(tpfx, "REveManager needs to be initialized before font setup can begin.");
184 s_have_warned = true;
185 }
186 return false;
187 }
188
189 std::string dir( gEnv->GetValue("WebGui.RootUi5Path", gSystem->ExpandPathName("${ROOTSYS}/ui5")) );
190 dir += "/eve7/sdf-fonts/";
191 s_font_init_failed = true;
192 if (SetSdfFontDir(dir)) {
193 ::Info(tpfx, "Using SDF font dir %s", dir.c_str());
194 } else if (SetSdfFontDir("./sdf-fonts/")) {
195 ::Info(tpfx, "Using SDF font dir sdf_fonts/ in current directory");
196 } else {
197 ::Error(tpfx, "Error setting up default SDF font dir. "
198 "Please set it manually through REveText::SetSdfFontDir(<dir-name>)");
199 return false;
200 }
201 s_font_init_failed = false;
202
203 return true;
204}
205
206////////////////////////////////////////////////////////////////////////////////
207/// Check if font exists, otherwise try to create it.
208/// If SDF font dir is not yet set, an attempt will be made to set it to
209/// one of the default locations, in $ROOTSYS or in the current working directory.
210/// Returns true if font files are present, false otherwise.
211/// Static function.
212
213bool REveText::AssertSdfFont(const std::string &font_name, const std::string &ttf_font)
214{
215 static const char* tpfx = "REveText::AssertSdfFont";
216
217 if (sSdfFontDir.empty() && ! SetDefaultSdfFontDir()) {
218 return false;
219 }
220
221 std::string base = sSdfFontDir + font_name.data();
222 std::string png = base + ".png";
223 std::string js = base + ".js.gz";
224
225 if (gSystem->AccessPathName(png.data()) || gSystem->AccessPathName(js.data())) {
226 if (gSystem->AccessPathName(ttf_font.data())) {
227 ::Warning(tpfx, "Source TTF font '%s' not found.", ttf_font.data());
228 return false;
229 }
230 // Invoke through interpreter to avoid REve dependece on RGL.
231 char command[8192];
232 int cl = snprintf(command, 8192, "TGLSdfFontMaker::MakeFont(\"%s\", \"%s\");",
233 ttf_font.data(), base.data());
234 if (cl < 0) {
235 ::Warning(tpfx, "Error generating interpreter command for TGLSdfFontMaker::MakeFont(), ret=%d.", cl);
236 return false;
237 }
238#ifdef WIN32
239 while (--cl >= 0) if (command[cl] == '\\') command[cl] = '/';
240#endif
241 gROOT->ProcessLine(command);
242 if (gSystem->AccessPathName(png.data()) || gSystem->AccessPathName(js.data())) {
243 ::Warning(tpfx, "Creation of font '%s' failed.", font_name.data());
244 return false;
245 }
246 }
247 return true;
248}
char Text_t
General string (char)
Definition RtypesCore.h:77
R__EXTERN TEnv * gEnv
Definition TEnv.h:126
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
Definition TError.cxx:241
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:208
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:252
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 target
#define gROOT
Definition TROOT.h:417
@ kReadPermission
Definition TSystem.h:55
@ kWritePermission
Definition TSystem.h:54
R__EXTERN TSystem * gSystem
Definition TSystem.h:582
virtual void RemoveNiece(REveElement *el)
virtual void RemoveAunt(REveAunt *au)
Remove el from the list of aunts.
std::unique_ptr< REveRenderData > fRenderData
! Vertex / normal / triangle index information for rendering.
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 std::string sSdfFontDir
Definition REveText.hxx:53
REveText(const REveText &)=delete
Int_t WriteCoreJson(nlohmann::json &j, Int_t rnr_offset) override
Fill core part of JSON representation.
Definition REveText.cxx:74
void ComputeBBox() override
Compute bounding-box of the data.
Definition REveText.cxx:112
std::string fFont
Default must be a face ROOT ships: only Liberation Mono and Serif are in $ROOTSYS/fonts,...
Definition REveText.hxx:36
static bool SetDefaultSdfFontDir()
Set default SDF font directory based on write permissions in the ui5 directory, WebGui....
Definition REveText.cxx:168
void SetClickAction(const std::string &mir, REveElement *target=nullptr)
Fill core part of JSON representation.
Definition REveText.cxx:40
void RemoveAunt(REveAunt *au) override
Clear the click action when the click target is destroyed, so the button does not keep an id that may...
Definition REveText.cxx:63
static bool AssertSdfFont(const std::string &font_name, const std::string &ttf_font)
Check if font exists, otherwise try to create it.
Definition REveText.cxx:213
Color_t fTextColor
target held as an aunt, so its death is noticed
Definition REveText.hxx:49
static bool SetSdfFontDir(const std::string &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:133
void BuildRenderData() override
Crates 3D point array for rendering.
Definition REveText.cxx:101
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition TEnv.cxx:511
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition TSystem.cxx:1296
virtual int mkdir(const char *name, Bool_t recursive=kFALSE)
Make a file system directory.
Definition TSystem.cxx:921
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:1318
const Int_t n
Definition legend1.C:16
Namespace for ROOT features in testing.
Definition TROOT.h:100
R__EXTERN REveManager * gEve