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
15/** \class TWebPainting
16\ingroup webgui6
17
18Object used to store paint operations and deliver them to JSROOT
19
20*/
21
22
23///////////////////////////////////////////////////////////////////////////////////////
24/// Constructor
25
27{
31}
32
33///////////////////////////////////////////////////////////////////////////////////////
34/// Add next custom operator to painting
35/// Operations are separated by semicolons
36/// Following operations are supported:
37/// t - text
38/// h - text coded into simple hex
39/// r - rectangle
40/// b - rectangular fill region
41/// l - polyline
42/// f - poly fill region
43/// m - poly marker
44/// z - line attributes
45/// y - fill attributes
46/// x - marker attributes
47/// o - text attributes
48/// After operation code optional arguments can be append like length of operation or coded text
49/// Each operation may use data from binary float buffer
50
51void TWebPainting::AddOper(const std::string &oper)
52{
53 if (!fOper.empty())
54 fOper.append(";");
55 fOper.append(oper);
56}
57
58///////////////////////////////////////////////////////////////////////////////////////
59/// Create text operation
60/// If text include special symbols - use simple hex coding
61
62std::string TWebPainting::MakeTextOper(const char *str)
63{
64 bool isany_special = false;
65 if (!str)
66 str = "";
67 for (auto p = str; *p; ++p) {
68 if ((*p < 32) || (*p > 126) || (*p == ';') || (*p == '\'') || (*p == '\"') || (*p == '%')) {
69 isany_special = true;
70 break;
71 }
72 }
73
74 if (!isany_special)
75 return std::string("t") + str;
76
77 // use simple hex coding while special symbols are hard to handle
78 std::string oper("h");
79 static const char *digits = "0123456789abcdef";
80 for (auto p = str; *p; ++p) {
81 unsigned code = (unsigned)*p;
82 oper.append(1, digits[(code >> 4) & 0xF]);
83 oper.append(1, digits[code & 0xF]);
84 }
85 return oper;
86}
87
88///////////////////////////////////////////////////////////////////////////////////////
89/// Reserve place in the float buffer
90/// Returns pointer on first element in reserved area
91
93{
94 if (sz <= 0)
95 return nullptr;
96
97 if (fSize + sz > fBuf.GetSize()) {
98 Int_t nextsz = fBuf.GetSize() + TMath::Max(1024, (sz/128 + 1) * 128);
99 fBuf.Set(nextsz);
100 }
101
102 Float_t *res = fBuf.GetArray() + fSize;
103 fSize += sz;
104 return res; // return size where drawing can start
105}
106
107///////////////////////////////////////////////////////////////////////////////////////
108/// Store line attributes
109/// If attributes were not changed - ignore operation
110
112{
113 if ((attr.GetLineColor() == fLastLine.GetLineColor()) &&
114 (attr.GetLineStyle() == fLastLine.GetLineStyle()) &&
115 (attr.GetLineWidth() == fLastLine.GetLineWidth())) return;
116
117 fLastLine = attr;
118
119 AddOper(std::string("z") +
120 std::to_string((int) attr.GetLineColor()) + ":" +
121 std::to_string((int) attr.GetLineStyle()) + ":" +
122 std::to_string((int) attr.GetLineWidth()));
123}
124
125///////////////////////////////////////////////////////////////////////////////////////
126/// Store fill attributes
127/// If attributes were not changed - ignore operation
128
130{
131 if ((fLastFill.GetFillColor() == attr.GetFillColor()) &&
132 (fLastFill.GetFillStyle() == attr.GetFillStyle())) return;
133
134 fLastFill = attr;
135
136 AddOper(std::string("y") +
137 std::to_string((int) attr.GetFillColor()) + ":" +
138 std::to_string((int) attr.GetFillStyle()));
139}
140
141///////////////////////////////////////////////////////////////////////////////////////
142/// Store text attributes
143/// If attributes were not changed - ignore operation
144
146{
147 AddOper(std::string("o") +
148 std::to_string((int) attr.GetTextColor()) + ":" +
149 std::to_string((int) attr.GetTextFont()) + ":" +
150 std::to_string((int) (attr.GetTextSize()>=1 ? attr.GetTextSize() : -1000*attr.GetTextSize())) + ":" +
151 std::to_string((int) attr.GetTextAlign()) + ":" +
152 std::to_string((int) attr.GetTextAngle()));
153}
154
155///////////////////////////////////////////////////////////////////////////////////////
156/// Store marker attributes
157/// If attributes were not changed - ignore operation
158
160{
161 if ((attr.GetMarkerColor() == fLastMarker.GetMarkerColor()) &&
162 (attr.GetMarkerStyle() == fLastMarker.GetMarkerStyle()) &&
163 (attr.GetMarkerSize() == fLastMarker.GetMarkerSize())) return;
164
166
167 AddOper(std::string("x") +
168 std::to_string((int) attr.GetMarkerColor()) + ":" +
169 std::to_string((int) attr.GetMarkerStyle()) + ":" +
170 std::to_string((int) attr.GetMarkerSize()));
171}
172
173///////////////////////////////////////////////////////////////////////////////////////
174/// Add custom color to operations
175
177{
178 if (!col) return;
179
180 TString code;
181
182 if (col->GetAlpha() == 1)
183 code.Form("%d:%d,%d,%d", indx, (int) (255*col->GetRed()), (int) (255*col->GetGreen()), (int) (255*col->GetBlue()));
184 else
185 code.Form("%d=%d,%d,%d,%5.3f", indx, (int) (255*col->GetRed()), (int) (255*col->GetGreen()), (int) (255*col->GetBlue()), col->GetAlpha());
186
187 AddOper(code.Data());
188}
189
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
The color creation and management class.
Definition TColor.h:19
Float_t GetRed() const
Definition TColor.h:58
Float_t GetAlpha() const
Definition TColor.h:64
Float_t GetBlue() const
Definition TColor.h:60
Float_t GetGreen() const
Definition TColor.h:59
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:380
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2334
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.
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition TMathBase.h:250