mirror of
https://github.com/typst/typst
synced 2025-06-08 21:26:24 +08:00
Port PageLabel conversion
This commit is contained in:
parent
f53ac9bfd9
commit
3ff40ed877
@ -1,7 +1,10 @@
|
|||||||
//! Convert paint types from typst to krilla.
|
//! Convert paint types from typst to krilla.
|
||||||
|
|
||||||
|
use std::num::NonZeroUsize;
|
||||||
use krilla::geom::NormalizedF32;
|
use krilla::geom::NormalizedF32;
|
||||||
|
use krilla::page::{NumberingStyle, PageLabel};
|
||||||
use typst_library::layout::Abs;
|
use typst_library::layout::Abs;
|
||||||
|
use typst_library::model::Numbering;
|
||||||
use typst_library::visualize::{ColorSpace, DashPattern, FillRule, FixedStroke, Paint};
|
use typst_library::visualize::{ColorSpace, DashPattern, FillRule, FixedStroke, Paint};
|
||||||
|
|
||||||
use crate::AbsExt;
|
use crate::AbsExt;
|
||||||
@ -55,3 +58,58 @@ fn paint(paint: &Paint) -> (krilla::paint::Paint, u8) {
|
|||||||
Paint::Pattern(_) => (krilla::color::rgb::Color::black().into(), 255),
|
Paint::Pattern(_) => (krilla::color::rgb::Color::black().into(), 255),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) trait PageLabelExt {
|
||||||
|
fn generate(numbering: &Numbering, number: usize) -> Option<PageLabel>;
|
||||||
|
fn arabic(number: usize) -> PageLabel;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PageLabelExt for PageLabel {
|
||||||
|
/// Create a new `PageLabel` from a `Numbering` applied to a page
|
||||||
|
/// number.
|
||||||
|
fn generate(numbering: &Numbering, number: usize) -> Option<PageLabel> {
|
||||||
|
{
|
||||||
|
let Numbering::Pattern(pat) = numbering else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
|
||||||
|
let (prefix, kind) = pat.pieces.first()?;
|
||||||
|
|
||||||
|
// If there is a suffix, we cannot use the common style optimisation,
|
||||||
|
// since PDF does not provide a suffix field.
|
||||||
|
let style = if pat.suffix.is_empty() {
|
||||||
|
use typst_library::model::NumberingKind as Kind;
|
||||||
|
use krilla::page::NumberingStyle as Style;
|
||||||
|
match kind {
|
||||||
|
Kind::Arabic => Some(Style::Arabic),
|
||||||
|
Kind::LowerRoman => Some(Style::LowerRoman),
|
||||||
|
Kind::UpperRoman => Some(Style::UpperRoman),
|
||||||
|
Kind::LowerLatin if number <= 26 => Some(Style::LowerAlpha),
|
||||||
|
Kind::LowerLatin if number <= 26 => Some(Style::UpperAlpha),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
// Prefix and offset depend on the style: If it is supported by the PDF
|
||||||
|
// spec, we use the given prefix and an offset. Otherwise, everything
|
||||||
|
// goes into prefix.
|
||||||
|
let prefix = if style.is_none() {
|
||||||
|
Some(pat.apply(&[number]))
|
||||||
|
} else {
|
||||||
|
(!prefix.is_empty()).then(|| prefix.clone())
|
||||||
|
};
|
||||||
|
|
||||||
|
let offset = style.and(NonZeroUsize::new(number));
|
||||||
|
Some(PageLabel::new(style, prefix.map(|s| s.to_string()), offset))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates an arabic page label with the specified page number.
|
||||||
|
/// For example, this will display page label `11` when given the page
|
||||||
|
/// number 11.
|
||||||
|
fn arabic(number: usize) -> PageLabel {
|
||||||
|
PageLabel::new(Some(NumberingStyle::Arabic), None, NonZeroUsize::new(number))
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user