diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-04-21 21:17:25 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-04-21 21:17:25 +0200 |
| commit | 72478946c261f04754c11f8a6abf6eb0f43dea31 (patch) | |
| tree | b2a621804d39ace4e57ec4a51b34d40bb4a98987 /src/layout | |
| parent | df58a4d89b67783b1ffc5c3b7282302d59db8c70 (diff) | |
Make frames serializable 📚
This also makes serialization support non-optional since it's too much feature-management for too little benefit.
Diffstat (limited to 'src/layout')
| -rw-r--r-- | src/layout/background.rs | 2 | ||||
| -rw-r--r-- | src/layout/frame.rs | 23 | ||||
| -rw-r--r-- | src/layout/shaping.rs | 8 |
3 files changed, 20 insertions, 13 deletions
diff --git a/src/layout/background.rs b/src/layout/background.rs index d3408182..515eef71 100644 --- a/src/layout/background.rs +++ b/src/layout/background.rs @@ -12,7 +12,7 @@ pub struct BackgroundNode { } /// The kind of shape to use as a background. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum BackgroundShape { Rect, Ellipse, diff --git a/src/layout/frame.rs b/src/layout/frame.rs index 21fdbf28..24ba65ce 100644 --- a/src/layout/frame.rs +++ b/src/layout/frame.rs @@ -1,12 +1,13 @@ use fontdock::FaceId; -use ttf_parser::GlyphId; use crate::color::Color; use crate::env::ResourceId; use crate::geom::{Length, Path, Point, Size}; +use serde::{Deserialize, Serialize}; + /// A finished layout with elements at fixed positions. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Frame { /// The size of the frame. pub size: Size, @@ -37,7 +38,7 @@ impl Frame { } /// The building block frames are composed of. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub enum Element { /// Shaped text. Text(Text), @@ -48,7 +49,7 @@ pub enum Element { } /// A run of shaped text. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Text { /// The font face the glyphs are contained in. pub face_id: FaceId, @@ -61,10 +62,10 @@ pub struct Text { } /// A glyph in a run of shaped text. -#[derive(Debug, Copy, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)] pub struct Glyph { /// The glyph's ID in the face. - pub id: GlyphId, + pub id: u16, /// The advance width of the glyph. pub x_advance: Length, /// The horizontal offset of the glyph. @@ -76,7 +77,7 @@ impl Text { pub fn encode_glyphs_be(&self) -> Vec<u8> { let mut bytes = Vec::with_capacity(2 * self.glyphs.len()); for glyph in &self.glyphs { - let id = glyph.id.0; + let id = glyph.id; bytes.push((id >> 8) as u8); bytes.push((id & 0xff) as u8); } @@ -85,7 +86,7 @@ impl Text { } /// A shape with some kind of fill. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Geometry { /// The shape to draw. pub shape: Shape, @@ -98,7 +99,7 @@ pub struct Geometry { } /// Some shape. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub enum Shape { /// A rectangle with its origin in the topleft corner. Rect(Size), @@ -109,7 +110,7 @@ pub enum Shape { } /// The kind of graphic fill to be applied to a [`Shape`]. -#[derive(Debug, Copy, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)] pub enum Fill { /// The fill is a color. Color(Color), @@ -118,7 +119,7 @@ pub enum Fill { } /// An image element. -#[derive(Debug, Copy, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)] pub struct Image { /// The image resource. pub res: ResourceId, diff --git a/src/layout/shaping.rs b/src/layout/shaping.rs index faa178d3..47d19a62 100644 --- a/src/layout/shaping.rs +++ b/src/layout/shaping.rs @@ -18,6 +18,7 @@ use crate::util::SliceExt; /// This type contains owned or borrowed shaped text runs, which can be /// measured, used to reshape substrings more quickly and converted into a /// frame. +#[derive(Clone)] pub struct ShapedText<'a> { /// The text that was shaped. pub text: &'a str, @@ -53,6 +54,7 @@ pub struct ShapedGlyph { } /// A visual side. +#[derive(Debug, Copy, Clone, Eq, PartialEq)] enum Side { Left, Right, @@ -77,7 +79,11 @@ impl<'a> ShapedText<'a> { for glyph in group { let x_advance = face.convert(glyph.x_advance).scale(self.props.size); let x_offset = face.convert(glyph.x_offset).scale(self.props.size); - text.glyphs.push(Glyph { id: glyph.glyph_id, x_advance, x_offset }); + text.glyphs.push(Glyph { + id: glyph.glyph_id.0, + x_advance, + x_offset, + }); offset += x_advance; } |
