mirror of
https://github.com/typst/typst
synced 2025-05-18 11:05:28 +08:00
More useful Debug
impls
This commit is contained in:
parent
72eb243e26
commit
a493b9533a
@ -360,7 +360,6 @@ impl FaceInfo {
|
||||
data: &'a [u8],
|
||||
) -> impl Iterator<Item = FaceInfo> + 'a {
|
||||
let count = ttf_parser::fonts_in_collection(data).unwrap_or(1);
|
||||
|
||||
(0 .. count).filter_map(move |index| {
|
||||
let face = ttf_parser::Face::from_slice(data, index).ok()?;
|
||||
let mut family = find_name(face.names(), name_id::TYPOGRAPHIC_FAMILY)
|
||||
|
@ -102,7 +102,6 @@ impl Image {
|
||||
pub fn parse(data: &[u8]) -> io::Result<Self> {
|
||||
let cursor = io::Cursor::new(data);
|
||||
let reader = ImageReader::new(cursor).with_guessed_format()?;
|
||||
|
||||
let format = reader.format().ok_or_else(|| {
|
||||
io::Error::new(io::ErrorKind::InvalidData, "unknown image format")
|
||||
})?;
|
||||
|
@ -1,3 +1,4 @@
|
||||
use std::fmt::{self, Debug, Formatter};
|
||||
use std::rc::Rc;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
@ -9,7 +10,7 @@ use crate::geom::{Em, Length, Path, Point, Size};
|
||||
use crate::image::ImageId;
|
||||
|
||||
/// A finished layout with elements at fixed positions.
|
||||
#[derive(Debug, Default, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
||||
#[derive(Default, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Frame {
|
||||
/// The size of the frame.
|
||||
pub size: Size,
|
||||
@ -21,7 +22,7 @@ pub struct Frame {
|
||||
|
||||
/// A frame can contain two different kinds of children: a leaf element or a
|
||||
/// nested frame.
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
||||
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
|
||||
pub enum FrameChild {
|
||||
/// A leaf node in the frame tree.
|
||||
Element(Element),
|
||||
@ -29,6 +30,66 @@ pub enum FrameChild {
|
||||
Frame(Option<usize>, Rc<Frame>),
|
||||
}
|
||||
|
||||
/// The building block frames are composed of.
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
||||
pub enum Element {
|
||||
/// Shaped text.
|
||||
Text(Text),
|
||||
/// A geometric shape and the paint which with it should be filled or
|
||||
/// stroked.
|
||||
Geometry(Geometry, Paint),
|
||||
/// A raster image.
|
||||
Image(ImageId, Size),
|
||||
/// A link to an external resource.
|
||||
Link(String, Size),
|
||||
}
|
||||
|
||||
/// A run of shaped text.
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Text {
|
||||
/// The font face the glyphs are contained in.
|
||||
pub face_id: FaceId,
|
||||
/// The font size.
|
||||
pub size: Length,
|
||||
/// The width of the text run.
|
||||
pub width: Length,
|
||||
/// Glyph color.
|
||||
pub fill: Paint,
|
||||
/// The glyphs.
|
||||
pub glyphs: Vec<Glyph>,
|
||||
}
|
||||
|
||||
/// A glyph in a run of shaped text.
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Glyph {
|
||||
/// The glyph's index in the face.
|
||||
pub id: u16,
|
||||
/// The advance width of the glyph.
|
||||
pub x_advance: Em,
|
||||
/// The horizontal offset of the glyph.
|
||||
pub x_offset: Em,
|
||||
}
|
||||
|
||||
/// A geometric shape.
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
||||
pub enum Geometry {
|
||||
/// A filled rectangle with its origin in the topleft corner.
|
||||
Rect(Size),
|
||||
/// A filled ellipse with its origin in the center.
|
||||
Ellipse(Size),
|
||||
/// A stroked line to a point (relative to its position) with a thickness.
|
||||
Line(Point, Length),
|
||||
/// A filled bezier path.
|
||||
Path(Path),
|
||||
}
|
||||
|
||||
/// How a fill or stroke should be painted.
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
|
||||
pub enum Paint {
|
||||
/// A solid color.
|
||||
Color(Color),
|
||||
}
|
||||
|
||||
impl Frame {
|
||||
/// Create a new, empty frame.
|
||||
#[track_caller]
|
||||
@ -111,62 +172,29 @@ impl<'a> Iterator for Elements<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The building block frames are composed of.
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
||||
pub enum Element {
|
||||
/// Shaped text.
|
||||
Text(Text),
|
||||
/// A geometric shape and the paint which with it should be filled or
|
||||
/// stroked.
|
||||
Geometry(Geometry, Paint),
|
||||
/// A raster image.
|
||||
Image(ImageId, Size),
|
||||
/// A link to an external resource.
|
||||
Link(String, Size),
|
||||
impl Debug for Frame {
|
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||
struct Children<'a>(&'a [(Point, FrameChild)]);
|
||||
|
||||
impl Debug for Children<'_> {
|
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||
f.debug_map().entries(self.0.iter().map(|(k, v)| (k, v))).finish()
|
||||
}
|
||||
}
|
||||
|
||||
f.debug_struct("Frame")
|
||||
.field("size", &self.size)
|
||||
.field("baseline", &self.baseline)
|
||||
.field("children", &Children(&self.children))
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
/// A run of shaped text.
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Text {
|
||||
/// The font face the glyphs are contained in.
|
||||
pub face_id: FaceId,
|
||||
/// The font size.
|
||||
pub size: Length,
|
||||
/// The width of the text run.
|
||||
pub width: Length,
|
||||
/// Glyph color.
|
||||
pub fill: Paint,
|
||||
/// The glyphs.
|
||||
pub glyphs: Vec<Glyph>,
|
||||
}
|
||||
|
||||
/// A glyph in a run of shaped text.
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Glyph {
|
||||
/// The glyph's index in the face.
|
||||
pub id: u16,
|
||||
/// The advance width of the glyph.
|
||||
pub x_advance: Em,
|
||||
/// The horizontal offset of the glyph.
|
||||
pub x_offset: Em,
|
||||
}
|
||||
|
||||
/// A geometric shape.
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
||||
pub enum Geometry {
|
||||
/// A filled rectangle with its origin in the topleft corner.
|
||||
Rect(Size),
|
||||
/// A filled ellipse with its origin in the center.
|
||||
Ellipse(Size),
|
||||
/// A stroked line to a point (relative to its position) with a thickness.
|
||||
Line(Point, Length),
|
||||
/// A filled bezier path.
|
||||
Path(Path),
|
||||
}
|
||||
|
||||
/// How a fill or stroke should be painted.
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
|
||||
pub enum Paint {
|
||||
/// A solid color.
|
||||
Color(Color),
|
||||
impl Debug for FrameChild {
|
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::Element(element) => element.fmt(f),
|
||||
Self::Frame(_, frame) => frame.fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
use std::fmt::{self, Debug, Formatter};
|
||||
use std::rc::Rc;
|
||||
|
||||
use unicode_bidi::{BidiInfo, Level};
|
||||
@ -22,7 +23,6 @@ pub struct ParNode {
|
||||
}
|
||||
|
||||
/// A child of a paragraph node.
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "layout-cache", derive(Hash))]
|
||||
pub enum ParChild {
|
||||
/// Spacing between other nodes.
|
||||
@ -93,6 +93,16 @@ impl From<ParNode> for LayoutNode {
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for ParChild {
|
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||
match self {
|
||||
ParChild::Spacing(v) => write!(f, "Spacing({:?})", v),
|
||||
ParChild::Text(text, ..) => write!(f, "Text({:?})", text),
|
||||
ParChild::Any(node, ..) => f.debug_tuple("Any").field(node).finish(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A paragraph representation in which children are already layouted and text
|
||||
/// is separated into shapable runs.
|
||||
struct ParLayouter<'a> {
|
||||
|
@ -10,7 +10,6 @@ use {
|
||||
};
|
||||
|
||||
/// A tree of layout nodes.
|
||||
#[derive(Debug)]
|
||||
pub struct LayoutTree {
|
||||
/// Runs of pages with the same properties.
|
||||
pub runs: Vec<PageRun>,
|
||||
@ -23,6 +22,12 @@ impl LayoutTree {
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for LayoutTree {
|
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||
f.debug_list().entries(&self.runs).finish()
|
||||
}
|
||||
}
|
||||
|
||||
/// A run of pages that all have the same properties.
|
||||
#[derive(Debug)]
|
||||
pub struct PageRun {
|
||||
|
Loading…
x
Reference in New Issue
Block a user