diff --git a/src/font.rs b/src/font.rs index 41bf9a81c..7113026be 100644 --- a/src/font.rs +++ b/src/font.rs @@ -360,7 +360,6 @@ impl FaceInfo { data: &'a [u8], ) -> impl Iterator + '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) diff --git a/src/image.rs b/src/image.rs index 455f3105d..512b24b16 100644 --- a/src/image.rs +++ b/src/image.rs @@ -102,7 +102,6 @@ impl Image { pub fn parse(data: &[u8]) -> io::Result { 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") })?; diff --git a/src/layout/frame.rs b/src/layout/frame.rs index f8d901cea..667c45662 100644 --- a/src/layout/frame.rs +++ b/src/layout/frame.rs @@ -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, Rc), } +/// 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, +} + +/// 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, -} - -/// 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), + } + } } diff --git a/src/layout/par.rs b/src/layout/par.rs index 18a701cd4..c8ced2f84 100644 --- a/src/layout/par.rs +++ b/src/layout/par.rs @@ -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 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> { diff --git a/src/layout/tree.rs b/src/layout/tree.rs index 36d0ac25b..3b7c4937c 100644 --- a/src/layout/tree.rs +++ b/src/layout/tree.rs @@ -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, @@ -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 {