diff --git a/sugarloaf/examples/line_height.rs b/sugarloaf/examples/line_height.rs index 66cc7c8712..f678fbc08b 100644 --- a/sugarloaf/examples/line_height.rs +++ b/sugarloaf/examples/line_height.rs @@ -1,15 +1,18 @@ -use rio_window::keyboard::{Key, NamedKey}; use raw_window_handle::{HasDisplayHandle, HasWindowHandle}; use rio_window::application::ApplicationHandler; use rio_window::event_loop::{ActiveEventLoop, ControlFlow, DeviceEvents}; +use rio_window::keyboard::{Key, NamedKey}; use rio_window::window::{Window, WindowId}; use rio_window::{ - dpi::LogicalSize, event::{WindowEvent, ElementState}, event_loop::EventLoop, window::WindowAttributes, + dpi::LogicalSize, + event::{ElementState, WindowEvent}, + event_loop::EventLoop, + window::WindowAttributes, }; use std::error::Error; use sugarloaf::{ - layout::RootStyle, FragmentStyle, Object, RichText, - Sugarloaf, SugarCursor, SugarloafWindow, SugarloafWindowSize, + layout::RootStyle, FragmentStyle, Object, RichText, SugarCursor, Sugarloaf, + SugarloafWindow, SugarloafWindowSize, }; fn main() { @@ -127,11 +130,13 @@ impl ApplicationHandler for Application { match key_event.logical_key.as_ref() { Key::Named(NamedKey::ArrowUp) => { self.line_height += 0.1; + sugarloaf.set_rich_text_line_height(&0, self.line_height); window.request_redraw(); } Key::Named(NamedKey::ArrowDown) => { if self.line_height > 1.0 { self.line_height -= 0.1; + sugarloaf.set_rich_text_line_height(&0, self.line_height); window.request_redraw(); } } diff --git a/sugarloaf/src/components/rich_text/mod.rs b/sugarloaf/src/components/rich_text/mod.rs index c294a290fd..e24b878758 100644 --- a/sugarloaf/src/components/rich_text/mod.rs +++ b/sugarloaf/src/components/rich_text/mod.rs @@ -278,6 +278,7 @@ impl RichTextBrush { position, library, &rt.layout.dimensions, + &rt.layout.line_height, graphics, ); } @@ -412,6 +413,7 @@ fn draw_layout( pos: (f32, f32), font_library: &FontLibrary, rect: &SugarDimensions, + custom_line_height: &f32, graphics: &mut Graphics, ) { // let start = std::time::Instant::now(); @@ -448,15 +450,16 @@ fn draw_layout( let first_run = &line.render_data.runs[0]; let ascent = first_run.ascent.round(); - // let descent = first_run.descent.round(); - // let leading = (first_run.leading).round() * 2.; + let descent = first_run.descent.round(); + let leading = (first_run.leading).round() * 2.; let mut px = x + 0.0; - // let baseline = line_y + ascent; - // line_y = baseline + descent; - line_y = line_y + rect.height * 2.0; + let baseline = line_y + ascent; + line_y = baseline + descent; + // line_y = line_y; let py = line_y; - // let line_height = ascent + descent + leading; - let line_height = rect.height * 2.0; + let line_height_calc = ascent + descent + leading; + let line_height = line_height_calc * custom_line_height; + // let line_height = rect.height * line_height; for run in &line.render_data.runs { glyphs.clear(); let font = run.span.font_id; @@ -524,6 +527,11 @@ fn draw_layout( &glyphs, ); } + + if custom_line_height > &1.0 { + line_y += line_height - line_height_calc; + } + // line_y += line_height; } // let duration = start.elapsed(); diff --git a/sugarloaf/src/sugarloaf.rs b/sugarloaf/src/sugarloaf.rs index 0a6461cf17..4b278a9acb 100644 --- a/sugarloaf/src/sugarloaf.rs +++ b/sugarloaf/src/sugarloaf.rs @@ -204,6 +204,11 @@ impl Sugarloaf<'_> { .set_rich_text_font_size(rt_id, font_size, &mut self.rich_text_brush); } + #[inline] + pub fn set_rich_text_line_height(&mut self, rt_id: &usize, line_height: f32) { + self.state.set_rich_text_line_height(rt_id, line_height); + } + #[inline] pub fn update_filters(&mut self, filter_paths: &[String]) { self.filters_brush.update_filters(&self.ctx, filter_paths); diff --git a/sugarloaf/src/sugarloaf/state.rs b/sugarloaf/src/sugarloaf/state.rs index 74a811a3fa..ff7761a947 100644 --- a/sugarloaf/src/sugarloaf/state.rs +++ b/sugarloaf/src/sugarloaf/state.rs @@ -118,6 +118,18 @@ impl SugarState { self.process_rich_text_repaint(advance_brush); } + #[inline] + pub fn set_rich_text_line_height(&mut self, rich_text_id: &usize, line_height: f32) { + if let Some(rte) = self + .compositors + .advanced + .content + .get_state_mut(rich_text_id) + { + rte.layout.line_height = line_height; + } + } + fn process_rich_text_repaint(&mut self, advance_brush: &mut RichTextBrush) { for rich_text in &self.rich_text_repaint { self.compositors