-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
basic_shape.rs
194 lines (176 loc) · 6.37 KB
/
basic_shape.rs
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! CSS handling for the computed value of
//! [`basic-shape`][basic-shape]s
//!
//! [basic-shape]: https://drafts.csswg.org/css-shapes/#typedef-basic-shape
use cssparser::ToCss;
use properties::shorthands::serialize_four_sides;
use std::fmt;
use values::computed::position::Position;
use values::computed::{BorderRadiusSize, LengthOrPercentage};
pub use values::specified::basic_shape::FillRule;
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum BasicShape {
Inset(InsetRect),
Circle(Circle),
Ellipse(Ellipse),
Polygon(Polygon),
}
impl ToCss for BasicShape {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
BasicShape::Inset(ref rect) => rect.to_css(dest),
BasicShape::Circle(ref circle) => circle.to_css(dest),
BasicShape::Ellipse(ref e) => e.to_css(dest),
BasicShape::Polygon(ref poly) => poly.to_css(dest),
}
}
}
#[derive(Clone, PartialEq, Copy, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct InsetRect {
pub top: LengthOrPercentage,
pub right: LengthOrPercentage,
pub bottom: LengthOrPercentage,
pub left: LengthOrPercentage,
pub round: Option<BorderRadius>,
}
impl ToCss for InsetRect {
// XXXManishearth again, we should try to reduce the number of values printed here
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(dest.write_str("inset("));
try!(self.top.to_css(dest));
try!(dest.write_str(" "));
try!(self.right.to_css(dest));
try!(dest.write_str(" "));
try!(self.bottom.to_css(dest));
try!(dest.write_str(" "));
try!(self.left.to_css(dest));
if let Some(ref radius) = self.round {
try!(dest.write_str(" round "));
try!(radius.to_css(dest));
}
dest.write_str(")")
}
}
#[derive(Clone, PartialEq, Copy, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct Circle {
pub radius: ShapeRadius,
pub position: Position,
}
impl ToCss for Circle {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(self.radius.to_css(dest));
try!(dest.write_str(" at "));
self.position.to_css(dest)
}
}
#[derive(Clone, PartialEq, Copy, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct Ellipse {
pub semiaxis_x: ShapeRadius,
pub semiaxis_y: ShapeRadius,
pub position: Position,
}
impl ToCss for Ellipse {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(dest.write_str("ellipse("));
if (self.semiaxis_x, self.semiaxis_y) != Default::default() {
try!(self.semiaxis_x.to_css(dest));
try!(dest.write_str(" "));
try!(self.semiaxis_y.to_css(dest));
try!(dest.write_str(" "));
}
try!(dest.write_str("at "));
try!(self.position.to_css(dest));
dest.write_str(")")
}
}
#[derive(Clone, PartialEq, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
/// https://drafts.csswg.org/css-shapes/#funcdef-polygon
pub struct Polygon {
pub fill: FillRule,
pub coordinates: Vec<(LengthOrPercentage, LengthOrPercentage)>,
}
impl ToCss for Polygon {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(dest.write_str("polygon("));
let mut need_space = false;
if self.fill != Default::default() {
try!(self.fill.to_css(dest));
try!(dest.write_str(", "));
}
for coord in &self.coordinates {
if need_space {
try!(dest.write_str(", "));
}
try!(coord.0.to_css(dest));
try!(dest.write_str(" "));
try!(coord.1.to_css(dest));
need_space = true;
}
dest.write_str(")")
}
}
/// https://drafts.csswg.org/css-shapes/#typedef-shape-radius
#[derive(Clone, PartialEq, Copy, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum ShapeRadius {
Length(LengthOrPercentage),
ClosestSide,
FarthestSide,
}
impl Default for ShapeRadius {
fn default() -> Self {
ShapeRadius::ClosestSide
}
}
impl ToCss for ShapeRadius {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
ShapeRadius::Length(lop) => lop.to_css(dest),
ShapeRadius::ClosestSide => dest.write_str("closest-side"),
ShapeRadius::FarthestSide => dest.write_str("farthest-side"),
}
}
}
/// https://drafts.csswg.org/css-backgrounds-3/#border-radius
#[derive(Clone, PartialEq, Copy, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct BorderRadius {
pub top_left: BorderRadiusSize,
pub top_right: BorderRadiusSize,
pub bottom_right: BorderRadiusSize,
pub bottom_left: BorderRadiusSize,
}
impl ToCss for BorderRadius {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
if self.top_left.0.width == self.top_left.0.height &&
self.top_right.0.width == self.top_right.0.height &&
self.bottom_right.0.width == self.bottom_right.0.height &&
self.bottom_left.0.width == self.bottom_left.0.height {
serialize_four_sides((&self.top_left.0.width,
&self.top_right.0.width,
&self.bottom_right.0.width,
&self.bottom_left.0.width),
dest)
} else {
try!(serialize_four_sides((&self.top_left.0.width,
&self.top_right.0.width,
&self.bottom_right.0.width,
&self.bottom_left.0.width),
dest));
try!(dest.write_str(" / "));
serialize_four_sides((&self.top_left.0.height,
&self.top_right.0.height,
&self.bottom_right.0.height,
&self.bottom_left.0.height),
dest)
}
}
}