Allow a function as an argument to size
in stretch
and lr
Previously there was always a short fall when scaling delimiters, even if the user requested a specific size. This is no longer the case; the short fall is only present in the default for `lr` (`x => x - 0.1em`), and is the reason for the updated tests - the size of the delimiters is now actually what was specified in the size argument. This also makes the default for `lr` much clearer to the user. A slight hack was used by exploiting the `name` property in the `func` attribute macro so that the default value in the docs for `lr.size` would clearly show what the default function was (instead of just its name `default_lr_size` which is meaningless and inaccessible to the user).
@ -1,8 +1,9 @@
|
|||||||
use typst_library::diag::SourceResult;
|
use typst_library::diag::SourceResult;
|
||||||
use typst_library::foundations::{Packed, StyleChain, SymbolElem};
|
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, Size};
|
||||||
use typst_library::math::{
|
use typst_library::math::{
|
||||||
AttachElem, EquationElem, LimitsElem, PrimesElem, ScriptsElem, StretchElem,
|
AttachElem, EquationElem, LimitsElem, PrimesElem, ScriptsElem, StretchElem,
|
||||||
|
StretchSize,
|
||||||
};
|
};
|
||||||
use typst_utils::OptionExt;
|
use typst_utils::OptionExt;
|
||||||
|
|
||||||
@ -63,16 +64,9 @@ pub fn layout_attach(
|
|||||||
let t = layout!(t, sup_style_chain)?;
|
let t = layout!(t, sup_style_chain)?;
|
||||||
let b = layout!(b, sub_style_chain)?;
|
let b = layout!(b, sub_style_chain)?;
|
||||||
if let Some(stretch) = stretch {
|
if let Some(stretch) = stretch {
|
||||||
let relative_to_width = measure!(t, width).max(measure!(b, width));
|
let relative_to = measure!(t, width).max(measure!(b, width));
|
||||||
stretch_fragment(
|
let width = stretch.resolve(ctx.engine, styles, relative_to)?;
|
||||||
ctx,
|
stretch_fragment(ctx, styles, &mut base, Axis::X, width);
|
||||||
styles,
|
|
||||||
&mut base,
|
|
||||||
Some(Axis::X),
|
|
||||||
Some(relative_to_width),
|
|
||||||
stretch,
|
|
||||||
Abs::zero(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let fragments = [
|
let fragments = [
|
||||||
@ -155,7 +149,7 @@ pub fn layout_limits(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get the size to stretch the base to.
|
/// Get the size to stretch the base to.
|
||||||
fn stretch_size(styles: StyleChain, elem: &Packed<AttachElem>) -> Option<Rel<Abs>> {
|
fn stretch_size(styles: StyleChain, elem: &Packed<AttachElem>) -> Option<StretchSize> {
|
||||||
// Extract from an EquationElem.
|
// Extract from an EquationElem.
|
||||||
let mut base = &elem.base;
|
let mut base = &elem.base;
|
||||||
while let Some(equation) = base.to_packed::<EquationElem>() {
|
while let Some(equation) = base.to_packed::<EquationElem>() {
|
||||||
|
@ -305,7 +305,7 @@ impl GlyphFragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Apply GSUB substitutions.
|
/// Apply GSUB substitutions.
|
||||||
fn adjust_glyph_index(ctx: &MathContext, id: GlyphId) -> GlyphId {
|
pub fn adjust_glyph_index(ctx: &MathContext, id: GlyphId) -> GlyphId {
|
||||||
if let Some(glyphwise_tables) = &ctx.glyphwise_tables {
|
if let Some(glyphwise_tables) = &ctx.glyphwise_tables {
|
||||||
glyphwise_tables.iter().fold(id, |id, table| table.apply(id))
|
glyphwise_tables.iter().fold(id, |id, table| table.apply(id))
|
||||||
} else {
|
} else {
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
use typst_library::diag::SourceResult;
|
use typst_library::diag::SourceResult;
|
||||||
use typst_library::foundations::{Packed, StyleChain};
|
use typst_library::foundations::{Packed, StyleChain};
|
||||||
use typst_library::layout::{Abs, Axis, Rel};
|
use typst_library::layout::{Abs, Axis};
|
||||||
use typst_library::math::{EquationElem, LrElem, MidElem};
|
use typst_library::math::{EquationElem, LrElem, MidElem};
|
||||||
use typst_utils::SliceExt;
|
use typst_utils::SliceExt;
|
||||||
use unicode_math_class::MathClass;
|
use unicode_math_class::MathClass;
|
||||||
|
|
||||||
use super::{stretch_fragment, MathContext, MathFragment, DELIM_SHORT_FALL};
|
use super::{stretch_fragment, MathContext, MathFragment};
|
||||||
|
|
||||||
/// Lays out an [`LrElem`].
|
/// Lays out an [`LrElem`].
|
||||||
#[typst_macros::time(name = "math.lr", span = elem.span())]
|
#[typst_macros::time(name = "math.lr", span = elem.span())]
|
||||||
@ -22,7 +22,7 @@ pub fn layout_lr(
|
|||||||
|
|
||||||
// Extract implicit LrElem.
|
// Extract implicit LrElem.
|
||||||
if let Some(lr) = body.to_packed::<LrElem>() {
|
if let Some(lr) = body.to_packed::<LrElem>() {
|
||||||
if lr.size(styles).is_one() {
|
if lr.size(styles).is_lr_default() {
|
||||||
body = &lr.body;
|
body = &lr.body;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -41,14 +41,14 @@ pub fn layout_lr(
|
|||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
let relative_to = 2.0 * max_extent;
|
let relative_to = 2.0 * max_extent;
|
||||||
let height = elem.size(styles);
|
let height = elem.size(styles).resolve(ctx.engine, styles, relative_to)?;
|
||||||
|
|
||||||
// Scale up fragments at both ends.
|
// Scale up fragments at both ends.
|
||||||
match inner_fragments {
|
match inner_fragments {
|
||||||
[one] => scale(ctx, styles, one, relative_to, height, None),
|
[one] => scale(ctx, styles, one, height, None),
|
||||||
[first, .., last] => {
|
[first, .., last] => {
|
||||||
scale(ctx, styles, first, relative_to, height, Some(MathClass::Opening));
|
scale(ctx, styles, first, height, Some(MathClass::Opening));
|
||||||
scale(ctx, styles, last, relative_to, height, Some(MathClass::Closing));
|
scale(ctx, styles, last, height, Some(MathClass::Closing));
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
@ -58,7 +58,7 @@ pub fn layout_lr(
|
|||||||
if let MathFragment::Variant(ref mut variant) = fragment {
|
if let MathFragment::Variant(ref mut variant) = fragment {
|
||||||
if variant.mid_stretched == Some(false) {
|
if variant.mid_stretched == Some(false) {
|
||||||
variant.mid_stretched = Some(true);
|
variant.mid_stretched = Some(true);
|
||||||
scale(ctx, styles, fragment, relative_to, height, Some(MathClass::Large));
|
scale(ctx, styles, fragment, height, Some(MathClass::Large));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -119,26 +119,14 @@ fn scale(
|
|||||||
ctx: &mut MathContext,
|
ctx: &mut MathContext,
|
||||||
styles: StyleChain,
|
styles: StyleChain,
|
||||||
fragment: &mut MathFragment,
|
fragment: &mut MathFragment,
|
||||||
relative_to: Abs,
|
height: Abs,
|
||||||
height: Rel<Abs>,
|
|
||||||
apply: Option<MathClass>,
|
apply: Option<MathClass>,
|
||||||
) {
|
) {
|
||||||
if matches!(
|
if matches!(
|
||||||
fragment.class(),
|
fragment.class(),
|
||||||
MathClass::Opening | MathClass::Closing | MathClass::Fence
|
MathClass::Opening | MathClass::Closing | MathClass::Fence
|
||||||
) {
|
) {
|
||||||
// This unwrap doesn't really matter. If it is None, then the fragment
|
stretch_fragment(ctx, styles, fragment, Axis::Y, height);
|
||||||
// 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,
|
|
||||||
);
|
|
||||||
|
|
||||||
if let Some(class) = apply {
|
if let Some(class) = apply {
|
||||||
fragment.set_class(class);
|
fragment.set_class(class);
|
||||||
|
@ -2,7 +2,7 @@ use ttf_parser::math::{GlyphAssembly, GlyphConstruction, GlyphPart};
|
|||||||
use ttf_parser::LazyArray16;
|
use ttf_parser::LazyArray16;
|
||||||
use typst_library::diag::{warning, SourceResult};
|
use typst_library::diag::{warning, SourceResult};
|
||||||
use typst_library::foundations::{Packed, StyleChain};
|
use typst_library::foundations::{Packed, StyleChain};
|
||||||
use typst_library::layout::{Abs, Axis, Frame, Point, Rel, Size};
|
use typst_library::layout::{Abs, Axis, Frame, Point, Size};
|
||||||
use typst_library::math::StretchElem;
|
use typst_library::math::StretchElem;
|
||||||
use typst_utils::Get;
|
use typst_utils::Get;
|
||||||
|
|
||||||
@ -23,15 +23,13 @@ pub fn layout_stretch(
|
|||||||
styles: StyleChain,
|
styles: StyleChain,
|
||||||
) -> SourceResult<()> {
|
) -> SourceResult<()> {
|
||||||
let mut fragment = ctx.layout_into_fragment(&elem.body, styles)?;
|
let mut fragment = ctx.layout_into_fragment(&elem.body, styles)?;
|
||||||
stretch_fragment(
|
|
||||||
ctx,
|
if let Some(axis) = stretch_axis(ctx, &fragment) {
|
||||||
styles,
|
let relative_to = fragment.size().get(axis);
|
||||||
&mut fragment,
|
let target = elem.size(styles).resolve(ctx.engine, styles, relative_to)?;
|
||||||
None,
|
stretch_fragment(ctx, styles, &mut fragment, axis, target);
|
||||||
None,
|
}
|
||||||
elem.size(styles),
|
|
||||||
Abs::zero(),
|
|
||||||
);
|
|
||||||
ctx.push(fragment);
|
ctx.push(fragment);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -41,10 +39,8 @@ pub fn stretch_fragment(
|
|||||||
ctx: &mut MathContext,
|
ctx: &mut MathContext,
|
||||||
styles: StyleChain,
|
styles: StyleChain,
|
||||||
fragment: &mut MathFragment,
|
fragment: &mut MathFragment,
|
||||||
axis: Option<Axis>,
|
axis: Axis,
|
||||||
relative_to: Option<Abs>,
|
target: Abs,
|
||||||
stretch: Rel<Abs>,
|
|
||||||
short_fall: Abs,
|
|
||||||
) {
|
) {
|
||||||
let glyph = match fragment {
|
let glyph = match fragment {
|
||||||
MathFragment::Glyph(glyph) => glyph.clone(),
|
MathFragment::Glyph(glyph) => glyph.clone(),
|
||||||
@ -56,21 +52,12 @@ pub fn stretch_fragment(
|
|||||||
|
|
||||||
// Return if we attempt to stretch along an axis which isn't stretchable,
|
// Return if we attempt to stretch along an axis which isn't stretchable,
|
||||||
// so that the original fragment isn't modified.
|
// so that the original fragment isn't modified.
|
||||||
let Some(stretch_axis) = stretch_axis(ctx, &glyph) else { return };
|
let Some(stretch_axis) = stretch_axis(ctx, fragment) else { return };
|
||||||
let axis = axis.unwrap_or(stretch_axis);
|
|
||||||
if axis != stretch_axis {
|
if axis != stretch_axis {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let relative_to_size = relative_to.unwrap_or_else(|| fragment.size().get(axis));
|
let mut variant = stretch_glyph(ctx, glyph, target, Abs::zero(), axis);
|
||||||
|
|
||||||
let mut variant = stretch_glyph(
|
|
||||||
ctx,
|
|
||||||
glyph,
|
|
||||||
stretch.relative_to(relative_to_size),
|
|
||||||
short_fall,
|
|
||||||
axis,
|
|
||||||
);
|
|
||||||
|
|
||||||
if axis == Axis::Y {
|
if axis == Axis::Y {
|
||||||
variant.align_on_axis(ctx, delimiter_alignment(variant.c));
|
variant.align_on_axis(ctx, delimiter_alignment(variant.c));
|
||||||
@ -81,17 +68,26 @@ pub fn stretch_fragment(
|
|||||||
|
|
||||||
/// Return whether the glyph is stretchable and if it is, along which axis it
|
/// Return whether the glyph is stretchable and if it is, along which axis it
|
||||||
/// can be stretched.
|
/// can be stretched.
|
||||||
fn stretch_axis(ctx: &mut MathContext, base: &GlyphFragment) -> Option<Axis> {
|
fn stretch_axis(ctx: &mut MathContext, fragment: &MathFragment) -> Option<Axis> {
|
||||||
let base_id = base.id;
|
let (id, span) = match fragment {
|
||||||
|
MathFragment::Glyph(glyph) => (glyph.id, glyph.span),
|
||||||
|
MathFragment::Variant(variant) => {
|
||||||
|
let id = ctx.ttf.glyph_index(variant.c).unwrap_or_default();
|
||||||
|
let id = GlyphFragment::adjust_glyph_index(ctx, id);
|
||||||
|
(id, variant.span)
|
||||||
|
}
|
||||||
|
_ => return None,
|
||||||
|
};
|
||||||
|
|
||||||
let vertical = ctx
|
let vertical = ctx
|
||||||
.table
|
.table
|
||||||
.variants
|
.variants
|
||||||
.and_then(|variants| variants.vertical_constructions.get(base_id))
|
.and_then(|variants| variants.vertical_constructions.get(id))
|
||||||
.map(|_| Axis::Y);
|
.map(|_| Axis::Y);
|
||||||
let horizontal = ctx
|
let horizontal = ctx
|
||||||
.table
|
.table
|
||||||
.variants
|
.variants
|
||||||
.and_then(|variants| variants.horizontal_constructions.get(base_id))
|
.and_then(|variants| variants.horizontal_constructions.get(id))
|
||||||
.map(|_| Axis::X);
|
.map(|_| Axis::X);
|
||||||
|
|
||||||
match (vertical, horizontal) {
|
match (vertical, horizontal) {
|
||||||
@ -102,7 +98,7 @@ fn stretch_axis(ctx: &mut MathContext, base: &GlyphFragment) -> Option<Axis> {
|
|||||||
// vertical and horizontal constructions. So for the time being, we
|
// vertical and horizontal constructions. So for the time being, we
|
||||||
// will assume that a glyph cannot have both.
|
// will assume that a glyph cannot have both.
|
||||||
ctx.engine.sink.warn(warning!(
|
ctx.engine.sink.warn(warning!(
|
||||||
base.span,
|
span,
|
||||||
"glyph has both vertical and horizontal constructions";
|
"glyph has both vertical and horizontal constructions";
|
||||||
hint: "this is probably a font bug";
|
hint: "this is probably a font bug";
|
||||||
hint: "please file an issue at https://github.com/typst/typst/issues"
|
hint: "please file an issue at https://github.com/typst/typst/issues"
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
use crate::foundations::{elem, Content, Packed};
|
use crate::foundations::{elem, Content, Packed};
|
||||||
use crate::layout::{Length, Rel};
|
|
||||||
use crate::math::{EquationElem, Mathy};
|
use crate::math::{EquationElem, Mathy};
|
||||||
|
|
||||||
/// A base with optional attachments.
|
/// A base with optional attachments.
|
||||||
@ -128,31 +127,3 @@ pub struct LimitsElem {
|
|||||||
#[default(true)]
|
#[default(true)]
|
||||||
pub inline: bool,
|
pub inline: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Stretches a glyph.
|
|
||||||
///
|
|
||||||
/// This function can also be used to automatically stretch the base of an
|
|
||||||
/// attachment, so that it fits the top and bottom attachments.
|
|
||||||
///
|
|
||||||
/// Note that only some glyphs can be stretched, and which ones can depend on
|
|
||||||
/// the math font being used. However, most math fonts are the same in this
|
|
||||||
/// regard.
|
|
||||||
///
|
|
||||||
/// ```example
|
|
||||||
/// $ H stretch(=)^"define" U + p V $
|
|
||||||
/// $ f : X stretch(->>, size: #150%)_"surjective" Y $
|
|
||||||
/// $ x stretch(harpoons.ltrb, size: #3em) y
|
|
||||||
/// stretch(\[, size: #150%) z $
|
|
||||||
/// ```
|
|
||||||
#[elem(Mathy)]
|
|
||||||
pub struct StretchElem {
|
|
||||||
/// The glyph to stretch.
|
|
||||||
#[required]
|
|
||||||
pub body: Content,
|
|
||||||
|
|
||||||
/// The size to stretch to, relative to the maximum size of the glyph and
|
|
||||||
/// its attachments.
|
|
||||||
#[resolve]
|
|
||||||
#[default(Rel::one())]
|
|
||||||
pub size: Rel<Length>,
|
|
||||||
}
|
|
||||||
|
@ -1,6 +1,16 @@
|
|||||||
use crate::foundations::{elem, func, Content, NativeElement, SymbolElem};
|
use crate::foundations::{elem, func, Content, NativeElement, NativeFunc, SymbolElem};
|
||||||
use crate::layout::{Length, Rel};
|
use crate::layout::{Em, Length, Ratio, Rel};
|
||||||
use crate::math::Mathy;
|
use crate::math::{Mathy, StretchSize};
|
||||||
|
|
||||||
|
const DELIM_SHORT_FALL: Em = Em::new(-0.1);
|
||||||
|
|
||||||
|
#[func(name = "x => x - 0.1em")]
|
||||||
|
pub const fn default_lr_size(base: Length) -> Rel {
|
||||||
|
Rel {
|
||||||
|
rel: Ratio::zero(),
|
||||||
|
abs: Length { abs: base.abs, em: DELIM_SHORT_FALL },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Scales delimiters.
|
/// Scales delimiters.
|
||||||
///
|
///
|
||||||
@ -8,10 +18,13 @@ use crate::math::Mathy;
|
|||||||
/// unmatched delimiters and to control the delimiter scaling more precisely.
|
/// unmatched delimiters and to control the delimiter scaling more precisely.
|
||||||
#[elem(title = "Left/Right", Mathy)]
|
#[elem(title = "Left/Right", Mathy)]
|
||||||
pub struct LrElem {
|
pub struct LrElem {
|
||||||
/// The size of the brackets, relative to the height of the wrapped content.
|
/// The size of the delimiters, relative to the height of the wrapped
|
||||||
#[resolve]
|
/// content.
|
||||||
#[default(Rel::one())]
|
///
|
||||||
pub size: Rel<Length>,
|
/// See the [stretch documentation]($math.stretch.size) for more
|
||||||
|
/// information on sizes.
|
||||||
|
#[default(<default_lr_size>::data().into())]
|
||||||
|
pub size: StretchSize,
|
||||||
|
|
||||||
/// The delimited content, including the delimiters.
|
/// The delimited content, including the delimiters.
|
||||||
#[required]
|
#[required]
|
||||||
@ -43,9 +56,13 @@ pub struct MidElem {
|
|||||||
/// ```
|
/// ```
|
||||||
#[func]
|
#[func]
|
||||||
pub fn floor(
|
pub fn floor(
|
||||||
/// The size of the brackets, relative to the height of the wrapped content.
|
/// The size of the delimiters, relative to the height of the wrapped
|
||||||
|
/// content.
|
||||||
|
///
|
||||||
|
/// See the [stretch documentation]($math.stretch.size) for more
|
||||||
|
/// information on sizes.
|
||||||
#[named]
|
#[named]
|
||||||
size: Option<Rel<Length>>,
|
size: Option<StretchSize>,
|
||||||
/// The expression to floor.
|
/// The expression to floor.
|
||||||
body: Content,
|
body: Content,
|
||||||
) -> Content {
|
) -> Content {
|
||||||
@ -59,9 +76,13 @@ pub fn floor(
|
|||||||
/// ```
|
/// ```
|
||||||
#[func]
|
#[func]
|
||||||
pub fn ceil(
|
pub fn ceil(
|
||||||
/// The size of the brackets, relative to the height of the wrapped content.
|
/// The size of the delimiters, relative to the height of the wrapped
|
||||||
|
/// content.
|
||||||
|
///
|
||||||
|
/// See the [stretch documentation]($math.stretch.size) for more
|
||||||
|
/// information on sizes.
|
||||||
#[named]
|
#[named]
|
||||||
size: Option<Rel<Length>>,
|
size: Option<StretchSize>,
|
||||||
/// The expression to ceil.
|
/// The expression to ceil.
|
||||||
body: Content,
|
body: Content,
|
||||||
) -> Content {
|
) -> Content {
|
||||||
@ -75,9 +96,13 @@ pub fn ceil(
|
|||||||
/// ```
|
/// ```
|
||||||
#[func]
|
#[func]
|
||||||
pub fn round(
|
pub fn round(
|
||||||
/// The size of the brackets, relative to the height of the wrapped content.
|
/// The size of the delimiters, relative to the height of the wrapped
|
||||||
|
/// content.
|
||||||
|
///
|
||||||
|
/// See the [stretch documentation]($math.stretch.size) for more
|
||||||
|
/// information on sizes.
|
||||||
#[named]
|
#[named]
|
||||||
size: Option<Rel<Length>>,
|
size: Option<StretchSize>,
|
||||||
/// The expression to round.
|
/// The expression to round.
|
||||||
body: Content,
|
body: Content,
|
||||||
) -> Content {
|
) -> Content {
|
||||||
@ -91,9 +116,13 @@ pub fn round(
|
|||||||
/// ```
|
/// ```
|
||||||
#[func]
|
#[func]
|
||||||
pub fn abs(
|
pub fn abs(
|
||||||
/// The size of the brackets, relative to the height of the wrapped content.
|
/// The size of the delimiters, relative to the height of the wrapped
|
||||||
|
/// content.
|
||||||
|
///
|
||||||
|
/// See the [stretch documentation]($math.stretch.size) for more
|
||||||
|
/// information on sizes.
|
||||||
#[named]
|
#[named]
|
||||||
size: Option<Rel<Length>>,
|
size: Option<StretchSize>,
|
||||||
/// The expression to take the absolute value of.
|
/// The expression to take the absolute value of.
|
||||||
body: Content,
|
body: Content,
|
||||||
) -> Content {
|
) -> Content {
|
||||||
@ -107,9 +136,13 @@ pub fn abs(
|
|||||||
/// ```
|
/// ```
|
||||||
#[func]
|
#[func]
|
||||||
pub fn norm(
|
pub fn norm(
|
||||||
/// The size of the brackets, relative to the height of the wrapped content.
|
/// The size of the delimiters, relative to the height of the wrapped
|
||||||
|
/// content.
|
||||||
|
///
|
||||||
|
/// See the [stretch documentation]($math.stretch.size) for more
|
||||||
|
/// information on sizes.
|
||||||
#[named]
|
#[named]
|
||||||
size: Option<Rel<Length>>,
|
size: Option<StretchSize>,
|
||||||
/// The expression to take the norm of.
|
/// The expression to take the norm of.
|
||||||
body: Content,
|
body: Content,
|
||||||
) -> Content {
|
) -> Content {
|
||||||
@ -120,7 +153,7 @@ fn delimited(
|
|||||||
body: Content,
|
body: Content,
|
||||||
left: char,
|
left: char,
|
||||||
right: char,
|
right: char,
|
||||||
size: Option<Rel<Length>>,
|
size: Option<StretchSize>,
|
||||||
) -> Content {
|
) -> Content {
|
||||||
let span = body.span();
|
let span = body.span();
|
||||||
let mut elem = LrElem::new(Content::sequence([
|
let mut elem = LrElem::new(Content::sequence([
|
||||||
|
@ -9,6 +9,7 @@ mod lr;
|
|||||||
mod matrix;
|
mod matrix;
|
||||||
mod op;
|
mod op;
|
||||||
mod root;
|
mod root;
|
||||||
|
mod stretch;
|
||||||
mod style;
|
mod style;
|
||||||
mod underover;
|
mod underover;
|
||||||
|
|
||||||
@ -21,6 +22,7 @@ pub use self::lr::*;
|
|||||||
pub use self::matrix::*;
|
pub use self::matrix::*;
|
||||||
pub use self::op::*;
|
pub use self::op::*;
|
||||||
pub use self::root::*;
|
pub use self::root::*;
|
||||||
|
pub use self::stretch::*;
|
||||||
pub use self::style::*;
|
pub use self::style::*;
|
||||||
pub use self::underover::*;
|
pub use self::underover::*;
|
||||||
|
|
||||||
|
125
crates/typst-library/src/math/stretch.rs
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
use comemo::Track;
|
||||||
|
|
||||||
|
use crate::diag::{At, SourceResult};
|
||||||
|
use crate::engine::Engine;
|
||||||
|
use crate::foundations::{
|
||||||
|
cast, elem, Content, Context, Func, NativeFunc, NativeFuncData, Resolve, StyleChain,
|
||||||
|
};
|
||||||
|
use crate::layout::{Abs, Rel};
|
||||||
|
use crate::math::{default_lr_size, Mathy};
|
||||||
|
|
||||||
|
/// Stretches a glyph.
|
||||||
|
///
|
||||||
|
/// This function can also be used to automatically stretch the base of an
|
||||||
|
/// attachment, so that it fits the top and bottom attachments.
|
||||||
|
///
|
||||||
|
/// Note that only some glyphs can be stretched, and which ones can depend on
|
||||||
|
/// the math font being used. However, most math fonts are the same in this
|
||||||
|
/// regard.
|
||||||
|
///
|
||||||
|
/// ```example
|
||||||
|
/// $ H stretch(=)^"define" U + p V $
|
||||||
|
/// $ f : X stretch(->>, size: #150%)_"surjective" Y $
|
||||||
|
/// $ x stretch(harpoons.ltrb, size: #3em) y
|
||||||
|
/// stretch(\[, size: #150%) z $
|
||||||
|
/// ```
|
||||||
|
#[elem(Mathy)]
|
||||||
|
pub struct StretchElem {
|
||||||
|
/// The glyph to stretch.
|
||||||
|
#[required]
|
||||||
|
pub body: Content,
|
||||||
|
|
||||||
|
/// The size to stretch to, relative to the maximum size of the glyph and
|
||||||
|
/// its attachments.
|
||||||
|
///
|
||||||
|
/// This value can be given as a [relative length]($relative), or a
|
||||||
|
/// [function]($function) that receives the size of the glyph as a
|
||||||
|
/// parameter (an absolute length) and should return a (relative) length.
|
||||||
|
/// For example, `{x => x * 80%}` would be equivalent to just specifying `{80%}`.
|
||||||
|
///
|
||||||
|
/// Note that the sizes of glyphs in math fonts come about in two ways:
|
||||||
|
///
|
||||||
|
/// - First, there are pre-made variants at specific sizes. This means you
|
||||||
|
/// will see discrete jumps in the stretched glyph's size as you increase
|
||||||
|
/// the size parameter. It is up to the font how many pre-made variants
|
||||||
|
/// there are and what their sizes are.
|
||||||
|
///
|
||||||
|
/// - Then, if the pre-made variants are all too small, a glyph of the
|
||||||
|
/// desired size is assembled from parts. The stretched glyph's size will
|
||||||
|
/// now be the exact size requested.
|
||||||
|
///
|
||||||
|
/// It could be the case that only one of the above exist for a glyph in
|
||||||
|
/// the font.
|
||||||
|
///
|
||||||
|
/// The value given here is really a minimum (but if there is no assembly
|
||||||
|
/// for the glyph, this minimum may not be reached), so the actual size of
|
||||||
|
/// the stretched glyph may not match what you specified.
|
||||||
|
///
|
||||||
|
/// ```example
|
||||||
|
/// #for i in range(0, 15) {
|
||||||
|
/// $stretch(\[, size: #(10pt + i * 2pt))$
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #set math.stretch(size: x => x + 0.5em)
|
||||||
|
/// $x stretch(=)^"def" y$
|
||||||
|
/// ```
|
||||||
|
#[default(Rel::one().into())]
|
||||||
|
pub size: StretchSize,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// How to size a stretched glyph.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Hash)]
|
||||||
|
pub enum StretchSize {
|
||||||
|
/// Sized by the specified length.
|
||||||
|
Rel(Rel),
|
||||||
|
/// Resolve the size for the given base size through the specified
|
||||||
|
/// function.
|
||||||
|
Func(Func),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StretchSize {
|
||||||
|
/// Resolve the stretch size given the base size.
|
||||||
|
pub fn resolve(
|
||||||
|
&self,
|
||||||
|
engine: &mut Engine,
|
||||||
|
styles: StyleChain,
|
||||||
|
base: Abs,
|
||||||
|
) -> SourceResult<Abs> {
|
||||||
|
Ok(match self {
|
||||||
|
Self::Rel(rel) => *rel,
|
||||||
|
Self::Func(func) => func
|
||||||
|
.call(engine, Context::new(None, Some(styles)).track(), [base])?
|
||||||
|
.cast()
|
||||||
|
.at(func.span())?,
|
||||||
|
}
|
||||||
|
.resolve(styles)
|
||||||
|
.relative_to(base))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether the size is the default used by `LrElem`.
|
||||||
|
pub fn is_lr_default(self) -> bool {
|
||||||
|
self == <default_lr_size>::data().into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Rel> for StretchSize {
|
||||||
|
fn from(rel: Rel) -> Self {
|
||||||
|
Self::Rel(rel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&'static NativeFuncData> for StretchSize {
|
||||||
|
fn from(data: &'static NativeFuncData) -> Self {
|
||||||
|
Self::Func(Func::from(data))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cast! {
|
||||||
|
StretchSize,
|
||||||
|
self => match self {
|
||||||
|
Self::Rel(v) => v.into_value(),
|
||||||
|
Self::Func(v) => v.into_value(),
|
||||||
|
},
|
||||||
|
v: Rel => Self::Rel(v),
|
||||||
|
v: Func => Self::Func(v),
|
||||||
|
}
|
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 1.0 KiB |
Before Width: | Height: | Size: 900 B After Width: | Height: | Size: 906 B |
Before Width: | Height: | Size: 2.2 KiB After Width: | Height: | Size: 2.2 KiB |
BIN
tests/ref/math-lr-size-function.png
Normal file
After Width: | Height: | Size: 603 B |
Before Width: | Height: | Size: 663 B After Width: | Height: | Size: 685 B |
BIN
tests/ref/math-stretch-function.png
Normal file
After Width: | Height: | Size: 291 B |
@ -34,6 +34,11 @@ $ lr(a/b\]) = a = lr(\{a/b) $
|
|||||||
$ lr(]sum_(x=1)^n x], size: #70%)
|
$ lr(]sum_(x=1)^n x], size: #70%)
|
||||||
< lr((1, 2), size: #200%) $
|
< lr((1, 2), size: #200%) $
|
||||||
|
|
||||||
|
--- math-lr-size-function ---
|
||||||
|
// Test using a function as an argument to size.
|
||||||
|
#set math.lr(size: x => if x > 10pt { 1em } else { 4 * x })
|
||||||
|
$ (a) (1/2) $
|
||||||
|
|
||||||
--- math-lr-shorthands ---
|
--- math-lr-shorthands ---
|
||||||
// Test predefined delimiter pairings.
|
// Test predefined delimiter pairings.
|
||||||
$floor(x/2), ceil(x/2), abs(x), norm(x)$
|
$floor(x/2), ceil(x/2), abs(x), norm(x)$
|
||||||
|
@ -91,3 +91,9 @@ $ body^"text" $
|
|||||||
}
|
}
|
||||||
$body^"long text"$
|
$body^"long text"$
|
||||||
}
|
}
|
||||||
|
|
||||||
|
--- math-stretch-function ---
|
||||||
|
// Test using a function as an argument to size.
|
||||||
|
$stretch(<-, size: #(x => x - 0.5em))_"function"$
|
||||||
|
#set math.stretch(size: x => x + 0.5em)
|
||||||
|
$stretch(|) |$
|
||||||
|