Compare commits

...

38 Commits

Author SHA1 Message Date
Tobias Schmitz
3d4d548934
feat: [WIP] generate alt text for ref elements 2025-07-03 18:43:30 +02:00
Tobias Schmitz
254aadccfc
docs: fix comment 2025-07-03 18:43:20 +02:00
Tobias Schmitz
8e10356234
refactor: use krilla as git dependency 2025-07-03 18:43:20 +02:00
Tobias Schmitz
7892a8c726
chore: update krilla 2025-07-03 18:43:20 +02:00
Tobias Schmitz
f324accff9
feat: generate paragraphs 2025-07-03 18:43:20 +02:00
Tobias Schmitz
0bc39338a1
fix: handle some edge cases instead of panicking 2025-07-03 18:43:20 +02:00
Tobias Schmitz
377dc87325
refactor: split up pdf tagging code into multiple modules 2025-07-03 18:43:20 +02:00
Tobias Schmitz
50cd81ee1f
feat: generate headers attribute table cells
- fix marking repeated headers/footers as artifacts
- fix table row grouping with empty cells
2025-07-03 18:43:17 +02:00
Tobias Schmitz
746926c7da
fix: ignore repeated table headers/footers in tag tree 2025-07-03 18:43:15 +02:00
Tobias Schmitz
773efb5572
fix: bug due to table cell start tags in grid layout code 2025-07-03 18:43:13 +02:00
Tobias Schmitz
3404fecd36
feat: tag table headers and footers 2025-07-03 18:43:10 +02:00
Tobias Schmitz
bfcf2bd4cc
feat: support headings with level >= 7 2025-07-03 18:43:08 +02:00
Tobias Schmitz
605681d435
refactor: move link tagging code 2025-07-03 18:43:04 +02:00
Tobias Schmitz
6ebe85d678
fix: don't include outline title in TOC hierarchy 2025-07-03 18:43:02 +02:00
Tobias Schmitz
76d09b5673
fix: only use link annotation quadpoints when exporting a PDF/UA-1 document 2025-07-03 18:43:02 +02:00
Tobias Schmitz
d6307831dd
feat: hierarchical outline tags 2025-07-03 18:42:59 +02:00
Tobias Schmitz
09b2cd6de5
docs: fixup some comments 2025-07-03 18:42:57 +02:00
Tobias Schmitz
6717a18414
feat: mark RepeatElem as artifact 2025-07-03 18:42:54 +02:00
Tobias Schmitz
612aa8fc53
fix: mark table gutter and fill as artifacts 2025-07-03 18:42:52 +02:00
Tobias Schmitz
5bd9accb9c
feat: always write alt text in marked content sequence for images 2025-07-03 18:42:52 +02:00
Tobias Schmitz
0d35ae28ad
feat: add cli args for PDF/UA-1 standard and to disable tagging 2025-07-03 18:42:46 +02:00
Tobias Schmitz
4894a227d2
refactor: revert some changes to FrameItem::Link 2025-07-03 18:42:46 +02:00
Tobias Schmitz
2d6e3b6151
refactor: derive(Cast) for ArtifactKind 2025-07-03 18:42:44 +02:00
Tobias Schmitz
e6341c0fe4
fix: avoid empty marked-content sequences 2025-07-03 18:42:41 +02:00
Tobias Schmitz
8231439b11
feat: generate tags for tables 2025-07-03 18:42:38 +02:00
Tobias Schmitz
8075f551e2
feat: use local krilla version 2025-07-03 18:42:35 +02:00
Tobias Schmitz
ac6b9d6008
feat: pdf.tag function to manually create pdf tags 2025-07-03 18:42:31 +02:00
Tobias Schmitz
00c3b62f1d
feat: write tags for more elements 2025-07-03 18:42:23 +02:00
Tobias Schmitz
6c686bd460
feat: write tags for links and use quadpoints in link annotations 2025-07-03 18:42:10 +02:00
Tobias Schmitz
9e2235dbd8
feat: pdf.artifact element 2025-07-03 18:42:10 +02:00
Tobias Schmitz
1980430578
feat: mark artifacts 2025-07-03 18:41:48 +02:00
Tobias Schmitz
cc70a785dd
feat: [WIP] allow specifying alt text for links
skip-checks:true

