use super::*;
use crate::units::layout_px;
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct UsedBorderSide {
pub(crate) specified_width: LayoutLength,
pub(crate) used_width: LayoutLength,
pub(crate) style: BorderStyle,
pub(crate) color: CssColor,
}
impl UsedBorderSide {
pub(crate) fn new(specified_width: LayoutLength, style: BorderStyle, color: CssColor) -> Self {
Self {
specified_width: layout_pt(specified_width.get().max(0.0)),
used_width: used_border_side_width(specified_width, style),
style,
color,
}
}
pub(crate) fn is_visible(self) -> bool {
self.used_width > layout_pt(0.0)
&& !self.style.suppresses_used_width()
&& self.color.is_visible()
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct UsedBorder {
pub(crate) top: UsedBorderSide,
pub(crate) right: UsedBorderSide,
pub(crate) bottom: UsedBorderSide,
pub(crate) left: UsedBorderSide,
}
impl UsedBorder {
pub(crate) fn widths(self) -> css::Edges {
css::Edges {
top: self.top.used_width.get(),
right: self.right.used_width.get(),
bottom: self.bottom.used_width.get(),
left: self.left.used_width.get(),
}
}
}
pub(crate) fn used_border(style: &ComputedStyle) -> UsedBorder {
let colors = style.border_colors.resolve(style.color);
UsedBorder {
top: UsedBorderSide::new(
layout_pt(style.border_widths.top),
style.border_styles.top,
colors.top,
),
right: UsedBorderSide::new(
layout_pt(style.border_widths.right),
style.border_styles.right,
colors.right,
),
bottom: UsedBorderSide::new(
layout_pt(style.border_widths.bottom),
style.border_styles.bottom,
colors.bottom,
),
left: UsedBorderSide::new(
layout_pt(style.border_widths.left),
style.border_styles.left,
colors.left,
),
}
}
pub(crate) fn used_border_widths(style: &ComputedStyle) -> css::Edges {
used_border(style).widths()
}
pub(crate) fn computed_border_widths(style: &ComputedStyle) -> css::Edges {
css::Edges {
top: style
.border_width_values
.top
.clone()
.length_max_zero()
.points(),
right: style
.border_width_values
.right
.clone()
.length_max_zero()
.points(),
bottom: style
.border_width_values
.bottom
.clone()
.length_max_zero()
.points(),
left: style
.border_width_values
.left
.clone()
.length_max_zero()
.points(),
}
}
pub(crate) fn used_border_side_width(width: LayoutLength, style: BorderStyle) -> LayoutLength {
if style.suppresses_used_width() {
layout_pt(0.0)
} else {
layout_pt(width.get().max(0.0))
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct DoubleBorderBands {
pub(crate) stripe: LayoutLength,
}
impl DoubleBorderBands {
pub(crate) fn for_used_width(used_width: LayoutLength) -> Option<Self> {
(used_width >= layout_px(3.0)).then(|| Self {
stripe: layout_pt(used_width.get() / 3.0),
})
}
}
pub(crate) fn used_border_width(style: &ComputedStyle) -> LayoutLength {
layout_pt(max_edge(used_border_widths(style)))
}
pub(crate) fn max_edge(edges: css::Edges) -> f32 {
edges.top.max(edges.right).max(edges.bottom).max(edges.left)
}
pub(crate) fn horizontal_border_width(style: &ComputedStyle) -> f32 {
let borders = used_border_widths(style);
borders.left + borders.right
}
pub(crate) fn vertical_border_width(style: &ComputedStyle) -> f32 {
let borders = used_border_widths(style);
borders.top + borders.bottom
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn used_border_width_preserves_layout_length_type() {
let mut style = ComputedStyle::initial();
style.border_widths.left = 3.0;
style.border_styles.left = BorderStyle::Solid;
let width: LayoutLength = used_border_width(&style);
assert_eq!(width, layout_pt(3.0));
}
#[test]
fn computed_border_width_remains_available_when_style_suppresses_used_width() {
let mut style = ComputedStyle::initial();
style.border_width_values.left = css::ComputedLengthPercentage::from_points(6.0);
style.border_widths.left = 0.0;
style.border_styles.left = BorderStyle::None;
assert_eq!(computed_border_widths(&style).left, 6.0);
assert_eq!(used_border_widths(&style).left, 0.0);
}
#[test]
fn double_border_bands_use_css_pixel_cutoff_and_equal_layout_bands() {
assert_eq!(DoubleBorderBands::for_used_width(layout_px(2.0)), None);
let bands = DoubleBorderBands::for_used_width(layout_px(3.0)).unwrap();
assert_eq!(bands.stripe, layout_px(1.0));
assert_eq!(bands.stripe.get() * 3.0, layout_px(3.0).get());
}
}