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/// Text has no box: its extent is known only to the client, which lays it
111/// out. It is not culled.
112
114{
116}
117
118
119#include "ROOT/REveManager.hxx"
120#include "TSystem.h"
121#include "TROOT.h"
122#include "TEnv.h"
123
124std::string REveText::sSdfFontDir;
125
126////////////////////////////////////////////////////////////////////////////////
127/// Set location where SDF fonts and their metrics data are stored or are to be
128/// created via the AssertSdfFont() static function.
129/// If require_write_access is true (default), write permission in the directory
130// dir is required.
131/// REveManager needs to be created before calling this function.
132/// Static function.
133
134bool REveText::SetSdfFontDir(const std::string &dir, bool require_write_access)
135{
136 static const char* tpfx = "REveText::SetSdfFontDir";
137
138 if (gEve == nullptr) {
139 ::Error(tpfx, "REveManager needs to be initialized before font setup can begin.");
140 return false;
141 }
142
143 std::string sanitized_dir(dir);
144 if (sanitized_dir.back() != '/')
145 sanitized_dir += '/';
146 if (gSystem->AccessPathName(sanitized_dir.data())) {
147 if (gSystem->mkdir(sanitized_dir.data(), true)) {
148 ::Error(tpfx, "Directory does not exist and mkdir failed for '%s", dir.data());
149 return false;
150 }
151 }
152 auto dir_perms = require_write_access ? kWritePermission : kReadPermission;
153 if (gSystem->AccessPathName(sanitized_dir.data(), dir_perms) == false) {
154 sSdfFontDir = sanitized_dir;
155 gEve->AddLocation("sdf-fonts/", sSdfFontDir.data());
156 return true;
157 } else {
158 return false;
159 }
160}
161
162////////////////////////////////////////////////////////////////////////////////
163/// Set default SDF font directory based on write permissions in the ui5
164/// directory, WebGui.RootUi5Path or else $ROOTSYS/ui5, and in the current
165/// working directory.
166/// Alternative fallback to /tmp or user's home directory is not attempted.
167/// Needs REveManager; called before it exists, it fails and can be retried.
168
170{
171 static const char* tpfx = "REveText::SetDefaultSdfFontDir";
172
173 static bool s_font_init_failed = false;
174
175 if (s_font_init_failed) {
176 return false;
177 }
178
179 // Both directories are registered with the manager. Without one, fail but
180 // do not mark the setup as failed, so a call after Create() can succeed.
181 if (gEve == nullptr) {
182 static bool s_have_warned = false;
183 if (!s_have_warned) {
184 ::Error(tpfx, "REveManager needs to be initialized before font setup can begin.");
185 s_have_warned = true;
186 }
187 return false;
188 }
189
190 std::string dir( gEnv->GetValue("WebGui.RootUi5Path", gSystem->ExpandPathName("${ROOTSYS}/ui5")) );
191 dir += "/eve7/sdf-fonts/";
192 s_font_init_failed = true;
193 if (SetSdfFontDir(dir)) {
194 ::Info(tpfx, "Using SDF font dir %s", dir.c_str());
195 } else if (SetSdfFontDir("./sdf-fonts/")) {
196 ::Info(tpfx, "Using SDF font dir sdf_fonts/ in current directory");
197 } else {
198 ::Error(tpfx, "Error setting up default SDF font dir. "
199 "Please set it manually through REveText::SetSdfFontDir(<dir-name>)");
200 return false;
201 }
202 s_font_init_failed = false;
203
204 return true;
205}
206
207////////////////////////////////////////////////////////////////////////////////
208/// Check if font exists, otherwise try to create it.
209/// If SDF font dir is not yet set, an attempt will be made to set it to
210/// one of the default locations, in $ROOTSYS or in the current working directory.
211/// Returns true if font files are present, false otherwise.
212/// Static function.
213
214bool REveText::AssertSdfFont(const std::string &font_name, const std::string &ttf_font)
215{
216 static const char* tpfx = "REveText::AssertSdfFont";
217
218 if (sSdfFontDir.empty() && ! SetDefaultSdfFontDir()) {
219 return false;
220 }
221
222 std::string base = sSdfFontDir + font_name.data();
223 std::string png = base + ".png";
224 std::string js = base + ".js.gz";
225
226 if (gSystem->AccessPathName(png.data()) || gSystem->AccessPathName(js.data())) {
227 if (gSystem->AccessPathName(ttf_font.data())) {
228 ::Warning(tpfx, "Source TTF font '%s' not found.", ttf_font.data());
229 return false;
230 }
231 // Invoke through interpreter to avoid REve dependece on RGL.
232 char command[8192];
233 int cl = snprintf(command, 8192, "TGLSdfFontMaker::MakeFont(\"%s\", \"%s\");",
234 ttf_font.data(), base.data());
235 if (cl < 0) {
236 ::Warning(tpfx, "Error generating interpreter command for TGLSdfFontMaker::MakeFont(), ret=%d.", cl);
237 return false;
238 }
239#ifdef WIN32
240 while (--cl >= 0) if (command[cl] == '\\') command[cl] = '/';
241#endif
242 gROOT->ProcessLine(command);
243 if (gSystem->AccessPathName(png.data()) || gSystem->AccessPathName(js.data())) {
244 ::Warning(tpfx, "Creation of font '%s' failed.", font_name.data());
245 return false;
246 }
247 }
248 return true;
249}
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()
Build fRenderData, what the client draws.
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
Text has no box: its extent is known only to the client, which lays it out.
Definition REveText.cxx:113
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:169
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:214
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:134
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