# Please enter the commit message for your changes. Lines starting
# with '#' will be kept; you may remove them yourself if you want to.
# An empty message aborts the commit.
#
# Date:      Wed May 28 17:47:35 2025 +0200
#
# On branch pdf-accessibility
# Your branch and 'origin/pdf-accessibility' have diverged,
# and have 11 and 5 different commits each, respectively.
#
# Changes to be committed:
#	modified:   crates/typst-ide/src/jump.rs
#	modified:   crates/typst-layout/src/flow/distribute.rs
#	modified:   crates/typst-layout/src/modifiers.rs
#	modified:   crates/typst-library/src/foundations/content.rs
#	modified:   crates/typst-library/src/layout/frame.rs
#	modified:   crates/typst-library/src/model/bibliography.rs
#	modified:   crates/typst-library/src/model/footnote.rs
#	modified:   crates/typst-library/src/model/link.rs
#	modified:   crates/typst-library/src/model/outline.rs
#	modified:   crates/typst-library/src/model/reference.rs
#	modified:   crates/typst-pdf/src/convert.rs
#	modified:   crates/typst-pdf/src/link.rs
#	modified:   crates/typst-render/src/lib.rs
#	modified:   crates/typst-svg/src/lib.rs
#	modified:   tests/src/run.rs
#
2025-07-03 15:58:07 +02:00
Tobias Schmitz
e8ea837514
feat: [WIP] include links in tag tree
skip-checks:true
2025-07-03 15:58:07 +02:00
Tobias Schmitz
c6b3b371b0
feat: [WIP] write tags
skip-checks:true
2025-07-03 15:58:07 +02:00
Tobias Schmitz
ab7eea23f1
feat: [WIP] make more things locatable
skip-checks:true
2025-07-03 15:58:07 +02:00
Tobias Schmitz
c5dbd85a81
feat: [draft] generate accessibility tag tree for headings
skip-checks:true
2025-07-03 15:58:07 +02:00
Max
22a57fcf5c
Use punctuation math class for Arabic comma (#6537) 2025-07-02 08:01:44 +00:00
Malo
09c831d3b3
Use "subs" and "sups" font features for typographic scripts (#5777) 2025-07-02 08:00:45 +00:00
67 changed files with 2019 additions and 358 deletions

9
Cargo.lock generated
View File

@ -1367,7 +1367,7 @@ dependencies = [
[[package]] [[package]]
name = "krilla" name = "krilla"
version = "0.4.0" version = "0.4.0"
source = "git+https://github.com/LaurenzV/krilla?rev=20c14fe#20c14fefee5002566b3d6668b338bbe2168784e7" source = "git+https://github.com/saecki/krilla?branch=tag-attributes#2897351d6eeb139675b5e7e2765fe6f082e26efd"
dependencies = [ dependencies = [
"base64", "base64",
"bumpalo", "bumpalo",
@ -1385,6 +1385,7 @@ dependencies = [
"rustybuzz", "rustybuzz",
"siphasher", "siphasher",
"skrifa", "skrifa",
"smallvec",
"subsetter", "subsetter",
"tiny-skia-path", "tiny-skia-path",
"xmp-writer", "xmp-writer",
@ -1395,7 +1396,7 @@ dependencies = [
[[package]] [[package]]
name = "krilla-svg" name = "krilla-svg"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/LaurenzV/krilla?rev=20c14fe#20c14fefee5002566b3d6668b338bbe2168784e7" source = "git+https://github.com/saecki/krilla?branch=tag-attributes#2897351d6eeb139675b5e7e2765fe6f082e26efd"
dependencies = [ dependencies = [
"flate2", "flate2",
"fontdb", "fontdb",
@ -2451,9 +2452,9 @@ dependencies = [
[[package]] [[package]]
name = "smallvec" name = "smallvec"
version = "1.13.2" version = "1.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
[[package]] [[package]]
name = "spin" name = "spin"

View File

@ -73,8 +73,8 @@ image = { version = "0.25.5", default-features = false, features = ["png", "jpeg
indexmap = { version = "2", features = ["serde"] } indexmap = { version = "2", features = ["serde"] }
infer = { version = "0.19.0", default-features = false } infer = { version = "0.19.0", default-features = false }
kamadak-exif = "0.6" kamadak-exif = "0.6"
krilla = { git = "https://github.com/LaurenzV/krilla", rev = "20c14fe", default-features = false, features = ["raster-images", "comemo", "rayon"] } krilla = { git = "https://github.com/saecki/krilla", branch = "tag-attributes", default-features = false, features = ["raster-images", "comemo", "rayon"] }
krilla-svg = { git = "https://github.com/LaurenzV/krilla", rev = "20c14fe" } krilla-svg = { git = "https://github.com/saecki/krilla", branch = "tag-attributes" }
kurbo = "0.11" kurbo = "0.11"
libfuzzer-sys = "0.4" libfuzzer-sys = "0.4"
lipsum = "0.9" lipsum = "0.9"

View File

@ -246,6 +246,13 @@ pub struct CompileArgs {
#[arg(long = "pdf-standard", value_delimiter = ',')] #[arg(long = "pdf-standard", value_delimiter = ',')]
pub pdf_standard: Vec<PdfStandard>, 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. /// The PPI (pixels per inch) to use for PNG export.
#[arg(long = "ppi", default_value_t = 144.0)] #[arg(long = "ppi", default_value_t = 144.0)]
pub ppi: f32, pub ppi: f32,
@ -506,6 +513,9 @@ pub enum PdfStandard {
/// PDF/A-4e. /// PDF/A-4e.
#[value(name = "a-4e")] #[value(name = "a-4e")]
A_4e, A_4e,
/// PDF/UA-1.
#[value(name = "ua-1")]
Ua_1,
} }
display_possible_values!(PdfStandard); display_possible_values!(PdfStandard);

View File

@ -65,6 +65,8 @@ pub struct CompileConfig {
pub open: Option<Option<String>>, pub open: Option<Option<String>>,
/// A list of standards the PDF should conform to. /// A list of standards the PDF should conform to.
pub pdf_standards: PdfStandards, 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. /// A path to write a Makefile rule describing the current compilation.
pub make_deps: Option<PathBuf>, pub make_deps: Option<PathBuf>,
/// The PPI (pixels per inch) to use for PNG export. /// The PPI (pixels per inch) to use for PNG export.
@ -150,6 +152,7 @@ impl CompileConfig {
output_format, output_format,
pages, pages,
pdf_standards, pdf_standards,
disable_pdf_tags: args.disable_pdf_tags,
creation_timestamp: args.world.creation_timestamp, creation_timestamp: args.world.creation_timestamp,
make_deps: args.make_deps.clone(), make_deps: args.make_deps.clone(),
ppi: args.ppi, ppi: args.ppi,
@ -291,6 +294,7 @@ fn export_pdf(document: &PagedDocument, config: &CompileConfig) -> SourceResult<
timestamp, timestamp,
page_ranges: config.pages.clone(), page_ranges: config.pages.clone(),
standards: config.pdf_standards.clone(), standards: config.pdf_standards.clone(),
disable_tags: config.disable_pdf_tags,
}; };
let buffer = typst_pdf::pdf(document, &options)?; let buffer = typst_pdf::pdf(document, &options)?;
config config
@ -773,6 +777,7 @@ impl From<PdfStandard> for typst_pdf::PdfStandard {
PdfStandard::A_4 => typst_pdf::PdfStandard::A_4, PdfStandard::A_4 => typst_pdf::PdfStandard::A_4,
PdfStandard::A_4f => typst_pdf::PdfStandard::A_4f, PdfStandard::A_4f => typst_pdf::PdfStandard::A_4f,
PdfStandard::A_4e => typst_pdf::PdfStandard::A_4e, PdfStandard::A_4e => typst_pdf::PdfStandard::A_4e,
PdfStandard::Ua_1 => typst_pdf::PdfStandard::Ua_1,
} }
} }
} }

View File

@ -93,7 +93,7 @@ impl Item<'_, '_> {
Self::Frame(frame, _) => { Self::Frame(frame, _) => {
frame.size().is_zero() frame.size().is_zero()
&& frame.items().all(|(_, item)| { && frame.items().all(|(_, item)| {
matches!(item, FrameItem::Link(_, _) | FrameItem::Tag(_)) matches!(item, FrameItem::Link(..) | FrameItem::Tag(_))
}) })
} }
Self::Placed(_, placed) => !placed.float, Self::Placed(_, placed) => !placed.float,

View File

@ -1284,10 +1284,18 @@ impl<'a> GridLayouter<'a> {
if let Some([first, rest @ ..]) = if let Some([first, rest @ ..]) =
frames.get(measurement_data.frames_in_previous_regions..) frames.get(measurement_data.frames_in_previous_regions..)
{ {
// HACK: reconsider if this is the right decision
fn is_empty_frame(frame: &Frame) -> bool {
!frame.items().any(|(_, item)| match item {
FrameItem::Group(group) => is_empty_frame(&group.frame),
FrameItem::Tag(_) => false,
_ => true,
})
}
if can_skip if can_skip
&& breakable && breakable
&& first.is_empty() && is_empty_frame(first)
&& rest.iter().any(|frame| !frame.is_empty()) && rest.iter().any(|frame| !is_empty_frame(frame))
{ {
return Ok(None); return Ok(None);
} }

View File

@ -206,7 +206,7 @@ pub fn collect<'a>(
} }
InlineItem::Frame(mut frame) => { InlineItem::Frame(mut frame) => {
frame.modify(&FrameModifiers::get_in(styles)); frame.modify(&FrameModifiers::get_in(styles));
apply_baseline_shift(&mut frame, styles); apply_shift(&engine.world, &mut frame, styles);
collector.push_item(Item::Frame(frame)); collector.push_item(Item::Frame(frame));
} }
} }
@ -221,7 +221,7 @@ pub fn collect<'a>(
let mut frame = layout_and_modify(styles, |styles| { let mut frame = layout_and_modify(styles, |styles| {
layout_box(elem, engine, loc, styles, region) layout_box(elem, engine, loc, styles, region)
})?; })?;
apply_baseline_shift(&mut frame, styles); apply_shift(&engine.world, &mut frame, styles);
collector.push_item(Item::Frame(frame)); collector.push_item(Item::Frame(frame));
} }
} else if let Some(elem) = child.to_packed::<TagElem>() { } else if let Some(elem) = child.to_packed::<TagElem>() {

View File

@ -5,7 +5,7 @@ use typst_library::engine::Engine;
use typst_library::introspection::{SplitLocator, Tag}; use typst_library::introspection::{SplitLocator, Tag};
use typst_library::layout::{Abs, Dir, Em, Fr, Frame, FrameItem, Point}; use typst_library::layout::{Abs, Dir, Em, Fr, Frame, FrameItem, Point};
use typst_library::model::ParLineMarker; use typst_library::model::ParLineMarker;
use typst_library::text::{Lang, TextElem}; use typst_library::text::{variant, Lang, TextElem};
use typst_utils::Numeric; use typst_utils::Numeric;
use super::*; use super::*;
@ -330,7 +330,7 @@ fn adjust_cj_at_line_start(p: &Preparation, items: &mut Items) {
let glyph = shaped.glyphs.to_mut().first_mut().unwrap(); let glyph = shaped.glyphs.to_mut().first_mut().unwrap();
let shrink = glyph.shrinkability().0; let shrink = glyph.shrinkability().0;
glyph.shrink_left(shrink); glyph.shrink_left(shrink);
shaped.width -= shrink.at(shaped.size); shaped.width -= shrink.at(glyph.size);
} else if p.config.cjk_latin_spacing } else if p.config.cjk_latin_spacing
&& glyph.is_cj_script() && glyph.is_cj_script()
&& glyph.x_offset > Em::zero() && glyph.x_offset > Em::zero()
@ -342,7 +342,7 @@ fn adjust_cj_at_line_start(p: &Preparation, items: &mut Items) {
glyph.x_advance -= shrink; glyph.x_advance -= shrink;
glyph.x_offset = Em::zero(); glyph.x_offset = Em::zero();
glyph.adjustability.shrinkability.0 = Em::zero(); glyph.adjustability.shrinkability.0 = Em::zero();
shaped.width -= shrink.at(shaped.size); shaped.width -= shrink.at(glyph.size);
} }
} }
@ -360,7 +360,7 @@ fn adjust_cj_at_line_end(p: &Preparation, items: &mut Items) {
let shrink = glyph.shrinkability().1; let shrink = glyph.shrinkability().1;
let punct = shaped.glyphs.to_mut().last_mut().unwrap(); let punct = shaped.glyphs.to_mut().last_mut().unwrap();
punct.shrink_right(shrink); punct.shrink_right(shrink);
shaped.width -= shrink.at(shaped.size); shaped.width -= shrink.at(punct.size);
} else if p.config.cjk_latin_spacing } else if p.config.cjk_latin_spacing
&& glyph.is_cj_script() && glyph.is_cj_script()
&& (glyph.x_advance - glyph.x_offset) > Em::one() && (glyph.x_advance - glyph.x_offset) > Em::one()
@ -371,7 +371,7 @@ fn adjust_cj_at_line_end(p: &Preparation, items: &mut Items) {
let glyph = shaped.glyphs.to_mut().last_mut().unwrap(); let glyph = shaped.glyphs.to_mut().last_mut().unwrap();
glyph.x_advance -= shrink; glyph.x_advance -= shrink;
glyph.adjustability.shrinkability.1 = Em::zero(); glyph.adjustability.shrinkability.1 = Em::zero();
shaped.width -= shrink.at(shaped.size); shaped.width -= shrink.at(glyph.size);
} }
} }
@ -412,9 +412,30 @@ fn should_repeat_hyphen(pred_line: &Line, text: &str) -> bool {
} }
} }
/// Apply the current baseline shift to a frame. /// Apply the current baseline shift and italic compensation to a frame.
pub fn apply_baseline_shift(frame: &mut Frame, styles: StyleChain) { pub fn apply_shift<'a>(
frame.translate(Point::with_y(TextElem::baseline_in(styles))); world: &Tracked<'a, dyn World + 'a>,
frame: &mut Frame,
styles: StyleChain,
) {
let mut baseline = TextElem::baseline_in(styles);
let mut compensation = Abs::zero();
if let Some(scripts) = TextElem::shift_settings_in(styles) {
let font_metrics = TextElem::font_in(styles)
.into_iter()
.find_map(|family| {
world
.book()
.select(family.as_str(), variant(styles))
.and_then(|id| world.font(id))
})
.map_or(*scripts.kind.default_metrics(), |f| {
*scripts.kind.read_metrics(f.metrics())
});
baseline -= scripts.shift.unwrap_or(font_metrics.vertical_offset).resolve(styles);
compensation += font_metrics.horizontal_offset.resolve(styles);
}
frame.translate(Point::new(compensation, baseline));
} }
/// Commit to a line and build its frame. /// Commit to a line and build its frame.
@ -444,7 +465,7 @@ pub fn commit(
&& TextElem::overhang_in(text.styles) && TextElem::overhang_in(text.styles)
&& (line.items.len() > 1 || text.glyphs.len() > 1) && (line.items.len() > 1 || text.glyphs.len() > 1)
{ {
let amount = overhang(glyph.c) * glyph.x_advance.at(text.size); let amount = overhang(glyph.c) * glyph.x_advance.at(glyph.size);
offset -= amount; offset -= amount;
remaining += amount; remaining += amount;
} }
@ -458,7 +479,7 @@ pub fn commit(
&& TextElem::overhang_in(text.styles) && TextElem::overhang_in(text.styles)
&& (line.items.len() > 1 || text.glyphs.len() > 1) && (line.items.len() > 1 || text.glyphs.len() > 1)
{ {
let amount = overhang(glyph.c) * glyph.x_advance.at(text.size); let amount = overhang(glyph.c) * glyph.x_advance.at(glyph.size);
remaining += amount; remaining += amount;
} }
} }
@ -519,7 +540,7 @@ pub fn commit(
let mut frame = layout_and_modify(*styles, |styles| { let mut frame = layout_and_modify(*styles, |styles| {
layout_box(elem, engine, loc.relayout(), styles, region) layout_box(elem, engine, loc.relayout(), styles, region)
})?; })?;
apply_baseline_shift(&mut frame, *styles); apply_shift(&engine.world, &mut frame, *styles);
push(&mut offset, frame, idx); push(&mut offset, frame, idx);
} else { } else {
offset += amount; offset += amount;

View File

@ -927,9 +927,9 @@ impl Estimates {
let byte_len = g.range.len(); let byte_len = g.range.len();
let stretch = g.stretchability().0 + g.stretchability().1; let stretch = g.stretchability().0 + g.stretchability().1;
let shrink = g.shrinkability().0 + g.shrinkability().1; let shrink = g.shrinkability().0 + g.shrinkability().1;
widths.push(byte_len, g.x_advance.at(shaped.size)); widths.push(byte_len, g.x_advance.at(g.size));
stretchability.push(byte_len, stretch.at(shaped.size)); stretchability.push(byte_len, stretch.at(g.size));
shrinkability.push(byte_len, shrink.at(shaped.size)); shrinkability.push(byte_len, shrink.at(g.size));
justifiables.push(byte_len, g.is_justifiable() as usize); justifiables.push(byte_len, g.is_justifiable() as usize);
} }
} else { } else {

View File

@ -29,7 +29,7 @@ use typst_utils::{Numeric, SliceExt};
use self::collect::{collect, Item, Segment, SpanMapper}; use self::collect::{collect, Item, Segment, SpanMapper};
use self::deco::decorate; use self::deco::decorate;
use self::finalize::finalize; use self::finalize::finalize;
use self::line::{apply_baseline_shift, commit, line, Line}; use self::line::{apply_shift, commit, line, Line};
use self::linebreak::{linebreak, Breakpoint}; use self::linebreak::{linebreak, Breakpoint};
use self::prepare::{prepare, Preparation}; use self::prepare::{prepare, Preparation};
use self::shaping::{ use self::shaping::{

View File

@ -144,7 +144,7 @@ fn add_cjk_latin_spacing(items: &mut [(Range, Item)]) {
// The spacing is default to 1/4 em, and can be shrunk to 1/8 em. // The spacing is default to 1/4 em, and can be shrunk to 1/8 em.
glyph.x_advance += Em::new(0.25); glyph.x_advance += Em::new(0.25);
glyph.adjustability.shrinkability.1 += Em::new(0.125); glyph.adjustability.shrinkability.1 += Em::new(0.125);
text.width += Em::new(0.25).at(text.size); text.width += Em::new(0.25).at(glyph.size);
} }
// Case 2: Latin followed by a CJ character // Case 2: Latin followed by a CJ character
@ -152,7 +152,7 @@ fn add_cjk_latin_spacing(items: &mut [(Range, Item)]) {
glyph.x_advance += Em::new(0.25); glyph.x_advance += Em::new(0.25);
glyph.x_offset += Em::new(0.25); glyph.x_offset += Em::new(0.25);
glyph.adjustability.shrinkability.0 += Em::new(0.125); glyph.adjustability.shrinkability.0 += Em::new(0.125);
text.width += Em::new(0.25).at(text.size); text.width += Em::new(0.25).at(glyph.size);
} }
prev = Some(glyph); prev = Some(glyph);

View File

@ -3,14 +3,15 @@ use std::fmt::{self, Debug, Formatter};
use std::sync::Arc; use std::sync::Arc;
use az::SaturatingAs; use az::SaturatingAs;
use rustybuzz::{BufferFlags, ShapePlan, UnicodeBuffer}; use rustybuzz::{BufferFlags, Feature, ShapePlan, UnicodeBuffer};
use ttf_parser::gsub::SubstitutionSubtable;
use ttf_parser::Tag; use ttf_parser::Tag;
use typst_library::engine::Engine; use typst_library::engine::Engine;
use typst_library::foundations::{Smart, StyleChain}; use typst_library::foundations::{Smart, StyleChain};
use typst_library::layout::{Abs, Dir, Em, Frame, FrameItem, Point, Size}; use typst_library::layout::{Abs, Dir, Em, Frame, FrameItem, Point, Size};
use typst_library::text::{ use typst_library::text::{
families, features, is_default_ignorable, language, variant, Font, FontFamily, families, features, is_default_ignorable, language, variant, Font, FontFamily,
FontVariant, Glyph, Lang, Region, TextEdgeBounds, TextElem, TextItem, FontVariant, Glyph, Lang, Region, ShiftSettings, TextEdgeBounds, TextElem, TextItem,
}; };
use typst_library::World; use typst_library::World;
use typst_utils::SliceExt; use typst_utils::SliceExt;
@ -41,8 +42,6 @@ pub struct ShapedText<'a> {
pub styles: StyleChain<'a>, pub styles: StyleChain<'a>,
/// The font variant. /// The font variant.
pub variant: FontVariant, pub variant: FontVariant,
/// The font size.
pub size: Abs,
/// The width of the text's bounding box. /// The width of the text's bounding box.
pub width: Abs, pub width: Abs,
/// The shaped glyphs. /// The shaped glyphs.
@ -62,6 +61,8 @@ pub struct ShapedGlyph {
pub x_offset: Em, pub x_offset: Em,
/// The vertical offset of the glyph. /// The vertical offset of the glyph.
pub y_offset: Em, pub y_offset: Em,
/// The font size for the glyph.
pub size: Abs,
/// The adjustability of the glyph. /// The adjustability of the glyph.
pub adjustability: Adjustability, pub adjustability: Adjustability,
/// The byte range of this glyph's cluster in the full inline layout. A /// The byte range of this glyph's cluster in the full inline layout. A
@ -222,14 +223,17 @@ impl<'a> ShapedText<'a> {
let mut frame = Frame::soft(size); let mut frame = Frame::soft(size);
frame.set_baseline(top); frame.set_baseline(top);
let size = TextElem::size_in(self.styles);
let shift = TextElem::baseline_in(self.styles); let shift = TextElem::baseline_in(self.styles);
let decos = TextElem::deco_in(self.styles); let decos = TextElem::deco_in(self.styles);
let fill = TextElem::fill_in(self.styles); let fill = TextElem::fill_in(self.styles);
let stroke = TextElem::stroke_in(self.styles); let stroke = TextElem::stroke_in(self.styles);
let span_offset = TextElem::span_offset_in(self.styles); let span_offset = TextElem::span_offset_in(self.styles);
for ((font, y_offset), group) in for ((font, y_offset, glyph_size), group) in self
self.glyphs.as_ref().group_by_key(|g| (g.font.clone(), g.y_offset)) .glyphs
.as_ref()
.group_by_key(|g| (g.font.clone(), g.y_offset, g.size))
{ {
let mut range = group[0].range.clone(); let mut range = group[0].range.clone();
for glyph in group { for glyph in group {
@ -237,7 +241,7 @@ impl<'a> ShapedText<'a> {
range.end = range.end.max(glyph.range.end); range.end = range.end.max(glyph.range.end);
} }
let pos = Point::new(offset, top + shift - y_offset.at(self.size)); let pos = Point::new(offset, top + shift - y_offset.at(size));
let glyphs: Vec<Glyph> = group let glyphs: Vec<Glyph> = group
.iter() .iter()
.map(|shaped: &ShapedGlyph| { .map(|shaped: &ShapedGlyph| {
@ -257,11 +261,11 @@ impl<'a> ShapedText<'a> {
adjustability_right * justification_ratio; adjustability_right * justification_ratio;
if shaped.is_justifiable() { if shaped.is_justifiable() {
justification_right += justification_right +=
Em::from_length(extra_justification, self.size) Em::from_abs(extra_justification, glyph_size)
} }
frame.size_mut().x += justification_left.at(self.size) frame.size_mut().x += justification_left.at(glyph_size)
+ justification_right.at(self.size); + justification_right.at(glyph_size);
// We may not be able to reach the offset completely if // We may not be able to reach the offset completely if
// it exceeds u16, but better to have a roughly correct // it exceeds u16, but better to have a roughly correct
@ -304,7 +308,7 @@ impl<'a> ShapedText<'a> {
let item = TextItem { let item = TextItem {
font, font,
size: self.size, size: glyph_size,
lang: self.lang, lang: self.lang,
region: self.region, region: self.region,
fill: fill.clone(), fill: fill.clone(),
@ -336,12 +340,13 @@ impl<'a> ShapedText<'a> {
let mut top = Abs::zero(); let mut top = Abs::zero();
let mut bottom = Abs::zero(); let mut bottom = Abs::zero();
let size = TextElem::size_in(self.styles);
let top_edge = TextElem::top_edge_in(self.styles); let top_edge = TextElem::top_edge_in(self.styles);
let bottom_edge = TextElem::bottom_edge_in(self.styles); let bottom_edge = TextElem::bottom_edge_in(self.styles);
// Expand top and bottom by reading the font's vertical metrics. // Expand top and bottom by reading the font's vertical metrics.
let mut expand = |font: &Font, bounds: TextEdgeBounds| { let mut expand = |font: &Font, bounds: TextEdgeBounds| {
let (t, b) = font.edges(top_edge, bottom_edge, self.size, bounds); let (t, b) = font.edges(top_edge, bottom_edge, size, bounds);
top.set_max(t); top.set_max(t);
bottom.set_max(b); bottom.set_max(b);
}; };
@ -388,18 +393,16 @@ impl<'a> ShapedText<'a> {
pub fn stretchability(&self) -> Abs { pub fn stretchability(&self) -> Abs {
self.glyphs self.glyphs
.iter() .iter()
.map(|g| g.stretchability().0 + g.stretchability().1) .map(|g| (g.stretchability().0 + g.stretchability().1).at(g.size))
.sum::<Em>() .sum()
.at(self.size)
} }
/// The shrinkability of the text /// The shrinkability of the text
pub fn shrinkability(&self) -> Abs { pub fn shrinkability(&self) -> Abs {
self.glyphs self.glyphs
.iter() .iter()
.map(|g| g.shrinkability().0 + g.shrinkability().1) .map(|g| (g.shrinkability().0 + g.shrinkability().1).at(g.size))
.sum::<Em>() .sum()
.at(self.size)
} }
/// Reshape a range of the shaped text, reusing information from this /// Reshape a range of the shaped text, reusing information from this
@ -418,9 +421,8 @@ impl<'a> ShapedText<'a> {
lang: self.lang, lang: self.lang,
region: self.region, region: self.region,
styles: self.styles, styles: self.styles,
size: self.size,
variant: self.variant, variant: self.variant,
width: glyphs.iter().map(|g| g.x_advance).sum::<Em>().at(self.size), width: glyphs_width(glyphs),
glyphs: Cow::Borrowed(glyphs), glyphs: Cow::Borrowed(glyphs),
} }
} else { } else {
@ -484,13 +486,15 @@ impl<'a> ShapedText<'a> {
// that subtracting either of the endpoints by self.base doesn't // that subtracting either of the endpoints by self.base doesn't
// underflow. See <https://github.com/typst/typst/issues/2283>. // underflow. See <https://github.com/typst/typst/issues/2283>.
.unwrap_or_else(|| self.base..self.base); .unwrap_or_else(|| self.base..self.base);
self.width += x_advance.at(self.size); let size = TextElem::size_in(self.styles);
self.width += x_advance.at(size);
let glyph = ShapedGlyph { let glyph = ShapedGlyph {
font, font,
glyph_id: glyph_id.0, glyph_id: glyph_id.0,
x_advance, x_advance,
x_offset: Em::zero(), x_offset: Em::zero(),
y_offset: Em::zero(), y_offset: Em::zero(),
size,
adjustability: Adjustability::default(), adjustability: Adjustability::default(),
range, range,
safe_to_break: true, safe_to_break: true,
@ -666,6 +670,7 @@ fn shape<'a>(
region: Option<Region>, region: Option<Region>,
) -> ShapedText<'a> { ) -> ShapedText<'a> {
let size = TextElem::size_in(styles); let size = TextElem::size_in(styles);
let shift_settings = TextElem::shift_settings_in(styles);
let mut ctx = ShapingContext { let mut ctx = ShapingContext {
engine, engine,
size, size,
@ -676,6 +681,7 @@ fn shape<'a>(
features: features(styles), features: features(styles),
fallback: TextElem::fallback_in(styles), fallback: TextElem::fallback_in(styles),
dir, dir,
shift_settings,
}; };
if !text.is_empty() { if !text.is_empty() {
@ -698,12 +704,17 @@ fn shape<'a>(
region, region,
styles, styles,
variant: ctx.variant, variant: ctx.variant,
size, width: glyphs_width(&ctx.glyphs),
width: ctx.glyphs.iter().map(|g| g.x_advance).sum::<Em>().at(size),
glyphs: Cow::Owned(ctx.glyphs), glyphs: Cow::Owned(ctx.glyphs),
} }
} }
/// Computes the width of a run of glyphs relative to the font size, accounting
/// for their individual scaling factors and other font metrics.
fn glyphs_width(glyphs: &[ShapedGlyph]) -> Abs {
glyphs.iter().map(|g| g.x_advance.at(g.size)).sum()
}
/// Holds shaping results and metadata common to all shaped segments. /// Holds shaping results and metadata common to all shaped segments.
struct ShapingContext<'a, 'v> { struct ShapingContext<'a, 'v> {
engine: &'a Engine<'v>, engine: &'a Engine<'v>,
@ -715,6 +726,7 @@ struct ShapingContext<'a, 'v> {
features: Vec<rustybuzz::Feature>, features: Vec<rustybuzz::Feature>,
fallback: bool, fallback: bool,
dir: Dir, dir: Dir,
shift_settings: Option<ShiftSettings>,
} }
/// Shape text with font fallback using the `families` iterator. /// Shape text with font fallback using the `families` iterator.
@ -789,6 +801,18 @@ fn shape_segment<'a>(
// text extraction. // text extraction.
buffer.set_flags(BufferFlags::REMOVE_DEFAULT_IGNORABLES); buffer.set_flags(BufferFlags::REMOVE_DEFAULT_IGNORABLES);
let (script_shift, script_compensation, scale, shift_feature) = ctx
.shift_settings
.map_or((Em::zero(), Em::zero(), Em::one(), None), |settings| {
determine_shift(text, &font, settings)
});
let has_shift_feature = shift_feature.is_some();
if let Some(feat) = shift_feature {
// Temporarily push the feature.
ctx.features.push(feat)
}
// Prepare the shape plan. This plan depends on direction, script, language, // Prepare the shape plan. This plan depends on direction, script, language,
// and features, but is independent from the text and can thus be memoized. // and features, but is independent from the text and can thus be memoized.
let plan = create_shape_plan( let plan = create_shape_plan(
@ -799,6 +823,10 @@ fn shape_segment<'a>(
&ctx.features, &ctx.features,
); );
if has_shift_feature {
ctx.features.pop();
}
// Shape! // Shape!
let buffer = rustybuzz::shape_with_plan(font.rusty(), &plan, buffer); let buffer = rustybuzz::shape_with_plan(font.rusty(), &plan, buffer);
let infos = buffer.glyph_infos(); let infos = buffer.glyph_infos();
@ -869,8 +897,9 @@ fn shape_segment<'a>(
glyph_id: info.glyph_id as u16, glyph_id: info.glyph_id as u16,
// TODO: Don't ignore y_advance. // TODO: Don't ignore y_advance.
x_advance, x_advance,
x_offset: font.to_em(pos[i].x_offset), x_offset: font.to_em(pos[i].x_offset) + script_compensation,
y_offset: font.to_em(pos[i].y_offset), y_offset: font.to_em(pos[i].y_offset) + script_shift,
size: scale.at(ctx.size),
adjustability: Adjustability::default(), adjustability: Adjustability::default(),
range: start..end, range: start..end,
safe_to_break: !info.unsafe_to_break(), safe_to_break: !info.unsafe_to_break(),
@ -932,6 +961,64 @@ fn shape_segment<'a>(
ctx.used.pop(); ctx.used.pop();
} }
/// Returns a `(script_shift, script_compensation, scale, feature)` quadruplet
/// describing how to produce scripts.
///
/// Those values determine how the rendered text should be transformed to
/// display sub-/super-scripts. If the OpenType feature can be used, the
/// rendered text should not be transformed in any way, and so those values are
/// neutral (`(0, 0, 1, None)`). If scripts should be synthesized, those values
/// determine how to transform the rendered text to display scripts as expected.
fn determine_shift(
text: &str,
font: &Font,
settings: ShiftSettings,
) -> (Em, Em, Em, Option<Feature>) {
settings
.typographic
.then(|| {
// If typographic scripts are enabled (i.e., we want to use the
// OpenType feature instead of synthesizing if possible), we add
// "subs"/"sups" to the feature list if supported by the font.
// In case of a problem, we just early exit
let gsub = font.rusty().tables().gsub?;
let subtable_index =
gsub.features.find(settings.kind.feature())?.lookup_indices.get(0)?;
let coverage = gsub
.lookups
.get(subtable_index)?
.subtables
.get::<SubstitutionSubtable>(0)?
.coverage();
text.chars()
.all(|c| {
font.rusty().glyph_index(c).is_some_and(|i| coverage.contains(i))
})
.then(|| {
// If we can use the OpenType feature, we can keep the text
// as is.
(
Em::zero(),
Em::zero(),
Em::one(),
Some(Feature::new(settings.kind.feature(), 1, ..)),
)
})
})
// Reunite the cases where `typographic` is `false` or where using the
// OpenType feature would not work.
.flatten()
.unwrap_or_else(|| {
let script_metrics = settings.kind.read_metrics(font.metrics());
(
settings.shift.unwrap_or(script_metrics.vertical_offset),
script_metrics.horizontal_offset,
settings.size.unwrap_or(script_metrics.height),
None,
)
})
}
/// Create a shape plan. /// Create a shape plan.
#[comemo::memoize] #[comemo::memoize]
pub fn create_shape_plan( pub fn create_shape_plan(
@ -963,6 +1050,7 @@ fn shape_tofus(ctx: &mut ShapingContext, base: usize, text: &str, font: Font) {
x_advance, x_advance,
x_offset: Em::zero(), x_offset: Em::zero(),
y_offset: Em::zero(), y_offset: Em::zero(),
size: ctx.size,
adjustability: Adjustability::default(), adjustability: Adjustability::default(),
range: start..end, range: start..end,
safe_to_break: true, safe_to_break: true,
@ -985,9 +1073,8 @@ fn shape_tofus(ctx: &mut ShapingContext, base: usize, text: &str, font: Font) {
/// Apply tracking and spacing to the shaped glyphs. /// Apply tracking and spacing to the shaped glyphs.
fn track_and_space(ctx: &mut ShapingContext) { fn track_and_space(ctx: &mut ShapingContext) {
let tracking = Em::from_length(TextElem::tracking_in(ctx.styles), ctx.size); let tracking = Em::from_abs(TextElem::tracking_in(ctx.styles), ctx.size);
let spacing = let spacing = TextElem::spacing_in(ctx.styles).map(|abs| Em::from_abs(abs, ctx.size));
TextElem::spacing_in(ctx.styles).map(|abs| Em::from_length(abs, ctx.size));
let mut glyphs = ctx.glyphs.iter_mut().peekable(); let mut glyphs = ctx.glyphs.iter_mut().peekable();
while let Some(glyph) = glyphs.next() { while let Some(glyph) = glyphs.next() {

View File

@ -215,7 +215,7 @@ impl MathFragment {
&glyph.item.font, &glyph.item.font,
GlyphId(glyph.item.glyphs[glyph_index].id), GlyphId(glyph.item.glyphs[glyph_index].id),
corner, corner,
Em::from_length(height, glyph.item.size), Em::from_abs(height, glyph.item.size),
) )
.unwrap_or_default() .unwrap_or_default()
.at(glyph.item.size) .at(glyph.item.size)
@ -767,8 +767,8 @@ fn assemble(
advance += ratio * (max_overlap - min_overlap); advance += ratio * (max_overlap - min_overlap);
} }
let (x, y) = match axis { let (x, y) = match axis {
Axis::X => (Em::from_length(advance, base.item.size), Em::zero()), Axis::X => (Em::from_abs(advance, base.item.size), Em::zero()),
Axis::Y => (Em::zero(), Em::from_length(advance, base.item.size)), Axis::Y => (Em::zero(), Em::from_abs(advance, base.item.size)),
}; };
glyphs.push(Glyph { glyphs.push(Glyph {
id: part.glyph_id.0, id: part.glyph_id.0,

View File

@ -13,6 +13,7 @@ use typst_library::layout::{
VAlignment, VAlignment,
}; };
use typst_library::model::Numbering; use typst_library::model::Numbering;
use typst_library::pdf::ArtifactKind;
use typst_library::routines::{Pair, Routines}; use typst_library::routines::{Pair, Routines};
use typst_library::text::{LocalName, TextElem}; use typst_library::text::{LocalName, TextElem};
use typst_library::visualize::Paint; use typst_library::visualize::Paint;
@ -200,6 +201,11 @@ fn layout_page_run_impl(
// Layout marginals. // Layout marginals.
let mut layouted = Vec::with_capacity(fragment.len()); 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 { for inner in fragment {
let header_size = Size::new(inner.width(), margin.top - header_ascent); let header_size = Size::new(inner.width(), margin.top - header_ascent);
let footer_size = Size::new(inner.width(), margin.bottom - footer_descent); let footer_size = Size::new(inner.width(), margin.bottom - footer_descent);
@ -210,9 +216,9 @@ fn layout_page_run_impl(
fill: fill.clone(), fill: fill.clone(),
numbering: numbering.clone(), numbering: numbering.clone(),
supplement: supplement.clone(), supplement: supplement.clone(),
header: layout_marginal(header, header_size, Alignment::BOTTOM)?, header: layout_marginal(&header, header_size, Alignment::BOTTOM)?,
footer: layout_marginal(footer, footer_size, Alignment::TOP)?, footer: layout_marginal(&footer, footer_size, Alignment::TOP)?,
background: layout_marginal(background, full_size, mid)?, background: layout_marginal(&background, full_size, mid)?,
foreground: layout_marginal(foreground, full_size, mid)?, foreground: layout_marginal(foreground, full_size, mid)?,
margin, margin,
binding, binding,

View File

@ -16,12 +16,13 @@ use crate::diag::{SourceResult, StrResult};
use crate::engine::Engine; use crate::engine::Engine;
use crate::foundations::{ use crate::foundations::{
elem, func, scope, ty, Context, Dict, Element, Fields, IntoValue, Label, elem, func, scope, ty, Context, Dict, Element, Fields, IntoValue, Label,
NativeElement, Recipe, RecipeIndex, Repr, Selector, Str, Style, StyleChain, Styles, NativeElement, Recipe, RecipeIndex, Repr, Selector, Show, Str, Style, StyleChain,
Value, Styles, Value,
}; };
use crate::introspection::Location; use crate::introspection::{Locatable, Location};
use crate::layout::{AlignElem, Alignment, Axes, Length, MoveElem, PadElem, Rel, Sides}; use crate::layout::{AlignElem, Alignment, Axes, Length, MoveElem, PadElem, Rel, Sides};
use crate::model::{Destination, EmphElem, LinkElem, StrongElem}; use crate::model::{Destination, EmphElem, LinkElem, StrongElem};
use crate::pdf::{ArtifactElem, ArtifactKind};
use crate::text::UnderlineElem; use crate::text::UnderlineElem;
/// A piece of document content. /// A piece of document content.
@ -503,8 +504,12 @@ impl Content {
} }
/// Link the content somewhere. /// Link the content somewhere.
pub fn linked(self, dest: Destination) -> Self { pub fn linked(self, dest: Destination, alt: Option<EcoString>) -> Self {
self.styled(LinkElem::set_current(Some(dest))) let span = self.span();
LinkMarker::new(self, dest.clone(), alt)
.pack()
.spanned(span)
.styled(LinkElem::set_current(Some(dest)))
} }
/// Set alignments for this content. /// Set alignments for this content.
@ -533,6 +538,12 @@ impl Content {
.pack() .pack()
.spanned(span) .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] #[scope]
@ -980,6 +991,24 @@ pub trait PlainText {
fn plain_text(&self, text: &mut EcoString); 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. /// An error arising when trying to access a field of content.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum FieldAccessError { pub enum FieldAccessError {

View File

@ -8,6 +8,7 @@ pub use self::dom::*;
use ecow::EcoString; use ecow::EcoString;
use crate::foundations::{elem, Content, Module, Scope}; use crate::foundations::{elem, Content, Module, Scope};
use crate::introspection::Locatable;
/// Create a module with all HTML definitions. /// Create a module with all HTML definitions.
pub fn module() -> Module { pub fn module() -> Module {
@ -40,7 +41,7 @@ pub fn module() -> Module {
/// A div with _Typst content_ inside! /// A div with _Typst content_ inside!
/// ] /// ]
/// ``` /// ```
#[elem(name = "elem")] #[elem(name = "elem", Locatable)]
pub struct HtmlElem { pub struct HtmlElem {
/// The element's tag. /// The element's tag.
#[required] #[required]

View File

@ -6,7 +6,7 @@ use ecow::EcoString;
use typst_utils::{Numeric, Scalar}; use typst_utils::{Numeric, Scalar};
use crate::foundations::{cast, repr, Repr, Resolve, StyleChain, Value}; use crate::foundations::{cast, repr, Repr, Resolve, StyleChain, Value};
use crate::layout::Abs; use crate::layout::{Abs, Length};
use crate::text::TextElem; use crate::text::TextElem;
/// A length that is relative to the font size. /// A length that is relative to the font size.
@ -26,18 +26,18 @@ impl Em {
Self(Scalar::ONE) Self(Scalar::ONE)
} }
/// Create a font-relative length. /// Creates a font-relative length.
pub const fn new(em: f64) -> Self { pub const fn new(em: f64) -> Self {
Self(Scalar::new(em)) Self(Scalar::new(em))
} }
/// Create an em length from font units at the given units per em. /// Creates an em length from font units at the given units per em.
pub fn from_units(units: impl Into<f64>, units_per_em: f64) -> Self { pub fn from_units(units: impl Into<f64>, units_per_em: f64) -> Self {
Self(Scalar::new(units.into() / units_per_em)) Self(Scalar::new(units.into() / units_per_em))
} }
/// Create an em length from a length at the given font size. /// Creates an em length from an absolute length at the given font size.
pub fn from_length(length: Abs, font_size: Abs) -> Self { pub fn from_abs(length: Abs, font_size: Abs) -> Self {
let result = length / font_size; let result = length / font_size;
if result.is_finite() { if result.is_finite() {
Self(Scalar::new(result)) Self(Scalar::new(result))
@ -46,6 +46,11 @@ impl Em {
} }
} }
/// Creates an em length from a length at the given font size.
pub fn from_length(length: Length, font_size: Abs) -> Em {
length.em + Self::from_abs(length.abs, font_size)
}
/// The number of em units. /// The number of em units.
pub const fn get(self) -> f64 { pub const fn get(self) -> f64 {
(self.0).get() (self.0).get()
@ -56,7 +61,7 @@ impl Em {
Self::new(self.get().abs()) Self::new(self.get().abs())
} }
/// Convert to an absolute length at the given font size. /// Converts to an absolute length at the given font size.
pub fn at(self, font_size: Abs) -> Abs { pub fn at(self, font_size: Abs) -> Abs {
let resolved = font_size * self.get(); let resolved = font_size * self.get();
if resolved.is_finite() { if resolved.is_finite() {

View File

@ -13,6 +13,7 @@ use crate::foundations::{
cast, elem, scope, Array, CastInfo, Content, Context, Fold, FromValue, Func, cast, elem, scope, Array, CastInfo, Content, Context, Fold, FromValue, Func,
IntoValue, NativeElement, Packed, Reflect, Resolve, Show, Smart, StyleChain, Value, IntoValue, NativeElement, Packed, Reflect, Resolve, Show, Smart, StyleChain, Value,
}; };
use crate::introspection::Locatable;
use crate::layout::{ use crate::layout::{
Alignment, BlockElem, Length, OuterHAlignment, OuterVAlignment, Rel, Sides, Sizing, 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 /// Furthermore, strokes of a repeated grid header or footer will take
/// precedence over regular cell strokes. /// precedence over regular cell strokes.
#[elem(scope, Show)] #[elem(scope, Locatable, Show)]
pub struct GridElem { pub struct GridElem {
/// The column sizes. /// 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 /// 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 /// an example, refer to the [`table.header`]($table.header) element and the
/// [`grid.stroke`]($grid.stroke) parameter. /// [`grid.stroke`]($grid.stroke) parameter.
#[elem(name = "header", title = "Grid Header")] #[elem(name = "header", title = "Grid Header", Locatable)]
pub struct GridHeader { pub struct GridHeader {
/// Whether this header should be repeated across pages. /// Whether this header should be repeated across pages.
#[default(true)] #[default(true)]
@ -490,7 +491,7 @@ pub struct GridHeader {
/// itself on every page of the table. /// itself on every page of the table.
/// ///
/// No other grid cells may be placed after the footer. /// 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 { pub struct GridFooter {
/// Whether this footer should be repeated across pages. /// Whether this footer should be repeated across pages.
#[default(true)] #[default(true)]
@ -657,7 +658,7 @@ pub struct GridVLine {
/// which allows you, for example, to apply styles based on a cell's position. /// 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 /// Refer to the examples of the [`table.cell`]($table.cell) element to learn
/// more about this. /// more about this.
#[elem(name = "cell", title = "Grid Cell", Show)] #[elem(name = "cell", title = "Grid Cell", Locatable, Show)]
pub struct GridCell { pub struct GridCell {
/// The cell's body. /// The cell's body.
#[required] #[required]

View File

@ -22,6 +22,7 @@ use typst_syntax::Span;
use typst_utils::NonZeroExt; use typst_utils::NonZeroExt;
use crate::introspection::SplitLocator; use crate::introspection::SplitLocator;
use crate::model::{TableCellKind, TableHeaderScope};
/// Convert a grid to a cell grid. /// Convert a grid to a cell grid.
#[typst_macros::time(span = elem.span())] #[typst_macros::time(span = elem.span())]
@ -217,6 +218,7 @@ impl ResolvableCell for Packed<TableCell> {
breakable: bool, breakable: bool,
locator: Locator<'a>, locator: Locator<'a>,
styles: StyleChain, styles: StyleChain,
kind: Smart<TableCellKind>,
) -> Cell<'a> { ) -> Cell<'a> {
let cell = &mut *self; let cell = &mut *self;
let colspan = cell.colspan(styles); let colspan = cell.colspan(styles);
@ -224,6 +226,8 @@ impl ResolvableCell for Packed<TableCell> {
let breakable = cell.breakable(styles).unwrap_or(breakable); let breakable = cell.breakable(styles).unwrap_or(breakable);
let fill = cell.fill(styles).unwrap_or_else(|| fill.clone()); let fill = cell.fill(styles).unwrap_or_else(|| fill.clone());
let kind = cell.kind(styles).or(kind);
let cell_stroke = cell.stroke(styles); let cell_stroke = cell.stroke(styles);
let stroke_overridden = let stroke_overridden =
cell_stroke.as_ref().map(|side| matches!(side, Some(Some(_)))); 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_breakable(Smart::Custom(breakable));
cell.push_kind(kind);
Cell { Cell {
body: self.pack(), body: self.pack(),
locator, locator,
@ -312,6 +317,7 @@ impl ResolvableCell for Packed<GridCell> {
breakable: bool, breakable: bool,
locator: Locator<'a>, locator: Locator<'a>,
styles: StyleChain, styles: StyleChain,
_: Smart<TableCellKind>,
) -> Cell<'a> { ) -> Cell<'a> {
let cell = &mut *self; let cell = &mut *self;
let colspan = cell.colspan(styles); let colspan = cell.colspan(styles);
@ -522,6 +528,7 @@ pub trait ResolvableCell {
breakable: bool, breakable: bool,
locator: Locator<'a>, locator: Locator<'a>,
styles: StyleChain, styles: StyleChain,
kind: Smart<TableCellKind>,
) -> Cell<'a>; ) -> Cell<'a>;
/// Returns this cell's column override. /// Returns this cell's column override.
@ -1206,8 +1213,14 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
// a non-empty row. // a non-empty row.
let mut first_available_row = 0; let mut first_available_row = 0;
// The cell kind is currently only used for tagged PDF.
let cell_kind;
let (header_footer_items, simple_item) = match child { let (header_footer_items, simple_item) = match child {
ResolvableGridChild::Header { repeat, level, span, items, .. } => { ResolvableGridChild::Header { repeat, level, span, items } => {
cell_kind =
Smart::Custom(TableCellKind::Header(level, TableHeaderScope::Column));
row_group_data = Some(RowGroupData { row_group_data = Some(RowGroupData {
range: None, range: None,
span, span,
@ -1234,11 +1247,13 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
(Some(items), None) (Some(items), None)
} }
ResolvableGridChild::Footer { repeat, span, items, .. } => { ResolvableGridChild::Footer { repeat, span, items } => {
if footer.is_some() { if footer.is_some() {
bail!(span, "cannot have more than one footer"); bail!(span, "cannot have more than one footer");
} }
cell_kind = Smart::Custom(TableCellKind::Footer);
row_group_data = Some(RowGroupData { row_group_data = Some(RowGroupData {
range: None, range: None,
span, span,
@ -1257,6 +1272,8 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
(Some(items), None) (Some(items), None)
} }
ResolvableGridChild::Item(item) => { ResolvableGridChild::Item(item) => {
cell_kind = Smart::Custom(TableCellKind::Data);
if matches!(item, ResolvableGridItem::Cell(_)) { if matches!(item, ResolvableGridItem::Cell(_)) {
*at_least_one_cell = true; *at_least_one_cell = true;
} }
@ -1447,7 +1464,7 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
// Let's resolve the cell so it can determine its own fields // Let's resolve the cell so it can determine its own fields
// based on its final position. // 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() { if largest_index >= resolved_cells.len() {
// Ensure the length of the vector of resolved cells is // Ensure the length of the vector of resolved cells is
@ -1542,6 +1559,13 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
// and footers without having to loop through them each time. // and footers without having to loop through them each time.
// Cells themselves, unfortunately, still have to. // Cells themselves, unfortunately, still have to.
assert!(resolved_cells[*local_auto_index].is_none()); assert!(resolved_cells[*local_auto_index].is_none());
let kind = match row_group.kind {
RowGroupKind::Header => TableCellKind::Header(
NonZeroU32::ONE,
TableHeaderScope::default(),
),
RowGroupKind::Footer => TableCellKind::Footer,
};
resolved_cells[*local_auto_index] = resolved_cells[*local_auto_index] =
Some(Entry::Cell(self.resolve_cell( Some(Entry::Cell(self.resolve_cell(
T::default(), T::default(),
@ -1549,6 +1573,7 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
first_available_row, first_available_row,
1, 1,
Span::detached(), Span::detached(),
Smart::Custom(kind),
)?)); )?));
group_start..group_end group_start..group_end
@ -1673,6 +1698,7 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
y, y,
1, 1,
Span::detached(), Span::detached(),
Smart::Auto,
)?)) )?))
} }
}) })
@ -1918,6 +1944,7 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
y: usize, y: usize,
rowspan: usize, rowspan: usize,
cell_span: Span, cell_span: Span,
kind: Smart<TableCellKind>,
) -> SourceResult<Cell<'x>> ) -> SourceResult<Cell<'x>>
where where
T: ResolvableCell + Default, T: ResolvableCell + Default,
@ -1954,6 +1981,7 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
breakable, breakable,
self.locator.next(&cell_span), self.locator.next(&cell_span),
self.styles, self.styles,
kind,
)) ))
} }
} }

View File

@ -1,6 +1,7 @@
use crate::diag::SourceResult; use crate::diag::SourceResult;
use crate::engine::Engine; use crate::engine::Engine;
use crate::foundations::{elem, Content, Packed, Show, StyleChain}; use crate::foundations::{elem, Content, Packed, Show, StyleChain};
use crate::introspection::Locatable;
/// Hides content without affecting layout. /// Hides content without affecting layout.
/// ///
@ -14,7 +15,7 @@ use crate::foundations::{elem, Content, Packed, Show, StyleChain};
/// Hello Jane \ /// Hello Jane \
/// #hide[Hello] Joe /// #hide[Hello] Joe
/// ``` /// ```
#[elem(Show)] #[elem(Locatable, Show)]
pub struct HideElem { pub struct HideElem {
/// The content to hide. /// The content to hide.
#[required] #[required]

View File

@ -1,6 +1,7 @@
use crate::diag::SourceResult; use crate::diag::SourceResult;
use crate::engine::Engine; use crate::engine::Engine;
use crate::foundations::{elem, Content, NativeElement, Packed, Show, StyleChain}; use crate::foundations::{elem, Content, NativeElement, Packed, Show, StyleChain};
use crate::introspection::Locatable;
use crate::layout::{BlockElem, Length}; use crate::layout::{BlockElem, Length};
/// Repeats content to the available space. /// Repeats content to the available space.
@ -24,7 +25,7 @@ use crate::layout::{BlockElem, Length};
/// Berlin, the 22nd of December, 2022 /// Berlin, the 22nd of December, 2022
/// ] /// ]
/// ``` /// ```
#[elem(Show)] #[elem(Locatable, Show)]
pub struct RepeatElem { pub struct RepeatElem {
/// The content to repeat. /// The content to repeat.
#[required] #[required]

View File

@ -28,6 +28,7 @@ use typst_utils::singleton;
use unicode_math_class::MathClass; use unicode_math_class::MathClass;
use crate::foundations::{elem, Content, Module, NativeElement, Scope}; use crate::foundations::{elem, Content, Module, NativeElement, Scope};
use crate::introspection::Locatable;
use crate::layout::{Em, HElem}; use crate::layout::{Em, HElem};
use crate::text::TextElem; use crate::text::TextElem;
@ -109,7 +110,7 @@ pub fn module() -> Module {
pub trait Mathy {} pub trait Mathy {}
/// A math alignment point: `&`, `&&`. /// A math alignment point: `&`, `&&`.
#[elem(title = "Alignment Point", Mathy)] #[elem(title = "Alignment Point", Mathy, Locatable)]
pub struct AlignPointElem {} pub struct AlignPointElem {}
impl AlignPointElem { impl AlignPointElem {
@ -136,7 +137,7 @@ impl AlignPointElem {
/// ///
/// $x loves y and y loves 5$ /// $x loves y and y loves 5$
/// ``` /// ```
#[elem(Mathy)] #[elem(Mathy, Locatable)]
pub struct ClassElem { pub struct ClassElem {
/// The class to apply to the content. /// The class to apply to the content.
#[required] #[required]

View File

@ -1,6 +1,7 @@
use typst_syntax::Span; use typst_syntax::Span;
use crate::foundations::{elem, func, Content, NativeElement}; use crate::foundations::{elem, func, Content, NativeElement};
use crate::introspection::Locatable;
use crate::math::Mathy; use crate::math::Mathy;
/// A square root. /// A square root.
@ -22,7 +23,7 @@ pub fn sqrt(
/// ```example /// ```example
/// $ root(3, x) $ /// $ root(3, x) $
/// ``` /// ```
#[elem(Mathy)] #[elem(Mathy, Locatable)]
pub struct RootElem { pub struct RootElem {
/// Which root of the radicand to take. /// Which root of the radicand to take.
#[positional] #[positional]

View File

@ -877,7 +877,8 @@ impl<'a> Generator<'a> {
renderer.display_elem_child(elem, &mut None, false)?; renderer.display_elem_child(elem, &mut None, false)?;
if let Some(location) = first_occurrences.get(item.key.as_str()) { if let Some(location) = first_occurrences.get(item.key.as_str()) {
let dest = Destination::Location(*location); let dest = Destination::Location(*location);
content = content.linked(dest); // TODO(accessibility): generate alt text
content = content.linked(dest, None);
} }
StrResult::Ok(content) StrResult::Ok(content)
}) })
@ -1012,7 +1013,8 @@ impl ElemRenderer<'_> {
if let Some(hayagriva::ElemMeta::Entry(i)) = elem.meta { if let Some(hayagriva::ElemMeta::Entry(i)) = elem.meta {
if let Some(location) = (self.link)(i) { if let Some(location) = (self.link)(i) {
let dest = Destination::Location(location); let dest = Destination::Location(location);
content = content.linked(dest); // TODO(accessibility): generate alt text
content = content.linked(dest, None);
} }
} }

View File

@ -43,7 +43,7 @@ use crate::text::{Lang, Region, TextElem};
/// This function indirectly has dedicated syntax. [References]($ref) can be /// This function indirectly has dedicated syntax. [References]($ref) can be
/// used to cite works from the bibliography. The label then corresponds to the /// used to cite works from the bibliography. The label then corresponds to the
/// citation key. /// citation key.
#[elem(Synthesize)] #[elem(Locatable, Synthesize)]
pub struct CiteElem { pub struct CiteElem {
/// The citation key that identifies the entry in the bibliography that /// The citation key that identifies the entry in the bibliography that
/// shall be cited, as a label. /// shall be cited, as a label.

View File

@ -4,6 +4,7 @@ use crate::foundations::{
elem, Content, NativeElement, Packed, Show, StyleChain, TargetElem, elem, Content, NativeElement, Packed, Show, StyleChain, TargetElem,
}; };
use crate::html::{tag, HtmlElem}; use crate::html::{tag, HtmlElem};
use crate::introspection::Locatable;
use crate::text::{ItalicToggle, TextElem}; use crate::text::{ItalicToggle, TextElem};
/// Emphasizes content by toggling italics. /// Emphasizes content by toggling italics.
@ -29,7 +30,7 @@ use crate::text::{ItalicToggle, TextElem};
/// This function also has dedicated syntax: To emphasize content, simply /// This function also has dedicated syntax: To emphasize content, simply
/// enclose it in underscores (`_`). Note that this only works at word /// enclose it in underscores (`_`). Note that this only works at word
/// boundaries. To emphasize part of a word, you have to use the function. /// 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 { pub struct EmphElem {
/// The content to emphasize. /// The content to emphasize.
#[required] #[required]

View File

@ -10,6 +10,7 @@ use crate::foundations::{
Styles, TargetElem, Styles, TargetElem,
}; };
use crate::html::{attr, tag, HtmlElem}; use crate::html::{attr, tag, HtmlElem};
use crate::introspection::Locatable;
use crate::layout::{Alignment, BlockElem, Em, HAlignment, Length, VAlignment, VElem}; use crate::layout::{Alignment, BlockElem, Em, HAlignment, Length, VAlignment, VElem};
use crate::model::{ use crate::model::{
ListItemLike, ListLike, Numbering, NumberingPattern, ParElem, ParbreakElem, ListItemLike, ListLike, Numbering, NumberingPattern, ParElem, ParbreakElem,
@ -71,7 +72,7 @@ use crate::model::{
/// Enumeration items can contain multiple paragraphs and other block-level /// Enumeration items can contain multiple paragraphs and other block-level
/// content. All content that is indented more than an item's marker becomes /// content. All content that is indented more than an item's marker becomes
/// part of that item. /// part of that item.
#[elem(scope, title = "Numbered List", Show)] #[elem(scope, title = "Numbered List", Locatable, Show)]
pub struct EnumElem { pub struct EnumElem {
/// Defines the default [spacing]($enum.spacing) of the enumeration. If it /// Defines the default [spacing]($enum.spacing) of the enumeration. If it
/// is `{false}`, the items are spaced apart with /// is `{false}`, the items are spaced apart with
@ -271,7 +272,7 @@ impl Show for Packed<EnumElem> {
} }
/// An enumeration item. /// An enumeration item.
#[elem(name = "item", title = "Numbered List Item")] #[elem(name = "item", title = "Numbered List Item", Locatable)]
pub struct EnumItem { pub struct EnumItem {
/// The item's number. /// The item's number.
#[positional] #[positional]

View File

@ -473,7 +473,7 @@ impl Outlinable for Packed<FigureElem> {
/// caption: [A rectangle], /// caption: [A rectangle],
/// ) /// )
/// ``` /// ```
#[elem(name = "caption", Synthesize, Show)] #[elem(name = "caption", Locatable, Synthesize, Show)]
pub struct FigureCaption { pub struct FigureCaption {
/// The caption's position in the figure. Either `{top}` or `{bottom}`. /// The caption's position in the figure. Either `{top}` or `{bottom}`.
/// ///

View File

@ -147,7 +147,8 @@ impl Show for Packed<FootnoteElem> {
let sup = SuperElem::new(num).pack().spanned(span); let sup = SuperElem::new(num).pack().spanned(span);
let loc = loc.variant(1); let loc = loc.variant(1);
// Add zero-width weak spacing to make the footnote "sticky". // Add zero-width weak spacing to make the footnote "sticky".
Ok(HElem::hole().pack() + sup.linked(Destination::Location(loc))) // TODO(accessibility): generate 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). /// 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 /// 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. /// 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 { pub struct FootnoteEntry {
/// The footnote for this entry. Its location can be used to determine /// The footnote for this entry. Its location can be used to determine
/// the footnote counter state. /// the footnote counter state.
@ -296,7 +297,8 @@ impl Show for Packed<FootnoteEntry> {
let sup = SuperElem::new(num) let sup = SuperElem::new(num)
.pack() .pack()
.spanned(span) .spanned(span)
.linked(Destination::Location(loc)) // TODO(accessibility): generate alt text
.linked(Destination::Location(loc), None)
.located(loc.variant(1)); .located(loc.variant(1));
Ok(Content::sequence([ Ok(Content::sequence([

View File

@ -9,7 +9,7 @@ use crate::foundations::{
StyleChain, Styles, TargetElem, StyleChain, Styles, TargetElem,
}; };
use crate::html::{attr, tag, HtmlElem}; use crate::html::{attr, tag, HtmlElem};
use crate::introspection::Location; use crate::introspection::{Locatable, Location};
use crate::layout::Position; use crate::layout::Position;
use crate::text::TextElem; use crate::text::TextElem;
@ -38,8 +38,11 @@ use crate::text::TextElem;
/// # Syntax /// # Syntax
/// This function also has dedicated syntax: Text that starts with `http://` or /// This function also has dedicated syntax: Text that starts with `http://` or
/// `https://` is automatically turned into a link. /// `https://` is automatically turned into a link.
#[elem(Show)] #[elem(Locatable, Show)]
pub struct LinkElem { pub struct LinkElem {
/// A text describing the link.
pub alt: Option<EcoString>,
/// The destination the link points to. /// The destination the link points to.
/// ///
/// - To link to web pages, `dest` should be a valid URL string. If the URL /// - 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 body
} }
} else { } else {
let alt = self.alt(styles);
match &self.dest { match &self.dest {
LinkTarget::Dest(dest) => body.linked(dest.clone()), LinkTarget::Dest(dest) => body.linked(dest.clone(), alt),
LinkTarget::Label(label) => { LinkTarget::Label(label) => {
let elem = engine.introspector.query_label(*label).at(self.span())?; let elem = engine.introspector.query_label(*label).at(self.span())?;
let dest = Destination::Location(elem.location().unwrap()); let dest = Destination::Location(elem.location().unwrap());
body.clone().linked(dest) body.linked(dest, alt)
} }
} }
}) })

View File

@ -7,6 +7,7 @@ use crate::foundations::{
Smart, StyleChain, Styles, TargetElem, Value, Smart, StyleChain, Styles, TargetElem, Value,
}; };
use crate::html::{tag, HtmlElem}; use crate::html::{tag, HtmlElem};
use crate::introspection::Locatable;
use crate::layout::{BlockElem, Em, Length, VElem}; use crate::layout::{BlockElem, Em, Length, VElem};
use crate::model::{ParElem, ParbreakElem}; use crate::model::{ParElem, ParbreakElem};
use crate::text::TextElem; 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 /// 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 /// paragraphs and other block-level content. All content that is indented
/// more than an item's marker becomes part of that item. /// 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 { pub struct ListElem {
/// Defines the default [spacing]($list.spacing) of the list. If it is /// Defines the default [spacing]($list.spacing) of the list. If it is
/// `{false}`, the items are spaced apart with /// `{false}`, the items are spaced apart with
@ -178,7 +179,7 @@ impl Show for Packed<ListElem> {
} }
/// A bullet list item. /// A bullet list item.
#[elem(name = "item", title = "Bullet List Item")] #[elem(name = "item", title = "Bullet List Item", Locatable)]
pub struct ListItem { pub struct ListItem {
/// The item's body. /// The item's body.
#[required] #[required]

View File

@ -2,6 +2,7 @@ use std::num::NonZeroUsize;
use std::str::FromStr; use std::str::FromStr;
use comemo::{Track, Tracked}; use comemo::{Track, Tracked};
use ecow::{eco_format, EcoString};
use smallvec::SmallVec; use smallvec::SmallVec;
use typst_syntax::Span; use typst_syntax::Span;
use typst_utils::{Get, NonZeroExt}; use typst_utils::{Get, NonZeroExt};
@ -17,12 +18,12 @@ use crate::introspection::{
Counter, CounterKey, Introspector, Locatable, Location, Locator, LocatorLink, Counter, CounterKey, Introspector, Locatable, Location, Locator, LocatorLink,
}; };
use crate::layout::{ use crate::layout::{
Abs, Axes, BlockBody, BlockElem, BoxElem, Dir, Em, Fr, HElem, Length, Region, Rel, Abs, Axes, BlockBody, BlockElem, BoxElem, Dir, Em, Fr, HElem, Length, PageElem,
RepeatElem, Sides, Region, Rel, RepeatElem, Sides,
}; };
use crate::math::EquationElem; use crate::math::EquationElem;
use crate::model::{Destination, HeadingElem, NumberingPattern, ParElem, Refable}; use crate::model::{Destination, HeadingElem, NumberingPattern, ParElem, Refable};
use crate::text::{LocalName, SpaceElem, TextElem}; use crate::text::{LocalName, SmartQuoteElem, SmartQuotes, SpaceElem, TextElem};
/// A table of contents, figures, or other elements. /// A table of contents, figures, or other elements.
/// ///
@ -272,6 +273,7 @@ impl Show for Packed<OutlineElem> {
let depth = self.depth(styles).unwrap_or(NonZeroUsize::MAX); let depth = self.depth(styles).unwrap_or(NonZeroUsize::MAX);
// Build the outline entries. // Build the outline entries.
let mut entries = vec![];
for elem in elems { for elem in elems {
let Some(outlinable) = elem.with::<dyn Outlinable>() else { let Some(outlinable) = elem.with::<dyn Outlinable>() else {
bail!(span, "cannot outline {}", elem.func().name()); bail!(span, "cannot outline {}", elem.func().name());
@ -280,10 +282,13 @@ impl Show for Packed<OutlineElem> {
let level = outlinable.level(); let level = outlinable.level();
if outlinable.outlined() && level <= depth { if outlinable.outlined() && level <= depth {
let entry = OutlineEntry::new(level, elem); 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)) Ok(Content::sequence(seq))
} }
} }
@ -306,6 +311,19 @@ impl LocalName for Packed<OutlineElem> {
const KEY: &'static str = "outline"; const KEY: &'static str = "outline";
} }
/// Only used to delimit the outline in tagged PDF.
#[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. /// Defines how an outline is indented.
#[derive(Debug, Clone, PartialEq, Hash)] #[derive(Debug, Clone, PartialEq, Hash)]
pub enum OutlineIndent { pub enum OutlineIndent {
@ -364,7 +382,7 @@ pub trait Outlinable: Refable {
/// With show-set and show rules on outline entries, you can richly customize /// With show-set and show rules on outline entries, you can richly customize
/// the outline's appearance. See the /// the outline's appearance. See the
/// [section on styling the outline]($outline/#styling-the-outline) for details. /// [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 { pub struct OutlineEntry {
/// The nesting level of this outline entry. Starts at `{1}` for top-level /// The nesting level of this outline entry. Starts at `{1}` for top-level
/// entries. /// entries.
@ -417,8 +435,12 @@ impl Show for Packed<OutlineEntry> {
let context = Context::new(None, Some(styles)); let context = Context::new(None, Some(styles));
let context = context.track(); let context = context.track();
// TODO(accessibility): prefix should be wrapped in a `Lbl` structure element
let prefix = self.prefix(engine, context, span)?; 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 = alt_text(styles, &prefix, &body, &page);
let inner = self.inner(context, span, body, page)?;
let block = if self.element.is::<EquationElem>() { let block = if self.element.is::<EquationElem>() {
let body = prefix.unwrap_or_default() + inner; let body = prefix.unwrap_or_default() + inner;
BlockElem::new() BlockElem::new()
@ -430,7 +452,7 @@ impl Show for Packed<OutlineEntry> {
}; };
let loc = self.element_location().at(span)?; let loc = self.element_location().at(span)?;
Ok(block.linked(Destination::Location(loc))) Ok(block.linked(Destination::Location(loc), Some(alt)))
} }
} }
@ -565,9 +587,10 @@ impl OutlineEntry {
#[func(contextual)] #[func(contextual)]
pub fn inner( pub fn inner(
&self, &self,
engine: &mut Engine,
context: Tracked<Context>, context: Tracked<Context>,
span: Span, span: Span,
body: Content,
page: Content,
) -> SourceResult<Content> { ) -> SourceResult<Content> {
let styles = context.styles().at(span)?; let styles = context.styles().at(span)?;
@ -588,7 +611,7 @@ impl OutlineEntry {
seq.push(TextElem::packed("\u{202B}")); seq.push(TextElem::packed("\u{202B}"));
} }
seq.push(self.body().at(span)?); seq.push(body);
if rtl { if rtl {
// "Pop Directional Formatting" // "Pop Directional Formatting"
@ -613,7 +636,7 @@ impl OutlineEntry {
// Add the page number. The word joiner in front ensures that the page // Add the page number. The word joiner in front ensures that the page
// number doesn't stand alone in its line. // number doesn't stand alone in its line.
seq.push(TextElem::packed("\u{2060}")); seq.push(TextElem::packed("\u{2060}"));
seq.push(self.page(engine, context, span)?); seq.push(page);
Ok(Content::sequence(seq)) Ok(Content::sequence(seq))
} }
@ -674,6 +697,27 @@ cast! {
v: Content => v.unpack::<Self>().map_err(|_| "expected outline entry")? v: Content => v.unpack::<Self>().map_err(|_| "expected outline entry")?
} }
fn alt_text(
styles: StyleChain,
prefix: &Option<Content>,
body: &Content,
page: &Content,
) -> EcoString {
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();
let quotes = SmartQuotes::get(
SmartQuoteElem::quotes_in(styles),
TextElem::lang_in(styles),
TextElem::region_in(styles),
SmartQuoteElem::alternative_in(styles),
);
let open = quotes.double_open;
let close = quotes.double_close;
eco_format!("{prefix} {open}{body}{close} {page_str} {page_nr}",)
}
/// Measures the width of a prefix. /// Measures the width of a prefix.
fn measure_prefix( fn measure_prefix(
engine: &mut Engine, engine: &mut Engine,

View File

@ -93,7 +93,7 @@ use crate::model::Numbering;
/// let $a$ be the smallest of the /// let $a$ be the smallest of the
/// three integers. Then, we ... /// three integers. Then, we ...
/// ``` /// ```
#[elem(scope, title = "Paragraph")] #[elem(scope, title = "Paragraph", Locatable)]
pub struct ParElem { pub struct ParElem {
/// The spacing between lines. /// The spacing between lines.
/// ///

View File

@ -338,12 +338,18 @@ fn show_reference(
Smart::Custom(Some(supplement)) => supplement.resolve(engine, styles, [elem])?, Smart::Custom(Some(supplement)) => supplement.resolve(engine, styles, [elem])?,
}; };
let alt = {
let supplement = supplement.plain_text();
let numbering = numbers.plain_text();
eco_format!("{supplement} {numbering}",)
};
let mut content = numbers; let mut content = numbers;
if !supplement.is_empty() { if !supplement.is_empty() {
content = supplement + TextElem::packed("\u{a0}") + content; content = supplement + TextElem::packed("\u{a0}") + content;
} }
Ok(content.linked(Destination::Location(loc))) Ok(content.linked(Destination::Location(loc), Some(alt)))
} }
/// Turn a reference into a citation. /// Turn a reference into a citation.

View File

@ -4,6 +4,7 @@ use crate::foundations::{
elem, Content, NativeElement, Packed, Show, StyleChain, TargetElem, elem, Content, NativeElement, Packed, Show, StyleChain, TargetElem,
}; };
use crate::html::{tag, HtmlElem}; use crate::html::{tag, HtmlElem};
use crate::introspection::Locatable;
use crate::text::{TextElem, WeightDelta}; use crate::text::{TextElem, WeightDelta};
/// Strongly emphasizes content by increasing the font weight. /// 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 /// 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 /// word boundaries. To strongly emphasize part of a word, you have to use the
/// function. /// function.
#[elem(title = "Strong Emphasis", keywords = ["bold", "weight"], Show)] #[elem(title = "Strong Emphasis", keywords = ["bold", "weight"], Locatable, Show)]
pub struct StrongElem { pub struct StrongElem {
/// The delta to apply on the font weight. /// The delta to apply on the font weight.
/// ///

View File

@ -1,16 +1,18 @@
use std::num::{NonZeroU32, NonZeroUsize}; use std::num::{NonZeroU32, NonZeroUsize};
use std::sync::Arc; use std::sync::Arc;
use ecow::EcoString;
use typst_macros::Cast;
use typst_utils::NonZeroExt; use typst_utils::NonZeroExt;
use crate::diag::{bail, HintedStrResult, HintedString, SourceResult}; use crate::diag::{bail, HintedStrResult, HintedString, SourceResult};
use crate::engine::Engine; use crate::engine::Engine;
use crate::foundations::{ use crate::foundations::{
cast, elem, scope, Content, NativeElement, Packed, Show, Smart, StyleChain, cast, dict, elem, scope, Content, Dict, NativeElement, Packed, Show, Smart,
TargetElem, StyleChain, TargetElem,
}; };
use crate::html::{attr, tag, HtmlAttrs, HtmlElem, HtmlTag}; 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::grid::resolve::{table_to_cellgrid, Cell, CellGrid, Entry};
use crate::layout::{ use crate::layout::{
show_grid_cell, Abs, Alignment, BlockElem, Celled, GridCell, GridFooter, GridHLine, show_grid_cell, Abs, Alignment, BlockElem, Celled, GridCell, GridFooter, GridHLine,
@ -121,7 +123,7 @@ use crate::visualize::{Paint, Stroke};
/// [Robert], b, a, b, /// [Robert], b, a, b,
/// ) /// )
/// ``` /// ```
#[elem(scope, Show, LocalName, Figurable)] #[elem(scope, Locatable, Show, LocalName, Figurable)]
pub struct TableElem { pub struct TableElem {
/// The column sizes. See the [grid documentation]($grid) for more /// The column sizes. See the [grid documentation]($grid) for more
/// information on track sizing. /// information on track sizing.
@ -237,6 +239,9 @@ pub struct TableElem {
#[default(Celled::Value(Sides::splat(Some(Abs::pt(5.0).into()))))] #[default(Celled::Value(Sides::splat(Some(Abs::pt(5.0).into()))))]
pub inset: Celled<Sides<Option<Rel<Length>>>>, 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 /// The contents of the table cells, plus any extra table lines specified
/// with the [`table.hline`]($table.hline) and /// with the [`table.hline`]($table.hline) and
/// [`table.vline`]($table.vline) elements. /// [`table.vline`]($table.vline) elements.
@ -531,7 +536,7 @@ impl TryFrom<Content> for TableItem {
/// [7.34], [57], [2], /// [7.34], [57], [2],
/// ) /// )
/// ``` /// ```
#[elem(name = "header", title = "Table Header")] #[elem(name = "header", title = "Table Header", Locatable)]
pub struct TableHeader { pub struct TableHeader {
/// Whether this header should be repeated across pages. /// Whether this header should be repeated across pages.
#[default(true)] #[default(true)]
@ -561,7 +566,7 @@ pub struct TableHeader {
/// totals, or other information that should be visible on every page. /// totals, or other information that should be visible on every page.
/// ///
/// No other table cells may be placed after the footer. /// 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 { pub struct TableFooter {
/// Whether this footer should be repeated across pages. /// Whether this footer should be repeated across pages.
#[default(true)] #[default(true)]
@ -604,7 +609,7 @@ pub struct TableFooter {
/// [19:00], [Day 1 Attendee Mixer], /// [19:00], [Day 1 Attendee Mixer],
/// ) /// )
/// ``` /// ```
#[elem(name = "hline", title = "Table Horizontal Line")] #[elem(name = "hline", title = "Table Horizontal Line", Locatable)]
pub struct TableHLine { pub struct TableHLine {
/// The row above which the horizontal line is placed (zero-indexed). /// The row above which the horizontal line is placed (zero-indexed).
/// Functions identically to the `y` field in [`grid.hline`]($grid.hline.y). /// 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 /// 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 /// `stroke`]($table.cell.stroke) field instead if the line you want to place is
/// part of all your tables' designs. /// part of all your tables' designs.
#[elem(name = "vline", title = "Table Vertical Line")] #[elem(name = "vline", title = "Table Vertical Line", Locatable)]
pub struct TableVLine { pub struct TableVLine {
/// The column before which the horizontal line is placed (zero-indexed). /// The column before which the horizontal line is placed (zero-indexed).
/// Functions identically to the `x` field in [`grid.vline`]($grid.vline). /// Functions identically to the `x` field in [`grid.vline`]($grid.vline).
@ -770,7 +775,7 @@ pub struct TableVLine {
/// [Vikram], [49], [Perseverance], /// [Vikram], [49], [Perseverance],
/// ) /// )
/// ``` /// ```
#[elem(name = "cell", title = "Table Cell", Show)] #[elem(name = "cell", title = "Table Cell", Locatable, Show)]
pub struct TableCell { pub struct TableCell {
/// The cell's body. /// The cell's body.
#[required] #[required]
@ -806,6 +811,9 @@ pub struct TableCell {
#[fold] #[fold]
pub stroke: Sides<Option<Option<Arc<Stroke>>>>, pub stroke: Sides<Option<Option<Arc<Stroke>>>>,
// TODO: feature gate
pub kind: Smart<TableCellKind>,
/// Whether rows spanned by this cell can be placed in different pages. /// Whether rows spanned by this cell can be placed in different pages.
/// When equal to `{auto}`, a cell spanning only fixed-size rows is /// When equal to `{auto}`, a cell spanning only fixed-size rows is
/// unbreakable, while a cell spanning at least one `{auto}`-sized row is /// unbreakable, while a cell spanning at least one `{auto}`-sized row is
@ -843,3 +851,65 @@ impl From<Content> for TableCell {
value.unpack::<Self>().unwrap_or_else(Self::new) value.unpack::<Self>().unwrap_or_else(Self::new)
} }
} }
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)]
pub enum TableCellKind {
Header(NonZeroU32, TableHeaderScope),
Footer,
#[default]
Data,
}
cast! {
TableCellKind,
self => match self {
Self::Header(level, scope) => dict! { "level" => level, "scope" => scope }.into_value(),
Self::Footer => "footer".into_value(),
Self::Data => "data".into_value(),
},
"header" => Self::Header(NonZeroU32::ONE, TableHeaderScope::default()),
"footer" => Self::Footer,
"data" => Self::Data,
mut dict: Dict => {
// TODO: have a `pdf.header` function instead?
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)]
enum HeaderKind {
Header,
}
dict.take("kind")?.cast::<HeaderKind>()?;
let level = dict.take("level").ok().map(|v| v.cast()).transpose()?;
let scope = dict.take("scope").ok().map(|v| v.cast()).transpose()?;
dict.finish(&["kind", "level", "scope"])?;
Self::Header(level.unwrap_or(NonZeroU32::ONE), scope.unwrap_or_default())
},
}
/// The scope of a table header cell.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash, Cast)]
pub enum TableHeaderScope {
/// The header cell refers to both the row and the column.
Both,
/// The header cell refers to the column.
#[default]
Column,
/// The header cell refers to the row.
Row,
}
impl TableHeaderScope {
pub fn refers_to_column(&self) -> bool {
match self {
TableHeaderScope::Both => true,
TableHeaderScope::Column => true,
TableHeaderScope::Row => false,
}
}
pub fn refers_to_row(&self) -> bool {
match self {
TableHeaderScope::Both => true,
TableHeaderScope::Column => false,
TableHeaderScope::Row => true,
}
}
}

View File

@ -7,6 +7,7 @@ use crate::foundations::{
Styles, TargetElem, Styles, TargetElem,
}; };
use crate::html::{tag, HtmlElem}; use crate::html::{tag, HtmlElem};
use crate::introspection::Locatable;
use crate::layout::{Em, HElem, Length, Sides, StackChild, StackElem, VElem}; use crate::layout::{Em, HElem, Length, Sides, StackChild, StackElem, VElem};
use crate::model::{ListItemLike, ListLike, ParElem, ParbreakElem}; use crate::model::{ListItemLike, ListLike, ParElem, ParbreakElem};
use crate::text::TextElem; use crate::text::TextElem;
@ -27,7 +28,7 @@ use crate::text::TextElem;
/// # Syntax /// # Syntax
/// This function also has dedicated syntax: Starting a line with a slash, /// 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. /// 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 { pub struct TermsElem {
/// Defines the default [spacing]($terms.spacing) of the term list. If it is /// Defines the default [spacing]($terms.spacing) of the term list. If it is
/// `{false}`, the items are spaced apart with /// `{false}`, the items are spaced apart with
@ -205,7 +206,7 @@ impl Show for Packed<TermsElem> {
} }
/// A term list item. /// A term list item.
#[elem(name = "item", title = "Term List Item")] #[elem(name = "item", title = "Term List Item", Locatable)]
pub struct TermItem { pub struct TermItem {
/// The term described by the list item. /// The term described by the list item.
#[required] #[required]

View File

@ -0,0 +1,212 @@
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;
use crate::model::TableHeaderScope;
// 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,
}
/// 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())
}
}

View File

@ -1,7 +1,9 @@
//! PDF-specific functionality. //! PDF-specific functionality.
mod accessibility;
mod embed; mod embed;
pub use self::accessibility::*;
pub use self::embed::*; pub use self::embed::*;
use crate::foundations::{Module, Scope}; use crate::foundations::{Module, Scope};
@ -11,5 +13,7 @@ pub fn module() -> Module {
let mut pdf = Scope::deduplicating(); let mut pdf = Scope::deduplicating();
pdf.start_category(crate::Category::Pdf); pdf.start_category(crate::Category::Pdf);
pdf.define_elem::<EmbedElem>(); pdf.define_elem::<EmbedElem>();
pdf.define_elem::<PdfTagElem>();
pdf.define_elem::<ArtifactElem>();
Module::new("pdf", pdf) Module::new("pdf", pdf)
} }

View File

@ -6,6 +6,7 @@ use crate::foundations::{
elem, Content, NativeElement, Packed, Show, Smart, StyleChain, TargetElem, elem, Content, NativeElement, Packed, Show, Smart, StyleChain, TargetElem,
}; };
use crate::html::{attr, tag, HtmlElem}; use crate::html::{attr, tag, HtmlElem};
use crate::introspection::Locatable;
use crate::layout::{Abs, Corners, Length, Rel, Sides}; use crate::layout::{Abs, Corners, Length, Rel, Sides};
use crate::text::{BottomEdge, BottomEdgeMetric, TextElem, TopEdge, TopEdgeMetric}; use crate::text::{BottomEdge, BottomEdgeMetric, TextElem, TopEdge, TopEdgeMetric};
use crate::visualize::{Color, FixedStroke, Paint, Stroke}; use crate::visualize::{Color, FixedStroke, Paint, Stroke};
@ -16,7 +17,7 @@ use crate::visualize::{Color, FixedStroke, Paint, Stroke};
/// ```example /// ```example
/// This is #underline[important]. /// This is #underline[important].
/// ``` /// ```
#[elem(Show)] #[elem(Locatable, Show)]
pub struct UnderlineElem { pub struct UnderlineElem {
/// How to [stroke] the line. /// How to [stroke] the line.
/// ///
@ -112,7 +113,7 @@ impl Show for Packed<UnderlineElem> {
/// ```example /// ```example
/// #overline[A line over text.] /// #overline[A line over text.]
/// ``` /// ```
#[elem(Show)] #[elem(Locatable, Show)]
pub struct OverlineElem { pub struct OverlineElem {
/// How to [stroke] the line. /// How to [stroke] the line.
/// ///
@ -211,7 +212,7 @@ impl Show for Packed<OverlineElem> {
/// ```example /// ```example
/// This is #strike[not] relevant. /// This is #strike[not] relevant.
/// ``` /// ```
#[elem(title = "Strikethrough", Show)] #[elem(title = "Strikethrough", Locatable, Show)]
pub struct StrikeElem { pub struct StrikeElem {
/// How to [stroke] the line. /// How to [stroke] the line.
/// ///
@ -292,7 +293,7 @@ impl Show for Packed<StrikeElem> {
/// ```example /// ```example
/// This is #highlight[important]. /// This is #highlight[important].
/// ``` /// ```
#[elem(Show)] #[elem(Locatable, Show)]
pub struct HighlightElem { pub struct HighlightElem {
/// The color to highlight the text with. /// The color to highlight the text with.
/// ///

View File

@ -228,6 +228,10 @@ pub struct FontMetrics {
pub underline: LineMetrics, pub underline: LineMetrics,
/// Recommended metrics for an overline. /// Recommended metrics for an overline.
pub overline: LineMetrics, pub overline: LineMetrics,
/// Metrics for subscripts, if provided by the font.
pub subscript: Option<ScriptMetrics>,
/// Metrics for superscripts, if provided by the font.
pub superscript: Option<ScriptMetrics>,
} }
impl FontMetrics { impl FontMetrics {
@ -240,6 +244,7 @@ impl FontMetrics {
let cap_height = ttf.capital_height().filter(|&h| h > 0).map_or(ascender, to_em); let cap_height = ttf.capital_height().filter(|&h| h > 0).map_or(ascender, to_em);
let x_height = ttf.x_height().filter(|&h| h > 0).map_or(ascender, to_em); let x_height = ttf.x_height().filter(|&h| h > 0).map_or(ascender, to_em);
let descender = to_em(ttf.typographic_descender().unwrap_or(ttf.descender())); let descender = to_em(ttf.typographic_descender().unwrap_or(ttf.descender()));
let strikeout = ttf.strikeout_metrics(); let strikeout = ttf.strikeout_metrics();
let underline = ttf.underline_metrics(); let underline = ttf.underline_metrics();
@ -262,6 +267,20 @@ impl FontMetrics {
thickness: underline.thickness, thickness: underline.thickness,
}; };
let subscript = ttf.subscript_metrics().map(|metrics| ScriptMetrics {
width: to_em(metrics.x_size),
height: to_em(metrics.y_size),
horizontal_offset: to_em(metrics.x_offset),
vertical_offset: -to_em(metrics.y_offset),
});
let superscript = ttf.superscript_metrics().map(|metrics| ScriptMetrics {
width: to_em(metrics.x_size),
height: to_em(metrics.y_size),
horizontal_offset: to_em(metrics.x_offset),
vertical_offset: to_em(metrics.y_offset),
});
Self { Self {
units_per_em, units_per_em,
ascender, ascender,
@ -271,6 +290,8 @@ impl FontMetrics {
strikethrough, strikethrough,
underline, underline,
overline, overline,
superscript,
subscript,
} }
} }
@ -296,6 +317,24 @@ pub struct LineMetrics {
pub thickness: Em, pub thickness: Em,
} }
/// Metrics for subscripts or superscripts.
#[derive(Debug, Copy, Clone)]
pub struct ScriptMetrics {
/// The width of those scripts, relative to the outer font size.
pub width: Em,
/// The height of those scripts, relative to the outer font size.
pub height: Em,
/// The horizontal (to the right) offset of those scripts, relative to the
/// outer font size.
///
/// This is used for italic correction.
pub horizontal_offset: Em,
/// The vertical (to the top) offset of those scripts, relative to the outer font size.
///
/// For superscripts, this is positive. For subscripts, this is negative.
pub vertical_offset: Em,
}
/// Identifies a vertical metric of a font. /// Identifies a vertical metric of a font.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)]
pub enum VerticalFontMetric { pub enum VerticalFontMetric {

View File

@ -755,6 +755,12 @@ pub struct TextElem {
#[internal] #[internal]
#[ghost] #[ghost]
pub smallcaps: Option<Smallcaps>, pub smallcaps: Option<Smallcaps>,
/// The configuration for superscripts or subscripts, if one of them is
/// enabled.
#[internal]
#[ghost]
pub shift_settings: Option<ShiftSettings>,
} }
impl TextElem { impl TextElem {

View File

@ -20,6 +20,7 @@ use crate::foundations::{
PlainText, Show, ShowSet, Smart, StyleChain, Styles, Synthesize, TargetElem, PlainText, Show, ShowSet, Smart, StyleChain, Styles, Synthesize, TargetElem,
}; };
use crate::html::{tag, HtmlElem}; use crate::html::{tag, HtmlElem};
use crate::introspection::Locatable;
use crate::layout::{BlockBody, BlockElem, Em, HAlignment}; use crate::layout::{BlockBody, BlockElem, Em, HAlignment};
use crate::loading::{DataSource, Load}; use crate::loading::{DataSource, Load};
use crate::model::{Figurable, ParElem}; use crate::model::{Figurable, ParElem};
@ -78,6 +79,7 @@ use crate::World;
scope, scope,
title = "Raw Text / Code", title = "Raw Text / Code",
Synthesize, Synthesize,
Locatable,
Show, Show,
ShowSet, ShowSet,
LocalName, 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 /// 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 /// number, the raw non-highlighted text, the highlighted text, and whether it
/// is the first or last line of the raw block. /// 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 { pub struct RawLine {
/// The line number of the raw line inside of the raw block, starts at 1. /// The line number of the raw line inside of the raw block, starts at 1.
#[required] #[required]

View File

@ -1,14 +1,14 @@
use ecow::EcoString;
use crate::diag::SourceResult; use crate::diag::SourceResult;
use crate::engine::Engine; use crate::engine::Engine;
use crate::foundations::{ use crate::foundations::{
elem, Content, NativeElement, Packed, SequenceElem, Show, StyleChain, TargetElem, elem, Content, NativeElement, Packed, Show, Smart, StyleChain, TargetElem,
}; };
use crate::html::{tag, HtmlElem}; use crate::html::{tag, HtmlElem};
use crate::introspection::Locatable;
use crate::layout::{Em, Length}; use crate::layout::{Em, Length};
use crate::text::{variant, SpaceElem, TextElem, TextSize}; use crate::text::{FontMetrics, TextElem, TextSize};
use crate::World; use ttf_parser::Tag;
use typst_library::text::ScriptMetrics;
/// Renders text in subscript. /// Renders text in subscript.
/// ///
@ -18,13 +18,18 @@ use crate::World;
/// ```example /// ```example
/// Revenue#sub[yearly] /// Revenue#sub[yearly]
/// ``` /// ```
#[elem(title = "Subscript", Show)] #[elem(title = "Subscript", Locatable, Show)]
pub struct SubElem { pub struct SubElem {
/// Whether to prefer the dedicated subscript characters of the font. /// Whether to create artificial subscripts by lowering and scaling down
/// regular glyphs.
/// ///
/// If this is enabled, Typst first tries to transform the text to subscript /// Ideally, subscripts glyphs are provided by the font (using the `subs`
/// codepoints. If that fails, it falls back to rendering lowered and shrunk /// OpenType feature). Otherwise, Typst is able to synthesize subscripts.
/// normal letters. ///
/// When this is set to `{false}`, synthesized glyphs will be used
/// regardless of whether the font provides dedicated subscript glyphs. When
/// `{true}`, synthesized glyphs may still be used in case the font does not
/// provide the necessary subscript glyphs.
/// ///
/// ```example /// ```example
/// N#sub(typographic: true)[1] /// N#sub(typographic: true)[1]
@ -33,17 +38,27 @@ pub struct SubElem {
#[default(true)] #[default(true)]
pub typographic: bool, pub typographic: bool,
/// The baseline shift for synthetic subscripts. Does not apply if /// The downward baseline shift for synthesized subscripts.
/// `typographic` is true and the font has subscript codepoints for the ///
/// given `body`. /// This only applies to synthesized subscripts. In other words, this has no
#[default(Em::new(0.2).into())] /// effect if `typographic` is `{true}` and the font provides the necessary
pub baseline: Length, /// subscript glyphs.
///
/// If set to `{auto}`, the baseline is shifted according to the metrics
/// provided by the font, with a fallback to `{0.2em}` in case the font does
/// not define the necessary metrics.
pub baseline: Smart<Length>,
/// The font size for synthetic subscripts. Does not apply if /// The font size for synthesized subscripts.
/// `typographic` is true and the font has subscript codepoints for the ///
/// given `body`. /// This only applies to synthesized subscripts. In other words, this has no
#[default(TextSize(Em::new(0.6).into()))] /// effect if `typographic` is `{true}` and the font provides the necessary
pub size: TextSize, /// subscript glyphs.
///
/// If set to `{auto}`, the size is scaled according to the metrics provided
/// by the font, with a fallback to `{0.6em}` in case the font does not
/// define the necessary metrics.
pub size: Smart<TextSize>,
/// The text to display in subscript. /// The text to display in subscript.
#[required] #[required]
@ -52,7 +67,7 @@ pub struct SubElem {
impl Show for Packed<SubElem> { impl Show for Packed<SubElem> {
#[typst_macros::time(name = "sub", span = self.span())] #[typst_macros::time(name = "sub", span = self.span())]
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> { fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
let body = self.body.clone(); let body = self.body.clone();
if TargetElem::target_in(styles).is_html() { if TargetElem::target_in(styles).is_html() {
@ -62,17 +77,14 @@ impl Show for Packed<SubElem> {
.spanned(self.span())); .spanned(self.span()));
} }
if self.typographic(styles) { show_script(
if let Some(text) = convert_script(&body, true) { styles,
if is_shapable(engine, &text, styles) { body,
return Ok(TextElem::packed(text)); self.typographic(styles),
} self.baseline(styles),
} self.size(styles),
}; ScriptKind::Sub,
)
Ok(body
.styled(TextElem::set_baseline(self.baseline(styles)))
.styled(TextElem::set_size(self.size(styles))))
} }
} }
@ -84,13 +96,18 @@ impl Show for Packed<SubElem> {
/// ```example /// ```example
/// 1#super[st] try! /// 1#super[st] try!
/// ``` /// ```
#[elem(title = "Superscript", Show)] #[elem(title = "Superscript", Locatable, Show)]
pub struct SuperElem { pub struct SuperElem {
/// Whether to prefer the dedicated superscript characters of the font. /// Whether to create artificial superscripts by raising and scaling down
/// regular glyphs.
/// ///
/// If this is enabled, Typst first tries to transform the text to /// Ideally, superscripts glyphs are provided by the font (using the `sups`
/// superscript codepoints. If that fails, it falls back to rendering /// OpenType feature). Otherwise, Typst is able to synthesize superscripts.
/// raised and shrunk normal letters. ///
/// When this is set to `{false}`, synthesized glyphs will be used
/// regardless of whether the font provides dedicated superscript glyphs.
/// When `{true}`, synthesized glyphs may still be used in case the font
/// does not provide the necessary superscript glyphs.
/// ///
/// ```example /// ```example
/// N#super(typographic: true)[1] /// N#super(typographic: true)[1]
@ -99,17 +116,31 @@ pub struct SuperElem {
#[default(true)] #[default(true)]
pub typographic: bool, pub typographic: bool,
/// The baseline shift for synthetic superscripts. Does not apply if /// The downward baseline shift for synthesized superscripts.
/// `typographic` is true and the font has superscript codepoints for the ///
/// given `body`. /// This only applies to synthesized superscripts. In other words, this has
#[default(Em::new(-0.5).into())] /// no effect if `typographic` is `{true}` and the font provides the
pub baseline: Length, /// necessary superscript glyphs.
///
/// If set to `{auto}`, the baseline is shifted according to the metrics
/// provided by the font, with a fallback to `{-0.5em}` in case the font
/// does not define the necessary metrics.
///
/// Note that, since the baseline shift is applied downward, you will need
/// to provide a negative value for the content to appear as raised above
/// the normal baseline.
pub baseline: Smart<Length>,
/// The font size for synthetic superscripts. Does not apply if /// The font size for synthesized superscripts.
/// `typographic` is true and the font has superscript codepoints for the ///
/// given `body`. /// This only applies to synthesized superscripts. In other words, this has
#[default(TextSize(Em::new(0.6).into()))] /// no effect if `typographic` is `{true}` and the font provides the
pub size: TextSize, /// necessary superscript glyphs.
///
/// If set to `{auto}`, the size is scaled according to the metrics provided
/// by the font, with a fallback to `{0.6em}` in case the font does not
/// define the necessary metrics.
pub size: Smart<TextSize>,
/// The text to display in superscript. /// The text to display in superscript.
#[required] #[required]
@ -118,7 +149,7 @@ pub struct SuperElem {
impl Show for Packed<SuperElem> { impl Show for Packed<SuperElem> {
#[typst_macros::time(name = "super", span = self.span())] #[typst_macros::time(name = "super", span = self.span())]
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> { fn show(&self, _: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
let body = self.body.clone(); let body = self.body.clone();
if TargetElem::target_in(styles).is_html() { if TargetElem::target_in(styles).is_html() {
@ -128,104 +159,102 @@ impl Show for Packed<SuperElem> {
.spanned(self.span())); .spanned(self.span()));
} }
if self.typographic(styles) { show_script(
if let Some(text) = convert_script(&body, false) { styles,
if is_shapable(engine, &text, styles) { body,
return Ok(TextElem::packed(text)); self.typographic(styles),
} self.baseline(styles),
} self.size(styles),
}; ScriptKind::Super,
)
Ok(body
.styled(TextElem::set_baseline(self.baseline(styles)))
.styled(TextElem::set_size(self.size(styles))))
} }
} }
/// Find and transform the text contained in `content` to the given script kind fn show_script(
/// if and only if it only consists of `Text`, `Space`, and `Empty` leaves. styles: StyleChain,
fn convert_script(content: &Content, sub: bool) -> Option<EcoString> { body: Content,
if content.is::<SpaceElem>() { typographic: bool,
Some(' '.into()) baseline: Smart<Length>,
} else if let Some(elem) = content.to_packed::<TextElem>() { size: Smart<TextSize>,
if sub { kind: ScriptKind,
elem.text.chars().map(to_subscript_codepoint).collect() ) -> SourceResult<Content> {
} else { let font_size = TextElem::size_in(styles);
elem.text.chars().map(to_superscript_codepoint).collect() Ok(body.styled(TextElem::set_shift_settings(Some(ShiftSettings {
} typographic,
} else if let Some(sequence) = content.to_packed::<SequenceElem>() { shift: baseline.map(|l| -Em::from_length(l, font_size)),
sequence size: size.map(|t| Em::from_length(t.0, font_size)),
.children kind,
.iter() }))))
.map(|item| convert_script(item, sub))
.collect()
} else {
None
}
} }
/// Checks whether the first retrievable family contains all code points of the /// Configuration values for sub- or superscript text.
/// given string. #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
fn is_shapable(engine: &Engine, text: &str, styles: StyleChain) -> bool { pub struct ShiftSettings {
let world = engine.world; /// Whether the OpenType feature should be used if possible.
for family in TextElem::font_in(styles) { pub typographic: bool,
if let Some(font) = world /// The baseline shift of the script, relative to the outer text size.
.book() ///
.select(family.as_str(), variant(styles)) /// For superscripts, this is positive. For subscripts, this is negative. A
.and_then(|id| world.font(id)) /// value of [`Smart::Auto`] indicates that the value should be obtained
{ /// from font metrics.
let covers = family.covers(); pub shift: Smart<Em>,
return text.chars().all(|c| { /// The size of the script, relative to the outer text size.
covers.is_none_or(|cov| cov.is_match(c.encode_utf8(&mut [0; 4]))) ///
&& font.ttf().glyph_index(c).is_some() /// A value of [`Smart::Auto`] indicates that the value should be obtained
}); /// from font metrics.
pub size: Smart<Em>,
/// The kind of script (either a subscript, or a superscript).
///
/// This is used to know which OpenType table to use to resolve
/// [`Smart::Auto`] values.
pub kind: ScriptKind,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum ScriptKind {
Sub,
Super,
}
impl ScriptKind {
/// Returns the default metrics for this script kind.
///
/// This can be used as a last resort if neither the user nor the font
/// provided those metrics.
pub fn default_metrics(self) -> &'static ScriptMetrics {
match self {
Self::Sub => &DEFAULT_SUBSCRIPT_METRICS,
Self::Super => &DEFAULT_SUPERSCRIPT_METRICS,
} }
} }
false /// Reads the script metrics from the font table for to this script kind.
} pub fn read_metrics(self, font_metrics: &FontMetrics) -> &ScriptMetrics {
match self {
Self::Sub => font_metrics.subscript.as_ref(),
Self::Super => font_metrics.superscript.as_ref(),
}
.unwrap_or(self.default_metrics())
}
/// Convert a character to its corresponding Unicode superscript. /// The corresponding OpenType feature.
fn to_superscript_codepoint(c: char) -> Option<char> { pub const fn feature(self) -> Tag {
match c { match self {
'1' => Some('¹'), Self::Sub => Tag::from_bytes(b"subs"),
'2' => Some('²'), Self::Super => Tag::from_bytes(b"sups"),
'3' => Some('³'), }
'0' | '4'..='9' => char::from_u32(c as u32 - '0' as u32 + '⁰' as u32),
'+' => Some('⁺'),
'' => Some('⁻'),
'=' => Some('⁼'),
'(' => Some('⁽'),
')' => Some('⁾'),
'n' => Some('ⁿ'),
'i' => Some('ⁱ'),
' ' => Some(' '),
_ => None,
} }
} }
static DEFAULT_SUBSCRIPT_METRICS: ScriptMetrics = ScriptMetrics {
width: Em::new(0.6),
height: Em::new(0.6),
horizontal_offset: Em::zero(),
vertical_offset: Em::new(-0.2),
};
/// Convert a character to its corresponding Unicode subscript. static DEFAULT_SUPERSCRIPT_METRICS: ScriptMetrics = ScriptMetrics {
fn to_subscript_codepoint(c: char) -> Option<char> { width: Em::new(0.6),
match c { height: Em::new(0.6),
'0'..='9' => char::from_u32(c as u32 - '0' as u32 + '₀' as u32), horizontal_offset: Em::zero(),
'+' => Some('₊'), vertical_offset: Em::new(0.5),
'' => Some('₋'), };
'=' => Some('₌'),
'(' => Some('₍'),
')' => Some('₎'),
'a' => Some('ₐ'),
'e' => Some('ₑ'),
'o' => Some('ₒ'),
'x' => Some('ₓ'),
'h' => Some('ₕ'),
'k' => Some('ₖ'),
'l' => Some('ₗ'),
'm' => Some('ₘ'),
'n' => Some('ₙ'),
'p' => Some('ₚ'),
's' => Some('ₛ'),
't' => Some('ₜ'),
' ' => Some(' '),
_ => None,
}
}

View File

@ -21,6 +21,7 @@ use crate::foundations::{
cast, elem, func, scope, Bytes, Cast, Content, Derived, NativeElement, Packed, Show, cast, elem, func, scope, Bytes, Cast, Content, Derived, NativeElement, Packed, Show,
Smart, StyleChain, Smart, StyleChain,
}; };
use crate::introspection::Locatable;
use crate::layout::{BlockElem, Length, Rel, Sizing}; use crate::layout::{BlockElem, Length, Rel, Sizing};
use crate::loading::{DataSource, Load, LoadSource, Loaded, Readable}; use crate::loading::{DataSource, Load, LoadSource, Loaded, Readable};
use crate::model::Figurable; 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 { pub struct ImageElem {
/// A [path]($syntax/#paths) to an image file or raw bytes making up an /// A [path]($syntax/#paths) to an image file or raw bytes making up an
/// image in one of the supported [formats]($image.format). /// image in one of the supported [formats]($image.format).

View File

@ -2,7 +2,6 @@ use std::collections::{BTreeMap, HashMap, HashSet};
use std::num::NonZeroU64; use std::num::NonZeroU64;
use ecow::{eco_format, EcoVec}; use ecow::{eco_format, EcoVec};
use krilla::annotation::Annotation;
use krilla::configure::{Configuration, ValidationError, Validator}; use krilla::configure::{Configuration, ValidationError, Validator};
use krilla::destination::{NamedDestination, XyzDestination}; use krilla::destination::{NamedDestination, XyzDestination};
use krilla::embed::EmbedError; use krilla::embed::EmbedError;
@ -14,7 +13,7 @@ use krilla::{Document, SerializeSettings};
use krilla_svg::render_svg_glyph; use krilla_svg::render_svg_glyph;
use typst_library::diag::{bail, error, SourceDiagnostic, SourceResult}; use typst_library::diag::{bail, error, SourceDiagnostic, SourceResult};
use typst_library::foundations::{NativeElement, Repr}; use typst_library::foundations::{NativeElement, Repr};
use typst_library::introspection::Location; use typst_library::introspection::{Location, Tag};
use typst_library::layout::{ use typst_library::layout::{
Abs, Frame, FrameItem, GroupItem, PagedDocument, Size, Transform, Abs, Frame, FrameItem, GroupItem, PagedDocument, Size, Transform,
}; };
@ -25,11 +24,12 @@ use typst_syntax::Span;
use crate::embed::embed_files; use crate::embed::embed_files;
use crate::image::handle_image; use crate::image::handle_image;
use crate::link::handle_link; use crate::link::{handle_link, LinkAnnotation};
use crate::metadata::build_metadata; use crate::metadata::build_metadata;
use crate::outline::build_outline; use crate::outline::build_outline;
use crate::page::PageLabelExt; use crate::page::PageLabelExt;
use crate::shape::handle_shape; use crate::shape::handle_shape;
use crate::tags::{self, Tags};
use crate::text::handle_text; use crate::text::handle_text;
use crate::util::{convert_path, display_font, AbsExt, TransformExt}; use crate::util::{convert_path, display_font, AbsExt, TransformExt};
use crate::PdfOptions; use crate::PdfOptions;
@ -46,7 +46,7 @@ pub fn convert(
xmp_metadata: true, xmp_metadata: true,
cmyk_profile: None, cmyk_profile: None,
configuration: options.standards.config, configuration: options.standards.config,
enable_tagging: false, enable_tagging: !options.disable_tags,
render_svg_glyph_fn: render_svg_glyph, render_svg_glyph_fn: render_svg_glyph,
}; };
@ -54,6 +54,7 @@ pub fn convert(
let page_index_converter = PageIndexConverter::new(typst_document, options); let page_index_converter = PageIndexConverter::new(typst_document, options);
let named_destinations = let named_destinations =
collect_named_destinations(typst_document, &page_index_converter); collect_named_destinations(typst_document, &page_index_converter);
let mut gc = GlobalContext::new( let mut gc = GlobalContext::new(
typst_document, typst_document,
options, options,
@ -66,6 +67,7 @@ pub fn convert(
document.set_outline(build_outline(&gc)); document.set_outline(build_outline(&gc));
document.set_metadata(build_metadata(&gc)); document.set_metadata(build_metadata(&gc));
document.set_tag_tree(gc.tags.build_tree());
finish(document, gc, options.standards.config) finish(document, gc, options.standards.config)
} }
@ -115,9 +117,7 @@ fn convert_pages(gc: &mut GlobalContext, document: &mut Document) -> SourceResul
surface.finish(); surface.finish();
for annotation in fc.annotations { tags::add_annotations(gc, &mut page, fc.link_annotations);
page.add_annotation(annotation);
}
} }
} }
@ -171,14 +171,14 @@ impl State {
/// Context needed for converting a single frame. /// Context needed for converting a single frame.
pub(crate) struct FrameContext { pub(crate) struct FrameContext {
states: Vec<State>, states: Vec<State>,
annotations: Vec<Annotation>, link_annotations: Vec<LinkAnnotation>,
} }
impl FrameContext { impl FrameContext {
pub(crate) fn new(size: Size) -> Self { pub(crate) fn new(size: Size) -> Self {
Self { Self {
states: vec![State::new(size)], states: vec![State::new(size)],
annotations: vec![], link_annotations: Vec::new(),
} }
} }
@ -198,8 +198,18 @@ impl FrameContext {
self.states.last_mut().unwrap() self.states.last_mut().unwrap()
} }
pub(crate) fn push_annotation(&mut self, annotation: Annotation) { pub(crate) fn get_link_annotation(
self.annotations.push(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. /// The languages used throughout the document.
pub(crate) languages: BTreeMap<Lang, usize>, pub(crate) languages: BTreeMap<Lang, usize>,
pub(crate) page_index_converter: PageIndexConverter, pub(crate) page_index_converter: PageIndexConverter,
/// Tagged PDF context.
pub(crate) tags: Tags,
} }
impl<'a> GlobalContext<'a> { impl<'a> GlobalContext<'a> {
@ -244,6 +256,8 @@ impl<'a> GlobalContext<'a> {
image_spans: HashSet::new(), image_spans: HashSet::new(),
languages: BTreeMap::new(), languages: BTreeMap::new(),
page_index_converter, page_index_converter,
tags: Tags::new(),
} }
} }
} }
@ -278,8 +292,9 @@ pub(crate) fn handle_frame(
FrameItem::Image(image, size, span) => { FrameItem::Image(image, size, span) => {
handle_image(gc, fc, image, *size, surface, *span)? handle_image(gc, fc, image, *size, surface, *span)?
} }
FrameItem::Link(d, s) => handle_link(fc, gc, d, *s), FrameItem::Link(dest, size) => handle_link(fc, gc, dest, *size),
FrameItem::Tag(_) => {} FrameItem::Tag(Tag::Start(elem)) => tags::handle_start(gc, elem)?,
FrameItem::Tag(Tag::End(loc, _)) => tags::handle_end(gc, *loc),
} }
fc.pop(); fc.pop();
@ -294,7 +309,7 @@ pub(crate) fn handle_group(
fc: &mut FrameContext, fc: &mut FrameContext,
group: &GroupItem, group: &GroupItem,
surface: &mut Surface, surface: &mut Surface,
context: &mut GlobalContext, gc: &mut GlobalContext,
) -> SourceResult<()> { ) -> SourceResult<()> {
fc.push(); fc.push();
fc.state_mut().pre_concat(group.transform); 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())); .and_then(|p| p.transform(fc.state().transform.to_krilla()));
if let Some(clip_path) = &clip_path { 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); 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() { if clip_path.is_some() {
surface.pop(); surface.pop();
@ -576,6 +593,16 @@ fn convert_error(
"{prefix} missing document date"; "{prefix} missing document date";
hint: "set the date of the document" 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")
}
} }
} }

View File

@ -4,6 +4,7 @@ use std::sync::{Arc, OnceLock};
use image::{DynamicImage, EncodableLayout, GenericImageView, Rgba}; use image::{DynamicImage, EncodableLayout, GenericImageView, Rgba};
use krilla::image::{BitsPerComponent, CustomImage, ImageColorspace}; use krilla::image::{BitsPerComponent, CustomImage, ImageColorspace};
use krilla::surface::Surface; use krilla::surface::Surface;
use krilla::tagging::SpanTag;
use krilla_svg::{SurfaceExt, SvgSettings}; use krilla_svg::{SurfaceExt, SvgSettings};
use typst_library::diag::{bail, SourceResult}; use typst_library::diag::{bail, SourceResult};
use typst_library::foundations::Smart; use typst_library::foundations::Smart;
@ -14,6 +15,7 @@ use typst_library::visualize::{
use typst_syntax::Span; use typst_syntax::Span;
use crate::convert::{FrameContext, GlobalContext}; use crate::convert::{FrameContext, GlobalContext};
use crate::tags;
use crate::util::{SizeExt, TransformExt}; use crate::util::{SizeExt, TransformExt};
#[typst_macros::time(name = "handle image")] #[typst_macros::time(name = "handle image")]
@ -30,12 +32,11 @@ pub(crate) fn handle_image(
let interpolate = image.scaling() == Smart::Custom(ImageScaling::Smooth); let interpolate = image.scaling() == Smart::Custom(ImageScaling::Smooth);
if let Some(alt) = image.alt() {
surface.start_alt_text(alt);
}
gc.image_spans.insert(span); 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() { match image.kind() {
ImageKind::Raster(raster) => { ImageKind::Raster(raster) => {
let (exif_transform, new_size) = exif_transform(raster, size); 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.pop();
surface.reset_location(); surface.reset_location();

View File

@ -9,6 +9,7 @@ mod outline;
mod page; mod page;
mod paint; mod paint;
mod shape; mod shape;
mod tags;
mod text; mod text;
mod util; mod util;
@ -53,6 +54,11 @@ pub struct PdfOptions<'a> {
pub page_ranges: Option<PageRanges>, pub page_ranges: Option<PageRanges>,
/// A list of PDF standards that Typst will enforce conformance with. /// A list of PDF standards that Typst will enforce conformance with.
pub standards: PdfStandards, 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. /// Encapsulates a list of compatible PDF standards.
@ -104,6 +110,7 @@ impl PdfStandards {
PdfStandard::A_4 => set_validator(Validator::A4)?, PdfStandard::A_4 => set_validator(Validator::A4)?,
PdfStandard::A_4f => set_validator(Validator::A4F)?, PdfStandard::A_4f => set_validator(Validator::A4F)?,
PdfStandard::A_4e => set_validator(Validator::A4E)?, PdfStandard::A_4e => set_validator(Validator::A4E)?,
PdfStandard::Ua_1 => set_validator(Validator::UA1)?,
} }
} }
@ -187,4 +194,7 @@ pub enum PdfStandard {
/// PDF/A-4e. /// PDF/A-4e.
#[serde(rename = "a-4e")] #[serde(rename = "a-4e")]
A_4e, A_4e,
/// PDF/UA-1.
#[serde(rename = "ua-1")]
Ua_1,
} }

View File

@ -1,19 +1,89 @@
use ecow::EcoString;
use krilla::action::{Action, LinkAction}; use krilla::action::{Action, LinkAction};
use krilla::annotation::{LinkAnnotation, Target}; use krilla::annotation::Target;
use krilla::configure::Validator;
use krilla::destination::XyzDestination; use krilla::destination::XyzDestination;
use krilla::geom::Rect; use krilla::geom as kg;
use typst_library::layout::{Abs, Point, Size}; use typst_library::layout::{Abs, Point, Position, Size};
use typst_library::model::Destination; use typst_library::model::Destination;
use crate::convert::{FrameContext, GlobalContext}; use crate::convert::{FrameContext, GlobalContext};
use crate::tags::{self, Placeholder, TagNode};
use crate::util::{AbsExt, PointExt}; 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( pub(crate) fn handle_link(
fc: &mut FrameContext, fc: &mut FrameContext,
gc: &mut GlobalContext, gc: &mut GlobalContext,
dest: &Destination, dest: &Destination,
size: Size, 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 Some((link_id, link)) = gc.tags.find_parent_link() 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_x = Abs::inf();
let mut min_y = Abs::inf(); let mut min_y = Abs::inf();
let mut max_x = -Abs::inf(); let mut max_x = -Abs::inf();
@ -21,7 +91,6 @@ pub(crate) fn handle_link(
let pos = Point::zero(); let pos = Point::zero();
// Compute the bounding box of the transformed link.
for point in [ for point in [
pos, pos,
pos + Point::with_x(size.x), pos + Point::with_x(size.x),
@ -40,55 +109,32 @@ pub(crate) fn handle_link(
let y1 = min_y.to_f32(); let y1 = min_y.to_f32();
let y2 = max_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.
fn bounding_rect(a: kg::Rect, b: kg::Rect) -> kg::Rect {
let pos = match dest { kg::Rect::from_ltrb(
Destination::Url(u) => { a.left().min(b.left()),
fc.push_annotation( a.top().min(b.top()),
LinkAnnotation::new( a.right().max(b.right()),
rect, a.bottom().max(b.bottom()),
None, )
Target::Action(Action::Link(LinkAction::new(u.to_string()))), .unwrap()
) }
.into(),
); fn quadpoints(rect: kg::Rect) -> [kg::Point; 4] {
return; [
} kg::Point::from_xy(rect.left(), rect.bottom()),
Destination::Position(p) => *p, kg::Point::from_xy(rect.right(), rect.bottom()),
Destination::Location(loc) => { kg::Point::from_xy(rect.right(), rect.top()),
if let Some(nd) = gc.loc_to_names.get(loc) { kg::Point::from_xy(rect.left(), rect.top()),
// If a named destination has been registered, it's already guaranteed to ]
// not point to an excluded page. }
fc.push_annotation(
LinkAnnotation::new( fn pos_to_target(gc: &mut GlobalContext, pos: Position) -> Option<Target> {
rect, let page_index = pos.page.get() - 1;
None, let index = gc.page_index_converter.pdf_page_index(page_index)?;
Target::Destination(krilla::destination::Destination::Named(
nd.clone(), let dest = XyzDestination::new(index, pos.point.to_krilla());
)), Some(Target::Destination(krilla::destination::Destination::Xyz(dest)))
)
.into(),
);
return;
} else {
gc.document.introspector.position(*loc)
}
}
};
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(),
);
}
} }

View File

@ -5,8 +5,8 @@ use typst_library::visualize::{Geometry, Shape};
use typst_syntax::Span; use typst_syntax::Span;
use crate::convert::{FrameContext, GlobalContext}; use crate::convert::{FrameContext, GlobalContext};
use crate::paint;
use crate::util::{convert_path, AbsExt, TransformExt}; use crate::util::{convert_path, AbsExt, TransformExt};
use crate::{paint, tags};
#[typst_macros::time(name = "handle shape")] #[typst_macros::time(name = "handle shape")]
pub(crate) fn handle_shape( pub(crate) fn handle_shape(
@ -16,6 +16,9 @@ pub(crate) fn handle_shape(
gc: &mut GlobalContext, gc: &mut GlobalContext,
span: Span, span: Span,
) -> SourceResult<()> { ) -> SourceResult<()> {
let mut handle = tags::start_marked(gc, surface);
let surface = handle.surface();
surface.set_location(span.into_raw().get()); surface.set_location(span.into_raw().get());
surface.push_transform(&fc.state().transform().to_krilla()); surface.push_transform(&fc.state().transform().to_krilla());

View File

@ -0,0 +1,467 @@
use std::cell::OnceCell;
use std::num::NonZeroU32;
use ecow::EcoString;
use krilla::configure::Validator;
use krilla::page::Page;
use krilla::surface::Surface;
use krilla::tagging::{
ArtifactType, ContentTag, Identifier, Node, SpanTag, TableDataCell, Tag, TagBuilder,
TagGroup, TagKind, TagTree,
};
use typst_library::diag::SourceResult;
use typst_library::foundations::{Content, LinkMarker, Packed, StyleChain};
use typst_library::introspection::Location;
use typst_library::layout::RepeatElem;
use typst_library::model::{
Destination, FigureCaption, FigureElem, HeadingElem, Outlinable, OutlineBody,
OutlineEntry, ParElem, TableCell, TableElem,
};
use typst_library::pdf::{ArtifactElem, ArtifactKind, PdfTagElem, PdfTagKind};
use typst_library::visualize::ImageElem;
use crate::convert::GlobalContext;
use crate::link::LinkAnnotation;
use crate::tags::outline::OutlineCtx;
use crate::tags::table::TableCtx;
mod outline;
mod table;
pub(crate) fn handle_start(gc: &mut GlobalContext, elem: &Content) -> SourceResult<()> {
if gc.tags.in_artifact.is_some() {
// Don't nest artifacts
return Ok(());
}
let loc = elem.location().expect("elem to be locatable");
if let Some(artifact) = elem.to_packed::<ArtifactElem>() {
let kind = artifact.kind(StyleChain::default());
start_artifact(gc, loc, kind);
return Ok(());
} else if let Some(_) = elem.to_packed::<RepeatElem>() {
start_artifact(gc, loc, ArtifactKind::Other);
return Ok(());
}
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 Ok(());
} else if let Some(entry) = elem.to_packed::<OutlineEntry>() {
push_stack(gc, loc, StackEntryKind::OutlineEntry(entry.clone()))?;
return Ok(());
} 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 Ok(());
} 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>() {
let table_id = gc.tags.next_table_id();
let ctx = TableCtx::new(table_id, table.clone());
push_stack(gc, loc, StackEntryKind::Table(ctx))?;
return Ok(());
} else if let Some(cell) = elem.to_packed::<TableCell>() {
let table_ctx = gc.tags.parent_table();
// Only repeated table headers and footer cells are layed out multiple
// times. Mark duplicate headers as artifacts, since they have no
// semantic meaning in the tag tree, which doesn't use page breaks for
// it's semantic structure.
if table_ctx.is_some_and(|ctx| ctx.contains(cell)) {
// TODO: currently the first layouted cell is picked to be part of
// the tag tree, for repeating footers this will be the cell on the
// first page. Maybe it should be the cell on the last page, but that
// would require more changes in the layouting code, or a pre-pass
// on the frames to figure out if there are other footers following.
start_artifact(gc, loc, ArtifactKind::Other);
} else {
push_stack(gc, loc, StackEntryKind::TableCell(cell.clone()))?;
}
return Ok(());
} 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 Ok(());
} else if let Some(_) = elem.to_packed::<ParElem>() {
TagKind::P.into()
} else {
return Ok(());
};
push_stack(gc, loc, StackEntryKind::Standard(tag))?;
Ok(())
}
fn push_stack(
gc: &mut GlobalContext,
loc: Location,
kind: StackEntryKind,
) -> SourceResult<()> {
if !gc.tags.context_supports(&kind) {
if gc.options.standards.config.validator() == Validator::UA1 {
// TODO: error
} else {
// TODO: warning
}
}
gc.tags.stack.push(StackEntry { loc, kind, nodes: Vec::new() });
Ok(())
}
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().and_then(|parent| {
let ctx = parent.kind.as_outline_mut()?;
Some((&mut parent.nodes, ctx))
});
let Some((parent_nodes, outline_ctx)) = parent else {
// 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.into();
gc.tags.push(TagNode::Group(tag, entry.nodes));
return;
};
outline_ctx.insert(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 Some(table_ctx) = gc.tags.parent_table() else {
// 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()).into();
gc.tags.push(TagNode::Group(tag, entry.nodes));
return;
};
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);
}
/// 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) 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)>,
/// Used to group multiple link annotations using quad points.
pub(crate) link_id: LinkId,
/// Used to generate IDs referenced in table `Headers` attributes.
/// The IDs must be document wide unique.
pub(crate) table_id: TableId,
/// The output.
pub(crate) tree: Vec<TagNode>,
}
impl Tags {
pub(crate) fn new() -> Self {
Self {
stack: Vec::new(),
placeholders: Vec::new(),
in_artifact: None,
tree: Vec::new(),
link_id: LinkId(0),
table_id: TableId(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")
}
pub(crate) fn parent(&mut self) -> Option<&mut StackEntryKind> {
self.stack.last_mut().map(|e| &mut e.kind)
}
pub(crate) fn parent_table(&mut self) -> Option<&mut TableCtx> {
self.parent()?.as_table_mut()
}
pub(crate) fn find_parent_link(&self) -> Option<(LinkId, &Packed<LinkMarker>)> {
self.stack.iter().rev().find_map(|entry| entry.kind.as_link())
}
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
}
fn next_table_id(&mut self) -> TableId {
self.table_id.0 += 1;
self.table_id
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct TableId(u32);
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct LinkId(u32);
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) fn as_outline_mut(&mut self) -> Option<&mut OutlineCtx> {
if let Self::Outline(v) = self {
Some(v)
} else {
None
}
}
pub(crate) fn as_table_mut(&mut self) -> Option<&mut TableCtx> {
if let Self::Table(v) = self {
Some(v)
} else {
None
}
}
pub(crate) fn as_link(&self) -> Option<(LinkId, &Packed<LinkMarker>)> {
if let Self::Link(id, link) = self {
Some((*id, link))
} else {
None
}
}
}
#[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)]
pub(crate) struct Placeholder(usize);
/// 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> {
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 }
}
fn start_artifact(gc: &mut GlobalContext, loc: Location, kind: ArtifactKind) {
gc.tags.in_artifact = Some((loc, kind));
}
fn artifact_type(kind: ArtifactKind) -> ArtifactType {
match kind {
ArtifactKind::Header => ArtifactType::Header,
ArtifactKind::Footer => ArtifactType::Footer,
ArtifactKind::Page => ArtifactType::Page,
ArtifactKind::Other => ArtifactType::Other,
}
}

View File

@ -0,0 +1,74 @@
use krilla::tagging::TagKind;
use typst_library::foundations::Packed;
use typst_library::model::OutlineEntry;
use crate::tags::TagNode;
pub(crate) struct OutlineCtx {
stack: Vec<OutlineSection>,
}
impl OutlineCtx {
pub(crate) fn new() -> Self {
Self { stack: Vec::new() }
}
pub(crate) 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),
}
}
pub(crate) fn build_outline(
mut self,
mut outline_nodes: Vec<TagNode>,
) -> Vec<TagNode> {
while !self.stack.is_empty() {
self.finish_section(&mut outline_nodes);
}
outline_nodes
}
}
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)
}
}

View File

@ -0,0 +1,330 @@
use std::num::{NonZeroU32, NonZeroUsize};
use krilla::tagging::{
TableCellHeaders, TableCellSpan, TableDataCell, TableHeaderCell, TagBuilder, TagId,
TagKind,
};
use typst_library::foundations::{Packed, Smart, StyleChain};
use typst_library::model::{TableCell, TableCellKind, TableElem, TableHeaderScope};
use crate::tags::{TableId, TagNode};
pub(crate) struct TableCtx {
pub(crate) id: TableId,
pub(crate) table: Packed<TableElem>,
rows: Vec<Vec<GridCell>>,
}
impl TableCtx {
pub(crate) fn new(id: TableId, table: Packed<TableElem>) -> Self {
Self { id, table: table.clone(), rows: Vec::new() }
}
fn get(&self, x: usize, y: usize) -> Option<&TableCtxCell> {
let cell = self.rows.get(y)?.get(x)?;
self.resolve_cell(cell)
}
fn get_mut(&mut self, x: usize, y: usize) -> Option<&mut TableCtxCell> {
let cell = self.rows.get_mut(y)?.get_mut(x)?;
match cell {
GridCell::Cell(cell) => {
// HACK: Workaround for the second mutable borrow when resolving
// the spanned cell.
Some(unsafe { std::mem::transmute(cell) })
}
&mut GridCell::Spanned(x, y) => self.rows[y][x].as_cell_mut(),
GridCell::Missing => None,
}
}
pub(crate) fn contains(&self, cell: &Packed<TableCell>) -> bool {
let x = cell.x(StyleChain::default()).unwrap_or_else(|| unreachable!());
let y = cell.y(StyleChain::default()).unwrap_or_else(|| unreachable!());
self.get(x, y).is_some()
}
fn resolve_cell<'a>(&'a self, cell: &'a GridCell) -> Option<&'a TableCtxCell> {
match cell {
GridCell::Cell(cell) => Some(cell),
&GridCell::Spanned(x, y) => self.rows[y][x].as_cell(),
GridCell::Missing => None,
}
}
pub(crate) 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());
// 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 {
x: x as u32,
y: y as u32,
rowspan,
colspan,
kind,
headers: TableCellHeaders::NONE,
nodes,
});
}
pub(crate) fn build_table(mut 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.
if self.rows.is_empty() {
return nodes;
}
let height = self.rows.len();
let width = self.rows[0].len();
// Only generate row groups such as `THead`, `TFoot`, and `TBody` if
// there are no rows with mixed cell kinds.
let mut gen_row_groups = true;
let row_kinds = (self.rows.iter())
.map(|row| {
row.iter()
.filter_map(|cell| self.resolve_cell(cell))
.map(|cell| cell.kind)
.fold(Smart::Auto, |a, b| {
if let Smart::Custom(TableCellKind::Header(_, scope)) = b {
gen_row_groups &= scope == TableHeaderScope::Column;
}
if let (Smart::Custom(a), Smart::Custom(b)) = (a, b) {
gen_row_groups &= a == b;
}
a.or(b)
})
.unwrap_or(TableCellKind::Data)
})
.collect::<Vec<_>>();
// Fixup all missing cell kinds.
for (row, row_kind) in self.rows.iter_mut().zip(row_kinds.iter().copied()) {
let default_kind =
if gen_row_groups { row_kind } else { TableCellKind::Data };
for cell in row.iter_mut() {
let Some(cell) = cell.as_cell_mut() else { continue };
cell.kind = cell.kind.or(Smart::Custom(default_kind));
}
}
// Explicitly set the headers attribute for cells.
for x in 0..width {
let mut column_header = None;
for y in 0..height {
self.resolve_cell_headers(
(x, y),
&mut column_header,
TableHeaderScope::refers_to_column,
);
}
}
for y in 0..height {
let mut row_header = None;
for x in 0..width {
self.resolve_cell_headers(
(x, y),
&mut row_header,
TableHeaderScope::refers_to_row,
);
}
}
let mut chunk_kind = row_kinds[0];
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.try_into().unwrap_or(NonZeroU32::MAX),
cols: cell.colspan.try_into().unwrap_or(NonZeroU32::MAX),
};
let tag = match cell.unwrap_kind() {
TableCellKind::Header(_, scope) => {
let id = table_cell_id(self.id, cell.x, cell.y);
let scope = table_header_scope(scope);
TagKind::TH(
TableHeaderCell::new(scope)
.with_span(span)
.with_headers(cell.headers),
)
.with_id(Some(id))
}
TableCellKind::Footer | TableCellKind::Data => TagKind::TD(
TableDataCell::new()
.with_span(span)
.with_headers(cell.headers),
)
.into(),
};
Some(TagNode::Group(tag, cell.nodes))
})
.collect();
let row = TagNode::Group(TagKind::TR.into(), row_nodes);
// Push the `TR` tags directly.
if !gen_row_groups {
nodes.push(row);
continue;
}
// Generate row groups.
if !should_group_rows(chunk_kind, row_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
}
fn resolve_cell_headers<F>(
&mut self,
(x, y): (usize, usize),
current_header: &mut Option<(NonZeroU32, TagId)>,
refers_to_dir: F,
) where
F: Fn(&TableHeaderScope) -> bool,
{
let table_id = self.id;
let Some(cell) = self.get_mut(x, y) else { return };
if let Some((prev_level, cell_id)) = current_header.clone() {
// The `Headers` attribute is also set for parent headers.
let mut is_parent_header = true;
if let TableCellKind::Header(level, scope) = cell.unwrap_kind() {
if refers_to_dir(&scope) {
is_parent_header = prev_level < level;
}
}
if is_parent_header && !cell.headers.ids.contains(&cell_id) {
cell.headers.ids.push(cell_id.clone());
}
}
if let TableCellKind::Header(level, scope) = cell.unwrap_kind() {
if refers_to_dir(&scope) {
let tag_id = table_cell_id(table_id, x as u32, y as u32);
*current_header = Some((level, tag_id));
}
}
}
}
#[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 as_cell_mut(&mut self) -> Option<&mut 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 {
x: u32,
y: u32,
rowspan: NonZeroUsize,
colspan: NonZeroUsize,
kind: Smart<TableCellKind>,
headers: TableCellHeaders,
nodes: Vec<TagNode>,
}
impl TableCtxCell {
fn unwrap_kind(&self) -> TableCellKind {
self.kind.unwrap_or_else(|| unreachable!())
}
}
fn should_group_rows(a: TableCellKind, b: TableCellKind) -> bool {
match (a, b) {
(TableCellKind::Header(..), TableCellKind::Header(..)) => true,
(TableCellKind::Footer, TableCellKind::Footer) => true,
(TableCellKind::Data, TableCellKind::Data) => true,
(_, _) => false,
}
}
fn table_cell_id(table_id: TableId, x: u32, y: u32) -> TagId {
let mut bytes = [0; 12];
bytes[0..4].copy_from_slice(&table_id.0.to_ne_bytes());
bytes[4..8].copy_from_slice(&x.to_ne_bytes());
bytes[8..12].copy_from_slice(&y.to_ne_bytes());
TagId::from_slice(&bytes)
}
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,
}
}

View File

@ -11,8 +11,8 @@ use typst_library::visualize::FillRule;
use typst_syntax::Span; use typst_syntax::Span;
use crate::convert::{FrameContext, GlobalContext}; use crate::convert::{FrameContext, GlobalContext};
use crate::paint;
use crate::util::{display_font, AbsExt, TransformExt}; use crate::util::{display_font, AbsExt, TransformExt};
use crate::{paint, tags};
#[typst_macros::time(name = "handle text")] #[typst_macros::time(name = "handle text")]
pub(crate) fn handle_text( pub(crate) fn handle_text(
@ -23,6 +23,9 @@ pub(crate) fn handle_text(
) -> SourceResult<()> { ) -> SourceResult<()> {
*gc.languages.entry(t.lang).or_insert(0) += t.glyphs.len(); *gc.languages.entry(t.lang).or_insert(0) += t.glyphs.len();
let mut handle = tags::start_marked(gc, surface);
let surface = handle.surface();
let font = convert_font(gc, t.font.clone())?; let font = convert_font(gc, t.font.clone())?;
let fill = paint::convert_fill( let fill = paint::convert_fill(
gc, gc,

View File

@ -167,7 +167,7 @@ fn render_frame(canvas: &mut sk::Pixmap, state: State, frame: &Frame) {
FrameItem::Image(image, size, _) => { FrameItem::Image(image, size, _) => {
image::render_image(canvas, state.pre_translate(*pos), image, *size); image::render_image(canvas, state.pre_translate(*pos), image, *size);
} }
FrameItem::Link(_, _) => {} FrameItem::Link(..) => {}
FrameItem::Tag(_) => {} FrameItem::Tag(_) => {}
} }
} }

View File

@ -207,7 +207,7 @@ impl SVGRenderer {
for (pos, item) in frame.items() { for (pos, item) in frame.items() {
// File size optimization. // File size optimization.
// TODO: SVGs could contain links, couldn't they? // TODO: SVGs could contain links, couldn't they?
if matches!(item, FrameItem::Link(_, _) | FrameItem::Tag(_)) { if matches!(item, FrameItem::Link(..) | FrameItem::Tag(_)) {
continue; continue;
} }
@ -228,7 +228,7 @@ impl SVGRenderer {
self.render_shape(state.pre_translate(*pos), shape) self.render_shape(state.pre_translate(*pos), shape)
} }
FrameItem::Image(image, size, _) => self.render_image(image, size), FrameItem::Image(image, size, _) => self.render_image(image, size),
FrameItem::Link(_, _) => unreachable!(), FrameItem::Link(..) => unreachable!(),
FrameItem::Tag(_) => unreachable!(), FrameItem::Tag(_) => unreachable!(),
}; };

View File

@ -395,6 +395,10 @@ pub fn default_math_class(c: char) -> Option<MathClass> {
// https://github.com/typst/typst/issues/5764 // https://github.com/typst/typst/issues/5764
'⟇' => Some(MathClass::Binary), '⟇' => Some(MathClass::Binary),
// Arabic comma.
// https://github.com/latex3/unicode-math/pull/633#issuecomment-2028936135
'،' => Some(MathClass::Punctuation),
c => unicode_math_class::class(c), c => unicode_math_class::class(c),
} }
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 841 B

After

Width:  |  Height:  |  Size: 903 B

BIN
tests/ref/long-scripts.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 956 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 311 B

After

Width:  |  Height:  |  Size: 318 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 835 B

View File

@ -1,22 +1,89 @@
// Test sub- and superscript shifts. // Test sub- and superscript shifts.
--- sub-super --- --- sub-super ---
#let sq = box(square(size: 4pt))
#table( #table(
columns: 3, columns: 3,
[Typo.], [Fallb.], [Synth], [Typo.], [Fallb.], [Synth.],
[x#super[1]], [x#super[5n]], [x#super[2 #box(square(size: 6pt))]], [x#super[1#sq]], [x#super[5: #sq]], [x#super(typographic: false)[2 #sq]],
[x#sub[1]], [x#sub[5n]], [x#sub[2 #box(square(size: 6pt))]], [x#sub[1#sq]], [x#sub[5: #sq]], [x#sub(typographic: false)[2 #sq]],
) )
--- sub-super-typographic ---
#set text(size: 20pt)
// Libertinus Serif supports "subs" and "sups" for `typo` and `sq`, but not for
// `synth`.
#let synth = [1,2,3]
#let typo = [123]
#let sq = [1#box(square(size: 4pt))2]
x#super(synth) x#super(typo) x#super(sq) \
x#sub(synth) x#sub(typo) x#sub(sq)
--- sub-super-italic-compensation ---
#set text(size: 20pt, style: "italic")
// Libertinus Serif supports "subs" and "sups" for `typo`, but not for `synth`.
#let synth = [1,2,3]
#let typo = [123]
#let sq = [1#box(square(size: 4pt))2]
x#super(synth) x#super(typo) x#super(sq) \
x#sub(synth) x#sub(typo) x#sub(sq)
--- sub-super-non-typographic --- --- sub-super-non-typographic ---
#set super(typographic: false, baseline: -0.25em, size: 0.7em) #set super(typographic: false, baseline: -0.25em, size: 0.7em)
n#super[1], n#sub[2], ... n#super[N] n#super[1], n#sub[2], ... n#super[N]
--- super-underline --- --- super-underline ---
#set underline(stroke: 0.5pt, offset: 0.15em) #set underline(stroke: 0.5pt, offset: 0.15em)
#underline[The claim#super[\[4\]]] has been disputed. \ #set super(typographic: false)
The claim#super[#underline[\[4\]]] has been disputed. \ #underline[A#super[4]] B \
It really has been#super(box(text(baseline: 0pt, underline[\[4\]]))) \ A#super[#underline[4]] B \
A #underline(super[4]) B \
#set super(typographic: true)
#underline[A#super[4]] B \
A#super[#underline[4]] B \
A #underline(super[4]) B
--- super-highlight ---
#set super(typographic: false)
#highlight[A#super[4]] B \
A#super[#highlight[4]] B \
A#super(highlight[4]) \
#set super(typographic: true)
#highlight[A#super[4]] B \
A#super[#highlight[4]] B \
A#super(highlight[4])
--- super-1em ---
#set text(size: 10pt)
#super(context test(1em.to-absolute(), 10pt))
--- long-scripts ---
|longscript| \
|#super(typographic: true)[longscript]| \
|#super(typographic: false)[longscript]| \
|#sub(typographic: true)[longscript]| \
|#sub(typographic: false)[longscript]|
--- script-metrics-bundeled-fonts ---
// Tests whether the script metrics are used properly by synthesizing
// superscripts and subscripts for all bundled fonts.
#set super(typographic: false)
#set sub(typographic: false)
#let test(font, weights, styles) = {
for weight in weights {
for style in styles {
text(font: font, weight: weight, style: style)[Xx#super[Xx]#sub[Xx]]
linebreak()
}
}
}
#test("DejaVu Sans Mono", ("regular", "bold"), ("normal", "oblique"))
#test("Libertinus Serif", ("regular", "semibold", "bold"), ("normal", "italic"))
#test("New Computer Modern", ("regular", "bold"), ("normal", "italic"))
#test("New Computer Modern Math", (400, 450, "bold"), ("normal",))
--- basic-sup-sub html --- --- basic-sup-sub html ---
1#super[st], 2#super[nd], 3#super[rd]. 1#super[st], 2#super[nd], 3#super[rd].