Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TWebPadPainter.cxx
Go to the documentation of this file.
1// Author: Sergey Linev, GSI 10/04/2017
2
3/*************************************************************************
4 * Copyright (C) 1995-2019, 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 "TWebPadPainter.h"
12#include "TError.h"
13#include "TImage.h"
14#include "TVirtualPad.h"
15#include "TWebCanvas.h"
16#include "TBufferJSON.h"
17
18
19/** \class TWebPadPainter
20\ingroup webgui6
21\brief Implement TVirtualPadPainter which abstracts painting operations.
22
23TWebPadPainter tries to support old Paint methods of the ROOT classes.
24Main classes (like histograms or graphs) should be painted on JavaScript side
25
26*/
27
28
29//////////////////////////////////////////////////////////////////////////
30/// Store operation identifier with appropriate attributes
31
32Float_t *TWebPadPainter::StoreOperation(const std::string &oper, unsigned attrkind, int opersize)
33{
34 if (!fPainting) return nullptr;
35
36 if (attrkind & attrLine)
37 fPainting->AddLineAttr(*this);
38
39 if (attrkind & attrFill)
40 fPainting->AddFillAttr(*this);
41
42 if (attrkind & attrMarker)
44
45 if (attrkind & attrText)
46 fPainting->AddTextAttr(*this);
47
48 fPainting->AddOper(oper);
49
50 return fPainting->Reserve(opersize);
51}
52
53////////////////////////////////////////////////////////////////////////////////
54///Noop, for non-gl pad TASImage calls gVirtualX->CopyArea.
55
56void TWebPadPainter::DrawPixels(const unsigned char * /*pixelData*/, UInt_t /*width*/, UInt_t /*height*/,
57 Int_t /*dstX*/, Int_t /*dstY*/, Bool_t /*enableAlphaBlending*/)
58{
59
60}
61
62
63////////////////////////////////////////////////////////////////////////////////
64/// Paint a simple line.
65
67{
68 if (GetLineWidth() <= 0)
69 return;
70
71 auto buf = StoreOperation("l2", attrLine, 4);
72 if (buf) {
73 buf[0] = x1;
74 buf[1] = y1;
75 buf[2] = x2;
76 buf[3] = y2;
77 }
78}
79
80
81////////////////////////////////////////////////////////////////////////////////
82/// Paint a simple line in normalized coordinates.
83
85{
86 if (GetLineWidth()<=0) return;
87
88 ::Error("DrawLineNDC", "Not supported correctly");
89
90 auto buf = StoreOperation("l2", attrLine, 4);
91 if (buf) {
92 buf[0] = u1;
93 buf[1] = v1;
94 buf[2] = u2;
95 buf[3] = v2;
96 }
97}
98
99
100////////////////////////////////////////////////////////////////////////////////
101/// Paint a simple box.
102
104{
105 if (GetLineWidth()<=0 && mode == TVirtualPadPainter::kHollow) return;
106
107 Float_t *buf = nullptr;
108
109 if (mode == TVirtualPadPainter::kHollow)
110 buf = StoreOperation("r", attrLine, 4); // only border
111 else
112 buf = StoreOperation("b", attrFill, 4); // only fill
113
114 if (buf) {
115 buf[0] = x1;
116 buf[1] = y1;
117 buf[2] = x2;
118 buf[3] = y2;
119 }
120}
121
122////////////////////////////////////////////////////////////////////////////////
123/// Paint filled area.
124
125void TWebPadPainter::DrawFillArea(Int_t nPoints, const Double_t *xs, const Double_t *ys)
126{
127 if ((GetFillStyle() <= 0) || (nPoints < 3))
128 return;
129
130 auto buf = StoreOperation("f" + std::to_string(nPoints), attrFill, nPoints * 2);
131 if (buf)
132 for (Int_t n = 0; n < nPoints; ++n) {
133 buf[n * 2] = xs[n];
134 buf[n * 2 + 1] = ys[n];
135 }
136}
137
138////////////////////////////////////////////////////////////////////////////////
139/// Paint filled area.
140
141void TWebPadPainter::DrawFillArea(Int_t nPoints, const Float_t *xs, const Float_t *ys)
142{
143 if ((GetFillStyle() <= 0) || (nPoints < 3))
144 return;
145
146 auto buf = StoreOperation("f" + std::to_string(nPoints), attrFill, nPoints * 2);
147 if (buf)
148 for (Int_t n = 0; n < nPoints; ++n) {
149 buf[n * 2] = xs[n];
150 buf[n * 2 + 1] = ys[n];
151 }
152}
153
154////////////////////////////////////////////////////////////////////////////////
155/// Paint Polyline.
156
157void TWebPadPainter::DrawPolyLine(Int_t nPoints, const Double_t *xs, const Double_t *ys)
158{
159 if ((GetLineWidth() <= 0) || (nPoints < 2))
160 return;
161
162 auto buf = StoreOperation("l" + std::to_string(nPoints), attrLine, nPoints * 2);
163 if (buf)
164 for (Int_t n = 0; n < nPoints; ++n) {
165 buf[n * 2] = xs[n];
166 buf[n * 2 + 1] = ys[n];
167 }
168}
169
170////////////////////////////////////////////////////////////////////////////////
171/// Paint polyline.
172
173void TWebPadPainter::DrawPolyLine(Int_t nPoints, const Float_t *xs, const Float_t *ys)
174{
175 if ((GetLineWidth() <= 0) || (nPoints < 2))
176 return;
177
178 auto buf = StoreOperation("l" + std::to_string(nPoints), attrLine, nPoints * 2);
179 if (buf)
180 for (Int_t n = 0; n < nPoints; ++n) {
181 buf[n * 2] = xs[n];
182 buf[n * 2 + 1] = ys[n];
183 }
184}
185
186////////////////////////////////////////////////////////////////////////////////
187/// Paint polyline in normalized coordinates.
188
190{
191 if ((GetLineWidth() <= 0) || (nPoints < 2))
192 return;
193
194 ::Error("DrawPolyLineNDC", "Not supported correctly");
195
196 auto buf = StoreOperation("l" + std::to_string(nPoints), attrLine, nPoints * 2);
197 if (buf)
198 for (Int_t n = 0; n < nPoints; ++n) {
199 buf[n * 2] = u[n];
200 buf[n * 2 + 1] = v[n];
201 }
202}
203
204////////////////////////////////////////////////////////////////////////////////
205/// Paint polymarker.
206
208{
209 if (nPoints < 1)
210 return;
211
212 auto buf = StoreOperation(std::string("m") + std::to_string(nPoints), attrLine | attrMarker, nPoints * 2);
213
214 if (buf)
215 for (Int_t n = 0; n < nPoints; ++n) {
216 buf[n * 2] = x[n];
217 buf[n * 2 + 1] = y[n];
218 }
219}
220
221////////////////////////////////////////////////////////////////////////////////
222/// Paint polymarker.
223
225{
226 if (nPoints < 1)
227 return;
228
229 auto buf = StoreOperation(std::string("m") + std::to_string(nPoints), attrLine | attrMarker, nPoints * 2);
230
231 if (buf)
232 for (Int_t n = 0; n < nPoints; ++n) {
233 buf[n * 2] = x[n];
234 buf[n * 2 + 1] = y[n];
235 }
236}
237
238////////////////////////////////////////////////////////////////////////////////
239/// Paint text.
240
242{
244 if (buf) {
245 buf[0] = x;
246 buf[1] = y;
247 }
248}
249
250////////////////////////////////////////////////////////////////////////////////
251/// Special version working with wchar_t and required by TMathText.
252
253void TWebPadPainter::DrawText(Double_t x, Double_t y, const wchar_t * /*text*/, ETextMode /*mode*/)
254{
255 auto buf = StoreOperation(TWebPainting::MakeTextOper("wchar_t"), attrText, 2);
256 if (buf) {
257 buf[0] = x;
258 buf[1] = y;
259 }
260}
261
262////////////////////////////////////////////////////////////////////////////////
263/// Paint text in normalized coordinates.
264
266{
267 ::Error("DrawTextNDC", "Not supported correctly");
268
270
271 if (buf) {
272 buf[0] = u;
273 buf[1] = v;
274 }
275}
276
277////////////////////////////////////////////////////////////////////////////////
278/// Paint text in normalized coordinates.
279
280void TWebPadPainter::DrawTextNDC(Double_t u , Double_t v, const wchar_t * /*text*/, ETextMode /*mode*/)
281{
282 ::Error("DrawTextNDC", "Not supported correctly");
283
284 auto buf = StoreOperation(TWebPainting::MakeTextOper("wchar_t"), attrText, 2);
285
286 if (buf) {
287 buf[0] = u;
288 buf[1] = v;
289 }
290}
291
292////////////////////////////////////////////////////////////////////////////////
293/// Produce image from WebPadPainter
294
295void TWebPadPainter::SaveImage(TVirtualPad *pad, const char *fileName, Int_t gtype) const
296{
297 if ((gtype == TImage::kPng) || (gtype == TImage::kJpeg))
298 TWebCanvas::ProduceImage(pad->GetCanvas(), fileName);
299}
300
static const double x2[5]
static const double x1[5]
double Double_t
Definition RtypesCore.h:59
float Float_t
Definition RtypesCore.h:57
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:187
@ kPng
Definition TImage.h:40
@ kJpeg
Definition TImage.h:41
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
virtual TCanvas * GetCanvas() const =0
static bool ProduceImage(TCanvas *c, const char *filename, Int_t width=0, Int_t height=0)
Create image using batch (headless) capability of Chrome browser Supported png, jpeg,...
void DrawPixels(const unsigned char *pixelData, UInt_t width, UInt_t height, Int_t dstX, Int_t dstY, Bool_t enableAlphaBlending) override
Noop, for non-gl pad TASImage calls gVirtualX->CopyArea.
Style_t GetFillStyle() const override
void DrawFillArea(Int_t n, const Double_t *x, const Double_t *y) override
Paint filled area.
void DrawBox(Double_t x1, Double_t y1, Double_t x2, Double_t y2, EBoxMode mode) override
Paint a simple box.
TWebPainting * fPainting
void DrawTextNDC(Double_t u, Double_t v, const char *text, ETextMode mode) override
Paint text in normalized coordinates.
Float_t * StoreOperation(const std::string &oper, unsigned attrkind, int opersize=0)
Store operation identifier with appropriate attributes.
void DrawText(Double_t x, Double_t y, const char *text, ETextMode mode) override
Paint text.
void DrawLineNDC(Double_t u1, Double_t v1, Double_t u2, Double_t v2) override
Paint a simple line in normalized coordinates.
void DrawPolyMarker(Int_t n, const Double_t *x, const Double_t *y) override
Paint polymarker.
void DrawPolyLineNDC(Int_t n, const Double_t *u, const Double_t *v) override
Paint polyline in normalized coordinates.
Width_t GetLineWidth() const override
void SaveImage(TVirtualPad *, const char *, Int_t) const override
Produce image from WebPadPainter.
void DrawPolyLine(Int_t n, const Double_t *x, const Double_t *y) override
Paint Polyline.
void DrawLine(Double_t x1, Double_t y1, Double_t x2, Double_t y2) override
Paint a simple line.
void AddTextAttr(const TAttText &attr)
Store text attributes If attributes were not changed - ignore operation.
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.
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.
void AddFillAttr(const TAttFill &attr)
Store fill attributes If attributes were not changed - ignore operation.
TText * text
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16