mirror of
https://github.com/typst/typst
synced 2025-05-18 11:05:28 +08:00
Remove unused template name field
This commit is contained in:
parent
285c2f617b
commit
784018124d
@ -55,11 +55,11 @@ pub enum Value {
|
|||||||
|
|
||||||
impl Value {
|
impl Value {
|
||||||
/// Create a new template value consisting of a single dynamic node.
|
/// Create a new template value consisting of a single dynamic node.
|
||||||
pub fn template<F>(name: impl Into<String>, f: F) -> Self
|
pub fn template<F>(f: F) -> Self
|
||||||
where
|
where
|
||||||
F: Fn(&mut ExecContext) + 'static,
|
F: Fn(&mut ExecContext) + 'static,
|
||||||
{
|
{
|
||||||
Self::Template(vec![TemplateNode::Func(TemplateFunc::new(name, f))])
|
Self::Template(vec![TemplateNode::Func(TemplateFunc::new(f))])
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The name of the stored value's type.
|
/// The name of the stored value's type.
|
||||||
@ -193,23 +193,15 @@ pub type ExprMap = HashMap<*const Expr, Value>;
|
|||||||
/// A reference-counted dynamic template node that can implement custom
|
/// A reference-counted dynamic template node that can implement custom
|
||||||
/// behaviour.
|
/// behaviour.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct TemplateFunc {
|
pub struct TemplateFunc(Rc<dyn Fn(&mut ExecContext)>);
|
||||||
name: String,
|
|
||||||
f: Rc<dyn Fn(&mut ExecContext)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TemplateFunc {
|
impl TemplateFunc {
|
||||||
/// Create a new function template from a rust function or closure.
|
/// Create a new function template from a rust function or closure.
|
||||||
pub fn new<F>(name: impl Into<String>, f: F) -> Self
|
pub fn new<F>(f: F) -> Self
|
||||||
where
|
where
|
||||||
F: Fn(&mut ExecContext) + 'static,
|
F: Fn(&mut ExecContext) + 'static,
|
||||||
{
|
{
|
||||||
Self { name: name.into(), f: Rc::new(f) }
|
Self(Rc::new(f))
|
||||||
}
|
|
||||||
|
|
||||||
/// The name of the template node.
|
|
||||||
pub fn name(&self) -> &str {
|
|
||||||
&self.name
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,7 +216,7 @@ impl Deref for TemplateFunc {
|
|||||||
type Target = dyn Fn(&mut ExecContext);
|
type Target = dyn Fn(&mut ExecContext);
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
self.f.as_ref()
|
self.0.as_ref()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ pub fn image(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Value::template("image", move |ctx| {
|
Value::template(move |ctx| {
|
||||||
if let Some(node) = node {
|
if let Some(node) = node {
|
||||||
ctx.push_into_par(node);
|
ctx.push_into_par(node);
|
||||||
}
|
}
|
||||||
@ -37,8 +37,8 @@ pub fn rect(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
|||||||
let width = args.named(ctx, "width");
|
let width = args.named(ctx, "width");
|
||||||
let height = args.named(ctx, "height");
|
let height = args.named(ctx, "height");
|
||||||
let fill = args.named(ctx, "fill");
|
let fill = args.named(ctx, "fill");
|
||||||
let body = args.eat::<TemplateValue>(ctx).unwrap_or_default();
|
let body = args.eat(ctx).unwrap_or_default();
|
||||||
rect_impl("rect", width, height, None, fill, body)
|
rect_impl(width, height, None, fill, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `square`: A square with optional content.
|
/// `square`: A square with optional content.
|
||||||
@ -47,19 +47,18 @@ pub fn square(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
|||||||
let width = length.or_else(|| args.named(ctx, "width"));
|
let width = length.or_else(|| args.named(ctx, "width"));
|
||||||
let height = width.is_none().then(|| args.named(ctx, "height")).flatten();
|
let height = width.is_none().then(|| args.named(ctx, "height")).flatten();
|
||||||
let fill = args.named(ctx, "fill");
|
let fill = args.named(ctx, "fill");
|
||||||
let body = args.eat::<TemplateValue>(ctx).unwrap_or_default();
|
let body = args.eat(ctx).unwrap_or_default();
|
||||||
rect_impl("square", width, height, Some(N64::from(1.0)), fill, body)
|
rect_impl(width, height, Some(N64::from(1.0)), fill, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rect_impl(
|
fn rect_impl(
|
||||||
name: &str,
|
|
||||||
width: Option<Linear>,
|
width: Option<Linear>,
|
||||||
height: Option<Linear>,
|
height: Option<Linear>,
|
||||||
aspect: Option<N64>,
|
aspect: Option<N64>,
|
||||||
fill: Option<Color>,
|
fill: Option<Color>,
|
||||||
body: TemplateValue,
|
body: TemplateValue,
|
||||||
) -> Value {
|
) -> Value {
|
||||||
Value::template(name, move |ctx| {
|
Value::template(move |ctx| {
|
||||||
let mut stack = ctx.exec_template_stack(&body);
|
let mut stack = ctx.exec_template_stack(&body);
|
||||||
stack.aspect = aspect;
|
stack.aspect = aspect;
|
||||||
|
|
||||||
@ -82,8 +81,8 @@ pub fn ellipse(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
|||||||
let width = args.named(ctx, "width");
|
let width = args.named(ctx, "width");
|
||||||
let height = args.named(ctx, "height");
|
let height = args.named(ctx, "height");
|
||||||
let fill = args.named(ctx, "fill");
|
let fill = args.named(ctx, "fill");
|
||||||
let body = args.eat::<TemplateValue>(ctx).unwrap_or_default();
|
let body = args.eat(ctx).unwrap_or_default();
|
||||||
ellipse_impl("ellipse", width, height, None, fill, body)
|
ellipse_impl(width, height, None, fill, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `circle`: A circle with optional content.
|
/// `circle`: A circle with optional content.
|
||||||
@ -92,19 +91,18 @@ pub fn circle(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
|||||||
let width = radius.or_else(|| args.named(ctx, "width"));
|
let width = radius.or_else(|| args.named(ctx, "width"));
|
||||||
let height = width.is_none().then(|| args.named(ctx, "height")).flatten();
|
let height = width.is_none().then(|| args.named(ctx, "height")).flatten();
|
||||||
let fill = args.named(ctx, "fill");
|
let fill = args.named(ctx, "fill");
|
||||||
let body = args.eat::<TemplateValue>(ctx).unwrap_or_default();
|
let body = args.eat(ctx).unwrap_or_default();
|
||||||
ellipse_impl("circle", width, height, Some(N64::from(1.0)), fill, body)
|
ellipse_impl(width, height, Some(N64::from(1.0)), fill, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ellipse_impl(
|
fn ellipse_impl(
|
||||||
name: &str,
|
|
||||||
width: Option<Linear>,
|
width: Option<Linear>,
|
||||||
height: Option<Linear>,
|
height: Option<Linear>,
|
||||||
aspect: Option<N64>,
|
aspect: Option<N64>,
|
||||||
fill: Option<Color>,
|
fill: Option<Color>,
|
||||||
body: TemplateValue,
|
body: TemplateValue,
|
||||||
) -> Value {
|
) -> Value {
|
||||||
Value::template(name, move |ctx| {
|
Value::template(move |ctx| {
|
||||||
// This padding ratio ensures that the rectangular padded region fits
|
// This padding ratio ensures that the rectangular padded region fits
|
||||||
// perfectly into the ellipse.
|
// perfectly into the ellipse.
|
||||||
const PAD: f64 = 0.5 - SQRT_2 / 4.0;
|
const PAD: f64 = 0.5 - SQRT_2 / 4.0;
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::layout::{GridNode, PadNode, StackChild, StackNode, TrackSizing};
|
use crate::layout::{FixedNode, GridNode, PadNode, StackChild, StackNode, TrackSizing};
|
||||||
use crate::paper::{Paper, PaperClass};
|
use crate::paper::{Paper, PaperClass};
|
||||||
|
|
||||||
/// `page`: Configure pages.
|
/// `page`: Configure pages.
|
||||||
pub fn page(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
pub fn page(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
||||||
|
let span = args.span;
|
||||||
let paper = args.eat::<Spanned<String>>(ctx).and_then(|name| {
|
let paper = args.eat::<Spanned<String>>(ctx).and_then(|name| {
|
||||||
Paper::from_name(&name.v).or_else(|| {
|
Paper::from_name(&name.v).or_else(|| {
|
||||||
ctx.diag(error!(name.span, "invalid paper name"));
|
ctx.diag(error!(name.span, "invalid paper name"));
|
||||||
@ -20,9 +21,8 @@ pub fn page(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
|||||||
let bottom = args.named(ctx, "bottom");
|
let bottom = args.named(ctx, "bottom");
|
||||||
let flip = args.named(ctx, "flip");
|
let flip = args.named(ctx, "flip");
|
||||||
let body = args.eat::<TemplateValue>(ctx);
|
let body = args.eat::<TemplateValue>(ctx);
|
||||||
let span = args.span;
|
|
||||||
|
|
||||||
Value::template("page", move |ctx| {
|
Value::template(move |ctx| {
|
||||||
let snapshot = ctx.state.clone();
|
let snapshot = ctx.state.clone();
|
||||||
|
|
||||||
if let Some(paper) = paper {
|
if let Some(paper) = paper {
|
||||||
@ -79,29 +79,24 @@ pub fn page(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
|||||||
/// `pagebreak`: Start a new page.
|
/// `pagebreak`: Start a new page.
|
||||||
pub fn pagebreak(_: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
pub fn pagebreak(_: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
||||||
let span = args.span;
|
let span = args.span;
|
||||||
Value::template("pagebreak", move |ctx| {
|
Value::template(move |ctx| {
|
||||||
ctx.pagebreak(true, true, span);
|
ctx.pagebreak(true, true, span);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `h`: Horizontal spacing.
|
/// `h`: Horizontal spacing.
|
||||||
pub fn h(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
pub fn h(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
||||||
spacing_impl("h", ctx, args, GenAxis::Cross)
|
spacing_impl(ctx, args, GenAxis::Cross)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `v`: Vertical spacing.
|
/// `v`: Vertical spacing.
|
||||||
pub fn v(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
pub fn v(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
||||||
spacing_impl("v", ctx, args, GenAxis::Main)
|
spacing_impl(ctx, args, GenAxis::Main)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn spacing_impl(
|
fn spacing_impl(ctx: &mut EvalContext, args: &mut FuncArgs, axis: GenAxis) -> Value {
|
||||||
name: &str,
|
|
||||||
ctx: &mut EvalContext,
|
|
||||||
args: &mut FuncArgs,
|
|
||||||
axis: GenAxis,
|
|
||||||
) -> Value {
|
|
||||||
let spacing: Option<Linear> = args.expect(ctx, "spacing");
|
let spacing: Option<Linear> = args.expect(ctx, "spacing");
|
||||||
Value::template(name, move |ctx| {
|
Value::template(move |ctx| {
|
||||||
if let Some(linear) = spacing {
|
if let Some(linear) = spacing {
|
||||||
// TODO: Should this really always be font-size relative?
|
// TODO: Should this really always be font-size relative?
|
||||||
let amount = linear.resolve(ctx.state.font.size);
|
let amount = linear.resolve(ctx.state.font.size);
|
||||||
@ -130,7 +125,7 @@ pub fn align(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Value::template("align", move |ctx| {
|
Value::template(move |ctx| {
|
||||||
let snapshot = ctx.state.clone();
|
let snapshot = ctx.state.clone();
|
||||||
|
|
||||||
if let Some(horizontal) = horizontal {
|
if let Some(horizontal) = horizontal {
|
||||||
@ -222,7 +217,7 @@ pub fn pad(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
|||||||
let top = args.named(ctx, "top");
|
let top = args.named(ctx, "top");
|
||||||
let right = args.named(ctx, "right");
|
let right = args.named(ctx, "right");
|
||||||
let bottom = args.named(ctx, "bottom");
|
let bottom = args.named(ctx, "bottom");
|
||||||
let body = args.expect::<TemplateValue>(ctx, "body").unwrap_or_default();
|
let body = args.expect(ctx, "body").unwrap_or_default();
|
||||||
|
|
||||||
let padding = Sides::new(
|
let padding = Sides::new(
|
||||||
left.or(all).unwrap_or_default(),
|
left.or(all).unwrap_or_default(),
|
||||||
@ -231,7 +226,7 @@ pub fn pad(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
|||||||
bottom.or(all).unwrap_or_default(),
|
bottom.or(all).unwrap_or_default(),
|
||||||
);
|
);
|
||||||
|
|
||||||
Value::template("pad", move |ctx| {
|
Value::template(move |ctx| {
|
||||||
let child = ctx.exec_template_stack(&body).into();
|
let child = ctx.exec_template_stack(&body).into();
|
||||||
ctx.push_into_stack(PadNode { padding, child });
|
ctx.push_into_stack(PadNode { padding, child });
|
||||||
})
|
})
|
||||||
@ -240,9 +235,9 @@ pub fn pad(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
|||||||
/// `stack`: Stack children along an axis.
|
/// `stack`: Stack children along an axis.
|
||||||
pub fn stack(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
pub fn stack(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
||||||
let dir = args.named::<Dir>(ctx, "dir").unwrap_or(Dir::TTB);
|
let dir = args.named::<Dir>(ctx, "dir").unwrap_or(Dir::TTB);
|
||||||
let children = args.all::<TemplateValue>(ctx);
|
let children = args.all(ctx);
|
||||||
|
|
||||||
Value::template("stack", move |ctx| {
|
Value::template(move |ctx| {
|
||||||
let children = children
|
let children = children
|
||||||
.iter()
|
.iter()
|
||||||
.map(|child| {
|
.map(|child| {
|
||||||
@ -271,9 +266,9 @@ pub fn grid(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
|||||||
let gutter_rows = args.named::<Tracks>(ctx, "gutter-rows");
|
let gutter_rows = args.named::<Tracks>(ctx, "gutter-rows");
|
||||||
let column_dir = args.named(ctx, "column-dir");
|
let column_dir = args.named(ctx, "column-dir");
|
||||||
let row_dir = args.named(ctx, "row-dir");
|
let row_dir = args.named(ctx, "row-dir");
|
||||||
let children = args.all::<TemplateValue>(ctx);
|
let children = args.all(ctx);
|
||||||
|
|
||||||
Value::template("grid", move |ctx| {
|
Value::template(move |ctx| {
|
||||||
let children = children
|
let children = children
|
||||||
.iter()
|
.iter()
|
||||||
.map(|child| ctx.exec_template_stack(child).into())
|
.map(|child| ctx.exec_template_stack(child).into())
|
||||||
|
@ -19,7 +19,7 @@ pub fn font(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
|||||||
let monospace = args.named(ctx, "monospace");
|
let monospace = args.named(ctx, "monospace");
|
||||||
let body = args.eat::<TemplateValue>(ctx);
|
let body = args.eat::<TemplateValue>(ctx);
|
||||||
|
|
||||||
Value::template("font", move |ctx| {
|
Value::template(move |ctx| {
|
||||||
let snapshot = ctx.state.clone();
|
let snapshot = ctx.state.clone();
|
||||||
let font = ctx.state.font_mut();
|
let font = ctx.state.font_mut();
|
||||||
|
|
||||||
@ -162,7 +162,7 @@ pub fn par(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
|||||||
let leading = args.named(ctx, "leading");
|
let leading = args.named(ctx, "leading");
|
||||||
let word_spacing = args.named(ctx, "word-spacing");
|
let word_spacing = args.named(ctx, "word-spacing");
|
||||||
|
|
||||||
Value::template("par", move |ctx| {
|
Value::template(move |ctx| {
|
||||||
if let Some(spacing) = spacing {
|
if let Some(spacing) = spacing {
|
||||||
ctx.state.par.spacing = spacing;
|
ctx.state.par.spacing = spacing;
|
||||||
}
|
}
|
||||||
@ -191,7 +191,7 @@ pub fn lang(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
|||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
Value::template("lang", move |ctx| {
|
Value::template(move |ctx| {
|
||||||
if let Some(dir) = dir.or(iso) {
|
if let Some(dir) = dir.or(iso) {
|
||||||
ctx.state.lang.dir = dir;
|
ctx.state.lang.dir = dir;
|
||||||
}
|
}
|
||||||
@ -210,21 +210,20 @@ fn lang_dir(iso: &str) -> Dir {
|
|||||||
|
|
||||||
/// `strike`: Enable striken-through text.
|
/// `strike`: Enable striken-through text.
|
||||||
pub fn strike(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
pub fn strike(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
||||||
line_impl("strike", ctx, args, |font| &mut font.strikethrough)
|
line_impl(ctx, args, |font| &mut font.strikethrough)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `underline`: Enable underlined text.
|
/// `underline`: Enable underlined text.
|
||||||
pub fn underline(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
pub fn underline(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
||||||
line_impl("underline", ctx, args, |font| &mut font.underline)
|
line_impl(ctx, args, |font| &mut font.underline)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `overline`: Add an overline above text.
|
/// `overline`: Add an overline above text.
|
||||||
pub fn overline(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
pub fn overline(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
|
||||||
line_impl("overline", ctx, args, |font| &mut font.overline)
|
line_impl(ctx, args, |font| &mut font.overline)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn line_impl(
|
fn line_impl(
|
||||||
name: &str,
|
|
||||||
ctx: &mut EvalContext,
|
ctx: &mut EvalContext,
|
||||||
args: &mut FuncArgs,
|
args: &mut FuncArgs,
|
||||||
substate: fn(&mut FontState) -> &mut Option<Rc<LineState>>,
|
substate: fn(&mut FontState) -> &mut Option<Rc<LineState>>,
|
||||||
@ -245,7 +244,7 @@ fn line_impl(
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
Value::template(name, move |ctx| {
|
Value::template(move |ctx| {
|
||||||
let snapshot = ctx.state.clone();
|
let snapshot = ctx.state.clone();
|
||||||
|
|
||||||
*substate(ctx.state.font_mut()) = state.clone();
|
*substate(ctx.state.font_mut()) = state.clone();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user