Compare commits

..

No commits in common. "9649def1081720d351def471cceb735fb594ea9e" and "99815f449ca712508b46d9c3eeef783c85337b05" have entirely different histories.

9 changed files with 34 additions and 94 deletions

4
Cargo.lock generated
View File

@ -1373,7 +1373,7 @@ dependencies = [
[[package]]
name = "krilla"
version = "0.4.0"
source = "git+https://github.com/LaurenzV/krilla?branch=main#32d070e737cd8ae4c3aa4ff901d15cb22bd052f3"
source = "git+https://github.com/LaurenzV/krilla?branch=main#d40f81a01ca8f8654510a76effeef12518437800"
dependencies = [
"base64",
"bumpalo",
@ -1402,7 +1402,7 @@ dependencies = [
[[package]]
name = "krilla-svg"
version = "0.1.0"
source = "git+https://github.com/LaurenzV/krilla?branch=main#32d070e737cd8ae4c3aa4ff901d15cb22bd052f3"
source = "git+https://github.com/LaurenzV/krilla?branch=main#d40f81a01ca8f8654510a76effeef12518437800"
dependencies = [
"flate2",
"fontdb",

View File

@ -23,10 +23,10 @@ use serde::{Serialize, Serializer};
use typst_syntax::Span;
use typst_utils::singleton;
use crate::diag::{bail, SourceResult, StrResult};
use crate::diag::{SourceResult, StrResult};
use crate::engine::Engine;
use crate::foundations::{
func, repr, scope, ty, Args, Context, Dict, IntoValue, Label, Property, Recipe,
func, repr, scope, ty, Context, Dict, IntoValue, Label, Property, Recipe,
RecipeIndex, Repr, Selector, Str, Style, StyleChain, Styles, Value,
};
use crate::introspection::{Locatable, Location};
@ -479,7 +479,7 @@ impl Content {
/// Link the content somewhere.
pub fn linked(self, dest: Destination, alt: Option<EcoString>) -> Self {
let span = self.span();
LinkMarker::new(self, dest.clone(), alt, span)
LinkMarker::new(self, dest.clone(), alt)
.pack()
.spanned(span)
.set(LinkElem::current, Some(dest))
@ -785,27 +785,15 @@ impl Repr for StyledElem {
}
/// An element that associates the body of a link with the destination.
#[elem(Locatable, Construct)]
#[elem(Locatable)]
pub struct LinkMarker {
/// The content.
#[internal]
#[required]
pub body: Content,
#[internal]
#[required]
pub dest: Destination,
#[internal]
#[required]
pub alt: Option<EcoString>,
#[internal]
#[required]
pub span: Span,
}
impl Construct for LinkMarker {
fn construct(_: &mut Engine, args: &mut Args) -> SourceResult<Content> {
bail!(args.span, "cannot be constructed manually");
}
}
impl<T: NativeElement> IntoValue for T {

View File

@ -1,7 +1,6 @@
use std::num::NonZeroUsize;
use codex::styling::MathVariant;
use ecow::EcoString;
use typst_utils::NonZeroExt;
use unicode_math_class::MathClass;
@ -48,9 +47,6 @@ use crate::text::{FontFamily, FontList, FontWeight, LocalName, TextElem};
/// [main math page]($category/math).
#[elem(Locatable, Synthesize, ShowSet, Count, LocalName, Refable, Outlinable)]
pub struct EquationElem {
/// An alternative description of the mathematical equation.
pub alt: Option<EcoString>,
/// Whether the equation is displayed as a separate block.
#[default(false)]
pub block: bool,

View File

@ -88,7 +88,7 @@ use crate::text::{LocalName, TextElem};
/// generated.
#[elem(Locatable)]
pub struct LinkElem {
/// An alternative description of the link.
/// A text describing the link.
pub alt: Option<EcoString>,
/// The destination the link points to.

View File

@ -124,7 +124,7 @@ pub struct ImageElem {
/// The height of the image.
pub height: Sizing,
/// An alternative description of the image.
/// A text describing the image.
pub alt: Option<EcoString>,
/// How the image should adjust itself to a given area (the area is defined

View File

@ -9,7 +9,6 @@ use krilla::error::KrillaError;
use krilla::geom::PathBuilder;
use krilla::page::{PageLabel, PageSettings};
use krilla::surface::Surface;
use krilla::tagging::TagId;
use krilla::{Document, SerializeSettings};
use krilla_svg::render_svg_glyph;
use typst_library::diag::{bail, error, SourceDiagnostic, SourceResult};
@ -374,21 +373,11 @@ fn finish(
.collect::<EcoVec<_>>();
Err(errors)
}
KrillaError::DuplicateTagId(id, loc) => {
let span = to_span(loc);
let id = display_tag_id(&id);
bail!(
span, "duplicate tag id `{id}`";
hint: "please report this as a bug"
)
KrillaError::DuplicateTagId(_, _) => {
unreachable!("duplicate IDs shouldn't be generated")
}
KrillaError::UnknownTagId(id, loc) => {
let span = to_span(loc);
let id = display_tag_id(&id);
bail!(
span, "unknown tag id `{id}`";
hint: "please report this as a bug"
)
KrillaError::UnknownTagId(_, _) => {
unreachable!("all referenced IDs should be present in the tag tree")
}
KrillaError::Image(_, loc) => {
let span = to_span(loc);
@ -405,20 +394,6 @@ fn finish(
}
}
fn display_tag_id(id: &TagId) -> impl std::fmt::Display + use<'_> {
typst_utils::display(|f| {
if let Ok(str) = std::str::from_utf8(id.as_bytes()) {
f.write_str(str)
} else {
f.write_str("0x")?;
for b in id.as_bytes() {
write!(f, "{b:x}")?;
}
Ok(())
}
})
}
/// Converts a krilla error into a Typst error.
fn convert_error(
gc: &GlobalContext,
@ -587,20 +562,16 @@ fn convert_error(
}
// The below errors cannot occur yet, only once Typst supports full PDF/A
// and PDF/UA. But let's still add a message just to be on the safe side.
ValidationError::MissingAnnotationAltText(loc) => {
let span = to_span(*loc);
error!(
span, "{prefix} missing annotation alt text";
hint: "please report this as a bug"
)
}
ValidationError::MissingAltText(loc) => {
let span = to_span(*loc);
error!(
span, "{prefix} missing alt text";
hint: "make sure your images and equations have alt text"
)
}
ValidationError::MissingAnnotationAltText => error!(
Span::detached(),
"{prefix} missing annotation alt text";
hint: "please report this as a bug"
),
ValidationError::MissingAltText => error!(
Span::detached(),
"{prefix} missing alt text";
hint: "make sure your images and equations have alt text"
),
ValidationError::NoDocumentLanguage => error!(
Span::detached(),
"{prefix} missing document language";

View File

@ -6,7 +6,6 @@ use krilla::destination::XyzDestination;
use krilla::geom as kg;
use typst_library::layout::{Point, Position, Size};
use typst_library::model::Destination;
use typst_syntax::Span;
use crate::convert::{FrameContext, GlobalContext};
use crate::tags::{self, Placeholder, TagNode};
@ -18,7 +17,6 @@ pub(crate) struct LinkAnnotation {
pub(crate) alt: Option<String>,
pub(crate) quad_points: Vec<kg::Quadrilateral>,
pub(crate) target: Target,
pub(crate) span: Span,
}
pub(crate) fn handle_link(
@ -72,7 +70,6 @@ pub(crate) fn handle_link(
quad_points: vec![quad],
alt,
target,
span: link.span,
});
}
}

View File

@ -123,9 +123,9 @@ pub(crate) fn handle_start(
} else {
TagKind::Figure.with_alt_text(alt)
}
} else if let Some(equation) = elem.to_packed::<EquationElem>() {
let alt = equation.alt.get_as_ref().map(|s| s.to_string());
TagKind::Formula.with_alt_text(alt)
} else if let Some(_) = elem.to_packed::<EquationElem>() {
// TODO: alt text
TagKind::Formula.into()
} else if let Some(table) = elem.to_packed::<TableElem>() {
let table_id = gc.tags.next_table_id();
let summary = table.summary.get_as_ref().map(|s| s.to_string());
@ -176,7 +176,6 @@ pub(crate) fn handle_start(
return Ok(());
};
let tag = tag.with_location(Some(elem.span().into_raw().get()));
push_stack(gc, loc, StackEntryKind::Standard(tag))?;
Ok(())
@ -203,8 +202,7 @@ pub(crate) fn handle_end(gc: &mut GlobalContext, surface: &mut Surface, loc: Loc
// PDF/UA compliance of the structure hierarchy is checked
// elsewhere. While this doesn't make a lot of sense, just
// avoid crashing here.
let tag = TagKind::TOCI
.with_location(Some(outline_entry.span().into_raw().get()));
let tag = TagKind::TOCI.into();
gc.tags.push(TagNode::Group(tag, entry.nodes));
return;
};
@ -218,8 +216,7 @@ pub(crate) fn handle_end(gc: &mut GlobalContext, surface: &mut Surface, loc: Loc
// PDF/UA compliance of the structure hierarchy is checked
// elsewhere. While this doesn't make a lot of sense, just
// avoid crashing here.
let tag = TagKind::TD(TableDataCell::new())
.with_location(Some(cell.span().into_raw().get()));
let tag = TagKind::TD(TableDataCell::new()).into();
gc.tags.push(TagNode::Group(tag, entry.nodes));
return;
};
@ -327,13 +324,11 @@ pub(crate) fn add_annotations(
annotations: Vec<LinkAnnotation>,
) {
for annotation in annotations.into_iter() {
let LinkAnnotation { id: _, placeholder, alt, quad_points, target, span } =
annotation;
let LinkAnnotation { id: _, placeholder, alt, quad_points, target } = annotation;
let annot = krilla::annotation::Annotation::new_link(
krilla::annotation::LinkAnnotation::new_with_quad_points(quad_points, target),
alt,
)
.with_location(Some(span.into_raw().get()));
);
let annot_id = page.add_tagged_annotation(annot);
gc.tags.placeholders.init(placeholder, Node::Leaf(annot_id));
}

View File

@ -9,7 +9,6 @@ use smallvec::SmallVec;
use typst_library::foundations::{Packed, Smart, StyleChain};
use typst_library::model::TableCell;
use typst_library::pdf::{TableCellKind, TableHeaderScope};
use typst_syntax::Span;
use crate::tags::{TableId, TagNode};
@ -58,7 +57,7 @@ impl TableCtx {
}
}
pub(crate) fn insert(&mut self, cell: &Packed<TableCell>, nodes: Vec<TagNode>) {
pub(crate) fn insert(&mut self, cell: &TableCell, nodes: Vec<TagNode>) {
let x = cell.x.get(StyleChain::default()).unwrap_or_else(|| unreachable!());
let y = cell.y.get(StyleChain::default()).unwrap_or_else(|| unreachable!());
let rowspan = cell.rowspan.get(StyleChain::default());
@ -93,7 +92,6 @@ impl TableCtx {
kind,
headers: SmallVec::new(),
nodes,
span: cell.span(),
});
}
@ -177,14 +175,13 @@ impl TableCtx {
.with_headers(cell.headers),
)
.with_id(Some(id))
.with_location(Some(cell.span.into_raw().get()))
}
TableCellKind::Footer | TableCellKind::Data => TagKind::TD(
TableDataCell::new()
.with_span(span)
.with_headers(cell.headers),
)
.with_location(Some(cell.span.into_raw().get())),
.into(),
};
Some(TagNode::Group(tag, cell.nodes))
})
@ -299,7 +296,6 @@ struct TableCtxCell {
kind: Smart<TableCellKind>,
headers: SmallVec<[TagId; 1]>,
nodes: Vec<TagNode>,
span: Span,
}
impl TableCtxCell {
@ -348,7 +344,7 @@ mod tests {
fn table<const SIZE: usize>(cells: [TableCell; SIZE]) -> TableCtx {
let mut table = TableCtx::new(TableId(324), Some("summary".into()));
for cell in cells {
table.insert(&Packed::new(cell), Vec::new());
table.insert(&cell, Vec::new());
}
table
}
@ -420,9 +416,7 @@ mod tests {
let id = table_cell_id(TableId(324), x, y);
let ids = headers.map(|(x, y)| table_cell_id(TableId(324), x, y));
TagNode::Group(
TagKind::TH(TableHeaderCell::new(scope).with_headers(ids))
.with_id(Some(id))
.with_location(Some(Span::detached().into_raw().get())),
TagKind::TH(TableHeaderCell::new(scope).with_headers(ids)).with_id(Some(id)),
Vec::new(),
)
}
@ -430,8 +424,7 @@ mod tests {
fn td<const SIZE: usize>(headers: [(u32, u32); SIZE]) -> TagNode {
let ids = headers.map(|(x, y)| table_cell_id(TableId(324), x, y));
TagNode::Group(
TagKind::TD(TableDataCell::new().with_headers(ids))
.with_location(Some(Span::detached().into_raw().get())),
TagKind::TD(TableDataCell::new().with_headers(ids)).into(),
Vec::new(),
)
}