mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Use SymbolElem in more places and add char
cast for content
This commit is contained in:
parent
c47b71b435
commit
fecdc39846
@ -7,12 +7,11 @@ use typst_library::diag::{
|
|||||||
use typst_library::engine::{Engine, Sink, Traced};
|
use typst_library::engine::{Engine, Sink, Traced};
|
||||||
use typst_library::foundations::{
|
use typst_library::foundations::{
|
||||||
Arg, Args, Bytes, Capturer, Closure, Content, Context, Func, IntoValue,
|
Arg, Args, Bytes, Capturer, Closure, Content, Context, Func, IntoValue,
|
||||||
NativeElement, Scope, Scopes, Value,
|
NativeElement, Scope, Scopes, SymbolElem, Value,
|
||||||
};
|
};
|
||||||
use typst_library::introspection::Introspector;
|
use typst_library::introspection::Introspector;
|
||||||
use typst_library::math::LrElem;
|
use typst_library::math::LrElem;
|
||||||
use typst_library::routines::Routines;
|
use typst_library::routines::Routines;
|
||||||
use typst_library::text::TextElem;
|
|
||||||
use typst_library::World;
|
use typst_library::World;
|
||||||
use typst_syntax::ast::{self, AstNode, Ident};
|
use typst_syntax::ast::{self, AstNode, Ident};
|
||||||
use typst_syntax::{Span, Spanned, SyntaxNode};
|
use typst_syntax::{Span, Spanned, SyntaxNode};
|
||||||
@ -402,16 +401,16 @@ fn wrap_args_in_math(
|
|||||||
let mut body = Content::empty();
|
let mut body = Content::empty();
|
||||||
for (i, arg) in args.all::<Content>()?.into_iter().enumerate() {
|
for (i, arg) in args.all::<Content>()?.into_iter().enumerate() {
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
body += TextElem::packed(',');
|
body += SymbolElem::packed(',');
|
||||||
}
|
}
|
||||||
body += arg;
|
body += arg;
|
||||||
}
|
}
|
||||||
if trailing_comma {
|
if trailing_comma {
|
||||||
body += TextElem::packed(',');
|
body += SymbolElem::packed(',');
|
||||||
}
|
}
|
||||||
Ok(Value::Content(
|
Ok(Value::Content(
|
||||||
callee.display().spanned(callee_span)
|
callee.display().spanned(callee_span)
|
||||||
+ LrElem::new(TextElem::packed('(') + body + TextElem::packed(')'))
|
+ LrElem::new(SymbolElem::packed('(') + body + SymbolElem::packed(')'))
|
||||||
.pack()
|
.pack()
|
||||||
.spanned(args.span),
|
.spanned(args.span),
|
||||||
))
|
))
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use ecow::eco_format;
|
use ecow::eco_format;
|
||||||
use typst_library::diag::{At, SourceResult};
|
use typst_library::diag::{At, SourceResult};
|
||||||
use typst_library::foundations::{Content, NativeElement, Symbol, Value};
|
use typst_library::foundations::{Content, NativeElement, Symbol, SymbolElem, Value};
|
||||||
use typst_library::math::{
|
use typst_library::math::{
|
||||||
AlignPointElem, AttachElem, FracElem, LrElem, PrimesElem, RootElem,
|
AlignPointElem, AttachElem, FracElem, LrElem, PrimesElem, RootElem,
|
||||||
};
|
};
|
||||||
@ -25,8 +25,7 @@ impl Eval for ast::MathText<'_> {
|
|||||||
|
|
||||||
fn eval(self, _: &mut Vm) -> SourceResult<Self::Output> {
|
fn eval(self, _: &mut Vm) -> SourceResult<Self::Output> {
|
||||||
match self.get() {
|
match self.get() {
|
||||||
// TODO: change to `SymbolElem` when added
|
MathTextKind::Character(c) => Ok(SymbolElem::packed(c)),
|
||||||
MathTextKind::Character(c) => Ok(Value::Symbol(Symbol::single(c)).display()),
|
|
||||||
MathTextKind::Number(text) => Ok(TextElem::packed(text.clone())),
|
MathTextKind::Number(text) => Ok(TextElem::packed(text.clone())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -114,6 +113,7 @@ impl Eval for ast::MathRoot<'_> {
|
|||||||
type Output = Content;
|
type Output = Content;
|
||||||
|
|
||||||
fn eval(self, vm: &mut Vm) -> SourceResult<Self::Output> {
|
fn eval(self, vm: &mut Vm) -> SourceResult<Self::Output> {
|
||||||
|
// Use `TextElem` to match `MathTextKind::Number` above.
|
||||||
let index = self.index().map(|i| TextElem::packed(eco_format!("{i}")));
|
let index = self.index().map(|i| TextElem::packed(eco_format!("{i}")));
|
||||||
let radicand = self.radicand().eval_display(vm)?;
|
let radicand = self.radicand().eval_display(vm)?;
|
||||||
Ok(RootElem::new(radicand).with_index(index).pack())
|
Ok(RootElem::new(radicand).with_index(index).pack())
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
use typst_library::diag::SourceResult;
|
use typst_library::diag::SourceResult;
|
||||||
use typst_library::foundations::{Packed, StyleChain};
|
use typst_library::foundations::{Packed, StyleChain, SymbolElem};
|
||||||
use typst_library::layout::{Abs, Axis, Corner, Frame, Point, Rel, Size};
|
use typst_library::layout::{Abs, Axis, Corner, Frame, Point, Rel, Size};
|
||||||
use typst_library::math::{
|
use typst_library::math::{
|
||||||
AttachElem, EquationElem, LimitsElem, PrimesElem, ScriptsElem, StretchElem,
|
AttachElem, EquationElem, LimitsElem, PrimesElem, ScriptsElem, StretchElem,
|
||||||
};
|
};
|
||||||
use typst_library::text::TextElem;
|
|
||||||
use typst_utils::OptionExt;
|
use typst_utils::OptionExt;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
@ -104,13 +103,14 @@ pub fn layout_primes(
|
|||||||
4 => '⁗',
|
4 => '⁗',
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
let f = ctx.layout_into_fragment(&TextElem::packed(c), styles)?;
|
let f = ctx.layout_into_fragment(&SymbolElem::packed(c), styles)?;
|
||||||
ctx.push(f);
|
ctx.push(f);
|
||||||
}
|
}
|
||||||
count => {
|
count => {
|
||||||
// Custom amount of primes
|
// Custom amount of primes
|
||||||
let prime =
|
let prime = ctx
|
||||||
ctx.layout_into_fragment(&TextElem::packed('′'), styles)?.into_frame();
|
.layout_into_fragment(&SymbolElem::packed('′'), styles)?
|
||||||
|
.into_frame();
|
||||||
let width = prime.width() * (count + 1) as f64 / 2.0;
|
let width = prime.width() * (count + 1) as f64 / 2.0;
|
||||||
let mut frame = Frame::soft(Size::new(width, prime.height()));
|
let mut frame = Frame::soft(Size::new(width, prime.height()));
|
||||||
frame.set_baseline(prime.ascent());
|
frame.set_baseline(prime.ascent());
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use typst_library::diag::SourceResult;
|
use typst_library::diag::SourceResult;
|
||||||
use typst_library::foundations::{Content, Packed, Resolve, StyleChain};
|
use typst_library::foundations::{Content, Packed, Resolve, StyleChain, SymbolElem};
|
||||||
use typst_library::layout::{Em, Frame, FrameItem, Point, Size};
|
use typst_library::layout::{Em, Frame, FrameItem, Point, Size};
|
||||||
use typst_library::math::{BinomElem, FracElem};
|
use typst_library::math::{BinomElem, FracElem};
|
||||||
use typst_library::text::TextElem;
|
use typst_library::text::TextElem;
|
||||||
@ -80,7 +80,10 @@ fn layout_frac_like(
|
|||||||
let denom = ctx.layout_into_frame(
|
let denom = ctx.layout_into_frame(
|
||||||
&Content::sequence(
|
&Content::sequence(
|
||||||
// Add a comma between each element.
|
// Add a comma between each element.
|
||||||
denom.iter().flat_map(|a| [TextElem::packed(','), a.clone()]).skip(1),
|
denom
|
||||||
|
.iter()
|
||||||
|
.flat_map(|a| [SymbolElem::packed(','), a.clone()])
|
||||||
|
.skip(1),
|
||||||
),
|
),
|
||||||
styles.chain(&denom_style),
|
styles.chain(&denom_style),
|
||||||
)?;
|
)?;
|
||||||
|
@ -136,18 +136,10 @@ impl Default for Delimiter {
|
|||||||
cast! {
|
cast! {
|
||||||
Delimiter,
|
Delimiter,
|
||||||
self => self.0.into_value(),
|
self => self.0.into_value(),
|
||||||
v: EcoString => {
|
c: char => if c.is_ascii() {
|
||||||
let mut chars = v.chars();
|
Self(c)
|
||||||
let first = chars.next().ok_or("delimiter must not be empty")?;
|
} else {
|
||||||
if chars.next().is_some() {
|
bail!("delimiter must be an ASCII character")
|
||||||
bail!("delimiter must be a single character");
|
|
||||||
}
|
|
||||||
|
|
||||||
if !first.is_ascii() {
|
|
||||||
bail!("delimiter must be an ASCII character");
|
|
||||||
}
|
|
||||||
|
|
||||||
Self(first)
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
use crate::foundations::{elem, func, Content, NativeElement};
|
use crate::foundations::{elem, func, Content, NativeElement, SymbolElem};
|
||||||
use crate::layout::{Length, Rel};
|
use crate::layout::{Length, Rel};
|
||||||
use crate::math::Mathy;
|
use crate::math::Mathy;
|
||||||
use crate::text::TextElem;
|
|
||||||
|
|
||||||
/// Scales delimiters.
|
/// Scales delimiters.
|
||||||
///
|
///
|
||||||
@ -19,7 +18,7 @@ pub struct LrElem {
|
|||||||
#[parse(
|
#[parse(
|
||||||
let mut arguments = args.all::<Content>()?.into_iter();
|
let mut arguments = args.all::<Content>()?.into_iter();
|
||||||
let mut body = arguments.next().unwrap_or_default();
|
let mut body = arguments.next().unwrap_or_default();
|
||||||
arguments.for_each(|arg| body += TextElem::packed(',') + arg);
|
arguments.for_each(|arg| body += SymbolElem::packed(',') + arg);
|
||||||
body
|
body
|
||||||
)]
|
)]
|
||||||
pub body: Content,
|
pub body: Content,
|
||||||
@ -125,9 +124,9 @@ fn delimited(
|
|||||||
) -> Content {
|
) -> Content {
|
||||||
let span = body.span();
|
let span = body.span();
|
||||||
let mut elem = LrElem::new(Content::sequence([
|
let mut elem = LrElem::new(Content::sequence([
|
||||||
TextElem::packed(left),
|
SymbolElem::packed(left),
|
||||||
body,
|
body,
|
||||||
TextElem::packed(right),
|
SymbolElem::packed(right),
|
||||||
]));
|
]));
|
||||||
// Push size only if size is provided
|
// Push size only if size is provided
|
||||||
if let Some(size) = size {
|
if let Some(size) = size {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use ecow::EcoString;
|
use ecow::EcoString;
|
||||||
|
|
||||||
use crate::foundations::{elem, Content, NativeElement, Scope};
|
use crate::foundations::{elem, Content, NativeElement, Scope, SymbolElem};
|
||||||
use crate::layout::HElem;
|
use crate::layout::HElem;
|
||||||
use crate::math::{upright, Mathy, THIN};
|
use crate::math::{upright, Mathy, THIN};
|
||||||
use crate::text::TextElem;
|
use crate::text::TextElem;
|
||||||
@ -38,6 +38,7 @@ macro_rules! ops {
|
|||||||
let operator = EcoString::from(ops!(@name $name $(: $value)?));
|
let operator = EcoString::from(ops!(@name $name $(: $value)?));
|
||||||
math.define(
|
math.define(
|
||||||
stringify!($name),
|
stringify!($name),
|
||||||
|
// Latex also uses their equivalent of `TextElem` here.
|
||||||
OpElem::new(TextElem::new(operator).into())
|
OpElem::new(TextElem::new(operator).into())
|
||||||
.with_limits(ops!(@limit $($tts)*))
|
.with_limits(ops!(@limit $($tts)*))
|
||||||
.pack()
|
.pack()
|
||||||
@ -46,7 +47,7 @@ macro_rules! ops {
|
|||||||
|
|
||||||
let dif = |d| {
|
let dif = |d| {
|
||||||
HElem::new(THIN.into()).with_weak(true).pack()
|
HElem::new(THIN.into()).with_weak(true).pack()
|
||||||
+ upright(TextElem::packed(d))
|
+ upright(SymbolElem::packed(d))
|
||||||
};
|
};
|
||||||
math.define("dif", dif('d'));
|
math.define("dif", dif('d'));
|
||||||
math.define("Dif", dif('D'));
|
math.define("Dif", dif('D'));
|
||||||
|
@ -25,3 +25,7 @@
|
|||||||
// Test error numbering with dictionary rows.
|
// Test error numbering with dictionary rows.
|
||||||
// Error: 6-28 failed to parse CSV (found 3 instead of 2 fields in line 3)
|
// Error: 6-28 failed to parse CSV (found 3 instead of 2 fields in line 3)
|
||||||
#csv("/assets/data/bad.csv", row-type: dictionary)
|
#csv("/assets/data/bad.csv", row-type: dictionary)
|
||||||
|
|
||||||
|
--- csv-invalid-delimiter ---
|
||||||
|
// Error: 41-51 delimiter must be an ASCII character
|
||||||
|
#csv("/assets/data/zoo.csv", delimiter: "\u{2008}")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user