Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RPadLength.cxx
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (C) 1995-2018, Rene Brun and Fons Rademakers. *
3 * All rights reserved. *
4 * *
5 * For the licensing terms see $ROOTSYS/LICENSE. *
6 * For the list of contributors see $ROOTSYS/README/CREDITS. *
7 *************************************************************************/
8
9#include "ROOT/RPadLength.hxx"
10
11#include "ROOT/RAttrBase.hxx" // for GPadLog()
12#include "ROOT/RLogger.hxx"
13
14#include <string>
15
16///////////////////////////////////////////////////////////////////////
17/// Converts RPadLength to string like "0.1 + 25px"
18/// User coordinates not (yet) supported
19
21{
22 std::string res;
23
24 if (HasNormal()) {
25 double v = GetNormal();
26 if (v) res = std::to_string(v);
27 }
28
29 if (HasPixel()) {
30 double v = GetPixel();
31 if ((v > 0) && !res.empty())
32 res += " + ";
33
34 if ((v != 0) || res.empty()) {
35 res += std::to_string(v);
36 res += "px";
37 }
38 }
39
40 if (!Empty() && res.empty())
41 res = "0";
42
43 return res;
44}
45
46///////////////////////////////////////////////////////////////////////
47/// Parse string and fill RPadLength attributes
48/// String can be like "0.1 + 25px"
49/// User coordinates not (yet) supported
50
52{
53 Clear();
54
55 if (value.empty())
56 return true;
57
58 if (value == "0") {
59 SetNormal(0);
60 return true;
61 }
62
63 if (value == "0px") {
64 SetPixel(0);
65 return true;
66 }
67
68 std::size_t pos = 0;
69 std::string val = value;
70 int operand = 0;
71
72 while (!val.empty()) {
73 // skip empty spaces
74 while ((pos < val.length()) && ((val[pos] == ' ') || (val[pos] == '\t')))
75 ++pos;
76
77 if (pos >= val.length())
78 break;
79
80 if ((val[pos] == '-') || (val[pos] == '+')) {
81 if (operand) {
82 Clear();
83 R__LOG_ERROR(GPadLog()) << "Fail to parse RPadLength " << value;
84 return false;
85 }
86 operand = (val[pos] == '-') ? -1 : 1;
87 pos++;
88 continue;
89 }
90
91 if (pos > 0) {
92 val.erase(0, pos);
93 pos = 0;
94 }
95
96 double v = 0;
97
98 try {
99 v = std::stod(val, &pos);
100 } catch (...) {
101 Clear();
102 return false;
103 }
104
105 val.erase(0, pos);
106 pos = 0;
107 if (!operand) operand = 1;
108
109 if ((val.length() > 0) && (val[0] == '%')) {
110 val.erase(0, 1);
111 SetNormal(GetNormal() + operand*0.01*v);
112 } else if ((val.length() > 1) && (val[0] == 'p') && (val[1] == 'x')) {
113 val.erase(0, 2);
114 SetPixel(GetPixel() + operand*v);
115 } else {
116 SetNormal(GetNormal() + operand*v);
117 }
118
119 operand = 0;
120 }
121
122 return true;
123}
#define R__LOG_ERROR(...)
Definition RLogger.hxx:362
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
static void GetPixel(int y, int width, Byte_t *scline)
Get pixels in line y and put in array scline.
Definition TGWin32.cxx:4302
std::string AsString() const
Converts RPadLength to string like "0.1 + 25px" User coordinates not (yet) supported.
bool ParseString(const std::string &val)
Parse string and fill RPadLength attributes String can be like "0.1 + 25px" User coordinates not (yet...
RLogChannel & GPadLog()
Log channel for GPad diagnostics.
Definition RAttrBase.cxx:17