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