use std::borrow::Cow;
use std::num::NonZeroUsize;
use std::ops::RangeInclusive;
use std::str::FromStr;
use comemo::Track;
use crate::diag::{bail, SourceResult};
use crate::engine::Engine;
use crate::foundations::{
cast, elem, Args, AutoValue, Cast, Construct, Content, Context, Dict, Fold, Func,
NativeElement, Set, Smart, StyleChain, Value,
};
use crate::layout::{
Abs, Alignment, FlushElem, Frame, HAlignment, Length, OuterVAlignment, Ratio, Rel,
Sides, SpecificAlignment,
};
use crate::model::Numbering;
use crate::utils::{singleton, NonZeroExt, Scalar};
use crate::visualize::{Color, Paint};
#[elem(Construct)]
pub struct PageElem {
#[external]
#[default(Paper::A4)]
pub paper: Paper,
#[resolve]
#[parse(
let paper = args.named_or_find::<Paper>("paper")?;
args.named("width")?
.or_else(|| paper.map(|paper| Smart::Custom(paper.width().into())))
)]
#[default(Smart::Custom(Paper::A4.width().into()))]
#[ghost]
pub width: Smart<Length>,
#[resolve]
#[parse(
args.named("height")?
.or_else(|| paper.map(|paper| Smart::Custom(paper.height().into())))
)]
#[default(Smart::Custom(Paper::A4.height().into()))]
#[ghost]
pub height: Smart<Length>,
#[default(false)]
#[ghost]
pub flipped: bool,
#[fold]
#[ghost]
pub margin: Margin,
#[ghost]
pub binding: Smart<Binding>,
#[default(NonZeroUsize::ONE)]
#[ghost]
pub columns: NonZeroUsize,
#[borrowed]
#[ghost]
pub fill: Smart<Option<Paint>>,
#[borrowed]
#[ghost]
pub numbering: Option<Numbering>,
#[default(SpecificAlignment::Both(HAlignment::Center, OuterVAlignment::Bottom))]
#[ghost]
pub number_align: SpecificAlignment<HAlignment, OuterVAlignment>,
#[borrowed]
#[ghost]
pub header: Smart<Option<Content>>,
#[resolve]
#[default(Ratio::new(0.3).into())]
#[ghost]
pub header_ascent: Rel<Length>,
#[borrowed]
#[ghost]
pub footer: Smart<Option<Content>>,
#[resolve]
#[default(Ratio::new(0.3).into())]
#[ghost]
pub footer_descent: Rel<Length>,
#[borrowed]
#[ghost]
pub background: Option<Content>,
#[borrowed]
#[ghost]
pub foreground: Option<Content>,
#[external]
#[required]
pub body: Content,
}
impl Construct for PageElem {
fn construct(engine: &mut Engine, args: &mut Args) -> SourceResult<Content> {
let styles = Self::set(engine, args)?;
let body = args.expect::<Content>("body")?;
Ok(Content::sequence([
PagebreakElem::shared_weak().clone(),
FlushElem::new().pack(),
body,
PagebreakElem::shared_boundary().clone(),
])
.styled_with_map(styles))
}
}
#[elem(title = "Page Break")]
pub struct PagebreakElem {
#[default(false)]
pub weak: bool,
pub to: Option<Parity>,
#[internal]
#[parse(None)]
#[default(false)]
pub boundary: bool,
}
impl PagebreakElem {
pub fn shared_weak() -> &'static Content {
singleton!(Content, PagebreakElem::new().with_weak(true).pack())
}
pub fn shared_boundary() -> &'static Content {
singleton!(
Content,
PagebreakElem::new().with_weak(true).with_boundary(true).pack()
)
}
}
#[derive(Debug, Clone)]
pub struct Page {
pub frame: Frame,
pub fill: Smart<Option<Paint>>,
pub numbering: Option<Numbering>,
pub number: usize,
}
impl Page {
pub fn fill_or_transparent(&self) -> Option<Paint> {
self.fill.clone().unwrap_or(None)
}
pub fn fill_or_white(&self) -> Option<Paint> {
self.fill.clone().unwrap_or_else(|| Some(Color::WHITE.into()))
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct Margin {
pub sides: Sides<Option<Smart<Rel<Length>>>>,
pub two_sided: Option<bool>,
}
impl Margin {
pub fn splat(value: Option<Smart<Rel<Length>>>) -> Self {
Self { sides: Sides::splat(value), two_sided: None }
}
}
impl Default for Margin {
fn default() -> Self {
Self {
sides: Sides::splat(Some(Smart::Auto)),
two_sided: None,
}
}
}
impl Fold for Margin {
fn fold(self, outer: Self) -> Self {
Margin {
sides: self.sides.fold(outer.sides),
two_sided: self.two_sided.fold(outer.two_sided),
}
}
}
cast! {
Margin,
self => {
let two_sided = self.two_sided.unwrap_or(false);
if !two_sided && self.sides.is_uniform() {
if let Some(left) = self.sides.left {
return left.into_value();
}
}
let mut dict = Dict::new();
let mut handle = |key: &str, component: Option<Smart<Rel<Length>>>| {
if let Some(c) = component {
dict.insert(key.into(), c.into_value());
}
};
handle("top", self.sides.top);
handle("bottom", self.sides.bottom);
if two_sided {
handle("inside", self.sides.left);
handle("outside", self.sides.right);
} else {
handle("left", self.sides.left);
handle("right", self.sides.right);
}
Value::Dict(dict)
},
_: AutoValue => Self::splat(Some(Smart::Auto)),
v: Rel<Length> => Self::splat(Some(Smart::Custom(v))),
mut dict: Dict => {
let mut take = |key| dict.take(key).ok().map(Value::cast).transpose();
let rest = take("rest")?;
let x = take("x")?.or(rest);
let y = take("y")?.or(rest);
let top = take("top")?.or(y);
let bottom = take("bottom")?.or(y);
let outside = take("outside")?;
let inside = take("inside")?;
let left = take("left")?;
let right = take("right")?;
let implicitly_two_sided = outside.is_some() || inside.is_some();
let implicitly_not_two_sided = left.is_some() || right.is_some();
if implicitly_two_sided && implicitly_not_two_sided {
bail!("`inside` and `outside` are mutually exclusive with `left` and `right`");
}
let two_sided = (implicitly_two_sided || implicitly_not_two_sided)
.then_some(implicitly_two_sided);
dict.finish(&[
"left", "top", "right", "bottom", "outside", "inside", "x", "y", "rest",
])?;
Margin {
sides: Sides {
left: inside.or(left).or(x),
top,
right: outside.or(right).or(x),
bottom,
},
two_sided,
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Binding {
Left,
Right,
}
impl Binding {
pub fn swap(self, number: NonZeroUsize) -> bool {
match self {
Self::Left => number.get() % 2 == 0,
Self::Right => number.get() % 2 == 1,
}
}
}
cast! {
Binding,
self => match self {
Self::Left => Alignment::LEFT.into_value(),
Self::Right => Alignment::RIGHT.into_value(),
},
v: Alignment => match v {
Alignment::LEFT => Self::Left,
Alignment::RIGHT => Self::Right,
_ => bail!("must be `left` or `right`"),
},
}
#[derive(Debug, Clone, Hash)]
pub enum Marginal {
Content(Content),
Func(Func),
}
impl Marginal {
pub fn resolve(
&self,
engine: &mut Engine,
styles: StyleChain,
page: usize,
) -> SourceResult<Cow<'_, Content>> {
Ok(match self {
Self::Content(content) => Cow::Borrowed(content),
Self::Func(func) => Cow::Owned(
func.call(engine, Context::new(None, Some(styles)).track(), [page])?
.display(),
),
})
}
}
cast! {
Marginal,
self => match self {
Self::Content(v) => v.into_value(),
Self::Func(v) => v.into_value(),
},
v: Content => Self::Content(v),
v: Func => Self::Func(v),
}
#[derive(Debug, Clone)]
pub struct PageRanges(Vec<PageRange>);
pub type PageRange = RangeInclusive<Option<NonZeroUsize>>;
impl PageRanges {
pub fn new(ranges: Vec<PageRange>) -> Self {
Self(ranges)
}
pub fn includes_page(&self, page: NonZeroUsize) -> bool {
self.includes_page_index(page.get() - 1)
}
pub fn includes_page_index(&self, page: usize) -> bool {
let page = NonZeroUsize::try_from(page + 1).unwrap();
self.0.iter().any(|range| match (range.start(), range.end()) {
(Some(start), Some(end)) => (start..=end).contains(&&page),
(Some(start), None) => (start..).contains(&&page),
(None, Some(end)) => (..=end).contains(&&page),
(None, None) => true,
})
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)]
pub enum Parity {
Even,
Odd,
}
impl Parity {
pub fn matches(self, number: usize) -> bool {
match self {
Self::Even => number % 2 == 0,
Self::Odd => number % 2 == 1,
}
}
}
#[derive(Debug, Copy, Clone, Hash)]
pub struct Paper {
name: &'static str,
width: Scalar,
height: Scalar,
}
impl Paper {
pub fn width(self) -> Abs {
Abs::mm(self.width.get())
}
pub fn height(self) -> Abs {
Abs::mm(self.height.get())
}
}
macro_rules! papers {
($(($var:ident: $width:expr, $height: expr, $name:literal))*) => {
impl Paper {
$(pub const $var: Self = Self {
name: $name,
width: Scalar::new($width),
height: Scalar::new($height),
};)*
}
impl FromStr for Paper {
type Err = &'static str;
fn from_str(name: &str) -> Result<Self, Self::Err> {
match name.to_lowercase().as_str() {
$($name => Ok(Self::$var),)*
_ => Err("unknown paper size"),
}
}
}
cast! {
Paper,
self => self.name.into_value(),
$(
$name => Self::$var,
)*
}
};
}
papers! {
(A0: 841.0, 1189.0, "a0")
(A1: 594.0, 841.0, "a1")
(A2: 420.0, 594.0, "a2")
(A3: 297.0, 420.0, "a3")
(A4: 210.0, 297.0, "a4")
(A5: 148.0, 210.0, "a5")
(A6: 105.0, 148.0, "a6")
(A7: 74.0, 105.0, "a7")
(A8: 52.0, 74.0, "a8")
(A9: 37.0, 52.0, "a9")
(A10: 26.0, 37.0, "a10")
(A11: 18.0, 26.0, "a11")
(ISO_B1: 707.0, 1000.0, "iso-b1")
(ISO_B2: 500.0, 707.0, "iso-b2")
(ISO_B3: 353.0, 500.0, "iso-b3")
(ISO_B4: 250.0, 353.0, "iso-b4")
(ISO_B5: 176.0, 250.0, "iso-b5")
(ISO_B6: 125.0, 176.0, "iso-b6")
(ISO_B7: 88.0, 125.0, "iso-b7")
(ISO_B8: 62.0, 88.0, "iso-b8")
(ISO_C3: 324.0, 458.0, "iso-c3")
(ISO_C4: 229.0, 324.0, "iso-c4")
(ISO_C5: 162.0, 229.0, "iso-c5")
(ISO_C6: 114.0, 162.0, "iso-c6")
(ISO_C7: 81.0, 114.0, "iso-c7")
(ISO_C8: 57.0, 81.0, "iso-c8")
(DIN_D3: 272.0, 385.0, "din-d3")
(DIN_D4: 192.0, 272.0, "din-d4")
(DIN_D5: 136.0, 192.0, "din-d5")
(DIN_D6: 96.0, 136.0, "din-d6")
(DIN_D7: 68.0, 96.0, "din-d7")
(DIN_D8: 48.0, 68.0, "din-d8")
(SIS_G5: 169.0, 239.0, "sis-g5")
(SIS_E5: 115.0, 220.0, "sis-e5")
(ANSI_A: 216.0, 279.0, "ansi-a")
(ANSI_B: 279.0, 432.0, "ansi-b")
(ANSI_C: 432.0, 559.0, "ansi-c")
(ANSI_D: 559.0, 864.0, "ansi-d")
(ANSI_E: 864.0, 1118.0, "ansi-e")
(ARCH_A: 229.0, 305.0, "arch-a")
(ARCH_B: 305.0, 457.0, "arch-b")
(ARCH_C: 457.0, 610.0, "arch-c")
(ARCH_D: 610.0, 914.0, "arch-d")
(ARCH_E1: 762.0, 1067.0, "arch-e1")
(ARCH_E: 914.0, 1219.0, "arch-e")
(JIS_B0: 1030.0, 1456.0, "jis-b0")
(JIS_B1: 728.0, 1030.0, "jis-b1")
(JIS_B2: 515.0, 728.0, "jis-b2")
(JIS_B3: 364.0, 515.0, "jis-b3")
(JIS_B4: 257.0, 364.0, "jis-b4")
(JIS_B5: 182.0, 257.0, "jis-b5")
(JIS_B6: 128.0, 182.0, "jis-b6")
(JIS_B7: 91.0, 128.0, "jis-b7")
(JIS_B8: 64.0, 91.0, "jis-b8")
(JIS_B9: 45.0, 64.0, "jis-b9")
(JIS_B10: 32.0, 45.0, "jis-b10")
(JIS_B11: 22.0, 32.0, "jis-b11")
(SAC_D0: 764.0, 1064.0, "sac-d0")
(SAC_D1: 532.0, 760.0, "sac-d1")
(SAC_D2: 380.0, 528.0, "sac-d2")
(SAC_D3: 264.0, 376.0, "sac-d3")
(SAC_D4: 188.0, 260.0, "sac-d4")
(SAC_D5: 130.0, 184.0, "sac-d5")
(SAC_D6: 92.0, 126.0, "sac-d6")
(ISO_ID_1: 85.6, 53.98, "iso-id-1")
(ISO_ID_2: 74.0, 105.0, "iso-id-2")
(ISO_ID_3: 88.0, 125.0, "iso-id-3")
(ASIA_F4: 210.0, 330.0, "asia-f4")
(JP_SHIROKU_BAN_4: 264.0, 379.0, "jp-shiroku-ban-4")
(JP_SHIROKU_BAN_5: 189.0, 262.0, "jp-shiroku-ban-5")
(JP_SHIROKU_BAN_6: 127.0, 188.0, "jp-shiroku-ban-6")
(JP_KIKU_4: 227.0, 306.0, "jp-kiku-4")
(JP_KIKU_5: 151.0, 227.0, "jp-kiku-5")
(JP_BUSINESS_CARD: 91.0, 55.0, "jp-business-card")
(CN_BUSINESS_CARD: 90.0, 54.0, "cn-business-card")
(EU_BUSINESS_CARD: 85.0, 55.0, "eu-business-card")
(FR_TELLIERE: 340.0, 440.0, "fr-tellière")
(FR_COURONNE_ECRITURE: 360.0, 460.0, "fr-couronne-écriture")
(FR_COURONNE_EDITION: 370.0, 470.0, "fr-couronne-édition")
(FR_RAISIN: 500.0, 650.0, "fr-raisin")
(FR_CARRE: 450.0, 560.0, "fr-carré")
(FR_JESUS: 560.0, 760.0, "fr-jésus")
(UK_BRIEF: 406.4, 342.9, "uk-brief")
(UK_DRAFT: 254.0, 406.4, "uk-draft")
(UK_FOOLSCAP: 203.2, 330.2, "uk-foolscap")
(UK_QUARTO: 203.2, 254.0, "uk-quarto")
(UK_CROWN: 508.0, 381.0, "uk-crown")
(UK_BOOK_A: 111.0, 178.0, "uk-book-a")
(UK_BOOK_B: 129.0, 198.0, "uk-book-b")
(US_LETTER: 215.9, 279.4, "us-letter")
(US_LEGAL: 215.9, 355.6, "us-legal")
(US_TABLOID: 279.4, 431.8, "us-tabloid")
(US_EXECUTIVE: 84.15, 266.7, "us-executive")
(US_FOOLSCAP_FOLIO: 215.9, 342.9, "us-foolscap-folio")
(US_STATEMENT: 139.7, 215.9, "us-statement")
(US_LEDGER: 431.8, 279.4, "us-ledger")
(US_OFICIO: 215.9, 340.36, "us-oficio")
(US_GOV_LETTER: 203.2, 266.7, "us-gov-letter")
(US_GOV_LEGAL: 215.9, 330.2, "us-gov-legal")
(US_BUSINESS_CARD: 88.9, 50.8, "us-business-card")
(US_DIGEST: 139.7, 215.9, "us-digest")
(US_TRADE: 152.4, 228.6, "us-trade")
(NEWSPAPER_COMPACT: 280.0, 430.0, "newspaper-compact")
(NEWSPAPER_BERLINER: 315.0, 470.0, "newspaper-berliner")
(NEWSPAPER_BROADSHEET: 381.0, 578.0, "newspaper-broadsheet")
(PRESENTATION_16_9: 297.0, 167.0625, "presentation-16-9")
(PRESENTATION_4_3: 280.0, 210.0, "presentation-4-3")
}