Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TWebPainting.cxx
Go to the documentation of this file.
1// Author: Sergey Linev, GSI 10/04/2017
2
3/*************************************************************************
4 * Copyright (C) 1995-2018, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#include "TWebPainting.h"
12
13#include "TColor.h"
14#include "TColorGradient.h"
15#include "TBufferJSON.h"
16
17/** \class TWebPainting
18\ingroup webgui6
19
20Object used to store paint operations and deliver them to JSROOT
21
22*/
23
24
25///////////////////////////////////////////////////////////////////////////////////////
26/// Constructor
27
29{
33}
34
35///////////////////////////////////////////////////////////////////////////////////////
36/// Add next custom operator to painting
37/// Operations are separated by semicolons
38/// Following operations are supported:
39/// t - text
40/// h - text coded into simple hex
41/// r - rectangle
42/// b - rectangular fill region
43/// l - polyline
44/// f - poly fill region
45/// m - poly marker
46/// z - line attributes
47/// y - fill attributes
48/// x - marker attributes
49/// o - text attributes
50/// After operation code optional arguments can be append like length of operation or coded text
51/// Each operation may use data from binary float buffer
52
53void TWebPainting::AddOper(const std::string &oper)
54{
55 if (!fOper.empty())
56 fOper.append(";");
57 fOper.append(oper);
58}
59
60///////////////////////////////////////////////////////////////////////////////////////
61/// Create text operation
62/// If text include special symbols - use simple hex coding
63
64std::string TWebPainting::MakeTextOper(const char *str)
65{
66 bool isany_special = false;
67 if (!str)
68 str = "";
69 for (auto p = str; *p; ++p) {
70 if ((*p < 32) || (*p > 126) || (*p == ';') || (*p == '\'') || (*p == '\"') || (*p == '%')) {
71 isany_special = true;
72 break;
73 }
74 }
75
76 if (!isany_special)
77 return std::string("t") + str;
78
79 // use simple hex coding while special symbols are hard to handle
80 std::string oper("h");
81 static const char *digits = "0123456789abcdef";
82 for (auto p = str; *p; ++p) {
83 unsigned code = (unsigned)*p;
84 oper.append(1, digits[(code >> 4) & 0xF]);
85 oper.append(1, digits[code & 0xF]);
86 }
87 return oper;
88}
89
90///////////////////////////////////////////////////////////////////////////////////////
91/// Reserve place in the float buffer
92/// Returns pointer on first element in reserved area
93
95{
96 if (sz <= 0)
97 return nullptr;
98
99 if (fSize + sz > fBuf.GetSize()) {
100 Int_t nextsz = fBuf.GetSize() + TMath::Max(1024, (sz/128 + 1) * 128);
101 fBuf.Set(nextsz);
102 }
103
104 Float_t *res = fBuf.GetArray() + fSize;
105 fSize += sz;
106 return res; // return size where drawing can start
107}
108
109///////////////////////////////////////////////////////////////////////////////////////
110/// Store line attributes
111/// If attributes were not changed - ignore operation
112
114{
115 if ((attr.GetLineColor() == fLastLine.GetLineColor()) &&
116 (attr.GetLineStyle() == fLastLine.GetLineStyle()) &&
117 (attr.GetLineWidth() == fLastLine.GetLineWidth())) return;
118
119 fLastLine = attr;
120
121 AddOper(std::string("z") +
122 std::to_string((int) attr.GetLineColor()) + ":" +
123 std::to_string((int) attr.GetLineStyle()) + ":" +
124 std::to_string((int) attr.GetLineWidth()));
125}
126
127///////////////////////////////////////////////////////////////////////////////////////
128/// Store fill attributes
129/// If attributes were not changed - ignore operation
130
132{
133 if ((fLastFill.GetFillColor() == attr.GetFillColor()) &&
134 (fLastFill.GetFillStyle() == attr.GetFillStyle())) return;
135
136 fLastFill = attr;
137
138 AddOper(std::string("y") +
139 std::to_string((int) attr.GetFillColor()) + ":" +
140 std::to_string((int) attr.GetFillStyle()));
141}
142
143///////////////////////////////////////////////////////////////////////////////////////
144/// Store text attributes
145/// If attributes were not changed - ignore operation
146
148{
149 AddOper(std::string("o") +
150 std::to_string((int) attr.GetTextColor()) + ":" +
151 std::to_string((int) attr.GetTextFont()) + ":" +
152 std::to_string((int) (attr.GetTextSize()>=1 ? attr.GetTextSize() : -1000*attr.GetTextSize())) + ":" +
153 std::to_string((int) attr.GetTextAlign()) + ":" +
154 std::to_string((int) attr.GetTextAngle()));
155}
156
157///////////////////////////////////////////////////////////////////////////////////////
158/// Store marker attributes
159/// If attributes were not changed - ignore operation
160
162{
163 if ((attr.GetMarkerColor() == fLastMarker.GetMarkerColor()) &&
164 (attr.GetMarkerStyle() == fLastMarker.GetMarkerStyle()) &&
165 (attr.GetMarkerSize() == fLastMarker.GetMarkerSize())) return;
166
168
169 AddOper(std::string("x") +
170 std::to_string((int) attr.GetMarkerColor()) + ":" +
171 std::to_string((int) attr.GetMarkerStyle()) + ":" +
172 std::to_string((int) attr.GetMarkerSize()));
173}
174
175///////////////////////////////////////////////////////////////////////////////////////
176/// Add custom color to operations
177
179{
180 if (!col) return;
181
182 TString code;
183 if ((col->IsA() == TLinearGradient::Class()) || (col->IsA() == TRadialGradient::Class())) {
184
185 auto grad = dynamic_cast<TColorGradient *>(col);
186 auto linear = dynamic_cast<TLinearGradient *> (col);
187 auto radial = dynamic_cast<TRadialGradient *> (col);
188
189 std::vector<double> arr;
190
191 if (radial)
192 arr.emplace_back((int) radial->GetGradientType());
193 else if (linear)
194 arr.emplace_back(10);
195 else
196 arr.emplace_back(-1);
197 arr.emplace_back((int) grad->GetCoordinateMode());
198 arr.emplace_back((int) grad->GetNumberOfSteps());
199 for (unsigned n = 0; n < grad->GetNumberOfSteps(); ++n)
200 arr.emplace_back(grad->GetColorPositions()[n]);
201 for (unsigned n = 0; n < grad->GetNumberOfSteps()*4; ++n)
202 arr.emplace_back(grad->GetColors()[n]);
203
204 if (linear) {
205 arr.emplace_back(linear->GetStart().fX);
206 arr.emplace_back(linear->GetStart().fY);
207 arr.emplace_back(linear->GetEnd().fX);
208 arr.emplace_back(linear->GetEnd().fY);
209 } else if (radial) {
210 arr.emplace_back(radial->GetStart().fX);
211 arr.emplace_back(radial->GetStart().fY);
212 arr.emplace_back(radial->GetEnd().fX);
213 arr.emplace_back(radial->GetEnd().fY);
214 arr.emplace_back(radial->GetR1());
215 arr.emplace_back(radial->GetR2());
216 }
217
219
220 code.Form("%d#%s", indx, json.Data());
221
222 } else if (col->GetAlpha() == 1)
223 code.Form("%d:%d,%d,%d", indx, (int) (255*col->GetRed()), (int) (255*col->GetGreen()), (int) (255*col->GetBlue()));
224 else
225 code.Form("%d=%d,%d,%d,%5.3f", indx, (int) (255*col->GetRed()), (int) (255*col->GetGreen()), (int) (255*col->GetBlue()), col->GetAlpha());
226
227 AddOper(code.Data());
228}
229
nlohmann::json json
float Float_t
Definition RtypesCore.h:57
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 attr
const Float_t * GetArray() const
Definition TArrayF.h:43
void Set(Int_t n) override
Set size of this array to n floats.
Definition TArrayF.cxx:105
Int_t GetSize() const
Definition TArray.h:47
Fill Area Attributes class.
Definition TAttFill.h:19
virtual Color_t GetFillColor() const
Return the fill area color.
Definition TAttFill.h:30
virtual Style_t GetFillStyle() const
Return the fill area style.
Definition TAttFill.h:31
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition TAttFill.h:39
Line Attributes class.
Definition TAttLine.h:18
virtual Color_t GetLineColor() const
Return the line color.
Definition TAttLine.h:33
virtual Width_t GetLineWidth() const
Return the line width.
Definition TAttLine.h:35
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:43
virtual Style_t GetLineStyle() const
Return the line style.
Definition TAttLine.h:34
Marker Attributes class.
Definition TAttMarker.h:19
virtual Style_t GetMarkerStyle() const
Return the marker style.
Definition TAttMarker.h:32
virtual Color_t GetMarkerColor() const
Return the marker color.
Definition TAttMarker.h:31
virtual Size_t GetMarkerSize() const
Return the marker size.
Definition TAttMarker.h:33
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:40
Text Attributes class.
Definition TAttText.h:18
static TString ToJSON(const T *obj, Int_t compact=0, const char *member_name=nullptr)
Definition TBufferJSON.h:75
@ kNoSpaces
no new lines plus remove all spaces around "," and ":" symbols
Definition TBufferJSON.h:39
TColorGradient extends basic TColor.
The color creation and management class.
Definition TColor.h:21
TClass * IsA() const override
Definition TColor.h:111
Float_t GetRed() const
Definition TColor.h:60
Float_t GetAlpha() const
Definition TColor.h:66
Float_t GetBlue() const
Definition TColor.h:62
Float_t GetGreen() const
Definition TColor.h:61
static TClass * Class()
static TClass * Class()
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2356
void AddColor(Int_t indx, TColor *col)
Add custom color to operations.
TWebPainting()
Constructor.
void AddTextAttr(const TAttText &attr)
Store text attributes If attributes were not changed - ignore operation.
TAttMarker fLastMarker
! last marker attributes
void AddMarkerAttr(const TAttMarker &attr)
Store marker attributes If attributes were not changed - ignore operation.
void AddLineAttr(const TAttLine &attr)
Store line attributes If attributes were not changed - ignore operation.
static std::string MakeTextOper(const char *str)
Create text operation If text include special symbols - use simple hex coding.
Int_t fSize
! filled buffer size
TAttFill fLastFill
! last fill attributes
std::string fOper
list of operations, separated by semicolons
void AddOper(const std::string &oper)
Add next custom operator to painting Operations are separated by semicolons Following operations are ...
Float_t * Reserve(Int_t sz)
Reserve place in the float buffer Returns pointer on first element in reserved area.
TArrayF fBuf
array of points for all operations
TAttLine fLastLine
! last line attributes
void AddFillAttr(const TAttFill &attr)
Store fill attributes If attributes were not changed - ignore operation.
const Int_t n
Definition legend1.C:16
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition TMathBase.h:250