diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-07-20 20:21:56 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-07-21 11:28:29 +0200 |
| commit | 9488b1b850152eb564dbfefc898c962bdac73eb4 (patch) | |
| tree | 0a99487cddfee1a46b5802dc6b64c81b70e549da /src/layout | |
| parent | 8000783f95ee007d9dda6f1dcc1c42c8e607b122 (diff) | |
Main context struct
Diffstat (limited to 'src/layout')
| -rw-r--r-- | src/layout/image.rs | 2 | ||||
| -rw-r--r-- | src/layout/mod.rs | 56 | ||||
| -rw-r--r-- | src/layout/shaping.rs | 16 |
3 files changed, 43 insertions, 31 deletions
diff --git a/src/layout/image.rs b/src/layout/image.rs index 07d7799c..2c20642b 100644 --- a/src/layout/image.rs +++ b/src/layout/image.rs @@ -28,7 +28,7 @@ impl Layout for ImageNode { let width = self.width.map(|w| w.resolve(base.width)); let height = self.height.map(|w| w.resolve(base.height)); - let dimensions = ctx.cache.image.get(self.id).buf.dimensions(); + let dimensions = ctx.images.get(self.id).buf.dimensions(); let pixel_width = dimensions.0 as f64; let pixel_height = dimensions.1 as f64; let pixel_ratio = pixel_width / pixel_height; diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 523d1a92..7f8ee4ff 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -32,22 +32,16 @@ use std::rc::Rc; #[cfg(feature = "layout-cache")] use fxhash::FxHasher64; -use crate::cache::Cache; +use crate::font::FontCache; use crate::geom::*; +use crate::image::ImageCache; use crate::loading::Loader; +use crate::Context; /// Layout a tree into a collection of frames. -pub fn layout( - loader: &mut dyn Loader, - cache: &mut Cache, - tree: &LayoutTree, -) -> Vec<Rc<Frame>> { - tree.layout(&mut LayoutContext { - loader, - cache, - #[cfg(feature = "layout-cache")] - level: 0, - }) +pub fn layout(ctx: &mut Context, tree: &LayoutTree) -> Vec<Rc<Frame>> { + let mut ctx = LayoutContext::new(ctx); + tree.layout(&mut ctx) } /// A tree of layout nodes. @@ -129,15 +123,15 @@ impl Layout for LayoutNode { #[cfg(feature = "layout-cache")] { ctx.level += 1; - let frames = - ctx.cache.layout.get(self.hash, regions.clone()).unwrap_or_else(|| { - let frames = self.node.layout(ctx, regions); - ctx.cache.layout.insert(self.hash, frames.clone(), ctx.level - 1); - frames - }); + let frames = ctx.layouts.get(self.hash, regions.clone()).unwrap_or_else(|| { + let frames = self.node.layout(ctx, regions); + ctx.layouts.insert(self.hash, frames.clone(), ctx.level - 1); + frames + }); ctx.level -= 1; frames } + #[cfg(not(feature = "layout-cache"))] self.node.layout(ctx, regions) } @@ -214,14 +208,34 @@ pub trait Layout { /// The context for layouting. pub struct LayoutContext<'a> { /// The loader from which fonts are loaded. - pub loader: &'a mut dyn Loader, - /// A cache for loaded fonts and artifacts from past layouting. - pub cache: &'a mut Cache, + pub loader: &'a dyn Loader, + /// The cache for parsed font faces. + pub fonts: &'a mut FontCache, + /// The cache for decoded imges. + pub images: &'a mut ImageCache, + /// The cache for layouting artifacts. + #[cfg(feature = "layout-cache")] + pub layouts: &'a mut LayoutCache, /// How deeply nested the current layout tree position is. #[cfg(feature = "layout-cache")] pub level: usize, } +impl<'a> LayoutContext<'a> { + /// Create a new layout context. + pub fn new(ctx: &'a mut Context) -> Self { + Self { + loader: ctx.loader.as_ref(), + fonts: &mut ctx.fonts, + images: &mut ctx.images, + #[cfg(feature = "layout-cache")] + layouts: &mut ctx.layouts, + #[cfg(feature = "layout-cache")] + level: 0, + } + } +} + /// A sequence of regions to layout into. #[derive(Debug, Clone, Eq, PartialEq)] pub struct Regions { diff --git a/src/layout/shaping.rs b/src/layout/shaping.rs index 9b8774cc..5e1bc327 100644 --- a/src/layout/shaping.rs +++ b/src/layout/shaping.rs @@ -235,7 +235,7 @@ fn shape_segment<'a>( } } - if let Some(id) = ctx.cache.font.select(ctx.loader, family, variant) { + if let Some(id) = ctx.fonts.select(family, variant) { break (id, true); } } @@ -262,7 +262,7 @@ fn shape_segment<'a>( }); // Shape! - let mut face = ctx.cache.font.get(face_id); + let mut face = ctx.fonts.get(face_id); let buffer = rustybuzz::shape(face.ttf(), &[], buffer); let infos = buffer.glyph_infos(); let pos = buffer.glyph_positions(); @@ -337,7 +337,7 @@ fn shape_segment<'a>( first_face, ); - face = ctx.cache.font.get(face_id); + face = ctx.fonts.get(face_id); } i += 1; @@ -351,8 +351,6 @@ fn measure( glyphs: &[ShapedGlyph], state: &FontState, ) -> (Size, Length) { - let cache = &mut ctx.cache.font; - let mut width = Length::zero(); let mut top = Length::zero(); let mut bottom = Length::zero(); @@ -365,14 +363,14 @@ fn measure( // When there are no glyphs, we just use the vertical metrics of the // first available font. for family in state.families.iter() { - if let Some(face_id) = cache.select(ctx.loader, family, state.variant) { - expand_vertical(cache.get(face_id)); + if let Some(face_id) = ctx.fonts.select(family, state.variant) { + expand_vertical(ctx.fonts.get(face_id)); break; } } } else { for (face_id, group) in glyphs.group_by_key(|g| g.face_id) { - let face = cache.get(face_id); + let face = ctx.fonts.get(face_id); expand_vertical(face); for glyph in group { @@ -394,7 +392,7 @@ fn decorate( state: &FontState, ) { let mut apply = |substate: &LineState, metrics: fn(&Face) -> &LineMetrics| { - let metrics = metrics(&ctx.cache.font.get(face_id)); + let metrics = metrics(ctx.fonts.get(face_id)); let stroke = substate.stroke.unwrap_or(state.fill); |
