mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Add spacing functions 🔛
This commit is contained in:
parent
991e879e1d
commit
cff325b520
@ -116,6 +116,7 @@ pub enum Command<'a> {
|
|||||||
Layout(&'a SyntaxTree),
|
Layout(&'a SyntaxTree),
|
||||||
Add(Layout),
|
Add(Layout),
|
||||||
AddMany(MultiLayout),
|
AddMany(MultiLayout),
|
||||||
|
AddFlex(Layout),
|
||||||
SetAlignment(Alignment),
|
SetAlignment(Alignment),
|
||||||
SetStyle(TextStyle),
|
SetStyle(TextStyle),
|
||||||
FinishLayout,
|
FinishLayout,
|
||||||
|
@ -40,6 +40,15 @@ pub struct Layout {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Layout {
|
impl Layout {
|
||||||
|
/// Create an empty layout with the specified dimensions.
|
||||||
|
pub fn empty(width: Size, height: Size) -> Layout {
|
||||||
|
Layout {
|
||||||
|
dimensions: Size2D::new(width, height),
|
||||||
|
actions: vec![],
|
||||||
|
debug_render: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Serialize this layout into an output buffer.
|
/// Serialize this layout into an output buffer.
|
||||||
pub fn serialize<W: Write>(&self, f: &mut W) -> io::Result<()> {
|
pub fn serialize<W: Write>(&self, f: &mut W) -> io::Result<()> {
|
||||||
writeln!(
|
writeln!(
|
||||||
|
@ -124,6 +124,8 @@ impl<'a, 'p> TreeLayouter<'a, 'p> {
|
|||||||
self.start_new_flex();
|
self.start_new_flex();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Command::AddFlex(layout) => self.flex.add(layout),
|
||||||
|
|
||||||
Command::SetAlignment(alignment) => {
|
Command::SetAlignment(alignment) => {
|
||||||
self.finish_flex()?;
|
self.finish_flex()?;
|
||||||
self.alignment = alignment;
|
self.alignment = alignment;
|
||||||
|
@ -5,6 +5,7 @@ use crate::func::Scope;
|
|||||||
mod align;
|
mod align;
|
||||||
mod boxed;
|
mod boxed;
|
||||||
mod breaks;
|
mod breaks;
|
||||||
|
mod spacing;
|
||||||
mod styles;
|
mod styles;
|
||||||
|
|
||||||
/// Useful imports for creating your own functions.
|
/// Useful imports for creating your own functions.
|
||||||
@ -20,6 +21,7 @@ pub mod prelude {
|
|||||||
pub use align::AlignFunc;
|
pub use align::AlignFunc;
|
||||||
pub use boxed::BoxFunc;
|
pub use boxed::BoxFunc;
|
||||||
pub use breaks::{LinebreakFunc, PagebreakFunc};
|
pub use breaks::{LinebreakFunc, PagebreakFunc};
|
||||||
|
pub use spacing::{HorizontalSpaceFunc, VerticalSpaceFunc};
|
||||||
pub use styles::{BoldFunc, ItalicFunc, MonospaceFunc};
|
pub use styles::{BoldFunc, ItalicFunc, MonospaceFunc};
|
||||||
|
|
||||||
/// Create a scope with all standard functions.
|
/// Create a scope with all standard functions.
|
||||||
@ -27,15 +29,21 @@ pub fn std() -> Scope {
|
|||||||
let mut std = Scope::new();
|
let mut std = Scope::new();
|
||||||
std.add::<AlignFunc>("align");
|
std.add::<AlignFunc>("align");
|
||||||
std.add::<BoxFunc>("box");
|
std.add::<BoxFunc>("box");
|
||||||
std.add::<LinebreakFunc>("linebreak");
|
|
||||||
|
std.add::<LinebreakFunc>("line.break");
|
||||||
std.add::<LinebreakFunc>("n");
|
std.add::<LinebreakFunc>("n");
|
||||||
std.add::<PagebreakFunc>("pagebreak");
|
std.add::<PagebreakFunc>("page.break");
|
||||||
|
|
||||||
|
std.add::<HorizontalSpaceFunc>("h");
|
||||||
|
std.add::<VerticalSpaceFunc>("v");
|
||||||
|
|
||||||
std.add::<BoldFunc>("bold");
|
std.add::<BoldFunc>("bold");
|
||||||
std.add::<ItalicFunc>("italic");
|
std.add::<ItalicFunc>("italic");
|
||||||
std.add::<MonospaceFunc>("mono");
|
std.add::<MonospaceFunc>("mono");
|
||||||
std
|
std
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Helpers for writing custom functions.
|
||||||
pub mod helpers {
|
pub mod helpers {
|
||||||
use super::prelude::*;
|
use super::prelude::*;
|
||||||
|
|
||||||
|
76
src/library/spacing.rs
Normal file
76
src/library/spacing.rs
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
use std::marker::PhantomData;
|
||||||
|
use super::prelude::*;
|
||||||
|
use crate::size::Size;
|
||||||
|
|
||||||
|
/// Adds vertical space.
|
||||||
|
pub type VerticalSpaceFunc = SpaceFunc<SpaceVertical>;
|
||||||
|
|
||||||
|
/// Adds horizontal space.
|
||||||
|
pub type HorizontalSpaceFunc = SpaceFunc<SpaceHorizontal>;
|
||||||
|
|
||||||
|
/// Adds generic space.
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub struct SpaceFunc<F: SpaceFlow> {
|
||||||
|
spacing: Spacing,
|
||||||
|
_phantom: PhantomData<F>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Absolute or font-relative spacing.
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
enum Spacing {
|
||||||
|
Absolute(Size),
|
||||||
|
Relative(f32),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F: SpaceFlow> Function for SpaceFunc<F> {
|
||||||
|
fn parse(header: &FuncHeader, body: Option<&str>, _: ParseContext) -> ParseResult<Self>
|
||||||
|
where Self: Sized {
|
||||||
|
if header.args.len() != 1 || !header.kwargs.is_empty() {
|
||||||
|
return err("align: expected exactly one positional argument");
|
||||||
|
}
|
||||||
|
|
||||||
|
let spacing = match header.args[0] {
|
||||||
|
Expression::Size(s) => Spacing::Absolute(s),
|
||||||
|
Expression::Number(f) => Spacing::Relative(f as f32),
|
||||||
|
_ => return err("space: expected size or number"),
|
||||||
|
};
|
||||||
|
|
||||||
|
if body.is_some() {
|
||||||
|
return err("space: expected no body");
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(SpaceFunc {
|
||||||
|
spacing,
|
||||||
|
_phantom: PhantomData,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn layout(&self, ctx: LayoutContext) -> LayoutResult<CommandList> {
|
||||||
|
let space = match self.spacing {
|
||||||
|
Spacing::Absolute(s) => s,
|
||||||
|
Spacing::Relative(f) => Size::pt(f * ctx.style.font_size),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(commands![F::cmd(space)])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait SpaceFlow: std::fmt::Debug + PartialEq + 'static {
|
||||||
|
fn cmd(space: Size) -> Command<'static>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub struct SpaceVertical;
|
||||||
|
impl SpaceFlow for SpaceVertical {
|
||||||
|
fn cmd(space: Size) -> Command<'static> {
|
||||||
|
Command::Add(Layout::empty(Size::zero(), space))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub struct SpaceHorizontal;
|
||||||
|
impl SpaceFlow for SpaceHorizontal {
|
||||||
|
fn cmd(space: Size) -> Command<'static> {
|
||||||
|
Command::AddFlex(Layout::empty(space, Size::zero()))
|
||||||
|
}
|
||||||
|
}
|
@ -404,13 +404,13 @@ fn is_identifier(string: &str) -> bool {
|
|||||||
let mut chars = string.chars();
|
let mut chars = string.chars();
|
||||||
|
|
||||||
match chars.next() {
|
match chars.next() {
|
||||||
Some(c) if !UnicodeXID::is_xid_start(c) => return false,
|
Some(c) if c != '.' && !UnicodeXID::is_xid_start(c) => return false,
|
||||||
None => return false,
|
None => return false,
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
while let Some(c) = chars.next() {
|
while let Some(c) = chars.next() {
|
||||||
if !UnicodeXID::is_xid_continue(c) {
|
if c != '.' && !UnicodeXID::is_xid_continue(c) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
// Over multiple pages after the pervious 3-page run.
|
// Over multiple pages after the pervious 3-page run.
|
||||||
[align: left][Left: {lorem:80}]
|
[align: left][Left: {lorem:80}]
|
||||||
|
|
||||||
[pagebreak]
|
[page.break]
|
||||||
|
|
||||||
// ---------------------------------- //
|
// ---------------------------------- //
|
||||||
// Context-modifying align.
|
// Context-modifying align.
|
||||||
@ -26,7 +26,7 @@ Right Again: {lorem:10}
|
|||||||
// Reset context-modifier.
|
// Reset context-modifier.
|
||||||
[align: left]
|
[align: left]
|
||||||
|
|
||||||
[pagebreak]
|
[page.break]
|
||||||
|
|
||||||
// ---------------------------------- //
|
// ---------------------------------- //
|
||||||
// All in one line.
|
// All in one line.
|
||||||
|
@ -9,9 +9,7 @@
|
|||||||
|
|
||||||
[align: right][*WiSe 2019/2020* [n] Week 1]
|
[align: right][*WiSe 2019/2020* [n] Week 1]
|
||||||
]
|
]
|
||||||
|
[v: 3mm]
|
||||||
// hack for more whitespace
|
|
||||||
[box][]
|
|
||||||
|
|
||||||
[align: center][
|
[align: center][
|
||||||
*3. Ubungsblatt Computerorientierte Mathematik II* [n]
|
*3. Ubungsblatt Computerorientierte Mathematik II* [n]
|
||||||
@ -27,5 +25,3 @@ Ein _Binärbaum_ ist ein Wurzelbaum, in dem jeder Knoten ≤ 2 Kinder hat.
|
|||||||
Die Tiefe eines Knotens _v_ ist die Länge des eindeutigen Weges von der Wurzel
|
Die Tiefe eines Knotens _v_ ist die Länge des eindeutigen Weges von der Wurzel
|
||||||
zu _v_, und die Höhe von _v_ ist die Länge eines längsten (absteigenden) Weges
|
zu _v_, und die Höhe von _v_ ist die Länge eines längsten (absteigenden) Weges
|
||||||
von _v_ zu einem Blatt. Die Höhe des Baumes ist die Höhe der Wurzel.
|
von _v_ zu einem Blatt. Die Höhe des Baumes ist die Höhe der Wurzel.
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
{size:150pt*200pt}
|
{size:150pt*200pt}
|
||||||
{lorem:100}
|
{lorem:100}
|
||||||
|
|
||||||
[pagebreak]
|
[page.break]
|
||||||
[pagebreak]
|
[page.break]
|
||||||
|
|
||||||
{lorem:20}
|
{lorem:20}
|
||||||
[pagebreak]
|
[page.break]
|
||||||
|
|
||||||
{lorem:150}
|
{lorem:150}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user