summaryrefslogtreecommitdiff
path: root/src/layout
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-09-26 12:24:24 +0200
committerLaurenz <laurmaedje@gmail.com>2021-09-26 12:24:24 +0200
commita493b9533a894b23fd33b307495919faee1c4a14 (patch)
tree8f0a5ec91a7236376af3b2267680a22adb924e57 /src/layout
parent72eb243e269fc74c9e59341e384ec3667c7848bf (diff)
More useful `Debug` impls
Diffstat (limited to 'src/layout')
-rw-r--r--src/layout/frame.rs140
-rw-r--r--src/layout/par.rs12
-rw-r--r--src/layout/tree.rs7
3 files changed, 101 insertions, 58 deletions
diff --git a/src/layout/frame.rs b/src/layout/frame.rs
index f8d901ce..667c4566 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<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),
-}
-
-/// 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>,
-}
+impl Debug for Frame {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ struct Children<'a>(&'a [(Point, FrameChild)]);
-/// 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,
-}
+ 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()
+ }
+ }
-/// 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),
+ f.debug_struct("Frame")
+ .field("size", &self.size)
+ .field("baseline", &self.baseline)
+ .field("children", &Children(&self.children))
+ .finish()
+ }
}
-/// 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 18a701cd..c8ced2f8 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<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> {
diff --git a/src/layout/tree.rs b/src/layout/tree.rs
index 36d0ac25..3b7c4937 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<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 {