From cfb6760fa27665dd751f3ee0e52241041f81d8c6 Mon Sep 17 00:00:00 2001 From: tigregalis Date: Fri, 12 May 2023 10:23:43 +0800 Subject: [PATCH 1/2] introduce `Buffer::new_empty` --- src/buffer.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/buffer.rs b/src/buffer.rs index 7ddd20d873..3eb96bb5a3 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -324,6 +324,30 @@ pub struct Buffer { } impl Buffer { + /// Create an empty [`Buffer`] with the provided [`Metrics`]. + /// This is useful for initializing a [`Buffer`] without a [`FontSystem`]. + /// + /// You must populate the [`Buffer`] with at least one [`BufferLine`] before shaping and layout, + /// for example by calling [`Buffer::set_text`]. + /// + /// If you have a [`FontSystem`] in scope, you should use [`Buffer::new`] instead. + /// + /// # Panics + /// + /// Will panic if `metrics.line_height` is zero. + pub fn new_empty(metrics: Metrics) -> Self { + assert_ne!(metrics.line_height, 0.0, "line height cannot be 0"); + Self { + lines: Vec::new(), + metrics, + width: 0.0, + height: 0.0, + scroll: 0, + redraw: false, + wrap: Wrap::Word, + } + } + /// Create a new [`Buffer`] with the provided [`FontSystem`] and [`Metrics`] /// /// # Panics From 2ed9c34796e80b653712dbb25da7d506626b09fd Mon Sep 17 00:00:00 2001 From: tigregalis Date: Fri, 12 May 2023 10:26:02 +0800 Subject: [PATCH 2/2] use `Buffer::new_empty` in `Buffer::new` --- src/buffer.rs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/buffer.rs b/src/buffer.rs index 3eb96bb5a3..cdf6a11e2a 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -354,17 +354,7 @@ impl Buffer { /// /// Will panic if `metrics.line_height` is zero. pub fn new(font_system: &mut FontSystem, metrics: Metrics) -> Self { - assert_ne!(metrics.line_height, 0.0, "line height cannot be 0"); - - let mut buffer = Self { - lines: Vec::new(), - metrics, - width: 0.0, - height: 0.0, - scroll: 0, - redraw: false, - wrap: Wrap::Word, - }; + let mut buffer = Self::new_empty(metrics); buffer.set_text(font_system, "", Attrs::new(), Shaping::Advanced); buffer }