mirror of
https://github.com/typst/typst
synced 2025-07-27 22:37:54 +08:00
Merge branch 'main' into query-html-export
# Conflicts: # crates/typst-cli/src/query.rs # crates/typst-library/src/diag.rs
This commit is contained in:
commit
fee95dd0b0
1
.github/workflows/ci.yml
vendored
1
.github/workflows/ci.yml
vendored
@ -81,6 +81,7 @@ jobs:
|
||||
- run: cargo clippy --workspace --all-targets --no-default-features
|
||||
- run: cargo fmt --check --all
|
||||
- run: cargo doc --workspace --no-deps
|
||||
- run: git diff --exit-code
|
||||
|
||||
min-version:
|
||||
name: Check minimum Rust version
|
||||
|
27
Cargo.lock
generated
27
Cargo.lock
generated
@ -413,8 +413,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "codex"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "724d27a0ee38b700e5e164350e79aba601a0db673ac47fce1cb74c3e38864036"
|
||||
source = "git+https://github.com/typst/codex?rev=56eb217#56eb2172fc0670f4c1c8b79a63d11f9354e5babe"
|
||||
|
||||
[[package]]
|
||||
name = "color-print"
|
||||
@ -787,9 +786,9 @@ checksum = "a0d2fde1f7b3d48b8395d5f2de76c18a528bd6a9cdde438df747bfcba3e05d6f"
|
||||
|
||||
[[package]]
|
||||
name = "font-types"
|
||||
version = "0.8.4"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fa6a5e5a77b5f3f7f9e32879f484aa5b3632ddfbe568a16266c904a6f32cdaf"
|
||||
checksum = "02a596f5713680923a2080d86de50fe472fb290693cf0f701187a1c8b36996b7"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
]
|
||||
@ -1368,8 +1367,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "krilla"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "69ee6128ebf52d7ce684613b6431ead2959f2be9ff8cf776eeaaad0427c953e9"
|
||||
source = "git+https://github.com/LaurenzV/krilla?rev=20c14fe#20c14fefee5002566b3d6668b338bbe2168784e7"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"bumpalo",
|
||||
@ -1397,8 +1395,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "krilla-svg"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3462989578155cf620ef8035f8921533cc95c28e2a0c75de172f7219e6aba84e"
|
||||
source = "git+https://github.com/LaurenzV/krilla?rev=20c14fe#20c14fefee5002566b3d6668b338bbe2168784e7"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"fontdb",
|
||||
@ -2107,9 +2104,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "read-fonts"
|
||||
version = "0.28.0"
|
||||
version = "0.30.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "600e807b48ac55bad68a8cb75cc3c7739f139b9248f7e003e01e080f589b5288"
|
||||
checksum = "192735ef611aac958468e670cb98432c925426f3cb71521fda202130f7388d91"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"font-types",
|
||||
@ -2435,9 +2432,9 @@ checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d"
|
||||
|
||||
[[package]]
|
||||
name = "skrifa"
|
||||
version = "0.30.0"
|
||||
version = "0.32.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6fa1e5622e4f7b98877e8a19890efddcac1230cec6198bd9de91ec0e00010dc8"
|
||||
checksum = "e6d632b5a73f566303dbeabd344dc3e716fd4ddc9a70d6fc8ea8e6f06617da97"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"read-fonts",
|
||||
@ -2864,7 +2861,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "typst-assets"
|
||||
version = "0.13.1"
|
||||
source = "git+https://github.com/typst/typst-assets?rev=c74e539#c74e539b090070a0c66fd007c550f5b6d3b724bd"
|
||||
source = "git+https://github.com/typst/typst-assets?rev=c1089b4#c1089b46c461bdde579c55caa941a3cc7dec3e8a"
|
||||
|
||||
[[package]]
|
||||
name = "typst-cli"
|
||||
@ -2921,6 +2918,7 @@ name = "typst-docs"
|
||||
version = "0.13.1"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"codex",
|
||||
"ecow",
|
||||
"heck",
|
||||
"pulldown-cmark",
|
||||
@ -3039,6 +3037,7 @@ dependencies = [
|
||||
"icu_provider_blob",
|
||||
"icu_segmenter",
|
||||
"kurbo",
|
||||
"memchr",
|
||||
"rustybuzz",
|
||||
"smallvec",
|
||||
"ttf-parser",
|
||||
@ -3112,6 +3111,7 @@ dependencies = [
|
||||
"unicode-segmentation",
|
||||
"unscanny",
|
||||
"usvg",
|
||||
"utf8_iter",
|
||||
"wasmi",
|
||||
"xmlwriter",
|
||||
]
|
||||
@ -3200,6 +3200,7 @@ dependencies = [
|
||||
name = "typst-syntax"
|
||||
version = "0.13.1"
|
||||
dependencies = [
|
||||
"comemo",
|
||||
"ecow",
|
||||
"serde",
|
||||
"toml",
|
||||
|
@ -32,7 +32,7 @@ typst-svg = { path = "crates/typst-svg", version = "0.13.1" }
|
||||
typst-syntax = { path = "crates/typst-syntax", version = "0.13.1" }
|
||||
typst-timing = { path = "crates/typst-timing", version = "0.13.1" }
|
||||
typst-utils = { path = "crates/typst-utils", version = "0.13.1" }
|
||||
typst-assets = { git = "https://github.com/typst/typst-assets", rev = "c74e539" }
|
||||
typst-assets = { git = "https://github.com/typst/typst-assets", rev = "c1089b4" }
|
||||
typst-dev-assets = { git = "https://github.com/typst/typst-dev-assets", rev = "fddbf8b" }
|
||||
arrayvec = "0.7.4"
|
||||
az = "1.2"
|
||||
@ -47,7 +47,7 @@ clap = { version = "4.4", features = ["derive", "env", "wrap_help"] }
|
||||
clap_complete = "4.2.1"
|
||||
clap_mangen = "0.2.10"
|
||||
codespan-reporting = "0.11"
|
||||
codex = "0.1.1"
|
||||
codex = { git = "https://github.com/typst/codex", rev = "56eb217" }
|
||||
color-print = "0.3.6"
|
||||
comemo = "0.4"
|
||||
csv = "1"
|
||||
@ -73,8 +73,8 @@ image = { version = "0.25.5", default-features = false, features = ["png", "jpeg
|
||||
indexmap = { version = "2", features = ["serde"] }
|
||||
infer = { version = "0.19.0", default-features = false }
|
||||
kamadak-exif = "0.6"
|
||||
krilla = { version = "0.4.0", default-features = false, features = ["raster-images", "comemo", "rayon"] }
|
||||
krilla-svg = "0.1.0"
|
||||
krilla = { git = "https://github.com/LaurenzV/krilla", rev = "20c14fe", default-features = false, features = ["raster-images", "comemo", "rayon"] }
|
||||
krilla-svg = { git = "https://github.com/LaurenzV/krilla", rev = "20c14fe" }
|
||||
kurbo = "0.11"
|
||||
libfuzzer-sys = "0.4"
|
||||
lipsum = "0.9"
|
||||
@ -135,6 +135,7 @@ unicode-segmentation = "1"
|
||||
unscanny = "0.1"
|
||||
ureq = { version = "2", default-features = false, features = ["native-tls", "gzip", "json"] }
|
||||
usvg = { version = "0.45", default-features = false, features = ["text"] }
|
||||
utf8_iter = "1.0.4"
|
||||
walkdir = "2"
|
||||
wasmi = "0.40.0"
|
||||
web-sys = "0.3"
|
||||
|
@ -16,7 +16,7 @@ use typst::diag::{
|
||||
use typst::foundations::{Datetime, Smart};
|
||||
use typst::html::HtmlDocument;
|
||||
use typst::layout::{Frame, Page, PageRanges, PagedDocument};
|
||||
use typst::syntax::{FileId, Source, Span};
|
||||
use typst::syntax::{FileId, Lines, Span};
|
||||
use typst::WorldExt;
|
||||
use typst_pdf::{PdfOptions, PdfStandards, Timestamp};
|
||||
|
||||
@ -696,7 +696,7 @@ fn label(world: &SystemWorld, span: Span) -> Option<Label<FileId>> {
|
||||
impl<'a> codespan_reporting::files::Files<'a> for SystemWorld {
|
||||
type FileId = FileId;
|
||||
type Name = String;
|
||||
type Source = Source;
|
||||
type Source = Lines<String>;
|
||||
|
||||
fn name(&'a self, id: FileId) -> CodespanResult<Self::Name> {
|
||||
let vpath = id.vpath();
|
||||
|
@ -6,9 +6,9 @@ use typst::engine::Sink;
|
||||
use typst::foundations::{Content, IntoValue, LocatableSelector, Scope};
|
||||
use typst::html::HtmlDocument;
|
||||
use typst::layout::PagedDocument;
|
||||
use typst::syntax::Span;
|
||||
use typst::syntax::{Span, SyntaxMode};
|
||||
use typst::{Document, World};
|
||||
use typst_eval::{eval_string, EvalMode};
|
||||
use typst_eval::eval_string;
|
||||
|
||||
use crate::args::{QueryCommand, SerializationFormat, Target};
|
||||
use crate::compile::print_diagnostics;
|
||||
@ -69,7 +69,7 @@ fn retrieve<D: Document>(
|
||||
Sink::new().track_mut(),
|
||||
&command.selector,
|
||||
Span::detached(),
|
||||
EvalMode::Code,
|
||||
SyntaxMode::Code,
|
||||
Scope::default(),
|
||||
)
|
||||
.map_err(|errors| {
|
||||
|
@ -85,6 +85,6 @@ fn resolve_span(world: &SystemWorld, span: Span) -> Option<(String, u32)> {
|
||||
let id = span.id()?;
|
||||
let source = world.source(id).ok()?;
|
||||
let range = source.range(span)?;
|
||||
let line = source.byte_to_line(range.start)?;
|
||||
let line = source.lines().byte_to_line(range.start)?;
|
||||
Some((format!("{id:?}"), line as u32 + 1))
|
||||
}
|
||||
|
@ -10,11 +10,12 @@ use codespan_reporting::term::{self, termcolor};
|
||||
use ecow::eco_format;
|
||||
use notify::{Event, RecommendedWatcher, RecursiveMode, Watcher as _};
|
||||
use same_file::is_same_file;
|
||||
use typst::diag::{bail, StrResult};
|
||||
use typst::diag::{bail, warning, StrResult};
|
||||
use typst::syntax::Span;
|
||||
use typst::utils::format_duration;
|
||||
|
||||
use crate::args::{Input, Output, WatchCommand};
|
||||
use crate::compile::{compile_once, CompileConfig};
|
||||
use crate::compile::{compile_once, print_diagnostics, CompileConfig};
|
||||
use crate::timings::Timer;
|
||||
use crate::world::{SystemWorld, WorldCreationError};
|
||||
use crate::{print_error, terminal};
|
||||
@ -55,6 +56,11 @@ pub fn watch(timer: &mut Timer, command: &WatchCommand) -> StrResult<()> {
|
||||
// Perform initial compilation.
|
||||
timer.record(&mut world, |world| compile_once(world, &mut config))??;
|
||||
|
||||
// Print warning when trying to watch stdin.
|
||||
if matches!(&config.input, Input::Stdin) {
|
||||
warn_watching_std(&world, &config)?;
|
||||
}
|
||||
|
||||
// Recompile whenever something relevant happens.
|
||||
loop {
|
||||
// Watch all dependencies of the most recent compilation.
|
||||
@ -332,3 +338,15 @@ impl Status {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Emits a warning when trying to watch stdin.
|
||||
fn warn_watching_std(world: &SystemWorld, config: &CompileConfig) -> StrResult<()> {
|
||||
let warning = warning!(
|
||||
Span::detached(),
|
||||
"cannot watch changes for stdin";
|
||||
hint: "to recompile on changes, watch a regular file instead";
|
||||
hint: "to compile once and exit, please use `typst compile` instead"
|
||||
);
|
||||
print_diagnostics(world, &[], &[warning], config.diagnostic_format)
|
||||
.map_err(|err| eco_format!("failed to print diagnostics ({err})"))
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ use ecow::{eco_format, EcoString};
|
||||
use parking_lot::Mutex;
|
||||
use typst::diag::{FileError, FileResult};
|
||||
use typst::foundations::{Bytes, Datetime, Dict, IntoValue};
|
||||
use typst::syntax::{FileId, Source, VirtualPath};
|
||||
use typst::syntax::{FileId, Lines, Source, VirtualPath};
|
||||
use typst::text::{Font, FontBook};
|
||||
use typst::utils::LazyHash;
|
||||
use typst::{Library, World};
|
||||
@ -181,10 +181,20 @@ impl SystemWorld {
|
||||
}
|
||||
}
|
||||
|
||||
/// Lookup a source file by id.
|
||||
/// Lookup line metadata for a file by id.
|
||||
#[track_caller]
|
||||
pub fn lookup(&self, id: FileId) -> Source {
|
||||
self.source(id).expect("file id does not point to any source file")
|
||||
pub fn lookup(&self, id: FileId) -> Lines<String> {
|
||||
self.slot(id, |slot| {
|
||||
if let Some(source) = slot.source.get() {
|
||||
let source = source.as_ref().expect("file is not valid");
|
||||
source.lines().clone()
|
||||
} else if let Some(bytes) = slot.file.get() {
|
||||
let bytes = bytes.as_ref().expect("file is not valid");
|
||||
Lines::try_from(bytes).expect("file is not valid utf-8")
|
||||
} else {
|
||||
panic!("file id does not point to any source file");
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -339,6 +349,11 @@ impl<T: Clone> SlotCell<T> {
|
||||
self.accessed = false;
|
||||
}
|
||||
|
||||
/// Gets the contents of the cell.
|
||||
fn get(&self) -> Option<&FileResult<T>> {
|
||||
self.data.as_ref()
|
||||
}
|
||||
|
||||
/// Gets the contents of the cell or initialize them.
|
||||
fn get_or_init(
|
||||
&mut self,
|
||||
|
@ -18,7 +18,6 @@ pub use self::call::{eval_closure, CapturesVisitor};
|
||||
pub use self::flow::FlowEvent;
|
||||
pub use self::import::import;
|
||||
pub use self::vm::Vm;
|
||||
pub use typst_library::routines::EvalMode;
|
||||
|
||||
use self::access::*;
|
||||
use self::binding::*;
|
||||
@ -32,7 +31,7 @@ use typst_library::introspection::Introspector;
|
||||
use typst_library::math::EquationElem;
|
||||
use typst_library::routines::Routines;
|
||||
use typst_library::World;
|
||||
use typst_syntax::{ast, parse, parse_code, parse_math, Source, Span};
|
||||
use typst_syntax::{ast, parse, parse_code, parse_math, Source, Span, SyntaxMode};
|
||||
|
||||
/// Evaluate a source file and return the resulting module.
|
||||
#[comemo::memoize]
|
||||
@ -104,13 +103,13 @@ pub fn eval_string(
|
||||
sink: TrackedMut<Sink>,
|
||||
string: &str,
|
||||
span: Span,
|
||||
mode: EvalMode,
|
||||
mode: SyntaxMode,
|
||||
scope: Scope,
|
||||
) -> SourceResult<Value> {
|
||||
let mut root = match mode {
|
||||
EvalMode::Code => parse_code(string),
|
||||
EvalMode::Markup => parse(string),
|
||||
EvalMode::Math => parse_math(string),
|
||||
SyntaxMode::Code => parse_code(string),
|
||||
SyntaxMode::Markup => parse(string),
|
||||
SyntaxMode::Math => parse_math(string),
|
||||
};
|
||||
|
||||
root.synthesize(span);
|
||||
@ -141,11 +140,11 @@ pub fn eval_string(
|
||||
|
||||
// Evaluate the code.
|
||||
let output = match mode {
|
||||
EvalMode::Code => root.cast::<ast::Code>().unwrap().eval(&mut vm)?,
|
||||
EvalMode::Markup => {
|
||||
SyntaxMode::Code => root.cast::<ast::Code>().unwrap().eval(&mut vm)?,
|
||||
SyntaxMode::Markup => {
|
||||
Value::Content(root.cast::<ast::Markup>().unwrap().eval(&mut vm)?)
|
||||
}
|
||||
EvalMode::Math => Value::Content(
|
||||
SyntaxMode::Math => Value::Content(
|
||||
EquationElem::new(root.cast::<ast::Math>().unwrap().eval(&mut vm)?)
|
||||
.with_block(false)
|
||||
.pack()
|
||||
|
@ -2,7 +2,9 @@ use std::fmt::Write;
|
||||
|
||||
use typst_library::diag::{bail, At, SourceResult, StrResult};
|
||||
use typst_library::foundations::Repr;
|
||||
use typst_library::html::{charsets, tag, HtmlDocument, HtmlElement, HtmlNode, HtmlTag};
|
||||
use typst_library::html::{
|
||||
attr, charsets, tag, HtmlDocument, HtmlElement, HtmlNode, HtmlTag,
|
||||
};
|
||||
use typst_library::layout::Frame;
|
||||
use typst_syntax::Span;
|
||||
|
||||
@ -28,7 +30,7 @@ struct Writer {
|
||||
pretty: bool,
|
||||
}
|
||||
|
||||
/// Write a newline and indent, if pretty printing is enabled.
|
||||
/// Writes a newline and indent, if pretty printing is enabled.
|
||||
fn write_indent(w: &mut Writer) {
|
||||
if w.pretty {
|
||||
w.buf.push('\n');
|
||||
@ -38,7 +40,7 @@ fn write_indent(w: &mut Writer) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Encode an HTML node into the writer.
|
||||
/// Encodes an HTML node into the writer.
|
||||
fn write_node(w: &mut Writer, node: &HtmlNode) -> SourceResult<()> {
|
||||
match node {
|
||||
HtmlNode::Tag(_) => {}
|
||||
@ -49,7 +51,7 @@ fn write_node(w: &mut Writer, node: &HtmlNode) -> SourceResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Encode plain text into the writer.
|
||||
/// Encodes plain text into the writer.
|
||||
fn write_text(w: &mut Writer, text: &str, span: Span) -> SourceResult<()> {
|
||||
for c in text.chars() {
|
||||
if charsets::is_valid_in_normal_element_text(c) {
|
||||
@ -61,7 +63,7 @@ fn write_text(w: &mut Writer, text: &str, span: Span) -> SourceResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Encode one element into the write.
|
||||
/// Encodes one element into the writer.
|
||||
fn write_element(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> {
|
||||
w.buf.push('<');
|
||||
w.buf.push_str(&element.tag.resolve());
|
||||
@ -69,54 +71,37 @@ fn write_element(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> {
|
||||
for (attr, value) in &element.attrs.0 {
|
||||
w.buf.push(' ');
|
||||
w.buf.push_str(&attr.resolve());
|
||||
w.buf.push('=');
|
||||
w.buf.push('"');
|
||||
for c in value.chars() {
|
||||
if charsets::is_valid_in_attribute_value(c) {
|
||||
w.buf.push(c);
|
||||
} else {
|
||||
write_escape(w, c).at(element.span)?;
|
||||
|
||||
// If the string is empty, we can use shorthand syntax.
|
||||
// `<elem attr="">..</div` is equivalent to `<elem attr>..</div>`
|
||||
if !value.is_empty() {
|
||||
w.buf.push('=');
|
||||
w.buf.push('"');
|
||||
for c in value.chars() {
|
||||
if charsets::is_valid_in_attribute_value(c) {
|
||||
w.buf.push(c);
|
||||
} else {
|
||||
write_escape(w, c).at(element.span)?;
|
||||
}
|
||||
}
|
||||
w.buf.push('"');
|
||||
}
|
||||
w.buf.push('"');
|
||||
}
|
||||
|
||||
w.buf.push('>');
|
||||
|
||||
if tag::is_void(element.tag) {
|
||||
if !element.children.is_empty() {
|
||||
bail!(element.span, "HTML void elements must not have children");
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let pretty = w.pretty;
|
||||
if !element.children.is_empty() {
|
||||
let pretty_inside = allows_pretty_inside(element.tag)
|
||||
&& element.children.iter().any(|node| match node {
|
||||
HtmlNode::Element(child) => wants_pretty_around(child.tag),
|
||||
_ => false,
|
||||
});
|
||||
|
||||
w.pretty &= pretty_inside;
|
||||
let mut indent = w.pretty;
|
||||
|
||||
w.level += 1;
|
||||
for c in &element.children {
|
||||
let pretty_around = match c {
|
||||
HtmlNode::Tag(_) => continue,
|
||||
HtmlNode::Element(child) => w.pretty && wants_pretty_around(child.tag),
|
||||
HtmlNode::Text(..) | HtmlNode::Frame(_) => false,
|
||||
};
|
||||
|
||||
if core::mem::take(&mut indent) || pretty_around {
|
||||
write_indent(w);
|
||||
}
|
||||
write_node(w, c)?;
|
||||
indent = pretty_around;
|
||||
}
|
||||
w.level -= 1;
|
||||
|
||||
write_indent(w);
|
||||
if tag::is_raw(element.tag) {
|
||||
write_raw(w, element)?;
|
||||
} else if !element.children.is_empty() {
|
||||
write_children(w, element)?;
|
||||
}
|
||||
w.pretty = pretty;
|
||||
|
||||
w.buf.push_str("</");
|
||||
w.buf.push_str(&element.tag.resolve());
|
||||
@ -125,6 +110,159 @@ fn write_element(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Encodes the children of an element.
|
||||
fn write_children(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> {
|
||||
// See HTML spec § 13.1.2.5.
|
||||
if matches!(element.tag, tag::pre | tag::textarea) && starts_with_newline(element) {
|
||||
w.buf.push('\n');
|
||||
}
|
||||
|
||||
let pretty = w.pretty;
|
||||
let pretty_inside = allows_pretty_inside(element.tag)
|
||||
&& element.children.iter().any(|node| match node {
|
||||
HtmlNode::Element(child) => wants_pretty_around(child.tag),
|
||||
_ => false,
|
||||
});
|
||||
|
||||
w.pretty &= pretty_inside;
|
||||
let mut indent = w.pretty;
|
||||
|
||||
w.level += 1;
|
||||
for c in &element.children {
|
||||
let pretty_around = match c {
|
||||
HtmlNode::Tag(_) => continue,
|
||||
HtmlNode::Element(child) => w.pretty && wants_pretty_around(child.tag),
|
||||
HtmlNode::Text(..) | HtmlNode::Frame(_) => false,
|
||||
};
|
||||
|
||||
if core::mem::take(&mut indent) || pretty_around {
|
||||
write_indent(w);
|
||||
}
|
||||
write_node(w, c)?;
|
||||
indent = pretty_around;
|
||||
}
|
||||
w.level -= 1;
|
||||
|
||||
write_indent(w);
|
||||
w.pretty = pretty;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Whether the first character in the element is a newline.
|
||||
fn starts_with_newline(element: &HtmlElement) -> bool {
|
||||
for child in &element.children {
|
||||
match child {
|
||||
HtmlNode::Tag(_) => {}
|
||||
HtmlNode::Text(text, _) => return text.starts_with(['\n', '\r']),
|
||||
_ => return false,
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Encodes the contents of a raw text element.
|
||||
fn write_raw(w: &mut Writer, element: &HtmlElement) -> SourceResult<()> {
|
||||
let text = collect_raw_text(element)?;
|
||||
|
||||
if let Some(closing) = find_closing_tag(&text, element.tag) {
|
||||
bail!(
|
||||
element.span,
|
||||
"HTML raw text element cannot contain its own closing tag";
|
||||
hint: "the sequence `{closing}` appears in the raw text",
|
||||
)
|
||||
}
|
||||
|
||||
let mode = if w.pretty { RawMode::of(element, &text) } else { RawMode::Keep };
|
||||
match mode {
|
||||
RawMode::Keep => {
|
||||
w.buf.push_str(&text);
|
||||
}
|
||||
RawMode::Wrap => {
|
||||
w.buf.push('\n');
|
||||
w.buf.push_str(&text);
|
||||
write_indent(w);
|
||||
}
|
||||
RawMode::Indent => {
|
||||
w.level += 1;
|
||||
for line in text.lines() {
|
||||
write_indent(w);
|
||||
w.buf.push_str(line);
|
||||
}
|
||||
w.level -= 1;
|
||||
write_indent(w);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Collects the textual contents of a raw text element.
|
||||
fn collect_raw_text(element: &HtmlElement) -> SourceResult<String> {
|
||||
let mut output = String::new();
|
||||
for c in &element.children {
|
||||
match c {
|
||||
HtmlNode::Tag(_) => continue,
|
||||
HtmlNode::Text(text, _) => output.push_str(text),
|
||||
HtmlNode::Element(_) | HtmlNode::Frame(_) => {
|
||||
let span = match c {
|
||||
HtmlNode::Element(child) => child.span,
|
||||
_ => element.span,
|
||||
};
|
||||
bail!(span, "HTML raw text element cannot have non-text children")
|
||||
}
|
||||
};
|
||||
}
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
/// Finds a closing sequence for the given tag in the text, if it exists.
|
||||
///
|
||||
/// See HTML spec § 13.1.2.6.
|
||||
fn find_closing_tag(text: &str, tag: HtmlTag) -> Option<&str> {
|
||||
let s = tag.resolve();
|
||||
let len = s.len();
|
||||
text.match_indices("</").find_map(|(i, _)| {
|
||||
let rest = &text[i + 2..];
|
||||
let disallowed = rest.len() >= len
|
||||
&& rest[..len].eq_ignore_ascii_case(&s)
|
||||
&& rest[len..].starts_with(['\t', '\n', '\u{c}', '\r', ' ', '>', '/']);
|
||||
disallowed.then(|| &text[i..i + 2 + len])
|
||||
})
|
||||
}
|
||||
|
||||
/// How to format the contents of a raw text element.
|
||||
enum RawMode {
|
||||
/// Just don't touch it.
|
||||
Keep,
|
||||
/// Newline after the opening and newline + indent before the closing tag.
|
||||
Wrap,
|
||||
/// Newlines after opening and before closing tag and each line indented.
|
||||
Indent,
|
||||
}
|
||||
|
||||
impl RawMode {
|
||||
fn of(element: &HtmlElement, text: &str) -> Self {
|
||||
match element.tag {
|
||||
tag::script
|
||||
if !element.attrs.0.iter().any(|(attr, value)| {
|
||||
*attr == attr::r#type && value != "text/javascript"
|
||||
}) =>
|
||||
{
|
||||
// Template literals can be multi-line, so indent may change
|
||||
// the semantics of the JavaScript.
|
||||
if text.contains('`') {
|
||||
Self::Wrap
|
||||
} else {
|
||||
Self::Indent
|
||||
}
|
||||
}
|
||||
tag::style => Self::Indent,
|
||||
_ => Self::Keep,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether we are allowed to add an extra newline at the start and end of the
|
||||
/// element's contents.
|
||||
///
|
||||
@ -160,7 +298,7 @@ fn write_escape(w: &mut Writer, c: char) -> StrResult<()> {
|
||||
c if charsets::is_w3c_text_char(c) && c != '\r' => {
|
||||
write!(w.buf, "&#x{:x};", c as u32).unwrap()
|
||||
}
|
||||
_ => bail!("the character {} cannot be encoded in HTML", c.repr()),
|
||||
_ => bail!("the character `{}` cannot be encoded in HTML", c.repr()),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -180,9 +180,6 @@ fn handle(
|
||||
if let Some(body) = elem.body(styles) {
|
||||
children = html_fragment(engine, body, locator.next(&elem.span()), styles)?;
|
||||
}
|
||||
if tag::is_void(elem.tag) && !children.is_empty() {
|
||||
bail!(elem.span(), "HTML void elements may not have children");
|
||||
}
|
||||
let element = HtmlElement {
|
||||
tag: elem.tag,
|
||||
attrs: elem.attrs(styles).clone(),
|
||||
|
@ -298,13 +298,20 @@ fn complete_math(ctx: &mut CompletionContext) -> bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Start of an interpolated identifier: "#|".
|
||||
// Start of an interpolated identifier: "$#|$".
|
||||
if ctx.leaf.kind() == SyntaxKind::Hash {
|
||||
ctx.from = ctx.cursor;
|
||||
code_completions(ctx, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Behind existing interpolated identifier: "$#pa|$".
|
||||
if ctx.leaf.kind() == SyntaxKind::Ident {
|
||||
ctx.from = ctx.leaf.offset();
|
||||
code_completions(ctx, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Behind existing atom or identifier: "$a|$" or "$abc|$".
|
||||
if matches!(
|
||||
ctx.leaf.kind(),
|
||||
@ -694,7 +701,10 @@ fn complete_params(ctx: &mut CompletionContext) -> bool {
|
||||
let mut deciding = ctx.leaf.clone();
|
||||
while !matches!(
|
||||
deciding.kind(),
|
||||
SyntaxKind::LeftParen | SyntaxKind::Comma | SyntaxKind::Colon
|
||||
SyntaxKind::LeftParen
|
||||
| SyntaxKind::RightParen
|
||||
| SyntaxKind::Comma
|
||||
| SyntaxKind::Colon
|
||||
) {
|
||||
let Some(prev) = deciding.prev_leaf() else { break };
|
||||
deciding = prev;
|
||||
@ -1666,6 +1676,13 @@ mod tests {
|
||||
test("#{() .a}", -2).must_include(["at", "any", "all"]);
|
||||
}
|
||||
|
||||
/// Test that autocomplete in math uses the correct global scope.
|
||||
#[test]
|
||||
fn test_autocomplete_math_scope() {
|
||||
test("$#col$", -2).must_include(["colbreak"]).must_exclude(["colon"]);
|
||||
test("$col$", -2).must_include(["colon"]).must_exclude(["colbreak"]);
|
||||
}
|
||||
|
||||
/// Test that the `before_window` doesn't slice into invalid byte
|
||||
/// boundaries.
|
||||
#[test]
|
||||
@ -1684,7 +1701,7 @@ mod tests {
|
||||
|
||||
// Then, add the invalid `#cite` call. Had the document been invalid
|
||||
// initially, we would have no populated document to autocomplete with.
|
||||
let end = world.main.len_bytes();
|
||||
let end = world.main.text().len();
|
||||
world.main.edit(end..end, " #cite()");
|
||||
|
||||
test_with_doc(&world, -2, doc.as_ref())
|
||||
@ -1720,6 +1737,8 @@ mod tests {
|
||||
test("#numbering(\"foo\", 1, )", -2)
|
||||
.must_include(["integer"])
|
||||
.must_exclude(["string"]);
|
||||
// After argument list no completions.
|
||||
test("#numbering()", -1).must_exclude(["string"]);
|
||||
}
|
||||
|
||||
/// Test that autocompletion for values of known type picks up nested
|
||||
@ -1815,18 +1834,27 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_autocomplete_fonts() {
|
||||
test("#text(font:)", -1)
|
||||
test("#text(font:)", -2)
|
||||
.must_include(["\"Libertinus Serif\"", "\"New Computer Modern Math\""]);
|
||||
|
||||
test("#show link: set text(font: )", -1)
|
||||
test("#show link: set text(font: )", -2)
|
||||
.must_include(["\"Libertinus Serif\"", "\"New Computer Modern Math\""]);
|
||||
|
||||
test("#show math.equation: set text(font: )", -1)
|
||||
test("#show math.equation: set text(font: )", -2)
|
||||
.must_include(["\"New Computer Modern Math\""])
|
||||
.must_exclude(["\"Libertinus Serif\""]);
|
||||
|
||||
test("#show math.equation: it => { set text(font: )\nit }", -6)
|
||||
test("#show math.equation: it => { set text(font: )\nit }", -7)
|
||||
.must_include(["\"New Computer Modern Math\""])
|
||||
.must_exclude(["\"Libertinus Serif\""]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_autocomplete_typed_html() {
|
||||
test("#html.div(translate: )", -2)
|
||||
.must_include(["true", "false"])
|
||||
.must_exclude(["\"yes\"", "\"no\""]);
|
||||
test("#html.input(value: )", -2).must_include(["float", "string", "red", "blue"]);
|
||||
test("#html.div(role: )", -2).must_include(["\"alertdialog\""]);
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ use typst::syntax::package::{PackageSpec, PackageVersion};
|
||||
use typst::syntax::{FileId, Source, VirtualPath};
|
||||
use typst::text::{Font, FontBook, TextElem, TextSize};
|
||||
use typst::utils::{singleton, LazyHash};
|
||||
use typst::{Library, World};
|
||||
use typst::{Feature, Library, World};
|
||||
|
||||
use crate::IdeWorld;
|
||||
|
||||
@ -168,7 +168,9 @@ fn library() -> Library {
|
||||
// Set page width to 120pt with 10pt margins, so that the inner page is
|
||||
// exactly 100pt wide. Page height is unbounded and font size is 10pt so
|
||||
// that it multiplies to nice round numbers.
|
||||
let mut lib = typst::Library::default();
|
||||
let mut lib = typst::Library::builder()
|
||||
.with_features([Feature::Html].into_iter().collect())
|
||||
.build();
|
||||
lib.styles
|
||||
.set(PageElem::set_width(Smart::Custom(Abs::pt(120.0).into())));
|
||||
lib.styles.set(PageElem::set_height(Smart::Auto));
|
||||
@ -202,7 +204,8 @@ impl WorldLike for &str {
|
||||
}
|
||||
}
|
||||
|
||||
/// Specifies a position in a file for a test.
|
||||
/// Specifies a position in a file for a test. Negative numbers index from the
|
||||
/// back. `-1` is at the very back.
|
||||
pub trait FilePos {
|
||||
fn resolve(self, world: &TestWorld) -> (Source, usize);
|
||||
}
|
||||
@ -228,7 +231,7 @@ impl FilePos for (&str, isize) {
|
||||
#[track_caller]
|
||||
fn cursor(source: &Source, cursor: isize) -> usize {
|
||||
if cursor < 0 {
|
||||
source.len_bytes().checked_add_signed(cursor + 1).unwrap()
|
||||
source.text().len().checked_add_signed(cursor + 1).unwrap()
|
||||
} else {
|
||||
cursor as usize
|
||||
}
|
||||
|
@ -114,7 +114,9 @@ pub fn globals<'a>(world: &'a dyn IdeWorld, leaf: &LinkedNode) -> &'a Scope {
|
||||
| Some(SyntaxKind::Math)
|
||||
| Some(SyntaxKind::MathFrac)
|
||||
| Some(SyntaxKind::MathAttach)
|
||||
);
|
||||
) && leaf
|
||||
.prev_leaf()
|
||||
.is_none_or(|prev| !matches!(prev.kind(), SyntaxKind::Hash));
|
||||
|
||||
let library = world.library();
|
||||
if in_math {
|
||||
|
@ -30,6 +30,7 @@ icu_provider_adapters = { workspace = true }
|
||||
icu_provider_blob = { workspace = true }
|
||||
icu_segmenter = { workspace = true }
|
||||
kurbo = { workspace = true }
|
||||
memchr = { workspace = true }
|
||||
rustybuzz = { workspace = true }
|
||||
smallvec = { workspace = true }
|
||||
ttf-parser = { workspace = true }
|
||||
|
@ -3,7 +3,9 @@ use std::fmt::Debug;
|
||||
use typst_library::diag::{bail, SourceResult};
|
||||
use typst_library::engine::Engine;
|
||||
use typst_library::foundations::{Resolve, StyleChain};
|
||||
use typst_library::layout::grid::resolve::{Cell, CellGrid, LinePosition, Repeatable};
|
||||
use typst_library::layout::grid::resolve::{
|
||||
Cell, CellGrid, Header, LinePosition, Repeatable,
|
||||
};
|
||||
use typst_library::layout::{
|
||||
Abs, Axes, Dir, Fr, Fragment, Frame, FrameItem, Length, Point, Region, Regions, Rel,
|
||||
Size, Sizing,
|
||||
@ -30,10 +32,8 @@ pub struct GridLayouter<'a> {
|
||||
pub(super) rcols: Vec<Abs>,
|
||||
/// The sum of `rcols`.
|
||||
pub(super) width: Abs,
|
||||
/// Resolve row sizes, by region.
|
||||
/// Resolved row sizes, by region.
|
||||
pub(super) rrows: Vec<Vec<RowPiece>>,
|
||||
/// Rows in the current region.
|
||||
pub(super) lrows: Vec<Row>,
|
||||
/// The amount of unbreakable rows remaining to be laid out in the
|
||||
/// current unbreakable row group. While this is positive, no region breaks
|
||||
/// should occur.
|
||||
@ -41,24 +41,155 @@ pub struct GridLayouter<'a> {
|
||||
/// Rowspans not yet laid out because not all of their spanned rows were
|
||||
/// laid out yet.
|
||||
pub(super) rowspans: Vec<Rowspan>,
|
||||
/// The initial size of the current region before we started subtracting.
|
||||
pub(super) initial: Size,
|
||||
/// Grid layout state for the current region.
|
||||
pub(super) current: Current,
|
||||
/// Frames for finished regions.
|
||||
pub(super) finished: Vec<Frame>,
|
||||
/// The amount and height of header rows on each finished region.
|
||||
pub(super) finished_header_rows: Vec<FinishedHeaderRowInfo>,
|
||||
/// Whether this is an RTL grid.
|
||||
pub(super) is_rtl: bool,
|
||||
/// The simulated header height.
|
||||
/// This field is reset in `layout_header` and properly updated by
|
||||
/// Currently repeating headers, one per level. Sorted by increasing
|
||||
/// levels.
|
||||
///
|
||||
/// Note that some levels may be absent, in particular level 0, which does
|
||||
/// not exist (so all levels are >= 1).
|
||||
pub(super) repeating_headers: Vec<&'a Header>,
|
||||
/// Headers, repeating or not, awaiting their first successful layout.
|
||||
/// Sorted by increasing levels.
|
||||
pub(super) pending_headers: &'a [Repeatable<Header>],
|
||||
/// Next headers to be processed.
|
||||
pub(super) upcoming_headers: &'a [Repeatable<Header>],
|
||||
/// State of the row being currently laid out.
|
||||
///
|
||||
/// This is kept as a field to avoid passing down too many parameters from
|
||||
/// `layout_row` into called functions, which would then have to pass them
|
||||
/// down to `push_row`, which reads these values.
|
||||
pub(super) row_state: RowState,
|
||||
/// The span of the grid element.
|
||||
pub(super) span: Span,
|
||||
}
|
||||
|
||||
/// Grid layout state for the current region. This should be reset or updated
|
||||
/// on each region break.
|
||||
pub(super) struct Current {
|
||||
/// The initial size of the current region before we started subtracting.
|
||||
pub(super) initial: Size,
|
||||
/// The height of the region after repeated headers were placed and footers
|
||||
/// prepared. This also includes pending repeating headers from the start,
|
||||
/// even if they were not repeated yet, since they will be repeated in the
|
||||
/// next region anyway (bar orphan prevention).
|
||||
///
|
||||
/// This is used to quickly tell if any additional space in the region has
|
||||
/// been occupied since then, meaning that additional space will become
|
||||
/// available after a region break (see
|
||||
/// [`GridLayouter::may_progress_with_repeats`]).
|
||||
pub(super) initial_after_repeats: Abs,
|
||||
/// Whether `layouter.regions.may_progress()` was `true` at the top of the
|
||||
/// region.
|
||||
pub(super) could_progress_at_top: bool,
|
||||
/// Rows in the current region.
|
||||
pub(super) lrows: Vec<Row>,
|
||||
/// The amount of repeated header rows at the start of the current region.
|
||||
/// Thus, excludes rows from pending headers (which were placed for the
|
||||
/// first time).
|
||||
///
|
||||
/// Note that `repeating_headers` and `pending_headers` can change if we
|
||||
/// find a new header inside the region (not at the top), so this field
|
||||
/// is required to access information from the top of the region.
|
||||
///
|
||||
/// This information is used on finish region to calculate the total height
|
||||
/// of resolved header rows at the top of the region, which is used by
|
||||
/// multi-page rowspans so they can properly skip the header rows at the
|
||||
/// top of each region during layout.
|
||||
pub(super) repeated_header_rows: usize,
|
||||
/// The end bound of the row range of the last repeating header at the
|
||||
/// start of the region.
|
||||
///
|
||||
/// The last row might have disappeared from layout due to being empty, so
|
||||
/// this is how we can become aware of where the last header ends without
|
||||
/// having to check the vector of rows. Line layout uses this to determine
|
||||
/// when to prioritize the last lines under a header.
|
||||
///
|
||||
/// A value of zero indicates no repeated headers were placed.
|
||||
pub(super) last_repeated_header_end: usize,
|
||||
/// Stores the length of `lrows` before a sequence of rows equipped with
|
||||
/// orphan prevention was laid out. In this case, if no more rows without
|
||||
/// orphan prevention are laid out after those rows before the region ends,
|
||||
/// the rows will be removed, and there may be an attempt to place them
|
||||
/// again in the new region. Effectively, this is the mechanism used for
|
||||
/// orphan prevention of rows.
|
||||
///
|
||||
/// At the moment, this is only used by repeated headers (they aren't laid
|
||||
/// out if alone in the region) and by new headers, which are moved to the
|
||||
/// `pending_headers` vector and so will automatically be placed again
|
||||
/// until they fit and are not orphans in at least one region (or exactly
|
||||
/// one, for non-repeated headers).
|
||||
pub(super) lrows_orphan_snapshot: Option<usize>,
|
||||
/// The height of effectively repeating headers, that is, ignoring
|
||||
/// non-repeating pending headers, in the current region.
|
||||
///
|
||||
/// This is used by multi-page auto rows so they can inform cell layout on
|
||||
/// how much space should be taken by headers if they break across regions.
|
||||
/// In particular, non-repeating headers only occupy the initial region,
|
||||
/// but disappear on new regions, so they can be ignored.
|
||||
///
|
||||
/// This field is reset on each new region and properly updated by
|
||||
/// `layout_auto_row` and `layout_relative_row`, and should not be read
|
||||
/// before all header rows are fully laid out. It is usually fine because
|
||||
/// header rows themselves are unbreakable, and unbreakable rows do not
|
||||
/// need to read this field at all.
|
||||
pub(super) header_height: Abs,
|
||||
///
|
||||
/// This height is not only computed at the beginning of the region. It is
|
||||
/// updated whenever a new header is found, subtracting the height of
|
||||
/// headers which stopped repeating and adding the height of all new
|
||||
/// headers.
|
||||
pub(super) repeating_header_height: Abs,
|
||||
/// The height for each repeating header that was placed in this region.
|
||||
/// Note that this includes headers not at the top of the region, before
|
||||
/// their first repetition (pending headers), and excludes headers removed
|
||||
/// by virtue of a new, conflicting header being found (short-lived
|
||||
/// headers).
|
||||
///
|
||||
/// This is used to know how much to update `repeating_header_height` by
|
||||
/// when finding a new header and causing existing repeating headers to
|
||||
/// stop.
|
||||
pub(super) repeating_header_heights: Vec<Abs>,
|
||||
/// The simulated footer height for this region.
|
||||
///
|
||||
/// The simulation occurs before any rows are laid out for a region.
|
||||
pub(super) footer_height: Abs,
|
||||
/// The span of the grid element.
|
||||
pub(super) span: Span,
|
||||
}
|
||||
|
||||
/// Data about the row being laid out right now.
|
||||
#[derive(Debug, Default)]
|
||||
pub(super) struct RowState {
|
||||
/// If this is `Some`, this will be updated by the currently laid out row's
|
||||
/// height if it is auto or relative. This is used for header height
|
||||
/// calculation.
|
||||
pub(super) current_row_height: Option<Abs>,
|
||||
/// This is `true` when laying out non-short lived headers and footers.
|
||||
/// That is, headers and footers which are not immediately followed or
|
||||
/// preceded (respectively) by conflicting headers and footers of same or
|
||||
/// lower level, or the end or start of the table (respectively), which
|
||||
/// would cause them to never repeat, even once.
|
||||
///
|
||||
/// If this is `false`, the next row to be laid out will remove an active
|
||||
/// orphan snapshot and will flush pending headers, as there is no risk
|
||||
/// that they will be orphans anymore.
|
||||
pub(super) in_active_repeatable: bool,
|
||||
}
|
||||
|
||||
/// Data about laid out repeated header rows for a specific finished region.
|
||||
#[derive(Debug, Default)]
|
||||
pub(super) struct FinishedHeaderRowInfo {
|
||||
/// The amount of repeated headers at the top of the region.
|
||||
pub(super) repeated_amount: usize,
|
||||
/// The end bound of the row range of the last repeated header at the top
|
||||
/// of the region.
|
||||
pub(super) last_repeated_header_end: usize,
|
||||
/// The total height of repeated headers at the top of the region.
|
||||
pub(super) repeated_height: Abs,
|
||||
}
|
||||
|
||||
/// Details about a resulting row piece.
|
||||
@ -114,14 +245,27 @@ impl<'a> GridLayouter<'a> {
|
||||
rcols: vec![Abs::zero(); grid.cols.len()],
|
||||
width: Abs::zero(),
|
||||
rrows: vec![],
|
||||
lrows: vec![],
|
||||
unbreakable_rows_left: 0,
|
||||
rowspans: vec![],
|
||||
initial: regions.size,
|
||||
finished: vec![],
|
||||
finished_header_rows: vec![],
|
||||
is_rtl: TextElem::dir_in(styles) == Dir::RTL,
|
||||
header_height: Abs::zero(),
|
||||
footer_height: Abs::zero(),
|
||||
repeating_headers: vec![],
|
||||
upcoming_headers: &grid.headers,
|
||||
pending_headers: Default::default(),
|
||||
row_state: RowState::default(),
|
||||
current: Current {
|
||||
initial: regions.size,
|
||||
initial_after_repeats: regions.size.y,
|
||||
could_progress_at_top: regions.may_progress(),
|
||||
lrows: vec![],
|
||||
repeated_header_rows: 0,
|
||||
last_repeated_header_end: 0,
|
||||
lrows_orphan_snapshot: None,
|
||||
repeating_header_height: Abs::zero(),
|
||||
repeating_header_heights: vec![],
|
||||
footer_height: Abs::zero(),
|
||||
},
|
||||
span,
|
||||
}
|
||||
}
|
||||
@ -130,38 +274,57 @@ impl<'a> GridLayouter<'a> {
|
||||
pub fn layout(mut self, engine: &mut Engine) -> SourceResult<Fragment> {
|
||||
self.measure_columns(engine)?;
|
||||
|
||||
if let Some(Repeatable::Repeated(footer)) = &self.grid.footer {
|
||||
// Ensure rows in the first region will be aware of the possible
|
||||
// presence of the footer.
|
||||
self.prepare_footer(footer, engine, 0)?;
|
||||
if matches!(self.grid.header, None | Some(Repeatable::NotRepeated(_))) {
|
||||
// No repeatable header, so we won't subtract it later.
|
||||
self.regions.size.y -= self.footer_height;
|
||||
if let Some(footer) = &self.grid.footer {
|
||||
if footer.repeated {
|
||||
// Ensure rows in the first region will be aware of the
|
||||
// possible presence of the footer.
|
||||
self.prepare_footer(footer, engine, 0)?;
|
||||
self.regions.size.y -= self.current.footer_height;
|
||||
self.current.initial_after_repeats = self.regions.size.y;
|
||||
}
|
||||
}
|
||||
|
||||
for y in 0..self.grid.rows.len() {
|
||||
if let Some(Repeatable::Repeated(header)) = &self.grid.header {
|
||||
if y < header.end {
|
||||
if y == 0 {
|
||||
self.layout_header(header, engine, 0)?;
|
||||
self.regions.size.y -= self.footer_height;
|
||||
}
|
||||
let mut y = 0;
|
||||
let mut consecutive_header_count = 0;
|
||||
while y < self.grid.rows.len() {
|
||||
if let Some(next_header) = self.upcoming_headers.get(consecutive_header_count)
|
||||
{
|
||||
if next_header.range.contains(&y) {
|
||||
self.place_new_headers(&mut consecutive_header_count, engine)?;
|
||||
y = next_header.range.end;
|
||||
|
||||
// Skip header rows during normal layout.
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(Repeatable::Repeated(footer)) = &self.grid.footer {
|
||||
if y >= footer.start {
|
||||
if let Some(footer) = &self.grid.footer {
|
||||
if footer.repeated && y >= footer.start {
|
||||
if y == footer.start {
|
||||
self.layout_footer(footer, engine, self.finished.len())?;
|
||||
self.flush_orphans();
|
||||
}
|
||||
y = footer.end;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
self.layout_row(y, engine, 0)?;
|
||||
|
||||
// After the first non-header row is placed, pending headers are no
|
||||
// longer orphans and can repeat, so we move them to repeating
|
||||
// headers.
|
||||
//
|
||||
// Note that this is usually done in `push_row`, since the call to
|
||||
// `layout_row` above might trigger region breaks (for multi-page
|
||||
// auto rows), whereas this needs to be called as soon as any part
|
||||
// of a row is laid out. However, it's possible a row has no
|
||||
// visible output and thus does not push any rows even though it
|
||||
// was successfully laid out, in which case we additionally flush
|
||||
// here just in case.
|
||||
self.flush_orphans();
|
||||
|
||||
y += 1;
|
||||
}
|
||||
|
||||
self.finish_region(engine, true)?;
|
||||
@ -184,12 +347,46 @@ impl<'a> GridLayouter<'a> {
|
||||
self.render_fills_strokes()
|
||||
}
|
||||
|
||||
/// Layout the given row.
|
||||
/// Layout a row with a certain initial state, returning the final state.
|
||||
#[inline]
|
||||
pub(super) fn layout_row_with_state(
|
||||
&mut self,
|
||||
y: usize,
|
||||
engine: &mut Engine,
|
||||
disambiguator: usize,
|
||||
initial_state: RowState,
|
||||
) -> SourceResult<RowState> {
|
||||
// Keep a copy of the previous value in the stack, as this function can
|
||||
// call itself recursively (e.g. if a region break is triggered and a
|
||||
// header is placed), so we shouldn't outright overwrite it, but rather
|
||||
// save and later restore the state when back to this call.
|
||||
let previous = std::mem::replace(&mut self.row_state, initial_state);
|
||||
|
||||
// Keep it as a separate function to allow inlining the return below,
|
||||
// as it's usually not needed.
|
||||
self.layout_row_internal(y, engine, disambiguator)?;
|
||||
|
||||
Ok(std::mem::replace(&mut self.row_state, previous))
|
||||
}
|
||||
|
||||
/// Layout the given row with the default row state.
|
||||
#[inline]
|
||||
pub(super) fn layout_row(
|
||||
&mut self,
|
||||
y: usize,
|
||||
engine: &mut Engine,
|
||||
disambiguator: usize,
|
||||
) -> SourceResult<()> {
|
||||
self.layout_row_with_state(y, engine, disambiguator, RowState::default())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Layout the given row using the current state.
|
||||
pub(super) fn layout_row_internal(
|
||||
&mut self,
|
||||
y: usize,
|
||||
engine: &mut Engine,
|
||||
disambiguator: usize,
|
||||
) -> SourceResult<()> {
|
||||
// Skip to next region if current one is full, but only for content
|
||||
// rows, not for gutter rows, and only if we aren't laying out an
|
||||
@ -206,13 +403,18 @@ impl<'a> GridLayouter<'a> {
|
||||
}
|
||||
|
||||
// Don't layout gutter rows at the top of a region.
|
||||
if is_content_row || !self.lrows.is_empty() {
|
||||
if is_content_row || !self.current.lrows.is_empty() {
|
||||
match self.grid.rows[y] {
|
||||
Sizing::Auto => self.layout_auto_row(engine, disambiguator, y)?,
|
||||
Sizing::Rel(v) => {
|
||||
self.layout_relative_row(engine, disambiguator, v, y)?
|
||||
}
|
||||
Sizing::Fr(v) => self.lrows.push(Row::Fr(v, y, disambiguator)),
|
||||
Sizing::Fr(v) => {
|
||||
if !self.row_state.in_active_repeatable {
|
||||
self.flush_orphans();
|
||||
}
|
||||
self.current.lrows.push(Row::Fr(v, y, disambiguator))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -225,8 +427,13 @@ impl<'a> GridLayouter<'a> {
|
||||
fn render_fills_strokes(mut self) -> SourceResult<Fragment> {
|
||||
let mut finished = std::mem::take(&mut self.finished);
|
||||
let frame_amount = finished.len();
|
||||
for ((frame_index, frame), rows) in
|
||||
finished.iter_mut().enumerate().zip(&self.rrows)
|
||||
for (((frame_index, frame), rows), finished_header_rows) in
|
||||
finished.iter_mut().enumerate().zip(&self.rrows).zip(
|
||||
self.finished_header_rows
|
||||
.iter()
|
||||
.map(Some)
|
||||
.chain(std::iter::repeat(None)),
|
||||
)
|
||||
{
|
||||
if self.rcols.is_empty() || rows.is_empty() {
|
||||
continue;
|
||||
@ -347,7 +554,8 @@ impl<'a> GridLayouter<'a> {
|
||||
let hline_indices = rows
|
||||
.iter()
|
||||
.map(|piece| piece.y)
|
||||
.chain(std::iter::once(self.grid.rows.len()));
|
||||
.chain(std::iter::once(self.grid.rows.len()))
|
||||
.enumerate();
|
||||
|
||||
// Converts a row to the corresponding index in the vector of
|
||||
// hlines.
|
||||
@ -372,7 +580,7 @@ impl<'a> GridLayouter<'a> {
|
||||
};
|
||||
|
||||
let mut prev_y = None;
|
||||
for (y, dy) in hline_indices.zip(hline_offsets) {
|
||||
for ((i, y), dy) in hline_indices.zip(hline_offsets) {
|
||||
// Position of lines below the row index in the previous iteration.
|
||||
let expected_prev_line_position = prev_y
|
||||
.map(|prev_y| {
|
||||
@ -383,47 +591,40 @@ impl<'a> GridLayouter<'a> {
|
||||
})
|
||||
.unwrap_or(LinePosition::Before);
|
||||
|
||||
// FIXME: In the future, directly specify in 'self.rrows' when
|
||||
// we place a repeated header rather than its original rows.
|
||||
// That would let us remove most of those verbose checks, both
|
||||
// in 'lines.rs' and here. Those checks also aren't fully
|
||||
// accurate either, since they will also trigger when some rows
|
||||
// have been removed between the header and what's below it.
|
||||
let is_under_repeated_header = self
|
||||
.grid
|
||||
.header
|
||||
.as_ref()
|
||||
.and_then(Repeatable::as_repeated)
|
||||
.zip(prev_y)
|
||||
.is_some_and(|(header, prev_y)| {
|
||||
// Note: 'y == header.end' would mean we're right below
|
||||
// the NON-REPEATED header, so that case should return
|
||||
// false.
|
||||
prev_y < header.end && y > header.end
|
||||
});
|
||||
// Header's lines at the bottom have priority when repeated.
|
||||
// This will store the end bound of the last header if the
|
||||
// current iteration is calculating lines under it.
|
||||
let last_repeated_header_end_above = match finished_header_rows {
|
||||
Some(info) if prev_y.is_some() && i == info.repeated_amount => {
|
||||
Some(info.last_repeated_header_end)
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
|
||||
// If some grid rows were omitted between the previous resolved
|
||||
// row and the current one, we ensure lines below the previous
|
||||
// row don't "disappear" and are considered, albeit with less
|
||||
// priority. However, don't do this when we're below a header,
|
||||
// as it must have more priority instead of less, so it is
|
||||
// chained later instead of before. The exception is when the
|
||||
// chained later instead of before (stored in the
|
||||
// 'header_hlines' variable below). The exception is when the
|
||||
// last row in the header is removed, in which case we append
|
||||
// both the lines under the row above us and also (later) the
|
||||
// lines under the header's (removed) last row.
|
||||
let prev_lines = prev_y
|
||||
.filter(|prev_y| {
|
||||
prev_y + 1 != y
|
||||
&& (!is_under_repeated_header
|
||||
|| self
|
||||
.grid
|
||||
.header
|
||||
.as_ref()
|
||||
.and_then(Repeatable::as_repeated)
|
||||
.is_some_and(|header| prev_y + 1 != header.end))
|
||||
})
|
||||
.map(|prev_y| get_hlines_at(prev_y + 1))
|
||||
.unwrap_or(&[]);
|
||||
let prev_lines = match prev_y {
|
||||
Some(prev_y)
|
||||
if prev_y + 1 != y
|
||||
&& last_repeated_header_end_above.is_none_or(
|
||||
|last_repeated_header_end| {
|
||||
prev_y + 1 != last_repeated_header_end
|
||||
},
|
||||
) =>
|
||||
{
|
||||
get_hlines_at(prev_y + 1)
|
||||
}
|
||||
|
||||
_ => &[],
|
||||
};
|
||||
|
||||
let expected_hline_position =
|
||||
expected_line_position(y, y == self.grid.rows.len());
|
||||
@ -441,15 +642,13 @@ impl<'a> GridLayouter<'a> {
|
||||
};
|
||||
|
||||
let mut expected_header_line_position = LinePosition::Before;
|
||||
let header_hlines = if let Some((Repeatable::Repeated(header), prev_y)) =
|
||||
self.grid.header.as_ref().zip(prev_y)
|
||||
{
|
||||
if is_under_repeated_header
|
||||
&& (!self.grid.has_gutter
|
||||
let header_hlines = match (last_repeated_header_end_above, prev_y) {
|
||||
(Some(header_end_above), Some(prev_y))
|
||||
if !self.grid.has_gutter
|
||||
|| matches!(
|
||||
self.grid.rows[prev_y],
|
||||
Sizing::Rel(length) if length.is_zero()
|
||||
))
|
||||
) =>
|
||||
{
|
||||
// For lines below a header, give priority to the
|
||||
// lines originally below the header rather than
|
||||
@ -468,15 +667,13 @@ impl<'a> GridLayouter<'a> {
|
||||
// column-gutter is specified, for example. In that
|
||||
// case, we still repeat the line under the gutter.
|
||||
expected_header_line_position = expected_line_position(
|
||||
header.end,
|
||||
header.end == self.grid.rows.len(),
|
||||
header_end_above,
|
||||
header_end_above == self.grid.rows.len(),
|
||||
);
|
||||
get_hlines_at(header.end)
|
||||
} else {
|
||||
&[]
|
||||
get_hlines_at(header_end_above)
|
||||
}
|
||||
} else {
|
||||
&[]
|
||||
|
||||
_ => &[],
|
||||
};
|
||||
|
||||
// The effective hlines to be considered at this row index are
|
||||
@ -529,6 +726,7 @@ impl<'a> GridLayouter<'a> {
|
||||
grid,
|
||||
rows,
|
||||
local_top_y,
|
||||
last_repeated_header_end_above,
|
||||
in_last_region,
|
||||
y,
|
||||
x,
|
||||
@ -941,15 +1139,9 @@ impl<'a> GridLayouter<'a> {
|
||||
let frame = self.layout_single_row(engine, disambiguator, first, y)?;
|
||||
self.push_row(frame, y, true);
|
||||
|
||||
if self
|
||||
.grid
|
||||
.header
|
||||
.as_ref()
|
||||
.and_then(Repeatable::as_repeated)
|
||||
.is_some_and(|header| y < header.end)
|
||||
{
|
||||
// Add to header height.
|
||||
self.header_height += first;
|
||||
if let Some(row_height) = &mut self.row_state.current_row_height {
|
||||
// Add to header height, as we are in a header row.
|
||||
*row_height += first;
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
@ -958,19 +1150,21 @@ impl<'a> GridLayouter<'a> {
|
||||
// Expand all but the last region.
|
||||
// Skip the first region if the space is eaten up by an fr row.
|
||||
let len = resolved.len();
|
||||
for ((i, region), target) in self
|
||||
.regions
|
||||
.iter()
|
||||
.enumerate()
|
||||
.zip(&mut resolved[..len - 1])
|
||||
.skip(self.lrows.iter().any(|row| matches!(row, Row::Fr(..))) as usize)
|
||||
for ((i, region), target) in
|
||||
self.regions
|
||||
.iter()
|
||||
.enumerate()
|
||||
.zip(&mut resolved[..len - 1])
|
||||
.skip(self.current.lrows.iter().any(|row| matches!(row, Row::Fr(..)))
|
||||
as usize)
|
||||
{
|
||||
// Subtract header and footer heights from the region height when
|
||||
// it's not the first.
|
||||
// it's not the first. Ignore non-repeating headers as they only
|
||||
// appear on the first region by definition.
|
||||
target.set_max(
|
||||
region.y
|
||||
- if i > 0 {
|
||||
self.header_height + self.footer_height
|
||||
self.current.repeating_header_height + self.current.footer_height
|
||||
} else {
|
||||
Abs::zero()
|
||||
},
|
||||
@ -1181,25 +1375,19 @@ impl<'a> GridLayouter<'a> {
|
||||
let resolved = v.resolve(self.styles).relative_to(self.regions.base().y);
|
||||
let frame = self.layout_single_row(engine, disambiguator, resolved, y)?;
|
||||
|
||||
if self
|
||||
.grid
|
||||
.header
|
||||
.as_ref()
|
||||
.and_then(Repeatable::as_repeated)
|
||||
.is_some_and(|header| y < header.end)
|
||||
{
|
||||
// Add to header height.
|
||||
self.header_height += resolved;
|
||||
if let Some(row_height) = &mut self.row_state.current_row_height {
|
||||
// Add to header height, as we are in a header row.
|
||||
*row_height += resolved;
|
||||
}
|
||||
|
||||
// Skip to fitting region, but only if we aren't part of an unbreakable
|
||||
// row group. We use 'in_last_with_offset' so our 'in_last' call
|
||||
// properly considers that a header and a footer would be added on each
|
||||
// region break.
|
||||
// row group. We use 'may_progress_with_repeats' to stop trying if we
|
||||
// would skip to a region with the same height and where the same
|
||||
// headers would be repeated.
|
||||
let height = frame.height();
|
||||
while self.unbreakable_rows_left == 0
|
||||
&& !self.regions.size.y.fits(height)
|
||||
&& !in_last_with_offset(self.regions, self.header_height + self.footer_height)
|
||||
&& self.may_progress_with_repeats()
|
||||
{
|
||||
self.finish_region(engine, false)?;
|
||||
|
||||
@ -1323,8 +1511,13 @@ impl<'a> GridLayouter<'a> {
|
||||
/// will be pushed for this particular row. It can be `false` for rows
|
||||
/// spanning multiple regions.
|
||||
fn push_row(&mut self, frame: Frame, y: usize, is_last: bool) {
|
||||
if !self.row_state.in_active_repeatable {
|
||||
// There is now a row after the rows equipped with orphan
|
||||
// prevention, so no need to keep moving them anymore.
|
||||
self.flush_orphans();
|
||||
}
|
||||
self.regions.size.y -= frame.height();
|
||||
self.lrows.push(Row::Frame(frame, y, is_last));
|
||||
self.current.lrows.push(Row::Frame(frame, y, is_last));
|
||||
}
|
||||
|
||||
/// Finish rows for one region.
|
||||
@ -1333,68 +1526,73 @@ impl<'a> GridLayouter<'a> {
|
||||
engine: &mut Engine,
|
||||
last: bool,
|
||||
) -> SourceResult<()> {
|
||||
// The latest rows have orphan prevention (headers) and no other rows
|
||||
// were placed, so remove those rows and try again in a new region,
|
||||
// unless this is the last region.
|
||||
if let Some(orphan_snapshot) = self.current.lrows_orphan_snapshot.take() {
|
||||
if !last {
|
||||
self.current.lrows.truncate(orphan_snapshot);
|
||||
self.current.repeated_header_rows =
|
||||
self.current.repeated_header_rows.min(orphan_snapshot);
|
||||
|
||||
if orphan_snapshot == 0 {
|
||||
// Removed all repeated headers.
|
||||
self.current.last_repeated_header_end = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if self
|
||||
.current
|
||||
.lrows
|
||||
.last()
|
||||
.is_some_and(|row| self.grid.is_gutter_track(row.index()))
|
||||
{
|
||||
// Remove the last row in the region if it is a gutter row.
|
||||
self.lrows.pop().unwrap();
|
||||
self.current.lrows.pop().unwrap();
|
||||
self.current.repeated_header_rows =
|
||||
self.current.repeated_header_rows.min(self.current.lrows.len());
|
||||
}
|
||||
|
||||
// If no rows other than the footer have been laid out so far, and
|
||||
// there are rows beside the footer, then don't lay it out at all.
|
||||
// This check doesn't apply, and is thus overridden, when there is a
|
||||
// header.
|
||||
let mut footer_would_be_orphan = self.lrows.is_empty()
|
||||
&& !in_last_with_offset(
|
||||
self.regions,
|
||||
self.header_height + self.footer_height,
|
||||
)
|
||||
&& self
|
||||
.grid
|
||||
.footer
|
||||
.as_ref()
|
||||
.and_then(Repeatable::as_repeated)
|
||||
.is_some_and(|footer| footer.start != 0);
|
||||
|
||||
if let Some(Repeatable::Repeated(header)) = &self.grid.header {
|
||||
if self.grid.rows.len() > header.end
|
||||
&& self
|
||||
.grid
|
||||
.footer
|
||||
.as_ref()
|
||||
.and_then(Repeatable::as_repeated)
|
||||
.is_none_or(|footer| footer.start != header.end)
|
||||
&& self.lrows.last().is_some_and(|row| row.index() < header.end)
|
||||
&& !in_last_with_offset(
|
||||
self.regions,
|
||||
self.header_height + self.footer_height,
|
||||
)
|
||||
{
|
||||
// Header and footer would be alone in this region, but there are more
|
||||
// rows beyond the header and the footer. Push an empty region.
|
||||
self.lrows.clear();
|
||||
footer_would_be_orphan = true;
|
||||
}
|
||||
}
|
||||
// If no rows other than the footer have been laid out so far
|
||||
// (e.g. due to header orphan prevention), and there are rows
|
||||
// beside the footer, then don't lay it out at all.
|
||||
//
|
||||
// It is worth noting that the footer is made non-repeatable at
|
||||
// the grid resolving stage if it is short-lived, that is, if
|
||||
// it is at the start of the table (or right after headers at
|
||||
// the start of the table).
|
||||
//
|
||||
// TODO(subfooters): explicitly check for short-lived footers.
|
||||
// TODO(subfooters): widow prevention for non-repeated footers with a
|
||||
// similar mechanism / when implementing multiple footers.
|
||||
let footer_would_be_widow = matches!(&self.grid.footer, Some(footer) if footer.repeated)
|
||||
&& self.current.lrows.is_empty()
|
||||
&& self.current.could_progress_at_top;
|
||||
|
||||
let mut laid_out_footer_start = None;
|
||||
if let Some(Repeatable::Repeated(footer)) = &self.grid.footer {
|
||||
// Don't layout the footer if it would be alone with the header in
|
||||
// the page, and don't layout it twice.
|
||||
if !footer_would_be_orphan
|
||||
&& self.lrows.iter().all(|row| row.index() < footer.start)
|
||||
{
|
||||
laid_out_footer_start = Some(footer.start);
|
||||
self.layout_footer(footer, engine, self.finished.len())?;
|
||||
if !footer_would_be_widow {
|
||||
if let Some(footer) = &self.grid.footer {
|
||||
// Don't layout the footer if it would be alone with the header
|
||||
// in the page (hence the widow check), and don't layout it
|
||||
// twice (check below).
|
||||
//
|
||||
// TODO(subfooters): this check can be replaced by a vector of
|
||||
// repeating footers in the future, and/or some "pending
|
||||
// footers" vector for footers we're about to place.
|
||||
if footer.repeated
|
||||
&& self.current.lrows.iter().all(|row| row.index() < footer.start)
|
||||
{
|
||||
laid_out_footer_start = Some(footer.start);
|
||||
self.layout_footer(footer, engine, self.finished.len())?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Determine the height of existing rows in the region.
|
||||
let mut used = Abs::zero();
|
||||
let mut fr = Fr::zero();
|
||||
for row in &self.lrows {
|
||||
for row in &self.current.lrows {
|
||||
match row {
|
||||
Row::Frame(frame, _, _) => used += frame.height(),
|
||||
Row::Fr(v, _, _) => fr += *v,
|
||||
@ -1403,9 +1601,9 @@ impl<'a> GridLayouter<'a> {
|
||||
|
||||
// Determine the size of the grid in this region, expanding fully if
|
||||
// there are fr rows.
|
||||
let mut size = Size::new(self.width, used).min(self.initial);
|
||||
if fr.get() > 0.0 && self.initial.y.is_finite() {
|
||||
size.y = self.initial.y;
|
||||
let mut size = Size::new(self.width, used).min(self.current.initial);
|
||||
if fr.get() > 0.0 && self.current.initial.y.is_finite() {
|
||||
size.y = self.current.initial.y;
|
||||
}
|
||||
|
||||
// The frame for the region.
|
||||
@ -1413,9 +1611,10 @@ impl<'a> GridLayouter<'a> {
|
||||
let mut pos = Point::zero();
|
||||
let mut rrows = vec![];
|
||||
let current_region = self.finished.len();
|
||||
let mut repeated_header_row_height = Abs::zero();
|
||||
|
||||
// Place finished rows and layout fractional rows.
|
||||
for row in std::mem::take(&mut self.lrows) {
|
||||
for (i, row) in std::mem::take(&mut self.current.lrows).into_iter().enumerate() {
|
||||
let (frame, y, is_last) = match row {
|
||||
Row::Frame(frame, y, is_last) => (frame, y, is_last),
|
||||
Row::Fr(v, y, disambiguator) => {
|
||||
@ -1426,6 +1625,9 @@ impl<'a> GridLayouter<'a> {
|
||||
};
|
||||
|
||||
let height = frame.height();
|
||||
if i < self.current.repeated_header_rows {
|
||||
repeated_header_row_height += height;
|
||||
}
|
||||
|
||||
// Ensure rowspans which span this row will have enough space to
|
||||
// be laid out over it later.
|
||||
@ -1504,7 +1706,11 @@ impl<'a> GridLayouter<'a> {
|
||||
// we have to check the same index again in the next
|
||||
// iteration.
|
||||
let rowspan = self.rowspans.remove(i);
|
||||
self.layout_rowspan(rowspan, Some((&mut output, &rrows)), engine)?;
|
||||
self.layout_rowspan(
|
||||
rowspan,
|
||||
Some((&mut output, repeated_header_row_height)),
|
||||
engine,
|
||||
)?;
|
||||
} else {
|
||||
i += 1;
|
||||
}
|
||||
@ -1515,21 +1721,40 @@ impl<'a> GridLayouter<'a> {
|
||||
pos.y += height;
|
||||
}
|
||||
|
||||
self.finish_region_internal(output, rrows);
|
||||
self.finish_region_internal(
|
||||
output,
|
||||
rrows,
|
||||
FinishedHeaderRowInfo {
|
||||
repeated_amount: self.current.repeated_header_rows,
|
||||
last_repeated_header_end: self.current.last_repeated_header_end,
|
||||
repeated_height: repeated_header_row_height,
|
||||
},
|
||||
);
|
||||
|
||||
if !last {
|
||||
self.current.repeated_header_rows = 0;
|
||||
self.current.last_repeated_header_end = 0;
|
||||
self.current.repeating_header_height = Abs::zero();
|
||||
self.current.repeating_header_heights.clear();
|
||||
|
||||
let disambiguator = self.finished.len();
|
||||
if let Some(Repeatable::Repeated(footer)) = &self.grid.footer {
|
||||
if let Some(footer) =
|
||||
self.grid.footer.as_ref().and_then(Repeatable::as_repeated)
|
||||
{
|
||||
self.prepare_footer(footer, engine, disambiguator)?;
|
||||
}
|
||||
|
||||
if let Some(Repeatable::Repeated(header)) = &self.grid.header {
|
||||
// Add a header to the new region.
|
||||
self.layout_header(header, engine, disambiguator)?;
|
||||
}
|
||||
|
||||
// Ensure rows don't try to overrun the footer.
|
||||
self.regions.size.y -= self.footer_height;
|
||||
// Note that header layout will only subtract this again if it has
|
||||
// to skip regions to fit headers, so there is no risk of
|
||||
// subtracting this twice.
|
||||
self.regions.size.y -= self.current.footer_height;
|
||||
self.current.initial_after_repeats = self.regions.size.y;
|
||||
|
||||
if !self.repeating_headers.is_empty() || !self.pending_headers.is_empty() {
|
||||
// Add headers to the new region.
|
||||
self.layout_active_headers(engine)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@ -1541,11 +1766,26 @@ impl<'a> GridLayouter<'a> {
|
||||
&mut self,
|
||||
output: Frame,
|
||||
resolved_rows: Vec<RowPiece>,
|
||||
header_row_info: FinishedHeaderRowInfo,
|
||||
) {
|
||||
self.finished.push(output);
|
||||
self.rrows.push(resolved_rows);
|
||||
self.regions.next();
|
||||
self.initial = self.regions.size;
|
||||
self.current.initial = self.regions.size;
|
||||
|
||||
// Repeats haven't been laid out yet, so in the meantime, this will
|
||||
// represent the initial height after repeats laid out so far, and will
|
||||
// be gradually updated when preparing footers and repeating headers.
|
||||
self.current.initial_after_repeats = self.current.initial.y;
|
||||
|
||||
self.current.could_progress_at_top = self.regions.may_progress();
|
||||
|
||||
if !self.grid.headers.is_empty() {
|
||||
self.finished_header_rows.push(header_row_info);
|
||||
}
|
||||
|
||||
// Ensure orphan prevention is handled before resolving rows.
|
||||
debug_assert!(self.current.lrows_orphan_snapshot.is_none());
|
||||
}
|
||||
}
|
||||
|
||||
@ -1560,13 +1800,3 @@ pub(super) fn points(
|
||||
offset
|
||||
})
|
||||
}
|
||||
|
||||
/// Checks if the first region of a sequence of regions is the last usable
|
||||
/// region, assuming that the last region will always be occupied by some
|
||||
/// specific offset height, even after calling `.next()`, due to some
|
||||
/// additional logic which adds content automatically on each region turn (in
|
||||
/// our case, headers).
|
||||
pub(super) fn in_last_with_offset(regions: Regions<'_>, offset: Abs) -> bool {
|
||||
regions.backlog.is_empty()
|
||||
&& regions.last.is_none_or(|height| regions.size.y + offset == height)
|
||||
}
|
||||
|
@ -391,10 +391,12 @@ pub fn vline_stroke_at_row(
|
||||
///
|
||||
/// This function assumes columns are sorted by increasing `x`, and rows are
|
||||
/// sorted by increasing `y`.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn hline_stroke_at_column(
|
||||
grid: &CellGrid,
|
||||
rows: &[RowPiece],
|
||||
local_top_y: Option<usize>,
|
||||
header_end_above: Option<usize>,
|
||||
in_last_region: bool,
|
||||
y: usize,
|
||||
x: usize,
|
||||
@ -499,17 +501,15 @@ pub fn hline_stroke_at_column(
|
||||
// Top border stroke and header stroke are generally prioritized, unless
|
||||
// they don't have explicit hline overrides and one or more user-provided
|
||||
// hlines would appear at the same position, which then are prioritized.
|
||||
let top_stroke_comes_from_header = grid
|
||||
.header
|
||||
.as_ref()
|
||||
.and_then(Repeatable::as_repeated)
|
||||
.zip(local_top_y)
|
||||
.is_some_and(|(header, local_top_y)| {
|
||||
// Ensure the row above us is a repeated header.
|
||||
// FIXME: Make this check more robust when headers at arbitrary
|
||||
// positions are added.
|
||||
local_top_y < header.end && y > header.end
|
||||
});
|
||||
let top_stroke_comes_from_header = header_end_above.zip(local_top_y).is_some_and(
|
||||
|(last_repeated_header_end, local_top_y)| {
|
||||
// Check if the last repeated header row is above this line.
|
||||
//
|
||||
// Note that `y == last_repeated_header_end` is impossible for a
|
||||
// strictly repeated header (not in its original position).
|
||||
local_top_y < last_repeated_header_end && y > last_repeated_header_end
|
||||
},
|
||||
);
|
||||
|
||||
// Prioritize the footer's top stroke as well where applicable.
|
||||
let bottom_stroke_comes_from_footer = grid
|
||||
@ -637,7 +637,7 @@ mod test {
|
||||
},
|
||||
vec![],
|
||||
vec![],
|
||||
None,
|
||||
vec![],
|
||||
None,
|
||||
entries,
|
||||
)
|
||||
@ -1175,7 +1175,7 @@ mod test {
|
||||
},
|
||||
vec![],
|
||||
vec![],
|
||||
None,
|
||||
vec![],
|
||||
None,
|
||||
entries,
|
||||
)
|
||||
@ -1268,6 +1268,7 @@ mod test {
|
||||
grid,
|
||||
&rows,
|
||||
y.checked_sub(1),
|
||||
None,
|
||||
true,
|
||||
y,
|
||||
x,
|
||||
@ -1461,6 +1462,7 @@ mod test {
|
||||
grid,
|
||||
&rows,
|
||||
y.checked_sub(1),
|
||||
None,
|
||||
true,
|
||||
y,
|
||||
x,
|
||||
@ -1506,6 +1508,7 @@ mod test {
|
||||
grid,
|
||||
&rows,
|
||||
if y == 4 { Some(2) } else { y.checked_sub(1) },
|
||||
None,
|
||||
true,
|
||||
y,
|
||||
x,
|
||||
|
@ -1,57 +1,446 @@
|
||||
use std::ops::Deref;
|
||||
|
||||
use typst_library::diag::SourceResult;
|
||||
use typst_library::engine::Engine;
|
||||
use typst_library::layout::grid::resolve::{Footer, Header, Repeatable};
|
||||
use typst_library::layout::{Abs, Axes, Frame, Regions};
|
||||
|
||||
use super::layouter::GridLayouter;
|
||||
use super::layouter::{GridLayouter, RowState};
|
||||
use super::rowspans::UnbreakableRowGroup;
|
||||
|
||||
impl GridLayouter<'_> {
|
||||
/// Layouts the header's rows.
|
||||
/// Skips regions as necessary.
|
||||
pub fn layout_header(
|
||||
impl<'a> GridLayouter<'a> {
|
||||
/// Checks whether a region break could help a situation where we're out of
|
||||
/// space for the next row. The criteria are:
|
||||
///
|
||||
/// 1. If we could progress at the top of the region, that indicates the
|
||||
/// region has a backlog, or (if we're at the first region) a region break
|
||||
/// is at all possible (`regions.last` is `Some()`), so that's sufficient.
|
||||
///
|
||||
/// 2. Otherwise, we may progress if another region break is possible
|
||||
/// (`regions.last` is still `Some()`) and non-repeating rows have been
|
||||
/// placed, since that means the space they occupy will be available in the
|
||||
/// next region.
|
||||
#[inline]
|
||||
pub fn may_progress_with_repeats(&self) -> bool {
|
||||
// TODO(subfooters): check below isn't enough to detect non-repeating
|
||||
// footers... we can also change 'initial_after_repeats' to stop being
|
||||
// calculated if there were any non-repeating footers.
|
||||
self.current.could_progress_at_top
|
||||
|| self.regions.last.is_some()
|
||||
&& self.regions.size.y != self.current.initial_after_repeats
|
||||
}
|
||||
|
||||
pub fn place_new_headers(
|
||||
&mut self,
|
||||
consecutive_header_count: &mut usize,
|
||||
engine: &mut Engine,
|
||||
) -> SourceResult<()> {
|
||||
*consecutive_header_count += 1;
|
||||
let (consecutive_headers, new_upcoming_headers) =
|
||||
self.upcoming_headers.split_at(*consecutive_header_count);
|
||||
|
||||
if new_upcoming_headers.first().is_some_and(|next_header| {
|
||||
consecutive_headers.last().is_none_or(|latest_header| {
|
||||
!latest_header.short_lived
|
||||
&& next_header.range.start == latest_header.range.end
|
||||
}) && !next_header.short_lived
|
||||
}) {
|
||||
// More headers coming, so wait until we reach them.
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
self.upcoming_headers = new_upcoming_headers;
|
||||
*consecutive_header_count = 0;
|
||||
|
||||
let [first_header, ..] = consecutive_headers else {
|
||||
self.flush_orphans();
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
// Assuming non-conflicting headers sorted by increasing y, this must
|
||||
// be the header with the lowest level (sorted by increasing levels).
|
||||
let first_level = first_header.level;
|
||||
|
||||
// Stop repeating conflicting headers, even if the new headers are
|
||||
// short-lived or won't repeat.
|
||||
//
|
||||
// If we go to a new region before the new headers fit alongside their
|
||||
// children (or in general, for short-lived), the old headers should
|
||||
// not be displayed anymore.
|
||||
let first_conflicting_pos =
|
||||
self.repeating_headers.partition_point(|h| h.level < first_level);
|
||||
self.repeating_headers.truncate(first_conflicting_pos);
|
||||
|
||||
// Ensure upcoming rows won't see that these headers will occupy any
|
||||
// space in future regions anymore.
|
||||
for removed_height in
|
||||
self.current.repeating_header_heights.drain(first_conflicting_pos..)
|
||||
{
|
||||
self.current.repeating_header_height -= removed_height;
|
||||
}
|
||||
|
||||
// Layout short-lived headers immediately.
|
||||
if consecutive_headers.last().is_some_and(|h| h.short_lived) {
|
||||
// No chance of orphans as we're immediately placing conflicting
|
||||
// headers afterwards, which basically are not headers, for all intents
|
||||
// and purposes. It is therefore guaranteed that all new headers have
|
||||
// been placed at least once.
|
||||
self.flush_orphans();
|
||||
|
||||
// Layout each conflicting header independently, without orphan
|
||||
// prevention (as they don't go into 'pending_headers').
|
||||
// These headers are short-lived as they are immediately followed by a
|
||||
// header of the same or lower level, such that they never actually get
|
||||
// to repeat.
|
||||
self.layout_new_headers(consecutive_headers, true, engine)?;
|
||||
} else {
|
||||
// Let's try to place pending headers at least once.
|
||||
// This might be a waste as we could generate an orphan and thus have
|
||||
// to try to place old and new headers all over again, but that happens
|
||||
// for every new region anyway, so it's rather unavoidable.
|
||||
let snapshot_created =
|
||||
self.layout_new_headers(consecutive_headers, false, engine)?;
|
||||
|
||||
// Queue the new headers for layout. They will remain in this
|
||||
// vector due to orphan prevention.
|
||||
//
|
||||
// After the first subsequent row is laid out, move to repeating, as
|
||||
// it's then confirmed the headers won't be moved due to orphan
|
||||
// prevention anymore.
|
||||
self.pending_headers = consecutive_headers;
|
||||
|
||||
if !snapshot_created {
|
||||
// Region probably couldn't progress.
|
||||
//
|
||||
// Mark new pending headers as final and ensure there isn't a
|
||||
// snapshot.
|
||||
self.flush_orphans();
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Lays out rows belonging to a header, returning the calculated header
|
||||
/// height only for that header. Indicates to the laid out rows that they
|
||||
/// should inform their laid out heights if appropriate (auto or fixed
|
||||
/// size rows only).
|
||||
#[inline]
|
||||
fn layout_header_rows(
|
||||
&mut self,
|
||||
header: &Header,
|
||||
engine: &mut Engine,
|
||||
disambiguator: usize,
|
||||
) -> SourceResult<()> {
|
||||
let header_rows =
|
||||
self.simulate_header(header, &self.regions, engine, disambiguator)?;
|
||||
let mut skipped_region = false;
|
||||
while self.unbreakable_rows_left == 0
|
||||
&& !self.regions.size.y.fits(header_rows.height + self.footer_height)
|
||||
&& self.regions.may_progress()
|
||||
{
|
||||
// Advance regions without any output until we can place the
|
||||
// header and the footer.
|
||||
self.finish_region_internal(Frame::soft(Axes::splat(Abs::zero())), vec![]);
|
||||
skipped_region = true;
|
||||
as_short_lived: bool,
|
||||
) -> SourceResult<Abs> {
|
||||
let mut header_height = Abs::zero();
|
||||
for y in header.range.clone() {
|
||||
header_height += self
|
||||
.layout_row_with_state(
|
||||
y,
|
||||
engine,
|
||||
disambiguator,
|
||||
RowState {
|
||||
current_row_height: Some(Abs::zero()),
|
||||
in_active_repeatable: !as_short_lived,
|
||||
},
|
||||
)?
|
||||
.current_row_height
|
||||
.unwrap_or_default();
|
||||
}
|
||||
Ok(header_height)
|
||||
}
|
||||
|
||||
/// This function should be called each time an additional row has been
|
||||
/// laid out in a region to indicate that orphan prevention has succeeded.
|
||||
///
|
||||
/// It removes the current orphan snapshot and flushes pending headers,
|
||||
/// such that a non-repeating header won't try to be laid out again
|
||||
/// anymore, and a repeating header will begin to be part of
|
||||
/// `repeating_headers`.
|
||||
pub fn flush_orphans(&mut self) {
|
||||
self.current.lrows_orphan_snapshot = None;
|
||||
self.flush_pending_headers();
|
||||
}
|
||||
|
||||
/// Indicates all currently pending headers have been successfully placed
|
||||
/// once, since another row has been placed after them, so they are
|
||||
/// certainly not orphans.
|
||||
pub fn flush_pending_headers(&mut self) {
|
||||
if self.pending_headers.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Reset the header height for this region.
|
||||
// It will be re-calculated when laying out each header row.
|
||||
self.header_height = Abs::zero();
|
||||
|
||||
if let Some(Repeatable::Repeated(footer)) = &self.grid.footer {
|
||||
if skipped_region {
|
||||
// Simulate the footer again; the region's 'full' might have
|
||||
// changed.
|
||||
self.footer_height = self
|
||||
.simulate_footer(footer, &self.regions, engine, disambiguator)?
|
||||
.height;
|
||||
for header in self.pending_headers {
|
||||
if header.repeated {
|
||||
// Vector remains sorted by increasing levels:
|
||||
// - 'pending_headers' themselves are sorted, since we only
|
||||
// push non-mutually-conflicting headers at a time.
|
||||
// - Before pushing new pending headers in
|
||||
// 'layout_new_pending_headers', we truncate repeating headers
|
||||
// to remove anything with the same or higher levels as the
|
||||
// first pending header.
|
||||
// - Assuming it was sorted before, that truncation only keeps
|
||||
// elements with a lower level.
|
||||
// - Therefore, by pushing this header to the end, it will have
|
||||
// a level larger than all the previous headers, and is thus
|
||||
// in its 'correct' position.
|
||||
self.repeating_headers.push(header);
|
||||
}
|
||||
}
|
||||
|
||||
// Header is unbreakable.
|
||||
self.pending_headers = Default::default();
|
||||
}
|
||||
|
||||
/// Lays out the rows of repeating and pending headers at the top of the
|
||||
/// region.
|
||||
///
|
||||
/// Assumes the footer height for the current region has already been
|
||||
/// calculated. Skips regions as necessary to fit all headers and all
|
||||
/// footers.
|
||||
pub fn layout_active_headers(&mut self, engine: &mut Engine) -> SourceResult<()> {
|
||||
// Generate different locations for content in headers across its
|
||||
// repetitions by assigning a unique number for each one.
|
||||
let disambiguator = self.finished.len();
|
||||
|
||||
let header_height = self.simulate_header_height(
|
||||
self.repeating_headers
|
||||
.iter()
|
||||
.copied()
|
||||
.chain(self.pending_headers.iter().map(Repeatable::deref)),
|
||||
&self.regions,
|
||||
engine,
|
||||
disambiguator,
|
||||
)?;
|
||||
|
||||
// We already take the footer into account below.
|
||||
// While skipping regions, footer height won't be automatically
|
||||
// re-calculated until the end.
|
||||
let mut skipped_region = false;
|
||||
while self.unbreakable_rows_left == 0
|
||||
&& !self.regions.size.y.fits(header_height)
|
||||
&& self.may_progress_with_repeats()
|
||||
{
|
||||
// Advance regions without any output until we can place the
|
||||
// header and the footer.
|
||||
self.finish_region_internal(
|
||||
Frame::soft(Axes::splat(Abs::zero())),
|
||||
vec![],
|
||||
Default::default(),
|
||||
);
|
||||
|
||||
// TODO(layout model): re-calculate heights of headers and footers
|
||||
// on each region if 'full' changes? (Assuming height doesn't
|
||||
// change for now...)
|
||||
//
|
||||
// Would remove the footer height update below (move it here).
|
||||
skipped_region = true;
|
||||
|
||||
self.regions.size.y -= self.current.footer_height;
|
||||
self.current.initial_after_repeats = self.regions.size.y;
|
||||
}
|
||||
|
||||
if let Some(footer) = &self.grid.footer {
|
||||
if footer.repeated && skipped_region {
|
||||
// Simulate the footer again; the region's 'full' might have
|
||||
// changed.
|
||||
self.regions.size.y += self.current.footer_height;
|
||||
self.current.footer_height = self
|
||||
.simulate_footer(footer, &self.regions, engine, disambiguator)?
|
||||
.height;
|
||||
self.regions.size.y -= self.current.footer_height;
|
||||
}
|
||||
}
|
||||
|
||||
let repeating_header_rows =
|
||||
total_header_row_count(self.repeating_headers.iter().copied());
|
||||
|
||||
let pending_header_rows =
|
||||
total_header_row_count(self.pending_headers.iter().map(Repeatable::deref));
|
||||
|
||||
// Group of headers is unbreakable.
|
||||
// Thus, no risk of 'finish_region' being recursively called from
|
||||
// within 'layout_row'.
|
||||
self.unbreakable_rows_left += header.end;
|
||||
for y in 0..header.end {
|
||||
self.layout_row(y, engine, disambiguator)?;
|
||||
self.unbreakable_rows_left += repeating_header_rows + pending_header_rows;
|
||||
|
||||
self.current.last_repeated_header_end =
|
||||
self.repeating_headers.last().map(|h| h.range.end).unwrap_or_default();
|
||||
|
||||
// Reset the header height for this region.
|
||||
// It will be re-calculated when laying out each header row.
|
||||
self.current.repeating_header_height = Abs::zero();
|
||||
self.current.repeating_header_heights.clear();
|
||||
|
||||
debug_assert!(self.current.lrows.is_empty());
|
||||
debug_assert!(self.current.lrows_orphan_snapshot.is_none());
|
||||
let may_progress = self.may_progress_with_repeats();
|
||||
|
||||
if may_progress {
|
||||
// Enable orphan prevention for headers at the top of the region.
|
||||
// Otherwise, we will flush pending headers below, after laying
|
||||
// them out.
|
||||
//
|
||||
// It is very rare for this to make a difference as we're usually
|
||||
// at the 'last' region after the first skip, at which the snapshot
|
||||
// is handled by 'layout_new_headers'. Either way, we keep this
|
||||
// here for correctness.
|
||||
self.current.lrows_orphan_snapshot = Some(self.current.lrows.len());
|
||||
}
|
||||
|
||||
// Use indices to avoid double borrow. We don't mutate headers in
|
||||
// 'layout_row' so this is fine.
|
||||
let mut i = 0;
|
||||
while let Some(&header) = self.repeating_headers.get(i) {
|
||||
let header_height =
|
||||
self.layout_header_rows(header, engine, disambiguator, false)?;
|
||||
self.current.repeating_header_height += header_height;
|
||||
|
||||
// We assume that this vector will be sorted according
|
||||
// to increasing levels like 'repeating_headers' and
|
||||
// 'pending_headers' - and, in particular, their union, as this
|
||||
// vector is pushed repeating heights from both.
|
||||
//
|
||||
// This is guaranteed by:
|
||||
// 1. We always push pending headers after repeating headers,
|
||||
// as we assume they don't conflict because we remove
|
||||
// conflicting repeating headers when pushing a new pending
|
||||
// header.
|
||||
//
|
||||
// 2. We push in the same order as each.
|
||||
//
|
||||
// 3. This vector is also modified when pushing a new pending
|
||||
// header, where we remove heights for conflicting repeating
|
||||
// headers which have now stopped repeating. They are always at
|
||||
// the end and new pending headers respect the existing sort,
|
||||
// so the vector will remain sorted.
|
||||
self.current.repeating_header_heights.push(header_height);
|
||||
|
||||
i += 1;
|
||||
}
|
||||
|
||||
self.current.repeated_header_rows = self.current.lrows.len();
|
||||
self.current.initial_after_repeats = self.regions.size.y;
|
||||
|
||||
let mut has_non_repeated_pending_header = false;
|
||||
for header in self.pending_headers {
|
||||
if !header.repeated {
|
||||
self.current.initial_after_repeats = self.regions.size.y;
|
||||
has_non_repeated_pending_header = true;
|
||||
}
|
||||
let header_height =
|
||||
self.layout_header_rows(header, engine, disambiguator, false)?;
|
||||
if header.repeated {
|
||||
self.current.repeating_header_height += header_height;
|
||||
self.current.repeating_header_heights.push(header_height);
|
||||
}
|
||||
}
|
||||
|
||||
if !has_non_repeated_pending_header {
|
||||
self.current.initial_after_repeats = self.regions.size.y;
|
||||
}
|
||||
|
||||
if !may_progress {
|
||||
// Flush pending headers immediately, as placing them again later
|
||||
// won't help.
|
||||
self.flush_orphans();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Lays out headers found for the first time during row layout.
|
||||
///
|
||||
/// If 'short_lived' is true, these headers are immediately followed by
|
||||
/// a conflicting header, so it is assumed they will not be pushed to
|
||||
/// pending headers.
|
||||
///
|
||||
/// Returns whether orphan prevention was successfully setup, or couldn't
|
||||
/// due to short-lived headers or the region couldn't progress.
|
||||
pub fn layout_new_headers(
|
||||
&mut self,
|
||||
headers: &'a [Repeatable<Header>],
|
||||
short_lived: bool,
|
||||
engine: &mut Engine,
|
||||
) -> SourceResult<bool> {
|
||||
// At first, only consider the height of the given headers. However,
|
||||
// for upcoming regions, we will have to consider repeating headers as
|
||||
// well.
|
||||
let header_height = self.simulate_header_height(
|
||||
headers.iter().map(Repeatable::deref),
|
||||
&self.regions,
|
||||
engine,
|
||||
0,
|
||||
)?;
|
||||
|
||||
while self.unbreakable_rows_left == 0
|
||||
&& !self.regions.size.y.fits(header_height)
|
||||
&& self.may_progress_with_repeats()
|
||||
{
|
||||
// Note that, after the first region skip, the new headers will go
|
||||
// at the top of the region, but after the repeating headers that
|
||||
// remained (which will be automatically placed in 'finish_region').
|
||||
self.finish_region(engine, false)?;
|
||||
}
|
||||
|
||||
// Remove new headers at the end of the region if the upcoming row
|
||||
// doesn't fit.
|
||||
// TODO(subfooters): what if there is a footer right after it?
|
||||
let should_snapshot = !short_lived
|
||||
&& self.current.lrows_orphan_snapshot.is_none()
|
||||
&& self.may_progress_with_repeats();
|
||||
|
||||
if should_snapshot {
|
||||
// If we don't enter this branch while laying out non-short lived
|
||||
// headers, that means we will have to immediately flush pending
|
||||
// headers and mark them as final, since trying to place them in
|
||||
// the next page won't help get more space.
|
||||
self.current.lrows_orphan_snapshot = Some(self.current.lrows.len());
|
||||
}
|
||||
|
||||
let mut at_top = self.regions.size.y == self.current.initial_after_repeats;
|
||||
|
||||
self.unbreakable_rows_left +=
|
||||
total_header_row_count(headers.iter().map(Repeatable::deref));
|
||||
|
||||
for header in headers {
|
||||
let header_height = self.layout_header_rows(header, engine, 0, false)?;
|
||||
|
||||
// Only store this header height if it is actually going to
|
||||
// become a pending header. Otherwise, pretend it's not a
|
||||
// header... This is fine for consumers of 'header_height' as
|
||||
// it is guaranteed this header won't appear in a future
|
||||
// region, so multi-page rows and cells can effectively ignore
|
||||
// this header.
|
||||
if !short_lived && header.repeated {
|
||||
self.current.repeating_header_height += header_height;
|
||||
self.current.repeating_header_heights.push(header_height);
|
||||
if at_top {
|
||||
self.current.initial_after_repeats = self.regions.size.y;
|
||||
}
|
||||
} else {
|
||||
at_top = false;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(should_snapshot)
|
||||
}
|
||||
|
||||
/// Calculates the total expected height of several headers.
|
||||
pub fn simulate_header_height<'h: 'a>(
|
||||
&self,
|
||||
headers: impl IntoIterator<Item = &'h Header>,
|
||||
regions: &Regions<'_>,
|
||||
engine: &mut Engine,
|
||||
disambiguator: usize,
|
||||
) -> SourceResult<Abs> {
|
||||
let mut height = Abs::zero();
|
||||
for header in headers {
|
||||
height +=
|
||||
self.simulate_header(header, regions, engine, disambiguator)?.height;
|
||||
}
|
||||
Ok(height)
|
||||
}
|
||||
|
||||
/// Simulate the header's group of rows.
|
||||
pub fn simulate_header(
|
||||
&self,
|
||||
@ -66,8 +455,8 @@ impl GridLayouter<'_> {
|
||||
// assume that the amount of unbreakable rows following the first row
|
||||
// in the header will be precisely the rows in the header.
|
||||
self.simulate_unbreakable_row_group(
|
||||
0,
|
||||
Some(header.end),
|
||||
header.range.start,
|
||||
Some(header.range.end - header.range.start),
|
||||
regions,
|
||||
engine,
|
||||
disambiguator,
|
||||
@ -91,11 +480,22 @@ impl GridLayouter<'_> {
|
||||
{
|
||||
// Advance regions without any output until we can place the
|
||||
// footer.
|
||||
self.finish_region_internal(Frame::soft(Axes::splat(Abs::zero())), vec![]);
|
||||
self.finish_region_internal(
|
||||
Frame::soft(Axes::splat(Abs::zero())),
|
||||
vec![],
|
||||
Default::default(),
|
||||
);
|
||||
skipped_region = true;
|
||||
}
|
||||
|
||||
self.footer_height = if skipped_region {
|
||||
// TODO(subfooters): Consider resetting header height etc. if we skip
|
||||
// region. (Maybe move that step to `finish_region_internal`.)
|
||||
//
|
||||
// That is unnecessary at the moment as 'prepare_footers' is only
|
||||
// called at the start of the region, so header height is always zero
|
||||
// and no headers were placed so far, but what about when we can have
|
||||
// footers in the middle of the region? Let's think about this then.
|
||||
self.current.footer_height = if skipped_region {
|
||||
// Simulate the footer again; the region's 'full' might have
|
||||
// changed.
|
||||
self.simulate_footer(footer, &self.regions, engine, disambiguator)?
|
||||
@ -118,12 +518,22 @@ impl GridLayouter<'_> {
|
||||
// Ensure footer rows have their own height available.
|
||||
// Won't change much as we're creating an unbreakable row group
|
||||
// anyway, so this is mostly for correctness.
|
||||
self.regions.size.y += self.footer_height;
|
||||
self.regions.size.y += self.current.footer_height;
|
||||
|
||||
let repeats = self.grid.footer.as_ref().is_some_and(|f| f.repeated);
|
||||
let footer_len = self.grid.rows.len() - footer.start;
|
||||
self.unbreakable_rows_left += footer_len;
|
||||
|
||||
for y in footer.start..self.grid.rows.len() {
|
||||
self.layout_row(y, engine, disambiguator)?;
|
||||
self.layout_row_with_state(
|
||||
y,
|
||||
engine,
|
||||
disambiguator,
|
||||
RowState {
|
||||
in_active_repeatable: repeats,
|
||||
..Default::default()
|
||||
},
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@ -144,10 +554,18 @@ impl GridLayouter<'_> {
|
||||
// in the footer will be precisely the rows in the footer.
|
||||
self.simulate_unbreakable_row_group(
|
||||
footer.start,
|
||||
Some(self.grid.rows.len() - footer.start),
|
||||
Some(footer.end - footer.start),
|
||||
regions,
|
||||
engine,
|
||||
disambiguator,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// The total amount of rows in the given list of headers.
|
||||
#[inline]
|
||||
pub fn total_header_row_count<'h>(
|
||||
headers: impl IntoIterator<Item = &'h Header>,
|
||||
) -> usize {
|
||||
headers.into_iter().map(|h| h.range.end - h.range.start).sum()
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ use typst_library::foundations::Resolve;
|
||||
use typst_library::layout::grid::resolve::Repeatable;
|
||||
use typst_library::layout::{Abs, Axes, Frame, Point, Region, Regions, Size, Sizing};
|
||||
|
||||
use super::layouter::{in_last_with_offset, points, Row, RowPiece};
|
||||
use super::layouter::{points, Row};
|
||||
use super::{layout_cell, Cell, GridLayouter};
|
||||
|
||||
/// All information needed to layout a single rowspan.
|
||||
@ -90,10 +90,10 @@ pub struct CellMeasurementData<'layouter> {
|
||||
|
||||
impl GridLayouter<'_> {
|
||||
/// Layout a rowspan over the already finished regions, plus the current
|
||||
/// region's frame and resolved rows, if it wasn't finished yet (because
|
||||
/// we're being called from `finish_region`, but note that this function is
|
||||
/// also called once after all regions are finished, in which case
|
||||
/// `current_region_data` is `None`).
|
||||
/// region's frame and height of resolved header rows, if it wasn't
|
||||
/// finished yet (because we're being called from `finish_region`, but note
|
||||
/// that this function is also called once after all regions are finished,
|
||||
/// in which case `current_region_data` is `None`).
|
||||
///
|
||||
/// We need to do this only once we already know the heights of all
|
||||
/// spanned rows, which is only possible after laying out the last row
|
||||
@ -101,7 +101,7 @@ impl GridLayouter<'_> {
|
||||
pub fn layout_rowspan(
|
||||
&mut self,
|
||||
rowspan_data: Rowspan,
|
||||
current_region_data: Option<(&mut Frame, &[RowPiece])>,
|
||||
current_region_data: Option<(&mut Frame, Abs)>,
|
||||
engine: &mut Engine,
|
||||
) -> SourceResult<()> {
|
||||
let Rowspan {
|
||||
@ -146,11 +146,31 @@ impl GridLayouter<'_> {
|
||||
|
||||
// Push the layouted frames directly into the finished frames.
|
||||
let fragment = layout_cell(cell, engine, disambiguator, self.styles, pod)?;
|
||||
let (current_region, current_rrows) = current_region_data.unzip();
|
||||
for ((i, finished), frame) in self
|
||||
let (current_region, current_header_row_height) = current_region_data.unzip();
|
||||
|
||||
// Clever trick to process finished header rows:
|
||||
// - If there are grid headers, the vector will be filled with one
|
||||
// finished header row height per region, so, chaining with the height
|
||||
// for the current one, we get the header row height for each region.
|
||||
//
|
||||
// - But if there are no grid headers, the vector will be empty, so in
|
||||
// theory the regions and resolved header row heights wouldn't match.
|
||||
// But that's fine - 'current_header_row_height' can only be either
|
||||
// 'Some(zero)' or 'None' in such a case, and for all other rows we
|
||||
// append infinite zeros. That is, in such a case, the resolved header
|
||||
// row height is always zero, so that's our fallback.
|
||||
let finished_header_rows = self
|
||||
.finished_header_rows
|
||||
.iter()
|
||||
.map(|info| info.repeated_height)
|
||||
.chain(current_header_row_height)
|
||||
.chain(std::iter::repeat(Abs::zero()));
|
||||
|
||||
for ((i, (finished, header_dy)), frame) in self
|
||||
.finished
|
||||
.iter_mut()
|
||||
.chain(current_region.into_iter())
|
||||
.zip(finished_header_rows)
|
||||
.skip(first_region)
|
||||
.enumerate()
|
||||
.zip(fragment)
|
||||
@ -162,22 +182,9 @@ impl GridLayouter<'_> {
|
||||
} else {
|
||||
// The rowspan continuation starts after the header (thus,
|
||||
// at a position after the sum of the laid out header
|
||||
// rows).
|
||||
if let Some(Repeatable::Repeated(header)) = &self.grid.header {
|
||||
let header_rows = self
|
||||
.rrows
|
||||
.get(i)
|
||||
.map(Vec::as_slice)
|
||||
.or(current_rrows)
|
||||
.unwrap_or(&[])
|
||||
.iter()
|
||||
.take_while(|row| row.y < header.end);
|
||||
|
||||
header_rows.map(|row| row.height).sum()
|
||||
} else {
|
||||
// Without a header, start at the very top of the region.
|
||||
Abs::zero()
|
||||
}
|
||||
// rows). Without a header, this is zero, so the rowspan can
|
||||
// start at the very top of the region as usual.
|
||||
header_dy
|
||||
};
|
||||
|
||||
finished.push_frame(Point::new(dx, dy), frame);
|
||||
@ -231,15 +238,13 @@ impl GridLayouter<'_> {
|
||||
// current row is dynamic and depends on the amount of upcoming
|
||||
// unbreakable cells (with or without a rowspan setting).
|
||||
let mut amount_unbreakable_rows = None;
|
||||
if let Some(Repeatable::NotRepeated(header)) = &self.grid.header {
|
||||
if current_row < header.end {
|
||||
// Non-repeated header, so keep it unbreakable.
|
||||
amount_unbreakable_rows = Some(header.end);
|
||||
}
|
||||
}
|
||||
if let Some(Repeatable::NotRepeated(footer)) = &self.grid.footer {
|
||||
if current_row >= footer.start {
|
||||
if let Some(footer) = &self.grid.footer {
|
||||
if !footer.repeated && current_row >= footer.start {
|
||||
// Non-repeated footer, so keep it unbreakable.
|
||||
//
|
||||
// TODO(subfooters): This will become unnecessary
|
||||
// once non-repeated footers are treated differently and
|
||||
// have widow prevention.
|
||||
amount_unbreakable_rows = Some(self.grid.rows.len() - footer.start);
|
||||
}
|
||||
}
|
||||
@ -254,10 +259,7 @@ impl GridLayouter<'_> {
|
||||
|
||||
// Skip to fitting region.
|
||||
while !self.regions.size.y.fits(row_group.height)
|
||||
&& !in_last_with_offset(
|
||||
self.regions,
|
||||
self.header_height + self.footer_height,
|
||||
)
|
||||
&& self.may_progress_with_repeats()
|
||||
{
|
||||
self.finish_region(engine, false)?;
|
||||
}
|
||||
@ -396,16 +398,29 @@ impl GridLayouter<'_> {
|
||||
// auto rows don't depend on the backlog, as they only span one
|
||||
// region.
|
||||
if breakable
|
||||
&& (matches!(self.grid.header, Some(Repeatable::Repeated(_)))
|
||||
|| matches!(self.grid.footer, Some(Repeatable::Repeated(_))))
|
||||
&& (!self.repeating_headers.is_empty()
|
||||
|| !self.pending_headers.is_empty()
|
||||
|| matches!(&self.grid.footer, Some(footer) if footer.repeated))
|
||||
{
|
||||
// Subtract header and footer height from all upcoming regions
|
||||
// when measuring the cell, including the last repeated region.
|
||||
//
|
||||
// This will update the 'custom_backlog' vector with the
|
||||
// updated heights of the upcoming regions.
|
||||
//
|
||||
// We predict that header height will only include that of
|
||||
// repeating headers, as we can assume non-repeating headers in
|
||||
// the first region have been successfully placed, unless
|
||||
// something didn't fit on the first region of the auto row,
|
||||
// but we will only find that out after measurement, and if
|
||||
// that happens, we discard the measurement and try again.
|
||||
let mapped_regions = self.regions.map(&mut custom_backlog, |size| {
|
||||
Size::new(size.x, size.y - self.header_height - self.footer_height)
|
||||
Size::new(
|
||||
size.x,
|
||||
size.y
|
||||
- self.current.repeating_header_height
|
||||
- self.current.footer_height,
|
||||
)
|
||||
});
|
||||
|
||||
// Callees must use the custom backlog instead of the current
|
||||
@ -459,6 +474,7 @@ impl GridLayouter<'_> {
|
||||
// Height of the rowspan covered by spanned rows in the current
|
||||
// region.
|
||||
let laid_out_height: Abs = self
|
||||
.current
|
||||
.lrows
|
||||
.iter()
|
||||
.filter_map(|row| match row {
|
||||
@ -506,7 +522,12 @@ impl GridLayouter<'_> {
|
||||
.iter()
|
||||
.copied()
|
||||
.chain(std::iter::once(if breakable {
|
||||
self.initial.y - self.header_height - self.footer_height
|
||||
// Here we are calculating the available height for a
|
||||
// rowspan from the top of the current region, so
|
||||
// we have to use initial header heights (note that
|
||||
// header height can change in the middle of the
|
||||
// region).
|
||||
self.current.initial_after_repeats
|
||||
} else {
|
||||
// When measuring unbreakable auto rows, infinite
|
||||
// height is available for content to expand.
|
||||
@ -518,11 +539,13 @@ impl GridLayouter<'_> {
|
||||
// rowspan's already laid out heights with the current
|
||||
// region's height and current backlog to ensure a good
|
||||
// level of accuracy in the measurements.
|
||||
let backlog = self
|
||||
.regions
|
||||
.backlog
|
||||
.iter()
|
||||
.map(|&size| size - self.header_height - self.footer_height);
|
||||
//
|
||||
// Assume only repeating headers will survive starting at
|
||||
// the next region.
|
||||
let backlog = self.regions.backlog.iter().map(|&size| {
|
||||
size - self.current.repeating_header_height
|
||||
- self.current.footer_height
|
||||
});
|
||||
|
||||
heights_up_to_current_region.chain(backlog).collect::<Vec<_>>()
|
||||
} else {
|
||||
@ -536,10 +559,10 @@ impl GridLayouter<'_> {
|
||||
height = *rowspan_height;
|
||||
backlog = None;
|
||||
full = rowspan_full;
|
||||
last = self
|
||||
.regions
|
||||
.last
|
||||
.map(|size| size - self.header_height - self.footer_height);
|
||||
last = self.regions.last.map(|size| {
|
||||
size - self.current.repeating_header_height
|
||||
- self.current.footer_height
|
||||
});
|
||||
} else {
|
||||
// The rowspan started in the current region, as its vector
|
||||
// of heights in regions is currently empty.
|
||||
@ -741,10 +764,11 @@ impl GridLayouter<'_> {
|
||||
simulated_regions.next();
|
||||
disambiguator += 1;
|
||||
|
||||
// Subtract the initial header and footer height, since that's the
|
||||
// height we used when subtracting from the region backlog's
|
||||
// Subtract the repeating header and footer height, since that's
|
||||
// the height we used when subtracting from the region backlog's
|
||||
// heights while measuring cells.
|
||||
simulated_regions.size.y -= self.header_height + self.footer_height;
|
||||
simulated_regions.size.y -=
|
||||
self.current.repeating_header_height + self.current.footer_height;
|
||||
}
|
||||
|
||||
if let Some(original_last_resolved_size) = last_resolved_size {
|
||||
@ -876,12 +900,8 @@ impl GridLayouter<'_> {
|
||||
// which, when used and combined with upcoming spanned rows, covers all
|
||||
// of the requested rowspan height, we give up.
|
||||
for _attempt in 0..5 {
|
||||
let rowspan_simulator = RowspanSimulator::new(
|
||||
disambiguator,
|
||||
simulated_regions,
|
||||
self.header_height,
|
||||
self.footer_height,
|
||||
);
|
||||
let rowspan_simulator =
|
||||
RowspanSimulator::new(disambiguator, simulated_regions, &self.current);
|
||||
|
||||
let total_spanned_height = rowspan_simulator.simulate_rowspan_layout(
|
||||
y,
|
||||
@ -963,7 +983,8 @@ impl GridLayouter<'_> {
|
||||
{
|
||||
extra_amount_to_grow -= simulated_regions.size.y.max(Abs::zero());
|
||||
simulated_regions.next();
|
||||
simulated_regions.size.y -= self.header_height + self.footer_height;
|
||||
simulated_regions.size.y -=
|
||||
self.current.repeating_header_height + self.current.footer_height;
|
||||
disambiguator += 1;
|
||||
}
|
||||
simulated_regions.size.y -= extra_amount_to_grow;
|
||||
@ -980,10 +1001,17 @@ struct RowspanSimulator<'a> {
|
||||
finished: usize,
|
||||
/// The state of regions during the simulation.
|
||||
regions: Regions<'a>,
|
||||
/// The height of the header in the currently simulated region.
|
||||
/// The total height of headers in the currently simulated region.
|
||||
header_height: Abs,
|
||||
/// The height of the footer in the currently simulated region.
|
||||
/// The total height of footers in the currently simulated region.
|
||||
footer_height: Abs,
|
||||
/// Whether `self.regions.may_progress()` was `true` at the top of the
|
||||
/// region, indicating we can progress anywhere in the current region,
|
||||
/// even right after a repeated header.
|
||||
could_progress_at_top: bool,
|
||||
/// Available height after laying out repeated headers at the top of the
|
||||
/// currently simulated region.
|
||||
initial_after_repeats: Abs,
|
||||
/// The total spanned height so far in the simulation.
|
||||
total_spanned_height: Abs,
|
||||
/// Height of the latest spanned gutter row in the simulation.
|
||||
@ -997,14 +1025,19 @@ impl<'a> RowspanSimulator<'a> {
|
||||
fn new(
|
||||
finished: usize,
|
||||
regions: Regions<'a>,
|
||||
header_height: Abs,
|
||||
footer_height: Abs,
|
||||
current: &super::layouter::Current,
|
||||
) -> Self {
|
||||
Self {
|
||||
finished,
|
||||
regions,
|
||||
header_height,
|
||||
footer_height,
|
||||
// There can be no new headers or footers within a multi-page
|
||||
// rowspan, since headers and footers are unbreakable, so
|
||||
// assuming the repeating header height and footer height
|
||||
// won't change is safe.
|
||||
header_height: current.repeating_header_height,
|
||||
footer_height: current.footer_height,
|
||||
could_progress_at_top: current.could_progress_at_top,
|
||||
initial_after_repeats: current.initial_after_repeats,
|
||||
total_spanned_height: Abs::zero(),
|
||||
latest_spanned_gutter_height: Abs::zero(),
|
||||
}
|
||||
@ -1053,10 +1086,7 @@ impl<'a> RowspanSimulator<'a> {
|
||||
0,
|
||||
)?;
|
||||
while !self.regions.size.y.fits(row_group.height)
|
||||
&& !in_last_with_offset(
|
||||
self.regions,
|
||||
self.header_height + self.footer_height,
|
||||
)
|
||||
&& self.may_progress_with_repeats()
|
||||
{
|
||||
self.finish_region(layouter, engine)?;
|
||||
}
|
||||
@ -1078,10 +1108,7 @@ impl<'a> RowspanSimulator<'a> {
|
||||
let mut skipped_region = false;
|
||||
while unbreakable_rows_left == 0
|
||||
&& !self.regions.size.y.fits(height)
|
||||
&& !in_last_with_offset(
|
||||
self.regions,
|
||||
self.header_height + self.footer_height,
|
||||
)
|
||||
&& self.may_progress_with_repeats()
|
||||
{
|
||||
self.finish_region(layouter, engine)?;
|
||||
|
||||
@ -1127,23 +1154,37 @@ impl<'a> RowspanSimulator<'a> {
|
||||
// our simulation checks what happens AFTER the auto row, so we can
|
||||
// just use the original backlog from `self.regions`.
|
||||
let disambiguator = self.finished;
|
||||
let header_height =
|
||||
if let Some(Repeatable::Repeated(header)) = &layouter.grid.header {
|
||||
layouter
|
||||
.simulate_header(header, &self.regions, engine, disambiguator)?
|
||||
.height
|
||||
} else {
|
||||
Abs::zero()
|
||||
};
|
||||
|
||||
let footer_height =
|
||||
if let Some(Repeatable::Repeated(footer)) = &layouter.grid.footer {
|
||||
layouter
|
||||
.simulate_footer(footer, &self.regions, engine, disambiguator)?
|
||||
.height
|
||||
} else {
|
||||
Abs::zero()
|
||||
};
|
||||
let (repeating_headers, header_height) = if !layouter.repeating_headers.is_empty()
|
||||
|| !layouter.pending_headers.is_empty()
|
||||
{
|
||||
// Only repeating headers have survived after the first region
|
||||
// break.
|
||||
let repeating_headers = layouter.repeating_headers.iter().copied().chain(
|
||||
layouter.pending_headers.iter().filter_map(Repeatable::as_repeated),
|
||||
);
|
||||
|
||||
let header_height = layouter.simulate_header_height(
|
||||
repeating_headers.clone(),
|
||||
&self.regions,
|
||||
engine,
|
||||
disambiguator,
|
||||
)?;
|
||||
|
||||
(Some(repeating_headers), header_height)
|
||||
} else {
|
||||
(None, Abs::zero())
|
||||
};
|
||||
|
||||
let footer_height = if let Some(footer) =
|
||||
layouter.grid.footer.as_ref().and_then(Repeatable::as_repeated)
|
||||
{
|
||||
layouter
|
||||
.simulate_footer(footer, &self.regions, engine, disambiguator)?
|
||||
.height
|
||||
} else {
|
||||
Abs::zero()
|
||||
};
|
||||
|
||||
let mut skipped_region = false;
|
||||
|
||||
@ -1156,19 +1197,24 @@ impl<'a> RowspanSimulator<'a> {
|
||||
skipped_region = true;
|
||||
}
|
||||
|
||||
if let Some(Repeatable::Repeated(header)) = &layouter.grid.header {
|
||||
if let Some(repeating_headers) = repeating_headers {
|
||||
self.header_height = if skipped_region {
|
||||
// Simulate headers again, at the new region, as
|
||||
// the full region height may change.
|
||||
layouter
|
||||
.simulate_header(header, &self.regions, engine, disambiguator)?
|
||||
.height
|
||||
layouter.simulate_header_height(
|
||||
repeating_headers,
|
||||
&self.regions,
|
||||
engine,
|
||||
disambiguator,
|
||||
)?
|
||||
} else {
|
||||
header_height
|
||||
};
|
||||
}
|
||||
|
||||
if let Some(Repeatable::Repeated(footer)) = &layouter.grid.footer {
|
||||
if let Some(footer) =
|
||||
layouter.grid.footer.as_ref().and_then(Repeatable::as_repeated)
|
||||
{
|
||||
self.footer_height = if skipped_region {
|
||||
// Simulate footers again, at the new region, as
|
||||
// the full region height may change.
|
||||
@ -1185,6 +1231,7 @@ impl<'a> RowspanSimulator<'a> {
|
||||
// header or footer (as an invariant, any rowspans spanning any header
|
||||
// or footer rows are fully contained within that header's or footer's rows).
|
||||
self.regions.size.y -= self.header_height + self.footer_height;
|
||||
self.initial_after_repeats = self.regions.size.y;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -1201,8 +1248,18 @@ impl<'a> RowspanSimulator<'a> {
|
||||
self.regions.next();
|
||||
self.finished += 1;
|
||||
|
||||
self.could_progress_at_top = self.regions.may_progress();
|
||||
self.simulate_header_footer_layout(layouter, engine)
|
||||
}
|
||||
|
||||
/// Similar to [`GridLayouter::may_progress_with_repeats`] but for rowspan
|
||||
/// simulation.
|
||||
#[inline]
|
||||
fn may_progress_with_repeats(&self) -> bool {
|
||||
self.could_progress_at_top
|
||||
|| self.regions.last.is_some()
|
||||
&& self.regions.size.y != self.initial_after_repeats
|
||||
}
|
||||
}
|
||||
|
||||
/// Subtracts some size from the end of a vector of sizes.
|
||||
|
@ -1,6 +1,6 @@
|
||||
use std::ffi::OsStr;
|
||||
|
||||
use typst_library::diag::{warning, At, SourceResult, StrResult};
|
||||
use typst_library::diag::{warning, At, LoadedWithin, SourceResult, StrResult};
|
||||
use typst_library::engine::Engine;
|
||||
use typst_library::foundations::{Bytes, Derived, Packed, Smart, StyleChain};
|
||||
use typst_library::introspection::Locator;
|
||||
@ -27,17 +27,17 @@ pub fn layout_image(
|
||||
|
||||
// Take the format that was explicitly defined, or parse the extension,
|
||||
// or try to detect the format.
|
||||
let Derived { source, derived: data } = &elem.source;
|
||||
let Derived { source, derived: loaded } = &elem.source;
|
||||
let format = match elem.format(styles) {
|
||||
Smart::Custom(v) => v,
|
||||
Smart::Auto => determine_format(source, data).at(span)?,
|
||||
Smart::Auto => determine_format(source, &loaded.data).at(span)?,
|
||||
};
|
||||
|
||||
// Warn the user if the image contains a foreign object. Not perfect
|
||||
// because the svg could also be encoded, but that's an edge case.
|
||||
if format == ImageFormat::Vector(VectorFormat::Svg) {
|
||||
let has_foreign_object =
|
||||
data.as_str().is_ok_and(|s| s.contains("<foreignObject"));
|
||||
memchr::memmem::find(&loaded.data, b"<foreignObject").is_some();
|
||||
|
||||
if has_foreign_object {
|
||||
engine.sink.warn(warning!(
|
||||
@ -53,7 +53,7 @@ pub fn layout_image(
|
||||
let kind = match format {
|
||||
ImageFormat::Raster(format) => ImageKind::Raster(
|
||||
RasterImage::new(
|
||||
data.clone(),
|
||||
loaded.data.clone(),
|
||||
format,
|
||||
elem.icc(styles).as_ref().map(|icc| icc.derived.clone()),
|
||||
)
|
||||
@ -61,11 +61,11 @@ pub fn layout_image(
|
||||
),
|
||||
ImageFormat::Vector(VectorFormat::Svg) => ImageKind::Svg(
|
||||
SvgImage::with_fonts(
|
||||
data.clone(),
|
||||
loaded.data.clone(),
|
||||
engine.world,
|
||||
&families(styles).map(|f| f.as_str()).collect::<Vec<_>>(),
|
||||
)
|
||||
.at(span)?,
|
||||
.within(loaded)?,
|
||||
),
|
||||
};
|
||||
|
||||
|
@ -9,6 +9,7 @@ mod prepare;
|
||||
mod shaping;
|
||||
|
||||
pub use self::box_::layout_box;
|
||||
pub use self::shaping::create_shape_plan;
|
||||
|
||||
use comemo::{Track, Tracked, TrackedMut};
|
||||
use typst_library::diag::SourceResult;
|
||||
|
@ -1,18 +1,16 @@
|
||||
use std::borrow::Cow;
|
||||
use std::fmt::{self, Debug, Formatter};
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
|
||||
use az::SaturatingAs;
|
||||
use ecow::EcoString;
|
||||
use rustybuzz::{BufferFlags, ShapePlan, UnicodeBuffer};
|
||||
use ttf_parser::Tag;
|
||||
use typst_library::engine::Engine;
|
||||
use typst_library::foundations::{Smart, StyleChain};
|
||||
use typst_library::layout::{Abs, Dir, Em, Frame, FrameItem, Point, Size};
|
||||
use typst_library::text::{
|
||||
families, features, is_default_ignorable, variant, Font, FontFamily, FontVariant,
|
||||
Glyph, Lang, Region, TextEdgeBounds, TextElem, TextItem,
|
||||
families, features, is_default_ignorable, language, variant, Font, FontFamily,
|
||||
FontVariant, Glyph, Lang, Region, TextEdgeBounds, TextElem, TextItem,
|
||||
};
|
||||
use typst_library::World;
|
||||
use typst_utils::SliceExt;
|
||||
@ -295,6 +293,8 @@ impl<'a> ShapedText<'a> {
|
||||
+ justification_left
|
||||
+ justification_right,
|
||||
x_offset: shaped.x_offset + justification_left,
|
||||
y_advance: Em::zero(),
|
||||
y_offset: Em::zero(),
|
||||
range: (shaped.range.start - range.start).saturating_as()
|
||||
..(shaped.range.end - range.start).saturating_as(),
|
||||
span,
|
||||
@ -934,7 +934,7 @@ fn shape_segment<'a>(
|
||||
|
||||
/// Create a shape plan.
|
||||
#[comemo::memoize]
|
||||
fn create_shape_plan(
|
||||
pub fn create_shape_plan(
|
||||
font: &Font,
|
||||
direction: rustybuzz::Direction,
|
||||
script: rustybuzz::Script,
|
||||
@ -952,7 +952,7 @@ fn create_shape_plan(
|
||||
|
||||
/// Shape the text with tofus from the given font.
|
||||
fn shape_tofus(ctx: &mut ShapingContext, base: usize, text: &str, font: Font) {
|
||||
let x_advance = font.advance(0).unwrap_or_default();
|
||||
let x_advance = font.x_advance(0).unwrap_or_default();
|
||||
let add_glyph = |(cluster, c): (usize, char)| {
|
||||
let start = base + cluster;
|
||||
let end = start + c.len_utf8();
|
||||
@ -1044,20 +1044,8 @@ fn calculate_adjustability(ctx: &mut ShapingContext, lang: Lang, region: Option<
|
||||
|
||||
/// Difference between non-breaking and normal space.
|
||||
fn nbsp_delta(font: &Font) -> Option<Em> {
|
||||
let space = font.ttf().glyph_index(' ')?.0;
|
||||
let nbsp = font.ttf().glyph_index('\u{00A0}')?.0;
|
||||
Some(font.advance(nbsp)? - font.advance(space)?)
|
||||
}
|
||||
|
||||
/// Process the language and region of a style chain into a
|
||||
/// rustybuzz-compatible BCP 47 language.
|
||||
fn language(styles: StyleChain) -> rustybuzz::Language {
|
||||
let mut bcp: EcoString = TextElem::lang_in(styles).as_str().into();
|
||||
if let Some(region) = TextElem::region_in(styles) {
|
||||
bcp.push('-');
|
||||
bcp.push_str(region.as_str());
|
||||
}
|
||||
rustybuzz::Language::from_str(&bcp).unwrap()
|
||||
Some(font.x_advance(nbsp)? - font.space_width()?)
|
||||
}
|
||||
|
||||
/// Returns true if all glyphs in `glyphs` have ranges within the range `range`.
|
||||
|
@ -3,7 +3,10 @@ use typst_library::foundations::{Packed, StyleChain};
|
||||
use typst_library::layout::{Em, Frame, Point, Size};
|
||||
use typst_library::math::AccentElem;
|
||||
|
||||
use super::{style_cramped, FrameFragment, GlyphFragment, MathContext, MathFragment};
|
||||
use super::{
|
||||
style_cramped, style_dtls, style_flac, FrameFragment, GlyphFragment, MathContext,
|
||||
MathFragment,
|
||||
};
|
||||
|
||||
/// How much the accent can be shorter than the base.
|
||||
const ACCENT_SHORT_FALL: Em = Em::new(0.5);
|
||||
@ -15,40 +18,40 @@ pub fn layout_accent(
|
||||
ctx: &mut MathContext,
|
||||
styles: StyleChain,
|
||||
) -> SourceResult<()> {
|
||||
let cramped = style_cramped();
|
||||
let mut base = ctx.layout_into_fragment(&elem.base, styles.chain(&cramped))?;
|
||||
|
||||
let accent = elem.accent;
|
||||
let top_accent = !accent.is_bottom();
|
||||
|
||||
// Try to replace base glyph with its dotless variant.
|
||||
if top_accent && elem.dotless(styles) {
|
||||
if let MathFragment::Glyph(glyph) = &mut base {
|
||||
glyph.make_dotless_form(ctx);
|
||||
}
|
||||
}
|
||||
// Try to replace the base glyph with its dotless variant.
|
||||
let dtls = style_dtls();
|
||||
let base_styles =
|
||||
if top_accent && elem.dotless(styles) { styles.chain(&dtls) } else { styles };
|
||||
|
||||
let cramped = style_cramped();
|
||||
let base = ctx.layout_into_fragment(&elem.base, base_styles.chain(&cramped))?;
|
||||
|
||||
// Preserve class to preserve automatic spacing.
|
||||
let base_class = base.class();
|
||||
let base_attach = base.accent_attach();
|
||||
|
||||
let mut glyph = GlyphFragment::new(ctx, styles, accent.0, elem.span());
|
||||
// Try to replace the accent glyph with its flattened variant.
|
||||
let flattened_base_height = scaled!(ctx, styles, flattened_accent_base_height);
|
||||
let flac = style_flac();
|
||||
let accent_styles = if top_accent && base.ascent() > flattened_base_height {
|
||||
styles.chain(&flac)
|
||||
} else {
|
||||
styles
|
||||
};
|
||||
|
||||
// Try to replace accent glyph with its flattened variant.
|
||||
if top_accent {
|
||||
let flattened_base_height = scaled!(ctx, styles, flattened_accent_base_height);
|
||||
if base.ascent() > flattened_base_height {
|
||||
glyph.make_flattened_accent_form(ctx);
|
||||
}
|
||||
}
|
||||
let mut glyph =
|
||||
GlyphFragment::new_char(ctx.font, accent_styles, accent.0, elem.span())?;
|
||||
|
||||
// Forcing the accent to be at least as large as the base makes it too
|
||||
// wide in many case.
|
||||
// Forcing the accent to be at least as large as the base makes it too wide
|
||||
// in many cases.
|
||||
let width = elem.size(styles).relative_to(base.width());
|
||||
let short_fall = ACCENT_SHORT_FALL.at(glyph.font_size);
|
||||
let variant = glyph.stretch_horizontal(ctx, width - short_fall);
|
||||
let accent = variant.frame;
|
||||
let accent_attach = variant.accent_attach.0;
|
||||
let short_fall = ACCENT_SHORT_FALL.at(glyph.item.size);
|
||||
glyph.stretch_horizontal(ctx, width - short_fall);
|
||||
let accent_attach = glyph.accent_attach.0;
|
||||
let accent = glyph.into_frame();
|
||||
|
||||
let (gap, accent_pos, base_pos) = if top_accent {
|
||||
// Descent is negative because the accent's ink bottom is above the
|
||||
|
@ -66,7 +66,6 @@ pub fn layout_attach(
|
||||
let relative_to_width = measure!(t, width).max(measure!(b, width));
|
||||
stretch_fragment(
|
||||
ctx,
|
||||
styles,
|
||||
&mut base,
|
||||
Some(Axis::X),
|
||||
Some(relative_to_width),
|
||||
@ -220,7 +219,6 @@ fn layout_attachments(
|
||||
// Calculate the distance each pre-script extends to the left of the base's
|
||||
// width.
|
||||
let (tl_pre_width, bl_pre_width) = compute_pre_script_widths(
|
||||
ctx,
|
||||
&base,
|
||||
[tl.as_ref(), bl.as_ref()],
|
||||
(tx_shift, bx_shift),
|
||||
@ -231,7 +229,6 @@ fn layout_attachments(
|
||||
// base's width. Also calculate each post-script's kerning (we need this for
|
||||
// its position later).
|
||||
let ((tr_post_width, tr_kern), (br_post_width, br_kern)) = compute_post_script_widths(
|
||||
ctx,
|
||||
&base,
|
||||
[tr.as_ref(), br.as_ref()],
|
||||
(tx_shift, bx_shift),
|
||||
@ -287,14 +284,13 @@ fn layout_attachments(
|
||||
/// post-script's kerning value. The first tuple is for the post-superscript,
|
||||
/// and the second is for the post-subscript.
|
||||
fn compute_post_script_widths(
|
||||
ctx: &MathContext,
|
||||
base: &MathFragment,
|
||||
[tr, br]: [Option<&MathFragment>; 2],
|
||||
(tr_shift, br_shift): (Abs, Abs),
|
||||
space_after_post_script: Abs,
|
||||
) -> ((Abs, Abs), (Abs, Abs)) {
|
||||
let tr_values = tr.map_or_default(|tr| {
|
||||
let kern = math_kern(ctx, base, tr, tr_shift, Corner::TopRight);
|
||||
let kern = math_kern(base, tr, tr_shift, Corner::TopRight);
|
||||
(space_after_post_script + tr.width() + kern, kern)
|
||||
});
|
||||
|
||||
@ -302,7 +298,7 @@ fn compute_post_script_widths(
|
||||
// need to shift the post-subscript left by the base's italic correction
|
||||
// (see the kerning algorithm as described in the OpenType MATH spec).
|
||||
let br_values = br.map_or_default(|br| {
|
||||
let kern = math_kern(ctx, base, br, br_shift, Corner::BottomRight)
|
||||
let kern = math_kern(base, br, br_shift, Corner::BottomRight)
|
||||
- base.italics_correction();
|
||||
(space_after_post_script + br.width() + kern, kern)
|
||||
});
|
||||
@ -317,19 +313,18 @@ fn compute_post_script_widths(
|
||||
/// extends left of the base's width and the second being the distance the
|
||||
/// pre-subscript extends left of the base's width.
|
||||
fn compute_pre_script_widths(
|
||||
ctx: &MathContext,
|
||||
base: &MathFragment,
|
||||
[tl, bl]: [Option<&MathFragment>; 2],
|
||||
(tl_shift, bl_shift): (Abs, Abs),
|
||||
space_before_pre_script: Abs,
|
||||
) -> (Abs, Abs) {
|
||||
let tl_pre_width = tl.map_or_default(|tl| {
|
||||
let kern = math_kern(ctx, base, tl, tl_shift, Corner::TopLeft);
|
||||
let kern = math_kern(base, tl, tl_shift, Corner::TopLeft);
|
||||
space_before_pre_script + tl.width() + kern
|
||||
});
|
||||
|
||||
let bl_pre_width = bl.map_or_default(|bl| {
|
||||
let kern = math_kern(ctx, base, bl, bl_shift, Corner::BottomLeft);
|
||||
let kern = math_kern(base, bl, bl_shift, Corner::BottomLeft);
|
||||
space_before_pre_script + bl.width() + kern
|
||||
});
|
||||
|
||||
@ -471,13 +466,7 @@ fn compute_script_shifts(
|
||||
/// a negative value means shifting the script closer to the base. Requires the
|
||||
/// distance from the base's baseline to the script's baseline, as well as the
|
||||
/// script's corner (tl, tr, bl, br).
|
||||
fn math_kern(
|
||||
ctx: &MathContext,
|
||||
base: &MathFragment,
|
||||
script: &MathFragment,
|
||||
shift: Abs,
|
||||
pos: Corner,
|
||||
) -> Abs {
|
||||
fn math_kern(base: &MathFragment, script: &MathFragment, shift: Abs, pos: Corner) -> Abs {
|
||||
// This process is described under the MathKernInfo table in the OpenType
|
||||
// MATH spec.
|
||||
|
||||
@ -502,8 +491,8 @@ fn math_kern(
|
||||
|
||||
// Calculate the sum of kerning values for each correction height.
|
||||
let summed_kern = |height| {
|
||||
let base_kern = base.kern_at_height(ctx, pos, height);
|
||||
let attach_kern = script.kern_at_height(ctx, pos.inv(), height);
|
||||
let base_kern = base.kern_at_height(pos, height);
|
||||
let attach_kern = script.kern_at_height(pos.inv(), height);
|
||||
base_kern + attach_kern
|
||||
};
|
||||
|
||||
|
@ -109,14 +109,14 @@ fn layout_frac_like(
|
||||
frame.push_frame(denom_pos, denom);
|
||||
|
||||
if binom {
|
||||
let mut left = GlyphFragment::new(ctx, styles, '(', span)
|
||||
.stretch_vertical(ctx, height - short_fall);
|
||||
left.center_on_axis(ctx);
|
||||
let mut left = GlyphFragment::new_char(ctx.font, styles, '(', span)?;
|
||||
left.stretch_vertical(ctx, height - short_fall);
|
||||
left.center_on_axis();
|
||||
ctx.push(left);
|
||||
ctx.push(FrameFragment::new(styles, frame));
|
||||
let mut right = GlyphFragment::new(ctx, styles, ')', span)
|
||||
.stretch_vertical(ctx, height - short_fall);
|
||||
right.center_on_axis(ctx);
|
||||
let mut right = GlyphFragment::new_char(ctx.font, styles, ')', span)?;
|
||||
right.stretch_vertical(ctx, height - short_fall);
|
||||
right.center_on_axis();
|
||||
ctx.push(right);
|
||||
} else {
|
||||
frame.push(
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -45,20 +45,20 @@ pub fn layout_lr(
|
||||
|
||||
// Scale up fragments at both ends.
|
||||
match inner_fragments {
|
||||
[one] => scale(ctx, styles, one, relative_to, height, None),
|
||||
[one] => scale_if_delimiter(ctx, one, relative_to, height, None),
|
||||
[first, .., last] => {
|
||||
scale(ctx, styles, first, relative_to, height, Some(MathClass::Opening));
|
||||
scale(ctx, styles, last, relative_to, height, Some(MathClass::Closing));
|
||||
scale_if_delimiter(ctx, first, relative_to, height, Some(MathClass::Opening));
|
||||
scale_if_delimiter(ctx, last, relative_to, height, Some(MathClass::Closing));
|
||||
}
|
||||
_ => {}
|
||||
[] => {}
|
||||
}
|
||||
|
||||
// Handle MathFragment::Variant fragments that should be scaled up.
|
||||
// Handle MathFragment::Glyph fragments that should be scaled up.
|
||||
for fragment in inner_fragments.iter_mut() {
|
||||
if let MathFragment::Variant(ref mut variant) = fragment {
|
||||
if variant.mid_stretched == Some(false) {
|
||||
variant.mid_stretched = Some(true);
|
||||
scale(ctx, styles, fragment, relative_to, height, Some(MathClass::Large));
|
||||
if let MathFragment::Glyph(ref mut glyph) = fragment {
|
||||
if glyph.mid_stretched == Some(false) {
|
||||
glyph.mid_stretched = Some(true);
|
||||
scale(ctx, fragment, relative_to, height);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -95,18 +95,9 @@ pub fn layout_mid(
|
||||
let mut fragments = ctx.layout_into_fragments(&elem.body, styles)?;
|
||||
|
||||
for fragment in &mut fragments {
|
||||
match fragment {
|
||||
MathFragment::Glyph(glyph) => {
|
||||
let mut new = glyph.clone().into_variant();
|
||||
new.mid_stretched = Some(false);
|
||||
new.class = MathClass::Fence;
|
||||
*fragment = MathFragment::Variant(new);
|
||||
}
|
||||
MathFragment::Variant(variant) => {
|
||||
variant.mid_stretched = Some(false);
|
||||
variant.class = MathClass::Fence;
|
||||
}
|
||||
_ => {}
|
||||
if let MathFragment::Glyph(ref mut glyph) = fragment {
|
||||
glyph.mid_stretched = Some(false);
|
||||
glyph.class = MathClass::Relation;
|
||||
}
|
||||
}
|
||||
|
||||
@ -114,10 +105,13 @@ pub fn layout_mid(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Scale a math fragment to a height.
|
||||
fn scale(
|
||||
/// Scales a math fragment to a height if it has the class Opening, Closing, or
|
||||
/// Fence.
|
||||
///
|
||||
/// In case `apply` is `Some(class)`, `class` will be applied to the fragment if
|
||||
/// it is a delimiter, in a way that cannot be overridden by the user.
|
||||
fn scale_if_delimiter(
|
||||
ctx: &mut MathContext,
|
||||
styles: StyleChain,
|
||||
fragment: &mut MathFragment,
|
||||
relative_to: Abs,
|
||||
height: Rel<Abs>,
|
||||
@ -127,21 +121,23 @@ fn scale(
|
||||
fragment.class(),
|
||||
MathClass::Opening | MathClass::Closing | MathClass::Fence
|
||||
) {
|
||||
// This unwrap doesn't really matter. If it is None, then the fragment
|
||||
// won't be stretchable anyways.
|
||||
let short_fall = DELIM_SHORT_FALL.at(fragment.font_size().unwrap_or_default());
|
||||
stretch_fragment(
|
||||
ctx,
|
||||
styles,
|
||||
fragment,
|
||||
Some(Axis::Y),
|
||||
Some(relative_to),
|
||||
height,
|
||||
short_fall,
|
||||
);
|
||||
scale(ctx, fragment, relative_to, height);
|
||||
|
||||
if let Some(class) = apply {
|
||||
fragment.set_class(class);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Scales a math fragment to a height.
|
||||
fn scale(
|
||||
ctx: &mut MathContext,
|
||||
fragment: &mut MathFragment,
|
||||
relative_to: Abs,
|
||||
height: Rel<Abs>,
|
||||
) {
|
||||
// This unwrap doesn't really matter. If it is None, then the fragment
|
||||
// won't be stretchable anyways.
|
||||
let short_fall = DELIM_SHORT_FALL.at(fragment.font_size().unwrap_or_default());
|
||||
stretch_fragment(ctx, fragment, Some(Axis::Y), Some(relative_to), height, short_fall);
|
||||
}
|
||||
|
@ -9,8 +9,8 @@ use typst_library::visualize::{FillRule, FixedStroke, Geometry, LineCap, Shape};
|
||||
use typst_syntax::Span;
|
||||
|
||||
use super::{
|
||||
alignments, delimiter_alignment, style_for_denominator, AlignmentResult,
|
||||
FrameFragment, GlyphFragment, LeftRightAlternator, MathContext, DELIM_SHORT_FALL,
|
||||
alignments, style_for_denominator, AlignmentResult, FrameFragment, GlyphFragment,
|
||||
LeftRightAlternator, MathContext, DELIM_SHORT_FALL,
|
||||
};
|
||||
|
||||
const VERTICAL_PADDING: Ratio = Ratio::new(0.1);
|
||||
@ -183,8 +183,12 @@ fn layout_body(
|
||||
// We pad ascent and descent with the ascent and descent of the paren
|
||||
// to ensure that normal matrices are aligned with others unless they are
|
||||
// way too big.
|
||||
let paren =
|
||||
GlyphFragment::new(ctx, styles.chain(&denom_style), '(', Span::detached());
|
||||
let paren = GlyphFragment::new_char(
|
||||
ctx.font,
|
||||
styles.chain(&denom_style),
|
||||
'(',
|
||||
Span::detached(),
|
||||
)?;
|
||||
|
||||
for (column, col) in columns.iter().zip(&mut cols) {
|
||||
for (cell, (ascent, descent)) in column.iter().zip(&mut heights) {
|
||||
@ -202,8 +206,8 @@ fn layout_body(
|
||||
));
|
||||
}
|
||||
|
||||
ascent.set_max(cell.ascent().max(paren.ascent));
|
||||
descent.set_max(cell.descent().max(paren.descent));
|
||||
ascent.set_max(cell.ascent().max(paren.ascent()));
|
||||
descent.set_max(cell.descent().max(paren.descent()));
|
||||
|
||||
col.push(cell);
|
||||
}
|
||||
@ -312,19 +316,19 @@ fn layout_delimiters(
|
||||
let target = height + VERTICAL_PADDING.of(height);
|
||||
frame.set_baseline(height / 2.0 + axis);
|
||||
|
||||
if let Some(left) = left {
|
||||
let mut left = GlyphFragment::new(ctx, styles, left, span)
|
||||
.stretch_vertical(ctx, target - short_fall);
|
||||
left.align_on_axis(ctx, delimiter_alignment(left.c));
|
||||
if let Some(left_c) = left {
|
||||
let mut left = GlyphFragment::new_char(ctx.font, styles, left_c, span)?;
|
||||
left.stretch_vertical(ctx, target - short_fall);
|
||||
left.center_on_axis();
|
||||
ctx.push(left);
|
||||
}
|
||||
|
||||
ctx.push(FrameFragment::new(styles, frame));
|
||||
|
||||
if let Some(right) = right {
|
||||
let mut right = GlyphFragment::new(ctx, styles, right, span)
|
||||
.stretch_vertical(ctx, target - short_fall);
|
||||
right.align_on_axis(ctx, delimiter_alignment(right.c));
|
||||
if let Some(right_c) = right {
|
||||
let mut right = GlyphFragment::new_char(ctx.font, styles, right_c, span)?;
|
||||
right.stretch_vertical(ctx, target - short_fall);
|
||||
right.center_on_axis();
|
||||
ctx.push(right);
|
||||
}
|
||||
|
||||
|
@ -13,8 +13,6 @@ mod stretch;
|
||||
mod text;
|
||||
mod underover;
|
||||
|
||||
use rustybuzz::Feature;
|
||||
use ttf_parser::Tag;
|
||||
use typst_library::diag::{bail, SourceResult};
|
||||
use typst_library::engine::Engine;
|
||||
use typst_library::foundations::{
|
||||
@ -30,7 +28,7 @@ use typst_library::math::*;
|
||||
use typst_library::model::ParElem;
|
||||
use typst_library::routines::{Arenas, RealizationKind};
|
||||
use typst_library::text::{
|
||||
families, features, variant, Font, LinebreakElem, SpaceElem, TextEdgeBounds, TextElem,
|
||||
families, variant, Font, LinebreakElem, SpaceElem, TextEdgeBounds, TextElem,
|
||||
};
|
||||
use typst_library::World;
|
||||
use typst_syntax::Span;
|
||||
@ -38,11 +36,11 @@ use typst_utils::Numeric;
|
||||
use unicode_math_class::MathClass;
|
||||
|
||||
use self::fragment::{
|
||||
FrameFragment, GlyphFragment, GlyphwiseSubsts, Limits, MathFragment, VariantFragment,
|
||||
has_dtls_feat, stretch_axes, FrameFragment, GlyphFragment, Limits, MathFragment,
|
||||
};
|
||||
use self::run::{LeftRightAlternator, MathRun, MathRunFrameBuilder};
|
||||
use self::shared::*;
|
||||
use self::stretch::{stretch_fragment, stretch_glyph};
|
||||
use self::stretch::stretch_fragment;
|
||||
|
||||
/// Layout an inline equation (in a paragraph).
|
||||
#[typst_macros::time(span = elem.span())]
|
||||
@ -58,7 +56,7 @@ pub fn layout_equation_inline(
|
||||
let font = find_math_font(engine, styles, elem.span())?;
|
||||
|
||||
let mut locator = locator.split();
|
||||
let mut ctx = MathContext::new(engine, &mut locator, styles, region, &font);
|
||||
let mut ctx = MathContext::new(engine, &mut locator, region, &font);
|
||||
|
||||
let scale_style = style_for_script_scale(&ctx);
|
||||
let styles = styles.chain(&scale_style);
|
||||
@ -113,7 +111,7 @@ pub fn layout_equation_block(
|
||||
let font = find_math_font(engine, styles, span)?;
|
||||
|
||||
let mut locator = locator.split();
|
||||
let mut ctx = MathContext::new(engine, &mut locator, styles, regions.base(), &font);
|
||||
let mut ctx = MathContext::new(engine, &mut locator, regions.base(), &font);
|
||||
|
||||
let scale_style = style_for_script_scale(&ctx);
|
||||
let styles = styles.chain(&scale_style);
|
||||
@ -374,14 +372,7 @@ struct MathContext<'a, 'v, 'e> {
|
||||
region: Region,
|
||||
// Font-related.
|
||||
font: &'a Font,
|
||||
ttf: &'a ttf_parser::Face<'a>,
|
||||
table: ttf_parser::math::Table<'a>,
|
||||
constants: ttf_parser::math::Constants<'a>,
|
||||
dtls_table: Option<GlyphwiseSubsts<'a>>,
|
||||
flac_table: Option<GlyphwiseSubsts<'a>>,
|
||||
ssty_table: Option<GlyphwiseSubsts<'a>>,
|
||||
glyphwise_tables: Option<Vec<GlyphwiseSubsts<'a>>>,
|
||||
space_width: Em,
|
||||
// Mutable.
|
||||
fragments: Vec<MathFragment>,
|
||||
}
|
||||
@ -391,46 +382,20 @@ impl<'a, 'v, 'e> MathContext<'a, 'v, 'e> {
|
||||
fn new(
|
||||
engine: &'v mut Engine<'e>,
|
||||
locator: &'v mut SplitLocator<'a>,
|
||||
styles: StyleChain<'a>,
|
||||
base: Size,
|
||||
font: &'a Font,
|
||||
) -> Self {
|
||||
let math_table = font.ttf().tables().math.unwrap();
|
||||
let gsub_table = font.ttf().tables().gsub;
|
||||
let constants = math_table.constants.unwrap();
|
||||
|
||||
let feat = |tag: &[u8; 4]| {
|
||||
GlyphwiseSubsts::new(gsub_table, Feature::new(Tag::from_bytes(tag), 0, ..))
|
||||
};
|
||||
|
||||
let features = features(styles);
|
||||
let glyphwise_tables = Some(
|
||||
features
|
||||
.into_iter()
|
||||
.filter_map(|feature| GlyphwiseSubsts::new(gsub_table, feature))
|
||||
.collect(),
|
||||
);
|
||||
|
||||
let ttf = font.ttf();
|
||||
let space_width = ttf
|
||||
.glyph_index(' ')
|
||||
.and_then(|id| ttf.glyph_hor_advance(id))
|
||||
.map(|advance| font.to_em(advance))
|
||||
.unwrap_or(THICK);
|
||||
// These unwraps are safe as the font given is one returned by the
|
||||
// find_math_font function, which only returns fonts that have a math
|
||||
// constants table.
|
||||
let constants = font.ttf().tables().math.unwrap().constants.unwrap();
|
||||
|
||||
Self {
|
||||
engine,
|
||||
locator,
|
||||
region: Region::new(base, Axes::splat(false)),
|
||||
font,
|
||||
ttf,
|
||||
table: math_table,
|
||||
constants,
|
||||
dtls_table: feat(b"dtls"),
|
||||
flac_table: feat(b"flac"),
|
||||
ssty_table: feat(b"ssty"),
|
||||
glyphwise_tables,
|
||||
space_width,
|
||||
fragments: vec![],
|
||||
}
|
||||
}
|
||||
@ -529,7 +494,8 @@ fn layout_realized(
|
||||
if let Some(elem) = elem.to_packed::<TagElem>() {
|
||||
ctx.push(MathFragment::Tag(elem.tag.clone()));
|
||||
} else if elem.is::<SpaceElem>() {
|
||||
ctx.push(MathFragment::Space(ctx.space_width.resolve(styles)));
|
||||
let space_width = ctx.font.space_width().unwrap_or(THICK);
|
||||
ctx.push(MathFragment::Space(space_width.resolve(styles)));
|
||||
} else if elem.is::<LinebreakElem>() {
|
||||
ctx.push(MathFragment::Linebreak);
|
||||
} else if let Some(elem) = elem.to_packed::<HElem>() {
|
||||
|
@ -49,9 +49,9 @@ pub fn layout_root(
|
||||
|
||||
// Layout root symbol.
|
||||
let target = radicand.height() + thickness + gap;
|
||||
let sqrt = GlyphFragment::new(ctx, styles, '√', span)
|
||||
.stretch_vertical(ctx, target)
|
||||
.frame;
|
||||
let mut sqrt = GlyphFragment::new_char(ctx.font, styles, '√', span)?;
|
||||
sqrt.stretch_vertical(ctx, target);
|
||||
let sqrt = sqrt.into_frame();
|
||||
|
||||
// Layout the index.
|
||||
let sscript = EquationElem::set_size(MathSize::ScriptScript).wrap();
|
||||
|
@ -1,7 +1,9 @@
|
||||
use ttf_parser::math::MathValue;
|
||||
use ttf_parser::Tag;
|
||||
use typst_library::foundations::{Style, StyleChain};
|
||||
use typst_library::layout::{Abs, Em, FixedAlignment, Frame, Point, Size, VAlignment};
|
||||
use typst_library::layout::{Abs, Em, FixedAlignment, Frame, Point, Size};
|
||||
use typst_library::math::{EquationElem, MathSize};
|
||||
use typst_library::text::{FontFeatures, TextElem};
|
||||
use typst_utils::LazyHash;
|
||||
|
||||
use super::{LeftRightAlternator, MathContext, MathFragment, MathRun};
|
||||
@ -59,6 +61,16 @@ pub fn style_cramped() -> LazyHash<Style> {
|
||||
EquationElem::set_cramped(true).wrap()
|
||||
}
|
||||
|
||||
/// Sets flac OpenType feature.
|
||||
pub fn style_flac() -> LazyHash<Style> {
|
||||
TextElem::set_features(FontFeatures(vec![(Tag::from_bytes(b"flac"), 1)])).wrap()
|
||||
}
|
||||
|
||||
/// Sets dtls OpenType feature.
|
||||
pub fn style_dtls() -> LazyHash<Style> {
|
||||
TextElem::set_features(FontFeatures(vec![(Tag::from_bytes(b"dtls"), 1)])).wrap()
|
||||
}
|
||||
|
||||
/// The style for subscripts in the current style.
|
||||
pub fn style_for_subscript(styles: StyleChain) -> [LazyHash<Style>; 2] {
|
||||
[style_for_superscript(styles), EquationElem::set_cramped(true).wrap()]
|
||||
@ -97,15 +109,6 @@ pub fn style_for_script_scale(ctx: &MathContext) -> LazyHash<Style> {
|
||||
.wrap()
|
||||
}
|
||||
|
||||
/// How a delimieter should be aligned when scaling.
|
||||
pub fn delimiter_alignment(delimiter: char) -> VAlignment {
|
||||
match delimiter {
|
||||
'⌜' | '⌝' => VAlignment::Top,
|
||||
'⌞' | '⌟' => VAlignment::Bottom,
|
||||
_ => VAlignment::Horizon,
|
||||
}
|
||||
}
|
||||
|
||||
/// Stack rows on top of each other.
|
||||
///
|
||||
/// Add a `gap` between each row and uses the baseline of the `baseline`-th
|
||||
|
@ -1,19 +1,10 @@
|
||||
use ttf_parser::math::{GlyphAssembly, GlyphConstruction, GlyphPart};
|
||||
use ttf_parser::LazyArray16;
|
||||
use typst_library::diag::{warning, SourceResult};
|
||||
use typst_library::foundations::{Packed, StyleChain};
|
||||
use typst_library::layout::{Abs, Axis, Frame, Point, Rel, Size};
|
||||
use typst_library::layout::{Abs, Axis, Rel};
|
||||
use typst_library::math::StretchElem;
|
||||
use typst_utils::Get;
|
||||
|
||||
use super::{
|
||||
delimiter_alignment, GlyphFragment, MathContext, MathFragment, Scaled,
|
||||
VariantFragment,
|
||||
};
|
||||
use crate::modifiers::FrameModify;
|
||||
|
||||
/// Maximum number of times extenders can be repeated.
|
||||
const MAX_REPEATS: usize = 1024;
|
||||
use super::{stretch_axes, MathContext, MathFragment};
|
||||
|
||||
/// Lays out a [`StretchElem`].
|
||||
#[typst_macros::time(name = "math.stretch", span = elem.span())]
|
||||
@ -23,15 +14,7 @@ pub fn layout_stretch(
|
||||
styles: StyleChain,
|
||||
) -> SourceResult<()> {
|
||||
let mut fragment = ctx.layout_into_fragment(&elem.body, styles)?;
|
||||
stretch_fragment(
|
||||
ctx,
|
||||
styles,
|
||||
&mut fragment,
|
||||
None,
|
||||
None,
|
||||
elem.size(styles),
|
||||
Abs::zero(),
|
||||
);
|
||||
stretch_fragment(ctx, &mut fragment, None, None, elem.size(styles), Abs::zero());
|
||||
ctx.push(fragment);
|
||||
Ok(())
|
||||
}
|
||||
@ -39,266 +22,49 @@ pub fn layout_stretch(
|
||||
/// Attempts to stretch the given fragment by/to the amount given in stretch.
|
||||
pub fn stretch_fragment(
|
||||
ctx: &mut MathContext,
|
||||
styles: StyleChain,
|
||||
fragment: &mut MathFragment,
|
||||
axis: Option<Axis>,
|
||||
relative_to: Option<Abs>,
|
||||
stretch: Rel<Abs>,
|
||||
short_fall: Abs,
|
||||
) {
|
||||
let glyph = match fragment {
|
||||
MathFragment::Glyph(glyph) => glyph.clone(),
|
||||
MathFragment::Variant(variant) => {
|
||||
GlyphFragment::new(ctx, styles, variant.c, variant.span)
|
||||
}
|
||||
_ => return,
|
||||
};
|
||||
let size = fragment.size();
|
||||
|
||||
let MathFragment::Glyph(ref mut glyph) = fragment else { return };
|
||||
|
||||
// Return if we attempt to stretch along an axis which isn't stretchable,
|
||||
// so that the original fragment isn't modified.
|
||||
let Some(stretch_axis) = stretch_axis(ctx, &glyph) else { return };
|
||||
let axis = axis.unwrap_or(stretch_axis);
|
||||
if axis != stretch_axis {
|
||||
return;
|
||||
}
|
||||
|
||||
let relative_to_size = relative_to.unwrap_or_else(|| fragment.size().get(axis));
|
||||
|
||||
let mut variant = stretch_glyph(
|
||||
ctx,
|
||||
glyph,
|
||||
stretch.relative_to(relative_to_size) - short_fall,
|
||||
axis,
|
||||
);
|
||||
|
||||
if axis == Axis::Y {
|
||||
variant.align_on_axis(ctx, delimiter_alignment(variant.c));
|
||||
}
|
||||
|
||||
*fragment = MathFragment::Variant(variant);
|
||||
}
|
||||
|
||||
/// Return whether the glyph is stretchable and if it is, along which axis it
|
||||
/// can be stretched.
|
||||
fn stretch_axis(ctx: &mut MathContext, base: &GlyphFragment) -> Option<Axis> {
|
||||
let base_id = base.id;
|
||||
let vertical = ctx
|
||||
.table
|
||||
.variants
|
||||
.and_then(|variants| variants.vertical_constructions.get(base_id))
|
||||
.map(|_| Axis::Y);
|
||||
let horizontal = ctx
|
||||
.table
|
||||
.variants
|
||||
.and_then(|variants| variants.horizontal_constructions.get(base_id))
|
||||
.map(|_| Axis::X);
|
||||
|
||||
match (vertical, horizontal) {
|
||||
(vertical, None) => vertical,
|
||||
(None, horizontal) => horizontal,
|
||||
_ => {
|
||||
// As far as we know, there aren't any glyphs that have both
|
||||
// vertical and horizontal constructions. So for the time being, we
|
||||
// will assume that a glyph cannot have both.
|
||||
ctx.engine.sink.warn(warning!(
|
||||
base.span,
|
||||
"glyph has both vertical and horizontal constructions";
|
||||
hint: "this is probably a font bug";
|
||||
hint: "please file an issue at https://github.com/typst/typst/issues"
|
||||
));
|
||||
|
||||
None
|
||||
let axes = stretch_axes(&glyph.item.font, glyph.base_glyph.id);
|
||||
let stretch_axis = if let Some(axis) = axis {
|
||||
if !axes.get(axis) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to stretch a glyph to a desired width or height.
|
||||
///
|
||||
/// The resulting frame may not have the exact desired width.
|
||||
pub fn stretch_glyph(
|
||||
ctx: &mut MathContext,
|
||||
mut base: GlyphFragment,
|
||||
target: Abs,
|
||||
axis: Axis,
|
||||
) -> VariantFragment {
|
||||
// If the base glyph is good enough, use it.
|
||||
let advance = match axis {
|
||||
Axis::X => base.width,
|
||||
Axis::Y => base.height(),
|
||||
};
|
||||
if target <= advance {
|
||||
return base.into_variant();
|
||||
}
|
||||
|
||||
let mut min_overlap = Abs::zero();
|
||||
let construction = ctx
|
||||
.table
|
||||
.variants
|
||||
.and_then(|variants| {
|
||||
min_overlap = variants.min_connector_overlap.scaled(ctx, base.font_size);
|
||||
match axis {
|
||||
Axis::X => variants.horizontal_constructions,
|
||||
Axis::Y => variants.vertical_constructions,
|
||||
axis
|
||||
} else {
|
||||
match (axes.x, axes.y) {
|
||||
(true, false) => Axis::X,
|
||||
(false, true) => Axis::Y,
|
||||
(false, false) => return,
|
||||
(true, true) => {
|
||||
// As far as we know, there aren't any glyphs that have both
|
||||
// vertical and horizontal constructions. So for the time being, we
|
||||
// will assume that a glyph cannot have both.
|
||||
ctx.engine.sink.warn(warning!(
|
||||
glyph.item.glyphs[0].span.0,
|
||||
"glyph has both vertical and horizontal constructions";
|
||||
hint: "this is probably a font bug";
|
||||
hint: "please file an issue at https://github.com/typst/typst/issues"
|
||||
));
|
||||
return;
|
||||
}
|
||||
.get(base.id)
|
||||
})
|
||||
.unwrap_or(GlyphConstruction { assembly: None, variants: LazyArray16::new(&[]) });
|
||||
|
||||
// Search for a pre-made variant with a good advance.
|
||||
let mut best_id = base.id;
|
||||
let mut best_advance = base.width;
|
||||
for variant in construction.variants {
|
||||
best_id = variant.variant_glyph;
|
||||
best_advance = base.font.to_em(variant.advance_measurement).at(base.font_size);
|
||||
if target <= best_advance {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// This is either good or the best we've got.
|
||||
if target <= best_advance || construction.assembly.is_none() {
|
||||
base.set_id(ctx, best_id);
|
||||
return base.into_variant();
|
||||
}
|
||||
|
||||
// Assemble from parts.
|
||||
let assembly = construction.assembly.unwrap();
|
||||
assemble(ctx, base, assembly, min_overlap, target, axis)
|
||||
}
|
||||
|
||||
/// Assemble a glyph from parts.
|
||||
fn assemble(
|
||||
ctx: &mut MathContext,
|
||||
base: GlyphFragment,
|
||||
assembly: GlyphAssembly,
|
||||
min_overlap: Abs,
|
||||
target: Abs,
|
||||
axis: Axis,
|
||||
) -> VariantFragment {
|
||||
// Determine the number of times the extenders need to be repeated as well
|
||||
// as a ratio specifying how much to spread the parts apart
|
||||
// (0 = maximal overlap, 1 = minimal overlap).
|
||||
let mut full;
|
||||
let mut ratio;
|
||||
let mut repeat = 0;
|
||||
loop {
|
||||
full = Abs::zero();
|
||||
ratio = 0.0;
|
||||
|
||||
let mut parts = parts(assembly, repeat).peekable();
|
||||
let mut growable = Abs::zero();
|
||||
|
||||
while let Some(part) = parts.next() {
|
||||
let mut advance = part.full_advance.scaled(ctx, base.font_size);
|
||||
if let Some(next) = parts.peek() {
|
||||
let max_overlap = part
|
||||
.end_connector_length
|
||||
.min(next.start_connector_length)
|
||||
.scaled(ctx, base.font_size);
|
||||
if max_overlap < min_overlap {
|
||||
// This condition happening is indicative of a bug in the
|
||||
// font.
|
||||
ctx.engine.sink.warn(warning!(
|
||||
base.span,
|
||||
"glyph has assembly parts with overlap less than minConnectorOverlap";
|
||||
hint: "its rendering may appear broken - this is probably a font bug";
|
||||
hint: "please file an issue at https://github.com/typst/typst/issues"
|
||||
));
|
||||
}
|
||||
|
||||
advance -= max_overlap;
|
||||
growable += max_overlap - min_overlap;
|
||||
}
|
||||
|
||||
full += advance;
|
||||
}
|
||||
|
||||
if full < target {
|
||||
let delta = target - full;
|
||||
ratio = (delta / growable).min(1.0);
|
||||
full += ratio * growable;
|
||||
}
|
||||
|
||||
if target <= full || repeat >= MAX_REPEATS {
|
||||
break;
|
||||
}
|
||||
|
||||
repeat += 1;
|
||||
}
|
||||
|
||||
let mut selected = vec![];
|
||||
let mut parts = parts(assembly, repeat).peekable();
|
||||
while let Some(part) = parts.next() {
|
||||
let mut advance = part.full_advance.scaled(ctx, base.font_size);
|
||||
if let Some(next) = parts.peek() {
|
||||
let max_overlap = part
|
||||
.end_connector_length
|
||||
.min(next.start_connector_length)
|
||||
.scaled(ctx, base.font_size);
|
||||
advance -= max_overlap;
|
||||
advance += ratio * (max_overlap - min_overlap);
|
||||
}
|
||||
|
||||
let mut fragment = base.clone();
|
||||
fragment.set_id(ctx, part.glyph_id);
|
||||
selected.push((fragment, advance));
|
||||
}
|
||||
|
||||
let size;
|
||||
let baseline;
|
||||
match axis {
|
||||
Axis::X => {
|
||||
let height = base.ascent + base.descent;
|
||||
size = Size::new(full, height);
|
||||
baseline = base.ascent;
|
||||
}
|
||||
Axis::Y => {
|
||||
let axis = ctx.constants.axis_height().scaled(ctx, base.font_size);
|
||||
let width = selected.iter().map(|(f, _)| f.width).max().unwrap_or_default();
|
||||
size = Size::new(width, full);
|
||||
baseline = full / 2.0 + axis;
|
||||
}
|
||||
}
|
||||
|
||||
let mut frame = Frame::soft(size);
|
||||
let mut offset = Abs::zero();
|
||||
frame.set_baseline(baseline);
|
||||
frame.modify(&base.modifiers);
|
||||
|
||||
for (fragment, advance) in selected {
|
||||
let pos = match axis {
|
||||
Axis::X => Point::new(offset, frame.baseline() - fragment.ascent),
|
||||
Axis::Y => Point::with_y(full - offset - fragment.height()),
|
||||
};
|
||||
frame.push_frame(pos, fragment.into_frame());
|
||||
offset += advance;
|
||||
}
|
||||
|
||||
let accent_attach = match axis {
|
||||
Axis::X => (frame.width() / 2.0, frame.width() / 2.0),
|
||||
Axis::Y => base.accent_attach,
|
||||
};
|
||||
|
||||
VariantFragment {
|
||||
c: base.c,
|
||||
frame,
|
||||
font_size: base.font_size,
|
||||
italics_correction: Abs::zero(),
|
||||
accent_attach,
|
||||
class: base.class,
|
||||
math_size: base.math_size,
|
||||
span: base.span,
|
||||
limits: base.limits,
|
||||
mid_stretched: None,
|
||||
extended_shape: true,
|
||||
let relative_to_size = relative_to.unwrap_or_else(|| size.get(stretch_axis));
|
||||
|
||||
glyph.stretch(ctx, stretch.relative_to(relative_to_size) - short_fall, stretch_axis);
|
||||
|
||||
if stretch_axis == Axis::Y {
|
||||
glyph.center_on_axis();
|
||||
}
|
||||
}
|
||||
|
||||
/// Return an iterator over the assembly's parts with extenders repeated the
|
||||
/// specified number of times.
|
||||
fn parts(assembly: GlyphAssembly, repeat: usize) -> impl Iterator<Item = GlyphPart> + '_ {
|
||||
assembly.parts.into_iter().flat_map(move |part| {
|
||||
let count = if part.part_flags.extender() { repeat } else { 1 };
|
||||
std::iter::repeat_n(part, count)
|
||||
})
|
||||
}
|
||||
|
@ -12,7 +12,10 @@ use typst_syntax::{is_newline, Span};
|
||||
use unicode_math_class::MathClass;
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
use super::{FrameFragment, GlyphFragment, MathContext, MathFragment, MathRun};
|
||||
use super::{
|
||||
has_dtls_feat, style_dtls, FrameFragment, GlyphFragment, MathContext, MathFragment,
|
||||
MathRun,
|
||||
};
|
||||
|
||||
/// Lays out a [`TextElem`].
|
||||
pub fn layout_text(
|
||||
@ -67,12 +70,7 @@ fn layout_inline_text(
|
||||
let mut fragments = vec![];
|
||||
for unstyled_c in text.chars() {
|
||||
let c = styled_char(styles, unstyled_c, false);
|
||||
let mut glyph = GlyphFragment::new(ctx, styles, c, span);
|
||||
match EquationElem::size_in(styles) {
|
||||
MathSize::Script => glyph.make_script_size(ctx),
|
||||
MathSize::ScriptScript => glyph.make_script_script_size(ctx),
|
||||
_ => {}
|
||||
}
|
||||
let glyph = GlyphFragment::new_char(ctx.font, styles, c, span)?;
|
||||
fragments.push(glyph.into());
|
||||
}
|
||||
let frame = MathRun::new(fragments).into_frame(styles);
|
||||
@ -121,54 +119,45 @@ pub fn layout_symbol(
|
||||
) -> SourceResult<()> {
|
||||
// Switch dotless char to normal when we have the dtls OpenType feature.
|
||||
// This should happen before the main styling pass.
|
||||
let (unstyled_c, dtls) = match try_dotless(elem.text) {
|
||||
Some(c) if ctx.dtls_table.is_some() => (c, true),
|
||||
_ => (elem.text, false),
|
||||
let dtls = style_dtls();
|
||||
let (unstyled_c, symbol_styles) = match try_dotless(elem.text) {
|
||||
Some(c) if has_dtls_feat(ctx.font) => (c, styles.chain(&dtls)),
|
||||
_ => (elem.text, styles),
|
||||
};
|
||||
let c = styled_char(styles, unstyled_c, true);
|
||||
let fragment = match GlyphFragment::try_new(ctx, styles, c, elem.span()) {
|
||||
Some(glyph) => layout_glyph(glyph, dtls, ctx, styles),
|
||||
None => {
|
||||
// Not in the math font, fallback to normal inline text layout.
|
||||
layout_inline_text(c.encode_utf8(&mut [0; 4]), elem.span(), ctx, styles)?
|
||||
.into()
|
||||
}
|
||||
};
|
||||
let fragment: MathFragment =
|
||||
match GlyphFragment::new_char(ctx.font, symbol_styles, c, elem.span()) {
|
||||
Ok(mut glyph) => {
|
||||
adjust_glyph_layout(&mut glyph, ctx, styles);
|
||||
glyph.into()
|
||||
}
|
||||
Err(_) => {
|
||||
// Not in the math font, fallback to normal inline text layout.
|
||||
// TODO: Should replace this with proper fallback in [`GlyphFragment::new`].
|
||||
layout_inline_text(c.encode_utf8(&mut [0; 4]), elem.span(), ctx, styles)?
|
||||
.into()
|
||||
}
|
||||
};
|
||||
ctx.push(fragment);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Layout a [`GlyphFragment`].
|
||||
fn layout_glyph(
|
||||
mut glyph: GlyphFragment,
|
||||
dtls: bool,
|
||||
/// Centers large glyphs vertically on the axis, scaling them if in display
|
||||
/// style.
|
||||
fn adjust_glyph_layout(
|
||||
glyph: &mut GlyphFragment,
|
||||
ctx: &mut MathContext,
|
||||
styles: StyleChain,
|
||||
) -> MathFragment {
|
||||
if dtls {
|
||||
glyph.make_dotless_form(ctx);
|
||||
}
|
||||
let math_size = EquationElem::size_in(styles);
|
||||
match math_size {
|
||||
MathSize::Script => glyph.make_script_size(ctx),
|
||||
MathSize::ScriptScript => glyph.make_script_script_size(ctx),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
) {
|
||||
if glyph.class == MathClass::Large {
|
||||
let mut variant = if math_size == MathSize::Display {
|
||||
if EquationElem::size_in(styles) == MathSize::Display {
|
||||
let height = scaled!(ctx, styles, display_operator_min_height)
|
||||
.max(SQRT_2 * glyph.height());
|
||||
glyph.stretch_vertical(ctx, height)
|
||||
} else {
|
||||
glyph.into_variant()
|
||||
.max(SQRT_2 * glyph.size.y);
|
||||
glyph.stretch_vertical(ctx, height);
|
||||
};
|
||||
// TeXbook p 155. Large operators are always vertically centered on the
|
||||
// axis.
|
||||
variant.center_on_axis(ctx);
|
||||
variant.into()
|
||||
} else {
|
||||
glyph.into()
|
||||
glyph.center_on_axis();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -285,14 +285,14 @@ fn layout_underoverspreader(
|
||||
let body = ctx.layout_into_run(body, styles)?;
|
||||
let body_class = body.class();
|
||||
let body = body.into_fragment(styles);
|
||||
let glyph = GlyphFragment::new(ctx, styles, c, span);
|
||||
let stretched = glyph.stretch_horizontal(ctx, body.width());
|
||||
let mut glyph = GlyphFragment::new_char(ctx.font, styles, c, span)?;
|
||||
glyph.stretch_horizontal(ctx, body.width());
|
||||
|
||||
let mut rows = vec![];
|
||||
let baseline = match position {
|
||||
Position::Under => {
|
||||
rows.push(MathRun::new(vec![body]));
|
||||
rows.push(stretched.into());
|
||||
rows.push(glyph.into());
|
||||
if let Some(annotation) = annotation {
|
||||
let under_style = style_for_subscript(styles);
|
||||
let annotation_styles = styles.chain(&under_style);
|
||||
@ -306,7 +306,7 @@ fn layout_underoverspreader(
|
||||
let annotation_styles = styles.chain(&over_style);
|
||||
rows.extend(ctx.layout_into_run(annotation, annotation_styles)?.rows());
|
||||
}
|
||||
rows.push(stretched.into());
|
||||
rows.push(glyph.into());
|
||||
rows.push(MathRun::new(vec![body]));
|
||||
rows.len() - 1
|
||||
}
|
||||
|
@ -11,8 +11,8 @@ use typst_library::layout::{
|
||||
};
|
||||
use typst_library::visualize::{
|
||||
CircleElem, CloseMode, Curve, CurveComponent, CurveElem, EllipseElem, FillRule,
|
||||
FixedStroke, Geometry, LineElem, Paint, PathElem, PathVertex, PolygonElem, RectElem,
|
||||
Shape, SquareElem, Stroke,
|
||||
FixedStroke, Geometry, LineCap, LineElem, Paint, PathElem, PathVertex, PolygonElem,
|
||||
RectElem, Shape, SquareElem, Stroke,
|
||||
};
|
||||
use typst_syntax::Span;
|
||||
use typst_utils::{Get, Numeric};
|
||||
@ -889,7 +889,13 @@ fn segmented_rect(
|
||||
let end = current;
|
||||
last = current;
|
||||
let Some(stroke) = strokes.get_ref(start.side_cw()) else { continue };
|
||||
let (shape, ontop) = segment(start, end, &corners, stroke);
|
||||
let start_cap = stroke.cap;
|
||||
let end_cap = match strokes.get_ref(end.side_ccw()) {
|
||||
Some(stroke) => stroke.cap,
|
||||
None => start_cap,
|
||||
};
|
||||
let (shape, ontop) =
|
||||
segment(start, end, start_cap, end_cap, &corners, stroke);
|
||||
if ontop {
|
||||
res.push(shape);
|
||||
} else {
|
||||
@ -899,7 +905,14 @@ fn segmented_rect(
|
||||
}
|
||||
} else if let Some(stroke) = &strokes.top {
|
||||
// single segment
|
||||
let (shape, _) = segment(Corner::TopLeft, Corner::TopLeft, &corners, stroke);
|
||||
let (shape, _) = segment(
|
||||
Corner::TopLeft,
|
||||
Corner::TopLeft,
|
||||
stroke.cap,
|
||||
stroke.cap,
|
||||
&corners,
|
||||
stroke,
|
||||
);
|
||||
res.push(shape);
|
||||
}
|
||||
res
|
||||
@ -946,6 +959,8 @@ fn curve_segment(
|
||||
fn segment(
|
||||
start: Corner,
|
||||
end: Corner,
|
||||
start_cap: LineCap,
|
||||
end_cap: LineCap,
|
||||
corners: &Corners<ControlPoints>,
|
||||
stroke: &FixedStroke,
|
||||
) -> (Shape, bool) {
|
||||
@ -979,7 +994,7 @@ fn segment(
|
||||
|
||||
let use_fill = solid && fill_corners(start, end, corners);
|
||||
let shape = if use_fill {
|
||||
fill_segment(start, end, corners, stroke)
|
||||
fill_segment(start, end, start_cap, end_cap, corners, stroke)
|
||||
} else {
|
||||
stroke_segment(start, end, corners, stroke.clone())
|
||||
};
|
||||
@ -1010,6 +1025,8 @@ fn stroke_segment(
|
||||
fn fill_segment(
|
||||
start: Corner,
|
||||
end: Corner,
|
||||
start_cap: LineCap,
|
||||
end_cap: LineCap,
|
||||
corners: &Corners<ControlPoints>,
|
||||
stroke: &FixedStroke,
|
||||
) -> Shape {
|
||||
@ -1035,8 +1052,7 @@ fn fill_segment(
|
||||
if c.arc_outer() {
|
||||
curve.arc_line(c.mid_outer(), c.center_outer(), c.end_outer());
|
||||
} else {
|
||||
curve.line(c.outer());
|
||||
curve.line(c.end_outer());
|
||||
c.start_cap(&mut curve, start_cap);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1079,7 +1095,7 @@ fn fill_segment(
|
||||
if c.arc_inner() {
|
||||
curve.arc_line(c.mid_inner(), c.center_inner(), c.start_inner());
|
||||
} else {
|
||||
curve.line(c.center_inner());
|
||||
c.end_cap(&mut curve, end_cap);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1134,6 +1150,16 @@ struct ControlPoints {
|
||||
}
|
||||
|
||||
impl ControlPoints {
|
||||
/// Rotate point around the origin, relative to the top-left.
|
||||
fn rotate_centered(&self, point: Point) -> Point {
|
||||
match self.corner {
|
||||
Corner::TopLeft => point,
|
||||
Corner::TopRight => Point { x: -point.y, y: point.x },
|
||||
Corner::BottomRight => Point { x: -point.x, y: -point.y },
|
||||
Corner::BottomLeft => Point { x: point.y, y: -point.x },
|
||||
}
|
||||
}
|
||||
|
||||
/// Move and rotate the point from top-left to the required corner.
|
||||
fn rotate(&self, point: Point) -> Point {
|
||||
match self.corner {
|
||||
@ -1280,6 +1306,77 @@ impl ControlPoints {
|
||||
y: self.stroke_after,
|
||||
})
|
||||
}
|
||||
|
||||
/// Draw the cap at the beginning of the segment.
|
||||
///
|
||||
/// If this corner has a stroke before it,
|
||||
/// a default "butt" cap is used.
|
||||
///
|
||||
/// NOTE: doesn't support the case where the corner has a radius.
|
||||
pub fn start_cap(&self, curve: &mut Curve, cap_type: LineCap) {
|
||||
if self.stroke_before != Abs::zero()
|
||||
|| self.radius != Abs::zero()
|
||||
|| cap_type == LineCap::Butt
|
||||
{
|
||||
// Just the default cap.
|
||||
curve.line(self.outer());
|
||||
} else if cap_type == LineCap::Square {
|
||||
// Extend by the stroke width.
|
||||
let offset =
|
||||
self.rotate_centered(Point { x: -self.stroke_after, y: Abs::zero() });
|
||||
curve.line(self.end_inner() + offset);
|
||||
curve.line(self.outer() + offset);
|
||||
} else if cap_type == LineCap::Round {
|
||||
// We push the center by a little bit to ensure the correct
|
||||
// half of the circle gets drawn. If it is perfectly centered
|
||||
// the `arc` function just degenerates into a line, which we
|
||||
// do not want in this case.
|
||||
curve.arc(
|
||||
self.end_inner(),
|
||||
(self.end_inner()
|
||||
+ self.rotate_centered(Point { x: Abs::raw(1.0), y: Abs::zero() })
|
||||
+ self.outer())
|
||||
/ 2.,
|
||||
self.outer(),
|
||||
);
|
||||
}
|
||||
curve.line(self.end_outer());
|
||||
}
|
||||
|
||||
/// Draw the cap at the end of the segment.
|
||||
///
|
||||
/// If this corner has a stroke before it,
|
||||
/// a default "butt" cap is used.
|
||||
///
|
||||
/// NOTE: doesn't support the case where the corner has a radius.
|
||||
pub fn end_cap(&self, curve: &mut Curve, cap_type: LineCap) {
|
||||
if self.stroke_after != Abs::zero()
|
||||
|| self.radius != Abs::zero()
|
||||
|| cap_type == LineCap::Butt
|
||||
{
|
||||
// Just the default cap.
|
||||
curve.line(self.center_inner());
|
||||
} else if cap_type == LineCap::Square {
|
||||
// Extend by the stroke width.
|
||||
let offset =
|
||||
self.rotate_centered(Point { x: Abs::zero(), y: -self.stroke_before });
|
||||
curve.line(self.outer() + offset);
|
||||
curve.line(self.center_inner() + offset);
|
||||
} else if cap_type == LineCap::Round {
|
||||
// We push the center by a little bit to ensure the correct
|
||||
// half of the circle gets drawn. If it is perfectly centered
|
||||
// the `arc` function just degenerates into a line, which we
|
||||
// do not want in this case.
|
||||
curve.arc(
|
||||
self.outer(),
|
||||
(self.outer()
|
||||
+ self.rotate_centered(Point { x: Abs::zero(), y: Abs::raw(1.0) })
|
||||
+ self.center_inner())
|
||||
/ 2.,
|
||||
self.center_inner(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper to draw arcs with Bézier curves.
|
||||
|
@ -66,6 +66,7 @@ unicode-normalization = { workspace = true }
|
||||
unicode-segmentation = { workspace = true }
|
||||
unscanny = { workspace = true }
|
||||
usvg = { workspace = true }
|
||||
utf8_iter = { workspace = true }
|
||||
wasmi = { workspace = true }
|
||||
xmlwriter = { workspace = true }
|
||||
|
||||
|
@ -1,17 +1,20 @@
|
||||
//! Diagnostics.
|
||||
|
||||
use std::fmt::{self, Display, Formatter};
|
||||
use std::fmt::{self, Display, Formatter, Write as _};
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::str::Utf8Error;
|
||||
use std::string::FromUtf8Error;
|
||||
|
||||
use az::SaturatingAs;
|
||||
use comemo::Tracked;
|
||||
use ecow::{eco_vec, EcoVec};
|
||||
use typst_syntax::package::{PackageSpec, PackageVersion};
|
||||
use typst_syntax::{Span, Spanned, SyntaxError};
|
||||
use typst_syntax::{Lines, Span, Spanned, SyntaxError};
|
||||
use utf8_iter::ErrorReportingUtf8Chars;
|
||||
|
||||
use crate::engine::Engine;
|
||||
use crate::loading::{LoadSource, Loaded};
|
||||
use crate::{World, WorldExt};
|
||||
|
||||
/// Early-return with a [`StrResult`] or [`SourceResult`].
|
||||
@ -155,7 +158,7 @@ impl<T> Warned<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// An error or warning in a source file.
|
||||
/// An error or warning in a source or text file.
|
||||
///
|
||||
/// The contained spans will only be detached if any of the input source files
|
||||
/// were detached.
|
||||
@ -575,31 +578,287 @@ impl From<PackageError> for EcoString {
|
||||
}
|
||||
}
|
||||
|
||||
/// A result type with a data-loading-related error.
|
||||
pub type LoadResult<T> = Result<T, LoadError>;
|
||||
|
||||
/// A call site independent error that occurred during data loading. This avoids
|
||||
/// polluting the memoization with [`Span`]s and [`FileId`]s from source files.
|
||||
/// Can be turned into a [`SourceDiagnostic`] using the [`LoadedWithin::within`]
|
||||
/// method available on [`LoadResult`].
|
||||
///
|
||||
/// [`FileId`]: typst_syntax::FileId
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct LoadError {
|
||||
/// The position in the file at which the error occured.
|
||||
pos: ReportPos,
|
||||
/// Must contain a message formatted like this: `"failed to do thing (cause)"`.
|
||||
message: EcoString,
|
||||
}
|
||||
|
||||
impl LoadError {
|
||||
/// Creates a new error from a position in a file, a base message
|
||||
/// (e.g. `failed to parse JSON`) and a concrete error (e.g. `invalid
|
||||
/// number`)
|
||||
pub fn new(
|
||||
pos: impl Into<ReportPos>,
|
||||
message: impl std::fmt::Display,
|
||||
error: impl std::fmt::Display,
|
||||
) -> Self {
|
||||
Self {
|
||||
pos: pos.into(),
|
||||
message: eco_format!("{message} ({error})"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Utf8Error> for LoadError {
|
||||
fn from(err: Utf8Error) -> Self {
|
||||
let start = err.valid_up_to();
|
||||
let end = start + err.error_len().unwrap_or(0);
|
||||
LoadError::new(
|
||||
start..end,
|
||||
"failed to convert to string",
|
||||
"file is not valid utf-8",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a [`LoadResult`] to a [`SourceResult`] by adding the [`Loaded`]
|
||||
/// context.
|
||||
pub trait LoadedWithin<T> {
|
||||
/// Report an error, possibly in an external file.
|
||||
fn within(self, loaded: &Loaded) -> SourceResult<T>;
|
||||
}
|
||||
|
||||
impl<T, E> LoadedWithin<T> for Result<T, E>
|
||||
where
|
||||
E: Into<LoadError>,
|
||||
{
|
||||
fn within(self, loaded: &Loaded) -> SourceResult<T> {
|
||||
self.map_err(|err| {
|
||||
let LoadError { pos, message } = err.into();
|
||||
load_err_in_text(loaded, pos, message)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Report an error, possibly in an external file. This will delegate to
|
||||
/// [`load_err_in_invalid_text`] if the data isn't valid utf-8.
|
||||
fn load_err_in_text(
|
||||
loaded: &Loaded,
|
||||
pos: impl Into<ReportPos>,
|
||||
mut message: EcoString,
|
||||
) -> EcoVec<SourceDiagnostic> {
|
||||
let pos = pos.into();
|
||||
// This also does utf-8 validation. Only report an error in an external
|
||||
// file if it is human readable (valid utf-8), otherwise fall back to
|
||||
// `load_err_in_invalid_text`.
|
||||
let lines = Lines::try_from(&loaded.data);
|
||||
match (loaded.source.v, lines) {
|
||||
(LoadSource::Path(file_id), Ok(lines)) => {
|
||||
if let Some(range) = pos.range(&lines) {
|
||||
let span = Span::from_range(file_id, range);
|
||||
return eco_vec![SourceDiagnostic::error(span, message)];
|
||||
}
|
||||
|
||||
// Either `ReportPos::None` was provided, or resolving the range
|
||||
// from the line/column failed. If present report the possibly
|
||||
// wrong line/column in the error message anyway.
|
||||
let span = Span::from_range(file_id, 0..loaded.data.len());
|
||||
if let Some(pair) = pos.line_col(&lines) {
|
||||
message.pop();
|
||||
let (line, col) = pair.numbers();
|
||||
write!(&mut message, " at {line}:{col})").ok();
|
||||
}
|
||||
eco_vec![SourceDiagnostic::error(span, message)]
|
||||
}
|
||||
(LoadSource::Bytes, Ok(lines)) => {
|
||||
if let Some(pair) = pos.line_col(&lines) {
|
||||
message.pop();
|
||||
let (line, col) = pair.numbers();
|
||||
write!(&mut message, " at {line}:{col})").ok();
|
||||
}
|
||||
eco_vec![SourceDiagnostic::error(loaded.source.span, message)]
|
||||
}
|
||||
_ => load_err_in_invalid_text(loaded, pos, message),
|
||||
}
|
||||
}
|
||||
|
||||
/// Report an error (possibly from an external file) that isn't valid utf-8.
|
||||
fn load_err_in_invalid_text(
|
||||
loaded: &Loaded,
|
||||
pos: impl Into<ReportPos>,
|
||||
mut message: EcoString,
|
||||
) -> EcoVec<SourceDiagnostic> {
|
||||
let line_col = pos.into().try_line_col(&loaded.data).map(|p| p.numbers());
|
||||
match (loaded.source.v, line_col) {
|
||||
(LoadSource::Path(file), _) => {
|
||||
message.pop();
|
||||
if let Some(package) = file.package() {
|
||||
write!(
|
||||
&mut message,
|
||||
" in {package}{}",
|
||||
file.vpath().as_rooted_path().display()
|
||||
)
|
||||
.ok();
|
||||
} else {
|
||||
write!(&mut message, " in {}", file.vpath().as_rootless_path().display())
|
||||
.ok();
|
||||
};
|
||||
if let Some((line, col)) = line_col {
|
||||
write!(&mut message, ":{line}:{col}").ok();
|
||||
}
|
||||
message.push(')');
|
||||
}
|
||||
(LoadSource::Bytes, Some((line, col))) => {
|
||||
message.pop();
|
||||
write!(&mut message, " at {line}:{col})").ok();
|
||||
}
|
||||
(LoadSource::Bytes, None) => (),
|
||||
}
|
||||
eco_vec![SourceDiagnostic::error(loaded.source.span, message)]
|
||||
}
|
||||
|
||||
/// A position at which an error was reported.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
|
||||
pub enum ReportPos {
|
||||
/// Contains a range, and a line/column pair.
|
||||
Full(std::ops::Range<u32>, LineCol),
|
||||
/// Contains a range.
|
||||
Range(std::ops::Range<u32>),
|
||||
/// Contains a line/column pair.
|
||||
LineCol(LineCol),
|
||||
#[default]
|
||||
None,
|
||||
}
|
||||
|
||||
impl From<std::ops::Range<usize>> for ReportPos {
|
||||
fn from(value: std::ops::Range<usize>) -> Self {
|
||||
Self::Range(value.start.saturating_as()..value.end.saturating_as())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<LineCol> for ReportPos {
|
||||
fn from(value: LineCol) -> Self {
|
||||
Self::LineCol(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl ReportPos {
|
||||
/// Creates a position from a pre-existing range and line-column pair.
|
||||
pub fn full(range: std::ops::Range<usize>, pair: LineCol) -> Self {
|
||||
let range = range.start.saturating_as()..range.end.saturating_as();
|
||||
Self::Full(range, pair)
|
||||
}
|
||||
|
||||
/// Tries to determine the byte range for this position.
|
||||
fn range(&self, lines: &Lines<String>) -> Option<std::ops::Range<usize>> {
|
||||
match self {
|
||||
ReportPos::Full(range, _) => Some(range.start as usize..range.end as usize),
|
||||
ReportPos::Range(range) => Some(range.start as usize..range.end as usize),
|
||||
&ReportPos::LineCol(pair) => {
|
||||
let i =
|
||||
lines.line_column_to_byte(pair.line as usize, pair.col as usize)?;
|
||||
Some(i..i)
|
||||
}
|
||||
ReportPos::None => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Tries to determine the line/column for this position.
|
||||
fn line_col(&self, lines: &Lines<String>) -> Option<LineCol> {
|
||||
match self {
|
||||
&ReportPos::Full(_, pair) => Some(pair),
|
||||
ReportPos::Range(range) => {
|
||||
let (line, col) = lines.byte_to_line_column(range.start as usize)?;
|
||||
Some(LineCol::zero_based(line, col))
|
||||
}
|
||||
&ReportPos::LineCol(pair) => Some(pair),
|
||||
ReportPos::None => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Either gets the line/column pair, or tries to compute it from possibly
|
||||
/// invalid utf-8 data.
|
||||
fn try_line_col(&self, bytes: &[u8]) -> Option<LineCol> {
|
||||
match self {
|
||||
&ReportPos::Full(_, pair) => Some(pair),
|
||||
ReportPos::Range(range) => {
|
||||
LineCol::try_from_byte_pos(range.start as usize, bytes)
|
||||
}
|
||||
&ReportPos::LineCol(pair) => Some(pair),
|
||||
ReportPos::None => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A line/column pair.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct LineCol {
|
||||
/// The 0-based line.
|
||||
line: u32,
|
||||
/// The 0-based column.
|
||||
col: u32,
|
||||
}
|
||||
|
||||
impl LineCol {
|
||||
/// Constructs the line/column pair from 0-based indices.
|
||||
pub fn zero_based(line: usize, col: usize) -> Self {
|
||||
Self {
|
||||
line: line.saturating_as(),
|
||||
col: col.saturating_as(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Constructs the line/column pair from 1-based numbers.
|
||||
pub fn one_based(line: usize, col: usize) -> Self {
|
||||
Self::zero_based(line.saturating_sub(1), col.saturating_sub(1))
|
||||
}
|
||||
|
||||
/// Try to compute a line/column pair from possibly invalid utf-8 data.
|
||||
pub fn try_from_byte_pos(pos: usize, bytes: &[u8]) -> Option<Self> {
|
||||
let bytes = &bytes[..pos];
|
||||
let mut line = 0;
|
||||
#[allow(clippy::double_ended_iterator_last)]
|
||||
let line_start = memchr::memchr_iter(b'\n', bytes)
|
||||
.inspect(|_| line += 1)
|
||||
.last()
|
||||
.map(|i| i + 1)
|
||||
.unwrap_or(bytes.len());
|
||||
|
||||
let col = ErrorReportingUtf8Chars::new(&bytes[line_start..]).count();
|
||||
Some(LineCol::zero_based(line, col))
|
||||
}
|
||||
|
||||
/// Returns the 0-based line/column indices.
|
||||
pub fn indices(&self) -> (usize, usize) {
|
||||
(self.line as usize, self.col as usize)
|
||||
}
|
||||
|
||||
/// Returns the 1-based line/column numbers.
|
||||
pub fn numbers(&self) -> (usize, usize) {
|
||||
(self.line as usize + 1, self.col as usize + 1)
|
||||
}
|
||||
}
|
||||
|
||||
/// Format a user-facing error message for an XML-like file format.
|
||||
pub fn format_xml_like_error(format: &str, error: roxmltree::Error) -> EcoString {
|
||||
match error {
|
||||
roxmltree::Error::UnexpectedCloseTag(expected, actual, pos) => {
|
||||
eco_format!(
|
||||
"failed to parse {format} (found closing tag '{actual}' \
|
||||
instead of '{expected}' in line {})",
|
||||
pos.row
|
||||
)
|
||||
pub fn format_xml_like_error(format: &str, error: roxmltree::Error) -> LoadError {
|
||||
let pos = LineCol::one_based(error.pos().row as usize, error.pos().col as usize);
|
||||
let message = match error {
|
||||
roxmltree::Error::UnexpectedCloseTag(expected, actual, _) => {
|
||||
eco_format!("failed to parse {format} (found closing tag '{actual}' instead of '{expected}')")
|
||||
}
|
||||
roxmltree::Error::UnknownEntityReference(entity, pos) => {
|
||||
eco_format!(
|
||||
"failed to parse {format} (unknown entity '{entity}' in line {})",
|
||||
pos.row
|
||||
)
|
||||
roxmltree::Error::UnknownEntityReference(entity, _) => {
|
||||
eco_format!("failed to parse {format} (unknown entity '{entity}')")
|
||||
}
|
||||
roxmltree::Error::DuplicatedAttribute(attr, pos) => {
|
||||
eco_format!(
|
||||
"failed to parse {format} (duplicate attribute '{attr}' in line {})",
|
||||
pos.row
|
||||
)
|
||||
roxmltree::Error::DuplicatedAttribute(attr, _) => {
|
||||
eco_format!("failed to parse {format} (duplicate attribute '{attr}')")
|
||||
}
|
||||
roxmltree::Error::NoRootNode => {
|
||||
eco_format!("failed to parse {format} (missing root node)")
|
||||
}
|
||||
err => eco_format!("failed to parse {format} ({err})"),
|
||||
}
|
||||
};
|
||||
|
||||
LoadError { pos: pos.into(), message }
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ use std::sync::Arc;
|
||||
|
||||
use ecow::{eco_format, EcoString};
|
||||
use serde::{Serialize, Serializer};
|
||||
use typst_syntax::Lines;
|
||||
use typst_utils::LazyHash;
|
||||
|
||||
use crate::diag::{bail, StrResult};
|
||||
@ -286,6 +287,16 @@ impl Serialize for Bytes {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&Bytes> for Lines<String> {
|
||||
type Error = Utf8Error;
|
||||
|
||||
#[comemo::memoize]
|
||||
fn try_from(value: &Bytes) -> Result<Lines<String>, Utf8Error> {
|
||||
let text = value.as_str()?;
|
||||
Ok(Lines::new(text.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Any type that can back a byte buffer.
|
||||
trait Bytelike: Send + Sync {
|
||||
fn as_bytes(&self) -> &[u8];
|
||||
|
@ -9,7 +9,7 @@ use std::ops::Add;
|
||||
|
||||
use ecow::eco_format;
|
||||
use smallvec::SmallVec;
|
||||
use typst_syntax::{Span, Spanned};
|
||||
use typst_syntax::{Span, Spanned, SyntaxMode};
|
||||
use unicode_math_class::MathClass;
|
||||
|
||||
use crate::diag::{At, HintedStrResult, HintedString, SourceResult, StrResult};
|
||||
@ -459,6 +459,21 @@ impl FromValue for Never {
|
||||
}
|
||||
}
|
||||
|
||||
cast! {
|
||||
SyntaxMode,
|
||||
self => IntoValue::into_value(match self {
|
||||
SyntaxMode::Markup => "markup",
|
||||
SyntaxMode::Math => "math",
|
||||
SyntaxMode::Code => "code",
|
||||
}),
|
||||
/// Evaluate as markup, as in a Typst file.
|
||||
"markup" => SyntaxMode::Markup,
|
||||
/// Evaluate as math, as in an equation.
|
||||
"math" => SyntaxMode::Math,
|
||||
/// Evaluate as code, as after a hash.
|
||||
"code" => SyntaxMode::Code,
|
||||
}
|
||||
|
||||
cast! {
|
||||
MathClass,
|
||||
self => IntoValue::into_value(match self {
|
||||
|
@ -16,6 +16,21 @@ impl Duration {
|
||||
pub fn is_zero(&self) -> bool {
|
||||
self.0.is_zero()
|
||||
}
|
||||
|
||||
/// Decomposes the time into whole weeks, days, hours, minutes, and seconds.
|
||||
pub fn decompose(&self) -> [i64; 5] {
|
||||
let mut tmp = self.0;
|
||||
let weeks = tmp.whole_weeks();
|
||||
tmp -= weeks.weeks();
|
||||
let days = tmp.whole_days();
|
||||
tmp -= days.days();
|
||||
let hours = tmp.whole_hours();
|
||||
tmp -= hours.hours();
|
||||
let minutes = tmp.whole_minutes();
|
||||
tmp -= minutes.minutes();
|
||||
let seconds = tmp.whole_seconds();
|
||||
[weeks, days, hours, minutes, seconds]
|
||||
}
|
||||
}
|
||||
|
||||
#[scope]
|
||||
@ -118,34 +133,25 @@ impl Debug for Duration {
|
||||
|
||||
impl Repr for Duration {
|
||||
fn repr(&self) -> EcoString {
|
||||
let mut tmp = self.0;
|
||||
let [weeks, days, hours, minutes, seconds] = self.decompose();
|
||||
let mut vec = Vec::with_capacity(5);
|
||||
|
||||
let weeks = tmp.whole_seconds() / 604_800.0 as i64;
|
||||
if weeks != 0 {
|
||||
vec.push(eco_format!("weeks: {}", weeks.repr()));
|
||||
}
|
||||
tmp -= weeks.weeks();
|
||||
|
||||
let days = tmp.whole_days();
|
||||
if days != 0 {
|
||||
vec.push(eco_format!("days: {}", days.repr()));
|
||||
}
|
||||
tmp -= days.days();
|
||||
|
||||
let hours = tmp.whole_hours();
|
||||
if hours != 0 {
|
||||
vec.push(eco_format!("hours: {}", hours.repr()));
|
||||
}
|
||||
tmp -= hours.hours();
|
||||
|
||||
let minutes = tmp.whole_minutes();
|
||||
if minutes != 0 {
|
||||
vec.push(eco_format!("minutes: {}", minutes.repr()));
|
||||
}
|
||||
tmp -= minutes.minutes();
|
||||
|
||||
let seconds = tmp.whole_seconds();
|
||||
if seconds != 0 {
|
||||
vec.push(eco_format!("seconds: {}", seconds.repr()));
|
||||
}
|
||||
|
@ -210,3 +210,25 @@ cast! {
|
||||
fn parse_float(s: EcoString) -> Result<f64, ParseFloatError> {
|
||||
s.replace(repr::MINUS_SIGN, "-").parse()
|
||||
}
|
||||
|
||||
/// A floating-point number that must be positive (strictly larger than zero).
|
||||
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
|
||||
pub struct PositiveF64(f64);
|
||||
|
||||
impl PositiveF64 {
|
||||
/// Wrap a float if it is positive.
|
||||
pub fn new(value: f64) -> Option<Self> {
|
||||
(value > 0.0).then_some(Self(value))
|
||||
}
|
||||
|
||||
/// Get the underlying value.
|
||||
pub fn get(self) -> f64 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
cast! {
|
||||
PositiveF64,
|
||||
self => self.get().into_value(),
|
||||
v: f64 => Self::new(v).ok_or("number must be positive")?,
|
||||
}
|
||||
|
@ -307,7 +307,7 @@ impl Func {
|
||||
) -> SourceResult<Value> {
|
||||
match &self.repr {
|
||||
Repr::Native(native) => {
|
||||
let value = (native.function)(engine, context, &mut args)?;
|
||||
let value = (native.function.0)(engine, context, &mut args)?;
|
||||
args.finish()?;
|
||||
Ok(value)
|
||||
}
|
||||
@ -491,8 +491,8 @@ pub trait NativeFunc {
|
||||
/// Defines a native function.
|
||||
#[derive(Debug)]
|
||||
pub struct NativeFuncData {
|
||||
/// Invokes the function from Typst.
|
||||
pub function: fn(&mut Engine, Tracked<Context>, &mut Args) -> SourceResult<Value>,
|
||||
/// The implementation of the function.
|
||||
pub function: NativeFuncPtr,
|
||||
/// The function's normal name (e.g. `align`), as exposed to Typst.
|
||||
pub name: &'static str,
|
||||
/// The function's title case name (e.g. `Align`).
|
||||
@ -504,11 +504,11 @@ pub struct NativeFuncData {
|
||||
/// Whether this function makes use of context.
|
||||
pub contextual: bool,
|
||||
/// Definitions in the scope of the function.
|
||||
pub scope: LazyLock<Scope>,
|
||||
pub scope: DynLazyLock<Scope>,
|
||||
/// A list of parameter information for each parameter.
|
||||
pub params: LazyLock<Vec<ParamInfo>>,
|
||||
pub params: DynLazyLock<Vec<ParamInfo>>,
|
||||
/// Information about the return value of this function.
|
||||
pub returns: LazyLock<CastInfo>,
|
||||
pub returns: DynLazyLock<CastInfo>,
|
||||
}
|
||||
|
||||
cast! {
|
||||
@ -516,6 +516,28 @@ cast! {
|
||||
self => Func::from(self).into_value(),
|
||||
}
|
||||
|
||||
/// A pointer to a native function's implementation.
|
||||
pub struct NativeFuncPtr(pub &'static NativeFuncSignature);
|
||||
|
||||
/// The signature of a native function's implementation.
|
||||
type NativeFuncSignature =
|
||||
dyn Fn(&mut Engine, Tracked<Context>, &mut Args) -> SourceResult<Value> + Send + Sync;
|
||||
|
||||
impl Debug for NativeFuncPtr {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
f.pad("NativeFuncPtr(..)")
|
||||
}
|
||||
}
|
||||
|
||||
/// A `LazyLock` that uses a static closure for initialization instead of only
|
||||
/// working with function pointers.
|
||||
///
|
||||
/// Can be created from a normal function or closure by prepending with a `&`,
|
||||
/// e.g. `LazyLock::new(&|| "hello")`. Can be created from a dynamic closure
|
||||
/// by allocating and then leaking it. This is equivalent to having it
|
||||
/// statically allocated, but allows for it to be generated at runtime.
|
||||
type DynLazyLock<T> = LazyLock<T, &'static (dyn Fn() -> T + Send + Sync)>;
|
||||
|
||||
/// Describes a function parameter.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ParamInfo {
|
||||
|
@ -1,4 +1,6 @@
|
||||
use std::num::{NonZeroI64, NonZeroIsize, NonZeroU64, NonZeroUsize, ParseIntError};
|
||||
use std::num::{
|
||||
NonZeroI64, NonZeroIsize, NonZeroU32, NonZeroU64, NonZeroUsize, ParseIntError,
|
||||
};
|
||||
|
||||
use ecow::{eco_format, EcoString};
|
||||
use smallvec::SmallVec;
|
||||
@ -482,3 +484,16 @@ cast! {
|
||||
"number too large"
|
||||
})?,
|
||||
}
|
||||
|
||||
cast! {
|
||||
NonZeroU32,
|
||||
self => Value::Int(self.get() as _),
|
||||
v: i64 => v
|
||||
.try_into()
|
||||
.and_then(|v: u32| v.try_into())
|
||||
.map_err(|_| if v <= 0 {
|
||||
"number must be positive"
|
||||
} else {
|
||||
"number too large"
|
||||
})?,
|
||||
}
|
||||
|
@ -79,7 +79,12 @@ impl Label {
|
||||
|
||||
impl Repr for Label {
|
||||
fn repr(&self) -> EcoString {
|
||||
eco_format!("<{}>", self.resolve())
|
||||
let resolved = self.resolve();
|
||||
if typst_syntax::is_valid_label_literal_id(&resolved) {
|
||||
eco_format!("<{resolved}>")
|
||||
} else {
|
||||
eco_format!("label({})", resolved.repr())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -69,6 +69,7 @@ pub use self::ty::*;
|
||||
pub use self::value::*;
|
||||
pub use self::version::*;
|
||||
pub use typst_macros::{scope, ty};
|
||||
use typst_syntax::SyntaxMode;
|
||||
|
||||
#[rustfmt::skip]
|
||||
#[doc(hidden)]
|
||||
@ -83,7 +84,6 @@ use typst_syntax::Spanned;
|
||||
|
||||
use crate::diag::{bail, SourceResult, StrResult};
|
||||
use crate::engine::Engine;
|
||||
use crate::routines::EvalMode;
|
||||
use crate::{Feature, Features};
|
||||
|
||||
/// Hook up all `foundations` definitions.
|
||||
@ -273,8 +273,8 @@ pub fn eval(
|
||||
/// #eval("1_2^3", mode: "math")
|
||||
/// ```
|
||||
#[named]
|
||||
#[default(EvalMode::Code)]
|
||||
mode: EvalMode,
|
||||
#[default(SyntaxMode::Code)]
|
||||
mode: SyntaxMode,
|
||||
/// A scope of definitions that are made available.
|
||||
///
|
||||
/// ```example
|
||||
|
@ -7,9 +7,10 @@ use typst_syntax::FileId;
|
||||
use crate::diag::{bail, DeprecationSink, StrResult};
|
||||
use crate::foundations::{repr, ty, Content, Scope, Value};
|
||||
|
||||
/// A module of definitions.
|
||||
/// A collection of variables and functions that are commonly related to
|
||||
/// a single theme.
|
||||
///
|
||||
/// A module
|
||||
/// A module can
|
||||
/// - be built-in
|
||||
/// - stem from a [file import]($scripting/#modules)
|
||||
/// - stem from a [package import]($scripting/#packages) (and thus indirectly
|
||||
|
@ -151,8 +151,8 @@ pub fn plugin(
|
||||
/// A [path]($syntax/#paths) to a WebAssembly file or raw WebAssembly bytes.
|
||||
source: Spanned<DataSource>,
|
||||
) -> SourceResult<Module> {
|
||||
let data = source.load(engine.world)?;
|
||||
Plugin::module(data).at(source.span)
|
||||
let loaded = source.load(engine.world)?;
|
||||
Plugin::module(loaded.data).at(source.span)
|
||||
}
|
||||
|
||||
#[scope]
|
||||
|
@ -1,8 +1,8 @@
|
||||
use std::cmp::Reverse;
|
||||
use std::collections::{BTreeSet, HashMap};
|
||||
use std::fmt::{self, Debug, Display, Formatter, Write};
|
||||
use std::sync::Arc;
|
||||
|
||||
use codex::ModifierSet;
|
||||
use ecow::{eco_format, EcoString};
|
||||
use serde::{Serialize, Serializer};
|
||||
use typst_syntax::{is_ident, Span, Spanned};
|
||||
@ -54,18 +54,18 @@ enum Repr {
|
||||
/// A native symbol that has no named variant.
|
||||
Single(char),
|
||||
/// A native symbol with multiple named variants.
|
||||
Complex(&'static [(&'static str, char)]),
|
||||
Complex(&'static [(ModifierSet<&'static str>, char)]),
|
||||
/// A symbol with multiple named variants, where some modifiers may have
|
||||
/// been applied. Also used for symbols defined at runtime by the user with
|
||||
/// no modifier applied.
|
||||
Modified(Arc<(List, EcoString)>),
|
||||
Modified(Arc<(List, ModifierSet<EcoString>)>),
|
||||
}
|
||||
|
||||
/// A collection of symbols.
|
||||
#[derive(Clone, Eq, PartialEq, Hash)]
|
||||
enum List {
|
||||
Static(&'static [(&'static str, char)]),
|
||||
Runtime(Box<[(EcoString, char)]>),
|
||||
Static(&'static [(ModifierSet<&'static str>, char)]),
|
||||
Runtime(Box<[(ModifierSet<EcoString>, char)]>),
|
||||
}
|
||||
|
||||
impl Symbol {
|
||||
@ -76,24 +76,26 @@ impl Symbol {
|
||||
|
||||
/// Create a symbol with a static variant list.
|
||||
#[track_caller]
|
||||
pub const fn list(list: &'static [(&'static str, char)]) -> Self {
|
||||
pub const fn list(list: &'static [(ModifierSet<&'static str>, char)]) -> Self {
|
||||
debug_assert!(!list.is_empty());
|
||||
Self(Repr::Complex(list))
|
||||
}
|
||||
|
||||
/// Create a symbol with a runtime variant list.
|
||||
#[track_caller]
|
||||
pub fn runtime(list: Box<[(EcoString, char)]>) -> Self {
|
||||
pub fn runtime(list: Box<[(ModifierSet<EcoString>, char)]>) -> Self {
|
||||
debug_assert!(!list.is_empty());
|
||||
Self(Repr::Modified(Arc::new((List::Runtime(list), EcoString::new()))))
|
||||
Self(Repr::Modified(Arc::new((List::Runtime(list), ModifierSet::default()))))
|
||||
}
|
||||
|
||||
/// Get the symbol's character.
|
||||
pub fn get(&self) -> char {
|
||||
match &self.0 {
|
||||
Repr::Single(c) => *c,
|
||||
Repr::Complex(_) => find(self.variants(), "").unwrap(),
|
||||
Repr::Modified(arc) => find(self.variants(), &arc.1).unwrap(),
|
||||
Repr::Complex(_) => ModifierSet::<&'static str>::default()
|
||||
.best_match_in(self.variants())
|
||||
.unwrap(),
|
||||
Repr::Modified(arc) => arc.1.best_match_in(self.variants()).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -128,16 +130,14 @@ impl Symbol {
|
||||
/// Apply a modifier to the symbol.
|
||||
pub fn modified(mut self, modifier: &str) -> StrResult<Self> {
|
||||
if let Repr::Complex(list) = self.0 {
|
||||
self.0 = Repr::Modified(Arc::new((List::Static(list), EcoString::new())));
|
||||
self.0 =
|
||||
Repr::Modified(Arc::new((List::Static(list), ModifierSet::default())));
|
||||
}
|
||||
|
||||
if let Repr::Modified(arc) = &mut self.0 {
|
||||
let (list, modifiers) = Arc::make_mut(arc);
|
||||
if !modifiers.is_empty() {
|
||||
modifiers.push('.');
|
||||
}
|
||||
modifiers.push_str(modifier);
|
||||
if find(list.variants(), modifiers).is_some() {
|
||||
modifiers.insert_raw(modifier);
|
||||
if modifiers.best_match_in(list.variants()).is_some() {
|
||||
return Ok(self);
|
||||
}
|
||||
}
|
||||
@ -146,7 +146,7 @@ impl Symbol {
|
||||
}
|
||||
|
||||
/// The characters that are covered by this symbol.
|
||||
pub fn variants(&self) -> impl Iterator<Item = (&str, char)> {
|
||||
pub fn variants(&self) -> impl Iterator<Item = (ModifierSet<&str>, char)> {
|
||||
match &self.0 {
|
||||
Repr::Single(c) => Variants::Single(Some(*c).into_iter()),
|
||||
Repr::Complex(list) => Variants::Static(list.iter()),
|
||||
@ -156,17 +156,15 @@ impl Symbol {
|
||||
|
||||
/// Possible modifiers.
|
||||
pub fn modifiers(&self) -> impl Iterator<Item = &str> + '_ {
|
||||
let mut set = BTreeSet::new();
|
||||
let modifiers = match &self.0 {
|
||||
Repr::Modified(arc) => arc.1.as_str(),
|
||||
_ => "",
|
||||
Repr::Modified(arc) => arc.1.as_deref(),
|
||||
_ => ModifierSet::default(),
|
||||
};
|
||||
for modifier in self.variants().flat_map(|(name, _)| name.split('.')) {
|
||||
if !modifier.is_empty() && !contained(modifiers, modifier) {
|
||||
set.insert(modifier);
|
||||
}
|
||||
}
|
||||
set.into_iter()
|
||||
self.variants()
|
||||
.flat_map(|(m, _)| m)
|
||||
.filter(|modifier| !modifier.is_empty() && !modifiers.contains(modifier))
|
||||
.collect::<BTreeSet<_>>()
|
||||
.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
@ -256,7 +254,10 @@ impl Symbol {
|
||||
seen.insert(hash, i);
|
||||
}
|
||||
|
||||
let list = variants.into_iter().map(|s| (s.v.0, s.v.1)).collect();
|
||||
let list = variants
|
||||
.into_iter()
|
||||
.map(|s| (ModifierSet::from_raw_dotted(s.v.0), s.v.1))
|
||||
.collect();
|
||||
Ok(Symbol::runtime(list))
|
||||
}
|
||||
}
|
||||
@ -291,14 +292,23 @@ impl crate::foundations::Repr for Symbol {
|
||||
match &self.0 {
|
||||
Repr::Single(c) => eco_format!("symbol(\"{}\")", *c),
|
||||
Repr::Complex(variants) => {
|
||||
eco_format!("symbol{}", repr_variants(variants.iter().copied(), ""))
|
||||
eco_format!(
|
||||
"symbol{}",
|
||||
repr_variants(variants.iter().copied(), ModifierSet::default())
|
||||
)
|
||||
}
|
||||
Repr::Modified(arc) => {
|
||||
let (list, modifiers) = arc.as_ref();
|
||||
if modifiers.is_empty() {
|
||||
eco_format!("symbol{}", repr_variants(list.variants(), ""))
|
||||
eco_format!(
|
||||
"symbol{}",
|
||||
repr_variants(list.variants(), ModifierSet::default())
|
||||
)
|
||||
} else {
|
||||
eco_format!("symbol{}", repr_variants(list.variants(), modifiers))
|
||||
eco_format!(
|
||||
"symbol{}",
|
||||
repr_variants(list.variants(), modifiers.as_deref())
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -306,24 +316,24 @@ impl crate::foundations::Repr for Symbol {
|
||||
}
|
||||
|
||||
fn repr_variants<'a>(
|
||||
variants: impl Iterator<Item = (&'a str, char)>,
|
||||
applied_modifiers: &str,
|
||||
variants: impl Iterator<Item = (ModifierSet<&'a str>, char)>,
|
||||
applied_modifiers: ModifierSet<&str>,
|
||||
) -> String {
|
||||
crate::foundations::repr::pretty_array_like(
|
||||
&variants
|
||||
.filter(|(variant, _)| {
|
||||
.filter(|(modifiers, _)| {
|
||||
// Only keep variants that can still be accessed, i.e., variants
|
||||
// that contain all applied modifiers.
|
||||
parts(applied_modifiers).all(|am| variant.split('.').any(|m| m == am))
|
||||
applied_modifiers.iter().all(|am| modifiers.contains(am))
|
||||
})
|
||||
.map(|(variant, c)| {
|
||||
let trimmed_variant = variant
|
||||
.split('.')
|
||||
.filter(|&m| parts(applied_modifiers).all(|am| m != am));
|
||||
if trimmed_variant.clone().all(|m| m.is_empty()) {
|
||||
.map(|(modifiers, c)| {
|
||||
let trimmed_modifiers =
|
||||
modifiers.into_iter().filter(|&m| !applied_modifiers.contains(m));
|
||||
if trimmed_modifiers.clone().all(|m| m.is_empty()) {
|
||||
eco_format!("\"{c}\"")
|
||||
} else {
|
||||
let trimmed_modifiers = trimmed_variant.collect::<Vec<_>>().join(".");
|
||||
let trimmed_modifiers =
|
||||
trimmed_modifiers.collect::<Vec<_>>().join(".");
|
||||
eco_format!("(\"{}\", \"{}\")", trimmed_modifiers, c)
|
||||
}
|
||||
})
|
||||
@ -369,67 +379,22 @@ cast! {
|
||||
/// Iterator over variants.
|
||||
enum Variants<'a> {
|
||||
Single(std::option::IntoIter<char>),
|
||||
Static(std::slice::Iter<'static, (&'static str, char)>),
|
||||
Runtime(std::slice::Iter<'a, (EcoString, char)>),
|
||||
Static(std::slice::Iter<'static, (ModifierSet<&'static str>, char)>),
|
||||
Runtime(std::slice::Iter<'a, (ModifierSet<EcoString>, char)>),
|
||||
}
|
||||
|
||||
impl<'a> Iterator for Variants<'a> {
|
||||
type Item = (&'a str, char);
|
||||
type Item = (ModifierSet<&'a str>, char);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
match self {
|
||||
Self::Single(iter) => Some(("", iter.next()?)),
|
||||
Self::Single(iter) => Some((ModifierSet::default(), iter.next()?)),
|
||||
Self::Static(list) => list.next().copied(),
|
||||
Self::Runtime(list) => list.next().map(|(s, c)| (s.as_str(), *c)),
|
||||
Self::Runtime(list) => list.next().map(|(m, c)| (m.as_deref(), *c)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Find the best symbol from the list.
|
||||
fn find<'a>(
|
||||
variants: impl Iterator<Item = (&'a str, char)>,
|
||||
modifiers: &str,
|
||||
) -> Option<char> {
|
||||
let mut best = None;
|
||||
let mut best_score = None;
|
||||
|
||||
// Find the best table entry with this name.
|
||||
'outer: for candidate in variants {
|
||||
for modifier in parts(modifiers) {
|
||||
if !contained(candidate.0, modifier) {
|
||||
continue 'outer;
|
||||
}
|
||||
}
|
||||
|
||||
let mut matching = 0;
|
||||
let mut total = 0;
|
||||
for modifier in parts(candidate.0) {
|
||||
if contained(modifiers, modifier) {
|
||||
matching += 1;
|
||||
}
|
||||
total += 1;
|
||||
}
|
||||
|
||||
let score = (matching, Reverse(total));
|
||||
if best_score.is_none_or(|b| score > b) {
|
||||
best = Some(candidate.1);
|
||||
best_score = Some(score);
|
||||
}
|
||||
}
|
||||
|
||||
best
|
||||
}
|
||||
|
||||
/// Split a modifier list into its parts.
|
||||
fn parts(modifiers: &str) -> impl Iterator<Item = &str> {
|
||||
modifiers.split('.').filter(|s| !s.is_empty())
|
||||
}
|
||||
|
||||
/// Whether the modifier string contains the modifier `m`.
|
||||
fn contained(modifiers: &str, m: &str) -> bool {
|
||||
parts(modifiers).any(|part| part == m)
|
||||
}
|
||||
|
||||
/// A single character.
|
||||
#[elem(Repr, PlainText)]
|
||||
pub struct SymbolElem {
|
||||
|
@ -188,7 +188,7 @@ cast! {
|
||||
.collect::<HintedStrResult<_>>()?),
|
||||
}
|
||||
|
||||
/// An attribute of an HTML.
|
||||
/// An attribute of an HTML element.
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
|
||||
pub struct HtmlAttr(PicoStr);
|
||||
|
||||
@ -347,135 +347,124 @@ pub mod charsets {
|
||||
}
|
||||
|
||||
/// Predefined constants for HTML tags.
|
||||
#[allow(non_upper_case_globals)]
|
||||
pub mod tag {
|
||||
use super::HtmlTag;
|
||||
|
||||
macro_rules! tags {
|
||||
($($tag:ident)*) => {
|
||||
$(#[allow(non_upper_case_globals)]
|
||||
pub const $tag: HtmlTag = HtmlTag::constant(
|
||||
stringify!($tag)
|
||||
);)*
|
||||
}
|
||||
}
|
||||
pub const a: HtmlTag = HtmlTag::constant("a");
|
||||
pub const abbr: HtmlTag = HtmlTag::constant("abbr");
|
||||
pub const address: HtmlTag = HtmlTag::constant("address");
|
||||
pub const area: HtmlTag = HtmlTag::constant("area");
|
||||
pub const article: HtmlTag = HtmlTag::constant("article");
|
||||
pub const aside: HtmlTag = HtmlTag::constant("aside");
|
||||
pub const audio: HtmlTag = HtmlTag::constant("audio");
|
||||
pub const b: HtmlTag = HtmlTag::constant("b");
|
||||
pub const base: HtmlTag = HtmlTag::constant("base");
|
||||
pub const bdi: HtmlTag = HtmlTag::constant("bdi");
|
||||
pub const bdo: HtmlTag = HtmlTag::constant("bdo");
|
||||
pub const blockquote: HtmlTag = HtmlTag::constant("blockquote");
|
||||
pub const body: HtmlTag = HtmlTag::constant("body");
|
||||
pub const br: HtmlTag = HtmlTag::constant("br");
|
||||
pub const button: HtmlTag = HtmlTag::constant("button");
|
||||
pub const canvas: HtmlTag = HtmlTag::constant("canvas");
|
||||
pub const caption: HtmlTag = HtmlTag::constant("caption");
|
||||
pub const cite: HtmlTag = HtmlTag::constant("cite");
|
||||
pub const code: HtmlTag = HtmlTag::constant("code");
|
||||
pub const col: HtmlTag = HtmlTag::constant("col");
|
||||
pub const colgroup: HtmlTag = HtmlTag::constant("colgroup");
|
||||
pub const data: HtmlTag = HtmlTag::constant("data");
|
||||
pub const datalist: HtmlTag = HtmlTag::constant("datalist");
|
||||
pub const dd: HtmlTag = HtmlTag::constant("dd");
|
||||
pub const del: HtmlTag = HtmlTag::constant("del");
|
||||
pub const details: HtmlTag = HtmlTag::constant("details");
|
||||
pub const dfn: HtmlTag = HtmlTag::constant("dfn");
|
||||
pub const dialog: HtmlTag = HtmlTag::constant("dialog");
|
||||
pub const div: HtmlTag = HtmlTag::constant("div");
|
||||
pub const dl: HtmlTag = HtmlTag::constant("dl");
|
||||
pub const dt: HtmlTag = HtmlTag::constant("dt");
|
||||
pub const em: HtmlTag = HtmlTag::constant("em");
|
||||
pub const embed: HtmlTag = HtmlTag::constant("embed");
|
||||
pub const fieldset: HtmlTag = HtmlTag::constant("fieldset");
|
||||
pub const figcaption: HtmlTag = HtmlTag::constant("figcaption");
|
||||
pub const figure: HtmlTag = HtmlTag::constant("figure");
|
||||
pub const footer: HtmlTag = HtmlTag::constant("footer");
|
||||
pub const form: HtmlTag = HtmlTag::constant("form");
|
||||
pub const h1: HtmlTag = HtmlTag::constant("h1");
|
||||
pub const h2: HtmlTag = HtmlTag::constant("h2");
|
||||
pub const h3: HtmlTag = HtmlTag::constant("h3");
|
||||
pub const h4: HtmlTag = HtmlTag::constant("h4");
|
||||
pub const h5: HtmlTag = HtmlTag::constant("h5");
|
||||
pub const h6: HtmlTag = HtmlTag::constant("h6");
|
||||
pub const head: HtmlTag = HtmlTag::constant("head");
|
||||
pub const header: HtmlTag = HtmlTag::constant("header");
|
||||
pub const hgroup: HtmlTag = HtmlTag::constant("hgroup");
|
||||
pub const hr: HtmlTag = HtmlTag::constant("hr");
|
||||
pub const html: HtmlTag = HtmlTag::constant("html");
|
||||
pub const i: HtmlTag = HtmlTag::constant("i");
|
||||
pub const iframe: HtmlTag = HtmlTag::constant("iframe");
|
||||
pub const img: HtmlTag = HtmlTag::constant("img");
|
||||
pub const input: HtmlTag = HtmlTag::constant("input");
|
||||
pub const ins: HtmlTag = HtmlTag::constant("ins");
|
||||
pub const kbd: HtmlTag = HtmlTag::constant("kbd");
|
||||
pub const label: HtmlTag = HtmlTag::constant("label");
|
||||
pub const legend: HtmlTag = HtmlTag::constant("legend");
|
||||
pub const li: HtmlTag = HtmlTag::constant("li");
|
||||
pub const link: HtmlTag = HtmlTag::constant("link");
|
||||
pub const main: HtmlTag = HtmlTag::constant("main");
|
||||
pub const map: HtmlTag = HtmlTag::constant("map");
|
||||
pub const mark: HtmlTag = HtmlTag::constant("mark");
|
||||
pub const menu: HtmlTag = HtmlTag::constant("menu");
|
||||
pub const meta: HtmlTag = HtmlTag::constant("meta");
|
||||
pub const meter: HtmlTag = HtmlTag::constant("meter");
|
||||
pub const nav: HtmlTag = HtmlTag::constant("nav");
|
||||
pub const noscript: HtmlTag = HtmlTag::constant("noscript");
|
||||
pub const object: HtmlTag = HtmlTag::constant("object");
|
||||
pub const ol: HtmlTag = HtmlTag::constant("ol");
|
||||
pub const optgroup: HtmlTag = HtmlTag::constant("optgroup");
|
||||
pub const option: HtmlTag = HtmlTag::constant("option");
|
||||
pub const output: HtmlTag = HtmlTag::constant("output");
|
||||
pub const p: HtmlTag = HtmlTag::constant("p");
|
||||
pub const picture: HtmlTag = HtmlTag::constant("picture");
|
||||
pub const pre: HtmlTag = HtmlTag::constant("pre");
|
||||
pub const progress: HtmlTag = HtmlTag::constant("progress");
|
||||
pub const q: HtmlTag = HtmlTag::constant("q");
|
||||
pub const rp: HtmlTag = HtmlTag::constant("rp");
|
||||
pub const rt: HtmlTag = HtmlTag::constant("rt");
|
||||
pub const ruby: HtmlTag = HtmlTag::constant("ruby");
|
||||
pub const s: HtmlTag = HtmlTag::constant("s");
|
||||
pub const samp: HtmlTag = HtmlTag::constant("samp");
|
||||
pub const script: HtmlTag = HtmlTag::constant("script");
|
||||
pub const search: HtmlTag = HtmlTag::constant("search");
|
||||
pub const section: HtmlTag = HtmlTag::constant("section");
|
||||
pub const select: HtmlTag = HtmlTag::constant("select");
|
||||
pub const slot: HtmlTag = HtmlTag::constant("slot");
|
||||
pub const small: HtmlTag = HtmlTag::constant("small");
|
||||
pub const source: HtmlTag = HtmlTag::constant("source");
|
||||
pub const span: HtmlTag = HtmlTag::constant("span");
|
||||
pub const strong: HtmlTag = HtmlTag::constant("strong");
|
||||
pub const style: HtmlTag = HtmlTag::constant("style");
|
||||
pub const sub: HtmlTag = HtmlTag::constant("sub");
|
||||
pub const summary: HtmlTag = HtmlTag::constant("summary");
|
||||
pub const sup: HtmlTag = HtmlTag::constant("sup");
|
||||
pub const table: HtmlTag = HtmlTag::constant("table");
|
||||
pub const tbody: HtmlTag = HtmlTag::constant("tbody");
|
||||
pub const td: HtmlTag = HtmlTag::constant("td");
|
||||
pub const template: HtmlTag = HtmlTag::constant("template");
|
||||
pub const textarea: HtmlTag = HtmlTag::constant("textarea");
|
||||
pub const tfoot: HtmlTag = HtmlTag::constant("tfoot");
|
||||
pub const th: HtmlTag = HtmlTag::constant("th");
|
||||
pub const thead: HtmlTag = HtmlTag::constant("thead");
|
||||
pub const time: HtmlTag = HtmlTag::constant("time");
|
||||
pub const title: HtmlTag = HtmlTag::constant("title");
|
||||
pub const tr: HtmlTag = HtmlTag::constant("tr");
|
||||
pub const track: HtmlTag = HtmlTag::constant("track");
|
||||
pub const u: HtmlTag = HtmlTag::constant("u");
|
||||
pub const ul: HtmlTag = HtmlTag::constant("ul");
|
||||
pub const var: HtmlTag = HtmlTag::constant("var");
|
||||
pub const video: HtmlTag = HtmlTag::constant("video");
|
||||
pub const wbr: HtmlTag = HtmlTag::constant("wbr");
|
||||
|
||||
tags! {
|
||||
a
|
||||
abbr
|
||||
address
|
||||
area
|
||||
article
|
||||
aside
|
||||
audio
|
||||
b
|
||||
base
|
||||
bdi
|
||||
bdo
|
||||
blockquote
|
||||
body
|
||||
br
|
||||
button
|
||||
canvas
|
||||
caption
|
||||
cite
|
||||
code
|
||||
col
|
||||
colgroup
|
||||
data
|
||||
datalist
|
||||
dd
|
||||
del
|
||||
details
|
||||
dfn
|
||||
dialog
|
||||
div
|
||||
dl
|
||||
dt
|
||||
em
|
||||
embed
|
||||
fieldset
|
||||
figcaption
|
||||
figure
|
||||
footer
|
||||
form
|
||||
h1
|
||||
h2
|
||||
h3
|
||||
h4
|
||||
h5
|
||||
h6
|
||||
head
|
||||
header
|
||||
hgroup
|
||||
hr
|
||||
html
|
||||
i
|
||||
iframe
|
||||
img
|
||||
input
|
||||
ins
|
||||
kbd
|
||||
label
|
||||
legend
|
||||
li
|
||||
link
|
||||
main
|
||||
map
|
||||
mark
|
||||
menu
|
||||
meta
|
||||
meter
|
||||
nav
|
||||
noscript
|
||||
object
|
||||
ol
|
||||
optgroup
|
||||
option
|
||||
output
|
||||
p
|
||||
param
|
||||
picture
|
||||
pre
|
||||
progress
|
||||
q
|
||||
rp
|
||||
rt
|
||||
ruby
|
||||
s
|
||||
samp
|
||||
script
|
||||
search
|
||||
section
|
||||
select
|
||||
slot
|
||||
small
|
||||
source
|
||||
span
|
||||
strong
|
||||
style
|
||||
sub
|
||||
summary
|
||||
sup
|
||||
table
|
||||
tbody
|
||||
td
|
||||
template
|
||||
textarea
|
||||
tfoot
|
||||
th
|
||||
thead
|
||||
time
|
||||
title
|
||||
tr
|
||||
track
|
||||
u
|
||||
ul
|
||||
var
|
||||
video
|
||||
wbr
|
||||
}
|
||||
|
||||
/// Whether this is a void tag whose associated element may not have a
|
||||
/// Whether this is a void tag whose associated element may not have
|
||||
/// children.
|
||||
pub fn is_void(tag: HtmlTag) -> bool {
|
||||
matches!(
|
||||
@ -490,7 +479,6 @@ pub mod tag {
|
||||
| self::input
|
||||
| self::link
|
||||
| self::meta
|
||||
| self::param
|
||||
| self::source
|
||||
| self::track
|
||||
| self::wbr
|
||||
@ -629,36 +617,196 @@ pub mod tag {
|
||||
}
|
||||
}
|
||||
|
||||
/// Predefined constants for HTML attributes.
|
||||
///
|
||||
/// Note: These are very incomplete.
|
||||
#[allow(non_upper_case_globals)]
|
||||
#[rustfmt::skip]
|
||||
pub mod attr {
|
||||
use super::HtmlAttr;
|
||||
|
||||
macro_rules! attrs {
|
||||
($($attr:ident)*) => {
|
||||
$(#[allow(non_upper_case_globals)]
|
||||
pub const $attr: HtmlAttr = HtmlAttr::constant(
|
||||
stringify!($attr)
|
||||
);)*
|
||||
}
|
||||
}
|
||||
|
||||
attrs! {
|
||||
charset
|
||||
cite
|
||||
colspan
|
||||
content
|
||||
href
|
||||
name
|
||||
reversed
|
||||
role
|
||||
rowspan
|
||||
start
|
||||
style
|
||||
value
|
||||
}
|
||||
|
||||
use crate::html::HtmlAttr;
|
||||
pub const abbr: HtmlAttr = HtmlAttr::constant("abbr");
|
||||
pub const accept: HtmlAttr = HtmlAttr::constant("accept");
|
||||
pub const accept_charset: HtmlAttr = HtmlAttr::constant("accept-charset");
|
||||
pub const accesskey: HtmlAttr = HtmlAttr::constant("accesskey");
|
||||
pub const action: HtmlAttr = HtmlAttr::constant("action");
|
||||
pub const allow: HtmlAttr = HtmlAttr::constant("allow");
|
||||
pub const allowfullscreen: HtmlAttr = HtmlAttr::constant("allowfullscreen");
|
||||
pub const alpha: HtmlAttr = HtmlAttr::constant("alpha");
|
||||
pub const alt: HtmlAttr = HtmlAttr::constant("alt");
|
||||
pub const aria_activedescendant: HtmlAttr = HtmlAttr::constant("aria-activedescendant");
|
||||
pub const aria_atomic: HtmlAttr = HtmlAttr::constant("aria-atomic");
|
||||
pub const aria_autocomplete: HtmlAttr = HtmlAttr::constant("aria-autocomplete");
|
||||
pub const aria_busy: HtmlAttr = HtmlAttr::constant("aria-busy");
|
||||
pub const aria_checked: HtmlAttr = HtmlAttr::constant("aria-checked");
|
||||
pub const aria_colcount: HtmlAttr = HtmlAttr::constant("aria-colcount");
|
||||
pub const aria_colindex: HtmlAttr = HtmlAttr::constant("aria-colindex");
|
||||
pub const aria_colspan: HtmlAttr = HtmlAttr::constant("aria-colspan");
|
||||
pub const aria_controls: HtmlAttr = HtmlAttr::constant("aria-controls");
|
||||
pub const aria_current: HtmlAttr = HtmlAttr::constant("aria-current");
|
||||
pub const aria_describedby: HtmlAttr = HtmlAttr::constant("aria-describedby");
|
||||
pub const aria_details: HtmlAttr = HtmlAttr::constant("aria-details");
|
||||
pub const aria_disabled: HtmlAttr = HtmlAttr::constant("aria-disabled");
|
||||
pub const aria_errormessage: HtmlAttr = HtmlAttr::constant("aria-errormessage");
|
||||
pub const aria_expanded: HtmlAttr = HtmlAttr::constant("aria-expanded");
|
||||
pub const aria_flowto: HtmlAttr = HtmlAttr::constant("aria-flowto");
|
||||
pub const aria_haspopup: HtmlAttr = HtmlAttr::constant("aria-haspopup");
|
||||
pub const aria_hidden: HtmlAttr = HtmlAttr::constant("aria-hidden");
|
||||
pub const aria_invalid: HtmlAttr = HtmlAttr::constant("aria-invalid");
|
||||
pub const aria_keyshortcuts: HtmlAttr = HtmlAttr::constant("aria-keyshortcuts");
|
||||
pub const aria_label: HtmlAttr = HtmlAttr::constant("aria-label");
|
||||
pub const aria_labelledby: HtmlAttr = HtmlAttr::constant("aria-labelledby");
|
||||
pub const aria_level: HtmlAttr = HtmlAttr::constant("aria-level");
|
||||
pub const aria_live: HtmlAttr = HtmlAttr::constant("aria-live");
|
||||
pub const aria_modal: HtmlAttr = HtmlAttr::constant("aria-modal");
|
||||
pub const aria_multiline: HtmlAttr = HtmlAttr::constant("aria-multiline");
|
||||
pub const aria_multiselectable: HtmlAttr = HtmlAttr::constant("aria-multiselectable");
|
||||
pub const aria_orientation: HtmlAttr = HtmlAttr::constant("aria-orientation");
|
||||
pub const aria_owns: HtmlAttr = HtmlAttr::constant("aria-owns");
|
||||
pub const aria_placeholder: HtmlAttr = HtmlAttr::constant("aria-placeholder");
|
||||
pub const aria_posinset: HtmlAttr = HtmlAttr::constant("aria-posinset");
|
||||
pub const aria_pressed: HtmlAttr = HtmlAttr::constant("aria-pressed");
|
||||
pub const aria_readonly: HtmlAttr = HtmlAttr::constant("aria-readonly");
|
||||
pub const aria_relevant: HtmlAttr = HtmlAttr::constant("aria-relevant");
|
||||
pub const aria_required: HtmlAttr = HtmlAttr::constant("aria-required");
|
||||
pub const aria_roledescription: HtmlAttr = HtmlAttr::constant("aria-roledescription");
|
||||
pub const aria_rowcount: HtmlAttr = HtmlAttr::constant("aria-rowcount");
|
||||
pub const aria_rowindex: HtmlAttr = HtmlAttr::constant("aria-rowindex");
|
||||
pub const aria_rowspan: HtmlAttr = HtmlAttr::constant("aria-rowspan");
|
||||
pub const aria_selected: HtmlAttr = HtmlAttr::constant("aria-selected");
|
||||
pub const aria_setsize: HtmlAttr = HtmlAttr::constant("aria-setsize");
|
||||
pub const aria_sort: HtmlAttr = HtmlAttr::constant("aria-sort");
|
||||
pub const aria_valuemax: HtmlAttr = HtmlAttr::constant("aria-valuemax");
|
||||
pub const aria_valuemin: HtmlAttr = HtmlAttr::constant("aria-valuemin");
|
||||
pub const aria_valuenow: HtmlAttr = HtmlAttr::constant("aria-valuenow");
|
||||
pub const aria_valuetext: HtmlAttr = HtmlAttr::constant("aria-valuetext");
|
||||
pub const r#as: HtmlAttr = HtmlAttr::constant("as");
|
||||
pub const r#async: HtmlAttr = HtmlAttr::constant("async");
|
||||
pub const autocapitalize: HtmlAttr = HtmlAttr::constant("autocapitalize");
|
||||
pub const autocomplete: HtmlAttr = HtmlAttr::constant("autocomplete");
|
||||
pub const autocorrect: HtmlAttr = HtmlAttr::constant("autocorrect");
|
||||
pub const autofocus: HtmlAttr = HtmlAttr::constant("autofocus");
|
||||
pub const autoplay: HtmlAttr = HtmlAttr::constant("autoplay");
|
||||
pub const blocking: HtmlAttr = HtmlAttr::constant("blocking");
|
||||
pub const charset: HtmlAttr = HtmlAttr::constant("charset");
|
||||
pub const checked: HtmlAttr = HtmlAttr::constant("checked");
|
||||
pub const cite: HtmlAttr = HtmlAttr::constant("cite");
|
||||
pub const class: HtmlAttr = HtmlAttr::constant("class");
|
||||
pub const closedby: HtmlAttr = HtmlAttr::constant("closedby");
|
||||
pub const color: HtmlAttr = HtmlAttr::constant("color");
|
||||
pub const colorspace: HtmlAttr = HtmlAttr::constant("colorspace");
|
||||
pub const cols: HtmlAttr = HtmlAttr::constant("cols");
|
||||
pub const colspan: HtmlAttr = HtmlAttr::constant("colspan");
|
||||
pub const command: HtmlAttr = HtmlAttr::constant("command");
|
||||
pub const commandfor: HtmlAttr = HtmlAttr::constant("commandfor");
|
||||
pub const content: HtmlAttr = HtmlAttr::constant("content");
|
||||
pub const contenteditable: HtmlAttr = HtmlAttr::constant("contenteditable");
|
||||
pub const controls: HtmlAttr = HtmlAttr::constant("controls");
|
||||
pub const coords: HtmlAttr = HtmlAttr::constant("coords");
|
||||
pub const crossorigin: HtmlAttr = HtmlAttr::constant("crossorigin");
|
||||
pub const data: HtmlAttr = HtmlAttr::constant("data");
|
||||
pub const datetime: HtmlAttr = HtmlAttr::constant("datetime");
|
||||
pub const decoding: HtmlAttr = HtmlAttr::constant("decoding");
|
||||
pub const default: HtmlAttr = HtmlAttr::constant("default");
|
||||
pub const defer: HtmlAttr = HtmlAttr::constant("defer");
|
||||
pub const dir: HtmlAttr = HtmlAttr::constant("dir");
|
||||
pub const dirname: HtmlAttr = HtmlAttr::constant("dirname");
|
||||
pub const disabled: HtmlAttr = HtmlAttr::constant("disabled");
|
||||
pub const download: HtmlAttr = HtmlAttr::constant("download");
|
||||
pub const draggable: HtmlAttr = HtmlAttr::constant("draggable");
|
||||
pub const enctype: HtmlAttr = HtmlAttr::constant("enctype");
|
||||
pub const enterkeyhint: HtmlAttr = HtmlAttr::constant("enterkeyhint");
|
||||
pub const fetchpriority: HtmlAttr = HtmlAttr::constant("fetchpriority");
|
||||
pub const r#for: HtmlAttr = HtmlAttr::constant("for");
|
||||
pub const form: HtmlAttr = HtmlAttr::constant("form");
|
||||
pub const formaction: HtmlAttr = HtmlAttr::constant("formaction");
|
||||
pub const formenctype: HtmlAttr = HtmlAttr::constant("formenctype");
|
||||
pub const formmethod: HtmlAttr = HtmlAttr::constant("formmethod");
|
||||
pub const formnovalidate: HtmlAttr = HtmlAttr::constant("formnovalidate");
|
||||
pub const formtarget: HtmlAttr = HtmlAttr::constant("formtarget");
|
||||
pub const headers: HtmlAttr = HtmlAttr::constant("headers");
|
||||
pub const height: HtmlAttr = HtmlAttr::constant("height");
|
||||
pub const hidden: HtmlAttr = HtmlAttr::constant("hidden");
|
||||
pub const high: HtmlAttr = HtmlAttr::constant("high");
|
||||
pub const href: HtmlAttr = HtmlAttr::constant("href");
|
||||
pub const hreflang: HtmlAttr = HtmlAttr::constant("hreflang");
|
||||
pub const http_equiv: HtmlAttr = HtmlAttr::constant("http-equiv");
|
||||
pub const id: HtmlAttr = HtmlAttr::constant("id");
|
||||
pub const imagesizes: HtmlAttr = HtmlAttr::constant("imagesizes");
|
||||
pub const imagesrcset: HtmlAttr = HtmlAttr::constant("imagesrcset");
|
||||
pub const inert: HtmlAttr = HtmlAttr::constant("inert");
|
||||
pub const inputmode: HtmlAttr = HtmlAttr::constant("inputmode");
|
||||
pub const integrity: HtmlAttr = HtmlAttr::constant("integrity");
|
||||
pub const is: HtmlAttr = HtmlAttr::constant("is");
|
||||
pub const ismap: HtmlAttr = HtmlAttr::constant("ismap");
|
||||
pub const itemid: HtmlAttr = HtmlAttr::constant("itemid");
|
||||
pub const itemprop: HtmlAttr = HtmlAttr::constant("itemprop");
|
||||
pub const itemref: HtmlAttr = HtmlAttr::constant("itemref");
|
||||
pub const itemscope: HtmlAttr = HtmlAttr::constant("itemscope");
|
||||
pub const itemtype: HtmlAttr = HtmlAttr::constant("itemtype");
|
||||
pub const kind: HtmlAttr = HtmlAttr::constant("kind");
|
||||
pub const label: HtmlAttr = HtmlAttr::constant("label");
|
||||
pub const lang: HtmlAttr = HtmlAttr::constant("lang");
|
||||
pub const list: HtmlAttr = HtmlAttr::constant("list");
|
||||
pub const loading: HtmlAttr = HtmlAttr::constant("loading");
|
||||
pub const r#loop: HtmlAttr = HtmlAttr::constant("loop");
|
||||
pub const low: HtmlAttr = HtmlAttr::constant("low");
|
||||
pub const max: HtmlAttr = HtmlAttr::constant("max");
|
||||
pub const maxlength: HtmlAttr = HtmlAttr::constant("maxlength");
|
||||
pub const media: HtmlAttr = HtmlAttr::constant("media");
|
||||
pub const method: HtmlAttr = HtmlAttr::constant("method");
|
||||
pub const min: HtmlAttr = HtmlAttr::constant("min");
|
||||
pub const minlength: HtmlAttr = HtmlAttr::constant("minlength");
|
||||
pub const multiple: HtmlAttr = HtmlAttr::constant("multiple");
|
||||
pub const muted: HtmlAttr = HtmlAttr::constant("muted");
|
||||
pub const name: HtmlAttr = HtmlAttr::constant("name");
|
||||
pub const nomodule: HtmlAttr = HtmlAttr::constant("nomodule");
|
||||
pub const nonce: HtmlAttr = HtmlAttr::constant("nonce");
|
||||
pub const novalidate: HtmlAttr = HtmlAttr::constant("novalidate");
|
||||
pub const open: HtmlAttr = HtmlAttr::constant("open");
|
||||
pub const optimum: HtmlAttr = HtmlAttr::constant("optimum");
|
||||
pub const pattern: HtmlAttr = HtmlAttr::constant("pattern");
|
||||
pub const ping: HtmlAttr = HtmlAttr::constant("ping");
|
||||
pub const placeholder: HtmlAttr = HtmlAttr::constant("placeholder");
|
||||
pub const playsinline: HtmlAttr = HtmlAttr::constant("playsinline");
|
||||
pub const popover: HtmlAttr = HtmlAttr::constant("popover");
|
||||
pub const popovertarget: HtmlAttr = HtmlAttr::constant("popovertarget");
|
||||
pub const popovertargetaction: HtmlAttr = HtmlAttr::constant("popovertargetaction");
|
||||
pub const poster: HtmlAttr = HtmlAttr::constant("poster");
|
||||
pub const preload: HtmlAttr = HtmlAttr::constant("preload");
|
||||
pub const readonly: HtmlAttr = HtmlAttr::constant("readonly");
|
||||
pub const referrerpolicy: HtmlAttr = HtmlAttr::constant("referrerpolicy");
|
||||
pub const rel: HtmlAttr = HtmlAttr::constant("rel");
|
||||
pub const required: HtmlAttr = HtmlAttr::constant("required");
|
||||
pub const reversed: HtmlAttr = HtmlAttr::constant("reversed");
|
||||
pub const role: HtmlAttr = HtmlAttr::constant("role");
|
||||
pub const rows: HtmlAttr = HtmlAttr::constant("rows");
|
||||
pub const rowspan: HtmlAttr = HtmlAttr::constant("rowspan");
|
||||
pub const sandbox: HtmlAttr = HtmlAttr::constant("sandbox");
|
||||
pub const scope: HtmlAttr = HtmlAttr::constant("scope");
|
||||
pub const selected: HtmlAttr = HtmlAttr::constant("selected");
|
||||
pub const shadowrootclonable: HtmlAttr = HtmlAttr::constant("shadowrootclonable");
|
||||
pub const shadowrootcustomelementregistry: HtmlAttr = HtmlAttr::constant("shadowrootcustomelementregistry");
|
||||
pub const shadowrootdelegatesfocus: HtmlAttr = HtmlAttr::constant("shadowrootdelegatesfocus");
|
||||
pub const shadowrootmode: HtmlAttr = HtmlAttr::constant("shadowrootmode");
|
||||
pub const shadowrootserializable: HtmlAttr = HtmlAttr::constant("shadowrootserializable");
|
||||
pub const shape: HtmlAttr = HtmlAttr::constant("shape");
|
||||
pub const size: HtmlAttr = HtmlAttr::constant("size");
|
||||
pub const sizes: HtmlAttr = HtmlAttr::constant("sizes");
|
||||
pub const slot: HtmlAttr = HtmlAttr::constant("slot");
|
||||
pub const span: HtmlAttr = HtmlAttr::constant("span");
|
||||
pub const spellcheck: HtmlAttr = HtmlAttr::constant("spellcheck");
|
||||
pub const src: HtmlAttr = HtmlAttr::constant("src");
|
||||
pub const srcdoc: HtmlAttr = HtmlAttr::constant("srcdoc");
|
||||
pub const srclang: HtmlAttr = HtmlAttr::constant("srclang");
|
||||
pub const srcset: HtmlAttr = HtmlAttr::constant("srcset");
|
||||
pub const start: HtmlAttr = HtmlAttr::constant("start");
|
||||
pub const step: HtmlAttr = HtmlAttr::constant("step");
|
||||
pub const style: HtmlAttr = HtmlAttr::constant("style");
|
||||
pub const tabindex: HtmlAttr = HtmlAttr::constant("tabindex");
|
||||
pub const target: HtmlAttr = HtmlAttr::constant("target");
|
||||
pub const title: HtmlAttr = HtmlAttr::constant("title");
|
||||
pub const translate: HtmlAttr = HtmlAttr::constant("translate");
|
||||
pub const r#type: HtmlAttr = HtmlAttr::constant("type");
|
||||
pub const usemap: HtmlAttr = HtmlAttr::constant("usemap");
|
||||
pub const value: HtmlAttr = HtmlAttr::constant("value");
|
||||
pub const width: HtmlAttr = HtmlAttr::constant("width");
|
||||
pub const wrap: HtmlAttr = HtmlAttr::constant("wrap");
|
||||
pub const writingsuggestions: HtmlAttr = HtmlAttr::constant("writingsuggestions");
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
//! HTML output.
|
||||
|
||||
mod dom;
|
||||
mod typed;
|
||||
|
||||
pub use self::dom::*;
|
||||
|
||||
@ -14,6 +15,7 @@ pub fn module() -> Module {
|
||||
html.start_category(crate::Category::Html);
|
||||
html.define_elem::<HtmlElem>();
|
||||
html.define_elem::<FrameElem>();
|
||||
self::typed::define(&mut html);
|
||||
Module::new("html", html)
|
||||
}
|
||||
|
||||
|
868
crates/typst-library/src/html/typed.rs
Normal file
868
crates/typst-library/src/html/typed.rs
Normal file
@ -0,0 +1,868 @@
|
||||
//! The typed HTML element API (e.g. `html.div`).
|
||||
//!
|
||||
//! The typed API is backed by generated data derived from the HTML
|
||||
//! specification. See [generated] and `tools/codegen`.
|
||||
|
||||
use std::fmt::Write;
|
||||
use std::num::{NonZeroI64, NonZeroU64};
|
||||
use std::sync::LazyLock;
|
||||
|
||||
use bumpalo::Bump;
|
||||
use comemo::Tracked;
|
||||
use ecow::{eco_format, eco_vec, EcoString};
|
||||
use typst_assets::html as data;
|
||||
use typst_macros::cast;
|
||||
|
||||
use crate::diag::{bail, At, Hint, HintedStrResult, SourceResult};
|
||||
use crate::engine::Engine;
|
||||
use crate::foundations::{
|
||||
Args, Array, AutoValue, CastInfo, Content, Context, Datetime, Dict, Duration,
|
||||
FromValue, IntoValue, NativeFuncData, NativeFuncPtr, NoneValue, ParamInfo,
|
||||
PositiveF64, Reflect, Scope, Str, Type, Value,
|
||||
};
|
||||
use crate::html::tag;
|
||||
use crate::html::{HtmlAttr, HtmlAttrs, HtmlElem, HtmlTag};
|
||||
use crate::layout::{Axes, Axis, Dir, Length};
|
||||
use crate::visualize::Color;
|
||||
|
||||
/// Hook up all typed HTML definitions.
|
||||
pub(super) fn define(html: &mut Scope) {
|
||||
for data in FUNCS.iter() {
|
||||
html.define_func_with_data(data);
|
||||
}
|
||||
}
|
||||
|
||||
/// Lazily created functions for all typed HTML constructors.
|
||||
static FUNCS: LazyLock<Vec<NativeFuncData>> = LazyLock::new(|| {
|
||||
// Leaking is okay here. It's not meaningfully different from having
|
||||
// memory-managed values as `FUNCS` is a static.
|
||||
let bump = Box::leak(Box::new(Bump::new()));
|
||||
data::ELEMS.iter().map(|info| create_func_data(info, bump)).collect()
|
||||
});
|
||||
|
||||
/// Creates metadata for a native HTML element constructor function.
|
||||
fn create_func_data(
|
||||
element: &'static data::ElemInfo,
|
||||
bump: &'static Bump,
|
||||
) -> NativeFuncData {
|
||||
NativeFuncData {
|
||||
function: NativeFuncPtr(bump.alloc(
|
||||
move |_: &mut Engine, _: Tracked<Context>, args: &mut Args| {
|
||||
construct(element, args)
|
||||
},
|
||||
)),
|
||||
name: element.name,
|
||||
title: {
|
||||
let title = bump.alloc_str(element.name);
|
||||
title[0..1].make_ascii_uppercase();
|
||||
title
|
||||
},
|
||||
docs: element.docs,
|
||||
keywords: &[],
|
||||
contextual: false,
|
||||
scope: LazyLock::new(&|| Scope::new()),
|
||||
params: LazyLock::new(bump.alloc(move || create_param_info(element))),
|
||||
returns: LazyLock::new(&|| CastInfo::Type(Type::of::<Content>())),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates parameter signature metadata for an element.
|
||||
fn create_param_info(element: &'static data::ElemInfo) -> Vec<ParamInfo> {
|
||||
let mut params = vec![];
|
||||
for attr in element.attributes() {
|
||||
params.push(ParamInfo {
|
||||
name: attr.name,
|
||||
docs: attr.docs,
|
||||
input: AttrType::convert(attr.ty).input(),
|
||||
default: None,
|
||||
positional: false,
|
||||
named: true,
|
||||
variadic: false,
|
||||
required: false,
|
||||
settable: false,
|
||||
});
|
||||
}
|
||||
let tag = HtmlTag::constant(element.name);
|
||||
if !tag::is_void(tag) {
|
||||
params.push(ParamInfo {
|
||||
name: "body",
|
||||
docs: "The contents of the HTML element.",
|
||||
input: CastInfo::Type(Type::of::<Content>()),
|
||||
default: None,
|
||||
positional: true,
|
||||
named: false,
|
||||
variadic: false,
|
||||
required: false,
|
||||
settable: false,
|
||||
});
|
||||
}
|
||||
params
|
||||
}
|
||||
|
||||
/// The native constructor function shared by all HTML elements.
|
||||
fn construct(element: &'static data::ElemInfo, args: &mut Args) -> SourceResult<Value> {
|
||||
let mut attrs = HtmlAttrs::default();
|
||||
let mut errors = eco_vec![];
|
||||
|
||||
args.items.retain(|item| {
|
||||
let Some(name) = &item.name else { return true };
|
||||
let Some(attr) = element.get_attr(name) else { return true };
|
||||
|
||||
let span = item.value.span;
|
||||
let value = std::mem::take(&mut item.value.v);
|
||||
let ty = AttrType::convert(attr.ty);
|
||||
match ty.cast(value).at(span) {
|
||||
Ok(Some(string)) => attrs.push(HtmlAttr::constant(attr.name), string),
|
||||
Ok(None) => {}
|
||||
Err(diags) => errors.extend(diags),
|
||||
}
|
||||
|
||||
false
|
||||
});
|
||||
|
||||
if !errors.is_empty() {
|
||||
return Err(errors);
|
||||
}
|
||||
|
||||
let tag = HtmlTag::constant(element.name);
|
||||
let mut elem = HtmlElem::new(tag);
|
||||
if !attrs.0.is_empty() {
|
||||
elem.push_attrs(attrs);
|
||||
}
|
||||
|
||||
if !tag::is_void(tag) {
|
||||
let body = args.eat::<Content>()?;
|
||||
elem.push_body(body);
|
||||
}
|
||||
|
||||
Ok(elem.into_value())
|
||||
}
|
||||
|
||||
/// A dynamic representation of an attribute's type.
|
||||
///
|
||||
/// See the documentation of [`data::Type`] for more details on variants.
|
||||
enum AttrType {
|
||||
Presence,
|
||||
Native(NativeType),
|
||||
Strings(StringsType),
|
||||
Union(UnionType),
|
||||
List(ListType),
|
||||
}
|
||||
|
||||
impl AttrType {
|
||||
/// Converts the type definition into a representation suitable for casting
|
||||
/// and reflection.
|
||||
const fn convert(ty: data::Type) -> AttrType {
|
||||
use data::Type;
|
||||
match ty {
|
||||
Type::Presence => Self::Presence,
|
||||
Type::None => Self::of::<NoneValue>(),
|
||||
Type::NoneEmpty => Self::of::<NoneEmpty>(),
|
||||
Type::NoneUndefined => Self::of::<NoneUndefined>(),
|
||||
Type::Auto => Self::of::<AutoValue>(),
|
||||
Type::TrueFalse => Self::of::<TrueFalseBool>(),
|
||||
Type::YesNo => Self::of::<YesNoBool>(),
|
||||
Type::OnOff => Self::of::<OnOffBool>(),
|
||||
Type::Int => Self::of::<i64>(),
|
||||
Type::NonNegativeInt => Self::of::<u64>(),
|
||||
Type::PositiveInt => Self::of::<NonZeroU64>(),
|
||||
Type::Float => Self::of::<f64>(),
|
||||
Type::PositiveFloat => Self::of::<PositiveF64>(),
|
||||
Type::Str => Self::of::<Str>(),
|
||||
Type::Char => Self::of::<char>(),
|
||||
Type::Datetime => Self::of::<Datetime>(),
|
||||
Type::Duration => Self::of::<Duration>(),
|
||||
Type::Color => Self::of::<Color>(),
|
||||
Type::HorizontalDir => Self::of::<HorizontalDir>(),
|
||||
Type::IconSize => Self::of::<IconSize>(),
|
||||
Type::ImageCandidate => Self::of::<ImageCandidate>(),
|
||||
Type::SourceSize => Self::of::<SourceSize>(),
|
||||
Type::Strings(start, end) => Self::Strings(StringsType { start, end }),
|
||||
Type::Union(variants) => Self::Union(UnionType(variants)),
|
||||
Type::List(inner, separator, shorthand) => {
|
||||
Self::List(ListType { inner, separator, shorthand })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Produces the dynamic representation of an attribute type backed by a
|
||||
/// native Rust type.
|
||||
const fn of<T: IntoAttr>() -> Self {
|
||||
Self::Native(NativeType::of::<T>())
|
||||
}
|
||||
|
||||
/// See [`Reflect::input`].
|
||||
fn input(&self) -> CastInfo {
|
||||
match self {
|
||||
Self::Presence => bool::input(),
|
||||
Self::Native(ty) => (ty.input)(),
|
||||
Self::Union(ty) => ty.input(),
|
||||
Self::Strings(ty) => ty.input(),
|
||||
Self::List(ty) => ty.input(),
|
||||
}
|
||||
}
|
||||
|
||||
/// See [`Reflect::castable`].
|
||||
fn castable(&self, value: &Value) -> bool {
|
||||
match self {
|
||||
Self::Presence => bool::castable(value),
|
||||
Self::Native(ty) => (ty.castable)(value),
|
||||
Self::Union(ty) => ty.castable(value),
|
||||
Self::Strings(ty) => ty.castable(value),
|
||||
Self::List(ty) => ty.castable(value),
|
||||
}
|
||||
}
|
||||
|
||||
/// Tries to cast the value into this attribute's type and serialize it into
|
||||
/// an HTML attribute string.
|
||||
fn cast(&self, value: Value) -> HintedStrResult<Option<EcoString>> {
|
||||
match self {
|
||||
Self::Presence => value.cast::<bool>().map(|b| b.then(EcoString::new)),
|
||||
Self::Native(ty) => (ty.cast)(value),
|
||||
Self::Union(ty) => ty.cast(value),
|
||||
Self::Strings(ty) => ty.cast(value),
|
||||
Self::List(ty) => ty.cast(value),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An enumeration with generated string variants.
|
||||
///
|
||||
/// `start` and `end` are used to index into `data::ATTR_STRINGS`.
|
||||
struct StringsType {
|
||||
start: usize,
|
||||
end: usize,
|
||||
}
|
||||
|
||||
impl StringsType {
|
||||
fn input(&self) -> CastInfo {
|
||||
CastInfo::Union(
|
||||
self.strings()
|
||||
.iter()
|
||||
.map(|(val, desc)| CastInfo::Value(val.into_value(), desc))
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
fn castable(&self, value: &Value) -> bool {
|
||||
match value {
|
||||
Value::Str(s) => self.strings().iter().any(|&(v, _)| v == s.as_str()),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn cast(&self, value: Value) -> HintedStrResult<Option<EcoString>> {
|
||||
if self.castable(&value) {
|
||||
value.cast().map(Some)
|
||||
} else {
|
||||
Err(self.input().error(&value))
|
||||
}
|
||||
}
|
||||
|
||||
fn strings(&self) -> &'static [(&'static str, &'static str)] {
|
||||
&data::ATTR_STRINGS[self.start..self.end]
|
||||
}
|
||||
}
|
||||
|
||||
/// A type that accepts any of the contained types.
|
||||
struct UnionType(&'static [data::Type]);
|
||||
|
||||
impl UnionType {
|
||||
fn input(&self) -> CastInfo {
|
||||
CastInfo::Union(self.iter().map(|ty| ty.input()).collect())
|
||||
}
|
||||
|
||||
fn castable(&self, value: &Value) -> bool {
|
||||
self.iter().any(|ty| ty.castable(value))
|
||||
}
|
||||
|
||||
fn cast(&self, value: Value) -> HintedStrResult<Option<EcoString>> {
|
||||
for item in self.iter() {
|
||||
if item.castable(&value) {
|
||||
return item.cast(value);
|
||||
}
|
||||
}
|
||||
Err(self.input().error(&value))
|
||||
}
|
||||
|
||||
fn iter(&self) -> impl Iterator<Item = AttrType> {
|
||||
self.0.iter().map(|&ty| AttrType::convert(ty))
|
||||
}
|
||||
}
|
||||
|
||||
/// A list of items separated by a specific separator char.
|
||||
///
|
||||
/// - <https://html.spec.whatwg.org/#space-separated-tokens>
|
||||
/// - <https://html.spec.whatwg.org/#comma-separated-tokens>
|
||||
struct ListType {
|
||||
inner: &'static data::Type,
|
||||
separator: char,
|
||||
shorthand: bool,
|
||||
}
|
||||
|
||||
impl ListType {
|
||||
fn input(&self) -> CastInfo {
|
||||
if self.shorthand {
|
||||
Array::input() + self.inner().input()
|
||||
} else {
|
||||
Array::input()
|
||||
}
|
||||
}
|
||||
|
||||
fn castable(&self, value: &Value) -> bool {
|
||||
Array::castable(value) || (self.shorthand && self.inner().castable(value))
|
||||
}
|
||||
|
||||
fn cast(&self, value: Value) -> HintedStrResult<Option<EcoString>> {
|
||||
let ty = self.inner();
|
||||
if Array::castable(&value) {
|
||||
let array = value.cast::<Array>()?;
|
||||
let mut out = EcoString::new();
|
||||
for (i, item) in array.into_iter().enumerate() {
|
||||
let item = ty.cast(item)?.unwrap();
|
||||
if item.as_str().contains(self.separator) {
|
||||
let buf;
|
||||
let name = match self.separator {
|
||||
' ' => "space",
|
||||
',' => "comma",
|
||||
_ => {
|
||||
buf = eco_format!("'{}'", self.separator);
|
||||
buf.as_str()
|
||||
}
|
||||
};
|
||||
bail!(
|
||||
"array item may not contain a {name}";
|
||||
hint: "the array attribute will be encoded as a \
|
||||
{name}-separated string"
|
||||
);
|
||||
}
|
||||
if i > 0 {
|
||||
out.push(self.separator);
|
||||
if self.separator == ',' {
|
||||
out.push(' ');
|
||||
}
|
||||
}
|
||||
out.push_str(&item);
|
||||
}
|
||||
Ok(Some(out))
|
||||
} else if self.shorthand && ty.castable(&value) {
|
||||
let item = ty.cast(value)?.unwrap();
|
||||
Ok(Some(item))
|
||||
} else {
|
||||
Err(self.input().error(&value))
|
||||
}
|
||||
}
|
||||
|
||||
fn inner(&self) -> AttrType {
|
||||
AttrType::convert(*self.inner)
|
||||
}
|
||||
}
|
||||
|
||||
/// A dynamic representation of attribute backed by a native type implementing
|
||||
/// - the standard `Reflect` and `FromValue` traits for casting from a value,
|
||||
/// - the special `IntoAttr` trait for conversion into an attribute string.
|
||||
#[derive(Copy, Clone)]
|
||||
struct NativeType {
|
||||
input: fn() -> CastInfo,
|
||||
cast: fn(Value) -> HintedStrResult<Option<EcoString>>,
|
||||
castable: fn(&Value) -> bool,
|
||||
}
|
||||
|
||||
impl NativeType {
|
||||
/// Creates a dynamic native type from a native Rust type.
|
||||
const fn of<T: IntoAttr>() -> Self {
|
||||
Self {
|
||||
cast: |value| {
|
||||
let this = value.cast::<T>()?;
|
||||
Ok(Some(this.into_attr()))
|
||||
},
|
||||
input: T::input,
|
||||
castable: T::castable,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Casts a native type into an HTML attribute.
|
||||
pub trait IntoAttr: FromValue {
|
||||
/// Turn the value into an attribute string.
|
||||
fn into_attr(self) -> EcoString;
|
||||
}
|
||||
|
||||
impl IntoAttr for Str {
|
||||
fn into_attr(self) -> EcoString {
|
||||
self.into()
|
||||
}
|
||||
}
|
||||
|
||||
/// A boolean that is encoded as a string:
|
||||
/// - `false` is encoded as `"false"`
|
||||
/// - `true` is encoded as `"true"`
|
||||
pub struct TrueFalseBool(pub bool);
|
||||
|
||||
cast! {
|
||||
TrueFalseBool,
|
||||
v: bool => Self(v),
|
||||
}
|
||||
|
||||
impl IntoAttr for TrueFalseBool {
|
||||
fn into_attr(self) -> EcoString {
|
||||
if self.0 { "true" } else { "false" }.into()
|
||||
}
|
||||
}
|
||||
|
||||
/// A boolean that is encoded as a string:
|
||||
/// - `false` is encoded as `"no"`
|
||||
/// - `true` is encoded as `"yes"`
|
||||
pub struct YesNoBool(pub bool);
|
||||
|
||||
cast! {
|
||||
YesNoBool,
|
||||
v: bool => Self(v),
|
||||
}
|
||||
|
||||
impl IntoAttr for YesNoBool {
|
||||
fn into_attr(self) -> EcoString {
|
||||
if self.0 { "yes" } else { "no" }.into()
|
||||
}
|
||||
}
|
||||
|
||||
/// A boolean that is encoded as a string:
|
||||
/// - `false` is encoded as `"off"`
|
||||
/// - `true` is encoded as `"on"`
|
||||
pub struct OnOffBool(pub bool);
|
||||
|
||||
cast! {
|
||||
OnOffBool,
|
||||
v: bool => Self(v),
|
||||
}
|
||||
|
||||
impl IntoAttr for OnOffBool {
|
||||
fn into_attr(self) -> EcoString {
|
||||
if self.0 { "on" } else { "off" }.into()
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoAttr for AutoValue {
|
||||
fn into_attr(self) -> EcoString {
|
||||
"auto".into()
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoAttr for NoneValue {
|
||||
fn into_attr(self) -> EcoString {
|
||||
"none".into()
|
||||
}
|
||||
}
|
||||
|
||||
/// A `none` value that turns into an empty string attribute.
|
||||
struct NoneEmpty;
|
||||
|
||||
cast! {
|
||||
NoneEmpty,
|
||||
_: NoneValue => NoneEmpty,
|
||||
}
|
||||
|
||||
impl IntoAttr for NoneEmpty {
|
||||
fn into_attr(self) -> EcoString {
|
||||
"".into()
|
||||
}
|
||||
}
|
||||
|
||||
/// A `none` value that turns into the string `"undefined"`.
|
||||
struct NoneUndefined;
|
||||
|
||||
cast! {
|
||||
NoneUndefined,
|
||||
_: NoneValue => NoneUndefined,
|
||||
}
|
||||
|
||||
impl IntoAttr for NoneUndefined {
|
||||
fn into_attr(self) -> EcoString {
|
||||
"undefined".into()
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoAttr for char {
|
||||
fn into_attr(self) -> EcoString {
|
||||
eco_format!("{self}")
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoAttr for i64 {
|
||||
fn into_attr(self) -> EcoString {
|
||||
eco_format!("{self}")
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoAttr for u64 {
|
||||
fn into_attr(self) -> EcoString {
|
||||
eco_format!("{self}")
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoAttr for NonZeroI64 {
|
||||
fn into_attr(self) -> EcoString {
|
||||
eco_format!("{self}")
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoAttr for NonZeroU64 {
|
||||
fn into_attr(self) -> EcoString {
|
||||
eco_format!("{self}")
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoAttr for f64 {
|
||||
fn into_attr(self) -> EcoString {
|
||||
// HTML float literal allows all the things that Rust's float `Display`
|
||||
// impl produces.
|
||||
eco_format!("{self}")
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoAttr for PositiveF64 {
|
||||
fn into_attr(self) -> EcoString {
|
||||
self.get().into_attr()
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoAttr for Color {
|
||||
fn into_attr(self) -> EcoString {
|
||||
eco_format!("{}", css::color(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoAttr for Duration {
|
||||
fn into_attr(self) -> EcoString {
|
||||
// https://html.spec.whatwg.org/#valid-duration-string
|
||||
let mut out = EcoString::new();
|
||||
macro_rules! part {
|
||||
($s:literal) => {
|
||||
if !out.is_empty() {
|
||||
out.push(' ');
|
||||
}
|
||||
write!(out, $s).unwrap();
|
||||
};
|
||||
}
|
||||
|
||||
let [weeks, days, hours, minutes, seconds] = self.decompose();
|
||||
if weeks > 0 {
|
||||
part!("{weeks}w");
|
||||
}
|
||||
if days > 0 {
|
||||
part!("{days}d");
|
||||
}
|
||||
if hours > 0 {
|
||||
part!("{hours}h");
|
||||
}
|
||||
if minutes > 0 {
|
||||
part!("{minutes}m");
|
||||
}
|
||||
if seconds > 0 || out.is_empty() {
|
||||
part!("{seconds}s");
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoAttr for Datetime {
|
||||
fn into_attr(self) -> EcoString {
|
||||
let fmt = typst_utils::display(|f| match self {
|
||||
Self::Date(date) => datetime::date(f, date),
|
||||
Self::Time(time) => datetime::time(f, time),
|
||||
Self::Datetime(datetime) => datetime::datetime(f, datetime),
|
||||
});
|
||||
eco_format!("{fmt}")
|
||||
}
|
||||
}
|
||||
|
||||
mod datetime {
|
||||
use std::fmt::{self, Formatter, Write};
|
||||
|
||||
pub fn datetime(f: &mut Formatter, datetime: time::PrimitiveDateTime) -> fmt::Result {
|
||||
// https://html.spec.whatwg.org/#valid-global-date-and-time-string
|
||||
date(f, datetime.date())?;
|
||||
f.write_char('T')?;
|
||||
time(f, datetime.time())
|
||||
}
|
||||
|
||||
pub fn date(f: &mut Formatter, date: time::Date) -> fmt::Result {
|
||||
// https://html.spec.whatwg.org/#valid-date-string
|
||||
write!(f, "{:04}-{:02}-{:02}", date.year(), date.month() as u8, date.day())
|
||||
}
|
||||
|
||||
pub fn time(f: &mut Formatter, time: time::Time) -> fmt::Result {
|
||||
// https://html.spec.whatwg.org/#valid-time-string
|
||||
write!(f, "{:02}:{:02}", time.hour(), time.minute())?;
|
||||
if time.second() > 0 {
|
||||
write!(f, ":{:02}", time.second())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// A direction on the X axis: `ltr` or `rtl`.
|
||||
pub struct HorizontalDir(Dir);
|
||||
|
||||
cast! {
|
||||
HorizontalDir,
|
||||
v: Dir => {
|
||||
if v.axis() == Axis::Y {
|
||||
bail!("direction must be horizontal");
|
||||
}
|
||||
Self(v)
|
||||
},
|
||||
}
|
||||
|
||||
impl IntoAttr for HorizontalDir {
|
||||
fn into_attr(self) -> EcoString {
|
||||
self.0.into_attr()
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoAttr for Dir {
|
||||
fn into_attr(self) -> EcoString {
|
||||
match self {
|
||||
Self::LTR => "ltr".into(),
|
||||
Self::RTL => "rtl".into(),
|
||||
Self::TTB => "ttb".into(),
|
||||
Self::BTT => "btt".into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A width/height pair for `<link rel="icon" sizes="..." />`.
|
||||
pub struct IconSize(Axes<u64>);
|
||||
|
||||
cast! {
|
||||
IconSize,
|
||||
v: Axes<u64> => Self(v),
|
||||
}
|
||||
|
||||
impl IntoAttr for IconSize {
|
||||
fn into_attr(self) -> EcoString {
|
||||
eco_format!("{}x{}", self.0.x, self.0.y)
|
||||
}
|
||||
}
|
||||
|
||||
/// <https://html.spec.whatwg.org/#image-candidate-string>
|
||||
pub struct ImageCandidate(EcoString);
|
||||
|
||||
cast! {
|
||||
ImageCandidate,
|
||||
mut v: Dict => {
|
||||
let src = v.take("src")?.cast::<EcoString>()?;
|
||||
let width: Option<NonZeroU64> =
|
||||
v.take("width").ok().map(Value::cast).transpose()?;
|
||||
let density: Option<PositiveF64> =
|
||||
v.take("density").ok().map(Value::cast).transpose()?;
|
||||
v.finish(&["src", "width", "density"])?;
|
||||
|
||||
if src.is_empty() {
|
||||
bail!("`src` must not be empty");
|
||||
} else if src.starts_with(',') || src.ends_with(',') {
|
||||
bail!("`src` must not start or end with a comma");
|
||||
}
|
||||
|
||||
let mut out = src;
|
||||
match (width, density) {
|
||||
(None, None) => {}
|
||||
(Some(width), None) => write!(out, " {width}w").unwrap(),
|
||||
(None, Some(density)) => write!(out, " {}d", density.get()).unwrap(),
|
||||
(Some(_), Some(_)) => bail!("cannot specify both `width` and `density`"),
|
||||
}
|
||||
|
||||
Self(out)
|
||||
},
|
||||
}
|
||||
|
||||
impl IntoAttr for ImageCandidate {
|
||||
fn into_attr(self) -> EcoString {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// <https://html.spec.whatwg.org/multipage/images.html#valid-source-size-list>
|
||||
pub struct SourceSize(EcoString);
|
||||
|
||||
cast! {
|
||||
SourceSize,
|
||||
mut v: Dict => {
|
||||
let condition = v.take("condition")?.cast::<EcoString>()?;
|
||||
let size = v
|
||||
.take("size")?
|
||||
.cast::<Length>()
|
||||
.hint("CSS lengths that are not expressible as Typst lengths are not yet supported")
|
||||
.hint("you can use `html.elem` to create a raw attribute")?;
|
||||
Self(eco_format!("({condition}) {}", css::length(size)))
|
||||
},
|
||||
}
|
||||
|
||||
impl IntoAttr for SourceSize {
|
||||
fn into_attr(self) -> EcoString {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Conversion from Typst data types into CSS data types.
|
||||
///
|
||||
/// This can be moved elsewhere once we start supporting more CSS stuff.
|
||||
mod css {
|
||||
use std::fmt::{self, Display};
|
||||
|
||||
use typst_utils::Numeric;
|
||||
|
||||
use crate::layout::Length;
|
||||
use crate::visualize::{Color, Hsl, LinearRgb, Oklab, Oklch, Rgb};
|
||||
|
||||
pub fn length(length: Length) -> impl Display {
|
||||
typst_utils::display(move |f| match (length.abs.is_zero(), length.em.is_zero()) {
|
||||
(false, false) => {
|
||||
write!(f, "calc({}pt + {}em)", length.abs.to_pt(), length.em.get())
|
||||
}
|
||||
(true, false) => write!(f, "{}em", length.em.get()),
|
||||
(_, true) => write!(f, "{}pt", length.abs.to_pt()),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn color(color: Color) -> impl Display {
|
||||
typst_utils::display(move |f| match color {
|
||||
Color::Rgb(_) | Color::Cmyk(_) | Color::Luma(_) => rgb(f, color.to_rgb()),
|
||||
Color::Oklab(v) => oklab(f, v),
|
||||
Color::Oklch(v) => oklch(f, v),
|
||||
Color::LinearRgb(v) => linear_rgb(f, v),
|
||||
Color::Hsl(_) | Color::Hsv(_) => hsl(f, color.to_hsl()),
|
||||
})
|
||||
}
|
||||
|
||||
fn oklab(f: &mut fmt::Formatter<'_>, v: Oklab) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"oklab({} {} {}{})",
|
||||
percent(v.l),
|
||||
number(v.a),
|
||||
number(v.b),
|
||||
alpha(v.alpha)
|
||||
)
|
||||
}
|
||||
|
||||
fn oklch(f: &mut fmt::Formatter<'_>, v: Oklch) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"oklch({} {} {}deg{})",
|
||||
percent(v.l),
|
||||
number(v.chroma),
|
||||
number(v.hue.into_degrees()),
|
||||
alpha(v.alpha)
|
||||
)
|
||||
}
|
||||
|
||||
fn rgb(f: &mut fmt::Formatter<'_>, v: Rgb) -> fmt::Result {
|
||||
if let Some(v) = rgb_to_8_bit_lossless(v) {
|
||||
let (r, g, b, a) = v.into_components();
|
||||
write!(f, "#{r:02x}{g:02x}{b:02x}")?;
|
||||
if a != u8::MAX {
|
||||
write!(f, "{a:02x}")?;
|
||||
}
|
||||
Ok(())
|
||||
} else {
|
||||
write!(
|
||||
f,
|
||||
"rgb({} {} {}{})",
|
||||
percent(v.red),
|
||||
percent(v.green),
|
||||
percent(v.blue),
|
||||
alpha(v.alpha)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts an f32 RGBA color to its 8-bit representation if the result is
|
||||
/// [very close](is_very_close) to the original.
|
||||
fn rgb_to_8_bit_lossless(
|
||||
v: Rgb,
|
||||
) -> Option<palette::rgb::Rgba<palette::encoding::Srgb, u8>> {
|
||||
let l = v.into_format::<u8, u8>();
|
||||
let h = l.into_format::<f32, f32>();
|
||||
(is_very_close(v.red, h.red)
|
||||
&& is_very_close(v.blue, h.blue)
|
||||
&& is_very_close(v.green, h.green)
|
||||
&& is_very_close(v.alpha, h.alpha))
|
||||
.then_some(l)
|
||||
}
|
||||
|
||||
fn linear_rgb(f: &mut fmt::Formatter<'_>, v: LinearRgb) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"color(srgb-linear {} {} {}{})",
|
||||
percent(v.red),
|
||||
percent(v.green),
|
||||
percent(v.blue),
|
||||
alpha(v.alpha),
|
||||
)
|
||||
}
|
||||
|
||||
fn hsl(f: &mut fmt::Formatter<'_>, v: Hsl) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"hsl({}deg {} {}{})",
|
||||
number(v.hue.into_degrees()),
|
||||
percent(v.saturation),
|
||||
percent(v.lightness),
|
||||
alpha(v.alpha),
|
||||
)
|
||||
}
|
||||
|
||||
/// Displays an alpha component if it not 1.
|
||||
fn alpha(value: f32) -> impl Display {
|
||||
typst_utils::display(move |f| {
|
||||
if !is_very_close(value, 1.0) {
|
||||
write!(f, " / {}", percent(value))?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
/// Displays a rounded percentage.
|
||||
///
|
||||
/// For a percentage, two significant digits after the comma gives us a
|
||||
/// precision of 1/10_000, which is more than 12 bits (see `is_very_close`).
|
||||
fn percent(ratio: f32) -> impl Display {
|
||||
typst_utils::display(move |f| {
|
||||
write!(f, "{}%", typst_utils::round_with_precision(ratio as f64 * 100.0, 2))
|
||||
})
|
||||
}
|
||||
|
||||
/// Rounds a number for display.
|
||||
///
|
||||
/// For a number between 0 and 1, four significant digits give us a
|
||||
/// precision of 1/10_000, which is more than 12 bits (see `is_very_close`).
|
||||
fn number(value: f32) -> impl Display {
|
||||
typst_utils::round_with_precision(value as f64, 4)
|
||||
}
|
||||
|
||||
/// Whether two component values are close enough that there is no
|
||||
/// difference when encoding them with 12-bit. 12 bit is the highest
|
||||
/// reasonable color bit depth found in the industry.
|
||||
fn is_very_close(a: f32, b: f32) -> bool {
|
||||
const MAX_BIT_DEPTH: u32 = 12;
|
||||
const EPS: f32 = 0.5 / 2_i32.pow(MAX_BIT_DEPTH) as f32;
|
||||
(a - b).abs() < EPS
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_tags_and_attr_const_internible() {
|
||||
for elem in data::ELEMS {
|
||||
let _ = HtmlTag::constant(elem.name);
|
||||
}
|
||||
for attr in data::ATTRS {
|
||||
let _ = HtmlAttr::constant(attr.name);
|
||||
}
|
||||
}
|
||||
}
|
@ -104,7 +104,7 @@ impl Show for Packed<AlignElem> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Where to [align] something along an axis.
|
||||
/// Where to align something along an axis.
|
||||
///
|
||||
/// Possible values are:
|
||||
/// - `start`: Aligns at the [start]($direction.start) of the [text
|
||||
|
@ -4,9 +4,12 @@ use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Deref, Not};
|
||||
|
||||
use typst_utils::Get;
|
||||
|
||||
use crate::diag::bail;
|
||||
use crate::foundations::{array, cast, Array, Resolve, Smart, StyleChain};
|
||||
use crate::layout::{Abs, Dir, Length, Ratio, Rel, Size};
|
||||
use crate::diag::{bail, HintedStrResult};
|
||||
use crate::foundations::{
|
||||
array, cast, Array, CastInfo, FromValue, IntoValue, Reflect, Resolve, Smart,
|
||||
StyleChain, Value,
|
||||
};
|
||||
use crate::layout::{Abs, Dir, Rel, Size};
|
||||
|
||||
/// A container with a horizontal and vertical component.
|
||||
#[derive(Default, Copy, Clone, Eq, PartialEq, Hash)]
|
||||
@ -275,40 +278,39 @@ impl BitAndAssign for Axes<bool> {
|
||||
}
|
||||
}
|
||||
|
||||
cast! {
|
||||
Axes<Rel<Length>>,
|
||||
self => array![self.x, self.y].into_value(),
|
||||
array: Array => {
|
||||
let mut iter = array.into_iter();
|
||||
match (iter.next(), iter.next(), iter.next()) {
|
||||
(Some(a), Some(b), None) => Axes::new(a.cast()?, b.cast()?),
|
||||
_ => bail!("point array must contain exactly two entries"),
|
||||
}
|
||||
},
|
||||
impl<T: Reflect> Reflect for Axes<T> {
|
||||
fn input() -> CastInfo {
|
||||
Array::input()
|
||||
}
|
||||
|
||||
fn output() -> CastInfo {
|
||||
Array::output()
|
||||
}
|
||||
|
||||
fn castable(value: &Value) -> bool {
|
||||
Array::castable(value)
|
||||
}
|
||||
}
|
||||
|
||||
cast! {
|
||||
Axes<Ratio>,
|
||||
self => array![self.x, self.y].into_value(),
|
||||
array: Array => {
|
||||
impl<T: FromValue> FromValue for Axes<T> {
|
||||
fn from_value(value: Value) -> HintedStrResult<Self> {
|
||||
let array = value.cast::<Array>()?;
|
||||
let mut iter = array.into_iter();
|
||||
match (iter.next(), iter.next(), iter.next()) {
|
||||
(Some(a), Some(b), None) => Axes::new(a.cast()?, b.cast()?),
|
||||
_ => bail!("ratio array must contain exactly two entries"),
|
||||
(Some(a), Some(b), None) => Ok(Axes::new(a.cast()?, b.cast()?)),
|
||||
_ => bail!(
|
||||
"array must contain exactly two items";
|
||||
hint: "the first item determines the value for the X axis \
|
||||
and the second item the value for the Y axis"
|
||||
),
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
cast! {
|
||||
Axes<Length>,
|
||||
self => array![self.x, self.y].into_value(),
|
||||
array: Array => {
|
||||
let mut iter = array.into_iter();
|
||||
match (iter.next(), iter.next(), iter.next()) {
|
||||
(Some(a), Some(b), None) => Axes::new(a.cast()?, b.cast()?),
|
||||
_ => bail!("length array must contain exactly two entries"),
|
||||
}
|
||||
},
|
||||
impl<T: IntoValue> IntoValue for Axes<T> {
|
||||
fn into_value(self) -> Value {
|
||||
array![self.x.into_value(), self.y.into_value()].into_value()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Resolve> Resolve for Axes<T> {
|
||||
|
@ -1,6 +1,6 @@
|
||||
pub mod resolve;
|
||||
|
||||
use std::num::NonZeroUsize;
|
||||
use std::num::{NonZeroU32, NonZeroUsize};
|
||||
use std::sync::Arc;
|
||||
|
||||
use comemo::Track;
|
||||
@ -468,6 +468,17 @@ pub struct GridHeader {
|
||||
#[default(true)]
|
||||
pub repeat: bool,
|
||||
|
||||
/// The level of the header. Must not be zero.
|
||||
///
|
||||
/// This allows repeating multiple headers at once. Headers with different
|
||||
/// levels can repeat together, as long as they have ascending levels.
|
||||
///
|
||||
/// Notably, when a header with a lower level starts repeating, all higher
|
||||
/// or equal level headers stop repeating (they are "replaced" by the new
|
||||
/// header).
|
||||
#[default(NonZeroU32::ONE)]
|
||||
pub level: NonZeroU32,
|
||||
|
||||
/// The cells and lines within the header.
|
||||
#[variadic]
|
||||
pub children: Vec<GridItem>,
|
||||
@ -755,7 +766,14 @@ impl Show for Packed<GridCell> {
|
||||
|
||||
impl Default for Packed<GridCell> {
|
||||
fn default() -> Self {
|
||||
Packed::new(GridCell::new(Content::default()))
|
||||
Packed::new(
|
||||
// Explicitly set colspan and rowspan to ensure they won't be
|
||||
// overridden by set rules (default cells are created after
|
||||
// colspans and rowspans are processed in the resolver)
|
||||
GridCell::new(Content::default())
|
||||
.with_colspan(NonZeroUsize::ONE)
|
||||
.with_rowspan(NonZeroUsize::ONE),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
use std::num::NonZeroUsize;
|
||||
use std::ops::Range;
|
||||
use std::num::{NonZeroU32, NonZeroUsize};
|
||||
use std::ops::{Deref, DerefMut, Range};
|
||||
use std::sync::Arc;
|
||||
|
||||
use ecow::eco_format;
|
||||
@ -48,6 +48,7 @@ pub fn grid_to_cellgrid<'a>(
|
||||
let children = elem.children.iter().map(|child| match child {
|
||||
GridChild::Header(header) => ResolvableGridChild::Header {
|
||||
repeat: header.repeat(styles),
|
||||
level: header.level(styles),
|
||||
span: header.span(),
|
||||
items: header.children.iter().map(resolve_item),
|
||||
},
|
||||
@ -101,6 +102,7 @@ pub fn table_to_cellgrid<'a>(
|
||||
let children = elem.children.iter().map(|child| match child {
|
||||
TableChild::Header(header) => ResolvableGridChild::Header {
|
||||
repeat: header.repeat(styles),
|
||||
level: header.level(styles),
|
||||
span: header.span(),
|
||||
items: header.children.iter().map(resolve_item),
|
||||
},
|
||||
@ -426,8 +428,20 @@ pub struct Line {
|
||||
/// A repeatable grid header. Starts at the first row.
|
||||
#[derive(Debug)]
|
||||
pub struct Header {
|
||||
/// The index after the last row included in this header.
|
||||
pub end: usize,
|
||||
/// The range of rows included in this header.
|
||||
pub range: Range<usize>,
|
||||
/// The header's level.
|
||||
///
|
||||
/// Higher level headers repeat together with lower level headers. If a
|
||||
/// lower level header stops repeating, all higher level headers do as
|
||||
/// well.
|
||||
pub level: u32,
|
||||
/// Whether this header cannot be repeated nor should have orphan
|
||||
/// prevention because it would be about to cease repetition, either
|
||||
/// because it is followed by headers of conflicting levels, or because
|
||||
/// it is at the end of the table (possibly followed by some footers at the
|
||||
/// end).
|
||||
pub short_lived: bool,
|
||||
}
|
||||
|
||||
/// A repeatable grid footer. Stops at the last row.
|
||||
@ -435,32 +449,56 @@ pub struct Header {
|
||||
pub struct Footer {
|
||||
/// The first row included in this footer.
|
||||
pub start: usize,
|
||||
/// The index after the last row included in this footer.
|
||||
pub end: usize,
|
||||
/// The footer's level.
|
||||
///
|
||||
/// Used similarly to header level.
|
||||
pub level: u32,
|
||||
}
|
||||
|
||||
/// A possibly repeatable grid object.
|
||||
impl Footer {
|
||||
/// The footer's range of included rows.
|
||||
#[inline]
|
||||
pub fn range(&self) -> Range<usize> {
|
||||
self.start..self.end
|
||||
}
|
||||
}
|
||||
|
||||
/// A possibly repeatable grid child (header or footer).
|
||||
///
|
||||
/// It still exists even when not repeatable, but must not have additional
|
||||
/// considerations by grid layout, other than for consistency (such as making
|
||||
/// a certain group of rows unbreakable).
|
||||
pub enum Repeatable<T> {
|
||||
Repeated(T),
|
||||
NotRepeated(T),
|
||||
pub struct Repeatable<T> {
|
||||
inner: T,
|
||||
|
||||
/// Whether the user requested the child to repeat.
|
||||
pub repeated: bool,
|
||||
}
|
||||
|
||||
impl<T> Deref for Repeatable<T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> DerefMut for Repeatable<T> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Repeatable<T> {
|
||||
/// Gets the value inside this repeatable, regardless of whether
|
||||
/// it repeats.
|
||||
pub fn unwrap(&self) -> &T {
|
||||
match self {
|
||||
Self::Repeated(repeated) => repeated,
|
||||
Self::NotRepeated(not_repeated) => not_repeated,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `Some` if the value is repeated, `None` otherwise.
|
||||
#[inline]
|
||||
pub fn as_repeated(&self) -> Option<&T> {
|
||||
match self {
|
||||
Self::Repeated(repeated) => Some(repeated),
|
||||
Self::NotRepeated(_) => None,
|
||||
if self.repeated {
|
||||
Some(&self.inner)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -617,7 +655,7 @@ impl<'a> Entry<'a> {
|
||||
|
||||
/// Any grid child, which can be either a header or an item.
|
||||
pub enum ResolvableGridChild<T: ResolvableCell, I> {
|
||||
Header { repeat: bool, span: Span, items: I },
|
||||
Header { repeat: bool, level: NonZeroU32, span: Span, items: I },
|
||||
Footer { repeat: bool, span: Span, items: I },
|
||||
Item(ResolvableGridItem<T>),
|
||||
}
|
||||
@ -638,8 +676,8 @@ pub struct CellGrid<'a> {
|
||||
/// Gutter rows are not included.
|
||||
/// Contains up to 'rows_without_gutter.len() + 1' vectors of lines.
|
||||
pub hlines: Vec<Vec<Line>>,
|
||||
/// The repeatable header of this grid.
|
||||
pub header: Option<Repeatable<Header>>,
|
||||
/// The repeatable headers of this grid.
|
||||
pub headers: Vec<Repeatable<Header>>,
|
||||
/// The repeatable footer of this grid.
|
||||
pub footer: Option<Repeatable<Footer>>,
|
||||
/// Whether this grid has gutters.
|
||||
@ -654,7 +692,7 @@ impl<'a> CellGrid<'a> {
|
||||
cells: impl IntoIterator<Item = Cell<'a>>,
|
||||
) -> Self {
|
||||
let entries = cells.into_iter().map(Entry::Cell).collect();
|
||||
Self::new_internal(tracks, gutter, vec![], vec![], None, None, entries)
|
||||
Self::new_internal(tracks, gutter, vec![], vec![], vec![], None, entries)
|
||||
}
|
||||
|
||||
/// Generates the cell grid, given the tracks and resolved entries.
|
||||
@ -663,7 +701,7 @@ impl<'a> CellGrid<'a> {
|
||||
gutter: Axes<&[Sizing]>,
|
||||
vlines: Vec<Vec<Line>>,
|
||||
hlines: Vec<Vec<Line>>,
|
||||
header: Option<Repeatable<Header>>,
|
||||
headers: Vec<Repeatable<Header>>,
|
||||
footer: Option<Repeatable<Footer>>,
|
||||
entries: Vec<Entry<'a>>,
|
||||
) -> Self {
|
||||
@ -717,7 +755,7 @@ impl<'a> CellGrid<'a> {
|
||||
entries,
|
||||
vlines,
|
||||
hlines,
|
||||
header,
|
||||
headers,
|
||||
footer,
|
||||
has_gutter,
|
||||
}
|
||||
@ -852,6 +890,11 @@ impl<'a> CellGrid<'a> {
|
||||
self.cols.len()
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn has_repeated_headers(&self) -> bool {
|
||||
self.headers.iter().any(|h| h.repeated)
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolves and positions all cells in the grid before creating it.
|
||||
@ -937,6 +980,12 @@ struct RowGroupData {
|
||||
span: Span,
|
||||
kind: RowGroupKind,
|
||||
|
||||
/// Whether this header or footer may repeat.
|
||||
repeat: bool,
|
||||
|
||||
/// Level of this header or footer.
|
||||
repeatable_level: NonZeroU32,
|
||||
|
||||
/// Start of the range of indices of hlines at the top of the row group.
|
||||
/// This is always the first index after the last hline before we started
|
||||
/// building the row group - any upcoming hlines would appear at least at
|
||||
@ -984,14 +1033,17 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
let mut pending_vlines: Vec<(Span, Line)> = vec![];
|
||||
let has_gutter = self.gutter.any(|tracks| !tracks.is_empty());
|
||||
|
||||
let mut header: Option<Header> = None;
|
||||
let mut repeat_header = false;
|
||||
let mut headers: Vec<Repeatable<Header>> = vec![];
|
||||
|
||||
// Stores where the footer is supposed to end, its span, and the
|
||||
// actual footer structure.
|
||||
let mut footer: Option<(usize, Span, Footer)> = None;
|
||||
let mut repeat_footer = false;
|
||||
|
||||
// If true, there has been at least one cell besides headers and
|
||||
// footers. When false, footers at the end are forced to not repeat.
|
||||
let mut at_least_one_cell = false;
|
||||
|
||||
// We can't just use the cell's index in the 'cells' vector to
|
||||
// determine its automatic position, since cells could have arbitrary
|
||||
// positions, so the position of a cell in 'cells' can differ from its
|
||||
@ -1008,6 +1060,11 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
// automatically-positioned cell.
|
||||
let mut auto_index: usize = 0;
|
||||
|
||||
// The next header after the latest auto-positioned cell. This is used
|
||||
// to avoid checking for collision with headers that were already
|
||||
// skipped.
|
||||
let mut next_header = 0;
|
||||
|
||||
// We have to rebuild the grid to account for fixed cell positions.
|
||||
//
|
||||
// Create at least 'children.len()' positions, since there could be at
|
||||
@ -1028,12 +1085,13 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
columns,
|
||||
&mut pending_hlines,
|
||||
&mut pending_vlines,
|
||||
&mut header,
|
||||
&mut repeat_header,
|
||||
&mut headers,
|
||||
&mut footer,
|
||||
&mut repeat_footer,
|
||||
&mut auto_index,
|
||||
&mut next_header,
|
||||
&mut resolved_cells,
|
||||
&mut at_least_one_cell,
|
||||
child,
|
||||
)?;
|
||||
}
|
||||
@ -1049,13 +1107,13 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
row_amount,
|
||||
)?;
|
||||
|
||||
let (header, footer) = self.finalize_headers_and_footers(
|
||||
let footer = self.finalize_headers_and_footers(
|
||||
has_gutter,
|
||||
header,
|
||||
repeat_header,
|
||||
&mut headers,
|
||||
footer,
|
||||
repeat_footer,
|
||||
row_amount,
|
||||
at_least_one_cell,
|
||||
)?;
|
||||
|
||||
Ok(CellGrid::new_internal(
|
||||
@ -1063,7 +1121,7 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
self.gutter,
|
||||
vlines,
|
||||
hlines,
|
||||
header,
|
||||
headers,
|
||||
footer,
|
||||
resolved_cells,
|
||||
))
|
||||
@ -1083,12 +1141,13 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
columns: usize,
|
||||
pending_hlines: &mut Vec<(Span, Line, bool)>,
|
||||
pending_vlines: &mut Vec<(Span, Line)>,
|
||||
header: &mut Option<Header>,
|
||||
repeat_header: &mut bool,
|
||||
headers: &mut Vec<Repeatable<Header>>,
|
||||
footer: &mut Option<(usize, Span, Footer)>,
|
||||
repeat_footer: &mut bool,
|
||||
auto_index: &mut usize,
|
||||
next_header: &mut usize,
|
||||
resolved_cells: &mut Vec<Option<Entry<'x>>>,
|
||||
at_least_one_cell: &mut bool,
|
||||
child: ResolvableGridChild<T, I>,
|
||||
) -> SourceResult<()>
|
||||
where
|
||||
@ -1112,7 +1171,32 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
// position than it would usually be if it would be in a non-empty
|
||||
// row, so we must step a local index inside headers and footers
|
||||
// instead, and use a separate counter outside them.
|
||||
let mut local_auto_index = *auto_index;
|
||||
let local_auto_index = if matches!(child, ResolvableGridChild::Item(_)) {
|
||||
auto_index
|
||||
} else {
|
||||
// Although 'usize' is Copy, we need to be explicit here that we
|
||||
// aren't reborrowing the original auto index but rather making a
|
||||
// mutable copy of it using 'clone'.
|
||||
&mut (*auto_index).clone()
|
||||
};
|
||||
|
||||
// NOTE: usually, if 'next_header' were to be updated inside a row
|
||||
// group (indicating a header was skipped by a cell), that would
|
||||
// indicate a collision between the row group and that header, which
|
||||
// is an error. However, the exception is for the first auto cell of
|
||||
// the row group, which may skip headers while searching for a position
|
||||
// where to begin the row group in the first place.
|
||||
//
|
||||
// Therefore, we cannot safely share the counter in the row group with
|
||||
// the counter used by auto cells outside, as it might update it in a
|
||||
// valid situation, whereas it must not, since its auto cells use a
|
||||
// different auto index counter and will have seen different headers,
|
||||
// so we copy the next header counter while inside a row group.
|
||||
let local_next_header = if matches!(child, ResolvableGridChild::Item(_)) {
|
||||
next_header
|
||||
} else {
|
||||
&mut (*next_header).clone()
|
||||
};
|
||||
|
||||
// The first row in which this table group can fit.
|
||||
//
|
||||
@ -1123,23 +1207,19 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
let mut first_available_row = 0;
|
||||
|
||||
let (header_footer_items, simple_item) = match child {
|
||||
ResolvableGridChild::Header { repeat, span, items, .. } => {
|
||||
if header.is_some() {
|
||||
bail!(span, "cannot have more than one header");
|
||||
}
|
||||
|
||||
ResolvableGridChild::Header { repeat, level, span, items, .. } => {
|
||||
row_group_data = Some(RowGroupData {
|
||||
range: None,
|
||||
span,
|
||||
kind: RowGroupKind::Header,
|
||||
repeat,
|
||||
repeatable_level: level,
|
||||
top_hlines_start: pending_hlines.len(),
|
||||
top_hlines_end: None,
|
||||
});
|
||||
|
||||
*repeat_header = repeat;
|
||||
|
||||
first_available_row =
|
||||
find_next_empty_row(resolved_cells, local_auto_index, columns);
|
||||
find_next_empty_row(resolved_cells, *local_auto_index, columns);
|
||||
|
||||
// If any cell in the header is automatically positioned,
|
||||
// have it skip to the next empty row. This is to avoid
|
||||
@ -1150,7 +1230,7 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
// latest auto-position cell, since each auto-position cell
|
||||
// always occupies the first available position after the
|
||||
// previous one. Therefore, this will be >= auto_index.
|
||||
local_auto_index = first_available_row * columns;
|
||||
*local_auto_index = first_available_row * columns;
|
||||
|
||||
(Some(items), None)
|
||||
}
|
||||
@ -1162,21 +1242,27 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
row_group_data = Some(RowGroupData {
|
||||
range: None,
|
||||
span,
|
||||
repeat,
|
||||
kind: RowGroupKind::Footer,
|
||||
repeatable_level: NonZeroU32::ONE,
|
||||
top_hlines_start: pending_hlines.len(),
|
||||
top_hlines_end: None,
|
||||
});
|
||||
|
||||
*repeat_footer = repeat;
|
||||
|
||||
first_available_row =
|
||||
find_next_empty_row(resolved_cells, local_auto_index, columns);
|
||||
find_next_empty_row(resolved_cells, *local_auto_index, columns);
|
||||
|
||||
local_auto_index = first_available_row * columns;
|
||||
*local_auto_index = first_available_row * columns;
|
||||
|
||||
(Some(items), None)
|
||||
}
|
||||
ResolvableGridChild::Item(item) => (None, Some(item)),
|
||||
ResolvableGridChild::Item(item) => {
|
||||
if matches!(item, ResolvableGridItem::Cell(_)) {
|
||||
*at_least_one_cell = true;
|
||||
}
|
||||
|
||||
(None, Some(item))
|
||||
}
|
||||
};
|
||||
|
||||
let items = header_footer_items.into_iter().flatten().chain(simple_item);
|
||||
@ -1191,7 +1277,7 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
// gutter.
|
||||
skip_auto_index_through_fully_merged_rows(
|
||||
resolved_cells,
|
||||
&mut local_auto_index,
|
||||
local_auto_index,
|
||||
columns,
|
||||
);
|
||||
|
||||
@ -1266,7 +1352,7 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
// automatically positioned cell. Same for footers.
|
||||
local_auto_index
|
||||
.checked_sub(1)
|
||||
.filter(|_| local_auto_index > first_available_row * columns)
|
||||
.filter(|_| *local_auto_index > first_available_row * columns)
|
||||
.map_or(0, |last_auto_index| last_auto_index % columns + 1)
|
||||
});
|
||||
if end.is_some_and(|end| end.get() < start) {
|
||||
@ -1295,10 +1381,11 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
cell_y,
|
||||
colspan,
|
||||
rowspan,
|
||||
header.as_ref(),
|
||||
headers,
|
||||
footer.as_ref(),
|
||||
resolved_cells,
|
||||
&mut local_auto_index,
|
||||
local_auto_index,
|
||||
local_next_header,
|
||||
first_available_row,
|
||||
columns,
|
||||
row_group_data.is_some(),
|
||||
@ -1350,7 +1437,7 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
);
|
||||
|
||||
if top_hlines_end.is_none()
|
||||
&& local_auto_index > first_available_row * columns
|
||||
&& *local_auto_index > first_available_row * columns
|
||||
{
|
||||
// Auto index was moved, so upcoming auto-pos hlines should
|
||||
// no longer appear at the top.
|
||||
@ -1437,7 +1524,7 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
None => {
|
||||
// Empty header/footer: consider the header/footer to be
|
||||
// at the next empty row after the latest auto index.
|
||||
local_auto_index = first_available_row * columns;
|
||||
*local_auto_index = first_available_row * columns;
|
||||
let group_start = first_available_row;
|
||||
let group_end = group_start + 1;
|
||||
|
||||
@ -1454,8 +1541,8 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
// 'find_next_empty_row' will skip through any existing headers
|
||||
// and footers without having to loop through them each time.
|
||||
// Cells themselves, unfortunately, still have to.
|
||||
assert!(resolved_cells[local_auto_index].is_none());
|
||||
resolved_cells[local_auto_index] =
|
||||
assert!(resolved_cells[*local_auto_index].is_none());
|
||||
resolved_cells[*local_auto_index] =
|
||||
Some(Entry::Cell(self.resolve_cell(
|
||||
T::default(),
|
||||
0,
|
||||
@ -1483,21 +1570,38 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
|
||||
match row_group.kind {
|
||||
RowGroupKind::Header => {
|
||||
if group_range.start != 0 {
|
||||
bail!(
|
||||
row_group.span,
|
||||
"header must start at the first row";
|
||||
hint: "remove any rows before the header"
|
||||
);
|
||||
}
|
||||
|
||||
*header = Some(Header {
|
||||
// Later on, we have to correct this number in case there
|
||||
let data = Header {
|
||||
// Later on, we have to correct this range in case there
|
||||
// is gutter. But only once all cells have been analyzed
|
||||
// and the header has fully expanded in the fixup loop
|
||||
// below.
|
||||
end: group_range.end,
|
||||
});
|
||||
range: group_range,
|
||||
|
||||
level: row_group.repeatable_level.get(),
|
||||
|
||||
// This can only change at a later iteration, if we
|
||||
// find a conflicting header or footer right away.
|
||||
short_lived: false,
|
||||
};
|
||||
|
||||
// Mark consecutive headers right before this one as short
|
||||
// lived if they would have a higher or equal level, as
|
||||
// then they would immediately stop repeating during
|
||||
// layout.
|
||||
let mut consecutive_header_start = data.range.start;
|
||||
for conflicting_header in
|
||||
headers.iter_mut().rev().take_while(move |h| {
|
||||
let conflicts = h.range.end == consecutive_header_start
|
||||
&& h.level >= data.level;
|
||||
|
||||
consecutive_header_start = h.range.start;
|
||||
conflicts
|
||||
})
|
||||
{
|
||||
conflicting_header.short_lived = true;
|
||||
}
|
||||
|
||||
headers.push(Repeatable { inner: data, repeated: row_group.repeat });
|
||||
}
|
||||
|
||||
RowGroupKind::Footer => {
|
||||
@ -1514,15 +1618,14 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
// before the footer might not be included as part of
|
||||
// the footer if it is contained within the header.
|
||||
start: group_range.start,
|
||||
end: group_range.end,
|
||||
level: 1,
|
||||
},
|
||||
));
|
||||
|
||||
*repeat_footer = row_group.repeat;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// The child was a single cell outside headers or footers.
|
||||
// Therefore, 'local_auto_index' for this table child was
|
||||
// simply an alias for 'auto_index', so we update it as needed.
|
||||
*auto_index = local_auto_index;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@ -1689,47 +1792,64 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
fn finalize_headers_and_footers(
|
||||
&self,
|
||||
has_gutter: bool,
|
||||
header: Option<Header>,
|
||||
repeat_header: bool,
|
||||
headers: &mut [Repeatable<Header>],
|
||||
footer: Option<(usize, Span, Footer)>,
|
||||
repeat_footer: bool,
|
||||
row_amount: usize,
|
||||
) -> SourceResult<(Option<Repeatable<Header>>, Option<Repeatable<Footer>>)> {
|
||||
let header = header
|
||||
.map(|mut header| {
|
||||
// Repeat the gutter below a header (hence why we don't
|
||||
// subtract 1 from the gutter case).
|
||||
// Don't do this if there are no rows under the header.
|
||||
if has_gutter {
|
||||
// - 'header.end' is always 'last y + 1'. The header stops
|
||||
// before that row.
|
||||
// - Therefore, '2 * header.end' will be 2 * (last y + 1),
|
||||
// which is the adjusted index of the row before which the
|
||||
// header stops, meaning it will still stop right before it
|
||||
// even with gutter thanks to the multiplication below.
|
||||
// - This means that it will span all rows up to
|
||||
// '2 * (last y + 1) - 1 = 2 * last y + 1', which equates
|
||||
// to the index of the gutter row right below the header,
|
||||
// which is what we want (that gutter spacing should be
|
||||
// repeated across pages to maintain uniformity).
|
||||
header.end *= 2;
|
||||
at_least_one_cell: bool,
|
||||
) -> SourceResult<Option<Repeatable<Footer>>> {
|
||||
// Mark consecutive headers right before the end of the table, or the
|
||||
// final footer, as short lived, given that there are no normal rows
|
||||
// after them, so repeating them is pointless.
|
||||
//
|
||||
// It is important to do this BEFORE we update header and footer ranges
|
||||
// due to gutter below as 'row_amount' doesn't consider gutter.
|
||||
//
|
||||
// TODO(subfooters): take the last footer if it is at the end and
|
||||
// backtrack through consecutive footers until the first one in the
|
||||
// sequence is found. If there is no footer at the end, there are no
|
||||
// haeders to turn short-lived.
|
||||
let mut consecutive_header_start =
|
||||
footer.as_ref().map(|(_, _, f)| f.start).unwrap_or(row_amount);
|
||||
for header_at_the_end in headers.iter_mut().rev().take_while(move |h| {
|
||||
let at_the_end = h.range.end == consecutive_header_start;
|
||||
|
||||
// If the header occupies the entire grid, ensure we don't
|
||||
// include an extra gutter row when it doesn't exist, since
|
||||
// the last row of the header is at the very bottom,
|
||||
// therefore '2 * last y + 1' is not a valid index.
|
||||
let row_amount = (2 * row_amount).saturating_sub(1);
|
||||
header.end = header.end.min(row_amount);
|
||||
}
|
||||
header
|
||||
})
|
||||
.map(|header| {
|
||||
if repeat_header {
|
||||
Repeatable::Repeated(header)
|
||||
} else {
|
||||
Repeatable::NotRepeated(header)
|
||||
}
|
||||
});
|
||||
consecutive_header_start = h.range.start;
|
||||
at_the_end
|
||||
}) {
|
||||
header_at_the_end.short_lived = true;
|
||||
}
|
||||
|
||||
// Repeat the gutter below a header (hence why we don't
|
||||
// subtract 1 from the gutter case).
|
||||
// Don't do this if there are no rows under the header.
|
||||
if has_gutter {
|
||||
for header in &mut *headers {
|
||||
// Index of first y is doubled, as each row before it
|
||||
// receives a gutter row below.
|
||||
header.range.start *= 2;
|
||||
|
||||
// - 'header.end' is always 'last y + 1'. The header stops
|
||||
// before that row.
|
||||
// - Therefore, '2 * header.end' will be 2 * (last y + 1),
|
||||
// which is the adjusted index of the row before which the
|
||||
// header stops, meaning it will still stop right before it
|
||||
// even with gutter thanks to the multiplication below.
|
||||
// - This means that it will span all rows up to
|
||||
// '2 * (last y + 1) - 1 = 2 * last y + 1', which equates
|
||||
// to the index of the gutter row right below the header,
|
||||
// which is what we want (that gutter spacing should be
|
||||
// repeated across pages to maintain uniformity).
|
||||
header.range.end *= 2;
|
||||
|
||||
// If the header occupies the entire grid, ensure we don't
|
||||
// include an extra gutter row when it doesn't exist, since
|
||||
// the last row of the header is at the very bottom,
|
||||
// therefore '2 * last y + 1' is not a valid index.
|
||||
let row_amount = (2 * row_amount).saturating_sub(1);
|
||||
header.range.end = header.range.end.min(row_amount);
|
||||
}
|
||||
}
|
||||
|
||||
let footer = footer
|
||||
.map(|(footer_end, footer_span, mut footer)| {
|
||||
@ -1737,8 +1857,17 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
bail!(footer_span, "footer must end at the last row");
|
||||
}
|
||||
|
||||
let header_end =
|
||||
header.as_ref().map(Repeatable::unwrap).map(|header| header.end);
|
||||
// TODO(subfooters): will need a global slice of headers and
|
||||
// footers for when we have multiple footers
|
||||
// Alternatively, never include the gutter in the footer's
|
||||
// range and manually add it later on layout. This would allow
|
||||
// laying out the gutter as part of both the header and footer,
|
||||
// and, if the page only has headers, the gutter row below the
|
||||
// header is automatically removed (as it becomes the last), so
|
||||
// only the gutter above the footer is kept, ensuring the same
|
||||
// gutter row isn't laid out two times in a row. When laying
|
||||
// out the footer for real, the mechanism can be disabled.
|
||||
let last_header_end = headers.last().map(|header| header.range.end);
|
||||
|
||||
if has_gutter {
|
||||
// Convert the footer's start index to post-gutter coordinates.
|
||||
@ -1747,23 +1876,38 @@ impl<'x> CellGridResolver<'_, '_, 'x> {
|
||||
// Include the gutter right before the footer, unless there is
|
||||
// none, or the gutter is already included in the header (no
|
||||
// rows between the header and the footer).
|
||||
if header_end != Some(footer.start) {
|
||||
if last_header_end != Some(footer.start) {
|
||||
footer.start = footer.start.saturating_sub(1);
|
||||
}
|
||||
|
||||
// Adapt footer end but DO NOT include the gutter below it,
|
||||
// if it exists. Calculation:
|
||||
// - Starts as 'last y + 1'.
|
||||
// - The result will be
|
||||
// 2 * (last_y + 1) - 1 = 2 * last_y + 1,
|
||||
// which is the new index of the last footer row plus one,
|
||||
// meaning we do exclude any gutter below this way.
|
||||
//
|
||||
// It also keeps us within the total amount of rows, so we
|
||||
// don't need to '.min()' later.
|
||||
footer.end = (2 * footer.end).saturating_sub(1);
|
||||
}
|
||||
|
||||
Ok(footer)
|
||||
})
|
||||
.transpose()?
|
||||
.map(|footer| {
|
||||
if repeat_footer {
|
||||
Repeatable::Repeated(footer)
|
||||
} else {
|
||||
Repeatable::NotRepeated(footer)
|
||||
// Don't repeat footers when the table only has headers and
|
||||
// footers.
|
||||
// TODO(subfooters): Switch this to marking the last N
|
||||
// consecutive footers as short lived.
|
||||
Repeatable {
|
||||
inner: footer,
|
||||
repeated: repeat_footer && at_least_one_cell,
|
||||
}
|
||||
});
|
||||
|
||||
Ok((header, footer))
|
||||
Ok(footer)
|
||||
}
|
||||
|
||||
/// Resolves the cell's fields based on grid-wide properties.
|
||||
@ -1934,28 +2078,28 @@ fn expand_row_group(
|
||||
|
||||
/// Check if a cell's fixed row would conflict with a header or footer.
|
||||
fn check_for_conflicting_cell_row(
|
||||
header: Option<&Header>,
|
||||
headers: &[Repeatable<Header>],
|
||||
footer: Option<&(usize, Span, Footer)>,
|
||||
cell_y: usize,
|
||||
rowspan: usize,
|
||||
) -> HintedStrResult<()> {
|
||||
if let Some(header) = header {
|
||||
// TODO: check start (right now zero, always satisfied)
|
||||
if cell_y < header.end {
|
||||
bail!(
|
||||
"cell would conflict with header spanning the same position";
|
||||
hint: "try moving the cell or the header"
|
||||
);
|
||||
}
|
||||
// NOTE: y + rowspan >, not >=, header.start, to check if the rowspan
|
||||
// enters the header. For example, consider a rowspan of 1: if
|
||||
// `y + 1 = header.start` holds, that means `y < header.start`, and it
|
||||
// only occupies one row (`y`), so the cell is actually not in
|
||||
// conflict.
|
||||
if headers
|
||||
.iter()
|
||||
.any(|header| cell_y < header.range.end && cell_y + rowspan > header.range.start)
|
||||
{
|
||||
bail!(
|
||||
"cell would conflict with header spanning the same position";
|
||||
hint: "try moving the cell or the header"
|
||||
);
|
||||
}
|
||||
|
||||
if let Some((footer_end, _, footer)) = footer {
|
||||
// NOTE: y + rowspan >, not >=, footer.start, to check if the rowspan
|
||||
// enters the footer. For example, consider a rowspan of 1: if
|
||||
// `y + 1 = footer.start` holds, that means `y < footer.start`, and it
|
||||
// only occupies one row (`y`), so the cell is actually not in
|
||||
// conflict.
|
||||
if cell_y < *footer_end && cell_y + rowspan > footer.start {
|
||||
if let Some((_, _, footer)) = footer {
|
||||
if cell_y < footer.end && cell_y + rowspan > footer.start {
|
||||
bail!(
|
||||
"cell would conflict with footer spanning the same position";
|
||||
hint: "try reducing the cell's rowspan or moving the footer"
|
||||
@ -1981,10 +2125,11 @@ fn resolve_cell_position(
|
||||
cell_y: Smart<usize>,
|
||||
colspan: usize,
|
||||
rowspan: usize,
|
||||
header: Option<&Header>,
|
||||
headers: &[Repeatable<Header>],
|
||||
footer: Option<&(usize, Span, Footer)>,
|
||||
resolved_cells: &[Option<Entry>],
|
||||
auto_index: &mut usize,
|
||||
next_header: &mut usize,
|
||||
first_available_row: usize,
|
||||
columns: usize,
|
||||
in_row_group: bool,
|
||||
@ -2005,12 +2150,14 @@ fn resolve_cell_position(
|
||||
// Note that the counter ignores any cells with fixed positions,
|
||||
// but automatically-positioned cells will avoid conflicts by
|
||||
// simply skipping existing cells, headers and footers.
|
||||
let resolved_index = find_next_available_position::<false>(
|
||||
header,
|
||||
let resolved_index = find_next_available_position(
|
||||
headers,
|
||||
footer,
|
||||
resolved_cells,
|
||||
columns,
|
||||
*auto_index,
|
||||
next_header,
|
||||
false,
|
||||
)?;
|
||||
|
||||
// Ensure the next cell with automatic position will be
|
||||
@ -2046,7 +2193,7 @@ fn resolve_cell_position(
|
||||
// footer (but only if it isn't already in one, otherwise there
|
||||
// will already be a separate check).
|
||||
if !in_row_group {
|
||||
check_for_conflicting_cell_row(header, footer, cell_y, rowspan)?;
|
||||
check_for_conflicting_cell_row(headers, footer, cell_y, rowspan)?;
|
||||
}
|
||||
|
||||
cell_index(cell_x, cell_y)
|
||||
@ -2063,12 +2210,28 @@ fn resolve_cell_position(
|
||||
// requested column ('Some(None)') or an out of bounds position
|
||||
// ('None'), in which case we'd create a new row to place this
|
||||
// cell in.
|
||||
find_next_available_position::<true>(
|
||||
header,
|
||||
find_next_available_position(
|
||||
headers,
|
||||
footer,
|
||||
resolved_cells,
|
||||
columns,
|
||||
initial_index,
|
||||
// Make our own copy of the 'next_header' counter, since it
|
||||
// should only be updated by auto cells. However, we cannot
|
||||
// start with the same value as we are searching from the
|
||||
// start, and not from 'auto_index', so auto cells might
|
||||
// have skipped some headers already which this cell will
|
||||
// also need to skip.
|
||||
//
|
||||
// We could, in theory, keep a separate 'next_header'
|
||||
// counter for cells with fixed columns. But then we would
|
||||
// need one for every column, and much like how there isn't
|
||||
// an index counter for each column either, the potential
|
||||
// speed gain seems less relevant for a less used feature.
|
||||
// Still, it is something to consider for the future if
|
||||
// this turns out to be a bottleneck in important cases.
|
||||
&mut 0,
|
||||
true,
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -2078,7 +2241,7 @@ fn resolve_cell_position(
|
||||
// footer (but only if it isn't already in one, otherwise there
|
||||
// will already be a separate check).
|
||||
if !in_row_group {
|
||||
check_for_conflicting_cell_row(header, footer, cell_y, rowspan)?;
|
||||
check_for_conflicting_cell_row(headers, footer, cell_y, rowspan)?;
|
||||
}
|
||||
|
||||
// Let's find the first column which has that row available.
|
||||
@ -2110,13 +2273,18 @@ fn resolve_cell_position(
|
||||
/// Finds the first available position after the initial index in the resolved
|
||||
/// grid of cells. Skips any non-absent positions (positions which already
|
||||
/// have cells specified by the user) as well as any headers and footers.
|
||||
///
|
||||
/// When `skip_rows` is true, one row is skipped on each iteration, preserving
|
||||
/// the column. That is used to find a position for a fixed column cell.
|
||||
#[inline]
|
||||
fn find_next_available_position<const SKIP_ROWS: bool>(
|
||||
header: Option<&Header>,
|
||||
fn find_next_available_position(
|
||||
headers: &[Repeatable<Header>],
|
||||
footer: Option<&(usize, Span, Footer)>,
|
||||
resolved_cells: &[Option<Entry<'_>>],
|
||||
columns: usize,
|
||||
initial_index: usize,
|
||||
next_header: &mut usize,
|
||||
skip_rows: bool,
|
||||
) -> HintedStrResult<usize> {
|
||||
let mut resolved_index = initial_index;
|
||||
|
||||
@ -2126,7 +2294,7 @@ fn find_next_available_position<const SKIP_ROWS: bool>(
|
||||
// determine where this cell will be placed. An out of
|
||||
// bounds position (thus `None`) is also a valid new
|
||||
// position (only requires expanding the vector).
|
||||
if SKIP_ROWS {
|
||||
if skip_rows {
|
||||
// Skip one row at a time (cell chose its column, so we don't
|
||||
// change it).
|
||||
resolved_index =
|
||||
@ -2139,24 +2307,33 @@ fn find_next_available_position<const SKIP_ROWS: bool>(
|
||||
// would become impractically large before this overflows.
|
||||
resolved_index += 1;
|
||||
}
|
||||
} else if let Some(header) =
|
||||
header.filter(|header| resolved_index < header.end * columns)
|
||||
} else if let Some(header) = headers
|
||||
.get(*next_header)
|
||||
.filter(|header| resolved_index >= header.range.start * columns)
|
||||
{
|
||||
// Skip header (can't place a cell inside it from outside it).
|
||||
resolved_index = header.end * columns;
|
||||
// No changes needed if we already passed this header (which
|
||||
// also triggers this branch) - in that case, we only update the
|
||||
// counter.
|
||||
if resolved_index < header.range.end * columns {
|
||||
resolved_index = header.range.end * columns;
|
||||
|
||||
if SKIP_ROWS {
|
||||
// Ensure the cell's chosen column is kept after the
|
||||
// header.
|
||||
resolved_index += initial_index % columns;
|
||||
if skip_rows {
|
||||
// Ensure the cell's chosen column is kept after the
|
||||
// header.
|
||||
resolved_index += initial_index % columns;
|
||||
}
|
||||
}
|
||||
|
||||
// From now on, only check the headers afterwards.
|
||||
*next_header += 1;
|
||||
} else if let Some((footer_end, _, _)) = footer.filter(|(end, _, footer)| {
|
||||
resolved_index >= footer.start * columns && resolved_index < *end * columns
|
||||
}) {
|
||||
// Skip footer, for the same reason.
|
||||
resolved_index = *footer_end * columns;
|
||||
|
||||
if SKIP_ROWS {
|
||||
if skip_rows {
|
||||
resolved_index += initial_index % columns;
|
||||
}
|
||||
} else {
|
||||
|
@ -148,7 +148,7 @@ pub struct Library {
|
||||
/// The default style properties (for page size, font selection, and
|
||||
/// everything else configurable via set and show rules).
|
||||
pub styles: Styles,
|
||||
/// The standard library as a value. Used to provide the `std` variable.
|
||||
/// The standard library as a value. Used to provide the `std` module.
|
||||
pub std: Binding,
|
||||
/// In-development features that were enabled.
|
||||
pub features: Features,
|
||||
|
@ -23,8 +23,8 @@ pub fn cbor(
|
||||
/// A [path]($syntax/#paths) to a CBOR file or raw CBOR bytes.
|
||||
source: Spanned<DataSource>,
|
||||
) -> SourceResult<Value> {
|
||||
let data = source.load(engine.world)?;
|
||||
ciborium::from_reader(data.as_slice())
|
||||
let loaded = source.load(engine.world)?;
|
||||
ciborium::from_reader(loaded.data.as_slice())
|
||||
.map_err(|err| eco_format!("failed to parse CBOR ({err})"))
|
||||
.at(source.span)
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use ecow::{eco_format, EcoString};
|
||||
use az::SaturatingAs;
|
||||
use typst_syntax::Spanned;
|
||||
|
||||
use crate::diag::{bail, At, SourceResult};
|
||||
use crate::diag::{bail, LineCol, LoadError, LoadedWithin, ReportPos, SourceResult};
|
||||
use crate::engine::Engine;
|
||||
use crate::foundations::{cast, func, scope, Array, Dict, IntoValue, Type, Value};
|
||||
use crate::loading::{DataSource, Load, Readable};
|
||||
@ -44,7 +44,7 @@ pub fn csv(
|
||||
#[default(RowType::Array)]
|
||||
row_type: RowType,
|
||||
) -> SourceResult<Array> {
|
||||
let data = source.load(engine.world)?;
|
||||
let loaded = source.load(engine.world)?;
|
||||
|
||||
let mut builder = ::csv::ReaderBuilder::new();
|
||||
let has_headers = row_type == RowType::Dict;
|
||||
@ -53,7 +53,7 @@ pub fn csv(
|
||||
|
||||
// Counting lines from 1 by default.
|
||||
let mut line_offset: usize = 1;
|
||||
let mut reader = builder.from_reader(data.as_slice());
|
||||
let mut reader = builder.from_reader(loaded.data.as_slice());
|
||||
let mut headers: Option<::csv::StringRecord> = None;
|
||||
|
||||
if has_headers {
|
||||
@ -62,9 +62,9 @@ pub fn csv(
|
||||
headers = Some(
|
||||
reader
|
||||
.headers()
|
||||
.cloned()
|
||||
.map_err(|err| format_csv_error(err, 1))
|
||||
.at(source.span)?
|
||||
.clone(),
|
||||
.within(&loaded)?,
|
||||
);
|
||||
}
|
||||
|
||||
@ -74,7 +74,7 @@ pub fn csv(
|
||||
// incorrect with `has_headers` set to `false`. See issue:
|
||||
// https://github.com/BurntSushi/rust-csv/issues/184
|
||||
let line = line + line_offset;
|
||||
let row = result.map_err(|err| format_csv_error(err, line)).at(source.span)?;
|
||||
let row = result.map_err(|err| format_csv_error(err, line)).within(&loaded)?;
|
||||
let item = if let Some(headers) = &headers {
|
||||
let mut dict = Dict::new();
|
||||
for (field, value) in headers.iter().zip(&row) {
|
||||
@ -164,15 +164,23 @@ cast! {
|
||||
}
|
||||
|
||||
/// Format the user-facing CSV error message.
|
||||
fn format_csv_error(err: ::csv::Error, line: usize) -> EcoString {
|
||||
fn format_csv_error(err: ::csv::Error, line: usize) -> LoadError {
|
||||
let msg = "failed to parse CSV";
|
||||
let pos = (err.kind().position())
|
||||
.map(|pos| {
|
||||
let start = pos.byte().saturating_as();
|
||||
ReportPos::from(start..start)
|
||||
})
|
||||
.unwrap_or(LineCol::one_based(line, 1).into());
|
||||
match err.kind() {
|
||||
::csv::ErrorKind::Utf8 { .. } => "file is not valid utf-8".into(),
|
||||
::csv::ErrorKind::UnequalLengths { expected_len, len, .. } => {
|
||||
eco_format!(
|
||||
"failed to parse CSV (found {len} instead of \
|
||||
{expected_len} fields in line {line})"
|
||||
)
|
||||
::csv::ErrorKind::Utf8 { .. } => {
|
||||
LoadError::new(pos, msg, "file is not valid utf-8")
|
||||
}
|
||||
_ => eco_format!("failed to parse CSV ({err})"),
|
||||
::csv::ErrorKind::UnequalLengths { expected_len, len, .. } => {
|
||||
let err =
|
||||
format!("found {len} instead of {expected_len} fields in line {line}");
|
||||
LoadError::new(pos, msg, err)
|
||||
}
|
||||
_ => LoadError::new(pos, "failed to parse CSV", err),
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use ecow::eco_format;
|
||||
use typst_syntax::Spanned;
|
||||
|
||||
use crate::diag::{At, SourceResult};
|
||||
use crate::diag::{At, LineCol, LoadError, LoadedWithin, SourceResult};
|
||||
use crate::engine::Engine;
|
||||
use crate::foundations::{func, scope, Str, Value};
|
||||
use crate::loading::{DataSource, Load, Readable};
|
||||
@ -54,10 +54,13 @@ pub fn json(
|
||||
/// A [path]($syntax/#paths) to a JSON file or raw JSON bytes.
|
||||
source: Spanned<DataSource>,
|
||||
) -> SourceResult<Value> {
|
||||
let data = source.load(engine.world)?;
|
||||
serde_json::from_slice(data.as_slice())
|
||||
.map_err(|err| eco_format!("failed to parse JSON ({err})"))
|
||||
.at(source.span)
|
||||
let loaded = source.load(engine.world)?;
|
||||
serde_json::from_slice(loaded.data.as_slice())
|
||||
.map_err(|err| {
|
||||
let pos = LineCol::one_based(err.line(), err.column());
|
||||
LoadError::new(pos, "failed to parse JSON", err)
|
||||
})
|
||||
.within(&loaded)
|
||||
}
|
||||
|
||||
#[scope]
|
||||
|
@ -17,7 +17,7 @@ mod yaml_;
|
||||
|
||||
use comemo::Tracked;
|
||||
use ecow::EcoString;
|
||||
use typst_syntax::Spanned;
|
||||
use typst_syntax::{FileId, Spanned};
|
||||
|
||||
pub use self::cbor_::*;
|
||||
pub use self::csv_::*;
|
||||
@ -74,39 +74,44 @@ pub trait Load {
|
||||
}
|
||||
|
||||
impl Load for Spanned<DataSource> {
|
||||
type Output = Bytes;
|
||||
type Output = Loaded;
|
||||
|
||||
fn load(&self, world: Tracked<dyn World + '_>) -> SourceResult<Bytes> {
|
||||
fn load(&self, world: Tracked<dyn World + '_>) -> SourceResult<Self::Output> {
|
||||
self.as_ref().load(world)
|
||||
}
|
||||
}
|
||||
|
||||
impl Load for Spanned<&DataSource> {
|
||||
type Output = Bytes;
|
||||
type Output = Loaded;
|
||||
|
||||
fn load(&self, world: Tracked<dyn World + '_>) -> SourceResult<Bytes> {
|
||||
fn load(&self, world: Tracked<dyn World + '_>) -> SourceResult<Self::Output> {
|
||||
match &self.v {
|
||||
DataSource::Path(path) => {
|
||||
let file_id = self.span.resolve_path(path).at(self.span)?;
|
||||
world.file(file_id).at(self.span)
|
||||
let data = world.file(file_id).at(self.span)?;
|
||||
let source = Spanned::new(LoadSource::Path(file_id), self.span);
|
||||
Ok(Loaded::new(source, data))
|
||||
}
|
||||
DataSource::Bytes(data) => {
|
||||
let source = Spanned::new(LoadSource::Bytes, self.span);
|
||||
Ok(Loaded::new(source, data.clone()))
|
||||
}
|
||||
DataSource::Bytes(bytes) => Ok(bytes.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Load for Spanned<OneOrMultiple<DataSource>> {
|
||||
type Output = Vec<Bytes>;
|
||||
type Output = Vec<Loaded>;
|
||||
|
||||
fn load(&self, world: Tracked<dyn World + '_>) -> SourceResult<Vec<Bytes>> {
|
||||
fn load(&self, world: Tracked<dyn World + '_>) -> SourceResult<Self::Output> {
|
||||
self.as_ref().load(world)
|
||||
}
|
||||
}
|
||||
|
||||
impl Load for Spanned<&OneOrMultiple<DataSource>> {
|
||||
type Output = Vec<Bytes>;
|
||||
type Output = Vec<Loaded>;
|
||||
|
||||
fn load(&self, world: Tracked<dyn World + '_>) -> SourceResult<Vec<Bytes>> {
|
||||
fn load(&self, world: Tracked<dyn World + '_>) -> SourceResult<Self::Output> {
|
||||
self.v
|
||||
.0
|
||||
.iter()
|
||||
@ -115,6 +120,28 @@ impl Load for Spanned<&OneOrMultiple<DataSource>> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Data loaded from a [`DataSource`].
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct Loaded {
|
||||
/// Details about where `data` was loaded from.
|
||||
pub source: Spanned<LoadSource>,
|
||||
/// The loaded data.
|
||||
pub data: Bytes,
|
||||
}
|
||||
|
||||
impl Loaded {
|
||||
pub fn new(source: Spanned<LoadSource>, bytes: Bytes) -> Self {
|
||||
Self { source, data: bytes }
|
||||
}
|
||||
}
|
||||
|
||||
/// A loaded [`DataSource`].
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum LoadSource {
|
||||
Path(FileId),
|
||||
Bytes,
|
||||
}
|
||||
|
||||
/// A value that can be read from a file.
|
||||
#[derive(Debug, Clone, PartialEq, Hash)]
|
||||
pub enum Readable {
|
||||
|
@ -1,11 +1,10 @@
|
||||
use ecow::EcoString;
|
||||
use typst_syntax::Spanned;
|
||||
|
||||
use crate::diag::{At, FileError, SourceResult};
|
||||
use crate::diag::{LoadedWithin, SourceResult};
|
||||
use crate::engine::Engine;
|
||||
use crate::foundations::{func, Cast};
|
||||
use crate::loading::Readable;
|
||||
use crate::World;
|
||||
use crate::loading::{DataSource, Load, Readable};
|
||||
|
||||
/// Reads plain text or data from a file.
|
||||
///
|
||||
@ -36,14 +35,10 @@ pub fn read(
|
||||
#[default(Some(Encoding::Utf8))]
|
||||
encoding: Option<Encoding>,
|
||||
) -> SourceResult<Readable> {
|
||||
let Spanned { v: path, span } = path;
|
||||
let id = span.resolve_path(&path).at(span)?;
|
||||
let data = engine.world.file(id).at(span)?;
|
||||
let loaded = path.map(DataSource::Path).load(engine.world)?;
|
||||
Ok(match encoding {
|
||||
None => Readable::Bytes(data),
|
||||
Some(Encoding::Utf8) => {
|
||||
Readable::Str(data.to_str().map_err(FileError::from).at(span)?)
|
||||
}
|
||||
None => Readable::Bytes(loaded.data),
|
||||
Some(Encoding::Utf8) => Readable::Str(loaded.data.to_str().within(&loaded)?),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
use ecow::{eco_format, EcoString};
|
||||
use typst_syntax::{is_newline, Spanned};
|
||||
use ecow::eco_format;
|
||||
use typst_syntax::Spanned;
|
||||
|
||||
use crate::diag::{At, FileError, SourceResult};
|
||||
use crate::diag::{At, LoadError, LoadedWithin, ReportPos, SourceResult};
|
||||
use crate::engine::Engine;
|
||||
use crate::foundations::{func, scope, Str, Value};
|
||||
use crate::loading::{DataSource, Load, Readable};
|
||||
@ -32,11 +32,9 @@ pub fn toml(
|
||||
/// A [path]($syntax/#paths) to a TOML file or raw TOML bytes.
|
||||
source: Spanned<DataSource>,
|
||||
) -> SourceResult<Value> {
|
||||
let data = source.load(engine.world)?;
|
||||
let raw = data.as_str().map_err(FileError::from).at(source.span)?;
|
||||
::toml::from_str(raw)
|
||||
.map_err(|err| format_toml_error(err, raw))
|
||||
.at(source.span)
|
||||
let loaded = source.load(engine.world)?;
|
||||
let raw = loaded.data.as_str().within(&loaded)?;
|
||||
::toml::from_str(raw).map_err(format_toml_error).within(&loaded)
|
||||
}
|
||||
|
||||
#[scope]
|
||||
@ -71,15 +69,7 @@ impl toml {
|
||||
}
|
||||
|
||||
/// Format the user-facing TOML error message.
|
||||
fn format_toml_error(error: ::toml::de::Error, raw: &str) -> EcoString {
|
||||
if let Some(head) = error.span().and_then(|range| raw.get(..range.start)) {
|
||||
let line = head.lines().count();
|
||||
let column = 1 + head.chars().rev().take_while(|&c| !is_newline(c)).count();
|
||||
eco_format!(
|
||||
"failed to parse TOML ({} at line {line} column {column})",
|
||||
error.message(),
|
||||
)
|
||||
} else {
|
||||
eco_format!("failed to parse TOML ({})", error.message())
|
||||
}
|
||||
fn format_toml_error(error: ::toml::de::Error) -> LoadError {
|
||||
let pos = error.span().map(ReportPos::from).unwrap_or_default();
|
||||
LoadError::new(pos, "failed to parse TOML", error.message())
|
||||
}
|
||||
|
@ -1,8 +1,7 @@
|
||||
use ecow::EcoString;
|
||||
use roxmltree::ParsingOptions;
|
||||
use typst_syntax::Spanned;
|
||||
|
||||
use crate::diag::{format_xml_like_error, At, FileError, SourceResult};
|
||||
use crate::diag::{format_xml_like_error, LoadError, LoadedWithin, SourceResult};
|
||||
use crate::engine::Engine;
|
||||
use crate::foundations::{dict, func, scope, Array, Dict, IntoValue, Str, Value};
|
||||
use crate::loading::{DataSource, Load, Readable};
|
||||
@ -61,14 +60,14 @@ pub fn xml(
|
||||
/// A [path]($syntax/#paths) to an XML file or raw XML bytes.
|
||||
source: Spanned<DataSource>,
|
||||
) -> SourceResult<Value> {
|
||||
let data = source.load(engine.world)?;
|
||||
let text = data.as_str().map_err(FileError::from).at(source.span)?;
|
||||
let loaded = source.load(engine.world)?;
|
||||
let text = loaded.data.as_str().within(&loaded)?;
|
||||
let document = roxmltree::Document::parse_with_options(
|
||||
text,
|
||||
ParsingOptions { allow_dtd: true, ..Default::default() },
|
||||
)
|
||||
.map_err(format_xml_error)
|
||||
.at(source.span)?;
|
||||
.within(&loaded)?;
|
||||
Ok(convert_xml(document.root()))
|
||||
}
|
||||
|
||||
@ -111,6 +110,6 @@ fn convert_xml(node: roxmltree::Node) -> Value {
|
||||
}
|
||||
|
||||
/// Format the user-facing XML error message.
|
||||
fn format_xml_error(error: roxmltree::Error) -> EcoString {
|
||||
fn format_xml_error(error: roxmltree::Error) -> LoadError {
|
||||
format_xml_like_error("XML", error)
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use ecow::eco_format;
|
||||
use typst_syntax::Spanned;
|
||||
|
||||
use crate::diag::{At, SourceResult};
|
||||
use crate::diag::{At, LineCol, LoadError, LoadedWithin, ReportPos, SourceResult};
|
||||
use crate::engine::Engine;
|
||||
use crate::foundations::{func, scope, Str, Value};
|
||||
use crate::loading::{DataSource, Load, Readable};
|
||||
@ -44,10 +44,10 @@ pub fn yaml(
|
||||
/// A [path]($syntax/#paths) to a YAML file or raw YAML bytes.
|
||||
source: Spanned<DataSource>,
|
||||
) -> SourceResult<Value> {
|
||||
let data = source.load(engine.world)?;
|
||||
serde_yaml::from_slice(data.as_slice())
|
||||
.map_err(|err| eco_format!("failed to parse YAML ({err})"))
|
||||
.at(source.span)
|
||||
let loaded = source.load(engine.world)?;
|
||||
serde_yaml::from_slice(loaded.data.as_slice())
|
||||
.map_err(format_yaml_error)
|
||||
.within(&loaded)
|
||||
}
|
||||
|
||||
#[scope]
|
||||
@ -76,3 +76,16 @@ impl yaml {
|
||||
.at(span)
|
||||
}
|
||||
}
|
||||
|
||||
/// Format the user-facing YAML error message.
|
||||
pub fn format_yaml_error(error: serde_yaml::Error) -> LoadError {
|
||||
let pos = error
|
||||
.location()
|
||||
.map(|loc| {
|
||||
let line_col = LineCol::one_based(loc.line(), loc.column());
|
||||
let range = loc.index()..loc.index();
|
||||
ReportPos::full(range, line_col)
|
||||
})
|
||||
.unwrap_or_default();
|
||||
LoadError::new(pos, "failed to parse YAML", error)
|
||||
}
|
||||
|
@ -1,3 +1,10 @@
|
||||
use std::sync::LazyLock;
|
||||
|
||||
use icu_properties::maps::CodePointMapData;
|
||||
use icu_properties::CanonicalCombiningClass;
|
||||
use icu_provider::AsDeserializingBufferProvider;
|
||||
use icu_provider_blob::BlobDataProvider;
|
||||
|
||||
use crate::diag::bail;
|
||||
use crate::foundations::{cast, elem, func, Content, NativeElement, SymbolElem};
|
||||
use crate::layout::{Length, Rel};
|
||||
@ -81,17 +88,22 @@ impl Accent {
|
||||
Self(Self::combine(c).unwrap_or(c))
|
||||
}
|
||||
|
||||
/// List of bottom accents. Currently just a list of ones included in the
|
||||
/// Unicode math class document.
|
||||
const BOTTOM: &[char] = &[
|
||||
'\u{0323}', '\u{032C}', '\u{032D}', '\u{032E}', '\u{032F}', '\u{0330}',
|
||||
'\u{0331}', '\u{0332}', '\u{0333}', '\u{033A}', '\u{20E8}', '\u{20EC}',
|
||||
'\u{20ED}', '\u{20EE}', '\u{20EF}',
|
||||
];
|
||||
|
||||
/// Whether this accent is a bottom accent or not.
|
||||
pub fn is_bottom(&self) -> bool {
|
||||
Self::BOTTOM.contains(&self.0)
|
||||
static COMBINING_CLASS_DATA: LazyLock<CodePointMapData<CanonicalCombiningClass>> =
|
||||
LazyLock::new(|| {
|
||||
icu_properties::maps::load_canonical_combining_class(
|
||||
&BlobDataProvider::try_new_from_static_blob(typst_assets::icu::ICU)
|
||||
.unwrap()
|
||||
.as_deserializing(),
|
||||
)
|
||||
.unwrap()
|
||||
});
|
||||
|
||||
matches!(
|
||||
COMBINING_CLASS_DATA.as_borrowed().get(self.0),
|
||||
CanonicalCombiningClass::Below
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -16,10 +16,13 @@ use hayagriva::{
|
||||
};
|
||||
use indexmap::IndexMap;
|
||||
use smallvec::{smallvec, SmallVec};
|
||||
use typst_syntax::{Span, Spanned};
|
||||
use typst_syntax::{Span, Spanned, SyntaxMode};
|
||||
use typst_utils::{Get, ManuallyHash, NonZeroExt, PicoStr};
|
||||
|
||||
use crate::diag::{bail, error, At, FileError, HintedStrResult, SourceResult, StrResult};
|
||||
use crate::diag::{
|
||||
bail, error, At, HintedStrResult, LoadError, LoadResult, LoadedWithin, ReportPos,
|
||||
SourceResult, StrResult,
|
||||
};
|
||||
use crate::engine::{Engine, Sink};
|
||||
use crate::foundations::{
|
||||
elem, Bytes, CastInfo, Content, Derived, FromValue, IntoValue, Label, NativeElement,
|
||||
@ -31,12 +34,12 @@ use crate::layout::{
|
||||
BlockBody, BlockElem, Em, GridCell, GridChild, GridElem, GridItem, HElem, PadElem,
|
||||
Sides, Sizing, TrackSizings,
|
||||
};
|
||||
use crate::loading::{DataSource, Load};
|
||||
use crate::loading::{format_yaml_error, DataSource, Load, LoadSource, Loaded};
|
||||
use crate::model::{
|
||||
CitationForm, CiteGroup, Destination, FootnoteElem, HeadingElem, LinkElem, ParElem,
|
||||
Url,
|
||||
};
|
||||
use crate::routines::{EvalMode, Routines};
|
||||
use crate::routines::Routines;
|
||||
use crate::text::{
|
||||
FontStyle, Lang, LocalName, Region, Smallcaps, SubElem, SuperElem, TextElem,
|
||||
WeightDelta,
|
||||
@ -48,8 +51,8 @@ use crate::World;
|
||||
/// You can create a new bibliography by calling this function with a path
|
||||
/// to a bibliography file in either one of two formats:
|
||||
///
|
||||
/// - A Hayagriva `.yml` file. Hayagriva is a new bibliography file format
|
||||
/// designed for use with Typst. Visit its
|
||||
/// - A Hayagriva `.yaml`/`.yml` file. Hayagriva is a new bibliography
|
||||
/// file format designed for use with Typst. Visit its
|
||||
/// [documentation](https://github.com/typst/hayagriva/blob/main/docs/file-format.md)
|
||||
/// for more details.
|
||||
/// - A BibLaTeX `.bib` file.
|
||||
@ -87,7 +90,7 @@ use crate::World;
|
||||
/// ```
|
||||
#[elem(Locatable, Synthesize, Show, ShowSet, LocalName)]
|
||||
pub struct BibliographyElem {
|
||||
/// One or multiple paths to or raw bytes for Hayagriva `.yml` and/or
|
||||
/// One or multiple paths to or raw bytes for Hayagriva `.yaml` and/or
|
||||
/// BibLaTeX `.bib` files.
|
||||
///
|
||||
/// This can be a:
|
||||
@ -224,7 +227,15 @@ impl Show for Packed<BibliographyElem> {
|
||||
let references = works
|
||||
.references
|
||||
.as_ref()
|
||||
.ok_or("CSL style is not suitable for bibliographies")
|
||||
.ok_or_else(|| match self.style(styles).source {
|
||||
CslSource::Named(style) => eco_format!(
|
||||
"CSL style \"{}\" is not suitable for bibliographies",
|
||||
style.display_name()
|
||||
),
|
||||
CslSource::Normal(..) => {
|
||||
"CSL style is not suitable for bibliographies".into()
|
||||
}
|
||||
})
|
||||
.at(span)?;
|
||||
|
||||
if references.iter().any(|(prefix, _)| prefix.is_some()) {
|
||||
@ -294,24 +305,21 @@ impl Bibliography {
|
||||
world: Tracked<dyn World + '_>,
|
||||
sources: Spanned<OneOrMultiple<DataSource>>,
|
||||
) -> SourceResult<Derived<OneOrMultiple<DataSource>, Self>> {
|
||||
let data = sources.load(world)?;
|
||||
let bibliography = Self::decode(&sources.v, &data).at(sources.span)?;
|
||||
let loaded = sources.load(world)?;
|
||||
let bibliography = Self::decode(&loaded)?;
|
||||
Ok(Derived::new(sources.v, bibliography))
|
||||
}
|
||||
|
||||
/// Decode a bibliography from loaded data sources.
|
||||
#[comemo::memoize]
|
||||
#[typst_macros::time(name = "load bibliography")]
|
||||
fn decode(
|
||||
sources: &OneOrMultiple<DataSource>,
|
||||
data: &[Bytes],
|
||||
) -> StrResult<Bibliography> {
|
||||
fn decode(data: &[Loaded]) -> SourceResult<Bibliography> {
|
||||
let mut map = IndexMap::new();
|
||||
let mut duplicates = Vec::<EcoString>::new();
|
||||
|
||||
// We might have multiple bib/yaml files
|
||||
for (source, data) in sources.0.iter().zip(data) {
|
||||
let library = decode_library(source, data)?;
|
||||
for d in data.iter() {
|
||||
let library = decode_library(d)?;
|
||||
for entry in library {
|
||||
match map.entry(Label::new(PicoStr::intern(entry.key()))) {
|
||||
indexmap::map::Entry::Vacant(vacant) => {
|
||||
@ -325,7 +333,11 @@ impl Bibliography {
|
||||
}
|
||||
|
||||
if !duplicates.is_empty() {
|
||||
bail!("duplicate bibliography keys: {}", duplicates.join(", "));
|
||||
// TODO: Store spans of entries for duplicate key error messages.
|
||||
// Requires hayagriva entries to store their location, which should
|
||||
// be fine, since they are 1kb anyway.
|
||||
let span = data.first().unwrap().source.span;
|
||||
bail!(span, "duplicate bibliography keys: {}", duplicates.join(", "));
|
||||
}
|
||||
|
||||
Ok(Bibliography(Arc::new(ManuallyHash::new(map, typst_utils::hash128(data)))))
|
||||
@ -351,36 +363,47 @@ impl Debug for Bibliography {
|
||||
}
|
||||
|
||||
/// Decode on library from one data source.
|
||||
fn decode_library(source: &DataSource, data: &Bytes) -> StrResult<Library> {
|
||||
let src = data.as_str().map_err(FileError::from)?;
|
||||
fn decode_library(loaded: &Loaded) -> SourceResult<Library> {
|
||||
let data = loaded.data.as_str().within(loaded)?;
|
||||
|
||||
if let DataSource::Path(path) = source {
|
||||
if let LoadSource::Path(file_id) = loaded.source.v {
|
||||
// If we got a path, use the extension to determine whether it is
|
||||
// YAML or BibLaTeX.
|
||||
let ext = Path::new(path.as_str())
|
||||
let ext = file_id
|
||||
.vpath()
|
||||
.as_rooted_path()
|
||||
.extension()
|
||||
.and_then(OsStr::to_str)
|
||||
.unwrap_or_default();
|
||||
|
||||
match ext.to_lowercase().as_str() {
|
||||
"yml" | "yaml" => hayagriva::io::from_yaml_str(src)
|
||||
.map_err(|err| eco_format!("failed to parse YAML ({err})")),
|
||||
"bib" => hayagriva::io::from_biblatex_str(src)
|
||||
.map_err(|errors| format_biblatex_error(src, Some(path), errors)),
|
||||
_ => bail!("unknown bibliography format (must be .yml/.yaml or .bib)"),
|
||||
"yml" | "yaml" => hayagriva::io::from_yaml_str(data)
|
||||
.map_err(format_yaml_error)
|
||||
.within(loaded),
|
||||
"bib" => hayagriva::io::from_biblatex_str(data)
|
||||
.map_err(format_biblatex_error)
|
||||
.within(loaded),
|
||||
_ => bail!(
|
||||
loaded.source.span,
|
||||
"unknown bibliography format (must be .yaml/.yml or .bib)"
|
||||
),
|
||||
}
|
||||
} else {
|
||||
// If we just got bytes, we need to guess. If it can be decoded as
|
||||
// hayagriva YAML, we'll use that.
|
||||
let haya_err = match hayagriva::io::from_yaml_str(src) {
|
||||
let haya_err = match hayagriva::io::from_yaml_str(data) {
|
||||
Ok(library) => return Ok(library),
|
||||
Err(err) => err,
|
||||
};
|
||||
|
||||
// If it can be decoded as BibLaTeX, we use that isntead.
|
||||
let bib_errs = match hayagriva::io::from_biblatex_str(src) {
|
||||
Ok(library) => return Ok(library),
|
||||
Err(err) => err,
|
||||
let bib_errs = match hayagriva::io::from_biblatex_str(data) {
|
||||
// If the file is almost valid yaml, but contains no `@` character
|
||||
// it will be successfully parsed as an empty BibLaTeX library,
|
||||
// since BibLaTeX does support arbitrary text outside of entries.
|
||||
Ok(library) if !library.is_empty() => return Ok(library),
|
||||
Ok(_) => None,
|
||||
Err(err) => Some(err),
|
||||
};
|
||||
|
||||
// If neither decoded correctly, check whether `:` or `{` appears
|
||||
@ -388,7 +411,7 @@ fn decode_library(source: &DataSource, data: &Bytes) -> StrResult<Library> {
|
||||
// and emit the more appropriate error.
|
||||
let mut yaml = 0;
|
||||
let mut biblatex = 0;
|
||||
for c in src.chars() {
|
||||
for c in data.chars() {
|
||||
match c {
|
||||
':' => yaml += 1,
|
||||
'{' => biblatex += 1,
|
||||
@ -396,37 +419,33 @@ fn decode_library(source: &DataSource, data: &Bytes) -> StrResult<Library> {
|
||||
}
|
||||
}
|
||||
|
||||
if yaml > biblatex {
|
||||
bail!("failed to parse YAML ({haya_err})")
|
||||
} else {
|
||||
Err(format_biblatex_error(src, None, bib_errs))
|
||||
match bib_errs {
|
||||
Some(bib_errs) if biblatex >= yaml => {
|
||||
Err(format_biblatex_error(bib_errs)).within(loaded)
|
||||
}
|
||||
_ => Err(format_yaml_error(haya_err)).within(loaded),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Format a BibLaTeX loading error.
|
||||
fn format_biblatex_error(
|
||||
src: &str,
|
||||
path: Option<&str>,
|
||||
errors: Vec<BibLaTeXError>,
|
||||
) -> EcoString {
|
||||
let Some(error) = errors.first() else {
|
||||
return match path {
|
||||
Some(path) => eco_format!("failed to parse BibLaTeX file ({path})"),
|
||||
None => eco_format!("failed to parse BibLaTeX"),
|
||||
};
|
||||
fn format_biblatex_error(errors: Vec<BibLaTeXError>) -> LoadError {
|
||||
// TODO: return multiple errors?
|
||||
let Some(error) = errors.into_iter().next() else {
|
||||
// TODO: can this even happen, should we just unwrap?
|
||||
return LoadError::new(
|
||||
ReportPos::None,
|
||||
"failed to parse BibLaTeX",
|
||||
"something went wrong",
|
||||
);
|
||||
};
|
||||
|
||||
let (span, msg) = match error {
|
||||
BibLaTeXError::Parse(error) => (&error.span, error.kind.to_string()),
|
||||
BibLaTeXError::Type(error) => (&error.span, error.kind.to_string()),
|
||||
let (range, msg) = match error {
|
||||
BibLaTeXError::Parse(error) => (error.span, error.kind.to_string()),
|
||||
BibLaTeXError::Type(error) => (error.span, error.kind.to_string()),
|
||||
};
|
||||
|
||||
let line = src.get(..span.start).unwrap_or_default().lines().count();
|
||||
match path {
|
||||
Some(path) => eco_format!("failed to parse BibLaTeX file ({path}:{line}: {msg})"),
|
||||
None => eco_format!("failed to parse BibLaTeX ({line}: {msg})"),
|
||||
}
|
||||
LoadError::new(range, "failed to parse BibLaTeX", msg)
|
||||
}
|
||||
|
||||
/// A loaded CSL style.
|
||||
@ -442,8 +461,8 @@ impl CslStyle {
|
||||
let style = match &source {
|
||||
CslSource::Named(style) => Self::from_archived(*style),
|
||||
CslSource::Normal(source) => {
|
||||
let data = Spanned::new(source, span).load(world)?;
|
||||
Self::from_data(data).at(span)?
|
||||
let loaded = Spanned::new(source, span).load(world)?;
|
||||
Self::from_data(&loaded.data).within(&loaded)?
|
||||
}
|
||||
};
|
||||
Ok(Derived::new(source, style))
|
||||
@ -464,16 +483,18 @@ impl CslStyle {
|
||||
|
||||
/// Load a CSL style from file contents.
|
||||
#[comemo::memoize]
|
||||
pub fn from_data(data: Bytes) -> StrResult<CslStyle> {
|
||||
let text = data.as_str().map_err(FileError::from)?;
|
||||
pub fn from_data(bytes: &Bytes) -> LoadResult<CslStyle> {
|
||||
let text = bytes.as_str()?;
|
||||
citationberg::IndependentStyle::from_xml(text)
|
||||
.map(|style| {
|
||||
Self(Arc::new(ManuallyHash::new(
|
||||
style,
|
||||
typst_utils::hash128(&(TypeId::of::<Bytes>(), data)),
|
||||
typst_utils::hash128(&(TypeId::of::<Bytes>(), bytes)),
|
||||
)))
|
||||
})
|
||||
.map_err(|err| eco_format!("failed to load CSL style ({err})"))
|
||||
.map_err(|err| {
|
||||
LoadError::new(ReportPos::None, "failed to load CSL style", err)
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the underlying independent style.
|
||||
@ -1003,7 +1024,7 @@ impl ElemRenderer<'_> {
|
||||
Sink::new().track_mut(),
|
||||
math,
|
||||
self.span,
|
||||
EvalMode::Math,
|
||||
SyntaxMode::Math,
|
||||
Scope::new(),
|
||||
)
|
||||
.map(Value::display)
|
||||
|
@ -225,25 +225,21 @@ pub struct OutlineElem {
|
||||
/// to just specifying `{2em}`.
|
||||
///
|
||||
/// ```example
|
||||
/// #set heading(numbering: "1.a.")
|
||||
/// >>> #show heading: none
|
||||
/// #set heading(numbering: "I-I.")
|
||||
/// #set outline(title: none)
|
||||
///
|
||||
/// #outline(
|
||||
/// title: [Contents (Automatic)],
|
||||
/// indent: auto,
|
||||
/// )
|
||||
/// #outline()
|
||||
/// #line(length: 100%)
|
||||
/// #outline(indent: 3em)
|
||||
///
|
||||
/// #outline(
|
||||
/// title: [Contents (Length)],
|
||||
/// indent: 2em,
|
||||
/// )
|
||||
///
|
||||
/// = About ACME Corp.
|
||||
/// == History
|
||||
/// === Origins
|
||||
/// #lorem(10)
|
||||
///
|
||||
/// == Products
|
||||
/// #lorem(10)
|
||||
/// = Software engineering technologies
|
||||
/// == Requirements
|
||||
/// == Tools and technologies
|
||||
/// === Code editors
|
||||
/// == Analyzing alternatives
|
||||
/// = Designing software components
|
||||
/// = Testing and integration
|
||||
/// ```
|
||||
pub indent: Smart<OutlineIndent>,
|
||||
}
|
||||
@ -450,8 +446,9 @@ impl OutlineEntry {
|
||||
/// at the same level are aligned.
|
||||
///
|
||||
/// If the outline's indent is a fixed value or a function, the prefixes are
|
||||
/// indented, but the inner contents are simply inset from the prefix by the
|
||||
/// specified `gap`, rather than aligning outline-wide.
|
||||
/// indented, but the inner contents are simply offset from the prefix by
|
||||
/// the specified `gap`, rather than aligning outline-wide. For a visual
|
||||
/// explanation, see [`outline.indent`]($outline.indent).
|
||||
#[func(contextual)]
|
||||
pub fn indented(
|
||||
&self,
|
||||
|
@ -5,7 +5,7 @@ use crate::diag::{bail, At, Hint, SourceResult};
|
||||
use crate::engine::Engine;
|
||||
use crate::foundations::{
|
||||
cast, elem, Cast, Content, Context, Func, IntoValue, Label, NativeElement, Packed,
|
||||
Show, Smart, StyleChain, Synthesize,
|
||||
Repr, Show, Smart, StyleChain, Synthesize,
|
||||
};
|
||||
use crate::introspection::{Counter, CounterKey, Locatable};
|
||||
use crate::math::EquationElem;
|
||||
@ -79,6 +79,36 @@ use crate::text::TextElem;
|
||||
/// reference: `[@intro[Chapter]]`.
|
||||
///
|
||||
/// # Customization
|
||||
/// When you only ever need to reference pages of a figure/table/heading/etc. in
|
||||
/// a document, the default `form` field value can be changed to `{"page"}` with
|
||||
/// a set rule. If you prefer a short "p." supplement over "page", the
|
||||
/// [`page.supplement`]($page.supplement) field can be used for changing this:
|
||||
///
|
||||
/// ```example
|
||||
/// #set page(
|
||||
/// numbering: "1",
|
||||
/// supplement: "p.",
|
||||
/// >>> margin: (bottom: 3em),
|
||||
/// >>> footer-descent: 1.25em,
|
||||
/// )
|
||||
/// #set ref(form: "page")
|
||||
///
|
||||
/// #figure(
|
||||
/// stack(
|
||||
/// dir: ltr,
|
||||
/// spacing: 1em,
|
||||
/// circle(),
|
||||
/// square(),
|
||||
/// ),
|
||||
/// caption: [Shapes],
|
||||
/// ) <shapes>
|
||||
///
|
||||
/// #pagebreak()
|
||||
///
|
||||
/// See @shapes for examples
|
||||
/// of different shapes.
|
||||
/// ```
|
||||
///
|
||||
/// If you write a show rule for references, you can access the referenced
|
||||
/// element through the `element` field of the reference. The `element` may
|
||||
/// be `{none}` even if it exists if Typst hasn't discovered it yet, so you
|
||||
@ -91,16 +121,13 @@ use crate::text::TextElem;
|
||||
/// #show ref: it => {
|
||||
/// let eq = math.equation
|
||||
/// let el = it.element
|
||||
/// if el != none and el.func() == eq {
|
||||
/// // Override equation references.
|
||||
/// link(el.location(),numbering(
|
||||
/// el.numbering,
|
||||
/// ..counter(eq).at(el.location())
|
||||
/// ))
|
||||
/// } else {
|
||||
/// // Other references as usual.
|
||||
/// it
|
||||
/// }
|
||||
/// // Skip all other references.
|
||||
/// if el == none or el.func() != eq { return it }
|
||||
/// // Override equation references.
|
||||
/// link(el.location(), numbering(
|
||||
/// el.numbering,
|
||||
/// ..counter(eq).at(el.location())
|
||||
/// ))
|
||||
/// }
|
||||
///
|
||||
/// = Beginnings <beginning>
|
||||
@ -229,8 +256,15 @@ impl Show for Packed<RefElem> {
|
||||
// RefForm::Normal
|
||||
|
||||
if BibliographyElem::has(engine, self.target) {
|
||||
if elem.is_ok() {
|
||||
bail!(span, "label occurs in the document and its bibliography");
|
||||
if let Ok(elem) = elem {
|
||||
bail!(
|
||||
span,
|
||||
"label `{}` occurs both in the document and its bibliography",
|
||||
self.target.repr();
|
||||
hint: "change either the {}'s label or the \
|
||||
bibliography key to resolve the ambiguity",
|
||||
elem.func().name(),
|
||||
);
|
||||
}
|
||||
|
||||
return Ok(to_citation(self, engine, styles)?.pack().spanned(span));
|
||||
|
@ -1,4 +1,4 @@
|
||||
use std::num::NonZeroUsize;
|
||||
use std::num::{NonZeroU32, NonZeroUsize};
|
||||
use std::sync::Arc;
|
||||
|
||||
use typst_utils::NonZeroExt;
|
||||
@ -292,16 +292,61 @@ fn show_cellgrid_html(grid: CellGrid, styles: StyleChain) -> Content {
|
||||
elem(tag::tr, Content::sequence(row))
|
||||
};
|
||||
|
||||
// TODO(subfooters): similarly to headers, take consecutive footers from
|
||||
// the end for 'tfoot'.
|
||||
let footer = grid.footer.map(|ft| {
|
||||
let rows = rows.drain(ft.unwrap().start..);
|
||||
let rows = rows.drain(ft.start..);
|
||||
elem(tag::tfoot, Content::sequence(rows.map(|row| tr(tag::td, row))))
|
||||
});
|
||||
let header = grid.header.map(|hd| {
|
||||
let rows = rows.drain(..hd.unwrap().end);
|
||||
elem(tag::thead, Content::sequence(rows.map(|row| tr(tag::th, row))))
|
||||
});
|
||||
|
||||
let mut body = Content::sequence(rows.into_iter().map(|row| tr(tag::td, row)));
|
||||
// Store all consecutive headers at the start in 'thead'. All remaining
|
||||
// headers are just 'th' rows across the table body.
|
||||
let mut consecutive_header_end = 0;
|
||||
let first_mid_table_header = grid
|
||||
.headers
|
||||
.iter()
|
||||
.take_while(|hd| {
|
||||
let is_consecutive = hd.range.start == consecutive_header_end;
|
||||
consecutive_header_end = hd.range.end;
|
||||
|
||||
is_consecutive
|
||||
})
|
||||
.count();
|
||||
|
||||
let (y_offset, header) = if first_mid_table_header > 0 {
|
||||
let removed_header_rows =
|
||||
grid.headers.get(first_mid_table_header - 1).unwrap().range.end;
|
||||
let rows = rows.drain(..removed_header_rows);
|
||||
|
||||
(
|
||||
removed_header_rows,
|
||||
Some(elem(tag::thead, Content::sequence(rows.map(|row| tr(tag::th, row))))),
|
||||
)
|
||||
} else {
|
||||
(0, None)
|
||||
};
|
||||
|
||||
// TODO: Consider improving accessibility properties of multi-level headers
|
||||
// inside tables in the future, e.g. indicating which columns they are
|
||||
// relative to and so on. See also:
|
||||
// https://www.w3.org/WAI/tutorials/tables/multi-level/
|
||||
let mut next_header = first_mid_table_header;
|
||||
let mut body =
|
||||
Content::sequence(rows.into_iter().enumerate().map(|(relative_y, row)| {
|
||||
let y = relative_y + y_offset;
|
||||
if let Some(current_header) =
|
||||
grid.headers.get(next_header).filter(|h| h.range.contains(&y))
|
||||
{
|
||||
if y + 1 == current_header.range.end {
|
||||
next_header += 1;
|
||||
}
|
||||
|
||||
tr(tag::th, row)
|
||||
} else {
|
||||
tr(tag::td, row)
|
||||
}
|
||||
}));
|
||||
|
||||
if header.is_some() || footer.is_some() {
|
||||
body = elem(tag::tbody, body);
|
||||
}
|
||||
@ -492,6 +537,17 @@ pub struct TableHeader {
|
||||
#[default(true)]
|
||||
pub repeat: bool,
|
||||
|
||||
/// The level of the header. Must not be zero.
|
||||
///
|
||||
/// This allows repeating multiple headers at once. Headers with different
|
||||
/// levels can repeat together, as long as they have ascending levels.
|
||||
///
|
||||
/// Notably, when a header with a lower level starts repeating, all higher
|
||||
/// or equal level headers stop repeating (they are "replaced" by the new
|
||||
/// header).
|
||||
#[default(NonZeroU32::ONE)]
|
||||
pub level: NonZeroU32,
|
||||
|
||||
/// The cells and lines within the header.
|
||||
#[variadic]
|
||||
pub children: Vec<TableItem>,
|
||||
@ -770,7 +826,14 @@ impl Show for Packed<TableCell> {
|
||||
|
||||
impl Default for Packed<TableCell> {
|
||||
fn default() -> Self {
|
||||
Packed::new(TableCell::new(Content::default()))
|
||||
Packed::new(
|
||||
// Explicitly set colspan and rowspan to ensure they won't be
|
||||
// overridden by set rules (default cells are created after
|
||||
// colspans and rowspans are processed in the resolver)
|
||||
TableCell::new(Content::default())
|
||||
.with_colspan(NonZeroUsize::ONE)
|
||||
.with_rowspan(NonZeroUsize::ONE),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -59,7 +59,7 @@ pub struct EmbedElem {
|
||||
// We can't distinguish between the two at the moment.
|
||||
#[required]
|
||||
#[parse(
|
||||
match args.find::<Bytes>()? {
|
||||
match args.eat::<Bytes>()? {
|
||||
Some(data) => data,
|
||||
None => engine.world.file(id).at(span)?,
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ use std::hash::{Hash, Hasher};
|
||||
use std::num::NonZeroUsize;
|
||||
|
||||
use comemo::{Tracked, TrackedMut};
|
||||
use typst_syntax::Span;
|
||||
use typst_syntax::{Span, SyntaxMode};
|
||||
use typst_utils::LazyHash;
|
||||
|
||||
use crate::diag::SourceResult;
|
||||
@ -58,7 +58,7 @@ routines! {
|
||||
sink: TrackedMut<Sink>,
|
||||
string: &str,
|
||||
span: Span,
|
||||
mode: EvalMode,
|
||||
mode: SyntaxMode,
|
||||
scope: Scope,
|
||||
) -> SourceResult<Value>
|
||||
|
||||
@ -312,17 +312,6 @@ routines! {
|
||||
) -> SourceResult<Fragment>
|
||||
}
|
||||
|
||||
/// In which mode to evaluate a string.
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)]
|
||||
pub enum EvalMode {
|
||||
/// Evaluate as code, as after a hash.
|
||||
Code,
|
||||
/// Evaluate as markup, like in a Typst file.
|
||||
Markup,
|
||||
/// Evaluate as math, as in an equation.
|
||||
Math,
|
||||
}
|
||||
|
||||
/// Defines what kind of realization we are performing.
|
||||
pub enum RealizationKind<'a> {
|
||||
/// This the root realization for layout. Requires a mutable reference
|
||||
|
@ -196,6 +196,8 @@ bitflags::bitflags! {
|
||||
const SERIF = 1 << 1;
|
||||
/// Font face has a MATH table
|
||||
const MATH = 1 << 2;
|
||||
/// Font face has an fvar table
|
||||
const VARIABLE = 1 << 3;
|
||||
}
|
||||
}
|
||||
|
||||
@ -275,6 +277,7 @@ impl FontInfo {
|
||||
let mut flags = FontFlags::empty();
|
||||
flags.set(FontFlags::MONOSPACE, ttf.is_monospaced());
|
||||
flags.set(FontFlags::MATH, ttf.tables().math.is_some());
|
||||
flags.set(FontFlags::VARIABLE, ttf.is_variable());
|
||||
|
||||
// Determine whether this is a serif or sans-serif font.
|
||||
if let Some(panose) = ttf
|
||||
|
@ -106,13 +106,26 @@ impl Font {
|
||||
}
|
||||
|
||||
/// Look up the horizontal advance width of a glyph.
|
||||
pub fn advance(&self, glyph: u16) -> Option<Em> {
|
||||
pub fn x_advance(&self, glyph: u16) -> Option<Em> {
|
||||
self.0
|
||||
.ttf
|
||||
.glyph_hor_advance(GlyphId(glyph))
|
||||
.map(|units| self.to_em(units))
|
||||
}
|
||||
|
||||
/// Look up the vertical advance width of a glyph.
|
||||
pub fn y_advance(&self, glyph: u16) -> Option<Em> {
|
||||
self.0
|
||||
.ttf
|
||||
.glyph_ver_advance(GlyphId(glyph))
|
||||
.map(|units| self.to_em(units))
|
||||
}
|
||||
|
||||
/// Look up the width of a space.
|
||||
pub fn space_width(&self) -> Option<Em> {
|
||||
self.0.ttf.glyph_index(' ').and_then(|id| self.x_advance(id.0))
|
||||
}
|
||||
|
||||
/// Lookup a name by id.
|
||||
pub fn find_name(&self, id: u16) -> Option<String> {
|
||||
find_name(&self.0.ttf, id)
|
||||
|
@ -35,6 +35,11 @@ impl TextItem {
|
||||
pub fn width(&self) -> Abs {
|
||||
self.glyphs.iter().map(|g| g.x_advance).sum::<Em>().at(self.size)
|
||||
}
|
||||
|
||||
/// The height of the text run.
|
||||
pub fn height(&self) -> Abs {
|
||||
self.glyphs.iter().map(|g| g.y_advance).sum::<Em>().at(self.size)
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for TextItem {
|
||||
@ -54,6 +59,10 @@ pub struct Glyph {
|
||||
pub x_advance: Em,
|
||||
/// The horizontal offset of the glyph.
|
||||
pub x_offset: Em,
|
||||
/// The advance height (Y-up) of the glyph.
|
||||
pub y_advance: Em,
|
||||
/// The vertical offset (Y-up) of the glyph.
|
||||
pub y_offset: Em,
|
||||
/// The range of the glyph in its item's text. The range's length may
|
||||
/// be more than one due to multi-byte UTF-8 encoding or ligatures.
|
||||
pub range: Range<u16>,
|
||||
@ -115,4 +124,13 @@ impl<'a> TextItemView<'a> {
|
||||
.sum::<Em>()
|
||||
.at(self.item.size)
|
||||
}
|
||||
|
||||
/// The total height of this text slice
|
||||
pub fn height(&self) -> Abs {
|
||||
self.glyphs()
|
||||
.iter()
|
||||
.map(|g| g.y_advance)
|
||||
.sum::<Em>()
|
||||
.at(self.item.size)
|
||||
}
|
||||
}
|
||||
|
@ -14,13 +14,14 @@ macro_rules! translation {
|
||||
};
|
||||
}
|
||||
|
||||
const TRANSLATIONS: [(&str, &str); 40] = [
|
||||
const TRANSLATIONS: &[(&str, &str)] = &[
|
||||
translation!("ar"),
|
||||
translation!("bg"),
|
||||
translation!("ca"),
|
||||
translation!("cs"),
|
||||
translation!("da"),
|
||||
translation!("de"),
|
||||
translation!("el"),
|
||||
translation!("en"),
|
||||
translation!("es"),
|
||||
translation!("et"),
|
||||
@ -28,8 +29,8 @@ const TRANSLATIONS: [(&str, &str); 40] = [
|
||||
translation!("fi"),
|
||||
translation!("fr"),
|
||||
translation!("gl"),
|
||||
translation!("el"),
|
||||
translation!("he"),
|
||||
translation!("hr"),
|
||||
translation!("hu"),
|
||||
translation!("id"),
|
||||
translation!("is"),
|
||||
@ -41,8 +42,8 @@ const TRANSLATIONS: [(&str, &str); 40] = [
|
||||
translation!("nl"),
|
||||
translation!("nn"),
|
||||
translation!("pl"),
|
||||
translation!("pt-PT"),
|
||||
translation!("pt"),
|
||||
translation!("pt-PT"),
|
||||
translation!("ro"),
|
||||
translation!("ru"),
|
||||
translation!("sl"),
|
||||
@ -53,8 +54,8 @@ const TRANSLATIONS: [(&str, &str); 40] = [
|
||||
translation!("tr"),
|
||||
translation!("uk"),
|
||||
translation!("vi"),
|
||||
translation!("zh-TW"),
|
||||
translation!("zh"),
|
||||
translation!("zh-TW"),
|
||||
];
|
||||
|
||||
/// An identifier for a natural language.
|
||||
@ -312,14 +313,74 @@ fn lang_str(lang: Lang, region: Option<Region>) -> EcoString {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::HashSet;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use typst_utils::option_eq;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn translation_files_iter() -> impl Iterator<Item = PathBuf> {
|
||||
std::fs::read_dir("translations")
|
||||
.unwrap()
|
||||
.map(|e| e.unwrap().path())
|
||||
.filter(|e| e.is_file() && e.extension().is_some_and(|e| e == "txt"))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_region_option_eq() {
|
||||
let region = Some(Region([b'U', b'S']));
|
||||
assert!(option_eq(region, "US"));
|
||||
assert!(!option_eq(region, "AB"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_all_translations_included() {
|
||||
let defined_keys =
|
||||
HashSet::<&str>::from_iter(TRANSLATIONS.iter().map(|(lang, _)| *lang));
|
||||
let mut checked = 0;
|
||||
for file in translation_files_iter() {
|
||||
assert!(
|
||||
defined_keys.contains(
|
||||
file.file_stem()
|
||||
.expect("translation file should have basename")
|
||||
.to_str()
|
||||
.expect("translation file name should be utf-8 encoded")
|
||||
),
|
||||
"translation from {:?} should be registered in TRANSLATIONS in {}",
|
||||
file.file_name().unwrap(),
|
||||
file!(),
|
||||
);
|
||||
checked += 1;
|
||||
}
|
||||
assert_eq!(TRANSLATIONS.len(), checked);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_all_translation_files_formatted() {
|
||||
for file in translation_files_iter() {
|
||||
let content = std::fs::read_to_string(&file)
|
||||
.expect("translation file should be in utf-8 encoding");
|
||||
let filename = file.file_name().unwrap();
|
||||
assert!(
|
||||
content.ends_with('\n'),
|
||||
"translation file {filename:?} should end with linebreak",
|
||||
);
|
||||
for line in content.lines() {
|
||||
assert_eq!(
|
||||
line.trim(),
|
||||
line,
|
||||
"line {line:?} in {filename:?} should not have extra whitespaces"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_translations_sorted() {
|
||||
assert!(
|
||||
TRANSLATIONS.is_sorted_by_key(|(lang, _)| lang),
|
||||
"TRANSLATIONS should be sorted"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -30,6 +30,7 @@ pub use self::space::*;
|
||||
|
||||
use std::fmt::{self, Debug, Formatter};
|
||||
use std::hash::Hash;
|
||||
use std::str::FromStr;
|
||||
use std::sync::LazyLock;
|
||||
|
||||
use ecow::{eco_format, EcoString};
|
||||
@ -1283,6 +1284,12 @@ pub fn features(styles: StyleChain) -> Vec<Feature> {
|
||||
feat(b"frac", 1);
|
||||
}
|
||||
|
||||
match EquationElem::size_in(styles) {
|
||||
MathSize::Script => feat(b"ssty", 1),
|
||||
MathSize::ScriptScript => feat(b"ssty", 2),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
for (tag, value) in TextElem::features_in(styles).0 {
|
||||
tags.push(Feature::new(tag, value, ..))
|
||||
}
|
||||
@ -1290,6 +1297,17 @@ pub fn features(styles: StyleChain) -> Vec<Feature> {
|
||||
tags
|
||||
}
|
||||
|
||||
/// Process the language and region of a style chain into a
|
||||
/// rustybuzz-compatible BCP 47 language.
|
||||
pub fn language(styles: StyleChain) -> rustybuzz::Language {
|
||||
let mut bcp: EcoString = TextElem::lang_in(styles).as_str().into();
|
||||
if let Some(region) = TextElem::region_in(styles) {
|
||||
bcp.push('-');
|
||||
bcp.push_str(region.as_str());
|
||||
}
|
||||
rustybuzz::Language::from_str(&bcp).unwrap()
|
||||
}
|
||||
|
||||
/// A toggle that turns on and off alternatingly if folded.
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
||||
pub struct ItalicToggle(pub bool);
|
||||
@ -1394,12 +1412,24 @@ pub fn is_default_ignorable(c: char) -> bool {
|
||||
fn check_font_list(engine: &mut Engine, list: &Spanned<FontList>) {
|
||||
let book = engine.world.book();
|
||||
for family in &list.v {
|
||||
if !book.contains_family(family.as_str()) {
|
||||
engine.sink.warn(warning!(
|
||||
match book.select_family(family.as_str()).next() {
|
||||
Some(index) => {
|
||||
if book
|
||||
.info(index)
|
||||
.is_some_and(|x| x.flags.contains(FontFlags::VARIABLE))
|
||||
{
|
||||
engine.sink.warn(warning!(
|
||||
list.span,
|
||||
"variable fonts are not currently supported and may render incorrectly";
|
||||
hint: "try installing a static version of \"{}\" instead", family.as_str()
|
||||
))
|
||||
}
|
||||
}
|
||||
None => engine.sink.warn(warning!(
|
||||
list.span,
|
||||
"unknown font family: {}",
|
||||
family.as_str(),
|
||||
));
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,15 +3,17 @@ use std::ops::Range;
|
||||
use std::sync::{Arc, LazyLock};
|
||||
|
||||
use comemo::Tracked;
|
||||
use ecow::{eco_format, EcoString, EcoVec};
|
||||
use syntect::highlighting as synt;
|
||||
use syntect::parsing::{SyntaxDefinition, SyntaxSet, SyntaxSetBuilder};
|
||||
use ecow::{EcoString, EcoVec};
|
||||
use syntect::highlighting::{self as synt};
|
||||
use syntect::parsing::{ParseSyntaxError, SyntaxDefinition, SyntaxSet, SyntaxSetBuilder};
|
||||
use typst_syntax::{split_newlines, LinkedNode, Span, Spanned};
|
||||
use typst_utils::ManuallyHash;
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
use super::Lang;
|
||||
use crate::diag::{At, FileError, SourceResult, StrResult};
|
||||
use crate::diag::{
|
||||
LineCol, LoadError, LoadResult, LoadedWithin, ReportPos, SourceResult,
|
||||
};
|
||||
use crate::engine::Engine;
|
||||
use crate::foundations::{
|
||||
cast, elem, scope, Bytes, Content, Derived, NativeElement, OneOrMultiple, Packed,
|
||||
@ -539,40 +541,29 @@ impl RawSyntax {
|
||||
world: Tracked<dyn World + '_>,
|
||||
sources: Spanned<OneOrMultiple<DataSource>>,
|
||||
) -> SourceResult<Derived<OneOrMultiple<DataSource>, Vec<RawSyntax>>> {
|
||||
let data = sources.load(world)?;
|
||||
let list = sources
|
||||
.v
|
||||
.0
|
||||
let loaded = sources.load(world)?;
|
||||
let list = loaded
|
||||
.iter()
|
||||
.zip(&data)
|
||||
.map(|(source, data)| Self::decode(source, data))
|
||||
.collect::<StrResult<_>>()
|
||||
.at(sources.span)?;
|
||||
.map(|data| Self::decode(&data.data).within(data))
|
||||
.collect::<SourceResult<_>>()?;
|
||||
Ok(Derived::new(sources.v, list))
|
||||
}
|
||||
|
||||
/// Decode a syntax from a loaded source.
|
||||
#[comemo::memoize]
|
||||
#[typst_macros::time(name = "load syntaxes")]
|
||||
fn decode(source: &DataSource, data: &Bytes) -> StrResult<RawSyntax> {
|
||||
let src = data.as_str().map_err(FileError::from)?;
|
||||
let syntax = SyntaxDefinition::load_from_str(src, false, None).map_err(
|
||||
|err| match source {
|
||||
DataSource::Path(path) => {
|
||||
eco_format!("failed to parse syntax file `{path}` ({err})")
|
||||
}
|
||||
DataSource::Bytes(_) => {
|
||||
eco_format!("failed to parse syntax ({err})")
|
||||
}
|
||||
},
|
||||
)?;
|
||||
fn decode(bytes: &Bytes) -> LoadResult<RawSyntax> {
|
||||
let str = bytes.as_str()?;
|
||||
|
||||
let syntax = SyntaxDefinition::load_from_str(str, false, None)
|
||||
.map_err(format_syntax_error)?;
|
||||
|
||||
let mut builder = SyntaxSetBuilder::new();
|
||||
builder.add(syntax);
|
||||
|
||||
Ok(RawSyntax(Arc::new(ManuallyHash::new(
|
||||
builder.build(),
|
||||
typst_utils::hash128(data),
|
||||
typst_utils::hash128(bytes),
|
||||
))))
|
||||
}
|
||||
|
||||
@ -582,6 +573,24 @@ impl RawSyntax {
|
||||
}
|
||||
}
|
||||
|
||||
fn format_syntax_error(error: ParseSyntaxError) -> LoadError {
|
||||
let pos = syntax_error_pos(&error);
|
||||
LoadError::new(pos, "failed to parse syntax", error)
|
||||
}
|
||||
|
||||
fn syntax_error_pos(error: &ParseSyntaxError) -> ReportPos {
|
||||
match error {
|
||||
ParseSyntaxError::InvalidYaml(scan_error) => {
|
||||
let m = scan_error.marker();
|
||||
ReportPos::full(
|
||||
m.index()..m.index(),
|
||||
LineCol::one_based(m.line(), m.col() + 1),
|
||||
)
|
||||
}
|
||||
_ => ReportPos::None,
|
||||
}
|
||||
}
|
||||
|
||||
/// A loaded syntect theme.
|
||||
#[derive(Debug, Clone, PartialEq, Hash)]
|
||||
pub struct RawTheme(Arc<ManuallyHash<synt::Theme>>);
|
||||
@ -592,18 +601,18 @@ impl RawTheme {
|
||||
world: Tracked<dyn World + '_>,
|
||||
source: Spanned<DataSource>,
|
||||
) -> SourceResult<Derived<DataSource, Self>> {
|
||||
let data = source.load(world)?;
|
||||
let theme = Self::decode(&data).at(source.span)?;
|
||||
let loaded = source.load(world)?;
|
||||
let theme = Self::decode(&loaded.data).within(&loaded)?;
|
||||
Ok(Derived::new(source.v, theme))
|
||||
}
|
||||
|
||||
/// Decode a theme from bytes.
|
||||
#[comemo::memoize]
|
||||
fn decode(data: &Bytes) -> StrResult<RawTheme> {
|
||||
let mut cursor = std::io::Cursor::new(data.as_slice());
|
||||
let theme = synt::ThemeSet::load_from_reader(&mut cursor)
|
||||
.map_err(|err| eco_format!("failed to parse theme ({err})"))?;
|
||||
Ok(RawTheme(Arc::new(ManuallyHash::new(theme, typst_utils::hash128(data)))))
|
||||
fn decode(bytes: &Bytes) -> LoadResult<RawTheme> {
|
||||
let mut cursor = std::io::Cursor::new(bytes.as_slice());
|
||||
let theme =
|
||||
synt::ThemeSet::load_from_reader(&mut cursor).map_err(format_theme_error)?;
|
||||
Ok(RawTheme(Arc::new(ManuallyHash::new(theme, typst_utils::hash128(bytes)))))
|
||||
}
|
||||
|
||||
/// Get the underlying syntect theme.
|
||||
@ -612,6 +621,14 @@ impl RawTheme {
|
||||
}
|
||||
}
|
||||
|
||||
fn format_theme_error(error: syntect::LoadingError) -> LoadError {
|
||||
let pos = match &error {
|
||||
syntect::LoadingError::ParseSyntax(err, _) => syntax_error_pos(err),
|
||||
_ => ReportPos::None,
|
||||
};
|
||||
LoadError::new(pos, "failed to parse theme", error)
|
||||
}
|
||||
|
||||
/// A highlighted line of raw text.
|
||||
///
|
||||
/// This is a helper element that is synthesized by [`raw`] elements.
|
||||
@ -819,7 +836,7 @@ fn to_typst(synt::Color { r, g, b, a }: synt::Color) -> Color {
|
||||
}
|
||||
|
||||
fn to_syn(color: Color) -> synt::Color {
|
||||
let [r, g, b, a] = color.to_rgb().to_vec4_u8();
|
||||
let (r, g, b, a) = color.to_rgb().into_format::<u8, u8>().into_components();
|
||||
synt::Color { r, g, b, a }
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,10 @@ use ecow::EcoString;
|
||||
|
||||
use crate::diag::SourceResult;
|
||||
use crate::engine::Engine;
|
||||
use crate::foundations::{elem, Content, Packed, SequenceElem, Show, StyleChain};
|
||||
use crate::foundations::{
|
||||
elem, Content, NativeElement, Packed, SequenceElem, Show, StyleChain, TargetElem,
|
||||
};
|
||||
use crate::html::{tag, HtmlElem};
|
||||
use crate::layout::{Em, Length};
|
||||
use crate::text::{variant, SpaceElem, TextElem, TextSize};
|
||||
use crate::World;
|
||||
@ -52,6 +55,13 @@ impl Show for Packed<SubElem> {
|
||||
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
||||
let body = self.body.clone();
|
||||
|
||||
if TargetElem::target_in(styles).is_html() {
|
||||
return Ok(HtmlElem::new(tag::sub)
|
||||
.with_body(Some(body))
|
||||
.pack()
|
||||
.spanned(self.span()));
|
||||
}
|
||||
|
||||
if self.typographic(styles) {
|
||||
if let Some(text) = convert_script(&body, true) {
|
||||
if is_shapable(engine, &text, styles) {
|
||||
@ -111,6 +121,13 @@ impl Show for Packed<SuperElem> {
|
||||
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
|
||||
let body = self.body.clone();
|
||||
|
||||
if TargetElem::target_in(styles).is_html() {
|
||||
return Ok(HtmlElem::new(tag::sup)
|
||||
.with_body(Some(body))
|
||||
.pack()
|
||||
.spanned(self.span()));
|
||||
}
|
||||
|
||||
if self.typographic(styles) {
|
||||
if let Some(text) = convert_script(&body, false) {
|
||||
if is_shapable(engine, &text, styles) {
|
||||
|
@ -262,7 +262,7 @@ impl Color {
|
||||
color: Color,
|
||||
) -> SourceResult<Color> {
|
||||
Ok(if let Some(color) = args.find::<Color>()? {
|
||||
color.to_luma()
|
||||
Color::Luma(color.to_luma())
|
||||
} else {
|
||||
let Component(gray) =
|
||||
args.expect("gray component").unwrap_or(Component(Ratio::one()));
|
||||
@ -318,7 +318,7 @@ impl Color {
|
||||
color: Color,
|
||||
) -> SourceResult<Color> {
|
||||
Ok(if let Some(color) = args.find::<Color>()? {
|
||||
color.to_oklab()
|
||||
Color::Oklab(color.to_oklab())
|
||||
} else {
|
||||
let RatioComponent(l) = args.expect("lightness component")?;
|
||||
let ChromaComponent(a) = args.expect("A component")?;
|
||||
@ -374,7 +374,7 @@ impl Color {
|
||||
color: Color,
|
||||
) -> SourceResult<Color> {
|
||||
Ok(if let Some(color) = args.find::<Color>()? {
|
||||
color.to_oklch()
|
||||
Color::Oklch(color.to_oklch())
|
||||
} else {
|
||||
let RatioComponent(l) = args.expect("lightness component")?;
|
||||
let ChromaComponent(c) = args.expect("chroma component")?;
|
||||
@ -434,7 +434,7 @@ impl Color {
|
||||
color: Color,
|
||||
) -> SourceResult<Color> {
|
||||
Ok(if let Some(color) = args.find::<Color>()? {
|
||||
color.to_linear_rgb()
|
||||
Color::LinearRgb(color.to_linear_rgb())
|
||||
} else {
|
||||
let Component(r) = args.expect("red component")?;
|
||||
let Component(g) = args.expect("green component")?;
|
||||
@ -505,7 +505,7 @@ impl Color {
|
||||
Ok(if let Some(string) = args.find::<Spanned<Str>>()? {
|
||||
Self::from_str(&string.v).at(string.span)?
|
||||
} else if let Some(color) = args.find::<Color>()? {
|
||||
color.to_rgb()
|
||||
Color::Rgb(color.to_rgb())
|
||||
} else {
|
||||
let Component(r) = args.expect("red component")?;
|
||||
let Component(g) = args.expect("green component")?;
|
||||
@ -565,7 +565,7 @@ impl Color {
|
||||
color: Color,
|
||||
) -> SourceResult<Color> {
|
||||
Ok(if let Some(color) = args.find::<Color>()? {
|
||||
color.to_cmyk()
|
||||
Color::Cmyk(color.to_cmyk())
|
||||
} else {
|
||||
let RatioComponent(c) = args.expect("cyan component")?;
|
||||
let RatioComponent(m) = args.expect("magenta component")?;
|
||||
@ -622,7 +622,7 @@ impl Color {
|
||||
color: Color,
|
||||
) -> SourceResult<Color> {
|
||||
Ok(if let Some(color) = args.find::<Color>()? {
|
||||
color.to_hsl()
|
||||
Color::Hsl(color.to_hsl())
|
||||
} else {
|
||||
let h: Angle = args.expect("hue component")?;
|
||||
let Component(s) = args.expect("saturation component")?;
|
||||
@ -679,7 +679,7 @@ impl Color {
|
||||
color: Color,
|
||||
) -> SourceResult<Color> {
|
||||
Ok(if let Some(color) = args.find::<Color>()? {
|
||||
color.to_hsv()
|
||||
Color::Hsv(color.to_hsv())
|
||||
} else {
|
||||
let h: Angle = args.expect("hue component")?;
|
||||
let Component(s) = args.expect("saturation component")?;
|
||||
@ -830,7 +830,7 @@ impl Color {
|
||||
/// omitted if it is equal to `ff` (255 / 100%).
|
||||
#[func]
|
||||
pub fn to_hex(self) -> EcoString {
|
||||
let [r, g, b, a] = self.to_rgb().to_vec4_u8();
|
||||
let (r, g, b, a) = self.to_rgb().into_format::<u8, u8>().into_components();
|
||||
if a != 255 {
|
||||
eco_format!("#{:02x}{:02x}{:02x}{:02x}", r, g, b, a)
|
||||
} else {
|
||||
@ -886,20 +886,21 @@ impl Color {
|
||||
/// The factor to saturate the color by.
|
||||
factor: Ratio,
|
||||
) -> SourceResult<Color> {
|
||||
let f = factor.get() as f32;
|
||||
Ok(match self {
|
||||
Self::Luma(_) => {
|
||||
bail!(
|
||||
span, "cannot saturate grayscale color";
|
||||
hint: "try converting your color to RGB first"
|
||||
);
|
||||
Self::Luma(_) => bail!(
|
||||
span, "cannot saturate grayscale color";
|
||||
hint: "try converting your color to RGB first"
|
||||
),
|
||||
Self::Hsl(c) => Self::Hsl(c.saturate(f)),
|
||||
Self::Hsv(c) => Self::Hsv(c.saturate(f)),
|
||||
Self::Oklab(_)
|
||||
| Self::Oklch(_)
|
||||
| Self::LinearRgb(_)
|
||||
| Self::Rgb(_)
|
||||
| Self::Cmyk(_) => {
|
||||
Color::Hsv(self.to_hsv().saturate(f)).to_space(self.space())
|
||||
}
|
||||
Self::Oklab(_) => self.to_hsv().saturate(span, factor)?.to_oklab(),
|
||||
Self::Oklch(_) => self.to_hsv().saturate(span, factor)?.to_oklch(),
|
||||
Self::LinearRgb(_) => self.to_hsv().saturate(span, factor)?.to_linear_rgb(),
|
||||
Self::Rgb(_) => self.to_hsv().saturate(span, factor)?.to_rgb(),
|
||||
Self::Cmyk(_) => self.to_hsv().saturate(span, factor)?.to_cmyk(),
|
||||
Self::Hsl(c) => Self::Hsl(c.saturate(factor.get() as f32)),
|
||||
Self::Hsv(c) => Self::Hsv(c.saturate(factor.get() as f32)),
|
||||
})
|
||||
}
|
||||
|
||||
@ -911,20 +912,21 @@ impl Color {
|
||||
/// The factor to desaturate the color by.
|
||||
factor: Ratio,
|
||||
) -> SourceResult<Color> {
|
||||
let f = factor.get() as f32;
|
||||
Ok(match self {
|
||||
Self::Luma(_) => {
|
||||
bail!(
|
||||
span, "cannot desaturate grayscale color";
|
||||
hint: "try converting your color to RGB first"
|
||||
);
|
||||
Self::Luma(_) => bail!(
|
||||
span, "cannot desaturate grayscale color";
|
||||
hint: "try converting your color to RGB first"
|
||||
),
|
||||
Self::Hsl(c) => Self::Hsl(c.desaturate(f)),
|
||||
Self::Hsv(c) => Self::Hsv(c.desaturate(f)),
|
||||
Self::Oklab(_)
|
||||
| Self::Oklch(_)
|
||||
| Self::LinearRgb(_)
|
||||
| Self::Rgb(_)
|
||||
| Self::Cmyk(_) => {
|
||||
Color::Hsv(self.to_hsv().desaturate(f)).to_space(self.space())
|
||||
}
|
||||
Self::Oklab(_) => self.to_hsv().desaturate(span, factor)?.to_oklab(),
|
||||
Self::Oklch(_) => self.to_hsv().desaturate(span, factor)?.to_oklch(),
|
||||
Self::LinearRgb(_) => self.to_hsv().desaturate(span, factor)?.to_linear_rgb(),
|
||||
Self::Rgb(_) => self.to_hsv().desaturate(span, factor)?.to_rgb(),
|
||||
Self::Cmyk(_) => self.to_hsv().desaturate(span, factor)?.to_cmyk(),
|
||||
Self::Hsl(c) => Self::Hsl(c.desaturate(factor.get() as f32)),
|
||||
Self::Hsv(c) => Self::Hsv(c.desaturate(factor.get() as f32)),
|
||||
})
|
||||
}
|
||||
|
||||
@ -994,23 +996,17 @@ impl Color {
|
||||
) -> SourceResult<Color> {
|
||||
Ok(match space {
|
||||
ColorSpace::Oklch => {
|
||||
let Self::Oklch(oklch) = self.to_oklch() else {
|
||||
unreachable!();
|
||||
};
|
||||
let oklch = self.to_oklch();
|
||||
let rotated = oklch.shift_hue(angle.to_deg() as f32);
|
||||
Self::Oklch(rotated).to_space(self.space())
|
||||
}
|
||||
ColorSpace::Hsl => {
|
||||
let Self::Hsl(hsl) = self.to_hsl() else {
|
||||
unreachable!();
|
||||
};
|
||||
let hsl = self.to_hsl();
|
||||
let rotated = hsl.shift_hue(angle.to_deg() as f32);
|
||||
Self::Hsl(rotated).to_space(self.space())
|
||||
}
|
||||
ColorSpace::Hsv => {
|
||||
let Self::Hsv(hsv) = self.to_hsv() else {
|
||||
unreachable!();
|
||||
};
|
||||
let hsv = self.to_hsv();
|
||||
let rotated = hsv.shift_hue(angle.to_deg() as f32);
|
||||
Self::Hsv(rotated).to_space(self.space())
|
||||
}
|
||||
@ -1281,19 +1277,19 @@ impl Color {
|
||||
|
||||
pub fn to_space(self, space: ColorSpace) -> Self {
|
||||
match space {
|
||||
ColorSpace::Oklab => self.to_oklab(),
|
||||
ColorSpace::Oklch => self.to_oklch(),
|
||||
ColorSpace::Srgb => self.to_rgb(),
|
||||
ColorSpace::LinearRgb => self.to_linear_rgb(),
|
||||
ColorSpace::Hsl => self.to_hsl(),
|
||||
ColorSpace::Hsv => self.to_hsv(),
|
||||
ColorSpace::Cmyk => self.to_cmyk(),
|
||||
ColorSpace::D65Gray => self.to_luma(),
|
||||
ColorSpace::D65Gray => Self::Luma(self.to_luma()),
|
||||
ColorSpace::Oklab => Self::Oklab(self.to_oklab()),
|
||||
ColorSpace::Oklch => Self::Oklch(self.to_oklch()),
|
||||
ColorSpace::Srgb => Self::Rgb(self.to_rgb()),
|
||||
ColorSpace::LinearRgb => Self::LinearRgb(self.to_linear_rgb()),
|
||||
ColorSpace::Cmyk => Self::Cmyk(self.to_cmyk()),
|
||||
ColorSpace::Hsl => Self::Hsl(self.to_hsl()),
|
||||
ColorSpace::Hsv => Self::Hsv(self.to_hsv()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_luma(self) -> Self {
|
||||
Self::Luma(match self {
|
||||
pub fn to_luma(self) -> Luma {
|
||||
match self {
|
||||
Self::Luma(c) => c,
|
||||
Self::Oklab(c) => Luma::from_color(c),
|
||||
Self::Oklch(c) => Luma::from_color(c),
|
||||
@ -1302,11 +1298,11 @@ impl Color {
|
||||
Self::Cmyk(c) => Luma::from_color(c.to_rgba()),
|
||||
Self::Hsl(c) => Luma::from_color(c),
|
||||
Self::Hsv(c) => Luma::from_color(c),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_oklab(self) -> Self {
|
||||
Self::Oklab(match self {
|
||||
pub fn to_oklab(self) -> Oklab {
|
||||
match self {
|
||||
Self::Luma(c) => Oklab::from_color(c),
|
||||
Self::Oklab(c) => c,
|
||||
Self::Oklch(c) => Oklab::from_color(c),
|
||||
@ -1315,11 +1311,11 @@ impl Color {
|
||||
Self::Cmyk(c) => Oklab::from_color(c.to_rgba()),
|
||||
Self::Hsl(c) => Oklab::from_color(c),
|
||||
Self::Hsv(c) => Oklab::from_color(c),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_oklch(self) -> Self {
|
||||
Self::Oklch(match self {
|
||||
pub fn to_oklch(self) -> Oklch {
|
||||
match self {
|
||||
Self::Luma(c) => Oklch::from_color(c),
|
||||
Self::Oklab(c) => Oklch::from_color(c),
|
||||
Self::Oklch(c) => c,
|
||||
@ -1328,11 +1324,11 @@ impl Color {
|
||||
Self::Cmyk(c) => Oklch::from_color(c.to_rgba()),
|
||||
Self::Hsl(c) => Oklch::from_color(c),
|
||||
Self::Hsv(c) => Oklch::from_color(c),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_rgb(self) -> Self {
|
||||
Self::Rgb(match self {
|
||||
pub fn to_rgb(self) -> Rgb {
|
||||
match self {
|
||||
Self::Luma(c) => Rgb::from_color(c),
|
||||
Self::Oklab(c) => Rgb::from_color(c),
|
||||
Self::Oklch(c) => Rgb::from_color(c),
|
||||
@ -1341,11 +1337,11 @@ impl Color {
|
||||
Self::Cmyk(c) => Rgb::from_color(c.to_rgba()),
|
||||
Self::Hsl(c) => Rgb::from_color(c),
|
||||
Self::Hsv(c) => Rgb::from_color(c),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_linear_rgb(self) -> Self {
|
||||
Self::LinearRgb(match self {
|
||||
pub fn to_linear_rgb(self) -> LinearRgb {
|
||||
match self {
|
||||
Self::Luma(c) => LinearRgb::from_color(c),
|
||||
Self::Oklab(c) => LinearRgb::from_color(c),
|
||||
Self::Oklch(c) => LinearRgb::from_color(c),
|
||||
@ -1354,11 +1350,11 @@ impl Color {
|
||||
Self::Cmyk(c) => LinearRgb::from_color(c.to_rgba()),
|
||||
Self::Hsl(c) => Rgb::from_color(c).into_linear(),
|
||||
Self::Hsv(c) => Rgb::from_color(c).into_linear(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_cmyk(self) -> Self {
|
||||
Self::Cmyk(match self {
|
||||
pub fn to_cmyk(self) -> Cmyk {
|
||||
match self {
|
||||
Self::Luma(c) => Cmyk::from_luma(c),
|
||||
Self::Oklab(c) => Cmyk::from_rgba(Rgb::from_color(c)),
|
||||
Self::Oklch(c) => Cmyk::from_rgba(Rgb::from_color(c)),
|
||||
@ -1367,11 +1363,11 @@ impl Color {
|
||||
Self::Cmyk(c) => c,
|
||||
Self::Hsl(c) => Cmyk::from_rgba(Rgb::from_color(c)),
|
||||
Self::Hsv(c) => Cmyk::from_rgba(Rgb::from_color(c)),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_hsl(self) -> Self {
|
||||
Self::Hsl(match self {
|
||||
pub fn to_hsl(self) -> Hsl {
|
||||
match self {
|
||||
Self::Luma(c) => Hsl::from_color(c),
|
||||
Self::Oklab(c) => Hsl::from_color(c),
|
||||
Self::Oklch(c) => Hsl::from_color(c),
|
||||
@ -1380,11 +1376,11 @@ impl Color {
|
||||
Self::Cmyk(c) => Hsl::from_color(c.to_rgba()),
|
||||
Self::Hsl(c) => c,
|
||||
Self::Hsv(c) => Hsl::from_color(c),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_hsv(self) -> Self {
|
||||
Self::Hsv(match self {
|
||||
pub fn to_hsv(self) -> Hsv {
|
||||
match self {
|
||||
Self::Luma(c) => Hsv::from_color(c),
|
||||
Self::Oklab(c) => Hsv::from_color(c),
|
||||
Self::Oklch(c) => Hsv::from_color(c),
|
||||
@ -1393,7 +1389,7 @@ impl Color {
|
||||
Self::Cmyk(c) => Hsv::from_color(c.to_rgba()),
|
||||
Self::Hsl(c) => Hsv::from_color(c),
|
||||
Self::Hsv(c) => c,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -549,7 +549,7 @@ impl Gradient {
|
||||
}
|
||||
|
||||
/// Repeats this gradient a given number of times, optionally mirroring it
|
||||
/// at each repetition.
|
||||
/// at every second repetition.
|
||||
///
|
||||
/// ```example
|
||||
/// #circle(
|
||||
@ -564,7 +564,17 @@ impl Gradient {
|
||||
&self,
|
||||
/// The number of times to repeat the gradient.
|
||||
repetitions: Spanned<usize>,
|
||||
/// Whether to mirror the gradient at each repetition.
|
||||
/// Whether to mirror the gradient at every second repetition, i.e.,
|
||||
/// the first instance (and all odd ones) stays unchanged.
|
||||
///
|
||||
/// ```example
|
||||
/// #circle(
|
||||
/// radius: 40pt,
|
||||
/// fill: gradient
|
||||
/// .conic(green, black)
|
||||
/// .repeat(2, mirror: true)
|
||||
/// )
|
||||
/// ```
|
||||
#[named]
|
||||
#[default(false)]
|
||||
mirror: bool,
|
||||
|
@ -22,7 +22,7 @@ use crate::foundations::{
|
||||
Smart, StyleChain,
|
||||
};
|
||||
use crate::layout::{BlockElem, Length, Rel, Sizing};
|
||||
use crate::loading::{DataSource, Load, Readable};
|
||||
use crate::loading::{DataSource, Load, LoadSource, Loaded, Readable};
|
||||
use crate::model::Figurable;
|
||||
use crate::text::LocalName;
|
||||
|
||||
@ -65,10 +65,10 @@ pub struct ImageElem {
|
||||
#[required]
|
||||
#[parse(
|
||||
let source = args.expect::<Spanned<DataSource>>("source")?;
|
||||
let data = source.load(engine.world)?;
|
||||
Derived::new(source.v, data)
|
||||
let loaded = source.load(engine.world)?;
|
||||
Derived::new(source.v, loaded)
|
||||
)]
|
||||
pub source: Derived<DataSource, Bytes>,
|
||||
pub source: Derived<DataSource, Loaded>,
|
||||
|
||||
/// The image's format.
|
||||
///
|
||||
@ -154,8 +154,8 @@ pub struct ImageElem {
|
||||
/// to `{auto}`, Typst will try to extract an ICC profile from the image.
|
||||
#[parse(match args.named::<Spanned<Smart<DataSource>>>("icc")? {
|
||||
Some(Spanned { v: Smart::Custom(source), span }) => Some(Smart::Custom({
|
||||
let data = Spanned::new(&source, span).load(engine.world)?;
|
||||
Derived::new(source, data)
|
||||
let loaded = Spanned::new(&source, span).load(engine.world)?;
|
||||
Derived::new(source, loaded.data)
|
||||
})),
|
||||
Some(Spanned { v: Smart::Auto, .. }) => Some(Smart::Auto),
|
||||
None => None,
|
||||
@ -173,7 +173,7 @@ impl ImageElem {
|
||||
pub fn decode(
|
||||
span: Span,
|
||||
/// The data to decode as an image. Can be a string for SVGs.
|
||||
data: Readable,
|
||||
data: Spanned<Readable>,
|
||||
/// The image's format. Detected automatically by default.
|
||||
#[named]
|
||||
format: Option<Smart<ImageFormat>>,
|
||||
@ -193,8 +193,10 @@ impl ImageElem {
|
||||
#[named]
|
||||
scaling: Option<Smart<ImageScaling>>,
|
||||
) -> StrResult<Content> {
|
||||
let bytes = data.into_bytes();
|
||||
let source = Derived::new(DataSource::Bytes(bytes.clone()), bytes);
|
||||
let bytes = data.v.into_bytes();
|
||||
let loaded =
|
||||
Loaded::new(Spanned::new(LoadSource::Bytes, data.span), bytes.clone());
|
||||
let source = Derived::new(DataSource::Bytes(bytes), loaded);
|
||||
let mut elem = ImageElem::new(source);
|
||||
if let Some(format) = format {
|
||||
elem.push_format(format);
|
||||
|
@ -3,10 +3,9 @@ use std::hash::{Hash, Hasher};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use comemo::Tracked;
|
||||
use ecow::EcoString;
|
||||
use siphasher::sip128::{Hasher128, SipHasher13};
|
||||
|
||||
use crate::diag::{format_xml_like_error, StrResult};
|
||||
use crate::diag::{format_xml_like_error, LoadError, LoadResult, ReportPos};
|
||||
use crate::foundations::Bytes;
|
||||
use crate::layout::Axes;
|
||||
use crate::text::{
|
||||
@ -30,7 +29,7 @@ impl SvgImage {
|
||||
/// Decode an SVG image without fonts.
|
||||
#[comemo::memoize]
|
||||
#[typst_macros::time(name = "load svg")]
|
||||
pub fn new(data: Bytes) -> StrResult<SvgImage> {
|
||||
pub fn new(data: Bytes) -> LoadResult<SvgImage> {
|
||||
let tree =
|
||||
usvg::Tree::from_data(&data, &base_options()).map_err(format_usvg_error)?;
|
||||
Ok(Self(Arc::new(Repr { data, size: tree_size(&tree), font_hash: 0, tree })))
|
||||
@ -43,7 +42,7 @@ impl SvgImage {
|
||||
data: Bytes,
|
||||
world: Tracked<dyn World + '_>,
|
||||
families: &[&str],
|
||||
) -> StrResult<SvgImage> {
|
||||
) -> LoadResult<SvgImage> {
|
||||
let book = world.book();
|
||||
let resolver = Mutex::new(FontResolver::new(world, book, families));
|
||||
let tree = usvg::Tree::from_data(
|
||||
@ -125,16 +124,15 @@ fn tree_size(tree: &usvg::Tree) -> Axes<f64> {
|
||||
}
|
||||
|
||||
/// Format the user-facing SVG decoding error message.
|
||||
fn format_usvg_error(error: usvg::Error) -> EcoString {
|
||||
match error {
|
||||
usvg::Error::NotAnUtf8Str => "file is not valid utf-8".into(),
|
||||
usvg::Error::MalformedGZip => "file is not compressed correctly".into(),
|
||||
usvg::Error::ElementsLimitReached => "file is too large".into(),
|
||||
usvg::Error::InvalidSize => {
|
||||
"failed to parse SVG (width, height, or viewbox is invalid)".into()
|
||||
}
|
||||
usvg::Error::ParsingFailed(error) => format_xml_like_error("SVG", error),
|
||||
}
|
||||
fn format_usvg_error(error: usvg::Error) -> LoadError {
|
||||
let error = match error {
|
||||
usvg::Error::NotAnUtf8Str => "file is not valid utf-8",
|
||||
usvg::Error::MalformedGZip => "file is not compressed correctly",
|
||||
usvg::Error::ElementsLimitReached => "file is too large",
|
||||
usvg::Error::InvalidSize => "width, height, or viewbox is invalid",
|
||||
usvg::Error::ParsingFailed(error) => return format_xml_like_error("SVG", error),
|
||||
};
|
||||
LoadError::new(ReportPos::None, "failed to parse SVG", error)
|
||||
}
|
||||
|
||||
/// Provides Typst's fonts to usvg.
|
||||
|
@ -5,4 +5,4 @@ bibliography = المراجع
|
||||
heading = الفصل
|
||||
outline = المحتويات
|
||||
raw = قائمة
|
||||
page = صفحة
|
||||
page = صفحة
|
||||
|
@ -5,4 +5,4 @@ bibliography = Библиография
|
||||
heading = Раздел
|
||||
outline = Съдържание
|
||||
raw = Приложение
|
||||
page = стр.
|
||||
page = стр.
|
||||
|
@ -5,4 +5,4 @@ bibliography = Bibliografia
|
||||
heading = Secció
|
||||
outline = Índex
|
||||
raw = Llistat
|
||||
page = pàgina
|
||||
page = pàgina
|
||||
|
@ -5,4 +5,4 @@ bibliography = Bibliografie
|
||||
heading = Kapitola
|
||||
outline = Obsah
|
||||
raw = Výpis
|
||||
page = strana
|
||||
page = strana
|
||||
|
@ -5,4 +5,4 @@ bibliography = Bibliografi
|
||||
heading = Afsnit
|
||||
outline = Indhold
|
||||
raw = Liste
|
||||
page = side
|
||||
page = side
|
||||
|
@ -5,4 +5,4 @@ bibliography = Bibliographie
|
||||
heading = Abschnitt
|
||||
outline = Inhaltsverzeichnis
|
||||
raw = Listing
|
||||
page = Seite
|
||||
page = Seite
|
||||
|
@ -4,4 +4,4 @@ equation = Εξίσωση
|
||||
bibliography = Βιβλιογραφία
|
||||
heading = Κεφάλαιο
|
||||
outline = Περιεχόμενα
|
||||
raw = Παράθεση
|
||||
raw = Παράθεση
|
||||
|
@ -5,4 +5,4 @@ bibliography = Bibliography
|
||||
heading = Section
|
||||
outline = Contents
|
||||
raw = Listing
|
||||
page = page
|
||||
page = page
|
||||
|
@ -5,4 +5,4 @@ bibliography = Bibliografía
|
||||
heading = Sección
|
||||
outline = Índice
|
||||
raw = Listado
|
||||
page = página
|
||||
page = página
|
||||
|
@ -5,4 +5,4 @@ bibliography = Viited
|
||||
heading = Peatükk
|
||||
outline = Sisukord
|
||||
raw = List
|
||||
page = lk.
|
||||
page = lk.
|
||||
|
@ -5,4 +5,4 @@ bibliography = Viitteet
|
||||
heading = Osio
|
||||
outline = Sisällys
|
||||
raw = Esimerkki
|
||||
page = sivu
|
||||
page = sivu
|
||||
|
@ -5,4 +5,4 @@ bibliography = Bibliographie
|
||||
heading = Chapitre
|
||||
outline = Table des matières
|
||||
raw = Liste
|
||||
page = page
|
||||
page = page
|
||||
|
@ -5,4 +5,4 @@ bibliography = Bibliografía
|
||||
heading = Sección
|
||||
outline = Índice
|
||||
raw = Listado
|
||||
page = páxina
|
||||
page = páxina
|
||||
|
@ -5,4 +5,4 @@ bibliography = רשימת מקורות
|
||||
heading = חלק
|
||||
outline = תוכן עניינים
|
||||
raw = קטע מקור
|
||||
page = עמוד
|
||||
page = עמוד
|
||||
|
8
crates/typst-library/translations/hr.txt
Normal file
8
crates/typst-library/translations/hr.txt
Normal file
@ -0,0 +1,8 @@
|
||||
figure = Slika
|
||||
table = Tablica
|
||||
equation = Jednadžba
|
||||
bibliography = Literatura
|
||||
heading = Odjeljak
|
||||
outline = Sadržaj
|
||||
raw = Kôd
|
||||
page = str.
|
@ -4,5 +4,5 @@ equation = Egyenlet
|
||||
bibliography = Irodalomjegyzék
|
||||
heading = Fejezet
|
||||
outline = Tartalomjegyzék
|
||||
# raw =
|
||||
page = oldal
|
||||
# raw =
|
||||
page = oldal
|
||||
|
@ -5,4 +5,4 @@ bibliography = Heimildaskrá
|
||||
heading = Kafli
|
||||
outline = Efnisyfirlit
|
||||
raw = Sýnishorn
|
||||
page = blaðsíða
|
||||
page = blaðsíða
|
||||
|
@ -5,4 +5,4 @@ bibliography = Bibliografia
|
||||
heading = Sezione
|
||||
outline = Indice
|
||||
raw = Codice
|
||||
page = pag.
|
||||
page = pag.
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user