use std::vec::IntoIter;
use app_units::Au;
use fonts::FontMetrics;
use serde::Serialize;
use servo_arc::Arc;
use style::properties::ComputedValues;
use super::{inline_container_needs_strut, InlineContainerState, InlineContainerStateFlags};
use crate::cell::ArcRefCell;
use crate::context::LayoutContext;
use crate::dom::NodeExt;
use crate::dom_traversal::NodeAndStyleInfo;
use crate::fragment_tree::BaseFragmentInfo;
use crate::style_ext::{ComputedValuesExt, PaddingBorderMargin};
use crate::ContainingBlock;
#[derive(Debug, Serialize)]
pub(crate) struct InlineBox {
pub base_fragment_info: BaseFragmentInfo,
#[serde(skip_serializing)]
pub style: Arc<ComputedValues>,
pub(super) identifier: InlineBoxIdentifier,
pub is_first_fragment: bool,
pub is_last_fragment: bool,
pub default_font_index: Option<usize>,
}
impl InlineBox {
pub(crate) fn new<'dom, Node: NodeExt<'dom>>(info: &NodeAndStyleInfo<Node>) -> Self {
Self {
base_fragment_info: info.into(),
style: info.style.clone(),
identifier: InlineBoxIdentifier::default(),
is_first_fragment: true,
is_last_fragment: false,
default_font_index: None,
}
}
pub(crate) fn split_around_block(&self) -> Self {
Self {
style: self.style.clone(),
is_first_fragment: false,
is_last_fragment: false,
..*self
}
}
}
#[derive(Debug, Default, Serialize)]
pub(crate) struct InlineBoxes {
inline_boxes: Vec<ArcRefCell<InlineBox>>,
inline_box_tree: Vec<InlineBoxTreePathToken>,
}
impl InlineBoxes {
pub(super) fn len(&self) -> usize {
self.inline_boxes.len()
}
pub(super) fn get(&self, identifier: &InlineBoxIdentifier) -> ArcRefCell<InlineBox> {
self.inline_boxes[identifier.index_in_inline_boxes as usize].clone()
}
pub(super) fn end_inline_box(&mut self, identifier: InlineBoxIdentifier) {
self.inline_box_tree
.push(InlineBoxTreePathToken::End(identifier));
}
pub(super) fn start_inline_box(&mut self, mut inline_box: InlineBox) -> InlineBoxIdentifier {
assert!(self.inline_boxes.len() <= u32::MAX as usize);
assert!(self.inline_box_tree.len() <= u32::MAX as usize);
let index_in_inline_boxes = self.inline_boxes.len() as u32;
let index_of_start_in_tree = self.inline_box_tree.len() as u32;
let identifier = InlineBoxIdentifier {
index_of_start_in_tree,
index_in_inline_boxes,
};
inline_box.identifier = identifier;
self.inline_boxes.push(ArcRefCell::new(inline_box));
self.inline_box_tree
.push(InlineBoxTreePathToken::Start(identifier));
identifier
}
pub(super) fn get_path(
&self,
from: Option<InlineBoxIdentifier>,
to: InlineBoxIdentifier,
) -> IntoIter<InlineBoxTreePathToken> {
if from == Some(to) {
return Vec::new().into_iter();
}
let mut from_index = match from {
Some(InlineBoxIdentifier {
index_of_start_in_tree,
..
}) => index_of_start_in_tree as usize,
None => 0,
};
let mut to_index = to.index_of_start_in_tree as usize;
let is_reversed = to_index < from_index;
if to_index > from_index && from.is_some() {
from_index += 1;
} else if to_index < from_index {
to_index += 1;
}
let mut path = Vec::with_capacity(from_index.abs_diff(to_index));
let min = from_index.min(to_index);
let max = from_index.max(to_index);
for token in &self.inline_box_tree[min..=max] {
if Some(&token.reverse()) == path.last() {
path.pop();
} else {
path.push(*token);
}
}
if is_reversed {
path.reverse();
for token in path.iter_mut() {
*token = token.reverse();
}
}
path.into_iter()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
pub(super) enum InlineBoxTreePathToken {
Start(InlineBoxIdentifier),
End(InlineBoxIdentifier),
}
impl InlineBoxTreePathToken {
fn reverse(&self) -> Self {
match self {
Self::Start(index) => Self::End(*index),
Self::End(index) => Self::Start(*index),
}
}
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq, Serialize)]
pub(crate) struct InlineBoxIdentifier {
pub index_of_start_in_tree: u32,
pub index_in_inline_boxes: u32,
}
pub(super) struct InlineBoxContainerState {
pub base: InlineContainerState,
pub identifier: InlineBoxIdentifier,
pub base_fragment_info: BaseFragmentInfo,
pub pbm: PaddingBorderMargin,
pub is_last_fragment: bool,
}
impl InlineBoxContainerState {
pub(super) fn new(
inline_box: &InlineBox,
containing_block: &ContainingBlock,
layout_context: &LayoutContext,
parent_container: &InlineContainerState,
is_last_fragment: bool,
font_metrics: Option<&FontMetrics>,
) -> Self {
let style = inline_box.style.clone();
let pbm = style.padding_border_margin(containing_block);
let mut flags = InlineContainerStateFlags::empty();
if inline_container_needs_strut(&style, layout_context, Some(&pbm)) {
flags.insert(InlineContainerStateFlags::CREATE_STRUT);
}
Self {
base: InlineContainerState::new(
style,
flags,
Some(parent_container),
parent_container.text_decoration_line,
font_metrics,
),
identifier: inline_box.identifier,
base_fragment_info: inline_box.base_fragment_info,
pbm,
is_last_fragment,
}
}
pub(super) fn calculate_space_above_baseline(&self) -> Au {
let (ascent, descent, line_gap) = (
self.base.font_metrics.ascent,
self.base.font_metrics.descent,
self.base.font_metrics.line_gap,
);
let leading = line_gap - (ascent + descent);
leading.scale_by(0.5) + ascent
}
}