diff --git a/benches/layout.rs b/benches/layout.rs index b154e0b..d41dc46 100644 --- a/benches/layout.rs +++ b/benches/layout.rs @@ -26,7 +26,7 @@ fn layout(c: &mut Criterion) { let mut run_on_text = |text: &str| { buffer.lines.clear(); - buffer.set_text(&mut fs, text, &ct::Attrs::new(), *shape); + buffer.set_text(&mut fs, text, &ct::Attrs::new(), *shape, None); buffer.shape_until_scroll(&mut fs, false); }; diff --git a/benches/text_shaping_benchmarks.rs b/benches/text_shaping_benchmarks.rs index df02d2c..723cc7f 100644 --- a/benches/text_shaping_benchmarks.rs +++ b/benches/text_shaping_benchmarks.rs @@ -16,6 +16,7 @@ fn bench_ascii_fast_path(c: &mut Criterion) { black_box(&ascii_text), &ct::Attrs::new(), ct::Shaping::Advanced, + None, ); buffer.shape_until_scroll(&mut fs, false); }); @@ -36,6 +37,7 @@ fn bench_bidi_processing(c: &mut Criterion) { black_box(&bidi_text), &ct::Attrs::new(), ct::Shaping::Advanced, + None, ); buffer.shape_until_scroll(&mut fs, false); }); @@ -56,6 +58,7 @@ fn bench_layout_heavy(c: &mut Criterion) { black_box(&layout_text), &ct::Attrs::new(), ct::Shaping::Advanced, + None, ); buffer.shape_until_scroll(&mut fs, false); }); @@ -81,6 +84,7 @@ fn bench_combined_stress(c: &mut Criterion) { black_box(&stress_text), &ct::Attrs::new(), ct::Shaping::Advanced, + None, ); buffer.shape_until_scroll(&mut fs, false); }); diff --git a/examples/multiview/src/main.rs b/examples/multiview/src/main.rs index 12713cc..e292fe9 100644 --- a/examples/multiview/src/main.rs +++ b/examples/multiview/src/main.rs @@ -31,7 +31,7 @@ fn main() { let attrs = Attrs::new().family(Family::Monospace); match fs::read_to_string(&path) { - Ok(text) => buffer.set_text(&text, &attrs, Shaping::Advanced), + Ok(text) => buffer.set_text(&text, &attrs, Shaping::Advanced, None), Err(err) => { log::error!("failed to load {:?}: {}", path, err); } diff --git a/examples/terminal/src/main.rs b/examples/terminal/src/main.rs index 286dc33..08596e7 100644 --- a/examples/terminal/src/main.rs +++ b/examples/terminal/src/main.rs @@ -36,7 +36,7 @@ fn main() { let text = std::env::args() .nth(1) .unwrap_or(" Hi, Rust! 🦀 ".to_string()); - buffer.set_text(&text, &attrs, Shaping::Advanced); + buffer.set_text(&text, &attrs, Shaping::Advanced, None); // Perform shaping as desired buffer.shape_until_scroll(true); diff --git a/src/buffer.rs b/src/buffer.rs index 9d0154c..a0f48e3 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -271,7 +271,7 @@ impl Buffer { /// 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.set_text(font_system, "", &Attrs::new(), Shaping::Advanced, None); buffer } @@ -679,6 +679,7 @@ impl Buffer { text: &str, attrs: &Attrs, shaping: Shaping, + alignment: Option, ) { self.lines.clear(); for (range, ending) in LineIter::new(text) { @@ -697,6 +698,13 @@ impl Buffer { shaping, )); } + + if alignment.is_some() { + self.lines.iter_mut().for_each(|line| { + line.set_align(alignment); + }); + } + self.scroll = Scroll::default(); self.shape_until_scroll(font_system, false); } @@ -1424,8 +1432,15 @@ impl BorrowedWithFontSystem<'_, Buffer> { } /// Set text of buffer, using provided attributes for each line by default - pub fn set_text(&mut self, text: &str, attrs: &Attrs, shaping: Shaping) { - self.inner.set_text(self.font_system, text, attrs, shaping); + pub fn set_text( + &mut self, + text: &str, + attrs: &Attrs, + shaping: Shaping, + alignment: Option, + ) { + self.inner + .set_text(self.font_system, text, attrs, shaping, alignment); } /// Set text of buffer, using an iterator of styled spans (pairs of text and attributes) diff --git a/src/edit/syntect.rs b/src/edit/syntect.rs index 8f70942..6fc041e 100644 --- a/src/edit/syntect.rs +++ b/src/edit/syntect.rs @@ -125,7 +125,7 @@ impl<'syntax_system, 'buffer> SyntaxEditor<'syntax_system, 'buffer> { let text = fs::read_to_string(path)?; self.editor.with_buffer_mut(|buffer| { - buffer.set_text(font_system, &text, &attrs, Shaping::Advanced); + buffer.set_text(font_system, &text, &attrs, Shaping::Advanced, None); }); //TODO: re-use text diff --git a/src/lib.rs b/src/lib.rs index 53c5ea5..37ae433 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,7 +36,7 @@ //! let attrs = Attrs::new(); //! //! // Add some text! -//! buffer.set_text("Hello, Rust! 🦀\n", &attrs, Shaping::Advanced); +//! buffer.set_text("Hello, Rust! 🦀\n", &attrs, Shaping::Advanced, None); //! //! // Perform shaping as desired //! buffer.shape_until_scroll(true); diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 8440070..6dae78b 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -92,7 +92,7 @@ impl DrawTestCfg { Some((self.canvas_width - margins * 2) as f32), Some((self.canvas_height - margins * 2) as f32), ); - buffer.set_text(&self.text, &self.font.as_attrs(), Shaping::Advanced); + buffer.set_text(&self.text, &self.font.as_attrs(), Shaping::Advanced, None); buffer.shape_until_scroll(true); // Black diff --git a/tests/wrap_stability.rs b/tests/wrap_stability.rs index 3bcdf29..781e357 100644 --- a/tests/wrap_stability.rs +++ b/tests/wrap_stability.rs @@ -105,7 +105,7 @@ fn wrap_extra_line() { // Add some text! buffer.set_wrap(Wrap::Word); - buffer.set_text("Lorem ipsum dolor sit amet, qui minim labore adipisicing\n\nweeewoooo minim sint cillum sint consectetur cupidatat.", &Attrs::new().family(cosmic_text::Family::Name("Inter")), Shaping::Advanced); + buffer.set_text("Lorem ipsum dolor sit amet, qui minim labore adipisicing\n\nweeewoooo minim sint cillum sint consectetur cupidatat.", &Attrs::new().family(cosmic_text::Family::Name("Inter")), Shaping::Advanced, None); // Set a size for the text buffer, in pixels buffer.set_size(Some(50.0), Some(1000.0)); diff --git a/tests/wrap_word_fallback.rs b/tests/wrap_word_fallback.rs index 0b3c664..db7f80a 100644 --- a/tests/wrap_word_fallback.rs +++ b/tests/wrap_word_fallback.rs @@ -15,7 +15,7 @@ fn wrap_word_fallback() { let mut buffer = buffer.borrow_with(&mut font_system); buffer.set_wrap(Wrap::WordOrGlyph); - buffer.set_text("Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat.", &Attrs::new().family(cosmic_text::Family::Name("Inter")), Shaping::Advanced); + buffer.set_text("Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat.", &Attrs::new().family(cosmic_text::Family::Name("Inter")), Shaping::Advanced, None); buffer.set_size(Some(50.0), Some(1000.0)); buffer.shape_until_scroll(false);