Skip to content

Commit

Permalink
fix cursors and decorations not getting affect by modified line_height
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Dec 25, 2024
1 parent 2ec3f25 commit 77e7402
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion sugarloaf/examples/line_height.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl ApplicationHandler for Application {
"?",
FragmentStyle {
color: [0.5, 0.5, 1.0, 1.0],
background_color: Some([1.0, 1.0, 1.0, 1.0]),
background_color: Some([1.0, 0.5, 1.0, 1.0]),
cursor: Some(SugarCursor::Block([1.0, 1.0, 1.0, 1.0])),
..FragmentStyle::default()
},
Expand Down
22 changes: 16 additions & 6 deletions sugarloaf/src/components/rich_text/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl Compositor {
}),
Some(FragmentStyleDecoration::Strikethrough) => Some(RunUnderline {
enabled: true,
offset: (style.line_height / 3.5).round() as i32,
offset: (style.line_height_without_mod / 3.5).round() as i32,
size: 2.0,
color: style.decoration_color.unwrap_or(style.color),
is_doubled: false,
Expand Down Expand Up @@ -205,14 +205,24 @@ impl Compositor {
match style.cursor {
Some(SugarCursor::Block(cursor_color)) => {
self.batches.add_rect(
&Rect::new(rect.x, style.topline, rect.width, style.line_height),
&Rect::new(
rect.x,
style.topline,
rect.width,
style.line_height_without_mod,
),
depth,
&cursor_color,
);
}
Some(SugarCursor::HollowBlock(cursor_color)) => {
self.batches.add_rect(
&Rect::new(rect.x, style.topline, rect.width, style.line_height),
&Rect::new(
rect.x,
style.topline,
rect.width,
style.line_height_without_mod,
),
depth,
&cursor_color,
);
Expand All @@ -223,7 +233,7 @@ impl Compositor {
rect.x + 2.0,
style.topline + 2.0,
rect.width - 4.0,
style.line_height - 4.0,
style.line_height_without_mod - 4.0,
),
depth,
&bg_color,
Expand All @@ -232,7 +242,7 @@ impl Compositor {
}
Some(SugarCursor::Caret(cursor_color)) => {
self.batches.add_rect(
&Rect::new(rect.x, style.topline, 3.0, style.line_height),
&Rect::new(rect.x, style.topline, 3.0, style.line_height_without_mod),
depth,
&cursor_color,
);
Expand All @@ -247,7 +257,7 @@ impl Compositor {
rect.width,
style.baseline,
depth,
style.line_height,
style.line_height_without_mod,
);
}

Expand Down
16 changes: 8 additions & 8 deletions sugarloaf/src/components/rich_text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ fn draw_layout(
pos: (f32, f32),
font_library: &FontLibrary,
rect: &SugarDimensions,
custom_line_height: &f32,
line_height_mod: &f32,
graphics: &mut Graphics,
) {
// let start = std::time::Instant::now();
Expand Down Expand Up @@ -455,11 +455,10 @@ fn draw_layout(
let mut px = x + 0.0;
let baseline = line_y + ascent;
line_y = baseline + descent;
// line_y = line_y;
let line_height_without_mod = ascent + descent + leading;
let line_height = line_height_without_mod * line_height_mod;

let py = line_y;
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 All @@ -485,6 +484,7 @@ fn draw_layout(
baseline: py,
topline: py - ascent,
line_height,
line_height_without_mod,
advance: px - run_x,
decoration: run.span.decoration,
decoration_color: run.span.decoration_color,
Expand Down Expand Up @@ -528,10 +528,9 @@ fn draw_layout(
);
}

if custom_line_height > &1.0 {
line_y += line_height - line_height_calc;
if line_height_mod > &1.0 {
line_y += line_height - line_height_without_mod;
}
// line_y += line_height;
}

// let duration = start.elapsed();
Expand Down Expand Up @@ -605,6 +604,7 @@ fn fetch_dimensions(
baseline: py,
topline: py - ascent,
line_height,
line_height_without_mod: line_height,
advance: px - run_x,
decoration: None,
decoration_color: None,
Expand Down
2 changes: 2 additions & 0 deletions sugarloaf/src/components/rich_text/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub struct TextRunStyle<'a> {
pub topline: f32,
/// Absolute line height of the run.
pub line_height: f32,
/// Absolute line height of the run without mod.
pub line_height_without_mod: f32,
/// Total advance of the run.
pub advance: f32,
/// Underline style.
Expand Down

0 comments on commit 77e7402

Please sign in to comment.