use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Keep {
#[default]
Auto,
Always,
Integer(i32),
}
impl Keep {
pub fn from_property_value(value: &fop_core::PropertyValue) -> Self {
if value.is_auto() {
Keep::Auto
} else if let Some(s) = value.as_string() {
if s == "always" {
Keep::Always
} else {
Keep::Auto
}
} else if let Some(i) = value.as_integer() {
Keep::Integer(i)
} else {
Keep::Auto
}
}
pub fn is_active(&self) -> bool {
!matches!(self, Keep::Auto)
}
pub fn strength(&self) -> i32 {
match self {
Keep::Auto => 0,
Keep::Always => i32::MAX,
Keep::Integer(i) => *i,
}
}
}
impl fmt::Display for Keep {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Keep::Auto => write!(f, "auto"),
Keep::Always => write!(f, "always"),
Keep::Integer(i) => write!(f, "{}", i),
}
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct KeepConstraint {
pub keep_together: Keep,
pub keep_with_next: Keep,
pub keep_with_previous: Keep,
}
impl KeepConstraint {
pub fn new() -> Self {
Self::default()
}
pub fn has_constraint(&self) -> bool {
self.keep_together.is_active()
|| self.keep_with_next.is_active()
|| self.keep_with_previous.is_active()
}
pub fn must_keep_together(&self) -> bool {
self.keep_together.is_active()
}
pub fn must_keep_with_next(&self) -> bool {
self.keep_with_next.is_active()
}
pub fn must_keep_with_previous(&self) -> bool {
self.keep_with_previous.is_active()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BreakValue {
#[default]
Auto,
Always,
Page,
Column,
EvenPage,
OddPage,
}
impl BreakValue {
pub fn from_property_value(value: &fop_core::PropertyValue) -> Self {
if value.is_auto() {
BreakValue::Auto
} else if let Some(s) = value.as_string() {
match s {
"always" => BreakValue::Always,
"page" => BreakValue::Page,
"column" => BreakValue::Column,
"even-page" => BreakValue::EvenPage,
"odd-page" => BreakValue::OddPage,
_ => BreakValue::Auto,
}
} else {
BreakValue::Auto
}
}
pub fn forces_break(&self) -> bool {
!matches!(self, BreakValue::Auto)
}
pub fn forces_page_break(&self) -> bool {
matches!(
self,
BreakValue::Always | BreakValue::Page | BreakValue::EvenPage | BreakValue::OddPage
)
}
pub fn requires_even_page(&self) -> bool {
matches!(self, BreakValue::EvenPage)
}
pub fn requires_odd_page(&self) -> bool {
matches!(self, BreakValue::OddPage)
}
}
impl fmt::Display for BreakValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BreakValue::Auto => write!(f, "auto"),
BreakValue::Always => write!(f, "always"),
BreakValue::Page => write!(f, "page"),
BreakValue::Column => write!(f, "column"),
BreakValue::EvenPage => write!(f, "even-page"),
BreakValue::OddPage => write!(f, "odd-page"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum OverflowBehavior {
#[default]
Visible,
Hidden,
Scroll,
Auto,
}
impl OverflowBehavior {
pub fn from_property_value(value: &fop_core::PropertyValue) -> Self {
if let Some(s) = value.as_string() {
match s {
"hidden" => OverflowBehavior::Hidden,
"scroll" => OverflowBehavior::Scroll,
"auto" => OverflowBehavior::Auto,
"visible" => OverflowBehavior::Visible,
_ => OverflowBehavior::Visible,
}
} else {
OverflowBehavior::Visible
}
}
pub fn clips_content(&self) -> bool {
matches!(self, OverflowBehavior::Hidden | OverflowBehavior::Scroll)
}
}
impl fmt::Display for OverflowBehavior {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
OverflowBehavior::Visible => write!(f, "visible"),
OverflowBehavior::Hidden => write!(f, "hidden"),
OverflowBehavior::Scroll => write!(f, "scroll"),
OverflowBehavior::Auto => write!(f, "auto"),
}
}
}