mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Add alignment parameter to matrices and vectors (#4998)
This commit is contained in:
parent
cf35c2f44c
commit
a40e068590
@ -7,7 +7,8 @@ use crate::foundations::{
|
|||||||
Smart, StyleChain, Value,
|
Smart, StyleChain, Value,
|
||||||
};
|
};
|
||||||
use crate::layout::{
|
use crate::layout::{
|
||||||
Abs, Axes, Em, FixedAlignment, Frame, FrameItem, Length, Point, Ratio, Rel, Size,
|
Abs, Axes, Em, FixedAlignment, Frame, FrameItem, HAlignment, Length, Point, Ratio,
|
||||||
|
Rel, Size,
|
||||||
};
|
};
|
||||||
use crate::math::{
|
use crate::math::{
|
||||||
alignments, scaled_font_size, stack, style_for_denominator, AlignmentResult,
|
alignments, scaled_font_size, stack, style_for_denominator, AlignmentResult,
|
||||||
@ -29,7 +30,8 @@ const DEFAULT_STROKE_THICKNESS: Em = Em::new(0.05);
|
|||||||
|
|
||||||
/// A column vector.
|
/// A column vector.
|
||||||
///
|
///
|
||||||
/// Content in the vector's elements can be aligned with the `&` symbol.
|
/// Content in the vector's elements can be aligned with the
|
||||||
|
/// [`align`]($math.vec.align) parameter, or the `&` symbol.
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
/// ```example
|
/// ```example
|
||||||
@ -47,6 +49,16 @@ pub struct VecElem {
|
|||||||
#[default(DelimiterPair::PAREN)]
|
#[default(DelimiterPair::PAREN)]
|
||||||
pub delim: DelimiterPair,
|
pub delim: DelimiterPair,
|
||||||
|
|
||||||
|
/// The horizontal alignment that each element should have.
|
||||||
|
///
|
||||||
|
/// ```example
|
||||||
|
/// #set math.vec(align: right)
|
||||||
|
/// $ vec(-1, 1, -1) $
|
||||||
|
/// ```
|
||||||
|
#[resolve]
|
||||||
|
#[default(HAlignment::Center)]
|
||||||
|
pub align: HAlignment,
|
||||||
|
|
||||||
/// The gap between elements.
|
/// The gap between elements.
|
||||||
///
|
///
|
||||||
/// ```example
|
/// ```example
|
||||||
@ -70,7 +82,7 @@ impl LayoutMath for Packed<VecElem> {
|
|||||||
ctx,
|
ctx,
|
||||||
styles,
|
styles,
|
||||||
self.children(),
|
self.children(),
|
||||||
FixedAlignment::Center,
|
self.align(styles),
|
||||||
self.gap(styles),
|
self.gap(styles),
|
||||||
LeftRightAlternator::Right,
|
LeftRightAlternator::Right,
|
||||||
)?;
|
)?;
|
||||||
@ -87,7 +99,9 @@ impl LayoutMath for Packed<VecElem> {
|
|||||||
/// special syntax of math function calls to define custom functions that take
|
/// special syntax of math function calls to define custom functions that take
|
||||||
/// 2D data.
|
/// 2D data.
|
||||||
///
|
///
|
||||||
/// Content in cells that are in the same row can be aligned with the `&` symbol.
|
/// Content in cells can be aligned with the [`align`]($math.mat.align)
|
||||||
|
/// parameter, or content in cells that are in the same row can be aligned with
|
||||||
|
/// the `&` symbol.
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
/// ```example
|
/// ```example
|
||||||
@ -109,6 +123,16 @@ pub struct MatElem {
|
|||||||
#[default(DelimiterPair::PAREN)]
|
#[default(DelimiterPair::PAREN)]
|
||||||
pub delim: DelimiterPair,
|
pub delim: DelimiterPair,
|
||||||
|
|
||||||
|
/// The horizontal alignment that each cell should have.
|
||||||
|
///
|
||||||
|
/// ```example
|
||||||
|
/// #set math.mat(align: right)
|
||||||
|
/// $ mat(-1, 1, 1; 1, -1, 1; 1, 1, -1) $
|
||||||
|
/// ```
|
||||||
|
#[resolve]
|
||||||
|
#[default(HAlignment::Center)]
|
||||||
|
pub align: HAlignment,
|
||||||
|
|
||||||
/// Draws augmentation lines in a matrix.
|
/// Draws augmentation lines in a matrix.
|
||||||
///
|
///
|
||||||
/// - `{none}`: No lines are drawn.
|
/// - `{none}`: No lines are drawn.
|
||||||
@ -249,6 +273,7 @@ impl LayoutMath for Packed<MatElem> {
|
|||||||
ctx,
|
ctx,
|
||||||
styles,
|
styles,
|
||||||
rows,
|
rows,
|
||||||
|
self.align(styles),
|
||||||
augment,
|
augment,
|
||||||
Axes::new(self.column_gap(styles), self.row_gap(styles)),
|
Axes::new(self.column_gap(styles), self.row_gap(styles)),
|
||||||
self.span(),
|
self.span(),
|
||||||
@ -454,6 +479,7 @@ fn layout_mat_body(
|
|||||||
ctx: &mut MathContext,
|
ctx: &mut MathContext,
|
||||||
styles: StyleChain,
|
styles: StyleChain,
|
||||||
rows: &[Vec<Content>],
|
rows: &[Vec<Content>],
|
||||||
|
align: FixedAlignment,
|
||||||
augment: Option<Augment<Abs>>,
|
augment: Option<Augment<Abs>>,
|
||||||
gap: Axes<Rel<Abs>>,
|
gap: Axes<Rel<Abs>>,
|
||||||
span: Span,
|
span: Span,
|
||||||
@ -538,7 +564,11 @@ fn layout_mat_body(
|
|||||||
for (cell, &(ascent, descent)) in col.into_iter().zip(&heights) {
|
for (cell, &(ascent, descent)) in col.into_iter().zip(&heights) {
|
||||||
let cell = cell.into_line_frame(&points, LeftRightAlternator::Right);
|
let cell = cell.into_line_frame(&points, LeftRightAlternator::Right);
|
||||||
let pos = Point::new(
|
let pos = Point::new(
|
||||||
if points.is_empty() { x + (rcol - cell.width()) / 2.0 } else { x },
|
if points.is_empty() {
|
||||||
|
x + align.position(rcol - cell.width())
|
||||||
|
} else {
|
||||||
|
x
|
||||||
|
},
|
||||||
y + ascent - cell.ascent(),
|
y + ascent - cell.ascent(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -476,7 +476,11 @@ pub(super) fn stack(
|
|||||||
|
|
||||||
let mut y = Abs::zero();
|
let mut y = Abs::zero();
|
||||||
for (i, row) in rows.into_iter().enumerate() {
|
for (i, row) in rows.into_iter().enumerate() {
|
||||||
let x = align.position(width - row.width());
|
let x = if points.is_empty() {
|
||||||
|
align.position(width - row.width())
|
||||||
|
} else {
|
||||||
|
Abs::zero()
|
||||||
|
};
|
||||||
let ascent_padded_part = minimum_ascent_descent
|
let ascent_padded_part = minimum_ascent_descent
|
||||||
.map_or(Abs::zero(), |(a, _)| (a - row.ascent()))
|
.map_or(Abs::zero(), |(a, _)| (a - row.ascent()))
|
||||||
.max(Abs::zero());
|
.max(Abs::zero());
|
||||||
|
Before Width: | Height: | Size: 1009 B After Width: | Height: | Size: 1009 B |
BIN
tests/ref/math-mat-align-explicit-mixed.png
Normal file
BIN
tests/ref/math-mat-align-explicit-mixed.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.5 KiB |
BIN
tests/ref/math-mat-align.png
Normal file
BIN
tests/ref/math-mat-align.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.5 KiB |
BIN
tests/ref/math-vec-align.png
Normal file
BIN
tests/ref/math-vec-align.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
@ -92,7 +92,12 @@ $ mat(1, 0, 0, 0; 0, 1, 0, 0; 0, 0, 1, 1) $
|
|||||||
// Error: 3-37 cannot draw a vertical line after column 3 of a matrix with 3 columns
|
// Error: 3-37 cannot draw a vertical line after column 3 of a matrix with 3 columns
|
||||||
$ mat(1, 0, 0; 0, 1, 1; augment: #3) $,
|
$ mat(1, 0, 0; 0, 1, 1; augment: #3) $,
|
||||||
|
|
||||||
--- math-mat-align-explicit--alternating ---
|
--- math-mat-align ---
|
||||||
|
$ mat(-1, 1, 1; 1, -1, 1; 1, 1, -1; align: #left) $
|
||||||
|
$ mat(-1, 1, 1; 1, -1, 1; 1, 1, -1; align: #center) $
|
||||||
|
$ mat(-1, 1, 1; 1, -1, 1; 1, 1, -1; align: #right) $
|
||||||
|
|
||||||
|
--- math-mat-align-explicit-alternating ---
|
||||||
// Test alternating explicit alignment in a matrix.
|
// Test alternating explicit alignment in a matrix.
|
||||||
$ mat(
|
$ mat(
|
||||||
"a" & "a a a" & "a a";
|
"a" & "a a a" & "a a";
|
||||||
@ -124,6 +129,17 @@ $ mat(
|
|||||||
"a a a"&, "a"&, "a a a"&;
|
"a a a"&, "a"&, "a a a"&;
|
||||||
) $
|
) $
|
||||||
|
|
||||||
|
--- math-mat-align-explicit-mixed ---
|
||||||
|
// Test explicit alignment in some columns with align parameter in a matrix.
|
||||||
|
#let data = (
|
||||||
|
($&18&&.02$, $1$, $+1$),
|
||||||
|
($-&9&&.3$, $-1$, $-&21$),
|
||||||
|
($&&&.011$, $1$, $&0$)
|
||||||
|
)
|
||||||
|
$ #math.mat(align: left, ..data) $
|
||||||
|
$ #math.mat(align: center, ..data) $
|
||||||
|
$ #math.mat(align: right, ..data) $
|
||||||
|
|
||||||
--- math-mat-align-complex ---
|
--- math-mat-align-complex ---
|
||||||
// Test #460 equations.
|
// Test #460 equations.
|
||||||
#let stop = {
|
#let stop = {
|
||||||
|
@ -4,6 +4,10 @@
|
|||||||
#set math.vec(gap: 1em)
|
#set math.vec(gap: 1em)
|
||||||
$ vec(1, 2) $
|
$ vec(1, 2) $
|
||||||
|
|
||||||
|
--- math-vec-align ---
|
||||||
|
$ vec(-1, 1, -1, align: #left)
|
||||||
|
vec(-1, 1, -1, align: #center)
|
||||||
|
vec(-1, 1, -1, align: #right) $
|
||||||
|
|
||||||
--- math-vec-align-explicit-alternating ---
|
--- math-vec-align-explicit-alternating ---
|
||||||
// Test alternating alignment in a vector.
|
// Test alternating alignment in a vector.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user