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#include "REveAssetDir.hxx"
119
120#include "TSystem.h"
121#include "TROOT.h"
122
123#include <set>
124
126
127std::string REveText::sSdfFontDir;
128
129namespace {
130
131////////////////////////////////////////////////////////////////////////////////
132/// The SDF font directories, served under "sdf-fonts/". The fallback is the
133/// directory of the fonts shipped with ROOT.
134
135REveAssetDir &SdfFontDirs()
136{
137 static REveAssetDir s_dirs("sdf-fonts/");
138 static bool s_have_fallback = false;
139 if (!s_have_fallback) {
140 s_have_fallback = true;
141 s_dirs.AddFallbackDir(REveAssetDir::GetAssetsDir() + "sdf-fonts/");
142 }
143 return s_dirs;
144}
145
146} // namespace
147
148////////////////////////////////////////////////////////////////////////////////
149/// Set location where SDF fonts and their metrics data are stored or are to be
150/// created via the AssertSdfFont() static function.
151/// The directory is created if it does not exist.
152/// If `require_write_access` is true (default), write permission in the
153/// directory is required, else read permission.
154/// The directory replaces the ones added with AddSdfFontDir(). The fonts shipped
155/// with ROOT are found after it.
156/// Can be called before REveManager::Create().
157/// Static function.
158
159bool REveText::SetSdfFontDir(const std::string &dir, bool require_write_access)
160{
161 static const char* tpfx = "REveText::SetSdfFontDir";
162
163 if (dir.empty()) {
164 ::Error(tpfx, "Empty directory given.");
165 return false;
166 }
167
168 std::string sanitized_dir = REveAssetDir::NormalizeDir(dir);
169 if (gSystem->AccessPathName(sanitized_dir.data())) {
170 if (gSystem->mkdir(sanitized_dir.data(), true)) {
171 ::Error(tpfx, "Directory does not exist and mkdir failed for '%s", dir.data());
172 return false;
173 }
174 }
175 auto dir_perms = require_write_access ? kWritePermission : kReadPermission;
176 if (gSystem->AccessPathName(sanitized_dir.data(), dir_perms) == false) {
177 sSdfFontDir = sanitized_dir;
178 auto &fonts = SdfFontDirs();
179 fonts.SetWriteDir(sSdfFontDir);
180 fonts.SetUserDir(sSdfFontDir);
181 return true;
182 } else {
183 return false;
184 }
185}
186
187////////////////////////////////////////////////////////////////////////////////
188/// Add a directory fonts are read from, searched after the one of
189/// SetSdfFontDir() and before the fonts shipped with ROOT. Fonts are not
190/// generated into it. If no font directory is set, the default one is set first.
191/// Can be called before REveManager::Create().
192/// Static function.
193
194bool REveText::AddSdfFontDir(const std::string &dir)
195{
196 static const char *tpfx = "REveText::AddSdfFontDir";
197
198 std::string sanitized_dir = REveAssetDir::NormalizeDir(dir);
199 if (sanitized_dir.empty() || gSystem->AccessPathName(sanitized_dir.data(), kReadPermission)) {
200 ::Error(tpfx, "SDF font directory '%s' does not exist or is not readable.", dir.data());
201 return false;
202 }
203 if (sSdfFontDir.empty())
205 SdfFontDirs().AddUserDir(sanitized_dir);
206 return true;
207}
208
209////////////////////////////////////////////////////////////////////////////////
210/// Set the default SDF font directory: sdf-fonts/ in the ui5 directory if it is
211/// writable or can be created, else sdf-fonts/ in the current working directory.
212/// The latter is created only when a font is generated into it.
213/// The fonts shipped with ROOT, in eve7/assets/sdf-fonts/ of the ui5 directory,
214/// are found after it.
215/// Alternative fallback to /tmp or user's home directory is not attempted.
216
218{
219 static const char *tpfx = "REveText::SetDefaultSdfFontDir";
220
221 std::string dir = REveAssetDir::GetUi5Dir() + "/eve7/sdf-fonts/";
222 bool usable = gSystem->AccessPathName(dir.c_str()) ? gSystem->mkdir(dir.c_str(), true) == 0
223 : !gSystem->AccessPathName(dir.c_str(), kWritePermission);
224 if (usable) {
225 ::Info(tpfx, "Using SDF font dir %s", dir.c_str());
226 } else {
227 dir = "./sdf-fonts/";
228 ::Info(tpfx, "Using SDF font dir sdf-fonts/ in current directory");
229 }
230
231 sSdfFontDir = dir;
232 auto &fonts = SdfFontDirs();
233 fonts.SetWriteDir(dir);
234 fonts.SetUserDir(dir);
235
236 return true;
237}
238
239////////////////////////////////////////////////////////////////////////////////
240/// Check if font exists, otherwise try to create it.
241/// The font is looked up in the directories of SetSdfFontDir() and
242/// AddSdfFontDir(), then in the fonts shipped with ROOT. If SDF font dir is not
243/// yet set, it is set to the default, see SetDefaultSdfFontDir().
244/// The font is present when the first directory that has its .png or its .js.gz
245/// has both, as the server serves each file from the first directory that has it.
246/// A missing font is generated into the directory of SetSdfFontDir().
247/// In batch mode a missing font is not generated, as that needs a display.
248/// Returns true if font files are present, false otherwise.
249/// Static function.
250
251bool REveText::AssertSdfFont(const std::string &font_name, const std::string &ttf_font)
252{
253 static const char* tpfx = "REveText::AssertSdfFont";
254
255 if (sSdfFontDir.empty())
257
258 auto &fonts = SdfFontDirs();
259 std::string png = font_name + ".png";
260 std::string js = font_name + ".js.gz";
261
262 if (fonts.FindPair(png, js).empty()) {
263 // A lone file, or two files in different directories, would be served as a mismatched pair.
264 std::string at_png = fonts.Find(png), at_js = fonts.Find(js), split;
265 auto where = [](const std::string &f, const std::string &p) {
266 return f + (p.empty() ? " not found" : " found at '" + p + "'");
267 };
268 if (!at_png.empty() || !at_js.empty())
269 split = " Its two files must be in one directory: " + where(png, at_png) + ", " + where(js, at_js) + ".";
270 if (gSystem->AccessPathName(ttf_font.data())) {
271 ::Warning(tpfx, "Source TTF font '%s' not found.%s", ttf_font.data(), split.data());
272 return false;
273 }
274 // Generating a font needs a GL context, and TGLWidget throws without a display.
275 if (gROOT->IsBatch()) {
276 static std::set<std::string> s_warned_fonts;
277 if (s_warned_fonts.insert(font_name).second) {
278 if (split.empty())
279 ::Warning(tpfx,
280 "SDF font '%s' is not in '%s' and is not generated in batch mode. "
281 "Generating it needs a GL context, which needs a display. "
282 "Run once with a display, or copy %s.png and %s.js.gz into the SDF font directory.",
283 font_name.data(), sSdfFontDir.data(), font_name.data(), font_name.data());
284 else
285 ::Warning(tpfx, "SDF font '%s' is not used and is not generated in batch mode.%s", font_name.data(),
286 split.data());
287 }
288 return false;
289 }
290 std::string base = fonts.WritePath(font_name);
291 if (base.empty()) {
292 ::Warning(tpfx, "Cannot create SDF font directory '%s', font '%s' not generated.", fonts.GetWriteDir().data(),
293 font_name.data());
294 return false;
295 }
296 // Invoke through interpreter to avoid REve dependece on RGL.
297 char command[8192];
298 int cl = snprintf(command, 8192, "TGLSdfFontMaker::MakeFont(\"%s\", \"%s\");",
299 ttf_font.data(), base.data());
300 if (cl < 0) {
301 ::Warning(tpfx, "Error generating interpreter command for TGLSdfFontMaker::MakeFont(), ret=%d.", cl);
302 return false;
303 }
304#ifdef WIN32
305 while (--cl >= 0) if (command[cl] == '\\') command[cl] = '/';
306#endif
307 gROOT->ProcessLine(command);
308 // The write directory is the first user directory, so the generated pair is found and served first.
309 if (fonts.FindPair(png, js).empty()) {
310 ::Warning(tpfx, "Creation of font '%s' failed.", font_name.data());
311 return false;
312 }
313 }
314 return true;
315}
#define f(i)
Definition RSha256.hxx:104
char Text_t
General string (char)
Definition RtypesCore.h:77
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
winID h TVirtualViewer3D TVirtualGLPainter p
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
Directories of one kind of file the REve server serves under one http location prefix.
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.
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 the default SDF font directory: sdf-fonts/ in the ui5 directory if it is writable or can be creat...
Definition REveText.cxx:217
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:251
Color_t fTextColor
target held as an aunt, so its death is noticed
Definition REveText.hxx:49
static bool AddSdfFontDir(const std::string &dir)
Add a directory fonts are read from, searched after the one of SetSdfFontDir() and before the fonts s...
Definition REveText.cxx:194
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:159
void BuildRenderData() override
Crates 3D point array for rendering.
Definition REveText.cxx:101
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
TCanvas * fonts()
Definition fonts.C:10
const Int_t n
Definition legend1.C:16
Namespace for ROOT features in testing.
Definition TROOT.h:100