More useful Debug impls

This commit is contained in:
Laurenz 2021-09-26 12:24:24 +02:00
parent 72eb243e26
commit a493b9533a
5 changed files with 103 additions and 62 deletions

View File

@ -360,7 +360,6 @@ impl FaceInfo {
data: &'a [u8], data: &'a [u8],
) -> impl Iterator<Item = FaceInfo> + 'a { ) -> impl Iterator<Item = FaceInfo> + 'a {
let count = ttf_parser::fonts_in_collection(data).unwrap_or(1); let count = ttf_parser::fonts_in_collection(data).unwrap_or(1);
(0 .. count).filter_map(move |index| { (0 .. count).filter_map(move |index| {
let face = ttf_parser::Face::from_slice(data, index).ok()?; let face = ttf_parser::Face::from_slice(data, index).ok()?;
let mut family = find_name(face.names(), name_id::TYPOGRAPHIC_FAMILY) let mut family = find_name(face.names(), name_id::TYPOGRAPHIC_FAMILY)

View File

@ -102,7 +102,6 @@ impl Image {
pub fn parse(data: &[u8]) -> io::Result<Self> { pub fn parse(data: &[u8]) -> io::Result<Self> {
let cursor = io::Cursor::new(data); let cursor = io::Cursor::new(data);
let reader = ImageReader::new(cursor).with_guessed_format()?; let reader = ImageReader::new(cursor).with_guessed_format()?;
let format = reader.format().ok_or_else(|| { let format = reader.format().ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidData, "unknown image format") io::Error::new(io::ErrorKind::InvalidData, "unknown image format")
})?; })?;

View File

@ -1,3 +1,4 @@
use std::fmt::{self, Debug, Formatter};
use std::rc::Rc; use std::rc::Rc;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -9,7 +10,7 @@ use crate::geom::{Em, Length, Path, Point, Size};
use crate::image::ImageId; use crate::image::ImageId;
/// A finished layout with elements at fixed positions. /// 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 { pub struct Frame {
/// The size of the frame. /// The size of the frame.
pub size: Size, pub size: Size,
@ -21,7 +22,7 @@ pub struct Frame {
/// A frame can contain two different kinds of children: a leaf element or a /// A frame can contain two different kinds of children: a leaf element or a
/// nested frame. /// nested frame.
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] #[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum FrameChild { pub enum FrameChild {
/// A leaf node in the frame tree. /// A leaf node in the frame tree.
Element(Element), Element(Element),
@ -29,6 +30,66 @@ pub enum FrameChild {
Frame(Option<usize>, Rc<Frame>), 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 { impl Frame {
/// Create a new, empty frame. /// Create a new, empty frame.
#[track_caller] #[track_caller]
@ -111,62 +172,29 @@ impl<'a> Iterator for Elements<'a> {
} }
} }
/// The building block frames are composed of. impl Debug for Frame {
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] fn fmt(&self, f: &mut Formatter) -> fmt::Result {
pub enum Element { struct Children<'a>(&'a [(Point, FrameChild)]);
/// Shaped text.
Text(Text), impl Debug for Children<'_> {
/// A geometric shape and the paint which with it should be filled or fn fmt(&self, f: &mut Formatter) -> fmt::Result {
/// stroked. f.debug_map().entries(self.0.iter().map(|(k, v)| (k, v))).finish()
Geometry(Geometry, Paint), }
/// A raster image.
Image(ImageId, Size),
/// A link to an external resource.
Link(String, Size),
} }
/// A run of shaped text. f.debug_struct("Frame")
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] .field("size", &self.size)
pub struct Text { .field("baseline", &self.baseline)
/// The font face the glyphs are contained in. .field("children", &Children(&self.children))
pub face_id: FaceId, .finish()
/// 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. impl Debug for FrameChild {
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)] fn fmt(&self, f: &mut Formatter) -> fmt::Result {
pub struct Glyph { match self {
/// The glyph's index in the face. Self::Element(element) => element.fmt(f),
pub id: u16, Self::Frame(_, frame) => frame.fmt(f),
/// 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),
} }

View File

@ -1,3 +1,4 @@
use std::fmt::{self, Debug, Formatter};
use std::rc::Rc; use std::rc::Rc;
use unicode_bidi::{BidiInfo, Level}; use unicode_bidi::{BidiInfo, Level};
@ -22,7 +23,6 @@ pub struct ParNode {
} }
/// A child of a paragraph node. /// A child of a paragraph node.
#[derive(Debug)]
#[cfg_attr(feature = "layout-cache", derive(Hash))] #[cfg_attr(feature = "layout-cache", derive(Hash))]
pub enum ParChild { pub enum ParChild {
/// Spacing between other nodes. /// 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 /// A paragraph representation in which children are already layouted and text
/// is separated into shapable runs. /// is separated into shapable runs.
struct ParLayouter<'a> { struct ParLayouter<'a> {

View File

@ -10,7 +10,6 @@ use {
}; };
/// A tree of layout nodes. /// A tree of layout nodes.
#[derive(Debug)]
pub struct LayoutTree { pub struct LayoutTree {
/// Runs of pages with the same properties. /// Runs of pages with the same properties.
pub runs: Vec<PageRun>, 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. /// A run of pages that all have the same properties.
#[derive(Debug)] #[derive(Debug)]
pub struct PageRun { pub struct PageRun {