mirror of
https://github.com/typst/typst
synced 2025-05-17 02:25:27 +08:00
Improve alignment of text inside raw blocks (#1034)
This commit is contained in:
parent
d19a4124de
commit
b5ad2468ad
@ -102,6 +102,27 @@ pub struct RawElem {
|
|||||||
/// ```
|
/// ```
|
||||||
/// ````
|
/// ````
|
||||||
pub lang: Option<EcoString>,
|
pub lang: Option<EcoString>,
|
||||||
|
|
||||||
|
/// The horizontal alignment that each line in a raw block should have.
|
||||||
|
/// This option is ignored if this is not a raw block (if specified
|
||||||
|
/// `block: false` or single backticks were used in markup mode).
|
||||||
|
///
|
||||||
|
/// By default, this is set to `{start}`, meaning that raw text is
|
||||||
|
/// aligned towards the start of the text direction inside the block
|
||||||
|
/// by default, regardless of the current context's alignment (allowing
|
||||||
|
/// you to center the raw block itself without centering the text inside
|
||||||
|
/// it, for example).
|
||||||
|
///
|
||||||
|
/// ````example
|
||||||
|
/// #set raw(align: center)
|
||||||
|
///
|
||||||
|
/// ```typc
|
||||||
|
/// let f(x) = x
|
||||||
|
/// code = "centered"
|
||||||
|
/// ```
|
||||||
|
/// ````
|
||||||
|
#[default(HorizontalAlign(GenAlign::Start))]
|
||||||
|
pub align: HorizontalAlign,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RawElem {
|
impl RawElem {
|
||||||
@ -180,6 +201,8 @@ impl Show for RawElem {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if self.block(styles) {
|
if self.block(styles) {
|
||||||
|
// Align the text before inserting it into the block.
|
||||||
|
realized = realized.aligned(Axes::with_x(Some(self.align(styles).into())));
|
||||||
realized = BlockElem::new().with_body(Some(realized)).pack();
|
realized = BlockElem::new().with_body(Some(realized)).pack();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,6 +106,18 @@ impl From<Align> for GenAlign {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<HorizontalAlign> for GenAlign {
|
||||||
|
fn from(align: HorizontalAlign) -> Self {
|
||||||
|
align.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<VerticalAlign> for GenAlign {
|
||||||
|
fn from(align: VerticalAlign) -> Self {
|
||||||
|
align.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Debug for GenAlign {
|
impl Debug for GenAlign {
|
||||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
@ -184,7 +196,45 @@ impl Fold for GenAlign {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
/// Utility struct to restrict a passed alignment value to the horizontal axis
|
||||||
|
/// on cast.
|
||||||
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
||||||
|
pub struct HorizontalAlign(pub GenAlign);
|
||||||
|
|
||||||
|
cast_from_value! {
|
||||||
|
HorizontalAlign,
|
||||||
|
align: GenAlign => {
|
||||||
|
if align.axis() != Axis::X {
|
||||||
|
Err("alignment must be horizontal")?;
|
||||||
|
}
|
||||||
|
Self(align)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
cast_to_value! {
|
||||||
|
v: HorizontalAlign => v.0.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Utility struct to restrict a passed alignment value to the vertical axis on
|
||||||
|
/// cast.
|
||||||
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
||||||
|
pub struct VerticalAlign(pub GenAlign);
|
||||||
|
|
||||||
|
cast_from_value! {
|
||||||
|
VerticalAlign,
|
||||||
|
align: GenAlign => {
|
||||||
|
if align.axis() != Axis::Y {
|
||||||
|
Err("alignment must be vertical")?;
|
||||||
|
}
|
||||||
|
Self(align)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
cast_to_value! {
|
||||||
|
v: VerticalAlign => v.0.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
pub enum LeftRightAlternator {
|
pub enum LeftRightAlternator {
|
||||||
Left,
|
Left,
|
||||||
Right,
|
Right,
|
||||||
|
BIN
tests/ref/text/raw-align.png
Normal file
BIN
tests/ref/text/raw-align.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 32 KiB |
37
tests/typ/text/raw-align.typ
Normal file
37
tests/typ/text/raw-align.typ
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
// Test the alignment of text inside of raw blocks.
|
||||||
|
|
||||||
|
---
|
||||||
|
// Text inside raw block should be unaffected by outer alignment by default.
|
||||||
|
#set align(center)
|
||||||
|
#set page(width: 180pt)
|
||||||
|
#set text(6pt)
|
||||||
|
|
||||||
|
#lorem(20)
|
||||||
|
|
||||||
|
```py
|
||||||
|
def something(x):
|
||||||
|
return x
|
||||||
|
|
||||||
|
a = 342395823859823958329
|
||||||
|
b = 324923
|
||||||
|
```
|
||||||
|
|
||||||
|
#lorem(20)
|
||||||
|
|
||||||
|
---
|
||||||
|
// Text inside raw block should follow the specified alignment.
|
||||||
|
#set page(width: 180pt)
|
||||||
|
#set text(6pt)
|
||||||
|
|
||||||
|
#lorem(20)
|
||||||
|
#align(center, raw(
|
||||||
|
lang: "typ",
|
||||||
|
block: true,
|
||||||
|
align: right,
|
||||||
|
"#let f(x) = x\n#align(center, line(length: 1em))",
|
||||||
|
))
|
||||||
|
#lorem(20)
|
||||||
|
|
||||||
|
---
|
||||||
|
// Error: 17-20 alignment must be horizontal
|
||||||
|
#set raw(align: top)
|
Loading…
x
Reference in New Issue
Block a user