Skip to content

Commit

Permalink
Merge pull request #127 from tigregalis/buffer-no-font-system
Browse files Browse the repository at this point in the history
Allow creating a `Buffer` with no `FontSystem`
  • Loading branch information
jackpot51 authored Jun 9, 2023
2 parents a93ec8a + 2ed9c34 commit b6b0358
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,23 +324,37 @@ pub struct Buffer {
}

impl Buffer {
/// Create a new [`Buffer`] with the provided [`FontSystem`] and [`Metrics`]
/// 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(font_system: &mut FontSystem, metrics: Metrics) -> Self {
pub fn new_empty(metrics: Metrics) -> Self {
assert_ne!(metrics.line_height, 0.0, "line height cannot be 0");

let mut buffer = Self {
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
///
/// Will panic if `metrics.line_height` is zero.
pub fn new(font_system: &mut FontSystem, metrics: Metrics) -> Self {
let mut buffer = Self::new_empty(metrics);
buffer.set_text(font_system, "", Attrs::new(), Shaping::Advanced);
buffer
}
Expand Down

0 comments on commit b6b0358

Please sign in to comment.