mirror of
https://github.com/typst/typst
synced 2025-08-23 11:14:13 +08:00
Compare commits
39 Commits
46229e73ba
...
bffd208691
Author | SHA1 | Date | |
---|---|---|---|
|
bffd208691 | ||
|
671ca337d4 | ||
|
8055ecc769 | ||
|
d6fea42984 | ||
|
ad9fc0cafb | ||
|
fa6d5f2c51 | ||
|
40f3b7756c | ||
|
9f0b320729 | ||
|
40b438e1ec | ||
|
0de83dbb05 | ||
|
1d90860357 | ||
|
298835e6fe | ||
|
5c44d2222a | ||
|
45759253d3 | ||
|
67f9b04617 | ||
|
1bf8c37a19 | ||
|
bcf40aa755 | ||
|
53e3d50611 | ||
|
d982bdba49 | ||
|
3d36f75e86 | ||
|
761ae38fcd | ||
|
8ee990c453 | ||
|
a80502a110 | ||
|
6f18f28a21 | ||
|
f3810dd36a | ||
|
6eaa12fab8 | ||
|
41b57697d9 | ||
|
74b1b10986 | ||
|
584dd5fec6 | ||
|
b9f3a95e03 | ||
|
e8ce894ee7 | ||
|
9311f6f08e | ||
|
7420ec972f | ||
|
5dd5771df0 | ||
|
04fd0acaca | ||
|
6a1d6c08e2 | ||
|
35809387f8 | ||
|
d3caedd813 | ||
|
d54544297b |
6
Cargo.lock
generated
6
Cargo.lock
generated
@ -413,7 +413,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "codex"
|
||||
version = "0.1.1"
|
||||
source = "git+https://github.com/typst/codex?rev=56eb217#56eb2172fc0670f4c1c8b79a63d11f9354e5babe"
|
||||
source = "git+https://github.com/typst/codex?rev=a5428cb#a5428cb9c81a41354d44b44dbd5a16a710bbd928"
|
||||
|
||||
[[package]]
|
||||
name = "color-print"
|
||||
@ -1367,7 +1367,6 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "krilla"
|
||||
version = "0.4.0"
|
||||
source = "git+https://github.com/LaurenzV/krilla?rev=20c14fe#20c14fefee5002566b3d6668b338bbe2168784e7"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"bumpalo",
|
||||
@ -1395,7 +1394,6 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "krilla-svg"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/LaurenzV/krilla?rev=20c14fe#20c14fefee5002566b3d6668b338bbe2168784e7"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"fontdb",
|
||||
@ -2911,7 +2909,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "typst-dev-assets"
|
||||
version = "0.13.1"
|
||||
source = "git+https://github.com/typst/typst-dev-assets?rev=fddbf8b#fddbf8b99506bc370ac0edcd4959add603a7fc92"
|
||||
source = "git+https://github.com/typst/typst-dev-assets?rev=bfa947f#bfa947f3433d7d13a995168c40ae788a2ebfe648"
|
||||
|
||||
[[package]]
|
||||
name = "typst-docs"
|
||||
|
@ -33,7 +33,7 @@ typst-syntax = { path = "crates/typst-syntax", version = "0.13.1" }
|
||||
typst-timing = { path = "crates/typst-timing", version = "0.13.1" }
|
||||
typst-utils = { path = "crates/typst-utils", version = "0.13.1" }
|
||||
typst-assets = { git = "https://github.com/typst/typst-assets", rev = "c1089b4" }
|
||||
typst-dev-assets = { git = "https://github.com/typst/typst-dev-assets", rev = "fddbf8b" }
|
||||
typst-dev-assets = { git = "https://github.com/typst/typst-dev-assets", rev = "bfa947f" }
|
||||
arrayvec = "0.7.4"
|
||||
az = "1.2"
|
||||
base64 = "0.22"
|
||||
@ -47,7 +47,7 @@ clap = { version = "4.4", features = ["derive", "env", "wrap_help"] }
|
||||
clap_complete = "4.2.1"
|
||||
clap_mangen = "0.2.10"
|
||||
codespan-reporting = "0.11"
|
||||
codex = { git = "https://github.com/typst/codex", rev = "56eb217" }
|
||||
codex = { git = "https://github.com/typst/codex", rev = "a5428cb" }
|
||||
color-print = "0.3.6"
|
||||
comemo = "0.4"
|
||||
csv = "1"
|
||||
@ -73,8 +73,8 @@ image = { version = "0.25.5", default-features = false, features = ["png", "jpeg
|
||||
indexmap = { version = "2", features = ["serde"] }
|
||||
infer = { version = "0.19.0", default-features = false }
|
||||
kamadak-exif = "0.6"
|
||||
krilla = { git = "https://github.com/LaurenzV/krilla", rev = "20c14fe", default-features = false, features = ["raster-images", "comemo", "rayon"] }
|
||||
krilla-svg = { git = "https://github.com/LaurenzV/krilla", rev = "20c14fe" }
|
||||
krilla = { path = "../krilla/crates/krilla", default-features = false, features = ["raster-images", "comemo", "rayon"] }
|
||||
krilla-svg = { path = "../krilla/crates/krilla-svg" }
|
||||
kurbo = "0.11"
|
||||
libfuzzer-sys = "0.4"
|
||||
lipsum = "0.9"
|
||||
|
@ -246,6 +246,13 @@ pub struct CompileArgs {
|
||||
#[arg(long = "pdf-standard", value_delimiter = ',')]
|
||||
pub pdf_standard: Vec<PdfStandard>,
|
||||
|
||||
/// By default, even when not producing a `PDF/UA-1` document, a tagged PDF
|
||||
/// document is written to provide a baseline of accessibility. In some
|
||||
/// circumstances (for example when trying to reduce the size of a document)
|
||||
/// it can be desirable to disable tagged PDF.
|
||||
#[arg(long = "disable-pdf-tags")]
|
||||
pub disable_pdf_tags: bool,
|
||||
|
||||
/// The PPI (pixels per inch) to use for PNG export.
|
||||
#[arg(long = "ppi", default_value_t = 144.0)]
|
||||
pub ppi: f32,
|
||||
@ -506,6 +513,9 @@ pub enum PdfStandard {
|
||||
/// PDF/A-4e.
|
||||
#[value(name = "a-4e")]
|
||||
A_4e,
|
||||
/// PDF/UA-1.
|
||||
#[value(name = "ua-1")]
|
||||
Ua_1,
|
||||
}
|
||||
|
||||
display_possible_values!(PdfStandard);
|
||||
|
@ -65,6 +65,8 @@ pub struct CompileConfig {
|
||||
pub open: Option<Option<String>>,
|
||||
/// A list of standards the PDF should conform to.
|
||||
pub pdf_standards: PdfStandards,
|
||||
/// Whether to write PDF (accessibility) tags.
|
||||
pub disable_pdf_tags: bool,
|
||||
/// A path to write a Makefile rule describing the current compilation.
|
||||
pub make_deps: Option<PathBuf>,
|
||||
/// The PPI (pixels per inch) to use for PNG export.
|
||||
@ -150,6 +152,7 @@ impl CompileConfig {
|
||||
output_format,
|
||||
pages,
|
||||
pdf_standards,
|
||||
disable_pdf_tags: args.disable_pdf_tags,
|
||||
creation_timestamp: args.world.creation_timestamp,
|
||||
make_deps: args.make_deps.clone(),
|
||||
ppi: args.ppi,
|
||||
@ -291,6 +294,7 @@ fn export_pdf(document: &PagedDocument, config: &CompileConfig) -> SourceResult<
|
||||
timestamp,
|
||||
page_ranges: config.pages.clone(),
|
||||
standards: config.pdf_standards.clone(),
|
||||
disable_tags: config.disable_pdf_tags,
|
||||
};
|
||||
let buffer = typst_pdf::pdf(document, &options)?;
|
||||
config
|
||||
@ -773,6 +777,7 @@ impl From<PdfStandard> for typst_pdf::PdfStandard {
|
||||
PdfStandard::A_4 => typst_pdf::PdfStandard::A_4,
|
||||
PdfStandard::A_4f => typst_pdf::PdfStandard::A_4f,
|
||||
PdfStandard::A_4e => typst_pdf::PdfStandard::A_4e,
|
||||
PdfStandard::Ua_1 => typst_pdf::PdfStandard::Ua_1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -205,7 +205,9 @@ impl Eval for ast::Label<'_> {
|
||||
type Output = Value;
|
||||
|
||||
fn eval(self, _: &mut Vm) -> SourceResult<Self::Output> {
|
||||
Ok(Value::Label(Label::new(PicoStr::intern(self.get()))))
|
||||
Ok(Value::Label(
|
||||
Label::new(PicoStr::intern(self.get())).expect("unexpected empty label"),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@ -213,7 +215,8 @@ impl Eval for ast::Ref<'_> {
|
||||
type Output = Content;
|
||||
|
||||
fn eval(self, vm: &mut Vm) -> SourceResult<Self::Output> {
|
||||
let target = Label::new(PicoStr::intern(self.target()));
|
||||
let target = Label::new(PicoStr::intern(self.target()))
|
||||
.expect("unexpected empty reference");
|
||||
let mut elem = RefElem::new(target);
|
||||
if let Some(supplement) = self.supplement() {
|
||||
elem.push_supplement(Smart::Custom(Some(Supplement::Content(
|
||||
|
@ -3,9 +3,8 @@ use std::fmt::Write;
|
||||
use typst_library::diag::{bail, At, SourceResult, StrResult};
|
||||
use typst_library::foundations::Repr;
|
||||
use typst_library::html::{
|
||||
attr, charsets, tag, HtmlDocument, HtmlElement, HtmlNode, HtmlTag,
|
||||
attr, charsets, tag, HtmlDocument, HtmlElement, HtmlFrame, HtmlNode, HtmlTag,
|
||||
};
|
||||
use typst_library::layout::Frame;
|
||||
use typst_syntax::Span;
|
||||
|
||||
/// Encodes an HTML document into a string.
|
||||
@ -304,9 +303,15 @@ fn write_escape(w: &mut Writer, c: char) -> StrResult<()> {
|
||||
}
|
||||
|
||||
/// Encode a laid out frame into the writer.
|
||||
fn write_frame(w: &mut Writer, frame: &Frame) {
|
||||
fn write_frame(w: &mut Writer, frame: &HtmlFrame) {
|
||||
// FIXME: This string replacement is obviously a hack.
|
||||
let svg = typst_svg::svg_frame(frame)
|
||||
.replace("<svg class", "<svg style=\"overflow: visible;\" class");
|
||||
let svg = typst_svg::svg_frame(&frame.inner).replace(
|
||||
"<svg class",
|
||||
&format!(
|
||||
"<svg style=\"overflow: visible; width: {}em; height: {}em;\" class",
|
||||
frame.inner.width() / frame.text_size,
|
||||
frame.inner.height() / frame.text_size,
|
||||
),
|
||||
);
|
||||
w.buf.push_str(&svg);
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ use typst_library::diag::{bail, warning, At, SourceResult};
|
||||
use typst_library::engine::{Engine, Route, Sink, Traced};
|
||||
use typst_library::foundations::{Content, StyleChain, Target, TargetElem};
|
||||
use typst_library::html::{
|
||||
attr, tag, FrameElem, HtmlDocument, HtmlElem, HtmlElement, HtmlNode,
|
||||
attr, tag, FrameElem, HtmlDocument, HtmlElem, HtmlElement, HtmlFrame, HtmlNode,
|
||||
};
|
||||
use typst_library::introspection::{
|
||||
Introspector, Locator, LocatorLink, SplitLocator, TagElem,
|
||||
@ -246,7 +246,10 @@ fn handle(
|
||||
styles.chain(&style),
|
||||
Region::new(Size::splat(Abs::inf()), Axes::splat(false)),
|
||||
)?;
|
||||
output.push(HtmlNode::Frame(frame));
|
||||
output.push(HtmlNode::Frame(HtmlFrame {
|
||||
inner: frame,
|
||||
text_size: TextElem::size_in(styles),
|
||||
}));
|
||||
} else {
|
||||
engine.sink.warn(warning!(
|
||||
child.span(),
|
||||
|
@ -448,7 +448,7 @@ fn field_access_completions(
|
||||
match value {
|
||||
Value::Symbol(symbol) => {
|
||||
for modifier in symbol.modifiers() {
|
||||
if let Ok(modified) = symbol.clone().modified(modifier) {
|
||||
if let Ok(modified) = symbol.clone().modified((), modifier) {
|
||||
ctx.completions.push(Completion {
|
||||
kind: CompletionKind::Symbol(modified.get()),
|
||||
label: modifier.into(),
|
||||
|
@ -72,7 +72,8 @@ pub fn definition(
|
||||
|
||||
// Try to jump to the referenced content.
|
||||
DerefTarget::Ref(node) => {
|
||||
let label = Label::new(PicoStr::intern(node.cast::<ast::Ref>()?.target()));
|
||||
let label = Label::new(PicoStr::intern(node.cast::<ast::Ref>()?.target()))
|
||||
.expect("unexpected empty reference");
|
||||
let selector = Selector::Label(label);
|
||||
let elem = document?.introspector.query_first(&selector)?;
|
||||
return Some(Definition::Span(elem.span()));
|
||||
|
@ -93,7 +93,7 @@ impl Item<'_, '_> {
|
||||
Self::Frame(frame, _) => {
|
||||
frame.size().is_zero()
|
||||
&& frame.items().all(|(_, item)| {
|
||||
matches!(item, FrameItem::Link(_, _) | FrameItem::Tag(_))
|
||||
matches!(item, FrameItem::Link(..) | FrameItem::Tag(_))
|
||||
})
|
||||
}
|
||||
Self::Placed(_, placed) => !placed.float,
|
||||
|
@ -219,7 +219,7 @@ fn collect_items<'a>(
|
||||
// Add fallback text to expand the line height, if necessary.
|
||||
if !items.iter().any(|item| matches!(item, Item::Text(_))) {
|
||||
if let Some(fallback) = fallback {
|
||||
items.push(fallback);
|
||||
items.push(fallback, usize::MAX);
|
||||
}
|
||||
}
|
||||
|
||||
@ -270,10 +270,10 @@ fn collect_range<'a>(
|
||||
items: &mut Items<'a>,
|
||||
fallback: &mut Option<ItemEntry<'a>>,
|
||||
) {
|
||||
for (subrange, item) in p.slice(range.clone()) {
|
||||
for (idx, (subrange, item)) in p.slice(range.clone()).enumerate() {
|
||||
// All non-text items are just kept, they can't be split.
|
||||
let Item::Text(shaped) = item else {
|
||||
items.push(item);
|
||||
items.push(item, idx);
|
||||
continue;
|
||||
};
|
||||
|
||||
@ -293,10 +293,10 @@ fn collect_range<'a>(
|
||||
} else if split {
|
||||
// When the item is split in half, reshape it.
|
||||
let reshaped = shaped.reshape(engine, sliced);
|
||||
items.push(Item::Text(reshaped));
|
||||
items.push(Item::Text(reshaped), idx);
|
||||
} else {
|
||||
// When the item is fully contained, just keep it.
|
||||
items.push(item);
|
||||
items.push(item, idx);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -499,16 +499,16 @@ pub fn commit(
|
||||
|
||||
// Build the frames and determine the height and baseline.
|
||||
let mut frames = vec![];
|
||||
for item in line.items.iter() {
|
||||
let mut push = |offset: &mut Abs, frame: Frame| {
|
||||
for &(idx, ref item) in line.items.indexed_iter() {
|
||||
let mut push = |offset: &mut Abs, frame: Frame, idx: usize| {
|
||||
let width = frame.width();
|
||||
top.set_max(frame.baseline());
|
||||
bottom.set_max(frame.size().y - frame.baseline());
|
||||
frames.push((*offset, frame));
|
||||
frames.push((*offset, frame, idx));
|
||||
*offset += width;
|
||||
};
|
||||
|
||||
match item {
|
||||
match &**item {
|
||||
Item::Absolute(v, _) => {
|
||||
offset += *v;
|
||||
}
|
||||
@ -520,7 +520,7 @@ pub fn commit(
|
||||
layout_box(elem, engine, loc.relayout(), styles, region)
|
||||
})?;
|
||||
apply_baseline_shift(&mut frame, *styles);
|
||||
push(&mut offset, frame);
|
||||
push(&mut offset, frame, idx);
|
||||
} else {
|
||||
offset += amount;
|
||||
}
|
||||
@ -532,15 +532,15 @@ pub fn commit(
|
||||
justification_ratio,
|
||||
extra_justification,
|
||||
);
|
||||
push(&mut offset, frame);
|
||||
push(&mut offset, frame, idx);
|
||||
}
|
||||
Item::Frame(frame) => {
|
||||
push(&mut offset, frame.clone());
|
||||
push(&mut offset, frame.clone(), idx);
|
||||
}
|
||||
Item::Tag(tag) => {
|
||||
let mut frame = Frame::soft(Size::zero());
|
||||
frame.push(Point::zero(), FrameItem::Tag((*tag).clone()));
|
||||
frames.push((offset, frame));
|
||||
frames.push((offset, frame, idx));
|
||||
}
|
||||
Item::Skip(_) => {}
|
||||
}
|
||||
@ -559,8 +559,13 @@ pub fn commit(
|
||||
add_par_line_marker(&mut output, marker, engine, locator, top);
|
||||
}
|
||||
|
||||
// Ensure that the final frame's items are in logical order rather than in
|
||||
// visual order. This is important because it affects the order of elements
|
||||
// during introspection and thus things like counters.
|
||||
frames.sort_unstable_by_key(|(_, _, idx)| *idx);
|
||||
|
||||
// Construct the line's frame.
|
||||
for (offset, frame) in frames {
|
||||
for (offset, frame, _) in frames {
|
||||
let x = offset + p.config.align.position(remaining);
|
||||
let y = top - frame.baseline();
|
||||
output.push_frame(Point::new(x, y), frame);
|
||||
@ -627,7 +632,7 @@ fn overhang(c: char) -> f64 {
|
||||
}
|
||||
|
||||
/// A collection of owned or borrowed inline items.
|
||||
pub struct Items<'a>(Vec<ItemEntry<'a>>);
|
||||
pub struct Items<'a>(Vec<(usize, ItemEntry<'a>)>);
|
||||
|
||||
impl<'a> Items<'a> {
|
||||
/// Create empty items.
|
||||
@ -636,33 +641,38 @@ impl<'a> Items<'a> {
|
||||
}
|
||||
|
||||
/// Push a new item.
|
||||
pub fn push(&mut self, entry: impl Into<ItemEntry<'a>>) {
|
||||
self.0.push(entry.into());
|
||||
pub fn push(&mut self, entry: impl Into<ItemEntry<'a>>, idx: usize) {
|
||||
self.0.push((idx, entry.into()));
|
||||
}
|
||||
|
||||
/// Iterate over the items
|
||||
/// Iterate over the items.
|
||||
pub fn iter(&self) -> impl Iterator<Item = &Item<'a>> {
|
||||
self.0.iter().map(|item| &**item)
|
||||
self.0.iter().map(|(_, item)| &**item)
|
||||
}
|
||||
|
||||
/// Iterate over the items with indices
|
||||
pub fn indexed_iter(&self) -> impl Iterator<Item = &(usize, ItemEntry<'a>)> {
|
||||
self.0.iter()
|
||||
}
|
||||
|
||||
/// Access the first item.
|
||||
pub fn first(&self) -> Option<&Item<'a>> {
|
||||
self.0.first().map(|item| &**item)
|
||||
self.0.first().map(|(_, item)| &**item)
|
||||
}
|
||||
|
||||
/// Access the last item.
|
||||
pub fn last(&self) -> Option<&Item<'a>> {
|
||||
self.0.last().map(|item| &**item)
|
||||
self.0.last().map(|(_, item)| &**item)
|
||||
}
|
||||
|
||||
/// Access the first item mutably, if it is text.
|
||||
pub fn first_text_mut(&mut self) -> Option<&mut ShapedText<'a>> {
|
||||
self.0.first_mut()?.text_mut()
|
||||
self.0.first_mut()?.1.text_mut()
|
||||
}
|
||||
|
||||
/// Access the last item mutably, if it is text.
|
||||
pub fn last_text_mut(&mut self) -> Option<&mut ShapedText<'a>> {
|
||||
self.0.last_mut()?.text_mut()
|
||||
self.0.last_mut()?.1.text_mut()
|
||||
}
|
||||
|
||||
/// Reorder the items starting at the given index to RTL.
|
||||
@ -673,12 +683,12 @@ impl<'a> Items<'a> {
|
||||
|
||||
impl<'a> FromIterator<ItemEntry<'a>> for Items<'a> {
|
||||
fn from_iter<I: IntoIterator<Item = ItemEntry<'a>>>(iter: I) -> Self {
|
||||
Self(iter.into_iter().collect())
|
||||
Self(iter.into_iter().enumerate().collect())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Deref for Items<'a> {
|
||||
type Target = Vec<ItemEntry<'a>>;
|
||||
type Target = Vec<(usize, ItemEntry<'a>)>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
@ -698,6 +708,10 @@ impl Debug for Items<'_> {
|
||||
}
|
||||
|
||||
/// A reference to or a boxed item.
|
||||
///
|
||||
/// This is conceptually similar to a [`Cow<'a, Item<'a>>`][std::borrow::Cow],
|
||||
/// but we box owned items since an [`Item`] is much bigger than
|
||||
/// a box.
|
||||
pub enum ItemEntry<'a> {
|
||||
Ref(&'a Item<'a>),
|
||||
Box(Box<Item<'a>>),
|
||||
|
@ -13,6 +13,7 @@ use typst_library::layout::{
|
||||
VAlignment,
|
||||
};
|
||||
use typst_library::model::Numbering;
|
||||
use typst_library::pdf::ArtifactKind;
|
||||
use typst_library::routines::{Pair, Routines};
|
||||
use typst_library::text::{LocalName, TextElem};
|
||||
use typst_library::visualize::Paint;
|
||||
@ -200,6 +201,11 @@ fn layout_page_run_impl(
|
||||
|
||||
// Layout marginals.
|
||||
let mut layouted = Vec::with_capacity(fragment.len());
|
||||
|
||||
let header = header.as_ref().map(|h| h.clone().artifact(ArtifactKind::Header));
|
||||
let footer = footer.as_ref().map(|f| f.clone().artifact(ArtifactKind::Footer));
|
||||
let background = background.as_ref().map(|b| b.clone().artifact(ArtifactKind::Page));
|
||||
|
||||
for inner in fragment {
|
||||
let header_size = Size::new(inner.width(), margin.top - header_ascent);
|
||||
let footer_size = Size::new(inner.width(), margin.bottom - footer_descent);
|
||||
@ -210,9 +216,9 @@ fn layout_page_run_impl(
|
||||
fill: fill.clone(),
|
||||
numbering: numbering.clone(),
|
||||
supplement: supplement.clone(),
|
||||
header: layout_marginal(header, header_size, Alignment::BOTTOM)?,
|
||||
footer: layout_marginal(footer, footer_size, Alignment::TOP)?,
|
||||
background: layout_marginal(background, full_size, mid)?,
|
||||
header: layout_marginal(&header, header_size, Alignment::BOTTOM)?,
|
||||
footer: layout_marginal(&footer, footer_size, Alignment::TOP)?,
|
||||
background: layout_marginal(&background, full_size, mid)?,
|
||||
foreground: layout_marginal(foreground, full_size, mid)?,
|
||||
margin,
|
||||
binding,
|
||||
|
@ -16,12 +16,13 @@ use crate::diag::{SourceResult, StrResult};
|
||||
use crate::engine::Engine;
|
||||
use crate::foundations::{
|
||||
elem, func, scope, ty, Context, Dict, Element, Fields, IntoValue, Label,
|
||||
NativeElement, Recipe, RecipeIndex, Repr, Selector, Str, Style, StyleChain, Styles,
|
||||
Value,
|
||||
NativeElement, Recipe, RecipeIndex, Repr, Selector, Show, Str, Style, StyleChain,
|
||||
Styles, Value,
|
||||
};
|
||||
use crate::introspection::Location;
|
||||
use crate::introspection::{Locatable, Location};
|
||||
use crate::layout::{AlignElem, Alignment, Axes, Length, MoveElem, PadElem, Rel, Sides};
|
||||
use crate::model::{Destination, EmphElem, LinkElem, StrongElem};
|
||||
use crate::pdf::{ArtifactElem, ArtifactKind};
|
||||
use crate::text::UnderlineElem;
|
||||
|
||||
/// A piece of document content.
|
||||
@ -503,8 +504,12 @@ impl Content {
|
||||
}
|
||||
|
||||
/// Link the content somewhere.
|
||||
pub fn linked(self, dest: Destination) -> Self {
|
||||
self.styled(LinkElem::set_current(Some(dest)))
|
||||
pub fn linked(self, dest: Destination, alt: Option<EcoString>) -> Self {
|
||||
let span = self.span();
|
||||
LinkMarker::new(self, dest.clone(), alt)
|
||||
.pack()
|
||||
.spanned(span)
|
||||
.styled(LinkElem::set_current(Some(dest)))
|
||||
}
|
||||
|
||||
/// Set alignments for this content.
|
||||
@ -533,6 +538,12 @@ impl Content {
|
||||
.pack()
|
||||
.spanned(span)
|
||||
}
|
||||
|
||||
/// Link the content somewhere.
|
||||
pub fn artifact(self, kind: ArtifactKind) -> Self {
|
||||
let span = self.span();
|
||||
ArtifactElem::new(self).with_kind(kind).pack().spanned(span)
|
||||
}
|
||||
}
|
||||
|
||||
#[scope]
|
||||
@ -980,6 +991,24 @@ pub trait PlainText {
|
||||
fn plain_text(&self, text: &mut EcoString);
|
||||
}
|
||||
|
||||
/// An element that associates the body of a link with the destination.
|
||||
#[elem(Show, Locatable)]
|
||||
pub struct LinkMarker {
|
||||
/// The content.
|
||||
#[required]
|
||||
pub body: Content,
|
||||
#[required]
|
||||
pub dest: Destination,
|
||||
#[required]
|
||||
pub alt: Option<EcoString>,
|
||||
}
|
||||
|
||||
impl Show for Packed<LinkMarker> {
|
||||
fn show(&self, _: &mut Engine, _: StyleChain) -> SourceResult<Content> {
|
||||
Ok(self.body.clone())
|
||||
}
|
||||
}
|
||||
|
||||
/// An error arising when trying to access a field of content.
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
||||
pub enum FieldAccessError {
|
||||
|
@ -1,7 +1,8 @@
|
||||
use ecow::{eco_format, EcoString};
|
||||
use typst_utils::{PicoStr, ResolvedPicoStr};
|
||||
|
||||
use crate::foundations::{func, scope, ty, Repr, Str};
|
||||
use crate::diag::StrResult;
|
||||
use crate::foundations::{bail, func, scope, ty, Repr, Str};
|
||||
|
||||
/// A label for an element.
|
||||
///
|
||||
@ -27,7 +28,8 @@ use crate::foundations::{func, scope, ty, Repr, Str};
|
||||
/// # Syntax
|
||||
/// This function also has dedicated syntax: You can create a label by enclosing
|
||||
/// its name in angle brackets. This works both in markup and code. A label's
|
||||
/// name can contain letters, numbers, `_`, `-`, `:`, and `.`.
|
||||
/// name can contain letters, numbers, `_`, `-`, `:`, and `.`. A label cannot
|
||||
/// be empty.
|
||||
///
|
||||
/// Note that there is a syntactical difference when using the dedicated syntax
|
||||
/// for this function. In the code below, the `[<a>]` terminates the heading and
|
||||
@ -50,8 +52,11 @@ pub struct Label(PicoStr);
|
||||
|
||||
impl Label {
|
||||
/// Creates a label from an interned string.
|
||||
pub fn new(name: PicoStr) -> Self {
|
||||
Self(name)
|
||||
///
|
||||
/// Returns `None` if the given string is empty.
|
||||
pub fn new(name: PicoStr) -> Option<Self> {
|
||||
const EMPTY: PicoStr = PicoStr::constant("");
|
||||
(name != EMPTY).then_some(Self(name))
|
||||
}
|
||||
|
||||
/// Resolves the label to a string.
|
||||
@ -70,10 +75,14 @@ impl Label {
|
||||
/// Creates a label from a string.
|
||||
#[func(constructor)]
|
||||
pub fn construct(
|
||||
/// The name of the label.
|
||||
/// The name of the label. Must not be empty.
|
||||
name: Str,
|
||||
) -> Label {
|
||||
Self(PicoStr::intern(name.as_str()))
|
||||
) -> StrResult<Label> {
|
||||
if name.is_empty() {
|
||||
bail!("label name must not be empty");
|
||||
}
|
||||
|
||||
Ok(Self(PicoStr::intern(name.as_str())))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -19,11 +19,8 @@ use crate::foundations::{repr, ty, Content, Scope, Value};
|
||||
///
|
||||
/// You can access definitions from the module using [field access
|
||||
/// notation]($scripting/#fields) and interact with it using the [import and
|
||||
/// include syntaxes]($scripting/#modules). Alternatively, it is possible to
|
||||
/// convert a module to a dictionary, and therefore access its contents
|
||||
/// dynamically, using the [dictionary constructor]($dictionary/#constructor).
|
||||
/// include syntaxes]($scripting/#modules).
|
||||
///
|
||||
/// # Example
|
||||
/// ```example
|
||||
/// <<< #import "utils.typ"
|
||||
/// <<< #utils.add(2, 5)
|
||||
@ -34,6 +31,20 @@ use crate::foundations::{repr, ty, Content, Scope, Value};
|
||||
/// >>>
|
||||
/// >>> #(-3)
|
||||
/// ```
|
||||
///
|
||||
/// You can check whether a definition is present in a module using the `{in}`
|
||||
/// operator, with a string on the left-hand side. This can be useful to
|
||||
/// [conditionally access]($category/foundations/std/#conditional-access)
|
||||
/// definitions in a module.
|
||||
///
|
||||
/// ```example
|
||||
/// #("table" in std) \
|
||||
/// #("nope" in std)
|
||||
/// ```
|
||||
///
|
||||
/// Alternatively, it is possible to convert a module to a dictionary, and
|
||||
/// therefore access its contents dynamically, using the [dictionary
|
||||
/// constructor]($dictionary/#constructor).
|
||||
#[ty(cast)]
|
||||
#[derive(Clone, Hash)]
|
||||
#[allow(clippy::derived_hash_with_manual_eq)]
|
||||
|
@ -558,6 +558,7 @@ pub fn contains(lhs: &Value, rhs: &Value) -> Option<bool> {
|
||||
(Str(a), Str(b)) => Some(b.as_str().contains(a.as_str())),
|
||||
(Dyn(a), Str(b)) => a.downcast::<Regex>().map(|regex| regex.is_match(b)),
|
||||
(Str(a), Dict(b)) => Some(b.contains(a)),
|
||||
(Str(a), Module(b)) => Some(b.scope().get(a).is_some()),
|
||||
(a, Array(b)) => Some(b.contains(a.clone())),
|
||||
|
||||
_ => Option::None,
|
||||
|
@ -8,7 +8,7 @@ use serde::{Serialize, Serializer};
|
||||
use typst_syntax::{is_ident, Span, Spanned};
|
||||
use typst_utils::hash128;
|
||||
|
||||
use crate::diag::{bail, SourceResult, StrResult};
|
||||
use crate::diag::{bail, DeprecationSink, SourceResult, StrResult};
|
||||
use crate::foundations::{
|
||||
cast, elem, func, scope, ty, Array, Content, Func, NativeElement, NativeFunc, Packed,
|
||||
PlainText, Repr as _,
|
||||
@ -54,18 +54,22 @@ enum Repr {
|
||||
/// A native symbol that has no named variant.
|
||||
Single(char),
|
||||
/// A native symbol with multiple named variants.
|
||||
Complex(&'static [(ModifierSet<&'static str>, char)]),
|
||||
Complex(&'static [Variant<&'static str>]),
|
||||
/// A symbol with multiple named variants, where some modifiers may have
|
||||
/// been applied. Also used for symbols defined at runtime by the user with
|
||||
/// no modifier applied.
|
||||
Modified(Arc<(List, ModifierSet<EcoString>)>),
|
||||
}
|
||||
|
||||
/// A symbol variant, consisting of a set of modifiers, a character, and an
|
||||
/// optional deprecation message.
|
||||
type Variant<S> = (ModifierSet<S>, char, Option<S>);
|
||||
|
||||
/// A collection of symbols.
|
||||
#[derive(Clone, Eq, PartialEq, Hash)]
|
||||
enum List {
|
||||
Static(&'static [(ModifierSet<&'static str>, char)]),
|
||||
Runtime(Box<[(ModifierSet<EcoString>, char)]>),
|
||||
Static(&'static [Variant<&'static str>]),
|
||||
Runtime(Box<[Variant<EcoString>]>),
|
||||
}
|
||||
|
||||
impl Symbol {
|
||||
@ -76,14 +80,14 @@ impl Symbol {
|
||||
|
||||
/// Create a symbol with a static variant list.
|
||||
#[track_caller]
|
||||
pub const fn list(list: &'static [(ModifierSet<&'static str>, char)]) -> Self {
|
||||
pub const fn list(list: &'static [Variant<&'static str>]) -> Self {
|
||||
debug_assert!(!list.is_empty());
|
||||
Self(Repr::Complex(list))
|
||||
}
|
||||
|
||||
/// Create a symbol with a runtime variant list.
|
||||
#[track_caller]
|
||||
pub fn runtime(list: Box<[(ModifierSet<EcoString>, char)]>) -> Self {
|
||||
pub fn runtime(list: Box<[Variant<EcoString>]>) -> Self {
|
||||
debug_assert!(!list.is_empty());
|
||||
Self(Repr::Modified(Arc::new((List::Runtime(list), ModifierSet::default()))))
|
||||
}
|
||||
@ -93,9 +97,11 @@ impl Symbol {
|
||||
match &self.0 {
|
||||
Repr::Single(c) => *c,
|
||||
Repr::Complex(_) => ModifierSet::<&'static str>::default()
|
||||
.best_match_in(self.variants())
|
||||
.best_match_in(self.variants().map(|(m, c, _)| (m, c)))
|
||||
.unwrap(),
|
||||
Repr::Modified(arc) => arc.1.best_match_in(self.variants()).unwrap(),
|
||||
Repr::Modified(arc) => {
|
||||
arc.1.best_match_in(self.variants().map(|(m, c, _)| (m, c))).unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -128,7 +134,11 @@ impl Symbol {
|
||||
}
|
||||
|
||||
/// Apply a modifier to the symbol.
|
||||
pub fn modified(mut self, modifier: &str) -> StrResult<Self> {
|
||||
pub fn modified(
|
||||
mut self,
|
||||
sink: impl DeprecationSink,
|
||||
modifier: &str,
|
||||
) -> StrResult<Self> {
|
||||
if let Repr::Complex(list) = self.0 {
|
||||
self.0 =
|
||||
Repr::Modified(Arc::new((List::Static(list), ModifierSet::default())));
|
||||
@ -137,7 +147,12 @@ impl Symbol {
|
||||
if let Repr::Modified(arc) = &mut self.0 {
|
||||
let (list, modifiers) = Arc::make_mut(arc);
|
||||
modifiers.insert_raw(modifier);
|
||||
if modifiers.best_match_in(list.variants()).is_some() {
|
||||
if let Some(deprecation) =
|
||||
modifiers.best_match_in(list.variants().map(|(m, _, d)| (m, d)))
|
||||
{
|
||||
if let Some(message) = deprecation {
|
||||
sink.emit(message)
|
||||
}
|
||||
return Ok(self);
|
||||
}
|
||||
}
|
||||
@ -146,7 +161,7 @@ impl Symbol {
|
||||
}
|
||||
|
||||
/// The characters that are covered by this symbol.
|
||||
pub fn variants(&self) -> impl Iterator<Item = (ModifierSet<&str>, char)> {
|
||||
pub fn variants(&self) -> impl Iterator<Item = Variant<&str>> {
|
||||
match &self.0 {
|
||||
Repr::Single(c) => Variants::Single(Some(*c).into_iter()),
|
||||
Repr::Complex(list) => Variants::Static(list.iter()),
|
||||
@ -161,7 +176,7 @@ impl Symbol {
|
||||
_ => ModifierSet::default(),
|
||||
};
|
||||
self.variants()
|
||||
.flat_map(|(m, _)| m)
|
||||
.flat_map(|(m, _, _)| m)
|
||||
.filter(|modifier| !modifier.is_empty() && !modifiers.contains(modifier))
|
||||
.collect::<BTreeSet<_>>()
|
||||
.into_iter()
|
||||
@ -256,7 +271,7 @@ impl Symbol {
|
||||
|
||||
let list = variants
|
||||
.into_iter()
|
||||
.map(|s| (ModifierSet::from_raw_dotted(s.v.0), s.v.1))
|
||||
.map(|s| (ModifierSet::from_raw_dotted(s.v.0), s.v.1, None))
|
||||
.collect();
|
||||
Ok(Symbol::runtime(list))
|
||||
}
|
||||
@ -316,17 +331,17 @@ impl crate::foundations::Repr for Symbol {
|
||||
}
|
||||
|
||||
fn repr_variants<'a>(
|
||||
variants: impl Iterator<Item = (ModifierSet<&'a str>, char)>,
|
||||
variants: impl Iterator<Item = Variant<&'a str>>,
|
||||
applied_modifiers: ModifierSet<&str>,
|
||||
) -> String {
|
||||
crate::foundations::repr::pretty_array_like(
|
||||
&variants
|
||||
.filter(|(modifiers, _)| {
|
||||
.filter(|(modifiers, _, _)| {
|
||||
// Only keep variants that can still be accessed, i.e., variants
|
||||
// that contain all applied modifiers.
|
||||
applied_modifiers.iter().all(|am| modifiers.contains(am))
|
||||
})
|
||||
.map(|(modifiers, c)| {
|
||||
.map(|(modifiers, c, _)| {
|
||||
let trimmed_modifiers =
|
||||
modifiers.into_iter().filter(|&m| !applied_modifiers.contains(m));
|
||||
if trimmed_modifiers.clone().all(|m| m.is_empty()) {
|
||||
@ -379,18 +394,20 @@ cast! {
|
||||
/// Iterator over variants.
|
||||
enum Variants<'a> {
|
||||
Single(std::option::IntoIter<char>),
|
||||
Static(std::slice::Iter<'static, (ModifierSet<&'static str>, char)>),
|
||||
Runtime(std::slice::Iter<'a, (ModifierSet<EcoString>, char)>),
|
||||
Static(std::slice::Iter<'static, Variant<&'static str>>),
|
||||
Runtime(std::slice::Iter<'a, Variant<EcoString>>),
|
||||
}
|
||||
|
||||
impl<'a> Iterator for Variants<'a> {
|
||||
type Item = (ModifierSet<&'a str>, char);
|
||||
type Item = Variant<&'a str>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
match self {
|
||||
Self::Single(iter) => Some((ModifierSet::default(), iter.next()?)),
|
||||
Self::Single(iter) => Some((ModifierSet::default(), iter.next()?, None)),
|
||||
Self::Static(list) => list.next().copied(),
|
||||
Self::Runtime(list) => list.next().map(|(m, c)| (m.as_deref(), *c)),
|
||||
Self::Runtime(list) => {
|
||||
list.next().map(|(m, c, d)| (m.as_deref(), *c, d.as_deref()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -157,7 +157,9 @@ impl Value {
|
||||
/// Try to access a field on the value.
|
||||
pub fn field(&self, field: &str, sink: impl DeprecationSink) -> StrResult<Value> {
|
||||
match self {
|
||||
Self::Symbol(symbol) => symbol.clone().modified(field).map(Self::Symbol),
|
||||
Self::Symbol(symbol) => {
|
||||
symbol.clone().modified(sink, field).map(Self::Symbol)
|
||||
}
|
||||
Self::Version(version) => version.component(field).map(Self::Int),
|
||||
Self::Dict(dict) => dict.get(field).cloned(),
|
||||
Self::Content(content) => content.field_by_name(field),
|
||||
|
@ -7,7 +7,7 @@ use typst_utils::{PicoStr, ResolvedPicoStr};
|
||||
use crate::diag::{bail, HintedStrResult, StrResult};
|
||||
use crate::foundations::{cast, Dict, Repr, Str};
|
||||
use crate::introspection::{Introspector, Tag};
|
||||
use crate::layout::Frame;
|
||||
use crate::layout::{Abs, Frame};
|
||||
use crate::model::DocumentInfo;
|
||||
|
||||
/// An HTML document.
|
||||
@ -30,8 +30,8 @@ pub enum HtmlNode {
|
||||
Text(EcoString, Span),
|
||||
/// Another element.
|
||||
Element(HtmlElement),
|
||||
/// A frame that will be displayed as an embedded SVG.
|
||||
Frame(Frame),
|
||||
/// Layouted content that will be embedded into HTML as an SVG.
|
||||
Frame(HtmlFrame),
|
||||
}
|
||||
|
||||
impl HtmlNode {
|
||||
@ -263,6 +263,17 @@ cast! {
|
||||
v: Str => Self::intern(&v)?,
|
||||
}
|
||||
|
||||
/// Layouted content that will be embedded into HTML as an SVG.
|
||||
#[derive(Debug, Clone, Hash)]
|
||||
pub struct HtmlFrame {
|
||||
/// The frame that will be displayed as an SVG.
|
||||
pub inner: Frame,
|
||||
/// The text size where the frame was defined. This is used to size the
|
||||
/// frame with em units to make text in and outside of the frame sized
|
||||
/// consistently.
|
||||
pub text_size: Abs,
|
||||
}
|
||||
|
||||
/// Defines syntactical properties of HTML tags, attributes, and text.
|
||||
pub mod charsets {
|
||||
/// Check whether a character is in a tag name.
|
||||
|
@ -8,6 +8,7 @@ pub use self::dom::*;
|
||||
use ecow::EcoString;
|
||||
|
||||
use crate::foundations::{elem, Content, Module, Scope};
|
||||
use crate::introspection::Locatable;
|
||||
|
||||
/// Create a module with all HTML definitions.
|
||||
pub fn module() -> Module {
|
||||
@ -40,7 +41,7 @@ pub fn module() -> Module {
|
||||
/// A div with _Typst content_ inside!
|
||||
/// ]
|
||||
/// ```
|
||||
#[elem(name = "elem")]
|
||||
#[elem(name = "elem", Locatable)]
|
||||
pub struct HtmlElem {
|
||||
/// The element's tag.
|
||||
#[required]
|
||||
|
@ -446,7 +446,7 @@ impl IntrospectorBuilder {
|
||||
HtmlNode::Element(elem) => self.discover_in_html(sink, &elem.children),
|
||||
HtmlNode::Frame(frame) => self.discover_in_frame(
|
||||
sink,
|
||||
frame,
|
||||
&frame.inner,
|
||||
NonZeroUsize::ONE,
|
||||
Transform::identity(),
|
||||
),
|
||||
|
@ -13,6 +13,7 @@ use crate::foundations::{
|
||||
cast, elem, scope, Array, CastInfo, Content, Context, Fold, FromValue, Func,
|
||||
IntoValue, NativeElement, Packed, Reflect, Resolve, Show, Smart, StyleChain, Value,
|
||||
};
|
||||
use crate::introspection::Locatable;
|
||||
use crate::layout::{
|
||||
Alignment, BlockElem, Length, OuterHAlignment, OuterVAlignment, Rel, Sides, Sizing,
|
||||
};
|
||||
@ -136,7 +137,7 @@ use crate::visualize::{Paint, Stroke};
|
||||
///
|
||||
/// Furthermore, strokes of a repeated grid header or footer will take
|
||||
/// precedence over regular cell strokes.
|
||||
#[elem(scope, Show)]
|
||||
#[elem(scope, Locatable, Show)]
|
||||
pub struct GridElem {
|
||||
/// The column sizes.
|
||||
///
|
||||
@ -462,7 +463,7 @@ impl TryFrom<Content> for GridItem {
|
||||
/// If `repeat` is set to `true`, the header will be repeated across pages. For
|
||||
/// an example, refer to the [`table.header`]($table.header) element and the
|
||||
/// [`grid.stroke`]($grid.stroke) parameter.
|
||||
#[elem(name = "header", title = "Grid Header")]
|
||||
#[elem(name = "header", title = "Grid Header", Locatable)]
|
||||
pub struct GridHeader {
|
||||
/// Whether this header should be repeated across pages.
|
||||
#[default(true)]
|
||||
@ -490,7 +491,7 @@ pub struct GridHeader {
|
||||
/// itself on every page of the table.
|
||||
///
|
||||
/// No other grid cells may be placed after the footer.
|
||||
#[elem(name = "footer", title = "Grid Footer")]
|
||||
#[elem(name = "footer", title = "Grid Footer", Locatable)]
|
||||
pub struct GridFooter {
|
||||
/// Whether this footer should be repeated across pages.
|
||||
#[default(true)]
|
||||
@ -657,7 +658,7 @@ pub struct GridVLine {
|
||||
/// which allows you, for example, to apply styles based on a cell's position.
|
||||
/// Refer to the examples of the [`table.cell`]($table.cell) element to learn
|
||||
/// more about this.
|
||||
#[elem(name = "cell", title = "Grid Cell", Show)]
|
||||
#[elem(name = "cell", title = "Grid Cell", Locatable, Show)]
|
||||
pub struct GridCell {
|
||||
/// The cell's body.
|
||||
#[required]
|
||||
|
@ -22,6 +22,7 @@ use typst_syntax::Span;
|
||||
use typst_utils::NonZeroExt;
|
||||
|
||||
use crate::introspection::SplitLocator;
|
||||
use crate::model::TableCellKind;
|
||||
|
||||
/// Convert a grid to a cell grid.
|
||||
#[typst_macros::time(span = elem.span())]
|
||||
@ -217,6 +218,7 @@ impl ResolvableCell for Packed<TableCell> {
|
||||
breakable: bool,
|
||||
locator: Locator<'a>,
|
||||
styles: StyleChain,
|
||||
kind: Smart<TableCellKind>,
|
||||
) -> Cell<'a> {
|
||||
let cell = &mut *self;
|
||||
let colspan = cell.colspan(styles);
|
||||
@ -224,6 +226,8 @@ impl ResolvableCell for Packed<TableCell> {
|
||||
let breakable = cell.breakable(styles).unwrap_or(breakable);
|
||||
let fill = cell.fill(styles).unwrap_or_else(|| fill.clone());
|
||||
|
||||
let kind = cell.kind(styles).or(kind);
|
||||
|
||||
let cell_stroke = cell.stroke(styles);
|
||||
let stroke_overridden =
|
||||
cell_stroke.as_ref().map(|side| matches!(side, Some(Some(_))));
|
||||
@ -267,6 +271,7 @@ impl ResolvableCell for Packed<TableCell> {
|
||||
}),
|
||||
);
|
||||
cell.push_breakable(Smart::Custom(breakable));
|
||||
cell.push_kind(kind);
|
||||
Cell {
|
||||
body: self.pack(),
|
||||
locator,
|
||||
@ -312,6 +317,7 @@ impl ResolvableCell for Packed<GridCell> {
|
||||
breakable: bool,
|
||||
locator: Locator<'a>,
|
||||
styles: StyleChain,
|
||||
_: Smart<TableCellKind>,
|
||||
) -> Cell<'a> {
|
||||
let cell = &mut *self;
|
||||
let colspan = cell.colspan(styles);
|
||||
@ -522,6 +528,7 @@ pub trait ResolvableCell {
|
||||
breakable: bool,
|
||||
locator: Locator<'a>,
|
||||
styles: StyleChain,
|
||||
kind: Smart<TableCellKind>,
|
||||
) -> Cell<'a>;
|
||||
|
||||
/// Returns this cell's column override.
|
||||
@ -1206,8 +1213,12 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
// a non-empty row.
|
||||
let mut first_available_row = 0;
|
||||
|
||||
let mut cell_kind: Smart<TableCellKind> = Smart::Auto;
|
||||
|
||||
let (header_footer_items, simple_item) = match child {
|
||||
ResolvableGridChild::Header { repeat, level, span, items, .. } => {
|
||||
cell_kind = Smart::Custom(TableCellKind::Header);
|
||||
|
||||
row_group_data = Some(RowGroupData {
|
||||
range: None,
|
||||
span,
|
||||
@ -1239,6 +1250,8 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
bail!(span, "cannot have more than one footer");
|
||||
}
|
||||
|
||||
cell_kind = Smart::Custom(TableCellKind::Footer);
|
||||
|
||||
row_group_data = Some(RowGroupData {
|
||||
range: None,
|
||||
span,
|
||||
@ -1447,7 +1460,7 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
|
||||
// Let's resolve the cell so it can determine its own fields
|
||||
// based on its final position.
|
||||
let cell = self.resolve_cell(cell, x, y, rowspan, cell_span)?;
|
||||
let cell = self.resolve_cell(cell, x, y, rowspan, cell_span, cell_kind)?;
|
||||
|
||||
if largest_index >= resolved_cells.len() {
|
||||
// Ensure the length of the vector of resolved cells is
|
||||
@ -1542,6 +1555,10 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
// and footers without having to loop through them each time.
|
||||
// Cells themselves, unfortunately, still have to.
|
||||
assert!(resolved_cells[*local_auto_index].is_none());
|
||||
let kind = match row_group.kind {
|
||||
RowGroupKind::Header => TableCellKind::Header,
|
||||
RowGroupKind::Footer => TableCellKind::Header,
|
||||
};
|
||||
resolved_cells[*local_auto_index] =
|
||||
Some(Entry::Cell(self.resolve_cell(
|
||||
T::default(),
|
||||
@ -1549,6 +1566,7 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
first_available_row,
|
||||
1,
|
||||
Span::detached(),
|
||||
Smart::Custom(kind),
|
||||
)?));
|
||||
|
||||
group_start..group_end
|
||||
@ -1673,6 +1691,7 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
y,
|
||||
1,
|
||||
Span::detached(),
|
||||
Smart::Auto,
|
||||
)?))
|
||||
}
|
||||
})
|
||||
@ -1918,6 +1937,7 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
y: usize,
|
||||
rowspan: usize,
|
||||
cell_span: Span,
|
||||
kind: Smart<TableCellKind>,
|
||||
) -> SourceResult<Cell<'x>>
|
||||
where
|
||||
T: ResolvableCell + Default,
|
||||
@ -1954,6 +1974,7 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
breakable,
|
||||
self.locator.next(&cell_span),
|
||||
self.styles,
|
||||
kind,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
use crate::diag::SourceResult;
|
||||
use crate::engine::Engine;
|
||||
use crate::foundations::{elem, Content, Packed, Show, StyleChain};
|
||||
use crate::introspection::Locatable;
|
||||
|
||||
/// Hides content without affecting layout.
|
||||
///
|
||||
@ -14,7 +15,7 @@ use crate::foundations::{elem, Content, Packed, Show, StyleChain};
|
||||
/// Hello Jane \
|
||||
/// #hide[Hello] Joe
|
||||
/// ```
|
||||
#[elem(Show)]
|
||||
#[elem(Locatable, Show)]
|
||||
pub struct HideElem {
|
||||
/// The content to hide.
|
||||
#[required]
|
||||
|
@ -1,6 +1,7 @@
|
||||
use crate::diag::SourceResult;
|
||||
use crate::engine::Engine;
|
||||
use crate::foundations::{elem, Content, NativeElement, Packed, Show, StyleChain};
|
||||
use crate::introspection::Locatable;
|
||||
use crate::layout::{BlockElem, Length};
|
||||
|
||||
/// Repeats content to the available space.
|
||||
@ -24,7 +25,7 @@ use crate::layout::{BlockElem, Length};
|
||||
/// Berlin, the 22nd of December, 2022
|
||||
/// ]
|
||||
/// ```
|
||||
#[elem(Show)]
|
||||
#[elem(Locatable, Show)]
|
||||
pub struct RepeatElem {
|
||||
/// The content to repeat.
|
||||
#[required]
|
||||
|
@ -28,6 +28,7 @@ use typst_utils::singleton;
|
||||
use unicode_math_class::MathClass;
|
||||
|
||||
use crate::foundations::{elem, Content, Module, NativeElement, Scope};
|
||||
use crate::introspection::Locatable;
|
||||
use crate::layout::{Em, HElem};
|
||||
use crate::text::TextElem;
|
||||
|
||||
@ -109,7 +110,7 @@ pub fn module() -> Module {
|
||||
pub trait Mathy {}
|
||||
|
||||
/// A math alignment point: `&`, `&&`.
|
||||
#[elem(title = "Alignment Point", Mathy)]
|
||||
#[elem(title = "Alignment Point", Mathy, Locatable)]
|
||||
pub struct AlignPointElem {}
|
||||
|
||||
impl AlignPointElem {
|
||||
@ -136,7 +137,7 @@ impl AlignPointElem {
|
||||
///
|
||||
/// $x loves y and y loves 5$
|
||||
/// ```
|
||||
#[elem(Mathy)]
|
||||
#[elem(Mathy, Locatable)]
|
||||
pub struct ClassElem {
|
||||
/// The class to apply to the content.
|
||||
#[required]
|
||||
|
@ -1,6 +1,7 @@
|
||||
use typst_syntax::Span;
|
||||
|
||||
use crate::foundations::{elem, func, Content, NativeElement};
|
||||
use crate::introspection::Locatable;
|
||||
use crate::math::Mathy;
|
||||
|
||||
/// A square root.
|
||||
@ -22,7 +23,7 @@ pub fn sqrt(
|
||||
/// ```example
|
||||
/// $ root(3, x) $
|
||||
/// ```
|
||||
#[elem(Mathy)]
|
||||
#[elem(Mathy, Locatable)]
|
||||
pub struct RootElem {
|
||||
/// Which root of the radicand to take.
|
||||
#[positional]
|
||||
|
@ -321,7 +321,11 @@ impl Bibliography {
|
||||
for d in data.iter() {
|
||||
let library = decode_library(d)?;
|
||||
for entry in library {
|
||||
match map.entry(Label::new(PicoStr::intern(entry.key()))) {
|
||||
let label = Label::new(PicoStr::intern(entry.key()))
|
||||
.ok_or("bibliography contains entry with empty key")
|
||||
.at(d.source.span)?;
|
||||
|
||||
match map.entry(label) {
|
||||
indexmap::map::Entry::Vacant(vacant) => {
|
||||
vacant.insert(entry);
|
||||
}
|
||||
@ -592,7 +596,7 @@ impl Works {
|
||||
|
||||
/// Context for generating the bibliography.
|
||||
struct Generator<'a> {
|
||||
/// The routines that is used to evaluate mathematical material in citations.
|
||||
/// The routines that are used to evaluate mathematical material in citations.
|
||||
routines: &'a Routines,
|
||||
/// The world that is used to evaluate mathematical material in citations.
|
||||
world: Tracked<'a, dyn World + 'a>,
|
||||
@ -609,7 +613,7 @@ struct Generator<'a> {
|
||||
|
||||
/// Details about a group of merged citations. All citations are put into groups
|
||||
/// of adjacent ones (e.g., `@foo @bar` will merge into a group of length two).
|
||||
/// Even single citations will be put into groups of length ones.
|
||||
/// Even single citations will be put into groups of length one.
|
||||
struct GroupInfo {
|
||||
/// The group's location.
|
||||
location: Location,
|
||||
@ -873,7 +877,8 @@ impl<'a> Generator<'a> {
|
||||
renderer.display_elem_child(elem, &mut None, false)?;
|
||||
if let Some(location) = first_occurrences.get(item.key.as_str()) {
|
||||
let dest = Destination::Location(*location);
|
||||
content = content.linked(dest);
|
||||
// TODO: accept user supplied alt text
|
||||
content = content.linked(dest, None);
|
||||
}
|
||||
StrResult::Ok(content)
|
||||
})
|
||||
@ -1008,7 +1013,8 @@ impl ElemRenderer<'_> {
|
||||
if let Some(hayagriva::ElemMeta::Entry(i)) = elem.meta {
|
||||
if let Some(location) = (self.link)(i) {
|
||||
let dest = Destination::Location(location);
|
||||
content = content.linked(dest);
|
||||
// TODO: accept user supplied alt text
|
||||
content = content.linked(dest, None);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -43,7 +43,7 @@ use crate::text::{Lang, Region, TextElem};
|
||||
/// This function indirectly has dedicated syntax. [References]($ref) can be
|
||||
/// used to cite works from the bibliography. The label then corresponds to the
|
||||
/// citation key.
|
||||
#[elem(Synthesize)]
|
||||
#[elem(Locatable, Synthesize)]
|
||||
pub struct CiteElem {
|
||||
/// The citation key that identifies the entry in the bibliography that
|
||||
/// shall be cited, as a label.
|
||||
|
@ -4,6 +4,7 @@ use crate::foundations::{
|
||||
elem, Content, NativeElement, Packed, Show, StyleChain, TargetElem,
|
||||
};
|
||||
use crate::html::{tag, HtmlElem};
|
||||
use crate::introspection::Locatable;
|
||||
use crate::text::{ItalicToggle, TextElem};
|
||||
|
||||
/// Emphasizes content by toggling italics.
|
||||
@ -29,7 +30,7 @@ use crate::text::{ItalicToggle, TextElem};
|
||||
/// This function also has dedicated syntax: To emphasize content, simply
|
||||
/// enclose it in underscores (`_`). Note that this only works at word
|
||||
/// boundaries. To emphasize part of a word, you have to use the function.
|
||||
#[elem(title = "Emphasis", keywords = ["italic"], Show)]
|
||||
#[elem(title = "Emphasis", keywords = ["italic"], Locatable, Show)]
|
||||
pub struct EmphElem {
|
||||
/// The content to emphasize.
|
||||
#[required]
|
||||
|
@ -10,6 +10,7 @@ use crate::foundations::{
|
||||
Styles, TargetElem,
|
||||
};
|
||||
use crate::html::{attr, tag, HtmlElem};
|
||||
use crate::introspection::Locatable;
|
||||
use crate::layout::{Alignment, BlockElem, Em, HAlignment, Length, VAlignment, VElem};
|
||||
use crate::model::{
|
||||
ListItemLike, ListLike, Numbering, NumberingPattern, ParElem, ParbreakElem,
|
||||
@ -71,7 +72,7 @@ use crate::model::{
|
||||
/// Enumeration items can contain multiple paragraphs and other block-level
|
||||
/// content. All content that is indented more than an item's marker becomes
|
||||
/// part of that item.
|
||||
#[elem(scope, title = "Numbered List", Show)]
|
||||
#[elem(scope, title = "Numbered List", Locatable, Show)]
|
||||
pub struct EnumElem {
|
||||
/// Defines the default [spacing]($enum.spacing) of the enumeration. If it
|
||||
/// is `{false}`, the items are spaced apart with
|
||||
@ -271,7 +272,7 @@ impl Show for Packed<EnumElem> {
|
||||
}
|
||||
|
||||
/// An enumeration item.
|
||||
#[elem(name = "item", title = "Numbered List Item")]
|
||||
#[elem(name = "item", title = "Numbered List Item", Locatable)]
|
||||
pub struct EnumItem {
|
||||
/// The item's number.
|
||||
#[positional]
|
||||
|
@ -473,7 +473,7 @@ impl Outlinable for Packed<FigureElem> {
|
||||
/// caption: [A rectangle],
|
||||
/// )
|
||||
/// ```
|
||||
#[elem(name = "caption", Synthesize, Show)]
|
||||
#[elem(name = "caption", Locatable, Synthesize, Show)]
|
||||
pub struct FigureCaption {
|
||||
/// The caption's position in the figure. Either `{top}` or `{bottom}`.
|
||||
///
|
||||
|
@ -147,7 +147,8 @@ impl Show for Packed<FootnoteElem> {
|
||||
let sup = SuperElem::new(num).pack().spanned(span);
|
||||
let loc = loc.variant(1);
|
||||
// Add zero-width weak spacing to make the footnote "sticky".
|
||||
Ok(HElem::hole().pack() + sup.linked(Destination::Location(loc)))
|
||||
// TODO: accept user supplied alt text
|
||||
Ok(HElem::hole().pack() + sup.linked(Destination::Location(loc), None))
|
||||
}
|
||||
}
|
||||
|
||||
@ -192,7 +193,7 @@ cast! {
|
||||
/// page run is a sequence of pages without an explicit pagebreak in between).
|
||||
/// For this reason, set and show rules for footnote entries should be defined
|
||||
/// before any page content, typically at the very start of the document.
|
||||
#[elem(name = "entry", title = "Footnote Entry", Show, ShowSet)]
|
||||
#[elem(name = "entry", title = "Footnote Entry", Locatable, Show, ShowSet)]
|
||||
pub struct FootnoteEntry {
|
||||
/// The footnote for this entry. Its location can be used to determine
|
||||
/// the footnote counter state.
|
||||
@ -296,7 +297,8 @@ impl Show for Packed<FootnoteEntry> {
|
||||
let sup = SuperElem::new(num)
|
||||
.pack()
|
||||
.spanned(span)
|
||||
.linked(Destination::Location(loc))
|
||||
// TODO: accept user supplied alt text
|
||||
.linked(Destination::Location(loc), None)
|
||||
.located(loc.variant(1));
|
||||
|
||||
Ok(Content::sequence([
|
||||
|
@ -9,7 +9,7 @@ use crate::foundations::{
|
||||
StyleChain, Styles, TargetElem,
|
||||
};
|
||||
use crate::html::{attr, tag, HtmlElem};
|
||||
use crate::introspection::Location;
|
||||
use crate::introspection::{Locatable, Location};
|
||||
use crate::layout::Position;
|
||||
use crate::text::TextElem;
|
||||
|
||||
@ -38,8 +38,11 @@ use crate::text::TextElem;
|
||||
/// # Syntax
|
||||
/// This function also has dedicated syntax: Text that starts with `http://` or
|
||||
/// `https://` is automatically turned into a link.
|
||||
#[elem(Show)]
|
||||
#[elem(Locatable, Show)]
|
||||
pub struct LinkElem {
|
||||
/// A text describing the link.
|
||||
pub alt: Option<EcoString>,
|
||||
|
||||
/// The destination the link points to.
|
||||
///
|
||||
/// - To link to web pages, `dest` should be a valid URL string. If the URL
|
||||
@ -123,12 +126,13 @@ impl Show for Packed<LinkElem> {
|
||||
body
|
||||
}
|
||||
} else {
|
||||
let alt = self.alt(styles);
|
||||
match &self.dest {
|
||||
LinkTarget::Dest(dest) => body.linked(dest.clone()),
|
||||
LinkTarget::Dest(dest) => body.linked(dest.clone(), alt),
|
||||
LinkTarget::Label(label) => {
|
||||
let elem = engine.introspector.query_label(*label).at(self.span())?;
|
||||
let dest = Destination::Location(elem.location().unwrap());
|
||||
body.clone().linked(dest)
|
||||
body.linked(dest, alt)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -7,6 +7,7 @@ use crate::foundations::{
|
||||
Smart, StyleChain, Styles, TargetElem, Value,
|
||||
};
|
||||
use crate::html::{tag, HtmlElem};
|
||||
use crate::introspection::Locatable;
|
||||
use crate::layout::{BlockElem, Em, Length, VElem};
|
||||
use crate::model::{ParElem, ParbreakElem};
|
||||
use crate::text::TextElem;
|
||||
@ -42,7 +43,7 @@ use crate::text::TextElem;
|
||||
/// followed by a space to create a list item. A list item can contain multiple
|
||||
/// paragraphs and other block-level content. All content that is indented
|
||||
/// more than an item's marker becomes part of that item.
|
||||
#[elem(scope, title = "Bullet List", Show)]
|
||||
#[elem(scope, title = "Bullet List", Locatable, Show)]
|
||||
pub struct ListElem {
|
||||
/// Defines the default [spacing]($list.spacing) of the list. If it is
|
||||
/// `{false}`, the items are spaced apart with
|
||||
@ -178,7 +179,7 @@ impl Show for Packed<ListElem> {
|
||||
}
|
||||
|
||||
/// A bullet list item.
|
||||
#[elem(name = "item", title = "Bullet List Item")]
|
||||
#[elem(name = "item", title = "Bullet List Item", Locatable)]
|
||||
pub struct ListItem {
|
||||
/// The item's body.
|
||||
#[required]
|
||||
|
@ -2,6 +2,7 @@ use std::num::NonZeroUsize;
|
||||
use std::str::FromStr;
|
||||
|
||||
use comemo::{Track, Tracked};
|
||||
use ecow::eco_format;
|
||||
use smallvec::SmallVec;
|
||||
use typst_syntax::Span;
|
||||
use typst_utils::{Get, NonZeroExt};
|
||||
@ -17,8 +18,8 @@ use crate::introspection::{
|
||||
Counter, CounterKey, Introspector, Locatable, Location, Locator, LocatorLink,
|
||||
};
|
||||
use crate::layout::{
|
||||
Abs, Axes, BlockBody, BlockElem, BoxElem, Dir, Em, Fr, HElem, Length, Region, Rel,
|
||||
RepeatElem, Sides,
|
||||
Abs, Axes, BlockBody, BlockElem, BoxElem, Dir, Em, Fr, HElem, Length, PageElem,
|
||||
Region, Rel, RepeatElem, Sides,
|
||||
};
|
||||
use crate::math::EquationElem;
|
||||
use crate::model::{Destination, HeadingElem, NumberingPattern, ParElem, Refable};
|
||||
@ -272,6 +273,7 @@ impl Show for Packed<OutlineElem> {
|
||||
let depth = self.depth(styles).unwrap_or(NonZeroUsize::MAX);
|
||||
|
||||
// Build the outline entries.
|
||||
let mut entries = vec![];
|
||||
for elem in elems {
|
||||
let Some(outlinable) = elem.with::<dyn Outlinable>() else {
|
||||
bail!(span, "cannot outline {}", elem.func().name());
|
||||
@ -280,10 +282,13 @@ impl Show for Packed<OutlineElem> {
|
||||
let level = outlinable.level();
|
||||
if outlinable.outlined() && level <= depth {
|
||||
let entry = OutlineEntry::new(level, elem);
|
||||
seq.push(entry.pack().spanned(span));
|
||||
entries.push(entry.pack().spanned(span));
|
||||
}
|
||||
}
|
||||
|
||||
// Wrap the entries into a marker for pdf tagging.
|
||||
seq.push(OutlineBody::new(Content::sequence(entries)).pack());
|
||||
|
||||
Ok(Content::sequence(seq))
|
||||
}
|
||||
}
|
||||
@ -306,6 +311,19 @@ impl LocalName for Packed<OutlineElem> {
|
||||
const KEY: &'static str = "outline";
|
||||
}
|
||||
|
||||
/// Only used to mark
|
||||
#[elem(Locatable, Show)]
|
||||
pub struct OutlineBody {
|
||||
#[required]
|
||||
body: Content,
|
||||
}
|
||||
|
||||
impl Show for Packed<OutlineBody> {
|
||||
fn show(&self, _: &mut Engine, _: StyleChain) -> SourceResult<Content> {
|
||||
Ok(self.body.clone())
|
||||
}
|
||||
}
|
||||
|
||||
/// Defines how an outline is indented.
|
||||
#[derive(Debug, Clone, PartialEq, Hash)]
|
||||
pub enum OutlineIndent {
|
||||
@ -364,7 +382,7 @@ pub trait Outlinable: Refable {
|
||||
/// With show-set and show rules on outline entries, you can richly customize
|
||||
/// the outline's appearance. See the
|
||||
/// [section on styling the outline]($outline/#styling-the-outline) for details.
|
||||
#[elem(scope, name = "entry", title = "Outline Entry", Show)]
|
||||
#[elem(scope, name = "entry", title = "Outline Entry", Locatable, Show)]
|
||||
pub struct OutlineEntry {
|
||||
/// The nesting level of this outline entry. Starts at `{1}` for top-level
|
||||
/// entries.
|
||||
@ -417,8 +435,19 @@ impl Show for Packed<OutlineEntry> {
|
||||
let context = Context::new(None, Some(styles));
|
||||
let context = context.track();
|
||||
|
||||
// TODO: prefix should be wrapped in a `Lbl` structure element
|
||||
let prefix = self.prefix(engine, context, span)?;
|
||||
let inner = self.inner(engine, context, span)?;
|
||||
let body = self.body().at(span)?;
|
||||
let page = self.page(engine, context, span)?;
|
||||
let alt = {
|
||||
// TODO: accept user supplied alt text
|
||||
let prefix = prefix.as_ref().map(|p| p.plain_text()).unwrap_or_default();
|
||||
let body = body.plain_text();
|
||||
let page_str = PageElem::local_name_in(styles);
|
||||
let page_nr = page.plain_text();
|
||||
eco_format!("{prefix} \"{body}\", {page_str} {page_nr}")
|
||||
};
|
||||
let inner = self.inner(context, span, body, page)?;
|
||||
let block = if self.element.is::<EquationElem>() {
|
||||
let body = prefix.unwrap_or_default() + inner;
|
||||
BlockElem::new()
|
||||
@ -430,7 +459,7 @@ impl Show for Packed<OutlineEntry> {
|
||||
};
|
||||
|
||||
let loc = self.element_location().at(span)?;
|
||||
Ok(block.linked(Destination::Location(loc)))
|
||||
Ok(block.linked(Destination::Location(loc), Some(alt)))
|
||||
}
|
||||
}
|
||||
|
||||
@ -565,9 +594,10 @@ impl OutlineEntry {
|
||||
#[func(contextual)]
|
||||
pub fn inner(
|
||||
&self,
|
||||
engine: &mut Engine,
|
||||
context: Tracked<Context>,
|
||||
span: Span,
|
||||
body: Content,
|
||||
page: Content,
|
||||
) -> SourceResult<Content> {
|
||||
let styles = context.styles().at(span)?;
|
||||
|
||||
@ -588,7 +618,7 @@ impl OutlineEntry {
|
||||
seq.push(TextElem::packed("\u{202B}"));
|
||||
}
|
||||
|
||||
seq.push(self.body().at(span)?);
|
||||
seq.push(body);
|
||||
|
||||
if rtl {
|
||||
// "Pop Directional Formatting"
|
||||
@ -613,7 +643,7 @@ impl OutlineEntry {
|
||||
// Add the page number. The word joiner in front ensures that the page
|
||||
// number doesn't stand alone in its line.
|
||||
seq.push(TextElem::packed("\u{2060}"));
|
||||
seq.push(self.page(engine, context, span)?);
|
||||
seq.push(page);
|
||||
|
||||
Ok(Content::sequence(seq))
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ use crate::model::Numbering;
|
||||
/// let $a$ be the smallest of the
|
||||
/// three integers. Then, we ...
|
||||
/// ```
|
||||
#[elem(scope, title = "Paragraph")]
|
||||
#[elem(scope, title = "Paragraph", Locatable)]
|
||||
pub struct ParElem {
|
||||
/// The spacing between lines.
|
||||
///
|
||||
|
@ -343,7 +343,8 @@ fn show_reference(
|
||||
content = supplement + TextElem::packed("\u{a0}") + content;
|
||||
}
|
||||
|
||||
Ok(content.linked(Destination::Location(loc)))
|
||||
// TODO: accept user supplied alt text
|
||||
Ok(content.linked(Destination::Location(loc), None))
|
||||
}
|
||||
|
||||
/// Turn a reference into a citation.
|
||||
|
@ -4,6 +4,7 @@ use crate::foundations::{
|
||||
elem, Content, NativeElement, Packed, Show, StyleChain, TargetElem,
|
||||
};
|
||||
use crate::html::{tag, HtmlElem};
|
||||
use crate::introspection::Locatable;
|
||||
use crate::text::{TextElem, WeightDelta};
|
||||
|
||||
/// Strongly emphasizes content by increasing the font weight.
|
||||
@ -24,7 +25,7 @@ use crate::text::{TextElem, WeightDelta};
|
||||
/// simply enclose it in stars/asterisks (`*`). Note that this only works at
|
||||
/// word boundaries. To strongly emphasize part of a word, you have to use the
|
||||
/// function.
|
||||
#[elem(title = "Strong Emphasis", keywords = ["bold", "weight"], Show)]
|
||||
#[elem(title = "Strong Emphasis", keywords = ["bold", "weight"], Locatable, Show)]
|
||||
pub struct StrongElem {
|
||||
/// The delta to apply on the font weight.
|
||||
///
|
||||
|
@ -1,6 +1,8 @@
|
||||
use std::num::{NonZeroU32, NonZeroUsize};
|
||||
use std::sync::Arc;
|
||||
|
||||
use ecow::EcoString;
|
||||
use typst_macros::Cast;
|
||||
use typst_utils::NonZeroExt;
|
||||
|
||||
use crate::diag::{bail, HintedStrResult, HintedString, SourceResult};
|
||||
@ -10,7 +12,7 @@ use crate::foundations::{
|
||||
TargetElem,
|
||||
};
|
||||
use crate::html::{attr, tag, HtmlAttrs, HtmlElem, HtmlTag};
|
||||
use crate::introspection::Locator;
|
||||
use crate::introspection::{Locatable, Locator};
|
||||
use crate::layout::grid::resolve::{table_to_cellgrid, Cell, CellGrid, Entry};
|
||||
use crate::layout::{
|
||||
show_grid_cell, Abs, Alignment, BlockElem, Celled, GridCell, GridFooter, GridHLine,
|
||||
@ -121,7 +123,7 @@ use crate::visualize::{Paint, Stroke};
|
||||
/// [Robert], b, a, b,
|
||||
/// )
|
||||
/// ```
|
||||
#[elem(scope, Show, LocalName, Figurable)]
|
||||
#[elem(scope, Locatable, Show, LocalName, Figurable)]
|
||||
pub struct TableElem {
|
||||
/// The column sizes. See the [grid documentation]($grid) for more
|
||||
/// information on track sizing.
|
||||
@ -237,6 +239,9 @@ pub struct TableElem {
|
||||
#[default(Celled::Value(Sides::splat(Some(Abs::pt(5.0).into()))))]
|
||||
pub inset: Celled<Sides<Option<Rel<Length>>>>,
|
||||
|
||||
// TODO: docs
|
||||
pub summary: Option<EcoString>,
|
||||
|
||||
/// The contents of the table cells, plus any extra table lines specified
|
||||
/// with the [`table.hline`]($table.hline) and
|
||||
/// [`table.vline`]($table.vline) elements.
|
||||
@ -531,7 +536,7 @@ impl TryFrom<Content> for TableItem {
|
||||
/// [7.34], [57], [2],
|
||||
/// )
|
||||
/// ```
|
||||
#[elem(name = "header", title = "Table Header")]
|
||||
#[elem(name = "header", title = "Table Header", Locatable)]
|
||||
pub struct TableHeader {
|
||||
/// Whether this header should be repeated across pages.
|
||||
#[default(true)]
|
||||
@ -561,7 +566,7 @@ pub struct TableHeader {
|
||||
/// totals, or other information that should be visible on every page.
|
||||
///
|
||||
/// No other table cells may be placed after the footer.
|
||||
#[elem(name = "footer", title = "Table Footer")]
|
||||
#[elem(name = "footer", title = "Table Footer", Locatable)]
|
||||
pub struct TableFooter {
|
||||
/// Whether this footer should be repeated across pages.
|
||||
#[default(true)]
|
||||
@ -604,7 +609,7 @@ pub struct TableFooter {
|
||||
/// [19:00], [Day 1 Attendee Mixer],
|
||||
/// )
|
||||
/// ```
|
||||
#[elem(name = "hline", title = "Table Horizontal Line")]
|
||||
#[elem(name = "hline", title = "Table Horizontal Line", Locatable)]
|
||||
pub struct TableHLine {
|
||||
/// The row above which the horizontal line is placed (zero-indexed).
|
||||
/// Functions identically to the `y` field in [`grid.hline`]($grid.hline.y).
|
||||
@ -649,7 +654,7 @@ pub struct TableHLine {
|
||||
/// use the [table's `stroke`]($table.stroke) field or [`table.cell`'s
|
||||
/// `stroke`]($table.cell.stroke) field instead if the line you want to place is
|
||||
/// part of all your tables' designs.
|
||||
#[elem(name = "vline", title = "Table Vertical Line")]
|
||||
#[elem(name = "vline", title = "Table Vertical Line", Locatable)]
|
||||
pub struct TableVLine {
|
||||
/// The column before which the horizontal line is placed (zero-indexed).
|
||||
/// Functions identically to the `x` field in [`grid.vline`]($grid.vline).
|
||||
@ -770,7 +775,7 @@ pub struct TableVLine {
|
||||
/// [Vikram], [49], [Perseverance],
|
||||
/// )
|
||||
/// ```
|
||||
#[elem(name = "cell", title = "Table Cell", Show)]
|
||||
#[elem(name = "cell", title = "Table Cell", Locatable, Show)]
|
||||
pub struct TableCell {
|
||||
/// The cell's body.
|
||||
#[required]
|
||||
@ -806,6 +811,12 @@ pub struct TableCell {
|
||||
#[fold]
|
||||
pub stroke: Sides<Option<Option<Arc<Stroke>>>>,
|
||||
|
||||
// TODO: feature gate
|
||||
pub kind: Smart<TableCellKind>,
|
||||
|
||||
// TODO: feature gate
|
||||
pub header_scope: Smart<TableHeaderScope>,
|
||||
|
||||
/// Whether rows spanned by this cell can be placed in different pages.
|
||||
/// When equal to `{auto}`, a cell spanning only fixed-size rows is
|
||||
/// unbreakable, while a cell spanning at least one `{auto}`-sized row is
|
||||
@ -843,3 +854,18 @@ impl From<Content> for TableCell {
|
||||
value.unpack::<Self>().unwrap_or_else(Self::new)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)]
|
||||
pub enum TableHeaderScope {
|
||||
Both,
|
||||
Column,
|
||||
Row,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash, Cast)]
|
||||
pub enum TableCellKind {
|
||||
Header,
|
||||
Footer,
|
||||
#[default]
|
||||
Data,
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ use crate::foundations::{
|
||||
Styles, TargetElem,
|
||||
};
|
||||
use crate::html::{tag, HtmlElem};
|
||||
use crate::introspection::Locatable;
|
||||
use crate::layout::{Em, HElem, Length, Sides, StackChild, StackElem, VElem};
|
||||
use crate::model::{ListItemLike, ListLike, ParElem, ParbreakElem};
|
||||
use crate::text::TextElem;
|
||||
@ -27,7 +28,7 @@ use crate::text::TextElem;
|
||||
/// # Syntax
|
||||
/// This function also has dedicated syntax: Starting a line with a slash,
|
||||
/// followed by a term, a colon and a description creates a term list item.
|
||||
#[elem(scope, title = "Term List", Show)]
|
||||
#[elem(scope, title = "Term List", Locatable, Show)]
|
||||
pub struct TermsElem {
|
||||
/// Defines the default [spacing]($terms.spacing) of the term list. If it is
|
||||
/// `{false}`, the items are spaced apart with
|
||||
@ -205,7 +206,7 @@ impl Show for Packed<TermsElem> {
|
||||
}
|
||||
|
||||
/// A term list item.
|
||||
#[elem(name = "item", title = "Term List Item")]
|
||||
#[elem(name = "item", title = "Term List Item", Locatable)]
|
||||
pub struct TermItem {
|
||||
/// The term described by the list item.
|
||||
#[required]
|
||||
|
222
crates/typst-library/src/pdf/accessibility.rs
Normal file
222
crates/typst-library/src/pdf/accessibility.rs
Normal file
@ -0,0 +1,222 @@
|
||||
use ecow::EcoString;
|
||||
use typst_macros::{cast, elem, Cast};
|
||||
|
||||
use crate::diag::SourceResult;
|
||||
use crate::engine::Engine;
|
||||
use crate::foundations::{Content, Packed, Show, StyleChain};
|
||||
use crate::introspection::Locatable;
|
||||
|
||||
// TODO: docs
|
||||
#[elem(Locatable, Show)]
|
||||
pub struct PdfTagElem {
|
||||
#[default(PdfTagKind::NonStruct)]
|
||||
pub kind: PdfTagKind,
|
||||
|
||||
/// An alternate description.
|
||||
pub alt: Option<EcoString>,
|
||||
/// Exact replacement for this structure element and its children.
|
||||
pub actual_text: Option<EcoString>,
|
||||
/// The expanded form of an abbreviation/acronym.
|
||||
pub expansion: Option<EcoString>,
|
||||
|
||||
/// The content to underline.
|
||||
#[required]
|
||||
pub body: Content,
|
||||
}
|
||||
|
||||
impl Show for Packed<PdfTagElem> {
|
||||
#[typst_macros::time(name = "pdf.tag", span = self.span())]
|
||||
fn show(&self, _: &mut Engine, _: StyleChain) -> SourceResult<Content> {
|
||||
Ok(self.body.clone())
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: docs
|
||||
/// PDF structure elements
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum PdfTagKind {
|
||||
// grouping elements
|
||||
/// (Part)
|
||||
Part,
|
||||
/// (Article)
|
||||
Art,
|
||||
/// (Section)
|
||||
Sect,
|
||||
/// (Division)
|
||||
Div,
|
||||
/// (Block quotation)
|
||||
BlockQuote,
|
||||
/// (Caption)
|
||||
Caption,
|
||||
/// (Table of contents)
|
||||
TOC,
|
||||
/// (Table of contents item)
|
||||
TOCI,
|
||||
/// (Index)
|
||||
Index,
|
||||
/// (Nonstructural element)
|
||||
NonStruct,
|
||||
/// (Private element)
|
||||
Private,
|
||||
|
||||
// paragraph like elements
|
||||
/// (Heading)
|
||||
H { title: Option<EcoString> },
|
||||
/// (Heading level 1)
|
||||
H1 { title: Option<EcoString> },
|
||||
/// (Heading level 2)
|
||||
H2 { title: Option<EcoString> },
|
||||
/// (Heading level 3)
|
||||
H4 { title: Option<EcoString> },
|
||||
/// (Heading level 4)
|
||||
H3 { title: Option<EcoString> },
|
||||
/// (Heading level 5)
|
||||
H5 { title: Option<EcoString> },
|
||||
/// (Heading level 6)
|
||||
H6 { title: Option<EcoString> },
|
||||
/// (Paragraph)
|
||||
P,
|
||||
|
||||
// list elements
|
||||
/// (List)
|
||||
L { numbering: ListNumbering },
|
||||
/// (List item)
|
||||
LI,
|
||||
/// (Label)
|
||||
Lbl,
|
||||
/// (List body)
|
||||
LBody,
|
||||
|
||||
// table elements
|
||||
/// (Table)
|
||||
Table,
|
||||
/// (Table row)
|
||||
TR,
|
||||
/// (Table header)
|
||||
TH { scope: TableHeaderScope },
|
||||
/// (Table data cell)
|
||||
TD,
|
||||
/// (Table header row group)
|
||||
THead,
|
||||
/// (Table body row group)
|
||||
TBody,
|
||||
/// (Table footer row group)
|
||||
TFoot,
|
||||
|
||||
// inline elements
|
||||
/// (Span)
|
||||
Span,
|
||||
/// (Quotation)
|
||||
Quote,
|
||||
/// (Note)
|
||||
Note,
|
||||
/// (Reference)
|
||||
Reference,
|
||||
/// (Bibliography Entry)
|
||||
BibEntry,
|
||||
/// (Code)
|
||||
Code,
|
||||
/// (Link)
|
||||
Link,
|
||||
/// (Annotation)
|
||||
Annot,
|
||||
|
||||
/// (Ruby)
|
||||
Ruby,
|
||||
/// (Ruby base text)
|
||||
RB,
|
||||
/// (Ruby annotation text)
|
||||
RT,
|
||||
/// (Ruby punctuation)
|
||||
RP,
|
||||
|
||||
/// (Warichu)
|
||||
Warichu,
|
||||
/// (Warichu text)
|
||||
WT,
|
||||
/// (Warichu punctuation)
|
||||
WP,
|
||||
|
||||
/// (Figure)
|
||||
Figure,
|
||||
/// (Formula)
|
||||
Formula,
|
||||
/// (Form)
|
||||
Form,
|
||||
}
|
||||
|
||||
cast! {
|
||||
PdfTagKind,
|
||||
self => match self {
|
||||
PdfTagKind::Part => "part".into_value(),
|
||||
_ => todo!(),
|
||||
},
|
||||
"part" => Self::Part,
|
||||
// TODO
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum ListNumbering {
|
||||
/// No numbering.
|
||||
None,
|
||||
/// Solid circular bullets.
|
||||
Disc,
|
||||
/// Open circular bullets.
|
||||
Circle,
|
||||
/// Solid square bullets.
|
||||
Square,
|
||||
/// Decimal numbers.
|
||||
Decimal,
|
||||
/// Lowercase Roman numerals.
|
||||
LowerRoman,
|
||||
/// Uppercase Roman numerals.
|
||||
UpperRoman,
|
||||
/// Lowercase letters.
|
||||
LowerAlpha,
|
||||
/// Uppercase letters.
|
||||
UpperAlpha,
|
||||
}
|
||||
|
||||
/// The scope of a table header cell.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum TableHeaderScope {
|
||||
/// The header cell refers to the row.
|
||||
Row,
|
||||
/// The header cell refers to the column.
|
||||
Column,
|
||||
/// The header cell refers to both the row and the column.
|
||||
Both,
|
||||
}
|
||||
|
||||
/// Mark content as a PDF artifact.
|
||||
/// TODO: maybe generalize this and use it to mark html elements with `aria-hidden="true"`?
|
||||
#[elem(Locatable, Show)]
|
||||
pub struct ArtifactElem {
|
||||
/// The artifact kind.
|
||||
#[default(ArtifactKind::Other)]
|
||||
pub kind: ArtifactKind,
|
||||
|
||||
/// The content that is an artifact.
|
||||
#[required]
|
||||
pub body: Content,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Cast)]
|
||||
pub enum ArtifactKind {
|
||||
/// Page header artifacts.
|
||||
Header,
|
||||
/// Page footer artifacts.
|
||||
Footer,
|
||||
/// Other page artifacts.
|
||||
Page,
|
||||
/// Other artifacts.
|
||||
#[default]
|
||||
Other,
|
||||
}
|
||||
|
||||
impl Show for Packed<ArtifactElem> {
|
||||
#[typst_macros::time(name = "pdf.artifact", span = self.span())]
|
||||
fn show(&self, _: &mut Engine, _: StyleChain) -> SourceResult<Content> {
|
||||
Ok(self.body.clone())
|
||||
}
|
||||
}
|
@ -1,7 +1,9 @@
|
||||
//! PDF-specific functionality.
|
||||
|
||||
mod accessibility;
|
||||
mod embed;
|
||||
|
||||
pub use self::accessibility::*;
|
||||
pub use self::embed::*;
|
||||
|
||||
use crate::foundations::{Module, Scope};
|
||||
@ -11,5 +13,7 @@ pub fn module() -> Module {
|
||||
let mut pdf = Scope::deduplicating();
|
||||
pdf.start_category(crate::Category::Pdf);
|
||||
pdf.define_elem::<EmbedElem>();
|
||||
pdf.define_elem::<PdfTagElem>();
|
||||
pdf.define_elem::<ArtifactElem>();
|
||||
Module::new("pdf", pdf)
|
||||
}
|
||||
|
@ -2,7 +2,11 @@ use smallvec::smallvec;
|
||||
|
||||
use crate::diag::SourceResult;
|
||||
use crate::engine::Engine;
|
||||
use crate::foundations::{elem, Content, Packed, Show, Smart, StyleChain};
|
||||
use crate::foundations::{
|
||||
elem, Content, NativeElement, Packed, Show, Smart, StyleChain, TargetElem,
|
||||
};
|
||||
use crate::html::{attr, tag, HtmlElem};
|
||||
use crate::introspection::Locatable;
|
||||
use crate::layout::{Abs, Corners, Length, Rel, Sides};
|
||||
use crate::text::{BottomEdge, BottomEdgeMetric, TextElem, TopEdge, TopEdgeMetric};
|
||||
use crate::visualize::{Color, FixedStroke, Paint, Stroke};
|
||||
@ -13,7 +17,7 @@ use crate::visualize::{Color, FixedStroke, Paint, Stroke};
|
||||
/// ```example
|
||||
/// This is #underline[important].
|
||||
/// ```
|
||||
#[elem(Show)]
|
||||
#[elem(Locatable, Show)]
|
||||
pub struct UnderlineElem {
|
||||
/// How to [stroke] the line.
|
||||
///
|
||||
@ -81,6 +85,16 @@ pub struct UnderlineElem {
|
||||
impl Show for Packed<UnderlineElem> {
|
||||
#[typst_macros::time(name = "underline", span = self.span())]
|
||||
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
||||
if TargetElem::target_in(styles).is_html() {
|
||||
// Note: In modern HTML, `<u>` is not the underline element, but
|
||||
// rather an "Unarticulated Annotation" element (see HTML spec
|
||||
// 4.5.22). Using `text-decoration` instead is recommended by MDN.
|
||||
return Ok(HtmlElem::new(tag::span)
|
||||
.with_attr(attr::style, "text-decoration: underline")
|
||||
.with_body(Some(self.body.clone()))
|
||||
.pack());
|
||||
}
|
||||
|
||||
Ok(self.body.clone().styled(TextElem::set_deco(smallvec![Decoration {
|
||||
line: DecoLine::Underline {
|
||||
stroke: self.stroke(styles).unwrap_or_default(),
|
||||
@ -99,7 +113,7 @@ impl Show for Packed<UnderlineElem> {
|
||||
/// ```example
|
||||
/// #overline[A line over text.]
|
||||
/// ```
|
||||
#[elem(Show)]
|
||||
#[elem(Locatable, Show)]
|
||||
pub struct OverlineElem {
|
||||
/// How to [stroke] the line.
|
||||
///
|
||||
@ -173,6 +187,13 @@ pub struct OverlineElem {
|
||||
impl Show for Packed<OverlineElem> {
|
||||
#[typst_macros::time(name = "overline", span = self.span())]
|
||||
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
||||
if TargetElem::target_in(styles).is_html() {
|
||||
return Ok(HtmlElem::new(tag::span)
|
||||
.with_attr(attr::style, "text-decoration: overline")
|
||||
.with_body(Some(self.body.clone()))
|
||||
.pack());
|
||||
}
|
||||
|
||||
Ok(self.body.clone().styled(TextElem::set_deco(smallvec![Decoration {
|
||||
line: DecoLine::Overline {
|
||||
stroke: self.stroke(styles).unwrap_or_default(),
|
||||
@ -191,7 +212,7 @@ impl Show for Packed<OverlineElem> {
|
||||
/// ```example
|
||||
/// This is #strike[not] relevant.
|
||||
/// ```
|
||||
#[elem(title = "Strikethrough", Show)]
|
||||
#[elem(title = "Strikethrough", Locatable, Show)]
|
||||
pub struct StrikeElem {
|
||||
/// How to [stroke] the line.
|
||||
///
|
||||
@ -250,6 +271,10 @@ pub struct StrikeElem {
|
||||
impl Show for Packed<StrikeElem> {
|
||||
#[typst_macros::time(name = "strike", span = self.span())]
|
||||
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
||||
if TargetElem::target_in(styles).is_html() {
|
||||
return Ok(HtmlElem::new(tag::s).with_body(Some(self.body.clone())).pack());
|
||||
}
|
||||
|
||||
Ok(self.body.clone().styled(TextElem::set_deco(smallvec![Decoration {
|
||||
// Note that we do not support evade option for strikethrough.
|
||||
line: DecoLine::Strikethrough {
|
||||
@ -268,7 +293,7 @@ impl Show for Packed<StrikeElem> {
|
||||
/// ```example
|
||||
/// This is #highlight[important].
|
||||
/// ```
|
||||
#[elem(Show)]
|
||||
#[elem(Locatable, Show)]
|
||||
pub struct HighlightElem {
|
||||
/// The color to highlight the text with.
|
||||
///
|
||||
@ -345,6 +370,12 @@ pub struct HighlightElem {
|
||||
impl Show for Packed<HighlightElem> {
|
||||
#[typst_macros::time(name = "highlight", span = self.span())]
|
||||
fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
||||
if TargetElem::target_in(styles).is_html() {
|
||||
return Ok(HtmlElem::new(tag::mark)
|
||||
.with_body(Some(self.body.clone()))
|
||||
.pack());
|
||||
}
|
||||
|
||||
Ok(self.body.clone().styled(TextElem::set_deco(smallvec![Decoration {
|
||||
line: DecoLine::Highlight {
|
||||
fill: self.fill(styles),
|
||||
|
@ -20,6 +20,7 @@ use crate::foundations::{
|
||||
PlainText, Show, ShowSet, Smart, StyleChain, Styles, Synthesize, TargetElem,
|
||||
};
|
||||
use crate::html::{tag, HtmlElem};
|
||||
use crate::introspection::Locatable;
|
||||
use crate::layout::{BlockBody, BlockElem, Em, HAlignment};
|
||||
use crate::loading::{DataSource, Load};
|
||||
use crate::model::{Figurable, ParElem};
|
||||
@ -78,6 +79,7 @@ use crate::World;
|
||||
scope,
|
||||
title = "Raw Text / Code",
|
||||
Synthesize,
|
||||
Locatable,
|
||||
Show,
|
||||
ShowSet,
|
||||
LocalName,
|
||||
@ -636,7 +638,7 @@ fn format_theme_error(error: syntect::LoadingError) -> LoadError {
|
||||
/// It allows you to access various properties of the line, such as the line
|
||||
/// number, the raw non-highlighted text, the highlighted text, and whether it
|
||||
/// is the first or last line of the raw block.
|
||||
#[elem(name = "line", title = "Raw Text / Code Line", Show, PlainText)]
|
||||
#[elem(name = "line", title = "Raw Text / Code Line", Locatable, Show, PlainText)]
|
||||
pub struct RawLine {
|
||||
/// The line number of the raw line inside of the raw block, starts at 1.
|
||||
#[required]
|
||||
|
@ -6,6 +6,7 @@ use crate::foundations::{
|
||||
elem, Content, NativeElement, Packed, SequenceElem, Show, StyleChain, TargetElem,
|
||||
};
|
||||
use crate::html::{tag, HtmlElem};
|
||||
use crate::introspection::Locatable;
|
||||
use crate::layout::{Em, Length};
|
||||
use crate::text::{variant, SpaceElem, TextElem, TextSize};
|
||||
use crate::World;
|
||||
@ -18,7 +19,7 @@ use crate::World;
|
||||
/// ```example
|
||||
/// Revenue#sub[yearly]
|
||||
/// ```
|
||||
#[elem(title = "Subscript", Show)]
|
||||
#[elem(title = "Subscript", Locatable, Show)]
|
||||
pub struct SubElem {
|
||||
/// Whether to prefer the dedicated subscript characters of the font.
|
||||
///
|
||||
@ -84,7 +85,7 @@ impl Show for Packed<SubElem> {
|
||||
/// ```example
|
||||
/// 1#super[st] try!
|
||||
/// ```
|
||||
#[elem(title = "Superscript", Show)]
|
||||
#[elem(title = "Superscript", Locatable, Show)]
|
||||
pub struct SuperElem {
|
||||
/// Whether to prefer the dedicated superscript characters of the font.
|
||||
///
|
||||
|
@ -1285,24 +1285,17 @@ fn process_stops(stops: &[Spanned<GradientStop>]) -> SourceResult<Vec<(Color, Ra
|
||||
/// Sample the stops at a given position.
|
||||
fn sample_stops(stops: &[(Color, Ratio)], mixing_space: ColorSpace, t: f64) -> Color {
|
||||
let t = t.clamp(0.0, 1.0);
|
||||
let mut low = 0;
|
||||
let mut high = stops.len();
|
||||
let mut j = stops.partition_point(|(_, ratio)| ratio.get() < t);
|
||||
|
||||
while low < high {
|
||||
let mid = (low + high) / 2;
|
||||
if stops[mid].1.get() < t {
|
||||
low = mid + 1;
|
||||
} else {
|
||||
high = mid;
|
||||
if j == 0 {
|
||||
while stops.get(j + 1).is_some_and(|(_, r)| r.is_zero()) {
|
||||
j += 1;
|
||||
}
|
||||
return stops[j].0;
|
||||
}
|
||||
|
||||
if low == 0 {
|
||||
low = 1;
|
||||
}
|
||||
|
||||
let (col_0, pos_0) = stops[low - 1];
|
||||
let (col_1, pos_1) = stops[low];
|
||||
let (col_0, pos_0) = stops[j - 1];
|
||||
let (col_1, pos_1) = stops[j];
|
||||
let t = (t - pos_0.get()) / (pos_1.get() - pos_0.get());
|
||||
|
||||
Color::mix_iter(
|
||||
|
@ -21,6 +21,7 @@ use crate::foundations::{
|
||||
cast, elem, func, scope, Bytes, Cast, Content, Derived, NativeElement, Packed, Show,
|
||||
Smart, StyleChain,
|
||||
};
|
||||
use crate::introspection::Locatable;
|
||||
use crate::layout::{BlockElem, Length, Rel, Sizing};
|
||||
use crate::loading::{DataSource, Load, LoadSource, Loaded, Readable};
|
||||
use crate::model::Figurable;
|
||||
@ -44,7 +45,7 @@ use crate::text::LocalName;
|
||||
/// ],
|
||||
/// )
|
||||
/// ```
|
||||
#[elem(scope, Show, LocalName, Figurable)]
|
||||
#[elem(scope, Locatable, Show, LocalName, Figurable)]
|
||||
pub struct ImageElem {
|
||||
/// A [path]($syntax/#paths) to an image file or raw bytes making up an
|
||||
/// image in one of the supported [formats]($image.format).
|
||||
|
@ -2,7 +2,6 @@ use std::collections::{BTreeMap, HashMap, HashSet};
|
||||
use std::num::NonZeroU64;
|
||||
|
||||
use ecow::{eco_format, EcoVec};
|
||||
use krilla::annotation::Annotation;
|
||||
use krilla::configure::{Configuration, ValidationError, Validator};
|
||||
use krilla::destination::{NamedDestination, XyzDestination};
|
||||
use krilla::embed::EmbedError;
|
||||
@ -14,7 +13,7 @@ use krilla::{Document, SerializeSettings};
|
||||
use krilla_svg::render_svg_glyph;
|
||||
use typst_library::diag::{bail, error, SourceDiagnostic, SourceResult};
|
||||
use typst_library::foundations::{NativeElement, Repr};
|
||||
use typst_library::introspection::Location;
|
||||
use typst_library::introspection::{Location, Tag};
|
||||
use typst_library::layout::{
|
||||
Abs, Frame, FrameItem, GroupItem, PagedDocument, Size, Transform,
|
||||
};
|
||||
@ -25,11 +24,12 @@ use typst_syntax::Span;
|
||||
|
||||
use crate::embed::embed_files;
|
||||
use crate::image::handle_image;
|
||||
use crate::link::handle_link;
|
||||
use crate::link::{handle_link, LinkAnnotation};
|
||||
use crate::metadata::build_metadata;
|
||||
use crate::outline::build_outline;
|
||||
use crate::page::PageLabelExt;
|
||||
use crate::shape::handle_shape;
|
||||
use crate::tags::{self, Tags};
|
||||
use crate::text::handle_text;
|
||||
use crate::util::{convert_path, display_font, AbsExt, TransformExt};
|
||||
use crate::PdfOptions;
|
||||
@ -46,7 +46,7 @@ pub fn convert(
|
||||
xmp_metadata: true,
|
||||
cmyk_profile: None,
|
||||
configuration: options.standards.config,
|
||||
enable_tagging: false,
|
||||
enable_tagging: !options.disable_tags,
|
||||
render_svg_glyph_fn: render_svg_glyph,
|
||||
};
|
||||
|
||||
@ -54,6 +54,7 @@ pub fn convert(
|
||||
let page_index_converter = PageIndexConverter::new(typst_document, options);
|
||||
let named_destinations =
|
||||
collect_named_destinations(typst_document, &page_index_converter);
|
||||
|
||||
let mut gc = GlobalContext::new(
|
||||
typst_document,
|
||||
options,
|
||||
@ -66,6 +67,7 @@ pub fn convert(
|
||||
|
||||
document.set_outline(build_outline(&gc));
|
||||
document.set_metadata(build_metadata(&gc));
|
||||
document.set_tag_tree(gc.tags.build_tree());
|
||||
|
||||
finish(document, gc, options.standards.config)
|
||||
}
|
||||
@ -115,9 +117,7 @@ fn convert_pages(gc: &mut GlobalContext, document: &mut Document) -> SourceResul
|
||||
|
||||
surface.finish();
|
||||
|
||||
for annotation in fc.annotations {
|
||||
page.add_annotation(annotation);
|
||||
}
|
||||
tags::add_annotations(gc, &mut page, fc.link_annotations);
|
||||
}
|
||||
}
|
||||
|
||||
@ -171,14 +171,14 @@ impl State {
|
||||
/// Context needed for converting a single frame.
|
||||
pub(crate) struct FrameContext {
|
||||
states: Vec<State>,
|
||||
annotations: Vec<Annotation>,
|
||||
link_annotations: Vec<LinkAnnotation>,
|
||||
}
|
||||
|
||||
impl FrameContext {
|
||||
pub(crate) fn new(size: Size) -> Self {
|
||||
Self {
|
||||
states: vec![State::new(size)],
|
||||
annotations: vec![],
|
||||
link_annotations: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -198,8 +198,18 @@ impl FrameContext {
|
||||
self.states.last_mut().unwrap()
|
||||
}
|
||||
|
||||
pub(crate) fn push_annotation(&mut self, annotation: Annotation) {
|
||||
self.annotations.push(annotation);
|
||||
pub(crate) fn get_link_annotation(
|
||||
&mut self,
|
||||
link_id: tags::LinkId,
|
||||
) -> Option<&mut LinkAnnotation> {
|
||||
self.link_annotations
|
||||
.iter_mut()
|
||||
.rev()
|
||||
.find(|annot| annot.id == link_id)
|
||||
}
|
||||
|
||||
pub(crate) fn push_link_annotation(&mut self, annotation: LinkAnnotation) {
|
||||
self.link_annotations.push(annotation);
|
||||
}
|
||||
}
|
||||
|
||||
@ -225,6 +235,8 @@ pub(crate) struct GlobalContext<'a> {
|
||||
/// The languages used throughout the document.
|
||||
pub(crate) languages: BTreeMap<Lang, usize>,
|
||||
pub(crate) page_index_converter: PageIndexConverter,
|
||||
/// Tagged PDF context.
|
||||
pub(crate) tags: Tags,
|
||||
}
|
||||
|
||||
impl<'a> GlobalContext<'a> {
|
||||
@ -244,6 +256,8 @@ impl<'a> GlobalContext<'a> {
|
||||
image_spans: HashSet::new(),
|
||||
languages: BTreeMap::new(),
|
||||
page_index_converter,
|
||||
|
||||
tags: Tags::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -278,8 +292,9 @@ pub(crate) fn handle_frame(
|
||||
FrameItem::Image(image, size, span) => {
|
||||
handle_image(gc, fc, image, *size, surface, *span)?
|
||||
}
|
||||
FrameItem::Link(d, s) => handle_link(fc, gc, d, *s),
|
||||
FrameItem::Tag(_) => {}
|
||||
FrameItem::Link(dest, size) => handle_link(fc, gc, dest, *size),
|
||||
FrameItem::Tag(Tag::Start(elem)) => tags::handle_start(gc, elem),
|
||||
FrameItem::Tag(Tag::End(loc, _)) => tags::handle_end(gc, *loc),
|
||||
}
|
||||
|
||||
fc.pop();
|
||||
@ -294,7 +309,7 @@ pub(crate) fn handle_group(
|
||||
fc: &mut FrameContext,
|
||||
group: &GroupItem,
|
||||
surface: &mut Surface,
|
||||
context: &mut GlobalContext,
|
||||
gc: &mut GlobalContext,
|
||||
) -> SourceResult<()> {
|
||||
fc.push();
|
||||
fc.state_mut().pre_concat(group.transform);
|
||||
@ -310,10 +325,12 @@ pub(crate) fn handle_group(
|
||||
.and_then(|p| p.transform(fc.state().transform.to_krilla()));
|
||||
|
||||
if let Some(clip_path) = &clip_path {
|
||||
let mut handle = tags::start_marked(gc, surface);
|
||||
let surface = handle.surface();
|
||||
surface.push_clip_path(clip_path, &krilla::paint::FillRule::NonZero);
|
||||
}
|
||||
|
||||
handle_frame(fc, &group.frame, None, surface, context)?;
|
||||
handle_frame(fc, &group.frame, None, surface, gc)?;
|
||||
|
||||
if clip_path.is_some() {
|
||||
surface.pop();
|
||||
@ -576,6 +593,16 @@ fn convert_error(
|
||||
"{prefix} missing document date";
|
||||
hint: "set the date of the document"
|
||||
),
|
||||
ValidationError::DuplicateTagId(_id, loc) => {
|
||||
// TODO: display the id and better error message
|
||||
let span = to_span(*loc);
|
||||
error!(span, "{prefix} duplicate tag id")
|
||||
}
|
||||
ValidationError::UnknownHeaderTagId(_id, loc) => {
|
||||
// TODO: display the id and better error message
|
||||
let span = to_span(*loc);
|
||||
error!(span, "{prefix} unknown header tag id")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@ use std::sync::{Arc, OnceLock};
|
||||
use image::{DynamicImage, EncodableLayout, GenericImageView, Rgba};
|
||||
use krilla::image::{BitsPerComponent, CustomImage, ImageColorspace};
|
||||
use krilla::surface::Surface;
|
||||
use krilla::tagging::SpanTag;
|
||||
use krilla_svg::{SurfaceExt, SvgSettings};
|
||||
use typst_library::diag::{bail, SourceResult};
|
||||
use typst_library::foundations::Smart;
|
||||
@ -14,6 +15,7 @@ use typst_library::visualize::{
|
||||
use typst_syntax::Span;
|
||||
|
||||
use crate::convert::{FrameContext, GlobalContext};
|
||||
use crate::tags;
|
||||
use crate::util::{SizeExt, TransformExt};
|
||||
|
||||
#[typst_macros::time(name = "handle image")]
|
||||
@ -30,12 +32,11 @@ pub(crate) fn handle_image(
|
||||
|
||||
let interpolate = image.scaling() == Smart::Custom(ImageScaling::Smooth);
|
||||
|
||||
if let Some(alt) = image.alt() {
|
||||
surface.start_alt_text(alt);
|
||||
}
|
||||
|
||||
gc.image_spans.insert(span);
|
||||
|
||||
let mut handle =
|
||||
tags::start_span(gc, surface, SpanTag::empty().with_alt_text(image.alt()));
|
||||
let surface = handle.surface();
|
||||
match image.kind() {
|
||||
ImageKind::Raster(raster) => {
|
||||
let (exif_transform, new_size) = exif_transform(raster, size);
|
||||
@ -62,10 +63,6 @@ pub(crate) fn handle_image(
|
||||
}
|
||||
}
|
||||
|
||||
if image.alt().is_some() {
|
||||
surface.end_alt_text();
|
||||
}
|
||||
|
||||
surface.pop();
|
||||
surface.reset_location();
|
||||
|
||||
|
@ -9,6 +9,7 @@ mod outline;
|
||||
mod page;
|
||||
mod paint;
|
||||
mod shape;
|
||||
mod tags;
|
||||
mod text;
|
||||
mod util;
|
||||
|
||||
@ -53,6 +54,11 @@ pub struct PdfOptions<'a> {
|
||||
pub page_ranges: Option<PageRanges>,
|
||||
/// A list of PDF standards that Typst will enforce conformance with.
|
||||
pub standards: PdfStandards,
|
||||
/// By default, even when not producing a `PDF/UA-1` document, a tagged PDF
|
||||
/// document is written to provide a baseline of accessibility. In some
|
||||
/// circumstances, for example when trying to reduce the size of a document,
|
||||
/// it can be desirable to disable tagged PDF.
|
||||
pub disable_tags: bool,
|
||||
}
|
||||
|
||||
/// Encapsulates a list of compatible PDF standards.
|
||||
@ -104,6 +110,7 @@ impl PdfStandards {
|
||||
PdfStandard::A_4 => set_validator(Validator::A4)?,
|
||||
PdfStandard::A_4f => set_validator(Validator::A4F)?,
|
||||
PdfStandard::A_4e => set_validator(Validator::A4E)?,
|
||||
PdfStandard::Ua_1 => set_validator(Validator::UA1)?,
|
||||
}
|
||||
}
|
||||
|
||||
@ -187,4 +194,7 @@ pub enum PdfStandard {
|
||||
/// PDF/A-4e.
|
||||
#[serde(rename = "a-4e")]
|
||||
A_4e,
|
||||
/// PDF/UA-1.
|
||||
#[serde(rename = "ua-1")]
|
||||
Ua_1,
|
||||
}
|
||||
|
@ -1,19 +1,90 @@
|
||||
use ecow::EcoString;
|
||||
use krilla::action::{Action, LinkAction};
|
||||
use krilla::annotation::{LinkAnnotation, Target};
|
||||
use krilla::annotation::Target;
|
||||
use krilla::configure::Validator;
|
||||
use krilla::destination::XyzDestination;
|
||||
use krilla::geom::Rect;
|
||||
use typst_library::layout::{Abs, Point, Size};
|
||||
use krilla::geom as kg;
|
||||
use typst_library::layout::{Abs, Point, Position, Size};
|
||||
use typst_library::model::Destination;
|
||||
|
||||
use crate::convert::{FrameContext, GlobalContext};
|
||||
use crate::tags::{self, Placeholder, StackEntryKind, TagNode};
|
||||
use crate::util::{AbsExt, PointExt};
|
||||
|
||||
pub(crate) struct LinkAnnotation {
|
||||
pub(crate) id: tags::LinkId,
|
||||
pub(crate) placeholder: Placeholder,
|
||||
pub(crate) alt: Option<String>,
|
||||
pub(crate) rect: kg::Rect,
|
||||
pub(crate) quad_points: Vec<kg::Point>,
|
||||
pub(crate) target: Target,
|
||||
}
|
||||
|
||||
pub(crate) fn handle_link(
|
||||
fc: &mut FrameContext,
|
||||
gc: &mut GlobalContext,
|
||||
dest: &Destination,
|
||||
size: Size,
|
||||
) {
|
||||
let target = match dest {
|
||||
Destination::Url(u) => {
|
||||
Target::Action(Action::Link(LinkAction::new(u.to_string())))
|
||||
}
|
||||
Destination::Position(p) => match pos_to_target(gc, *p) {
|
||||
Some(target) => target,
|
||||
None => return,
|
||||
},
|
||||
Destination::Location(loc) => {
|
||||
if let Some(nd) = gc.loc_to_names.get(loc) {
|
||||
// If a named destination has been registered, it's already guaranteed to
|
||||
// not point to an excluded page.
|
||||
Target::Destination(krilla::destination::Destination::Named(nd.clone()))
|
||||
} else {
|
||||
let pos = gc.document.introspector.position(*loc);
|
||||
match pos_to_target(gc, pos) {
|
||||
Some(target) => target,
|
||||
None => return,
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let entry = gc.tags.stack.last_mut().expect("a link parent");
|
||||
let StackEntryKind::Link(link_id, ref link) = entry.kind else {
|
||||
unreachable!("expected a link parent")
|
||||
};
|
||||
let alt = link.alt.as_ref().map(EcoString::to_string);
|
||||
|
||||
let rect = to_rect(fc, size);
|
||||
let quadpoints = quadpoints(rect);
|
||||
|
||||
// Unfortunately quadpoints still aren't well supported by most PDF readers,
|
||||
// even by acrobat. Which is understandable since they were only introduced
|
||||
// in PDF 1.6 (2005) /s
|
||||
let should_use_quadpoints = gc.options.standards.config.validator() == Validator::UA1;
|
||||
match fc.get_link_annotation(link_id) {
|
||||
Some(annotation) if should_use_quadpoints => {
|
||||
// Update the bounding box and add the quadpoints to an existing link annotation.
|
||||
annotation.rect = bounding_rect(annotation.rect, rect);
|
||||
annotation.quad_points.extend_from_slice(&quadpoints);
|
||||
}
|
||||
_ => {
|
||||
let placeholder = gc.tags.reserve_placeholder();
|
||||
gc.tags.push(TagNode::Placeholder(placeholder));
|
||||
fc.push_link_annotation(LinkAnnotation {
|
||||
id: link_id,
|
||||
placeholder,
|
||||
rect,
|
||||
quad_points: quadpoints.to_vec(),
|
||||
alt,
|
||||
target,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compute the bounding box of the transformed link.
|
||||
fn to_rect(fc: &FrameContext, size: Size) -> kg::Rect {
|
||||
let mut min_x = Abs::inf();
|
||||
let mut min_y = Abs::inf();
|
||||
let mut max_x = -Abs::inf();
|
||||
@ -21,7 +92,6 @@ pub(crate) fn handle_link(
|
||||
|
||||
let pos = Point::zero();
|
||||
|
||||
// Compute the bounding box of the transformed link.
|
||||
for point in [
|
||||
pos,
|
||||
pos + Point::with_x(size.x),
|
||||
@ -40,55 +110,32 @@ pub(crate) fn handle_link(
|
||||
let y1 = min_y.to_f32();
|
||||
let y2 = max_y.to_f32();
|
||||
|
||||
let rect = Rect::from_ltrb(x1, y1, x2, y2).unwrap();
|
||||
kg::Rect::from_ltrb(x1, y1, x2, y2).unwrap()
|
||||
}
|
||||
|
||||
// TODO: Support quad points.
|
||||
|
||||
let pos = match dest {
|
||||
Destination::Url(u) => {
|
||||
fc.push_annotation(
|
||||
LinkAnnotation::new(
|
||||
rect,
|
||||
None,
|
||||
Target::Action(Action::Link(LinkAction::new(u.to_string()))),
|
||||
fn bounding_rect(a: kg::Rect, b: kg::Rect) -> kg::Rect {
|
||||
kg::Rect::from_ltrb(
|
||||
a.left().min(b.left()),
|
||||
a.top().min(b.top()),
|
||||
a.right().max(b.right()),
|
||||
a.bottom().max(b.bottom()),
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
return;
|
||||
.unwrap()
|
||||
}
|
||||
Destination::Position(p) => *p,
|
||||
Destination::Location(loc) => {
|
||||
if let Some(nd) = gc.loc_to_names.get(loc) {
|
||||
// If a named destination has been registered, it's already guaranteed to
|
||||
// not point to an excluded page.
|
||||
fc.push_annotation(
|
||||
LinkAnnotation::new(
|
||||
rect,
|
||||
None,
|
||||
Target::Destination(krilla::destination::Destination::Named(
|
||||
nd.clone(),
|
||||
)),
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
return;
|
||||
} else {
|
||||
gc.document.introspector.position(*loc)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
fn quadpoints(rect: kg::Rect) -> [kg::Point; 4] {
|
||||
[
|
||||
kg::Point::from_xy(rect.left(), rect.bottom()),
|
||||
kg::Point::from_xy(rect.right(), rect.bottom()),
|
||||
kg::Point::from_xy(rect.right(), rect.top()),
|
||||
kg::Point::from_xy(rect.left(), rect.top()),
|
||||
]
|
||||
}
|
||||
|
||||
fn pos_to_target(gc: &mut GlobalContext, pos: Position) -> Option<Target> {
|
||||
let page_index = pos.page.get() - 1;
|
||||
if let Some(index) = gc.page_index_converter.pdf_page_index(page_index) {
|
||||
fc.push_annotation(
|
||||
LinkAnnotation::new(
|
||||
rect,
|
||||
None,
|
||||
Target::Destination(krilla::destination::Destination::Xyz(
|
||||
XyzDestination::new(index, pos.point.to_krilla()),
|
||||
)),
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
let index = gc.page_index_converter.pdf_page_index(page_index)?;
|
||||
|
||||
let dest = XyzDestination::new(index, pos.point.to_krilla());
|
||||
Some(Target::Destination(krilla::destination::Destination::Xyz(dest)))
|
||||
}
|
||||
|
@ -5,8 +5,8 @@ use typst_library::visualize::{Geometry, Shape};
|
||||
use typst_syntax::Span;
|
||||
|
||||
use crate::convert::{FrameContext, GlobalContext};
|
||||
use crate::paint;
|
||||
use crate::util::{convert_path, AbsExt, TransformExt};
|
||||
use crate::{paint, tags};
|
||||
|
||||
#[typst_macros::time(name = "handle shape")]
|
||||
pub(crate) fn handle_shape(
|
||||
@ -16,6 +16,9 @@ pub(crate) fn handle_shape(
|
||||
gc: &mut GlobalContext,
|
||||
span: Span,
|
||||
) -> SourceResult<()> {
|
||||
let mut handle = tags::start_marked(gc, surface);
|
||||
let surface = handle.surface();
|
||||
|
||||
surface.set_location(span.into_raw().get());
|
||||
surface.push_transform(&fc.state().transform().to_krilla());
|
||||
|
||||
|
625
crates/typst-pdf/src/tags.rs
Normal file
625
crates/typst-pdf/src/tags.rs
Normal file
@ -0,0 +1,625 @@
|
||||
use std::cell::OnceCell;
|
||||
use std::num::{NonZeroU32, NonZeroUsize};
|
||||
|
||||
use ecow::EcoString;
|
||||
use krilla::page::Page;
|
||||
use krilla::surface::Surface;
|
||||
use krilla::tagging::{
|
||||
ArtifactType, ContentTag, Identifier, Node, SpanTag, TableCellSpan, TableDataCell,
|
||||
TableHeaderCell, Tag, TagBuilder, TagGroup, TagKind, TagTree,
|
||||
};
|
||||
use typst_library::foundations::{Content, LinkMarker, Packed, Smart, StyleChain};
|
||||
use typst_library::introspection::Location;
|
||||
use typst_library::layout::RepeatElem;
|
||||
use typst_library::model::{
|
||||
Destination, FigureCaption, FigureElem, HeadingElem, Outlinable, OutlineBody,
|
||||
OutlineEntry, TableCell, TableCellKind, TableElem, TableHeaderScope,
|
||||
};
|
||||
use typst_library::pdf::{ArtifactElem, ArtifactKind, PdfTagElem, PdfTagKind};
|
||||
use typst_library::visualize::ImageElem;
|
||||
|
||||
use crate::convert::GlobalContext;
|
||||
use crate::link::LinkAnnotation;
|
||||
|
||||
pub(crate) struct Tags {
|
||||
/// The intermediary stack of nested tag groups.
|
||||
pub(crate) stack: Vec<StackEntry>,
|
||||
/// A list of placeholders corresponding to a [`TagNode::Placeholder`].
|
||||
pub(crate) placeholders: Vec<OnceCell<Node>>,
|
||||
pub(crate) in_artifact: Option<(Location, ArtifactKind)>,
|
||||
pub(crate) link_id: LinkId,
|
||||
|
||||
/// The output.
|
||||
pub(crate) tree: Vec<TagNode>,
|
||||
}
|
||||
|
||||
pub(crate) struct StackEntry {
|
||||
pub(crate) loc: Location,
|
||||
pub(crate) kind: StackEntryKind,
|
||||
pub(crate) nodes: Vec<TagNode>,
|
||||
}
|
||||
|
||||
pub(crate) enum StackEntryKind {
|
||||
Standard(Tag),
|
||||
Outline(OutlineCtx),
|
||||
OutlineEntry(Packed<OutlineEntry>),
|
||||
Table(TableCtx),
|
||||
TableCell(Packed<TableCell>),
|
||||
Link(LinkId, Packed<LinkMarker>),
|
||||
}
|
||||
|
||||
impl StackEntryKind {
|
||||
pub(crate) fn as_standard_mut(&mut self) -> Option<&mut Tag> {
|
||||
if let Self::Standard(v) = self {
|
||||
Some(v)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct OutlineCtx {
|
||||
stack: Vec<OutlineSection>,
|
||||
}
|
||||
|
||||
pub(crate) struct OutlineSection {
|
||||
entries: Vec<TagNode>,
|
||||
}
|
||||
|
||||
impl OutlineSection {
|
||||
const fn new() -> Self {
|
||||
OutlineSection { entries: Vec::new() }
|
||||
}
|
||||
|
||||
fn push(&mut self, entry: TagNode) {
|
||||
self.entries.push(entry);
|
||||
}
|
||||
|
||||
fn into_tag(self) -> TagNode {
|
||||
TagNode::Group(TagKind::TOC.into(), self.entries)
|
||||
}
|
||||
}
|
||||
|
||||
impl OutlineCtx {
|
||||
fn new() -> Self {
|
||||
Self { stack: Vec::new() }
|
||||
}
|
||||
|
||||
fn insert(
|
||||
&mut self,
|
||||
outline_nodes: &mut Vec<TagNode>,
|
||||
entry: Packed<OutlineEntry>,
|
||||
nodes: Vec<TagNode>,
|
||||
) {
|
||||
let expected_len = entry.level.get() - 1;
|
||||
if self.stack.len() < expected_len {
|
||||
self.stack.resize_with(expected_len, || OutlineSection::new());
|
||||
} else {
|
||||
while self.stack.len() > expected_len {
|
||||
self.finish_section(outline_nodes);
|
||||
}
|
||||
}
|
||||
|
||||
let section_entry = TagNode::Group(TagKind::TOCI.into(), nodes);
|
||||
self.push(outline_nodes, section_entry);
|
||||
}
|
||||
|
||||
fn finish_section(&mut self, outline_nodes: &mut Vec<TagNode>) {
|
||||
let sub_section = self.stack.pop().unwrap().into_tag();
|
||||
self.push(outline_nodes, sub_section);
|
||||
}
|
||||
|
||||
fn push(&mut self, outline_nodes: &mut Vec<TagNode>, entry: TagNode) {
|
||||
match self.stack.last_mut() {
|
||||
Some(section) => section.push(entry),
|
||||
None => outline_nodes.push(entry),
|
||||
}
|
||||
}
|
||||
|
||||
fn build_outline(mut self, mut outline_nodes: Vec<TagNode>) -> Vec<TagNode> {
|
||||
while self.stack.len() > 0 {
|
||||
self.finish_section(&mut outline_nodes);
|
||||
}
|
||||
outline_nodes
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct TableCtx {
|
||||
table: Packed<TableElem>,
|
||||
rows: Vec<Vec<GridCell>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
enum GridCell {
|
||||
Cell(TableCtxCell),
|
||||
Spanned(usize, usize),
|
||||
#[default]
|
||||
Missing,
|
||||
}
|
||||
|
||||
impl GridCell {
|
||||
fn as_cell(&self) -> Option<&TableCtxCell> {
|
||||
if let Self::Cell(v) = self {
|
||||
Some(v)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn into_cell(self) -> Option<TableCtxCell> {
|
||||
if let Self::Cell(v) = self {
|
||||
Some(v)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct TableCtxCell {
|
||||
rowspan: NonZeroUsize,
|
||||
colspan: NonZeroUsize,
|
||||
kind: TableCellKind,
|
||||
header_scope: Smart<TableHeaderScope>,
|
||||
nodes: Vec<TagNode>,
|
||||
}
|
||||
|
||||
impl TableCtx {
|
||||
fn new(table: Packed<TableElem>) -> Self {
|
||||
Self { table: table.clone(), rows: Vec::new() }
|
||||
}
|
||||
|
||||
fn insert(&mut self, cell: Packed<TableCell>, nodes: Vec<TagNode>) {
|
||||
let x = cell.x(StyleChain::default()).unwrap_or_else(|| unreachable!());
|
||||
let y = cell.y(StyleChain::default()).unwrap_or_else(|| unreachable!());
|
||||
let rowspan = cell.rowspan(StyleChain::default());
|
||||
let colspan = cell.colspan(StyleChain::default());
|
||||
let kind = cell.kind(StyleChain::default());
|
||||
let header_scope = cell.header_scope(StyleChain::default());
|
||||
|
||||
// The explicit cell kind takes precedence, but if it is `auto` and a
|
||||
// scope was specified, make this a header cell.
|
||||
let kind = match (kind, header_scope) {
|
||||
(Smart::Custom(kind), _) => kind,
|
||||
(Smart::Auto, Smart::Custom(_)) => TableCellKind::Header,
|
||||
(Smart::Auto, Smart::Auto) => TableCellKind::Data,
|
||||
};
|
||||
|
||||
// Extend the table grid to fit this cell.
|
||||
let required_height = y + rowspan.get();
|
||||
let required_width = x + colspan.get();
|
||||
if self.rows.len() < required_height {
|
||||
self.rows
|
||||
.resize(required_height, vec![GridCell::Missing; required_width]);
|
||||
}
|
||||
let row = &mut self.rows[y];
|
||||
if row.len() < required_width {
|
||||
row.resize_with(required_width, || GridCell::Missing);
|
||||
}
|
||||
|
||||
// Store references to the cell for all spanned cells.
|
||||
for i in y..y + rowspan.get() {
|
||||
for j in x..x + colspan.get() {
|
||||
self.rows[i][j] = GridCell::Spanned(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
self.rows[y][x] =
|
||||
GridCell::Cell(TableCtxCell { rowspan, colspan, kind, header_scope, nodes });
|
||||
}
|
||||
|
||||
fn build_table(self, mut nodes: Vec<TagNode>) -> Vec<TagNode> {
|
||||
// Table layouting ensures that there are no overlapping cells, and that
|
||||
// any gaps left by the user are filled with empty cells.
|
||||
|
||||
// Only generate row groups such as `THead`, `TFoot`, and `TBody` if
|
||||
// there are no rows with mixed cell kinds.
|
||||
let mut mixed_row_kinds = false;
|
||||
let row_kinds = (self.rows.iter())
|
||||
.map(|row| {
|
||||
row.iter()
|
||||
.filter_map(|cell| match cell {
|
||||
GridCell::Cell(cell) => Some(cell),
|
||||
&GridCell::Spanned(x, y) => self.rows[y][x].as_cell(),
|
||||
GridCell::Missing => None,
|
||||
})
|
||||
.map(|cell| cell.kind)
|
||||
.reduce(|a, b| {
|
||||
if a != b {
|
||||
mixed_row_kinds = true;
|
||||
}
|
||||
a
|
||||
})
|
||||
.expect("tables must have at least one column")
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let Some(mut chunk_kind) = row_kinds.first().copied() else {
|
||||
return nodes;
|
||||
};
|
||||
let mut row_chunk = Vec::new();
|
||||
for (row, row_kind) in self.rows.into_iter().zip(row_kinds) {
|
||||
let row_nodes = row
|
||||
.into_iter()
|
||||
.filter_map(|cell| {
|
||||
let cell = cell.into_cell()?;
|
||||
let span = TableCellSpan {
|
||||
rows: cell.rowspan.get() as i32,
|
||||
cols: cell.colspan.get() as i32,
|
||||
};
|
||||
let tag = match cell.kind {
|
||||
TableCellKind::Header => {
|
||||
let scope = match cell.header_scope {
|
||||
Smart::Custom(scope) => table_header_scope(scope),
|
||||
Smart::Auto => krilla::tagging::TableHeaderScope::Column,
|
||||
};
|
||||
TagKind::TH(TableHeaderCell::new(scope).with_span(span))
|
||||
}
|
||||
TableCellKind::Footer | TableCellKind::Data => {
|
||||
TagKind::TD(TableDataCell::new().with_span(span))
|
||||
}
|
||||
};
|
||||
|
||||
Some(TagNode::Group(tag.into(), cell.nodes))
|
||||
})
|
||||
.collect();
|
||||
|
||||
let row = TagNode::Group(TagKind::TR.into(), row_nodes);
|
||||
|
||||
// Push the `TR` tags directly.
|
||||
if mixed_row_kinds {
|
||||
nodes.push(row);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Generate row groups.
|
||||
if row_kind != chunk_kind {
|
||||
let tag = match chunk_kind {
|
||||
TableCellKind::Header => TagKind::THead,
|
||||
TableCellKind::Footer => TagKind::TFoot,
|
||||
TableCellKind::Data => TagKind::TBody,
|
||||
};
|
||||
nodes.push(TagNode::Group(tag.into(), std::mem::take(&mut row_chunk)));
|
||||
|
||||
chunk_kind = row_kind;
|
||||
}
|
||||
row_chunk.push(row);
|
||||
}
|
||||
|
||||
if !row_chunk.is_empty() {
|
||||
let tag = match chunk_kind {
|
||||
TableCellKind::Header => TagKind::THead,
|
||||
TableCellKind::Footer => TagKind::TFoot,
|
||||
TableCellKind::Data => TagKind::TBody,
|
||||
};
|
||||
nodes.push(TagNode::Group(tag.into(), row_chunk));
|
||||
}
|
||||
|
||||
nodes
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) enum TagNode {
|
||||
Group(Tag, Vec<TagNode>),
|
||||
Leaf(Identifier),
|
||||
/// Allows inserting a placeholder into the tag tree.
|
||||
/// Currently used for [`krilla::page::Page::add_tagged_annotation`].
|
||||
Placeholder(Placeholder),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub(crate) struct LinkId(u32);
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) struct Placeholder(usize);
|
||||
|
||||
impl Tags {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self {
|
||||
stack: Vec::new(),
|
||||
placeholders: Vec::new(),
|
||||
in_artifact: None,
|
||||
|
||||
tree: Vec::new(),
|
||||
link_id: LinkId(0),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn reserve_placeholder(&mut self) -> Placeholder {
|
||||
let idx = self.placeholders.len();
|
||||
self.placeholders.push(OnceCell::new());
|
||||
Placeholder(idx)
|
||||
}
|
||||
|
||||
pub(crate) fn init_placeholder(&mut self, placeholder: Placeholder, node: Node) {
|
||||
self.placeholders[placeholder.0]
|
||||
.set(node)
|
||||
.map_err(|_| ())
|
||||
.expect("placeholder to be uninitialized");
|
||||
}
|
||||
|
||||
pub(crate) fn take_placeholder(&mut self, placeholder: Placeholder) -> Node {
|
||||
self.placeholders[placeholder.0]
|
||||
.take()
|
||||
.expect("initialized placeholder node")
|
||||
}
|
||||
|
||||
/// Returns the current parent's list of children and the structure type ([Tag]).
|
||||
/// In case of the document root the structure type will be `None`.
|
||||
pub(crate) fn parent(&mut self) -> Option<&mut StackEntryKind> {
|
||||
self.stack.last_mut().map(|e| &mut e.kind)
|
||||
}
|
||||
|
||||
pub(crate) fn push(&mut self, node: TagNode) {
|
||||
if let Some(entry) = self.stack.last_mut() {
|
||||
entry.nodes.push(node);
|
||||
} else {
|
||||
self.tree.push(node);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn build_tree(&mut self) -> TagTree {
|
||||
let children = std::mem::take(&mut self.tree)
|
||||
.into_iter()
|
||||
.map(|node| self.resolve_node(node))
|
||||
.collect::<Vec<_>>();
|
||||
TagTree::from(children)
|
||||
}
|
||||
|
||||
/// Resolves [`Placeholder`] nodes.
|
||||
fn resolve_node(&mut self, node: TagNode) -> Node {
|
||||
match node {
|
||||
TagNode::Group(tag, nodes) => {
|
||||
let children = nodes
|
||||
.into_iter()
|
||||
.map(|node| self.resolve_node(node))
|
||||
.collect::<Vec<_>>();
|
||||
Node::Group(TagGroup::with_children(tag, children))
|
||||
}
|
||||
TagNode::Leaf(identifier) => Node::Leaf(identifier),
|
||||
TagNode::Placeholder(placeholder) => self.take_placeholder(placeholder),
|
||||
}
|
||||
}
|
||||
|
||||
fn context_supports(&self, _tag: &StackEntryKind) -> bool {
|
||||
// TODO: generate using: https://pdfa.org/resource/iso-ts-32005-hierarchical-inclusion-rules/
|
||||
true
|
||||
}
|
||||
|
||||
fn next_link_id(&mut self) -> LinkId {
|
||||
self.link_id.0 += 1;
|
||||
self.link_id
|
||||
}
|
||||
}
|
||||
|
||||
/// Automatically calls [`Surface::end_tagged`] when dropped.
|
||||
pub(crate) struct TagHandle<'a, 'b> {
|
||||
surface: &'b mut Surface<'a>,
|
||||
}
|
||||
|
||||
impl Drop for TagHandle<'_, '_> {
|
||||
fn drop(&mut self) {
|
||||
self.surface.end_tagged();
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> TagHandle<'a, '_> {
|
||||
pub(crate) fn surface<'c>(&'c mut self) -> &'c mut Surface<'a> {
|
||||
&mut self.surface
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a [`TagHandle`] that automatically calls [`Surface::end_tagged`]
|
||||
/// when dropped.
|
||||
pub(crate) fn start_marked<'a, 'b>(
|
||||
gc: &mut GlobalContext,
|
||||
surface: &'b mut Surface<'a>,
|
||||
) -> TagHandle<'a, 'b> {
|
||||
start_content(gc, surface, ContentTag::Other)
|
||||
}
|
||||
|
||||
/// Returns a [`TagHandle`] that automatically calls [`Surface::end_tagged`]
|
||||
/// when dropped.
|
||||
pub(crate) fn start_span<'a, 'b>(
|
||||
gc: &mut GlobalContext,
|
||||
surface: &'b mut Surface<'a>,
|
||||
span: SpanTag,
|
||||
) -> TagHandle<'a, 'b> {
|
||||
start_content(gc, surface, ContentTag::Span(span))
|
||||
}
|
||||
|
||||
fn start_content<'a, 'b>(
|
||||
gc: &mut GlobalContext,
|
||||
surface: &'b mut Surface<'a>,
|
||||
content: ContentTag,
|
||||
) -> TagHandle<'a, 'b> {
|
||||
let content = if let Some((_, kind)) = gc.tags.in_artifact {
|
||||
let ty = artifact_type(kind);
|
||||
ContentTag::Artifact(ty)
|
||||
} else if let Some(StackEntryKind::Table(_)) = gc.tags.stack.last().map(|e| &e.kind) {
|
||||
// Mark any direct child of a table as an aritfact. Any real content
|
||||
// will be wrapped inside a `TableCell`.
|
||||
ContentTag::Artifact(ArtifactType::Other)
|
||||
} else {
|
||||
content
|
||||
};
|
||||
let id = surface.start_tagged(content);
|
||||
gc.tags.push(TagNode::Leaf(id));
|
||||
TagHandle { surface }
|
||||
}
|
||||
|
||||
/// Add all annotations that were found in the page frame.
|
||||
pub(crate) fn add_annotations(
|
||||
gc: &mut GlobalContext,
|
||||
page: &mut Page,
|
||||
annotations: Vec<LinkAnnotation>,
|
||||
) {
|
||||
for annotation in annotations.into_iter() {
|
||||
let LinkAnnotation { id: _, placeholder, alt, rect, quad_points, target } =
|
||||
annotation;
|
||||
let annot = krilla::annotation::Annotation::new_link(
|
||||
krilla::annotation::LinkAnnotation::new(rect, Some(quad_points), target),
|
||||
alt,
|
||||
);
|
||||
let annot_id = page.add_tagged_annotation(annot);
|
||||
gc.tags.init_placeholder(placeholder, Node::Leaf(annot_id));
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn handle_start(gc: &mut GlobalContext, elem: &Content) {
|
||||
if gc.tags.in_artifact.is_some() {
|
||||
// Don't nest artifacts
|
||||
return;
|
||||
}
|
||||
|
||||
let loc = elem.location().unwrap();
|
||||
|
||||
if let Some(artifact) = elem.to_packed::<ArtifactElem>() {
|
||||
let kind = artifact.kind(StyleChain::default());
|
||||
start_artifact(gc, loc, kind);
|
||||
return;
|
||||
} else if let Some(_) = elem.to_packed::<RepeatElem>() {
|
||||
start_artifact(gc, loc, ArtifactKind::Other);
|
||||
return;
|
||||
}
|
||||
|
||||
let tag: Tag = if let Some(pdf_tag) = elem.to_packed::<PdfTagElem>() {
|
||||
let kind = pdf_tag.kind(StyleChain::default());
|
||||
match kind {
|
||||
PdfTagKind::Part => TagKind::Part.into(),
|
||||
_ => todo!(),
|
||||
}
|
||||
} else if let Some(heading) = elem.to_packed::<HeadingElem>() {
|
||||
let level = heading.level().try_into().unwrap_or(NonZeroU32::MAX);
|
||||
let name = heading.body.plain_text().to_string();
|
||||
TagKind::Hn(level, Some(name)).into()
|
||||
} else if let Some(_) = elem.to_packed::<OutlineBody>() {
|
||||
push_stack(gc, loc, StackEntryKind::Outline(OutlineCtx::new()));
|
||||
return;
|
||||
} else if let Some(entry) = elem.to_packed::<OutlineEntry>() {
|
||||
push_stack(gc, loc, StackEntryKind::OutlineEntry(entry.clone()));
|
||||
return;
|
||||
} else if let Some(_) = elem.to_packed::<FigureElem>() {
|
||||
let alt = None; // TODO
|
||||
TagKind::Figure.with_alt_text(alt)
|
||||
} else if let Some(image) = elem.to_packed::<ImageElem>() {
|
||||
let alt = image.alt(StyleChain::default()).map(|s| s.to_string());
|
||||
|
||||
let figure_tag = (gc.tags.parent())
|
||||
.and_then(StackEntryKind::as_standard_mut)
|
||||
.filter(|tag| tag.kind == TagKind::Figure);
|
||||
if let Some(figure_tag) = figure_tag {
|
||||
// Set alt text of outer figure tag, if not present.
|
||||
if figure_tag.alt_text.is_none() {
|
||||
figure_tag.alt_text = alt;
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
TagKind::Figure.with_alt_text(alt)
|
||||
}
|
||||
} else if let Some(_) = elem.to_packed::<FigureCaption>() {
|
||||
TagKind::Caption.into()
|
||||
} else if let Some(table) = elem.to_packed::<TableElem>() {
|
||||
push_stack(gc, loc, StackEntryKind::Table(TableCtx::new(table.clone())));
|
||||
return;
|
||||
} else if let Some(cell) = elem.to_packed::<TableCell>() {
|
||||
push_stack(gc, loc, StackEntryKind::TableCell(cell.clone()));
|
||||
return;
|
||||
} else if let Some(link) = elem.to_packed::<LinkMarker>() {
|
||||
let link_id = gc.tags.next_link_id();
|
||||
push_stack(gc, loc, StackEntryKind::Link(link_id, link.clone()));
|
||||
return;
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
|
||||
push_stack(gc, loc, StackEntryKind::Standard(tag));
|
||||
}
|
||||
|
||||
fn push_stack(gc: &mut GlobalContext, loc: Location, kind: StackEntryKind) {
|
||||
if !gc.tags.context_supports(&kind) {
|
||||
// TODO: error or warning?
|
||||
}
|
||||
|
||||
gc.tags.stack.push(StackEntry { loc, kind, nodes: Vec::new() });
|
||||
}
|
||||
|
||||
pub(crate) fn handle_end(gc: &mut GlobalContext, loc: Location) {
|
||||
if let Some((l, _)) = gc.tags.in_artifact {
|
||||
if l == loc {
|
||||
gc.tags.in_artifact = None;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
let Some(entry) = gc.tags.stack.pop_if(|e| e.loc == loc) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let node = match entry.kind {
|
||||
StackEntryKind::Standard(tag) => TagNode::Group(tag, entry.nodes),
|
||||
StackEntryKind::Outline(ctx) => {
|
||||
let nodes = ctx.build_outline(entry.nodes);
|
||||
TagNode::Group(TagKind::TOC.into(), nodes)
|
||||
}
|
||||
StackEntryKind::OutlineEntry(outline_entry) => {
|
||||
let parent = gc.tags.stack.last_mut().expect("outline");
|
||||
let StackEntryKind::Outline(outline_ctx) = &mut parent.kind else {
|
||||
unreachable!("expected outline")
|
||||
};
|
||||
|
||||
outline_ctx.insert(&mut parent.nodes, outline_entry, entry.nodes);
|
||||
|
||||
return;
|
||||
}
|
||||
StackEntryKind::Table(ctx) => {
|
||||
let summary = ctx.table.summary(StyleChain::default()).map(EcoString::into);
|
||||
let nodes = ctx.build_table(entry.nodes);
|
||||
TagNode::Group(TagKind::Table(summary).into(), nodes)
|
||||
}
|
||||
StackEntryKind::TableCell(cell) => {
|
||||
let parent = gc.tags.stack.last_mut().expect("table");
|
||||
let StackEntryKind::Table(table_ctx) = &mut parent.kind else {
|
||||
unreachable!("expected table")
|
||||
};
|
||||
|
||||
table_ctx.insert(cell, entry.nodes);
|
||||
|
||||
return;
|
||||
}
|
||||
StackEntryKind::Link(_, link) => {
|
||||
let alt = link.alt.as_ref().map(EcoString::to_string);
|
||||
let tag = TagKind::Link.with_alt_text(alt);
|
||||
let mut node = TagNode::Group(tag, entry.nodes);
|
||||
// Wrap link in reference tag, if it's not a url.
|
||||
if let Destination::Position(_) | Destination::Location(_) = link.dest {
|
||||
node = TagNode::Group(TagKind::Reference.into(), vec![node]);
|
||||
}
|
||||
node
|
||||
}
|
||||
};
|
||||
|
||||
gc.tags.push(node);
|
||||
}
|
||||
|
||||
fn start_artifact(gc: &mut GlobalContext, loc: Location, kind: ArtifactKind) {
|
||||
gc.tags.in_artifact = Some((loc, kind));
|
||||
}
|
||||
|
||||
fn table_header_scope(scope: TableHeaderScope) -> krilla::tagging::TableHeaderScope {
|
||||
match scope {
|
||||
TableHeaderScope::Both => krilla::tagging::TableHeaderScope::Both,
|
||||
TableHeaderScope::Column => krilla::tagging::TableHeaderScope::Column,
|
||||
TableHeaderScope::Row => krilla::tagging::TableHeaderScope::Row,
|
||||
}
|
||||
}
|
||||
|
||||
fn artifact_type(kind: ArtifactKind) -> ArtifactType {
|
||||
match kind {
|
||||
ArtifactKind::Header => ArtifactType::Header,
|
||||
ArtifactKind::Footer => ArtifactType::Footer,
|
||||
ArtifactKind::Page => ArtifactType::Page,
|
||||
ArtifactKind::Other => ArtifactType::Other,
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ use std::sync::Arc;
|
||||
|
||||
use bytemuck::TransparentWrapper;
|
||||
use krilla::surface::{Location, Surface};
|
||||
use krilla::tagging::SpanTag;
|
||||
use krilla::text::GlyphId;
|
||||
use typst_library::diag::{bail, SourceResult};
|
||||
use typst_library::layout::Size;
|
||||
@ -11,8 +12,8 @@ use typst_library::visualize::FillRule;
|
||||
use typst_syntax::Span;
|
||||
|
||||
use crate::convert::{FrameContext, GlobalContext};
|
||||
use crate::paint;
|
||||
use crate::util::{display_font, AbsExt, TransformExt};
|
||||
use crate::{paint, tags};
|
||||
|
||||
#[typst_macros::time(name = "handle text")]
|
||||
pub(crate) fn handle_text(
|
||||
@ -23,6 +24,10 @@ pub(crate) fn handle_text(
|
||||
) -> SourceResult<()> {
|
||||
*gc.languages.entry(t.lang).or_insert(0) += t.glyphs.len();
|
||||
|
||||
let mut handle =
|
||||
tags::start_span(gc, surface, SpanTag::empty().with_actual_text(Some(&t.text)));
|
||||
let surface = handle.surface();
|
||||
|
||||
let font = convert_font(gc, t.font.clone())?;
|
||||
let fill = paint::convert_fill(
|
||||
gc,
|
||||
|
@ -18,7 +18,7 @@ use typst_library::foundations::{
|
||||
SequenceElem, Show, ShowSet, Style, StyleChain, StyledElem, Styles, SymbolElem,
|
||||
Synthesize, Transformation,
|
||||
};
|
||||
use typst_library::html::{tag, HtmlElem};
|
||||
use typst_library::html::{tag, FrameElem, HtmlElem};
|
||||
use typst_library::introspection::{Locatable, SplitLocator, Tag, TagElem};
|
||||
use typst_library::layout::{
|
||||
AlignElem, BoxElem, HElem, InlineElem, PageElem, PagebreakElem, VElem,
|
||||
@ -237,9 +237,9 @@ fn visit<'a>(
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Transformations for math content based on the realization kind. Needs
|
||||
// Transformations for content based on the realization kind. Needs
|
||||
// to happen before show rules.
|
||||
if visit_math_rules(s, content, styles)? {
|
||||
if visit_kind_rules(s, content, styles)? {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@ -280,9 +280,8 @@ fn visit<'a>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Handles special cases for math in normal content and nested equations in
|
||||
// math.
|
||||
fn visit_math_rules<'a>(
|
||||
// Handles transformations based on the realization kind.
|
||||
fn visit_kind_rules<'a>(
|
||||
s: &mut State<'a, '_, '_, '_>,
|
||||
content: &'a Content,
|
||||
styles: StyleChain<'a>,
|
||||
@ -335,6 +334,13 @@ fn visit_math_rules<'a>(
|
||||
}
|
||||
}
|
||||
|
||||
if !s.kind.is_html() {
|
||||
if let Some(elem) = content.to_packed::<FrameElem>() {
|
||||
visit(s, &elem.body, styles)?;
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
|
@ -167,7 +167,7 @@ fn render_frame(canvas: &mut sk::Pixmap, state: State, frame: &Frame) {
|
||||
FrameItem::Image(image, size, _) => {
|
||||
image::render_image(canvas, state.pre_translate(*pos), image, *size);
|
||||
}
|
||||
FrameItem::Link(_, _) => {}
|
||||
FrameItem::Link(..) => {}
|
||||
FrameItem::Tag(_) => {}
|
||||
}
|
||||
}
|
||||
|
@ -207,7 +207,7 @@ impl SVGRenderer {
|
||||
for (pos, item) in frame.items() {
|
||||
// File size optimization.
|
||||
// TODO: SVGs could contain links, couldn't they?
|
||||
if matches!(item, FrameItem::Link(_, _) | FrameItem::Tag(_)) {
|
||||
if matches!(item, FrameItem::Link(..) | FrameItem::Tag(_)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -228,7 +228,7 @@ impl SVGRenderer {
|
||||
self.render_shape(state.pre_translate(*pos), shape)
|
||||
}
|
||||
FrameItem::Image(image, size, _) => self.render_image(image, size),
|
||||
FrameItem::Link(_, _) => unreachable!(),
|
||||
FrameItem::Link(..) => unreachable!(),
|
||||
FrameItem::Tag(_) => unreachable!(),
|
||||
};
|
||||
|
||||
|
@ -724,6 +724,8 @@ node! {
|
||||
|
||||
impl<'a> Ref<'a> {
|
||||
/// Get the target.
|
||||
///
|
||||
/// Will not be empty.
|
||||
pub fn target(self) -> &'a str {
|
||||
self.0
|
||||
.children()
|
||||
|
@ -185,7 +185,7 @@ impl Lexer<'_> {
|
||||
'h' if self.s.eat_if("ttp://") => self.link(),
|
||||
'h' if self.s.eat_if("ttps://") => self.link(),
|
||||
'<' if self.s.at(is_id_continue) => self.label(),
|
||||
'@' => self.ref_marker(),
|
||||
'@' if self.s.at(is_id_continue) => self.ref_marker(),
|
||||
|
||||
'.' if self.s.eat_if("..") => SyntaxKind::Shorthand,
|
||||
'-' if self.s.eat_if("--") => SyntaxKind::Shorthand,
|
||||
|
@ -256,8 +256,8 @@ In Typst, the same function can be used both to affect the appearance for the
|
||||
remainder of the document, a block (or scope), or just its arguments. For
|
||||
example, `[#text(weight: "bold")[bold text]]` will only embolden its argument,
|
||||
while `[#set text(weight: "bold")]` will embolden any text until the end of the
|
||||
current block, or, if there is none, document. The effects of a function are
|
||||
immediately obvious based on whether it is used in a call or a
|
||||
current block, or the end of the document, if there is none. The effects of a
|
||||
function are immediately obvious based on whether it is used in a call or a
|
||||
[set rule.]($styling/#set-rules)
|
||||
|
||||
```example
|
||||
|
@ -206,7 +206,6 @@ label exists on the current page:
|
||||
```typ
|
||||
>>> #set page("a5", margin: (x: 2.5cm, y: 3cm))
|
||||
#set page(header: context {
|
||||
let page-counter =
|
||||
let matches = query(<big-table>)
|
||||
let current = counter(page).get()
|
||||
let has-table = matches.any(m =>
|
||||
@ -218,7 +217,7 @@ label exists on the current page:
|
||||
#h(1fr)
|
||||
National Academy of Sciences
|
||||
]
|
||||
}))
|
||||
})
|
||||
|
||||
#lorem(100)
|
||||
#pagebreak()
|
||||
|
@ -181,11 +181,7 @@
|
||||
[`sys.version`]($category/foundations/sys) can also be very useful.
|
||||
|
||||
```typ
|
||||
#let tiling = if "tiling" in dictionary(std) {
|
||||
tiling
|
||||
} else {
|
||||
pattern
|
||||
}
|
||||
#let tiling = if "tiling" in std { tiling } else { pattern }
|
||||
|
||||
...
|
||||
```
|
||||
|
@ -720,18 +720,12 @@ fn symbols_model(resolver: &dyn Resolver, group: &GroupData) -> SymbolsModel {
|
||||
}
|
||||
};
|
||||
|
||||
for (variant, c) in symbol.variants() {
|
||||
for (variant, c, deprecation) in symbol.variants() {
|
||||
let shorthand = |list: &[(&'static str, char)]| {
|
||||
list.iter().copied().find(|&(_, x)| x == c).map(|(s, _)| s)
|
||||
};
|
||||
|
||||
let name = complete(variant);
|
||||
let deprecation = match name.as_str() {
|
||||
"integral.sect" => {
|
||||
Some("`integral.sect` is deprecated, use `integral.inter` instead")
|
||||
}
|
||||
_ => binding.deprecation(),
|
||||
};
|
||||
|
||||
list.push(SymbolModel {
|
||||
name,
|
||||
@ -742,10 +736,10 @@ fn symbols_model(resolver: &dyn Resolver, group: &GroupData) -> SymbolsModel {
|
||||
accent: typst::math::Accent::combine(c).is_some(),
|
||||
alternates: symbol
|
||||
.variants()
|
||||
.filter(|(other, _)| other != &variant)
|
||||
.map(|(other, _)| complete(other))
|
||||
.filter(|(other, _, _)| other != &variant)
|
||||
.map(|(other, _, _)| complete(other))
|
||||
.collect(),
|
||||
deprecation,
|
||||
deprecation: deprecation.or_else(|| binding.deprecation()),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
BIN
tests/ref/html-frame-in-layout.png
Normal file
BIN
tests/ref/html-frame-in-layout.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 146 B |
11
tests/ref/html/html-deco.html
Normal file
11
tests/ref/html/html-deco.html
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
</head>
|
||||
<body>
|
||||
<p><s>Struck</s> <mark>Highlighted</mark> <span style="text-decoration: underline">Underlined</span> <span style="text-decoration: overline">Overlined</span></p>
|
||||
<p><span style="text-decoration: overline"><span style="text-decoration: underline"><mark><s>Mixed</s></mark></span></span></p>
|
||||
</body>
|
||||
</html>
|
BIN
tests/ref/issue-5775-cite-order-rtl.png
Normal file
BIN
tests/ref/issue-5775-cite-order-rtl.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
BIN
tests/ref/issue-6162-coincident-gradient-stops-export-png.png
Normal file
BIN
tests/ref/issue-6162-coincident-gradient-stops-export-png.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 522 B |
BIN
tests/ref/ref-to-empty-label-not-possible.png
Normal file
BIN
tests/ref/ref-to-empty-label-not-possible.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 182 B |
@ -92,3 +92,7 @@ _Visible_
|
||||
--- label-non-existent-error ---
|
||||
// Error: 5-10 sequence does not have field "label"
|
||||
#[].label
|
||||
|
||||
--- label-empty ---
|
||||
// Error: 23-32 label name must not be empty
|
||||
= Something to label #label("")
|
||||
|
8
tests/suite/html/frame.typ
Normal file
8
tests/suite/html/frame.typ
Normal file
@ -0,0 +1,8 @@
|
||||
// No proper HTML tests here yet because we don't want to test SVG export just
|
||||
// yet. We'll definitely add tests at some point.
|
||||
|
||||
--- html-frame-in-layout ---
|
||||
// Ensure that HTML frames are transparent in layout. This is less important for
|
||||
// actual paged export than for _nested_ HTML frames, which take the same code
|
||||
// path.
|
||||
#html.frame[A]
|
@ -34,7 +34,7 @@ To the right! Where the sunlight peeks behind the mountain.
|
||||
#align(start)[Start]
|
||||
#align(end)[Ende]
|
||||
|
||||
#set text(lang: "ar")
|
||||
#set text(lang: "ar", font: "Noto Sans Arabic")
|
||||
#align(start)[يبدأ]
|
||||
#align(end)[نهاية]
|
||||
|
||||
|
@ -45,6 +45,7 @@ Lריווח #h(1cm) R
|
||||
|
||||
--- bidi-whitespace-reset ---
|
||||
// Test whether L1 whitespace resetting destroys stuff.
|
||||
#set text(font: ("Libertinus Serif", "Noto Sans Arabic"))
|
||||
الغالب #h(70pt) ن#" "ة
|
||||
|
||||
--- bidi-explicit-dir ---
|
||||
@ -87,7 +88,7 @@ Lריווח #h(1cm) R
|
||||
columns: (1fr, 1fr),
|
||||
lines(6),
|
||||
[
|
||||
#text(lang: "ar")[مجرد نص مؤقت لأغراض العرض التوضيحي. ]
|
||||
#text(lang: "ar", font: ("Libertinus Serif", "Noto Sans Arabic"))[مجرد نص مؤقت لأغراض العرض التوضيحي. ]
|
||||
#text(lang: "ar")[سلام]
|
||||
],
|
||||
)
|
||||
|
@ -29,6 +29,7 @@ ABCअपार्टमेंट
|
||||
\ ט
|
||||
|
||||
--- shaping-font-fallback ---
|
||||
#set text(font: ("Libertinus Serif", "Noto Sans Arabic"))
|
||||
// Font fallback for emoji.
|
||||
A😀B
|
||||
|
||||
|
@ -80,7 +80,7 @@ I'm in#text(tracking: 0.15em + 1.5pt)[ spaace]!
|
||||
|
||||
--- text-tracking-arabic ---
|
||||
// Test tracking in arabic text (makes no sense whatsoever)
|
||||
#set text(tracking: 0.3em)
|
||||
#set text(tracking: 0.3em, font: "Noto Sans Arabic")
|
||||
النص
|
||||
|
||||
--- text-spacing ---
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
--- repeat-dots-rtl ---
|
||||
// Test dots with RTL.
|
||||
#set text(lang: "ar")
|
||||
#set text(lang: "ar", font: ("Libertinus Serif", "Noto Sans Arabic"))
|
||||
مقدمة #box(width: 1fr, repeat[.]) 15
|
||||
|
||||
--- repeat-empty ---
|
||||
@ -35,7 +35,7 @@ A#box(width: 1fr, repeat(rect(width: 6em, height: 0.7em)))B
|
||||
#set align(center)
|
||||
A#box(width: 1fr, repeat(rect(width: 6em, height: 0.7em)))B
|
||||
|
||||
#set text(dir: rtl)
|
||||
#set text(dir: rtl, font: "Noto Sans Arabic")
|
||||
ريجين#box(width: 1fr, repeat(rect(width: 4em, height: 0.7em)))سون
|
||||
|
||||
--- repeat-unrestricted ---
|
||||
|
@ -121,8 +121,8 @@ $a scripts(=)^"def" b quad a scripts(lt.eq)_"really" b quad a scripts(arrow.r.lo
|
||||
|
||||
--- math-attach-integral ---
|
||||
// Test default of scripts attachments on integrals at display size.
|
||||
$ integral.sect_a^b quad \u{2a1b}_a^b quad limits(\u{2a1b})_a^b $
|
||||
$integral.sect_a^b quad \u{2a1b}_a^b quad limits(\u{2a1b})_a^b$
|
||||
$ integral.inter_a^b quad \u{2a1b}_a^b quad limits(\u{2a1b})_a^b $
|
||||
$integral.inter_a^b quad \u{2a1b}_a^b quad limits(\u{2a1b})_a^b$
|
||||
|
||||
--- math-attach-large-operator ---
|
||||
// Test default of limit attachments on large operators at display size only.
|
||||
|
@ -75,6 +75,14 @@ Now we have multiple bibliographies containing @glacier-melt @keshav2007read
|
||||
// Error: 2-62 CSL style "Alphanumeric" is not suitable for bibliographies
|
||||
#bibliography("/assets/bib/works.bib", style: "alphanumeric")
|
||||
|
||||
--- bibliography-empty-key ---
|
||||
#let src = ```yaml
|
||||
"":
|
||||
type: Book
|
||||
```
|
||||
// Error: 15-30 bibliography contains entry with empty key
|
||||
#bibliography(bytes(src.text))
|
||||
|
||||
--- issue-4618-bibliography-set-heading-level ---
|
||||
// Test that the bibliography block's heading is set to 2 by the show rule,
|
||||
// and therefore should be rendered like a level-2 heading. Notably, this
|
||||
|
@ -147,3 +147,16 @@ B #cite(<netwok>) #cite(<arrgh>).
|
||||
// Error: 7-17 expected label, found string
|
||||
// Hint: 7-17 use `label("%@&#*!\\")` to create a label
|
||||
#cite("%@&#*!\\")
|
||||
|
||||
--- issue-5775-cite-order-rtl ---
|
||||
// Test citation order in RTL text.
|
||||
#set page(width: 300pt)
|
||||
#set text(font: ("Libertinus Serif", "Noto Sans Arabic"))
|
||||
@netwok
|
||||
aaa
|
||||
این است
|
||||
@tolkien54
|
||||
و این یکی هست
|
||||
@arrgh
|
||||
|
||||
#bibliography("/assets/bib/works.bib")
|
||||
|
@ -231,7 +231,7 @@ Welcome \ here. Does this work well?
|
||||
|
||||
--- par-hanging-indent-rtl ---
|
||||
#set par(hanging-indent: 2em)
|
||||
#set text(dir: rtl)
|
||||
#set text(dir: rtl, font: ("Libertinus Serif", "Noto Sans Arabic"))
|
||||
لآن وقد أظلم الليل وبدأت النجوم
|
||||
تنضخ وجه الطبيعة التي أعْيَتْ من طول ما انبعثت في النهار
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
--- quote-dir-author-pos ---
|
||||
// Text direction affects author positioning
|
||||
#set text(font: ("Libertinus Serif", "Noto Sans Arabic"))
|
||||
And I quote: #quote(attribution: [René Descartes])[cogito, ergo sum].
|
||||
|
||||
#set text(lang: "ar")
|
||||
@ -9,6 +10,7 @@ And I quote: #quote(attribution: [René Descartes])[cogito, ergo sum].
|
||||
|
||||
--- quote-dir-align ---
|
||||
// Text direction affects block alignment
|
||||
#set text(font: ("Libertinus Serif", "Noto Sans Arabic"))
|
||||
#set quote(block: true)
|
||||
#quote(attribution: [René Descartes])[cogito, ergo sum]
|
||||
|
||||
|
@ -86,3 +86,14 @@ Text seen on #ref(<text>, form: "page", supplement: "Page").
|
||||
// Test reference with non-whitespace before it.
|
||||
#figure[] <1>
|
||||
#test([(#ref(<1>))], [(@1)])
|
||||
|
||||
--- ref-to-empty-label-not-possible ---
|
||||
// @ without any following label should just produce the symbol in the output
|
||||
// and not produce a reference to a label with an empty name.
|
||||
@
|
||||
|
||||
--- ref-function-empty-label ---
|
||||
// using ref() should also not be possible
|
||||
// Error: 6-7 unexpected less-than operator
|
||||
// Error: 7-8 unexpected greater-than operator
|
||||
#ref(<>)
|
||||
|
@ -264,6 +264,8 @@
|
||||
#test("Hey" not in "abheyCd", true)
|
||||
#test("a" not
|
||||
/* fun comment? */ in "abc", false)
|
||||
#test("sys" in std, true)
|
||||
#test("system" in std, false)
|
||||
|
||||
--- ops-not-trailing ---
|
||||
// Error: 10 expected keyword `in`
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
--- numbers ---
|
||||
// Test numbers in text mode.
|
||||
#set text(font: ("Libertinus Serif", "Noto Sans Arabic"))
|
||||
12 \
|
||||
12.0 \
|
||||
3.14 \
|
||||
|
@ -83,3 +83,11 @@ We can also specify a customized value
|
||||
#highlight(stroke: 2pt + blue)[abc]
|
||||
#highlight(stroke: (top: blue, left: red, bottom: green, right: orange))[abc]
|
||||
#highlight(stroke: 1pt, radius: 3pt)[#lorem(5)]
|
||||
|
||||
--- html-deco html ---
|
||||
#strike[Struck]
|
||||
#highlight[Highlighted]
|
||||
#underline[Underlined]
|
||||
#overline[Overlined]
|
||||
|
||||
#(strike, highlight, underline, overline).fold([Mixed], (it, f) => f(it))
|
||||
|
@ -666,3 +666,29 @@ $ A = mat(
|
||||
#let _ = gradient.linear(..my-gradient.stops())
|
||||
#let my-gradient2 = gradient.linear(red, blue).repeat(5, mirror: true)
|
||||
#let _ = gradient.linear(..my-gradient2.stops())
|
||||
|
||||
--- issue-6162-coincident-gradient-stops-export-png ---
|
||||
// Ensure that multiple gradient stops with the same position
|
||||
// don't cause a panic.
|
||||
#rect(
|
||||
fill: gradient.linear(
|
||||
(red, 0%),
|
||||
(green, 0%),
|
||||
(blue, 100%),
|
||||
)
|
||||
)
|
||||
#rect(
|
||||
fill: gradient.linear(
|
||||
(red, 0%),
|
||||
(green, 100%),
|
||||
(blue, 100%),
|
||||
)
|
||||
)
|
||||
#rect(
|
||||
fill: gradient.linear(
|
||||
(white, 0%),
|
||||
(red, 50%),
|
||||
(green, 50%),
|
||||
(blue, 100%),
|
||||
)
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user