mirror of
https://github.com/typst/typst
synced 2025-07-27 14:27:56 +08:00
Merge 8c811110f3fa3815a3c722107e3383fdf8075ad2 into 7278d887cf05fadc9a96478830e5876739b78f53
This commit is contained in:
commit
3f2af060bd
@ -1,6 +1,6 @@
|
|||||||
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::{Em, Frame, Point, Size};
|
use typst_library::layout::{Frame, Point, Size};
|
||||||
use typst_library::math::AccentElem;
|
use typst_library::math::AccentElem;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
@ -8,9 +8,6 @@ use super::{
|
|||||||
style_flac,
|
style_flac,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// How much the accent can be shorter than the base.
|
|
||||||
const ACCENT_SHORT_FALL: Em = Em::new(0.5);
|
|
||||||
|
|
||||||
/// Lays out an [`AccentElem`].
|
/// Lays out an [`AccentElem`].
|
||||||
#[typst_macros::time(name = "math.accent", span = elem.span())]
|
#[typst_macros::time(name = "math.accent", span = elem.span())]
|
||||||
pub fn layout_accent(
|
pub fn layout_accent(
|
||||||
@ -45,11 +42,8 @@ pub fn layout_accent(
|
|||||||
let mut glyph =
|
let mut glyph =
|
||||||
GlyphFragment::new_char(ctx.font, accent_styles, accent.0, elem.span())?;
|
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
|
let width = elem.size.get_ref(styles).resolve(ctx.engine, styles, base.width())?;
|
||||||
// in many cases.
|
glyph.stretch_horizontal(ctx, width);
|
||||||
let width = elem.size.resolve(styles).relative_to(base.width());
|
|
||||||
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_attach = glyph.accent_attach.0;
|
||||||
let accent = glyph.into_frame();
|
let accent = glyph.into_frame();
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
|
||||||
@ -66,12 +67,12 @@ pub fn layout_attach(
|
|||||||
let relative_to_width = measure!(t, width).max(measure!(b, width));
|
let relative_to_width = measure!(t, width).max(measure!(b, width));
|
||||||
stretch_fragment(
|
stretch_fragment(
|
||||||
ctx,
|
ctx,
|
||||||
|
styles,
|
||||||
&mut base,
|
&mut base,
|
||||||
Some(Axis::X),
|
Some(Axis::X),
|
||||||
Some(relative_to_width),
|
Some(relative_to_width),
|
||||||
stretch,
|
&stretch,
|
||||||
Abs::zero(),
|
)?;
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let fragments = [
|
let fragments = [
|
||||||
@ -154,7 +155,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>() {
|
||||||
@ -162,7 +163,7 @@ fn stretch_size(styles: StyleChain, elem: &Packed<AttachElem>) -> Option<Rel<Abs
|
|||||||
}
|
}
|
||||||
|
|
||||||
base.to_packed::<StretchElem>()
|
base.to_packed::<StretchElem>()
|
||||||
.map(|stretch| stretch.size.resolve(styles))
|
.map(|stretch| stretch.size.get_cloned(styles))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Lay out the attachments.
|
/// Lay out the attachments.
|
||||||
|
@ -1,14 +1,13 @@
|
|||||||
use typst_library::diag::SourceResult;
|
use typst_library::diag::SourceResult;
|
||||||
use typst_library::foundations::{Content, Packed, Resolve, StyleChain, SymbolElem};
|
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, DELIM_SHORT_FALL, FracElem};
|
||||||
use typst_library::text::TextElem;
|
use typst_library::text::TextElem;
|
||||||
use typst_library::visualize::{FixedStroke, Geometry};
|
use typst_library::visualize::{FixedStroke, Geometry};
|
||||||
use typst_syntax::Span;
|
use typst_syntax::Span;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
DELIM_SHORT_FALL, FrameFragment, GlyphFragment, MathContext, style_for_denominator,
|
FrameFragment, GlyphFragment, MathContext, style_for_denominator, style_for_numerator,
|
||||||
style_for_numerator,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const FRAC_AROUND: Em = Em::new(0.1);
|
const FRAC_AROUND: Em = Em::new(0.1);
|
||||||
@ -49,7 +48,7 @@ fn layout_frac_like(
|
|||||||
binom: bool,
|
binom: bool,
|
||||||
span: Span,
|
span: Span,
|
||||||
) -> SourceResult<()> {
|
) -> SourceResult<()> {
|
||||||
let short_fall = DELIM_SHORT_FALL.resolve(styles);
|
let short_fall = DELIM_SHORT_FALL.abs().resolve(styles);
|
||||||
let axis = scaled!(ctx, styles, axis_height);
|
let axis = scaled!(ctx, styles, axis_height);
|
||||||
let thickness = scaled!(ctx, styles, fraction_rule_thickness);
|
let thickness = scaled!(ctx, styles, fraction_rule_thickness);
|
||||||
let shift_up = scaled!(
|
let shift_up = scaled!(
|
||||||
|
@ -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, StretchSize};
|
||||||
use typst_utils::SliceExt;
|
use typst_utils::SliceExt;
|
||||||
use unicode_math_class::MathClass;
|
use unicode_math_class::MathClass;
|
||||||
|
|
||||||
use super::{DELIM_SHORT_FALL, MathContext, MathFragment, stretch_fragment};
|
use super::{MathContext, MathFragment, stretch_fragment};
|
||||||
|
|
||||||
/// 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>()
|
||||||
&& lr.size.get(styles).is_one()
|
&& lr.size.get_ref(styles).is_lr_default()
|
||||||
{
|
{
|
||||||
body = &lr.body;
|
body = &lr.body;
|
||||||
}
|
}
|
||||||
@ -41,14 +41,28 @@ 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.resolve(styles);
|
let height = elem.size.get_ref(styles);
|
||||||
|
|
||||||
// Scale up fragments at both ends.
|
// Scale up fragments at both ends.
|
||||||
match inner_fragments {
|
match inner_fragments {
|
||||||
[one] => scale_if_delimiter(ctx, one, relative_to, height, None),
|
[one] => scale_if_delimiter(ctx, styles, one, relative_to, height, None)?,
|
||||||
[first, .., last] => {
|
[first, .., last] => {
|
||||||
scale_if_delimiter(ctx, first, relative_to, height, Some(MathClass::Opening));
|
scale_if_delimiter(
|
||||||
scale_if_delimiter(ctx, last, relative_to, height, Some(MathClass::Closing));
|
ctx,
|
||||||
|
styles,
|
||||||
|
first,
|
||||||
|
relative_to,
|
||||||
|
height,
|
||||||
|
Some(MathClass::Opening),
|
||||||
|
)?;
|
||||||
|
scale_if_delimiter(
|
||||||
|
ctx,
|
||||||
|
styles,
|
||||||
|
last,
|
||||||
|
relative_to,
|
||||||
|
height,
|
||||||
|
Some(MathClass::Closing),
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
[] => {}
|
[] => {}
|
||||||
}
|
}
|
||||||
@ -59,7 +73,7 @@ pub fn layout_lr(
|
|||||||
&& glyph.mid_stretched == Some(false)
|
&& glyph.mid_stretched == Some(false)
|
||||||
{
|
{
|
||||||
glyph.mid_stretched = Some(true);
|
glyph.mid_stretched = Some(true);
|
||||||
scale(ctx, fragment, relative_to, height);
|
scale(ctx, styles, fragment, relative_to, height)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,32 +126,32 @@ pub fn layout_mid(
|
|||||||
/// it is a delimiter, in a way that cannot be overridden by the user.
|
/// it is a delimiter, in a way that cannot be overridden by the user.
|
||||||
fn scale_if_delimiter(
|
fn scale_if_delimiter(
|
||||||
ctx: &mut MathContext,
|
ctx: &mut MathContext,
|
||||||
|
styles: StyleChain,
|
||||||
fragment: &mut MathFragment,
|
fragment: &mut MathFragment,
|
||||||
relative_to: Abs,
|
relative_to: Abs,
|
||||||
height: Rel<Abs>,
|
height: &StretchSize,
|
||||||
apply: Option<MathClass>,
|
apply: Option<MathClass>,
|
||||||
) {
|
) -> SourceResult<()> {
|
||||||
if matches!(
|
if matches!(
|
||||||
fragment.class(),
|
fragment.class(),
|
||||||
MathClass::Opening | MathClass::Closing | MathClass::Fence
|
MathClass::Opening | MathClass::Closing | MathClass::Fence
|
||||||
) {
|
) {
|
||||||
scale(ctx, fragment, relative_to, height);
|
scale(ctx, styles, fragment, relative_to, height)?;
|
||||||
|
|
||||||
if let Some(class) = apply {
|
if let Some(class) = apply {
|
||||||
fragment.set_class(class);
|
fragment.set_class(class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Scales a math fragment to a height.
|
/// Scales a math fragment to a height.
|
||||||
fn scale(
|
fn scale(
|
||||||
ctx: &mut MathContext,
|
ctx: &mut MathContext,
|
||||||
|
styles: StyleChain,
|
||||||
fragment: &mut MathFragment,
|
fragment: &mut MathFragment,
|
||||||
relative_to: Abs,
|
relative_to: Abs,
|
||||||
height: Rel<Abs>,
|
height: &StretchSize,
|
||||||
) {
|
) -> SourceResult<()> {
|
||||||
// This unwrap doesn't really matter. If it is None, then the fragment
|
stretch_fragment(ctx, styles, fragment, Some(Axis::Y), Some(relative_to), height)
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,20 @@
|
|||||||
use typst_library::diag::{SourceResult, bail, warning};
|
use typst_library::diag::{SourceResult, bail, warning};
|
||||||
use typst_library::foundations::{Content, Packed, Resolve, StyleChain};
|
use typst_library::foundations::{Content, Packed, Resolve, StyleChain};
|
||||||
use typst_library::layout::{
|
use typst_library::layout::{
|
||||||
Abs, Axes, Em, FixedAlignment, Frame, FrameItem, Point, Ratio, Rel, Size,
|
Abs, Axes, Em, FixedAlignment, Frame, FrameItem, Point, Rel, Size,
|
||||||
|
};
|
||||||
|
use typst_library::math::{
|
||||||
|
Augment, AugmentOffsets, CasesElem, MatElem, StretchSize, VecElem,
|
||||||
};
|
};
|
||||||
use typst_library::math::{Augment, AugmentOffsets, CasesElem, MatElem, VecElem};
|
|
||||||
use typst_library::text::TextElem;
|
use typst_library::text::TextElem;
|
||||||
use typst_library::visualize::{FillRule, FixedStroke, Geometry, LineCap, Shape};
|
use typst_library::visualize::{FillRule, FixedStroke, Geometry, LineCap, Shape};
|
||||||
use typst_syntax::Span;
|
use typst_syntax::Span;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
AlignmentResult, DELIM_SHORT_FALL, FrameFragment, GlyphFragment, LeftRightAlternator,
|
AlignmentResult, FrameFragment, GlyphFragment, LeftRightAlternator, MathContext,
|
||||||
MathContext, alignments, style_for_denominator,
|
alignments, style_for_denominator,
|
||||||
};
|
};
|
||||||
|
|
||||||
const VERTICAL_PADDING: Ratio = Ratio::new(0.1);
|
|
||||||
const DEFAULT_STROKE_THICKNESS: Em = Em::new(0.05);
|
const DEFAULT_STROKE_THICKNESS: Em = Em::new(0.05);
|
||||||
|
|
||||||
/// Lays out a [`VecElem`].
|
/// Lays out a [`VecElem`].
|
||||||
@ -39,7 +40,15 @@ pub fn layout_vec(
|
|||||||
)?;
|
)?;
|
||||||
|
|
||||||
let delim = elem.delim.get(styles);
|
let delim = elem.delim.get(styles);
|
||||||
layout_delimiters(ctx, styles, frame, delim.open(), delim.close(), span)
|
layout_delimiters(
|
||||||
|
ctx,
|
||||||
|
styles,
|
||||||
|
frame,
|
||||||
|
elem.delim_size.get_ref(styles),
|
||||||
|
delim.open(),
|
||||||
|
delim.close(),
|
||||||
|
span,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Lays out a [`CasesElem`].
|
/// Lays out a [`CasesElem`].
|
||||||
@ -70,7 +79,15 @@ pub fn layout_cases(
|
|||||||
} else {
|
} else {
|
||||||
(delim.open(), None)
|
(delim.open(), None)
|
||||||
};
|
};
|
||||||
layout_delimiters(ctx, styles, frame, open, close, span)
|
layout_delimiters(
|
||||||
|
ctx,
|
||||||
|
styles,
|
||||||
|
frame,
|
||||||
|
elem.delim_size.get_ref(styles),
|
||||||
|
open,
|
||||||
|
close,
|
||||||
|
span,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Lays out a [`MatElem`].
|
/// Lays out a [`MatElem`].
|
||||||
@ -128,7 +145,15 @@ pub fn layout_mat(
|
|||||||
)?;
|
)?;
|
||||||
|
|
||||||
let delim = elem.delim.get(styles);
|
let delim = elem.delim.get(styles);
|
||||||
layout_delimiters(ctx, styles, frame, delim.open(), delim.close(), span)
|
layout_delimiters(
|
||||||
|
ctx,
|
||||||
|
styles,
|
||||||
|
frame,
|
||||||
|
elem.delim_size.get_ref(styles),
|
||||||
|
delim.open(),
|
||||||
|
delim.close(),
|
||||||
|
span,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Layout the inner contents of a matrix, vector, or cases.
|
/// Layout the inner contents of a matrix, vector, or cases.
|
||||||
@ -309,19 +334,20 @@ fn layout_delimiters(
|
|||||||
ctx: &mut MathContext,
|
ctx: &mut MathContext,
|
||||||
styles: StyleChain,
|
styles: StyleChain,
|
||||||
mut frame: Frame,
|
mut frame: Frame,
|
||||||
|
size: &StretchSize,
|
||||||
left: Option<char>,
|
left: Option<char>,
|
||||||
right: Option<char>,
|
right: Option<char>,
|
||||||
span: Span,
|
span: Span,
|
||||||
) -> SourceResult<()> {
|
) -> SourceResult<()> {
|
||||||
let short_fall = DELIM_SHORT_FALL.resolve(styles);
|
|
||||||
let axis = scaled!(ctx, styles, axis_height);
|
let axis = scaled!(ctx, styles, axis_height);
|
||||||
let height = frame.height();
|
let height = frame.height();
|
||||||
let target = height + VERTICAL_PADDING.of(height);
|
|
||||||
frame.set_baseline(height / 2.0 + axis);
|
frame.set_baseline(height / 2.0 + axis);
|
||||||
|
|
||||||
|
let target = size.resolve(ctx.engine, styles, height)?;
|
||||||
|
|
||||||
if let Some(left_c) = left {
|
if let Some(left_c) = left {
|
||||||
let mut left = GlyphFragment::new_char(ctx.font, styles, left_c, span)?;
|
let mut left = GlyphFragment::new_char(ctx.font, styles, left_c, span)?;
|
||||||
left.stretch_vertical(ctx, target - short_fall);
|
left.stretch_vertical(ctx, target);
|
||||||
left.center_on_axis();
|
left.center_on_axis();
|
||||||
ctx.push(left);
|
ctx.push(left);
|
||||||
}
|
}
|
||||||
@ -330,7 +356,7 @@ fn layout_delimiters(
|
|||||||
|
|
||||||
if let Some(right_c) = right {
|
if let Some(right_c) = right {
|
||||||
let mut right = GlyphFragment::new_char(ctx.font, styles, right_c, span)?;
|
let mut right = GlyphFragment::new_char(ctx.font, styles, right_c, span)?;
|
||||||
right.stretch_vertical(ctx, target - short_fall);
|
right.stretch_vertical(ctx, target);
|
||||||
right.center_on_axis();
|
right.center_on_axis();
|
||||||
ctx.push(right);
|
ctx.push(right);
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use ttf_parser::Tag;
|
use ttf_parser::Tag;
|
||||||
use ttf_parser::math::MathValue;
|
use ttf_parser::math::MathValue;
|
||||||
use typst_library::foundations::{Style, StyleChain};
|
use typst_library::foundations::{Style, StyleChain};
|
||||||
use typst_library::layout::{Abs, Em, FixedAlignment, Frame, Point, Size};
|
use typst_library::layout::{Abs, FixedAlignment, Frame, Point, Size};
|
||||||
use typst_library::math::{EquationElem, MathSize};
|
use typst_library::math::{EquationElem, MathSize};
|
||||||
use typst_library::text::{FontFeatures, TextElem};
|
use typst_library::text::{FontFeatures, TextElem};
|
||||||
use typst_utils::LazyHash;
|
use typst_utils::LazyHash;
|
||||||
@ -30,9 +30,6 @@ macro_rules! percent {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// How much less high scaled delimiters can be than what they wrap.
|
|
||||||
pub const DELIM_SHORT_FALL: Em = Em::new(0.1);
|
|
||||||
|
|
||||||
/// Converts some unit to an absolute length with the current font & font size.
|
/// Converts some unit to an absolute length with the current font & font size.
|
||||||
pub trait Scaled {
|
pub trait Scaled {
|
||||||
fn scaled(self, ctx: &MathContext, font_size: Abs) -> Abs;
|
fn scaled(self, ctx: &MathContext, font_size: Abs) -> Abs;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use typst_library::diag::{SourceResult, warning};
|
use typst_library::diag::{SourceResult, warning};
|
||||||
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::StretchElem;
|
use typst_library::math::{StretchElem, StretchSize};
|
||||||
use typst_utils::Get;
|
use typst_utils::Get;
|
||||||
|
|
||||||
use super::{MathContext, MathFragment, stretch_axes};
|
use super::{MathContext, MathFragment, stretch_axes};
|
||||||
@ -14,14 +14,7 @@ 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(
|
stretch_fragment(ctx, styles, &mut fragment, None, None, elem.size.get_ref(styles))?;
|
||||||
ctx,
|
|
||||||
&mut fragment,
|
|
||||||
None,
|
|
||||||
None,
|
|
||||||
elem.size.resolve(styles),
|
|
||||||
Abs::zero(),
|
|
||||||
);
|
|
||||||
ctx.push(fragment);
|
ctx.push(fragment);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -29,29 +22,29 @@ pub fn layout_stretch(
|
|||||||
/// Attempts to stretch the given fragment by/to the amount given in stretch.
|
/// Attempts to stretch the given fragment by/to the amount given in stretch.
|
||||||
pub fn stretch_fragment(
|
pub fn stretch_fragment(
|
||||||
ctx: &mut MathContext,
|
ctx: &mut MathContext,
|
||||||
|
styles: StyleChain,
|
||||||
fragment: &mut MathFragment,
|
fragment: &mut MathFragment,
|
||||||
axis: Option<Axis>,
|
axis: Option<Axis>,
|
||||||
relative_to: Option<Abs>,
|
relative_to: Option<Abs>,
|
||||||
stretch: Rel<Abs>,
|
stretch: &StretchSize,
|
||||||
short_fall: Abs,
|
) -> SourceResult<()> {
|
||||||
) {
|
|
||||||
let size = fragment.size();
|
let size = fragment.size();
|
||||||
|
|
||||||
let MathFragment::Glyph(glyph) = fragment else { return };
|
let MathFragment::Glyph(glyph) = fragment else { return Ok(()) };
|
||||||
|
|
||||||
// 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 axes = stretch_axes(&glyph.item.font, glyph.base_glyph.id);
|
let axes = stretch_axes(&glyph.item.font, glyph.base_glyph.id);
|
||||||
let stretch_axis = if let Some(axis) = axis {
|
let stretch_axis = if let Some(axis) = axis {
|
||||||
if !axes.get(axis) {
|
if !axes.get(axis) {
|
||||||
return;
|
return Ok(());
|
||||||
}
|
}
|
||||||
axis
|
axis
|
||||||
} else {
|
} else {
|
||||||
match (axes.x, axes.y) {
|
match (axes.x, axes.y) {
|
||||||
(true, false) => Axis::X,
|
(true, false) => Axis::X,
|
||||||
(false, true) => Axis::Y,
|
(false, true) => Axis::Y,
|
||||||
(false, false) => return,
|
(false, false) => return Ok(()),
|
||||||
(true, true) => {
|
(true, true) => {
|
||||||
// As far as we know, there aren't any glyphs that have both
|
// As far as we know, there aren't any glyphs that have both
|
||||||
// vertical and horizontal constructions. So for the time being, we
|
// vertical and horizontal constructions. So for the time being, we
|
||||||
@ -62,16 +55,19 @@ pub fn stretch_fragment(
|
|||||||
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"
|
||||||
));
|
));
|
||||||
return;
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let relative_to_size = relative_to.unwrap_or_else(|| size.get(stretch_axis));
|
let relative_to_size = relative_to.unwrap_or_else(|| size.get(stretch_axis));
|
||||||
|
let target = stretch.resolve(ctx.engine, styles, relative_to_size)?;
|
||||||
|
|
||||||
glyph.stretch(ctx, stretch.relative_to(relative_to_size) - short_fall, stretch_axis);
|
glyph.stretch(ctx, target, stretch_axis);
|
||||||
|
|
||||||
if stretch_axis == Axis::Y {
|
if stretch_axis == Axis::Y {
|
||||||
glyph.center_on_axis();
|
glyph.center_on_axis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -6,9 +6,21 @@ use icu_provider::AsDeserializingBufferProvider;
|
|||||||
use icu_provider_blob::BlobDataProvider;
|
use icu_provider_blob::BlobDataProvider;
|
||||||
|
|
||||||
use crate::diag::bail;
|
use crate::diag::bail;
|
||||||
use crate::foundations::{Content, NativeElement, SymbolElem, cast, elem, func};
|
use crate::foundations::{
|
||||||
use crate::layout::{Length, Rel};
|
Content, NativeElement, NativeFunc, SymbolElem, cast, elem, func,
|
||||||
use crate::math::Mathy;
|
};
|
||||||
|
use crate::layout::{Em, Length, Ratio, Rel};
|
||||||
|
use crate::math::{Mathy, StretchSize};
|
||||||
|
|
||||||
|
const ACCENT_SHORT_FALL: Em = Em::new(-0.5);
|
||||||
|
|
||||||
|
#[func(name = "x => x - 0.5em")]
|
||||||
|
const fn default_accent_size(base: Length) -> Rel {
|
||||||
|
Rel {
|
||||||
|
rel: Ratio::zero(),
|
||||||
|
abs: Length { abs: base.abs, em: ACCENT_SHORT_FALL },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Attaches an accent to a base.
|
/// Attaches an accent to a base.
|
||||||
///
|
///
|
||||||
@ -59,11 +71,14 @@ pub struct AccentElem {
|
|||||||
|
|
||||||
/// The size of the accent, relative to the width of the base.
|
/// The size of the accent, relative to the width of the base.
|
||||||
///
|
///
|
||||||
|
/// See the [stretch documentation]($math.stretch.size) for more
|
||||||
|
/// information on sizes.
|
||||||
|
///
|
||||||
/// ```example
|
/// ```example
|
||||||
/// $dash(A, size: #150%)$
|
/// $dash(A, size: #150%)$
|
||||||
/// ```
|
/// ```
|
||||||
#[default(Rel::one())]
|
#[default(<default_accent_size>::data().into())]
|
||||||
pub size: Rel<Length>,
|
pub size: StretchSize,
|
||||||
|
|
||||||
/// Whether to remove the dot on top of lowercase i and j when adding a top
|
/// Whether to remove the dot on top of lowercase i and j when adding a top
|
||||||
/// accent.
|
/// accent.
|
||||||
@ -140,8 +155,11 @@ macro_rules! accents {
|
|||||||
/// The base to which the accent is applied.
|
/// The base to which the accent is applied.
|
||||||
base: Content,
|
base: Content,
|
||||||
/// The size of the accent, relative to the width of the base.
|
/// The size of the accent, relative to the width of the base.
|
||||||
|
///
|
||||||
|
/// See the [stretch documentation]($math.stretch.size) for
|
||||||
|
/// more information on sizes.
|
||||||
#[named]
|
#[named]
|
||||||
size: Option<Rel<Length>>,
|
size: Option<StretchSize>,
|
||||||
/// Whether to remove the dot on top of lowercase i and j when
|
/// Whether to remove the dot on top of lowercase i and j when
|
||||||
/// adding a top accent.
|
/// adding a top accent.
|
||||||
#[named]
|
#[named]
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
use crate::foundations::{Content, Packed, elem};
|
use crate::foundations::{Content, Packed, elem};
|
||||||
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,30 +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.
|
|
||||||
#[default(Rel::one())]
|
|
||||||
pub size: Rel<Length>,
|
|
||||||
}
|
|
||||||
|
@ -1,6 +1,16 @@
|
|||||||
use crate::foundations::{Content, NativeElement, SymbolElem, elem, func};
|
use crate::foundations::{Content, NativeElement, NativeFunc, SymbolElem, elem, func};
|
||||||
use crate::layout::{Length, Rel};
|
use crate::layout::{Em, Length, Ratio, Rel};
|
||||||
use crate::math::Mathy;
|
use crate::math::{Mathy, StretchSize};
|
||||||
|
|
||||||
|
pub 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,9 +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
|
||||||
#[default(Rel::one())]
|
/// content.
|
||||||
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]
|
||||||
@ -42,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 {
|
||||||
@ -58,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 {
|
||||||
@ -74,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 {
|
||||||
@ -90,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 {
|
||||||
@ -106,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 {
|
||||||
@ -119,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([
|
||||||
|
@ -5,15 +5,27 @@ use unicode_math_class::MathClass;
|
|||||||
|
|
||||||
use crate::diag::{At, HintedStrResult, StrResult, bail};
|
use crate::diag::{At, HintedStrResult, StrResult, bail};
|
||||||
use crate::foundations::{
|
use crate::foundations::{
|
||||||
Array, Content, Dict, Fold, NoneValue, Resolve, Smart, StyleChain, Symbol, Value,
|
Array, Content, Dict, Fold, NativeFunc, NoneValue, Resolve, Smart, StyleChain,
|
||||||
array, cast, dict, elem,
|
Symbol, Value, array, cast, dict, elem, func,
|
||||||
};
|
};
|
||||||
use crate::layout::{Abs, Em, HAlignment, Length, Rel};
|
use crate::layout::{Abs, Em, HAlignment, Length, Ratio, Rel};
|
||||||
use crate::math::Mathy;
|
use crate::math::{DELIM_SHORT_FALL, Mathy, StretchSize};
|
||||||
use crate::visualize::Stroke;
|
use crate::visualize::Stroke;
|
||||||
|
|
||||||
const DEFAULT_ROW_GAP: Em = Em::new(0.2);
|
const DEFAULT_ROW_GAP: Em = Em::new(0.2);
|
||||||
const DEFAULT_COL_GAP: Em = Em::new(0.5);
|
const DEFAULT_COL_GAP: Em = Em::new(0.5);
|
||||||
|
const VERTICAL_PADDING: Ratio = Ratio::new(1.1);
|
||||||
|
|
||||||
|
#[func(name = "x => x * 1.1 - 0.1em")]
|
||||||
|
const fn default_mat_size(base: Length) -> Rel {
|
||||||
|
Rel {
|
||||||
|
rel: Ratio::zero(),
|
||||||
|
abs: Length {
|
||||||
|
abs: Abs::raw(base.abs.to_raw() * VERTICAL_PADDING.get()),
|
||||||
|
em: DELIM_SHORT_FALL,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A column vector.
|
/// A column vector.
|
||||||
///
|
///
|
||||||
@ -40,6 +52,13 @@ pub struct VecElem {
|
|||||||
#[default(DelimiterPair::PAREN)]
|
#[default(DelimiterPair::PAREN)]
|
||||||
pub delim: DelimiterPair,
|
pub delim: DelimiterPair,
|
||||||
|
|
||||||
|
/// The size of the delimiters, relative to the elements' total height.
|
||||||
|
///
|
||||||
|
/// See the [stretch documentation]($math.stretch.size) for more
|
||||||
|
/// information on sizes.
|
||||||
|
#[default(<default_mat_size>::data().into())]
|
||||||
|
pub delim_size: StretchSize,
|
||||||
|
|
||||||
/// The horizontal alignment that each element should have.
|
/// The horizontal alignment that each element should have.
|
||||||
///
|
///
|
||||||
/// ```example
|
/// ```example
|
||||||
@ -99,6 +118,13 @@ pub struct MatElem {
|
|||||||
#[default(DelimiterPair::PAREN)]
|
#[default(DelimiterPair::PAREN)]
|
||||||
pub delim: DelimiterPair,
|
pub delim: DelimiterPair,
|
||||||
|
|
||||||
|
/// The size of the delimiters, relative to the cells' total height.
|
||||||
|
///
|
||||||
|
/// See the [stretch documentation]($math.stretch.size) for more
|
||||||
|
/// information on sizes.
|
||||||
|
#[default(<default_mat_size>::data().into())]
|
||||||
|
pub delim_size: StretchSize,
|
||||||
|
|
||||||
/// The horizontal alignment that each cell should have.
|
/// The horizontal alignment that each cell should have.
|
||||||
///
|
///
|
||||||
/// ```example
|
/// ```example
|
||||||
@ -238,6 +264,13 @@ pub struct CasesElem {
|
|||||||
#[default(DelimiterPair::BRACE)]
|
#[default(DelimiterPair::BRACE)]
|
||||||
pub delim: DelimiterPair,
|
pub delim: DelimiterPair,
|
||||||
|
|
||||||
|
/// The size of the delimiters, relative to the branches' total height.
|
||||||
|
///
|
||||||
|
/// See the [stretch documentation]($math.stretch.size) for more
|
||||||
|
/// information on sizes.
|
||||||
|
#[default(<default_mat_size>::data().into())]
|
||||||
|
pub delim_size: StretchSize,
|
||||||
|
|
||||||
/// Whether the direction of cases should be reversed.
|
/// Whether the direction of cases should be reversed.
|
||||||
///
|
///
|
||||||
/// ```example
|
/// ```example
|
||||||
|
@ -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
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::{
|
||||||
|
Content, Context, Func, NativeFunc, NativeFuncData, Resolve, StyleChain, cast, elem,
|
||||||
|
};
|
||||||
|
use crate::layout::{Abs, Rel};
|
||||||
|
use crate::math::{Mathy, default_lr_size};
|
||||||
|
|
||||||
|
/// 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),
|
||||||
|
}
|
BIN
tests/ref/math-accent-sized-function.png
Normal file
BIN
tests/ref/math-accent-sized-function.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 417 B |
BIN
tests/ref/math-cases-delim-size.png
Normal file
BIN
tests/ref/math-cases-delim-size.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1005 B |
BIN
tests/ref/math-lr-size-function.png
Normal file
BIN
tests/ref/math-lr-size-function.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 603 B |
BIN
tests/ref/math-mat-delim-size.png
Normal file
BIN
tests/ref/math-mat-delim-size.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.3 KiB |
BIN
tests/ref/math-stretch-function.png
Normal file
BIN
tests/ref/math-stretch-function.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 291 B |
BIN
tests/ref/math-vec-delim-size.png
Normal file
BIN
tests/ref/math-vec-delim-size.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.3 KiB |
@ -30,11 +30,17 @@ $ tilde(integral), tilde(integral)_a^b, tilde(integral_a^b) $
|
|||||||
|
|
||||||
--- math-accent-sized ---
|
--- math-accent-sized ---
|
||||||
// Test accent size.
|
// Test accent size.
|
||||||
$tilde(sum), tilde(sum, size: #50%), accent(H, hat, size: #200%)$
|
$tilde(sum), tilde(sum, size: #25%), accent(H, hat, size: #125%)$
|
||||||
|
|
||||||
--- math-accent-sized-script ---
|
--- math-accent-sized-script ---
|
||||||
// Test accent size in script size.
|
// Test accent size in script size.
|
||||||
$tilde(U, size: #1.1em), x^tilde(U, size: #1.1em), sscript(tilde(U, size: #1.1em))$
|
$tilde(U, size: #0.6em), x^tilde(U, size: #0.6em), sscript(tilde(U, size: #0.6em))$
|
||||||
|
|
||||||
|
--- math-accent-sized-function ---
|
||||||
|
// Test accent size with a function.
|
||||||
|
$dash(A) arrow(I) hat(L)$ \
|
||||||
|
#set math.accent(size: x => x - 0.1em)
|
||||||
|
$dash(A) arrow(I) hat(L)$
|
||||||
|
|
||||||
--- math-accent-dotless ---
|
--- math-accent-dotless ---
|
||||||
// Test dotless glyph variants.
|
// Test dotless glyph variants.
|
||||||
@ -85,7 +91,7 @@ $ accent(integral, \u{20EC}), accent(integral, \u{20EC})_a^b, accent(integral_a^
|
|||||||
|
|
||||||
--- math-accent-bottom-sized ---
|
--- math-accent-bottom-sized ---
|
||||||
// Test bottom accent size.
|
// Test bottom accent size.
|
||||||
$accent(sum, \u{0330}), accent(sum, \u{0330}, size: #50%), accent(H, \u{032D}, size: #200%)$
|
$accent(sum, \u{0330}), accent(sum, \u{0330}, size: #25%), accent(H, \u{032D}, size: #125%)$
|
||||||
|
|
||||||
--- math-accent-nested ---
|
--- math-accent-nested ---
|
||||||
// Test nested top and bottom accents.
|
// Test nested top and bottom accents.
|
||||||
|
@ -187,5 +187,5 @@ $ a0 + a1 + a0_2 \
|
|||||||
--- math-attach-scripts-extended-shapes ---
|
--- math-attach-scripts-extended-shapes ---
|
||||||
// Test script attachments positioning if the base is an extended shape (or a
|
// Test script attachments positioning if the base is an extended shape (or a
|
||||||
// sequence of extended shapes).
|
// sequence of extended shapes).
|
||||||
$lr(size: #130%, [x])_0^1, [x]_0^1, \]_0^1, x_0^1, A_0^1$ \
|
$lr(size: #115%, [x])_0^1, [x]_0^1, \]_0^1, x_0^1, A_0^1$ \
|
||||||
$n^2, (n + 1)^2, sum_0^1, integral_0^1$
|
$n^2, (n + 1)^2, sum_0^1, integral_0^1$
|
||||||
|
@ -16,6 +16,12 @@ $ x = cases(1, 2) $
|
|||||||
#set math.cases(delim: sym.angle.l)
|
#set math.cases(delim: sym.angle.l)
|
||||||
$ cases(a, b, c) $
|
$ cases(a, b, c) $
|
||||||
|
|
||||||
|
--- math-cases-delim-size ---
|
||||||
|
// Test setting delimiter size.
|
||||||
|
$ cases(reverse: #true, 1, 2, 3) cases(delim-size: #100%, 1, 2, 3) $
|
||||||
|
#set math.cases(delim-size: x => calc.max(x - 5pt, x * 0.901))
|
||||||
|
$ cases(1, 2) cases(1, 2, 3, 4) $
|
||||||
|
|
||||||
--- math-cases-linebreaks ---
|
--- math-cases-linebreaks ---
|
||||||
// Warning: 40-49 linebreaks are ignored in branches
|
// Warning: 40-49 linebreaks are ignored in branches
|
||||||
// Hint: 40-49 use commas instead to separate each line
|
// Hint: 40-49 use commas instead to separate each line
|
||||||
|
@ -31,9 +31,14 @@ $ lr(a/b\]) = a = lr(\{a/b) $
|
|||||||
|
|
||||||
--- math-lr-size ---
|
--- math-lr-size ---
|
||||||
// Test manual scaling.
|
// Test manual scaling.
|
||||||
$ lr(]sum_(x=1)^n x], size: #70%)
|
$ lr(]sum_(x=1)^n x], size: #60%)
|
||||||
< 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)$
|
||||||
@ -57,16 +62,16 @@ $ { x mid(|) sum_(i=1)^oo phi_i (x) < 1 } \
|
|||||||
#set page(width: auto)
|
#set page(width: auto)
|
||||||
|
|
||||||
$ lr({ A mid(|) integral }) quad
|
$ lr({ A mid(|) integral }) quad
|
||||||
lr(size: #1em, { A mid(|) integral }) quad
|
lr(size: #0%, { A mid(|) integral }) quad
|
||||||
lr(size: #(1em+20%), { A mid(|) integral }) \
|
lr(size: #(1em+10%), { A mid(|) integral }) \
|
||||||
|
|
||||||
lr(] A mid(|) integral ]) quad
|
lr(] A mid(|) integral ]) quad
|
||||||
lr(size: #1em, ] A mid(|) integral ]) quad
|
lr(size: #0%, ] A mid(|) integral ]) quad
|
||||||
lr(size: #(1em+20%), ] A mid(|) integral ]) \
|
lr(size: #(1em+10%), ] A mid(|) integral ]) \
|
||||||
|
|
||||||
lr(( A mid(|) integral ]) quad
|
lr(( A mid(|) integral ]) quad
|
||||||
lr(size: #1em, ( A mid(|) integral ]) quad
|
lr(size: #0%, ( A mid(|) integral ]) quad
|
||||||
lr(size: #(1em+20%), ( A mid(|) integral ]) $
|
lr(size: #(1em+10%), ( A mid(|) integral ]) $
|
||||||
|
|
||||||
--- math-lr-mid-size-nested-equation ---
|
--- math-lr-mid-size-nested-equation ---
|
||||||
// Test mid size when lr size is set, when nested in an equation.
|
// Test mid size when lr size is set, when nested in an equation.
|
||||||
@ -74,8 +79,8 @@ $ lr({ A mid(|) integral }) quad
|
|||||||
|
|
||||||
#let body = ${ A mid(|) integral }$
|
#let body = ${ A mid(|) integral }$
|
||||||
$ lr(body) quad
|
$ lr(body) quad
|
||||||
lr(size: #1em, body) quad
|
lr(size: #0%, body) quad
|
||||||
lr(size: #(1em+20%), body) $
|
lr(size: #(1em+10%), body) $
|
||||||
|
|
||||||
--- math-lr-mid-class ---
|
--- math-lr-mid-class ---
|
||||||
// Test that `mid` creates a Relation, but that can be overridden.
|
// Test that `mid` creates a Relation, but that can be overridden.
|
||||||
|
@ -54,6 +54,12 @@ $ a + mat(delim: #none, 1, 2; 3, 4) + b $
|
|||||||
$ mat(1, 2; 3, 4; delim: "[") $,
|
$ mat(1, 2; 3, 4; delim: "[") $,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
--- math-mat-delim-size ---
|
||||||
|
// Test setting delimiter size.
|
||||||
|
$ mat(c; c; c) mat(delim-size: #100%, c; c; c) $
|
||||||
|
#set math.mat(delim-size: x => calc.max(x - 5pt, x * 0.901))
|
||||||
|
$ mat(delim: "[", f; f; f; f) mat(delim: ||, x; x; x; x) $
|
||||||
|
|
||||||
--- math-mat-spread ---
|
--- math-mat-spread ---
|
||||||
// Test argument spreading in matrix.
|
// Test argument spreading in matrix.
|
||||||
$ mat(..#range(1, 5).chunks(2))
|
$ mat(..#range(1, 5).chunks(2))
|
||||||
@ -263,7 +269,7 @@ $ mat(a; b; c) mat(a \ b \ c) $
|
|||||||
--- math-mat-vec-cases-unity ---
|
--- math-mat-vec-cases-unity ---
|
||||||
// Test that matrices, vectors, and cases are all laid out the same.
|
// Test that matrices, vectors, and cases are all laid out the same.
|
||||||
$ mat(z_(n_p); a^2)
|
$ mat(z_(n_p); a^2)
|
||||||
vec(z_(n_p), a^2)
|
vec(z_(n_p), a^2)
|
||||||
cases(reverse: #true, delim: \(, z_(n_p), a^2)
|
cases(reverse: #true, delim: \(, z_(n_p), a^2)
|
||||||
cases(delim: \(, z_(n_p), a^2) $
|
cases(delim: \(, z_(n_p), a^2) $
|
||||||
|
|
||||||
|
@ -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(|) |$
|
||||||
|
@ -50,6 +50,12 @@ $ vec(1, 2) $
|
|||||||
// Error: 22-33 invalid delimiter: "%"
|
// Error: 22-33 invalid delimiter: "%"
|
||||||
#set math.vec(delim: (none, "%"))
|
#set math.vec(delim: (none, "%"))
|
||||||
|
|
||||||
|
--- math-vec-delim-size ---
|
||||||
|
// Test setting delimiter size.
|
||||||
|
$ vec(1, 2, 3) vec(delim-size: #100%, 1, 2, 3) $
|
||||||
|
#set math.vec(delim-size: x => calc.max(x - 5pt, x * 0.901))
|
||||||
|
$ vec(delim: "{", 1, 2, 3) vec(delim: "[", 1, 2, 3) $
|
||||||
|
|
||||||
--- math-vec-linebreaks ---
|
--- math-vec-linebreaks ---
|
||||||
// Warning: 20-29 linebreaks are ignored in elements
|
// Warning: 20-29 linebreaks are ignored in elements
|
||||||
// Hint: 20-29 use commas instead to separate each line
|
// Hint: 20-29 use commas instead to separate each line
|
||||||
|
Loading…
x
Reference in New Issue
Block a user