mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Add even
and odd
functions
This commit is contained in:
parent
e74ae6ce70
commit
c7f52ed048
@ -129,6 +129,8 @@ pub fn new() -> Scope {
|
|||||||
std.def_func("abs", abs);
|
std.def_func("abs", abs);
|
||||||
std.def_func("min", min);
|
std.def_func("min", min);
|
||||||
std.def_func("max", max);
|
std.def_func("max", max);
|
||||||
|
std.def_func("even", even);
|
||||||
|
std.def_func("odd", odd);
|
||||||
std.def_func("range", range);
|
std.def_func("range", range);
|
||||||
std.def_func("rgb", rgb);
|
std.def_func("rgb", rgb);
|
||||||
std.def_func("lower", lower);
|
std.def_func("lower", lower);
|
||||||
|
@ -22,7 +22,7 @@ impl<S: ShapeKind> ShapeNode<S> {
|
|||||||
pub const STROKE: Smart<Option<Paint>> = Smart::Auto;
|
pub const STROKE: Smart<Option<Paint>> = Smart::Auto;
|
||||||
/// The stroke's thickness.
|
/// The stroke's thickness.
|
||||||
pub const THICKNESS: Length = Length::pt(1.0);
|
pub const THICKNESS: Length = Length::pt(1.0);
|
||||||
/// The How much to pad the shape's content.
|
/// How much to pad the shape's content.
|
||||||
pub const PADDING: Linear = Linear::zero();
|
pub const PADDING: Linear = Linear::zero();
|
||||||
|
|
||||||
fn construct(_: &mut EvalContext, args: &mut Args) -> TypResult<Node> {
|
fn construct(_: &mut EvalContext, args: &mut Args) -> TypResult<Node> {
|
||||||
|
@ -135,6 +135,16 @@ pub fn max(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
|
|||||||
minmax(args, Ordering::Greater)
|
minmax(args, Ordering::Greater)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Whether an integer is even.
|
||||||
|
pub fn even(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
|
||||||
|
Ok(Value::Bool(args.expect::<i64>("integer")? % 2 == 0))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether an integer is odd.
|
||||||
|
pub fn odd(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
|
||||||
|
Ok(Value::Bool(args.expect::<i64>("integer")? % 2 != 0))
|
||||||
|
}
|
||||||
|
|
||||||
/// Find the minimum or maximum of a sequence of values.
|
/// Find the minimum or maximum of a sequence of values.
|
||||||
fn minmax(args: &mut Args, goal: Ordering) -> TypResult<Value> {
|
fn minmax(args: &mut Args, goal: Ordering) -> TypResult<Value> {
|
||||||
let mut extremum = args.expect::<Value>("value")?;
|
let mut extremum = args.expect::<Value>("value")?;
|
||||||
|
@ -11,6 +11,13 @@
|
|||||||
#test(abs(-12pt), 12pt)
|
#test(abs(-12pt), 12pt)
|
||||||
#test(abs(50%), 50%)
|
#test(abs(50%), 50%)
|
||||||
|
|
||||||
|
---
|
||||||
|
// Test the `even` and `odd` functions.
|
||||||
|
#test(even(2), true)
|
||||||
|
#test(odd(2), false)
|
||||||
|
#test(odd(-1), true)
|
||||||
|
#test(even(-11), false)
|
||||||
|
|
||||||
---
|
---
|
||||||
// Error: 6-16 cannot take absolute value of a linear
|
// Error: 6-16 cannot take absolute value of a linear
|
||||||
#abs(10pt + 50%)
|
#abs(10pt + 50%)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user