/
Margin.h
116 lines (93 loc) · 3.42 KB
/
Margin.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#pragma once
#include <ostream>
namespace bdn
{
/** Represents the size of a margin or border.
When margin components are listed individually (for example in a
constructor) then their order is always the same as the order of the
numbers on a clock: top, right, bottom, left
This is also the same order that is used in the CSS standard.
Margin objects are supported by the global function bdn::toString().
They can also be written to standard output streams (std::basic_ostream,
bdn::TextOutStream, bdn::StringBuffer) with the << operator.
*/
struct Margin
{
public:
double top = 0;
double right = 0;
double bottom = 0;
double left = 0;
Margin() : top(0), right(0), bottom(0), left(0) {}
explicit Margin(double all)
{
top = all;
right = all;
bottom = all;
left = all;
}
Margin(double topBottom, double leftRight)
{
top = topBottom;
right = leftRight;
bottom = topBottom;
left = leftRight;
}
Margin(double top, double right, double bottom, double left)
{
this->top = top;
this->right = right;
this->bottom = bottom;
this->left = left;
}
Margin operator+(const Margin &o) const { return Margin(*this) += o; }
Margin operator-(const Margin &o) const { return Margin(*this) -= o; }
Margin &operator+=(const Margin &o)
{
top += o.top;
right += o.right;
bottom += o.bottom;
left += o.left;
return *this;
}
Margin &operator-=(const Margin &o)
{
top -= o.top;
right -= o.right;
bottom -= o.bottom;
left -= o.left;
return *this;
}
};
template <typename CHAR_TYPE, class CHAR_TRAITS>
std::basic_ostream<CHAR_TYPE, CHAR_TRAITS> &operator<<(std::basic_ostream<CHAR_TYPE, CHAR_TRAITS> &stream,
const Margin &m)
{
return stream << "(" << m.top << ", " << m.right << ", " << m.bottom << ", " << m.left << ")";
}
}
inline bool operator==(const bdn::Margin &a, const bdn::Margin &b)
{
return (a.top == b.top && a.right == b.right && a.bottom == b.bottom && a.left == b.left);
}
inline bool operator!=(const bdn::Margin &a, const bdn::Margin &b) { return !operator==(a, b); }
/** Returns true if a's fields are each smaller than b's */
inline bool operator<(const bdn::Margin &a, const bdn::Margin &b)
{
return (a.top < b.top && a.right < b.right && a.bottom < b.bottom && a.left < b.left);
}
/** Returns true if a's fields are each smaller or equal to b's */
inline bool operator<=(const bdn::Margin &a, const bdn::Margin &b)
{
return (a.top <= b.top && a.right <= b.right && a.bottom <= b.bottom && a.left <= b.left);
}
/** Returns true if a's fields are each bigger than b's */
inline bool operator>(const bdn::Margin &a, const bdn::Margin &b)
{
return (a.top > b.top && a.right > b.right && a.bottom > b.bottom && a.left > b.left);
}
/** Returns true if a's fields are each bigger or equal to b's */
inline bool operator>=(const bdn::Margin &a, const bdn::Margin &b)
{
return (a.top >= b.top && a.right >= b.right && a.bottom >= b.bottom && a.left >= b.left);
}