mirror of
https://github.com/typst/typst
synced 2025-05-13 12:36:23 +08:00
Move Fragment
into library
This commit is contained in:
parent
669f9707c3
commit
924a27bc37
82
library/src/layout/fragment.rs
Normal file
82
library/src/layout/fragment.rs
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
/// A partial layout result.
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Fragment(Vec<Frame>);
|
||||||
|
|
||||||
|
impl Fragment {
|
||||||
|
/// Create a fragment from a single frame.
|
||||||
|
pub fn frame(frame: Frame) -> Self {
|
||||||
|
Self(vec![frame])
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a fragment from multiple frames.
|
||||||
|
pub fn frames(frames: Vec<Frame>) -> Self {
|
||||||
|
Self(frames)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The number of frames in the fragment.
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.0.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extract the first and only frame.
|
||||||
|
///
|
||||||
|
/// Panics if there are multiple frames.
|
||||||
|
#[track_caller]
|
||||||
|
pub fn into_frame(self) -> Frame {
|
||||||
|
assert_eq!(self.0.len(), 1, "expected exactly one frame");
|
||||||
|
self.0.into_iter().next().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extract the frames.
|
||||||
|
pub fn into_frames(self) -> Vec<Frame> {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Iterate over the contained frames.
|
||||||
|
pub fn iter(&self) -> std::slice::Iter<Frame> {
|
||||||
|
self.0.iter()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Iterate over the contained frames.
|
||||||
|
pub fn iter_mut(&mut self) -> std::slice::IterMut<Frame> {
|
||||||
|
self.0.iter_mut()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Debug for Fragment {
|
||||||
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
|
match self.0.as_slice() {
|
||||||
|
[frame] => frame.fmt(f),
|
||||||
|
frames => frames.fmt(f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoIterator for Fragment {
|
||||||
|
type Item = Frame;
|
||||||
|
type IntoIter = std::vec::IntoIter<Frame>;
|
||||||
|
|
||||||
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
|
self.0.into_iter()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> IntoIterator for &'a Fragment {
|
||||||
|
type Item = &'a Frame;
|
||||||
|
type IntoIter = std::slice::Iter<'a, Frame>;
|
||||||
|
|
||||||
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
|
self.0.iter()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> IntoIterator for &'a mut Fragment {
|
||||||
|
type Item = &'a mut Frame;
|
||||||
|
type IntoIter = std::slice::IterMut<'a, Frame>;
|
||||||
|
|
||||||
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
|
self.0.iter_mut()
|
||||||
|
}
|
||||||
|
}
|
@ -6,6 +6,7 @@ mod container;
|
|||||||
#[path = "enum.rs"]
|
#[path = "enum.rs"]
|
||||||
mod enum_;
|
mod enum_;
|
||||||
mod flow;
|
mod flow;
|
||||||
|
mod fragment;
|
||||||
mod grid;
|
mod grid;
|
||||||
mod hide;
|
mod hide;
|
||||||
mod list;
|
mod list;
|
||||||
@ -25,6 +26,7 @@ pub use self::columns::*;
|
|||||||
pub use self::container::*;
|
pub use self::container::*;
|
||||||
pub use self::enum_::*;
|
pub use self::enum_::*;
|
||||||
pub use self::flow::*;
|
pub use self::flow::*;
|
||||||
|
pub use self::fragment::*;
|
||||||
pub use self::grid::*;
|
pub use self::grid::*;
|
||||||
pub use self::hide::*;
|
pub use self::hide::*;
|
||||||
pub use self::list::*;
|
pub use self::list::*;
|
||||||
|
@ -28,6 +28,6 @@ pub use typst::util::{format_eco, EcoString};
|
|||||||
pub use typst::World;
|
pub use typst::World;
|
||||||
|
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use crate::layout::{Inline, Layout, Regions};
|
pub use crate::layout::{Fragment, Inline, Layout, Regions};
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use crate::shared::{Behave, Behaviour, ContentExt, StyleMapExt};
|
pub use crate::shared::{Behave, Behaviour, ContentExt, StyleMapExt};
|
||||||
|
81
src/doc.rs
81
src/doc.rs
@ -27,87 +27,6 @@ pub struct Document {
|
|||||||
pub author: Option<EcoString>,
|
pub author: Option<EcoString>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A partial layout result.
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Fragment(Vec<Frame>);
|
|
||||||
|
|
||||||
impl Fragment {
|
|
||||||
/// Create a fragment from a single frame.
|
|
||||||
pub fn frame(frame: Frame) -> Self {
|
|
||||||
Self(vec![frame])
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create a fragment from multiple frames.
|
|
||||||
pub fn frames(frames: Vec<Frame>) -> Self {
|
|
||||||
Self(frames)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The number of frames in the fragment.
|
|
||||||
pub fn len(&self) -> usize {
|
|
||||||
self.0.len()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Extract the first and only frame.
|
|
||||||
///
|
|
||||||
/// Panics if there are multiple frames.
|
|
||||||
#[track_caller]
|
|
||||||
pub fn into_frame(self) -> Frame {
|
|
||||||
assert_eq!(self.0.len(), 1, "expected exactly one frame");
|
|
||||||
self.0.into_iter().next().unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Extract the frames.
|
|
||||||
pub fn into_frames(self) -> Vec<Frame> {
|
|
||||||
self.0
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Iterate over the contained frames.
|
|
||||||
pub fn iter(&self) -> std::slice::Iter<Frame> {
|
|
||||||
self.0.iter()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Iterate over the contained frames.
|
|
||||||
pub fn iter_mut(&mut self) -> std::slice::IterMut<Frame> {
|
|
||||||
self.0.iter_mut()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for Fragment {
|
|
||||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
||||||
match self.0.as_slice() {
|
|
||||||
[frame] => frame.fmt(f),
|
|
||||||
frames => frames.fmt(f),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IntoIterator for Fragment {
|
|
||||||
type Item = Frame;
|
|
||||||
type IntoIter = std::vec::IntoIter<Frame>;
|
|
||||||
|
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
|
||||||
self.0.into_iter()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> IntoIterator for &'a Fragment {
|
|
||||||
type Item = &'a Frame;
|
|
||||||
type IntoIter = std::slice::Iter<'a, Frame>;
|
|
||||||
|
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
|
||||||
self.0.iter()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> IntoIterator for &'a mut Fragment {
|
|
||||||
type Item = &'a mut Frame;
|
|
||||||
type IntoIter = std::slice::IterMut<'a, Frame>;
|
|
||||||
|
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
|
||||||
self.0.iter_mut()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A finished layout with elements at fixed positions.
|
/// A finished layout with elements at fixed positions.
|
||||||
#[derive(Default, Clone)]
|
#[derive(Default, Clone)]
|
||||||
pub struct Frame {
|
pub struct Frame {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user