mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +01:00
Rustfmt layout crate
This commit is contained in:
parent
577830de90
commit
349047b096
37 changed files with 7177 additions and 4832 deletions
|
@ -16,7 +16,7 @@ use style::values::computed::LengthOrPercentageOrAuto;
|
|||
#[derive(Clone, Copy, Debug, Serialize)]
|
||||
pub enum FloatKind {
|
||||
Left,
|
||||
Right
|
||||
Right,
|
||||
}
|
||||
|
||||
impl FloatKind {
|
||||
|
@ -78,7 +78,12 @@ impl FloatList {
|
|||
|
||||
impl fmt::Debug for FloatList {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "max_block_start={:?} floats={}", self.max_block_start, self.floats.len())?;
|
||||
write!(
|
||||
f,
|
||||
"max_block_start={:?} floats={}",
|
||||
self.max_block_start,
|
||||
self.floats.len()
|
||||
)?;
|
||||
for float in self.floats.iter() {
|
||||
write!(f, " {:?}", float)?;
|
||||
}
|
||||
|
@ -95,22 +100,29 @@ pub struct PlacementInfo {
|
|||
/// The maximum inline-end position of the float, generally determined by the containing block.
|
||||
pub max_inline_size: Au,
|
||||
/// The kind of float.
|
||||
pub kind: FloatKind
|
||||
pub kind: FloatKind,
|
||||
}
|
||||
|
||||
impl fmt::Debug for PlacementInfo {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f,
|
||||
"size={:?} ceiling={:?} max_inline_size={:?} kind={:?}",
|
||||
self.size,
|
||||
self.ceiling,
|
||||
self.max_inline_size,
|
||||
self.kind)
|
||||
write!(
|
||||
f,
|
||||
"size={:?} ceiling={:?} max_inline_size={:?} kind={:?}",
|
||||
self.size, self.ceiling, self.max_inline_size, self.kind
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn range_intersect(block_start_1: Au, block_end_1: Au, block_start_2: Au, block_end_2: Au) -> (Au, Au) {
|
||||
(max(block_start_1, block_start_2), min(block_end_1, block_end_2))
|
||||
fn range_intersect(
|
||||
block_start_1: Au,
|
||||
block_end_1: Au,
|
||||
block_start_2: Au,
|
||||
block_end_2: Au,
|
||||
) -> (Au, Au) {
|
||||
(
|
||||
max(block_start_1, block_start_2),
|
||||
min(block_end_1, block_end_2),
|
||||
)
|
||||
}
|
||||
|
||||
/// Encapsulates information about floats. This is optimized to avoid allocation if there are
|
||||
|
@ -162,8 +174,12 @@ impl Floats {
|
|||
/// with inline-size small enough that it doesn't collide with any floats. max_x is the
|
||||
/// inline-size beyond which floats have no effect. (Generally this is the containing block
|
||||
/// inline-size.)
|
||||
pub fn available_rect(&self, block_start: Au, block_size: Au, max_x: Au)
|
||||
-> Option<LogicalRect<Au>> {
|
||||
pub fn available_rect(
|
||||
&self,
|
||||
block_start: Au,
|
||||
block_size: Au,
|
||||
max_x: Au,
|
||||
) -> Option<LogicalRect<Au>> {
|
||||
let list = &self.list;
|
||||
let block_start = block_start - self.offset.block;
|
||||
|
||||
|
@ -186,30 +202,38 @@ impl Floats {
|
|||
|
||||
debug!("float_pos: {:?}, float_size: {:?}", float_pos, float_size);
|
||||
match float.kind {
|
||||
FloatKind::Left if float_pos.i + float_size.inline > max_inline_start &&
|
||||
FloatKind::Left
|
||||
if float_pos.i + float_size.inline > max_inline_start &&
|
||||
float_pos.b + float_size.block > block_start &&
|
||||
float_pos.b < block_start + block_size => {
|
||||
float_pos.b < block_start + block_size =>
|
||||
{
|
||||
max_inline_start = float_pos.i + float_size.inline;
|
||||
|
||||
l_block_start = Some(float_pos.b);
|
||||
l_block_end = Some(float_pos.b + float_size.block);
|
||||
|
||||
debug!("available_rect: collision with inline_start float: new \
|
||||
max_inline_start is {:?}",
|
||||
max_inline_start);
|
||||
debug!(
|
||||
"available_rect: collision with inline_start float: new \
|
||||
max_inline_start is {:?}",
|
||||
max_inline_start
|
||||
);
|
||||
}
|
||||
FloatKind::Right if float_pos.i < min_inline_end &&
|
||||
float_pos.b + float_size.block > block_start &&
|
||||
float_pos.b < block_start + block_size => {
|
||||
FloatKind::Right
|
||||
if float_pos.i < min_inline_end &&
|
||||
float_pos.b + float_size.block > block_start &&
|
||||
float_pos.b < block_start + block_size =>
|
||||
{
|
||||
min_inline_end = float_pos.i;
|
||||
|
||||
r_block_start = Some(float_pos.b);
|
||||
r_block_end = Some(float_pos.b + float_size.block);
|
||||
debug!("available_rect: collision with inline_end float: new min_inline_end \
|
||||
is {:?}",
|
||||
min_inline_end);
|
||||
debug!(
|
||||
"available_rect: collision with inline_end float: new min_inline_end \
|
||||
is {:?}",
|
||||
min_inline_end
|
||||
);
|
||||
}
|
||||
FloatKind::Left | FloatKind::Right => {}
|
||||
FloatKind::Left | FloatKind::Right => {},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -217,25 +241,28 @@ impl Floats {
|
|||
// If there are floats on both sides, take the intersection of the
|
||||
// two areas. Also make sure we never return a block-start smaller than the
|
||||
// given upper bound.
|
||||
let (block_start, block_end) = match (r_block_start,
|
||||
r_block_end,
|
||||
l_block_start,
|
||||
l_block_end) {
|
||||
(Some(r_block_start), Some(r_block_end), Some(l_block_start), Some(l_block_end)) => {
|
||||
range_intersect(max(block_start, r_block_start),
|
||||
r_block_end,
|
||||
max(block_start, l_block_start),
|
||||
l_block_end)
|
||||
}
|
||||
(None, None, Some(l_block_start), Some(l_block_end)) => {
|
||||
(max(block_start, l_block_start), l_block_end)
|
||||
}
|
||||
(Some(r_block_start), Some(r_block_end), None, None) => {
|
||||
(max(block_start, r_block_start), r_block_end)
|
||||
}
|
||||
(None, None, None, None) => return None,
|
||||
_ => panic!("Reached unreachable state when computing float area")
|
||||
};
|
||||
let (block_start, block_end) =
|
||||
match (r_block_start, r_block_end, l_block_start, l_block_end) {
|
||||
(
|
||||
Some(r_block_start),
|
||||
Some(r_block_end),
|
||||
Some(l_block_start),
|
||||
Some(l_block_end),
|
||||
) => range_intersect(
|
||||
max(block_start, r_block_start),
|
||||
r_block_end,
|
||||
max(block_start, l_block_start),
|
||||
l_block_end,
|
||||
),
|
||||
(None, None, Some(l_block_start), Some(l_block_end)) => {
|
||||
(max(block_start, l_block_start), l_block_end)
|
||||
},
|
||||
(Some(r_block_start), Some(r_block_end), None, None) => {
|
||||
(max(block_start, r_block_start), r_block_end)
|
||||
},
|
||||
(None, None, None, None) => return None,
|
||||
_ => panic!("Reached unreachable state when computing float area"),
|
||||
};
|
||||
|
||||
// FIXME(eatkinson): This assertion is too strong and fails in some cases. It is OK to
|
||||
// return negative inline-sizes since we check against that inline-end away, but we should
|
||||
|
@ -244,11 +271,13 @@ impl Floats {
|
|||
|
||||
assert!(block_start <= block_end, "Float position error");
|
||||
|
||||
Some(LogicalRect::new(self.writing_mode,
|
||||
max_inline_start + self.offset.inline,
|
||||
block_start + self.offset.block,
|
||||
min_inline_end - max_inline_start,
|
||||
block_end - block_start))
|
||||
Some(LogicalRect::new(
|
||||
self.writing_mode,
|
||||
max_inline_start + self.offset.inline,
|
||||
block_start + self.offset.block,
|
||||
min_inline_end - max_inline_start,
|
||||
block_end - block_start,
|
||||
))
|
||||
}
|
||||
|
||||
/// Adds a new float to the list.
|
||||
|
@ -260,7 +289,7 @@ impl Floats {
|
|||
Some(max_block_start) => max(info.ceiling, max_block_start + self.offset.block),
|
||||
},
|
||||
max_inline_size: info.max_inline_size,
|
||||
kind: info.kind
|
||||
kind: info.kind,
|
||||
};
|
||||
|
||||
debug!("add_float: added float with info {:?}", new_info);
|
||||
|
@ -271,7 +300,7 @@ impl Floats {
|
|||
self.place_between_floats(&new_info).start - self.offset,
|
||||
info.size,
|
||||
),
|
||||
kind: info.kind
|
||||
kind: info.kind,
|
||||
};
|
||||
|
||||
self.list.floats = self.list.floats.prepend_elem(new_float);
|
||||
|
@ -284,8 +313,12 @@ impl Floats {
|
|||
/// Given the three sides of the bounding rectangle in the block-start direction, finds the
|
||||
/// largest block-size that will result in the rectangle not colliding with any floats. Returns
|
||||
/// `None` if that block-size is infinite.
|
||||
fn max_block_size_for_bounds(&self, inline_start: Au, block_start: Au, inline_size: Au)
|
||||
-> Option<Au> {
|
||||
fn max_block_size_for_bounds(
|
||||
&self,
|
||||
inline_start: Au,
|
||||
block_start: Au,
|
||||
inline_size: Au,
|
||||
) -> Option<Au> {
|
||||
let list = &self.list;
|
||||
|
||||
let block_start = block_start - self.offset.block;
|
||||
|
@ -294,10 +327,11 @@ impl Floats {
|
|||
|
||||
for float in list.floats.iter() {
|
||||
if float.bounds.start.b + float.bounds.size.block > block_start &&
|
||||
float.bounds.start.i + float.bounds.size.inline > inline_start &&
|
||||
float.bounds.start.i < inline_start + inline_size {
|
||||
let new_y = float.bounds.start.b;
|
||||
max_block_size = Some(min(max_block_size.unwrap_or(new_y), new_y));
|
||||
float.bounds.start.i + float.bounds.size.inline > inline_start &&
|
||||
float.bounds.start.i < inline_start + inline_size
|
||||
{
|
||||
let new_y = float.bounds.start.b;
|
||||
max_block_size = Some(min(max_block_size.unwrap_or(new_y), new_y));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -318,85 +352,88 @@ impl Floats {
|
|||
Au(0),
|
||||
info.ceiling,
|
||||
info.max_inline_size,
|
||||
MAX_AU)
|
||||
}
|
||||
MAX_AU,
|
||||
)
|
||||
},
|
||||
FloatKind::Right => {
|
||||
return LogicalRect::new(
|
||||
self.writing_mode,
|
||||
info.max_inline_size - info.size.inline,
|
||||
info.ceiling,
|
||||
info.max_inline_size,
|
||||
MAX_AU)
|
||||
}
|
||||
MAX_AU,
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Can't go any higher than previous floats or previous elements in the document.
|
||||
let mut float_b = info.ceiling;
|
||||
loop {
|
||||
let maybe_location = self.available_rect(float_b,
|
||||
info.size.block,
|
||||
info.max_inline_size);
|
||||
debug!("place_float: got available rect: {:?} for block-pos: {:?}",
|
||||
maybe_location,
|
||||
float_b);
|
||||
let maybe_location =
|
||||
self.available_rect(float_b, info.size.block, info.max_inline_size);
|
||||
debug!(
|
||||
"place_float: got available rect: {:?} for block-pos: {:?}",
|
||||
maybe_location, float_b
|
||||
);
|
||||
match maybe_location {
|
||||
// If there are no floats blocking us, return the current location
|
||||
// TODO(eatkinson): integrate with overflow
|
||||
None => {
|
||||
return match info.kind {
|
||||
FloatKind::Left => {
|
||||
LogicalRect::new(
|
||||
self.writing_mode,
|
||||
Au(0),
|
||||
float_b,
|
||||
info.max_inline_size,
|
||||
MAX_AU)
|
||||
}
|
||||
FloatKind::Right => {
|
||||
LogicalRect::new(
|
||||
self.writing_mode,
|
||||
info.max_inline_size - info.size.inline,
|
||||
float_b,
|
||||
info.max_inline_size,
|
||||
MAX_AU)
|
||||
}
|
||||
FloatKind::Left => LogicalRect::new(
|
||||
self.writing_mode,
|
||||
Au(0),
|
||||
float_b,
|
||||
info.max_inline_size,
|
||||
MAX_AU,
|
||||
),
|
||||
FloatKind::Right => LogicalRect::new(
|
||||
self.writing_mode,
|
||||
info.max_inline_size - info.size.inline,
|
||||
float_b,
|
||||
info.max_inline_size,
|
||||
MAX_AU,
|
||||
),
|
||||
}
|
||||
}
|
||||
},
|
||||
Some(rect) => {
|
||||
assert_ne!(rect.start.b + rect.size.block, float_b,
|
||||
"Non-terminating float placement");
|
||||
assert_ne!(
|
||||
rect.start.b + rect.size.block,
|
||||
float_b,
|
||||
"Non-terminating float placement"
|
||||
);
|
||||
|
||||
// Place here if there is enough room
|
||||
if rect.size.inline >= info.size.inline {
|
||||
let block_size = self.max_block_size_for_bounds(rect.start.i,
|
||||
rect.start.b,
|
||||
rect.size.inline);
|
||||
let block_size = self.max_block_size_for_bounds(
|
||||
rect.start.i,
|
||||
rect.start.b,
|
||||
rect.size.inline,
|
||||
);
|
||||
let block_size = block_size.unwrap_or(MAX_AU);
|
||||
return match info.kind {
|
||||
FloatKind::Left => {
|
||||
LogicalRect::new(
|
||||
self.writing_mode,
|
||||
rect.start.i,
|
||||
float_b,
|
||||
rect.size.inline,
|
||||
block_size)
|
||||
}
|
||||
FloatKind::Right => {
|
||||
LogicalRect::new(
|
||||
self.writing_mode,
|
||||
rect.start.i + rect.size.inline - info.size.inline,
|
||||
float_b,
|
||||
rect.size.inline,
|
||||
block_size)
|
||||
}
|
||||
}
|
||||
FloatKind::Left => LogicalRect::new(
|
||||
self.writing_mode,
|
||||
rect.start.i,
|
||||
float_b,
|
||||
rect.size.inline,
|
||||
block_size,
|
||||
),
|
||||
FloatKind::Right => LogicalRect::new(
|
||||
self.writing_mode,
|
||||
rect.start.i + rect.size.inline - info.size.inline,
|
||||
float_b,
|
||||
rect.size.inline,
|
||||
block_size,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
// Try to place at the next-lowest location.
|
||||
// Need to be careful of fencepost errors.
|
||||
float_b = rect.start.b + rect.size.block;
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -411,8 +448,8 @@ impl Floats {
|
|||
(ClearType::Both, _) => {
|
||||
let b = self.offset.block + float.bounds.start.b + float.bounds.size.block;
|
||||
clearance = max(clearance, b);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
clearance
|
||||
|
@ -486,14 +523,20 @@ impl SpeculatedFloatPlacement {
|
|||
}
|
||||
}
|
||||
|
||||
self.left = max(self.left, block_flow.base.speculated_float_placement_in.left);
|
||||
self.right = max(self.right, block_flow.base.speculated_float_placement_in.right);
|
||||
self.left = max(
|
||||
self.left,
|
||||
block_flow.base.speculated_float_placement_in.left,
|
||||
);
|
||||
self.right = max(
|
||||
self.right,
|
||||
block_flow.base.speculated_float_placement_in.right,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let base_flow = flow.base();
|
||||
if !base_flow.flags.is_float() {
|
||||
return
|
||||
return;
|
||||
}
|
||||
|
||||
let mut float_inline_size = base_flow.intrinsic_inline_sizes.preferred_inline_size;
|
||||
|
@ -504,7 +547,8 @@ impl SpeculatedFloatPlacement {
|
|||
// that the layout traversal logic will know that objects later in the document
|
||||
// might flow around this float.
|
||||
if let LengthOrPercentageOrAuto::Percentage(percentage) =
|
||||
flow.as_block().fragment.style.content_inline_size() {
|
||||
flow.as_block().fragment.style.content_inline_size()
|
||||
{
|
||||
if percentage.0 > 0.0 {
|
||||
float_inline_size = Au::from_px(1)
|
||||
}
|
||||
|
@ -513,7 +557,7 @@ impl SpeculatedFloatPlacement {
|
|||
}
|
||||
|
||||
match base_flow.flags.float_kind() {
|
||||
StyleFloat::None => {}
|
||||
StyleFloat::None => {},
|
||||
StyleFloat::Left => self.left = self.left + float_inline_size,
|
||||
StyleFloat::Right => self.right = self.right + float_inline_size,
|
||||
}
|
||||
|
@ -522,17 +566,18 @@ impl SpeculatedFloatPlacement {
|
|||
/// Given a flow, computes the speculated inline size of the floats in of its first child.
|
||||
pub fn compute_floats_in_for_first_child(parent_flow: &mut Flow) -> SpeculatedFloatPlacement {
|
||||
if !parent_flow.is_block_like() {
|
||||
return parent_flow.base().speculated_float_placement_in
|
||||
return parent_flow.base().speculated_float_placement_in;
|
||||
}
|
||||
|
||||
let parent_block_flow = parent_flow.as_block();
|
||||
if parent_block_flow.formatting_context_type() != FormattingContextType::None {
|
||||
return SpeculatedFloatPlacement::zero()
|
||||
return SpeculatedFloatPlacement::zero();
|
||||
}
|
||||
|
||||
let mut placement = parent_block_flow.base.speculated_float_placement_in;
|
||||
let speculated_inline_content_edge_offsets =
|
||||
parent_block_flow.fragment.guess_inline_content_edge_offsets();
|
||||
let speculated_inline_content_edge_offsets = parent_block_flow
|
||||
.fragment
|
||||
.guess_inline_content_edge_offsets();
|
||||
|
||||
if speculated_inline_content_edge_offsets.start > Au(0) {
|
||||
placement.left = if placement.left > speculated_inline_content_edge_offsets.start {
|
||||
|
@ -552,4 +597,3 @@ impl SpeculatedFloatPlacement {
|
|||
placement
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue