Skip to content

Commit

Permalink
set_rich_text_line_height
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Dec 25, 2024
1 parent 21343e3 commit 2ec3f25
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
13 changes: 9 additions & 4 deletions sugarloaf/examples/line_height.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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();
}
}
Expand Down
22 changes: 15 additions & 7 deletions sugarloaf/src/components/rich_text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ impl RichTextBrush {
position,
library,
&rt.layout.dimensions,
&rt.layout.line_height,
graphics,
);
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions sugarloaf/src/sugarloaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions sugarloaf/src/sugarloaf/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2ec3f25

Please sign in to comment.