use std::num::NonZeroUsize;
use std::str::FromStr;
use comemo::{Track, Tracked, TrackedMut};
use ecow::{eco_format, eco_vec, EcoString, EcoVec};
use smallvec::{smallvec, SmallVec};
use crate::diag::{bail, warning, At, HintedStrResult, SourceResult};
use crate::engine::{Engine, Route, Sink, Traced};
use crate::foundations::{
cast, elem, func, scope, select_where, ty, Args, Array, Construct, Content, Context,
Element, Func, IntoValue, Label, LocatableSelector, NativeElement, Packed, Repr,
Selector, Show, Smart, Str, StyleChain, Value,
};
use crate::introspection::{Introspector, Locatable, Location, Tag};
use crate::layout::{Frame, FrameItem, PageElem};
use crate::math::EquationElem;
use crate::model::{FigureElem, FootnoteElem, HeadingElem, Numbering, NumberingPattern};
use crate::syntax::Span;
use crate::utils::NonZeroExt;
use crate::World;
#[ty(scope)]
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct Counter(CounterKey);
impl Counter {
pub fn new(key: CounterKey) -> Counter {
Self(key)
}
pub fn of(func: Element) -> Self {
Self::new(CounterKey::Selector(Selector::Elem(func, None)))
}
pub fn both(
&self,
engine: &mut Engine,
location: Location,
) -> SourceResult<CounterState> {
let sequence = self.sequence(engine)?;
let offset = engine.introspector.query_count_before(&self.selector(), location);
let (mut at_state, at_page) = sequence[offset].clone();
let (mut final_state, final_page) = sequence.last().unwrap().clone();
if self.is_page() {
let at_delta =
engine.introspector.page(location).get().saturating_sub(at_page.get());
at_state.step(NonZeroUsize::ONE, at_delta);
let final_delta =
engine.introspector.pages().get().saturating_sub(final_page.get());
final_state.step(NonZeroUsize::ONE, final_delta);
}
Ok(CounterState(smallvec![at_state.first(), final_state.first()]))
}
pub fn at_loc(
&self,
engine: &mut Engine,
location: Location,
) -> SourceResult<CounterState> {
let sequence = self.sequence(engine)?;
let offset = engine.introspector.query_count_before(&self.selector(), location);
let (mut state, page) = sequence[offset].clone();
if self.is_page() {
let delta =
engine.introspector.page(location).get().saturating_sub(page.get());
state.step(NonZeroUsize::ONE, delta);
}
Ok(state)
}
pub fn display_at_loc(
&self,
engine: &mut Engine,
loc: Location,
styles: StyleChain,
numbering: &Numbering,
) -> SourceResult<Content> {
let context = Context::new(Some(loc), Some(styles));
Ok(self
.at_loc(engine, loc)?
.display(engine, context.track(), numbering)?
.display())
}
fn sequence(
&self,
engine: &mut Engine,
) -> SourceResult<EcoVec<(CounterState, NonZeroUsize)>> {
self.sequence_impl(
engine.world,
engine.introspector,
engine.traced,
TrackedMut::reborrow_mut(&mut engine.sink),
engine.route.track(),
)
}
#[comemo::memoize]
fn sequence_impl(
&self,
world: Tracked<dyn World + '_>,
introspector: Tracked<Introspector>,
traced: Tracked<Traced>,
sink: TrackedMut<Sink>,
route: Tracked<Route>,
) -> SourceResult<EcoVec<(CounterState, NonZeroUsize)>> {
let mut engine = Engine {
world,
introspector,
traced,
sink,
route: Route::extend(route).unnested(),
};
let mut state = CounterState::init(matches!(self.0, CounterKey::Page));
let mut page = NonZeroUsize::ONE;
let mut stops = eco_vec![(state.clone(), page)];
for elem in introspector.query(&self.selector()) {
if self.is_page() {
let prev = page;
page = introspector.page(elem.location().unwrap());
let delta = page.get() - prev.get();
if delta > 0 {
state.step(NonZeroUsize::ONE, delta);
}
}
if let Some(update) = match elem.with::<dyn Count>() {
Some(countable) => countable.update(),
None => Some(CounterUpdate::Step(NonZeroUsize::ONE)),
} {
state.update(&mut engine, update)?;
}
stops.push((state.clone(), page));
}
Ok(stops)
}
fn selector(&self) -> Selector {
let mut selector = select_where!(CounterUpdateElem, Key => self.0.clone());
if let CounterKey::Selector(key) = &self.0 {
selector = Selector::Or(eco_vec![selector, key.clone()]);
}
selector
}
fn is_page(&self) -> bool {
self.0 == CounterKey::Page
}
fn display_impl(
&self,
engine: &mut Engine,
location: Location,
numbering: Smart<Numbering>,
both: bool,
styles: Option<StyleChain>,
) -> SourceResult<Value> {
let numbering = numbering
.custom()
.or_else(|| {
let styles = styles?;
let CounterKey::Selector(Selector::Elem(func, _)) = self.0 else {
return None;
};
if func == HeadingElem::elem() {
HeadingElem::numbering_in(styles).clone()
} else if func == FigureElem::elem() {
FigureElem::numbering_in(styles).clone()
} else if func == EquationElem::elem() {
EquationElem::numbering_in(styles).clone()
} else if func == FootnoteElem::elem() {
Some(FootnoteElem::numbering_in(styles).clone())
} else {
None
}
})
.unwrap_or_else(|| NumberingPattern::from_str("1.1").unwrap().into());
let state = if both {
self.both(engine, location)?
} else {
self.at_loc(engine, location)?
};
let context = Context::new(Some(location), styles);
state.display(engine, context.track(), &numbering)
}
}
#[scope]
impl Counter {
#[func(constructor)]
pub fn construct(
key: CounterKey,
) -> Counter {
Self::new(key)
}
#[func(contextual)]
pub fn get(
&self,
engine: &mut Engine,
context: Tracked<Context>,
span: Span,
) -> SourceResult<CounterState> {
let loc = context.location().at(span)?;
self.at_loc(engine, loc)
}
#[func(contextual)]
pub fn display(
self,
engine: &mut Engine,
context: Tracked<Context>,
span: Span,
#[default]
numbering: Smart<Numbering>,
#[named]
#[default(false)]
both: bool,
) -> SourceResult<Value> {
if let Ok(loc) = context.location() {
self.display_impl(engine, loc, numbering, both, context.styles().ok())
} else {
engine.sink.warn(warning!(
span, "`counter.display` without context is deprecated";
hint: "use it in a `context` expression instead"
));
Ok(CounterDisplayElem::new(self, numbering, both)
.pack()
.spanned(span)
.into_value())
}
}
#[func(contextual)]
pub fn at(
&self,
engine: &mut Engine,
context: Tracked<Context>,
span: Span,
selector: LocatableSelector,
) -> SourceResult<CounterState> {
let loc = selector.resolve_unique(engine.introspector, context).at(span)?;
self.at_loc(engine, loc)
}
#[func(contextual)]
pub fn final_(
&self,
engine: &mut Engine,
context: Tracked<Context>,
span: Span,
#[default]
location: Option<Location>,
) -> SourceResult<CounterState> {
if location.is_none() {
context.location().at(span)?;
} else {
engine.sink.warn(warning!(
span, "calling `counter.final` with a location is deprecated";
hint: "try removing the location argument"
));
}
let sequence = self.sequence(engine)?;
let (mut state, page) = sequence.last().unwrap().clone();
if self.is_page() {
let delta = engine.introspector.pages().get().saturating_sub(page.get());
state.step(NonZeroUsize::ONE, delta);
}
Ok(state)
}
#[func]
pub fn step(
self,
span: Span,
#[named]
#[default(NonZeroUsize::ONE)]
level: NonZeroUsize,
) -> Content {
self.update(span, CounterUpdate::Step(level))
}
#[func]
pub fn update(
self,
span: Span,
update: CounterUpdate,
) -> Content {
CounterUpdateElem::new(self.0, update).pack().spanned(span)
}
}
impl Repr for Counter {
fn repr(&self) -> EcoString {
eco_format!("counter({})", self.0.repr())
}
}
#[derive(Debug, Clone, PartialEq, Hash)]
pub enum CounterKey {
Page,
Selector(Selector),
Str(Str),
}
cast! {
CounterKey,
self => match self {
Self::Page => PageElem::elem().into_value(),
Self::Selector(v) => v.into_value(),
Self::Str(v) => v.into_value(),
},
v: Str => Self::Str(v),
v: Label => Self::Selector(Selector::Label(v)),
v: Element => {
if v == PageElem::elem() {
Self::Page
} else {
Self::Selector(LocatableSelector::from_value(v.into_value())?.0)
}
},
v: LocatableSelector => Self::Selector(v.0),
}
impl Repr for CounterKey {
fn repr(&self) -> EcoString {
match self {
Self::Page => "page".into(),
Self::Selector(selector) => selector.repr(),
Self::Str(str) => str.repr(),
}
}
}
#[derive(Debug, Clone, PartialEq, Hash)]
pub enum CounterUpdate {
Set(CounterState),
Step(NonZeroUsize),
Func(Func),
}
cast! {
CounterUpdate,
v: CounterState => Self::Set(v),
v: Func => Self::Func(v),
}
pub trait Count {
fn update(&self) -> Option<CounterUpdate>;
}
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct CounterState(pub SmallVec<[usize; 3]>);
impl CounterState {
pub fn init(page: bool) -> Self {
Self(smallvec![usize::from(page)])
}
pub fn update(
&mut self,
engine: &mut Engine,
update: CounterUpdate,
) -> SourceResult<()> {
match update {
CounterUpdate::Set(state) => *self = state,
CounterUpdate::Step(level) => self.step(level, 1),
CounterUpdate::Func(func) => {
*self = func
.call(engine, Context::none().track(), self.0.iter().copied())?
.cast()
.at(func.span())?
}
}
Ok(())
}
pub fn step(&mut self, level: NonZeroUsize, by: usize) {
let level = level.get();
while self.0.len() < level {
self.0.push(0);
}
self.0[level - 1] = self.0[level - 1].saturating_add(by);
self.0.truncate(level);
}
pub fn first(&self) -> usize {
self.0.first().copied().unwrap_or(1)
}
pub fn display(
&self,
engine: &mut Engine,
context: Tracked<Context>,
numbering: &Numbering,
) -> SourceResult<Value> {
numbering.apply(engine, context, &self.0)
}
}
cast! {
CounterState,
self => Value::Array(self.0.into_iter().map(IntoValue::into_value).collect()),
num: usize => Self(smallvec![num]),
array: Array => Self(array
.into_iter()
.map(Value::cast)
.collect::<HintedStrResult<_>>()?),
}
#[elem(Construct, Locatable, Show, Count)]
struct CounterUpdateElem {
#[required]
key: CounterKey,
#[required]
#[internal]
update: CounterUpdate,
}
impl Construct for CounterUpdateElem {
fn construct(_: &mut Engine, args: &mut Args) -> SourceResult<Content> {
bail!(args.span, "cannot be constructed manually");
}
}
impl Show for Packed<CounterUpdateElem> {
fn show(&self, _: &mut Engine, _: StyleChain) -> SourceResult<Content> {
Ok(Content::empty())
}
}
impl Count for Packed<CounterUpdateElem> {
fn update(&self) -> Option<CounterUpdate> {
Some(self.update.clone())
}
}
#[elem(Construct, Locatable, Show)]
pub struct CounterDisplayElem {
#[required]
#[internal]
counter: Counter,
#[required]
#[internal]
numbering: Smart<Numbering>,
#[required]
#[internal]
both: bool,
}
impl Construct for CounterDisplayElem {
fn construct(_: &mut Engine, args: &mut Args) -> SourceResult<Content> {
bail!(args.span, "cannot be constructed manually");
}
}
impl Show for Packed<CounterDisplayElem> {
#[typst_macros::time(name = "counter.display", span = self.span())]
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
Ok(self
.counter
.display_impl(
engine,
self.location().unwrap(),
self.numbering.clone(),
self.both,
Some(styles),
)?
.display())
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct ManualPageCounter {
physical: NonZeroUsize,
logical: usize,
}
impl ManualPageCounter {
pub fn new() -> Self {
Self { physical: NonZeroUsize::ONE, logical: 1 }
}
pub fn physical(&self) -> NonZeroUsize {
self.physical
}
pub fn logical(&self) -> usize {
self.logical
}
pub fn visit(&mut self, engine: &mut Engine, page: &Frame) -> SourceResult<()> {
for (_, item) in page.items() {
match item {
FrameItem::Group(group) => self.visit(engine, &group.frame)?,
FrameItem::Tag(Tag::Start(elem)) => {
let Some(elem) = elem.to_packed::<CounterUpdateElem>() else {
continue;
};
if *elem.key() == CounterKey::Page {
let mut state = CounterState(smallvec![self.logical]);
state.update(engine, elem.update.clone())?;
self.logical = state.first();
}
}
_ => {}
}
}
Ok(())
}
pub fn step(&mut self) {
self.physical = self.physical.saturating_add(1);
self.logical += 1;
}
}
impl Default for ManualPageCounter {
fn default() -> Self {
Self::new()
}
}